Index: chrome/browser/chromeos/audio_mixer_alsa.cc |
diff --git a/chrome/browser/chromeos/audio_mixer_alsa.cc b/chrome/browser/chromeos/audio_mixer_alsa.cc |
index 7f96fa4f2cd9daaa67abbc0bdc61c63940f78256..cd08e27d7ae9226923856f18a16b8ca79f09cb1f 100644 |
--- a/chrome/browser/chromeos/audio_mixer_alsa.cc |
+++ b/chrome/browser/chromeos/audio_mixer_alsa.cc |
@@ -8,6 +8,10 @@ |
#include "base/logging.h" |
#include "base/task.h" |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/browser_thread.h" |
+#include "chrome/browser/prefs/pref_service.h" |
+#include "chrome/common/pref_names.h" |
namespace chromeos { |
@@ -19,8 +23,6 @@ namespace chromeos { |
// adjusting that as well. If the PCM element has more volume steps, it allows |
// for finer granularity in the total volume. |
-// TODO(davej): Serialize volume/mute to preserve settings when restarting. |
- |
typedef long alsa_long_t; // 'long' is required for ALSA API calls. |
namespace { |
@@ -29,6 +31,7 @@ const char* kMasterVolume = "Master"; |
const char* kPCMVolume = "PCM"; |
const double kDefaultMinVolume = -90.0; |
const double kDefaultMaxVolume = 0.0; |
+const double kPrefVolumeNotSet = -999.0; |
} // namespace |
@@ -57,6 +60,7 @@ void AudioMixerAlsa::Init(InitDoneCallback* callback) { |
delete callback; |
return; |
} |
+ InitPrefs(); |
// Post the task of starting up, which can block for 200-500ms, |
// so best not to do it on the caller's thread. |
@@ -67,6 +71,7 @@ void AudioMixerAlsa::Init(InitDoneCallback* callback) { |
bool AudioMixerAlsa::InitSync() { |
if (!InitThread()) |
return false; |
+ InitPrefs(); |
return InitializeAlsaMixer(); |
} |
@@ -94,6 +99,7 @@ void AudioMixerAlsa::SetVolumeDb(double vol_db) { |
if (mixer_state_ != READY) |
return; |
DoSetVolumeDb_Locked(vol_db); |
+ volume_pref_.SetValue(vol_db); |
} |
bool AudioMixerAlsa::IsMute() const { |
@@ -111,8 +117,8 @@ void AudioMixerAlsa::SetMute(bool mute) { |
// Set volume to minimum on mute, since switching the element off does not |
// always mute as it should. |
- // TODO(davej): Setting volume to minimum can be removed once switching the |
- // element off can be guaranteed to work. |
+ // TODO(davej): Remove save_volume_ and setting volume to minimum if |
+ // switching the element off can be guaranteed to mute it. |
bool old_value = GetElementMuted_Locked(elem_master_); |
@@ -128,6 +134,7 @@ void AudioMixerAlsa::SetMute(bool mute) { |
SetElementMuted_Locked(elem_master_, mute); |
if (elem_pcm_) |
SetElementMuted_Locked(elem_pcm_, mute); |
+ mute_pref_.SetValue(mute); |
} |
AudioMixer::State AudioMixerAlsa::GetState() const { |
@@ -144,6 +151,12 @@ AudioMixer::State AudioMixerAlsa::GetState() const { |
void AudioMixerAlsa::DoInit(InitDoneCallback* callback) { |
bool success = InitializeAlsaMixer(); |
+ if (success) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ NewRunnableMethod(this, &AudioMixerAlsa::RestoreVolumeMuteOnUIThread)); |
+ } |
+ |
if (callback) { |
callback->Run(success); |
delete callback; |
@@ -168,6 +181,19 @@ bool AudioMixerAlsa::InitThread() { |
return true; |
} |
+void AudioMixerAlsa::InitPrefs() { |
+ DCHECK(g_browser_process); |
+ PrefService* prefs = g_browser_process->local_state(); |
+ DCHECK(prefs); |
+ |
+ if (!prefs->FindPreference(prefs::kAudioVolume)) { |
+ prefs->RegisterRealPref(prefs::kAudioVolume, kPrefVolumeNotSet); |
+ prefs->RegisterBooleanPref(prefs::kAudioMute, false); |
+ } |
+ volume_pref_.Init(prefs::kAudioVolume, prefs, NULL); |
+ mute_pref_.Init(prefs::kAudioMute, prefs, NULL); |
+} |
+ |
bool AudioMixerAlsa::InitializeAlsaMixer() { |
AutoLock lock(mixer_state_lock_); |
if (mixer_state_ != INITIALIZING) |
@@ -241,6 +267,29 @@ void AudioMixerAlsa::FreeAlsaMixer() { |
} |
} |
+void AudioMixerAlsa::DoSetVolumeMute(double volume, bool mute) { |
+ AutoLock lock(mixer_state_lock_); |
+ if (mixer_state_ != READY) |
+ return; |
+ |
+ VLOG(1) << "Setting volume to " << volume << "and mute to " << mute; |
+ if (mute) { |
+ save_volume_ = volume; |
+ DoSetVolumeDb_Locked(min_volume_); |
+ } else { |
+ DoSetVolumeDb_Locked(volume); |
+ } |
+ |
+ SetElementMuted_Locked(elem_master_, mute); |
+ if (elem_pcm_) |
+ SetElementMuted_Locked(elem_pcm_, mute); |
+} |
+ |
+void AudioMixerAlsa::RestoreVolumeMuteOnUIThread() { |
+ 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
|
+ DoSetVolumeMute(volume_pref_.GetValue(), mute_pref_.GetValue()); |
+} |
+ |
double AudioMixerAlsa::DoGetVolumeDb_Locked() const { |
double vol_total = 0.0; |
GetElementVolume_Locked(elem_master_, &vol_total); |
@@ -258,7 +307,6 @@ void AudioMixerAlsa::DoSetVolumeDb_Locked(double vol_db) { |
// If a PCM volume slider exists, then first set the Master volume to the |
// nearest volume >= requested volume, then adjust PCM volume down to get |
// closer to the requested volume. |
- |
if (elem_pcm_) { |
SetElementVolume_Locked(elem_master_, vol_db, &actual_vol, 0.9999f); |
SetElementVolume_Locked(elem_pcm_, vol_db - actual_vol, NULL, 0.5f); |