blob: 2448bad746f32e2b0d740152ae59313a7c941753 [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);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000504 return stats_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000505}
506
pbosf42376c2015-08-28 07:35:32 -0700507void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) {
508 rtc::CritScope lock(&crit_);
509 stats_.current_payload_type = payload_type;
510}
511
Peter Boströmb7d9a972015-12-18 16:01:11 +0100512void ReceiveStatisticsProxy::OnDecoderImplementationName(
513 const char* implementation_name) {
514 rtc::CritScope lock(&crit_);
515 stats_.decoder_implementation_name = implementation_name;
516}
pbosf42376c2015-08-28 07:35:32 -0700517void ReceiveStatisticsProxy::OnIncomingRate(unsigned int framerate,
518 unsigned int bitrate_bps) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200519 rtc::CritScope lock(&crit_);
palmkvista40672a2017-01-13 05:58:34 -0800520 if (stats_.rtp_stats.first_packet_time_ms != -1)
521 QualitySample();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000522}
523
philipela45102f2017-02-22 05:30:39 -0800524void ReceiveStatisticsProxy::OnFrameBufferTimingsUpdated(
525 int decode_ms,
526 int max_decode_ms,
527 int current_delay_ms,
528 int target_delay_ms,
529 int jitter_buffer_ms,
530 int min_playout_delay_ms,
531 int render_delay_ms) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200532 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000533 stats_.decode_ms = decode_ms;
534 stats_.max_decode_ms = max_decode_ms;
535 stats_.current_delay_ms = current_delay_ms;
536 stats_.target_delay_ms = target_delay_ms;
537 stats_.jitter_buffer_ms = jitter_buffer_ms;
538 stats_.min_playout_delay_ms = min_playout_delay_ms;
539 stats_.render_delay_ms = render_delay_ms;
asapersson6718e972015-07-24 00:20:58 -0700540 decode_time_counter_.Add(decode_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700541 jitter_buffer_delay_counter_.Add(jitter_buffer_ms);
542 target_delay_counter_.Add(target_delay_ms);
543 current_delay_counter_.Add(current_delay_ms);
asaperssona1862882016-04-18 00:41:05 -0700544 // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time +
545 // render delay).
philipela45102f2017-02-22 05:30:39 -0800546 delay_counter_.Add(target_delay_ms + avg_rtt_ms_ / 2);
pbos@webrtc.org98c04b32014-12-18 13:12:52 +0000547}
548
ilnik2edc6842017-07-06 03:06:50 -0700549void ReceiveStatisticsProxy::OnTimingFrameInfoUpdated(
550 const TimingFrameInfo& info) {
ilnik75204c52017-09-04 03:35:40 -0700551 int64_t now_ms = clock_->TimeInMilliseconds();
ilnik2edc6842017-07-06 03:06:50 -0700552 rtc::CritScope lock(&crit_);
ilnik75204c52017-09-04 03:35:40 -0700553 timing_frame_info_counter_.Add(info, now_ms);
ilnik2edc6842017-07-06 03:06:50 -0700554}
555
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000556void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated(
557 uint32_t ssrc,
558 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200559 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000560 if (stats_.ssrc != ssrc)
561 return;
562 stats_.rtcp_packet_type_counts = packet_counter;
563}
564
sprang@webrtc.org09315702014-02-07 12:06:29 +0000565void ReceiveStatisticsProxy::StatisticsUpdated(
566 const webrtc::RtcpStatistics& statistics,
567 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200568 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700569 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000570 // receive stats from one of them.
571 if (stats_.ssrc != ssrc)
572 return;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000573 stats_.rtcp_stats = statistics;
Åsa Persson3c391cb2015-04-27 10:09:49 +0200574 report_block_stats_.Store(statistics, ssrc, 0);
asapersson0c43f772016-11-30 01:42:26 -0800575
576 if (first_report_block_time_ms_ == -1)
577 first_report_block_time_ms_ = clock_->TimeInMilliseconds();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000578}
579
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000580void ReceiveStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200581 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700582 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000583 // receive stats from one of them.
584 if (stats_.ssrc != ssrc)
585 return;
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000586 stats_.c_name = cname;
587}
588
sprang@webrtc.org09315702014-02-07 12:06:29 +0000589void ReceiveStatisticsProxy::DataCountersUpdated(
590 const webrtc::StreamDataCounters& counters,
591 uint32_t ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700592 size_t last_total_bytes = 0;
593 size_t total_bytes = 0;
Peter Boströmf2f82832015-05-01 13:00:41 +0200594 rtc::CritScope lock(&crit_);
sprang0ab8e812016-02-24 01:35:40 -0800595 if (ssrc == stats_.ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700596 last_total_bytes = stats_.rtp_stats.transmitted.TotalBytes();
597 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800598 stats_.rtp_stats = counters;
599 } else {
600 auto it = rtx_stats_.find(ssrc);
601 if (it != rtx_stats_.end()) {
asapersson0255acb2017-03-28 02:44:58 -0700602 last_total_bytes = it->second.transmitted.TotalBytes();
603 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800604 it->second = counters;
605 } else {
606 RTC_NOTREACHED() << "Unexpected stream ssrc: " << ssrc;
607 }
608 }
asapersson0255acb2017-03-28 02:44:58 -0700609 if (total_bytes > last_total_bytes)
610 total_byte_tracker_.AddSamples(total_bytes - last_total_bytes);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000611}
612
ilnik00d802b2017-04-11 10:34:31 -0700613void ReceiveStatisticsProxy::OnDecodedFrame(rtc::Optional<uint8_t> qp,
614 VideoContentType content_type) {
sprang@webrtc.org09315702014-02-07 12:06:29 +0000615 uint64_t now = clock_->TimeInMilliseconds();
616
Peter Boströmf2f82832015-05-01 13:00:41 +0200617 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700618
619 ContentSpecificStats* content_specific_stats =
620 &content_specific_stats_[content_type];
sakale5ba44e2016-10-26 07:09:24 -0700621 ++stats_.frames_decoded;
sakalcc452e12017-02-09 04:53:45 -0800622 if (qp) {
623 if (!stats_.qp_sum) {
624 if (stats_.frames_decoded != 1) {
625 LOG(LS_WARNING)
626 << "Frames decoded was not 1 when first qp value was received.";
627 stats_.frames_decoded = 1;
628 }
629 stats_.qp_sum = rtc::Optional<uint64_t>(0);
630 }
631 *stats_.qp_sum += *qp;
ilnik6d5b4d62017-08-30 03:32:14 -0700632 content_specific_stats->qp_counter.Add(*qp);
sakalcc452e12017-02-09 04:53:45 -0800633 } else if (stats_.qp_sum) {
634 LOG(LS_WARNING)
635 << "QP sum was already set and no QP was given for a frame.";
636 stats_.qp_sum = rtc::Optional<uint64_t>();
637 }
ilnik00d802b2017-04-11 10:34:31 -0700638 last_content_type_ = content_type;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000639 decode_fps_estimator_.Update(1, now);
ilnik4257ab22017-07-03 01:15:58 -0700640 if (last_decoded_frame_time_ms_) {
641 int64_t interframe_delay_ms = now - *last_decoded_frame_time_ms_;
642 RTC_DCHECK_GE(interframe_delay_ms, 0);
ilnika79cc282017-08-23 05:24:10 -0700643 interframe_delay_max_moving_.Add(interframe_delay_ms, now);
ilnik6d5b4d62017-08-30 03:32:14 -0700644 content_specific_stats->interframe_delay_counter.Add(interframe_delay_ms);
645 content_specific_stats->flow_duration_ms += interframe_delay_ms;
ilnik4257ab22017-07-03 01:15:58 -0700646 }
647 last_decoded_frame_time_ms_.emplace(now);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000648}
649
asapersson1490f7a2016-09-23 02:09:46 -0700650void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) {
651 int width = frame.width();
652 int height = frame.height();
asaperssonf839dcc2015-10-08 00:41:59 -0700653 RTC_DCHECK_GT(width, 0);
654 RTC_DCHECK_GT(height, 0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000655 uint64_t now = clock_->TimeInMilliseconds();
Peter Boströmf2f82832015-05-01 13:00:41 +0200656 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700657 ContentSpecificStats* content_specific_stats =
658 &content_specific_stats_[last_content_type_];
sprang@webrtc.org09315702014-02-07 12:06:29 +0000659 renders_fps_estimator_.Update(1, now);
hbos50cfe1f2017-01-23 07:21:55 -0800660 ++stats_.frames_rendered;
asapersson2e5cfcd2016-08-11 08:41:18 -0700661 stats_.width = width;
662 stats_.height = height;
Tim Psiaki63046262015-09-14 10:38:08 -0700663 render_fps_tracker_.AddSamples(1);
asaperssonf839dcc2015-10-08 00:41:59 -0700664 render_pixel_tracker_.AddSamples(sqrt(width * height));
ilnik6d5b4d62017-08-30 03:32:14 -0700665 content_specific_stats->received_width.Add(width);
666 content_specific_stats->received_height.Add(height);
asapersson1490f7a2016-09-23 02:09:46 -0700667
668 if (frame.ntp_time_ms() > 0) {
669 int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms();
ilnik00d802b2017-04-11 10:34:31 -0700670 if (delay_ms >= 0) {
ilnik6d5b4d62017-08-30 03:32:14 -0700671 content_specific_stats->e2e_delay_counter.Add(delay_ms);
ilnik00d802b2017-04-11 10:34:31 -0700672 }
asapersson1490f7a2016-09-23 02:09:46 -0700673 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000674}
675
asaperssonde9e5ff2016-11-02 07:14:03 -0700676void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t sync_offset_ms,
677 double estimated_freq_khz) {
asaperssonf8cdd182016-03-15 01:00:47 -0700678 rtc::CritScope lock(&crit_);
679 sync_offset_counter_.Add(std::abs(sync_offset_ms));
680 stats_.sync_offset_ms = sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700681
682 const double kMaxFreqKhz = 10000.0;
683 int offset_khz = kMaxFreqKhz;
684 // Should not be zero or negative. If so, report max.
685 if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0)
686 offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5);
687
688 freq_offset_counter_.Add(offset_khz);
asaperssonf8cdd182016-03-15 01:00:47 -0700689}
690
pbos@webrtc.org55707692014-12-19 15:45:03 +0000691void ReceiveStatisticsProxy::OnReceiveRatesUpdated(uint32_t bitRate,
692 uint32_t frameRate) {
693}
694
philipela45102f2017-02-22 05:30:39 -0800695void ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe,
ilnik6d5b4d62017-08-30 03:32:14 -0700696 size_t size_bytes,
697 VideoContentType content_type) {
philipela45102f2017-02-22 05:30:39 -0800698 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700699 if (is_keyframe) {
philipela45102f2017-02-22 05:30:39 -0800700 ++stats_.frame_counts.key_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700701 } else {
philipela45102f2017-02-22 05:30:39 -0800702 ++stats_.frame_counts.delta_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700703 }
704
705 ContentSpecificStats* content_specific_stats =
706 &content_specific_stats_[content_type];
707
708 content_specific_stats->total_media_bytes += size_bytes;
709 if (is_keyframe) {
710 ++content_specific_stats->frame_counts.key_frames;
711 } else {
712 ++content_specific_stats->frame_counts.delta_frames;
713 }
philipela45102f2017-02-22 05:30:39 -0800714
715 int64_t now_ms = clock_->TimeInMilliseconds();
philipela45102f2017-02-22 05:30:39 -0800716 frame_window_.insert(std::make_pair(now_ms, size_bytes));
asapersson0255acb2017-03-28 02:44:58 -0700717 UpdateFramerate(now_ms);
philipela45102f2017-02-22 05:30:39 -0800718}
719
pbos@webrtc.org55707692014-12-19 15:45:03 +0000720void ReceiveStatisticsProxy::OnFrameCountsUpdated(
721 const FrameCounts& frame_counts) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200722 rtc::CritScope lock(&crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000723 stats_.frame_counts = frame_counts;
724}
725
pbos@webrtc.org55707692014-12-19 15:45:03 +0000726void ReceiveStatisticsProxy::OnDiscardedPacketsUpdated(int discarded_packets) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200727 rtc::CritScope lock(&crit_);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000728 stats_.discarded_packets = discarded_packets;
729}
730
asapersson86b01602015-10-20 23:55:26 -0700731void ReceiveStatisticsProxy::OnPreDecode(
732 const EncodedImage& encoded_image,
733 const CodecSpecificInfo* codec_specific_info) {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200734 if (!codec_specific_info || encoded_image.qp_ == -1) {
asapersson86b01602015-10-20 23:55:26 -0700735 return;
736 }
737 if (codec_specific_info->codecType == kVideoCodecVP8) {
738 qp_counters_.vp8.Add(encoded_image.qp_);
palmkvist349092b2016-12-13 02:45:57 -0800739 rtc::CritScope lock(&crit_);
740 qp_sample_.Add(encoded_image.qp_);
asapersson86b01602015-10-20 23:55:26 -0700741 }
742}
743
sprang3e86e7e2017-08-22 09:23:28 -0700744void ReceiveStatisticsProxy::OnStreamInactive() {
745 // TODO(sprang): Figure out any other state that should be reset.
746
747 rtc::CritScope lock(&crit_);
748 // Don't report inter-frame delay if stream was paused.
749 last_decoded_frame_time_ms_.reset();
750}
751
asaperssond89920b2015-07-22 06:52:00 -0700752void ReceiveStatisticsProxy::SampleCounter::Add(int sample) {
753 sum += sample;
754 ++num_samples;
ilnik6d5b4d62017-08-30 03:32:14 -0700755 if (!max || sample > *max) {
756 max.emplace(sample);
757 }
758}
759
760void ReceiveStatisticsProxy::SampleCounter::Add(const SampleCounter& other) {
761 sum += other.sum;
762 num_samples += other.num_samples;
763 if (other.max && (!max || *max < *other.max))
764 max = other.max;
asaperssond89920b2015-07-22 06:52:00 -0700765}
766
asapersson6966bd52017-01-03 00:44:06 -0800767int ReceiveStatisticsProxy::SampleCounter::Avg(
768 int64_t min_required_samples) const {
asaperssond89920b2015-07-22 06:52:00 -0700769 if (num_samples < min_required_samples || num_samples == 0)
770 return -1;
asapersson6966bd52017-01-03 00:44:06 -0800771 return static_cast<int>(sum / num_samples);
asaperssond89920b2015-07-22 06:52:00 -0700772}
773
ilnik6d5b4d62017-08-30 03:32:14 -0700774int ReceiveStatisticsProxy::SampleCounter::Max() const {
775 return max.value_or(-1);
776}
777
palmkvist349092b2016-12-13 02:45:57 -0800778void ReceiveStatisticsProxy::SampleCounter::Reset() {
779 num_samples = 0;
780 sum = 0;
ilnik6d5b4d62017-08-30 03:32:14 -0700781 max.reset();
palmkvist349092b2016-12-13 02:45:57 -0800782}
783
philipela45102f2017-02-22 05:30:39 -0800784void ReceiveStatisticsProxy::OnRttUpdate(int64_t avg_rtt_ms,
785 int64_t max_rtt_ms) {
786 rtc::CritScope lock(&crit_);
787 avg_rtt_ms_ = avg_rtt_ms;
788}
789
ilnik6d5b4d62017-08-30 03:32:14 -0700790void ReceiveStatisticsProxy::ContentSpecificStats::Add(
791 const ContentSpecificStats& other) {
792 e2e_delay_counter.Add(other.e2e_delay_counter);
793 interframe_delay_counter.Add(other.interframe_delay_counter);
794 flow_duration_ms += other.flow_duration_ms;
795 total_media_bytes += other.total_media_bytes;
796 received_height.Add(other.received_height);
797 received_width.Add(other.received_width);
798 qp_counter.Add(other.qp_counter);
799 frame_counts.key_frames += other.frame_counts.key_frames;
800 frame_counts.delta_frames += other.frame_counts.delta_frames;
801}
802
sprang@webrtc.org09315702014-02-07 12:06:29 +0000803} // namespace webrtc