【问题标题】:How to stream audio from browser to WebRTC native C++ application如何将音频从浏览器流式传输到 WebRTC 本机 C++ 应用程序
【发布时间】:2014-06-20 09:48:24
【问题描述】:

到目前为止,我已经成功运行了以下示例:

WebRTC native c++ to browser video streaming example

该示例展示了如何将视频从本机 C++ 应用程序 (peerconnection_client.exe) 流式传输到浏览器(我使用的是 Chrome)。这工作正常,我可以在浏览器中看到自己。

我想做的是将音频从浏览器流式传输到本机应用程序,但我不确定如何。谁能给我一些建议吗?

【问题讨论】:

    标签: javascript c++ audio webrtc


    【解决方案1】:

    我正在尝试找到一种将视频和音频从浏览器流式传输到我的本机程序的方法。到目前为止,这是我的方式。

    要在没有 gui 的情况下将视频从浏览器流式传输到您的本机程序,只需按照此处的示例进行操作。 https://chromium.googlesource.com/external/webrtc/+/refs/heads/master/examples/peerconnection/client/

    使用AddOrUpdateSink 添加您自己的VideoSinkInterface,您将在回调void OnFrame(const cricket::VideoFrame& frame) 中收到您的帧数据。无需像示例那样将框架渲染到 GUI,您可以做任何您想做的事情。

    无需真正的音频设备即可将音频从浏览器流式传输到您的本机程序。您可以使用假音频设备。

    1. 在文件https://chromium.googlesource.com/external/webrtc/+/master/webrtc/build/webrtc.gni中将变量rtc_use_dummy_audio_file_devices修改为true
    2. 调用全局静态函数指定文件名webrtc::FileAudioDeviceFactory::SetFilenamesToUse("", "file_to_save_audio");
    3. 补丁file_audio_device.cc 代码自爆。 (在我写这个答案时,FileAudioDevice 有一些问题,可能已经修复)
    4. 重新编译你的程序touch file_to_save_audio,webrtc连接建立后你会在file_to_save_audio看到pcm数据。

    补丁:

        diff --git a/webrtc/modules/audio_device/dummy/file_audio_device.cc b/webrtc/modules/audio_device/dummy/file_audio_device.cc
    index 8b3fa5e..2717cda 100644
    --- a/webrtc/modules/audio_device/dummy/file_audio_device.cc
    +++ b/webrtc/modules/audio_device/dummy/file_audio_device.cc
    @@ -35,6 +35,7 @@ FileAudioDevice::FileAudioDevice(const int32_t id,
         _recordingBufferSizeIn10MS(0),
         _recordingFramesIn10MS(0),
         _playoutFramesIn10MS(0),
    +    _initialized(false),
         _playing(false),
         _recording(false),
         _lastCallPlayoutMillis(0),
    @@ -135,12 +136,13 @@ int32_t FileAudioDevice::InitPlayout() {
           // Update webrtc audio buffer with the selected parameters
           _ptrAudioBuffer->SetPlayoutSampleRate(kPlayoutFixedSampleRate);
           _ptrAudioBuffer->SetPlayoutChannels(kPlayoutNumChannels);
    +      _initialized = true;
       }
       return 0;
     }
    
     bool FileAudioDevice::PlayoutIsInitialized() const {
    -  return true;
    +  return _initialized;
     }
    
     int32_t FileAudioDevice::RecordingIsAvailable(bool& available) {
    @@ -236,7 +238,7 @@ int32_t FileAudioDevice::StopPlayout() {
     }
    
     bool FileAudioDevice::Playing() const {
    -  return true;
    +  return _playing;
     }
    
     int32_t FileAudioDevice::StartRecording() {
    diff --git a/webrtc/modules/audio_device/dummy/file_audio_device.h b/webrtc/modules/audio_device/dummy/file_audio_device.h
    index a69b47e..3f3c841 100644
    --- a/webrtc/modules/audio_device/dummy/file_audio_device.h
    +++ b/webrtc/modules/audio_device/dummy/file_audio_device.h
    @@ -185,6 +185,7 @@ class FileAudioDevice : public AudioDeviceGeneric {
       std::unique_ptr<rtc::PlatformThread> _ptrThreadRec;
       std::unique_ptr<rtc::PlatformThread> _ptrThreadPlay;
    
    +  bool _initialized;;
       bool _playing;
       bool _recording;
       uint64_t _lastCallPlayoutMillis;
    

    【讨论】:

      【解决方案2】:

      我知道这是一个老问题,但我目前正在努力寻找解决方案,所以我认为分享是值得的。

      有一种或多或少的简单方法可以让示例运行从浏览器流式传输到本机代码。您需要 webrtc 源 http://www.webrtc.org/native-code/development

      您需要的两个工具是对等连接服务器和客户端。两者都可以在文件夹 talk/example/peerconnection 中找到

      要使其正常工作,您需要对其进行修补以启用对等连接客户端的 DTLS。因此,使用此处提供的补丁https://code.google.com/p/webrtc/issues/detail?id=3872 对其进行修补并重建客户端。现在您已在本机站点上进行设置!

      对于浏览器,我推荐从此处https://github.com/GoogleChrome/webrtc 开始的 peer2peer 示例,在启动 peerconnection_server 并连接 peerconnection_client 后尝试连接 peer2peer 示例。

      可能需要连接约束:
      { “DtlsSrtpKeyAgreement”:真 }

      【讨论】:

        【解决方案3】:

        您可以使用以下示例来实现 appRTC 的桌面客户端。

        https://github.com/TemasysCommunications/appRTCDesk

        这完成并与 webrtc.org 上的开源实现提供的 web 客户端、android 客户端和 iOs 客户端互操作,为您提供一整套客户端来使用他们的免费服务器。 peer connection_{client|server} 是 lib jingle time (pre webrtc) 中的一个旧示例,不与其他任何东西互操作。

        【讨论】:

        • 感谢您的回复。我会看看它。我设法将音频和视频从浏览器流式传输到 peerconnection_client。您提供的示例是否在接收端处理将流保存到文件中,或者您知道我可以使用哪些本机 WebRTC 函数来实现这一点?
        • 原来peer连接客户端和服务器的问题是他们使用纯套接字连接,所以你不能从ICE中受益,并且扩展非常困难。此外,您在 iOS 和 android 上没有可与之互操作的示例,就像您使用 appRTC 一样。这是一个更难的凝视点。 appRTCDesk 不提供保存/记录功能。该标准正在努力直接在浏览器中提供记录容量,但在相当长的一段时间内都不会推出。简而言之,现在还没有原生 (JS) API。
        • WebRTC 的 VoEFile 类有一些转换函数,例如“int ConvertPCMToCompressed(InStream* streamIn, OutStream* streamOut, CodecInst* compression)”。我正在尝试弄清楚如何从接收方(本机 C++ 应用程序)中使用此功能。 VoERTP_RTCP 也有 StartRTPDump() 但建议仅用于调试目的。
        猜你喜欢
        • 2015-04-01
        • 2010-09-14
        • 2014-11-10
        • 2017-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-25
        • 2014-05-29
        相关资源
        最近更新 更多