VideoStreamEncoder: move PostTasks to WebRtcVideoChannel.

This change moves the responsibility of posting
EncoderSwitchRequestCallback calls closer to the top-level
users which has a better idea about threading requirements.

The change is planned to be followed-up with more changes removing
the need for VSE to post to the worker thread.

Bug: webrtc:13414, chromium:1255737
Change-Id: I57a2962a70e9f245460c59c0d61824371394b952
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/238420
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Commit-Queue: Markus Handell <handellm@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35387}
diff --git a/media/engine/webrtc_video_engine.cc b/media/engine/webrtc_video_engine.cc
index f497d90..6d17fe9 100644
--- a/media/engine/webrtc_video_engine.cc
+++ b/media/engine/webrtc_video_engine.cc
@@ -868,6 +868,12 @@
 }
 
 void WebRtcVideoChannel::RequestEncoderFallback() {
+  if (!worker_thread_->IsCurrent()) {
+    worker_thread_->PostTask(
+        ToQueuedTask(task_safety_, [this] { RequestEncoderFallback(); }));
+    return;
+  }
+
   RTC_DCHECK_RUN_ON(&thread_checker_);
   if (negotiated_codecs_.size() <= 1) {
     RTC_LOG(LS_WARNING) << "Encoder failed but no fallback codec is available";
@@ -883,6 +889,12 @@
 
 void WebRtcVideoChannel::RequestEncoderSwitch(
     const EncoderSwitchRequestCallback::Config& conf) {
+  if (!worker_thread_->IsCurrent()) {
+    worker_thread_->PostTask(ToQueuedTask(
+        task_safety_, [this, conf] { RequestEncoderSwitch(conf); }));
+    return;
+  }
+
   RTC_DCHECK_RUN_ON(&thread_checker_);
 
   if (!allow_codec_switching_) {
@@ -923,6 +935,12 @@
 
 void WebRtcVideoChannel::RequestEncoderSwitch(
     const webrtc::SdpVideoFormat& format) {
+  if (!worker_thread_->IsCurrent()) {
+    worker_thread_->PostTask(ToQueuedTask(
+        task_safety_, [this, format] { RequestEncoderSwitch(format); }));
+    return;
+  }
+
   RTC_DCHECK_RUN_ON(&thread_checker_);
 
   for (const VideoCodecSettings& codec_setting : negotiated_codecs_) {
diff --git a/video/video_stream_encoder.cc b/video/video_stream_encoder.cc
index 1c0de4b..9fd8e69 100644
--- a/video/video_stream_encoder.cc
+++ b/video/video_stream_encoder.cc
@@ -1768,14 +1768,12 @@
       if (settings_.encoder_switch_request_callback) {
         if (encoder_selector_) {
           if (auto encoder = encoder_selector_->OnEncoderBroken()) {
-            QueueRequestEncoderSwitch(*encoder);
+            settings_.encoder_switch_request_callback->RequestEncoderSwitch(
+                *encoder);
           }
         } else {
           encoder_failed_ = true;
-          worker_queue_->PostTask(ToQueuedTask(task_safety_, [this]() {
-            RTC_DCHECK_RUN_ON(worker_queue_);
-            settings_.encoder_switch_request_callback->RequestEncoderFallback();
-          }));
+          settings_.encoder_switch_request_callback->RequestEncoderFallback();
         }
       } else {
         RTC_LOG(LS_ERROR)
@@ -2014,7 +2012,7 @@
   if (!video_is_suspended && settings_.encoder_switch_request_callback &&
       encoder_selector_) {
     if (auto encoder = encoder_selector_->OnAvailableBitrate(link_allocation)) {
-      QueueRequestEncoderSwitch(*encoder);
+      settings_.encoder_switch_request_callback->RequestEncoderSwitch(*encoder);
     }
   }
 
@@ -2265,24 +2263,6 @@
   }
 }
 
-// RTC_RUN_ON(&encoder_queue_)
-void VideoStreamEncoder::QueueRequestEncoderSwitch(
-    const EncoderSwitchRequestCallback::Config& conf) {
-  worker_queue_->PostTask(ToQueuedTask(task_safety_, [this, conf]() {
-    RTC_DCHECK_RUN_ON(worker_queue_);
-    settings_.encoder_switch_request_callback->RequestEncoderSwitch(conf);
-  }));
-}
-
-// RTC_RUN_ON(&encoder_queue_)
-void VideoStreamEncoder::QueueRequestEncoderSwitch(
-    const webrtc::SdpVideoFormat& format) {
-  worker_queue_->PostTask(ToQueuedTask(task_safety_, [this, format]() {
-    RTC_DCHECK_RUN_ON(worker_queue_);
-    settings_.encoder_switch_request_callback->RequestEncoderSwitch(format);
-  }));
-}
-
 void VideoStreamEncoder::InjectAdaptationResource(
     rtc::scoped_refptr<Resource> resource,
     VideoAdaptationReason reason) {
diff --git a/video/video_stream_encoder.h b/video/video_stream_encoder.h
index 4231f1b..5194ff3 100644
--- a/video/video_stream_encoder.h
+++ b/video/video_stream_encoder.h
@@ -241,13 +241,6 @@
                                int64_t time_when_posted_in_ms)
       RTC_RUN_ON(&encoder_queue_);
 
-  // TODO(bugs.webrtc.org/11341) : Remove this version of RequestEncoderSwitch.
-  void QueueRequestEncoderSwitch(
-      const EncoderSwitchRequestCallback::Config& conf)
-      RTC_RUN_ON(&encoder_queue_);
-  void QueueRequestEncoderSwitch(const webrtc::SdpVideoFormat& format)
-      RTC_RUN_ON(&encoder_queue_);
-
   TaskQueueBase* const worker_queue_;
 
   const uint32_t number_of_cores_;