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 "extensions/browser/api/audio/audio_service.h" | 5 #include "extensions/browser/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 "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 | 13 |
14 using content::BrowserThread; | 14 using content::BrowserThread; |
15 | 15 |
16 namespace extensions { | 16 namespace extensions { |
17 | 17 |
18 using core_api::audio::OutputDeviceInfo; | 18 using core_api::audio::OutputDeviceInfo; |
19 using core_api::audio::InputDeviceInfo; | 19 using core_api::audio::InputDeviceInfo; |
| 20 using core_api::audio::AudioDeviceInfo; |
20 | 21 |
21 class AudioServiceImpl : public AudioService, | 22 class AudioServiceImpl : public AudioService, |
22 public chromeos::CrasAudioHandler::AudioObserver { | 23 public chromeos::CrasAudioHandler::AudioObserver { |
23 public: | 24 public: |
24 AudioServiceImpl(); | 25 AudioServiceImpl(); |
25 ~AudioServiceImpl() override; | 26 ~AudioServiceImpl() override; |
26 | 27 |
27 // Called by listeners to this service to add/remove themselves as observers. | 28 // Called by listeners to this service to add/remove themselves as observers. |
28 void AddObserver(AudioService::Observer* observer) override; | 29 void AddObserver(AudioService::Observer* observer) override; |
29 void RemoveObserver(AudioService::Observer* observer) override; | 30 void RemoveObserver(AudioService::Observer* observer) override; |
30 | 31 |
31 // Start to query audio device information. | 32 // Start to query audio device information. |
32 void StartGetInfo(const GetInfoCallback& callback) override; | 33 void StartGetInfo(const GetInfoCallback& callback) override; |
33 void SetActiveDevices(const DeviceIdList& device_list) override; | 34 void SetActiveDevices(const DeviceIdList& device_list) override; |
34 bool SetDeviceProperties(const std::string& device_id, | 35 bool SetDeviceProperties(const std::string& device_id, |
35 bool muted, | 36 bool muted, |
36 int volume, | 37 int volume, |
37 int gain) override; | 38 int gain) override; |
| 39 void StartGetDeviceInfo(const GetDeviceInfoCallback& callback) override; |
38 | 40 |
39 protected: | 41 protected: |
40 // chromeos::CrasAudioHandler::AudioObserver overrides. | 42 // chromeos::CrasAudioHandler::AudioObserver overrides. |
41 void OnOutputVolumeChanged() override; | 43 void OnOutputNodeVolumeChanged(uint64 id, double volume) override; |
42 void OnInputGainChanged() override; | 44 void OnInputGainChanged() override; |
43 void OnOutputMuteChanged() override; | 45 void OnOutputMuteChanged() override; |
44 void OnInputMuteChanged() override; | 46 void OnInputMuteChanged() override; |
45 void OnAudioNodesChanged() override; | 47 void OnAudioNodesChanged() override; |
46 void OnActiveOutputNodeChanged() override; | 48 void OnActiveOutputNodeChanged() override; |
47 void OnActiveInputNodeChanged() override; | 49 void OnActiveInputNodeChanged() override; |
48 | 50 |
49 private: | 51 private: |
50 void NotifyDeviceChanged(); | 52 void NotifyDeviceChanged(); |
51 | 53 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 cras_audio_handler_->SetVolumeGainPercentForDevice(device.id, volume); | 165 cras_audio_handler_->SetVolumeGainPercentForDevice(device.id, volume); |
164 return true; | 166 return true; |
165 } else if (device.is_input && gain != -1) { | 167 } else if (device.is_input && gain != -1) { |
166 cras_audio_handler_->SetVolumeGainPercentForDevice(device.id, gain); | 168 cras_audio_handler_->SetVolumeGainPercentForDevice(device.id, gain); |
167 return true; | 169 return true; |
168 } | 170 } |
169 | 171 |
170 return false; | 172 return false; |
171 } | 173 } |
172 | 174 |
| 175 void AudioServiceImpl::StartGetDeviceInfo( |
| 176 const GetDeviceInfoCallback& callback) { |
| 177 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 178 DCHECK(cras_audio_handler_); |
| 179 DCHECK(!callback.is_null()); |
| 180 |
| 181 if (callback.is_null()) |
| 182 return; |
| 183 |
| 184 DevicesInfo devices_info; |
| 185 if (!cras_audio_handler_) { |
| 186 callback.Run(devices_info, false); |
| 187 return; |
| 188 } |
| 189 |
| 190 chromeos::AudioDeviceList devices; |
| 191 cras_audio_handler_->GetAudioDevices(&devices); |
| 192 for (size_t i = 0; i < devices.size(); ++i) { |
| 193 linked_ptr<AudioDeviceInfo> info(new AudioDeviceInfo()); |
| 194 info->id = base::Uint64ToString(devices[i].id); |
| 195 info->display_name = devices[i].display_name; |
| 196 info->device_name = devices[i].device_name; |
| 197 info->is_input = devices[i].is_input; |
| 198 info->is_active = devices[i].active; |
| 199 info->volume_gain = |
| 200 devices[i].is_input |
| 201 ? cras_audio_handler_->GetInputGainPercentForDevice(devices[i].id) |
| 202 : cras_audio_handler_->GetOutputVolumePercentForDevice( |
| 203 devices[i].id); |
| 204 info->is_muted = |
| 205 devices[i].is_input |
| 206 ? cras_audio_handler_->IsInputMutedForDevice(devices[i].id) |
| 207 : cras_audio_handler_->IsOutputMutedForDevice(devices[i].id); |
| 208 devices_info.push_back(info); |
| 209 } |
| 210 |
| 211 callback.Run(devices_info, true); |
| 212 } |
| 213 |
173 bool AudioServiceImpl::FindDevice(uint64 id, chromeos::AudioDevice* device) { | 214 bool AudioServiceImpl::FindDevice(uint64 id, chromeos::AudioDevice* device) { |
174 chromeos::AudioDeviceList devices; | 215 chromeos::AudioDeviceList devices; |
175 cras_audio_handler_->GetAudioDevices(&devices); | 216 cras_audio_handler_->GetAudioDevices(&devices); |
176 | 217 |
177 for (size_t i = 0; i < devices.size(); ++i) { | 218 for (size_t i = 0; i < devices.size(); ++i) { |
178 if (devices[i].id == id) { | 219 if (devices[i].id == id) { |
179 *device = devices[i]; | 220 *device = devices[i]; |
180 return true; | 221 return true; |
181 } | 222 } |
182 } | 223 } |
183 return false; | 224 return false; |
184 } | 225 } |
185 | 226 |
186 uint64 AudioServiceImpl::GetIdFromStr(const std::string& id_str) { | 227 uint64 AudioServiceImpl::GetIdFromStr(const std::string& id_str) { |
187 uint64 device_id; | 228 uint64 device_id; |
188 if (!base::StringToUint64(id_str, &device_id)) | 229 if (!base::StringToUint64(id_str, &device_id)) |
189 return 0; | 230 return 0; |
190 else | 231 else |
191 return device_id; | 232 return device_id; |
192 } | 233 } |
193 | 234 |
194 void AudioServiceImpl::OnOutputVolumeChanged() { | 235 void AudioServiceImpl::OnOutputNodeVolumeChanged(uint64 id, double volume) { |
| 236 FOR_EACH_OBSERVER( |
| 237 AudioService::Observer, observer_list_, |
| 238 OnOutputNodeVolumeChanged(base::Uint64ToString(id), volume)); |
| 239 |
| 240 // Notify DeviceChanged event for backward compatibility. |
| 241 // TODO(jennyz): remove this code when the old version of hotrod retires. |
195 NotifyDeviceChanged(); | 242 NotifyDeviceChanged(); |
196 } | 243 } |
197 | 244 |
198 void AudioServiceImpl::OnOutputMuteChanged() { | 245 void AudioServiceImpl::OnOutputMuteChanged() { |
199 NotifyDeviceChanged(); | 246 NotifyDeviceChanged(); |
200 } | 247 } |
201 | 248 |
202 void AudioServiceImpl::OnInputGainChanged() { | 249 void AudioServiceImpl::OnInputGainChanged() { |
203 NotifyDeviceChanged(); | 250 NotifyDeviceChanged(); |
204 } | 251 } |
(...skipping 16 matching lines...) Expand all Loading... |
221 | 268 |
222 void AudioServiceImpl::NotifyDeviceChanged() { | 269 void AudioServiceImpl::NotifyDeviceChanged() { |
223 FOR_EACH_OBSERVER(AudioService::Observer, observer_list_, OnDeviceChanged()); | 270 FOR_EACH_OBSERVER(AudioService::Observer, observer_list_, OnDeviceChanged()); |
224 } | 271 } |
225 | 272 |
226 AudioService* AudioService::CreateInstance() { | 273 AudioService* AudioService::CreateInstance() { |
227 return new AudioServiceImpl; | 274 return new AudioServiceImpl; |
228 } | 275 } |
229 | 276 |
230 } // namespace extensions | 277 } // namespace extensions |
OLD | NEW |