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 |
11 #include "webrtc/voice_engine/file_player.h" | 11 #include "webrtc/voice_engine/file_player.h" |
12 | 12 |
13 #include "webrtc/common_audio/resampler/include/resampler.h" | 13 #include "webrtc/common_audio/resampler/include/resampler.h" |
14 #include "webrtc/common_types.h" | 14 #include "webrtc/common_types.h" |
15 #include "webrtc/modules/media_file/media_file.h" | 15 #include "webrtc/modules/media_file/media_file.h" |
16 #include "webrtc/modules/media_file/media_file_defines.h" | 16 #include "webrtc/modules/media_file/media_file_defines.h" |
17 #include "webrtc/system_wrappers/include/logging.h" | |
18 #include "webrtc/typedefs.h" | 17 #include "webrtc/typedefs.h" |
19 #include "webrtc/voice_engine/coder.h" | 18 #include "webrtc/voice_engine/coder.h" |
20 | 19 |
21 namespace webrtc { | 20 namespace webrtc { |
22 | 21 |
23 namespace { | 22 namespace { |
24 | 23 |
25 class FilePlayerImpl : public FilePlayer { | 24 class FilePlayerImpl : public FilePlayer { |
26 public: | 25 public: |
27 FilePlayerImpl(uint32_t instanceID, FileFormats fileFormat); | 26 FilePlayerImpl(uint32_t instanceID, FileFormats fileFormat); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 107 |
109 int32_t FilePlayerImpl::AudioCodec(CodecInst* audioCodec) const { | 108 int32_t FilePlayerImpl::AudioCodec(CodecInst* audioCodec) const { |
110 *audioCodec = _codec; | 109 *audioCodec = _codec; |
111 return 0; | 110 return 0; |
112 } | 111 } |
113 | 112 |
114 int32_t FilePlayerImpl::Get10msAudioFromFile(int16_t* outBuffer, | 113 int32_t FilePlayerImpl::Get10msAudioFromFile(int16_t* outBuffer, |
115 size_t* lengthInSamples, | 114 size_t* lengthInSamples, |
116 int frequencyInHz) { | 115 int frequencyInHz) { |
117 if (_codec.plfreq == 0) { | 116 if (_codec.plfreq == 0) { |
118 LOG(LS_WARNING) << "Get10msAudioFromFile() playing not started!" | |
119 << " codec freq = " << _codec.plfreq | |
120 << ", wanted freq = " << frequencyInHz; | |
121 return -1; | 117 return -1; |
122 } | 118 } |
123 | 119 |
124 AudioFrame unresampledAudioFrame; | 120 AudioFrame unresampledAudioFrame; |
125 if (STR_CASE_CMP(_codec.plname, "L16") == 0) { | 121 if (STR_CASE_CMP(_codec.plname, "L16") == 0) { |
126 unresampledAudioFrame.sample_rate_hz_ = _codec.plfreq; | 122 unresampledAudioFrame.sample_rate_hz_ = _codec.plfreq; |
127 | 123 |
128 // L16 is un-encoded data. Just pull 10 ms. | 124 // L16 is un-encoded data. Just pull 10 ms. |
129 size_t lengthInBytes = sizeof(unresampledAudioFrame.data_); | 125 size_t lengthInBytes = sizeof(unresampledAudioFrame.data_); |
130 if (_fileModule.PlayoutAudioData( | 126 if (_fileModule.PlayoutAudioData( |
(...skipping 28 matching lines...) Expand all Loading... |
159 if (_audioDecoder.Decode(&unresampledAudioFrame, frequencyInHz, | 155 if (_audioDecoder.Decode(&unresampledAudioFrame, frequencyInHz, |
160 reinterpret_cast<int8_t*>(encodedBuffer), | 156 reinterpret_cast<int8_t*>(encodedBuffer), |
161 encodedLengthInBytes) == -1) { | 157 encodedLengthInBytes) == -1) { |
162 return -1; | 158 return -1; |
163 } | 159 } |
164 } | 160 } |
165 | 161 |
166 size_t outLen = 0; | 162 size_t outLen = 0; |
167 if (_resampler.ResetIfNeeded(unresampledAudioFrame.sample_rate_hz_, | 163 if (_resampler.ResetIfNeeded(unresampledAudioFrame.sample_rate_hz_, |
168 frequencyInHz, 1)) { | 164 frequencyInHz, 1)) { |
169 LOG(LS_WARNING) << "Get10msAudioFromFile() unexpected codec."; | |
170 | 165 |
171 // New sampling frequency. Update state. | 166 // New sampling frequency. Update state. |
172 outLen = static_cast<size_t>(frequencyInHz / 100); | 167 outLen = static_cast<size_t>(frequencyInHz / 100); |
173 memset(outBuffer, 0, outLen * sizeof(int16_t)); | 168 memset(outBuffer, 0, outLen * sizeof(int16_t)); |
174 return 0; | 169 return 0; |
175 } | 170 } |
176 _resampler.Push(unresampledAudioFrame.data_, | 171 _resampler.Push(unresampledAudioFrame.data_, |
177 unresampledAudioFrame.samples_per_channel_, outBuffer, | 172 unresampledAudioFrame.samples_per_channel_, outBuffer, |
178 MAX_AUDIO_BUFFER_IN_SAMPLES, outLen); | 173 MAX_AUDIO_BUFFER_IN_SAMPLES, outLen); |
179 | 174 |
(...skipping 10 matching lines...) Expand all Loading... |
190 | 185 |
191 int32_t FilePlayerImpl::RegisterModuleFileCallback(FileCallback* callback) { | 186 int32_t FilePlayerImpl::RegisterModuleFileCallback(FileCallback* callback) { |
192 return _fileModule.SetModuleFileCallback(callback); | 187 return _fileModule.SetModuleFileCallback(callback); |
193 } | 188 } |
194 | 189 |
195 int32_t FilePlayerImpl::SetAudioScaling(float scaleFactor) { | 190 int32_t FilePlayerImpl::SetAudioScaling(float scaleFactor) { |
196 if ((scaleFactor >= 0) && (scaleFactor <= 2.0)) { | 191 if ((scaleFactor >= 0) && (scaleFactor <= 2.0)) { |
197 _scaling = scaleFactor; | 192 _scaling = scaleFactor; |
198 return 0; | 193 return 0; |
199 } | 194 } |
200 LOG(LS_WARNING) << "SetAudioScaling() non-allowed scale factor."; | |
201 return -1; | 195 return -1; |
202 } | 196 } |
203 | 197 |
204 int32_t FilePlayerImpl::StartPlayingFile(const char* fileName, | 198 int32_t FilePlayerImpl::StartPlayingFile(const char* fileName, |
205 bool loop, | 199 bool loop, |
206 uint32_t startPosition, | 200 uint32_t startPosition, |
207 float volumeScaling, | 201 float volumeScaling, |
208 uint32_t notification, | 202 uint32_t notification, |
209 uint32_t stopPosition, | 203 uint32_t stopPosition, |
210 const CodecInst* codecInst) { | 204 const CodecInst* codecInst) { |
(...skipping 13 matching lines...) Expand all Loading... |
224 } else if (_fileFormat == kFileFormatPcm16kHzFile) { | 218 } else if (_fileFormat == kFileFormatPcm16kHzFile) { |
225 codecInstL16.rate = 256000; | 219 codecInstL16.rate = 256000; |
226 codecInstL16.plfreq = 16000; | 220 codecInstL16.plfreq = 16000; |
227 codecInstL16.pacsize = 160; | 221 codecInstL16.pacsize = 160; |
228 | 222 |
229 } else if (_fileFormat == kFileFormatPcm32kHzFile) { | 223 } else if (_fileFormat == kFileFormatPcm32kHzFile) { |
230 codecInstL16.rate = 512000; | 224 codecInstL16.rate = 512000; |
231 codecInstL16.plfreq = 32000; | 225 codecInstL16.plfreq = 32000; |
232 codecInstL16.pacsize = 160; | 226 codecInstL16.pacsize = 160; |
233 } else { | 227 } else { |
234 LOG(LS_ERROR) << "StartPlayingFile() sample frequency not " | |
235 << "supported for PCM format."; | |
236 return -1; | 228 return -1; |
237 } | 229 } |
238 | 230 |
239 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop, | 231 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop, |
240 _fileFormat, &codecInstL16, | 232 _fileFormat, &codecInstL16, |
241 startPosition, stopPosition) == -1) { | 233 startPosition, stopPosition) == -1) { |
242 LOG(LS_WARNING) << "StartPlayingFile() failed to initialize " | |
243 << "pcm file " << fileName; | |
244 return -1; | 234 return -1; |
245 } | 235 } |
246 SetAudioScaling(volumeScaling); | 236 SetAudioScaling(volumeScaling); |
247 } else if (_fileFormat == kFileFormatPreencodedFile) { | 237 } else if (_fileFormat == kFileFormatPreencodedFile) { |
248 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop, | 238 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop, |
249 _fileFormat, codecInst) == -1) { | 239 _fileFormat, codecInst) == -1) { |
250 LOG(LS_WARNING) << "StartPlayingFile() failed to initialize " | |
251 << "pre-encoded file " << fileName; | |
252 return -1; | 240 return -1; |
253 } | 241 } |
254 } else { | 242 } else { |
255 CodecInst* no_inst = NULL; | 243 CodecInst* no_inst = NULL; |
256 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop, | 244 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop, |
257 _fileFormat, no_inst, startPosition, | 245 _fileFormat, no_inst, startPosition, |
258 stopPosition) == -1) { | 246 stopPosition) == -1) { |
259 LOG(LS_WARNING) << "StartPlayingFile() failed to initialize file " | |
260 << fileName; | |
261 return -1; | 247 return -1; |
262 } | 248 } |
263 SetAudioScaling(volumeScaling); | 249 SetAudioScaling(volumeScaling); |
264 } | 250 } |
265 if (SetUpAudioDecoder() == -1) { | 251 if (SetUpAudioDecoder() == -1) { |
266 StopPlayingFile(); | 252 StopPlayingFile(); |
267 return -1; | 253 return -1; |
268 } | 254 } |
269 return 0; | 255 return 0; |
270 } | 256 } |
(...skipping 20 matching lines...) Expand all Loading... |
291 } else if (_fileFormat == kFileFormatPcm16kHzFile) { | 277 } else if (_fileFormat == kFileFormatPcm16kHzFile) { |
292 codecInstL16.rate = 256000; | 278 codecInstL16.rate = 256000; |
293 codecInstL16.plfreq = 16000; | 279 codecInstL16.plfreq = 16000; |
294 codecInstL16.pacsize = 160; | 280 codecInstL16.pacsize = 160; |
295 | 281 |
296 } else if (_fileFormat == kFileFormatPcm32kHzFile) { | 282 } else if (_fileFormat == kFileFormatPcm32kHzFile) { |
297 codecInstL16.rate = 512000; | 283 codecInstL16.rate = 512000; |
298 codecInstL16.plfreq = 32000; | 284 codecInstL16.plfreq = 32000; |
299 codecInstL16.pacsize = 160; | 285 codecInstL16.pacsize = 160; |
300 } else { | 286 } else { |
301 LOG(LS_ERROR) << "StartPlayingFile() sample frequency not " | |
302 << "supported for PCM format."; | |
303 return -1; | 287 return -1; |
304 } | 288 } |
305 if (_fileModule.StartPlayingAudioStream( | 289 if (_fileModule.StartPlayingAudioStream( |
306 *sourceStream, notification, _fileFormat, &codecInstL16, | 290 *sourceStream, notification, _fileFormat, &codecInstL16, |
307 startPosition, stopPosition) == -1) { | 291 startPosition, stopPosition) == -1) { |
308 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream " | |
309 << "playout."; | |
310 return -1; | 292 return -1; |
311 } | 293 } |
312 | 294 |
313 } else if (_fileFormat == kFileFormatPreencodedFile) { | 295 } else if (_fileFormat == kFileFormatPreencodedFile) { |
314 if (_fileModule.StartPlayingAudioStream(*sourceStream, notification, | 296 if (_fileModule.StartPlayingAudioStream(*sourceStream, notification, |
315 _fileFormat, codecInst) == -1) { | 297 _fileFormat, codecInst) == -1) { |
316 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream " | |
317 << "playout."; | |
318 return -1; | 298 return -1; |
319 } | 299 } |
320 } else { | 300 } else { |
321 CodecInst* no_inst = NULL; | 301 CodecInst* no_inst = NULL; |
322 if (_fileModule.StartPlayingAudioStream(*sourceStream, notification, | 302 if (_fileModule.StartPlayingAudioStream(*sourceStream, notification, |
323 _fileFormat, no_inst, startPosition, | 303 _fileFormat, no_inst, startPosition, |
324 stopPosition) == -1) { | 304 stopPosition) == -1) { |
325 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream " | |
326 << "playout."; | |
327 return -1; | 305 return -1; |
328 } | 306 } |
329 } | 307 } |
330 SetAudioScaling(volumeScaling); | 308 SetAudioScaling(volumeScaling); |
331 | 309 |
332 if (SetUpAudioDecoder() == -1) { | 310 if (SetUpAudioDecoder() == -1) { |
333 StopPlayingFile(); | 311 StopPlayingFile(); |
334 return -1; | 312 return -1; |
335 } | 313 } |
336 return 0; | 314 return 0; |
337 } | 315 } |
338 | 316 |
339 int32_t FilePlayerImpl::StopPlayingFile() { | 317 int32_t FilePlayerImpl::StopPlayingFile() { |
340 memset(&_codec, 0, sizeof(CodecInst)); | 318 memset(&_codec, 0, sizeof(CodecInst)); |
341 _numberOf10MsPerFrame = 0; | 319 _numberOf10MsPerFrame = 0; |
342 _numberOf10MsInDecoder = 0; | 320 _numberOf10MsInDecoder = 0; |
343 return _fileModule.StopPlaying(); | 321 return _fileModule.StopPlaying(); |
344 } | 322 } |
345 | 323 |
346 bool FilePlayerImpl::IsPlayingFile() const { | 324 bool FilePlayerImpl::IsPlayingFile() const { |
347 return _fileModule.IsPlaying(); | 325 return _fileModule.IsPlaying(); |
348 } | 326 } |
349 | 327 |
350 int32_t FilePlayerImpl::GetPlayoutPosition(uint32_t* durationMs) { | 328 int32_t FilePlayerImpl::GetPlayoutPosition(uint32_t* durationMs) { |
351 return _fileModule.PlayoutPositionMs(*durationMs); | 329 return _fileModule.PlayoutPositionMs(*durationMs); |
352 } | 330 } |
353 | 331 |
354 int32_t FilePlayerImpl::SetUpAudioDecoder() { | 332 int32_t FilePlayerImpl::SetUpAudioDecoder() { |
355 if ((_fileModule.codec_info(_codec) == -1)) { | 333 if ((_fileModule.codec_info(_codec) == -1)) { |
356 LOG(LS_WARNING) << "Failed to retrieve codec info of file data."; | |
357 return -1; | 334 return -1; |
358 } | 335 } |
359 if (STR_CASE_CMP(_codec.plname, "L16") != 0 && | 336 if (STR_CASE_CMP(_codec.plname, "L16") != 0 && |
360 _audioDecoder.SetDecodeCodec(_codec) == -1) { | 337 _audioDecoder.SetDecodeCodec(_codec) == -1) { |
361 LOG(LS_WARNING) << "SetUpAudioDecoder() codec " << _codec.plname | |
362 << " not supported."; | |
363 return -1; | 338 return -1; |
364 } | 339 } |
365 _numberOf10MsPerFrame = _codec.pacsize / (_codec.plfreq / 100); | 340 _numberOf10MsPerFrame = _codec.pacsize / (_codec.plfreq / 100); |
366 _numberOf10MsInDecoder = 0; | 341 _numberOf10MsInDecoder = 0; |
367 return 0; | 342 return 0; |
368 } | 343 } |
369 | 344 |
370 } // namespace | 345 } // namespace |
371 | 346 |
372 std::unique_ptr<FilePlayer> FilePlayer::CreateFilePlayer( | 347 std::unique_ptr<FilePlayer> FilePlayer::CreateFilePlayer( |
373 uint32_t instanceID, | 348 uint32_t instanceID, |
374 FileFormats fileFormat) { | 349 FileFormats fileFormat) { |
375 switch (fileFormat) { | 350 switch (fileFormat) { |
376 case kFileFormatWavFile: | 351 case kFileFormatWavFile: |
377 case kFileFormatCompressedFile: | 352 case kFileFormatCompressedFile: |
378 case kFileFormatPreencodedFile: | 353 case kFileFormatPreencodedFile: |
379 case kFileFormatPcm16kHzFile: | 354 case kFileFormatPcm16kHzFile: |
380 case kFileFormatPcm8kHzFile: | 355 case kFileFormatPcm8kHzFile: |
381 case kFileFormatPcm32kHzFile: | 356 case kFileFormatPcm32kHzFile: |
382 // audio formats | 357 // audio formats |
383 return std::unique_ptr<FilePlayer>( | 358 return std::unique_ptr<FilePlayer>( |
384 new FilePlayerImpl(instanceID, fileFormat)); | 359 new FilePlayerImpl(instanceID, fileFormat)); |
385 default: | 360 default: |
386 assert(false); | 361 assert(false); |
387 return nullptr; | 362 return nullptr; |
388 } | 363 } |
389 } | 364 } |
390 | 365 |
391 } // namespace webrtc | 366 } // namespace webrtc |
OLD | NEW |