OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 const size_t totalBytesNeeded = _readSizeBytes; | 418 const size_t totalBytesNeeded = _readSizeBytes; |
419 // The number of bytes that will be written to outData. | 419 // The number of bytes that will be written to outData. |
420 const size_t bytesRequested = (codec_info_.channels == 2) ? | 420 const size_t bytesRequested = (codec_info_.channels == 2) ? |
421 totalBytesNeeded >> 1 : totalBytesNeeded; | 421 totalBytesNeeded >> 1 : totalBytesNeeded; |
422 if(bufferSize < bytesRequested) | 422 if(bufferSize < bytesRequested) |
423 { | 423 { |
424 WEBRTC_TRACE(kTraceError, kTraceFile, _id, | 424 WEBRTC_TRACE(kTraceError, kTraceFile, _id, |
425 "ReadWavDataAsMono: output buffer is too short!"); | 425 "ReadWavDataAsMono: output buffer is too short!"); |
426 return -1; | 426 return -1; |
427 } | 427 } |
428 if(outData == NULL) | 428 if (outData == nullptr) { |
429 { | 429 WEBRTC_TRACE(kTraceError, kTraceFile, _id, |
430 WEBRTC_TRACE(kTraceError, kTraceFile, _id, | 430 "ReadWavDataAsMono: output buffer null!"); |
431 "ReadWavDataAsMono: output buffer NULL!"); | 431 return -1; |
432 return -1; | |
433 } | 432 } |
434 | 433 |
435 if(!_reading) | 434 if(!_reading) |
436 { | 435 { |
437 WEBRTC_TRACE(kTraceError, kTraceFile, _id, | 436 WEBRTC_TRACE(kTraceError, kTraceFile, _id, |
438 "ReadWavDataAsMono: no longer reading file."); | 437 "ReadWavDataAsMono: no longer reading file."); |
439 return -1; | 438 return -1; |
440 } | 439 } |
441 | 440 |
442 int32_t bytesRead = ReadWavData( | 441 int32_t bytesRead = ReadWavData( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 kTraceStream, | 486 kTraceStream, |
488 kTraceFile, | 487 kTraceFile, |
489 _id, | 488 _id, |
490 "ModuleFileUtility::ReadWavDataAsStereo(wav= 0x%x, outLeft= 0x%x, " | 489 "ModuleFileUtility::ReadWavDataAsStereo(wav= 0x%x, outLeft= 0x%x, " |
491 "outRight= 0x%x, bufSize= %" PRIuS ")", | 490 "outRight= 0x%x, bufSize= %" PRIuS ")", |
492 &wav, | 491 &wav, |
493 outDataLeft, | 492 outDataLeft, |
494 outDataRight, | 493 outDataRight, |
495 bufferSize); | 494 bufferSize); |
496 | 495 |
497 if((outDataLeft == NULL) || | 496 if ((outDataLeft == nullptr) || (outDataRight == nullptr)) { |
498 (outDataRight == NULL)) | 497 WEBRTC_TRACE(kTraceError, kTraceFile, _id, |
499 { | 498 "ReadWavDataAsMono: an input buffer is null!"); |
500 WEBRTC_TRACE(kTraceError, kTraceFile, _id, | 499 return -1; |
501 "ReadWavDataAsMono: an input buffer is NULL!"); | |
502 return -1; | |
503 } | 500 } |
504 if(codec_info_.channels != 2) | 501 if(codec_info_.channels != 2) |
505 { | 502 { |
506 WEBRTC_TRACE( | 503 WEBRTC_TRACE( |
507 kTraceError, | 504 kTraceError, |
508 kTraceFile, | 505 kTraceFile, |
509 _id, | 506 _id, |
510 "ReadWavDataAsStereo: WAV file does not contain stereo data!"); | 507 "ReadWavDataAsStereo: WAV file does not contain stereo data!"); |
511 return -1; | 508 return -1; |
512 } | 509 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 } | 570 } |
574 | 571 |
575 int32_t ModuleFileUtility::ReadWavData(InStream& wav, | 572 int32_t ModuleFileUtility::ReadWavData(InStream& wav, |
576 uint8_t* buffer, | 573 uint8_t* buffer, |
577 size_t dataLengthInBytes) | 574 size_t dataLengthInBytes) |
578 { | 575 { |
579 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, | 576 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, |
580 "ModuleFileUtility::ReadWavData(wav= 0x%x, buffer= 0x%x, " | 577 "ModuleFileUtility::ReadWavData(wav= 0x%x, buffer= 0x%x, " |
581 "dataLen= %" PRIuS ")", &wav, buffer, dataLengthInBytes); | 578 "dataLen= %" PRIuS ")", &wav, buffer, dataLengthInBytes); |
582 | 579 |
583 | 580 if (buffer == nullptr) { |
584 if(buffer == NULL) | 581 WEBRTC_TRACE(kTraceError, kTraceFile, _id, |
585 { | 582 "ReadWavDataAsMono: output buffer null!"); |
586 WEBRTC_TRACE(kTraceError, kTraceFile, _id, | 583 return -1; |
587 "ReadWavDataAsMono: output buffer NULL!"); | |
588 return -1; | |
589 } | 584 } |
590 | 585 |
591 // Make sure that a read won't return too few samples. | 586 // Make sure that a read won't return too few samples. |
592 // TODO (hellner): why not read the remaining bytes needed from the start | 587 // TODO (hellner): why not read the remaining bytes needed from the start |
593 // of the file? | 588 // of the file? |
594 if(_dataSize < (_readPos + dataLengthInBytes)) | 589 if(_dataSize < (_readPos + dataLengthInBytes)) |
595 { | 590 { |
596 // Rewind() being -1 may be due to the file not supposed to be looped. | 591 // Rewind() being -1 may be due to the file not supposed to be looped. |
597 if(wav.Rewind() == -1) | 592 if(wav.Rewind() == -1) |
598 { | 593 { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 } | 699 } |
705 | 700 |
706 int32_t ModuleFileUtility::WriteWavData(OutStream& out, | 701 int32_t ModuleFileUtility::WriteWavData(OutStream& out, |
707 const int8_t* buffer, | 702 const int8_t* buffer, |
708 const size_t dataLength) | 703 const size_t dataLength) |
709 { | 704 { |
710 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, | 705 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, |
711 "ModuleFileUtility::WriteWavData(out= 0x%x, buf= 0x%x, " | 706 "ModuleFileUtility::WriteWavData(out= 0x%x, buf= 0x%x, " |
712 "dataLen= %" PRIuS ")", &out, buffer, dataLength); | 707 "dataLen= %" PRIuS ")", &out, buffer, dataLength); |
713 | 708 |
714 if(buffer == NULL) | 709 if (buffer == nullptr) { |
715 { | 710 WEBRTC_TRACE(kTraceError, kTraceFile, _id, |
716 WEBRTC_TRACE(kTraceError, kTraceFile, _id, | 711 "WriteWavData: input buffer null!"); |
717 "WriteWavData: input buffer NULL!"); | 712 return -1; |
718 return -1; | |
719 } | 713 } |
720 | 714 |
721 if(!out.Write(buffer, dataLength)) | 715 if(!out.Write(buffer, dataLength)) |
722 { | 716 { |
723 return -1; | 717 return -1; |
724 } | 718 } |
725 _bytesWritten += dataLength; | 719 _bytesWritten += dataLength; |
726 return static_cast<int32_t>(dataLength); | 720 return static_cast<int32_t>(dataLength); |
727 } | 721 } |
728 | 722 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 int32_t ModuleFileUtility::ReadPreEncodedData( | 802 int32_t ModuleFileUtility::ReadPreEncodedData( |
809 InStream& in, | 803 InStream& in, |
810 int8_t* outData, | 804 int8_t* outData, |
811 const size_t bufferSize) | 805 const size_t bufferSize) |
812 { | 806 { |
813 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, | 807 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, |
814 "ModuleFileUtility::ReadPreEncodedData(in= 0x%x, " | 808 "ModuleFileUtility::ReadPreEncodedData(in= 0x%x, " |
815 "outData= 0x%x, bufferSize= %" PRIuS ")", &in, outData, | 809 "outData= 0x%x, bufferSize= %" PRIuS ")", &in, outData, |
816 bufferSize); | 810 bufferSize); |
817 | 811 |
818 if(outData == NULL) | 812 if (outData == nullptr) { |
819 { | 813 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "output buffer null"); |
820 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "output buffer NULL"); | |
821 } | 814 } |
822 | 815 |
823 size_t frameLen; | 816 size_t frameLen; |
824 uint8_t buf[64]; | 817 uint8_t buf[64]; |
825 // Each frame has a two byte header containing the frame length. | 818 // Each frame has a two byte header containing the frame length. |
826 int32_t res = in.Read(buf, 2); | 819 int32_t res = in.Read(buf, 2); |
827 if(res != 2) | 820 if(res != 2) |
828 { | 821 { |
829 if(!in.Rewind()) | 822 if(!in.Rewind()) |
830 { | 823 { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
867 int32_t ModuleFileUtility::WritePreEncodedData( | 860 int32_t ModuleFileUtility::WritePreEncodedData( |
868 OutStream& out, | 861 OutStream& out, |
869 const int8_t* buffer, | 862 const int8_t* buffer, |
870 const size_t dataLength) | 863 const size_t dataLength) |
871 { | 864 { |
872 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, | 865 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, |
873 "ModuleFileUtility::WritePreEncodedData(out= 0x%x, " | 866 "ModuleFileUtility::WritePreEncodedData(out= 0x%x, " |
874 "inData= 0x%x, dataLen= %" PRIuS ")", &out, buffer, | 867 "inData= 0x%x, dataLen= %" PRIuS ")", &out, buffer, |
875 dataLength); | 868 dataLength); |
876 | 869 |
877 if(buffer == NULL) | 870 if (buffer == nullptr) { |
878 { | 871 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "buffer null"); |
879 WEBRTC_TRACE(kTraceError, kTraceFile, _id,"buffer NULL"); | |
880 } | 872 } |
881 | 873 |
882 size_t bytesWritten = 0; | 874 size_t bytesWritten = 0; |
883 // The first two bytes is the size of the frame. | 875 // The first two bytes is the size of the frame. |
884 int16_t lengthBuf; | 876 int16_t lengthBuf; |
885 lengthBuf = (int16_t)dataLength; | 877 lengthBuf = (int16_t)dataLength; |
886 if(dataLength > static_cast<size_t>(std::numeric_limits<int16_t>::max()) || | 878 if(dataLength > static_cast<size_t>(std::numeric_limits<int16_t>::max()) || |
887 !out.Write(&lengthBuf, 2)) | 879 !out.Write(&lengthBuf, 2)) |
888 { | 880 { |
889 return -1; | 881 return -1; |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1112 | 1104 |
1113 int32_t ModuleFileUtility::WriteCompressedData( | 1105 int32_t ModuleFileUtility::WriteCompressedData( |
1114 OutStream& out, | 1106 OutStream& out, |
1115 const int8_t* buffer, | 1107 const int8_t* buffer, |
1116 const size_t dataLength) | 1108 const size_t dataLength) |
1117 { | 1109 { |
1118 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, | 1110 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, |
1119 "ModuleFileUtility::WriteCompressedData(out= 0x%x, buf= 0x%x, " | 1111 "ModuleFileUtility::WriteCompressedData(out= 0x%x, buf= 0x%x, " |
1120 "dataLen= %" PRIuS ")", &out, buffer, dataLength); | 1112 "dataLen= %" PRIuS ")", &out, buffer, dataLength); |
1121 | 1113 |
1122 if(buffer == NULL) | 1114 if (buffer == nullptr) { |
1123 { | 1115 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "buffer null"); |
1124 WEBRTC_TRACE(kTraceError, kTraceFile, _id,"buffer NULL"); | |
1125 } | 1116 } |
1126 | 1117 |
1127 if(!out.Write(buffer, dataLength)) | 1118 if(!out.Write(buffer, dataLength)) |
1128 { | 1119 { |
1129 return -1; | 1120 return -1; |
1130 } | 1121 } |
1131 return static_cast<int32_t>(dataLength); | 1122 return static_cast<int32_t>(dataLength); |
1132 } | 1123 } |
1133 | 1124 |
1134 int32_t ModuleFileUtility::InitPCMReading(InStream& pcm, | 1125 int32_t ModuleFileUtility::InitPCMReading(InStream& pcm, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1198 } | 1189 } |
1199 | 1190 |
1200 int32_t ModuleFileUtility::ReadPCMData(InStream& pcm, | 1191 int32_t ModuleFileUtility::ReadPCMData(InStream& pcm, |
1201 int8_t* outData, | 1192 int8_t* outData, |
1202 size_t bufferSize) | 1193 size_t bufferSize) |
1203 { | 1194 { |
1204 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, | 1195 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, |
1205 "ModuleFileUtility::ReadPCMData(pcm= 0x%x, outData= 0x%x, " | 1196 "ModuleFileUtility::ReadPCMData(pcm= 0x%x, outData= 0x%x, " |
1206 "bufSize= %" PRIuS ")", &pcm, outData, bufferSize); | 1197 "bufSize= %" PRIuS ")", &pcm, outData, bufferSize); |
1207 | 1198 |
1208 if(outData == NULL) | 1199 if (outData == nullptr) { |
1209 { | 1200 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "buffer null"); |
1210 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "buffer NULL"); | |
1211 } | 1201 } |
1212 | 1202 |
1213 // Readsize for 10ms of audio data (2 bytes per sample). | 1203 // Readsize for 10ms of audio data (2 bytes per sample). |
1214 size_t bytesRequested = static_cast<size_t>(2 * codec_info_.plfreq / 100); | 1204 size_t bytesRequested = static_cast<size_t>(2 * codec_info_.plfreq / 100); |
1215 if(bufferSize < bytesRequested) | 1205 if(bufferSize < bytesRequested) |
1216 { | 1206 { |
1217 WEBRTC_TRACE(kTraceError, kTraceFile, _id, | 1207 WEBRTC_TRACE(kTraceError, kTraceFile, _id, |
1218 "ReadPCMData: buffer not long enough for a 10ms frame."); | 1208 "ReadPCMData: buffer not long enough for a 10ms frame."); |
1219 assert(false); | 1209 assert(false); |
1220 return -1; | 1210 return -1; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1327 } | 1317 } |
1328 | 1318 |
1329 int32_t ModuleFileUtility::WritePCMData(OutStream& out, | 1319 int32_t ModuleFileUtility::WritePCMData(OutStream& out, |
1330 const int8_t* buffer, | 1320 const int8_t* buffer, |
1331 const size_t dataLength) | 1321 const size_t dataLength) |
1332 { | 1322 { |
1333 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, | 1323 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, |
1334 "ModuleFileUtility::WritePCMData(out= 0x%x, buf= 0x%x, " | 1324 "ModuleFileUtility::WritePCMData(out= 0x%x, buf= 0x%x, " |
1335 "dataLen= %" PRIuS ")", &out, buffer, dataLength); | 1325 "dataLen= %" PRIuS ")", &out, buffer, dataLength); |
1336 | 1326 |
1337 if(buffer == NULL) | 1327 if (buffer == nullptr) { |
1338 { | 1328 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "buffer null"); |
1339 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "buffer NULL"); | |
1340 } | 1329 } |
1341 | 1330 |
1342 if(!out.Write(buffer, dataLength)) | 1331 if(!out.Write(buffer, dataLength)) |
1343 { | 1332 { |
1344 return -1; | 1333 return -1; |
1345 } | 1334 } |
1346 | 1335 |
1347 _bytesWritten += dataLength; | 1336 _bytesWritten += dataLength; |
1348 return static_cast<int32_t>(dataLength); | 1337 return static_cast<int32_t>(dataLength); |
1349 } | 1338 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1427 return -1; | 1416 return -1; |
1428 } | 1417 } |
1429 memcpy(&codec_info_, &codecInst, sizeof(CodecInst)); | 1418 memcpy(&codec_info_, &codecInst, sizeof(CodecInst)); |
1430 return 0; | 1419 return 0; |
1431 } | 1420 } |
1432 | 1421 |
1433 int32_t ModuleFileUtility::FileDurationMs(const char* fileName, | 1422 int32_t ModuleFileUtility::FileDurationMs(const char* fileName, |
1434 const FileFormats fileFormat, | 1423 const FileFormats fileFormat, |
1435 const uint32_t freqInHz) | 1424 const uint32_t freqInHz) |
1436 { | 1425 { |
1437 | 1426 if (fileName == nullptr) { |
1438 if(fileName == NULL) | 1427 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "filename null"); |
1439 { | 1428 return -1; |
1440 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "filename NULL"); | |
1441 return -1; | |
1442 } | 1429 } |
1443 | 1430 |
1444 int32_t time_in_ms = -1; | 1431 int32_t time_in_ms = -1; |
1445 struct stat file_size; | 1432 struct stat file_size; |
1446 if(stat(fileName,&file_size) == -1) | 1433 if(stat(fileName,&file_size) == -1) |
1447 { | 1434 { |
1448 WEBRTC_TRACE(kTraceError, kTraceFile, _id, | 1435 WEBRTC_TRACE(kTraceError, kTraceFile, _id, |
1449 "failed to retrieve file size with stat!"); | 1436 "failed to retrieve file size with stat!"); |
1450 return -1; | 1437 return -1; |
1451 } | 1438 } |
1452 FileWrapper* inStreamObj = FileWrapper::Create(); | 1439 FileWrapper* inStreamObj = FileWrapper::Create(); |
1453 if(inStreamObj == NULL) | 1440 if (inStreamObj == nullptr) { |
1454 { | 1441 WEBRTC_TRACE(kTraceMemory, kTraceFile, _id, |
1455 WEBRTC_TRACE(kTraceMemory, kTraceFile, _id, | 1442 "failed to create InStream object!"); |
1456 "failed to create InStream object!"); | 1443 return -1; |
1457 return -1; | |
1458 } | 1444 } |
1459 if (!inStreamObj->OpenFile(fileName, true)) { | 1445 if (!inStreamObj->OpenFile(fileName, true)) { |
1460 delete inStreamObj; | 1446 delete inStreamObj; |
1461 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "failed to open file %s!", | 1447 WEBRTC_TRACE(kTraceError, kTraceFile, _id, "failed to open file %s!", |
1462 fileName); | 1448 fileName); |
1463 return -1; | 1449 return -1; |
1464 } | 1450 } |
1465 | 1451 |
1466 switch (fileFormat) | 1452 switch (fileFormat) |
1467 { | 1453 { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1549 } | 1535 } |
1550 | 1536 |
1551 uint32_t ModuleFileUtility::PlayoutPositionMs() | 1537 uint32_t ModuleFileUtility::PlayoutPositionMs() |
1552 { | 1538 { |
1553 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, | 1539 WEBRTC_TRACE(kTraceStream, kTraceFile, _id, |
1554 "ModuleFileUtility::PlayoutPosition()"); | 1540 "ModuleFileUtility::PlayoutPosition()"); |
1555 | 1541 |
1556 return _reading ? _playoutPositionMs : 0; | 1542 return _reading ? _playoutPositionMs : 0; |
1557 } | 1543 } |
1558 } // namespace webrtc | 1544 } // namespace webrtc |
OLD | NEW |