| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/audio_modem/audio_recorder_impl.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <utility> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/bind_helpers.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/run_loop.h" | |
| 15 #include "base/synchronization/waitable_event.h" | |
| 16 #include "components/audio_modem/public/audio_modem_types.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 #include "media/audio/audio_device_description.h" | |
| 19 #include "media/audio/audio_manager.h" | |
| 20 #include "media/base/audio_bus.h" | |
| 21 | |
| 22 namespace audio_modem { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const float kProcessIntervalMs = 500.0f; // milliseconds. | |
| 27 | |
| 28 void AudioBusToString(std::unique_ptr<media::AudioBus> source, | |
| 29 std::string* buffer) { | |
| 30 buffer->resize(source->frames() * source->channels() * sizeof(float)); | |
| 31 float* buffer_view = reinterpret_cast<float*>(string_as_array(buffer)); | |
| 32 | |
| 33 const int channels = source->channels(); | |
| 34 for (int ch = 0; ch < channels; ++ch) { | |
| 35 for (int si = 0, di = ch; si < source->frames(); ++si, di += channels) | |
| 36 buffer_view[di] = source->channel(ch)[si]; | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 // Called every kProcessIntervalMs to process the recorded audio. This | |
| 41 // converts our samples to the required sample rate, interleaves the samples | |
| 42 // and sends them to the whispernet decoder to process. | |
| 43 void ProcessSamples( | |
| 44 std::unique_ptr<media::AudioBus> bus, | |
| 45 const AudioRecorderImpl::RecordedSamplesCallback& callback) { | |
| 46 std::string samples; | |
| 47 AudioBusToString(std::move(bus), &samples); | |
| 48 content::BrowserThread::PostTask( | |
| 49 content::BrowserThread::UI, FROM_HERE, base::Bind(callback, samples)); | |
| 50 } | |
| 51 | |
| 52 void OnLogMessage(const std::string& message) {} | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 56 // Public methods. | |
| 57 | |
| 58 AudioRecorderImpl::AudioRecorderImpl() | |
| 59 : is_recording_(false), | |
| 60 stream_(nullptr), | |
| 61 total_buffer_frames_(0), | |
| 62 buffer_frame_index_(0) { | |
| 63 } | |
| 64 | |
| 65 void AudioRecorderImpl::Initialize( | |
| 66 const RecordedSamplesCallback& decode_callback) { | |
| 67 decode_callback_ = decode_callback; | |
| 68 media::AudioManager::Get()->GetTaskRunner()->PostTask( | |
| 69 FROM_HERE, | |
| 70 base::Bind(&AudioRecorderImpl::InitializeOnAudioThread, | |
| 71 base::Unretained(this))); | |
| 72 } | |
| 73 | |
| 74 AudioRecorderImpl::~AudioRecorderImpl() { | |
| 75 } | |
| 76 | |
| 77 void AudioRecorderImpl::Record() { | |
| 78 media::AudioManager::Get()->GetTaskRunner()->PostTask( | |
| 79 FROM_HERE, | |
| 80 base::Bind(&AudioRecorderImpl::RecordOnAudioThread, | |
| 81 base::Unretained(this))); | |
| 82 } | |
| 83 | |
| 84 void AudioRecorderImpl::Stop() { | |
| 85 media::AudioManager::Get()->GetTaskRunner()->PostTask( | |
| 86 FROM_HERE, | |
| 87 base::Bind(&AudioRecorderImpl::StopOnAudioThread, | |
| 88 base::Unretained(this))); | |
| 89 } | |
| 90 | |
| 91 void AudioRecorderImpl::Finalize() { | |
| 92 media::AudioManager::Get()->GetTaskRunner()->PostTask( | |
| 93 FROM_HERE, | |
| 94 base::Bind(&AudioRecorderImpl::FinalizeOnAudioThread, | |
| 95 base::Unretained(this))); | |
| 96 } | |
| 97 | |
| 98 // Private methods. | |
| 99 | |
| 100 void AudioRecorderImpl::InitializeOnAudioThread() { | |
| 101 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); | |
| 102 | |
| 103 media::AudioParameters params; | |
| 104 if (params_for_testing_) { | |
| 105 params = *params_for_testing_; | |
| 106 } else { | |
| 107 params = media::AudioManager::Get()->GetInputStreamParameters( | |
| 108 media::AudioDeviceDescription::kDefaultDeviceId); | |
| 109 params.set_effects(media::AudioParameters::NO_EFFECTS); | |
| 110 } | |
| 111 | |
| 112 total_buffer_frames_ = kProcessIntervalMs * params.sample_rate() / 1000; | |
| 113 buffer_ = media::AudioBus::Create(params.channels(), total_buffer_frames_); | |
| 114 buffer_frame_index_ = 0; | |
| 115 | |
| 116 stream_ = input_stream_for_testing_ | |
| 117 ? input_stream_for_testing_.get() | |
| 118 : media::AudioManager::Get()->MakeAudioInputStream( | |
| 119 params, media::AudioDeviceDescription::kDefaultDeviceId, | |
| 120 base::Bind(&OnLogMessage)); | |
| 121 | |
| 122 if (!stream_ || !stream_->Open()) { | |
| 123 LOG(ERROR) << "Failed to open an input stream."; | |
| 124 if (stream_) { | |
| 125 stream_->Close(); | |
| 126 stream_ = nullptr; | |
| 127 } | |
| 128 return; | |
| 129 } | |
| 130 stream_->SetVolume(stream_->GetMaxVolume()); | |
| 131 } | |
| 132 | |
| 133 void AudioRecorderImpl::RecordOnAudioThread() { | |
| 134 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); | |
| 135 if (!stream_ || is_recording_) | |
| 136 return; | |
| 137 | |
| 138 VLOG(3) << "Starting recording."; | |
| 139 stream_->Start(this); | |
| 140 is_recording_ = true; | |
| 141 } | |
| 142 | |
| 143 void AudioRecorderImpl::StopOnAudioThread() { | |
| 144 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); | |
| 145 if (!stream_ || !is_recording_) | |
| 146 return; | |
| 147 | |
| 148 VLOG(3) << "Stopping recording."; | |
| 149 stream_->Stop(); | |
| 150 is_recording_ = false; | |
| 151 } | |
| 152 | |
| 153 void AudioRecorderImpl::StopAndCloseOnAudioThread() { | |
| 154 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); | |
| 155 if (!stream_) | |
| 156 return; | |
| 157 | |
| 158 StopOnAudioThread(); | |
| 159 stream_->Close(); | |
| 160 stream_ = nullptr; | |
| 161 } | |
| 162 | |
| 163 void AudioRecorderImpl::FinalizeOnAudioThread() { | |
| 164 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); | |
| 165 StopAndCloseOnAudioThread(); | |
| 166 delete this; | |
| 167 } | |
| 168 | |
| 169 void AudioRecorderImpl::OnData(media::AudioInputStream* stream, | |
| 170 const media::AudioBus* source, | |
| 171 uint32_t /* hardware_delay_bytes */, | |
| 172 double /* volume */) { | |
| 173 // source->frames() == source_params.frames_per_buffer(), so we only have | |
| 174 // one chunk of data in the source; correspondingly set the destination | |
| 175 // size to one chunk. | |
| 176 | |
| 177 int remaining_buffer_frames = buffer_->frames() - buffer_frame_index_; | |
| 178 int frames_to_copy = std::min(remaining_buffer_frames, source->frames()); | |
| 179 source->CopyPartialFramesTo(0, frames_to_copy, buffer_frame_index_, | |
| 180 buffer_.get()); | |
| 181 buffer_frame_index_ += frames_to_copy; | |
| 182 | |
| 183 // Buffer full, send it for processing. | |
| 184 if (buffer_->frames() == buffer_frame_index_) { | |
| 185 ProcessSamples(std::move(buffer_), decode_callback_); | |
| 186 buffer_ = media::AudioBus::Create(source->channels(), total_buffer_frames_); | |
| 187 buffer_frame_index_ = 0; | |
| 188 | |
| 189 // Copy any remaining frames in the source to our buffer. | |
| 190 int remaining_source_frames = source->frames() - frames_to_copy; | |
| 191 source->CopyPartialFramesTo(frames_to_copy, remaining_source_frames, | |
| 192 buffer_frame_index_, buffer_.get()); | |
| 193 buffer_frame_index_ += remaining_source_frames; | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 void AudioRecorderImpl::OnError(media::AudioInputStream* /* stream */) { | |
| 198 LOG(ERROR) << "Error during sound recording."; | |
| 199 media::AudioManager::Get()->GetTaskRunner()->PostTask( | |
| 200 FROM_HERE, | |
| 201 base::Bind(&AudioRecorderImpl::StopAndCloseOnAudioThread, | |
| 202 base::Unretained(this))); | |
| 203 } | |
| 204 | |
| 205 } // namespace audio_modem | |
| OLD | NEW |