webrtc_m130/pc/connection_context.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

184 lines
6.5 KiB
C++
Raw Normal View History

/*
* Copyright 2020 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "pc/connection_context.h"
#include <type_traits>
#include <utility>
#include "api/transport/field_trial_based_config.h"
#include "media/base/media_engine.h"
#include "media/sctp/sctp_transport_factory.h"
#include "rtc_base/helpers.h"
#include "rtc_base/internal/default_socket_server.h"
#include "rtc_base/socket_server.h"
#include "rtc_base/task_utils/to_queued_task.h"
#include "rtc_base/time_utils.h"
namespace webrtc {
namespace {
rtc::Thread* MaybeStartNetworkThread(
rtc::Thread* old_thread,
std::unique_ptr<rtc::SocketFactory>& socket_factory_holder,
std::unique_ptr<rtc::Thread>& thread_holder) {
if (old_thread) {
return old_thread;
}
std::unique_ptr<rtc::SocketServer> socket_server =
rtc::CreateDefaultSocketServer();
thread_holder = std::make_unique<rtc::Thread>(socket_server.get());
socket_factory_holder = std::move(socket_server);
thread_holder->SetName("pc_network_thread", nullptr);
thread_holder->Start();
return thread_holder.get();
}
rtc::Thread* MaybeStartWorkerThread(
rtc::Thread* old_thread,
std::unique_ptr<rtc::Thread>& thread_holder) {
if (old_thread) {
return old_thread;
}
thread_holder = rtc::Thread::Create();
thread_holder->SetName("pc_worker_thread", nullptr);
thread_holder->Start();
return thread_holder.get();
}
rtc::Thread* MaybeWrapThread(rtc::Thread* signaling_thread,
bool& wraps_current_thread) {
wraps_current_thread = false;
if (signaling_thread) {
return signaling_thread;
}
auto this_thread = rtc::Thread::Current();
if (!this_thread) {
// If this thread isn't already wrapped by an rtc::Thread, create a
// wrapper and own it in this class.
this_thread = rtc::ThreadManager::Instance()->WrapCurrentThread();
wraps_current_thread = true;
}
return this_thread;
}
std::unique_ptr<SctpTransportFactoryInterface> MaybeCreateSctpFactory(
std::unique_ptr<SctpTransportFactoryInterface> factory,
rtc::Thread* network_thread) {
if (factory) {
return factory;
}
#ifdef WEBRTC_HAVE_SCTP
return std::make_unique<cricket::SctpTransportFactory>(network_thread);
#else
return nullptr;
#endif
}
} // namespace
// Static
rtc::scoped_refptr<ConnectionContext> ConnectionContext::Create(
PeerConnectionFactoryDependencies* dependencies) {
return rtc::scoped_refptr<ConnectionContext>(
new ConnectionContext(dependencies));
}
ConnectionContext::ConnectionContext(
PeerConnectionFactoryDependencies* dependencies)
: network_thread_(MaybeStartNetworkThread(dependencies->network_thread,
owned_socket_factory_,
owned_network_thread_)),
worker_thread_(MaybeStartWorkerThread(dependencies->worker_thread,
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)),
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_);
if (network_thread_->IsCurrent()) {
// TODO(https://crbug.com/webrtc/12802) switch to DisallowAllInvokes
network_thread_->AllowInvokesToThread(network_thread_);
} else {
network_thread_->PostTask(ToQueuedTask([thread = network_thread_] {
thread->DisallowBlockingCalls();
// TODO(https://crbug.com/webrtc/12802) switch to DisallowAllInvokes
thread->AllowInvokesToThread(thread);
}));
}
RTC_DCHECK_RUN_ON(signaling_thread_);
rtc::InitRandom(rtc::Time32());
rtc::SocketFactory* socket_factory = dependencies->socket_factory;
if (socket_factory == nullptr) {
if (owned_socket_factory_) {
socket_factory = owned_socket_factory_.get();
} else {
// TODO(bugs.webrtc.org/13145): This case should be deleted. Either
// require that a PacketSocketFactory and NetworkManager always are
// injected (with no need to construct these default objects), or require
// that if a network_thread is injected, an approprite rtc::SocketServer
// should be injected too.
socket_factory = network_thread()->socketserver();
}
}
// 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(), socket_factory);
default_socket_factory_ =
std::make_unique<rtc::BasicPacketSocketFactory>(socket_factory);
Add a channel factory interface. The interface is implemented by the ChannelManager and contains methods to create and destroy media channel objects as used by a transceiver. This will subsequently allow us to delete the channel objects from the transceiver class where ownership really lies rather than from the outside - which is currently required by some tests that keep channel objects on the stack. We'll furthermore be able to do the destruction asynchronously without additional Invoke()s as we do now which will remove an Invoke when making sdp changes. With introducing the interface, the following simplifications were made: * ChannelManager constructed on the signaling thread. Before, there was an Invoke in the context class, which existed for the purposes of calling MediaEngine::Init() (which in turn is only needed for the VoiceEngine). This Invoke has now been moved into the CM (more tbd). * The CM now has a pointer to the signaling thread (since that's the construction thread). That allows us to remove the signaling thread parameter from the CreateFooChannel methods. * The ssrc_generator (UniqueRandomIdGenerator) instance for SSRCs moved from SdpOfferAnswerHandler to the CM, as it's always used in combination with the CM. This simplifies the CreateFooChannel methods as well as a couple of other classes that have a CM dependency. * Removed DestroyFooChannel related code from SdpOfferAnswerHandler since the channel type detail can be taken care of by the CM. Bug: webrtc:11992, webrtc:13540 Change-Id: I04938a803734de8489ba31e6212d9eaecc244126 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/247904 Reviewed-by: Harald Alvestrand <hta@webrtc.org> Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org> Cr-Commit-Position: refs/heads/main@{#35766}
2022-01-24 08:45:26 +01:00
channel_manager_ = cricket::ChannelManager::Create(
std::move(dependencies->media_engine),
/*enable_rtx=*/true, worker_thread(), network_thread());
// 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);
}
ConnectionContext::~ConnectionContext() {
RTC_DCHECK_RUN_ON(signaling_thread_);
Add a channel factory interface. The interface is implemented by the ChannelManager and contains methods to create and destroy media channel objects as used by a transceiver. This will subsequently allow us to delete the channel objects from the transceiver class where ownership really lies rather than from the outside - which is currently required by some tests that keep channel objects on the stack. We'll furthermore be able to do the destruction asynchronously without additional Invoke()s as we do now which will remove an Invoke when making sdp changes. With introducing the interface, the following simplifications were made: * ChannelManager constructed on the signaling thread. Before, there was an Invoke in the context class, which existed for the purposes of calling MediaEngine::Init() (which in turn is only needed for the VoiceEngine). This Invoke has now been moved into the CM (more tbd). * The CM now has a pointer to the signaling thread (since that's the construction thread). That allows us to remove the signaling thread parameter from the CreateFooChannel methods. * The ssrc_generator (UniqueRandomIdGenerator) instance for SSRCs moved from SdpOfferAnswerHandler to the CM, as it's always used in combination with the CM. This simplifies the CreateFooChannel methods as well as a couple of other classes that have a CM dependency. * Removed DestroyFooChannel related code from SdpOfferAnswerHandler since the channel type detail can be taken care of by the CM. Bug: webrtc:11992, webrtc:13540 Change-Id: I04938a803734de8489ba31e6212d9eaecc244126 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/247904 Reviewed-by: Harald Alvestrand <hta@webrtc.org> Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org> Cr-Commit-Position: refs/heads/main@{#35766}
2022-01-24 08:45:26 +01:00
channel_manager_.reset(nullptr);
Reland "Use backticks not vertical bars to denote variables in comments for /pc" Original change's description: > Revert "Use backticks not vertical bars to denote variables in comments for /pc" > > This reverts commit 37ee0f5e594dd772ec6d620b5e5ea8a751b684f0. > > Reason for revert: Revert in order to be able to revert https://webrtc-review.googlesource.com/c/src/+/225642 > > Original change's description: > > Use backticks not vertical bars to denote variables in comments for /pc > > > > Bug: webrtc:12338 > > Change-Id: I88cf10afa5fc810b95d2a585ab2e895dcc163b63 > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/226953 > > Reviewed-by: Harald Alvestrand <hta@webrtc.org> > > Commit-Queue: Artem Titov <titovartem@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#34575} > > TBR=hta@webrtc.org,titovartem@webrtc.org,webrtc-scoped@luci-project-accounts.iam.gserviceaccount.com > > Change-Id: I5eddd3a14e1f664bf831e5c294fbc4de5f6a88af > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:12338 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/227082 > Reviewed-by: Björn Terelius <terelius@webrtc.org> > Commit-Queue: Björn Terelius <terelius@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#34577} Bug: webrtc:12338 Change-Id: I96bd229b73613c162d11d75fa4f5934e1b4295c7 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/227087 Commit-Queue: Artem Titov <titovartem@webrtc.org> Reviewed-by: Harald Alvestrand <hta@webrtc.org> Cr-Commit-Position: refs/heads/master@{#34611}
2021-07-30 22:30:23 +02:00
// Make sure `worker_thread()` and `signaling_thread()` outlive
// `default_socket_factory_` and `default_network_manager_`.
default_socket_factory_ = nullptr;
default_network_manager_ = nullptr;
if (wraps_current_thread_)
rtc::ThreadManager::Instance()->UnwrapCurrentThread();
}
cricket::ChannelManager* ConnectionContext::channel_manager() const {
return channel_manager_.get();
}
} // namespace webrtc