| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/video/send_delay_stats.h" | 11 #include "webrtc/video/send_delay_stats.h" |
| 12 | 12 |
| 13 #include "webrtc/base/logging.h" | 13 #include "webrtc/base/logging.h" |
| 14 #include "webrtc/system_wrappers/include/metrics.h" | 14 #include "webrtc/system_wrappers/include/metrics.h" |
| 15 | 15 |
| 16 namespace webrtc { | 16 namespace webrtc { |
| 17 namespace { | 17 namespace { |
| 18 // Packet with a larger delay are removed and excluded from the delay stats. | 18 // Packet with a larger delay are removed and excluded from the delay stats. |
| 19 // Set to larger than max histogram delay which is 10000. | 19 // Set to larger than max histogram delay which is 10000. |
| 20 const int64_t kMaxSentPacketDelayMs = 11000; | 20 const int64_t kMaxSentPacketDelayMs = 11000; |
| 21 const size_t kMaxPacketMapSize = 2000; | 21 const size_t kMaxPacketMapSize = 2000; |
| 22 | 22 |
| 23 // Limit for the maximum number of streams to calculate stats for. | 23 // Limit for the maximum number of streams to calculate stats for. |
| 24 const size_t kMaxSsrcMapSize = 50; | 24 const size_t kMaxSsrcMapSize = 50; |
| 25 const int kMinRequiredSamples = 200; | 25 const int kMinRequiredPeriodicSamples = 5; |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 SendDelayStats::SendDelayStats(Clock* clock) | 28 SendDelayStats::SendDelayStats(Clock* clock) |
| 29 : clock_(clock), num_old_packets_(0), num_skipped_packets_(0) {} | 29 : clock_(clock), num_old_packets_(0), num_skipped_packets_(0) {} |
| 30 | 30 |
| 31 SendDelayStats::~SendDelayStats() { | 31 SendDelayStats::~SendDelayStats() { |
| 32 if (num_old_packets_ > 0 || num_skipped_packets_ > 0) { | 32 if (num_old_packets_ > 0 || num_skipped_packets_ > 0) { |
| 33 LOG(LS_WARNING) << "Delay stats: number of old packets " << num_old_packets_ | 33 LOG(LS_WARNING) << "Delay stats: number of old packets " << num_old_packets_ |
| 34 << ", skipped packets " << num_skipped_packets_ | 34 << ", skipped packets " << num_skipped_packets_ |
| 35 << ". Number of streams " << send_delay_counters_.size(); | 35 << ". Number of streams " << send_delay_counters_.size(); |
| 36 } | 36 } |
| 37 UpdateHistograms(); | 37 UpdateHistograms(); |
| 38 } | 38 } |
| 39 | 39 |
| 40 void SendDelayStats::UpdateHistograms() { | 40 void SendDelayStats::UpdateHistograms() { |
| 41 rtc::CritScope lock(&crit_); | 41 rtc::CritScope lock(&crit_); |
| 42 for (const auto& it : send_delay_counters_) { | 42 for (const auto& it : send_delay_counters_) { |
| 43 int send_delay_ms = it.second.Avg(kMinRequiredSamples); | 43 AggregatedStats stats = it.second->GetStats(); |
| 44 if (send_delay_ms != -1) { | 44 if (stats.num_samples >= kMinRequiredPeriodicSamples) { |
| 45 RTC_LOGGED_HISTOGRAM_COUNTS_10000("WebRTC.Video.SendDelayInMs", | 45 RTC_LOGGED_HISTOGRAM_COUNTS_10000("WebRTC.Video.SendDelayInMs", |
| 46 send_delay_ms); | 46 stats.average); |
| 47 } | 47 } |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 | 50 |
| 51 void SendDelayStats::AddSsrcs(const VideoSendStream::Config& config) { | 51 void SendDelayStats::AddSsrcs(const VideoSendStream::Config& config) { |
| 52 rtc::CritScope lock(&crit_); | 52 rtc::CritScope lock(&crit_); |
| 53 if (ssrcs_.size() > kMaxSsrcMapSize) | 53 if (ssrcs_.size() > kMaxSsrcMapSize) |
| 54 return; | 54 return; |
| 55 for (const auto& ssrc : config.rtp.ssrcs) | 55 for (const auto& ssrc : config.rtp.ssrcs) |
| 56 ssrcs_.insert(ssrc); | 56 ssrcs_.insert(ssrc); |
| 57 } | 57 } |
| 58 | 58 |
| 59 AvgCounter* SendDelayStats::GetSendDelayCounter(uint32_t ssrc) { |
| 60 const auto& it = send_delay_counters_.find(ssrc); |
| 61 if (it != send_delay_counters_.end()) |
| 62 return it->second.get(); |
| 63 |
| 64 AvgCounter* counter = new AvgCounter(clock_, nullptr); |
| 65 send_delay_counters_[ssrc].reset(counter); |
| 66 return counter; |
| 67 } |
| 68 |
| 59 void SendDelayStats::OnSendPacket(uint16_t packet_id, | 69 void SendDelayStats::OnSendPacket(uint16_t packet_id, |
| 60 int64_t capture_time_ms, | 70 int64_t capture_time_ms, |
| 61 uint32_t ssrc) { | 71 uint32_t ssrc) { |
| 62 // Packet sent to transport. | 72 // Packet sent to transport. |
| 63 rtc::CritScope lock(&crit_); | 73 rtc::CritScope lock(&crit_); |
| 64 if (ssrcs_.find(ssrc) == ssrcs_.end()) | 74 if (ssrcs_.find(ssrc) == ssrcs_.end()) |
| 65 return; | 75 return; |
| 66 | 76 |
| 67 int64_t now = clock_->TimeInMilliseconds(); | 77 int64_t now = clock_->TimeInMilliseconds(); |
| 68 RemoveOld(now, &packets_); | 78 RemoveOld(now, &packets_); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 81 return false; | 91 return false; |
| 82 | 92 |
| 83 rtc::CritScope lock(&crit_); | 93 rtc::CritScope lock(&crit_); |
| 84 auto it = packets_.find(packet_id); | 94 auto it = packets_.find(packet_id); |
| 85 if (it == packets_.end()) | 95 if (it == packets_.end()) |
| 86 return false; | 96 return false; |
| 87 | 97 |
| 88 // TODO(asapersson): Remove SendSideDelayUpdated(), use capture -> sent. | 98 // TODO(asapersson): Remove SendSideDelayUpdated(), use capture -> sent. |
| 89 // Elapsed time from send (to transport) -> sent (leaving socket). | 99 // Elapsed time from send (to transport) -> sent (leaving socket). |
| 90 int diff_ms = time_ms - it->second.send_time_ms; | 100 int diff_ms = time_ms - it->second.send_time_ms; |
| 91 send_delay_counters_[it->second.ssrc].Add(diff_ms); | 101 GetSendDelayCounter(it->second.ssrc)->Add(diff_ms); |
| 92 packets_.erase(it); | 102 packets_.erase(it); |
| 93 return true; | 103 return true; |
| 94 } | 104 } |
| 95 | 105 |
| 96 void SendDelayStats::RemoveOld(int64_t now, PacketMap* packets) { | 106 void SendDelayStats::RemoveOld(int64_t now, PacketMap* packets) { |
| 97 while (!packets->empty()) { | 107 while (!packets->empty()) { |
| 98 auto it = packets->begin(); | 108 auto it = packets->begin(); |
| 99 if (now - it->second.capture_time_ms < kMaxSentPacketDelayMs) | 109 if (now - it->second.capture_time_ms < kMaxSentPacketDelayMs) |
| 100 break; | 110 break; |
| 101 | 111 |
| 102 packets->erase(it); | 112 packets->erase(it); |
| 103 ++num_old_packets_; | 113 ++num_old_packets_; |
| 104 } | 114 } |
| 105 } | 115 } |
| 106 | 116 |
| 107 void SendDelayStats::SampleCounter::Add(int sample) { | |
| 108 sum += sample; | |
| 109 ++num_samples; | |
| 110 } | |
| 111 | |
| 112 int SendDelayStats::SampleCounter::Avg(int min_required_samples) const { | |
| 113 if (num_samples < min_required_samples || num_samples == 0) | |
| 114 return -1; | |
| 115 return (sum + (num_samples / 2)) / num_samples; | |
| 116 } | |
| 117 | |
| 118 } // namespace webrtc | 117 } // namespace webrtc |
| OLD | NEW |