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 | |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 11 | #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 15 | #include <memory> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 16 | |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 17 | #include "webrtc/modules/audio_coding/neteq/tick_timer.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 18 | #include "webrtc/modules/include/module_common_types.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 19 | #include "webrtc/typedefs.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | // Struct for holding RTP packets. |
| 24 | struct Packet { |
| 25 | RTPHeader header; |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 26 | // Datagram excluding RTP header and header extension. |
| 27 | uint8_t* payload = nullptr; |
| 28 | size_t payload_length = 0; |
| 29 | bool primary = true; // Primary, i.e., not redundant payload. |
| 30 | bool sync_packet = false; |
| 31 | std::unique_ptr<TickTimer::Stopwatch> waiting_time; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 32 | |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 33 | Packet(); |
| 34 | ~Packet(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 35 | |
| 36 | // Comparison operators. Establish a packet ordering based on (1) timestamp, |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 37 | // (2) sequence number, (3) regular packet vs sync-packet and (4) redundancy. |
| 38 | // Timestamp and sequence numbers are compared taking wrap-around into |
| 39 | // account. If both timestamp and sequence numbers are identical and one of |
| 40 | // the packets is sync-packet, the regular packet is considered earlier. For |
| 41 | // two regular packets with the same sequence number and timestamp a primary |
| 42 | // payload is considered "smaller" than a secondary. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 43 | bool operator==(const Packet& rhs) const { |
| 44 | return (this->header.timestamp == rhs.header.timestamp && |
| 45 | this->header.sequenceNumber == rhs.header.sequenceNumber && |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 46 | this->primary == rhs.primary && |
| 47 | this->sync_packet == rhs.sync_packet); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 48 | } |
| 49 | bool operator!=(const Packet& rhs) const { return !operator==(rhs); } |
| 50 | bool operator<(const Packet& rhs) const { |
| 51 | if (this->header.timestamp == rhs.header.timestamp) { |
| 52 | if (this->header.sequenceNumber == rhs.header.sequenceNumber) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 53 | // Timestamp and sequence numbers are identical. A sync packet should |
| 54 | // be recognized "larger" (i.e. "later") compared to a "network packet" |
| 55 | // (regular packet from network not sync-packet). If none of the packets |
| 56 | // are sync-packets, then deem the left hand side to be "smaller" |
| 57 | // (i.e., "earlier") if it is primary, and right hand side is not. |
| 58 | // |
| 59 | // The condition on sync packets to be larger than "network packets," |
| 60 | // given same RTP sequence number and timestamp, guarantees that a |
| 61 | // "network packet" to be inserted in an earlier position into |
| 62 | // |packet_buffer_| compared to a sync packet of same timestamp and |
| 63 | // sequence number. |
| 64 | if (rhs.sync_packet) |
| 65 | return true; |
| 66 | if (this->sync_packet) |
| 67 | return false; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 68 | return (this->primary && !rhs.primary); |
| 69 | } |
| 70 | return (static_cast<uint16_t>(rhs.header.sequenceNumber |
| 71 | - this->header.sequenceNumber) < 0xFFFF / 2); |
| 72 | } |
| 73 | return (static_cast<uint32_t>(rhs.header.timestamp |
| 74 | - this->header.timestamp) < 0xFFFFFFFF / 2); |
| 75 | } |
| 76 | bool operator>(const Packet& rhs) const { return rhs.operator<(*this); } |
| 77 | bool operator<=(const Packet& rhs) const { return !operator>(rhs); } |
| 78 | bool operator>=(const Packet& rhs) const { return !operator<(rhs); } |
| 79 | }; |
| 80 | |
| 81 | // A list of packets. |
| 82 | typedef std::list<Packet*> PacketList; |
| 83 | |
| 84 | } // namespace webrtc |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 85 | #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_H_ |