niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
mflodman@webrtc.org | 1f99280 | 2012-01-27 13:42:53 +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 | |
kwiberg | 144dd27 | 2016-08-17 02:46:53 -0700 | [diff] [blame] | 11 | #include "webrtc/modules/utility/include/file_player.h" |
| 12 | |
| 13 | #include "webrtc/common_audio/resampler/include/resampler.h" |
| 14 | #include "webrtc/common_types.h" |
| 15 | #include "webrtc/engine_configurations.h" |
| 16 | #include "webrtc/modules/media_file/media_file.h" |
| 17 | #include "webrtc/modules/media_file/media_file_defines.h" |
| 18 | #include "webrtc/modules/utility/source/coder.h" |
| 19 | #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/include/logging.h" |
kwiberg | 144dd27 | 2016-08-17 02:46:53 -0700 | [diff] [blame] | 21 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
kwiberg | 144dd27 | 2016-08-17 02:46:53 -0700 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | class FilePlayerImpl : public FilePlayer { |
| 28 | public: |
| 29 | FilePlayerImpl(uint32_t instanceID, FileFormats fileFormat); |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 30 | ~FilePlayerImpl() override; |
kwiberg | 144dd27 | 2016-08-17 02:46:53 -0700 | [diff] [blame] | 31 | |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 32 | int Get10msAudioFromFile(int16_t* outBuffer, |
| 33 | size_t* lengthInSamples, |
| 34 | int frequencyInHz) override; |
| 35 | int32_t RegisterModuleFileCallback(FileCallback* callback) override; |
| 36 | int32_t StartPlayingFile(const char* fileName, |
| 37 | bool loop, |
| 38 | uint32_t startPosition, |
| 39 | float volumeScaling, |
| 40 | uint32_t notification, |
| 41 | uint32_t stopPosition, |
| 42 | const CodecInst* codecInst) override; |
| 43 | int32_t StartPlayingFile(InStream* sourceStream, |
| 44 | uint32_t startPosition, |
| 45 | float volumeScaling, |
| 46 | uint32_t notification, |
| 47 | uint32_t stopPosition, |
| 48 | const CodecInst* codecInst) override; |
| 49 | int32_t StopPlayingFile() override; |
| 50 | bool IsPlayingFile() const override; |
| 51 | int32_t GetPlayoutPosition(uint32_t* durationMs) override; |
| 52 | int32_t AudioCodec(CodecInst* audioCodec) const override; |
| 53 | int32_t Frequency() const override; |
| 54 | int32_t SetAudioScaling(float scaleFactor) override; |
kwiberg | 144dd27 | 2016-08-17 02:46:53 -0700 | [diff] [blame] | 55 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 56 | private: |
kwiberg | 144dd27 | 2016-08-17 02:46:53 -0700 | [diff] [blame] | 57 | int32_t SetUpAudioDecoder(); |
| 58 | |
kwiberg | 144dd27 | 2016-08-17 02:46:53 -0700 | [diff] [blame] | 59 | const FileFormats _fileFormat; |
| 60 | MediaFile& _fileModule; |
| 61 | |
| 62 | uint32_t _decodedLengthInMS; |
| 63 | |
kwiberg | 144dd27 | 2016-08-17 02:46:53 -0700 | [diff] [blame] | 64 | AudioCoder _audioDecoder; |
| 65 | |
| 66 | CodecInst _codec; |
| 67 | int32_t _numberOf10MsPerFrame; |
| 68 | int32_t _numberOf10MsInDecoder; |
| 69 | |
| 70 | Resampler _resampler; |
| 71 | float _scaling; |
| 72 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 74 | FilePlayerImpl::FilePlayerImpl(const uint32_t instanceID, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | const FileFormats fileFormat) |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 76 | : _fileFormat(fileFormat), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | _fileModule(*MediaFile::CreateMediaFile(instanceID)), |
| 78 | _decodedLengthInMS(0), |
| 79 | _audioDecoder(instanceID), |
| 80 | _codec(), |
| 81 | _numberOf10MsPerFrame(0), |
| 82 | _numberOf10MsInDecoder(0), |
henrike@webrtc.org | 6b9253e | 2012-02-15 18:48:16 +0000 | [diff] [blame] | 83 | _resampler(), |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 84 | _scaling(1.0) { |
| 85 | _codec.plfreq = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | } |
| 87 | |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 88 | FilePlayerImpl::~FilePlayerImpl() { |
| 89 | MediaFile::DestroyMediaFile(&_fileModule); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | } |
| 91 | |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 92 | int32_t FilePlayerImpl::Frequency() const { |
| 93 | if (_codec.plfreq == 0) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | return -1; |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 95 | } |
| 96 | // Make sure that sample rate is 8,16 or 32 kHz. E.g. WAVE files may have |
| 97 | // other sampling rates. |
| 98 | if (_codec.plfreq == 11000) { |
| 99 | return 16000; |
| 100 | } else if (_codec.plfreq == 22000) { |
| 101 | return 32000; |
| 102 | } else if (_codec.plfreq == 44000) { |
| 103 | return 32000; |
| 104 | } else if (_codec.plfreq == 48000) { |
| 105 | return 32000; |
| 106 | } else { |
| 107 | return _codec.plfreq; |
| 108 | } |
| 109 | } |
| 110 | |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 111 | int32_t FilePlayerImpl::AudioCodec(CodecInst* audioCodec) const { |
| 112 | *audioCodec = _codec; |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int32_t FilePlayerImpl::Get10msAudioFromFile(int16_t* outBuffer, |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 117 | size_t* lengthInSamples, |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 118 | int frequencyInHz) { |
| 119 | if (_codec.plfreq == 0) { |
| 120 | LOG(LS_WARNING) << "Get10msAudioFromFile() playing not started!" |
| 121 | << " codec freq = " << _codec.plfreq |
| 122 | << ", wanted freq = " << frequencyInHz; |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | AudioFrame unresampledAudioFrame; |
| 127 | if (STR_CASE_CMP(_codec.plname, "L16") == 0) { |
| 128 | unresampledAudioFrame.sample_rate_hz_ = _codec.plfreq; |
| 129 | |
| 130 | // L16 is un-encoded data. Just pull 10 ms. |
| 131 | size_t lengthInBytes = sizeof(unresampledAudioFrame.data_); |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 132 | if (_fileModule.PlayoutAudioData( |
| 133 | reinterpret_cast<int8_t*>(unresampledAudioFrame.data_), |
| 134 | lengthInBytes) == -1) { |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 135 | // End of file reached. |
| 136 | return -1; |
| 137 | } |
| 138 | if (lengthInBytes == 0) { |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 139 | *lengthInSamples = 0; |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | // One sample is two bytes. |
| 143 | unresampledAudioFrame.samples_per_channel_ = lengthInBytes >> 1; |
| 144 | |
| 145 | } else { |
| 146 | // Decode will generate 10 ms of audio data. PlayoutAudioData(..) |
| 147 | // expects a full frame. If the frame size is larger than 10 ms, |
| 148 | // PlayoutAudioData(..) data should be called proportionally less often. |
| 149 | int16_t encodedBuffer[MAX_AUDIO_BUFFER_IN_SAMPLES]; |
| 150 | size_t encodedLengthInBytes = 0; |
| 151 | if (++_numberOf10MsInDecoder >= _numberOf10MsPerFrame) { |
| 152 | _numberOf10MsInDecoder = 0; |
| 153 | size_t bytesFromFile = sizeof(encodedBuffer); |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 154 | if (_fileModule.PlayoutAudioData(reinterpret_cast<int8_t*>(encodedBuffer), |
| 155 | bytesFromFile) == -1) { |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 156 | // End of file reached. |
| 157 | return -1; |
| 158 | } |
| 159 | encodedLengthInBytes = bytesFromFile; |
| 160 | } |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 161 | if (_audioDecoder.Decode(&unresampledAudioFrame, frequencyInHz, |
| 162 | reinterpret_cast<int8_t*>(encodedBuffer), |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 163 | encodedLengthInBytes) == -1) { |
| 164 | return -1; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | size_t outLen = 0; |
| 169 | if (_resampler.ResetIfNeeded(unresampledAudioFrame.sample_rate_hz_, |
| 170 | frequencyInHz, 1)) { |
| 171 | LOG(LS_WARNING) << "Get10msAudioFromFile() unexpected codec."; |
| 172 | |
| 173 | // New sampling frequency. Update state. |
| 174 | outLen = static_cast<size_t>(frequencyInHz / 100); |
| 175 | memset(outBuffer, 0, outLen * sizeof(int16_t)); |
| 176 | return 0; |
| 177 | } |
| 178 | _resampler.Push(unresampledAudioFrame.data_, |
| 179 | unresampledAudioFrame.samples_per_channel_, outBuffer, |
| 180 | MAX_AUDIO_BUFFER_IN_SAMPLES, outLen); |
| 181 | |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 182 | *lengthInSamples = outLen; |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 183 | |
| 184 | if (_scaling != 1.0) { |
| 185 | for (size_t i = 0; i < outLen; i++) { |
| 186 | outBuffer[i] = (int16_t)(outBuffer[i] * _scaling); |
| 187 | } |
| 188 | } |
| 189 | _decodedLengthInMS += 10; |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | int32_t FilePlayerImpl::RegisterModuleFileCallback(FileCallback* callback) { |
| 194 | return _fileModule.SetModuleFileCallback(callback); |
| 195 | } |
| 196 | |
| 197 | int32_t FilePlayerImpl::SetAudioScaling(float scaleFactor) { |
| 198 | if ((scaleFactor >= 0) && (scaleFactor <= 2.0)) { |
| 199 | _scaling = scaleFactor; |
| 200 | return 0; |
| 201 | } |
| 202 | LOG(LS_WARNING) << "SetAudioScaling() non-allowed scale factor."; |
| 203 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | } |
| 205 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 206 | int32_t FilePlayerImpl::StartPlayingFile(const char* fileName, |
| 207 | bool loop, |
| 208 | uint32_t startPosition, |
| 209 | float volumeScaling, |
| 210 | uint32_t notification, |
| 211 | uint32_t stopPosition, |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 212 | const CodecInst* codecInst) { |
| 213 | if (_fileFormat == kFileFormatPcm16kHzFile || |
| 214 | _fileFormat == kFileFormatPcm8kHzFile || |
| 215 | _fileFormat == kFileFormatPcm32kHzFile) { |
| 216 | CodecInst codecInstL16; |
| 217 | strncpy(codecInstL16.plname, "L16", 32); |
| 218 | codecInstL16.pltype = 93; |
| 219 | codecInstL16.channels = 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 220 | |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 221 | if (_fileFormat == kFileFormatPcm8kHzFile) { |
| 222 | codecInstL16.rate = 128000; |
| 223 | codecInstL16.plfreq = 8000; |
| 224 | codecInstL16.pacsize = 80; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 225 | |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 226 | } else if (_fileFormat == kFileFormatPcm16kHzFile) { |
| 227 | codecInstL16.rate = 256000; |
| 228 | codecInstL16.plfreq = 16000; |
| 229 | codecInstL16.pacsize = 160; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 230 | |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 231 | } else if (_fileFormat == kFileFormatPcm32kHzFile) { |
| 232 | codecInstL16.rate = 512000; |
| 233 | codecInstL16.plfreq = 32000; |
| 234 | codecInstL16.pacsize = 160; |
| 235 | } else { |
| 236 | LOG(LS_ERROR) << "StartPlayingFile() sample frequency not " |
| 237 | << "supported for PCM format."; |
| 238 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 239 | } |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 240 | |
| 241 | if (_fileModule.StartPlayingAudioFile(fileName, notification, loop, |
| 242 | _fileFormat, &codecInstL16, |
| 243 | startPosition, stopPosition) == -1) { |
| 244 | LOG(LS_WARNING) << "StartPlayingFile() failed to initialize " |
| 245 | << "pcm file " << fileName; |
| 246 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 247 | } |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 248 | SetAudioScaling(volumeScaling); |
| 249 | } else if (_fileFormat == kFileFormatPreencodedFile) { |
| 250 | if (_fileModule.StartPlayingAudioFile(fileName, notification, loop, |
| 251 | _fileFormat, codecInst) == -1) { |
| 252 | LOG(LS_WARNING) << "StartPlayingFile() failed to initialize " |
| 253 | << "pre-encoded file " << fileName; |
| 254 | return -1; |
| 255 | } |
| 256 | } else { |
| 257 | CodecInst* no_inst = NULL; |
| 258 | if (_fileModule.StartPlayingAudioFile(fileName, notification, loop, |
| 259 | _fileFormat, no_inst, startPosition, |
| 260 | stopPosition) == -1) { |
| 261 | LOG(LS_WARNING) << "StartPlayingFile() failed to initialize file " |
| 262 | << fileName; |
| 263 | return -1; |
| 264 | } |
| 265 | SetAudioScaling(volumeScaling); |
| 266 | } |
| 267 | if (SetUpAudioDecoder() == -1) { |
| 268 | StopPlayingFile(); |
| 269 | return -1; |
| 270 | } |
| 271 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 272 | } |
| 273 | |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 274 | int32_t FilePlayerImpl::StartPlayingFile(InStream* sourceStream, |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 275 | uint32_t startPosition, |
| 276 | float volumeScaling, |
| 277 | uint32_t notification, |
| 278 | uint32_t stopPosition, |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 279 | const CodecInst* codecInst) { |
| 280 | if (_fileFormat == kFileFormatPcm16kHzFile || |
| 281 | _fileFormat == kFileFormatPcm32kHzFile || |
| 282 | _fileFormat == kFileFormatPcm8kHzFile) { |
| 283 | CodecInst codecInstL16; |
| 284 | strncpy(codecInstL16.plname, "L16", 32); |
| 285 | codecInstL16.pltype = 93; |
| 286 | codecInstL16.channels = 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 287 | |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 288 | if (_fileFormat == kFileFormatPcm8kHzFile) { |
| 289 | codecInstL16.rate = 128000; |
| 290 | codecInstL16.plfreq = 8000; |
| 291 | codecInstL16.pacsize = 80; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 292 | |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 293 | } else if (_fileFormat == kFileFormatPcm16kHzFile) { |
| 294 | codecInstL16.rate = 256000; |
| 295 | codecInstL16.plfreq = 16000; |
| 296 | codecInstL16.pacsize = 160; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 297 | |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 298 | } else if (_fileFormat == kFileFormatPcm32kHzFile) { |
| 299 | codecInstL16.rate = 512000; |
| 300 | codecInstL16.plfreq = 32000; |
| 301 | codecInstL16.pacsize = 160; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 302 | } else { |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 303 | LOG(LS_ERROR) << "StartPlayingFile() sample frequency not " |
| 304 | << "supported for PCM format."; |
| 305 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 306 | } |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 307 | if (_fileModule.StartPlayingAudioStream( |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 308 | *sourceStream, notification, _fileFormat, &codecInstL16, |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 309 | startPosition, stopPosition) == -1) { |
| 310 | LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream " |
| 311 | << "playout."; |
| 312 | return -1; |
| 313 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 314 | |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 315 | } else if (_fileFormat == kFileFormatPreencodedFile) { |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 316 | if (_fileModule.StartPlayingAudioStream(*sourceStream, notification, |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 317 | _fileFormat, codecInst) == -1) { |
| 318 | LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream " |
| 319 | << "playout."; |
| 320 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 321 | } |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 322 | } else { |
| 323 | CodecInst* no_inst = NULL; |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 324 | if (_fileModule.StartPlayingAudioStream(*sourceStream, notification, |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 325 | _fileFormat, no_inst, startPosition, |
| 326 | stopPosition) == -1) { |
| 327 | LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream " |
| 328 | << "playout."; |
| 329 | return -1; |
| 330 | } |
| 331 | } |
| 332 | SetAudioScaling(volumeScaling); |
| 333 | |
| 334 | if (SetUpAudioDecoder() == -1) { |
| 335 | StopPlayingFile(); |
| 336 | return -1; |
| 337 | } |
| 338 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 339 | } |
| 340 | |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 341 | int32_t FilePlayerImpl::StopPlayingFile() { |
| 342 | memset(&_codec, 0, sizeof(CodecInst)); |
| 343 | _numberOf10MsPerFrame = 0; |
| 344 | _numberOf10MsInDecoder = 0; |
| 345 | return _fileModule.StopPlaying(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 346 | } |
| 347 | |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 348 | bool FilePlayerImpl::IsPlayingFile() const { |
| 349 | return _fileModule.IsPlaying(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 350 | } |
| 351 | |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 352 | int32_t FilePlayerImpl::GetPlayoutPosition(uint32_t* durationMs) { |
| 353 | return _fileModule.PlayoutPositionMs(*durationMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 354 | } |
| 355 | |
kwiberg | a06ce49 | 2016-08-16 05:35:24 -0700 | [diff] [blame] | 356 | int32_t FilePlayerImpl::SetUpAudioDecoder() { |
| 357 | if ((_fileModule.codec_info(_codec) == -1)) { |
| 358 | LOG(LS_WARNING) << "Failed to retrieve codec info of file data."; |
| 359 | return -1; |
| 360 | } |
| 361 | if (STR_CASE_CMP(_codec.plname, "L16") != 0 && |
| 362 | _audioDecoder.SetDecodeCodec(_codec) == -1) { |
| 363 | LOG(LS_WARNING) << "SetUpAudioDecoder() codec " << _codec.plname |
| 364 | << " not supported."; |
| 365 | return -1; |
| 366 | } |
| 367 | _numberOf10MsPerFrame = _codec.pacsize / (_codec.plfreq / 100); |
| 368 | _numberOf10MsInDecoder = 0; |
| 369 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 370 | } |
kwiberg | 144dd27 | 2016-08-17 02:46:53 -0700 | [diff] [blame] | 371 | |
| 372 | } // namespace |
| 373 | |
kwiberg | 5b356f4 | 2016-09-08 04:32:33 -0700 | [diff] [blame^] | 374 | std::unique_ptr<FilePlayer> FilePlayer::CreateFilePlayer( |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 375 | uint32_t instanceID, |
| 376 | FileFormats fileFormat) { |
kwiberg | 144dd27 | 2016-08-17 02:46:53 -0700 | [diff] [blame] | 377 | switch (fileFormat) { |
| 378 | case kFileFormatWavFile: |
| 379 | case kFileFormatCompressedFile: |
| 380 | case kFileFormatPreencodedFile: |
| 381 | case kFileFormatPcm16kHzFile: |
| 382 | case kFileFormatPcm8kHzFile: |
| 383 | case kFileFormatPcm32kHzFile: |
| 384 | // audio formats |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 385 | return std::unique_ptr<FilePlayer>( |
| 386 | new FilePlayerImpl(instanceID, fileFormat)); |
kwiberg | 144dd27 | 2016-08-17 02:46:53 -0700 | [diff] [blame] | 387 | default: |
| 388 | assert(false); |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 389 | return nullptr; |
kwiberg | 144dd27 | 2016-08-17 02:46:53 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 393 | } // namespace webrtc |