Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / extensions / shell / browser / shell_audio_controller_chromeos.cc
blob19e34ce291df058a01a1c162a95fb91f9c10519b
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"
7 #include <algorithm>
9 #include "chromeos/audio/audio_device.h"
11 namespace extensions {
13 namespace {
15 // Returns a pointer to the device in |devices| with ID |node_id|, or NULL if it
16 // isn't present.
17 const chromeos::AudioDevice* GetDevice(const chromeos::AudioDeviceList& devices,
18 uint64_t node_id) {
19 for (chromeos::AudioDeviceList::const_iterator it = devices.begin();
20 it != devices.end(); ++it) {
21 if (it->id == node_id)
22 return &(*it);
24 return NULL;
27 } // namespace
29 ShellAudioController::ShellAudioController() {
30 chromeos::CrasAudioHandler::Get()->AddAudioObserver(this);
31 ActivateDevices();
34 ShellAudioController::~ShellAudioController() {
35 chromeos::CrasAudioHandler::Get()->RemoveAudioObserver(this);
38 void ShellAudioController::OnOutputNodeVolumeChanged(uint64_t /* node_id */,
39 int /* volume */) {
42 void ShellAudioController::OnOutputMuteChanged(bool /* mute_on */,
43 bool /* system_adjust */) {}
45 void ShellAudioController::OnInputNodeGainChanged(uint64_t /* node_id */,
46 int /* gain */) {
49 void ShellAudioController::OnInputMuteChanged(bool /* mute_on */) {
52 void ShellAudioController::OnAudioNodesChanged() {
53 VLOG(1) << "Audio nodes changed";
54 ActivateDevices();
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)
72 best_input = it->id;
73 else if (!it->is_input && !best_output)
74 best_output = it->id;
77 if (best_input && best_input != handler->GetPrimaryActiveInputNode()) {
78 const chromeos::AudioDevice* device = GetDevice(devices, best_input);
79 DCHECK(device);
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);
85 DCHECK(device);
86 VLOG(1) << "Activating output device: " << device->ToString();
87 handler->SwitchToDevice(*device, true);
91 } // namespace extensions