| 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 "video/receive_statistics_proxy.h" |
| 12 |
| 13 | #include <algorithm> |
| 14 | #include <cmath> |
| 15 | #include <utility> |
| 16 |
| 17 | #include "modules/video_coding/include/video_codec_interface.h" |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/logging.h" |
| 20 | #include "rtc_base/strings/string_builder.h" |
| 21 | #include "rtc_base/time_utils.h" |
| 22 | #include "system_wrappers/include/clock.h" |
| 23 | #include "system_wrappers/include/field_trial.h" |
| 24 | #include "system_wrappers/include/metrics.h" |
| 25 |
| 26 | namespace webrtc { |
| 27 | namespace { |
| 28 | // Periodic time interval for processing samples for `freq_offset_counter_`. |
| 29 | const int64_t kFreqOffsetProcessIntervalMs = 40000; |
| 30 |
| 31 | // Configuration for bad call detection. |
| 32 | const int kBadCallMinRequiredSamples = 10; |
| 33 | const int kMinSampleLengthMs = 990; |
| 34 | const int kNumMeasurements = 10; |
| 35 | const int kNumMeasurementsVariance = kNumMeasurements * 1.5; |
| 36 | const float kBadFraction = 0.8f; |
| 37 | // For fps: |
| 38 | // Low means low enough to be bad, high means high enough to be good |
| 39 | const int kLowFpsThreshold = 12; |
| 40 | const int kHighFpsThreshold = 14; |
| 41 | // For qp and fps variance: |
| 42 | // Low means low enough to be good, high means high enough to be bad |
| 43 | const int kLowQpThresholdVp8 = 60; |
| 44 | const int kHighQpThresholdVp8 = 70; |
| 45 | const int kLowVarianceThreshold = 1; |
| 46 | const int kHighVarianceThreshold = 2; |
| 47 |
| 48 | // Some metrics are reported as a maximum over this period. |
| 49 | // This should be synchronized with a typical getStats polling interval in |
| 50 | // the clients. |
| 51 | const int kMovingMaxWindowMs = 1000; |
| 52 |
| 53 | // How large window we use to calculate the framerate/bitrate. |
| 54 | const int kRateStatisticsWindowSizeMs = 1000; |
| 55 |
| 56 | // Some sane ballpark estimate for maximum common value of inter-frame delay. |
| 57 | // Values below that will be stored explicitly in the array, |
| 58 | // values above - in the map. |
| 59 | const int kMaxCommonInterframeDelayMs = 500; |
| 60 |
| 61 | const char* UmaPrefixForContentType(VideoContentType content_type) { |
| 62 | if (videocontenttypehelpers::IsScreenshare(content_type)) |
| 63 | return "WebRTC.Video.Screenshare"; |
| 64 | return "WebRTC.Video"; |
| 65 | } |
| 66 |
| 67 | std::string UmaSuffixForContentType(VideoContentType content_type) { |
| 68 | char ss_buf[1024]; |
| 69 | rtc::SimpleStringBuilder ss(ss_buf); |
| 70 | int simulcast_id = videocontenttypehelpers::GetSimulcastId(content_type); |
| 71 | if (simulcast_id > 0) { |
| 72 | ss << ".S" << simulcast_id - 1; |
| 73 | } |
| 74 | int experiment_id = videocontenttypehelpers::GetExperimentId(content_type); |
| 75 | if (experiment_id > 0) { |
| 76 | ss << ".ExperimentGroup" << experiment_id - 1; |
| 77 | } |
| 78 | return ss.str(); |
| 79 | } |
| 80 |
| 81 | } // namespace |
| 82 |
| 83 | ReceiveStatisticsProxy::ReceiveStatisticsProxy(uint32_t remote_ssrc, |
| 84 | Clock* clock) |
| 85 | : clock_(clock), |
| 86 | start_ms_(clock->TimeInMilliseconds()), |
| 87 | enable_decode_time_histograms_( |
| 88 | !field_trial::IsEnabled("WebRTC-DecodeTimeHistogramsKillSwitch")), |
| 89 | last_sample_time_(clock->TimeInMilliseconds()), |
| 90 | fps_threshold_(kLowFpsThreshold, |
| 91 | kHighFpsThreshold, |
| 92 | kBadFraction, |
| 93 | kNumMeasurements), |
| 94 | qp_threshold_(kLowQpThresholdVp8, |
| 95 | kHighQpThresholdVp8, |
| 96 | kBadFraction, |
| 97 | kNumMeasurements), |
| 98 | variance_threshold_(kLowVarianceThreshold, |
| 99 | kHighVarianceThreshold, |
| 100 | kBadFraction, |
| 101 | kNumMeasurementsVariance), |
| 102 | num_bad_states_(0), |
| 103 | num_certain_states_(0), |
| 104 | // 1000ms window, scale 1000 for ms to s. |
| 105 | decode_fps_estimator_(1000, 1000), |
| 106 | renders_fps_estimator_(1000, 1000), |
| 107 | render_fps_tracker_(100, 10u), |
| 108 | render_pixel_tracker_(100, 10u), |
| 109 | video_quality_observer_( |
| 110 | new VideoQualityObserver(VideoContentType::UNSPECIFIED)), |
| 111 | interframe_delay_max_moving_(kMovingMaxWindowMs), |
| 112 | freq_offset_counter_(clock, nullptr, kFreqOffsetProcessIntervalMs), |
| 113 | avg_rtt_ms_(0), |
| 114 | last_content_type_(VideoContentType::UNSPECIFIED), |
| 115 | last_codec_type_(kVideoCodecVP8), |
| 116 | num_delayed_frames_rendered_(0), |
| 117 | sum_missed_render_deadline_ms_(0), |
| 118 | timing_frame_info_counter_(kMovingMaxWindowMs) { |
| 119 | decode_thread_.Detach(); |
| 120 | network_thread_.Detach(); |
| 121 | stats_.ssrc = remote_ssrc; |
| 122 | } |
| 123 |
| 124 | void ReceiveStatisticsProxy::UpdateHistograms( |
| 125 | absl::optional<int> fraction_lost, |
| 126 | const StreamDataCounters& rtp_stats, |
| 127 | const StreamDataCounters* rtx_stats) { |
| 128 | // Not actually running on the decoder thread, but must be called after |
| 129 | // DecoderThreadStopped, which detaches the thread checker. It is therefore |
| 130 | // safe to access `qp_counters_`, which were updated on the decode thread |
| 131 | // earlier. |
| 132 | RTC_DCHECK_RUN_ON(&decode_thread_); |
| 133 |
| 134 | MutexLock lock(&mutex_); |
| 135 |
| 136 | char log_stream_buf[8 * 1024]; |
| 137 | rtc::SimpleStringBuilder log_stream(log_stream_buf); |
| 138 | int stream_duration_sec = (clock_->TimeInMilliseconds() - start_ms_) / 1000; |
| 139 | if (stats_.frame_counts.key_frames > 0 || |
| 140 | stats_.frame_counts.delta_frames > 0) { |
| 141 | RTC_HISTOGRAM_COUNTS_100000("WebRTC.Video.ReceiveStreamLifetimeInSeconds", |
| 142 | stream_duration_sec); |
| 143 | log_stream << "WebRTC.Video.ReceiveStreamLifetimeInSeconds " |
| 144 | << stream_duration_sec << '\n'; |
| 145 | } |
| 146 |
| 147 | log_stream << "Frames decoded " << stats_.frames_decoded << '\n'; |
| 148 |
| 149 | if (num_unique_frames_) { |
| 150 | int num_dropped_frames = *num_unique_frames_ - stats_.frames_decoded; |
| 151 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DroppedFrames.Receiver", |
| 152 | num_dropped_frames); |
| 153 | log_stream << "WebRTC.Video.DroppedFrames.Receiver " << num_dropped_frames |
| 154 | << '\n'; |
| 155 | } |
| 156 |
| 157 | if (fraction_lost && stream_duration_sec >= metrics::kMinRunTimeInSeconds) { |
| 158 | RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.ReceivedPacketsLostInPercent", |
| 159 | *fraction_lost); |
| 160 | log_stream << "WebRTC.Video.ReceivedPacketsLostInPercent " << *fraction_lost |
| 161 | << '\n'; |
| 162 | } |
| 163 |
| 164 | if (first_decoded_frame_time_ms_) { |
| 165 | const int64_t elapsed_ms = |
| 166 | (clock_->TimeInMilliseconds() - *first_decoded_frame_time_ms_); |
| 167 | if (elapsed_ms >= |
| 168 | metrics::kMinRunTimeInSeconds * rtc::kNumMillisecsPerSec) { |
| 169 | int decoded_fps = static_cast<int>( |
| 170 | (stats_.frames_decoded * 1000.0f / elapsed_ms) + 0.5f); |
| 171 | RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.DecodedFramesPerSecond", |
| 172 | decoded_fps); |
| 173 | log_stream << "WebRTC.Video.DecodedFramesPerSecond " << decoded_fps |
| 174 | << '\n'; |
| 175 |
| 176 | const uint32_t frames_rendered = stats_.frames_rendered; |
| 177 | if (frames_rendered > 0) { |
| 178 | RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.DelayedFramesToRenderer", |
| 179 | static_cast<int>(num_delayed_frames_rendered_ * |
| 180 | 100 / frames_rendered)); |
| 181 | if (num_delayed_frames_rendered_ > 0) { |
| 182 | RTC_HISTOGRAM_COUNTS_1000( |
| 183 | "WebRTC.Video.DelayedFramesToRenderer_AvgDelayInMs", |
| 184 | static_cast<int>(sum_missed_render_deadline_ms_ / |
| 185 | num_delayed_frames_rendered_)); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 |
| 191 | const int kMinRequiredSamples = 200; |
| 192 | int samples = static_cast<int>(render_fps_tracker_.TotalSampleCount()); |
| 193 | if (samples >= kMinRequiredSamples) { |
| 194 | int rendered_fps = round(render_fps_tracker_.ComputeTotalRate()); |
| 195 | RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.RenderFramesPerSecond", |
| 196 | rendered_fps); |
| 197 | log_stream << "WebRTC.Video.RenderFramesPerSecond " << rendered_fps << '\n'; |
| 198 | RTC_HISTOGRAM_COUNTS_100000( |
| 199 | "WebRTC.Video.RenderSqrtPixelsPerSecond", |
| 200 | round(render_pixel_tracker_.ComputeTotalRate())); |
| 201 | } |
| 202 |
| 203 | absl::optional<int> sync_offset_ms = |
| 204 | sync_offset_counter_.Avg(kMinRequiredSamples); |
| 205 | if (sync_offset_ms) { |
| 206 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.AVSyncOffsetInMs", |
| 207 | *sync_offset_ms); |
| 208 | log_stream << "WebRTC.Video.AVSyncOffsetInMs " << *sync_offset_ms << '\n'; |
| 209 | } |
| 210 | AggregatedStats freq_offset_stats = freq_offset_counter_.GetStats(); |
| 211 | if (freq_offset_stats.num_samples > 0) { |
| 212 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.RtpToNtpFreqOffsetInKhz", |
| 213 | freq_offset_stats.average); |
| 214 | log_stream << "WebRTC.Video.RtpToNtpFreqOffsetInKhz " |
| 215 | << freq_offset_stats.ToString() << '\n'; |
| 216 | } |
| 217 |
| 218 | int num_total_frames = |
| 219 | stats_.frame_counts.key_frames + stats_.frame_counts.delta_frames; |
| 220 | if (num_total_frames >= kMinRequiredSamples) { |
| 221 | int num_key_frames = stats_.frame_counts.key_frames; |
| 222 | int key_frames_permille = |
| 223 | (num_key_frames * 1000 + num_total_frames / 2) / num_total_frames; |
| 224 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesReceivedInPermille", |
| 225 | key_frames_permille); |
| 226 | log_stream << "WebRTC.Video.KeyFramesReceivedInPermille " |
| 227 | << key_frames_permille << '\n'; |
| 228 | } |
| 229 |
| 230 | absl::optional<int> qp = qp_counters_.vp8.Avg(kMinRequiredSamples); |
| 231 | if (qp) { |
| 232 | RTC_HISTOGRAM_COUNTS_200("WebRTC.Video.Decoded.Vp8.Qp", *qp); |
| 233 | log_stream << "WebRTC.Video.Decoded.Vp8.Qp " << *qp << '\n'; |
| 234 | } |
| 235 | absl::optional<int> decode_ms = decode_time_counter_.Avg(kMinRequiredSamples); |
| 236 | if (decode_ms) { |
| 237 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DecodeTimeInMs", *decode_ms); |
| 238 | log_stream << "WebRTC.Video.DecodeTimeInMs " << *decode_ms << '\n'; |
| 239 | } |
| 240 | absl::optional<int> jb_delay_ms = |
| 241 | jitter_buffer_delay_counter_.Avg(kMinRequiredSamples); |
| 242 | if (jb_delay_ms) { |
| 243 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.JitterBufferDelayInMs", |
| 244 | *jb_delay_ms); |
| 245 | log_stream << "WebRTC.Video.JitterBufferDelayInMs " << *jb_delay_ms << '\n'; |
| 246 | } |
| 247 |
| 248 | absl::optional<int> target_delay_ms = |
| 249 | target_delay_counter_.Avg(kMinRequiredSamples); |
| 250 | if (target_delay_ms) { |
| 251 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.TargetDelayInMs", |
| 252 | *target_delay_ms); |
| 253 | log_stream << "WebRTC.Video.TargetDelayInMs " << *target_delay_ms << '\n'; |
| 254 | } |
| 255 | absl::optional<int> current_delay_ms = |
| 256 | current_delay_counter_.Avg(kMinRequiredSamples); |
| 257 | if (current_delay_ms) { |
| 258 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.CurrentDelayInMs", |
| 259 | *current_delay_ms); |
| 260 | log_stream << "WebRTC.Video.CurrentDelayInMs " << *current_delay_ms << '\n'; |
| 261 | } |
| 262 | absl::optional<int> delay_ms = delay_counter_.Avg(kMinRequiredSamples); |
| 263 | if (delay_ms) |
| 264 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.OnewayDelayInMs", *delay_ms); |
| 265 |
| 266 | // Aggregate content_specific_stats_ by removing experiment or simulcast |
| 267 | // information; |
| 268 | std::map<VideoContentType, ContentSpecificStats> aggregated_stats; |
| 269 | for (const auto& it : content_specific_stats_) { |
| 270 | // Calculate simulcast specific metrics (".S0" ... ".S2" suffixes). |
| 271 | VideoContentType content_type = it.first; |
| 272 | if (videocontenttypehelpers::GetSimulcastId(content_type) > 0) { |
| 273 | // Aggregate on experiment id. |
| 274 | videocontenttypehelpers::SetExperimentId(&content_type, 0); |
| 275 | aggregated_stats[content_type].Add(it.second); |
| 276 | } |
| 277 | // Calculate experiment specific metrics (".ExperimentGroup[0-7]" suffixes). |
| 278 | content_type = it.first; |
| 279 | if (videocontenttypehelpers::GetExperimentId(content_type) > 0) { |
| 280 | // Aggregate on simulcast id. |
| 281 | videocontenttypehelpers::SetSimulcastId(&content_type, 0); |
| 282 | aggregated_stats[content_type].Add(it.second); |
| 283 | } |
| 284 | // Calculate aggregated metrics (no suffixes. Aggregated on everything). |
| 285 | content_type = it.first; |
| 286 | videocontenttypehelpers::SetSimulcastId(&content_type, 0); |
| 287 | videocontenttypehelpers::SetExperimentId(&content_type, 0); |
| 288 | aggregated_stats[content_type].Add(it.second); |
| 289 | } |
| 290 |
| 291 | for (const auto& it : aggregated_stats) { |
| 292 | // For the metric Foo we report the following slices: |
| 293 | // WebRTC.Video.Foo, |
| 294 | // WebRTC.Video.Screenshare.Foo, |
| 295 | // WebRTC.Video.Foo.S[0-3], |
| 296 | // WebRTC.Video.Foo.ExperimentGroup[0-7], |
| 297 | // WebRTC.Video.Screenshare.Foo.S[0-3], |
| 298 | // WebRTC.Video.Screenshare.Foo.ExperimentGroup[0-7]. |
| 299 | auto content_type = it.first; |
| 300 | auto stats = it.second; |
| 301 | std::string uma_prefix = UmaPrefixForContentType(content_type); |
| 302 | std::string uma_suffix = UmaSuffixForContentType(content_type); |
| 303 | // Metrics can be sliced on either simulcast id or experiment id but not |
| 304 | // both. |
| 305 | RTC_DCHECK(videocontenttypehelpers::GetExperimentId(content_type) == 0 || |
| 306 | videocontenttypehelpers::GetSimulcastId(content_type) == 0); |
| 307 |
| 308 | absl::optional<int> e2e_delay_ms = |
| 309 | stats.e2e_delay_counter.Avg(kMinRequiredSamples); |
| 310 | if (e2e_delay_ms) { |
| 311 | RTC_HISTOGRAM_COUNTS_SPARSE_10000( |
| 312 | uma_prefix + ".EndToEndDelayInMs" + uma_suffix, *e2e_delay_ms); |
| 313 | log_stream << uma_prefix << ".EndToEndDelayInMs" << uma_suffix << " " |
| 314 | << *e2e_delay_ms << '\n'; |
| 315 | } |
| 316 | absl::optional<int> e2e_delay_max_ms = stats.e2e_delay_counter.Max(); |
| 317 | if (e2e_delay_max_ms && e2e_delay_ms) { |
| 318 | RTC_HISTOGRAM_COUNTS_SPARSE_100000( |
| 319 | uma_prefix + ".EndToEndDelayMaxInMs" + uma_suffix, *e2e_delay_max_ms); |
| 320 | log_stream << uma_prefix << ".EndToEndDelayMaxInMs" << uma_suffix << " " |
| 321 | << *e2e_delay_max_ms << '\n'; |
| 322 | } |
| 323 | absl::optional<int> interframe_delay_ms = |
| 324 | stats.interframe_delay_counter.Avg(kMinRequiredSamples); |
| 325 | if (interframe_delay_ms) { |
| 326 | RTC_HISTOGRAM_COUNTS_SPARSE_10000( |
| 327 | uma_prefix + ".InterframeDelayInMs" + uma_suffix, |
| 328 | *interframe_delay_ms); |
| 329 | log_stream << uma_prefix << ".InterframeDelayInMs" << uma_suffix << " " |
| 330 | << *interframe_delay_ms << '\n'; |
| 331 | } |
| 332 | absl::optional<int> interframe_delay_max_ms = |
| 333 | stats.interframe_delay_counter.Max(); |
| 334 | if (interframe_delay_max_ms && interframe_delay_ms) { |
| 335 | RTC_HISTOGRAM_COUNTS_SPARSE_10000( |
| 336 | uma_prefix + ".InterframeDelayMaxInMs" + uma_suffix, |
| 337 | *interframe_delay_max_ms); |
| 338 | log_stream << uma_prefix << ".InterframeDelayMaxInMs" << uma_suffix << " " |
| 339 | << *interframe_delay_max_ms << '\n'; |
| 340 | } |
| 341 |
| 342 | absl::optional<uint32_t> interframe_delay_95p_ms = |
| 343 | stats.interframe_delay_percentiles.GetPercentile(0.95f); |
| 344 | if (interframe_delay_95p_ms && interframe_delay_ms != -1) { |
| 345 | RTC_HISTOGRAM_COUNTS_SPARSE_10000( |
| 346 | uma_prefix + ".InterframeDelay95PercentileInMs" + uma_suffix, |
| 347 | *interframe_delay_95p_ms); |
| 348 | log_stream << uma_prefix << ".InterframeDelay95PercentileInMs" |
| 349 | << uma_suffix << " " << *interframe_delay_95p_ms << '\n'; |
| 350 | } |
| 351 |
| 352 | absl::optional<int> width = stats.received_width.Avg(kMinRequiredSamples); |
| 353 | if (width) { |
| 354 | RTC_HISTOGRAM_COUNTS_SPARSE_10000( |
| 355 | uma_prefix + ".ReceivedWidthInPixels" + uma_suffix, *width); |
| 356 | log_stream << uma_prefix << ".ReceivedWidthInPixels" << uma_suffix << " " |
| 357 | << *width << '\n'; |
| 358 | } |
| 359 |
| 360 | absl::optional<int> height = stats.received_height.Avg(kMinRequiredSamples); |
| 361 | if (height) { |
| 362 | RTC_HISTOGRAM_COUNTS_SPARSE_10000( |
| 363 | uma_prefix + ".ReceivedHeightInPixels" + uma_suffix, *height); |
| 364 | log_stream << uma_prefix << ".ReceivedHeightInPixels" << uma_suffix << " " |
| 365 | << *height << '\n'; |
| 366 | } |
| 367 |
| 368 | if (content_type != VideoContentType::UNSPECIFIED) { |
| 369 | // Don't report these 3 metrics unsliced, as more precise variants |
| 370 | // are reported separately in this method. |
| 371 | float flow_duration_sec = stats.flow_duration_ms / 1000.0; |
| 372 | if (flow_duration_sec >= metrics::kMinRunTimeInSeconds) { |
| 373 | int media_bitrate_kbps = static_cast<int>(stats.total_media_bytes * 8 / |
| 374 | flow_duration_sec / 1000); |
| 375 | RTC_HISTOGRAM_COUNTS_SPARSE_10000( |
| 376 | uma_prefix + ".MediaBitrateReceivedInKbps" + uma_suffix, |
| 377 | media_bitrate_kbps); |
| 378 | log_stream << uma_prefix << ".MediaBitrateReceivedInKbps" << uma_suffix |
| 379 | << " " << media_bitrate_kbps << '\n'; |
| 380 | } |
| 381 |
| 382 | int num_total_frames2 = |
| 383 | stats.frame_counts.key_frames + stats.frame_counts.delta_frames; |
| 384 | if (num_total_frames2 >= kMinRequiredSamples) { |
| 385 | int num_key_frames = stats.frame_counts.key_frames; |
| 386 | int key_frames_permille = |
| 387 | (num_key_frames * 1000 + num_total_frames2 / 2) / num_total_frames2; |
| 388 | RTC_HISTOGRAM_COUNTS_SPARSE_1000( |
| 389 | uma_prefix + ".KeyFramesReceivedInPermille" + uma_suffix, |
| 390 | key_frames_permille); |
| 391 | log_stream << uma_prefix << ".KeyFramesReceivedInPermille" << uma_suffix |
| 392 | << " " << key_frames_permille << '\n'; |
| 393 | } |
| 394 |
| 395 | absl::optional<int> qp2 = stats.qp_counter.Avg(kMinRequiredSamples); |
| 396 | if (qp2) { |
| 397 | RTC_HISTOGRAM_COUNTS_SPARSE_200( |
| 398 | uma_prefix + ".Decoded.Vp8.Qp" + uma_suffix, *qp2); |
| 399 | log_stream << uma_prefix << ".Decoded.Vp8.Qp" << uma_suffix << " " |
| 400 | << *qp2 << '\n'; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 |
| 405 | StreamDataCounters rtp_rtx_stats = rtp_stats; |
| 406 | if (rtx_stats) |
| 407 | rtp_rtx_stats.Add(*rtx_stats); |
| 408 | int64_t elapsed_sec = |
| 409 | rtp_rtx_stats.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) / |
| 410 | 1000; |
| 411 | if (elapsed_sec >= metrics::kMinRunTimeInSeconds) { |
| 412 | RTC_HISTOGRAM_COUNTS_10000( |
| 413 | "WebRTC.Video.BitrateReceivedInKbps", |
| 414 | static_cast<int>(rtp_rtx_stats.transmitted.TotalBytes() * 8 / |
| 415 | elapsed_sec / 1000)); |
| 416 | int media_bitrate_kbs = static_cast<int>(rtp_stats.MediaPayloadBytes() * 8 / |
| 417 | elapsed_sec / 1000); |
| 418 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.MediaBitrateReceivedInKbps", |
| 419 | media_bitrate_kbs); |
| 420 | log_stream << "WebRTC.Video.MediaBitrateReceivedInKbps " |
| 421 | << media_bitrate_kbs << '\n'; |
| 422 | RTC_HISTOGRAM_COUNTS_10000( |
| 423 | "WebRTC.Video.PaddingBitrateReceivedInKbps", |
| 424 | static_cast<int>(rtp_rtx_stats.transmitted.padding_bytes * 8 / |
| 425 | elapsed_sec / 1000)); |
| 426 | RTC_HISTOGRAM_COUNTS_10000( |
| 427 | "WebRTC.Video.RetransmittedBitrateReceivedInKbps", |
| 428 | static_cast<int>(rtp_rtx_stats.retransmitted.TotalBytes() * 8 / |
| 429 | elapsed_sec / 1000)); |
| 430 | if (rtx_stats) { |
| 431 | RTC_HISTOGRAM_COUNTS_10000( |
| 432 | "WebRTC.Video.RtxBitrateReceivedInKbps", |
| 433 | static_cast<int>(rtx_stats->transmitted.TotalBytes() * 8 / |
| 434 | elapsed_sec / 1000)); |
| 435 | } |
| 436 | const RtcpPacketTypeCounter& counters = stats_.rtcp_packet_type_counts; |
| 437 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.NackPacketsSentPerMinute", |
| 438 | counters.nack_packets * 60 / elapsed_sec); |
| 439 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.FirPacketsSentPerMinute", |
| 440 | counters.fir_packets * 60 / elapsed_sec); |
| 441 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.PliPacketsSentPerMinute", |
| 442 | counters.pli_packets * 60 / elapsed_sec); |
| 443 | if (counters.nack_requests > 0) { |
| 444 | RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.UniqueNackRequestsSentInPercent", |
| 445 | counters.UniqueNackRequestsInPercent()); |
| 446 | } |
| 447 | } |
| 448 |
| 449 | if (num_certain_states_ >= kBadCallMinRequiredSamples) { |
| 450 | RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Any", |
| 451 | 100 * num_bad_states_ / num_certain_states_); |
| 452 | } |
| 453 | absl::optional<double> fps_fraction = |
| 454 | fps_threshold_.FractionHigh(kBadCallMinRequiredSamples); |
| 455 | if (fps_fraction) { |
| 456 | RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRate", |
| 457 | static_cast<int>(100 * (1 - *fps_fraction))); |
| 458 | } |
| 459 | absl::optional<double> variance_fraction = |
| 460 | variance_threshold_.FractionHigh(kBadCallMinRequiredSamples); |
| 461 | if (variance_fraction) { |
| 462 | RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.FrameRateVariance", |
| 463 | static_cast<int>(100 * *variance_fraction)); |
| 464 | } |
| 465 | absl::optional<double> qp_fraction = |
| 466 | qp_threshold_.FractionHigh(kBadCallMinRequiredSamples); |
| 467 | if (qp_fraction) { |
| 468 | RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.BadCall.Qp", |
| 469 | static_cast<int>(100 * *qp_fraction)); |
| 470 | } |
| 471 |
| 472 | RTC_LOG(LS_INFO) << log_stream.str(); |
| 473 | video_quality_observer_->UpdateHistograms(); |
| 474 | } |
| 475 |
| 476 | void ReceiveStatisticsProxy::QualitySample() { |
| 477 | int64_t now = clock_->TimeInMilliseconds(); |
| 478 | if (last_sample_time_ + kMinSampleLengthMs > now) |
| 479 | return; |
| 480 |
| 481 | double fps = |
| 482 | render_fps_tracker_.ComputeRateForInterval(now - last_sample_time_); |
| 483 | absl::optional<int> qp = qp_sample_.Avg(1); |
| 484 |
| 485 | bool prev_fps_bad = !fps_threshold_.IsHigh().value_or(true); |
| 486 | bool prev_qp_bad = qp_threshold_.IsHigh().value_or(false); |
| 487 | bool prev_variance_bad = variance_threshold_.IsHigh().value_or(false); |
| 488 | bool prev_any_bad = prev_fps_bad || prev_qp_bad || prev_variance_bad; |
| 489 |
| 490 | fps_threshold_.AddMeasurement(static_cast<int>(fps)); |
| 491 | if (qp) |
| 492 | qp_threshold_.AddMeasurement(*qp); |
| 493 | absl::optional<double> fps_variance_opt = fps_threshold_.CalculateVariance(); |
| 494 | double fps_variance = fps_variance_opt.value_or(0); |
| 495 | if (fps_variance_opt) { |
| 496 | variance_threshold_.AddMeasurement(static_cast<int>(fps_variance)); |
| 497 | } |
| 498 |
| 499 | bool fps_bad = !fps_threshold_.IsHigh().value_or(true); |
| 500 | bool qp_bad = qp_threshold_.IsHigh().value_or(false); |
| 501 | bool variance_bad = variance_threshold_.IsHigh().value_or(false); |
| 502 | bool any_bad = fps_bad || qp_bad || variance_bad; |
| 503 |
| 504 | if (!prev_any_bad && any_bad) { |
| 505 | RTC_LOG(LS_INFO) << "Bad call (any) start: " << now; |
| 506 | } else if (prev_any_bad && !any_bad) { |
| 507 | RTC_LOG(LS_INFO) << "Bad call (any) end: " << now; |
| 508 | } |
| 509 |
| 510 | if (!prev_fps_bad && fps_bad) { |
| 511 | RTC_LOG(LS_INFO) << "Bad call (fps) start: " << now; |
| 512 | } else if (prev_fps_bad && !fps_bad) { |
| 513 | RTC_LOG(LS_INFO) << "Bad call (fps) end: " << now; |
| 514 | } |
| 515 |
| 516 | if (!prev_qp_bad && qp_bad) { |
| 517 | RTC_LOG(LS_INFO) << "Bad call (qp) start: " << now; |
| 518 | } else if (prev_qp_bad && !qp_bad) { |
| 519 | RTC_LOG(LS_INFO) << "Bad call (qp) end: " << now; |
| 520 | } |
| 521 |
| 522 | if (!prev_variance_bad && variance_bad) { |
| 523 | RTC_LOG(LS_INFO) << "Bad call (variance) start: " << now; |
| 524 | } else if (prev_variance_bad && !variance_bad) { |
| 525 | RTC_LOG(LS_INFO) << "Bad call (variance) end: " << now; |
| 526 | } |
| 527 |
| 528 | RTC_LOG(LS_VERBOSE) << "SAMPLE: sample_length: " << (now - last_sample_time_) |
| 529 | << " fps: " << fps << " fps_bad: " << fps_bad |
| 530 | << " qp: " << qp.value_or(-1) << " qp_bad: " << qp_bad |
| 531 | << " variance_bad: " << variance_bad |
| 532 | << " fps_variance: " << fps_variance; |
| 533 |
| 534 | last_sample_time_ = now; |
| 535 | qp_sample_.Reset(); |
| 536 |
| 537 | if (fps_threshold_.IsHigh() || variance_threshold_.IsHigh() || |
| 538 | qp_threshold_.IsHigh()) { |
| 539 | if (any_bad) |
| 540 | ++num_bad_states_; |
| 541 | ++num_certain_states_; |
| 542 | } |
| 543 | } |
| 544 |
| 545 | void ReceiveStatisticsProxy::UpdateFramerate(int64_t now_ms) const { |
| 546 | int64_t old_frames_ms = now_ms - kRateStatisticsWindowSizeMs; |
| 547 | while (!frame_window_.empty() && |
| 548 | frame_window_.begin()->first < old_frames_ms) { |
| 549 | frame_window_.erase(frame_window_.begin()); |
| 550 | } |
| 551 |
| 552 | size_t framerate = |
| 553 | (frame_window_.size() * 1000 + 500) / kRateStatisticsWindowSizeMs; |
| 554 | stats_.network_frame_rate = static_cast<int>(framerate); |
| 555 | } |
| 556 |
| 557 | void ReceiveStatisticsProxy::UpdateDecodeTimeHistograms( |
| 558 | int width, |
| 559 | int height, |
| 560 | int decode_time_ms) const { |
| 561 | bool is_4k = (width == 3840 || width == 4096) && height == 2160; |
| 562 | bool is_hd = width == 1920 && height == 1080; |
| 563 | // Only update histograms for 4k/HD and VP9/H264. |
| 564 | if ((is_4k || is_hd) && (last_codec_type_ == kVideoCodecVP9 || |
| 565 | last_codec_type_ == kVideoCodecH264)) { |
| 566 | const std::string kDecodeTimeUmaPrefix = |
| 567 | "WebRTC.Video.DecodeTimePerFrameInMs."; |
| 568 |
| 569 | // Each histogram needs its own line for it to not be reused in the wrong |
| 570 | // way when the format changes. |
| 571 | if (last_codec_type_ == kVideoCodecVP9) { |
| 572 | bool is_sw_decoder = |
| 573 | stats_.decoder_implementation_name.compare(0, 6, "libvpx") == 0; |
| 574 | if (is_4k) { |
| 575 | if (is_sw_decoder) |
| 576 | RTC_HISTOGRAM_COUNTS_1000(kDecodeTimeUmaPrefix + "Vp9.4k.Sw", |
| 577 | decode_time_ms); |
| 578 | else |
| 579 | RTC_HISTOGRAM_COUNTS_1000(kDecodeTimeUmaPrefix + "Vp9.4k.Hw", |
| 580 | decode_time_ms); |
| 581 | } else { |
| 582 | if (is_sw_decoder) |
| 583 | RTC_HISTOGRAM_COUNTS_1000(kDecodeTimeUmaPrefix + "Vp9.Hd.Sw", |
| 584 | decode_time_ms); |
| 585 | else |
| 586 | RTC_HISTOGRAM_COUNTS_1000(kDecodeTimeUmaPrefix + "Vp9.Hd.Hw", |
| 587 | decode_time_ms); |
| 588 | } |
| 589 | } else { |
| 590 | bool is_sw_decoder = |
| 591 | stats_.decoder_implementation_name.compare(0, 6, "FFmpeg") == 0; |
| 592 | if (is_4k) { |
| 593 | if (is_sw_decoder) |
| 594 | RTC_HISTOGRAM_COUNTS_1000(kDecodeTimeUmaPrefix + "H264.4k.Sw", |
| 595 | decode_time_ms); |
| 596 | else |
| 597 | RTC_HISTOGRAM_COUNTS_1000(kDecodeTimeUmaPrefix + "H264.4k.Hw", |
| 598 | decode_time_ms); |
| 599 |
| 600 | } else { |
| 601 | if (is_sw_decoder) |
| 602 | RTC_HISTOGRAM_COUNTS_1000(kDecodeTimeUmaPrefix + "H264.Hd.Sw", |
| 603 | decode_time_ms); |
| 604 | else |
| 605 | RTC_HISTOGRAM_COUNTS_1000(kDecodeTimeUmaPrefix + "H264.Hd.Hw", |
| 606 | decode_time_ms); |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 |
| 612 | absl::optional<int64_t> |
| 613 | ReceiveStatisticsProxy::GetCurrentEstimatedPlayoutNtpTimestampMs( |
| 614 | int64_t now_ms) const { |
| 615 | if (!last_estimated_playout_ntp_timestamp_ms_ || |
| 616 | !last_estimated_playout_time_ms_) { |
| 617 | return absl::nullopt; |
| 618 | } |
| 619 | int64_t elapsed_ms = now_ms - *last_estimated_playout_time_ms_; |
| 620 | return *last_estimated_playout_ntp_timestamp_ms_ + elapsed_ms; |
| 621 | } |
| 622 |
| 623 | VideoReceiveStream::Stats ReceiveStatisticsProxy::GetStats() const { |
| 624 | MutexLock lock(&mutex_); |
| 625 | // Get current frame rates here, as only updating them on new frames prevents |
| 626 | // us from ever correctly displaying frame rate of 0. |
| 627 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 628 | UpdateFramerate(now_ms); |
| 629 | stats_.render_frame_rate = renders_fps_estimator_.Rate(now_ms).value_or(0); |
| 630 | stats_.decode_frame_rate = decode_fps_estimator_.Rate(now_ms).value_or(0); |
| 631 | stats_.interframe_delay_max_ms = |
| 632 | interframe_delay_max_moving_.Max(now_ms).value_or(-1); |
| 633 | stats_.freeze_count = video_quality_observer_->NumFreezes(); |
| 634 | stats_.pause_count = video_quality_observer_->NumPauses(); |
| 635 | stats_.total_freezes_duration_ms = |
| 636 | video_quality_observer_->TotalFreezesDurationMs(); |
| 637 | stats_.total_pauses_duration_ms = |
| 638 | video_quality_observer_->TotalPausesDurationMs(); |
| 639 | stats_.total_frames_duration_ms = |
| 640 | video_quality_observer_->TotalFramesDurationMs(); |
| 641 | stats_.sum_squared_frame_durations = |
| 642 | video_quality_observer_->SumSquaredFrameDurationsSec(); |
| 643 | stats_.content_type = last_content_type_; |
| 644 | stats_.timing_frame_info = timing_frame_info_counter_.Max(now_ms); |
| 645 | stats_.jitter_buffer_delay_seconds = |
| 646 | static_cast<double>(current_delay_counter_.Sum(1).value_or(0)) / |
| 647 | rtc::kNumMillisecsPerSec; |
| 648 | stats_.jitter_buffer_emitted_count = current_delay_counter_.NumSamples(); |
| 649 | stats_.estimated_playout_ntp_timestamp_ms = |
| 650 | GetCurrentEstimatedPlayoutNtpTimestampMs(now_ms); |
| 651 | return stats_; |
| 652 | } |
| 653 |
| 654 | void ReceiveStatisticsProxy::OnIncomingPayloadType(int payload_type) { |
| 655 | MutexLock lock(&mutex_); |
| 656 | stats_.current_payload_type = payload_type; |
| 657 | } |
| 658 |
| 659 | void ReceiveStatisticsProxy::OnDecoderImplementationName( |
| 660 | const char* implementation_name) { |
| 661 | MutexLock lock(&mutex_); |
| 662 | stats_.decoder_implementation_name = implementation_name; |
| 663 | } |
| 664 |
| 665 | void ReceiveStatisticsProxy::OnFrameBufferTimingsUpdated( |
| 666 | int max_decode_ms, |
| 667 | int current_delay_ms, |
| 668 | int target_delay_ms, |
| 669 | int jitter_buffer_ms, |
| 670 | int min_playout_delay_ms, |
| 671 | int render_delay_ms) { |
| 672 | MutexLock lock(&mutex_); |
| 673 | stats_.max_decode_ms = max_decode_ms; |
| 674 | stats_.current_delay_ms = current_delay_ms; |
| 675 | stats_.target_delay_ms = target_delay_ms; |
| 676 | stats_.jitter_buffer_ms = jitter_buffer_ms; |
| 677 | stats_.min_playout_delay_ms = min_playout_delay_ms; |
| 678 | stats_.render_delay_ms = render_delay_ms; |
| 679 | jitter_buffer_delay_counter_.Add(jitter_buffer_ms); |
| 680 | target_delay_counter_.Add(target_delay_ms); |
| 681 | current_delay_counter_.Add(current_delay_ms); |
| 682 | // Network delay (rtt/2) + target_delay_ms (jitter delay + decode time + |
| 683 | // render delay). |
| 684 | delay_counter_.Add(target_delay_ms + avg_rtt_ms_ / 2); |
| 685 | } |
| 686 |
| 687 | void ReceiveStatisticsProxy::OnUniqueFramesCounted(int num_unique_frames) { |
| 688 | MutexLock lock(&mutex_); |
| 689 | num_unique_frames_.emplace(num_unique_frames); |
| 690 | } |
| 691 |
| 692 | void ReceiveStatisticsProxy::OnTimingFrameInfoUpdated( |
| 693 | const TimingFrameInfo& info) { |
| 694 | MutexLock lock(&mutex_); |
| 695 | if (info.flags != VideoSendTiming::kInvalid) { |
| 696 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 697 | timing_frame_info_counter_.Add(info, now_ms); |
| 698 | } |
| 699 |
| 700 | // Measure initial decoding latency between the first frame arriving and the |
| 701 | // first frame being decoded. |
| 702 | if (!first_frame_received_time_ms_.has_value()) { |
| 703 | first_frame_received_time_ms_ = info.receive_finish_ms; |
| 704 | } |
| 705 | if (stats_.first_frame_received_to_decoded_ms == -1 && |
| 706 | first_decoded_frame_time_ms_) { |
| 707 | stats_.first_frame_received_to_decoded_ms = |
| 708 | *first_decoded_frame_time_ms_ - *first_frame_received_time_ms_; |
| 709 | } |
| 710 | } |
| 711 |
| 712 | void ReceiveStatisticsProxy::RtcpPacketTypesCounterUpdated( |
| 713 | uint32_t ssrc, |
| 714 | const RtcpPacketTypeCounter& packet_counter) { |
| 715 | MutexLock lock(&mutex_); |
| 716 | if (stats_.ssrc != ssrc) |
| 717 | return; |
| 718 | stats_.rtcp_packet_type_counts = packet_counter; |
| 719 | } |
| 720 |
| 721 | void ReceiveStatisticsProxy::OnCname(uint32_t ssrc, absl::string_view cname) { |
| 722 | MutexLock lock(&mutex_); |
| 723 | // TODO(pbos): Handle both local and remote ssrcs here and RTC_DCHECK that we |
| 724 | // receive stats from one of them. |
| 725 | if (stats_.ssrc != ssrc) |
| 726 | return; |
| 727 | stats_.c_name = std::string(cname); |
| 728 | } |
| 729 |
| 730 | void ReceiveStatisticsProxy::OnDecodedFrame(const VideoFrame& frame, |
| 731 | absl::optional<uint8_t> qp, |
| 732 | int32_t decode_time_ms, |
| 733 | VideoContentType content_type) { |
| 734 | MutexLock lock(&mutex_); |
| 735 |
| 736 | uint64_t now_ms = clock_->TimeInMilliseconds(); |
| 737 |
| 738 | if (videocontenttypehelpers::IsScreenshare(content_type) != |
| 739 | videocontenttypehelpers::IsScreenshare(last_content_type_)) { |
| 740 | // Reset the quality observer if content type is switched. But first report |
| 741 | // stats for the previous part of the call. |
| 742 | video_quality_observer_->UpdateHistograms(); |
| 743 | video_quality_observer_.reset(new VideoQualityObserver(content_type)); |
| 744 | } |
| 745 |
| 746 | video_quality_observer_->OnDecodedFrame(frame, qp, last_codec_type_); |
| 747 |
| 748 | ContentSpecificStats* content_specific_stats = |
| 749 | &content_specific_stats_[content_type]; |
| 750 | ++stats_.frames_decoded; |
| 751 | if (qp) { |
| 752 | if (!stats_.qp_sum) { |
| 753 | if (stats_.frames_decoded != 1) { |
| 754 | RTC_LOG(LS_WARNING) |
| 755 | << "Frames decoded was not 1 when first qp value was received."; |
| 756 | } |
| 757 | stats_.qp_sum = 0; |
| 758 | } |
| 759 | *stats_.qp_sum += *qp; |
| 760 | content_specific_stats->qp_counter.Add(*qp); |
| 761 | } else if (stats_.qp_sum) { |
| 762 | RTC_LOG(LS_WARNING) |
| 763 | << "QP sum was already set and no QP was given for a frame."; |
| 764 | stats_.qp_sum.reset(); |
| 765 | } |
| 766 | decode_time_counter_.Add(decode_time_ms); |
| 767 | stats_.decode_ms = decode_time_ms; |
| 768 | stats_.total_decode_time_ms += decode_time_ms; |
| 769 | if (enable_decode_time_histograms_) { |
| 770 | UpdateDecodeTimeHistograms(frame.width(), frame.height(), decode_time_ms); |
| 771 | } |
| 772 |
| 773 | last_content_type_ = content_type; |
| 774 | decode_fps_estimator_.Update(1, now_ms); |
| 775 | if (last_decoded_frame_time_ms_) { |
| 776 | int64_t interframe_delay_ms = now_ms - *last_decoded_frame_time_ms_; |
| 777 | RTC_DCHECK_GE(interframe_delay_ms, 0); |
| 778 | double interframe_delay = interframe_delay_ms / 1000.0; |
| 779 | stats_.total_inter_frame_delay += interframe_delay; |
| 780 | stats_.total_squared_inter_frame_delay += |
| 781 | interframe_delay * interframe_delay; |
| 782 | interframe_delay_max_moving_.Add(interframe_delay_ms, now_ms); |
| 783 | content_specific_stats->interframe_delay_counter.Add(interframe_delay_ms); |
| 784 | content_specific_stats->interframe_delay_percentiles.Add( |
| 785 | interframe_delay_ms); |
| 786 | content_specific_stats->flow_duration_ms += interframe_delay_ms; |
| 787 | } |
| 788 | if (stats_.frames_decoded == 1) { |
| 789 | first_decoded_frame_time_ms_.emplace(now_ms); |
| 790 | } |
| 791 | last_decoded_frame_time_ms_.emplace(now_ms); |
| 792 | } |
| 793 |
| 794 | void ReceiveStatisticsProxy::OnRenderedFrame(const VideoFrame& frame) { |
| 795 | int width = frame.width(); |
| 796 | int height = frame.height(); |
| 797 | RTC_DCHECK_GT(width, 0); |
| 798 | RTC_DCHECK_GT(height, 0); |
| 799 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 800 | MutexLock lock(&mutex_); |
| 801 |
| 802 | video_quality_observer_->OnRenderedFrame(frame, now_ms); |
| 803 |
| 804 | ContentSpecificStats* content_specific_stats = |
| 805 | &content_specific_stats_[last_content_type_]; |
| 806 | renders_fps_estimator_.Update(1, now_ms); |
| 807 | ++stats_.frames_rendered; |
| 808 | stats_.width = width; |
| 809 | stats_.height = height; |
| 810 | render_fps_tracker_.AddSamples(1); |
| 811 | render_pixel_tracker_.AddSamples(sqrt(width * height)); |
| 812 | content_specific_stats->received_width.Add(width); |
| 813 | content_specific_stats->received_height.Add(height); |
| 814 |
| 815 | // Consider taking stats_.render_delay_ms into account. |
| 816 | const int64_t time_until_rendering_ms = frame.render_time_ms() - now_ms; |
| 817 | if (time_until_rendering_ms < 0) { |
| 818 | sum_missed_render_deadline_ms_ += -time_until_rendering_ms; |
| 819 | ++num_delayed_frames_rendered_; |
| 820 | } |
| 821 |
| 822 | if (frame.ntp_time_ms() > 0) { |
| 823 | int64_t delay_ms = clock_->CurrentNtpInMilliseconds() - frame.ntp_time_ms(); |
| 824 | if (delay_ms >= 0) { |
| 825 | content_specific_stats->e2e_delay_counter.Add(delay_ms); |
| 826 | } |
| 827 | } |
| 828 | QualitySample(); |
| 829 | } |
| 830 |
| 831 | void ReceiveStatisticsProxy::OnSyncOffsetUpdated(int64_t video_playout_ntp_ms, |
| 832 | int64_t sync_offset_ms, |
| 833 | double estimated_freq_khz) { |
| 834 | MutexLock lock(&mutex_); |
| 835 | sync_offset_counter_.Add(std::abs(sync_offset_ms)); |
| 836 | stats_.sync_offset_ms = sync_offset_ms; |
| 837 | last_estimated_playout_ntp_timestamp_ms_ = video_playout_ntp_ms; |
| 838 | last_estimated_playout_time_ms_ = clock_->TimeInMilliseconds(); |
| 839 |
| 840 | const double kMaxFreqKhz = 10000.0; |
| 841 | int offset_khz = kMaxFreqKhz; |
| 842 | // Should not be zero or negative. If so, report max. |
| 843 | if (estimated_freq_khz < kMaxFreqKhz && estimated_freq_khz > 0.0) |
| 844 | offset_khz = static_cast<int>(std::fabs(estimated_freq_khz - 90.0) + 0.5); |
| 845 |
| 846 | freq_offset_counter_.Add(offset_khz); |
| 847 | } |
| 848 |
| 849 | void ReceiveStatisticsProxy::OnCompleteFrame(bool is_keyframe, |
| 850 | size_t size_bytes, |
| 851 | VideoContentType content_type) { |
| 852 | MutexLock lock(&mutex_); |
| 853 | if (is_keyframe) { |
| 854 | ++stats_.frame_counts.key_frames; |
| 855 | } else { |
| 856 | ++stats_.frame_counts.delta_frames; |
| 857 | } |
| 858 |
| 859 | // Content type extension is set only for keyframes and should be propagated |
| 860 | // for all the following delta frames. Here we may receive frames out of order |
| 861 | // and miscategorise some delta frames near the layer switch. |
| 862 | // This may slightly offset calculated bitrate and keyframes permille metrics. |
| 863 | VideoContentType propagated_content_type = |
| 864 | is_keyframe ? content_type : last_content_type_; |
| 865 |
| 866 | ContentSpecificStats* content_specific_stats = |
| 867 | &content_specific_stats_[propagated_content_type]; |
| 868 |
| 869 | content_specific_stats->total_media_bytes += size_bytes; |
| 870 | if (is_keyframe) { |
| 871 | ++content_specific_stats->frame_counts.key_frames; |
| 872 | } else { |
| 873 | ++content_specific_stats->frame_counts.delta_frames; |
| 874 | } |
| 875 |
| 876 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 877 | frame_window_.insert(std::make_pair(now_ms, size_bytes)); |
| 878 | UpdateFramerate(now_ms); |
| 879 | } |
| 880 |
| 881 | void ReceiveStatisticsProxy::OnDroppedFrames(uint32_t frames_dropped) { |
| 882 | MutexLock lock(&mutex_); |
| 883 | stats_.frames_dropped += frames_dropped; |
| 884 | } |
| 885 |
| 886 | void ReceiveStatisticsProxy::OnPreDecode(VideoCodecType codec_type, int qp) { |
| 887 | RTC_DCHECK_RUN_ON(&decode_thread_); |
| 888 | MutexLock lock(&mutex_); |
| 889 | last_codec_type_ = codec_type; |
| 890 | if (last_codec_type_ == kVideoCodecVP8 && qp != -1) { |
| 891 | qp_counters_.vp8.Add(qp); |
| 892 | qp_sample_.Add(qp); |
| 893 | } |
| 894 | } |
| 895 |
| 896 | void ReceiveStatisticsProxy::OnStreamInactive() { |
| 897 | // TODO(sprang): Figure out any other state that should be reset. |
| 898 |
| 899 | MutexLock lock(&mutex_); |
| 900 | // Don't report inter-frame delay if stream was paused. |
| 901 | last_decoded_frame_time_ms_.reset(); |
| 902 | video_quality_observer_->OnStreamInactive(); |
| 903 | } |
| 904 |
| 905 | void ReceiveStatisticsProxy::OnRttUpdate(int64_t avg_rtt_ms, |
| 906 | int64_t max_rtt_ms) { |
| 907 | MutexLock lock(&mutex_); |
| 908 | avg_rtt_ms_ = avg_rtt_ms; |
| 909 | } |
| 910 |
| 911 | void ReceiveStatisticsProxy::DecoderThreadStarting() { |
| 912 | RTC_DCHECK_RUN_ON(&main_thread_); |
| 913 | } |
| 914 |
| 915 | void ReceiveStatisticsProxy::DecoderThreadStopped() { |
| 916 | RTC_DCHECK_RUN_ON(&main_thread_); |
| 917 | decode_thread_.Detach(); |
| 918 | } |
| 919 |
| 920 | ReceiveStatisticsProxy::ContentSpecificStats::ContentSpecificStats() |
| 921 | : interframe_delay_percentiles(kMaxCommonInterframeDelayMs) {} |
| 922 |
| 923 | ReceiveStatisticsProxy::ContentSpecificStats::~ContentSpecificStats() = default; |
| 924 |
| 925 | void ReceiveStatisticsProxy::ContentSpecificStats::Add( |
| 926 | const ContentSpecificStats& other) { |
| 927 | e2e_delay_counter.Add(other.e2e_delay_counter); |
| 928 | interframe_delay_counter.Add(other.interframe_delay_counter); |
| 929 | flow_duration_ms += other.flow_duration_ms; |
| 930 | total_media_bytes += other.total_media_bytes; |
| 931 | received_height.Add(other.received_height); |
| 932 | received_width.Add(other.received_width); |
| 933 | qp_counter.Add(other.qp_counter); |
| 934 | frame_counts.key_frames += other.frame_counts.key_frames; |
| 935 | frame_counts.delta_frames += other.frame_counts.delta_frames; |
| 936 | interframe_delay_percentiles.Add(other.interframe_delay_percentiles); |
| 937 | } |
| 938 | } // namespace webrtc |
| 939 |