Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "media/audio/audio_debug_recording_helper.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/single_thread_task_runner.h" | |
| 10 #include "media/audio/audio_manager.h" | |
| 11 | |
| 12 namespace media { | |
| 13 | |
| 14 AudioDebugRecordingHelper::AudioDebugRecordingHelper( | |
| 15 const AudioParameters& params, | |
| 16 const CreateAudioFileWriterCallback& create_audio_file_writer_callback, | |
| 17 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 18 base::OnceClosure on_destruction_closure) | |
| 19 : params_(params), | |
| 20 create_audio_file_writer_callback_(create_audio_file_writer_callback), | |
| 21 task_runner_(std::move(task_runner)), | |
| 22 on_destruction_closure_(std::move(on_destruction_closure)), | |
| 23 weak_factory_(this) { | |
| 24 DCHECK(create_audio_file_writer_callback_); | |
| 25 } | |
| 26 | |
| 27 AudioDebugRecordingHelper::~AudioDebugRecordingHelper() { | |
| 28 if (on_destruction_closure_) | |
| 29 std::move(on_destruction_closure_).Run(); | |
| 30 } | |
| 31 | |
| 32 void AudioDebugRecordingHelper::EnableDebugRecording( | |
| 33 const base::FilePath& file_name) { | |
| 34 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 35 DCHECK(!debug_writer_); | |
| 36 DCHECK(!file_name.empty()); | |
| 37 | |
| 38 debug_writer_ = create_audio_file_writer_callback_.Run(params_); | |
| 39 debug_writer_->Start(file_name.AddExtension(debug_writer_->GetExtension())); | |
| 40 } | |
| 41 | |
| 42 void AudioDebugRecordingHelper::DisableDebugRecording() { | |
| 43 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 44 | |
| 45 if (debug_writer_) { | |
| 46 debug_writer_->Stop(); | |
| 47 debug_writer_.reset(); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 void AudioDebugRecordingHelper::OnData(const AudioBus* source) { | |
| 52 // This function is normally called on the sound card thread, so there is a | |
| 53 // race accessing |debug_writer_|. For this reason, we only check the | |
| 54 // pointer, and not |debug_writer_|->WillWrite(). The race checking the | |
| 55 // pointer is OK because we'll check again in DoWrite(), and one possible | |
| 56 // unnecessary copy when disabling is fine. | |
| 57 // Furthermore, |debug_writer_| is always started right after creation, and | |
| 58 // stopped right before being reset, and since WillWrite() simply checks | |
| 59 // whether started or not, it's not needed either. | |
|
Max Morin
2017/02/09 09:47:26
Could you introduce an atomic flag instead?
o1ka
2017/02/09 13:04:02
This is good enough, isn't it? What would we gain
Henrik Grunell
2017/02/10 09:00:56
After discussing offline, I decided to use an atom
| |
| 60 if (!debug_writer_) | |
| 61 return; | |
| 62 | |
| 63 // TODO(grunell) Don't create a new AudioBus each time. Maybe a pool of | |
| 64 // AudioBuses. See also comment in | |
| 65 // AudioInputController::AudioCallback::PerformOptionalDebugRecording. | |
| 66 std::unique_ptr<AudioBus> audio_bus_copy = | |
| 67 AudioBus::Create(source->channels(), source->frames()); | |
| 68 source->CopyTo(audio_bus_copy.get()); | |
| 69 | |
| 70 task_runner_->PostTask( | |
| 71 FROM_HERE, | |
| 72 base::Bind(&AudioDebugRecordingHelper::DoWrite, | |
| 73 weak_factory_.GetWeakPtr(), base::Passed(&audio_bus_copy))); | |
| 74 } | |
| 75 | |
| 76 void AudioDebugRecordingHelper::DoWrite(std::unique_ptr<media::AudioBus> data) { | |
| 77 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 78 | |
| 79 if (debug_writer_) | |
| 80 debug_writer_->Write(std::move(data)); | |
| 81 } | |
| 82 | |
| 83 } // namespace media | |
| OLD | NEW |