前言
构造MediaEngineDependencies的作用,是将所有创建的参数打包到这里,方便参数的传递和管理。
代码分析
CreatePeerConnectionFactory
H:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\api\create_peerconnection_factory.cc
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) {
PeerConnectionFactoryDependencies dependencies;
dependencies.network_thread = network_thread;
dependencies.worker_thread = worker_thread;
dependencies.signaling_thread = signaling_thread;
dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
dependencies.call_factory = CreateCallFactory();
dependencies.event_log_factory = std::make_unique<RtcEventLogFactory>(
dependencies.task_queue_factory.get());
dependencies.trials = std::make_unique<webrtc::FieldTrialBasedConfig>();
cricket::MediaEngineDependencies media_dependencies;
media_dependencies.task_queue_factory = dependencies.task_queue_factory.get();
media_dependencies.adm = std::move(default_adm);
media_dependencies.audio_encoder_factory = std::move(audio_encoder_factory);
media_dependencies.audio_decoder_factory = std::move(audio_decoder_factory);
media_dependencies.audio_frame_processor = audio_frame_processor;
if (audio_processing) {
media_dependencies.audio_processing = std::move(audio_processing);
} else {
media_dependencies.audio_processing = AudioProcessingBuilder().Create();
}
media_dependencies.audio_mixer = std::move(audio_mixer);
media_dependencies.video_encoder_factory = std::move(video_encoder_factory);
media_dependencies.video_decoder_factory = std::move(video_decoder_factory);
media_dependencies.trials = dependencies.trials.get();
dependencies.media_engine =
cricket::CreateMediaEngine(std::move(media_dependencies));
return CreateModularPeerConnectionFactory(std::move(dependencies));
}
—》1、dependencies.media_engine =
cricket::CreateMediaEngine(std::move(media_dependencies));
CreateMediaEngine
H:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\media\engine\webrtc_media_engine.cc
std::unique_ptr<MediaEngineInterface> CreateMediaEngine(
MediaEngineDependencies dependencies) {
// TODO(sprang): Make populating |dependencies.trials| mandatory and remove
// these fallbacks.
std::unique_ptr<webrtc::WebRtcKeyValueConfig> fallback_trials(
dependencies.trials ? nullptr : new webrtc::FieldTrialBasedConfig());
const webrtc::WebRtcKeyValueConfig& trials =
dependencies.trials ? *dependencies.trials : *fallback_trials;
// 创建音频引擎
auto audio_engine = std::make_unique<WebRtcVoiceEngine>(
dependencies.task_queue_factory, std::move(dependencies.adm),
std::move(dependencies.audio_encoder_factory),
std::move(dependencies.audio_decoder_factory),
std::move(dependencies.audio_mixer),
std::move(dependencies.audio_processing),
dependencies.audio_frame_processor, trials);
// 创建视频引擎
#ifdef HAVE_WEBRTC_VIDEO
auto video_engine = std::make_unique<WebRtcVideoEngine>(
std::move(dependencies.video_encoder_factory),
std::move(dependencies.video_decoder_factory), trials);
#else
auto video_engine = std::make_unique<NullWebRtcVideoEngine>();
#endif
// 创建音频和视频引擎后,保存到CompositeMediaEngine媒体引擎中
return std::make_unique<CompositeMediaEngine>(std::move(fallback_trials),
std::move(audio_engine),
std::move(video_engine));
}
—》2、 return CreateModularPeerConnectionFactory(std::move(dependencies));
CreateModularPeerConnectionFactory
rtc::scoped_refptr<PeerConnectionFactoryInterface>
CreateModularPeerConnectionFactory(
PeerConnectionFactoryDependencies dependencies) {
// The PeerConnectionFactory must be created on the signaling thread.
if (dependencies.signaling_thread &&
!dependencies.signaling_thread->IsCurrent()) {
return dependencies.signaling_thread
->Invoke<rtc::scoped_refptr<PeerConnectionFactoryInterface>>(
RTC_FROM_HERE, [&dependencies] {
return CreateModularPeerConnectionFactory(
std::move(dependencies));
});
}
auto pc_factory = PeerConnectionFactory::Create(std::move(dependencies));
if (!pc_factory) {
return nullptr;
}
// Verify that the invocation and the initialization ended up agreeing on the
// thread.
RTC_DCHECK_RUN_ON(pc_factory->signaling_thread());
return PeerConnectionFactoryProxy::Create(pc_factory->signaling_thread(),
pc_factory);
}
—》PeerConnectionFactory::Create(std::move(dependencies));
PeerConnectionFactory::Create
rtc::scoped_refptr<PeerConnectionFactory> PeerConnectionFactory::Create(
PeerConnectionFactoryDependencies dependencies) {
auto context = ConnectionContext::Create(&dependencies);
if (!context) {
return nullptr;
}
return new rtc::RefCountedObject<PeerConnectionFactory>(context,
&dependencies);
}
—》ConnectionContext::Create(&dependencies);
ConnectionContext::Create
rtc::scoped_refptr<ConnectionContext> ConnectionContext::Create(
PeerConnectionFactoryDependencies* dependencies) {
auto context = new rtc::RefCountedObject<ConnectionContext>(dependencies);
if (!context->channel_manager_->Init()) {
return nullptr;
}
return context;
}
—》ConnectionContext::ConnectionContext
创建好该对象实例后,会调用 ChannelManager::Init, 主要是做了
bool ChannelManager::Init() {
if (mediaengine) {
initialized = worker_thread->Invoke
RTCFROM_HERE, [&] { return media_engine->Init(); });
RTCDCHECK(initialized);
}
就是判断媒体引擎是否已经创建好,如果已经创建好了,则会调用CompositeMediaEngine::Init -》WebRtcVoiceEngine::Init。
ConnectionContext::ConnectionContext
ConnectionContext::ConnectionContext(
PeerConnectionFactoryDependencies* dependencies)
: network_thread_(MaybeStartThread(dependencies->network_thread,
"pc_network_thread",
true,
owned_network_thread_)),
worker_thread_(MaybeStartThread(dependencies->worker_thread,
"pc_worker_thread",
false,
owned_worker_thread_)),
signaling_thread_(MaybeWrapThread(dependencies->signaling_thread,
wraps_current_thread_)),
network_monitor_factory_(
std::move(dependencies->network_monitor_factory)),
call_factory_(std::move(dependencies->call_factory)),
media_engine_(std::move(dependencies->media_engine)),
sctp_factory_(
MaybeCreateSctpFactory(std::move(dependencies->sctp_factory),
network_thread())),
trials_(dependencies->trials
? std::move(dependencies->trials)
: std::make_unique<FieldTrialBasedConfig>()) {
signaling_thread_->AllowInvokesToThread(worker_thread_);
signaling_thread_->AllowInvokesToThread(network_thread_);
worker_thread_->AllowInvokesToThread(network_thread_);
network_thread_->DisallowAllInvokes();
RTC_DCHECK_RUN_ON(signaling_thread_);
rtc::InitRandom(rtc::Time32());
// If network_monitor_factory_ is non-null, it will be used to create a
// network monitor while on the network thread.
default_network_manager_ = std::make_unique<rtc::BasicNetworkManager>(
network_monitor_factory_.get());
default_socket_factory_ =
std::make_unique<rtc::BasicPacketSocketFactory>(network_thread());
channel_manager_ = std::make_unique<cricket::ChannelManager>(
std::move(media_engine_), std::make_unique<cricket::RtpDataEngine>(),
worker_thread(), network_thread());
channel_manager_->SetVideoRtxEnabled(true);
// Set warning levels on the threads, to give warnings when response
// may be slower than is expected of the thread.
// Since some of the threads may be the same, start with the least
// restrictive limits and end with the least permissive ones.
// This will give warnings for all cases.
signaling_thread_->SetDispatchWarningMs(100);
worker_thread_->SetDispatchWarningMs(30);
network_thread_->SetDispatchWarningMs(10);
}