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