blob: 75b7214f5c8e79055f17312afd9bd8edc04651f9 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mflodman@webrtc.org1f992802012-01-27 13:42:53 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
kwiberg144dd272016-08-17 02:46:53 -070011#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 Kjellander98f53512015-10-28 18:17:40 +010020#include "webrtc/system_wrappers/include/logging.h"
kwiberg144dd272016-08-17 02:46:53 -070021#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
niklase@google.com470e71d2011-07-07 08:21:25 +000023namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
kwiberg144dd272016-08-17 02:46:53 -070025namespace {
26
27class FilePlayerImpl : public FilePlayer {
28 public:
29 FilePlayerImpl(uint32_t instanceID, FileFormats fileFormat);
kwiberg4ec01d92016-08-22 08:43:54 -070030 ~FilePlayerImpl() override;
kwiberg144dd272016-08-17 02:46:53 -070031
kwiberg4ec01d92016-08-22 08:43:54 -070032 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;
kwiberg144dd272016-08-17 02:46:53 -070055
kwiberg5a25d952016-08-17 07:31:12 -070056 private:
kwiberg144dd272016-08-17 02:46:53 -070057 int32_t SetUpAudioDecoder();
58
kwiberg144dd272016-08-17 02:46:53 -070059 const FileFormats _fileFormat;
60 MediaFile& _fileModule;
61
62 uint32_t _decodedLengthInMS;
63
kwiberg144dd272016-08-17 02:46:53 -070064 AudioCoder _audioDecoder;
65
66 CodecInst _codec;
67 int32_t _numberOf10MsPerFrame;
68 int32_t _numberOf10MsInDecoder;
69
70 Resampler _resampler;
71 float _scaling;
72};
niklase@google.com470e71d2011-07-07 08:21:25 +000073
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000074FilePlayerImpl::FilePlayerImpl(const uint32_t instanceID,
niklase@google.com470e71d2011-07-07 08:21:25 +000075 const FileFormats fileFormat)
kwiberg5a25d952016-08-17 07:31:12 -070076 : _fileFormat(fileFormat),
niklase@google.com470e71d2011-07-07 08:21:25 +000077 _fileModule(*MediaFile::CreateMediaFile(instanceID)),
78 _decodedLengthInMS(0),
79 _audioDecoder(instanceID),
80 _codec(),
81 _numberOf10MsPerFrame(0),
82 _numberOf10MsInDecoder(0),
henrike@webrtc.org6b9253e2012-02-15 18:48:16 +000083 _resampler(),
kwiberga06ce492016-08-16 05:35:24 -070084 _scaling(1.0) {
85 _codec.plfreq = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000086}
87
kwiberga06ce492016-08-16 05:35:24 -070088FilePlayerImpl::~FilePlayerImpl() {
89 MediaFile::DestroyMediaFile(&_fileModule);
niklase@google.com470e71d2011-07-07 08:21:25 +000090}
91
kwiberga06ce492016-08-16 05:35:24 -070092int32_t FilePlayerImpl::Frequency() const {
93 if (_codec.plfreq == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000094 return -1;
kwiberga06ce492016-08-16 05:35:24 -070095 }
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
kwiberg4ec01d92016-08-22 08:43:54 -0700111int32_t FilePlayerImpl::AudioCodec(CodecInst* audioCodec) const {
112 *audioCodec = _codec;
kwiberga06ce492016-08-16 05:35:24 -0700113 return 0;
114}
115
116int32_t FilePlayerImpl::Get10msAudioFromFile(int16_t* outBuffer,
kwiberg4ec01d92016-08-22 08:43:54 -0700117 size_t* lengthInSamples,
kwiberga06ce492016-08-16 05:35:24 -0700118 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_);
kwiberg4ec01d92016-08-22 08:43:54 -0700132 if (_fileModule.PlayoutAudioData(
133 reinterpret_cast<int8_t*>(unresampledAudioFrame.data_),
134 lengthInBytes) == -1) {
kwiberga06ce492016-08-16 05:35:24 -0700135 // End of file reached.
136 return -1;
137 }
138 if (lengthInBytes == 0) {
kwiberg4ec01d92016-08-22 08:43:54 -0700139 *lengthInSamples = 0;
kwiberga06ce492016-08-16 05:35:24 -0700140 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);
kwiberg4ec01d92016-08-22 08:43:54 -0700154 if (_fileModule.PlayoutAudioData(reinterpret_cast<int8_t*>(encodedBuffer),
155 bytesFromFile) == -1) {
kwiberga06ce492016-08-16 05:35:24 -0700156 // End of file reached.
157 return -1;
158 }
159 encodedLengthInBytes = bytesFromFile;
160 }
kwiberg4ec01d92016-08-22 08:43:54 -0700161 if (_audioDecoder.Decode(&unresampledAudioFrame, frequencyInHz,
162 reinterpret_cast<int8_t*>(encodedBuffer),
kwiberga06ce492016-08-16 05:35:24 -0700163 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
kwiberg4ec01d92016-08-22 08:43:54 -0700182 *lengthInSamples = outLen;
kwiberga06ce492016-08-16 05:35:24 -0700183
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
193int32_t FilePlayerImpl::RegisterModuleFileCallback(FileCallback* callback) {
194 return _fileModule.SetModuleFileCallback(callback);
195}
196
197int32_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.com470e71d2011-07-07 08:21:25 +0000204}
205
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000206int32_t FilePlayerImpl::StartPlayingFile(const char* fileName,
207 bool loop,
208 uint32_t startPosition,
209 float volumeScaling,
210 uint32_t notification,
211 uint32_t stopPosition,
kwiberga06ce492016-08-16 05:35:24 -0700212 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.com470e71d2011-07-07 08:21:25 +0000220
kwiberga06ce492016-08-16 05:35:24 -0700221 if (_fileFormat == kFileFormatPcm8kHzFile) {
222 codecInstL16.rate = 128000;
223 codecInstL16.plfreq = 8000;
224 codecInstL16.pacsize = 80;
niklase@google.com470e71d2011-07-07 08:21:25 +0000225
kwiberga06ce492016-08-16 05:35:24 -0700226 } else if (_fileFormat == kFileFormatPcm16kHzFile) {
227 codecInstL16.rate = 256000;
228 codecInstL16.plfreq = 16000;
229 codecInstL16.pacsize = 160;
niklase@google.com470e71d2011-07-07 08:21:25 +0000230
kwiberga06ce492016-08-16 05:35:24 -0700231 } 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.com470e71d2011-07-07 08:21:25 +0000239 }
kwiberga06ce492016-08-16 05:35:24 -0700240
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.com470e71d2011-07-07 08:21:25 +0000247 }
kwiberga06ce492016-08-16 05:35:24 -0700248 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.com470e71d2011-07-07 08:21:25 +0000272}
273
kwiberg4ec01d92016-08-22 08:43:54 -0700274int32_t FilePlayerImpl::StartPlayingFile(InStream* sourceStream,
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000275 uint32_t startPosition,
276 float volumeScaling,
277 uint32_t notification,
278 uint32_t stopPosition,
kwiberga06ce492016-08-16 05:35:24 -0700279 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.com470e71d2011-07-07 08:21:25 +0000287
kwiberga06ce492016-08-16 05:35:24 -0700288 if (_fileFormat == kFileFormatPcm8kHzFile) {
289 codecInstL16.rate = 128000;
290 codecInstL16.plfreq = 8000;
291 codecInstL16.pacsize = 80;
niklase@google.com470e71d2011-07-07 08:21:25 +0000292
kwiberga06ce492016-08-16 05:35:24 -0700293 } else if (_fileFormat == kFileFormatPcm16kHzFile) {
294 codecInstL16.rate = 256000;
295 codecInstL16.plfreq = 16000;
296 codecInstL16.pacsize = 160;
niklase@google.com470e71d2011-07-07 08:21:25 +0000297
kwiberga06ce492016-08-16 05:35:24 -0700298 } else if (_fileFormat == kFileFormatPcm32kHzFile) {
299 codecInstL16.rate = 512000;
300 codecInstL16.plfreq = 32000;
301 codecInstL16.pacsize = 160;
niklase@google.com470e71d2011-07-07 08:21:25 +0000302 } else {
kwiberga06ce492016-08-16 05:35:24 -0700303 LOG(LS_ERROR) << "StartPlayingFile() sample frequency not "
304 << "supported for PCM format.";
305 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000306 }
kwiberga06ce492016-08-16 05:35:24 -0700307 if (_fileModule.StartPlayingAudioStream(
kwiberg4ec01d92016-08-22 08:43:54 -0700308 *sourceStream, notification, _fileFormat, &codecInstL16,
kwiberga06ce492016-08-16 05:35:24 -0700309 startPosition, stopPosition) == -1) {
310 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream "
311 << "playout.";
312 return -1;
313 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000314
kwiberga06ce492016-08-16 05:35:24 -0700315 } else if (_fileFormat == kFileFormatPreencodedFile) {
kwiberg4ec01d92016-08-22 08:43:54 -0700316 if (_fileModule.StartPlayingAudioStream(*sourceStream, notification,
kwiberga06ce492016-08-16 05:35:24 -0700317 _fileFormat, codecInst) == -1) {
318 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream "
319 << "playout.";
320 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000321 }
kwiberga06ce492016-08-16 05:35:24 -0700322 } else {
323 CodecInst* no_inst = NULL;
kwiberg4ec01d92016-08-22 08:43:54 -0700324 if (_fileModule.StartPlayingAudioStream(*sourceStream, notification,
kwiberga06ce492016-08-16 05:35:24 -0700325 _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.com470e71d2011-07-07 08:21:25 +0000339}
340
kwiberga06ce492016-08-16 05:35:24 -0700341int32_t FilePlayerImpl::StopPlayingFile() {
342 memset(&_codec, 0, sizeof(CodecInst));
343 _numberOf10MsPerFrame = 0;
344 _numberOf10MsInDecoder = 0;
345 return _fileModule.StopPlaying();
niklase@google.com470e71d2011-07-07 08:21:25 +0000346}
347
kwiberga06ce492016-08-16 05:35:24 -0700348bool FilePlayerImpl::IsPlayingFile() const {
349 return _fileModule.IsPlaying();
niklase@google.com470e71d2011-07-07 08:21:25 +0000350}
351
kwiberg4ec01d92016-08-22 08:43:54 -0700352int32_t FilePlayerImpl::GetPlayoutPosition(uint32_t* durationMs) {
353 return _fileModule.PlayoutPositionMs(*durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000354}
355
kwiberga06ce492016-08-16 05:35:24 -0700356int32_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.com470e71d2011-07-07 08:21:25 +0000370}
kwiberg144dd272016-08-17 02:46:53 -0700371
372} // namespace
373
kwiberg5a25d952016-08-17 07:31:12 -0700374std::unique_ptr<FilePlayer> FilePlayer::NewFilePlayer(
375 uint32_t instanceID,
376 FileFormats fileFormat) {
kwiberg144dd272016-08-17 02:46:53 -0700377 switch (fileFormat) {
378 case kFileFormatWavFile:
379 case kFileFormatCompressedFile:
380 case kFileFormatPreencodedFile:
381 case kFileFormatPcm16kHzFile:
382 case kFileFormatPcm8kHzFile:
383 case kFileFormatPcm32kHzFile:
384 // audio formats
kwiberg5a25d952016-08-17 07:31:12 -0700385 return std::unique_ptr<FilePlayer>(
386 new FilePlayerImpl(instanceID, fileFormat));
kwiberg144dd272016-08-17 02:46:53 -0700387 default:
388 assert(false);
kwiberg5a25d952016-08-17 07:31:12 -0700389 return nullptr;
kwiberg144dd272016-08-17 02:46:53 -0700390 }
391}
392
kwiberg5a25d952016-08-17 07:31:12 -0700393FilePlayer* FilePlayer::CreateFilePlayer(uint32_t instanceID,
394 FileFormats fileFormat) {
395 return FilePlayer::NewFilePlayer(instanceID, fileFormat).release();
396}
397
kwiberg144dd272016-08-17 02:46:53 -0700398void FilePlayer::DestroyFilePlayer(FilePlayer* player) {
399 delete player;
400}
401
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000402} // namespace webrtc