| 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 "voice_engine/file_recorder.h" | |
| 12 | |
| 13 #include <list> | |
| 14 | |
| 15 #include "audio/utility/audio_frame_operations.h" | |
| 16 #include "common_audio/resampler/include/resampler.h" | |
| 17 #include "common_types.h" // NOLINT(build/include) | |
| 18 #include "modules/include/module_common_types.h" | |
| 19 #include "modules/media_file/media_file.h" | |
| 20 #include "modules/media_file/media_file_defines.h" | |
| 21 #include "rtc_base/logging.h" | |
| 22 #include "rtc_base/platform_thread.h" | |
| 23 #include "system_wrappers/include/event_wrapper.h" | |
| 24 #include "typedefs.h" // NOLINT(build/include) | |
| 25 #include "voice_engine/coder.h" | |
| 26 | |
| 27 namespace webrtc { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 // The largest decoded frame size in samples (60ms with 32kHz sample rate). | |
| 32 enum { MAX_AUDIO_BUFFER_IN_SAMPLES = 60 * 32 }; | |
| 33 enum { MAX_AUDIO_BUFFER_IN_BYTES = MAX_AUDIO_BUFFER_IN_SAMPLES * 2 }; | |
| 34 enum { kMaxAudioBufferQueueLength = 100 }; | |
| 35 | |
| 36 class FileRecorderImpl : public FileRecorder { | |
| 37 public: | |
| 38 FileRecorderImpl(uint32_t instanceID, FileFormats fileFormat); | |
| 39 ~FileRecorderImpl() override; | |
| 40 | |
| 41 // FileRecorder functions. | |
| 42 int32_t RegisterModuleFileCallback(FileCallback* callback) override; | |
| 43 FileFormats RecordingFileFormat() const override; | |
| 44 int32_t StartRecordingAudioFile(const char* fileName, | |
| 45 const CodecInst& codecInst, | |
| 46 uint32_t notificationTimeMs) override; | |
| 47 int32_t StartRecordingAudioFile(OutStream* destStream, | |
| 48 const CodecInst& codecInst, | |
| 49 uint32_t notificationTimeMs) override; | |
| 50 int32_t StopRecording() override; | |
| 51 bool IsRecording() const override; | |
| 52 int32_t codec_info(CodecInst* codecInst) const override; | |
| 53 int32_t RecordAudioToFile(const AudioFrame& frame) override; | |
| 54 | |
| 55 private: | |
| 56 int32_t WriteEncodedAudioData(const int8_t* audioBuffer, size_t bufferLength); | |
| 57 | |
| 58 int32_t SetUpAudioEncoder(); | |
| 59 | |
| 60 uint32_t _instanceID; | |
| 61 FileFormats _fileFormat; | |
| 62 MediaFile* _moduleFile; | |
| 63 | |
| 64 CodecInst codec_info_; | |
| 65 int8_t _audioBuffer[MAX_AUDIO_BUFFER_IN_BYTES]; | |
| 66 AudioCoder _audioEncoder; | |
| 67 Resampler _audioResampler; | |
| 68 }; | |
| 69 | |
| 70 FileRecorderImpl::FileRecorderImpl(uint32_t instanceID, FileFormats fileFormat) | |
| 71 : _instanceID(instanceID), | |
| 72 _fileFormat(fileFormat), | |
| 73 _moduleFile(MediaFile::CreateMediaFile(_instanceID)), | |
| 74 codec_info_(), | |
| 75 _audioBuffer(), | |
| 76 _audioEncoder(instanceID), | |
| 77 _audioResampler() {} | |
| 78 | |
| 79 FileRecorderImpl::~FileRecorderImpl() { | |
| 80 MediaFile::DestroyMediaFile(_moduleFile); | |
| 81 } | |
| 82 | |
| 83 FileFormats FileRecorderImpl::RecordingFileFormat() const { | |
| 84 return _fileFormat; | |
| 85 } | |
| 86 | |
| 87 int32_t FileRecorderImpl::RegisterModuleFileCallback(FileCallback* callback) { | |
| 88 if (_moduleFile == NULL) { | |
| 89 return -1; | |
| 90 } | |
| 91 return _moduleFile->SetModuleFileCallback(callback); | |
| 92 } | |
| 93 | |
| 94 int32_t FileRecorderImpl::StartRecordingAudioFile(const char* fileName, | |
| 95 const CodecInst& codecInst, | |
| 96 uint32_t notificationTimeMs) { | |
| 97 if (_moduleFile == NULL) { | |
| 98 return -1; | |
| 99 } | |
| 100 codec_info_ = codecInst; | |
| 101 int32_t retVal = 0; | |
| 102 retVal = _moduleFile->StartRecordingAudioFile(fileName, _fileFormat, | |
| 103 codecInst, notificationTimeMs); | |
| 104 | |
| 105 if (retVal == 0) { | |
| 106 retVal = SetUpAudioEncoder(); | |
| 107 } | |
| 108 if (retVal != 0) { | |
| 109 LOG(LS_WARNING) << "Failed to initialize file " << fileName | |
| 110 << " for recording."; | |
| 111 | |
| 112 if (IsRecording()) { | |
| 113 StopRecording(); | |
| 114 } | |
| 115 } | |
| 116 return retVal; | |
| 117 } | |
| 118 | |
| 119 int32_t FileRecorderImpl::StartRecordingAudioFile(OutStream* destStream, | |
| 120 const CodecInst& codecInst, | |
| 121 uint32_t notificationTimeMs) { | |
| 122 codec_info_ = codecInst; | |
| 123 int32_t retVal = _moduleFile->StartRecordingAudioStream( | |
| 124 *destStream, _fileFormat, codecInst, notificationTimeMs); | |
| 125 | |
| 126 if (retVal == 0) { | |
| 127 retVal = SetUpAudioEncoder(); | |
| 128 } | |
| 129 if (retVal != 0) { | |
| 130 LOG(LS_WARNING) << "Failed to initialize outStream for recording."; | |
| 131 | |
| 132 if (IsRecording()) { | |
| 133 StopRecording(); | |
| 134 } | |
| 135 } | |
| 136 return retVal; | |
| 137 } | |
| 138 | |
| 139 int32_t FileRecorderImpl::StopRecording() { | |
| 140 memset(&codec_info_, 0, sizeof(CodecInst)); | |
| 141 return _moduleFile->StopRecording(); | |
| 142 } | |
| 143 | |
| 144 bool FileRecorderImpl::IsRecording() const { | |
| 145 return _moduleFile->IsRecording(); | |
| 146 } | |
| 147 | |
| 148 int32_t FileRecorderImpl::RecordAudioToFile( | |
| 149 const AudioFrame& incomingAudioFrame) { | |
| 150 if (codec_info_.plfreq == 0) { | |
| 151 LOG(LS_WARNING) << "RecordAudioToFile() recording audio is not " | |
| 152 << "turned on."; | |
| 153 return -1; | |
| 154 } | |
| 155 AudioFrame tempAudioFrame; | |
| 156 tempAudioFrame.samples_per_channel_ = 0; | |
| 157 if (incomingAudioFrame.num_channels_ == 2 && !_moduleFile->IsStereo()) { | |
| 158 // Recording mono but incoming audio is (interleaved) stereo. | |
| 159 tempAudioFrame.num_channels_ = 1; | |
| 160 tempAudioFrame.sample_rate_hz_ = incomingAudioFrame.sample_rate_hz_; | |
| 161 tempAudioFrame.samples_per_channel_ = | |
| 162 incomingAudioFrame.samples_per_channel_; | |
| 163 if (!incomingAudioFrame.muted()) { | |
| 164 AudioFrameOperations::StereoToMono( | |
| 165 incomingAudioFrame.data(), incomingAudioFrame.samples_per_channel_, | |
| 166 tempAudioFrame.mutable_data()); | |
| 167 } | |
| 168 } else if (incomingAudioFrame.num_channels_ == 1 && _moduleFile->IsStereo()) { | |
| 169 // Recording stereo but incoming audio is mono. | |
| 170 tempAudioFrame.num_channels_ = 2; | |
| 171 tempAudioFrame.sample_rate_hz_ = incomingAudioFrame.sample_rate_hz_; | |
| 172 tempAudioFrame.samples_per_channel_ = | |
| 173 incomingAudioFrame.samples_per_channel_; | |
| 174 if (!incomingAudioFrame.muted()) { | |
| 175 AudioFrameOperations::MonoToStereo( | |
| 176 incomingAudioFrame.data(), incomingAudioFrame.samples_per_channel_, | |
| 177 tempAudioFrame.mutable_data()); | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 const AudioFrame* ptrAudioFrame = &incomingAudioFrame; | |
| 182 if (tempAudioFrame.samples_per_channel_ != 0) { | |
| 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 if (_audioEncoder.Encode(*ptrAudioFrame, _audioBuffer, | |
| 196 &encodedLenInBytes) == -1) { | |
| 197 LOG(LS_WARNING) << "RecordAudioToFile() codec " << codec_info_.plname | |
| 198 << " not supported or failed to encode stream."; | |
| 199 return -1; | |
| 200 } | |
| 201 } else { | |
| 202 size_t outLen = 0; | |
| 203 _audioResampler.ResetIfNeeded(ptrAudioFrame->sample_rate_hz_, | |
| 204 codec_info_.plfreq, | |
| 205 ptrAudioFrame->num_channels_); | |
| 206 // TODO(yujo): skip resample if frame is muted. | |
| 207 _audioResampler.Push( | |
| 208 ptrAudioFrame->data(), | |
| 209 ptrAudioFrame->samples_per_channel_ * ptrAudioFrame->num_channels_, | |
| 210 reinterpret_cast<int16_t*>(_audioBuffer), MAX_AUDIO_BUFFER_IN_BYTES, | |
| 211 outLen); | |
| 212 encodedLenInBytes = outLen * sizeof(int16_t); | |
| 213 } | |
| 214 | |
| 215 // Codec may not be operating at a frame rate of 10 ms. Whenever enough | |
| 216 // 10 ms chunks of data has been pushed to the encoder an encoded frame | |
| 217 // will be available. Wait until then. | |
| 218 if (encodedLenInBytes) { | |
| 219 if (WriteEncodedAudioData(_audioBuffer, encodedLenInBytes) == -1) { | |
| 220 return -1; | |
| 221 } | |
| 222 } | |
| 223 return 0; | |
| 224 } | |
| 225 | |
| 226 int32_t FileRecorderImpl::SetUpAudioEncoder() { | |
| 227 if (_fileFormat == kFileFormatPreencodedFile || | |
| 228 STR_CASE_CMP(codec_info_.plname, "L16") != 0) { | |
| 229 if (_audioEncoder.SetEncodeCodec(codec_info_) == -1) { | |
| 230 LOG(LS_ERROR) << "SetUpAudioEncoder() codec " << codec_info_.plname | |
| 231 << " not supported."; | |
| 232 return -1; | |
| 233 } | |
| 234 } | |
| 235 return 0; | |
| 236 } | |
| 237 | |
| 238 int32_t FileRecorderImpl::codec_info(CodecInst* codecInst) const { | |
| 239 if (codec_info_.plfreq == 0) { | |
| 240 return -1; | |
| 241 } | |
| 242 *codecInst = codec_info_; | |
| 243 return 0; | |
| 244 } | |
| 245 | |
| 246 int32_t FileRecorderImpl::WriteEncodedAudioData(const int8_t* audioBuffer, | |
| 247 size_t bufferLength) { | |
| 248 return _moduleFile->IncomingAudioData(audioBuffer, bufferLength); | |
| 249 } | |
| 250 | |
| 251 } // namespace | |
| 252 | |
| 253 std::unique_ptr<FileRecorder> FileRecorder::CreateFileRecorder( | |
| 254 uint32_t instanceID, | |
| 255 FileFormats fileFormat) { | |
| 256 return std::unique_ptr<FileRecorder>( | |
| 257 new FileRecorderImpl(instanceID, fileFormat)); | |
| 258 } | |
| 259 | |
| 260 } // namespace webrtc | |
| OLD | NEW |