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