1 /*
2 * Copyright (c) 2019 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 "media/engine/unhandled_packets_buffer.h"
12
13 #include "absl/algorithm/container.h"
14 #include "rtc_base/logging.h"
15 #include "rtc_base/strings/string_builder.h"
16
17 namespace cricket {
18
UnhandledPacketsBuffer()19 UnhandledPacketsBuffer::UnhandledPacketsBuffer() {
20 buffer_.reserve(kMaxStashedPackets);
21 }
22
23 UnhandledPacketsBuffer::~UnhandledPacketsBuffer() = default;
24
25 // Store packet in buffer.
AddPacket(uint32_t ssrc,int64_t packet_time_us,rtc::CopyOnWriteBuffer packet)26 void UnhandledPacketsBuffer::AddPacket(uint32_t ssrc,
27 int64_t packet_time_us,
28 rtc::CopyOnWriteBuffer packet) {
29 if (buffer_.size() < kMaxStashedPackets) {
30 buffer_.push_back({ssrc, packet_time_us, packet});
31 } else {
32 RTC_DCHECK_LT(insert_pos_, kMaxStashedPackets);
33 buffer_[insert_pos_] = {ssrc, packet_time_us, packet};
34 }
35 insert_pos_ = (insert_pos_ + 1) % kMaxStashedPackets;
36 }
37
38 // Backfill |consumer| with all stored packet related |ssrcs|.
BackfillPackets(rtc::ArrayView<const uint32_t> ssrcs,std::function<void (uint32_t,int64_t,rtc::CopyOnWriteBuffer)> consumer)39 void UnhandledPacketsBuffer::BackfillPackets(
40 rtc::ArrayView<const uint32_t> ssrcs,
41 std::function<void(uint32_t, int64_t, rtc::CopyOnWriteBuffer)> consumer) {
42 size_t start;
43 if (buffer_.size() < kMaxStashedPackets) {
44 start = 0;
45 } else {
46 start = insert_pos_;
47 }
48
49 size_t count = 0;
50 std::vector<PacketWithMetadata> remaining;
51 remaining.reserve(kMaxStashedPackets);
52 for (size_t i = 0; i < buffer_.size(); ++i) {
53 const size_t pos = (i + start) % kMaxStashedPackets;
54
55 // One or maybe 2 ssrcs is expected => loop array instead of more elaborate
56 // scheme.
57 const uint32_t ssrc = buffer_[pos].ssrc;
58 if (absl::c_linear_search(ssrcs, ssrc)) {
59 ++count;
60 consumer(ssrc, buffer_[pos].packet_time_us, buffer_[pos].packet);
61 } else {
62 remaining.push_back(buffer_[pos]);
63 }
64 }
65
66 insert_pos_ = 0; // insert_pos is only used when buffer is full.
67 buffer_.swap(remaining);
68 }
69
70 } // namespace cricket
71