blob: f2822efbea26e3ac9a71dc256d2507da90906150 [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#include "video/video_analyzer.h"
11
12#include <algorithm>
13#include <utility>
14
Steve Antonbd631a02019-03-28 10:51:27 -070015#include "absl/algorithm/container.h"
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020016#include "absl/flags/flag.h"
17#include "absl/flags/parse.h"
Niels Möller1c931c42018-12-18 16:08:11 +010018#include "common_video/libyuv/include/webrtc_libyuv.h"
Danil Chapovalovb57fe172019-12-11 09:38:44 +010019#include "modules/rtp_rtcp/source/create_video_rtp_depacketizer.h"
20#include "modules/rtp_rtcp/source/rtp_packet.h"
Sebastian Janssond4c5d632018-07-10 12:57:37 +020021#include "rtc_base/cpu_time.h"
Sebastian Janssond4c5d632018-07-10 12:57:37 +020022#include "rtc_base/format_macros.h"
23#include "rtc_base/memory_usage.h"
Danil Chapovalov9cd53b42019-10-21 13:36:59 +020024#include "rtc_base/task_queue_for_test.h"
25#include "rtc_base/task_utils/repeating_task.h"
Sebastian Janssond4c5d632018-07-10 12:57:37 +020026#include "system_wrappers/include/cpu_info.h"
27#include "test/call_test.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "test/testsupport/file_utils.h"
Sebastian Janssond4c5d632018-07-10 12:57:37 +020029#include "test/testsupport/frame_writer.h"
30#include "test/testsupport/perf_test.h"
31#include "test/testsupport/test_artifacts.h"
32
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020033ABSL_FLAG(bool,
34 save_worst_frame,
35 false,
36 "Enable saving a frame with the lowest PSNR to a jpeg file in the "
37 "test_artifacts_dir");
Sebastian Janssond4c5d632018-07-10 12:57:37 +020038
39namespace webrtc {
40namespace {
Danil Chapovalov9cd53b42019-10-21 13:36:59 +020041constexpr TimeDelta kSendStatsPollingInterval = TimeDelta::Seconds<1>();
Sebastian Janssond4c5d632018-07-10 12:57:37 +020042constexpr size_t kMaxComparisons = 10;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +010043// How often is keep alive message printed.
44constexpr int kKeepAliveIntervalSeconds = 30;
45// Interval between checking that the test is over.
46constexpr int kProbingIntervalMs = 500;
47constexpr int kKeepAliveIntervalIterations =
48 kKeepAliveIntervalSeconds * 1000 / kProbingIntervalMs;
Sebastian Janssond4c5d632018-07-10 12:57:37 +020049
50bool IsFlexfec(int payload_type) {
51 return payload_type == test::CallTest::kFlexfecPayloadType;
52}
53} // namespace
54
Danil Chapovalov9cd53b42019-10-21 13:36:59 +020055VideoAnalyzer::VideoAnalyzer(test::LayerFilteringTransport* transport,
56 const std::string& test_label,
57 double avg_psnr_threshold,
58 double avg_ssim_threshold,
59 int duration_frames,
60 FILE* graph_data_output_file,
61 const std::string& graph_title,
62 uint32_t ssrc_to_analyze,
63 uint32_t rtx_ssrc_to_analyze,
64 size_t selected_stream,
65 int selected_sl,
66 int selected_tl,
67 bool is_quick_test_enabled,
68 Clock* clock,
69 std::string rtp_dump_name,
70 TaskQueueBase* task_queue)
Sebastian Janssond4c5d632018-07-10 12:57:37 +020071 : transport_(transport),
72 receiver_(nullptr),
73 call_(nullptr),
74 send_stream_(nullptr),
75 receive_stream_(nullptr),
Christoffer Rodbroc2a02882018-08-07 14:10:56 +020076 audio_receive_stream_(nullptr),
Ilya Nikolaevskiy85fc3252019-02-11 10:41:50 +010077 captured_frame_forwarder_(this, clock, duration_frames),
Sebastian Janssond4c5d632018-07-10 12:57:37 +020078 test_label_(test_label),
79 graph_data_output_file_(graph_data_output_file),
80 graph_title_(graph_title),
81 ssrc_to_analyze_(ssrc_to_analyze),
82 rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze),
83 selected_stream_(selected_stream),
84 selected_sl_(selected_sl),
85 selected_tl_(selected_tl),
Johannes Krona1b99b32019-07-30 15:08:16 +020086 mean_decode_time_ms_(0.0),
Elad Alon8c513c72019-05-07 21:22:24 +020087 freeze_count_(0),
88 total_freezes_duration_ms_(0),
89 total_frames_duration_ms_(0),
90 sum_squared_frame_durations_(0),
Elad Alon58e06572019-05-08 15:34:24 +020091 decode_frame_rate_(0),
92 render_frame_rate_(0),
Sebastian Janssond4c5d632018-07-10 12:57:37 +020093 last_fec_bytes_(0),
94 frames_to_process_(duration_frames),
95 frames_recorded_(0),
96 frames_processed_(0),
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +010097 captured_frames_(0),
Yves Gerey0c67c802019-08-01 17:45:54 +020098 dropped_frames_(0),
Sebastian Janssond4c5d632018-07-10 12:57:37 +020099 dropped_frames_before_first_encode_(0),
100 dropped_frames_before_rendering_(0),
101 last_render_time_(0),
102 last_render_delta_ms_(0),
103 last_unfreeze_time_ms_(0),
104 rtp_timestamp_delta_(0),
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200105 cpu_time_(0),
106 wallclock_time_(0),
107 avg_psnr_threshold_(avg_psnr_threshold),
108 avg_ssim_threshold_(avg_ssim_threshold),
109 is_quick_test_enabled_(is_quick_test_enabled),
Niels Möller4731f002019-05-03 09:34:24 +0200110 quit_(false),
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200111 done_(true, false),
Danil Chapovalovb57fe172019-12-11 09:38:44 +0100112 vp8_depacketizer_(CreateVideoRtpDepacketizer(kVideoCodecVP8)),
113 vp9_depacketizer_(CreateVideoRtpDepacketizer(kVideoCodecVP9)),
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200114 clock_(clock),
Artem Titovff7730d2019-04-02 13:46:53 +0200115 start_ms_(clock->TimeInMilliseconds()),
116 task_queue_(task_queue) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200117 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
118
119 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
120 // so that we don't accidentally starve "real" worker threads (codec etc).
121 // Also, don't allocate more than kMaxComparisonThreads, even if there are
122 // spare cores.
123
124 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
125 RTC_DCHECK_GE(num_cores, 1);
126 static const uint32_t kMinCoresLeft = 4;
127 static const uint32_t kMaxComparisonThreads = 8;
128
129 if (num_cores <= kMinCoresLeft) {
130 num_cores = 1;
131 } else {
132 num_cores -= kMinCoresLeft;
133 num_cores = std::min(num_cores, kMaxComparisonThreads);
134 }
135
136 for (uint32_t i = 0; i < num_cores; ++i) {
137 rtc::PlatformThread* thread =
138 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
139 thread->Start();
140 comparison_thread_pool_.push_back(thread);
141 }
142
143 if (!rtp_dump_name.empty()) {
144 fprintf(stdout, "Writing rtp dump to %s\n", rtp_dump_name.c_str());
145 rtp_file_writer_.reset(test::RtpFileWriter::Create(
146 test::RtpFileWriter::kRtpDump, rtp_dump_name));
147 }
148}
149
150VideoAnalyzer::~VideoAnalyzer() {
Niels Möller4731f002019-05-03 09:34:24 +0200151 {
152 rtc::CritScope crit(&comparison_lock_);
153 quit_ = true;
154 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200155 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
156 thread->Stop();
157 delete thread;
158 }
159}
160
161void VideoAnalyzer::SetReceiver(PacketReceiver* receiver) {
162 receiver_ = receiver;
163}
164
Niels Möller1c931c42018-12-18 16:08:11 +0100165void VideoAnalyzer::SetSource(
166 rtc::VideoSourceInterface<VideoFrame>* video_source,
167 bool respect_sink_wants) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200168 if (respect_sink_wants)
Niels Möller1c931c42018-12-18 16:08:11 +0100169 captured_frame_forwarder_.SetSource(video_source);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200170 rtc::VideoSinkWants wants;
Niels Möller1c931c42018-12-18 16:08:11 +0100171 video_source->AddOrUpdateSink(InputInterface(), wants);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200172}
173
174void VideoAnalyzer::SetCall(Call* call) {
175 rtc::CritScope lock(&crit_);
176 RTC_DCHECK(!call_);
177 call_ = call;
178}
179
180void VideoAnalyzer::SetSendStream(VideoSendStream* stream) {
181 rtc::CritScope lock(&crit_);
182 RTC_DCHECK(!send_stream_);
183 send_stream_ = stream;
184}
185
186void VideoAnalyzer::SetReceiveStream(VideoReceiveStream* stream) {
187 rtc::CritScope lock(&crit_);
188 RTC_DCHECK(!receive_stream_);
189 receive_stream_ = stream;
190}
191
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200192void VideoAnalyzer::SetAudioReceiveStream(AudioReceiveStream* recv_stream) {
193 rtc::CritScope lock(&crit_);
194 RTC_CHECK(!audio_receive_stream_);
195 audio_receive_stream_ = recv_stream;
196}
197
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200198rtc::VideoSinkInterface<VideoFrame>* VideoAnalyzer::InputInterface() {
199 return &captured_frame_forwarder_;
200}
201
202rtc::VideoSourceInterface<VideoFrame>* VideoAnalyzer::OutputInterface() {
203 return &captured_frame_forwarder_;
204}
205
206PacketReceiver::DeliveryStatus VideoAnalyzer::DeliverPacket(
207 MediaType media_type,
208 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200209 int64_t packet_time_us) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200210 // Ignore timestamps of RTCP packets. They're not synchronized with
211 // RTP packet timestamps and so they would confuse wrap_handler_.
212 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size())) {
Niels Möller70082872018-08-07 11:03:12 +0200213 return receiver_->DeliverPacket(media_type, std::move(packet),
214 packet_time_us);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200215 }
216
217 if (rtp_file_writer_) {
218 test::RtpPacket p;
219 memcpy(p.data, packet.cdata(), packet.size());
220 p.length = packet.size();
221 p.original_length = packet.size();
222 p.time_ms = clock_->TimeInMilliseconds() - start_ms_;
223 rtp_file_writer_->WritePacket(&p);
224 }
225
Danil Chapovalovb57fe172019-12-11 09:38:44 +0100226 RtpPacket rtp_packet;
227 rtp_packet.Parse(packet);
228 if (!IsFlexfec(rtp_packet.PayloadType()) &&
229 (rtp_packet.Ssrc() == ssrc_to_analyze_ ||
230 rtp_packet.Ssrc() == rtx_ssrc_to_analyze_)) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200231 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
232 // (FlexFEC and media are sent on different SSRCs, which have different
233 // timestamps spaces.)
234 // Also ignore packets from wrong SSRC, but include retransmits.
235 rtc::CritScope lock(&crit_);
236 int64_t timestamp =
Danil Chapovalovb57fe172019-12-11 09:38:44 +0100237 wrap_handler_.Unwrap(rtp_packet.Timestamp() - rtp_timestamp_delta_);
Sebastian Jansson11c012a2019-03-29 14:17:26 +0100238 recv_times_[timestamp] = clock_->CurrentNtpInMilliseconds();
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200239 }
240
Niels Möller70082872018-08-07 11:03:12 +0200241 return receiver_->DeliverPacket(media_type, std::move(packet),
242 packet_time_us);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200243}
244
245void VideoAnalyzer::PreEncodeOnFrame(const VideoFrame& video_frame) {
246 rtc::CritScope lock(&crit_);
247 if (!first_encoded_timestamp_) {
248 while (frames_.front().timestamp() != video_frame.timestamp()) {
249 ++dropped_frames_before_first_encode_;
250 frames_.pop_front();
251 RTC_CHECK(!frames_.empty());
252 }
253 first_encoded_timestamp_ = video_frame.timestamp();
254 }
255}
256
Niels Möller88be9722018-10-10 10:58:52 +0200257void VideoAnalyzer::PostEncodeOnFrame(size_t stream_id, uint32_t timestamp) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200258 rtc::CritScope lock(&crit_);
Niels Möller88be9722018-10-10 10:58:52 +0200259 if (!first_sent_timestamp_ && stream_id == selected_stream_) {
260 first_sent_timestamp_ = timestamp;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200261 }
262}
263
264bool VideoAnalyzer::SendRtp(const uint8_t* packet,
265 size_t length,
266 const PacketOptions& options) {
Danil Chapovalovb57fe172019-12-11 09:38:44 +0100267 RtpPacket rtp_packet;
268 rtp_packet.Parse(packet, length);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200269
Sebastian Jansson11c012a2019-03-29 14:17:26 +0100270 int64_t current_time = clock_->CurrentNtpInMilliseconds();
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200271
272 bool result = transport_->SendRtp(packet, length, options);
273 {
274 rtc::CritScope lock(&crit_);
Danil Chapovalovb57fe172019-12-11 09:38:44 +0100275 if (rtp_timestamp_delta_ == 0 && rtp_packet.Ssrc() == ssrc_to_analyze_) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200276 RTC_CHECK(static_cast<bool>(first_sent_timestamp_));
Danil Chapovalovb57fe172019-12-11 09:38:44 +0100277 rtp_timestamp_delta_ = rtp_packet.Timestamp() - *first_sent_timestamp_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200278 }
279
Danil Chapovalovb57fe172019-12-11 09:38:44 +0100280 if (!IsFlexfec(rtp_packet.PayloadType()) &&
281 rtp_packet.Ssrc() == ssrc_to_analyze_) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200282 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
283 // (FlexFEC and media are sent on different SSRCs, which have different
284 // timestamps spaces.)
285 // Also ignore packets from wrong SSRC and retransmits.
286 int64_t timestamp =
Danil Chapovalovb57fe172019-12-11 09:38:44 +0100287 wrap_handler_.Unwrap(rtp_packet.Timestamp() - rtp_timestamp_delta_);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200288 send_times_[timestamp] = current_time;
289
Danil Chapovalovb57fe172019-12-11 09:38:44 +0100290 if (IsInSelectedSpatialAndTemporalLayer(rtp_packet)) {
291 encoded_frame_sizes_[timestamp] += rtp_packet.payload_size();
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200292 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200293 }
294 }
295 return result;
296}
297
298bool VideoAnalyzer::SendRtcp(const uint8_t* packet, size_t length) {
299 return transport_->SendRtcp(packet, length);
300}
301
302void VideoAnalyzer::OnFrame(const VideoFrame& video_frame) {
Sebastian Jansson11c012a2019-03-29 14:17:26 +0100303 int64_t render_time_ms = clock_->CurrentNtpInMilliseconds();
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200304
305 rtc::CritScope lock(&crit_);
306
307 StartExcludingCpuThreadTime();
308
309 int64_t send_timestamp =
310 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
311
312 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
313 if (!last_rendered_frame_) {
314 // No previous frame rendered, this one was dropped after sending but
315 // before rendering.
316 ++dropped_frames_before_rendering_;
317 } else {
318 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
319 render_time_ms);
320 }
321 frames_.pop_front();
322 RTC_DCHECK(!frames_.empty());
323 }
324
325 VideoFrame reference_frame = frames_.front();
326 frames_.pop_front();
327 int64_t reference_timestamp =
328 wrap_handler_.Unwrap(reference_frame.timestamp());
329 if (send_timestamp == reference_timestamp - 1) {
330 // TODO(ivica): Make this work for > 2 streams.
331 // Look at RTPSender::BuildRTPHeader.
332 ++send_timestamp;
333 }
334 ASSERT_EQ(reference_timestamp, send_timestamp);
335
336 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
337
338 last_rendered_frame_ = video_frame;
339
340 StopExcludingCpuThreadTime();
341}
342
343void VideoAnalyzer::Wait() {
344 // Frame comparisons can be very expensive. Wait for test to be done, but
345 // at time-out check if frames_processed is going up. If so, give it more
346 // time, otherwise fail. Hopefully this will reduce test flakiness.
347
Danil Chapovalov9cd53b42019-10-21 13:36:59 +0200348 RepeatingTaskHandle stats_polling_task = RepeatingTaskHandle::DelayedStart(
349 task_queue_, kSendStatsPollingInterval, [this] {
350 PollStats();
351 return kSendStatsPollingInterval;
352 });
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200353
354 int last_frames_processed = -1;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100355 int last_frames_captured = -1;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200356 int iteration = 0;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100357
358 while (!done_.Wait(kProbingIntervalMs)) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200359 int frames_processed;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100360 int frames_captured;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200361 {
362 rtc::CritScope crit(&comparison_lock_);
363 frames_processed = frames_processed_;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100364 frames_captured = captured_frames_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200365 }
366
367 // Print some output so test infrastructure won't think we've crashed.
368 const char* kKeepAliveMessages[3] = {
369 "Uh, I'm-I'm not quite dead, sir.",
370 "Uh, I-I think uh, I could pull through, sir.",
371 "Actually, I think I'm all right to come with you--"};
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100372 if (++iteration % kKeepAliveIntervalIterations == 0) {
373 printf("- %s\n", kKeepAliveMessages[iteration % 3]);
374 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200375
376 if (last_frames_processed == -1) {
377 last_frames_processed = frames_processed;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100378 last_frames_captured = frames_captured;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200379 continue;
380 }
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100381 if (frames_processed == last_frames_processed &&
382 last_frames_captured == frames_captured) {
383 if (frames_captured < frames_to_process_) {
384 EXPECT_GT(frames_processed, last_frames_processed)
385 << "Analyzer stalled while waiting for test to finish.";
386 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200387 done_.Set();
388 break;
389 }
390 last_frames_processed = frames_processed;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100391 last_frames_captured = frames_captured;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200392 }
393
394 if (iteration > 0)
395 printf("- Farewell, sweet Concorde!\n");
396
Danil Chapovalov9cd53b42019-10-21 13:36:59 +0200397 SendTask(RTC_FROM_HERE, task_queue_, [&] { stats_polling_task.Stop(); });
Artem Titovff7730d2019-04-02 13:46:53 +0200398
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100399 PrintResults();
400 if (graph_data_output_file_)
401 PrintSamplesToFile();
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200402}
403
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200404void VideoAnalyzer::StartMeasuringCpuProcessTime() {
405 rtc::CritScope lock(&cpu_measurement_lock_);
406 cpu_time_ -= rtc::GetProcessCpuTimeNanos();
407 wallclock_time_ -= rtc::SystemTimeNanos();
408}
409
410void VideoAnalyzer::StopMeasuringCpuProcessTime() {
411 rtc::CritScope lock(&cpu_measurement_lock_);
412 cpu_time_ += rtc::GetProcessCpuTimeNanos();
413 wallclock_time_ += rtc::SystemTimeNanos();
414}
415
416void VideoAnalyzer::StartExcludingCpuThreadTime() {
417 rtc::CritScope lock(&cpu_measurement_lock_);
418 cpu_time_ += rtc::GetThreadCpuTimeNanos();
419}
420
421void VideoAnalyzer::StopExcludingCpuThreadTime() {
422 rtc::CritScope lock(&cpu_measurement_lock_);
423 cpu_time_ -= rtc::GetThreadCpuTimeNanos();
424}
425
426double VideoAnalyzer::GetCpuUsagePercent() {
427 rtc::CritScope lock(&cpu_measurement_lock_);
428 return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
429}
430
431bool VideoAnalyzer::IsInSelectedSpatialAndTemporalLayer(
Danil Chapovalovb57fe172019-12-11 09:38:44 +0100432 const RtpPacket& rtp_packet) {
433 if (rtp_packet.PayloadType() == test::CallTest::kPayloadTypeVP8) {
434 auto parsed_payload = vp8_depacketizer_->Parse(rtp_packet.PayloadBuffer());
435 RTC_DCHECK(parsed_payload);
436 const auto& vp8_header = absl::get<RTPVideoHeaderVP8>(
437 parsed_payload->video_header.video_type_header);
438 int temporal_idx = vp8_header.temporalIdx;
439 return selected_tl_ < 0 || temporal_idx == kNoTemporalIdx ||
440 temporal_idx <= selected_tl_;
441 }
philipel29d88462018-08-08 14:26:00 +0200442
Danil Chapovalovb57fe172019-12-11 09:38:44 +0100443 if (rtp_packet.PayloadType() == test::CallTest::kPayloadTypeVP9) {
444 auto parsed_payload = vp9_depacketizer_->Parse(rtp_packet.PayloadBuffer());
445 RTC_DCHECK(parsed_payload);
446 const auto& vp9_header = absl::get<RTPVideoHeaderVP9>(
447 parsed_payload->video_header.video_type_header);
448 int temporal_idx = vp9_header.temporal_idx;
449 int spatial_idx = vp9_header.spatial_idx;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200450 return (selected_tl_ < 0 || temporal_idx == kNoTemporalIdx ||
451 temporal_idx <= selected_tl_) &&
452 (selected_sl_ < 0 || spatial_idx == kNoSpatialIdx ||
453 spatial_idx <= selected_sl_);
454 }
Danil Chapovalovb57fe172019-12-11 09:38:44 +0100455
456 return true;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200457}
458
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200459void VideoAnalyzer::PollStats() {
Artem Titovff7730d2019-04-02 13:46:53 +0200460 rtc::CritScope crit(&comparison_lock_);
Artem Titovff7730d2019-04-02 13:46:53 +0200461
462 Call::Stats call_stats = call_->GetStats();
463 send_bandwidth_bps_.AddSample(call_stats.send_bandwidth_bps);
464
465 VideoSendStream::Stats send_stats = send_stream_->GetStats();
466 // It's not certain that we yet have estimates for any of these stats.
467 // Check that they are positive before mixing them in.
468 if (send_stats.encode_frame_rate > 0)
469 encode_frame_rate_.AddSample(send_stats.encode_frame_rate);
470 if (send_stats.avg_encode_time_ms > 0)
471 encode_time_ms_.AddSample(send_stats.avg_encode_time_ms);
472 if (send_stats.encode_usage_percent > 0)
473 encode_usage_percent_.AddSample(send_stats.encode_usage_percent);
474 if (send_stats.media_bitrate_bps > 0)
475 media_bitrate_bps_.AddSample(send_stats.media_bitrate_bps);
476 size_t fec_bytes = 0;
477 for (const auto& kv : send_stats.substreams) {
478 fec_bytes += kv.second.rtp_stats.fec.payload_bytes +
479 kv.second.rtp_stats.fec.padding_bytes;
480 }
481 fec_bitrate_bps_.AddSample((fec_bytes - last_fec_bytes_) * 8);
482 last_fec_bytes_ = fec_bytes;
483
484 if (receive_stream_ != nullptr) {
485 VideoReceiveStream::Stats receive_stats = receive_stream_->GetStats();
Johannes Krona1b99b32019-07-30 15:08:16 +0200486 // |total_decode_time_ms| gives a good estimate of the mean decode time,
487 // |decode_ms| is used to keep track of the standard deviation.
488 if (receive_stats.frames_decoded > 0)
489 mean_decode_time_ms_ =
490 static_cast<double>(receive_stats.total_decode_time_ms) /
491 receive_stats.frames_decoded;
Artem Titovff7730d2019-04-02 13:46:53 +0200492 if (receive_stats.decode_ms > 0)
493 decode_time_ms_.AddSample(receive_stats.decode_ms);
494 if (receive_stats.max_decode_ms > 0)
495 decode_time_max_ms_.AddSample(receive_stats.max_decode_ms);
496 if (receive_stats.width > 0 && receive_stats.height > 0) {
497 pixels_.AddSample(receive_stats.width * receive_stats.height);
498 }
Elad Alon58e06572019-05-08 15:34:24 +0200499
500 // |frames_decoded| and |frames_rendered| are used because they are more
501 // accurate than |decode_frame_rate| and |render_frame_rate|.
502 // The latter two are calculated on a momentary basis.
503 const double total_frames_duration_sec_double =
504 static_cast<double>(receive_stats.total_frames_duration_ms) / 1000.0;
505 if (total_frames_duration_sec_double > 0) {
506 decode_frame_rate_ = static_cast<double>(receive_stats.frames_decoded) /
507 total_frames_duration_sec_double;
508 render_frame_rate_ = static_cast<double>(receive_stats.frames_rendered) /
509 total_frames_duration_sec_double;
510 }
511
512 // Freeze metrics.
Elad Alon8c513c72019-05-07 21:22:24 +0200513 freeze_count_ = receive_stats.freeze_count;
514 total_freezes_duration_ms_ = receive_stats.total_freezes_duration_ms;
515 total_frames_duration_ms_ = receive_stats.total_frames_duration_ms;
516 sum_squared_frame_durations_ = receive_stats.sum_squared_frame_durations;
Artem Titovff7730d2019-04-02 13:46:53 +0200517 }
518
519 if (audio_receive_stream_ != nullptr) {
520 AudioReceiveStream::Stats receive_stats = audio_receive_stream_->GetStats();
521 audio_expand_rate_.AddSample(receive_stats.expand_rate);
522 audio_accelerate_rate_.AddSample(receive_stats.accelerate_rate);
523 audio_jitter_buffer_ms_.AddSample(receive_stats.jitter_buffer_ms);
524 }
525
526 memory_usage_.AddSample(rtc::GetProcessResidentSizeBytes());
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200527}
528
Niels Möller4731f002019-05-03 09:34:24 +0200529void VideoAnalyzer::FrameComparisonThread(void* obj) {
530 VideoAnalyzer* analyzer = static_cast<VideoAnalyzer*>(obj);
531 while (analyzer->CompareFrames()) {
532 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200533}
534
535bool VideoAnalyzer::CompareFrames() {
536 if (AllFramesRecorded())
537 return false;
538
539 FrameComparison comparison;
540
541 if (!PopComparison(&comparison)) {
542 // Wait until new comparison task is available, or test is done.
543 // If done, wake up remaining threads waiting.
544 comparison_available_event_.Wait(1000);
545 if (AllFramesRecorded()) {
546 comparison_available_event_.Set();
547 return false;
548 }
549 return true; // Try again.
550 }
551
552 StartExcludingCpuThreadTime();
553
554 PerformFrameComparison(comparison);
555
556 StopExcludingCpuThreadTime();
557
558 if (FrameProcessed()) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200559 done_.Set();
560 comparison_available_event_.Set();
561 return false;
562 }
563
564 return true;
565}
566
567bool VideoAnalyzer::PopComparison(VideoAnalyzer::FrameComparison* comparison) {
568 rtc::CritScope crit(&comparison_lock_);
569 // If AllFramesRecorded() is true, it means we have already popped
570 // frames_to_process_ frames from comparisons_, so there is no more work
571 // for this thread to be done. frames_processed_ might still be lower if
572 // all comparisons are not done, but those frames are currently being
573 // worked on by other threads.
574 if (comparisons_.empty() || AllFramesRecorded())
575 return false;
576
577 *comparison = comparisons_.front();
578 comparisons_.pop_front();
579
580 FrameRecorded();
581 return true;
582}
583
584void VideoAnalyzer::FrameRecorded() {
585 rtc::CritScope crit(&comparison_lock_);
586 ++frames_recorded_;
587}
588
589bool VideoAnalyzer::AllFramesRecorded() {
590 rtc::CritScope crit(&comparison_lock_);
Niels Möller4731f002019-05-03 09:34:24 +0200591 RTC_DCHECK(frames_recorded_ <= frames_to_process_);
592 return frames_recorded_ == frames_to_process_ || quit_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200593}
594
595bool VideoAnalyzer::FrameProcessed() {
596 rtc::CritScope crit(&comparison_lock_);
597 ++frames_processed_;
598 assert(frames_processed_ <= frames_to_process_);
599 return frames_processed_ == frames_to_process_;
600}
601
602void VideoAnalyzer::PrintResults() {
Artem Titov82ce3842019-09-23 17:55:52 +0200603 using ::webrtc::test::ImproveDirection;
604
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200605 StopMeasuringCpuProcessTime();
Yves Gerey0c67c802019-08-01 17:45:54 +0200606 int dropped_frames_diff;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100607 {
608 rtc::CritScope crit(&crit_);
Yves Gerey0c67c802019-08-01 17:45:54 +0200609 dropped_frames_diff = dropped_frames_before_first_encode_ +
610 dropped_frames_before_rendering_ + frames_.size();
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100611 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200612 rtc::CritScope crit(&comparison_lock_);
Artem Titov82ce3842019-09-23 17:55:52 +0200613 PrintResult("psnr", psnr_, "dB", ImproveDirection::kBiggerIsBetter);
614 PrintResult("ssim", ssim_, "unitless", ImproveDirection::kBiggerIsBetter);
615 PrintResult("sender_time", sender_time_, "ms",
616 ImproveDirection::kSmallerIsBetter);
617 PrintResult("receiver_time", receiver_time_, "ms",
618 ImproveDirection::kSmallerIsBetter);
619 PrintResult("network_time", network_time_, "ms",
620 ImproveDirection::kSmallerIsBetter);
621 PrintResult("total_delay_incl_network", end_to_end_, "ms",
622 ImproveDirection::kSmallerIsBetter);
623 PrintResult("time_between_rendered_frames", rendered_delta_, "ms",
624 ImproveDirection::kSmallerIsBetter);
625 PrintResult("encode_frame_rate", encode_frame_rate_, "fps",
626 ImproveDirection::kBiggerIsBetter);
627 PrintResult("encode_time", encode_time_ms_, "ms",
628 ImproveDirection::kSmallerIsBetter);
629 PrintResult("media_bitrate", media_bitrate_bps_, "bps",
630 ImproveDirection::kNone);
631 PrintResult("fec_bitrate", fec_bitrate_bps_, "bps", ImproveDirection::kNone);
632 PrintResult("send_bandwidth", send_bandwidth_bps_, "bps",
633 ImproveDirection::kNone);
634 PrintResult("pixels_per_frame", pixels_, "count",
635 ImproveDirection::kBiggerIsBetter);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200636
Elad Alon58e06572019-05-08 15:34:24 +0200637 test::PrintResult("decode_frame_rate", "", test_label_.c_str(),
Artem Titov82ce3842019-09-23 17:55:52 +0200638 decode_frame_rate_, "fps", false,
639 ImproveDirection::kBiggerIsBetter);
Elad Alon58e06572019-05-08 15:34:24 +0200640 test::PrintResult("render_frame_rate", "", test_label_.c_str(),
Artem Titov82ce3842019-09-23 17:55:52 +0200641 render_frame_rate_, "fps", false,
642 ImproveDirection::kBiggerIsBetter);
Elad Alon58e06572019-05-08 15:34:24 +0200643
Elad Alon8c513c72019-05-07 21:22:24 +0200644 // Record the time from the last freeze until the last rendered frame to
645 // ensure we cover the full timespan of the session. Otherwise the metric
646 // would penalize an early freeze followed by no freezes until the end.
647 time_between_freezes_.AddSample(last_render_time_ - last_unfreeze_time_ms_);
648
649 // Freeze metrics.
Artem Titov82ce3842019-09-23 17:55:52 +0200650 PrintResult("time_between_freezes", time_between_freezes_, "ms",
651 ImproveDirection::kBiggerIsBetter);
Elad Alon8c513c72019-05-07 21:22:24 +0200652
653 const double freeze_count_double = static_cast<double>(freeze_count_);
654 const double total_freezes_duration_ms_double =
655 static_cast<double>(total_freezes_duration_ms_);
656 const double total_frames_duration_ms_double =
657 static_cast<double>(total_frames_duration_ms_);
658
659 if (total_frames_duration_ms_double > 0) {
660 test::PrintResult(
661 "freeze_duration_ratio", "", test_label_.c_str(),
Artem Titovea9798c2019-07-31 14:27:42 +0200662 total_freezes_duration_ms_double / total_frames_duration_ms_double,
Artem Titov82ce3842019-09-23 17:55:52 +0200663 "unitless", false, ImproveDirection::kSmallerIsBetter);
Elad Alon8c513c72019-05-07 21:22:24 +0200664 RTC_DCHECK_LE(total_freezes_duration_ms_double,
665 total_frames_duration_ms_double);
666
667 constexpr double ms_per_minute = 60 * 1000;
668 const double total_frames_duration_min =
669 total_frames_duration_ms_double / ms_per_minute;
670 if (total_frames_duration_min > 0) {
671 test::PrintResult("freeze_count_per_minute", "", test_label_.c_str(),
672 freeze_count_double / total_frames_duration_min,
Artem Titov82ce3842019-09-23 17:55:52 +0200673 "unitless", false, ImproveDirection::kSmallerIsBetter);
Elad Alon8c513c72019-05-07 21:22:24 +0200674 }
675 }
676
Elad Alon133f7e72019-05-08 09:51:56 +0200677 test::PrintResult("freeze_duration_average", "", test_label_.c_str(),
Elad Alon8c513c72019-05-07 21:22:24 +0200678 freeze_count_double > 0
679 ? total_freezes_duration_ms_double / freeze_count_double
680 : 0,
Artem Titov82ce3842019-09-23 17:55:52 +0200681 "ms", false, ImproveDirection::kSmallerIsBetter);
Elad Alon8c513c72019-05-07 21:22:24 +0200682
683 if (1000 * sum_squared_frame_durations_ > 0) {
684 test::PrintResult(
685 "harmonic_frame_rate", "", test_label_.c_str(),
686 total_frames_duration_ms_double / (1000 * sum_squared_frame_durations_),
Artem Titov82ce3842019-09-23 17:55:52 +0200687 "fps", false, ImproveDirection::kBiggerIsBetter);
Elad Alon8c513c72019-05-07 21:22:24 +0200688 }
689
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200690 if (worst_frame_) {
691 test::PrintResult("min_psnr", "", test_label_.c_str(), worst_frame_->psnr,
Artem Titov82ce3842019-09-23 17:55:52 +0200692 "dB", false, ImproveDirection::kBiggerIsBetter);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200693 }
694
695 if (receive_stream_ != nullptr) {
Johannes Krona1b99b32019-07-30 15:08:16 +0200696 PrintResultWithExternalMean("decode_time", mean_decode_time_ms_,
Artem Titov82ce3842019-09-23 17:55:52 +0200697 decode_time_ms_, "ms",
698 ImproveDirection::kSmallerIsBetter);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200699 }
Yves Gerey0c67c802019-08-01 17:45:54 +0200700 dropped_frames_ += dropped_frames_diff;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200701 test::PrintResult("dropped_frames", "", test_label_.c_str(), dropped_frames_,
Artem Titov82ce3842019-09-23 17:55:52 +0200702 "count", false, ImproveDirection::kSmallerIsBetter);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200703 test::PrintResult("cpu_usage", "", test_label_.c_str(), GetCpuUsagePercent(),
Artem Titov82ce3842019-09-23 17:55:52 +0200704 "%", false, ImproveDirection::kSmallerIsBetter);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200705
706#if defined(WEBRTC_WIN)
707 // On Linux and Mac in Resident Set some unused pages may be counted.
708 // Therefore this metric will depend on order in which tests are run and
709 // will be flaky.
Artem Titov82ce3842019-09-23 17:55:52 +0200710 PrintResult("memory_usage", memory_usage_, "sizeInBytes",
711 ImproveDirection::kSmallerIsBetter);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200712#endif
713
714 // Saving only the worst frame for manual analysis. Intention here is to
715 // only detect video corruptions and not to track picture quality. Thus,
716 // jpeg is used here.
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200717 if (absl::GetFlag(FLAGS_save_worst_frame) && worst_frame_) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200718 std::string output_dir;
719 test::GetTestArtifactsDir(&output_dir);
720 std::string output_path =
Niels Möller7b3c76b2018-11-07 09:54:28 +0100721 test::JoinFilename(output_dir, test_label_ + ".jpg");
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200722 RTC_LOG(LS_INFO) << "Saving worst frame to " << output_path;
723 test::JpegFrameWriter frame_writer(output_path);
724 RTC_CHECK(
725 frame_writer.WriteFrame(worst_frame_->frame, 100 /*best quality*/));
726 }
727
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200728 if (audio_receive_stream_ != nullptr) {
Artem Titov82ce3842019-09-23 17:55:52 +0200729 PrintResult("audio_expand_rate", audio_expand_rate_, "unitless",
730 ImproveDirection::kSmallerIsBetter);
731 PrintResult("audio_accelerate_rate", audio_accelerate_rate_, "unitless",
732 ImproveDirection::kSmallerIsBetter);
733 PrintResult("audio_jitter_buffer", audio_jitter_buffer_ms_, "ms",
734 ImproveDirection::kNone);
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200735 }
736
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200737 // Disable quality check for quick test, as quality checks may fail
738 // because too few samples were collected.
739 if (!is_quick_test_enabled_) {
Yves Gerey79e9f4b2019-04-13 18:59:53 +0200740 EXPECT_GT(*psnr_.GetMean(), avg_psnr_threshold_);
741 EXPECT_GT(*ssim_.GetMean(), avg_ssim_threshold_);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200742 }
743}
744
745void VideoAnalyzer::PerformFrameComparison(
746 const VideoAnalyzer::FrameComparison& comparison) {
747 // Perform expensive psnr and ssim calculations while not holding lock.
748 double psnr = -1.0;
749 double ssim = -1.0;
750 if (comparison.reference && !comparison.dropped) {
751 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
752 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
753 }
754
755 rtc::CritScope crit(&comparison_lock_);
756
757 if (psnr >= 0.0 && (!worst_frame_ || worst_frame_->psnr > psnr)) {
758 worst_frame_.emplace(FrameWithPsnr{psnr, *comparison.render});
759 }
760
761 if (graph_data_output_file_) {
762 samples_.push_back(Sample(comparison.dropped, comparison.input_time_ms,
763 comparison.send_time_ms, comparison.recv_time_ms,
764 comparison.render_time_ms,
765 comparison.encoded_frame_size, psnr, ssim));
766 }
767 if (psnr >= 0.0)
768 psnr_.AddSample(psnr);
769 if (ssim >= 0.0)
770 ssim_.AddSample(ssim);
771
772 if (comparison.dropped) {
773 ++dropped_frames_;
774 return;
775 }
776 if (last_unfreeze_time_ms_ == 0)
777 last_unfreeze_time_ms_ = comparison.render_time_ms;
778 if (last_render_time_ != 0) {
779 const int64_t render_delta_ms =
780 comparison.render_time_ms - last_render_time_;
781 rendered_delta_.AddSample(render_delta_ms);
782 if (last_render_delta_ms_ != 0 &&
783 render_delta_ms - last_render_delta_ms_ > 150) {
784 time_between_freezes_.AddSample(last_render_time_ -
785 last_unfreeze_time_ms_);
786 last_unfreeze_time_ms_ = comparison.render_time_ms;
787 }
788 last_render_delta_ms_ = render_delta_ms;
789 }
790 last_render_time_ = comparison.render_time_ms;
791
792 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
793 if (comparison.recv_time_ms > 0) {
794 // If recv_time_ms == 0, this frame consisted of a packets which were all
795 // lost in the transport. Since we were able to render the frame, however,
796 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
797 // happens internally in Call, and we can therefore here not know which
798 // FEC packets that protected the lost media packets. Consequently, we
799 // were not able to record a meaningful recv_time_ms. We therefore skip
800 // this sample.
801 //
802 // The reasoning above does not hold for ULPFEC and RTX, as for those
803 // strategies the timestamp of the received packets is set to the
804 // timestamp of the protected/retransmitted media packet. I.e., then
805 // recv_time_ms != 0, even though the media packets were lost.
806 receiver_time_.AddSample(comparison.render_time_ms -
807 comparison.recv_time_ms);
808 network_time_.AddSample(comparison.recv_time_ms - comparison.send_time_ms);
809 }
810 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
811 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
812}
813
Artem Titov82ce3842019-09-23 17:55:52 +0200814void VideoAnalyzer::PrintResult(
815 const char* result_type,
816 Statistics stats,
817 const char* unit,
818 webrtc::test::ImproveDirection improve_direction) {
Yves Gerey79e9f4b2019-04-13 18:59:53 +0200819 test::PrintResultMeanAndError(
820 result_type, "", test_label_.c_str(), stats.GetMean().value_or(0),
Artem Titov82ce3842019-09-23 17:55:52 +0200821 stats.GetStandardDeviation().value_or(0), unit, false, improve_direction);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200822}
823
Artem Titov82ce3842019-09-23 17:55:52 +0200824void VideoAnalyzer::PrintResultWithExternalMean(
825 const char* result_type,
826 double mean,
827 Statistics stats,
828 const char* unit,
829 webrtc::test::ImproveDirection improve_direction) {
Johannes Krona1b99b32019-07-30 15:08:16 +0200830 // If the true mean is different than the sample mean, the sample variance is
831 // too low. The sample variance given a known mean is obtained by adding the
832 // squared error between the true mean and the sample mean.
833 double compensated_variance =
834 stats.Size() > 0
835 ? *stats.GetVariance() + pow(mean - *stats.GetMean(), 2.0)
836 : 0.0;
837 test::PrintResultMeanAndError(result_type, "", test_label_.c_str(), mean,
Artem Titov82ce3842019-09-23 17:55:52 +0200838 std::sqrt(compensated_variance), unit, false,
839 improve_direction);
Johannes Krona1b99b32019-07-30 15:08:16 +0200840}
841
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200842void VideoAnalyzer::PrintSamplesToFile() {
843 FILE* out = graph_data_output_file_;
844 rtc::CritScope crit(&comparison_lock_);
Steve Antonbd631a02019-03-28 10:51:27 -0700845 absl::c_sort(samples_, [](const Sample& A, const Sample& B) -> bool {
846 return A.input_time_ms < B.input_time_ms;
847 });
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200848
849 fprintf(out, "%s\n", graph_title_.c_str());
Oleh Prypinb1686782019-08-02 09:36:47 +0200850 fprintf(out, "%" RTC_PRIuS "\n", samples_.size());
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200851 fprintf(out,
852 "dropped "
853 "input_time_ms "
854 "send_time_ms "
855 "recv_time_ms "
856 "render_time_ms "
857 "encoded_frame_size "
858 "psnr "
859 "ssim "
860 "encode_time_ms\n");
861 for (const Sample& sample : samples_) {
862 fprintf(out,
Oleh Prypinb1686782019-08-02 09:36:47 +0200863 "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" RTC_PRIuS
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200864 " %lf %lf\n",
865 sample.dropped, sample.input_time_ms, sample.send_time_ms,
866 sample.recv_time_ms, sample.render_time_ms,
867 sample.encoded_frame_size, sample.psnr, sample.ssim);
868 }
869}
870
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200871void VideoAnalyzer::AddCapturedFrameForComparison(
872 const VideoFrame& video_frame) {
Yves Gerey0c67c802019-08-01 17:45:54 +0200873 bool must_capture = false;
874 {
875 rtc::CritScope lock(&comparison_lock_);
876 must_capture = captured_frames_ < frames_to_process_;
877 if (must_capture) {
878 ++captured_frames_;
879 }
880 }
881 if (must_capture) {
882 rtc::CritScope lock(&crit_);
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100883 frames_.push_back(video_frame);
884 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200885}
886
887void VideoAnalyzer::AddFrameComparison(const VideoFrame& reference,
888 const VideoFrame& render,
889 bool dropped,
890 int64_t render_time_ms) {
891 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
892 int64_t send_time_ms = send_times_[reference_timestamp];
893 send_times_.erase(reference_timestamp);
894 int64_t recv_time_ms = recv_times_[reference_timestamp];
895 recv_times_.erase(reference_timestamp);
896
897 // TODO(ivica): Make this work for > 2 streams.
898 auto it = encoded_frame_sizes_.find(reference_timestamp);
899 if (it == encoded_frame_sizes_.end())
900 it = encoded_frame_sizes_.find(reference_timestamp - 1);
901 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
902 if (it != encoded_frame_sizes_.end())
903 encoded_frame_sizes_.erase(it);
904
905 rtc::CritScope crit(&comparison_lock_);
906 if (comparisons_.size() < kMaxComparisons) {
907 comparisons_.push_back(FrameComparison(
908 reference, render, dropped, reference.ntp_time_ms(), send_time_ms,
909 recv_time_ms, render_time_ms, encoded_size));
910 } else {
911 comparisons_.push_back(FrameComparison(dropped, reference.ntp_time_ms(),
912 send_time_ms, recv_time_ms,
913 render_time_ms, encoded_size));
914 }
915 comparison_available_event_.Set();
916}
917
918VideoAnalyzer::FrameComparison::FrameComparison()
919 : dropped(false),
920 input_time_ms(0),
921 send_time_ms(0),
922 recv_time_ms(0),
923 render_time_ms(0),
924 encoded_frame_size(0) {}
925
926VideoAnalyzer::FrameComparison::FrameComparison(const VideoFrame& reference,
927 const VideoFrame& render,
928 bool dropped,
929 int64_t input_time_ms,
930 int64_t send_time_ms,
931 int64_t recv_time_ms,
932 int64_t render_time_ms,
933 size_t encoded_frame_size)
934 : reference(reference),
935 render(render),
936 dropped(dropped),
937 input_time_ms(input_time_ms),
938 send_time_ms(send_time_ms),
939 recv_time_ms(recv_time_ms),
940 render_time_ms(render_time_ms),
941 encoded_frame_size(encoded_frame_size) {}
942
943VideoAnalyzer::FrameComparison::FrameComparison(bool dropped,
944 int64_t input_time_ms,
945 int64_t send_time_ms,
946 int64_t recv_time_ms,
947 int64_t render_time_ms,
948 size_t encoded_frame_size)
949 : dropped(dropped),
950 input_time_ms(input_time_ms),
951 send_time_ms(send_time_ms),
952 recv_time_ms(recv_time_ms),
953 render_time_ms(render_time_ms),
954 encoded_frame_size(encoded_frame_size) {}
955
956VideoAnalyzer::Sample::Sample(int dropped,
957 int64_t input_time_ms,
958 int64_t send_time_ms,
959 int64_t recv_time_ms,
960 int64_t render_time_ms,
961 size_t encoded_frame_size,
962 double psnr,
963 double ssim)
964 : dropped(dropped),
965 input_time_ms(input_time_ms),
966 send_time_ms(send_time_ms),
967 recv_time_ms(recv_time_ms),
968 render_time_ms(render_time_ms),
969 encoded_frame_size(encoded_frame_size),
970 psnr(psnr),
971 ssim(ssim) {}
972
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200973VideoAnalyzer::CapturedFrameForwarder::CapturedFrameForwarder(
974 VideoAnalyzer* analyzer,
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100975 Clock* clock,
Ilya Nikolaevskiy85fc3252019-02-11 10:41:50 +0100976 int frames_to_process)
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200977 : analyzer_(analyzer),
978 send_stream_input_(nullptr),
Niels Möller1c931c42018-12-18 16:08:11 +0100979 video_source_(nullptr),
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100980 clock_(clock),
981 captured_frames_(0),
Ilya Nikolaevskiy85fc3252019-02-11 10:41:50 +0100982 frames_to_process_(frames_to_process) {}
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200983
984void VideoAnalyzer::CapturedFrameForwarder::SetSource(
Niels Möller1c931c42018-12-18 16:08:11 +0100985 VideoSourceInterface<VideoFrame>* video_source) {
986 video_source_ = video_source;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200987}
988
989void VideoAnalyzer::CapturedFrameForwarder::OnFrame(
990 const VideoFrame& video_frame) {
991 VideoFrame copy = video_frame;
992 // Frames from the capturer does not have a rtp timestamp.
993 // Create one so it can be used for comparison.
994 RTC_DCHECK_EQ(0, video_frame.timestamp());
995 if (video_frame.ntp_time_ms() == 0)
996 copy.set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
997 copy.set_timestamp(copy.ntp_time_ms() * 90);
998 analyzer_->AddCapturedFrameForComparison(copy);
999 rtc::CritScope lock(&crit_);
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +01001000 ++captured_frames_;
Ilya Nikolaevskiy85fc3252019-02-11 10:41:50 +01001001 if (send_stream_input_ && captured_frames_ <= frames_to_process_)
Sebastian Janssond4c5d632018-07-10 12:57:37 +02001002 send_stream_input_->OnFrame(copy);
1003}
1004
1005void VideoAnalyzer::CapturedFrameForwarder::AddOrUpdateSink(
1006 rtc::VideoSinkInterface<VideoFrame>* sink,
1007 const rtc::VideoSinkWants& wants) {
1008 {
1009 rtc::CritScope lock(&crit_);
1010 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
1011 send_stream_input_ = sink;
1012 }
Niels Möller1c931c42018-12-18 16:08:11 +01001013 if (video_source_) {
1014 video_source_->AddOrUpdateSink(this, wants);
Sebastian Janssond4c5d632018-07-10 12:57:37 +02001015 }
1016}
1017
1018void VideoAnalyzer::CapturedFrameForwarder::RemoveSink(
1019 rtc::VideoSinkInterface<VideoFrame>* sink) {
1020 rtc::CritScope lock(&crit_);
1021 RTC_DCHECK(sink == send_stream_input_);
1022 send_stream_input_ = nullptr;
Sebastian Janssond4c5d632018-07-10 12:57:37 +02001023}
1024
1025} // namespace webrtc