blob: e84bf10287afbab25cce5b68f2db268de96e815f [file] [log] [blame]
asapersson35151f32016-05-03 06:44:011/*
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
16namespace webrtc {
17namespace {
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.
20const int64_t kMaxSentPacketDelayMs = 11000;
21const size_t kMaxPacketMapSize = 2000;
22
23// Limit for the maximum number of streams to calculate stats for.
24const size_t kMaxSsrcMapSize = 50;
asapersson40f54002016-06-09 07:09:2225const int kMinRequiredPeriodicSamples = 5;
asapersson35151f32016-05-03 06:44:0126} // namespace
27
28SendDelayStats::SendDelayStats(Clock* clock)
29 : clock_(clock), num_old_packets_(0), num_skipped_packets_(0) {}
30
31SendDelayStats::~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
40void SendDelayStats::UpdateHistograms() {
41 rtc::CritScope lock(&crit_);
42 for (const auto& it : send_delay_counters_) {
asapersson40f54002016-06-09 07:09:2243 AggregatedStats stats = it.second->GetStats();
44 if (stats.num_samples >= kMinRequiredPeriodicSamples) {
asapersson35151f32016-05-03 06:44:0145 RTC_LOGGED_HISTOGRAM_COUNTS_10000("WebRTC.Video.SendDelayInMs",
asapersson40f54002016-06-09 07:09:2246 stats.average);
asapersson35151f32016-05-03 06:44:0147 }
48 }
49}
50
51void 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
asapersson40f54002016-06-09 07:09:2259AvgCounter* 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
asapersson35151f32016-05-03 06:44:0169void 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
88bool 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;
asapersson40f54002016-06-09 07:09:22101 GetSendDelayCounter(it->second.ssrc)->Add(diff_ms);
asapersson35151f32016-05-03 06:44:01102 packets_.erase(it);
103 return true;
104}
105
106void 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
asapersson35151f32016-05-03 06:44:01117} // namespace webrtc