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 | |
| 13 | namespace webrtc { |
| 14 | |
Markus Handell | d5e2f21 | 2019-11-26 09:30:08 +0100 | [diff] [blame] | 15 | VideoRtpTrackSource::VideoRtpTrackSource(Callback* callback) |
| 16 | : VideoTrackSource(true /* remote */), callback_(callback) { |
| 17 | worker_sequence_checker_.Detach(); |
| 18 | } |
| 19 | |
| 20 | void VideoRtpTrackSource::ClearCallback() { |
| 21 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 22 | callback_ = nullptr; |
| 23 | } |
Markus Handell | 15f2ff4 | 2019-11-22 10:34:37 +0100 | [diff] [blame] | 24 | |
| 25 | rtc::VideoSourceInterface<VideoFrame>* VideoRtpTrackSource::source() { |
| 26 | return &broadcaster_; |
| 27 | } |
| 28 | rtc::VideoSinkInterface<VideoFrame>* VideoRtpTrackSource::sink() { |
| 29 | return &broadcaster_; |
| 30 | } |
| 31 | |
Markus Handell | d5e2f21 | 2019-11-26 09:30:08 +0100 | [diff] [blame] | 32 | void VideoRtpTrackSource::BroadcastRecordableEncodedFrame( |
| 33 | const RecordableEncodedFrame& frame) const { |
| 34 | rtc::CritScope cs(&mu_); |
| 35 | for (rtc::VideoSinkInterface<RecordableEncodedFrame>* sink : encoded_sinks_) { |
| 36 | sink->OnFrame(frame); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | bool VideoRtpTrackSource::SupportsEncodedOutput() const { |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | void VideoRtpTrackSource::GenerateKeyFrame() { |
| 45 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 46 | if (callback_) { |
| 47 | callback_->OnGenerateKeyFrame(); |
| 48 | } |
| 49 | } |
| 50 | |
| 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 | rtc::CritScope cs(&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 | |
| 68 | void VideoRtpTrackSource::RemoveEncodedSink( |
| 69 | rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) { |
| 70 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 71 | size_t size = 0; |
| 72 | { |
| 73 | rtc::CritScope cs(&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 | |
Markus Handell | 15f2ff4 | 2019-11-22 10:34:37 +0100 | [diff] [blame] | 85 | } // namespace webrtc |