blob: 853706c4f5dc140909ee22c9e26c02d47fac1b94 [file] [log] [blame]
Niels Mölleraf175952018-08-13 13:23:08 +02001/*
2 * Copyright (c) 2018 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 "modules/rtp_rtcp/source/contributing_sources.h"
12
13namespace webrtc {
14
15namespace {
16
Niels Mölleraf175952018-08-13 13:23:08 +020017// Allow some stale records to accumulate before cleaning.
18constexpr int64_t kPruningIntervalMs = 15 * rtc::kNumMillisecsPerSec;
19
20} // namespace
21
Niels Möller30b48392018-08-15 15:05:26 +020022constexpr int64_t ContributingSources::kHistoryMs;
23
Niels Mölleraf175952018-08-13 13:23:08 +020024ContributingSources::ContributingSources() = default;
25ContributingSources::~ContributingSources() = default;
26
27void ContributingSources::Update(int64_t now_ms,
28 rtc::ArrayView<const uint32_t> csrcs) {
29 for (uint32_t csrc : csrcs) {
30 last_seen_ms_[csrc] = now_ms;
31 }
32 if (!next_pruning_ms_) {
33 next_pruning_ms_ = now_ms + kPruningIntervalMs;
34 } else if (now_ms > next_pruning_ms_) {
35 // To prevent unlimited growth, prune it every 15 seconds.
36 DeleteOldEntries(now_ms);
37 }
38}
39
40// Return contributing sources seen the last 10 s.
41// TODO(nisse): It would be more efficient to delete any stale entries while
42// iterating over the mapping, but then we'd have to make the method
43// non-const.
44std::vector<RtpSource> ContributingSources::GetSources(int64_t now_ms) const {
45 std::vector<RtpSource> sources;
46 for (auto& record : last_seen_ms_) {
47 if (record.second >= now_ms - kHistoryMs) {
48 sources.emplace_back(record.second, record.first, RtpSourceType::CSRC);
49 }
50 }
51
52 return sources;
53}
54
55// Delete stale entries.
56void ContributingSources::DeleteOldEntries(int64_t now_ms) {
57 for (auto it = last_seen_ms_.begin(); it != last_seen_ms_.end();) {
58 if (it->second >= now_ms - kHistoryMs) {
59 // Still relevant.
60 ++it;
61 } else {
62 it = last_seen_ms_.erase(it);
63 }
64 }
65 next_pruning_ms_ = now_ms + kPruningIntervalMs;
66}
67
68} // namespace webrtc