OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_coding/neteq/payload_splitter.h" | 11 #include "webrtc/modules/audio_coding/neteq/red_payload_splitter.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 #include <vector> | |
14 | 15 |
15 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
16 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" |
17 #include "webrtc/modules/audio_coding/neteq/decoder_database.h" | 18 #include "webrtc/modules/audio_coding/neteq/decoder_database.h" |
18 | 19 |
19 namespace webrtc { | 20 namespace webrtc { |
20 | 21 |
21 // The method loops through a list of packets {A, B, C, ...}. Each packet is | 22 // The method loops through a list of packets {A, B, C, ...}. Each packet is |
22 // split into its corresponding RED payloads, {A1, A2, ...}, which is | 23 // split into its corresponding RED payloads, {A1, A2, ...}, which is |
23 // temporarily held in the list |new_packets|. | 24 // temporarily held in the list |new_packets|. |
24 // When the first packet in |packet_list| has been processed, the orignal packet | 25 // When the first packet in |packet_list| has been processed, the orignal packet |
25 // is replaced by the new ones in |new_packets|, so that |packet_list| becomes: | 26 // is replaced by the new ones in |new_packets|, so that |packet_list| becomes: |
26 // {A1, A2, ..., B, C, ...}. The method then continues with B, and C, until all | 27 // {A1, A2, ..., B, C, ...}. The method then continues with B, and C, until all |
27 // the original packets have been replaced by their split payloads. | 28 // the original packets have been replaced by their split payloads. |
28 int PayloadSplitter::SplitRed(PacketList* packet_list) { | 29 bool RedPayloadSplitter::SplitRed(PacketList* packet_list) { |
29 int ret = kOK; | 30 // This could be as high as 256 (to match the number of priority levels) but |
kwiberg-webrtc
2016/09/20 14:56:23
You allow more than 256 priority levels now, right
ossu
2016/09/20 15:45:21
I do. I'll change this to 2147483648. :)
I'll chan
| |
31 // very high values here probably indicate some sort of problem. Having a max | |
32 // value here makes the implementation easier, since we don't need to deal | |
33 // with the (very unexpected) case of more blocks than there are priority | |
34 // levels. | |
35 const size_t kMaxRedBlocks = 32; | |
36 bool ret = true; | |
30 PacketList::iterator it = packet_list->begin(); | 37 PacketList::iterator it = packet_list->begin(); |
31 while (it != packet_list->end()) { | 38 while (it != packet_list->end()) { |
32 const Packet* red_packet = (*it); | 39 const Packet* red_packet = (*it); |
33 assert(!red_packet->payload.empty()); | 40 assert(!red_packet->payload.empty()); |
34 const uint8_t* payload_ptr = red_packet->payload.data(); | 41 const uint8_t* payload_ptr = red_packet->payload.data(); |
35 | 42 |
36 // Read RED headers (according to RFC 2198): | 43 // Read RED headers (according to RFC 2198): |
37 // | 44 // |
38 // 0 1 2 3 | 45 // 0 1 2 3 |
39 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | 46 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
40 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 47 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
41 // |F| block PT | timestamp offset | block length | | 48 // |F| block PT | timestamp offset | block length | |
42 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 49 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
43 // Last RED header: | 50 // Last RED header: |
44 // 0 1 2 3 4 5 6 7 | 51 // 0 1 2 3 4 5 6 7 |
45 // +-+-+-+-+-+-+-+-+ | 52 // +-+-+-+-+-+-+-+-+ |
46 // |0| Block PT | | 53 // |0| Block PT | |
47 // +-+-+-+-+-+-+-+-+ | 54 // +-+-+-+-+-+-+-+-+ |
48 | 55 |
49 struct RedHeader { | 56 struct RedHeader { |
50 uint8_t payload_type; | 57 uint8_t payload_type; |
51 uint32_t timestamp; | 58 uint32_t timestamp; |
52 size_t payload_length; | 59 size_t payload_length; |
53 bool primary; | |
54 }; | 60 }; |
55 | 61 |
56 std::vector<RedHeader> new_headers; | 62 std::vector<RedHeader> new_headers; |
57 bool last_block = false; | 63 bool last_block = false; |
58 size_t sum_length = 0; | 64 size_t sum_length = 0; |
59 while (!last_block) { | 65 while (!last_block) { |
60 RedHeader new_header; | 66 RedHeader new_header; |
61 // Check the F bit. If F == 0, this was the last block. | 67 // Check the F bit. If F == 0, this was the last block. |
62 last_block = ((*payload_ptr & 0x80) == 0); | 68 last_block = ((*payload_ptr & 0x80) == 0); |
63 // Bits 1 through 7 are payload type. | 69 // Bits 1 through 7 are payload type. |
64 new_header.payload_type = payload_ptr[0] & 0x7F; | 70 new_header.payload_type = payload_ptr[0] & 0x7F; |
65 if (last_block) { | 71 if (last_block) { |
66 // No more header data to read. | 72 // No more header data to read. |
67 ++sum_length; // Account for RED header size of 1 byte. | 73 ++sum_length; // Account for RED header size of 1 byte. |
68 new_header.timestamp = red_packet->header.timestamp; | 74 new_header.timestamp = red_packet->header.timestamp; |
69 new_header.payload_length = red_packet->payload.size() - sum_length; | 75 new_header.payload_length = red_packet->payload.size() - sum_length; |
70 new_header.primary = true; // Last block is always primary. | |
71 payload_ptr += 1; // Advance to first payload byte. | 76 payload_ptr += 1; // Advance to first payload byte. |
72 } else { | 77 } else { |
73 // Bits 8 through 21 are timestamp offset. | 78 // Bits 8 through 21 are timestamp offset. |
74 int timestamp_offset = (payload_ptr[1] << 6) + | 79 int timestamp_offset = |
75 ((payload_ptr[2] & 0xFC) >> 2); | 80 (payload_ptr[1] << 6) + ((payload_ptr[2] & 0xFC) >> 2); |
76 new_header.timestamp = red_packet->header.timestamp - timestamp_offset; | 81 new_header.timestamp = red_packet->header.timestamp - timestamp_offset; |
77 // Bits 22 through 31 are payload length. | 82 // Bits 22 through 31 are payload length. |
78 new_header.payload_length = | 83 new_header.payload_length = |
79 ((payload_ptr[2] & 0x03) << 8) + payload_ptr[3]; | 84 ((payload_ptr[2] & 0x03) << 8) + payload_ptr[3]; |
80 new_header.primary = false; | |
81 payload_ptr += 4; // Advance to next RED header. | 85 payload_ptr += 4; // Advance to next RED header. |
82 } | 86 } |
83 sum_length += new_header.payload_length; | 87 sum_length += new_header.payload_length; |
84 sum_length += 4; // Account for RED header size of 4 bytes. | 88 sum_length += 4; // Account for RED header size of 4 bytes. |
85 // Store in new list of packets. | 89 // Store in new list of packets. |
86 new_headers.push_back(new_header); | 90 new_headers.push_back(new_header); |
87 } | 91 } |
88 | 92 |
89 // Populate the new packets with payload data. | 93 if (new_headers.size() <= kMaxRedBlocks) { |
90 // |payload_ptr| now points at the first payload byte. | 94 // Populate the new packets with payload data. |
91 PacketList new_packets; // An empty list to store the split packets in. | 95 // |payload_ptr| now points at the first payload byte. |
92 for (const auto& new_header : new_headers) { | 96 PacketList new_packets; // An empty list to store the split packets in. |
93 size_t payload_length = new_header.payload_length; | 97 for (size_t i = 0; i != new_headers.size(); ++i) { |
94 if (payload_ptr + payload_length > | 98 const auto& new_header = new_headers[i]; |
95 red_packet->payload.data() + red_packet->payload.size()) { | 99 size_t payload_length = new_header.payload_length; |
96 // The block lengths in the RED headers do not match the overall packet | 100 if (payload_ptr + payload_length > |
97 // length. Something is corrupt. Discard this and the remaining | 101 red_packet->payload.data() + red_packet->payload.size()) { |
98 // payloads from this packet. | 102 // The block lengths in the RED headers do not match the overall |
99 LOG(LS_WARNING) << "SplitRed length mismatch"; | 103 // packet length. Something is corrupt. Discard this and the remaining |
100 ret = kRedLengthMismatch; | 104 // payloads from this packet. |
101 break; | 105 LOG(LS_WARNING) << "SplitRed length mismatch"; |
106 ret = false; | |
107 break; | |
108 } | |
109 | |
110 Packet* new_packet = new Packet; | |
111 new_packet->header = red_packet->header; | |
112 new_packet->header.timestamp = new_header.timestamp; | |
113 new_packet->header.payloadType = new_header.payload_type; | |
114 new_packet->priority.red_level = | |
115 static_cast<uint8_t>((new_headers.size() - 1) - i); | |
kwiberg-webrtc
2016/09/20 14:56:23
Cast to int? And use checked_cast?
ossu
2016/09/20 15:45:21
D'oh!
| |
116 new_packet->payload.SetData(payload_ptr, payload_length); | |
117 new_packets.push_front(new_packet); | |
118 payload_ptr += payload_length; | |
102 } | 119 } |
103 Packet* new_packet = new Packet; | 120 // Insert new packets into original list, before the element pointed to by |
104 new_packet->header = red_packet->header; | 121 // iterator |it|. |
105 new_packet->header.timestamp = new_header.timestamp; | 122 packet_list->splice(it, new_packets, new_packets.begin(), |
106 new_packet->header.payloadType = new_header.payload_type; | 123 new_packets.end()); |
107 new_packet->primary = new_header.primary; | 124 } else { |
108 new_packet->payload.SetData(payload_ptr, payload_length); | 125 LOG(LS_WARNING) << "SplitRed too many blocks: " << new_headers.size(); |
109 new_packets.push_front(new_packet); | 126 ret = false; |
110 payload_ptr += payload_length; | |
111 } | 127 } |
112 // Insert new packets into original list, before the element pointed to by | |
113 // iterator |it|. | |
114 packet_list->splice(it, new_packets, new_packets.begin(), | |
115 new_packets.end()); | |
116 // Delete old packet payload. | 128 // Delete old packet payload. |
117 delete (*it); | 129 delete (*it); |
118 // Remove |it| from the packet list. This operation effectively moves the | 130 // Remove |it| from the packet list. This operation effectively moves the |
119 // iterator |it| to the next packet in the list. Thus, we do not have to | 131 // iterator |it| to the next packet in the list. Thus, we do not have to |
120 // increment it manually. | 132 // increment it manually. |
121 it = packet_list->erase(it); | 133 it = packet_list->erase(it); |
122 } | 134 } |
123 return ret; | 135 return ret; |
124 } | 136 } |
125 | 137 |
126 int PayloadSplitter::SplitFec(PacketList* packet_list, | 138 int RedPayloadSplitter::CheckRedPayloads( |
127 DecoderDatabase* decoder_database) { | 139 PacketList* packet_list, |
128 PacketList::iterator it = packet_list->begin(); | 140 const DecoderDatabase& decoder_database) { |
129 // Iterate through all packets in |packet_list|. | |
130 while (it != packet_list->end()) { | |
131 Packet* packet = (*it); // Just to make the notation more intuitive. | |
132 // Get codec type for this payload. | |
133 uint8_t payload_type = packet->header.payloadType; | |
134 const DecoderDatabase::DecoderInfo* info = | |
135 decoder_database->GetDecoderInfo(payload_type); | |
136 if (!info) { | |
137 LOG(LS_WARNING) << "SplitFec unknown payload type"; | |
138 return kUnknownPayloadType; | |
139 } | |
140 | |
141 // Not an FEC packet. | |
142 AudioDecoder* decoder = decoder_database->GetDecoder(payload_type); | |
143 // decoder should not return NULL, except for comfort noise payloads which | |
144 // are handled separately. | |
145 assert(decoder != NULL || decoder_database->IsComfortNoise(payload_type)); | |
146 if (!decoder || | |
147 !decoder->PacketHasFec(packet->payload.data(), | |
148 packet->payload.size())) { | |
149 ++it; | |
150 continue; | |
151 } | |
152 | |
153 switch (info->codec_type) { | |
154 case NetEqDecoder::kDecoderOpus: | |
155 case NetEqDecoder::kDecoderOpus_2ch: { | |
156 // The main payload of this packet should be decoded as a primary | |
157 // payload, even if it comes as a secondary payload in a RED packet. | |
158 packet->primary = true; | |
159 | |
160 Packet* new_packet = new Packet; | |
161 new_packet->header = packet->header; | |
162 int duration = decoder->PacketDurationRedundant(packet->payload.data(), | |
163 packet->payload.size()); | |
164 new_packet->header.timestamp -= duration; | |
165 new_packet->payload.SetData(packet->payload); | |
166 new_packet->primary = false; | |
167 // Waiting time should not be set here. | |
168 RTC_DCHECK(!packet->waiting_time); | |
169 | |
170 packet_list->insert(it, new_packet); | |
171 break; | |
172 } | |
173 default: { | |
174 LOG(LS_WARNING) << "SplitFec wrong payload type"; | |
175 return kFecSplitError; | |
176 } | |
177 } | |
178 | |
179 ++it; | |
180 } | |
181 return kOK; | |
182 } | |
183 | |
184 int PayloadSplitter::CheckRedPayloads(PacketList* packet_list, | |
185 const DecoderDatabase& decoder_database) { | |
186 PacketList::iterator it = packet_list->begin(); | 141 PacketList::iterator it = packet_list->begin(); |
187 int main_payload_type = -1; | 142 int main_payload_type = -1; |
188 int num_deleted_packets = 0; | 143 int num_deleted_packets = 0; |
189 while (it != packet_list->end()) { | 144 while (it != packet_list->end()) { |
190 uint8_t this_payload_type = (*it)->header.payloadType; | 145 uint8_t this_payload_type = (*it)->header.payloadType; |
191 if (!decoder_database.IsDtmf(this_payload_type) && | 146 if (!decoder_database.IsDtmf(this_payload_type) && |
192 !decoder_database.IsComfortNoise(this_payload_type)) { | 147 !decoder_database.IsComfortNoise(this_payload_type)) { |
193 if (main_payload_type == -1) { | 148 if (main_payload_type == -1) { |
194 // This is the first packet in the list which is non-DTMF non-CNG. | 149 // This is the first packet in the list which is non-DTMF non-CNG. |
195 main_payload_type = this_payload_type; | 150 main_payload_type = this_payload_type; |
(...skipping 10 matching lines...) Expand all Loading... | |
206 continue; | 161 continue; |
207 } | 162 } |
208 } | 163 } |
209 } | 164 } |
210 ++it; | 165 ++it; |
211 } | 166 } |
212 return num_deleted_packets; | 167 return num_deleted_packets; |
213 } | 168 } |
214 | 169 |
215 } // namespace webrtc | 170 } // namespace webrtc |
OLD | NEW |