receive_statistics_proxy.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_RECEIVE_STATISTICS_PROXY_H_
  11. #define VIDEO_RECEIVE_STATISTICS_PROXY_H_
  12. #include <map>
  13. #include <memory>
  14. #include <string>
  15. #include <vector>
  16. #include "absl/types/optional.h"
  17. #include "call/video_receive_stream.h"
  18. #include "modules/include/module_common_types.h"
  19. #include "modules/video_coding/include/video_coding_defines.h"
  20. #include "rtc_base/numerics/histogram_percentile_counter.h"
  21. #include "rtc_base/numerics/moving_max_counter.h"
  22. #include "rtc_base/numerics/sample_counter.h"
  23. #include "rtc_base/rate_statistics.h"
  24. #include "rtc_base/rate_tracker.h"
  25. #include "rtc_base/synchronization/mutex.h"
  26. #include "rtc_base/thread_annotations.h"
  27. #include "rtc_base/thread_checker.h"
  28. #include "video/quality_threshold.h"
  29. #include "video/stats_counter.h"
  30. #include "video/video_quality_observer.h"
  31. namespace webrtc {
  32. class Clock;
  33. struct CodecSpecificInfo;
  34. class ReceiveStatisticsProxy : public VCMReceiveStatisticsCallback,
  35. public RtcpCnameCallback,
  36. public RtcpPacketTypeCounterObserver,
  37. public CallStatsObserver {
  38. public:
  39. ReceiveStatisticsProxy(const VideoReceiveStream::Config* config,
  40. Clock* clock);
  41. ~ReceiveStatisticsProxy() = default;
  42. VideoReceiveStream::Stats GetStats() const;
  43. void OnDecodedFrame(const VideoFrame& frame,
  44. absl::optional<uint8_t> qp,
  45. int32_t decode_time_ms,
  46. VideoContentType content_type);
  47. void OnSyncOffsetUpdated(int64_t video_playout_ntp_ms,
  48. int64_t sync_offset_ms,
  49. double estimated_freq_khz);
  50. void OnRenderedFrame(const VideoFrame& frame);
  51. void OnIncomingPayloadType(int payload_type);
  52. void OnDecoderImplementationName(const char* implementation_name);
  53. void OnPreDecode(VideoCodecType codec_type, int qp);
  54. void OnUniqueFramesCounted(int num_unique_frames);
  55. // Indicates video stream has been paused (no incoming packets).
  56. void OnStreamInactive();
  57. // Overrides VCMReceiveStatisticsCallback.
  58. void OnCompleteFrame(bool is_keyframe,
  59. size_t size_bytes,
  60. VideoContentType content_type) override;
  61. void OnDroppedFrames(uint32_t frames_dropped) override;
  62. void OnFrameBufferTimingsUpdated(int max_decode_ms,
  63. int current_delay_ms,
  64. int target_delay_ms,
  65. int jitter_buffer_ms,
  66. int min_playout_delay_ms,
  67. int render_delay_ms) override;
  68. void OnTimingFrameInfoUpdated(const TimingFrameInfo& info) override;
  69. // Overrides RtcpCnameCallback.
  70. void OnCname(uint32_t ssrc, absl::string_view cname) override;
  71. // Overrides RtcpPacketTypeCounterObserver.
  72. void RtcpPacketTypesCounterUpdated(
  73. uint32_t ssrc,
  74. const RtcpPacketTypeCounter& packet_counter) override;
  75. // Implements CallStatsObserver.
  76. void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
  77. // Notification methods that are used to check our internal state and validate
  78. // threading assumptions. These are called by VideoReceiveStream.
  79. void DecoderThreadStarting();
  80. void DecoderThreadStopped();
  81. // Produce histograms. Must be called after DecoderThreadStopped(), typically
  82. // at the end of the call.
  83. void UpdateHistograms(absl::optional<int> fraction_lost,
  84. const StreamDataCounters& rtp_stats,
  85. const StreamDataCounters* rtx_stats);
  86. private:
  87. struct QpCounters {
  88. rtc::SampleCounter vp8;
  89. };
  90. struct ContentSpecificStats {
  91. ContentSpecificStats();
  92. ~ContentSpecificStats();
  93. void Add(const ContentSpecificStats& other);
  94. rtc::SampleCounter e2e_delay_counter;
  95. rtc::SampleCounter interframe_delay_counter;
  96. int64_t flow_duration_ms = 0;
  97. int64_t total_media_bytes = 0;
  98. rtc::SampleCounter received_width;
  99. rtc::SampleCounter received_height;
  100. rtc::SampleCounter qp_counter;
  101. FrameCounts frame_counts;
  102. rtc::HistogramPercentileCounter interframe_delay_percentiles;
  103. };
  104. void QualitySample() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  105. // Removes info about old frames and then updates the framerate.
  106. void UpdateFramerate(int64_t now_ms) const
  107. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  108. void UpdateDecodeTimeHistograms(int width,
  109. int height,
  110. int decode_time_ms) const
  111. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  112. absl::optional<int64_t> GetCurrentEstimatedPlayoutNtpTimestampMs(
  113. int64_t now_ms) const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  114. Clock* const clock_;
  115. // Ownership of this object lies with the owner of the ReceiveStatisticsProxy
  116. // instance. Lifetime is guaranteed to outlive |this|.
  117. // TODO(tommi): In practice the config_ reference is only used for accessing
  118. // config_.rtp.ulpfec.ulpfec_payload_type. Instead of holding a pointer back,
  119. // we could just store the value of ulpfec_payload_type and change the
  120. // ReceiveStatisticsProxy() ctor to accept a const& of Config (since we'll
  121. // then no longer store a pointer to the object).
  122. const VideoReceiveStream::Config& config_;
  123. const int64_t start_ms_;
  124. const bool enable_decode_time_histograms_;
  125. mutable Mutex mutex_;
  126. int64_t last_sample_time_ RTC_GUARDED_BY(mutex_);
  127. QualityThreshold fps_threshold_ RTC_GUARDED_BY(mutex_);
  128. QualityThreshold qp_threshold_ RTC_GUARDED_BY(mutex_);
  129. QualityThreshold variance_threshold_ RTC_GUARDED_BY(mutex_);
  130. rtc::SampleCounter qp_sample_ RTC_GUARDED_BY(mutex_);
  131. int num_bad_states_ RTC_GUARDED_BY(mutex_);
  132. int num_certain_states_ RTC_GUARDED_BY(mutex_);
  133. // Note: The |stats_.rtp_stats| member is not used or populated by this class.
  134. mutable VideoReceiveStream::Stats stats_ RTC_GUARDED_BY(mutex_);
  135. RateStatistics decode_fps_estimator_ RTC_GUARDED_BY(mutex_);
  136. RateStatistics renders_fps_estimator_ RTC_GUARDED_BY(mutex_);
  137. rtc::RateTracker render_fps_tracker_ RTC_GUARDED_BY(mutex_);
  138. rtc::RateTracker render_pixel_tracker_ RTC_GUARDED_BY(mutex_);
  139. rtc::SampleCounter sync_offset_counter_ RTC_GUARDED_BY(mutex_);
  140. rtc::SampleCounter decode_time_counter_ RTC_GUARDED_BY(mutex_);
  141. rtc::SampleCounter jitter_buffer_delay_counter_ RTC_GUARDED_BY(mutex_);
  142. rtc::SampleCounter target_delay_counter_ RTC_GUARDED_BY(mutex_);
  143. rtc::SampleCounter current_delay_counter_ RTC_GUARDED_BY(mutex_);
  144. rtc::SampleCounter delay_counter_ RTC_GUARDED_BY(mutex_);
  145. std::unique_ptr<VideoQualityObserver> video_quality_observer_
  146. RTC_GUARDED_BY(mutex_);
  147. mutable rtc::MovingMaxCounter<int> interframe_delay_max_moving_
  148. RTC_GUARDED_BY(mutex_);
  149. std::map<VideoContentType, ContentSpecificStats> content_specific_stats_
  150. RTC_GUARDED_BY(mutex_);
  151. MaxCounter freq_offset_counter_ RTC_GUARDED_BY(mutex_);
  152. QpCounters qp_counters_ RTC_GUARDED_BY(decode_thread_);
  153. int64_t avg_rtt_ms_ RTC_GUARDED_BY(mutex_);
  154. mutable std::map<int64_t, size_t> frame_window_ RTC_GUARDED_BY(&mutex_);
  155. VideoContentType last_content_type_ RTC_GUARDED_BY(&mutex_);
  156. VideoCodecType last_codec_type_ RTC_GUARDED_BY(&mutex_);
  157. absl::optional<int64_t> first_frame_received_time_ms_ RTC_GUARDED_BY(&mutex_);
  158. absl::optional<int64_t> first_decoded_frame_time_ms_ RTC_GUARDED_BY(&mutex_);
  159. absl::optional<int64_t> last_decoded_frame_time_ms_ RTC_GUARDED_BY(&mutex_);
  160. size_t num_delayed_frames_rendered_ RTC_GUARDED_BY(&mutex_);
  161. int64_t sum_missed_render_deadline_ms_ RTC_GUARDED_BY(&mutex_);
  162. // Mutable because calling Max() on MovingMaxCounter is not const. Yet it is
  163. // called from const GetStats().
  164. mutable rtc::MovingMaxCounter<TimingFrameInfo> timing_frame_info_counter_
  165. RTC_GUARDED_BY(&mutex_);
  166. absl::optional<int> num_unique_frames_ RTC_GUARDED_BY(mutex_);
  167. absl::optional<int64_t> last_estimated_playout_ntp_timestamp_ms_
  168. RTC_GUARDED_BY(&mutex_);
  169. absl::optional<int64_t> last_estimated_playout_time_ms_
  170. RTC_GUARDED_BY(&mutex_);
  171. rtc::ThreadChecker decode_thread_;
  172. rtc::ThreadChecker network_thread_;
  173. rtc::ThreadChecker main_thread_;
  174. };
  175. } // namespace webrtc
  176. #endif // VIDEO_RECEIVE_STATISTICS_PROXY_H_