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