blob: e4b333c7c2b2f4a7831fb6301f40832a3ccff525 [file] [log] [blame]
Markus Handell15f2ff42019-11-22 09:34:371/*
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
Harald Alvestrand5761e7b2021-01-29 14:45:0813#include <stddef.h>
14
15#include <algorithm>
16
17#include "rtc_base/checks.h"
18
Markus Handell15f2ff42019-11-22 09:34:3719namespace webrtc {
20
Markus Handelld5e2f212019-11-26 08:30:0821VideoRtpTrackSource::VideoRtpTrackSource(Callback* callback)
Tommic8482682023-03-23 21:20:3922 : VideoTrackSource(true /* remote */), callback_(callback) {}
Markus Handelld5e2f212019-11-26 08:30:0823
24void VideoRtpTrackSource::ClearCallback() {
25 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
26 callback_ = nullptr;
27}
Markus Handell15f2ff42019-11-22 09:34:3728
29rtc::VideoSourceInterface<VideoFrame>* VideoRtpTrackSource::source() {
30 return &broadcaster_;
31}
32rtc::VideoSinkInterface<VideoFrame>* VideoRtpTrackSource::sink() {
33 return &broadcaster_;
34}
35
Markus Handelld5e2f212019-11-26 08:30:0836void VideoRtpTrackSource::BroadcastRecordableEncodedFrame(
37 const RecordableEncodedFrame& frame) const {
Markus Handell6fcd0f82020-07-07 17:08:5338 MutexLock lock(&mu_);
Markus Handelld5e2f212019-11-26 08:30:0839 for (rtc::VideoSinkInterface<RecordableEncodedFrame>* sink : encoded_sinks_) {
40 sink->OnFrame(frame);
41 }
42}
43
44bool VideoRtpTrackSource::SupportsEncodedOutput() const {
45 return true;
46}
47
48void VideoRtpTrackSource::GenerateKeyFrame() {
49 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
50 if (callback_) {
51 callback_->OnGenerateKeyFrame();
52 }
53}
54
55void VideoRtpTrackSource::AddEncodedSink(
56 rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) {
57 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
58 RTC_DCHECK(sink);
59 size_t size = 0;
60 {
Markus Handell6fcd0f82020-07-07 17:08:5361 MutexLock lock(&mu_);
Markus Handelld5e2f212019-11-26 08:30:0862 RTC_DCHECK(std::find(encoded_sinks_.begin(), encoded_sinks_.end(), sink) ==
63 encoded_sinks_.end());
64 encoded_sinks_.push_back(sink);
65 size = encoded_sinks_.size();
66 }
67 if (size == 1 && callback_) {
68 callback_->OnEncodedSinkEnabled(true);
69 }
70}
71
72void VideoRtpTrackSource::RemoveEncodedSink(
73 rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) {
74 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
75 size_t size = 0;
76 {
Markus Handell6fcd0f82020-07-07 17:08:5377 MutexLock lock(&mu_);
Markus Handelld5e2f212019-11-26 08:30:0878 auto it = std::find(encoded_sinks_.begin(), encoded_sinks_.end(), sink);
79 if (it != encoded_sinks_.end()) {
80 encoded_sinks_.erase(it);
81 }
82 size = encoded_sinks_.size();
83 }
84 if (size == 0 && callback_) {
85 callback_->OnEncodedSinkEnabled(false);
86 }
87}
88
Markus Handell15f2ff42019-11-22 09:34:3789} // namespace webrtc