Index: media/audio/audio_debug_recording_helper.cc |
diff --git a/media/audio/audio_debug_recording_helper.cc b/media/audio/audio_debug_recording_helper.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..14f2382f1a26cd0c6ea8c881f9f9a1a64bbe6fc2 |
--- /dev/null |
+++ b/media/audio/audio_debug_recording_helper.cc |
@@ -0,0 +1,80 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/audio/audio_debug_recording_helper.h" |
+ |
+#include "base/bind.h" |
+#include "base/files/file_path.h" |
+#include "base/single_thread_task_runner.h" |
+#include "media/audio/audio_manager.h" |
+ |
+namespace media { |
+ |
+AudioDebugRecordingHelper::AudioDebugRecordingHelper( |
+ const AudioParameters& params, |
+ const CreateAudioFileWriterCallback& create_audio_file_writer_callback, |
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
+ base::OnceClosure on_destruction_closure) |
+ : params_(params), |
+ create_audio_file_writer_callback_(create_audio_file_writer_callback), |
+ task_runner_(std::move(task_runner)), |
+ on_destruction_closure_(std::move(on_destruction_closure)), |
+ weak_factory_(this) { |
+ DCHECK(create_audio_file_writer_callback_); |
+} |
+ |
+AudioDebugRecordingHelper::~AudioDebugRecordingHelper() { |
+ if (on_destruction_closure_) |
+ std::move(on_destruction_closure_).Run(); |
+} |
+ |
+void AudioDebugRecordingHelper::EnableDebugRecording( |
+ const base::FilePath& file_name) { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ DCHECK(!debug_writer_); |
+ |
+ debug_writer_ = create_audio_file_writer_callback_.Run(params_); |
+ |
+ // The debug writer writes in wave format. |
o1ka
2017/01/31 11:00:11
How do we know it here? It's just some audio file
Henrik Grunell
2017/02/08 11:29:37
Agree. Added AudioFileWriter::GetExtension(), and
|
+ debug_writer_->Start(file_name.AddExtension("wav")); |
+} |
+ |
+void AudioDebugRecordingHelper::DisableDebugRecording() { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ |
+ if (debug_writer_) { |
+ debug_writer_->Stop(); |
+ debug_writer_.reset(); |
+ } |
+} |
+ |
+bool AudioDebugRecordingHelper::WillWrite() { |
+ // AudioFileWriter::WillWrite() can be called on any thread. |
o1ka
2017/01/31 11:00:11
Please explain why this method is ok to call from
Henrik Grunell
2017/02/08 11:29:37
Removed this function.
|
+ return debug_writer_ && debug_writer_->WillWrite(); |
+} |
+ |
+void AudioDebugRecordingHelper::OnData(const AudioBus* source) { |
+ if (!WillWrite()) |
o1ka
2017/01/31 11:00:11
Why is it safe to call it here?
Henrik Grunell
2017/02/08 11:29:37
WillWrite() has been removed, and I just check |de
|
+ return; |
+ |
+ // TODO(grunell) Don't create a new AudioBus each time. Maybe a pool of |
+ // AudioBuses. See also AudioInputController. |
o1ka
2017/01/31 11:00:11
What exactly in AudioInputController?
Henrik Grunell
2017/02/08 11:29:37
Updated comment.
|
+ std::unique_ptr<AudioBus> audio_bus_copy = |
+ AudioBus::Create(source->channels(), source->frames()); |
+ source->CopyTo(audio_bus_copy.get()); |
+ |
+ task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&AudioDebugRecordingHelper::DoWrite, |
+ weak_factory_.GetWeakPtr(), base::Passed(&audio_bus_copy))); |
+} |
+ |
+void AudioDebugRecordingHelper::DoWrite(std::unique_ptr<media::AudioBus> data) { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ |
+ if (debug_writer_) |
+ debug_writer_->Write(std::move(data)); |
+} |
+ |
+} // namespace media |