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_manager.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "media/audio/audio_manager.h" |
| 13 |
| 14 namespace media { |
| 15 |
| 16 namespace { |
| 17 |
| 18 // Running id recording sources. |
| 19 int g_next_stream_id = 1; |
| 20 |
| 21 #if defined(OS_WIN) |
| 22 #define IntToStringType base::IntToString16 |
| 23 #else |
| 24 #define IntToStringType base::IntToString |
| 25 #endif |
| 26 |
| 27 // Helper function that returns |base_file_name| with |file_name_extension| and |
| 28 // |id| added to it as as extensions. |
| 29 base::FilePath GetOutputDebugRecordingFileNameWithExtensions( |
| 30 const base::FilePath& base_file_name, |
| 31 const base::FilePath::StringType& file_name_extension, |
| 32 int id) { |
| 33 return base_file_name.AddExtension(file_name_extension) |
| 34 .AddExtension(IntToStringType(id)); |
| 35 } |
| 36 |
| 37 } // namespace |
| 38 |
| 39 AudioDebugRecordingManager::AudioDebugRecordingManager( |
| 40 AudioFileWriter::CreateCallback create_audio_file_writer_callback, |
| 41 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 42 : create_audio_file_writer_callback_( |
| 43 std::move(create_audio_file_writer_callback)), |
| 44 task_runner_(std::move(task_runner)), |
| 45 weak_factory_(this) { |
| 46 DCHECK(create_audio_file_writer_callback_); |
| 47 } |
| 48 |
| 49 AudioDebugRecordingManager::~AudioDebugRecordingManager() {} |
| 50 |
| 51 void AudioDebugRecordingManager::EnableDebugRecording( |
| 52 const base::FilePath& base_file_name) { |
| 53 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 54 DCHECK(!base_file_name.empty()); |
| 55 |
| 56 for (const auto& it : debug_recording_helpers_) { |
| 57 it.second.first->EnableDebugRecording( |
| 58 GetOutputDebugRecordingFileNameWithExtensions( |
| 59 base_file_name, it.second.second, it.first)); |
| 60 } |
| 61 debug_recording_base_file_name_ = base_file_name; |
| 62 } |
| 63 |
| 64 void AudioDebugRecordingManager::DisableDebugRecording() { |
| 65 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 66 for (const auto& it : debug_recording_helpers_) |
| 67 it.second.first->DisableDebugRecording(); |
| 68 debug_recording_base_file_name_.clear(); |
| 69 } |
| 70 |
| 71 std::unique_ptr<AudioDebugRecorder> |
| 72 AudioDebugRecordingManager::RegisterDebugRecordingSource( |
| 73 const base::FilePath::StringType& file_name_extension, |
| 74 const AudioParameters& params) { |
| 75 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 76 |
| 77 const int id = g_next_stream_id++; |
| 78 |
| 79 // Normally, the manager will outlive the one who registers and owns the |
| 80 // returned recorder. But to not require this we use a weak pointer. |
| 81 std::unique_ptr<AudioDebugRecordingHelper> recording_helper = |
| 82 CreateAudioDebugRecordingHelper( |
| 83 params, create_audio_file_writer_callback_, task_runner_, |
| 84 base::BindOnce( |
| 85 &AudioDebugRecordingManager::UnregisterDebugRecordingSource, |
| 86 weak_factory_.GetWeakPtr(), id)); |
| 87 |
| 88 if (IsDebugRecordingEnabled()) { |
| 89 recording_helper->EnableDebugRecording( |
| 90 GetOutputDebugRecordingFileNameWithExtensions( |
| 91 debug_recording_base_file_name_, file_name_extension, id)); |
| 92 } |
| 93 |
| 94 debug_recording_helpers_[id] = |
| 95 std::make_pair(recording_helper.get(), file_name_extension); |
| 96 |
| 97 return base::WrapUnique<AudioDebugRecorder>(recording_helper.release()); |
| 98 } |
| 99 |
| 100 void AudioDebugRecordingManager::UnregisterDebugRecordingSource(int id) { |
| 101 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 102 auto it = debug_recording_helpers_.find(id); |
| 103 DCHECK(it != debug_recording_helpers_.end()); |
| 104 debug_recording_helpers_.erase(id); |
| 105 } |
| 106 |
| 107 std::unique_ptr<AudioDebugRecordingHelper> |
| 108 AudioDebugRecordingManager::CreateAudioDebugRecordingHelper( |
| 109 const AudioParameters& params, |
| 110 const AudioFileWriter::CreateCallback& create_audio_file_writer_callback, |
| 111 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 112 base::OnceClosure on_destruction_closure) { |
| 113 return base::MakeUnique<AudioDebugRecordingHelper>( |
| 114 params, create_audio_file_writer_callback, task_runner, |
| 115 std::move(on_destruction_closure)); |
| 116 } |
| 117 |
| 118 bool AudioDebugRecordingManager::IsDebugRecordingEnabled() { |
| 119 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 120 return !debug_recording_base_file_name_.empty(); |
| 121 } |
| 122 |
| 123 } // namespace media |
OLD | NEW |