Line data Source code
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 :
11 : #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_H_
12 : #define WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_H_
13 :
14 : #include <list>
15 : #include <memory>
16 :
17 : #include "webrtc/base/buffer.h"
18 : #include "webrtc/modules/audio_coding/codecs/audio_decoder.h"
19 : #include "webrtc/modules/audio_coding/neteq/tick_timer.h"
20 : #include "webrtc/typedefs.h"
21 :
22 : namespace webrtc {
23 :
24 : // Struct for holding RTP packets.
25 0 : struct Packet {
26 : struct Priority {
27 0 : Priority() : codec_level(0), red_level(0) {}
28 0 : Priority(int codec_level, int red_level)
29 0 : : codec_level(codec_level), red_level(red_level) {
30 0 : CheckInvariant();
31 0 : }
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 0 : bool operator<(const Priority& b) const {
44 0 : CheckInvariant();
45 0 : b.CheckInvariant();
46 0 : if (codec_level == b.codec_level)
47 0 : return red_level < b.red_level;
48 :
49 0 : return codec_level < b.codec_level;
50 : }
51 0 : bool operator==(const Priority& b) const {
52 0 : CheckInvariant();
53 0 : b.CheckInvariant();
54 0 : return codec_level == b.codec_level && red_level == b.red_level;
55 : }
56 0 : 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 0 : void CheckInvariant() const {
63 0 : RTC_DCHECK_GE(codec_level, 0);
64 0 : RTC_DCHECK_GE(red_level, 0);
65 0 : }
66 : };
67 :
68 : uint32_t timestamp;
69 : uint16_t sequence_number;
70 : uint8_t payload_type;
71 : // Datagram excluding RTP header and header extension.
72 : rtc::Buffer payload;
73 : Priority priority;
74 : std::unique_ptr<TickTimer::Stopwatch> waiting_time;
75 : std::unique_ptr<AudioDecoder::EncodedAudioFrame> frame;
76 :
77 : Packet();
78 : Packet(Packet&& b);
79 : ~Packet();
80 :
81 : // 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 :
89 : // Comparison operators. Establish a packet ordering based on (1) timestamp,
90 : // (2) sequence number and (3) redundancy.
91 : // Timestamp and sequence numbers are compared taking wrap-around into
92 : // account. For two packets with the same sequence number and timestamp a
93 : // primary payload is considered "smaller" than a secondary.
94 : bool operator==(const Packet& rhs) const {
95 : return (this->timestamp == rhs.timestamp &&
96 : this->sequence_number == rhs.sequence_number &&
97 : this->priority == rhs.priority);
98 : }
99 : bool operator!=(const Packet& rhs) const { return !operator==(rhs); }
100 0 : bool operator<(const Packet& rhs) const {
101 0 : if (this->timestamp == rhs.timestamp) {
102 0 : if (this->sequence_number == rhs.sequence_number) {
103 : // Timestamp and sequence numbers are identical - deem the left hand
104 : // side to be "smaller" (i.e., "earlier") if it has higher priority.
105 0 : return this->priority < rhs.priority;
106 : }
107 0 : return (static_cast<uint16_t>(rhs.sequence_number -
108 0 : this->sequence_number) < 0xFFFF / 2);
109 : }
110 0 : return (static_cast<uint32_t>(rhs.timestamp - this->timestamp) <
111 0 : 0xFFFFFFFF / 2);
112 : }
113 : bool operator>(const Packet& rhs) const { return rhs.operator<(*this); }
114 : bool operator<=(const Packet& rhs) const { return !operator>(rhs); }
115 0 : bool operator>=(const Packet& rhs) const { return !operator<(rhs); }
116 :
117 0 : bool empty() const { return !frame && payload.empty(); }
118 : };
119 :
120 : // A list of packets.
121 : typedef std::list<Packet> PacketList;
122 :
123 : } // namespace webrtc
124 : #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_H_
|