1 /*
2  *  Copyright (c) 2019 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_rtp_track_source.h"
12 
13 namespace webrtc {
14 
VideoRtpTrackSource(Callback * callback)15 VideoRtpTrackSource::VideoRtpTrackSource(Callback* callback)
16     : VideoTrackSource(true /* remote */), callback_(callback) {
17   worker_sequence_checker_.Detach();
18 }
19 
ClearCallback()20 void VideoRtpTrackSource::ClearCallback() {
21   RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
22   callback_ = nullptr;
23 }
24 
source()25 rtc::VideoSourceInterface<VideoFrame>* VideoRtpTrackSource::source() {
26   return &broadcaster_;
27 }
sink()28 rtc::VideoSinkInterface<VideoFrame>* VideoRtpTrackSource::sink() {
29   return &broadcaster_;
30 }
31 
BroadcastRecordableEncodedFrame(const RecordableEncodedFrame & frame) const32 void VideoRtpTrackSource::BroadcastRecordableEncodedFrame(
33     const RecordableEncodedFrame& frame) const {
34   MutexLock lock(&mu_);
35   for (rtc::VideoSinkInterface<RecordableEncodedFrame>* sink : encoded_sinks_) {
36     sink->OnFrame(frame);
37   }
38 }
39 
SupportsEncodedOutput() const40 bool VideoRtpTrackSource::SupportsEncodedOutput() const {
41   return true;
42 }
43 
GenerateKeyFrame()44 void VideoRtpTrackSource::GenerateKeyFrame() {
45   RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
46   if (callback_) {
47     callback_->OnGenerateKeyFrame();
48   }
49 }
50 
AddEncodedSink(rtc::VideoSinkInterface<RecordableEncodedFrame> * sink)51 void VideoRtpTrackSource::AddEncodedSink(
52     rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) {
53   RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
54   RTC_DCHECK(sink);
55   size_t size = 0;
56   {
57     MutexLock lock(&mu_);
58     RTC_DCHECK(std::find(encoded_sinks_.begin(), encoded_sinks_.end(), sink) ==
59                encoded_sinks_.end());
60     encoded_sinks_.push_back(sink);
61     size = encoded_sinks_.size();
62   }
63   if (size == 1 && callback_) {
64     callback_->OnEncodedSinkEnabled(true);
65   }
66 }
67 
RemoveEncodedSink(rtc::VideoSinkInterface<RecordableEncodedFrame> * sink)68 void VideoRtpTrackSource::RemoveEncodedSink(
69     rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) {
70   RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
71   size_t size = 0;
72   {
73     MutexLock lock(&mu_);
74     auto it = std::find(encoded_sinks_.begin(), encoded_sinks_.end(), sink);
75     if (it != encoded_sinks_.end()) {
76       encoded_sinks_.erase(it);
77     }
78     size = encoded_sinks_.size();
79   }
80   if (size == 0 && callback_) {
81     callback_->OnEncodedSinkEnabled(false);
82   }
83 }
84 
85 }  // namespace webrtc
86