blob: f8afe45cffb48d6f4d4b3e75fa117bf209410bb9 [file] [log] [blame]
sprang@webrtc.org09315702014-02-07 12:06:29 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef VIDEO_RECEIVE_STATISTICS_PROXY_H_
12#define VIDEO_RECEIVE_STATISTICS_PROXY_H_
sprang@webrtc.org09315702014-02-07 12:06:29 +000013
sprang0ab8e812016-02-24 01:35:40 -080014#include <map>
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +020015#include <memory>
sprang@webrtc.org09315702014-02-07 12:06:29 +000016#include <string>
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +020017#include <vector>
sprang@webrtc.org09315702014-02-07 12:06:29 +000018
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020019#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "call/video_receive_stream.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020021#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "common_video/include/frame_callback.h"
23#include "modules/video_coding/include/video_coding_defines.h"
24#include "rtc_base/criticalsection.h"
Karl Wiberg65c39222017-11-22 12:25:14 +010025#include "rtc_base/numerics/histogram_percentile_counter.h"
26#include "rtc_base/numerics/moving_max_counter.h"
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020027#include "rtc_base/numerics/sample_counter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/rate_statistics.h"
29#include "rtc_base/ratetracker.h"
30#include "rtc_base/thread_annotations.h"
Tommi132e28e2018-02-24 17:57:33 +010031#include "rtc_base/thread_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "video/quality_threshold.h"
33#include "video/report_block_stats.h"
34#include "video/stats_counter.h"
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +020035#include "video/video_quality_observer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "video/video_stream_decoder.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000037
38namespace webrtc {
39
40class Clock;
asapersson86b01602015-10-20 23:55:26 -070041struct CodecSpecificInfo;
sprang@webrtc.org09315702014-02-07 12:06:29 +000042
pbosf42376c2015-08-28 07:35:32 -070043class ReceiveStatisticsProxy : public VCMReceiveStatisticsCallback,
sprang@webrtc.org09315702014-02-07 12:06:29 +000044 public RtcpStatisticsCallback,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000045 public RtcpPacketTypeCounterObserver,
philipela45102f2017-02-22 05:30:39 -080046 public StreamDataCountersCallback,
47 public CallStatsObserver {
sprang@webrtc.org09315702014-02-07 12:06:29 +000048 public:
Tommi733b5472016-06-10 17:58:01 +020049 ReceiveStatisticsProxy(const VideoReceiveStream::Config* config,
sprang0ab8e812016-02-24 01:35:40 -080050 Clock* clock);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020051 ~ReceiveStatisticsProxy() override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000052
53 VideoReceiveStream::Stats GetStats() const;
54
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020055 void OnDecodedFrame(absl::optional<uint8_t> qp,
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +020056 int width,
57 int height,
58 VideoContentType content_type);
asaperssonde9e5ff2016-11-02 07:14:03 -070059 void OnSyncOffsetUpdated(int64_t sync_offset_ms, double estimated_freq_khz);
asapersson1490f7a2016-09-23 02:09:46 -070060 void OnRenderedFrame(const VideoFrame& frame);
pbosf42376c2015-08-28 07:35:32 -070061 void OnIncomingPayloadType(int payload_type);
Peter Boströmb7d9a972015-12-18 16:01:11 +010062 void OnDecoderImplementationName(const char* implementation_name);
pbosf42376c2015-08-28 07:35:32 -070063 void OnIncomingRate(unsigned int framerate, unsigned int bitrate_bps);
sprang@webrtc.org09315702014-02-07 12:06:29 +000064
asapersson86b01602015-10-20 23:55:26 -070065 void OnPreDecode(const EncodedImage& encoded_image,
66 const CodecSpecificInfo* codec_specific_info);
67
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010068 void OnUniqueFramesCounted(int num_unique_frames);
69
sprang3e86e7e2017-08-22 09:23:28 -070070 // Indicates video stream has been paused (no incoming packets).
71 void OnStreamInactive();
72
asaperssond89920b2015-07-22 06:52:00 -070073 // Overrides VCMReceiveStatisticsCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000074 void OnReceiveRatesUpdated(uint32_t bitRate, uint32_t frameRate) override;
75 void OnFrameCountsUpdated(const FrameCounts& frame_counts) override;
76 void OnDiscardedPacketsUpdated(int discarded_packets) override;
ilnik6d5b4d62017-08-30 03:32:14 -070077 void OnCompleteFrame(bool is_keyframe,
78 size_t size_bytes,
79 VideoContentType content_type) override;
philipela45102f2017-02-22 05:30:39 -080080 void OnFrameBufferTimingsUpdated(int decode_ms,
81 int max_decode_ms,
82 int current_delay_ms,
83 int target_delay_ms,
84 int jitter_buffer_ms,
85 int min_playout_delay_ms,
86 int render_delay_ms) override;
pbos@webrtc.org55707692014-12-19 15:45:03 +000087
ilnik2edc6842017-07-06 03:06:50 -070088 void OnTimingFrameInfoUpdated(const TimingFrameInfo& info) override;
89
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000090 // Overrides RtcpStatisticsCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000091 void StatisticsUpdated(const webrtc::RtcpStatistics& statistics,
92 uint32_t ssrc) override;
93 void CNameChanged(const char* cname, uint32_t ssrc) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000094
asaperssond89920b2015-07-22 06:52:00 -070095 // Overrides RtcpPacketTypeCounterObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000096 void RtcpPacketTypesCounterUpdated(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000097 uint32_t ssrc,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000098 const RtcpPacketTypeCounter& packet_counter) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000099 // Overrides StreamDataCountersCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000100 void DataCountersUpdated(const webrtc::StreamDataCounters& counters,
101 uint32_t ssrc) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000102
philipela45102f2017-02-22 05:30:39 -0800103 // Implements CallStatsObserver.
104 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
105
Tommi132e28e2018-02-24 17:57:33 +0100106 // Notification methods that are used to check our internal state and validate
107 // threading assumptions. These are called by VideoReceiveStream.
108 void DecoderThreadStarting();
109 void DecoderThreadStopped();
110
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000111 private:
asapersson86b01602015-10-20 23:55:26 -0700112 struct QpCounters {
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200113 rtc::SampleCounter vp8;
asapersson86b01602015-10-20 23:55:26 -0700114 };
asaperssond89920b2015-07-22 06:52:00 -0700115
ilnik6d5b4d62017-08-30 03:32:14 -0700116 struct ContentSpecificStats {
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200117 ContentSpecificStats();
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200118 ~ContentSpecificStats();
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200119
ilnik6d5b4d62017-08-30 03:32:14 -0700120 void Add(const ContentSpecificStats& other);
121
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200122 rtc::SampleCounter e2e_delay_counter;
123 rtc::SampleCounter interframe_delay_counter;
ilnik6d5b4d62017-08-30 03:32:14 -0700124 int64_t flow_duration_ms = 0;
125 int64_t total_media_bytes = 0;
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200126 rtc::SampleCounter received_width;
127 rtc::SampleCounter received_height;
128 rtc::SampleCounter qp_counter;
ilnik6d5b4d62017-08-30 03:32:14 -0700129 FrameCounts frame_counts;
Ilya Nikolaevskiyed23be92017-10-12 12:38:01 +0200130 rtc::HistogramPercentileCounter interframe_delay_percentiles;
ilnik6d5b4d62017-08-30 03:32:14 -0700131 };
132
danilchapa37de392017-09-09 04:17:22 -0700133 void UpdateHistograms() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
asaperssond89920b2015-07-22 06:52:00 -0700134
danilchapa37de392017-09-09 04:17:22 -0700135 void QualitySample() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
palmkvist349092b2016-12-13 02:45:57 -0800136
asapersson0255acb2017-03-28 02:44:58 -0700137 // Removes info about old frames and then updates the framerate.
danilchapa37de392017-09-09 04:17:22 -0700138 void UpdateFramerate(int64_t now_ms) const
139 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipela45102f2017-02-22 05:30:39 -0800140
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000141 Clock* const clock_;
Tommi733b5472016-06-10 17:58:01 +0200142 // Ownership of this object lies with the owner of the ReceiveStatisticsProxy
143 // instance. Lifetime is guaranteed to outlive |this|.
144 // TODO(tommi): In practice the config_ reference is only used for accessing
brandtrb5f2c3f2016-10-04 23:28:39 -0700145 // config_.rtp.ulpfec.ulpfec_payload_type. Instead of holding a pointer back,
Tommi733b5472016-06-10 17:58:01 +0200146 // we could just store the value of ulpfec_payload_type and change the
147 // ReceiveStatisticsProxy() ctor to accept a const& of Config (since we'll
148 // then no longer store a pointer to the object).
149 const VideoReceiveStream::Config& config_;
asapersson4374a092016-07-27 00:39:09 -0700150 const int64_t start_ms_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000151
pbos5ad935c2016-01-25 03:52:44 -0800152 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700153 int64_t last_sample_time_ RTC_GUARDED_BY(crit_);
154 QualityThreshold fps_threshold_ RTC_GUARDED_BY(crit_);
155 QualityThreshold qp_threshold_ RTC_GUARDED_BY(crit_);
156 QualityThreshold variance_threshold_ RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200157 rtc::SampleCounter qp_sample_ RTC_GUARDED_BY(crit_);
danilchapa37de392017-09-09 04:17:22 -0700158 int num_bad_states_ RTC_GUARDED_BY(crit_);
159 int num_certain_states_ RTC_GUARDED_BY(crit_);
160 mutable VideoReceiveStream::Stats stats_ RTC_GUARDED_BY(crit_);
161 RateStatistics decode_fps_estimator_ RTC_GUARDED_BY(crit_);
162 RateStatistics renders_fps_estimator_ RTC_GUARDED_BY(crit_);
163 rtc::RateTracker render_fps_tracker_ RTC_GUARDED_BY(crit_);
164 rtc::RateTracker render_pixel_tracker_ RTC_GUARDED_BY(crit_);
165 rtc::RateTracker total_byte_tracker_ RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200166 rtc::SampleCounter sync_offset_counter_ RTC_GUARDED_BY(crit_);
167 rtc::SampleCounter decode_time_counter_ RTC_GUARDED_BY(crit_);
168 rtc::SampleCounter jitter_buffer_delay_counter_ RTC_GUARDED_BY(crit_);
169 rtc::SampleCounter target_delay_counter_ RTC_GUARDED_BY(crit_);
170 rtc::SampleCounter current_delay_counter_ RTC_GUARDED_BY(crit_);
171 rtc::SampleCounter delay_counter_ RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200172 std::unique_ptr<VideoQualityObserver> video_quality_observer_
173 RTC_GUARDED_BY(crit_);
ilnika79cc282017-08-23 05:24:10 -0700174 mutable rtc::MovingMaxCounter<int> interframe_delay_max_moving_
danilchapa37de392017-09-09 04:17:22 -0700175 RTC_GUARDED_BY(crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700176 std::map<VideoContentType, ContentSpecificStats> content_specific_stats_
danilchapa37de392017-09-09 04:17:22 -0700177 RTC_GUARDED_BY(crit_);
178 MaxCounter freq_offset_counter_ RTC_GUARDED_BY(crit_);
179 int64_t first_report_block_time_ms_ RTC_GUARDED_BY(crit_);
180 ReportBlockStats report_block_stats_ RTC_GUARDED_BY(crit_);
Tommi132e28e2018-02-24 17:57:33 +0100181 QpCounters qp_counters_ RTC_GUARDED_BY(decode_thread_);
danilchapa37de392017-09-09 04:17:22 -0700182 std::map<uint32_t, StreamDataCounters> rtx_stats_ RTC_GUARDED_BY(crit_);
183 int64_t avg_rtt_ms_ RTC_GUARDED_BY(crit_);
184 mutable std::map<int64_t, size_t> frame_window_ RTC_GUARDED_BY(&crit_);
185 VideoContentType last_content_type_ RTC_GUARDED_BY(&crit_);
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200186 VideoCodecType last_codec_type_ RTC_GUARDED_BY(&crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200187 absl::optional<int64_t> first_decoded_frame_time_ms_ RTC_GUARDED_BY(&crit_);
188 absl::optional<int64_t> last_decoded_frame_time_ms_ RTC_GUARDED_BY(&crit_);
Åsa Persson81327d52018-06-05 13:34:33 +0200189 size_t num_delayed_frames_rendered_ RTC_GUARDED_BY(&crit_);
190 int64_t sum_missed_render_deadline_ms_ RTC_GUARDED_BY(&crit_);
ilnik75204c52017-09-04 03:35:40 -0700191 // Mutable because calling Max() on MovingMaxCounter is not const. Yet it is
192 // called from const GetStats().
193 mutable rtc::MovingMaxCounter<TimingFrameInfo> timing_frame_info_counter_
danilchapa37de392017-09-09 04:17:22 -0700194 RTC_GUARDED_BY(&crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200195 absl::optional<int> num_unique_frames_ RTC_GUARDED_BY(crit_);
Tommi132e28e2018-02-24 17:57:33 +0100196 rtc::ThreadChecker decode_thread_;
197 rtc::ThreadChecker network_thread_;
198 rtc::ThreadChecker main_thread_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000199};
200
sprang@webrtc.org09315702014-02-07 12:06:29 +0000201} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200202#endif // VIDEO_RECEIVE_STATISTICS_PROXY_H_