OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 #include <stdio.h> | 10 #include <stdio.h> |
(...skipping 25 matching lines...) Expand all Loading... |
36 | 36 |
37 namespace webrtc { | 37 namespace webrtc { |
38 | 38 |
39 static const int kSendStatsPollingIntervalMs = 1000; | 39 static const int kSendStatsPollingIntervalMs = 1000; |
40 static const int kPayloadTypeH264 = 122; | 40 static const int kPayloadTypeH264 = 122; |
41 static const int kPayloadTypeVP8 = 123; | 41 static const int kPayloadTypeVP8 = 123; |
42 static const int kPayloadTypeVP9 = 124; | 42 static const int kPayloadTypeVP9 = 124; |
43 | 43 |
44 class VideoAnalyzer : public PacketReceiver, | 44 class VideoAnalyzer : public PacketReceiver, |
45 public Transport, | 45 public Transport, |
46 public I420FrameCallback, | |
47 public VideoRenderer, | 46 public VideoRenderer, |
48 public VideoCaptureInput, | 47 public VideoCaptureInput, |
49 public EncodedFrameObserver { | 48 public EncodedFrameObserver { |
50 public: | 49 public: |
51 VideoAnalyzer(test::LayerFilteringTransport* transport, | 50 VideoAnalyzer(test::LayerFilteringTransport* transport, |
52 const std::string& test_label, | 51 const std::string& test_label, |
53 double avg_psnr_threshold, | 52 double avg_psnr_threshold, |
54 double avg_ssim_threshold, | 53 double avg_ssim_threshold, |
55 int duration_frames, | 54 int duration_frames, |
56 FILE* graph_data_output_file, | 55 FILE* graph_data_output_file, |
57 const std::string& graph_title, | 56 const std::string& graph_title, |
58 uint32_t ssrc_to_analyze) | 57 uint32_t ssrc_to_analyze) |
59 : input_(nullptr), | 58 : input_(nullptr), |
60 transport_(transport), | 59 transport_(transport), |
61 receiver_(nullptr), | 60 receiver_(nullptr), |
62 send_stream_(nullptr), | 61 send_stream_(nullptr), |
63 test_label_(test_label), | 62 test_label_(test_label), |
64 graph_data_output_file_(graph_data_output_file), | 63 graph_data_output_file_(graph_data_output_file), |
65 graph_title_(graph_title), | 64 graph_title_(graph_title), |
66 ssrc_to_analyze_(ssrc_to_analyze), | 65 ssrc_to_analyze_(ssrc_to_analyze), |
67 encode_timing_proxy_(this), | 66 encode_timing_proxy_(this), |
68 frames_to_process_(duration_frames), | 67 frames_to_process_(duration_frames), |
69 frames_recorded_(0), | 68 frames_recorded_(0), |
70 frames_processed_(0), | 69 frames_processed_(0), |
71 dropped_frames_(0), | 70 dropped_frames_(0), |
72 dropped_frames_before_first_encode_(0), | |
73 dropped_frames_before_rendering_(0), | |
74 last_render_time_(0), | 71 last_render_time_(0), |
75 rtp_timestamp_delta_(0), | 72 rtp_timestamp_delta_(0), |
76 avg_psnr_threshold_(avg_psnr_threshold), | 73 avg_psnr_threshold_(avg_psnr_threshold), |
77 avg_ssim_threshold_(avg_ssim_threshold), | 74 avg_ssim_threshold_(avg_ssim_threshold), |
78 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"), | 75 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"), |
79 comparison_available_event_(false, false), | 76 comparison_available_event_(false, false), |
80 done_(true, false) { | 77 done_(true, false) { |
81 // Create thread pool for CPU-expensive PSNR/SSIM calculations. | 78 // Create thread pool for CPU-expensive PSNR/SSIM calculations. |
82 | 79 |
83 // Try to use about as many threads as cores, but leave kMinCoresLeft alone, | 80 // Try to use about as many threads as cores, but leave kMinCoresLeft alone, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 } | 130 } |
134 | 131 |
135 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) { | 132 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) { |
136 rtc::CritScope crit(&comparison_lock_); | 133 rtc::CritScope crit(&comparison_lock_); |
137 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms; | 134 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms; |
138 } | 135 } |
139 | 136 |
140 void IncomingCapturedFrame(const VideoFrame& video_frame) override { | 137 void IncomingCapturedFrame(const VideoFrame& video_frame) override { |
141 VideoFrame copy = video_frame; | 138 VideoFrame copy = video_frame; |
142 copy.set_timestamp(copy.ntp_time_ms() * 90); | 139 copy.set_timestamp(copy.ntp_time_ms() * 90); |
| 140 |
143 { | 141 { |
144 rtc::CritScope lock(&crit_); | 142 rtc::CritScope lock(&crit_); |
| 143 if (first_send_frame_.IsZeroSize() && rtp_timestamp_delta_ == 0) |
| 144 first_send_frame_ = copy; |
| 145 |
145 frames_.push_back(copy); | 146 frames_.push_back(copy); |
146 } | 147 } |
147 | 148 |
148 input_->IncomingCapturedFrame(video_frame); | 149 input_->IncomingCapturedFrame(video_frame); |
149 } | 150 } |
150 | 151 |
151 void FrameCallback(VideoFrame* video_frame) { | |
152 rtc::CritScope lock(&crit_); | |
153 if (first_send_frame_.IsZeroSize() && rtp_timestamp_delta_ == 0) { | |
154 while (frames_.front().timestamp() != video_frame->timestamp()) { | |
155 ++dropped_frames_before_first_encode_; | |
156 frames_.pop_front(); | |
157 RTC_CHECK(!frames_.empty()); | |
158 } | |
159 first_send_frame_ = *video_frame; | |
160 } | |
161 } | |
162 | |
163 bool SendRtp(const uint8_t* packet, | 152 bool SendRtp(const uint8_t* packet, |
164 size_t length, | 153 size_t length, |
165 const PacketOptions& options) override { | 154 const PacketOptions& options) override { |
166 RtpUtility::RtpHeaderParser parser(packet, length); | 155 RtpUtility::RtpHeaderParser parser(packet, length); |
167 RTPHeader header; | 156 RTPHeader header; |
168 parser.Parse(&header); | 157 parser.Parse(&header); |
169 | 158 |
170 int64_t current_time = | 159 int64_t current_time = |
171 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds(); | 160 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds(); |
172 bool result = transport_->SendRtp(packet, length, options); | 161 bool result = transport_->SendRtp(packet, length, options); |
173 { | 162 { |
174 rtc::CritScope lock(&crit_); | 163 rtc::CritScope lock(&crit_); |
175 | 164 |
176 if (!first_send_frame_.IsZeroSize()) { | 165 if (rtp_timestamp_delta_ == 0) { |
177 rtp_timestamp_delta_ = header.timestamp - first_send_frame_.timestamp(); | 166 rtp_timestamp_delta_ = header.timestamp - first_send_frame_.timestamp(); |
178 first_send_frame_.Reset(); | 167 first_send_frame_.Reset(); |
179 } | 168 } |
180 int64_t timestamp = | 169 int64_t timestamp = |
181 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_); | 170 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_); |
182 send_times_[timestamp] = current_time; | 171 send_times_[timestamp] = current_time; |
183 if (!transport_->DiscardedLastPacket() && | 172 if (!transport_->DiscardedLastPacket() && |
184 header.ssrc == ssrc_to_analyze_) { | 173 header.ssrc == ssrc_to_analyze_) { |
185 encoded_frame_sizes_[timestamp] += | 174 encoded_frame_sizes_[timestamp] += |
186 length - (header.headerLength + header.paddingLength); | 175 length - (header.headerLength + header.paddingLength); |
(...skipping 15 matching lines...) Expand all Loading... |
202 void RenderFrame(const VideoFrame& video_frame, | 191 void RenderFrame(const VideoFrame& video_frame, |
203 int time_to_render_ms) override { | 192 int time_to_render_ms) override { |
204 int64_t render_time_ms = | 193 int64_t render_time_ms = |
205 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds(); | 194 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds(); |
206 | 195 |
207 rtc::CritScope lock(&crit_); | 196 rtc::CritScope lock(&crit_); |
208 uint32_t send_timestamp = | 197 uint32_t send_timestamp = |
209 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_); | 198 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_); |
210 | 199 |
211 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) { | 200 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) { |
212 if (last_rendered_frame_.IsZeroSize()) { | |
213 // No previous frame rendered, this one was dropped after sending but | |
214 // before rendering. | |
215 ++dropped_frames_before_rendering_; | |
216 frames_.pop_front(); | |
217 RTC_CHECK(!frames_.empty()); | |
218 continue; | |
219 } | |
220 AddFrameComparison(frames_.front(), last_rendered_frame_, true, | 201 AddFrameComparison(frames_.front(), last_rendered_frame_, true, |
221 render_time_ms); | 202 render_time_ms); |
222 frames_.pop_front(); | 203 frames_.pop_front(); |
223 RTC_DCHECK(!frames_.empty()); | |
224 } | 204 } |
225 | 205 |
226 VideoFrame reference_frame = frames_.front(); | 206 VideoFrame reference_frame = frames_.front(); |
227 frames_.pop_front(); | 207 frames_.pop_front(); |
228 assert(!reference_frame.IsZeroSize()); | 208 assert(!reference_frame.IsZeroSize()); |
229 int64_t reference_timestamp = | 209 int64_t reference_timestamp = |
230 wrap_handler_.Unwrap(reference_frame.timestamp()); | 210 wrap_handler_.Unwrap(reference_frame.timestamp()); |
231 if (send_timestamp == reference_timestamp - 1) { | 211 if (send_timestamp == reference_timestamp - 1) { |
232 // TODO(ivica): Make this work for > 2 streams. | 212 // TODO(ivica): Make this work for > 2 streams. |
233 // Look at RTPSender::BuildRTPHeader. | 213 // Look at RTPSender::BuildRTPHeader. |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 | 345 |
366 private: | 346 private: |
367 VideoAnalyzer* const parent_; | 347 VideoAnalyzer* const parent_; |
368 }; | 348 }; |
369 | 349 |
370 void AddFrameComparison(const VideoFrame& reference, | 350 void AddFrameComparison(const VideoFrame& reference, |
371 const VideoFrame& render, | 351 const VideoFrame& render, |
372 bool dropped, | 352 bool dropped, |
373 int64_t render_time_ms) | 353 int64_t render_time_ms) |
374 EXCLUSIVE_LOCKS_REQUIRED(crit_) { | 354 EXCLUSIVE_LOCKS_REQUIRED(crit_) { |
375 RTC_DCHECK(!render.IsZeroSize()); | |
376 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp()); | 355 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp()); |
377 int64_t send_time_ms = send_times_[reference_timestamp]; | 356 int64_t send_time_ms = send_times_[reference_timestamp]; |
378 send_times_.erase(reference_timestamp); | 357 send_times_.erase(reference_timestamp); |
379 int64_t recv_time_ms = recv_times_[reference_timestamp]; | 358 int64_t recv_time_ms = recv_times_[reference_timestamp]; |
380 recv_times_.erase(reference_timestamp); | 359 recv_times_.erase(reference_timestamp); |
381 | 360 |
382 // TODO(ivica): Make this work for > 2 streams. | 361 // TODO(ivica): Make this work for > 2 streams. |
383 auto it = encoded_frame_sizes_.find(reference_timestamp); | 362 auto it = encoded_frame_sizes_.find(reference_timestamp); |
384 if (it == encoded_frame_sizes_.end()) | 363 if (it == encoded_frame_sizes_.end()) |
385 it = encoded_frame_sizes_.find(reference_timestamp - 1); | 364 it = encoded_frame_sizes_.find(reference_timestamp - 1); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 ++frames_processed_; | 477 ++frames_processed_; |
499 assert(frames_processed_ <= frames_to_process_); | 478 assert(frames_processed_ <= frames_to_process_); |
500 return frames_processed_ == frames_to_process_; | 479 return frames_processed_ == frames_to_process_; |
501 } | 480 } |
502 | 481 |
503 void PrintResults() { | 482 void PrintResults() { |
504 rtc::CritScope crit(&comparison_lock_); | 483 rtc::CritScope crit(&comparison_lock_); |
505 PrintResult("psnr", psnr_, " dB"); | 484 PrintResult("psnr", psnr_, " dB"); |
506 PrintResult("ssim", ssim_, " score"); | 485 PrintResult("ssim", ssim_, " score"); |
507 PrintResult("sender_time", sender_time_, " ms"); | 486 PrintResult("sender_time", sender_time_, " ms"); |
| 487 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(), |
| 488 dropped_frames_); |
508 PrintResult("receiver_time", receiver_time_, " ms"); | 489 PrintResult("receiver_time", receiver_time_, " ms"); |
509 PrintResult("total_delay_incl_network", end_to_end_, " ms"); | 490 PrintResult("total_delay_incl_network", end_to_end_, " ms"); |
510 PrintResult("time_between_rendered_frames", rendered_delta_, " ms"); | 491 PrintResult("time_between_rendered_frames", rendered_delta_, " ms"); |
511 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes"); | 492 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes"); |
512 PrintResult("encode_frame_rate", encode_frame_rate_, " fps"); | 493 PrintResult("encode_frame_rate", encode_frame_rate_, " fps"); |
513 PrintResult("encode_time", encode_time_ms, " ms"); | 494 PrintResult("encode_time", encode_time_ms, " ms"); |
514 PrintResult("encode_usage_percent", encode_usage_percent, " percent"); | 495 PrintResult("encode_usage_percent", encode_usage_percent, " percent"); |
515 PrintResult("media_bitrate", media_bitrate_bps, " bps"); | 496 PrintResult("media_bitrate", media_bitrate_bps, " bps"); |
516 | 497 |
517 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(), | |
518 dropped_frames_); | |
519 printf("RESULT dropped_frames_before_first_encode: %s = %d frames\n", | |
520 test_label_.c_str(), dropped_frames_before_first_encode_); | |
521 printf("RESULT dropped_frames_before_rendering: %s = %d frames\n", | |
522 test_label_.c_str(), dropped_frames_before_rendering_); | |
523 | |
524 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_); | 498 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_); |
525 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_); | 499 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_); |
526 } | 500 } |
527 | 501 |
528 void PerformFrameComparison(const FrameComparison& comparison) { | 502 void PerformFrameComparison(const FrameComparison& comparison) { |
529 // Perform expensive psnr and ssim calculations while not holding lock. | 503 // Perform expensive psnr and ssim calculations while not holding lock. |
530 double psnr = I420PSNR(&comparison.reference, &comparison.render); | 504 double psnr = I420PSNR(&comparison.reference, &comparison.render); |
531 double ssim = I420SSIM(&comparison.reference, &comparison.render); | 505 double ssim = I420SSIM(&comparison.reference, &comparison.render); |
532 | 506 |
533 int64_t input_time_ms = comparison.reference.ntp_time_ms(); | 507 int64_t input_time_ms = comparison.reference.ntp_time_ms(); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
628 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_); | 602 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_); |
629 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_); | 603 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_); |
630 test::Statistics encode_time_ms GUARDED_BY(comparison_lock_); | 604 test::Statistics encode_time_ms GUARDED_BY(comparison_lock_); |
631 test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_); | 605 test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_); |
632 test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_); | 606 test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_); |
633 | 607 |
634 const int frames_to_process_; | 608 const int frames_to_process_; |
635 int frames_recorded_; | 609 int frames_recorded_; |
636 int frames_processed_; | 610 int frames_processed_; |
637 int dropped_frames_; | 611 int dropped_frames_; |
638 int dropped_frames_before_first_encode_; | |
639 int dropped_frames_before_rendering_; | |
640 int64_t last_render_time_; | 612 int64_t last_render_time_; |
641 uint32_t rtp_timestamp_delta_; | 613 uint32_t rtp_timestamp_delta_; |
642 | 614 |
643 rtc::CriticalSection crit_; | 615 rtc::CriticalSection crit_; |
644 std::deque<VideoFrame> frames_ GUARDED_BY(crit_); | 616 std::deque<VideoFrame> frames_ GUARDED_BY(crit_); |
645 VideoFrame last_rendered_frame_ GUARDED_BY(crit_); | 617 VideoFrame last_rendered_frame_ GUARDED_BY(crit_); |
646 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_); | 618 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_); |
647 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_); | 619 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_); |
648 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_); | 620 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_); |
649 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_); | 621 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_); |
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1023 params_.analyzer.test_durations_secs * params_.common.fps, | 995 params_.analyzer.test_durations_secs * params_.common.fps, |
1024 graph_data_output_file, graph_title, | 996 graph_data_output_file, graph_title, |
1025 kVideoSendSsrcs[params_.ss.selected_stream]); | 997 kVideoSendSsrcs[params_.ss.selected_stream]); |
1026 | 998 |
1027 analyzer.SetReceiver(receiver_call_->Receiver()); | 999 analyzer.SetReceiver(receiver_call_->Receiver()); |
1028 send_transport.SetReceiver(&analyzer); | 1000 send_transport.SetReceiver(&analyzer); |
1029 recv_transport.SetReceiver(sender_call_->Receiver()); | 1001 recv_transport.SetReceiver(sender_call_->Receiver()); |
1030 | 1002 |
1031 SetupCommon(&analyzer, &recv_transport); | 1003 SetupCommon(&analyzer, &recv_transport); |
1032 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer; | 1004 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer; |
1033 video_send_config_.pre_encode_callback = &analyzer; | |
1034 for (auto& config : video_receive_configs_) | 1005 for (auto& config : video_receive_configs_) |
1035 config.pre_decode_callback = &analyzer; | 1006 config.pre_decode_callback = &analyzer; |
1036 RTC_DCHECK(!video_send_config_.post_encode_callback); | 1007 RTC_DCHECK(!video_send_config_.post_encode_callback); |
1037 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy(); | 1008 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy(); |
1038 | 1009 |
1039 if (params_.screenshare.enabled) | 1010 if (params_.screenshare.enabled) |
1040 SetupScreenshare(); | 1011 SetupScreenshare(); |
1041 | 1012 |
1042 CreateVideoStreams(); | 1013 CreateVideoStreams(); |
1043 analyzer.input_ = video_send_stream_->Input(); | 1014 analyzer.input_ = video_send_stream_->Input(); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1124 video_send_stream_->Stop(); | 1095 video_send_stream_->Stop(); |
1125 receive_stream->Stop(); | 1096 receive_stream->Stop(); |
1126 | 1097 |
1127 call->DestroyVideoReceiveStream(receive_stream); | 1098 call->DestroyVideoReceiveStream(receive_stream); |
1128 call->DestroyVideoSendStream(video_send_stream_); | 1099 call->DestroyVideoSendStream(video_send_stream_); |
1129 | 1100 |
1130 transport.StopSending(); | 1101 transport.StopSending(); |
1131 } | 1102 } |
1132 | 1103 |
1133 } // namespace webrtc | 1104 } // namespace webrtc |
OLD | NEW |