Skip to content
Draft
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
33 changes: 23 additions & 10 deletions packages/react-native-sdk/ios/StreamInCallManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum DefaultAudioDevice {
@objc(StreamInCallManager)
class StreamInCallManager: RCTEventEmitter {

private let audioSessionQueue = DispatchQueue(label: "io.getstream.rn.audioSessionQueue")
private let audioSessionQueue = DispatchQueue(label: "io.getstream.rn.audioSessionQueue", qos: .userInitiated)

private var audioManagerActivated = false
private var callAudioRole: CallAudioRole = .communicator
Expand Down Expand Up @@ -118,13 +118,23 @@ class StreamInCallManager: RCTEventEmitter {

if (defaultAudioDevice == .speaker) {
// defaultToSpeaker will route to speaker if nothing else is connected
intendedOptions = [.allowBluetooth, .defaultToSpeaker]
intendedOptions = [.allowBluetoothHFP, .defaultToSpeaker]
} else {
// having no defaultToSpeaker makes sure audio goes to earpiece if nothing is connected
intendedOptions = [.allowBluetooth]
intendedOptions = [.allowBluetoothHFP]
}
}

// STEP 1: Configure iOS native audio session FIRST (this does the pre-warming so that webrtc worker thread isnt stalled on audio unit initialisation)
let nativeSession = AVAudioSession.sharedInstance()
do {
try nativeSession.setCategory(intendedCategory, mode: intendedMode, options: intendedOptions)
try nativeSession.setActive(true)
log("configureAudioSession: Native AVAudioSession configured successfully")
} catch {
log("configureAudioSession: Failed to configure native session: \(error.localizedDescription)")
}

// START: set the config that webrtc must use when it takes control
let rtcConfig = RTCAudioSessionConfiguration.webRTC()
rtcConfig.category = intendedCategory.rawValue
Expand All @@ -133,6 +143,7 @@ class StreamInCallManager: RCTEventEmitter {
RTCAudioSessionConfiguration.setWebRTC(rtcConfig)
// END


// START: compare current audio session with intended, and update if different
let session = RTCAudioSession.sharedInstance()
let currentCategory = session.category
Expand All @@ -142,6 +153,7 @@ class StreamInCallManager: RCTEventEmitter {

if currentCategory != intendedCategory.rawValue || currentMode != intendedMode.rawValue || currentOptions != intendedOptions || !currentIsActive {
session.lockForConfiguration()
defer { session.unlockForConfiguration() }
do {
try session.setCategory(intendedCategory, mode: intendedMode, options: intendedOptions)
try session.setActive(true)
Expand All @@ -156,7 +168,6 @@ class StreamInCallManager: RCTEventEmitter {
log("configureAudioSession: Error setting mode: \(error.localizedDescription)")
}
}
session.unlockForConfiguration()
} else {
log("configureAudioSession: no change needed")
}
Expand All @@ -180,12 +191,14 @@ class StreamInCallManager: RCTEventEmitter {

@objc(setForceSpeakerphoneOn:)
func setForceSpeakerphoneOn(enable: Bool) {
let session = AVAudioSession.sharedInstance()
do {
try session.overrideOutputAudioPort(enable ? .speaker : .none)
try session.setActive(true)
} catch {
log("Error setting speakerphone: \(error)")
audioSessionQueue.async {
let session = AVAudioSession.sharedInstance()
do {
try session.overrideOutputAudioPort(enable ? .speaker : .none)
try session.setActive(true)
} catch {
self.log("Error setting speakerphone: \(error)")
}
}
}

Expand Down
Loading