blob: a8e21fa5b8603dfbacb0d9623ae6fd3ec2125ca4 [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"
19#include "media/base/videosinkinterface.h"
20#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();
nisseacd935b2016-11-11 03:55:13 -080036 void AddOrUpdateSink(VideoSinkInterface<webrtc::VideoFrame>* sink,
Pera5092412016-02-12 13:30:57 +010037 const VideoSinkWants& wants) override;
nisseacd935b2016-11-11 03:55:13 -080038 void RemoveSink(VideoSinkInterface<webrtc::VideoFrame>* sink) override;
Pera5092412016-02-12 13:30:57 +010039
40 // Returns true if the next frame will be delivered to at least one sink.
41 bool frame_wanted() const;
42
43 // Returns VideoSinkWants a source is requested to fulfill. They are
44 // aggregated by all VideoSinkWants from all sinks.
45 VideoSinkWants wants() const;
46
nisse6f5a6c32016-09-22 01:25:59 -070047 // This method ensures that if a sink sets rotation_applied == true,
48 // it will never receive a frame with pending rotation. Our caller
49 // may pass in frames without precise synchronization with changes
50 // to the VideoSinkWants.
nisseacd935b2016-11-11 03:55:13 -080051 void OnFrame(const webrtc::VideoFrame& frame) override;
Pera5092412016-02-12 13:30:57 +010052
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020053 void OnDiscardedFrame() override;
54
Pera5092412016-02-12 13:30:57 +010055 protected:
danilchapa37de392017-09-09 04:17:22 -070056 void UpdateWants() RTC_EXCLUSIVE_LOCKS_REQUIRED(sinks_and_wants_lock_);
nisseefec5902016-06-09 00:31:39 -070057 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& GetBlackFrameBuffer(
danilchapa37de392017-09-09 04:17:22 -070058 int width,
59 int height) RTC_EXCLUSIVE_LOCKS_REQUIRED(sinks_and_wants_lock_);
Pera5092412016-02-12 13:30:57 +010060
61 ThreadChecker thread_checker_;
perkjf0dcfe22016-03-10 18:32:00 +010062 rtc::CriticalSection sinks_and_wants_lock_;
Pera5092412016-02-12 13:30:57 +010063
danilchapa37de392017-09-09 04:17:22 -070064 VideoSinkWants current_wants_ RTC_GUARDED_BY(sinks_and_wants_lock_);
nisseefec5902016-06-09 00:31:39 -070065 rtc::scoped_refptr<webrtc::VideoFrameBuffer> black_frame_buffer_;
Pera5092412016-02-12 13:30:57 +010066};
67
68} // namespace rtc
Per8e16e612016-02-11 15:56:51 +010069
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020070#endif // MEDIA_BASE_VIDEOBROADCASTER_H_