xref: /aosp_15_r20/external/webrtc/test/pc/e2e/analyzer/video/analyzing_video_sinks_helper.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2022 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 TEST_PC_E2E_ANALYZER_VIDEO_ANALYZING_VIDEO_SINKS_HELPER_H_
12 #define TEST_PC_E2E_ANALYZER_VIDEO_ANALYZING_VIDEO_SINKS_HELPER_H_
13 
14 #include <list>
15 #include <map>
16 #include <memory>
17 #include <set>
18 #include <string>
19 #include <utility>
20 
21 #include "absl/strings/string_view.h"
22 #include "absl/types/optional.h"
23 #include "api/test/pclf/media_configuration.h"
24 #include "api/test/video/video_frame_writer.h"
25 #include "rtc_base/synchronization/mutex.h"
26 #include "rtc_base/thread_annotations.h"
27 
28 namespace webrtc {
29 namespace webrtc_pc_e2e {
30 
31 // Registry of known video configs and video writers.
32 // This class is thread safe.
33 class AnalyzingVideoSinksHelper {
34  public:
35   // Adds config in the registry. If config with such stream label was
36   // registered before, the new value will override the old one.
37   void AddConfig(absl::string_view sender_peer_name, VideoConfig config);
38   absl::optional<std::pair<std::string, VideoConfig>> GetPeerAndConfig(
39       absl::string_view stream_label);
40   // Removes video config for specified stream label. If there are no know video
41   // config for such stream label - does nothing.
42   void RemoveConfig(absl::string_view stream_label);
43 
44   // Takes ownership of the provided video writer. All video writers owned by
45   // this class will be closed during `AnalyzingVideoSinksHelper` destruction
46   // and guaranteed to be alive either until explicitly removed by
47   // `CloseAndRemoveVideoWriters` or until `AnalyzingVideoSinksHelper` is
48   // destroyed.
49   //
50   // Returns pointer to the added writer. Ownership is maintained by
51   // `AnalyzingVideoSinksHelper`.
52   test::VideoFrameWriter* AddVideoWriter(
53       std::unique_ptr<test::VideoFrameWriter> video_writer);
54   // For each provided `writers_to_close`, if it is known, will close and
55   // destroy it, otherwise does nothing with it.
56   void CloseAndRemoveVideoWriters(
57       std::set<test::VideoFrameWriter*> writers_to_close);
58 
59   // Removes all added configs and close and removes all added writers.
60   void Clear();
61 
62  private:
63   Mutex mutex_;
64   std::map<std::string, std::pair<std::string, VideoConfig>> video_configs_
65       RTC_GUARDED_BY(mutex_);
66   std::list<std::unique_ptr<test::VideoFrameWriter>> video_writers_
67       RTC_GUARDED_BY(mutex_);
68 };
69 
70 }  // namespace webrtc_pc_e2e
71 }  // namespace webrtc
72 
73 #endif  // TEST_PC_E2E_ANALYZER_VIDEO_ANALYZING_VIDEO_SINKS_HELPER_H_
74