blob: 7852a059134c47e17a198612385afd0ed35d2fa2 [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);
sprang892dab52017-08-15 05:00:33 -0700219 RTC_DCHECK_GE(interframe_delay_max_ms_screenshare_,
220 interframe_delay_ms_screenshare);
221 RTC_HISTOGRAM_COUNTS_10000(
222 "WebRTC.Video.Screenshare.InterframeDelayMaxInMs",
223 interframe_delay_max_ms_screenshare_);
ilnik4257ab22017-07-03 01:15:58 -0700224 }
225
226 int interframe_delay_ms_video =
227 interframe_delay_counter_video_.Avg(kMinRequiredSamples);
228 if (interframe_delay_ms_video != -1) {
229 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.InterframeDelayInMs",
230 interframe_delay_ms_video);
sprang892dab52017-08-15 05:00:33 -0700231 RTC_DCHECK_GE(interframe_delay_max_ms_video_, interframe_delay_ms_video);
232 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.InterframeDelayMaxInMs",
233 interframe_delay_max_ms_video_);
ilnik4257ab22017-07-03 01:15:58 -0700234 }
235
sprang0ab8e812016-02-24 01:35:40 -0800236 StreamDataCounters rtp = stats_.rtp_stats;
237 StreamDataCounters rtx;
238 for (auto it : rtx_stats_)
239 rtx.Add(it.second);
240 StreamDataCounters rtp_rtx = rtp;
241 rtp_rtx.Add(rtx);
242 int64_t elapsed_sec =
243 rtp_rtx.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) / 1000;
asapersson2077f2f2017-05-11 05:37:35 -0700244 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
asapersson1d02d3e2016-09-09 22:40:25 -0700245 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800246 "WebRTC.Video.BitrateReceivedInKbps",
247 static_cast<int>(rtp_rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
248 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700249 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800250 "WebRTC.Video.MediaBitrateReceivedInKbps",
251 static_cast<int>(rtp.MediaPayloadBytes() * 8 / elapsed_sec / 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700252 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800253 "WebRTC.Video.PaddingBitrateReceivedInKbps",
254 static_cast<int>(rtp_rtx.transmitted.padding_bytes * 8 / elapsed_sec /
255 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700256 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800257 "WebRTC.Video.RetransmittedBitrateReceivedInKbps",
258 static_cast<int>(rtp_rtx.retransmitted.TotalBytes() * 8 / elapsed_sec /
259 1000));
260 if (!rtx_stats_.empty()) {
asapersson1d02d3e2016-09-09 22:40:25 -0700261 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtxBitrateReceivedInKbps",
262 static_cast<int>(rtx.transmitted.TotalBytes() *
263 8 / elapsed_sec / 1000));
sprang0ab8e812016-02-24 01:35:40 -0800264 }
brandtrb5f2c3f2016-10-04 23:28:39 -0700265 if (config_.rtp.ulpfec.ulpfec_payload_type != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700266 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800267 "WebRTC.Video.FecBitrateReceivedInKbps",
268 static_cast<int>(rtp_rtx.fec.TotalBytes() * 8 / elapsed_sec / 1000));
269 }
sprang07fb9be2016-02-24 07:55:00 -0800270 const RtcpPacketTypeCounter& counters = stats_.rtcp_packet_type_counts;
asapersson1d02d3e2016-09-09 22:40:25 -0700271 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.NackPacketsSentPerMinute",
272 counters.nack_packets * 60 / elapsed_sec);
273 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.FirPacketsSentPerMinute",
274 counters.fir_packets * 60 / elapsed_sec);
275 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.PliPacketsSentPerMinute",
276 counters.pli_packets * 60 / elapsed_sec);
sprang07fb9be2016-02-24 07:55:00 -0800277 if (counters.nack_requests > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700278 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.UniqueNackRequestsSentInPercent",
279 counters.UniqueNackRequestsInPercent());
sprang07fb9be2016-02-24 07:55:00 -0800280 }
sprang0ab8e812016-02-24 01:35:40 -0800281 }
palmkvista40672a2017-01-13 05:58:34 -0800282
283 if (num_certain_states_ >= kBadCallMinRequiredSamples) {
284 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Any",
285 100 * num_bad_states_ / num_certain_states_);
286 }
287 rtc::Optional<double> fps_fraction =
288 fps_threshold_.FractionHigh(kBadCallMinRequiredSamples);
289 if (fps_fraction) {
290 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRate",
291 static_cast<int>(100 * (1 - *fps_fraction)));
292 }
293 rtc::Optional<double> variance_fraction =
294 variance_threshold_.FractionHigh(kBadCallMinRequiredSamples);
295 if (variance_fraction) {
296 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRateVariance",
297 static_cast<int>(100 * *variance_fraction));
298 }
299 rtc::Optional<double> qp_fraction =
300 qp_threshold_.FractionHigh(kBadCallMinRequiredSamples);
301 if (qp_fraction) {
302 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Qp",
303 static_cast<int>(100 * *qp_fraction));
304 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200305}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000306
palmkvist349092b2016-12-13 02:45:57 -0800307void ReceiveStatisticsProxy::QualitySample() {
308 int64_t now = clock_->TimeInMilliseconds();
309 if (last_sample_time_ + kMinSampleLengthMs > now)
310 return;
311
312 double fps =
313 render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_);
314 int qp = qp_sample_.Avg(1);
315
316 bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true);
317 bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false);
318 bool prev_variance_bad = variance_threshold_.IsHigh().value_or(false);
319 bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad;
320
321 fps_threshold_.AddMeasurement(static_cast<int>(fps));
322 if (qp != -1)
323 qp_threshold_.AddMeasurement(qp);
324 rtc::Optional<double> fps_variance_opt = fps_threshold_.CalculateVariance();
325 double fps_variance = fps_variance_opt.value_or(0);
326 if (fps_variance_opt) {
327 variance_threshold_.AddMeasurement(static_cast<int>(fps_variance));
328 }
329
330 bool fps_bad = !fps_threshold_.IsHigh().value_or(true);
331 bool qp_bad = qp_threshold_.IsHigh().value_or(false);
332 bool variance_bad = variance_threshold_.IsHigh().value_or(false);
333 bool any_bad = fps_bad || qp_bad || variance_bad;
334
335 if (!prev_any_bad && any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800336 LOG(LS_INFO) << "Bad call (any) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800337 } else if (prev_any_bad && !any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800338 LOG(LS_INFO) << "Bad call (any) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800339 }
340
341 if (!prev_fps_bad && fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800342 LOG(LS_INFO) << "Bad call (fps) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800343 } else if (prev_fps_bad && !fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800344 LOG(LS_INFO) << "Bad call (fps) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800345 }
346
347 if (!prev_qp_bad && qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800348 LOG(LS_INFO) << "Bad call (qp) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800349 } else if (prev_qp_bad && !qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800350 LOG(LS_INFO) << "Bad call (qp) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800351 }
352
353 if (!prev_variance_bad && variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800354 LOG(LS_INFO) << "Bad call (variance) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800355 } else if (prev_variance_bad && !variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800356 LOG(LS_INFO) << "Bad call (variance) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800357 }
358
palmkvistc7e7e072017-01-09 07:23:33 -0800359 LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_)
360 << " fps: " << fps << " fps_bad: " << fps_bad << " qp: " << qp
361 << " qp_bad: " << qp_bad << " variance_bad: " << variance_bad
362 << " fps_variance: " << fps_variance;
palmkvist349092b2016-12-13 02:45:57 -0800363
364 last_sample_time_ = now;
365 qp_sample_.Reset();
palmkvista40672a2017-01-13 05:58:34 -0800366
367 if (fps_threshold_.IsHigh() || variance_threshold_.IsHigh() ||
368 qp_threshold_.IsHigh()) {
369 if (any_bad)
370 ++num_bad_states_;
371 ++num_certain_states_;
372 }
palmkvist349092b2016-12-13 02:45:57 -0800373}
374
asapersson0255acb2017-03-28 02:44:58 -0700375void ReceiveStatisticsProxy::UpdateFramerate(int64_t now_ms) const {
philipela45102f2017-02-22 05:30:39 -0800376 int64_t old_frames_ms = now_ms - kRateStatisticsWindowSizeMs;
377 while (!frame_window_.empty() &&
378 frame_window_.begin()->first < old_frames_ms) {
philipela45102f2017-02-22 05:30:39 -0800379 frame_window_.erase(frame_window_.begin());
380 }
381
382 size_t framerate =
383 (frame_window_.size() * 1000 + 500) / kRateStatisticsWindowSizeMs;
philipela45102f2017-02-22 05:30:39 -0800384 stats_.network_frame_rate = static_cast<int>(framerate);
philipela45102f2017-02-22 05:30:39 -0800385}
386
sprang@webrtc.org09315702014-02-07 12:06:29 +0000387VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const {
Peter Boströmf2f82832015-05-01 13:00:41 +0200388 rtc::CritScope lock(&crit_);
sprang948b2752017-05-04 02:47:13 -0700389 // Get current frame rates here, as only updating them on new frames prevents
390 // us from ever correctly displaying frame rate of 0.
391 int64_t now_ms = clock_->TimeInMilliseconds();
392 UpdateFramerate(now_ms);
393 stats_.render_frame_rate = renders_fps_estimator_.Rate(now_ms).value_or(0);
394 stats_.decode_frame_rate = decode_fps_estimator_.Rate(now_ms).value_or(0);
asapersson0255acb2017-03-28 02:44:58 -0700395 stats_.total_bitrate_bps =
396 static_cast<int>(total_byte_tracker_.ComputeRate() * 8);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000397 return stats_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000398}
399
ilnik2edc6842017-07-06 03:06:50 -0700400rtc::Optional<TimingFrameInfo>
401ReceiveStatisticsProxy::GetAndResetTimingFrameInfo() {
402 rtc::CritScope lock(&crit_);
403 rtc::Optional<TimingFrameInfo> info = timing_frame_info_;
404 // Reset reported value to empty, so it will be always
405 // overwritten in |OnTimingFrameInfoUpdated|, thus allowing to store new
406 // value instead of reported one.
407 timing_frame_info_.reset();
408 return info;
409}
410
pbosf42376c2015-08-28 07:35:32 -0700411void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) {
412 rtc::CritScope lock(&crit_);
413 stats_.current_payload_type = payload_type;
414}
415
Peter Boströmb7d9a972015-12-18 16:01:11 +0100416void ReceiveStatisticsProxy::OnDecoderImplementationName(
417 const char* implementation_name) {
418 rtc::CritScope lock(&crit_);
419 stats_.decoder_implementation_name = implementation_name;
420}
pbosf42376c2015-08-28 07:35:32 -0700421void ReceiveStatisticsProxy::OnIncomingRate(unsigned int framerate,
422 unsigned int bitrate_bps) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200423 rtc::CritScope lock(&crit_);
palmkvista40672a2017-01-13 05:58:34 -0800424 if (stats_.rtp_stats.first_packet_time_ms != -1)
425 QualitySample();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000426}
427
philipela45102f2017-02-22 05:30:39 -0800428void ReceiveStatisticsProxy::OnFrameBufferTimingsUpdated(
429 int decode_ms,
430 int max_decode_ms,
431 int current_delay_ms,
432 int target_delay_ms,
433 int jitter_buffer_ms,
434 int min_playout_delay_ms,
435 int render_delay_ms) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200436 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000437 stats_.decode_ms = decode_ms;
438 stats_.max_decode_ms = max_decode_ms;
439 stats_.current_delay_ms = current_delay_ms;
440 stats_.target_delay_ms = target_delay_ms;
441 stats_.jitter_buffer_ms = jitter_buffer_ms;
442 stats_.min_playout_delay_ms = min_playout_delay_ms;
443 stats_.render_delay_ms = render_delay_ms;
asapersson6718e972015-07-24 00:20:58 -0700444 decode_time_counter_.Add(decode_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700445 jitter_buffer_delay_counter_.Add(jitter_buffer_ms);
446 target_delay_counter_.Add(target_delay_ms);
447 current_delay_counter_.Add(current_delay_ms);
asaperssona1862882016-04-18 00:41:05 -0700448 // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time +
449 // render delay).
philipela45102f2017-02-22 05:30:39 -0800450 delay_counter_.Add(target_delay_ms + avg_rtt_ms_ / 2);
pbos@webrtc.org98c04b32014-12-18 13:12:52 +0000451}
452
ilnik2edc6842017-07-06 03:06:50 -0700453void ReceiveStatisticsProxy::OnTimingFrameInfoUpdated(
454 const TimingFrameInfo& info) {
455 rtc::CritScope lock(&crit_);
456 // Only the frame which was processed the longest since the last
457 // GetStats() call is reported. Therefore, only single 'longest' frame is
458 // stored.
459 if (!timing_frame_info_ || info.IsLongerThan(*timing_frame_info_)) {
460 timing_frame_info_.emplace(info);
461 }
462}
463
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000464void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated(
465 uint32_t ssrc,
466 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200467 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000468 if (stats_.ssrc != ssrc)
469 return;
470 stats_.rtcp_packet_type_counts = packet_counter;
471}
472
sprang@webrtc.org09315702014-02-07 12:06:29 +0000473void ReceiveStatisticsProxy::StatisticsUpdated(
474 const webrtc::RtcpStatistics& statistics,
475 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200476 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700477 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000478 // receive stats from one of them.
479 if (stats_.ssrc != ssrc)
480 return;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000481 stats_.rtcp_stats = statistics;
Åsa Persson3c391cb2015-04-27 10:09:49 +0200482 report_block_stats_.Store(statistics, ssrc, 0);
asapersson0c43f772016-11-30 01:42:26 -0800483
484 if (first_report_block_time_ms_ == -1)
485 first_report_block_time_ms_ = clock_->TimeInMilliseconds();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000486}
487
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000488void ReceiveStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200489 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700490 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000491 // receive stats from one of them.
492 if (stats_.ssrc != ssrc)
493 return;
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000494 stats_.c_name = cname;
495}
496
sprang@webrtc.org09315702014-02-07 12:06:29 +0000497void ReceiveStatisticsProxy::DataCountersUpdated(
498 const webrtc::StreamDataCounters& counters,
499 uint32_t ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700500 size_t last_total_bytes = 0;
501 size_t total_bytes = 0;
Peter Boströmf2f82832015-05-01 13:00:41 +0200502 rtc::CritScope lock(&crit_);
sprang0ab8e812016-02-24 01:35:40 -0800503 if (ssrc == stats_.ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700504 last_total_bytes = stats_.rtp_stats.transmitted.TotalBytes();
505 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800506 stats_.rtp_stats = counters;
507 } else {
508 auto it = rtx_stats_.find(ssrc);
509 if (it != rtx_stats_.end()) {
asapersson0255acb2017-03-28 02:44:58 -0700510 last_total_bytes = it->second.transmitted.TotalBytes();
511 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800512 it->second = counters;
513 } else {
514 RTC_NOTREACHED() << "Unexpected stream ssrc: " << ssrc;
515 }
516 }
asapersson0255acb2017-03-28 02:44:58 -0700517 if (total_bytes > last_total_bytes)
518 total_byte_tracker_.AddSamples(total_bytes - last_total_bytes);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000519}
520
ilnik00d802b2017-04-11 10:34:31 -0700521void ReceiveStatisticsProxy::OnDecodedFrame(rtc::Optional<uint8_t> qp,
522 VideoContentType content_type) {
sprang@webrtc.org09315702014-02-07 12:06:29 +0000523 uint64_t now = clock_->TimeInMilliseconds();
524
Peter Boströmf2f82832015-05-01 13:00:41 +0200525 rtc::CritScope lock(&crit_);
sakale5ba44e2016-10-26 07:09:24 -0700526 ++stats_.frames_decoded;
sakalcc452e12017-02-09 04:53:45 -0800527 if (qp) {
528 if (!stats_.qp_sum) {
529 if (stats_.frames_decoded != 1) {
530 LOG(LS_WARNING)
531 << "Frames decoded was not 1 when first qp value was received.";
532 stats_.frames_decoded = 1;
533 }
534 stats_.qp_sum = rtc::Optional<uint64_t>(0);
535 }
536 *stats_.qp_sum += *qp;
537 } else if (stats_.qp_sum) {
538 LOG(LS_WARNING)
539 << "QP sum was already set and no QP was given for a frame.";
540 stats_.qp_sum = rtc::Optional<uint64_t>();
541 }
ilnik00d802b2017-04-11 10:34:31 -0700542 last_content_type_ = content_type;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000543 decode_fps_estimator_.Update(1, now);
ilnik4257ab22017-07-03 01:15:58 -0700544 if (last_decoded_frame_time_ms_) {
545 int64_t interframe_delay_ms = now - *last_decoded_frame_time_ms_;
546 RTC_DCHECK_GE(interframe_delay_ms, 0);
ilnikf04afde2017-07-07 01:26:24 -0700547 stats_.interframe_delay_sum_ms += interframe_delay_ms;
ilnik4257ab22017-07-03 01:15:58 -0700548 if (last_content_type_ == VideoContentType::SCREENSHARE) {
549 interframe_delay_counter_screenshare_.Add(interframe_delay_ms);
550 if (interframe_delay_max_ms_screenshare_ < interframe_delay_ms) {
551 interframe_delay_max_ms_screenshare_ = interframe_delay_ms;
552 }
553 } else {
554 interframe_delay_counter_video_.Add(interframe_delay_ms);
555 if (interframe_delay_max_ms_video_ < interframe_delay_ms) {
556 interframe_delay_max_ms_video_ = interframe_delay_ms;
557 }
558 }
559 }
560 last_decoded_frame_time_ms_.emplace(now);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000561}
562
asapersson1490f7a2016-09-23 02:09:46 -0700563void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) {
564 int width = frame.width();
565 int height = frame.height();
asaperssonf839dcc2015-10-08 00:41:59 -0700566 RTC_DCHECK_GT(width, 0);
567 RTC_DCHECK_GT(height, 0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000568 uint64_t now = clock_->TimeInMilliseconds();
569
Peter Boströmf2f82832015-05-01 13:00:41 +0200570 rtc::CritScope lock(&crit_);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000571 renders_fps_estimator_.Update(1, now);
hbos50cfe1f2017-01-23 07:21:55 -0800572 ++stats_.frames_rendered;
asapersson2e5cfcd2016-08-11 08:41:18 -0700573 stats_.width = width;
574 stats_.height = height;
asaperssond89920b2015-07-22 06:52:00 -0700575 render_width_counter_.Add(width);
576 render_height_counter_.Add(height);
Tim Psiaki63046262015-09-14 10:38:08 -0700577 render_fps_tracker_.AddSamples(1);
asaperssonf839dcc2015-10-08 00:41:59 -0700578 render_pixel_tracker_.AddSamples(sqrt(width * height));
asapersson1490f7a2016-09-23 02:09:46 -0700579
580 if (frame.ntp_time_ms() > 0) {
581 int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms();
ilnik00d802b2017-04-11 10:34:31 -0700582 if (delay_ms >= 0) {
583 if (last_content_type_ == VideoContentType::SCREENSHARE) {
584 e2e_delay_max_ms_screenshare_ =
585 std::max(delay_ms, e2e_delay_max_ms_screenshare_);
586 e2e_delay_counter_screenshare_.Add(delay_ms);
587 } else {
588 e2e_delay_max_ms_video_ = std::max(delay_ms, e2e_delay_max_ms_video_);
589 e2e_delay_counter_video_.Add(delay_ms);
590 }
591 }
asapersson1490f7a2016-09-23 02:09:46 -0700592 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000593}
594
asaperssonde9e5ff2016-11-02 07:14:03 -0700595void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t sync_offset_ms,
596 double estimated_freq_khz) {
asaperssonf8cdd182016-03-15 01:00:47 -0700597 rtc::CritScope lock(&crit_);
598 sync_offset_counter_.Add(std::abs(sync_offset_ms));
599 stats_.sync_offset_ms = sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700600
601 const double kMaxFreqKhz = 10000.0;
602 int offset_khz = kMaxFreqKhz;
603 // Should not be zero or negative. If so, report max.
604 if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0)
605 offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5);
606
607 freq_offset_counter_.Add(offset_khz);
asaperssonf8cdd182016-03-15 01:00:47 -0700608}
609
pbos@webrtc.org55707692014-12-19 15:45:03 +0000610void ReceiveStatisticsProxy::OnReceiveRatesUpdated(uint32_t bitRate,
611 uint32_t frameRate) {
612}
613
philipela45102f2017-02-22 05:30:39 -0800614void ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe,
615 size_t size_bytes) {
616 rtc::CritScope lock(&crit_);
617 if (is_keyframe)
618 ++stats_.frame_counts.key_frames;
619 else
620 ++stats_.frame_counts.delta_frames;
621
622 int64_t now_ms = clock_->TimeInMilliseconds();
philipela45102f2017-02-22 05:30:39 -0800623 frame_window_.insert(std::make_pair(now_ms, size_bytes));
asapersson0255acb2017-03-28 02:44:58 -0700624 UpdateFramerate(now_ms);
philipela45102f2017-02-22 05:30:39 -0800625}
626
pbos@webrtc.org55707692014-12-19 15:45:03 +0000627void ReceiveStatisticsProxy::OnFrameCountsUpdated(
628 const FrameCounts& frame_counts) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200629 rtc::CritScope lock(&crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000630 stats_.frame_counts = frame_counts;
631}
632
pbos@webrtc.org55707692014-12-19 15:45:03 +0000633void ReceiveStatisticsProxy::OnDiscardedPacketsUpdated(int discarded_packets) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200634 rtc::CritScope lock(&crit_);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000635 stats_.discarded_packets = discarded_packets;
636}
637
asapersson86b01602015-10-20 23:55:26 -0700638void ReceiveStatisticsProxy::OnPreDecode(
639 const EncodedImage& encoded_image,
640 const CodecSpecificInfo* codec_specific_info) {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200641 if (!codec_specific_info || encoded_image.qp_ == -1) {
asapersson86b01602015-10-20 23:55:26 -0700642 return;
643 }
644 if (codec_specific_info->codecType == kVideoCodecVP8) {
645 qp_counters_.vp8.Add(encoded_image.qp_);
palmkvist349092b2016-12-13 02:45:57 -0800646 rtc::CritScope lock(&crit_);
647 qp_sample_.Add(encoded_image.qp_);
asapersson86b01602015-10-20 23:55:26 -0700648 }
649}
650
asaperssond89920b2015-07-22 06:52:00 -0700651void ReceiveStatisticsProxy::SampleCounter::Add(int sample) {
652 sum += sample;
653 ++num_samples;
654}
655
asapersson6966bd52017-01-03 00:44:06 -0800656int ReceiveStatisticsProxy::SampleCounter::Avg(
657 int64_t min_required_samples) const {
asaperssond89920b2015-07-22 06:52:00 -0700658 if (num_samples < min_required_samples || num_samples == 0)
659 return -1;
asapersson6966bd52017-01-03 00:44:06 -0800660 return static_cast<int>(sum / num_samples);
asaperssond89920b2015-07-22 06:52:00 -0700661}
662
palmkvist349092b2016-12-13 02:45:57 -0800663void ReceiveStatisticsProxy::SampleCounter::Reset() {
664 num_samples = 0;
665 sum = 0;
666}
667
philipela45102f2017-02-22 05:30:39 -0800668void ReceiveStatisticsProxy::OnRttUpdate(int64_t avg_rtt_ms,
669 int64_t max_rtt_ms) {
670 rtc::CritScope lock(&crit_);
671 avg_rtt_ms_ = avg_rtt_ms;
672}
673
sprang@webrtc.org09315702014-02-07 12:06:29 +0000674} // namespace webrtc