Chromium Code Reviews| Index: media/audio/audio_debug_recording_manager.cc |
| diff --git a/media/audio/audio_debug_recording_manager.cc b/media/audio/audio_debug_recording_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8854cb1469e433d3d56a6cb931b3375a71e526ac |
| --- /dev/null |
| +++ b/media/audio/audio_debug_recording_manager.cc |
| @@ -0,0 +1,118 @@ |
| +// 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_manager.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_path.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "media/audio/audio_manager.h" |
| + |
| +namespace media { |
| + |
| +namespace { |
| + |
| +// Running id recording sources. |
| +int g_next_stream_id = 1; |
| + |
| +#if defined(OS_WIN) |
| +#define IntToStringType base::IntToString16 |
| +#else |
| +#define IntToStringType base::IntToString |
| +#endif |
| + |
| +// Helper function that returns |base_file_name| with |file_name_extension| and |
| +// |id| added to it as as extensions. |
| +base::FilePath GetOutputDebugRecordingFileNameWithExtensions( |
| + const base::FilePath& base_file_name, |
| + const std::string& file_name_extension, |
| + int id) { |
| + return base_file_name.AddExtension(file_name_extension) |
| + .AddExtension(IntToStringType(id)); |
| +} |
| + |
| +} // namespace |
| + |
| +AudioDebugRecordingManager::AudioDebugRecordingManager( |
| + CreateAudioFileWriterCallback create_audio_file_writer_callback, |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| + : create_audio_file_writer_callback_( |
| + std::move(create_audio_file_writer_callback)), |
| + task_runner_(std::move(task_runner)), |
| + weak_factory_(this) { |
| + DCHECK(create_audio_file_writer_callback_); |
| +} |
| + |
| +AudioDebugRecordingManager::~AudioDebugRecordingManager() {} |
| + |
| +void AudioDebugRecordingManager::EnableDebugRecording( |
| + const base::FilePath& base_file_name) { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + DCHECK(!base_file_name.empty()); |
| + |
| + for (const auto& it : debug_recording_helpers_) { |
| + it.second.first->EnableDebugRecording( |
| + GetOutputDebugRecordingFileNameWithExtensions( |
| + base_file_name, it.second.second, it.first)); |
| + } |
| + debug_recording_base_file_name_ = base_file_name; |
| +} |
| + |
| +void AudioDebugRecordingManager::DisableDebugRecording() { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + for (const auto& it : debug_recording_helpers_) |
| + it.second.first->DisableDebugRecording(); |
| + debug_recording_base_file_name_.clear(); |
| +} |
| + |
| +std::unique_ptr<AudioDebugRecorder> |
| +AudioDebugRecordingManager::RegisterDebugRecordingSource( |
| + const std::string& file_name_extension, |
| + const AudioParameters& params) { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + |
| + const int id = g_next_stream_id++; |
| + |
| + // Normally, the manager will outlive the one who registers and owns the |
| + // returned recorder. But to not require this we use a weak pointer. |
| + std::unique_ptr<AudioDebugRecordingHelper> recording_helper = |
| + CreateAudioDebugRecordingHelper( |
| + params, create_audio_file_writer_callback_, task_runner_, |
| + base::BindOnce( |
| + &AudioDebugRecordingManager::UnregisterDebugRecordingSource, |
|
o1ka
2017/02/09 13:04:03
Is it necessary to pass a pointer to AudioDebugRec
Henrik Grunell
2017/02/10 09:00:56
The helper doesn't know about the manager.
o1ka
2017/02/10 15:21:53
CreateAudioDebugRecordingHelper does. Why to pass
Henrik Grunell
2017/02/13 15:16:48
Oh sorry, I misread, I thought you meant AudioDebu
|
| + weak_factory_.GetWeakPtr(), id)); |
| + |
| + if (!debug_recording_base_file_name_.empty()) { |
|
o1ka
2017/02/09 13:04:03
This needs a comment (or better a helper function
Henrik Grunell
2017/02/10 09:00:56
Yep, a helper is better. Done.
|
| + recording_helper->EnableDebugRecording( |
| + GetOutputDebugRecordingFileNameWithExtensions( |
| + debug_recording_base_file_name_, file_name_extension, id)); |
| + } |
| + |
| + debug_recording_helpers_[id] = |
| + std::make_pair(recording_helper.get(), file_name_extension); |
| + |
| + return recording_helper; |
| +} |
| + |
| +void AudioDebugRecordingManager::UnregisterDebugRecordingSource(int id) { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + auto it = debug_recording_helpers_.find(id); |
| + DCHECK(it != debug_recording_helpers_.end()); |
| + debug_recording_helpers_.erase(id); |
| +} |
| + |
| +std::unique_ptr<AudioDebugRecordingHelper> |
| +AudioDebugRecordingManager::CreateAudioDebugRecordingHelper( |
| + const AudioParameters& params, |
| + const CreateAudioFileWriterCallback& create_audio_file_writer_callback, |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| + base::OnceClosure on_destruction_closure) { |
| + return base::MakeUnique<AudioDebugRecordingHelper>( |
| + params, create_audio_file_writer_callback, task_runner, |
| + std::move(on_destruction_closure)); |
| +} |
| + |
| +} // namespace media |