xref: /aosp_15_r20/external/webrtc/media/base/video_source_base.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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_SOURCE_BASE_H_
12 #define MEDIA_BASE_VIDEO_SOURCE_BASE_H_
13 
14 #include <vector>
15 
16 #include "api/sequence_checker.h"
17 #include "api/video/video_frame.h"
18 #include "api/video/video_sink_interface.h"
19 #include "api/video/video_source_interface.h"
20 #include "rtc_base/system/no_unique_address.h"
21 
22 namespace rtc {
23 
24 // VideoSourceBase is not thread safe. Before using this class, consider using
25 // VideoSourceBaseGuarded below instead, which is an identical implementation
26 // but applies a sequence checker to help protect internal state.
27 // TODO(bugs.webrtc.org/12780): Delete this class.
28 class VideoSourceBase : public VideoSourceInterface<webrtc::VideoFrame> {
29  public:
30   VideoSourceBase();
31   ~VideoSourceBase() override;
32   void AddOrUpdateSink(VideoSinkInterface<webrtc::VideoFrame>* sink,
33                        const VideoSinkWants& wants) override;
34   void RemoveSink(VideoSinkInterface<webrtc::VideoFrame>* sink) override;
35 
36  protected:
37   struct SinkPair {
SinkPairSinkPair38     SinkPair(VideoSinkInterface<webrtc::VideoFrame>* sink, VideoSinkWants wants)
39         : sink(sink), wants(wants) {}
40     VideoSinkInterface<webrtc::VideoFrame>* sink;
41     VideoSinkWants wants;
42   };
43   SinkPair* FindSinkPair(const VideoSinkInterface<webrtc::VideoFrame>* sink);
44 
sink_pairs()45   const std::vector<SinkPair>& sink_pairs() const { return sinks_; }
46 
47  private:
48   std::vector<SinkPair> sinks_;
49 };
50 
51 // VideoSourceBaseGuarded assumes that operations related to sinks, occur on the
52 // same TQ/thread that the object was constructed on.
53 class VideoSourceBaseGuarded : public VideoSourceInterface<webrtc::VideoFrame> {
54  public:
55   VideoSourceBaseGuarded();
56   ~VideoSourceBaseGuarded() override;
57 
58   void AddOrUpdateSink(VideoSinkInterface<webrtc::VideoFrame>* sink,
59                        const VideoSinkWants& wants) override;
60   void RemoveSink(VideoSinkInterface<webrtc::VideoFrame>* sink) override;
61 
62  protected:
63   struct SinkPair {
SinkPairSinkPair64     SinkPair(VideoSinkInterface<webrtc::VideoFrame>* sink, VideoSinkWants wants)
65         : sink(sink), wants(wants) {}
66     VideoSinkInterface<webrtc::VideoFrame>* sink;
67     VideoSinkWants wants;
68   };
69 
70   SinkPair* FindSinkPair(const VideoSinkInterface<webrtc::VideoFrame>* sink);
71   const std::vector<SinkPair>& sink_pairs() const;
72 
73   // Keep the `source_sequence_` checker protected to allow sub classes the
74   // ability to call Detach() if/when appropriate.
75   RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker source_sequence_;
76 
77  private:
78   std::vector<SinkPair> sinks_ RTC_GUARDED_BY(&source_sequence_);
79 };
80 
81 }  // namespace rtc
82 
83 #endif  // MEDIA_BASE_VIDEO_SOURCE_BASE_H_
84