1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/shell/browser/shell_audio_controller_chromeos.h"
9 #include "chromeos/audio/audio_device.h"
11 namespace extensions
{
15 // Returns a pointer to the device in |devices| with ID |node_id|, or NULL if it
17 const chromeos::AudioDevice
* GetDevice(const chromeos::AudioDeviceList
& devices
,
19 for (chromeos::AudioDeviceList::const_iterator it
= devices
.begin();
20 it
!= devices
.end(); ++it
) {
21 if (it
->id
== node_id
)
29 ShellAudioController::ShellAudioController() {
30 chromeos::CrasAudioHandler::Get()->AddAudioObserver(this);
34 ShellAudioController::~ShellAudioController() {
35 chromeos::CrasAudioHandler::Get()->RemoveAudioObserver(this);
38 void ShellAudioController::OnOutputNodeVolumeChanged(uint64_t /* node_id */,
42 void ShellAudioController::OnOutputMuteChanged(bool /* mute_on */) {
45 void ShellAudioController::OnInputNodeGainChanged(uint64_t /* node_id */,
49 void ShellAudioController::OnInputMuteChanged(bool /* mute_on */) {
52 void ShellAudioController::OnAudioNodesChanged() {
53 VLOG(1) << "Audio nodes changed";
57 void ShellAudioController::OnActiveOutputNodeChanged() {}
59 void ShellAudioController::OnActiveInputNodeChanged() {}
61 void ShellAudioController::ActivateDevices() {
62 chromeos::CrasAudioHandler
* handler
= chromeos::CrasAudioHandler::Get();
63 chromeos::AudioDeviceList devices
;
64 handler
->GetAudioDevices(&devices
);
65 sort(devices
.begin(), devices
.end(), chromeos::AudioDeviceCompare());
67 uint64_t best_input
= 0, best_output
= 0;
68 for (chromeos::AudioDeviceList::const_reverse_iterator it
= devices
.rbegin();
69 it
!= devices
.rend() && (!best_input
|| !best_output
); ++it
) {
70 // TODO(derat): Need to check |plugged_time|?
71 if (it
->is_input
&& !best_input
)
73 else if (!it
->is_input
&& !best_output
)
77 if (best_input
&& best_input
!= handler
->GetPrimaryActiveInputNode()) {
78 const chromeos::AudioDevice
* device
= GetDevice(devices
, best_input
);
80 VLOG(1) << "Activating input device: " << device
->ToString();
81 handler
->SwitchToDevice(*device
, true);
83 if (best_output
&& best_output
!= handler
->GetPrimaryActiveOutputNode()) {
84 const chromeos::AudioDevice
* device
= GetDevice(devices
, best_output
);
86 VLOG(1) << "Activating output device: " << device
->ToString();
87 handler
->SwitchToDevice(*device
, true);
91 } // namespace extensions