blob: 031331656749ddf4c6715b56465c51434343eb8a [file] [log] [blame]
sprang@webrtc.orgccd42842014-01-07 09:54:34 +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/send_statistics_proxy.h"
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000012
asaperssond89920b2015-07-22 06:52:00 -070013#include <algorithm>
Tim Psiakiad13d2f2015-11-10 16:34:50 -080014#include <cmath>
Åsa Persson20317f92018-08-15 08:57:54 +020015#include <limits>
Åsa Perssonaa329e72017-12-15 15:54:44 +010016#include <utility>
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000017
Steve Antonbd631a02019-03-28 10:51:27 -070018#include "absl/algorithm/container.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/video_coding/include/video_codec_interface.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
Åsa Persson20317f92018-08-15 08:57:54 +020022#include "rtc_base/numerics/mod_ops.h"
Tommifef05002018-02-27 13:51:08 +010023#include "rtc_base/strings/string_builder.h"
asapersson8d75ac72017-09-15 06:41:15 -070024#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "system_wrappers/include/metrics.h"
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000026
27namespace webrtc {
asapersson2a0a2a42015-10-27 01:32:00 -070028namespace {
asapersson1aa420b2015-12-07 03:12:22 -080029const float kEncodeTimeWeigthFactor = 0.5f;
Åsa Persson0122e842017-10-16 12:19:23 +020030const size_t kMaxEncodedFrameMapSize = 150;
31const int64_t kMaxEncodedFrameWindowMs = 800;
Åsa Persson20317f92018-08-15 08:57:54 +020032const uint32_t kMaxEncodedFrameTimestampDiff = 900000; // 10 sec.
Åsa Persson0122e842017-10-16 12:19:23 +020033const int64_t kBucketSizeMs = 100;
34const size_t kBucketCount = 10;
asapersson1aa420b2015-12-07 03:12:22 -080035
asapersson8d75ac72017-09-15 06:41:15 -070036const char kVp8ForcedFallbackEncoderFieldTrial[] =
Åsa Persson45bbc8a2017-11-13 10:16:47 +010037 "WebRTC-VP8-Forced-Fallback-Encoder-v2";
asapersson8d75ac72017-09-15 06:41:15 -070038const char kVp8SwCodecName[] = "libvpx";
39
asapersson2a0a2a42015-10-27 01:32:00 -070040// Used by histograms. Values of entries should not be changed.
41enum HistogramCodecType {
42 kVideoUnknown = 0,
43 kVideoVp8 = 1,
44 kVideoVp9 = 2,
45 kVideoH264 = 3,
46 kVideoMax = 64,
47};
48
asaperssonc2148a52016-02-04 00:33:21 -080049const char* kRealtimePrefix = "WebRTC.Video.";
50const char* kScreenPrefix = "WebRTC.Video.Screenshare.";
51
sprangb4a1ae52015-12-03 08:10:08 -080052const char* GetUmaPrefix(VideoEncoderConfig::ContentType content_type) {
53 switch (content_type) {
54 case VideoEncoderConfig::ContentType::kRealtimeVideo:
asaperssonc2148a52016-02-04 00:33:21 -080055 return kRealtimePrefix;
sprangb4a1ae52015-12-03 08:10:08 -080056 case VideoEncoderConfig::ContentType::kScreen:
asaperssonc2148a52016-02-04 00:33:21 -080057 return kScreenPrefix;
sprangb4a1ae52015-12-03 08:10:08 -080058 }
59 RTC_NOTREACHED();
60 return nullptr;
61}
62
asapersson2a0a2a42015-10-27 01:32:00 -070063HistogramCodecType PayloadNameToHistogramCodecType(
64 const std::string& payload_name) {
kthelgason1cdddc92017-08-24 03:52:48 -070065 VideoCodecType codecType = PayloadStringToCodecType(payload_name);
66 switch (codecType) {
deadbeefc964d0b2017-04-03 10:03:35 -070067 case kVideoCodecVP8:
68 return kVideoVp8;
69 case kVideoCodecVP9:
70 return kVideoVp9;
71 case kVideoCodecH264:
72 return kVideoH264;
73 default:
74 return kVideoUnknown;
75 }
asapersson2a0a2a42015-10-27 01:32:00 -070076}
77
78void UpdateCodecTypeHistogram(const std::string& payload_name) {
asapersson28ba9272016-01-25 05:58:23 -080079 RTC_HISTOGRAM_ENUMERATION("WebRTC.Video.Encoder.CodecType",
80 PayloadNameToHistogramCodecType(payload_name),
81 kVideoMax);
asapersson2a0a2a42015-10-27 01:32:00 -070082}
asapersson8d75ac72017-09-15 06:41:15 -070083
Niels Möllerd3b8c632018-08-27 15:33:42 +020084bool IsForcedFallbackPossible(const CodecSpecificInfo* codec_info,
85 int simulcast_index) {
86 return codec_info->codecType == kVideoCodecVP8 && simulcast_index == 0 &&
asapersson8d75ac72017-09-15 06:41:15 -070087 (codec_info->codecSpecific.VP8.temporalIdx == 0 ||
88 codec_info->codecSpecific.VP8.temporalIdx == kNoTemporalIdx);
89}
90
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020091absl::optional<int> GetFallbackMaxPixels(const std::string& group) {
asapersson8d75ac72017-09-15 06:41:15 -070092 if (group.empty())
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020093 return absl::nullopt;
asapersson8d75ac72017-09-15 06:41:15 -070094
asapersson8d75ac72017-09-15 06:41:15 -070095 int min_pixels;
Åsa Persson45bbc8a2017-11-13 10:16:47 +010096 int max_pixels;
97 int min_bps;
Åsa Perssonc3ed6302017-11-16 14:04:52 +010098 if (sscanf(group.c_str(), "-%d,%d,%d", &min_pixels, &max_pixels, &min_bps) !=
99 3) {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200100 return absl::optional<int>();
asapersson8d75ac72017-09-15 06:41:15 -0700101 }
102
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100103 if (min_pixels <= 0 || max_pixels <= 0 || max_pixels < min_pixels)
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200104 return absl::optional<int>();
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100105
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200106 return absl::optional<int>(max_pixels);
asapersson8d75ac72017-09-15 06:41:15 -0700107}
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100108
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200109absl::optional<int> GetFallbackMaxPixelsIfFieldTrialEnabled() {
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100110 std::string group =
111 webrtc::field_trial::FindFullName(kVp8ForcedFallbackEncoderFieldTrial);
112 return (group.find("Enabled") == 0) ? GetFallbackMaxPixels(group.substr(7))
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200113 : absl::optional<int>();
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100114}
115
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200116absl::optional<int> GetFallbackMaxPixelsIfFieldTrialDisabled() {
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100117 std::string group =
118 webrtc::field_trial::FindFullName(kVp8ForcedFallbackEncoderFieldTrial);
119 return (group.find("Disabled") == 0) ? GetFallbackMaxPixels(group.substr(8))
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200120 : absl::optional<int>();
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100121}
asapersson2a0a2a42015-10-27 01:32:00 -0700122} // namespace
123
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000124const int SendStatisticsProxy::kStatsTimeoutMs = 5000;
125
sprangb4a1ae52015-12-03 08:10:08 -0800126SendStatisticsProxy::SendStatisticsProxy(
127 Clock* clock,
128 const VideoSendStream::Config& config,
129 VideoEncoderConfig::ContentType content_type)
asaperssond89920b2015-07-22 06:52:00 -0700130 : clock_(clock),
Niels Möller259a4972018-04-05 15:36:51 +0200131 payload_name_(config.rtp.payload_name),
perkj26091b12016-09-01 01:17:40 -0700132 rtp_config_(config.rtp),
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100133 fallback_max_pixels_(GetFallbackMaxPixelsIfFieldTrialEnabled()),
134 fallback_max_pixels_disabled_(GetFallbackMaxPixelsIfFieldTrialDisabled()),
sprangb4a1ae52015-12-03 08:10:08 -0800135 content_type_(content_type),
asapersson4374a092016-07-27 00:39:09 -0700136 start_ms_(clock->TimeInMilliseconds()),
asapersson1aa420b2015-12-07 03:12:22 -0800137 encode_time_(kEncodeTimeWeigthFactor),
asapersson0944a802017-04-07 00:57:58 -0700138 quality_downscales_(-1),
139 cpu_downscales_(-1),
Åsa Persson0122e842017-10-16 12:19:23 +0200140 media_byte_rate_tracker_(kBucketSizeMs, kBucketCount),
141 encoded_frame_rate_tracker_(kBucketSizeMs, kBucketCount),
sprang07fb9be2016-02-24 07:55:00 -0800142 uma_container_(
143 new UmaSamplesContainer(GetUmaPrefix(content_type_), stats_, clock)) {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000144}
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000145
sprang07fb9be2016-02-24 07:55:00 -0800146SendStatisticsProxy::~SendStatisticsProxy() {
147 rtc::CritScope lock(&crit_);
perkj26091b12016-09-01 01:17:40 -0700148 uma_container_->UpdateHistograms(rtp_config_, stats_);
asapersson4374a092016-07-27 00:39:09 -0700149
150 int64_t elapsed_sec = (clock_->TimeInMilliseconds() - start_ms_) / 1000;
asapersson1d02d3e2016-09-09 22:40:25 -0700151 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Video.SendStreamLifetimeInSeconds",
152 elapsed_sec);
asapersson4374a092016-07-27 00:39:09 -0700153
154 if (elapsed_sec >= metrics::kMinRunTimeInSeconds)
perkj26091b12016-09-01 01:17:40 -0700155 UpdateCodecTypeHistogram(payload_name_);
sprang07fb9be2016-02-24 07:55:00 -0800156}
sprangb4a1ae52015-12-03 08:10:08 -0800157
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200158SendStatisticsProxy::FallbackEncoderInfo::FallbackEncoderInfo() = default;
159
sprangb4a1ae52015-12-03 08:10:08 -0800160SendStatisticsProxy::UmaSamplesContainer::UmaSamplesContainer(
sprang07fb9be2016-02-24 07:55:00 -0800161 const char* prefix,
162 const VideoSendStream::Stats& stats,
163 Clock* const clock)
sprangb4a1ae52015-12-03 08:10:08 -0800164 : uma_prefix_(prefix),
sprang07fb9be2016-02-24 07:55:00 -0800165 clock_(clock),
Honghai Zhang82d78622016-05-06 11:29:15 -0700166 input_frame_rate_tracker_(100, 10u),
asapersson320e45a2016-11-29 01:40:35 -0800167 input_fps_counter_(clock, nullptr, true),
168 sent_fps_counter_(clock, nullptr, true),
asapersson93e1e232017-02-06 05:18:35 -0800169 total_byte_counter_(clock, nullptr, true),
170 media_byte_counter_(clock, nullptr, true),
171 rtx_byte_counter_(clock, nullptr, true),
172 padding_byte_counter_(clock, nullptr, true),
173 retransmit_byte_counter_(clock, nullptr, true),
174 fec_byte_counter_(clock, nullptr, true),
sprang07fb9be2016-02-24 07:55:00 -0800175 first_rtcp_stats_time_ms_(-1),
Erik Språng22c2b482016-03-01 09:40:42 +0100176 first_rtp_stats_time_ms_(-1),
Åsa Perssonaa329e72017-12-15 15:54:44 +0100177 start_stats_(stats),
178 num_streams_(0),
179 num_pixels_highest_stream_(0) {
asapersson93e1e232017-02-06 05:18:35 -0800180 InitializeBitrateCounters(stats);
Åsa Persson20317f92018-08-15 08:57:54 +0200181 static_assert(
182 kMaxEncodedFrameTimestampDiff < std::numeric_limits<uint32_t>::max() / 2,
183 "has to be smaller than half range");
asapersson93e1e232017-02-06 05:18:35 -0800184}
sprangb4a1ae52015-12-03 08:10:08 -0800185
sprang07fb9be2016-02-24 07:55:00 -0800186SendStatisticsProxy::UmaSamplesContainer::~UmaSamplesContainer() {}
Åsa Persson24b4eda2015-06-16 10:17:01 +0200187
asapersson93e1e232017-02-06 05:18:35 -0800188void SendStatisticsProxy::UmaSamplesContainer::InitializeBitrateCounters(
189 const VideoSendStream::Stats& stats) {
190 for (const auto& it : stats.substreams) {
191 uint32_t ssrc = it.first;
192 total_byte_counter_.SetLast(it.second.rtp_stats.transmitted.TotalBytes(),
193 ssrc);
194 padding_byte_counter_.SetLast(it.second.rtp_stats.transmitted.padding_bytes,
195 ssrc);
196 retransmit_byte_counter_.SetLast(
197 it.second.rtp_stats.retransmitted.TotalBytes(), ssrc);
198 fec_byte_counter_.SetLast(it.second.rtp_stats.fec.TotalBytes(), ssrc);
199 if (it.second.is_rtx) {
200 rtx_byte_counter_.SetLast(it.second.rtp_stats.transmitted.TotalBytes(),
201 ssrc);
Erik Språng22c2b482016-03-01 09:40:42 +0100202 } else {
asapersson93e1e232017-02-06 05:18:35 -0800203 media_byte_counter_.SetLast(it.second.rtp_stats.MediaPayloadBytes(),
204 ssrc);
Erik Språng22c2b482016-03-01 09:40:42 +0100205 }
206 }
207}
208
Åsa Perssonaa329e72017-12-15 15:54:44 +0100209void SendStatisticsProxy::UmaSamplesContainer::RemoveOld(
210 int64_t now_ms,
211 bool* is_limited_in_resolution) {
Åsa Persson0122e842017-10-16 12:19:23 +0200212 while (!encoded_frames_.empty()) {
213 auto it = encoded_frames_.begin();
214 if (now_ms - it->second.send_ms < kMaxEncodedFrameWindowMs)
215 break;
216
217 // Use max per timestamp.
218 sent_width_counter_.Add(it->second.max_width);
219 sent_height_counter_.Add(it->second.max_height);
Åsa Perssonaa329e72017-12-15 15:54:44 +0100220
221 // Check number of encoded streams per timestamp.
Niels Möllerd3b8c632018-08-27 15:33:42 +0200222 if (num_streams_ > static_cast<size_t>(it->second.max_simulcast_idx)) {
Åsa Perssonaa329e72017-12-15 15:54:44 +0100223 *is_limited_in_resolution = false;
224 if (num_streams_ > 1) {
225 int disabled_streams =
226 static_cast<int>(num_streams_ - 1 - it->second.max_simulcast_idx);
227 // Can be limited in resolution or framerate.
228 uint32_t pixels = it->second.max_width * it->second.max_height;
229 bool bw_limited_resolution =
230 disabled_streams > 0 && pixels < num_pixels_highest_stream_;
231 bw_limited_frame_counter_.Add(bw_limited_resolution);
232 if (bw_limited_resolution) {
233 bw_resolutions_disabled_counter_.Add(disabled_streams);
234 *is_limited_in_resolution = true;
235 }
236 }
237 }
Åsa Persson0122e842017-10-16 12:19:23 +0200238 encoded_frames_.erase(it);
239 }
240}
241
242bool SendStatisticsProxy::UmaSamplesContainer::InsertEncodedFrame(
Åsa Perssonaa329e72017-12-15 15:54:44 +0100243 const EncodedImage& encoded_frame,
Niels Möllerd3b8c632018-08-27 15:33:42 +0200244 int simulcast_idx,
Åsa Perssonaa329e72017-12-15 15:54:44 +0100245 bool* is_limited_in_resolution) {
Åsa Persson0122e842017-10-16 12:19:23 +0200246 int64_t now_ms = clock_->TimeInMilliseconds();
Åsa Perssonaa329e72017-12-15 15:54:44 +0100247 RemoveOld(now_ms, is_limited_in_resolution);
Åsa Persson0122e842017-10-16 12:19:23 +0200248 if (encoded_frames_.size() > kMaxEncodedFrameMapSize) {
249 encoded_frames_.clear();
250 }
251
Åsa Persson20317f92018-08-15 08:57:54 +0200252 // Check for jump in timestamp.
253 if (!encoded_frames_.empty()) {
254 uint32_t oldest_timestamp = encoded_frames_.begin()->first;
Niels Möller23775882018-08-16 10:24:12 +0200255 if (ForwardDiff(oldest_timestamp, encoded_frame.Timestamp()) >
Åsa Persson20317f92018-08-15 08:57:54 +0200256 kMaxEncodedFrameTimestampDiff) {
257 // Gap detected, clear frames to have a sequence where newest timestamp
258 // is not too far away from oldest in order to distinguish old and new.
259 encoded_frames_.clear();
260 }
261 }
262
Niels Möller72bc8d62018-09-12 10:03:51 +0200263 auto it = encoded_frames_.find(encoded_frame.Timestamp());
Åsa Persson0122e842017-10-16 12:19:23 +0200264 if (it == encoded_frames_.end()) {
265 // First frame with this timestamp.
Åsa Perssonaa329e72017-12-15 15:54:44 +0100266 encoded_frames_.insert(
Niels Möller23775882018-08-16 10:24:12 +0200267 std::make_pair(encoded_frame.Timestamp(),
Åsa Perssonaa329e72017-12-15 15:54:44 +0100268 Frame(now_ms, encoded_frame._encodedWidth,
269 encoded_frame._encodedHeight, simulcast_idx)));
Åsa Persson0122e842017-10-16 12:19:23 +0200270 sent_fps_counter_.Add(1);
271 return true;
272 }
273
274 it->second.max_width =
275 std::max(it->second.max_width, encoded_frame._encodedWidth);
276 it->second.max_height =
277 std::max(it->second.max_height, encoded_frame._encodedHeight);
Åsa Perssonaa329e72017-12-15 15:54:44 +0100278 it->second.max_simulcast_idx =
279 std::max(it->second.max_simulcast_idx, simulcast_idx);
Åsa Persson0122e842017-10-16 12:19:23 +0200280 return false;
281}
282
sprang07fb9be2016-02-24 07:55:00 -0800283void SendStatisticsProxy::UmaSamplesContainer::UpdateHistograms(
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200284 const RtpConfig& rtp_config,
sprang07fb9be2016-02-24 07:55:00 -0800285 const VideoSendStream::Stats& current_stats) {
asaperssonc2148a52016-02-04 00:33:21 -0800286 RTC_DCHECK(uma_prefix_ == kRealtimePrefix || uma_prefix_ == kScreenPrefix);
287 const int kIndex = uma_prefix_ == kScreenPrefix ? 1 : 0;
asapersson320e45a2016-11-29 01:40:35 -0800288 const int kMinRequiredPeriodicSamples = 6;
Karl Wiberg881f1682018-03-08 15:03:23 +0100289 char log_stream_buf[8 * 1024];
290 rtc::SimpleStringBuilder log_stream(log_stream_buf);
perkj803d97f2016-11-01 11:45:46 -0700291 int in_width = input_width_counter_.Avg(kMinRequiredMetricsSamples);
292 int in_height = input_height_counter_.Avg(kMinRequiredMetricsSamples);
asaperssond89920b2015-07-22 06:52:00 -0700293 if (in_width != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700294 RTC_HISTOGRAMS_COUNTS_10000(kIndex, uma_prefix_ + "InputWidthInPixels",
295 in_width);
296 RTC_HISTOGRAMS_COUNTS_10000(kIndex, uma_prefix_ + "InputHeightInPixels",
297 in_height);
Tommifef05002018-02-27 13:51:08 +0100298 log_stream << uma_prefix_ << "InputWidthInPixels " << in_width << "\n"
299 << uma_prefix_ << "InputHeightInPixels " << in_height << "\n";
asaperssond89920b2015-07-22 06:52:00 -0700300 }
asapersson320e45a2016-11-29 01:40:35 -0800301 AggregatedStats in_fps = input_fps_counter_.GetStats();
302 if (in_fps.num_samples >= kMinRequiredPeriodicSamples) {
303 RTC_HISTOGRAMS_COUNTS_100(kIndex, uma_prefix_ + "InputFramesPerSecond",
304 in_fps.average);
Tommifef05002018-02-27 13:51:08 +0100305 log_stream << uma_prefix_ << "InputFramesPerSecond " << in_fps.ToString()
306 << "\n";
asapersson320e45a2016-11-29 01:40:35 -0800307 }
308
perkj803d97f2016-11-01 11:45:46 -0700309 int sent_width = sent_width_counter_.Avg(kMinRequiredMetricsSamples);
310 int sent_height = sent_height_counter_.Avg(kMinRequiredMetricsSamples);
asaperssond89920b2015-07-22 06:52:00 -0700311 if (sent_width != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700312 RTC_HISTOGRAMS_COUNTS_10000(kIndex, uma_prefix_ + "SentWidthInPixels",
313 sent_width);
314 RTC_HISTOGRAMS_COUNTS_10000(kIndex, uma_prefix_ + "SentHeightInPixels",
315 sent_height);
Tommifef05002018-02-27 13:51:08 +0100316 log_stream << uma_prefix_ << "SentWidthInPixels " << sent_width << "\n"
317 << uma_prefix_ << "SentHeightInPixels " << sent_height << "\n";
asaperssond89920b2015-07-22 06:52:00 -0700318 }
asapersson320e45a2016-11-29 01:40:35 -0800319 AggregatedStats sent_fps = sent_fps_counter_.GetStats();
320 if (sent_fps.num_samples >= kMinRequiredPeriodicSamples) {
321 RTC_HISTOGRAMS_COUNTS_100(kIndex, uma_prefix_ + "SentFramesPerSecond",
322 sent_fps.average);
Tommifef05002018-02-27 13:51:08 +0100323 log_stream << uma_prefix_ << "SentFramesPerSecond " << sent_fps.ToString()
324 << "\n";
asapersson320e45a2016-11-29 01:40:35 -0800325 }
326
ilnikf4ded682017-08-30 02:30:20 -0700327 if (in_fps.num_samples > kMinRequiredPeriodicSamples &&
328 sent_fps.num_samples >= kMinRequiredPeriodicSamples) {
329 int in_fps_avg = in_fps.average;
330 if (in_fps_avg > 0) {
331 int sent_fps_avg = sent_fps.average;
332 int sent_to_in_fps_ratio_percent =
333 (100 * sent_fps_avg + in_fps_avg / 2) / in_fps_avg;
334 // If reported period is small, it may happen that sent_fps is larger than
335 // input_fps briefly on average. This should be treated as 100% sent to
336 // input ratio.
337 if (sent_to_in_fps_ratio_percent > 100)
338 sent_to_in_fps_ratio_percent = 100;
339 RTC_HISTOGRAMS_PERCENTAGE(kIndex,
340 uma_prefix_ + "SentToInputFpsRatioPercent",
341 sent_to_in_fps_ratio_percent);
Tommifef05002018-02-27 13:51:08 +0100342 log_stream << uma_prefix_ << "SentToInputFpsRatioPercent "
343 << sent_to_in_fps_ratio_percent << "\n";
ilnikf4ded682017-08-30 02:30:20 -0700344 }
345 }
346
perkj803d97f2016-11-01 11:45:46 -0700347 int encode_ms = encode_time_counter_.Avg(kMinRequiredMetricsSamples);
asaperssonc2148a52016-02-04 00:33:21 -0800348 if (encode_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700349 RTC_HISTOGRAMS_COUNTS_1000(kIndex, uma_prefix_ + "EncodeTimeInMs",
350 encode_ms);
Tommifef05002018-02-27 13:51:08 +0100351 log_stream << uma_prefix_ << "EncodeTimeInMs " << encode_ms << "\n";
asaperssonc2148a52016-02-04 00:33:21 -0800352 }
perkj803d97f2016-11-01 11:45:46 -0700353 int key_frames_permille =
354 key_frame_counter_.Permille(kMinRequiredMetricsSamples);
asaperssondec5ebf2015-10-05 02:36:17 -0700355 if (key_frames_permille != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700356 RTC_HISTOGRAMS_COUNTS_1000(kIndex, uma_prefix_ + "KeyFramesSentInPermille",
357 key_frames_permille);
Tommifef05002018-02-27 13:51:08 +0100358 log_stream << uma_prefix_ << "KeyFramesSentInPermille "
359 << key_frames_permille << "\n";
asaperssondec5ebf2015-10-05 02:36:17 -0700360 }
asapersson4306fc72015-10-19 00:35:21 -0700361 int quality_limited =
perkj803d97f2016-11-01 11:45:46 -0700362 quality_limited_frame_counter_.Percent(kMinRequiredMetricsSamples);
asapersson4306fc72015-10-19 00:35:21 -0700363 if (quality_limited != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700364 RTC_HISTOGRAMS_PERCENTAGE(kIndex,
365 uma_prefix_ + "QualityLimitedResolutionInPercent",
366 quality_limited);
Tommifef05002018-02-27 13:51:08 +0100367 log_stream << uma_prefix_ << "QualityLimitedResolutionInPercent "
368 << quality_limited << "\n";
asapersson4306fc72015-10-19 00:35:21 -0700369 }
perkj803d97f2016-11-01 11:45:46 -0700370 int downscales = quality_downscales_counter_.Avg(kMinRequiredMetricsSamples);
asapersson4306fc72015-10-19 00:35:21 -0700371 if (downscales != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700372 RTC_HISTOGRAMS_ENUMERATION(
asaperssonc2148a52016-02-04 00:33:21 -0800373 kIndex, uma_prefix_ + "QualityLimitedResolutionDownscales", downscales,
374 20);
asapersson4306fc72015-10-19 00:35:21 -0700375 }
perkj803d97f2016-11-01 11:45:46 -0700376 int cpu_limited =
377 cpu_limited_frame_counter_.Percent(kMinRequiredMetricsSamples);
378 if (cpu_limited != -1) {
379 RTC_HISTOGRAMS_PERCENTAGE(
380 kIndex, uma_prefix_ + "CpuLimitedResolutionInPercent", cpu_limited);
381 }
382 int bw_limited =
383 bw_limited_frame_counter_.Percent(kMinRequiredMetricsSamples);
asaperssonda535c42015-10-19 23:32:41 -0700384 if (bw_limited != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700385 RTC_HISTOGRAMS_PERCENTAGE(
asaperssonc2148a52016-02-04 00:33:21 -0800386 kIndex, uma_prefix_ + "BandwidthLimitedResolutionInPercent",
387 bw_limited);
asaperssonda535c42015-10-19 23:32:41 -0700388 }
perkj803d97f2016-11-01 11:45:46 -0700389 int num_disabled =
390 bw_resolutions_disabled_counter_.Avg(kMinRequiredMetricsSamples);
asaperssonda535c42015-10-19 23:32:41 -0700391 if (num_disabled != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700392 RTC_HISTOGRAMS_ENUMERATION(
asaperssonc2148a52016-02-04 00:33:21 -0800393 kIndex, uma_prefix_ + "BandwidthLimitedResolutionsDisabled",
394 num_disabled, 10);
asaperssonda535c42015-10-19 23:32:41 -0700395 }
perkj803d97f2016-11-01 11:45:46 -0700396 int delay_ms = delay_counter_.Avg(kMinRequiredMetricsSamples);
asaperssonf040b232015-11-04 00:59:03 -0800397 if (delay_ms != -1)
asapersson1d02d3e2016-09-09 22:40:25 -0700398 RTC_HISTOGRAMS_COUNTS_100000(kIndex, uma_prefix_ + "SendSideDelayInMs",
399 delay_ms);
asaperssonf040b232015-11-04 00:59:03 -0800400
perkj803d97f2016-11-01 11:45:46 -0700401 int max_delay_ms = max_delay_counter_.Avg(kMinRequiredMetricsSamples);
asaperssonf040b232015-11-04 00:59:03 -0800402 if (max_delay_ms != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700403 RTC_HISTOGRAMS_COUNTS_100000(kIndex, uma_prefix_ + "SendSideDelayMaxInMs",
404 max_delay_ms);
sprangb4a1ae52015-12-03 08:10:08 -0800405 }
sprang07fb9be2016-02-24 07:55:00 -0800406
asapersson118ef002016-03-31 00:00:19 -0700407 for (const auto& it : qp_counters_) {
perkj803d97f2016-11-01 11:45:46 -0700408 int qp_vp8 = it.second.vp8.Avg(kMinRequiredMetricsSamples);
asapersson5265fed2016-04-18 02:58:47 -0700409 if (qp_vp8 != -1) {
asapersson118ef002016-03-31 00:00:19 -0700410 int spatial_idx = it.first;
411 if (spatial_idx == -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700412 RTC_HISTOGRAMS_COUNTS_200(kIndex, uma_prefix_ + "Encoded.Qp.Vp8",
413 qp_vp8);
asapersson118ef002016-03-31 00:00:19 -0700414 } else if (spatial_idx == 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700415 RTC_HISTOGRAMS_COUNTS_200(kIndex, uma_prefix_ + "Encoded.Qp.Vp8.S0",
416 qp_vp8);
asapersson118ef002016-03-31 00:00:19 -0700417 } else if (spatial_idx == 1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700418 RTC_HISTOGRAMS_COUNTS_200(kIndex, uma_prefix_ + "Encoded.Qp.Vp8.S1",
419 qp_vp8);
asapersson118ef002016-03-31 00:00:19 -0700420 } else if (spatial_idx == 2) {
asapersson1d02d3e2016-09-09 22:40:25 -0700421 RTC_HISTOGRAMS_COUNTS_200(kIndex, uma_prefix_ + "Encoded.Qp.Vp8.S2",
422 qp_vp8);
asapersson118ef002016-03-31 00:00:19 -0700423 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100424 RTC_LOG(LS_WARNING)
425 << "QP stats not recorded for VP8 spatial idx " << spatial_idx;
asapersson118ef002016-03-31 00:00:19 -0700426 }
427 }
perkj803d97f2016-11-01 11:45:46 -0700428 int qp_vp9 = it.second.vp9.Avg(kMinRequiredMetricsSamples);
asapersson5265fed2016-04-18 02:58:47 -0700429 if (qp_vp9 != -1) {
430 int spatial_idx = it.first;
431 if (spatial_idx == -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700432 RTC_HISTOGRAMS_COUNTS_500(kIndex, uma_prefix_ + "Encoded.Qp.Vp9",
433 qp_vp9);
asapersson5265fed2016-04-18 02:58:47 -0700434 } else if (spatial_idx == 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700435 RTC_HISTOGRAMS_COUNTS_500(kIndex, uma_prefix_ + "Encoded.Qp.Vp9.S0",
436 qp_vp9);
asapersson5265fed2016-04-18 02:58:47 -0700437 } else if (spatial_idx == 1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700438 RTC_HISTOGRAMS_COUNTS_500(kIndex, uma_prefix_ + "Encoded.Qp.Vp9.S1",
439 qp_vp9);
asapersson5265fed2016-04-18 02:58:47 -0700440 } else if (spatial_idx == 2) {
asapersson1d02d3e2016-09-09 22:40:25 -0700441 RTC_HISTOGRAMS_COUNTS_500(kIndex, uma_prefix_ + "Encoded.Qp.Vp9.S2",
442 qp_vp9);
asapersson5265fed2016-04-18 02:58:47 -0700443 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100444 RTC_LOG(LS_WARNING)
445 << "QP stats not recorded for VP9 spatial layer " << spatial_idx;
asapersson5265fed2016-04-18 02:58:47 -0700446 }
447 }
asapersson827cab32016-11-02 09:08:47 -0700448 int qp_h264 = it.second.h264.Avg(kMinRequiredMetricsSamples);
449 if (qp_h264 != -1) {
450 int spatial_idx = it.first;
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200451 if (spatial_idx == -1) {
452 RTC_HISTOGRAMS_COUNTS_200(kIndex, uma_prefix_ + "Encoded.Qp.H264",
453 qp_h264);
454 } else if (spatial_idx == 0) {
455 RTC_HISTOGRAMS_COUNTS_200(kIndex, uma_prefix_ + "Encoded.Qp.H264.S0",
456 qp_h264);
457 } else if (spatial_idx == 1) {
458 RTC_HISTOGRAMS_COUNTS_200(kIndex, uma_prefix_ + "Encoded.Qp.H264.S1",
459 qp_h264);
460 } else if (spatial_idx == 2) {
461 RTC_HISTOGRAMS_COUNTS_200(kIndex, uma_prefix_ + "Encoded.Qp.H264.S2",
462 qp_h264);
463 } else {
464 RTC_LOG(LS_WARNING)
465 << "QP stats not recorded for H264 spatial idx " << spatial_idx;
466 }
asapersson827cab32016-11-02 09:08:47 -0700467 }
asapersson118ef002016-03-31 00:00:19 -0700468 }
469
asapersson0944a802017-04-07 00:57:58 -0700470 if (first_rtp_stats_time_ms_ != -1) {
asapersson09f05612017-05-15 23:40:18 -0700471 quality_adapt_timer_.Stop(clock_->TimeInMilliseconds());
472 int64_t elapsed_sec = quality_adapt_timer_.total_ms / 1000;
asapersson0944a802017-04-07 00:57:58 -0700473 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
474 int quality_changes = current_stats.number_of_quality_adapt_changes -
475 start_stats_.number_of_quality_adapt_changes;
Åsa Persson875841d2018-01-08 08:49:53 +0100476 // Only base stats on changes during a call, discard initial changes.
477 int initial_changes =
478 initial_quality_changes_.down + initial_quality_changes_.up;
479 if (initial_changes <= quality_changes)
480 quality_changes -= initial_changes;
asapersson0944a802017-04-07 00:57:58 -0700481 RTC_HISTOGRAMS_COUNTS_100(kIndex,
482 uma_prefix_ + "AdaptChangesPerMinute.Quality",
483 quality_changes * 60 / elapsed_sec);
484 }
asapersson09f05612017-05-15 23:40:18 -0700485 cpu_adapt_timer_.Stop(clock_->TimeInMilliseconds());
486 elapsed_sec = cpu_adapt_timer_.total_ms / 1000;
asapersson0944a802017-04-07 00:57:58 -0700487 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
488 int cpu_changes = current_stats.number_of_cpu_adapt_changes -
489 start_stats_.number_of_cpu_adapt_changes;
490 RTC_HISTOGRAMS_COUNTS_100(kIndex,
491 uma_prefix_ + "AdaptChangesPerMinute.Cpu",
492 cpu_changes * 60 / elapsed_sec);
493 }
asapersson6eca98b2017-04-04 23:40:50 -0700494 }
495
sprange2d83d62016-02-19 09:03:26 -0800496 if (first_rtcp_stats_time_ms_ != -1) {
sprang07fb9be2016-02-24 07:55:00 -0800497 int64_t elapsed_sec =
498 (clock_->TimeInMilliseconds() - first_rtcp_stats_time_ms_) / 1000;
499 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
500 int fraction_lost = report_block_stats_.FractionLostInPercent();
501 if (fraction_lost != -1) {
asapersson1d02d3e2016-09-09 22:40:25 -0700502 RTC_HISTOGRAMS_PERCENTAGE(
sprang07fb9be2016-02-24 07:55:00 -0800503 kIndex, uma_prefix_ + "SentPacketsLostInPercent", fraction_lost);
Tommifef05002018-02-27 13:51:08 +0100504 log_stream << uma_prefix_ << "SentPacketsLostInPercent "
Stefan Holmer0a5792e2018-10-05 13:47:12 +0200505 << fraction_lost << "\n";
sprang07fb9be2016-02-24 07:55:00 -0800506 }
507
508 // The RTCP packet type counters, delivered via the
509 // RtcpPacketTypeCounterObserver interface, are aggregates over the entire
510 // life of the send stream and are not reset when switching content type.
511 // For the purpose of these statistics though, we want new counts when
512 // switching since we switch histogram name. On every reset of the
513 // UmaSamplesContainer, we save the initial state of the counters, so that
514 // we can calculate the delta here and aggregate over all ssrcs.
515 RtcpPacketTypeCounter counters;
perkj26091b12016-09-01 01:17:40 -0700516 for (uint32_t ssrc : rtp_config.ssrcs) {
sprang07fb9be2016-02-24 07:55:00 -0800517 auto kv = current_stats.substreams.find(ssrc);
518 if (kv == current_stats.substreams.end())
519 continue;
520
521 RtcpPacketTypeCounter stream_counters =
522 kv->second.rtcp_packet_type_counts;
523 kv = start_stats_.substreams.find(ssrc);
524 if (kv != start_stats_.substreams.end())
525 stream_counters.Subtract(kv->second.rtcp_packet_type_counts);
526
527 counters.Add(stream_counters);
528 }
asapersson1d02d3e2016-09-09 22:40:25 -0700529 RTC_HISTOGRAMS_COUNTS_10000(kIndex,
530 uma_prefix_ + "NackPacketsReceivedPerMinute",
531 counters.nack_packets * 60 / elapsed_sec);
532 RTC_HISTOGRAMS_COUNTS_10000(kIndex,
533 uma_prefix_ + "FirPacketsReceivedPerMinute",
534 counters.fir_packets * 60 / elapsed_sec);
535 RTC_HISTOGRAMS_COUNTS_10000(kIndex,
536 uma_prefix_ + "PliPacketsReceivedPerMinute",
537 counters.pli_packets * 60 / elapsed_sec);
sprang07fb9be2016-02-24 07:55:00 -0800538 if (counters.nack_requests > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700539 RTC_HISTOGRAMS_PERCENTAGE(
sprang07fb9be2016-02-24 07:55:00 -0800540 kIndex, uma_prefix_ + "UniqueNackRequestsReceivedInPercent",
541 counters.UniqueNackRequestsInPercent());
542 }
sprange2d83d62016-02-19 09:03:26 -0800543 }
544 }
Erik Språng22c2b482016-03-01 09:40:42 +0100545
546 if (first_rtp_stats_time_ms_ != -1) {
547 int64_t elapsed_sec =
548 (clock_->TimeInMilliseconds() - first_rtp_stats_time_ms_) / 1000;
549 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
asapersson66d4b372016-12-19 06:50:53 -0800550 RTC_HISTOGRAMS_COUNTS_100(kIndex, uma_prefix_ + "NumberOfPauseEvents",
551 target_rate_updates_.pause_resume_events);
Tommifef05002018-02-27 13:51:08 +0100552 log_stream << uma_prefix_ << "NumberOfPauseEvents "
553 << target_rate_updates_.pause_resume_events << "\n";
asapersson66d4b372016-12-19 06:50:53 -0800554
555 int paused_time_percent =
556 paused_time_counter_.Percent(metrics::kMinRunTimeInSeconds * 1000);
557 if (paused_time_percent != -1) {
558 RTC_HISTOGRAMS_PERCENTAGE(kIndex, uma_prefix_ + "PausedTimeInPercent",
559 paused_time_percent);
Tommifef05002018-02-27 13:51:08 +0100560 log_stream << uma_prefix_ << "PausedTimeInPercent "
561 << paused_time_percent << "\n";
asapersson66d4b372016-12-19 06:50:53 -0800562 }
asapersson93e1e232017-02-06 05:18:35 -0800563 }
564 }
asapersson66d4b372016-12-19 06:50:53 -0800565
asapersson8d75ac72017-09-15 06:41:15 -0700566 if (fallback_info_.is_possible) {
567 // Double interval since there is some time before fallback may occur.
568 const int kMinRunTimeMs = 2 * metrics::kMinRunTimeInSeconds * 1000;
569 int64_t elapsed_ms = fallback_info_.elapsed_ms;
570 int fallback_time_percent = fallback_active_counter_.Percent(kMinRunTimeMs);
571 if (fallback_time_percent != -1 && elapsed_ms >= kMinRunTimeMs) {
572 RTC_HISTOGRAMS_PERCENTAGE(
573 kIndex, uma_prefix_ + "Encoder.ForcedSwFallbackTimeInPercent.Vp8",
574 fallback_time_percent);
575 RTC_HISTOGRAMS_COUNTS_100(
576 kIndex, uma_prefix_ + "Encoder.ForcedSwFallbackChangesPerMinute.Vp8",
577 fallback_info_.on_off_events * 60 / (elapsed_ms / 1000));
578 }
579 }
580
asapersson93e1e232017-02-06 05:18:35 -0800581 AggregatedStats total_bytes_per_sec = total_byte_counter_.GetStats();
582 if (total_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
583 RTC_HISTOGRAMS_COUNTS_10000(kIndex, uma_prefix_ + "BitrateSentInKbps",
584 total_bytes_per_sec.average * 8 / 1000);
Tommifef05002018-02-27 13:51:08 +0100585 log_stream << uma_prefix_ << "BitrateSentInBps "
586 << total_bytes_per_sec.ToStringWithMultiplier(8) << "\n";
asapersson93e1e232017-02-06 05:18:35 -0800587 }
588 AggregatedStats media_bytes_per_sec = media_byte_counter_.GetStats();
589 if (media_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
590 RTC_HISTOGRAMS_COUNTS_10000(kIndex, uma_prefix_ + "MediaBitrateSentInKbps",
591 media_bytes_per_sec.average * 8 / 1000);
Tommifef05002018-02-27 13:51:08 +0100592 log_stream << uma_prefix_ << "MediaBitrateSentInBps "
593 << media_bytes_per_sec.ToStringWithMultiplier(8) << "\n";
asapersson93e1e232017-02-06 05:18:35 -0800594 }
595 AggregatedStats padding_bytes_per_sec = padding_byte_counter_.GetStats();
596 if (padding_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
597 RTC_HISTOGRAMS_COUNTS_10000(kIndex,
598 uma_prefix_ + "PaddingBitrateSentInKbps",
599 padding_bytes_per_sec.average * 8 / 1000);
Tommifef05002018-02-27 13:51:08 +0100600 log_stream << uma_prefix_ << "PaddingBitrateSentInBps "
601 << padding_bytes_per_sec.ToStringWithMultiplier(8) << "\n";
asapersson93e1e232017-02-06 05:18:35 -0800602 }
603 AggregatedStats retransmit_bytes_per_sec =
604 retransmit_byte_counter_.GetStats();
605 if (retransmit_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
606 RTC_HISTOGRAMS_COUNTS_10000(kIndex,
607 uma_prefix_ + "RetransmittedBitrateSentInKbps",
608 retransmit_bytes_per_sec.average * 8 / 1000);
Tommifef05002018-02-27 13:51:08 +0100609 log_stream << uma_prefix_ << "RetransmittedBitrateSentInBps "
610 << retransmit_bytes_per_sec.ToStringWithMultiplier(8) << "\n";
asapersson93e1e232017-02-06 05:18:35 -0800611 }
612 if (!rtp_config.rtx.ssrcs.empty()) {
613 AggregatedStats rtx_bytes_per_sec = rtx_byte_counter_.GetStats();
614 int rtx_bytes_per_sec_avg = -1;
615 if (rtx_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
616 rtx_bytes_per_sec_avg = rtx_bytes_per_sec.average;
Tommifef05002018-02-27 13:51:08 +0100617 log_stream << uma_prefix_ << "RtxBitrateSentInBps "
618 << rtx_bytes_per_sec.ToStringWithMultiplier(8) << "\n";
asapersson93e1e232017-02-06 05:18:35 -0800619 } else if (total_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
620 rtx_bytes_per_sec_avg = 0; // RTX enabled but no RTX data sent, record 0.
621 }
622 if (rtx_bytes_per_sec_avg != -1) {
623 RTC_HISTOGRAMS_COUNTS_10000(kIndex, uma_prefix_ + "RtxBitrateSentInKbps",
624 rtx_bytes_per_sec_avg * 8 / 1000);
625 }
626 }
627 if (rtp_config.flexfec.payload_type != -1 ||
628 rtp_config.ulpfec.red_payload_type != -1) {
629 AggregatedStats fec_bytes_per_sec = fec_byte_counter_.GetStats();
630 if (fec_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
631 RTC_HISTOGRAMS_COUNTS_10000(kIndex, uma_prefix_ + "FecBitrateSentInKbps",
632 fec_bytes_per_sec.average * 8 / 1000);
Tommifef05002018-02-27 13:51:08 +0100633 log_stream << uma_prefix_ << "FecBitrateSentInBps "
634 << fec_bytes_per_sec.ToStringWithMultiplier(8) << "\n";
Erik Språng22c2b482016-03-01 09:40:42 +0100635 }
636 }
Tommifef05002018-02-27 13:51:08 +0100637 log_stream << "Frames encoded " << current_stats.frames_encoded << "\n"
638 << uma_prefix_ << "DroppedFrames.Capturer "
639 << current_stats.frames_dropped_by_capturer << "\n";
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200640 RTC_HISTOGRAMS_COUNTS_1000(kIndex, uma_prefix_ + "DroppedFrames.Capturer",
641 current_stats.frames_dropped_by_capturer);
Tommifef05002018-02-27 13:51:08 +0100642 log_stream << uma_prefix_ << "DroppedFrames.EncoderQueue "
643 << current_stats.frames_dropped_by_encoder_queue << "\n";
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200644 RTC_HISTOGRAMS_COUNTS_1000(kIndex, uma_prefix_ + "DroppedFrames.EncoderQueue",
645 current_stats.frames_dropped_by_encoder_queue);
Tommifef05002018-02-27 13:51:08 +0100646 log_stream << uma_prefix_ << "DroppedFrames.Encoder "
647 << current_stats.frames_dropped_by_encoder << "\n";
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200648 RTC_HISTOGRAMS_COUNTS_1000(kIndex, uma_prefix_ + "DroppedFrames.Encoder",
649 current_stats.frames_dropped_by_encoder);
Tommifef05002018-02-27 13:51:08 +0100650 log_stream << uma_prefix_ << "DroppedFrames.Ratelimiter "
651 << current_stats.frames_dropped_by_rate_limiter;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200652 RTC_HISTOGRAMS_COUNTS_1000(kIndex, uma_prefix_ + "DroppedFrames.Ratelimiter",
653 current_stats.frames_dropped_by_rate_limiter);
Jonas Olsson694a36f2018-02-16 13:09:41 +0100654
Tommifef05002018-02-27 13:51:08 +0100655 RTC_LOG(LS_INFO) << log_stream.str();
sprangb4a1ae52015-12-03 08:10:08 -0800656}
657
Pera48ddb72016-09-29 11:48:50 +0200658void SendStatisticsProxy::OnEncoderReconfigured(
659 const VideoEncoderConfig& config,
Niels Möller97e04882018-05-25 09:43:26 +0200660 const std::vector<VideoStream>& streams) {
sprangb4a1ae52015-12-03 08:10:08 -0800661 rtc::CritScope lock(&crit_);
Pera48ddb72016-09-29 11:48:50 +0200662
663 if (content_type_ != config.content_type) {
perkj26091b12016-09-01 01:17:40 -0700664 uma_container_->UpdateHistograms(rtp_config_, stats_);
Pera48ddb72016-09-29 11:48:50 +0200665 uma_container_.reset(new UmaSamplesContainer(
666 GetUmaPrefix(config.content_type), stats_, clock_));
667 content_type_ = config.content_type;
asaperssonf040b232015-11-04 00:59:03 -0800668 }
Åsa Perssonaa329e72017-12-15 15:54:44 +0100669 uma_container_->encoded_frames_.clear();
670 uma_container_->num_streams_ = streams.size();
671 uma_container_->num_pixels_highest_stream_ =
672 streams.empty() ? 0 : (streams.back().width * streams.back().height);
Åsa Persson24b4eda2015-06-16 10:17:01 +0200673}
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000674
Niels Möller213618e2018-07-24 09:29:58 +0200675void SendStatisticsProxy::OnEncodedFrameTimeMeasured(int encode_time_ms,
676 int encode_usage_percent) {
Henrik Boström5684af52019-04-02 15:05:21 +0200677 RTC_DCHECK_GE(encode_time_ms, 0);
Peter Boströmf2f82832015-05-01 13:00:41 +0200678 rtc::CritScope lock(&crit_);
Peter Boströme4499152016-02-05 11:13:28 +0100679 uma_container_->encode_time_counter_.Add(encode_time_ms);
680 encode_time_.Apply(1.0f, encode_time_ms);
Oleh Prypin19929582019-04-23 08:50:04 +0200681 stats_.avg_encode_time_ms = std::round(encode_time_.filtered());
Henrik Boström5684af52019-04-02 15:05:21 +0200682 stats_.total_encode_time_ms += encode_time_ms;
Niels Möller213618e2018-07-24 09:29:58 +0200683 stats_.encode_usage_percent = encode_usage_percent;
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000684}
685
Peter Boström7083e112015-09-22 16:28:51 +0200686void SendStatisticsProxy::OnSuspendChange(bool is_suspended) {
asapersson0944a802017-04-07 00:57:58 -0700687 int64_t now_ms = clock_->TimeInMilliseconds();
Peter Boströmf2f82832015-05-01 13:00:41 +0200688 rtc::CritScope lock(&crit_);
henrik.lundin@webrtc.orgb10363f2014-03-13 13:31:21 +0000689 stats_.suspended = is_suspended;
asapersson320e45a2016-11-29 01:40:35 -0800690 if (is_suspended) {
asapersson93e1e232017-02-06 05:18:35 -0800691 // Pause framerate (add min pause time since there may be frames/packets
692 // that are not yet sent).
693 const int64_t kMinMs = 500;
694 uma_container_->input_fps_counter_.ProcessAndPauseForDuration(kMinMs);
695 uma_container_->sent_fps_counter_.ProcessAndPauseForDuration(kMinMs);
696 // Pause bitrate stats.
697 uma_container_->total_byte_counter_.ProcessAndPauseForDuration(kMinMs);
698 uma_container_->media_byte_counter_.ProcessAndPauseForDuration(kMinMs);
699 uma_container_->rtx_byte_counter_.ProcessAndPauseForDuration(kMinMs);
700 uma_container_->padding_byte_counter_.ProcessAndPauseForDuration(kMinMs);
701 uma_container_->retransmit_byte_counter_.ProcessAndPauseForDuration(kMinMs);
702 uma_container_->fec_byte_counter_.ProcessAndPauseForDuration(kMinMs);
asapersson0944a802017-04-07 00:57:58 -0700703 // Stop adaptation stats.
asapersson09f05612017-05-15 23:40:18 -0700704 uma_container_->cpu_adapt_timer_.Stop(now_ms);
705 uma_container_->quality_adapt_timer_.Stop(now_ms);
asapersson93e1e232017-02-06 05:18:35 -0800706 } else {
asapersson0944a802017-04-07 00:57:58 -0700707 // Start adaptation stats if scaling is enabled.
708 if (cpu_downscales_ >= 0)
asapersson09f05612017-05-15 23:40:18 -0700709 uma_container_->cpu_adapt_timer_.Start(now_ms);
asapersson0944a802017-04-07 00:57:58 -0700710 if (quality_downscales_ >= 0)
asapersson09f05612017-05-15 23:40:18 -0700711 uma_container_->quality_adapt_timer_.Start(now_ms);
asapersson93e1e232017-02-06 05:18:35 -0800712 // Stop pause explicitly for stats that may be zero/not updated for some
713 // time.
714 uma_container_->rtx_byte_counter_.ProcessAndStopPause();
715 uma_container_->padding_byte_counter_.ProcessAndStopPause();
716 uma_container_->retransmit_byte_counter_.ProcessAndStopPause();
717 uma_container_->fec_byte_counter_.ProcessAndStopPause();
asapersson320e45a2016-11-29 01:40:35 -0800718 }
henrik.lundin@webrtc.orgb10363f2014-03-13 13:31:21 +0000719}
720
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000721VideoSendStream::Stats SendStatisticsProxy::GetStats() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200722 rtc::CritScope lock(&crit_);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000723 PurgeOldStats();
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000724 stats_.input_frame_rate =
sprangb4a1ae52015-12-03 08:10:08 -0800725 round(uma_container_->input_frame_rate_tracker_.ComputeRate());
ilnik50864a82017-09-06 12:32:35 -0700726 stats_.content_type =
727 content_type_ == VideoEncoderConfig::ContentType::kRealtimeVideo
728 ? VideoContentType::UNSPECIFIED
729 : VideoContentType::SCREENSHARE;
Åsa Persson0122e842017-10-16 12:19:23 +0200730 stats_.encode_frame_rate = round(encoded_frame_rate_tracker_.ComputeRate());
731 stats_.media_bitrate_bps = media_byte_rate_tracker_.ComputeRate() * 8;
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000732 return stats_;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000733}
734
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000735void SendStatisticsProxy::PurgeOldStats() {
Peter Boström20f3f942015-05-15 11:33:39 +0200736 int64_t old_stats_ms = clock_->TimeInMilliseconds() - kStatsTimeoutMs;
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000737 for (std::map<uint32_t, VideoSendStream::StreamStats>::iterator it =
738 stats_.substreams.begin();
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000739 it != stats_.substreams.end(); ++it) {
740 uint32_t ssrc = it->first;
Peter Boström20f3f942015-05-15 11:33:39 +0200741 if (update_times_[ssrc].resolution_update_ms <= old_stats_ms) {
742 it->second.width = 0;
743 it->second.height = 0;
744 }
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000745 }
746}
747
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000748VideoSendStream::StreamStats* SendStatisticsProxy::GetStatsEntry(
749 uint32_t ssrc) {
750 std::map<uint32_t, VideoSendStream::StreamStats>::iterator it =
751 stats_.substreams.find(ssrc);
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000752 if (it != stats_.substreams.end())
753 return &it->second;
754
Steve Antonbd631a02019-03-28 10:51:27 -0700755 bool is_media = absl::c_linear_search(rtp_config_.ssrcs, ssrc);
brandtr3d200bd2017-01-16 06:59:19 -0800756 bool is_flexfec = rtp_config_.flexfec.payload_type != -1 &&
757 ssrc == rtp_config_.flexfec.ssrc;
Steve Antonbd631a02019-03-28 10:51:27 -0700758 bool is_rtx = absl::c_linear_search(rtp_config_.rtx.ssrcs, ssrc);
brandtrcd188f62016-11-15 08:21:52 -0800759 if (!is_media && !is_flexfec && !is_rtx)
760 return nullptr;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000761
asapersson2e5cfcd2016-08-11 08:41:18 -0700762 // Insert new entry and return ptr.
763 VideoSendStream::StreamStats* entry = &stats_.substreams[ssrc];
764 entry->is_rtx = is_rtx;
asaperssona6a699a2016-11-25 03:52:46 -0800765 entry->is_flexfec = is_flexfec;
asapersson2e5cfcd2016-08-11 08:41:18 -0700766
767 return entry;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000768}
769
Peter Boström20f3f942015-05-15 11:33:39 +0200770void SendStatisticsProxy::OnInactiveSsrc(uint32_t ssrc) {
771 rtc::CritScope lock(&crit_);
772 VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc);
Peter Boström74f6e9e2016-04-04 17:56:10 +0200773 if (!stats)
Peter Boström20f3f942015-05-15 11:33:39 +0200774 return;
775
776 stats->total_bitrate_bps = 0;
777 stats->retransmit_bitrate_bps = 0;
778 stats->height = 0;
779 stats->width = 0;
780}
781
perkjf5b2e512016-07-05 08:34:04 -0700782void SendStatisticsProxy::OnSetEncoderTargetRate(uint32_t bitrate_bps) {
Peter Boströmf2f82832015-05-01 13:00:41 +0200783 rtc::CritScope lock(&crit_);
asapersson66d4b372016-12-19 06:50:53 -0800784 if (uma_container_->target_rate_updates_.last_ms == -1 && bitrate_bps == 0)
785 return; // Start on first non-zero bitrate, may initially be zero.
786
787 int64_t now = clock_->TimeInMilliseconds();
788 if (uma_container_->target_rate_updates_.last_ms != -1) {
789 bool was_paused = stats_.target_media_bitrate_bps == 0;
790 int64_t diff_ms = now - uma_container_->target_rate_updates_.last_ms;
791 uma_container_->paused_time_counter_.Add(was_paused, diff_ms);
792
793 // Use last to not include update when stream is stopped and video disabled.
794 if (uma_container_->target_rate_updates_.last_paused_or_resumed)
795 ++uma_container_->target_rate_updates_.pause_resume_events;
796
797 // Check if video is paused/resumed.
798 uma_container_->target_rate_updates_.last_paused_or_resumed =
799 (bitrate_bps == 0) != was_paused;
800 }
801 uma_container_->target_rate_updates_.last_ms = now;
802
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000803 stats_.target_media_bitrate_bps = bitrate_bps;
804}
805
asapersson8d75ac72017-09-15 06:41:15 -0700806void SendStatisticsProxy::UpdateEncoderFallbackStats(
Åsa Persson45bbc8a2017-11-13 10:16:47 +0100807 const CodecSpecificInfo* codec_info,
Niels Möllerd3b8c632018-08-27 15:33:42 +0200808 int pixels,
809 int simulcast_index) {
810 UpdateFallbackDisabledStats(codec_info, pixels, simulcast_index);
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100811
Åsa Persson45bbc8a2017-11-13 10:16:47 +0100812 if (!fallback_max_pixels_ || !uma_container_->fallback_info_.is_possible) {
asapersson8d75ac72017-09-15 06:41:15 -0700813 return;
814 }
815
Niels Möllerd3b8c632018-08-27 15:33:42 +0200816 if (!IsForcedFallbackPossible(codec_info, simulcast_index)) {
asapersson8d75ac72017-09-15 06:41:15 -0700817 uma_container_->fallback_info_.is_possible = false;
818 return;
819 }
820
821 FallbackEncoderInfo* fallback_info = &uma_container_->fallback_info_;
822
823 const int64_t now_ms = clock_->TimeInMilliseconds();
824 bool is_active = fallback_info->is_active;
Erik Språnge2fd86a2018-10-24 11:32:39 +0200825 if (encoder_changed_) {
asapersson8d75ac72017-09-15 06:41:15 -0700826 // Implementation changed.
Erik Språnge2fd86a2018-10-24 11:32:39 +0200827 const bool last_was_vp8_software =
828 encoder_changed_->previous_encoder_implementation == kVp8SwCodecName;
829 is_active = encoder_changed_->new_encoder_implementation == kVp8SwCodecName;
830 encoder_changed_.reset();
831 if (!is_active && !last_was_vp8_software) {
asapersson8d75ac72017-09-15 06:41:15 -0700832 // First or not a VP8 SW change, update stats on next call.
833 return;
834 }
Åsa Persson45bbc8a2017-11-13 10:16:47 +0100835 if (is_active && (pixels > *fallback_max_pixels_)) {
836 // Pixels should not be above |fallback_max_pixels_|. If above skip to
837 // avoid fallbacks due to failure.
838 fallback_info->is_possible = false;
839 return;
asapersson8d75ac72017-09-15 06:41:15 -0700840 }
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100841 stats_.has_entered_low_resolution = true;
asapersson8d75ac72017-09-15 06:41:15 -0700842 ++fallback_info->on_off_events;
843 }
844
845 if (fallback_info->last_update_ms) {
846 int64_t diff_ms = now_ms - *(fallback_info->last_update_ms);
847 // If the time diff since last update is greater than |max_frame_diff_ms|,
848 // video is considered paused/muted and the change is not included.
849 if (diff_ms < fallback_info->max_frame_diff_ms) {
850 uma_container_->fallback_active_counter_.Add(fallback_info->is_active,
851 diff_ms);
852 fallback_info->elapsed_ms += diff_ms;
853 }
854 }
855 fallback_info->is_active = is_active;
856 fallback_info->last_update_ms.emplace(now_ms);
857}
858
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100859void SendStatisticsProxy::UpdateFallbackDisabledStats(
860 const CodecSpecificInfo* codec_info,
Niels Möllerd3b8c632018-08-27 15:33:42 +0200861 int pixels,
862 int simulcast_index) {
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100863 if (!fallback_max_pixels_disabled_ ||
864 !uma_container_->fallback_info_disabled_.is_possible ||
865 stats_.has_entered_low_resolution) {
866 return;
867 }
868
Niels Möllerd3b8c632018-08-27 15:33:42 +0200869 if (!IsForcedFallbackPossible(codec_info, simulcast_index) ||
Erik Språnge2fd86a2018-10-24 11:32:39 +0200870 stats_.encoder_implementation_name == kVp8SwCodecName) {
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100871 uma_container_->fallback_info_disabled_.is_possible = false;
872 return;
873 }
874
875 if (pixels <= *fallback_max_pixels_disabled_ ||
876 uma_container_->fallback_info_disabled_.min_pixel_limit_reached) {
877 stats_.has_entered_low_resolution = true;
878 }
879}
880
881void SendStatisticsProxy::OnMinPixelLimitReached() {
882 rtc::CritScope lock(&crit_);
883 uma_container_->fallback_info_disabled_.min_pixel_limit_reached = true;
884}
885
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000886void SendStatisticsProxy::OnSendEncodedImage(
887 const EncodedImage& encoded_image,
kjellander02b3d272016-04-20 05:05:54 -0700888 const CodecSpecificInfo* codec_info) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200889 // Simulcast is used for VP8, H264 and Generic.
890 int simulcast_idx =
891 (codec_info && (codec_info->codecType == kVideoCodecVP8 ||
892 codec_info->codecType == kVideoCodecH264 ||
893 codec_info->codecType == kVideoCodecGeneric))
894 ? encoded_image.SpatialIndex().value_or(0)
895 : 0;
kjellander02b3d272016-04-20 05:05:54 -0700896
perkj275afc52016-09-01 00:21:16 -0700897 rtc::CritScope lock(&crit_);
sakal43536c32016-10-24 01:46:43 -0700898 ++stats_.frames_encoded;
Henrik Boström23aff9b2019-05-20 15:15:38 +0200899 // The current encode frame rate is based on previously encoded frames.
900 double encode_frame_rate = encoded_frame_rate_tracker_.ComputeRate();
901 // We assume that less than 1 FPS is not a trustworthy estimate - perhaps we
902 // just started encoding for the first time or after a pause. Assuming frame
903 // rate is at least 1 FPS is conservative to avoid too large increments.
904 if (encode_frame_rate < 1.0)
905 encode_frame_rate = 1.0;
906 double target_frame_size_bytes =
907 stats_.target_media_bitrate_bps / (8.0 * encode_frame_rate);
908 // |stats_.target_media_bitrate_bps| is set in
909 // SendStatisticsProxy::OnSetEncoderTargetRate.
910 stats_.total_encoded_bytes_target += round(target_frame_size_bytes);
kjellander02b3d272016-04-20 05:05:54 -0700911 if (codec_info) {
Erik Språnge2fd86a2018-10-24 11:32:39 +0200912 UpdateEncoderFallbackStats(
913 codec_info, encoded_image._encodedWidth * encoded_image._encodedHeight,
914 simulcast_idx);
kjellander02b3d272016-04-20 05:05:54 -0700915 }
916
Niels Möllerd3b8c632018-08-27 15:33:42 +0200917 if (static_cast<size_t>(simulcast_idx) >= rtp_config_.ssrcs.size()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100918 RTC_LOG(LS_ERROR) << "Encoded image outside simulcast range ("
919 << simulcast_idx << " >= " << rtp_config_.ssrcs.size()
920 << ").";
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000921 return;
922 }
perkj26091b12016-09-01 01:17:40 -0700923 uint32_t ssrc = rtp_config_.ssrcs[simulcast_idx];
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000924
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000925 VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc);
Peter Boström74f6e9e2016-04-04 17:56:10 +0200926 if (!stats)
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000927 return;
928
Sergey Silkinbb081a62018-09-04 18:34:22 +0200929 // Report resolution of top spatial layer in case of VP9 SVC.
930 bool is_svc_low_spatial_layer =
931 (codec_info && codec_info->codecType == kVideoCodecVP9)
932 ? !codec_info->codecSpecific.VP9.end_of_picture
933 : false;
934
935 if (!stats->width || !stats->height || !is_svc_low_spatial_layer) {
936 stats->width = encoded_image._encodedWidth;
937 stats->height = encoded_image._encodedHeight;
938 update_times_[ssrc].resolution_update_ms = clock_->TimeInMilliseconds();
939 }
asaperssond89920b2015-07-22 06:52:00 -0700940
sprangb4a1ae52015-12-03 08:10:08 -0800941 uma_container_->key_frame_counter_.Add(encoded_image._frameType ==
Niels Möller8f7ce222019-03-21 15:43:58 +0100942 VideoFrameType::kVideoFrameKey);
asapersson4306fc72015-10-19 00:35:21 -0700943
sakal87da4042016-10-31 06:53:47 -0700944 if (encoded_image.qp_ != -1) {
945 if (!stats_.qp_sum)
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100946 stats_.qp_sum = 0;
sakal87da4042016-10-31 06:53:47 -0700947 *stats_.qp_sum += encoded_image.qp_;
948
949 if (codec_info) {
950 if (codec_info->codecType == kVideoCodecVP8) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200951 int spatial_idx = (rtp_config_.ssrcs.size() == 1) ? -1 : simulcast_idx;
sakal87da4042016-10-31 06:53:47 -0700952 uma_container_->qp_counters_[spatial_idx].vp8.Add(encoded_image.qp_);
953 } else if (codec_info->codecType == kVideoCodecVP9) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200954 int spatial_idx = encoded_image.SpatialIndex().value_or(-1);
sakal87da4042016-10-31 06:53:47 -0700955 uma_container_->qp_counters_[spatial_idx].vp9.Add(encoded_image.qp_);
asapersson827cab32016-11-02 09:08:47 -0700956 } else if (codec_info->codecType == kVideoCodecH264) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200957 int spatial_idx = (rtp_config_.ssrcs.size() == 1) ? -1 : simulcast_idx;
asapersson827cab32016-11-02 09:08:47 -0700958 uma_container_->qp_counters_[spatial_idx].h264.Add(encoded_image.qp_);
sakal87da4042016-10-31 06:53:47 -0700959 }
asapersson5265fed2016-04-18 02:58:47 -0700960 }
asapersson118ef002016-03-31 00:00:19 -0700961 }
962
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100963 // If any of the simulcast streams have a huge frame, it should be counted
964 // as a single difficult input frame.
965 // https://w3c.github.io/webrtc-stats/#dom-rtcvideosenderstats-hugeframessent
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200966 if (encoded_image.timing_.flags & VideoSendTiming::kTriggeredBySize) {
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100967 if (!last_outlier_timestamp_ ||
968 *last_outlier_timestamp_ < encoded_image.capture_time_ms_) {
969 last_outlier_timestamp_.emplace(encoded_image.capture_time_ms_);
970 ++stats_.huge_frames_sent;
971 }
972 }
973
Niels Möller77536a22019-01-15 08:50:01 +0100974 media_byte_rate_tracker_.AddSamples(encoded_image.size());
Åsa Perssonaa329e72017-12-15 15:54:44 +0100975
976 // Initialize to current since |is_limited_in_resolution| is only updated
977 // when an encoded frame is removed from the EncodedFrameMap.
978 bool is_limited_in_resolution = stats_.bw_limited_resolution;
979 if (uma_container_->InsertEncodedFrame(encoded_image, simulcast_idx,
980 &is_limited_in_resolution)) {
Åsa Persson0122e842017-10-16 12:19:23 +0200981 encoded_frame_rate_tracker_.AddSamples(1);
Åsa Perssonaa329e72017-12-15 15:54:44 +0100982 }
983
984 stats_.bw_limited_resolution =
985 is_limited_in_resolution || quality_downscales_ > 0;
986
987 if (quality_downscales_ != -1) {
988 uma_container_->quality_limited_frame_counter_.Add(quality_downscales_ > 0);
989 if (quality_downscales_ > 0)
990 uma_container_->quality_downscales_counter_.Add(quality_downscales_);
991 }
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000992}
993
Erik Språnge2fd86a2018-10-24 11:32:39 +0200994void SendStatisticsProxy::OnEncoderImplementationChanged(
995 const std::string& implementation_name) {
996 rtc::CritScope lock(&crit_);
997 encoder_changed_ = EncoderChangeEvent{stats_.encoder_implementation_name,
998 implementation_name};
999 stats_.encoder_implementation_name = implementation_name;
1000}
1001
Niels Möller213618e2018-07-24 09:29:58 +02001002int SendStatisticsProxy::GetInputFrameRate() const {
1003 rtc::CritScope lock(&crit_);
1004 return round(uma_container_->input_frame_rate_tracker_.ComputeRate());
1005}
1006
Per69b332d2016-06-02 15:45:42 +02001007int SendStatisticsProxy::GetSendFrameRate() const {
1008 rtc::CritScope lock(&crit_);
Åsa Persson0122e842017-10-16 12:19:23 +02001009 return round(encoded_frame_rate_tracker_.ComputeRate());
Per69b332d2016-06-02 15:45:42 +02001010}
1011
asaperssond89920b2015-07-22 06:52:00 -07001012void SendStatisticsProxy::OnIncomingFrame(int width, int height) {
Peter Boströmf2f82832015-05-01 13:00:41 +02001013 rtc::CritScope lock(&crit_);
sprangb4a1ae52015-12-03 08:10:08 -08001014 uma_container_->input_frame_rate_tracker_.AddSamples(1);
asapersson320e45a2016-11-29 01:40:35 -08001015 uma_container_->input_fps_counter_.Add(1);
sprangb4a1ae52015-12-03 08:10:08 -08001016 uma_container_->input_width_counter_.Add(width);
1017 uma_container_->input_height_counter_.Add(height);
asaperssonf4e44af2017-04-19 02:01:06 -07001018 if (cpu_downscales_ >= 0) {
1019 uma_container_->cpu_limited_frame_counter_.Add(
1020 stats_.cpu_limited_resolution);
1021 }
Åsa Perssond29b54c2017-10-19 17:32:17 +02001022 if (encoded_frame_rate_tracker_.TotalSampleCount() == 0) {
1023 // Set start time now instead of when first key frame is encoded to avoid a
1024 // too high initial estimate.
1025 encoded_frame_rate_tracker_.AddSamples(0);
1026 }
perkj803d97f2016-11-01 11:45:46 -07001027}
1028
Niels Möller213618e2018-07-24 09:29:58 +02001029void SendStatisticsProxy::OnFrameDropped(DropReason reason) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +02001030 rtc::CritScope lock(&crit_);
Niels Möller213618e2018-07-24 09:29:58 +02001031 switch (reason) {
1032 case DropReason::kSource:
1033 ++stats_.frames_dropped_by_capturer;
1034 break;
1035 case DropReason::kEncoderQueue:
1036 ++stats_.frames_dropped_by_encoder_queue;
1037 break;
1038 case DropReason::kEncoder:
1039 ++stats_.frames_dropped_by_encoder;
1040 break;
1041 case DropReason::kMediaOptimization:
1042 ++stats_.frames_dropped_by_rate_limiter;
1043 break;
1044 }
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +02001045}
1046
Niels Möller213618e2018-07-24 09:29:58 +02001047void SendStatisticsProxy::OnAdaptationChanged(
1048 AdaptationReason reason,
1049 const AdaptationSteps& cpu_counts,
1050 const AdaptationSteps& quality_counts) {
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +02001051 rtc::CritScope lock(&crit_);
Niels Möller213618e2018-07-24 09:29:58 +02001052 switch (reason) {
1053 case AdaptationReason::kNone:
1054 SetAdaptTimer(cpu_counts, &uma_container_->cpu_adapt_timer_);
1055 SetAdaptTimer(quality_counts, &uma_container_->quality_adapt_timer_);
1056 break;
1057 case AdaptationReason::kCpu:
1058 ++stats_.number_of_cpu_adapt_changes;
1059 break;
1060 case AdaptationReason::kQuality:
1061 TryUpdateInitialQualityResolutionAdaptUp(quality_counts);
1062 ++stats_.number_of_quality_adapt_changes;
1063 break;
1064 }
asapersson09f05612017-05-15 23:40:18 -07001065 UpdateAdaptationStats(cpu_counts, quality_counts);
1066}
1067
1068void SendStatisticsProxy::UpdateAdaptationStats(
Niels Möller213618e2018-07-24 09:29:58 +02001069 const AdaptationSteps& cpu_counts,
1070 const AdaptationSteps& quality_counts) {
1071 cpu_downscales_ = cpu_counts.num_resolution_reductions.value_or(-1);
1072 quality_downscales_ = quality_counts.num_resolution_reductions.value_or(-1);
asapersson09f05612017-05-15 23:40:18 -07001073
Niels Möller213618e2018-07-24 09:29:58 +02001074 stats_.cpu_limited_resolution = cpu_counts.num_resolution_reductions > 0;
1075 stats_.cpu_limited_framerate = cpu_counts.num_framerate_reductions > 0;
1076 stats_.bw_limited_resolution = quality_counts.num_resolution_reductions > 0;
1077 stats_.bw_limited_framerate = quality_counts.num_framerate_reductions > 0;
asapersson09f05612017-05-15 23:40:18 -07001078}
1079
Åsa Persson875841d2018-01-08 08:49:53 +01001080// TODO(asapersson): Include fps changes.
1081void SendStatisticsProxy::OnInitialQualityResolutionAdaptDown() {
1082 rtc::CritScope lock(&crit_);
1083 ++uma_container_->initial_quality_changes_.down;
1084}
1085
1086void SendStatisticsProxy::TryUpdateInitialQualityResolutionAdaptUp(
Niels Möller213618e2018-07-24 09:29:58 +02001087 const AdaptationSteps& quality_counts) {
Åsa Persson875841d2018-01-08 08:49:53 +01001088 if (uma_container_->initial_quality_changes_.down == 0)
1089 return;
1090
1091 if (quality_downscales_ > 0 &&
Niels Möller213618e2018-07-24 09:29:58 +02001092 quality_counts.num_resolution_reductions.value_or(-1) <
1093 quality_downscales_) {
Åsa Persson875841d2018-01-08 08:49:53 +01001094 // Adapting up in quality.
1095 if (uma_container_->initial_quality_changes_.down >
1096 uma_container_->initial_quality_changes_.up) {
1097 ++uma_container_->initial_quality_changes_.up;
1098 }
1099 }
1100}
1101
Niels Möller213618e2018-07-24 09:29:58 +02001102void SendStatisticsProxy::SetAdaptTimer(const AdaptationSteps& counts,
1103 StatsTimer* timer) {
1104 if (counts.num_resolution_reductions || counts.num_framerate_reductions) {
asapersson09f05612017-05-15 23:40:18 -07001105 // Adaptation enabled.
1106 if (!stats_.suspended)
1107 timer->Start(clock_->TimeInMilliseconds());
1108 return;
1109 }
1110 timer->Stop(clock_->TimeInMilliseconds());
kthelgason876222f2016-11-29 01:44:11 -08001111}
1112
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00001113void SendStatisticsProxy::RtcpPacketTypesCounterUpdated(
1114 uint32_t ssrc,
1115 const RtcpPacketTypeCounter& packet_counter) {
Peter Boströmf2f82832015-05-01 13:00:41 +02001116 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001117 VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc);
Peter Boström74f6e9e2016-04-04 17:56:10 +02001118 if (!stats)
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00001119 return;
1120
1121 stats->rtcp_packet_type_counts = packet_counter;
sprang07fb9be2016-02-24 07:55:00 -08001122 if (uma_container_->first_rtcp_stats_time_ms_ == -1)
1123 uma_container_->first_rtcp_stats_time_ms_ = clock_->TimeInMilliseconds();
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00001124}
1125
sprang@webrtc.orgccd42842014-01-07 09:54:34 +00001126void SendStatisticsProxy::StatisticsUpdated(const RtcpStatistics& statistics,
1127 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +02001128 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001129 VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc);
Peter Boström74f6e9e2016-04-04 17:56:10 +02001130 if (!stats)
sprang@webrtc.orgccd42842014-01-07 09:54:34 +00001131 return;
1132
1133 stats->rtcp_stats = statistics;
sprange2d83d62016-02-19 09:03:26 -08001134 uma_container_->report_block_stats_.Store(statistics, 0, ssrc);
sprang@webrtc.orgccd42842014-01-07 09:54:34 +00001135}
1136
Peter Boströmd1d66ba2016-02-08 14:07:14 +01001137void SendStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +00001138
Henrik Boström87e3f9d2019-05-27 10:44:24 +02001139void SendStatisticsProxy::OnReportBlockDataUpdated(
1140 ReportBlockData report_block_data) {
1141 rtc::CritScope lock(&crit_);
1142 VideoSendStream::StreamStats* stats =
1143 GetStatsEntry(report_block_data.report_block().source_ssrc);
1144 if (!stats)
1145 return;
1146 stats->report_block_data = std::move(report_block_data);
1147}
1148
sprang@webrtc.orgccd42842014-01-07 09:54:34 +00001149void SendStatisticsProxy::DataCountersUpdated(
1150 const StreamDataCounters& counters,
1151 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +02001152 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001153 VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc);
asapersson93e1e232017-02-06 05:18:35 -08001154 RTC_DCHECK(stats) << "DataCountersUpdated reported for unknown ssrc " << ssrc;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +00001155
asaperssona6a699a2016-11-25 03:52:46 -08001156 if (stats->is_flexfec) {
1157 // The same counters are reported for both the media ssrc and flexfec ssrc.
1158 // Bitrate stats are summed for all SSRCs. Use fec stats from media update.
1159 return;
1160 }
1161
sprang@webrtc.orgccd42842014-01-07 09:54:34 +00001162 stats->rtp_stats = counters;
asapersson6eca98b2017-04-04 23:40:50 -07001163 if (uma_container_->first_rtp_stats_time_ms_ == -1) {
1164 int64_t now_ms = clock_->TimeInMilliseconds();
1165 uma_container_->first_rtp_stats_time_ms_ = now_ms;
asapersson09f05612017-05-15 23:40:18 -07001166 uma_container_->cpu_adapt_timer_.Restart(now_ms);
1167 uma_container_->quality_adapt_timer_.Restart(now_ms);
asapersson6eca98b2017-04-04 23:40:50 -07001168 }
asapersson93e1e232017-02-06 05:18:35 -08001169
1170 uma_container_->total_byte_counter_.Set(counters.transmitted.TotalBytes(),
1171 ssrc);
1172 uma_container_->padding_byte_counter_.Set(counters.transmitted.padding_bytes,
1173 ssrc);
1174 uma_container_->retransmit_byte_counter_.Set(
1175 counters.retransmitted.TotalBytes(), ssrc);
1176 uma_container_->fec_byte_counter_.Set(counters.fec.TotalBytes(), ssrc);
1177 if (stats->is_rtx) {
1178 uma_container_->rtx_byte_counter_.Set(counters.transmitted.TotalBytes(),
1179 ssrc);
1180 } else {
1181 uma_container_->media_byte_counter_.Set(counters.MediaPayloadBytes(), ssrc);
1182 }
sprang@webrtc.orgccd42842014-01-07 09:54:34 +00001183}
1184
sprangcd349d92016-07-13 09:11:28 -07001185void SendStatisticsProxy::Notify(uint32_t total_bitrate_bps,
1186 uint32_t retransmit_bitrate_bps,
sprang@webrtc.orgccd42842014-01-07 09:54:34 +00001187 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +02001188 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001189 VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc);
Peter Boström74f6e9e2016-04-04 17:56:10 +02001190 if (!stats)
sprang@webrtc.orgccd42842014-01-07 09:54:34 +00001191 return;
1192
sprangcd349d92016-07-13 09:11:28 -07001193 stats->total_bitrate_bps = total_bitrate_bps;
1194 stats->retransmit_bitrate_bps = retransmit_bitrate_bps;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +00001195}
1196
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +00001197void SendStatisticsProxy::FrameCountUpdated(const FrameCounts& frame_counts,
1198 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +02001199 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001200 VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc);
Peter Boström74f6e9e2016-04-04 17:56:10 +02001201 if (!stats)
sprang@webrtc.orgccd42842014-01-07 09:54:34 +00001202 return;
1203
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +00001204 stats->frame_counts = frame_counts;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +00001205}
1206
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001207void SendStatisticsProxy::SendSideDelayUpdated(int avg_delay_ms,
1208 int max_delay_ms,
Henrik Boström9fe18342019-05-16 18:38:20 +02001209 uint64_t total_delay_ms,
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001210 uint32_t ssrc) {
Peter Boströmf2f82832015-05-01 13:00:41 +02001211 rtc::CritScope lock(&crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001212 VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc);
Peter Boström74f6e9e2016-04-04 17:56:10 +02001213 if (!stats)
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001214 return;
1215 stats->avg_delay_ms = avg_delay_ms;
1216 stats->max_delay_ms = max_delay_ms;
Henrik Boström9fe18342019-05-16 18:38:20 +02001217 stats->total_packet_send_delay_ms = total_delay_ms;
asaperssonf040b232015-11-04 00:59:03 -08001218
sprangb4a1ae52015-12-03 08:10:08 -08001219 uma_container_->delay_counter_.Add(avg_delay_ms);
1220 uma_container_->max_delay_counter_.Add(max_delay_ms);
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001221}
1222
asapersson6eca98b2017-04-04 23:40:50 -07001223void SendStatisticsProxy::StatsTimer::Start(int64_t now_ms) {
1224 if (start_ms == -1)
1225 start_ms = now_ms;
1226}
1227
1228void SendStatisticsProxy::StatsTimer::Stop(int64_t now_ms) {
1229 if (start_ms != -1) {
1230 total_ms += now_ms - start_ms;
1231 start_ms = -1;
1232 }
1233}
1234
1235void SendStatisticsProxy::StatsTimer::Restart(int64_t now_ms) {
1236 total_ms = 0;
1237 if (start_ms != -1)
1238 start_ms = now_ms;
1239}
1240
asaperssond89920b2015-07-22 06:52:00 -07001241void SendStatisticsProxy::SampleCounter::Add(int sample) {
1242 sum += sample;
1243 ++num_samples;
1244}
1245
asapersson66d4b372016-12-19 06:50:53 -08001246int SendStatisticsProxy::SampleCounter::Avg(
1247 int64_t min_required_samples) const {
asaperssond89920b2015-07-22 06:52:00 -07001248 if (num_samples < min_required_samples || num_samples == 0)
1249 return -1;
asapersson66d4b372016-12-19 06:50:53 -08001250 return static_cast<int>((sum + (num_samples / 2)) / num_samples);
asaperssond89920b2015-07-22 06:52:00 -07001251}
1252
asaperssondec5ebf2015-10-05 02:36:17 -07001253void SendStatisticsProxy::BoolSampleCounter::Add(bool sample) {
1254 if (sample)
1255 ++sum;
1256 ++num_samples;
1257}
1258
asapersson66d4b372016-12-19 06:50:53 -08001259void SendStatisticsProxy::BoolSampleCounter::Add(bool sample, int64_t count) {
1260 if (sample)
1261 sum += count;
1262 num_samples += count;
1263}
asaperssondec5ebf2015-10-05 02:36:17 -07001264int SendStatisticsProxy::BoolSampleCounter::Percent(
asapersson66d4b372016-12-19 06:50:53 -08001265 int64_t min_required_samples) const {
asaperssondec5ebf2015-10-05 02:36:17 -07001266 return Fraction(min_required_samples, 100.0f);
1267}
1268
1269int SendStatisticsProxy::BoolSampleCounter::Permille(
asapersson66d4b372016-12-19 06:50:53 -08001270 int64_t min_required_samples) const {
asaperssondec5ebf2015-10-05 02:36:17 -07001271 return Fraction(min_required_samples, 1000.0f);
1272}
1273
1274int SendStatisticsProxy::BoolSampleCounter::Fraction(
asapersson66d4b372016-12-19 06:50:53 -08001275 int64_t min_required_samples,
1276 float multiplier) const {
asaperssondec5ebf2015-10-05 02:36:17 -07001277 if (num_samples < min_required_samples || num_samples == 0)
1278 return -1;
1279 return static_cast<int>((sum * multiplier / num_samples) + 0.5f);
1280}
sprang@webrtc.orgccd42842014-01-07 09:54:34 +00001281} // namespace webrtc