blob: c847a178e8072c15df945b4653925120a8293f6d [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
asaperssonf839dcc2015-10-08 00:41:59 -070013#include <cmath>
14
15#include "webrtc/base/checks.h"
palmkvist349092b2016-12-13 02:45:57 -080016#include "webrtc/base/logging.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010017#include "webrtc/modules/video_coding/include/video_codec_interface.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010018#include "webrtc/system_wrappers/include/clock.h"
philipelbe742702016-11-30 01:31:40 -080019#include "webrtc/system_wrappers/include/field_trial.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010020#include "webrtc/system_wrappers/include/metrics.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000021
22namespace webrtc {
asaperssonde9e5ff2016-11-02 07:14:03 -070023namespace {
24// Periodic time interval for processing samples for |freq_offset_counter_|.
25const int64_t kFreqOffsetProcessIntervalMs = 40000;
palmkvist349092b2016-12-13 02:45:57 -080026
27// Configuration for bad call detection.
palmkvista40672a2017-01-13 05:58:34 -080028const int kBadCallMinRequiredSamples = 10;
palmkvist349092b2016-12-13 02:45:57 -080029const int kMinSampleLengthMs = 990;
30const int kNumMeasurements = 10;
31const int kNumMeasurementsVariance = kNumMeasurements * 1.5;
32const float kBadFraction = 0.8f;
33// For fps:
34// Low means low enough to be bad, high means high enough to be good
35const int kLowFpsThreshold = 12;
36const int kHighFpsThreshold = 14;
37// For qp and fps variance:
38// Low means low enough to be good, high means high enough to be bad
39const int kLowQpThresholdVp8 = 60;
40const int kHighQpThresholdVp8 = 70;
41const int kLowVarianceThreshold = 1;
42const int kHighVarianceThreshold = 2;
asaperssonde9e5ff2016-11-02 07:14:03 -070043} // namespace
sprang@webrtc.org09315702014-02-07 12:06:29 +000044
sprang0ab8e812016-02-24 01:35:40 -080045ReceiveStatisticsProxy::ReceiveStatisticsProxy(
Tommi733b5472016-06-10 17:58:01 +020046 const VideoReceiveStream::Config* config,
sprang0ab8e812016-02-24 01:35:40 -080047 Clock* clock)
pbos@webrtc.org55707692014-12-19 15:45:03 +000048 : clock_(clock),
Tommi733b5472016-06-10 17:58:01 +020049 config_(*config),
asapersson4374a092016-07-27 00:39:09 -070050 start_ms_(clock->TimeInMilliseconds()),
palmkvist349092b2016-12-13 02:45:57 -080051 last_sample_time_(clock->TimeInMilliseconds()),
52 fps_threshold_(kLowFpsThreshold,
53 kHighFpsThreshold,
54 kBadFraction,
55 kNumMeasurements),
56 qp_threshold_(kLowQpThresholdVp8,
57 kHighQpThresholdVp8,
58 kBadFraction,
59 kNumMeasurements),
60 variance_threshold_(kLowVarianceThreshold,
61 kHighVarianceThreshold,
62 kBadFraction,
63 kNumMeasurementsVariance),
palmkvista40672a2017-01-13 05:58:34 -080064 num_bad_states_(0),
65 num_certain_states_(0),
sprang@webrtc.org09315702014-02-07 12:06:29 +000066 // 1000ms window, scale 1000 for ms to s.
67 decode_fps_estimator_(1000, 1000),
Tim Psiaki63046262015-09-14 10:38:08 -070068 renders_fps_estimator_(1000, 1000),
Honghai Zhang82d78622016-05-06 11:29:15 -070069 render_fps_tracker_(100, 10u),
asaperssonde9e5ff2016-11-02 07:14:03 -070070 render_pixel_tracker_(100, 10u),
asapersson0c43f772016-11-30 01:42:26 -080071 freq_offset_counter_(clock, nullptr, kFreqOffsetProcessIntervalMs),
stefane525d6a2017-02-08 05:25:42 -080072 first_report_block_time_ms_(-1) {
Tommi733b5472016-06-10 17:58:01 +020073 stats_.ssrc = config_.rtp.remote_ssrc;
brandtr14742122017-01-27 04:53:07 -080074 // TODO(brandtr): Replace |rtx_stats_| with a single instance of
75 // StreamDataCounters.
76 if (config_.rtp.rtx_ssrc) {
77 rtx_stats_[config_.rtp.rtx_ssrc] = StreamDataCounters();
78 }
sprang@webrtc.org09315702014-02-07 12:06:29 +000079}
80
Åsa Persson3c391cb2015-04-27 10:09:49 +020081ReceiveStatisticsProxy::~ReceiveStatisticsProxy() {
82 UpdateHistograms();
83}
84
asaperssond89920b2015-07-22 06:52:00 -070085void ReceiveStatisticsProxy::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -070086 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -070087 "WebRTC.Video.ReceiveStreamLifetimeInSeconds",
88 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
89
asapersson0c43f772016-11-30 01:42:26 -080090 if (first_report_block_time_ms_ != -1 &&
91 ((clock_->TimeInMilliseconds() - first_report_block_time_ms_) / 1000) >=
92 metrics::kMinRunTimeInSeconds) {
93 int fraction_lost = report_block_stats_.FractionLostInPercent();
94 if (fraction_lost != -1) {
95 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedPacketsLostInPercent",
96 fraction_lost);
97 }
Åsa Persson3c391cb2015-04-27 10:09:49 +020098 }
asapersson0c43f772016-11-30 01:42:26 -080099
asapersson6718e972015-07-24 00:20:58 -0700100 const int kMinRequiredSamples = 200;
asaperssonf839dcc2015-10-08 00:41:59 -0700101 int samples = static_cast<int>(render_fps_tracker_.TotalSampleCount());
102 if (samples > kMinRequiredSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700103 RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.RenderFramesPerSecond",
104 round(render_fps_tracker_.ComputeTotalRate()));
105 RTC_HISTOGRAM_COUNTS_100000(
asapersson28ba9272016-01-25 05:58:23 -0800106 "WebRTC.Video.RenderSqrtPixelsPerSecond",
Tim Psiakiad13d2f2015-11-10 16:34:50 -0800107 round(render_pixel_tracker_.ComputeTotalRate()));
asaperssonf839dcc2015-10-08 00:41:59 -0700108 }
asaperssond89920b2015-07-22 06:52:00 -0700109 int width = render_width_counter_.Avg(kMinRequiredSamples);
110 int height = render_height_counter_.Avg(kMinRequiredSamples);
111 if (width != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700112 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.ReceivedWidthInPixels", width);
113 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.ReceivedHeightInPixels", height);
asaperssond89920b2015-07-22 06:52:00 -0700114 }
asaperssonf8cdd182016-03-15 01:00:47 -0700115 int sync_offset_ms = sync_offset_counter_.Avg(kMinRequiredSamples);
pbos35fdb2a2016-05-03 03:32:10 -0700116 if (sync_offset_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700117 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.AVSyncOffsetInMs", sync_offset_ms);
pbos35fdb2a2016-05-03 03:32:10 -0700118 }
asaperssonde9e5ff2016-11-02 07:14:03 -0700119 AggregatedStats freq_offset_stats = freq_offset_counter_.GetStats();
120 if (freq_offset_stats.num_samples > 0) {
121 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtpToNtpFreqOffsetInKhz",
122 freq_offset_stats.average);
asapersson43cb7162016-11-15 08:20:48 -0800123 LOG(LS_INFO) << "WebRTC.Video.RtpToNtpFreqOffsetInKhz, "
124 << freq_offset_stats.ToString();
asaperssonde9e5ff2016-11-02 07:14:03 -0700125 }
asaperssonf8cdd182016-03-15 01:00:47 -0700126
asapersson86b01602015-10-20 23:55:26 -0700127 int qp = qp_counters_.vp8.Avg(kMinRequiredSamples);
128 if (qp != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700129 RTC_HISTOGRAM_COUNTS_200("WebRTC.Video.Decoded.Vp8.Qp", qp);
asapersson86b01602015-10-20 23:55:26 -0700130
asapersson6718e972015-07-24 00:20:58 -0700131 // TODO(asapersson): DecoderTiming() is call periodically (each 1000ms) and
132 // not per frame. Change decode time to include every frame.
133 const int kMinRequiredDecodeSamples = 5;
134 int decode_ms = decode_time_counter_.Avg(kMinRequiredDecodeSamples);
135 if (decode_ms != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700136 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", decode_ms);
asapersson13c433c2015-10-06 04:08:15 -0700137
stefane525d6a2017-02-08 05:25:42 -0800138 if (field_trial::FindFullName("WebRTC-NewVideoJitterBuffer") !=
139 "Enabled") {
140 int jb_delay_ms =
141 jitter_buffer_delay_counter_.Avg(kMinRequiredDecodeSamples);
142 if (jb_delay_ms != -1) {
143 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
144 jb_delay_ms);
145 }
asapersson8688a4e2016-04-27 23:42:35 -0700146 }
147 int target_delay_ms = target_delay_counter_.Avg(kMinRequiredDecodeSamples);
148 if (target_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700149 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs", target_delay_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700150 }
151 int current_delay_ms = current_delay_counter_.Avg(kMinRequiredDecodeSamples);
152 if (current_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700153 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.CurrentDelayInMs",
154 current_delay_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700155 }
asapersson13c433c2015-10-06 04:08:15 -0700156 int delay_ms = delay_counter_.Avg(kMinRequiredDecodeSamples);
157 if (delay_ms != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700158 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", delay_ms);
sprang0ab8e812016-02-24 01:35:40 -0800159
asapersson1490f7a2016-09-23 02:09:46 -0700160 int e2e_delay_ms = e2e_delay_counter_.Avg(kMinRequiredSamples);
161 if (e2e_delay_ms != -1)
162 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.EndToEndDelayInMs", e2e_delay_ms);
163
sprang0ab8e812016-02-24 01:35:40 -0800164 StreamDataCounters rtp = stats_.rtp_stats;
165 StreamDataCounters rtx;
166 for (auto it : rtx_stats_)
167 rtx.Add(it.second);
168 StreamDataCounters rtp_rtx = rtp;
169 rtp_rtx.Add(rtx);
170 int64_t elapsed_sec =
171 rtp_rtx.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) / 1000;
172 if (elapsed_sec > metrics::kMinRunTimeInSeconds) {
asapersson1d02d3e2016-09-09 22:40:25 -0700173 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800174 "WebRTC.Video.BitrateReceivedInKbps",
175 static_cast<int>(rtp_rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
176 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700177 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800178 "WebRTC.Video.MediaBitrateReceivedInKbps",
179 static_cast<int>(rtp.MediaPayloadBytes() * 8 / elapsed_sec / 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700180 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800181 "WebRTC.Video.PaddingBitrateReceivedInKbps",
182 static_cast<int>(rtp_rtx.transmitted.padding_bytes * 8 / elapsed_sec /
183 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700184 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800185 "WebRTC.Video.RetransmittedBitrateReceivedInKbps",
186 static_cast<int>(rtp_rtx.retransmitted.TotalBytes() * 8 / elapsed_sec /
187 1000));
188 if (!rtx_stats_.empty()) {
asapersson1d02d3e2016-09-09 22:40:25 -0700189 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtxBitrateReceivedInKbps",
190 static_cast<int>(rtx.transmitted.TotalBytes() *
191 8 / elapsed_sec / 1000));
sprang0ab8e812016-02-24 01:35:40 -0800192 }
brandtrb5f2c3f2016-10-04 23:28:39 -0700193 if (config_.rtp.ulpfec.ulpfec_payload_type != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700194 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800195 "WebRTC.Video.FecBitrateReceivedInKbps",
196 static_cast<int>(rtp_rtx.fec.TotalBytes() * 8 / elapsed_sec / 1000));
197 }
sprang07fb9be2016-02-24 07:55:00 -0800198 const RtcpPacketTypeCounter& counters = stats_.rtcp_packet_type_counts;
asapersson1d02d3e2016-09-09 22:40:25 -0700199 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.NackPacketsSentPerMinute",
200 counters.nack_packets * 60 / elapsed_sec);
201 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.FirPacketsSentPerMinute",
202 counters.fir_packets * 60 / elapsed_sec);
203 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.PliPacketsSentPerMinute",
204 counters.pli_packets * 60 / elapsed_sec);
sprang07fb9be2016-02-24 07:55:00 -0800205 if (counters.nack_requests > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700206 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.UniqueNackRequestsSentInPercent",
207 counters.UniqueNackRequestsInPercent());
sprang07fb9be2016-02-24 07:55:00 -0800208 }
sprang0ab8e812016-02-24 01:35:40 -0800209 }
palmkvista40672a2017-01-13 05:58:34 -0800210
211 if (num_certain_states_ >= kBadCallMinRequiredSamples) {
212 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Any",
213 100 * num_bad_states_ / num_certain_states_);
214 }
215 rtc::Optional<double> fps_fraction =
216 fps_threshold_.FractionHigh(kBadCallMinRequiredSamples);
217 if (fps_fraction) {
218 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRate",
219 static_cast<int>(100 * (1 - *fps_fraction)));
220 }
221 rtc::Optional<double> variance_fraction =
222 variance_threshold_.FractionHigh(kBadCallMinRequiredSamples);
223 if (variance_fraction) {
224 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRateVariance",
225 static_cast<int>(100 * *variance_fraction));
226 }
227 rtc::Optional<double> qp_fraction =
228 qp_threshold_.FractionHigh(kBadCallMinRequiredSamples);
229 if (qp_fraction) {
230 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Qp",
231 static_cast<int>(100 * *qp_fraction));
232 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200233}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000234
palmkvist349092b2016-12-13 02:45:57 -0800235void ReceiveStatisticsProxy::QualitySample() {
236 int64_t now = clock_->TimeInMilliseconds();
237 if (last_sample_time_ + kMinSampleLengthMs > now)
238 return;
239
240 double fps =
241 render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_);
242 int qp = qp_sample_.Avg(1);
243
244 bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true);
245 bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false);
246 bool prev_variance_bad = variance_threshold_.IsHigh().value_or(false);
247 bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad;
248
249 fps_threshold_.AddMeasurement(static_cast<int>(fps));
250 if (qp != -1)
251 qp_threshold_.AddMeasurement(qp);
252 rtc::Optional<double> fps_variance_opt = fps_threshold_.CalculateVariance();
253 double fps_variance = fps_variance_opt.value_or(0);
254 if (fps_variance_opt) {
255 variance_threshold_.AddMeasurement(static_cast<int>(fps_variance));
256 }
257
258 bool fps_bad = !fps_threshold_.IsHigh().value_or(true);
259 bool qp_bad = qp_threshold_.IsHigh().value_or(false);
260 bool variance_bad = variance_threshold_.IsHigh().value_or(false);
261 bool any_bad = fps_bad || qp_bad || variance_bad;
262
263 if (!prev_any_bad && any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800264 LOG(LS_INFO) << "Bad call (any) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800265 } else if (prev_any_bad && !any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800266 LOG(LS_INFO) << "Bad call (any) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800267 }
268
269 if (!prev_fps_bad && fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800270 LOG(LS_INFO) << "Bad call (fps) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800271 } else if (prev_fps_bad && !fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800272 LOG(LS_INFO) << "Bad call (fps) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800273 }
274
275 if (!prev_qp_bad && qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800276 LOG(LS_INFO) << "Bad call (qp) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800277 } else if (prev_qp_bad && !qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800278 LOG(LS_INFO) << "Bad call (qp) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800279 }
280
281 if (!prev_variance_bad && variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800282 LOG(LS_INFO) << "Bad call (variance) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800283 } else if (prev_variance_bad && !variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800284 LOG(LS_INFO) << "Bad call (variance) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800285 }
286
palmkvistc7e7e072017-01-09 07:23:33 -0800287 LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_)
288 << " fps: " << fps << " fps_bad: " << fps_bad << " qp: " << qp
289 << " qp_bad: " << qp_bad << " variance_bad: " << variance_bad
290 << " fps_variance: " << fps_variance;
palmkvist349092b2016-12-13 02:45:57 -0800291
292 last_sample_time_ = now;
293 qp_sample_.Reset();
palmkvista40672a2017-01-13 05:58:34 -0800294
295 if (fps_threshold_.IsHigh() || variance_threshold_.IsHigh() ||
296 qp_threshold_.IsHigh()) {
297 if (any_bad)
298 ++num_bad_states_;
299 ++num_certain_states_;
300 }
palmkvist349092b2016-12-13 02:45:57 -0800301}
302
sprang@webrtc.org09315702014-02-07 12:06:29 +0000303VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const {
Peter Boströmf2f82832015-05-01 13:00:41 +0200304 rtc::CritScope lock(&crit_);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000305 return stats_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000306}
307
pbosf42376c2015-08-28 07:35:32 -0700308void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) {
309 rtc::CritScope lock(&crit_);
310 stats_.current_payload_type = payload_type;
311}
312
Peter Boströmb7d9a972015-12-18 16:01:11 +0100313void ReceiveStatisticsProxy::OnDecoderImplementationName(
314 const char* implementation_name) {
315 rtc::CritScope lock(&crit_);
316 stats_.decoder_implementation_name = implementation_name;
317}
pbosf42376c2015-08-28 07:35:32 -0700318void ReceiveStatisticsProxy::OnIncomingRate(unsigned int framerate,
319 unsigned int bitrate_bps) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200320 rtc::CritScope lock(&crit_);
palmkvista40672a2017-01-13 05:58:34 -0800321 if (stats_.rtp_stats.first_packet_time_ms != -1)
322 QualitySample();
stefane525d6a2017-02-08 05:25:42 -0800323 stats_.network_frame_rate = framerate;
324 stats_.total_bitrate_bps = bitrate_bps;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000325}
326
stefane525d6a2017-02-08 05:25:42 -0800327void ReceiveStatisticsProxy::OnDecoderTiming(int decode_ms,
328 int max_decode_ms,
329 int current_delay_ms,
330 int target_delay_ms,
331 int jitter_buffer_ms,
332 int min_playout_delay_ms,
333 int render_delay_ms,
334 int64_t rtt_ms) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200335 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000336 stats_.decode_ms = decode_ms;
337 stats_.max_decode_ms = max_decode_ms;
338 stats_.current_delay_ms = current_delay_ms;
339 stats_.target_delay_ms = target_delay_ms;
340 stats_.jitter_buffer_ms = jitter_buffer_ms;
341 stats_.min_playout_delay_ms = min_playout_delay_ms;
342 stats_.render_delay_ms = render_delay_ms;
asapersson6718e972015-07-24 00:20:58 -0700343 decode_time_counter_.Add(decode_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700344 jitter_buffer_delay_counter_.Add(jitter_buffer_ms);
345 target_delay_counter_.Add(target_delay_ms);
346 current_delay_counter_.Add(current_delay_ms);
asaperssona1862882016-04-18 00:41:05 -0700347 // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time +
348 // render delay).
stefane525d6a2017-02-08 05:25:42 -0800349 delay_counter_.Add(target_delay_ms + rtt_ms / 2);
pbos@webrtc.org98c04b32014-12-18 13:12:52 +0000350}
351
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000352void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated(
353 uint32_t ssrc,
354 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200355 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000356 if (stats_.ssrc != ssrc)
357 return;
358 stats_.rtcp_packet_type_counts = packet_counter;
359}
360
sprang@webrtc.org09315702014-02-07 12:06:29 +0000361void ReceiveStatisticsProxy::StatisticsUpdated(
362 const webrtc::RtcpStatistics& statistics,
363 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200364 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700365 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000366 // receive stats from one of them.
367 if (stats_.ssrc != ssrc)
368 return;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000369 stats_.rtcp_stats = statistics;
Åsa Persson3c391cb2015-04-27 10:09:49 +0200370 report_block_stats_.Store(statistics, ssrc, 0);
asapersson0c43f772016-11-30 01:42:26 -0800371
372 if (first_report_block_time_ms_ == -1)
373 first_report_block_time_ms_ = clock_->TimeInMilliseconds();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000374}
375
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000376void ReceiveStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200377 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700378 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000379 // receive stats from one of them.
380 if (stats_.ssrc != ssrc)
381 return;
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000382 stats_.c_name = cname;
383}
384
sprang@webrtc.org09315702014-02-07 12:06:29 +0000385void ReceiveStatisticsProxy::DataCountersUpdated(
386 const webrtc::StreamDataCounters& counters,
387 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200388 rtc::CritScope lock(&crit_);
sprang0ab8e812016-02-24 01:35:40 -0800389 if (ssrc == stats_.ssrc) {
390 stats_.rtp_stats = counters;
391 } else {
392 auto it = rtx_stats_.find(ssrc);
393 if (it != rtx_stats_.end()) {
394 it->second = counters;
395 } else {
396 RTC_NOTREACHED() << "Unexpected stream ssrc: " << ssrc;
397 }
398 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000399}
400
sakalcc452e12017-02-09 04:53:45 -0800401void ReceiveStatisticsProxy::OnDecodedFrame(rtc::Optional<uint8_t> qp) {
sprang@webrtc.org09315702014-02-07 12:06:29 +0000402 uint64_t now = clock_->TimeInMilliseconds();
403
Peter Boströmf2f82832015-05-01 13:00:41 +0200404 rtc::CritScope lock(&crit_);
sakale5ba44e2016-10-26 07:09:24 -0700405 ++stats_.frames_decoded;
sakalcc452e12017-02-09 04:53:45 -0800406 if (qp) {
407 if (!stats_.qp_sum) {
408 if (stats_.frames_decoded != 1) {
409 LOG(LS_WARNING)
410 << "Frames decoded was not 1 when first qp value was received.";
411 stats_.frames_decoded = 1;
412 }
413 stats_.qp_sum = rtc::Optional<uint64_t>(0);
414 }
415 *stats_.qp_sum += *qp;
416 } else if (stats_.qp_sum) {
417 LOG(LS_WARNING)
418 << "QP sum was already set and no QP was given for a frame.";
419 stats_.qp_sum = rtc::Optional<uint64_t>();
420 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000421 decode_fps_estimator_.Update(1, now);
Erik Språng51e60302016-06-10 22:13:21 +0200422 stats_.decode_frame_rate = decode_fps_estimator_.Rate(now).value_or(0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000423}
424
asapersson1490f7a2016-09-23 02:09:46 -0700425void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) {
426 int width = frame.width();
427 int height = frame.height();
asaperssonf839dcc2015-10-08 00:41:59 -0700428 RTC_DCHECK_GT(width, 0);
429 RTC_DCHECK_GT(height, 0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000430 uint64_t now = clock_->TimeInMilliseconds();
431
Peter Boströmf2f82832015-05-01 13:00:41 +0200432 rtc::CritScope lock(&crit_);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000433 renders_fps_estimator_.Update(1, now);
Erik Språng51e60302016-06-10 22:13:21 +0200434 stats_.render_frame_rate = renders_fps_estimator_.Rate(now).value_or(0);
hbos50cfe1f2017-01-23 07:21:55 -0800435 ++stats_.frames_rendered;
asapersson2e5cfcd2016-08-11 08:41:18 -0700436 stats_.width = width;
437 stats_.height = height;
asaperssond89920b2015-07-22 06:52:00 -0700438 render_width_counter_.Add(width);
439 render_height_counter_.Add(height);
Tim Psiaki63046262015-09-14 10:38:08 -0700440 render_fps_tracker_.AddSamples(1);
asaperssonf839dcc2015-10-08 00:41:59 -0700441 render_pixel_tracker_.AddSamples(sqrt(width * height));
asapersson1490f7a2016-09-23 02:09:46 -0700442
443 if (frame.ntp_time_ms() > 0) {
444 int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms();
445 if (delay_ms >= 0)
446 e2e_delay_counter_.Add(delay_ms);
447 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000448}
449
asaperssonde9e5ff2016-11-02 07:14:03 -0700450void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t sync_offset_ms,
451 double estimated_freq_khz) {
asaperssonf8cdd182016-03-15 01:00:47 -0700452 rtc::CritScope lock(&crit_);
453 sync_offset_counter_.Add(std::abs(sync_offset_ms));
454 stats_.sync_offset_ms = sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700455
456 const double kMaxFreqKhz = 10000.0;
457 int offset_khz = kMaxFreqKhz;
458 // Should not be zero or negative. If so, report max.
459 if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0)
460 offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5);
461
462 freq_offset_counter_.Add(offset_khz);
asaperssonf8cdd182016-03-15 01:00:47 -0700463}
464
pbos@webrtc.org55707692014-12-19 15:45:03 +0000465void ReceiveStatisticsProxy::OnReceiveRatesUpdated(uint32_t bitRate,
466 uint32_t frameRate) {
467}
468
469void ReceiveStatisticsProxy::OnFrameCountsUpdated(
470 const FrameCounts& frame_counts) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200471 rtc::CritScope lock(&crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000472 stats_.frame_counts = frame_counts;
473}
474
pbos@webrtc.org55707692014-12-19 15:45:03 +0000475void ReceiveStatisticsProxy::OnDiscardedPacketsUpdated(int discarded_packets) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200476 rtc::CritScope lock(&crit_);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000477 stats_.discarded_packets = discarded_packets;
478}
479
asapersson86b01602015-10-20 23:55:26 -0700480void ReceiveStatisticsProxy::OnPreDecode(
481 const EncodedImage& encoded_image,
482 const CodecSpecificInfo* codec_specific_info) {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200483 if (!codec_specific_info || encoded_image.qp_ == -1) {
asapersson86b01602015-10-20 23:55:26 -0700484 return;
485 }
486 if (codec_specific_info->codecType == kVideoCodecVP8) {
487 qp_counters_.vp8.Add(encoded_image.qp_);
palmkvist349092b2016-12-13 02:45:57 -0800488 rtc::CritScope lock(&crit_);
489 qp_sample_.Add(encoded_image.qp_);
asapersson86b01602015-10-20 23:55:26 -0700490 }
491}
492
asaperssond89920b2015-07-22 06:52:00 -0700493void ReceiveStatisticsProxy::SampleCounter::Add(int sample) {
494 sum += sample;
495 ++num_samples;
496}
497
asapersson6966bd52017-01-03 00:44:06 -0800498int ReceiveStatisticsProxy::SampleCounter::Avg(
499 int64_t min_required_samples) const {
asaperssond89920b2015-07-22 06:52:00 -0700500 if (num_samples < min_required_samples || num_samples == 0)
501 return -1;
asapersson6966bd52017-01-03 00:44:06 -0800502 return static_cast<int>(sum / num_samples);
asaperssond89920b2015-07-22 06:52:00 -0700503}
504
palmkvist349092b2016-12-13 02:45:57 -0800505void ReceiveStatisticsProxy::SampleCounter::Reset() {
506 num_samples = 0;
507 sum = 0;
508}
509
sprang@webrtc.org09315702014-02-07 12:06:29 +0000510} // namespace webrtc