blob: cf590caf4b4b4ad5e8a82346dfaeba2ad77e0efd [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"
ossu61a208b2016-09-20 01:38:00 -070018#include "webrtc/modules/audio_coding/codecs/audio_decoder.h"
henrik.lundin84f8cd62016-04-26 07:45:16 -070019#include "webrtc/modules/audio_coding/neteq/tick_timer.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010020#include "webrtc/modules/include/module_common_types.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000021#include "webrtc/typedefs.h"
22
23namespace webrtc {
24
25// Struct for holding RTP packets.
26struct Packet {
ossua70695a2016-09-22 02:06:28 -070027 struct Priority {
28 Priority() : codec_level(0), red_level(0) {}
29 Priority(int codec_level, int red_level)
30 : codec_level(codec_level), red_level(red_level) {
31 CheckInvariant();
32 }
33
34 int codec_level;
35 int red_level;
36
37 // Priorities are sorted low-to-high, first on the level the codec
38 // prioritizes it, then on the level of RED packet it is; i.e. if it is a
39 // primary or secondary payload of a RED packet. For example: with Opus, an
40 // Fec packet (which the decoder prioritizes lower than a regular packet)
41 // will not be used if there is _any_ RED payload for the same
42 // timeframe. The highest priority packet will have levels {0, 0}. Negative
43 // priorities are not allowed.
44 bool operator<(const Priority& b) const {
45 CheckInvariant();
46 b.CheckInvariant();
47 if (codec_level == b.codec_level)
48 return red_level < b.red_level;
49
50 return codec_level < b.codec_level;
51 }
52 bool operator==(const Priority& b) const {
53 CheckInvariant();
54 b.CheckInvariant();
55 return codec_level == b.codec_level && red_level == b.red_level;
56 }
57 bool operator!=(const Priority& b) const { return !(*this == b); }
58 bool operator>(const Priority& b) const { return b < *this; }
59 bool operator<=(const Priority& b) const { return !(b > *this); }
60 bool operator>=(const Priority& b) const { return !(b < *this); }
61
62 private:
63 void CheckInvariant() const {
64 RTC_DCHECK_GE(codec_level, 0);
65 RTC_DCHECK_GE(red_level, 0);
66 }
67 };
68
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069 RTPHeader header;
henrik.lundin84f8cd62016-04-26 07:45:16 -070070 // Datagram excluding RTP header and header extension.
ossudc431ce2016-08-31 08:51:13 -070071 rtc::Buffer payload;
ossua70695a2016-09-22 02:06:28 -070072 Priority priority;
henrik.lundin84f8cd62016-04-26 07:45:16 -070073 std::unique_ptr<TickTimer::Stopwatch> waiting_time;
ossu61a208b2016-09-20 01:38:00 -070074 std::unique_ptr<AudioDecoder::EncodedAudioFrame> frame;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075
henrik.lundin84f8cd62016-04-26 07:45:16 -070076 Packet();
77 ~Packet();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000078
79 // Comparison operators. Establish a packet ordering based on (1) timestamp,
ossu17e3fa12016-09-08 04:52:55 -070080 // (2) sequence number and (3) redundancy.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000081 // Timestamp and sequence numbers are compared taking wrap-around into
ossu17e3fa12016-09-08 04:52:55 -070082 // account. For two packets with the same sequence number and timestamp a
83 // primary payload is considered "smaller" than a secondary.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000084 bool operator==(const Packet& rhs) const {
85 return (this->header.timestamp == rhs.header.timestamp &&
ossua70695a2016-09-22 02:06:28 -070086 this->header.sequenceNumber == rhs.header.sequenceNumber &&
87 this->priority == rhs.priority);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000088 }
89 bool operator!=(const Packet& rhs) const { return !operator==(rhs); }
90 bool operator<(const Packet& rhs) const {
91 if (this->header.timestamp == rhs.header.timestamp) {
92 if (this->header.sequenceNumber == rhs.header.sequenceNumber) {
ossua70695a2016-09-22 02:06:28 -070093 // Timestamp and sequence numbers are identical - deem the left hand
94 // side to be "smaller" (i.e., "earlier") if it has higher priority.
95 return this->priority < rhs.priority;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000096 }
97 return (static_cast<uint16_t>(rhs.header.sequenceNumber
98 - this->header.sequenceNumber) < 0xFFFF / 2);
99 }
100 return (static_cast<uint32_t>(rhs.header.timestamp
101 - this->header.timestamp) < 0xFFFFFFFF / 2);
102 }
103 bool operator>(const Packet& rhs) const { return rhs.operator<(*this); }
104 bool operator<=(const Packet& rhs) const { return !operator>(rhs); }
105 bool operator>=(const Packet& rhs) const { return !operator<(rhs); }
ossu61a208b2016-09-20 01:38:00 -0700106
107 bool empty() const { return !frame && payload.empty(); }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000108};
109
110// A list of packets.
111typedef std::list<Packet*> PacketList;
112
113} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000114#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_H_