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