Orion Cygnus
Orion Cygnus

Reputation: 198

Issue with speaker - WebRTC & iOS

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.

Upvotes: 4

Views: 3870

Answers (1)

Orion Cygnus
Orion Cygnus

Reputation: 198

First I thought of deleting this question, then thought if someone stumbled upon it like me, so:

The following solved the issue with being unable to access at times.

self.rtcAudioSession.setCategory(AVAudioSession.Category.playAndRecord.rawValue, with: [.defaultToSpeaker, .allowBluetoothA2DP, .allowAirPlay, .allowBluetooth])

As for the sound coming out of both earpiece and the loudspeaker, I tried with multiple VoIP apps, it behaves the same. Looks like it is how apple wants it to work, so nothing to interfere with that.

Upvotes: 7

Related Questions