From 250c31df009631faeb14706ea1f273bc6d6d0811 Mon Sep 17 00:00:00 2001 From: Tomas Gunnarsson Date: Mon, 14 Feb 2022 23:48:41 +0100 Subject: [PATCH] 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 Commit-Queue: Tomas Gunnarsson Cr-Commit-Position: refs/heads/main@{#36009} --- pc/media_stream_track_proxy.h | 4 ++-- pc/video_track.cc | 30 +++++++++++++++++++++--------- pc/video_track.h | 5 +++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/pc/media_stream_track_proxy.h b/pc/media_stream_track_proxy.h index 02784233fd..0c13f83df8 100644 --- a/pc/media_stream_track_proxy.h +++ b/pc/media_stream_track_proxy.h @@ -44,8 +44,8 @@ PROXY_PRIMARY_THREAD_DESTRUCTOR() 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 ad552ea1c9..57af724511 100644 --- a/pc/video_track.cc +++ b/pc/video_track.cc @@ -53,7 +53,7 @@ void VideoTrack::AddOrUpdateSink(rtc::VideoSinkInterface* sink, 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 @@ void VideoTrack::set_content_hint(ContentHint hint) { } 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::set_enabled(enable); + RTC_DCHECK_RUN_ON(&signaling_thread_); + + bool ret = MediaStreamTrack::set_enabled(enable); + + worker_thread_->Invoke(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::enabled(); } diff --git a/pc/video_track.h b/pc/video_track.h index 15bfc878f5..a23f7a02ba 100644 --- a/pc/video_track.h +++ b/pc/video_track.h @@ -62,6 +62,11 @@ class VideoTrack : public MediaStreamTrack, rtc::Thread* const worker_thread_; const rtc::scoped_refptr 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