I am trying to make a video chat app with webRTC using google's webRTC pod. The issue I am facing is while trying to use the loudspeaker for sound output. If accessing loudspeaker is successful, sound starts to come from all the speakers, including the one on the top and the loud speaker; and there is a lot of echo and noise. Sometimes though, it could not access the speaker at all and I see this error:
Error Domain=NSOSStatusErrorDomain Code=-50
I have used the webRTC part mostly from this repo: https://github.com/lg-tawsenior/WebRTC-iOS/blob/master/WebRTC-Demo-App/Sources/Services/WebRTCClient.swift
The function which tries to output through speaker is:
func speakerOn() {
self.audioQueue.async { [weak self] in
guard let self = self else {
return
}
self.rtcAudioSession.lockForConfiguration()
do {
try self.rtcAudioSession.setCategory(AVAudioSession.Category.playAndRecord.rawValue)
try self.rtcAudioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
try self.rtcAudioSession.setActive(true)
} catch let error {
debugPrint("Couldn't force audio to speaker: \(error)")
}
self.rtcAudioSession.unlockForConfiguration()
}
}
I have tried setting rtcAudioSession inactive, but that is not possible because there is I/O going on. I have tried to pause all the remote tracks in the webRTC transceivers, no luck.
Thanks a lot in advance for any help.