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