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

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

Issue 2635983006: Final cleanup pass over audio device API (Closed)
Patch Set: rebase Created 3 years, 10 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
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"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
13 #include "base/stl_util.h"
13 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
14 #include "chromeos/audio/audio_device.h" 15 #include "chromeos/audio/audio_device.h"
15 #include "chromeos/audio/cras_audio_handler.h" 16 #include "chromeos/audio/cras_audio_handler.h"
16 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
17 18
18 using content::BrowserThread; 19 using content::BrowserThread;
19 20
20 namespace extensions { 21 namespace extensions {
21 22
22 using api::audio::OutputDeviceInfo; 23 using api::audio::OutputDeviceInfo;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 public: 71 public:
71 AudioServiceImpl(); 72 AudioServiceImpl();
72 ~AudioServiceImpl() override; 73 ~AudioServiceImpl() override;
73 74
74 // Called by listeners to this service to add/remove themselves as observers. 75 // Called by listeners to this service to add/remove themselves as observers.
75 void AddObserver(AudioService::Observer* observer) override; 76 void AddObserver(AudioService::Observer* observer) override;
76 void RemoveObserver(AudioService::Observer* observer) override; 77 void RemoveObserver(AudioService::Observer* observer) override;
77 78
78 // Start to query audio device information. 79 // Start to query audio device information.
79 bool GetInfo(OutputInfo* output_info_out, InputInfo* input_info_out) override; 80 bool GetInfo(OutputInfo* output_info_out, InputInfo* input_info_out) override;
81 bool GetDevices(const api::audio::DeviceFilter& filter,
82 DeviceInfoList* devices_out) override;
80 void SetActiveDevices(const DeviceIdList& device_list) override; 83 void SetActiveDevices(const DeviceIdList& device_list) override;
81 bool SetActiveDeviceLists( 84 bool SetActiveDeviceLists(
82 const std::unique_ptr<DeviceIdList>& input_devices, 85 const std::unique_ptr<DeviceIdList>& input_devices,
83 const std::unique_ptr<DeviceIdList>& output_devives) override; 86 const std::unique_ptr<DeviceIdList>& output_devives) override;
84 bool SetDeviceSoundLevel(const std::string& device_id, 87 bool SetDeviceSoundLevel(const std::string& device_id,
85 int volume, 88 int volume,
86 int gain) override; 89 int gain) override;
87 bool SetMuteForDevice(const std::string& device_id, bool value) override; 90 bool SetMuteForDevice(const std::string& device_id, bool value) override;
88 bool SetMute(bool is_input, bool value) override; 91 bool SetMute(bool is_input, bool value) override;
89 bool GetMute(bool is_input, bool* value) override; 92 bool GetMute(bool is_input, bool* value) override;
(...skipping 11 matching lines...) Expand all
101 private: 104 private:
102 void NotifyDeviceChanged(); 105 void NotifyDeviceChanged();
103 void NotifyLevelChanged(uint64_t id, int level); 106 void NotifyLevelChanged(uint64_t id, int level);
104 void NotifyMuteChanged(bool is_input, bool is_muted); 107 void NotifyMuteChanged(bool is_input, bool is_muted);
105 void NotifyDevicesChanged(); 108 void NotifyDevicesChanged();
106 109
107 uint64_t GetIdFromStr(const std::string& id_str); 110 uint64_t GetIdFromStr(const std::string& id_str);
108 bool GetAudioNodeIdList(const DeviceIdList& ids, 111 bool GetAudioNodeIdList(const DeviceIdList& ids,
109 bool is_input, 112 bool is_input,
110 chromeos::CrasAudioHandler::NodeIdList* node_ids); 113 chromeos::CrasAudioHandler::NodeIdList* node_ids);
114 AudioDeviceInfo ToAudioDeviceInfo(const chromeos::AudioDevice& device);
111 115
112 // List of observers. 116 // List of observers.
113 base::ObserverList<AudioService::Observer> observer_list_; 117 base::ObserverList<AudioService::Observer> observer_list_;
114 118
115 chromeos::CrasAudioHandler* cras_audio_handler_; 119 chromeos::CrasAudioHandler* cras_audio_handler_;
116 120
117 // Note: This should remain the last member so it'll be destroyed and 121 // Note: This should remain the last member so it'll be destroyed and
118 // invalidate the weak pointers before any other members are destroyed. 122 // invalidate the weak pointers before any other members are destroyed.
119 base::WeakPtrFactory<AudioServiceImpl> weak_ptr_factory_; 123 base::WeakPtrFactory<AudioServiceImpl> weak_ptr_factory_;
120 124
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 info.is_active = devices[i].active; 178 info.is_active = devices[i].active;
175 info.gain = 179 info.gain =
176 cras_audio_handler_->GetInputGainPercentForDevice(devices[i].id); 180 cras_audio_handler_->GetInputGainPercentForDevice(devices[i].id);
177 info.is_muted = cras_audio_handler_->IsInputMutedForDevice(devices[i].id); 181 info.is_muted = cras_audio_handler_->IsInputMutedForDevice(devices[i].id);
178 input_info_out->push_back(std::move(info)); 182 input_info_out->push_back(std::move(info));
179 } 183 }
180 } 184 }
181 return true; 185 return true;
182 } 186 }
183 187
188 bool AudioServiceImpl::GetDevices(const api::audio::DeviceFilter& filter,
189 DeviceInfoList* device_info_list_out) {
jennyz 2017/02/02 01:10:44 Please keep the argument name consistent: device_i
tbarzic 2017/02/02 01:24:41 Done.
190 if (!cras_audio_handler_)
191 return false;
192
193 chromeos::AudioDeviceList devices;
194 cras_audio_handler_->GetAudioDevices(&devices);
195
196 bool accept_input =
197 !filter.stream_types ||
198 base::ContainsValue(*filter.stream_types, api::audio::STREAM_TYPE_INPUT);
199 bool accept_output =
200 !filter.stream_types ||
201 base::ContainsValue(*filter.stream_types, api::audio::STREAM_TYPE_OUTPUT);
202
203 for (const auto& device : devices) {
204 if (filter.is_active && *filter.is_active != device.active)
205 continue;
206 if (device.is_input && !accept_input)
207 continue;
208 if (!device.is_input && !accept_output)
209 continue;
210 device_info_list_out->push_back(ToAudioDeviceInfo(device));
211 }
212
213 return true;
214 }
215
184 void AudioServiceImpl::SetActiveDevices(const DeviceIdList& device_list) { 216 void AudioServiceImpl::SetActiveDevices(const DeviceIdList& device_list) {
185 DCHECK(cras_audio_handler_); 217 DCHECK(cras_audio_handler_);
186 if (!cras_audio_handler_) 218 if (!cras_audio_handler_)
187 return; 219 return;
188 220
189 chromeos::CrasAudioHandler::NodeIdList id_list; 221 chromeos::CrasAudioHandler::NodeIdList id_list;
190 for (const auto& id : device_list) { 222 for (const auto& id : device_list) {
191 const chromeos::AudioDevice* device = 223 const chromeos::AudioDevice* device =
192 cras_audio_handler_->GetDeviceFromId(GetIdFromStr(id)); 224 cras_audio_handler_->GetDeviceFromId(GetIdFromStr(id));
193 if (device) 225 if (device)
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 cras_audio_handler_->GetDeviceFromId(GetIdFromStr(device_id)); 337 cras_audio_handler_->GetDeviceFromId(GetIdFromStr(device_id));
306 if (!device) 338 if (!device)
307 return false; 339 return false;
308 if (device->is_input != is_input) 340 if (device->is_input != is_input)
309 return false; 341 return false;
310 node_ids->push_back(device->id); 342 node_ids->push_back(device->id);
311 } 343 }
312 return true; 344 return true;
313 } 345 }
314 346
347 AudioDeviceInfo AudioServiceImpl::ToAudioDeviceInfo(
348 const chromeos::AudioDevice& device) {
349 AudioDeviceInfo info;
350 info.id = base::Uint64ToString(device.id);
351 info.stream_type = device.is_input
352 ? extensions::api::audio::STREAM_TYPE_INPUT
353 : extensions::api::audio::STREAM_TYPE_OUTPUT;
354 info.is_input = device.is_input;
355 info.device_type = GetAsAudioApiDeviceType(device.type);
356 info.display_name = device.display_name;
357 info.device_name = device.device_name;
358 info.is_active = device.active;
359 info.is_muted = device.is_input
360 ? cras_audio_handler_->IsInputMutedForDevice(device.id)
361 : cras_audio_handler_->IsOutputMutedForDevice(device.id);
362 info.level =
363 device.is_input
364 ? cras_audio_handler_->GetOutputVolumePercentForDevice(device.id)
365 : cras_audio_handler_->GetInputGainPercentForDevice(device.id);
366 info.stable_device_id.reset(
367 new std::string(base::Uint64ToString(device.stable_device_id)));
368
369 return info;
370 }
371
315 void AudioServiceImpl::OnOutputNodeVolumeChanged(uint64_t id, int volume) { 372 void AudioServiceImpl::OnOutputNodeVolumeChanged(uint64_t id, int volume) {
316 NotifyLevelChanged(id, volume); 373 NotifyLevelChanged(id, volume);
317 } 374 }
318 375
319 void AudioServiceImpl::OnOutputMuteChanged(bool mute_on, bool system_adjust) { 376 void AudioServiceImpl::OnOutputMuteChanged(bool mute_on, bool system_adjust) {
320 NotifyMuteChanged(false, mute_on); 377 NotifyMuteChanged(false, mute_on);
321 } 378 }
322 379
323 void AudioServiceImpl::OnInputNodeGainChanged(uint64_t id, int gain) { 380 void AudioServiceImpl::OnInputNodeGainChanged(uint64_t id, int gain) {
324 NotifyLevelChanged(id, gain); 381 NotifyLevelChanged(id, gain);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 for (auto& observer : observer_list_) 415 for (auto& observer : observer_list_)
359 observer.OnMuteChanged(is_input, is_muted); 416 observer.OnMuteChanged(is_input, is_muted);
360 417
361 // Notify DeviceChanged event for backward compatibility. 418 // Notify DeviceChanged event for backward compatibility.
362 // TODO(jennyz): remove this code when the old version of hotrod retires. 419 // TODO(jennyz): remove this code when the old version of hotrod retires.
363 NotifyDeviceChanged(); 420 NotifyDeviceChanged();
364 } 421 }
365 422
366 void AudioServiceImpl::NotifyDevicesChanged() { 423 void AudioServiceImpl::NotifyDevicesChanged() {
367 DCHECK_CURRENTLY_ON(BrowserThread::UI); 424 DCHECK_CURRENTLY_ON(BrowserThread::UI);
368 DCHECK(cras_audio_handler_);
369 425
370 DeviceInfoList devices_info_list;
371 chromeos::AudioDeviceList devices; 426 chromeos::AudioDeviceList devices;
372 cras_audio_handler_->GetAudioDevices(&devices); 427 cras_audio_handler_->GetAudioDevices(&devices);
373 for (size_t i = 0; i < devices.size(); ++i) {
374 AudioDeviceInfo info;
375 info.id = base::Uint64ToString(devices[i].id);
376 info.stream_type = devices[i].is_input
377 ? extensions::api::audio::STREAM_TYPE_INPUT
378 : extensions::api::audio::STREAM_TYPE_OUTPUT;
379 info.is_input = devices[i].is_input;
380 info.device_type = GetAsAudioApiDeviceType(devices[i].type);
381 info.display_name = devices[i].display_name;
382 info.device_name = devices[i].device_name;
383 info.is_active = devices[i].active;
384 info.is_muted =
385 devices[i].is_input
386 ? cras_audio_handler_->IsInputMutedForDevice(devices[i].id)
387 : cras_audio_handler_->IsOutputMutedForDevice(devices[i].id);
388 info.level =
389 devices[i].is_input
390 ? cras_audio_handler_->GetOutputVolumePercentForDevice(
391 devices[i].id)
392 : cras_audio_handler_->GetInputGainPercentForDevice(devices[i].id);
393 info.stable_device_id.reset(
394 new std::string(base::Uint64ToString(devices[i].stable_device_id)));
395 428
396 devices_info_list.push_back(std::move(info)); 429 DeviceInfoList device_info_list;
430 for (const auto& device : devices) {
431 device_info_list.push_back(ToAudioDeviceInfo(device));
397 } 432 }
398 433
399 for (auto& observer : observer_list_) 434 for (auto& observer : observer_list_)
400 observer.OnDevicesChanged(devices_info_list); 435 observer.OnDevicesChanged(device_info_list);
401 436
402 // Notify DeviceChanged event for backward compatibility. 437 // Notify DeviceChanged event for backward compatibility.
403 // TODO(jennyz): remove this code when the old version of hotrod retires. 438 // TODO(jennyz): remove this code when the old version of hotrod retires.
404 NotifyDeviceChanged(); 439 NotifyDeviceChanged();
405 } 440 }
406 441
407 AudioService* AudioService::CreateInstance() { 442 AudioService* AudioService::CreateInstance() {
408 return new AudioServiceImpl; 443 return new AudioServiceImpl;
409 } 444 }
410 445
411 } // namespace extensions 446 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698