blob: bd6d9c26ddd64d245f209fd687cef3b6240ea73c [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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:36 +00009 */
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +000010
Steve Anton36b29d12017-10-30 09:57:42 -070011#include <string>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "pc/videotrack.h"
Niels Möller84255bb2017-10-06 13:43:23 +020014#include "rtc_base/refcountedobject.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016namespace webrtc {
17
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018VideoTrack::VideoTrack(const std::string& label,
perkj773be362017-07-31 23:22:01 -070019 VideoTrackSourceInterface* video_source,
20 rtc::Thread* worker_thread)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021 : MediaStreamTrack<VideoTrackInterface>(label),
perkj773be362017-07-31 23:22:01 -070022 worker_thread_(worker_thread),
pbos5214a0a2016-12-16 15:39:11 -080023 video_source_(video_source),
24 content_hint_(ContentHint::kNone) {
perkjc8f952d2016-03-23 00:33:56 -070025 video_source_->RegisterObserver(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026}
27
28VideoTrack::~VideoTrack() {
perkjc8f952d2016-03-23 00:33:56 -070029 video_source_->UnregisterObserver(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030}
31
32std::string VideoTrack::kind() const {
deadbeeffac06552015-11-25 11:26:01 -080033 return kVideoKind;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034}
35
nisse5b68ab52016-04-07 07:45:54 -070036// AddOrUpdateSink and RemoveSink should be called on the worker
37// thread.
nisseacd935b2016-11-11 03:55:13 -080038void VideoTrack::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
39 const rtc::VideoSinkWants& wants) {
perkj773be362017-07-31 23:22:01 -070040 RTC_DCHECK(worker_thread_->IsCurrent());
perkjd6c39542016-03-17 10:35:23 +010041 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.org28e20752013-07-10 00:45:36 +000045}
46
nisseacd935b2016-11-11 03:55:13 -080047void VideoTrack::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
perkj773be362017-07-31 23:22:01 -070048 RTC_DCHECK(worker_thread_->IsCurrent());
perkjd6c39542016-03-17 10:35:23 +010049 VideoSourceBase::RemoveSink(sink);
50 video_source_->RemoveSink(sink);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051}
52
pbos5214a0a2016-12-16 15:39:11 -080053VideoTrackInterface::ContentHint VideoTrack::content_hint() const {
54 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
55 return content_hint_;
56}
57
58void 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.org28e20752013-07-10 00:45:36 +000066bool VideoTrack::set_enabled(bool enable) {
nisse5b68ab52016-04-07 07:45:54 -070067 RTC_DCHECK(signaling_thread_checker_.CalledOnValidThread());
perkj773be362017-07-31 23:22:01 -070068 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.org28e20752013-07-10 00:45:36 +000076 return MediaStreamTrack<VideoTrackInterface>::set_enabled(enable);
77}
78
perkjc8f952d2016-03-23 00:33:56 -070079void VideoTrack::OnChanged() {
nisse5b68ab52016-04-07 07:45:54 -070080 RTC_DCHECK(signaling_thread_checker_.CalledOnValidThread());
perkjc8f952d2016-03-23 00:33:56 -070081 if (video_source_->state() == MediaSourceInterface::kEnded) {
82 set_state(kEnded);
83 } else {
84 set_state(kLive);
85 }
86}
87
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088rtc::scoped_refptr<VideoTrack> VideoTrack::Create(
nissedb25d2e2016-02-26 01:24:58 -080089 const std::string& id,
perkj773be362017-07-31 23:22:01 -070090 VideoTrackSourceInterface* source,
91 rtc::Thread* worker_thread) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000092 rtc::RefCountedObject<VideoTrack>* track =
perkj773be362017-07-31 23:22:01 -070093 new rtc::RefCountedObject<VideoTrack>(id, source, worker_thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 return track;
95}
96
97} // namespace webrtc