Skip to content

Commit

Permalink
Merge branch 'main' into hiroshi/change-add-remove-videorenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie authored Sep 23, 2024
2 parents 8979088 + 2bc3d6b commit 6531440
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 58 deletions.
38 changes: 22 additions & 16 deletions Sources/LiveKit/Track/AudioManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public class AudioManager: Loggable {

#if os(iOS) || os(visionOS) || os(tvOS)

public typealias ConfigureAudioSessionFunc = (_ newState: State,
_ oldState: State) -> Void
public typealias ConfigureAudioSessionFunc = @Sendable (_ newState: State,
_ oldState: State) -> Void

/// Use this to provide a custom function to configure the audio session, overriding the default behavior
/// provided by ``defaultConfigureAudioSessionFunc(newState:oldState:)``.
Expand Down Expand Up @@ -119,7 +119,7 @@ public class AudioManager: Loggable {
case localAndRemote
}

public struct State: Equatable {
public struct State: Equatable, Sendable {
// Only consider State mutated when public vars change
public static func == (lhs: AudioManager.State, rhs: AudioManager.State) -> Bool {
var isEqual = lhs.localTracksCount == rhs.localTracksCount &&
Expand Down Expand Up @@ -226,34 +226,40 @@ public class AudioManager: Loggable {

// MARK: - Private

// Singleton
private init() {
// trigger events when state mutates
state.onDidMutate = { [weak self] newState, oldState in
guard let self else { return }
// Return if state is equal.
guard newState != oldState else { return }
private let _configureRunner = SerialRunnerActor<Void>()

#if os(iOS) || os(visionOS) || os(tvOS)
private func _asyncConfigure(newState: State, oldState: State) async throws {
try await _configureRunner.run {
self.log("\(oldState) -> \(newState)")
#if os(iOS) || os(visionOS) || os(tvOS)
let configureFunc = newState.customConfigureFunc ?? self.defaultConfigureAudioSessionFunc
configureFunc(newState, oldState)
#endif
}
}
#endif

func trackDidStart(_ type: Type) {
state.mutate { state in
func trackDidStart(_ type: Type) async throws {
let (newState, oldState) = state.mutate { state in
let oldState = state
if type == .local { state.localTracksCount += 1 }
if type == .remote { state.remoteTracksCount += 1 }
return (state, oldState)
}
#if os(iOS) || os(visionOS) || os(tvOS)
try await _asyncConfigure(newState: newState, oldState: oldState)
#endif
}

func trackDidStop(_ type: Type) {
state.mutate { state in
func trackDidStop(_ type: Type) async throws {
let (newState, oldState) = state.mutate { state in
let oldState = state
if type == .local { state.localTracksCount = max(state.localTracksCount - 1, 0) }
if type == .remote { state.remoteTracksCount = max(state.remoteTracksCount - 1, 0) }
return (state, oldState)
}
#if os(iOS) || os(visionOS) || os(tvOS)
try await _asyncConfigure(newState: newState, oldState: oldState)
#endif
}

#if os(iOS) || os(visionOS) || os(tvOS)
Expand Down
4 changes: 2 additions & 2 deletions Sources/LiveKit/Track/Local/LocalAudioTrack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ public class LocalAudioTrack: Track, LocalTrack, AudioTrack {
// MARK: - Internal

override func startCapture() async throws {
AudioManager.shared.trackDidStart(.local)
try await AudioManager.shared.trackDidStart(.local)
}

override func stopCapture() async throws {
AudioManager.shared.trackDidStop(.local)
try await AudioManager.shared.trackDidStop(.local)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/LiveKit/Track/Remote/RemoteAudioTrack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public class RemoteAudioTrack: Track, RemoteTrack, AudioTrack {
// MARK: - Internal

override func startCapture() async throws {
AudioManager.shared.trackDidStart(.remote)
try await AudioManager.shared.trackDidStart(.remote)
}

override func stopCapture() async throws {
AudioManager.shared.trackDidStop(.remote)
try await AudioManager.shared.trackDidStop(.remote)
}
}

Expand Down
61 changes: 25 additions & 36 deletions Sources/LiveKit/Types/Statistics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,14 @@ public class Statistics: NSObject, Identifiable {
public let id: String
public let type: StatisticsType
public let timestamp: Double
public let rawValues: [String: NSObject]

init?(id: String,
type: StatisticsType,
timestamp: Double,
rawValues: [String: NSObject])
timestamp: Double)
{
self.id = id
self.type = type
self.timestamp = timestamp
self.rawValues = rawValues
}
}

Expand All @@ -149,8 +146,7 @@ public class CodecStatistics: Statistics {

super.init(id: id,
type: .codec,
timestamp: timestamp,
rawValues: rawValues)
timestamp: timestamp)
}
}

Expand All @@ -168,8 +164,7 @@ public class MediaSourceStatistics: Statistics {

super.init(id: id,
type: .mediaSource,
timestamp: timestamp,
rawValues: rawValues)
timestamp: timestamp)
}
}

Expand All @@ -180,10 +175,10 @@ public class RtpStreamStatistics: Statistics {
public let transportId: String?
public let codecId: String?

override init?(id: String,
type: StatisticsType,
timestamp: Double,
rawValues: [String: NSObject])
init?(id: String,
type: StatisticsType,
timestamp: Double,
rawValues: [String: NSObject])
{
ssrc = rawValues.readOptional("ssrc")
kind = rawValues.readOptional("kind")
Expand All @@ -192,8 +187,7 @@ public class RtpStreamStatistics: Statistics {

super.init(id: id,
type: type,
timestamp: timestamp,
rawValues: rawValues)
timestamp: timestamp)
}
}

Expand All @@ -220,8 +214,7 @@ public class AudioPlayoutStatistics: Statistics {

super.init(id: id,
type: .mediaPlayout,
timestamp: timestamp,
rawValues: rawValues)
timestamp: timestamp)
}
}

Expand All @@ -240,8 +233,7 @@ public class PeerConnectionStatistics: Statistics {

super.init(id: id,
type: .peerConnection,
timestamp: timestamp,
rawValues: rawValues)
timestamp: timestamp)
}
}

Expand All @@ -262,7 +254,7 @@ public class DataChannelStatistics: Statistics {
rawValues: [String: NSObject])
{
label = rawValues.readOptional("label")
self.protocol = rawValues.readOptional("protocol")
`protocol` = rawValues.readOptional("protocol")
dataChannelIdentifier = rawValues.readOptional("dataChannelIdentifier")
state = DataChannelState(rawValue: rawValues.readNonOptional("state"))
messagesSent = rawValues.readOptional("messagesSent")
Expand All @@ -272,8 +264,7 @@ public class DataChannelStatistics: Statistics {

super.init(id: id,
type: .dataChannel,
timestamp: timestamp,
rawValues: rawValues)
timestamp: timestamp)
}
}

Expand Down Expand Up @@ -320,8 +311,7 @@ public class TransportStatistics: Statistics {

super.init(id: id,
type: .transport,
timestamp: timestamp,
rawValues: rawValues)
timestamp: timestamp)
}
}

Expand All @@ -342,15 +332,15 @@ public class IceCandidateStatistics: Statistics {
public let usernameFragment: String?
public let tcpType: IceTcpCandidateType?

override init?(id: String,
type: StatisticsType,
timestamp: Double,
rawValues: [String: NSObject])
init?(id: String,
type: StatisticsType,
timestamp: Double,
rawValues: [String: NSObject])
{
transportId = rawValues.readOptional("transportId")
address = rawValues.readOptional("address")
port = rawValues.readOptional("port")
self.protocol = rawValues.readOptional("protocol")
`protocol` = rawValues.readOptional("protocol")
candidateType = IceCandidateType(rawValue: rawValues.readNonOptional("candidateType"))
priority = rawValues.readOptional("priority")
url = rawValues.readOptional("url")
Expand All @@ -363,8 +353,7 @@ public class IceCandidateStatistics: Statistics {

super.init(id: id,
type: type,
timestamp: timestamp,
rawValues: rawValues)
timestamp: timestamp)
}
}

Expand Down Expand Up @@ -449,8 +438,7 @@ public class IceCandidatePairStatistics: Statistics {

super.init(id: id,
type: .candidatePair,
timestamp: timestamp,
rawValues: rawValues)
timestamp: timestamp)
}
}

Expand All @@ -473,8 +461,7 @@ public class CertificateStatistics: Statistics {

super.init(id: id,
type: .certificate,
timestamp: timestamp,
rawValues: rawValues)
timestamp: timestamp)
}
}

Expand Down Expand Up @@ -575,7 +562,8 @@ public class InboundRtpStreamStatistics: ReceivedRtpStreamStatistics {
public let retransmittedPacketsReceived: UInt64?
public let retransmittedBytesReceived: UInt64?

public let previous: InboundRtpStreamStatistics?
// Weak reference to previous stat so we can compare later.
public weak var previous: InboundRtpStreamStatistics?

init?(id: String,
timestamp: Double,
Expand Down Expand Up @@ -720,7 +708,8 @@ public class OutboundRtpStreamStatistics: SentRtpStreamStatistics {
public let active: Bool?
public let scalabilityMode: String?

public let previous: OutboundRtpStreamStatistics?
// Weak reference to previous stat so we can compare later.
public weak var previous: OutboundRtpStreamStatistics?

init?(id: String,
timestamp: Double,
Expand Down
4 changes: 2 additions & 2 deletions Tests/LiveKitTests/TrackTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class TestTrack: LocalAudioTrack {

override func startCapture() async throws {
try? await Task.sleep(nanoseconds: UInt64(Double.random(in: 0.0 ... 1.0) * 1_000_000))
AudioManager.shared.trackDidStart(.local)
try await AudioManager.shared.trackDidStart(.local)
}

override func stopCapture() async throws {
try? await Task.sleep(nanoseconds: UInt64(Double.random(in: 0.0 ... 1.0) * 1_000_000))
AudioManager.shared.trackDidStop(.local)
try await AudioManager.shared.trackDidStop(.local)
}
}

Expand Down

0 comments on commit 6531440

Please sign in to comment.