blob: 4f231694aa1b5efd7c7daa82f497858fd7153924 [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
ilnika79cc282017-08-23 05:24:10 -070046// Some metrics are reported as a maximum over this period.
47const int kMovingMaxWindowMs = 10000;
48
philipela45102f2017-02-22 05:30:39 -080049// How large window we use to calculate the framerate/bitrate.
50const int kRateStatisticsWindowSizeMs = 1000;
asaperssonde9e5ff2016-11-02 07:14:03 -070051} // namespace
sprang@webrtc.org09315702014-02-07 12:06:29 +000052
sprang0ab8e812016-02-24 01:35:40 -080053ReceiveStatisticsProxy::ReceiveStatisticsProxy(
Tommi733b5472016-06-10 17:58:01 +020054 const VideoReceiveStream::Config* config,
sprang0ab8e812016-02-24 01:35:40 -080055 Clock* clock)
pbos@webrtc.org55707692014-12-19 15:45:03 +000056 : clock_(clock),
Tommi733b5472016-06-10 17:58:01 +020057 config_(*config),
asapersson4374a092016-07-27 00:39:09 -070058 start_ms_(clock->TimeInMilliseconds()),
palmkvist349092b2016-12-13 02:45:57 -080059 last_sample_time_(clock->TimeInMilliseconds()),
60 fps_threshold_(kLowFpsThreshold,
61 kHighFpsThreshold,
62 kBadFraction,
63 kNumMeasurements),
64 qp_threshold_(kLowQpThresholdVp8,
65 kHighQpThresholdVp8,
66 kBadFraction,
67 kNumMeasurements),
68 variance_threshold_(kLowVarianceThreshold,
69 kHighVarianceThreshold,
70 kBadFraction,
71 kNumMeasurementsVariance),
palmkvista40672a2017-01-13 05:58:34 -080072 num_bad_states_(0),
73 num_certain_states_(0),
sprang@webrtc.org09315702014-02-07 12:06:29 +000074 // 1000ms window, scale 1000 for ms to s.
75 decode_fps_estimator_(1000, 1000),
Tim Psiaki63046262015-09-14 10:38:08 -070076 renders_fps_estimator_(1000, 1000),
Honghai Zhang82d78622016-05-06 11:29:15 -070077 render_fps_tracker_(100, 10u),
asaperssonde9e5ff2016-11-02 07:14:03 -070078 render_pixel_tracker_(100, 10u),
asapersson0255acb2017-03-28 02:44:58 -070079 total_byte_tracker_(100, 10u), // bucket_interval_ms, bucket_count
ilnik00d802b2017-04-11 10:34:31 -070080 e2e_delay_max_ms_video_(-1),
81 e2e_delay_max_ms_screenshare_(-1),
ilnik4257ab22017-07-03 01:15:58 -070082 interframe_delay_max_ms_video_(-1),
83 interframe_delay_max_ms_screenshare_(-1),
ilnika79cc282017-08-23 05:24:10 -070084 interframe_delay_max_moving_(kMovingMaxWindowMs),
asapersson0c43f772016-11-30 01:42:26 -080085 freq_offset_counter_(clock, nullptr, kFreqOffsetProcessIntervalMs),
philipela45102f2017-02-22 05:30:39 -080086 first_report_block_time_ms_(-1),
ilnik00d802b2017-04-11 10:34:31 -070087 avg_rtt_ms_(0),
88 last_content_type_(VideoContentType::UNSPECIFIED) {
Tommi733b5472016-06-10 17:58:01 +020089 stats_.ssrc = config_.rtp.remote_ssrc;
brandtr14742122017-01-27 04:53:07 -080090 // TODO(brandtr): Replace |rtx_stats_| with a single instance of
91 // StreamDataCounters.
92 if (config_.rtp.rtx_ssrc) {
93 rtx_stats_[config_.rtp.rtx_ssrc] = StreamDataCounters();
94 }
sprang@webrtc.org09315702014-02-07 12:06:29 +000095}
96
Åsa Persson3c391cb2015-04-27 10:09:49 +020097ReceiveStatisticsProxy::~ReceiveStatisticsProxy() {
98 UpdateHistograms();
99}
100
asaperssond89920b2015-07-22 06:52:00 -0700101void ReceiveStatisticsProxy::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -0700102 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -0700103 "WebRTC.Video.ReceiveStreamLifetimeInSeconds",
104 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
105
asapersson0c43f772016-11-30 01:42:26 -0800106 if (first_report_block_time_ms_ != -1 &&
107 ((clock_->TimeInMilliseconds() - first_report_block_time_ms_) / 1000) >=
108 metrics::kMinRunTimeInSeconds) {
109 int fraction_lost = report_block_stats_.FractionLostInPercent();
110 if (fraction_lost != -1) {
111 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedPacketsLostInPercent",
112 fraction_lost);
asapersson2077f2f2017-05-11 05:37:35 -0700113 LOG(LS_INFO) << "WebRTC.Video.ReceivedPacketsLostInPercent "
114 << fraction_lost;
asapersson0c43f772016-11-30 01:42:26 -0800115 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200116 }
asapersson0c43f772016-11-30 01:42:26 -0800117
asapersson6718e972015-07-24 00:20:58 -0700118 const int kMinRequiredSamples = 200;
asaperssonf839dcc2015-10-08 00:41:59 -0700119 int samples = static_cast<int>(render_fps_tracker_.TotalSampleCount());
asapersson2077f2f2017-05-11 05:37:35 -0700120 if (samples >= kMinRequiredSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700121 RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.RenderFramesPerSecond",
122 round(render_fps_tracker_.ComputeTotalRate()));
123 RTC_HISTOGRAM_COUNTS_100000(
asapersson28ba9272016-01-25 05:58:23 -0800124 "WebRTC.Video.RenderSqrtPixelsPerSecond",
Tim Psiakiad13d2f2015-11-10 16:34:50 -0800125 round(render_pixel_tracker_.ComputeTotalRate()));
asaperssonf839dcc2015-10-08 00:41:59 -0700126 }
asaperssond89920b2015-07-22 06:52:00 -0700127 int width = render_width_counter_.Avg(kMinRequiredSamples);
128 int height = render_height_counter_.Avg(kMinRequiredSamples);
129 if (width != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700130 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.ReceivedWidthInPixels", width);
131 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.ReceivedHeightInPixels", height);
asapersson2077f2f2017-05-11 05:37:35 -0700132 LOG(LS_INFO) << "WebRTC.Video.ReceivedWidthInPixels " << width;
133 LOG(LS_INFO) << "WebRTC.Video.ReceivedHeightInPixels " << height;
asaperssond89920b2015-07-22 06:52:00 -0700134 }
asaperssonf8cdd182016-03-15 01:00:47 -0700135 int sync_offset_ms = sync_offset_counter_.Avg(kMinRequiredSamples);
pbos35fdb2a2016-05-03 03:32:10 -0700136 if (sync_offset_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700137 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.AVSyncOffsetInMs", sync_offset_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700138 LOG(LS_INFO) << "WebRTC.Video.AVSyncOffsetInMs " << sync_offset_ms;
pbos35fdb2a2016-05-03 03:32:10 -0700139 }
asaperssonde9e5ff2016-11-02 07:14:03 -0700140 AggregatedStats freq_offset_stats = freq_offset_counter_.GetStats();
141 if (freq_offset_stats.num_samples > 0) {
142 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtpToNtpFreqOffsetInKhz",
143 freq_offset_stats.average);
asapersson43cb7162016-11-15 08:20:48 -0800144 LOG(LS_INFO) << "WebRTC.Video.RtpToNtpFreqOffsetInKhz, "
145 << freq_offset_stats.ToString();
asaperssonde9e5ff2016-11-02 07:14:03 -0700146 }
asaperssonf8cdd182016-03-15 01:00:47 -0700147
asaperssonb99baf82017-04-20 04:05:43 -0700148 int num_total_frames =
149 stats_.frame_counts.key_frames + stats_.frame_counts.delta_frames;
150 if (num_total_frames >= kMinRequiredSamples) {
151 int num_key_frames = stats_.frame_counts.key_frames;
philipela45102f2017-02-22 05:30:39 -0800152 int key_frames_permille =
asaperssonb99baf82017-04-20 04:05:43 -0700153 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
philipela45102f2017-02-22 05:30:39 -0800154 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesReceivedInPermille",
155 key_frames_permille);
asapersson2077f2f2017-05-11 05:37:35 -0700156 LOG(LS_INFO) << "WebRTC.Video.KeyFramesReceivedInPermille "
157 << key_frames_permille;
philipela45102f2017-02-22 05:30:39 -0800158 }
159
asapersson86b01602015-10-20 23:55:26 -0700160 int qp = qp_counters_.vp8.Avg(kMinRequiredSamples);
asapersson2077f2f2017-05-11 05:37:35 -0700161 if (qp != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700162 RTC_HISTOGRAM_COUNTS_200("WebRTC.Video.Decoded.Vp8.Qp", qp);
asapersson2077f2f2017-05-11 05:37:35 -0700163 LOG(LS_INFO) << "WebRTC.Video.Decoded.Vp8.Qp " << qp;
164 }
asaperssona563c212017-03-02 08:25:46 -0800165 int decode_ms = decode_time_counter_.Avg(kMinRequiredSamples);
asapersson2077f2f2017-05-11 05:37:35 -0700166 if (decode_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700167 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", decode_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700168 LOG(LS_INFO) << "WebRTC.Video.DecodeTimeInMs " << decode_ms;
169 }
asaperssona563c212017-03-02 08:25:46 -0800170 int jb_delay_ms = jitter_buffer_delay_counter_.Avg(kMinRequiredSamples);
philipela45102f2017-02-22 05:30:39 -0800171 if (jb_delay_ms != -1) {
172 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
173 jb_delay_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700174 LOG(LS_INFO) << "WebRTC.Video.JitterBufferDelayInMs " << jb_delay_ms;
asapersson8688a4e2016-04-27 23:42:35 -0700175 }
philipela45102f2017-02-22 05:30:39 -0800176
asaperssona563c212017-03-02 08:25:46 -0800177 int target_delay_ms = target_delay_counter_.Avg(kMinRequiredSamples);
asapersson8688a4e2016-04-27 23:42:35 -0700178 if (target_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700179 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs", target_delay_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700180 LOG(LS_INFO) << "WebRTC.Video.TargetDelayInMs " << target_delay_ms;
asapersson8688a4e2016-04-27 23:42:35 -0700181 }
asaperssona563c212017-03-02 08:25:46 -0800182 int current_delay_ms = current_delay_counter_.Avg(kMinRequiredSamples);
asapersson8688a4e2016-04-27 23:42:35 -0700183 if (current_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700184 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.CurrentDelayInMs",
185 current_delay_ms);
asapersson2077f2f2017-05-11 05:37:35 -0700186 LOG(LS_INFO) << "WebRTC.Video.CurrentDelayInMs " << current_delay_ms;
asapersson8688a4e2016-04-27 23:42:35 -0700187 }
asaperssona563c212017-03-02 08:25:46 -0800188 int delay_ms = delay_counter_.Avg(kMinRequiredSamples);
asapersson13c433c2015-10-06 04:08:15 -0700189 if (delay_ms != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700190 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", delay_ms);
sprang0ab8e812016-02-24 01:35:40 -0800191
ilnik00d802b2017-04-11 10:34:31 -0700192 int e2e_delay_ms_video = e2e_delay_counter_video_.Avg(kMinRequiredSamples);
193 if (e2e_delay_ms_video != -1) {
194 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.EndToEndDelayInMs",
195 e2e_delay_ms_video);
asapersson2077f2f2017-05-11 05:37:35 -0700196 LOG(LS_INFO) << "WebRTC.Video.EndToEndDelayInMs " << e2e_delay_ms_video;
ilnik00d802b2017-04-11 10:34:31 -0700197 }
198
199 int e2e_delay_ms_screenshare =
200 e2e_delay_counter_screenshare_.Avg(kMinRequiredSamples);
201 if (e2e_delay_ms_screenshare != -1) {
202 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.Screenshare.EndToEndDelayInMs",
203 e2e_delay_ms_screenshare);
204 }
205
206 int e2e_delay_max_ms_video = e2e_delay_max_ms_video_;
207 if (e2e_delay_max_ms_video != -1) {
208 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Video.EndToEndDelayMaxInMs",
209 e2e_delay_max_ms_video);
210 }
211
212 int e2e_delay_max_ms_screenshare = e2e_delay_max_ms_screenshare_;
213 if (e2e_delay_max_ms_screenshare != -1) {
214 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Video.Screenshare.EndToEndDelayMaxInMs",
215 e2e_delay_max_ms_screenshare);
216 }
asapersson1490f7a2016-09-23 02:09:46 -0700217
ilnik4257ab22017-07-03 01:15:58 -0700218 int interframe_delay_ms_screenshare =
219 interframe_delay_counter_screenshare_.Avg(kMinRequiredSamples);
220 if (interframe_delay_ms_screenshare != -1) {
221 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.Screenshare.InterframeDelayInMs",
222 interframe_delay_ms_screenshare);
sprang892dab52017-08-15 05:00:33 -0700223 RTC_DCHECK_GE(interframe_delay_max_ms_screenshare_,
224 interframe_delay_ms_screenshare);
225 RTC_HISTOGRAM_COUNTS_10000(
226 "WebRTC.Video.Screenshare.InterframeDelayMaxInMs",
227 interframe_delay_max_ms_screenshare_);
ilnik4257ab22017-07-03 01:15:58 -0700228 }
229
230 int interframe_delay_ms_video =
231 interframe_delay_counter_video_.Avg(kMinRequiredSamples);
232 if (interframe_delay_ms_video != -1) {
233 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.InterframeDelayInMs",
234 interframe_delay_ms_video);
sprang892dab52017-08-15 05:00:33 -0700235 RTC_DCHECK_GE(interframe_delay_max_ms_video_, interframe_delay_ms_video);
236 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.InterframeDelayMaxInMs",
237 interframe_delay_max_ms_video_);
ilnik4257ab22017-07-03 01:15:58 -0700238 }
239
sprang0ab8e812016-02-24 01:35:40 -0800240 StreamDataCounters rtp = stats_.rtp_stats;
241 StreamDataCounters rtx;
242 for (auto it : rtx_stats_)
243 rtx.Add(it.second);
244 StreamDataCounters rtp_rtx = rtp;
245 rtp_rtx.Add(rtx);
246 int64_t elapsed_sec =
247 rtp_rtx.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) / 1000;
asapersson2077f2f2017-05-11 05:37:35 -0700248 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
asapersson1d02d3e2016-09-09 22:40:25 -0700249 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800250 "WebRTC.Video.BitrateReceivedInKbps",
251 static_cast<int>(rtp_rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
252 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700253 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800254 "WebRTC.Video.MediaBitrateReceivedInKbps",
255 static_cast<int>(rtp.MediaPayloadBytes() * 8 / elapsed_sec / 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700256 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800257 "WebRTC.Video.PaddingBitrateReceivedInKbps",
258 static_cast<int>(rtp_rtx.transmitted.padding_bytes * 8 / elapsed_sec /
259 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700260 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800261 "WebRTC.Video.RetransmittedBitrateReceivedInKbps",
262 static_cast<int>(rtp_rtx.retransmitted.TotalBytes() * 8 / elapsed_sec /
263 1000));
264 if (!rtx_stats_.empty()) {
asapersson1d02d3e2016-09-09 22:40:25 -0700265 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtxBitrateReceivedInKbps",
266 static_cast<int>(rtx.transmitted.TotalBytes() *
267 8 / elapsed_sec / 1000));
sprang0ab8e812016-02-24 01:35:40 -0800268 }
brandtrb5f2c3f2016-10-04 23:28:39 -0700269 if (config_.rtp.ulpfec.ulpfec_payload_type != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700270 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800271 "WebRTC.Video.FecBitrateReceivedInKbps",
272 static_cast<int>(rtp_rtx.fec.TotalBytes() * 8 / elapsed_sec / 1000));
273 }
sprang07fb9be2016-02-24 07:55:00 -0800274 const RtcpPacketTypeCounter& counters = stats_.rtcp_packet_type_counts;
asapersson1d02d3e2016-09-09 22:40:25 -0700275 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.NackPacketsSentPerMinute",
276 counters.nack_packets * 60 / elapsed_sec);
277 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.FirPacketsSentPerMinute",
278 counters.fir_packets * 60 / elapsed_sec);
279 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.PliPacketsSentPerMinute",
280 counters.pli_packets * 60 / elapsed_sec);
sprang07fb9be2016-02-24 07:55:00 -0800281 if (counters.nack_requests > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700282 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.UniqueNackRequestsSentInPercent",
283 counters.UniqueNackRequestsInPercent());
sprang07fb9be2016-02-24 07:55:00 -0800284 }
sprang0ab8e812016-02-24 01:35:40 -0800285 }
palmkvista40672a2017-01-13 05:58:34 -0800286
287 if (num_certain_states_ >= kBadCallMinRequiredSamples) {
288 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Any",
289 100 * num_bad_states_ / num_certain_states_);
290 }
291 rtc::Optional<double> fps_fraction =
292 fps_threshold_.FractionHigh(kBadCallMinRequiredSamples);
293 if (fps_fraction) {
294 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRate",
295 static_cast<int>(100 * (1 - *fps_fraction)));
296 }
297 rtc::Optional<double> variance_fraction =
298 variance_threshold_.FractionHigh(kBadCallMinRequiredSamples);
299 if (variance_fraction) {
300 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRateVariance",
301 static_cast<int>(100 * *variance_fraction));
302 }
303 rtc::Optional<double> qp_fraction =
304 qp_threshold_.FractionHigh(kBadCallMinRequiredSamples);
305 if (qp_fraction) {
306 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Qp",
307 static_cast<int>(100 * *qp_fraction));
308 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200309}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000310
palmkvist349092b2016-12-13 02:45:57 -0800311void ReceiveStatisticsProxy::QualitySample() {
312 int64_t now = clock_->TimeInMilliseconds();
313 if (last_sample_time_ + kMinSampleLengthMs > now)
314 return;
315
316 double fps =
317 render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_);
318 int qp = qp_sample_.Avg(1);
319
320 bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true);
321 bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false);
322 bool prev_variance_bad = variance_threshold_.IsHigh().value_or(false);
323 bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad;
324
325 fps_threshold_.AddMeasurement(static_cast<int>(fps));
326 if (qp != -1)
327 qp_threshold_.AddMeasurement(qp);
328 rtc::Optional<double> fps_variance_opt = fps_threshold_.CalculateVariance();
329 double fps_variance = fps_variance_opt.value_or(0);
330 if (fps_variance_opt) {
331 variance_threshold_.AddMeasurement(static_cast<int>(fps_variance));
332 }
333
334 bool fps_bad = !fps_threshold_.IsHigh().value_or(true);
335 bool qp_bad = qp_threshold_.IsHigh().value_or(false);
336 bool variance_bad = variance_threshold_.IsHigh().value_or(false);
337 bool any_bad = fps_bad || qp_bad || variance_bad;
338
339 if (!prev_any_bad && any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800340 LOG(LS_INFO) << "Bad call (any) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800341 } else if (prev_any_bad && !any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800342 LOG(LS_INFO) << "Bad call (any) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800343 }
344
345 if (!prev_fps_bad && fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800346 LOG(LS_INFO) << "Bad call (fps) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800347 } else if (prev_fps_bad && !fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800348 LOG(LS_INFO) << "Bad call (fps) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800349 }
350
351 if (!prev_qp_bad && qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800352 LOG(LS_INFO) << "Bad call (qp) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800353 } else if (prev_qp_bad && !qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800354 LOG(LS_INFO) << "Bad call (qp) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800355 }
356
357 if (!prev_variance_bad && variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800358 LOG(LS_INFO) << "Bad call (variance) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800359 } else if (prev_variance_bad && !variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800360 LOG(LS_INFO) << "Bad call (variance) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800361 }
362
palmkvistc7e7e072017-01-09 07:23:33 -0800363 LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_)
364 << " fps: " << fps << " fps_bad: " << fps_bad << " qp: " << qp
365 << " qp_bad: " << qp_bad << " variance_bad: " << variance_bad
366 << " fps_variance: " << fps_variance;
palmkvist349092b2016-12-13 02:45:57 -0800367
368 last_sample_time_ = now;
369 qp_sample_.Reset();
palmkvista40672a2017-01-13 05:58:34 -0800370
371 if (fps_threshold_.IsHigh() || variance_threshold_.IsHigh() ||
372 qp_threshold_.IsHigh()) {
373 if (any_bad)
374 ++num_bad_states_;
375 ++num_certain_states_;
376 }
palmkvist349092b2016-12-13 02:45:57 -0800377}
378
asapersson0255acb2017-03-28 02:44:58 -0700379void ReceiveStatisticsProxy::UpdateFramerate(int64_t now_ms) const {
philipela45102f2017-02-22 05:30:39 -0800380 int64_t old_frames_ms = now_ms - kRateStatisticsWindowSizeMs;
381 while (!frame_window_.empty() &&
382 frame_window_.begin()->first < old_frames_ms) {
philipela45102f2017-02-22 05:30:39 -0800383 frame_window_.erase(frame_window_.begin());
384 }
385
386 size_t framerate =
387 (frame_window_.size() * 1000 + 500) / kRateStatisticsWindowSizeMs;
philipela45102f2017-02-22 05:30:39 -0800388 stats_.network_frame_rate = static_cast<int>(framerate);
philipela45102f2017-02-22 05:30:39 -0800389}
390
sprang@webrtc.org09315702014-02-07 12:06:29 +0000391VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const {
Peter Boströmf2f82832015-05-01 13:00:41 +0200392 rtc::CritScope lock(&crit_);
sprang948b2752017-05-04 02:47:13 -0700393 // Get current frame rates here, as only updating them on new frames prevents
394 // us from ever correctly displaying frame rate of 0.
395 int64_t now_ms = clock_->TimeInMilliseconds();
396 UpdateFramerate(now_ms);
397 stats_.render_frame_rate = renders_fps_estimator_.Rate(now_ms).value_or(0);
398 stats_.decode_frame_rate = decode_fps_estimator_.Rate(now_ms).value_or(0);
asapersson0255acb2017-03-28 02:44:58 -0700399 stats_.total_bitrate_bps =
400 static_cast<int>(total_byte_tracker_.ComputeRate() * 8);
ilnika79cc282017-08-23 05:24:10 -0700401 stats_.interframe_delay_max_ms =
402 interframe_delay_max_moving_.Max(now_ms).value_or(-1);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000403 return stats_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000404}
405
ilnik2edc6842017-07-06 03:06:50 -0700406rtc::Optional<TimingFrameInfo>
407ReceiveStatisticsProxy::GetAndResetTimingFrameInfo() {
408 rtc::CritScope lock(&crit_);
409 rtc::Optional<TimingFrameInfo> info = timing_frame_info_;
410 // Reset reported value to empty, so it will be always
411 // overwritten in |OnTimingFrameInfoUpdated|, thus allowing to store new
412 // value instead of reported one.
413 timing_frame_info_.reset();
414 return info;
415}
416
pbosf42376c2015-08-28 07:35:32 -0700417void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) {
418 rtc::CritScope lock(&crit_);
419 stats_.current_payload_type = payload_type;
420}
421
Peter Boströmb7d9a972015-12-18 16:01:11 +0100422void ReceiveStatisticsProxy::OnDecoderImplementationName(
423 const char* implementation_name) {
424 rtc::CritScope lock(&crit_);
425 stats_.decoder_implementation_name = implementation_name;
426}
pbosf42376c2015-08-28 07:35:32 -0700427void ReceiveStatisticsProxy::OnIncomingRate(unsigned int framerate,
428 unsigned int bitrate_bps) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200429 rtc::CritScope lock(&crit_);
palmkvista40672a2017-01-13 05:58:34 -0800430 if (stats_.rtp_stats.first_packet_time_ms != -1)
431 QualitySample();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000432}
433
philipela45102f2017-02-22 05:30:39 -0800434void ReceiveStatisticsProxy::OnFrameBufferTimingsUpdated(
435 int decode_ms,
436 int max_decode_ms,
437 int current_delay_ms,
438 int target_delay_ms,
439 int jitter_buffer_ms,
440 int min_playout_delay_ms,
441 int render_delay_ms) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200442 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000443 stats_.decode_ms = decode_ms;
444 stats_.max_decode_ms = max_decode_ms;
445 stats_.current_delay_ms = current_delay_ms;
446 stats_.target_delay_ms = target_delay_ms;
447 stats_.jitter_buffer_ms = jitter_buffer_ms;
448 stats_.min_playout_delay_ms = min_playout_delay_ms;
449 stats_.render_delay_ms = render_delay_ms;
asapersson6718e972015-07-24 00:20:58 -0700450 decode_time_counter_.Add(decode_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700451 jitter_buffer_delay_counter_.Add(jitter_buffer_ms);
452 target_delay_counter_.Add(target_delay_ms);
453 current_delay_counter_.Add(current_delay_ms);
asaperssona1862882016-04-18 00:41:05 -0700454 // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time +
455 // render delay).
philipela45102f2017-02-22 05:30:39 -0800456 delay_counter_.Add(target_delay_ms + avg_rtt_ms_ / 2);
pbos@webrtc.org98c04b32014-12-18 13:12:52 +0000457}
458
ilnik2edc6842017-07-06 03:06:50 -0700459void ReceiveStatisticsProxy::OnTimingFrameInfoUpdated(
460 const TimingFrameInfo& info) {
461 rtc::CritScope lock(&crit_);
462 // Only the frame which was processed the longest since the last
463 // GetStats() call is reported. Therefore, only single 'longest' frame is
464 // stored.
465 if (!timing_frame_info_ || info.IsLongerThan(*timing_frame_info_)) {
466 timing_frame_info_.emplace(info);
467 }
468}
469
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000470void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated(
471 uint32_t ssrc,
472 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200473 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000474 if (stats_.ssrc != ssrc)
475 return;
476 stats_.rtcp_packet_type_counts = packet_counter;
477}
478
sprang@webrtc.org09315702014-02-07 12:06:29 +0000479void ReceiveStatisticsProxy::StatisticsUpdated(
480 const webrtc::RtcpStatistics& statistics,
481 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200482 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700483 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000484 // receive stats from one of them.
485 if (stats_.ssrc != ssrc)
486 return;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000487 stats_.rtcp_stats = statistics;
Åsa Persson3c391cb2015-04-27 10:09:49 +0200488 report_block_stats_.Store(statistics, ssrc, 0);
asapersson0c43f772016-11-30 01:42:26 -0800489
490 if (first_report_block_time_ms_ == -1)
491 first_report_block_time_ms_ = clock_->TimeInMilliseconds();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000492}
493
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000494void ReceiveStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200495 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700496 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000497 // receive stats from one of them.
498 if (stats_.ssrc != ssrc)
499 return;
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000500 stats_.c_name = cname;
501}
502
sprang@webrtc.org09315702014-02-07 12:06:29 +0000503void ReceiveStatisticsProxy::DataCountersUpdated(
504 const webrtc::StreamDataCounters& counters,
505 uint32_t ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700506 size_t last_total_bytes = 0;
507 size_t total_bytes = 0;
Peter Boströmf2f82832015-05-01 13:00:41 +0200508 rtc::CritScope lock(&crit_);
sprang0ab8e812016-02-24 01:35:40 -0800509 if (ssrc == stats_.ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700510 last_total_bytes = stats_.rtp_stats.transmitted.TotalBytes();
511 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800512 stats_.rtp_stats = counters;
513 } else {
514 auto it = rtx_stats_.find(ssrc);
515 if (it != rtx_stats_.end()) {
asapersson0255acb2017-03-28 02:44:58 -0700516 last_total_bytes = it->second.transmitted.TotalBytes();
517 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800518 it->second = counters;
519 } else {
520 RTC_NOTREACHED() << "Unexpected stream ssrc: " << ssrc;
521 }
522 }
asapersson0255acb2017-03-28 02:44:58 -0700523 if (total_bytes > last_total_bytes)
524 total_byte_tracker_.AddSamples(total_bytes - last_total_bytes);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000525}
526
ilnik00d802b2017-04-11 10:34:31 -0700527void ReceiveStatisticsProxy::OnDecodedFrame(rtc::Optional<uint8_t> qp,
528 VideoContentType content_type) {
sprang@webrtc.org09315702014-02-07 12:06:29 +0000529 uint64_t now = clock_->TimeInMilliseconds();
530
Peter Boströmf2f82832015-05-01 13:00:41 +0200531 rtc::CritScope lock(&crit_);
sakale5ba44e2016-10-26 07:09:24 -0700532 ++stats_.frames_decoded;
sakalcc452e12017-02-09 04:53:45 -0800533 if (qp) {
534 if (!stats_.qp_sum) {
535 if (stats_.frames_decoded != 1) {
536 LOG(LS_WARNING)
537 << "Frames decoded was not 1 when first qp value was received.";
538 stats_.frames_decoded = 1;
539 }
540 stats_.qp_sum = rtc::Optional<uint64_t>(0);
541 }
542 *stats_.qp_sum += *qp;
543 } else if (stats_.qp_sum) {
544 LOG(LS_WARNING)
545 << "QP sum was already set and no QP was given for a frame.";
546 stats_.qp_sum = rtc::Optional<uint64_t>();
547 }
ilnik00d802b2017-04-11 10:34:31 -0700548 last_content_type_ = content_type;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000549 decode_fps_estimator_.Update(1, now);
ilnik4257ab22017-07-03 01:15:58 -0700550 if (last_decoded_frame_time_ms_) {
551 int64_t interframe_delay_ms = now - *last_decoded_frame_time_ms_;
552 RTC_DCHECK_GE(interframe_delay_ms, 0);
ilnika79cc282017-08-23 05:24:10 -0700553 interframe_delay_max_moving_.Add(interframe_delay_ms, now);
ilnik4257ab22017-07-03 01:15:58 -0700554 if (last_content_type_ == VideoContentType::SCREENSHARE) {
555 interframe_delay_counter_screenshare_.Add(interframe_delay_ms);
556 if (interframe_delay_max_ms_screenshare_ < interframe_delay_ms) {
557 interframe_delay_max_ms_screenshare_ = interframe_delay_ms;
558 }
559 } else {
560 interframe_delay_counter_video_.Add(interframe_delay_ms);
561 if (interframe_delay_max_ms_video_ < interframe_delay_ms) {
562 interframe_delay_max_ms_video_ = interframe_delay_ms;
563 }
564 }
565 }
566 last_decoded_frame_time_ms_.emplace(now);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000567}
568
asapersson1490f7a2016-09-23 02:09:46 -0700569void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) {
570 int width = frame.width();
571 int height = frame.height();
asaperssonf839dcc2015-10-08 00:41:59 -0700572 RTC_DCHECK_GT(width, 0);
573 RTC_DCHECK_GT(height, 0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000574 uint64_t now = clock_->TimeInMilliseconds();
575
Peter Boströmf2f82832015-05-01 13:00:41 +0200576 rtc::CritScope lock(&crit_);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000577 renders_fps_estimator_.Update(1, now);
hbos50cfe1f2017-01-23 07:21:55 -0800578 ++stats_.frames_rendered;
asapersson2e5cfcd2016-08-11 08:41:18 -0700579 stats_.width = width;
580 stats_.height = height;
asaperssond89920b2015-07-22 06:52:00 -0700581 render_width_counter_.Add(width);
582 render_height_counter_.Add(height);
Tim Psiaki63046262015-09-14 10:38:08 -0700583 render_fps_tracker_.AddSamples(1);
asaperssonf839dcc2015-10-08 00:41:59 -0700584 render_pixel_tracker_.AddSamples(sqrt(width * height));
asapersson1490f7a2016-09-23 02:09:46 -0700585
586 if (frame.ntp_time_ms() > 0) {
587 int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms();
ilnik00d802b2017-04-11 10:34:31 -0700588 if (delay_ms >= 0) {
589 if (last_content_type_ == VideoContentType::SCREENSHARE) {
590 e2e_delay_max_ms_screenshare_ =
591 std::max(delay_ms, e2e_delay_max_ms_screenshare_);
592 e2e_delay_counter_screenshare_.Add(delay_ms);
593 } else {
594 e2e_delay_max_ms_video_ = std::max(delay_ms, e2e_delay_max_ms_video_);
595 e2e_delay_counter_video_.Add(delay_ms);
596 }
597 }
asapersson1490f7a2016-09-23 02:09:46 -0700598 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000599}
600
asaperssonde9e5ff2016-11-02 07:14:03 -0700601void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t sync_offset_ms,
602 double estimated_freq_khz) {
asaperssonf8cdd182016-03-15 01:00:47 -0700603 rtc::CritScope lock(&crit_);
604 sync_offset_counter_.Add(std::abs(sync_offset_ms));
605 stats_.sync_offset_ms = sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700606
607 const double kMaxFreqKhz = 10000.0;
608 int offset_khz = kMaxFreqKhz;
609 // Should not be zero or negative. If so, report max.
610 if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0)
611 offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5);
612
613 freq_offset_counter_.Add(offset_khz);
asaperssonf8cdd182016-03-15 01:00:47 -0700614}
615
pbos@webrtc.org55707692014-12-19 15:45:03 +0000616void ReceiveStatisticsProxy::OnReceiveRatesUpdated(uint32_t bitRate,
617 uint32_t frameRate) {
618}
619
philipela45102f2017-02-22 05:30:39 -0800620void ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe,
621 size_t size_bytes) {
622 rtc::CritScope lock(&crit_);
623 if (is_keyframe)
624 ++stats_.frame_counts.key_frames;
625 else
626 ++stats_.frame_counts.delta_frames;
627
628 int64_t now_ms = clock_->TimeInMilliseconds();
philipela45102f2017-02-22 05:30:39 -0800629 frame_window_.insert(std::make_pair(now_ms, size_bytes));
asapersson0255acb2017-03-28 02:44:58 -0700630 UpdateFramerate(now_ms);
philipela45102f2017-02-22 05:30:39 -0800631}
632
pbos@webrtc.org55707692014-12-19 15:45:03 +0000633void ReceiveStatisticsProxy::OnFrameCountsUpdated(
634 const FrameCounts& frame_counts) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200635 rtc::CritScope lock(&crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000636 stats_.frame_counts = frame_counts;
637}
638
pbos@webrtc.org55707692014-12-19 15:45:03 +0000639void ReceiveStatisticsProxy::OnDiscardedPacketsUpdated(int discarded_packets) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200640 rtc::CritScope lock(&crit_);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000641 stats_.discarded_packets = discarded_packets;
642}
643
asapersson86b01602015-10-20 23:55:26 -0700644void ReceiveStatisticsProxy::OnPreDecode(
645 const EncodedImage& encoded_image,
646 const CodecSpecificInfo* codec_specific_info) {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200647 if (!codec_specific_info || encoded_image.qp_ == -1) {
asapersson86b01602015-10-20 23:55:26 -0700648 return;
649 }
650 if (codec_specific_info->codecType == kVideoCodecVP8) {
651 qp_counters_.vp8.Add(encoded_image.qp_);
palmkvist349092b2016-12-13 02:45:57 -0800652 rtc::CritScope lock(&crit_);
653 qp_sample_.Add(encoded_image.qp_);
asapersson86b01602015-10-20 23:55:26 -0700654 }
655}
656
sprang3e86e7e2017-08-22 09:23:28 -0700657void ReceiveStatisticsProxy::OnStreamInactive() {
658 // TODO(sprang): Figure out any other state that should be reset.
659
660 rtc::CritScope lock(&crit_);
661 // Don't report inter-frame delay if stream was paused.
662 last_decoded_frame_time_ms_.reset();
663}
664
asaperssond89920b2015-07-22 06:52:00 -0700665void ReceiveStatisticsProxy::SampleCounter::Add(int sample) {
666 sum += sample;
667 ++num_samples;
668}
669
asapersson6966bd52017-01-03 00:44:06 -0800670int ReceiveStatisticsProxy::SampleCounter::Avg(
671 int64_t min_required_samples) const {
asaperssond89920b2015-07-22 06:52:00 -0700672 if (num_samples < min_required_samples || num_samples == 0)
673 return -1;
asapersson6966bd52017-01-03 00:44:06 -0800674 return static_cast<int>(sum / num_samples);
asaperssond89920b2015-07-22 06:52:00 -0700675}
676
palmkvist349092b2016-12-13 02:45:57 -0800677void ReceiveStatisticsProxy::SampleCounter::Reset() {
678 num_samples = 0;
679 sum = 0;
680}
681
philipela45102f2017-02-22 05:30:39 -0800682void ReceiveStatisticsProxy::OnRttUpdate(int64_t avg_rtt_ms,
683 int64_t max_rtt_ms) {
684 rtc::CritScope lock(&crit_);
685 avg_rtt_ms_ = avg_rtt_ms;
686}
687
sprang@webrtc.org09315702014-02-07 12:06:29 +0000688} // namespace webrtc