blob: 4e2abcd4e1077b7ce4e681efc6670c2abd986091 [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
Niels Möller1c931c42018-12-18 16:08:11 +010019#include "api/video/video_source_interface.h"
Bjorn Terelius5c2f1f02019-01-16 17:45:05 +010020#include "rtc_base/time_utils.h"
Sebastian Janssond4c5d632018-07-10 12:57:37 +020021#include "test/layer_filtering_transport.h"
22#include "test/rtp_file_writer.h"
23#include "test/statistics.h"
Sebastian Janssond4c5d632018-07-10 12:57:37 +020024
25namespace webrtc {
26
27class VideoAnalyzer : public PacketReceiver,
28 public Transport,
Niels Möller88be9722018-10-10 10:58:52 +020029 public rtc::VideoSinkInterface<VideoFrame> {
Sebastian Janssond4c5d632018-07-10 12:57:37 +020030 public:
31 VideoAnalyzer(test::LayerFilteringTransport* transport,
32 const std::string& test_label,
33 double avg_psnr_threshold,
34 double avg_ssim_threshold,
35 int duration_frames,
36 FILE* graph_data_output_file,
37 const std::string& graph_title,
38 uint32_t ssrc_to_analyze,
39 uint32_t rtx_ssrc_to_analyze,
40 size_t selected_stream,
41 int selected_sl,
42 int selected_tl,
43 bool is_quick_test_enabled,
44 Clock* clock,
45 std::string rtp_dump_name);
46 ~VideoAnalyzer();
47
48 virtual void SetReceiver(PacketReceiver* receiver);
Niels Möller1c931c42018-12-18 16:08:11 +010049 void SetSource(rtc::VideoSourceInterface<VideoFrame>* video_source,
Sebastian Janssonf1f363f2018-08-13 14:24:58 +020050 bool respect_sink_wants);
Sebastian Janssond4c5d632018-07-10 12:57:37 +020051 void SetCall(Call* call);
52 void SetSendStream(VideoSendStream* stream);
53 void SetReceiveStream(VideoReceiveStream* stream);
Christoffer Rodbroc2a02882018-08-07 14:10:56 +020054 void SetAudioReceiveStream(AudioReceiveStream* recv_stream);
55
Sebastian Janssond4c5d632018-07-10 12:57:37 +020056 rtc::VideoSinkInterface<VideoFrame>* InputInterface();
57 rtc::VideoSourceInterface<VideoFrame>* OutputInterface();
58
59 DeliveryStatus DeliverPacket(MediaType media_type,
60 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +020061 int64_t packet_time_us) override;
Sebastian Janssond4c5d632018-07-10 12:57:37 +020062
63 void PreEncodeOnFrame(const VideoFrame& video_frame);
Niels Möller88be9722018-10-10 10:58:52 +020064 void PostEncodeOnFrame(size_t stream_id, uint32_t timestamp);
Sebastian Janssond4c5d632018-07-10 12:57:37 +020065
66 bool SendRtp(const uint8_t* packet,
67 size_t length,
68 const PacketOptions& options) override;
69
70 bool SendRtcp(const uint8_t* packet, size_t length) override;
71 void OnFrame(const VideoFrame& video_frame) override;
72 void Wait();
73
Sebastian Janssond4c5d632018-07-10 12:57:37 +020074 void StartMeasuringCpuProcessTime();
75 void StopMeasuringCpuProcessTime();
76 void StartExcludingCpuThreadTime();
77 void StopExcludingCpuThreadTime();
78 double GetCpuUsagePercent();
79
80 test::LayerFilteringTransport* const transport_;
81 PacketReceiver* receiver_;
82
83 private:
84 struct FrameComparison {
85 FrameComparison();
86 FrameComparison(const VideoFrame& reference,
87 const VideoFrame& render,
88 bool dropped,
89 int64_t input_time_ms,
90 int64_t send_time_ms,
91 int64_t recv_time_ms,
92 int64_t render_time_ms,
93 size_t encoded_frame_size);
94 FrameComparison(bool dropped,
95 int64_t input_time_ms,
96 int64_t send_time_ms,
97 int64_t recv_time_ms,
98 int64_t render_time_ms,
99 size_t encoded_frame_size);
100
101 absl::optional<VideoFrame> reference;
102 absl::optional<VideoFrame> render;
103 bool dropped;
104 int64_t input_time_ms;
105 int64_t send_time_ms;
106 int64_t recv_time_ms;
107 int64_t render_time_ms;
108 size_t encoded_frame_size;
109 };
110
111 struct Sample {
112 Sample(int dropped,
113 int64_t input_time_ms,
114 int64_t send_time_ms,
115 int64_t recv_time_ms,
116 int64_t render_time_ms,
117 size_t encoded_frame_size,
118 double psnr,
119 double ssim);
120
121 int dropped;
122 int64_t input_time_ms;
123 int64_t send_time_ms;
124 int64_t recv_time_ms;
125 int64_t render_time_ms;
126 size_t encoded_frame_size;
127 double psnr;
128 double ssim;
129 };
130
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200131 // Implements VideoSinkInterface to receive captured frames from a
132 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
133 // as a source to VideoSendStream.
134 // It forwards all input frames to the VideoAnalyzer for later comparison and
135 // forwards the captured frames to the VideoSendStream.
136 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
137 public rtc::VideoSourceInterface<VideoFrame> {
138 public:
Ilya Nikolaevskiyb2d71412019-01-25 13:50:22 +0000139 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer, Clock* clock);
Niels Möller1c931c42018-12-18 16:08:11 +0100140 void SetSource(rtc::VideoSourceInterface<VideoFrame>* video_source);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200141
142 private:
143 void OnFrame(const VideoFrame& video_frame) override;
144
145 // Called when |send_stream_.SetSource()| is called.
146 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
147 const rtc::VideoSinkWants& wants) override;
148
149 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
150 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override;
151
152 VideoAnalyzer* const analyzer_;
153 rtc::CriticalSection crit_;
154 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_
155 RTC_GUARDED_BY(crit_);
Niels Möller1c931c42018-12-18 16:08:11 +0100156 VideoSourceInterface<VideoFrame>* video_source_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200157 Clock* clock_;
158 };
159
160 struct FrameWithPsnr {
161 double psnr;
162 VideoFrame frame;
163 };
164
165 bool IsInSelectedSpatialAndTemporalLayer(const uint8_t* packet,
166 size_t length,
167 const RTPHeader& header);
168
169 void AddFrameComparison(const VideoFrame& reference,
170 const VideoFrame& render,
171 bool dropped,
172 int64_t render_time_ms)
173 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
174
175 static void PollStatsThread(void* obj);
176 void PollStats();
177 static bool FrameComparisonThread(void* obj);
178 bool CompareFrames();
179 bool PopComparison(FrameComparison* comparison);
180 // Increment counter for number of frames received for comparison.
181 void FrameRecorded();
182 // Returns true if all frames to be compared have been taken from the queue.
183 bool AllFramesRecorded();
184 // Increase count of number of frames processed. Returns true if this was the
185 // last frame to be processed.
186 bool FrameProcessed();
187 void PrintResults();
188 void PerformFrameComparison(const FrameComparison& comparison);
189 void PrintResult(const char* result_type,
190 test::Statistics stats,
191 const char* unit);
192 void PrintSamplesToFile(void);
193 double GetAverageMediaBitrateBps();
194 void AddCapturedFrameForComparison(const VideoFrame& video_frame);
195
196 Call* call_;
197 VideoSendStream* send_stream_;
198 VideoReceiveStream* receive_stream_;
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200199 AudioReceiveStream* audio_receive_stream_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200200 CapturedFrameForwarder captured_frame_forwarder_;
201 const std::string test_label_;
202 FILE* const graph_data_output_file_;
203 const std::string graph_title_;
204 const uint32_t ssrc_to_analyze_;
205 const uint32_t rtx_ssrc_to_analyze_;
206 const size_t selected_stream_;
207 const int selected_sl_;
208 const int selected_tl_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200209
210 rtc::CriticalSection comparison_lock_;
211 std::vector<Sample> samples_ RTC_GUARDED_BY(comparison_lock_);
212 test::Statistics sender_time_ RTC_GUARDED_BY(comparison_lock_);
213 test::Statistics receiver_time_ RTC_GUARDED_BY(comparison_lock_);
214 test::Statistics network_time_ RTC_GUARDED_BY(comparison_lock_);
215 test::Statistics psnr_ RTC_GUARDED_BY(comparison_lock_);
216 test::Statistics ssim_ RTC_GUARDED_BY(comparison_lock_);
217 test::Statistics end_to_end_ RTC_GUARDED_BY(comparison_lock_);
218 test::Statistics rendered_delta_ RTC_GUARDED_BY(comparison_lock_);
219 test::Statistics encoded_frame_size_ RTC_GUARDED_BY(comparison_lock_);
220 test::Statistics encode_frame_rate_ RTC_GUARDED_BY(comparison_lock_);
221 test::Statistics encode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
222 test::Statistics encode_usage_percent_ RTC_GUARDED_BY(comparison_lock_);
223 test::Statistics decode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
224 test::Statistics decode_time_max_ms_ RTC_GUARDED_BY(comparison_lock_);
225 test::Statistics media_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
226 test::Statistics fec_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
227 test::Statistics send_bandwidth_bps_ RTC_GUARDED_BY(comparison_lock_);
228 test::Statistics memory_usage_ RTC_GUARDED_BY(comparison_lock_);
229 test::Statistics time_between_freezes_ RTC_GUARDED_BY(comparison_lock_);
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200230 test::Statistics audio_expand_rate_ RTC_GUARDED_BY(comparison_lock_);
231 test::Statistics audio_accelerate_rate_ RTC_GUARDED_BY(comparison_lock_);
232 test::Statistics audio_jitter_buffer_ms_ RTC_GUARDED_BY(comparison_lock_);
Ilya Nikolaevskiyd47d3eb2019-01-21 16:27:17 +0100233 test::Statistics pixels_ RTC_GUARDED_BY(comparison_lock_);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200234 // Rendered frame with worst PSNR is saved for further analysis.
235 absl::optional<FrameWithPsnr> worst_frame_ RTC_GUARDED_BY(comparison_lock_);
236
237 size_t last_fec_bytes_;
238
239 const int frames_to_process_;
240 int frames_recorded_;
241 int frames_processed_;
242 int dropped_frames_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200243 int dropped_frames_before_first_encode_;
244 int dropped_frames_before_rendering_;
245 int64_t last_render_time_;
246 int64_t last_render_delta_ms_;
247 int64_t last_unfreeze_time_ms_;
248 uint32_t rtp_timestamp_delta_;
249 int64_t total_media_bytes_;
250 int64_t first_sending_time_;
251 int64_t last_sending_time_;
252
253 rtc::CriticalSection cpu_measurement_lock_;
254 int64_t cpu_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
255 int64_t wallclock_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
256
257 rtc::CriticalSection crit_;
258 std::deque<VideoFrame> frames_ RTC_GUARDED_BY(crit_);
259 absl::optional<VideoFrame> last_rendered_frame_ RTC_GUARDED_BY(crit_);
260 rtc::TimestampWrapAroundHandler wrap_handler_ RTC_GUARDED_BY(crit_);
261 std::map<int64_t, int64_t> send_times_ RTC_GUARDED_BY(crit_);
262 std::map<int64_t, int64_t> recv_times_ RTC_GUARDED_BY(crit_);
263 std::map<int64_t, size_t> encoded_frame_sizes_ RTC_GUARDED_BY(crit_);
264 absl::optional<uint32_t> first_encoded_timestamp_ RTC_GUARDED_BY(crit_);
265 absl::optional<uint32_t> first_sent_timestamp_ RTC_GUARDED_BY(crit_);
266 const double avg_psnr_threshold_;
267 const double avg_ssim_threshold_;
268 bool is_quick_test_enabled_;
269
270 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
271 rtc::PlatformThread stats_polling_thread_;
272 rtc::Event comparison_available_event_;
273 std::deque<FrameComparison> comparisons_ RTC_GUARDED_BY(comparison_lock_);
274 rtc::Event done_;
275
276 std::unique_ptr<test::RtpFileWriter> rtp_file_writer_;
277 Clock* const clock_;
278 const int64_t start_ms_;
279};
280
281} // namespace webrtc
282#endif // VIDEO_VIDEO_ANALYZER_H_