receive_statistics_proxy2.h 8.9 KB

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