blob: 7aed47064aefa91f36e73acfe9ad1ccc27fadf63 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "video/receive_statistics_proxy.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000012
philipela45102f2017-02-22 05:30:39 -080013#include <algorithm>
asaperssonf839dcc2015-10-08 00:41:59 -070014#include <cmath>
ilnik6d5b4d62017-08-30 03:32:14 -070015#include <sstream>
philipela45102f2017-02-22 05:30:39 -080016#include <utility>
asaperssonf839dcc2015-10-08 00:41:59 -070017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_coding/include/video_codec_interface.h"
19#include "rtc_base/checks.h"
20#include "rtc_base/logging.h"
Åsa Perssonb9b07ea2018-01-24 17:04:07 +010021#include "rtc_base/timeutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "system_wrappers/include/clock.h"
23#include "system_wrappers/include/metrics.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000024
25namespace webrtc {
asaperssonde9e5ff2016-11-02 07:14:03 -070026namespace {
27// Periodic time interval for processing samples for |freq_offset_counter_|.
28const int64_t kFreqOffsetProcessIntervalMs = 40000;
palmkvist349092b2016-12-13 02:45:57 -080029
30// Configuration for bad call detection.
palmkvista40672a2017-01-13 05:58:34 -080031const int kBadCallMinRequiredSamples = 10;
palmkvist349092b2016-12-13 02:45:57 -080032const int kMinSampleLengthMs = 990;
33const int kNumMeasurements = 10;
34const int kNumMeasurementsVariance = kNumMeasurements * 1.5;
35const float kBadFraction = 0.8f;
36// For fps:
37// Low means low enough to be bad, high means high enough to be good
38const int kLowFpsThreshold = 12;
39const int kHighFpsThreshold = 14;
40// For qp and fps variance:
41// Low means low enough to be good, high means high enough to be bad
42const int kLowQpThresholdVp8 = 60;
43const int kHighQpThresholdVp8 = 70;
44const int kLowVarianceThreshold = 1;
45const int kHighVarianceThreshold = 2;
philipela45102f2017-02-22 05:30:39 -080046
ilnika79cc282017-08-23 05:24:10 -070047// Some metrics are reported as a maximum over this period.
Ilya Nikolaevskiyb06b3582017-10-16 17:59:12 +020048// This should be synchronized with a typical getStats polling interval in
49// the clients.
50const int kMovingMaxWindowMs = 1000;
ilnika79cc282017-08-23 05:24:10 -070051
philipela45102f2017-02-22 05:30:39 -080052// How large window we use to calculate the framerate/bitrate.
53const int kRateStatisticsWindowSizeMs = 1000;
ilnik6d5b4d62017-08-30 03:32:14 -070054
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +020055// Some sane ballpark estimate for maximum common value of inter-frame delay.
56// Values below that will be stored explicitly in the array,
57// values above - in the map.
58const int kMaxCommonInterframeDelayMs = 500;
59
ilnik6d5b4d62017-08-30 03:32:14 -070060std::string UmaPrefixForContentType(VideoContentType content_type) {
61 std::stringstream ss;
62 ss << "WebRTC.Video";
63 if (videocontenttypehelpers::IsScreenshare(content_type)) {
64 ss << ".Screenshare";
65 }
66 return ss.str();
67}
68
69std::string UmaSuffixForContentType(VideoContentType content_type) {
70 std::stringstream ss;
71 int simulcast_id = videocontenttypehelpers::GetSimulcastId(content_type);
72 if (simulcast_id > 0) {
73 ss << ".S" << simulcast_id - 1;
74 }
75 int experiment_id = videocontenttypehelpers::GetExperimentId(content_type);
76 if (experiment_id > 0) {
77 ss << ".ExperimentGroup" << experiment_id - 1;
78 }
79 return ss.str();
80}
asaperssonde9e5ff2016-11-02 07:14:03 -070081} // namespace
sprang@webrtc.org09315702014-02-07 12:06:29 +000082
sprang0ab8e812016-02-24 01:35:40 -080083ReceiveStatisticsProxy::ReceiveStatisticsProxy(
Tommi733b5472016-06-10 17:58:01 +020084 const VideoReceiveStream::Config* config,
sprang0ab8e812016-02-24 01:35:40 -080085 Clock* clock)
pbos@webrtc.org55707692014-12-19 15:45:03 +000086 : clock_(clock),
Tommi733b5472016-06-10 17:58:01 +020087 config_(*config),
asapersson4374a092016-07-27 00:39:09 -070088 start_ms_(clock->TimeInMilliseconds()),
palmkvist349092b2016-12-13 02:45:57 -080089 last_sample_time_(clock->TimeInMilliseconds()),
90 fps_threshold_(kLowFpsThreshold,
91 kHighFpsThreshold,
92 kBadFraction,
93 kNumMeasurements),
94 qp_threshold_(kLowQpThresholdVp8,
95 kHighQpThresholdVp8,
96 kBadFraction,
97 kNumMeasurements),
98 variance_threshold_(kLowVarianceThreshold,
99 kHighVarianceThreshold,
100 kBadFraction,
101 kNumMeasurementsVariance),
palmkvista40672a2017-01-13 05:58:34 -0800102 num_bad_states_(0),
103 num_certain_states_(0),
sprang@webrtc.org09315702014-02-07 12:06:29 +0000104 // 1000ms window, scale 1000 for ms to s.
105 decode_fps_estimator_(1000, 1000),
Tim Psiaki63046262015-09-14 10:38:08 -0700106 renders_fps_estimator_(1000, 1000),
Honghai Zhang82d78622016-05-06 11:29:15 -0700107 render_fps_tracker_(100, 10u),
asaperssonde9e5ff2016-11-02 07:14:03 -0700108 render_pixel_tracker_(100, 10u),
asapersson0255acb2017-03-28 02:44:58 -0700109 total_byte_tracker_(100, 10u), // bucket_interval_ms, bucket_count
ilnika79cc282017-08-23 05:24:10 -0700110 interframe_delay_max_moving_(kMovingMaxWindowMs),
asapersson0c43f772016-11-30 01:42:26 -0800111 freq_offset_counter_(clock, nullptr, kFreqOffsetProcessIntervalMs),
philipela45102f2017-02-22 05:30:39 -0800112 first_report_block_time_ms_(-1),
ilnik00d802b2017-04-11 10:34:31 -0700113 avg_rtt_ms_(0),
ilnik75204c52017-09-04 03:35:40 -0700114 last_content_type_(VideoContentType::UNSPECIFIED),
115 timing_frame_info_counter_(kMovingMaxWindowMs) {
Tommi132e28e2018-02-24 17:57:33 +0100116 decode_thread_.DetachFromThread();
117 network_thread_.DetachFromThread();
Tommi733b5472016-06-10 17:58:01 +0200118 stats_.ssrc = config_.rtp.remote_ssrc;
brandtr14742122017-01-27 04:53:07 -0800119 // TODO(brandtr): Replace |rtx_stats_| with a single instance of
120 // StreamDataCounters.
121 if (config_.rtp.rtx_ssrc) {
122 rtx_stats_[config_.rtp.rtx_ssrc] = StreamDataCounters();
123 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000124}
125
Åsa Persson3c391cb2015-04-27 10:09:49 +0200126ReceiveStatisticsProxy::~ReceiveStatisticsProxy() {
Tommi132e28e2018-02-24 17:57:33 +0100127 RTC_DCHECK_RUN_ON(&main_thread_);
128 // In case you're reading this wondering "hmm... we're on the main thread but
129 // calling a method that needs to be called on the decoder thread...", then
130 // here's what's going on:
131 // - The decoder thread has been stopped and DecoderThreadStopped() has been
132 // called.
133 // - The decode_thread_ thread checker has been detached, and will now become
134 // attached to the current thread, which is OK since we're in the dtor.
Åsa Persson3c391cb2015-04-27 10:09:49 +0200135 UpdateHistograms();
136}
137
asaperssond89920b2015-07-22 06:52:00 -0700138void ReceiveStatisticsProxy::UpdateHistograms() {
Tommi132e28e2018-02-24 17:57:33 +0100139 RTC_DCHECK_RUN_ON(&decode_thread_);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100140 std::ostringstream logStream;
ilnik6d5b4d62017-08-30 03:32:14 -0700141 int stream_duration_sec = (clock_->TimeInMilliseconds() - start_ms_) / 1000;
142 if (stats_.frame_counts.key_frames > 0 ||
143 stats_.frame_counts.delta_frames > 0) {
144 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Video.ReceiveStreamLifetimeInSeconds",
145 stream_duration_sec);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100146 logStream << "WebRTC.Video.ReceiveStreamLifetimeInSeconds "
147 << stream_duration_sec << "\n";
ilnik6d5b4d62017-08-30 03:32:14 -0700148 }
asapersson4374a092016-07-27 00:39:09 -0700149
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100150 RTC_LOG(LS_INFO) << "Frames decoded " << stats_.frames_decoded;
151
152 if (num_unique_frames_) {
153 int num_dropped_frames = *num_unique_frames_ - stats_.frames_decoded;
154 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DroppedFrames.Receiver",
155 num_dropped_frames);
156 RTC_LOG(LS_INFO) << "WebRTC.Video.DroppedFrames.Receiver "
157 << num_dropped_frames;
158 }
159
asapersson0c43f772016-11-30 01:42:26 -0800160 if (first_report_block_time_ms_ != -1 &&
161 ((clock_->TimeInMilliseconds() - first_report_block_time_ms_) / 1000) >=
162 metrics::kMinRunTimeInSeconds) {
163 int fraction_lost = report_block_stats_.FractionLostInPercent();
164 if (fraction_lost != -1) {
165 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedPacketsLostInPercent",
166 fraction_lost);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100167 logStream << "WebRTC.Video.ReceivedPacketsLostInPercent "
168 << fraction_lost << "\n";
asapersson0c43f772016-11-30 01:42:26 -0800169 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200170 }
asapersson0c43f772016-11-30 01:42:26 -0800171
Åsa Perssonb9b07ea2018-01-24 17:04:07 +0100172 if (first_decoded_frame_time_ms_) {
173 const int64_t elapsed_ms =
174 (clock_->TimeInMilliseconds() - *first_decoded_frame_time_ms_);
175 if (elapsed_ms >=
176 metrics::kMinRunTimeInSeconds * rtc::kNumMillisecsPerSec) {
177 RTC_HISTOGRAM_COUNTS_100(
178 "WebRTC.Video.DecodedFramesPerSecond",
179 static_cast<int>((stats_.frames_decoded * 1000.0f / elapsed_ms) +
180 0.5f));
181 }
182 }
183
asapersson6718e972015-07-24 00:20:58 -0700184 const int kMinRequiredSamples = 200;
asaperssonf839dcc2015-10-08 00:41:59 -0700185 int samples = static_cast<int>(render_fps_tracker_.TotalSampleCount());
asapersson2077f2f2017-05-11 05:37:35 -0700186 if (samples >= kMinRequiredSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700187 RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.RenderFramesPerSecond",
188 round(render_fps_tracker_.ComputeTotalRate()));
189 RTC_HISTOGRAM_COUNTS_100000(
asapersson28ba9272016-01-25 05:58:23 -0800190 "WebRTC.Video.RenderSqrtPixelsPerSecond",
Tim Psiakiad13d2f2015-11-10 16:34:50 -0800191 round(render_pixel_tracker_.ComputeTotalRate()));
asaperssonf839dcc2015-10-08 00:41:59 -0700192 }
ilnik6d5b4d62017-08-30 03:32:14 -0700193
asaperssonf8cdd182016-03-15 01:00:47 -0700194 int sync_offset_ms = sync_offset_counter_.Avg(kMinRequiredSamples);
pbos35fdb2a2016-05-03 03:32:10 -0700195 if (sync_offset_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700196 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.AVSyncOffsetInMs", sync_offset_ms);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100197 logStream << "WebRTC.Video.AVSyncOffsetInMs " << sync_offset_ms << "\n";
pbos35fdb2a2016-05-03 03:32:10 -0700198 }
asaperssonde9e5ff2016-11-02 07:14:03 -0700199 AggregatedStats freq_offset_stats = freq_offset_counter_.GetStats();
200 if (freq_offset_stats.num_samples > 0) {
201 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtpToNtpFreqOffsetInKhz",
202 freq_offset_stats.average);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100203 logStream << "WebRTC.Video.RtpToNtpFreqOffsetInKhz "
204 << freq_offset_stats.ToString() << "\n";
asaperssonde9e5ff2016-11-02 07:14:03 -0700205 }
asaperssonf8cdd182016-03-15 01:00:47 -0700206
asaperssonb99baf82017-04-20 04:05:43 -0700207 int num_total_frames =
208 stats_.frame_counts.key_frames + stats_.frame_counts.delta_frames;
209 if (num_total_frames >= kMinRequiredSamples) {
210 int num_key_frames = stats_.frame_counts.key_frames;
philipela45102f2017-02-22 05:30:39 -0800211 int key_frames_permille =
asaperssonb99baf82017-04-20 04:05:43 -0700212 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
philipela45102f2017-02-22 05:30:39 -0800213 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesReceivedInPermille",
214 key_frames_permille);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100215 logStream << "WebRTC.Video.KeyFramesReceivedInPermille "
216 << key_frames_permille << "\n";
philipela45102f2017-02-22 05:30:39 -0800217 }
218
asapersson86b01602015-10-20 23:55:26 -0700219 int qp = qp_counters_.vp8.Avg(kMinRequiredSamples);
asapersson2077f2f2017-05-11 05:37:35 -0700220 if (qp != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700221 RTC_HISTOGRAM_COUNTS_200("WebRTC.Video.Decoded.Vp8.Qp", qp);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100222 logStream << "WebRTC.Video.Decoded.Vp8.Qp " << qp << "\n";
asapersson2077f2f2017-05-11 05:37:35 -0700223 }
asaperssona563c212017-03-02 08:25:46 -0800224 int decode_ms = decode_time_counter_.Avg(kMinRequiredSamples);
asapersson2077f2f2017-05-11 05:37:35 -0700225 if (decode_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700226 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", decode_ms);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100227 logStream << "WebRTC.Video.DecodeTimeInMs " << decode_ms << "\n";
asapersson2077f2f2017-05-11 05:37:35 -0700228 }
asaperssona563c212017-03-02 08:25:46 -0800229 int jb_delay_ms = jitter_buffer_delay_counter_.Avg(kMinRequiredSamples);
philipela45102f2017-02-22 05:30:39 -0800230 if (jb_delay_ms != -1) {
231 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
232 jb_delay_ms);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100233 logStream << "WebRTC.Video.JitterBufferDelayInMs " << jb_delay_ms << "\n";
asapersson8688a4e2016-04-27 23:42:35 -0700234 }
philipela45102f2017-02-22 05:30:39 -0800235
asaperssona563c212017-03-02 08:25:46 -0800236 int target_delay_ms = target_delay_counter_.Avg(kMinRequiredSamples);
asapersson8688a4e2016-04-27 23:42:35 -0700237 if (target_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700238 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs", target_delay_ms);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100239 logStream << "WebRTC.Video.TargetDelayInMs " << target_delay_ms << "\n";
asapersson8688a4e2016-04-27 23:42:35 -0700240 }
asaperssona563c212017-03-02 08:25:46 -0800241 int current_delay_ms = current_delay_counter_.Avg(kMinRequiredSamples);
asapersson8688a4e2016-04-27 23:42:35 -0700242 if (current_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700243 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.CurrentDelayInMs",
244 current_delay_ms);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100245 logStream << "WebRTC.Video.CurrentDelayInMs " << current_delay_ms << "\n";
asapersson8688a4e2016-04-27 23:42:35 -0700246 }
asaperssona563c212017-03-02 08:25:46 -0800247 int delay_ms = delay_counter_.Avg(kMinRequiredSamples);
asapersson13c433c2015-10-06 04:08:15 -0700248 if (delay_ms != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700249 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", delay_ms);
sprang0ab8e812016-02-24 01:35:40 -0800250
ilnik6d5b4d62017-08-30 03:32:14 -0700251 // Aggregate content_specific_stats_ by removing experiment or simulcast
252 // information;
253 std::map<VideoContentType, ContentSpecificStats> aggregated_stats;
254 for (auto it : content_specific_stats_) {
255 // Calculate simulcast specific metrics (".S0" ... ".S2" suffixes).
256 VideoContentType content_type = it.first;
257 if (videocontenttypehelpers::GetSimulcastId(content_type) > 0) {
258 // Aggregate on experiment id.
259 videocontenttypehelpers::SetExperimentId(&content_type, 0);
260 aggregated_stats[content_type].Add(it.second);
261 }
262 // Calculate experiment specific metrics (".ExperimentGroup[0-7]" suffixes).
263 content_type = it.first;
264 if (videocontenttypehelpers::GetExperimentId(content_type) > 0) {
265 // Aggregate on simulcast id.
266 videocontenttypehelpers::SetSimulcastId(&content_type, 0);
267 aggregated_stats[content_type].Add(it.second);
268 }
269 // Calculate aggregated metrics (no suffixes. Aggregated on everything).
270 content_type = it.first;
271 videocontenttypehelpers::SetSimulcastId(&content_type, 0);
272 videocontenttypehelpers::SetExperimentId(&content_type, 0);
273 aggregated_stats[content_type].Add(it.second);
ilnik00d802b2017-04-11 10:34:31 -0700274 }
275
ilnik6d5b4d62017-08-30 03:32:14 -0700276 for (auto it : aggregated_stats) {
277 // For the metric Foo we report the following slices:
278 // WebRTC.Video.Foo,
279 // WebRTC.Video.Screenshare.Foo,
280 // WebRTC.Video.Foo.S[0-3],
281 // WebRTC.Video.Foo.ExperimentGroup[0-7],
282 // WebRTC.Video.Screenshare.Foo.S[0-3],
283 // WebRTC.Video.Screenshare.Foo.ExperimentGroup[0-7].
284 auto content_type = it.first;
285 auto stats = it.second;
286 std::string uma_prefix = UmaPrefixForContentType(content_type);
287 std::string uma_suffix = UmaSuffixForContentType(content_type);
288 // Metrics can be sliced on either simulcast id or experiment id but not
289 // both.
290 RTC_DCHECK(videocontenttypehelpers::GetExperimentId(content_type) == 0 ||
291 videocontenttypehelpers::GetSimulcastId(content_type) == 0);
ilnik00d802b2017-04-11 10:34:31 -0700292
ilnik6d5b4d62017-08-30 03:32:14 -0700293 int e2e_delay_ms = stats.e2e_delay_counter.Avg(kMinRequiredSamples);
294 if (e2e_delay_ms != -1) {
295 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
296 uma_prefix + ".EndToEndDelayInMs" + uma_suffix, e2e_delay_ms);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100297 logStream << uma_prefix << ".EndToEndDelayInMs" << uma_suffix
298 << " " << e2e_delay_ms << "\n";
ilnik6d5b4d62017-08-30 03:32:14 -0700299 }
300 int e2e_delay_max_ms = stats.e2e_delay_counter.Max();
301 if (e2e_delay_max_ms != -1 && e2e_delay_ms != -1) {
302 RTC_HISTOGRAM_COUNTS_SPARSE_100000(
303 uma_prefix + ".EndToEndDelayMaxInMs" + uma_suffix, e2e_delay_max_ms);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100304 logStream << uma_prefix << ".EndToEndDelayMaxInMs" << uma_suffix
305 << " " << e2e_delay_max_ms << "\n";
ilnik6d5b4d62017-08-30 03:32:14 -0700306 }
307 int interframe_delay_ms =
308 stats.interframe_delay_counter.Avg(kMinRequiredSamples);
309 if (interframe_delay_ms != -1) {
310 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
311 uma_prefix + ".InterframeDelayInMs" + uma_suffix,
312 interframe_delay_ms);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100313 logStream << uma_prefix << ".InterframeDelayInMs" << uma_suffix
314 << " " << interframe_delay_ms << "\n";
ilnik6d5b4d62017-08-30 03:32:14 -0700315 }
316 int interframe_delay_max_ms = stats.interframe_delay_counter.Max();
317 if (interframe_delay_max_ms != -1 && interframe_delay_ms != -1) {
318 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
319 uma_prefix + ".InterframeDelayMaxInMs" + uma_suffix,
320 interframe_delay_max_ms);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100321 logStream << uma_prefix << ".InterframeDelayMaxInMs" << uma_suffix
322 << " " << interframe_delay_max_ms << "\n";
ilnik6d5b4d62017-08-30 03:32:14 -0700323 }
ilnik00d802b2017-04-11 10:34:31 -0700324
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200325 rtc::Optional<uint32_t> interframe_delay_95p_ms =
326 stats.interframe_delay_percentiles.GetPercentile(0.95f);
327 if (interframe_delay_95p_ms && interframe_delay_ms != -1) {
328 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
329 uma_prefix + ".InterframeDelay95PercentileInMs" + uma_suffix,
330 *interframe_delay_95p_ms);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100331 logStream << uma_prefix << ".InterframeDelay95PercentileInMs"
332 << uma_suffix << " " << *interframe_delay_95p_ms << "\n";
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200333 }
334
ilnik6d5b4d62017-08-30 03:32:14 -0700335 int width = stats.received_width.Avg(kMinRequiredSamples);
336 if (width != -1) {
337 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
338 uma_prefix + ".ReceivedWidthInPixels" + uma_suffix, width);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100339 logStream << uma_prefix << ".ReceivedWidthInPixels" << uma_suffix
340 << " " << width << "\n";
ilnik6d5b4d62017-08-30 03:32:14 -0700341 }
asapersson1490f7a2016-09-23 02:09:46 -0700342
ilnik6d5b4d62017-08-30 03:32:14 -0700343 int height = stats.received_height.Avg(kMinRequiredSamples);
344 if (height != -1) {
345 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
346 uma_prefix + ".ReceivedHeightInPixels" + uma_suffix, height);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100347 logStream << uma_prefix << ".ReceivedHeightInPixels" << uma_suffix
348 << " " << height << "\n";
ilnik6d5b4d62017-08-30 03:32:14 -0700349 }
ilnik4257ab22017-07-03 01:15:58 -0700350
ilnik6d5b4d62017-08-30 03:32:14 -0700351 if (content_type != VideoContentType::UNSPECIFIED) {
352 // Don't report these 3 metrics unsliced, as more precise variants
353 // are reported separately in this method.
354 float flow_duration_sec = stats.flow_duration_ms / 1000.0;
355 if (flow_duration_sec >= metrics::kMinRunTimeInSeconds) {
356 int media_bitrate_kbps = static_cast<int>(stats.total_media_bytes * 8 /
357 flow_duration_sec / 1000);
358 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
359 uma_prefix + ".MediaBitrateReceivedInKbps" + uma_suffix,
360 media_bitrate_kbps);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100361 logStream << uma_prefix << ".MediaBitrateReceivedInKbps"
362 << uma_suffix << " " << media_bitrate_kbps << "\n";
ilnik6d5b4d62017-08-30 03:32:14 -0700363 }
364
365 int num_total_frames =
366 stats.frame_counts.key_frames + stats.frame_counts.delta_frames;
367 if (num_total_frames >= kMinRequiredSamples) {
368 int num_key_frames = stats.frame_counts.key_frames;
369 int key_frames_permille =
370 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
371 RTC_HISTOGRAM_COUNTS_SPARSE_1000(
372 uma_prefix + ".KeyFramesReceivedInPermille" + uma_suffix,
373 key_frames_permille);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100374 logStream << uma_prefix << ".KeyFramesReceivedInPermille"
375 << uma_suffix << " " << key_frames_permille << "\n";
ilnik6d5b4d62017-08-30 03:32:14 -0700376 }
377
378 int qp = stats.qp_counter.Avg(kMinRequiredSamples);
379 if (qp != -1) {
380 RTC_HISTOGRAM_COUNTS_SPARSE_200(
381 uma_prefix + ".Decoded.Vp8.Qp" + uma_suffix, qp);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100382 logStream << uma_prefix << ".Decoded.Vp8.Qp" << uma_suffix << " "
383 << qp << "\n";
ilnik6d5b4d62017-08-30 03:32:14 -0700384 }
385 }
ilnik4257ab22017-07-03 01:15:58 -0700386 }
387
sprang0ab8e812016-02-24 01:35:40 -0800388 StreamDataCounters rtp = stats_.rtp_stats;
389 StreamDataCounters rtx;
390 for (auto it : rtx_stats_)
391 rtx.Add(it.second);
392 StreamDataCounters rtp_rtx = rtp;
393 rtp_rtx.Add(rtx);
394 int64_t elapsed_sec =
395 rtp_rtx.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) / 1000;
asapersson2077f2f2017-05-11 05:37:35 -0700396 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
asapersson1d02d3e2016-09-09 22:40:25 -0700397 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800398 "WebRTC.Video.BitrateReceivedInKbps",
399 static_cast<int>(rtp_rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
400 1000));
ilnik6d5b4d62017-08-30 03:32:14 -0700401 int media_bitrate_kbs =
402 static_cast<int>(rtp.MediaPayloadBytes() * 8 / elapsed_sec / 1000);
403 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.MediaBitrateReceivedInKbps",
404 media_bitrate_kbs);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100405 logStream << "WebRTC.Video.MediaBitrateReceivedInKbps "
406 << media_bitrate_kbs << "\n";
asapersson1d02d3e2016-09-09 22:40:25 -0700407 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800408 "WebRTC.Video.PaddingBitrateReceivedInKbps",
409 static_cast<int>(rtp_rtx.transmitted.padding_bytes * 8 / elapsed_sec /
410 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700411 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800412 "WebRTC.Video.RetransmittedBitrateReceivedInKbps",
413 static_cast<int>(rtp_rtx.retransmitted.TotalBytes() * 8 / elapsed_sec /
414 1000));
415 if (!rtx_stats_.empty()) {
asapersson1d02d3e2016-09-09 22:40:25 -0700416 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtxBitrateReceivedInKbps",
417 static_cast<int>(rtx.transmitted.TotalBytes() *
418 8 / elapsed_sec / 1000));
sprang0ab8e812016-02-24 01:35:40 -0800419 }
nisse3b3622f2017-09-26 02:49:21 -0700420 if (config_.rtp.ulpfec_payload_type != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700421 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800422 "WebRTC.Video.FecBitrateReceivedInKbps",
423 static_cast<int>(rtp_rtx.fec.TotalBytes() * 8 / elapsed_sec / 1000));
424 }
sprang07fb9be2016-02-24 07:55:00 -0800425 const RtcpPacketTypeCounter& counters = stats_.rtcp_packet_type_counts;
asapersson1d02d3e2016-09-09 22:40:25 -0700426 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.NackPacketsSentPerMinute",
427 counters.nack_packets * 60 / elapsed_sec);
428 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.FirPacketsSentPerMinute",
429 counters.fir_packets * 60 / elapsed_sec);
430 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.PliPacketsSentPerMinute",
431 counters.pli_packets * 60 / elapsed_sec);
sprang07fb9be2016-02-24 07:55:00 -0800432 if (counters.nack_requests > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700433 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.UniqueNackRequestsSentInPercent",
434 counters.UniqueNackRequestsInPercent());
sprang07fb9be2016-02-24 07:55:00 -0800435 }
sprang0ab8e812016-02-24 01:35:40 -0800436 }
palmkvista40672a2017-01-13 05:58:34 -0800437
438 if (num_certain_states_ >= kBadCallMinRequiredSamples) {
439 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Any",
440 100 * num_bad_states_ / num_certain_states_);
441 }
442 rtc::Optional<double> fps_fraction =
443 fps_threshold_.FractionHigh(kBadCallMinRequiredSamples);
444 if (fps_fraction) {
445 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRate",
446 static_cast<int>(100 * (1 - *fps_fraction)));
447 }
448 rtc::Optional<double> variance_fraction =
449 variance_threshold_.FractionHigh(kBadCallMinRequiredSamples);
450 if (variance_fraction) {
451 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRateVariance",
452 static_cast<int>(100 * *variance_fraction));
453 }
454 rtc::Optional<double> qp_fraction =
455 qp_threshold_.FractionHigh(kBadCallMinRequiredSamples);
456 if (qp_fraction) {
457 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Qp",
458 static_cast<int>(100 * *qp_fraction));
459 }
Jonas Olsson694a36f2018-02-16 13:09:41 +0100460 RTC_LOG(LS_INFO) << logStream.str();
Åsa Persson3c391cb2015-04-27 10:09:49 +0200461}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000462
palmkvist349092b2016-12-13 02:45:57 -0800463void ReceiveStatisticsProxy::QualitySample() {
Tommi132e28e2018-02-24 17:57:33 +0100464 RTC_DCHECK_RUN_ON(&network_thread_);
465
palmkvist349092b2016-12-13 02:45:57 -0800466 int64_t now = clock_->TimeInMilliseconds();
467 if (last_sample_time_ + kMinSampleLengthMs > now)
468 return;
469
470 double fps =
471 render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_);
472 int qp = qp_sample_.Avg(1);
473
474 bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true);
475 bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false);
476 bool prev_variance_bad = variance_threshold_.IsHigh().value_or(false);
477 bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad;
478
479 fps_threshold_.AddMeasurement(static_cast<int>(fps));
480 if (qp != -1)
481 qp_threshold_.AddMeasurement(qp);
482 rtc::Optional<double> fps_variance_opt = fps_threshold_.CalculateVariance();
483 double fps_variance = fps_variance_opt.value_or(0);
484 if (fps_variance_opt) {
485 variance_threshold_.AddMeasurement(static_cast<int>(fps_variance));
486 }
487
488 bool fps_bad = !fps_threshold_.IsHigh().value_or(true);
489 bool qp_bad = qp_threshold_.IsHigh().value_or(false);
490 bool variance_bad = variance_threshold_.IsHigh().value_or(false);
491 bool any_bad = fps_bad || qp_bad || variance_bad;
492
493 if (!prev_any_bad && any_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100494 RTC_LOG(LS_INFO) << "Bad call (any) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800495 } else if (prev_any_bad && !any_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100496 RTC_LOG(LS_INFO) << "Bad call (any) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800497 }
498
499 if (!prev_fps_bad && fps_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100500 RTC_LOG(LS_INFO) << "Bad call (fps) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800501 } else if (prev_fps_bad && !fps_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100502 RTC_LOG(LS_INFO) << "Bad call (fps) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800503 }
504
505 if (!prev_qp_bad && qp_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100506 RTC_LOG(LS_INFO) << "Bad call (qp) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800507 } else if (prev_qp_bad && !qp_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100508 RTC_LOG(LS_INFO) << "Bad call (qp) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800509 }
510
511 if (!prev_variance_bad && variance_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100512 RTC_LOG(LS_INFO) << "Bad call (variance) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800513 } else if (prev_variance_bad && !variance_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100514 RTC_LOG(LS_INFO) << "Bad call (variance) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800515 }
516
Mirko Bonadei675513b2017-11-09 11:09:25 +0100517 RTC_LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_)
518 << " fps: " << fps << " fps_bad: " << fps_bad
519 << " qp: " << qp << " qp_bad: " << qp_bad
520 << " variance_bad: " << variance_bad
521 << " fps_variance: " << fps_variance;
palmkvist349092b2016-12-13 02:45:57 -0800522
523 last_sample_time_ = now;
524 qp_sample_.Reset();
palmkvista40672a2017-01-13 05:58:34 -0800525
526 if (fps_threshold_.IsHigh() || variance_threshold_.IsHigh() ||
527 qp_threshold_.IsHigh()) {
528 if (any_bad)
529 ++num_bad_states_;
530 ++num_certain_states_;
531 }
palmkvist349092b2016-12-13 02:45:57 -0800532}
533
asapersson0255acb2017-03-28 02:44:58 -0700534void ReceiveStatisticsProxy::UpdateFramerate(int64_t now_ms) const {
philipela45102f2017-02-22 05:30:39 -0800535 int64_t old_frames_ms = now_ms - kRateStatisticsWindowSizeMs;
536 while (!frame_window_.empty() &&
537 frame_window_.begin()->first < old_frames_ms) {
philipela45102f2017-02-22 05:30:39 -0800538 frame_window_.erase(frame_window_.begin());
539 }
540
541 size_t framerate =
542 (frame_window_.size() * 1000 + 500) / kRateStatisticsWindowSizeMs;
philipela45102f2017-02-22 05:30:39 -0800543 stats_.network_frame_rate = static_cast<int>(framerate);
philipela45102f2017-02-22 05:30:39 -0800544}
545
sprang@webrtc.org09315702014-02-07 12:06:29 +0000546VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const {
Peter Boströmf2f82832015-05-01 13:00:41 +0200547 rtc::CritScope lock(&crit_);
sprang948b2752017-05-04 02:47:13 -0700548 // Get current frame rates here, as only updating them on new frames prevents
549 // us from ever correctly displaying frame rate of 0.
550 int64_t now_ms = clock_->TimeInMilliseconds();
551 UpdateFramerate(now_ms);
552 stats_.render_frame_rate = renders_fps_estimator_.Rate(now_ms).value_or(0);
553 stats_.decode_frame_rate = decode_fps_estimator_.Rate(now_ms).value_or(0);
asapersson0255acb2017-03-28 02:44:58 -0700554 stats_.total_bitrate_bps =
555 static_cast<int>(total_byte_tracker_.ComputeRate() * 8);
ilnika79cc282017-08-23 05:24:10 -0700556 stats_.interframe_delay_max_ms =
557 interframe_delay_max_moving_.Max(now_ms).value_or(-1);
ilnik75204c52017-09-04 03:35:40 -0700558 stats_.timing_frame_info = timing_frame_info_counter_.Max(now_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700559 stats_.content_type = last_content_type_;
pbos@webrtc.org55707692014-12-19 15:45:03 +0000560 return stats_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000561}
562
pbosf42376c2015-08-28 07:35:32 -0700563void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) {
564 rtc::CritScope lock(&crit_);
565 stats_.current_payload_type = payload_type;
566}
567
Peter Boströmb7d9a972015-12-18 16:01:11 +0100568void ReceiveStatisticsProxy::OnDecoderImplementationName(
569 const char* implementation_name) {
570 rtc::CritScope lock(&crit_);
571 stats_.decoder_implementation_name = implementation_name;
572}
pbosf42376c2015-08-28 07:35:32 -0700573void ReceiveStatisticsProxy::OnIncomingRate(unsigned int framerate,
574 unsigned int bitrate_bps) {
Tommi132e28e2018-02-24 17:57:33 +0100575 RTC_DCHECK_RUN_ON(&network_thread_);
Peter Boströmf2f82832015-05-01 13:00:41 +0200576 rtc::CritScope lock(&crit_);
palmkvista40672a2017-01-13 05:58:34 -0800577 if (stats_.rtp_stats.first_packet_time_ms != -1)
578 QualitySample();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000579}
580
philipela45102f2017-02-22 05:30:39 -0800581void ReceiveStatisticsProxy::OnFrameBufferTimingsUpdated(
582 int decode_ms,
583 int max_decode_ms,
584 int current_delay_ms,
585 int target_delay_ms,
586 int jitter_buffer_ms,
587 int min_playout_delay_ms,
588 int render_delay_ms) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200589 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000590 stats_.decode_ms = decode_ms;
591 stats_.max_decode_ms = max_decode_ms;
592 stats_.current_delay_ms = current_delay_ms;
593 stats_.target_delay_ms = target_delay_ms;
594 stats_.jitter_buffer_ms = jitter_buffer_ms;
595 stats_.min_playout_delay_ms = min_playout_delay_ms;
596 stats_.render_delay_ms = render_delay_ms;
asapersson6718e972015-07-24 00:20:58 -0700597 decode_time_counter_.Add(decode_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700598 jitter_buffer_delay_counter_.Add(jitter_buffer_ms);
599 target_delay_counter_.Add(target_delay_ms);
600 current_delay_counter_.Add(current_delay_ms);
asaperssona1862882016-04-18 00:41:05 -0700601 // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time +
602 // render delay).
philipela45102f2017-02-22 05:30:39 -0800603 delay_counter_.Add(target_delay_ms + avg_rtt_ms_ / 2);
pbos@webrtc.org98c04b32014-12-18 13:12:52 +0000604}
605
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100606void ReceiveStatisticsProxy::OnUniqueFramesCounted(int num_unique_frames) {
607 rtc::CritScope lock(&crit_);
608 num_unique_frames_.emplace(num_unique_frames);
609}
610
ilnik2edc6842017-07-06 03:06:50 -0700611void ReceiveStatisticsProxy::OnTimingFrameInfoUpdated(
612 const TimingFrameInfo& info) {
613 rtc::CritScope lock(&crit_);
Ilya Nikolaevskiy3f670e02017-10-10 11:18:49 +0200614 int64_t now_ms = clock_->TimeInMilliseconds();
ilnik75204c52017-09-04 03:35:40 -0700615 timing_frame_info_counter_.Add(info, now_ms);
ilnik2edc6842017-07-06 03:06:50 -0700616}
617
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000618void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated(
619 uint32_t ssrc,
620 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200621 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000622 if (stats_.ssrc != ssrc)
623 return;
624 stats_.rtcp_packet_type_counts = packet_counter;
625}
626
sprang@webrtc.org09315702014-02-07 12:06:29 +0000627void ReceiveStatisticsProxy::StatisticsUpdated(
628 const webrtc::RtcpStatistics& statistics,
629 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200630 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700631 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000632 // receive stats from one of them.
633 if (stats_.ssrc != ssrc)
634 return;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000635 stats_.rtcp_stats = statistics;
Åsa Persson3c391cb2015-04-27 10:09:49 +0200636 report_block_stats_.Store(statistics, ssrc, 0);
asapersson0c43f772016-11-30 01:42:26 -0800637
638 if (first_report_block_time_ms_ == -1)
639 first_report_block_time_ms_ = clock_->TimeInMilliseconds();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000640}
641
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000642void ReceiveStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200643 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700644 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000645 // receive stats from one of them.
646 if (stats_.ssrc != ssrc)
647 return;
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000648 stats_.c_name = cname;
649}
650
sprang@webrtc.org09315702014-02-07 12:06:29 +0000651void ReceiveStatisticsProxy::DataCountersUpdated(
652 const webrtc::StreamDataCounters& counters,
653 uint32_t ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700654 size_t last_total_bytes = 0;
655 size_t total_bytes = 0;
Peter Boströmf2f82832015-05-01 13:00:41 +0200656 rtc::CritScope lock(&crit_);
sprang0ab8e812016-02-24 01:35:40 -0800657 if (ssrc == stats_.ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700658 last_total_bytes = stats_.rtp_stats.transmitted.TotalBytes();
659 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800660 stats_.rtp_stats = counters;
661 } else {
662 auto it = rtx_stats_.find(ssrc);
663 if (it != rtx_stats_.end()) {
asapersson0255acb2017-03-28 02:44:58 -0700664 last_total_bytes = it->second.transmitted.TotalBytes();
665 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800666 it->second = counters;
667 } else {
668 RTC_NOTREACHED() << "Unexpected stream ssrc: " << ssrc;
669 }
670 }
asapersson0255acb2017-03-28 02:44:58 -0700671 if (total_bytes > last_total_bytes)
672 total_byte_tracker_.AddSamples(total_bytes - last_total_bytes);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000673}
674
ilnik00d802b2017-04-11 10:34:31 -0700675void ReceiveStatisticsProxy::OnDecodedFrame(rtc::Optional<uint8_t> qp,
676 VideoContentType content_type) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200677 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700678
Ilya Nikolaevskiy3f670e02017-10-10 11:18:49 +0200679 uint64_t now = clock_->TimeInMilliseconds();
680
ilnik6d5b4d62017-08-30 03:32:14 -0700681 ContentSpecificStats* content_specific_stats =
682 &content_specific_stats_[content_type];
sakale5ba44e2016-10-26 07:09:24 -0700683 ++stats_.frames_decoded;
sakalcc452e12017-02-09 04:53:45 -0800684 if (qp) {
685 if (!stats_.qp_sum) {
686 if (stats_.frames_decoded != 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100687 RTC_LOG(LS_WARNING)
sakalcc452e12017-02-09 04:53:45 -0800688 << "Frames decoded was not 1 when first qp value was received.";
689 stats_.frames_decoded = 1;
690 }
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100691 stats_.qp_sum = 0;
sakalcc452e12017-02-09 04:53:45 -0800692 }
693 *stats_.qp_sum += *qp;
ilnik6d5b4d62017-08-30 03:32:14 -0700694 content_specific_stats->qp_counter.Add(*qp);
sakalcc452e12017-02-09 04:53:45 -0800695 } else if (stats_.qp_sum) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100696 RTC_LOG(LS_WARNING)
sakalcc452e12017-02-09 04:53:45 -0800697 << "QP sum was already set and no QP was given for a frame.";
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100698 stats_.qp_sum = rtc::nullopt;
sakalcc452e12017-02-09 04:53:45 -0800699 }
ilnik00d802b2017-04-11 10:34:31 -0700700 last_content_type_ = content_type;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000701 decode_fps_estimator_.Update(1, now);
ilnik4257ab22017-07-03 01:15:58 -0700702 if (last_decoded_frame_time_ms_) {
703 int64_t interframe_delay_ms = now - *last_decoded_frame_time_ms_;
704 RTC_DCHECK_GE(interframe_delay_ms, 0);
ilnika79cc282017-08-23 05:24:10 -0700705 interframe_delay_max_moving_.Add(interframe_delay_ms, now);
ilnik6d5b4d62017-08-30 03:32:14 -0700706 content_specific_stats->interframe_delay_counter.Add(interframe_delay_ms);
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200707 content_specific_stats->interframe_delay_percentiles.Add(
708 interframe_delay_ms);
ilnik6d5b4d62017-08-30 03:32:14 -0700709 content_specific_stats->flow_duration_ms += interframe_delay_ms;
ilnik4257ab22017-07-03 01:15:58 -0700710 }
Åsa Perssonb9b07ea2018-01-24 17:04:07 +0100711 if (stats_.frames_decoded == 1)
712 first_decoded_frame_time_ms_.emplace(now);
ilnik4257ab22017-07-03 01:15:58 -0700713 last_decoded_frame_time_ms_.emplace(now);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000714}
715
asapersson1490f7a2016-09-23 02:09:46 -0700716void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) {
717 int width = frame.width();
718 int height = frame.height();
asaperssonf839dcc2015-10-08 00:41:59 -0700719 RTC_DCHECK_GT(width, 0);
720 RTC_DCHECK_GT(height, 0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000721 uint64_t now = clock_->TimeInMilliseconds();
Peter Boströmf2f82832015-05-01 13:00:41 +0200722 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700723 ContentSpecificStats* content_specific_stats =
724 &content_specific_stats_[last_content_type_];
sprang@webrtc.org09315702014-02-07 12:06:29 +0000725 renders_fps_estimator_.Update(1, now);
hbos50cfe1f2017-01-23 07:21:55 -0800726 ++stats_.frames_rendered;
asapersson2e5cfcd2016-08-11 08:41:18 -0700727 stats_.width = width;
728 stats_.height = height;
Tim Psiaki63046262015-09-14 10:38:08 -0700729 render_fps_tracker_.AddSamples(1);
asaperssonf839dcc2015-10-08 00:41:59 -0700730 render_pixel_tracker_.AddSamples(sqrt(width * height));
ilnik6d5b4d62017-08-30 03:32:14 -0700731 content_specific_stats->received_width.Add(width);
732 content_specific_stats->received_height.Add(height);
asapersson1490f7a2016-09-23 02:09:46 -0700733
734 if (frame.ntp_time_ms() > 0) {
735 int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms();
ilnik00d802b2017-04-11 10:34:31 -0700736 if (delay_ms >= 0) {
ilnik6d5b4d62017-08-30 03:32:14 -0700737 content_specific_stats->e2e_delay_counter.Add(delay_ms);
ilnik00d802b2017-04-11 10:34:31 -0700738 }
asapersson1490f7a2016-09-23 02:09:46 -0700739 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000740}
741
asaperssonde9e5ff2016-11-02 07:14:03 -0700742void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t sync_offset_ms,
743 double estimated_freq_khz) {
asaperssonf8cdd182016-03-15 01:00:47 -0700744 rtc::CritScope lock(&crit_);
745 sync_offset_counter_.Add(std::abs(sync_offset_ms));
746 stats_.sync_offset_ms = sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700747
748 const double kMaxFreqKhz = 10000.0;
749 int offset_khz = kMaxFreqKhz;
750 // Should not be zero or negative. If so, report max.
751 if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0)
752 offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5);
753
754 freq_offset_counter_.Add(offset_khz);
asaperssonf8cdd182016-03-15 01:00:47 -0700755}
756
pbos@webrtc.org55707692014-12-19 15:45:03 +0000757void ReceiveStatisticsProxy::OnReceiveRatesUpdated(uint32_t bitRate,
758 uint32_t frameRate) {
759}
760
philipela45102f2017-02-22 05:30:39 -0800761void ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe,
ilnik6d5b4d62017-08-30 03:32:14 -0700762 size_t size_bytes,
763 VideoContentType content_type) {
philipela45102f2017-02-22 05:30:39 -0800764 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700765 if (is_keyframe) {
philipela45102f2017-02-22 05:30:39 -0800766 ++stats_.frame_counts.key_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700767 } else {
philipela45102f2017-02-22 05:30:39 -0800768 ++stats_.frame_counts.delta_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700769 }
770
771 ContentSpecificStats* content_specific_stats =
772 &content_specific_stats_[content_type];
773
774 content_specific_stats->total_media_bytes += size_bytes;
775 if (is_keyframe) {
776 ++content_specific_stats->frame_counts.key_frames;
777 } else {
778 ++content_specific_stats->frame_counts.delta_frames;
779 }
philipela45102f2017-02-22 05:30:39 -0800780
781 int64_t now_ms = clock_->TimeInMilliseconds();
philipela45102f2017-02-22 05:30:39 -0800782 frame_window_.insert(std::make_pair(now_ms, size_bytes));
asapersson0255acb2017-03-28 02:44:58 -0700783 UpdateFramerate(now_ms);
philipela45102f2017-02-22 05:30:39 -0800784}
785
pbos@webrtc.org55707692014-12-19 15:45:03 +0000786void ReceiveStatisticsProxy::OnFrameCountsUpdated(
787 const FrameCounts& frame_counts) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200788 rtc::CritScope lock(&crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000789 stats_.frame_counts = frame_counts;
790}
791
pbos@webrtc.org55707692014-12-19 15:45:03 +0000792void ReceiveStatisticsProxy::OnDiscardedPacketsUpdated(int discarded_packets) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200793 rtc::CritScope lock(&crit_);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000794 stats_.discarded_packets = discarded_packets;
795}
796
asapersson86b01602015-10-20 23:55:26 -0700797void ReceiveStatisticsProxy::OnPreDecode(
798 const EncodedImage& encoded_image,
799 const CodecSpecificInfo* codec_specific_info) {
Tommi132e28e2018-02-24 17:57:33 +0100800 RTC_DCHECK_RUN_ON(&decode_thread_);
Peter Boström74f6e9e2016-04-04 17:56:10 +0200801 if (!codec_specific_info || encoded_image.qp_ == -1) {
asapersson86b01602015-10-20 23:55:26 -0700802 return;
803 }
804 if (codec_specific_info->codecType == kVideoCodecVP8) {
805 qp_counters_.vp8.Add(encoded_image.qp_);
palmkvist349092b2016-12-13 02:45:57 -0800806 rtc::CritScope lock(&crit_);
807 qp_sample_.Add(encoded_image.qp_);
asapersson86b01602015-10-20 23:55:26 -0700808 }
809}
810
sprang3e86e7e2017-08-22 09:23:28 -0700811void ReceiveStatisticsProxy::OnStreamInactive() {
812 // TODO(sprang): Figure out any other state that should be reset.
813
814 rtc::CritScope lock(&crit_);
815 // Don't report inter-frame delay if stream was paused.
816 last_decoded_frame_time_ms_.reset();
817}
818
asaperssond89920b2015-07-22 06:52:00 -0700819void ReceiveStatisticsProxy::SampleCounter::Add(int sample) {
820 sum += sample;
821 ++num_samples;
ilnik6d5b4d62017-08-30 03:32:14 -0700822 if (!max || sample > *max) {
823 max.emplace(sample);
824 }
825}
826
827void ReceiveStatisticsProxy::SampleCounter::Add(const SampleCounter& other) {
828 sum += other.sum;
829 num_samples += other.num_samples;
830 if (other.max && (!max || *max < *other.max))
831 max = other.max;
asaperssond89920b2015-07-22 06:52:00 -0700832}
833
asapersson6966bd52017-01-03 00:44:06 -0800834int ReceiveStatisticsProxy::SampleCounter::Avg(
835 int64_t min_required_samples) const {
asaperssond89920b2015-07-22 06:52:00 -0700836 if (num_samples < min_required_samples || num_samples == 0)
837 return -1;
asapersson6966bd52017-01-03 00:44:06 -0800838 return static_cast<int>(sum / num_samples);
asaperssond89920b2015-07-22 06:52:00 -0700839}
840
ilnik6d5b4d62017-08-30 03:32:14 -0700841int ReceiveStatisticsProxy::SampleCounter::Max() const {
842 return max.value_or(-1);
843}
844
palmkvist349092b2016-12-13 02:45:57 -0800845void ReceiveStatisticsProxy::SampleCounter::Reset() {
846 num_samples = 0;
847 sum = 0;
ilnik6d5b4d62017-08-30 03:32:14 -0700848 max.reset();
palmkvist349092b2016-12-13 02:45:57 -0800849}
850
philipela45102f2017-02-22 05:30:39 -0800851void ReceiveStatisticsProxy::OnRttUpdate(int64_t avg_rtt_ms,
852 int64_t max_rtt_ms) {
853 rtc::CritScope lock(&crit_);
854 avg_rtt_ms_ = avg_rtt_ms;
855}
856
Tommi132e28e2018-02-24 17:57:33 +0100857void ReceiveStatisticsProxy::DecoderThreadStarting() {
858 RTC_DCHECK_RUN_ON(&main_thread_);
859}
860
861void ReceiveStatisticsProxy::DecoderThreadStopped() {
862 RTC_DCHECK_RUN_ON(&main_thread_);
863 decode_thread_.DetachFromThread();
864}
865
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200866ReceiveStatisticsProxy::ContentSpecificStats::ContentSpecificStats()
867 : interframe_delay_percentiles(kMaxCommonInterframeDelayMs) {}
868
ilnik6d5b4d62017-08-30 03:32:14 -0700869void ReceiveStatisticsProxy::ContentSpecificStats::Add(
870 const ContentSpecificStats& other) {
871 e2e_delay_counter.Add(other.e2e_delay_counter);
872 interframe_delay_counter.Add(other.interframe_delay_counter);
873 flow_duration_ms += other.flow_duration_ms;
874 total_media_bytes += other.total_media_bytes;
875 received_height.Add(other.received_height);
876 received_width.Add(other.received_width);
877 qp_counter.Add(other.qp_counter);
878 frame_counts.key_frames += other.frame_counts.key_frames;
879 frame_counts.delta_frames += other.frame_counts.delta_frames;
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200880 interframe_delay_percentiles.Add(other.interframe_delay_percentiles);
ilnik6d5b4d62017-08-30 03:32:14 -0700881}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000882} // namespace webrtc