Markus Handell | 15f2ff4 | 2019-11-22 10:34:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | #ifndef PC_VIDEO_RTP_TRACK_SOURCE_H_ |
| 12 | #define PC_VIDEO_RTP_TRACK_SOURCE_H_ |
| 13 | |
Markus Handell | d5e2f21 | 2019-11-26 09:30:08 +0100 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame^] | 16 | #include "api/video/recordable_encoded_frame.h" |
| 17 | #include "api/video/video_frame.h" |
| 18 | #include "api/video/video_sink_interface.h" |
| 19 | #include "api/video/video_source_interface.h" |
Markus Handell | 15f2ff4 | 2019-11-22 10:34:37 +0100 | [diff] [blame] | 20 | #include "media/base/video_broadcaster.h" |
| 21 | #include "pc/video_track_source.h" |
Markus Handell | d5e2f21 | 2019-11-26 09:30:08 +0100 | [diff] [blame] | 22 | #include "rtc_base/callback.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame^] | 23 | #include "rtc_base/constructor_magic.h" |
Markus Handell | 6fcd0f8 | 2020-07-07 19:08:53 +0200 | [diff] [blame] | 24 | #include "rtc_base/synchronization/mutex.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame^] | 25 | #include "rtc_base/synchronization/sequence_checker.h" |
Mirko Bonadei | 20e4c80 | 2020-11-23 11:07:42 +0100 | [diff] [blame] | 26 | #include "rtc_base/system/no_unique_address.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame^] | 27 | #include "rtc_base/thread_annotations.h" |
Markus Handell | 15f2ff4 | 2019-11-22 10:34:37 +0100 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | |
| 31 | // Video track source in use by VideoRtpReceiver |
| 32 | class VideoRtpTrackSource : public VideoTrackSource { |
| 33 | public: |
Markus Handell | d5e2f21 | 2019-11-26 09:30:08 +0100 | [diff] [blame] | 34 | class Callback { |
| 35 | public: |
| 36 | virtual ~Callback() = default; |
| 37 | |
| 38 | // Called when a keyframe should be generated |
| 39 | virtual void OnGenerateKeyFrame() = 0; |
| 40 | |
| 41 | // Called when the implementor should eventually start to serve encoded |
| 42 | // frames using BroadcastEncodedFrameBuffer. |
| 43 | // The implementor should cause a keyframe to be eventually generated. |
| 44 | virtual void OnEncodedSinkEnabled(bool enable) = 0; |
| 45 | }; |
| 46 | |
| 47 | explicit VideoRtpTrackSource(Callback* callback); |
| 48 | |
| 49 | // Call before the object implementing Callback finishes it's destructor. No |
| 50 | // more callbacks will be fired after completion. Must be called on the |
| 51 | // worker thread |
| 52 | void ClearCallback(); |
| 53 | |
| 54 | // Call to broadcast an encoded frame to registered sinks. |
| 55 | // This method can be called on any thread or queue. |
| 56 | void BroadcastRecordableEncodedFrame( |
| 57 | const RecordableEncodedFrame& frame) const; |
Markus Handell | 15f2ff4 | 2019-11-22 10:34:37 +0100 | [diff] [blame] | 58 | |
| 59 | // VideoTrackSource |
| 60 | rtc::VideoSourceInterface<VideoFrame>* source() override; |
| 61 | rtc::VideoSinkInterface<VideoFrame>* sink(); |
| 62 | |
Markus Handell | d5e2f21 | 2019-11-26 09:30:08 +0100 | [diff] [blame] | 63 | // Returns true. This method can be called on any thread. |
| 64 | bool SupportsEncodedOutput() const override; |
| 65 | |
| 66 | // Generates a key frame. Must be called on the worker thread. |
| 67 | void GenerateKeyFrame() override; |
| 68 | |
| 69 | // Adds an encoded sink. Must be called on the worker thread. |
| 70 | void AddEncodedSink( |
| 71 | rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) override; |
| 72 | |
| 73 | // Removes an encoded sink. Must be called on the worker thread. |
| 74 | void RemoveEncodedSink( |
| 75 | rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) override; |
| 76 | |
Markus Handell | 15f2ff4 | 2019-11-22 10:34:37 +0100 | [diff] [blame] | 77 | private: |
Mirko Bonadei | 20e4c80 | 2020-11-23 11:07:42 +0100 | [diff] [blame] | 78 | RTC_NO_UNIQUE_ADDRESS SequenceChecker worker_sequence_checker_; |
Markus Handell | 15f2ff4 | 2019-11-22 10:34:37 +0100 | [diff] [blame] | 79 | // |broadcaster_| is needed since the decoder can only handle one sink. |
| 80 | // It might be better if the decoder can handle multiple sinks and consider |
| 81 | // the VideoSinkWants. |
| 82 | rtc::VideoBroadcaster broadcaster_; |
Markus Handell | 6fcd0f8 | 2020-07-07 19:08:53 +0200 | [diff] [blame] | 83 | mutable Mutex mu_; |
Markus Handell | d5e2f21 | 2019-11-26 09:30:08 +0100 | [diff] [blame] | 84 | std::vector<rtc::VideoSinkInterface<RecordableEncodedFrame>*> encoded_sinks_ |
| 85 | RTC_GUARDED_BY(mu_); |
| 86 | Callback* callback_ RTC_GUARDED_BY(worker_sequence_checker_); |
Markus Handell | 15f2ff4 | 2019-11-22 10:34:37 +0100 | [diff] [blame] | 87 | |
| 88 | RTC_DISALLOW_COPY_AND_ASSIGN(VideoRtpTrackSource); |
| 89 | }; |
| 90 | |
| 91 | } // namespace webrtc |
| 92 | |
| 93 | #endif // PC_VIDEO_RTP_TRACK_SOURCE_H_ |