blob: 7efbaf3b60eb8380c16a2adf46a8326cc9fae046 [file] [log] [blame]
Sebastian Janssond4c5d632018-07-10 12:57:37 +02001/*
2 * Copyright 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10#include "video/video_analyzer.h"
11
12#include <algorithm>
13#include <utility>
14
Steve Antonbd631a02019-03-28 10:51:27 -070015#include "absl/algorithm/container.h"
Niels Möller1c931c42018-12-18 16:08:11 +010016#include "common_video/libyuv/include/webrtc_libyuv.h"
Sebastian Janssond4c5d632018-07-10 12:57:37 +020017#include "modules/rtp_rtcp/source/rtp_format.h"
18#include "modules/rtp_rtcp/source/rtp_utility.h"
19#include "rtc_base/cpu_time.h"
20#include "rtc_base/flags.h"
21#include "rtc_base/format_macros.h"
22#include "rtc_base/memory_usage.h"
Sebastian Janssond4c5d632018-07-10 12:57:37 +020023#include "system_wrappers/include/cpu_info.h"
24#include "test/call_test.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "test/testsupport/file_utils.h"
Sebastian Janssond4c5d632018-07-10 12:57:37 +020026#include "test/testsupport/frame_writer.h"
27#include "test/testsupport/perf_test.h"
28#include "test/testsupport/test_artifacts.h"
29
Mirko Bonadei2dfa9982018-10-18 11:35:32 +020030WEBRTC_DEFINE_bool(
31 save_worst_frame,
32 false,
33 "Enable saving a frame with the lowest PSNR to a jpeg file in the "
34 "test_artifacts_dir");
Sebastian Janssond4c5d632018-07-10 12:57:37 +020035
36namespace webrtc {
37namespace {
38constexpr int kSendStatsPollingIntervalMs = 1000;
39constexpr size_t kMaxComparisons = 10;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +010040// How often is keep alive message printed.
41constexpr int kKeepAliveIntervalSeconds = 30;
42// Interval between checking that the test is over.
43constexpr int kProbingIntervalMs = 500;
44constexpr int kKeepAliveIntervalIterations =
45 kKeepAliveIntervalSeconds * 1000 / kProbingIntervalMs;
Sebastian Janssond4c5d632018-07-10 12:57:37 +020046
47bool IsFlexfec(int payload_type) {
48 return payload_type == test::CallTest::kFlexfecPayloadType;
49}
50} // namespace
51
Artem Titovff7730d2019-04-02 13:46:53 +020052VideoAnalyzer::VideoAnalyzer(
53 test::LayerFilteringTransport* transport,
54 const std::string& test_label,
55 double avg_psnr_threshold,
56 double avg_ssim_threshold,
57 int duration_frames,
58 FILE* graph_data_output_file,
59 const std::string& graph_title,
60 uint32_t ssrc_to_analyze,
61 uint32_t rtx_ssrc_to_analyze,
62 size_t selected_stream,
63 int selected_sl,
64 int selected_tl,
65 bool is_quick_test_enabled,
66 Clock* clock,
67 std::string rtp_dump_name,
68 test::SingleThreadedTaskQueueForTesting* task_queue)
Sebastian Janssond4c5d632018-07-10 12:57:37 +020069 : transport_(transport),
70 receiver_(nullptr),
71 call_(nullptr),
72 send_stream_(nullptr),
73 receive_stream_(nullptr),
Christoffer Rodbroc2a02882018-08-07 14:10:56 +020074 audio_receive_stream_(nullptr),
Ilya Nikolaevskiy85fc3252019-02-11 10:41:50 +010075 captured_frame_forwarder_(this, clock, duration_frames),
Sebastian Janssond4c5d632018-07-10 12:57:37 +020076 test_label_(test_label),
77 graph_data_output_file_(graph_data_output_file),
78 graph_title_(graph_title),
79 ssrc_to_analyze_(ssrc_to_analyze),
80 rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze),
81 selected_stream_(selected_stream),
82 selected_sl_(selected_sl),
83 selected_tl_(selected_tl),
Elad Alon8c513c72019-05-07 21:22:24 +020084 freeze_count_(0),
85 total_freezes_duration_ms_(0),
86 total_frames_duration_ms_(0),
87 sum_squared_frame_durations_(0),
Elad Alon58e06572019-05-08 15:34:24 +020088 decode_frame_rate_(0),
89 render_frame_rate_(0),
Sebastian Janssond4c5d632018-07-10 12:57:37 +020090 last_fec_bytes_(0),
91 frames_to_process_(duration_frames),
92 frames_recorded_(0),
93 frames_processed_(0),
94 dropped_frames_(0),
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +010095 captured_frames_(0),
Sebastian Janssond4c5d632018-07-10 12:57:37 +020096 dropped_frames_before_first_encode_(0),
97 dropped_frames_before_rendering_(0),
98 last_render_time_(0),
99 last_render_delta_ms_(0),
100 last_unfreeze_time_ms_(0),
101 rtp_timestamp_delta_(0),
102 total_media_bytes_(0),
103 first_sending_time_(0),
104 last_sending_time_(0),
105 cpu_time_(0),
106 wallclock_time_(0),
107 avg_psnr_threshold_(avg_psnr_threshold),
108 avg_ssim_threshold_(avg_ssim_threshold),
109 is_quick_test_enabled_(is_quick_test_enabled),
Niels Möller4731f002019-05-03 09:34:24 +0200110 quit_(false),
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200111 done_(true, false),
112 clock_(clock),
Artem Titovff7730d2019-04-02 13:46:53 +0200113 start_ms_(clock->TimeInMilliseconds()),
114 task_queue_(task_queue) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200115 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
116
117 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
118 // so that we don't accidentally starve "real" worker threads (codec etc).
119 // Also, don't allocate more than kMaxComparisonThreads, even if there are
120 // spare cores.
121
122 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
123 RTC_DCHECK_GE(num_cores, 1);
124 static const uint32_t kMinCoresLeft = 4;
125 static const uint32_t kMaxComparisonThreads = 8;
126
127 if (num_cores <= kMinCoresLeft) {
128 num_cores = 1;
129 } else {
130 num_cores -= kMinCoresLeft;
131 num_cores = std::min(num_cores, kMaxComparisonThreads);
132 }
133
134 for (uint32_t i = 0; i < num_cores; ++i) {
135 rtc::PlatformThread* thread =
136 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
137 thread->Start();
138 comparison_thread_pool_.push_back(thread);
139 }
140
141 if (!rtp_dump_name.empty()) {
142 fprintf(stdout, "Writing rtp dump to %s\n", rtp_dump_name.c_str());
143 rtp_file_writer_.reset(test::RtpFileWriter::Create(
144 test::RtpFileWriter::kRtpDump, rtp_dump_name));
145 }
146}
147
148VideoAnalyzer::~VideoAnalyzer() {
Niels Möller4731f002019-05-03 09:34:24 +0200149 {
150 rtc::CritScope crit(&comparison_lock_);
151 quit_ = true;
152 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200153 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
154 thread->Stop();
155 delete thread;
156 }
157}
158
159void VideoAnalyzer::SetReceiver(PacketReceiver* receiver) {
160 receiver_ = receiver;
161}
162
Niels Möller1c931c42018-12-18 16:08:11 +0100163void VideoAnalyzer::SetSource(
164 rtc::VideoSourceInterface<VideoFrame>* video_source,
165 bool respect_sink_wants) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200166 if (respect_sink_wants)
Niels Möller1c931c42018-12-18 16:08:11 +0100167 captured_frame_forwarder_.SetSource(video_source);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200168 rtc::VideoSinkWants wants;
Niels Möller1c931c42018-12-18 16:08:11 +0100169 video_source->AddOrUpdateSink(InputInterface(), wants);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200170}
171
172void VideoAnalyzer::SetCall(Call* call) {
173 rtc::CritScope lock(&crit_);
174 RTC_DCHECK(!call_);
175 call_ = call;
176}
177
178void VideoAnalyzer::SetSendStream(VideoSendStream* stream) {
179 rtc::CritScope lock(&crit_);
180 RTC_DCHECK(!send_stream_);
181 send_stream_ = stream;
182}
183
184void VideoAnalyzer::SetReceiveStream(VideoReceiveStream* stream) {
185 rtc::CritScope lock(&crit_);
186 RTC_DCHECK(!receive_stream_);
187 receive_stream_ = stream;
188}
189
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200190void VideoAnalyzer::SetAudioReceiveStream(AudioReceiveStream* recv_stream) {
191 rtc::CritScope lock(&crit_);
192 RTC_CHECK(!audio_receive_stream_);
193 audio_receive_stream_ = recv_stream;
194}
195
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200196rtc::VideoSinkInterface<VideoFrame>* VideoAnalyzer::InputInterface() {
197 return &captured_frame_forwarder_;
198}
199
200rtc::VideoSourceInterface<VideoFrame>* VideoAnalyzer::OutputInterface() {
201 return &captured_frame_forwarder_;
202}
203
204PacketReceiver::DeliveryStatus VideoAnalyzer::DeliverPacket(
205 MediaType media_type,
206 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200207 int64_t packet_time_us) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200208 // Ignore timestamps of RTCP packets. They're not synchronized with
209 // RTP packet timestamps and so they would confuse wrap_handler_.
210 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size())) {
Niels Möller70082872018-08-07 11:03:12 +0200211 return receiver_->DeliverPacket(media_type, std::move(packet),
212 packet_time_us);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200213 }
214
215 if (rtp_file_writer_) {
216 test::RtpPacket p;
217 memcpy(p.data, packet.cdata(), packet.size());
218 p.length = packet.size();
219 p.original_length = packet.size();
220 p.time_ms = clock_->TimeInMilliseconds() - start_ms_;
221 rtp_file_writer_->WritePacket(&p);
222 }
223
224 RtpUtility::RtpHeaderParser parser(packet.cdata(), packet.size());
225 RTPHeader header;
226 parser.Parse(&header);
227 if (!IsFlexfec(header.payloadType) && (header.ssrc == ssrc_to_analyze_ ||
228 header.ssrc == rtx_ssrc_to_analyze_)) {
229 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
230 // (FlexFEC and media are sent on different SSRCs, which have different
231 // timestamps spaces.)
232 // Also ignore packets from wrong SSRC, but include retransmits.
233 rtc::CritScope lock(&crit_);
234 int64_t timestamp =
235 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
Sebastian Jansson11c012a2019-03-29 14:17:26 +0100236 recv_times_[timestamp] = clock_->CurrentNtpInMilliseconds();
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200237 }
238
Niels Möller70082872018-08-07 11:03:12 +0200239 return receiver_->DeliverPacket(media_type, std::move(packet),
240 packet_time_us);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200241}
242
243void VideoAnalyzer::PreEncodeOnFrame(const VideoFrame& video_frame) {
244 rtc::CritScope lock(&crit_);
245 if (!first_encoded_timestamp_) {
246 while (frames_.front().timestamp() != video_frame.timestamp()) {
247 ++dropped_frames_before_first_encode_;
248 frames_.pop_front();
249 RTC_CHECK(!frames_.empty());
250 }
251 first_encoded_timestamp_ = video_frame.timestamp();
252 }
253}
254
Niels Möller88be9722018-10-10 10:58:52 +0200255void VideoAnalyzer::PostEncodeOnFrame(size_t stream_id, uint32_t timestamp) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200256 rtc::CritScope lock(&crit_);
Niels Möller88be9722018-10-10 10:58:52 +0200257 if (!first_sent_timestamp_ && stream_id == selected_stream_) {
258 first_sent_timestamp_ = timestamp;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200259 }
260}
261
262bool VideoAnalyzer::SendRtp(const uint8_t* packet,
263 size_t length,
264 const PacketOptions& options) {
265 RtpUtility::RtpHeaderParser parser(packet, length);
266 RTPHeader header;
267 parser.Parse(&header);
268
Sebastian Jansson11c012a2019-03-29 14:17:26 +0100269 int64_t current_time = clock_->CurrentNtpInMilliseconds();
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200270
271 bool result = transport_->SendRtp(packet, length, options);
272 {
273 rtc::CritScope lock(&crit_);
274 if (rtp_timestamp_delta_ == 0 && header.ssrc == ssrc_to_analyze_) {
275 RTC_CHECK(static_cast<bool>(first_sent_timestamp_));
276 rtp_timestamp_delta_ = header.timestamp - *first_sent_timestamp_;
277 }
278
279 if (!IsFlexfec(header.payloadType) && header.ssrc == ssrc_to_analyze_) {
280 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
281 // (FlexFEC and media are sent on different SSRCs, which have different
282 // timestamps spaces.)
283 // Also ignore packets from wrong SSRC and retransmits.
284 int64_t timestamp =
285 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
286 send_times_[timestamp] = current_time;
287
288 if (IsInSelectedSpatialAndTemporalLayer(packet, length, header)) {
289 encoded_frame_sizes_[timestamp] +=
290 length - (header.headerLength + header.paddingLength);
291 total_media_bytes_ +=
292 length - (header.headerLength + header.paddingLength);
293 }
294 if (first_sending_time_ == 0)
295 first_sending_time_ = current_time;
296 last_sending_time_ = current_time;
297 }
298 }
299 return result;
300}
301
302bool VideoAnalyzer::SendRtcp(const uint8_t* packet, size_t length) {
303 return transport_->SendRtcp(packet, length);
304}
305
306void VideoAnalyzer::OnFrame(const VideoFrame& video_frame) {
Sebastian Jansson11c012a2019-03-29 14:17:26 +0100307 int64_t render_time_ms = clock_->CurrentNtpInMilliseconds();
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200308
309 rtc::CritScope lock(&crit_);
310
311 StartExcludingCpuThreadTime();
312
313 int64_t send_timestamp =
314 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
315
316 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
317 if (!last_rendered_frame_) {
318 // No previous frame rendered, this one was dropped after sending but
319 // before rendering.
320 ++dropped_frames_before_rendering_;
321 } else {
322 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
323 render_time_ms);
324 }
325 frames_.pop_front();
326 RTC_DCHECK(!frames_.empty());
327 }
328
329 VideoFrame reference_frame = frames_.front();
330 frames_.pop_front();
331 int64_t reference_timestamp =
332 wrap_handler_.Unwrap(reference_frame.timestamp());
333 if (send_timestamp == reference_timestamp - 1) {
334 // TODO(ivica): Make this work for > 2 streams.
335 // Look at RTPSender::BuildRTPHeader.
336 ++send_timestamp;
337 }
338 ASSERT_EQ(reference_timestamp, send_timestamp);
339
340 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
341
342 last_rendered_frame_ = video_frame;
343
344 StopExcludingCpuThreadTime();
345}
346
347void VideoAnalyzer::Wait() {
348 // Frame comparisons can be very expensive. Wait for test to be done, but
349 // at time-out check if frames_processed is going up. If so, give it more
350 // time, otherwise fail. Hopefully this will reduce test flakiness.
351
Artem Titovff7730d2019-04-02 13:46:53 +0200352 {
353 rtc::CritScope lock(&comparison_lock_);
354 stop_stats_poller_ = false;
355 stats_polling_task_id_ = task_queue_->PostDelayedTask(
356 [this]() { PollStats(); }, kSendStatsPollingIntervalMs);
357 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200358
359 int last_frames_processed = -1;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100360 int last_frames_captured = -1;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200361 int iteration = 0;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100362
363 while (!done_.Wait(kProbingIntervalMs)) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200364 int frames_processed;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100365 int frames_captured;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200366 {
367 rtc::CritScope crit(&comparison_lock_);
368 frames_processed = frames_processed_;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100369 frames_captured = captured_frames_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200370 }
371
372 // Print some output so test infrastructure won't think we've crashed.
373 const char* kKeepAliveMessages[3] = {
374 "Uh, I'm-I'm not quite dead, sir.",
375 "Uh, I-I think uh, I could pull through, sir.",
376 "Actually, I think I'm all right to come with you--"};
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100377 if (++iteration % kKeepAliveIntervalIterations == 0) {
378 printf("- %s\n", kKeepAliveMessages[iteration % 3]);
379 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200380
381 if (last_frames_processed == -1) {
382 last_frames_processed = frames_processed;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100383 last_frames_captured = frames_captured;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200384 continue;
385 }
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100386 if (frames_processed == last_frames_processed &&
387 last_frames_captured == frames_captured) {
388 if (frames_captured < frames_to_process_) {
389 EXPECT_GT(frames_processed, last_frames_processed)
390 << "Analyzer stalled while waiting for test to finish.";
391 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200392 done_.Set();
393 break;
394 }
395 last_frames_processed = frames_processed;
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100396 last_frames_captured = frames_captured;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200397 }
398
399 if (iteration > 0)
400 printf("- Farewell, sweet Concorde!\n");
401
Artem Titovff7730d2019-04-02 13:46:53 +0200402 {
403 rtc::CritScope lock(&comparison_lock_);
404 stop_stats_poller_ = true;
405 task_queue_->CancelTask(stats_polling_task_id_);
406 }
407
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100408 PrintResults();
409 if (graph_data_output_file_)
410 PrintSamplesToFile();
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200411}
412
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200413void VideoAnalyzer::StartMeasuringCpuProcessTime() {
414 rtc::CritScope lock(&cpu_measurement_lock_);
415 cpu_time_ -= rtc::GetProcessCpuTimeNanos();
416 wallclock_time_ -= rtc::SystemTimeNanos();
417}
418
419void VideoAnalyzer::StopMeasuringCpuProcessTime() {
420 rtc::CritScope lock(&cpu_measurement_lock_);
421 cpu_time_ += rtc::GetProcessCpuTimeNanos();
422 wallclock_time_ += rtc::SystemTimeNanos();
423}
424
425void VideoAnalyzer::StartExcludingCpuThreadTime() {
426 rtc::CritScope lock(&cpu_measurement_lock_);
427 cpu_time_ += rtc::GetThreadCpuTimeNanos();
428}
429
430void VideoAnalyzer::StopExcludingCpuThreadTime() {
431 rtc::CritScope lock(&cpu_measurement_lock_);
432 cpu_time_ -= rtc::GetThreadCpuTimeNanos();
433}
434
435double VideoAnalyzer::GetCpuUsagePercent() {
436 rtc::CritScope lock(&cpu_measurement_lock_);
437 return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
438}
439
440bool VideoAnalyzer::IsInSelectedSpatialAndTemporalLayer(
441 const uint8_t* packet,
442 size_t length,
443 const RTPHeader& header) {
444 if (header.payloadType != test::CallTest::kPayloadTypeVP9 &&
445 header.payloadType != test::CallTest::kPayloadTypeVP8) {
446 return true;
447 } else {
448 // Get VP8 and VP9 specific header to check layers indexes.
449 const uint8_t* payload = packet + header.headerLength;
450 const size_t payload_length = length - header.headerLength;
451 const size_t payload_data_length = payload_length - header.paddingLength;
452 const bool is_vp8 = header.payloadType == test::CallTest::kPayloadTypeVP8;
453 std::unique_ptr<RtpDepacketizer> depacketizer(
454 RtpDepacketizer::Create(is_vp8 ? kVideoCodecVP8 : kVideoCodecVP9));
455 RtpDepacketizer::ParsedPayload parsed_payload;
456 bool result =
457 depacketizer->Parse(&parsed_payload, payload, payload_data_length);
458 RTC_DCHECK(result);
philipel29d88462018-08-08 14:26:00 +0200459
460 int temporal_idx;
461 int spatial_idx;
462 if (is_vp8) {
Philip Eliassond52a1a62018-09-07 13:03:55 +0000463 temporal_idx = absl::get<RTPVideoHeaderVP8>(
464 parsed_payload.video_header().video_type_header)
465 .temporalIdx;
philipel29d88462018-08-08 14:26:00 +0200466 spatial_idx = kNoTemporalIdx;
467 } else {
468 const auto& vp9_header = absl::get<RTPVideoHeaderVP9>(
469 parsed_payload.video_header().video_type_header);
470 temporal_idx = vp9_header.temporal_idx;
471 spatial_idx = vp9_header.spatial_idx;
472 }
473
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200474 return (selected_tl_ < 0 || temporal_idx == kNoTemporalIdx ||
475 temporal_idx <= selected_tl_) &&
476 (selected_sl_ < 0 || spatial_idx == kNoSpatialIdx ||
477 spatial_idx <= selected_sl_);
478 }
479}
480
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200481void VideoAnalyzer::PollStats() {
Artem Titovff7730d2019-04-02 13:46:53 +0200482 rtc::CritScope crit(&comparison_lock_);
483 if (stop_stats_poller_) {
484 return;
Artem Titovf537da62019-04-02 13:46:53 +0200485 }
Artem Titovff7730d2019-04-02 13:46:53 +0200486
487 Call::Stats call_stats = call_->GetStats();
488 send_bandwidth_bps_.AddSample(call_stats.send_bandwidth_bps);
489
490 VideoSendStream::Stats send_stats = send_stream_->GetStats();
491 // It's not certain that we yet have estimates for any of these stats.
492 // Check that they are positive before mixing them in.
493 if (send_stats.encode_frame_rate > 0)
494 encode_frame_rate_.AddSample(send_stats.encode_frame_rate);
495 if (send_stats.avg_encode_time_ms > 0)
496 encode_time_ms_.AddSample(send_stats.avg_encode_time_ms);
497 if (send_stats.encode_usage_percent > 0)
498 encode_usage_percent_.AddSample(send_stats.encode_usage_percent);
499 if (send_stats.media_bitrate_bps > 0)
500 media_bitrate_bps_.AddSample(send_stats.media_bitrate_bps);
501 size_t fec_bytes = 0;
502 for (const auto& kv : send_stats.substreams) {
503 fec_bytes += kv.second.rtp_stats.fec.payload_bytes +
504 kv.second.rtp_stats.fec.padding_bytes;
505 }
506 fec_bitrate_bps_.AddSample((fec_bytes - last_fec_bytes_) * 8);
507 last_fec_bytes_ = fec_bytes;
508
509 if (receive_stream_ != nullptr) {
510 VideoReceiveStream::Stats receive_stats = receive_stream_->GetStats();
511 if (receive_stats.decode_ms > 0)
512 decode_time_ms_.AddSample(receive_stats.decode_ms);
513 if (receive_stats.max_decode_ms > 0)
514 decode_time_max_ms_.AddSample(receive_stats.max_decode_ms);
515 if (receive_stats.width > 0 && receive_stats.height > 0) {
516 pixels_.AddSample(receive_stats.width * receive_stats.height);
517 }
Elad Alon58e06572019-05-08 15:34:24 +0200518
519 // |frames_decoded| and |frames_rendered| are used because they are more
520 // accurate than |decode_frame_rate| and |render_frame_rate|.
521 // The latter two are calculated on a momentary basis.
522 const double total_frames_duration_sec_double =
523 static_cast<double>(receive_stats.total_frames_duration_ms) / 1000.0;
524 if (total_frames_duration_sec_double > 0) {
525 decode_frame_rate_ = static_cast<double>(receive_stats.frames_decoded) /
526 total_frames_duration_sec_double;
527 render_frame_rate_ = static_cast<double>(receive_stats.frames_rendered) /
528 total_frames_duration_sec_double;
529 }
530
531 // Freeze metrics.
Elad Alon8c513c72019-05-07 21:22:24 +0200532 freeze_count_ = receive_stats.freeze_count;
533 total_freezes_duration_ms_ = receive_stats.total_freezes_duration_ms;
534 total_frames_duration_ms_ = receive_stats.total_frames_duration_ms;
535 sum_squared_frame_durations_ = receive_stats.sum_squared_frame_durations;
Artem Titovff7730d2019-04-02 13:46:53 +0200536 }
537
538 if (audio_receive_stream_ != nullptr) {
539 AudioReceiveStream::Stats receive_stats = audio_receive_stream_->GetStats();
540 audio_expand_rate_.AddSample(receive_stats.expand_rate);
541 audio_accelerate_rate_.AddSample(receive_stats.accelerate_rate);
542 audio_jitter_buffer_ms_.AddSample(receive_stats.jitter_buffer_ms);
543 }
544
545 memory_usage_.AddSample(rtc::GetProcessResidentSizeBytes());
546
547 stats_polling_task_id_ = task_queue_->PostDelayedTask(
548 [this]() { PollStats(); }, kSendStatsPollingIntervalMs);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200549}
550
Niels Möller4731f002019-05-03 09:34:24 +0200551void VideoAnalyzer::FrameComparisonThread(void* obj) {
552 VideoAnalyzer* analyzer = static_cast<VideoAnalyzer*>(obj);
553 while (analyzer->CompareFrames()) {
554 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200555}
556
557bool VideoAnalyzer::CompareFrames() {
558 if (AllFramesRecorded())
559 return false;
560
561 FrameComparison comparison;
562
563 if (!PopComparison(&comparison)) {
564 // Wait until new comparison task is available, or test is done.
565 // If done, wake up remaining threads waiting.
566 comparison_available_event_.Wait(1000);
567 if (AllFramesRecorded()) {
568 comparison_available_event_.Set();
569 return false;
570 }
571 return true; // Try again.
572 }
573
574 StartExcludingCpuThreadTime();
575
576 PerformFrameComparison(comparison);
577
578 StopExcludingCpuThreadTime();
579
580 if (FrameProcessed()) {
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200581 done_.Set();
582 comparison_available_event_.Set();
583 return false;
584 }
585
586 return true;
587}
588
589bool VideoAnalyzer::PopComparison(VideoAnalyzer::FrameComparison* comparison) {
590 rtc::CritScope crit(&comparison_lock_);
591 // If AllFramesRecorded() is true, it means we have already popped
592 // frames_to_process_ frames from comparisons_, so there is no more work
593 // for this thread to be done. frames_processed_ might still be lower if
594 // all comparisons are not done, but those frames are currently being
595 // worked on by other threads.
596 if (comparisons_.empty() || AllFramesRecorded())
597 return false;
598
599 *comparison = comparisons_.front();
600 comparisons_.pop_front();
601
602 FrameRecorded();
603 return true;
604}
605
606void VideoAnalyzer::FrameRecorded() {
607 rtc::CritScope crit(&comparison_lock_);
608 ++frames_recorded_;
609}
610
611bool VideoAnalyzer::AllFramesRecorded() {
612 rtc::CritScope crit(&comparison_lock_);
Niels Möller4731f002019-05-03 09:34:24 +0200613 RTC_DCHECK(frames_recorded_ <= frames_to_process_);
614 return frames_recorded_ == frames_to_process_ || quit_;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200615}
616
617bool VideoAnalyzer::FrameProcessed() {
618 rtc::CritScope crit(&comparison_lock_);
619 ++frames_processed_;
620 assert(frames_processed_ <= frames_to_process_);
621 return frames_processed_ == frames_to_process_;
622}
623
624void VideoAnalyzer::PrintResults() {
625 StopMeasuringCpuProcessTime();
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100626 int frames_left;
627 {
628 rtc::CritScope crit(&crit_);
629 frames_left = frames_.size();
630 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200631 rtc::CritScope crit(&comparison_lock_);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200632 PrintResult("psnr", psnr_, " dB");
633 PrintResult("ssim", ssim_, " score");
634 PrintResult("sender_time", sender_time_, " ms");
635 PrintResult("receiver_time", receiver_time_, " ms");
636 PrintResult("network_time", network_time_, " ms");
637 PrintResult("total_delay_incl_network", end_to_end_, " ms");
638 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
639 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
640 PrintResult("encode_time", encode_time_ms_, " ms");
641 PrintResult("media_bitrate", media_bitrate_bps_, " bps");
642 PrintResult("fec_bitrate", fec_bitrate_bps_, " bps");
643 PrintResult("send_bandwidth", send_bandwidth_bps_, " bps");
Ilya Nikolaevskiyd47d3eb2019-01-21 16:27:17 +0100644 PrintResult("pixels_per_frame", pixels_, " px");
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200645
Elad Alon58e06572019-05-08 15:34:24 +0200646 test::PrintResult("decode_frame_rate", "", test_label_.c_str(),
647 decode_frame_rate_, "fps", false);
648 test::PrintResult("render_frame_rate", "", test_label_.c_str(),
649 render_frame_rate_, "fps", false);
650
Elad Alon8c513c72019-05-07 21:22:24 +0200651 // Record the time from the last freeze until the last rendered frame to
652 // ensure we cover the full timespan of the session. Otherwise the metric
653 // would penalize an early freeze followed by no freezes until the end.
654 time_between_freezes_.AddSample(last_render_time_ - last_unfreeze_time_ms_);
655
656 // Freeze metrics.
657 PrintResult("time_between_freezes", time_between_freezes_, " ms");
658
659 const double freeze_count_double = static_cast<double>(freeze_count_);
660 const double total_freezes_duration_ms_double =
661 static_cast<double>(total_freezes_duration_ms_);
662 const double total_frames_duration_ms_double =
663 static_cast<double>(total_frames_duration_ms_);
664
665 if (total_frames_duration_ms_double > 0) {
666 test::PrintResult(
667 "freeze_duration_ratio", "", test_label_.c_str(),
668 total_freezes_duration_ms_double / total_frames_duration_ms_double, "",
669 false);
670 RTC_DCHECK_LE(total_freezes_duration_ms_double,
671 total_frames_duration_ms_double);
672
673 constexpr double ms_per_minute = 60 * 1000;
674 const double total_frames_duration_min =
675 total_frames_duration_ms_double / ms_per_minute;
676 if (total_frames_duration_min > 0) {
677 test::PrintResult("freeze_count_per_minute", "", test_label_.c_str(),
678 freeze_count_double / total_frames_duration_min,
679 "freezes", false);
680 }
681 }
682
Elad Alon133f7e72019-05-08 09:51:56 +0200683 test::PrintResult("freeze_duration_average", "", test_label_.c_str(),
Elad Alon8c513c72019-05-07 21:22:24 +0200684 freeze_count_double > 0
685 ? total_freezes_duration_ms_double / freeze_count_double
686 : 0,
687 "ms", false);
688
689 if (1000 * sum_squared_frame_durations_ > 0) {
690 test::PrintResult(
691 "harmonic_frame_rate", "", test_label_.c_str(),
692 total_frames_duration_ms_double / (1000 * sum_squared_frame_durations_),
693 "", false);
694 }
695
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200696 if (worst_frame_) {
697 test::PrintResult("min_psnr", "", test_label_.c_str(), worst_frame_->psnr,
698 "dB", false);
699 }
700
701 if (receive_stream_ != nullptr) {
702 PrintResult("decode_time", decode_time_ms_, " ms");
703 }
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100704 dropped_frames_ += dropped_frames_before_first_encode_ +
705 dropped_frames_before_rendering_ + frames_left;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200706 test::PrintResult("dropped_frames", "", test_label_.c_str(), dropped_frames_,
707 "frames", false);
708 test::PrintResult("cpu_usage", "", test_label_.c_str(), GetCpuUsagePercent(),
709 "%", false);
710
711#if defined(WEBRTC_WIN)
712 // On Linux and Mac in Resident Set some unused pages may be counted.
713 // Therefore this metric will depend on order in which tests are run and
714 // will be flaky.
715 PrintResult("memory_usage", memory_usage_, " bytes");
716#endif
717
718 // Saving only the worst frame for manual analysis. Intention here is to
719 // only detect video corruptions and not to track picture quality. Thus,
720 // jpeg is used here.
721 if (FLAG_save_worst_frame && worst_frame_) {
722 std::string output_dir;
723 test::GetTestArtifactsDir(&output_dir);
724 std::string output_path =
Niels Möller7b3c76b2018-11-07 09:54:28 +0100725 test::JoinFilename(output_dir, test_label_ + ".jpg");
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200726 RTC_LOG(LS_INFO) << "Saving worst frame to " << output_path;
727 test::JpegFrameWriter frame_writer(output_path);
728 RTC_CHECK(
729 frame_writer.WriteFrame(worst_frame_->frame, 100 /*best quality*/));
730 }
731
Christoffer Rodbroc2a02882018-08-07 14:10:56 +0200732 if (audio_receive_stream_ != nullptr) {
733 PrintResult("audio_expand_rate", audio_expand_rate_, "");
734 PrintResult("audio_accelerate_rate", audio_accelerate_rate_, "");
735 PrintResult("audio_jitter_buffer", audio_jitter_buffer_ms_, " ms");
736 }
737
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200738 // Disable quality check for quick test, as quality checks may fail
739 // because too few samples were collected.
740 if (!is_quick_test_enabled_) {
Yves Gerey79e9f4b2019-04-13 18:59:53 +0200741 EXPECT_GT(*psnr_.GetMean(), avg_psnr_threshold_);
742 EXPECT_GT(*ssim_.GetMean(), avg_ssim_threshold_);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200743 }
744}
745
746void VideoAnalyzer::PerformFrameComparison(
747 const VideoAnalyzer::FrameComparison& comparison) {
748 // Perform expensive psnr and ssim calculations while not holding lock.
749 double psnr = -1.0;
750 double ssim = -1.0;
751 if (comparison.reference && !comparison.dropped) {
752 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
753 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
754 }
755
756 rtc::CritScope crit(&comparison_lock_);
757
758 if (psnr >= 0.0 && (!worst_frame_ || worst_frame_->psnr > psnr)) {
759 worst_frame_.emplace(FrameWithPsnr{psnr, *comparison.render});
760 }
761
762 if (graph_data_output_file_) {
763 samples_.push_back(Sample(comparison.dropped, comparison.input_time_ms,
764 comparison.send_time_ms, comparison.recv_time_ms,
765 comparison.render_time_ms,
766 comparison.encoded_frame_size, psnr, ssim));
767 }
768 if (psnr >= 0.0)
769 psnr_.AddSample(psnr);
770 if (ssim >= 0.0)
771 ssim_.AddSample(ssim);
772
773 if (comparison.dropped) {
774 ++dropped_frames_;
775 return;
776 }
777 if (last_unfreeze_time_ms_ == 0)
778 last_unfreeze_time_ms_ = comparison.render_time_ms;
779 if (last_render_time_ != 0) {
780 const int64_t render_delta_ms =
781 comparison.render_time_ms - last_render_time_;
782 rendered_delta_.AddSample(render_delta_ms);
783 if (last_render_delta_ms_ != 0 &&
784 render_delta_ms - last_render_delta_ms_ > 150) {
785 time_between_freezes_.AddSample(last_render_time_ -
786 last_unfreeze_time_ms_);
787 last_unfreeze_time_ms_ = comparison.render_time_ms;
788 }
789 last_render_delta_ms_ = render_delta_ms;
790 }
791 last_render_time_ = comparison.render_time_ms;
792
793 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
794 if (comparison.recv_time_ms > 0) {
795 // If recv_time_ms == 0, this frame consisted of a packets which were all
796 // lost in the transport. Since we were able to render the frame, however,
797 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
798 // happens internally in Call, and we can therefore here not know which
799 // FEC packets that protected the lost media packets. Consequently, we
800 // were not able to record a meaningful recv_time_ms. We therefore skip
801 // this sample.
802 //
803 // The reasoning above does not hold for ULPFEC and RTX, as for those
804 // strategies the timestamp of the received packets is set to the
805 // timestamp of the protected/retransmitted media packet. I.e., then
806 // recv_time_ms != 0, even though the media packets were lost.
807 receiver_time_.AddSample(comparison.render_time_ms -
808 comparison.recv_time_ms);
809 network_time_.AddSample(comparison.recv_time_ms - comparison.send_time_ms);
810 }
811 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
812 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
813}
814
815void VideoAnalyzer::PrintResult(const char* result_type,
Yves Gerey79e9f4b2019-04-13 18:59:53 +0200816 Statistics stats,
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200817 const char* unit) {
Yves Gerey79e9f4b2019-04-13 18:59:53 +0200818 test::PrintResultMeanAndError(
819 result_type, "", test_label_.c_str(), stats.GetMean().value_or(0),
820 stats.GetStandardDeviation().value_or(0), unit, false);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200821}
822
823void VideoAnalyzer::PrintSamplesToFile() {
824 FILE* out = graph_data_output_file_;
825 rtc::CritScope crit(&comparison_lock_);
Steve Antonbd631a02019-03-28 10:51:27 -0700826 absl::c_sort(samples_, [](const Sample& A, const Sample& B) -> bool {
827 return A.input_time_ms < B.input_time_ms;
828 });
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200829
830 fprintf(out, "%s\n", graph_title_.c_str());
831 fprintf(out, "%" PRIuS "\n", samples_.size());
832 fprintf(out,
833 "dropped "
834 "input_time_ms "
835 "send_time_ms "
836 "recv_time_ms "
837 "render_time_ms "
838 "encoded_frame_size "
839 "psnr "
840 "ssim "
841 "encode_time_ms\n");
842 for (const Sample& sample : samples_) {
843 fprintf(out,
844 "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
845 " %lf %lf\n",
846 sample.dropped, sample.input_time_ms, sample.send_time_ms,
847 sample.recv_time_ms, sample.render_time_ms,
848 sample.encoded_frame_size, sample.psnr, sample.ssim);
849 }
850}
851
852double VideoAnalyzer::GetAverageMediaBitrateBps() {
853 if (last_sending_time_ == first_sending_time_) {
854 return 0;
855 } else {
856 return static_cast<double>(total_media_bytes_) * 8 /
857 (last_sending_time_ - first_sending_time_) *
858 rtc::kNumMillisecsPerSec;
859 }
860}
861
862void VideoAnalyzer::AddCapturedFrameForComparison(
863 const VideoFrame& video_frame) {
864 rtc::CritScope lock(&crit_);
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100865 if (captured_frames_ < frames_to_process_) {
866 ++captured_frames_;
867 frames_.push_back(video_frame);
868 }
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200869}
870
871void VideoAnalyzer::AddFrameComparison(const VideoFrame& reference,
872 const VideoFrame& render,
873 bool dropped,
874 int64_t render_time_ms) {
875 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
876 int64_t send_time_ms = send_times_[reference_timestamp];
877 send_times_.erase(reference_timestamp);
878 int64_t recv_time_ms = recv_times_[reference_timestamp];
879 recv_times_.erase(reference_timestamp);
880
881 // TODO(ivica): Make this work for > 2 streams.
882 auto it = encoded_frame_sizes_.find(reference_timestamp);
883 if (it == encoded_frame_sizes_.end())
884 it = encoded_frame_sizes_.find(reference_timestamp - 1);
885 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
886 if (it != encoded_frame_sizes_.end())
887 encoded_frame_sizes_.erase(it);
888
889 rtc::CritScope crit(&comparison_lock_);
890 if (comparisons_.size() < kMaxComparisons) {
891 comparisons_.push_back(FrameComparison(
892 reference, render, dropped, reference.ntp_time_ms(), send_time_ms,
893 recv_time_ms, render_time_ms, encoded_size));
894 } else {
895 comparisons_.push_back(FrameComparison(dropped, reference.ntp_time_ms(),
896 send_time_ms, recv_time_ms,
897 render_time_ms, encoded_size));
898 }
899 comparison_available_event_.Set();
900}
901
902VideoAnalyzer::FrameComparison::FrameComparison()
903 : dropped(false),
904 input_time_ms(0),
905 send_time_ms(0),
906 recv_time_ms(0),
907 render_time_ms(0),
908 encoded_frame_size(0) {}
909
910VideoAnalyzer::FrameComparison::FrameComparison(const VideoFrame& reference,
911 const VideoFrame& render,
912 bool dropped,
913 int64_t input_time_ms,
914 int64_t send_time_ms,
915 int64_t recv_time_ms,
916 int64_t render_time_ms,
917 size_t encoded_frame_size)
918 : reference(reference),
919 render(render),
920 dropped(dropped),
921 input_time_ms(input_time_ms),
922 send_time_ms(send_time_ms),
923 recv_time_ms(recv_time_ms),
924 render_time_ms(render_time_ms),
925 encoded_frame_size(encoded_frame_size) {}
926
927VideoAnalyzer::FrameComparison::FrameComparison(bool dropped,
928 int64_t input_time_ms,
929 int64_t send_time_ms,
930 int64_t recv_time_ms,
931 int64_t render_time_ms,
932 size_t encoded_frame_size)
933 : dropped(dropped),
934 input_time_ms(input_time_ms),
935 send_time_ms(send_time_ms),
936 recv_time_ms(recv_time_ms),
937 render_time_ms(render_time_ms),
938 encoded_frame_size(encoded_frame_size) {}
939
940VideoAnalyzer::Sample::Sample(int dropped,
941 int64_t input_time_ms,
942 int64_t send_time_ms,
943 int64_t recv_time_ms,
944 int64_t render_time_ms,
945 size_t encoded_frame_size,
946 double psnr,
947 double ssim)
948 : dropped(dropped),
949 input_time_ms(input_time_ms),
950 send_time_ms(send_time_ms),
951 recv_time_ms(recv_time_ms),
952 render_time_ms(render_time_ms),
953 encoded_frame_size(encoded_frame_size),
954 psnr(psnr),
955 ssim(ssim) {}
956
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200957VideoAnalyzer::CapturedFrameForwarder::CapturedFrameForwarder(
958 VideoAnalyzer* analyzer,
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100959 Clock* clock,
Ilya Nikolaevskiy85fc3252019-02-11 10:41:50 +0100960 int frames_to_process)
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200961 : analyzer_(analyzer),
962 send_stream_input_(nullptr),
Niels Möller1c931c42018-12-18 16:08:11 +0100963 video_source_(nullptr),
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100964 clock_(clock),
965 captured_frames_(0),
Ilya Nikolaevskiy85fc3252019-02-11 10:41:50 +0100966 frames_to_process_(frames_to_process) {}
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200967
968void VideoAnalyzer::CapturedFrameForwarder::SetSource(
Niels Möller1c931c42018-12-18 16:08:11 +0100969 VideoSourceInterface<VideoFrame>* video_source) {
970 video_source_ = video_source;
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200971}
972
973void VideoAnalyzer::CapturedFrameForwarder::OnFrame(
974 const VideoFrame& video_frame) {
975 VideoFrame copy = video_frame;
976 // Frames from the capturer does not have a rtp timestamp.
977 // Create one so it can be used for comparison.
978 RTC_DCHECK_EQ(0, video_frame.timestamp());
979 if (video_frame.ntp_time_ms() == 0)
980 copy.set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
981 copy.set_timestamp(copy.ntp_time_ms() * 90);
982 analyzer_->AddCapturedFrameForComparison(copy);
983 rtc::CritScope lock(&crit_);
Ilya Nikolaevskiy6957abe2019-01-29 16:33:04 +0100984 ++captured_frames_;
Ilya Nikolaevskiy85fc3252019-02-11 10:41:50 +0100985 if (send_stream_input_ && captured_frames_ <= frames_to_process_)
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200986 send_stream_input_->OnFrame(copy);
987}
988
989void VideoAnalyzer::CapturedFrameForwarder::AddOrUpdateSink(
990 rtc::VideoSinkInterface<VideoFrame>* sink,
991 const rtc::VideoSinkWants& wants) {
992 {
993 rtc::CritScope lock(&crit_);
994 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
995 send_stream_input_ = sink;
996 }
Niels Möller1c931c42018-12-18 16:08:11 +0100997 if (video_source_) {
998 video_source_->AddOrUpdateSink(this, wants);
Sebastian Janssond4c5d632018-07-10 12:57:37 +0200999 }
1000}
1001
1002void VideoAnalyzer::CapturedFrameForwarder::RemoveSink(
1003 rtc::VideoSinkInterface<VideoFrame>* sink) {
1004 rtc::CritScope lock(&crit_);
1005 RTC_DCHECK(sink == send_stream_input_);
1006 send_stream_input_ = nullptr;
Sebastian Janssond4c5d632018-07-10 12:57:37 +02001007}
1008
1009} // namespace webrtc