blob: 7f6df06ad8efcdb8539fa353a9b6d9fe561f5043 [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>
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000015#include <string>
16
Peter Boströmf2f82832015-05-01 13:00:41 +020017#include "webrtc/base/criticalsection.h"
asapersson1aa420b2015-12-07 03:12:22 -080018#include "webrtc/base/exp_filter.h"
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000019#include "webrtc/base/ratetracker.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000020#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000021#include "webrtc/base/thread_annotations.h"
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000022#include "webrtc/common_types.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010023#include "webrtc/modules/video_coding/include/video_codec_interface.h"
24#include "webrtc/modules/video_coding/include/video_coding_defines.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010025#include "webrtc/system_wrappers/include/clock.h"
Peter Boström7623ce42015-12-09 12:13:30 +010026#include "webrtc/video/overuse_frame_detector.h"
27#include "webrtc/video/vie_encoder.h"
pbos@webrtc.org273a4142014-12-01 15:23:21 +000028#include "webrtc/video_send_stream.h"
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000029
30namespace webrtc {
31
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +000032class SendStatisticsProxy : public CpuOveruseMetricsObserver,
33 public RtcpStatisticsCallback,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000034 public RtcpPacketTypeCounterObserver,
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000035 public StreamDataCountersCallback,
36 public BitrateStatisticsObserver,
37 public FrameCountObserver,
pbos@webrtc.org891d4832015-02-26 13:15:22 +000038 public VideoEncoderRateObserver,
stefan@webrtc.org168f23f2014-07-11 13:44:02 +000039 public SendSideDelayObserver {
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000040 public:
pbos@webrtc.org273a4142014-12-01 15:23:21 +000041 static const int kStatsTimeoutMs;
42
sprangb4a1ae52015-12-03 08:10:08 -080043 SendStatisticsProxy(Clock* clock,
44 const VideoSendStream::Config& config,
45 VideoEncoderConfig::ContentType content_type);
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000046 virtual ~SendStatisticsProxy();
47
pbos@webrtc.org273a4142014-12-01 15:23:21 +000048 VideoSendStream::Stats GetStats();
49
50 virtual void OnSendEncodedImage(const EncodedImage& encoded_image,
51 const RTPVideoHeader* rtp_video_header);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000052 // Used to update incoming frame rate.
asaperssond89920b2015-07-22 06:52:00 -070053 void OnIncomingFrame(int width, int height);
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000054
asapersson6718e972015-07-24 00:20:58 -070055 // Used to update encode time of frames.
56 void OnEncodedFrame(int encode_time_ms);
57
pbos@webrtc.org891d4832015-02-26 13:15:22 +000058 // From VideoEncoderRateObserver.
59 void OnSetRates(uint32_t bitrate_bps, int framerate) override;
60
Peter Boströmb7d9a972015-12-18 16:01:11 +010061 void OnEncoderImplementationName(const char* implementation_name);
Peter Boström7083e112015-09-22 16:28:51 +020062 void OnOutgoingRate(uint32_t framerate, uint32_t bitrate);
63 void OnSuspendChange(bool is_suspended);
Peter Boström20f3f942015-05-15 11:33:39 +020064 void OnInactiveSsrc(uint32_t ssrc);
65
sprangb4a1ae52015-12-03 08:10:08 -080066 // Used to indicate change in content type, which may require a change in
67 // how stats are collected.
68 void SetContentType(VideoEncoderConfig::ContentType content_type);
69
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000070 protected:
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +000071 // From CpuOveruseMetricsObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000072 void CpuOveruseMetricsUpdated(const CpuOveruseMetrics& metrics) override;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000073 // From RtcpStatisticsCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000074 void StatisticsUpdated(const RtcpStatistics& statistics,
75 uint32_t ssrc) override;
76 void CNameChanged(const char* cname, uint32_t ssrc) override;
asaperssond89920b2015-07-22 06:52:00 -070077 // From RtcpPacketTypeCounterObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000078 void RtcpPacketTypesCounterUpdated(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000079 uint32_t ssrc,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000080 const RtcpPacketTypeCounter& packet_counter) override;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000081 // From StreamDataCountersCallback.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000082 void DataCountersUpdated(const StreamDataCounters& counters,
83 uint32_t ssrc) override;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000084
85 // From BitrateStatisticsObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000086 void Notify(const BitrateStatistics& total_stats,
87 const BitrateStatistics& retransmit_stats,
88 uint32_t ssrc) override;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000089
90 // From FrameCountObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000091 void FrameCountUpdated(const FrameCounts& frame_counts,
92 uint32_t ssrc) override;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000093
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000094 void SendSideDelayUpdated(int avg_delay_ms,
95 int max_delay_ms,
96 uint32_t ssrc) override;
stefan@webrtc.org168f23f2014-07-11 13:44:02 +000097
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000098 private:
asaperssonda535c42015-10-19 23:32:41 -070099 class SampleCounter {
100 public:
asaperssond89920b2015-07-22 06:52:00 -0700101 SampleCounter() : sum(0), num_samples(0) {}
asaperssonda535c42015-10-19 23:32:41 -0700102 ~SampleCounter() {}
asaperssond89920b2015-07-22 06:52:00 -0700103 void Add(int sample);
104 int Avg(int min_required_samples) const;
105
106 private:
107 int sum;
108 int num_samples;
109 };
asaperssonda535c42015-10-19 23:32:41 -0700110 class BoolSampleCounter {
111 public:
asaperssondec5ebf2015-10-05 02:36:17 -0700112 BoolSampleCounter() : sum(0), num_samples(0) {}
asaperssonda535c42015-10-19 23:32:41 -0700113 ~BoolSampleCounter() {}
asaperssondec5ebf2015-10-05 02:36:17 -0700114 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.org273a4142014-12-01 15:23:21 +0000123 struct StatsUpdateTimes {
sprangb4a1ae52015-12-03 08:10:08 -0800124 StatsUpdateTimes() : resolution_update_ms(0), bitrate_update_ms(0) {}
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000125 int64_t resolution_update_ms;
Peter Boström20f3f942015-05-15 11:33:39 +0200126 int64_t bitrate_update_ms;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000127 };
128 void PurgeOldStats() EXCLUSIVE_LOCKS_REQUIRED(crit_);
pbos@webrtc.org09c77b92015-02-25 10:42:16 +0000129 VideoSendStream::StreamStats* GetStatsEntry(uint32_t ssrc)
130 EXCLUSIVE_LOCKS_REQUIRED(crit_);
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000131
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000132 Clock* const clock_;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000133 const VideoSendStream::Config config_;
Peter Boströmf2f82832015-05-01 13:00:41 +0200134 mutable rtc::CriticalSection crit_;
sprangb4a1ae52015-12-03 08:10:08 -0800135 VideoEncoderConfig::ContentType content_type_ GUARDED_BY(crit_);
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000136 VideoSendStream::Stats stats_ GUARDED_BY(crit_);
Åsa Persson24b4eda2015-06-16 10:17:01 +0200137 uint32_t last_sent_frame_timestamp_ GUARDED_BY(crit_);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000138 std::map<uint32_t, StatsUpdateTimes> update_times_ GUARDED_BY(crit_);
asapersson1aa420b2015-12-07 03:12:22 -0800139 rtc::ExpFilter encode_time_ GUARDED_BY(crit_);
asaperssond89920b2015-07-22 06:52:00 -0700140
sprangb4a1ae52015-12-03 08:10:08 -0800141 // 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 {
145 explicit UmaSamplesContainer(const char* prefix);
146 ~UmaSamplesContainer();
147
148 void UpdateHistograms();
149
150 const std::string uma_prefix_;
151 int max_sent_width_per_timestamp_;
152 int max_sent_height_per_timestamp_;
153 SampleCounter input_width_counter_;
154 SampleCounter input_height_counter_;
155 SampleCounter sent_width_counter_;
156 SampleCounter sent_height_counter_;
157 SampleCounter encode_time_counter_;
158 BoolSampleCounter key_frame_counter_;
159 BoolSampleCounter quality_limited_frame_counter_;
160 SampleCounter quality_downscales_counter_;
161 BoolSampleCounter bw_limited_frame_counter_;
162 SampleCounter bw_resolutions_disabled_counter_;
163 SampleCounter delay_counter_;
164 SampleCounter max_delay_counter_;
165 rtc::RateTracker input_frame_rate_tracker_;
166 rtc::RateTracker sent_frame_rate_tracker_;
167 };
168
169 rtc::scoped_ptr<UmaSamplesContainer> uma_container_ GUARDED_BY(crit_);
sprang@webrtc.orgccd42842014-01-07 09:54:34 +0000170};
171
172} // namespace webrtc
173#endif // WEBRTC_VIDEO_SEND_STATISTICS_PROXY_H_