rtp_sequence_number_map.h (2907B)
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 #ifndef MODULES_RTP_RTCP_SOURCE_RTP_SEQUENCE_NUMBER_MAP_H_ 12 #define MODULES_RTP_RTCP_SOURCE_RTP_SEQUENCE_NUMBER_MAP_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <deque> 17 #include <optional> 18 19 namespace webrtc { 20 21 // Records the association of RTP sequence numbers to timestamps and to whether 22 // the packet was first and/or last in the frame. 23 // 24 // 1. Limits number of entries. Whenever `max_entries` is about to be exceeded, 25 // the size is reduced by approximately 25%. 26 // 2. RTP sequence numbers wrap around relatively infrequently. 27 // This class therefore only remembers at most the last 2^15 RTP packets, 28 // so that the newest packet's sequence number is still AheadOf the oldest 29 // packet's sequence number. 30 // 3. Media frames are sometimes split into several RTP packets. 31 // In such a case, Insert() is expected to be called once for each packet. 32 // The timestamp is not expected to change between those calls. 33 class RtpSequenceNumberMap final { 34 public: 35 struct Info final { 36 Info(uint32_t timestamp, bool is_first, bool is_last) 37 : timestamp(timestamp), is_first(is_first), is_last(is_last) {} 38 39 friend bool operator==(const Info& lhs, const Info& rhs) { 40 return lhs.timestamp == rhs.timestamp && lhs.is_first == rhs.is_first && 41 lhs.is_last == rhs.is_last; 42 } 43 44 uint32_t timestamp; 45 bool is_first; 46 bool is_last; 47 }; 48 49 explicit RtpSequenceNumberMap(size_t max_entries); 50 RtpSequenceNumberMap(const RtpSequenceNumberMap& other) = delete; 51 RtpSequenceNumberMap& operator=(const RtpSequenceNumberMap& other) = delete; 52 ~RtpSequenceNumberMap(); 53 54 void InsertPacket(uint16_t sequence_number, Info info); 55 void InsertFrame(uint16_t first_sequence_number, 56 size_t packet_count, 57 uint32_t timestamp); 58 59 std::optional<Info> Get(uint16_t sequence_number) const; 60 61 size_t AssociationCountForTesting() const; 62 63 private: 64 struct Association { 65 explicit Association(uint16_t sequence_number) 66 : Association(sequence_number, Info(0, false, false)) {} 67 68 Association(uint16_t sequence_number, Info info) 69 : sequence_number(sequence_number), info(info) {} 70 71 uint16_t sequence_number; 72 Info info; 73 }; 74 75 const size_t max_entries_; 76 77 // The non-transitivity of AheadOf() would be problematic with a map, 78 // so we use a deque instead. 79 std::deque<Association> associations_; 80 }; 81 82 } // namespace webrtc 83 84 #endif // MODULES_RTP_RTCP_SOURCE_RTP_SEQUENCE_NUMBER_MAP_H_