blob: 2e93d70f153770d775a3fd08112b7570ca7cb518 [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
aleloi440b6d92017-08-22 05:43:23 -070017#include "webrtc/call/video_receive_stream.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000018#include "webrtc/common_types.h"
pbosa96b60b2016-04-18 21:12:48 -070019#include "webrtc/common_video/include/frame_callback.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010020#include "webrtc/modules/video_coding/include/video_coding_defines.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020021#include "webrtc/rtc_base/criticalsection.h"
ilnika79cc282017-08-23 05:24:10 -070022#include "webrtc/rtc_base/moving_max_counter.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020023#include "webrtc/rtc_base/rate_statistics.h"
24#include "webrtc/rtc_base/ratetracker.h"
25#include "webrtc/rtc_base/thread_annotations.h"
palmkvist349092b2016-12-13 02:45:57 -080026#include "webrtc/video/quality_threshold.h"
Peter Boström7623ce42015-12-09 12:13:30 +010027#include "webrtc/video/report_block_stats.h"
asaperssonde9e5ff2016-11-02 07:14:03 -070028#include "webrtc/video/stats_counter.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070029#include "webrtc/video/video_stream_decoder.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000030
31namespace webrtc {
32
33class Clock;
sprang@webrtc.org09315702014-02-07 12:06:29 +000034class ViECodec;
35class ViEDecoderObserver;
asapersson86b01602015-10-20 23:55:26 -070036struct CodecSpecificInfo;
sprang@webrtc.org09315702014-02-07 12:06:29 +000037
pbosf42376c2015-08-28 07:35:32 -070038class ReceiveStatisticsProxy : public VCMReceiveStatisticsCallback,
sprang@webrtc.org09315702014-02-07 12:06:29 +000039 public RtcpStatisticsCallback,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000040 public RtcpPacketTypeCounterObserver,
philipela45102f2017-02-22 05:30:39 -080041 public StreamDataCountersCallback,
42 public CallStatsObserver {
sprang@webrtc.org09315702014-02-07 12:06:29 +000043 public:
Tommi733b5472016-06-10 17:58:01 +020044 ReceiveStatisticsProxy(const VideoReceiveStream::Config* config,
sprang0ab8e812016-02-24 01:35:40 -080045 Clock* clock);
sprang@webrtc.org09315702014-02-07 12:06:29 +000046 virtual ~ReceiveStatisticsProxy();
47
48 VideoReceiveStream::Stats GetStats() const;
49
ilnik2edc6842017-07-06 03:06:50 -070050 rtc::Optional<TimingFrameInfo> GetAndResetTimingFrameInfo();
51
ilnik00d802b2017-04-11 10:34:31 -070052 void OnDecodedFrame(rtc::Optional<uint8_t> qp, VideoContentType content_type);
asaperssonde9e5ff2016-11-02 07:14:03 -070053 void OnSyncOffsetUpdated(int64_t sync_offset_ms, double estimated_freq_khz);
asapersson1490f7a2016-09-23 02:09:46 -070054 void OnRenderedFrame(const VideoFrame& frame);
pbosf42376c2015-08-28 07:35:32 -070055 void OnIncomingPayloadType(int payload_type);
Peter Boströmb7d9a972015-12-18 16:01:11 +010056 void OnDecoderImplementationName(const char* implementation_name);
pbosf42376c2015-08-28 07:35:32 -070057 void OnIncomingRate(unsigned int framerate, unsigned int bitrate_bps);
sprang@webrtc.org09315702014-02-07 12:06:29 +000058
asapersson86b01602015-10-20 23:55:26 -070059 void OnPreDecode(const EncodedImage& encoded_image,
60 const CodecSpecificInfo* codec_specific_info);
61
sprang3e86e7e2017-08-22 09:23:28 -070062 // Indicates video stream has been paused (no incoming packets).
63 void OnStreamInactive();
64
asaperssond89920b2015-07-22 06:52:00 -070065 // Overrides VCMReceiveStatisticsCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000066 void OnReceiveRatesUpdated(uint32_t bitRate, uint32_t frameRate) override;
67 void OnFrameCountsUpdated(const FrameCounts& frame_counts) override;
68 void OnDiscardedPacketsUpdated(int discarded_packets) override;
philipela45102f2017-02-22 05:30:39 -080069 void OnCompleteFrame(bool is_keyframe, size_t size_bytes) override;
70 void OnFrameBufferTimingsUpdated(int decode_ms,
71 int max_decode_ms,
72 int current_delay_ms,
73 int target_delay_ms,
74 int jitter_buffer_ms,
75 int min_playout_delay_ms,
76 int render_delay_ms) override;
pbos@webrtc.org55707692014-12-19 15:45:03 +000077
ilnik2edc6842017-07-06 03:06:50 -070078 void OnTimingFrameInfoUpdated(const TimingFrameInfo& info) override;
79
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000080 // Overrides RtcpStatisticsCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000081 void StatisticsUpdated(const webrtc::RtcpStatistics& statistics,
82 uint32_t ssrc) override;
83 void CNameChanged(const char* cname, uint32_t ssrc) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000084
asaperssond89920b2015-07-22 06:52:00 -070085 // Overrides RtcpPacketTypeCounterObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000086 void RtcpPacketTypesCounterUpdated(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000087 uint32_t ssrc,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000088 const RtcpPacketTypeCounter& packet_counter) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000089 // Overrides StreamDataCountersCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000090 void DataCountersUpdated(const webrtc::StreamDataCounters& counters,
91 uint32_t ssrc) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000092
philipela45102f2017-02-22 05:30:39 -080093 // Implements CallStatsObserver.
94 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
95
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000096 private:
asaperssond89920b2015-07-22 06:52:00 -070097 struct SampleCounter {
98 SampleCounter() : sum(0), num_samples(0) {}
99 void Add(int sample);
asapersson6966bd52017-01-03 00:44:06 -0800100 int Avg(int64_t min_required_samples) const;
palmkvist349092b2016-12-13 02:45:57 -0800101 void Reset();
asaperssond89920b2015-07-22 06:52:00 -0700102
103 private:
asapersson6966bd52017-01-03 00:44:06 -0800104 int64_t sum;
105 int64_t num_samples;
asaperssond89920b2015-07-22 06:52:00 -0700106 };
asapersson86b01602015-10-20 23:55:26 -0700107 struct QpCounters {
108 SampleCounter vp8;
109 };
asaperssond89920b2015-07-22 06:52:00 -0700110
111 void UpdateHistograms() EXCLUSIVE_LOCKS_REQUIRED(crit_);
112
palmkvist349092b2016-12-13 02:45:57 -0800113 void QualitySample() EXCLUSIVE_LOCKS_REQUIRED(crit_);
114
asapersson0255acb2017-03-28 02:44:58 -0700115 // Removes info about old frames and then updates the framerate.
116 void UpdateFramerate(int64_t now_ms) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipela45102f2017-02-22 05:30:39 -0800117
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000118 Clock* const clock_;
Tommi733b5472016-06-10 17:58:01 +0200119 // Ownership of this object lies with the owner of the ReceiveStatisticsProxy
120 // instance. Lifetime is guaranteed to outlive |this|.
121 // TODO(tommi): In practice the config_ reference is only used for accessing
brandtrb5f2c3f2016-10-04 23:28:39 -0700122 // config_.rtp.ulpfec.ulpfec_payload_type. Instead of holding a pointer back,
Tommi733b5472016-06-10 17:58:01 +0200123 // we could just store the value of ulpfec_payload_type and change the
124 // ReceiveStatisticsProxy() ctor to accept a const& of Config (since we'll
125 // then no longer store a pointer to the object).
126 const VideoReceiveStream::Config& config_;
asapersson4374a092016-07-27 00:39:09 -0700127 const int64_t start_ms_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000128
pbos5ad935c2016-01-25 03:52:44 -0800129 rtc::CriticalSection crit_;
palmkvist349092b2016-12-13 02:45:57 -0800130 int64_t last_sample_time_ GUARDED_BY(crit_);
131 QualityThreshold fps_threshold_ GUARDED_BY(crit_);
132 QualityThreshold qp_threshold_ GUARDED_BY(crit_);
133 QualityThreshold variance_threshold_ GUARDED_BY(crit_);
134 SampleCounter qp_sample_ GUARDED_BY(crit_);
palmkvista40672a2017-01-13 05:58:34 -0800135 int num_bad_states_ GUARDED_BY(crit_);
136 int num_certain_states_ GUARDED_BY(crit_);
philipela45102f2017-02-22 05:30:39 -0800137 mutable VideoReceiveStream::Stats stats_ GUARDED_BY(crit_);
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000138 RateStatistics decode_fps_estimator_ GUARDED_BY(crit_);
139 RateStatistics renders_fps_estimator_ GUARDED_BY(crit_);
Tim Psiaki63046262015-09-14 10:38:08 -0700140 rtc::RateTracker render_fps_tracker_ GUARDED_BY(crit_);
asaperssonf839dcc2015-10-08 00:41:59 -0700141 rtc::RateTracker render_pixel_tracker_ GUARDED_BY(crit_);
asapersson0255acb2017-03-28 02:44:58 -0700142 rtc::RateTracker total_byte_tracker_ GUARDED_BY(crit_);
asaperssond89920b2015-07-22 06:52:00 -0700143 SampleCounter render_width_counter_ GUARDED_BY(crit_);
144 SampleCounter render_height_counter_ GUARDED_BY(crit_);
asaperssonf8cdd182016-03-15 01:00:47 -0700145 SampleCounter sync_offset_counter_ GUARDED_BY(crit_);
asapersson6718e972015-07-24 00:20:58 -0700146 SampleCounter decode_time_counter_ GUARDED_BY(crit_);
asapersson8688a4e2016-04-27 23:42:35 -0700147 SampleCounter jitter_buffer_delay_counter_ GUARDED_BY(crit_);
148 SampleCounter target_delay_counter_ GUARDED_BY(crit_);
149 SampleCounter current_delay_counter_ GUARDED_BY(crit_);
asapersson13c433c2015-10-06 04:08:15 -0700150 SampleCounter delay_counter_ GUARDED_BY(crit_);
ilnik00d802b2017-04-11 10:34:31 -0700151 SampleCounter e2e_delay_counter_video_ GUARDED_BY(crit_);
152 SampleCounter e2e_delay_counter_screenshare_ GUARDED_BY(crit_);
ilnik4257ab22017-07-03 01:15:58 -0700153 SampleCounter interframe_delay_counter_video_ GUARDED_BY(crit_);
154 SampleCounter interframe_delay_counter_screenshare_ GUARDED_BY(crit_);
ilnik00d802b2017-04-11 10:34:31 -0700155 int64_t e2e_delay_max_ms_video_ GUARDED_BY(crit_);
156 int64_t e2e_delay_max_ms_screenshare_ GUARDED_BY(crit_);
ilnik4257ab22017-07-03 01:15:58 -0700157 int64_t interframe_delay_max_ms_video_ GUARDED_BY(crit_);
158 int64_t interframe_delay_max_ms_screenshare_ GUARDED_BY(crit_);
ilnika79cc282017-08-23 05:24:10 -0700159 mutable rtc::MovingMaxCounter<int> interframe_delay_max_moving_
160 GUARDED_BY(crit_);
asaperssonde9e5ff2016-11-02 07:14:03 -0700161 MaxCounter freq_offset_counter_ GUARDED_BY(crit_);
asapersson0c43f772016-11-30 01:42:26 -0800162 int64_t first_report_block_time_ms_ GUARDED_BY(crit_);
Åsa Persson3c391cb2015-04-27 10:09:49 +0200163 ReportBlockStats report_block_stats_ GUARDED_BY(crit_);
asapersson86b01602015-10-20 23:55:26 -0700164 QpCounters qp_counters_; // Only accessed on the decoding thread.
sprang0ab8e812016-02-24 01:35:40 -0800165 std::map<uint32_t, StreamDataCounters> rtx_stats_ GUARDED_BY(crit_);
philipela45102f2017-02-22 05:30:39 -0800166 int64_t avg_rtt_ms_ GUARDED_BY(crit_);
167 mutable std::map<int64_t, size_t> frame_window_ GUARDED_BY(&crit_);
ilnik00d802b2017-04-11 10:34:31 -0700168 VideoContentType last_content_type_ GUARDED_BY(&crit_);
sprang3e86e7e2017-08-22 09:23:28 -0700169 rtc::Optional<int64_t> last_decoded_frame_time_ms_ GUARDED_BY(&crit_);
ilnik2edc6842017-07-06 03:06:50 -0700170 rtc::Optional<TimingFrameInfo> timing_frame_info_ GUARDED_BY(&crit_);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000171};
172
sprang@webrtc.org09315702014-02-07 12:06:29 +0000173} // namespace webrtc
174#endif // WEBRTC_VIDEO_RECEIVE_STATISTICS_PROXY_H_