| 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" | |
| 10 #include "chromeos/audio/audio_device.h" | |
| 11 #include "chromeos/audio/cras_audio_handler.h" | |
| 12 #include "chromeos/dbus/audio_node.h" | 9 #include "chromeos/dbus/audio_node.h" |
| 13 #include "chromeos/dbus/cras_audio_client.h" | 10 #include "chromeos/dbus/cras_audio_client.h" |
| 14 #include "chromeos/dbus/dbus_thread_manager.h" | 11 #include "chromeos/dbus/dbus_thread_manager.h" |
| 15 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 16 | 13 |
| 17 using content::BrowserThread; | 14 using content::BrowserThread; |
| 18 | 15 |
| 19 namespace extensions { | 16 namespace extensions { |
| 20 | 17 |
| 21 using api::audio::OutputDeviceInfo; | 18 using api::audio::OutputDeviceInfo; |
| 22 using api::audio::InputDeviceInfo; | 19 using api::audio::InputDeviceInfo; |
| 23 | 20 |
| 24 class AudioServiceImpl : public AudioService, | 21 class AudioServiceImpl : public AudioService, |
| 25 public chromeos::CrasAudioHandler::Observer { | 22 public chromeos::CrasAudioClient::Observer { |
| 26 public: | 23 public: |
| 27 AudioServiceImpl(); | 24 AudioServiceImpl(); |
| 28 virtual ~AudioServiceImpl(); | 25 virtual ~AudioServiceImpl(); |
| 29 | 26 |
| 30 // Called by listeners to this service to add/remove themselves as observers. | 27 // Called by listeners to this service to add/remove themselves as observers. |
| 31 virtual void AddObserver(AudioService::Observer* observer) OVERRIDE; | 28 virtual void AddObserver(AudioService::Observer* observer); |
| 32 virtual void RemoveObserver(AudioService::Observer* observer) OVERRIDE; | 29 virtual void RemoveObserver(AudioService::Observer* observer); |
| 33 | 30 |
| 34 // Start to query audio device information. | 31 // Start to query audio device information. |
| 35 virtual void StartGetInfo(const GetInfoCallback& callback) OVERRIDE; | 32 virtual void StartGetInfo(const GetInfoCallback& callback); |
| 36 virtual void SetActiveDevices(const DeviceIdList& device_list) OVERRIDE; | |
| 37 virtual bool SetDeviceProperties(const std::string& device_id, | |
| 38 bool muted, | |
| 39 int volume, | |
| 40 int gain) OVERRIDE; | |
| 41 | 33 |
| 42 protected: | 34 protected: |
| 43 // chromeos::CrasAudioClient::Observer overrides. | 35 // chromeos::CrasAudioClient::Observer overrides. |
| 44 virtual void OutputVolumeChanged(int volume) OVERRIDE; | 36 virtual void OutputVolumeChanged(int volume) OVERRIDE; |
| 45 virtual void OutputMuteChanged(bool mute_on) OVERRIDE; | 37 virtual void OutputMuteChanged(bool mute_on) OVERRIDE; |
| 46 virtual void InputGainChanged(int gain) OVERRIDE; | 38 virtual void InputGainChanged(int gain) OVERRIDE; |
| 47 virtual void InputMuteChanged(bool mute_on) OVERRIDE; | 39 virtual void InputMuteChanged(bool mute_on) OVERRIDE; |
| 48 virtual void NodesChanged() OVERRIDE; | 40 virtual void NodesChanged() OVERRIDE; |
| 49 virtual void ActiveOutputNodeChanged(uint64 node_id) OVERRIDE; | 41 virtual void ActiveOutputNodeChanged(uint64 node_id) OVERRIDE; |
| 50 virtual void ActiveInputNodeChanged(uint64 node_id) OVERRIDE; | 42 virtual void ActiveInputNodeChanged(uint64 node_id) OVERRIDE; |
| 51 | 43 |
| 52 private: | 44 private: |
| 53 void NotifyDeviceChanged(); | 45 void NotifyDeviceChanged(); |
| 54 | 46 |
| 55 // Callback for CrasAudioClient::GetNodes(). | 47 // Callback for CrasAudioClient::GetNodes(). |
| 56 void OnGetNodes(const GetInfoCallback& callback, | 48 void OnGetNodes(const GetInfoCallback& callback, |
| 57 const chromeos::AudioNodeList& audio_nodes, | 49 const chromeos::AudioNodeList& audio_nodes, |
| 58 bool success); | 50 bool success); |
| 59 | 51 |
| 60 bool FindDevice(uint64 id, chromeos::AudioDevice* device); | |
| 61 uint64 GetIdFromStr(const std::string& id_str); | |
| 62 | |
| 63 // List of observers. | 52 // List of observers. |
| 64 ObserverList<AudioService::Observer> observer_list_; | 53 ObserverList<AudioService::Observer> observer_list_; |
| 65 | 54 |
| 66 chromeos::CrasAudioClient* cras_audio_client_; | 55 chromeos::CrasAudioClient* cras_audio_client_; |
| 67 chromeos::CrasAudioHandler* cras_audio_handler_; | |
| 68 | 56 |
| 69 // Note: This should remain the last member so it'll be destroyed and | 57 // Note: This should remain the last member so it'll be destroyed and |
| 70 // invalidate the weak pointers before any other members are destroyed. | 58 // invalidate the weak pointers before any other members are destroyed. |
| 71 base::WeakPtrFactory<AudioServiceImpl> weak_ptr_factory_; | 59 base::WeakPtrFactory<AudioServiceImpl> weak_ptr_factory_; |
| 72 | 60 |
| 73 DISALLOW_COPY_AND_ASSIGN(AudioServiceImpl); | 61 DISALLOW_COPY_AND_ASSIGN(AudioServiceImpl); |
| 74 }; | 62 }; |
| 75 | 63 |
| 76 AudioServiceImpl::AudioServiceImpl() | 64 AudioServiceImpl::AudioServiceImpl() |
| 77 : cras_audio_client_(NULL), | 65 : cras_audio_client_(NULL), |
| 78 cras_audio_handler_(NULL), | |
| 79 weak_ptr_factory_(this) { | 66 weak_ptr_factory_(this) { |
| 80 if (chromeos::DBusThreadManager::IsInitialized() && | 67 if (chromeos::DBusThreadManager::IsInitialized() && |
| 81 chromeos::DBusThreadManager::Get()) { | 68 chromeos::DBusThreadManager::Get()) { |
| 82 cras_audio_client_ = | 69 cras_audio_client_ = |
| 83 chromeos::DBusThreadManager::Get()->GetCrasAudioClient(); | 70 chromeos::DBusThreadManager::Get()->GetCrasAudioClient(); |
| 84 if (cras_audio_client_) | 71 if (cras_audio_client_) |
| 85 cras_audio_client_->AddObserver(this); | 72 cras_audio_client_->AddObserver(this); |
| 86 if (chromeos::CrasAudioHandler::IsInitialized()) | |
| 87 cras_audio_handler_ = chromeos::CrasAudioHandler::Get(); | |
| 88 } | 73 } |
| 89 } | 74 } |
| 90 | 75 |
| 91 AudioServiceImpl::~AudioServiceImpl() { | 76 AudioServiceImpl::~AudioServiceImpl() { |
| 92 if (cras_audio_client_) | 77 if (cras_audio_client_) |
| 93 cras_audio_client_->RemoveObserver(this); | 78 cras_audio_client_->RemoveObserver(this); |
| 94 } | 79 } |
| 95 | 80 |
| 96 void AudioServiceImpl::AddObserver(AudioService::Observer* observer) { | 81 void AudioServiceImpl::AddObserver(AudioService::Observer* observer) { |
| 97 observer_list_.AddObserver(observer); | 82 observer_list_.AddObserver(observer); |
| 98 } | 83 } |
| 99 | 84 |
| 100 void AudioServiceImpl::RemoveObserver(AudioService::Observer* observer) { | 85 void AudioServiceImpl::RemoveObserver(AudioService::Observer* observer) { |
| 101 observer_list_.RemoveObserver(observer); | 86 observer_list_.RemoveObserver(observer); |
| 102 } | 87 } |
| 103 | 88 |
| 104 void AudioServiceImpl::StartGetInfo(const GetInfoCallback& callback) { | 89 void AudioServiceImpl::StartGetInfo(const GetInfoCallback& callback) { |
| 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 106 DCHECK(cras_audio_client_); | 91 DCHECK(cras_audio_client_); |
| 107 if (cras_audio_client_) | 92 if (cras_audio_client_) |
| 108 cras_audio_client_->GetNodes(base::Bind(&AudioServiceImpl::OnGetNodes, | 93 cras_audio_client_->GetNodes(base::Bind(&AudioServiceImpl::OnGetNodes, |
| 109 weak_ptr_factory_.GetWeakPtr(), | 94 weak_ptr_factory_.GetWeakPtr(), |
| 110 callback)); | 95 callback)); |
| 111 } | 96 } |
| 112 | 97 |
| 113 void AudioServiceImpl::SetActiveDevices(const DeviceIdList& device_list) { | |
| 114 DCHECK(cras_audio_handler_); | |
| 115 if (!cras_audio_handler_) | |
| 116 return; | |
| 117 | |
| 118 bool input_device_set = false; | |
| 119 bool output_device_set = false; | |
| 120 | |
| 121 for (size_t i = 0; i < device_list.size(); ++i) { | |
| 122 chromeos::AudioDevice device; | |
| 123 bool found = FindDevice(GetIdFromStr(device_list[i]), &device); | |
| 124 if (found) { | |
| 125 if (device.is_input && !input_device_set) { | |
| 126 cras_audio_handler_->SetActiveInputNode(device.id); | |
| 127 input_device_set = true; | |
| 128 } else if (!device.is_input && !output_device_set) { | |
| 129 cras_audio_handler_->SetActiveOutputNode(device.id); | |
| 130 output_device_set = true; | |
| 131 } | |
| 132 } | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 bool AudioServiceImpl::SetDeviceProperties(const std::string& device_id, | |
| 137 bool muted, | |
| 138 int volume, | |
| 139 int gain) { | |
| 140 DCHECK(cras_audio_handler_); | |
| 141 if (!cras_audio_handler_) | |
| 142 return false; | |
| 143 | |
| 144 chromeos::AudioDevice device; | |
| 145 bool found = FindDevice(GetIdFromStr(device_id), &device); | |
| 146 if (!found) | |
| 147 return false; | |
| 148 | |
| 149 if (!device.is_input && volume != -1) { | |
| 150 cras_audio_handler_->SetVolumeGainPercentForDevice(GetIdFromStr(device_id), | |
| 151 volume); | |
| 152 return true; | |
| 153 } else if (device.is_input && gain != -1) { | |
| 154 cras_audio_handler_->SetVolumeGainPercentForDevice(GetIdFromStr(device_id), | |
| 155 gain); | |
| 156 return true; | |
| 157 } | |
| 158 | |
| 159 return false; | |
| 160 } | |
| 161 | |
| 162 void AudioServiceImpl::OnGetNodes(const GetInfoCallback& callback, | 98 void AudioServiceImpl::OnGetNodes(const GetInfoCallback& callback, |
| 163 const chromeos::AudioNodeList& audio_nodes, | 99 const chromeos::AudioNodeList& audio_nodes, |
| 164 bool success) { | 100 bool success) { |
| 165 OutputInfo output_info; | 101 OutputInfo output_info; |
| 166 InputInfo input_info; | 102 InputInfo input_info; |
| 167 if (success) { | 103 if (success) { |
| 168 for (chromeos::AudioNodeList::const_iterator iter = audio_nodes.begin(); | 104 for (chromeos::AudioNodeList::const_iterator iter = audio_nodes.begin(); |
| 169 iter != audio_nodes.end(); ++iter) { | 105 iter != audio_nodes.end(); ++iter) { |
| 170 if (!iter->is_input) { | 106 if (!iter->is_input) { |
| 171 linked_ptr<OutputDeviceInfo> info(new OutputDeviceInfo()); | 107 linked_ptr<OutputDeviceInfo> info(new OutputDeviceInfo()); |
| 172 info->id = base::Uint64ToString(iter->id); | 108 info->id = iter->id; |
| 173 info->name = iter->name; | 109 info->name = iter->name; |
| 174 info->is_active = iter->active; | 110 info->is_active = iter->active; |
| 175 info->volume = cras_audio_handler_->GetOutputVolumePercentForDevice( | |
| 176 iter->id); | |
| 177 output_info.push_back(info); | 111 output_info.push_back(info); |
| 178 } else { | 112 } else { |
| 179 linked_ptr<InputDeviceInfo> info(new InputDeviceInfo()); | 113 linked_ptr<InputDeviceInfo> info(new InputDeviceInfo()); |
| 180 info->id = base::Uint64ToString(iter->id); | 114 info->id = iter->id; |
| 181 info->name = iter->name; | 115 info->name = iter->name; |
| 182 info->is_active = iter->active; | 116 info->is_active = iter->active; |
| 183 info->gain = cras_audio_handler_->GetInputGainPercentForDevice( | |
| 184 iter->id); | |
| 185 input_info.push_back(info); | 117 input_info.push_back(info); |
| 186 } | 118 } |
| 187 } | 119 } |
| 188 } | 120 } |
| 189 | 121 |
| 190 DCHECK(!callback.is_null()); | 122 DCHECK(!callback.is_null()); |
| 191 if (!callback.is_null()) | 123 if (!callback.is_null()) |
| 192 callback.Run(output_info, input_info, success); | 124 callback.Run(output_info, input_info, success); |
| 193 } | 125 } |
| 194 | 126 |
| 195 bool AudioServiceImpl::FindDevice(uint64 id, chromeos::AudioDevice* device) { | |
| 196 chromeos::AudioDeviceList devices; | |
| 197 cras_audio_handler_->GetAudioDevices(&devices); | |
| 198 | |
| 199 for (size_t i = 0; i < devices.size(); ++i) { | |
| 200 if (devices[i].id == id) { | |
| 201 *device = devices[i]; | |
| 202 return true; | |
| 203 } | |
| 204 } | |
| 205 return false; | |
| 206 } | |
| 207 | |
| 208 uint64 AudioServiceImpl::GetIdFromStr(const std::string& id_str) { | |
| 209 uint64 device_id; | |
| 210 if (!base::StringToUint64(id_str, &device_id)) | |
| 211 return 0; | |
| 212 else | |
| 213 return device_id; | |
| 214 } | |
| 215 | |
| 216 void AudioServiceImpl::OutputVolumeChanged(int volume) { | 127 void AudioServiceImpl::OutputVolumeChanged(int volume) { |
| 217 NotifyDeviceChanged(); | 128 NotifyDeviceChanged(); |
| 218 } | 129 } |
| 219 | 130 |
| 220 void AudioServiceImpl::OutputMuteChanged(bool mute_on) { | 131 void AudioServiceImpl::OutputMuteChanged(bool mute_on) { |
| 221 NotifyDeviceChanged(); | 132 NotifyDeviceChanged(); |
| 222 } | 133 } |
| 223 | 134 |
| 224 void AudioServiceImpl::InputGainChanged(int gain) { | 135 void AudioServiceImpl::InputGainChanged(int gain) { |
| 225 NotifyDeviceChanged(); | 136 NotifyDeviceChanged(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 243 | 154 |
| 244 void AudioServiceImpl::NotifyDeviceChanged() { | 155 void AudioServiceImpl::NotifyDeviceChanged() { |
| 245 FOR_EACH_OBSERVER(AudioService::Observer, observer_list_, OnDeviceChanged()); | 156 FOR_EACH_OBSERVER(AudioService::Observer, observer_list_, OnDeviceChanged()); |
| 246 } | 157 } |
| 247 | 158 |
| 248 AudioService* AudioService::CreateInstance() { | 159 AudioService* AudioService::CreateInstance() { |
| 249 return new AudioServiceImpl; | 160 return new AudioServiceImpl; |
| 250 } | 161 } |
| 251 | 162 |
| 252 } // namespace extensions | 163 } // namespace extensions |
| OLD | NEW |