草庐IT

ios - 在 iOS 的 webrtc 上启用立体声

coder 2023-09-07 原文

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

我从这篇博客文章中借鉴了一些想法,希望我能让它发挥作用。我们能够为 Web 客户端而非 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 完全是一个黑客,但我稍后会谈到)

最佳答案

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

     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
        }

    }

关于ios - 在 iOS 的 webrtc 上启用立体声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35281569/

有关ios - 在 iOS 的 webrtc 上启用立体声的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. ruby-on-rails - 启用 Rack::Deflater 时 ETag 发生变化 - 2

    在启用Rack::Deflater来gzip我的响应主体时偶然发现了一些奇怪的东西。也许我遗漏了一些东西,但启用此功能后,响应被压缩,但是资源的ETag在每个请求上都会发生变化。这会强制应用程序每次都响应,而不是发送304。这在没有启用Rack::Deflater的情况下有效,我已经验证页面源没有改变。我正在运行一个使用thin作为Web服务器的Rails应用程序。Gemfile.lockhttps://gist.github.com/2510816有没有什么方法可以让我从Rack中间件获得更多的输出,这样我就可以看到发生了什么?提前致谢。 最佳答案

  4. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  5. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  6. ruby - 为 IO::popen 拯救 "command not found" - 2

    当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby​​1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#

  7. ruby - IO::EAGAINWaitReadable:资源暂时不可用 - 读取会阻塞 - 2

    当我尝试使用“套接字”库中的方法“read_nonblock”时出现以下错误IO::EAGAINWaitReadable:Resourcetemporarilyunavailable-readwouldblock但是当我通过终端上的IRB尝试时它工作正常如何让它读取缓冲区? 最佳答案 IgetthefollowingerrorwhenItrytousethemethod"read_nonblock"fromthe"socket"library当缓冲区中的数据未准备好时,这是预期的行为。由于异常IO::EAGAINWaitReadab

  8. ruby - 使用 Selenium WebDriver 启用/禁用 javascript - 2

    出于某种原因,我必须为Firefox禁用javascript(手动,我们按照提到的步骤执行http://support.mozilla.org/en-US/kb/javascript-settings-for-interactive-web-pages#w_enabling-and-disabling-javascript)。使用Ruby的SeleniumWebDriver如何实现这一点? 最佳答案 是的,这是可能的。而是另一种方式。您首先需要查看链接Selenium::WebDriver::Firefox::Profile#[]=

  9. ruby-on-rails - 如何在 Rails 中启用 Ruby 警告? - 2

    我在test.rb中做了这个:defsome_methodp"Firstdefinition"enddefsome_methodp"Seconddefinition"endsome_method当我调用rubytest.rb时,它打印出Seconddefinition(预期)当我调用ruby-wtest.rb时,它会打印Seconddefinition(预期)并打印警告test。rb:5:警告:方法重新定义;丢弃旧的some_method有没有办法在Rails中启用这些警告?(并将警告打印到控制台/日志文件)为什么我要启用警告:例如,如果我无意中重新定义Controller中的一个方法

  10. ruby - 为什么这个启用 SSL 的 Ruby 服务器/客户端测试有效? - 2

    我正在努力在Ruby中创建启用SSL的服务器,以及与服务器一起使用的相应Ruby客户端。为了进行测试,我使用以下命令创建了自己的根CA证书。$:~/devel/ssl-test/ssl/CA$opensslgenrsa-outTestCA.key2048GeneratingRSAprivatekey,2048bitlongmodulus............+++...........................+++eis65537(0x10001)$:~/devel/ssl-test/ssl/CA$opensslreq-new-keyTestCA.key-outTestCA.

随机推荐