From 6b983784f8b59c475e5fa328721f70370bd0e7aa Mon Sep 17 00:00:00 2001 From: hiroshihorie <548776+hiroshihorie@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:52:52 +0700 Subject: [PATCH 1/4] Set debug level --- Sources/LiveKit/Core/RTC.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/LiveKit/Core/RTC.swift b/Sources/LiveKit/Core/RTC.swift index 055c5b1ec..362e863a3 100644 --- a/Sources/LiveKit/Core/RTC.swift +++ b/Sources/LiveKit/Core/RTC.swift @@ -83,6 +83,8 @@ class RTC { static let audioSenderCapabilities = peerConnectionFactory.rtpSenderCapabilities(forKind: kRTCMediaStreamTrackKindAudio) static let peerConnectionFactory: LKRTCPeerConnectionFactory = { + RTCSetMinDebugLogLevel(.verbose) + logger.log("Initializing SSL...", type: Room.self) RTCInitializeSSL() From 35ef24533f1e795999a21cf58bd9c0654f2a771a Mon Sep 17 00:00:00 2001 From: hiroshihorie <548776+hiroshihorie@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:53:05 +0700 Subject: [PATCH 2/4] No audio devices for now --- Sources/LiveKit/Track/AudioManager.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/LiveKit/Track/AudioManager.swift b/Sources/LiveKit/Track/AudioManager.swift index f597d71b3..d1c4d1b30 100644 --- a/Sources/LiveKit/Track/AudioManager.swift +++ b/Sources/LiveKit/Track/AudioManager.swift @@ -189,11 +189,13 @@ public class AudioManager: Loggable { public let defaultInputDevice = AudioDevice(ioDevice: LKRTCIODevice.defaultDevice(with: .input)) public var outputDevices: [AudioDevice] { - RTC.audioDeviceModule.outputDevices.map { AudioDevice(ioDevice: $0) } + [] + // RTC.audioDeviceModule.outputDevices.map { AudioDevice(ioDevice: $0) } } public var inputDevices: [AudioDevice] { - RTC.audioDeviceModule.inputDevices.map { AudioDevice(ioDevice: $0) } + [] + // RTC.audioDeviceModule.inputDevices.map { AudioDevice(ioDevice: $0) } } public var outputDevice: AudioDevice { From 84821ecc31a69d7b488ea40bff3e060823fa52a0 Mon Sep 17 00:00:00 2001 From: hiroshihorie <548776+hiroshihorie@users.noreply.github.com> Date: Tue, 10 Dec 2024 12:53:03 +0700 Subject: [PATCH 3/4] Update audio manager --- Sources/LiveKit/Track/AudioManager.swift | 113 ++++++++--------------- 1 file changed, 36 insertions(+), 77 deletions(-) diff --git a/Sources/LiveKit/Track/AudioManager.swift b/Sources/LiveKit/Track/AudioManager.swift index d1c4d1b30..c7fb47d47 100644 --- a/Sources/LiveKit/Track/AudioManager.swift +++ b/Sources/LiveKit/Track/AudioManager.swift @@ -59,6 +59,29 @@ public class LKAudioBuffer: NSObject { // Audio Session Configuration related public class AudioManager: Loggable { + #if os(iOS) + class AudioSessionDelegateObserver: NSObject, Loggable, LKRTCAudioSessionDelegate { + func audioSessionDidStartPlayOrRecord(_: LKRTCAudioSession) { + log() + } + + func audioSession(_: LKRTCAudioSession, audioEngineWillUpdateStateWithOutputEnabled isOutputEnabled: Bool, inputEnabled isInputEnabled: Bool) { + log("isOutputEnabled: \(isOutputEnabled), isInputEnabled: \(isInputEnabled)") + + // Configure audio session + let config = LKRTCAudioSessionConfiguration.webRTC() + config.category = AVAudioSession.Category.playAndRecord.rawValue + config.categoryOptions = [.allowBluetooth, .allowBluetoothA2DP, .allowAirPlay] + config.mode = AVAudioSession.Mode.videoChat.rawValue + LKRTCAudioSessionConfiguration.setWebRTC(config) + } + + func audioSessionDidStopPlayOrRecord(_: LKRTCAudioSession) { + log() + } + } + #endif + // MARK: - Public #if compiler(>=6.0) @@ -226,102 +249,38 @@ public class AudioManager: Loggable { let state = StateSync(State()) - // MARK: - Private - - private let _configureRunner = SerialRunnerActor() + #if os(iOS) + let _audioSessionDelegateObserver = AudioSessionDelegateObserver() + init() { + LKRTCAudioSession.sharedInstance().add(_audioSessionDelegateObserver) + } - #if os(iOS) || os(visionOS) || os(tvOS) - private func _asyncConfigure(newState: State, oldState: State) async throws { - try await _configureRunner.run { - self.log("\(oldState) -> \(newState)") - let configureFunc = newState.customConfigureFunc ?? self.defaultConfigureAudioSessionFunc - configureFunc(newState, oldState) - } + deinit { + LKRTCAudioSession.sharedInstance().remove(_audioSessionDelegateObserver) } #endif + // MARK: - Private + + private let _configureRunner = SerialRunnerActor() + func trackDidStart(_ type: Type) async throws { - let (newState, oldState) = state.mutate { state in + 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) async throws { - let (newState, oldState) = state.mutate { state in + 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) - /// The default implementation when audio session configuration is requested by the SDK. - /// Configure the `RTCAudioSession` of `WebRTC` framework. - /// - /// > Note: It is recommended to use `RTCAudioSessionConfiguration.webRTC()` to obtain an instance of `RTCAudioSessionConfiguration` instead of instantiating directly. - /// - /// - Parameters: - /// - configuration: A configured RTCAudioSessionConfiguration - /// - setActive: passing true/false will call `AVAudioSession.setActive` internally - public func defaultConfigureAudioSessionFunc(newState: State, oldState: State) { - // Lazily computed config - let computeConfiguration: (() -> AudioSessionConfiguration) = { - switch newState.trackState { - case .none: - // Use .soloAmbient configuration - return .soloAmbient - case .remoteOnly where newState.isSpeakerOutputPreferred: - // Use .playback configuration with spoken audio - return .playback - default: - // Use .playAndRecord configuration - return newState.isSpeakerOutputPreferred ? .playAndRecordSpeaker : .playAndRecordReceiver - } - } - - let configuration = newState.sessionConfiguration ?? computeConfiguration() - - var setActive: Bool? - if newState.trackState != .none, oldState.trackState == .none { - // activate audio session when there is any local/remote audio track - setActive = true - } else if newState.trackState == .none, oldState.trackState != .none { - // deactivate audio session when there are no more local/remote audio tracks - setActive = false - } - - let session = LKRTCAudioSession.sharedInstance() - // Check if needs setConfiguration - guard configuration != session.toAudioSessionConfiguration() else { - log("Skipping configure audio session, no changes") - return - } - - session.lockForConfiguration() - defer { session.unlockForConfiguration() } - - do { - log("Configuring audio session: \(String(describing: configuration))") - if let setActive { - try session.setConfiguration(configuration.toRTCType(), active: setActive) - } else { - try session.setConfiguration(configuration.toRTCType()) - } - } catch { - log("Failed to configure audio session with error: \(error)", .error) - } } - #endif } public extension AudioManager { From c5f4154c97ae06f2884e1add4345bb3ab77e57aa Mon Sep 17 00:00:00 2001 From: hiroshihorie <548776+hiroshihorie@users.noreply.github.com> Date: Tue, 10 Dec 2024 13:24:44 +0700 Subject: [PATCH 4/4] Use exp1 lib --- Package.swift | 2 +- Package@swift-5.9.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index bf7183a9a..ef24d3e43 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ let package = Package( ], dependencies: [ // LK-Prefixed Dynamic WebRTC XCFramework - .package(url: "https://github.com/livekit/webrtc-xcframework.git", exact: "125.6422.11"), + .package(url: "https://github.com/livekit/webrtc-xcframework.git", exact: "125.6422.12-exp.1"), .package(url: "https://github.com/apple/swift-protobuf.git", from: "1.26.0"), .package(url: "https://github.com/apple/swift-log.git", from: "1.5.4"), // Only used for DocC generation diff --git a/Package@swift-5.9.swift b/Package@swift-5.9.swift index 37e1c664d..233065abe 100644 --- a/Package@swift-5.9.swift +++ b/Package@swift-5.9.swift @@ -20,7 +20,7 @@ let package = Package( ], dependencies: [ // LK-Prefixed Dynamic WebRTC XCFramework - .package(url: "https://github.com/livekit/webrtc-xcframework.git", exact: "125.6422.11"), + .package(url: "https://github.com/livekit/webrtc-xcframework.git", exact: "125.6422.12-exp.1"), .package(url: "https://github.com/apple/swift-protobuf.git", from: "1.26.0"), .package(url: "https://github.com/apple/swift-log.git", from: "1.5.4"), // Only used for DocC generation