receive_statistics_proxy2.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2020 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_RECEIVE_STATISTICS_PROXY2_H_
  11. #define VIDEO_RECEIVE_STATISTICS_PROXY2_H_
  12. #include <map>
  13. #include <memory>
  14. #include <string>
  15. #include <vector>
  16. #include "absl/types/optional.h"
  17. #include "api/task_queue/task_queue_base.h"
  18. #include "api/units/timestamp.h"
  19. #include "call/video_receive_stream.h"
  20. #include "modules/include/module_common_types.h"
  21. #include "modules/video_coding/include/video_coding_defines.h"
  22. #include "rtc_base/numerics/histogram_percentile_counter.h"
  23. #include "rtc_base/numerics/moving_max_counter.h"
  24. #include "rtc_base/numerics/sample_counter.h"
  25. #include "rtc_base/rate_statistics.h"
  26. #include "rtc_base/rate_tracker.h"
  27. #include "rtc_base/synchronization/sequence_checker.h"
  28. #include "rtc_base/system/no_unique_address.h"
  29. #include "rtc_base/task_utils/pending_task_safety_flag.h"
  30. #include "rtc_base/thread_annotations.h"
  31. #include "rtc_base/thread_checker.h"
  32. #include "video/quality_threshold.h"
  33. #include "video/stats_counter.h"
  34. #include "video/video_quality_observer2.h"
  35. namespace webrtc {
  36. class Clock;
  37. struct CodecSpecificInfo;
  38. namespace internal {
  39. // Declared in video_receive_stream2.h.
  40. struct VideoFrameMetaData;
  41. class ReceiveStatisticsProxy : public VCMReceiveStatisticsCallback,
  42. public RtcpCnameCallback,
  43. public RtcpPacketTypeCounterObserver {
  44. public:
  45. ReceiveStatisticsProxy(const VideoReceiveStream::Config* config,
  46. Clock* clock,
  47. TaskQueueBase* worker_thread);
  48. ~ReceiveStatisticsProxy() override;
  49. VideoReceiveStream::Stats GetStats() const;
  50. void OnDecodedFrame(const VideoFrame& frame,
  51. absl::optional<uint8_t> qp,
  52. int32_t decode_time_ms,
  53. VideoContentType content_type);
  54. // Called asyncronously on the worker thread as a result of a call to the
  55. // above OnDecodedFrame method, which is called back on the thread where
  56. // the actual decoding happens.
  57. void OnDecodedFrame(const VideoFrameMetaData& frame_meta,
  58. absl::optional<uint8_t> qp,
  59. int32_t decode_time_ms,
  60. VideoContentType content_type);
  61. void OnSyncOffsetUpdated(int64_t video_playout_ntp_ms,
  62. int64_t sync_offset_ms,
  63. double estimated_freq_khz);
  64. void OnRenderedFrame(const VideoFrameMetaData& frame_meta);
  65. void OnIncomingPayloadType(int payload_type);
  66. void OnDecoderImplementationName(const char* implementation_name);
  67. void OnPreDecode(VideoCodecType codec_type, int qp);
  68. void OnUniqueFramesCounted(int num_unique_frames);
  69. // Indicates video stream has been paused (no incoming packets).
  70. void OnStreamInactive();
  71. // Overrides VCMReceiveStatisticsCallback.
  72. void OnCompleteFrame(bool is_keyframe,
  73. size_t size_bytes,
  74. VideoContentType content_type) override;
  75. void OnDroppedFrames(uint32_t frames_dropped) override;
  76. void OnFrameBufferTimingsUpdated(int max_decode_ms,
  77. int current_delay_ms,
  78. int target_delay_ms,
  79. int jitter_buffer_ms,
  80. int min_playout_delay_ms,
  81. int render_delay_ms) override;
  82. void OnTimingFrameInfoUpdated(const TimingFrameInfo& info) override;
  83. // Overrides RtcpCnameCallback.
  84. void OnCname(uint32_t ssrc, absl::string_view cname) override;
  85. // Overrides RtcpPacketTypeCounterObserver.
  86. void RtcpPacketTypesCounterUpdated(
  87. uint32_t ssrc,
  88. const RtcpPacketTypeCounter& packet_counter) override;
  89. void OnRttUpdate(int64_t avg_rtt_ms);
  90. // Notification methods that are used to check our internal state and validate
  91. // threading assumptions. These are called by VideoReceiveStream.
  92. void DecoderThreadStarting();
  93. void DecoderThreadStopped();
  94. // Produce histograms. Must be called after DecoderThreadStopped(), typically
  95. // at the end of the call.
  96. void UpdateHistograms(absl::optional<int> fraction_lost,
  97. const StreamDataCounters& rtp_stats,
  98. const StreamDataCounters* rtx_stats);
  99. private:
  100. struct QpCounters {
  101. rtc::SampleCounter vp8;
  102. };
  103. struct ContentSpecificStats {
  104. ContentSpecificStats();
  105. ~ContentSpecificStats();
  106. void Add(const ContentSpecificStats& other);
  107. rtc::SampleCounter e2e_delay_counter;
  108. rtc::SampleCounter interframe_delay_counter;
  109. int64_t flow_duration_ms = 0;
  110. int64_t total_media_bytes = 0;
  111. rtc::SampleCounter received_width;
  112. rtc::SampleCounter received_height;
  113. rtc::SampleCounter qp_counter;
  114. FrameCounts frame_counts;
  115. rtc::HistogramPercentileCounter interframe_delay_percentiles;
  116. };
  117. void QualitySample(Timestamp now);
  118. // Removes info about old frames and then updates the framerate.
  119. void UpdateFramerate(int64_t now_ms) const;
  120. void UpdateDecodeTimeHistograms(int width,
  121. int height,
  122. int decode_time_ms) const;
  123. absl::optional<int64_t> GetCurrentEstimatedPlayoutNtpTimestampMs(
  124. int64_t now_ms) const;
  125. Clock* const clock_;
  126. const int64_t start_ms_;
  127. const bool enable_decode_time_histograms_;
  128. int64_t last_sample_time_ RTC_GUARDED_BY(main_thread_);
  129. QualityThreshold fps_threshold_ RTC_GUARDED_BY(main_thread_);
  130. QualityThreshold qp_threshold_ RTC_GUARDED_BY(main_thread_);
  131. QualityThreshold variance_threshold_ RTC_GUARDED_BY(main_thread_);
  132. rtc::SampleCounter qp_sample_ RTC_GUARDED_BY(main_thread_);
  133. int num_bad_states_ RTC_GUARDED_BY(main_thread_);
  134. int num_certain_states_ RTC_GUARDED_BY(main_thread_);
  135. // Note: The |stats_.rtp_stats| member is not used or populated by this class.
  136. mutable VideoReceiveStream::Stats stats_ RTC_GUARDED_BY(main_thread_);
  137. // Same as stats_.ssrc, but const (no lock required).
  138. const uint32_t remote_ssrc_;
  139. RateStatistics decode_fps_estimator_ RTC_GUARDED_BY(main_thread_);
  140. RateStatistics renders_fps_estimator_ RTC_GUARDED_BY(main_thread_);
  141. rtc::RateTracker render_fps_tracker_ RTC_GUARDED_BY(main_thread_);
  142. rtc::RateTracker render_pixel_tracker_ RTC_GUARDED_BY(main_thread_);
  143. rtc::SampleCounter sync_offset_counter_ RTC_GUARDED_BY(main_thread_);
  144. rtc::SampleCounter decode_time_counter_ RTC_GUARDED_BY(main_thread_);
  145. rtc::SampleCounter jitter_buffer_delay_counter_ RTC_GUARDED_BY(main_thread_);
  146. rtc::SampleCounter target_delay_counter_ RTC_GUARDED_BY(main_thread_);
  147. rtc::SampleCounter current_delay_counter_ RTC_GUARDED_BY(main_thread_);
  148. rtc::SampleCounter delay_counter_ RTC_GUARDED_BY(main_thread_);
  149. std::unique_ptr<VideoQualityObserver> video_quality_observer_
  150. RTC_GUARDED_BY(main_thread_);
  151. mutable rtc::MovingMaxCounter<int> interframe_delay_max_moving_
  152. RTC_GUARDED_BY(main_thread_);
  153. std::map<VideoContentType, ContentSpecificStats> content_specific_stats_
  154. RTC_GUARDED_BY(main_thread_);
  155. MaxCounter freq_offset_counter_ RTC_GUARDED_BY(main_thread_);
  156. QpCounters qp_counters_ RTC_GUARDED_BY(main_thread_);
  157. int64_t avg_rtt_ms_ RTC_GUARDED_BY(main_thread_) = 0;
  158. mutable std::map<int64_t, size_t> frame_window_ RTC_GUARDED_BY(main_thread_);
  159. VideoContentType last_content_type_ RTC_GUARDED_BY(&main_thread_);
  160. VideoCodecType last_codec_type_ RTC_GUARDED_BY(main_thread_);
  161. absl::optional<int64_t> first_frame_received_time_ms_
  162. RTC_GUARDED_BY(main_thread_);
  163. absl::optional<int64_t> first_decoded_frame_time_ms_
  164. RTC_GUARDED_BY(main_thread_);
  165. absl::optional<int64_t> last_decoded_frame_time_ms_
  166. RTC_GUARDED_BY(main_thread_);
  167. size_t num_delayed_frames_rendered_ RTC_GUARDED_BY(main_thread_);
  168. int64_t sum_missed_render_deadline_ms_ RTC_GUARDED_BY(main_thread_);
  169. // Mutable because calling Max() on MovingMaxCounter is not const. Yet it is
  170. // called from const GetStats().
  171. mutable rtc::MovingMaxCounter<TimingFrameInfo> timing_frame_info_counter_
  172. RTC_GUARDED_BY(main_thread_);
  173. absl::optional<int> num_unique_frames_ RTC_GUARDED_BY(main_thread_);
  174. absl::optional<int64_t> last_estimated_playout_ntp_timestamp_ms_
  175. RTC_GUARDED_BY(main_thread_);
  176. absl::optional<int64_t> last_estimated_playout_time_ms_
  177. RTC_GUARDED_BY(main_thread_);
  178. // The thread on which this instance is constructed and some of its main
  179. // methods are invoked on such as GetStats().
  180. TaskQueueBase* const worker_thread_;
  181. ScopedTaskSafety task_safety_;
  182. RTC_NO_UNIQUE_ADDRESS SequenceChecker decode_queue_;
  183. rtc::ThreadChecker main_thread_;
  184. RTC_NO_UNIQUE_ADDRESS SequenceChecker incoming_render_queue_;
  185. };
  186. } // namespace internal
  187. } // namespace webrtc
  188. #endif // VIDEO_RECEIVE_STATISTICS_PROXY2_H_