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 { |
| 20 return new FileRecorderImpl(instanceID, fileFormat); |
| 21 } |
| 22 |
| 23 void FileRecorder::DestroyFileRecorder(FileRecorder* recorder) |
| 24 { |
| 25 delete recorder; |
| 26 } |
| 27 |
| 28 FileRecorderImpl::FileRecorderImpl(uint32_t instanceID, |
| 29 FileFormats fileFormat) |
| 30 : _instanceID(instanceID), |
| 31 _fileFormat(fileFormat), |
| 32 _moduleFile(MediaFile::CreateMediaFile(_instanceID)), |
| 33 codec_info_(), |
| 34 _audioBuffer(), |
| 35 _audioEncoder(instanceID), |
| 36 _audioResampler() |
| 37 { |
| 38 } |
| 39 |
| 40 FileRecorderImpl::~FileRecorderImpl() |
| 41 { |
| 42 MediaFile::DestroyMediaFile(_moduleFile); |
| 43 } |
| 44 |
| 45 FileFormats FileRecorderImpl::RecordingFileFormat() const |
| 46 { |
| 47 return _fileFormat; |
| 48 } |
| 49 |
| 50 int32_t FileRecorderImpl::RegisterModuleFileCallback( |
| 51 FileCallback* callback) |
| 52 { |
| 53 if(_moduleFile == NULL) |
| 54 { |
| 55 return -1; |
| 56 } |
| 57 return _moduleFile->SetModuleFileCallback(callback); |
| 58 } |
| 59 |
| 60 int32_t FileRecorderImpl::StartRecordingAudioFile( |
| 61 const char* fileName, |
| 62 const CodecInst& codecInst, |
| 63 uint32_t notificationTimeMs) |
| 64 { |
| 65 if(_moduleFile == NULL) |
| 66 { |
| 67 return -1; |
| 68 } |
| 69 codec_info_ = codecInst; |
| 70 int32_t retVal = 0; |
| 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 } |
| 91 |
| 92 int32_t FileRecorderImpl::StartRecordingAudioFile( |
| 93 OutStream& destStream, |
| 94 const CodecInst& codecInst, |
| 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 } |
| 119 |
| 120 int32_t FileRecorderImpl::StopRecording() |
| 121 { |
| 122 memset(&codec_info_, 0, sizeof(CodecInst)); |
| 123 return _moduleFile->StopRecording(); |
| 124 } |
| 125 |
| 126 bool FileRecorderImpl::IsRecording() const |
| 127 { |
| 128 return _moduleFile->IsRecording(); |
| 129 } |
| 130 |
| 131 int32_t FileRecorderImpl::RecordAudioToFile( |
| 132 const AudioFrame& incomingAudioFrame) |
| 133 { |
| 134 if (codec_info_.plfreq == 0) |
| 135 { |
| 136 LOG(LS_WARNING) << "RecordAudioToFile() recording audio is not " |
| 137 << "turned on."; |
| 138 return -1; |
| 139 } |
| 140 AudioFrame tempAudioFrame; |
| 141 tempAudioFrame.samples_per_channel_ = 0; |
| 142 if( incomingAudioFrame.num_channels_ == 2 && |
| 143 !_moduleFile->IsStereo()) |
| 144 { |
| 145 // Recording mono but incoming audio is (interleaved) stereo. |
| 146 tempAudioFrame.num_channels_ = 1; |
| 147 tempAudioFrame.sample_rate_hz_ = incomingAudioFrame.sample_rate_hz_; |
| 148 tempAudioFrame.samples_per_channel_ = |
| 149 incomingAudioFrame.samples_per_channel_; |
| 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 } |
| 160 else if( incomingAudioFrame.num_channels_ == 1 && |
| 161 _moduleFile->IsStereo()) |
| 162 { |
| 163 // Recording stereo but incoming audio is mono. |
| 164 tempAudioFrame.num_channels_ = 2; |
| 165 tempAudioFrame.sample_rate_hz_ = incomingAudioFrame.sample_rate_hz_; |
| 166 tempAudioFrame.samples_per_channel_ = |
| 167 incomingAudioFrame.samples_per_channel_; |
| 168 for (size_t i = 0; |
| 169 i < (incomingAudioFrame.samples_per_channel_); i++) |
| 170 { |
| 171 // Duplicate sample to both channels |
| 172 tempAudioFrame.data_[2*i] = |
| 173 incomingAudioFrame.data_[i]; |
| 174 tempAudioFrame.data_[2*i+1] = |
| 175 incomingAudioFrame.data_[i]; |
| 176 } |
| 177 } |
| 178 |
| 179 const AudioFrame* ptrAudioFrame = &incomingAudioFrame; |
| 180 if(tempAudioFrame.samples_per_channel_ != 0) |
| 181 { |
| 182 // If ptrAudioFrame is not empty it contains the audio to be recorded. |
| 183 ptrAudioFrame = &tempAudioFrame; |
| 184 } |
| 185 |
| 186 // Encode the audio data before writing to file. Don't encode if the codec |
| 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 } |
| 228 |
| 229 int32_t FileRecorderImpl::SetUpAudioEncoder() |
| 230 { |
| 231 if (_fileFormat == kFileFormatPreencodedFile || |
| 232 STR_CASE_CMP(codec_info_.plname, "L16") != 0) |
| 233 { |
| 234 if(_audioEncoder.SetEncodeCodec(codec_info_) == -1) |
| 235 { |
| 236 LOG(LS_ERROR) << "SetUpAudioEncoder() codec " |
| 237 << codec_info_.plname << " not supported."; |
| 238 return -1; |
| 239 } |
| 240 } |
| 241 return 0; |
| 242 } |
| 243 |
| 244 int32_t FileRecorderImpl::codec_info(CodecInst& codecInst) const |
| 245 { |
| 246 if(codec_info_.plfreq == 0) |
| 247 { |
| 248 return -1; |
| 249 } |
| 250 codecInst = codec_info_; |
| 251 return 0; |
| 252 } |
| 253 |
| 254 int32_t FileRecorderImpl::WriteEncodedAudioData(const int8_t* audioBuffer, |
| 255 size_t bufferLength) |
| 256 { |
| 257 return _moduleFile->IncomingAudioData(audioBuffer, bufferLength); |
| 258 } |
| 259 } // namespace webrtc |
OLD | NEW |