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 | |
| 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; |
asapersson | 40f5400 | 2016-06-09 07:09:22 | [diff] [blame^] | 25 | const int kMinRequiredPeriodicSamples = 5; |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 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_) { |
asapersson | 40f5400 | 2016-06-09 07:09:22 | [diff] [blame^] | 43 | AggregatedStats stats = it.second->GetStats(); |
| 44 | if (stats.num_samples >= kMinRequiredPeriodicSamples) { |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 45 | RTC_LOGGED_HISTOGRAM_COUNTS_10000("WebRTC.Video.SendDelayInMs", |
asapersson | 40f5400 | 2016-06-09 07:09:22 | [diff] [blame^] | 46 | stats.average); |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 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 | |
asapersson | 40f5400 | 2016-06-09 07:09:22 | [diff] [blame^] | 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 | |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 69 | void SendDelayStats::OnSendPacket(uint16_t packet_id, |
| 70 | int64_t capture_time_ms, |
| 71 | uint32_t ssrc) { |
| 72 | // Packet sent to transport. |
| 73 | rtc::CritScope lock(&crit_); |
| 74 | if (ssrcs_.find(ssrc) == ssrcs_.end()) |
| 75 | return; |
| 76 | |
| 77 | int64_t now = clock_->TimeInMilliseconds(); |
| 78 | RemoveOld(now, &packets_); |
| 79 | |
| 80 | if (packets_.size() > kMaxPacketMapSize) { |
| 81 | ++num_skipped_packets_; |
| 82 | return; |
| 83 | } |
| 84 | packets_.insert( |
| 85 | std::make_pair(packet_id, Packet(ssrc, capture_time_ms, now))); |
| 86 | } |
| 87 | |
| 88 | bool SendDelayStats::OnSentPacket(int packet_id, int64_t time_ms) { |
| 89 | // Packet leaving socket. |
| 90 | if (packet_id == -1) |
| 91 | return false; |
| 92 | |
| 93 | rtc::CritScope lock(&crit_); |
| 94 | auto it = packets_.find(packet_id); |
| 95 | if (it == packets_.end()) |
| 96 | return false; |
| 97 | |
| 98 | // TODO(asapersson): Remove SendSideDelayUpdated(), use capture -> sent. |
| 99 | // Elapsed time from send (to transport) -> sent (leaving socket). |
| 100 | int diff_ms = time_ms - it->second.send_time_ms; |
asapersson | 40f5400 | 2016-06-09 07:09:22 | [diff] [blame^] | 101 | GetSendDelayCounter(it->second.ssrc)->Add(diff_ms); |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 102 | packets_.erase(it); |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | void SendDelayStats::RemoveOld(int64_t now, PacketMap* packets) { |
| 107 | while (!packets->empty()) { |
| 108 | auto it = packets->begin(); |
| 109 | if (now - it->second.capture_time_ms < kMaxSentPacketDelayMs) |
| 110 | break; |
| 111 | |
| 112 | packets->erase(it); |
| 113 | ++num_old_packets_; |
| 114 | } |
| 115 | } |
| 116 | |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 117 | } // namespace webrtc |