Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1603)

Side by Side Diff: webrtc/modules/media_file/media_file_utility.cc

Issue 1534193008: Misc. small cleanups (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Formatting fixes Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 "ModuleFileUtility::~ModuleFileUtility()"); 72 "ModuleFileUtility::~ModuleFileUtility()");
73 } 73 }
74 74
75 int32_t ModuleFileUtility::ReadWavHeader(InStream& wav) 75 int32_t ModuleFileUtility::ReadWavHeader(InStream& wav)
76 { 76 {
77 WAVE_RIFF_header RIFFheaderObj; 77 WAVE_RIFF_header RIFFheaderObj;
78 WAVE_CHUNK_header CHUNKheaderObj; 78 WAVE_CHUNK_header CHUNKheaderObj;
79 // TODO (hellner): tmpStr and tmpStr2 seems unnecessary here. 79 // TODO (hellner): tmpStr and tmpStr2 seems unnecessary here.
80 char tmpStr[6] = "FOUR"; 80 char tmpStr[6] = "FOUR";
81 unsigned char tmpStr2[4]; 81 unsigned char tmpStr2[4];
82 int32_t i, len; 82 int32_t i;
83 bool dataFound = false; 83 bool dataFound = false;
84 bool fmtFound = false; 84 bool fmtFound = false;
85 int8_t dummyRead; 85 int8_t dummyRead;
86 86
87 87
88 _dataSize = 0; 88 _dataSize = 0;
89 len = wav.Read(&RIFFheaderObj, sizeof(WAVE_RIFF_header)); 89 int len = wav.Read(&RIFFheaderObj, sizeof(WAVE_RIFF_header));
90 if(len != sizeof(WAVE_RIFF_header)) 90 if (len != static_cast<int>(sizeof(WAVE_RIFF_header)))
91 { 91 {
92 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 92 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
93 "Not a wave file (too short)"); 93 "Not a wave file (too short)");
94 return -1; 94 return -1;
95 } 95 }
96 96
97 for (i = 0; i < 4; i++) 97 for (i = 0; i < 4; i++)
98 { 98 {
99 tmpStr[i] = RIFFheaderObj.ckID[i]; 99 tmpStr[i] = RIFFheaderObj.ckID[i];
100 } 100 }
(...skipping 19 matching lines...) Expand all
120 // WAVE files are stored in little endian byte order. Make sure that the 120 // WAVE files are stored in little endian byte order. Make sure that the
121 // data can be read on big endian as well. 121 // data can be read on big endian as well.
122 // TODO (hellner): little endian to system byte order should be done in 122 // TODO (hellner): little endian to system byte order should be done in
123 // in a subroutine. 123 // in a subroutine.
124 memcpy(tmpStr2, &CHUNKheaderObj.fmt_ckSize, 4); 124 memcpy(tmpStr2, &CHUNKheaderObj.fmt_ckSize, 4);
125 CHUNKheaderObj.fmt_ckSize = 125 CHUNKheaderObj.fmt_ckSize =
126 (int32_t) ((uint32_t) tmpStr2[0] + 126 (int32_t) ((uint32_t) tmpStr2[0] +
127 (((uint32_t)tmpStr2[1])<<8) + 127 (((uint32_t)tmpStr2[1])<<8) +
128 (((uint32_t)tmpStr2[2])<<16) + 128 (((uint32_t)tmpStr2[2])<<16) +
129 (((uint32_t)tmpStr2[3])<<24)); 129 (((uint32_t)tmpStr2[3])<<24));
130 if (CHUNKheaderObj.fmt_ckSize < 0)
niklas.enbom 2016/01/07 20:16:46 Chunk Size in wav header is an unsigned int and ca
Peter Kasting 2016/01/07 21:35:05 So, to be clear, are you suggesting I remove this
niklas.enbom 2016/01/07 21:50:36 That was my suggestion yes, but I haven't looked a
Peter Kasting 2016/01/08 01:36:27 I made the change. As you suspected, this value i
131 {
132 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
133 "Chunk size may not be negative");
134 return -1;
135 }
130 136
131 memcpy(tmpStr, CHUNKheaderObj.fmt_ckID, 4); 137 memcpy(tmpStr, CHUNKheaderObj.fmt_ckID, 4);
132 138
133 while ((len == sizeof(WAVE_CHUNK_header)) && (!fmtFound || !dataFound)) 139 while ((len == static_cast<int>(sizeof(WAVE_CHUNK_header))) &&
140 (!fmtFound || !dataFound))
134 { 141 {
135 if(strcmp(tmpStr, "fmt ") == 0) 142 if(strcmp(tmpStr, "fmt ") == 0)
136 { 143 {
137 len = wav.Read(&_wavFormatObj, sizeof(WAVE_FMTINFO_header)); 144 len = wav.Read(&_wavFormatObj, sizeof(WAVE_FMTINFO_header));
138 145
139 memcpy(tmpStr2, &_wavFormatObj.formatTag, 2); 146 memcpy(tmpStr2, &_wavFormatObj.formatTag, 2);
140 _wavFormatObj.formatTag = 147 _wavFormatObj.formatTag =
141 (uint32_t)tmpStr2[0] + (((uint32_t)tmpStr2[1])<<8); 148 (uint32_t)tmpStr2[0] + (((uint32_t)tmpStr2[1])<<8);
142 memcpy(tmpStr2, &_wavFormatObj.nChannels, 2); 149 memcpy(tmpStr2, &_wavFormatObj.nChannels, 2);
143 _wavFormatObj.nChannels = 150 _wavFormatObj.nChannels =
(...skipping 30 matching lines...) Expand all
174 { 181 {
175 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 182 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
176 "File corrupted, reached EOF (reading fmt)"); 183 "File corrupted, reached EOF (reading fmt)");
177 return -1; 184 return -1;
178 } 185 }
179 } 186 }
180 fmtFound = true; 187 fmtFound = true;
181 } 188 }
182 else if(strcmp(tmpStr, "data") == 0) 189 else if(strcmp(tmpStr, "data") == 0)
183 { 190 {
184 _dataSize = CHUNKheaderObj.fmt_ckSize; 191 _dataSize = static_cast<size_t>(CHUNKheaderObj.fmt_ckSize);
185 dataFound = true; 192 dataFound = true;
186 break; 193 break;
187 } 194 }
188 else 195 else
189 { 196 {
190 for (i = 0; i < (CHUNKheaderObj.fmt_ckSize); i++) 197 for (i = 0; i < (CHUNKheaderObj.fmt_ckSize); i++)
191 { 198 {
192 len = wav.Read(&dummyRead, 1); 199 len = wav.Read(&dummyRead, 1);
193 if(len != 1) 200 if(len != 1)
194 { 201 {
195 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 202 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
196 "File corrupted, reached EOF (reading other)"); 203 "File corrupted, reached EOF (reading other)");
197 return -1; 204 return -1;
198 } 205 }
199 } 206 }
200 } 207 }
201 208
202 len = wav.Read(&CHUNKheaderObj, sizeof(WAVE_CHUNK_header)); 209 len = wav.Read(&CHUNKheaderObj, sizeof(WAVE_CHUNK_header));
203 210
204 memcpy(tmpStr2, &CHUNKheaderObj.fmt_ckSize, 4); 211 memcpy(tmpStr2, &CHUNKheaderObj.fmt_ckSize, 4);
205 CHUNKheaderObj.fmt_ckSize = 212 CHUNKheaderObj.fmt_ckSize =
206 (int32_t) ((uint32_t)tmpStr2[0] + 213 (int32_t) ((uint32_t)tmpStr2[0] +
207 (((uint32_t)tmpStr2[1])<<8) + 214 (((uint32_t)tmpStr2[1])<<8) +
208 (((uint32_t)tmpStr2[2])<<16) + 215 (((uint32_t)tmpStr2[2])<<16) +
209 (((uint32_t)tmpStr2[3])<<24)); 216 (((uint32_t)tmpStr2[3])<<24));
217 if (CHUNKheaderObj.fmt_ckSize < 0)
218 {
219 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
220 "Chunk size may not be negative");
221 return -1;
222 }
210 223
211 memcpy(tmpStr, CHUNKheaderObj.fmt_ckID, 4); 224 memcpy(tmpStr, CHUNKheaderObj.fmt_ckID, 4);
212 } 225 }
213 226
214 // Either a proper format chunk has been read or a data chunk was come 227 // Either a proper format chunk has been read or a data chunk was come
215 // across. 228 // across.
216 if( (_wavFormatObj.formatTag != kWavFormatPcm) && 229 if( (_wavFormatObj.formatTag != kWavFormatPcm) &&
217 (_wavFormatObj.formatTag != kWavFormatALaw) && 230 (_wavFormatObj.formatTag != kWavFormatALaw) &&
218 (_wavFormatObj.formatTag != kWavFormatMuLaw)) 231 (_wavFormatObj.formatTag != kWavFormatMuLaw))
219 { 232 {
(...skipping 14 matching lines...) Expand all
234 if((_wavFormatObj.nBitsPerSample != 8) && 247 if((_wavFormatObj.nBitsPerSample != 8) &&
235 (_wavFormatObj.nBitsPerSample != 16)) 248 (_wavFormatObj.nBitsPerSample != 16))
236 { 249 {
237 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 250 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
238 "nBitsPerSample value=%d not supported!", 251 "nBitsPerSample value=%d not supported!",
239 _wavFormatObj.nBitsPerSample); 252 _wavFormatObj.nBitsPerSample);
240 return -1; 253 return -1;
241 } 254 }
242 255
243 // Calculate the number of bytes that 10 ms of audio data correspond to. 256 // Calculate the number of bytes that 10 ms of audio data correspond to.
244 if(_wavFormatObj.formatTag == kWavFormatPcm) 257 size_t samples_per_10ms =
245 { 258 ((_wavFormatObj.formatTag == kWavFormatPcm) &&
246 // TODO (hellner): integer division for 22050 and 11025 would yield 259 (_wavFormatObj.nSamplesPerSec == 44100)) ?
247 // the same result as the else statement. Remove those 260 440 : static_cast<size_t>(_wavFormatObj.nSamplesPerSec / 100);
248 // special cases? 261 _readSizeBytes = samples_per_10ms * _wavFormatObj.nChannels *
249 if(_wavFormatObj.nSamplesPerSec == 44100) 262 (_wavFormatObj.nBitsPerSample / 8);
250 {
251 _readSizeBytes = 440 * _wavFormatObj.nChannels *
252 (_wavFormatObj.nBitsPerSample / 8);
253 } else if(_wavFormatObj.nSamplesPerSec == 22050) {
254 _readSizeBytes = 220 * _wavFormatObj.nChannels *
255 (_wavFormatObj.nBitsPerSample / 8);
256 } else if(_wavFormatObj.nSamplesPerSec == 11025) {
257 _readSizeBytes = 110 * _wavFormatObj.nChannels *
258 (_wavFormatObj.nBitsPerSample / 8);
259 } else {
260 _readSizeBytes = (_wavFormatObj.nSamplesPerSec/100) *
261 _wavFormatObj.nChannels * (_wavFormatObj.nBitsPerSample / 8);
262 }
263
264 } else {
265 _readSizeBytes = (_wavFormatObj.nSamplesPerSec/100) *
266 _wavFormatObj.nChannels * (_wavFormatObj.nBitsPerSample / 8);
267 }
268 return 0; 263 return 0;
269 } 264 }
270 265
271 int32_t ModuleFileUtility::InitWavCodec(uint32_t samplesPerSec, 266 int32_t ModuleFileUtility::InitWavCodec(uint32_t samplesPerSec,
272 uint32_t channels, 267 uint32_t channels,
273 uint32_t bitsPerSample, 268 uint32_t bitsPerSample,
274 uint32_t formatTag) 269 uint32_t formatTag)
275 { 270 {
276 codec_info_.pltype = -1; 271 codec_info_.pltype = -1;
277 codec_info_.plfreq = samplesPerSec; 272 codec_info_.plfreq = samplesPerSec;
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 "failed to read WAV header!"); 364 "failed to read WAV header!");
370 return -1; 365 return -1;
371 } 366 }
372 367
373 _playoutPositionMs = 0; 368 _playoutPositionMs = 0;
374 _readPos = 0; 369 _readPos = 0;
375 370
376 if(start > 0) 371 if(start > 0)
377 { 372 {
378 uint8_t dummy[WAV_MAX_BUFFER_SIZE]; 373 uint8_t dummy[WAV_MAX_BUFFER_SIZE];
379 int32_t readLength; 374 int readLength;
380 if(_readSizeBytes <= WAV_MAX_BUFFER_SIZE) 375 if(_readSizeBytes <= WAV_MAX_BUFFER_SIZE)
381 { 376 {
382 while (_playoutPositionMs < start) 377 while (_playoutPositionMs < start)
383 { 378 {
384 readLength = wav.Read(dummy, _readSizeBytes); 379 readLength = wav.Read(dummy, _readSizeBytes);
385 if(readLength == _readSizeBytes) 380 if(readLength == static_cast<int>(_readSizeBytes))
386 { 381 {
387 _readPos += readLength; 382 _readPos += _readSizeBytes;
388 _playoutPositionMs += 10; 383 _playoutPositionMs += 10;
389 } 384 }
390 else // Must have reached EOF before start position! 385 else // Must have reached EOF before start position!
391 { 386 {
392 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 387 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
393 "InitWavReading(), EOF before start position"); 388 "InitWavReading(), EOF before start position");
394 return -1; 389 return -1;
395 } 390 }
396 } 391 }
397 } 392 }
398 else 393 else
399 { 394 {
400 return -1; 395 return -1;
401 } 396 }
402 } 397 }
403 if( InitWavCodec(_wavFormatObj.nSamplesPerSec, _wavFormatObj.nChannels, 398 if( InitWavCodec(_wavFormatObj.nSamplesPerSec, _wavFormatObj.nChannels,
404 _wavFormatObj.nBitsPerSample, 399 _wavFormatObj.nBitsPerSample,
405 _wavFormatObj.formatTag) != 0) 400 _wavFormatObj.formatTag) != 0)
406 { 401 {
407 return -1; 402 return -1;
408 } 403 }
409 _bytesPerSample = _wavFormatObj.nBitsPerSample / 8; 404 _bytesPerSample = static_cast<size_t>(_wavFormatObj.nBitsPerSample / 8);
410 405
411 406
412 _startPointInMs = start; 407 _startPointInMs = start;
413 _stopPointInMs = stop; 408 _stopPointInMs = stop;
414 _reading = true; 409 _reading = true;
415 return 0; 410 return 0;
416 } 411 }
417 412
418 int32_t ModuleFileUtility::ReadWavDataAsMono( 413 int32_t ModuleFileUtility::ReadWavDataAsMono(
419 InStream& wav, 414 InStream& wav,
420 int8_t* outData, 415 int8_t* outData,
421 const size_t bufferSize) 416 const size_t bufferSize)
422 { 417 {
423 WEBRTC_TRACE( 418 WEBRTC_TRACE(
424 kTraceStream, 419 kTraceStream,
425 kTraceFile, 420 kTraceFile,
426 _id, 421 _id,
427 "ModuleFileUtility::ReadWavDataAsMono(wav= 0x%x, outData= 0x%d, " 422 "ModuleFileUtility::ReadWavDataAsMono(wav= 0x%x, outData= 0x%d, "
428 "bufSize= %" PRIuS ")", 423 "bufSize= %" PRIuS ")",
429 &wav, 424 &wav,
430 outData, 425 outData,
431 bufferSize); 426 bufferSize);
432 427
433 // The number of bytes that should be read from file. 428 // The number of bytes that should be read from file.
434 const uint32_t totalBytesNeeded = _readSizeBytes; 429 const size_t totalBytesNeeded = _readSizeBytes;
435 // The number of bytes that will be written to outData. 430 // The number of bytes that will be written to outData.
436 const uint32_t bytesRequested = (codec_info_.channels == 2) ? 431 const size_t bytesRequested = (codec_info_.channels == 2) ?
437 totalBytesNeeded >> 1 : totalBytesNeeded; 432 totalBytesNeeded >> 1 : totalBytesNeeded;
438 if(bufferSize < bytesRequested) 433 if(bufferSize < bytesRequested)
439 { 434 {
440 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 435 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
441 "ReadWavDataAsMono: output buffer is too short!"); 436 "ReadWavDataAsMono: output buffer is too short!");
442 return -1; 437 return -1;
443 } 438 }
444 if(outData == NULL) 439 if(outData == NULL)
445 { 440 {
446 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 441 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
(...skipping 18 matching lines...) Expand all
465 } 460 }
466 if(bytesRead < 0) 461 if(bytesRead < 0)
467 { 462 {
468 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 463 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
469 "ReadWavDataAsMono: failed to read data from WAV file."); 464 "ReadWavDataAsMono: failed to read data from WAV file.");
470 return -1; 465 return -1;
471 } 466 }
472 // Output data is should be mono. 467 // Output data is should be mono.
473 if(codec_info_.channels == 2) 468 if(codec_info_.channels == 2)
474 { 469 {
475 for (uint32_t i = 0; i < bytesRequested / _bytesPerSample; i++) 470 for (size_t i = 0; i < bytesRequested / _bytesPerSample; i++)
476 { 471 {
477 // Sample value is the average of left and right buffer rounded to 472 // Sample value is the average of left and right buffer rounded to
478 // closest integer value. Note samples can be either 1 or 2 byte. 473 // closest integer value. Note samples can be either 1 or 2 byte.
479 if(_bytesPerSample == 1) 474 if(_bytesPerSample == 1)
480 { 475 {
481 _tempData[i] = ((_tempData[2 * i] + _tempData[(2 * i) + 1] + 476 _tempData[i] = ((_tempData[2 * i] + _tempData[(2 * i) + 1] +
482 1) >> 1); 477 1) >> 1);
483 } 478 }
484 else 479 else
485 { 480 {
486 int16_t* sampleData = (int16_t*) _tempData; 481 int16_t* sampleData = (int16_t*) _tempData;
487 sampleData[i] = ((sampleData[2 * i] + sampleData[(2 * i) + 1] + 482 sampleData[i] = ((sampleData[2 * i] + sampleData[(2 * i) + 1] +
488 1) >> 1); 483 1) >> 1);
489 } 484 }
490 } 485 }
491 memcpy(outData, _tempData, bytesRequested); 486 memcpy(outData, _tempData, bytesRequested);
492 } 487 }
493 return bytesRequested; 488 return static_cast<int32_t>(bytesRequested);
494 } 489 }
495 490
496 int32_t ModuleFileUtility::ReadWavDataAsStereo( 491 int32_t ModuleFileUtility::ReadWavDataAsStereo(
497 InStream& wav, 492 InStream& wav,
498 int8_t* outDataLeft, 493 int8_t* outDataLeft,
499 int8_t* outDataRight, 494 int8_t* outDataRight,
500 const size_t bufferSize) 495 const size_t bufferSize)
501 { 496 {
502 WEBRTC_TRACE( 497 WEBRTC_TRACE(
503 kTraceStream, 498 kTraceStream,
(...skipping 23 matching lines...) Expand all
527 return -1; 522 return -1;
528 } 523 }
529 if(! _reading) 524 if(! _reading)
530 { 525 {
531 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 526 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
532 "ReadWavDataAsStereo: no longer reading file."); 527 "ReadWavDataAsStereo: no longer reading file.");
533 return -1; 528 return -1;
534 } 529 }
535 530
536 // The number of bytes that should be read from file. 531 // The number of bytes that should be read from file.
537 const uint32_t totalBytesNeeded = _readSizeBytes; 532 const size_t totalBytesNeeded = _readSizeBytes;
538 // The number of bytes that will be written to the left and the right 533 // The number of bytes that will be written to the left and the right
539 // buffers. 534 // buffers.
540 const uint32_t bytesRequested = totalBytesNeeded >> 1; 535 const size_t bytesRequested = totalBytesNeeded >> 1;
541 if(bufferSize < bytesRequested) 536 if(bufferSize < bytesRequested)
542 { 537 {
543 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 538 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
544 "ReadWavData: Output buffers are too short!"); 539 "ReadWavData: Output buffers are too short!");
545 assert(false); 540 assert(false);
546 return -1; 541 return -1;
547 } 542 }
548 543
549 int32_t bytesRead = ReadWavData(wav, _tempData, totalBytesNeeded); 544 int32_t bytesRead = ReadWavData(wav, _tempData, totalBytesNeeded);
550 if(bytesRead <= 0) 545 if(bytesRead <= 0)
551 { 546 {
552 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 547 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
553 "ReadWavDataAsStereo: failed to read data from WAV file."); 548 "ReadWavDataAsStereo: failed to read data from WAV file.");
554 return -1; 549 return -1;
555 } 550 }
556 551
557 // Turn interleaved audio to left and right buffer. Note samples can be 552 // Turn interleaved audio to left and right buffer. Note samples can be
558 // either 1 or 2 bytes 553 // either 1 or 2 bytes
559 if(_bytesPerSample == 1) 554 if(_bytesPerSample == 1)
560 { 555 {
561 for (uint32_t i = 0; i < bytesRequested; i++) 556 for (size_t i = 0; i < bytesRequested; i++)
562 { 557 {
563 outDataLeft[i] = _tempData[2 * i]; 558 outDataLeft[i] = _tempData[2 * i];
564 outDataRight[i] = _tempData[(2 * i) + 1]; 559 outDataRight[i] = _tempData[(2 * i) + 1];
565 } 560 }
566 } 561 }
567 else if(_bytesPerSample == 2) 562 else if(_bytesPerSample == 2)
568 { 563 {
569 int16_t* sampleData = reinterpret_cast<int16_t*>(_tempData); 564 int16_t* sampleData = reinterpret_cast<int16_t*>(_tempData);
570 int16_t* outLeft = reinterpret_cast<int16_t*>(outDataLeft); 565 int16_t* outLeft = reinterpret_cast<int16_t*>(outDataLeft);
571 int16_t* outRight = reinterpret_cast<int16_t*>( 566 int16_t* outRight = reinterpret_cast<int16_t*>(
572 outDataRight); 567 outDataRight);
573 568
574 // Bytes requested to samples requested. 569 // Bytes requested to samples requested.
575 uint32_t sampleCount = bytesRequested >> 1; 570 size_t sampleCount = bytesRequested >> 1;
576 for (uint32_t i = 0; i < sampleCount; i++) 571 for (size_t i = 0; i < sampleCount; i++)
577 { 572 {
578 outLeft[i] = sampleData[2 * i]; 573 outLeft[i] = sampleData[2 * i];
579 outRight[i] = sampleData[(2 * i) + 1]; 574 outRight[i] = sampleData[(2 * i) + 1];
580 } 575 }
581 } else { 576 } else {
582 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 577 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
583 "ReadWavStereoData: unsupported sample size %d!", 578 "ReadWavStereoData: unsupported sample size %" PRIuS "!",
584 _bytesPerSample); 579 _bytesPerSample);
585 assert(false); 580 assert(false);
586 return -1; 581 return -1;
587 } 582 }
588 return bytesRequested; 583 return static_cast<int32_t>(bytesRequested);
589 } 584 }
590 585
591 int32_t ModuleFileUtility::ReadWavData( 586 int32_t ModuleFileUtility::ReadWavData(InStream& wav,
592 InStream& wav, 587 uint8_t* buffer,
593 uint8_t* buffer, 588 size_t dataLengthInBytes)
594 const uint32_t dataLengthInBytes)
595 { 589 {
596 WEBRTC_TRACE( 590 WEBRTC_TRACE(kTraceStream, kTraceFile, _id,
597 kTraceStream, 591 "ModuleFileUtility::ReadWavData(wav= 0x%x, buffer= 0x%x, "
598 kTraceFile, 592 "dataLen= %" PRIuS ")", &wav, buffer, dataLengthInBytes);
599 _id,
600 "ModuleFileUtility::ReadWavData(wav= 0x%x, buffer= 0x%x, dataLen= %ld)",
601 &wav,
602 buffer,
603 dataLengthInBytes);
604 593
605 594
606 if(buffer == NULL) 595 if(buffer == NULL)
607 { 596 {
608 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 597 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
609 "ReadWavDataAsMono: output buffer NULL!"); 598 "ReadWavDataAsMono: output buffer NULL!");
610 return -1; 599 return -1;
611 } 600 }
612 601
613 // Make sure that a read won't return too few samples. 602 // Make sure that a read won't return too few samples.
614 // TODO (hellner): why not read the remaining bytes needed from the start 603 // TODO (hellner): why not read the remaining bytes needed from the start
615 // of the file? 604 // of the file?
616 if((_dataSize - _readPos) < (int32_t)dataLengthInBytes) 605 if(_dataSize < (_readPos + dataLengthInBytes))
617 { 606 {
618 // Rewind() being -1 may be due to the file not supposed to be looped. 607 // Rewind() being -1 may be due to the file not supposed to be looped.
619 if(wav.Rewind() == -1) 608 if(wav.Rewind() == -1)
620 { 609 {
621 _reading = false; 610 _reading = false;
622 return 0; 611 return 0;
623 } 612 }
624 if(InitWavReading(wav, _startPointInMs, _stopPointInMs) == -1) 613 if(InitWavReading(wav, _startPointInMs, _stopPointInMs) == -1)
625 { 614 {
626 _reading = false; 615 _reading = false;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 1 : codecInst.channels; 678 1 : codecInst.channels;
690 679
691 if(STR_CASE_CMP(codecInst.plname, "PCMU") == 0) 680 if(STR_CASE_CMP(codecInst.plname, "PCMU") == 0)
692 { 681 {
693 _bytesPerSample = 1; 682 _bytesPerSample = 1;
694 if(WriteWavHeader(wav, 8000, _bytesPerSample, channels, 683 if(WriteWavHeader(wav, 8000, _bytesPerSample, channels,
695 kWavFormatMuLaw, 0) == -1) 684 kWavFormatMuLaw, 0) == -1)
696 { 685 {
697 return -1; 686 return -1;
698 } 687 }
699 }else if(STR_CASE_CMP(codecInst.plname, "PCMA") == 0) 688 }
689 else if(STR_CASE_CMP(codecInst.plname, "PCMA") == 0)
700 { 690 {
701 _bytesPerSample = 1; 691 _bytesPerSample = 1;
702 if(WriteWavHeader(wav, 8000, _bytesPerSample, channels, kWavFormatALaw, 692 if(WriteWavHeader(wav, 8000, _bytesPerSample, channels, kWavFormatALaw,
703 0) == -1) 693 0) == -1)
704 { 694 {
705 return -1; 695 return -1;
706 } 696 }
707 } 697 }
708 else if(STR_CASE_CMP(codecInst.plname, "L16") == 0) 698 else if(STR_CASE_CMP(codecInst.plname, "L16") == 0)
709 { 699 {
(...skipping 12 matching lines...) Expand all
722 } 712 }
723 _writing = true; 713 _writing = true;
724 _bytesWritten = 0; 714 _bytesWritten = 0;
725 return 0; 715 return 0;
726 } 716 }
727 717
728 int32_t ModuleFileUtility::WriteWavData(OutStream& out, 718 int32_t ModuleFileUtility::WriteWavData(OutStream& out,
729 const int8_t* buffer, 719 const int8_t* buffer,
730 const size_t dataLength) 720 const size_t dataLength)
731 { 721 {
732 WEBRTC_TRACE( 722 WEBRTC_TRACE(kTraceStream, kTraceFile, _id,
733 kTraceStream, 723 "ModuleFileUtility::WriteWavData(out= 0x%x, buf= 0x%x, "
734 kTraceFile, 724 "dataLen= %" PRIuS ")", &out, buffer, dataLength);
735 _id,
736 "ModuleFileUtility::WriteWavData(out= 0x%x, buf= 0x%x, dataLen= %" PRIuS
737 ")",
738 &out,
739 buffer,
740 dataLength);
741 725
742 if(buffer == NULL) 726 if(buffer == NULL)
743 { 727 {
744 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 728 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
745 "WriteWavData: input buffer NULL!"); 729 "WriteWavData: input buffer NULL!");
746 return -1; 730 return -1;
747 } 731 }
748 732
749 if(!out.Write(buffer, dataLength)) 733 if(!out.Write(buffer, dataLength))
750 { 734 {
751 return -1; 735 return -1;
752 } 736 }
753 _bytesWritten += dataLength; 737 _bytesWritten += dataLength;
754 return static_cast<int32_t>(dataLength); 738 return static_cast<int32_t>(dataLength);
755 } 739 }
756 740
757 741
758 int32_t ModuleFileUtility::WriteWavHeader( 742 int32_t ModuleFileUtility::WriteWavHeader(
759 OutStream& wav, 743 OutStream& wav,
760 const uint32_t freq, 744 uint32_t freq,
761 const uint32_t bytesPerSample, 745 size_t bytesPerSample,
762 const uint32_t channels, 746 uint32_t channels,
763 const uint32_t format, 747 uint32_t format,
764 const uint32_t lengthInBytes) 748 size_t lengthInBytes)
765 { 749 {
766 // Frame size in bytes for 10 ms of audio. 750 // Frame size in bytes for 10 ms of audio.
767 // TODO (hellner): 44.1 kHz has 440 samples frame size. Doesn't seem to 751 // TODO (hellner): 44.1 kHz has 440 samples frame size. Doesn't seem to
768 // be taken into consideration here! 752 // be taken into consideration here!
769 const int32_t frameSize = (freq / 100) * channels; 753 const size_t frameSize = (freq / 100) * channels;
770 754
771 // Calculate the number of full frames that the wave file contain. 755 // Calculate the number of full frames that the wave file contain.
772 const int32_t dataLengthInBytes = frameSize * (lengthInBytes / frameSize); 756 const size_t dataLengthInBytes = frameSize * (lengthInBytes / frameSize);
773 757
774 uint8_t buf[kWavHeaderSize]; 758 uint8_t buf[kWavHeaderSize];
775 webrtc::WriteWavHeader(buf, channels, freq, static_cast<WavFormat>(format), 759 webrtc::WriteWavHeader(buf, channels, freq, static_cast<WavFormat>(format),
776 bytesPerSample, dataLengthInBytes / bytesPerSample); 760 bytesPerSample, dataLengthInBytes / bytesPerSample);
777 wav.Write(buf, kWavHeaderSize); 761 wav.Write(buf, kWavHeaderSize);
778 return 0; 762 return 0;
779 } 763 }
780 764
781 int32_t ModuleFileUtility::UpdateWavHeader(OutStream& wav) 765 int32_t ModuleFileUtility::UpdateWavHeader(OutStream& wav)
782 { 766 {
783 int32_t res = -1; 767 int32_t res = -1;
784 if(wav.Rewind() == -1) 768 if(wav.Rewind() == -1)
785 { 769 {
786 return -1; 770 return -1;
787 } 771 }
788 uint32_t channels = (codec_info_.channels == 0) ? 772 uint32_t channels = (codec_info_.channels == 0) ? 1 : codec_info_.channels;
789 1 : codec_info_.channels;
790 773
791 if(STR_CASE_CMP(codec_info_.plname, "L16") == 0) 774 if(STR_CASE_CMP(codec_info_.plname, "L16") == 0)
792 { 775 {
793 res = WriteWavHeader(wav, codec_info_.plfreq, 2, channels, 776 res = WriteWavHeader(wav, codec_info_.plfreq, 2, channels,
794 kWavFormatPcm, _bytesWritten); 777 kWavFormatPcm, _bytesWritten);
795 } else if(STR_CASE_CMP(codec_info_.plname, "PCMU") == 0) { 778 } else if(STR_CASE_CMP(codec_info_.plname, "PCMU") == 0) {
796 res = WriteWavHeader(wav, 8000, 1, channels, kWavFormatMuLaw, 779 res = WriteWavHeader(wav, 8000, 1, channels, kWavFormatMuLaw,
797 _bytesWritten); 780 _bytesWritten);
798 } else if(STR_CASE_CMP(codec_info_.plname, "PCMA") == 0) { 781 } else if(STR_CASE_CMP(codec_info_.plname, "PCMA") == 0) {
799 res = WriteWavHeader(wav, 8000, 1, channels, kWavFormatALaw, 782 res = WriteWavHeader(wav, 8000, 1, channels, kWavFormatALaw,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
832 memcpy(&codec_info_,&cinst,sizeof(CodecInst)); 815 memcpy(&codec_info_,&cinst,sizeof(CodecInst));
833 _reading = true; 816 _reading = true;
834 return 0; 817 return 0;
835 } 818 }
836 819
837 int32_t ModuleFileUtility::ReadPreEncodedData( 820 int32_t ModuleFileUtility::ReadPreEncodedData(
838 InStream& in, 821 InStream& in,
839 int8_t* outData, 822 int8_t* outData,
840 const size_t bufferSize) 823 const size_t bufferSize)
841 { 824 {
842 WEBRTC_TRACE( 825 WEBRTC_TRACE(kTraceStream, kTraceFile, _id,
843 kTraceStream, 826 "ModuleFileUtility::ReadPreEncodedData(in= 0x%x, "
844 kTraceFile, 827 "outData= 0x%x, bufferSize= %" PRIuS ")", &in, outData,
845 _id, 828 bufferSize);
846 "ModuleFileUtility::ReadPreEncodedData(in= 0x%x, outData= 0x%x, "
847 "bufferSize= %" PRIuS ")",
848 &in,
849 outData,
850 bufferSize);
851 829
852 if(outData == NULL) 830 if(outData == NULL)
853 { 831 {
854 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "output buffer NULL"); 832 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "output buffer NULL");
855 } 833 }
856 834
857 uint32_t frameLen; 835 size_t frameLen;
858 uint8_t buf[64]; 836 uint8_t buf[64];
859 // Each frame has a two byte header containing the frame length. 837 // Each frame has a two byte header containing the frame length.
860 int32_t res = in.Read(buf, 2); 838 int32_t res = in.Read(buf, 2);
861 if(res != 2) 839 if(res != 2)
862 { 840 {
863 if(!in.Rewind()) 841 if(!in.Rewind())
864 { 842 {
865 // The first byte is the codec identifier. 843 // The first byte is the codec identifier.
866 in.Read(buf, 1); 844 in.Read(buf, 1);
867 res = in.Read(buf, 2); 845 res = in.Read(buf, 2);
868 } 846 }
869 else 847 else
870 { 848 {
871 return -1; 849 return -1;
872 } 850 }
873 } 851 }
874 frameLen = buf[0] + buf[1] * 256; 852 frameLen = buf[0] + buf[1] * 256;
875 if(bufferSize < frameLen) 853 if(bufferSize < frameLen)
876 { 854 {
877 WEBRTC_TRACE( 855 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
878 kTraceError, 856 "buffer not large enough to read %" PRIuS " bytes of "
879 kTraceFile, 857 "pre-encoded data!", frameLen);
880 _id,
881 "buffer not large enough to read %d bytes of pre-encoded data!",
882 frameLen);
883 return -1; 858 return -1;
884 } 859 }
885 return in.Read(outData, frameLen); 860 return in.Read(outData, frameLen);
886 } 861 }
887 862
888 int32_t ModuleFileUtility::InitPreEncodedWriting( 863 int32_t ModuleFileUtility::InitPreEncodedWriting(
889 OutStream& out, 864 OutStream& out,
890 const CodecInst& codecInst) 865 const CodecInst& codecInst)
891 { 866 {
892 867
893 if(set_codec_info(codecInst) != 0) 868 if(set_codec_info(codecInst) != 0)
894 { 869 {
895 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "CodecInst not recognized!"); 870 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "CodecInst not recognized!");
896 return -1; 871 return -1;
897 } 872 }
898 _writing = true; 873 _writing = true;
899 _bytesWritten = 1; 874 _bytesWritten = 1;
900 out.Write(&_codecId, 1); 875 out.Write(&_codecId, 1);
901 return 0; 876 return 0;
902 } 877 }
903 878
904 int32_t ModuleFileUtility::WritePreEncodedData( 879 int32_t ModuleFileUtility::WritePreEncodedData(
905 OutStream& out, 880 OutStream& out,
906 const int8_t* buffer, 881 const int8_t* buffer,
907 const size_t dataLength) 882 const size_t dataLength)
908 { 883 {
909 WEBRTC_TRACE( 884 WEBRTC_TRACE(kTraceStream, kTraceFile, _id,
910 kTraceStream, 885 "ModuleFileUtility::WritePreEncodedData(out= 0x%x, "
911 kTraceFile, 886 "inData= 0x%x, dataLen= %" PRIuS ")", &out, buffer,
912 _id, 887 dataLength);
913 "ModuleFileUtility::WritePreEncodedData(out= 0x%x, inData= 0x%x, "
914 "dataLen= %" PRIuS ")",
915 &out,
916 buffer,
917 dataLength);
918 888
919 if(buffer == NULL) 889 if(buffer == NULL)
920 { 890 {
921 WEBRTC_TRACE(kTraceError, kTraceFile, _id,"buffer NULL"); 891 WEBRTC_TRACE(kTraceError, kTraceFile, _id,"buffer NULL");
922 } 892 }
923 893
924 size_t bytesWritten = 0; 894 size_t bytesWritten = 0;
925 // The first two bytes is the size of the frame. 895 // The first two bytes is the size of the frame.
926 int16_t lengthBuf; 896 int16_t lengthBuf;
927 lengthBuf = (int16_t)dataLength; 897 lengthBuf = (int16_t)dataLength;
(...skipping 10 matching lines...) Expand all
938 } 908 }
939 bytesWritten += dataLength; 909 bytesWritten += dataLength;
940 return static_cast<int32_t>(bytesWritten); 910 return static_cast<int32_t>(bytesWritten);
941 } 911 }
942 912
943 int32_t ModuleFileUtility::InitCompressedReading( 913 int32_t ModuleFileUtility::InitCompressedReading(
944 InStream& in, 914 InStream& in,
945 const uint32_t start, 915 const uint32_t start,
946 const uint32_t stop) 916 const uint32_t stop)
947 { 917 {
948 WEBRTC_TRACE( 918 WEBRTC_TRACE(kTraceDebug, kTraceFile, _id,
949 kTraceDebug, 919 "ModuleFileUtility::InitCompressedReading(in= 0x%x, "
950 kTraceFile, 920 "start= %d, stop= %d)", &in, start, stop);
951 _id,
952 "ModuleFileUtility::InitCompressedReading(in= 0x%x, start= %d,\
953 stop= %d)",
954 &in,
955 start,
956 stop);
957 921
958 #if defined(WEBRTC_CODEC_ILBC) 922 #if defined(WEBRTC_CODEC_ILBC)
959 int16_t read_len = 0; 923 int16_t read_len = 0;
960 #endif 924 #endif
961 _codecId = kCodecNoCodec; 925 _codecId = kCodecNoCodec;
962 _playoutPositionMs = 0; 926 _playoutPositionMs = 0;
963 _reading = false; 927 _reading = false;
964 928
965 _startPointInMs = start; 929 _startPointInMs = start;
966 _stopPointInMs = stop; 930 _stopPointInMs = stop;
967 931
968 // Read the codec name 932 // Read the codec name
969 int32_t cnt = 0; 933 int32_t cnt = 0;
970 char buf[64]; 934 char buf[64];
971 do 935 do
972 { 936 {
973 in.Read(&buf[cnt++], 1); 937 in.Read(&buf[cnt++], 1);
974 } while ((buf[cnt-1] != '\n') && (64 > cnt)); 938 } while ((buf[cnt-1] != '\n') && (64 > cnt));
975 939
976 if(cnt==64) 940 if(cnt==64)
977 { 941 {
978 return -1; 942 return -1;
979 } else {
980 buf[cnt]=0;
981 } 943 }
944 buf[cnt]=0;
982 945
983 #ifdef WEBRTC_CODEC_ILBC 946 #ifdef WEBRTC_CODEC_ILBC
984 if(!strcmp("#!iLBC20\n", buf)) 947 if(!strcmp("#!iLBC20\n", buf))
985 { 948 {
986 codec_info_.pltype = 102; 949 codec_info_.pltype = 102;
987 strcpy(codec_info_.plname, "ilbc"); 950 strcpy(codec_info_.plname, "ilbc");
988 codec_info_.plfreq = 8000; 951 codec_info_.plfreq = 8000;
989 codec_info_.pacsize = 160; 952 codec_info_.pacsize = 160;
990 codec_info_.channels = 1; 953 codec_info_.channels = 1;
991 codec_info_.rate = 13300; 954 codec_info_.rate = 13300;
992 _codecId = kCodecIlbc20Ms; 955 _codecId = kCodecIlbc20Ms;
993 956
994 if(_startPointInMs > 0) 957 if(_startPointInMs > 0)
995 { 958 {
996 while (_playoutPositionMs <= _startPointInMs) 959 while (_playoutPositionMs <= _startPointInMs)
997 { 960 {
998 read_len = in.Read(buf, 38); 961 read_len = in.Read(buf, 38);
999 if(read_len == 38) 962 if(read_len != 38)
1000 {
1001 _playoutPositionMs += 20;
1002 }
1003 else
1004 { 963 {
1005 return -1; 964 return -1;
1006 } 965 }
966 _playoutPositionMs += 20;
1007 } 967 }
1008 } 968 }
1009 } 969 }
1010 970
1011 if(!strcmp("#!iLBC30\n", buf)) 971 if(!strcmp("#!iLBC30\n", buf))
1012 { 972 {
1013 codec_info_.pltype = 102; 973 codec_info_.pltype = 102;
1014 strcpy(codec_info_.plname, "ilbc"); 974 strcpy(codec_info_.plname, "ilbc");
1015 codec_info_.plfreq = 8000; 975 codec_info_.plfreq = 8000;
1016 codec_info_.pacsize = 240; 976 codec_info_.pacsize = 240;
1017 codec_info_.channels = 1; 977 codec_info_.channels = 1;
1018 codec_info_.rate = 13300; 978 codec_info_.rate = 13300;
1019 _codecId = kCodecIlbc30Ms; 979 _codecId = kCodecIlbc30Ms;
1020 980
1021 if(_startPointInMs > 0) 981 if(_startPointInMs > 0)
1022 { 982 {
1023 while (_playoutPositionMs <= _startPointInMs) 983 while (_playoutPositionMs <= _startPointInMs)
1024 { 984 {
1025 read_len = in.Read(buf, 50); 985 read_len = in.Read(buf, 50);
1026 if(read_len == 50) 986 if(read_len != 50)
1027 {
1028 _playoutPositionMs += 20;
1029 }
1030 else
1031 { 987 {
1032 return -1; 988 return -1;
1033 } 989 }
990 _playoutPositionMs += 20;
1034 } 991 }
1035 } 992 }
1036 } 993 }
1037 #endif 994 #endif
1038 if(_codecId == kCodecNoCodec) 995 if(_codecId == kCodecNoCodec)
1039 { 996 {
1040 return -1; 997 return -1;
1041 } 998 }
1042 _reading = true; 999 _reading = true;
1043 return 0; 1000 return 0;
1044 } 1001 }
1045 1002
1046 int32_t ModuleFileUtility::ReadCompressedData(InStream& in, 1003 int32_t ModuleFileUtility::ReadCompressedData(InStream& in,
1047 int8_t* outData, 1004 int8_t* outData,
1048 size_t bufferSize) 1005 size_t bufferSize)
1049 { 1006 {
1050 WEBRTC_TRACE( 1007 WEBRTC_TRACE(kTraceStream, kTraceFile, _id,
1051 kTraceStream, 1008 "ModuleFileUtility::ReadCompressedData(in=0x%x, outData=0x%x, "
1052 kTraceFile, 1009 "bytes=%" PRIuS ")", &in, outData, bufferSize);
1053 _id,
1054 "ModuleFileUtility::ReadCompressedData(in=0x%x, outData=0x%x, bytes=%"
1055 PRIuS ")",
1056 &in,
1057 outData,
1058 bufferSize);
1059 1010
1060 uint32_t bytesRead = 0; 1011 int bytesRead = 0;
1061 1012
1062 if(! _reading) 1013 if(! _reading)
1063 { 1014 {
1064 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "not currently reading!"); 1015 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "not currently reading!");
1065 return -1; 1016 return -1;
1066 } 1017 }
1067 1018
1068 #ifdef WEBRTC_CODEC_ILBC 1019 #ifdef WEBRTC_CODEC_ILBC
1069 if((_codecId == kCodecIlbc20Ms) || 1020 if((_codecId == kCodecIlbc20Ms) ||
1070 (_codecId == kCodecIlbc30Ms)) 1021 (_codecId == kCodecIlbc30Ms))
1071 { 1022 {
1072 uint32_t byteSize = 0; 1023 size_t byteSize = 0;
1073 if(_codecId == kCodecIlbc30Ms) 1024 if(_codecId == kCodecIlbc30Ms)
1074 { 1025 {
1075 byteSize = 50; 1026 byteSize = 50;
1076 } 1027 }
1077 if(_codecId == kCodecIlbc20Ms) 1028 if(_codecId == kCodecIlbc20Ms)
1078 { 1029 {
1079 byteSize = 38; 1030 byteSize = 38;
1080 } 1031 }
1081 if(bufferSize < byteSize) 1032 if(bufferSize < byteSize)
1082 { 1033 {
1083 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 1034 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
1084 "output buffer is too short to read ILBC compressed\ 1035 "output buffer is too short to read ILBC compressed "
1085 data."); 1036 "data.");
1086 assert(false); 1037 assert(false);
1087 return -1; 1038 return -1;
1088 } 1039 }
1089 1040
1090 bytesRead = in.Read(outData, byteSize); 1041 bytesRead = in.Read(outData, byteSize);
1091 if(bytesRead != byteSize) 1042 if(bytesRead != static_cast<int>(byteSize))
1092 { 1043 {
1093 if(!in.Rewind()) 1044 if(!in.Rewind())
1094 { 1045 {
1095 InitCompressedReading(in, _startPointInMs, _stopPointInMs); 1046 InitCompressedReading(in, _startPointInMs, _stopPointInMs);
1096 bytesRead = in.Read(outData, byteSize); 1047 bytesRead = in.Read(outData, byteSize);
1097 if(bytesRead != byteSize) 1048 if(bytesRead != static_cast<int>(byteSize))
1098 { 1049 {
1099 _reading = false; 1050 _reading = false;
1100 return -1; 1051 return -1;
1101 } 1052 }
1102 } 1053 }
1103 else 1054 else
1104 { 1055 {
1105 _reading = false; 1056 _reading = false;
1106 return -1; 1057 return -1;
1107 } 1058 }
(...skipping 21 matching lines...) Expand all
1129 } 1080 }
1130 } 1081 }
1131 return bytesRead; 1082 return bytesRead;
1132 } 1083 }
1133 1084
1134 int32_t ModuleFileUtility::InitCompressedWriting( 1085 int32_t ModuleFileUtility::InitCompressedWriting(
1135 OutStream& out, 1086 OutStream& out,
1136 const CodecInst& codecInst) 1087 const CodecInst& codecInst)
1137 { 1088 {
1138 WEBRTC_TRACE(kTraceDebug, kTraceFile, _id, 1089 WEBRTC_TRACE(kTraceDebug, kTraceFile, _id,
1139 "ModuleFileUtility::InitCompressedWriting(out= 0x%x,\ 1090 "ModuleFileUtility::InitCompressedWriting(out= 0x%x, "
1140 codecName= %s)", 1091 "codecName= %s)", &out, codecInst.plname);
1141 &out, codecInst.plname);
1142 1092
1143 _writing = false; 1093 _writing = false;
1144 1094
1145 #ifdef WEBRTC_CODEC_ILBC 1095 #ifdef WEBRTC_CODEC_ILBC
1146 if(STR_CASE_CMP(codecInst.plname, "ilbc") == 0) 1096 if(STR_CASE_CMP(codecInst.plname, "ilbc") == 0)
1147 { 1097 {
1148 if(codecInst.pacsize == 160) 1098 if(codecInst.pacsize == 160)
1149 { 1099 {
1150 _codecId = kCodecIlbc20Ms; 1100 _codecId = kCodecIlbc20Ms;
1151 out.Write("#!iLBC20\n",9); 1101 out.Write("#!iLBC20\n",9);
(...skipping 18 matching lines...) Expand all
1170 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 1120 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
1171 "codecInst defines unsupported compression codec!"); 1121 "codecInst defines unsupported compression codec!");
1172 return -1; 1122 return -1;
1173 } 1123 }
1174 1124
1175 int32_t ModuleFileUtility::WriteCompressedData( 1125 int32_t ModuleFileUtility::WriteCompressedData(
1176 OutStream& out, 1126 OutStream& out,
1177 const int8_t* buffer, 1127 const int8_t* buffer,
1178 const size_t dataLength) 1128 const size_t dataLength)
1179 { 1129 {
1180 WEBRTC_TRACE( 1130 WEBRTC_TRACE(kTraceStream, kTraceFile, _id,
1181 kTraceStream, 1131 "ModuleFileUtility::WriteCompressedData(out= 0x%x, buf= 0x%x, "
1182 kTraceFile, 1132 "dataLen= %" PRIuS ")", &out, buffer, dataLength);
1183 _id,
1184 "ModuleFileUtility::WriteCompressedData(out= 0x%x, buf= 0x%x, "
1185 "dataLen= %" PRIuS ")",
1186 &out,
1187 buffer,
1188 dataLength);
1189 1133
1190 if(buffer == NULL) 1134 if(buffer == NULL)
1191 { 1135 {
1192 WEBRTC_TRACE(kTraceError, kTraceFile, _id,"buffer NULL"); 1136 WEBRTC_TRACE(kTraceError, kTraceFile, _id,"buffer NULL");
1193 } 1137 }
1194 1138
1195 if(!out.Write(buffer, dataLength)) 1139 if(!out.Write(buffer, dataLength))
1196 { 1140 {
1197 return -1; 1141 return -1;
1198 } 1142 }
1199 return static_cast<int32_t>(dataLength); 1143 return static_cast<int32_t>(dataLength);
1200 } 1144 }
1201 1145
1202 int32_t ModuleFileUtility::InitPCMReading(InStream& pcm, 1146 int32_t ModuleFileUtility::InitPCMReading(InStream& pcm,
1203 const uint32_t start, 1147 const uint32_t start,
1204 const uint32_t stop, 1148 const uint32_t stop,
1205 uint32_t freq) 1149 uint32_t freq)
1206 { 1150 {
1207 WEBRTC_TRACE( 1151 WEBRTC_TRACE(kTraceInfo, kTraceFile, _id,
1208 kTraceInfo, 1152 "ModuleFileUtility::InitPCMReading(pcm= 0x%x, start=%d, "
1209 kTraceFile, 1153 "stop=%d, freq=%d)", &pcm, start, stop, freq);
1210 _id,
1211 "ModuleFileUtility::InitPCMReading(pcm= 0x%x, start=%d, stop=%d,\
1212 freq=%d)",
1213 &pcm,
1214 start,
1215 stop,
1216 freq);
1217 1154
1218 int8_t dummy[320]; 1155 int8_t dummy[320];
1219 int32_t read_len; 1156 int read_len;
1220 1157
1221 _playoutPositionMs = 0; 1158 _playoutPositionMs = 0;
1222 _startPointInMs = start; 1159 _startPointInMs = start;
1223 _stopPointInMs = stop; 1160 _stopPointInMs = stop;
1224 _reading = false; 1161 _reading = false;
1225 1162
1226 if(freq == 8000) 1163 if(freq == 8000)
1227 { 1164 {
1228 strcpy(codec_info_.plname, "L16"); 1165 strcpy(codec_info_.plname, "L16");
1229 codec_info_.pltype = -1; 1166 codec_info_.pltype = -1;
(...skipping 24 matching lines...) Expand all
1254 _codecId = kCodecL16_32Khz; 1191 _codecId = kCodecL16_32Khz;
1255 } 1192 }
1256 1193
1257 // Readsize for 10ms of audio data (2 bytes per sample). 1194 // Readsize for 10ms of audio data (2 bytes per sample).
1258 _readSizeBytes = 2 * codec_info_. plfreq / 100; 1195 _readSizeBytes = 2 * codec_info_. plfreq / 100;
1259 if(_startPointInMs > 0) 1196 if(_startPointInMs > 0)
1260 { 1197 {
1261 while (_playoutPositionMs < _startPointInMs) 1198 while (_playoutPositionMs < _startPointInMs)
1262 { 1199 {
1263 read_len = pcm.Read(dummy, _readSizeBytes); 1200 read_len = pcm.Read(dummy, _readSizeBytes);
1264 if(read_len == _readSizeBytes) 1201 if(read_len != static_cast<int>(_readSizeBytes))
1265 { 1202 {
1266 _playoutPositionMs += 10; 1203 return -1; // Must have reached EOF before start position!
1267 } 1204 }
1268 else // Must have reached EOF before start position! 1205 _playoutPositionMs += 10;
1269 {
1270 return -1;
1271 }
1272 } 1206 }
1273 } 1207 }
1274 _reading = true; 1208 _reading = true;
1275 return 0; 1209 return 0;
1276 } 1210 }
1277 1211
1278 int32_t ModuleFileUtility::ReadPCMData(InStream& pcm, 1212 int32_t ModuleFileUtility::ReadPCMData(InStream& pcm,
1279 int8_t* outData, 1213 int8_t* outData,
1280 size_t bufferSize) 1214 size_t bufferSize)
1281 { 1215 {
1282 WEBRTC_TRACE( 1216 WEBRTC_TRACE(kTraceStream, kTraceFile, _id,
1283 kTraceStream, 1217 "ModuleFileUtility::ReadPCMData(pcm= 0x%x, outData= 0x%x, "
1284 kTraceFile, 1218 "bufSize= %" PRIuS ")", &pcm, outData, bufferSize);
1285 _id,
1286 "ModuleFileUtility::ReadPCMData(pcm= 0x%x, outData= 0x%x, bufSize= %"
1287 PRIuS ")",
1288 &pcm,
1289 outData,
1290 bufferSize);
1291 1219
1292 if(outData == NULL) 1220 if(outData == NULL)
1293 { 1221 {
1294 WEBRTC_TRACE(kTraceError, kTraceFile, _id,"buffer NULL"); 1222 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "buffer NULL");
1295 } 1223 }
1296 1224
1297 // Readsize for 10ms of audio data (2 bytes per sample). 1225 // Readsize for 10ms of audio data (2 bytes per sample).
1298 uint32_t bytesRequested = 2 * codec_info_.plfreq / 100; 1226 size_t bytesRequested = static_cast<size_t>(2 * codec_info_.plfreq / 100);
1299 if(bufferSize < bytesRequested) 1227 if(bufferSize < bytesRequested)
1300 { 1228 {
1301 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 1229 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
1302 "ReadPCMData: buffer not long enough for a 10ms frame."); 1230 "ReadPCMData: buffer not long enough for a 10ms frame.");
1303 assert(false); 1231 assert(false);
1304 return -1; 1232 return -1;
1305 } 1233 }
1306 1234
1307 uint32_t bytesRead = pcm.Read(outData, bytesRequested); 1235 int bytesRead = pcm.Read(outData, bytesRequested);
1308 if(bytesRead < bytesRequested) 1236 if(bytesRead < static_cast<int>(bytesRequested))
1309 { 1237 {
1310 if(pcm.Rewind() == -1) 1238 if(pcm.Rewind() == -1)
1311 { 1239 {
1312 _reading = false; 1240 _reading = false;
1313 } 1241 }
1314 else 1242 else
1315 { 1243 {
1316 if(InitPCMReading(pcm, _startPointInMs, _stopPointInMs, 1244 if(InitPCMReading(pcm, _startPointInMs, _stopPointInMs,
1317 codec_info_.plfreq) == -1) 1245 codec_info_.plfreq) == -1)
1318 { 1246 {
1319 _reading = false; 1247 _reading = false;
1320 } 1248 }
1321 else 1249 else
1322 { 1250 {
1323 int32_t rest = bytesRequested - bytesRead; 1251 size_t rest = bytesRequested - bytesRead;
1324 int32_t len = pcm.Read(&(outData[bytesRead]), rest); 1252 int len = pcm.Read(&(outData[bytesRead]), rest);
1325 if(len == rest) 1253 if(len == static_cast<int>(rest))
1326 { 1254 {
1327 bytesRead += len; 1255 bytesRead += len;
1328 } 1256 }
1329 else 1257 else
1330 { 1258 {
1331 _reading = false; 1259 _reading = false;
1332 } 1260 }
1333 } 1261 }
1334 if(bytesRead <= 0) 1262 if(bytesRead <= 0)
1335 { 1263 {
1336 WEBRTC_TRACE(kTraceError, kTraceFile, _id, 1264 WEBRTC_TRACE(kTraceError, kTraceFile, _id,
1337 "ReadPCMData: Failed to rewind audio file."); 1265 "ReadPCMData: Failed to rewind audio file.");
1338 return -1; 1266 return -1;
1339 } 1267 }
1340 } 1268 }
1341 } 1269 }
1342 1270
1343 if(bytesRead <= 0) 1271 if(bytesRead <= 0)
1344 { 1272 {
1345 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, 1273 WEBRTC_TRACE(kTraceStream, kTraceFile, _id,
1346 "ReadPCMData: end of file"); 1274 "ReadPCMData: end of file");
1347 return -1; 1275 return -1;
1348 } 1276 }
1349 _playoutPositionMs += 10; 1277 _playoutPositionMs += 10;
1350 if(_stopPointInMs && _playoutPositionMs >= _stopPointInMs) 1278 if(_stopPointInMs && _playoutPositionMs >= _stopPointInMs)
1351 { 1279 {
1352 if(!pcm.Rewind()) 1280 if(!pcm.Rewind())
1353 { 1281 {
1354 if(InitPCMReading(pcm, _startPointInMs, _stopPointInMs, 1282 if(InitPCMReading(pcm, _startPointInMs, _stopPointInMs,
1355 codec_info_.plfreq) == -1) 1283 codec_info_.plfreq) == -1)
1356 { 1284 {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1407 } 1335 }
1408 _writing = true; 1336 _writing = true;
1409 _bytesWritten = 0; 1337 _bytesWritten = 0;
1410 return 0; 1338 return 0;
1411 } 1339 }
1412 1340
1413 int32_t ModuleFileUtility::WritePCMData(OutStream& out, 1341 int32_t ModuleFileUtility::WritePCMData(OutStream& out,
1414 const int8_t* buffer, 1342 const int8_t* buffer,
1415 const size_t dataLength) 1343 const size_t dataLength)
1416 { 1344 {
1417 WEBRTC_TRACE( 1345 WEBRTC_TRACE(kTraceStream, kTraceFile, _id,
1418 kTraceStream, 1346 "ModuleFileUtility::WritePCMData(out= 0x%x, buf= 0x%x, "
1419 kTraceFile, 1347 "dataLen= %" PRIuS ")", &out, buffer, dataLength);
1420 _id,
1421 "ModuleFileUtility::WritePCMData(out= 0x%x, buf= 0x%x, dataLen= %" PRIuS
1422 ")",
1423 &out,
1424 buffer,
1425 dataLength);
1426 1348
1427 if(buffer == NULL) 1349 if(buffer == NULL)
1428 { 1350 {
1429 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "buffer NULL"); 1351 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "buffer NULL");
1430 } 1352 }
1431 1353
1432 if(!out.Write(buffer, dataLength)) 1354 if(!out.Write(buffer, dataLength))
1433 { 1355 {
1434 return -1; 1356 return -1;
1435 } 1357 }
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
1578 case kFileFormatPcm8kHzFile: 1500 case kFileFormatPcm8kHzFile:
1579 { 1501 {
1580 // 8 samples per ms. 2 bytes per sample. 1502 // 8 samples per ms. 2 bytes per sample.
1581 int32_t denominator = 8*2; 1503 int32_t denominator = 8*2;
1582 time_in_ms = (file_size.st_size)/denominator; 1504 time_in_ms = (file_size.st_size)/denominator;
1583 break; 1505 break;
1584 } 1506 }
1585 case kFileFormatCompressedFile: 1507 case kFileFormatCompressedFile:
1586 { 1508 {
1587 int32_t cnt = 0; 1509 int32_t cnt = 0;
1588 int32_t read_len = 0; 1510 int read_len = 0;
1589 char buf[64]; 1511 char buf[64];
1590 do 1512 do
1591 { 1513 {
1592 read_len = inStreamObj->Read(&buf[cnt++], 1); 1514 read_len = inStreamObj->Read(&buf[cnt++], 1);
1593 if(read_len != 1) 1515 if(read_len != 1)
1594 { 1516 {
1595 return -1; 1517 return -1;
1596 } 1518 }
1597 } while ((buf[cnt-1] != '\n') && (64 > cnt)); 1519 } while ((buf[cnt-1] != '\n') && (64 > cnt));
1598 1520
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1635 break; 1557 break;
1636 } 1558 }
1637 inStreamObj->CloseFile(); 1559 inStreamObj->CloseFile();
1638 delete inStreamObj; 1560 delete inStreamObj;
1639 return time_in_ms; 1561 return time_in_ms;
1640 } 1562 }
1641 1563
1642 uint32_t ModuleFileUtility::PlayoutPositionMs() 1564 uint32_t ModuleFileUtility::PlayoutPositionMs()
1643 { 1565 {
1644 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, 1566 WEBRTC_TRACE(kTraceStream, kTraceFile, _id,
1645 "ModuleFileUtility::PlayoutPosition()"); 1567 "ModuleFileUtility::PlayoutPosition()");
1646 1568
1647 if(_reading) 1569 return _reading ? _playoutPositionMs : 0;
1648 {
1649 return _playoutPositionMs;
1650 }
1651 else
1652 {
1653 return 0;
1654 }
1655 } 1570 }
1656 } // namespace webrtc 1571 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698