OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/utility/include/file_recorder.h" | 11 #include "webrtc/modules/utility/include/file_recorder.h" |
12 | 12 |
13 #include <list> | 13 #include <list> |
14 | 14 |
15 #include "webrtc/base/platform_thread.h" | 15 #include "webrtc/base/platform_thread.h" |
16 #include "webrtc/common_audio/resampler/include/resampler.h" | 16 #include "webrtc/common_audio/resampler/include/resampler.h" |
17 #include "webrtc/common_types.h" | 17 #include "webrtc/common_types.h" |
18 #include "webrtc/engine_configurations.h" | 18 #include "webrtc/engine_configurations.h" |
19 #include "webrtc/engine_configurations.h" | |
20 #include "webrtc/modules/include/module_common_types.h" | 19 #include "webrtc/modules/include/module_common_types.h" |
21 #include "webrtc/modules/media_file/media_file.h" | 20 #include "webrtc/modules/media_file/media_file.h" |
22 #include "webrtc/modules/media_file/media_file.h" | |
23 #include "webrtc/modules/media_file/media_file_defines.h" | 21 #include "webrtc/modules/media_file/media_file_defines.h" |
24 #include "webrtc/modules/utility/source/coder.h" | 22 #include "webrtc/modules/utility/source/coder.h" |
25 #include "webrtc/system_wrappers/include/event_wrapper.h" | 23 #include "webrtc/system_wrappers/include/event_wrapper.h" |
26 #include "webrtc/system_wrappers/include/logging.h" | 24 #include "webrtc/system_wrappers/include/logging.h" |
27 #include "webrtc/typedefs.h" | 25 #include "webrtc/typedefs.h" |
28 | 26 |
29 namespace webrtc { | 27 namespace webrtc { |
30 | 28 |
31 namespace { | 29 namespace { |
32 | 30 |
33 // The largest decoded frame size in samples (60ms with 32kHz sample rate). | 31 // The largest decoded frame size in samples (60ms with 32kHz sample rate). |
34 enum { MAX_AUDIO_BUFFER_IN_SAMPLES = 60 * 32 }; | 32 enum { MAX_AUDIO_BUFFER_IN_SAMPLES = 60 * 32 }; |
35 enum { MAX_AUDIO_BUFFER_IN_BYTES = MAX_AUDIO_BUFFER_IN_SAMPLES * 2 }; | 33 enum { MAX_AUDIO_BUFFER_IN_BYTES = MAX_AUDIO_BUFFER_IN_SAMPLES * 2 }; |
36 enum { kMaxAudioBufferQueueLength = 100 }; | 34 enum { kMaxAudioBufferQueueLength = 100 }; |
37 | 35 |
38 class CriticalSectionWrapper; | 36 class CriticalSectionWrapper; |
39 | 37 |
40 class FileRecorderImpl : public FileRecorder { | 38 class FileRecorderImpl : public FileRecorder { |
41 public: | 39 public: |
42 FileRecorderImpl(uint32_t instanceID, FileFormats fileFormat); | 40 FileRecorderImpl(uint32_t instanceID, FileFormats fileFormat); |
43 ~FileRecorderImpl() override; | 41 ~FileRecorderImpl() override; |
44 | 42 |
45 // FileRecorder functions. | 43 // FileRecorder functions. |
46 int32_t RegisterModuleFileCallback(FileCallback* callback) override; | 44 int32_t RegisterModuleFileCallback(FileCallback* callback) override; |
47 FileFormats RecordingFileFormat() const override; | 45 FileFormats RecordingFileFormat() const override; |
48 int32_t StartRecordingAudioFile(const char* fileName, | 46 int32_t StartRecordingAudioFile(const char* fileName, |
49 const CodecInst& codecInst, | 47 const CodecInst& codecInst, |
50 uint32_t notificationTimeMs) override; | 48 uint32_t notificationTimeMs) override; |
51 int32_t StartRecordingAudioFile(OutStream& destStream, | 49 int32_t StartRecordingAudioFile(OutStream* destStream, |
52 const CodecInst& codecInst, | 50 const CodecInst& codecInst, |
53 uint32_t notificationTimeMs) override; | 51 uint32_t notificationTimeMs) override; |
54 int32_t StopRecording() override; | 52 int32_t StopRecording() override; |
55 bool IsRecording() const override; | 53 bool IsRecording() const override; |
56 int32_t codec_info(CodecInst& codecInst) const override; | 54 int32_t codec_info(CodecInst* codecInst) const override; |
57 int32_t RecordAudioToFile(const AudioFrame& frame) override; | 55 int32_t RecordAudioToFile(const AudioFrame& frame) override; |
58 | 56 |
59 private: | 57 private: |
60 int32_t WriteEncodedAudioData(const int8_t* audioBuffer, size_t bufferLength); | 58 int32_t WriteEncodedAudioData(const int8_t* audioBuffer, size_t bufferLength); |
61 | 59 |
62 int32_t SetUpAudioEncoder(); | 60 int32_t SetUpAudioEncoder(); |
63 | 61 |
64 uint32_t _instanceID; | 62 uint32_t _instanceID; |
65 FileFormats _fileFormat; | 63 FileFormats _fileFormat; |
66 MediaFile* _moduleFile; | 64 MediaFile* _moduleFile; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 LOG(LS_WARNING) << "Failed to initialize file " << fileName | 111 LOG(LS_WARNING) << "Failed to initialize file " << fileName |
114 << " for recording."; | 112 << " for recording."; |
115 | 113 |
116 if (IsRecording()) { | 114 if (IsRecording()) { |
117 StopRecording(); | 115 StopRecording(); |
118 } | 116 } |
119 } | 117 } |
120 return retVal; | 118 return retVal; |
121 } | 119 } |
122 | 120 |
123 int32_t FileRecorderImpl::StartRecordingAudioFile(OutStream& destStream, | 121 int32_t FileRecorderImpl::StartRecordingAudioFile(OutStream* destStream, |
124 const CodecInst& codecInst, | 122 const CodecInst& codecInst, |
125 uint32_t notificationTimeMs) { | 123 uint32_t notificationTimeMs) { |
126 codec_info_ = codecInst; | 124 codec_info_ = codecInst; |
127 int32_t retVal = _moduleFile->StartRecordingAudioStream( | 125 int32_t retVal = _moduleFile->StartRecordingAudioStream( |
128 destStream, _fileFormat, codecInst, notificationTimeMs); | 126 *destStream, _fileFormat, codecInst, notificationTimeMs); |
129 | 127 |
130 if (retVal == 0) { | 128 if (retVal == 0) { |
131 retVal = SetUpAudioEncoder(); | 129 retVal = SetUpAudioEncoder(); |
132 } | 130 } |
133 if (retVal != 0) { | 131 if (retVal != 0) { |
134 LOG(LS_WARNING) << "Failed to initialize outStream for recording."; | 132 LOG(LS_WARNING) << "Failed to initialize outStream for recording."; |
135 | 133 |
136 if (IsRecording()) { | 134 if (IsRecording()) { |
137 StopRecording(); | 135 StopRecording(); |
138 } | 136 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 | 184 |
187 const AudioFrame* ptrAudioFrame = &incomingAudioFrame; | 185 const AudioFrame* ptrAudioFrame = &incomingAudioFrame; |
188 if (tempAudioFrame.samples_per_channel_ != 0) { | 186 if (tempAudioFrame.samples_per_channel_ != 0) { |
189 // If ptrAudioFrame is not empty it contains the audio to be recorded. | 187 // If ptrAudioFrame is not empty it contains the audio to be recorded. |
190 ptrAudioFrame = &tempAudioFrame; | 188 ptrAudioFrame = &tempAudioFrame; |
191 } | 189 } |
192 | 190 |
193 // Encode the audio data before writing to file. Don't encode if the codec | 191 // Encode the audio data before writing to file. Don't encode if the codec |
194 // is PCM. | 192 // is PCM. |
195 // NOTE: stereo recording is only supported for WAV files. | 193 // NOTE: stereo recording is only supported for WAV files. |
196 // TODO (hellner): WAV expect PCM in little endian byte order. Not | 194 // TODO(hellner): WAV expect PCM in little endian byte order. Not |
197 // "encoding" with PCM coder should be a problem for big endian systems. | 195 // "encoding" with PCM coder should be a problem for big endian systems. |
198 size_t encodedLenInBytes = 0; | 196 size_t encodedLenInBytes = 0; |
199 if (_fileFormat == kFileFormatPreencodedFile || | 197 if (_fileFormat == kFileFormatPreencodedFile || |
200 STR_CASE_CMP(codec_info_.plname, "L16") != 0) { | 198 STR_CASE_CMP(codec_info_.plname, "L16") != 0) { |
201 if (_audioEncoder.Encode(*ptrAudioFrame, _audioBuffer, encodedLenInBytes) == | 199 if (_audioEncoder.Encode(*ptrAudioFrame, _audioBuffer, |
202 -1) { | 200 &encodedLenInBytes) == -1) { |
203 LOG(LS_WARNING) << "RecordAudioToFile() codec " << codec_info_.plname | 201 LOG(LS_WARNING) << "RecordAudioToFile() codec " << codec_info_.plname |
204 << " not supported or failed to encode stream."; | 202 << " not supported or failed to encode stream."; |
205 return -1; | 203 return -1; |
206 } | 204 } |
207 } else { | 205 } else { |
208 size_t outLen = 0; | 206 size_t outLen = 0; |
209 _audioResampler.ResetIfNeeded(ptrAudioFrame->sample_rate_hz_, | 207 _audioResampler.ResetIfNeeded(ptrAudioFrame->sample_rate_hz_, |
210 codec_info_.plfreq, | 208 codec_info_.plfreq, |
211 ptrAudioFrame->num_channels_); | 209 ptrAudioFrame->num_channels_); |
212 _audioResampler.Push( | 210 _audioResampler.Push( |
213 ptrAudioFrame->data_, | 211 ptrAudioFrame->data_, |
214 ptrAudioFrame->samples_per_channel_ * ptrAudioFrame->num_channels_, | 212 ptrAudioFrame->samples_per_channel_ * ptrAudioFrame->num_channels_, |
215 (int16_t*)_audioBuffer, MAX_AUDIO_BUFFER_IN_BYTES, outLen); | 213 reinterpret_cast<int16_t*>(_audioBuffer), MAX_AUDIO_BUFFER_IN_BYTES, |
| 214 outLen); |
216 encodedLenInBytes = outLen * sizeof(int16_t); | 215 encodedLenInBytes = outLen * sizeof(int16_t); |
217 } | 216 } |
218 | 217 |
219 // Codec may not be operating at a frame rate of 10 ms. Whenever enough | 218 // Codec may not be operating at a frame rate of 10 ms. Whenever enough |
220 // 10 ms chunks of data has been pushed to the encoder an encoded frame | 219 // 10 ms chunks of data has been pushed to the encoder an encoded frame |
221 // will be available. Wait until then. | 220 // will be available. Wait until then. |
222 if (encodedLenInBytes) { | 221 if (encodedLenInBytes) { |
223 if (WriteEncodedAudioData(_audioBuffer, encodedLenInBytes) == -1) { | 222 if (WriteEncodedAudioData(_audioBuffer, encodedLenInBytes) == -1) { |
224 return -1; | 223 return -1; |
225 } | 224 } |
226 } | 225 } |
227 return 0; | 226 return 0; |
228 } | 227 } |
229 | 228 |
230 int32_t FileRecorderImpl::SetUpAudioEncoder() { | 229 int32_t FileRecorderImpl::SetUpAudioEncoder() { |
231 if (_fileFormat == kFileFormatPreencodedFile || | 230 if (_fileFormat == kFileFormatPreencodedFile || |
232 STR_CASE_CMP(codec_info_.plname, "L16") != 0) { | 231 STR_CASE_CMP(codec_info_.plname, "L16") != 0) { |
233 if (_audioEncoder.SetEncodeCodec(codec_info_) == -1) { | 232 if (_audioEncoder.SetEncodeCodec(codec_info_) == -1) { |
234 LOG(LS_ERROR) << "SetUpAudioEncoder() codec " << codec_info_.plname | 233 LOG(LS_ERROR) << "SetUpAudioEncoder() codec " << codec_info_.plname |
235 << " not supported."; | 234 << " not supported."; |
236 return -1; | 235 return -1; |
237 } | 236 } |
238 } | 237 } |
239 return 0; | 238 return 0; |
240 } | 239 } |
241 | 240 |
242 int32_t FileRecorderImpl::codec_info(CodecInst& codecInst) const { | 241 int32_t FileRecorderImpl::codec_info(CodecInst* codecInst) const { |
243 if (codec_info_.plfreq == 0) { | 242 if (codec_info_.plfreq == 0) { |
244 return -1; | 243 return -1; |
245 } | 244 } |
246 codecInst = codec_info_; | 245 *codecInst = codec_info_; |
247 return 0; | 246 return 0; |
248 } | 247 } |
249 | 248 |
250 int32_t FileRecorderImpl::WriteEncodedAudioData(const int8_t* audioBuffer, | 249 int32_t FileRecorderImpl::WriteEncodedAudioData(const int8_t* audioBuffer, |
251 size_t bufferLength) { | 250 size_t bufferLength) { |
252 return _moduleFile->IncomingAudioData(audioBuffer, bufferLength); | 251 return _moduleFile->IncomingAudioData(audioBuffer, bufferLength); |
253 } | 252 } |
254 | 253 |
255 } // namespace | 254 } // namespace |
256 | 255 |
257 std::unique_ptr<FileRecorder> FileRecorder::CreateFileRecorder( | 256 std::unique_ptr<FileRecorder> FileRecorder::CreateFileRecorder( |
258 uint32_t instanceID, | 257 uint32_t instanceID, |
259 FileFormats fileFormat) { | 258 FileFormats fileFormat) { |
260 return std::unique_ptr<FileRecorder>( | 259 return std::unique_ptr<FileRecorder>( |
261 new FileRecorderImpl(instanceID, fileFormat)); | 260 new FileRecorderImpl(instanceID, fileFormat)); |
262 } | 261 } |
263 | 262 |
264 } // namespace webrtc | 263 } // namespace webrtc |
OLD | NEW |