blob: a1cf14fc70536193d6db5c6d68f8e47399cd7dd4 [file] [log] [blame]
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/media/webrtc/peer_connection_dependency_factory.h"
#include <stddef.h>
#include <memory>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/renderer/content_renderer_client.h"
#include "content/renderer/p2p/ipc_socket_factory.h"
#include "content/renderer/p2p/mdns_responder_adapter.h"
#include "content/renderer/p2p/port_allocator.h"
#include "content/renderer/p2p/socket_dispatcher.h"
#include "content/renderer/render_thread_impl.h"
#include "crypto/openssl_util.h"
#include "jingle/glue/thread_wrapper.h"
#include "media/base/media_permission.h"
#include "media/media_buildflags.h"
#include "media/video/gpu_video_accelerator_factories.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/peerconnection/webrtc_ip_handling_policy.h"
#include "third_party/blink/public/platform/modules/p2p/empty_network_manager.h"
#include "third_party/blink/public/platform/modules/p2p/filtering_network_manager.h"
#include "third_party/blink/public/platform/modules/p2p/ipc_network_manager.h"
#include "third_party/blink/public/platform/modules/peerconnection/audio_codec_factory.h"
#include "third_party/blink/public/platform/modules/peerconnection/video_codec_factory.h"
#include "third_party/blink/public/platform/modules/webrtc/webrtc_logging.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_media_constraints.h"
#include "third_party/blink/public/platform/web_media_stream.h"
#include "third_party/blink/public/platform/web_media_stream_source.h"
#include "third_party/blink/public/platform/web_media_stream_track.h"
#include "third_party/blink/public/platform/web_rtc_peer_connection_handler.h"
#include "third_party/blink/public/platform/web_url.h"
#include "third_party/blink/public/web/modules/mediastream/media_stream_video_source.h"
#include "third_party/blink/public/web/modules/mediastream/media_stream_video_track.h"
#include "third_party/blink/public/web/modules/webrtc/webrtc_audio_device_impl.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/webrtc/api/call/call_factory_interface.h"
#include "third_party/webrtc/api/peer_connection_interface.h"
#include "third_party/webrtc/api/rtc_event_log/rtc_event_log_factory.h"
#include "third_party/webrtc/api/video_track_source_proxy.h"
#include "third_party/webrtc/media/engine/fake_video_codec_factory.h"
#include "third_party/webrtc/media/engine/multiplex_codec_factory.h"
#include "third_party/webrtc/media/engine/webrtc_media_engine.h"
#include "third_party/webrtc/modules/audio_processing/include/audio_processing.h"
#include "third_party/webrtc/modules/video_coding/codecs/h264/include/h264.h"
#include "third_party/webrtc/rtc_base/ref_counted_object.h"
#include "third_party/webrtc/rtc_base/ssl_adapter.h"
#include "third_party/webrtc_overrides/task_queue_factory.h"
namespace content {
namespace {
enum WebRTCIPHandlingPolicy {
DEFAULT,
DEFAULT_PUBLIC_AND_PRIVATE_INTERFACES,
DEFAULT_PUBLIC_INTERFACE_ONLY,
DISABLE_NON_PROXIED_UDP,
};
WebRTCIPHandlingPolicy GetWebRTCIPHandlingPolicy(
const std::string& preference) {
if (preference == blink::kWebRTCIPHandlingDefaultPublicAndPrivateInterfaces)
return DEFAULT_PUBLIC_AND_PRIVATE_INTERFACES;
if (preference == blink::kWebRTCIPHandlingDefaultPublicInterfaceOnly)
return DEFAULT_PUBLIC_INTERFACE_ONLY;
if (preference == blink::kWebRTCIPHandlingDisableNonProxiedUdp)
return DISABLE_NON_PROXIED_UDP;
return DEFAULT;
}
bool IsValidPortRange(uint16_t min_port, uint16_t max_port) {
DCHECK(min_port <= max_port);
return min_port != 0 && max_port != 0;
}
// PeerConnectionDependencies wants to own the factory, so we provide a simple
// object that delegates calls to the IpcPacketSocketFactory.
// TODO(zstein): Move the creation logic from IpcPacketSocketFactory in to this
// class.
class ProxyAsyncResolverFactory final : public webrtc::AsyncResolverFactory {
public:
ProxyAsyncResolverFactory(IpcPacketSocketFactory* ipc_psf)
: ipc_psf_(ipc_psf) {
DCHECK(ipc_psf);
}
rtc::AsyncResolverInterface* Create() override {
return ipc_psf_->CreateAsyncResolver();
}
private:
IpcPacketSocketFactory* ipc_psf_;
};
} // namespace
PeerConnectionDependencyFactory::PeerConnectionDependencyFactory(
bool create_p2p_socket_dispatcher)
: network_manager_(nullptr),
p2p_socket_dispatcher_(
create_p2p_socket_dispatcher ? new P2PSocketDispatcher() : nullptr),
signaling_thread_(nullptr),
worker_thread_(nullptr),
chrome_signaling_thread_("Chrome_libJingle_Signaling"),
chrome_worker_thread_("Chrome_libJingle_WorkerThread") {
TryScheduleStunProbeTrial();
}
PeerConnectionDependencyFactory::~PeerConnectionDependencyFactory() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DVLOG(1) << "~PeerConnectionDependencyFactory()";
DCHECK(!pc_factory_);
}
std::unique_ptr<blink::WebRTCPeerConnectionHandler>
PeerConnectionDependencyFactory::CreateRTCPeerConnectionHandler(
blink::WebRTCPeerConnectionHandlerClient* client,
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
return blink::Platform::Current()->CreateRTCPeerConnectionHandler(
client, std::move(task_runner));
}
const scoped_refptr<webrtc::PeerConnectionFactoryInterface>&
PeerConnectionDependencyFactory::GetPcFactory() {
if (!pc_factory_.get())
CreatePeerConnectionFactory();
CHECK(pc_factory_.get());
return pc_factory_;
}
void PeerConnectionDependencyFactory::WillDestroyCurrentMessageLoop() {
CleanupPeerConnectionFactory();
}
void PeerConnectionDependencyFactory::CreatePeerConnectionFactory() {
DCHECK(!pc_factory_.get());
DCHECK(!signaling_thread_);
DCHECK(!worker_thread_);
DCHECK(!network_manager_);
DCHECK(!socket_factory_);
DCHECK(!chrome_signaling_thread_.IsRunning());
DCHECK(!chrome_worker_thread_.IsRunning());
DVLOG(1) << "PeerConnectionDependencyFactory::CreatePeerConnectionFactory()";
#if BUILDFLAG(RTC_USE_H264) && BUILDFLAG(ENABLE_FFMPEG_VIDEO_DECODERS)
// Building /w |rtc_use_h264|, is the corresponding run-time feature enabled?
if (!base::FeatureList::IsEnabled(
blink::features::kWebRtcH264WithOpenH264FFmpeg)) {
// Feature is to be disabled.
webrtc::DisableRtcUseH264();
}
#else
webrtc::DisableRtcUseH264();
#endif // BUILDFLAG(RTC_USE_H264) && BUILDFLAG(ENABLE_FFMPEG_VIDEO_DECODERS)
base::MessageLoopCurrent::Get()->AddDestructionObserver(this);
// To allow sending to the signaling/worker threads.
jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
EnsureWebRtcAudioDeviceImpl();
CHECK(chrome_signaling_thread_.Start());
CHECK(chrome_worker_thread_.Start());
base::WaitableEvent start_worker_event(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
chrome_worker_thread_.task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&PeerConnectionDependencyFactory::InitializeWorkerThread,
base::Unretained(this), &worker_thread_,
&start_worker_event));
base::WaitableEvent create_network_manager_event(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
std::unique_ptr<MdnsResponderAdapter> mdns_responder;
#if BUILDFLAG(ENABLE_MDNS)
if (base::FeatureList::IsEnabled(
blink::features::kWebRtcHideLocalIpsWithMdns)) {
// Note that MdnsResponderAdapter is created on the main thread to have
// access to the connector to the service manager.
mdns_responder = std::make_unique<MdnsResponderAdapter>();
}
#endif // BUILDFLAG(ENABLE_MDNS)
chrome_worker_thread_.task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&PeerConnectionDependencyFactory::
CreateIpcNetworkManagerOnWorkerThread,
base::Unretained(this), &create_network_manager_event,
std::move(mdns_responder)));
start_worker_event.Wait();
create_network_manager_event.Wait();
CHECK(worker_thread_);
// Init SSL, which will be needed by PeerConnection.
if (!rtc::InitializeSSL()) {
LOG(ERROR) << "Failed on InitializeSSL.";
NOTREACHED();
return;
}
base::WaitableEvent start_signaling_event(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
chrome_signaling_thread_.task_runner()->PostTask(
FROM_HERE,
base::BindOnce(
&PeerConnectionDependencyFactory::InitializeSignalingThread,
base::Unretained(this),
RenderThreadImpl::current()->GetGpuFactories(),
&start_signaling_event));
start_signaling_event.Wait();
CHECK(signaling_thread_);
}
void PeerConnectionDependencyFactory::InitializeSignalingThread(
media::GpuVideoAcceleratorFactories* gpu_factories,
base::WaitableEvent* event) {
DCHECK(chrome_signaling_thread_.task_runner()->BelongsToCurrentThread());
DCHECK(worker_thread_);
DCHECK(p2p_socket_dispatcher_.get());
jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
signaling_thread_ = jingle_glue::JingleThreadWrapper::current();
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("webrtc_peer_connection", R"(
semantics {
sender: "WebRTC"
description:
"WebRTC is an API that provides web applications with Real Time "
"Communication (RTC) capabilities. It is used to establish a "
"secure session with a remote peer, transmitting and receiving "
"audio, video and potentially other data."
trigger:
"Application creates an RTCPeerConnection and connects it to a "
"remote peer by exchanging an SDP offer and answer."
data:
"Media encrypted using DTLS-SRTP, and protocol-level messages for "
"the various subprotocols employed by WebRTC (including ICE, DTLS, "
"RTCP, etc.). Note that ICE connectivity checks may leak the "
"user's IP address(es), subject to the restrictions/guidance in "
"https://datatracker.ietf.org/doc/draft-ietf-rtcweb-ip-handling."
destination: OTHER
destination_other:
"A destination determined by the web application that created the "
"connection."
}
policy {
cookies_allowed: NO
setting:
"This feature cannot be disabled in settings, but it won't be used "
"unless the application creates an RTCPeerConnection. Media can "
"only be captured with user's consent, but data may be sent "
"withouth that."
policy_exception_justification:
"Not implemented. 'WebRtcUdpPortRange' policy can limit the range "
"of ports used by WebRTC, but there is no policy to generally "
"block it."
}
)");
socket_factory_.reset(new IpcPacketSocketFactory(p2p_socket_dispatcher_.get(),
traffic_annotation));
const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
std::unique_ptr<webrtc::VideoEncoderFactory> webrtc_encoder_factory =
blink::CreateWebrtcVideoEncoderFactory(gpu_factories);
std::unique_ptr<webrtc::VideoDecoderFactory> webrtc_decoder_factory =
blink::CreateWebrtcVideoDecoderFactory(gpu_factories);
// Enable Multiplex codec in SDP optionally.
if (base::FeatureList::IsEnabled(blink::features::kWebRtcMultiplexCodec)) {
webrtc_encoder_factory = std::make_unique<webrtc::MultiplexEncoderFactory>(
std::move(webrtc_encoder_factory));
webrtc_decoder_factory = std::make_unique<webrtc::MultiplexDecoderFactory>(
std::move(webrtc_decoder_factory));
}
if (cmd_line->HasSwitch(switches::kUseFakeCodecForPeerConnection)) {
webrtc_encoder_factory =
std::make_unique<webrtc::FakeVideoEncoderFactory>();
webrtc_decoder_factory =
std::make_unique<webrtc::FakeVideoDecoderFactory>();
}
webrtc::PeerConnectionFactoryDependencies pcf_deps;
pcf_deps.worker_thread = worker_thread_;
pcf_deps.network_thread = worker_thread_;
pcf_deps.signaling_thread = signaling_thread_;
pcf_deps.task_queue_factory = CreateWebRtcTaskQueueFactory();
pcf_deps.call_factory = webrtc::CreateCallFactory();
pcf_deps.event_log_factory = std::make_unique<webrtc::RtcEventLogFactory>(
pcf_deps.task_queue_factory.get());
cricket::MediaEngineDependencies media_deps;
media_deps.task_queue_factory = pcf_deps.task_queue_factory.get();
media_deps.adm = audio_device_.get();
media_deps.audio_encoder_factory = blink::CreateWebrtcAudioEncoderFactory();
media_deps.audio_decoder_factory = blink::CreateWebrtcAudioDecoderFactory();
media_deps.video_encoder_factory = std::move(webrtc_encoder_factory);
media_deps.video_decoder_factory = std::move(webrtc_decoder_factory);
media_deps.audio_processing = webrtc::AudioProcessingBuilder().Create();
pcf_deps.media_engine = cricket::CreateMediaEngine(std::move(media_deps));
pc_factory_ = webrtc::CreateModularPeerConnectionFactory(std::move(pcf_deps));
CHECK(pc_factory_.get());
webrtc::PeerConnectionFactoryInterface::Options factory_options;
factory_options.disable_sctp_data_channels = false;
factory_options.disable_encryption =
cmd_line->HasSwitch(switches::kDisableWebRtcEncryption);
pc_factory_->SetOptions(factory_options);
event->Signal();
}
bool PeerConnectionDependencyFactory::PeerConnectionFactoryCreated() {
return pc_factory_.get() != nullptr;
}
scoped_refptr<webrtc::PeerConnectionInterface>
PeerConnectionDependencyFactory::CreatePeerConnection(
const webrtc::PeerConnectionInterface::RTCConfiguration& config,
blink::WebLocalFrame* web_frame,
webrtc::PeerConnectionObserver* observer) {
CHECK(web_frame);
CHECK(observer);
if (!GetPcFactory().get())
return nullptr;
webrtc::PeerConnectionDependencies dependencies(observer);
dependencies.allocator = CreatePortAllocator(web_frame);
dependencies.async_resolver_factory = CreateAsyncResolverFactory();
return GetPcFactory()
->CreatePeerConnection(config, std::move(dependencies))
.get();
}
std::unique_ptr<cricket::PortAllocator>
PeerConnectionDependencyFactory::CreatePortAllocator(
blink::WebLocalFrame* web_frame) {
DCHECK(web_frame);
// Copy the flag from Preference associated with this WebLocalFrame.
P2PPortAllocator::Config port_config;
uint16_t min_port = 0;
uint16_t max_port = 0;
// |media_permission| will be called to check mic/camera permission. If at
// least one of them is granted, P2PPortAllocator is allowed to gather local
// host IP addresses as ICE candidates. |media_permission| could be nullptr,
// which means the permission will be granted automatically. This could be the
// case when either the experiment is not enabled or the preference is not
// enforced.
//
// Note on |media_permission| lifetime: |media_permission| is owned by a frame
// (RenderFrameImpl). It is also stored as an indirect member of
// RTCPeerConnectionHandler (through PeerConnection/PeerConnectionInterface ->
// P2PPortAllocator -> FilteringNetworkManager -> |media_permission|).
// The RTCPeerConnectionHandler is owned as RTCPeerConnection::m_peerHandler
// in Blink, which will be reset in RTCPeerConnection::stop(). Since
// ActiveDOMObject::stop() is guaranteed to be called before a frame is
// detached, it is impossible for RTCPeerConnectionHandler to outlive the
// frame. Therefore using a raw pointer of |media_permission| is safe here.
media::MediaPermission* media_permission = nullptr;
if (!GetContentClient()
->renderer()
->ShouldEnforceWebRTCRoutingPreferences()) {
port_config.enable_multiple_routes = true;
port_config.enable_nonproxied_udp = true;
VLOG(3) << "WebRTC routing preferences will not be enforced";
} else {
if (web_frame && web_frame->View()) {
blink::WebString webrtc_ip_handling_policy;
blink::Platform::Current()->GetWebRTCRendererPreferences(
web_frame, &webrtc_ip_handling_policy, &min_port, &max_port);
// TODO(guoweis): |enable_multiple_routes| should be renamed to
// |request_multiple_routes|. Whether local IP addresses could be
// collected depends on if mic/camera permission is granted for this
// origin.
WebRTCIPHandlingPolicy policy =
GetWebRTCIPHandlingPolicy(webrtc_ip_handling_policy.Utf8());
switch (policy) {
// TODO(guoweis): specify the flag of disabling local candidate
// collection when webrtc is updated.
case DEFAULT_PUBLIC_INTERFACE_ONLY:
case DEFAULT_PUBLIC_AND_PRIVATE_INTERFACES:
port_config.enable_multiple_routes = false;
port_config.enable_nonproxied_udp = true;
port_config.enable_default_local_candidate =
(policy == DEFAULT_PUBLIC_AND_PRIVATE_INTERFACES);
break;
case DISABLE_NON_PROXIED_UDP:
port_config.enable_multiple_routes = false;
port_config.enable_nonproxied_udp = false;
break;
case DEFAULT:
port_config.enable_multiple_routes = true;
port_config.enable_nonproxied_udp = true;
break;
}
VLOG(3) << "WebRTC routing preferences: "
<< "policy: " << policy
<< ", multiple_routes: " << port_config.enable_multiple_routes
<< ", nonproxied_udp: " << port_config.enable_nonproxied_udp
<< ", min_udp_port: " << min_port
<< ", max_udp_port: " << max_port;
}
if (port_config.enable_multiple_routes) {
media_permission =
blink::Platform::Current()->GetWebRTCMediaPermission(web_frame);
}
}
const GURL& requesting_origin =
GURL(web_frame->GetDocument().Url()).GetOrigin();
std::unique_ptr<rtc::NetworkManager> network_manager;
if (port_config.enable_multiple_routes) {
network_manager = std::make_unique<blink::FilteringNetworkManager>(
network_manager_.get(), requesting_origin, media_permission);
} else {
network_manager =
std::make_unique<blink::EmptyNetworkManager>(network_manager_.get());
}
auto port_allocator = std::make_unique<P2PPortAllocator>(
p2p_socket_dispatcher_, std::move(network_manager), socket_factory_.get(),
port_config, requesting_origin);
if (IsValidPortRange(min_port, max_port))
port_allocator->SetPortRange(min_port, max_port);
return port_allocator;
}
std::unique_ptr<webrtc::AsyncResolverFactory>
PeerConnectionDependencyFactory::CreateAsyncResolverFactory() {
return std::make_unique<ProxyAsyncResolverFactory>(socket_factory_.get());
}
scoped_refptr<webrtc::MediaStreamInterface>
PeerConnectionDependencyFactory::CreateLocalMediaStream(
const std::string& label) {
return GetPcFactory()->CreateLocalMediaStream(label).get();
}
scoped_refptr<webrtc::VideoTrackSourceInterface>
PeerConnectionDependencyFactory::CreateVideoTrackSourceProxy(
webrtc::VideoTrackSourceInterface* source) {
// PeerConnectionFactory needs to be instantiated to make sure that
// signaling_thread_ and worker_thread_ exist.
if (!PeerConnectionFactoryCreated())
CreatePeerConnectionFactory();
return webrtc::VideoTrackSourceProxy::Create(signaling_thread_,
worker_thread_, source)
.get();
}
scoped_refptr<webrtc::VideoTrackInterface>
PeerConnectionDependencyFactory::CreateLocalVideoTrack(
const std::string& id,
webrtc::VideoTrackSourceInterface* source) {
return GetPcFactory()->CreateVideoTrack(id, source).get();
}
webrtc::SessionDescriptionInterface*
PeerConnectionDependencyFactory::CreateSessionDescription(
const std::string& type,
const std::string& sdp,
webrtc::SdpParseError* error) {
return webrtc::CreateSessionDescription(type, sdp, error);
}
webrtc::IceCandidateInterface*
PeerConnectionDependencyFactory::CreateIceCandidate(
const std::string& sdp_mid,
int sdp_mline_index,
const std::string& sdp) {
return webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, sdp, nullptr);
}
blink::WebRtcAudioDeviceImpl*
PeerConnectionDependencyFactory::GetWebRtcAudioDevice() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
EnsureWebRtcAudioDeviceImpl();
return audio_device_.get();
}
void PeerConnectionDependencyFactory::InitializeWorkerThread(
rtc::Thread** thread,
base::WaitableEvent* event) {
jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
*thread = jingle_glue::JingleThreadWrapper::current();
event->Signal();
}
void PeerConnectionDependencyFactory::TryScheduleStunProbeTrial() {
const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
if (!cmd_line->HasSwitch(switches::kWebRtcStunProbeTrialParameter))
return;
GetPcFactory();
const std::string params =
cmd_line->GetSwitchValueASCII(switches::kWebRtcStunProbeTrialParameter);
chrome_worker_thread_.task_runner()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
&PeerConnectionDependencyFactory::StartStunProbeTrialOnWorkerThread,
base::Unretained(this), params),
base::TimeDelta::FromMilliseconds(blink::kExperimentStartDelayMs));
}
void PeerConnectionDependencyFactory::StartStunProbeTrialOnWorkerThread(
const std::string& params) {
DCHECK(network_manager_);
DCHECK(chrome_worker_thread_.task_runner()->BelongsToCurrentThread());
stun_trial_.reset(new blink::StunProberTrial(network_manager_.get(), params,
socket_factory_.get()));
}
void PeerConnectionDependencyFactory::CreateIpcNetworkManagerOnWorkerThread(
base::WaitableEvent* event,
std::unique_ptr<MdnsResponderAdapter> mdns_responder) {
DCHECK(chrome_worker_thread_.task_runner()->BelongsToCurrentThread());
network_manager_ = std::make_unique<blink::IpcNetworkManager>(
p2p_socket_dispatcher_.get(), std::move(mdns_responder));
event->Signal();
}
void PeerConnectionDependencyFactory::DeleteIpcNetworkManager() {
DCHECK(chrome_worker_thread_.task_runner()->BelongsToCurrentThread());
network_manager_.reset();
}
void PeerConnectionDependencyFactory::CleanupPeerConnectionFactory() {
DVLOG(1) << "PeerConnectionDependencyFactory::CleanupPeerConnectionFactory()";
pc_factory_ = nullptr;
if (network_manager_) {
// The network manager needs to free its resources on the thread they were
// created, which is the worked thread.
if (chrome_worker_thread_.IsRunning()) {
chrome_worker_thread_.task_runner()->PostTask(
FROM_HERE,
base::BindOnce(
&PeerConnectionDependencyFactory::DeleteIpcNetworkManager,
base::Unretained(this)));
// Stopping the thread will wait until all tasks have been
// processed before returning. We wait for the above task to finish before
// letting the the function continue to avoid any potential race issues.
chrome_worker_thread_.Stop();
} else {
NOTREACHED() << "Worker thread not running.";
}
}
}
void PeerConnectionDependencyFactory::EnsureInitialized() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
GetPcFactory();
}
scoped_refptr<base::SingleThreadTaskRunner>
PeerConnectionDependencyFactory::GetWebRtcWorkerThread() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return chrome_worker_thread_.IsRunning() ? chrome_worker_thread_.task_runner()
: nullptr;
}
rtc::Thread* PeerConnectionDependencyFactory::GetWebRtcWorkerThreadRtcThread()
const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return chrome_worker_thread_.IsRunning() ? worker_thread_ : nullptr;
}
scoped_refptr<base::SingleThreadTaskRunner>
PeerConnectionDependencyFactory::GetWebRtcSignalingThread() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return chrome_signaling_thread_.IsRunning()
? chrome_signaling_thread_.task_runner()
: nullptr;
}
void PeerConnectionDependencyFactory::EnsureWebRtcAudioDeviceImpl() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (audio_device_.get())
return;
audio_device_ = new rtc::RefCountedObject<blink::WebRtcAudioDeviceImpl>();
}
std::unique_ptr<webrtc::RtpCapabilities>
PeerConnectionDependencyFactory::GetSenderCapabilities(
const std::string& kind) {
if (kind == "audio") {
return std::make_unique<webrtc::RtpCapabilities>(
GetPcFactory()->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_AUDIO));
} else if (kind == "video") {
return std::make_unique<webrtc::RtpCapabilities>(
GetPcFactory()->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_VIDEO));
}
return nullptr;
}
std::unique_ptr<webrtc::RtpCapabilities>
PeerConnectionDependencyFactory::GetReceiverCapabilities(
const std::string& kind) {
if (kind == "audio") {
return std::make_unique<webrtc::RtpCapabilities>(
GetPcFactory()->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_AUDIO));
} else if (kind == "video") {
return std::make_unique<webrtc::RtpCapabilities>(
GetPcFactory()->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_VIDEO));
}
return nullptr;
}
} // namespace content