blob: 4673707cd95b10654f3f344bccebc9098c3bf122 [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>
philipela45102f2017-02-22 05:30:39 -080015#include <utility>
asaperssonf839dcc2015-10-08 00:41:59 -070016
Henrik Kjellander2557b862015-11-18 22:00:21 +010017#include "webrtc/modules/video_coding/include/video_codec_interface.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020018#include "webrtc/rtc_base/checks.h"
19#include "webrtc/rtc_base/logging.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010020#include "webrtc/system_wrappers/include/clock.h"
philipelbe742702016-11-30 01:31:40 -080021#include "webrtc/system_wrappers/include/field_trial.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010022#include "webrtc/system_wrappers/include/metrics.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000023
24namespace webrtc {
asaperssonde9e5ff2016-11-02 07:14:03 -070025namespace {
26// Periodic time interval for processing samples for |freq_offset_counter_|.
27const int64_t kFreqOffsetProcessIntervalMs = 40000;
palmkvist349092b2016-12-13 02:45:57 -080028
29// Configuration for bad call detection.
palmkvista40672a2017-01-13 05:58:34 -080030const int kBadCallMinRequiredSamples = 10;
palmkvist349092b2016-12-13 02:45:57 -080031const int kMinSampleLengthMs = 990;
32const int kNumMeasurements = 10;
33const int kNumMeasurementsVariance = kNumMeasurements * 1.5;
34const float kBadFraction = 0.8f;
35// For fps:
36// Low means low enough to be bad, high means high enough to be good
37const int kLowFpsThreshold = 12;
38const int kHighFpsThreshold = 14;
39// For qp and fps variance:
40// Low means low enough to be good, high means high enough to be bad
41const int kLowQpThresholdVp8 = 60;
42const int kHighQpThresholdVp8 = 70;
43const int kLowVarianceThreshold = 1;
44const int kHighVarianceThreshold = 2;
philipela45102f2017-02-22 05:30:39 -080045
46// How large window we use to calculate the framerate/bitrate.
47const int kRateStatisticsWindowSizeMs = 1000;
asaperssonde9e5ff2016-11-02 07:14:03 -070048} // namespace
sprang@webrtc.org09315702014-02-07 12:06:29 +000049
sprang0ab8e812016-02-24 01:35:40 -080050ReceiveStatisticsProxy::ReceiveStatisticsProxy(
Tommi733b5472016-06-10 17:58:01 +020051 const VideoReceiveStream::Config* config,
sprang0ab8e812016-02-24 01:35:40 -080052 Clock* clock)
pbos@webrtc.org55707692014-12-19 15:45:03 +000053 : clock_(clock),
Tommi733b5472016-06-10 17:58:01 +020054 config_(*config),
asapersson4374a092016-07-27 00:39:09 -070055 start_ms_(clock->TimeInMilliseconds()),
palmkvist349092b2016-12-13 02:45:57 -080056 last_sample_time_(clock->TimeInMilliseconds()),
57 fps_threshold_(kLowFpsThreshold,
58 kHighFpsThreshold,
59 kBadFraction,
60 kNumMeasurements),
61 qp_threshold_(kLowQpThresholdVp8,
62 kHighQpThresholdVp8,
63 kBadFraction,
64 kNumMeasurements),
65 variance_threshold_(kLowVarianceThreshold,
66 kHighVarianceThreshold,
67 kBadFraction,
68 kNumMeasurementsVariance),
palmkvista40672a2017-01-13 05:58:34 -080069 num_bad_states_(0),
70 num_certain_states_(0),
sprang@webrtc.org09315702014-02-07 12:06:29 +000071 // 1000ms window, scale 1000 for ms to s.
72 decode_fps_estimator_(1000, 1000),
Tim Psiaki63046262015-09-14 10:38:08 -070073 renders_fps_estimator_(1000, 1000),
Honghai Zhang82d78622016-05-06 11:29:15 -070074 render_fps_tracker_(100, 10u),
asaperssonde9e5ff2016-11-02 07:14:03 -070075 render_pixel_tracker_(100, 10u),
asapersson0255acb2017-03-28 02:44:58 -070076 total_byte_tracker_(100, 10u), // bucket_interval_ms, bucket_count
ilnik00d802b2017-04-11 10:34:31 -070077 e2e_delay_max_ms_video_(-1),
78 e2e_delay_max_ms_screenshare_(-1),
ilnik4257ab22017-07-03 01:15:58 -070079 interframe_delay_max_ms_video_(-1),
80 interframe_delay_max_ms_screenshare_(-1),
asapersson0c43f772016-11-30 01:42:26 -080081 freq_offset_counter_(clock, nullptr, kFreqOffsetProcessIntervalMs),
philipela45102f2017-02-22 05:30:39 -080082 first_report_block_time_ms_(-1),
ilnik00d802b2017-04-11 10:34:31 -070083 avg_rtt_ms_(0),
84 last_content_type_(VideoContentType::UNSPECIFIED) {
Tommi733b5472016-06-10 17:58:01 +020085 stats_.ssrc = config_.rtp.remote_ssrc;
brandtr14742122017-01-27 04:53:07 -080086 // TODO(brandtr): Replace |rtx_stats_| with a single instance of
87 // StreamDataCounters.
88 if (config_.rtp.rtx_ssrc) {
89 rtx_stats_[config_.rtp.rtx_ssrc] = StreamDataCounters();
90 }
sprang@webrtc.org09315702014-02-07 12:06:29 +000091}
92
Åsa Persson3c391cb2015-04-27 10:09:49 +020093ReceiveStatisticsProxy::~ReceiveStatisticsProxy() {
94 UpdateHistograms();
95}
96
asaperssond89920b2015-07-22 06:52:00 -070097void ReceiveStatisticsProxy::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -070098 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -070099 "WebRTC.Video.ReceiveStreamLifetimeInSeconds",
100 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
101
asapersson0c43f772016-11-30 01:42:26 -0800102 if (first_report_block_time_ms_ != -1 &&
103 ((clock_->TimeInMilliseconds() - first_report_block_time_ms_) / 1000) >=
104 metrics::kMinRunTimeInSeconds) {
105 int fraction_lost = report_block_stats_.FractionLostInPercent();
106 if (fraction_lost != -1) {
107 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedPacketsLostInPercent",
108 fraction_lost);
asapersson2077f2f2017-05-11 05:37:35 -0700109 LOG(LS_INFO) << "WebRTC.Video.ReceivedPacketsLostInPercent "
110 << fraction_lost;
asapersson0c43f772016-11-30 01:42:26 -0800111 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200112 }
asapersson0c43f772016-11-30 01:42:26 -0800113
asapersson6718e972015-07-24 00:20:58 -0700114 const int kMinRequiredSamples = 200;
asaperssonf839dcc2015-10-08 00:41:59 -0700115 int samples = static_cast<int>(render_fps_tracker_.TotalSampleCount());
asapersson2077f2f2017-05-11 05:37:35 -0700116 if (samples >= kMinRequiredSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700117 RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.RenderFramesPerSecond",
118 round(render_fps_tracker_.ComputeTotalRate()));
119 RTC_HISTOGRAM_COUNTS_100000(
asapersson28ba9272016-01-25 05:58:23 -0800120 "WebRTC.Video.RenderSqrtPixelsPerSecond",
Tim Psiakiad13d2f2015-11-10 16:34:50 -0800121 round(render_pixel_tracker_.ComputeTotalRate()));
asaperssonf839dcc2015-10-08 00:41:59 -0700122 }
asaperssond89920b2015-07-22 06:52:00 -0700123 int width = render_width_counter_.Avg(kMinRequiredSamples);
124 int height = render_height_counter_.Avg(kMinRequiredSamples);
125 if (width != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700126 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.ReceivedWidthInPixels", width);
127 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.ReceivedHeightInPixels", height);
asapersson2077f2f2017-05-11 05:37:35 -0700128 LOG(LS_INFO) << "WebRTC.Video.ReceivedWidthInPixels " << width;
129 LOG(LS_INFO) << "WebRTC.Video.ReceivedHeightInPixels " << height;
asaperssond89920b2015-07-22 06:52:00 -0700130 }
asaperssonf8cdd182016-03-15 01:00:47 -0700131 int sync_offset_ms = sync_offset_counter_.Avg(kMinRequiredSamples);
pbos35fdb2a2016-05-03 03:32:10 -0700132 if (sync_offset_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700133 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.AVSyncOffsetInMs", sync_offset_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700134 LOG(LS_INFO) << "WebRTC.Video.AVSyncOffsetInMs " << sync_offset_ms;
pbos35fdb2a2016-05-03 03:32:10 -0700135 }
asaperssonde9e5ff2016-11-02 07:14:03 -0700136 AggregatedStats freq_offset_stats = freq_offset_counter_.GetStats();
137 if (freq_offset_stats.num_samples > 0) {
138 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtpToNtpFreqOffsetInKhz",
139 freq_offset_stats.average);
asapersson43cb7162016-11-15 08:20:48 -0800140 LOG(LS_INFO) << "WebRTC.Video.RtpToNtpFreqOffsetInKhz, "
141 << freq_offset_stats.ToString();
asaperssonde9e5ff2016-11-02 07:14:03 -0700142 }
asaperssonf8cdd182016-03-15 01:00:47 -0700143
asaperssonb99baf82017-04-20 04:05:43 -0700144 int num_total_frames =
145 stats_.frame_counts.key_frames + stats_.frame_counts.delta_frames;
146 if (num_total_frames >= kMinRequiredSamples) {
147 int num_key_frames = stats_.frame_counts.key_frames;
philipela45102f2017-02-22 05:30:39 -0800148 int key_frames_permille =
asaperssonb99baf82017-04-20 04:05:43 -0700149 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
philipela45102f2017-02-22 05:30:39 -0800150 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesReceivedInPermille",
151 key_frames_permille);
asapersson2077f2f2017-05-11 05:37:35 -0700152 LOG(LS_INFO) << "WebRTC.Video.KeyFramesReceivedInPermille "
153 << key_frames_permille;
philipela45102f2017-02-22 05:30:39 -0800154 }
155
asapersson86b01602015-10-20 23:55:26 -0700156 int qp = qp_counters_.vp8.Avg(kMinRequiredSamples);
asapersson2077f2f2017-05-11 05:37:35 -0700157 if (qp != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700158 RTC_HISTOGRAM_COUNTS_200("WebRTC.Video.Decoded.Vp8.Qp", qp);
asapersson2077f2f2017-05-11 05:37:35 -0700159 LOG(LS_INFO) << "WebRTC.Video.Decoded.Vp8.Qp " << qp;
160 }
asaperssona563c212017-03-02 08:25:46 -0800161 int decode_ms = decode_time_counter_.Avg(kMinRequiredSamples);
asapersson2077f2f2017-05-11 05:37:35 -0700162 if (decode_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700163 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", decode_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700164 LOG(LS_INFO) << "WebRTC.Video.DecodeTimeInMs " << decode_ms;
165 }
asaperssona563c212017-03-02 08:25:46 -0800166 int jb_delay_ms = jitter_buffer_delay_counter_.Avg(kMinRequiredSamples);
philipela45102f2017-02-22 05:30:39 -0800167 if (jb_delay_ms != -1) {
168 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
169 jb_delay_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700170 LOG(LS_INFO) << "WebRTC.Video.JitterBufferDelayInMs " << jb_delay_ms;
asapersson8688a4e2016-04-27 23:42:35 -0700171 }
philipela45102f2017-02-22 05:30:39 -0800172
asaperssona563c212017-03-02 08:25:46 -0800173 int target_delay_ms = target_delay_counter_.Avg(kMinRequiredSamples);
asapersson8688a4e2016-04-27 23:42:35 -0700174 if (target_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700175 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs", target_delay_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700176 LOG(LS_INFO) << "WebRTC.Video.TargetDelayInMs " << target_delay_ms;
asapersson8688a4e2016-04-27 23:42:35 -0700177 }
asaperssona563c212017-03-02 08:25:46 -0800178 int current_delay_ms = current_delay_counter_.Avg(kMinRequiredSamples);
asapersson8688a4e2016-04-27 23:42:35 -0700179 if (current_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700180 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.CurrentDelayInMs",
181 current_delay_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700182 LOG(LS_INFO) << "WebRTC.Video.CurrentDelayInMs " << current_delay_ms;
asapersson8688a4e2016-04-27 23:42:35 -0700183 }
asaperssona563c212017-03-02 08:25:46 -0800184 int delay_ms = delay_counter_.Avg(kMinRequiredSamples);
asapersson13c433c2015-10-06 04:08:15 -0700185 if (delay_ms != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700186 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", delay_ms);
sprang0ab8e812016-02-24 01:35:40 -0800187
ilnik00d802b2017-04-11 10:34:31 -0700188 int e2e_delay_ms_video = e2e_delay_counter_video_.Avg(kMinRequiredSamples);
189 if (e2e_delay_ms_video != -1) {
190 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.EndToEndDelayInMs",
191 e2e_delay_ms_video);
asapersson2077f2f2017-05-11 05:37:35 -0700192 LOG(LS_INFO) << "WebRTC.Video.EndToEndDelayInMs " << e2e_delay_ms_video;
ilnik00d802b2017-04-11 10:34:31 -0700193 }
194
195 int e2e_delay_ms_screenshare =
196 e2e_delay_counter_screenshare_.Avg(kMinRequiredSamples);
197 if (e2e_delay_ms_screenshare != -1) {
198 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.Screenshare.EndToEndDelayInMs",
199 e2e_delay_ms_screenshare);
200 }
201
202 int e2e_delay_max_ms_video = e2e_delay_max_ms_video_;
203 if (e2e_delay_max_ms_video != -1) {
204 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Video.EndToEndDelayMaxInMs",
205 e2e_delay_max_ms_video);
206 }
207
208 int e2e_delay_max_ms_screenshare = e2e_delay_max_ms_screenshare_;
209 if (e2e_delay_max_ms_screenshare != -1) {
210 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Video.Screenshare.EndToEndDelayMaxInMs",
211 e2e_delay_max_ms_screenshare);
212 }
asapersson1490f7a2016-09-23 02:09:46 -0700213
ilnik4257ab22017-07-03 01:15:58 -0700214 int interframe_delay_ms_screenshare =
215 interframe_delay_counter_screenshare_.Avg(kMinRequiredSamples);
216 if (interframe_delay_ms_screenshare != -1) {
217 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.Screenshare.InterframeDelayInMs",
218 interframe_delay_ms_screenshare);
219 }
220
221 int interframe_delay_ms_video =
222 interframe_delay_counter_video_.Avg(kMinRequiredSamples);
223 if (interframe_delay_ms_video != -1) {
224 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.InterframeDelayInMs",
225 interframe_delay_ms_video);
226 }
227
228 int interframe_delay_max_ms_screenshare =
229 interframe_delay_max_ms_screenshare_;
230 if (interframe_delay_max_ms_screenshare != -1) {
231 RTC_HISTOGRAM_COUNTS_10000(
232 "WebRTC.Video.Screenshare.InterframeDelayMaxInMs",
233 interframe_delay_ms_screenshare);
234 }
235
236 int interframe_delay_max_ms_video = interframe_delay_max_ms_video_;
237 if (interframe_delay_max_ms_video != -1) {
238 RTC_HISTOGRAM_COUNTS_10000(
239 "WebRTC.Video.InterframeDelayMaxInMs",
240 interframe_delay_ms_video);
241 }
242
243
sprang0ab8e812016-02-24 01:35:40 -0800244 StreamDataCounters rtp = stats_.rtp_stats;
245 StreamDataCounters rtx;
246 for (auto it : rtx_stats_)
247 rtx.Add(it.second);
248 StreamDataCounters rtp_rtx = rtp;
249 rtp_rtx.Add(rtx);
250 int64_t elapsed_sec =
251 rtp_rtx.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) / 1000;
asapersson2077f2f2017-05-11 05:37:35 -0700252 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
asapersson1d02d3e2016-09-09 22:40:25 -0700253 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800254 "WebRTC.Video.BitrateReceivedInKbps",
255 static_cast<int>(rtp_rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
256 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700257 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800258 "WebRTC.Video.MediaBitrateReceivedInKbps",
259 static_cast<int>(rtp.MediaPayloadBytes() * 8 / elapsed_sec / 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700260 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800261 "WebRTC.Video.PaddingBitrateReceivedInKbps",
262 static_cast<int>(rtp_rtx.transmitted.padding_bytes * 8 / elapsed_sec /
263 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700264 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800265 "WebRTC.Video.RetransmittedBitrateReceivedInKbps",
266 static_cast<int>(rtp_rtx.retransmitted.TotalBytes() * 8 / elapsed_sec /
267 1000));
268 if (!rtx_stats_.empty()) {
asapersson1d02d3e2016-09-09 22:40:25 -0700269 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtxBitrateReceivedInKbps",
270 static_cast<int>(rtx.transmitted.TotalBytes() *
271 8 / elapsed_sec / 1000));
sprang0ab8e812016-02-24 01:35:40 -0800272 }
brandtrb5f2c3f2016-10-04 23:28:39 -0700273 if (config_.rtp.ulpfec.ulpfec_payload_type != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700274 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800275 "WebRTC.Video.FecBitrateReceivedInKbps",
276 static_cast<int>(rtp_rtx.fec.TotalBytes() * 8 / elapsed_sec / 1000));
277 }
sprang07fb9be2016-02-24 07:55:00 -0800278 const RtcpPacketTypeCounter& counters = stats_.rtcp_packet_type_counts;
asapersson1d02d3e2016-09-09 22:40:25 -0700279 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.NackPacketsSentPerMinute",
280 counters.nack_packets * 60 / elapsed_sec);
281 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.FirPacketsSentPerMinute",
282 counters.fir_packets * 60 / elapsed_sec);
283 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.PliPacketsSentPerMinute",
284 counters.pli_packets * 60 / elapsed_sec);
sprang07fb9be2016-02-24 07:55:00 -0800285 if (counters.nack_requests > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700286 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.UniqueNackRequestsSentInPercent",
287 counters.UniqueNackRequestsInPercent());
sprang07fb9be2016-02-24 07:55:00 -0800288 }
sprang0ab8e812016-02-24 01:35:40 -0800289 }
palmkvista40672a2017-01-13 05:58:34 -0800290
291 if (num_certain_states_ >= kBadCallMinRequiredSamples) {
292 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Any",
293 100 * num_bad_states_ / num_certain_states_);
294 }
295 rtc::Optional<double> fps_fraction =
296 fps_threshold_.FractionHigh(kBadCallMinRequiredSamples);
297 if (fps_fraction) {
298 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRate",
299 static_cast<int>(100 * (1 - *fps_fraction)));
300 }
301 rtc::Optional<double> variance_fraction =
302 variance_threshold_.FractionHigh(kBadCallMinRequiredSamples);
303 if (variance_fraction) {
304 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRateVariance",
305 static_cast<int>(100 * *variance_fraction));
306 }
307 rtc::Optional<double> qp_fraction =
308 qp_threshold_.FractionHigh(kBadCallMinRequiredSamples);
309 if (qp_fraction) {
310 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Qp",
311 static_cast<int>(100 * *qp_fraction));
312 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200313}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000314
palmkvist349092b2016-12-13 02:45:57 -0800315void ReceiveStatisticsProxy::QualitySample() {
316 int64_t now = clock_->TimeInMilliseconds();
317 if (last_sample_time_ + kMinSampleLengthMs > now)
318 return;
319
320 double fps =
321 render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_);
322 int qp = qp_sample_.Avg(1);
323
324 bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true);
325 bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false);
326 bool prev_variance_bad = variance_threshold_.IsHigh().value_or(false);
327 bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad;
328
329 fps_threshold_.AddMeasurement(static_cast<int>(fps));
330 if (qp != -1)
331 qp_threshold_.AddMeasurement(qp);
332 rtc::Optional<double> fps_variance_opt = fps_threshold_.CalculateVariance();
333 double fps_variance = fps_variance_opt.value_or(0);
334 if (fps_variance_opt) {
335 variance_threshold_.AddMeasurement(static_cast<int>(fps_variance));
336 }
337
338 bool fps_bad = !fps_threshold_.IsHigh().value_or(true);
339 bool qp_bad = qp_threshold_.IsHigh().value_or(false);
340 bool variance_bad = variance_threshold_.IsHigh().value_or(false);
341 bool any_bad = fps_bad || qp_bad || variance_bad;
342
343 if (!prev_any_bad && any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800344 LOG(LS_INFO) << "Bad call (any) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800345 } else if (prev_any_bad && !any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800346 LOG(LS_INFO) << "Bad call (any) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800347 }
348
349 if (!prev_fps_bad && fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800350 LOG(LS_INFO) << "Bad call (fps) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800351 } else if (prev_fps_bad && !fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800352 LOG(LS_INFO) << "Bad call (fps) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800353 }
354
355 if (!prev_qp_bad && qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800356 LOG(LS_INFO) << "Bad call (qp) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800357 } else if (prev_qp_bad && !qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800358 LOG(LS_INFO) << "Bad call (qp) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800359 }
360
361 if (!prev_variance_bad && variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800362 LOG(LS_INFO) << "Bad call (variance) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800363 } else if (prev_variance_bad && !variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800364 LOG(LS_INFO) << "Bad call (variance) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800365 }
366
palmkvistc7e7e072017-01-09 07:23:33 -0800367 LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_)
368 << " fps: " << fps << " fps_bad: " << fps_bad << " qp: " << qp
369 << " qp_bad: " << qp_bad << " variance_bad: " << variance_bad
370 << " fps_variance: " << fps_variance;
palmkvist349092b2016-12-13 02:45:57 -0800371
372 last_sample_time_ = now;
373 qp_sample_.Reset();
palmkvista40672a2017-01-13 05:58:34 -0800374
375 if (fps_threshold_.IsHigh() || variance_threshold_.IsHigh() ||
376 qp_threshold_.IsHigh()) {
377 if (any_bad)
378 ++num_bad_states_;
379 ++num_certain_states_;
380 }
palmkvist349092b2016-12-13 02:45:57 -0800381}
382
asapersson0255acb2017-03-28 02:44:58 -0700383void ReceiveStatisticsProxy::UpdateFramerate(int64_t now_ms) const {
philipela45102f2017-02-22 05:30:39 -0800384 int64_t old_frames_ms = now_ms - kRateStatisticsWindowSizeMs;
385 while (!frame_window_.empty() &&
386 frame_window_.begin()->first < old_frames_ms) {
philipela45102f2017-02-22 05:30:39 -0800387 frame_window_.erase(frame_window_.begin());
388 }
389
390 size_t framerate =
391 (frame_window_.size() * 1000 + 500) / kRateStatisticsWindowSizeMs;
philipela45102f2017-02-22 05:30:39 -0800392 stats_.network_frame_rate = static_cast<int>(framerate);
philipela45102f2017-02-22 05:30:39 -0800393}
394
sprang@webrtc.org09315702014-02-07 12:06:29 +0000395VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const {
Peter Boströmf2f82832015-05-01 13:00:41 +0200396 rtc::CritScope lock(&crit_);
sprang948b2752017-05-04 02:47:13 -0700397 // Get current frame rates here, as only updating them on new frames prevents
398 // us from ever correctly displaying frame rate of 0.
399 int64_t now_ms = clock_->TimeInMilliseconds();
400 UpdateFramerate(now_ms);
401 stats_.render_frame_rate = renders_fps_estimator_.Rate(now_ms).value_or(0);
402 stats_.decode_frame_rate = decode_fps_estimator_.Rate(now_ms).value_or(0);
asapersson0255acb2017-03-28 02:44:58 -0700403 stats_.total_bitrate_bps =
404 static_cast<int>(total_byte_tracker_.ComputeRate() * 8);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000405 return stats_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000406}
407
ilnik2edc6842017-07-06 03:06:50 -0700408rtc::Optional<TimingFrameInfo>
409ReceiveStatisticsProxy::GetAndResetTimingFrameInfo() {
410 rtc::CritScope lock(&crit_);
411 rtc::Optional<TimingFrameInfo> info = timing_frame_info_;
412 // Reset reported value to empty, so it will be always
413 // overwritten in |OnTimingFrameInfoUpdated|, thus allowing to store new
414 // value instead of reported one.
415 timing_frame_info_.reset();
416 return info;
417}
418
pbosf42376c2015-08-28 07:35:32 -0700419void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) {
420 rtc::CritScope lock(&crit_);
421 stats_.current_payload_type = payload_type;
422}
423
Peter Boströmb7d9a972015-12-18 16:01:11 +0100424void ReceiveStatisticsProxy::OnDecoderImplementationName(
425 const char* implementation_name) {
426 rtc::CritScope lock(&crit_);
427 stats_.decoder_implementation_name = implementation_name;
428}
pbosf42376c2015-08-28 07:35:32 -0700429void ReceiveStatisticsProxy::OnIncomingRate(unsigned int framerate,
430 unsigned int bitrate_bps) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200431 rtc::CritScope lock(&crit_);
palmkvista40672a2017-01-13 05:58:34 -0800432 if (stats_.rtp_stats.first_packet_time_ms != -1)
433 QualitySample();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000434}
435
philipela45102f2017-02-22 05:30:39 -0800436void ReceiveStatisticsProxy::OnFrameBufferTimingsUpdated(
437 int decode_ms,
438 int max_decode_ms,
439 int current_delay_ms,
440 int target_delay_ms,
441 int jitter_buffer_ms,
442 int min_playout_delay_ms,
443 int render_delay_ms) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200444 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000445 stats_.decode_ms = decode_ms;
446 stats_.max_decode_ms = max_decode_ms;
447 stats_.current_delay_ms = current_delay_ms;
448 stats_.target_delay_ms = target_delay_ms;
449 stats_.jitter_buffer_ms = jitter_buffer_ms;
450 stats_.min_playout_delay_ms = min_playout_delay_ms;
451 stats_.render_delay_ms = render_delay_ms;
asapersson6718e972015-07-24 00:20:58 -0700452 decode_time_counter_.Add(decode_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700453 jitter_buffer_delay_counter_.Add(jitter_buffer_ms);
454 target_delay_counter_.Add(target_delay_ms);
455 current_delay_counter_.Add(current_delay_ms);
asaperssona1862882016-04-18 00:41:05 -0700456 // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time +
457 // render delay).
philipela45102f2017-02-22 05:30:39 -0800458 delay_counter_.Add(target_delay_ms + avg_rtt_ms_ / 2);
pbos@webrtc.org98c04b32014-12-18 13:12:52 +0000459}
460
ilnik2edc6842017-07-06 03:06:50 -0700461void ReceiveStatisticsProxy::OnTimingFrameInfoUpdated(
462 const TimingFrameInfo& info) {
463 rtc::CritScope lock(&crit_);
464 // Only the frame which was processed the longest since the last
465 // GetStats() call is reported. Therefore, only single 'longest' frame is
466 // stored.
467 if (!timing_frame_info_ || info.IsLongerThan(*timing_frame_info_)) {
468 timing_frame_info_.emplace(info);
469 }
470}
471
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000472void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated(
473 uint32_t ssrc,
474 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200475 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000476 if (stats_.ssrc != ssrc)
477 return;
478 stats_.rtcp_packet_type_counts = packet_counter;
479}
480
sprang@webrtc.org09315702014-02-07 12:06:29 +0000481void ReceiveStatisticsProxy::StatisticsUpdated(
482 const webrtc::RtcpStatistics& statistics,
483 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200484 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700485 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000486 // receive stats from one of them.
487 if (stats_.ssrc != ssrc)
488 return;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000489 stats_.rtcp_stats = statistics;
Åsa Persson3c391cb2015-04-27 10:09:49 +0200490 report_block_stats_.Store(statistics, ssrc, 0);
asapersson0c43f772016-11-30 01:42:26 -0800491
492 if (first_report_block_time_ms_ == -1)
493 first_report_block_time_ms_ = clock_->TimeInMilliseconds();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000494}
495
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000496void ReceiveStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200497 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700498 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000499 // receive stats from one of them.
500 if (stats_.ssrc != ssrc)
501 return;
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000502 stats_.c_name = cname;
503}
504
sprang@webrtc.org09315702014-02-07 12:06:29 +0000505void ReceiveStatisticsProxy::DataCountersUpdated(
506 const webrtc::StreamDataCounters& counters,
507 uint32_t ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700508 size_t last_total_bytes = 0;
509 size_t total_bytes = 0;
Peter Boströmf2f82832015-05-01 13:00:41 +0200510 rtc::CritScope lock(&crit_);
sprang0ab8e812016-02-24 01:35:40 -0800511 if (ssrc == stats_.ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700512 last_total_bytes = stats_.rtp_stats.transmitted.TotalBytes();
513 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800514 stats_.rtp_stats = counters;
515 } else {
516 auto it = rtx_stats_.find(ssrc);
517 if (it != rtx_stats_.end()) {
asapersson0255acb2017-03-28 02:44:58 -0700518 last_total_bytes = it->second.transmitted.TotalBytes();
519 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800520 it->second = counters;
521 } else {
522 RTC_NOTREACHED() << "Unexpected stream ssrc: " << ssrc;
523 }
524 }
asapersson0255acb2017-03-28 02:44:58 -0700525 if (total_bytes > last_total_bytes)
526 total_byte_tracker_.AddSamples(total_bytes - last_total_bytes);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000527}
528
ilnik00d802b2017-04-11 10:34:31 -0700529void ReceiveStatisticsProxy::OnDecodedFrame(rtc::Optional<uint8_t> qp,
530 VideoContentType content_type) {
sprang@webrtc.org09315702014-02-07 12:06:29 +0000531 uint64_t now = clock_->TimeInMilliseconds();
532
Peter Boströmf2f82832015-05-01 13:00:41 +0200533 rtc::CritScope lock(&crit_);
sakale5ba44e2016-10-26 07:09:24 -0700534 ++stats_.frames_decoded;
sakalcc452e12017-02-09 04:53:45 -0800535 if (qp) {
536 if (!stats_.qp_sum) {
537 if (stats_.frames_decoded != 1) {
538 LOG(LS_WARNING)
539 << "Frames decoded was not 1 when first qp value was received.";
540 stats_.frames_decoded = 1;
541 }
542 stats_.qp_sum = rtc::Optional<uint64_t>(0);
543 }
544 *stats_.qp_sum += *qp;
545 } else if (stats_.qp_sum) {
546 LOG(LS_WARNING)
547 << "QP sum was already set and no QP was given for a frame.";
548 stats_.qp_sum = rtc::Optional<uint64_t>();
549 }
ilnik00d802b2017-04-11 10:34:31 -0700550 last_content_type_ = content_type;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000551 decode_fps_estimator_.Update(1, now);
ilnik4257ab22017-07-03 01:15:58 -0700552 if (last_decoded_frame_time_ms_) {
553 int64_t interframe_delay_ms = now - *last_decoded_frame_time_ms_;
554 RTC_DCHECK_GE(interframe_delay_ms, 0);
ilnikf04afde2017-07-07 01:26:24 -0700555 stats_.interframe_delay_sum_ms += interframe_delay_ms;
ilnik4257ab22017-07-03 01:15:58 -0700556 if (last_content_type_ == VideoContentType::SCREENSHARE) {
557 interframe_delay_counter_screenshare_.Add(interframe_delay_ms);
558 if (interframe_delay_max_ms_screenshare_ < interframe_delay_ms) {
559 interframe_delay_max_ms_screenshare_ = interframe_delay_ms;
560 }
561 } else {
562 interframe_delay_counter_video_.Add(interframe_delay_ms);
563 if (interframe_delay_max_ms_video_ < interframe_delay_ms) {
564 interframe_delay_max_ms_video_ = interframe_delay_ms;
565 }
566 }
567 }
568 last_decoded_frame_time_ms_.emplace(now);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000569}
570
asapersson1490f7a2016-09-23 02:09:46 -0700571void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) {
572 int width = frame.width();
573 int height = frame.height();
asaperssonf839dcc2015-10-08 00:41:59 -0700574 RTC_DCHECK_GT(width, 0);
575 RTC_DCHECK_GT(height, 0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000576 uint64_t now = clock_->TimeInMilliseconds();
577
Peter Boströmf2f82832015-05-01 13:00:41 +0200578 rtc::CritScope lock(&crit_);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000579 renders_fps_estimator_.Update(1, now);
hbos50cfe1f2017-01-23 07:21:55 -0800580 ++stats_.frames_rendered;
asapersson2e5cfcd2016-08-11 08:41:18 -0700581 stats_.width = width;
582 stats_.height = height;
asaperssond89920b2015-07-22 06:52:00 -0700583 render_width_counter_.Add(width);
584 render_height_counter_.Add(height);
Tim Psiaki63046262015-09-14 10:38:08 -0700585 render_fps_tracker_.AddSamples(1);
asaperssonf839dcc2015-10-08 00:41:59 -0700586 render_pixel_tracker_.AddSamples(sqrt(width * height));
asapersson1490f7a2016-09-23 02:09:46 -0700587
588 if (frame.ntp_time_ms() > 0) {
589 int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms();
ilnik00d802b2017-04-11 10:34:31 -0700590 if (delay_ms >= 0) {
591 if (last_content_type_ == VideoContentType::SCREENSHARE) {
592 e2e_delay_max_ms_screenshare_ =
593 std::max(delay_ms, e2e_delay_max_ms_screenshare_);
594 e2e_delay_counter_screenshare_.Add(delay_ms);
595 } else {
596 e2e_delay_max_ms_video_ = std::max(delay_ms, e2e_delay_max_ms_video_);
597 e2e_delay_counter_video_.Add(delay_ms);
598 }
599 }
asapersson1490f7a2016-09-23 02:09:46 -0700600 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000601}
602
asaperssonde9e5ff2016-11-02 07:14:03 -0700603void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t sync_offset_ms,
604 double estimated_freq_khz) {
asaperssonf8cdd182016-03-15 01:00:47 -0700605 rtc::CritScope lock(&crit_);
606 sync_offset_counter_.Add(std::abs(sync_offset_ms));
607 stats_.sync_offset_ms = sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700608
609 const double kMaxFreqKhz = 10000.0;
610 int offset_khz = kMaxFreqKhz;
611 // Should not be zero or negative. If so, report max.
612 if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0)
613 offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5);
614
615 freq_offset_counter_.Add(offset_khz);
asaperssonf8cdd182016-03-15 01:00:47 -0700616}
617
pbos@webrtc.org55707692014-12-19 15:45:03 +0000618void ReceiveStatisticsProxy::OnReceiveRatesUpdated(uint32_t bitRate,
619 uint32_t frameRate) {
620}
621
philipela45102f2017-02-22 05:30:39 -0800622void ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe,
623 size_t size_bytes) {
624 rtc::CritScope lock(&crit_);
625 if (is_keyframe)
626 ++stats_.frame_counts.key_frames;
627 else
628 ++stats_.frame_counts.delta_frames;
629
630 int64_t now_ms = clock_->TimeInMilliseconds();
philipela45102f2017-02-22 05:30:39 -0800631 frame_window_.insert(std::make_pair(now_ms, size_bytes));
asapersson0255acb2017-03-28 02:44:58 -0700632 UpdateFramerate(now_ms);
philipela45102f2017-02-22 05:30:39 -0800633}
634
pbos@webrtc.org55707692014-12-19 15:45:03 +0000635void ReceiveStatisticsProxy::OnFrameCountsUpdated(
636 const FrameCounts& frame_counts) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200637 rtc::CritScope lock(&crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000638 stats_.frame_counts = frame_counts;
639}
640
pbos@webrtc.org55707692014-12-19 15:45:03 +0000641void ReceiveStatisticsProxy::OnDiscardedPacketsUpdated(int discarded_packets) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200642 rtc::CritScope lock(&crit_);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000643 stats_.discarded_packets = discarded_packets;
644}
645
asapersson86b01602015-10-20 23:55:26 -0700646void ReceiveStatisticsProxy::OnPreDecode(
647 const EncodedImage& encoded_image,
648 const CodecSpecificInfo* codec_specific_info) {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200649 if (!codec_specific_info || encoded_image.qp_ == -1) {
asapersson86b01602015-10-20 23:55:26 -0700650 return;
651 }
652 if (codec_specific_info->codecType == kVideoCodecVP8) {
653 qp_counters_.vp8.Add(encoded_image.qp_);
palmkvist349092b2016-12-13 02:45:57 -0800654 rtc::CritScope lock(&crit_);
655 qp_sample_.Add(encoded_image.qp_);
asapersson86b01602015-10-20 23:55:26 -0700656 }
657}
658
asaperssond89920b2015-07-22 06:52:00 -0700659void ReceiveStatisticsProxy::SampleCounter::Add(int sample) {
660 sum += sample;
661 ++num_samples;
662}
663
asapersson6966bd52017-01-03 00:44:06 -0800664int ReceiveStatisticsProxy::SampleCounter::Avg(
665 int64_t min_required_samples) const {
asaperssond89920b2015-07-22 06:52:00 -0700666 if (num_samples < min_required_samples || num_samples == 0)
667 return -1;
asapersson6966bd52017-01-03 00:44:06 -0800668 return static_cast<int>(sum / num_samples);
asaperssond89920b2015-07-22 06:52:00 -0700669}
670
palmkvist349092b2016-12-13 02:45:57 -0800671void ReceiveStatisticsProxy::SampleCounter::Reset() {
672 num_samples = 0;
673 sum = 0;
674}
675
philipela45102f2017-02-22 05:30:39 -0800676void ReceiveStatisticsProxy::OnRttUpdate(int64_t avg_rtt_ms,
677 int64_t max_rtt_ms) {
678 rtc::CritScope lock(&crit_);
679 avg_rtt_ms_ = avg_rtt_ms;
680}
681
sprang@webrtc.org09315702014-02-07 12:06:29 +0000682} // namespace webrtc