blob: be771f423c20d9e140851a3bc89af1858c301966 [file] [log] [blame]
Sebastian Janssond4c5d632018-07-10 12:57:37 +02001/*
2 * Copyright 2018 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#ifndef VIDEO_VIDEO_ANALYZER_H_
11#define VIDEO_VIDEO_ANALYZER_H_
12
13#include <deque>
14#include <map>
15#include <memory>
16#include <string>
17#include <vector>
18
Danil Chapovalov9cd53b42019-10-21 13:36:59 +020019#include "api/task_queue/task_queue_base.h"
Niels Möller1c931c42018-12-18 16:08:11 +010020#include "api/video/video_source_interface.h"
Yves Gerey79e9f4b2019-04-13 18:59:53 +020021#include "rtc_base/numerics/running_statistics.h"
Bjorn Terelius5c2f1f02019-01-16 17:45:05 +010022#include "rtc_base/time_utils.h"
Sebastian Janssond4c5d632018-07-10 12:57:37 +020023#include "test/layer_filtering_transport.h"
24#include "test/rtp_file_writer.h"
Artem Titov82ce3842019-09-23 17:55:52 +020025#include "test/testsupport/perf_test.h"
Sebastian Janssond4c5d632018-07-10 12:57:37 +020026
27namespace webrtc {
28
29class VideoAnalyzer : public PacketReceiver,
30 public Transport,
Niels Möller88be9722018-10-10 10:58:52 +020031 public rtc::VideoSinkInterface<VideoFrame> {
Sebastian Janssond4c5d632018-07-10 12:57:37 +020032 public:
Yves Gerey79e9f4b2019-04-13 18:59:53 +020033 using Statistics = RunningStatistics<double>;
34
Sebastian Janssond4c5d632018-07-10 12:57:37 +020035 VideoAnalyzer(test::LayerFilteringTransport* transport,
36 const std::string& test_label,
37 double avg_psnr_threshold,
38 double avg_ssim_threshold,
39 int duration_frames,
40 FILE* graph_data_output_file,
41 const std::string& graph_title,
42 uint32_t ssrc_to_analyze,
43 uint32_t rtx_ssrc_to_analyze,
44 size_t selected_stream,
45 int selected_sl,
46 int selected_tl,
47 bool is_quick_test_enabled,
48 Clock* clock,
Artem Titovff7730d2019-04-02 13:46:53 +020049 std::string rtp_dump_name,
Danil Chapovalov9cd53b42019-10-21 13:36:59 +020050 TaskQueueBase* task_queue);
Sebastian Janssond4c5d632018-07-10 12:57:37 +020051 ~VideoAnalyzer();
52
53 virtual void SetReceiver(PacketReceiver* receiver);
Niels Möller1c931c42018-12-18 16:08:11 +010054 void SetSource(rtc::VideoSourceInterface<VideoFrame>* video_source,
Sebastian Janssonf1f363f2018-08-13 14:24:58 +020055 bool respect_sink_wants);
Sebastian Janssond4c5d632018-07-10 12:57:37 +020056 void SetCall(Call* call);
57 void SetSendStream(VideoSendStream* stream);
58 void SetReceiveStream(VideoReceiveStream* stream);
Christoffer Rodbroc2a02882018-08-07 14:10:56 +020059 void SetAudioReceiveStream(AudioReceiveStream* recv_stream);
60
Sebastian Janssond4c5d632018-07-10 12:57:37 +020061 rtc::VideoSinkInterface<VideoFrame>* InputInterface();
62 rtc::VideoSourceInterface<VideoFrame>* OutputInterface();
63
64 DeliveryStatus DeliverPacket(MediaType media_type,
65 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +020066 int64_t packet_time_us) override;
Sebastian Janssond4c5d632018-07-10 12:57:37 +020067
68 void PreEncodeOnFrame(const VideoFrame& video_frame);
Niels Möller88be9722018-10-10 10:58:52 +020069 void PostEncodeOnFrame(size_t stream_id, uint32_t timestamp);
Sebastian Janssond4c5d632018-07-10 12:57:37 +020070
71 bool SendRtp(const uint8_t* packet,
72 size_t length,
73 const PacketOptions& options) override;
74
75 bool SendRtcp(const uint8_t* packet, size_t length) override;
76 void OnFrame(const VideoFrame& video_frame) override;
77 void Wait();
78
Sebastian Janssond4c5d632018-07-10 12:57:37 +020079 void StartMeasuringCpuProcessTime();
80 void StopMeasuringCpuProcessTime();
81 void StartExcludingCpuThreadTime();
82 void StopExcludingCpuThreadTime();
83 double GetCpuUsagePercent();
84
85 test::LayerFilteringTransport* const transport_;
86 PacketReceiver* receiver_;
87
88 private:
89 struct FrameComparison {
90 FrameComparison();
91 FrameComparison(const VideoFrame& reference,
92 const VideoFrame& render,
93 bool dropped,
94 int64_t input_time_ms,
95 int64_t send_time_ms,
96 int64_t recv_time_ms,
97 int64_t render_time_ms,
98 size_t encoded_frame_size);
99 FrameComparison(bool dropped,
100 int64_t input_time_ms,
101 int64_t send_time_ms,
102 int64_t recv_time_ms,
103 int64_t render_time_ms,
104 size_t encoded_frame_size);
105
106 absl::optional<VideoFrame> reference;
107 absl::optional<VideoFrame> render;
108 bool dropped;
109 int64_t input_time_ms;
110 int64_t send_time_ms;
111 int64_t recv_time_ms;
112 int64_t render_time_ms;
113 size_t encoded_frame_size;
114 };
115
116 struct Sample {
117 Sample(int dropped,
118 int64_t input_time_ms,
119 int64_t send_time_ms,
120 int64_t recv_time_ms,
121 int64_t render_time_ms,
122 size_t encoded_frame_size,
123 double psnr,
124 double ssim);
125
126 int dropped;
127 int64_t input_time_ms;
128 int64_t send_time_ms;
129 int64_t recv_time_ms;
130 int64_t render_time_ms;
131 size_t encoded_frame_size;
132 double psnr;
133 double ssim;
134 };
135
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200136 // Implements VideoSinkInterface to receive captured frames from a
137 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
138 // as a source to VideoSendStream.
139 // It forwards all input frames to the VideoAnalyzer for later comparison and
140 // forwards the captured frames to the VideoSendStream.
141 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
142 public rtc::VideoSourceInterface<VideoFrame> {
143 public:
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100144 CapturedFrameForwarder(VideoAnalyzer* analyzer,
145 Clock* clock,
Ilya Nikolaevskiy85fc3252019-02-11 10:41:50 +0100146 int frames_to_process);
Niels Möller1c931c42018-12-18 16:08:11 +0100147 void SetSource(rtc::VideoSourceInterface<VideoFrame>* video_source);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200148
149 private:
150 void OnFrame(const VideoFrame& video_frame) override;
151
152 // Called when |send_stream_.SetSource()| is called.
153 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
154 const rtc::VideoSinkWants& wants) override;
155
156 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
157 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override;
158
159 VideoAnalyzer* const analyzer_;
160 rtc::CriticalSection crit_;
161 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_
162 RTC_GUARDED_BY(crit_);
Niels Möller1c931c42018-12-18 16:08:11 +0100163 VideoSourceInterface<VideoFrame>* video_source_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200164 Clock* clock_;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100165 int captured_frames_ RTC_GUARDED_BY(crit_);
Yves Gerey0c67c802019-08-01 17:45:54 +0200166 const int frames_to_process_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200167 };
168
169 struct FrameWithPsnr {
170 double psnr;
171 VideoFrame frame;
172 };
173
174 bool IsInSelectedSpatialAndTemporalLayer(const uint8_t* packet,
175 size_t length,
176 const RTPHeader& header);
177
178 void AddFrameComparison(const VideoFrame& reference,
179 const VideoFrame& render,
180 bool dropped,
181 int64_t render_time_ms)
182 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
183
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200184 void PollStats();
Niels Möller4731f002019-05-03 09:34:24 +0200185 static void FrameComparisonThread(void* obj);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200186 bool CompareFrames();
187 bool PopComparison(FrameComparison* comparison);
188 // Increment counter for number of frames received for comparison.
189 void FrameRecorded();
190 // Returns true if all frames to be compared have been taken from the queue.
191 bool AllFramesRecorded();
192 // Increase count of number of frames processed. Returns true if this was the
193 // last frame to be processed.
194 bool FrameProcessed();
195 void PrintResults();
196 void PerformFrameComparison(const FrameComparison& comparison);
Artem Titov82ce3842019-09-23 17:55:52 +0200197 void PrintResult(const char* result_type,
198 Statistics stats,
199 const char* unit,
200 webrtc::test::ImproveDirection improve_direction);
201 void PrintResultWithExternalMean(
202 const char* result_type,
203 double mean,
204 Statistics stats,
205 const char* unit,
206 webrtc::test::ImproveDirection improve_direction);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200207 void PrintSamplesToFile(void);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200208 void AddCapturedFrameForComparison(const VideoFrame& video_frame);
209
210 Call* call_;
211 VideoSendStream* send_stream_;
212 VideoReceiveStream* receive_stream_;
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200213 AudioReceiveStream* audio_receive_stream_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200214 CapturedFrameForwarder captured_frame_forwarder_;
215 const std::string test_label_;
216 FILE* const graph_data_output_file_;
217 const std::string graph_title_;
218 const uint32_t ssrc_to_analyze_;
219 const uint32_t rtx_ssrc_to_analyze_;
220 const size_t selected_stream_;
221 const int selected_sl_;
222 const int selected_tl_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200223
224 rtc::CriticalSection comparison_lock_;
225 std::vector<Sample> samples_ RTC_GUARDED_BY(comparison_lock_);
Yves Gerey79e9f4b2019-04-13 18:59:53 +0200226 Statistics sender_time_ RTC_GUARDED_BY(comparison_lock_);
227 Statistics receiver_time_ RTC_GUARDED_BY(comparison_lock_);
228 Statistics network_time_ RTC_GUARDED_BY(comparison_lock_);
229 Statistics psnr_ RTC_GUARDED_BY(comparison_lock_);
230 Statistics ssim_ RTC_GUARDED_BY(comparison_lock_);
231 Statistics end_to_end_ RTC_GUARDED_BY(comparison_lock_);
232 Statistics rendered_delta_ RTC_GUARDED_BY(comparison_lock_);
233 Statistics encoded_frame_size_ RTC_GUARDED_BY(comparison_lock_);
234 Statistics encode_frame_rate_ RTC_GUARDED_BY(comparison_lock_);
235 Statistics encode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
236 Statistics encode_usage_percent_ RTC_GUARDED_BY(comparison_lock_);
Johannes Krona1b99b32019-07-30 15:08:16 +0200237 double mean_decode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
Yves Gerey79e9f4b2019-04-13 18:59:53 +0200238 Statistics decode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
239 Statistics decode_time_max_ms_ RTC_GUARDED_BY(comparison_lock_);
240 Statistics media_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
241 Statistics fec_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
242 Statistics send_bandwidth_bps_ RTC_GUARDED_BY(comparison_lock_);
243 Statistics memory_usage_ RTC_GUARDED_BY(comparison_lock_);
Yves Gerey79e9f4b2019-04-13 18:59:53 +0200244 Statistics audio_expand_rate_ RTC_GUARDED_BY(comparison_lock_);
245 Statistics audio_accelerate_rate_ RTC_GUARDED_BY(comparison_lock_);
246 Statistics audio_jitter_buffer_ms_ RTC_GUARDED_BY(comparison_lock_);
247 Statistics pixels_ RTC_GUARDED_BY(comparison_lock_);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200248 // Rendered frame with worst PSNR is saved for further analysis.
249 absl::optional<FrameWithPsnr> worst_frame_ RTC_GUARDED_BY(comparison_lock_);
Elad Alon8c513c72019-05-07 21:22:24 +0200250 // Freeze metrics.
251 Statistics time_between_freezes_ RTC_GUARDED_BY(comparison_lock_);
252 uint32_t freeze_count_ RTC_GUARDED_BY(comparison_lock_);
253 uint32_t total_freezes_duration_ms_ RTC_GUARDED_BY(comparison_lock_);
254 uint32_t total_frames_duration_ms_ RTC_GUARDED_BY(comparison_lock_);
255 double sum_squared_frame_durations_ RTC_GUARDED_BY(comparison_lock_);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200256
Elad Alon58e06572019-05-08 15:34:24 +0200257 double decode_frame_rate_ RTC_GUARDED_BY(comparison_lock_);
258 double render_frame_rate_ RTC_GUARDED_BY(comparison_lock_);
259
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200260 size_t last_fec_bytes_;
261
Yves Gerey0c67c802019-08-01 17:45:54 +0200262 rtc::CriticalSection crit_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200263 const int frames_to_process_;
Yves Gerey0c67c802019-08-01 17:45:54 +0200264 int frames_recorded_ RTC_GUARDED_BY(comparison_lock_);
265 int frames_processed_ RTC_GUARDED_BY(comparison_lock_);
266 int captured_frames_ RTC_GUARDED_BY(comparison_lock_);
267 int dropped_frames_ RTC_GUARDED_BY(comparison_lock_);
268 int dropped_frames_before_first_encode_ RTC_GUARDED_BY(crit_);
269 int dropped_frames_before_rendering_ RTC_GUARDED_BY(crit_);
270 int64_t last_render_time_ RTC_GUARDED_BY(comparison_lock_);
271 int64_t last_render_delta_ms_ RTC_GUARDED_BY(comparison_lock_);
272 int64_t last_unfreeze_time_ms_ RTC_GUARDED_BY(comparison_lock_);
273 uint32_t rtp_timestamp_delta_ RTC_GUARDED_BY(crit_);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200274
275 rtc::CriticalSection cpu_measurement_lock_;
276 int64_t cpu_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
277 int64_t wallclock_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
278
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200279 std::deque<VideoFrame> frames_ RTC_GUARDED_BY(crit_);
280 absl::optional<VideoFrame> last_rendered_frame_ RTC_GUARDED_BY(crit_);
281 rtc::TimestampWrapAroundHandler wrap_handler_ RTC_GUARDED_BY(crit_);
282 std::map<int64_t, int64_t> send_times_ RTC_GUARDED_BY(crit_);
283 std::map<int64_t, int64_t> recv_times_ RTC_GUARDED_BY(crit_);
284 std::map<int64_t, size_t> encoded_frame_sizes_ RTC_GUARDED_BY(crit_);
285 absl::optional<uint32_t> first_encoded_timestamp_ RTC_GUARDED_BY(crit_);
286 absl::optional<uint32_t> first_sent_timestamp_ RTC_GUARDED_BY(crit_);
287 const double avg_psnr_threshold_;
288 const double avg_ssim_threshold_;
289 bool is_quick_test_enabled_;
290
291 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200292 rtc::Event comparison_available_event_;
293 std::deque<FrameComparison> comparisons_ RTC_GUARDED_BY(comparison_lock_);
Niels Möller4731f002019-05-03 09:34:24 +0200294 bool quit_ RTC_GUARDED_BY(comparison_lock_);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200295 rtc::Event done_;
296
297 std::unique_ptr<test::RtpFileWriter> rtp_file_writer_;
298 Clock* const clock_;
299 const int64_t start_ms_;
Danil Chapovalov9cd53b42019-10-21 13:36:59 +0200300 TaskQueueBase* task_queue_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200301};
302
303} // namespace webrtc
304#endif // VIDEO_VIDEO_ANALYZER_H_