如何将浏览器中的音频流传输到WebRTC本地C++应用程序

12

我已经成功运行了以下示例:

WebRTC原生C++到浏览器的视频流示例

这个示例展示了如何从一个本地的C++应用程序(peerconnection_client.exe)向浏览器(stream)视频。 这很好用,我可以在浏览器中看到自己。

我想做的是从浏览器向本地应用程序(stream)音频,但我不确定怎么做。 有人能给我一些指导吗?

3个回答

4

我正在寻找一种从浏览器到本地程序同时流传视频和音频的方法,以下是我目前的方式。

要在没有GUI的情况下从浏览器流传视频到本地程序,请按照此处的示例操作:https://chromium.googlesource.com/external/webrtc/+/refs/heads/master/examples/peerconnection/client/

使用AddOrUpdateSink添加您自己的VideoSinkInterface,并在回调函数void OnFrame(const cricket::VideoFrame& frame)中接收帧数据。与示例中呈现框架不同,您可以进行任何您想要的操作。

要从浏览器流传音频到本地程序而不使用真实音频设备,可以使用虚拟音频设备。

  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连接建立后,您将看到pcm数据在file_to_save_audio中。

修补:

    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 您需要的两个工具是peerconnection服务器和客户端。这两个工具都可以在talk / example / peerconnection文件夹中找到。
要使其正常工作,您需要修补它以启用DTLS用于peerconnection客户端。因此,请使用此处提供的补丁https://code.google.com/p/webrtc/issues/detail?id=3872对其进行修补并重新构建客户端。现在您已经在本地站点上设置好了!
对于浏览器,我建议从这里https://github.com/GoogleChrome/webrtc使用peer2peer示例,在启动peerconnection_server并连接peerconnection_client后,尝试使用peer2peer示例进行连接。
也许需要一个连接约束:
{ "DtlsSrtpKeyAgreement": true }

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

https://github.com/TemasysCommunications/appRTCDesk

这样就完成了与webrtc.org提供的Web客户端、Android客户端和iOS客户端的互操作,为您提供了一整套可用于其免费服务器的客户端。peer connection_{client|server}是lib jingle时代(即webrtc之前)的一个旧示例,不与其他任何内容互操作。

谢谢您的回复。我会看一下的。我已经成功地从浏览器流式传输音频和视频到peerconnection_client。您提供的示例是否处理将流保存到接收端的文件,或者您知道可以使用哪些本机WebRTC函数来实现此目的? - jpen
原始的点对点连接客户端和服务器使用纯套接字进行连接,因此您无法从ICE中受益,并且扩展非常困难。此外,您没有iOS和Android的示例可与之互操作,就像使用appRTC一样。这是一个更难的起点。 appRTCDesk不提供保存/记录功能。 标准中有一个努力直接在浏览器中提供录制功能,但它需要相当长的时间才能推出。因此,简而言之,现在没有本地(JS)API。 - Dr. Alex Gouaillard
WebRTC的VoEFile类有一些转换函数,例如“int ConvertPCMToCompressed(InStream * streamIn,OutStream * streamOut,CodecInst * compression)”。我正在尝试弄清楚如何从接收端(本地C ++应用程序)使用此函数。另外,VoERTP_RTCP具有StartRTPDump(),但仅建议用于调试目的。 - jpen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接