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