Convert env to a defaultdict in run_executable() to fix other callers of that function.
[chromium-blink-merge.git] / extensions / shell / browser / shell_audio_controller_chromeos.cc
blob312a3a571b8ecd9ba43eb3ddb0e4c50836f874fb
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 // Default output and input volume.
16 const double kOutputVolumePercent = 100.0;
17 const double kInputGainPercent = 100.0;
19 // Returns a pointer to the device in |devices| with ID |node_id|, or NULL if it
20 // isn't present.
21 const chromeos::AudioDevice* GetDevice(const chromeos::AudioDeviceList& devices,
22 uint64 node_id) {
23 for (chromeos::AudioDeviceList::const_iterator it = devices.begin();
24 it != devices.end(); ++it) {
25 if (it->id == node_id)
26 return &(*it);
28 return NULL;
31 } // namespace
33 ShellAudioController::PrefHandler::PrefHandler() {}
35 double ShellAudioController::PrefHandler::GetOutputVolumeValue(
36 const chromeos::AudioDevice* device) {
37 return kOutputVolumePercent;
40 double ShellAudioController::PrefHandler::GetInputGainValue(
41 const chromeos::AudioDevice* device) {
42 return kInputGainPercent;
45 void ShellAudioController::PrefHandler::SetVolumeGainValue(
46 const chromeos::AudioDevice& device,
47 double value) {
48 // TODO(derat): Shove volume and mute prefs into a map so we can at least
49 // honor changes that are made at runtime.
52 bool ShellAudioController::PrefHandler::GetMuteValue(
53 const chromeos::AudioDevice& device) {
54 return false;
57 void ShellAudioController::PrefHandler::SetMuteValue(
58 const chromeos::AudioDevice& device,
59 bool mute_on) {}
61 bool ShellAudioController::PrefHandler::GetAudioCaptureAllowedValue() {
62 return true;
65 bool ShellAudioController::PrefHandler::GetAudioOutputAllowedValue() {
66 return true;
69 void ShellAudioController::PrefHandler::AddAudioPrefObserver(
70 chromeos::AudioPrefObserver* observer) {}
72 void ShellAudioController::PrefHandler::RemoveAudioPrefObserver(
73 chromeos::AudioPrefObserver* observer) {}
75 ShellAudioController::PrefHandler::~PrefHandler() {}
77 ShellAudioController::ShellAudioController() {
78 chromeos::CrasAudioHandler::Get()->AddAudioObserver(this);
79 ActivateDevices();
82 ShellAudioController::~ShellAudioController() {
83 chromeos::CrasAudioHandler::Get()->RemoveAudioObserver(this);
86 void ShellAudioController::OnOutputVolumeChanged() {}
88 void ShellAudioController::OnOutputMuteChanged() {}
90 void ShellAudioController::OnInputGainChanged() {}
92 void ShellAudioController::OnInputMuteChanged() {}
94 void ShellAudioController::OnAudioNodesChanged() {
95 VLOG(1) << "Audio nodes changed";
96 ActivateDevices();
99 void ShellAudioController::OnActiveOutputNodeChanged() {}
101 void ShellAudioController::OnActiveInputNodeChanged() {}
103 void ShellAudioController::ActivateDevices() {
104 chromeos::CrasAudioHandler* handler = chromeos::CrasAudioHandler::Get();
105 chromeos::AudioDeviceList devices;
106 handler->GetAudioDevices(&devices);
107 sort(devices.begin(), devices.end(), chromeos::AudioDeviceCompare());
109 uint64 best_input = 0, best_output = 0;
110 for (chromeos::AudioDeviceList::const_reverse_iterator it = devices.rbegin();
111 it != devices.rend() && (!best_input || !best_output); ++it) {
112 // TODO(derat): Need to check |plugged_time|?
113 if (it->is_input && !best_input)
114 best_input = it->id;
115 else if (!it->is_input && !best_output)
116 best_output = it->id;
119 if (best_input && best_input != handler->GetPrimaryActiveInputNode()) {
120 const chromeos::AudioDevice* device = GetDevice(devices, best_input);
121 DCHECK(device);
122 VLOG(1) << "Activating input device: " << device->ToString();
123 handler->SwitchToDevice(*device);
125 if (best_output && best_output != handler->GetPrimaryActiveOutputNode()) {
126 const chromeos::AudioDevice* device = GetDevice(devices, best_output);
127 DCHECK(device);
128 VLOG(1) << "Activating output device: " << device->ToString();
129 handler->SwitchToDevice(*device);
133 } // namespace extensions