WebRTC 源码阅读 2——初始化

WebRTC 源码阅读 2——初始化

初始化集中于PeerConnectionFactoryInterface

PeerConnectionFactoryInterface

 一个抽象工厂,用于管理与业务相关的一切抽象类的创建,正如它原本的注释所说:

PeerConnectionFactoryInterface is the factory interface used for creating PeerConnection, MeadiaStream,MediaStreamTrack objects。包括:

  1. 创建音、视轨
  2. 创建线程
  3. 创建媒体管理引擎
  4. …(后续会再来梳理一次)

创建音、视轨对应于下述的方法,会在signal线程中创建轨道接口,并放在signalling_thread中运行==(线程之间的调用关系目前还未理清。)==

    virtual rtc::scoped_refptr<MediaStreamInterface> CreateLocalMediaStream(
      const std::string& stream_id) = 0;
  // Creates an AudioSourceInterface.
  // `options` decides audio processing settings.
  virtual rtc::scoped_refptr<AudioSourceInterface> CreateAudioSource(
      const cricket::AudioOptions& options) = 0;
  // Creates a new local VideoTrack. The same `source` can be used in several
  // tracks.
  virtual rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
      const std::string& label,
      VideoTrackSourceInterface* source) = 0;
  // Creates an new AudioTrack. At the moment `source` can be null.
  virtual rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
      const std::string& label,
      AudioSourceInterface* source) {
  		RTC_DCHECK(signaling_thread()->IsCurrent());
  		rtc::scoped_refptr<AudioTrackInterface> track(AudioTrack::Create(id, source));
  		return AudioTrackProxy::Create(signaling_thread(), track);
  }

 创建线程与音视频工厂对应函数CreatePeerConnectionFactory:

RTC_EXPORT rtc::scoped_refptr<PeerConnectionFactoryInterface>
CreatePeerConnectionFactory(
    rtc::Thread* network_thread,
    rtc::Thread* worker_thread,
    rtc::Thread* signaling_thread,
    rtc::scoped_refptr<AudioDeviceModule> default_adm,
    rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory,
    rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory,
    std::unique_ptr<VideoEncoderFactory> video_encoder_factory,
    std::unique_ptr<VideoDecoderFactory> video_decoder_factory,
    rtc::scoped_refptr<AudioMixer> audio_mixer,
    rtc::scoped_refptr<AudioProcessing> audio_processing,
    AudioFrameProcessor* audio_frame_processor = nullptr);

}  // namespace webrtc

函数内部有两个重要的信息存储结构:

  1. PeerConnectionFactoryDependencies
  2. cricket::MediaEngineDependencies

其中PeerConnectionFactoryDependencies包含cricket::MediaEngineDependenciesPeerConnectionFactoryInterface则由PeerConnectionFactoryDependencies作为唯一参数构建:

实际存储这些信息的数据结构是ConnectionContext,他是一个典型的Singleton:

rtc::scoped_refptr<ConnectionContext> ConnectionContext::Create(
    PeerConnectionFactoryDependencies* dependencies)

ConnectionContext下又有一个std::unique_ptr<cricket::ChannelManager> channel_manager_用于存放MediaEngineDependencies

它们三者的构建关系如图所示:
在这里插入图片描述

 voice_channel和video_channels对应两个列表,其官方注释如下:

// ChannelManager allows the MediaEngine to run on a separate thread, and takes
// care of marshalling calls between threads. It also creates and keeps track of
// voice and video channels; by doing so, it can temporarily pause all the
// channels when a new audio or video device is chosen. The voice and video
// channels are stored in separate vectors, to easily allow operations on just
// voice or just video channels.
// ChannelManager also allows the application to discover what devices it has
// using device manager.

想要做Video和Voice相关工作的自然要去理解两个Channel的设计思路。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值