blob: 02043d69442a8ac2d4b2f53a00035fda681fbe2e [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"
Niels Möller834a5542019-09-23 10:31:16 +020021#include "modules/include/module_common_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/include/video_coding_defines.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/critical_section.h"
Karl Wiberg65c39222017-11-22 12:25:14 +010024#include "rtc_base/numerics/histogram_percentile_counter.h"
25#include "rtc_base/numerics/moving_max_counter.h"
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020026#include "rtc_base/numerics/sample_counter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/rate_statistics.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/rate_tracker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/thread_annotations.h"
Tommi132e28e2018-02-24 17:57:33 +010030#include "rtc_base/thread_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "video/quality_threshold.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "video/stats_counter.h"
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +020033#include "video/video_quality_observer.h"
sprang@webrtc.org09315702014-02-07 12:06:29 +000034
35namespace webrtc {
36
37class Clock;
asapersson86b01602015-10-20 23:55:26 -070038struct CodecSpecificInfo;
sprang@webrtc.org09315702014-02-07 12:06:29 +000039
pbosf42376c2015-08-28 07:35:32 -070040class ReceiveStatisticsProxy : public VCMReceiveStatisticsCallback,
Niels Möller4d7c4052019-08-05 12:45:19 +020041 public RtcpCnameCallback,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000042 public RtcpPacketTypeCounterObserver,
philipela45102f2017-02-22 05:30:39 -080043 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);
Niels Möller9a9f18a2019-08-02 13:52:37 +020047 ~ReceiveStatisticsProxy() = default;
sprang@webrtc.org09315702014-02-07 12:06:29 +000048
49 VideoReceiveStream::Stats GetStats() const;
50
Sergey Silkin278f8252019-01-09 14:37:40 +010051 void OnDecodedFrame(const VideoFrame& frame,
52 absl::optional<uint8_t> qp,
Johannes Kronbfd343b2019-07-01 10:07:50 +020053 int32_t decode_time_ms,
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +020054 VideoContentType content_type);
Åsa Perssonfcf79cc2019-10-22 15:23:44 +020055 void OnSyncOffsetUpdated(int64_t video_playout_ntp_ms,
56 int64_t sync_offset_ms,
57 double estimated_freq_khz);
asapersson1490f7a2016-09-23 02:09:46 -070058 void OnRenderedFrame(const VideoFrame& frame);
pbosf42376c2015-08-28 07:35:32 -070059 void OnIncomingPayloadType(int payload_type);
Peter Boströmb7d9a972015-12-18 16:01:11 +010060 void OnDecoderImplementationName(const char* implementation_name);
sprang@webrtc.org09315702014-02-07 12:06:29 +000061
Niels Möller147013a2018-10-01 15:56:33 +020062 void OnPreDecode(VideoCodecType codec_type, int qp);
asapersson86b01602015-10-20 23:55:26 -070063
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010064 void OnUniqueFramesCounted(int num_unique_frames);
65
sprang3e86e7e2017-08-22 09:23:28 -070066 // Indicates video stream has been paused (no incoming packets).
67 void OnStreamInactive();
68
asaperssond89920b2015-07-22 06:52:00 -070069 // Overrides VCMReceiveStatisticsCallback.
ilnik6d5b4d62017-08-30 03:32:14 -070070 void OnCompleteFrame(bool is_keyframe,
71 size_t size_bytes,
72 VideoContentType content_type) override;
Johannes Kron0c141c52019-08-26 15:04:43 +020073 void OnDroppedFrames(uint32_t frames_dropped) override;
Johannes Kronbfd343b2019-07-01 10:07:50 +020074 void OnFrameBufferTimingsUpdated(int max_decode_ms,
philipela45102f2017-02-22 05:30:39 -080075 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
Niels Möller4d7c4052019-08-05 12:45:19 +020083 // Overrides RtcpCnameCallback.
84 void OnCname(uint32_t ssrc, absl::string_view cname) 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
philipela45102f2017-02-22 05:30:39 -080091 // Implements CallStatsObserver.
92 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
93
Tommi132e28e2018-02-24 17:57:33 +010094 // Notification methods that are used to check our internal state and validate
95 // threading assumptions. These are called by VideoReceiveStream.
96 void DecoderThreadStarting();
97 void DecoderThreadStopped();
98
Niels Möller9a9f18a2019-08-02 13:52:37 +020099 // Produce histograms. Must be called after DecoderThreadStopped(), typically
100 // at the end of the call.
Niels Möllerd7819652019-08-13 14:43:02 +0200101 void UpdateHistograms(absl::optional<int> fraction_lost,
102 const StreamDataCounters& rtp_stats,
103 const StreamDataCounters* rtx_stats);
Niels Möller9a9f18a2019-08-02 13:52:37 +0200104
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000105 private:
asapersson86b01602015-10-20 23:55:26 -0700106 struct QpCounters {
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200107 rtc::SampleCounter vp8;
asapersson86b01602015-10-20 23:55:26 -0700108 };
asaperssond89920b2015-07-22 06:52:00 -0700109
ilnik6d5b4d62017-08-30 03:32:14 -0700110 struct ContentSpecificStats {
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200111 ContentSpecificStats();
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200112 ~ContentSpecificStats();
Ilya Nikolaevskiydaa4f7a2017-10-06 12:29:47 +0200113
ilnik6d5b4d62017-08-30 03:32:14 -0700114 void Add(const ContentSpecificStats& other);
115
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200116 rtc::SampleCounter e2e_delay_counter;
117 rtc::SampleCounter interframe_delay_counter;
ilnik6d5b4d62017-08-30 03:32:14 -0700118 int64_t flow_duration_ms = 0;
119 int64_t total_media_bytes = 0;
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200120 rtc::SampleCounter received_width;
121 rtc::SampleCounter received_height;
122 rtc::SampleCounter qp_counter;
ilnik6d5b4d62017-08-30 03:32:14 -0700123 FrameCounts frame_counts;
Ilya Nikolaevskiyed23be92017-10-12 12:38:01 +0200124 rtc::HistogramPercentileCounter interframe_delay_percentiles;
ilnik6d5b4d62017-08-30 03:32:14 -0700125 };
126
danilchapa37de392017-09-09 04:17:22 -0700127 void QualitySample() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
palmkvist349092b2016-12-13 02:45:57 -0800128
asapersson0255acb2017-03-28 02:44:58 -0700129 // Removes info about old frames and then updates the framerate.
danilchapa37de392017-09-09 04:17:22 -0700130 void UpdateFramerate(int64_t now_ms) const
131 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipela45102f2017-02-22 05:30:39 -0800132
Johannes Krone76b3ab2019-10-22 13:22:26 +0200133 void UpdateDecodeTimeHistograms(int width,
134 int height,
135 int decode_time_ms) const
136 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
137
Åsa Perssonfcf79cc2019-10-22 15:23:44 +0200138 absl::optional<int64_t> GetCurrentEstimatedPlayoutNtpTimestampMs(
139 int64_t now_ms) const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
140
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_;
Johannes Krone76b3ab2019-10-22 13:22:26 +0200151 const bool enable_decode_time_histograms_;
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_);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200158 rtc::SampleCounter qp_sample_ RTC_GUARDED_BY(crit_);
danilchapa37de392017-09-09 04:17:22 -0700159 int num_bad_states_ RTC_GUARDED_BY(crit_);
160 int num_certain_states_ RTC_GUARDED_BY(crit_);
Niels Möllerd7819652019-08-13 14:43:02 +0200161 // Note: The |stats_.rtp_stats| member is not used or populated by this class.
danilchapa37de392017-09-09 04:17:22 -0700162 mutable VideoReceiveStream::Stats stats_ RTC_GUARDED_BY(crit_);
163 RateStatistics decode_fps_estimator_ RTC_GUARDED_BY(crit_);
164 RateStatistics renders_fps_estimator_ RTC_GUARDED_BY(crit_);
165 rtc::RateTracker render_fps_tracker_ RTC_GUARDED_BY(crit_);
166 rtc::RateTracker render_pixel_tracker_ RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +0200167 rtc::SampleCounter sync_offset_counter_ RTC_GUARDED_BY(crit_);
168 rtc::SampleCounter decode_time_counter_ RTC_GUARDED_BY(crit_);
169 rtc::SampleCounter jitter_buffer_delay_counter_ RTC_GUARDED_BY(crit_);
170 rtc::SampleCounter target_delay_counter_ RTC_GUARDED_BY(crit_);
171 rtc::SampleCounter current_delay_counter_ RTC_GUARDED_BY(crit_);
172 rtc::SampleCounter delay_counter_ RTC_GUARDED_BY(crit_);
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200173 std::unique_ptr<VideoQualityObserver> video_quality_observer_
174 RTC_GUARDED_BY(crit_);
ilnika79cc282017-08-23 05:24:10 -0700175 mutable rtc::MovingMaxCounter<int> interframe_delay_max_moving_
danilchapa37de392017-09-09 04:17:22 -0700176 RTC_GUARDED_BY(crit_);
ilnik6d5b4d62017-08-30 03:32:14 -0700177 std::map<VideoContentType, ContentSpecificStats> content_specific_stats_
danilchapa37de392017-09-09 04:17:22 -0700178 RTC_GUARDED_BY(crit_);
179 MaxCounter freq_offset_counter_ RTC_GUARDED_BY(crit_);
Tommi132e28e2018-02-24 17:57:33 +0100180 QpCounters qp_counters_ RTC_GUARDED_BY(decode_thread_);
danilchapa37de392017-09-09 04:17:22 -0700181 int64_t avg_rtt_ms_ RTC_GUARDED_BY(crit_);
182 mutable std::map<int64_t, size_t> frame_window_ RTC_GUARDED_BY(&crit_);
183 VideoContentType last_content_type_ RTC_GUARDED_BY(&crit_);
Ilya Nikolaevskiy94150ee2018-05-23 11:53:19 +0200184 VideoCodecType last_codec_type_ RTC_GUARDED_BY(&crit_);
Benjamin Wright514f0842018-12-10 09:55:17 -0800185 absl::optional<int64_t> first_frame_received_time_ms_ RTC_GUARDED_BY(&crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200186 absl::optional<int64_t> first_decoded_frame_time_ms_ RTC_GUARDED_BY(&crit_);
187 absl::optional<int64_t> last_decoded_frame_time_ms_ RTC_GUARDED_BY(&crit_);
Åsa Persson81327d52018-06-05 13:34:33 +0200188 size_t num_delayed_frames_rendered_ RTC_GUARDED_BY(&crit_);
189 int64_t sum_missed_render_deadline_ms_ RTC_GUARDED_BY(&crit_);
ilnik75204c52017-09-04 03:35:40 -0700190 // Mutable because calling Max() on MovingMaxCounter is not const. Yet it is
191 // called from const GetStats().
192 mutable rtc::MovingMaxCounter<TimingFrameInfo> timing_frame_info_counter_
danilchapa37de392017-09-09 04:17:22 -0700193 RTC_GUARDED_BY(&crit_);
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200194 absl::optional<int> num_unique_frames_ RTC_GUARDED_BY(crit_);
Åsa Perssonfcf79cc2019-10-22 15:23:44 +0200195 absl::optional<int64_t> last_estimated_playout_ntp_timestamp_ms_
196 RTC_GUARDED_BY(&crit_);
197 absl::optional<int64_t> last_estimated_playout_time_ms_
198 RTC_GUARDED_BY(&crit_);
Tommi132e28e2018-02-24 17:57:33 +0100199 rtc::ThreadChecker decode_thread_;
200 rtc::ThreadChecker network_thread_;
201 rtc::ThreadChecker main_thread_;
sprang@webrtc.org09315702014-02-07 12:06:29 +0000202};
203
sprang@webrtc.org09315702014-02-07 12:06:29 +0000204} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200205#endif // VIDEO_RECEIVE_STATISTICS_PROXY_H_