Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(379)

Side by Side Diff: chromeos/audio/cras_audio_handler.cc

Issue 14314002: Implement new audio handler which talks to the new audio dbus apis. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add comments in class header file and todo for cleaning up legacy code. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 "chromeos/audio/cras_audio_handler.h"
6
7 #include <algorithm>
8 #include <cmath>
9
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/logging.h"
13 #include "chromeos/audio/audio_pref_handler.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
15
16 using std::max;
17 using std::min;
18
19 namespace chromeos {
20
21 namespace {
22
23 // Default value for unmuting, as a percent in the range [0, 100].
24 // Used when sound is unmuted, but volume was less than kMuteThresholdPercent.
25 const int kDefaultUnmuteVolumePercent = 4;
26
27 // Volume value which should be considered as muted in range [0, 100].
28 const int kMuteThresholdPercent = 1;
29
30 static CrasAudioHandler* g_cras_audio_handler = NULL;
31
32 } // namespace
33
34 CrasAudioHandler::AudioObserver::AudioObserver() {
35 }
36
37 CrasAudioHandler::AudioObserver::~AudioObserver() {
38 }
39
40 void CrasAudioHandler::AudioObserver::OnOutputVolumeChanged() {
41 }
42
43 void CrasAudioHandler::AudioObserver::OnOutputMuteChanged() {
44 }
45
46 void CrasAudioHandler::AudioObserver::OnInputMuteChanged() {
47 }
48
49 void CrasAudioHandler::AudioObserver::OnAudioNodesChanged() {
50 }
51
52 void CrasAudioHandler::AudioObserver::OnActiveOutputNodeChanged() {
53 }
54
55 void CrasAudioHandler::AudioObserver::OnActiveInputNodeChanged() {
56 }
57
58 // static
59 void CrasAudioHandler::Initialize(
60 scoped_refptr<AudioPrefHandler> audio_pref_handler) {
61 CHECK(!g_cras_audio_handler);
62 g_cras_audio_handler = new CrasAudioHandler(audio_pref_handler);
63 }
64
65 // static
66 void CrasAudioHandler::Shutdown() {
67 CHECK(g_cras_audio_handler);
68 delete g_cras_audio_handler;
69 g_cras_audio_handler = NULL;
70 }
71
72 // static
73 bool CrasAudioHandler::IsInitialized() {
74 return g_cras_audio_handler != NULL;
75 }
76
77 // static
78 CrasAudioHandler* CrasAudioHandler::Get() {
79 CHECK(g_cras_audio_handler)
80 << "CrasAudioHandler::Get() called before Initialize().";
81 return g_cras_audio_handler;
82 }
83
84 void CrasAudioHandler::AddAudioObserver(AudioObserver* observer) {
85 observers_.AddObserver(observer);
86 }
87
88 void CrasAudioHandler::RemoveAudioObserver(AudioObserver* observer) {
89 observers_.RemoveObserver(observer);
90 }
91
92 bool CrasAudioHandler::IsOutputMuted() {
93 return output_mute_on_;
94 }
95
96 bool CrasAudioHandler::IsInputMuted() {
97 return input_mute_on_;
98 }
99
100 int CrasAudioHandler::GetOutputVolumePercent() {
101 return output_volume_;
102 }
103
104 uint64 CrasAudioHandler::GetActiveOutputNode() const {
105 return active_output_node_id_;
106 }
107
108 uint64 CrasAudioHandler::GetActiveInputNode() const {
109 return active_input_node_id_;
110 }
111
112 void CrasAudioHandler::SetOutputVolumePercent(int volume_percent) {
113 volume_percent = min(max(volume_percent, 0), 100);
114 if (volume_percent <= kMuteThresholdPercent)
115 volume_percent = 0;
116 if (IsOutputMuted() && volume_percent > 0)
117 SetOutputMute(false);
118 if (!IsOutputMuted() && volume_percent == 0)
119 SetOutputMute(true);
120 SetOutputVolumeInternal(volume_percent);
dgreid 2013/04/19 04:20:18 Should these be reversed? Is it possible that som
jennyz 2013/04/19 16:48:18 This clones the exact logic we used in AudioHandle
dgreid 2013/04/22 16:23:26 I think that is safer. It will most likely not ha
jennyz 2013/04/23 17:22:55 Will changed in the next cl.
121 }
122
123 void CrasAudioHandler::AdjustOutputVolumeByPercent(int adjust_by_percent) {
124 SetOutputVolumePercent(output_volume_ + adjust_by_percent);
125 }
126
127 void CrasAudioHandler::SetOutputMute(bool mute_on) {
128 if (output_mute_locked_) {
129 NOTREACHED() << "Output mute has been locked";
130 return;
131 }
132
133 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
134 SetOutputMute(mute_on);
135
136 if (!mute_on) {
137 if (output_volume_ <= kMuteThresholdPercent) {
dgreid 2013/04/19 04:20:18 If SetOutputVolumePercent(50) is called when curre
jennyz 2013/04/19 16:48:18 It could if the audio is muted when user sets it t
dgreid 2013/04/22 16:23:26 I was less worried about the user noticing and mor
jennyz 2013/04/23 17:22:55 Shouldn't happen after the change made in SetOutpu
138 // Avoid the situation when sound has been unmuted, but the volume
139 // is set to a very low value, so user still can't hear any sound.
140 SetOutputVolumeInternal(kDefaultUnmuteVolumePercent);
141 }
142 }
143 }
144
145 void CrasAudioHandler::SetInputMute(bool mute_on) {
146 if (input_mute_locked_) {
147 NOTREACHED() << "Input mute has been locked";
148 return;
149 }
150
151 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
152 SetInputMute(mute_on);
153 }
154
155 void CrasAudioHandler::SetActiveOutputNode(uint64 node_id) {
156 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
157 SetActiveOutputNode(node_id);
158 }
159
160 void CrasAudioHandler::SetActiveInputNode(uint64 node_id) {
161 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
162 SetActiveInputNode(node_id);
163 }
164
165 CrasAudioHandler::CrasAudioHandler(
166 scoped_refptr<AudioPrefHandler> audio_pref_handler)
167 : audio_pref_handler_(audio_pref_handler),
168 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
169 output_mute_on_(false),
170 input_mute_on_(false),
171 output_volume_(0),
172 active_output_node_id_(0),
173 active_input_node_id_(0),
174 output_mute_locked_(false),
175 input_mute_locked_(false) {
176 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->AddObserver(this);
177 DCHECK(audio_pref_handler_.get());
178 audio_pref_handler_->AddAudioPrefObserver(this);
179 SetupInitialAudioState();
180 }
181
182 CrasAudioHandler::~CrasAudioHandler() {
183 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
184 RemoveObserver(this);
185 audio_pref_handler_->RemoveAudioPrefObserver(this);
186 audio_pref_handler_ = NULL;
187 }
188
189 void CrasAudioHandler::AudioClientRestarted() {
190 SetupInitialAudioState();
191 }
192
193 void CrasAudioHandler::OutputVolumeChanged(int volume) {
194 if (output_volume_ == volume)
195 return;
196
197 output_volume_ = volume;
198 audio_pref_handler_->SetOutputVolumeValue(output_volume_);
199 FOR_EACH_OBSERVER(AudioObserver, observers_, OnOutputVolumeChanged());
200 }
201
202 void CrasAudioHandler::OutputMuteChanged(bool mute_on) {
203 if (output_mute_on_ == mute_on)
204 return;
205
206 output_mute_on_ = mute_on;
207 audio_pref_handler_->SetOutputMuteValue(mute_on);
208 FOR_EACH_OBSERVER(AudioObserver, observers_, OnOutputMuteChanged());
209 }
210
211 void CrasAudioHandler::InputMuteChanged(bool mute_on) {
212 if (input_mute_on_ == mute_on)
213 return;
214
215 input_mute_on_ = mute_on;
216 FOR_EACH_OBSERVER(AudioObserver, observers_, OnInputMuteChanged());
217 }
218
219 void CrasAudioHandler::NodesChanged() {
220 // Refresh audio nodes data.
221 GetNodes();
222 }
223
224 void CrasAudioHandler::ActiveOutputNodeChanged(uint64 node_id) {
225 active_output_node_id_ = node_id;
226 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveOutputNodeChanged());
227 }
228
229 void CrasAudioHandler::ActiveInputNodeChanged(uint64 node_id) {
230 active_input_node_id_ = node_id;
231 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveInputNodeChanged());
232 }
233
234 void CrasAudioHandler::OnAudioPolicyPrefChanged() {
235 ApplyAudioPolicy();
236 }
237
238 void CrasAudioHandler::SetupInitialAudioState() {
239 ApplyAudioPolicy();
240
241 // Set the initial audio state to the ones read from audio prefs.
242 output_mute_on_ = audio_pref_handler_->GetOutputMuteValue();
243 output_volume_ = audio_pref_handler_->GetOutputVolumeValue();
244 SetOutputMute(output_mute_on_);
245 SetOutputVolumeInternal(output_volume_);
dgreid 2013/04/19 04:20:18 similar comment to above about the order of this.
jennyz 2013/04/19 16:48:18 Same as we discussed in previous comment.
jennyz 2013/04/23 17:22:55 Changed in the next cl.
246
247 // Get the initial audio data.
248 GetNodes();
249 }
250
251 void CrasAudioHandler::ApplyAudioPolicy() {
252 output_mute_locked_ = false;
253 if (!audio_pref_handler_->GetAudioOutputAllowedValue()) {
254 SetOutputMute(true);
255 output_mute_locked_ = true;
256 }
257
258 input_mute_locked_ = false;
259 if (audio_pref_handler_->GetAudioCaptureAllowedValue()) {
260 SetInputMute(false);
261 } else {
262 SetInputMute(true);
263 input_mute_locked_ = true;
264 }
265 }
266
267 void CrasAudioHandler::SetOutputVolumeInternal(int volume) {
268 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
269 SetOutputVolume(volume);
270 }
271
272 void CrasAudioHandler::GetNodes() {
273 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->GetNodes(
274 base::Bind(&CrasAudioHandler::HandleGetNodes,
275 weak_ptr_factory_.GetWeakPtr()));
276 }
277
278 void CrasAudioHandler::HandleGetNodes(const chromeos::AudioNodeList& node_list,
279 bool success) {
280 if (!success) {
281 LOG(ERROR) << "Failed to retrieve audio nodes data";
282 return;
283 }
284
285 audio_nodes_.clear();
286 for (size_t i = 0; i < node_list.size(); ++i) {
287 if (node_list[i].is_input && node_list[i].active)
288 active_input_node_id_ = node_list[i].id;
289 else if (!node_list[i].is_input && node_list[i].active)
290 active_output_node_id_ = node_list[i].id;
291 audio_nodes_.push_back(node_list[i]);
292 }
293
294 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged());
295 }
296
297 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698