| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/copresence/mediums/audio/audio_recorder_impl.h" | 5 #include "components/copresence/mediums/audio/audio_recorder_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 content::BrowserThread::PostTask( | 46 content::BrowserThread::PostTask( |
| 47 content::BrowserThread::UI, FROM_HERE, base::Bind(callback, samples)); | 47 content::BrowserThread::UI, FROM_HERE, base::Bind(callback, samples)); |
| 48 } | 48 } |
| 49 | 49 |
| 50 } // namespace | 50 } // namespace |
| 51 | 51 |
| 52 // Public methods. | 52 // Public methods. |
| 53 | 53 |
| 54 AudioRecorderImpl::AudioRecorderImpl() | 54 AudioRecorderImpl::AudioRecorderImpl() |
| 55 : is_recording_(false), | 55 : is_recording_(false), |
| 56 stream_(NULL), | 56 stream_(nullptr), |
| 57 temp_conversion_buffer_(NULL), | 57 temp_conversion_buffer_(nullptr), |
| 58 total_buffer_frames_(0), | 58 total_buffer_frames_(0), |
| 59 buffer_frame_index_(0) { | 59 buffer_frame_index_(0) { |
| 60 } | 60 } |
| 61 | 61 |
| 62 void AudioRecorderImpl::Initialize( | 62 void AudioRecorderImpl::Initialize( |
| 63 const RecordedSamplesCallback& decode_callback) { | 63 const RecordedSamplesCallback& decode_callback) { |
| 64 decode_callback_ = decode_callback; | 64 decode_callback_ = decode_callback; |
| 65 media::AudioManager::Get()->GetTaskRunner()->PostTask( | 65 media::AudioManager::Get()->GetTaskRunner()->PostTask( |
| 66 FROM_HERE, | 66 FROM_HERE, |
| 67 base::Bind(&AudioRecorderImpl::InitializeOnAudioThread, | 67 base::Bind(&AudioRecorderImpl::InitializeOnAudioThread, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 125 |
| 126 stream_ = input_stream_for_testing_ | 126 stream_ = input_stream_for_testing_ |
| 127 ? input_stream_for_testing_.get() | 127 ? input_stream_for_testing_.get() |
| 128 : media::AudioManager::Get()->MakeAudioInputStream( | 128 : media::AudioManager::Get()->MakeAudioInputStream( |
| 129 params, media::AudioManagerBase::kDefaultDeviceId); | 129 params, media::AudioManagerBase::kDefaultDeviceId); |
| 130 | 130 |
| 131 if (!stream_ || !stream_->Open()) { | 131 if (!stream_ || !stream_->Open()) { |
| 132 LOG(ERROR) << "Failed to open an input stream."; | 132 LOG(ERROR) << "Failed to open an input stream."; |
| 133 if (stream_) { | 133 if (stream_) { |
| 134 stream_->Close(); | 134 stream_->Close(); |
| 135 stream_ = NULL; | 135 stream_ = nullptr; |
| 136 } | 136 } |
| 137 return; | 137 return; |
| 138 } | 138 } |
| 139 stream_->SetVolume(stream_->GetMaxVolume()); | 139 stream_->SetVolume(stream_->GetMaxVolume()); |
| 140 } | 140 } |
| 141 | 141 |
| 142 void AudioRecorderImpl::RecordOnAudioThread() { | 142 void AudioRecorderImpl::RecordOnAudioThread() { |
| 143 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); | 143 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
| 144 if (!stream_ || is_recording_) | 144 if (!stream_ || is_recording_) |
| 145 return; | 145 return; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 158 is_recording_ = false; | 158 is_recording_ = false; |
| 159 } | 159 } |
| 160 | 160 |
| 161 void AudioRecorderImpl::StopAndCloseOnAudioThread() { | 161 void AudioRecorderImpl::StopAndCloseOnAudioThread() { |
| 162 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); | 162 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
| 163 if (!stream_) | 163 if (!stream_) |
| 164 return; | 164 return; |
| 165 | 165 |
| 166 StopOnAudioThread(); | 166 StopOnAudioThread(); |
| 167 stream_->Close(); | 167 stream_->Close(); |
| 168 stream_ = NULL; | 168 stream_ = nullptr; |
| 169 } | 169 } |
| 170 | 170 |
| 171 void AudioRecorderImpl::FinalizeOnAudioThread() { | 171 void AudioRecorderImpl::FinalizeOnAudioThread() { |
| 172 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); | 172 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
| 173 StopAndCloseOnAudioThread(); | 173 StopAndCloseOnAudioThread(); |
| 174 delete this; | 174 delete this; |
| 175 } | 175 } |
| 176 | 176 |
| 177 void AudioRecorderImpl::OnData(media::AudioInputStream* stream, | 177 void AudioRecorderImpl::OnData(media::AudioInputStream* stream, |
| 178 const media::AudioBus* source, | 178 const media::AudioBus* source, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 FROM_HERE, | 227 FROM_HERE, |
| 228 base::Bind(&AudioRecorderImpl::StopAndCloseOnAudioThread, | 228 base::Bind(&AudioRecorderImpl::StopAndCloseOnAudioThread, |
| 229 base::Unretained(this))); | 229 base::Unretained(this))); |
| 230 } | 230 } |
| 231 | 231 |
| 232 double AudioRecorderImpl::ProvideInput(media::AudioBus* dest, | 232 double AudioRecorderImpl::ProvideInput(media::AudioBus* dest, |
| 233 base::TimeDelta /* buffer_delay */) { | 233 base::TimeDelta /* buffer_delay */) { |
| 234 DCHECK(temp_conversion_buffer_); | 234 DCHECK(temp_conversion_buffer_); |
| 235 DCHECK_LE(temp_conversion_buffer_->frames(), dest->frames()); | 235 DCHECK_LE(temp_conversion_buffer_->frames(), dest->frames()); |
| 236 temp_conversion_buffer_->CopyTo(dest); | 236 temp_conversion_buffer_->CopyTo(dest); |
| 237 temp_conversion_buffer_ = NULL; | 237 temp_conversion_buffer_ = nullptr; |
| 238 return 1.0; | 238 return 1.0; |
| 239 } | 239 } |
| 240 | 240 |
| 241 void AudioRecorderImpl::FlushAudioLoopForTesting() { | 241 void AudioRecorderImpl::FlushAudioLoopForTesting() { |
| 242 if (media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()) | 242 if (media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()) |
| 243 return; | 243 return; |
| 244 | 244 |
| 245 // Queue task on the audio thread, when it is executed, that means we've | 245 // Queue task on the audio thread, when it is executed, that means we've |
| 246 // successfully executed all the tasks before us. | 246 // successfully executed all the tasks before us. |
| 247 base::RunLoop rl; | 247 base::RunLoop rl; |
| 248 media::AudioManager::Get()->GetTaskRunner()->PostTaskAndReply( | 248 media::AudioManager::Get()->GetTaskRunner()->PostTaskAndReply( |
| 249 FROM_HERE, | 249 FROM_HERE, |
| 250 base::Bind( | 250 base::Bind( |
| 251 base::IgnoreResult(&AudioRecorderImpl::FlushAudioLoopForTesting), | 251 base::IgnoreResult(&AudioRecorderImpl::FlushAudioLoopForTesting), |
| 252 base::Unretained(this)), | 252 base::Unretained(this)), |
| 253 rl.QuitClosure()); | 253 rl.QuitClosure()); |
| 254 rl.Run(); | 254 rl.Run(); |
| 255 } | 255 } |
| 256 | 256 |
| 257 } // namespace copresence | 257 } // namespace copresence |
| OLD | NEW |