blob: b581d5235b78dc892018cece8c127b0aaabb7a17 [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
kwiberg97744472017-01-10 01:12:51 -080011#include "webrtc/voice_engine/file_player.h"
kwiberg144dd272016-08-17 02:46:53 -070012
13#include "webrtc/common_audio/resampler/include/resampler.h"
14#include "webrtc/common_types.h"
kwiberg144dd272016-08-17 02:46:53 -070015#include "webrtc/modules/media_file/media_file.h"
16#include "webrtc/modules/media_file/media_file_defines.h"
kwiberg144dd272016-08-17 02:46:53 -070017#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010018#include "webrtc/system_wrappers/include/logging.h"
kwiberg144dd272016-08-17 02:46:53 -070019#include "webrtc/typedefs.h"
kwiberg97744472017-01-10 01:12:51 -080020#include "webrtc/voice_engine/coder.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
niklase@google.com470e71d2011-07-07 08:21:25 +000022namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
kwiberg144dd272016-08-17 02:46:53 -070024namespace {
25
26class FilePlayerImpl : public FilePlayer {
27 public:
28 FilePlayerImpl(uint32_t instanceID, FileFormats fileFormat);
kwiberg4ec01d92016-08-22 08:43:54 -070029 ~FilePlayerImpl() override;
kwiberg144dd272016-08-17 02:46:53 -070030
kwiberg4ec01d92016-08-22 08:43:54 -070031 int Get10msAudioFromFile(int16_t* outBuffer,
32 size_t* lengthInSamples,
33 int frequencyInHz) override;
34 int32_t RegisterModuleFileCallback(FileCallback* callback) override;
35 int32_t StartPlayingFile(const char* fileName,
36 bool loop,
37 uint32_t startPosition,
38 float volumeScaling,
39 uint32_t notification,
40 uint32_t stopPosition,
41 const CodecInst* codecInst) override;
42 int32_t StartPlayingFile(InStream* sourceStream,
43 uint32_t startPosition,
44 float volumeScaling,
45 uint32_t notification,
46 uint32_t stopPosition,
47 const CodecInst* codecInst) override;
48 int32_t StopPlayingFile() override;
49 bool IsPlayingFile() const override;
50 int32_t GetPlayoutPosition(uint32_t* durationMs) override;
51 int32_t AudioCodec(CodecInst* audioCodec) const override;
52 int32_t Frequency() const override;
53 int32_t SetAudioScaling(float scaleFactor) override;
kwiberg144dd272016-08-17 02:46:53 -070054
kwiberg5a25d952016-08-17 07:31:12 -070055 private:
kwiberg144dd272016-08-17 02:46:53 -070056 int32_t SetUpAudioDecoder();
57
kwiberg144dd272016-08-17 02:46:53 -070058 const FileFormats _fileFormat;
59 MediaFile& _fileModule;
60
61 uint32_t _decodedLengthInMS;
62
kwiberg144dd272016-08-17 02:46:53 -070063 AudioCoder _audioDecoder;
64
65 CodecInst _codec;
66 int32_t _numberOf10MsPerFrame;
67 int32_t _numberOf10MsInDecoder;
68
69 Resampler _resampler;
70 float _scaling;
71};
niklase@google.com470e71d2011-07-07 08:21:25 +000072
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000073FilePlayerImpl::FilePlayerImpl(const uint32_t instanceID,
niklase@google.com470e71d2011-07-07 08:21:25 +000074 const FileFormats fileFormat)
kwiberg5a25d952016-08-17 07:31:12 -070075 : _fileFormat(fileFormat),
niklase@google.com470e71d2011-07-07 08:21:25 +000076 _fileModule(*MediaFile::CreateMediaFile(instanceID)),
77 _decodedLengthInMS(0),
78 _audioDecoder(instanceID),
79 _codec(),
80 _numberOf10MsPerFrame(0),
81 _numberOf10MsInDecoder(0),
henrike@webrtc.org6b9253e2012-02-15 18:48:16 +000082 _resampler(),
kwiberga06ce492016-08-16 05:35:24 -070083 _scaling(1.0) {
84 _codec.plfreq = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000085}
86
kwiberga06ce492016-08-16 05:35:24 -070087FilePlayerImpl::~FilePlayerImpl() {
88 MediaFile::DestroyMediaFile(&_fileModule);
niklase@google.com470e71d2011-07-07 08:21:25 +000089}
90
kwiberga06ce492016-08-16 05:35:24 -070091int32_t FilePlayerImpl::Frequency() const {
92 if (_codec.plfreq == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000093 return -1;
kwiberga06ce492016-08-16 05:35:24 -070094 }
95 // Make sure that sample rate is 8,16 or 32 kHz. E.g. WAVE files may have
96 // other sampling rates.
97 if (_codec.plfreq == 11000) {
98 return 16000;
99 } else if (_codec.plfreq == 22000) {
100 return 32000;
101 } else if (_codec.plfreq == 44000) {
102 return 32000;
103 } else if (_codec.plfreq == 48000) {
104 return 32000;
105 } else {
106 return _codec.plfreq;
107 }
108}
109
kwiberg4ec01d92016-08-22 08:43:54 -0700110int32_t FilePlayerImpl::AudioCodec(CodecInst* audioCodec) const {
111 *audioCodec = _codec;
kwiberga06ce492016-08-16 05:35:24 -0700112 return 0;
113}
114
115int32_t FilePlayerImpl::Get10msAudioFromFile(int16_t* outBuffer,
kwiberg4ec01d92016-08-22 08:43:54 -0700116 size_t* lengthInSamples,
kwiberga06ce492016-08-16 05:35:24 -0700117 int frequencyInHz) {
118 if (_codec.plfreq == 0) {
119 LOG(LS_WARNING) << "Get10msAudioFromFile() playing not started!"
120 << " codec freq = " << _codec.plfreq
121 << ", wanted freq = " << frequencyInHz;
122 return -1;
123 }
124
125 AudioFrame unresampledAudioFrame;
126 if (STR_CASE_CMP(_codec.plname, "L16") == 0) {
127 unresampledAudioFrame.sample_rate_hz_ = _codec.plfreq;
128
129 // L16 is un-encoded data. Just pull 10 ms.
130 size_t lengthInBytes = sizeof(unresampledAudioFrame.data_);
kwiberg4ec01d92016-08-22 08:43:54 -0700131 if (_fileModule.PlayoutAudioData(
132 reinterpret_cast<int8_t*>(unresampledAudioFrame.data_),
133 lengthInBytes) == -1) {
kwiberga06ce492016-08-16 05:35:24 -0700134 // End of file reached.
135 return -1;
136 }
137 if (lengthInBytes == 0) {
kwiberg4ec01d92016-08-22 08:43:54 -0700138 *lengthInSamples = 0;
kwiberga06ce492016-08-16 05:35:24 -0700139 return 0;
140 }
141 // One sample is two bytes.
142 unresampledAudioFrame.samples_per_channel_ = lengthInBytes >> 1;
143
144 } else {
145 // Decode will generate 10 ms of audio data. PlayoutAudioData(..)
146 // expects a full frame. If the frame size is larger than 10 ms,
147 // PlayoutAudioData(..) data should be called proportionally less often.
148 int16_t encodedBuffer[MAX_AUDIO_BUFFER_IN_SAMPLES];
149 size_t encodedLengthInBytes = 0;
150 if (++_numberOf10MsInDecoder >= _numberOf10MsPerFrame) {
151 _numberOf10MsInDecoder = 0;
152 size_t bytesFromFile = sizeof(encodedBuffer);
kwiberg4ec01d92016-08-22 08:43:54 -0700153 if (_fileModule.PlayoutAudioData(reinterpret_cast<int8_t*>(encodedBuffer),
154 bytesFromFile) == -1) {
kwiberga06ce492016-08-16 05:35:24 -0700155 // End of file reached.
156 return -1;
157 }
158 encodedLengthInBytes = bytesFromFile;
159 }
kwiberg4ec01d92016-08-22 08:43:54 -0700160 if (_audioDecoder.Decode(&unresampledAudioFrame, frequencyInHz,
161 reinterpret_cast<int8_t*>(encodedBuffer),
kwiberga06ce492016-08-16 05:35:24 -0700162 encodedLengthInBytes) == -1) {
163 return -1;
164 }
165 }
166
167 size_t outLen = 0;
168 if (_resampler.ResetIfNeeded(unresampledAudioFrame.sample_rate_hz_,
169 frequencyInHz, 1)) {
170 LOG(LS_WARNING) << "Get10msAudioFromFile() unexpected codec.";
171
172 // New sampling frequency. Update state.
173 outLen = static_cast<size_t>(frequencyInHz / 100);
174 memset(outBuffer, 0, outLen * sizeof(int16_t));
175 return 0;
176 }
177 _resampler.Push(unresampledAudioFrame.data_,
178 unresampledAudioFrame.samples_per_channel_, outBuffer,
179 MAX_AUDIO_BUFFER_IN_SAMPLES, outLen);
180
kwiberg4ec01d92016-08-22 08:43:54 -0700181 *lengthInSamples = outLen;
kwiberga06ce492016-08-16 05:35:24 -0700182
183 if (_scaling != 1.0) {
184 for (size_t i = 0; i < outLen; i++) {
185 outBuffer[i] = (int16_t)(outBuffer[i] * _scaling);
186 }
187 }
188 _decodedLengthInMS += 10;
189 return 0;
190}
191
192int32_t FilePlayerImpl::RegisterModuleFileCallback(FileCallback* callback) {
193 return _fileModule.SetModuleFileCallback(callback);
194}
195
196int32_t FilePlayerImpl::SetAudioScaling(float scaleFactor) {
197 if ((scaleFactor >= 0) && (scaleFactor <= 2.0)) {
198 _scaling = scaleFactor;
199 return 0;
200 }
201 LOG(LS_WARNING) << "SetAudioScaling() non-allowed scale factor.";
202 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000203}
204
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000205int32_t FilePlayerImpl::StartPlayingFile(const char* fileName,
206 bool loop,
207 uint32_t startPosition,
208 float volumeScaling,
209 uint32_t notification,
210 uint32_t stopPosition,
kwiberga06ce492016-08-16 05:35:24 -0700211 const CodecInst* codecInst) {
212 if (_fileFormat == kFileFormatPcm16kHzFile ||
213 _fileFormat == kFileFormatPcm8kHzFile ||
214 _fileFormat == kFileFormatPcm32kHzFile) {
215 CodecInst codecInstL16;
216 strncpy(codecInstL16.plname, "L16", 32);
217 codecInstL16.pltype = 93;
218 codecInstL16.channels = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219
kwiberga06ce492016-08-16 05:35:24 -0700220 if (_fileFormat == kFileFormatPcm8kHzFile) {
221 codecInstL16.rate = 128000;
222 codecInstL16.plfreq = 8000;
223 codecInstL16.pacsize = 80;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224
kwiberga06ce492016-08-16 05:35:24 -0700225 } else if (_fileFormat == kFileFormatPcm16kHzFile) {
226 codecInstL16.rate = 256000;
227 codecInstL16.plfreq = 16000;
228 codecInstL16.pacsize = 160;
niklase@google.com470e71d2011-07-07 08:21:25 +0000229
kwiberga06ce492016-08-16 05:35:24 -0700230 } else if (_fileFormat == kFileFormatPcm32kHzFile) {
231 codecInstL16.rate = 512000;
232 codecInstL16.plfreq = 32000;
233 codecInstL16.pacsize = 160;
234 } else {
235 LOG(LS_ERROR) << "StartPlayingFile() sample frequency not "
236 << "supported for PCM format.";
237 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000238 }
kwiberga06ce492016-08-16 05:35:24 -0700239
240 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop,
241 _fileFormat, &codecInstL16,
242 startPosition, stopPosition) == -1) {
243 LOG(LS_WARNING) << "StartPlayingFile() failed to initialize "
244 << "pcm file " << fileName;
245 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000246 }
kwiberga06ce492016-08-16 05:35:24 -0700247 SetAudioScaling(volumeScaling);
248 } else if (_fileFormat == kFileFormatPreencodedFile) {
249 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop,
250 _fileFormat, codecInst) == -1) {
251 LOG(LS_WARNING) << "StartPlayingFile() failed to initialize "
252 << "pre-encoded file " << fileName;
253 return -1;
254 }
255 } else {
256 CodecInst* no_inst = NULL;
257 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop,
258 _fileFormat, no_inst, startPosition,
259 stopPosition) == -1) {
260 LOG(LS_WARNING) << "StartPlayingFile() failed to initialize file "
261 << fileName;
262 return -1;
263 }
264 SetAudioScaling(volumeScaling);
265 }
266 if (SetUpAudioDecoder() == -1) {
267 StopPlayingFile();
268 return -1;
269 }
270 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000271}
272
kwiberg4ec01d92016-08-22 08:43:54 -0700273int32_t FilePlayerImpl::StartPlayingFile(InStream* sourceStream,
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000274 uint32_t startPosition,
275 float volumeScaling,
276 uint32_t notification,
277 uint32_t stopPosition,
kwiberga06ce492016-08-16 05:35:24 -0700278 const CodecInst* codecInst) {
279 if (_fileFormat == kFileFormatPcm16kHzFile ||
280 _fileFormat == kFileFormatPcm32kHzFile ||
281 _fileFormat == kFileFormatPcm8kHzFile) {
282 CodecInst codecInstL16;
283 strncpy(codecInstL16.plname, "L16", 32);
284 codecInstL16.pltype = 93;
285 codecInstL16.channels = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000286
kwiberga06ce492016-08-16 05:35:24 -0700287 if (_fileFormat == kFileFormatPcm8kHzFile) {
288 codecInstL16.rate = 128000;
289 codecInstL16.plfreq = 8000;
290 codecInstL16.pacsize = 80;
niklase@google.com470e71d2011-07-07 08:21:25 +0000291
kwiberga06ce492016-08-16 05:35:24 -0700292 } else if (_fileFormat == kFileFormatPcm16kHzFile) {
293 codecInstL16.rate = 256000;
294 codecInstL16.plfreq = 16000;
295 codecInstL16.pacsize = 160;
niklase@google.com470e71d2011-07-07 08:21:25 +0000296
kwiberga06ce492016-08-16 05:35:24 -0700297 } else if (_fileFormat == kFileFormatPcm32kHzFile) {
298 codecInstL16.rate = 512000;
299 codecInstL16.plfreq = 32000;
300 codecInstL16.pacsize = 160;
niklase@google.com470e71d2011-07-07 08:21:25 +0000301 } else {
kwiberga06ce492016-08-16 05:35:24 -0700302 LOG(LS_ERROR) << "StartPlayingFile() sample frequency not "
303 << "supported for PCM format.";
304 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000305 }
kwiberga06ce492016-08-16 05:35:24 -0700306 if (_fileModule.StartPlayingAudioStream(
kwiberg4ec01d92016-08-22 08:43:54 -0700307 *sourceStream, notification, _fileFormat, &codecInstL16,
kwiberga06ce492016-08-16 05:35:24 -0700308 startPosition, stopPosition) == -1) {
309 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream "
310 << "playout.";
311 return -1;
312 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000313
kwiberga06ce492016-08-16 05:35:24 -0700314 } else if (_fileFormat == kFileFormatPreencodedFile) {
kwiberg4ec01d92016-08-22 08:43:54 -0700315 if (_fileModule.StartPlayingAudioStream(*sourceStream, notification,
kwiberga06ce492016-08-16 05:35:24 -0700316 _fileFormat, codecInst) == -1) {
317 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream "
318 << "playout.";
319 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000320 }
kwiberga06ce492016-08-16 05:35:24 -0700321 } else {
322 CodecInst* no_inst = NULL;
kwiberg4ec01d92016-08-22 08:43:54 -0700323 if (_fileModule.StartPlayingAudioStream(*sourceStream, notification,
kwiberga06ce492016-08-16 05:35:24 -0700324 _fileFormat, no_inst, startPosition,
325 stopPosition) == -1) {
326 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream "
327 << "playout.";
328 return -1;
329 }
330 }
331 SetAudioScaling(volumeScaling);
332
333 if (SetUpAudioDecoder() == -1) {
334 StopPlayingFile();
335 return -1;
336 }
337 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000338}
339
kwiberga06ce492016-08-16 05:35:24 -0700340int32_t FilePlayerImpl::StopPlayingFile() {
341 memset(&_codec, 0, sizeof(CodecInst));
342 _numberOf10MsPerFrame = 0;
343 _numberOf10MsInDecoder = 0;
344 return _fileModule.StopPlaying();
niklase@google.com470e71d2011-07-07 08:21:25 +0000345}
346
kwiberga06ce492016-08-16 05:35:24 -0700347bool FilePlayerImpl::IsPlayingFile() const {
348 return _fileModule.IsPlaying();
niklase@google.com470e71d2011-07-07 08:21:25 +0000349}
350
kwiberg4ec01d92016-08-22 08:43:54 -0700351int32_t FilePlayerImpl::GetPlayoutPosition(uint32_t* durationMs) {
352 return _fileModule.PlayoutPositionMs(*durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000353}
354
kwiberga06ce492016-08-16 05:35:24 -0700355int32_t FilePlayerImpl::SetUpAudioDecoder() {
356 if ((_fileModule.codec_info(_codec) == -1)) {
357 LOG(LS_WARNING) << "Failed to retrieve codec info of file data.";
358 return -1;
359 }
360 if (STR_CASE_CMP(_codec.plname, "L16") != 0 &&
361 _audioDecoder.SetDecodeCodec(_codec) == -1) {
362 LOG(LS_WARNING) << "SetUpAudioDecoder() codec " << _codec.plname
363 << " not supported.";
364 return -1;
365 }
366 _numberOf10MsPerFrame = _codec.pacsize / (_codec.plfreq / 100);
367 _numberOf10MsInDecoder = 0;
368 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000369}
kwiberg144dd272016-08-17 02:46:53 -0700370
371} // namespace
372
kwiberg5b356f42016-09-08 04:32:33 -0700373std::unique_ptr<FilePlayer> FilePlayer::CreateFilePlayer(
kwiberg5a25d952016-08-17 07:31:12 -0700374 uint32_t instanceID,
375 FileFormats fileFormat) {
kwiberg144dd272016-08-17 02:46:53 -0700376 switch (fileFormat) {
377 case kFileFormatWavFile:
378 case kFileFormatCompressedFile:
379 case kFileFormatPreencodedFile:
380 case kFileFormatPcm16kHzFile:
381 case kFileFormatPcm8kHzFile:
382 case kFileFormatPcm32kHzFile:
383 // audio formats
kwiberg5a25d952016-08-17 07:31:12 -0700384 return std::unique_ptr<FilePlayer>(
385 new FilePlayerImpl(instanceID, fileFormat));
kwiberg144dd272016-08-17 02:46:53 -0700386 default:
387 assert(false);
kwiberg5a25d952016-08-17 07:31:12 -0700388 return nullptr;
kwiberg144dd272016-08-17 02:46:53 -0700389 }
390}
391
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000392} // namespace webrtc