| 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( | 39 GetTaskRunner()->PostTask( |
| 101 FROM_HERE, base::Bind(on_params_cb, GetInputParametersOnDeviceThread( | 40 FROM_HERE, base::Bind(on_params_cb, GetInputParametersOnDeviceThread( |
| 102 audio_manager_, device_id))); | 41 audio_manager_, device_id))); |
| 103 return; | 42 return; |
| 104 } | 43 } |
| 105 base::PostTaskAndReplyWithResult( | 44 base::PostTaskAndReplyWithResult( |
| 106 GetTaskRunner(), FROM_HERE, | 45 GetTaskRunner(), FROM_HERE, |
| 107 base::Bind(&GetInputParametersOnDeviceThread, | 46 base::Bind(&AudioSystemImpl::GetInputParametersOnDeviceThread, |
| 108 base::Unretained(audio_manager_), device_id), | 47 base::Unretained(audio_manager_), device_id), |
| 109 std::move(on_params_cb)); | 48 std::move(on_params_cb)); |
| 110 } | 49 } |
| 111 | 50 |
| 112 void AudioSystemImpl::GetOutputStreamParameters( | 51 void AudioSystemImpl::GetOutputStreamParameters( |
| 113 const std::string& device_id, | 52 const std::string& device_id, |
| 114 OnAudioParamsCallback on_params_cb) const { | 53 OnAudioParamsCallback on_params_cb) const { |
| 115 if (GetTaskRunner()->BelongsToCurrentThread()) { | 54 if (GetTaskRunner()->BelongsToCurrentThread()) { |
| 116 GetTaskRunner()->PostTask( | 55 GetTaskRunner()->PostTask( |
| 117 FROM_HERE, base::Bind(on_params_cb, GetOutputParametersOnDeviceThread( | 56 FROM_HERE, base::Bind(on_params_cb, GetOutputParametersOnDeviceThread( |
| 118 audio_manager_, device_id))); | 57 audio_manager_, device_id))); |
| 119 return; | 58 return; |
| 120 } | 59 } |
| 121 base::PostTaskAndReplyWithResult( | 60 base::PostTaskAndReplyWithResult( |
| 122 GetTaskRunner(), FROM_HERE, | 61 GetTaskRunner(), FROM_HERE, |
| 123 base::Bind(&GetOutputParametersOnDeviceThread, | 62 base::Bind(&AudioSystemImpl::GetOutputParametersOnDeviceThread, |
| 124 base::Unretained(audio_manager_), device_id), | 63 base::Unretained(audio_manager_), device_id), |
| 125 std::move(on_params_cb)); | 64 std::move(on_params_cb)); |
| 126 } | 65 } |
| 127 | 66 |
| 128 void AudioSystemImpl::HasInputDevices(OnBoolCallback on_has_devices_cb) const { | 67 void AudioSystemImpl::HasInputDevices(OnBoolCallback on_has_devices_cb) const { |
| 129 if (GetTaskRunner()->BelongsToCurrentThread()) { | 68 if (GetTaskRunner()->BelongsToCurrentThread()) { |
| 130 GetTaskRunner()->PostTask( | 69 GetTaskRunner()->PostTask( |
| 131 FROM_HERE, | 70 FROM_HERE, |
| 132 base::Bind(on_has_devices_cb, audio_manager_->HasAudioInputDevices())); | 71 base::Bind(on_has_devices_cb, audio_manager_->HasAudioInputDevices())); |
| 133 return; | 72 return; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 159 if (GetTaskRunner()->BelongsToCurrentThread()) { | 98 if (GetTaskRunner()->BelongsToCurrentThread()) { |
| 160 GetTaskRunner()->PostTask( | 99 GetTaskRunner()->PostTask( |
| 161 FROM_HERE, base::Bind(on_descriptions_cb, | 100 FROM_HERE, base::Bind(on_descriptions_cb, |
| 162 base::Passed(GetDeviceDescriptionsOnDeviceThread( | 101 base::Passed(GetDeviceDescriptionsOnDeviceThread( |
| 163 audio_manager_, for_input)))); | 102 audio_manager_, for_input)))); |
| 164 return; | 103 return; |
| 165 } | 104 } |
| 166 | 105 |
| 167 base::PostTaskAndReplyWithResult( | 106 base::PostTaskAndReplyWithResult( |
| 168 GetTaskRunner(), FROM_HERE, | 107 GetTaskRunner(), FROM_HERE, |
| 169 base::Bind(&GetDeviceDescriptionsOnDeviceThread, | 108 base::Bind(&AudioSystemImpl::GetDeviceDescriptionsOnDeviceThread, |
| 170 base::Unretained(audio_manager_), for_input), | 109 base::Unretained(audio_manager_), for_input), |
| 171 std::move(on_descriptions_cb)); | 110 std::move(on_descriptions_cb)); |
| 172 } | 111 } |
| 173 | 112 |
| 174 void AudioSystemImpl::GetAssociatedOutputDeviceID( | 113 void AudioSystemImpl::GetAssociatedOutputDeviceID( |
| 175 const std::string& input_device_id, | 114 const std::string& input_device_id, |
| 176 OnDeviceIdCallback on_device_id_cb) { | 115 OnDeviceIdCallback on_device_id_cb) { |
| 177 if (GetTaskRunner()->BelongsToCurrentThread()) { | 116 if (GetTaskRunner()->BelongsToCurrentThread()) { |
| 178 GetTaskRunner()->PostTask( | 117 GetTaskRunner()->PostTask( |
| 179 FROM_HERE, | 118 FROM_HERE, |
| 180 base::Bind(on_device_id_cb, audio_manager_->GetAssociatedOutputDeviceID( | 119 base::Bind(on_device_id_cb, audio_manager_->GetAssociatedOutputDeviceID( |
| 181 input_device_id))); | 120 input_device_id))); |
| 182 return; | 121 return; |
| 183 } | 122 } |
| 184 base::PostTaskAndReplyWithResult( | 123 base::PostTaskAndReplyWithResult( |
| 185 GetTaskRunner(), FROM_HERE, | 124 GetTaskRunner(), FROM_HERE, |
| 186 base::Bind(&AudioManager::GetAssociatedOutputDeviceID, | 125 base::Bind(&AudioManager::GetAssociatedOutputDeviceID, |
| 187 base::Unretained(audio_manager_), input_device_id), | 126 base::Unretained(audio_manager_), input_device_id), |
| 188 std::move(on_device_id_cb)); | 127 std::move(on_device_id_cb)); |
| 189 } | 128 } |
| 190 | 129 |
| 191 void AudioSystemImpl::GetInputDeviceInfo( | 130 void AudioSystemImpl::GetInputDeviceInfo( |
| 192 const std::string& input_device_id, | 131 const std::string& input_device_id, |
| 193 OnInputDeviceInfoCallback on_input_device_info_cb) { | 132 OnInputDeviceInfoCallback on_input_device_info_cb) { |
| 194 // No need to bind |on_input_device_info_cb| to the current loop if we are on | 133 // 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 | 134 // the audio thread. However, the client still expect to receive the reply |
| 196 // asynchronously, so we always post GetInputDeviceInfoOnDeviceThread(), which | 135 // asynchronously, so we always post GetInputDeviceInfoOnDeviceThread(), which |
| 197 // will syncronously call the (bound to current loop or not) callback. | 136 // will syncronously call the (bound to current loop or not) callback. |
| 198 GetTaskRunner()->PostTask( | 137 GetTaskRunner()->PostTask( |
| 199 FROM_HERE, base::Bind(&GetInputDeviceInfoOnDeviceThread, | 138 FROM_HERE, base::Bind(&AudioSystemImpl::GetInputDeviceInfoOnDeviceThread, |
| 200 base::Unretained(audio_manager_), input_device_id, | 139 base::Unretained(audio_manager_), input_device_id, |
| 201 GetTaskRunner()->BelongsToCurrentThread() | 140 GetTaskRunner()->BelongsToCurrentThread() |
| 202 ? std::move(on_input_device_info_cb) | 141 ? std::move(on_input_device_info_cb) |
| 203 : media::BindToCurrentLoop( | 142 : media::BindToCurrentLoop( |
| 204 std::move(on_input_device_info_cb)))); | 143 std::move(on_input_device_info_cb)))); |
| 205 } | 144 } |
| 206 | 145 |
| 207 base::SingleThreadTaskRunner* AudioSystemImpl::GetTaskRunner() const { | 146 base::SingleThreadTaskRunner* AudioSystemImpl::GetTaskRunner() const { |
| 208 return audio_manager_->GetTaskRunner(); | 147 return audio_manager_->GetTaskRunner(); |
| 209 } | 148 } |
| 210 | 149 |
| 150 // static |
| 151 AudioParameters AudioSystemImpl::GetInputParametersOnDeviceThread( |
| 152 AudioManager* audio_manager, |
| 153 const std::string& device_id) { |
| 154 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); |
| 155 |
| 156 // TODO(olka): remove this when AudioManager::GetInputStreamParameters() |
| 157 // returns invalid parameters if the device is not found. |
| 158 if (!audio_manager->HasAudioInputDevices()) |
| 159 return AudioParameters(); |
| 160 |
| 161 return audio_manager->GetInputStreamParameters(device_id); |
| 162 } |
| 163 |
| 164 // static |
| 165 AudioParameters AudioSystemImpl::GetOutputParametersOnDeviceThread( |
| 166 AudioManager* audio_manager, |
| 167 const std::string& device_id) { |
| 168 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); |
| 169 |
| 170 // TODO(olka): remove this when |
| 171 // AudioManager::Get[Default]OutputStreamParameters() returns invalid |
| 172 // parameters if the device is not found. |
| 173 if (!audio_manager->HasAudioOutputDevices()) |
| 174 return AudioParameters(); |
| 175 |
| 176 return media::AudioDeviceDescription::IsDefaultDevice(device_id) |
| 177 ? audio_manager->GetDefaultOutputStreamParameters() |
| 178 : audio_manager->GetOutputStreamParameters(device_id); |
| 179 } |
| 180 |
| 181 // static |
| 182 AudioDeviceDescriptions AudioSystemImpl::GetDeviceDescriptionsOnDeviceThread( |
| 183 AudioManager* audio_manager, |
| 184 bool for_input) { |
| 185 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); |
| 186 AudioDeviceDescriptions descriptions; |
| 187 if (for_input) |
| 188 audio_manager->GetAudioInputDeviceDescriptions(&descriptions); |
| 189 else |
| 190 audio_manager->GetAudioOutputDeviceDescriptions(&descriptions); |
| 191 return descriptions; |
| 192 } |
| 193 |
| 194 // static |
| 195 void AudioSystemImpl::GetInputDeviceInfoOnDeviceThread( |
| 196 AudioManager* audio_manager, |
| 197 const std::string& input_device_id, |
| 198 AudioSystem::OnInputDeviceInfoCallback on_input_device_info_cb) { |
| 199 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); |
| 200 const std::string associated_output_device_id = |
| 201 audio_manager->GetAssociatedOutputDeviceID(input_device_id); |
| 202 |
| 203 on_input_device_info_cb.Run( |
| 204 GetInputParametersOnDeviceThread(audio_manager, input_device_id), |
| 205 associated_output_device_id.empty() |
| 206 ? AudioParameters() |
| 207 : GetOutputParametersOnDeviceThread(audio_manager, |
| 208 associated_output_device_id), |
| 209 associated_output_device_id); |
| 210 } |
| 211 |
| 211 } // namespace media | 212 } // namespace media |
| OLD | NEW |