blob: 110cb063b5c60ac4ea97332639801f8e040f8168 [file] [log] [blame]
ivica5d6a06c2015-09-17 05:30:24 -07001/*
2 * Copyright (c) 2015 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 <stdio.h>
11
12#include <algorithm>
13#include <deque>
14#include <map>
15#include <vector>
16
17#include "testing/gtest/include/gtest/gtest.h"
18
19#include "webrtc/base/checks.h"
20#include "webrtc/base/format_macros.h"
21#include "webrtc/base/scoped_ptr.h"
22#include "webrtc/call.h"
23#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
24#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
25#include "webrtc/system_wrappers/interface/cpu_info.h"
26#include "webrtc/test/layer_filtering_transport.h"
27#include "webrtc/test/run_loop.h"
28#include "webrtc/test/statistics.h"
29#include "webrtc/test/testsupport/fileutils.h"
30#include "webrtc/test/video_renderer.h"
31#include "webrtc/video/video_quality_test.h"
32
33namespace webrtc {
34
sprangef165ee2015-09-22 05:10:52 -070035static const int kTransportSeqExtensionId =
36 VideoQualityTest::kAbsSendTimeExtensionId + 1;
ivica5d6a06c2015-09-17 05:30:24 -070037static const int kSendStatsPollingIntervalMs = 1000;
38static const int kPayloadTypeVP8 = 123;
39static const int kPayloadTypeVP9 = 124;
40
41class VideoAnalyzer : public PacketReceiver,
42 public newapi::Transport,
43 public VideoRenderer,
44 public VideoCaptureInput,
45 public EncodedFrameObserver {
46 public:
47 VideoAnalyzer(VideoCaptureInput* input,
48 Transport* transport,
49 const std::string& test_label,
50 double avg_psnr_threshold,
51 double avg_ssim_threshold,
52 int duration_frames,
53 FILE* graph_data_output_file)
54 : input_(input),
55 transport_(transport),
56 receiver_(nullptr),
57 send_stream_(nullptr),
58 test_label_(test_label),
59 graph_data_output_file_(graph_data_output_file),
60 frames_to_process_(duration_frames),
61 frames_recorded_(0),
62 frames_processed_(0),
63 dropped_frames_(0),
64 last_render_time_(0),
65 rtp_timestamp_delta_(0),
66 avg_psnr_threshold_(avg_psnr_threshold),
67 avg_ssim_threshold_(avg_ssim_threshold),
68 comparison_available_event_(EventWrapper::Create()),
69 done_(EventWrapper::Create()) {
70 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
71
72 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
73 // so that we don't accidentally starve "real" worker threads (codec etc).
74 // Also, don't allocate more than kMaxComparisonThreads, even if there are
75 // spare cores.
76
77 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
78 RTC_DCHECK_GE(num_cores, 1u);
79 static const uint32_t kMinCoresLeft = 4;
80 static const uint32_t kMaxComparisonThreads = 8;
81
82 if (num_cores <= kMinCoresLeft) {
83 num_cores = 1;
84 } else {
85 num_cores -= kMinCoresLeft;
86 num_cores = std::min(num_cores, kMaxComparisonThreads);
87 }
88
89 for (uint32_t i = 0; i < num_cores; ++i) {
90 rtc::scoped_ptr<ThreadWrapper> thread =
91 ThreadWrapper::CreateThread(&FrameComparisonThread, this, "Analyzer");
92 EXPECT_TRUE(thread->Start());
93 comparison_thread_pool_.push_back(thread.release());
94 }
95
96 stats_polling_thread_ =
97 ThreadWrapper::CreateThread(&PollStatsThread, this, "StatsPoller");
98 EXPECT_TRUE(stats_polling_thread_->Start());
99 }
100
101 ~VideoAnalyzer() {
102 for (ThreadWrapper* thread : comparison_thread_pool_) {
103 EXPECT_TRUE(thread->Stop());
104 delete thread;
105 }
106 }
107
108 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
109
110 DeliveryStatus DeliverPacket(MediaType media_type,
111 const uint8_t* packet,
112 size_t length,
113 const PacketTime& packet_time) override {
114 rtc::scoped_ptr<RtpHeaderParser> parser(RtpHeaderParser::Create());
115 RTPHeader header;
116 parser->Parse(packet, length, &header);
117 {
118 rtc::CritScope lock(&crit_);
119 recv_times_[header.timestamp - rtp_timestamp_delta_] =
120 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
121 }
122
123 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
124 }
125
126 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
127 VideoFrame copy = video_frame;
128 copy.set_timestamp(copy.ntp_time_ms() * 90);
129
130 {
131 rtc::CritScope lock(&crit_);
132 if (first_send_frame_.IsZeroSize() && rtp_timestamp_delta_ == 0)
133 first_send_frame_ = copy;
134
135 frames_.push_back(copy);
136 }
137
138 input_->IncomingCapturedFrame(video_frame);
139 }
140
141 bool SendRtp(const uint8_t* packet, size_t length) override {
142 rtc::scoped_ptr<RtpHeaderParser> parser(RtpHeaderParser::Create());
143 RTPHeader header;
144 parser->Parse(packet, length, &header);
145
146 {
147 rtc::CritScope lock(&crit_);
148 if (rtp_timestamp_delta_ == 0) {
149 rtp_timestamp_delta_ = header.timestamp - first_send_frame_.timestamp();
150 first_send_frame_.Reset();
151 }
152 uint32_t timestamp = header.timestamp - rtp_timestamp_delta_;
153 send_times_[timestamp] =
154 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
155 encoded_frame_sizes_[timestamp] +=
156 length - (header.headerLength + header.paddingLength);
157 }
158
159 return transport_->SendRtp(packet, length);
160 }
161
162 bool SendRtcp(const uint8_t* packet, size_t length) override {
163 return transport_->SendRtcp(packet, length);
164 }
165
166 void EncodedFrameCallback(const EncodedFrame& frame) override {
167 rtc::CritScope lock(&comparison_lock_);
168 if (frames_recorded_ < frames_to_process_)
169 encoded_frame_size_.AddSample(frame.length_);
170 }
171
172 void RenderFrame(const VideoFrame& video_frame,
173 int time_to_render_ms) override {
174 int64_t render_time_ms =
175 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
176 uint32_t send_timestamp = video_frame.timestamp() - rtp_timestamp_delta_;
177
178 rtc::CritScope lock(&crit_);
179
180 while (frames_.front().timestamp() < send_timestamp) {
181 AddFrameComparison(frames_.front(), last_rendered_frame_, true,
182 render_time_ms);
183 frames_.pop_front();
184 }
185
186 VideoFrame reference_frame = frames_.front();
187 frames_.pop_front();
188 assert(!reference_frame.IsZeroSize());
189 EXPECT_EQ(reference_frame.timestamp(), send_timestamp);
190 assert(reference_frame.timestamp() == send_timestamp);
191
192 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
193
194 last_rendered_frame_ = video_frame;
195 }
196
197 bool IsTextureSupported() const override { return false; }
198
199 void Wait() {
200 // Frame comparisons can be very expensive. Wait for test to be done, but
201 // at time-out check if frames_processed is going up. If so, give it more
202 // time, otherwise fail. Hopefully this will reduce test flakiness.
203
204 int last_frames_processed = -1;
205 EventTypeWrapper eventType;
206 int iteration = 0;
207 while ((eventType = done_->Wait(VideoQualityTest::kDefaultTimeoutMs)) !=
208 kEventSignaled) {
209 int frames_processed;
210 {
211 rtc::CritScope crit(&comparison_lock_);
212 frames_processed = frames_processed_;
213 }
214
215 // Print some output so test infrastructure won't think we've crashed.
216 const char* kKeepAliveMessages[3] = {
217 "Uh, I'm-I'm not quite dead, sir.",
218 "Uh, I-I think uh, I could pull through, sir.",
219 "Actually, I think I'm all right to come with you--"};
220 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
221
222 if (last_frames_processed == -1) {
223 last_frames_processed = frames_processed;
224 continue;
225 }
226 ASSERT_GT(frames_processed, last_frames_processed)
227 << "Analyzer stalled while waiting for test to finish.";
228 last_frames_processed = frames_processed;
229 }
230
231 if (iteration > 0)
232 printf("- Farewell, sweet Concorde!\n");
233
234 // Signal stats polling thread if that is still waiting and stop it now,
235 // since it uses the send_stream_ reference that might be reclaimed after
236 // returning from this method.
237 done_->Set();
238 EXPECT_TRUE(stats_polling_thread_->Stop());
239 }
240
241 VideoCaptureInput* input_;
242 Transport* transport_;
243 PacketReceiver* receiver_;
244 VideoSendStream* send_stream_;
245
246 private:
247 struct FrameComparison {
248 FrameComparison()
249 : dropped(false),
250 send_time_ms(0),
251 recv_time_ms(0),
252 render_time_ms(0),
253 encoded_frame_size(0) {}
254
255 FrameComparison(const VideoFrame& reference,
256 const VideoFrame& render,
257 bool dropped,
258 int64_t send_time_ms,
259 int64_t recv_time_ms,
260 int64_t render_time_ms,
261 size_t encoded_frame_size)
262 : reference(reference),
263 render(render),
264 dropped(dropped),
265 send_time_ms(send_time_ms),
266 recv_time_ms(recv_time_ms),
267 render_time_ms(render_time_ms),
268 encoded_frame_size(encoded_frame_size) {}
269
270 VideoFrame reference;
271 VideoFrame render;
272 bool dropped;
273 int64_t send_time_ms;
274 int64_t recv_time_ms;
275 int64_t render_time_ms;
276 size_t encoded_frame_size;
277 };
278
279 struct Sample {
280 Sample(double dropped,
281 double input_time_ms,
282 double send_time_ms,
283 double recv_time_ms,
284 double encoded_frame_size,
285 double psnr,
286 double ssim,
287 double render_time_ms)
288 : dropped(dropped),
289 input_time_ms(input_time_ms),
290 send_time_ms(send_time_ms),
291 recv_time_ms(recv_time_ms),
292 encoded_frame_size(encoded_frame_size),
293 psnr(psnr),
294 ssim(ssim),
295 render_time_ms(render_time_ms) {}
296
297 double dropped;
298 double input_time_ms;
299 double send_time_ms;
300 double recv_time_ms;
301 double encoded_frame_size;
302 double psnr;
303 double ssim;
304 double render_time_ms;
305 };
306
307 void AddFrameComparison(const VideoFrame& reference,
308 const VideoFrame& render,
309 bool dropped,
310 int64_t render_time_ms)
311 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
312 int64_t send_time_ms = send_times_[reference.timestamp()];
313 send_times_.erase(reference.timestamp());
314 int64_t recv_time_ms = recv_times_[reference.timestamp()];
315 recv_times_.erase(reference.timestamp());
316
317 size_t encoded_size = encoded_frame_sizes_[reference.timestamp()];
318 encoded_frame_sizes_.erase(reference.timestamp());
319
320 VideoFrame reference_copy;
321 VideoFrame render_copy;
322 reference_copy.CopyFrame(reference);
323 render_copy.CopyFrame(render);
324
325 rtc::CritScope crit(&comparison_lock_);
326 comparisons_.push_back(FrameComparison(reference_copy, render_copy, dropped,
327 send_time_ms, recv_time_ms,
328 render_time_ms, encoded_size));
329 comparison_available_event_->Set();
330 }
331
332 static bool PollStatsThread(void* obj) {
333 return static_cast<VideoAnalyzer*>(obj)->PollStats();
334 }
335
336 bool PollStats() {
337 switch (done_->Wait(kSendStatsPollingIntervalMs)) {
338 case kEventSignaled:
339 case kEventError:
340 done_->Set(); // Make sure main thread is also signaled.
341 return false;
342 case kEventTimeout:
343 break;
344 default:
345 RTC_NOTREACHED();
346 }
347
348 VideoSendStream::Stats stats = send_stream_->GetStats();
349
350 rtc::CritScope crit(&comparison_lock_);
351 encode_frame_rate_.AddSample(stats.encode_frame_rate);
352 encode_time_ms.AddSample(stats.avg_encode_time_ms);
353 encode_usage_percent.AddSample(stats.encode_usage_percent);
354 media_bitrate_bps.AddSample(stats.media_bitrate_bps);
355
356 return true;
357 }
358
359 static bool FrameComparisonThread(void* obj) {
360 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
361 }
362
363 bool CompareFrames() {
364 if (AllFramesRecorded())
365 return false;
366
367 VideoFrame reference;
368 VideoFrame render;
369 FrameComparison comparison;
370
371 if (!PopComparison(&comparison)) {
372 // Wait until new comparison task is available, or test is done.
373 // If done, wake up remaining threads waiting.
374 comparison_available_event_->Wait(1000);
375 if (AllFramesRecorded()) {
376 comparison_available_event_->Set();
377 return false;
378 }
379 return true; // Try again.
380 }
381
382 PerformFrameComparison(comparison);
383
384 if (FrameProcessed()) {
385 PrintResults();
386 if (graph_data_output_file_)
387 PrintSamplesToFile();
388 done_->Set();
389 comparison_available_event_->Set();
390 return false;
391 }
392
393 return true;
394 }
395
396 bool PopComparison(FrameComparison* comparison) {
397 rtc::CritScope crit(&comparison_lock_);
398 // If AllFramesRecorded() is true, it means we have already popped
399 // frames_to_process_ frames from comparisons_, so there is no more work
400 // for this thread to be done. frames_processed_ might still be lower if
401 // all comparisons are not done, but those frames are currently being
402 // worked on by other threads.
403 if (comparisons_.empty() || AllFramesRecorded())
404 return false;
405
406 *comparison = comparisons_.front();
407 comparisons_.pop_front();
408
409 FrameRecorded();
410 return true;
411 }
412
413 // Increment counter for number of frames received for comparison.
414 void FrameRecorded() {
415 rtc::CritScope crit(&comparison_lock_);
416 ++frames_recorded_;
417 }
418
419 // Returns true if all frames to be compared have been taken from the queue.
420 bool AllFramesRecorded() {
421 rtc::CritScope crit(&comparison_lock_);
422 assert(frames_recorded_ <= frames_to_process_);
423 return frames_recorded_ == frames_to_process_;
424 }
425
426 // Increase count of number of frames processed. Returns true if this was the
427 // last frame to be processed.
428 bool FrameProcessed() {
429 rtc::CritScope crit(&comparison_lock_);
430 ++frames_processed_;
431 assert(frames_processed_ <= frames_to_process_);
432 return frames_processed_ == frames_to_process_;
433 }
434
435 void PrintResults() {
436 rtc::CritScope crit(&comparison_lock_);
437 PrintResult("psnr", psnr_, " dB");
438 PrintResult("ssim", ssim_, "");
439 PrintResult("sender_time", sender_time_, " ms");
440 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
441 dropped_frames_);
442 PrintResult("receiver_time", receiver_time_, " ms");
443 PrintResult("total_delay_incl_network", end_to_end_, " ms");
444 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
445 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes");
446 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
447 PrintResult("encode_time", encode_time_ms, " ms");
448 PrintResult("encode_usage_percent", encode_usage_percent, " percent");
449 PrintResult("media_bitrate", media_bitrate_bps, " bps");
450
451 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
452 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
453 }
454
455 void PerformFrameComparison(const FrameComparison& comparison) {
456 // Perform expensive psnr and ssim calculations while not holding lock.
457 double psnr = I420PSNR(&comparison.reference, &comparison.render);
458 double ssim = I420SSIM(&comparison.reference, &comparison.render);
459
460 int64_t input_time_ms = comparison.reference.ntp_time_ms();
461
462 rtc::CritScope crit(&comparison_lock_);
463 if (graph_data_output_file_) {
464 samples_.push_back(
465 Sample(comparison.dropped, input_time_ms, comparison.send_time_ms,
466 comparison.recv_time_ms, comparison.encoded_frame_size, psnr,
467 ssim, comparison.render_time_ms));
468 }
469 psnr_.AddSample(psnr);
470 ssim_.AddSample(ssim);
471
472 if (comparison.dropped) {
473 ++dropped_frames_;
474 return;
475 }
476 if (last_render_time_ != 0)
477 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
478 last_render_time_ = comparison.render_time_ms;
479
480 sender_time_.AddSample(comparison.send_time_ms - input_time_ms);
481 receiver_time_.AddSample(comparison.render_time_ms -
482 comparison.recv_time_ms);
483 end_to_end_.AddSample(comparison.render_time_ms - input_time_ms);
484 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
485 }
486
487 void PrintResult(const char* result_type,
488 test::Statistics stats,
489 const char* unit) {
490 printf("RESULT %s: %s = {%f, %f}%s\n",
491 result_type,
492 test_label_.c_str(),
493 stats.Mean(),
494 stats.StandardDeviation(),
495 unit);
496 }
497
498 void PrintSamplesToFile(void) {
499 FILE* out = graph_data_output_file_;
500 rtc::CritScope crit(&comparison_lock_);
501 std::sort(samples_.begin(), samples_.end(),
502 [](const Sample& A, const Sample& B) -> bool {
503 return A.input_time_ms < B.input_time_ms;
504 });
505
506 fprintf(out, "%s\n", test_label_.c_str());
507 fprintf(out, "%" PRIuS "\n", samples_.size());
508 fprintf(out,
509 "dropped "
510 "input_time_ms "
511 "send_time_ms "
512 "recv_time_ms "
513 "encoded_frame_size "
514 "psnr "
515 "ssim "
516 "render_time_ms\n");
517 for (const Sample& sample : samples_) {
518 fprintf(out, "%lf %lf %lf %lf %lf %lf %lf %lf\n", sample.dropped,
519 sample.input_time_ms, sample.send_time_ms, sample.recv_time_ms,
520 sample.encoded_frame_size, sample.psnr, sample.ssim,
521 sample.render_time_ms);
522 }
523 }
524
525 const std::string test_label_;
526 FILE* const graph_data_output_file_;
527 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
528 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
529 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
530 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
531 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
532 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
533 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
534 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
535 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
536 test::Statistics encode_time_ms GUARDED_BY(comparison_lock_);
537 test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_);
538 test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_);
539
540 const int frames_to_process_;
541 int frames_recorded_;
542 int frames_processed_;
543 int dropped_frames_;
544 int64_t last_render_time_;
545 uint32_t rtp_timestamp_delta_;
546
547 rtc::CriticalSection crit_;
548 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
549 VideoFrame last_rendered_frame_ GUARDED_BY(crit_);
550 std::map<uint32_t, int64_t> send_times_ GUARDED_BY(crit_);
551 std::map<uint32_t, int64_t> recv_times_ GUARDED_BY(crit_);
552 std::map<uint32_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
553 VideoFrame first_send_frame_ GUARDED_BY(crit_);
554 const double avg_psnr_threshold_;
555 const double avg_ssim_threshold_;
556
557 rtc::CriticalSection comparison_lock_;
558 std::vector<ThreadWrapper*> comparison_thread_pool_;
559 rtc::scoped_ptr<ThreadWrapper> stats_polling_thread_;
560 const rtc::scoped_ptr<EventWrapper> comparison_available_event_;
561 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
562 const rtc::scoped_ptr<EventWrapper> done_;
563};
564
565VideoQualityTest::VideoQualityTest() : clock_(Clock::GetRealTimeClock()) {}
566
567void VideoQualityTest::ValidateParams(const Params& params) {
568 RTC_CHECK_GE(params.common.max_bitrate_bps, params.common.target_bitrate_bps);
569 RTC_CHECK_GE(params.common.target_bitrate_bps, params.common.min_bitrate_bps);
570 RTC_CHECK_LT(params.common.tl_discard_threshold,
571 params.common.num_temporal_layers);
572}
573
574void VideoQualityTest::TestBody() {}
575
576void VideoQualityTest::SetupFullStack(const Params& params,
577 newapi::Transport* send_transport,
578 newapi::Transport* recv_transport) {
579 if (params.logs)
580 trace_to_stderr_.reset(new test::TraceToStderr);
581
582 CreateSendConfig(1, send_transport);
583
584 int payload_type;
585 if (params.common.codec == "VP8") {
586 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp8));
587 payload_type = kPayloadTypeVP8;
588 } else if (params.common.codec == "VP9") {
589 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp9));
590 payload_type = kPayloadTypeVP9;
591 } else {
592 RTC_NOTREACHED() << "Codec not supported!";
593 return;
594 }
595 send_config_.encoder_settings.encoder = encoder_.get();
596 send_config_.encoder_settings.payload_name = params.common.codec;
597 send_config_.encoder_settings.payload_type = payload_type;
598
599 send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
600 send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[0]);
601 send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
602
sprangef165ee2015-09-22 05:10:52 -0700603 send_config_.rtp.extensions.clear();
604 if (params.common.send_side_bwe) {
605 send_config_.rtp.extensions.push_back(RtpExtension(
606 RtpExtension::kTransportSequenceNumber, kTransportSeqExtensionId));
607 } else {
608 send_config_.rtp.extensions.push_back(
609 RtpExtension(RtpExtension::kAbsSendTime, kAbsSendTimeExtensionId));
610 }
611
ivica5d6a06c2015-09-17 05:30:24 -0700612 // Automatically fill out streams[0] with params.
613 VideoStream* stream = &encoder_config_.streams[0];
614 stream->width = params.common.width;
615 stream->height = params.common.height;
616 stream->min_bitrate_bps = params.common.min_bitrate_bps;
617 stream->target_bitrate_bps = params.common.target_bitrate_bps;
618 stream->max_bitrate_bps = params.common.max_bitrate_bps;
619 stream->max_framerate = static_cast<int>(params.common.fps);
620
621 stream->temporal_layer_thresholds_bps.clear();
622 if (params.common.num_temporal_layers > 1) {
623 stream->temporal_layer_thresholds_bps.push_back(stream->target_bitrate_bps);
624 }
625
626 CreateMatchingReceiveConfigs(recv_transport);
627
628 receive_configs_[0].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
629 receive_configs_[0].rtp.rtx[kSendRtxPayloadType].ssrc = kSendRtxSsrcs[0];
630 receive_configs_[0].rtp.rtx[kSendRtxPayloadType].payload_type =
631 kSendRtxPayloadType;
632
633 encoder_config_.min_transmit_bitrate_bps = params.common.min_transmit_bps;
634}
635
636void VideoQualityTest::SetupScreenshare(const Params& params) {
637 RTC_CHECK(params.screenshare.enabled);
638
639 // Fill out codec settings.
640 encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
641 if (params.common.codec == "VP8") {
642 codec_settings_.VP8 = VideoEncoder::GetDefaultVp8Settings();
643 codec_settings_.VP8.denoisingOn = false;
644 codec_settings_.VP8.frameDroppingOn = false;
645 codec_settings_.VP8.numberOfTemporalLayers =
646 static_cast<unsigned char>(params.common.num_temporal_layers);
647 encoder_config_.encoder_specific_settings = &codec_settings_.VP8;
648 } else if (params.common.codec == "VP9") {
649 codec_settings_.VP9 = VideoEncoder::GetDefaultVp9Settings();
650 codec_settings_.VP9.denoisingOn = false;
651 codec_settings_.VP9.frameDroppingOn = false;
652 codec_settings_.VP9.numberOfTemporalLayers =
653 static_cast<unsigned char>(params.common.num_temporal_layers);
654 encoder_config_.encoder_specific_settings = &codec_settings_.VP9;
655 }
656
657 // Setup frame generator.
658 const size_t kWidth = 1850;
659 const size_t kHeight = 1110;
660 std::vector<std::string> slides;
661 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
662 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
663 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
664 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
665
666 if (params.screenshare.scroll_duration == 0) {
667 // Cycle image every slide_change_interval seconds.
668 frame_generator_.reset(test::FrameGenerator::CreateFromYuvFile(
669 slides, kWidth, kHeight,
670 params.screenshare.slide_change_interval * params.common.fps));
671 } else {
672 RTC_CHECK_LE(params.common.width, kWidth);
673 RTC_CHECK_LE(params.common.height, kHeight);
674 RTC_CHECK_GT(params.screenshare.slide_change_interval, 0);
675 const int kPauseDurationMs = (params.screenshare.slide_change_interval -
676 params.screenshare.scroll_duration) * 1000;
677 RTC_CHECK_LE(params.screenshare.scroll_duration,
678 params.screenshare.slide_change_interval);
679
ivicad4818e72015-09-22 05:47:27 -0700680 if (params.screenshare.scroll_duration) {
681 frame_generator_.reset(
682 test::FrameGenerator::CreateScrollingInputFromYuvFiles(
683 clock_, slides, kWidth, kHeight, params.common.width,
684 params.common.height, params.screenshare.scroll_duration * 1000,
685 kPauseDurationMs));
686 } else {
687 frame_generator_.reset(test::FrameGenerator::CreateFromYuvFile(
688 slides, kWidth, kHeight,
689 params.screenshare.slide_change_interval * params.common.fps));
690 }
ivica5d6a06c2015-09-17 05:30:24 -0700691 }
692}
693
694void VideoQualityTest::CreateCapturer(const Params& params,
695 VideoCaptureInput* input) {
696 if (params.screenshare.enabled) {
ivica2d4e6c52015-09-23 01:57:06 -0700697 test::FrameGeneratorCapturer *frame_generator_capturer =
698 new test::FrameGeneratorCapturer(
699 clock_, input, frame_generator_.release(), params.common.fps);
700 EXPECT_TRUE(frame_generator_capturer->Init());
701 capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -0700702 } else {
703 if (params.video.clip_name.empty()) {
ivica2d4e6c52015-09-23 01:57:06 -0700704 capturer_.reset(test::VideoCapturer::Create(
ivica5d6a06c2015-09-17 05:30:24 -0700705 input, params.common.width, params.common.height, params.common.fps,
706 clock_));
ivica5d6a06c2015-09-17 05:30:24 -0700707 } else {
ivica2d4e6c52015-09-23 01:57:06 -0700708 capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
709 input, test::ResourcePath(params.video.clip_name, "yuv"),
710 params.common.width, params.common.height, params.common.fps,
711 clock_));
712 ASSERT_TRUE(capturer_.get() != nullptr)
ivica5d6a06c2015-09-17 05:30:24 -0700713 << "Could not create capturer for " << params.video.clip_name
714 << ".yuv. Is this resource file present?";
715 }
716 }
717}
718
719void VideoQualityTest::RunWithAnalyzer(const Params& params) {
720 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
721 // differentiate between the analyzer and the renderer case.
722 ValidateParams(params);
723
724 FILE* graph_data_output_file = nullptr;
725 if (!params.analyzer.graph_data_output_filename.empty()) {
726 graph_data_output_file =
727 fopen(params.analyzer.graph_data_output_filename.c_str(), "w");
728 RTC_CHECK(graph_data_output_file != nullptr)
729 << "Can't open the file "
730 << params.analyzer.graph_data_output_filename << "!";
731 }
732
733 test::LayerFilteringTransport send_transport(
734 params.pipe, kPayloadTypeVP8, kPayloadTypeVP9,
735 static_cast<uint8_t>(params.common.tl_discard_threshold), 0);
736 test::DirectTransport recv_transport(params.pipe);
737 VideoAnalyzer analyzer(
738 nullptr, &send_transport, params.analyzer.test_label,
739 params.analyzer.avg_psnr_threshold, params.analyzer.avg_ssim_threshold,
740 params.analyzer.test_durations_secs * params.common.fps,
741 graph_data_output_file);
742
743 Call::Config call_config;
744 call_config.bitrate_config = params.common.call_bitrate_config;
745 CreateCalls(call_config, call_config);
746
747 analyzer.SetReceiver(receiver_call_->Receiver());
748 send_transport.SetReceiver(&analyzer);
749 recv_transport.SetReceiver(sender_call_->Receiver());
750
751 SetupFullStack(params, &analyzer, &recv_transport);
752 receive_configs_[0].renderer = &analyzer;
753 for (auto& config : receive_configs_)
754 config.pre_decode_callback = &analyzer;
755
756 if (params.screenshare.enabled)
757 SetupScreenshare(params);
758
759 CreateCapturer(params, &analyzer);
760
761 CreateStreams();
762 analyzer.input_ = send_stream_->Input();
763 analyzer.send_stream_ = send_stream_;
764
ivica2d4e6c52015-09-23 01:57:06 -0700765 send_stream_->Start();
766 for (size_t i = 0; i < receive_streams_.size(); ++i)
767 receive_streams_[i]->Start();
768 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -0700769
770 analyzer.Wait();
771
772 send_transport.StopSending();
773 recv_transport.StopSending();
774
ivica2d4e6c52015-09-23 01:57:06 -0700775 capturer_->Stop();
776 for (size_t i = 0; i < receive_streams_.size(); ++i)
777 receive_streams_[i]->Stop();
778 send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700779
780 DestroyStreams();
781
782 if (graph_data_output_file)
783 fclose(graph_data_output_file);
784}
785
786void VideoQualityTest::RunWithVideoRenderer(const Params& params) {
787 ValidateParams(params);
788
789 rtc::scoped_ptr<test::VideoRenderer> local_preview(
790 test::VideoRenderer::Create("Local Preview", params.common.width,
791 params.common.height));
792 rtc::scoped_ptr<test::VideoRenderer> loopback_video(
793 test::VideoRenderer::Create("Loopback Video", params.common.width,
794 params.common.height));
795
796 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
797 // match the full stack tests.
798 Call::Config call_config;
799 call_config.bitrate_config = params.common.call_bitrate_config;
800 rtc::scoped_ptr<Call> call(Call::Create(call_config));
801
802 test::LayerFilteringTransport transport(
803 params.pipe, kPayloadTypeVP8, kPayloadTypeVP9,
804 static_cast<uint8_t>(params.common.tl_discard_threshold), 0);
805 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
806 // least share as much code as possible. That way this test would also match
807 // the full stack tests better.
808 transport.SetReceiver(call->Receiver());
809
810 SetupFullStack(params, &transport, &transport);
811 send_config_.local_renderer = local_preview.get();
812 receive_configs_[0].renderer = loopback_video.get();
813
814 if (params.screenshare.enabled)
815 SetupScreenshare(params);
816
817 send_stream_ = call->CreateVideoSendStream(send_config_, encoder_config_);
818 CreateCapturer(params, send_stream_->Input());
819
820 VideoReceiveStream* receive_stream =
821 call->CreateVideoReceiveStream(receive_configs_[0]);
822
823 receive_stream->Start();
824 send_stream_->Start();
ivica2d4e6c52015-09-23 01:57:06 -0700825 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -0700826
827 test::PressEnterToContinue();
828
ivica2d4e6c52015-09-23 01:57:06 -0700829 capturer_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700830 send_stream_->Stop();
831 receive_stream->Stop();
832
833 call->DestroyVideoReceiveStream(receive_stream);
834 call->DestroyVideoSendStream(send_stream_);
835
836 transport.StopSending();
837}
838
839} // namespace webrtc