blob: b7c6658ad32474cbe56dd1a6d92b92d841ec4638 [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
11#include "webrtc/video/send_delay_stats.h"
12
asaperssonce2e1362016-09-09 00:13:35 -070013#include <utility>
14
Edward Lemurc20978e2017-07-06 19:44:34 +020015#include "webrtc/rtc_base/logging.h"
asapersson35151f32016-05-02 23:44:01 -070016#include "webrtc/system_wrappers/include/metrics.h"
17
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) {
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
42void SendDelayStats::UpdateHistograms() {
43 rtc::CritScope lock(&crit_);
44 for (const auto& it : send_delay_counters_) {
asapersson40f54002016-06-09 00:09:22 -070045 AggregatedStats stats = it.second->GetStats();
46 if (stats.num_samples >= kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -070047 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.SendDelayInMs", stats.average);
asapersson43cb7162016-11-15 08:20:48 -080048 LOG(LS_INFO) << "WebRTC.Video.SendDelayInMs, " << stats.ToString();
asapersson35151f32016-05-02 23:44:01 -070049 }
50 }
51}
52
53void 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
asapersson40f54002016-06-09 00:09:22 -070061AvgCounter* 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
asaperssonce2e1362016-09-09 00:13:35 -070066 AvgCounter* counter = new AvgCounter(clock_, nullptr, false);
asapersson40f54002016-06-09 00:09:22 -070067 send_delay_counters_[ssrc].reset(counter);
68 return counter;
69}
70
asapersson35151f32016-05-02 23:44:01 -070071void 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
90bool 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;
asapersson40f54002016-06-09 00:09:22 -0700103 GetSendDelayCounter(it->second.ssrc)->Add(diff_ms);
asapersson35151f32016-05-02 23:44:01 -0700104 packets_.erase(it);
105 return true;
106}
107
108void 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
asapersson35151f32016-05-02 23:44:01 -0700119} // namespace webrtc