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/scoped_refptr.h" 15 #include "api/video/video_frame_buffer.h" 16 #include "api/video/video_source_interface.h" 17 #include "media/base/video_source_base.h" 18 #include "rtc_base/synchronization/mutex.h" 19 #include "rtc_base/thread_annotations.h" 20 #include "rtc_base/thread_checker.h" 21 22 namespace rtc { 23 24 // VideoBroadcaster broadcast video frames to sinks and combines VideoSinkWants 25 // from its sinks. It does that by implementing rtc::VideoSourceInterface and 26 // rtc::VideoSinkInterface. The class is threadsafe; methods may be called on 27 // any thread. This is needed because VideoStreamEncoder calls AddOrUpdateSink 28 // both on the worker thread and on the encoder task queue. 29 class VideoBroadcaster : public VideoSourceBase, 30 public VideoSinkInterface<webrtc::VideoFrame> { 31 public: 32 VideoBroadcaster(); 33 ~VideoBroadcaster() override; 34 void AddOrUpdateSink(VideoSinkInterface<webrtc::VideoFrame>* sink, 35 const VideoSinkWants& wants) override; 36 void RemoveSink(VideoSinkInterface<webrtc::VideoFrame>* sink) override; 37 38 // Returns true if the next frame will be delivered to at least one sink. 39 bool frame_wanted() const; 40 41 // Returns VideoSinkWants a source is requested to fulfill. They are 42 // aggregated by all VideoSinkWants from all sinks. 43 VideoSinkWants wants() const; 44 45 // This method ensures that if a sink sets rotation_applied == true, 46 // it will never receive a frame with pending rotation. Our caller 47 // may pass in frames without precise synchronization with changes 48 // to the VideoSinkWants. 49 void OnFrame(const webrtc::VideoFrame& frame) override; 50 51 void OnDiscardedFrame() override; 52 53 protected: 54 void UpdateWants() RTC_EXCLUSIVE_LOCKS_REQUIRED(sinks_and_wants_lock_); 55 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& GetBlackFrameBuffer( 56 int width, 57 int height) RTC_EXCLUSIVE_LOCKS_REQUIRED(sinks_and_wants_lock_); 58 59 mutable webrtc::Mutex sinks_and_wants_lock_; 60 61 VideoSinkWants current_wants_ RTC_GUARDED_BY(sinks_and_wants_lock_); 62 rtc::scoped_refptr<webrtc::VideoFrameBuffer> black_frame_buffer_; 63 bool previous_frame_sent_to_all_sinks_ RTC_GUARDED_BY(sinks_and_wants_lock_) = 64 true; 65 }; 66 67 } // namespace rtc 68 69 #endif // MEDIA_BASE_VIDEO_BROADCASTER_H_ 70