You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef VIDEO_SEND_STATISTICS_PROXY_H_
  11. #define VIDEO_SEND_STATISTICS_PROXY_H_
  12. #include <array>
  13. #include <map>
  14. #include <memory>
  15. #include <string>
  16. #include <vector>
  17. #include "api/video/video_codec_constants.h"
  18. #include "api/video/video_stream_encoder_observer.h"
  19. #include "api/video_codecs/video_encoder_config.h"
  20. #include "call/video_send_stream.h"
  21. #include "modules/include/module_common_types_public.h"
  22. #include "modules/rtp_rtcp/include/report_block_data.h"
  23. #include "modules/video_coding/include/video_codec_interface.h"
  24. #include "modules/video_coding/include/video_coding_defines.h"
  25. #include "rtc_base/critical_section.h"
  26. #include "rtc_base/numerics/exp_filter.h"
  27. #include "rtc_base/rate_tracker.h"
  28. #include "rtc_base/thread_annotations.h"
  29. #include "system_wrappers/include/clock.h"
  30. #include "video/quality_limitation_reason_tracker.h"
  31. #include "video/report_block_stats.h"
  32. #include "video/stats_counter.h"
  33. namespace webrtc {
  34. class SendStatisticsProxy : public VideoStreamEncoderObserver,
  35. public RtcpStatisticsCallback,
  36. public ReportBlockDataObserver,
  37. public RtcpPacketTypeCounterObserver,
  38. public StreamDataCountersCallback,
  39. public BitrateStatisticsObserver,
  40. public FrameCountObserver,
  41. public SendSideDelayObserver {
  42. public:
  43. static const int kStatsTimeoutMs;
  44. // Number of required samples to be collected before a metric is added
  45. // to a rtc histogram.
  46. static const int kMinRequiredMetricsSamples = 200;
  47. SendStatisticsProxy(Clock* clock,
  48. const VideoSendStream::Config& config,
  49. VideoEncoderConfig::ContentType content_type);
  50. ~SendStatisticsProxy() override;
  51. virtual VideoSendStream::Stats GetStats();
  52. void OnSendEncodedImage(const EncodedImage& encoded_image,
  53. const CodecSpecificInfo* codec_info) override;
  54. void OnEncoderImplementationChanged(
  55. const std::string& implementation_name) override;
  56. // Used to update incoming frame rate.
  57. void OnIncomingFrame(int width, int height) override;
  58. // Dropped frame stats.
  59. void OnFrameDropped(DropReason) override;
  60. // Adaptation stats.
  61. void OnAdaptationChanged(AdaptationReason reason,
  62. const AdaptationSteps& cpu_counts,
  63. const AdaptationSteps& quality_counts) override;
  64. void OnBitrateAllocationUpdated(
  65. const VideoCodec& codec,
  66. const VideoBitrateAllocation& allocation) override;
  67. void OnMinPixelLimitReached() override;
  68. void OnInitialQualityResolutionAdaptDown() override;
  69. void OnSuspendChange(bool is_suspended) override;
  70. void OnInactiveSsrc(uint32_t ssrc);
  71. // Used to indicate change in content type, which may require a change in
  72. // how stats are collected.
  73. void OnEncoderReconfigured(const VideoEncoderConfig& encoder_config,
  74. const std::vector<VideoStream>& streams) override;
  75. // Used to update the encoder target rate.
  76. void OnSetEncoderTargetRate(uint32_t bitrate_bps);
  77. // Implements CpuOveruseMetricsObserver.
  78. void OnEncodedFrameTimeMeasured(int encode_time_ms,
  79. int encode_usage_percent) override;
  80. int GetInputFrameRate() const override;
  81. int GetSendFrameRate() const;
  82. protected:
  83. // From RtcpStatisticsCallback.
  84. void StatisticsUpdated(const RtcpStatistics& statistics,
  85. uint32_t ssrc) override;
  86. // From ReportBlockDataObserver.
  87. void OnReportBlockDataUpdated(ReportBlockData report_block_data) override;
  88. // From RtcpPacketTypeCounterObserver.
  89. void RtcpPacketTypesCounterUpdated(
  90. uint32_t ssrc,
  91. const RtcpPacketTypeCounter& packet_counter) override;
  92. // From StreamDataCountersCallback.
  93. void DataCountersUpdated(const StreamDataCounters& counters,
  94. uint32_t ssrc) override;
  95. // From BitrateStatisticsObserver.
  96. void Notify(uint32_t total_bitrate_bps,
  97. uint32_t retransmit_bitrate_bps,
  98. uint32_t ssrc) override;
  99. // From FrameCountObserver.
  100. void FrameCountUpdated(const FrameCounts& frame_counts,
  101. uint32_t ssrc) override;
  102. void SendSideDelayUpdated(int avg_delay_ms,
  103. int max_delay_ms,
  104. uint64_t total_delay_ms,
  105. uint32_t ssrc) override;
  106. private:
  107. class SampleCounter {
  108. public:
  109. SampleCounter() : sum(0), num_samples(0) {}
  110. ~SampleCounter() {}
  111. void Add(int sample);
  112. int Avg(int64_t min_required_samples) const;
  113. private:
  114. int64_t sum;
  115. int64_t num_samples;
  116. };
  117. class BoolSampleCounter {
  118. public:
  119. BoolSampleCounter() : sum(0), num_samples(0) {}
  120. ~BoolSampleCounter() {}
  121. void Add(bool sample);
  122. void Add(bool sample, int64_t count);
  123. int Percent(int64_t min_required_samples) const;
  124. int Permille(int64_t min_required_samples) const;
  125. private:
  126. int Fraction(int64_t min_required_samples, float multiplier) const;
  127. int64_t sum;
  128. int64_t num_samples;
  129. };
  130. struct StatsUpdateTimes {
  131. StatsUpdateTimes() : resolution_update_ms(0), bitrate_update_ms(0) {}
  132. int64_t resolution_update_ms;
  133. int64_t bitrate_update_ms;
  134. };
  135. struct TargetRateUpdates {
  136. TargetRateUpdates()
  137. : pause_resume_events(0), last_paused_or_resumed(false), last_ms(-1) {}
  138. int pause_resume_events;
  139. bool last_paused_or_resumed;
  140. int64_t last_ms;
  141. };
  142. struct FallbackEncoderInfo {
  143. FallbackEncoderInfo();
  144. bool is_possible = true;
  145. bool is_active = false;
  146. int on_off_events = 0;
  147. int64_t elapsed_ms = 0;
  148. absl::optional<int64_t> last_update_ms;
  149. const int max_frame_diff_ms = 2000;
  150. };
  151. struct FallbackEncoderInfoDisabled {
  152. bool is_possible = true;
  153. bool min_pixel_limit_reached = false;
  154. };
  155. struct StatsTimer {
  156. void Start(int64_t now_ms);
  157. void Stop(int64_t now_ms);
  158. void Restart(int64_t now_ms);
  159. int64_t start_ms = -1;
  160. int64_t total_ms = 0;
  161. };
  162. struct QpCounters {
  163. SampleCounter vp8; // QP range: 0-127.
  164. SampleCounter vp9; // QP range: 0-255.
  165. SampleCounter h264; // QP range: 0-51.
  166. };
  167. struct AdaptChanges {
  168. int down = 0;
  169. int up = 0;
  170. };
  171. // Map holding encoded frames (mapped by timestamp).
  172. // If simulcast layers are encoded on different threads, there is no guarantee
  173. // that one frame of all layers are encoded before the next start.
  174. struct TimestampOlderThan {
  175. bool operator()(uint32_t ts1, uint32_t ts2) const {
  176. return IsNewerTimestamp(ts2, ts1);
  177. }
  178. };
  179. struct Frame {
  180. Frame(int64_t send_ms, uint32_t width, uint32_t height, int simulcast_idx)
  181. : send_ms(send_ms),
  182. max_width(width),
  183. max_height(height),
  184. max_simulcast_idx(simulcast_idx) {}
  185. const int64_t
  186. send_ms; // Time when first frame with this timestamp is sent.
  187. uint32_t max_width; // Max width with this timestamp.
  188. uint32_t max_height; // Max height with this timestamp.
  189. int max_simulcast_idx; // Max simulcast index with this timestamp.
  190. };
  191. typedef std::map<uint32_t, Frame, TimestampOlderThan> EncodedFrameMap;
  192. void PurgeOldStats() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  193. VideoSendStream::StreamStats* GetStatsEntry(uint32_t ssrc)
  194. RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  195. void SetAdaptTimer(const AdaptationSteps& counts, StatsTimer* timer)
  196. RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  197. void UpdateAdaptationStats() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  198. void TryUpdateInitialQualityResolutionAdaptUp(
  199. const AdaptationSteps& quality_counts)
  200. RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  201. void UpdateEncoderFallbackStats(const CodecSpecificInfo* codec_info,
  202. int pixels,
  203. int simulcast_index)
  204. RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  205. void UpdateFallbackDisabledStats(const CodecSpecificInfo* codec_info,
  206. int pixels,
  207. int simulcast_index)
  208. RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  209. Clock* const clock_;
  210. const std::string payload_name_;
  211. const RtpConfig rtp_config_;
  212. const absl::optional<int> fallback_max_pixels_;
  213. const absl::optional<int> fallback_max_pixels_disabled_;
  214. rtc::CriticalSection crit_;
  215. VideoEncoderConfig::ContentType content_type_ RTC_GUARDED_BY(crit_);
  216. const int64_t start_ms_;
  217. VideoSendStream::Stats stats_ RTC_GUARDED_BY(crit_);
  218. std::map<uint32_t, StatsUpdateTimes> update_times_ RTC_GUARDED_BY(crit_);
  219. rtc::ExpFilter encode_time_ RTC_GUARDED_BY(crit_);
  220. int quality_downscales_ RTC_GUARDED_BY(crit_);
  221. int cpu_downscales_ RTC_GUARDED_BY(crit_);
  222. QualityLimitationReasonTracker quality_limitation_reason_tracker_
  223. RTC_GUARDED_BY(crit_);
  224. rtc::RateTracker media_byte_rate_tracker_ RTC_GUARDED_BY(crit_);
  225. rtc::RateTracker encoded_frame_rate_tracker_ RTC_GUARDED_BY(crit_);
  226. absl::optional<int64_t> last_outlier_timestamp_ RTC_GUARDED_BY(crit_);
  227. int last_num_spatial_layers_ RTC_GUARDED_BY(crit_);
  228. int last_num_simulcast_streams_ RTC_GUARDED_BY(crit_);
  229. std::array<bool, kMaxSpatialLayers> last_spatial_layer_use_
  230. RTC_GUARDED_BY(crit_);
  231. // Indicates if the latest bitrate allocation had layers disabled by low
  232. // available bandwidth.
  233. bool bw_limited_layers_ RTC_GUARDED_BY(crit_);
  234. AdaptationSteps cpu_counts_ RTC_GUARDED_BY(crit_);
  235. AdaptationSteps quality_counts_ RTC_GUARDED_BY(crit_);
  236. struct EncoderChangeEvent {
  237. std::string previous_encoder_implementation;
  238. std::string new_encoder_implementation;
  239. };
  240. // Stores the last change in encoder implementation in an optional, so that
  241. // the event can be consumed.
  242. absl::optional<EncoderChangeEvent> encoder_changed_;
  243. // Contains stats used for UMA histograms. These stats will be reset if
  244. // content type changes between real-time video and screenshare, since these
  245. // will be reported separately.
  246. struct UmaSamplesContainer {
  247. UmaSamplesContainer(const char* prefix,
  248. const VideoSendStream::Stats& start_stats,
  249. Clock* clock);
  250. ~UmaSamplesContainer();
  251. void UpdateHistograms(const RtpConfig& rtp_config,
  252. const VideoSendStream::Stats& current_stats);
  253. void InitializeBitrateCounters(const VideoSendStream::Stats& stats);
  254. bool InsertEncodedFrame(const EncodedImage& encoded_frame,
  255. int simulcast_idx,
  256. bool* is_limited_in_resolution);
  257. void RemoveOld(int64_t now_ms, bool* is_limited_in_resolution);
  258. const std::string uma_prefix_;
  259. Clock* const clock_;
  260. SampleCounter input_width_counter_;
  261. SampleCounter input_height_counter_;
  262. SampleCounter sent_width_counter_;
  263. SampleCounter sent_height_counter_;
  264. SampleCounter encode_time_counter_;
  265. BoolSampleCounter key_frame_counter_;
  266. BoolSampleCounter quality_limited_frame_counter_;
  267. SampleCounter quality_downscales_counter_;
  268. BoolSampleCounter cpu_limited_frame_counter_;
  269. BoolSampleCounter bw_limited_frame_counter_;
  270. SampleCounter bw_resolutions_disabled_counter_;
  271. SampleCounter delay_counter_;
  272. SampleCounter max_delay_counter_;
  273. rtc::RateTracker input_frame_rate_tracker_;
  274. RateCounter input_fps_counter_;
  275. RateCounter sent_fps_counter_;
  276. RateAccCounter total_byte_counter_;
  277. RateAccCounter media_byte_counter_;
  278. RateAccCounter rtx_byte_counter_;
  279. RateAccCounter padding_byte_counter_;
  280. RateAccCounter retransmit_byte_counter_;
  281. RateAccCounter fec_byte_counter_;
  282. int64_t first_rtcp_stats_time_ms_;
  283. int64_t first_rtp_stats_time_ms_;
  284. StatsTimer cpu_adapt_timer_;
  285. StatsTimer quality_adapt_timer_;
  286. BoolSampleCounter paused_time_counter_;
  287. TargetRateUpdates target_rate_updates_;
  288. BoolSampleCounter fallback_active_counter_;
  289. FallbackEncoderInfo fallback_info_;
  290. FallbackEncoderInfoDisabled fallback_info_disabled_;
  291. ReportBlockStats report_block_stats_;
  292. const VideoSendStream::Stats start_stats_;
  293. size_t num_streams_; // Number of configured streams to encoder.
  294. size_t num_pixels_highest_stream_;
  295. EncodedFrameMap encoded_frames_;
  296. AdaptChanges initial_quality_changes_;
  297. std::map<int, QpCounters>
  298. qp_counters_; // QP counters mapped by spatial idx.
  299. };
  300. std::unique_ptr<UmaSamplesContainer> uma_container_ RTC_GUARDED_BY(crit_);
  301. };
  302. } // namespace webrtc
  303. #endif // VIDEO_SEND_STATISTICS_PROXY_H_