henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2011 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 10 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 11 | #include <string> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "pc/videotrack.h" |
Niels Möller | 84255bb | 2017-10-06 13:43:23 +0200 | [diff] [blame] | 14 | #include "rtc_base/refcountedobject.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 15 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 16 | namespace webrtc { |
| 17 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | VideoTrack::VideoTrack(const std::string& label, |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 19 | VideoTrackSourceInterface* video_source, |
| 20 | rtc::Thread* worker_thread) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | : MediaStreamTrack<VideoTrackInterface>(label), |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 22 | worker_thread_(worker_thread), |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 23 | video_source_(video_source), |
| 24 | content_hint_(ContentHint::kNone) { |
perkj | c8f952d | 2016-03-23 00:33:56 -0700 | [diff] [blame] | 25 | video_source_->RegisterObserver(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | VideoTrack::~VideoTrack() { |
perkj | c8f952d | 2016-03-23 00:33:56 -0700 | [diff] [blame] | 29 | video_source_->UnregisterObserver(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | std::string VideoTrack::kind() const { |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 33 | return kVideoKind; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | } |
| 35 | |
nisse | 5b68ab5 | 2016-04-07 07:45:54 -0700 | [diff] [blame] | 36 | // AddOrUpdateSink and RemoveSink should be called on the worker |
| 37 | // thread. |
nisse | acd935b | 2016-11-11 03:55:13 -0800 | [diff] [blame] | 38 | void VideoTrack::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 39 | const rtc::VideoSinkWants& wants) { |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 40 | RTC_DCHECK(worker_thread_->IsCurrent()); |
perkj | d6c3954 | 2016-03-17 10:35:23 +0100 | [diff] [blame] | 41 | VideoSourceBase::AddOrUpdateSink(sink, wants); |
| 42 | rtc::VideoSinkWants modified_wants = wants; |
| 43 | modified_wants.black_frames = !enabled(); |
| 44 | video_source_->AddOrUpdateSink(sink, modified_wants); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | } |
| 46 | |
nisse | acd935b | 2016-11-11 03:55:13 -0800 | [diff] [blame] | 47 | void VideoTrack::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) { |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 48 | RTC_DCHECK(worker_thread_->IsCurrent()); |
perkj | d6c3954 | 2016-03-17 10:35:23 +0100 | [diff] [blame] | 49 | VideoSourceBase::RemoveSink(sink); |
| 50 | video_source_->RemoveSink(sink); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 51 | } |
| 52 | |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 53 | VideoTrackInterface::ContentHint VideoTrack::content_hint() const { |
| 54 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 55 | return content_hint_; |
| 56 | } |
| 57 | |
| 58 | void VideoTrack::set_content_hint(ContentHint hint) { |
| 59 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 60 | if (content_hint_ == hint) |
| 61 | return; |
| 62 | content_hint_ = hint; |
| 63 | Notifier<VideoTrackInterface>::FireOnChanged(); |
| 64 | } |
| 65 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | bool VideoTrack::set_enabled(bool enable) { |
nisse | 5b68ab5 | 2016-04-07 07:45:54 -0700 | [diff] [blame] | 67 | RTC_DCHECK(signaling_thread_checker_.CalledOnValidThread()); |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 68 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [enable, this] { |
| 69 | RTC_DCHECK(worker_thread_->IsCurrent()); |
| 70 | for (auto& sink_pair : sink_pairs()) { |
| 71 | rtc::VideoSinkWants modified_wants = sink_pair.wants; |
| 72 | modified_wants.black_frames = !enable; |
| 73 | video_source_->AddOrUpdateSink(sink_pair.sink, modified_wants); |
| 74 | } |
| 75 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 76 | return MediaStreamTrack<VideoTrackInterface>::set_enabled(enable); |
| 77 | } |
| 78 | |
perkj | c8f952d | 2016-03-23 00:33:56 -0700 | [diff] [blame] | 79 | void VideoTrack::OnChanged() { |
nisse | 5b68ab5 | 2016-04-07 07:45:54 -0700 | [diff] [blame] | 80 | RTC_DCHECK(signaling_thread_checker_.CalledOnValidThread()); |
perkj | c8f952d | 2016-03-23 00:33:56 -0700 | [diff] [blame] | 81 | if (video_source_->state() == MediaSourceInterface::kEnded) { |
| 82 | set_state(kEnded); |
| 83 | } else { |
| 84 | set_state(kLive); |
| 85 | } |
| 86 | } |
| 87 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 88 | rtc::scoped_refptr<VideoTrack> VideoTrack::Create( |
nisse | db25d2e | 2016-02-26 01:24:58 -0800 | [diff] [blame] | 89 | const std::string& id, |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 90 | VideoTrackSourceInterface* source, |
| 91 | rtc::Thread* worker_thread) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 92 | rtc::RefCountedObject<VideoTrack>* track = |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 93 | new rtc::RefCountedObject<VideoTrack>(id, source, worker_thread); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | return track; |
| 95 | } |
| 96 | |
| 97 | } // namespace webrtc |