| 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 | 13 |
| 13 // 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 |
| 14 // 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 |
| 15 // thread has been stopped and before |audio_manager_| deletion is scheduled. | 16 // thread has been stopped and before |audio_manager_| deletion is scheduled. |
| 16 namespace media { | 17 namespace media { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 AudioParameters GetInputParametersOnDeviceThread(AudioManager* audio_manager, | 21 AudioParameters GetInputParametersOnDeviceThread(AudioManager* audio_manager, |
| 21 const std::string& device_id) { | 22 const std::string& device_id) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 50 bool for_input) { | 51 bool for_input) { |
| 51 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); | 52 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); |
| 52 AudioDeviceDescriptions descriptions; | 53 AudioDeviceDescriptions descriptions; |
| 53 if (for_input) | 54 if (for_input) |
| 54 audio_manager->GetAudioInputDeviceDescriptions(&descriptions); | 55 audio_manager->GetAudioInputDeviceDescriptions(&descriptions); |
| 55 else | 56 else |
| 56 audio_manager->GetAudioOutputDeviceDescriptions(&descriptions); | 57 audio_manager->GetAudioOutputDeviceDescriptions(&descriptions); |
| 57 return descriptions; | 58 return descriptions; |
| 58 } | 59 } |
| 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 |
| 60 } // namespace | 78 } // namespace |
| 61 | 79 |
| 62 AudioSystemImpl::AudioSystemImpl(AudioManager* audio_manager) | 80 AudioSystemImpl::AudioSystemImpl(AudioManager* audio_manager) |
| 63 : audio_manager_(audio_manager) { | 81 : audio_manager_(audio_manager) { |
| 64 DCHECK(audio_manager_); | 82 DCHECK(audio_manager_); |
| 65 AudioSystem::SetInstance(this); | 83 AudioSystem::SetInstance(this); |
| 66 } | 84 } |
| 67 | 85 |
| 68 AudioSystemImpl::~AudioSystemImpl() { | 86 AudioSystemImpl::~AudioSystemImpl() { |
| 69 AudioSystem::ClearInstance(this); | 87 AudioSystem::ClearInstance(this); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 return; | 147 return; |
| 130 } | 148 } |
| 131 base::PostTaskAndReplyWithResult( | 149 base::PostTaskAndReplyWithResult( |
| 132 GetTaskRunner(), FROM_HERE, | 150 GetTaskRunner(), FROM_HERE, |
| 133 base::Bind(&AudioManager::HasAudioOutputDevices, | 151 base::Bind(&AudioManager::HasAudioOutputDevices, |
| 134 base::Unretained(audio_manager_)), | 152 base::Unretained(audio_manager_)), |
| 135 std::move(on_has_devices_cb)); | 153 std::move(on_has_devices_cb)); |
| 136 } | 154 } |
| 137 | 155 |
| 138 void AudioSystemImpl::GetDeviceDescriptions( | 156 void AudioSystemImpl::GetDeviceDescriptions( |
| 139 OnDeviceDescriptionsCallback on_descriptions_cp, | 157 OnDeviceDescriptionsCallback on_descriptions_cb, |
| 140 bool for_input) { | 158 bool for_input) { |
| 141 if (GetTaskRunner()->BelongsToCurrentThread()) { | 159 if (GetTaskRunner()->BelongsToCurrentThread()) { |
| 142 GetTaskRunner()->PostTask( | 160 GetTaskRunner()->PostTask( |
| 143 FROM_HERE, base::Bind(on_descriptions_cp, | 161 FROM_HERE, base::Bind(on_descriptions_cb, |
| 144 base::Passed(GetDeviceDescriptionsOnDeviceThread( | 162 base::Passed(GetDeviceDescriptionsOnDeviceThread( |
| 145 audio_manager_, for_input)))); | 163 audio_manager_, for_input)))); |
| 146 return; | 164 return; |
| 147 } | 165 } |
| 148 | 166 |
| 149 base::PostTaskAndReplyWithResult( | 167 base::PostTaskAndReplyWithResult( |
| 150 GetTaskRunner(), FROM_HERE, | 168 GetTaskRunner(), FROM_HERE, |
| 151 base::Bind(&GetDeviceDescriptionsOnDeviceThread, | 169 base::Bind(&GetDeviceDescriptionsOnDeviceThread, |
| 152 base::Unretained(audio_manager_), for_input), | 170 base::Unretained(audio_manager_), for_input), |
| 153 std::move(on_descriptions_cp)); | 171 std::move(on_descriptions_cb)); |
| 154 } | 172 } |
| 155 | 173 |
| 156 AudioManager* AudioSystemImpl::GetAudioManager() const { | 174 void AudioSystemImpl::GetAssociatedOutputDeviceID( |
| 157 return audio_manager_; | 175 const std::string& input_device_id, |
| 176 OnDeviceIdCallback on_device_id_cb) { |
| 177 if (GetTaskRunner()->BelongsToCurrentThread()) { |
| 178 GetTaskRunner()->PostTask( |
| 179 FROM_HERE, |
| 180 base::Bind(on_device_id_cb, audio_manager_->GetAssociatedOutputDeviceID( |
| 181 input_device_id))); |
| 182 return; |
| 183 } |
| 184 base::PostTaskAndReplyWithResult( |
| 185 GetTaskRunner(), FROM_HERE, |
| 186 base::Bind(&AudioManager::GetAssociatedOutputDeviceID, |
| 187 base::Unretained(audio_manager_), input_device_id), |
| 188 std::move(on_device_id_cb)); |
| 189 } |
| 190 |
| 191 void AudioSystemImpl::GetInputDeviceInfo( |
| 192 const std::string& input_device_id, |
| 193 OnInputDeviceInfoCallback on_input_device_info_cb) { |
| 194 // No need to bind |on_input_device_info_cb| to the current loop if we are on |
| 195 // the audio thread. However, the client still expect to receive the reply |
| 196 // asynchronously, so we always post GetInputDeviceInfoOnDeviceThread(), which |
| 197 // will syncronously call the (bound to current loop or not) callback. |
| 198 GetTaskRunner()->PostTask( |
| 199 FROM_HERE, base::Bind(&GetInputDeviceInfoOnDeviceThread, |
| 200 base::Unretained(audio_manager_), input_device_id, |
| 201 GetTaskRunner()->BelongsToCurrentThread() |
| 202 ? std::move(on_input_device_info_cb) |
| 203 : media::BindToCurrentLoop( |
| 204 std::move(on_input_device_info_cb)))); |
| 158 } | 205 } |
| 159 | 206 |
| 160 base::SingleThreadTaskRunner* AudioSystemImpl::GetTaskRunner() const { | 207 base::SingleThreadTaskRunner* AudioSystemImpl::GetTaskRunner() const { |
| 161 return audio_manager_->GetTaskRunner(); | 208 return audio_manager_->GetTaskRunner(); |
| 162 } | 209 } |
| 163 | 210 |
| 164 } // namespace media | 211 } // namespace media |
| OLD | NEW |