blob: 262be64d53f5187c796a45fa921a1cf4e6422b79 [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
11#ifndef WEBRTC_VIDEO_RECEIVE_STATISTICS_PROXY_H_
12#define WEBRTC_VIDEO_RECEIVE_STATISTICS_PROXY_H_
13
sprang0ab8e812016-02-24 01:35:40 -080014#include <map>
sprang@webrtc.org09315702014-02-07 12:06:29 +000015#include <string>
16
kwiberg84f6a3f2017-09-05 08:43:13 -070017#include "webrtc/api/optional.h"
aleloi440b6d92017-08-22 05:43:23 -070018#include "webrtc/call/video_receive_stream.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000019#include "webrtc/common_types.h"
pbosa96b60b2016-04-18 21:12:48 -070020#include "webrtc/common_video/include/frame_callback.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010021#include "webrtc/modules/video_coding/include/video_coding_defines.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020022#include "webrtc/rtc_base/criticalsection.h"
ilnika79cc282017-08-23 05:24:10 -070023#include "webrtc/rtc_base/moving_max_counter.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020024#include "webrtc/rtc_base/rate_statistics.h"
25#include "webrtc/rtc_base/ratetracker.h"
26#include "webrtc/rtc_base/thread_annotations.h"
palmkvist349092b2016-12-13 02:45:57 -080027#include "webrtc/video/quality_threshold.h"
Peter Boström7623ce42015-12-09 12:13:30 +010028#include "webrtc/video/report_block_stats.h"
asaperssonde9e5ff2016-11-02 07:14:03 -070029#include "webrtc/video/stats_counter.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070030#include "webrtc/video/video_stream_decoder.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000031
32namespace webrtc {
33
34class Clock;
sprang@webrtc.org09315702014-02-07 12:06:29 +000035class ViECodec;
36class ViEDecoderObserver;
asapersson86b01602015-10-20 23:55:26 -070037struct CodecSpecificInfo;
sprang@webrtc.org09315702014-02-07 12:06:29 +000038
pbosf42376c2015-08-28 07:35:32 -070039class ReceiveStatisticsProxy : public VCMReceiveStatisticsCallback,
sprang@webrtc.org09315702014-02-07 12:06:29 +000040 public RtcpStatisticsCallback,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000041 public RtcpPacketTypeCounterObserver,
philipela45102f2017-02-22 05:30:39 -080042 public StreamDataCountersCallback,
43 public CallStatsObserver {
sprang@webrtc.org09315702014-02-07 12:06:29 +000044 public:
Tommi733b5472016-06-10 17:58:01 +020045 ReceiveStatisticsProxy(const VideoReceiveStream::Config* config,
sprang0ab8e812016-02-24 01:35:40 -080046 Clock* clock);
sprang@webrtc.org09315702014-02-07 12:06:29 +000047 virtual ~ReceiveStatisticsProxy();
48
49 VideoReceiveStream::Stats GetStats() const;
50
ilnik00d802b2017-04-11 10:34:31 -070051 void OnDecodedFrame(rtc::Optional<uint8_t> qp, VideoContentType content_type);
asaperssonde9e5ff2016-11-02 07:14:03 -070052 void OnSyncOffsetUpdated(int64_t sync_offset_ms, double estimated_freq_khz);
asapersson1490f7a2016-09-23 02:09:46 -070053 void OnRenderedFrame(const VideoFrame& frame);
pbosf42376c2015-08-28 07:35:32 -070054 void OnIncomingPayloadType(int payload_type);
Peter Boströmb7d9a972015-12-18 16:01:11 +010055 void OnDecoderImplementationName(const char* implementation_name);
pbosf42376c2015-08-28 07:35:32 -070056 void OnIncomingRate(unsigned int framerate, unsigned int bitrate_bps);
sprang@webrtc.org09315702014-02-07 12:06:29 +000057
asapersson86b01602015-10-20 23:55:26 -070058 void OnPreDecode(const EncodedImage& encoded_image,
59 const CodecSpecificInfo* codec_specific_info);
60
sprang3e86e7e2017-08-22 09:23:28 -070061 // Indicates video stream has been paused (no incoming packets).
62 void OnStreamInactive();
63
asaperssond89920b2015-07-22 06:52:00 -070064 // Overrides VCMReceiveStatisticsCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000065 void OnReceiveRatesUpdated(uint32_t bitRate, uint32_t frameRate) override;
66 void OnFrameCountsUpdated(const FrameCounts& frame_counts) override;
67 void OnDiscardedPacketsUpdated(int discarded_packets) override;
ilnik6d5b4d62017-08-30 03:32:14 -070068 void OnCompleteFrame(bool is_keyframe,
69 size_t size_bytes,
70 VideoContentType content_type) override;
philipela45102f2017-02-22 05:30:39 -080071 void OnFrameBufferTimingsUpdated(int decode_ms,
72 int max_decode_ms,
73 int current_delay_ms,
74 int target_delay_ms,
75 int jitter_buffer_ms,
76 int min_playout_delay_ms,
77 int render_delay_ms) override;
pbos@webrtc.org55707692014-12-19 15:45:03 +000078
ilnik2edc6842017-07-06 03:06:50 -070079 void OnTimingFrameInfoUpdated(const TimingFrameInfo& info) override;
80
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000081 // Overrides RtcpStatisticsCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000082 void StatisticsUpdated(const webrtc::RtcpStatistics& statistics,
83 uint32_t ssrc) override;
84 void CNameChanged(const char* cname, uint32_t ssrc) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000085
asaperssond89920b2015-07-22 06:52:00 -070086 // Overrides RtcpPacketTypeCounterObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000087 void RtcpPacketTypesCounterUpdated(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000088 uint32_t ssrc,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000089 const RtcpPacketTypeCounter& packet_counter) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000090 // Overrides StreamDataCountersCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000091 void DataCountersUpdated(const webrtc::StreamDataCounters& counters,
92 uint32_t ssrc) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000093
philipela45102f2017-02-22 05:30:39 -080094 // Implements CallStatsObserver.
95 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
96
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000097 private:
asaperssond89920b2015-07-22 06:52:00 -070098 struct SampleCounter {
99 SampleCounter() : sum(0), num_samples(0) {}
100 void Add(int sample);
asapersson6966bd52017-01-03 00:44:06 -0800101 int Avg(int64_t min_required_samples) const;
ilnik6d5b4d62017-08-30 03:32:14 -0700102 int Max() const;
palmkvist349092b2016-12-13 02:45:57 -0800103 void Reset();
ilnik6d5b4d62017-08-30 03:32:14 -0700104 void Add(const SampleCounter& other);
asaperssond89920b2015-07-22 06:52:00 -0700105
106 private:
asapersson6966bd52017-01-03 00:44:06 -0800107 int64_t sum;
108 int64_t num_samples;
ilnik6d5b4d62017-08-30 03:32:14 -0700109 rtc::Optional<int> max;
asaperssond89920b2015-07-22 06:52:00 -0700110 };
ilnik6d5b4d62017-08-30 03:32:14 -0700111
asapersson86b01602015-10-20 23:55:26 -0700112 struct QpCounters {
113 SampleCounter vp8;
114 };
asaperssond89920b2015-07-22 06:52:00 -0700115
ilnik6d5b4d62017-08-30 03:32:14 -0700116 struct ContentSpecificStats {
117 void Add(const ContentSpecificStats& other);
118
119 SampleCounter e2e_delay_counter;
120 SampleCounter interframe_delay_counter;
121 int64_t flow_duration_ms = 0;
122 int64_t total_media_bytes = 0;
123 SampleCounter received_width;
124 SampleCounter received_height;
125 SampleCounter qp_counter;
126 FrameCounts frame_counts;
127 };
128
danilchapa37de392017-09-09 04:17:22 -0700129 void UpdateHistograms() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
asaperssond89920b2015-07-22 06:52:00 -0700130
danilchapa37de392017-09-09 04:17:22 -0700131 void QualitySample() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
palmkvist349092b2016-12-13 02:45:57 -0800132
asapersson0255acb2017-03-28 02:44:58 -0700133 // Removes info about old frames and then updates the framerate.
danilchapa37de392017-09-09 04:17:22 -0700134 void UpdateFramerate(int64_t now_ms) const
135 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipela45102f2017-02-22 05:30:39 -0800136
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000137 Clock* const clock_;
Tommi733b5472016-06-10 17:58:01 +0200138 // Ownership of this object lies with the owner of the ReceiveStatisticsProxy
139 // instance. Lifetime is guaranteed to outlive |this|.
140 // TODO(tommi): In practice the config_ reference is only used for accessing
brandtrb5f2c3f2016-10-04 23:28:39 -0700141 // config_.rtp.ulpfec.ulpfec_payload_type. Instead of holding a pointer back,
Tommi733b5472016-06-10 17:58:01 +0200142 // we could just store the value of ulpfec_payload_type and change the
143 // ReceiveStatisticsProxy() ctor to accept a const& of Config (since we'll
144 // then no longer store a pointer to the object).
145 const VideoReceiveStream::Config& config_;
asapersson4374a092016-07-27 00:39:09 -0700146 const int64_t start_ms_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000147
pbos5ad935c2016-01-25 03:52:44 -0800148 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700149 int64_t last_sample_time_ RTC_GUARDED_BY(crit_);
150 QualityThreshold fps_threshold_ RTC_GUARDED_BY(crit_);
151 QualityThreshold qp_threshold_ RTC_GUARDED_BY(crit_);
152 QualityThreshold variance_threshold_ RTC_GUARDED_BY(crit_);
153 SampleCounter qp_sample_ RTC_GUARDED_BY(crit_);
154 int num_bad_states_ RTC_GUARDED_BY(crit_);
155 int num_certain_states_ RTC_GUARDED_BY(crit_);
156 mutable VideoReceiveStream::Stats stats_ RTC_GUARDED_BY(crit_);
157 RateStatistics decode_fps_estimator_ RTC_GUARDED_BY(crit_);
158 RateStatistics renders_fps_estimator_ RTC_GUARDED_BY(crit_);
159 rtc::RateTracker render_fps_tracker_ RTC_GUARDED_BY(crit_);
160 rtc::RateTracker render_pixel_tracker_ RTC_GUARDED_BY(crit_);
161 rtc::RateTracker total_byte_tracker_ RTC_GUARDED_BY(crit_);
162 SampleCounter sync_offset_counter_ RTC_GUARDED_BY(crit_);
163 SampleCounter decode_time_counter_ RTC_GUARDED_BY(crit_);
164 SampleCounter jitter_buffer_delay_counter_ RTC_GUARDED_BY(crit_);
165 SampleCounter target_delay_counter_ RTC_GUARDED_BY(crit_);
166 SampleCounter current_delay_counter_ RTC_GUARDED_BY(crit_);
167 SampleCounter delay_counter_ RTC_GUARDED_BY(crit_);
ilnika79cc282017-08-23 05:24:10 -0700168 mutable rtc::MovingMaxCounter<int> interframe_delay_max_moving_
danilchapa37de392017-09-09 04:17:22 -0700169 RTC_GUARDED_BY(crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700170 std::map<VideoContentType, ContentSpecificStats> content_specific_stats_
danilchapa37de392017-09-09 04:17:22 -0700171 RTC_GUARDED_BY(crit_);
172 MaxCounter freq_offset_counter_ RTC_GUARDED_BY(crit_);
173 int64_t first_report_block_time_ms_ RTC_GUARDED_BY(crit_);
174 ReportBlockStats report_block_stats_ RTC_GUARDED_BY(crit_);
asapersson86b01602015-10-20 23:55:26 -0700175 QpCounters qp_counters_; // Only accessed on the decoding thread.
danilchapa37de392017-09-09 04:17:22 -0700176 std::map<uint32_t, StreamDataCounters> rtx_stats_ RTC_GUARDED_BY(crit_);
177 int64_t avg_rtt_ms_ RTC_GUARDED_BY(crit_);
178 mutable std::map<int64_t, size_t> frame_window_ RTC_GUARDED_BY(&crit_);
179 VideoContentType last_content_type_ RTC_GUARDED_BY(&crit_);
180 rtc::Optional<int64_t> last_decoded_frame_time_ms_ RTC_GUARDED_BY(&crit_);
ilnik75204c52017-09-04 03:35:40 -0700181 // Mutable because calling Max() on MovingMaxCounter is not const. Yet it is
182 // called from const GetStats().
183 mutable rtc::MovingMaxCounter<TimingFrameInfo> timing_frame_info_counter_
danilchapa37de392017-09-09 04:17:22 -0700184 RTC_GUARDED_BY(&crit_);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000185};
186
sprang@webrtc.org09315702014-02-07 12:06:29 +0000187} // namespace webrtc
188#endif // WEBRTC_VIDEO_RECEIVE_STATISTICS_PROXY_H_