OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 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_delay_stats.h" |
| 12 |
| 13 #include "webrtc/base/logging.h" |
| 14 #include "webrtc/system_wrappers/include/metrics.h" |
| 15 |
| 16 namespace webrtc { |
| 17 namespace { |
| 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. |
| 20 const int64_t kMaxSentPacketDelayMs = 11000; |
| 21 const size_t kMaxPacketMapSize = 2000; |
| 22 |
| 23 // Limit for the maximum number of streams to calculate stats for. |
| 24 const size_t kMaxSsrcMapSize = 50; |
| 25 const int kMinRequiredSamples = 200; |
| 26 } // namespace |
| 27 |
| 28 SendDelayStats::SendDelayStats(Clock* clock) |
| 29 : clock_(clock), num_old_packets_(0), num_skipped_packets_(0) {} |
| 30 |
| 31 SendDelayStats::~SendDelayStats() { |
| 32 if (num_old_packets_ > 0 || num_skipped_packets_ > 0) { |
| 33 LOG(LS_WARNING) << "Delay stats: number of old packets " << num_old_packets_ |
| 34 << ", skipped packets " << num_skipped_packets_ |
| 35 << ". Number of streams " << send_delay_counters_.size(); |
| 36 } |
| 37 UpdateHistograms(); |
| 38 } |
| 39 |
| 40 void SendDelayStats::UpdateHistograms() { |
| 41 rtc::CritScope lock(&crit_); |
| 42 for (const auto& it : send_delay_counters_) { |
| 43 int send_delay_ms = it.second.Avg(kMinRequiredSamples); |
| 44 if (send_delay_ms != -1) { |
| 45 RTC_LOGGED_HISTOGRAM_COUNTS_10000("WebRTC.Video.SendDelayInMs", |
| 46 send_delay_ms); |
| 47 } |
| 48 } |
| 49 } |
| 50 |
| 51 void SendDelayStats::AddSsrcs(const VideoSendStream::Config& config) { |
| 52 rtc::CritScope lock(&crit_); |
| 53 if (ssrcs_.size() > kMaxSsrcMapSize) |
| 54 return; |
| 55 for (const auto& ssrc : config.rtp.ssrcs) |
| 56 ssrcs_.insert(ssrc); |
| 57 } |
| 58 |
| 59 void SendDelayStats::OnSendPacket(uint16_t packet_id, |
| 60 int64_t capture_time_ms, |
| 61 uint32_t ssrc) { |
| 62 // Packet sent to transport. |
| 63 rtc::CritScope lock(&crit_); |
| 64 if (ssrcs_.find(ssrc) == ssrcs_.end()) |
| 65 return; |
| 66 |
| 67 int64_t now = clock_->TimeInMilliseconds(); |
| 68 RemoveOld(now, &packets_); |
| 69 |
| 70 if (packets_.size() > kMaxPacketMapSize) { |
| 71 ++num_skipped_packets_; |
| 72 return; |
| 73 } |
| 74 packets_.insert( |
| 75 std::make_pair(packet_id, Packet(ssrc, capture_time_ms, now))); |
| 76 } |
| 77 |
| 78 bool SendDelayStats::OnSentPacket(int packet_id, int64_t time_ms) { |
| 79 // Packet leaving socket. |
| 80 if (packet_id == -1) |
| 81 return false; |
| 82 |
| 83 rtc::CritScope lock(&crit_); |
| 84 auto it = packets_.find(packet_id); |
| 85 if (it == packets_.end()) |
| 86 return false; |
| 87 |
| 88 // TODO(asapersson): Remove SendSideDelayUpdated(), use capture -> sent. |
| 89 // Elapsed time from send (to transport) -> sent (leaving socket). |
| 90 int diff_ms = time_ms - it->second.send_time_ms; |
| 91 send_delay_counters_[it->second.ssrc].Add(diff_ms); |
| 92 packets_.erase(it); |
| 93 return true; |
| 94 } |
| 95 |
| 96 void SendDelayStats::RemoveOld(int64_t now, PacketMap* packets) { |
| 97 while (!packets->empty()) { |
| 98 auto it = packets->begin(); |
| 99 if (now - it->second.capture_time_ms < kMaxSentPacketDelayMs) |
| 100 break; |
| 101 |
| 102 packets->erase(it); |
| 103 ++num_old_packets_; |
| 104 } |
| 105 } |
| 106 |
| 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 |
OLD | NEW |