Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
Niels Möller | 1c931c4 | 2018-12-18 16:08:11 +0100 | [diff] [blame] | 15 | #include "common_video/libyuv/include/webrtc_libyuv.h" |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 16 | #include "modules/rtp_rtcp/source/rtp_format.h" |
| 17 | #include "modules/rtp_rtcp/source/rtp_utility.h" |
| 18 | #include "rtc_base/cpu_time.h" |
| 19 | #include "rtc_base/flags.h" |
| 20 | #include "rtc_base/format_macros.h" |
| 21 | #include "rtc_base/memory_usage.h" |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 22 | #include "system_wrappers/include/cpu_info.h" |
| 23 | #include "test/call_test.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "test/testsupport/file_utils.h" |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 25 | #include "test/testsupport/frame_writer.h" |
| 26 | #include "test/testsupport/perf_test.h" |
| 27 | #include "test/testsupport/test_artifacts.h" |
| 28 | |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 29 | WEBRTC_DEFINE_bool( |
| 30 | save_worst_frame, |
| 31 | false, |
| 32 | "Enable saving a frame with the lowest PSNR to a jpeg file in the " |
| 33 | "test_artifacts_dir"); |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 34 | |
| 35 | namespace webrtc { |
| 36 | namespace { |
| 37 | constexpr int kSendStatsPollingIntervalMs = 1000; |
| 38 | constexpr size_t kMaxComparisons = 10; |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 39 | // How often is keep alive message printed. |
| 40 | constexpr int kKeepAliveIntervalSeconds = 30; |
| 41 | // Interval between checking that the test is over. |
| 42 | constexpr int kProbingIntervalMs = 500; |
| 43 | constexpr int kKeepAliveIntervalIterations = |
| 44 | kKeepAliveIntervalSeconds * 1000 / kProbingIntervalMs; |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 45 | |
| 46 | bool IsFlexfec(int payload_type) { |
| 47 | return payload_type == test::CallTest::kFlexfecPayloadType; |
| 48 | } |
| 49 | } // namespace |
| 50 | |
| 51 | VideoAnalyzer::VideoAnalyzer(test::LayerFilteringTransport* transport, |
| 52 | const std::string& test_label, |
| 53 | double avg_psnr_threshold, |
| 54 | double avg_ssim_threshold, |
| 55 | int duration_frames, |
| 56 | FILE* graph_data_output_file, |
| 57 | const std::string& graph_title, |
| 58 | uint32_t ssrc_to_analyze, |
| 59 | uint32_t rtx_ssrc_to_analyze, |
| 60 | size_t selected_stream, |
| 61 | int selected_sl, |
| 62 | int selected_tl, |
| 63 | bool is_quick_test_enabled, |
| 64 | Clock* clock, |
Ilya Nikolaevskiy | 85fc325 | 2019-02-11 10:41:50 +0100 | [diff] [blame^] | 65 | std::string rtp_dump_name) |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 66 | : transport_(transport), |
| 67 | receiver_(nullptr), |
| 68 | call_(nullptr), |
| 69 | send_stream_(nullptr), |
| 70 | receive_stream_(nullptr), |
Christoffer Rodbro | c2a0288 | 2018-08-07 14:10:56 +0200 | [diff] [blame] | 71 | audio_receive_stream_(nullptr), |
Ilya Nikolaevskiy | 85fc325 | 2019-02-11 10:41:50 +0100 | [diff] [blame^] | 72 | captured_frame_forwarder_(this, clock, duration_frames), |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 73 | test_label_(test_label), |
| 74 | graph_data_output_file_(graph_data_output_file), |
| 75 | graph_title_(graph_title), |
| 76 | ssrc_to_analyze_(ssrc_to_analyze), |
| 77 | rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze), |
| 78 | selected_stream_(selected_stream), |
| 79 | selected_sl_(selected_sl), |
| 80 | selected_tl_(selected_tl), |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 81 | last_fec_bytes_(0), |
| 82 | frames_to_process_(duration_frames), |
| 83 | frames_recorded_(0), |
| 84 | frames_processed_(0), |
| 85 | dropped_frames_(0), |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 86 | captured_frames_(0), |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 87 | dropped_frames_before_first_encode_(0), |
| 88 | dropped_frames_before_rendering_(0), |
| 89 | last_render_time_(0), |
| 90 | last_render_delta_ms_(0), |
| 91 | last_unfreeze_time_ms_(0), |
| 92 | rtp_timestamp_delta_(0), |
| 93 | total_media_bytes_(0), |
| 94 | first_sending_time_(0), |
| 95 | last_sending_time_(0), |
| 96 | cpu_time_(0), |
| 97 | wallclock_time_(0), |
| 98 | avg_psnr_threshold_(avg_psnr_threshold), |
| 99 | avg_ssim_threshold_(avg_ssim_threshold), |
| 100 | is_quick_test_enabled_(is_quick_test_enabled), |
| 101 | stats_polling_thread_(&PollStatsThread, this, "StatsPoller"), |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 102 | done_(true, false), |
| 103 | clock_(clock), |
| 104 | start_ms_(clock->TimeInMilliseconds()) { |
| 105 | // Create thread pool for CPU-expensive PSNR/SSIM calculations. |
| 106 | |
| 107 | // Try to use about as many threads as cores, but leave kMinCoresLeft alone, |
| 108 | // so that we don't accidentally starve "real" worker threads (codec etc). |
| 109 | // Also, don't allocate more than kMaxComparisonThreads, even if there are |
| 110 | // spare cores. |
| 111 | |
| 112 | uint32_t num_cores = CpuInfo::DetectNumberOfCores(); |
| 113 | RTC_DCHECK_GE(num_cores, 1); |
| 114 | static const uint32_t kMinCoresLeft = 4; |
| 115 | static const uint32_t kMaxComparisonThreads = 8; |
| 116 | |
| 117 | if (num_cores <= kMinCoresLeft) { |
| 118 | num_cores = 1; |
| 119 | } else { |
| 120 | num_cores -= kMinCoresLeft; |
| 121 | num_cores = std::min(num_cores, kMaxComparisonThreads); |
| 122 | } |
| 123 | |
| 124 | for (uint32_t i = 0; i < num_cores; ++i) { |
| 125 | rtc::PlatformThread* thread = |
| 126 | new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer"); |
| 127 | thread->Start(); |
| 128 | comparison_thread_pool_.push_back(thread); |
| 129 | } |
| 130 | |
| 131 | if (!rtp_dump_name.empty()) { |
| 132 | fprintf(stdout, "Writing rtp dump to %s\n", rtp_dump_name.c_str()); |
| 133 | rtp_file_writer_.reset(test::RtpFileWriter::Create( |
| 134 | test::RtpFileWriter::kRtpDump, rtp_dump_name)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | VideoAnalyzer::~VideoAnalyzer() { |
| 139 | for (rtc::PlatformThread* thread : comparison_thread_pool_) { |
| 140 | thread->Stop(); |
| 141 | delete thread; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void VideoAnalyzer::SetReceiver(PacketReceiver* receiver) { |
| 146 | receiver_ = receiver; |
| 147 | } |
| 148 | |
Niels Möller | 1c931c4 | 2018-12-18 16:08:11 +0100 | [diff] [blame] | 149 | void VideoAnalyzer::SetSource( |
| 150 | rtc::VideoSourceInterface<VideoFrame>* video_source, |
| 151 | bool respect_sink_wants) { |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 152 | if (respect_sink_wants) |
Niels Möller | 1c931c4 | 2018-12-18 16:08:11 +0100 | [diff] [blame] | 153 | captured_frame_forwarder_.SetSource(video_source); |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 154 | rtc::VideoSinkWants wants; |
Niels Möller | 1c931c4 | 2018-12-18 16:08:11 +0100 | [diff] [blame] | 155 | video_source->AddOrUpdateSink(InputInterface(), wants); |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void VideoAnalyzer::SetCall(Call* call) { |
| 159 | rtc::CritScope lock(&crit_); |
| 160 | RTC_DCHECK(!call_); |
| 161 | call_ = call; |
| 162 | } |
| 163 | |
| 164 | void VideoAnalyzer::SetSendStream(VideoSendStream* stream) { |
| 165 | rtc::CritScope lock(&crit_); |
| 166 | RTC_DCHECK(!send_stream_); |
| 167 | send_stream_ = stream; |
| 168 | } |
| 169 | |
| 170 | void VideoAnalyzer::SetReceiveStream(VideoReceiveStream* stream) { |
| 171 | rtc::CritScope lock(&crit_); |
| 172 | RTC_DCHECK(!receive_stream_); |
| 173 | receive_stream_ = stream; |
| 174 | } |
| 175 | |
Christoffer Rodbro | c2a0288 | 2018-08-07 14:10:56 +0200 | [diff] [blame] | 176 | void VideoAnalyzer::SetAudioReceiveStream(AudioReceiveStream* recv_stream) { |
| 177 | rtc::CritScope lock(&crit_); |
| 178 | RTC_CHECK(!audio_receive_stream_); |
| 179 | audio_receive_stream_ = recv_stream; |
| 180 | } |
| 181 | |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 182 | rtc::VideoSinkInterface<VideoFrame>* VideoAnalyzer::InputInterface() { |
| 183 | return &captured_frame_forwarder_; |
| 184 | } |
| 185 | |
| 186 | rtc::VideoSourceInterface<VideoFrame>* VideoAnalyzer::OutputInterface() { |
| 187 | return &captured_frame_forwarder_; |
| 188 | } |
| 189 | |
| 190 | PacketReceiver::DeliveryStatus VideoAnalyzer::DeliverPacket( |
| 191 | MediaType media_type, |
| 192 | rtc::CopyOnWriteBuffer packet, |
Niels Möller | 7008287 | 2018-08-07 11:03:12 +0200 | [diff] [blame] | 193 | int64_t packet_time_us) { |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 194 | // Ignore timestamps of RTCP packets. They're not synchronized with |
| 195 | // RTP packet timestamps and so they would confuse wrap_handler_. |
| 196 | if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size())) { |
Niels Möller | 7008287 | 2018-08-07 11:03:12 +0200 | [diff] [blame] | 197 | return receiver_->DeliverPacket(media_type, std::move(packet), |
| 198 | packet_time_us); |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | if (rtp_file_writer_) { |
| 202 | test::RtpPacket p; |
| 203 | memcpy(p.data, packet.cdata(), packet.size()); |
| 204 | p.length = packet.size(); |
| 205 | p.original_length = packet.size(); |
| 206 | p.time_ms = clock_->TimeInMilliseconds() - start_ms_; |
| 207 | rtp_file_writer_->WritePacket(&p); |
| 208 | } |
| 209 | |
| 210 | RtpUtility::RtpHeaderParser parser(packet.cdata(), packet.size()); |
| 211 | RTPHeader header; |
| 212 | parser.Parse(&header); |
| 213 | if (!IsFlexfec(header.payloadType) && (header.ssrc == ssrc_to_analyze_ || |
| 214 | header.ssrc == rtx_ssrc_to_analyze_)) { |
| 215 | // Ignore FlexFEC timestamps, to avoid collisions with media timestamps. |
| 216 | // (FlexFEC and media are sent on different SSRCs, which have different |
| 217 | // timestamps spaces.) |
| 218 | // Also ignore packets from wrong SSRC, but include retransmits. |
| 219 | rtc::CritScope lock(&crit_); |
| 220 | int64_t timestamp = |
| 221 | wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_); |
| 222 | recv_times_[timestamp] = |
| 223 | Clock::GetRealTimeClock()->CurrentNtpInMilliseconds(); |
| 224 | } |
| 225 | |
Niels Möller | 7008287 | 2018-08-07 11:03:12 +0200 | [diff] [blame] | 226 | return receiver_->DeliverPacket(media_type, std::move(packet), |
| 227 | packet_time_us); |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void VideoAnalyzer::PreEncodeOnFrame(const VideoFrame& video_frame) { |
| 231 | rtc::CritScope lock(&crit_); |
| 232 | if (!first_encoded_timestamp_) { |
| 233 | while (frames_.front().timestamp() != video_frame.timestamp()) { |
| 234 | ++dropped_frames_before_first_encode_; |
| 235 | frames_.pop_front(); |
| 236 | RTC_CHECK(!frames_.empty()); |
| 237 | } |
| 238 | first_encoded_timestamp_ = video_frame.timestamp(); |
| 239 | } |
| 240 | } |
| 241 | |
Niels Möller | 88be972 | 2018-10-10 10:58:52 +0200 | [diff] [blame] | 242 | void VideoAnalyzer::PostEncodeOnFrame(size_t stream_id, uint32_t timestamp) { |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 243 | rtc::CritScope lock(&crit_); |
Niels Möller | 88be972 | 2018-10-10 10:58:52 +0200 | [diff] [blame] | 244 | if (!first_sent_timestamp_ && stream_id == selected_stream_) { |
| 245 | first_sent_timestamp_ = timestamp; |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | bool VideoAnalyzer::SendRtp(const uint8_t* packet, |
| 250 | size_t length, |
| 251 | const PacketOptions& options) { |
| 252 | RtpUtility::RtpHeaderParser parser(packet, length); |
| 253 | RTPHeader header; |
| 254 | parser.Parse(&header); |
| 255 | |
| 256 | int64_t current_time = Clock::GetRealTimeClock()->CurrentNtpInMilliseconds(); |
| 257 | |
| 258 | bool result = transport_->SendRtp(packet, length, options); |
| 259 | { |
| 260 | rtc::CritScope lock(&crit_); |
| 261 | if (rtp_timestamp_delta_ == 0 && header.ssrc == ssrc_to_analyze_) { |
| 262 | RTC_CHECK(static_cast<bool>(first_sent_timestamp_)); |
| 263 | rtp_timestamp_delta_ = header.timestamp - *first_sent_timestamp_; |
| 264 | } |
| 265 | |
| 266 | if (!IsFlexfec(header.payloadType) && header.ssrc == ssrc_to_analyze_) { |
| 267 | // Ignore FlexFEC timestamps, to avoid collisions with media timestamps. |
| 268 | // (FlexFEC and media are sent on different SSRCs, which have different |
| 269 | // timestamps spaces.) |
| 270 | // Also ignore packets from wrong SSRC and retransmits. |
| 271 | int64_t timestamp = |
| 272 | wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_); |
| 273 | send_times_[timestamp] = current_time; |
| 274 | |
| 275 | if (IsInSelectedSpatialAndTemporalLayer(packet, length, header)) { |
| 276 | encoded_frame_sizes_[timestamp] += |
| 277 | length - (header.headerLength + header.paddingLength); |
| 278 | total_media_bytes_ += |
| 279 | length - (header.headerLength + header.paddingLength); |
| 280 | } |
| 281 | if (first_sending_time_ == 0) |
| 282 | first_sending_time_ = current_time; |
| 283 | last_sending_time_ = current_time; |
| 284 | } |
| 285 | } |
| 286 | return result; |
| 287 | } |
| 288 | |
| 289 | bool VideoAnalyzer::SendRtcp(const uint8_t* packet, size_t length) { |
| 290 | return transport_->SendRtcp(packet, length); |
| 291 | } |
| 292 | |
| 293 | void VideoAnalyzer::OnFrame(const VideoFrame& video_frame) { |
| 294 | int64_t render_time_ms = |
| 295 | Clock::GetRealTimeClock()->CurrentNtpInMilliseconds(); |
| 296 | |
| 297 | rtc::CritScope lock(&crit_); |
| 298 | |
| 299 | StartExcludingCpuThreadTime(); |
| 300 | |
| 301 | int64_t send_timestamp = |
| 302 | wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_); |
| 303 | |
| 304 | while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) { |
| 305 | if (!last_rendered_frame_) { |
| 306 | // No previous frame rendered, this one was dropped after sending but |
| 307 | // before rendering. |
| 308 | ++dropped_frames_before_rendering_; |
| 309 | } else { |
| 310 | AddFrameComparison(frames_.front(), *last_rendered_frame_, true, |
| 311 | render_time_ms); |
| 312 | } |
| 313 | frames_.pop_front(); |
| 314 | RTC_DCHECK(!frames_.empty()); |
| 315 | } |
| 316 | |
| 317 | VideoFrame reference_frame = frames_.front(); |
| 318 | frames_.pop_front(); |
| 319 | int64_t reference_timestamp = |
| 320 | wrap_handler_.Unwrap(reference_frame.timestamp()); |
| 321 | if (send_timestamp == reference_timestamp - 1) { |
| 322 | // TODO(ivica): Make this work for > 2 streams. |
| 323 | // Look at RTPSender::BuildRTPHeader. |
| 324 | ++send_timestamp; |
| 325 | } |
| 326 | ASSERT_EQ(reference_timestamp, send_timestamp); |
| 327 | |
| 328 | AddFrameComparison(reference_frame, video_frame, false, render_time_ms); |
| 329 | |
| 330 | last_rendered_frame_ = video_frame; |
| 331 | |
| 332 | StopExcludingCpuThreadTime(); |
| 333 | } |
| 334 | |
| 335 | void VideoAnalyzer::Wait() { |
| 336 | // Frame comparisons can be very expensive. Wait for test to be done, but |
| 337 | // at time-out check if frames_processed is going up. If so, give it more |
| 338 | // time, otherwise fail. Hopefully this will reduce test flakiness. |
| 339 | |
| 340 | stats_polling_thread_.Start(); |
| 341 | |
| 342 | int last_frames_processed = -1; |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 343 | int last_frames_captured = -1; |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 344 | int iteration = 0; |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 345 | |
| 346 | while (!done_.Wait(kProbingIntervalMs)) { |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 347 | int frames_processed; |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 348 | int frames_captured; |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 349 | { |
| 350 | rtc::CritScope crit(&comparison_lock_); |
| 351 | frames_processed = frames_processed_; |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 352 | frames_captured = captured_frames_; |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Print some output so test infrastructure won't think we've crashed. |
| 356 | const char* kKeepAliveMessages[3] = { |
| 357 | "Uh, I'm-I'm not quite dead, sir.", |
| 358 | "Uh, I-I think uh, I could pull through, sir.", |
| 359 | "Actually, I think I'm all right to come with you--"}; |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 360 | if (++iteration % kKeepAliveIntervalIterations == 0) { |
| 361 | printf("- %s\n", kKeepAliveMessages[iteration % 3]); |
| 362 | } |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 363 | |
| 364 | if (last_frames_processed == -1) { |
| 365 | last_frames_processed = frames_processed; |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 366 | last_frames_captured = frames_captured; |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 367 | continue; |
| 368 | } |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 369 | if (frames_processed == last_frames_processed && |
| 370 | last_frames_captured == frames_captured) { |
| 371 | if (frames_captured < frames_to_process_) { |
| 372 | EXPECT_GT(frames_processed, last_frames_processed) |
| 373 | << "Analyzer stalled while waiting for test to finish."; |
| 374 | } |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 375 | done_.Set(); |
| 376 | break; |
| 377 | } |
| 378 | last_frames_processed = frames_processed; |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 379 | last_frames_captured = frames_captured; |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | if (iteration > 0) |
| 383 | printf("- Farewell, sweet Concorde!\n"); |
| 384 | |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 385 | PrintResults(); |
| 386 | if (graph_data_output_file_) |
| 387 | PrintSamplesToFile(); |
| 388 | |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 389 | stats_polling_thread_.Stop(); |
| 390 | } |
| 391 | |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 392 | void VideoAnalyzer::StartMeasuringCpuProcessTime() { |
| 393 | rtc::CritScope lock(&cpu_measurement_lock_); |
| 394 | cpu_time_ -= rtc::GetProcessCpuTimeNanos(); |
| 395 | wallclock_time_ -= rtc::SystemTimeNanos(); |
| 396 | } |
| 397 | |
| 398 | void VideoAnalyzer::StopMeasuringCpuProcessTime() { |
| 399 | rtc::CritScope lock(&cpu_measurement_lock_); |
| 400 | cpu_time_ += rtc::GetProcessCpuTimeNanos(); |
| 401 | wallclock_time_ += rtc::SystemTimeNanos(); |
| 402 | } |
| 403 | |
| 404 | void VideoAnalyzer::StartExcludingCpuThreadTime() { |
| 405 | rtc::CritScope lock(&cpu_measurement_lock_); |
| 406 | cpu_time_ += rtc::GetThreadCpuTimeNanos(); |
| 407 | } |
| 408 | |
| 409 | void VideoAnalyzer::StopExcludingCpuThreadTime() { |
| 410 | rtc::CritScope lock(&cpu_measurement_lock_); |
| 411 | cpu_time_ -= rtc::GetThreadCpuTimeNanos(); |
| 412 | } |
| 413 | |
| 414 | double VideoAnalyzer::GetCpuUsagePercent() { |
| 415 | rtc::CritScope lock(&cpu_measurement_lock_); |
| 416 | return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0; |
| 417 | } |
| 418 | |
| 419 | bool VideoAnalyzer::IsInSelectedSpatialAndTemporalLayer( |
| 420 | const uint8_t* packet, |
| 421 | size_t length, |
| 422 | const RTPHeader& header) { |
| 423 | if (header.payloadType != test::CallTest::kPayloadTypeVP9 && |
| 424 | header.payloadType != test::CallTest::kPayloadTypeVP8) { |
| 425 | return true; |
| 426 | } else { |
| 427 | // Get VP8 and VP9 specific header to check layers indexes. |
| 428 | const uint8_t* payload = packet + header.headerLength; |
| 429 | const size_t payload_length = length - header.headerLength; |
| 430 | const size_t payload_data_length = payload_length - header.paddingLength; |
| 431 | const bool is_vp8 = header.payloadType == test::CallTest::kPayloadTypeVP8; |
| 432 | std::unique_ptr<RtpDepacketizer> depacketizer( |
| 433 | RtpDepacketizer::Create(is_vp8 ? kVideoCodecVP8 : kVideoCodecVP9)); |
| 434 | RtpDepacketizer::ParsedPayload parsed_payload; |
| 435 | bool result = |
| 436 | depacketizer->Parse(&parsed_payload, payload, payload_data_length); |
| 437 | RTC_DCHECK(result); |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 438 | |
| 439 | int temporal_idx; |
| 440 | int spatial_idx; |
| 441 | if (is_vp8) { |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 +0000 | [diff] [blame] | 442 | temporal_idx = absl::get<RTPVideoHeaderVP8>( |
| 443 | parsed_payload.video_header().video_type_header) |
| 444 | .temporalIdx; |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 445 | spatial_idx = kNoTemporalIdx; |
| 446 | } else { |
| 447 | const auto& vp9_header = absl::get<RTPVideoHeaderVP9>( |
| 448 | parsed_payload.video_header().video_type_header); |
| 449 | temporal_idx = vp9_header.temporal_idx; |
| 450 | spatial_idx = vp9_header.spatial_idx; |
| 451 | } |
| 452 | |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 453 | return (selected_tl_ < 0 || temporal_idx == kNoTemporalIdx || |
| 454 | temporal_idx <= selected_tl_) && |
| 455 | (selected_sl_ < 0 || spatial_idx == kNoSpatialIdx || |
| 456 | spatial_idx <= selected_sl_); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | void VideoAnalyzer::PollStatsThread(void* obj) { |
| 461 | static_cast<VideoAnalyzer*>(obj)->PollStats(); |
| 462 | } |
| 463 | |
| 464 | void VideoAnalyzer::PollStats() { |
| 465 | while (!done_.Wait(kSendStatsPollingIntervalMs)) { |
| 466 | rtc::CritScope crit(&comparison_lock_); |
| 467 | |
| 468 | Call::Stats call_stats = call_->GetStats(); |
| 469 | send_bandwidth_bps_.AddSample(call_stats.send_bandwidth_bps); |
| 470 | |
| 471 | VideoSendStream::Stats send_stats = send_stream_->GetStats(); |
| 472 | // It's not certain that we yet have estimates for any of these stats. |
| 473 | // Check that they are positive before mixing them in. |
| 474 | if (send_stats.encode_frame_rate > 0) |
| 475 | encode_frame_rate_.AddSample(send_stats.encode_frame_rate); |
| 476 | if (send_stats.avg_encode_time_ms > 0) |
| 477 | encode_time_ms_.AddSample(send_stats.avg_encode_time_ms); |
| 478 | if (send_stats.encode_usage_percent > 0) |
| 479 | encode_usage_percent_.AddSample(send_stats.encode_usage_percent); |
| 480 | if (send_stats.media_bitrate_bps > 0) |
| 481 | media_bitrate_bps_.AddSample(send_stats.media_bitrate_bps); |
| 482 | size_t fec_bytes = 0; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 483 | for (const auto& kv : send_stats.substreams) { |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 484 | fec_bytes += kv.second.rtp_stats.fec.payload_bytes + |
| 485 | kv.second.rtp_stats.fec.padding_bytes; |
| 486 | } |
| 487 | fec_bitrate_bps_.AddSample((fec_bytes - last_fec_bytes_) * 8); |
| 488 | last_fec_bytes_ = fec_bytes; |
| 489 | |
| 490 | if (receive_stream_ != nullptr) { |
| 491 | VideoReceiveStream::Stats receive_stats = receive_stream_->GetStats(); |
| 492 | 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); |
Ilya Nikolaevskiy | d47d3eb | 2019-01-21 16:27:17 +0100 | [diff] [blame] | 496 | if (receive_stats.width > 0 && receive_stats.height > 0) { |
| 497 | pixels_.AddSample(receive_stats.width * receive_stats.height); |
| 498 | } |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 499 | } |
| 500 | |
Christoffer Rodbro | c2a0288 | 2018-08-07 14:10:56 +0200 | [diff] [blame] | 501 | if (audio_receive_stream_ != nullptr) { |
| 502 | AudioReceiveStream::Stats receive_stats = |
| 503 | audio_receive_stream_->GetStats(); |
| 504 | audio_expand_rate_.AddSample(receive_stats.expand_rate); |
| 505 | audio_accelerate_rate_.AddSample(receive_stats.accelerate_rate); |
| 506 | audio_jitter_buffer_ms_.AddSample(receive_stats.jitter_buffer_ms); |
| 507 | } |
| 508 | |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 509 | memory_usage_.AddSample(rtc::GetProcessResidentSizeBytes()); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | bool VideoAnalyzer::FrameComparisonThread(void* obj) { |
| 514 | return static_cast<VideoAnalyzer*>(obj)->CompareFrames(); |
| 515 | } |
| 516 | |
| 517 | bool VideoAnalyzer::CompareFrames() { |
| 518 | if (AllFramesRecorded()) |
| 519 | return false; |
| 520 | |
| 521 | FrameComparison comparison; |
| 522 | |
| 523 | if (!PopComparison(&comparison)) { |
| 524 | // Wait until new comparison task is available, or test is done. |
| 525 | // If done, wake up remaining threads waiting. |
| 526 | comparison_available_event_.Wait(1000); |
| 527 | if (AllFramesRecorded()) { |
| 528 | comparison_available_event_.Set(); |
| 529 | return false; |
| 530 | } |
| 531 | return true; // Try again. |
| 532 | } |
| 533 | |
| 534 | StartExcludingCpuThreadTime(); |
| 535 | |
| 536 | PerformFrameComparison(comparison); |
| 537 | |
| 538 | StopExcludingCpuThreadTime(); |
| 539 | |
| 540 | if (FrameProcessed()) { |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 541 | done_.Set(); |
| 542 | comparison_available_event_.Set(); |
| 543 | return false; |
| 544 | } |
| 545 | |
| 546 | return true; |
| 547 | } |
| 548 | |
| 549 | bool VideoAnalyzer::PopComparison(VideoAnalyzer::FrameComparison* comparison) { |
| 550 | rtc::CritScope crit(&comparison_lock_); |
| 551 | // If AllFramesRecorded() is true, it means we have already popped |
| 552 | // frames_to_process_ frames from comparisons_, so there is no more work |
| 553 | // for this thread to be done. frames_processed_ might still be lower if |
| 554 | // all comparisons are not done, but those frames are currently being |
| 555 | // worked on by other threads. |
| 556 | if (comparisons_.empty() || AllFramesRecorded()) |
| 557 | return false; |
| 558 | |
| 559 | *comparison = comparisons_.front(); |
| 560 | comparisons_.pop_front(); |
| 561 | |
| 562 | FrameRecorded(); |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | void VideoAnalyzer::FrameRecorded() { |
| 567 | rtc::CritScope crit(&comparison_lock_); |
| 568 | ++frames_recorded_; |
| 569 | } |
| 570 | |
| 571 | bool VideoAnalyzer::AllFramesRecorded() { |
| 572 | rtc::CritScope crit(&comparison_lock_); |
| 573 | assert(frames_recorded_ <= frames_to_process_); |
| 574 | return frames_recorded_ == frames_to_process_; |
| 575 | } |
| 576 | |
| 577 | bool VideoAnalyzer::FrameProcessed() { |
| 578 | rtc::CritScope crit(&comparison_lock_); |
| 579 | ++frames_processed_; |
| 580 | assert(frames_processed_ <= frames_to_process_); |
| 581 | return frames_processed_ == frames_to_process_; |
| 582 | } |
| 583 | |
| 584 | void VideoAnalyzer::PrintResults() { |
| 585 | StopMeasuringCpuProcessTime(); |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 586 | int frames_left; |
| 587 | { |
| 588 | rtc::CritScope crit(&crit_); |
| 589 | frames_left = frames_.size(); |
| 590 | } |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 591 | rtc::CritScope crit(&comparison_lock_); |
| 592 | // Record the time from the last freeze until the last rendered frame to |
| 593 | // ensure we cover the full timespan of the session. Otherwise the metric |
| 594 | // would penalize an early freeze followed by no freezes until the end. |
| 595 | time_between_freezes_.AddSample(last_render_time_ - last_unfreeze_time_ms_); |
| 596 | PrintResult("psnr", psnr_, " dB"); |
| 597 | PrintResult("ssim", ssim_, " score"); |
| 598 | PrintResult("sender_time", sender_time_, " ms"); |
| 599 | PrintResult("receiver_time", receiver_time_, " ms"); |
| 600 | PrintResult("network_time", network_time_, " ms"); |
| 601 | PrintResult("total_delay_incl_network", end_to_end_, " ms"); |
| 602 | PrintResult("time_between_rendered_frames", rendered_delta_, " ms"); |
| 603 | PrintResult("encode_frame_rate", encode_frame_rate_, " fps"); |
| 604 | PrintResult("encode_time", encode_time_ms_, " ms"); |
| 605 | PrintResult("media_bitrate", media_bitrate_bps_, " bps"); |
| 606 | PrintResult("fec_bitrate", fec_bitrate_bps_, " bps"); |
| 607 | PrintResult("send_bandwidth", send_bandwidth_bps_, " bps"); |
| 608 | PrintResult("time_between_freezes", time_between_freezes_, " ms"); |
Ilya Nikolaevskiy | d47d3eb | 2019-01-21 16:27:17 +0100 | [diff] [blame] | 609 | PrintResult("pixels_per_frame", pixels_, " px"); |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 610 | |
| 611 | if (worst_frame_) { |
| 612 | test::PrintResult("min_psnr", "", test_label_.c_str(), worst_frame_->psnr, |
| 613 | "dB", false); |
| 614 | } |
| 615 | |
| 616 | if (receive_stream_ != nullptr) { |
| 617 | PrintResult("decode_time", decode_time_ms_, " ms"); |
| 618 | } |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 619 | dropped_frames_ += dropped_frames_before_first_encode_ + |
| 620 | dropped_frames_before_rendering_ + frames_left; |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 621 | test::PrintResult("dropped_frames", "", test_label_.c_str(), dropped_frames_, |
| 622 | "frames", false); |
| 623 | test::PrintResult("cpu_usage", "", test_label_.c_str(), GetCpuUsagePercent(), |
| 624 | "%", false); |
| 625 | |
| 626 | #if defined(WEBRTC_WIN) |
| 627 | // On Linux and Mac in Resident Set some unused pages may be counted. |
| 628 | // Therefore this metric will depend on order in which tests are run and |
| 629 | // will be flaky. |
| 630 | PrintResult("memory_usage", memory_usage_, " bytes"); |
| 631 | #endif |
| 632 | |
| 633 | // Saving only the worst frame for manual analysis. Intention here is to |
| 634 | // only detect video corruptions and not to track picture quality. Thus, |
| 635 | // jpeg is used here. |
| 636 | if (FLAG_save_worst_frame && worst_frame_) { |
| 637 | std::string output_dir; |
| 638 | test::GetTestArtifactsDir(&output_dir); |
| 639 | std::string output_path = |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 640 | test::JoinFilename(output_dir, test_label_ + ".jpg"); |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 641 | RTC_LOG(LS_INFO) << "Saving worst frame to " << output_path; |
| 642 | test::JpegFrameWriter frame_writer(output_path); |
| 643 | RTC_CHECK( |
| 644 | frame_writer.WriteFrame(worst_frame_->frame, 100 /*best quality*/)); |
| 645 | } |
| 646 | |
Christoffer Rodbro | c2a0288 | 2018-08-07 14:10:56 +0200 | [diff] [blame] | 647 | if (audio_receive_stream_ != nullptr) { |
| 648 | PrintResult("audio_expand_rate", audio_expand_rate_, ""); |
| 649 | PrintResult("audio_accelerate_rate", audio_accelerate_rate_, ""); |
| 650 | PrintResult("audio_jitter_buffer", audio_jitter_buffer_ms_, " ms"); |
| 651 | } |
| 652 | |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 653 | // Disable quality check for quick test, as quality checks may fail |
| 654 | // because too few samples were collected. |
| 655 | if (!is_quick_test_enabled_) { |
| 656 | EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_); |
| 657 | EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | void VideoAnalyzer::PerformFrameComparison( |
| 662 | const VideoAnalyzer::FrameComparison& comparison) { |
| 663 | // Perform expensive psnr and ssim calculations while not holding lock. |
| 664 | double psnr = -1.0; |
| 665 | double ssim = -1.0; |
| 666 | if (comparison.reference && !comparison.dropped) { |
| 667 | psnr = I420PSNR(&*comparison.reference, &*comparison.render); |
| 668 | ssim = I420SSIM(&*comparison.reference, &*comparison.render); |
| 669 | } |
| 670 | |
| 671 | rtc::CritScope crit(&comparison_lock_); |
| 672 | |
| 673 | if (psnr >= 0.0 && (!worst_frame_ || worst_frame_->psnr > psnr)) { |
| 674 | worst_frame_.emplace(FrameWithPsnr{psnr, *comparison.render}); |
| 675 | } |
| 676 | |
| 677 | if (graph_data_output_file_) { |
| 678 | samples_.push_back(Sample(comparison.dropped, comparison.input_time_ms, |
| 679 | comparison.send_time_ms, comparison.recv_time_ms, |
| 680 | comparison.render_time_ms, |
| 681 | comparison.encoded_frame_size, psnr, ssim)); |
| 682 | } |
| 683 | if (psnr >= 0.0) |
| 684 | psnr_.AddSample(psnr); |
| 685 | if (ssim >= 0.0) |
| 686 | ssim_.AddSample(ssim); |
| 687 | |
| 688 | if (comparison.dropped) { |
| 689 | ++dropped_frames_; |
| 690 | return; |
| 691 | } |
| 692 | if (last_unfreeze_time_ms_ == 0) |
| 693 | last_unfreeze_time_ms_ = comparison.render_time_ms; |
| 694 | if (last_render_time_ != 0) { |
| 695 | const int64_t render_delta_ms = |
| 696 | comparison.render_time_ms - last_render_time_; |
| 697 | rendered_delta_.AddSample(render_delta_ms); |
| 698 | if (last_render_delta_ms_ != 0 && |
| 699 | render_delta_ms - last_render_delta_ms_ > 150) { |
| 700 | time_between_freezes_.AddSample(last_render_time_ - |
| 701 | last_unfreeze_time_ms_); |
| 702 | last_unfreeze_time_ms_ = comparison.render_time_ms; |
| 703 | } |
| 704 | last_render_delta_ms_ = render_delta_ms; |
| 705 | } |
| 706 | last_render_time_ = comparison.render_time_ms; |
| 707 | |
| 708 | sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms); |
| 709 | if (comparison.recv_time_ms > 0) { |
| 710 | // If recv_time_ms == 0, this frame consisted of a packets which were all |
| 711 | // lost in the transport. Since we were able to render the frame, however, |
| 712 | // the dropped packets were recovered by FlexFEC. The FlexFEC recovery |
| 713 | // happens internally in Call, and we can therefore here not know which |
| 714 | // FEC packets that protected the lost media packets. Consequently, we |
| 715 | // were not able to record a meaningful recv_time_ms. We therefore skip |
| 716 | // this sample. |
| 717 | // |
| 718 | // The reasoning above does not hold for ULPFEC and RTX, as for those |
| 719 | // strategies the timestamp of the received packets is set to the |
| 720 | // timestamp of the protected/retransmitted media packet. I.e., then |
| 721 | // recv_time_ms != 0, even though the media packets were lost. |
| 722 | receiver_time_.AddSample(comparison.render_time_ms - |
| 723 | comparison.recv_time_ms); |
| 724 | network_time_.AddSample(comparison.recv_time_ms - comparison.send_time_ms); |
| 725 | } |
| 726 | end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms); |
| 727 | encoded_frame_size_.AddSample(comparison.encoded_frame_size); |
| 728 | } |
| 729 | |
| 730 | void VideoAnalyzer::PrintResult(const char* result_type, |
| 731 | test::Statistics stats, |
| 732 | const char* unit) { |
| 733 | test::PrintResultMeanAndError(result_type, "", test_label_.c_str(), |
| 734 | stats.Mean(), stats.StandardDeviation(), unit, |
| 735 | false); |
| 736 | } |
| 737 | |
| 738 | void VideoAnalyzer::PrintSamplesToFile() { |
| 739 | FILE* out = graph_data_output_file_; |
| 740 | rtc::CritScope crit(&comparison_lock_); |
| 741 | std::sort(samples_.begin(), samples_.end(), |
| 742 | [](const Sample& A, const Sample& B) -> bool { |
| 743 | return A.input_time_ms < B.input_time_ms; |
| 744 | }); |
| 745 | |
| 746 | fprintf(out, "%s\n", graph_title_.c_str()); |
| 747 | fprintf(out, "%" PRIuS "\n", samples_.size()); |
| 748 | fprintf(out, |
| 749 | "dropped " |
| 750 | "input_time_ms " |
| 751 | "send_time_ms " |
| 752 | "recv_time_ms " |
| 753 | "render_time_ms " |
| 754 | "encoded_frame_size " |
| 755 | "psnr " |
| 756 | "ssim " |
| 757 | "encode_time_ms\n"); |
| 758 | for (const Sample& sample : samples_) { |
| 759 | fprintf(out, |
| 760 | "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS |
| 761 | " %lf %lf\n", |
| 762 | sample.dropped, sample.input_time_ms, sample.send_time_ms, |
| 763 | sample.recv_time_ms, sample.render_time_ms, |
| 764 | sample.encoded_frame_size, sample.psnr, sample.ssim); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | double VideoAnalyzer::GetAverageMediaBitrateBps() { |
| 769 | if (last_sending_time_ == first_sending_time_) { |
| 770 | return 0; |
| 771 | } else { |
| 772 | return static_cast<double>(total_media_bytes_) * 8 / |
| 773 | (last_sending_time_ - first_sending_time_) * |
| 774 | rtc::kNumMillisecsPerSec; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | void VideoAnalyzer::AddCapturedFrameForComparison( |
| 779 | const VideoFrame& video_frame) { |
| 780 | rtc::CritScope lock(&crit_); |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 781 | if (captured_frames_ < frames_to_process_) { |
| 782 | ++captured_frames_; |
| 783 | frames_.push_back(video_frame); |
| 784 | } |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | void VideoAnalyzer::AddFrameComparison(const VideoFrame& reference, |
| 788 | const VideoFrame& render, |
| 789 | bool dropped, |
| 790 | int64_t render_time_ms) { |
| 791 | int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp()); |
| 792 | int64_t send_time_ms = send_times_[reference_timestamp]; |
| 793 | send_times_.erase(reference_timestamp); |
| 794 | int64_t recv_time_ms = recv_times_[reference_timestamp]; |
| 795 | recv_times_.erase(reference_timestamp); |
| 796 | |
| 797 | // TODO(ivica): Make this work for > 2 streams. |
| 798 | auto it = encoded_frame_sizes_.find(reference_timestamp); |
| 799 | if (it == encoded_frame_sizes_.end()) |
| 800 | it = encoded_frame_sizes_.find(reference_timestamp - 1); |
| 801 | size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second; |
| 802 | if (it != encoded_frame_sizes_.end()) |
| 803 | encoded_frame_sizes_.erase(it); |
| 804 | |
| 805 | rtc::CritScope crit(&comparison_lock_); |
| 806 | if (comparisons_.size() < kMaxComparisons) { |
| 807 | comparisons_.push_back(FrameComparison( |
| 808 | reference, render, dropped, reference.ntp_time_ms(), send_time_ms, |
| 809 | recv_time_ms, render_time_ms, encoded_size)); |
| 810 | } else { |
| 811 | comparisons_.push_back(FrameComparison(dropped, reference.ntp_time_ms(), |
| 812 | send_time_ms, recv_time_ms, |
| 813 | render_time_ms, encoded_size)); |
| 814 | } |
| 815 | comparison_available_event_.Set(); |
| 816 | } |
| 817 | |
| 818 | VideoAnalyzer::FrameComparison::FrameComparison() |
| 819 | : dropped(false), |
| 820 | input_time_ms(0), |
| 821 | send_time_ms(0), |
| 822 | recv_time_ms(0), |
| 823 | render_time_ms(0), |
| 824 | encoded_frame_size(0) {} |
| 825 | |
| 826 | VideoAnalyzer::FrameComparison::FrameComparison(const VideoFrame& reference, |
| 827 | const VideoFrame& render, |
| 828 | bool dropped, |
| 829 | int64_t input_time_ms, |
| 830 | int64_t send_time_ms, |
| 831 | int64_t recv_time_ms, |
| 832 | int64_t render_time_ms, |
| 833 | size_t encoded_frame_size) |
| 834 | : reference(reference), |
| 835 | render(render), |
| 836 | dropped(dropped), |
| 837 | input_time_ms(input_time_ms), |
| 838 | send_time_ms(send_time_ms), |
| 839 | recv_time_ms(recv_time_ms), |
| 840 | render_time_ms(render_time_ms), |
| 841 | encoded_frame_size(encoded_frame_size) {} |
| 842 | |
| 843 | VideoAnalyzer::FrameComparison::FrameComparison(bool dropped, |
| 844 | int64_t input_time_ms, |
| 845 | int64_t send_time_ms, |
| 846 | int64_t recv_time_ms, |
| 847 | int64_t render_time_ms, |
| 848 | size_t encoded_frame_size) |
| 849 | : dropped(dropped), |
| 850 | input_time_ms(input_time_ms), |
| 851 | send_time_ms(send_time_ms), |
| 852 | recv_time_ms(recv_time_ms), |
| 853 | render_time_ms(render_time_ms), |
| 854 | encoded_frame_size(encoded_frame_size) {} |
| 855 | |
| 856 | VideoAnalyzer::Sample::Sample(int dropped, |
| 857 | int64_t input_time_ms, |
| 858 | int64_t send_time_ms, |
| 859 | int64_t recv_time_ms, |
| 860 | int64_t render_time_ms, |
| 861 | size_t encoded_frame_size, |
| 862 | double psnr, |
| 863 | double ssim) |
| 864 | : dropped(dropped), |
| 865 | input_time_ms(input_time_ms), |
| 866 | send_time_ms(send_time_ms), |
| 867 | recv_time_ms(recv_time_ms), |
| 868 | render_time_ms(render_time_ms), |
| 869 | encoded_frame_size(encoded_frame_size), |
| 870 | psnr(psnr), |
| 871 | ssim(ssim) {} |
| 872 | |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 873 | VideoAnalyzer::CapturedFrameForwarder::CapturedFrameForwarder( |
| 874 | VideoAnalyzer* analyzer, |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 875 | Clock* clock, |
Ilya Nikolaevskiy | 85fc325 | 2019-02-11 10:41:50 +0100 | [diff] [blame^] | 876 | int frames_to_process) |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 877 | : analyzer_(analyzer), |
| 878 | send_stream_input_(nullptr), |
Niels Möller | 1c931c4 | 2018-12-18 16:08:11 +0100 | [diff] [blame] | 879 | video_source_(nullptr), |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 880 | clock_(clock), |
| 881 | captured_frames_(0), |
Ilya Nikolaevskiy | 85fc325 | 2019-02-11 10:41:50 +0100 | [diff] [blame^] | 882 | frames_to_process_(frames_to_process) {} |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 883 | |
| 884 | void VideoAnalyzer::CapturedFrameForwarder::SetSource( |
Niels Möller | 1c931c4 | 2018-12-18 16:08:11 +0100 | [diff] [blame] | 885 | VideoSourceInterface<VideoFrame>* video_source) { |
| 886 | video_source_ = video_source; |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | void VideoAnalyzer::CapturedFrameForwarder::OnFrame( |
| 890 | const VideoFrame& video_frame) { |
| 891 | VideoFrame copy = video_frame; |
| 892 | // Frames from the capturer does not have a rtp timestamp. |
| 893 | // Create one so it can be used for comparison. |
| 894 | RTC_DCHECK_EQ(0, video_frame.timestamp()); |
| 895 | if (video_frame.ntp_time_ms() == 0) |
| 896 | copy.set_ntp_time_ms(clock_->CurrentNtpInMilliseconds()); |
| 897 | copy.set_timestamp(copy.ntp_time_ms() * 90); |
| 898 | analyzer_->AddCapturedFrameForComparison(copy); |
| 899 | rtc::CritScope lock(&crit_); |
Ilya Nikolaevskiy | 6957abe | 2019-01-29 16:33:04 +0100 | [diff] [blame] | 900 | ++captured_frames_; |
Ilya Nikolaevskiy | 85fc325 | 2019-02-11 10:41:50 +0100 | [diff] [blame^] | 901 | if (send_stream_input_ && captured_frames_ <= frames_to_process_) |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 902 | send_stream_input_->OnFrame(copy); |
| 903 | } |
| 904 | |
| 905 | void VideoAnalyzer::CapturedFrameForwarder::AddOrUpdateSink( |
| 906 | rtc::VideoSinkInterface<VideoFrame>* sink, |
| 907 | const rtc::VideoSinkWants& wants) { |
| 908 | { |
| 909 | rtc::CritScope lock(&crit_); |
| 910 | RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink); |
| 911 | send_stream_input_ = sink; |
| 912 | } |
Niels Möller | 1c931c4 | 2018-12-18 16:08:11 +0100 | [diff] [blame] | 913 | if (video_source_) { |
| 914 | video_source_->AddOrUpdateSink(this, wants); |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | |
| 918 | void VideoAnalyzer::CapturedFrameForwarder::RemoveSink( |
| 919 | rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 920 | rtc::CritScope lock(&crit_); |
| 921 | RTC_DCHECK(sink == send_stream_input_); |
| 922 | send_stream_input_ = nullptr; |
Sebastian Jansson | d4c5d63 | 2018-07-10 12:57:37 +0200 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | } // namespace webrtc |