gpt4 book ai didi

c++ - WebRTC Native,AudioTrackSinkInterface 添加到轨道,但永远不会调用 OnData

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:59:33 25 4
gpt4 key购买 nike

我一直在开发一种使用 WebRTC 在浏览器和 native 客户端之间交换音频的产品, native 端是用 C++ 实现的。目前我已经构建了 webRtc 的最新稳定版本(分支:branch-heads/65)。

到目前为止,我能够让连接对等点连接,在浏览器上正确接收和呈现音频。然而,尽管 chrome 调试工具表明数据正在从浏览器发送到 native 客户端,但 native 客户端似乎从未通过其音轨接收器接收到任何数据。

肯定会调用以下代码,并且正在按预期添加 channel 。

void Conductor::OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream)
{

webrtc::AudioTrackVector atracks = stream->GetAudioTracks();
for (auto track : atracks)
{
remote_audio.reset(new Native::AudioRenderer(this, track));
track->set_enabled(true);
}
}

// Audio renderer derived from webrtc::AudioTrackSinkInterface
// In the audio renderer constructor, AddSink is called on the track.
AudioRenderer::AudioRenderer(AudioCallback* callback, webrtc::AudioTrackInterface* track) : track_(track), callback_(callback)
{
// Can confirm this point is reached.
track_->AddSink(this);
}

AudioRenderer::~AudioRenderer()
{
track_->RemoveSink(this);
}

void AudioRenderer::OnData(const void* audio_data, int bits_per_sample, int sample_rate, size_t number_of_channels,
size_t number_of_frames)
{
// This is never hit, despite the connection starting and streams being added.
if (callback_ != nullptr)
{
callback_->OnAudioData(audio_data, bits_per_sample, sample_rate, number_of_channels, number_of_frames);
}
}

我还可以确认这两个报价都包含接收音频的选项:

浏览器客户端报价:

// Create offer
var offerOptions = {
offerToReceiveAudio: 1,
offerToReceiveVideo: 0
};
pc.createOffer(offerOptions)
.then(offerCreated);

本地客户端回答:

webrtc::PeerConnectionInterface::RTCOfferAnswerOptions o;
{
o.voice_activity_detection = false;
o.offer_to_receive_audio = webrtc::PeerConnectionInterface::RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
o.offer_to_receive_video = webrtc::PeerConnectionInterface::RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
}
peer_connection_->CreateAnswer(this, o);

我找不到关于此问题的任何最新信息,这似乎是框架的一个常见用例,能够在客户端应用程序中使用接收到的音频。关于我在收听入站音频时可能在哪里犯错的任何想法,或者关于我如何调查为什么这不起作用的策略?

非常感谢

最佳答案

我已经设法找到一种替代方法来从 WebRTC 获取音频数据,从而可以解决此问题。

  1. 实现自定义 webrtc::AudioDeviceModule 实现。查看 webrtc 源代码,了解如何做到这一点。
  2. RegisterAudioCallback 方法中捕获音频传输,该方法在建立调用时调用。

片段:

int32_t AudioDevice::RegisterAudioCallback(webrtc::AudioTransport * transport)
{
transport_ = transport;
return 0;
}
  1. 向设备类添加自定义方法,以使用 NeedMorePlayData 方法从音频传输中提取音频。 (注意:这似乎适用于 ntp_time_ms 作为 0 传入,似乎不是必需的)。

片段:

int32_t AudioDevice::NeedMorePlayData(
const size_t nSamples,
const size_t nBytesPerSample,
const size_t nChannels,
const uint32_t samplesPerSec,
void* audioSamples,
size_t& nSamplesOut,
int64_t* elapsed_time_ms,
int64_t* ntp_time_ms) const
{
return transport_->NeedMorePlayData(nSamples,
nBytesPerSample,
nChannels,
samplesPerSec,
audioSamples,
nSamplesOut,
elapsed_time_ms,
ntp_time_ms);
}

关于c++ - WebRTC Native,AudioTrackSinkInterface 添加到轨道,但永远不会调用 OnData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48702418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com