xref: /aosp_15_r20/external/webrtc/media/base/video_source_base.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef MEDIA_BASE_VIDEO_SOURCE_BASE_H_
12*d9f75844SAndroid Build Coastguard Worker #define MEDIA_BASE_VIDEO_SOURCE_BASE_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <vector>
15*d9f75844SAndroid Build Coastguard Worker 
16*d9f75844SAndroid Build Coastguard Worker #include "api/sequence_checker.h"
17*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_frame.h"
18*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_sink_interface.h"
19*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_source_interface.h"
20*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/no_unique_address.h"
21*d9f75844SAndroid Build Coastguard Worker 
22*d9f75844SAndroid Build Coastguard Worker namespace rtc {
23*d9f75844SAndroid Build Coastguard Worker 
24*d9f75844SAndroid Build Coastguard Worker // VideoSourceBase is not thread safe. Before using this class, consider using
25*d9f75844SAndroid Build Coastguard Worker // VideoSourceBaseGuarded below instead, which is an identical implementation
26*d9f75844SAndroid Build Coastguard Worker // but applies a sequence checker to help protect internal state.
27*d9f75844SAndroid Build Coastguard Worker // TODO(bugs.webrtc.org/12780): Delete this class.
28*d9f75844SAndroid Build Coastguard Worker class VideoSourceBase : public VideoSourceInterface<webrtc::VideoFrame> {
29*d9f75844SAndroid Build Coastguard Worker  public:
30*d9f75844SAndroid Build Coastguard Worker   VideoSourceBase();
31*d9f75844SAndroid Build Coastguard Worker   ~VideoSourceBase() override;
32*d9f75844SAndroid Build Coastguard Worker   void AddOrUpdateSink(VideoSinkInterface<webrtc::VideoFrame>* sink,
33*d9f75844SAndroid Build Coastguard Worker                        const VideoSinkWants& wants) override;
34*d9f75844SAndroid Build Coastguard Worker   void RemoveSink(VideoSinkInterface<webrtc::VideoFrame>* sink) override;
35*d9f75844SAndroid Build Coastguard Worker 
36*d9f75844SAndroid Build Coastguard Worker  protected:
37*d9f75844SAndroid Build Coastguard Worker   struct SinkPair {
SinkPairSinkPair38*d9f75844SAndroid Build Coastguard Worker     SinkPair(VideoSinkInterface<webrtc::VideoFrame>* sink, VideoSinkWants wants)
39*d9f75844SAndroid Build Coastguard Worker         : sink(sink), wants(wants) {}
40*d9f75844SAndroid Build Coastguard Worker     VideoSinkInterface<webrtc::VideoFrame>* sink;
41*d9f75844SAndroid Build Coastguard Worker     VideoSinkWants wants;
42*d9f75844SAndroid Build Coastguard Worker   };
43*d9f75844SAndroid Build Coastguard Worker   SinkPair* FindSinkPair(const VideoSinkInterface<webrtc::VideoFrame>* sink);
44*d9f75844SAndroid Build Coastguard Worker 
sink_pairs()45*d9f75844SAndroid Build Coastguard Worker   const std::vector<SinkPair>& sink_pairs() const { return sinks_; }
46*d9f75844SAndroid Build Coastguard Worker 
47*d9f75844SAndroid Build Coastguard Worker  private:
48*d9f75844SAndroid Build Coastguard Worker   std::vector<SinkPair> sinks_;
49*d9f75844SAndroid Build Coastguard Worker };
50*d9f75844SAndroid Build Coastguard Worker 
51*d9f75844SAndroid Build Coastguard Worker // VideoSourceBaseGuarded assumes that operations related to sinks, occur on the
52*d9f75844SAndroid Build Coastguard Worker // same TQ/thread that the object was constructed on.
53*d9f75844SAndroid Build Coastguard Worker class VideoSourceBaseGuarded : public VideoSourceInterface<webrtc::VideoFrame> {
54*d9f75844SAndroid Build Coastguard Worker  public:
55*d9f75844SAndroid Build Coastguard Worker   VideoSourceBaseGuarded();
56*d9f75844SAndroid Build Coastguard Worker   ~VideoSourceBaseGuarded() override;
57*d9f75844SAndroid Build Coastguard Worker 
58*d9f75844SAndroid Build Coastguard Worker   void AddOrUpdateSink(VideoSinkInterface<webrtc::VideoFrame>* sink,
59*d9f75844SAndroid Build Coastguard Worker                        const VideoSinkWants& wants) override;
60*d9f75844SAndroid Build Coastguard Worker   void RemoveSink(VideoSinkInterface<webrtc::VideoFrame>* sink) override;
61*d9f75844SAndroid Build Coastguard Worker 
62*d9f75844SAndroid Build Coastguard Worker  protected:
63*d9f75844SAndroid Build Coastguard Worker   struct SinkPair {
SinkPairSinkPair64*d9f75844SAndroid Build Coastguard Worker     SinkPair(VideoSinkInterface<webrtc::VideoFrame>* sink, VideoSinkWants wants)
65*d9f75844SAndroid Build Coastguard Worker         : sink(sink), wants(wants) {}
66*d9f75844SAndroid Build Coastguard Worker     VideoSinkInterface<webrtc::VideoFrame>* sink;
67*d9f75844SAndroid Build Coastguard Worker     VideoSinkWants wants;
68*d9f75844SAndroid Build Coastguard Worker   };
69*d9f75844SAndroid Build Coastguard Worker 
70*d9f75844SAndroid Build Coastguard Worker   SinkPair* FindSinkPair(const VideoSinkInterface<webrtc::VideoFrame>* sink);
71*d9f75844SAndroid Build Coastguard Worker   const std::vector<SinkPair>& sink_pairs() const;
72*d9f75844SAndroid Build Coastguard Worker 
73*d9f75844SAndroid Build Coastguard Worker   // Keep the `source_sequence_` checker protected to allow sub classes the
74*d9f75844SAndroid Build Coastguard Worker   // ability to call Detach() if/when appropriate.
75*d9f75844SAndroid Build Coastguard Worker   RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker source_sequence_;
76*d9f75844SAndroid Build Coastguard Worker 
77*d9f75844SAndroid Build Coastguard Worker  private:
78*d9f75844SAndroid Build Coastguard Worker   std::vector<SinkPair> sinks_ RTC_GUARDED_BY(&source_sequence_);
79*d9f75844SAndroid Build Coastguard Worker };
80*d9f75844SAndroid Build Coastguard Worker 
81*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
82*d9f75844SAndroid Build Coastguard Worker 
83*d9f75844SAndroid Build Coastguard Worker #endif  // MEDIA_BASE_VIDEO_SOURCE_BASE_H_
84