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