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