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