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