asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "video/send_delay_stats.h" |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 12 | |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 15 | #include "rtc_base/logging.h" |
| 16 | #include "system_wrappers/include/metrics.h" |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | namespace { |
| 20 | // Packet with a larger delay are removed and excluded from the delay stats. |
| 21 | // Set to larger than max histogram delay which is 10000. |
| 22 | const int64_t kMaxSentPacketDelayMs = 11000; |
| 23 | const size_t kMaxPacketMapSize = 2000; |
| 24 | |
| 25 | // Limit for the maximum number of streams to calculate stats for. |
| 26 | const size_t kMaxSsrcMapSize = 50; |
asapersson | 40f5400 | 2016-06-09 07:09:22 | [diff] [blame] | 27 | const int kMinRequiredPeriodicSamples = 5; |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 28 | } // namespace |
| 29 | |
| 30 | SendDelayStats::SendDelayStats(Clock* clock) |
| 31 | : clock_(clock), num_old_packets_(0), num_skipped_packets_(0) {} |
| 32 | |
| 33 | SendDelayStats::~SendDelayStats() { |
| 34 | if (num_old_packets_ > 0 || num_skipped_packets_ > 0) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 35 | RTC_LOG(LS_WARNING) << "Delay stats: number of old packets " |
| 36 | << num_old_packets_ << ", skipped packets " |
| 37 | << num_skipped_packets_ << ". Number of streams " |
| 38 | << send_delay_counters_.size(); |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 39 | } |
| 40 | UpdateHistograms(); |
| 41 | } |
| 42 | |
| 43 | void SendDelayStats::UpdateHistograms() { |
| 44 | rtc::CritScope lock(&crit_); |
| 45 | for (const auto& it : send_delay_counters_) { |
asapersson | 40f5400 | 2016-06-09 07:09:22 | [diff] [blame] | 46 | AggregatedStats stats = it.second->GetStats(); |
| 47 | if (stats.num_samples >= kMinRequiredPeriodicSamples) { |
asapersson | 1d02d3e | 2016-09-10 05:40:25 | [diff] [blame] | 48 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.SendDelayInMs", stats.average); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 49 | RTC_LOG(LS_INFO) << "WebRTC.Video.SendDelayInMs, " << stats.ToString(); |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void SendDelayStats::AddSsrcs(const VideoSendStream::Config& config) { |
| 55 | rtc::CritScope lock(&crit_); |
| 56 | if (ssrcs_.size() > kMaxSsrcMapSize) |
| 57 | return; |
| 58 | for (const auto& ssrc : config.rtp.ssrcs) |
| 59 | ssrcs_.insert(ssrc); |
| 60 | } |
| 61 | |
asapersson | 40f5400 | 2016-06-09 07:09:22 | [diff] [blame] | 62 | AvgCounter* SendDelayStats::GetSendDelayCounter(uint32_t ssrc) { |
| 63 | const auto& it = send_delay_counters_.find(ssrc); |
| 64 | if (it != send_delay_counters_.end()) |
| 65 | return it->second.get(); |
| 66 | |
asapersson | ce2e136 | 2016-09-09 07:13:35 | [diff] [blame] | 67 | AvgCounter* counter = new AvgCounter(clock_, nullptr, false); |
asapersson | 40f5400 | 2016-06-09 07:09:22 | [diff] [blame] | 68 | send_delay_counters_[ssrc].reset(counter); |
| 69 | return counter; |
| 70 | } |
| 71 | |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 72 | void SendDelayStats::OnSendPacket(uint16_t packet_id, |
| 73 | int64_t capture_time_ms, |
| 74 | uint32_t ssrc) { |
| 75 | // Packet sent to transport. |
| 76 | rtc::CritScope lock(&crit_); |
| 77 | if (ssrcs_.find(ssrc) == ssrcs_.end()) |
| 78 | return; |
| 79 | |
| 80 | int64_t now = clock_->TimeInMilliseconds(); |
| 81 | RemoveOld(now, &packets_); |
| 82 | |
| 83 | if (packets_.size() > kMaxPacketMapSize) { |
| 84 | ++num_skipped_packets_; |
| 85 | return; |
| 86 | } |
| 87 | packets_.insert( |
| 88 | std::make_pair(packet_id, Packet(ssrc, capture_time_ms, now))); |
| 89 | } |
| 90 | |
| 91 | bool SendDelayStats::OnSentPacket(int packet_id, int64_t time_ms) { |
| 92 | // Packet leaving socket. |
| 93 | if (packet_id == -1) |
| 94 | return false; |
| 95 | |
| 96 | rtc::CritScope lock(&crit_); |
| 97 | auto it = packets_.find(packet_id); |
| 98 | if (it == packets_.end()) |
| 99 | return false; |
| 100 | |
| 101 | // TODO(asapersson): Remove SendSideDelayUpdated(), use capture -> sent. |
| 102 | // Elapsed time from send (to transport) -> sent (leaving socket). |
| 103 | int diff_ms = time_ms - it->second.send_time_ms; |
asapersson | 40f5400 | 2016-06-09 07:09:22 | [diff] [blame] | 104 | GetSendDelayCounter(it->second.ssrc)->Add(diff_ms); |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 105 | packets_.erase(it); |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | void SendDelayStats::RemoveOld(int64_t now, PacketMap* packets) { |
| 110 | while (!packets->empty()) { |
| 111 | auto it = packets->begin(); |
| 112 | if (now - it->second.capture_time_ms < kMaxSentPacketDelayMs) |
| 113 | break; |
| 114 | |
| 115 | packets->erase(it); |
| 116 | ++num_old_packets_; |
| 117 | } |
| 118 | } |
| 119 | |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 120 | } // namespace webrtc |