blob: 8434b7211de9bf0c3c72870d2d7785b6a61e58a2 [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);
30 ~FilePlayerImpl();
31
32 virtual int Get10msAudioFromFile(int16_t* outBuffer,
33 size_t& lengthInSamples,
34 int frequencyInHz);
35 virtual int32_t RegisterModuleFileCallback(FileCallback* callback);
36 virtual int32_t StartPlayingFile(const char* fileName,
37 bool loop,
38 uint32_t startPosition,
39 float volumeScaling,
40 uint32_t notification,
41 uint32_t stopPosition = 0,
42 const CodecInst* codecInst = NULL);
43 virtual int32_t StartPlayingFile(InStream& sourceStream,
44 uint32_t startPosition,
45 float volumeScaling,
46 uint32_t notification,
47 uint32_t stopPosition = 0,
48 const CodecInst* codecInst = NULL);
49 virtual int32_t StopPlayingFile();
50 virtual bool IsPlayingFile() const;
51 virtual int32_t GetPlayoutPosition(uint32_t& durationMs);
52 virtual int32_t AudioCodec(CodecInst& audioCodec) const;
53 virtual int32_t Frequency() const;
54 virtual int32_t SetAudioScaling(float scaleFactor);
55
56 protected:
57 int32_t SetUpAudioDecoder();
58
59 uint32_t _instanceID;
60 const FileFormats _fileFormat;
61 MediaFile& _fileModule;
62
63 uint32_t _decodedLengthInMS;
64
65 private:
66 AudioCoder _audioDecoder;
67
68 CodecInst _codec;
69 int32_t _numberOf10MsPerFrame;
70 int32_t _numberOf10MsInDecoder;
71
72 Resampler _resampler;
73 float _scaling;
74};
niklase@google.com470e71d2011-07-07 08:21:25 +000075
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000076FilePlayerImpl::FilePlayerImpl(const uint32_t instanceID,
niklase@google.com470e71d2011-07-07 08:21:25 +000077 const FileFormats fileFormat)
78 : _instanceID(instanceID),
79 _fileFormat(fileFormat),
80 _fileModule(*MediaFile::CreateMediaFile(instanceID)),
81 _decodedLengthInMS(0),
82 _audioDecoder(instanceID),
83 _codec(),
84 _numberOf10MsPerFrame(0),
85 _numberOf10MsInDecoder(0),
henrike@webrtc.org6b9253e2012-02-15 18:48:16 +000086 _resampler(),
kwiberga06ce492016-08-16 05:35:24 -070087 _scaling(1.0) {
88 _codec.plfreq = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000089}
90
kwiberga06ce492016-08-16 05:35:24 -070091FilePlayerImpl::~FilePlayerImpl() {
92 MediaFile::DestroyMediaFile(&_fileModule);
niklase@google.com470e71d2011-07-07 08:21:25 +000093}
94
kwiberga06ce492016-08-16 05:35:24 -070095int32_t FilePlayerImpl::Frequency() const {
96 if (_codec.plfreq == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000097 return -1;
kwiberga06ce492016-08-16 05:35:24 -070098 }
99 // Make sure that sample rate is 8,16 or 32 kHz. E.g. WAVE files may have
100 // other sampling rates.
101 if (_codec.plfreq == 11000) {
102 return 16000;
103 } else if (_codec.plfreq == 22000) {
104 return 32000;
105 } else if (_codec.plfreq == 44000) {
106 return 32000;
107 } else if (_codec.plfreq == 48000) {
108 return 32000;
109 } else {
110 return _codec.plfreq;
111 }
112}
113
114int32_t FilePlayerImpl::AudioCodec(CodecInst& audioCodec) const {
115 audioCodec = _codec;
116 return 0;
117}
118
119int32_t FilePlayerImpl::Get10msAudioFromFile(int16_t* outBuffer,
120 size_t& lengthInSamples,
121 int frequencyInHz) {
122 if (_codec.plfreq == 0) {
123 LOG(LS_WARNING) << "Get10msAudioFromFile() playing not started!"
124 << " codec freq = " << _codec.plfreq
125 << ", wanted freq = " << frequencyInHz;
126 return -1;
127 }
128
129 AudioFrame unresampledAudioFrame;
130 if (STR_CASE_CMP(_codec.plname, "L16") == 0) {
131 unresampledAudioFrame.sample_rate_hz_ = _codec.plfreq;
132
133 // L16 is un-encoded data. Just pull 10 ms.
134 size_t lengthInBytes = sizeof(unresampledAudioFrame.data_);
135 if (_fileModule.PlayoutAudioData((int8_t*)unresampledAudioFrame.data_,
136 lengthInBytes) == -1) {
137 // End of file reached.
138 return -1;
139 }
140 if (lengthInBytes == 0) {
141 lengthInSamples = 0;
142 return 0;
143 }
144 // One sample is two bytes.
145 unresampledAudioFrame.samples_per_channel_ = lengthInBytes >> 1;
146
147 } else {
148 // Decode will generate 10 ms of audio data. PlayoutAudioData(..)
149 // expects a full frame. If the frame size is larger than 10 ms,
150 // PlayoutAudioData(..) data should be called proportionally less often.
151 int16_t encodedBuffer[MAX_AUDIO_BUFFER_IN_SAMPLES];
152 size_t encodedLengthInBytes = 0;
153 if (++_numberOf10MsInDecoder >= _numberOf10MsPerFrame) {
154 _numberOf10MsInDecoder = 0;
155 size_t bytesFromFile = sizeof(encodedBuffer);
156 if (_fileModule.PlayoutAudioData((int8_t*)encodedBuffer, bytesFromFile) ==
157 -1) {
158 // End of file reached.
159 return -1;
160 }
161 encodedLengthInBytes = bytesFromFile;
162 }
163 if (_audioDecoder.Decode(unresampledAudioFrame, frequencyInHz,
164 (int8_t*)encodedBuffer,
165 encodedLengthInBytes) == -1) {
166 return -1;
167 }
168 }
169
170 size_t outLen = 0;
171 if (_resampler.ResetIfNeeded(unresampledAudioFrame.sample_rate_hz_,
172 frequencyInHz, 1)) {
173 LOG(LS_WARNING) << "Get10msAudioFromFile() unexpected codec.";
174
175 // New sampling frequency. Update state.
176 outLen = static_cast<size_t>(frequencyInHz / 100);
177 memset(outBuffer, 0, outLen * sizeof(int16_t));
178 return 0;
179 }
180 _resampler.Push(unresampledAudioFrame.data_,
181 unresampledAudioFrame.samples_per_channel_, outBuffer,
182 MAX_AUDIO_BUFFER_IN_SAMPLES, outLen);
183
184 lengthInSamples = outLen;
185
186 if (_scaling != 1.0) {
187 for (size_t i = 0; i < outLen; i++) {
188 outBuffer[i] = (int16_t)(outBuffer[i] * _scaling);
189 }
190 }
191 _decodedLengthInMS += 10;
192 return 0;
193}
194
195int32_t FilePlayerImpl::RegisterModuleFileCallback(FileCallback* callback) {
196 return _fileModule.SetModuleFileCallback(callback);
197}
198
199int32_t FilePlayerImpl::SetAudioScaling(float scaleFactor) {
200 if ((scaleFactor >= 0) && (scaleFactor <= 2.0)) {
201 _scaling = scaleFactor;
202 return 0;
203 }
204 LOG(LS_WARNING) << "SetAudioScaling() non-allowed scale factor.";
205 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000206}
207
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000208int32_t FilePlayerImpl::StartPlayingFile(const char* fileName,
209 bool loop,
210 uint32_t startPosition,
211 float volumeScaling,
212 uint32_t notification,
213 uint32_t stopPosition,
kwiberga06ce492016-08-16 05:35:24 -0700214 const CodecInst* codecInst) {
215 if (_fileFormat == kFileFormatPcm16kHzFile ||
216 _fileFormat == kFileFormatPcm8kHzFile ||
217 _fileFormat == kFileFormatPcm32kHzFile) {
218 CodecInst codecInstL16;
219 strncpy(codecInstL16.plname, "L16", 32);
220 codecInstL16.pltype = 93;
221 codecInstL16.channels = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000222
kwiberga06ce492016-08-16 05:35:24 -0700223 if (_fileFormat == kFileFormatPcm8kHzFile) {
224 codecInstL16.rate = 128000;
225 codecInstL16.plfreq = 8000;
226 codecInstL16.pacsize = 80;
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
kwiberga06ce492016-08-16 05:35:24 -0700228 } else if (_fileFormat == kFileFormatPcm16kHzFile) {
229 codecInstL16.rate = 256000;
230 codecInstL16.plfreq = 16000;
231 codecInstL16.pacsize = 160;
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
kwiberga06ce492016-08-16 05:35:24 -0700233 } else if (_fileFormat == kFileFormatPcm32kHzFile) {
234 codecInstL16.rate = 512000;
235 codecInstL16.plfreq = 32000;
236 codecInstL16.pacsize = 160;
237 } else {
238 LOG(LS_ERROR) << "StartPlayingFile() sample frequency not "
239 << "supported for PCM format.";
240 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000241 }
kwiberga06ce492016-08-16 05:35:24 -0700242
243 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop,
244 _fileFormat, &codecInstL16,
245 startPosition, stopPosition) == -1) {
246 LOG(LS_WARNING) << "StartPlayingFile() failed to initialize "
247 << "pcm file " << fileName;
248 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000249 }
kwiberga06ce492016-08-16 05:35:24 -0700250 SetAudioScaling(volumeScaling);
251 } else if (_fileFormat == kFileFormatPreencodedFile) {
252 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop,
253 _fileFormat, codecInst) == -1) {
254 LOG(LS_WARNING) << "StartPlayingFile() failed to initialize "
255 << "pre-encoded file " << fileName;
256 return -1;
257 }
258 } else {
259 CodecInst* no_inst = NULL;
260 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop,
261 _fileFormat, no_inst, startPosition,
262 stopPosition) == -1) {
263 LOG(LS_WARNING) << "StartPlayingFile() failed to initialize file "
264 << fileName;
265 return -1;
266 }
267 SetAudioScaling(volumeScaling);
268 }
269 if (SetUpAudioDecoder() == -1) {
270 StopPlayingFile();
271 return -1;
272 }
273 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000274}
275
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000276int32_t FilePlayerImpl::StartPlayingFile(InStream& sourceStream,
277 uint32_t startPosition,
278 float volumeScaling,
279 uint32_t notification,
280 uint32_t stopPosition,
kwiberga06ce492016-08-16 05:35:24 -0700281 const CodecInst* codecInst) {
282 if (_fileFormat == kFileFormatPcm16kHzFile ||
283 _fileFormat == kFileFormatPcm32kHzFile ||
284 _fileFormat == kFileFormatPcm8kHzFile) {
285 CodecInst codecInstL16;
286 strncpy(codecInstL16.plname, "L16", 32);
287 codecInstL16.pltype = 93;
288 codecInstL16.channels = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000289
kwiberga06ce492016-08-16 05:35:24 -0700290 if (_fileFormat == kFileFormatPcm8kHzFile) {
291 codecInstL16.rate = 128000;
292 codecInstL16.plfreq = 8000;
293 codecInstL16.pacsize = 80;
niklase@google.com470e71d2011-07-07 08:21:25 +0000294
kwiberga06ce492016-08-16 05:35:24 -0700295 } else if (_fileFormat == kFileFormatPcm16kHzFile) {
296 codecInstL16.rate = 256000;
297 codecInstL16.plfreq = 16000;
298 codecInstL16.pacsize = 160;
niklase@google.com470e71d2011-07-07 08:21:25 +0000299
kwiberga06ce492016-08-16 05:35:24 -0700300 } else if (_fileFormat == kFileFormatPcm32kHzFile) {
301 codecInstL16.rate = 512000;
302 codecInstL16.plfreq = 32000;
303 codecInstL16.pacsize = 160;
niklase@google.com470e71d2011-07-07 08:21:25 +0000304 } else {
kwiberga06ce492016-08-16 05:35:24 -0700305 LOG(LS_ERROR) << "StartPlayingFile() sample frequency not "
306 << "supported for PCM format.";
307 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000308 }
kwiberga06ce492016-08-16 05:35:24 -0700309 if (_fileModule.StartPlayingAudioStream(
310 sourceStream, notification, _fileFormat, &codecInstL16,
311 startPosition, stopPosition) == -1) {
312 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream "
313 << "playout.";
314 return -1;
315 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000316
kwiberga06ce492016-08-16 05:35:24 -0700317 } else if (_fileFormat == kFileFormatPreencodedFile) {
318 if (_fileModule.StartPlayingAudioStream(sourceStream, notification,
319 _fileFormat, codecInst) == -1) {
320 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream "
321 << "playout.";
322 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000323 }
kwiberga06ce492016-08-16 05:35:24 -0700324 } else {
325 CodecInst* no_inst = NULL;
326 if (_fileModule.StartPlayingAudioStream(sourceStream, notification,
327 _fileFormat, no_inst, startPosition,
328 stopPosition) == -1) {
329 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream "
330 << "playout.";
331 return -1;
332 }
333 }
334 SetAudioScaling(volumeScaling);
335
336 if (SetUpAudioDecoder() == -1) {
337 StopPlayingFile();
338 return -1;
339 }
340 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000341}
342
kwiberga06ce492016-08-16 05:35:24 -0700343int32_t FilePlayerImpl::StopPlayingFile() {
344 memset(&_codec, 0, sizeof(CodecInst));
345 _numberOf10MsPerFrame = 0;
346 _numberOf10MsInDecoder = 0;
347 return _fileModule.StopPlaying();
niklase@google.com470e71d2011-07-07 08:21:25 +0000348}
349
kwiberga06ce492016-08-16 05:35:24 -0700350bool FilePlayerImpl::IsPlayingFile() const {
351 return _fileModule.IsPlaying();
niklase@google.com470e71d2011-07-07 08:21:25 +0000352}
353
kwiberga06ce492016-08-16 05:35:24 -0700354int32_t FilePlayerImpl::GetPlayoutPosition(uint32_t& durationMs) {
355 return _fileModule.PlayoutPositionMs(durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000356}
357
kwiberga06ce492016-08-16 05:35:24 -0700358int32_t FilePlayerImpl::SetUpAudioDecoder() {
359 if ((_fileModule.codec_info(_codec) == -1)) {
360 LOG(LS_WARNING) << "Failed to retrieve codec info of file data.";
361 return -1;
362 }
363 if (STR_CASE_CMP(_codec.plname, "L16") != 0 &&
364 _audioDecoder.SetDecodeCodec(_codec) == -1) {
365 LOG(LS_WARNING) << "SetUpAudioDecoder() codec " << _codec.plname
366 << " not supported.";
367 return -1;
368 }
369 _numberOf10MsPerFrame = _codec.pacsize / (_codec.plfreq / 100);
370 _numberOf10MsInDecoder = 0;
371 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000372}
kwiberg144dd272016-08-17 02:46:53 -0700373
374} // namespace
375
376FilePlayer* FilePlayer::CreateFilePlayer(uint32_t instanceID,
377 FileFormats fileFormat) {
378 switch (fileFormat) {
379 case kFileFormatWavFile:
380 case kFileFormatCompressedFile:
381 case kFileFormatPreencodedFile:
382 case kFileFormatPcm16kHzFile:
383 case kFileFormatPcm8kHzFile:
384 case kFileFormatPcm32kHzFile:
385 // audio formats
386 return new FilePlayerImpl(instanceID, fileFormat);
387 default:
388 assert(false);
389 return NULL;
390 }
391}
392
393void FilePlayer::DestroyFilePlayer(FilePlayer* player) {
394 delete player;
395}
396
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000397} // namespace webrtc