Skip to content

Commit

Permalink
Make customConfigureAudioSessionFunc thread safe (livekit#253)
Browse files Browse the repository at this point in the history
* public states

* impl

* ref

* doc
  • Loading branch information
hiroshihorie committed Sep 24, 2023
1 parent 1cd132c commit 697e432
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 69 deletions.
45 changes: 0 additions & 45 deletions Sources/LiveKit/Extensions/LiveKit+Deprecated.swift

This file was deleted.

49 changes: 25 additions & 24 deletions Sources/LiveKit/Track/AudioManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public class AudioManager: Loggable {

/// Use this to provide a custom func to configure the audio session instead of ``defaultConfigureAudioSessionFunc(newState:oldState:)``.
/// This method should not block and is expected to return immediately.
public var customConfigureAudioSessionFunc: ConfigureAudioSessionFunc?
public var customConfigureAudioSessionFunc: ConfigureAudioSessionFunc? {
get { _state.customConfigureFunc }
set { _state.mutate { $0.customConfigureFunc = newValue } }
}

public enum TrackState {
case none
Expand All @@ -39,9 +42,20 @@ public class AudioManager: Loggable {
}

public struct State: Equatable {
var localTracksCount: Int = 0
var remoteTracksCount: Int = 0
var preferSpeakerOutput: Bool = true

// Only consider State mutated when public vars change
public static func == (lhs: AudioManager.State, rhs: AudioManager.State) -> Bool {
lhs.localTracksCount == rhs.localTracksCount &&
lhs.remoteTracksCount == rhs.remoteTracksCount &&
lhs.preferSpeakerOutput == rhs.preferSpeakerOutput
}

// Keep this var within State so it's protected by UnfairLock
internal var customConfigureFunc: ConfigureAudioSessionFunc?

public var localTracksCount: Int = 0
public var remoteTracksCount: Int = 0
public var preferSpeakerOutput: Bool = true

public var trackState: TrackState {

Expand Down Expand Up @@ -84,12 +98,14 @@ public class AudioManager: Loggable {
// trigger events when state mutates
_state.onDidMutate = { [weak self] newState, oldState in
guard let self = self else { return }
self.configureAudioSession(newState: newState, oldState: oldState)
}
}

deinit {
//
log("\(oldState) -> \(newState)")

#if os(iOS)
let configureFunc = newState.customConfigureFunc ?? defaultConfigureAudioSessionFunc
configureFunc(newState, oldState)
#endif
}
}

internal func trackDidStart(_ type: Type) {
Expand All @@ -108,21 +124,6 @@ public class AudioManager: Loggable {
}
}

private func configureAudioSession(newState: State, oldState: State) {

log("\(oldState) -> \(newState)")

#if os(iOS)
if let _deprecatedFunc = LiveKit.onShouldConfigureAudioSession {
_deprecatedFunc(newState.trackState, oldState.trackState)
} else if let customConfigureAudioSessionFunc = customConfigureAudioSessionFunc {
customConfigureAudioSessionFunc(newState, oldState)
} else {
defaultConfigureAudioSessionFunc(newState: newState, oldState: oldState)
}
#endif
}

#if os(iOS)
/// The default implementation when audio session configuration is requested by the SDK.
/// Configure the `RTCAudioSession` of `WebRTC` framework.
Expand Down

0 comments on commit 697e432

Please sign in to comment.