OLD | NEW |
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/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 } // namespace | 41 } // namespace |
42 | 42 |
43 AudioMixerAlsa::AudioMixerAlsa() | 43 AudioMixerAlsa::AudioMixerAlsa() |
44 : min_volume_(kDefaultMinVolume), | 44 : min_volume_(kDefaultMinVolume), |
45 max_volume_(kDefaultMaxVolume), | 45 max_volume_(kDefaultMaxVolume), |
46 save_volume_(0), | 46 save_volume_(0), |
47 mixer_state_(UNINITIALIZED), | 47 mixer_state_(UNINITIALIZED), |
48 alsa_mixer_(NULL), | 48 alsa_mixer_(NULL), |
49 elem_master_(NULL), | 49 elem_master_(NULL), |
50 elem_pcm_(NULL) { | 50 elem_pcm_(NULL), |
| 51 prefs_(NULL) { |
51 } | 52 } |
52 | 53 |
53 AudioMixerAlsa::~AudioMixerAlsa() { | 54 AudioMixerAlsa::~AudioMixerAlsa() { |
54 FreeAlsaMixer(); | 55 FreeAlsaMixer(); |
55 if (thread_ != NULL) { | 56 if (thread_ != NULL) { |
56 // A ScopedAllowIO object is required to join the thread when calling Stop. | 57 // A ScopedAllowIO object is required to join the thread when calling Stop. |
57 // The worker thread should be idle at this time. | 58 // The worker thread should be idle at this time. |
58 // See http://crosbug.com/11110 for discussion. | 59 // See http://crosbug.com/11110 for discussion. |
59 base::ThreadRestrictions::ScopedAllowIO allow_io_for_thread_join; | 60 base::ThreadRestrictions::ScopedAllowIO allow_io_for_thread_join; |
60 thread_->message_loop()->AssertIdle(); | 61 thread_->message_loop()->AssertIdle(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 return true; | 106 return true; |
106 } | 107 } |
107 | 108 |
108 void AudioMixerAlsa::SetVolumeDb(double vol_db) { | 109 void AudioMixerAlsa::SetVolumeDb(double vol_db) { |
109 base::AutoLock lock(mixer_state_lock_); | 110 base::AutoLock lock(mixer_state_lock_); |
110 if (mixer_state_ != READY) | 111 if (mixer_state_ != READY) |
111 return; | 112 return; |
112 if (vol_db < kSilenceDb) | 113 if (vol_db < kSilenceDb) |
113 vol_db = kSilenceDb; | 114 vol_db = kSilenceDb; |
114 DoSetVolumeDb_Locked(vol_db); | 115 DoSetVolumeDb_Locked(vol_db); |
115 volume_pref_.SetValue(vol_db); | 116 prefs_->SetDouble(prefs::kAudioVolume, vol_db); |
116 } | 117 } |
117 | 118 |
118 bool AudioMixerAlsa::IsMute() const { | 119 bool AudioMixerAlsa::IsMute() const { |
119 base::AutoLock lock(mixer_state_lock_); | 120 base::AutoLock lock(mixer_state_lock_); |
120 if (mixer_state_ != READY) | 121 if (mixer_state_ != READY) |
121 return false; | 122 return false; |
122 return GetElementMuted_Locked(elem_master_); | 123 return GetElementMuted_Locked(elem_master_); |
123 } | 124 } |
124 | 125 |
125 // To indicate the volume is not valid yet, a very low volume value is stored. | 126 // To indicate the volume is not valid yet, a very low volume value is stored. |
(...skipping 21 matching lines...) Expand all Loading... |
147 save_volume_ = DoGetVolumeDb_Locked(); | 148 save_volume_ = DoGetVolumeDb_Locked(); |
148 DoSetVolumeDb_Locked(min_volume_); | 149 DoSetVolumeDb_Locked(min_volume_); |
149 } else { | 150 } else { |
150 DoSetVolumeDb_Locked(save_volume_); | 151 DoSetVolumeDb_Locked(save_volume_); |
151 } | 152 } |
152 } | 153 } |
153 | 154 |
154 SetElementMuted_Locked(elem_master_, mute); | 155 SetElementMuted_Locked(elem_master_, mute); |
155 if (elem_pcm_) | 156 if (elem_pcm_) |
156 SetElementMuted_Locked(elem_pcm_, mute); | 157 SetElementMuted_Locked(elem_pcm_, mute); |
157 mute_pref_.SetValue(mute ? kPrefMuteOn : kPrefMuteOff); | 158 prefs_->SetInteger(prefs::kAudioMute, mute ? kPrefMuteOn : kPrefMuteOff); |
158 } | 159 } |
159 | 160 |
160 AudioMixer::State AudioMixerAlsa::GetState() const { | 161 AudioMixer::State AudioMixerAlsa::GetState() const { |
161 base::AutoLock lock(mixer_state_lock_); | 162 base::AutoLock lock(mixer_state_lock_); |
162 // If we think it's ready, verify it is actually so. | 163 // If we think it's ready, verify it is actually so. |
163 if ((mixer_state_ == READY) && (alsa_mixer_ == NULL)) | 164 if ((mixer_state_ == READY) && (alsa_mixer_ == NULL)) |
164 mixer_state_ = IN_ERROR; | 165 mixer_state_ = IN_ERROR; |
165 return mixer_state_; | 166 return mixer_state_; |
166 } | 167 } |
167 | 168 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 thread_.reset(); | 204 thread_.reset(); |
204 return false; | 205 return false; |
205 } | 206 } |
206 } | 207 } |
207 | 208 |
208 mixer_state_ = INITIALIZING; | 209 mixer_state_ = INITIALIZING; |
209 return true; | 210 return true; |
210 } | 211 } |
211 | 212 |
212 void AudioMixerAlsa::InitPrefs() { | 213 void AudioMixerAlsa::InitPrefs() { |
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 214 prefs_ = g_browser_process->local_state(); |
214 PrefService* prefs = g_browser_process->local_state(); | |
215 volume_pref_.Init(prefs::kAudioVolume, prefs, NULL); | |
216 mute_pref_.Init(prefs::kAudioMute, prefs, NULL); | |
217 } | 215 } |
218 | 216 |
219 bool AudioMixerAlsa::InitializeAlsaMixer() { | 217 bool AudioMixerAlsa::InitializeAlsaMixer() { |
220 base::AutoLock lock(mixer_state_lock_); | 218 base::AutoLock lock(mixer_state_lock_); |
221 if (mixer_state_ != INITIALIZING) | 219 if (mixer_state_ != INITIALIZING) |
222 return false; | 220 return false; |
223 | 221 |
224 int err; | 222 int err; |
225 snd_mixer_t* handle = NULL; | 223 snd_mixer_t* handle = NULL; |
226 const char* card = "default"; | 224 const char* card = "default"; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 } | 313 } |
316 | 314 |
317 SetElementMuted_Locked(elem_master_, mute); | 315 SetElementMuted_Locked(elem_master_, mute); |
318 if (elem_pcm_) | 316 if (elem_pcm_) |
319 SetElementMuted_Locked(elem_pcm_, mute); | 317 SetElementMuted_Locked(elem_pcm_, mute); |
320 } | 318 } |
321 | 319 |
322 void AudioMixerAlsa::RestoreVolumeMuteOnUIThread() { | 320 void AudioMixerAlsa::RestoreVolumeMuteOnUIThread() { |
323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 321 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
324 // This happens during init, so set the volume off the UI thread. | 322 // This happens during init, so set the volume off the UI thread. |
| 323 int mute = prefs_->GetInteger(prefs::kAudioMute); |
| 324 double volume = prefs_->GetDouble(prefs::kAudioVolume); |
325 thread_->message_loop()->PostTask(FROM_HERE, | 325 thread_->message_loop()->PostTask(FROM_HERE, |
326 NewRunnableMethod(this, &AudioMixerAlsa::DoSetVolumeMute, | 326 NewRunnableMethod(this, &AudioMixerAlsa::DoSetVolumeMute, volume, mute)); |
327 volume_pref_.GetValue(), mute_pref_.GetValue())); | |
328 } | 327 } |
329 | 328 |
330 double AudioMixerAlsa::DoGetVolumeDb_Locked() const { | 329 double AudioMixerAlsa::DoGetVolumeDb_Locked() const { |
331 double vol_total = 0.0; | 330 double vol_total = 0.0; |
332 GetElementVolume_Locked(elem_master_, &vol_total); | 331 GetElementVolume_Locked(elem_master_, &vol_total); |
333 | 332 |
334 double vol_pcm = 0.0; | 333 double vol_pcm = 0.0; |
335 if (elem_pcm_ && (GetElementVolume_Locked(elem_pcm_, &vol_pcm))) | 334 if (elem_pcm_ && (GetElementVolume_Locked(elem_pcm_, &vol_pcm))) |
336 vol_total += vol_pcm; | 335 vol_total += vol_pcm; |
337 | 336 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 | 440 |
442 void AudioMixerAlsa::SetElementMuted_Locked(snd_mixer_elem_t* elem, bool mute) { | 441 void AudioMixerAlsa::SetElementMuted_Locked(snd_mixer_elem_t* elem, bool mute) { |
443 int enabled = mute ? 0 : 1; | 442 int enabled = mute ? 0 : 1; |
444 snd_mixer_selem_set_playback_switch_all(elem, enabled); | 443 snd_mixer_selem_set_playback_switch_all(elem, enabled); |
445 | 444 |
446 VLOG(1) << "Set playback switch " << snd_mixer_selem_get_name(elem) | 445 VLOG(1) << "Set playback switch " << snd_mixer_selem_get_name(elem) |
447 << " to " << enabled; | 446 << " to " << enabled; |
448 } | 447 } |
449 | 448 |
450 } // namespace chromeos | 449 } // namespace chromeos |
OLD | NEW |