Allow media channel creation from the signaling thread

Introduce VoiceChannelFactoryInterface and VideoChannelFactoryInterface
to allow media send and receive channels to be safely created from the
signaling thread. This avoids exposing the full MediaEngineInterface,
which is restricted to the worker thread and makes it slightly more
modular.

Bug: webrtc:42222804
Change-Id: I064415cc86eadd48e830019b8b09488d153518f7
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/475040
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#47822}
diff --git a/media/base/fake_media_engine.h b/media/base/fake_media_engine.h
index 0e978ef..51b45b4 100644
--- a/media/base/fake_media_engine.h
+++ b/media/base/fake_media_engine.h
@@ -953,7 +953,7 @@
       const CryptoOptions& crypto_options,
       VideoBitrateAllocatorFactory* video_bitrate_allocator_factory,
       VideoMediaSendChannelInterface::EncoderSwitchRequestCallback
-          video_encoder_switch_request_callback = nullptr) override;
+          video_encoder_switch_request_callback) override;
   std::unique_ptr<VideoMediaReceiveChannelInterface> CreateReceiveChannel(
       const Environment& env,
       Call* call,
diff --git a/media/base/media_engine.h b/media/base/media_engine.h
index dc153ab..eced04b 100644
--- a/media/base/media_engine.h
+++ b/media/base/media_engine.h
@@ -83,7 +83,63 @@
       const FieldTrialsView* field_trials) const = 0;
 };
 
-class VoiceEngineInterface : public RtpHeaderExtensionQueryInterface {
+// Interface for creating voice media channels.
+//
+// Methods on this interface only perform thread-safe initialization and do not
+// mutate engine-level state. Consequently, it is safe to call these methods
+// from any thread, including the signaling thread.
+class VoiceChannelFactoryInterface {
+ public:
+  virtual ~VoiceChannelFactoryInterface() = default;
+
+  // Safe to be called from the signaling thread.
+  virtual std::unique_ptr<VoiceMediaSendChannelInterface> CreateSendChannel(
+      const Environment& env,
+      Call* call,
+      const MediaConfig& config,
+      const AudioOptions& options,
+      const CryptoOptions& crypto_options) = 0;
+
+  // Safe to be called from the signaling thread.
+  virtual std::unique_ptr<VoiceMediaReceiveChannelInterface>
+  CreateReceiveChannel(const Environment& env,
+                       Call* call,
+                       const MediaConfig& config,
+                       const AudioOptions& options,
+                       const CryptoOptions& crypto_options) = 0;
+};
+
+// Interface for creating video media channels.
+//
+// Methods on this interface only perform thread-safe initialization and do not
+// mutate engine-level state. Consequently, it is safe to call these methods
+// from any thread, including the signaling thread.
+class VideoChannelFactoryInterface {
+ public:
+  virtual ~VideoChannelFactoryInterface() = default;
+
+  // Safe to be called from the signaling thread.
+  virtual std::unique_ptr<VideoMediaSendChannelInterface> CreateSendChannel(
+      const Environment& env,
+      Call* call,
+      const MediaConfig& config,
+      const VideoOptions& options,
+      const CryptoOptions& crypto_options,
+      VideoBitrateAllocatorFactory* video_bitrate_allocator_factory,
+      VideoMediaSendChannelInterface::EncoderSwitchRequestCallback
+          video_encoder_switch_request_callback) = 0;
+
+  // Safe to be called from the signaling thread.
+  virtual std::unique_ptr<VideoMediaReceiveChannelInterface>
+  CreateReceiveChannel(const Environment& env,
+                       Call* call,
+                       const MediaConfig& config,
+                       const VideoOptions& options,
+                       const CryptoOptions& crypto_options) = 0;
+};
+
+class VoiceEngineInterface : public RtpHeaderExtensionQueryInterface,
+                             public VoiceChannelFactoryInterface {
  public:
   VoiceEngineInterface() = default;
   ~VoiceEngineInterface() override = default;
@@ -100,19 +156,20 @@
   // TODO(solenberg): Remove once VoE API refactoring is done.
   virtual scoped_refptr<AudioState> GetAudioState() const = 0;
 
-  virtual std::unique_ptr<VoiceMediaSendChannelInterface> CreateSendChannel(
+  // VoiceChannelFactoryInterface overrides.
+  std::unique_ptr<VoiceMediaSendChannelInterface> CreateSendChannel(
       const Environment& env,
       Call* call,
       const MediaConfig& config,
       const AudioOptions& options,
-      const CryptoOptions& crypto_options) = 0;
+      const CryptoOptions& crypto_options) override = 0;
 
-  virtual std::unique_ptr<VoiceMediaReceiveChannelInterface>
-  CreateReceiveChannel(const Environment& env,
-                       Call* call,
-                       const MediaConfig& config,
-                       const AudioOptions& options,
-                       const CryptoOptions& crypto_options) = 0;
+  std::unique_ptr<VoiceMediaReceiveChannelInterface> CreateReceiveChannel(
+      const Environment& env,
+      Call* call,
+      const MediaConfig& config,
+      const AudioOptions& options,
+      const CryptoOptions& crypto_options) override = 0;
 
   // Legacy: Retrieve list of supported codecs.
   // + protection codecs, and assigns PT numbers that may have to be
@@ -139,7 +196,8 @@
   virtual bool NeedsAuxiliaryCodecsAdded() const { return false; }
 };
 
-class VideoEngineInterface : public RtpHeaderExtensionQueryInterface {
+class VideoEngineInterface : public RtpHeaderExtensionQueryInterface,
+                             public VideoChannelFactoryInterface {
  public:
   VideoEngineInterface() = default;
   ~VideoEngineInterface() override = default;
@@ -147,7 +205,8 @@
   VideoEngineInterface(const VideoEngineInterface&) = delete;
   VideoEngineInterface& operator=(const VideoEngineInterface&) = delete;
 
-  virtual std::unique_ptr<VideoMediaSendChannelInterface> CreateSendChannel(
+  // VideoChannelFactoryInterface overrides.
+  std::unique_ptr<VideoMediaSendChannelInterface> CreateSendChannel(
       const Environment& env,
       Call* call,
       const MediaConfig& config,
@@ -155,14 +214,14 @@
       const CryptoOptions& crypto_options,
       VideoBitrateAllocatorFactory* video_bitrate_allocator_factory,
       VideoMediaSendChannelInterface::EncoderSwitchRequestCallback
-          video_encoder_switch_request_callback = nullptr) = 0;
+          video_encoder_switch_request_callback) override = 0;
 
-  virtual std::unique_ptr<VideoMediaReceiveChannelInterface>
-  CreateReceiveChannel(const Environment& env,
-                       Call* call,
-                       const MediaConfig& config,
-                       const VideoOptions& options,
-                       const CryptoOptions& crypto_options) = 0;
+  std::unique_ptr<VideoMediaReceiveChannelInterface> CreateReceiveChannel(
+      const Environment& env,
+      Call* call,
+      const MediaConfig& config,
+      const VideoOptions& options,
+      const CryptoOptions& crypto_options) override = 0;
 
   // Legacy: Retrieve list of supported codecs.
   // + protection codecs, and assigns PT numbers that may have to be
diff --git a/media/engine/webrtc_video_engine.h b/media/engine/webrtc_video_engine.h
index 13e6c49..efa314a 100644
--- a/media/engine/webrtc_video_engine.h
+++ b/media/engine/webrtc_video_engine.h
@@ -109,7 +109,7 @@
       const CryptoOptions& crypto_options,
       VideoBitrateAllocatorFactory* video_bitrate_allocator_factory,
       VideoMediaSendChannelInterface::EncoderSwitchRequestCallback
-          video_encoder_switch_request_callback = nullptr) override;
+          video_encoder_switch_request_callback) override;
   std::unique_ptr<VideoMediaReceiveChannelInterface> CreateReceiveChannel(
       const Environment& env,
       Call* call,
diff --git a/media/engine/webrtc_video_engine_unittest.cc b/media/engine/webrtc_video_engine_unittest.cc
index 93d4723..70e7e01 100644
--- a/media/engine/webrtc_video_engine_unittest.cc
+++ b/media/engine/webrtc_video_engine_unittest.cc
@@ -723,9 +723,9 @@
   AddSupportedVideoCodecType("VP8");
 
   std::unique_ptr<VideoMediaSendChannelInterface> send_channel =
-      engine_->CreateSendChannel(env_, call_.get(), GetMediaConfig(),
-                                 VideoOptions(), CryptoOptions(),
-                                 video_bitrate_allocator_factory_.get());
+      engine_->CreateSendChannel(
+          env_, call_.get(), GetMediaConfig(), VideoOptions(), CryptoOptions(),
+          video_bitrate_allocator_factory_.get(), nullptr);
 
   EXPECT_TRUE(send_channel->AddSendStream(StreamParams::CreateLegacy(123)));
 
@@ -739,9 +739,9 @@
   AddSupportedVideoCodecType("VP8");
 
   std::unique_ptr<VideoMediaSendChannelInterface> send_channel =
-      engine_->CreateSendChannel(env_, call_.get(), GetMediaConfig(),
-                                 VideoOptions(), CryptoOptions(),
-                                 video_bitrate_allocator_factory_.get());
+      engine_->CreateSendChannel(
+          env_, call_.get(), GetMediaConfig(), VideoOptions(), CryptoOptions(),
+          video_bitrate_allocator_factory_.get(), nullptr);
   EXPECT_TRUE(send_channel->AddSendStream(StreamParams::CreateLegacy(123)));
   VideoMediaSendInfo send_info;
   send_channel->GetStats(&send_info);
@@ -1001,9 +1001,9 @@
 std::unique_ptr<VideoMediaSendChannelInterface>
 WebRtcVideoEngineTest::SetSendParamsWithAllSupportedCodecs() {
   std::unique_ptr<VideoMediaSendChannelInterface> channel =
-      engine_->CreateSendChannel(env_, call_.get(), GetMediaConfig(),
-                                 VideoOptions(), CryptoOptions(),
-                                 video_bitrate_allocator_factory_.get());
+      engine_->CreateSendChannel(
+          env_, call_.get(), GetMediaConfig(), VideoOptions(), CryptoOptions(),
+          video_bitrate_allocator_factory_.get(), nullptr);
   VideoSenderParameters parameters;
   // We need to look up the codec in the engine to get the correct payload type.
   for (const SdpVideoFormat& format : encoder_factory_->GetSupportedFormats()) {
@@ -1184,9 +1184,9 @@
                                env_.clock().CurrentTime());
 
   std::unique_ptr<VideoMediaSendChannelInterface> send_channel =
-      engine_->CreateSendChannel(env_, call_.get(), GetMediaConfig(),
-                                 VideoOptions(), CryptoOptions(),
-                                 video_bitrate_allocator_factory_.get());
+      engine_->CreateSendChannel(
+          env_, call_.get(), GetMediaConfig(), VideoOptions(), CryptoOptions(),
+          video_bitrate_allocator_factory_.get(), nullptr);
   VideoSenderParameters parameters;
   parameters.codecs.push_back(GetEngineCodec("H264"));
   EXPECT_TRUE(send_channel->SetSenderParameters(parameters));
@@ -1216,9 +1216,9 @@
   AddSupportedVideoCodecType("H264");
 
   std::unique_ptr<VideoMediaSendChannelInterface> send_channel =
-      engine_->CreateSendChannel(env_, call_.get(), GetMediaConfig(),
-                                 VideoOptions(), CryptoOptions(),
-                                 video_bitrate_allocator_factory_.get());
+      engine_->CreateSendChannel(
+          env_, call_.get(), GetMediaConfig(), VideoOptions(), CryptoOptions(),
+          video_bitrate_allocator_factory_.get(), nullptr);
   VideoSenderParameters parameters;
   parameters.codecs.push_back(GetEngineCodec("VP8"));
   EXPECT_TRUE(send_channel->SetSenderParameters(parameters));
@@ -1255,9 +1255,9 @@
   AddSupportedVideoCodecType("H264");
 
   std::unique_ptr<VideoMediaSendChannelInterface> send_channel =
-      engine_->CreateSendChannel(env_, call_.get(), GetMediaConfig(),
-                                 VideoOptions(), CryptoOptions(),
-                                 video_bitrate_allocator_factory_.get());
+      engine_->CreateSendChannel(
+          env_, call_.get(), GetMediaConfig(), VideoOptions(), CryptoOptions(),
+          video_bitrate_allocator_factory_.get(), nullptr);
   VideoSenderParameters parameters;
   parameters.codecs.push_back(GetEngineCodec("H264"));
   EXPECT_TRUE(send_channel->SetSenderParameters(parameters));
@@ -1286,9 +1286,9 @@
   AddSupportedVideoCodecType("H264");
 
   std::unique_ptr<VideoMediaSendChannelInterface> send_channel =
-      engine_->CreateSendChannel(env_, call_.get(), GetMediaConfig(),
-                                 VideoOptions(), CryptoOptions(),
-                                 video_bitrate_allocator_factory_.get());
+      engine_->CreateSendChannel(
+          env_, call_.get(), GetMediaConfig(), VideoOptions(), CryptoOptions(),
+          video_bitrate_allocator_factory_.get(), nullptr);
 
   VideoSenderParameters parameters;
   parameters.codecs.push_back(GetEngineCodec("H264"));
@@ -1557,7 +1557,7 @@
   std::unique_ptr<VideoMediaSendChannelInterface> send_channel =
       engine.CreateSendChannel(env, call.get(), GetMediaConfig(),
                                VideoOptions(), CryptoOptions(),
-                               rate_allocator_factory.get());
+                               rate_allocator_factory.get(), nullptr);
 
   VideoSenderParameters send_parameters;
   send_parameters.codecs.push_back(engine_codecs.at(0));
@@ -1698,7 +1698,7 @@
         network_interface_(env_) {
     send_channel_ = engine_.CreateSendChannel(
         env_, call_.get(), MediaConfig(), VideoOptions(), CryptoOptions(),
-        video_bitrate_allocator_factory_.get());
+        video_bitrate_allocator_factory_.get(), nullptr);
     receive_channel_ = engine_.CreateReceiveChannel(
         env_, call_.get(), MediaConfig(), VideoOptions(), CryptoOptions());
 
@@ -1883,7 +1883,7 @@
     media_config.video.enable_cpu_adaptation = false;
     send_channel_ = engine_->CreateSendChannel(
         env_, call_.get(), media_config, VideoOptions(), CryptoOptions(),
-        video_bitrate_allocator_factory_.get());
+        video_bitrate_allocator_factory_.get(), nullptr);
     receive_channel_ = engine_->CreateReceiveChannel(
         env_, call_.get(), media_config, VideoOptions(), CryptoOptions());
     send_channel_->OnReadyToSend(true);
@@ -2789,7 +2789,7 @@
     fake_call_ = std::make_unique<FakeCall>(env_);
     send_channel_ = engine_->CreateSendChannel(
         env_, fake_call_.get(), GetMediaConfig(), VideoOptions(),
-        CryptoOptions(), video_bitrate_allocator_factory_.get());
+        CryptoOptions(), video_bitrate_allocator_factory_.get(), nullptr);
     receive_channel_ =
         engine_->CreateReceiveChannel(env_, fake_call_.get(), GetMediaConfig(),
                                       VideoOptions(), CryptoOptions());
@@ -3614,7 +3614,7 @@
 
   send_channel_ = engine_->CreateSendChannel(
       env_, fake_call_.get(), media_config, VideoOptions(), CryptoOptions(),
-      video_bitrate_allocator_factory_.get());
+      video_bitrate_allocator_factory_.get(), nullptr);
   receive_channel_ = engine_->CreateReceiveChannel(
       env_, fake_call_.get(), media_config, VideoOptions(), CryptoOptions());
   send_channel_->OnReadyToSend(true);
@@ -3627,7 +3627,7 @@
   media_config.video.suspend_below_min_bitrate = false;
   send_channel_ = engine_->CreateSendChannel(
       env_, fake_call_.get(), media_config, VideoOptions(), CryptoOptions(),
-      video_bitrate_allocator_factory_.get());
+      video_bitrate_allocator_factory_.get(), nullptr);
   receive_channel_ = engine_->CreateReceiveChannel(
       env_, fake_call_.get(), media_config, VideoOptions(), CryptoOptions());
   send_channel_->OnReadyToSend(true);
@@ -4243,7 +4243,7 @@
   media_config.video.enable_cpu_adaptation = true;
   send_channel_ = engine_->CreateSendChannel(
       env_, fake_call_.get(), media_config, VideoOptions(), CryptoOptions(),
-      video_bitrate_allocator_factory_.get());
+      video_bitrate_allocator_factory_.get(), nullptr);
   receive_channel_ = engine_->CreateReceiveChannel(
       env_, fake_call_.get(), media_config, VideoOptions(), CryptoOptions());
 
@@ -4297,7 +4297,7 @@
   media_config.video.enable_cpu_adaptation = true;
   send_channel_ = engine_->CreateSendChannel(
       env_, fake_call_.get(), media_config, VideoOptions(), CryptoOptions(),
-      video_bitrate_allocator_factory_.get());
+      video_bitrate_allocator_factory_.get(), nullptr);
   receive_channel_ = engine_->CreateReceiveChannel(
       env_, fake_call_.get(), media_config, VideoOptions(), CryptoOptions());
   send_channel_->OnReadyToSend(true);
@@ -4333,7 +4333,7 @@
   }
   send_channel_ = engine_->CreateSendChannel(
       env_, fake_call_.get(), media_config, VideoOptions(), CryptoOptions(),
-      video_bitrate_allocator_factory_.get());
+      video_bitrate_allocator_factory_.get(), nullptr);
   receive_channel_ = engine_->CreateReceiveChannel(
       env_, fake_call_.get(), media_config, VideoOptions(), CryptoOptions());
   send_channel_->OnReadyToSend(true);
@@ -5738,7 +5738,7 @@
 
   send_channel = engine_->CreateSendChannel(
       env_, call_.get(), config, VideoOptions(), CryptoOptions(),
-      video_bitrate_allocator_factory_.get());
+      video_bitrate_allocator_factory_.get(), nullptr);
 
   send_channel->SetInterface(network_interface.get());
   // Default value when DSCP is disabled should be DSCP_DEFAULT.
@@ -5750,7 +5750,7 @@
   config.enable_dscp = true;
   send_channel = engine_->CreateSendChannel(
       env_, call_.get(), config, VideoOptions(), CryptoOptions(),
-      video_bitrate_allocator_factory_.get());
+      video_bitrate_allocator_factory_.get(), nullptr);
   send_channel->SetInterface(network_interface.get());
   EXPECT_EQ(DSCP_DEFAULT, network_interface->dscp());
 
@@ -5781,7 +5781,7 @@
   config.enable_dscp = false;
   send_channel = engine_->CreateSendChannel(
       env_, call_.get(), config, VideoOptions(), CryptoOptions(),
-      video_bitrate_allocator_factory_.get());
+      video_bitrate_allocator_factory_.get(), nullptr);
   send_channel->SetInterface(network_interface.get());
   EXPECT_EQ(DSCP_DEFAULT, network_interface->dscp());
   send_channel->SetInterface(nullptr);
@@ -9779,7 +9779,7 @@
     decoder_factory_->AddSupportedVideoCodecType("VP8");
     send_channel_ = engine_.CreateSendChannel(
         env_, &fake_call_, GetMediaConfig(), VideoOptions(), CryptoOptions(),
-        mock_rate_allocator_factory_.get());
+        mock_rate_allocator_factory_.get(), nullptr);
     receive_channel_ = engine_.CreateReceiveChannel(
         env_, &fake_call_, GetMediaConfig(), VideoOptions(), CryptoOptions());
     send_channel_->OnReadyToSend(true);
diff --git a/media/engine/webrtc_voice_engine.cc b/media/engine/webrtc_voice_engine.cc
index 444937a..a074b80 100644
--- a/media/engine/webrtc_voice_engine.cc
+++ b/media/engine/webrtc_voice_engine.cc
@@ -586,7 +586,6 @@
                                      const MediaConfig& config,
                                      const AudioOptions& options,
                                      const CryptoOptions& crypto_options) {
-  RTC_DCHECK_RUN_ON(&worker_thread_checker_);
   return std::make_unique<WebRtcVoiceSendChannel>(env, this, config, options,
                                                   crypto_options, call);
 }
@@ -597,7 +596,6 @@
                                         const MediaConfig& config,
                                         const AudioOptions& options,
                                         const CryptoOptions& crypto_options) {
-  RTC_DCHECK_RUN_ON(&worker_thread_checker_);
   return std::make_unique<WebRtcVoiceReceiveChannel>(env, this, config, options,
                                                      crypto_options, call);
 }
diff --git a/pc/connection_context.cc b/pc/connection_context.cc
index 28f2794..8499755 100644
--- a/pc/connection_context.cc
+++ b/pc/connection_context.cc
@@ -243,4 +243,14 @@
   }
 }
 
+VoiceChannelFactoryInterface* ConnectionContext::voice_channel_factory() {
+  RTC_DCHECK(is_configured_for_media());
+  return media_engine_ != nullptr ? &media_engine_->voice() : nullptr;
+}
+
+VideoChannelFactoryInterface* ConnectionContext::video_channel_factory() {
+  RTC_DCHECK(is_configured_for_media());
+  return media_engine_ != nullptr ? &media_engine_->video() : nullptr;
+}
+
 }  // namespace webrtc
diff --git a/pc/connection_context.h b/pc/connection_context.h
index 9c4a617..07306bb 100644
--- a/pc/connection_context.h
+++ b/pc/connection_context.h
@@ -84,6 +84,9 @@
     return media_engine_.get();
   }
 
+  VoiceChannelFactoryInterface* voice_channel_factory();
+  VideoChannelFactoryInterface* video_channel_factory();
+
   bool is_configured_for_media() const { return is_configured_for_media_; }
 
   Thread* signaling_thread() { return signaling_thread_; }
diff --git a/pc/rtp_sender_receiver_unittest.cc b/pc/rtp_sender_receiver_unittest.cc
index 3c8a3d7..dad9fd6 100644
--- a/pc/rtp_sender_receiver_unittest.cc
+++ b/pc/rtp_sender_receiver_unittest.cc
@@ -136,7 +136,7 @@
         env_, &fake_call_, MediaConfig(), AudioOptions(), CryptoOptions());
     video_media_send_channel_ = media_engine_->video().CreateSendChannel(
         env_, &fake_call_, MediaConfig(), VideoOptions(), CryptoOptions(),
-        video_bitrate_allocator_factory_.get());
+        video_bitrate_allocator_factory_.get(), nullptr);
     voice_media_receive_channel_ = media_engine_->voice().CreateReceiveChannel(
         env_, &fake_call_, MediaConfig(), AudioOptions(), CryptoOptions());
     video_media_receive_channel_ = media_engine_->video().CreateReceiveChannel(
@@ -1376,7 +1376,7 @@
           CreateBuiltinVideoBitrateAllocatorFactory();
   auto video_media_send_channel = media_engine->video().CreateSendChannel(
       env, &fake_call, MediaConfig(), VideoOptions(), CryptoOptions(),
-      video_bitrate_allocator_factory.get());
+      video_bitrate_allocator_factory.get(), nullptr);
 
   scoped_refptr<MediaStreamInterface> local_stream =
       MediaStream::Create(kStreamId1);
diff --git a/pc/rtp_transceiver.cc b/pc/rtp_transceiver.cc
index db52919..9be0a7b 100644
--- a/pc/rtp_transceiver.cc
+++ b/pc/rtp_transceiver.cc
@@ -223,7 +223,8 @@
 CreateMediaContentChannels(
     MediaType media_type,
     const Environment& env,
-    MediaEngineInterface* media_engine,
+    VoiceChannelFactoryInterface* voice_factory,
+    VideoChannelFactoryInterface* video_factory,
     Call* call,
     const MediaConfig& media_config,
     const AudioOptions& audio_options,
@@ -233,17 +234,19 @@
     VideoMediaSendChannelInterface::EncoderSwitchRequestCallback
         video_encoder_switch_request_callback = nullptr) {
   if (media_type == MediaType::AUDIO) {
-    return {media_engine->voice().CreateSendChannel(
-                env, call, media_config, audio_options, crypto_options),
-            media_engine->voice().CreateReceiveChannel(
-                env, call, media_config, audio_options, crypto_options)};
+    RTC_DCHECK(voice_factory);
+    return {voice_factory->CreateSendChannel(env, call, media_config,
+                                             audio_options, crypto_options),
+            voice_factory->CreateReceiveChannel(env, call, media_config,
+                                                audio_options, crypto_options)};
   }
-  return {media_engine->video().CreateSendChannel(
+  RTC_DCHECK(video_factory);
+  return {video_factory->CreateSendChannel(
               env, call, media_config, video_options, crypto_options,
               video_bitrate_allocator_factory,
               std::move(video_encoder_switch_request_callback)),
-          media_engine->video().CreateReceiveChannel(
-              env, call, media_config, video_options, crypto_options)};
+          video_factory->CreateReceiveChannel(env, call, media_config,
+                                              video_options, crypto_options)};
 }
 
 std::vector<absl::AnyInvocable<void() &&>> DetachAndGetStopTasksForSenders(
@@ -370,7 +373,6 @@
       network_thread_safety_(PendingTaskSafetyFlag::CreateAttachedToTaskQueue(
           /*alive=*/true,
           context->network_thread())),
-      media_engine_ref_(nullptr),
       context_(context),
       codec_lookup_helper_(codec_lookup_helper),
       legacy_stats_(legacy_stats),
@@ -402,8 +404,8 @@
            receiver_id)]() mutable -> ScopedOperationsBatcher::FinalizerTask {
         RTC_DCHECK_RUN_ON(this->context()->worker_thread());
         auto channels = CreateMediaContentChannels(
-            media_type_, env_, media_engine(), call, media_config,
-            audio_options, video_options, crypto_options,
+            media_type_, env_, voice_channel_factory(), video_channel_factory(),
+            call, media_config, audio_options, video_options, crypto_options,
             video_bitrate_allocator_factory,
             std::move(encoder_switch_callback));
         auto sender = CreateSender(
@@ -443,7 +445,6 @@
   }
 
   RTC_CHECK(!channel_) << "Missing call to ClearChannel?";
-  RTC_DCHECK(!media_engine_ref_);
   RTC_DCHECK(!owned_send_channel_);
   RTC_DCHECK(!owned_receive_channel_);
 }
@@ -524,9 +525,9 @@
           }
         } else {
           auto channels = CreateMediaContentChannels(
-              media_type(), env_, media_engine(), call_ptr, media_config,
-              audio_options, video_options, crypto_options,
-              video_bitrate_allocator_factory,
+              media_type(), env_, voice_channel_factory(),
+              video_channel_factory(), call_ptr, media_config, audio_options,
+              video_options, crypto_options, video_bitrate_allocator_factory,
               GetEncoderSwitchRequestCallback());
           media_send_channel = std::move(channels.first);
           media_receive_channel = std::move(channels.second);
@@ -731,7 +732,6 @@
   SetMediaChannels(nullptr, nullptr);
   owned_send_channel_ = nullptr;
   owned_receive_channel_ = nullptr;
-  media_engine_ref_ = nullptr;
 }
 
 PLAN_B_ONLY void RtpTransceiver::AddSenderPlanB(
@@ -833,16 +833,6 @@
   return mid_;
 }
 
-// RTC_RUN_ON(context()->worker_thread())
-MediaEngineInterface* RtpTransceiver::media_engine() {
-  if (!media_engine_ref_) {
-    media_engine_ref_ =
-        std::make_unique<ConnectionContext::MediaEngineReference>(
-            scoped_refptr<ConnectionContext>(context_));
-  }
-  return media_engine_ref_->media_engine();
-}
-
 void RtpTransceiver::OnFirstPacketReceived(uint32_t ssrc) {
   for (const auto& receiver : receivers_) {
     receiver->internal()->NotifyFirstPacketReceived(ssrc);
diff --git a/pc/rtp_transceiver.h b/pc/rtp_transceiver.h
index c8904aa..c5ce21c 100644
--- a/pc/rtp_transceiver.h
+++ b/pc/rtp_transceiver.h
@@ -437,7 +437,12 @@
   VoiceMediaReceiveChannelInterface* voice_media_receive_channel();
 
  private:
-  MediaEngineInterface* media_engine() RTC_RUN_ON(context()->worker_thread());
+  VoiceChannelFactoryInterface* voice_channel_factory() const {
+    return context_->voice_channel_factory();
+  }
+  VideoChannelFactoryInterface* video_channel_factory() const {
+    return context_->video_channel_factory();
+  }
   ConnectionContext* context() const { return context_; }
   CodecVendor& codec_vendor() {
     return *codec_lookup_helper_->GetCodecVendor();
@@ -548,8 +553,6 @@
   // because all access on the network thread is within an invoke()
   // from thread_.
   std::unique_ptr<ChannelInterface> channel_ = nullptr;
-  std::unique_ptr<ConnectionContext::MediaEngineReference> media_engine_ref_
-      RTC_GUARDED_BY(context()->worker_thread());
   ConnectionContext* const context_;
   CodecLookupHelper* const codec_lookup_helper_;
   LegacyStatsCollectorInterface* const legacy_stats_;