sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
mflodman | d1590b2 | 2015-12-09 07:07:59 -0800 | [diff] [blame] | 14 | #include <map> |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 17 | #include "webrtc/base/criticalsection.h" |
asapersson | 1aa420b | 2015-12-07 03:12:22 -0800 | [diff] [blame] | 18 | #include "webrtc/base/exp_filter.h" |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 19 | #include "webrtc/base/ratetracker.h" |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 20 | #include "webrtc/base/scoped_ptr.h" |
pbos@webrtc.org | 38344ed | 2014-09-24 06:05:00 +0000 | [diff] [blame] | 21 | #include "webrtc/base/thread_annotations.h" |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 22 | #include "webrtc/common_types.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 23 | #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
| 24 | #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 25 | #include "webrtc/system_wrappers/include/clock.h" |
Peter Boström | 7623ce4 | 2015-12-09 12:13:30 +0100 | [diff] [blame] | 26 | #include "webrtc/video/overuse_frame_detector.h" |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 27 | #include "webrtc/video/report_block_stats.h" |
Peter Boström | 7623ce4 | 2015-12-09 12:13:30 +0100 | [diff] [blame] | 28 | #include "webrtc/video/vie_encoder.h" |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 29 | #include "webrtc/video_send_stream.h" |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 30 | |
| 31 | namespace webrtc { |
| 32 | |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 33 | class SendStatisticsProxy : public CpuOveruseMetricsObserver, |
| 34 | public RtcpStatisticsCallback, |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 35 | public RtcpPacketTypeCounterObserver, |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 36 | public StreamDataCountersCallback, |
| 37 | public BitrateStatisticsObserver, |
| 38 | public FrameCountObserver, |
pbos@webrtc.org | 891d483 | 2015-02-26 13:15:22 +0000 | [diff] [blame] | 39 | public VideoEncoderRateObserver, |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 40 | public SendSideDelayObserver { |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 41 | public: |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 42 | static const int kStatsTimeoutMs; |
| 43 | |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 44 | SendStatisticsProxy(Clock* clock, |
| 45 | const VideoSendStream::Config& config, |
| 46 | VideoEncoderConfig::ContentType content_type); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 47 | virtual ~SendStatisticsProxy(); |
| 48 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 49 | VideoSendStream::Stats GetStats(); |
| 50 | |
| 51 | virtual void OnSendEncodedImage(const EncodedImage& encoded_image, |
| 52 | const RTPVideoHeader* rtp_video_header); |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 53 | // Used to update incoming frame rate. |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 54 | void OnIncomingFrame(int width, int height); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 55 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 56 | void OnEncoderImplementationName(const char* implementation_name); |
Peter Boström | 7083e11 | 2015-09-22 16:28:51 +0200 | [diff] [blame] | 57 | void OnOutgoingRate(uint32_t framerate, uint32_t bitrate); |
| 58 | void OnSuspendChange(bool is_suspended); |
Peter Boström | 20f3f94 | 2015-05-15 11:33:39 +0200 | [diff] [blame] | 59 | void OnInactiveSsrc(uint32_t ssrc); |
| 60 | |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 61 | // Used to indicate change in content type, which may require a change in |
| 62 | // how stats are collected. |
| 63 | void SetContentType(VideoEncoderConfig::ContentType content_type); |
| 64 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 65 | // Implements VideoEncoderRateObserver. |
| 66 | void OnSetRates(uint32_t bitrate_bps, int framerate) override; |
| 67 | |
| 68 | // Implements CpuOveruseMetricsObserver. |
| 69 | void OnEncodedFrameTimeMeasured(int encode_time_ms, |
| 70 | const CpuOveruseMetrics& metrics) override; |
| 71 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 72 | protected: |
| 73 | // From RtcpStatisticsCallback. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 74 | void StatisticsUpdated(const RtcpStatistics& statistics, |
| 75 | uint32_t ssrc) override; |
| 76 | void CNameChanged(const char* cname, uint32_t ssrc) override; |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 77 | // From RtcpPacketTypeCounterObserver. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 78 | void RtcpPacketTypesCounterUpdated( |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 79 | uint32_t ssrc, |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 80 | const RtcpPacketTypeCounter& packet_counter) override; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 81 | // From StreamDataCountersCallback. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 82 | void DataCountersUpdated(const StreamDataCounters& counters, |
| 83 | uint32_t ssrc) override; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 84 | |
| 85 | // From BitrateStatisticsObserver. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 86 | void Notify(const BitrateStatistics& total_stats, |
| 87 | const BitrateStatistics& retransmit_stats, |
| 88 | uint32_t ssrc) override; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 89 | |
| 90 | // From FrameCountObserver. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 91 | void FrameCountUpdated(const FrameCounts& frame_counts, |
| 92 | uint32_t ssrc) override; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 93 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 94 | void SendSideDelayUpdated(int avg_delay_ms, |
| 95 | int max_delay_ms, |
| 96 | uint32_t ssrc) override; |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 97 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 98 | private: |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 99 | class SampleCounter { |
| 100 | public: |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 101 | SampleCounter() : sum(0), num_samples(0) {} |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 102 | ~SampleCounter() {} |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 103 | void Add(int sample); |
| 104 | int Avg(int min_required_samples) const; |
| 105 | |
| 106 | private: |
| 107 | int sum; |
| 108 | int num_samples; |
| 109 | }; |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 110 | class BoolSampleCounter { |
| 111 | public: |
asapersson | dec5ebf | 2015-10-05 02:36:17 -0700 | [diff] [blame] | 112 | BoolSampleCounter() : sum(0), num_samples(0) {} |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 113 | ~BoolSampleCounter() {} |
asapersson | dec5ebf | 2015-10-05 02:36:17 -0700 | [diff] [blame] | 114 | void Add(bool sample); |
| 115 | int Percent(int min_required_samples) const; |
| 116 | int Permille(int min_required_samples) const; |
| 117 | |
| 118 | private: |
| 119 | int Fraction(int min_required_samples, float multiplier) const; |
| 120 | int sum; |
| 121 | int num_samples; |
| 122 | }; |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 123 | struct StatsUpdateTimes { |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 124 | StatsUpdateTimes() : resolution_update_ms(0), bitrate_update_ms(0) {} |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 125 | int64_t resolution_update_ms; |
Peter Boström | 20f3f94 | 2015-05-15 11:33:39 +0200 | [diff] [blame] | 126 | int64_t bitrate_update_ms; |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 127 | }; |
| 128 | void PurgeOldStats() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 129 | VideoSendStream::StreamStats* GetStatsEntry(uint32_t ssrc) |
| 130 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 131 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 132 | Clock* const clock_; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 133 | const VideoSendStream::Config config_; |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 134 | rtc::CriticalSection crit_; |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 135 | VideoEncoderConfig::ContentType content_type_ GUARDED_BY(crit_); |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 136 | VideoSendStream::Stats stats_ GUARDED_BY(crit_); |
Åsa Persson | 24b4eda | 2015-06-16 10:17:01 +0200 | [diff] [blame] | 137 | uint32_t last_sent_frame_timestamp_ GUARDED_BY(crit_); |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 138 | std::map<uint32_t, StatsUpdateTimes> update_times_ GUARDED_BY(crit_); |
asapersson | 1aa420b | 2015-12-07 03:12:22 -0800 | [diff] [blame] | 139 | rtc::ExpFilter encode_time_ GUARDED_BY(crit_); |
asapersson | d89920b | 2015-07-22 06:52:00 -0700 | [diff] [blame] | 140 | |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 141 | // Contains stats used for UMA histograms. These stats will be reset if |
| 142 | // content type changes between real-time video and screenshare, since these |
| 143 | // will be reported separately. |
| 144 | struct UmaSamplesContainer { |
sprang | 07fb9be | 2016-02-24 07:55:00 -0800 | [diff] [blame] | 145 | UmaSamplesContainer(const char* prefix, |
| 146 | const VideoSendStream::Stats& start_stats, |
| 147 | Clock* clock); |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 148 | ~UmaSamplesContainer(); |
| 149 | |
sprang | 07fb9be | 2016-02-24 07:55:00 -0800 | [diff] [blame] | 150 | void UpdateHistograms(const VideoSendStream::Config& config, |
| 151 | const VideoSendStream::Stats& current_stats); |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 152 | |
| 153 | const std::string uma_prefix_; |
sprang | 07fb9be | 2016-02-24 07:55:00 -0800 | [diff] [blame] | 154 | Clock* const clock_; |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 155 | int max_sent_width_per_timestamp_; |
| 156 | int max_sent_height_per_timestamp_; |
| 157 | SampleCounter input_width_counter_; |
| 158 | SampleCounter input_height_counter_; |
| 159 | SampleCounter sent_width_counter_; |
| 160 | SampleCounter sent_height_counter_; |
| 161 | SampleCounter encode_time_counter_; |
| 162 | BoolSampleCounter key_frame_counter_; |
| 163 | BoolSampleCounter quality_limited_frame_counter_; |
| 164 | SampleCounter quality_downscales_counter_; |
| 165 | BoolSampleCounter bw_limited_frame_counter_; |
| 166 | SampleCounter bw_resolutions_disabled_counter_; |
| 167 | SampleCounter delay_counter_; |
| 168 | SampleCounter max_delay_counter_; |
| 169 | rtc::RateTracker input_frame_rate_tracker_; |
| 170 | rtc::RateTracker sent_frame_rate_tracker_; |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 171 | int64_t first_rtcp_stats_time_ms_; |
Erik Språng | 22c2b48 | 2016-03-01 09:40:42 +0100 | [diff] [blame^] | 172 | int64_t first_rtp_stats_time_ms_; |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 173 | ReportBlockStats report_block_stats_; |
sprang | 07fb9be | 2016-02-24 07:55:00 -0800 | [diff] [blame] | 174 | const VideoSendStream::Stats start_stats_; |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | rtc::scoped_ptr<UmaSamplesContainer> uma_container_ GUARDED_BY(crit_); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | } // namespace webrtc |
| 181 | #endif // WEBRTC_VIDEO_SEND_STATISTICS_PROXY_H_ |