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" |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 22 | const int SendStatisticsProxy::kStatsTimeoutMs = 5000; |
| 23 | |
| 24 | SendStatisticsProxy::SendStatisticsProxy(Clock* clock, |
| 25 | const VideoSendStream::Config& config) |
| 26 | : clock_(clock), |
| 27 | config_(config), |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 28 | crit_(CriticalSectionWrapper::CreateCriticalSection()) { |
| 29 | } |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 30 | |
| 31 | SendStatisticsProxy::~SendStatisticsProxy() {} |
| 32 | |
| 33 | void SendStatisticsProxy::OutgoingRate(const int video_channel, |
| 34 | const unsigned int framerate, |
| 35 | const unsigned int bitrate) { |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 36 | CriticalSectionScoped lock(crit_.get()); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 37 | stats_.encode_frame_rate = framerate; |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 38 | stats_.media_bitrate_bps = bitrate; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 39 | } |
| 40 | |
henrik.lundin@webrtc.org | b10363f | 2014-03-13 13:31:21 +0000 | [diff] [blame] | 41 | void SendStatisticsProxy::SuspendChange(int video_channel, bool is_suspended) { |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 42 | CriticalSectionScoped lock(crit_.get()); |
henrik.lundin@webrtc.org | b10363f | 2014-03-13 13:31:21 +0000 | [diff] [blame] | 43 | stats_.suspended = is_suspended; |
| 44 | } |
| 45 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 46 | void SendStatisticsProxy::CapturedFrameRate(const int capture_id, |
| 47 | const unsigned char frame_rate) { |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 48 | CriticalSectionScoped lock(crit_.get()); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 49 | stats_.input_frame_rate = frame_rate; |
| 50 | } |
| 51 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 52 | VideoSendStream::Stats SendStatisticsProxy::GetStats() { |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 53 | CriticalSectionScoped lock(crit_.get()); |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 54 | PurgeOldStats(); |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 55 | return stats_; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 56 | } |
| 57 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 58 | void SendStatisticsProxy::PurgeOldStats() { |
| 59 | int64_t current_time_ms = clock_->TimeInMilliseconds(); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame^] | 60 | for (std::map<uint32_t, VideoSendStream::StreamStats>::iterator it = |
| 61 | stats_.substreams.begin(); |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 62 | it != stats_.substreams.end(); ++it) { |
| 63 | uint32_t ssrc = it->first; |
| 64 | if (update_times_[ssrc].resolution_update_ms + kStatsTimeoutMs > |
| 65 | current_time_ms) |
| 66 | continue; |
| 67 | |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame^] | 68 | it->second.width = 0; |
| 69 | it->second.height = 0; |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame^] | 73 | VideoSendStream::StreamStats* SendStatisticsProxy::GetStatsEntry( |
| 74 | uint32_t ssrc) { |
| 75 | std::map<uint32_t, VideoSendStream::StreamStats>::iterator it = |
| 76 | stats_.substreams.find(ssrc); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 77 | if (it != stats_.substreams.end()) |
| 78 | return &it->second; |
| 79 | |
| 80 | 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] | 81 | config_.rtp.ssrcs.end() && |
| 82 | std::find(config_.rtp.rtx.ssrcs.begin(), |
| 83 | config_.rtp.rtx.ssrcs.end(), |
| 84 | ssrc) == config_.rtp.rtx.ssrcs.end()) { |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 85 | return NULL; |
stefan@webrtc.org | 58e2d26 | 2014-08-14 15:10:49 +0000 | [diff] [blame] | 86 | } |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 87 | |
| 88 | return &stats_.substreams[ssrc]; // Insert new entry and return ptr. |
| 89 | } |
| 90 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 91 | void SendStatisticsProxy::OnSendEncodedImage( |
| 92 | const EncodedImage& encoded_image, |
| 93 | const RTPVideoHeader* rtp_video_header) { |
| 94 | size_t simulcast_idx = |
| 95 | rtp_video_header != NULL ? rtp_video_header->simulcastIdx : 0; |
| 96 | if (simulcast_idx >= config_.rtp.ssrcs.size()) { |
| 97 | LOG(LS_ERROR) << "Encoded image outside simulcast range (" << simulcast_idx |
| 98 | << " >= " << config_.rtp.ssrcs.size() << ")."; |
| 99 | return; |
| 100 | } |
| 101 | uint32_t ssrc = config_.rtp.ssrcs[simulcast_idx]; |
| 102 | |
| 103 | CriticalSectionScoped lock(crit_.get()); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame^] | 104 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 105 | if (stats == NULL) |
| 106 | return; |
| 107 | |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame^] | 108 | stats->width = encoded_image._encodedWidth; |
| 109 | stats->height = encoded_image._encodedHeight; |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 110 | update_times_[ssrc].resolution_update_ms = clock_->TimeInMilliseconds(); |
| 111 | } |
| 112 | |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 113 | void SendStatisticsProxy::RtcpPacketTypesCounterUpdated( |
| 114 | uint32_t ssrc, |
| 115 | const RtcpPacketTypeCounter& packet_counter) { |
| 116 | CriticalSectionScoped lock(crit_.get()); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame^] | 117 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 118 | if (stats == NULL) |
| 119 | return; |
| 120 | |
| 121 | stats->rtcp_packet_type_counts = packet_counter; |
| 122 | } |
| 123 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 124 | void SendStatisticsProxy::StatisticsUpdated(const RtcpStatistics& statistics, |
| 125 | uint32_t ssrc) { |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 126 | CriticalSectionScoped lock(crit_.get()); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame^] | 127 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 128 | if (stats == NULL) |
| 129 | return; |
| 130 | |
| 131 | stats->rtcp_stats = statistics; |
| 132 | } |
| 133 | |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 134 | void SendStatisticsProxy::CNameChanged(const char* cname, uint32_t ssrc) { |
| 135 | } |
| 136 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 137 | void SendStatisticsProxy::DataCountersUpdated( |
| 138 | const StreamDataCounters& counters, |
| 139 | uint32_t ssrc) { |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 140 | CriticalSectionScoped lock(crit_.get()); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame^] | 141 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
pbos@webrtc.org | 49096de | 2015-02-24 22:37:52 +0000 | [diff] [blame] | 142 | DCHECK(stats != NULL) << "DataCountersUpdated reported for unknown ssrc: " |
| 143 | << ssrc; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 144 | |
| 145 | stats->rtp_stats = counters; |
| 146 | } |
| 147 | |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 148 | void SendStatisticsProxy::Notify(const BitrateStatistics& total_stats, |
| 149 | const BitrateStatistics& retransmit_stats, |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 150 | uint32_t ssrc) { |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 151 | CriticalSectionScoped lock(crit_.get()); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame^] | 152 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 153 | if (stats == NULL) |
| 154 | return; |
| 155 | |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 156 | stats->total_bitrate_bps = total_stats.bitrate_bps; |
| 157 | stats->retransmit_bitrate_bps = retransmit_stats.bitrate_bps; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 158 | } |
| 159 | |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 160 | void SendStatisticsProxy::FrameCountUpdated(const FrameCounts& frame_counts, |
| 161 | uint32_t ssrc) { |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 162 | CriticalSectionScoped lock(crit_.get()); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame^] | 163 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 164 | if (stats == NULL) |
| 165 | return; |
| 166 | |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 167 | stats->frame_counts = frame_counts; |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 168 | } |
| 169 | |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 170 | void SendStatisticsProxy::SendSideDelayUpdated(int avg_delay_ms, |
| 171 | int max_delay_ms, |
| 172 | uint32_t ssrc) { |
| 173 | CriticalSectionScoped lock(crit_.get()); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame^] | 174 | VideoSendStream::StreamStats* stats = GetStatsEntry(ssrc); |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 175 | if (stats == NULL) |
| 176 | return; |
| 177 | stats->avg_delay_ms = avg_delay_ms; |
| 178 | stats->max_delay_ms = max_delay_ms; |
| 179 | } |
| 180 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 181 | } // namespace webrtc |