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