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/engine_configurations.h" | |
12 #include "webrtc/modules/media_file/media_file.h" | |
13 #include "webrtc/modules/utility/source/file_recorder_impl.h" | |
14 #include "webrtc/system_wrappers/include/logging.h" | |
15 | |
16 namespace webrtc { | |
17 FileRecorder* FileRecorder::CreateFileRecorder(uint32_t instanceID, | |
18 FileFormats fileFormat) { | |
19 return new FileRecorderImpl(instanceID, fileFormat); | |
20 } | |
21 | |
22 void FileRecorder::DestroyFileRecorder(FileRecorder* recorder) { | |
23 delete recorder; | |
24 } | |
25 | |
26 FileRecorderImpl::FileRecorderImpl(uint32_t instanceID, FileFormats fileFormat) | |
27 : _instanceID(instanceID), | |
28 _fileFormat(fileFormat), | |
29 _moduleFile(MediaFile::CreateMediaFile(_instanceID)), | |
30 codec_info_(), | |
31 _audioBuffer(), | |
32 _audioEncoder(instanceID), | |
33 _audioResampler() {} | |
34 | |
35 FileRecorderImpl::~FileRecorderImpl() { | |
36 MediaFile::DestroyMediaFile(_moduleFile); | |
37 } | |
38 | |
39 FileFormats FileRecorderImpl::RecordingFileFormat() const { | |
40 return _fileFormat; | |
41 } | |
42 | |
43 int32_t FileRecorderImpl::RegisterModuleFileCallback(FileCallback* callback) { | |
44 if (_moduleFile == NULL) { | |
45 return -1; | |
46 } | |
47 return _moduleFile->SetModuleFileCallback(callback); | |
48 } | |
49 | |
50 int32_t FileRecorderImpl::StartRecordingAudioFile(const char* fileName, | |
51 const CodecInst& codecInst, | |
52 uint32_t notificationTimeMs) { | |
53 if (_moduleFile == NULL) { | |
54 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(); | |
70 } | |
71 } | |
72 return retVal; | |
73 } | |
74 | |
75 int32_t FileRecorderImpl::StartRecordingAudioFile(OutStream& destStream, | |
76 const CodecInst& codecInst, | |
77 uint32_t notificationTimeMs) { | |
78 codec_info_ = codecInst; | |
79 int32_t retVal = _moduleFile->StartRecordingAudioStream( | |
80 destStream, _fileFormat, codecInst, notificationTimeMs); | |
81 | |
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(); | |
90 } | |
91 } | |
92 return retVal; | |
93 } | |
94 | |
95 int32_t FileRecorderImpl::StopRecording() { | |
96 memset(&codec_info_, 0, sizeof(CodecInst)); | |
97 return _moduleFile->StopRecording(); | |
98 } | |
99 | |
100 bool FileRecorderImpl::IsRecording() const { | |
101 return _moduleFile->IsRecording(); | |
102 } | |
103 | |
104 int32_t FileRecorderImpl::RecordAudioToFile( | |
105 const AudioFrame& incomingAudioFrame) { | |
106 if (codec_info_.plfreq == 0) { | |
107 LOG(LS_WARNING) << "RecordAudioToFile() recording audio is not " | |
108 << "turned on."; | |
109 return -1; | |
110 } | |
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); | |
125 } | |
126 } else if (incomingAudioFrame.num_channels_ == 1 && _moduleFile->IsStereo()) { | |
127 // Recording stereo but incoming audio is mono. | |
128 tempAudioFrame.num_channels_ = 2; | |
129 tempAudioFrame.sample_rate_hz_ = incomingAudioFrame.sample_rate_hz_; | |
130 tempAudioFrame.samples_per_channel_ = | |
131 incomingAudioFrame.samples_per_channel_; | |
132 for (size_t i = 0; i < (incomingAudioFrame.samples_per_channel_); i++) { | |
133 // Duplicate sample to both channels | |
134 tempAudioFrame.data_[2 * i] = incomingAudioFrame.data_[i]; | |
135 tempAudioFrame.data_[2 * i + 1] = incomingAudioFrame.data_[i]; | |
136 } | |
137 } | |
138 | |
139 const AudioFrame* ptrAudioFrame = &incomingAudioFrame; | |
140 if (tempAudioFrame.samples_per_channel_ != 0) { | |
141 // If ptrAudioFrame is not empty it contains the audio to be recorded. | |
142 ptrAudioFrame = &tempAudioFrame; | |
143 } | |
144 | |
145 // Encode the audio data before writing to file. Don't encode if the codec | |
146 // is PCM. | |
147 // NOTE: stereo recording is only supported for WAV files. | |
148 // TODO (hellner): WAV expect PCM in little endian byte order. Not | |
149 // "encoding" with PCM coder should be a problem for big endian systems. | |
150 size_t encodedLenInBytes = 0; | |
151 if (_fileFormat == kFileFormatPreencodedFile || | |
152 STR_CASE_CMP(codec_info_.plname, "L16") != 0) { | |
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; | |
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 } | |
170 | |
171 // Codec may not be operating at a frame rate of 10 ms. Whenever enough | |
172 // 10 ms chunks of data has been pushed to the encoder an encoded frame | |
173 // will be available. Wait until then. | |
174 if (encodedLenInBytes) { | |
175 if (WriteEncodedAudioData(_audioBuffer, encodedLenInBytes) == -1) { | |
176 return -1; | |
177 } | |
178 } | |
179 return 0; | |
180 } | |
181 | |
182 int32_t FileRecorderImpl::SetUpAudioEncoder() { | |
183 if (_fileFormat == kFileFormatPreencodedFile || | |
184 STR_CASE_CMP(codec_info_.plname, "L16") != 0) { | |
185 if (_audioEncoder.SetEncodeCodec(codec_info_) == -1) { | |
186 LOG(LS_ERROR) << "SetUpAudioEncoder() codec " << codec_info_.plname | |
187 << " not supported."; | |
188 return -1; | |
189 } | |
190 } | |
191 return 0; | |
192 } | |
193 | |
194 int32_t FileRecorderImpl::codec_info(CodecInst& codecInst) const { | |
195 if (codec_info_.plfreq == 0) { | |
196 return -1; | |
197 } | |
198 codecInst = codec_info_; | |
199 return 0; | |
200 } | |
201 | |
202 int32_t FileRecorderImpl::WriteEncodedAudioData(const int8_t* audioBuffer, | |
203 size_t bufferLength) { | |
204 return _moduleFile->IncomingAudioData(audioBuffer, bufferLength); | |
205 } | |
206 } // namespace webrtc | |
OLD | NEW |