在适用于iOS的webrtc上启用立体声声音

时间:2016-02-08 23:55:45

标签: ios swift webrtc libjingle

我在iOS上使用Opus编解码器进行webrtc音频流(libjingle_peerconnection)。如何为音频播放启用立体声?

我从这篇博文中借用了一些想法,希望我可以让它发挥作用。我们能够为我们的网络客户端启用立体声,而不是我们的iOS客户端。

https://www.webrtcexample.com/blog/?go=all/how-to-support-stereo-in-a-webrtc-application/

我在提供和对等连接约束的约束中禁用回声消除,如下所示:

private func initializeConstraints() -> RTCMediaConstraints {
    let mandatoryConstraints = [
        RTCPair(key: "OfferToReceiveAudio", value: "true"),
        RTCPair(key: "OfferToReceiveVideo", value: "false"),
        RTCPair(key: "echoCancellation", value: "false"),
        RTCPair(key: "googEchoCancellation", value: "false")
    ]
    let optionalConstraints = [
        RTCPair(key: "internalSctpDataChannels", value: "true"),
        RTCPair(key: "DtlsSrtpKeyAgreement", value: "true")
    ]
    return RTCMediaConstraints(mandatoryConstraints: mandatoryConstraints, optionalConstraints: optionalConstraints)
}

我正在为Opus音频编解码器启用立体声,如下所示:

func peerConnection(peerConnection: RTCPeerConnection!, didCreateSessionDescription sdp: RTCSessionDescription!, error: NSError?) {
    LOGD("created sdp")

    guard error == nil else {
        LOGE("error creating session description: \(error!)")
        delegate.onError(self, description: "Error creating sdp")
        return
    }

    dispatch_async(dispatch_get_main_queue()) {
        let replaceThis = "fmtp:111 minptime=10; useinbandfec=1"
        let replaceWith = "fmtp:111 minptime=10; useinbandfec=1; stereo=1; sprop-stereo=1"
        let sdpDescriptionWithStereo = sdp.description.stringByReplacingOccurrencesOfString(replaceThis, withString: replaceWith)
        let sdpWithStereo = RTCSessionDescription(type: sdp.type, sdp: sdpDescriptionWithStereo)
        peerConnection.setLocalDescriptionWithDelegate(self, sessionDescription: sdpWithStereo)

        self.delegate.onLocalSDP(self, type: sdp.type, sdp: sdpDescriptionWithStereo)
    }
}

我在sdpDescriptionWithStereo得到了理想的结果。但我仍然无法获得立体声音效。

(并且,是的,我知道stringByReplacingOccurrencesOfString是一个彻头彻尾的黑客,但我稍后会谈到)

1 个答案:

答案 0 :(得分:1)

您可以在通知中心捕获该事件,然后进行切换。

     NotificationCenter.default.addObserver(self, selector: #selector(JanusCommunicationManager.didSessionRouteChange), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil)

     @objc func didSessionRouteChange(notification:Notification) {
        let dict = notification.userInfo
        let routeChangeReason = dict![AVAudioSessionRouteChangeReasonKey] as! UInt
        let error:Error? = nil
        switch routeChangeReason {
        case AVAudioSessionRouteChangeReason.categoryChange.rawValue:
            try? AVAudioSession.sharedInstance().overrideOutputAudioPort(.none)
            break
        default:
            break
        }

    }