blob: 1cfeca651327c61f32b36a2249ac109565cfe7fc [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
Henrik Kjellandera80c16a2017-07-01 16:48:15 +020017#include "webrtc/base/criticalsection.h"
18#include "webrtc/base/rate_statistics.h"
19#include "webrtc/base/ratetracker.h"
20#include "webrtc/base/thread_annotations.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000021#include "webrtc/common_types.h"
pbosa96b60b2016-04-18 21:12:48 -070022#include "webrtc/common_video/include/frame_callback.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010023#include "webrtc/modules/video_coding/include/video_coding_defines.h"
palmkvist349092b2016-12-13 02:45:57 -080024#include "webrtc/video/quality_threshold.h"
Peter Boström7623ce42015-12-09 12:13:30 +010025#include "webrtc/video/report_block_stats.h"
asaperssonde9e5ff2016-11-02 07:14:03 -070026#include "webrtc/video/stats_counter.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070027#include "webrtc/video/video_stream_decoder.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000028#include "webrtc/video_receive_stream.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000029
30namespace webrtc {
31
32class Clock;
sprang@webrtc.org09315702014-02-07 12:06:29 +000033class ViECodec;
34class ViEDecoderObserver;
asapersson86b01602015-10-20 23:55:26 -070035struct CodecSpecificInfo;
sprang@webrtc.org09315702014-02-07 12:06:29 +000036
pbosf42376c2015-08-28 07:35:32 -070037class ReceiveStatisticsProxy : public VCMReceiveStatisticsCallback,
sprang@webrtc.org09315702014-02-07 12:06:29 +000038 public RtcpStatisticsCallback,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000039 public RtcpPacketTypeCounterObserver,
philipela45102f2017-02-22 05:30:39 -080040 public StreamDataCountersCallback,
41 public CallStatsObserver {
sprang@webrtc.org09315702014-02-07 12:06:29 +000042 public:
Tommi733b5472016-06-10 17:58:01 +020043 ReceiveStatisticsProxy(const VideoReceiveStream::Config* config,
sprang0ab8e812016-02-24 01:35:40 -080044 Clock* clock);
sprang@webrtc.org09315702014-02-07 12:06:29 +000045 virtual ~ReceiveStatisticsProxy();
46
47 VideoReceiveStream::Stats GetStats() const;
48
ilnik2edc6842017-07-06 03:06:50 -070049 rtc::Optional<TimingFrameInfo> GetAndResetTimingFrameInfo();
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
asaperssond89920b2015-07-22 06:52:00 -070061 // Overrides VCMReceiveStatisticsCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000062 void OnReceiveRatesUpdated(uint32_t bitRate, uint32_t frameRate) override;
63 void OnFrameCountsUpdated(const FrameCounts& frame_counts) override;
64 void OnDiscardedPacketsUpdated(int discarded_packets) override;
philipela45102f2017-02-22 05:30:39 -080065 void OnCompleteFrame(bool is_keyframe, size_t size_bytes) override;
66 void OnFrameBufferTimingsUpdated(int decode_ms,
67 int max_decode_ms,
68 int current_delay_ms,
69 int target_delay_ms,
70 int jitter_buffer_ms,
71 int min_playout_delay_ms,
72 int render_delay_ms) override;
pbos@webrtc.org55707692014-12-19 15:45:03 +000073
ilnik2edc6842017-07-06 03:06:50 -070074 void OnTimingFrameInfoUpdated(const TimingFrameInfo& info) override;
75
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000076 // Overrides RtcpStatisticsCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000077 void StatisticsUpdated(const webrtc::RtcpStatistics& statistics,
78 uint32_t ssrc) override;
79 void CNameChanged(const char* cname, uint32_t ssrc) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000080
asaperssond89920b2015-07-22 06:52:00 -070081 // Overrides RtcpPacketTypeCounterObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000082 void RtcpPacketTypesCounterUpdated(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000083 uint32_t ssrc,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000084 const RtcpPacketTypeCounter& packet_counter) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000085 // Overrides StreamDataCountersCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000086 void DataCountersUpdated(const webrtc::StreamDataCounters& counters,
87 uint32_t ssrc) override;
sprang@webrtc.org09315702014-02-07 12:06:29 +000088
philipela45102f2017-02-22 05:30:39 -080089 // Implements CallStatsObserver.
90 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
91
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000092 private:
asaperssond89920b2015-07-22 06:52:00 -070093 struct SampleCounter {
94 SampleCounter() : sum(0), num_samples(0) {}
95 void Add(int sample);
asapersson6966bd52017-01-03 00:44:06 -080096 int Avg(int64_t min_required_samples) const;
palmkvist349092b2016-12-13 02:45:57 -080097 void Reset();
asaperssond89920b2015-07-22 06:52:00 -070098
99 private:
asapersson6966bd52017-01-03 00:44:06 -0800100 int64_t sum;
101 int64_t num_samples;
asaperssond89920b2015-07-22 06:52:00 -0700102 };
asapersson86b01602015-10-20 23:55:26 -0700103 struct QpCounters {
104 SampleCounter vp8;
105 };
asaperssond89920b2015-07-22 06:52:00 -0700106
107 void UpdateHistograms() EXCLUSIVE_LOCKS_REQUIRED(crit_);
108
palmkvist349092b2016-12-13 02:45:57 -0800109 void QualitySample() EXCLUSIVE_LOCKS_REQUIRED(crit_);
110
asapersson0255acb2017-03-28 02:44:58 -0700111 // Removes info about old frames and then updates the framerate.
112 void UpdateFramerate(int64_t now_ms) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipela45102f2017-02-22 05:30:39 -0800113
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000114 Clock* const clock_;
Tommi733b5472016-06-10 17:58:01 +0200115 // Ownership of this object lies with the owner of the ReceiveStatisticsProxy
116 // instance. Lifetime is guaranteed to outlive |this|.
117 // TODO(tommi): In practice the config_ reference is only used for accessing
brandtrb5f2c3f2016-10-04 23:28:39 -0700118 // config_.rtp.ulpfec.ulpfec_payload_type. Instead of holding a pointer back,
Tommi733b5472016-06-10 17:58:01 +0200119 // we could just store the value of ulpfec_payload_type and change the
120 // ReceiveStatisticsProxy() ctor to accept a const& of Config (since we'll
121 // then no longer store a pointer to the object).
122 const VideoReceiveStream::Config& config_;
asapersson4374a092016-07-27 00:39:09 -0700123 const int64_t start_ms_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000124
pbos5ad935c2016-01-25 03:52:44 -0800125 rtc::CriticalSection crit_;
palmkvist349092b2016-12-13 02:45:57 -0800126 int64_t last_sample_time_ GUARDED_BY(crit_);
127 QualityThreshold fps_threshold_ GUARDED_BY(crit_);
128 QualityThreshold qp_threshold_ GUARDED_BY(crit_);
129 QualityThreshold variance_threshold_ GUARDED_BY(crit_);
130 SampleCounter qp_sample_ GUARDED_BY(crit_);
palmkvista40672a2017-01-13 05:58:34 -0800131 int num_bad_states_ GUARDED_BY(crit_);
132 int num_certain_states_ GUARDED_BY(crit_);
philipela45102f2017-02-22 05:30:39 -0800133 mutable VideoReceiveStream::Stats stats_ GUARDED_BY(crit_);
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000134 RateStatistics decode_fps_estimator_ GUARDED_BY(crit_);
135 RateStatistics renders_fps_estimator_ GUARDED_BY(crit_);
Tim Psiaki63046262015-09-14 10:38:08 -0700136 rtc::RateTracker render_fps_tracker_ GUARDED_BY(crit_);
asaperssonf839dcc2015-10-08 00:41:59 -0700137 rtc::RateTracker render_pixel_tracker_ GUARDED_BY(crit_);
asapersson0255acb2017-03-28 02:44:58 -0700138 rtc::RateTracker total_byte_tracker_ GUARDED_BY(crit_);
asaperssond89920b2015-07-22 06:52:00 -0700139 SampleCounter render_width_counter_ GUARDED_BY(crit_);
140 SampleCounter render_height_counter_ GUARDED_BY(crit_);
asaperssonf8cdd182016-03-15 01:00:47 -0700141 SampleCounter sync_offset_counter_ GUARDED_BY(crit_);
asapersson6718e972015-07-24 00:20:58 -0700142 SampleCounter decode_time_counter_ GUARDED_BY(crit_);
asapersson8688a4e2016-04-27 23:42:35 -0700143 SampleCounter jitter_buffer_delay_counter_ GUARDED_BY(crit_);
144 SampleCounter target_delay_counter_ GUARDED_BY(crit_);
145 SampleCounter current_delay_counter_ GUARDED_BY(crit_);
asapersson13c433c2015-10-06 04:08:15 -0700146 SampleCounter delay_counter_ GUARDED_BY(crit_);
ilnik00d802b2017-04-11 10:34:31 -0700147 SampleCounter e2e_delay_counter_video_ GUARDED_BY(crit_);
148 SampleCounter e2e_delay_counter_screenshare_ GUARDED_BY(crit_);
ilnik4257ab22017-07-03 01:15:58 -0700149 SampleCounter interframe_delay_counter_video_ GUARDED_BY(crit_);
150 SampleCounter interframe_delay_counter_screenshare_ GUARDED_BY(crit_);
ilnik00d802b2017-04-11 10:34:31 -0700151 int64_t e2e_delay_max_ms_video_ GUARDED_BY(crit_);
152 int64_t e2e_delay_max_ms_screenshare_ GUARDED_BY(crit_);
ilnik4257ab22017-07-03 01:15:58 -0700153 int64_t interframe_delay_max_ms_video_ GUARDED_BY(crit_);
154 int64_t interframe_delay_max_ms_screenshare_ GUARDED_BY(crit_);
asaperssonde9e5ff2016-11-02 07:14:03 -0700155 MaxCounter freq_offset_counter_ GUARDED_BY(crit_);
asapersson0c43f772016-11-30 01:42:26 -0800156 int64_t first_report_block_time_ms_ GUARDED_BY(crit_);
Åsa Persson3c391cb2015-04-27 10:09:49 +0200157 ReportBlockStats report_block_stats_ GUARDED_BY(crit_);
asapersson86b01602015-10-20 23:55:26 -0700158 QpCounters qp_counters_; // Only accessed on the decoding thread.
sprang0ab8e812016-02-24 01:35:40 -0800159 std::map<uint32_t, StreamDataCounters> rtx_stats_ GUARDED_BY(crit_);
philipela45102f2017-02-22 05:30:39 -0800160 int64_t avg_rtt_ms_ GUARDED_BY(crit_);
161 mutable std::map<int64_t, size_t> frame_window_ GUARDED_BY(&crit_);
ilnik00d802b2017-04-11 10:34:31 -0700162 VideoContentType last_content_type_ GUARDED_BY(&crit_);
ilnik4257ab22017-07-03 01:15:58 -0700163 rtc::Optional<int64_t> last_decoded_frame_time_ms_;
ilnik2edc6842017-07-06 03:06:50 -0700164 rtc::Optional<TimingFrameInfo> timing_frame_info_ GUARDED_BY(&crit_);
sprang@webrtc.org09315702014-02-07 12:06:29 +0000165};
166
sprang@webrtc.org09315702014-02-07 12:06:29 +0000167} // namespace webrtc
168#endif // WEBRTC_VIDEO_RECEIVE_STATISTICS_PROXY_H_