blob: ff4c6fef70ed8151e6a81b03f0a1a6bfe883a35a [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
11#include "webrtc/video/receive_statistics_proxy.h"
12
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
ilnik6d5b4d62017-08-30 03:32:14 -070018#include "webrtc/modules/pacing/alr_detector.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010019#include "webrtc/modules/video_coding/include/video_codec_interface.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020020#include "webrtc/rtc_base/checks.h"
21#include "webrtc/rtc_base/logging.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010022#include "webrtc/system_wrappers/include/clock.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010023#include "webrtc/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.
48const int kMovingMaxWindowMs = 10000;
49
philipela45102f2017-02-22 05:30:39 -080050// How large window we use to calculate the framerate/bitrate.
51const int kRateStatisticsWindowSizeMs = 1000;
ilnik6d5b4d62017-08-30 03:32:14 -070052
53std::string UmaPrefixForContentType(VideoContentType content_type) {
54 std::stringstream ss;
55 ss << "WebRTC.Video";
56 if (videocontenttypehelpers::IsScreenshare(content_type)) {
57 ss << ".Screenshare";
58 }
59 return ss.str();
60}
61
62std::string UmaSuffixForContentType(VideoContentType content_type) {
63 std::stringstream ss;
64 int simulcast_id = videocontenttypehelpers::GetSimulcastId(content_type);
65 if (simulcast_id > 0) {
66 ss << ".S" << simulcast_id - 1;
67 }
68 int experiment_id = videocontenttypehelpers::GetExperimentId(content_type);
69 if (experiment_id > 0) {
70 ss << ".ExperimentGroup" << experiment_id - 1;
71 }
72 return ss.str();
73}
asaperssonde9e5ff2016-11-02 07:14:03 -070074} // namespace
sprang@webrtc.org09315702014-02-07 12:06:29 +000075
sprang0ab8e812016-02-24 01:35:40 -080076ReceiveStatisticsProxy::ReceiveStatisticsProxy(
Tommi733b5472016-06-10 17:58:01 +020077 const VideoReceiveStream::Config* config,
sprang0ab8e812016-02-24 01:35:40 -080078 Clock* clock)
pbos@webrtc.org55707692014-12-19 15:45:03 +000079 : clock_(clock),
Tommi733b5472016-06-10 17:58:01 +020080 config_(*config),
asapersson4374a092016-07-27 00:39:09 -070081 start_ms_(clock->TimeInMilliseconds()),
palmkvist349092b2016-12-13 02:45:57 -080082 last_sample_time_(clock->TimeInMilliseconds()),
83 fps_threshold_(kLowFpsThreshold,
84 kHighFpsThreshold,
85 kBadFraction,
86 kNumMeasurements),
87 qp_threshold_(kLowQpThresholdVp8,
88 kHighQpThresholdVp8,
89 kBadFraction,
90 kNumMeasurements),
91 variance_threshold_(kLowVarianceThreshold,
92 kHighVarianceThreshold,
93 kBadFraction,
94 kNumMeasurementsVariance),
palmkvista40672a2017-01-13 05:58:34 -080095 num_bad_states_(0),
96 num_certain_states_(0),
sprang@webrtc.org09315702014-02-07 12:06:29 +000097 // 1000ms window, scale 1000 for ms to s.
98 decode_fps_estimator_(1000, 1000),
Tim Psiaki63046262015-09-14 10:38:08 -070099 renders_fps_estimator_(1000, 1000),
Honghai Zhang82d78622016-05-06 11:29:15 -0700100 render_fps_tracker_(100, 10u),
asaperssonde9e5ff2016-11-02 07:14:03 -0700101 render_pixel_tracker_(100, 10u),
asapersson0255acb2017-03-28 02:44:58 -0700102 total_byte_tracker_(100, 10u), // bucket_interval_ms, bucket_count
ilnika79cc282017-08-23 05:24:10 -0700103 interframe_delay_max_moving_(kMovingMaxWindowMs),
asapersson0c43f772016-11-30 01:42:26 -0800104 freq_offset_counter_(clock, nullptr, kFreqOffsetProcessIntervalMs),
philipela45102f2017-02-22 05:30:39 -0800105 first_report_block_time_ms_(-1),
ilnik00d802b2017-04-11 10:34:31 -0700106 avg_rtt_ms_(0),
ilnik75204c52017-09-04 03:35:40 -0700107 last_content_type_(VideoContentType::UNSPECIFIED),
108 timing_frame_info_counter_(kMovingMaxWindowMs) {
Tommi733b5472016-06-10 17:58:01 +0200109 stats_.ssrc = config_.rtp.remote_ssrc;
brandtr14742122017-01-27 04:53:07 -0800110 // TODO(brandtr): Replace |rtx_stats_| with a single instance of
111 // StreamDataCounters.
112 if (config_.rtp.rtx_ssrc) {
113 rtx_stats_[config_.rtp.rtx_ssrc] = StreamDataCounters();
114 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000115}
116
Åsa Persson3c391cb2015-04-27 10:09:49 +0200117ReceiveStatisticsProxy::~ReceiveStatisticsProxy() {
118 UpdateHistograms();
119}
120
asaperssond89920b2015-07-22 06:52:00 -0700121void ReceiveStatisticsProxy::UpdateHistograms() {
ilnik6d5b4d62017-08-30 03:32:14 -0700122 int stream_duration_sec = (clock_->TimeInMilliseconds() - start_ms_) / 1000;
123 if (stats_.frame_counts.key_frames > 0 ||
124 stats_.frame_counts.delta_frames > 0) {
125 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Video.ReceiveStreamLifetimeInSeconds",
126 stream_duration_sec);
127 LOG(LS_INFO) << "WebRTC.Video.ReceiveStreamLifetimeInSeconds "
128 << stream_duration_sec;
129 }
asapersson4374a092016-07-27 00:39:09 -0700130
asapersson0c43f772016-11-30 01:42:26 -0800131 if (first_report_block_time_ms_ != -1 &&
132 ((clock_->TimeInMilliseconds() - first_report_block_time_ms_) / 1000) >=
133 metrics::kMinRunTimeInSeconds) {
134 int fraction_lost = report_block_stats_.FractionLostInPercent();
135 if (fraction_lost != -1) {
136 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedPacketsLostInPercent",
137 fraction_lost);
asapersson2077f2f2017-05-11 05:37:35 -0700138 LOG(LS_INFO) << "WebRTC.Video.ReceivedPacketsLostInPercent "
139 << fraction_lost;
asapersson0c43f772016-11-30 01:42:26 -0800140 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200141 }
asapersson0c43f772016-11-30 01:42:26 -0800142
asapersson6718e972015-07-24 00:20:58 -0700143 const int kMinRequiredSamples = 200;
asaperssonf839dcc2015-10-08 00:41:59 -0700144 int samples = static_cast<int>(render_fps_tracker_.TotalSampleCount());
asapersson2077f2f2017-05-11 05:37:35 -0700145 if (samples >= kMinRequiredSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700146 RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.RenderFramesPerSecond",
147 round(render_fps_tracker_.ComputeTotalRate()));
148 RTC_HISTOGRAM_COUNTS_100000(
asapersson28ba9272016-01-25 05:58:23 -0800149 "WebRTC.Video.RenderSqrtPixelsPerSecond",
Tim Psiakiad13d2f2015-11-10 16:34:50 -0800150 round(render_pixel_tracker_.ComputeTotalRate()));
asaperssonf839dcc2015-10-08 00:41:59 -0700151 }
ilnik6d5b4d62017-08-30 03:32:14 -0700152
asaperssonf8cdd182016-03-15 01:00:47 -0700153 int sync_offset_ms = sync_offset_counter_.Avg(kMinRequiredSamples);
pbos35fdb2a2016-05-03 03:32:10 -0700154 if (sync_offset_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700155 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.AVSyncOffsetInMs", sync_offset_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700156 LOG(LS_INFO) << "WebRTC.Video.AVSyncOffsetInMs " << sync_offset_ms;
pbos35fdb2a2016-05-03 03:32:10 -0700157 }
asaperssonde9e5ff2016-11-02 07:14:03 -0700158 AggregatedStats freq_offset_stats = freq_offset_counter_.GetStats();
159 if (freq_offset_stats.num_samples > 0) {
160 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtpToNtpFreqOffsetInKhz",
161 freq_offset_stats.average);
asapersson43cb7162016-11-15 08:20:48 -0800162 LOG(LS_INFO) << "WebRTC.Video.RtpToNtpFreqOffsetInKhz, "
163 << freq_offset_stats.ToString();
asaperssonde9e5ff2016-11-02 07:14:03 -0700164 }
asaperssonf8cdd182016-03-15 01:00:47 -0700165
asaperssonb99baf82017-04-20 04:05:43 -0700166 int num_total_frames =
167 stats_.frame_counts.key_frames + stats_.frame_counts.delta_frames;
168 if (num_total_frames >= kMinRequiredSamples) {
169 int num_key_frames = stats_.frame_counts.key_frames;
philipela45102f2017-02-22 05:30:39 -0800170 int key_frames_permille =
asaperssonb99baf82017-04-20 04:05:43 -0700171 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
philipela45102f2017-02-22 05:30:39 -0800172 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesReceivedInPermille",
173 key_frames_permille);
asapersson2077f2f2017-05-11 05:37:35 -0700174 LOG(LS_INFO) << "WebRTC.Video.KeyFramesReceivedInPermille "
175 << key_frames_permille;
philipela45102f2017-02-22 05:30:39 -0800176 }
177
asapersson86b01602015-10-20 23:55:26 -0700178 int qp = qp_counters_.vp8.Avg(kMinRequiredSamples);
asapersson2077f2f2017-05-11 05:37:35 -0700179 if (qp != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700180 RTC_HISTOGRAM_COUNTS_200("WebRTC.Video.Decoded.Vp8.Qp", qp);
asapersson2077f2f2017-05-11 05:37:35 -0700181 LOG(LS_INFO) << "WebRTC.Video.Decoded.Vp8.Qp " << qp;
182 }
asaperssona563c212017-03-02 08:25:46 -0800183 int decode_ms = decode_time_counter_.Avg(kMinRequiredSamples);
asapersson2077f2f2017-05-11 05:37:35 -0700184 if (decode_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700185 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", decode_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700186 LOG(LS_INFO) << "WebRTC.Video.DecodeTimeInMs " << decode_ms;
187 }
asaperssona563c212017-03-02 08:25:46 -0800188 int jb_delay_ms = jitter_buffer_delay_counter_.Avg(kMinRequiredSamples);
philipela45102f2017-02-22 05:30:39 -0800189 if (jb_delay_ms != -1) {
190 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
191 jb_delay_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700192 LOG(LS_INFO) << "WebRTC.Video.JitterBufferDelayInMs " << jb_delay_ms;
asapersson8688a4e2016-04-27 23:42:35 -0700193 }
philipela45102f2017-02-22 05:30:39 -0800194
asaperssona563c212017-03-02 08:25:46 -0800195 int target_delay_ms = target_delay_counter_.Avg(kMinRequiredSamples);
asapersson8688a4e2016-04-27 23:42:35 -0700196 if (target_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700197 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs", target_delay_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700198 LOG(LS_INFO) << "WebRTC.Video.TargetDelayInMs " << target_delay_ms;
asapersson8688a4e2016-04-27 23:42:35 -0700199 }
asaperssona563c212017-03-02 08:25:46 -0800200 int current_delay_ms = current_delay_counter_.Avg(kMinRequiredSamples);
asapersson8688a4e2016-04-27 23:42:35 -0700201 if (current_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700202 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.CurrentDelayInMs",
203 current_delay_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700204 LOG(LS_INFO) << "WebRTC.Video.CurrentDelayInMs " << current_delay_ms;
asapersson8688a4e2016-04-27 23:42:35 -0700205 }
asaperssona563c212017-03-02 08:25:46 -0800206 int delay_ms = delay_counter_.Avg(kMinRequiredSamples);
asapersson13c433c2015-10-06 04:08:15 -0700207 if (delay_ms != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700208 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", delay_ms);
sprang0ab8e812016-02-24 01:35:40 -0800209
ilnik6d5b4d62017-08-30 03:32:14 -0700210 // Aggregate content_specific_stats_ by removing experiment or simulcast
211 // information;
212 std::map<VideoContentType, ContentSpecificStats> aggregated_stats;
213 for (auto it : content_specific_stats_) {
214 // Calculate simulcast specific metrics (".S0" ... ".S2" suffixes).
215 VideoContentType content_type = it.first;
216 if (videocontenttypehelpers::GetSimulcastId(content_type) > 0) {
217 // Aggregate on experiment id.
218 videocontenttypehelpers::SetExperimentId(&content_type, 0);
219 aggregated_stats[content_type].Add(it.second);
220 }
221 // Calculate experiment specific metrics (".ExperimentGroup[0-7]" suffixes).
222 content_type = it.first;
223 if (videocontenttypehelpers::GetExperimentId(content_type) > 0) {
224 // Aggregate on simulcast id.
225 videocontenttypehelpers::SetSimulcastId(&content_type, 0);
226 aggregated_stats[content_type].Add(it.second);
227 }
228 // Calculate aggregated metrics (no suffixes. Aggregated on everything).
229 content_type = it.first;
230 videocontenttypehelpers::SetSimulcastId(&content_type, 0);
231 videocontenttypehelpers::SetExperimentId(&content_type, 0);
232 aggregated_stats[content_type].Add(it.second);
ilnik00d802b2017-04-11 10:34:31 -0700233 }
234
ilnik6d5b4d62017-08-30 03:32:14 -0700235 for (auto it : aggregated_stats) {
236 // For the metric Foo we report the following slices:
237 // WebRTC.Video.Foo,
238 // WebRTC.Video.Screenshare.Foo,
239 // WebRTC.Video.Foo.S[0-3],
240 // WebRTC.Video.Foo.ExperimentGroup[0-7],
241 // WebRTC.Video.Screenshare.Foo.S[0-3],
242 // WebRTC.Video.Screenshare.Foo.ExperimentGroup[0-7].
243 auto content_type = it.first;
244 auto stats = it.second;
245 std::string uma_prefix = UmaPrefixForContentType(content_type);
246 std::string uma_suffix = UmaSuffixForContentType(content_type);
247 // Metrics can be sliced on either simulcast id or experiment id but not
248 // both.
249 RTC_DCHECK(videocontenttypehelpers::GetExperimentId(content_type) == 0 ||
250 videocontenttypehelpers::GetSimulcastId(content_type) == 0);
ilnik00d802b2017-04-11 10:34:31 -0700251
ilnik6d5b4d62017-08-30 03:32:14 -0700252 int e2e_delay_ms = stats.e2e_delay_counter.Avg(kMinRequiredSamples);
253 if (e2e_delay_ms != -1) {
254 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
255 uma_prefix + ".EndToEndDelayInMs" + uma_suffix, e2e_delay_ms);
256 LOG(LS_INFO) << uma_prefix << ".EndToEndDelayInMs" << uma_suffix << " "
257 << e2e_delay_ms;
258 }
259 int e2e_delay_max_ms = stats.e2e_delay_counter.Max();
260 if (e2e_delay_max_ms != -1 && e2e_delay_ms != -1) {
261 RTC_HISTOGRAM_COUNTS_SPARSE_100000(
262 uma_prefix + ".EndToEndDelayMaxInMs" + uma_suffix, e2e_delay_max_ms);
263 LOG(LS_INFO) << uma_prefix << ".EndToEndDelayMaxInMs" << uma_suffix << " "
264 << e2e_delay_max_ms;
265 }
266 int interframe_delay_ms =
267 stats.interframe_delay_counter.Avg(kMinRequiredSamples);
268 if (interframe_delay_ms != -1) {
269 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
270 uma_prefix + ".InterframeDelayInMs" + uma_suffix,
271 interframe_delay_ms);
272 LOG(LS_INFO) << uma_prefix << ".InterframeDelayInMs" << uma_suffix << " "
273 << interframe_delay_ms;
274 }
275 int interframe_delay_max_ms = stats.interframe_delay_counter.Max();
276 if (interframe_delay_max_ms != -1 && interframe_delay_ms != -1) {
277 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
278 uma_prefix + ".InterframeDelayMaxInMs" + uma_suffix,
279 interframe_delay_max_ms);
280 LOG(LS_INFO) << uma_prefix << ".InterframeDelayMaxInMs" << uma_suffix
281 << " " << interframe_delay_max_ms;
282 }
ilnik00d802b2017-04-11 10:34:31 -0700283
ilnik6d5b4d62017-08-30 03:32:14 -0700284 int width = stats.received_width.Avg(kMinRequiredSamples);
285 if (width != -1) {
286 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
287 uma_prefix + ".ReceivedWidthInPixels" + uma_suffix, width);
288 LOG(LS_INFO) << uma_prefix << ".ReceivedWidthInPixels" << uma_suffix
289 << " " << width;
290 }
asapersson1490f7a2016-09-23 02:09:46 -0700291
ilnik6d5b4d62017-08-30 03:32:14 -0700292 int height = stats.received_height.Avg(kMinRequiredSamples);
293 if (height != -1) {
294 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
295 uma_prefix + ".ReceivedHeightInPixels" + uma_suffix, height);
296 LOG(LS_INFO) << uma_prefix << ".ReceivedHeightInPixels" << uma_suffix
297 << " " << height;
298 }
ilnik4257ab22017-07-03 01:15:58 -0700299
ilnik6d5b4d62017-08-30 03:32:14 -0700300 if (content_type != VideoContentType::UNSPECIFIED) {
301 // Don't report these 3 metrics unsliced, as more precise variants
302 // are reported separately in this method.
303 float flow_duration_sec = stats.flow_duration_ms / 1000.0;
304 if (flow_duration_sec >= metrics::kMinRunTimeInSeconds) {
305 int media_bitrate_kbps = static_cast<int>(stats.total_media_bytes * 8 /
306 flow_duration_sec / 1000);
307 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
308 uma_prefix + ".MediaBitrateReceivedInKbps" + uma_suffix,
309 media_bitrate_kbps);
310 LOG(LS_INFO) << uma_prefix << ".MediaBitrateReceivedInKbps"
311 << uma_suffix << " " << media_bitrate_kbps;
312 }
313
314 int num_total_frames =
315 stats.frame_counts.key_frames + stats.frame_counts.delta_frames;
316 if (num_total_frames >= kMinRequiredSamples) {
317 int num_key_frames = stats.frame_counts.key_frames;
318 int key_frames_permille =
319 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
320 RTC_HISTOGRAM_COUNTS_SPARSE_1000(
321 uma_prefix + ".KeyFramesReceivedInPermille" + uma_suffix,
322 key_frames_permille);
323 LOG(LS_INFO) << uma_prefix << ".KeyFramesReceivedInPermille"
324 << uma_suffix << " " << key_frames_permille;
325 }
326
327 int qp = stats.qp_counter.Avg(kMinRequiredSamples);
328 if (qp != -1) {
329 RTC_HISTOGRAM_COUNTS_SPARSE_200(
330 uma_prefix + ".Decoded.Vp8.Qp" + uma_suffix, qp);
331 LOG(LS_INFO) << uma_prefix << ".Decoded.Vp8.Qp" << uma_suffix << " "
332 << qp;
333 }
334 }
ilnik4257ab22017-07-03 01:15:58 -0700335 }
336
sprang0ab8e812016-02-24 01:35:40 -0800337 StreamDataCounters rtp = stats_.rtp_stats;
338 StreamDataCounters rtx;
339 for (auto it : rtx_stats_)
340 rtx.Add(it.second);
341 StreamDataCounters rtp_rtx = rtp;
342 rtp_rtx.Add(rtx);
343 int64_t elapsed_sec =
344 rtp_rtx.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) / 1000;
asapersson2077f2f2017-05-11 05:37:35 -0700345 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
asapersson1d02d3e2016-09-09 22:40:25 -0700346 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800347 "WebRTC.Video.BitrateReceivedInKbps",
348 static_cast<int>(rtp_rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
349 1000));
ilnik6d5b4d62017-08-30 03:32:14 -0700350 int media_bitrate_kbs =
351 static_cast<int>(rtp.MediaPayloadBytes() * 8 / elapsed_sec / 1000);
352 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.MediaBitrateReceivedInKbps",
353 media_bitrate_kbs);
354 LOG(LS_INFO) << "WebRTC.Video.MediaBitrateReceivedInKbps "
355 << media_bitrate_kbs;
asapersson1d02d3e2016-09-09 22:40:25 -0700356 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800357 "WebRTC.Video.PaddingBitrateReceivedInKbps",
358 static_cast<int>(rtp_rtx.transmitted.padding_bytes * 8 / elapsed_sec /
359 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700360 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800361 "WebRTC.Video.RetransmittedBitrateReceivedInKbps",
362 static_cast<int>(rtp_rtx.retransmitted.TotalBytes() * 8 / elapsed_sec /
363 1000));
364 if (!rtx_stats_.empty()) {
asapersson1d02d3e2016-09-09 22:40:25 -0700365 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtxBitrateReceivedInKbps",
366 static_cast<int>(rtx.transmitted.TotalBytes() *
367 8 / elapsed_sec / 1000));
sprang0ab8e812016-02-24 01:35:40 -0800368 }
brandtrb5f2c3f2016-10-04 23:28:39 -0700369 if (config_.rtp.ulpfec.ulpfec_payload_type != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700370 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800371 "WebRTC.Video.FecBitrateReceivedInKbps",
372 static_cast<int>(rtp_rtx.fec.TotalBytes() * 8 / elapsed_sec / 1000));
373 }
sprang07fb9be2016-02-24 07:55:00 -0800374 const RtcpPacketTypeCounter& counters = stats_.rtcp_packet_type_counts;
asapersson1d02d3e2016-09-09 22:40:25 -0700375 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.NackPacketsSentPerMinute",
376 counters.nack_packets * 60 / elapsed_sec);
377 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.FirPacketsSentPerMinute",
378 counters.fir_packets * 60 / elapsed_sec);
379 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.PliPacketsSentPerMinute",
380 counters.pli_packets * 60 / elapsed_sec);
sprang07fb9be2016-02-24 07:55:00 -0800381 if (counters.nack_requests > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700382 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.UniqueNackRequestsSentInPercent",
383 counters.UniqueNackRequestsInPercent());
sprang07fb9be2016-02-24 07:55:00 -0800384 }
sprang0ab8e812016-02-24 01:35:40 -0800385 }
palmkvista40672a2017-01-13 05:58:34 -0800386
387 if (num_certain_states_ >= kBadCallMinRequiredSamples) {
388 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Any",
389 100 * num_bad_states_ / num_certain_states_);
390 }
391 rtc::Optional<double> fps_fraction =
392 fps_threshold_.FractionHigh(kBadCallMinRequiredSamples);
393 if (fps_fraction) {
394 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRate",
395 static_cast<int>(100 * (1 - *fps_fraction)));
396 }
397 rtc::Optional<double> variance_fraction =
398 variance_threshold_.FractionHigh(kBadCallMinRequiredSamples);
399 if (variance_fraction) {
400 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRateVariance",
401 static_cast<int>(100 * *variance_fraction));
402 }
403 rtc::Optional<double> qp_fraction =
404 qp_threshold_.FractionHigh(kBadCallMinRequiredSamples);
405 if (qp_fraction) {
406 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Qp",
407 static_cast<int>(100 * *qp_fraction));
408 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200409}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000410
palmkvist349092b2016-12-13 02:45:57 -0800411void ReceiveStatisticsProxy::QualitySample() {
412 int64_t now = clock_->TimeInMilliseconds();
413 if (last_sample_time_ + kMinSampleLengthMs > now)
414 return;
415
416 double fps =
417 render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_);
418 int qp = qp_sample_.Avg(1);
419
420 bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true);
421 bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false);
422 bool prev_variance_bad = variance_threshold_.IsHigh().value_or(false);
423 bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad;
424
425 fps_threshold_.AddMeasurement(static_cast<int>(fps));
426 if (qp != -1)
427 qp_threshold_.AddMeasurement(qp);
428 rtc::Optional<double> fps_variance_opt = fps_threshold_.CalculateVariance();
429 double fps_variance = fps_variance_opt.value_or(0);
430 if (fps_variance_opt) {
431 variance_threshold_.AddMeasurement(static_cast<int>(fps_variance));
432 }
433
434 bool fps_bad = !fps_threshold_.IsHigh().value_or(true);
435 bool qp_bad = qp_threshold_.IsHigh().value_or(false);
436 bool variance_bad = variance_threshold_.IsHigh().value_or(false);
437 bool any_bad = fps_bad || qp_bad || variance_bad;
438
439 if (!prev_any_bad && any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800440 LOG(LS_INFO) << "Bad call (any) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800441 } else if (prev_any_bad && !any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800442 LOG(LS_INFO) << "Bad call (any) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800443 }
444
445 if (!prev_fps_bad && fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800446 LOG(LS_INFO) << "Bad call (fps) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800447 } else if (prev_fps_bad && !fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800448 LOG(LS_INFO) << "Bad call (fps) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800449 }
450
451 if (!prev_qp_bad && qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800452 LOG(LS_INFO) << "Bad call (qp) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800453 } else if (prev_qp_bad && !qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800454 LOG(LS_INFO) << "Bad call (qp) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800455 }
456
457 if (!prev_variance_bad && variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800458 LOG(LS_INFO) << "Bad call (variance) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800459 } else if (prev_variance_bad && !variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800460 LOG(LS_INFO) << "Bad call (variance) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800461 }
462
palmkvistc7e7e072017-01-09 07:23:33 -0800463 LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_)
464 << " fps: " << fps << " fps_bad: " << fps_bad << " qp: " << qp
465 << " qp_bad: " << qp_bad << " variance_bad: " << variance_bad
466 << " fps_variance: " << fps_variance;
palmkvist349092b2016-12-13 02:45:57 -0800467
468 last_sample_time_ = now;
469 qp_sample_.Reset();
palmkvista40672a2017-01-13 05:58:34 -0800470
471 if (fps_threshold_.IsHigh() || variance_threshold_.IsHigh() ||
472 qp_threshold_.IsHigh()) {
473 if (any_bad)
474 ++num_bad_states_;
475 ++num_certain_states_;
476 }
palmkvist349092b2016-12-13 02:45:57 -0800477}
478
asapersson0255acb2017-03-28 02:44:58 -0700479void ReceiveStatisticsProxy::UpdateFramerate(int64_t now_ms) const {
philipela45102f2017-02-22 05:30:39 -0800480 int64_t old_frames_ms = now_ms - kRateStatisticsWindowSizeMs;
481 while (!frame_window_.empty() &&
482 frame_window_.begin()->first < old_frames_ms) {
philipela45102f2017-02-22 05:30:39 -0800483 frame_window_.erase(frame_window_.begin());
484 }
485
486 size_t framerate =
487 (frame_window_.size() * 1000 + 500) / kRateStatisticsWindowSizeMs;
philipela45102f2017-02-22 05:30:39 -0800488 stats_.network_frame_rate = static_cast<int>(framerate);
philipela45102f2017-02-22 05:30:39 -0800489}
490
sprang@webrtc.org09315702014-02-07 12:06:29 +0000491VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const {
Peter Boströmf2f82832015-05-01 13:00:41 +0200492 rtc::CritScope lock(&crit_);
sprang948b2752017-05-04 02:47:13 -0700493 // Get current frame rates here, as only updating them on new frames prevents
494 // us from ever correctly displaying frame rate of 0.
495 int64_t now_ms = clock_->TimeInMilliseconds();
496 UpdateFramerate(now_ms);
497 stats_.render_frame_rate = renders_fps_estimator_.Rate(now_ms).value_or(0);
498 stats_.decode_frame_rate = decode_fps_estimator_.Rate(now_ms).value_or(0);
asapersson0255acb2017-03-28 02:44:58 -0700499 stats_.total_bitrate_bps =
500 static_cast<int>(total_byte_tracker_.ComputeRate() * 8);
ilnika79cc282017-08-23 05:24:10 -0700501 stats_.interframe_delay_max_ms =
502 interframe_delay_max_moving_.Max(now_ms).value_or(-1);
ilnik75204c52017-09-04 03:35:40 -0700503 stats_.timing_frame_info = timing_frame_info_counter_.Max(now_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700504 stats_.content_type = last_content_type_;
pbos@webrtc.org55707692014-12-19 15:45:03 +0000505 return stats_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000506}
507
pbosf42376c2015-08-28 07:35:32 -0700508void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) {
509 rtc::CritScope lock(&crit_);
510 stats_.current_payload_type = payload_type;
511}
512
Peter Boströmb7d9a972015-12-18 16:01:11 +0100513void ReceiveStatisticsProxy::OnDecoderImplementationName(
514 const char* implementation_name) {
515 rtc::CritScope lock(&crit_);
516 stats_.decoder_implementation_name = implementation_name;
517}
pbosf42376c2015-08-28 07:35:32 -0700518void ReceiveStatisticsProxy::OnIncomingRate(unsigned int framerate,
519 unsigned int bitrate_bps) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200520 rtc::CritScope lock(&crit_);
palmkvista40672a2017-01-13 05:58:34 -0800521 if (stats_.rtp_stats.first_packet_time_ms != -1)
522 QualitySample();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000523}
524
philipela45102f2017-02-22 05:30:39 -0800525void ReceiveStatisticsProxy::OnFrameBufferTimingsUpdated(
526 int decode_ms,
527 int max_decode_ms,
528 int current_delay_ms,
529 int target_delay_ms,
530 int jitter_buffer_ms,
531 int min_playout_delay_ms,
532 int render_delay_ms) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200533 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000534 stats_.decode_ms = decode_ms;
535 stats_.max_decode_ms = max_decode_ms;
536 stats_.current_delay_ms = current_delay_ms;
537 stats_.target_delay_ms = target_delay_ms;
538 stats_.jitter_buffer_ms = jitter_buffer_ms;
539 stats_.min_playout_delay_ms = min_playout_delay_ms;
540 stats_.render_delay_ms = render_delay_ms;
asapersson6718e972015-07-24 00:20:58 -0700541 decode_time_counter_.Add(decode_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700542 jitter_buffer_delay_counter_.Add(jitter_buffer_ms);
543 target_delay_counter_.Add(target_delay_ms);
544 current_delay_counter_.Add(current_delay_ms);
asaperssona1862882016-04-18 00:41:05 -0700545 // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time +
546 // render delay).
philipela45102f2017-02-22 05:30:39 -0800547 delay_counter_.Add(target_delay_ms + avg_rtt_ms_ / 2);
pbos@webrtc.org98c04b32014-12-18 13:12:52 +0000548}
549
ilnik2edc6842017-07-06 03:06:50 -0700550void ReceiveStatisticsProxy::OnTimingFrameInfoUpdated(
551 const TimingFrameInfo& info) {
ilnik75204c52017-09-04 03:35:40 -0700552 int64_t now_ms = clock_->TimeInMilliseconds();
ilnik2edc6842017-07-06 03:06:50 -0700553 rtc::CritScope lock(&crit_);
ilnik75204c52017-09-04 03:35:40 -0700554 timing_frame_info_counter_.Add(info, now_ms);
ilnik2edc6842017-07-06 03:06:50 -0700555}
556
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000557void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated(
558 uint32_t ssrc,
559 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200560 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000561 if (stats_.ssrc != ssrc)
562 return;
563 stats_.rtcp_packet_type_counts = packet_counter;
564}
565
sprang@webrtc.org09315702014-02-07 12:06:29 +0000566void ReceiveStatisticsProxy::StatisticsUpdated(
567 const webrtc::RtcpStatistics& statistics,
568 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200569 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700570 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000571 // receive stats from one of them.
572 if (stats_.ssrc != ssrc)
573 return;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000574 stats_.rtcp_stats = statistics;
Åsa Persson3c391cb2015-04-27 10:09:49 +0200575 report_block_stats_.Store(statistics, ssrc, 0);
asapersson0c43f772016-11-30 01:42:26 -0800576
577 if (first_report_block_time_ms_ == -1)
578 first_report_block_time_ms_ = clock_->TimeInMilliseconds();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000579}
580
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000581void ReceiveStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200582 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700583 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000584 // receive stats from one of them.
585 if (stats_.ssrc != ssrc)
586 return;
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000587 stats_.c_name = cname;
588}
589
sprang@webrtc.org09315702014-02-07 12:06:29 +0000590void ReceiveStatisticsProxy::DataCountersUpdated(
591 const webrtc::StreamDataCounters& counters,
592 uint32_t ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700593 size_t last_total_bytes = 0;
594 size_t total_bytes = 0;
Peter Boströmf2f82832015-05-01 13:00:41 +0200595 rtc::CritScope lock(&crit_);
sprang0ab8e812016-02-24 01:35:40 -0800596 if (ssrc == stats_.ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700597 last_total_bytes = stats_.rtp_stats.transmitted.TotalBytes();
598 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800599 stats_.rtp_stats = counters;
600 } else {
601 auto it = rtx_stats_.find(ssrc);
602 if (it != rtx_stats_.end()) {
asapersson0255acb2017-03-28 02:44:58 -0700603 last_total_bytes = it->second.transmitted.TotalBytes();
604 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800605 it->second = counters;
606 } else {
607 RTC_NOTREACHED() << "Unexpected stream ssrc: " << ssrc;
608 }
609 }
asapersson0255acb2017-03-28 02:44:58 -0700610 if (total_bytes > last_total_bytes)
611 total_byte_tracker_.AddSamples(total_bytes - last_total_bytes);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000612}
613
ilnik00d802b2017-04-11 10:34:31 -0700614void ReceiveStatisticsProxy::OnDecodedFrame(rtc::Optional<uint8_t> qp,
615 VideoContentType content_type) {
sprang@webrtc.org09315702014-02-07 12:06:29 +0000616 uint64_t now = clock_->TimeInMilliseconds();
617
Peter Boströmf2f82832015-05-01 13:00:41 +0200618 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700619
620 ContentSpecificStats* content_specific_stats =
621 &content_specific_stats_[content_type];
sakale5ba44e2016-10-26 07:09:24 -0700622 ++stats_.frames_decoded;
sakalcc452e12017-02-09 04:53:45 -0800623 if (qp) {
624 if (!stats_.qp_sum) {
625 if (stats_.frames_decoded != 1) {
626 LOG(LS_WARNING)
627 << "Frames decoded was not 1 when first qp value was received.";
628 stats_.frames_decoded = 1;
629 }
630 stats_.qp_sum = rtc::Optional<uint64_t>(0);
631 }
632 *stats_.qp_sum += *qp;
ilnik6d5b4d62017-08-30 03:32:14 -0700633 content_specific_stats->qp_counter.Add(*qp);
sakalcc452e12017-02-09 04:53:45 -0800634 } else if (stats_.qp_sum) {
635 LOG(LS_WARNING)
636 << "QP sum was already set and no QP was given for a frame.";
637 stats_.qp_sum = rtc::Optional<uint64_t>();
638 }
ilnik00d802b2017-04-11 10:34:31 -0700639 last_content_type_ = content_type;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000640 decode_fps_estimator_.Update(1, now);
ilnik4257ab22017-07-03 01:15:58 -0700641 if (last_decoded_frame_time_ms_) {
642 int64_t interframe_delay_ms = now - *last_decoded_frame_time_ms_;
643 RTC_DCHECK_GE(interframe_delay_ms, 0);
ilnika79cc282017-08-23 05:24:10 -0700644 interframe_delay_max_moving_.Add(interframe_delay_ms, now);
ilnik6d5b4d62017-08-30 03:32:14 -0700645 content_specific_stats->interframe_delay_counter.Add(interframe_delay_ms);
646 content_specific_stats->flow_duration_ms += interframe_delay_ms;
ilnik4257ab22017-07-03 01:15:58 -0700647 }
648 last_decoded_frame_time_ms_.emplace(now);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000649}
650
asapersson1490f7a2016-09-23 02:09:46 -0700651void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) {
652 int width = frame.width();
653 int height = frame.height();
asaperssonf839dcc2015-10-08 00:41:59 -0700654 RTC_DCHECK_GT(width, 0);
655 RTC_DCHECK_GT(height, 0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000656 uint64_t now = clock_->TimeInMilliseconds();
Peter Boströmf2f82832015-05-01 13:00:41 +0200657 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700658 ContentSpecificStats* content_specific_stats =
659 &content_specific_stats_[last_content_type_];
sprang@webrtc.org09315702014-02-07 12:06:29 +0000660 renders_fps_estimator_.Update(1, now);
hbos50cfe1f2017-01-23 07:21:55 -0800661 ++stats_.frames_rendered;
asapersson2e5cfcd2016-08-11 08:41:18 -0700662 stats_.width = width;
663 stats_.height = height;
Tim Psiaki63046262015-09-14 10:38:08 -0700664 render_fps_tracker_.AddSamples(1);
asaperssonf839dcc2015-10-08 00:41:59 -0700665 render_pixel_tracker_.AddSamples(sqrt(width * height));
ilnik6d5b4d62017-08-30 03:32:14 -0700666 content_specific_stats->received_width.Add(width);
667 content_specific_stats->received_height.Add(height);
asapersson1490f7a2016-09-23 02:09:46 -0700668
669 if (frame.ntp_time_ms() > 0) {
670 int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms();
ilnik00d802b2017-04-11 10:34:31 -0700671 if (delay_ms >= 0) {
ilnik6d5b4d62017-08-30 03:32:14 -0700672 content_specific_stats->e2e_delay_counter.Add(delay_ms);
ilnik00d802b2017-04-11 10:34:31 -0700673 }
asapersson1490f7a2016-09-23 02:09:46 -0700674 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000675}
676
asaperssonde9e5ff2016-11-02 07:14:03 -0700677void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t sync_offset_ms,
678 double estimated_freq_khz) {
asaperssonf8cdd182016-03-15 01:00:47 -0700679 rtc::CritScope lock(&crit_);
680 sync_offset_counter_.Add(std::abs(sync_offset_ms));
681 stats_.sync_offset_ms = sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700682
683 const double kMaxFreqKhz = 10000.0;
684 int offset_khz = kMaxFreqKhz;
685 // Should not be zero or negative. If so, report max.
686 if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0)
687 offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5);
688
689 freq_offset_counter_.Add(offset_khz);
asaperssonf8cdd182016-03-15 01:00:47 -0700690}
691
pbos@webrtc.org55707692014-12-19 15:45:03 +0000692void ReceiveStatisticsProxy::OnReceiveRatesUpdated(uint32_t bitRate,
693 uint32_t frameRate) {
694}
695
philipela45102f2017-02-22 05:30:39 -0800696void ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe,
ilnik6d5b4d62017-08-30 03:32:14 -0700697 size_t size_bytes,
698 VideoContentType content_type) {
philipela45102f2017-02-22 05:30:39 -0800699 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700700 if (is_keyframe) {
philipela45102f2017-02-22 05:30:39 -0800701 ++stats_.frame_counts.key_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700702 } else {
philipela45102f2017-02-22 05:30:39 -0800703 ++stats_.frame_counts.delta_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700704 }
705
706 ContentSpecificStats* content_specific_stats =
707 &content_specific_stats_[content_type];
708
709 content_specific_stats->total_media_bytes += size_bytes;
710 if (is_keyframe) {
711 ++content_specific_stats->frame_counts.key_frames;
712 } else {
713 ++content_specific_stats->frame_counts.delta_frames;
714 }
philipela45102f2017-02-22 05:30:39 -0800715
716 int64_t now_ms = clock_->TimeInMilliseconds();
philipela45102f2017-02-22 05:30:39 -0800717 frame_window_.insert(std::make_pair(now_ms, size_bytes));
asapersson0255acb2017-03-28 02:44:58 -0700718 UpdateFramerate(now_ms);
philipela45102f2017-02-22 05:30:39 -0800719}
720
pbos@webrtc.org55707692014-12-19 15:45:03 +0000721void ReceiveStatisticsProxy::OnFrameCountsUpdated(
722 const FrameCounts& frame_counts) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200723 rtc::CritScope lock(&crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000724 stats_.frame_counts = frame_counts;
725}
726
pbos@webrtc.org55707692014-12-19 15:45:03 +0000727void ReceiveStatisticsProxy::OnDiscardedPacketsUpdated(int discarded_packets) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200728 rtc::CritScope lock(&crit_);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000729 stats_.discarded_packets = discarded_packets;
730}
731
asapersson86b01602015-10-20 23:55:26 -0700732void ReceiveStatisticsProxy::OnPreDecode(
733 const EncodedImage& encoded_image,
734 const CodecSpecificInfo* codec_specific_info) {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200735 if (!codec_specific_info || encoded_image.qp_ == -1) {
asapersson86b01602015-10-20 23:55:26 -0700736 return;
737 }
738 if (codec_specific_info->codecType == kVideoCodecVP8) {
739 qp_counters_.vp8.Add(encoded_image.qp_);
palmkvist349092b2016-12-13 02:45:57 -0800740 rtc::CritScope lock(&crit_);
741 qp_sample_.Add(encoded_image.qp_);
asapersson86b01602015-10-20 23:55:26 -0700742 }
743}
744
sprang3e86e7e2017-08-22 09:23:28 -0700745void ReceiveStatisticsProxy::OnStreamInactive() {
746 // TODO(sprang): Figure out any other state that should be reset.
747
748 rtc::CritScope lock(&crit_);
749 // Don't report inter-frame delay if stream was paused.
750 last_decoded_frame_time_ms_.reset();
751}
752
asaperssond89920b2015-07-22 06:52:00 -0700753void ReceiveStatisticsProxy::SampleCounter::Add(int sample) {
754 sum += sample;
755 ++num_samples;
ilnik6d5b4d62017-08-30 03:32:14 -0700756 if (!max || sample > *max) {
757 max.emplace(sample);
758 }
759}
760
761void ReceiveStatisticsProxy::SampleCounter::Add(const SampleCounter& other) {
762 sum += other.sum;
763 num_samples += other.num_samples;
764 if (other.max && (!max || *max < *other.max))
765 max = other.max;
asaperssond89920b2015-07-22 06:52:00 -0700766}
767
asapersson6966bd52017-01-03 00:44:06 -0800768int ReceiveStatisticsProxy::SampleCounter::Avg(
769 int64_t min_required_samples) const {
asaperssond89920b2015-07-22 06:52:00 -0700770 if (num_samples < min_required_samples || num_samples == 0)
771 return -1;
asapersson6966bd52017-01-03 00:44:06 -0800772 return static_cast<int>(sum / num_samples);
asaperssond89920b2015-07-22 06:52:00 -0700773}
774
ilnik6d5b4d62017-08-30 03:32:14 -0700775int ReceiveStatisticsProxy::SampleCounter::Max() const {
776 return max.value_or(-1);
777}
778
palmkvist349092b2016-12-13 02:45:57 -0800779void ReceiveStatisticsProxy::SampleCounter::Reset() {
780 num_samples = 0;
781 sum = 0;
ilnik6d5b4d62017-08-30 03:32:14 -0700782 max.reset();
palmkvist349092b2016-12-13 02:45:57 -0800783}
784
philipela45102f2017-02-22 05:30:39 -0800785void ReceiveStatisticsProxy::OnRttUpdate(int64_t avg_rtt_ms,
786 int64_t max_rtt_ms) {
787 rtc::CritScope lock(&crit_);
788 avg_rtt_ms_ = avg_rtt_ms;
789}
790
ilnik6d5b4d62017-08-30 03:32:14 -0700791void ReceiveStatisticsProxy::ContentSpecificStats::Add(
792 const ContentSpecificStats& other) {
793 e2e_delay_counter.Add(other.e2e_delay_counter);
794 interframe_delay_counter.Add(other.interframe_delay_counter);
795 flow_duration_ms += other.flow_duration_ms;
796 total_media_bytes += other.total_media_bytes;
797 received_height.Add(other.received_height);
798 received_width.Add(other.received_width);
799 qp_counter.Add(other.qp_counter);
800 frame_counts.key_frames += other.frame_counts.key_frames;
801 frame_counts.delta_frames += other.frame_counts.delta_frames;
802}
803
sprang@webrtc.org09315702014-02-07 12:06:29 +0000804} // namespace webrtc