| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/audio/audio_system_impl.h" | 5 #include "media/audio/audio_system_impl.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
| 9 #include "base/task_runner_util.h" | 9 #include "base/task_runner_util.h" |
| 10 #include "media/audio/audio_device_description.h" | 10 #include "media/audio/audio_device_description.h" |
| 11 #include "media/audio/audio_manager.h" | 11 #include "media/audio/audio_manager.h" |
| 12 #include "media/base/bind_to_current_loop.h" | 12 #include "media/base/bind_to_current_loop.h" |
| 13 | 13 |
| 14 // Using base::Unretained for |audio_manager_| is safe since it is deleted after | 14 // Using base::Unretained for |audio_manager_| is safe since it is deleted after |
| 15 // its task runner, and AudioSystemImpl is deleted on the UI thread after the IO | 15 // its task runner, and AudioSystemImpl is deleted on the UI thread after the IO |
| 16 // thread has been stopped and before |audio_manager_| deletion is scheduled. | 16 // thread has been stopped and before |audio_manager_| deletion is scheduled. |
| 17 namespace media { | 17 namespace media { |
| 18 | 18 |
| 19 namespace { | |
| 20 | |
| 21 AudioParameters GetInputParametersOnDeviceThread(AudioManager* audio_manager, | |
| 22 const std::string& device_id) { | |
| 23 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); | |
| 24 | |
| 25 // TODO(olka): remove this when AudioManager::GetInputStreamParameters() | |
| 26 // returns invalid parameters if the device is not found. | |
| 27 if (!audio_manager->HasAudioInputDevices()) | |
| 28 return AudioParameters(); | |
| 29 | |
| 30 return audio_manager->GetInputStreamParameters(device_id); | |
| 31 } | |
| 32 | |
| 33 AudioParameters GetOutputParametersOnDeviceThread( | |
| 34 AudioManager* audio_manager, | |
| 35 const std::string& device_id) { | |
| 36 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); | |
| 37 | |
| 38 // TODO(olka): remove this when | |
| 39 // AudioManager::Get[Default]OutputStreamParameters() returns invalid | |
| 40 // parameters if the device is not found. | |
| 41 if (!audio_manager->HasAudioOutputDevices()) | |
| 42 return AudioParameters(); | |
| 43 | |
| 44 return media::AudioDeviceDescription::IsDefaultDevice(device_id) | |
| 45 ? audio_manager->GetDefaultOutputStreamParameters() | |
| 46 : audio_manager->GetOutputStreamParameters(device_id); | |
| 47 } | |
| 48 | |
| 49 AudioDeviceDescriptions GetDeviceDescriptionsOnDeviceThread( | |
| 50 AudioManager* audio_manager, | |
| 51 bool for_input) { | |
| 52 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); | |
| 53 AudioDeviceDescriptions descriptions; | |
| 54 if (for_input) | |
| 55 audio_manager->GetAudioInputDeviceDescriptions(&descriptions); | |
| 56 else | |
| 57 audio_manager->GetAudioOutputDeviceDescriptions(&descriptions); | |
| 58 return descriptions; | |
| 59 } | |
| 60 | |
| 61 void GetInputDeviceInfoOnDeviceThread( | |
| 62 AudioManager* audio_manager, | |
| 63 const std::string& input_device_id, | |
| 64 AudioSystem::OnInputDeviceInfoCallback on_input_device_info_cb) { | |
| 65 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); | |
| 66 const std::string associated_output_device_id = | |
| 67 audio_manager->GetAssociatedOutputDeviceID(input_device_id); | |
| 68 | |
| 69 on_input_device_info_cb.Run( | |
| 70 GetInputParametersOnDeviceThread(audio_manager, input_device_id), | |
| 71 associated_output_device_id.empty() | |
| 72 ? AudioParameters() | |
| 73 : GetOutputParametersOnDeviceThread(audio_manager, | |
| 74 associated_output_device_id), | |
| 75 associated_output_device_id); | |
| 76 } | |
| 77 | |
| 78 } // namespace | |
| 79 | |
| 80 AudioSystemImpl::AudioSystemImpl(AudioManager* audio_manager) | 19 AudioSystemImpl::AudioSystemImpl(AudioManager* audio_manager) |
| 81 : audio_manager_(audio_manager) { | 20 : audio_manager_(audio_manager) { |
| 82 DCHECK(audio_manager_); | 21 DCHECK(audio_manager_); |
| 83 AudioSystem::SetInstance(this); | 22 AudioSystem::SetInstance(this); |
| 84 } | 23 } |
| 85 | 24 |
| 86 AudioSystemImpl::~AudioSystemImpl() { | 25 AudioSystemImpl::~AudioSystemImpl() { |
| 87 AudioSystem::ClearInstance(this); | 26 AudioSystem::ClearInstance(this); |
| 88 } | 27 } |
| 89 | 28 |
| 90 // static | 29 // static |
| 91 std::unique_ptr<AudioSystem> AudioSystemImpl::Create( | 30 std::unique_ptr<AudioSystem> AudioSystemImpl::Create( |
| 92 AudioManager* audio_manager) { | 31 AudioManager* audio_manager) { |
| 93 return base::WrapUnique(new AudioSystemImpl(audio_manager)); | 32 return base::WrapUnique(new AudioSystemImpl(audio_manager)); |
| 94 } | 33 } |
| 95 | 34 |
| 96 void AudioSystemImpl::GetInputStreamParameters( | 35 void AudioSystemImpl::GetInputStreamParameters( |
| 97 const std::string& device_id, | 36 const std::string& device_id, |
| 98 OnAudioParamsCallback on_params_cb) const { | 37 OnAudioParamsCallback on_params_cb) const { |
| 99 if (GetTaskRunner()->BelongsToCurrentThread()) { | 38 if (GetTaskRunner()->BelongsToCurrentThread()) { |
| 100 GetTaskRunner()->PostTask(FROM_HERE, | 39 GetTaskRunner()->PostTask(FROM_HERE, |
| 101 base::BindOnce(std::move(on_params_cb), | 40 base::BindOnce(std::move(on_params_cb), |
| 102 GetInputParametersOnDeviceThread( | 41 GetInputParametersOnDeviceThread( |
| 103 audio_manager_, device_id))); | 42 audio_manager_, device_id))); |
| 104 return; | 43 return; |
| 105 } | 44 } |
| 106 base::PostTaskAndReplyWithResult( | 45 base::PostTaskAndReplyWithResult( |
| 107 GetTaskRunner(), FROM_HERE, | 46 GetTaskRunner(), FROM_HERE, |
| 108 base::Bind(&GetInputParametersOnDeviceThread, | 47 base::Bind(&AudioSystemImpl::GetInputParametersOnDeviceThread, |
| 109 base::Unretained(audio_manager_), device_id), | 48 base::Unretained(audio_manager_), device_id), |
| 110 std::move(on_params_cb)); | 49 std::move(on_params_cb)); |
| 111 } | 50 } |
| 112 | 51 |
| 113 void AudioSystemImpl::GetOutputStreamParameters( | 52 void AudioSystemImpl::GetOutputStreamParameters( |
| 114 const std::string& device_id, | 53 const std::string& device_id, |
| 115 OnAudioParamsCallback on_params_cb) const { | 54 OnAudioParamsCallback on_params_cb) const { |
| 116 if (GetTaskRunner()->BelongsToCurrentThread()) { | 55 if (GetTaskRunner()->BelongsToCurrentThread()) { |
| 117 GetTaskRunner()->PostTask(FROM_HERE, | 56 GetTaskRunner()->PostTask(FROM_HERE, |
| 118 base::BindOnce(std::move(on_params_cb), | 57 base::BindOnce(std::move(on_params_cb), |
| 119 GetOutputParametersOnDeviceThread( | 58 GetOutputParametersOnDeviceThread( |
| 120 audio_manager_, device_id))); | 59 audio_manager_, device_id))); |
| 121 return; | 60 return; |
| 122 } | 61 } |
| 123 base::PostTaskAndReplyWithResult( | 62 base::PostTaskAndReplyWithResult( |
| 124 GetTaskRunner(), FROM_HERE, | 63 GetTaskRunner(), FROM_HERE, |
| 125 base::Bind(&GetOutputParametersOnDeviceThread, | 64 base::Bind(&AudioSystemImpl::GetOutputParametersOnDeviceThread, |
| 126 base::Unretained(audio_manager_), device_id), | 65 base::Unretained(audio_manager_), device_id), |
| 127 std::move(on_params_cb)); | 66 std::move(on_params_cb)); |
| 128 } | 67 } |
| 129 | 68 |
| 130 void AudioSystemImpl::HasInputDevices(OnBoolCallback on_has_devices_cb) const { | 69 void AudioSystemImpl::HasInputDevices(OnBoolCallback on_has_devices_cb) const { |
| 131 if (GetTaskRunner()->BelongsToCurrentThread()) { | 70 if (GetTaskRunner()->BelongsToCurrentThread()) { |
| 132 GetTaskRunner()->PostTask( | 71 GetTaskRunner()->PostTask( |
| 133 FROM_HERE, base::BindOnce(std::move(on_has_devices_cb), | 72 FROM_HERE, base::BindOnce(std::move(on_has_devices_cb), |
| 134 audio_manager_->HasAudioInputDevices())); | 73 audio_manager_->HasAudioInputDevices())); |
| 135 return; | 74 return; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 162 GetTaskRunner()->PostTask( | 101 GetTaskRunner()->PostTask( |
| 163 FROM_HERE, | 102 FROM_HERE, |
| 164 base::BindOnce(std::move(on_descriptions_cb), | 103 base::BindOnce(std::move(on_descriptions_cb), |
| 165 base::Passed(GetDeviceDescriptionsOnDeviceThread( | 104 base::Passed(GetDeviceDescriptionsOnDeviceThread( |
| 166 audio_manager_, for_input)))); | 105 audio_manager_, for_input)))); |
| 167 return; | 106 return; |
| 168 } | 107 } |
| 169 | 108 |
| 170 base::PostTaskAndReplyWithResult( | 109 base::PostTaskAndReplyWithResult( |
| 171 GetTaskRunner(), FROM_HERE, | 110 GetTaskRunner(), FROM_HERE, |
| 172 base::Bind(&GetDeviceDescriptionsOnDeviceThread, | 111 base::Bind(&AudioSystemImpl::GetDeviceDescriptionsOnDeviceThread, |
| 173 base::Unretained(audio_manager_), for_input), | 112 base::Unretained(audio_manager_), for_input), |
| 174 std::move(on_descriptions_cb)); | 113 std::move(on_descriptions_cb)); |
| 175 } | 114 } |
| 176 | 115 |
| 177 void AudioSystemImpl::GetAssociatedOutputDeviceID( | 116 void AudioSystemImpl::GetAssociatedOutputDeviceID( |
| 178 const std::string& input_device_id, | 117 const std::string& input_device_id, |
| 179 OnDeviceIdCallback on_device_id_cb) { | 118 OnDeviceIdCallback on_device_id_cb) { |
| 180 if (GetTaskRunner()->BelongsToCurrentThread()) { | 119 if (GetTaskRunner()->BelongsToCurrentThread()) { |
| 181 GetTaskRunner()->PostTask( | 120 GetTaskRunner()->PostTask( |
| 182 FROM_HERE, base::BindOnce(std::move(on_device_id_cb), | 121 FROM_HERE, base::BindOnce(std::move(on_device_id_cb), |
| (...skipping 11 matching lines...) Expand all Loading... |
| 194 void AudioSystemImpl::GetInputDeviceInfo( | 133 void AudioSystemImpl::GetInputDeviceInfo( |
| 195 const std::string& input_device_id, | 134 const std::string& input_device_id, |
| 196 OnInputDeviceInfoCallback on_input_device_info_cb) { | 135 OnInputDeviceInfoCallback on_input_device_info_cb) { |
| 197 // No need to bind |on_input_device_info_cb| to the current loop if we are on | 136 // No need to bind |on_input_device_info_cb| to the current loop if we are on |
| 198 // the audio thread. However, the client still expect to receive the reply | 137 // the audio thread. However, the client still expect to receive the reply |
| 199 // asynchronously, so we always post GetInputDeviceInfoOnDeviceThread(), which | 138 // asynchronously, so we always post GetInputDeviceInfoOnDeviceThread(), which |
| 200 // will syncronously call the (bound to current loop or not) callback. | 139 // will syncronously call the (bound to current loop or not) callback. |
| 201 GetTaskRunner()->PostTask( | 140 GetTaskRunner()->PostTask( |
| 202 FROM_HERE, | 141 FROM_HERE, |
| 203 base::BindOnce( | 142 base::BindOnce( |
| 204 &GetInputDeviceInfoOnDeviceThread, base::Unretained(audio_manager_), | 143 &AudioSystemImpl::GetInputDeviceInfoOnDeviceThread, |
| 205 input_device_id, | 144 base::Unretained(audio_manager_), input_device_id, |
| 206 GetTaskRunner()->BelongsToCurrentThread() | 145 GetTaskRunner()->BelongsToCurrentThread() |
| 207 ? std::move(on_input_device_info_cb) | 146 ? std::move(on_input_device_info_cb) |
| 208 : media::BindToCurrentLoop(std::move(on_input_device_info_cb)))); | 147 : media::BindToCurrentLoop(std::move(on_input_device_info_cb)))); |
| 209 } | 148 } |
| 210 | 149 |
| 211 base::SingleThreadTaskRunner* AudioSystemImpl::GetTaskRunner() const { | 150 base::SingleThreadTaskRunner* AudioSystemImpl::GetTaskRunner() const { |
| 212 return audio_manager_->GetTaskRunner(); | 151 return audio_manager_->GetTaskRunner(); |
| 213 } | 152 } |
| 214 | 153 |
| 154 // static |
| 155 AudioParameters AudioSystemImpl::GetInputParametersOnDeviceThread( |
| 156 AudioManager* audio_manager, |
| 157 const std::string& device_id) { |
| 158 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); |
| 159 |
| 160 // TODO(olka): remove this when AudioManager::GetInputStreamParameters() |
| 161 // returns invalid parameters if the device is not found. |
| 162 if (!audio_manager->HasAudioInputDevices()) |
| 163 return AudioParameters(); |
| 164 |
| 165 return audio_manager->GetInputStreamParameters(device_id); |
| 166 } |
| 167 |
| 168 // static |
| 169 AudioParameters AudioSystemImpl::GetOutputParametersOnDeviceThread( |
| 170 AudioManager* audio_manager, |
| 171 const std::string& device_id) { |
| 172 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); |
| 173 |
| 174 // TODO(olka): remove this when |
| 175 // AudioManager::Get[Default]OutputStreamParameters() returns invalid |
| 176 // parameters if the device is not found. |
| 177 if (!audio_manager->HasAudioOutputDevices()) |
| 178 return AudioParameters(); |
| 179 |
| 180 return media::AudioDeviceDescription::IsDefaultDevice(device_id) |
| 181 ? audio_manager->GetDefaultOutputStreamParameters() |
| 182 : audio_manager->GetOutputStreamParameters(device_id); |
| 183 } |
| 184 |
| 185 // static |
| 186 AudioDeviceDescriptions AudioSystemImpl::GetDeviceDescriptionsOnDeviceThread( |
| 187 AudioManager* audio_manager, |
| 188 bool for_input) { |
| 189 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); |
| 190 AudioDeviceDescriptions descriptions; |
| 191 if (for_input) |
| 192 audio_manager->GetAudioInputDeviceDescriptions(&descriptions); |
| 193 else |
| 194 audio_manager->GetAudioOutputDeviceDescriptions(&descriptions); |
| 195 return descriptions; |
| 196 } |
| 197 |
| 198 // static |
| 199 void AudioSystemImpl::GetInputDeviceInfoOnDeviceThread( |
| 200 AudioManager* audio_manager, |
| 201 const std::string& input_device_id, |
| 202 AudioSystem::OnInputDeviceInfoCallback on_input_device_info_cb) { |
| 203 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); |
| 204 const std::string associated_output_device_id = |
| 205 audio_manager->GetAssociatedOutputDeviceID(input_device_id); |
| 206 |
| 207 on_input_device_info_cb.Run( |
| 208 GetInputParametersOnDeviceThread(audio_manager, input_device_id), |
| 209 associated_output_device_id.empty() |
| 210 ? AudioParameters() |
| 211 : GetOutputParametersOnDeviceThread(audio_manager, |
| 212 associated_output_device_id), |
| 213 associated_output_device_id); |
| 214 } |
| 215 |
| 215 } // namespace media | 216 } // namespace media |
| OLD | NEW |