Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: webrtc/modules/utility/source/file_player.cc

Issue 2321473004: Move coder, file_player, and file_recorder to webrtc/voice_engine (Closed)
Patch Set: Update .gyp files Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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
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"
20 #include "webrtc/system_wrappers/include/logging.h"
21 #include "webrtc/typedefs.h"
22
23 namespace webrtc {
24
25 namespace {
26
27 class FilePlayerImpl : public FilePlayer {
28 public:
29 FilePlayerImpl(uint32_t instanceID, FileFormats fileFormat);
30 ~FilePlayerImpl() override;
31
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;
55
56 private:
57 int32_t SetUpAudioDecoder();
58
59 const FileFormats _fileFormat;
60 MediaFile& _fileModule;
61
62 uint32_t _decodedLengthInMS;
63
64 AudioCoder _audioDecoder;
65
66 CodecInst _codec;
67 int32_t _numberOf10MsPerFrame;
68 int32_t _numberOf10MsInDecoder;
69
70 Resampler _resampler;
71 float _scaling;
72 };
73
74 FilePlayerImpl::FilePlayerImpl(const uint32_t instanceID,
75 const FileFormats fileFormat)
76 : _fileFormat(fileFormat),
77 _fileModule(*MediaFile::CreateMediaFile(instanceID)),
78 _decodedLengthInMS(0),
79 _audioDecoder(instanceID),
80 _codec(),
81 _numberOf10MsPerFrame(0),
82 _numberOf10MsInDecoder(0),
83 _resampler(),
84 _scaling(1.0) {
85 _codec.plfreq = 0;
86 }
87
88 FilePlayerImpl::~FilePlayerImpl() {
89 MediaFile::DestroyMediaFile(&_fileModule);
90 }
91
92 int32_t FilePlayerImpl::Frequency() const {
93 if (_codec.plfreq == 0) {
94 return -1;
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
111 int32_t FilePlayerImpl::AudioCodec(CodecInst* audioCodec) const {
112 *audioCodec = _codec;
113 return 0;
114 }
115
116 int32_t FilePlayerImpl::Get10msAudioFromFile(int16_t* outBuffer,
117 size_t* lengthInSamples,
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_);
132 if (_fileModule.PlayoutAudioData(
133 reinterpret_cast<int8_t*>(unresampledAudioFrame.data_),
134 lengthInBytes) == -1) {
135 // End of file reached.
136 return -1;
137 }
138 if (lengthInBytes == 0) {
139 *lengthInSamples = 0;
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);
154 if (_fileModule.PlayoutAudioData(reinterpret_cast<int8_t*>(encodedBuffer),
155 bytesFromFile) == -1) {
156 // End of file reached.
157 return -1;
158 }
159 encodedLengthInBytes = bytesFromFile;
160 }
161 if (_audioDecoder.Decode(&unresampledAudioFrame, frequencyInHz,
162 reinterpret_cast<int8_t*>(encodedBuffer),
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
182 *lengthInSamples = outLen;
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;
204 }
205
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,
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;
220
221 if (_fileFormat == kFileFormatPcm8kHzFile) {
222 codecInstL16.rate = 128000;
223 codecInstL16.plfreq = 8000;
224 codecInstL16.pacsize = 80;
225
226 } else if (_fileFormat == kFileFormatPcm16kHzFile) {
227 codecInstL16.rate = 256000;
228 codecInstL16.plfreq = 16000;
229 codecInstL16.pacsize = 160;
230
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;
239 }
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;
247 }
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;
272 }
273
274 int32_t FilePlayerImpl::StartPlayingFile(InStream* sourceStream,
275 uint32_t startPosition,
276 float volumeScaling,
277 uint32_t notification,
278 uint32_t stopPosition,
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;
287
288 if (_fileFormat == kFileFormatPcm8kHzFile) {
289 codecInstL16.rate = 128000;
290 codecInstL16.plfreq = 8000;
291 codecInstL16.pacsize = 80;
292
293 } else if (_fileFormat == kFileFormatPcm16kHzFile) {
294 codecInstL16.rate = 256000;
295 codecInstL16.plfreq = 16000;
296 codecInstL16.pacsize = 160;
297
298 } else if (_fileFormat == kFileFormatPcm32kHzFile) {
299 codecInstL16.rate = 512000;
300 codecInstL16.plfreq = 32000;
301 codecInstL16.pacsize = 160;
302 } else {
303 LOG(LS_ERROR) << "StartPlayingFile() sample frequency not "
304 << "supported for PCM format.";
305 return -1;
306 }
307 if (_fileModule.StartPlayingAudioStream(
308 *sourceStream, notification, _fileFormat, &codecInstL16,
309 startPosition, stopPosition) == -1) {
310 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream "
311 << "playout.";
312 return -1;
313 }
314
315 } else if (_fileFormat == kFileFormatPreencodedFile) {
316 if (_fileModule.StartPlayingAudioStream(*sourceStream, notification,
317 _fileFormat, codecInst) == -1) {
318 LOG(LS_ERROR) << "StartPlayingFile() failed to initialize stream "
319 << "playout.";
320 return -1;
321 }
322 } else {
323 CodecInst* no_inst = NULL;
324 if (_fileModule.StartPlayingAudioStream(*sourceStream, notification,
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;
339 }
340
341 int32_t FilePlayerImpl::StopPlayingFile() {
342 memset(&_codec, 0, sizeof(CodecInst));
343 _numberOf10MsPerFrame = 0;
344 _numberOf10MsInDecoder = 0;
345 return _fileModule.StopPlaying();
346 }
347
348 bool FilePlayerImpl::IsPlayingFile() const {
349 return _fileModule.IsPlaying();
350 }
351
352 int32_t FilePlayerImpl::GetPlayoutPosition(uint32_t* durationMs) {
353 return _fileModule.PlayoutPositionMs(*durationMs);
354 }
355
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;
370 }
371
372 } // namespace
373
374 std::unique_ptr<FilePlayer> FilePlayer::CreateFilePlayer(
375 uint32_t instanceID,
376 FileFormats fileFormat) {
377 switch (fileFormat) {
378 case kFileFormatWavFile:
379 case kFileFormatCompressedFile:
380 case kFileFormatPreencodedFile:
381 case kFileFormatPcm16kHzFile:
382 case kFileFormatPcm8kHzFile:
383 case kFileFormatPcm32kHzFile:
384 // audio formats
385 return std::unique_ptr<FilePlayer>(
386 new FilePlayerImpl(instanceID, fileFormat));
387 default:
388 assert(false);
389 return nullptr;
390 }
391 }
392
393 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/utility/source/coder.cc ('k') | webrtc/modules/utility/source/file_player_unittests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698