blob: a243eda29223a79c6fb3de5c738386dd7988e44a [file] [log] [blame]
asapersson35151f32016-05-02 23:44:01 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "video/send_delay_stats.h"
asapersson35151f32016-05-02 23:44:01 -070012
asaperssonce2e1362016-09-09 00:13:35 -070013#include <utility>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/logging.h"
16#include "system_wrappers/include/metrics.h"
asapersson35151f32016-05-02 23:44:01 -070017
18namespace webrtc {
19namespace {
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.
22const int64_t kMaxSentPacketDelayMs = 11000;
23const size_t kMaxPacketMapSize = 2000;
24
25// Limit for the maximum number of streams to calculate stats for.
26const size_t kMaxSsrcMapSize = 50;
asapersson40f54002016-06-09 00:09:22 -070027const int kMinRequiredPeriodicSamples = 5;
asapersson35151f32016-05-02 23:44:01 -070028} // namespace
29
30SendDelayStats::SendDelayStats(Clock* clock)
31 : clock_(clock), num_old_packets_(0), num_skipped_packets_(0) {}
32
33SendDelayStats::~SendDelayStats() {
34 if (num_old_packets_ > 0 || num_skipped_packets_ > 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010035 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();
asapersson35151f32016-05-02 23:44:01 -070039 }
40 UpdateHistograms();
41}
42
43void SendDelayStats::UpdateHistograms() {
44 rtc::CritScope lock(&crit_);
45 for (const auto& it : send_delay_counters_) {
asapersson40f54002016-06-09 00:09:22 -070046 AggregatedStats stats = it.second->GetStats();
47 if (stats.num_samples >= kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -070048 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.SendDelayInMs", stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +010049 RTC_LOG(LS_INFO) << "WebRTC.Video.SendDelayInMs, " << stats.ToString();
asapersson35151f32016-05-02 23:44:01 -070050 }
51 }
52}
53
54void 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
asapersson40f54002016-06-09 00:09:22 -070062AvgCounter* 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
asaperssonce2e1362016-09-09 00:13:35 -070067 AvgCounter* counter = new AvgCounter(clock_, nullptr, false);
asapersson40f54002016-06-09 00:09:22 -070068 send_delay_counters_[ssrc].reset(counter);
69 return counter;
70}
71
asapersson35151f32016-05-02 23:44:01 -070072void 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
91bool 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;
asapersson40f54002016-06-09 00:09:22 -0700104 GetSendDelayCounter(it->second.ssrc)->Add(diff_ms);
asapersson35151f32016-05-02 23:44:01 -0700105 packets_.erase(it);
106 return true;
107}
108
109void 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
asapersson35151f32016-05-02 23:44:01 -0700120} // namespace webrtc