blob: 8ad6ef5c62a54378e6de70978b4d483206c8e5d4 [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),
asapersson0255acb2017-03-28 02:44:58 -0700108 total_byte_tracker_(100, 10u), // bucket_interval_ms, bucket_count
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200109 video_quality_observer_(
110 new VideoQualityObserver(VideoContentType::UNSPECIFIED)),
ilnika79cc282017-08-23 05:24:10 -0700111 interframe_delay_max_moving_(kMovingMaxWindowMs),
asapersson0c43f772016-11-30 01:42:26 -0800112 freq_offset_counter_(clock, nullptr, kFreqOffsetProcessIntervalMs),
philipela45102f2017-02-22 05:30:39 -0800113 first_report_block_time_ms_(-1),
ilnik00d802b2017-04-11 10:34:31 -0700114 avg_rtt_ms_(0),
ilnik75204c52017-09-04 03:35:40 -0700115 last_content_type_(VideoContentType::UNSPECIFIED),
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200116 last_codec_type_(kVideoCodecVP8),
Åsa Persson81327d52018-06-05 13:34:33 +0200117 num_delayed_frames_rendered_(0),
118 sum_missed_render_deadline_ms_(0),
ilnik75204c52017-09-04 03:35:40 -0700119 timing_frame_info_counter_(kMovingMaxWindowMs) {
Tommi132e28e2018-02-24 17:57:33 +0100120 decode_thread_.DetachFromThread();
121 network_thread_.DetachFromThread();
Tommi733b5472016-06-10 17:58:01 +0200122 stats_.ssrc = config_.rtp.remote_ssrc;
brandtr14742122017-01-27 04:53:07 -0800123 // TODO(brandtr): Replace |rtx_stats_| with a single instance of
124 // StreamDataCounters.
125 if (config_.rtp.rtx_ssrc) {
126 rtx_stats_[config_.rtp.rtx_ssrc] = StreamDataCounters();
127 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000128}
129
Åsa Persson3c391cb2015-04-27 10:09:49 +0200130ReceiveStatisticsProxy::~ReceiveStatisticsProxy() {
Tommi132e28e2018-02-24 17:57:33 +0100131 RTC_DCHECK_RUN_ON(&main_thread_);
132 // In case you're reading this wondering "hmm... we're on the main thread but
133 // calling a method that needs to be called on the decoder thread...", then
134 // here's what's going on:
135 // - The decoder thread has been stopped and DecoderThreadStopped() has been
136 // called.
137 // - The decode_thread_ thread checker has been detached, and will now become
138 // attached to the current thread, which is OK since we're in the dtor.
Åsa Persson3c391cb2015-04-27 10:09:49 +0200139 UpdateHistograms();
140}
141
asaperssond89920b2015-07-22 06:52:00 -0700142void ReceiveStatisticsProxy::UpdateHistograms() {
Tommi132e28e2018-02-24 17:57:33 +0100143 RTC_DCHECK_RUN_ON(&decode_thread_);
Karl Wiberg881f1682018-03-08 15:03:23 +0100144 char log_stream_buf[8 * 1024];
145 rtc::SimpleStringBuilder log_stream(log_stream_buf);
ilnik6d5b4d62017-08-30 03:32:14 -0700146 int stream_duration_sec = (clock_->TimeInMilliseconds() - start_ms_) / 1000;
147 if (stats_.frame_counts.key_frames > 0 ||
148 stats_.frame_counts.delta_frames > 0) {
149 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Video.ReceiveStreamLifetimeInSeconds",
150 stream_duration_sec);
Tommifef05002018-02-27 13:51:08 +0100151 log_stream << "WebRTC.Video.ReceiveStreamLifetimeInSeconds "
152 << stream_duration_sec << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700153 }
asapersson4374a092016-07-27 00:39:09 -0700154
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200155 log_stream << "Frames decoded " << stats_.frames_decoded << '\n';
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100156
157 if (num_unique_frames_) {
158 int num_dropped_frames = *num_unique_frames_ - stats_.frames_decoded;
159 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DroppedFrames.Receiver",
160 num_dropped_frames);
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200161 log_stream << "WebRTC.Video.DroppedFrames.Receiver " << num_dropped_frames
162 << '\n';
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100163 }
164
asapersson0c43f772016-11-30 01:42:26 -0800165 if (first_report_block_time_ms_ != -1 &&
166 ((clock_->TimeInMilliseconds() - first_report_block_time_ms_) / 1000) >=
167 metrics::kMinRunTimeInSeconds) {
168 int fraction_lost = report_block_stats_.FractionLostInPercent();
169 if (fraction_lost != -1) {
170 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedPacketsLostInPercent",
171 fraction_lost);
Tommifef05002018-02-27 13:51:08 +0100172 log_stream << "WebRTC.Video.ReceivedPacketsLostInPercent "
173 << fraction_lost << '\n';
asapersson0c43f772016-11-30 01:42:26 -0800174 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200175 }
asapersson0c43f772016-11-30 01:42:26 -0800176
Åsa Perssonb9b07ea2018-01-24 17:04:07 +0100177 if (first_decoded_frame_time_ms_) {
178 const int64_t elapsed_ms =
179 (clock_->TimeInMilliseconds() - *first_decoded_frame_time_ms_);
180 if (elapsed_ms >=
181 metrics::kMinRunTimeInSeconds * rtc::kNumMillisecsPerSec) {
182 RTC_HISTOGRAM_COUNTS_100(
183 "WebRTC.Video.DecodedFramesPerSecond",
184 static_cast<int>((stats_.frames_decoded * 1000.0f / elapsed_ms) +
185 0.5f));
Åsa Persson81327d52018-06-05 13:34:33 +0200186
187 const uint32_t frames_rendered = stats_.frames_rendered;
188 if (frames_rendered > 0) {
189 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.DelayedFramesToRenderer",
190 static_cast<int>(num_delayed_frames_rendered_ *
191 100 / frames_rendered));
192 if (num_delayed_frames_rendered_ > 0) {
193 RTC_HISTOGRAM_COUNTS_1000(
194 "WebRTC.Video.DelayedFramesToRenderer_AvgDelayInMs",
195 static_cast<int>(sum_missed_render_deadline_ms_ /
196 num_delayed_frames_rendered_));
197 }
198 }
Åsa Perssonb9b07ea2018-01-24 17:04:07 +0100199 }
200 }
201
asapersson6718e972015-07-24 00:20:58 -0700202 const int kMinRequiredSamples = 200;
asaperssonf839dcc2015-10-08 00:41:59 -0700203 int samples = static_cast<int>(render_fps_tracker_.TotalSampleCount());
asapersson2077f2f2017-05-11 05:37:35 -0700204 if (samples >= kMinRequiredSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700205 RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.RenderFramesPerSecond",
206 round(render_fps_tracker_.ComputeTotalRate()));
207 RTC_HISTOGRAM_COUNTS_100000(
asapersson28ba9272016-01-25 05:58:23 -0800208 "WebRTC.Video.RenderSqrtPixelsPerSecond",
Tim Psiakiad13d2f2015-11-10 16:34:50 -0800209 round(render_pixel_tracker_.ComputeTotalRate()));
asaperssonf839dcc2015-10-08 00:41:59 -0700210 }
ilnik6d5b4d62017-08-30 03:32:14 -0700211
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200212 absl::optional<int> sync_offset_ms =
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200213 sync_offset_counter_.Avg(kMinRequiredSamples);
214 if (sync_offset_ms) {
215 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.AVSyncOffsetInMs",
216 *sync_offset_ms);
217 log_stream << "WebRTC.Video.AVSyncOffsetInMs " << *sync_offset_ms << '\n';
pbos35fdb2a2016-05-03 03:32:10 -0700218 }
asaperssonde9e5ff2016-11-02 07:14:03 -0700219 AggregatedStats freq_offset_stats = freq_offset_counter_.GetStats();
220 if (freq_offset_stats.num_samples > 0) {
221 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtpToNtpFreqOffsetInKhz",
222 freq_offset_stats.average);
Tommifef05002018-02-27 13:51:08 +0100223 log_stream << "WebRTC.Video.RtpToNtpFreqOffsetInKhz "
224 << freq_offset_stats.ToString() << '\n';
asaperssonde9e5ff2016-11-02 07:14:03 -0700225 }
asaperssonf8cdd182016-03-15 01:00:47 -0700226
asaperssonb99baf82017-04-20 04:05:43 -0700227 int num_total_frames =
228 stats_.frame_counts.key_frames + stats_.frame_counts.delta_frames;
229 if (num_total_frames >= kMinRequiredSamples) {
230 int num_key_frames = stats_.frame_counts.key_frames;
philipela45102f2017-02-22 05:30:39 -0800231 int key_frames_permille =
asaperssonb99baf82017-04-20 04:05:43 -0700232 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
philipela45102f2017-02-22 05:30:39 -0800233 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesReceivedInPermille",
234 key_frames_permille);
Tommifef05002018-02-27 13:51:08 +0100235 log_stream << "WebRTC.Video.KeyFramesReceivedInPermille "
236 << key_frames_permille << '\n';
philipela45102f2017-02-22 05:30:39 -0800237 }
238
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200239 absl::optional<int> qp = qp_counters_.vp8.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200240 if (qp) {
241 RTC_HISTOGRAM_COUNTS_200("WebRTC.Video.Decoded.Vp8.Qp", *qp);
242 log_stream << "WebRTC.Video.Decoded.Vp8.Qp " << *qp << '\n';
asapersson2077f2f2017-05-11 05:37:35 -0700243 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200244 absl::optional<int> decode_ms = decode_time_counter_.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200245 if (decode_ms) {
246 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", *decode_ms);
247 log_stream << "WebRTC.Video.DecodeTimeInMs " << *decode_ms << '\n';
asapersson2077f2f2017-05-11 05:37:35 -0700248 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200249 absl::optional<int> jb_delay_ms =
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200250 jitter_buffer_delay_counter_.Avg(kMinRequiredSamples);
251 if (jb_delay_ms) {
philipela45102f2017-02-22 05:30:39 -0800252 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200253 *jb_delay_ms);
254 log_stream << "WebRTC.Video.JitterBufferDelayInMs " << *jb_delay_ms << '\n';
asapersson8688a4e2016-04-27 23:42:35 -0700255 }
philipela45102f2017-02-22 05:30:39 -0800256
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200257 absl::optional<int> target_delay_ms =
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200258 target_delay_counter_.Avg(kMinRequiredSamples);
259 if (target_delay_ms) {
260 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs",
261 *target_delay_ms);
262 log_stream << "WebRTC.Video.TargetDelayInMs " << *target_delay_ms << '\n';
asapersson8688a4e2016-04-27 23:42:35 -0700263 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200264 absl::optional<int> current_delay_ms =
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200265 current_delay_counter_.Avg(kMinRequiredSamples);
266 if (current_delay_ms) {
asapersson1d02d3e2016-09-09 22:40:25 -0700267 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.CurrentDelayInMs",
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200268 *current_delay_ms);
269 log_stream << "WebRTC.Video.CurrentDelayInMs " << *current_delay_ms << '\n';
asapersson8688a4e2016-04-27 23:42:35 -0700270 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200271 absl::optional<int> delay_ms = delay_counter_.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200272 if (delay_ms)
273 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", *delay_ms);
sprang0ab8e812016-02-24 01:35:40 -0800274
ilnik6d5b4d62017-08-30 03:32:14 -0700275 // Aggregate content_specific_stats_ by removing experiment or simulcast
276 // information;
277 std::map<VideoContentType, ContentSpecificStats> aggregated_stats;
278 for (auto it : content_specific_stats_) {
279 // Calculate simulcast specific metrics (".S0" ... ".S2" suffixes).
280 VideoContentType content_type = it.first;
281 if (videocontenttypehelpers::GetSimulcastId(content_type) > 0) {
282 // Aggregate on experiment id.
283 videocontenttypehelpers::SetExperimentId(&content_type, 0);
284 aggregated_stats[content_type].Add(it.second);
285 }
286 // Calculate experiment specific metrics (".ExperimentGroup[0-7]" suffixes).
287 content_type = it.first;
288 if (videocontenttypehelpers::GetExperimentId(content_type) > 0) {
289 // Aggregate on simulcast id.
290 videocontenttypehelpers::SetSimulcastId(&content_type, 0);
291 aggregated_stats[content_type].Add(it.second);
292 }
293 // Calculate aggregated metrics (no suffixes. Aggregated on everything).
294 content_type = it.first;
295 videocontenttypehelpers::SetSimulcastId(&content_type, 0);
296 videocontenttypehelpers::SetExperimentId(&content_type, 0);
297 aggregated_stats[content_type].Add(it.second);
ilnik00d802b2017-04-11 10:34:31 -0700298 }
299
ilnik6d5b4d62017-08-30 03:32:14 -0700300 for (auto it : aggregated_stats) {
301 // For the metric Foo we report the following slices:
302 // WebRTC.Video.Foo,
303 // WebRTC.Video.Screenshare.Foo,
304 // WebRTC.Video.Foo.S[0-3],
305 // WebRTC.Video.Foo.ExperimentGroup[0-7],
306 // WebRTC.Video.Screenshare.Foo.S[0-3],
307 // WebRTC.Video.Screenshare.Foo.ExperimentGroup[0-7].
308 auto content_type = it.first;
309 auto stats = it.second;
310 std::string uma_prefix = UmaPrefixForContentType(content_type);
311 std::string uma_suffix = UmaSuffixForContentType(content_type);
312 // Metrics can be sliced on either simulcast id or experiment id but not
313 // both.
314 RTC_DCHECK(videocontenttypehelpers::GetExperimentId(content_type) == 0 ||
315 videocontenttypehelpers::GetSimulcastId(content_type) == 0);
ilnik00d802b2017-04-11 10:34:31 -0700316
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200317 absl::optional<int> e2e_delay_ms =
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200318 stats.e2e_delay_counter.Avg(kMinRequiredSamples);
319 if (e2e_delay_ms) {
ilnik6d5b4d62017-08-30 03:32:14 -0700320 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200321 uma_prefix + ".EndToEndDelayInMs" + uma_suffix, *e2e_delay_ms);
Tommifef05002018-02-27 13:51:08 +0100322 log_stream << uma_prefix << ".EndToEndDelayInMs" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200323 << *e2e_delay_ms << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700324 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200325 absl::optional<int> e2e_delay_max_ms = stats.e2e_delay_counter.Max();
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200326 if (e2e_delay_max_ms && e2e_delay_ms) {
ilnik6d5b4d62017-08-30 03:32:14 -0700327 RTC_HISTOGRAM_COUNTS_SPARSE_100000(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200328 uma_prefix + ".EndToEndDelayMaxInMs" + uma_suffix, *e2e_delay_max_ms);
Tommifef05002018-02-27 13:51:08 +0100329 log_stream << uma_prefix << ".EndToEndDelayMaxInMs" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200330 << *e2e_delay_max_ms << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700331 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200332 absl::optional<int> interframe_delay_ms =
ilnik6d5b4d62017-08-30 03:32:14 -0700333 stats.interframe_delay_counter.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200334 if (interframe_delay_ms) {
ilnik6d5b4d62017-08-30 03:32:14 -0700335 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
336 uma_prefix + ".InterframeDelayInMs" + uma_suffix,
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200337 *interframe_delay_ms);
Tommifef05002018-02-27 13:51:08 +0100338 log_stream << uma_prefix << ".InterframeDelayInMs" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200339 << *interframe_delay_ms << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700340 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200341 absl::optional<int> interframe_delay_max_ms =
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200342 stats.interframe_delay_counter.Max();
343 if (interframe_delay_max_ms && interframe_delay_ms) {
ilnik6d5b4d62017-08-30 03:32:14 -0700344 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
345 uma_prefix + ".InterframeDelayMaxInMs" + uma_suffix,
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200346 *interframe_delay_max_ms);
Tommifef05002018-02-27 13:51:08 +0100347 log_stream << uma_prefix << ".InterframeDelayMaxInMs" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200348 << *interframe_delay_max_ms << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700349 }
ilnik00d802b2017-04-11 10:34:31 -0700350
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200351 absl::optional<uint32_t> interframe_delay_95p_ms =
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200352 stats.interframe_delay_percentiles.GetPercentile(0.95f);
353 if (interframe_delay_95p_ms && interframe_delay_ms != -1) {
354 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
355 uma_prefix + ".InterframeDelay95PercentileInMs" + uma_suffix,
356 *interframe_delay_95p_ms);
Tommifef05002018-02-27 13:51:08 +0100357 log_stream << uma_prefix << ".InterframeDelay95PercentileInMs"
358 << uma_suffix << " " << *interframe_delay_95p_ms << '\n';
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200359 }
360
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200361 absl::optional<int> width = stats.received_width.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200362 if (width) {
ilnik6d5b4d62017-08-30 03:32:14 -0700363 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200364 uma_prefix + ".ReceivedWidthInPixels" + uma_suffix, *width);
Tommifef05002018-02-27 13:51:08 +0100365 log_stream << uma_prefix << ".ReceivedWidthInPixels" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200366 << *width << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700367 }
asapersson1490f7a2016-09-23 02:09:46 -0700368
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200369 absl::optional<int> height = stats.received_height.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200370 if (height) {
ilnik6d5b4d62017-08-30 03:32:14 -0700371 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200372 uma_prefix + ".ReceivedHeightInPixels" + uma_suffix, *height);
Tommifef05002018-02-27 13:51:08 +0100373 log_stream << uma_prefix << ".ReceivedHeightInPixels" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200374 << *height << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700375 }
ilnik4257ab22017-07-03 01:15:58 -0700376
ilnik6d5b4d62017-08-30 03:32:14 -0700377 if (content_type != VideoContentType::UNSPECIFIED) {
378 // Don't report these 3 metrics unsliced, as more precise variants
379 // are reported separately in this method.
380 float flow_duration_sec = stats.flow_duration_ms / 1000.0;
381 if (flow_duration_sec >= metrics::kMinRunTimeInSeconds) {
382 int media_bitrate_kbps = static_cast<int>(stats.total_media_bytes * 8 /
383 flow_duration_sec / 1000);
384 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
385 uma_prefix + ".MediaBitrateReceivedInKbps" + uma_suffix,
386 media_bitrate_kbps);
Tommifef05002018-02-27 13:51:08 +0100387 log_stream << uma_prefix << ".MediaBitrateReceivedInKbps" << uma_suffix
388 << " " << media_bitrate_kbps << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700389 }
390
391 int num_total_frames =
392 stats.frame_counts.key_frames + stats.frame_counts.delta_frames;
393 if (num_total_frames >= kMinRequiredSamples) {
394 int num_key_frames = stats.frame_counts.key_frames;
395 int key_frames_permille =
396 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
397 RTC_HISTOGRAM_COUNTS_SPARSE_1000(
398 uma_prefix + ".KeyFramesReceivedInPermille" + uma_suffix,
399 key_frames_permille);
Tommifef05002018-02-27 13:51:08 +0100400 log_stream << uma_prefix << ".KeyFramesReceivedInPermille" << uma_suffix
401 << " " << key_frames_permille << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700402 }
403
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200404 absl::optional<int> qp = stats.qp_counter.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200405 if (qp) {
ilnik6d5b4d62017-08-30 03:32:14 -0700406 RTC_HISTOGRAM_COUNTS_SPARSE_200(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200407 uma_prefix + ".Decoded.Vp8.Qp" + uma_suffix, *qp);
408 log_stream << uma_prefix << ".Decoded.Vp8.Qp" << uma_suffix << " "
409 << *qp << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700410 }
411 }
ilnik4257ab22017-07-03 01:15:58 -0700412 }
413
sprang0ab8e812016-02-24 01:35:40 -0800414 StreamDataCounters rtp = stats_.rtp_stats;
415 StreamDataCounters rtx;
416 for (auto it : rtx_stats_)
417 rtx.Add(it.second);
418 StreamDataCounters rtp_rtx = rtp;
419 rtp_rtx.Add(rtx);
420 int64_t elapsed_sec =
421 rtp_rtx.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) / 1000;
asapersson2077f2f2017-05-11 05:37:35 -0700422 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
asapersson1d02d3e2016-09-09 22:40:25 -0700423 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800424 "WebRTC.Video.BitrateReceivedInKbps",
425 static_cast<int>(rtp_rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
426 1000));
ilnik6d5b4d62017-08-30 03:32:14 -0700427 int media_bitrate_kbs =
428 static_cast<int>(rtp.MediaPayloadBytes() * 8 / elapsed_sec / 1000);
429 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.MediaBitrateReceivedInKbps",
430 media_bitrate_kbs);
Tommifef05002018-02-27 13:51:08 +0100431 log_stream << "WebRTC.Video.MediaBitrateReceivedInKbps "
432 << media_bitrate_kbs << '\n';
asapersson1d02d3e2016-09-09 22:40:25 -0700433 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800434 "WebRTC.Video.PaddingBitrateReceivedInKbps",
435 static_cast<int>(rtp_rtx.transmitted.padding_bytes * 8 / elapsed_sec /
436 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700437 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800438 "WebRTC.Video.RetransmittedBitrateReceivedInKbps",
439 static_cast<int>(rtp_rtx.retransmitted.TotalBytes() * 8 / elapsed_sec /
440 1000));
441 if (!rtx_stats_.empty()) {
asapersson1d02d3e2016-09-09 22:40:25 -0700442 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtxBitrateReceivedInKbps",
443 static_cast<int>(rtx.transmitted.TotalBytes() *
444 8 / elapsed_sec / 1000));
sprang0ab8e812016-02-24 01:35:40 -0800445 }
nisse3b3622f2017-09-26 02:49:21 -0700446 if (config_.rtp.ulpfec_payload_type != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700447 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800448 "WebRTC.Video.FecBitrateReceivedInKbps",
449 static_cast<int>(rtp_rtx.fec.TotalBytes() * 8 / elapsed_sec / 1000));
450 }
sprang07fb9be2016-02-24 07:55:00 -0800451 const RtcpPacketTypeCounter& counters = stats_.rtcp_packet_type_counts;
asapersson1d02d3e2016-09-09 22:40:25 -0700452 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.NackPacketsSentPerMinute",
453 counters.nack_packets * 60 / elapsed_sec);
454 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.FirPacketsSentPerMinute",
455 counters.fir_packets * 60 / elapsed_sec);
456 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.PliPacketsSentPerMinute",
457 counters.pli_packets * 60 / elapsed_sec);
sprang07fb9be2016-02-24 07:55:00 -0800458 if (counters.nack_requests > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700459 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.UniqueNackRequestsSentInPercent",
460 counters.UniqueNackRequestsInPercent());
sprang07fb9be2016-02-24 07:55:00 -0800461 }
sprang0ab8e812016-02-24 01:35:40 -0800462 }
palmkvista40672a2017-01-13 05:58:34 -0800463
464 if (num_certain_states_ >= kBadCallMinRequiredSamples) {
465 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Any",
466 100 * num_bad_states_ / num_certain_states_);
467 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200468 absl::optional<double> fps_fraction =
palmkvista40672a2017-01-13 05:58:34 -0800469 fps_threshold_.FractionHigh(kBadCallMinRequiredSamples);
470 if (fps_fraction) {
471 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRate",
472 static_cast<int>(100 * (1 - *fps_fraction)));
473 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200474 absl::optional<double> variance_fraction =
palmkvista40672a2017-01-13 05:58:34 -0800475 variance_threshold_.FractionHigh(kBadCallMinRequiredSamples);
476 if (variance_fraction) {
477 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRateVariance",
478 static_cast<int>(100 * *variance_fraction));
479 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200480 absl::optional<double> qp_fraction =
palmkvista40672a2017-01-13 05:58:34 -0800481 qp_threshold_.FractionHigh(kBadCallMinRequiredSamples);
482 if (qp_fraction) {
483 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Qp",
484 static_cast<int>(100 * *qp_fraction));
485 }
Tommifef05002018-02-27 13:51:08 +0100486
487 RTC_LOG(LS_INFO) << log_stream.str();
Åsa Persson3c391cb2015-04-27 10:09:49 +0200488}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000489
palmkvist349092b2016-12-13 02:45:57 -0800490void ReceiveStatisticsProxy::QualitySample() {
Tommi132e28e2018-02-24 17:57:33 +0100491 RTC_DCHECK_RUN_ON(&network_thread_);
492
palmkvist349092b2016-12-13 02:45:57 -0800493 int64_t now = clock_->TimeInMilliseconds();
494 if (last_sample_time_ + kMinSampleLengthMs > now)
495 return;
496
497 double fps =
498 render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200499 absl::optional<int> qp = qp_sample_.Avg(1);
palmkvist349092b2016-12-13 02:45:57 -0800500
501 bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true);
502 bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false);
503 bool prev_variance_bad = variance_threshold_.IsHigh().value_or(false);
504 bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad;
505
506 fps_threshold_.AddMeasurement(static_cast<int>(fps));
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200507 if (qp)
508 qp_threshold_.AddMeasurement(*qp);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200509 absl::optional<double> fps_variance_opt = fps_threshold_.CalculateVariance();
palmkvist349092b2016-12-13 02:45:57 -0800510 double fps_variance = fps_variance_opt.value_or(0);
511 if (fps_variance_opt) {
512 variance_threshold_.AddMeasurement(static_cast<int>(fps_variance));
513 }
514
515 bool fps_bad = !fps_threshold_.IsHigh().value_or(true);
516 bool qp_bad = qp_threshold_.IsHigh().value_or(false);
517 bool variance_bad = variance_threshold_.IsHigh().value_or(false);
518 bool any_bad = fps_bad || qp_bad || variance_bad;
519
520 if (!prev_any_bad && any_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100521 RTC_LOG(LS_INFO) << "Bad call (any) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800522 } else if (prev_any_bad && !any_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100523 RTC_LOG(LS_INFO) << "Bad call (any) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800524 }
525
526 if (!prev_fps_bad && fps_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100527 RTC_LOG(LS_INFO) << "Bad call (fps) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800528 } else if (prev_fps_bad && !fps_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100529 RTC_LOG(LS_INFO) << "Bad call (fps) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800530 }
531
532 if (!prev_qp_bad && qp_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100533 RTC_LOG(LS_INFO) << "Bad call (qp) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800534 } else if (prev_qp_bad && !qp_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100535 RTC_LOG(LS_INFO) << "Bad call (qp) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800536 }
537
538 if (!prev_variance_bad && variance_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100539 RTC_LOG(LS_INFO) << "Bad call (variance) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800540 } else if (prev_variance_bad && !variance_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100541 RTC_LOG(LS_INFO) << "Bad call (variance) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800542 }
543
Mirko Bonadei675513b2017-11-09 11:09:25 +0100544 RTC_LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_)
545 << " fps: " << fps << " fps_bad: " << fps_bad
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200546 << " qp: " << qp.value_or(-1) << " qp_bad: " << qp_bad
Mirko Bonadei675513b2017-11-09 11:09:25 +0100547 << " variance_bad: " << variance_bad
548 << " fps_variance: " << fps_variance;
palmkvist349092b2016-12-13 02:45:57 -0800549
550 last_sample_time_ = now;
551 qp_sample_.Reset();
palmkvista40672a2017-01-13 05:58:34 -0800552
553 if (fps_threshold_.IsHigh() || variance_threshold_.IsHigh() ||
554 qp_threshold_.IsHigh()) {
555 if (any_bad)
556 ++num_bad_states_;
557 ++num_certain_states_;
558 }
palmkvist349092b2016-12-13 02:45:57 -0800559}
560
asapersson0255acb2017-03-28 02:44:58 -0700561void ReceiveStatisticsProxy::UpdateFramerate(int64_t now_ms) const {
philipela45102f2017-02-22 05:30:39 -0800562 int64_t old_frames_ms = now_ms - kRateStatisticsWindowSizeMs;
563 while (!frame_window_.empty() &&
564 frame_window_.begin()->first < old_frames_ms) {
philipela45102f2017-02-22 05:30:39 -0800565 frame_window_.erase(frame_window_.begin());
566 }
567
568 size_t framerate =
569 (frame_window_.size() * 1000 + 500) / kRateStatisticsWindowSizeMs;
philipela45102f2017-02-22 05:30:39 -0800570 stats_.network_frame_rate = static_cast<int>(framerate);
philipela45102f2017-02-22 05:30:39 -0800571}
572
sprang@webrtc.org09315702014-02-07 12:06:29 +0000573VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const {
Peter Boströmf2f82832015-05-01 13:00:41 +0200574 rtc::CritScope lock(&crit_);
sprang948b2752017-05-04 02:47:13 -0700575 // Get current frame rates here, as only updating them on new frames prevents
576 // us from ever correctly displaying frame rate of 0.
577 int64_t now_ms = clock_->TimeInMilliseconds();
578 UpdateFramerate(now_ms);
579 stats_.render_frame_rate = renders_fps_estimator_.Rate(now_ms).value_or(0);
580 stats_.decode_frame_rate = decode_fps_estimator_.Rate(now_ms).value_or(0);
asapersson0255acb2017-03-28 02:44:58 -0700581 stats_.total_bitrate_bps =
582 static_cast<int>(total_byte_tracker_.ComputeRate() * 8);
ilnika79cc282017-08-23 05:24:10 -0700583 stats_.interframe_delay_max_ms =
584 interframe_delay_max_moving_.Max(now_ms).value_or(-1);
ilnik75204c52017-09-04 03:35:40 -0700585 stats_.timing_frame_info = timing_frame_info_counter_.Max(now_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700586 stats_.content_type = last_content_type_;
pbos@webrtc.org55707692014-12-19 15:45:03 +0000587 return stats_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000588}
589
pbosf42376c2015-08-28 07:35:32 -0700590void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) {
591 rtc::CritScope lock(&crit_);
592 stats_.current_payload_type = payload_type;
593}
594
Peter Boströmb7d9a972015-12-18 16:01:11 +0100595void ReceiveStatisticsProxy::OnDecoderImplementationName(
596 const char* implementation_name) {
597 rtc::CritScope lock(&crit_);
598 stats_.decoder_implementation_name = implementation_name;
599}
pbosf42376c2015-08-28 07:35:32 -0700600void ReceiveStatisticsProxy::OnIncomingRate(unsigned int framerate,
601 unsigned int bitrate_bps) {
Tommi132e28e2018-02-24 17:57:33 +0100602 RTC_DCHECK_RUN_ON(&network_thread_);
Peter Boströmf2f82832015-05-01 13:00:41 +0200603 rtc::CritScope lock(&crit_);
palmkvista40672a2017-01-13 05:58:34 -0800604 if (stats_.rtp_stats.first_packet_time_ms != -1)
605 QualitySample();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000606}
607
philipela45102f2017-02-22 05:30:39 -0800608void ReceiveStatisticsProxy::OnFrameBufferTimingsUpdated(
609 int decode_ms,
610 int max_decode_ms,
611 int current_delay_ms,
612 int target_delay_ms,
613 int jitter_buffer_ms,
614 int min_playout_delay_ms,
615 int render_delay_ms) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200616 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000617 stats_.decode_ms = decode_ms;
618 stats_.max_decode_ms = max_decode_ms;
619 stats_.current_delay_ms = current_delay_ms;
620 stats_.target_delay_ms = target_delay_ms;
621 stats_.jitter_buffer_ms = jitter_buffer_ms;
622 stats_.min_playout_delay_ms = min_playout_delay_ms;
623 stats_.render_delay_ms = render_delay_ms;
asapersson6718e972015-07-24 00:20:58 -0700624 decode_time_counter_.Add(decode_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700625 jitter_buffer_delay_counter_.Add(jitter_buffer_ms);
626 target_delay_counter_.Add(target_delay_ms);
627 current_delay_counter_.Add(current_delay_ms);
asaperssona1862882016-04-18 00:41:05 -0700628 // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time +
629 // render delay).
philipela45102f2017-02-22 05:30:39 -0800630 delay_counter_.Add(target_delay_ms + avg_rtt_ms_ / 2);
pbos@webrtc.org98c04b32014-12-18 13:12:52 +0000631}
632
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100633void ReceiveStatisticsProxy::OnUniqueFramesCounted(int num_unique_frames) {
634 rtc::CritScope lock(&crit_);
635 num_unique_frames_.emplace(num_unique_frames);
636}
637
ilnik2edc6842017-07-06 03:06:50 -0700638void ReceiveStatisticsProxy::OnTimingFrameInfoUpdated(
639 const TimingFrameInfo& info) {
640 rtc::CritScope lock(&crit_);
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800641 if (info.flags != VideoSendTiming::kInvalid) {
642 int64_t now_ms = clock_->TimeInMilliseconds();
643 timing_frame_info_counter_.Add(info, now_ms);
644 }
Benjamin Wright514f0842018-12-10 09:55:17 -0800645
646 // Measure initial decoding latency between the first frame arriving and the
647 // first frame being decoded.
648 if (!first_frame_received_time_ms_.has_value()) {
649 first_frame_received_time_ms_ = info.receive_finish_ms;
650 }
651 if (stats_.first_frame_received_to_decoded_ms == -1 &&
652 first_decoded_frame_time_ms_) {
653 stats_.first_frame_received_to_decoded_ms =
654 *first_decoded_frame_time_ms_ - *first_frame_received_time_ms_;
655 }
ilnik2edc6842017-07-06 03:06:50 -0700656}
657
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000658void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated(
659 uint32_t ssrc,
660 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200661 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000662 if (stats_.ssrc != ssrc)
663 return;
664 stats_.rtcp_packet_type_counts = packet_counter;
665}
666
sprang@webrtc.org09315702014-02-07 12:06:29 +0000667void ReceiveStatisticsProxy::StatisticsUpdated(
668 const webrtc::RtcpStatistics& statistics,
669 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200670 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700671 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000672 // receive stats from one of them.
673 if (stats_.ssrc != ssrc)
674 return;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000675 stats_.rtcp_stats = statistics;
Åsa Persson3c391cb2015-04-27 10:09:49 +0200676 report_block_stats_.Store(statistics, ssrc, 0);
asapersson0c43f772016-11-30 01:42:26 -0800677
678 if (first_report_block_time_ms_ == -1)
679 first_report_block_time_ms_ = clock_->TimeInMilliseconds();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000680}
681
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000682void ReceiveStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200683 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700684 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000685 // receive stats from one of them.
686 if (stats_.ssrc != ssrc)
687 return;
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000688 stats_.c_name = cname;
689}
690
sprang@webrtc.org09315702014-02-07 12:06:29 +0000691void ReceiveStatisticsProxy::DataCountersUpdated(
692 const webrtc::StreamDataCounters& counters,
693 uint32_t ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700694 size_t last_total_bytes = 0;
695 size_t total_bytes = 0;
Peter Boströmf2f82832015-05-01 13:00:41 +0200696 rtc::CritScope lock(&crit_);
sprang0ab8e812016-02-24 01:35:40 -0800697 if (ssrc == stats_.ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700698 last_total_bytes = stats_.rtp_stats.transmitted.TotalBytes();
699 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800700 stats_.rtp_stats = counters;
701 } else {
702 auto it = rtx_stats_.find(ssrc);
703 if (it != rtx_stats_.end()) {
asapersson0255acb2017-03-28 02:44:58 -0700704 last_total_bytes = it->second.transmitted.TotalBytes();
705 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800706 it->second = counters;
707 } else {
708 RTC_NOTREACHED() << "Unexpected stream ssrc: " << ssrc;
709 }
710 }
asapersson0255acb2017-03-28 02:44:58 -0700711 if (total_bytes > last_total_bytes)
712 total_byte_tracker_.AddSamples(total_bytes - last_total_bytes);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000713}
714
Sergey Silkin278f8252019-01-09 14:37:40 +0100715void ReceiveStatisticsProxy::OnDecodedFrame(const VideoFrame& frame,
716 absl::optional<uint8_t> qp,
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200717 VideoContentType content_type) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200718 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700719
Sergey Silkin278f8252019-01-09 14:37:40 +0100720 uint64_t now_ms = clock_->TimeInMilliseconds();
Ilya Nikolaevskiy3f670e02017-10-10 11:18:49 +0200721
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200722 if (videocontenttypehelpers::IsScreenshare(content_type) !=
723 videocontenttypehelpers::IsScreenshare(last_content_type_)) {
724 // Reset the quality observer if content type is switched. This will
725 // report stats for the previous part of the call.
726 video_quality_observer_.reset(new VideoQualityObserver(content_type));
727 }
728
Sergey Silkin278f8252019-01-09 14:37:40 +0100729 video_quality_observer_->OnDecodedFrame(frame, qp, last_codec_type_);
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200730
ilnik6d5b4d62017-08-30 03:32:14 -0700731 ContentSpecificStats* content_specific_stats =
732 &content_specific_stats_[content_type];
sakale5ba44e2016-10-26 07:09:24 -0700733 ++stats_.frames_decoded;
sakalcc452e12017-02-09 04:53:45 -0800734 if (qp) {
735 if (!stats_.qp_sum) {
736 if (stats_.frames_decoded != 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100737 RTC_LOG(LS_WARNING)
sakalcc452e12017-02-09 04:53:45 -0800738 << "Frames decoded was not 1 when first qp value was received.";
739 stats_.frames_decoded = 1;
740 }
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100741 stats_.qp_sum = 0;
sakalcc452e12017-02-09 04:53:45 -0800742 }
743 *stats_.qp_sum += *qp;
ilnik6d5b4d62017-08-30 03:32:14 -0700744 content_specific_stats->qp_counter.Add(*qp);
sakalcc452e12017-02-09 04:53:45 -0800745 } else if (stats_.qp_sum) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100746 RTC_LOG(LS_WARNING)
sakalcc452e12017-02-09 04:53:45 -0800747 << "QP sum was already set and no QP was given for a frame.";
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200748 stats_.qp_sum = absl::nullopt;
sakalcc452e12017-02-09 04:53:45 -0800749 }
ilnik00d802b2017-04-11 10:34:31 -0700750 last_content_type_ = content_type;
Sergey Silkin278f8252019-01-09 14:37:40 +0100751 decode_fps_estimator_.Update(1, now_ms);
ilnik4257ab22017-07-03 01:15:58 -0700752 if (last_decoded_frame_time_ms_) {
Sergey Silkin278f8252019-01-09 14:37:40 +0100753 int64_t interframe_delay_ms = now_ms - *last_decoded_frame_time_ms_;
ilnik4257ab22017-07-03 01:15:58 -0700754 RTC_DCHECK_GE(interframe_delay_ms, 0);
Sergey Silkin278f8252019-01-09 14:37:40 +0100755 interframe_delay_max_moving_.Add(interframe_delay_ms, now_ms);
ilnik6d5b4d62017-08-30 03:32:14 -0700756 content_specific_stats->interframe_delay_counter.Add(interframe_delay_ms);
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200757 content_specific_stats->interframe_delay_percentiles.Add(
758 interframe_delay_ms);
ilnik6d5b4d62017-08-30 03:32:14 -0700759 content_specific_stats->flow_duration_ms += interframe_delay_ms;
ilnik4257ab22017-07-03 01:15:58 -0700760 }
Benjamin Wright514f0842018-12-10 09:55:17 -0800761 if (stats_.frames_decoded == 1) {
Sergey Silkin278f8252019-01-09 14:37:40 +0100762 first_decoded_frame_time_ms_.emplace(now_ms);
Benjamin Wright514f0842018-12-10 09:55:17 -0800763 }
Sergey Silkin278f8252019-01-09 14:37:40 +0100764 last_decoded_frame_time_ms_.emplace(now_ms);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000765}
766
asapersson1490f7a2016-09-23 02:09:46 -0700767void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) {
768 int width = frame.width();
769 int height = frame.height();
asaperssonf839dcc2015-10-08 00:41:59 -0700770 RTC_DCHECK_GT(width, 0);
771 RTC_DCHECK_GT(height, 0);
Åsa Persson81327d52018-06-05 13:34:33 +0200772 int64_t now_ms = clock_->TimeInMilliseconds();
Peter Boströmf2f82832015-05-01 13:00:41 +0200773 rtc::CritScope lock(&crit_);
Ilya Nikolaevskiycdc959f2018-10-10 13:15:09 +0200774
Sergey Silkin278f8252019-01-09 14:37:40 +0100775 video_quality_observer_->OnRenderedFrame(frame, now_ms);
Ilya Nikolaevskiycdc959f2018-10-10 13:15:09 +0200776
ilnik6d5b4d62017-08-30 03:32:14 -0700777 ContentSpecificStats* content_specific_stats =
778 &content_specific_stats_[last_content_type_];
Åsa Persson81327d52018-06-05 13:34:33 +0200779 renders_fps_estimator_.Update(1, now_ms);
hbos50cfe1f2017-01-23 07:21:55 -0800780 ++stats_.frames_rendered;
asapersson2e5cfcd2016-08-11 08:41:18 -0700781 stats_.width = width;
782 stats_.height = height;
Tim Psiaki63046262015-09-14 10:38:08 -0700783 render_fps_tracker_.AddSamples(1);
asaperssonf839dcc2015-10-08 00:41:59 -0700784 render_pixel_tracker_.AddSamples(sqrt(width * height));
ilnik6d5b4d62017-08-30 03:32:14 -0700785 content_specific_stats->received_width.Add(width);
786 content_specific_stats->received_height.Add(height);
asapersson1490f7a2016-09-23 02:09:46 -0700787
Åsa Persson81327d52018-06-05 13:34:33 +0200788 // Consider taking stats_.render_delay_ms into account.
789 const int64_t time_until_rendering_ms = frame.render_time_ms() - now_ms;
790 if (time_until_rendering_ms < 0) {
791 sum_missed_render_deadline_ms_ += -time_until_rendering_ms;
792 ++num_delayed_frames_rendered_;
793 }
794
asapersson1490f7a2016-09-23 02:09:46 -0700795 if (frame.ntp_time_ms() > 0) {
796 int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms();
ilnik00d802b2017-04-11 10:34:31 -0700797 if (delay_ms >= 0) {
ilnik6d5b4d62017-08-30 03:32:14 -0700798 content_specific_stats->e2e_delay_counter.Add(delay_ms);
ilnik00d802b2017-04-11 10:34:31 -0700799 }
asapersson1490f7a2016-09-23 02:09:46 -0700800 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000801}
802
asaperssonde9e5ff2016-11-02 07:14:03 -0700803void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t sync_offset_ms,
804 double estimated_freq_khz) {
asaperssonf8cdd182016-03-15 01:00:47 -0700805 rtc::CritScope lock(&crit_);
806 sync_offset_counter_.Add(std::abs(sync_offset_ms));
807 stats_.sync_offset_ms = sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700808
809 const double kMaxFreqKhz = 10000.0;
810 int offset_khz = kMaxFreqKhz;
811 // Should not be zero or negative. If so, report max.
812 if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0)
813 offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5);
814
815 freq_offset_counter_.Add(offset_khz);
asaperssonf8cdd182016-03-15 01:00:47 -0700816}
817
pbos@webrtc.org55707692014-12-19 15:45:03 +0000818void ReceiveStatisticsProxy::OnReceiveRatesUpdated(uint32_t bitRate,
Yves Gerey665174f2018-06-19 15:03:05 +0200819 uint32_t frameRate) {}
pbos@webrtc.org55707692014-12-19 15:45:03 +0000820
philipela45102f2017-02-22 05:30:39 -0800821void ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe,
ilnik6d5b4d62017-08-30 03:32:14 -0700822 size_t size_bytes,
823 VideoContentType content_type) {
philipela45102f2017-02-22 05:30:39 -0800824 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700825 if (is_keyframe) {
philipela45102f2017-02-22 05:30:39 -0800826 ++stats_.frame_counts.key_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700827 } else {
philipela45102f2017-02-22 05:30:39 -0800828 ++stats_.frame_counts.delta_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700829 }
830
Ilya Nikolaevskiyf203d732018-10-17 14:41:52 +0200831 // Content type extension is set only for keyframes and should be propagated
832 // for all the following delta frames. Here we may receive frames out of order
833 // and miscategorise some delta frames near the layer switch.
834 // This may slightly offset calculated bitrate and keyframes permille metrics.
835 VideoContentType propagated_content_type =
836 is_keyframe ? content_type : last_content_type_;
837
ilnik6d5b4d62017-08-30 03:32:14 -0700838 ContentSpecificStats* content_specific_stats =
Ilya Nikolaevskiyf203d732018-10-17 14:41:52 +0200839 &content_specific_stats_[propagated_content_type];
ilnik6d5b4d62017-08-30 03:32:14 -0700840
841 content_specific_stats->total_media_bytes += size_bytes;
842 if (is_keyframe) {
843 ++content_specific_stats->frame_counts.key_frames;
844 } else {
845 ++content_specific_stats->frame_counts.delta_frames;
846 }
philipela45102f2017-02-22 05:30:39 -0800847
848 int64_t now_ms = clock_->TimeInMilliseconds();
philipela45102f2017-02-22 05:30:39 -0800849 frame_window_.insert(std::make_pair(now_ms, size_bytes));
asapersson0255acb2017-03-28 02:44:58 -0700850 UpdateFramerate(now_ms);
philipela45102f2017-02-22 05:30:39 -0800851}
852
pbos@webrtc.org55707692014-12-19 15:45:03 +0000853void ReceiveStatisticsProxy::OnFrameCountsUpdated(
854 const FrameCounts& frame_counts) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200855 rtc::CritScope lock(&crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000856 stats_.frame_counts = frame_counts;
857}
858
pbos@webrtc.org55707692014-12-19 15:45:03 +0000859void ReceiveStatisticsProxy::OnDiscardedPacketsUpdated(int discarded_packets) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200860 rtc::CritScope lock(&crit_);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000861 stats_.discarded_packets = discarded_packets;
862}
863
Niels Möller147013a2018-10-01 15:56:33 +0200864void ReceiveStatisticsProxy::OnPreDecode(VideoCodecType codec_type, int qp) {
Tommi132e28e2018-02-24 17:57:33 +0100865 RTC_DCHECK_RUN_ON(&decode_thread_);
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200866 rtc::CritScope lock(&crit_);
Niels Möller147013a2018-10-01 15:56:33 +0200867 last_codec_type_ = codec_type;
868 if (last_codec_type_ == kVideoCodecVP8 && qp != -1) {
869 qp_counters_.vp8.Add(qp);
870 qp_sample_.Add(qp);
asapersson86b01602015-10-20 23:55:26 -0700871 }
872}
873
sprang3e86e7e2017-08-22 09:23:28 -0700874void ReceiveStatisticsProxy::OnStreamInactive() {
875 // TODO(sprang): Figure out any other state that should be reset.
876
877 rtc::CritScope lock(&crit_);
878 // Don't report inter-frame delay if stream was paused.
879 last_decoded_frame_time_ms_.reset();
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200880 video_quality_observer_->OnStreamInactive();
sprang3e86e7e2017-08-22 09:23:28 -0700881}
882
philipela45102f2017-02-22 05:30:39 -0800883void ReceiveStatisticsProxy::OnRttUpdate(int64_t avg_rtt_ms,
884 int64_t max_rtt_ms) {
885 rtc::CritScope lock(&crit_);
886 avg_rtt_ms_ = avg_rtt_ms;
887}
888
Tommi132e28e2018-02-24 17:57:33 +0100889void ReceiveStatisticsProxy::DecoderThreadStarting() {
890 RTC_DCHECK_RUN_ON(&main_thread_);
891}
892
893void ReceiveStatisticsProxy::DecoderThreadStopped() {
894 RTC_DCHECK_RUN_ON(&main_thread_);
895 decode_thread_.DetachFromThread();
896}
897
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200898ReceiveStatisticsProxy::ContentSpecificStats::ContentSpecificStats()
899 : interframe_delay_percentiles(kMaxCommonInterframeDelayMs) {}
900
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200901ReceiveStatisticsProxy::ContentSpecificStats::~ContentSpecificStats() = default;
902
ilnik6d5b4d62017-08-30 03:32:14 -0700903void ReceiveStatisticsProxy::ContentSpecificStats::Add(
904 const ContentSpecificStats& other) {
905 e2e_delay_counter.Add(other.e2e_delay_counter);
906 interframe_delay_counter.Add(other.interframe_delay_counter);
907 flow_duration_ms += other.flow_duration_ms;
908 total_media_bytes += other.total_media_bytes;
909 received_height.Add(other.received_height);
910 received_width.Add(other.received_width);
911 qp_counter.Add(other.qp_counter);
912 frame_counts.key_frames += other.frame_counts.key_frames;
913 frame_counts.delta_frames += other.frame_counts.delta_frames;
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200914 interframe_delay_percentiles.Add(other.interframe_delay_percentiles);
ilnik6d5b4d62017-08-30 03:32:14 -0700915}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000916} // namespace webrtc