henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_coding/neteq/payload_splitter.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 15 | #include "webrtc/base/logging.h" |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 16 | #include "webrtc/modules/audio_coding/neteq/decoder_database.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | // The method loops through a list of packets {A, B, C, ...}. Each packet is |
| 21 | // split into its corresponding RED payloads, {A1, A2, ...}, which is |
| 22 | // temporarily held in the list |new_packets|. |
| 23 | // When the first packet in |packet_list| has been processed, the orignal packet |
| 24 | // is replaced by the new ones in |new_packets|, so that |packet_list| becomes: |
| 25 | // {A1, A2, ..., B, C, ...}. The method then continues with B, and C, until all |
| 26 | // the original packets have been replaced by their split payloads. |
| 27 | int PayloadSplitter::SplitRed(PacketList* packet_list) { |
| 28 | int ret = kOK; |
| 29 | PacketList::iterator it = packet_list->begin(); |
| 30 | while (it != packet_list->end()) { |
| 31 | PacketList new_packets; // An empty list to store the split packets in. |
| 32 | Packet* red_packet = (*it); |
| 33 | assert(red_packet->payload); |
| 34 | uint8_t* payload_ptr = red_packet->payload; |
| 35 | |
| 36 | // Read RED headers (according to RFC 2198): |
| 37 | // |
| 38 | // 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 |
| 40 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | // |F| block PT | timestamp offset | block length | |
| 42 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 43 | // Last RED header: |
| 44 | // 0 1 2 3 4 5 6 7 |
| 45 | // +-+-+-+-+-+-+-+-+ |
| 46 | // |0| Block PT | |
| 47 | // +-+-+-+-+-+-+-+-+ |
| 48 | |
| 49 | bool last_block = false; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 50 | size_t sum_length = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 51 | while (!last_block) { |
| 52 | Packet* new_packet = new Packet; |
| 53 | new_packet->header = red_packet->header; |
| 54 | // Check the F bit. If F == 0, this was the last block. |
| 55 | last_block = ((*payload_ptr & 0x80) == 0); |
| 56 | // Bits 1 through 7 are payload type. |
| 57 | new_packet->header.payloadType = payload_ptr[0] & 0x7F; |
| 58 | if (last_block) { |
| 59 | // No more header data to read. |
| 60 | ++sum_length; // Account for RED header size of 1 byte. |
| 61 | new_packet->payload_length = red_packet->payload_length - sum_length; |
| 62 | new_packet->primary = true; // Last block is always primary. |
| 63 | payload_ptr += 1; // Advance to first payload byte. |
| 64 | } else { |
| 65 | // Bits 8 through 21 are timestamp offset. |
| 66 | int timestamp_offset = (payload_ptr[1] << 6) + |
| 67 | ((payload_ptr[2] & 0xFC) >> 2); |
| 68 | new_packet->header.timestamp = red_packet->header.timestamp - |
| 69 | timestamp_offset; |
| 70 | // Bits 22 through 31 are payload length. |
| 71 | new_packet->payload_length = ((payload_ptr[2] & 0x03) << 8) + |
| 72 | payload_ptr[3]; |
| 73 | new_packet->primary = false; |
| 74 | payload_ptr += 4; // Advance to next RED header. |
| 75 | } |
| 76 | sum_length += new_packet->payload_length; |
| 77 | sum_length += 4; // Account for RED header size of 4 bytes. |
| 78 | // Store in new list of packets. |
| 79 | new_packets.push_back(new_packet); |
| 80 | } |
| 81 | |
| 82 | // Populate the new packets with payload data. |
| 83 | // |payload_ptr| now points at the first payload byte. |
| 84 | PacketList::iterator new_it; |
| 85 | for (new_it = new_packets.begin(); new_it != new_packets.end(); ++new_it) { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 86 | size_t payload_length = (*new_it)->payload_length; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 87 | if (payload_ptr + payload_length > |
| 88 | red_packet->payload + red_packet->payload_length) { |
| 89 | // The block lengths in the RED headers do not match the overall packet |
| 90 | // length. Something is corrupt. Discard this and the remaining |
| 91 | // payloads from this packet. |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 92 | LOG(LS_WARNING) << "SplitRed length mismatch"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 93 | while (new_it != new_packets.end()) { |
| 94 | // Payload should not have been allocated yet. |
| 95 | assert(!(*new_it)->payload); |
| 96 | delete (*new_it); |
| 97 | new_it = new_packets.erase(new_it); |
| 98 | } |
| 99 | ret = kRedLengthMismatch; |
| 100 | break; |
| 101 | } |
| 102 | (*new_it)->payload = new uint8_t[payload_length]; |
| 103 | memcpy((*new_it)->payload, payload_ptr, payload_length); |
| 104 | payload_ptr += payload_length; |
| 105 | } |
| 106 | // Reverse the order of the new packets, so that the primary payload is |
| 107 | // always first. |
| 108 | new_packets.reverse(); |
| 109 | // Insert new packets into original list, before the element pointed to by |
| 110 | // iterator |it|. |
| 111 | packet_list->splice(it, new_packets, new_packets.begin(), |
| 112 | new_packets.end()); |
| 113 | // Delete old packet payload. |
| 114 | delete [] (*it)->payload; |
| 115 | delete (*it); |
| 116 | // Remove |it| from the packet list. This operation effectively moves the |
| 117 | // iterator |it| to the next packet in the list. Thus, we do not have to |
| 118 | // increment it manually. |
| 119 | it = packet_list->erase(it); |
| 120 | } |
| 121 | return ret; |
| 122 | } |
| 123 | |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 124 | int PayloadSplitter::SplitFec(PacketList* packet_list, |
| 125 | DecoderDatabase* decoder_database) { |
| 126 | PacketList::iterator it = packet_list->begin(); |
| 127 | // Iterate through all packets in |packet_list|. |
| 128 | while (it != packet_list->end()) { |
| 129 | Packet* packet = (*it); // Just to make the notation more intuitive. |
| 130 | // Get codec type for this payload. |
| 131 | uint8_t payload_type = packet->header.payloadType; |
| 132 | const DecoderDatabase::DecoderInfo* info = |
| 133 | decoder_database->GetDecoderInfo(payload_type); |
| 134 | if (!info) { |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 135 | LOG(LS_WARNING) << "SplitFec unknown payload type"; |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 136 | return kUnknownPayloadType; |
| 137 | } |
| 138 | // No splitting for a sync-packet. |
| 139 | if (packet->sync_packet) { |
| 140 | ++it; |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | // Not an FEC packet. |
| 145 | AudioDecoder* decoder = decoder_database->GetDecoder(payload_type); |
minyue@webrtc.org | 7549ff4 | 2014-04-02 15:03:01 +0000 | [diff] [blame] | 146 | // decoder should not return NULL. |
| 147 | assert(decoder != NULL); |
| 148 | if (!decoder || |
| 149 | !decoder->PacketHasFec(packet->payload, packet->payload_length)) { |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 150 | ++it; |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | switch (info->codec_type) { |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 155 | case NetEqDecoder::kDecoderOpus: |
| 156 | case NetEqDecoder::kDecoderOpus_2ch: { |
minyue@webrtc.org | a8cc344 | 2015-02-13 14:01:54 +0000 | [diff] [blame] | 157 | // The main payload of this packet should be decoded as a primary |
| 158 | // payload, even if it comes as a secondary payload in a RED packet. |
| 159 | packet->primary = true; |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 160 | |
minyue@webrtc.org | a8cc344 | 2015-02-13 14:01:54 +0000 | [diff] [blame] | 161 | Packet* new_packet = new Packet; |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 162 | new_packet->header = packet->header; |
| 163 | int duration = decoder-> |
minyue@webrtc.org | 7549ff4 | 2014-04-02 15:03:01 +0000 | [diff] [blame] | 164 | PacketDurationRedundant(packet->payload, packet->payload_length); |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 165 | new_packet->header.timestamp -= duration; |
| 166 | new_packet->payload = new uint8_t[packet->payload_length]; |
| 167 | memcpy(new_packet->payload, packet->payload, packet->payload_length); |
| 168 | new_packet->payload_length = packet->payload_length; |
| 169 | new_packet->primary = false; |
| 170 | new_packet->waiting_time = packet->waiting_time; |
| 171 | new_packet->sync_packet = packet->sync_packet; |
| 172 | |
| 173 | packet_list->insert(it, new_packet); |
| 174 | break; |
| 175 | } |
| 176 | default: { |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 177 | LOG(LS_WARNING) << "SplitFec wrong payload type"; |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 178 | return kFecSplitError; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | ++it; |
| 183 | } |
| 184 | return kOK; |
| 185 | } |
| 186 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 187 | int PayloadSplitter::CheckRedPayloads(PacketList* packet_list, |
| 188 | const DecoderDatabase& decoder_database) { |
| 189 | PacketList::iterator it = packet_list->begin(); |
| 190 | int main_payload_type = -1; |
| 191 | int num_deleted_packets = 0; |
| 192 | while (it != packet_list->end()) { |
| 193 | uint8_t this_payload_type = (*it)->header.payloadType; |
| 194 | if (!decoder_database.IsDtmf(this_payload_type) && |
| 195 | !decoder_database.IsComfortNoise(this_payload_type)) { |
| 196 | if (main_payload_type == -1) { |
| 197 | // This is the first packet in the list which is non-DTMF non-CNG. |
| 198 | main_payload_type = this_payload_type; |
| 199 | } else { |
| 200 | if (this_payload_type != main_payload_type) { |
| 201 | // We do not allow redundant payloads of a different type. |
| 202 | // Discard this payload. |
| 203 | delete [] (*it)->payload; |
| 204 | delete (*it); |
| 205 | // Remove |it| from the packet list. This operation effectively |
| 206 | // moves the iterator |it| to the next packet in the list. Thus, we |
| 207 | // do not have to increment it manually. |
| 208 | it = packet_list->erase(it); |
| 209 | ++num_deleted_packets; |
| 210 | continue; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | ++it; |
| 215 | } |
| 216 | return num_deleted_packets; |
| 217 | } |
| 218 | |
| 219 | int PayloadSplitter::SplitAudio(PacketList* packet_list, |
| 220 | const DecoderDatabase& decoder_database) { |
| 221 | PacketList::iterator it = packet_list->begin(); |
| 222 | // Iterate through all packets in |packet_list|. |
| 223 | while (it != packet_list->end()) { |
| 224 | Packet* packet = (*it); // Just to make the notation more intuitive. |
| 225 | // Get codec type for this payload. |
| 226 | const DecoderDatabase::DecoderInfo* info = |
| 227 | decoder_database.GetDecoderInfo(packet->header.payloadType); |
| 228 | if (!info) { |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 229 | LOG(LS_WARNING) << "SplitAudio unknown payload type"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 230 | return kUnknownPayloadType; |
| 231 | } |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 232 | // No splitting for a sync-packet. |
| 233 | if (packet->sync_packet) { |
| 234 | ++it; |
| 235 | continue; |
| 236 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 237 | PacketList new_packets; |
| 238 | switch (info->codec_type) { |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 239 | case NetEqDecoder::kDecoderPCMu: |
| 240 | case NetEqDecoder::kDecoderPCMa: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 241 | // 8 bytes per ms; 8 timestamps per ms. |
| 242 | SplitBySamples(packet, 8, 8, &new_packets); |
| 243 | break; |
| 244 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 245 | case NetEqDecoder::kDecoderPCMu_2ch: |
| 246 | case NetEqDecoder::kDecoderPCMa_2ch: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 247 | // 2 * 8 bytes per ms; 8 timestamps per ms. |
| 248 | SplitBySamples(packet, 2 * 8, 8, &new_packets); |
| 249 | break; |
| 250 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 251 | case NetEqDecoder::kDecoderG722: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 252 | // 8 bytes per ms; 16 timestamps per ms. |
| 253 | SplitBySamples(packet, 8, 16, &new_packets); |
| 254 | break; |
| 255 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 256 | case NetEqDecoder::kDecoderPCM16B: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 257 | // 16 bytes per ms; 8 timestamps per ms. |
| 258 | SplitBySamples(packet, 16, 8, &new_packets); |
| 259 | break; |
| 260 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 261 | case NetEqDecoder::kDecoderPCM16Bwb: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 262 | // 32 bytes per ms; 16 timestamps per ms. |
| 263 | SplitBySamples(packet, 32, 16, &new_packets); |
| 264 | break; |
| 265 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 266 | case NetEqDecoder::kDecoderPCM16Bswb32kHz: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 267 | // 64 bytes per ms; 32 timestamps per ms. |
| 268 | SplitBySamples(packet, 64, 32, &new_packets); |
| 269 | break; |
| 270 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 271 | case NetEqDecoder::kDecoderPCM16Bswb48kHz: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 272 | // 96 bytes per ms; 48 timestamps per ms. |
| 273 | SplitBySamples(packet, 96, 48, &new_packets); |
| 274 | break; |
| 275 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 276 | case NetEqDecoder::kDecoderPCM16B_2ch: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 277 | // 2 * 16 bytes per ms; 8 timestamps per ms. |
| 278 | SplitBySamples(packet, 2 * 16, 8, &new_packets); |
| 279 | break; |
| 280 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 281 | case NetEqDecoder::kDecoderPCM16Bwb_2ch: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 282 | // 2 * 32 bytes per ms; 16 timestamps per ms. |
| 283 | SplitBySamples(packet, 2 * 32, 16, &new_packets); |
| 284 | break; |
| 285 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 286 | case NetEqDecoder::kDecoderPCM16Bswb32kHz_2ch: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 287 | // 2 * 64 bytes per ms; 32 timestamps per ms. |
| 288 | SplitBySamples(packet, 2 * 64, 32, &new_packets); |
| 289 | break; |
| 290 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 291 | case NetEqDecoder::kDecoderPCM16Bswb48kHz_2ch: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 292 | // 2 * 96 bytes per ms; 48 timestamps per ms. |
| 293 | SplitBySamples(packet, 2 * 96, 48, &new_packets); |
| 294 | break; |
| 295 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 296 | case NetEqDecoder::kDecoderPCM16B_5ch: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 297 | // 5 * 16 bytes per ms; 8 timestamps per ms. |
| 298 | SplitBySamples(packet, 5 * 16, 8, &new_packets); |
| 299 | break; |
| 300 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 301 | case NetEqDecoder::kDecoderILBC: { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 302 | size_t bytes_per_frame; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 303 | int timestamps_per_frame; |
| 304 | if (packet->payload_length >= 950) { |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 305 | LOG(LS_WARNING) << "SplitAudio too large iLBC payload"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 306 | return kTooLargePayload; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 307 | } |
| 308 | if (packet->payload_length % 38 == 0) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 309 | // 20 ms frames. |
| 310 | bytes_per_frame = 38; |
| 311 | timestamps_per_frame = 160; |
| 312 | } else if (packet->payload_length % 50 == 0) { |
| 313 | // 30 ms frames. |
| 314 | bytes_per_frame = 50; |
| 315 | timestamps_per_frame = 240; |
| 316 | } else { |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 317 | LOG(LS_WARNING) << "SplitAudio invalid iLBC payload"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 318 | return kFrameSplitError; |
| 319 | } |
| 320 | int ret = SplitByFrames(packet, bytes_per_frame, timestamps_per_frame, |
| 321 | &new_packets); |
| 322 | if (ret < 0) { |
| 323 | return ret; |
| 324 | } else if (ret == kNoSplit) { |
| 325 | // Do not split at all. Simply advance to the next packet in the list. |
| 326 | ++it; |
| 327 | // We do not have any new packets to insert, and should not delete the |
| 328 | // old one. Skip the code after the switch case, and jump straight to |
| 329 | // the next packet in the while loop. |
| 330 | continue; |
| 331 | } |
| 332 | break; |
| 333 | } |
| 334 | default: { |
| 335 | // Do not split at all. Simply advance to the next packet in the list. |
| 336 | ++it; |
| 337 | // We do not have any new packets to insert, and should not delete the |
| 338 | // old one. Skip the code after the switch case, and jump straight to |
| 339 | // the next packet in the while loop. |
| 340 | continue; |
| 341 | } |
| 342 | } |
| 343 | // Insert new packets into original list, before the element pointed to by |
| 344 | // iterator |it|. |
| 345 | packet_list->splice(it, new_packets, new_packets.begin(), |
| 346 | new_packets.end()); |
| 347 | // Delete old packet payload. |
| 348 | delete [] (*it)->payload; |
| 349 | delete (*it); |
| 350 | // Remove |it| from the packet list. This operation effectively moves the |
| 351 | // iterator |it| to the next packet in the list. Thus, we do not have to |
| 352 | // increment it manually. |
| 353 | it = packet_list->erase(it); |
| 354 | } |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 355 | return kOK; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void PayloadSplitter::SplitBySamples(const Packet* packet, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 359 | size_t bytes_per_ms, |
| 360 | uint32_t timestamps_per_ms, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 361 | PacketList* new_packets) { |
| 362 | assert(packet); |
| 363 | assert(new_packets); |
| 364 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 365 | size_t split_size_bytes = packet->payload_length; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 366 | |
| 367 | // Find a "chunk size" >= 20 ms and < 40 ms. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 368 | size_t min_chunk_size = bytes_per_ms * 20; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 369 | // Reduce the split size by half as long as |split_size_bytes| is at least |
| 370 | // twice the minimum chunk size (so that the resulting size is at least as |
| 371 | // large as the minimum chunk size). |
| 372 | while (split_size_bytes >= 2 * min_chunk_size) { |
| 373 | split_size_bytes >>= 1; |
| 374 | } |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 375 | uint32_t timestamps_per_chunk = static_cast<uint32_t>( |
| 376 | split_size_bytes * timestamps_per_ms / bytes_per_ms); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 377 | uint32_t timestamp = packet->header.timestamp; |
| 378 | |
| 379 | uint8_t* payload_ptr = packet->payload; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 380 | size_t len = packet->payload_length; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 381 | while (len >= (2 * split_size_bytes)) { |
| 382 | Packet* new_packet = new Packet; |
| 383 | new_packet->payload_length = split_size_bytes; |
| 384 | new_packet->header = packet->header; |
| 385 | new_packet->header.timestamp = timestamp; |
| 386 | timestamp += timestamps_per_chunk; |
| 387 | new_packet->primary = packet->primary; |
| 388 | new_packet->payload = new uint8_t[split_size_bytes]; |
| 389 | memcpy(new_packet->payload, payload_ptr, split_size_bytes); |
| 390 | payload_ptr += split_size_bytes; |
| 391 | new_packets->push_back(new_packet); |
| 392 | len -= split_size_bytes; |
| 393 | } |
| 394 | |
| 395 | if (len > 0) { |
| 396 | Packet* new_packet = new Packet; |
| 397 | new_packet->payload_length = len; |
| 398 | new_packet->header = packet->header; |
| 399 | new_packet->header.timestamp = timestamp; |
| 400 | new_packet->primary = packet->primary; |
| 401 | new_packet->payload = new uint8_t[len]; |
| 402 | memcpy(new_packet->payload, payload_ptr, len); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 403 | new_packets->push_back(new_packet); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | int PayloadSplitter::SplitByFrames(const Packet* packet, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 408 | size_t bytes_per_frame, |
| 409 | uint32_t timestamps_per_frame, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 410 | PacketList* new_packets) { |
| 411 | if (packet->payload_length % bytes_per_frame != 0) { |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 412 | LOG(LS_WARNING) << "SplitByFrames length mismatch"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 413 | return kFrameSplitError; |
| 414 | } |
| 415 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 416 | if (packet->payload_length == bytes_per_frame) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 417 | // Special case. Do not split the payload. |
| 418 | return kNoSplit; |
| 419 | } |
| 420 | |
| 421 | uint32_t timestamp = packet->header.timestamp; |
| 422 | uint8_t* payload_ptr = packet->payload; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 423 | size_t len = packet->payload_length; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 424 | while (len > 0) { |
| 425 | assert(len >= bytes_per_frame); |
| 426 | Packet* new_packet = new Packet; |
| 427 | new_packet->payload_length = bytes_per_frame; |
| 428 | new_packet->header = packet->header; |
| 429 | new_packet->header.timestamp = timestamp; |
| 430 | timestamp += timestamps_per_frame; |
| 431 | new_packet->primary = packet->primary; |
| 432 | new_packet->payload = new uint8_t[bytes_per_frame]; |
| 433 | memcpy(new_packet->payload, payload_ptr, bytes_per_frame); |
| 434 | payload_ptr += bytes_per_frame; |
| 435 | new_packets->push_back(new_packet); |
| 436 | len -= bytes_per_frame; |
| 437 | } |
| 438 | return kOK; |
| 439 | } |
| 440 | |
| 441 | } // namespace webrtc |