Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading