mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-16 07:10:38 +01:00

RTP timestamp was recently added to contributing sources in the WebRTC specification. This CL implements that change in WebRTC. Bug: webrtc:10650 Change-Id: Ic0ccfbea7049a5b66063fa6cf60d01d5bd713132 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/137515 Reviewed-by: Fredrik Solenberg <solenberg@webrtc.org> Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org> Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Commit-Queue: Johannes Kron <kron@webrtc.org> Cr-Commit-Position: refs/heads/master@{#28020}
81 lines
2.7 KiB
C++
81 lines
2.7 KiB
C++
/*
|
|
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "modules/rtp_rtcp/source/contributing_sources.h"
|
|
|
|
namespace webrtc {
|
|
|
|
namespace {
|
|
|
|
// Allow some stale records to accumulate before cleaning.
|
|
constexpr int64_t kPruningIntervalMs = 15 * rtc::kNumMillisecsPerSec;
|
|
|
|
} // namespace
|
|
|
|
constexpr int64_t ContributingSources::kHistoryMs;
|
|
|
|
ContributingSources::ContributingSources() = default;
|
|
ContributingSources::~ContributingSources() = default;
|
|
|
|
void ContributingSources::Update(int64_t now_ms,
|
|
rtc::ArrayView<const uint32_t> csrcs,
|
|
absl::optional<uint8_t> audio_level,
|
|
uint32_t rtp_timestamp) {
|
|
Entry entry = {now_ms, audio_level, rtp_timestamp};
|
|
for (uint32_t csrc : csrcs) {
|
|
active_csrcs_[csrc] = entry;
|
|
}
|
|
if (!next_pruning_ms_) {
|
|
next_pruning_ms_ = now_ms + kPruningIntervalMs;
|
|
} else if (now_ms > next_pruning_ms_) {
|
|
// To prevent unlimited growth, prune it every 15 seconds.
|
|
DeleteOldEntries(now_ms);
|
|
}
|
|
}
|
|
|
|
// Return contributing sources seen the last 10 s.
|
|
// TODO(nisse): It would be more efficient to delete any stale entries while
|
|
// iterating over the mapping, but then we'd have to make the method
|
|
// non-const.
|
|
std::vector<RtpSource> ContributingSources::GetSources(int64_t now_ms) const {
|
|
std::vector<RtpSource> sources;
|
|
for (auto& record : active_csrcs_) {
|
|
if (record.second.last_seen_ms >= now_ms - kHistoryMs) {
|
|
sources.emplace_back(record.second.last_seen_ms, record.first,
|
|
RtpSourceType::CSRC, record.second.audio_level,
|
|
record.second.rtp_timestamp);
|
|
}
|
|
}
|
|
|
|
return sources;
|
|
}
|
|
|
|
// Delete stale entries.
|
|
void ContributingSources::DeleteOldEntries(int64_t now_ms) {
|
|
for (auto it = active_csrcs_.begin(); it != active_csrcs_.end();) {
|
|
if (it->second.last_seen_ms >= now_ms - kHistoryMs) {
|
|
// Still relevant.
|
|
++it;
|
|
} else {
|
|
it = active_csrcs_.erase(it);
|
|
}
|
|
}
|
|
next_pruning_ms_ = now_ms + kPruningIntervalMs;
|
|
}
|
|
|
|
ContributingSources::Entry::Entry() = default;
|
|
ContributingSources::Entry::Entry(int64_t timestamp_ms,
|
|
absl::optional<uint8_t> audio_level_arg,
|
|
uint32_t rtp_timestamp)
|
|
: last_seen_ms(timestamp_ms),
|
|
audio_level(audio_level_arg),
|
|
rtp_timestamp(rtp_timestamp) {}
|
|
|
|
} // namespace webrtc
|