xref: /aosp_15_r20/external/webrtc/media/base/video_broadcaster.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2016 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 MEDIA_BASE_VIDEO_BROADCASTER_H_
12 #define MEDIA_BASE_VIDEO_BROADCASTER_H_
13 
14 #include "api/media_stream_interface.h"
15 #include "api/scoped_refptr.h"
16 #include "api/sequence_checker.h"
17 #include "api/video/video_frame_buffer.h"
18 #include "api/video/video_source_interface.h"
19 #include "media/base/video_source_base.h"
20 #include "rtc_base/synchronization/mutex.h"
21 #include "rtc_base/thread_annotations.h"
22 
23 namespace rtc {
24 
25 // VideoBroadcaster broadcast video frames to sinks and combines VideoSinkWants
26 // from its sinks. It does that by implementing rtc::VideoSourceInterface and
27 // rtc::VideoSinkInterface. The class is threadsafe; methods may be called on
28 // any thread. This is needed because VideoStreamEncoder calls AddOrUpdateSink
29 // both on the worker thread and on the encoder task queue.
30 class VideoBroadcaster : public VideoSourceBase,
31                          public VideoSinkInterface<webrtc::VideoFrame> {
32  public:
33   VideoBroadcaster();
34   ~VideoBroadcaster() override;
35 
36   // Adds a new, or updates an already existing sink. If the sink is new and
37   // ProcessConstraints has been called previously, the new sink's
38   // OnConstraintsCalled method will be invoked with the most recent
39   // constraints.
40   void AddOrUpdateSink(VideoSinkInterface<webrtc::VideoFrame>* sink,
41                        const VideoSinkWants& wants) override;
42   void RemoveSink(VideoSinkInterface<webrtc::VideoFrame>* sink) override;
43 
44   // Returns true if the next frame will be delivered to at least one sink.
45   bool frame_wanted() const;
46 
47   // Returns VideoSinkWants a source is requested to fulfill. They are
48   // aggregated by all VideoSinkWants from all sinks.
49   VideoSinkWants wants() const;
50 
51   // This method ensures that if a sink sets rotation_applied == true,
52   // it will never receive a frame with pending rotation. Our caller
53   // may pass in frames without precise synchronization with changes
54   // to the VideoSinkWants.
55   void OnFrame(const webrtc::VideoFrame& frame) override;
56 
57   void OnDiscardedFrame() override;
58 
59   // Called on the network thread when constraints change. Forwards the
60   // constraints to sinks added with AddOrUpdateSink via OnConstraintsChanged.
61   void ProcessConstraints(
62       const webrtc::VideoTrackSourceConstraints& constraints);
63 
64  protected:
65   void UpdateWants() RTC_EXCLUSIVE_LOCKS_REQUIRED(sinks_and_wants_lock_);
66   const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& GetBlackFrameBuffer(
67       int width,
68       int height) RTC_EXCLUSIVE_LOCKS_REQUIRED(sinks_and_wants_lock_);
69 
70   mutable webrtc::Mutex sinks_and_wants_lock_;
71 
72   VideoSinkWants current_wants_ RTC_GUARDED_BY(sinks_and_wants_lock_);
73   rtc::scoped_refptr<webrtc::VideoFrameBuffer> black_frame_buffer_;
74   bool previous_frame_sent_to_all_sinks_ RTC_GUARDED_BY(sinks_and_wants_lock_) =
75       true;
76   absl::optional<webrtc::VideoTrackSourceConstraints> last_constraints_
77       RTC_GUARDED_BY(sinks_and_wants_lock_);
78 };
79 
80 }  // namespace rtc
81 
82 #endif  // MEDIA_BASE_VIDEO_BROADCASTER_H_
83