blob: 0bf8687af39990ea59e8c3b4874014881774efd8 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:369 */
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:1310
Jonas Olssona4d87372019-07-05 17:08:3311#include "pc/video_track.h"
12
Tommi816134a2021-05-24 14:54:4113#include <utility>
Yves Gerey3e707812018-11-28 15:47:4914#include <vector>
Steve Anton36b29d12017-10-30 16:57:4215
Yves Gerey3e707812018-11-28 15:47:4916#include "api/notifier.h"
Artem Titovd15a5752021-02-10 13:31:2417#include "api/sequence_checker.h"
Yves Gerey3e707812018-11-28 15:47:4918#include "rtc_base/checks.h"
henrike@webrtc.org28e20752013-07-10 00:45:3619
henrike@webrtc.org28e20752013-07-10 00:45:3620namespace webrtc {
21
Tommi09f57132022-02-17 12:19:5522VideoTrack::VideoTrack(
Niels Möllerc397fc62022-05-30 09:26:4023 absl::string_view label,
Tommi09f57132022-02-17 12:19:5524 rtc::scoped_refptr<
25 VideoTrackSourceProxyWithInternal<VideoTrackSourceInterface>> source,
26 rtc::Thread* worker_thread)
henrike@webrtc.org28e20752013-07-10 00:45:3627 : MediaStreamTrack<VideoTrackInterface>(label),
perkj773be362017-08-01 06:22:0128 worker_thread_(worker_thread),
Tommi09f57132022-02-17 12:19:5529 video_source_(std::move(source)),
pbos5214a0a2016-12-16 23:39:1130 content_hint_(ContentHint::kNone) {
Tommi816134a2021-05-24 14:54:4131 RTC_DCHECK_RUN_ON(&signaling_thread_);
32 // Detach the thread checker for VideoSourceBaseGuarded since we'll make calls
33 // to VideoSourceBaseGuarded on the worker thread, but we're currently on the
34 // signaling thread.
35 source_sequence_.Detach();
perkjc8f952d2016-03-23 07:33:5636 video_source_->RegisterObserver(this);
henrike@webrtc.org28e20752013-07-10 00:45:3637}
38
39VideoTrack::~VideoTrack() {
Tommi816134a2021-05-24 14:54:4140 RTC_DCHECK_RUN_ON(&signaling_thread_);
perkjc8f952d2016-03-23 07:33:5641 video_source_->UnregisterObserver(this);
henrike@webrtc.org28e20752013-07-10 00:45:3642}
43
44std::string VideoTrack::kind() const {
deadbeeffac06552015-11-25 19:26:0145 return kVideoKind;
henrike@webrtc.org28e20752013-07-10 00:45:3646}
47
nisse5b68ab52016-04-07 14:45:5448// AddOrUpdateSink and RemoveSink should be called on the worker
49// thread.
nisseacd935b2016-11-11 11:55:1350void VideoTrack::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
51 const rtc::VideoSinkWants& wants) {
Tommi816134a2021-05-24 14:54:4152 RTC_DCHECK_RUN_ON(worker_thread_);
53 VideoSourceBaseGuarded::AddOrUpdateSink(sink, wants);
perkjd6c39542016-03-17 09:35:2354 rtc::VideoSinkWants modified_wants = wants;
Tomas Gunnarsson250c31d2022-02-14 22:48:4155 modified_wants.black_frames = !enabled_w_;
Tommi34cd1d32022-02-17 12:28:5756 video_source_->internal()->AddOrUpdateSink(sink, modified_wants);
henrike@webrtc.org28e20752013-07-10 00:45:3657}
58
nisseacd935b2016-11-11 11:55:1359void VideoTrack::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
Tommi816134a2021-05-24 14:54:4160 RTC_DCHECK_RUN_ON(worker_thread_);
61 VideoSourceBaseGuarded::RemoveSink(sink);
Tommi34cd1d32022-02-17 12:28:5762 video_source_->internal()->RemoveSink(sink);
henrike@webrtc.org28e20752013-07-10 00:45:3663}
64
Markus Handelldbef2bd2021-12-22 09:47:2765void VideoTrack::RequestRefreshFrame() {
66 RTC_DCHECK_RUN_ON(worker_thread_);
Tommi34cd1d32022-02-17 12:28:5767 video_source_->internal()->RequestRefreshFrame();
Markus Handelldbef2bd2021-12-22 09:47:2768}
69
Tommi816134a2021-05-24 14:54:4170VideoTrackSourceInterface* VideoTrack::GetSource() const {
71 // Callable from any thread.
72 return video_source_.get();
73}
74
Tommi34cd1d32022-02-17 12:28:5775VideoTrackSourceInterface* VideoTrack::GetSourceInternal() const {
76 return video_source_->internal();
77}
78
pbos5214a0a2016-12-16 23:39:1179VideoTrackInterface::ContentHint VideoTrack::content_hint() const {
Tomas Gunnarssondfd69c22022-02-15 12:56:5080 RTC_DCHECK_RUN_ON(&signaling_thread_);
pbos5214a0a2016-12-16 23:39:1181 return content_hint_;
82}
83
84void VideoTrack::set_content_hint(ContentHint hint) {
Tomas Gunnarssondfd69c22022-02-15 12:56:5085 RTC_DCHECK_RUN_ON(&signaling_thread_);
pbos5214a0a2016-12-16 23:39:1186 if (content_hint_ == hint)
87 return;
88 content_hint_ = hint;
89 Notifier<VideoTrackInterface>::FireOnChanged();
90}
91
henrike@webrtc.org28e20752013-07-10 00:45:3692bool VideoTrack::set_enabled(bool enable) {
Tomas Gunnarsson250c31d2022-02-14 22:48:4193 RTC_DCHECK_RUN_ON(&signaling_thread_);
94
95 bool ret = MediaStreamTrack<VideoTrackInterface>::set_enabled(enable);
96
Danil Chapovalov9e09a1f2022-09-08 16:38:1097 worker_thread_->BlockingCall([&]() {
Tomas Gunnarsson250c31d2022-02-14 22:48:4198 RTC_DCHECK_RUN_ON(worker_thread_);
99 enabled_w_ = enable;
100 for (auto& sink_pair : sink_pairs()) {
101 rtc::VideoSinkWants modified_wants = sink_pair.wants;
102 modified_wants.black_frames = !enable;
103 video_source_->AddOrUpdateSink(sink_pair.sink, modified_wants);
104 }
105 });
106
107 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36108}
109
Tommi816134a2021-05-24 14:54:41110bool VideoTrack::enabled() const {
Tomas Gunnarsson250c31d2022-02-14 22:48:41111 if (worker_thread_->IsCurrent()) {
112 RTC_DCHECK_RUN_ON(worker_thread_);
113 return enabled_w_;
114 }
115 RTC_DCHECK_RUN_ON(&signaling_thread_);
Tommi816134a2021-05-24 14:54:41116 return MediaStreamTrack<VideoTrackInterface>::enabled();
117}
118
119MediaStreamTrackInterface::TrackState VideoTrack::state() const {
Tomas Gunnarssone7b4c132022-02-14 11:03:29120 RTC_DCHECK_RUN_ON(worker_thread_);
Tommi816134a2021-05-24 14:54:41121 return MediaStreamTrack<VideoTrackInterface>::state();
122}
123
perkjc8f952d2016-03-23 07:33:56124void VideoTrack::OnChanged() {
Tommi816134a2021-05-24 14:54:41125 RTC_DCHECK_RUN_ON(&signaling_thread_);
Tomas Gunnarssondfd69c22022-02-15 12:56:50126 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
127 MediaSourceInterface::SourceState state = video_source_->state();
128 set_state(state == MediaSourceInterface::kEnded ? kEnded : kLive);
perkjc8f952d2016-03-23 07:33:56129}
130
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52131rtc::scoped_refptr<VideoTrack> VideoTrack::Create(
Niels Möllerc397fc62022-05-30 09:26:40132 absl::string_view id,
Niels Möller769de492022-03-21 09:40:09133 rtc::scoped_refptr<VideoTrackSourceInterface> source,
perkj773be362017-08-01 06:22:01134 rtc::Thread* worker_thread) {
Tommi09f57132022-02-17 12:19:55135 rtc::scoped_refptr<
136 VideoTrackSourceProxyWithInternal<VideoTrackSourceInterface>>
Tomas Gunnarsson0d5ce622022-03-18 14:57:15137 source_proxy = VideoTrackSourceProxy::Create(
Niels Möller769de492022-03-21 09:40:09138 rtc::Thread::Current(), worker_thread, std::move(source));
Tommi09f57132022-02-17 12:19:55139
140 return rtc::make_ref_counted<VideoTrack>(id, std::move(source_proxy),
141 worker_thread);
henrike@webrtc.org28e20752013-07-10 00:45:36142}
143
144} // namespace webrtc