blob: 9e652c119543c92fda28a0a4bc16eff132134802 [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>
ilnik6d5b4d62017-08-30 03:32:14 -070015#include <sstream>
philipela45102f2017-02-22 05:30:39 -080016#include <utility>
asaperssonf839dcc2015-10-08 00:41:59 -070017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_coding/include/video_codec_interface.h"
19#include "rtc_base/checks.h"
20#include "rtc_base/logging.h"
Åsa Perssonb9b07ea2018-01-24 17:04:07 +010021#include "rtc_base/timeutils.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
ilnik6d5b4d62017-08-30 03:32:14 -070060std::string UmaPrefixForContentType(VideoContentType content_type) {
61 std::stringstream ss;
62 ss << "WebRTC.Video";
63 if (videocontenttypehelpers::IsScreenshare(content_type)) {
64 ss << ".Screenshare";
65 }
66 return ss.str();
67}
68
69std::string UmaSuffixForContentType(VideoContentType content_type) {
70 std::stringstream ss;
71 int simulcast_id = videocontenttypehelpers::GetSimulcastId(content_type);
72 if (simulcast_id > 0) {
73 ss << ".S" << simulcast_id - 1;
74 }
75 int experiment_id = videocontenttypehelpers::GetExperimentId(content_type);
76 if (experiment_id > 0) {
77 ss << ".ExperimentGroup" << experiment_id - 1;
78 }
79 return ss.str();
80}
asaperssonde9e5ff2016-11-02 07:14:03 -070081} // namespace
sprang@webrtc.org09315702014-02-07 12:06:29 +000082
sprang0ab8e812016-02-24 01:35:40 -080083ReceiveStatisticsProxy::ReceiveStatisticsProxy(
Tommi733b5472016-06-10 17:58:01 +020084 const VideoReceiveStream::Config* config,
sprang0ab8e812016-02-24 01:35:40 -080085 Clock* clock)
pbos@webrtc.org55707692014-12-19 15:45:03 +000086 : clock_(clock),
Tommi733b5472016-06-10 17:58:01 +020087 config_(*config),
asapersson4374a092016-07-27 00:39:09 -070088 start_ms_(clock->TimeInMilliseconds()),
palmkvist349092b2016-12-13 02:45:57 -080089 last_sample_time_(clock->TimeInMilliseconds()),
90 fps_threshold_(kLowFpsThreshold,
91 kHighFpsThreshold,
92 kBadFraction,
93 kNumMeasurements),
94 qp_threshold_(kLowQpThresholdVp8,
95 kHighQpThresholdVp8,
96 kBadFraction,
97 kNumMeasurements),
98 variance_threshold_(kLowVarianceThreshold,
99 kHighVarianceThreshold,
100 kBadFraction,
101 kNumMeasurementsVariance),
palmkvista40672a2017-01-13 05:58:34 -0800102 num_bad_states_(0),
103 num_certain_states_(0),
sprang@webrtc.org09315702014-02-07 12:06:29 +0000104 // 1000ms window, scale 1000 for ms to s.
105 decode_fps_estimator_(1000, 1000),
Tim Psiaki63046262015-09-14 10:38:08 -0700106 renders_fps_estimator_(1000, 1000),
Honghai Zhang82d78622016-05-06 11:29:15 -0700107 render_fps_tracker_(100, 10u),
asaperssonde9e5ff2016-11-02 07:14:03 -0700108 render_pixel_tracker_(100, 10u),
asapersson0255acb2017-03-28 02:44:58 -0700109 total_byte_tracker_(100, 10u), // bucket_interval_ms, bucket_count
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),
philipela45102f2017-02-22 05:30:39 -0800112 first_report_block_time_ms_(-1),
ilnik00d802b2017-04-11 10:34:31 -0700113 avg_rtt_ms_(0),
ilnik75204c52017-09-04 03:35:40 -0700114 last_content_type_(VideoContentType::UNSPECIFIED),
115 timing_frame_info_counter_(kMovingMaxWindowMs) {
Tommi733b5472016-06-10 17:58:01 +0200116 stats_.ssrc = config_.rtp.remote_ssrc;
brandtr14742122017-01-27 04:53:07 -0800117 // TODO(brandtr): Replace |rtx_stats_| with a single instance of
118 // StreamDataCounters.
119 if (config_.rtp.rtx_ssrc) {
120 rtx_stats_[config_.rtp.rtx_ssrc] = StreamDataCounters();
121 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000122}
123
Åsa Persson3c391cb2015-04-27 10:09:49 +0200124ReceiveStatisticsProxy::~ReceiveStatisticsProxy() {
125 UpdateHistograms();
126}
127
asaperssond89920b2015-07-22 06:52:00 -0700128void ReceiveStatisticsProxy::UpdateHistograms() {
ilnik6d5b4d62017-08-30 03:32:14 -0700129 int stream_duration_sec = (clock_->TimeInMilliseconds() - start_ms_) / 1000;
130 if (stats_.frame_counts.key_frames > 0 ||
131 stats_.frame_counts.delta_frames > 0) {
132 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Video.ReceiveStreamLifetimeInSeconds",
133 stream_duration_sec);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100134 RTC_LOG(LS_INFO) << "WebRTC.Video.ReceiveStreamLifetimeInSeconds "
135 << stream_duration_sec;
ilnik6d5b4d62017-08-30 03:32:14 -0700136 }
asapersson4374a092016-07-27 00:39:09 -0700137
asapersson0c43f772016-11-30 01:42:26 -0800138 if (first_report_block_time_ms_ != -1 &&
139 ((clock_->TimeInMilliseconds() - first_report_block_time_ms_) / 1000) >=
140 metrics::kMinRunTimeInSeconds) {
141 int fraction_lost = report_block_stats_.FractionLostInPercent();
142 if (fraction_lost != -1) {
143 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedPacketsLostInPercent",
144 fraction_lost);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100145 RTC_LOG(LS_INFO) << "WebRTC.Video.ReceivedPacketsLostInPercent "
146 << fraction_lost;
asapersson0c43f772016-11-30 01:42:26 -0800147 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200148 }
asapersson0c43f772016-11-30 01:42:26 -0800149
Åsa Perssonb9b07ea2018-01-24 17:04:07 +0100150 if (first_decoded_frame_time_ms_) {
151 const int64_t elapsed_ms =
152 (clock_->TimeInMilliseconds() - *first_decoded_frame_time_ms_);
153 if (elapsed_ms >=
154 metrics::kMinRunTimeInSeconds * rtc::kNumMillisecsPerSec) {
155 RTC_HISTOGRAM_COUNTS_100(
156 "WebRTC.Video.DecodedFramesPerSecond",
157 static_cast<int>((stats_.frames_decoded * 1000.0f / elapsed_ms) +
158 0.5f));
159 }
160 }
161
asapersson6718e972015-07-24 00:20:58 -0700162 const int kMinRequiredSamples = 200;
asaperssonf839dcc2015-10-08 00:41:59 -0700163 int samples = static_cast<int>(render_fps_tracker_.TotalSampleCount());
asapersson2077f2f2017-05-11 05:37:35 -0700164 if (samples >= kMinRequiredSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700165 RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.RenderFramesPerSecond",
166 round(render_fps_tracker_.ComputeTotalRate()));
167 RTC_HISTOGRAM_COUNTS_100000(
asapersson28ba9272016-01-25 05:58:23 -0800168 "WebRTC.Video.RenderSqrtPixelsPerSecond",
Tim Psiakiad13d2f2015-11-10 16:34:50 -0800169 round(render_pixel_tracker_.ComputeTotalRate()));
asaperssonf839dcc2015-10-08 00:41:59 -0700170 }
ilnik6d5b4d62017-08-30 03:32:14 -0700171
asaperssonf8cdd182016-03-15 01:00:47 -0700172 int sync_offset_ms = sync_offset_counter_.Avg(kMinRequiredSamples);
pbos35fdb2a2016-05-03 03:32:10 -0700173 if (sync_offset_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700174 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.AVSyncOffsetInMs", sync_offset_ms);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100175 RTC_LOG(LS_INFO) << "WebRTC.Video.AVSyncOffsetInMs " << sync_offset_ms;
pbos35fdb2a2016-05-03 03:32:10 -0700176 }
asaperssonde9e5ff2016-11-02 07:14:03 -0700177 AggregatedStats freq_offset_stats = freq_offset_counter_.GetStats();
178 if (freq_offset_stats.num_samples > 0) {
179 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtpToNtpFreqOffsetInKhz",
180 freq_offset_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100181 RTC_LOG(LS_INFO) << "WebRTC.Video.RtpToNtpFreqOffsetInKhz, "
182 << freq_offset_stats.ToString();
asaperssonde9e5ff2016-11-02 07:14:03 -0700183 }
asaperssonf8cdd182016-03-15 01:00:47 -0700184
asaperssonb99baf82017-04-20 04:05:43 -0700185 int num_total_frames =
186 stats_.frame_counts.key_frames + stats_.frame_counts.delta_frames;
187 if (num_total_frames >= kMinRequiredSamples) {
188 int num_key_frames = stats_.frame_counts.key_frames;
philipela45102f2017-02-22 05:30:39 -0800189 int key_frames_permille =
asaperssonb99baf82017-04-20 04:05:43 -0700190 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
philipela45102f2017-02-22 05:30:39 -0800191 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesReceivedInPermille",
192 key_frames_permille);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100193 RTC_LOG(LS_INFO) << "WebRTC.Video.KeyFramesReceivedInPermille "
194 << key_frames_permille;
philipela45102f2017-02-22 05:30:39 -0800195 }
196
asapersson86b01602015-10-20 23:55:26 -0700197 int qp = qp_counters_.vp8.Avg(kMinRequiredSamples);
asapersson2077f2f2017-05-11 05:37:35 -0700198 if (qp != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700199 RTC_HISTOGRAM_COUNTS_200("WebRTC.Video.Decoded.Vp8.Qp", qp);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100200 RTC_LOG(LS_INFO) << "WebRTC.Video.Decoded.Vp8.Qp " << qp;
asapersson2077f2f2017-05-11 05:37:35 -0700201 }
asaperssona563c212017-03-02 08:25:46 -0800202 int decode_ms = decode_time_counter_.Avg(kMinRequiredSamples);
asapersson2077f2f2017-05-11 05:37:35 -0700203 if (decode_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700204 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", decode_ms);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100205 RTC_LOG(LS_INFO) << "WebRTC.Video.DecodeTimeInMs " << decode_ms;
asapersson2077f2f2017-05-11 05:37:35 -0700206 }
asaperssona563c212017-03-02 08:25:46 -0800207 int jb_delay_ms = jitter_buffer_delay_counter_.Avg(kMinRequiredSamples);
philipela45102f2017-02-22 05:30:39 -0800208 if (jb_delay_ms != -1) {
209 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
210 jb_delay_ms);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100211 RTC_LOG(LS_INFO) << "WebRTC.Video.JitterBufferDelayInMs " << jb_delay_ms;
asapersson8688a4e2016-04-27 23:42:35 -0700212 }
philipela45102f2017-02-22 05:30:39 -0800213
asaperssona563c212017-03-02 08:25:46 -0800214 int target_delay_ms = target_delay_counter_.Avg(kMinRequiredSamples);
asapersson8688a4e2016-04-27 23:42:35 -0700215 if (target_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700216 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs", target_delay_ms);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100217 RTC_LOG(LS_INFO) << "WebRTC.Video.TargetDelayInMs " << target_delay_ms;
asapersson8688a4e2016-04-27 23:42:35 -0700218 }
asaperssona563c212017-03-02 08:25:46 -0800219 int current_delay_ms = current_delay_counter_.Avg(kMinRequiredSamples);
asapersson8688a4e2016-04-27 23:42:35 -0700220 if (current_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700221 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.CurrentDelayInMs",
222 current_delay_ms);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100223 RTC_LOG(LS_INFO) << "WebRTC.Video.CurrentDelayInMs " << current_delay_ms;
asapersson8688a4e2016-04-27 23:42:35 -0700224 }
asaperssona563c212017-03-02 08:25:46 -0800225 int delay_ms = delay_counter_.Avg(kMinRequiredSamples);
asapersson13c433c2015-10-06 04:08:15 -0700226 if (delay_ms != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700227 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", delay_ms);
sprang0ab8e812016-02-24 01:35:40 -0800228
ilnik6d5b4d62017-08-30 03:32:14 -0700229 // Aggregate content_specific_stats_ by removing experiment or simulcast
230 // information;
231 std::map<VideoContentType, ContentSpecificStats> aggregated_stats;
232 for (auto it : content_specific_stats_) {
233 // Calculate simulcast specific metrics (".S0" ... ".S2" suffixes).
234 VideoContentType content_type = it.first;
235 if (videocontenttypehelpers::GetSimulcastId(content_type) > 0) {
236 // Aggregate on experiment id.
237 videocontenttypehelpers::SetExperimentId(&content_type, 0);
238 aggregated_stats[content_type].Add(it.second);
239 }
240 // Calculate experiment specific metrics (".ExperimentGroup[0-7]" suffixes).
241 content_type = it.first;
242 if (videocontenttypehelpers::GetExperimentId(content_type) > 0) {
243 // Aggregate on simulcast id.
244 videocontenttypehelpers::SetSimulcastId(&content_type, 0);
245 aggregated_stats[content_type].Add(it.second);
246 }
247 // Calculate aggregated metrics (no suffixes. Aggregated on everything).
248 content_type = it.first;
249 videocontenttypehelpers::SetSimulcastId(&content_type, 0);
250 videocontenttypehelpers::SetExperimentId(&content_type, 0);
251 aggregated_stats[content_type].Add(it.second);
ilnik00d802b2017-04-11 10:34:31 -0700252 }
253
ilnik6d5b4d62017-08-30 03:32:14 -0700254 for (auto it : aggregated_stats) {
255 // For the metric Foo we report the following slices:
256 // WebRTC.Video.Foo,
257 // WebRTC.Video.Screenshare.Foo,
258 // WebRTC.Video.Foo.S[0-3],
259 // WebRTC.Video.Foo.ExperimentGroup[0-7],
260 // WebRTC.Video.Screenshare.Foo.S[0-3],
261 // WebRTC.Video.Screenshare.Foo.ExperimentGroup[0-7].
262 auto content_type = it.first;
263 auto stats = it.second;
264 std::string uma_prefix = UmaPrefixForContentType(content_type);
265 std::string uma_suffix = UmaSuffixForContentType(content_type);
266 // Metrics can be sliced on either simulcast id or experiment id but not
267 // both.
268 RTC_DCHECK(videocontenttypehelpers::GetExperimentId(content_type) == 0 ||
269 videocontenttypehelpers::GetSimulcastId(content_type) == 0);
ilnik00d802b2017-04-11 10:34:31 -0700270
ilnik6d5b4d62017-08-30 03:32:14 -0700271 int e2e_delay_ms = stats.e2e_delay_counter.Avg(kMinRequiredSamples);
272 if (e2e_delay_ms != -1) {
273 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
274 uma_prefix + ".EndToEndDelayInMs" + uma_suffix, e2e_delay_ms);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100275 RTC_LOG(LS_INFO) << uma_prefix << ".EndToEndDelayInMs" << uma_suffix
276 << " " << e2e_delay_ms;
ilnik6d5b4d62017-08-30 03:32:14 -0700277 }
278 int e2e_delay_max_ms = stats.e2e_delay_counter.Max();
279 if (e2e_delay_max_ms != -1 && e2e_delay_ms != -1) {
280 RTC_HISTOGRAM_COUNTS_SPARSE_100000(
281 uma_prefix + ".EndToEndDelayMaxInMs" + uma_suffix, e2e_delay_max_ms);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100282 RTC_LOG(LS_INFO) << uma_prefix << ".EndToEndDelayMaxInMs" << uma_suffix
283 << " " << e2e_delay_max_ms;
ilnik6d5b4d62017-08-30 03:32:14 -0700284 }
285 int interframe_delay_ms =
286 stats.interframe_delay_counter.Avg(kMinRequiredSamples);
287 if (interframe_delay_ms != -1) {
288 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
289 uma_prefix + ".InterframeDelayInMs" + uma_suffix,
290 interframe_delay_ms);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100291 RTC_LOG(LS_INFO) << uma_prefix << ".InterframeDelayInMs" << uma_suffix
292 << " " << interframe_delay_ms;
ilnik6d5b4d62017-08-30 03:32:14 -0700293 }
294 int interframe_delay_max_ms = stats.interframe_delay_counter.Max();
295 if (interframe_delay_max_ms != -1 && interframe_delay_ms != -1) {
296 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
297 uma_prefix + ".InterframeDelayMaxInMs" + uma_suffix,
298 interframe_delay_max_ms);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100299 RTC_LOG(LS_INFO) << uma_prefix << ".InterframeDelayMaxInMs" << uma_suffix
300 << " " << interframe_delay_max_ms;
ilnik6d5b4d62017-08-30 03:32:14 -0700301 }
ilnik00d802b2017-04-11 10:34:31 -0700302
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200303 rtc::Optional<uint32_t> interframe_delay_95p_ms =
304 stats.interframe_delay_percentiles.GetPercentile(0.95f);
305 if (interframe_delay_95p_ms && interframe_delay_ms != -1) {
306 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
307 uma_prefix + ".InterframeDelay95PercentileInMs" + uma_suffix,
308 *interframe_delay_95p_ms);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100309 RTC_LOG(LS_INFO) << uma_prefix << ".InterframeDelay95PercentileInMs"
310 << uma_suffix << " " << *interframe_delay_95p_ms;
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200311 }
312
ilnik6d5b4d62017-08-30 03:32:14 -0700313 int width = stats.received_width.Avg(kMinRequiredSamples);
314 if (width != -1) {
315 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
316 uma_prefix + ".ReceivedWidthInPixels" + uma_suffix, width);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100317 RTC_LOG(LS_INFO) << uma_prefix << ".ReceivedWidthInPixels" << uma_suffix
318 << " " << width;
ilnik6d5b4d62017-08-30 03:32:14 -0700319 }
asapersson1490f7a2016-09-23 02:09:46 -0700320
ilnik6d5b4d62017-08-30 03:32:14 -0700321 int height = stats.received_height.Avg(kMinRequiredSamples);
322 if (height != -1) {
323 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
324 uma_prefix + ".ReceivedHeightInPixels" + uma_suffix, height);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100325 RTC_LOG(LS_INFO) << uma_prefix << ".ReceivedHeightInPixels" << uma_suffix
326 << " " << height;
ilnik6d5b4d62017-08-30 03:32:14 -0700327 }
ilnik4257ab22017-07-03 01:15:58 -0700328
ilnik6d5b4d62017-08-30 03:32:14 -0700329 if (content_type != VideoContentType::UNSPECIFIED) {
330 // Don't report these 3 metrics unsliced, as more precise variants
331 // are reported separately in this method.
332 float flow_duration_sec = stats.flow_duration_ms / 1000.0;
333 if (flow_duration_sec >= metrics::kMinRunTimeInSeconds) {
334 int media_bitrate_kbps = static_cast<int>(stats.total_media_bytes * 8 /
335 flow_duration_sec / 1000);
336 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
337 uma_prefix + ".MediaBitrateReceivedInKbps" + uma_suffix,
338 media_bitrate_kbps);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100339 RTC_LOG(LS_INFO) << uma_prefix << ".MediaBitrateReceivedInKbps"
340 << uma_suffix << " " << media_bitrate_kbps;
ilnik6d5b4d62017-08-30 03:32:14 -0700341 }
342
343 int num_total_frames =
344 stats.frame_counts.key_frames + stats.frame_counts.delta_frames;
345 if (num_total_frames >= kMinRequiredSamples) {
346 int num_key_frames = stats.frame_counts.key_frames;
347 int key_frames_permille =
348 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
349 RTC_HISTOGRAM_COUNTS_SPARSE_1000(
350 uma_prefix + ".KeyFramesReceivedInPermille" + uma_suffix,
351 key_frames_permille);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100352 RTC_LOG(LS_INFO) << uma_prefix << ".KeyFramesReceivedInPermille"
353 << uma_suffix << " " << key_frames_permille;
ilnik6d5b4d62017-08-30 03:32:14 -0700354 }
355
356 int qp = stats.qp_counter.Avg(kMinRequiredSamples);
357 if (qp != -1) {
358 RTC_HISTOGRAM_COUNTS_SPARSE_200(
359 uma_prefix + ".Decoded.Vp8.Qp" + uma_suffix, qp);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100360 RTC_LOG(LS_INFO) << uma_prefix << ".Decoded.Vp8.Qp" << uma_suffix << " "
361 << qp;
ilnik6d5b4d62017-08-30 03:32:14 -0700362 }
363 }
ilnik4257ab22017-07-03 01:15:58 -0700364 }
365
sprang0ab8e812016-02-24 01:35:40 -0800366 StreamDataCounters rtp = stats_.rtp_stats;
367 StreamDataCounters rtx;
368 for (auto it : rtx_stats_)
369 rtx.Add(it.second);
370 StreamDataCounters rtp_rtx = rtp;
371 rtp_rtx.Add(rtx);
372 int64_t elapsed_sec =
373 rtp_rtx.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) / 1000;
asapersson2077f2f2017-05-11 05:37:35 -0700374 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
asapersson1d02d3e2016-09-09 22:40:25 -0700375 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800376 "WebRTC.Video.BitrateReceivedInKbps",
377 static_cast<int>(rtp_rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
378 1000));
ilnik6d5b4d62017-08-30 03:32:14 -0700379 int media_bitrate_kbs =
380 static_cast<int>(rtp.MediaPayloadBytes() * 8 / elapsed_sec / 1000);
381 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.MediaBitrateReceivedInKbps",
382 media_bitrate_kbs);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100383 RTC_LOG(LS_INFO) << "WebRTC.Video.MediaBitrateReceivedInKbps "
384 << media_bitrate_kbs;
asapersson1d02d3e2016-09-09 22:40:25 -0700385 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800386 "WebRTC.Video.PaddingBitrateReceivedInKbps",
387 static_cast<int>(rtp_rtx.transmitted.padding_bytes * 8 / elapsed_sec /
388 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700389 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800390 "WebRTC.Video.RetransmittedBitrateReceivedInKbps",
391 static_cast<int>(rtp_rtx.retransmitted.TotalBytes() * 8 / elapsed_sec /
392 1000));
393 if (!rtx_stats_.empty()) {
asapersson1d02d3e2016-09-09 22:40:25 -0700394 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtxBitrateReceivedInKbps",
395 static_cast<int>(rtx.transmitted.TotalBytes() *
396 8 / elapsed_sec / 1000));
sprang0ab8e812016-02-24 01:35:40 -0800397 }
nisse3b3622f2017-09-26 02:49:21 -0700398 if (config_.rtp.ulpfec_payload_type != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700399 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800400 "WebRTC.Video.FecBitrateReceivedInKbps",
401 static_cast<int>(rtp_rtx.fec.TotalBytes() * 8 / elapsed_sec / 1000));
402 }
sprang07fb9be2016-02-24 07:55:00 -0800403 const RtcpPacketTypeCounter& counters = stats_.rtcp_packet_type_counts;
asapersson1d02d3e2016-09-09 22:40:25 -0700404 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.NackPacketsSentPerMinute",
405 counters.nack_packets * 60 / elapsed_sec);
406 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.FirPacketsSentPerMinute",
407 counters.fir_packets * 60 / elapsed_sec);
408 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.PliPacketsSentPerMinute",
409 counters.pli_packets * 60 / elapsed_sec);
sprang07fb9be2016-02-24 07:55:00 -0800410 if (counters.nack_requests > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700411 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.UniqueNackRequestsSentInPercent",
412 counters.UniqueNackRequestsInPercent());
sprang07fb9be2016-02-24 07:55:00 -0800413 }
sprang0ab8e812016-02-24 01:35:40 -0800414 }
palmkvista40672a2017-01-13 05:58:34 -0800415
416 if (num_certain_states_ >= kBadCallMinRequiredSamples) {
417 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Any",
418 100 * num_bad_states_ / num_certain_states_);
419 }
420 rtc::Optional<double> fps_fraction =
421 fps_threshold_.FractionHigh(kBadCallMinRequiredSamples);
422 if (fps_fraction) {
423 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRate",
424 static_cast<int>(100 * (1 - *fps_fraction)));
425 }
426 rtc::Optional<double> variance_fraction =
427 variance_threshold_.FractionHigh(kBadCallMinRequiredSamples);
428 if (variance_fraction) {
429 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRateVariance",
430 static_cast<int>(100 * *variance_fraction));
431 }
432 rtc::Optional<double> qp_fraction =
433 qp_threshold_.FractionHigh(kBadCallMinRequiredSamples);
434 if (qp_fraction) {
435 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Qp",
436 static_cast<int>(100 * *qp_fraction));
437 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200438}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000439
palmkvist349092b2016-12-13 02:45:57 -0800440void ReceiveStatisticsProxy::QualitySample() {
441 int64_t now = clock_->TimeInMilliseconds();
442 if (last_sample_time_ + kMinSampleLengthMs > now)
443 return;
444
445 double fps =
446 render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_);
447 int qp = qp_sample_.Avg(1);
448
449 bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true);
450 bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false);
451 bool prev_variance_bad = variance_threshold_.IsHigh().value_or(false);
452 bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad;
453
454 fps_threshold_.AddMeasurement(static_cast<int>(fps));
455 if (qp != -1)
456 qp_threshold_.AddMeasurement(qp);
457 rtc::Optional<double> fps_variance_opt = fps_threshold_.CalculateVariance();
458 double fps_variance = fps_variance_opt.value_or(0);
459 if (fps_variance_opt) {
460 variance_threshold_.AddMeasurement(static_cast<int>(fps_variance));
461 }
462
463 bool fps_bad = !fps_threshold_.IsHigh().value_or(true);
464 bool qp_bad = qp_threshold_.IsHigh().value_or(false);
465 bool variance_bad = variance_threshold_.IsHigh().value_or(false);
466 bool any_bad = fps_bad || qp_bad || variance_bad;
467
468 if (!prev_any_bad && any_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100469 RTC_LOG(LS_INFO) << "Bad call (any) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800470 } else if (prev_any_bad && !any_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100471 RTC_LOG(LS_INFO) << "Bad call (any) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800472 }
473
474 if (!prev_fps_bad && fps_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100475 RTC_LOG(LS_INFO) << "Bad call (fps) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800476 } else if (prev_fps_bad && !fps_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100477 RTC_LOG(LS_INFO) << "Bad call (fps) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800478 }
479
480 if (!prev_qp_bad && qp_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100481 RTC_LOG(LS_INFO) << "Bad call (qp) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800482 } else if (prev_qp_bad && !qp_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100483 RTC_LOG(LS_INFO) << "Bad call (qp) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800484 }
485
486 if (!prev_variance_bad && variance_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100487 RTC_LOG(LS_INFO) << "Bad call (variance) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800488 } else if (prev_variance_bad && !variance_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100489 RTC_LOG(LS_INFO) << "Bad call (variance) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800490 }
491
Mirko Bonadei675513b2017-11-09 11:09:25 +0100492 RTC_LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_)
493 << " fps: " << fps << " fps_bad: " << fps_bad
494 << " qp: " << qp << " qp_bad: " << qp_bad
495 << " variance_bad: " << variance_bad
496 << " fps_variance: " << fps_variance;
palmkvist349092b2016-12-13 02:45:57 -0800497
498 last_sample_time_ = now;
499 qp_sample_.Reset();
palmkvista40672a2017-01-13 05:58:34 -0800500
501 if (fps_threshold_.IsHigh() || variance_threshold_.IsHigh() ||
502 qp_threshold_.IsHigh()) {
503 if (any_bad)
504 ++num_bad_states_;
505 ++num_certain_states_;
506 }
palmkvist349092b2016-12-13 02:45:57 -0800507}
508
asapersson0255acb2017-03-28 02:44:58 -0700509void ReceiveStatisticsProxy::UpdateFramerate(int64_t now_ms) const {
philipela45102f2017-02-22 05:30:39 -0800510 int64_t old_frames_ms = now_ms - kRateStatisticsWindowSizeMs;
511 while (!frame_window_.empty() &&
512 frame_window_.begin()->first < old_frames_ms) {
philipela45102f2017-02-22 05:30:39 -0800513 frame_window_.erase(frame_window_.begin());
514 }
515
516 size_t framerate =
517 (frame_window_.size() * 1000 + 500) / kRateStatisticsWindowSizeMs;
philipela45102f2017-02-22 05:30:39 -0800518 stats_.network_frame_rate = static_cast<int>(framerate);
philipela45102f2017-02-22 05:30:39 -0800519}
520
sprang@webrtc.org09315702014-02-07 12:06:29 +0000521VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const {
Peter Boströmf2f82832015-05-01 13:00:41 +0200522 rtc::CritScope lock(&crit_);
sprang948b2752017-05-04 02:47:13 -0700523 // Get current frame rates here, as only updating them on new frames prevents
524 // us from ever correctly displaying frame rate of 0.
525 int64_t now_ms = clock_->TimeInMilliseconds();
526 UpdateFramerate(now_ms);
527 stats_.render_frame_rate = renders_fps_estimator_.Rate(now_ms).value_or(0);
528 stats_.decode_frame_rate = decode_fps_estimator_.Rate(now_ms).value_or(0);
asapersson0255acb2017-03-28 02:44:58 -0700529 stats_.total_bitrate_bps =
530 static_cast<int>(total_byte_tracker_.ComputeRate() * 8);
ilnika79cc282017-08-23 05:24:10 -0700531 stats_.interframe_delay_max_ms =
532 interframe_delay_max_moving_.Max(now_ms).value_or(-1);
ilnik75204c52017-09-04 03:35:40 -0700533 stats_.timing_frame_info = timing_frame_info_counter_.Max(now_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700534 stats_.content_type = last_content_type_;
pbos@webrtc.org55707692014-12-19 15:45:03 +0000535 return stats_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000536}
537
pbosf42376c2015-08-28 07:35:32 -0700538void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) {
539 rtc::CritScope lock(&crit_);
540 stats_.current_payload_type = payload_type;
541}
542
Peter Boströmb7d9a972015-12-18 16:01:11 +0100543void ReceiveStatisticsProxy::OnDecoderImplementationName(
544 const char* implementation_name) {
545 rtc::CritScope lock(&crit_);
546 stats_.decoder_implementation_name = implementation_name;
547}
pbosf42376c2015-08-28 07:35:32 -0700548void ReceiveStatisticsProxy::OnIncomingRate(unsigned int framerate,
549 unsigned int bitrate_bps) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200550 rtc::CritScope lock(&crit_);
palmkvista40672a2017-01-13 05:58:34 -0800551 if (stats_.rtp_stats.first_packet_time_ms != -1)
552 QualitySample();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000553}
554
philipela45102f2017-02-22 05:30:39 -0800555void ReceiveStatisticsProxy::OnFrameBufferTimingsUpdated(
556 int decode_ms,
557 int max_decode_ms,
558 int current_delay_ms,
559 int target_delay_ms,
560 int jitter_buffer_ms,
561 int min_playout_delay_ms,
562 int render_delay_ms) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200563 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000564 stats_.decode_ms = decode_ms;
565 stats_.max_decode_ms = max_decode_ms;
566 stats_.current_delay_ms = current_delay_ms;
567 stats_.target_delay_ms = target_delay_ms;
568 stats_.jitter_buffer_ms = jitter_buffer_ms;
569 stats_.min_playout_delay_ms = min_playout_delay_ms;
570 stats_.render_delay_ms = render_delay_ms;
asapersson6718e972015-07-24 00:20:58 -0700571 decode_time_counter_.Add(decode_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700572 jitter_buffer_delay_counter_.Add(jitter_buffer_ms);
573 target_delay_counter_.Add(target_delay_ms);
574 current_delay_counter_.Add(current_delay_ms);
asaperssona1862882016-04-18 00:41:05 -0700575 // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time +
576 // render delay).
philipela45102f2017-02-22 05:30:39 -0800577 delay_counter_.Add(target_delay_ms + avg_rtt_ms_ / 2);
pbos@webrtc.org98c04b32014-12-18 13:12:52 +0000578}
579
ilnik2edc6842017-07-06 03:06:50 -0700580void ReceiveStatisticsProxy::OnTimingFrameInfoUpdated(
581 const TimingFrameInfo& info) {
582 rtc::CritScope lock(&crit_);
Ilya Nikolaevskiy3f670e02017-10-10 11:18:49 +0200583 int64_t now_ms = clock_->TimeInMilliseconds();
ilnik75204c52017-09-04 03:35:40 -0700584 timing_frame_info_counter_.Add(info, now_ms);
ilnik2edc6842017-07-06 03:06:50 -0700585}
586
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000587void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated(
588 uint32_t ssrc,
589 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200590 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000591 if (stats_.ssrc != ssrc)
592 return;
593 stats_.rtcp_packet_type_counts = packet_counter;
594}
595
sprang@webrtc.org09315702014-02-07 12:06:29 +0000596void ReceiveStatisticsProxy::StatisticsUpdated(
597 const webrtc::RtcpStatistics& statistics,
598 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200599 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700600 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000601 // receive stats from one of them.
602 if (stats_.ssrc != ssrc)
603 return;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000604 stats_.rtcp_stats = statistics;
Åsa Persson3c391cb2015-04-27 10:09:49 +0200605 report_block_stats_.Store(statistics, ssrc, 0);
asapersson0c43f772016-11-30 01:42:26 -0800606
607 if (first_report_block_time_ms_ == -1)
608 first_report_block_time_ms_ = clock_->TimeInMilliseconds();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000609}
610
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000611void ReceiveStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200612 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700613 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000614 // receive stats from one of them.
615 if (stats_.ssrc != ssrc)
616 return;
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000617 stats_.c_name = cname;
618}
619
sprang@webrtc.org09315702014-02-07 12:06:29 +0000620void ReceiveStatisticsProxy::DataCountersUpdated(
621 const webrtc::StreamDataCounters& counters,
622 uint32_t ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700623 size_t last_total_bytes = 0;
624 size_t total_bytes = 0;
Peter Boströmf2f82832015-05-01 13:00:41 +0200625 rtc::CritScope lock(&crit_);
sprang0ab8e812016-02-24 01:35:40 -0800626 if (ssrc == stats_.ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700627 last_total_bytes = stats_.rtp_stats.transmitted.TotalBytes();
628 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800629 stats_.rtp_stats = counters;
630 } else {
631 auto it = rtx_stats_.find(ssrc);
632 if (it != rtx_stats_.end()) {
asapersson0255acb2017-03-28 02:44:58 -0700633 last_total_bytes = it->second.transmitted.TotalBytes();
634 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800635 it->second = counters;
636 } else {
637 RTC_NOTREACHED() << "Unexpected stream ssrc: " << ssrc;
638 }
639 }
asapersson0255acb2017-03-28 02:44:58 -0700640 if (total_bytes > last_total_bytes)
641 total_byte_tracker_.AddSamples(total_bytes - last_total_bytes);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000642}
643
ilnik00d802b2017-04-11 10:34:31 -0700644void ReceiveStatisticsProxy::OnDecodedFrame(rtc::Optional<uint8_t> qp,
645 VideoContentType content_type) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200646 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700647
Ilya Nikolaevskiy3f670e02017-10-10 11:18:49 +0200648 uint64_t now = clock_->TimeInMilliseconds();
649
ilnik6d5b4d62017-08-30 03:32:14 -0700650 ContentSpecificStats* content_specific_stats =
651 &content_specific_stats_[content_type];
sakale5ba44e2016-10-26 07:09:24 -0700652 ++stats_.frames_decoded;
sakalcc452e12017-02-09 04:53:45 -0800653 if (qp) {
654 if (!stats_.qp_sum) {
655 if (stats_.frames_decoded != 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100656 RTC_LOG(LS_WARNING)
sakalcc452e12017-02-09 04:53:45 -0800657 << "Frames decoded was not 1 when first qp value was received.";
658 stats_.frames_decoded = 1;
659 }
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100660 stats_.qp_sum = 0;
sakalcc452e12017-02-09 04:53:45 -0800661 }
662 *stats_.qp_sum += *qp;
ilnik6d5b4d62017-08-30 03:32:14 -0700663 content_specific_stats->qp_counter.Add(*qp);
sakalcc452e12017-02-09 04:53:45 -0800664 } else if (stats_.qp_sum) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100665 RTC_LOG(LS_WARNING)
sakalcc452e12017-02-09 04:53:45 -0800666 << "QP sum was already set and no QP was given for a frame.";
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100667 stats_.qp_sum = rtc::nullopt;
sakalcc452e12017-02-09 04:53:45 -0800668 }
ilnik00d802b2017-04-11 10:34:31 -0700669 last_content_type_ = content_type;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000670 decode_fps_estimator_.Update(1, now);
ilnik4257ab22017-07-03 01:15:58 -0700671 if (last_decoded_frame_time_ms_) {
672 int64_t interframe_delay_ms = now - *last_decoded_frame_time_ms_;
673 RTC_DCHECK_GE(interframe_delay_ms, 0);
ilnika79cc282017-08-23 05:24:10 -0700674 interframe_delay_max_moving_.Add(interframe_delay_ms, now);
ilnik6d5b4d62017-08-30 03:32:14 -0700675 content_specific_stats->interframe_delay_counter.Add(interframe_delay_ms);
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200676 content_specific_stats->interframe_delay_percentiles.Add(
677 interframe_delay_ms);
ilnik6d5b4d62017-08-30 03:32:14 -0700678 content_specific_stats->flow_duration_ms += interframe_delay_ms;
ilnik4257ab22017-07-03 01:15:58 -0700679 }
Åsa Perssonb9b07ea2018-01-24 17:04:07 +0100680 if (stats_.frames_decoded == 1)
681 first_decoded_frame_time_ms_.emplace(now);
ilnik4257ab22017-07-03 01:15:58 -0700682 last_decoded_frame_time_ms_.emplace(now);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000683}
684
asapersson1490f7a2016-09-23 02:09:46 -0700685void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) {
686 int width = frame.width();
687 int height = frame.height();
asaperssonf839dcc2015-10-08 00:41:59 -0700688 RTC_DCHECK_GT(width, 0);
689 RTC_DCHECK_GT(height, 0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000690 uint64_t now = clock_->TimeInMilliseconds();
Peter Boströmf2f82832015-05-01 13:00:41 +0200691 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700692 ContentSpecificStats* content_specific_stats =
693 &content_specific_stats_[last_content_type_];
sprang@webrtc.org09315702014-02-07 12:06:29 +0000694 renders_fps_estimator_.Update(1, now);
hbos50cfe1f2017-01-23 07:21:55 -0800695 ++stats_.frames_rendered;
asapersson2e5cfcd2016-08-11 08:41:18 -0700696 stats_.width = width;
697 stats_.height = height;
Tim Psiaki63046262015-09-14 10:38:08 -0700698 render_fps_tracker_.AddSamples(1);
asaperssonf839dcc2015-10-08 00:41:59 -0700699 render_pixel_tracker_.AddSamples(sqrt(width * height));
ilnik6d5b4d62017-08-30 03:32:14 -0700700 content_specific_stats->received_width.Add(width);
701 content_specific_stats->received_height.Add(height);
asapersson1490f7a2016-09-23 02:09:46 -0700702
703 if (frame.ntp_time_ms() > 0) {
704 int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms();
ilnik00d802b2017-04-11 10:34:31 -0700705 if (delay_ms >= 0) {
ilnik6d5b4d62017-08-30 03:32:14 -0700706 content_specific_stats->e2e_delay_counter.Add(delay_ms);
ilnik00d802b2017-04-11 10:34:31 -0700707 }
asapersson1490f7a2016-09-23 02:09:46 -0700708 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000709}
710
asaperssonde9e5ff2016-11-02 07:14:03 -0700711void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t sync_offset_ms,
712 double estimated_freq_khz) {
asaperssonf8cdd182016-03-15 01:00:47 -0700713 rtc::CritScope lock(&crit_);
714 sync_offset_counter_.Add(std::abs(sync_offset_ms));
715 stats_.sync_offset_ms = sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700716
717 const double kMaxFreqKhz = 10000.0;
718 int offset_khz = kMaxFreqKhz;
719 // Should not be zero or negative. If so, report max.
720 if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0)
721 offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5);
722
723 freq_offset_counter_.Add(offset_khz);
asaperssonf8cdd182016-03-15 01:00:47 -0700724}
725
pbos@webrtc.org55707692014-12-19 15:45:03 +0000726void ReceiveStatisticsProxy::OnReceiveRatesUpdated(uint32_t bitRate,
727 uint32_t frameRate) {
728}
729
philipela45102f2017-02-22 05:30:39 -0800730void ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe,
ilnik6d5b4d62017-08-30 03:32:14 -0700731 size_t size_bytes,
732 VideoContentType content_type) {
philipela45102f2017-02-22 05:30:39 -0800733 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700734 if (is_keyframe) {
philipela45102f2017-02-22 05:30:39 -0800735 ++stats_.frame_counts.key_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700736 } else {
philipela45102f2017-02-22 05:30:39 -0800737 ++stats_.frame_counts.delta_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700738 }
739
740 ContentSpecificStats* content_specific_stats =
741 &content_specific_stats_[content_type];
742
743 content_specific_stats->total_media_bytes += size_bytes;
744 if (is_keyframe) {
745 ++content_specific_stats->frame_counts.key_frames;
746 } else {
747 ++content_specific_stats->frame_counts.delta_frames;
748 }
philipela45102f2017-02-22 05:30:39 -0800749
750 int64_t now_ms = clock_->TimeInMilliseconds();
philipela45102f2017-02-22 05:30:39 -0800751 frame_window_.insert(std::make_pair(now_ms, size_bytes));
asapersson0255acb2017-03-28 02:44:58 -0700752 UpdateFramerate(now_ms);
philipela45102f2017-02-22 05:30:39 -0800753}
754
pbos@webrtc.org55707692014-12-19 15:45:03 +0000755void ReceiveStatisticsProxy::OnFrameCountsUpdated(
756 const FrameCounts& frame_counts) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200757 rtc::CritScope lock(&crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000758 stats_.frame_counts = frame_counts;
759}
760
pbos@webrtc.org55707692014-12-19 15:45:03 +0000761void ReceiveStatisticsProxy::OnDiscardedPacketsUpdated(int discarded_packets) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200762 rtc::CritScope lock(&crit_);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000763 stats_.discarded_packets = discarded_packets;
764}
765
asapersson86b01602015-10-20 23:55:26 -0700766void ReceiveStatisticsProxy::OnPreDecode(
767 const EncodedImage& encoded_image,
768 const CodecSpecificInfo* codec_specific_info) {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200769 if (!codec_specific_info || encoded_image.qp_ == -1) {
asapersson86b01602015-10-20 23:55:26 -0700770 return;
771 }
772 if (codec_specific_info->codecType == kVideoCodecVP8) {
773 qp_counters_.vp8.Add(encoded_image.qp_);
palmkvist349092b2016-12-13 02:45:57 -0800774 rtc::CritScope lock(&crit_);
775 qp_sample_.Add(encoded_image.qp_);
asapersson86b01602015-10-20 23:55:26 -0700776 }
777}
778
sprang3e86e7e2017-08-22 09:23:28 -0700779void ReceiveStatisticsProxy::OnStreamInactive() {
780 // TODO(sprang): Figure out any other state that should be reset.
781
782 rtc::CritScope lock(&crit_);
783 // Don't report inter-frame delay if stream was paused.
784 last_decoded_frame_time_ms_.reset();
785}
786
asaperssond89920b2015-07-22 06:52:00 -0700787void ReceiveStatisticsProxy::SampleCounter::Add(int sample) {
788 sum += sample;
789 ++num_samples;
ilnik6d5b4d62017-08-30 03:32:14 -0700790 if (!max || sample > *max) {
791 max.emplace(sample);
792 }
793}
794
795void ReceiveStatisticsProxy::SampleCounter::Add(const SampleCounter& other) {
796 sum += other.sum;
797 num_samples += other.num_samples;
798 if (other.max && (!max || *max < *other.max))
799 max = other.max;
asaperssond89920b2015-07-22 06:52:00 -0700800}
801
asapersson6966bd52017-01-03 00:44:06 -0800802int ReceiveStatisticsProxy::SampleCounter::Avg(
803 int64_t min_required_samples) const {
asaperssond89920b2015-07-22 06:52:00 -0700804 if (num_samples < min_required_samples || num_samples == 0)
805 return -1;
asapersson6966bd52017-01-03 00:44:06 -0800806 return static_cast<int>(sum / num_samples);
asaperssond89920b2015-07-22 06:52:00 -0700807}
808
ilnik6d5b4d62017-08-30 03:32:14 -0700809int ReceiveStatisticsProxy::SampleCounter::Max() const {
810 return max.value_or(-1);
811}
812
palmkvist349092b2016-12-13 02:45:57 -0800813void ReceiveStatisticsProxy::SampleCounter::Reset() {
814 num_samples = 0;
815 sum = 0;
ilnik6d5b4d62017-08-30 03:32:14 -0700816 max.reset();
palmkvist349092b2016-12-13 02:45:57 -0800817}
818
philipela45102f2017-02-22 05:30:39 -0800819void ReceiveStatisticsProxy::OnRttUpdate(int64_t avg_rtt_ms,
820 int64_t max_rtt_ms) {
821 rtc::CritScope lock(&crit_);
822 avg_rtt_ms_ = avg_rtt_ms;
823}
824
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200825ReceiveStatisticsProxy::ContentSpecificStats::ContentSpecificStats()
826 : interframe_delay_percentiles(kMaxCommonInterframeDelayMs) {}
827
ilnik6d5b4d62017-08-30 03:32:14 -0700828void ReceiveStatisticsProxy::ContentSpecificStats::Add(
829 const ContentSpecificStats& other) {
830 e2e_delay_counter.Add(other.e2e_delay_counter);
831 interframe_delay_counter.Add(other.interframe_delay_counter);
832 flow_duration_ms += other.flow_duration_ms;
833 total_media_bytes += other.total_media_bytes;
834 received_height.Add(other.received_height);
835 received_width.Add(other.received_width);
836 qp_counter.Add(other.qp_counter);
837 frame_counts.key_frames += other.frame_counts.key_frames;
838 frame_counts.delta_frames += other.frame_counts.delta_frames;
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200839 interframe_delay_percentiles.Add(other.interframe_delay_percentiles);
ilnik6d5b4d62017-08-30 03:32:14 -0700840}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000841} // namespace webrtc