Skip to content

Commit f6f2978

Browse files
authored
Flag to disable automatic audio session configuration (#711)
Related: #695
1 parent 930c962 commit f6f2978

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

Sources/LiveKit/Audio/AudioSessionEngineObserver.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,29 @@ internal import LiveKitWebRTC
2626
@_implementationOnly import LiveKitWebRTC
2727
#endif
2828

29+
/// An ``AudioEngineObserver`` that configures the `AVAudioSession` based on the state of the audio engine.
2930
public class AudioSessionEngineObserver: AudioEngineObserver, Loggable, @unchecked Sendable {
31+
/// Controls automatic configuration of the `AVAudioSession` based on audio engine state.
32+
///
33+
/// - When `true`: The `AVAudioSession` is automatically configured based on the audio engine state
34+
/// - When `false`: Manual configuration of the `AVAudioSession` is required
35+
///
36+
/// > Note: It is recommended to set this value before connecting to a room.
37+
///
38+
/// Default value: `true`
39+
public var isAutomaticConfigurationEnabled: Bool {
40+
get { _state.isAutomaticConfigurationEnabled }
41+
set { _state.mutate { $0.isAutomaticConfigurationEnabled = newValue } }
42+
}
43+
44+
/// Controls the speaker output preference for audio routing.
45+
///
46+
/// - When `true`: The speaker output is preferred over the receiver output
47+
/// - When `false`: The receiver output is preferred over the speaker output
48+
///
49+
/// > Note: This value is only used when `isAutomaticConfigurationEnabled` is `true`.
50+
///
51+
/// Default value: `true`
3052
public var isSpeakerOutputPreferred: Bool {
3153
get { _state.isSpeakerOutputPreferred }
3254
set { _state.mutate { $0.isSpeakerOutputPreferred = newValue } }
@@ -35,6 +57,7 @@ public class AudioSessionEngineObserver: AudioEngineObserver, Loggable, @uncheck
3557
struct State: Sendable {
3658
var next: (any AudioEngineObserver)?
3759

60+
var isAutomaticConfigurationEnabled: Bool = true
3861
var isPlayoutEnabled: Bool = false
3962
var isRecordingEnabled: Bool = false
4063
var isSpeakerOutputPreferred: Bool = true
@@ -49,7 +72,7 @@ public class AudioSessionEngineObserver: AudioEngineObserver, Loggable, @uncheck
4972

5073
public init() {
5174
_state.onDidMutate = { new_, old_ in
52-
if new_.isPlayoutEnabled != old_.isPlayoutEnabled ||
75+
if new_.isAutomaticConfigurationEnabled, new_.isPlayoutEnabled != old_.isPlayoutEnabled ||
5376
new_.isRecordingEnabled != old_.isRecordingEnabled ||
5477
new_.isSpeakerOutputPreferred != old_.isSpeakerOutputPreferred
5578
{

Sources/LiveKit/Audio/Manager/AudioManager.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class AudioManager: Loggable {
5858
/// - ``isSpeakerOutputPreferred``
5959
///
6060
/// If you want to revert to default behavior, set this to `nil`.
61-
@available(*, deprecated, message: "Use `set(engineObservers:)` instead. See `DefaultAudioSessionObserver` for example.")
61+
@available(*, deprecated, message: "Use `set(engineObservers:)` instead. See `AudioSessionEngineObserver` for example.")
6262
public var customConfigureAudioSessionFunc: ConfigureAudioSessionFunc? {
6363
get { _state.customConfigureFunc }
6464
set { _state.mutate { $0.customConfigureFunc = newValue } }
@@ -314,7 +314,7 @@ public class AudioManager: Loggable {
314314
}
315315

316316
/// Set a chain of ``AudioEngineObserver``s.
317-
/// Defaults to having a single ``DefaultAudioSessionObserver`` initially.
317+
/// Defaults to having a single ``AudioSessionEngineObserver`` initially.
318318
///
319319
/// The first object will be invoked and is responsible for calling the next object.
320320
/// See ``NextInvokable`` protocol for details.
@@ -345,6 +345,9 @@ public class AudioManager: Loggable {
345345
public let mixer = MixerEngineObserver()
346346

347347
#if os(iOS) || os(visionOS) || os(tvOS)
348+
/// Configures the `AVAudioSession` based on the audio engine's state.
349+
/// Set `AudioManager.shared.audioSession.isAutomaticConfigurationEnabled` to `false` to manually configure the `AVAudioSession` instead.
350+
/// > Note: It is recommended to set this before connecting to a room.
348351
public let audioSession = AudioSessionEngineObserver()
349352
#endif
350353

0 commit comments

Comments
 (0)