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 AudioFileWriter::CreateCallback& 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 recording_enabled_(0), |
| 22 task_runner_(std::move(task_runner)), |
| 23 on_destruction_closure_(std::move(on_destruction_closure)), |
| 24 weak_factory_(this) { |
| 25 DCHECK(create_audio_file_writer_callback_); |
| 26 } |
| 27 |
| 28 AudioDebugRecordingHelper::~AudioDebugRecordingHelper() { |
| 29 if (on_destruction_closure_) |
| 30 std::move(on_destruction_closure_).Run(); |
| 31 } |
| 32 |
| 33 void AudioDebugRecordingHelper::EnableDebugRecording( |
| 34 const base::FilePath& file_name) { |
| 35 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 36 DCHECK(!debug_writer_); |
| 37 DCHECK(!file_name.empty()); |
| 38 |
| 39 debug_writer_ = create_audio_file_writer_callback_.Run(params_); |
| 40 debug_writer_->Start( |
| 41 file_name.AddExtension(debug_writer_->GetFileNameExtension())); |
| 42 |
| 43 base::subtle::NoBarrier_Store(&recording_enabled_, 1); |
| 44 } |
| 45 |
| 46 void AudioDebugRecordingHelper::DisableDebugRecording() { |
| 47 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 48 |
| 49 base::subtle::NoBarrier_Store(&recording_enabled_, 0); |
| 50 |
| 51 if (debug_writer_) { |
| 52 debug_writer_->Stop(); |
| 53 debug_writer_.reset(); |
| 54 } |
| 55 } |
| 56 |
| 57 void AudioDebugRecordingHelper::OnData(const AudioBus* source) { |
| 58 // Check if debug recording is enabled to avoid an unecessary copy and thread |
| 59 // jump if not. Recording can be disabled between the atomic Load() here and |
| 60 // DoWrite(), but it's fine with a single unnecessary copy+jump at disable |
| 61 // time. We use an atomic operation for accessing the flag on different |
| 62 // threads. No memory barrier is needed for the same reason; a race is no |
| 63 // problem at enable and disable time. Missing one buffer of data doesn't |
| 64 // matter. |
| 65 base::subtle::Atomic32 recording_enabled = |
| 66 base::subtle::NoBarrier_Load(&recording_enabled_); |
| 67 if (!recording_enabled) |
| 68 return; |
| 69 |
| 70 // TODO(grunell) Don't create a new AudioBus each time. Maybe a pool of |
| 71 // AudioBuses. See also comment in |
| 72 // AudioInputController::AudioCallback::PerformOptionalDebugRecording. |
| 73 std::unique_ptr<AudioBus> audio_bus_copy = |
| 74 AudioBus::Create(source->channels(), source->frames()); |
| 75 source->CopyTo(audio_bus_copy.get()); |
| 76 |
| 77 task_runner_->PostTask( |
| 78 FROM_HERE, |
| 79 base::Bind(&AudioDebugRecordingHelper::DoWrite, |
| 80 weak_factory_.GetWeakPtr(), base::Passed(&audio_bus_copy))); |
| 81 } |
| 82 |
| 83 void AudioDebugRecordingHelper::DoWrite(std::unique_ptr<media::AudioBus> data) { |
| 84 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 85 |
| 86 if (debug_writer_) |
| 87 debug_writer_->Write(std::move(data)); |
| 88 } |
| 89 |
| 90 } // namespace media |
OLD | NEW |