WebRTC 源码阅读 2——初始化
初始化集中于PeerConnectionFactoryInterface
PeerConnectionFactoryInterface
一个抽象工厂,用于管理与业务相关的一切抽象类的创建,正如它原本的注释所说:
PeerConnectionFactoryInterface is the factory interface used for creating PeerConnection, MeadiaStream,MediaStreamTrack objects。包括:
- 创建音、视轨
- 创建线程
- 创建媒体管理引擎
- …(后续会再来梳理一次)
创建音、视轨对应于下述的方法,会在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
函数内部有两个重要的信息存储结构:
PeerConnectionFactoryDependencies
cricket::MediaEngineDependencies
其中PeerConnectionFactoryDependencies
包含cricket::MediaEngineDependencies
,PeerConnectionFactoryInterface
则由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的设计思路。