Upstream version 11.39.250.0
[platform/framework/web/crosswalk.git] / src / extensions / shell / browser / shell_audio_controller_chromeos.cc
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
11 namespace extensions {
12
13 namespace {
14
15 // Default output and input volume.
16 const double kOutputVolumePercent = 100.0;
17 const 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.
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);
27   }
28   return NULL;
29 }
30
31 }  // namespace
32
33 ShellAudioController::PrefHandler::PrefHandler() {}
34
35 double ShellAudioController::PrefHandler::GetOutputVolumeValue(
36     const chromeos::AudioDevice* device) {
37   return kOutputVolumePercent;
38 }
39
40 double ShellAudioController::PrefHandler::GetInputGainValue(
41     const chromeos::AudioDevice* device) {
42   return kInputGainPercent;
43 }
44
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.
50 }
51
52 bool ShellAudioController::PrefHandler::GetMuteValue(
53     const chromeos::AudioDevice& device) {
54   return false;
55 }
56
57 void ShellAudioController::PrefHandler::SetMuteValue(
58     const chromeos::AudioDevice& device,
59     bool mute_on) {}
60
61 bool ShellAudioController::PrefHandler::GetAudioCaptureAllowedValue() {
62   return true;
63 }
64
65 bool ShellAudioController::PrefHandler::GetAudioOutputAllowedValue() {
66   return true;
67 }
68
69 void ShellAudioController::PrefHandler::AddAudioPrefObserver(
70     chromeos::AudioPrefObserver* observer) {}
71
72 void ShellAudioController::PrefHandler::RemoveAudioPrefObserver(
73     chromeos::AudioPrefObserver* observer) {}
74
75 ShellAudioController::PrefHandler::~PrefHandler() {}
76
77 ShellAudioController::ShellAudioController() {
78   chromeos::CrasAudioHandler::Get()->AddAudioObserver(this);
79   ActivateDevices();
80 }
81
82 ShellAudioController::~ShellAudioController() {
83   chromeos::CrasAudioHandler::Get()->RemoveAudioObserver(this);
84 }
85
86 void ShellAudioController::OnOutputVolumeChanged() {}
87
88 void ShellAudioController::OnOutputMuteChanged() {}
89
90 void ShellAudioController::OnInputGainChanged() {}
91
92 void ShellAudioController::OnInputMuteChanged() {}
93
94 void ShellAudioController::OnAudioNodesChanged() {
95   VLOG(1) << "Audio nodes changed";
96   ActivateDevices();
97 }
98
99 void ShellAudioController::OnActiveOutputNodeChanged() {}
100
101 void ShellAudioController::OnActiveInputNodeChanged() {}
102
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());
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