| 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 | #ifndef VIDEO_SEND_STATISTICS_PROXY_H_ |
| 12 | #define VIDEO_SEND_STATISTICS_PROXY_H_ |
| 13 |
| 14 | #include <array> |
| 15 | #include <deque> |
| 16 | #include <map> |
| 17 | #include <memory> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 |
| 21 | #include "api/field_trials_view.h" |
| 22 | #include "api/video/video_codec_constants.h" |
| 23 | #include "call/video_send_stream.h" |
| 24 | #include "modules/include/module_common_types_public.h" |
| 25 | #include "modules/rtp_rtcp/include/report_block_data.h" |
| 26 | #include "modules/video_coding/include/video_codec_interface.h" |
| 27 | #include "modules/video_coding/include/video_coding_defines.h" |
| 28 | #include "rtc_base/numerics/exp_filter.h" |
| 29 | #include "rtc_base/rate_tracker.h" |
| 30 | #include "rtc_base/synchronization/mutex.h" |
| 31 | #include "rtc_base/thread_annotations.h" |
| 32 | #include "system_wrappers/include/clock.h" |
| 33 | #include "video/config/video_encoder_config.h" |
| 34 | #include "video/quality_limitation_reason_tracker.h" |
| 35 | #include "video/report_block_stats.h" |
| 36 | #include "video/stats_counter.h" |
| 37 | #include "video/video_stream_encoder_observer.h" |
| 38 |
| 39 | namespace webrtc { |
| 40 |
| 41 | class SendStatisticsProxy : public VideoStreamEncoderObserver, |
| 42 | public ReportBlockDataObserver, |
| 43 | public RtcpPacketTypeCounterObserver, |
| 44 | public StreamDataCountersCallback, |
| 45 | public BitrateStatisticsObserver, |
| 46 | public FrameCountObserver { |
| 47 | public: |
| 48 | static constexpr TimeDelta kStatsTimeout = TimeDelta::Seconds(5); |
| 49 | // Number of required samples to be collected before a metric is added |
| 50 | // to a rtc histogram. |
| 51 | static const int kMinRequiredMetricsSamples = 200; |
| 52 |
| 53 | SendStatisticsProxy(Clock* clock, |
| 54 | const VideoSendStream::Config& config, |
| 55 | VideoEncoderConfig::ContentType content_type, |
| 56 | const FieldTrialsView& field_trials); |
| 57 | ~SendStatisticsProxy() override; |
| 58 |
| 59 | virtual VideoSendStream::Stats GetStats(); |
| 60 |
| 61 | void OnSendEncodedImage(const EncodedImage& encoded_image, |
| 62 | const CodecSpecificInfo* codec_info) override; |
| 63 |
| 64 | void OnEncoderImplementationChanged( |
| 65 | EncoderImplementation implementation) override; |
| 66 |
| 67 | // Used to update incoming frame rate. |
| 68 | void OnIncomingFrame(int width, int height) override; |
| 69 |
| 70 | // Dropped frame stats. |
| 71 | void OnFrameDropped(DropReason) override; |
| 72 |
| 73 | // Adaptation stats. |
| 74 | void OnAdaptationChanged( |
| 75 | VideoAdaptationReason reason, |
| 76 | const VideoAdaptationCounters& cpu_counters, |
| 77 | const VideoAdaptationCounters& quality_counters) override; |
| 78 | void ClearAdaptationStats() override; |
| 79 | void UpdateAdaptationSettings(AdaptationSettings cpu_settings, |
| 80 | AdaptationSettings quality_settings) override; |
| 81 |
| 82 | void OnBitrateAllocationUpdated( |
| 83 | const VideoCodec& codec, |
| 84 | const VideoBitrateAllocation& allocation) override; |
| 85 |
| 86 | void OnEncoderInternalScalerUpdate(bool is_scaled) override; |
| 87 |
| 88 | void OnMinPixelLimitReached() override; |
| 89 | void OnInitialQualityResolutionAdaptDown() override; |
| 90 |
| 91 | void OnSuspendChange(bool is_suspended) override; |
| 92 | void OnInactiveSsrc(uint32_t ssrc); |
| 93 |
| 94 | // Used to indicate change in content type, which may require a change in |
| 95 | // how stats are collected. |
| 96 | void OnEncoderReconfigured(const VideoEncoderConfig& encoder_config, |
| 97 | const std::vector<VideoStream>& streams) override; |
| 98 |
| 99 | // Used to update the encoder target rate. |
| 100 | void OnSetEncoderTargetRate(uint32_t bitrate_bps); |
| 101 |
| 102 | // Implements CpuOveruseMetricsObserver. |
| 103 | void OnEncodedFrameTimeMeasured(int encode_time_ms, |
| 104 | int encode_usage_percent) override; |
| 105 |
| 106 | void OnSendPacket(uint32_t ssrc, Timestamp capture_time); |
| 107 |
| 108 | int GetInputFrameRate() const override; |
| 109 | int GetSendFrameRate() const; |
| 110 |
| 111 | protected: |
| 112 | // From ReportBlockDataObserver. |
| 113 | void OnReportBlockDataUpdated(ReportBlockData report_block_data) override; |
| 114 | // From RtcpPacketTypeCounterObserver. |
| 115 | void RtcpPacketTypesCounterUpdated( |
| 116 | uint32_t ssrc, |
| 117 | const RtcpPacketTypeCounter& packet_counter) override; |
| 118 | // From StreamDataCountersCallback. |
| 119 | void DataCountersUpdated(const StreamDataCounters& counters, |
| 120 | uint32_t ssrc) override; |
| 121 |
| 122 | // From BitrateStatisticsObserver. |
| 123 | void Notify(uint32_t total_bitrate_bps, |
| 124 | uint32_t retransmit_bitrate_bps, |
| 125 | uint32_t ssrc) override; |
| 126 |
| 127 | // From FrameCountObserver. |
| 128 | void FrameCountUpdated(const FrameCounts& frame_counts, |
| 129 | uint32_t ssrc) override; |
| 130 |
| 131 | private: |
| 132 | class SampleCounter { |
| 133 | public: |
| 134 | SampleCounter() : sum(0), num_samples(0) {} |
| 135 | ~SampleCounter() {} |
| 136 | void Add(int sample); |
| 137 | int Avg(int64_t min_required_samples) const; |
| 138 |
| 139 | private: |
| 140 | int64_t sum; |
| 141 | int64_t num_samples; |
| 142 | }; |
| 143 | class BoolSampleCounter { |
| 144 | public: |
| 145 | BoolSampleCounter() : sum(0), num_samples(0) {} |
| 146 | ~BoolSampleCounter() {} |
| 147 | void Add(bool sample); |
| 148 | void Add(bool sample, int64_t count); |
| 149 | int Percent(int64_t min_required_samples) const; |
| 150 | int Permille(int64_t min_required_samples) const; |
| 151 |
| 152 | private: |
| 153 | int Fraction(int64_t min_required_samples, float multiplier) const; |
| 154 | int64_t sum; |
| 155 | int64_t num_samples; |
| 156 | }; |
| 157 | struct TargetRateUpdates { |
| 158 | TargetRateUpdates() |
| 159 | : pause_resume_events(0), last_paused_or_resumed(false), last_ms(-1) {} |
| 160 | int pause_resume_events; |
| 161 | bool last_paused_or_resumed; |
| 162 | int64_t last_ms; |
| 163 | }; |
| 164 | struct FallbackEncoderInfo { |
| 165 | FallbackEncoderInfo(); |
| 166 | bool is_possible = true; |
| 167 | bool is_active = false; |
| 168 | int on_off_events = 0; |
| 169 | int64_t elapsed_ms = 0; |
| 170 | absl::optional<int64_t> last_update_ms; |
| 171 | const int max_frame_diff_ms = 2000; |
| 172 | }; |
| 173 | struct FallbackEncoderInfoDisabled { |
| 174 | bool is_possible = true; |
| 175 | bool min_pixel_limit_reached = false; |
| 176 | }; |
| 177 | struct StatsTimer { |
| 178 | void Start(int64_t now_ms); |
| 179 | void Stop(int64_t now_ms); |
| 180 | void Restart(int64_t now_ms); |
| 181 | int64_t start_ms = -1; |
| 182 | int64_t total_ms = 0; |
| 183 | }; |
| 184 | struct QpCounters { |
| 185 | SampleCounter vp8; // QP range: 0-127. |
| 186 | SampleCounter vp9; // QP range: 0-255. |
| 187 | SampleCounter h264; // QP range: 0-51. |
| 188 | SampleCounter h265; // QP range: 0-51. |
| 189 | }; |
| 190 | struct AdaptChanges { |
| 191 | int down = 0; |
| 192 | int up = 0; |
| 193 | }; |
| 194 |
| 195 | // Map holding encoded frames (mapped by timestamp). |
| 196 | // If simulcast layers are encoded on different threads, there is no guarantee |
| 197 | // that one frame of all layers are encoded before the next start. |
| 198 | struct TimestampOlderThan { |
| 199 | bool operator()(uint32_t ts1, uint32_t ts2) const { |
| 200 | return IsNewerTimestamp(ts2, ts1); |
| 201 | } |
| 202 | }; |
| 203 | struct Frame { |
| 204 | Frame(int64_t send_ms, uint32_t width, uint32_t height, int simulcast_idx) |
| 205 | : send_ms(send_ms), |
| 206 | max_width(width), |
| 207 | max_height(height), |
| 208 | max_simulcast_idx(simulcast_idx) {} |
| 209 | const int64_t |
| 210 | send_ms; // Time when first frame with this timestamp is sent. |
| 211 | uint32_t max_width; // Max width with this timestamp. |
| 212 | uint32_t max_height; // Max height with this timestamp. |
| 213 | int max_simulcast_idx; // Max simulcast index with this timestamp. |
| 214 | }; |
| 215 | typedef std::map<uint32_t, Frame, TimestampOlderThan> EncodedFrameMap; |
| 216 |
| 217 | void PurgeOldStats() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 218 | VideoSendStream::StreamStats* GetStatsEntry(uint32_t ssrc) |
| 219 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 220 |
| 221 | struct MaskedAdaptationCounts { |
| 222 | absl::optional<int> resolution_adaptations = absl::nullopt; |
| 223 | absl::optional<int> num_framerate_reductions = absl::nullopt; |
| 224 | }; |
| 225 |
| 226 | struct Adaptations { |
| 227 | public: |
| 228 | MaskedAdaptationCounts MaskedCpuCounts() const; |
| 229 | MaskedAdaptationCounts MaskedQualityCounts() const; |
| 230 |
| 231 | void set_cpu_counts(const VideoAdaptationCounters& cpu_counts); |
| 232 | void set_quality_counts(const VideoAdaptationCounters& quality_counts); |
| 233 |
| 234 | VideoAdaptationCounters cpu_counts() const; |
| 235 | VideoAdaptationCounters quality_counts() const; |
| 236 |
| 237 | void UpdateMaskingSettings(AdaptationSettings cpu_settings, |
| 238 | AdaptationSettings quality_settings); |
| 239 |
| 240 | private: |
| 241 | VideoAdaptationCounters cpu_counts_; |
| 242 | AdaptationSettings cpu_settings_; |
| 243 | VideoAdaptationCounters quality_counts_; |
| 244 | AdaptationSettings quality_settings_; |
| 245 |
| 246 | MaskedAdaptationCounts Mask(const VideoAdaptationCounters& counters, |
| 247 | const AdaptationSettings& settings) const; |
| 248 | }; |
| 249 | // Collection of various stats that are tracked per ssrc. |
| 250 | struct Trackers { |
| 251 | struct SendDelayEntry { |
| 252 | Timestamp when; |
| 253 | TimeDelta send_delay; |
| 254 | }; |
| 255 |
| 256 | Trackers(); |
| 257 | Trackers(const Trackers&) = delete; |
| 258 | Trackers& operator=(const Trackers&) = delete; |
| 259 |
| 260 | void AddSendDelay(Timestamp now, TimeDelta send_delay); |
| 261 |
| 262 | Timestamp resolution_update = Timestamp::MinusInfinity(); |
| 263 | rtc::RateTracker encoded_frame_rate; |
| 264 |
| 265 | std::deque<SendDelayEntry> send_delays; |
| 266 |
| 267 | // The sum of `send_delay` in `send_delays`. |
| 268 | TimeDelta send_delay_sum = TimeDelta::Zero(); |
| 269 |
| 270 | // Pointer to the maximum `send_delay` in `send_delays` or nullptr if |
| 271 | // `send_delays.empty()` |
| 272 | const TimeDelta* send_delay_max = nullptr; |
| 273 | }; |
| 274 |
| 275 | void SetAdaptTimer(const MaskedAdaptationCounts& counts, StatsTimer* timer) |
| 276 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 277 | void UpdateAdaptationStats() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 278 | void TryUpdateInitialQualityResolutionAdaptUp( |
| 279 | absl::optional<int> old_quality_downscales, |
| 280 | absl::optional<int> updated_quality_downscales) |
| 281 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 282 |
| 283 | void UpdateEncoderFallbackStats(const CodecSpecificInfo* codec_info, |
| 284 | int pixels, |
| 285 | int simulcast_index) |
| 286 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 287 | void UpdateFallbackDisabledStats(const CodecSpecificInfo* codec_info, |
| 288 | int pixels, |
| 289 | int simulcast_index) |
| 290 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 291 |
| 292 | Clock* const clock_; |
| 293 | const std::string payload_name_; |
| 294 | const RtpConfig rtp_config_; |
| 295 | const absl::optional<int> fallback_max_pixels_; |
| 296 | const absl::optional<int> fallback_max_pixels_disabled_; |
| 297 | mutable Mutex mutex_; |
| 298 | VideoEncoderConfig::ContentType content_type_ RTC_GUARDED_BY(mutex_); |
| 299 | const int64_t start_ms_; |
| 300 | VideoSendStream::Stats stats_ RTC_GUARDED_BY(mutex_); |
| 301 | rtc::ExpFilter encode_time_ RTC_GUARDED_BY(mutex_); |
| 302 | QualityLimitationReasonTracker quality_limitation_reason_tracker_ |
| 303 | RTC_GUARDED_BY(mutex_); |
| 304 | rtc::RateTracker media_byte_rate_tracker_ RTC_GUARDED_BY(mutex_); |
| 305 | rtc::RateTracker encoded_frame_rate_tracker_ RTC_GUARDED_BY(mutex_); |
| 306 | // Trackers mapped by ssrc. |
| 307 | std::map<uint32_t, Trackers> trackers_ RTC_GUARDED_BY(mutex_); |
| 308 |
| 309 | absl::optional<int64_t> last_outlier_timestamp_ RTC_GUARDED_BY(mutex_); |
| 310 |
| 311 | int last_num_spatial_layers_ RTC_GUARDED_BY(mutex_); |
| 312 | int last_num_simulcast_streams_ RTC_GUARDED_BY(mutex_); |
| 313 | std::array<bool, kMaxSpatialLayers> last_spatial_layer_use_ |
| 314 | RTC_GUARDED_BY(mutex_); |
| 315 | // Indicates if the latest bitrate allocation had layers disabled by low |
| 316 | // available bandwidth. |
| 317 | bool bw_limited_layers_ RTC_GUARDED_BY(mutex_); |
| 318 | // Indicastes if the encoder internally downscales input image. |
| 319 | bool internal_encoder_scaler_ RTC_GUARDED_BY(mutex_); |
| 320 | Adaptations adaptation_limitations_ RTC_GUARDED_BY(mutex_); |
| 321 |
| 322 | struct EncoderChangeEvent { |
| 323 | std::string previous_encoder_implementation; |
| 324 | std::string new_encoder_implementation; |
| 325 | }; |
| 326 | // Stores the last change in encoder implementation in an optional, so that |
| 327 | // the event can be consumed. |
| 328 | absl::optional<EncoderChangeEvent> encoder_changed_; |
| 329 |
| 330 | // Contains stats used for UMA histograms. These stats will be reset if |
| 331 | // content type changes between real-time video and screenshare, since these |
| 332 | // will be reported separately. |
| 333 | struct UmaSamplesContainer { |
| 334 | UmaSamplesContainer(const char* prefix, |
| 335 | const VideoSendStream::Stats& start_stats, |
| 336 | Clock* clock); |
| 337 | ~UmaSamplesContainer(); |
| 338 |
| 339 | void UpdateHistograms(const RtpConfig& rtp_config, |
| 340 | const VideoSendStream::Stats& current_stats); |
| 341 |
| 342 | void InitializeBitrateCounters(const VideoSendStream::Stats& stats); |
| 343 |
| 344 | bool InsertEncodedFrame(const EncodedImage& encoded_frame, |
| 345 | int simulcast_idx); |
| 346 | void RemoveOld(int64_t now_ms); |
| 347 |
| 348 | const std::string uma_prefix_; |
| 349 | Clock* const clock_; |
| 350 | SampleCounter input_width_counter_; |
| 351 | SampleCounter input_height_counter_; |
| 352 | SampleCounter sent_width_counter_; |
| 353 | SampleCounter sent_height_counter_; |
| 354 | SampleCounter encode_time_counter_; |
| 355 | BoolSampleCounter key_frame_counter_; |
| 356 | BoolSampleCounter quality_limited_frame_counter_; |
| 357 | SampleCounter quality_downscales_counter_; |
| 358 | BoolSampleCounter cpu_limited_frame_counter_; |
| 359 | BoolSampleCounter bw_limited_frame_counter_; |
| 360 | SampleCounter bw_resolutions_disabled_counter_; |
| 361 | SampleCounter delay_counter_; |
| 362 | SampleCounter max_delay_counter_; |
| 363 | rtc::RateTracker input_frame_rate_tracker_; |
| 364 | RateCounter input_fps_counter_; |
| 365 | RateCounter sent_fps_counter_; |
| 366 | RateAccCounter total_byte_counter_; |
| 367 | RateAccCounter media_byte_counter_; |
| 368 | RateAccCounter rtx_byte_counter_; |
| 369 | RateAccCounter padding_byte_counter_; |
| 370 | RateAccCounter retransmit_byte_counter_; |
| 371 | RateAccCounter fec_byte_counter_; |
| 372 | int64_t first_rtcp_stats_time_ms_; |
| 373 | int64_t first_rtp_stats_time_ms_; |
| 374 | StatsTimer cpu_adapt_timer_; |
| 375 | StatsTimer quality_adapt_timer_; |
| 376 | BoolSampleCounter paused_time_counter_; |
| 377 | TargetRateUpdates target_rate_updates_; |
| 378 | BoolSampleCounter fallback_active_counter_; |
| 379 | FallbackEncoderInfo fallback_info_; |
| 380 | FallbackEncoderInfoDisabled fallback_info_disabled_; |
| 381 | ReportBlockStats report_block_stats_; |
| 382 | const VideoSendStream::Stats start_stats_; |
| 383 | size_t num_streams_; // Number of configured streams to encoder. |
| 384 | size_t num_pixels_highest_stream_; |
| 385 | EncodedFrameMap encoded_frames_; |
| 386 | AdaptChanges initial_quality_changes_; |
| 387 |
| 388 | std::map<int, QpCounters> |
| 389 | qp_counters_; // QP counters mapped by spatial idx. |
| 390 | }; |
| 391 |
| 392 | std::unique_ptr<UmaSamplesContainer> uma_container_ RTC_GUARDED_BY(mutex_); |
| 393 | }; |
| 394 |
| 395 | } // namespace webrtc |
| 396 | #endif // VIDEO_SEND_STATISTICS_PROXY_H_ |
| 397 |