blob: e874ddc827d92d61ebe179293e4260771f055ff3 [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>
sprang@webrtc.org09315702014-02-07 12:06:29 +000015#include <string>
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +020016#include <vector>
sprang@webrtc.org09315702014-02-07 12:06:29 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/optional.h"
19#include "call/video_receive_stream.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020020#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "common_video/include/frame_callback.h"
22#include "modules/video_coding/include/video_coding_defines.h"
23#include "rtc_base/criticalsection.h"
Ilya Nikolaevskiyed23be92017-10-12 12:38:01 +020024#include "rtc_base/histogram_percentile_counter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/moving_max_counter.h"
26#include "rtc_base/rate_statistics.h"
27#include "rtc_base/ratetracker.h"
28#include "rtc_base/thread_annotations.h"
29#include "video/quality_threshold.h"
30#include "video/report_block_stats.h"
31#include "video/stats_counter.h"
32#include "video/video_stream_decoder.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000033
34namespace webrtc {
35
36class Clock;
sprang@webrtc.org09315702014-02-07 12:06:29 +000037class ViECodec;
38class ViEDecoderObserver;
asapersson86b01602015-10-20 23:55:26 -070039struct CodecSpecificInfo;
sprang@webrtc.org09315702014-02-07 12:06:29 +000040
pbosf42376c2015-08-28 07:35:32 -070041class ReceiveStatisticsProxy : public VCMReceiveStatisticsCallback,
sprang@webrtc.org09315702014-02-07 12:06:29 +000042 public RtcpStatisticsCallback,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000043 public RtcpPacketTypeCounterObserver,
philipela45102f2017-02-22 05:30:39 -080044 public StreamDataCountersCallback,
45 public CallStatsObserver {
sprang@webrtc.org09315702014-02-07 12:06:29 +000046 public:
Tommi733b5472016-06-10 17:58:01 +020047 ReceiveStatisticsProxy(const VideoReceiveStream::Config* config,
sprang0ab8e812016-02-24 01:35:40 -080048 Clock* clock);
sprang@webrtc.org09315702014-02-07 12:06:29 +000049 virtual ~ReceiveStatisticsProxy();
50
51 VideoReceiveStream::Stats GetStats() const;
52
ilnik00d802b2017-04-11 10:34:31 -070053 void OnDecodedFrame(rtc::Optional<uint8_t> qp, VideoContentType content_type);
asaperssonde9e5ff2016-11-02 07:14:03 -070054 void OnSyncOffsetUpdated(int64_t sync_offset_ms, double estimated_freq_khz);
asapersson1490f7a2016-09-23 02:09:46 -070055 void OnRenderedFrame(const VideoFrame& frame);
pbosf42376c2015-08-28 07:35:32 -070056 void OnIncomingPayloadType(int payload_type);
Peter Boströmb7d9a972015-12-18 16:01:11 +010057 void OnDecoderImplementationName(const char* implementation_name);
pbosf42376c2015-08-28 07:35:32 -070058 void OnIncomingRate(unsigned int framerate, unsigned int bitrate_bps);
sprang@webrtc.org09315702014-02-07 12:06:29 +000059
asapersson86b01602015-10-20 23:55:26 -070060 void OnPreDecode(const EncodedImage& encoded_image,
61 const CodecSpecificInfo* codec_specific_info);
62
sprang3e86e7e2017-08-22 09:23:28 -070063 // Indicates video stream has been paused (no incoming packets).
64 void OnStreamInactive();
65
asaperssond89920b2015-07-22 06:52:00 -070066 // Overrides VCMReceiveStatisticsCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000067 void OnReceiveRatesUpdated(uint32_t bitRate, uint32_t frameRate) override;
68 void OnFrameCountsUpdated(const FrameCounts& frame_counts) override;
69 void OnDiscardedPacketsUpdated(int discarded_packets) override;
ilnik6d5b4d62017-08-30 03:32:14 -070070 void OnCompleteFrame(bool is_keyframe,
71 size_t size_bytes,
72 VideoContentType content_type) override;
philipela45102f2017-02-22 05:30:39 -080073 void OnFrameBufferTimingsUpdated(int decode_ms,
74 int max_decode_ms,
75 int current_delay_ms,
76 int target_delay_ms,
77 int jitter_buffer_ms,
78 int min_playout_delay_ms,
79 int render_delay_ms) override;
pbos@webrtc.org55707692014-12-19 15:45:03 +000080
ilnik2edc6842017-07-06 03:06:50 -070081 void OnTimingFrameInfoUpdated(const TimingFrameInfo& info) override;
82
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000083 // Overrides RtcpStatisticsCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000084 void StatisticsUpdated(const webrtc::RtcpStatistics& statistics,
85 uint32_t ssrc) override;
86 void CNameChanged(const char* cname, uint32_t ssrc) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000087
asaperssond89920b2015-07-22 06:52:00 -070088 // Overrides RtcpPacketTypeCounterObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000089 void RtcpPacketTypesCounterUpdated(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000090 uint32_t ssrc,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000091 const RtcpPacketTypeCounter& packet_counter) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000092 // Overrides StreamDataCountersCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000093 void DataCountersUpdated(const webrtc::StreamDataCounters& counters,
94 uint32_t ssrc) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000095
philipela45102f2017-02-22 05:30:39 -080096 // Implements CallStatsObserver.
97 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
98
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000099 private:
asaperssond89920b2015-07-22 06:52:00 -0700100 struct SampleCounter {
101 SampleCounter() : sum(0), num_samples(0) {}
102 void Add(int sample);
asapersson6966bd52017-01-03 00:44:06 -0800103 int Avg(int64_t min_required_samples) const;
ilnik6d5b4d62017-08-30 03:32:14 -0700104 int Max() const;
palmkvist349092b2016-12-13 02:45:57 -0800105 void Reset();
ilnik6d5b4d62017-08-30 03:32:14 -0700106 void Add(const SampleCounter& other);
asaperssond89920b2015-07-22 06:52:00 -0700107
108 private:
asapersson6966bd52017-01-03 00:44:06 -0800109 int64_t sum;
110 int64_t num_samples;
ilnik6d5b4d62017-08-30 03:32:14 -0700111 rtc::Optional<int> max;
asaperssond89920b2015-07-22 06:52:00 -0700112 };
ilnik6d5b4d62017-08-30 03:32:14 -0700113
asapersson86b01602015-10-20 23:55:26 -0700114 struct QpCounters {
115 SampleCounter vp8;
116 };
asaperssond89920b2015-07-22 06:52:00 -0700117
ilnik6d5b4d62017-08-30 03:32:14 -0700118 struct ContentSpecificStats {
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200119 ContentSpecificStats();
120
ilnik6d5b4d62017-08-30 03:32:14 -0700121 void Add(const ContentSpecificStats& other);
122
123 SampleCounter e2e_delay_counter;
124 SampleCounter interframe_delay_counter;
125 int64_t flow_duration_ms = 0;
126 int64_t total_media_bytes = 0;
127 SampleCounter received_width;
128 SampleCounter received_height;
129 SampleCounter qp_counter;
130 FrameCounts frame_counts;
Ilya Nikolaevskiyed23be92017-10-12 12:38:01 +0200131 rtc::HistogramPercentileCounter interframe_delay_percentiles;
ilnik6d5b4d62017-08-30 03:32:14 -0700132 };
133
danilchapa37de392017-09-09 04:17:22 -0700134 void UpdateHistograms() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
asaperssond89920b2015-07-22 06:52:00 -0700135
danilchapa37de392017-09-09 04:17:22 -0700136 void QualitySample() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
palmkvist349092b2016-12-13 02:45:57 -0800137
asapersson0255acb2017-03-28 02:44:58 -0700138 // Removes info about old frames and then updates the framerate.
danilchapa37de392017-09-09 04:17:22 -0700139 void UpdateFramerate(int64_t now_ms) const
140 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipela45102f2017-02-22 05:30:39 -0800141
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000142 Clock* const clock_;
Tommi733b5472016-06-10 17:58:01 +0200143 // Ownership of this object lies with the owner of the ReceiveStatisticsProxy
144 // instance. Lifetime is guaranteed to outlive |this|.
145 // TODO(tommi): In practice the config_ reference is only used for accessing
brandtrb5f2c3f2016-10-04 23:28:39 -0700146 // config_.rtp.ulpfec.ulpfec_payload_type. Instead of holding a pointer back,
Tommi733b5472016-06-10 17:58:01 +0200147 // we could just store the value of ulpfec_payload_type and change the
148 // ReceiveStatisticsProxy() ctor to accept a const& of Config (since we'll
149 // then no longer store a pointer to the object).
150 const VideoReceiveStream::Config& config_;
asapersson4374a092016-07-27 00:39:09 -0700151 const int64_t start_ms_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000152
pbos5ad935c2016-01-25 03:52:44 -0800153 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700154 int64_t last_sample_time_ RTC_GUARDED_BY(crit_);
155 QualityThreshold fps_threshold_ RTC_GUARDED_BY(crit_);
156 QualityThreshold qp_threshold_ RTC_GUARDED_BY(crit_);
157 QualityThreshold variance_threshold_ RTC_GUARDED_BY(crit_);
158 SampleCounter qp_sample_ RTC_GUARDED_BY(crit_);
159 int num_bad_states_ RTC_GUARDED_BY(crit_);
160 int num_certain_states_ RTC_GUARDED_BY(crit_);
161 mutable VideoReceiveStream::Stats stats_ RTC_GUARDED_BY(crit_);
162 RateStatistics decode_fps_estimator_ RTC_GUARDED_BY(crit_);
163 RateStatistics renders_fps_estimator_ RTC_GUARDED_BY(crit_);
164 rtc::RateTracker render_fps_tracker_ RTC_GUARDED_BY(crit_);
165 rtc::RateTracker render_pixel_tracker_ RTC_GUARDED_BY(crit_);
166 rtc::RateTracker total_byte_tracker_ RTC_GUARDED_BY(crit_);
167 SampleCounter sync_offset_counter_ RTC_GUARDED_BY(crit_);
168 SampleCounter decode_time_counter_ RTC_GUARDED_BY(crit_);
169 SampleCounter jitter_buffer_delay_counter_ RTC_GUARDED_BY(crit_);
170 SampleCounter target_delay_counter_ RTC_GUARDED_BY(crit_);
171 SampleCounter current_delay_counter_ RTC_GUARDED_BY(crit_);
172 SampleCounter delay_counter_ RTC_GUARDED_BY(crit_);
ilnika79cc282017-08-23 05:24:10 -0700173 mutable rtc::MovingMaxCounter<int> interframe_delay_max_moving_
danilchapa37de392017-09-09 04:17:22 -0700174 RTC_GUARDED_BY(crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700175 std::map<VideoContentType, ContentSpecificStats> content_specific_stats_
danilchapa37de392017-09-09 04:17:22 -0700176 RTC_GUARDED_BY(crit_);
177 MaxCounter freq_offset_counter_ RTC_GUARDED_BY(crit_);
178 int64_t first_report_block_time_ms_ RTC_GUARDED_BY(crit_);
179 ReportBlockStats report_block_stats_ RTC_GUARDED_BY(crit_);
asapersson86b01602015-10-20 23:55:26 -0700180 QpCounters qp_counters_; // Only accessed on the decoding thread.
danilchapa37de392017-09-09 04:17:22 -0700181 std::map<uint32_t, StreamDataCounters> rtx_stats_ RTC_GUARDED_BY(crit_);
182 int64_t avg_rtt_ms_ RTC_GUARDED_BY(crit_);
183 mutable std::map<int64_t, size_t> frame_window_ RTC_GUARDED_BY(&crit_);
184 VideoContentType last_content_type_ RTC_GUARDED_BY(&crit_);
185 rtc::Optional<int64_t> last_decoded_frame_time_ms_ RTC_GUARDED_BY(&crit_);
ilnik75204c52017-09-04 03:35:40 -0700186 // Mutable because calling Max() on MovingMaxCounter is not const. Yet it is
187 // called from const GetStats().
188 mutable rtc::MovingMaxCounter<TimingFrameInfo> timing_frame_info_counter_
danilchapa37de392017-09-09 04:17:22 -0700189 RTC_GUARDED_BY(&crit_);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000190};
191
sprang@webrtc.org09315702014-02-07 12:06:29 +0000192} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200193#endif // VIDEO_RECEIVE_STATISTICS_PROXY_H_