Markus Handell | 15f2ff4 | 2019-11-22 09:34:37 | [diff] [blame] | 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 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 13 | #include <stddef.h> |
| 14 | |
| 15 | #include <algorithm> |
| 16 | |
| 17 | #include "rtc_base/checks.h" |
| 18 | |
Markus Handell | 15f2ff4 | 2019-11-22 09:34:37 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | |
Markus Handell | d5e2f21 | 2019-11-26 08:30:08 | [diff] [blame] | 21 | VideoRtpTrackSource::VideoRtpTrackSource(Callback* callback) |
Tommi | c848268 | 2023-03-23 21:20:39 | [diff] [blame] | 22 | : VideoTrackSource(true /* remote */), callback_(callback) {} |
Markus Handell | d5e2f21 | 2019-11-26 08:30:08 | [diff] [blame] | 23 | |
| 24 | void VideoRtpTrackSource::ClearCallback() { |
| 25 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 26 | callback_ = nullptr; |
| 27 | } |
Markus Handell | 15f2ff4 | 2019-11-22 09:34:37 | [diff] [blame] | 28 | |
| 29 | rtc::VideoSourceInterface<VideoFrame>* VideoRtpTrackSource::source() { |
| 30 | return &broadcaster_; |
| 31 | } |
| 32 | rtc::VideoSinkInterface<VideoFrame>* VideoRtpTrackSource::sink() { |
| 33 | return &broadcaster_; |
| 34 | } |
| 35 | |
Markus Handell | d5e2f21 | 2019-11-26 08:30:08 | [diff] [blame] | 36 | void VideoRtpTrackSource::BroadcastRecordableEncodedFrame( |
| 37 | const RecordableEncodedFrame& frame) const { |
Markus Handell | 6fcd0f8 | 2020-07-07 17:08:53 | [diff] [blame] | 38 | MutexLock lock(&mu_); |
Markus Handell | d5e2f21 | 2019-11-26 08:30:08 | [diff] [blame] | 39 | for (rtc::VideoSinkInterface<RecordableEncodedFrame>* sink : encoded_sinks_) { |
| 40 | sink->OnFrame(frame); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | bool VideoRtpTrackSource::SupportsEncodedOutput() const { |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | void VideoRtpTrackSource::GenerateKeyFrame() { |
| 49 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 50 | if (callback_) { |
| 51 | callback_->OnGenerateKeyFrame(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void 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 Handell | 6fcd0f8 | 2020-07-07 17:08:53 | [diff] [blame] | 61 | MutexLock lock(&mu_); |
Markus Handell | d5e2f21 | 2019-11-26 08:30:08 | [diff] [blame] | 62 | 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 | |
| 72 | void VideoRtpTrackSource::RemoveEncodedSink( |
| 73 | rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) { |
| 74 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 75 | size_t size = 0; |
| 76 | { |
Markus Handell | 6fcd0f8 | 2020-07-07 17:08:53 | [diff] [blame] | 77 | MutexLock lock(&mu_); |
Markus Handell | d5e2f21 | 2019-11-26 08:30:08 | [diff] [blame] | 78 | 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 Handell | 15f2ff4 | 2019-11-22 09:34:37 | [diff] [blame] | 89 | } // namespace webrtc |