blob: 074d1a2f0cfd4a037d478eeed51c52a8cb0cde33 [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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#include "webrtc/modules/audio_coding/neteq/payload_splitter.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
13#include <assert.h>
14
henrik.lundin84f8cd62016-04-26 07:45:16 -070015#include "webrtc/base/checks.h"
Henrik Lundind67a2192015-08-03 12:54:37 +020016#include "webrtc/base/logging.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000017#include "webrtc/modules/audio_coding/neteq/decoder_database.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018
19namespace webrtc {
20
21// 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// temporarily held in the list |new_packets|.
24// 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// {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.
28int PayloadSplitter::SplitRed(PacketList* packet_list) {
29 int ret = kOK;
30 PacketList::iterator it = packet_list->begin();
31 while (it != packet_list->end()) {
ossudc431ce2016-08-31 08:51:13 -070032 const Packet* red_packet = (*it);
33 assert(!red_packet->payload.empty());
34 const uint8_t* payload_ptr = red_packet->payload.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035
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
ossudc431ce2016-08-31 08:51:13 -070049 struct RedHeader {
50 uint8_t payload_type;
51 uint32_t timestamp;
52 size_t payload_length;
53 bool primary;
54 };
55
56 std::vector<RedHeader> new_headers;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000057 bool last_block = false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000058 size_t sum_length = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059 while (!last_block) {
ossudc431ce2016-08-31 08:51:13 -070060 RedHeader new_header;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061 // Check the F bit. If F == 0, this was the last block.
62 last_block = ((*payload_ptr & 0x80) == 0);
63 // Bits 1 through 7 are payload type.
ossudc431ce2016-08-31 08:51:13 -070064 new_header.payload_type = payload_ptr[0] & 0x7F;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065 if (last_block) {
66 // No more header data to read.
67 ++sum_length; // Account for RED header size of 1 byte.
ossudc431ce2016-08-31 08:51:13 -070068 new_header.timestamp = red_packet->header.timestamp;
69 new_header.payload_length = red_packet->payload.size() - sum_length;
70 new_header.primary = true; // Last block is always primary.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000071 payload_ptr += 1; // Advance to first payload byte.
72 } else {
73 // Bits 8 through 21 are timestamp offset.
74 int timestamp_offset = (payload_ptr[1] << 6) +
75 ((payload_ptr[2] & 0xFC) >> 2);
ossudc431ce2016-08-31 08:51:13 -070076 new_header.timestamp = red_packet->header.timestamp - timestamp_offset;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000077 // Bits 22 through 31 are payload length.
ossudc431ce2016-08-31 08:51:13 -070078 new_header.payload_length =
79 ((payload_ptr[2] & 0x03) << 8) + payload_ptr[3];
80 new_header.primary = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081 payload_ptr += 4; // Advance to next RED header.
82 }
ossudc431ce2016-08-31 08:51:13 -070083 sum_length += new_header.payload_length;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000084 sum_length += 4; // Account for RED header size of 4 bytes.
85 // Store in new list of packets.
ossudc431ce2016-08-31 08:51:13 -070086 new_headers.push_back(new_header);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000087 }
88
89 // Populate the new packets with payload data.
90 // |payload_ptr| now points at the first payload byte.
ossudc431ce2016-08-31 08:51:13 -070091 PacketList new_packets; // An empty list to store the split packets in.
92 for (const auto& new_header : new_headers) {
93 size_t payload_length = new_header.payload_length;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000094 if (payload_ptr + payload_length >
ossudc431ce2016-08-31 08:51:13 -070095 red_packet->payload.data() + red_packet->payload.size()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000096 // The block lengths in the RED headers do not match the overall packet
97 // length. Something is corrupt. Discard this and the remaining
98 // payloads from this packet.
Henrik Lundind67a2192015-08-03 12:54:37 +020099 LOG(LS_WARNING) << "SplitRed length mismatch";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000100 ret = kRedLengthMismatch;
101 break;
102 }
ossudc431ce2016-08-31 08:51:13 -0700103 Packet* new_packet = new Packet;
104 new_packet->header = red_packet->header;
105 new_packet->header.timestamp = new_header.timestamp;
106 new_packet->header.payloadType = new_header.payload_type;
107 new_packet->primary = new_header.primary;
108 new_packet->payload.SetData(payload_ptr, payload_length);
109 new_packets.push_front(new_packet);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000110 payload_ptr += payload_length;
111 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000112 // 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.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117 delete (*it);
118 // 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
120 // increment it manually.
121 it = packet_list->erase(it);
122 }
123 return ret;
124}
125
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000126int PayloadSplitter::SplitFec(PacketList* packet_list,
127 DecoderDatabase* decoder_database) {
128 PacketList::iterator it = packet_list->begin();
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) {
Henrik Lundind67a2192015-08-03 12:54:37 +0200137 LOG(LS_WARNING) << "SplitFec unknown payload type";
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000138 return kUnknownPayloadType;
139 }
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000140
141 // Not an FEC packet.
142 AudioDecoder* decoder = decoder_database->GetDecoder(payload_type);
ossu97ba30e2016-04-25 07:55:58 -0700143 // decoder should not return NULL, except for comfort noise payloads which
144 // are handled separately.
145 assert(decoder != NULL || decoder_database->IsComfortNoise(payload_type));
minyue@webrtc.org7549ff42014-04-02 15:03:01 +0000146 if (!decoder ||
ossudc431ce2016-08-31 08:51:13 -0700147 !decoder->PacketHasFec(packet->payload.data(),
148 packet->payload.size())) {
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000149 ++it;
150 continue;
151 }
152
153 switch (info->codec_type) {
kwibergee1879c2015-10-29 06:20:28 -0700154 case NetEqDecoder::kDecoderOpus:
155 case NetEqDecoder::kDecoderOpus_2ch: {
minyue@webrtc.orga8cc3442015-02-13 14:01:54 +0000156 // 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;
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000159
minyue@webrtc.orga8cc3442015-02-13 14:01:54 +0000160 Packet* new_packet = new Packet;
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000161 new_packet->header = packet->header;
ossudc431ce2016-08-31 08:51:13 -0700162 int duration = decoder->PacketDurationRedundant(packet->payload.data(),
163 packet->payload.size());
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000164 new_packet->header.timestamp -= duration;
ossudc431ce2016-08-31 08:51:13 -0700165 new_packet->payload.SetData(packet->payload);
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000166 new_packet->primary = false;
henrik.lundin84f8cd62016-04-26 07:45:16 -0700167 // Waiting time should not be set here.
168 RTC_DCHECK(!packet->waiting_time);
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000169
170 packet_list->insert(it, new_packet);
171 break;
172 }
173 default: {
Henrik Lundind67a2192015-08-03 12:54:37 +0200174 LOG(LS_WARNING) << "SplitFec wrong payload type";
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000175 return kFecSplitError;
176 }
177 }
178
179 ++it;
180 }
181 return kOK;
182}
183
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000184int PayloadSplitter::CheckRedPayloads(PacketList* packet_list,
185 const DecoderDatabase& decoder_database) {
186 PacketList::iterator it = packet_list->begin();
187 int main_payload_type = -1;
188 int num_deleted_packets = 0;
189 while (it != packet_list->end()) {
190 uint8_t this_payload_type = (*it)->header.payloadType;
191 if (!decoder_database.IsDtmf(this_payload_type) &&
192 !decoder_database.IsComfortNoise(this_payload_type)) {
193 if (main_payload_type == -1) {
194 // This is the first packet in the list which is non-DTMF non-CNG.
195 main_payload_type = this_payload_type;
196 } else {
197 if (this_payload_type != main_payload_type) {
198 // We do not allow redundant payloads of a different type.
199 // Discard this payload.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000200 delete (*it);
201 // Remove |it| from the packet list. This operation effectively
202 // moves the iterator |it| to the next packet in the list. Thus, we
203 // do not have to increment it manually.
204 it = packet_list->erase(it);
205 ++num_deleted_packets;
206 continue;
207 }
208 }
209 }
210 ++it;
211 }
212 return num_deleted_packets;
213}
214
215int PayloadSplitter::SplitAudio(PacketList* packet_list,
216 const DecoderDatabase& decoder_database) {
217 PacketList::iterator it = packet_list->begin();
218 // Iterate through all packets in |packet_list|.
219 while (it != packet_list->end()) {
220 Packet* packet = (*it); // Just to make the notation more intuitive.
221 // Get codec type for this payload.
222 const DecoderDatabase::DecoderInfo* info =
223 decoder_database.GetDecoderInfo(packet->header.payloadType);
224 if (!info) {
Henrik Lundind67a2192015-08-03 12:54:37 +0200225 LOG(LS_WARNING) << "SplitAudio unknown payload type";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000226 return kUnknownPayloadType;
227 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000228 PacketList new_packets;
229 switch (info->codec_type) {
kwibergee1879c2015-10-29 06:20:28 -0700230 case NetEqDecoder::kDecoderPCMu:
231 case NetEqDecoder::kDecoderPCMa: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000232 // 8 bytes per ms; 8 timestamps per ms.
233 SplitBySamples(packet, 8, 8, &new_packets);
234 break;
235 }
kwibergee1879c2015-10-29 06:20:28 -0700236 case NetEqDecoder::kDecoderPCMu_2ch:
237 case NetEqDecoder::kDecoderPCMa_2ch: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000238 // 2 * 8 bytes per ms; 8 timestamps per ms.
239 SplitBySamples(packet, 2 * 8, 8, &new_packets);
240 break;
241 }
kwibergee1879c2015-10-29 06:20:28 -0700242 case NetEqDecoder::kDecoderG722: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000243 // 8 bytes per ms; 16 timestamps per ms.
244 SplitBySamples(packet, 8, 16, &new_packets);
245 break;
246 }
kwibergee1879c2015-10-29 06:20:28 -0700247 case NetEqDecoder::kDecoderPCM16B: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000248 // 16 bytes per ms; 8 timestamps per ms.
249 SplitBySamples(packet, 16, 8, &new_packets);
250 break;
251 }
kwibergee1879c2015-10-29 06:20:28 -0700252 case NetEqDecoder::kDecoderPCM16Bwb: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000253 // 32 bytes per ms; 16 timestamps per ms.
254 SplitBySamples(packet, 32, 16, &new_packets);
255 break;
256 }
kwibergee1879c2015-10-29 06:20:28 -0700257 case NetEqDecoder::kDecoderPCM16Bswb32kHz: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000258 // 64 bytes per ms; 32 timestamps per ms.
259 SplitBySamples(packet, 64, 32, &new_packets);
260 break;
261 }
kwibergee1879c2015-10-29 06:20:28 -0700262 case NetEqDecoder::kDecoderPCM16Bswb48kHz: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000263 // 96 bytes per ms; 48 timestamps per ms.
264 SplitBySamples(packet, 96, 48, &new_packets);
265 break;
266 }
kwibergee1879c2015-10-29 06:20:28 -0700267 case NetEqDecoder::kDecoderPCM16B_2ch: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268 // 2 * 16 bytes per ms; 8 timestamps per ms.
269 SplitBySamples(packet, 2 * 16, 8, &new_packets);
270 break;
271 }
kwibergee1879c2015-10-29 06:20:28 -0700272 case NetEqDecoder::kDecoderPCM16Bwb_2ch: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000273 // 2 * 32 bytes per ms; 16 timestamps per ms.
274 SplitBySamples(packet, 2 * 32, 16, &new_packets);
275 break;
276 }
kwibergee1879c2015-10-29 06:20:28 -0700277 case NetEqDecoder::kDecoderPCM16Bswb32kHz_2ch: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000278 // 2 * 64 bytes per ms; 32 timestamps per ms.
279 SplitBySamples(packet, 2 * 64, 32, &new_packets);
280 break;
281 }
kwibergee1879c2015-10-29 06:20:28 -0700282 case NetEqDecoder::kDecoderPCM16Bswb48kHz_2ch: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000283 // 2 * 96 bytes per ms; 48 timestamps per ms.
284 SplitBySamples(packet, 2 * 96, 48, &new_packets);
285 break;
286 }
kwibergee1879c2015-10-29 06:20:28 -0700287 case NetEqDecoder::kDecoderPCM16B_5ch: {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000288 // 5 * 16 bytes per ms; 8 timestamps per ms.
289 SplitBySamples(packet, 5 * 16, 8, &new_packets);
290 break;
291 }
kwibergee1879c2015-10-29 06:20:28 -0700292 case NetEqDecoder::kDecoderILBC: {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000293 size_t bytes_per_frame;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000294 int timestamps_per_frame;
ossudc431ce2016-08-31 08:51:13 -0700295 if (packet->payload.size() >= 950) {
Henrik Lundind67a2192015-08-03 12:54:37 +0200296 LOG(LS_WARNING) << "SplitAudio too large iLBC payload";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000297 return kTooLargePayload;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000298 }
ossudc431ce2016-08-31 08:51:13 -0700299 if (packet->payload.size() % 38 == 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000300 // 20 ms frames.
301 bytes_per_frame = 38;
302 timestamps_per_frame = 160;
ossudc431ce2016-08-31 08:51:13 -0700303 } else if (packet->payload.size() % 50 == 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000304 // 30 ms frames.
305 bytes_per_frame = 50;
306 timestamps_per_frame = 240;
307 } else {
Henrik Lundind67a2192015-08-03 12:54:37 +0200308 LOG(LS_WARNING) << "SplitAudio invalid iLBC payload";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000309 return kFrameSplitError;
310 }
311 int ret = SplitByFrames(packet, bytes_per_frame, timestamps_per_frame,
312 &new_packets);
313 if (ret < 0) {
314 return ret;
315 } else if (ret == kNoSplit) {
316 // Do not split at all. Simply advance to the next packet in the list.
317 ++it;
318 // We do not have any new packets to insert, and should not delete the
319 // old one. Skip the code after the switch case, and jump straight to
320 // the next packet in the while loop.
321 continue;
322 }
323 break;
324 }
325 default: {
326 // Do not split at all. Simply advance to the next packet in the list.
327 ++it;
328 // We do not have any new packets to insert, and should not delete the
329 // old one. Skip the code after the switch case, and jump straight to
330 // the next packet in the while loop.
331 continue;
332 }
333 }
334 // Insert new packets into original list, before the element pointed to by
335 // iterator |it|.
336 packet_list->splice(it, new_packets, new_packets.begin(),
337 new_packets.end());
338 // Delete old packet payload.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000339 delete (*it);
340 // Remove |it| from the packet list. This operation effectively moves the
341 // iterator |it| to the next packet in the list. Thus, we do not have to
342 // increment it manually.
343 it = packet_list->erase(it);
344 }
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000345 return kOK;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000346}
347
348void PayloadSplitter::SplitBySamples(const Packet* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000349 size_t bytes_per_ms,
350 uint32_t timestamps_per_ms,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000351 PacketList* new_packets) {
352 assert(packet);
353 assert(new_packets);
354
ossudc431ce2016-08-31 08:51:13 -0700355 size_t split_size_bytes = packet->payload.size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000356
357 // Find a "chunk size" >= 20 ms and < 40 ms.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000358 size_t min_chunk_size = bytes_per_ms * 20;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000359 // Reduce the split size by half as long as |split_size_bytes| is at least
360 // twice the minimum chunk size (so that the resulting size is at least as
361 // large as the minimum chunk size).
362 while (split_size_bytes >= 2 * min_chunk_size) {
363 split_size_bytes >>= 1;
364 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000365 uint32_t timestamps_per_chunk = static_cast<uint32_t>(
366 split_size_bytes * timestamps_per_ms / bytes_per_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000367 uint32_t timestamp = packet->header.timestamp;
368
ossudc431ce2016-08-31 08:51:13 -0700369 const uint8_t* payload_ptr = packet->payload.data();
370 size_t len = packet->payload.size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000371 while (len >= (2 * split_size_bytes)) {
372 Packet* new_packet = new Packet;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000373 new_packet->header = packet->header;
374 new_packet->header.timestamp = timestamp;
375 timestamp += timestamps_per_chunk;
376 new_packet->primary = packet->primary;
ossudc431ce2016-08-31 08:51:13 -0700377 new_packet->payload.SetData(payload_ptr, split_size_bytes);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378 payload_ptr += split_size_bytes;
379 new_packets->push_back(new_packet);
380 len -= split_size_bytes;
381 }
382
383 if (len > 0) {
384 Packet* new_packet = new Packet;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000385 new_packet->header = packet->header;
386 new_packet->header.timestamp = timestamp;
387 new_packet->primary = packet->primary;
ossudc431ce2016-08-31 08:51:13 -0700388 new_packet->payload.SetData(payload_ptr, len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000389 new_packets->push_back(new_packet);
390 }
391}
392
393int PayloadSplitter::SplitByFrames(const Packet* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000394 size_t bytes_per_frame,
395 uint32_t timestamps_per_frame,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000396 PacketList* new_packets) {
ossudc431ce2016-08-31 08:51:13 -0700397 if (packet->payload.size() % bytes_per_frame != 0) {
Henrik Lundind67a2192015-08-03 12:54:37 +0200398 LOG(LS_WARNING) << "SplitByFrames length mismatch";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000399 return kFrameSplitError;
400 }
401
ossudc431ce2016-08-31 08:51:13 -0700402 if (packet->payload.size() == bytes_per_frame) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000403 // Special case. Do not split the payload.
404 return kNoSplit;
405 }
406
407 uint32_t timestamp = packet->header.timestamp;
ossudc431ce2016-08-31 08:51:13 -0700408 const uint8_t* payload_ptr = packet->payload.data();
409 size_t len = packet->payload.size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000410 while (len > 0) {
411 assert(len >= bytes_per_frame);
412 Packet* new_packet = new Packet;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000413 new_packet->header = packet->header;
414 new_packet->header.timestamp = timestamp;
415 timestamp += timestamps_per_frame;
416 new_packet->primary = packet->primary;
ossudc431ce2016-08-31 08:51:13 -0700417 new_packet->payload.SetData(payload_ptr, bytes_per_frame);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000418 payload_ptr += bytes_per_frame;
419 new_packets->push_back(new_packet);
420 len -= bytes_per_frame;
421 }
422 return kOK;
423}
424
425} // namespace webrtc