shell_audio_controller_chromeos.cc revision 29b820f8d84e3bc97d62552e54923c42407f2f29
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.
4
5#include "extensions/shell/browser/shell_audio_controller_chromeos.h"
6
7#include <algorithm>
8
9#include "chromeos/audio/audio_device.h"
10
11namespace extensions {
12
13namespace {
14
15// Default output and input volume.
16const double kOutputVolumePercent = 100.0;
17const double kInputGainPercent = 100.0;
18
19// Returns a pointer to the device in |devices| with ID |node_id|, or NULL if it
20// isn't present.
21const 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);
27  }
28  return NULL;
29}
30
31}  // namespace
32
33ShellAudioController::PrefHandler::PrefHandler() {}
34
35double ShellAudioController::PrefHandler::GetOutputVolumeValue(
36    const chromeos::AudioDevice* device) {
37  return kOutputVolumePercent;
38}
39
40double ShellAudioController::PrefHandler::GetInputGainValue(
41    const chromeos::AudioDevice* device) {
42  return kInputGainPercent;
43}
44
45void 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.
50}
51
52bool ShellAudioController::PrefHandler::GetMuteValue(
53    const chromeos::AudioDevice& device) {
54  return false;
55}
56
57void ShellAudioController::PrefHandler::SetMuteValue(
58    const chromeos::AudioDevice& device,
59    bool mute_on) {}
60
61bool ShellAudioController::PrefHandler::GetAudioCaptureAllowedValue() {
62  return true;
63}
64
65bool ShellAudioController::PrefHandler::GetAudioOutputAllowedValue() {
66  return true;
67}
68
69void ShellAudioController::PrefHandler::AddAudioPrefObserver(
70    chromeos::AudioPrefObserver* observer) {}
71
72void ShellAudioController::PrefHandler::RemoveAudioPrefObserver(
73    chromeos::AudioPrefObserver* observer) {}
74
75ShellAudioController::PrefHandler::~PrefHandler() {}
76
77ShellAudioController::ShellAudioController() {
78  chromeos::CrasAudioHandler::Get()->AddAudioObserver(this);
79  ActivateDevices();
80}
81
82ShellAudioController::~ShellAudioController() {
83  chromeos::CrasAudioHandler::Get()->RemoveAudioObserver(this);
84}
85
86void ShellAudioController::OnOutputVolumeChanged() {}
87
88void ShellAudioController::OnOutputMuteChanged() {}
89
90void ShellAudioController::OnInputGainChanged() {}
91
92void ShellAudioController::OnInputMuteChanged() {}
93
94void ShellAudioController::OnAudioNodesChanged() {
95  VLOG(1) << "Audio nodes changed";
96  ActivateDevices();
97}
98
99void ShellAudioController::OnActiveOutputNodeChanged() {}
100
101void ShellAudioController::OnActiveInputNodeChanged() {}
102
103void 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());
108
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;
117  }
118
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, true);
124  }
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, true);
130  }
131}
132
133}  // namespace extensions
134