1 /*
2  *  Copyright 2011 The WebRTC project authors. All Rights Reserved.
3  *
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.
9  */
10 
11 #include "pc/video_track.h"
12 
13 #include <string>
14 #include <vector>
15 
16 #include "api/notifier.h"
17 #include "api/sequence_checker.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/location.h"
20 #include "rtc_base/ref_counted_object.h"
21 
22 namespace webrtc {
23 
VideoTrack(const std::string & label,VideoTrackSourceInterface * video_source,rtc::Thread * worker_thread)24 VideoTrack::VideoTrack(const std::string& label,
25                        VideoTrackSourceInterface* video_source,
26                        rtc::Thread* worker_thread)
27     : MediaStreamTrack<VideoTrackInterface>(label),
28       worker_thread_(worker_thread),
29       video_source_(video_source),
30       content_hint_(ContentHint::kNone) {
31   video_source_->RegisterObserver(this);
32 }
33 
~VideoTrack()34 VideoTrack::~VideoTrack() {
35   video_source_->UnregisterObserver(this);
36 }
37 
kind() const38 std::string VideoTrack::kind() const {
39   return kVideoKind;
40 }
41 
42 // AddOrUpdateSink and RemoveSink should be called on the worker
43 // thread.
AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame> * sink,const rtc::VideoSinkWants & wants)44 void VideoTrack::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
45                                  const rtc::VideoSinkWants& wants) {
46   RTC_DCHECK(worker_thread_->IsCurrent());
47   VideoSourceBase::AddOrUpdateSink(sink, wants);
48   rtc::VideoSinkWants modified_wants = wants;
49   modified_wants.black_frames = !enabled();
50   video_source_->AddOrUpdateSink(sink, modified_wants);
51 }
52 
RemoveSink(rtc::VideoSinkInterface<VideoFrame> * sink)53 void VideoTrack::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
54   RTC_DCHECK(worker_thread_->IsCurrent());
55   VideoSourceBase::RemoveSink(sink);
56   video_source_->RemoveSink(sink);
57 }
58 
content_hint() const59 VideoTrackInterface::ContentHint VideoTrack::content_hint() const {
60   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
61   return content_hint_;
62 }
63 
set_content_hint(ContentHint hint)64 void VideoTrack::set_content_hint(ContentHint hint) {
65   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
66   if (content_hint_ == hint)
67     return;
68   content_hint_ = hint;
69   Notifier<VideoTrackInterface>::FireOnChanged();
70 }
71 
set_enabled(bool enable)72 bool VideoTrack::set_enabled(bool enable) {
73   RTC_DCHECK(signaling_thread_checker_.IsCurrent());
74   worker_thread_->Invoke<void>(RTC_FROM_HERE, [enable, this] {
75     RTC_DCHECK(worker_thread_->IsCurrent());
76     for (auto& sink_pair : sink_pairs()) {
77       rtc::VideoSinkWants modified_wants = sink_pair.wants;
78       modified_wants.black_frames = !enable;
79       video_source_->AddOrUpdateSink(sink_pair.sink, modified_wants);
80     }
81   });
82   return MediaStreamTrack<VideoTrackInterface>::set_enabled(enable);
83 }
84 
OnChanged()85 void VideoTrack::OnChanged() {
86   RTC_DCHECK(signaling_thread_checker_.IsCurrent());
87   if (video_source_->state() == MediaSourceInterface::kEnded) {
88     set_state(kEnded);
89   } else {
90     set_state(kLive);
91   }
92 }
93 
Create(const std::string & id,VideoTrackSourceInterface * source,rtc::Thread * worker_thread)94 rtc::scoped_refptr<VideoTrack> VideoTrack::Create(
95     const std::string& id,
96     VideoTrackSourceInterface* source,
97     rtc::Thread* worker_thread) {
98   rtc::RefCountedObject<VideoTrack>* track =
99       new rtc::RefCountedObject<VideoTrack>(id, source, worker_thread);
100   return track;
101 }
102 
103 }  // namespace webrtc
104