blob: 119769d2cf7be7611f8a300dba2c5583261d34bf [file] [log] [blame]
Per8e16e612016-02-11 15:56:51 +01001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MEDIA_BASE_VIDEOBROADCASTER_H_
12#define MEDIA_BASE_VIDEOBROADCASTER_H_
Per8e16e612016-02-11 15:56:51 +010013
kwibergbfefb032016-05-01 14:53:46 -070014#include <memory>
Pera5092412016-02-12 13:30:57 +010015#include <utility>
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/video/video_frame.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020019#include "api/video/video_sink_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "media/base/videosourcebase.h"
21#include "rtc_base/criticalsection.h"
22#include "rtc_base/thread_checker.h"
Pera5092412016-02-12 13:30:57 +010023
24namespace rtc {
25
perkjf0dcfe22016-03-10 18:32:00 +010026// VideoBroadcaster broadcast video frames to sinks and combines
27// VideoSinkWants from its sinks. It does that by implementing
28// rtc::VideoSourceInterface and rtc::VideoSinkInterface.
29// Sinks must be added and removed on one and only one thread.
30// Video frames can be broadcasted on any thread. I.e VideoBroadcaster::OnFrame
31// can be called on any thread.
perkjd6c39542016-03-17 10:35:23 +010032class VideoBroadcaster : public VideoSourceBase,
nisseacd935b2016-11-11 03:55:13 -080033 public VideoSinkInterface<webrtc::VideoFrame> {
Pera5092412016-02-12 13:30:57 +010034 public:
35 VideoBroadcaster();
Paulina Hensmana680a6a2018-04-05 11:42:24 +020036 ~VideoBroadcaster() override;
nisseacd935b2016-11-11 03:55:13 -080037 void AddOrUpdateSink(VideoSinkInterface<webrtc::VideoFrame>* sink,
Pera5092412016-02-12 13:30:57 +010038 const VideoSinkWants& wants) override;
nisseacd935b2016-11-11 03:55:13 -080039 void RemoveSink(VideoSinkInterface<webrtc::VideoFrame>* sink) override;
Pera5092412016-02-12 13:30:57 +010040
41 // Returns true if the next frame will be delivered to at least one sink.
42 bool frame_wanted() const;
43
44 // Returns VideoSinkWants a source is requested to fulfill. They are
45 // aggregated by all VideoSinkWants from all sinks.
46 VideoSinkWants wants() const;
47
nisse6f5a6c32016-09-22 01:25:59 -070048 // This method ensures that if a sink sets rotation_applied == true,
49 // it will never receive a frame with pending rotation. Our caller
50 // may pass in frames without precise synchronization with changes
51 // to the VideoSinkWants.
nisseacd935b2016-11-11 03:55:13 -080052 void OnFrame(const webrtc::VideoFrame& frame) override;
Pera5092412016-02-12 13:30:57 +010053
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020054 void OnDiscardedFrame() override;
55
Pera5092412016-02-12 13:30:57 +010056 protected:
danilchapa37de392017-09-09 04:17:22 -070057 void UpdateWants() RTC_EXCLUSIVE_LOCKS_REQUIRED(sinks_and_wants_lock_);
nisseefec5902016-06-09 00:31:39 -070058 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& GetBlackFrameBuffer(
danilchapa37de392017-09-09 04:17:22 -070059 int width,
60 int height) RTC_EXCLUSIVE_LOCKS_REQUIRED(sinks_and_wants_lock_);
Pera5092412016-02-12 13:30:57 +010061
62 ThreadChecker thread_checker_;
perkjf0dcfe22016-03-10 18:32:00 +010063 rtc::CriticalSection sinks_and_wants_lock_;
Pera5092412016-02-12 13:30:57 +010064
danilchapa37de392017-09-09 04:17:22 -070065 VideoSinkWants current_wants_ RTC_GUARDED_BY(sinks_and_wants_lock_);
nisseefec5902016-06-09 00:31:39 -070066 rtc::scoped_refptr<webrtc::VideoFrameBuffer> black_frame_buffer_;
Pera5092412016-02-12 13:30:57 +010067};
68
69} // namespace rtc
Per8e16e612016-02-11 15:56:51 +010070
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020071#endif // MEDIA_BASE_VIDEOBROADCASTER_H_