niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
xians@webrtc.org | 03039d5 | 2012-02-20 08:37:49 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 Kjellander | 0b9e29c | 2015-11-16 11:12:24 +0100 | [diff] [blame] | 11 | #include "webrtc/modules/media_file/media_file_utility.h" |
stefan@webrtc.org | b082ade | 2013-11-18 11:45:11 +0000 | [diff] [blame] | 12 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | #include <assert.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <sys/types.h> |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 16 | #include <limits> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 18 | #include "webrtc/common_audio/wav_header.h" |
pbos@webrtc.org | 0c4e05a | 2013-07-16 13:05:40 +0000 | [diff] [blame] | 19 | #include "webrtc/common_types.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 20 | #include "webrtc/modules/include/module_common_types.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 21 | #include "webrtc/rtc_base/format_macros.h" |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 22 | #include "webrtc/rtc_base/logging.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 23 | #include "webrtc/system_wrappers/include/file_wrapper.h" |
henrik.lundin | a9a6d4b | 2016-12-12 05:03:02 -0800 | [diff] [blame] | 24 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | namespace { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
| 28 | // First 16 bytes the WAVE header. ckID should be "RIFF", wave_ckID should be |
| 29 | // "WAVE" and ckSize is the chunk size (4 + n) |
| 30 | struct WAVE_RIFF_header |
| 31 | { |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 32 | int8_t ckID[4]; |
| 33 | int32_t ckSize; |
| 34 | int8_t wave_ckID[4]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | // First 8 byte of the format chunk. fmt_ckID should be "fmt ". fmt_ckSize is |
| 38 | // the chunk size (16, 18 or 40 byte) |
| 39 | struct WAVE_CHUNK_header |
| 40 | { |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 41 | int8_t fmt_ckID[4]; |
| 42 | uint32_t fmt_ckSize; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | }; |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 44 | } // unnamed namespace |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | |
| 46 | namespace webrtc { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 47 | ModuleFileUtility::ModuleFileUtility() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | : _wavFormatObj(), |
| 49 | _dataSize(0), |
| 50 | _readSizeBytes(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | _stopPointInMs(0), |
| 52 | _startPointInMs(0), |
| 53 | _playoutPositionMs(0), |
| 54 | _bytesWritten(0), |
| 55 | codec_info_(), |
| 56 | _codecId(kCodecNoCodec), |
| 57 | _bytesPerSample(0), |
| 58 | _readPos(0), |
| 59 | _reading(false), |
| 60 | _writing(false), |
andresp@webrtc.org | e8f50df | 2015-03-02 13:07:02 +0000 | [diff] [blame] | 61 | _tempData() { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 62 | LOG(LS_INFO) << "ModuleFileUtility::ModuleFileUtility()"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | memset(&codec_info_,0,sizeof(CodecInst)); |
| 64 | codec_info_.pltype = -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | ModuleFileUtility::~ModuleFileUtility() |
| 68 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 69 | LOG(LS_INFO) << "ModuleFileUtility::~ModuleFileUtility()"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 72 | int32_t ModuleFileUtility::ReadWavHeader(InStream& wav) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | { |
| 74 | WAVE_RIFF_header RIFFheaderObj; |
| 75 | WAVE_CHUNK_header CHUNKheaderObj; |
| 76 | // TODO (hellner): tmpStr and tmpStr2 seems unnecessary here. |
leozwang@webrtc.org | 09e7719 | 2012-03-01 18:35:54 +0000 | [diff] [blame] | 77 | char tmpStr[6] = "FOUR"; |
leozwang@webrtc.org | 0dc8efe | 2012-04-03 15:11:01 +0000 | [diff] [blame] | 78 | unsigned char tmpStr2[4]; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 79 | size_t i; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | bool dataFound = false; |
| 81 | bool fmtFound = false; |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 82 | int8_t dummyRead; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | |
| 85 | _dataSize = 0; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 86 | int len = wav.Read(&RIFFheaderObj, sizeof(WAVE_RIFF_header)); |
| 87 | if (len != static_cast<int>(sizeof(WAVE_RIFF_header))) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 89 | LOG(LS_ERROR) << "Not a wave file (too short)"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | for (i = 0; i < 4; i++) |
| 94 | { |
| 95 | tmpStr[i] = RIFFheaderObj.ckID[i]; |
| 96 | } |
| 97 | if(strcmp(tmpStr, "RIFF") != 0) |
| 98 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 99 | LOG(LS_ERROR) << "Not a wave file (does not have RIFF)"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | return -1; |
| 101 | } |
| 102 | for (i = 0; i < 4; i++) |
| 103 | { |
| 104 | tmpStr[i] = RIFFheaderObj.wave_ckID[i]; |
| 105 | } |
| 106 | if(strcmp(tmpStr, "WAVE") != 0) |
| 107 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 108 | LOG(LS_ERROR) << "Not a wave file (does not have WAVE)"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | len = wav.Read(&CHUNKheaderObj, sizeof(WAVE_CHUNK_header)); |
| 113 | |
| 114 | // WAVE files are stored in little endian byte order. Make sure that the |
| 115 | // data can be read on big endian as well. |
| 116 | // TODO (hellner): little endian to system byte order should be done in |
| 117 | // in a subroutine. |
| 118 | memcpy(tmpStr2, &CHUNKheaderObj.fmt_ckSize, 4); |
| 119 | CHUNKheaderObj.fmt_ckSize = |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 120 | (uint32_t)tmpStr2[0] + (((uint32_t)tmpStr2[1]) << 8) + |
| 121 | (((uint32_t)tmpStr2[2]) << 16) + (((uint32_t)tmpStr2[3]) << 24); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | |
| 123 | memcpy(tmpStr, CHUNKheaderObj.fmt_ckID, 4); |
| 124 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 125 | while ((len == static_cast<int>(sizeof(WAVE_CHUNK_header))) && |
| 126 | (!fmtFound || !dataFound)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 127 | { |
| 128 | if(strcmp(tmpStr, "fmt ") == 0) |
| 129 | { |
| 130 | len = wav.Read(&_wavFormatObj, sizeof(WAVE_FMTINFO_header)); |
| 131 | |
| 132 | memcpy(tmpStr2, &_wavFormatObj.formatTag, 2); |
| 133 | _wavFormatObj.formatTag = |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 134 | (uint32_t)tmpStr2[0] + (((uint32_t)tmpStr2[1])<<8); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 135 | memcpy(tmpStr2, &_wavFormatObj.nChannels, 2); |
| 136 | _wavFormatObj.nChannels = |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 137 | (int16_t) ((uint32_t)tmpStr2[0] + |
| 138 | (((uint32_t)tmpStr2[1])<<8)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | memcpy(tmpStr2, &_wavFormatObj.nSamplesPerSec, 4); |
| 140 | _wavFormatObj.nSamplesPerSec = |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 141 | (int32_t) ((uint32_t)tmpStr2[0] + |
| 142 | (((uint32_t)tmpStr2[1])<<8) + |
| 143 | (((uint32_t)tmpStr2[2])<<16) + |
| 144 | (((uint32_t)tmpStr2[3])<<24)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | memcpy(tmpStr2, &_wavFormatObj.nAvgBytesPerSec, 4); |
| 146 | _wavFormatObj.nAvgBytesPerSec = |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 147 | (int32_t) ((uint32_t)tmpStr2[0] + |
| 148 | (((uint32_t)tmpStr2[1])<<8) + |
| 149 | (((uint32_t)tmpStr2[2])<<16) + |
| 150 | (((uint32_t)tmpStr2[3])<<24)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | memcpy(tmpStr2, &_wavFormatObj.nBlockAlign, 2); |
| 152 | _wavFormatObj.nBlockAlign = |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 153 | (int16_t) ((uint32_t)tmpStr2[0] + |
| 154 | (((uint32_t)tmpStr2[1])<<8)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 155 | memcpy(tmpStr2, &_wavFormatObj.nBitsPerSample, 2); |
| 156 | _wavFormatObj.nBitsPerSample = |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 157 | (int16_t) ((uint32_t)tmpStr2[0] + |
| 158 | (((uint32_t)tmpStr2[1])<<8)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 160 | if (CHUNKheaderObj.fmt_ckSize < sizeof(WAVE_FMTINFO_header)) |
| 161 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 162 | LOG(LS_ERROR) << "Chunk size is too small"; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 163 | return -1; |
| 164 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 165 | for (i = 0; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 166 | i < CHUNKheaderObj.fmt_ckSize - sizeof(WAVE_FMTINFO_header); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | i++) |
| 168 | { |
| 169 | len = wav.Read(&dummyRead, 1); |
| 170 | if(len != 1) |
| 171 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 172 | LOG(LS_ERROR) |
| 173 | << "File corrupted, reached EOF (reading fmt)"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 174 | return -1; |
| 175 | } |
| 176 | } |
| 177 | fmtFound = true; |
| 178 | } |
| 179 | else if(strcmp(tmpStr, "data") == 0) |
| 180 | { |
| 181 | _dataSize = CHUNKheaderObj.fmt_ckSize; |
| 182 | dataFound = true; |
| 183 | break; |
| 184 | } |
| 185 | else |
| 186 | { |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 187 | for (i = 0; i < CHUNKheaderObj.fmt_ckSize; i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | { |
| 189 | len = wav.Read(&dummyRead, 1); |
| 190 | if(len != 1) |
| 191 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 192 | LOG(LS_ERROR) |
| 193 | << "File corrupted, reached EOF (reading other)"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | len = wav.Read(&CHUNKheaderObj, sizeof(WAVE_CHUNK_header)); |
| 200 | |
| 201 | memcpy(tmpStr2, &CHUNKheaderObj.fmt_ckSize, 4); |
| 202 | CHUNKheaderObj.fmt_ckSize = |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 203 | (uint32_t)tmpStr2[0] + (((uint32_t)tmpStr2[1]) << 8) + |
| 204 | (((uint32_t)tmpStr2[2]) << 16) + (((uint32_t)tmpStr2[3]) << 24); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | |
| 206 | memcpy(tmpStr, CHUNKheaderObj.fmt_ckID, 4); |
| 207 | } |
| 208 | |
| 209 | // Either a proper format chunk has been read or a data chunk was come |
| 210 | // across. |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 211 | if( (_wavFormatObj.formatTag != kWavFormatPcm) && |
| 212 | (_wavFormatObj.formatTag != kWavFormatALaw) && |
| 213 | (_wavFormatObj.formatTag != kWavFormatMuLaw)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 214 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 215 | LOG(LS_ERROR) << "Coding formatTag value=" << _wavFormatObj.formatTag |
| 216 | << " not supported!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 217 | return -1; |
| 218 | } |
| 219 | if((_wavFormatObj.nChannels < 1) || |
| 220 | (_wavFormatObj.nChannels > 2)) |
| 221 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 222 | LOG(LS_ERROR) << "nChannels value=" << _wavFormatObj.nChannels |
| 223 | << " not supported!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | if((_wavFormatObj.nBitsPerSample != 8) && |
| 228 | (_wavFormatObj.nBitsPerSample != 16)) |
| 229 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 230 | LOG(LS_ERROR) << "nBitsPerSample value=" << _wavFormatObj.nBitsPerSample |
| 231 | << " not supported!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | // Calculate the number of bytes that 10 ms of audio data correspond to. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 236 | size_t samples_per_10ms = |
| 237 | ((_wavFormatObj.formatTag == kWavFormatPcm) && |
| 238 | (_wavFormatObj.nSamplesPerSec == 44100)) ? |
| 239 | 440 : static_cast<size_t>(_wavFormatObj.nSamplesPerSec / 100); |
| 240 | _readSizeBytes = samples_per_10ms * _wavFormatObj.nChannels * |
| 241 | (_wavFormatObj.nBitsPerSample / 8); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 245 | int32_t ModuleFileUtility::InitWavCodec(uint32_t samplesPerSec, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 246 | size_t channels, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 247 | uint32_t bitsPerSample, |
| 248 | uint32_t formatTag) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 249 | { |
| 250 | codec_info_.pltype = -1; |
| 251 | codec_info_.plfreq = samplesPerSec; |
| 252 | codec_info_.channels = channels; |
| 253 | codec_info_.rate = bitsPerSample * samplesPerSec; |
| 254 | |
| 255 | // Calculate the packet size for 10ms frames |
| 256 | switch(formatTag) |
| 257 | { |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 258 | case kWavFormatALaw: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 259 | strcpy(codec_info_.plname, "PCMA"); |
| 260 | _codecId = kCodecPcma; |
| 261 | codec_info_.pltype = 8; |
| 262 | codec_info_.pacsize = codec_info_.plfreq / 100; |
| 263 | break; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 264 | case kWavFormatMuLaw: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 265 | strcpy(codec_info_.plname, "PCMU"); |
| 266 | _codecId = kCodecPcmu; |
| 267 | codec_info_.pltype = 0; |
| 268 | codec_info_.pacsize = codec_info_.plfreq / 100; |
| 269 | break; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 270 | case kWavFormatPcm: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 271 | codec_info_.pacsize = (bitsPerSample * (codec_info_.plfreq / 100)) / 8; |
| 272 | if(samplesPerSec == 8000) |
| 273 | { |
| 274 | strcpy(codec_info_.plname, "L16"); |
| 275 | _codecId = kCodecL16_8Khz; |
| 276 | } |
| 277 | else if(samplesPerSec == 16000) |
| 278 | { |
| 279 | strcpy(codec_info_.plname, "L16"); |
| 280 | _codecId = kCodecL16_16kHz; |
| 281 | } |
| 282 | else if(samplesPerSec == 32000) |
| 283 | { |
| 284 | strcpy(codec_info_.plname, "L16"); |
| 285 | _codecId = kCodecL16_32Khz; |
| 286 | } |
| 287 | // Set the packet size for "odd" sampling frequencies so that it |
| 288 | // properly corresponds to _readSizeBytes. |
| 289 | else if(samplesPerSec == 11025) |
| 290 | { |
| 291 | strcpy(codec_info_.plname, "L16"); |
| 292 | _codecId = kCodecL16_16kHz; |
| 293 | codec_info_.pacsize = 110; |
| 294 | codec_info_.plfreq = 11000; |
| 295 | } |
| 296 | else if(samplesPerSec == 22050) |
| 297 | { |
| 298 | strcpy(codec_info_.plname, "L16"); |
| 299 | _codecId = kCodecL16_16kHz; |
| 300 | codec_info_.pacsize = 220; |
| 301 | codec_info_.plfreq = 22000; |
| 302 | } |
| 303 | else if(samplesPerSec == 44100) |
| 304 | { |
| 305 | strcpy(codec_info_.plname, "L16"); |
| 306 | _codecId = kCodecL16_16kHz; |
| 307 | codec_info_.pacsize = 440; |
| 308 | codec_info_.plfreq = 44000; |
| 309 | } |
| 310 | else if(samplesPerSec == 48000) |
| 311 | { |
| 312 | strcpy(codec_info_.plname, "L16"); |
| 313 | _codecId = kCodecL16_16kHz; |
| 314 | codec_info_.pacsize = 480; |
| 315 | codec_info_.plfreq = 48000; |
| 316 | } |
| 317 | else |
| 318 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 319 | LOG(LS_ERROR) << "Unsupported PCM frequency!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 320 | return -1; |
| 321 | } |
| 322 | break; |
| 323 | default: |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 324 | LOG(LS_ERROR) << "unknown WAV format TAG!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 325 | return -1; |
| 326 | break; |
| 327 | } |
| 328 | return 0; |
| 329 | } |
| 330 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 331 | int32_t ModuleFileUtility::InitWavReading(InStream& wav, |
| 332 | const uint32_t start, |
| 333 | const uint32_t stop) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 334 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 335 | |
| 336 | _reading = false; |
| 337 | |
| 338 | if(ReadWavHeader(wav) == -1) |
| 339 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 340 | LOG(LS_ERROR) << "failed to read WAV header!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 341 | return -1; |
| 342 | } |
| 343 | |
| 344 | _playoutPositionMs = 0; |
| 345 | _readPos = 0; |
| 346 | |
| 347 | if(start > 0) |
| 348 | { |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 349 | uint8_t dummy[WAV_MAX_BUFFER_SIZE]; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 350 | int readLength; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 351 | if(_readSizeBytes <= WAV_MAX_BUFFER_SIZE) |
| 352 | { |
| 353 | while (_playoutPositionMs < start) |
| 354 | { |
| 355 | readLength = wav.Read(dummy, _readSizeBytes); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 356 | if(readLength == static_cast<int>(_readSizeBytes)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 357 | { |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 358 | _readPos += _readSizeBytes; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 359 | _playoutPositionMs += 10; |
| 360 | } |
| 361 | else // Must have reached EOF before start position! |
| 362 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 363 | LOG(LS_ERROR) |
| 364 | << "InitWavReading(), EOF before start position"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 365 | return -1; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | return -1; |
| 372 | } |
| 373 | } |
| 374 | if( InitWavCodec(_wavFormatObj.nSamplesPerSec, _wavFormatObj.nChannels, |
| 375 | _wavFormatObj.nBitsPerSample, |
| 376 | _wavFormatObj.formatTag) != 0) |
| 377 | { |
| 378 | return -1; |
| 379 | } |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 380 | _bytesPerSample = static_cast<size_t>(_wavFormatObj.nBitsPerSample / 8); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 381 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 382 | |
| 383 | _startPointInMs = start; |
| 384 | _stopPointInMs = stop; |
| 385 | _reading = true; |
| 386 | return 0; |
| 387 | } |
| 388 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 389 | int32_t ModuleFileUtility::ReadWavDataAsMono( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 390 | InStream& wav, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 391 | int8_t* outData, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 392 | const size_t bufferSize) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 393 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 394 | LOG(LS_VERBOSE) << "ModuleFileUtility::ReadWavDataAsMono(wav= " << &wav |
| 395 | << ", outData= " << static_cast<void*>(outData) |
| 396 | << ", bufSize= " << bufferSize << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 397 | |
| 398 | // The number of bytes that should be read from file. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 399 | const size_t totalBytesNeeded = _readSizeBytes; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 400 | // The number of bytes that will be written to outData. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 401 | const size_t bytesRequested = (codec_info_.channels == 2) ? |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 402 | totalBytesNeeded >> 1 : totalBytesNeeded; |
| 403 | if(bufferSize < bytesRequested) |
| 404 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 405 | LOG(LS_ERROR) << "ReadWavDataAsMono: output buffer is too short!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 406 | return -1; |
| 407 | } |
| 408 | if(outData == NULL) |
| 409 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 410 | LOG(LS_ERROR) << "ReadWavDataAsMono: output buffer NULL!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 411 | return -1; |
| 412 | } |
| 413 | |
| 414 | if(!_reading) |
| 415 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 416 | LOG(LS_ERROR) << "ReadWavDataAsMono: no longer reading file."; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 417 | return -1; |
| 418 | } |
| 419 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 420 | int32_t bytesRead = ReadWavData( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 421 | wav, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 422 | (codec_info_.channels == 2) ? _tempData : (uint8_t*)outData, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 423 | totalBytesNeeded); |
| 424 | if(bytesRead == 0) |
| 425 | { |
| 426 | return 0; |
| 427 | } |
| 428 | if(bytesRead < 0) |
| 429 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 430 | LOG(LS_ERROR) |
| 431 | << "ReadWavDataAsMono: failed to read data from WAV file."; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 432 | return -1; |
| 433 | } |
| 434 | // Output data is should be mono. |
| 435 | if(codec_info_.channels == 2) |
| 436 | { |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 437 | for (size_t i = 0; i < bytesRequested / _bytesPerSample; i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 438 | { |
| 439 | // Sample value is the average of left and right buffer rounded to |
| 440 | // closest integer value. Note samples can be either 1 or 2 byte. |
| 441 | if(_bytesPerSample == 1) |
| 442 | { |
| 443 | _tempData[i] = ((_tempData[2 * i] + _tempData[(2 * i) + 1] + |
| 444 | 1) >> 1); |
| 445 | } |
| 446 | else |
| 447 | { |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 448 | int16_t* sampleData = (int16_t*) _tempData; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 449 | sampleData[i] = ((sampleData[2 * i] + sampleData[(2 * i) + 1] + |
| 450 | 1) >> 1); |
| 451 | } |
| 452 | } |
| 453 | memcpy(outData, _tempData, bytesRequested); |
| 454 | } |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 455 | return static_cast<int32_t>(bytesRequested); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 456 | } |
| 457 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 458 | int32_t ModuleFileUtility::ReadWavDataAsStereo( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 459 | InStream& wav, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 460 | int8_t* outDataLeft, |
| 461 | int8_t* outDataRight, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 462 | const size_t bufferSize) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 463 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 464 | LOG(LS_VERBOSE) << "ModuleFileUtility::ReadWavDataAsStereo(wav= " << &wav |
| 465 | << ", outLeft= " << static_cast<void*>(outDataLeft) |
| 466 | << ", outRight= " << static_cast<void*>(outDataRight) |
| 467 | << ", bufSize= " << bufferSize << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 468 | |
| 469 | if((outDataLeft == NULL) || |
| 470 | (outDataRight == NULL)) |
| 471 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 472 | LOG(LS_ERROR) << "ReadWavDataAsStereo: an input buffer is NULL!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 473 | return -1; |
| 474 | } |
| 475 | if(codec_info_.channels != 2) |
| 476 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 477 | LOG(LS_ERROR) |
| 478 | << "ReadWavDataAsStereo: WAV file does not contain stereo data!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 479 | return -1; |
| 480 | } |
| 481 | if(! _reading) |
| 482 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 483 | LOG(LS_ERROR) << "ReadWavDataAsStereo: no longer reading file."; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 484 | return -1; |
| 485 | } |
| 486 | |
| 487 | // The number of bytes that should be read from file. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 488 | const size_t totalBytesNeeded = _readSizeBytes; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 489 | // The number of bytes that will be written to the left and the right |
| 490 | // buffers. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 491 | const size_t bytesRequested = totalBytesNeeded >> 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 492 | if(bufferSize < bytesRequested) |
| 493 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 494 | LOG(LS_ERROR) << "ReadWavDataAsStereo: Output buffers are too short!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 495 | assert(false); |
| 496 | return -1; |
| 497 | } |
| 498 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 499 | int32_t bytesRead = ReadWavData(wav, _tempData, totalBytesNeeded); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 500 | if(bytesRead <= 0) |
| 501 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 502 | LOG(LS_ERROR) |
| 503 | << "ReadWavDataAsStereo: failed to read data from WAV file."; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 504 | return -1; |
| 505 | } |
| 506 | |
| 507 | // Turn interleaved audio to left and right buffer. Note samples can be |
| 508 | // either 1 or 2 bytes |
| 509 | if(_bytesPerSample == 1) |
| 510 | { |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 511 | for (size_t i = 0; i < bytesRequested; i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 512 | { |
| 513 | outDataLeft[i] = _tempData[2 * i]; |
| 514 | outDataRight[i] = _tempData[(2 * i) + 1]; |
| 515 | } |
| 516 | } |
| 517 | else if(_bytesPerSample == 2) |
| 518 | { |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 519 | int16_t* sampleData = reinterpret_cast<int16_t*>(_tempData); |
| 520 | int16_t* outLeft = reinterpret_cast<int16_t*>(outDataLeft); |
| 521 | int16_t* outRight = reinterpret_cast<int16_t*>( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 522 | outDataRight); |
| 523 | |
| 524 | // Bytes requested to samples requested. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 525 | size_t sampleCount = bytesRequested >> 1; |
| 526 | for (size_t i = 0; i < sampleCount; i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 527 | { |
| 528 | outLeft[i] = sampleData[2 * i]; |
| 529 | outRight[i] = sampleData[(2 * i) + 1]; |
| 530 | } |
| 531 | } else { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 532 | LOG(LS_ERROR) << "ReadWavStereoData: unsupported sample size " |
| 533 | << _bytesPerSample << "!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 534 | assert(false); |
| 535 | return -1; |
| 536 | } |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 537 | return static_cast<int32_t>(bytesRequested); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 538 | } |
| 539 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 540 | int32_t ModuleFileUtility::ReadWavData(InStream& wav, |
| 541 | uint8_t* buffer, |
| 542 | size_t dataLengthInBytes) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 543 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 544 | LOG(LS_VERBOSE) << "ModuleFileUtility::ReadWavData(wav= " << &wav |
| 545 | << ", buffer= " << static_cast<void*>(buffer) |
| 546 | << ", dataLen= " << dataLengthInBytes << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 547 | |
| 548 | if(buffer == NULL) |
| 549 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 550 | LOG(LS_ERROR) << "ReadWavDataAsMono: output buffer NULL!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 551 | return -1; |
| 552 | } |
| 553 | |
| 554 | // Make sure that a read won't return too few samples. |
| 555 | // TODO (hellner): why not read the remaining bytes needed from the start |
| 556 | // of the file? |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 557 | if(_dataSize < (_readPos + dataLengthInBytes)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 558 | { |
| 559 | // Rewind() being -1 may be due to the file not supposed to be looped. |
| 560 | if(wav.Rewind() == -1) |
| 561 | { |
| 562 | _reading = false; |
| 563 | return 0; |
| 564 | } |
| 565 | if(InitWavReading(wav, _startPointInMs, _stopPointInMs) == -1) |
| 566 | { |
| 567 | _reading = false; |
| 568 | return -1; |
| 569 | } |
| 570 | } |
| 571 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 572 | int32_t bytesRead = wav.Read(buffer, dataLengthInBytes); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 573 | if(bytesRead < 0) |
| 574 | { |
| 575 | _reading = false; |
| 576 | return -1; |
| 577 | } |
| 578 | |
| 579 | // This should never happen due to earlier sanity checks. |
| 580 | // TODO (hellner): change to an assert and fail here since this should |
| 581 | // never happen... |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 582 | if(bytesRead < (int32_t)dataLengthInBytes) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 583 | { |
| 584 | if((wav.Rewind() == -1) || |
| 585 | (InitWavReading(wav, _startPointInMs, _stopPointInMs) == -1)) |
| 586 | { |
| 587 | _reading = false; |
| 588 | return -1; |
| 589 | } |
| 590 | else |
| 591 | { |
| 592 | bytesRead = wav.Read(buffer, dataLengthInBytes); |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 593 | if(bytesRead < (int32_t)dataLengthInBytes) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 594 | { |
| 595 | _reading = false; |
| 596 | return -1; |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | _readPos += bytesRead; |
| 602 | |
| 603 | // TODO (hellner): Why is dataLengthInBytes let dictate the number of bytes |
| 604 | // to read when exactly 10ms should be read?! |
| 605 | _playoutPositionMs += 10; |
| 606 | if((_stopPointInMs > 0) && |
| 607 | (_playoutPositionMs >= _stopPointInMs)) |
| 608 | { |
| 609 | if((wav.Rewind() == -1) || |
| 610 | (InitWavReading(wav, _startPointInMs, _stopPointInMs) == -1)) |
| 611 | { |
| 612 | _reading = false; |
| 613 | } |
| 614 | } |
| 615 | return bytesRead; |
| 616 | } |
| 617 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 618 | int32_t ModuleFileUtility::InitWavWriting(OutStream& wav, |
| 619 | const CodecInst& codecInst) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 620 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 621 | |
| 622 | if(set_codec_info(codecInst) != 0) |
| 623 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 624 | LOG(LS_ERROR) << "codecInst identifies unsupported codec!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 625 | return -1; |
| 626 | } |
| 627 | _writing = false; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 628 | size_t channels = (codecInst.channels == 0) ? 1 : codecInst.channels; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 629 | |
| 630 | if(STR_CASE_CMP(codecInst.plname, "PCMU") == 0) |
| 631 | { |
| 632 | _bytesPerSample = 1; |
| 633 | if(WriteWavHeader(wav, 8000, _bytesPerSample, channels, |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 634 | kWavFormatMuLaw, 0) == -1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 635 | { |
| 636 | return -1; |
| 637 | } |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 638 | } |
| 639 | else if(STR_CASE_CMP(codecInst.plname, "PCMA") == 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 640 | { |
| 641 | _bytesPerSample = 1; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 642 | if(WriteWavHeader(wav, 8000, _bytesPerSample, channels, kWavFormatALaw, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 643 | 0) == -1) |
| 644 | { |
| 645 | return -1; |
| 646 | } |
| 647 | } |
| 648 | else if(STR_CASE_CMP(codecInst.plname, "L16") == 0) |
| 649 | { |
| 650 | _bytesPerSample = 2; |
| 651 | if(WriteWavHeader(wav, codecInst.plfreq, _bytesPerSample, channels, |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 652 | kWavFormatPcm, 0) == -1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 653 | { |
| 654 | return -1; |
| 655 | } |
| 656 | } |
| 657 | else |
| 658 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 659 | LOG(LS_ERROR) << "codecInst identifies unsupported codec for WAV file!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 660 | return -1; |
| 661 | } |
| 662 | _writing = true; |
| 663 | _bytesWritten = 0; |
| 664 | return 0; |
| 665 | } |
| 666 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 667 | int32_t ModuleFileUtility::WriteWavData(OutStream& out, |
| 668 | const int8_t* buffer, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 669 | const size_t dataLength) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 670 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 671 | LOG(LS_VERBOSE) << "ModuleFileUtility::WriteWavData(out= " << &out |
| 672 | << ", buf= " << static_cast<const void*>(buffer) |
| 673 | << ", dataLen= " << dataLength << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 674 | |
| 675 | if(buffer == NULL) |
| 676 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 677 | LOG(LS_ERROR) << "WriteWavData: input buffer NULL!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 678 | return -1; |
| 679 | } |
| 680 | |
| 681 | if(!out.Write(buffer, dataLength)) |
| 682 | { |
| 683 | return -1; |
| 684 | } |
| 685 | _bytesWritten += dataLength; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 686 | return static_cast<int32_t>(dataLength); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 690 | int32_t ModuleFileUtility::WriteWavHeader( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 691 | OutStream& wav, |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 692 | uint32_t freq, |
| 693 | size_t bytesPerSample, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 694 | size_t channels, |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 695 | uint32_t format, |
| 696 | size_t lengthInBytes) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 697 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 698 | // Frame size in bytes for 10 ms of audio. |
| 699 | // TODO (hellner): 44.1 kHz has 440 samples frame size. Doesn't seem to |
| 700 | // be taken into consideration here! |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 701 | const size_t frameSize = (freq / 100) * channels; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 702 | |
| 703 | // Calculate the number of full frames that the wave file contain. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 704 | const size_t dataLengthInBytes = frameSize * (lengthInBytes / frameSize); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 705 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 706 | uint8_t buf[kWavHeaderSize]; |
| 707 | webrtc::WriteWavHeader(buf, channels, freq, static_cast<WavFormat>(format), |
| 708 | bytesPerSample, dataLengthInBytes / bytesPerSample); |
| 709 | wav.Write(buf, kWavHeaderSize); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 710 | return 0; |
| 711 | } |
| 712 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 713 | int32_t ModuleFileUtility::UpdateWavHeader(OutStream& wav) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 714 | { |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 715 | int32_t res = -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 716 | if(wav.Rewind() == -1) |
| 717 | { |
| 718 | return -1; |
| 719 | } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 720 | size_t channels = (codec_info_.channels == 0) ? 1 : codec_info_.channels; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 721 | |
| 722 | if(STR_CASE_CMP(codec_info_.plname, "L16") == 0) |
| 723 | { |
| 724 | res = WriteWavHeader(wav, codec_info_.plfreq, 2, channels, |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 725 | kWavFormatPcm, _bytesWritten); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 726 | } else if(STR_CASE_CMP(codec_info_.plname, "PCMU") == 0) { |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 727 | res = WriteWavHeader(wav, 8000, 1, channels, kWavFormatMuLaw, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 728 | _bytesWritten); |
henrike@webrtc.org | 4df8c9a | 2011-10-19 18:30:25 +0000 | [diff] [blame] | 729 | } else if(STR_CASE_CMP(codec_info_.plname, "PCMA") == 0) { |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 730 | res = WriteWavHeader(wav, 8000, 1, channels, kWavFormatALaw, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 731 | _bytesWritten); |
| 732 | } else { |
| 733 | // Allow calling this API even if not writing to a WAVE file. |
| 734 | // TODO (hellner): why?! |
| 735 | return 0; |
| 736 | } |
| 737 | return res; |
| 738 | } |
| 739 | |
| 740 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 741 | int32_t ModuleFileUtility::InitPreEncodedReading(InStream& in, |
| 742 | const CodecInst& cinst) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 743 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 744 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 745 | uint8_t preEncodedID; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 746 | in.Read(&preEncodedID, 1); |
| 747 | |
| 748 | MediaFileUtility_CodecType codecType = |
| 749 | (MediaFileUtility_CodecType)preEncodedID; |
| 750 | |
| 751 | if(set_codec_info(cinst) != 0) |
| 752 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 753 | LOG(LS_ERROR) << "Pre-encoded file send codec mismatch!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 754 | return -1; |
| 755 | } |
| 756 | if(codecType != _codecId) |
| 757 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 758 | LOG(LS_ERROR) << "Pre-encoded file format codec mismatch!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 759 | return -1; |
| 760 | } |
| 761 | memcpy(&codec_info_,&cinst,sizeof(CodecInst)); |
| 762 | _reading = true; |
| 763 | return 0; |
| 764 | } |
| 765 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 766 | int32_t ModuleFileUtility::ReadPreEncodedData( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 767 | InStream& in, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 768 | int8_t* outData, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 769 | const size_t bufferSize) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 770 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 771 | LOG(LS_VERBOSE) << "ModuleFileUtility::ReadPreEncodedData(in= " << &in |
| 772 | << ", outData= " << static_cast<void*>(outData) |
| 773 | << ", bufferSize= " << bufferSize << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 774 | |
| 775 | if(outData == NULL) |
| 776 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 777 | LOG(LS_ERROR) << "output buffer NULL"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 778 | } |
| 779 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 780 | size_t frameLen; |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 781 | uint8_t buf[64]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 782 | // Each frame has a two byte header containing the frame length. |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 783 | int32_t res = in.Read(buf, 2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 784 | if(res != 2) |
| 785 | { |
| 786 | if(!in.Rewind()) |
| 787 | { |
| 788 | // The first byte is the codec identifier. |
| 789 | in.Read(buf, 1); |
| 790 | res = in.Read(buf, 2); |
| 791 | } |
| 792 | else |
| 793 | { |
| 794 | return -1; |
| 795 | } |
| 796 | } |
| 797 | frameLen = buf[0] + buf[1] * 256; |
| 798 | if(bufferSize < frameLen) |
| 799 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 800 | LOG(LS_ERROR) << "buffer not large enough to read " << frameLen |
| 801 | << " bytes of pre-encoded data!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 802 | return -1; |
| 803 | } |
| 804 | return in.Read(outData, frameLen); |
| 805 | } |
| 806 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 807 | int32_t ModuleFileUtility::InitPreEncodedWriting( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 808 | OutStream& out, |
| 809 | const CodecInst& codecInst) |
| 810 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 811 | |
| 812 | if(set_codec_info(codecInst) != 0) |
| 813 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 814 | LOG(LS_ERROR) << "CodecInst not recognized!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 815 | return -1; |
| 816 | } |
| 817 | _writing = true; |
| 818 | _bytesWritten = 1; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 819 | out.Write(&_codecId, 1); |
| 820 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 821 | } |
| 822 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 823 | int32_t ModuleFileUtility::WritePreEncodedData( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 824 | OutStream& out, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 825 | const int8_t* buffer, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 826 | const size_t dataLength) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 827 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 828 | LOG(LS_VERBOSE) << "ModuleFileUtility::WritePreEncodedData(out= " << &out |
| 829 | << " , inData= " << static_cast<const void*>(buffer) |
| 830 | << ", dataLen= " << dataLength << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 831 | |
| 832 | if(buffer == NULL) |
| 833 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 834 | LOG(LS_ERROR) << "buffer NULL"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 835 | } |
| 836 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 837 | size_t bytesWritten = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 838 | // The first two bytes is the size of the frame. |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 839 | int16_t lengthBuf; |
| 840 | lengthBuf = (int16_t)dataLength; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 841 | if(dataLength > static_cast<size_t>(std::numeric_limits<int16_t>::max()) || |
| 842 | !out.Write(&lengthBuf, 2)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 843 | { |
| 844 | return -1; |
| 845 | } |
| 846 | bytesWritten = 2; |
| 847 | |
| 848 | if(!out.Write(buffer, dataLength)) |
| 849 | { |
| 850 | return -1; |
| 851 | } |
| 852 | bytesWritten += dataLength; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 853 | return static_cast<int32_t>(bytesWritten); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 854 | } |
| 855 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 856 | int32_t ModuleFileUtility::InitCompressedReading( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 857 | InStream& in, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 858 | const uint32_t start, |
| 859 | const uint32_t stop) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 860 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 861 | LOG(LS_VERBOSE) << "ModuleFileUtility::InitCompressedReading(in= " << &in |
| 862 | << ", start= " << start << ", stop= " << stop << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 863 | |
henrik.lundin@webrtc.org | fa58745 | 2015-02-23 09:25:40 +0000 | [diff] [blame] | 864 | #if defined(WEBRTC_CODEC_ILBC) |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 865 | int16_t read_len = 0; |
henrikg@webrtc.org | af225d6 | 2011-12-09 09:58:39 +0000 | [diff] [blame] | 866 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 867 | _codecId = kCodecNoCodec; |
| 868 | _playoutPositionMs = 0; |
| 869 | _reading = false; |
| 870 | |
| 871 | _startPointInMs = start; |
| 872 | _stopPointInMs = stop; |
| 873 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 874 | // Read the codec name |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 875 | int32_t cnt = 0; |
leozwang@webrtc.org | 09e7719 | 2012-03-01 18:35:54 +0000 | [diff] [blame] | 876 | char buf[64]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 877 | do |
| 878 | { |
| 879 | in.Read(&buf[cnt++], 1); |
| 880 | } while ((buf[cnt-1] != '\n') && (64 > cnt)); |
| 881 | |
| 882 | if(cnt==64) |
| 883 | { |
| 884 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 885 | } |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 886 | buf[cnt]=0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 887 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 888 | #ifdef WEBRTC_CODEC_ILBC |
| 889 | if(!strcmp("#!iLBC20\n", buf)) |
| 890 | { |
| 891 | codec_info_.pltype = 102; |
| 892 | strcpy(codec_info_.plname, "ilbc"); |
| 893 | codec_info_.plfreq = 8000; |
| 894 | codec_info_.pacsize = 160; |
| 895 | codec_info_.channels = 1; |
| 896 | codec_info_.rate = 13300; |
| 897 | _codecId = kCodecIlbc20Ms; |
| 898 | |
| 899 | if(_startPointInMs > 0) |
| 900 | { |
| 901 | while (_playoutPositionMs <= _startPointInMs) |
| 902 | { |
| 903 | read_len = in.Read(buf, 38); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 904 | if(read_len != 38) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 905 | { |
| 906 | return -1; |
| 907 | } |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 908 | _playoutPositionMs += 20; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | if(!strcmp("#!iLBC30\n", buf)) |
| 914 | { |
| 915 | codec_info_.pltype = 102; |
| 916 | strcpy(codec_info_.plname, "ilbc"); |
| 917 | codec_info_.plfreq = 8000; |
| 918 | codec_info_.pacsize = 240; |
| 919 | codec_info_.channels = 1; |
| 920 | codec_info_.rate = 13300; |
| 921 | _codecId = kCodecIlbc30Ms; |
| 922 | |
| 923 | if(_startPointInMs > 0) |
| 924 | { |
| 925 | while (_playoutPositionMs <= _startPointInMs) |
| 926 | { |
| 927 | read_len = in.Read(buf, 50); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 928 | if(read_len != 50) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 929 | { |
| 930 | return -1; |
| 931 | } |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 932 | _playoutPositionMs += 20; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | } |
| 936 | #endif |
| 937 | if(_codecId == kCodecNoCodec) |
| 938 | { |
| 939 | return -1; |
| 940 | } |
| 941 | _reading = true; |
| 942 | return 0; |
| 943 | } |
| 944 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 945 | int32_t ModuleFileUtility::ReadCompressedData(InStream& in, |
| 946 | int8_t* outData, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 947 | size_t bufferSize) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 948 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 949 | LOG(LS_VERBOSE) << "ModuleFileUtility::ReadCompressedData(in=" << &in |
| 950 | << ", outData=" << static_cast<void*>(outData) << ", bytes=" |
| 951 | << bufferSize << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 952 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 953 | int bytesRead = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 954 | |
| 955 | if(! _reading) |
| 956 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 957 | LOG(LS_ERROR) << "not currently reading!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 958 | return -1; |
| 959 | } |
| 960 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 961 | #ifdef WEBRTC_CODEC_ILBC |
| 962 | if((_codecId == kCodecIlbc20Ms) || |
| 963 | (_codecId == kCodecIlbc30Ms)) |
| 964 | { |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 965 | size_t byteSize = 0; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 966 | if(_codecId == kCodecIlbc30Ms) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 967 | { |
| 968 | byteSize = 50; |
| 969 | } |
| 970 | if(_codecId == kCodecIlbc20Ms) |
| 971 | { |
| 972 | byteSize = 38; |
| 973 | } |
| 974 | if(bufferSize < byteSize) |
| 975 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 976 | LOG(LS_ERROR) |
| 977 | << "output buffer is too short to read ILBC compressed data."; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 978 | assert(false); |
| 979 | return -1; |
| 980 | } |
| 981 | |
| 982 | bytesRead = in.Read(outData, byteSize); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 983 | if(bytesRead != static_cast<int>(byteSize)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 984 | { |
| 985 | if(!in.Rewind()) |
| 986 | { |
| 987 | InitCompressedReading(in, _startPointInMs, _stopPointInMs); |
| 988 | bytesRead = in.Read(outData, byteSize); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 989 | if(bytesRead != static_cast<int>(byteSize)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 990 | { |
| 991 | _reading = false; |
| 992 | return -1; |
| 993 | } |
| 994 | } |
| 995 | else |
| 996 | { |
| 997 | _reading = false; |
| 998 | return -1; |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | #endif |
| 1003 | if(bytesRead == 0) |
| 1004 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1005 | LOG(LS_ERROR) |
| 1006 | << "ReadCompressedData() no bytes read, codec not supported"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1007 | return -1; |
| 1008 | } |
| 1009 | |
| 1010 | _playoutPositionMs += 20; |
| 1011 | if((_stopPointInMs > 0) && |
| 1012 | (_playoutPositionMs >= _stopPointInMs)) |
| 1013 | { |
| 1014 | if(!in.Rewind()) |
| 1015 | { |
| 1016 | InitCompressedReading(in, _startPointInMs, _stopPointInMs); |
| 1017 | } |
| 1018 | else |
| 1019 | { |
| 1020 | _reading = false; |
| 1021 | } |
| 1022 | } |
| 1023 | return bytesRead; |
| 1024 | } |
| 1025 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1026 | int32_t ModuleFileUtility::InitCompressedWriting( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1027 | OutStream& out, |
| 1028 | const CodecInst& codecInst) |
| 1029 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1030 | LOG(LS_VERBOSE) << "ModuleFileUtility::InitCompressedWriting(out= " << &out |
| 1031 | << ", codecName= " << codecInst.plname << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1032 | |
| 1033 | _writing = false; |
| 1034 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1035 | #ifdef WEBRTC_CODEC_ILBC |
| 1036 | if(STR_CASE_CMP(codecInst.plname, "ilbc") == 0) |
| 1037 | { |
| 1038 | if(codecInst.pacsize == 160) |
| 1039 | { |
| 1040 | _codecId = kCodecIlbc20Ms; |
| 1041 | out.Write("#!iLBC20\n",9); |
| 1042 | } |
| 1043 | else if(codecInst.pacsize == 240) |
| 1044 | { |
| 1045 | _codecId = kCodecIlbc30Ms; |
| 1046 | out.Write("#!iLBC30\n",9); |
| 1047 | } |
| 1048 | else |
| 1049 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1050 | LOG(LS_ERROR) << "codecInst defines unsupported compression codec!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1051 | return -1; |
| 1052 | } |
| 1053 | memcpy(&codec_info_,&codecInst,sizeof(CodecInst)); |
| 1054 | _writing = true; |
| 1055 | return 0; |
| 1056 | } |
| 1057 | #endif |
| 1058 | |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1059 | LOG(LS_ERROR) << "codecInst defines unsupported compression codec!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1060 | return -1; |
| 1061 | } |
| 1062 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1063 | int32_t ModuleFileUtility::WriteCompressedData( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1064 | OutStream& out, |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1065 | const int8_t* buffer, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1066 | const size_t dataLength) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1067 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1068 | LOG(LS_VERBOSE) << "ModuleFileUtility::WriteCompressedData(out= " << &out |
| 1069 | << ", buf= " << static_cast<const void*>(buffer) |
| 1070 | << ", dataLen= " << dataLength << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1071 | |
| 1072 | if(buffer == NULL) |
| 1073 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1074 | LOG(LS_ERROR) << "buffer NULL"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | if(!out.Write(buffer, dataLength)) |
| 1078 | { |
| 1079 | return -1; |
| 1080 | } |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1081 | return static_cast<int32_t>(dataLength); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1084 | int32_t ModuleFileUtility::InitPCMReading(InStream& pcm, |
| 1085 | const uint32_t start, |
| 1086 | const uint32_t stop, |
| 1087 | uint32_t freq) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1088 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1089 | LOG(LS_VERBOSE) << "ModuleFileUtility::InitPCMReading(pcm= " << &pcm |
| 1090 | << ", start=" << start << ", stop=" << stop << ", freq=" |
| 1091 | << freq << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1092 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1093 | int8_t dummy[320]; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 1094 | int read_len; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1095 | |
| 1096 | _playoutPositionMs = 0; |
| 1097 | _startPointInMs = start; |
| 1098 | _stopPointInMs = stop; |
| 1099 | _reading = false; |
| 1100 | |
| 1101 | if(freq == 8000) |
| 1102 | { |
| 1103 | strcpy(codec_info_.plname, "L16"); |
| 1104 | codec_info_.pltype = -1; |
| 1105 | codec_info_.plfreq = 8000; |
| 1106 | codec_info_.pacsize = 160; |
| 1107 | codec_info_.channels = 1; |
| 1108 | codec_info_.rate = 128000; |
| 1109 | _codecId = kCodecL16_8Khz; |
| 1110 | } |
| 1111 | else if(freq == 16000) |
| 1112 | { |
| 1113 | strcpy(codec_info_.plname, "L16"); |
| 1114 | codec_info_.pltype = -1; |
| 1115 | codec_info_.plfreq = 16000; |
| 1116 | codec_info_.pacsize = 320; |
| 1117 | codec_info_.channels = 1; |
| 1118 | codec_info_.rate = 256000; |
| 1119 | _codecId = kCodecL16_16kHz; |
| 1120 | } |
| 1121 | else if(freq == 32000) |
| 1122 | { |
| 1123 | strcpy(codec_info_.plname, "L16"); |
| 1124 | codec_info_.pltype = -1; |
| 1125 | codec_info_.plfreq = 32000; |
| 1126 | codec_info_.pacsize = 320; |
| 1127 | codec_info_.channels = 1; |
| 1128 | codec_info_.rate = 512000; |
| 1129 | _codecId = kCodecL16_32Khz; |
| 1130 | } |
| 1131 | |
| 1132 | // Readsize for 10ms of audio data (2 bytes per sample). |
| 1133 | _readSizeBytes = 2 * codec_info_. plfreq / 100; |
| 1134 | if(_startPointInMs > 0) |
| 1135 | { |
| 1136 | while (_playoutPositionMs < _startPointInMs) |
| 1137 | { |
| 1138 | read_len = pcm.Read(dummy, _readSizeBytes); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 1139 | if(read_len != static_cast<int>(_readSizeBytes)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1140 | { |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 1141 | return -1; // Must have reached EOF before start position! |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1142 | } |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 1143 | _playoutPositionMs += 10; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | _reading = true; |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1150 | int32_t ModuleFileUtility::ReadPCMData(InStream& pcm, |
| 1151 | int8_t* outData, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1152 | size_t bufferSize) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1153 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1154 | LOG(LS_VERBOSE) << "ModuleFileUtility::ReadPCMData(pcm= " << &pcm |
| 1155 | << ", outData= " << static_cast<void*>(outData) |
| 1156 | << ", bufSize= " << bufferSize << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1157 | |
| 1158 | if(outData == NULL) |
| 1159 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1160 | LOG(LS_ERROR) << "buffer NULL"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | // Readsize for 10ms of audio data (2 bytes per sample). |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 1164 | size_t bytesRequested = static_cast<size_t>(2 * codec_info_.plfreq / 100); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1165 | if(bufferSize < bytesRequested) |
| 1166 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1167 | LOG(LS_ERROR) |
| 1168 | << "ReadPCMData: buffer not long enough for a 10ms frame."; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1169 | assert(false); |
| 1170 | return -1; |
| 1171 | } |
| 1172 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 1173 | int bytesRead = pcm.Read(outData, bytesRequested); |
| 1174 | if(bytesRead < static_cast<int>(bytesRequested)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1175 | { |
| 1176 | if(pcm.Rewind() == -1) |
| 1177 | { |
| 1178 | _reading = false; |
| 1179 | } |
| 1180 | else |
| 1181 | { |
| 1182 | if(InitPCMReading(pcm, _startPointInMs, _stopPointInMs, |
| 1183 | codec_info_.plfreq) == -1) |
| 1184 | { |
| 1185 | _reading = false; |
| 1186 | } |
| 1187 | else |
| 1188 | { |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 1189 | size_t rest = bytesRequested - bytesRead; |
| 1190 | int len = pcm.Read(&(outData[bytesRead]), rest); |
| 1191 | if(len == static_cast<int>(rest)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1192 | { |
| 1193 | bytesRead += len; |
| 1194 | } |
| 1195 | else |
| 1196 | { |
| 1197 | _reading = false; |
| 1198 | } |
| 1199 | } |
| 1200 | if(bytesRead <= 0) |
| 1201 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1202 | LOG(LS_ERROR) << "ReadPCMData: Failed to rewind audio file."; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1203 | return -1; |
| 1204 | } |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | if(bytesRead <= 0) |
| 1209 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1210 | LOG(LS_VERBOSE) << "ReadPCMData: end of file"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1211 | return -1; |
| 1212 | } |
| 1213 | _playoutPositionMs += 10; |
| 1214 | if(_stopPointInMs && _playoutPositionMs >= _stopPointInMs) |
| 1215 | { |
| 1216 | if(!pcm.Rewind()) |
| 1217 | { |
| 1218 | if(InitPCMReading(pcm, _startPointInMs, _stopPointInMs, |
| 1219 | codec_info_.plfreq) == -1) |
| 1220 | { |
| 1221 | _reading = false; |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | return bytesRead; |
| 1226 | } |
| 1227 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1228 | int32_t ModuleFileUtility::InitPCMWriting(OutStream& out, uint32_t freq) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1229 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1230 | |
| 1231 | if(freq == 8000) |
| 1232 | { |
| 1233 | strcpy(codec_info_.plname, "L16"); |
| 1234 | codec_info_.pltype = -1; |
| 1235 | codec_info_.plfreq = 8000; |
| 1236 | codec_info_.pacsize = 160; |
| 1237 | codec_info_.channels = 1; |
| 1238 | codec_info_.rate = 128000; |
| 1239 | |
| 1240 | _codecId = kCodecL16_8Khz; |
| 1241 | } |
| 1242 | else if(freq == 16000) |
| 1243 | { |
| 1244 | strcpy(codec_info_.plname, "L16"); |
| 1245 | codec_info_.pltype = -1; |
| 1246 | codec_info_.plfreq = 16000; |
| 1247 | codec_info_.pacsize = 320; |
| 1248 | codec_info_.channels = 1; |
| 1249 | codec_info_.rate = 256000; |
| 1250 | |
| 1251 | _codecId = kCodecL16_16kHz; |
| 1252 | } |
| 1253 | else if(freq == 32000) |
| 1254 | { |
| 1255 | strcpy(codec_info_.plname, "L16"); |
| 1256 | codec_info_.pltype = -1; |
| 1257 | codec_info_.plfreq = 32000; |
| 1258 | codec_info_.pacsize = 320; |
| 1259 | codec_info_.channels = 1; |
| 1260 | codec_info_.rate = 512000; |
| 1261 | |
| 1262 | _codecId = kCodecL16_32Khz; |
| 1263 | } |
| 1264 | if((_codecId != kCodecL16_8Khz) && |
| 1265 | (_codecId != kCodecL16_16kHz) && |
lliuu | bc436ed | 2017-03-31 16:32:28 -0700 | [diff] [blame] | 1266 | (_codecId != kCodecL16_32Khz)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1267 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1268 | LOG(LS_ERROR) << "CodecInst is not 8KHz PCM or 16KHz PCM!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1269 | return -1; |
| 1270 | } |
| 1271 | _writing = true; |
| 1272 | _bytesWritten = 0; |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1276 | int32_t ModuleFileUtility::WritePCMData(OutStream& out, |
| 1277 | const int8_t* buffer, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1278 | const size_t dataLength) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1279 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1280 | LOG(LS_VERBOSE) << "ModuleFileUtility::WritePCMData(out= " << &out |
| 1281 | << ", buf= " << static_cast<const void*>(buffer) |
| 1282 | << ", dataLen= " << dataLength << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1283 | |
| 1284 | if(buffer == NULL) |
| 1285 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1286 | LOG(LS_ERROR) << "buffer NULL"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | if(!out.Write(buffer, dataLength)) |
| 1290 | { |
| 1291 | return -1; |
| 1292 | } |
| 1293 | |
| 1294 | _bytesWritten += dataLength; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1295 | return static_cast<int32_t>(dataLength); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1298 | int32_t ModuleFileUtility::codec_info(CodecInst& codecInst) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1299 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1300 | LOG(LS_VERBOSE) << "ModuleFileUtility::codec_info(codecInst= " << &codecInst |
| 1301 | << ")"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1302 | |
| 1303 | if(!_reading && !_writing) |
| 1304 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1305 | LOG(LS_ERROR) << "CodecInst: not currently reading audio file!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1306 | return -1; |
| 1307 | } |
| 1308 | memcpy(&codecInst,&codec_info_,sizeof(CodecInst)); |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1312 | int32_t ModuleFileUtility::set_codec_info(const CodecInst& codecInst) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1313 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1314 | |
| 1315 | _codecId = kCodecNoCodec; |
| 1316 | if(STR_CASE_CMP(codecInst.plname, "PCMU") == 0) |
| 1317 | { |
| 1318 | _codecId = kCodecPcmu; |
| 1319 | } |
| 1320 | else if(STR_CASE_CMP(codecInst.plname, "PCMA") == 0) |
| 1321 | { |
| 1322 | _codecId = kCodecPcma; |
| 1323 | } |
| 1324 | else if(STR_CASE_CMP(codecInst.plname, "L16") == 0) |
| 1325 | { |
| 1326 | if(codecInst.plfreq == 8000) |
| 1327 | { |
| 1328 | _codecId = kCodecL16_8Khz; |
| 1329 | } |
| 1330 | else if(codecInst.plfreq == 16000) |
| 1331 | { |
| 1332 | _codecId = kCodecL16_16kHz; |
| 1333 | } |
| 1334 | else if(codecInst.plfreq == 32000) |
| 1335 | { |
| 1336 | _codecId = kCodecL16_32Khz; |
| 1337 | } |
| 1338 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1339 | #ifdef WEBRTC_CODEC_ILBC |
| 1340 | else if(STR_CASE_CMP(codecInst.plname, "ilbc") == 0) |
| 1341 | { |
| 1342 | if(codecInst.pacsize == 160) |
| 1343 | { |
| 1344 | _codecId = kCodecIlbc20Ms; |
| 1345 | } |
| 1346 | else if(codecInst.pacsize == 240) |
| 1347 | { |
| 1348 | _codecId = kCodecIlbc30Ms; |
| 1349 | } |
| 1350 | } |
| 1351 | #endif |
| 1352 | #if(defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) |
| 1353 | else if(STR_CASE_CMP(codecInst.plname, "isac") == 0) |
| 1354 | { |
| 1355 | if(codecInst.plfreq == 16000) |
| 1356 | { |
| 1357 | _codecId = kCodecIsac; |
| 1358 | } |
| 1359 | else if(codecInst.plfreq == 32000) |
| 1360 | { |
| 1361 | _codecId = kCodecIsacSwb; |
| 1362 | } |
| 1363 | } |
| 1364 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1365 | #ifdef WEBRTC_CODEC_G722 |
| 1366 | else if(STR_CASE_CMP(codecInst.plname, "G722") == 0) |
| 1367 | { |
| 1368 | _codecId = kCodecG722; |
| 1369 | } |
| 1370 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1371 | if(_codecId == kCodecNoCodec) |
| 1372 | { |
| 1373 | return -1; |
| 1374 | } |
| 1375 | memcpy(&codec_info_, &codecInst, sizeof(CodecInst)); |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1379 | int32_t ModuleFileUtility::FileDurationMs(const char* fileName, |
| 1380 | const FileFormats fileFormat, |
| 1381 | const uint32_t freqInHz) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1382 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1383 | |
| 1384 | if(fileName == NULL) |
| 1385 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1386 | LOG(LS_ERROR) << "filename NULL"; |
henrike@webrtc.org | 26085e1 | 2012-02-27 21:50:40 +0000 | [diff] [blame] | 1387 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1390 | int32_t time_in_ms = -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1391 | struct stat file_size; |
| 1392 | if(stat(fileName,&file_size) == -1) |
| 1393 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1394 | LOG(LS_ERROR) << "failed to retrieve file size with stat!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1395 | return -1; |
| 1396 | } |
| 1397 | FileWrapper* inStreamObj = FileWrapper::Create(); |
| 1398 | if(inStreamObj == NULL) |
| 1399 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1400 | LOG(LS_INFO) << "failed to create InStream object!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1401 | return -1; |
| 1402 | } |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 1403 | if (!inStreamObj->OpenFile(fileName, true)) { |
| 1404 | delete inStreamObj; |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1405 | LOG(LS_ERROR) << "failed to open file " << fileName << "!"; |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 1406 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | switch (fileFormat) |
| 1410 | { |
| 1411 | case kFileFormatWavFile: |
| 1412 | { |
| 1413 | if(ReadWavHeader(*inStreamObj) == -1) |
| 1414 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1415 | LOG(LS_ERROR) << "failed to read WAV file header!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1416 | return -1; |
| 1417 | } |
| 1418 | time_in_ms = ((file_size.st_size - 44) / |
| 1419 | (_wavFormatObj.nAvgBytesPerSec/1000)); |
| 1420 | break; |
| 1421 | } |
| 1422 | case kFileFormatPcm16kHzFile: |
| 1423 | { |
| 1424 | // 16 samples per ms. 2 bytes per sample. |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1425 | int32_t denominator = 16*2; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1426 | time_in_ms = (file_size.st_size)/denominator; |
| 1427 | break; |
| 1428 | } |
| 1429 | case kFileFormatPcm8kHzFile: |
| 1430 | { |
| 1431 | // 8 samples per ms. 2 bytes per sample. |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1432 | int32_t denominator = 8*2; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1433 | time_in_ms = (file_size.st_size)/denominator; |
| 1434 | break; |
| 1435 | } |
| 1436 | case kFileFormatCompressedFile: |
| 1437 | { |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1438 | int32_t cnt = 0; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 1439 | int read_len = 0; |
leozwang@webrtc.org | 09e7719 | 2012-03-01 18:35:54 +0000 | [diff] [blame] | 1440 | char buf[64]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1441 | do |
| 1442 | { |
| 1443 | read_len = inStreamObj->Read(&buf[cnt++], 1); |
| 1444 | if(read_len != 1) |
| 1445 | { |
| 1446 | return -1; |
| 1447 | } |
| 1448 | } while ((buf[cnt-1] != '\n') && (64 > cnt)); |
| 1449 | |
| 1450 | if(cnt == 64) |
| 1451 | { |
| 1452 | return -1; |
| 1453 | } |
| 1454 | else |
| 1455 | { |
| 1456 | buf[cnt] = 0; |
| 1457 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1458 | #ifdef WEBRTC_CODEC_ILBC |
| 1459 | if(!strcmp("#!iLBC20\n", buf)) |
| 1460 | { |
| 1461 | // 20 ms is 304 bits |
| 1462 | time_in_ms = ((file_size.st_size)*160)/304; |
| 1463 | break; |
| 1464 | } |
| 1465 | if(!strcmp("#!iLBC30\n", buf)) |
| 1466 | { |
| 1467 | // 30 ms takes 400 bits. |
| 1468 | // file size in bytes * 8 / 400 is the number of |
| 1469 | // 30 ms frames in the file -> |
| 1470 | // time_in_ms = file size * 8 / 400 * 30 |
| 1471 | time_in_ms = ((file_size.st_size)*240)/400; |
| 1472 | break; |
| 1473 | } |
| 1474 | #endif |
mflodman@webrtc.org | 19fc09e | 2014-06-04 05:21:56 +0000 | [diff] [blame] | 1475 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1476 | } |
| 1477 | case kFileFormatPreencodedFile: |
| 1478 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1479 | LOG(LS_ERROR) << "cannot determine duration of Pre-Encoded file!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1480 | break; |
| 1481 | } |
| 1482 | default: |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1483 | LOG(LS_ERROR) << "unsupported file format " << fileFormat << "!"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1484 | break; |
| 1485 | } |
| 1486 | inStreamObj->CloseFile(); |
| 1487 | delete inStreamObj; |
| 1488 | return time_in_ms; |
| 1489 | } |
| 1490 | |
pbos@webrtc.org | 0ea11c1 | 2013-04-09 13:31:37 +0000 | [diff] [blame] | 1491 | uint32_t ModuleFileUtility::PlayoutPositionMs() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1492 | { |
Sam Zackrisson | 45ca37c | 2017-08-22 13:44:09 +0200 | [diff] [blame^] | 1493 | LOG(LS_VERBOSE) << "ModuleFileUtility::PlayoutPosition()"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1494 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 1495 | return _reading ? _playoutPositionMs : 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1496 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 1497 | } // namespace webrtc |