You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.2 KiB

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