blob: ec2fe4c50e93c2be31496e61030b9ade246df839 [file] [log] [blame]
sprang@webrtc.orgccd42842014-01-07 09:54:34 +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_SEND_STATISTICS_PROXY_H_
12#define WEBRTC_VIDEO_SEND_STATISTICS_PROXY_H_
13
mflodmand1590b22015-12-09 07:07:59 -080014#include <map>
kwiberg27f982b2016-03-01 11:52:33 -080015#include <memory>
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000016#include <string>
Pera48ddb72016-09-29 11:48:50 +020017#include <vector>
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000018
Peter Boströmf2f82832015-05-01 13:00:41 +020019#include "webrtc/base/criticalsection.h"
asapersson1aa420b2015-12-07 03:12:22 -080020#include "webrtc/base/exp_filter.h"
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000021#include "webrtc/base/ratetracker.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000022#include "webrtc/base/thread_annotations.h"
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000023#include "webrtc/common_types.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010024#include "webrtc/modules/video_coding/include/video_codec_interface.h"
25#include "webrtc/modules/video_coding/include/video_coding_defines.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010026#include "webrtc/system_wrappers/include/clock.h"
Peter Boström7623ce42015-12-09 12:13:30 +010027#include "webrtc/video/overuse_frame_detector.h"
sprange2d83d62016-02-19 09:03:26 -080028#include "webrtc/video/report_block_stats.h"
Peter Boström7623ce42015-12-09 12:13:30 +010029#include "webrtc/video/vie_encoder.h"
pbos@webrtc.org273a4142014-12-01 15:23:21 +000030#include "webrtc/video_send_stream.h"
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000031
32namespace webrtc {
33
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +000034class SendStatisticsProxy : public CpuOveruseMetricsObserver,
35 public RtcpStatisticsCallback,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000036 public RtcpPacketTypeCounterObserver,
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000037 public StreamDataCountersCallback,
38 public BitrateStatisticsObserver,
39 public FrameCountObserver,
stefan@webrtc.org168f23f2014-07-11 13:44:02 +000040 public SendSideDelayObserver {
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000041 public:
pbos@webrtc.org273a4142014-12-01 15:23:21 +000042 static const int kStatsTimeoutMs;
perkj803d97f2016-11-01 11:45:46 -070043 // Number of required samples to be collected before a metric is added
44 // to a rtc histogram.
45 static const int kMinRequiredMetricsSamples = 200;
pbos@webrtc.org273a4142014-12-01 15:23:21 +000046
sprangb4a1ae52015-12-03 08:10:08 -080047 SendStatisticsProxy(Clock* clock,
48 const VideoSendStream::Config& config,
49 VideoEncoderConfig::ContentType content_type);
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000050 virtual ~SendStatisticsProxy();
51
pbos@webrtc.org273a4142014-12-01 15:23:21 +000052 VideoSendStream::Stats GetStats();
53
54 virtual void OnSendEncodedImage(const EncodedImage& encoded_image,
kjellander02b3d272016-04-20 05:05:54 -070055 const CodecSpecificInfo* codec_info);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000056 // Used to update incoming frame rate.
asaperssond89920b2015-07-22 06:52:00 -070057 void OnIncomingFrame(int width, int height);
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000058
perkj803d97f2016-11-01 11:45:46 -070059 // Used to indicate that the current input frame resolution is restricted due
60 // to cpu usage.
61 void SetCpuRestrictedResolution(bool cpu_restricted);
62 // Used to update the number of times the input frame resolution has changed
63 // due to cpu adaptation.
64 void OnCpuRestrictedResolutionChanged(bool cpu_restricted_resolution);
65
perkj275afc52016-09-01 00:21:16 -070066 void OnEncoderStatsUpdate(uint32_t framerate, uint32_t bitrate);
Peter Boström7083e112015-09-22 16:28:51 +020067 void OnSuspendChange(bool is_suspended);
Peter Boström20f3f942015-05-15 11:33:39 +020068 void OnInactiveSsrc(uint32_t ssrc);
69
sprangb4a1ae52015-12-03 08:10:08 -080070 // Used to indicate change in content type, which may require a change in
Pera48ddb72016-09-29 11:48:50 +020071 // how stats are collected and set the configured preferred media bitrate.
72 void OnEncoderReconfigured(const VideoEncoderConfig& encoder_config,
73 uint32_t preferred_bitrate_bps);
sprangb4a1ae52015-12-03 08:10:08 -080074
perkjf5b2e512016-07-05 08:34:04 -070075 // Used to update the encoder target rate.
76 void OnSetEncoderTargetRate(uint32_t bitrate_bps);
Peter Boströme4499152016-02-05 11:13:28 +010077
78 // Implements CpuOveruseMetricsObserver.
79 void OnEncodedFrameTimeMeasured(int encode_time_ms,
80 const CpuOveruseMetrics& metrics) override;
81
Per69b332d2016-06-02 15:45:42 +020082 int GetSendFrameRate() const;
83
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000084 protected:
85 // From RtcpStatisticsCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000086 void StatisticsUpdated(const RtcpStatistics& statistics,
87 uint32_t ssrc) override;
88 void CNameChanged(const char* cname, uint32_t ssrc) override;
asaperssond89920b2015-07-22 06:52:00 -070089 // From RtcpPacketTypeCounterObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000090 void RtcpPacketTypesCounterUpdated(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000091 uint32_t ssrc,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000092 const RtcpPacketTypeCounter& packet_counter) override;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000093 // From StreamDataCountersCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000094 void DataCountersUpdated(const StreamDataCounters& counters,
95 uint32_t ssrc) override;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000096
97 // From BitrateStatisticsObserver.
sprangcd349d92016-07-13 09:11:28 -070098 void Notify(uint32_t total_bitrate_bps,
99 uint32_t retransmit_bitrate_bps,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000100 uint32_t ssrc) override;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000101
102 // From FrameCountObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000103 void FrameCountUpdated(const FrameCounts& frame_counts,
104 uint32_t ssrc) override;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000105
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000106 void SendSideDelayUpdated(int avg_delay_ms,
107 int max_delay_ms,
108 uint32_t ssrc) override;
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000109
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000110 private:
asaperssonda535c42015-10-19 23:32:41 -0700111 class SampleCounter {
112 public:
asaperssond89920b2015-07-22 06:52:00 -0700113 SampleCounter() : sum(0), num_samples(0) {}
asaperssonda535c42015-10-19 23:32:41 -0700114 ~SampleCounter() {}
asaperssond89920b2015-07-22 06:52:00 -0700115 void Add(int sample);
116 int Avg(int min_required_samples) const;
117
118 private:
119 int sum;
120 int num_samples;
121 };
asaperssonda535c42015-10-19 23:32:41 -0700122 class BoolSampleCounter {
123 public:
asaperssondec5ebf2015-10-05 02:36:17 -0700124 BoolSampleCounter() : sum(0), num_samples(0) {}
asaperssonda535c42015-10-19 23:32:41 -0700125 ~BoolSampleCounter() {}
asaperssondec5ebf2015-10-05 02:36:17 -0700126 void Add(bool sample);
127 int Percent(int min_required_samples) const;
128 int Permille(int min_required_samples) const;
129
130 private:
131 int Fraction(int min_required_samples, float multiplier) const;
132 int sum;
133 int num_samples;
134 };
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000135 struct StatsUpdateTimes {
sprangb4a1ae52015-12-03 08:10:08 -0800136 StatsUpdateTimes() : resolution_update_ms(0), bitrate_update_ms(0) {}
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000137 int64_t resolution_update_ms;
Peter Boström20f3f942015-05-15 11:33:39 +0200138 int64_t bitrate_update_ms;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000139 };
asapersson118ef002016-03-31 00:00:19 -0700140 struct QpCounters {
asapersson827cab32016-11-02 09:08:47 -0700141 SampleCounter vp8; // QP range: 0-127
142 SampleCounter vp9; // QP range: 0-255
143 SampleCounter h264; // QP range: 0-51
asapersson118ef002016-03-31 00:00:19 -0700144 };
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000145 void PurgeOldStats() EXCLUSIVE_LOCKS_REQUIRED(crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000146 VideoSendStream::StreamStats* GetStatsEntry(uint32_t ssrc)
147 EXCLUSIVE_LOCKS_REQUIRED(crit_);
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000148
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000149 Clock* const clock_;
perkj26091b12016-09-01 01:17:40 -0700150 const std::string payload_name_;
151 const VideoSendStream::Config::Rtp rtp_config_;
pbos5ad935c2016-01-25 03:52:44 -0800152 rtc::CriticalSection crit_;
sprangb4a1ae52015-12-03 08:10:08 -0800153 VideoEncoderConfig::ContentType content_type_ GUARDED_BY(crit_);
asapersson4374a092016-07-27 00:39:09 -0700154 const int64_t start_ms_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000155 VideoSendStream::Stats stats_ GUARDED_BY(crit_);
Åsa Persson24b4eda2015-06-16 10:17:01 +0200156 uint32_t last_sent_frame_timestamp_ GUARDED_BY(crit_);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000157 std::map<uint32_t, StatsUpdateTimes> update_times_ GUARDED_BY(crit_);
asapersson1aa420b2015-12-07 03:12:22 -0800158 rtc::ExpFilter encode_time_ GUARDED_BY(crit_);
asaperssond89920b2015-07-22 06:52:00 -0700159
sprangb4a1ae52015-12-03 08:10:08 -0800160 // Contains stats used for UMA histograms. These stats will be reset if
161 // content type changes between real-time video and screenshare, since these
162 // will be reported separately.
163 struct UmaSamplesContainer {
sprang07fb9be2016-02-24 07:55:00 -0800164 UmaSamplesContainer(const char* prefix,
165 const VideoSendStream::Stats& start_stats,
166 Clock* clock);
sprangb4a1ae52015-12-03 08:10:08 -0800167 ~UmaSamplesContainer();
168
perkj26091b12016-09-01 01:17:40 -0700169 void UpdateHistograms(const VideoSendStream::Config::Rtp& rtp_config,
sprang07fb9be2016-02-24 07:55:00 -0800170 const VideoSendStream::Stats& current_stats);
sprangb4a1ae52015-12-03 08:10:08 -0800171
172 const std::string uma_prefix_;
sprang07fb9be2016-02-24 07:55:00 -0800173 Clock* const clock_;
sprangb4a1ae52015-12-03 08:10:08 -0800174 int max_sent_width_per_timestamp_;
175 int max_sent_height_per_timestamp_;
176 SampleCounter input_width_counter_;
177 SampleCounter input_height_counter_;
178 SampleCounter sent_width_counter_;
179 SampleCounter sent_height_counter_;
180 SampleCounter encode_time_counter_;
181 BoolSampleCounter key_frame_counter_;
182 BoolSampleCounter quality_limited_frame_counter_;
183 SampleCounter quality_downscales_counter_;
perkj803d97f2016-11-01 11:45:46 -0700184 BoolSampleCounter cpu_limited_frame_counter_;
sprangb4a1ae52015-12-03 08:10:08 -0800185 BoolSampleCounter bw_limited_frame_counter_;
186 SampleCounter bw_resolutions_disabled_counter_;
187 SampleCounter delay_counter_;
188 SampleCounter max_delay_counter_;
189 rtc::RateTracker input_frame_rate_tracker_;
190 rtc::RateTracker sent_frame_rate_tracker_;
sprange2d83d62016-02-19 09:03:26 -0800191 int64_t first_rtcp_stats_time_ms_;
Erik Språng22c2b482016-03-01 09:40:42 +0100192 int64_t first_rtp_stats_time_ms_;
sprange2d83d62016-02-19 09:03:26 -0800193 ReportBlockStats report_block_stats_;
sprang07fb9be2016-02-24 07:55:00 -0800194 const VideoSendStream::Stats start_stats_;
asapersson118ef002016-03-31 00:00:19 -0700195 std::map<int, QpCounters>
196 qp_counters_; // QP counters mapped by spatial idx.
sprangb4a1ae52015-12-03 08:10:08 -0800197 };
198
kwiberg27f982b2016-03-01 11:52:33 -0800199 std::unique_ptr<UmaSamplesContainer> uma_container_ GUARDED_BY(crit_);
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000200};
201
202} // namespace webrtc
203#endif // WEBRTC_VIDEO_SEND_STATISTICS_PROXY_H_