blob: e32696de712a6f7bce8c4f62036fe551a88baa61 [file] [log] [blame]
Sebastian Jansson7150d8c2019-04-09 12:18:091/*
2 * Copyright 2019 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
11#include "test/scenario/stats_collection.h"
Jonas Olssona4d87372019-07-05 17:08:3312
Sebastian Jansson7150d8c2019-04-09 12:18:0913#include "common_video/libyuv/include/webrtc_libyuv.h"
Sebastian Jansson9a2ca0a2019-04-15 11:18:1914#include "rtc_base/memory_usage.h"
Niels Möllera8370302019-09-02 13:16:4915#include "rtc_base/thread.h"
Sebastian Jansson7150d8c2019-04-09 12:18:0916
17namespace webrtc {
18namespace test {
19
20VideoQualityAnalyzer::VideoQualityAnalyzer(
21 VideoQualityAnalyzerConfig config,
22 std::unique_ptr<RtcEventLogOutput> writer)
23 : config_(config), writer_(std::move(writer)) {
24 if (writer_) {
25 PrintHeaders();
26 }
27}
28
29VideoQualityAnalyzer::~VideoQualityAnalyzer() = default;
30
31void VideoQualityAnalyzer::PrintHeaders() {
Ali Tofighb7821ce2022-07-12 14:08:1332 writer_->Write(
Sebastian Jansson7150d8c2019-04-09 12:18:0933 "capture_time render_time capture_width capture_height render_width "
Ali Tofighb7821ce2022-07-12 14:08:1334 "render_height psnr\n");
Sebastian Jansson7150d8c2019-04-09 12:18:0935}
36
37std::function<void(const VideoFramePair&)> VideoQualityAnalyzer::Handler() {
38 return [this](VideoFramePair pair) { HandleFramePair(pair); };
39}
40
Sebastian Jansson7cbee842019-08-06 15:19:3841void VideoQualityAnalyzer::HandleFramePair(VideoFramePair sample, double psnr) {
42 layer_analyzers_[sample.layer_id].HandleFramePair(sample, psnr,
43 writer_.get());
Sebastian Jansson3d351c62019-08-05 10:46:1244 cached_.reset();
Sebastian Janssonad5c4ac2019-08-05 09:11:5845}
46
Sebastian Jansson7cbee842019-08-06 15:19:3847void VideoQualityAnalyzer::HandleFramePair(VideoFramePair sample) {
48 double psnr = NAN;
49 if (sample.decoded)
50 psnr = I420PSNR(*sample.captured->ToI420(), *sample.decoded->ToI420());
51
52 if (config_.thread) {
Henrik Boström2deee4b2022-01-20 10:58:0553 config_.thread->PostTask(
54 [this, sample, psnr] { HandleFramePair(std::move(sample), psnr); });
Sebastian Jansson7cbee842019-08-06 15:19:3855 } else {
56 HandleFramePair(std::move(sample), psnr);
57 }
58}
59
Sebastian Jansson9a2ca0a2019-04-15 11:18:1960std::vector<VideoQualityStats> VideoQualityAnalyzer::layer_stats() const {
61 std::vector<VideoQualityStats> res;
62 for (auto& layer : layer_analyzers_)
63 res.push_back(layer.second.stats_);
64 return res;
65}
66
67VideoQualityStats& VideoQualityAnalyzer::stats() {
68 if (!cached_) {
69 cached_ = VideoQualityStats();
70 for (auto& layer : layer_analyzers_)
71 cached_->AddStats(layer.second.stats_);
72 }
73 return *cached_;
74}
75
76void VideoLayerAnalyzer::HandleFramePair(VideoFramePair sample,
Sebastian Jansson7cbee842019-08-06 15:19:3877 double psnr,
Sebastian Jansson9a2ca0a2019-04-15 11:18:1978 RtcEventLogOutput* writer) {
Sebastian Jansson7150d8c2019-04-09 12:18:0979 RTC_CHECK(sample.captured);
Sebastian Jansson9a2ca0a2019-04-15 11:18:1980 HandleCapturedFrame(sample);
Sebastian Jansson7150d8c2019-04-09 12:18:0981 if (!sample.decoded) {
Sebastian Janssone9cac4f2019-06-24 15:10:5582 // Can only happen in the beginning of a call or if the resolution is
83 // reduced. Otherwise we will detect a freeze.
Sebastian Jansson7150d8c2019-04-09 12:18:0984 ++stats_.lost_count;
Sebastian Jansson9a2ca0a2019-04-15 11:18:1985 ++skip_count_;
Sebastian Jansson7150d8c2019-04-09 12:18:0986 } else {
Sebastian Janssone9cac4f2019-06-24 15:10:5587 stats_.psnr_with_freeze.AddSample(psnr);
Sebastian Jansson9a2ca0a2019-04-15 11:18:1988 if (sample.repeated) {
89 ++stats_.freeze_count;
90 ++skip_count_;
91 } else {
Sebastian Janssone9cac4f2019-06-24 15:10:5592 stats_.psnr.AddSample(psnr);
Sebastian Jansson9a2ca0a2019-04-15 11:18:1993 HandleRenderedFrame(sample);
94 }
Sebastian Jansson7150d8c2019-04-09 12:18:0995 }
Sebastian Jansson9a2ca0a2019-04-15 11:18:1996 if (writer) {
97 LogWriteFormat(writer, "%.3f %.3f %.3f %i %i %i %i %.3f\n",
Sebastian Jansson7150d8c2019-04-09 12:18:0998 sample.capture_time.seconds<double>(),
99 sample.render_time.seconds<double>(),
100 sample.captured->width(), sample.captured->height(),
Sebastian Jansson6e07cde2020-01-30 17:14:01101 sample.decoded ? sample.decoded->width() : 0,
102 sample.decoded ? sample.decoded->height() : 0, psnr);
Sebastian Jansson7150d8c2019-04-09 12:18:09103 }
104}
105
Sebastian Jansson9a2ca0a2019-04-15 11:18:19106void VideoLayerAnalyzer::HandleCapturedFrame(const VideoFramePair& sample) {
107 stats_.capture.AddFrameInfo(*sample.captured, sample.capture_time);
108 if (last_freeze_time_.IsInfinite())
109 last_freeze_time_ = sample.capture_time;
Sebastian Jansson7150d8c2019-04-09 12:18:09110}
111
Sebastian Jansson9a2ca0a2019-04-15 11:18:19112void VideoLayerAnalyzer::HandleRenderedFrame(const VideoFramePair& sample) {
Sebastian Janssone9cac4f2019-06-24 15:10:55113 stats_.capture_to_decoded_delay.AddSample(sample.decoded_time -
114 sample.capture_time);
115 stats_.end_to_end_delay.AddSample(sample.render_time - sample.capture_time);
Sebastian Jansson9a2ca0a2019-04-15 11:18:19116 stats_.render.AddFrameInfo(*sample.decoded, sample.render_time);
117 stats_.skipped_between_rendered.AddSample(skip_count_);
118 skip_count_ = 0;
119
120 if (last_render_time_.IsFinite()) {
121 RTC_DCHECK(sample.render_time.IsFinite());
122 TimeDelta render_interval = sample.render_time - last_render_time_;
123 TimeDelta mean_interval = stats_.render.frames.interval().Mean();
Danil Chapovalov0c626af2020-02-10 10:16:00124 if (render_interval > TimeDelta::Millis(150) + mean_interval ||
Sebastian Jansson9a2ca0a2019-04-15 11:18:19125 render_interval > 3 * mean_interval) {
126 stats_.freeze_duration.AddSample(render_interval);
127 stats_.time_between_freezes.AddSample(last_render_time_ -
128 last_freeze_time_);
129 last_freeze_time_ = sample.render_time;
130 }
131 }
132 last_render_time_ = sample.render_time;
133}
134
135void CallStatsCollector::AddStats(Call::Stats sample) {
Sebastian Jansson72b75242019-04-15 13:10:18136 if (sample.send_bandwidth_bps > 0)
137 stats_.target_rate.AddSampleBps(sample.send_bandwidth_bps);
138 if (sample.pacer_delay_ms > 0)
Danil Chapovalov0c626af2020-02-10 10:16:00139 stats_.pacer_delay.AddSample(TimeDelta::Millis(sample.pacer_delay_ms));
Sebastian Jansson72b75242019-04-15 13:10:18140 if (sample.rtt_ms > 0)
Danil Chapovalov0c626af2020-02-10 10:16:00141 stats_.round_trip_time.AddSample(TimeDelta::Millis(sample.rtt_ms));
Sebastian Jansson9a2ca0a2019-04-15 11:18:19142 stats_.memory_usage.AddSample(rtc::GetProcessResidentSizeBytes());
143}
144
Tommi3176ef72022-05-22 18:47:28145void AudioReceiveStatsCollector::AddStats(
146 AudioReceiveStreamInterface::Stats sample) {
Sebastian Jansson9a2ca0a2019-04-15 11:18:19147 stats_.expand_rate.AddSample(sample.expand_rate);
148 stats_.accelerate_rate.AddSample(sample.accelerate_rate);
149 stats_.jitter_buffer.AddSampleMs(sample.jitter_buffer_ms);
150}
151
152void VideoSendStatsCollector::AddStats(VideoSendStream::Stats sample,
153 Timestamp at_time) {
154 // It's not certain that we yet have estimates for any of these stats.
155 // Check that they are positive before mixing them in.
156 if (sample.encode_frame_rate <= 0)
157 return;
158
159 stats_.encode_frame_rate.AddSample(sample.encode_frame_rate);
160 stats_.encode_time.AddSampleMs(sample.avg_encode_time_ms);
161 stats_.encode_usage.AddSample(sample.encode_usage_percent / 100.0);
162 stats_.media_bitrate.AddSampleBps(sample.media_bitrate_bps);
163
164 size_t fec_bytes = 0;
165 for (const auto& kv : sample.substreams) {
166 fec_bytes += kv.second.rtp_stats.fec.payload_bytes +
167 kv.second.rtp_stats.fec.padding_bytes;
168 }
169 if (last_update_.IsFinite()) {
Danil Chapovalovcad3e0e2020-02-17 17:46:07170 auto fec_delta = DataSize::Bytes(fec_bytes - last_fec_bytes_);
Sebastian Jansson9a2ca0a2019-04-15 11:18:19171 auto time_delta = at_time - last_update_;
172 stats_.fec_bitrate.AddSample(fec_delta / time_delta);
173 }
174 last_fec_bytes_ = fec_bytes;
175 last_update_ = at_time;
176}
177
Tommif6f45432022-05-20 13:21:20178void VideoReceiveStatsCollector::AddStats(
179 VideoReceiveStreamInterface::Stats sample) {
Sebastian Jansson9a2ca0a2019-04-15 11:18:19180 if (sample.decode_ms > 0)
181 stats_.decode_time.AddSampleMs(sample.decode_ms);
182 if (sample.max_decode_ms > 0)
183 stats_.decode_time_max.AddSampleMs(sample.max_decode_ms);
184 if (sample.width > 0 && sample.height > 0) {
185 stats_.decode_pixels.AddSample(sample.width * sample.height);
186 stats_.resolution.AddSample(sample.height);
187 }
188}
Sebastian Jansson7150d8c2019-04-09 12:18:09189} // namespace test
190} // namespace webrtc