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 | #include "webrtc/video/send_statistics_proxy.h" |
| 12 | |
| 13 | #include <map> |
| 14 | |
pbos@webrtc.org | 49096de | 2015-02-24 22:37:52 +0000 | [diff] [blame] | 15 | #include "webrtc/base/checks.h" |
| 16 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 17 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 18 | #include "webrtc/system_wrappers/interface/logging.h" |
Åsa Persson | 24b4eda | 2015-06-16 10:17:01 +0200 | [diff] [blame^] | 19 | #include "webrtc/system_wrappers/interface/metrics.h" |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 23 | const int SendStatisticsProxy::kStatsTimeoutMs = 5000; |
| 24 | |
| 25 | SendStatisticsProxy::SendStatisticsProxy(Clock* clock, |
| 26 | const VideoSendStream::Config& config) |
Åsa Persson | 24b4eda | 2015-06-16 10:17:01 +0200 | [diff] [blame^] | 27 | : clock_(clock), config_(config), last_sent_frame_timestamp_(0) { |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 28 | } |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 29 | |
Åsa Persson | 24b4eda | 2015-06-16 10:17:01 +0200 | [diff] [blame^] | 30 | SendStatisticsProxy::~SendStatisticsProxy() { |
| 31 | UpdateHistograms(); |
| 32 | } |
| 33 | |
| 34 | void SendStatisticsProxy::UpdateHistograms() { |
| 35 | int input_fps = |
| 36 | static_cast<int>(input_frame_rate_tracker_total_.units_second()); |
| 37 | int sent_fps = |
| 38 | static_cast<int>(sent_frame_rate_tracker_total_.units_second()); |
| 39 | |
| 40 | if (input_fps > 0) |
| 41 | RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.InputFramesPerSecond", input_fps); |
| 42 | if (sent_fps > 0) |
| 43 | RTC_HISTOGRAM_COUNTS_100("WebRTC.Video.SentFramesPerSecond", sent_fps); |
| 44 | } |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 45 | |
| 46 | void SendStatisticsProxy::OutgoingRate(const int video_channel, |
| 47 | const unsigned int framerate, |
| 48 | const unsigned int bitrate) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 49 | rtc::CritScope lock(&crit_); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 50 | stats_.encode_frame_rate = framerate; |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 51 | stats_.media_bitrate_bps = bitrate; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 52 | } |
| 53 | |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 54 | void SendStatisticsProxy::CpuOveruseMetricsUpdated( |
| 55 | const CpuOveruseMetrics& metrics) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 56 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 57 | stats_.avg_encode_time_ms = metrics.avg_encode_time_ms; |
| 58 | stats_.encode_usage_percent = metrics.encode_usage_percent; |
| 59 | } |
| 60 | |
henrik.lundin@webrtc.org | b10363f | 2014-03-13 13:31:21 +0000 | [diff] [blame] | 61 | void SendStatisticsProxy::SuspendChange(int video_channel, bool is_suspended) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 62 | rtc::CritScope lock(&crit_); |
henrik.lundin@webrtc.org | b10363f | 2014-03-13 13:31:21 +0000 | [diff] [blame] | 63 | stats_.suspended = is_suspended; |
| 64 | } |
| 65 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 66 | VideoSendStream::Stats SendStatisticsProxy::GetStats() { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 67 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 68 | PurgeOldStats(); |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 69 | stats_.input_frame_rate = |
| 70 | static_cast<int>(input_frame_rate_tracker_.units_second()); |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 71 | return stats_; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 72 | } |
| 73 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 74 | void SendStatisticsProxy::PurgeOldStats() { |
Peter Boström | 20f3f94 | 2015-05-15 11:33:39 +0200 | [diff] [blame] | 75 | int64_t old_stats_ms = clock_->TimeInMilliseconds() - kStatsTimeoutMs; |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 76 | for (std::map<uint32_t, VideoSendStream::StreamStats>::iterator it = |
| 77 | stats_.substreams.begin(); |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 78 | it != stats_.substreams.end(); ++it) { |
| 79 | uint32_t ssrc = it->first; |
Peter Boström | 20f3f94 | 2015-05-15 11:33:39 +0200 | [diff] [blame] | 80 | if (update_times_[ssrc].resolution_update_ms <= old_stats_ms) { |
| 81 | it->second.width = 0; |
| 82 | it->second.height = 0; |
| 83 | } |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 87 | VideoSendStream::StreamStats* SendStatisticsProxy::GetStatsEntry( |
| 88 | uint32_t ssrc) { |
| 89 | std::map<uint32_t, VideoSendStream::StreamStats>::iterator it = |
| 90 | stats_.substreams.find(ssrc); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 91 | if (it != stats_.substreams.end()) |
| 92 | return &it->second; |
| 93 | |
| 94 | if (std::find(config_.rtp.ssrcs.begin(), config_.rtp.ssrcs.end(), ssrc) == |
stefan@webrtc.org | 58e2d26 | 2014-08-14 15:10:49 +0000 | [diff] [blame] | 95 | config_.rtp.ssrcs.end() && |
| 96 | std::find(config_.rtp.rtx.ssrcs.begin(), |
| 97 | config_.rtp.rtx.ssrcs.end(), |
| 98 | ssrc) == config_.rtp.rtx.ssrcs.end()) { |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 99 | return nullptr; |
stefan@webrtc.org | 58e2d26 | 2014-08-14 15:10:49 +0000 | [diff] [blame] | 100 | } |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 101 | |
| 102 | return &stats_.substreams[ssrc]; // Insert new entry and return ptr. |
| 103 | } |
| 104 | |
Peter Boström | 20f3f94 | 2015-05-15 11:33:39 +0200 | [diff] [blame] | 105 | void SendStatisticsProxy::OnInactiveSsrc(uint32_t ssrc) { |
| 106 | rtc::CritScope lock(&crit_); |
| 107 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
| 108 | if (stats == nullptr) |
| 109 | return; |
| 110 | |
| 111 | stats->total_bitrate_bps = 0; |
| 112 | stats->retransmit_bitrate_bps = 0; |
| 113 | stats->height = 0; |
| 114 | stats->width = 0; |
| 115 | } |
| 116 | |
pbos@webrtc.org | 891d483 | 2015-02-26 13:15:22 +0000 | [diff] [blame] | 117 | void SendStatisticsProxy::OnSetRates(uint32_t bitrate_bps, int framerate) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 118 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 891d483 | 2015-02-26 13:15:22 +0000 | [diff] [blame] | 119 | stats_.target_media_bitrate_bps = bitrate_bps; |
| 120 | } |
| 121 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 122 | void SendStatisticsProxy::OnSendEncodedImage( |
| 123 | const EncodedImage& encoded_image, |
| 124 | const RTPVideoHeader* rtp_video_header) { |
| 125 | size_t simulcast_idx = |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 126 | rtp_video_header != nullptr ? rtp_video_header->simulcastIdx : 0; |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 127 | if (simulcast_idx >= config_.rtp.ssrcs.size()) { |
| 128 | LOG(LS_ERROR) << "Encoded image outside simulcast range (" << simulcast_idx |
| 129 | << " >= " << config_.rtp.ssrcs.size() << ")."; |
| 130 | return; |
| 131 | } |
| 132 | uint32_t ssrc = config_.rtp.ssrcs[simulcast_idx]; |
| 133 | |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 134 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 135 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 136 | if (stats == nullptr) |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 137 | return; |
| 138 | |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 139 | stats->width = encoded_image._encodedWidth; |
| 140 | stats->height = encoded_image._encodedHeight; |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 141 | update_times_[ssrc].resolution_update_ms = clock_->TimeInMilliseconds(); |
Åsa Persson | 24b4eda | 2015-06-16 10:17:01 +0200 | [diff] [blame^] | 142 | if (encoded_image._timeStamp != last_sent_frame_timestamp_) { |
| 143 | last_sent_frame_timestamp_ = encoded_image._timeStamp; |
| 144 | sent_frame_rate_tracker_total_.Update(1); |
| 145 | } |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 146 | } |
| 147 | |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 148 | void SendStatisticsProxy::OnIncomingFrame() { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 149 | rtc::CritScope lock(&crit_); |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 150 | input_frame_rate_tracker_.Update(1); |
Åsa Persson | 24b4eda | 2015-06-16 10:17:01 +0200 | [diff] [blame^] | 151 | input_frame_rate_tracker_total_.Update(1); |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 152 | } |
| 153 | |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 154 | void SendStatisticsProxy::RtcpPacketTypesCounterUpdated( |
| 155 | uint32_t ssrc, |
| 156 | const RtcpPacketTypeCounter& packet_counter) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 157 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 158 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 159 | if (stats == nullptr) |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 160 | return; |
| 161 | |
| 162 | stats->rtcp_packet_type_counts = packet_counter; |
| 163 | } |
| 164 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 165 | void SendStatisticsProxy::StatisticsUpdated(const RtcpStatistics& statistics, |
| 166 | uint32_t ssrc) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 167 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 168 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 169 | if (stats == nullptr) |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 170 | return; |
| 171 | |
| 172 | stats->rtcp_stats = statistics; |
| 173 | } |
| 174 | |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 175 | void SendStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) { |
| 176 | } |
| 177 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 178 | void SendStatisticsProxy::DataCountersUpdated( |
| 179 | const StreamDataCounters& counters, |
| 180 | uint32_t ssrc) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 181 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 182 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 183 | DCHECK(stats != nullptr) << "DataCountersUpdated reported for unknown ssrc: " |
| 184 | << ssrc; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 185 | |
| 186 | stats->rtp_stats = counters; |
| 187 | } |
| 188 | |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 189 | void SendStatisticsProxy::Notify(const BitrateStatistics& total_stats, |
| 190 | const BitrateStatistics& retransmit_stats, |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 191 | uint32_t ssrc) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 192 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 193 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 194 | if (stats == nullptr) |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 195 | return; |
| 196 | |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 197 | stats->total_bitrate_bps = total_stats.bitrate_bps; |
| 198 | stats->retransmit_bitrate_bps = retransmit_stats.bitrate_bps; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 199 | } |
| 200 | |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 201 | void SendStatisticsProxy::FrameCountUpdated(const FrameCounts& frame_counts, |
| 202 | uint32_t ssrc) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 203 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 204 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 205 | if (stats == nullptr) |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 206 | return; |
| 207 | |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 208 | stats->frame_counts = frame_counts; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 209 | } |
| 210 | |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 211 | void SendStatisticsProxy::SendSideDelayUpdated(int avg_delay_ms, |
| 212 | int max_delay_ms, |
| 213 | uint32_t ssrc) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 214 | rtc::CritScope lock(&crit_); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 215 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 2b4ce3a | 2015-03-23 13:12:24 +0000 | [diff] [blame] | 216 | if (stats == nullptr) |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 217 | return; |
| 218 | stats->avg_delay_ms = avg_delay_ms; |
| 219 | stats->max_delay_ms = max_delay_ms; |
| 220 | } |
| 221 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 222 | } // namespace webrtc |