sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/video/send_statistics_proxy.h" |
| 12 | |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 13 | #include <algorithm> |
Tim Psiaki | ad13d2f | 2015-11-10 16:34:50 -0800 | [diff] [blame] | 14 | #include <cmath> |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 15 | #include <map> |
| 16 | |
pbos@webrtc.org | 49096de | 2015-02-24 22:37:52 +0000 | [diff] [blame] | 17 | #include "webrtc/base/checks.h" |
Peter Boström | 415d2cd | 2015-10-26 11:35:17 +0100 | [diff] [blame] | 18 | #include "webrtc/base/logging.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 19 | #include "webrtc/system_wrappers/include/metrics.h" |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
asapersson | 2a0a2a4 | 2015-10-27 01:32:00 -0700 | [diff] [blame] | 22 | namespace { |
asapersson | 1aa420b | 2015-12-07 03:12:22 -0800 | [diff] [blame] | 23 | const float kEncodeTimeWeigthFactor = 0.5f; |
| 24 | |
asapersson | 2a0a2a4 | 2015-10-27 01:32:00 -0700 | [diff] [blame] | 25 | // Used by histograms. Values of entries should not be changed. |
| 26 | enum HistogramCodecType { |
| 27 | kVideoUnknown = 0, |
| 28 | kVideoVp8 = 1, |
| 29 | kVideoVp9 = 2, |
| 30 | kVideoH264 = 3, |
| 31 | kVideoMax = 64, |
| 32 | }; |
| 33 | |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 34 | const char* kRealtimePrefix = "WebRTC.Video."; |
| 35 | const char* kScreenPrefix = "WebRTC.Video.Screenshare."; |
| 36 | |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 37 | const char* GetUmaPrefix(VideoEncoderConfig::ContentType content_type) { |
| 38 | switch (content_type) { |
| 39 | case VideoEncoderConfig::ContentType::kRealtimeVideo: |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 40 | return kRealtimePrefix; |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 41 | case VideoEncoderConfig::ContentType::kScreen: |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 42 | return kScreenPrefix; |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 43 | } |
| 44 | RTC_NOTREACHED(); |
| 45 | return nullptr; |
| 46 | } |
| 47 | |
asapersson | 2a0a2a4 | 2015-10-27 01:32:00 -0700 | [diff] [blame] | 48 | HistogramCodecType PayloadNameToHistogramCodecType( |
| 49 | const std::string& payload_name) { |
| 50 | if (payload_name == "VP8") { |
| 51 | return kVideoVp8; |
| 52 | } else if (payload_name == "VP9") { |
| 53 | return kVideoVp9; |
| 54 | } else if (payload_name == "H264") { |
| 55 | return kVideoH264; |
| 56 | } else { |
| 57 | return kVideoUnknown; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void UpdateCodecTypeHistogram(const std::string& payload_name) { |
asapersson | 28ba927 | 2016-01-25 05:58:23 -0800 | [diff] [blame] | 62 | RTC_HISTOGRAM_ENUMERATION("WebRTC.Video.Encoder.CodecType", |
| 63 | PayloadNameToHistogramCodecType(payload_name), |
| 64 | kVideoMax); |
asapersson | 2a0a2a4 | 2015-10-27 01:32:00 -0700 | [diff] [blame] | 65 | } |
| 66 | } // namespace |
| 67 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 68 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 69 | const int SendStatisticsProxy::kStatsTimeoutMs = 5000; |
| 70 | |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 71 | SendStatisticsProxy::SendStatisticsProxy( |
| 72 | Clock* clock, |
| 73 | const VideoSendStream::Config& config, |
| 74 | VideoEncoderConfig::ContentType content_type) |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 75 | : clock_(clock), |
| 76 | config_(config), |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 77 | content_type_(content_type), |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 78 | last_sent_frame_timestamp_(0), |
asapersson | 1aa420b | 2015-12-07 03:12:22 -0800 | [diff] [blame] | 79 | encode_time_(kEncodeTimeWeigthFactor), |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 80 | uma_container_(new UmaSamplesContainer(GetUmaPrefix(content_type_))) { |
asapersson | 2a0a2a4 | 2015-10-27 01:32:00 -0700 | [diff] [blame] | 81 | UpdateCodecTypeHistogram(config_.encoder_settings.payload_name); |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 82 | } |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 83 | |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 84 | SendStatisticsProxy::~SendStatisticsProxy() {} |
| 85 | |
| 86 | SendStatisticsProxy::UmaSamplesContainer::UmaSamplesContainer( |
| 87 | const char* prefix) |
| 88 | : uma_prefix_(prefix), |
| 89 | max_sent_width_per_timestamp_(0), |
| 90 | max_sent_height_per_timestamp_(0), |
| 91 | input_frame_rate_tracker_(100u, 10u), |
| 92 | sent_frame_rate_tracker_(100u, 10u) {} |
| 93 | |
| 94 | SendStatisticsProxy::UmaSamplesContainer::~UmaSamplesContainer() { |
Åsa Persson | 24b4eda | 2015-06-16 10:17:01 +0200 | [diff] [blame] | 95 | UpdateHistograms(); |
| 96 | } |
| 97 | |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 98 | void SendStatisticsProxy::UmaSamplesContainer::UpdateHistograms() { |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 99 | RTC_DCHECK(uma_prefix_ == kRealtimePrefix || uma_prefix_ == kScreenPrefix); |
| 100 | const int kIndex = uma_prefix_ == kScreenPrefix ? 1 : 0; |
asapersson | 6718e97 | 2015-07-24 00:20:58 -0700 | [diff] [blame] | 101 | const int kMinRequiredSamples = 200; |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 102 | int in_width = input_width_counter_.Avg(kMinRequiredSamples); |
| 103 | int in_height = input_height_counter_.Avg(kMinRequiredSamples); |
asapersson | 6f14be8 | 2015-11-16 00:40:49 -0800 | [diff] [blame] | 104 | int in_fps = round(input_frame_rate_tracker_.ComputeTotalRate()); |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 105 | if (in_width != -1) { |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 106 | RTC_HISTOGRAMS_COUNTS_10000(kIndex, uma_prefix_ + "InputWidthInPixels", |
| 107 | in_width); |
| 108 | RTC_HISTOGRAMS_COUNTS_10000(kIndex, uma_prefix_ + "InputHeightInPixels", |
| 109 | in_height); |
| 110 | RTC_HISTOGRAMS_COUNTS_100(kIndex, uma_prefix_ + "InputFramesPerSecond", |
| 111 | in_fps); |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 112 | } |
| 113 | int sent_width = sent_width_counter_.Avg(kMinRequiredSamples); |
| 114 | int sent_height = sent_height_counter_.Avg(kMinRequiredSamples); |
asapersson | 6f14be8 | 2015-11-16 00:40:49 -0800 | [diff] [blame] | 115 | int sent_fps = round(sent_frame_rate_tracker_.ComputeTotalRate()); |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 116 | if (sent_width != -1) { |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 117 | RTC_HISTOGRAMS_COUNTS_10000(kIndex, uma_prefix_ + "SentWidthInPixels", |
| 118 | sent_width); |
| 119 | RTC_HISTOGRAMS_COUNTS_10000(kIndex, uma_prefix_ + "SentHeightInPixels", |
| 120 | sent_height); |
| 121 | RTC_HISTOGRAMS_COUNTS_100(kIndex, uma_prefix_ + "SentFramesPerSecond", |
| 122 | sent_fps); |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 123 | } |
asapersson | 6718e97 | 2015-07-24 00:20:58 -0700 | [diff] [blame] | 124 | int encode_ms = encode_time_counter_.Avg(kMinRequiredSamples); |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 125 | if (encode_ms != -1) { |
| 126 | RTC_HISTOGRAMS_COUNTS_1000(kIndex, uma_prefix_ + "EncodeTimeInMs", |
| 127 | encode_ms); |
| 128 | } |
asapersson | dec5ebf | 2015-10-05 02:36:17 -0700 | [diff] [blame] | 129 | int key_frames_permille = key_frame_counter_.Permille(kMinRequiredSamples); |
| 130 | if (key_frames_permille != -1) { |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 131 | RTC_HISTOGRAMS_COUNTS_1000(kIndex, uma_prefix_ + "KeyFramesSentInPermille", |
| 132 | key_frames_permille); |
asapersson | dec5ebf | 2015-10-05 02:36:17 -0700 | [diff] [blame] | 133 | } |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 134 | int quality_limited = |
| 135 | quality_limited_frame_counter_.Percent(kMinRequiredSamples); |
| 136 | if (quality_limited != -1) { |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 137 | RTC_HISTOGRAMS_PERCENTAGE(kIndex, |
| 138 | uma_prefix_ + "QualityLimitedResolutionInPercent", |
| 139 | quality_limited); |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 140 | } |
| 141 | int downscales = quality_downscales_counter_.Avg(kMinRequiredSamples); |
| 142 | if (downscales != -1) { |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 143 | RTC_HISTOGRAMS_ENUMERATION( |
| 144 | kIndex, uma_prefix_ + "QualityLimitedResolutionDownscales", downscales, |
| 145 | 20); |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 146 | } |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 147 | int bw_limited = bw_limited_frame_counter_.Percent(kMinRequiredSamples); |
| 148 | if (bw_limited != -1) { |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 149 | RTC_HISTOGRAMS_PERCENTAGE( |
| 150 | kIndex, uma_prefix_ + "BandwidthLimitedResolutionInPercent", |
| 151 | bw_limited); |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 152 | } |
| 153 | int num_disabled = bw_resolutions_disabled_counter_.Avg(kMinRequiredSamples); |
| 154 | if (num_disabled != -1) { |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 155 | RTC_HISTOGRAMS_ENUMERATION( |
| 156 | kIndex, uma_prefix_ + "BandwidthLimitedResolutionsDisabled", |
| 157 | num_disabled, 10); |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 158 | } |
asapersson | f040b23 | 2015-11-04 00:59:03 -0800 | [diff] [blame] | 159 | int delay_ms = delay_counter_.Avg(kMinRequiredSamples); |
| 160 | if (delay_ms != -1) |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 161 | RTC_HISTOGRAMS_COUNTS_100000(kIndex, uma_prefix_ + "SendSideDelayInMs", |
| 162 | delay_ms); |
asapersson | f040b23 | 2015-11-04 00:59:03 -0800 | [diff] [blame] | 163 | |
| 164 | int max_delay_ms = max_delay_counter_.Avg(kMinRequiredSamples); |
| 165 | if (max_delay_ms != -1) { |
asapersson | c2148a5 | 2016-02-04 00:33:21 -0800 | [diff] [blame] | 166 | RTC_HISTOGRAMS_COUNTS_100000(kIndex, uma_prefix_ + "SendSideDelayMaxInMs", |
| 167 | max_delay_ms); |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | void SendStatisticsProxy::SetContentType( |
| 172 | VideoEncoderConfig::ContentType content_type) { |
| 173 | rtc::CritScope lock(&crit_); |
| 174 | if (content_type_ != content_type) { |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 175 | uma_container_.reset(new UmaSamplesContainer(GetUmaPrefix(content_type))); |
| 176 | content_type_ = content_type; |
asapersson | f040b23 | 2015-11-04 00:59:03 -0800 | [diff] [blame] | 177 | } |
Åsa Persson | 24b4eda | 2015-06-16 10:17:01 +0200 | [diff] [blame] | 178 | } |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 179 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 180 | void SendStatisticsProxy::OnEncoderImplementationName( |
| 181 | const char* implementation_name) { |
| 182 | rtc::CritScope lock(&crit_); |
| 183 | stats_.encoder_implementation_name = implementation_name; |
| 184 | } |
| 185 | |
Peter Boström | 7083e11 | 2015-09-22 16:28:51 +0200 | [diff] [blame] | 186 | void SendStatisticsProxy::OnOutgoingRate(uint32_t framerate, uint32_t bitrate) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 187 | rtc::CritScope lock(&crit_); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 188 | stats_.encode_frame_rate = framerate; |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 189 | stats_.media_bitrate_bps = bitrate; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 192 | void SendStatisticsProxy::OnEncodedFrameTimeMeasured( |
| 193 | int encode_time_ms, |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 194 | const CpuOveruseMetrics& metrics) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 195 | rtc::CritScope lock(&crit_); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 196 | uma_container_->encode_time_counter_.Add(encode_time_ms); |
| 197 | encode_time_.Apply(1.0f, encode_time_ms); |
| 198 | stats_.avg_encode_time_ms = round(encode_time_.filtered()); |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 199 | stats_.encode_usage_percent = metrics.encode_usage_percent; |
| 200 | } |
| 201 | |
Peter Boström | 7083e11 | 2015-09-22 16:28:51 +0200 | [diff] [blame] | 202 | void SendStatisticsProxy::OnSuspendChange(bool is_suspended) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 203 | rtc::CritScope lock(&crit_); |
henrik.lundin@webrtc.org | b10363f | 2014-03-13 13:31:21 +0000 | [diff] [blame] | 204 | stats_.suspended = is_suspended; |
| 205 | } |
| 206 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 207 | VideoSendStream::Stats SendStatisticsProxy::GetStats() { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 208 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 209 | PurgeOldStats(); |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 210 | stats_.input_frame_rate = |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 211 | round(uma_container_->input_frame_rate_tracker_.ComputeRate()); |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 212 | return stats_; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 213 | } |
| 214 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 215 | void SendStatisticsProxy::PurgeOldStats() { |
Peter Boström | 20f3f94 | 2015-05-15 11:33:39 +0200 | [diff] [blame] | 216 | int64_t old_stats_ms = clock_->TimeInMilliseconds() - kStatsTimeoutMs; |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 217 | for (std::map<uint32_t, VideoSendStream::StreamStats>::iterator it = |
| 218 | stats_.substreams.begin(); |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 219 | it != stats_.substreams.end(); ++it) { |
| 220 | uint32_t ssrc = it->first; |
Peter Boström | 20f3f94 | 2015-05-15 11:33:39 +0200 | [diff] [blame] | 221 | if (update_times_[ssrc].resolution_update_ms <= old_stats_ms) { |
| 222 | it->second.width = 0; |
| 223 | it->second.height = 0; |
| 224 | } |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 228 | VideoSendStream::StreamStats* SendStatisticsProxy::GetStatsEntry( |
| 229 | uint32_t ssrc) { |
| 230 | std::map<uint32_t, VideoSendStream::StreamStats>::iterator it = |
| 231 | stats_.substreams.find(ssrc); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 232 | if (it != stats_.substreams.end()) |
| 233 | return &it->second; |
| 234 | |
| 235 | if (std::find(config_.rtp.ssrcs.begin(), config_.rtp.ssrcs.end(), ssrc) == |
stefan@webrtc.org | 58e2d26 | 2014-08-14 15:10:49 +0000 | [diff] [blame] | 236 | config_.rtp.ssrcs.end() && |
| 237 | std::find(config_.rtp.rtx.ssrcs.begin(), |
| 238 | config_.rtp.rtx.ssrcs.end(), |
| 239 | ssrc) == config_.rtp.rtx.ssrcs.end()) { |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 240 | return nullptr; |
stefan@webrtc.org | 58e2d26 | 2014-08-14 15:10:49 +0000 | [diff] [blame] | 241 | } |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 242 | |
| 243 | return &stats_.substreams[ssrc]; // Insert new entry and return ptr. |
| 244 | } |
| 245 | |
Peter Boström | 20f3f94 | 2015-05-15 11:33:39 +0200 | [diff] [blame] | 246 | void SendStatisticsProxy::OnInactiveSsrc(uint32_t ssrc) { |
| 247 | rtc::CritScope lock(&crit_); |
| 248 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
| 249 | if (stats == nullptr) |
| 250 | return; |
| 251 | |
| 252 | stats->total_bitrate_bps = 0; |
| 253 | stats->retransmit_bitrate_bps = 0; |
| 254 | stats->height = 0; |
| 255 | stats->width = 0; |
| 256 | } |
| 257 | |
pbos@webrtc.org | 891d483 | 2015-02-26 13:15:22 +0000 | [diff] [blame] | 258 | void SendStatisticsProxy::OnSetRates(uint32_t bitrate_bps, int framerate) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 259 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 891d483 | 2015-02-26 13:15:22 +0000 | [diff] [blame] | 260 | stats_.target_media_bitrate_bps = bitrate_bps; |
| 261 | } |
| 262 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 263 | void SendStatisticsProxy::OnSendEncodedImage( |
| 264 | const EncodedImage& encoded_image, |
| 265 | const RTPVideoHeader* rtp_video_header) { |
| 266 | size_t simulcast_idx = |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 267 | rtp_video_header != nullptr ? rtp_video_header->simulcastIdx : 0; |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 268 | if (simulcast_idx >= config_.rtp.ssrcs.size()) { |
| 269 | LOG(LS_ERROR) << "Encoded image outside simulcast range (" << simulcast_idx |
| 270 | << " >= " << config_.rtp.ssrcs.size() << ")."; |
| 271 | return; |
| 272 | } |
| 273 | uint32_t ssrc = config_.rtp.ssrcs[simulcast_idx]; |
| 274 | |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 275 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 276 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 277 | if (stats == nullptr) |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 278 | return; |
| 279 | |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 280 | stats->width = encoded_image._encodedWidth; |
| 281 | stats->height = encoded_image._encodedHeight; |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 282 | update_times_[ssrc].resolution_update_ms = clock_->TimeInMilliseconds(); |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 283 | |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 284 | uma_container_->key_frame_counter_.Add(encoded_image._frameType == |
| 285 | kVideoFrameKey); |
asapersson | dec5ebf | 2015-10-05 02:36:17 -0700 | [diff] [blame] | 286 | |
asapersson | 17821db | 2015-12-14 02:08:12 -0800 | [diff] [blame] | 287 | stats_.bw_limited_resolution = |
| 288 | encoded_image.adapt_reason_.quality_resolution_downscales > 0 || |
| 289 | encoded_image.adapt_reason_.bw_resolutions_disabled > 0; |
| 290 | |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 291 | if (encoded_image.adapt_reason_.quality_resolution_downscales != -1) { |
| 292 | bool downscaled = |
| 293 | encoded_image.adapt_reason_.quality_resolution_downscales > 0; |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 294 | uma_container_->quality_limited_frame_counter_.Add(downscaled); |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 295 | if (downscaled) { |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 296 | uma_container_->quality_downscales_counter_.Add( |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 297 | encoded_image.adapt_reason_.quality_resolution_downscales); |
| 298 | } |
| 299 | } |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 300 | if (encoded_image.adapt_reason_.bw_resolutions_disabled != -1) { |
| 301 | bool bw_limited = encoded_image.adapt_reason_.bw_resolutions_disabled > 0; |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 302 | uma_container_->bw_limited_frame_counter_.Add(bw_limited); |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 303 | if (bw_limited) { |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 304 | uma_container_->bw_resolutions_disabled_counter_.Add( |
| 305 | encoded_image.adapt_reason_.bw_resolutions_disabled); |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 308 | |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 309 | // TODO(asapersson): This is incorrect if simulcast layers are encoded on |
| 310 | // different threads and there is no guarantee that one frame of all layers |
| 311 | // are encoded before the next start. |
| 312 | if (last_sent_frame_timestamp_ > 0 && |
| 313 | encoded_image._timeStamp != last_sent_frame_timestamp_) { |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 314 | uma_container_->sent_frame_rate_tracker_.AddSamples(1); |
| 315 | uma_container_->sent_width_counter_.Add( |
| 316 | uma_container_->max_sent_width_per_timestamp_); |
| 317 | uma_container_->sent_height_counter_.Add( |
| 318 | uma_container_->max_sent_height_per_timestamp_); |
| 319 | uma_container_->max_sent_width_per_timestamp_ = 0; |
| 320 | uma_container_->max_sent_height_per_timestamp_ = 0; |
Åsa Persson | 24b4eda | 2015-06-16 10:17:01 +0200 | [diff] [blame] | 321 | } |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 322 | last_sent_frame_timestamp_ = encoded_image._timeStamp; |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 323 | uma_container_->max_sent_width_per_timestamp_ = |
| 324 | std::max(uma_container_->max_sent_width_per_timestamp_, |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 325 | static_cast<int>(encoded_image._encodedWidth)); |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 326 | uma_container_->max_sent_height_per_timestamp_ = |
| 327 | std::max(uma_container_->max_sent_height_per_timestamp_, |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 328 | static_cast<int>(encoded_image._encodedHeight)); |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 329 | } |
| 330 | |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 331 | void SendStatisticsProxy::OnIncomingFrame(int width, int height) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 332 | rtc::CritScope lock(&crit_); |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 333 | uma_container_->input_frame_rate_tracker_.AddSamples(1); |
| 334 | uma_container_->input_width_counter_.Add(width); |
| 335 | uma_container_->input_height_counter_.Add(height); |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 336 | } |
| 337 | |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 338 | void SendStatisticsProxy::RtcpPacketTypesCounterUpdated( |
| 339 | uint32_t ssrc, |
| 340 | const RtcpPacketTypeCounter& packet_counter) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 341 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 342 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 343 | if (stats == nullptr) |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 344 | return; |
| 345 | |
| 346 | stats->rtcp_packet_type_counts = packet_counter; |
| 347 | } |
| 348 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 349 | void SendStatisticsProxy::StatisticsUpdated(const RtcpStatistics& statistics, |
| 350 | uint32_t ssrc) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 351 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 352 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 353 | if (stats == nullptr) |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 354 | return; |
| 355 | |
| 356 | stats->rtcp_stats = statistics; |
| 357 | } |
| 358 | |
Peter Boström | d1d66ba | 2016-02-08 14:07:14 +0100 | [diff] [blame] | 359 | void SendStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) {} |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 360 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 361 | void SendStatisticsProxy::DataCountersUpdated( |
| 362 | const StreamDataCounters& counters, |
| 363 | uint32_t ssrc) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 364 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 365 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 366 | RTC_DCHECK(stats != nullptr) |
| 367 | << "DataCountersUpdated reported for unknown ssrc: " << ssrc; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 368 | |
| 369 | stats->rtp_stats = counters; |
| 370 | } |
| 371 | |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 372 | void SendStatisticsProxy::Notify(const BitrateStatistics& total_stats, |
| 373 | const BitrateStatistics& retransmit_stats, |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 374 | uint32_t ssrc) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 375 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 376 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 377 | if (stats == nullptr) |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 378 | return; |
| 379 | |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 380 | stats->total_bitrate_bps = total_stats.bitrate_bps; |
| 381 | stats->retransmit_bitrate_bps = retransmit_stats.bitrate_bps; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 382 | } |
| 383 | |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 384 | void SendStatisticsProxy::FrameCountUpdated(const FrameCounts& frame_counts, |
| 385 | uint32_t ssrc) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 386 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 387 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 388 | if (stats == nullptr) |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 389 | return; |
| 390 | |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 391 | stats->frame_counts = frame_counts; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 392 | } |
| 393 | |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 394 | void SendStatisticsProxy::SendSideDelayUpdated(int avg_delay_ms, |
| 395 | int max_delay_ms, |
| 396 | uint32_t ssrc) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 397 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 398 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 399 | if (stats == nullptr) |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 400 | return; |
| 401 | stats->avg_delay_ms = avg_delay_ms; |
| 402 | stats->max_delay_ms = max_delay_ms; |
asapersson | f040b23 | 2015-11-04 00:59:03 -0800 | [diff] [blame] | 403 | |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 404 | uma_container_->delay_counter_.Add(avg_delay_ms); |
| 405 | uma_container_->max_delay_counter_.Add(max_delay_ms); |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 406 | } |
| 407 | |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 408 | void SendStatisticsProxy::SampleCounter::Add(int sample) { |
| 409 | sum += sample; |
| 410 | ++num_samples; |
| 411 | } |
| 412 | |
| 413 | int SendStatisticsProxy::SampleCounter::Avg(int min_required_samples) const { |
| 414 | if (num_samples < min_required_samples || num_samples == 0) |
| 415 | return -1; |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 416 | return (sum + (num_samples / 2)) / num_samples; |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 417 | } |
| 418 | |
asapersson | dec5ebf | 2015-10-05 02:36:17 -0700 | [diff] [blame] | 419 | void SendStatisticsProxy::BoolSampleCounter::Add(bool sample) { |
| 420 | if (sample) |
| 421 | ++sum; |
| 422 | ++num_samples; |
| 423 | } |
| 424 | |
| 425 | int SendStatisticsProxy::BoolSampleCounter::Percent( |
| 426 | int min_required_samples) const { |
| 427 | return Fraction(min_required_samples, 100.0f); |
| 428 | } |
| 429 | |
| 430 | int SendStatisticsProxy::BoolSampleCounter::Permille( |
| 431 | int min_required_samples) const { |
| 432 | return Fraction(min_required_samples, 1000.0f); |
| 433 | } |
| 434 | |
| 435 | int SendStatisticsProxy::BoolSampleCounter::Fraction( |
| 436 | int min_required_samples, float multiplier) const { |
| 437 | if (num_samples < min_required_samples || num_samples == 0) |
| 438 | return -1; |
| 439 | return static_cast<int>((sum * multiplier / num_samples) + 0.5f); |
| 440 | } |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 441 | } // namespace webrtc |