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