OLD | NEW |
| (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/voice_engine/file_recorder_impl.h" | |
12 | |
13 #include "webrtc/engine_configurations.h" | |
14 #include "webrtc/modules/media_file/media_file.h" | |
15 #include "webrtc/system_wrappers/include/logging.h" | |
16 | |
17 namespace webrtc { | |
18 FileRecorder* FileRecorder::CreateFileRecorder(uint32_t instanceID, | |
19 FileFormats fileFormat) | |
20 { | |
21 return new FileRecorderImpl(instanceID, fileFormat); | |
22 } | |
23 | |
24 void FileRecorder::DestroyFileRecorder(FileRecorder* recorder) | |
25 { | |
26 delete recorder; | |
27 } | |
28 | |
29 FileRecorderImpl::FileRecorderImpl(uint32_t instanceID, | |
30 FileFormats fileFormat) | |
31 : _instanceID(instanceID), | |
32 _fileFormat(fileFormat), | |
33 _moduleFile(MediaFile::CreateMediaFile(_instanceID)), | |
34 codec_info_(), | |
35 _audioBuffer(), | |
36 _audioEncoder(instanceID), | |
37 _audioResampler() | |
38 { | |
39 } | |
40 | |
41 FileRecorderImpl::~FileRecorderImpl() | |
42 { | |
43 MediaFile::DestroyMediaFile(_moduleFile); | |
44 } | |
45 | |
46 FileFormats FileRecorderImpl::RecordingFileFormat() const | |
47 { | |
48 return _fileFormat; | |
49 } | |
50 | |
51 int32_t FileRecorderImpl::RegisterModuleFileCallback( | |
52 FileCallback* callback) | |
53 { | |
54 if(_moduleFile == NULL) | |
55 { | |
56 return -1; | |
57 } | |
58 return _moduleFile->SetModuleFileCallback(callback); | |
59 } | |
60 | |
61 int32_t FileRecorderImpl::StartRecordingAudioFile( | |
62 const char* fileName, | |
63 const CodecInst& codecInst, | |
64 uint32_t notificationTimeMs) | |
65 { | |
66 if(_moduleFile == NULL) | |
67 { | |
68 return -1; | |
69 } | |
70 codec_info_ = codecInst; | |
71 int32_t retVal = 0; | |
72 retVal =_moduleFile->StartRecordingAudioFile(fileName, _fileFormat, | |
73 codecInst, | |
74 notificationTimeMs); | |
75 | |
76 if( retVal == 0) | |
77 { | |
78 retVal = SetUpAudioEncoder(); | |
79 } | |
80 if( retVal != 0) | |
81 { | |
82 LOG(LS_WARNING) << "Failed to initialize file " << fileName | |
83 << " for recording."; | |
84 | |
85 if(IsRecording()) | |
86 { | |
87 StopRecording(); | |
88 } | |
89 } | |
90 return retVal; | |
91 } | |
92 | |
93 int32_t FileRecorderImpl::StartRecordingAudioFile( | |
94 OutStream& destStream, | |
95 const CodecInst& codecInst, | |
96 uint32_t notificationTimeMs) | |
97 { | |
98 codec_info_ = codecInst; | |
99 int32_t retVal = _moduleFile->StartRecordingAudioStream( | |
100 destStream, | |
101 _fileFormat, | |
102 codecInst, | |
103 notificationTimeMs); | |
104 | |
105 if( retVal == 0) | |
106 { | |
107 retVal = SetUpAudioEncoder(); | |
108 } | |
109 if( retVal != 0) | |
110 { | |
111 LOG(LS_WARNING) << "Failed to initialize outStream for recording."; | |
112 | |
113 if(IsRecording()) | |
114 { | |
115 StopRecording(); | |
116 } | |
117 } | |
118 return retVal; | |
119 } | |
120 | |
121 int32_t FileRecorderImpl::StopRecording() | |
122 { | |
123 memset(&codec_info_, 0, sizeof(CodecInst)); | |
124 return _moduleFile->StopRecording(); | |
125 } | |
126 | |
127 bool FileRecorderImpl::IsRecording() const | |
128 { | |
129 return _moduleFile->IsRecording(); | |
130 } | |
131 | |
132 int32_t FileRecorderImpl::RecordAudioToFile( | |
133 const AudioFrame& incomingAudioFrame) | |
134 { | |
135 if (codec_info_.plfreq == 0) | |
136 { | |
137 LOG(LS_WARNING) << "RecordAudioToFile() recording audio is not " | |
138 << "turned on."; | |
139 return -1; | |
140 } | |
141 AudioFrame tempAudioFrame; | |
142 tempAudioFrame.samples_per_channel_ = 0; | |
143 if( incomingAudioFrame.num_channels_ == 2 && | |
144 !_moduleFile->IsStereo()) | |
145 { | |
146 // Recording mono but incoming audio is (interleaved) stereo. | |
147 tempAudioFrame.num_channels_ = 1; | |
148 tempAudioFrame.sample_rate_hz_ = incomingAudioFrame.sample_rate_hz_; | |
149 tempAudioFrame.samples_per_channel_ = | |
150 incomingAudioFrame.samples_per_channel_; | |
151 for (size_t i = 0; | |
152 i < (incomingAudioFrame.samples_per_channel_); i++) | |
153 { | |
154 // Sample value is the average of left and right buffer rounded to | |
155 // closest integer value. Note samples can be either 1 or 2 byte. | |
156 tempAudioFrame.data_[i] = | |
157 ((incomingAudioFrame.data_[2 * i] + | |
158 incomingAudioFrame.data_[(2 * i) + 1] + 1) >> 1); | |
159 } | |
160 } | |
161 else if( incomingAudioFrame.num_channels_ == 1 && | |
162 _moduleFile->IsStereo()) | |
163 { | |
164 // Recording stereo but incoming audio is mono. | |
165 tempAudioFrame.num_channels_ = 2; | |
166 tempAudioFrame.sample_rate_hz_ = incomingAudioFrame.sample_rate_hz_; | |
167 tempAudioFrame.samples_per_channel_ = | |
168 incomingAudioFrame.samples_per_channel_; | |
169 for (size_t i = 0; | |
170 i < (incomingAudioFrame.samples_per_channel_); i++) | |
171 { | |
172 // Duplicate sample to both channels | |
173 tempAudioFrame.data_[2*i] = | |
174 incomingAudioFrame.data_[i]; | |
175 tempAudioFrame.data_[2*i+1] = | |
176 incomingAudioFrame.data_[i]; | |
177 } | |
178 } | |
179 | |
180 const AudioFrame* ptrAudioFrame = &incomingAudioFrame; | |
181 if(tempAudioFrame.samples_per_channel_ != 0) | |
182 { | |
183 // If ptrAudioFrame is not empty it contains the audio to be recorded. | |
184 ptrAudioFrame = &tempAudioFrame; | |
185 } | |
186 | |
187 // Encode the audio data before writing to file. Don't encode if the codec | |
188 // is PCM. | |
189 // NOTE: stereo recording is only supported for WAV files. | |
190 // TODO (hellner): WAV expect PCM in little endian byte order. Not | |
191 // "encoding" with PCM coder should be a problem for big endian systems. | |
192 size_t encodedLenInBytes = 0; | |
193 if (_fileFormat == kFileFormatPreencodedFile || | |
194 STR_CASE_CMP(codec_info_.plname, "L16") != 0) | |
195 { | |
196 if (_audioEncoder.Encode(*ptrAudioFrame, _audioBuffer, | |
197 encodedLenInBytes) == -1) | |
198 { | |
199 LOG(LS_WARNING) << "RecordAudioToFile() codec " | |
200 << codec_info_.plname | |
201 << " not supported or failed to encode stream."; | |
202 return -1; | |
203 } | |
204 } else { | |
205 size_t outLen = 0; | |
206 _audioResampler.ResetIfNeeded(ptrAudioFrame->sample_rate_hz_, | |
207 codec_info_.plfreq, | |
208 ptrAudioFrame->num_channels_); | |
209 _audioResampler.Push(ptrAudioFrame->data_, | |
210 ptrAudioFrame->samples_per_channel_ * | |
211 ptrAudioFrame->num_channels_, | |
212 (int16_t*)_audioBuffer, | |
213 MAX_AUDIO_BUFFER_IN_BYTES, outLen); | |
214 encodedLenInBytes = outLen * sizeof(int16_t); | |
215 } | |
216 | |
217 // Codec may not be operating at a frame rate of 10 ms. Whenever enough | |
218 // 10 ms chunks of data has been pushed to the encoder an encoded frame | |
219 // will be available. Wait until then. | |
220 if (encodedLenInBytes) | |
221 { | |
222 if (WriteEncodedAudioData(_audioBuffer, encodedLenInBytes) == -1) | |
223 { | |
224 return -1; | |
225 } | |
226 } | |
227 return 0; | |
228 } | |
229 | |
230 int32_t FileRecorderImpl::SetUpAudioEncoder() | |
231 { | |
232 if (_fileFormat == kFileFormatPreencodedFile || | |
233 STR_CASE_CMP(codec_info_.plname, "L16") != 0) | |
234 { | |
235 if(_audioEncoder.SetEncodeCodec(codec_info_) == -1) | |
236 { | |
237 LOG(LS_ERROR) << "SetUpAudioEncoder() codec " | |
238 << codec_info_.plname << " not supported."; | |
239 return -1; | |
240 } | |
241 } | |
242 return 0; | |
243 } | |
244 | |
245 int32_t FileRecorderImpl::codec_info(CodecInst& codecInst) const | |
246 { | |
247 if(codec_info_.plfreq == 0) | |
248 { | |
249 return -1; | |
250 } | |
251 codecInst = codec_info_; | |
252 return 0; | |
253 } | |
254 | |
255 int32_t FileRecorderImpl::WriteEncodedAudioData(const int8_t* audioBuffer, | |
256 size_t bufferLength) | |
257 { | |
258 return _moduleFile->IncomingAudioData(audioBuffer, bufferLength); | |
259 } | |
260 } // namespace webrtc | |
OLD | NEW |