packet.h (4701B)
1 /* 2 * Copyright (c) 2012 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_AUDIO_CODING_NETEQ_PACKET_H_ 12 #define MODULES_AUDIO_CODING_NETEQ_PACKET_H_ 13 14 #include <stdint.h> 15 16 #include <list> 17 #include <memory> 18 #include <optional> 19 20 #include "api/audio_codecs/audio_decoder.h" 21 #include "api/neteq/tick_timer.h" 22 #include "api/rtp_packet_info.h" 23 #include "rtc_base/buffer.h" 24 #include "rtc_base/checks.h" 25 26 namespace webrtc { 27 28 // Struct for holding RTP packets. 29 struct Packet { 30 struct Priority { 31 Priority() : codec_level(0), red_level(0) {} 32 Priority(int codec_level, int red_level) 33 : codec_level(codec_level), red_level(red_level) { 34 CheckInvariant(); 35 } 36 37 int codec_level; 38 int red_level; 39 40 // Priorities are sorted low-to-high, first on the level the codec 41 // prioritizes it, then on the level of RED packet it is; i.e. if it is a 42 // primary or secondary payload of a RED packet. For example: with Opus, an 43 // Fec packet (which the decoder prioritizes lower than a regular packet) 44 // will not be used if there is _any_ RED payload for the same 45 // timeframe. The highest priority packet will have levels {0, 0}. Negative 46 // priorities are not allowed. 47 bool operator<(const Priority& b) const { 48 CheckInvariant(); 49 b.CheckInvariant(); 50 if (codec_level == b.codec_level) 51 return red_level < b.red_level; 52 53 return codec_level < b.codec_level; 54 } 55 bool operator==(const Priority& b) const { 56 CheckInvariant(); 57 b.CheckInvariant(); 58 return codec_level == b.codec_level && red_level == b.red_level; 59 } 60 bool operator!=(const Priority& b) const { return !(*this == b); } 61 bool operator>(const Priority& b) const { return b < *this; } 62 bool operator<=(const Priority& b) const { return !(b > *this); } 63 bool operator>=(const Priority& b) const { return !(b < *this); } 64 65 private: 66 void CheckInvariant() const { 67 RTC_DCHECK_GE(codec_level, 0); 68 RTC_DCHECK_GE(red_level, 0); 69 } 70 }; 71 72 uint32_t timestamp; 73 uint16_t sequence_number; 74 uint8_t payload_type; 75 // Datagram excluding RTP header and header extension. 76 Buffer payload; 77 Priority priority; 78 std::optional<RtpPacketInfo> packet_info; 79 std::unique_ptr<TickTimer::Stopwatch> waiting_time; 80 std::unique_ptr<AudioDecoder::EncodedAudioFrame> frame; 81 82 Packet(); 83 Packet(Packet&& b); 84 ~Packet(); 85 86 // Packets should generally be moved around but sometimes it's useful to make 87 // a copy, for example for testing purposes. NOTE: Will only work for 88 // un-parsed packets, i.e. `frame` must be unset. The payload will, however, 89 // be copied. `waiting_time` will also not be copied. 90 Packet Clone() const; 91 92 Packet& operator=(Packet&& b); 93 94 // Comparison operators. Establish a packet ordering based on (1) timestamp, 95 // (2) sequence number and (3) redundancy. 96 // Timestamp and sequence numbers are compared taking wrap-around into 97 // account. For two packets with the same sequence number and timestamp a 98 // primary payload is considered "smaller" than a secondary. 99 bool operator==(const Packet& rhs) const { 100 return (this->timestamp == rhs.timestamp && 101 this->sequence_number == rhs.sequence_number && 102 this->priority == rhs.priority); 103 } 104 bool operator!=(const Packet& rhs) const { return !operator==(rhs); } 105 bool operator<(const Packet& rhs) const { 106 if (this->timestamp == rhs.timestamp) { 107 if (this->sequence_number == rhs.sequence_number) { 108 // Timestamp and sequence numbers are identical - deem the left hand 109 // side to be "smaller" (i.e., "earlier") if it has higher priority. 110 return this->priority < rhs.priority; 111 } 112 return (static_cast<uint16_t>(rhs.sequence_number - 113 this->sequence_number) < 0xFFFF / 2); 114 } 115 return (static_cast<uint32_t>(rhs.timestamp - this->timestamp) < 116 0xFFFFFFFF / 2); 117 } 118 bool operator>(const Packet& rhs) const { return rhs.operator<(*this); } 119 bool operator<=(const Packet& rhs) const { return !operator>(rhs); } 120 bool operator>=(const Packet& rhs) const { return !operator<(rhs); } 121 122 bool empty() const { return !frame && payload.empty(); } 123 }; 124 125 // A list of packets. 126 typedef std::list<Packet> PacketList; 127 128 } // namespace webrtc 129 #endif // MODULES_AUDIO_CODING_NETEQ_PACKET_H_