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::OnOutputVolumeChanged() {}
40 void ShellAudioController::OnOutputMuteChanged() {}
42 void ShellAudioController::OnInputGainChanged() {}
44 void ShellAudioController::OnInputMuteChanged() {}
46 void ShellAudioController::OnAudioNodesChanged() {
47 VLOG(1) << "Audio nodes changed";
51 void ShellAudioController::OnActiveOutputNodeChanged() {}
53 void ShellAudioController::OnActiveInputNodeChanged() {}
55 void ShellAudioController::ActivateDevices() {
56 chromeos::CrasAudioHandler
* handler
= chromeos::CrasAudioHandler::Get();
57 chromeos::AudioDeviceList devices
;
58 handler
->GetAudioDevices(&devices
);
59 sort(devices
.begin(), devices
.end(), chromeos::AudioDeviceCompare());
61 uint64 best_input
= 0, best_output
= 0;
62 for (chromeos::AudioDeviceList::const_reverse_iterator it
= devices
.rbegin();
63 it
!= devices
.rend() && (!best_input
|| !best_output
); ++it
) {
64 // TODO(derat): Need to check |plugged_time|?
65 if (it
->is_input
&& !best_input
)
67 else if (!it
->is_input
&& !best_output
)
71 if (best_input
&& best_input
!= handler
->GetPrimaryActiveInputNode()) {
72 const chromeos::AudioDevice
* device
= GetDevice(devices
, best_input
);
74 VLOG(1) << "Activating input device: " << device
->ToString();
75 handler
->SwitchToDevice(*device
, true);
77 if (best_output
&& best_output
!= handler
->GetPrimaryActiveOutputNode()) {
78 const chromeos::AudioDevice
* device
= GetDevice(devices
, best_output
);
80 VLOG(1) << "Activating output device: " << device
->ToString();
81 handler
->SwitchToDevice(*device
, true);
85 } // namespace extensions