xref: /aosp_15_r20/external/webrtc/media/base/video_source_base.cc (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 #include "media/base/video_source_base.h"
12 
13 #include <algorithm>
14 
15 #include "absl/algorithm/container.h"
16 #include "rtc_base/checks.h"
17 
18 namespace rtc {
19 
20 VideoSourceBase::VideoSourceBase() = default;
21 VideoSourceBase::~VideoSourceBase() = default;
22 
AddOrUpdateSink(VideoSinkInterface<webrtc::VideoFrame> * sink,const VideoSinkWants & wants)23 void VideoSourceBase::AddOrUpdateSink(
24     VideoSinkInterface<webrtc::VideoFrame>* sink,
25     const VideoSinkWants& wants) {
26   RTC_DCHECK(sink != nullptr);
27 
28   SinkPair* sink_pair = FindSinkPair(sink);
29   if (!sink_pair) {
30     sinks_.push_back(SinkPair(sink, wants));
31   } else {
32     sink_pair->wants = wants;
33   }
34 }
35 
RemoveSink(VideoSinkInterface<webrtc::VideoFrame> * sink)36 void VideoSourceBase::RemoveSink(VideoSinkInterface<webrtc::VideoFrame>* sink) {
37   RTC_DCHECK(sink != nullptr);
38   RTC_DCHECK(FindSinkPair(sink));
39   sinks_.erase(std::remove_if(sinks_.begin(), sinks_.end(),
40                               [sink](const SinkPair& sink_pair) {
41                                 return sink_pair.sink == sink;
42                               }),
43                sinks_.end());
44 }
45 
FindSinkPair(const VideoSinkInterface<webrtc::VideoFrame> * sink)46 VideoSourceBase::SinkPair* VideoSourceBase::FindSinkPair(
47     const VideoSinkInterface<webrtc::VideoFrame>* sink) {
48   auto sink_pair_it = absl::c_find_if(
49       sinks_,
50       [sink](const SinkPair& sink_pair) { return sink_pair.sink == sink; });
51   if (sink_pair_it != sinks_.end()) {
52     return &*sink_pair_it;
53   }
54   return nullptr;
55 }
56 
57 VideoSourceBaseGuarded::VideoSourceBaseGuarded() = default;
58 VideoSourceBaseGuarded::~VideoSourceBaseGuarded() = default;
59 
AddOrUpdateSink(VideoSinkInterface<webrtc::VideoFrame> * sink,const VideoSinkWants & wants)60 void VideoSourceBaseGuarded::AddOrUpdateSink(
61     VideoSinkInterface<webrtc::VideoFrame>* sink,
62     const VideoSinkWants& wants) {
63   RTC_DCHECK_RUN_ON(&source_sequence_);
64   RTC_DCHECK(sink != nullptr);
65 
66   SinkPair* sink_pair = FindSinkPair(sink);
67   if (!sink_pair) {
68     sinks_.push_back(SinkPair(sink, wants));
69   } else {
70     sink_pair->wants = wants;
71   }
72 }
73 
RemoveSink(VideoSinkInterface<webrtc::VideoFrame> * sink)74 void VideoSourceBaseGuarded::RemoveSink(
75     VideoSinkInterface<webrtc::VideoFrame>* sink) {
76   RTC_DCHECK_RUN_ON(&source_sequence_);
77   RTC_DCHECK(sink != nullptr);
78   RTC_DCHECK(FindSinkPair(sink));
79   sinks_.erase(std::remove_if(sinks_.begin(), sinks_.end(),
80                               [sink](const SinkPair& sink_pair) {
81                                 return sink_pair.sink == sink;
82                               }),
83                sinks_.end());
84 }
85 
FindSinkPair(const VideoSinkInterface<webrtc::VideoFrame> * sink)86 VideoSourceBaseGuarded::SinkPair* VideoSourceBaseGuarded::FindSinkPair(
87     const VideoSinkInterface<webrtc::VideoFrame>* sink) {
88   RTC_DCHECK_RUN_ON(&source_sequence_);
89   auto sink_pair_it = absl::c_find_if(
90       sinks_,
91       [sink](const SinkPair& sink_pair) { return sink_pair.sink == sink; });
92   if (sink_pair_it != sinks_.end()) {
93     return &*sink_pair_it;
94   }
95   return nullptr;
96 }
97 
98 const std::vector<VideoSourceBaseGuarded::SinkPair>&
sink_pairs() const99 VideoSourceBaseGuarded::sink_pairs() const {
100   RTC_DCHECK_RUN_ON(&source_sequence_);
101   return sinks_;
102 }
103 
104 }  // namespace rtc
105