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" | |
14 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
15 | 13 |
16 using content::BrowserThread; | 14 using content::BrowserThread; |
17 | 15 |
18 namespace extensions { | 16 namespace extensions { |
19 | 17 |
20 using api::audio::OutputDeviceInfo; | 18 using api::audio::OutputDeviceInfo; |
21 using api::audio::InputDeviceInfo; | 19 using api::audio::InputDeviceInfo; |
22 | 20 |
23 class AudioServiceImpl : public AudioService, | 21 class AudioServiceImpl : public AudioService, |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 } | 128 } |
131 } | 129 } |
132 callback.Run(output_info, input_info, true); | 130 callback.Run(output_info, input_info, true); |
133 } | 131 } |
134 | 132 |
135 void AudioServiceImpl::SetActiveDevices(const DeviceIdList& device_list) { | 133 void AudioServiceImpl::SetActiveDevices(const DeviceIdList& device_list) { |
136 DCHECK(cras_audio_handler_); | 134 DCHECK(cras_audio_handler_); |
137 if (!cras_audio_handler_) | 135 if (!cras_audio_handler_) |
138 return; | 136 return; |
139 | 137 |
140 // De-activate all the nodes with RemoveActive{Input/Output}Node API. This is | 138 cras_audio_handler_->RemoveAllActiveNodes(); |
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 | 139 |
158 bool input_device_set = false; | |
159 bool output_device_set = false; | |
160 std::string active_input_node_ids, active_output_node_ids; | |
161 for (size_t i = 0; i < device_list.size(); ++i) { | 140 for (size_t i = 0; i < device_list.size(); ++i) { |
162 chromeos::AudioDevice device; | 141 chromeos::AudioDevice device; |
163 bool found = FindDevice(GetIdFromStr(device_list[i]), &device); | 142 if (FindDevice(GetIdFromStr(device_list[i]), &device)) |
164 if (found) { | 143 cras_audio_handler_->AddActiveNode(device.id); |
165 if (device.is_input) { | |
166 if (!input_device_set) { | |
167 cras_audio_handler_->SwitchToDevice(device); | |
168 input_device_set = true; | |
169 } else { | |
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 } | |
179 } | |
180 } | |
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 } | 144 } |
197 } | 145 } |
198 | 146 |
199 bool AudioServiceImpl::SetDeviceProperties(const std::string& device_id, | 147 bool AudioServiceImpl::SetDeviceProperties(const std::string& device_id, |
200 bool muted, | 148 bool muted, |
201 int volume, | 149 int volume, |
202 int gain) { | 150 int gain) { |
203 DCHECK(cras_audio_handler_); | 151 DCHECK(cras_audio_handler_); |
204 if (!cras_audio_handler_) | 152 if (!cras_audio_handler_) |
205 return false; | 153 return false; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 | 221 |
274 void AudioServiceImpl::NotifyDeviceChanged() { | 222 void AudioServiceImpl::NotifyDeviceChanged() { |
275 FOR_EACH_OBSERVER(AudioService::Observer, observer_list_, OnDeviceChanged()); | 223 FOR_EACH_OBSERVER(AudioService::Observer, observer_list_, OnDeviceChanged()); |
276 } | 224 } |
277 | 225 |
278 AudioService* AudioService::CreateInstance() { | 226 AudioService* AudioService::CreateInstance() { |
279 return new AudioServiceImpl; | 227 return new AudioServiceImpl; |
280 } | 228 } |
281 | 229 |
282 } // namespace extensions | 230 } // namespace extensions |
OLD | NEW |