Move enabled() methods for VideoTrack over to signaling

Bug: webrtc:13680
Change-Id: I0faf0d03541fce2b93c03857e01e85588389ccd1
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/251720
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36009}
diff --git a/pc/media_stream_track_proxy.h b/pc/media_stream_track_proxy.h
index 0278423..0c13f83 100644
--- a/pc/media_stream_track_proxy.h
+++ b/pc/media_stream_track_proxy.h
@@ -44,8 +44,8 @@
 BYPASS_PROXY_CONSTMETHOD0(std::string, kind)
 BYPASS_PROXY_CONSTMETHOD0(std::string, id)
 PROXY_SECONDARY_CONSTMETHOD0(TrackState, state)
-PROXY_SECONDARY_CONSTMETHOD0(bool, enabled)
-PROXY_SECONDARY_METHOD1(bool, set_enabled, bool)
+PROXY_CONSTMETHOD0(bool, enabled)
+PROXY_METHOD1(bool, set_enabled, bool)
 PROXY_SECONDARY_CONSTMETHOD0(ContentHint, content_hint)
 PROXY_SECONDARY_METHOD1(void, set_content_hint, ContentHint)
 PROXY_SECONDARY_METHOD2(void,
diff --git a/pc/video_track.cc b/pc/video_track.cc
index ad552ea..57af724 100644
--- a/pc/video_track.cc
+++ b/pc/video_track.cc
@@ -53,7 +53,7 @@
   RTC_DCHECK_RUN_ON(worker_thread_);
   VideoSourceBaseGuarded::AddOrUpdateSink(sink, wants);
   rtc::VideoSinkWants modified_wants = wants;
-  modified_wants.black_frames = !enabled();
+  modified_wants.black_frames = !enabled_w_;
   video_source_->AddOrUpdateSink(sink, modified_wants);
 }
 
@@ -87,17 +87,29 @@
 }
 
 bool VideoTrack::set_enabled(bool enable) {
-  RTC_DCHECK_RUN_ON(worker_thread_);
-  for (auto& sink_pair : sink_pairs()) {
-    rtc::VideoSinkWants modified_wants = sink_pair.wants;
-    modified_wants.black_frames = !enable;
-    video_source_->AddOrUpdateSink(sink_pair.sink, modified_wants);
-  }
-  return MediaStreamTrack<VideoTrackInterface>::set_enabled(enable);
+  RTC_DCHECK_RUN_ON(&signaling_thread_);
+
+  bool ret = MediaStreamTrack<VideoTrackInterface>::set_enabled(enable);
+
+  worker_thread_->Invoke<void>(RTC_FROM_HERE, [&]() {
+    RTC_DCHECK_RUN_ON(worker_thread_);
+    enabled_w_ = enable;
+    for (auto& sink_pair : sink_pairs()) {
+      rtc::VideoSinkWants modified_wants = sink_pair.wants;
+      modified_wants.black_frames = !enable;
+      video_source_->AddOrUpdateSink(sink_pair.sink, modified_wants);
+    }
+  });
+
+  return ret;
 }
 
 bool VideoTrack::enabled() const {
-  RTC_DCHECK_RUN_ON(worker_thread_);
+  if (worker_thread_->IsCurrent()) {
+    RTC_DCHECK_RUN_ON(worker_thread_);
+    return enabled_w_;
+  }
+  RTC_DCHECK_RUN_ON(&signaling_thread_);
   return MediaStreamTrack<VideoTrackInterface>::enabled();
 }
 
diff --git a/pc/video_track.h b/pc/video_track.h
index 15bfc87..a23f7a0 100644
--- a/pc/video_track.h
+++ b/pc/video_track.h
@@ -62,6 +62,11 @@
   rtc::Thread* const worker_thread_;
   const rtc::scoped_refptr<VideoTrackSourceInterface> video_source_;
   ContentHint content_hint_ RTC_GUARDED_BY(worker_thread_);
+  // Cached `enabled` state for the worker thread. This is kept in sync with
+  // the state maintained on the signaling thread via set_enabled() but can
+  // be queried without blocking on the worker thread by callers that don't
+  // use an api proxy to call the `enabled()` method.
+  bool enabled_w_ RTC_GUARDED_BY(worker_thread_) = true;
 };
 
 }  // namespace webrtc