blob: 273c2d21c03a3666fee77e00d1b8201b7a64075f [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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.org9c55f0f2014-06-09 08:10:28 +000011#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
14#include <list>
henrik.lundin84f8cd62016-04-26 07:45:16 -070015#include <memory>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
ossudc431ce2016-08-31 08:51:13 -070017#include "webrtc/base/buffer.h"
henrik.lundin84f8cd62016-04-26 07:45:16 -070018#include "webrtc/modules/audio_coding/neteq/tick_timer.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010019#include "webrtc/modules/include/module_common_types.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020#include "webrtc/typedefs.h"
21
22namespace webrtc {
23
24// Struct for holding RTP packets.
25struct Packet {
26 RTPHeader header;
henrik.lundin84f8cd62016-04-26 07:45:16 -070027 // Datagram excluding RTP header and header extension.
ossudc431ce2016-08-31 08:51:13 -070028 rtc::Buffer payload;
henrik.lundin84f8cd62016-04-26 07:45:16 -070029 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.orgd94659d2013-01-29 12:09:21 +000032
henrik.lundin84f8cd62016-04-26 07:45:16 -070033 Packet();
34 ~Packet();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035
36 // Comparison operators. Establish a packet ordering based on (1) timestamp,
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000037 // (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.orgd94659d2013-01-29 12:09:21 +000043 bool operator==(const Packet& rhs) const {
44 return (this->header.timestamp == rhs.header.timestamp &&
45 this->header.sequenceNumber == rhs.header.sequenceNumber &&
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000046 this->primary == rhs.primary &&
47 this->sync_packet == rhs.sync_packet);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000048 }
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.org7b75ac62013-09-26 00:27:56 +000053 // 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.orgd94659d2013-01-29 12:09:21 +000068 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.
82typedef std::list<Packet*> PacketList;
83
84} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000085#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_H_