| 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 "video/send_delay_stats.h" |
| 12 |
| 13 | #include <utility> |
| 14 |
| 15 | #include "rtc_base/logging.h" |
| 16 | #include "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 10 seconds. |
| 22 | constexpr TimeDelta kMaxSentPacketDelay = TimeDelta::Seconds(11); |
| 23 | constexpr size_t kMaxPacketMapSize = 2000; |
| 24 |
| 25 | // Limit for the maximum number of streams to calculate stats for. |
| 26 | constexpr size_t kMaxSsrcMapSize = 50; |
| 27 | constexpr int kMinRequiredPeriodicSamples = 5; |
| 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 | 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(); |
| 39 | } |
| 40 | UpdateHistograms(); |
| 41 | } |
| 42 |
| 43 | void SendDelayStats::UpdateHistograms() { |
| 44 | MutexLock lock(&mutex_); |
| 45 | for (auto& [unused, counter] : send_delay_counters_) { |
| 46 | AggregatedStats stats = counter.GetStats(); |
| 47 | if (stats.num_samples >= kMinRequiredPeriodicSamples) { |
| 48 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.SendDelayInMs", stats.average); |
| 49 | RTC_LOG(LS_INFO) << "WebRTC.Video.SendDelayInMs, " << stats.ToString(); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 |
| 54 | void SendDelayStats::AddSsrcs(const VideoSendStream::Config& config) { |
| 55 | MutexLock lock(&mutex_); |
| 56 | if (send_delay_counters_.size() + config.rtp.ssrcs.size() > kMaxSsrcMapSize) |
| 57 | return; |
| 58 | for (uint32_t ssrc : config.rtp.ssrcs) { |
| 59 | send_delay_counters_.try_emplace(ssrc, clock_, nullptr, false); |
| 60 | } |
| 61 | } |
| 62 |
| 63 | void SendDelayStats::OnSendPacket(uint16_t packet_id, |
| 64 | Timestamp capture_time, |
| 65 | uint32_t ssrc) { |
| 66 | // Packet sent to transport. |
| 67 | MutexLock lock(&mutex_); |
| 68 | auto it = send_delay_counters_.find(ssrc); |
| 69 | if (it == send_delay_counters_.end()) |
| 70 | return; |
| 71 |
| 72 | Timestamp now = clock_->CurrentTime(); |
| 73 | RemoveOld(now); |
| 74 |
| 75 | if (packets_.size() > kMaxPacketMapSize) { |
| 76 | ++num_skipped_packets_; |
| 77 | return; |
| 78 | } |
| 79 | // `send_delay_counters_` is an std::map - adding new entries doesn't |
| 80 | // invalidate existent iterators, and it has pointer stability for values. |
| 81 | // Entries are never remove from the `send_delay_counters_`. |
| 82 | // Thus memorizing pointer to the AvgCounter is safe. |
| 83 | packets_.emplace(packet_id, Packet{.send_delay = &it->second, |
| 84 | .capture_time = capture_time, |
| 85 | .send_time = now}); |
| 86 | } |
| 87 |
| 88 | bool SendDelayStats::OnSentPacket(int packet_id, Timestamp time) { |
| 89 | // Packet leaving socket. |
| 90 | if (packet_id == -1) |
| 91 | return false; |
| 92 |
| 93 | MutexLock lock(&mutex_); |
| 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 | TimeDelta diff = time - it->second.send_time; |
| 101 | it->second.send_delay->Add(diff.ms()); |
| 102 | packets_.erase(it); |
| 103 | return true; |
| 104 | } |
| 105 |
| 106 | void SendDelayStats::RemoveOld(Timestamp now) { |
| 107 | while (!packets_.empty()) { |
| 108 | auto it = packets_.begin(); |
| 109 | if (now - it->second.capture_time < kMaxSentPacketDelay) |
| 110 | break; |
| 111 |
| 112 | packets_.erase(it); |
| 113 | ++num_old_packets_; |
| 114 | } |
| 115 | } |
| 116 |
| 117 | } // namespace webrtc |
| 118 |