| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 public chromeos::CrasAudioHandler::AudioObserver { | 27 public chromeos::CrasAudioHandler::AudioObserver { |
| 28 public: | 28 public: |
| 29 AudioServiceImpl(); | 29 AudioServiceImpl(); |
| 30 ~AudioServiceImpl() override; | 30 ~AudioServiceImpl() override; |
| 31 | 31 |
| 32 // Called by listeners to this service to add/remove themselves as observers. | 32 // Called by listeners to this service to add/remove themselves as observers. |
| 33 void AddObserver(AudioService::Observer* observer) override; | 33 void AddObserver(AudioService::Observer* observer) override; |
| 34 void RemoveObserver(AudioService::Observer* observer) override; | 34 void RemoveObserver(AudioService::Observer* observer) override; |
| 35 | 35 |
| 36 // Start to query audio device information. | 36 // Start to query audio device information. |
| 37 void StartGetInfo(const GetInfoCallback& callback) override; | 37 bool GetInfo(OutputInfo* output_info_out, InputInfo* input_info_out) override; |
| 38 void SetActiveDevices(const DeviceIdList& device_list) override; | 38 void SetActiveDevices(const DeviceIdList& device_list) override; |
| 39 bool SetDeviceProperties(const std::string& device_id, | 39 bool SetDeviceProperties(const std::string& device_id, |
| 40 bool muted, | 40 bool muted, |
| 41 int volume, | 41 int volume, |
| 42 int gain) override; | 42 int gain) override; |
| 43 | 43 |
| 44 protected: | 44 protected: |
| 45 // chromeos::CrasAudioHandler::AudioObserver overrides. | 45 // chromeos::CrasAudioHandler::AudioObserver overrides. |
| 46 void OnOutputNodeVolumeChanged(uint64_t id, int volume) override; | 46 void OnOutputNodeVolumeChanged(uint64_t id, int volume) override; |
| 47 void OnInputNodeGainChanged(uint64_t id, int gain) override; | 47 void OnInputNodeGainChanged(uint64_t id, int gain) override; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 } | 88 } |
| 89 | 89 |
| 90 void AudioServiceImpl::AddObserver(AudioService::Observer* observer) { | 90 void AudioServiceImpl::AddObserver(AudioService::Observer* observer) { |
| 91 observer_list_.AddObserver(observer); | 91 observer_list_.AddObserver(observer); |
| 92 } | 92 } |
| 93 | 93 |
| 94 void AudioServiceImpl::RemoveObserver(AudioService::Observer* observer) { | 94 void AudioServiceImpl::RemoveObserver(AudioService::Observer* observer) { |
| 95 observer_list_.RemoveObserver(observer); | 95 observer_list_.RemoveObserver(observer); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void AudioServiceImpl::StartGetInfo(const GetInfoCallback& callback) { | 98 bool AudioServiceImpl::GetInfo(OutputInfo* output_info_out, |
| 99 InputInfo* input_info_out) { |
| 99 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 100 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 100 DCHECK(cras_audio_handler_); | 101 DCHECK(cras_audio_handler_); |
| 101 DCHECK(!callback.is_null()); | 102 DCHECK(output_info_out); |
| 103 DCHECK(input_info_out); |
| 102 | 104 |
| 103 if (callback.is_null()) | 105 if (!cras_audio_handler_) |
| 104 return; | 106 return false; |
| 105 | |
| 106 OutputInfo output_info; | |
| 107 InputInfo input_info; | |
| 108 if (!cras_audio_handler_) { | |
| 109 callback.Run(output_info, input_info, false); | |
| 110 return; | |
| 111 } | |
| 112 | 107 |
| 113 chromeos::AudioDeviceList devices; | 108 chromeos::AudioDeviceList devices; |
| 114 cras_audio_handler_->GetAudioDevices(&devices); | 109 cras_audio_handler_->GetAudioDevices(&devices); |
| 115 for (size_t i = 0; i < devices.size(); ++i) { | 110 for (size_t i = 0; i < devices.size(); ++i) { |
| 116 if (!devices[i].is_input) { | 111 if (!devices[i].is_input) { |
| 117 linked_ptr<OutputDeviceInfo> info(new OutputDeviceInfo()); | 112 OutputDeviceInfo info; |
| 118 info->id = base::Uint64ToString(devices[i].id); | 113 info.id = base::Uint64ToString(devices[i].id); |
| 119 info->name = devices[i].device_name + ": " + devices[i].display_name; | 114 info.name = devices[i].device_name + ": " + devices[i].display_name; |
| 120 info->is_active = devices[i].active; | 115 info.is_active = devices[i].active; |
| 121 info->volume = | 116 info.volume = |
| 122 cras_audio_handler_->GetOutputVolumePercentForDevice(devices[i].id); | 117 cras_audio_handler_->GetOutputVolumePercentForDevice(devices[i].id); |
| 123 info->is_muted = | 118 info.is_muted = |
| 124 cras_audio_handler_->IsOutputMutedForDevice(devices[i].id); | 119 cras_audio_handler_->IsOutputMutedForDevice(devices[i].id); |
| 125 output_info.push_back(info); | 120 output_info_out->push_back(std::move(info)); |
| 126 } else { | 121 } else { |
| 127 linked_ptr<InputDeviceInfo> info(new InputDeviceInfo()); | 122 InputDeviceInfo info; |
| 128 info->id = base::Uint64ToString(devices[i].id); | 123 info.id = base::Uint64ToString(devices[i].id); |
| 129 info->name = devices[i].device_name + ": " + devices[i].display_name; | 124 info.name = devices[i].device_name + ": " + devices[i].display_name; |
| 130 info->is_active = devices[i].active; | 125 info.is_active = devices[i].active; |
| 131 info->gain = | 126 info.gain = |
| 132 cras_audio_handler_->GetInputGainPercentForDevice(devices[i].id); | 127 cras_audio_handler_->GetInputGainPercentForDevice(devices[i].id); |
| 133 info->is_muted = | 128 info.is_muted = cras_audio_handler_->IsInputMutedForDevice(devices[i].id); |
| 134 cras_audio_handler_->IsInputMutedForDevice(devices[i].id); | 129 input_info_out->push_back(std::move(info)); |
| 135 input_info.push_back(info); | |
| 136 } | 130 } |
| 137 } | 131 } |
| 138 callback.Run(output_info, input_info, true); | 132 return true; |
| 139 } | 133 } |
| 140 | 134 |
| 141 void AudioServiceImpl::SetActiveDevices(const DeviceIdList& device_list) { | 135 void AudioServiceImpl::SetActiveDevices(const DeviceIdList& device_list) { |
| 142 DCHECK(cras_audio_handler_); | 136 DCHECK(cras_audio_handler_); |
| 143 if (!cras_audio_handler_) | 137 if (!cras_audio_handler_) |
| 144 return; | 138 return; |
| 145 | 139 |
| 146 chromeos::CrasAudioHandler::NodeIdList id_list; | 140 chromeos::CrasAudioHandler::NodeIdList id_list; |
| 147 for (size_t i = 0; i < device_list.size(); ++i) { | 141 for (size_t i = 0; i < device_list.size(); ++i) { |
| 148 chromeos::AudioDevice device; | 142 chromeos::AudioDevice device; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 } | 244 } |
| 251 | 245 |
| 252 void AudioServiceImpl::NotifyDevicesChanged() { | 246 void AudioServiceImpl::NotifyDevicesChanged() { |
| 253 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 247 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 254 DCHECK(cras_audio_handler_); | 248 DCHECK(cras_audio_handler_); |
| 255 | 249 |
| 256 DeviceInfoList devices_info_list; | 250 DeviceInfoList devices_info_list; |
| 257 chromeos::AudioDeviceList devices; | 251 chromeos::AudioDeviceList devices; |
| 258 cras_audio_handler_->GetAudioDevices(&devices); | 252 cras_audio_handler_->GetAudioDevices(&devices); |
| 259 for (size_t i = 0; i < devices.size(); ++i) { | 253 for (size_t i = 0; i < devices.size(); ++i) { |
| 260 linked_ptr<AudioDeviceInfo> info(new AudioDeviceInfo()); | 254 AudioDeviceInfo info; |
| 261 info->id = base::Uint64ToString(devices[i].id); | 255 info.id = base::Uint64ToString(devices[i].id); |
| 262 info->is_input = devices[i].is_input; | 256 info.is_input = devices[i].is_input; |
| 263 info->device_type = chromeos::AudioDevice::GetTypeString(devices[i].type); | 257 info.device_type = chromeos::AudioDevice::GetTypeString(devices[i].type); |
| 264 info->display_name = devices[i].display_name; | 258 info.display_name = devices[i].display_name; |
| 265 info->device_name = devices[i].device_name; | 259 info.device_name = devices[i].device_name; |
| 266 info->is_active = devices[i].active; | 260 info.is_active = devices[i].active; |
| 267 info->is_muted = | 261 info.is_muted = |
| 268 devices[i].is_input | 262 devices[i].is_input |
| 269 ? cras_audio_handler_->IsInputMutedForDevice(devices[i].id) | 263 ? cras_audio_handler_->IsInputMutedForDevice(devices[i].id) |
| 270 : cras_audio_handler_->IsOutputMutedForDevice(devices[i].id); | 264 : cras_audio_handler_->IsOutputMutedForDevice(devices[i].id); |
| 271 info->level = | 265 info.level = |
| 272 devices[i].is_input | 266 devices[i].is_input |
| 273 ? cras_audio_handler_->GetOutputVolumePercentForDevice( | 267 ? cras_audio_handler_->GetOutputVolumePercentForDevice( |
| 274 devices[i].id) | 268 devices[i].id) |
| 275 : cras_audio_handler_->GetInputGainPercentForDevice(devices[i].id); | 269 : cras_audio_handler_->GetInputGainPercentForDevice(devices[i].id); |
| 276 info->stable_device_id.reset( | 270 info.stable_device_id.reset( |
| 277 new std::string(base::Uint64ToString(devices[i].stable_device_id))); | 271 new std::string(base::Uint64ToString(devices[i].stable_device_id))); |
| 278 | 272 |
| 279 devices_info_list.push_back(info); | 273 devices_info_list.push_back(std::move(info)); |
| 280 } | 274 } |
| 281 | 275 |
| 282 FOR_EACH_OBSERVER(AudioService::Observer, observer_list_, | 276 FOR_EACH_OBSERVER(AudioService::Observer, observer_list_, |
| 283 OnDevicesChanged(devices_info_list)); | 277 OnDevicesChanged(devices_info_list)); |
| 284 | 278 |
| 285 // Notify DeviceChanged event for backward compatibility. | 279 // Notify DeviceChanged event for backward compatibility. |
| 286 // TODO(jennyz): remove this code when the old version of hotrod retires. | 280 // TODO(jennyz): remove this code when the old version of hotrod retires. |
| 287 NotifyDeviceChanged(); | 281 NotifyDeviceChanged(); |
| 288 } | 282 } |
| 289 | 283 |
| 290 AudioService* AudioService::CreateInstance() { | 284 AudioService* AudioService::CreateInstance() { |
| 291 return new AudioServiceImpl; | 285 return new AudioServiceImpl; |
| 292 } | 286 } |
| 293 | 287 |
| 294 } // namespace extensions | 288 } // namespace extensions |
| OLD | NEW |