blob: 42ab7e19ecf27bbd8b6f7bcf28c1d68983af5b82 [file] [log] [blame]
sprang@webrtc.org09315702014-02-07 12:06:29 +00001/*
2 * Copyright (c) 2013 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "video/receive_statistics_proxy.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000012
philipela45102f2017-02-22 05:30:39 -080013#include <algorithm>
asaperssonf839dcc2015-10-08 00:41:59 -070014#include <cmath>
philipela45102f2017-02-22 05:30:39 -080015#include <utility>
asaperssonf839dcc2015-10-08 00:41:59 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/video_coding/include/video_codec_interface.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
Tommifef05002018-02-27 13:51:08 +010020#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "system_wrappers/include/clock.h"
23#include "system_wrappers/include/metrics.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000024
25namespace webrtc {
asaperssonde9e5ff2016-11-02 07:14:03 -070026namespace {
27// Periodic time interval for processing samples for |freq_offset_counter_|.
28const int64_t kFreqOffsetProcessIntervalMs = 40000;
palmkvist349092b2016-12-13 02:45:57 -080029
30// Configuration for bad call detection.
palmkvista40672a2017-01-13 05:58:34 -080031const int kBadCallMinRequiredSamples = 10;
palmkvist349092b2016-12-13 02:45:57 -080032const int kMinSampleLengthMs = 990;
33const int kNumMeasurements = 10;
34const int kNumMeasurementsVariance = kNumMeasurements * 1.5;
35const float kBadFraction = 0.8f;
36// For fps:
37// Low means low enough to be bad, high means high enough to be good
38const int kLowFpsThreshold = 12;
39const int kHighFpsThreshold = 14;
40// For qp and fps variance:
41// Low means low enough to be good, high means high enough to be bad
42const int kLowQpThresholdVp8 = 60;
43const int kHighQpThresholdVp8 = 70;
44const int kLowVarianceThreshold = 1;
45const int kHighVarianceThreshold = 2;
philipela45102f2017-02-22 05:30:39 -080046
ilnika79cc282017-08-23 05:24:10 -070047// Some metrics are reported as a maximum over this period.
Ilya Nikolaevskiyb06b3582017-10-16 17:59:12 +020048// This should be synchronized with a typical getStats polling interval in
49// the clients.
50const int kMovingMaxWindowMs = 1000;
ilnika79cc282017-08-23 05:24:10 -070051
philipela45102f2017-02-22 05:30:39 -080052// How large window we use to calculate the framerate/bitrate.
53const int kRateStatisticsWindowSizeMs = 1000;
ilnik6d5b4d62017-08-30 03:32:14 -070054
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +020055// Some sane ballpark estimate for maximum common value of inter-frame delay.
56// Values below that will be stored explicitly in the array,
57// values above - in the map.
58const int kMaxCommonInterframeDelayMs = 500;
59
Tommifef05002018-02-27 13:51:08 +010060const char* UmaPrefixForContentType(VideoContentType content_type) {
61 if (videocontenttypehelpers::IsScreenshare(content_type))
62 return "WebRTC.Video.Screenshare";
63 return "WebRTC.Video";
ilnik6d5b4d62017-08-30 03:32:14 -070064}
65
66std::string UmaSuffixForContentType(VideoContentType content_type) {
Karl Wiberg881f1682018-03-08 15:03:23 +010067 char ss_buf[1024];
68 rtc::SimpleStringBuilder ss(ss_buf);
ilnik6d5b4d62017-08-30 03:32:14 -070069 int simulcast_id = videocontenttypehelpers::GetSimulcastId(content_type);
70 if (simulcast_id > 0) {
71 ss << ".S" << simulcast_id - 1;
72 }
73 int experiment_id = videocontenttypehelpers::GetExperimentId(content_type);
74 if (experiment_id > 0) {
75 ss << ".ExperimentGroup" << experiment_id - 1;
76 }
77 return ss.str();
78}
Tommifef05002018-02-27 13:51:08 +010079
asaperssonde9e5ff2016-11-02 07:14:03 -070080} // namespace
sprang@webrtc.org09315702014-02-07 12:06:29 +000081
sprang0ab8e812016-02-24 01:35:40 -080082ReceiveStatisticsProxy::ReceiveStatisticsProxy(
Tommi733b5472016-06-10 17:58:01 +020083 const VideoReceiveStream::Config* config,
sprang0ab8e812016-02-24 01:35:40 -080084 Clock* clock)
pbos@webrtc.org55707692014-12-19 15:45:03 +000085 : clock_(clock),
Tommi733b5472016-06-10 17:58:01 +020086 config_(*config),
asapersson4374a092016-07-27 00:39:09 -070087 start_ms_(clock->TimeInMilliseconds()),
palmkvist349092b2016-12-13 02:45:57 -080088 last_sample_time_(clock->TimeInMilliseconds()),
89 fps_threshold_(kLowFpsThreshold,
90 kHighFpsThreshold,
91 kBadFraction,
92 kNumMeasurements),
93 qp_threshold_(kLowQpThresholdVp8,
94 kHighQpThresholdVp8,
95 kBadFraction,
96 kNumMeasurements),
97 variance_threshold_(kLowVarianceThreshold,
98 kHighVarianceThreshold,
99 kBadFraction,
100 kNumMeasurementsVariance),
palmkvista40672a2017-01-13 05:58:34 -0800101 num_bad_states_(0),
102 num_certain_states_(0),
sprang@webrtc.org09315702014-02-07 12:06:29 +0000103 // 1000ms window, scale 1000 for ms to s.
104 decode_fps_estimator_(1000, 1000),
Tim Psiaki63046262015-09-14 10:38:08 -0700105 renders_fps_estimator_(1000, 1000),
Honghai Zhang82d78622016-05-06 11:29:15 -0700106 render_fps_tracker_(100, 10u),
asaperssonde9e5ff2016-11-02 07:14:03 -0700107 render_pixel_tracker_(100, 10u),
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200108 video_quality_observer_(
109 new VideoQualityObserver(VideoContentType::UNSPECIFIED)),
ilnika79cc282017-08-23 05:24:10 -0700110 interframe_delay_max_moving_(kMovingMaxWindowMs),
asapersson0c43f772016-11-30 01:42:26 -0800111 freq_offset_counter_(clock, nullptr, kFreqOffsetProcessIntervalMs),
ilnik00d802b2017-04-11 10:34:31 -0700112 avg_rtt_ms_(0),
ilnik75204c52017-09-04 03:35:40 -0700113 last_content_type_(VideoContentType::UNSPECIFIED),
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200114 last_codec_type_(kVideoCodecVP8),
Åsa Persson81327d52018-06-05 13:34:33 +0200115 num_delayed_frames_rendered_(0),
116 sum_missed_render_deadline_ms_(0),
ilnik75204c52017-09-04 03:35:40 -0700117 timing_frame_info_counter_(kMovingMaxWindowMs) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200118 decode_thread_.Detach();
119 network_thread_.Detach();
Tommi733b5472016-06-10 17:58:01 +0200120 stats_.ssrc = config_.rtp.remote_ssrc;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000121}
122
Niels Möller9a9f18a2019-08-02 13:52:37 +0200123void ReceiveStatisticsProxy::UpdateHistograms(
Niels Möllerd7819652019-08-13 14:43:02 +0200124 absl::optional<int> fraction_lost,
125 const StreamDataCounters& rtp_stats,
126 const StreamDataCounters* rtx_stats) {
Niels Möller9a9f18a2019-08-02 13:52:37 +0200127 // Not actually running on the decoder thread, but must be called after
128 // DecoderThreadStopped, which detaches the thread checker. It is therefore
129 // safe to access |qp_counters_|, which were updated on the decode thread
130 // earlier.
Tommi132e28e2018-02-24 17:57:33 +0100131 RTC_DCHECK_RUN_ON(&decode_thread_);
Niels Möller9a9f18a2019-08-02 13:52:37 +0200132
133 rtc::CritScope lock(&crit_);
134
Karl Wiberg881f1682018-03-08 15:03:23 +0100135 char log_stream_buf[8 * 1024];
136 rtc::SimpleStringBuilder log_stream(log_stream_buf);
ilnik6d5b4d62017-08-30 03:32:14 -0700137 int stream_duration_sec = (clock_->TimeInMilliseconds() - start_ms_) / 1000;
138 if (stats_.frame_counts.key_frames > 0 ||
139 stats_.frame_counts.delta_frames > 0) {
140 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Video.ReceiveStreamLifetimeInSeconds",
141 stream_duration_sec);
Tommifef05002018-02-27 13:51:08 +0100142 log_stream << "WebRTC.Video.ReceiveStreamLifetimeInSeconds "
143 << stream_duration_sec << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700144 }
asapersson4374a092016-07-27 00:39:09 -0700145
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200146 log_stream << "Frames decoded " << stats_.frames_decoded << '\n';
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100147
148 if (num_unique_frames_) {
149 int num_dropped_frames = *num_unique_frames_ - stats_.frames_decoded;
150 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DroppedFrames.Receiver",
151 num_dropped_frames);
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200152 log_stream << "WebRTC.Video.DroppedFrames.Receiver " << num_dropped_frames
153 << '\n';
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100154 }
155
Niels Möller9a9f18a2019-08-02 13:52:37 +0200156 if (fraction_lost && stream_duration_sec >= metrics::kMinRunTimeInSeconds) {
157 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedPacketsLostInPercent",
158 *fraction_lost);
159 log_stream << "WebRTC.Video.ReceivedPacketsLostInPercent " << *fraction_lost
160 << '\n';
Åsa Persson3c391cb2015-04-27 10:09:49 +0200161 }
asapersson0c43f772016-11-30 01:42:26 -0800162
Åsa Perssonb9b07ea2018-01-24 17:04:07 +0100163 if (first_decoded_frame_time_ms_) {
164 const int64_t elapsed_ms =
165 (clock_->TimeInMilliseconds() - *first_decoded_frame_time_ms_);
166 if (elapsed_ms >=
167 metrics::kMinRunTimeInSeconds * rtc::kNumMillisecsPerSec) {
Åsa Perssonafb8d5c2019-05-21 14:42:29 +0200168 int decoded_fps = static_cast<int>(
169 (stats_.frames_decoded * 1000.0f / elapsed_ms) + 0.5f);
170 RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.DecodedFramesPerSecond",
171 decoded_fps);
172 log_stream << "WebRTC.Video.DecodedFramesPerSecond " << decoded_fps
173 << '\n';
Åsa Persson81327d52018-06-05 13:34:33 +0200174
175 const uint32_t frames_rendered = stats_.frames_rendered;
176 if (frames_rendered > 0) {
177 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.DelayedFramesToRenderer",
178 static_cast<int>(num_delayed_frames_rendered_ *
179 100 / frames_rendered));
180 if (num_delayed_frames_rendered_ > 0) {
181 RTC_HISTOGRAM_COUNTS_1000(
182 "WebRTC.Video.DelayedFramesToRenderer_AvgDelayInMs",
183 static_cast<int>(sum_missed_render_deadline_ms_ /
184 num_delayed_frames_rendered_));
185 }
186 }
Åsa Perssonb9b07ea2018-01-24 17:04:07 +0100187 }
188 }
189
asapersson6718e972015-07-24 00:20:58 -0700190 const int kMinRequiredSamples = 200;
asaperssonf839dcc2015-10-08 00:41:59 -0700191 int samples = static_cast<int>(render_fps_tracker_.TotalSampleCount());
asapersson2077f2f2017-05-11 05:37:35 -0700192 if (samples >= kMinRequiredSamples) {
Åsa Perssonafb8d5c2019-05-21 14:42:29 +0200193 int rendered_fps = round(render_fps_tracker_.ComputeTotalRate());
asapersson1d02d3e2016-09-09 22:40:25 -0700194 RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.RenderFramesPerSecond",
Åsa Perssonafb8d5c2019-05-21 14:42:29 +0200195 rendered_fps);
196 log_stream << "WebRTC.Video.RenderFramesPerSecond " << rendered_fps << '\n';
asapersson1d02d3e2016-09-09 22:40:25 -0700197 RTC_HISTOGRAM_COUNTS_100000(
asapersson28ba9272016-01-25 05:58:23 -0800198 "WebRTC.Video.RenderSqrtPixelsPerSecond",
Tim Psiakiad13d2f2015-11-10 16:34:50 -0800199 round(render_pixel_tracker_.ComputeTotalRate()));
asaperssonf839dcc2015-10-08 00:41:59 -0700200 }
ilnik6d5b4d62017-08-30 03:32:14 -0700201
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200202 absl::optional<int> sync_offset_ms =
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200203 sync_offset_counter_.Avg(kMinRequiredSamples);
204 if (sync_offset_ms) {
205 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.AVSyncOffsetInMs",
206 *sync_offset_ms);
207 log_stream << "WebRTC.Video.AVSyncOffsetInMs " << *sync_offset_ms << '\n';
pbos35fdb2a2016-05-03 03:32:10 -0700208 }
asaperssonde9e5ff2016-11-02 07:14:03 -0700209 AggregatedStats freq_offset_stats = freq_offset_counter_.GetStats();
210 if (freq_offset_stats.num_samples > 0) {
211 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtpToNtpFreqOffsetInKhz",
212 freq_offset_stats.average);
Tommifef05002018-02-27 13:51:08 +0100213 log_stream << "WebRTC.Video.RtpToNtpFreqOffsetInKhz "
214 << freq_offset_stats.ToString() << '\n';
asaperssonde9e5ff2016-11-02 07:14:03 -0700215 }
asaperssonf8cdd182016-03-15 01:00:47 -0700216
asaperssonb99baf82017-04-20 04:05:43 -0700217 int num_total_frames =
218 stats_.frame_counts.key_frames + stats_.frame_counts.delta_frames;
219 if (num_total_frames >= kMinRequiredSamples) {
220 int num_key_frames = stats_.frame_counts.key_frames;
philipela45102f2017-02-22 05:30:39 -0800221 int key_frames_permille =
asaperssonb99baf82017-04-20 04:05:43 -0700222 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
philipela45102f2017-02-22 05:30:39 -0800223 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesReceivedInPermille",
224 key_frames_permille);
Tommifef05002018-02-27 13:51:08 +0100225 log_stream << "WebRTC.Video.KeyFramesReceivedInPermille "
226 << key_frames_permille << '\n';
philipela45102f2017-02-22 05:30:39 -0800227 }
228
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200229 absl::optional<int> qp = qp_counters_.vp8.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200230 if (qp) {
231 RTC_HISTOGRAM_COUNTS_200("WebRTC.Video.Decoded.Vp8.Qp", *qp);
232 log_stream << "WebRTC.Video.Decoded.Vp8.Qp " << *qp << '\n';
asapersson2077f2f2017-05-11 05:37:35 -0700233 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200234 absl::optional<int> decode_ms = decode_time_counter_.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200235 if (decode_ms) {
236 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", *decode_ms);
237 log_stream << "WebRTC.Video.DecodeTimeInMs " << *decode_ms << '\n';
asapersson2077f2f2017-05-11 05:37:35 -0700238 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200239 absl::optional<int> jb_delay_ms =
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200240 jitter_buffer_delay_counter_.Avg(kMinRequiredSamples);
241 if (jb_delay_ms) {
philipela45102f2017-02-22 05:30:39 -0800242 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200243 *jb_delay_ms);
244 log_stream << "WebRTC.Video.JitterBufferDelayInMs " << *jb_delay_ms << '\n';
asapersson8688a4e2016-04-27 23:42:35 -0700245 }
philipela45102f2017-02-22 05:30:39 -0800246
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200247 absl::optional<int> target_delay_ms =
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200248 target_delay_counter_.Avg(kMinRequiredSamples);
249 if (target_delay_ms) {
250 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs",
251 *target_delay_ms);
252 log_stream << "WebRTC.Video.TargetDelayInMs " << *target_delay_ms << '\n';
asapersson8688a4e2016-04-27 23:42:35 -0700253 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200254 absl::optional<int> current_delay_ms =
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200255 current_delay_counter_.Avg(kMinRequiredSamples);
256 if (current_delay_ms) {
asapersson1d02d3e2016-09-09 22:40:25 -0700257 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.CurrentDelayInMs",
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200258 *current_delay_ms);
259 log_stream << "WebRTC.Video.CurrentDelayInMs " << *current_delay_ms << '\n';
asapersson8688a4e2016-04-27 23:42:35 -0700260 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200261 absl::optional<int> delay_ms = delay_counter_.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200262 if (delay_ms)
263 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", *delay_ms);
sprang0ab8e812016-02-24 01:35:40 -0800264
ilnik6d5b4d62017-08-30 03:32:14 -0700265 // Aggregate content_specific_stats_ by removing experiment or simulcast
266 // information;
267 std::map<VideoContentType, ContentSpecificStats> aggregated_stats;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100268 for (const auto& it : content_specific_stats_) {
ilnik6d5b4d62017-08-30 03:32:14 -0700269 // Calculate simulcast specific metrics (".S0" ... ".S2" suffixes).
270 VideoContentType content_type = it.first;
271 if (videocontenttypehelpers::GetSimulcastId(content_type) > 0) {
272 // Aggregate on experiment id.
273 videocontenttypehelpers::SetExperimentId(&content_type, 0);
274 aggregated_stats[content_type].Add(it.second);
275 }
276 // Calculate experiment specific metrics (".ExperimentGroup[0-7]" suffixes).
277 content_type = it.first;
278 if (videocontenttypehelpers::GetExperimentId(content_type) > 0) {
279 // Aggregate on simulcast id.
280 videocontenttypehelpers::SetSimulcastId(&content_type, 0);
281 aggregated_stats[content_type].Add(it.second);
282 }
283 // Calculate aggregated metrics (no suffixes. Aggregated on everything).
284 content_type = it.first;
285 videocontenttypehelpers::SetSimulcastId(&content_type, 0);
286 videocontenttypehelpers::SetExperimentId(&content_type, 0);
287 aggregated_stats[content_type].Add(it.second);
ilnik00d802b2017-04-11 10:34:31 -0700288 }
289
Mirko Bonadei739baf02019-01-27 17:29:42 +0100290 for (const auto& it : aggregated_stats) {
ilnik6d5b4d62017-08-30 03:32:14 -0700291 // For the metric Foo we report the following slices:
292 // WebRTC.Video.Foo,
293 // WebRTC.Video.Screenshare.Foo,
294 // WebRTC.Video.Foo.S[0-3],
295 // WebRTC.Video.Foo.ExperimentGroup[0-7],
296 // WebRTC.Video.Screenshare.Foo.S[0-3],
297 // WebRTC.Video.Screenshare.Foo.ExperimentGroup[0-7].
298 auto content_type = it.first;
299 auto stats = it.second;
300 std::string uma_prefix = UmaPrefixForContentType(content_type);
301 std::string uma_suffix = UmaSuffixForContentType(content_type);
302 // Metrics can be sliced on either simulcast id or experiment id but not
303 // both.
304 RTC_DCHECK(videocontenttypehelpers::GetExperimentId(content_type) == 0 ||
305 videocontenttypehelpers::GetSimulcastId(content_type) == 0);
ilnik00d802b2017-04-11 10:34:31 -0700306
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200307 absl::optional<int> e2e_delay_ms =
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200308 stats.e2e_delay_counter.Avg(kMinRequiredSamples);
309 if (e2e_delay_ms) {
ilnik6d5b4d62017-08-30 03:32:14 -0700310 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200311 uma_prefix + ".EndToEndDelayInMs" + uma_suffix, *e2e_delay_ms);
Tommifef05002018-02-27 13:51:08 +0100312 log_stream << uma_prefix << ".EndToEndDelayInMs" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200313 << *e2e_delay_ms << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700314 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200315 absl::optional<int> e2e_delay_max_ms = stats.e2e_delay_counter.Max();
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200316 if (e2e_delay_max_ms && e2e_delay_ms) {
ilnik6d5b4d62017-08-30 03:32:14 -0700317 RTC_HISTOGRAM_COUNTS_SPARSE_100000(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200318 uma_prefix + ".EndToEndDelayMaxInMs" + uma_suffix, *e2e_delay_max_ms);
Tommifef05002018-02-27 13:51:08 +0100319 log_stream << uma_prefix << ".EndToEndDelayMaxInMs" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200320 << *e2e_delay_max_ms << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700321 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200322 absl::optional<int> interframe_delay_ms =
ilnik6d5b4d62017-08-30 03:32:14 -0700323 stats.interframe_delay_counter.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200324 if (interframe_delay_ms) {
ilnik6d5b4d62017-08-30 03:32:14 -0700325 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
326 uma_prefix + ".InterframeDelayInMs" + uma_suffix,
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200327 *interframe_delay_ms);
Tommifef05002018-02-27 13:51:08 +0100328 log_stream << uma_prefix << ".InterframeDelayInMs" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200329 << *interframe_delay_ms << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700330 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200331 absl::optional<int> interframe_delay_max_ms =
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200332 stats.interframe_delay_counter.Max();
333 if (interframe_delay_max_ms && interframe_delay_ms) {
ilnik6d5b4d62017-08-30 03:32:14 -0700334 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
335 uma_prefix + ".InterframeDelayMaxInMs" + uma_suffix,
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200336 *interframe_delay_max_ms);
Tommifef05002018-02-27 13:51:08 +0100337 log_stream << uma_prefix << ".InterframeDelayMaxInMs" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200338 << *interframe_delay_max_ms << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700339 }
ilnik00d802b2017-04-11 10:34:31 -0700340
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200341 absl::optional<uint32_t> interframe_delay_95p_ms =
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200342 stats.interframe_delay_percentiles.GetPercentile(0.95f);
343 if (interframe_delay_95p_ms && interframe_delay_ms != -1) {
344 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
345 uma_prefix + ".InterframeDelay95PercentileInMs" + uma_suffix,
346 *interframe_delay_95p_ms);
Tommifef05002018-02-27 13:51:08 +0100347 log_stream << uma_prefix << ".InterframeDelay95PercentileInMs"
348 << uma_suffix << " " << *interframe_delay_95p_ms << '\n';
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200349 }
350
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200351 absl::optional<int> width = stats.received_width.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200352 if (width) {
ilnik6d5b4d62017-08-30 03:32:14 -0700353 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200354 uma_prefix + ".ReceivedWidthInPixels" + uma_suffix, *width);
Tommifef05002018-02-27 13:51:08 +0100355 log_stream << uma_prefix << ".ReceivedWidthInPixels" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200356 << *width << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700357 }
asapersson1490f7a2016-09-23 02:09:46 -0700358
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200359 absl::optional<int> height = stats.received_height.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200360 if (height) {
ilnik6d5b4d62017-08-30 03:32:14 -0700361 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200362 uma_prefix + ".ReceivedHeightInPixels" + uma_suffix, *height);
Tommifef05002018-02-27 13:51:08 +0100363 log_stream << uma_prefix << ".ReceivedHeightInPixels" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200364 << *height << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700365 }
ilnik4257ab22017-07-03 01:15:58 -0700366
ilnik6d5b4d62017-08-30 03:32:14 -0700367 if (content_type != VideoContentType::UNSPECIFIED) {
368 // Don't report these 3 metrics unsliced, as more precise variants
369 // are reported separately in this method.
370 float flow_duration_sec = stats.flow_duration_ms / 1000.0;
371 if (flow_duration_sec >= metrics::kMinRunTimeInSeconds) {
372 int media_bitrate_kbps = static_cast<int>(stats.total_media_bytes * 8 /
373 flow_duration_sec / 1000);
374 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
375 uma_prefix + ".MediaBitrateReceivedInKbps" + uma_suffix,
376 media_bitrate_kbps);
Tommifef05002018-02-27 13:51:08 +0100377 log_stream << uma_prefix << ".MediaBitrateReceivedInKbps" << uma_suffix
378 << " " << media_bitrate_kbps << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700379 }
380
381 int num_total_frames =
382 stats.frame_counts.key_frames + stats.frame_counts.delta_frames;
383 if (num_total_frames >= kMinRequiredSamples) {
384 int num_key_frames = stats.frame_counts.key_frames;
385 int key_frames_permille =
386 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
387 RTC_HISTOGRAM_COUNTS_SPARSE_1000(
388 uma_prefix + ".KeyFramesReceivedInPermille" + uma_suffix,
389 key_frames_permille);
Tommifef05002018-02-27 13:51:08 +0100390 log_stream << uma_prefix << ".KeyFramesReceivedInPermille" << uma_suffix
391 << " " << key_frames_permille << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700392 }
393
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200394 absl::optional<int> qp = stats.qp_counter.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200395 if (qp) {
ilnik6d5b4d62017-08-30 03:32:14 -0700396 RTC_HISTOGRAM_COUNTS_SPARSE_200(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200397 uma_prefix + ".Decoded.Vp8.Qp" + uma_suffix, *qp);
398 log_stream << uma_prefix << ".Decoded.Vp8.Qp" << uma_suffix << " "
399 << *qp << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700400 }
401 }
ilnik4257ab22017-07-03 01:15:58 -0700402 }
403
Niels Möllerd7819652019-08-13 14:43:02 +0200404 StreamDataCounters rtp_rtx_stats = rtp_stats;
405 if (rtx_stats)
406 rtp_rtx_stats.Add(*rtx_stats);
sprang0ab8e812016-02-24 01:35:40 -0800407 int64_t elapsed_sec =
Niels Möllerd7819652019-08-13 14:43:02 +0200408 rtp_rtx_stats.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) /
409 1000;
asapersson2077f2f2017-05-11 05:37:35 -0700410 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
asapersson1d02d3e2016-09-09 22:40:25 -0700411 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800412 "WebRTC.Video.BitrateReceivedInKbps",
Niels Möllerd7819652019-08-13 14:43:02 +0200413 static_cast<int>(rtp_rtx_stats.transmitted.TotalBytes() * 8 /
414 elapsed_sec / 1000));
415 int media_bitrate_kbs = static_cast<int>(rtp_stats.MediaPayloadBytes() * 8 /
416 elapsed_sec / 1000);
ilnik6d5b4d62017-08-30 03:32:14 -0700417 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.MediaBitrateReceivedInKbps",
418 media_bitrate_kbs);
Tommifef05002018-02-27 13:51:08 +0100419 log_stream << "WebRTC.Video.MediaBitrateReceivedInKbps "
420 << media_bitrate_kbs << '\n';
asapersson1d02d3e2016-09-09 22:40:25 -0700421 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800422 "WebRTC.Video.PaddingBitrateReceivedInKbps",
Niels Möllerd7819652019-08-13 14:43:02 +0200423 static_cast<int>(rtp_rtx_stats.transmitted.padding_bytes * 8 /
424 elapsed_sec / 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700425 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800426 "WebRTC.Video.RetransmittedBitrateReceivedInKbps",
Niels Möllerd7819652019-08-13 14:43:02 +0200427 static_cast<int>(rtp_rtx_stats.retransmitted.TotalBytes() * 8 /
428 elapsed_sec / 1000));
429 if (rtx_stats) {
430 RTC_HISTOGRAM_COUNTS_10000(
431 "WebRTC.Video.RtxBitrateReceivedInKbps",
432 static_cast<int>(rtx_stats->transmitted.TotalBytes() * 8 /
433 elapsed_sec / 1000));
sprang0ab8e812016-02-24 01:35:40 -0800434 }
sprang07fb9be2016-02-24 07:55:00 -0800435 const RtcpPacketTypeCounter& counters = stats_.rtcp_packet_type_counts;
asapersson1d02d3e2016-09-09 22:40:25 -0700436 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.NackPacketsSentPerMinute",
437 counters.nack_packets * 60 / elapsed_sec);
438 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.FirPacketsSentPerMinute",
439 counters.fir_packets * 60 / elapsed_sec);
440 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.PliPacketsSentPerMinute",
441 counters.pli_packets * 60 / elapsed_sec);
sprang07fb9be2016-02-24 07:55:00 -0800442 if (counters.nack_requests > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700443 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.UniqueNackRequestsSentInPercent",
444 counters.UniqueNackRequestsInPercent());
sprang07fb9be2016-02-24 07:55:00 -0800445 }
sprang0ab8e812016-02-24 01:35:40 -0800446 }
palmkvista40672a2017-01-13 05:58:34 -0800447
448 if (num_certain_states_ >= kBadCallMinRequiredSamples) {
449 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Any",
450 100 * num_bad_states_ / num_certain_states_);
451 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200452 absl::optional<double> fps_fraction =
palmkvista40672a2017-01-13 05:58:34 -0800453 fps_threshold_.FractionHigh(kBadCallMinRequiredSamples);
454 if (fps_fraction) {
455 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRate",
456 static_cast<int>(100 * (1 - *fps_fraction)));
457 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200458 absl::optional<double> variance_fraction =
palmkvista40672a2017-01-13 05:58:34 -0800459 variance_threshold_.FractionHigh(kBadCallMinRequiredSamples);
460 if (variance_fraction) {
461 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRateVariance",
462 static_cast<int>(100 * *variance_fraction));
463 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200464 absl::optional<double> qp_fraction =
palmkvista40672a2017-01-13 05:58:34 -0800465 qp_threshold_.FractionHigh(kBadCallMinRequiredSamples);
466 if (qp_fraction) {
467 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Qp",
468 static_cast<int>(100 * *qp_fraction));
469 }
Tommifef05002018-02-27 13:51:08 +0100470
471 RTC_LOG(LS_INFO) << log_stream.str();
Niels Möller9a9f18a2019-08-02 13:52:37 +0200472 video_quality_observer_->UpdateHistograms();
Åsa Persson3c391cb2015-04-27 10:09:49 +0200473}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000474
palmkvist349092b2016-12-13 02:45:57 -0800475void ReceiveStatisticsProxy::QualitySample() {
476 int64_t now = clock_->TimeInMilliseconds();
477 if (last_sample_time_ + kMinSampleLengthMs > now)
478 return;
479
480 double fps =
481 render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200482 absl::optional<int> qp = qp_sample_.Avg(1);
palmkvist349092b2016-12-13 02:45:57 -0800483
484 bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true);
485 bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false);
486 bool prev_variance_bad = variance_threshold_.IsHigh().value_or(false);
487 bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad;
488
489 fps_threshold_.AddMeasurement(static_cast<int>(fps));
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200490 if (qp)
491 qp_threshold_.AddMeasurement(*qp);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200492 absl::optional<double> fps_variance_opt = fps_threshold_.CalculateVariance();
palmkvist349092b2016-12-13 02:45:57 -0800493 double fps_variance = fps_variance_opt.value_or(0);
494 if (fps_variance_opt) {
495 variance_threshold_.AddMeasurement(static_cast<int>(fps_variance));
496 }
497
498 bool fps_bad = !fps_threshold_.IsHigh().value_or(true);
499 bool qp_bad = qp_threshold_.IsHigh().value_or(false);
500 bool variance_bad = variance_threshold_.IsHigh().value_or(false);
501 bool any_bad = fps_bad || qp_bad || variance_bad;
502
503 if (!prev_any_bad && any_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100504 RTC_LOG(LS_INFO) << "Bad call (any) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800505 } else if (prev_any_bad && !any_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100506 RTC_LOG(LS_INFO) << "Bad call (any) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800507 }
508
509 if (!prev_fps_bad && fps_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100510 RTC_LOG(LS_INFO) << "Bad call (fps) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800511 } else if (prev_fps_bad && !fps_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100512 RTC_LOG(LS_INFO) << "Bad call (fps) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800513 }
514
515 if (!prev_qp_bad && qp_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100516 RTC_LOG(LS_INFO) << "Bad call (qp) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800517 } else if (prev_qp_bad && !qp_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100518 RTC_LOG(LS_INFO) << "Bad call (qp) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800519 }
520
521 if (!prev_variance_bad && variance_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100522 RTC_LOG(LS_INFO) << "Bad call (variance) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800523 } else if (prev_variance_bad && !variance_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100524 RTC_LOG(LS_INFO) << "Bad call (variance) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800525 }
526
Mirko Bonadei675513b2017-11-09 11:09:25 +0100527 RTC_LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_)
528 << " fps: " << fps << " fps_bad: " << fps_bad
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200529 << " qp: " << qp.value_or(-1) << " qp_bad: " << qp_bad
Mirko Bonadei675513b2017-11-09 11:09:25 +0100530 << " variance_bad: " << variance_bad
531 << " fps_variance: " << fps_variance;
palmkvist349092b2016-12-13 02:45:57 -0800532
533 last_sample_time_ = now;
534 qp_sample_.Reset();
palmkvista40672a2017-01-13 05:58:34 -0800535
536 if (fps_threshold_.IsHigh() || variance_threshold_.IsHigh() ||
537 qp_threshold_.IsHigh()) {
538 if (any_bad)
539 ++num_bad_states_;
540 ++num_certain_states_;
541 }
palmkvist349092b2016-12-13 02:45:57 -0800542}
543
asapersson0255acb2017-03-28 02:44:58 -0700544void ReceiveStatisticsProxy::UpdateFramerate(int64_t now_ms) const {
philipela45102f2017-02-22 05:30:39 -0800545 int64_t old_frames_ms = now_ms - kRateStatisticsWindowSizeMs;
546 while (!frame_window_.empty() &&
547 frame_window_.begin()->first < old_frames_ms) {
philipela45102f2017-02-22 05:30:39 -0800548 frame_window_.erase(frame_window_.begin());
549 }
550
551 size_t framerate =
552 (frame_window_.size() * 1000 + 500) / kRateStatisticsWindowSizeMs;
philipela45102f2017-02-22 05:30:39 -0800553 stats_.network_frame_rate = static_cast<int>(framerate);
philipela45102f2017-02-22 05:30:39 -0800554}
555
sprang@webrtc.org09315702014-02-07 12:06:29 +0000556VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const {
Peter Boströmf2f82832015-05-01 13:00:41 +0200557 rtc::CritScope lock(&crit_);
sprang948b2752017-05-04 02:47:13 -0700558 // Get current frame rates here, as only updating them on new frames prevents
559 // us from ever correctly displaying frame rate of 0.
560 int64_t now_ms = clock_->TimeInMilliseconds();
561 UpdateFramerate(now_ms);
562 stats_.render_frame_rate = renders_fps_estimator_.Rate(now_ms).value_or(0);
563 stats_.decode_frame_rate = decode_fps_estimator_.Rate(now_ms).value_or(0);
ilnika79cc282017-08-23 05:24:10 -0700564 stats_.interframe_delay_max_ms =
565 interframe_delay_max_moving_.Max(now_ms).value_or(-1);
Sergey Silkin02371062019-01-31 16:45:42 +0100566 stats_.freeze_count = video_quality_observer_->NumFreezes();
567 stats_.pause_count = video_quality_observer_->NumPauses();
568 stats_.total_freezes_duration_ms =
569 video_quality_observer_->TotalFreezesDurationMs();
570 stats_.total_pauses_duration_ms =
571 video_quality_observer_->TotalPausesDurationMs();
572 stats_.total_frames_duration_ms =
573 video_quality_observer_->TotalFramesDurationMs();
574 stats_.sum_squared_frame_durations =
575 video_quality_observer_->SumSquaredFrameDurationsSec();
ilnik2e1b40b2017-09-04 07:57:17 -0700576 stats_.content_type = last_content_type_;
Sergey Silkin02371062019-01-31 16:45:42 +0100577 stats_.timing_frame_info = timing_frame_info_counter_.Max(now_ms);
Guido Urdaneta67378412019-05-28 17:38:08 +0200578 stats_.jitter_buffer_delay_seconds =
579 static_cast<double>(current_delay_counter_.Sum(1).value_or(0)) /
580 rtc::kNumMillisecsPerSec;
581 stats_.jitter_buffer_emitted_count = current_delay_counter_.NumSamples();
pbos@webrtc.org55707692014-12-19 15:45:03 +0000582 return stats_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000583}
584
pbosf42376c2015-08-28 07:35:32 -0700585void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) {
586 rtc::CritScope lock(&crit_);
587 stats_.current_payload_type = payload_type;
588}
589
Peter Boströmb7d9a972015-12-18 16:01:11 +0100590void ReceiveStatisticsProxy::OnDecoderImplementationName(
591 const char* implementation_name) {
592 rtc::CritScope lock(&crit_);
593 stats_.decoder_implementation_name = implementation_name;
594}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000595
philipela45102f2017-02-22 05:30:39 -0800596void ReceiveStatisticsProxy::OnFrameBufferTimingsUpdated(
philipela45102f2017-02-22 05:30:39 -0800597 int max_decode_ms,
598 int current_delay_ms,
599 int target_delay_ms,
600 int jitter_buffer_ms,
601 int min_playout_delay_ms,
602 int render_delay_ms) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200603 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000604 stats_.max_decode_ms = max_decode_ms;
605 stats_.current_delay_ms = current_delay_ms;
606 stats_.target_delay_ms = target_delay_ms;
607 stats_.jitter_buffer_ms = jitter_buffer_ms;
608 stats_.min_playout_delay_ms = min_playout_delay_ms;
609 stats_.render_delay_ms = render_delay_ms;
asapersson8688a4e2016-04-27 23:42:35 -0700610 jitter_buffer_delay_counter_.Add(jitter_buffer_ms);
611 target_delay_counter_.Add(target_delay_ms);
612 current_delay_counter_.Add(current_delay_ms);
asaperssona1862882016-04-18 00:41:05 -0700613 // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time +
614 // render delay).
philipela45102f2017-02-22 05:30:39 -0800615 delay_counter_.Add(target_delay_ms + avg_rtt_ms_ / 2);
pbos@webrtc.org98c04b32014-12-18 13:12:52 +0000616}
617
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100618void ReceiveStatisticsProxy::OnUniqueFramesCounted(int num_unique_frames) {
619 rtc::CritScope lock(&crit_);
620 num_unique_frames_.emplace(num_unique_frames);
621}
622
ilnik2edc6842017-07-06 03:06:50 -0700623void ReceiveStatisticsProxy::OnTimingFrameInfoUpdated(
624 const TimingFrameInfo& info) {
625 rtc::CritScope lock(&crit_);
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800626 if (info.flags != VideoSendTiming::kInvalid) {
627 int64_t now_ms = clock_->TimeInMilliseconds();
628 timing_frame_info_counter_.Add(info, now_ms);
629 }
Benjamin Wright514f0842018-12-10 09:55:17 -0800630
631 // Measure initial decoding latency between the first frame arriving and the
632 // first frame being decoded.
633 if (!first_frame_received_time_ms_.has_value()) {
634 first_frame_received_time_ms_ = info.receive_finish_ms;
635 }
636 if (stats_.first_frame_received_to_decoded_ms == -1 &&
637 first_decoded_frame_time_ms_) {
638 stats_.first_frame_received_to_decoded_ms =
639 *first_decoded_frame_time_ms_ - *first_frame_received_time_ms_;
640 }
ilnik2edc6842017-07-06 03:06:50 -0700641}
642
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000643void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated(
644 uint32_t ssrc,
645 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200646 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000647 if (stats_.ssrc != ssrc)
648 return;
649 stats_.rtcp_packet_type_counts = packet_counter;
650}
651
Niels Möller4d7c4052019-08-05 12:45:19 +0200652void ReceiveStatisticsProxy::OnCname(uint32_t ssrc, absl::string_view cname) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200653 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700654 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000655 // receive stats from one of them.
656 if (stats_.ssrc != ssrc)
657 return;
Niels Möller4d7c4052019-08-05 12:45:19 +0200658 stats_.c_name = std::string(cname);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000659}
660
Sergey Silkin278f8252019-01-09 14:37:40 +0100661void ReceiveStatisticsProxy::OnDecodedFrame(const VideoFrame& frame,
662 absl::optional<uint8_t> qp,
Johannes Kronbfd343b2019-07-01 10:07:50 +0200663 int32_t decode_time_ms,
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200664 VideoContentType content_type) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200665 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700666
Sergey Silkin278f8252019-01-09 14:37:40 +0100667 uint64_t now_ms = clock_->TimeInMilliseconds();
Ilya Nikolaevskiy3f670e02017-10-10 11:18:49 +0200668
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200669 if (videocontenttypehelpers::IsScreenshare(content_type) !=
670 videocontenttypehelpers::IsScreenshare(last_content_type_)) {
Niels Möller9a9f18a2019-08-02 13:52:37 +0200671 // Reset the quality observer if content type is switched. But first report
672 // stats for the previous part of the call.
673 video_quality_observer_->UpdateHistograms();
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200674 video_quality_observer_.reset(new VideoQualityObserver(content_type));
675 }
676
Sergey Silkin278f8252019-01-09 14:37:40 +0100677 video_quality_observer_->OnDecodedFrame(frame, qp, last_codec_type_);
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200678
ilnik6d5b4d62017-08-30 03:32:14 -0700679 ContentSpecificStats* content_specific_stats =
680 &content_specific_stats_[content_type];
sakale5ba44e2016-10-26 07:09:24 -0700681 ++stats_.frames_decoded;
sakalcc452e12017-02-09 04:53:45 -0800682 if (qp) {
683 if (!stats_.qp_sum) {
684 if (stats_.frames_decoded != 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100685 RTC_LOG(LS_WARNING)
sakalcc452e12017-02-09 04:53:45 -0800686 << "Frames decoded was not 1 when first qp value was received.";
sakalcc452e12017-02-09 04:53:45 -0800687 }
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100688 stats_.qp_sum = 0;
sakalcc452e12017-02-09 04:53:45 -0800689 }
690 *stats_.qp_sum += *qp;
ilnik6d5b4d62017-08-30 03:32:14 -0700691 content_specific_stats->qp_counter.Add(*qp);
sakalcc452e12017-02-09 04:53:45 -0800692 } else if (stats_.qp_sum) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100693 RTC_LOG(LS_WARNING)
sakalcc452e12017-02-09 04:53:45 -0800694 << "QP sum was already set and no QP was given for a frame.";
Mirta Dvornicic608083b2019-10-03 15:52:36 +0200695 stats_.qp_sum.reset();
sakalcc452e12017-02-09 04:53:45 -0800696 }
Johannes Kronbfd343b2019-07-01 10:07:50 +0200697 decode_time_counter_.Add(decode_time_ms);
698 stats_.decode_ms = decode_time_ms;
699 stats_.total_decode_time_ms += decode_time_ms;
ilnik00d802b2017-04-11 10:34:31 -0700700 last_content_type_ = content_type;
Sergey Silkin278f8252019-01-09 14:37:40 +0100701 decode_fps_estimator_.Update(1, now_ms);
ilnik4257ab22017-07-03 01:15:58 -0700702 if (last_decoded_frame_time_ms_) {
Sergey Silkin278f8252019-01-09 14:37:40 +0100703 int64_t interframe_delay_ms = now_ms - *last_decoded_frame_time_ms_;
ilnik4257ab22017-07-03 01:15:58 -0700704 RTC_DCHECK_GE(interframe_delay_ms, 0);
Sergey Silkin278f8252019-01-09 14:37:40 +0100705 interframe_delay_max_moving_.Add(interframe_delay_ms, now_ms);
ilnik6d5b4d62017-08-30 03:32:14 -0700706 content_specific_stats->interframe_delay_counter.Add(interframe_delay_ms);
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200707 content_specific_stats->interframe_delay_percentiles.Add(
708 interframe_delay_ms);
ilnik6d5b4d62017-08-30 03:32:14 -0700709 content_specific_stats->flow_duration_ms += interframe_delay_ms;
ilnik4257ab22017-07-03 01:15:58 -0700710 }
Benjamin Wright514f0842018-12-10 09:55:17 -0800711 if (stats_.frames_decoded == 1) {
Sergey Silkin278f8252019-01-09 14:37:40 +0100712 first_decoded_frame_time_ms_.emplace(now_ms);
Benjamin Wright514f0842018-12-10 09:55:17 -0800713 }
Sergey Silkin278f8252019-01-09 14:37:40 +0100714 last_decoded_frame_time_ms_.emplace(now_ms);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000715}
716
asapersson1490f7a2016-09-23 02:09:46 -0700717void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) {
718 int width = frame.width();
719 int height = frame.height();
asaperssonf839dcc2015-10-08 00:41:59 -0700720 RTC_DCHECK_GT(width, 0);
721 RTC_DCHECK_GT(height, 0);
Åsa Persson81327d52018-06-05 13:34:33 +0200722 int64_t now_ms = clock_->TimeInMilliseconds();
Peter Boströmf2f82832015-05-01 13:00:41 +0200723 rtc::CritScope lock(&crit_);
Ilya Nikolaevskiycdc959f2018-10-10 13:15:09 +0200724
Sergey Silkin278f8252019-01-09 14:37:40 +0100725 video_quality_observer_->OnRenderedFrame(frame, now_ms);
Ilya Nikolaevskiycdc959f2018-10-10 13:15:09 +0200726
ilnik6d5b4d62017-08-30 03:32:14 -0700727 ContentSpecificStats* content_specific_stats =
728 &content_specific_stats_[last_content_type_];
Åsa Persson81327d52018-06-05 13:34:33 +0200729 renders_fps_estimator_.Update(1, now_ms);
hbos50cfe1f2017-01-23 07:21:55 -0800730 ++stats_.frames_rendered;
asapersson2e5cfcd2016-08-11 08:41:18 -0700731 stats_.width = width;
732 stats_.height = height;
Tim Psiaki63046262015-09-14 10:38:08 -0700733 render_fps_tracker_.AddSamples(1);
asaperssonf839dcc2015-10-08 00:41:59 -0700734 render_pixel_tracker_.AddSamples(sqrt(width * height));
ilnik6d5b4d62017-08-30 03:32:14 -0700735 content_specific_stats->received_width.Add(width);
736 content_specific_stats->received_height.Add(height);
asapersson1490f7a2016-09-23 02:09:46 -0700737
Åsa Persson81327d52018-06-05 13:34:33 +0200738 // Consider taking stats_.render_delay_ms into account.
739 const int64_t time_until_rendering_ms = frame.render_time_ms() - now_ms;
740 if (time_until_rendering_ms < 0) {
741 sum_missed_render_deadline_ms_ += -time_until_rendering_ms;
742 ++num_delayed_frames_rendered_;
743 }
744
asapersson1490f7a2016-09-23 02:09:46 -0700745 if (frame.ntp_time_ms() > 0) {
746 int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms();
ilnik00d802b2017-04-11 10:34:31 -0700747 if (delay_ms >= 0) {
ilnik6d5b4d62017-08-30 03:32:14 -0700748 content_specific_stats->e2e_delay_counter.Add(delay_ms);
ilnik00d802b2017-04-11 10:34:31 -0700749 }
asapersson1490f7a2016-09-23 02:09:46 -0700750 }
Niels Möller9b0b1e02019-03-22 13:56:46 +0100751 QualitySample();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000752}
753
asaperssonde9e5ff2016-11-02 07:14:03 -0700754void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t sync_offset_ms,
755 double estimated_freq_khz) {
asaperssonf8cdd182016-03-15 01:00:47 -0700756 rtc::CritScope lock(&crit_);
757 sync_offset_counter_.Add(std::abs(sync_offset_ms));
758 stats_.sync_offset_ms = sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700759
760 const double kMaxFreqKhz = 10000.0;
761 int offset_khz = kMaxFreqKhz;
762 // Should not be zero or negative. If so, report max.
763 if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0)
764 offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5);
765
766 freq_offset_counter_.Add(offset_khz);
asaperssonf8cdd182016-03-15 01:00:47 -0700767}
768
philipela45102f2017-02-22 05:30:39 -0800769void ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe,
ilnik6d5b4d62017-08-30 03:32:14 -0700770 size_t size_bytes,
771 VideoContentType content_type) {
philipela45102f2017-02-22 05:30:39 -0800772 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700773 if (is_keyframe) {
philipela45102f2017-02-22 05:30:39 -0800774 ++stats_.frame_counts.key_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700775 } else {
philipela45102f2017-02-22 05:30:39 -0800776 ++stats_.frame_counts.delta_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700777 }
778
Ilya Nikolaevskiyf203d732018-10-17 14:41:52 +0200779 // Content type extension is set only for keyframes and should be propagated
780 // for all the following delta frames. Here we may receive frames out of order
781 // and miscategorise some delta frames near the layer switch.
782 // This may slightly offset calculated bitrate and keyframes permille metrics.
783 VideoContentType propagated_content_type =
784 is_keyframe ? content_type : last_content_type_;
785
ilnik6d5b4d62017-08-30 03:32:14 -0700786 ContentSpecificStats* content_specific_stats =
Ilya Nikolaevskiyf203d732018-10-17 14:41:52 +0200787 &content_specific_stats_[propagated_content_type];
ilnik6d5b4d62017-08-30 03:32:14 -0700788
789 content_specific_stats->total_media_bytes += size_bytes;
790 if (is_keyframe) {
791 ++content_specific_stats->frame_counts.key_frames;
792 } else {
793 ++content_specific_stats->frame_counts.delta_frames;
794 }
philipela45102f2017-02-22 05:30:39 -0800795
796 int64_t now_ms = clock_->TimeInMilliseconds();
philipela45102f2017-02-22 05:30:39 -0800797 frame_window_.insert(std::make_pair(now_ms, size_bytes));
asapersson0255acb2017-03-28 02:44:58 -0700798 UpdateFramerate(now_ms);
philipela45102f2017-02-22 05:30:39 -0800799}
800
Johannes Kron0c141c52019-08-26 15:04:43 +0200801void ReceiveStatisticsProxy::OnDroppedFrames(uint32_t frames_dropped) {
802 rtc::CritScope lock(&crit_);
803 stats_.frames_dropped += frames_dropped;
804}
805
Niels Möller147013a2018-10-01 15:56:33 +0200806void ReceiveStatisticsProxy::OnPreDecode(VideoCodecType codec_type, int qp) {
Tommi132e28e2018-02-24 17:57:33 +0100807 RTC_DCHECK_RUN_ON(&decode_thread_);
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200808 rtc::CritScope lock(&crit_);
Niels Möller147013a2018-10-01 15:56:33 +0200809 last_codec_type_ = codec_type;
810 if (last_codec_type_ == kVideoCodecVP8 && qp != -1) {
811 qp_counters_.vp8.Add(qp);
812 qp_sample_.Add(qp);
asapersson86b01602015-10-20 23:55:26 -0700813 }
814}
815
sprang3e86e7e2017-08-22 09:23:28 -0700816void ReceiveStatisticsProxy::OnStreamInactive() {
817 // TODO(sprang): Figure out any other state that should be reset.
818
819 rtc::CritScope lock(&crit_);
820 // Don't report inter-frame delay if stream was paused.
821 last_decoded_frame_time_ms_.reset();
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200822 video_quality_observer_->OnStreamInactive();
sprang3e86e7e2017-08-22 09:23:28 -0700823}
824
philipela45102f2017-02-22 05:30:39 -0800825void ReceiveStatisticsProxy::OnRttUpdate(int64_t avg_rtt_ms,
826 int64_t max_rtt_ms) {
827 rtc::CritScope lock(&crit_);
828 avg_rtt_ms_ = avg_rtt_ms;
829}
830
Tommi132e28e2018-02-24 17:57:33 +0100831void ReceiveStatisticsProxy::DecoderThreadStarting() {
832 RTC_DCHECK_RUN_ON(&main_thread_);
833}
834
835void ReceiveStatisticsProxy::DecoderThreadStopped() {
836 RTC_DCHECK_RUN_ON(&main_thread_);
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200837 decode_thread_.Detach();
Tommi132e28e2018-02-24 17:57:33 +0100838}
839
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200840ReceiveStatisticsProxy::ContentSpecificStats::ContentSpecificStats()
841 : interframe_delay_percentiles(kMaxCommonInterframeDelayMs) {}
842
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200843ReceiveStatisticsProxy::ContentSpecificStats::~ContentSpecificStats() = default;
844
ilnik6d5b4d62017-08-30 03:32:14 -0700845void ReceiveStatisticsProxy::ContentSpecificStats::Add(
846 const ContentSpecificStats& other) {
847 e2e_delay_counter.Add(other.e2e_delay_counter);
848 interframe_delay_counter.Add(other.interframe_delay_counter);
849 flow_duration_ms += other.flow_duration_ms;
850 total_media_bytes += other.total_media_bytes;
851 received_height.Add(other.received_height);
852 received_width.Add(other.received_width);
853 qp_counter.Add(other.qp_counter);
854 frame_counts.key_frames += other.frame_counts.key_frames;
855 frame_counts.delta_frames += other.frame_counts.delta_frames;
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200856 interframe_delay_percentiles.Add(other.interframe_delay_percentiles);
ilnik6d5b4d62017-08-30 03:32:14 -0700857}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000858} // namespace webrtc