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

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

Issue 6118006: Save/Restore ALSA volume/mute (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/chrome/browser/chromeos
Patch Set: responded to comments 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/common/pref_names.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/audio_mixer_alsa.h" 5 #include "chrome/browser/chromeos/audio_mixer_alsa.h"
6 6
7 #include <alsa/asoundlib.h> 7 #include <alsa/asoundlib.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/task.h" 10 #include "base/task.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/browser_thread.h"
13 #include "chrome/browser/prefs/pref_service.h"
14 #include "chrome/common/pref_names.h"
11 15
12 namespace chromeos { 16 namespace chromeos {
13 17
14 // Connect to the ALSA mixer using their simple element API. Init is performed 18 // Connect to the ALSA mixer using their simple element API. Init is performed
15 // asynchronously on the worker thread. 19 // asynchronously on the worker thread.
16 // 20 //
17 // To get a wider range and finer control over volume levels, first the Master 21 // 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 22 // 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 23 // adjusting that as well. If the PCM element has more volume steps, it allows
20 // for finer granularity in the total volume. 24 // for finer granularity in the total volume.
21 25
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. 26 typedef long alsa_long_t; // 'long' is required for ALSA API calls.
25 27
26 namespace { 28 namespace {
27 29
28 const char* kMasterVolume = "Master"; 30 const char* kMasterVolume = "Master";
29 const char* kPCMVolume = "PCM"; 31 const char* kPCMVolume = "PCM";
30 const double kDefaultMinVolume = -90.0; 32 const double kDefaultMinVolume = -90.0;
31 const double kDefaultMaxVolume = 0.0; 33 const double kDefaultMaxVolume = 0.0;
34 const double kPrefVolumeNotSet = AudioMixer::kSilenceDb - 100.0;
32 35
33 } // namespace 36 } // namespace
34 37
35 AudioMixerAlsa::AudioMixerAlsa() 38 AudioMixerAlsa::AudioMixerAlsa()
36 : min_volume_(kDefaultMinVolume), 39 : min_volume_(kDefaultMinVolume),
37 max_volume_(kDefaultMaxVolume), 40 max_volume_(kDefaultMaxVolume),
38 save_volume_(0), 41 save_volume_(0),
39 mixer_state_(UNINITIALIZED), 42 mixer_state_(UNINITIALIZED),
40 alsa_mixer_(NULL), 43 alsa_mixer_(NULL),
41 elem_master_(NULL), 44 elem_master_(NULL),
42 elem_pcm_(NULL) { 45 elem_pcm_(NULL) {
43 } 46 }
44 47
45 AudioMixerAlsa::~AudioMixerAlsa() { 48 AudioMixerAlsa::~AudioMixerAlsa() {
46 FreeAlsaMixer(); 49 FreeAlsaMixer();
47 if (thread_ != NULL) { 50 if (thread_ != NULL) {
48 thread_->Stop(); 51 thread_->Stop();
49 thread_.reset(); 52 thread_.reset();
50 } 53 }
51 } 54 }
52 55
53 void AudioMixerAlsa::Init(InitDoneCallback* callback) { 56 void AudioMixerAlsa::Init(InitDoneCallback* callback) {
54 DCHECK(callback); 57 DCHECK(callback);
55 if (!InitThread()) { 58 if (!InitThread()) {
56 callback->Run(false); 59 callback->Run(false);
57 delete callback; 60 delete callback;
58 return; 61 return;
59 } 62 }
63 InitPrefs();
60 64
61 // Post the task of starting up, which can block for 200-500ms, 65 // Post the task of starting up, which can block for 200-500ms,
62 // so best not to do it on the caller's thread. 66 // so best not to do it on the caller's thread.
63 thread_->message_loop()->PostTask(FROM_HERE, 67 thread_->message_loop()->PostTask(FROM_HERE,
64 NewRunnableMethod(this, &AudioMixerAlsa::DoInit, callback)); 68 NewRunnableMethod(this, &AudioMixerAlsa::DoInit, callback));
65 } 69 }
66 70
67 bool AudioMixerAlsa::InitSync() { 71 bool AudioMixerAlsa::InitSync() {
68 if (!InitThread()) 72 if (!InitThread())
69 return false; 73 return false;
74 InitPrefs();
70 return InitializeAlsaMixer(); 75 return InitializeAlsaMixer();
71 } 76 }
72 77
73 double AudioMixerAlsa::GetVolumeDb() const { 78 double AudioMixerAlsa::GetVolumeDb() const {
74 AutoLock lock(mixer_state_lock_); 79 AutoLock lock(mixer_state_lock_);
75 if (mixer_state_ != READY) 80 if (mixer_state_ != READY)
76 return kSilenceDb; 81 return kSilenceDb;
77 82
78 return DoGetVolumeDb_Locked(); 83 return DoGetVolumeDb_Locked();
79 } 84 }
80 85
81 bool AudioMixerAlsa::GetVolumeLimits(double* vol_min, double* vol_max) { 86 bool AudioMixerAlsa::GetVolumeLimits(double* vol_min, double* vol_max) {
82 AutoLock lock(mixer_state_lock_); 87 AutoLock lock(mixer_state_lock_);
83 if (mixer_state_ != READY) 88 if (mixer_state_ != READY)
84 return false; 89 return false;
85 if (vol_min) 90 if (vol_min)
86 *vol_min = min_volume_; 91 *vol_min = min_volume_;
87 if (vol_max) 92 if (vol_max)
88 *vol_max = max_volume_; 93 *vol_max = max_volume_;
89 return true; 94 return true;
90 } 95 }
91 96
92 void AudioMixerAlsa::SetVolumeDb(double vol_db) { 97 void AudioMixerAlsa::SetVolumeDb(double vol_db) {
93 AutoLock lock(mixer_state_lock_); 98 AutoLock lock(mixer_state_lock_);
94 if (mixer_state_ != READY) 99 if (mixer_state_ != READY)
95 return; 100 return;
101 if (vol_db < kSilenceDb)
102 vol_db = kSilenceDb;
96 DoSetVolumeDb_Locked(vol_db); 103 DoSetVolumeDb_Locked(vol_db);
104 volume_pref_.SetValue(vol_db);
97 } 105 }
98 106
99 bool AudioMixerAlsa::IsMute() const { 107 bool AudioMixerAlsa::IsMute() const {
100 AutoLock lock(mixer_state_lock_); 108 AutoLock lock(mixer_state_lock_);
101 if (mixer_state_ != READY) 109 if (mixer_state_ != READY)
102 return false; 110 return false;
103 return GetElementMuted_Locked(elem_master_); 111 return GetElementMuted_Locked(elem_master_);
104 } 112 }
105 113
106 void AudioMixerAlsa::SetMute(bool mute) { 114 void AudioMixerAlsa::SetMute(bool mute) {
107 AutoLock lock(mixer_state_lock_); 115 AutoLock lock(mixer_state_lock_);
108 if (mixer_state_ != READY) 116 if (mixer_state_ != READY)
109 return; 117 return;
110 118
111 // Set volume to minimum on mute, since switching the element off does not 119 // Set volume to minimum on mute, since switching the element off does not
112 // always mute as it should. 120 // always mute as it should.
113 121
114 // TODO(davej): Setting volume to minimum can be removed once switching the 122 // TODO(davej): Remove save_volume_ and setting volume to minimum if
115 // element off can be guaranteed to work. 123 // switching the element off can be guaranteed to mute it.
scherkus (not reviewing) 2011/01/14 00:54:49 nit: no need to align comment here
116 124
117 bool old_value = GetElementMuted_Locked(elem_master_); 125 bool old_value = GetElementMuted_Locked(elem_master_);
118 126
119 if (old_value != mute) { 127 if (old_value != mute) {
120 if (mute) { 128 if (mute) {
121 save_volume_ = DoGetVolumeDb_Locked(); 129 save_volume_ = DoGetVolumeDb_Locked();
122 DoSetVolumeDb_Locked(min_volume_); 130 DoSetVolumeDb_Locked(min_volume_);
123 } else { 131 } else {
124 DoSetVolumeDb_Locked(save_volume_); 132 DoSetVolumeDb_Locked(save_volume_);
125 } 133 }
126 } 134 }
127 135
128 SetElementMuted_Locked(elem_master_, mute); 136 SetElementMuted_Locked(elem_master_, mute);
129 if (elem_pcm_) 137 if (elem_pcm_)
130 SetElementMuted_Locked(elem_pcm_, mute); 138 SetElementMuted_Locked(elem_pcm_, mute);
139 mute_pref_.SetValue(mute);
131 } 140 }
132 141
133 AudioMixer::State AudioMixerAlsa::GetState() const { 142 AudioMixer::State AudioMixerAlsa::GetState() const {
134 AutoLock lock(mixer_state_lock_); 143 AutoLock lock(mixer_state_lock_);
135 // If we think it's ready, verify it is actually so. 144 // If we think it's ready, verify it is actually so.
136 if ((mixer_state_ == READY) && (alsa_mixer_ == NULL)) 145 if ((mixer_state_ == READY) && (alsa_mixer_ == NULL))
137 mixer_state_ = IN_ERROR; 146 mixer_state_ = IN_ERROR;
138 return mixer_state_; 147 return mixer_state_;
139 } 148 }
140 149
141 //////////////////////////////////////////////////////////////////////////////// 150 ////////////////////////////////////////////////////////////////////////////////
142 // Private functions follow 151 // Private functions follow
143 152
144 void AudioMixerAlsa::DoInit(InitDoneCallback* callback) { 153 void AudioMixerAlsa::DoInit(InitDoneCallback* callback) {
145 bool success = InitializeAlsaMixer(); 154 bool success = InitializeAlsaMixer();
146 155
156 if (success) {
157 BrowserThread::PostTask(
158 BrowserThread::UI, FROM_HERE,
159 NewRunnableMethod(this, &AudioMixerAlsa::RestoreVolumeMuteOnUIThread));
160 }
161
147 if (callback) { 162 if (callback) {
148 callback->Run(success); 163 callback->Run(success);
149 delete callback; 164 delete callback;
150 } 165 }
151 } 166 }
152 167
153 bool AudioMixerAlsa::InitThread() { 168 bool AudioMixerAlsa::InitThread() {
154 AutoLock lock(mixer_state_lock_); 169 AutoLock lock(mixer_state_lock_);
155 170
156 if (mixer_state_ != UNINITIALIZED) 171 if (mixer_state_ != UNINITIALIZED)
157 return false; 172 return false;
158 173
159 if (thread_ == NULL) { 174 if (thread_ == NULL) {
160 thread_.reset(new base::Thread("AudioMixerAlsa")); 175 thread_.reset(new base::Thread("AudioMixerAlsa"));
161 if (!thread_->Start()) { 176 if (!thread_->Start()) {
162 thread_.reset(); 177 thread_.reset();
163 return false; 178 return false;
164 } 179 }
165 } 180 }
166 181
167 mixer_state_ = INITIALIZING; 182 mixer_state_ = INITIALIZING;
168 return true; 183 return true;
169 } 184 }
170 185
186 void AudioMixerAlsa::InitPrefs() {
scherkus (not reviewing) 2011/01/14 00:54:49 might be helpful to add DCHECKs to methods that mu
davejcool 2011/01/14 02:23:47 The ALSA calls do not need to be called on the UI
187 DCHECK(g_browser_process);
188 PrefService* prefs = g_browser_process->local_state();
189 DCHECK(prefs);
190
191 if (!prefs->FindPreference(prefs::kAudioVolume)) {
192 prefs->RegisterRealPref(prefs::kAudioVolume, kPrefVolumeNotSet);
scherkus (not reviewing) 2011/01/14 00:54:49 sanity check: do you know whether there are other
davejcool 2011/01/14 02:23:47 It looked like the preferences in chromeos::Prefer
193 prefs->RegisterBooleanPref(prefs::kAudioMute, false);
194 }
195 volume_pref_.Init(prefs::kAudioVolume, prefs, NULL);
196 mute_pref_.Init(prefs::kAudioMute, prefs, NULL);
197 }
198
171 bool AudioMixerAlsa::InitializeAlsaMixer() { 199 bool AudioMixerAlsa::InitializeAlsaMixer() {
172 AutoLock lock(mixer_state_lock_); 200 AutoLock lock(mixer_state_lock_);
173 if (mixer_state_ != INITIALIZING) 201 if (mixer_state_ != INITIALIZING)
174 return false; 202 return false;
175 203
176 int err; 204 int err;
177 snd_mixer_t* handle = NULL; 205 snd_mixer_t* handle = NULL;
178 const char* card = "default"; 206 const char* card = "default";
179 207
180 if ((err = snd_mixer_open(&handle, 0)) < 0) { 208 if ((err = snd_mixer_open(&handle, 0)) < 0) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 262
235 void AudioMixerAlsa::FreeAlsaMixer() { 263 void AudioMixerAlsa::FreeAlsaMixer() {
236 AutoLock lock(mixer_state_lock_); 264 AutoLock lock(mixer_state_lock_);
237 mixer_state_ = SHUTTING_DOWN; 265 mixer_state_ = SHUTTING_DOWN;
238 if (alsa_mixer_) { 266 if (alsa_mixer_) {
239 snd_mixer_close(alsa_mixer_); 267 snd_mixer_close(alsa_mixer_);
240 alsa_mixer_ = NULL; 268 alsa_mixer_ = NULL;
241 } 269 }
242 } 270 }
243 271
272 void AudioMixerAlsa::DoSetVolumeMute(double volume, bool mute) {
scherkus (not reviewing) 2011/01/14 00:54:49 hmm I only see one callee.. perhaps inline into Re
davejcool 2011/01/14 02:23:47 Originally I had it calling DoSetVolumeMute on the
273 AutoLock lock(mixer_state_lock_);
274 if (mixer_state_ != READY)
275 return;
276
277 VLOG(1) << "Setting volume to " << volume << "and mute to " << mute;
278 if (mute) {
279 save_volume_ = volume;
280 DoSetVolumeDb_Locked(min_volume_);
281 } else {
282 DoSetVolumeDb_Locked(volume);
283 }
284
285 SetElementMuted_Locked(elem_master_, mute);
286 if (elem_pcm_)
287 SetElementMuted_Locked(elem_pcm_, mute);
288 }
289
290 void AudioMixerAlsa::RestoreVolumeMuteOnUIThread() {
291 if (volume_pref_.GetValue() >= kSilenceDb)
292 DoSetVolumeMute(volume_pref_.GetValue(), mute_pref_.GetValue());
293 }
294
244 double AudioMixerAlsa::DoGetVolumeDb_Locked() const { 295 double AudioMixerAlsa::DoGetVolumeDb_Locked() const {
245 double vol_total = 0.0; 296 double vol_total = 0.0;
246 GetElementVolume_Locked(elem_master_, &vol_total); 297 GetElementVolume_Locked(elem_master_, &vol_total);
247 298
248 double vol_pcm = 0.0; 299 double vol_pcm = 0.0;
249 if (elem_pcm_ && (GetElementVolume_Locked(elem_pcm_, &vol_pcm))) 300 if (elem_pcm_ && (GetElementVolume_Locked(elem_pcm_, &vol_pcm)))
250 vol_total += vol_pcm; 301 vol_total += vol_pcm;
251 302
252 return vol_total; 303 return vol_total;
253 } 304 }
254 305
255 void AudioMixerAlsa::DoSetVolumeDb_Locked(double vol_db) { 306 void AudioMixerAlsa::DoSetVolumeDb_Locked(double vol_db) {
256 double actual_vol = 0.0; 307 double actual_vol = 0.0;
257 308
258 // If a PCM volume slider exists, then first set the Master volume to the 309 // If a PCM volume slider exists, then first set the Master volume to the
259 // nearest volume >= requested volume, then adjust PCM volume down to get 310 // nearest volume >= requested volume, then adjust PCM volume down to get
260 // closer to the requested volume. 311 // closer to the requested volume.
261
262 if (elem_pcm_) { 312 if (elem_pcm_) {
263 SetElementVolume_Locked(elem_master_, vol_db, &actual_vol, 0.9999f); 313 SetElementVolume_Locked(elem_master_, vol_db, &actual_vol, 0.9999f);
264 SetElementVolume_Locked(elem_pcm_, vol_db - actual_vol, NULL, 0.5f); 314 SetElementVolume_Locked(elem_pcm_, vol_db - actual_vol, NULL, 0.5f);
265 } else { 315 } else {
266 SetElementVolume_Locked(elem_master_, vol_db, NULL, 0.5f); 316 SetElementVolume_Locked(elem_master_, vol_db, NULL, 0.5f);
267 } 317 }
268 } 318 }
269 319
270 snd_mixer_elem_t* AudioMixerAlsa::FindElementWithName_Locked( 320 snd_mixer_elem_t* AudioMixerAlsa::FindElementWithName_Locked(
271 snd_mixer_t* handle, 321 snd_mixer_t* handle,
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 void AudioMixerAlsa::SetElementMuted_Locked(snd_mixer_elem_t* elem, bool mute) { 407 void AudioMixerAlsa::SetElementMuted_Locked(snd_mixer_elem_t* elem, bool mute) {
358 int enabled = mute ? 0 : 1; 408 int enabled = mute ? 0 : 1;
359 snd_mixer_selem_set_playback_switch_all(elem, enabled); 409 snd_mixer_selem_set_playback_switch_all(elem, enabled);
360 410
361 VLOG(1) << "Set playback switch " << snd_mixer_selem_get_name(elem) 411 VLOG(1) << "Set playback switch " << snd_mixer_selem_get_name(elem)
362 << " to " << enabled; 412 << " to " << enabled;
363 } 413 }
364 414
365 } // namespace chromeos 415 } // namespace chromeos
366 416
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/audio_mixer_alsa.h ('k') | chrome/common/pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698