[iOS App] App Crash during toggle between Speaker to bluetooth headphone.

161 views
Skip to first unread message

Omkar Keluskar

unread,
Aug 8, 2022, 1:45:11 AM8/8/22
to discuss-webrtc

We have developed an iOS app using WebRTC for video communication. We have the option to toggle between headphone and speaker.

We are facing an app crash when bluetooth headphone is connected and the user tries to toggle between speaker and headphone continuously.

Following is the crash :
Fatal Exception: com.apple.coreaudio.avfaudio
required condition is false: nil == owningEngine || GetEngine() == owningEngine


Following are the code snippet :
func headphoneOn() -> Bool {

        audioSession.lockForConfiguration()

        var bluetoothPort : AVAudioSessionPortDescription? = nil

        var headphonePort : AVAudioSessionPortDescription? = nil

        var inbuiltMicPort : AVAudioSessionPortDescription? = nil

        

        if let availableInputs = AVAudioSession.sharedInstance().availableInputs {

            for inputDevice in availableInputs {

                if inputDevice.portType == .builtInMic {

                    inbuiltMicPort = inputDevice

                } else if inputDevice.portType == .headsetMic  || inputDevice.portType == .headphones {

                    headphonePort = inputDevice

                } else if inputDevice.portType == .bluetoothHFP || inputDevice.portType == .bluetoothA2DP || inputDevice.portType == .bluetoothLE {

                    bluetoothPort = inputDevice

                }

            }

        }

        

        do {

            try audioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.none)

            if let bluetoothPort = bluetoothPort {

                try audioSession.setPreferredInput(bluetoothPort)

                try audioSession.setMode(AVAudioSession.Mode.voiceChat.rawValue)

                audioSession.unlockForConfiguration()

                return true

            } else if let headphonePort = headphonePort {

                try audioSession.setPreferredInput(headphonePort)

                try audioSession.setMode(AVAudioSession.Mode.voiceChat.rawValue)

                audioSession.unlockForConfiguration()

                return true

            } else if let inbuiltMicPort = inbuiltMicPort {

                try audioSession.setMode(AVAudioSession.Mode.default.rawValue)

                try audioSession.setPreferredInput(inbuiltMicPort)

                audioSession.unlockForConfiguration()

                return true

            } else {

                audioSession.unlockForConfiguration()

                return false

            }

            

        } catch let error as NSError {

            debugPrint("#configureAudioSessionToSpeaker Error \(error.localizedDescription)")

            audioSession.unlockForConfiguration()

            return false

        }

        

    }


func speakerOn() -> Bool {

        audioSession.lockForConfiguration()

        var inbuiltSpeakerPort : AVAudioSessionPortDescription? = nil

        

        if let availableInputs = AVAudioSession.sharedInstance().availableInputs {

            for inputDevice in availableInputs {

                if inputDevice.portType == .builtInSpeaker {

                    inbuiltSpeakerPort = inputDevice

                }

            }

        }

        do { ///Audio Session: Set on Speaker

            if let inbuiltSpeakerPort = inbuiltSpeakerPort {

                try audioSession.setPreferredInput(inbuiltSpeakerPort)

            }

            try audioSession.setMode(AVAudioSession.Mode.videoChat.rawValue)

            try audioSession.overrideOutputAudioPort(.speaker)

            audioSession.unlockForConfiguration()

            return true

            

        } catch let error as NSError {

            debugPrint("#configureAudioSessionToSpeaker Error \(error.localizedDescription)")

            audioSession.unlockForConfiguration()

            return false

            

        }

    }

Any help will be appreciated to solve this crash issue.

guest271314

unread,
Aug 8, 2022, 8:21:19 PM8/8/22
to discuss-webrtc
AFAIK WebRTC does not support capture of speakers out of the box on Chrome/Chromium. 

Are you sure you are able to capture speakers on iOS at all? If so, kindly confirm.
Reply all
Reply to author
Forward
0 new messages