Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1084)

Side by Side Diff: extensions/browser/api/audio/audio_service_chromeos.cc

Issue 2605983002: Simplify logic behind chrome.audio.setActiveDevices (Closed)
Patch Set: . Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/browser/api/audio/audio_service.cc ('k') | extensions/common/api/audio.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 30 matching lines...) Expand all
41 AudioServiceImpl(); 41 AudioServiceImpl();
42 ~AudioServiceImpl() override; 42 ~AudioServiceImpl() override;
43 43
44 // Called by listeners to this service to add/remove themselves as observers. 44 // Called by listeners to this service to add/remove themselves as observers.
45 void AddObserver(AudioService::Observer* observer) override; 45 void AddObserver(AudioService::Observer* observer) override;
46 void RemoveObserver(AudioService::Observer* observer) override; 46 void RemoveObserver(AudioService::Observer* observer) override;
47 47
48 // Start to query audio device information. 48 // Start to query audio device information.
49 bool GetInfo(OutputInfo* output_info_out, InputInfo* input_info_out) override; 49 bool GetInfo(OutputInfo* output_info_out, InputInfo* input_info_out) override;
50 void SetActiveDevices(const DeviceIdList& device_list) override; 50 void SetActiveDevices(const DeviceIdList& device_list) override;
51 bool SetActiveDeviceLists(
52 const std::unique_ptr<DeviceIdList>& input_devices,
53 const std::unique_ptr<DeviceIdList>& output_devives) override;
51 bool SetDeviceProperties(const std::string& device_id, 54 bool SetDeviceProperties(const std::string& device_id,
52 bool muted, 55 bool muted,
53 int volume, 56 int volume,
54 int gain) override; 57 int gain) override;
55 58
56 protected: 59 protected:
57 // chromeos::CrasAudioHandler::AudioObserver overrides. 60 // chromeos::CrasAudioHandler::AudioObserver overrides.
58 void OnOutputNodeVolumeChanged(uint64_t id, int volume) override; 61 void OnOutputNodeVolumeChanged(uint64_t id, int volume) override;
59 void OnInputNodeGainChanged(uint64_t id, int gain) override; 62 void OnInputNodeGainChanged(uint64_t id, int gain) override;
60 void OnOutputMuteChanged(bool mute_on, bool system_adjust) override; 63 void OnOutputMuteChanged(bool mute_on, bool system_adjust) override;
61 void OnInputMuteChanged(bool mute_on) override; 64 void OnInputMuteChanged(bool mute_on) override;
62 void OnAudioNodesChanged() override; 65 void OnAudioNodesChanged() override;
63 void OnActiveOutputNodeChanged() override; 66 void OnActiveOutputNodeChanged() override;
64 void OnActiveInputNodeChanged() override; 67 void OnActiveInputNodeChanged() override;
65 68
66 private: 69 private:
67 void NotifyDeviceChanged(); 70 void NotifyDeviceChanged();
68 void NotifyLevelChanged(uint64_t id, int level); 71 void NotifyLevelChanged(uint64_t id, int level);
69 void NotifyMuteChanged(bool is_input, bool is_muted); 72 void NotifyMuteChanged(bool is_input, bool is_muted);
70 void NotifyDevicesChanged(); 73 void NotifyDevicesChanged();
71 74
72 bool FindDevice(uint64_t id, chromeos::AudioDevice* device);
73 uint64_t GetIdFromStr(const std::string& id_str); 75 uint64_t GetIdFromStr(const std::string& id_str);
76 bool GetAudioNodeIdList(const DeviceIdList& ids,
77 bool is_input,
78 chromeos::CrasAudioHandler::NodeIdList* node_ids);
74 79
75 // List of observers. 80 // List of observers.
76 base::ObserverList<AudioService::Observer> observer_list_; 81 base::ObserverList<AudioService::Observer> observer_list_;
77 82
78 chromeos::CrasAudioHandler* cras_audio_handler_; 83 chromeos::CrasAudioHandler* cras_audio_handler_;
79 84
80 // Note: This should remain the last member so it'll be destroyed and 85 // Note: This should remain the last member so it'll be destroyed and
81 // invalidate the weak pointers before any other members are destroyed. 86 // invalidate the weak pointers before any other members are destroyed.
82 base::WeakPtrFactory<AudioServiceImpl> weak_ptr_factory_; 87 base::WeakPtrFactory<AudioServiceImpl> weak_ptr_factory_;
83 88
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 } 148 }
144 return true; 149 return true;
145 } 150 }
146 151
147 void AudioServiceImpl::SetActiveDevices(const DeviceIdList& device_list) { 152 void AudioServiceImpl::SetActiveDevices(const DeviceIdList& device_list) {
148 DCHECK(cras_audio_handler_); 153 DCHECK(cras_audio_handler_);
149 if (!cras_audio_handler_) 154 if (!cras_audio_handler_)
150 return; 155 return;
151 156
152 chromeos::CrasAudioHandler::NodeIdList id_list; 157 chromeos::CrasAudioHandler::NodeIdList id_list;
153 for (size_t i = 0; i < device_list.size(); ++i) { 158 for (const auto& id : device_list) {
154 chromeos::AudioDevice device; 159 const chromeos::AudioDevice* device =
155 if (FindDevice(GetIdFromStr(device_list[i]), &device)) 160 cras_audio_handler_->GetDeviceFromId(GetIdFromStr(id));
156 id_list.push_back(device.id); 161 if (device)
162 id_list.push_back(device->id);
157 } 163 }
158 cras_audio_handler_->ChangeActiveNodes(id_list); 164 cras_audio_handler_->ChangeActiveNodes(id_list);
159 } 165 }
160 166
167 bool AudioServiceImpl::SetActiveDeviceLists(
168 const std::unique_ptr<DeviceIdList>& input_ids,
169 const std::unique_ptr<DeviceIdList>& output_ids) {
170 DCHECK(cras_audio_handler_);
171 if (!cras_audio_handler_)
172 return false;
173
174 chromeos::CrasAudioHandler::NodeIdList input_nodes;
175 if (input_ids.get() && !GetAudioNodeIdList(*input_ids, true, &input_nodes))
176 return false;
177
178 chromeos::CrasAudioHandler::NodeIdList output_nodes;
179 if (output_ids.get() &&
180 !GetAudioNodeIdList(*output_ids, false, &output_nodes)) {
181 return false;
182 }
183
184 bool success = true;
185 if (output_ids.get()) {
186 success = cras_audio_handler_->SetActiveOutputNodes(output_nodes);
187 DCHECK(success);
188 }
189
190 if (input_ids.get()) {
191 success = success && cras_audio_handler_->SetActiveInputNodes(input_nodes);
192 DCHECK(success);
193 }
194 return success;
195 }
196
161 bool AudioServiceImpl::SetDeviceProperties(const std::string& device_id, 197 bool AudioServiceImpl::SetDeviceProperties(const std::string& device_id,
162 bool muted, 198 bool muted,
163 int volume, 199 int volume,
164 int gain) { 200 int gain) {
165 DCHECK(cras_audio_handler_); 201 DCHECK(cras_audio_handler_);
166 if (!cras_audio_handler_) 202 if (!cras_audio_handler_)
167 return false; 203 return false;
168 204
169 chromeos::AudioDevice device; 205 const chromeos::AudioDevice* device =
170 bool found = FindDevice(GetIdFromStr(device_id), &device); 206 cras_audio_handler_->GetDeviceFromId(GetIdFromStr(device_id));
171 if (!found) 207 if (!device)
172 return false; 208 return false;
173 209
174 cras_audio_handler_->SetMuteForDevice(device.id, muted); 210 cras_audio_handler_->SetMuteForDevice(device->id, muted);
175 211
176 if (!device.is_input && volume != -1) { 212 if (!device->is_input && volume != -1) {
177 cras_audio_handler_->SetVolumeGainPercentForDevice(device.id, volume); 213 cras_audio_handler_->SetVolumeGainPercentForDevice(device->id, volume);
178 return true; 214 return true;
179 } else if (device.is_input && gain != -1) { 215 } else if (device->is_input && gain != -1) {
180 cras_audio_handler_->SetVolumeGainPercentForDevice(device.id, gain); 216 cras_audio_handler_->SetVolumeGainPercentForDevice(device->id, gain);
181 return true; 217 return true;
182 } 218 }
183 219
184 return false; 220 return false;
185 } 221 }
186 222
187 bool AudioServiceImpl::FindDevice(uint64_t id, chromeos::AudioDevice* device) {
188 chromeos::AudioDeviceList devices;
189 cras_audio_handler_->GetAudioDevices(&devices);
190
191 for (size_t i = 0; i < devices.size(); ++i) {
192 if (devices[i].id == id) {
193 *device = devices[i];
194 return true;
195 }
196 }
197 return false;
198 }
199
200 uint64_t AudioServiceImpl::GetIdFromStr(const std::string& id_str) { 223 uint64_t AudioServiceImpl::GetIdFromStr(const std::string& id_str) {
201 uint64_t device_id; 224 uint64_t device_id;
202 if (!base::StringToUint64(id_str, &device_id)) 225 if (!base::StringToUint64(id_str, &device_id))
203 return 0; 226 return 0;
204 else 227 else
205 return device_id; 228 return device_id;
206 } 229 }
207 230
231 bool AudioServiceImpl::GetAudioNodeIdList(
232 const DeviceIdList& ids,
233 bool is_input,
234 chromeos::CrasAudioHandler::NodeIdList* node_ids) {
235 for (const auto& device_id : ids) {
236 const chromeos::AudioDevice* device =
237 cras_audio_handler_->GetDeviceFromId(GetIdFromStr(device_id));
238 if (!device)
239 return false;
240 if (device->is_input != is_input)
241 return false;
242 node_ids->push_back(device->id);
243 }
244 return true;
245 }
246
208 void AudioServiceImpl::OnOutputNodeVolumeChanged(uint64_t id, int volume) { 247 void AudioServiceImpl::OnOutputNodeVolumeChanged(uint64_t id, int volume) {
209 NotifyLevelChanged(id, volume); 248 NotifyLevelChanged(id, volume);
210 } 249 }
211 250
212 void AudioServiceImpl::OnOutputMuteChanged(bool mute_on, bool system_adjust) { 251 void AudioServiceImpl::OnOutputMuteChanged(bool mute_on, bool system_adjust) {
213 NotifyMuteChanged(false, mute_on); 252 NotifyMuteChanged(false, mute_on);
214 } 253 }
215 254
216 void AudioServiceImpl::OnInputNodeGainChanged(uint64_t id, int gain) { 255 void AudioServiceImpl::OnInputNodeGainChanged(uint64_t id, int gain) {
217 NotifyLevelChanged(id, gain); 256 NotifyLevelChanged(id, gain);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 // Notify DeviceChanged event for backward compatibility. 331 // Notify DeviceChanged event for backward compatibility.
293 // TODO(jennyz): remove this code when the old version of hotrod retires. 332 // TODO(jennyz): remove this code when the old version of hotrod retires.
294 NotifyDeviceChanged(); 333 NotifyDeviceChanged();
295 } 334 }
296 335
297 AudioService* AudioService::CreateInstance() { 336 AudioService* AudioService::CreateInstance() {
298 return new AudioServiceImpl; 337 return new AudioServiceImpl;
299 } 338 }
300 339
301 } // namespace extensions 340 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/audio/audio_service.cc ('k') | extensions/common/api/audio.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698