OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chrome/browser/extensions/api/audio/audio_service.h" | 5 #include "chrome/browser/extensions/api/audio/audio_service.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "chromeos/audio/audio_device.h" | 10 #include "chromeos/audio/audio_device.h" |
11 #include "chromeos/audio/cras_audio_handler.h" | 11 #include "chromeos/audio/cras_audio_handler.h" |
| 12 #include "chromeos/dbus/cras_audio_client.h" |
| 13 #include "chromeos/dbus/dbus_thread_manager.h" |
12 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
13 | 15 |
14 using content::BrowserThread; | 16 using content::BrowserThread; |
15 | 17 |
16 namespace extensions { | 18 namespace extensions { |
17 | 19 |
18 using api::audio::OutputDeviceInfo; | 20 using api::audio::OutputDeviceInfo; |
19 using api::audio::InputDeviceInfo; | 21 using api::audio::InputDeviceInfo; |
20 | 22 |
21 class AudioServiceImpl : public AudioService, | 23 class AudioServiceImpl : public AudioService, |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 } | 130 } |
129 } | 131 } |
130 callback.Run(output_info, input_info, true); | 132 callback.Run(output_info, input_info, true); |
131 } | 133 } |
132 | 134 |
133 void AudioServiceImpl::SetActiveDevices(const DeviceIdList& device_list) { | 135 void AudioServiceImpl::SetActiveDevices(const DeviceIdList& device_list) { |
134 DCHECK(cras_audio_handler_); | 136 DCHECK(cras_audio_handler_); |
135 if (!cras_audio_handler_) | 137 if (!cras_audio_handler_) |
136 return; | 138 return; |
137 | 139 |
| 140 // De-activate all the nodes with RemoveActive{Input/Output}Node API. This is |
| 141 // kind of hacky, but we don't know which set of nodes are active from |
| 142 // CrasAudioHandler. |
| 143 // TODO(rkc): Fix it in http://crbug.com/402072. |
| 144 chromeos::AudioDeviceList devices; |
| 145 cras_audio_handler_->GetAudioDevices(&devices); |
| 146 for (size_t i = 0; i < devices.size(); ++i) { |
| 147 if (devices[i].is_input) { |
| 148 chromeos::DBusThreadManager::Get() |
| 149 ->GetCrasAudioClient() |
| 150 ->RemoveActiveInputNode(devices[i].id); |
| 151 } else { // output |
| 152 chromeos::DBusThreadManager::Get() |
| 153 ->GetCrasAudioClient() |
| 154 ->RemoveActiveOutputNode(devices[i].id); |
| 155 } |
| 156 } |
| 157 |
138 bool input_device_set = false; | 158 bool input_device_set = false; |
139 bool output_device_set = false; | 159 bool output_device_set = false; |
140 | 160 std::string active_input_node_ids, active_output_node_ids; |
141 for (size_t i = 0; i < device_list.size(); ++i) { | 161 for (size_t i = 0; i < device_list.size(); ++i) { |
142 chromeos::AudioDevice device; | 162 chromeos::AudioDevice device; |
143 bool found = FindDevice(GetIdFromStr(device_list[i]), &device); | 163 bool found = FindDevice(GetIdFromStr(device_list[i]), &device); |
144 if (found) { | 164 if (found) { |
145 if (device.is_input && !input_device_set) { | 165 if (device.is_input) { |
146 cras_audio_handler_->SwitchToDevice(device); | 166 if (!input_device_set) { |
147 input_device_set = true; | 167 cras_audio_handler_->SwitchToDevice(device); |
148 } else if (!device.is_input && !output_device_set) { | 168 input_device_set = true; |
149 cras_audio_handler_->SwitchToDevice(device); | 169 } else { |
150 output_device_set = true; | 170 active_input_node_ids.push_back(device.id); |
| 171 } |
| 172 } else { // output device |
| 173 if (!output_device_set) { |
| 174 cras_audio_handler_->SwitchToDevice(device); |
| 175 output_device_set = true; |
| 176 } else { |
| 177 active_output_node_ids.push_back(device.id); |
| 178 } |
151 } | 179 } |
152 } | 180 } |
153 } | 181 } |
| 182 |
| 183 // Once we have set our devices to active and all the inactive ones have been |
| 184 // set correctly to inactive, go through our active devices again and set |
| 185 // them to active using the AddActiveNode API. |
| 186 // TODO(rkc):Fix this ugly hack in http://crbug.com/402072. |
| 187 for (size_t i = 0; i < active_input_node_ids.size(); ++i) { |
| 188 chromeos::DBusThreadManager::Get() |
| 189 ->GetCrasAudioClient() |
| 190 ->AddActiveInputNode(active_input_node_ids[i]); |
| 191 } |
| 192 for (size_t i = 0; i < active_output_node_ids.size(); ++i) { |
| 193 chromeos::DBusThreadManager::Get() |
| 194 ->GetCrasAudioClient() |
| 195 ->AddActiveOutputNode(active_output_node_ids[i]); |
| 196 } |
154 } | 197 } |
155 | 198 |
156 bool AudioServiceImpl::SetDeviceProperties(const std::string& device_id, | 199 bool AudioServiceImpl::SetDeviceProperties(const std::string& device_id, |
157 bool muted, | 200 bool muted, |
158 int volume, | 201 int volume, |
159 int gain) { | 202 int gain) { |
160 DCHECK(cras_audio_handler_); | 203 DCHECK(cras_audio_handler_); |
161 if (!cras_audio_handler_) | 204 if (!cras_audio_handler_) |
162 return false; | 205 return false; |
163 | 206 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 | 273 |
231 void AudioServiceImpl::NotifyDeviceChanged() { | 274 void AudioServiceImpl::NotifyDeviceChanged() { |
232 FOR_EACH_OBSERVER(AudioService::Observer, observer_list_, OnDeviceChanged()); | 275 FOR_EACH_OBSERVER(AudioService::Observer, observer_list_, OnDeviceChanged()); |
233 } | 276 } |
234 | 277 |
235 AudioService* AudioService::CreateInstance() { | 278 AudioService* AudioService::CreateInstance() { |
236 return new AudioServiceImpl; | 279 return new AudioServiceImpl; |
237 } | 280 } |
238 | 281 |
239 } // namespace extensions | 282 } // namespace extensions |
OLD | NEW |