blob: 066b7ef68c48a9ecdaec0fe8a00fef2312b4a3f4 [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
17#include "webrtc/base/checks.h"
palmkvist349092b2016-12-13 02:45:57 -080018#include "webrtc/base/logging.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010019#include "webrtc/modules/video_coding/include/video_codec_interface.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),
asapersson0c43f772016-11-30 01:42:26 -080076 freq_offset_counter_(clock, nullptr, kFreqOffsetProcessIntervalMs),
philipela45102f2017-02-22 05:30:39 -080077 first_report_block_time_ms_(-1),
78 avg_rtt_ms_(0),
79 frame_window_accumulated_bytes_(0) {
Tommi733b5472016-06-10 17:58:01 +020080 stats_.ssrc = config_.rtp.remote_ssrc;
brandtr14742122017-01-27 04:53:07 -080081 // TODO(brandtr): Replace |rtx_stats_| with a single instance of
82 // StreamDataCounters.
83 if (config_.rtp.rtx_ssrc) {
84 rtx_stats_[config_.rtp.rtx_ssrc] = StreamDataCounters();
85 }
sprang@webrtc.org09315702014-02-07 12:06:29 +000086}
87
Åsa Persson3c391cb2015-04-27 10:09:49 +020088ReceiveStatisticsProxy::~ReceiveStatisticsProxy() {
89 UpdateHistograms();
90}
91
asaperssond89920b2015-07-22 06:52:00 -070092void ReceiveStatisticsProxy::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -070093 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -070094 "WebRTC.Video.ReceiveStreamLifetimeInSeconds",
95 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
96
asapersson0c43f772016-11-30 01:42:26 -080097 if (first_report_block_time_ms_ != -1 &&
98 ((clock_->TimeInMilliseconds() - first_report_block_time_ms_) / 1000) >=
99 metrics::kMinRunTimeInSeconds) {
100 int fraction_lost = report_block_stats_.FractionLostInPercent();
101 if (fraction_lost != -1) {
102 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedPacketsLostInPercent",
103 fraction_lost);
104 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200105 }
asapersson0c43f772016-11-30 01:42:26 -0800106
asapersson6718e972015-07-24 00:20:58 -0700107 const int kMinRequiredSamples = 200;
asaperssonf839dcc2015-10-08 00:41:59 -0700108 int samples = static_cast<int>(render_fps_tracker_.TotalSampleCount());
109 if (samples > kMinRequiredSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700110 RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.RenderFramesPerSecond",
111 round(render_fps_tracker_.ComputeTotalRate()));
112 RTC_HISTOGRAM_COUNTS_100000(
asapersson28ba9272016-01-25 05:58:23 -0800113 "WebRTC.Video.RenderSqrtPixelsPerSecond",
Tim Psiakiad13d2f2015-11-10 16:34:50 -0800114 round(render_pixel_tracker_.ComputeTotalRate()));
asaperssonf839dcc2015-10-08 00:41:59 -0700115 }
asaperssond89920b2015-07-22 06:52:00 -0700116 int width = render_width_counter_.Avg(kMinRequiredSamples);
117 int height = render_height_counter_.Avg(kMinRequiredSamples);
118 if (width != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700119 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.ReceivedWidthInPixels", width);
120 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.ReceivedHeightInPixels", height);
asaperssond89920b2015-07-22 06:52:00 -0700121 }
asaperssonf8cdd182016-03-15 01:00:47 -0700122 int sync_offset_ms = sync_offset_counter_.Avg(kMinRequiredSamples);
pbos35fdb2a2016-05-03 03:32:10 -0700123 if (sync_offset_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700124 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.AVSyncOffsetInMs", sync_offset_ms);
pbos35fdb2a2016-05-03 03:32:10 -0700125 }
asaperssonde9e5ff2016-11-02 07:14:03 -0700126 AggregatedStats freq_offset_stats = freq_offset_counter_.GetStats();
127 if (freq_offset_stats.num_samples > 0) {
128 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtpToNtpFreqOffsetInKhz",
129 freq_offset_stats.average);
asapersson43cb7162016-11-15 08:20:48 -0800130 LOG(LS_INFO) << "WebRTC.Video.RtpToNtpFreqOffsetInKhz, "
131 << freq_offset_stats.ToString();
asaperssonde9e5ff2016-11-02 07:14:03 -0700132 }
asaperssonf8cdd182016-03-15 01:00:47 -0700133
philipela45102f2017-02-22 05:30:39 -0800134 if (stats_.frame_counts.key_frames > 0 ||
135 stats_.frame_counts.delta_frames > 0) {
136 float num_key_frames = stats_.frame_counts.key_frames;
137 float num_total_frames =
138 stats_.frame_counts.key_frames + stats_.frame_counts.delta_frames;
139 int key_frames_permille =
140 (num_key_frames * 1000.0f / num_total_frames + 0.5f);
141 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesReceivedInPermille",
142 key_frames_permille);
143 }
144
asapersson86b01602015-10-20 23:55:26 -0700145 int qp = qp_counters_.vp8.Avg(kMinRequiredSamples);
146 if (qp != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700147 RTC_HISTOGRAM_COUNTS_200("WebRTC.Video.Decoded.Vp8.Qp", qp);
asapersson86b01602015-10-20 23:55:26 -0700148
asapersson6718e972015-07-24 00:20:58 -0700149 // TODO(asapersson): DecoderTiming() is call periodically (each 1000ms) and
150 // not per frame. Change decode time to include every frame.
151 const int kMinRequiredDecodeSamples = 5;
152 int decode_ms = decode_time_counter_.Avg(kMinRequiredDecodeSamples);
153 if (decode_ms != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700154 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", decode_ms);
asapersson13c433c2015-10-06 04:08:15 -0700155
philipela45102f2017-02-22 05:30:39 -0800156 int jb_delay_ms = jitter_buffer_delay_counter_.Avg(kMinRequiredDecodeSamples);
157 if (jb_delay_ms != -1) {
158 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
159 jb_delay_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700160 }
philipela45102f2017-02-22 05:30:39 -0800161
asapersson8688a4e2016-04-27 23:42:35 -0700162 int target_delay_ms = target_delay_counter_.Avg(kMinRequiredDecodeSamples);
163 if (target_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700164 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs", target_delay_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700165 }
166 int current_delay_ms = current_delay_counter_.Avg(kMinRequiredDecodeSamples);
167 if (current_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700168 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.CurrentDelayInMs",
169 current_delay_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700170 }
asapersson13c433c2015-10-06 04:08:15 -0700171 int delay_ms = delay_counter_.Avg(kMinRequiredDecodeSamples);
172 if (delay_ms != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700173 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", delay_ms);
sprang0ab8e812016-02-24 01:35:40 -0800174
asapersson1490f7a2016-09-23 02:09:46 -0700175 int e2e_delay_ms = e2e_delay_counter_.Avg(kMinRequiredSamples);
176 if (e2e_delay_ms != -1)
177 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.EndToEndDelayInMs", e2e_delay_ms);
178
sprang0ab8e812016-02-24 01:35:40 -0800179 StreamDataCounters rtp = stats_.rtp_stats;
180 StreamDataCounters rtx;
181 for (auto it : rtx_stats_)
182 rtx.Add(it.second);
183 StreamDataCounters rtp_rtx = rtp;
184 rtp_rtx.Add(rtx);
185 int64_t elapsed_sec =
186 rtp_rtx.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) / 1000;
187 if (elapsed_sec > metrics::kMinRunTimeInSeconds) {
asapersson1d02d3e2016-09-09 22:40:25 -0700188 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800189 "WebRTC.Video.BitrateReceivedInKbps",
190 static_cast<int>(rtp_rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
191 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700192 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800193 "WebRTC.Video.MediaBitrateReceivedInKbps",
194 static_cast<int>(rtp.MediaPayloadBytes() * 8 / elapsed_sec / 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700195 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800196 "WebRTC.Video.PaddingBitrateReceivedInKbps",
197 static_cast<int>(rtp_rtx.transmitted.padding_bytes * 8 / elapsed_sec /
198 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700199 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800200 "WebRTC.Video.RetransmittedBitrateReceivedInKbps",
201 static_cast<int>(rtp_rtx.retransmitted.TotalBytes() * 8 / elapsed_sec /
202 1000));
203 if (!rtx_stats_.empty()) {
asapersson1d02d3e2016-09-09 22:40:25 -0700204 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtxBitrateReceivedInKbps",
205 static_cast<int>(rtx.transmitted.TotalBytes() *
206 8 / elapsed_sec / 1000));
sprang0ab8e812016-02-24 01:35:40 -0800207 }
brandtrb5f2c3f2016-10-04 23:28:39 -0700208 if (config_.rtp.ulpfec.ulpfec_payload_type != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700209 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800210 "WebRTC.Video.FecBitrateReceivedInKbps",
211 static_cast<int>(rtp_rtx.fec.TotalBytes() * 8 / elapsed_sec / 1000));
212 }
sprang07fb9be2016-02-24 07:55:00 -0800213 const RtcpPacketTypeCounter& counters = stats_.rtcp_packet_type_counts;
asapersson1d02d3e2016-09-09 22:40:25 -0700214 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.NackPacketsSentPerMinute",
215 counters.nack_packets * 60 / elapsed_sec);
216 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.FirPacketsSentPerMinute",
217 counters.fir_packets * 60 / elapsed_sec);
218 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.PliPacketsSentPerMinute",
219 counters.pli_packets * 60 / elapsed_sec);
sprang07fb9be2016-02-24 07:55:00 -0800220 if (counters.nack_requests > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700221 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.UniqueNackRequestsSentInPercent",
222 counters.UniqueNackRequestsInPercent());
sprang07fb9be2016-02-24 07:55:00 -0800223 }
sprang0ab8e812016-02-24 01:35:40 -0800224 }
palmkvista40672a2017-01-13 05:58:34 -0800225
226 if (num_certain_states_ >= kBadCallMinRequiredSamples) {
227 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Any",
228 100 * num_bad_states_ / num_certain_states_);
229 }
230 rtc::Optional<double> fps_fraction =
231 fps_threshold_.FractionHigh(kBadCallMinRequiredSamples);
232 if (fps_fraction) {
233 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRate",
234 static_cast<int>(100 * (1 - *fps_fraction)));
235 }
236 rtc::Optional<double> variance_fraction =
237 variance_threshold_.FractionHigh(kBadCallMinRequiredSamples);
238 if (variance_fraction) {
239 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRateVariance",
240 static_cast<int>(100 * *variance_fraction));
241 }
242 rtc::Optional<double> qp_fraction =
243 qp_threshold_.FractionHigh(kBadCallMinRequiredSamples);
244 if (qp_fraction) {
245 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Qp",
246 static_cast<int>(100 * *qp_fraction));
247 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200248}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000249
palmkvist349092b2016-12-13 02:45:57 -0800250void ReceiveStatisticsProxy::QualitySample() {
251 int64_t now = clock_->TimeInMilliseconds();
252 if (last_sample_time_ + kMinSampleLengthMs > now)
253 return;
254
255 double fps =
256 render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_);
257 int qp = qp_sample_.Avg(1);
258
259 bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true);
260 bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false);
261 bool prev_variance_bad = variance_threshold_.IsHigh().value_or(false);
262 bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad;
263
264 fps_threshold_.AddMeasurement(static_cast<int>(fps));
265 if (qp != -1)
266 qp_threshold_.AddMeasurement(qp);
267 rtc::Optional<double> fps_variance_opt = fps_threshold_.CalculateVariance();
268 double fps_variance = fps_variance_opt.value_or(0);
269 if (fps_variance_opt) {
270 variance_threshold_.AddMeasurement(static_cast<int>(fps_variance));
271 }
272
273 bool fps_bad = !fps_threshold_.IsHigh().value_or(true);
274 bool qp_bad = qp_threshold_.IsHigh().value_or(false);
275 bool variance_bad = variance_threshold_.IsHigh().value_or(false);
276 bool any_bad = fps_bad || qp_bad || variance_bad;
277
278 if (!prev_any_bad && any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800279 LOG(LS_INFO) << "Bad call (any) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800280 } else if (prev_any_bad && !any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800281 LOG(LS_INFO) << "Bad call (any) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800282 }
283
284 if (!prev_fps_bad && fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800285 LOG(LS_INFO) << "Bad call (fps) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800286 } else if (prev_fps_bad && !fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800287 LOG(LS_INFO) << "Bad call (fps) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800288 }
289
290 if (!prev_qp_bad && qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800291 LOG(LS_INFO) << "Bad call (qp) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800292 } else if (prev_qp_bad && !qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800293 LOG(LS_INFO) << "Bad call (qp) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800294 }
295
296 if (!prev_variance_bad && variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800297 LOG(LS_INFO) << "Bad call (variance) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800298 } else if (prev_variance_bad && !variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800299 LOG(LS_INFO) << "Bad call (variance) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800300 }
301
palmkvistc7e7e072017-01-09 07:23:33 -0800302 LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_)
303 << " fps: " << fps << " fps_bad: " << fps_bad << " qp: " << qp
304 << " qp_bad: " << qp_bad << " variance_bad: " << variance_bad
305 << " fps_variance: " << fps_variance;
palmkvist349092b2016-12-13 02:45:57 -0800306
307 last_sample_time_ = now;
308 qp_sample_.Reset();
palmkvista40672a2017-01-13 05:58:34 -0800309
310 if (fps_threshold_.IsHigh() || variance_threshold_.IsHigh() ||
311 qp_threshold_.IsHigh()) {
312 if (any_bad)
313 ++num_bad_states_;
314 ++num_certain_states_;
315 }
palmkvist349092b2016-12-13 02:45:57 -0800316}
317
philipela45102f2017-02-22 05:30:39 -0800318void ReceiveStatisticsProxy::UpdateFrameAndBitrate(int64_t now_ms) const {
319 int64_t old_frames_ms = now_ms - kRateStatisticsWindowSizeMs;
320 while (!frame_window_.empty() &&
321 frame_window_.begin()->first < old_frames_ms) {
322 frame_window_accumulated_bytes_ -= frame_window_.begin()->second;
323 frame_window_.erase(frame_window_.begin());
324 }
325
326 size_t framerate =
327 (frame_window_.size() * 1000 + 500) / kRateStatisticsWindowSizeMs;
328 size_t bitrate_bps =
329 frame_window_accumulated_bytes_ * 8000 / kRateStatisticsWindowSizeMs;
330 stats_.network_frame_rate = static_cast<int>(framerate);
331 stats_.total_bitrate_bps = static_cast<int>(bitrate_bps);
332}
333
sprang@webrtc.org09315702014-02-07 12:06:29 +0000334VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const {
Peter Boströmf2f82832015-05-01 13:00:41 +0200335 rtc::CritScope lock(&crit_);
philipela45102f2017-02-22 05:30:39 -0800336 UpdateFrameAndBitrate(clock_->TimeInMilliseconds());
pbos@webrtc.org55707692014-12-19 15:45:03 +0000337 return stats_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000338}
339
pbosf42376c2015-08-28 07:35:32 -0700340void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) {
341 rtc::CritScope lock(&crit_);
342 stats_.current_payload_type = payload_type;
343}
344
Peter Boströmb7d9a972015-12-18 16:01:11 +0100345void ReceiveStatisticsProxy::OnDecoderImplementationName(
346 const char* implementation_name) {
347 rtc::CritScope lock(&crit_);
348 stats_.decoder_implementation_name = implementation_name;
349}
pbosf42376c2015-08-28 07:35:32 -0700350void ReceiveStatisticsProxy::OnIncomingRate(unsigned int framerate,
351 unsigned int bitrate_bps) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200352 rtc::CritScope lock(&crit_);
palmkvista40672a2017-01-13 05:58:34 -0800353 if (stats_.rtp_stats.first_packet_time_ms != -1)
354 QualitySample();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000355}
356
philipela45102f2017-02-22 05:30:39 -0800357void ReceiveStatisticsProxy::OnFrameBufferTimingsUpdated(
358 int decode_ms,
359 int max_decode_ms,
360 int current_delay_ms,
361 int target_delay_ms,
362 int jitter_buffer_ms,
363 int min_playout_delay_ms,
364 int render_delay_ms) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200365 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000366 stats_.decode_ms = decode_ms;
367 stats_.max_decode_ms = max_decode_ms;
368 stats_.current_delay_ms = current_delay_ms;
369 stats_.target_delay_ms = target_delay_ms;
370 stats_.jitter_buffer_ms = jitter_buffer_ms;
371 stats_.min_playout_delay_ms = min_playout_delay_ms;
372 stats_.render_delay_ms = render_delay_ms;
asapersson6718e972015-07-24 00:20:58 -0700373 decode_time_counter_.Add(decode_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700374 jitter_buffer_delay_counter_.Add(jitter_buffer_ms);
375 target_delay_counter_.Add(target_delay_ms);
376 current_delay_counter_.Add(current_delay_ms);
asaperssona1862882016-04-18 00:41:05 -0700377 // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time +
378 // render delay).
philipela45102f2017-02-22 05:30:39 -0800379 delay_counter_.Add(target_delay_ms + avg_rtt_ms_ / 2);
pbos@webrtc.org98c04b32014-12-18 13:12:52 +0000380}
381
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000382void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated(
383 uint32_t ssrc,
384 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200385 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000386 if (stats_.ssrc != ssrc)
387 return;
388 stats_.rtcp_packet_type_counts = packet_counter;
389}
390
sprang@webrtc.org09315702014-02-07 12:06:29 +0000391void ReceiveStatisticsProxy::StatisticsUpdated(
392 const webrtc::RtcpStatistics& statistics,
393 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200394 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700395 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000396 // receive stats from one of them.
397 if (stats_.ssrc != ssrc)
398 return;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000399 stats_.rtcp_stats = statistics;
Åsa Persson3c391cb2015-04-27 10:09:49 +0200400 report_block_stats_.Store(statistics, ssrc, 0);
asapersson0c43f772016-11-30 01:42:26 -0800401
402 if (first_report_block_time_ms_ == -1)
403 first_report_block_time_ms_ = clock_->TimeInMilliseconds();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000404}
405
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000406void ReceiveStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200407 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700408 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000409 // receive stats from one of them.
410 if (stats_.ssrc != ssrc)
411 return;
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000412 stats_.c_name = cname;
413}
414
sprang@webrtc.org09315702014-02-07 12:06:29 +0000415void ReceiveStatisticsProxy::DataCountersUpdated(
416 const webrtc::StreamDataCounters& counters,
417 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200418 rtc::CritScope lock(&crit_);
sprang0ab8e812016-02-24 01:35:40 -0800419 if (ssrc == stats_.ssrc) {
420 stats_.rtp_stats = counters;
421 } else {
422 auto it = rtx_stats_.find(ssrc);
423 if (it != rtx_stats_.end()) {
424 it->second = counters;
425 } else {
426 RTC_NOTREACHED() << "Unexpected stream ssrc: " << ssrc;
427 }
428 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000429}
430
sakalcc452e12017-02-09 04:53:45 -0800431void ReceiveStatisticsProxy::OnDecodedFrame(rtc::Optional<uint8_t> qp) {
sprang@webrtc.org09315702014-02-07 12:06:29 +0000432 uint64_t now = clock_->TimeInMilliseconds();
433
Peter Boströmf2f82832015-05-01 13:00:41 +0200434 rtc::CritScope lock(&crit_);
sakale5ba44e2016-10-26 07:09:24 -0700435 ++stats_.frames_decoded;
sakalcc452e12017-02-09 04:53:45 -0800436 if (qp) {
437 if (!stats_.qp_sum) {
438 if (stats_.frames_decoded != 1) {
439 LOG(LS_WARNING)
440 << "Frames decoded was not 1 when first qp value was received.";
441 stats_.frames_decoded = 1;
442 }
443 stats_.qp_sum = rtc::Optional<uint64_t>(0);
444 }
445 *stats_.qp_sum += *qp;
446 } else if (stats_.qp_sum) {
447 LOG(LS_WARNING)
448 << "QP sum was already set and no QP was given for a frame.";
449 stats_.qp_sum = rtc::Optional<uint64_t>();
450 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000451 decode_fps_estimator_.Update(1, now);
Erik Språng51e60302016-06-10 22:13:21 +0200452 stats_.decode_frame_rate = decode_fps_estimator_.Rate(now).value_or(0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000453}
454
asapersson1490f7a2016-09-23 02:09:46 -0700455void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) {
456 int width = frame.width();
457 int height = frame.height();
asaperssonf839dcc2015-10-08 00:41:59 -0700458 RTC_DCHECK_GT(width, 0);
459 RTC_DCHECK_GT(height, 0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000460 uint64_t now = clock_->TimeInMilliseconds();
461
Peter Boströmf2f82832015-05-01 13:00:41 +0200462 rtc::CritScope lock(&crit_);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000463 renders_fps_estimator_.Update(1, now);
Erik Språng51e60302016-06-10 22:13:21 +0200464 stats_.render_frame_rate = renders_fps_estimator_.Rate(now).value_or(0);
hbos50cfe1f2017-01-23 07:21:55 -0800465 ++stats_.frames_rendered;
asapersson2e5cfcd2016-08-11 08:41:18 -0700466 stats_.width = width;
467 stats_.height = height;
asaperssond89920b2015-07-22 06:52:00 -0700468 render_width_counter_.Add(width);
469 render_height_counter_.Add(height);
Tim Psiaki63046262015-09-14 10:38:08 -0700470 render_fps_tracker_.AddSamples(1);
asaperssonf839dcc2015-10-08 00:41:59 -0700471 render_pixel_tracker_.AddSamples(sqrt(width * height));
asapersson1490f7a2016-09-23 02:09:46 -0700472
473 if (frame.ntp_time_ms() > 0) {
474 int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms();
475 if (delay_ms >= 0)
476 e2e_delay_counter_.Add(delay_ms);
477 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000478}
479
asaperssonde9e5ff2016-11-02 07:14:03 -0700480void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t sync_offset_ms,
481 double estimated_freq_khz) {
asaperssonf8cdd182016-03-15 01:00:47 -0700482 rtc::CritScope lock(&crit_);
483 sync_offset_counter_.Add(std::abs(sync_offset_ms));
484 stats_.sync_offset_ms = sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700485
486 const double kMaxFreqKhz = 10000.0;
487 int offset_khz = kMaxFreqKhz;
488 // Should not be zero or negative. If so, report max.
489 if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0)
490 offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5);
491
492 freq_offset_counter_.Add(offset_khz);
asaperssonf8cdd182016-03-15 01:00:47 -0700493}
494
pbos@webrtc.org55707692014-12-19 15:45:03 +0000495void ReceiveStatisticsProxy::OnReceiveRatesUpdated(uint32_t bitRate,
496 uint32_t frameRate) {
497}
498
philipela45102f2017-02-22 05:30:39 -0800499void ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe,
500 size_t size_bytes) {
501 rtc::CritScope lock(&crit_);
502 if (is_keyframe)
503 ++stats_.frame_counts.key_frames;
504 else
505 ++stats_.frame_counts.delta_frames;
506
507 int64_t now_ms = clock_->TimeInMilliseconds();
508 frame_window_accumulated_bytes_ += size_bytes;
509 frame_window_.insert(std::make_pair(now_ms, size_bytes));
510 UpdateFrameAndBitrate(now_ms);
511}
512
pbos@webrtc.org55707692014-12-19 15:45:03 +0000513void ReceiveStatisticsProxy::OnFrameCountsUpdated(
514 const FrameCounts& frame_counts) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200515 rtc::CritScope lock(&crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000516 stats_.frame_counts = frame_counts;
517}
518
pbos@webrtc.org55707692014-12-19 15:45:03 +0000519void ReceiveStatisticsProxy::OnDiscardedPacketsUpdated(int discarded_packets) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200520 rtc::CritScope lock(&crit_);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000521 stats_.discarded_packets = discarded_packets;
522}
523
asapersson86b01602015-10-20 23:55:26 -0700524void ReceiveStatisticsProxy::OnPreDecode(
525 const EncodedImage& encoded_image,
526 const CodecSpecificInfo* codec_specific_info) {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200527 if (!codec_specific_info || encoded_image.qp_ == -1) {
asapersson86b01602015-10-20 23:55:26 -0700528 return;
529 }
530 if (codec_specific_info->codecType == kVideoCodecVP8) {
531 qp_counters_.vp8.Add(encoded_image.qp_);
palmkvist349092b2016-12-13 02:45:57 -0800532 rtc::CritScope lock(&crit_);
533 qp_sample_.Add(encoded_image.qp_);
asapersson86b01602015-10-20 23:55:26 -0700534 }
535}
536
asaperssond89920b2015-07-22 06:52:00 -0700537void ReceiveStatisticsProxy::SampleCounter::Add(int sample) {
538 sum += sample;
539 ++num_samples;
540}
541
asapersson6966bd52017-01-03 00:44:06 -0800542int ReceiveStatisticsProxy::SampleCounter::Avg(
543 int64_t min_required_samples) const {
asaperssond89920b2015-07-22 06:52:00 -0700544 if (num_samples < min_required_samples || num_samples == 0)
545 return -1;
asapersson6966bd52017-01-03 00:44:06 -0800546 return static_cast<int>(sum / num_samples);
asaperssond89920b2015-07-22 06:52:00 -0700547}
548
palmkvist349092b2016-12-13 02:45:57 -0800549void ReceiveStatisticsProxy::SampleCounter::Reset() {
550 num_samples = 0;
551 sum = 0;
552}
553
philipela45102f2017-02-22 05:30:39 -0800554void ReceiveStatisticsProxy::OnRttUpdate(int64_t avg_rtt_ms,
555 int64_t max_rtt_ms) {
556 rtc::CritScope lock(&crit_);
557 avg_rtt_ms_ = avg_rtt_ms;
558}
559
sprang@webrtc.org09315702014-02-07 12:06:29 +0000560} // namespace webrtc