blob: 8ae0615c597a6205f81600d4d2150fe9f73a8ec2 [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
asapersson35151f32016-05-02 23:44:01 -070015#include "webrtc/base/logging.h"
16#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);
asapersson35151f32016-05-02 23:44:01 -070048 }
49 }
50}
51
52void SendDelayStats::AddSsrcs(const VideoSendStream::Config& config) {
53 rtc::CritScope lock(&crit_);
54 if (ssrcs_.size() > kMaxSsrcMapSize)
55 return;
56 for (const auto& ssrc : config.rtp.ssrcs)
57 ssrcs_.insert(ssrc);
58}
59
asapersson40f54002016-06-09 00:09:22 -070060AvgCounter* SendDelayStats::GetSendDelayCounter(uint32_t ssrc) {
61 const auto& it = send_delay_counters_.find(ssrc);
62 if (it != send_delay_counters_.end())
63 return it->second.get();
64
asaperssonce2e1362016-09-09 00:13:35 -070065 AvgCounter* counter = new AvgCounter(clock_, nullptr, false);
asapersson40f54002016-06-09 00:09:22 -070066 send_delay_counters_[ssrc].reset(counter);
67 return counter;
68}
69
asapersson35151f32016-05-02 23:44:01 -070070void SendDelayStats::OnSendPacket(uint16_t packet_id,
71 int64_t capture_time_ms,
72 uint32_t ssrc) {
73 // Packet sent to transport.
74 rtc::CritScope lock(&crit_);
75 if (ssrcs_.find(ssrc) == ssrcs_.end())
76 return;
77
78 int64_t now = clock_->TimeInMilliseconds();
79 RemoveOld(now, &packets_);
80
81 if (packets_.size() > kMaxPacketMapSize) {
82 ++num_skipped_packets_;
83 return;
84 }
85 packets_.insert(
86 std::make_pair(packet_id, Packet(ssrc, capture_time_ms, now)));
87}
88
89bool SendDelayStats::OnSentPacket(int packet_id, int64_t time_ms) {
90 // Packet leaving socket.
91 if (packet_id == -1)
92 return false;
93
94 rtc::CritScope lock(&crit_);
95 auto it = packets_.find(packet_id);
96 if (it == packets_.end())
97 return false;
98
99 // TODO(asapersson): Remove SendSideDelayUpdated(), use capture -> sent.
100 // Elapsed time from send (to transport) -> sent (leaving socket).
101 int diff_ms = time_ms - it->second.send_time_ms;
asapersson40f54002016-06-09 00:09:22 -0700102 GetSendDelayCounter(it->second.ssrc)->Add(diff_ms);
asapersson35151f32016-05-02 23:44:01 -0700103 packets_.erase(it);
104 return true;
105}
106
107void SendDelayStats::RemoveOld(int64_t now, PacketMap* packets) {
108 while (!packets->empty()) {
109 auto it = packets->begin();
110 if (now - it->second.capture_time_ms < kMaxSentPacketDelayMs)
111 break;
112
113 packets->erase(it);
114 ++num_old_packets_;
115 }
116}
117
asapersson35151f32016-05-02 23:44:01 -0700118} // namespace webrtc