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