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

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: Simplified prefs 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 = -999.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;
96 DoSetVolumeDb_Locked(vol_db); 101 DoSetVolumeDb_Locked(vol_db);
102 volume_pref_.SetValue(vol_db);
97 } 103 }
98 104
99 bool AudioMixerAlsa::IsMute() const { 105 bool AudioMixerAlsa::IsMute() const {
100 AutoLock lock(mixer_state_lock_); 106 AutoLock lock(mixer_state_lock_);
101 if (mixer_state_ != READY) 107 if (mixer_state_ != READY)
102 return false; 108 return false;
103 return GetElementMuted_Locked(elem_master_); 109 return GetElementMuted_Locked(elem_master_);
104 } 110 }
105 111
106 void AudioMixerAlsa::SetMute(bool mute) { 112 void AudioMixerAlsa::SetMute(bool mute) {
107 AutoLock lock(mixer_state_lock_); 113 AutoLock lock(mixer_state_lock_);
108 if (mixer_state_ != READY) 114 if (mixer_state_ != READY)
109 return; 115 return;
110 116
111 // Set volume to minimum on mute, since switching the element off does not 117 // Set volume to minimum on mute, since switching the element off does not
112 // always mute as it should. 118 // always mute as it should.
113 119
114 // TODO(davej): Setting volume to minimum can be removed once switching the 120 // TODO(davej): Remove save_volume_ and setting volume to minimum if
115 // element off can be guaranteed to work. 121 // switching the element off can be guaranteed to mute it.
116 122
117 bool old_value = GetElementMuted_Locked(elem_master_); 123 bool old_value = GetElementMuted_Locked(elem_master_);
118 124
119 if (old_value != mute) { 125 if (old_value != mute) {
120 if (mute) { 126 if (mute) {
121 save_volume_ = DoGetVolumeDb_Locked(); 127 save_volume_ = DoGetVolumeDb_Locked();
122 DoSetVolumeDb_Locked(min_volume_); 128 DoSetVolumeDb_Locked(min_volume_);
123 } else { 129 } else {
124 DoSetVolumeDb_Locked(save_volume_); 130 DoSetVolumeDb_Locked(save_volume_);
125 } 131 }
126 } 132 }
127 133
128 SetElementMuted_Locked(elem_master_, mute); 134 SetElementMuted_Locked(elem_master_, mute);
129 if (elem_pcm_) 135 if (elem_pcm_)
130 SetElementMuted_Locked(elem_pcm_, mute); 136 SetElementMuted_Locked(elem_pcm_, mute);
137 mute_pref_.SetValue(mute);
131 } 138 }
132 139
133 AudioMixer::State AudioMixerAlsa::GetState() const { 140 AudioMixer::State AudioMixerAlsa::GetState() const {
134 AutoLock lock(mixer_state_lock_); 141 AutoLock lock(mixer_state_lock_);
135 // If we think it's ready, verify it is actually so. 142 // If we think it's ready, verify it is actually so.
136 if ((mixer_state_ == READY) && (alsa_mixer_ == NULL)) 143 if ((mixer_state_ == READY) && (alsa_mixer_ == NULL))
137 mixer_state_ = IN_ERROR; 144 mixer_state_ = IN_ERROR;
138 return mixer_state_; 145 return mixer_state_;
139 } 146 }
140 147
141 //////////////////////////////////////////////////////////////////////////////// 148 ////////////////////////////////////////////////////////////////////////////////
142 // Private functions follow 149 // Private functions follow
143 150
144 void AudioMixerAlsa::DoInit(InitDoneCallback* callback) { 151 void AudioMixerAlsa::DoInit(InitDoneCallback* callback) {
145 bool success = InitializeAlsaMixer(); 152 bool success = InitializeAlsaMixer();
146 153
154 if (success) {
155 BrowserThread::PostTask(
156 BrowserThread::UI, FROM_HERE,
157 NewRunnableMethod(this, &AudioMixerAlsa::RestoreVolumeMuteOnUIThread));
158 }
159
147 if (callback) { 160 if (callback) {
148 callback->Run(success); 161 callback->Run(success);
149 delete callback; 162 delete callback;
150 } 163 }
151 } 164 }
152 165
153 bool AudioMixerAlsa::InitThread() { 166 bool AudioMixerAlsa::InitThread() {
154 AutoLock lock(mixer_state_lock_); 167 AutoLock lock(mixer_state_lock_);
155 168
156 if (mixer_state_ != UNINITIALIZED) 169 if (mixer_state_ != UNINITIALIZED)
157 return false; 170 return false;
158 171
159 if (thread_ == NULL) { 172 if (thread_ == NULL) {
160 thread_.reset(new base::Thread("AudioMixerAlsa")); 173 thread_.reset(new base::Thread("AudioMixerAlsa"));
161 if (!thread_->Start()) { 174 if (!thread_->Start()) {
162 thread_.reset(); 175 thread_.reset();
163 return false; 176 return false;
164 } 177 }
165 } 178 }
166 179
167 mixer_state_ = INITIALIZING; 180 mixer_state_ = INITIALIZING;
168 return true; 181 return true;
169 } 182 }
170 183
184 void AudioMixerAlsa::InitPrefs() {
185 DCHECK(g_browser_process);
186 PrefService* prefs = g_browser_process->local_state();
187 DCHECK(prefs);
188
189 if (!prefs->FindPreference(prefs::kAudioVolume)) {
190 prefs->RegisterRealPref(prefs::kAudioVolume, kPrefVolumeNotSet);
191 prefs->RegisterBooleanPref(prefs::kAudioMute, false);
192 }
193 volume_pref_.Init(prefs::kAudioVolume, prefs, NULL);
194 mute_pref_.Init(prefs::kAudioMute, prefs, NULL);
195 }
196
171 bool AudioMixerAlsa::InitializeAlsaMixer() { 197 bool AudioMixerAlsa::InitializeAlsaMixer() {
172 AutoLock lock(mixer_state_lock_); 198 AutoLock lock(mixer_state_lock_);
173 if (mixer_state_ != INITIALIZING) 199 if (mixer_state_ != INITIALIZING)
174 return false; 200 return false;
175 201
176 int err; 202 int err;
177 snd_mixer_t* handle = NULL; 203 snd_mixer_t* handle = NULL;
178 const char* card = "default"; 204 const char* card = "default";
179 205
180 if ((err = snd_mixer_open(&handle, 0)) < 0) { 206 if ((err = snd_mixer_open(&handle, 0)) < 0) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 260
235 void AudioMixerAlsa::FreeAlsaMixer() { 261 void AudioMixerAlsa::FreeAlsaMixer() {
236 AutoLock lock(mixer_state_lock_); 262 AutoLock lock(mixer_state_lock_);
237 mixer_state_ = SHUTTING_DOWN; 263 mixer_state_ = SHUTTING_DOWN;
238 if (alsa_mixer_) { 264 if (alsa_mixer_) {
239 snd_mixer_close(alsa_mixer_); 265 snd_mixer_close(alsa_mixer_);
240 alsa_mixer_ = NULL; 266 alsa_mixer_ = NULL;
241 } 267 }
242 } 268 }
243 269
270 void AudioMixerAlsa::DoSetVolumeMute(double volume, bool mute) {
271 AutoLock lock(mixer_state_lock_);
272 if (mixer_state_ != READY)
273 return;
274
275 VLOG(1) << "Setting volume to " << volume << "and mute to " << mute;
276 if (mute) {
277 save_volume_ = volume;
278 DoSetVolumeDb_Locked(min_volume_);
279 } else {
280 DoSetVolumeDb_Locked(volume);
281 }
282
283 SetElementMuted_Locked(elem_master_, mute);
284 if (elem_pcm_)
285 SetElementMuted_Locked(elem_pcm_, mute);
286 }
287
288 void AudioMixerAlsa::RestoreVolumeMuteOnUIThread() {
289 if (volume_pref_.GetValue() != kPrefVolumeNotSet)
Daniel Erat 2011/01/13 21:37:21 Could you do something like GetValue() <= kDefault
davejcool 2011/01/14 00:14:12 I recall problems in the distant past with compari
290 DoSetVolumeMute(volume_pref_.GetValue(), mute_pref_.GetValue());
291 }
292
244 double AudioMixerAlsa::DoGetVolumeDb_Locked() const { 293 double AudioMixerAlsa::DoGetVolumeDb_Locked() const {
245 double vol_total = 0.0; 294 double vol_total = 0.0;
246 GetElementVolume_Locked(elem_master_, &vol_total); 295 GetElementVolume_Locked(elem_master_, &vol_total);
247 296
248 double vol_pcm = 0.0; 297 double vol_pcm = 0.0;
249 if (elem_pcm_ && (GetElementVolume_Locked(elem_pcm_, &vol_pcm))) 298 if (elem_pcm_ && (GetElementVolume_Locked(elem_pcm_, &vol_pcm)))
250 vol_total += vol_pcm; 299 vol_total += vol_pcm;
251 300
252 return vol_total; 301 return vol_total;
253 } 302 }
254 303
255 void AudioMixerAlsa::DoSetVolumeDb_Locked(double vol_db) { 304 void AudioMixerAlsa::DoSetVolumeDb_Locked(double vol_db) {
256 double actual_vol = 0.0; 305 double actual_vol = 0.0;
257 306
258 // If a PCM volume slider exists, then first set the Master volume to the 307 // 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 308 // nearest volume >= requested volume, then adjust PCM volume down to get
260 // closer to the requested volume. 309 // closer to the requested volume.
261
262 if (elem_pcm_) { 310 if (elem_pcm_) {
263 SetElementVolume_Locked(elem_master_, vol_db, &actual_vol, 0.9999f); 311 SetElementVolume_Locked(elem_master_, vol_db, &actual_vol, 0.9999f);
264 SetElementVolume_Locked(elem_pcm_, vol_db - actual_vol, NULL, 0.5f); 312 SetElementVolume_Locked(elem_pcm_, vol_db - actual_vol, NULL, 0.5f);
265 } else { 313 } else {
266 SetElementVolume_Locked(elem_master_, vol_db, NULL, 0.5f); 314 SetElementVolume_Locked(elem_master_, vol_db, NULL, 0.5f);
267 } 315 }
268 } 316 }
269 317
270 snd_mixer_elem_t* AudioMixerAlsa::FindElementWithName_Locked( 318 snd_mixer_elem_t* AudioMixerAlsa::FindElementWithName_Locked(
271 snd_mixer_t* handle, 319 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) { 405 void AudioMixerAlsa::SetElementMuted_Locked(snd_mixer_elem_t* elem, bool mute) {
358 int enabled = mute ? 0 : 1; 406 int enabled = mute ? 0 : 1;
359 snd_mixer_selem_set_playback_switch_all(elem, enabled); 407 snd_mixer_selem_set_playback_switch_all(elem, enabled);
360 408
361 VLOG(1) << "Set playback switch " << snd_mixer_selem_get_name(elem) 409 VLOG(1) << "Set playback switch " << snd_mixer_selem_get_name(elem)
362 << " to " << enabled; 410 << " to " << enabled;
363 } 411 }
364 412
365 } // namespace chromeos 413 } // namespace chromeos
366 414
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