blob: 61b014429ab2df192a27673c73c5dd25164b3c8c [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_PACKET_H_
12#define 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/audio_decoder.h"
18#include "modules/audio_coding/neteq/tick_timer.h"
19#include "rtc_base/buffer.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020020#include "typedefs.h" // NOLINT(build/include)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000021
22namespace webrtc {
23
24// Struct for holding RTP packets.
25struct Packet {
ossua70695a2016-09-22 02:06:28 -070026 struct Priority {
27 Priority() : codec_level(0), red_level(0) {}
28 Priority(int codec_level, int red_level)
29 : codec_level(codec_level), red_level(red_level) {
30 CheckInvariant();
31 }
32
33 int codec_level;
34 int red_level;
35
36 // Priorities are sorted low-to-high, first on the level the codec
37 // prioritizes it, then on the level of RED packet it is; i.e. if it is a
38 // primary or secondary payload of a RED packet. For example: with Opus, an
39 // Fec packet (which the decoder prioritizes lower than a regular packet)
40 // will not be used if there is _any_ RED payload for the same
41 // timeframe. The highest priority packet will have levels {0, 0}. Negative
42 // priorities are not allowed.
43 bool operator<(const Priority& b) const {
44 CheckInvariant();
45 b.CheckInvariant();
46 if (codec_level == b.codec_level)
47 return red_level < b.red_level;
48
49 return codec_level < b.codec_level;
50 }
51 bool operator==(const Priority& b) const {
52 CheckInvariant();
53 b.CheckInvariant();
54 return codec_level == b.codec_level && red_level == b.red_level;
55 }
56 bool operator!=(const Priority& b) const { return !(*this == b); }
57 bool operator>(const Priority& b) const { return b < *this; }
58 bool operator<=(const Priority& b) const { return !(b > *this); }
59 bool operator>=(const Priority& b) const { return !(b < *this); }
60
61 private:
62 void CheckInvariant() const {
63 RTC_DCHECK_GE(codec_level, 0);
64 RTC_DCHECK_GE(red_level, 0);
65 }
66 };
67
ossu7a377612016-10-18 04:06:13 -070068 uint32_t timestamp;
69 uint16_t sequence_number;
70 uint8_t payload_type;
henrik.lundin84f8cd62016-04-26 07:45:16 -070071 // Datagram excluding RTP header and header extension.
ossudc431ce2016-08-31 08:51:13 -070072 rtc::Buffer payload;
ossua70695a2016-09-22 02:06:28 -070073 Priority priority;
henrik.lundin84f8cd62016-04-26 07:45:16 -070074 std::unique_ptr<TickTimer::Stopwatch> waiting_time;
ossu61a208b2016-09-20 01:38:00 -070075 std::unique_ptr<AudioDecoder::EncodedAudioFrame> frame;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000076
henrik.lundin84f8cd62016-04-26 07:45:16 -070077 Packet();
ossua73f6c92016-10-24 08:25:28 -070078 Packet(Packet&& b);
henrik.lundin84f8cd62016-04-26 07:45:16 -070079 ~Packet();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080
ossua73f6c92016-10-24 08:25:28 -070081 // Packets should generally be moved around but sometimes it's useful to make
82 // a copy, for example for testing purposes. NOTE: Will only work for
83 // un-parsed packets, i.e. |frame| must be unset. The payload will, however,
84 // be copied. |waiting_time| will also not be copied.
85 Packet Clone() const;
86
87 Packet& operator=(Packet&& b);
88
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089 // Comparison operators. Establish a packet ordering based on (1) timestamp,
ossu17e3fa12016-09-08 04:52:55 -070090 // (2) sequence number and (3) redundancy.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000091 // Timestamp and sequence numbers are compared taking wrap-around into
ossu17e3fa12016-09-08 04:52:55 -070092 // account. For two packets with the same sequence number and timestamp a
93 // primary payload is considered "smaller" than a secondary.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000094 bool operator==(const Packet& rhs) const {
ossu7a377612016-10-18 04:06:13 -070095 return (this->timestamp == rhs.timestamp &&
96 this->sequence_number == rhs.sequence_number &&
ossua70695a2016-09-22 02:06:28 -070097 this->priority == rhs.priority);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000098 }
99 bool operator!=(const Packet& rhs) const { return !operator==(rhs); }
100 bool operator<(const Packet& rhs) const {
ossu7a377612016-10-18 04:06:13 -0700101 if (this->timestamp == rhs.timestamp) {
102 if (this->sequence_number == rhs.sequence_number) {
ossua70695a2016-09-22 02:06:28 -0700103 // Timestamp and sequence numbers are identical - deem the left hand
104 // side to be "smaller" (i.e., "earlier") if it has higher priority.
105 return this->priority < rhs.priority;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000106 }
ossu7a377612016-10-18 04:06:13 -0700107 return (static_cast<uint16_t>(rhs.sequence_number -
108 this->sequence_number) < 0xFFFF / 2);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000109 }
ossu7a377612016-10-18 04:06:13 -0700110 return (static_cast<uint32_t>(rhs.timestamp - this->timestamp) <
111 0xFFFFFFFF / 2);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000112 }
113 bool operator>(const Packet& rhs) const { return rhs.operator<(*this); }
114 bool operator<=(const Packet& rhs) const { return !operator>(rhs); }
115 bool operator>=(const Packet& rhs) const { return !operator<(rhs); }
ossu61a208b2016-09-20 01:38:00 -0700116
117 bool empty() const { return !frame && payload.empty(); }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000118};
119
120// A list of packets.
ossua73f6c92016-10-24 08:25:28 -0700121typedef std::list<Packet> PacketList;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000122
123} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200124#endif // MODULES_AUDIO_CODING_NETEQ_PACKET_H_