blob: 590bd03b78d0e8d82b8614ddd518c9adbc2c7886 [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
asaperssona563c212017-03-02 08:25:46 -0800149 int decode_ms = decode_time_counter_.Avg(kMinRequiredSamples);
asapersson6718e972015-07-24 00:20:58 -0700150 if (decode_ms != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700151 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", decode_ms);
asapersson13c433c2015-10-06 04:08:15 -0700152
asaperssona563c212017-03-02 08:25:46 -0800153 int jb_delay_ms = jitter_buffer_delay_counter_.Avg(kMinRequiredSamples);
philipela45102f2017-02-22 05:30:39 -0800154 if (jb_delay_ms != -1) {
155 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
156 jb_delay_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700157 }
philipela45102f2017-02-22 05:30:39 -0800158
asaperssona563c212017-03-02 08:25:46 -0800159 int target_delay_ms = target_delay_counter_.Avg(kMinRequiredSamples);
asapersson8688a4e2016-04-27 23:42:35 -0700160 if (target_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700161 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs", target_delay_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700162 }
asaperssona563c212017-03-02 08:25:46 -0800163 int current_delay_ms = current_delay_counter_.Avg(kMinRequiredSamples);
asapersson8688a4e2016-04-27 23:42:35 -0700164 if (current_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700165 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.CurrentDelayInMs",
166 current_delay_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700167 }
asaperssona563c212017-03-02 08:25:46 -0800168 int delay_ms = delay_counter_.Avg(kMinRequiredSamples);
asapersson13c433c2015-10-06 04:08:15 -0700169 if (delay_ms != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700170 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", delay_ms);
sprang0ab8e812016-02-24 01:35:40 -0800171
asapersson1490f7a2016-09-23 02:09:46 -0700172 int e2e_delay_ms = e2e_delay_counter_.Avg(kMinRequiredSamples);
173 if (e2e_delay_ms != -1)
174 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.EndToEndDelayInMs", e2e_delay_ms);
175
sprang0ab8e812016-02-24 01:35:40 -0800176 StreamDataCounters rtp = stats_.rtp_stats;
177 StreamDataCounters rtx;
178 for (auto it : rtx_stats_)
179 rtx.Add(it.second);
180 StreamDataCounters rtp_rtx = rtp;
181 rtp_rtx.Add(rtx);
182 int64_t elapsed_sec =
183 rtp_rtx.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) / 1000;
184 if (elapsed_sec > metrics::kMinRunTimeInSeconds) {
asapersson1d02d3e2016-09-09 22:40:25 -0700185 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800186 "WebRTC.Video.BitrateReceivedInKbps",
187 static_cast<int>(rtp_rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
188 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700189 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800190 "WebRTC.Video.MediaBitrateReceivedInKbps",
191 static_cast<int>(rtp.MediaPayloadBytes() * 8 / elapsed_sec / 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700192 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800193 "WebRTC.Video.PaddingBitrateReceivedInKbps",
194 static_cast<int>(rtp_rtx.transmitted.padding_bytes * 8 / elapsed_sec /
195 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700196 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800197 "WebRTC.Video.RetransmittedBitrateReceivedInKbps",
198 static_cast<int>(rtp_rtx.retransmitted.TotalBytes() * 8 / elapsed_sec /
199 1000));
200 if (!rtx_stats_.empty()) {
asapersson1d02d3e2016-09-09 22:40:25 -0700201 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtxBitrateReceivedInKbps",
202 static_cast<int>(rtx.transmitted.TotalBytes() *
203 8 / elapsed_sec / 1000));
sprang0ab8e812016-02-24 01:35:40 -0800204 }
brandtrb5f2c3f2016-10-04 23:28:39 -0700205 if (config_.rtp.ulpfec.ulpfec_payload_type != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700206 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800207 "WebRTC.Video.FecBitrateReceivedInKbps",
208 static_cast<int>(rtp_rtx.fec.TotalBytes() * 8 / elapsed_sec / 1000));
209 }
sprang07fb9be2016-02-24 07:55:00 -0800210 const RtcpPacketTypeCounter& counters = stats_.rtcp_packet_type_counts;
asapersson1d02d3e2016-09-09 22:40:25 -0700211 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.NackPacketsSentPerMinute",
212 counters.nack_packets * 60 / elapsed_sec);
213 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.FirPacketsSentPerMinute",
214 counters.fir_packets * 60 / elapsed_sec);
215 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.PliPacketsSentPerMinute",
216 counters.pli_packets * 60 / elapsed_sec);
sprang07fb9be2016-02-24 07:55:00 -0800217 if (counters.nack_requests > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700218 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.UniqueNackRequestsSentInPercent",
219 counters.UniqueNackRequestsInPercent());
sprang07fb9be2016-02-24 07:55:00 -0800220 }
sprang0ab8e812016-02-24 01:35:40 -0800221 }
palmkvista40672a2017-01-13 05:58:34 -0800222
223 if (num_certain_states_ >= kBadCallMinRequiredSamples) {
224 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Any",
225 100 * num_bad_states_ / num_certain_states_);
226 }
227 rtc::Optional<double> fps_fraction =
228 fps_threshold_.FractionHigh(kBadCallMinRequiredSamples);
229 if (fps_fraction) {
230 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRate",
231 static_cast<int>(100 * (1 - *fps_fraction)));
232 }
233 rtc::Optional<double> variance_fraction =
234 variance_threshold_.FractionHigh(kBadCallMinRequiredSamples);
235 if (variance_fraction) {
236 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRateVariance",
237 static_cast<int>(100 * *variance_fraction));
238 }
239 rtc::Optional<double> qp_fraction =
240 qp_threshold_.FractionHigh(kBadCallMinRequiredSamples);
241 if (qp_fraction) {
242 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Qp",
243 static_cast<int>(100 * *qp_fraction));
244 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200245}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000246
palmkvist349092b2016-12-13 02:45:57 -0800247void ReceiveStatisticsProxy::QualitySample() {
248 int64_t now = clock_->TimeInMilliseconds();
249 if (last_sample_time_ + kMinSampleLengthMs > now)
250 return;
251
252 double fps =
253 render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_);
254 int qp = qp_sample_.Avg(1);
255
256 bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true);
257 bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false);
258 bool prev_variance_bad = variance_threshold_.IsHigh().value_or(false);
259 bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad;
260
261 fps_threshold_.AddMeasurement(static_cast<int>(fps));
262 if (qp != -1)
263 qp_threshold_.AddMeasurement(qp);
264 rtc::Optional<double> fps_variance_opt = fps_threshold_.CalculateVariance();
265 double fps_variance = fps_variance_opt.value_or(0);
266 if (fps_variance_opt) {
267 variance_threshold_.AddMeasurement(static_cast<int>(fps_variance));
268 }
269
270 bool fps_bad = !fps_threshold_.IsHigh().value_or(true);
271 bool qp_bad = qp_threshold_.IsHigh().value_or(false);
272 bool variance_bad = variance_threshold_.IsHigh().value_or(false);
273 bool any_bad = fps_bad || qp_bad || variance_bad;
274
275 if (!prev_any_bad && any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800276 LOG(LS_INFO) << "Bad call (any) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800277 } else if (prev_any_bad && !any_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800278 LOG(LS_INFO) << "Bad call (any) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800279 }
280
281 if (!prev_fps_bad && fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800282 LOG(LS_INFO) << "Bad call (fps) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800283 } else if (prev_fps_bad && !fps_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800284 LOG(LS_INFO) << "Bad call (fps) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800285 }
286
287 if (!prev_qp_bad && qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800288 LOG(LS_INFO) << "Bad call (qp) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800289 } else if (prev_qp_bad && !qp_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800290 LOG(LS_INFO) << "Bad call (qp) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800291 }
292
293 if (!prev_variance_bad && variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800294 LOG(LS_INFO) << "Bad call (variance) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800295 } else if (prev_variance_bad && !variance_bad) {
palmkvistc7e7e072017-01-09 07:23:33 -0800296 LOG(LS_INFO) << "Bad call (variance) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800297 }
298
palmkvistc7e7e072017-01-09 07:23:33 -0800299 LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_)
300 << " fps: " << fps << " fps_bad: " << fps_bad << " qp: " << qp
301 << " qp_bad: " << qp_bad << " variance_bad: " << variance_bad
302 << " fps_variance: " << fps_variance;
palmkvist349092b2016-12-13 02:45:57 -0800303
304 last_sample_time_ = now;
305 qp_sample_.Reset();
palmkvista40672a2017-01-13 05:58:34 -0800306
307 if (fps_threshold_.IsHigh() || variance_threshold_.IsHigh() ||
308 qp_threshold_.IsHigh()) {
309 if (any_bad)
310 ++num_bad_states_;
311 ++num_certain_states_;
312 }
palmkvist349092b2016-12-13 02:45:57 -0800313}
314
philipela45102f2017-02-22 05:30:39 -0800315void ReceiveStatisticsProxy::UpdateFrameAndBitrate(int64_t now_ms) const {
316 int64_t old_frames_ms = now_ms - kRateStatisticsWindowSizeMs;
317 while (!frame_window_.empty() &&
318 frame_window_.begin()->first < old_frames_ms) {
319 frame_window_accumulated_bytes_ -= frame_window_.begin()->second;
320 frame_window_.erase(frame_window_.begin());
321 }
322
323 size_t framerate =
324 (frame_window_.size() * 1000 + 500) / kRateStatisticsWindowSizeMs;
325 size_t bitrate_bps =
326 frame_window_accumulated_bytes_ * 8000 / kRateStatisticsWindowSizeMs;
327 stats_.network_frame_rate = static_cast<int>(framerate);
328 stats_.total_bitrate_bps = static_cast<int>(bitrate_bps);
329}
330
sprang@webrtc.org09315702014-02-07 12:06:29 +0000331VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const {
Peter Boströmf2f82832015-05-01 13:00:41 +0200332 rtc::CritScope lock(&crit_);
philipela45102f2017-02-22 05:30:39 -0800333 UpdateFrameAndBitrate(clock_->TimeInMilliseconds());
pbos@webrtc.org55707692014-12-19 15:45:03 +0000334 return stats_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000335}
336
pbosf42376c2015-08-28 07:35:32 -0700337void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) {
338 rtc::CritScope lock(&crit_);
339 stats_.current_payload_type = payload_type;
340}
341
Peter Boströmb7d9a972015-12-18 16:01:11 +0100342void ReceiveStatisticsProxy::OnDecoderImplementationName(
343 const char* implementation_name) {
344 rtc::CritScope lock(&crit_);
345 stats_.decoder_implementation_name = implementation_name;
346}
pbosf42376c2015-08-28 07:35:32 -0700347void ReceiveStatisticsProxy::OnIncomingRate(unsigned int framerate,
348 unsigned int bitrate_bps) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200349 rtc::CritScope lock(&crit_);
palmkvista40672a2017-01-13 05:58:34 -0800350 if (stats_.rtp_stats.first_packet_time_ms != -1)
351 QualitySample();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000352}
353
philipela45102f2017-02-22 05:30:39 -0800354void ReceiveStatisticsProxy::OnFrameBufferTimingsUpdated(
355 int decode_ms,
356 int max_decode_ms,
357 int current_delay_ms,
358 int target_delay_ms,
359 int jitter_buffer_ms,
360 int min_playout_delay_ms,
361 int render_delay_ms) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200362 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000363 stats_.decode_ms = decode_ms;
364 stats_.max_decode_ms = max_decode_ms;
365 stats_.current_delay_ms = current_delay_ms;
366 stats_.target_delay_ms = target_delay_ms;
367 stats_.jitter_buffer_ms = jitter_buffer_ms;
368 stats_.min_playout_delay_ms = min_playout_delay_ms;
369 stats_.render_delay_ms = render_delay_ms;
asapersson6718e972015-07-24 00:20:58 -0700370 decode_time_counter_.Add(decode_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700371 jitter_buffer_delay_counter_.Add(jitter_buffer_ms);
372 target_delay_counter_.Add(target_delay_ms);
373 current_delay_counter_.Add(current_delay_ms);
asaperssona1862882016-04-18 00:41:05 -0700374 // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time +
375 // render delay).
philipela45102f2017-02-22 05:30:39 -0800376 delay_counter_.Add(target_delay_ms + avg_rtt_ms_ / 2);
pbos@webrtc.org98c04b32014-12-18 13:12:52 +0000377}
378
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000379void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated(
380 uint32_t ssrc,
381 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200382 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000383 if (stats_.ssrc != ssrc)
384 return;
385 stats_.rtcp_packet_type_counts = packet_counter;
386}
387
sprang@webrtc.org09315702014-02-07 12:06:29 +0000388void ReceiveStatisticsProxy::StatisticsUpdated(
389 const webrtc::RtcpStatistics& statistics,
390 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200391 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700392 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000393 // receive stats from one of them.
394 if (stats_.ssrc != ssrc)
395 return;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000396 stats_.rtcp_stats = statistics;
Åsa Persson3c391cb2015-04-27 10:09:49 +0200397 report_block_stats_.Store(statistics, ssrc, 0);
asapersson0c43f772016-11-30 01:42:26 -0800398
399 if (first_report_block_time_ms_ == -1)
400 first_report_block_time_ms_ = clock_->TimeInMilliseconds();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000401}
402
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000403void ReceiveStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200404 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700405 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000406 // receive stats from one of them.
407 if (stats_.ssrc != ssrc)
408 return;
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000409 stats_.c_name = cname;
410}
411
sprang@webrtc.org09315702014-02-07 12:06:29 +0000412void ReceiveStatisticsProxy::DataCountersUpdated(
413 const webrtc::StreamDataCounters& counters,
414 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200415 rtc::CritScope lock(&crit_);
sprang0ab8e812016-02-24 01:35:40 -0800416 if (ssrc == stats_.ssrc) {
417 stats_.rtp_stats = counters;
418 } else {
419 auto it = rtx_stats_.find(ssrc);
420 if (it != rtx_stats_.end()) {
421 it->second = counters;
422 } else {
423 RTC_NOTREACHED() << "Unexpected stream ssrc: " << ssrc;
424 }
425 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000426}
427
sakalcc452e12017-02-09 04:53:45 -0800428void ReceiveStatisticsProxy::OnDecodedFrame(rtc::Optional<uint8_t> qp) {
sprang@webrtc.org09315702014-02-07 12:06:29 +0000429 uint64_t now = clock_->TimeInMilliseconds();
430
Peter Boströmf2f82832015-05-01 13:00:41 +0200431 rtc::CritScope lock(&crit_);
sakale5ba44e2016-10-26 07:09:24 -0700432 ++stats_.frames_decoded;
sakalcc452e12017-02-09 04:53:45 -0800433 if (qp) {
434 if (!stats_.qp_sum) {
435 if (stats_.frames_decoded != 1) {
436 LOG(LS_WARNING)
437 << "Frames decoded was not 1 when first qp value was received.";
438 stats_.frames_decoded = 1;
439 }
440 stats_.qp_sum = rtc::Optional<uint64_t>(0);
441 }
442 *stats_.qp_sum += *qp;
443 } else if (stats_.qp_sum) {
444 LOG(LS_WARNING)
445 << "QP sum was already set and no QP was given for a frame.";
446 stats_.qp_sum = rtc::Optional<uint64_t>();
447 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000448 decode_fps_estimator_.Update(1, now);
Erik Språng51e60302016-06-10 22:13:21 +0200449 stats_.decode_frame_rate = decode_fps_estimator_.Rate(now).value_or(0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000450}
451
asapersson1490f7a2016-09-23 02:09:46 -0700452void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) {
453 int width = frame.width();
454 int height = frame.height();
asaperssonf839dcc2015-10-08 00:41:59 -0700455 RTC_DCHECK_GT(width, 0);
456 RTC_DCHECK_GT(height, 0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000457 uint64_t now = clock_->TimeInMilliseconds();
458
Peter Boströmf2f82832015-05-01 13:00:41 +0200459 rtc::CritScope lock(&crit_);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000460 renders_fps_estimator_.Update(1, now);
Erik Språng51e60302016-06-10 22:13:21 +0200461 stats_.render_frame_rate = renders_fps_estimator_.Rate(now).value_or(0);
hbos50cfe1f2017-01-23 07:21:55 -0800462 ++stats_.frames_rendered;
asapersson2e5cfcd2016-08-11 08:41:18 -0700463 stats_.width = width;
464 stats_.height = height;
asaperssond89920b2015-07-22 06:52:00 -0700465 render_width_counter_.Add(width);
466 render_height_counter_.Add(height);
Tim Psiaki63046262015-09-14 10:38:08 -0700467 render_fps_tracker_.AddSamples(1);
asaperssonf839dcc2015-10-08 00:41:59 -0700468 render_pixel_tracker_.AddSamples(sqrt(width * height));
asapersson1490f7a2016-09-23 02:09:46 -0700469
470 if (frame.ntp_time_ms() > 0) {
471 int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms();
472 if (delay_ms >= 0)
473 e2e_delay_counter_.Add(delay_ms);
474 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000475}
476
asaperssonde9e5ff2016-11-02 07:14:03 -0700477void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t sync_offset_ms,
478 double estimated_freq_khz) {
asaperssonf8cdd182016-03-15 01:00:47 -0700479 rtc::CritScope lock(&crit_);
480 sync_offset_counter_.Add(std::abs(sync_offset_ms));
481 stats_.sync_offset_ms = sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700482
483 const double kMaxFreqKhz = 10000.0;
484 int offset_khz = kMaxFreqKhz;
485 // Should not be zero or negative. If so, report max.
486 if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0)
487 offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5);
488
489 freq_offset_counter_.Add(offset_khz);
asaperssonf8cdd182016-03-15 01:00:47 -0700490}
491
pbos@webrtc.org55707692014-12-19 15:45:03 +0000492void ReceiveStatisticsProxy::OnReceiveRatesUpdated(uint32_t bitRate,
493 uint32_t frameRate) {
494}
495
philipela45102f2017-02-22 05:30:39 -0800496void ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe,
497 size_t size_bytes) {
498 rtc::CritScope lock(&crit_);
499 if (is_keyframe)
500 ++stats_.frame_counts.key_frames;
501 else
502 ++stats_.frame_counts.delta_frames;
503
504 int64_t now_ms = clock_->TimeInMilliseconds();
505 frame_window_accumulated_bytes_ += size_bytes;
506 frame_window_.insert(std::make_pair(now_ms, size_bytes));
507 UpdateFrameAndBitrate(now_ms);
508}
509
pbos@webrtc.org55707692014-12-19 15:45:03 +0000510void ReceiveStatisticsProxy::OnFrameCountsUpdated(
511 const FrameCounts& frame_counts) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200512 rtc::CritScope lock(&crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000513 stats_.frame_counts = frame_counts;
514}
515
pbos@webrtc.org55707692014-12-19 15:45:03 +0000516void ReceiveStatisticsProxy::OnDiscardedPacketsUpdated(int discarded_packets) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200517 rtc::CritScope lock(&crit_);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000518 stats_.discarded_packets = discarded_packets;
519}
520
asapersson86b01602015-10-20 23:55:26 -0700521void ReceiveStatisticsProxy::OnPreDecode(
522 const EncodedImage& encoded_image,
523 const CodecSpecificInfo* codec_specific_info) {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200524 if (!codec_specific_info || encoded_image.qp_ == -1) {
asapersson86b01602015-10-20 23:55:26 -0700525 return;
526 }
527 if (codec_specific_info->codecType == kVideoCodecVP8) {
528 qp_counters_.vp8.Add(encoded_image.qp_);
palmkvist349092b2016-12-13 02:45:57 -0800529 rtc::CritScope lock(&crit_);
530 qp_sample_.Add(encoded_image.qp_);
asapersson86b01602015-10-20 23:55:26 -0700531 }
532}
533
asaperssond89920b2015-07-22 06:52:00 -0700534void ReceiveStatisticsProxy::SampleCounter::Add(int sample) {
535 sum += sample;
536 ++num_samples;
537}
538
asapersson6966bd52017-01-03 00:44:06 -0800539int ReceiveStatisticsProxy::SampleCounter::Avg(
540 int64_t min_required_samples) const {
asaperssond89920b2015-07-22 06:52:00 -0700541 if (num_samples < min_required_samples || num_samples == 0)
542 return -1;
asapersson6966bd52017-01-03 00:44:06 -0800543 return static_cast<int>(sum / num_samples);
asaperssond89920b2015-07-22 06:52:00 -0700544}
545
palmkvist349092b2016-12-13 02:45:57 -0800546void ReceiveStatisticsProxy::SampleCounter::Reset() {
547 num_samples = 0;
548 sum = 0;
549}
550
philipela45102f2017-02-22 05:30:39 -0800551void ReceiveStatisticsProxy::OnRttUpdate(int64_t avg_rtt_ms,
552 int64_t max_rtt_ms) {
553 rtc::CritScope lock(&crit_);
554 avg_rtt_ms_ = avg_rtt_ms;
555}
556
sprang@webrtc.org09315702014-02-07 12:06:29 +0000557} // namespace webrtc