blob: 2fd5d9be7284264623f796fbad4d08dfef166913 [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>
philipela45102f2017-02-22 05:30:39 -080015#include <utility>
asaperssonf839dcc2015-10-08 00:41:59 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/video_coding/include/video_codec_interface.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
Tommifef05002018-02-27 13:51:08 +010020#include "rtc_base/strings/string_builder.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
Tommifef05002018-02-27 13:51:08 +010060const char* UmaPrefixForContentType(VideoContentType content_type) {
61 if (videocontenttypehelpers::IsScreenshare(content_type))
62 return "WebRTC.Video.Screenshare";
63 return "WebRTC.Video";
ilnik6d5b4d62017-08-30 03:32:14 -070064}
65
66std::string UmaSuffixForContentType(VideoContentType content_type) {
Karl Wiberg881f1682018-03-08 15:03:23 +010067 char ss_buf[1024];
68 rtc::SimpleStringBuilder ss(ss_buf);
ilnik6d5b4d62017-08-30 03:32:14 -070069 int simulcast_id = videocontenttypehelpers::GetSimulcastId(content_type);
70 if (simulcast_id > 0) {
71 ss << ".S" << simulcast_id - 1;
72 }
73 int experiment_id = videocontenttypehelpers::GetExperimentId(content_type);
74 if (experiment_id > 0) {
75 ss << ".ExperimentGroup" << experiment_id - 1;
76 }
77 return ss.str();
78}
Tommifef05002018-02-27 13:51:08 +010079
asaperssonde9e5ff2016-11-02 07:14:03 -070080} // namespace
sprang@webrtc.org09315702014-02-07 12:06:29 +000081
sprang0ab8e812016-02-24 01:35:40 -080082ReceiveStatisticsProxy::ReceiveStatisticsProxy(
Tommi733b5472016-06-10 17:58:01 +020083 const VideoReceiveStream::Config* config,
sprang0ab8e812016-02-24 01:35:40 -080084 Clock* clock)
pbos@webrtc.org55707692014-12-19 15:45:03 +000085 : clock_(clock),
Tommi733b5472016-06-10 17:58:01 +020086 config_(*config),
asapersson4374a092016-07-27 00:39:09 -070087 start_ms_(clock->TimeInMilliseconds()),
palmkvist349092b2016-12-13 02:45:57 -080088 last_sample_time_(clock->TimeInMilliseconds()),
89 fps_threshold_(kLowFpsThreshold,
90 kHighFpsThreshold,
91 kBadFraction,
92 kNumMeasurements),
93 qp_threshold_(kLowQpThresholdVp8,
94 kHighQpThresholdVp8,
95 kBadFraction,
96 kNumMeasurements),
97 variance_threshold_(kLowVarianceThreshold,
98 kHighVarianceThreshold,
99 kBadFraction,
100 kNumMeasurementsVariance),
palmkvista40672a2017-01-13 05:58:34 -0800101 num_bad_states_(0),
102 num_certain_states_(0),
sprang@webrtc.org09315702014-02-07 12:06:29 +0000103 // 1000ms window, scale 1000 for ms to s.
104 decode_fps_estimator_(1000, 1000),
Tim Psiaki63046262015-09-14 10:38:08 -0700105 renders_fps_estimator_(1000, 1000),
Honghai Zhang82d78622016-05-06 11:29:15 -0700106 render_fps_tracker_(100, 10u),
asaperssonde9e5ff2016-11-02 07:14:03 -0700107 render_pixel_tracker_(100, 10u),
asapersson0255acb2017-03-28 02:44:58 -0700108 total_byte_tracker_(100, 10u), // bucket_interval_ms, bucket_count
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200109 video_quality_observer_(
110 new VideoQualityObserver(VideoContentType::UNSPECIFIED)),
ilnika79cc282017-08-23 05:24:10 -0700111 interframe_delay_max_moving_(kMovingMaxWindowMs),
asapersson0c43f772016-11-30 01:42:26 -0800112 freq_offset_counter_(clock, nullptr, kFreqOffsetProcessIntervalMs),
philipela45102f2017-02-22 05:30:39 -0800113 first_report_block_time_ms_(-1),
ilnik00d802b2017-04-11 10:34:31 -0700114 avg_rtt_ms_(0),
ilnik75204c52017-09-04 03:35:40 -0700115 last_content_type_(VideoContentType::UNSPECIFIED),
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200116 last_codec_type_(kVideoCodecVP8),
ilnik75204c52017-09-04 03:35:40 -0700117 timing_frame_info_counter_(kMovingMaxWindowMs) {
Tommi132e28e2018-02-24 17:57:33 +0100118 decode_thread_.DetachFromThread();
119 network_thread_.DetachFromThread();
Tommi733b5472016-06-10 17:58:01 +0200120 stats_.ssrc = config_.rtp.remote_ssrc;
brandtr14742122017-01-27 04:53:07 -0800121 // TODO(brandtr): Replace |rtx_stats_| with a single instance of
122 // StreamDataCounters.
123 if (config_.rtp.rtx_ssrc) {
124 rtx_stats_[config_.rtp.rtx_ssrc] = StreamDataCounters();
125 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000126}
127
Åsa Persson3c391cb2015-04-27 10:09:49 +0200128ReceiveStatisticsProxy::~ReceiveStatisticsProxy() {
Tommi132e28e2018-02-24 17:57:33 +0100129 RTC_DCHECK_RUN_ON(&main_thread_);
130 // In case you're reading this wondering "hmm... we're on the main thread but
131 // calling a method that needs to be called on the decoder thread...", then
132 // here's what's going on:
133 // - The decoder thread has been stopped and DecoderThreadStopped() has been
134 // called.
135 // - The decode_thread_ thread checker has been detached, and will now become
136 // attached to the current thread, which is OK since we're in the dtor.
Åsa Persson3c391cb2015-04-27 10:09:49 +0200137 UpdateHistograms();
138}
139
asaperssond89920b2015-07-22 06:52:00 -0700140void ReceiveStatisticsProxy::UpdateHistograms() {
Tommi132e28e2018-02-24 17:57:33 +0100141 RTC_DCHECK_RUN_ON(&decode_thread_);
Karl Wiberg881f1682018-03-08 15:03:23 +0100142 char log_stream_buf[8 * 1024];
143 rtc::SimpleStringBuilder log_stream(log_stream_buf);
ilnik6d5b4d62017-08-30 03:32:14 -0700144 int stream_duration_sec = (clock_->TimeInMilliseconds() - start_ms_) / 1000;
145 if (stats_.frame_counts.key_frames > 0 ||
146 stats_.frame_counts.delta_frames > 0) {
147 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Video.ReceiveStreamLifetimeInSeconds",
148 stream_duration_sec);
Tommifef05002018-02-27 13:51:08 +0100149 log_stream << "WebRTC.Video.ReceiveStreamLifetimeInSeconds "
150 << stream_duration_sec << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700151 }
asapersson4374a092016-07-27 00:39:09 -0700152
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200153 log_stream << "Frames decoded " << stats_.frames_decoded << '\n';
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100154
155 if (num_unique_frames_) {
156 int num_dropped_frames = *num_unique_frames_ - stats_.frames_decoded;
157 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DroppedFrames.Receiver",
158 num_dropped_frames);
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200159 log_stream << "WebRTC.Video.DroppedFrames.Receiver " << num_dropped_frames
160 << '\n';
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100161 }
162
asapersson0c43f772016-11-30 01:42:26 -0800163 if (first_report_block_time_ms_ != -1 &&
164 ((clock_->TimeInMilliseconds() - first_report_block_time_ms_) / 1000) >=
165 metrics::kMinRunTimeInSeconds) {
166 int fraction_lost = report_block_stats_.FractionLostInPercent();
167 if (fraction_lost != -1) {
168 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedPacketsLostInPercent",
169 fraction_lost);
Tommifef05002018-02-27 13:51:08 +0100170 log_stream << "WebRTC.Video.ReceivedPacketsLostInPercent "
171 << fraction_lost << '\n';
asapersson0c43f772016-11-30 01:42:26 -0800172 }
Åsa Persson3c391cb2015-04-27 10:09:49 +0200173 }
asapersson0c43f772016-11-30 01:42:26 -0800174
Åsa Perssonb9b07ea2018-01-24 17:04:07 +0100175 if (first_decoded_frame_time_ms_) {
176 const int64_t elapsed_ms =
177 (clock_->TimeInMilliseconds() - *first_decoded_frame_time_ms_);
178 if (elapsed_ms >=
179 metrics::kMinRunTimeInSeconds * rtc::kNumMillisecsPerSec) {
180 RTC_HISTOGRAM_COUNTS_100(
181 "WebRTC.Video.DecodedFramesPerSecond",
182 static_cast<int>((stats_.frames_decoded * 1000.0f / elapsed_ms) +
183 0.5f));
184 }
185 }
186
asapersson6718e972015-07-24 00:20:58 -0700187 const int kMinRequiredSamples = 200;
asaperssonf839dcc2015-10-08 00:41:59 -0700188 int samples = static_cast<int>(render_fps_tracker_.TotalSampleCount());
asapersson2077f2f2017-05-11 05:37:35 -0700189 if (samples >= kMinRequiredSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700190 RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.RenderFramesPerSecond",
191 round(render_fps_tracker_.ComputeTotalRate()));
192 RTC_HISTOGRAM_COUNTS_100000(
asapersson28ba9272016-01-25 05:58:23 -0800193 "WebRTC.Video.RenderSqrtPixelsPerSecond",
Tim Psiakiad13d2f2015-11-10 16:34:50 -0800194 round(render_pixel_tracker_.ComputeTotalRate()));
asaperssonf839dcc2015-10-08 00:41:59 -0700195 }
ilnik6d5b4d62017-08-30 03:32:14 -0700196
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200197 rtc::Optional<int> sync_offset_ms =
198 sync_offset_counter_.Avg(kMinRequiredSamples);
199 if (sync_offset_ms) {
200 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.AVSyncOffsetInMs",
201 *sync_offset_ms);
202 log_stream << "WebRTC.Video.AVSyncOffsetInMs " << *sync_offset_ms << '\n';
pbos35fdb2a2016-05-03 03:32:10 -0700203 }
asaperssonde9e5ff2016-11-02 07:14:03 -0700204 AggregatedStats freq_offset_stats = freq_offset_counter_.GetStats();
205 if (freq_offset_stats.num_samples > 0) {
206 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtpToNtpFreqOffsetInKhz",
207 freq_offset_stats.average);
Tommifef05002018-02-27 13:51:08 +0100208 log_stream << "WebRTC.Video.RtpToNtpFreqOffsetInKhz "
209 << freq_offset_stats.ToString() << '\n';
asaperssonde9e5ff2016-11-02 07:14:03 -0700210 }
asaperssonf8cdd182016-03-15 01:00:47 -0700211
asaperssonb99baf82017-04-20 04:05:43 -0700212 int num_total_frames =
213 stats_.frame_counts.key_frames + stats_.frame_counts.delta_frames;
214 if (num_total_frames >= kMinRequiredSamples) {
215 int num_key_frames = stats_.frame_counts.key_frames;
philipela45102f2017-02-22 05:30:39 -0800216 int key_frames_permille =
asaperssonb99baf82017-04-20 04:05:43 -0700217 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
philipela45102f2017-02-22 05:30:39 -0800218 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesReceivedInPermille",
219 key_frames_permille);
Tommifef05002018-02-27 13:51:08 +0100220 log_stream << "WebRTC.Video.KeyFramesReceivedInPermille "
221 << key_frames_permille << '\n';
philipela45102f2017-02-22 05:30:39 -0800222 }
223
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200224 rtc::Optional<int> qp = qp_counters_.vp8.Avg(kMinRequiredSamples);
225 if (qp) {
226 RTC_HISTOGRAM_COUNTS_200("WebRTC.Video.Decoded.Vp8.Qp", *qp);
227 log_stream << "WebRTC.Video.Decoded.Vp8.Qp " << *qp << '\n';
asapersson2077f2f2017-05-11 05:37:35 -0700228 }
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200229 rtc::Optional<int> decode_ms = decode_time_counter_.Avg(kMinRequiredSamples);
230 if (decode_ms) {
231 RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", *decode_ms);
232 log_stream << "WebRTC.Video.DecodeTimeInMs " << *decode_ms << '\n';
asapersson2077f2f2017-05-11 05:37:35 -0700233 }
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200234 rtc::Optional<int> jb_delay_ms =
235 jitter_buffer_delay_counter_.Avg(kMinRequiredSamples);
236 if (jb_delay_ms) {
philipela45102f2017-02-22 05:30:39 -0800237 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs",
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200238 *jb_delay_ms);
239 log_stream << "WebRTC.Video.JitterBufferDelayInMs " << *jb_delay_ms << '\n';
asapersson8688a4e2016-04-27 23:42:35 -0700240 }
philipela45102f2017-02-22 05:30:39 -0800241
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200242 rtc::Optional<int> target_delay_ms =
243 target_delay_counter_.Avg(kMinRequiredSamples);
244 if (target_delay_ms) {
245 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs",
246 *target_delay_ms);
247 log_stream << "WebRTC.Video.TargetDelayInMs " << *target_delay_ms << '\n';
asapersson8688a4e2016-04-27 23:42:35 -0700248 }
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200249 rtc::Optional<int> current_delay_ms =
250 current_delay_counter_.Avg(kMinRequiredSamples);
251 if (current_delay_ms) {
asapersson1d02d3e2016-09-09 22:40:25 -0700252 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.CurrentDelayInMs",
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200253 *current_delay_ms);
254 log_stream << "WebRTC.Video.CurrentDelayInMs " << *current_delay_ms << '\n';
asapersson8688a4e2016-04-27 23:42:35 -0700255 }
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200256 rtc::Optional<int> delay_ms = delay_counter_.Avg(kMinRequiredSamples);
257 if (delay_ms)
258 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", *delay_ms);
sprang0ab8e812016-02-24 01:35:40 -0800259
ilnik6d5b4d62017-08-30 03:32:14 -0700260 // Aggregate content_specific_stats_ by removing experiment or simulcast
261 // information;
262 std::map<VideoContentType, ContentSpecificStats> aggregated_stats;
263 for (auto it : content_specific_stats_) {
264 // Calculate simulcast specific metrics (".S0" ... ".S2" suffixes).
265 VideoContentType content_type = it.first;
266 if (videocontenttypehelpers::GetSimulcastId(content_type) > 0) {
267 // Aggregate on experiment id.
268 videocontenttypehelpers::SetExperimentId(&content_type, 0);
269 aggregated_stats[content_type].Add(it.second);
270 }
271 // Calculate experiment specific metrics (".ExperimentGroup[0-7]" suffixes).
272 content_type = it.first;
273 if (videocontenttypehelpers::GetExperimentId(content_type) > 0) {
274 // Aggregate on simulcast id.
275 videocontenttypehelpers::SetSimulcastId(&content_type, 0);
276 aggregated_stats[content_type].Add(it.second);
277 }
278 // Calculate aggregated metrics (no suffixes. Aggregated on everything).
279 content_type = it.first;
280 videocontenttypehelpers::SetSimulcastId(&content_type, 0);
281 videocontenttypehelpers::SetExperimentId(&content_type, 0);
282 aggregated_stats[content_type].Add(it.second);
ilnik00d802b2017-04-11 10:34:31 -0700283 }
284
ilnik6d5b4d62017-08-30 03:32:14 -0700285 for (auto it : aggregated_stats) {
286 // For the metric Foo we report the following slices:
287 // WebRTC.Video.Foo,
288 // WebRTC.Video.Screenshare.Foo,
289 // WebRTC.Video.Foo.S[0-3],
290 // WebRTC.Video.Foo.ExperimentGroup[0-7],
291 // WebRTC.Video.Screenshare.Foo.S[0-3],
292 // WebRTC.Video.Screenshare.Foo.ExperimentGroup[0-7].
293 auto content_type = it.first;
294 auto stats = it.second;
295 std::string uma_prefix = UmaPrefixForContentType(content_type);
296 std::string uma_suffix = UmaSuffixForContentType(content_type);
297 // Metrics can be sliced on either simulcast id or experiment id but not
298 // both.
299 RTC_DCHECK(videocontenttypehelpers::GetExperimentId(content_type) == 0 ||
300 videocontenttypehelpers::GetSimulcastId(content_type) == 0);
ilnik00d802b2017-04-11 10:34:31 -0700301
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200302 rtc::Optional<int> e2e_delay_ms =
303 stats.e2e_delay_counter.Avg(kMinRequiredSamples);
304 if (e2e_delay_ms) {
ilnik6d5b4d62017-08-30 03:32:14 -0700305 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200306 uma_prefix + ".EndToEndDelayInMs" + uma_suffix, *e2e_delay_ms);
Tommifef05002018-02-27 13:51:08 +0100307 log_stream << uma_prefix << ".EndToEndDelayInMs" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200308 << *e2e_delay_ms << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700309 }
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200310 rtc::Optional<int> e2e_delay_max_ms = stats.e2e_delay_counter.Max();
311 if (e2e_delay_max_ms && e2e_delay_ms) {
ilnik6d5b4d62017-08-30 03:32:14 -0700312 RTC_HISTOGRAM_COUNTS_SPARSE_100000(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200313 uma_prefix + ".EndToEndDelayMaxInMs" + uma_suffix, *e2e_delay_max_ms);
Tommifef05002018-02-27 13:51:08 +0100314 log_stream << uma_prefix << ".EndToEndDelayMaxInMs" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200315 << *e2e_delay_max_ms << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700316 }
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200317 rtc::Optional<int> interframe_delay_ms =
ilnik6d5b4d62017-08-30 03:32:14 -0700318 stats.interframe_delay_counter.Avg(kMinRequiredSamples);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200319 if (interframe_delay_ms) {
ilnik6d5b4d62017-08-30 03:32:14 -0700320 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
321 uma_prefix + ".InterframeDelayInMs" + uma_suffix,
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200322 *interframe_delay_ms);
Tommifef05002018-02-27 13:51:08 +0100323 log_stream << uma_prefix << ".InterframeDelayInMs" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200324 << *interframe_delay_ms << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700325 }
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200326 rtc::Optional<int> interframe_delay_max_ms =
327 stats.interframe_delay_counter.Max();
328 if (interframe_delay_max_ms && interframe_delay_ms) {
ilnik6d5b4d62017-08-30 03:32:14 -0700329 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
330 uma_prefix + ".InterframeDelayMaxInMs" + uma_suffix,
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200331 *interframe_delay_max_ms);
Tommifef05002018-02-27 13:51:08 +0100332 log_stream << uma_prefix << ".InterframeDelayMaxInMs" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200333 << *interframe_delay_max_ms << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700334 }
ilnik00d802b2017-04-11 10:34:31 -0700335
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200336 rtc::Optional<uint32_t> interframe_delay_95p_ms =
337 stats.interframe_delay_percentiles.GetPercentile(0.95f);
338 if (interframe_delay_95p_ms && interframe_delay_ms != -1) {
339 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
340 uma_prefix + ".InterframeDelay95PercentileInMs" + uma_suffix,
341 *interframe_delay_95p_ms);
Tommifef05002018-02-27 13:51:08 +0100342 log_stream << uma_prefix << ".InterframeDelay95PercentileInMs"
343 << uma_suffix << " " << *interframe_delay_95p_ms << '\n';
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200344 }
345
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200346 rtc::Optional<int> width = stats.received_width.Avg(kMinRequiredSamples);
347 if (width) {
ilnik6d5b4d62017-08-30 03:32:14 -0700348 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200349 uma_prefix + ".ReceivedWidthInPixels" + uma_suffix, *width);
Tommifef05002018-02-27 13:51:08 +0100350 log_stream << uma_prefix << ".ReceivedWidthInPixels" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200351 << *width << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700352 }
asapersson1490f7a2016-09-23 02:09:46 -0700353
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200354 rtc::Optional<int> height = stats.received_height.Avg(kMinRequiredSamples);
355 if (height) {
ilnik6d5b4d62017-08-30 03:32:14 -0700356 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200357 uma_prefix + ".ReceivedHeightInPixels" + uma_suffix, *height);
Tommifef05002018-02-27 13:51:08 +0100358 log_stream << uma_prefix << ".ReceivedHeightInPixels" << uma_suffix << " "
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200359 << *height << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700360 }
ilnik4257ab22017-07-03 01:15:58 -0700361
ilnik6d5b4d62017-08-30 03:32:14 -0700362 if (content_type != VideoContentType::UNSPECIFIED) {
363 // Don't report these 3 metrics unsliced, as more precise variants
364 // are reported separately in this method.
365 float flow_duration_sec = stats.flow_duration_ms / 1000.0;
366 if (flow_duration_sec >= metrics::kMinRunTimeInSeconds) {
367 int media_bitrate_kbps = static_cast<int>(stats.total_media_bytes * 8 /
368 flow_duration_sec / 1000);
369 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
370 uma_prefix + ".MediaBitrateReceivedInKbps" + uma_suffix,
371 media_bitrate_kbps);
Tommifef05002018-02-27 13:51:08 +0100372 log_stream << uma_prefix << ".MediaBitrateReceivedInKbps" << uma_suffix
373 << " " << media_bitrate_kbps << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700374 }
375
376 int num_total_frames =
377 stats.frame_counts.key_frames + stats.frame_counts.delta_frames;
378 if (num_total_frames >= kMinRequiredSamples) {
379 int num_key_frames = stats.frame_counts.key_frames;
380 int key_frames_permille =
381 (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames;
382 RTC_HISTOGRAM_COUNTS_SPARSE_1000(
383 uma_prefix + ".KeyFramesReceivedInPermille" + uma_suffix,
384 key_frames_permille);
Tommifef05002018-02-27 13:51:08 +0100385 log_stream << uma_prefix << ".KeyFramesReceivedInPermille" << uma_suffix
386 << " " << key_frames_permille << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700387 }
388
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200389 rtc::Optional<int> qp = stats.qp_counter.Avg(kMinRequiredSamples);
390 if (qp) {
ilnik6d5b4d62017-08-30 03:32:14 -0700391 RTC_HISTOGRAM_COUNTS_SPARSE_200(
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200392 uma_prefix + ".Decoded.Vp8.Qp" + uma_suffix, *qp);
393 log_stream << uma_prefix << ".Decoded.Vp8.Qp" << uma_suffix << " "
394 << *qp << '\n';
ilnik6d5b4d62017-08-30 03:32:14 -0700395 }
396 }
ilnik4257ab22017-07-03 01:15:58 -0700397 }
398
sprang0ab8e812016-02-24 01:35:40 -0800399 StreamDataCounters rtp = stats_.rtp_stats;
400 StreamDataCounters rtx;
401 for (auto it : rtx_stats_)
402 rtx.Add(it.second);
403 StreamDataCounters rtp_rtx = rtp;
404 rtp_rtx.Add(rtx);
405 int64_t elapsed_sec =
406 rtp_rtx.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) / 1000;
asapersson2077f2f2017-05-11 05:37:35 -0700407 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
asapersson1d02d3e2016-09-09 22:40:25 -0700408 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800409 "WebRTC.Video.BitrateReceivedInKbps",
410 static_cast<int>(rtp_rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
411 1000));
ilnik6d5b4d62017-08-30 03:32:14 -0700412 int media_bitrate_kbs =
413 static_cast<int>(rtp.MediaPayloadBytes() * 8 / elapsed_sec / 1000);
414 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.MediaBitrateReceivedInKbps",
415 media_bitrate_kbs);
Tommifef05002018-02-27 13:51:08 +0100416 log_stream << "WebRTC.Video.MediaBitrateReceivedInKbps "
417 << media_bitrate_kbs << '\n';
asapersson1d02d3e2016-09-09 22:40:25 -0700418 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800419 "WebRTC.Video.PaddingBitrateReceivedInKbps",
420 static_cast<int>(rtp_rtx.transmitted.padding_bytes * 8 / elapsed_sec /
421 1000));
asapersson1d02d3e2016-09-09 22:40:25 -0700422 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800423 "WebRTC.Video.RetransmittedBitrateReceivedInKbps",
424 static_cast<int>(rtp_rtx.retransmitted.TotalBytes() * 8 / elapsed_sec /
425 1000));
426 if (!rtx_stats_.empty()) {
asapersson1d02d3e2016-09-09 22:40:25 -0700427 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtxBitrateReceivedInKbps",
428 static_cast<int>(rtx.transmitted.TotalBytes() *
429 8 / elapsed_sec / 1000));
sprang0ab8e812016-02-24 01:35:40 -0800430 }
nisse3b3622f2017-09-26 02:49:21 -0700431 if (config_.rtp.ulpfec_payload_type != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700432 RTC_HISTOGRAM_COUNTS_10000(
sprang0ab8e812016-02-24 01:35:40 -0800433 "WebRTC.Video.FecBitrateReceivedInKbps",
434 static_cast<int>(rtp_rtx.fec.TotalBytes() * 8 / elapsed_sec / 1000));
435 }
sprang07fb9be2016-02-24 07:55:00 -0800436 const RtcpPacketTypeCounter& counters = stats_.rtcp_packet_type_counts;
asapersson1d02d3e2016-09-09 22:40:25 -0700437 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.NackPacketsSentPerMinute",
438 counters.nack_packets * 60 / elapsed_sec);
439 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.FirPacketsSentPerMinute",
440 counters.fir_packets * 60 / elapsed_sec);
441 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.PliPacketsSentPerMinute",
442 counters.pli_packets * 60 / elapsed_sec);
sprang07fb9be2016-02-24 07:55:00 -0800443 if (counters.nack_requests > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700444 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.UniqueNackRequestsSentInPercent",
445 counters.UniqueNackRequestsInPercent());
sprang07fb9be2016-02-24 07:55:00 -0800446 }
sprang0ab8e812016-02-24 01:35:40 -0800447 }
palmkvista40672a2017-01-13 05:58:34 -0800448
449 if (num_certain_states_ >= kBadCallMinRequiredSamples) {
450 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Any",
451 100 * num_bad_states_ / num_certain_states_);
452 }
453 rtc::Optional<double> fps_fraction =
454 fps_threshold_.FractionHigh(kBadCallMinRequiredSamples);
455 if (fps_fraction) {
456 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRate",
457 static_cast<int>(100 * (1 - *fps_fraction)));
458 }
459 rtc::Optional<double> variance_fraction =
460 variance_threshold_.FractionHigh(kBadCallMinRequiredSamples);
461 if (variance_fraction) {
462 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRateVariance",
463 static_cast<int>(100 * *variance_fraction));
464 }
465 rtc::Optional<double> qp_fraction =
466 qp_threshold_.FractionHigh(kBadCallMinRequiredSamples);
467 if (qp_fraction) {
468 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Qp",
469 static_cast<int>(100 * *qp_fraction));
470 }
Tommifef05002018-02-27 13:51:08 +0100471
472 RTC_LOG(LS_INFO) << log_stream.str();
Åsa Persson3c391cb2015-04-27 10:09:49 +0200473}
sprang@webrtc.org09315702014-02-07 12:06:29 +0000474
palmkvist349092b2016-12-13 02:45:57 -0800475void ReceiveStatisticsProxy::QualitySample() {
Tommi132e28e2018-02-24 17:57:33 +0100476 RTC_DCHECK_RUN_ON(&network_thread_);
477
palmkvist349092b2016-12-13 02:45:57 -0800478 int64_t now = clock_->TimeInMilliseconds();
479 if (last_sample_time_ + kMinSampleLengthMs > now)
480 return;
481
482 double fps =
483 render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200484 rtc::Optional<int> qp = qp_sample_.Avg(1);
palmkvist349092b2016-12-13 02:45:57 -0800485
486 bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true);
487 bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false);
488 bool prev_variance_bad = variance_threshold_.IsHigh().value_or(false);
489 bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad;
490
491 fps_threshold_.AddMeasurement(static_cast<int>(fps));
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200492 if (qp)
493 qp_threshold_.AddMeasurement(*qp);
palmkvist349092b2016-12-13 02:45:57 -0800494 rtc::Optional<double> fps_variance_opt = fps_threshold_.CalculateVariance();
495 double fps_variance = fps_variance_opt.value_or(0);
496 if (fps_variance_opt) {
497 variance_threshold_.AddMeasurement(static_cast<int>(fps_variance));
498 }
499
500 bool fps_bad = !fps_threshold_.IsHigh().value_or(true);
501 bool qp_bad = qp_threshold_.IsHigh().value_or(false);
502 bool variance_bad = variance_threshold_.IsHigh().value_or(false);
503 bool any_bad = fps_bad || qp_bad || variance_bad;
504
505 if (!prev_any_bad && any_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100506 RTC_LOG(LS_INFO) << "Bad call (any) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800507 } else if (prev_any_bad && !any_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100508 RTC_LOG(LS_INFO) << "Bad call (any) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800509 }
510
511 if (!prev_fps_bad && fps_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100512 RTC_LOG(LS_INFO) << "Bad call (fps) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800513 } else if (prev_fps_bad && !fps_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100514 RTC_LOG(LS_INFO) << "Bad call (fps) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800515 }
516
517 if (!prev_qp_bad && qp_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100518 RTC_LOG(LS_INFO) << "Bad call (qp) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800519 } else if (prev_qp_bad && !qp_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100520 RTC_LOG(LS_INFO) << "Bad call (qp) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800521 }
522
523 if (!prev_variance_bad && variance_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100524 RTC_LOG(LS_INFO) << "Bad call (variance) start: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800525 } else if (prev_variance_bad && !variance_bad) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100526 RTC_LOG(LS_INFO) << "Bad call (variance) end: " << now;
palmkvist349092b2016-12-13 02:45:57 -0800527 }
528
Mirko Bonadei675513b2017-11-09 11:09:25 +0100529 RTC_LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_)
530 << " fps: " << fps << " fps_bad: " << fps_bad
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200531 << " qp: " << qp.value_or(-1) << " qp_bad: " << qp_bad
Mirko Bonadei675513b2017-11-09 11:09:25 +0100532 << " variance_bad: " << variance_bad
533 << " fps_variance: " << fps_variance;
palmkvist349092b2016-12-13 02:45:57 -0800534
535 last_sample_time_ = now;
536 qp_sample_.Reset();
palmkvista40672a2017-01-13 05:58:34 -0800537
538 if (fps_threshold_.IsHigh() || variance_threshold_.IsHigh() ||
539 qp_threshold_.IsHigh()) {
540 if (any_bad)
541 ++num_bad_states_;
542 ++num_certain_states_;
543 }
palmkvist349092b2016-12-13 02:45:57 -0800544}
545
asapersson0255acb2017-03-28 02:44:58 -0700546void ReceiveStatisticsProxy::UpdateFramerate(int64_t now_ms) const {
philipela45102f2017-02-22 05:30:39 -0800547 int64_t old_frames_ms = now_ms - kRateStatisticsWindowSizeMs;
548 while (!frame_window_.empty() &&
549 frame_window_.begin()->first < old_frames_ms) {
philipela45102f2017-02-22 05:30:39 -0800550 frame_window_.erase(frame_window_.begin());
551 }
552
553 size_t framerate =
554 (frame_window_.size() * 1000 + 500) / kRateStatisticsWindowSizeMs;
philipela45102f2017-02-22 05:30:39 -0800555 stats_.network_frame_rate = static_cast<int>(framerate);
philipela45102f2017-02-22 05:30:39 -0800556}
557
sprang@webrtc.org09315702014-02-07 12:06:29 +0000558VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const {
Peter Boströmf2f82832015-05-01 13:00:41 +0200559 rtc::CritScope lock(&crit_);
sprang948b2752017-05-04 02:47:13 -0700560 // Get current frame rates here, as only updating them on new frames prevents
561 // us from ever correctly displaying frame rate of 0.
562 int64_t now_ms = clock_->TimeInMilliseconds();
563 UpdateFramerate(now_ms);
564 stats_.render_frame_rate = renders_fps_estimator_.Rate(now_ms).value_or(0);
565 stats_.decode_frame_rate = decode_fps_estimator_.Rate(now_ms).value_or(0);
asapersson0255acb2017-03-28 02:44:58 -0700566 stats_.total_bitrate_bps =
567 static_cast<int>(total_byte_tracker_.ComputeRate() * 8);
ilnika79cc282017-08-23 05:24:10 -0700568 stats_.interframe_delay_max_ms =
569 interframe_delay_max_moving_.Max(now_ms).value_or(-1);
ilnik75204c52017-09-04 03:35:40 -0700570 stats_.timing_frame_info = timing_frame_info_counter_.Max(now_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700571 stats_.content_type = last_content_type_;
pbos@webrtc.org55707692014-12-19 15:45:03 +0000572 return stats_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000573}
574
pbosf42376c2015-08-28 07:35:32 -0700575void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) {
576 rtc::CritScope lock(&crit_);
577 stats_.current_payload_type = payload_type;
578}
579
Peter Boströmb7d9a972015-12-18 16:01:11 +0100580void ReceiveStatisticsProxy::OnDecoderImplementationName(
581 const char* implementation_name) {
582 rtc::CritScope lock(&crit_);
583 stats_.decoder_implementation_name = implementation_name;
584}
pbosf42376c2015-08-28 07:35:32 -0700585void ReceiveStatisticsProxy::OnIncomingRate(unsigned int framerate,
586 unsigned int bitrate_bps) {
Tommi132e28e2018-02-24 17:57:33 +0100587 RTC_DCHECK_RUN_ON(&network_thread_);
Peter Boströmf2f82832015-05-01 13:00:41 +0200588 rtc::CritScope lock(&crit_);
palmkvista40672a2017-01-13 05:58:34 -0800589 if (stats_.rtp_stats.first_packet_time_ms != -1)
590 QualitySample();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000591}
592
philipela45102f2017-02-22 05:30:39 -0800593void ReceiveStatisticsProxy::OnFrameBufferTimingsUpdated(
594 int decode_ms,
595 int max_decode_ms,
596 int current_delay_ms,
597 int target_delay_ms,
598 int jitter_buffer_ms,
599 int min_playout_delay_ms,
600 int render_delay_ms) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200601 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000602 stats_.decode_ms = decode_ms;
603 stats_.max_decode_ms = max_decode_ms;
604 stats_.current_delay_ms = current_delay_ms;
605 stats_.target_delay_ms = target_delay_ms;
606 stats_.jitter_buffer_ms = jitter_buffer_ms;
607 stats_.min_playout_delay_ms = min_playout_delay_ms;
608 stats_.render_delay_ms = render_delay_ms;
asapersson6718e972015-07-24 00:20:58 -0700609 decode_time_counter_.Add(decode_ms);
asapersson8688a4e2016-04-27 23:42:35 -0700610 jitter_buffer_delay_counter_.Add(jitter_buffer_ms);
611 target_delay_counter_.Add(target_delay_ms);
612 current_delay_counter_.Add(current_delay_ms);
asaperssona1862882016-04-18 00:41:05 -0700613 // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time +
614 // render delay).
philipela45102f2017-02-22 05:30:39 -0800615 delay_counter_.Add(target_delay_ms + avg_rtt_ms_ / 2);
pbos@webrtc.org98c04b32014-12-18 13:12:52 +0000616}
617
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100618void ReceiveStatisticsProxy::OnUniqueFramesCounted(int num_unique_frames) {
619 rtc::CritScope lock(&crit_);
620 num_unique_frames_.emplace(num_unique_frames);
621}
622
ilnik2edc6842017-07-06 03:06:50 -0700623void ReceiveStatisticsProxy::OnTimingFrameInfoUpdated(
624 const TimingFrameInfo& info) {
625 rtc::CritScope lock(&crit_);
Ilya Nikolaevskiy3f670e02017-10-10 11:18:49 +0200626 int64_t now_ms = clock_->TimeInMilliseconds();
ilnik75204c52017-09-04 03:35:40 -0700627 timing_frame_info_counter_.Add(info, now_ms);
ilnik2edc6842017-07-06 03:06:50 -0700628}
629
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000630void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated(
631 uint32_t ssrc,
632 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200633 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000634 if (stats_.ssrc != ssrc)
635 return;
636 stats_.rtcp_packet_type_counts = packet_counter;
637}
638
sprang@webrtc.org09315702014-02-07 12:06:29 +0000639void ReceiveStatisticsProxy::StatisticsUpdated(
640 const webrtc::RtcpStatistics& statistics,
641 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200642 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700643 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000644 // receive stats from one of them.
645 if (stats_.ssrc != ssrc)
646 return;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000647 stats_.rtcp_stats = statistics;
Åsa Persson3c391cb2015-04-27 10:09:49 +0200648 report_block_stats_.Store(statistics, ssrc, 0);
asapersson0c43f772016-11-30 01:42:26 -0800649
650 if (first_report_block_time_ms_ == -1)
651 first_report_block_time_ms_ = clock_->TimeInMilliseconds();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000652}
653
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000654void ReceiveStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200655 rtc::CritScope lock(&crit_);
henrikg91d6ede2015-09-17 00:24:34 -0700656 // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000657 // receive stats from one of them.
658 if (stats_.ssrc != ssrc)
659 return;
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000660 stats_.c_name = cname;
661}
662
sprang@webrtc.org09315702014-02-07 12:06:29 +0000663void ReceiveStatisticsProxy::DataCountersUpdated(
664 const webrtc::StreamDataCounters& counters,
665 uint32_t ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700666 size_t last_total_bytes = 0;
667 size_t total_bytes = 0;
Peter Boströmf2f82832015-05-01 13:00:41 +0200668 rtc::CritScope lock(&crit_);
sprang0ab8e812016-02-24 01:35:40 -0800669 if (ssrc == stats_.ssrc) {
asapersson0255acb2017-03-28 02:44:58 -0700670 last_total_bytes = stats_.rtp_stats.transmitted.TotalBytes();
671 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800672 stats_.rtp_stats = counters;
673 } else {
674 auto it = rtx_stats_.find(ssrc);
675 if (it != rtx_stats_.end()) {
asapersson0255acb2017-03-28 02:44:58 -0700676 last_total_bytes = it->second.transmitted.TotalBytes();
677 total_bytes = counters.transmitted.TotalBytes();
sprang0ab8e812016-02-24 01:35:40 -0800678 it->second = counters;
679 } else {
680 RTC_NOTREACHED() << "Unexpected stream ssrc: " << ssrc;
681 }
682 }
asapersson0255acb2017-03-28 02:44:58 -0700683 if (total_bytes > last_total_bytes)
684 total_byte_tracker_.AddSamples(total_bytes - last_total_bytes);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000685}
686
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200687// Deprecated. TODO(ilnik): remove once all depending projects are updated.
ilnik00d802b2017-04-11 10:34:31 -0700688void ReceiveStatisticsProxy::OnDecodedFrame(rtc::Optional<uint8_t> qp,
689 VideoContentType content_type) {
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200690 OnDecodedFrame(qp, 0, 0, content_type);
691}
692
693void ReceiveStatisticsProxy::OnDecodedFrame(rtc::Optional<uint8_t> qp,
694 int width,
695 int height,
696 VideoContentType content_type) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200697 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700698
Ilya Nikolaevskiy3f670e02017-10-10 11:18:49 +0200699 uint64_t now = clock_->TimeInMilliseconds();
700
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200701 if (videocontenttypehelpers::IsScreenshare(content_type) !=
702 videocontenttypehelpers::IsScreenshare(last_content_type_)) {
703 // Reset the quality observer if content type is switched. This will
704 // report stats for the previous part of the call.
705 video_quality_observer_.reset(new VideoQualityObserver(content_type));
706 }
707
708 video_quality_observer_->OnDecodedFrame(qp, width, height, now,
709 last_codec_type_);
710
ilnik6d5b4d62017-08-30 03:32:14 -0700711 ContentSpecificStats* content_specific_stats =
712 &content_specific_stats_[content_type];
sakale5ba44e2016-10-26 07:09:24 -0700713 ++stats_.frames_decoded;
sakalcc452e12017-02-09 04:53:45 -0800714 if (qp) {
715 if (!stats_.qp_sum) {
716 if (stats_.frames_decoded != 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100717 RTC_LOG(LS_WARNING)
sakalcc452e12017-02-09 04:53:45 -0800718 << "Frames decoded was not 1 when first qp value was received.";
719 stats_.frames_decoded = 1;
720 }
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100721 stats_.qp_sum = 0;
sakalcc452e12017-02-09 04:53:45 -0800722 }
723 *stats_.qp_sum += *qp;
ilnik6d5b4d62017-08-30 03:32:14 -0700724 content_specific_stats->qp_counter.Add(*qp);
sakalcc452e12017-02-09 04:53:45 -0800725 } else if (stats_.qp_sum) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100726 RTC_LOG(LS_WARNING)
sakalcc452e12017-02-09 04:53:45 -0800727 << "QP sum was already set and no QP was given for a frame.";
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100728 stats_.qp_sum = rtc::nullopt;
sakalcc452e12017-02-09 04:53:45 -0800729 }
ilnik00d802b2017-04-11 10:34:31 -0700730 last_content_type_ = content_type;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000731 decode_fps_estimator_.Update(1, now);
ilnik4257ab22017-07-03 01:15:58 -0700732 if (last_decoded_frame_time_ms_) {
733 int64_t interframe_delay_ms = now - *last_decoded_frame_time_ms_;
734 RTC_DCHECK_GE(interframe_delay_ms, 0);
ilnika79cc282017-08-23 05:24:10 -0700735 interframe_delay_max_moving_.Add(interframe_delay_ms, now);
ilnik6d5b4d62017-08-30 03:32:14 -0700736 content_specific_stats->interframe_delay_counter.Add(interframe_delay_ms);
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200737 content_specific_stats->interframe_delay_percentiles.Add(
738 interframe_delay_ms);
ilnik6d5b4d62017-08-30 03:32:14 -0700739 content_specific_stats->flow_duration_ms += interframe_delay_ms;
ilnik4257ab22017-07-03 01:15:58 -0700740 }
Åsa Perssonb9b07ea2018-01-24 17:04:07 +0100741 if (stats_.frames_decoded == 1)
742 first_decoded_frame_time_ms_.emplace(now);
ilnik4257ab22017-07-03 01:15:58 -0700743 last_decoded_frame_time_ms_.emplace(now);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000744}
745
asapersson1490f7a2016-09-23 02:09:46 -0700746void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) {
747 int width = frame.width();
748 int height = frame.height();
asaperssonf839dcc2015-10-08 00:41:59 -0700749 RTC_DCHECK_GT(width, 0);
750 RTC_DCHECK_GT(height, 0);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000751 uint64_t now = clock_->TimeInMilliseconds();
Peter Boströmf2f82832015-05-01 13:00:41 +0200752 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700753 ContentSpecificStats* content_specific_stats =
754 &content_specific_stats_[last_content_type_];
sprang@webrtc.org09315702014-02-07 12:06:29 +0000755 renders_fps_estimator_.Update(1, now);
hbos50cfe1f2017-01-23 07:21:55 -0800756 ++stats_.frames_rendered;
asapersson2e5cfcd2016-08-11 08:41:18 -0700757 stats_.width = width;
758 stats_.height = height;
Tim Psiaki63046262015-09-14 10:38:08 -0700759 render_fps_tracker_.AddSamples(1);
asaperssonf839dcc2015-10-08 00:41:59 -0700760 render_pixel_tracker_.AddSamples(sqrt(width * height));
ilnik6d5b4d62017-08-30 03:32:14 -0700761 content_specific_stats->received_width.Add(width);
762 content_specific_stats->received_height.Add(height);
asapersson1490f7a2016-09-23 02:09:46 -0700763
764 if (frame.ntp_time_ms() > 0) {
765 int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms();
ilnik00d802b2017-04-11 10:34:31 -0700766 if (delay_ms >= 0) {
ilnik6d5b4d62017-08-30 03:32:14 -0700767 content_specific_stats->e2e_delay_counter.Add(delay_ms);
ilnik00d802b2017-04-11 10:34:31 -0700768 }
asapersson1490f7a2016-09-23 02:09:46 -0700769 }
sprang@webrtc.org09315702014-02-07 12:06:29 +0000770}
771
asaperssonde9e5ff2016-11-02 07:14:03 -0700772void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t sync_offset_ms,
773 double estimated_freq_khz) {
asaperssonf8cdd182016-03-15 01:00:47 -0700774 rtc::CritScope lock(&crit_);
775 sync_offset_counter_.Add(std::abs(sync_offset_ms));
776 stats_.sync_offset_ms = sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700777
778 const double kMaxFreqKhz = 10000.0;
779 int offset_khz = kMaxFreqKhz;
780 // Should not be zero or negative. If so, report max.
781 if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0)
782 offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5);
783
784 freq_offset_counter_.Add(offset_khz);
asaperssonf8cdd182016-03-15 01:00:47 -0700785}
786
pbos@webrtc.org55707692014-12-19 15:45:03 +0000787void ReceiveStatisticsProxy::OnReceiveRatesUpdated(uint32_t bitRate,
788 uint32_t frameRate) {
789}
790
philipela45102f2017-02-22 05:30:39 -0800791void ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe,
ilnik6d5b4d62017-08-30 03:32:14 -0700792 size_t size_bytes,
793 VideoContentType content_type) {
philipela45102f2017-02-22 05:30:39 -0800794 rtc::CritScope lock(&crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700795 if (is_keyframe) {
philipela45102f2017-02-22 05:30:39 -0800796 ++stats_.frame_counts.key_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700797 } else {
philipela45102f2017-02-22 05:30:39 -0800798 ++stats_.frame_counts.delta_frames;
ilnik6d5b4d62017-08-30 03:32:14 -0700799 }
800
801 ContentSpecificStats* content_specific_stats =
802 &content_specific_stats_[content_type];
803
804 content_specific_stats->total_media_bytes += size_bytes;
805 if (is_keyframe) {
806 ++content_specific_stats->frame_counts.key_frames;
807 } else {
808 ++content_specific_stats->frame_counts.delta_frames;
809 }
philipela45102f2017-02-22 05:30:39 -0800810
811 int64_t now_ms = clock_->TimeInMilliseconds();
philipela45102f2017-02-22 05:30:39 -0800812 frame_window_.insert(std::make_pair(now_ms, size_bytes));
asapersson0255acb2017-03-28 02:44:58 -0700813 UpdateFramerate(now_ms);
philipela45102f2017-02-22 05:30:39 -0800814}
815
pbos@webrtc.org55707692014-12-19 15:45:03 +0000816void ReceiveStatisticsProxy::OnFrameCountsUpdated(
817 const FrameCounts& frame_counts) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200818 rtc::CritScope lock(&crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000819 stats_.frame_counts = frame_counts;
820}
821
pbos@webrtc.org55707692014-12-19 15:45:03 +0000822void ReceiveStatisticsProxy::OnDiscardedPacketsUpdated(int discarded_packets) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200823 rtc::CritScope lock(&crit_);
pbos@webrtc.org55707692014-12-19 15:45:03 +0000824 stats_.discarded_packets = discarded_packets;
825}
826
asapersson86b01602015-10-20 23:55:26 -0700827void ReceiveStatisticsProxy::OnPreDecode(
828 const EncodedImage& encoded_image,
829 const CodecSpecificInfo* codec_specific_info) {
Tommi132e28e2018-02-24 17:57:33 +0100830 RTC_DCHECK_RUN_ON(&decode_thread_);
Peter Boström74f6e9e2016-04-04 17:56:10 +0200831 if (!codec_specific_info || encoded_image.qp_ == -1) {
asapersson86b01602015-10-20 23:55:26 -0700832 return;
833 }
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200834 rtc::CritScope lock(&crit_);
835 last_codec_type_ = codec_specific_info->codecType;
836 if (last_codec_type_ == kVideoCodecVP8) {
asapersson86b01602015-10-20 23:55:26 -0700837 qp_counters_.vp8.Add(encoded_image.qp_);
palmkvist349092b2016-12-13 02:45:57 -0800838 qp_sample_.Add(encoded_image.qp_);
asapersson86b01602015-10-20 23:55:26 -0700839 }
840}
841
sprang3e86e7e2017-08-22 09:23:28 -0700842void ReceiveStatisticsProxy::OnStreamInactive() {
843 // TODO(sprang): Figure out any other state that should be reset.
844
845 rtc::CritScope lock(&crit_);
846 // Don't report inter-frame delay if stream was paused.
847 last_decoded_frame_time_ms_.reset();
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200848 video_quality_observer_->OnStreamInactive();
sprang3e86e7e2017-08-22 09:23:28 -0700849}
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