video_broadcaster.h 2.7 KB

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