webrtc 代码学习(五)AddAudioTrack

本文深入探讨了WebRTC中AddAudioTrack的实现过程,从创建AudioTrack到AddTrack,再到UnifiedPlan中的Transceiver管理。通过源代码分析,详细阐述了AudioTrack的创建、AddTrack的功能以及RtpTransceiver在音频传输中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AddAudioTrack
作者:LanPZzzz
本章我们简单讲述下pc->AddAudioTrack

1. 如下代码\examples\peerconnection\client\conductor.cc(528行)
  kAudioLabel = "xxxxxxxx"
  rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
      peer_connection_factory_->CreateAudioTrack(
          kAudioLabel, peer_connection_factory_->CreateAudioSource(
                           cricket::AudioOptions())));
  auto result_or_error = peer_connection_->AddTrack(audio_track, {kStreamId});
2. CreateAudioTrack,需要先CreateAudioSource
rtc::scoped_refptr<AudioSourceInterface>
PeerConnectionFactory::CreateAudioSource(const cricket::AudioOptions& options) {
  RTC_DCHECK(signaling_thread_->IsCurrent());
  rtc::scoped_refptr<LocalAudioSource> source(
      LocalAudioSource::Create(&options));
  return source;
}

最后创建的是LocalAudioSource(pc\localaudiosource.cc),这是在options 上进行赋值

3. CreateAudioTrack (pc\audiotrack.h ),AudioTrack 就是MediaTrack,也继承了AudioTrackInterface

返回AudioTrackInterface

T = AudioTrackInterface 
class AudioTrack : public MediaStreamTrack<AudioTrackInterface>,
                   public ObserverInterface
                   
template <typename T>
class MediaStreamTrack : public Notifier<T>

template <class T>
class Notifier : public T

这样层层继承,返回就是AudioTrackInterface,他们太优秀了,整的我代码看着好难受

4. 这里简单的介绍下AudioTrack,正好代码也不多
void AudioTrack::AddSink(AudioTrackSinkInterface* sink) {
  RTC_DCHECK(thread_checker_.CalledOnValidThread());
  if (audio_source_)
    audio_source_->AddSink(sink);
}

void AudioTrack::RemoveSink(AudioTrackSinkInterface* sink) {
  RTC_DCHECK(thread_checker_.CalledOnValidThread());
  if (audio_source_)
    audio_source_->RemoveSink(sink);
}

AddSink + RemoveSink 都是加入到audio_source中,但是我们的audio_source 是LocalAudioSource,木有用,空方法

5. 上面CreateAudioTrack 已经完成,下面是AddTrack (pc\peerconnection.cc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值