blob: 358d8fa1abb8f8b9e097fe175545ba869ef1fdfc [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
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000015#include <list>
henrik.lundin84f8cd62016-04-26 07:45:16 -070016#include <memory>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/audio_codecs/audio_decoder.h"
19#include "modules/audio_coding/neteq/tick_timer.h"
20#include "rtc_base/buffer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020021#include "rtc_base/checks.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000022
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
ossu7a377612016-10-18 04:06:13 -070069 uint32_t timestamp;
70 uint16_t sequence_number;
71 uint8_t payload_type;
henrik.lundin84f8cd62016-04-26 07:45:16 -070072 // Datagram excluding RTP header and header extension.
ossudc431ce2016-08-31 08:51:13 -070073 rtc::Buffer payload;
ossua70695a2016-09-22 02:06:28 -070074 Priority priority;
henrik.lundin84f8cd62016-04-26 07:45:16 -070075 std::unique_ptr<TickTimer::Stopwatch> waiting_time;
ossu61a208b2016-09-20 01:38:00 -070076 std::unique_ptr<AudioDecoder::EncodedAudioFrame> frame;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000077
henrik.lundin84f8cd62016-04-26 07:45:16 -070078 Packet();
ossua73f6c92016-10-24 08:25:28 -070079 Packet(Packet&& b);
henrik.lundin84f8cd62016-04-26 07:45:16 -070080 ~Packet();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081
ossua73f6c92016-10-24 08:25:28 -070082 // Packets should generally be moved around but sometimes it's useful to make
83 // a copy, for example for testing purposes. NOTE: Will only work for
84 // un-parsed packets, i.e. |frame| must be unset. The payload will, however,
85 // be copied. |waiting_time| will also not be copied.
86 Packet Clone() const;
87
88 Packet& operator=(Packet&& b);
89
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000090 // Comparison operators. Establish a packet ordering based on (1) timestamp,
ossu17e3fa12016-09-08 04:52:55 -070091 // (2) sequence number and (3) redundancy.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000092 // Timestamp and sequence numbers are compared taking wrap-around into
ossu17e3fa12016-09-08 04:52:55 -070093 // account. For two packets with the same sequence number and timestamp a
94 // primary payload is considered "smaller" than a secondary.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000095 bool operator==(const Packet& rhs) const {
ossu7a377612016-10-18 04:06:13 -070096 return (this->timestamp == rhs.timestamp &&
97 this->sequence_number == rhs.sequence_number &&
ossua70695a2016-09-22 02:06:28 -070098 this->priority == rhs.priority);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000099 }
100 bool operator!=(const Packet& rhs) const { return !operator==(rhs); }
101 bool operator<(const Packet& rhs) const {
ossu7a377612016-10-18 04:06:13 -0700102 if (this->timestamp == rhs.timestamp) {
103 if (this->sequence_number == rhs.sequence_number) {
ossua70695a2016-09-22 02:06:28 -0700104 // Timestamp and sequence numbers are identical - deem the left hand
105 // side to be "smaller" (i.e., "earlier") if it has higher priority.
106 return this->priority < rhs.priority;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000107 }
ossu7a377612016-10-18 04:06:13 -0700108 return (static_cast<uint16_t>(rhs.sequence_number -
109 this->sequence_number) < 0xFFFF / 2);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000110 }
ossu7a377612016-10-18 04:06:13 -0700111 return (static_cast<uint32_t>(rhs.timestamp - this->timestamp) <
112 0xFFFFFFFF / 2);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113 }
114 bool operator>(const Packet& rhs) const { return rhs.operator<(*this); }
115 bool operator<=(const Packet& rhs) const { return !operator>(rhs); }
116 bool operator>=(const Packet& rhs) const { return !operator<(rhs); }
ossu61a208b2016-09-20 01:38:00 -0700117
118 bool empty() const { return !frame && payload.empty(); }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000119};
120
121// A list of packets.
ossua73f6c92016-10-24 08:25:28 -0700122typedef std::list<Packet> PacketList;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000123
124} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200125#endif // MODULES_AUDIO_CODING_NETEQ_PACKET_H_