| 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/modules/utility/include/file_player.h" | 11 #include "webrtc/modules/utility/include/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/engine_configurations.h" | 15 #include "webrtc/engine_configurations.h" |
| 16 #include "webrtc/modules/media_file/media_file.h" | 16 #include "webrtc/modules/media_file/media_file.h" |
| 17 #include "webrtc/modules/media_file/media_file_defines.h" | 17 #include "webrtc/modules/media_file/media_file_defines.h" |
| 18 #include "webrtc/modules/utility/source/coder.h" | 18 #include "webrtc/modules/utility/source/coder.h" |
| 19 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 19 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
| 20 #include "webrtc/system_wrappers/include/logging.h" | 20 #include "webrtc/system_wrappers/include/logging.h" |
| 21 #include "webrtc/typedefs.h" | 21 #include "webrtc/typedefs.h" |
| 22 | 22 |
| 23 namespace webrtc { | 23 namespace webrtc { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 class FilePlayerImpl : public FilePlayer { | 27 class FilePlayerImpl : public FilePlayer { |
| 28 public: | 28 public: |
| 29 FilePlayerImpl(uint32_t instanceID, FileFormats fileFormat); | 29 FilePlayerImpl(uint32_t instanceID, FileFormats fileFormat); |
| 30 ~FilePlayerImpl(); | 30 ~FilePlayerImpl() override; |
| 31 | 31 |
| 32 virtual int Get10msAudioFromFile(int16_t* outBuffer, | 32 int Get10msAudioFromFile(int16_t* outBuffer, |
| 33 size_t& lengthInSamples, | 33 size_t* lengthInSamples, |
| 34 int frequencyInHz); | 34 int frequencyInHz) override; |
| 35 virtual int32_t RegisterModuleFileCallback(FileCallback* callback); | 35 int32_t RegisterModuleFileCallback(FileCallback* callback) override; |
| 36 virtual int32_t StartPlayingFile(const char* fileName, | 36 int32_t StartPlayingFile(const char* fileName, |
| 37 bool loop, | 37 bool loop, |
| 38 uint32_t startPosition, | 38 uint32_t startPosition, |
| 39 float volumeScaling, | 39 float volumeScaling, |
| 40 uint32_t notification, | 40 uint32_t notification, |
| 41 uint32_t stopPosition, | 41 uint32_t stopPosition, |
| 42 const CodecInst* codecInst); | 42 const CodecInst* codecInst) override; |
| 43 virtual int32_t StartPlayingFile(InStream& sourceStream, | 43 int32_t StartPlayingFile(InStream* sourceStream, |
| 44 uint32_t startPosition, | 44 uint32_t startPosition, |
| 45 float volumeScaling, | 45 float volumeScaling, |
| 46 uint32_t notification, | 46 uint32_t notification, |
| 47 uint32_t stopPosition, | 47 uint32_t stopPosition, |
| 48 const CodecInst* codecInst); | 48 const CodecInst* codecInst) override; |
| 49 virtual int32_t StopPlayingFile(); | 49 int32_t StopPlayingFile() override; |
| 50 virtual bool IsPlayingFile() const; | 50 bool IsPlayingFile() const override; |
| 51 virtual int32_t GetPlayoutPosition(uint32_t& durationMs); | 51 int32_t GetPlayoutPosition(uint32_t* durationMs) override; |
| 52 virtual int32_t AudioCodec(CodecInst& audioCodec) const; | 52 int32_t AudioCodec(CodecInst* audioCodec) const override; |
| 53 virtual int32_t Frequency() const; | 53 int32_t Frequency() const override; |
| 54 virtual int32_t SetAudioScaling(float scaleFactor); | 54 int32_t SetAudioScaling(float scaleFactor) override; |
| 55 | 55 |
| 56 private: | 56 private: |
| 57 int32_t SetUpAudioDecoder(); | 57 int32_t SetUpAudioDecoder(); |
| 58 | 58 |
| 59 const FileFormats _fileFormat; | 59 const FileFormats _fileFormat; |
| 60 MediaFile& _fileModule; | 60 MediaFile& _fileModule; |
| 61 | 61 |
| 62 uint32_t _decodedLengthInMS; | 62 uint32_t _decodedLengthInMS; |
| 63 | 63 |
| 64 AudioCoder _audioDecoder; | 64 AudioCoder _audioDecoder; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 return 32000; | 101 return 32000; |
| 102 } else if (_codec.plfreq == 44000) { | 102 } else if (_codec.plfreq == 44000) { |
| 103 return 32000; | 103 return 32000; |
| 104 } else if (_codec.plfreq == 48000) { | 104 } else if (_codec.plfreq == 48000) { |
| 105 return 32000; | 105 return 32000; |
| 106 } else { | 106 } else { |
| 107 return _codec.plfreq; | 107 return _codec.plfreq; |
| 108 } | 108 } |
| 109 } | 109 } |
| 110 | 110 |
| 111 int32_t FilePlayerImpl::AudioCodec(CodecInst& audioCodec) const { | 111 int32_t FilePlayerImpl::AudioCodec(CodecInst* audioCodec) const { |
| 112 audioCodec = _codec; | 112 *audioCodec = _codec; |
| 113 return 0; | 113 return 0; |
| 114 } | 114 } |
| 115 | 115 |
| 116 int32_t FilePlayerImpl::Get10msAudioFromFile(int16_t* outBuffer, | 116 int32_t FilePlayerImpl::Get10msAudioFromFile(int16_t* outBuffer, |
| 117 size_t& lengthInSamples, | 117 size_t* lengthInSamples, |
| 118 int frequencyInHz) { | 118 int frequencyInHz) { |
| 119 if (_codec.plfreq == 0) { | 119 if (_codec.plfreq == 0) { |
| 120 LOG(LS_WARNING) << "Get10msAudioFromFile() playing not started!" | 120 LOG(LS_WARNING) << "Get10msAudioFromFile() playing not started!" |
| 121 << " codec freq = " << _codec.plfreq | 121 << " codec freq = " << _codec.plfreq |
| 122 << ", wanted freq = " << frequencyInHz; | 122 << ", wanted freq = " << frequencyInHz; |
| 123 return -1; | 123 return -1; |
| 124 } | 124 } |
| 125 | 125 |
| 126 AudioFrame unresampledAudioFrame; | 126 AudioFrame unresampledAudioFrame; |
| 127 if (STR_CASE_CMP(_codec.plname, "L16") == 0) { | 127 if (STR_CASE_CMP(_codec.plname, "L16") == 0) { |
| 128 unresampledAudioFrame.sample_rate_hz_ = _codec.plfreq; | 128 unresampledAudioFrame.sample_rate_hz_ = _codec.plfreq; |
| 129 | 129 |
| 130 // L16 is un-encoded data. Just pull 10 ms. | 130 // L16 is un-encoded data. Just pull 10 ms. |
| 131 size_t lengthInBytes = sizeof(unresampledAudioFrame.data_); | 131 size_t lengthInBytes = sizeof(unresampledAudioFrame.data_); |
| 132 if (_fileModule.PlayoutAudioData((int8_t*)unresampledAudioFrame.data_, | 132 if (_fileModule.PlayoutAudioData( |
| 133 lengthInBytes) == -1) { | 133 reinterpret_cast<int8_t*>(unresampledAudioFrame.data_), |
| 134 lengthInBytes) == -1) { |
| 134 // End of file reached. | 135 // End of file reached. |
| 135 return -1; | 136 return -1; |
| 136 } | 137 } |
| 137 if (lengthInBytes == 0) { | 138 if (lengthInBytes == 0) { |
| 138 lengthInSamples = 0; | 139 *lengthInSamples = 0; |
| 139 return 0; | 140 return 0; |
| 140 } | 141 } |
| 141 // One sample is two bytes. | 142 // One sample is two bytes. |
| 142 unresampledAudioFrame.samples_per_channel_ = lengthInBytes >> 1; | 143 unresampledAudioFrame.samples_per_channel_ = lengthInBytes >> 1; |
| 143 | 144 |
| 144 } else { | 145 } else { |
| 145 // Decode will generate 10 ms of audio data. PlayoutAudioData(..) | 146 // Decode will generate 10 ms of audio data. PlayoutAudioData(..) |
| 146 // expects a full frame. If the frame size is larger than 10 ms, | 147 // expects a full frame. If the frame size is larger than 10 ms, |
| 147 // PlayoutAudioData(..) data should be called proportionally less often. | 148 // PlayoutAudioData(..) data should be called proportionally less often. |
| 148 int16_t encodedBuffer[MAX_AUDIO_BUFFER_IN_SAMPLES]; | 149 int16_t encodedBuffer[MAX_AUDIO_BUFFER_IN_SAMPLES]; |
| 149 size_t encodedLengthInBytes = 0; | 150 size_t encodedLengthInBytes = 0; |
| 150 if (++_numberOf10MsInDecoder >= _numberOf10MsPerFrame) { | 151 if (++_numberOf10MsInDecoder >= _numberOf10MsPerFrame) { |
| 151 _numberOf10MsInDecoder = 0; | 152 _numberOf10MsInDecoder = 0; |
| 152 size_t bytesFromFile = sizeof(encodedBuffer); | 153 size_t bytesFromFile = sizeof(encodedBuffer); |
| 153 if (_fileModule.PlayoutAudioData((int8_t*)encodedBuffer, bytesFromFile) == | 154 if (_fileModule.PlayoutAudioData(reinterpret_cast<int8_t*>(encodedBuffer), |
| 154 -1) { | 155 bytesFromFile) == -1) { |
| 155 // End of file reached. | 156 // End of file reached. |
| 156 return -1; | 157 return -1; |
| 157 } | 158 } |
| 158 encodedLengthInBytes = bytesFromFile; | 159 encodedLengthInBytes = bytesFromFile; |
| 159 } | 160 } |
| 160 if (_audioDecoder.Decode(unresampledAudioFrame, frequencyInHz, | 161 if (_audioDecoder.Decode(&unresampledAudioFrame, frequencyInHz, |
| 161 (int8_t*)encodedBuffer, | 162 reinterpret_cast<int8_t*>(encodedBuffer), |
| 162 encodedLengthInBytes) == -1) { | 163 encodedLengthInBytes) == -1) { |
| 163 return -1; | 164 return -1; |
| 164 } | 165 } |
| 165 } | 166 } |
| 166 | 167 |
| 167 size_t outLen = 0; | 168 size_t outLen = 0; |
| 168 if (_resampler.ResetIfNeeded(unresampledAudioFrame.sample_rate_hz_, | 169 if (_resampler.ResetIfNeeded(unresampledAudioFrame.sample_rate_hz_, |
| 169 frequencyInHz, 1)) { | 170 frequencyInHz, 1)) { |
| 170 LOG(LS_WARNING) << "Get10msAudioFromFile() unexpected codec."; | 171 LOG(LS_WARNING) << "Get10msAudioFromFile() unexpected codec."; |
| 171 | 172 |
| 172 // New sampling frequency. Update state. | 173 // New sampling frequency. Update state. |
| 173 outLen = static_cast<size_t>(frequencyInHz / 100); | 174 outLen = static_cast<size_t>(frequencyInHz / 100); |
| 174 memset(outBuffer, 0, outLen * sizeof(int16_t)); | 175 memset(outBuffer, 0, outLen * sizeof(int16_t)); |
| 175 return 0; | 176 return 0; |
| 176 } | 177 } |
| 177 _resampler.Push(unresampledAudioFrame.data_, | 178 _resampler.Push(unresampledAudioFrame.data_, |
| 178 unresampledAudioFrame.samples_per_channel_, outBuffer, | 179 unresampledAudioFrame.samples_per_channel_, outBuffer, |
| 179 MAX_AUDIO_BUFFER_IN_SAMPLES, outLen); | 180 MAX_AUDIO_BUFFER_IN_SAMPLES, outLen); |
| 180 | 181 |
| 181 lengthInSamples = outLen; | 182 *lengthInSamples = outLen; |
| 182 | 183 |
| 183 if (_scaling != 1.0) { | 184 if (_scaling != 1.0) { |
| 184 for (size_t i = 0; i < outLen; i++) { | 185 for (size_t i = 0; i < outLen; i++) { |
| 185 outBuffer[i] = (int16_t)(outBuffer[i] * _scaling); | 186 outBuffer[i] = (int16_t)(outBuffer[i] * _scaling); |
| 186 } | 187 } |
| 187 } | 188 } |
| 188 _decodedLengthInMS += 10; | 189 _decodedLengthInMS += 10; |
| 189 return 0; | 190 return 0; |
| 190 } | 191 } |
| 191 | 192 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 } | 264 } |
| 264 SetAudioScaling(volumeScaling); | 265 SetAudioScaling(volumeScaling); |
| 265 } | 266 } |
| 266 if (SetUpAudioDecoder() == -1) { | 267 if (SetUpAudioDecoder() == -1) { |
| 267 StopPlayingFile(); | 268 StopPlayingFile(); |
| 268 return -1; | 269 return -1; |
| 269 } | 270 } |
| 270 return 0; | 271 return 0; |
| 271 } | 272 } |
| 272 | 273 |
| 273 int32_t FilePlayerImpl::StartPlayingFile(InStream& sourceStream, | 274 int32_t FilePlayerImpl::StartPlayingFile(InStream* sourceStream, |
| 274 uint32_t startPosition, | 275 uint32_t startPosition, |
| 275 float volumeScaling, | 276 float volumeScaling, |
| 276 uint32_t notification, | 277 uint32_t notification, |
| 277 uint32_t stopPosition, | 278 uint32_t stopPosition, |
| 278 const CodecInst* codecInst) { | 279 const CodecInst* codecInst) { |
| 279 if (_fileFormat == kFileFormatPcm16kHzFile || | 280 if (_fileFormat == kFileFormatPcm16kHzFile || |
| 280 _fileFormat == kFileFormatPcm32kHzFile || | 281 _fileFormat == kFileFormatPcm32kHzFile || |
| 281 _fileFormat == kFileFormatPcm8kHzFile) { | 282 _fileFormat == kFileFormatPcm8kHzFile) { |
| 282 CodecInst codecInstL16; | 283 CodecInst codecInstL16; |
| 283 strncpy(codecInstL16.plname, "L16", 32); | 284 strncpy(codecInstL16.plname, "L16", 32); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 297 } else if (_fileFormat == kFileFormatPcm32kHzFile) { | 298 } else if (_fileFormat == kFileFormatPcm32kHzFile) { |
| 298 codecInstL16.rate = 512000; | 299 codecInstL16.rate = 512000; |
| 299 codecInstL16.plfreq = 32000; | 300 codecInstL16.plfreq = 32000; |
| 300 codecInstL16.pacsize = 160; | 301 codecInstL16.pacsize = 160; |
| 301 } else { | 302 } else { |
| 302 LOG(LS_ERROR) << "StartPlayingFile() sample frequency not " | 303 LOG(LS_ERROR) << "StartPlayingFile() sample frequency not " |
| 303 << "supported for PCM format."; | 304 << "supported for PCM format."; |
| 304 return -1; | 305 return -1; |
| 305 } | 306 } |
| 306 if (_fileModule.StartPlayingAudioStream( | 307 if (_fileModule.StartPlayingAudioStream( |
| 307 sourceStream, notification, _fileFormat, &codecInstL16, | 308 *sourceStream, notification, _fileFormat, &codecInstL16, |
| 308 startPosition, stopPosition) == -1) { | 309 startPosition, stopPosition) == -1) { |
| 309 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream " | 310 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream " |
| 310 << "playout."; | 311 << "playout."; |
| 311 return -1; | 312 return -1; |
| 312 } | 313 } |
| 313 | 314 |
| 314 } else if (_fileFormat == kFileFormatPreencodedFile) { | 315 } else if (_fileFormat == kFileFormatPreencodedFile) { |
| 315 if (_fileModule.StartPlayingAudioStream(sourceStream, notification, | 316 if (_fileModule.StartPlayingAudioStream(*sourceStream, notification, |
| 316 _fileFormat, codecInst) == -1) { | 317 _fileFormat, codecInst) == -1) { |
| 317 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream " | 318 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream " |
| 318 << "playout."; | 319 << "playout."; |
| 319 return -1; | 320 return -1; |
| 320 } | 321 } |
| 321 } else { | 322 } else { |
| 322 CodecInst* no_inst = NULL; | 323 CodecInst* no_inst = NULL; |
| 323 if (_fileModule.StartPlayingAudioStream(sourceStream, notification, | 324 if (_fileModule.StartPlayingAudioStream(*sourceStream, notification, |
| 324 _fileFormat, no_inst, startPosition, | 325 _fileFormat, no_inst, startPosition, |
| 325 stopPosition) == -1) { | 326 stopPosition) == -1) { |
| 326 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream " | 327 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream " |
| 327 << "playout."; | 328 << "playout."; |
| 328 return -1; | 329 return -1; |
| 329 } | 330 } |
| 330 } | 331 } |
| 331 SetAudioScaling(volumeScaling); | 332 SetAudioScaling(volumeScaling); |
| 332 | 333 |
| 333 if (SetUpAudioDecoder() == -1) { | 334 if (SetUpAudioDecoder() == -1) { |
| 334 StopPlayingFile(); | 335 StopPlayingFile(); |
| 335 return -1; | 336 return -1; |
| 336 } | 337 } |
| 337 return 0; | 338 return 0; |
| 338 } | 339 } |
| 339 | 340 |
| 340 int32_t FilePlayerImpl::StopPlayingFile() { | 341 int32_t FilePlayerImpl::StopPlayingFile() { |
| 341 memset(&_codec, 0, sizeof(CodecInst)); | 342 memset(&_codec, 0, sizeof(CodecInst)); |
| 342 _numberOf10MsPerFrame = 0; | 343 _numberOf10MsPerFrame = 0; |
| 343 _numberOf10MsInDecoder = 0; | 344 _numberOf10MsInDecoder = 0; |
| 344 return _fileModule.StopPlaying(); | 345 return _fileModule.StopPlaying(); |
| 345 } | 346 } |
| 346 | 347 |
| 347 bool FilePlayerImpl::IsPlayingFile() const { | 348 bool FilePlayerImpl::IsPlayingFile() const { |
| 348 return _fileModule.IsPlaying(); | 349 return _fileModule.IsPlaying(); |
| 349 } | 350 } |
| 350 | 351 |
| 351 int32_t FilePlayerImpl::GetPlayoutPosition(uint32_t& durationMs) { | 352 int32_t FilePlayerImpl::GetPlayoutPosition(uint32_t* durationMs) { |
| 352 return _fileModule.PlayoutPositionMs(durationMs); | 353 return _fileModule.PlayoutPositionMs(*durationMs); |
| 353 } | 354 } |
| 354 | 355 |
| 355 int32_t FilePlayerImpl::SetUpAudioDecoder() { | 356 int32_t FilePlayerImpl::SetUpAudioDecoder() { |
| 356 if ((_fileModule.codec_info(_codec) == -1)) { | 357 if ((_fileModule.codec_info(_codec) == -1)) { |
| 357 LOG(LS_WARNING) << "Failed to retrieve codec info of file data."; | 358 LOG(LS_WARNING) << "Failed to retrieve codec info of file data."; |
| 358 return -1; | 359 return -1; |
| 359 } | 360 } |
| 360 if (STR_CASE_CMP(_codec.plname, "L16") != 0 && | 361 if (STR_CASE_CMP(_codec.plname, "L16") != 0 && |
| 361 _audioDecoder.SetDecodeCodec(_codec) == -1) { | 362 _audioDecoder.SetDecodeCodec(_codec) == -1) { |
| 362 LOG(LS_WARNING) << "SetUpAudioDecoder() codec " << _codec.plname | 363 LOG(LS_WARNING) << "SetUpAudioDecoder() codec " << _codec.plname |
| (...skipping 29 matching lines...) Expand all Loading... |
| 392 FilePlayer* FilePlayer::CreateFilePlayer(uint32_t instanceID, | 393 FilePlayer* FilePlayer::CreateFilePlayer(uint32_t instanceID, |
| 393 FileFormats fileFormat) { | 394 FileFormats fileFormat) { |
| 394 return FilePlayer::NewFilePlayer(instanceID, fileFormat).release(); | 395 return FilePlayer::NewFilePlayer(instanceID, fileFormat).release(); |
| 395 } | 396 } |
| 396 | 397 |
| 397 void FilePlayer::DestroyFilePlayer(FilePlayer* player) { | 398 void FilePlayer::DestroyFilePlayer(FilePlayer* player) { |
| 398 delete player; | 399 delete player; |
| 399 } | 400 } |
| 400 | 401 |
| 401 } // namespace webrtc | 402 } // namespace webrtc |
| OLD | NEW |