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

Side by Side Diff: chrome/browser/chromeos/audio_mixer_alsa.cc

Issue 5859003: Add ALSA support to volume keys (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/chrome/browser/chromeos
Patch Set: cleaning Created 9 years, 11 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
« no previous file with comments | « chrome/browser/chromeos/audio_mixer_alsa.h ('k') | chrome/browser/chromeos/audio_mixer_pulse.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 "chrome/browser/chromeos/audio_mixer_alsa.h"
6
7 #include <alsa/asoundlib.h>
8
9 #include "base/logging.h"
10 #include "base/task.h"
11
12 namespace chromeos {
13
14 // Connect to the ALSA mixer using their simple element API. Init is performed
15 // asynchronously on the worker thread.
16 //
17 // To get a wider range and finer control over volume levels, first the Master
18 // level is set, then if the PCM element exists, the total level is refined by
19 // adjusting that as well. If the PCM element has more volume steps, it allows
20 // for finer granularity in the total volume.
21
22 // TODO(davej): Serialize volume/mute to preserve settings when restarting.
23
24 typedef long alsa_long_t; // 'long' is required for ALSA API calls.
25
26 namespace {
27
28 const char* kMasterVolume = "Master";
29 const char* kPCMVolume = "PCM";
30 const double kDefaultMinVolume = -90.0;
31 const double kDefaultMaxVolume = 0.0;
32
33 } // namespace
34
35 AudioMixerAlsa::AudioMixerAlsa()
36 : min_volume_(kDefaultMinVolume),
37 max_volume_(kDefaultMaxVolume),
38 save_volume_(0),
39 mixer_state_(UNINITIALIZED),
40 alsa_mixer_(NULL),
41 elem_master_(NULL),
42 elem_pcm_(NULL) {
43 }
44
45 AudioMixerAlsa::~AudioMixerAlsa() {
46 FreeAlsaMixer();
47 if (thread_ != NULL) {
48 thread_->Stop();
49 thread_.reset();
50 }
51 }
52
53 bool AudioMixerAlsa::Init(InitDoneCallback* callback) {
54 if (!InitThread())
55 return false;
56
57 // Post the task of starting up, which can block for 200-500ms,
58 // so best not to do it on the caller's thread.
59 thread_->message_loop()->PostTask(FROM_HERE,
60 NewRunnableMethod(this, &AudioMixerAlsa::DoInit, callback));
61 return true;
62 }
63
64 bool AudioMixerAlsa::InitSync() {
65 if (!InitThread())
66 return false;
67 return InitializeAlsaMixer();
68 }
69
70 double AudioMixerAlsa::GetVolumeDb() const {
71 AutoLock lock(mixer_state_lock_);
72 if (mixer_state_ != READY)
davejcool 2011/01/05 21:31:07 Changing to locking for the duration of the call.
73 return kSilenceDb;
74
75 double vol_total = 0.0;
76 GetElementVolume(elem_master_, &vol_total);
77
78 double vol_pcm = 0.0;
79 if (elem_pcm_ && (GetElementVolume(elem_pcm_, &vol_pcm)))
80 vol_total += vol_pcm;
81
82 return vol_total;
83 }
84
85 bool AudioMixerAlsa::GetVolumeLimits(double* vol_min, double* vol_max) {
86 AutoLock lock(mixer_state_lock_);
87 if (mixer_state_ != READY)
88 return false;
89 if (vol_min)
90 *vol_min = min_volume_;
91 if (vol_max)
92 *vol_max = max_volume_;
93 return true;
94 }
95
96 void AudioMixerAlsa::SetVolumeDb(double vol_db) {
97 AutoLock lock(mixer_state_lock_);
98 if (mixer_state_ != READY)
99 return;
100 double actual_vol = 0.0;
101
102 // If a PCM volume slider exists, then first set the Master volume to the
103 // nearest volume >= requested volume, then adjust PCM volume down to get
104 // closer to the requested volume.
105
106 if (elem_pcm_) {
107 SetElementVolume(elem_master_, vol_db, &actual_vol, 0.9999f);
108 SetElementVolume(elem_pcm_, vol_db - actual_vol, NULL, 0.5f);
109 } else {
110 SetElementVolume(elem_master_, vol_db, NULL, 0.5f);
111 }
112 }
113
114 bool AudioMixerAlsa::IsMute() const {
115 AutoLock lock(mixer_state_lock_);
116 if (mixer_state_ != READY)
117 return false;
118 return GetElementMuted(elem_master_);
119 }
120
121 void AudioMixerAlsa::SetMute(bool mute) {
122 AutoLock lock(mixer_state_lock_);
123 if (mixer_state_ != READY)
124 return;
125
126 // Set volume to minimum on mute, since switching the element off does not
127 // always mute as it should.
128
129 // TODO(davej): Setting volume to minimum can be removed once switching the
130 // element off can be guaranteed to work.
131
132 bool old_value = GetElementMuted(elem_master_);
133
134 if (old_value != mute) {
135 if (mute) {
136 save_volume_ = GetVolumeDb();
137 SetVolumeDb(min_volume_);
138 } else {
139 SetVolumeDb(save_volume_);
140 }
141 }
142
143 SetElementMuted(elem_master_, mute);
144 if (elem_pcm_)
145 SetElementMuted(elem_pcm_, mute);
146 }
147
148 AudioMixer::State AudioMixerAlsa::CheckState() const {
149 AutoLock lock(mixer_state_lock_);
150 // If we think it's ready, verify it is actually so.
151 if ((mixer_state_ == READY) && (alsa_mixer_ == NULL))
152 mixer_state_ = IN_ERROR;
153 return mixer_state_;
154 }
155
156 ////////////////////////////////////////////////////////////////////////////////
157 // Private functions follow
158
159 void AudioMixerAlsa::DoInit(InitDoneCallback* callback) {
160 bool success = InitializeAlsaMixer();
161
162 if (callback) {
163 callback->Run(success);
164 delete callback;
165 }
166 }
167
168 bool AudioMixerAlsa::InitThread() {
169 AutoLock lock(mixer_state_lock_);
170
171 if (mixer_state_ != UNINITIALIZED)
172 return false;
173
174 if (thread_ == NULL) {
175 thread_.reset(new base::Thread("AudioMixerAlsa"));
176 if (!thread_->Start()) {
177 thread_.reset();
178 return false;
179 }
180 }
181
182 mixer_state_ = INITIALIZING;
183 return true;
184 }
185
186 bool AudioMixerAlsa::InitializeAlsaMixer() {
187 AutoLock lock(mixer_state_lock_);
188 if (mixer_state_ != INITIALIZING)
189 return false;
190
191 int err;
192 snd_mixer_t* handle = NULL;
193 const char* card = "default";
194
195 if ((err = snd_mixer_open(&handle, 0)) < 0) {
196 LOG(ERROR) << "ALSA mixer " << card << " open error: " << snd_strerror(err);
197 return false;
198 }
199
200 if ((err = snd_mixer_attach(handle, card)) < 0) {
201 LOG(ERROR) << "ALSA Attach to card " << card << " failed: "
202 << snd_strerror(err);
203 snd_mixer_close(handle);
204 return false;
205 }
206
207 if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
208 LOG(ERROR) << "ALSA mixer register error: " << snd_strerror(err);
209 snd_mixer_close(handle);
210 return false;
211 }
212
213 if ((err = snd_mixer_load(handle)) < 0) {
214 LOG(ERROR) << "ALSA mixer " << card << " load error: %s"
215 << snd_strerror(err);
216 snd_mixer_close(handle);
217 return false;
218 }
219
220 VLOG(1) << "Opened ALSA mixer " << card << " OK";
221
222 elem_master_ = FindElementWithName(handle, kMasterVolume);
223 if (elem_master_) {
224 alsa_long_t long_lo, long_hi;
225 snd_mixer_selem_get_playback_dB_range(elem_master_, &long_lo, &long_hi);
226 min_volume_ = static_cast<double>(long_lo) / 100.0;
227 max_volume_ = static_cast<double>(long_hi) / 100.0;
228 } else {
229 LOG(ERROR) << "Cannot find 'Master' ALSA mixer element on " << card;
230 snd_mixer_close(handle);
231 return false;
232 }
233
234 elem_pcm_ = FindElementWithName(handle, kPCMVolume);
235 if (elem_pcm_) {
236 alsa_long_t long_lo, long_hi;
237 snd_mixer_selem_get_playback_dB_range(elem_pcm_, &long_lo, &long_hi);
238 min_volume_ += static_cast<double>(long_lo) / 100.0;
239 max_volume_ += static_cast<double>(long_hi) / 100.0;
240 }
241
242 VLOG(1) << "ALSA volume range is " << min_volume_ << " dB to "
243 << max_volume_ << " dB";
244
245 alsa_mixer_ = handle;
246 mixer_state_ = READY;
247 return true;
248 }
249
250 void AudioMixerAlsa::FreeAlsaMixer() {
251 AutoLock lock(mixer_state_lock_);
252 mixer_state_ = SHUTTING_DOWN;
253 if (alsa_mixer_) {
254 snd_mixer_close(alsa_mixer_);
255 alsa_mixer_ = NULL;
256 }
257 }
258
259 snd_mixer_elem_t* AudioMixerAlsa::FindElementWithName(
260 snd_mixer_t* handle,
261 const char* element_name) const {
262 snd_mixer_selem_id_t* sid;
263 snd_mixer_selem_id_alloca(&sid);
264 snd_mixer_selem_id_set_index(sid, 0);
265 snd_mixer_selem_id_set_name(sid, element_name);
266 snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid);
267 if (!elem) {
268 LOG(ERROR) << "ALSA unable to find simple control "
269 << snd_mixer_selem_id_get_name(sid);
270 }
271 return elem;
272 }
273
274 bool AudioMixerAlsa::GetElementVolume(snd_mixer_elem_t* elem,
275 double* current_vol) const {
276 alsa_long_t long_vol = 0;
277 snd_mixer_selem_get_playback_dB(elem,
278 static_cast<snd_mixer_selem_channel_id_t>(0),
279 &long_vol);
280 *current_vol = static_cast<double>(long_vol) / 100.0;
281
282 return true;
283 }
284
285 bool AudioMixerAlsa::SetElementVolume(snd_mixer_elem_t* elem,
286 double new_vol,
287 double* actual_vol,
288 double rounding_bias) {
289 alsa_long_t vol_lo = 0;
290 alsa_long_t vol_hi = 0;
291 snd_mixer_selem_get_playback_volume_range(elem, &vol_lo, &vol_hi);
292 alsa_long_t vol_range = vol_hi - vol_lo;
293 if (vol_range <= 0)
294 return false;
295
296 alsa_long_t db_lo_int = 0;
297 alsa_long_t db_hi_int = 0;
298 snd_mixer_selem_get_playback_dB_range(elem, &db_lo_int, &db_hi_int);
299 double db_lo = static_cast<double>(db_lo_int) / 100.0;
300 double db_hi = static_cast<double>(db_hi_int) / 100.0;
301 double db_step = static_cast<double>(db_hi - db_lo) / vol_range;
302 if (db_step <= 0.0)
303 return false;
304
305 if (new_vol < db_lo)
306 new_vol = db_lo;
307
308 alsa_long_t value = static_cast<alsa_long_t>(rounding_bias +
309 (new_vol - db_lo) / db_step) + vol_lo;
310 snd_mixer_selem_set_playback_volume_all(elem, value);
311
312 VLOG(1) << "Set volume " << snd_mixer_selem_get_name(elem)
313 << " to " << new_vol << " ==> " << (value - vol_lo) * db_step + db_lo
314 << " dB";
315
316 if (actual_vol) {
317 alsa_long_t volume;
318 snd_mixer_selem_get_playback_volume(
319 elem,
320 static_cast<snd_mixer_selem_channel_id_t>(0),
321 &volume);
322 *actual_vol = db_lo + (volume - vol_lo) * db_step;
323
324 VLOG(1) << "Actual volume " << snd_mixer_selem_get_name(elem)
325 << " now " << *actual_vol << " dB";
326 }
327 return true;
328 }
329
330 bool AudioMixerAlsa::GetElementMuted(snd_mixer_elem_t* elem) const {
331 int enabled;
332 snd_mixer_selem_get_playback_switch(
333 elem,
334 static_cast<snd_mixer_selem_channel_id_t>(0),
335 &enabled);
336 return (enabled) ? false : true;
337 }
338
339 void AudioMixerAlsa::SetElementMuted(snd_mixer_elem_t* elem, bool mute) {
340 int enabled = mute ? 0 : 1;
341 snd_mixer_selem_set_playback_switch_all(elem, enabled);
342
343 VLOG(1) << "Set playback switch " << snd_mixer_selem_get_name(elem)
344 << " to " << enabled;
345 }
346
347 } // namespace chromeos
348
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/audio_mixer_alsa.h ('k') | chrome/browser/chromeos/audio_mixer_pulse.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698