xref: /aosp_15_r20/external/webrtc/test/pc/e2e/analyzer/video/default_video_quality_analyzer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 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_DEFAULT_VIDEO_QUALITY_ANALYZER_H_
12 #define TEST_PC_E2E_ANALYZER_VIDEO_DEFAULT_VIDEO_QUALITY_ANALYZER_H_
13 
14 #include <atomic>
15 #include <cstdint>
16 #include <deque>
17 #include <map>
18 #include <memory>
19 #include <set>
20 #include <string>
21 #include <vector>
22 
23 #include "api/array_view.h"
24 #include "api/test/metrics/metrics_logger.h"
25 #include "api/test/video_quality_analyzer_interface.h"
26 #include "api/units/data_size.h"
27 #include "api/units/timestamp.h"
28 #include "api/video/encoded_image.h"
29 #include "api/video/video_frame.h"
30 #include "rtc_base/synchronization/mutex.h"
31 #include "rtc_base/thread_annotations.h"
32 #include "system_wrappers/include/clock.h"
33 #include "test/pc/e2e/analyzer/video/default_video_quality_analyzer_cpu_measurer.h"
34 #include "test/pc/e2e/analyzer/video/default_video_quality_analyzer_frame_in_flight.h"
35 #include "test/pc/e2e/analyzer/video/default_video_quality_analyzer_frames_comparator.h"
36 #include "test/pc/e2e/analyzer/video/default_video_quality_analyzer_internal_shared_objects.h"
37 #include "test/pc/e2e/analyzer/video/default_video_quality_analyzer_shared_objects.h"
38 #include "test/pc/e2e/analyzer/video/default_video_quality_analyzer_stream_state.h"
39 #include "test/pc/e2e/analyzer/video/names_collection.h"
40 
41 namespace webrtc {
42 
43 class DefaultVideoQualityAnalyzer : public VideoQualityAnalyzerInterface {
44  public:
45   DefaultVideoQualityAnalyzer(webrtc::Clock* clock,
46                               test::MetricsLogger* metrics_logger,
47                               DefaultVideoQualityAnalyzerOptions options = {});
48   ~DefaultVideoQualityAnalyzer() override;
49 
50   void Start(std::string test_case_name,
51              rtc::ArrayView<const std::string> peer_names,
52              int max_threads_count) override;
53   uint16_t OnFrameCaptured(absl::string_view peer_name,
54                            const std::string& stream_label,
55                            const VideoFrame& frame) override;
56   void OnFramePreEncode(absl::string_view peer_name,
57                         const VideoFrame& frame) override;
58   void OnFrameEncoded(absl::string_view peer_name,
59                       uint16_t frame_id,
60                       const EncodedImage& encoded_image,
61                       const EncoderStats& stats,
62                       bool discarded) override;
63   void OnFrameDropped(absl::string_view peer_name,
64                       EncodedImageCallback::DropReason reason) override;
65   void OnFramePreDecode(absl::string_view peer_name,
66                         uint16_t frame_id,
67                         const EncodedImage& input_image) override;
68   void OnFrameDecoded(absl::string_view peer_name,
69                       const VideoFrame& frame,
70                       const DecoderStats& stats) override;
71   void OnFrameRendered(absl::string_view peer_name,
72                        const VideoFrame& frame) override;
73   void OnEncoderError(absl::string_view peer_name,
74                       const VideoFrame& frame,
75                       int32_t error_code) override;
76   void OnDecoderError(absl::string_view peer_name,
77                       uint16_t frame_id,
78                       int32_t error_code,
79                       const DecoderStats& stats) override;
80 
81   void RegisterParticipantInCall(absl::string_view peer_name) override;
82   void UnregisterParticipantInCall(absl::string_view peer_name) override;
83 
84   void Stop() override;
85   std::string GetStreamLabel(uint16_t frame_id) override;
OnStatsReports(absl::string_view pc_label,const rtc::scoped_refptr<const RTCStatsReport> & report)86   void OnStatsReports(
87       absl::string_view pc_label,
88       const rtc::scoped_refptr<const RTCStatsReport>& report) override {}
89 
90   // Returns set of stream labels, that were met during test call.
91   std::set<StatsKey> GetKnownVideoStreams() const;
92   VideoStreamsInfo GetKnownStreams() const;
93   FrameCounters GetGlobalCounters() const;
94   // Returns frame counter for frames received without frame id set.
95   std::map<std::string, FrameCounters> GetUnknownSenderFrameCounters() const;
96   // Returns frame counter per stream label. Valid stream labels can be obtained
97   // by calling GetKnownVideoStreams()
98   std::map<StatsKey, FrameCounters> GetPerStreamCounters() const;
99   // Returns video quality stats per stream label. Valid stream labels can be
100   // obtained by calling GetKnownVideoStreams()
101   std::map<StatsKey, StreamStats> GetStats() const;
102   AnalyzerStats GetAnalyzerStats() const;
103   double GetCpuUsagePercent();
104 
105   // Returns mapping from the stream label to the history of frames that were
106   // met in this stream in the order as they were captured.
107   std::map<std::string, std::vector<uint16_t>> GetStreamFrames() const;
108 
109  private:
110   enum State { kNew, kActive, kStopped };
111 
112   // Returns next frame id to use. Frame ID can't be `VideoFrame::kNotSetId`,
113   // because this value is reserved by `VideoFrame` as "ID not set".
114   uint16_t GetNextFrameId() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
115 
116   void AddExistingFramesInFlightForStreamToComparator(size_t stream_index,
117                                                       StreamState& stream_state,
118                                                       size_t peer_index)
119       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
120 
121   // Report results for all metrics for all streams.
122   void ReportResults();
123   void ReportResults(const InternalStatsKey& key,
124                      const StreamStats& stats,
125                      const FrameCounters& frame_counters)
126       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
127   // Returns name of current test case for reporting.
128   std::string GetTestCaseName(const std::string& stream_label) const;
129   Timestamp Now();
130   StatsKey ToStatsKey(const InternalStatsKey& key) const
131       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
132   // Returns string representation of stats key for metrics naming. Used for
133   // backward compatibility by metrics naming for 2 peers cases.
134   std::string ToMetricName(const InternalStatsKey& key) const
135       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
136 
137   static const uint16_t kStartingFrameId = 1;
138 
139   const DefaultVideoQualityAnalyzerOptions options_;
140   webrtc::Clock* const clock_;
141   test::MetricsLogger* const metrics_logger_;
142 
143   std::string test_label_;
144 
145   mutable Mutex mutex_;
146   uint16_t next_frame_id_ RTC_GUARDED_BY(mutex_) = kStartingFrameId;
147   std::unique_ptr<NamesCollection> peers_ RTC_GUARDED_BY(mutex_);
148   State state_ RTC_GUARDED_BY(mutex_) = State::kNew;
149   Timestamp start_time_ RTC_GUARDED_BY(mutex_) = Timestamp::MinusInfinity();
150   // Mapping from stream label to unique size_t value to use in stats and avoid
151   // extra string copying.
152   NamesCollection streams_ RTC_GUARDED_BY(mutex_);
153   // Frames that were captured by all streams and still aren't rendered on
154   // receivers or deemed dropped. Frame with id X can be removed from this map
155   // if:
156   // 1. The frame with id X was received in OnFrameRendered by all expected
157   //    receivers.
158   // 2. The frame with id Y > X was received in OnFrameRendered by all expected
159   //    receivers.
160   // 3. Next available frame id for newly captured frame is X
161   // 4. There too many frames in flight for current video stream and X is the
162   //    oldest frame id in this stream. In such case only the frame content
163   //    will be removed, but the map entry will be preserved.
164   std::map<uint16_t, FrameInFlight> captured_frames_in_flight_
165       RTC_GUARDED_BY(mutex_);
166   // Global frames count for all video streams.
167   FrameCounters frame_counters_ RTC_GUARDED_BY(mutex_);
168   // Frame counters for received frames without video frame id set.
169   // Map from peer name to the frame counters.
170   std::map<std::string, FrameCounters> unknown_sender_frame_counters_
171       RTC_GUARDED_BY(mutex_);
172   // Frame counters per each stream per each receiver.
173   std::map<InternalStatsKey, FrameCounters> stream_frame_counters_
174       RTC_GUARDED_BY(mutex_);
175   // Map from stream index in `streams_` to its StreamState.
176   std::map<size_t, StreamState> stream_states_ RTC_GUARDED_BY(mutex_);
177   // Map from stream index in `streams_` to sender peer index in `peers_`.
178   std::map<size_t, size_t> stream_to_sender_ RTC_GUARDED_BY(mutex_);
179 
180   // Stores history mapping between stream index in `streams_` and frame ids.
181   // Updated when frame id overlap. It required to properly return stream label
182   // after 1st frame from simulcast streams was already rendered and last is
183   // still encoding.
184   std::map<size_t, std::set<uint16_t>> stream_to_frame_id_history_
185       RTC_GUARDED_BY(mutex_);
186   // Map from stream index to the list of frames as they were met in the stream.
187   std::map<size_t, std::vector<uint16_t>> stream_to_frame_id_full_history_
188       RTC_GUARDED_BY(mutex_);
189   AnalyzerStats analyzer_stats_ RTC_GUARDED_BY(mutex_);
190 
191   DefaultVideoQualityAnalyzerCpuMeasurer cpu_measurer_;
192   DefaultVideoQualityAnalyzerFramesComparator frames_comparator_;
193 };
194 
195 }  // namespace webrtc
196 
197 #endif  // TEST_PC_E2E_ANALYZER_VIDEO_DEFAULT_VIDEO_QUALITY_ANALYZER_H_
198