blob: e783a7eca888ced0efd74da6e091a6fd1e267f73 [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
kwiberg9d7eb132016-08-16 04:08:30 -070011#include "webrtc/modules/utility/source/file_player_impl.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010012#include "webrtc/system_wrappers/include/logging.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000013
niklase@google.com470e71d2011-07-07 08:21:25 +000014namespace webrtc {
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000015FilePlayer* FilePlayer::CreateFilePlayer(uint32_t instanceID,
niklase@google.com470e71d2011-07-07 08:21:25 +000016 FileFormats fileFormat)
17{
18 switch(fileFormat)
19 {
20 case kFileFormatWavFile:
21 case kFileFormatCompressedFile:
22 case kFileFormatPreencodedFile:
23 case kFileFormatPcm16kHzFile:
24 case kFileFormatPcm8kHzFile:
25 case kFileFormatPcm32kHzFile:
26 // audio formats
27 return new FilePlayerImpl(instanceID, fileFormat);
andresp@webrtc.orge8f50df2015-03-02 13:07:02 +000028 default:
mflodman@webrtc.orgc80d9d92012-02-06 10:11:25 +000029 assert(false);
niklase@google.com470e71d2011-07-07 08:21:25 +000030 return NULL;
31 }
32}
33
34void FilePlayer::DestroyFilePlayer(FilePlayer* player)
35{
36 delete player;
37}
38
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000039FilePlayerImpl::FilePlayerImpl(const uint32_t instanceID,
niklase@google.com470e71d2011-07-07 08:21:25 +000040 const FileFormats fileFormat)
41 : _instanceID(instanceID),
42 _fileFormat(fileFormat),
43 _fileModule(*MediaFile::CreateMediaFile(instanceID)),
44 _decodedLengthInMS(0),
45 _audioDecoder(instanceID),
46 _codec(),
47 _numberOf10MsPerFrame(0),
48 _numberOf10MsInDecoder(0),
henrike@webrtc.org6b9253e2012-02-15 18:48:16 +000049 _resampler(),
niklase@google.com470e71d2011-07-07 08:21:25 +000050 _scaling(1.0)
51{
52 _codec.plfreq = 0;
53}
54
55FilePlayerImpl::~FilePlayerImpl()
56{
57 MediaFile::DestroyMediaFile(&_fileModule);
58}
59
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000060int32_t FilePlayerImpl::Frequency() const
niklase@google.com470e71d2011-07-07 08:21:25 +000061{
62 if(_codec.plfreq == 0)
63 {
64 return -1;
65 }
66 // Make sure that sample rate is 8,16 or 32 kHz. E.g. WAVE files may have
67 // other sampling rates.
68 if(_codec.plfreq == 11000)
69 {
70 return 16000;
71 }
72 else if(_codec.plfreq == 22000)
73 {
74 return 32000;
75 }
76 else if(_codec.plfreq == 44000)
77 {
78 return 32000;
79 }
80 else if(_codec.plfreq == 48000)
81 {
82 return 32000;
83 }
84 else
85 {
86 return _codec.plfreq;
87 }
88}
89
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000090int32_t FilePlayerImpl::AudioCodec(CodecInst& audioCodec) const
niklase@google.com470e71d2011-07-07 08:21:25 +000091{
92 audioCodec = _codec;
93 return 0;
94}
95
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000096int32_t FilePlayerImpl::Get10msAudioFromFile(
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +000097 int16_t* outBuffer,
Peter Kastingdce40cf2015-08-24 14:52:23 -070098 size_t& lengthInSamples,
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +000099 int frequencyInHz)
niklase@google.com470e71d2011-07-07 08:21:25 +0000100{
101 if(_codec.plfreq == 0)
102 {
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +0000103 LOG(LS_WARNING) << "Get10msAudioFromFile() playing not started!"
104 << " codec freq = " << _codec.plfreq
105 << ", wanted freq = " << frequencyInHz;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106 return -1;
107 }
108
109 AudioFrame unresampledAudioFrame;
110 if(STR_CASE_CMP(_codec.plname, "L16") == 0)
111 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000112 unresampledAudioFrame.sample_rate_hz_ = _codec.plfreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113
114 // L16 is un-encoded data. Just pull 10 ms.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000115 size_t lengthInBytes =
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000116 sizeof(unresampledAudioFrame.data_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000117 if (_fileModule.PlayoutAudioData(
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000118 (int8_t*)unresampledAudioFrame.data_,
niklase@google.com470e71d2011-07-07 08:21:25 +0000119 lengthInBytes) == -1)
120 {
121 // End of file reached.
122 return -1;
123 }
124 if(lengthInBytes == 0)
125 {
126 lengthInSamples = 0;
127 return 0;
128 }
129 // One sample is two bytes.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700130 unresampledAudioFrame.samples_per_channel_ = lengthInBytes >> 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700132 } else {
niklase@google.com470e71d2011-07-07 08:21:25 +0000133 // Decode will generate 10 ms of audio data. PlayoutAudioData(..)
134 // expects a full frame. If the frame size is larger than 10 ms,
135 // PlayoutAudioData(..) data should be called proportionally less often.
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000136 int16_t encodedBuffer[MAX_AUDIO_BUFFER_IN_SAMPLES];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000137 size_t encodedLengthInBytes = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000138 if(++_numberOf10MsInDecoder >= _numberOf10MsPerFrame)
139 {
140 _numberOf10MsInDecoder = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000141 size_t bytesFromFile = sizeof(encodedBuffer);
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000142 if (_fileModule.PlayoutAudioData((int8_t*)encodedBuffer,
niklase@google.com470e71d2011-07-07 08:21:25 +0000143 bytesFromFile) == -1)
144 {
145 // End of file reached.
146 return -1;
147 }
148 encodedLengthInBytes = bytesFromFile;
149 }
150 if(_audioDecoder.Decode(unresampledAudioFrame,frequencyInHz,
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000151 (int8_t*)encodedBuffer,
niklase@google.com470e71d2011-07-07 08:21:25 +0000152 encodedLengthInBytes) == -1)
153 {
154 return -1;
155 }
156 }
157
Peter Kastingdce40cf2015-08-24 14:52:23 -0700158 size_t outLen = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000159 if(_resampler.ResetIfNeeded(unresampledAudioFrame.sample_rate_hz_,
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700160 frequencyInHz, 1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000161 {
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +0000162 LOG(LS_WARNING) << "Get10msAudioFromFile() unexpected codec.";
niklase@google.com470e71d2011-07-07 08:21:25 +0000163
164 // New sampling frequency. Update state.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700165 outLen = static_cast<size_t>(frequencyInHz / 100);
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000166 memset(outBuffer, 0, outLen * sizeof(int16_t));
niklase@google.com470e71d2011-07-07 08:21:25 +0000167 return 0;
168 }
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000169 _resampler.Push(unresampledAudioFrame.data_,
170 unresampledAudioFrame.samples_per_channel_,
niklase@google.com470e71d2011-07-07 08:21:25 +0000171 outBuffer,
172 MAX_AUDIO_BUFFER_IN_SAMPLES,
173 outLen);
174
175 lengthInSamples = outLen;
176
177 if(_scaling != 1.0)
178 {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700179 for (size_t i = 0;i < outLen; i++)
niklase@google.com470e71d2011-07-07 08:21:25 +0000180 {
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000181 outBuffer[i] = (int16_t)(outBuffer[i] * _scaling);
niklase@google.com470e71d2011-07-07 08:21:25 +0000182 }
183 }
184 _decodedLengthInMS += 10;
185 return 0;
186}
187
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000188int32_t FilePlayerImpl::RegisterModuleFileCallback(FileCallback* callback)
niklase@google.com470e71d2011-07-07 08:21:25 +0000189{
190 return _fileModule.SetModuleFileCallback(callback);
191}
192
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000193int32_t FilePlayerImpl::SetAudioScaling(float scaleFactor)
niklase@google.com470e71d2011-07-07 08:21:25 +0000194{
195 if((scaleFactor >= 0)&&(scaleFactor <= 2.0))
196 {
197 _scaling = scaleFactor;
198 return 0;
199 }
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +0000200 LOG(LS_WARNING) << "SetAudioScaling() non-allowed scale factor.";
niklase@google.com470e71d2011-07-07 08:21:25 +0000201 return -1;
202}
203
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000204int32_t FilePlayerImpl::StartPlayingFile(const char* fileName,
205 bool loop,
206 uint32_t startPosition,
207 float volumeScaling,
208 uint32_t notification,
209 uint32_t stopPosition,
210 const CodecInst* codecInst)
niklase@google.com470e71d2011-07-07 08:21:25 +0000211{
212 if (_fileFormat == kFileFormatPcm16kHzFile ||
213 _fileFormat == kFileFormatPcm8kHzFile||
214 _fileFormat == kFileFormatPcm32kHzFile )
215 {
216 CodecInst codecInstL16;
217 strncpy(codecInstL16.plname,"L16",32);
218 codecInstL16.pltype = 93;
219 codecInstL16.channels = 1;
220
221 if (_fileFormat == kFileFormatPcm8kHzFile)
222 {
223 codecInstL16.rate = 128000;
224 codecInstL16.plfreq = 8000;
225 codecInstL16.pacsize = 80;
226
227 } else if(_fileFormat == kFileFormatPcm16kHzFile)
228 {
229 codecInstL16.rate = 256000;
230 codecInstL16.plfreq = 16000;
231 codecInstL16.pacsize = 160;
232
233 }else if(_fileFormat == kFileFormatPcm32kHzFile)
234 {
235 codecInstL16.rate = 512000;
236 codecInstL16.plfreq = 32000;
237 codecInstL16.pacsize = 160;
238 } else
239 {
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +0000240 LOG(LS_ERROR) << "StartPlayingFile() sample frequency not "
241 << "supported for PCM format.";
niklase@google.com470e71d2011-07-07 08:21:25 +0000242 return -1;
243 }
244
245 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop,
246 _fileFormat, &codecInstL16,
247 startPosition,
248 stopPosition) == -1)
249 {
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +0000250 LOG(LS_WARNING) << "StartPlayingFile() failed to initialize "
251 << "pcm file " << fileName;
niklase@google.com470e71d2011-07-07 08:21:25 +0000252 return -1;
253 }
254 SetAudioScaling(volumeScaling);
255 }else if(_fileFormat == kFileFormatPreencodedFile)
256 {
257 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop,
258 _fileFormat, codecInst) == -1)
259 {
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +0000260 LOG(LS_WARNING) << "StartPlayingFile() failed to initialize "
261 << "pre-encoded file " << fileName;
niklase@google.com470e71d2011-07-07 08:21:25 +0000262 return -1;
263 }
264 } else
265 {
266 CodecInst* no_inst = NULL;
267 if (_fileModule.StartPlayingAudioFile(fileName, notification, loop,
268 _fileFormat, no_inst,
269 startPosition,
270 stopPosition) == -1)
271 {
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +0000272 LOG(LS_WARNING) << "StartPlayingFile() failed to initialize file "
273 << fileName;
niklase@google.com470e71d2011-07-07 08:21:25 +0000274 return -1;
275 }
276 SetAudioScaling(volumeScaling);
277 }
278 if (SetUpAudioDecoder() == -1)
279 {
280 StopPlayingFile();
281 return -1;
282 }
283 return 0;
284}
285
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000286int32_t FilePlayerImpl::StartPlayingFile(InStream& sourceStream,
287 uint32_t startPosition,
288 float volumeScaling,
289 uint32_t notification,
290 uint32_t stopPosition,
291 const CodecInst* codecInst)
niklase@google.com470e71d2011-07-07 08:21:25 +0000292{
293 if (_fileFormat == kFileFormatPcm16kHzFile ||
294 _fileFormat == kFileFormatPcm32kHzFile ||
295 _fileFormat == kFileFormatPcm8kHzFile)
296 {
297 CodecInst codecInstL16;
298 strncpy(codecInstL16.plname,"L16",32);
299 codecInstL16.pltype = 93;
300 codecInstL16.channels = 1;
301
302 if (_fileFormat == kFileFormatPcm8kHzFile)
303 {
304 codecInstL16.rate = 128000;
305 codecInstL16.plfreq = 8000;
306 codecInstL16.pacsize = 80;
307
308 }else if (_fileFormat == kFileFormatPcm16kHzFile)
309 {
310 codecInstL16.rate = 256000;
311 codecInstL16.plfreq = 16000;
312 codecInstL16.pacsize = 160;
313
314 }else if (_fileFormat == kFileFormatPcm32kHzFile)
315 {
316 codecInstL16.rate = 512000;
317 codecInstL16.plfreq = 32000;
318 codecInstL16.pacsize = 160;
319 }else
320 {
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +0000321 LOG(LS_ERROR) << "StartPlayingFile() sample frequency not "
322 << "supported for PCM format.";
niklase@google.com470e71d2011-07-07 08:21:25 +0000323 return -1;
324 }
325 if (_fileModule.StartPlayingAudioStream(sourceStream, notification,
326 _fileFormat, &codecInstL16,
327 startPosition,
328 stopPosition) == -1)
329 {
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +0000330 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream "
331 << "playout.";
niklase@google.com470e71d2011-07-07 08:21:25 +0000332 return -1;
333 }
334
335 }else if(_fileFormat == kFileFormatPreencodedFile)
336 {
337 if (_fileModule.StartPlayingAudioStream(sourceStream, notification,
338 _fileFormat, codecInst) == -1)
339 {
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +0000340 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream "
341 << "playout.";
niklase@google.com470e71d2011-07-07 08:21:25 +0000342 return -1;
343 }
344 } else {
345 CodecInst* no_inst = NULL;
346 if (_fileModule.StartPlayingAudioStream(sourceStream, notification,
347 _fileFormat, no_inst,
348 startPosition,
349 stopPosition) == -1)
350 {
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +0000351 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream "
352 << "playout.";
niklase@google.com470e71d2011-07-07 08:21:25 +0000353 return -1;
354 }
355 }
356 SetAudioScaling(volumeScaling);
357
358 if (SetUpAudioDecoder() == -1)
359 {
360 StopPlayingFile();
361 return -1;
362 }
363 return 0;
364}
365
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000366int32_t FilePlayerImpl::StopPlayingFile()
niklase@google.com470e71d2011-07-07 08:21:25 +0000367{
368 memset(&_codec, 0, sizeof(CodecInst));
369 _numberOf10MsPerFrame = 0;
370 _numberOf10MsInDecoder = 0;
371 return _fileModule.StopPlaying();
372}
373
374bool FilePlayerImpl::IsPlayingFile() const
375{
376 return _fileModule.IsPlaying();
377}
378
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000379int32_t FilePlayerImpl::GetPlayoutPosition(uint32_t& durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000380{
381 return _fileModule.PlayoutPositionMs(durationMs);
382}
383
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000384int32_t FilePlayerImpl::SetUpAudioDecoder()
niklase@google.com470e71d2011-07-07 08:21:25 +0000385{
386 if ((_fileModule.codec_info(_codec) == -1))
387 {
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +0000388 LOG(LS_WARNING) << "Failed to retrieve codec info of file data.";
niklase@google.com470e71d2011-07-07 08:21:25 +0000389 return -1;
390 }
391 if( STR_CASE_CMP(_codec.plname, "L16") != 0 &&
henrik.lundin8387c5f2015-09-28 09:24:51 -0700392 _audioDecoder.SetDecodeCodec(_codec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000393 {
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +0000394 LOG(LS_WARNING) << "SetUpAudioDecoder() codec " << _codec.plname
395 << " not supported.";
niklase@google.com470e71d2011-07-07 08:21:25 +0000396 return -1;
397 }
398 _numberOf10MsPerFrame = _codec.pacsize / (_codec.plfreq / 100);
399 _numberOf10MsInDecoder = 0;
400 return 0;
401}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000402} // namespace webrtc