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