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

Side by Side Diff: chrome/browser/chromeos/audio/audio_handler.cc

Issue 14314002: Implement new audio handler which talks to the new audio dbus apis. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add comments in class header file and todo for cleaning up legacy code. Created 7 years, 8 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/audio_handler.h" 5 #include "chrome/browser/chromeos/audio/audio_handler.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/singleton.h" 13 #include "base/memory/singleton.h"
14 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
15 #if defined(USE_CRAS) 15 #if defined(USE_CRAS)
16 #include "chrome/browser/chromeos/audio/audio_mixer_cras.h" 16 #include "chrome/browser/chromeos/audio/audio_mixer_cras.h"
17 #else 17 #else
18 #include "chrome/browser/chromeos/audio/audio_mixer_alsa.h" 18 #include "chrome/browser/chromeos/audio/audio_mixer_alsa.h"
19 #endif 19 #endif
20 #include "base/prefs/pref_registry_simple.h" 20 #include "base/prefs/pref_registry_simple.h"
21 #include "base/prefs/pref_service.h" 21 #include "base/prefs/pref_service.h"
22 #include "chrome/common/chrome_notification_types.h" 22 #include "chrome/common/chrome_notification_types.h"
23 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
24 #include "chromeos/audio/audio_pref_handler.h"
24 25
25 using std::max; 26 using std::max;
26 using std::min; 27 using std::min;
27 28
28 namespace chromeos { 29 namespace chromeos {
29 30
30 namespace { 31 namespace {
31 32
32 // Default value for the volume pref, as a percent in the range [0.0, 100.0].
33 const double kDefaultVolumePercent = 75.0;
34
35 // Default value for unmuting, as a percent in the range [0.0, 100.0]. 33 // Default value for unmuting, as a percent in the range [0.0, 100.0].
36 // Used when sound is unmuted, but volume was less than kMuteThresholdPercent. 34 // Used when sound is unmuted, but volume was less than kMuteThresholdPercent.
37 const double kDefaultUnmuteVolumePercent = 4.0; 35 const double kDefaultUnmuteVolumePercent = 4.0;
38 36
39 // Volume value which should be considered as muted in range [0.0, 100.0]. 37 // Volume value which should be considered as muted in range [0.0, 100.0].
40 const double kMuteThresholdPercent = 1.0; 38 const double kMuteThresholdPercent = 1.0;
41 39
42 // Values used for muted preference.
43 const int kPrefMuteOff = 0;
44 const int kPrefMuteOn = 1;
45
46 static AudioHandler* g_audio_handler = NULL; 40 static AudioHandler* g_audio_handler = NULL;
47 41
48 } // namespace 42 } // namespace
49 43
50 // static 44 // static
51 void AudioHandler::Initialize() { 45 void AudioHandler::Initialize(
46 scoped_refptr<AudioPrefHandler> audio_pref_handler) {
52 CHECK(!g_audio_handler); 47 CHECK(!g_audio_handler);
53 #if defined(USE_CRAS) 48 #if defined(USE_CRAS)
54 g_audio_handler = new AudioHandler(new AudioMixerCras()); 49 g_audio_handler = new AudioHandler(new AudioMixerCras(), audio_pref_handler);
55 #else 50 #else
56 g_audio_handler = new AudioHandler(new AudioMixerAlsa()); 51 g_audio_handler = new AudioHandler(new AudioMixerAlsa(), audio_pref_handler);
57 #endif 52 #endif
58 } 53 }
59 54
60 // static 55 // static
61 void AudioHandler::Shutdown() { 56 void AudioHandler::Shutdown() {
62 // We may call Shutdown without calling Initialize, e.g. if we exit early. 57 // We may call Shutdown without calling Initialize, e.g. if we exit early.
63 if (g_audio_handler) { 58 if (g_audio_handler) {
64 delete g_audio_handler; 59 delete g_audio_handler;
65 g_audio_handler = NULL; 60 g_audio_handler = NULL;
66 } 61 }
67 } 62 }
68 63
69 // static 64 // static
70 void AudioHandler::InitializeForTesting(AudioMixer* mixer) { 65 void AudioHandler::InitializeForTesting(
66 AudioMixer* mixer,
67 scoped_refptr<AudioPrefHandler> audio_pref_handler) {
71 CHECK(!g_audio_handler); 68 CHECK(!g_audio_handler);
72 g_audio_handler = new AudioHandler(mixer); 69 g_audio_handler = new AudioHandler(mixer, audio_pref_handler);
73 } 70 }
74 71
75 // static 72 // static
76 AudioHandler* AudioHandler::GetInstance() { 73 AudioHandler* AudioHandler::GetInstance() {
77 VLOG_IF(1, !g_audio_handler) 74 VLOG_IF(1, !g_audio_handler)
78 << "AudioHandler::GetInstance() called with NULL global instance."; 75 << "AudioHandler::GetInstance() called with NULL global instance.";
79 return g_audio_handler; 76 return g_audio_handler;
80 } 77 }
81 78
82 // static
83 void AudioHandler::RegisterPrefs(PrefRegistrySimple* registry) {
84 registry->RegisterDoublePref(prefs::kAudioVolumePercent,
85 kDefaultVolumePercent);
86 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff);
87 // Register the prefs backing the audio muting policies.
88 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true);
89 // This pref has moved to the media subsystem but we should verify it is there
90 // before we use it.
91 registry->RegisterBooleanPref(prefs::kAudioCaptureAllowed, true);
92 }
93
94 double AudioHandler::GetVolumePercent() { 79 double AudioHandler::GetVolumePercent() {
95 return mixer_->GetVolumePercent(); 80 return mixer_->GetVolumePercent();
96 } 81 }
97 82
98 void AudioHandler::SetVolumePercent(double volume_percent) { 83 void AudioHandler::SetVolumePercent(double volume_percent) {
99 volume_percent = min(max(volume_percent, 0.0), 100.0); 84 volume_percent = min(max(volume_percent, 0.0), 100.0);
100 if (volume_percent <= kMuteThresholdPercent) 85 if (volume_percent <= kMuteThresholdPercent)
101 volume_percent = 0.0; 86 volume_percent = 0.0;
102 if (IsMuted() && volume_percent > 0.0) 87 if (IsMuted() && volume_percent > 0.0)
103 SetMuted(false); 88 SetMuted(false);
104 if (!IsMuted() && volume_percent == 0.0) 89 if (!IsMuted() && volume_percent == 0.0)
105 SetMuted(true); 90 SetMuted(true);
106 SetVolumePercentInternal(volume_percent); 91 SetVolumePercentInternal(volume_percent);
107 } 92 }
108 93
109 void AudioHandler::SetVolumePercentInternal(double volume_percent) { 94 void AudioHandler::SetVolumePercentInternal(double volume_percent) {
110 mixer_->SetVolumePercent(volume_percent); 95 mixer_->SetVolumePercent(volume_percent);
111 local_state_->SetDouble(prefs::kAudioVolumePercent, volume_percent); 96 audio_pref_handler_->SetOutputVolumeValue(volume_percent);
112 if (volume_percent != volume_percent_) { 97 if (volume_percent != volume_percent_) {
113 volume_percent_ = volume_percent; 98 volume_percent_ = volume_percent;
114 FOR_EACH_OBSERVER(VolumeObserver, volume_observers_, OnVolumeChanged()); 99 FOR_EACH_OBSERVER(VolumeObserver, volume_observers_, OnVolumeChanged());
115 } 100 }
116 } 101 }
117 102
118 void AudioHandler::AdjustVolumeByPercent(double adjust_by_percent) { 103 void AudioHandler::AdjustVolumeByPercent(double adjust_by_percent) {
119 SetVolumePercent(mixer_->GetVolumePercent() + adjust_by_percent); 104 SetVolumePercent(mixer_->GetVolumePercent() + adjust_by_percent);
120 } 105 }
121 106
122 bool AudioHandler::IsMuted() { 107 bool AudioHandler::IsMuted() {
123 return mixer_->IsMuted(); 108 return mixer_->IsMuted();
124 } 109 }
125 110
126 void AudioHandler::SetMuted(bool mute) { 111 void AudioHandler::SetMuted(bool mute) {
127 if (!mixer_->IsMuteLocked()) { 112 if (!mixer_->IsMuteLocked()) {
128 mixer_->SetMuted(mute); 113 mixer_->SetMuted(mute);
129 local_state_->SetInteger(prefs::kAudioMute, 114 audio_pref_handler_->SetOutputMuteValue(mute);
130 mute ? kPrefMuteOn : kPrefMuteOff);
131 115
132 if (!mute) { 116 if (!mute) {
133 if (GetVolumePercent() <= kMuteThresholdPercent) { 117 if (GetVolumePercent() <= kMuteThresholdPercent) {
134 // Avoid the situation when sound has been unmuted, but the volume 118 // Avoid the situation when sound has been unmuted, but the volume
135 // is set to a very low value, so user still can't hear any sound. 119 // is set to a very low value, so user still can't hear any sound.
136 SetVolumePercentInternal(kDefaultUnmuteVolumePercent); 120 SetVolumePercentInternal(kDefaultUnmuteVolumePercent);
137 } 121 }
138 } 122 }
139 if (mute != muted_) { 123 if (mute != muted_) {
140 muted_ = mute; 124 muted_ = mute;
(...skipping 12 matching lines...) Expand all
153 } 137 }
154 138
155 void AudioHandler::AddVolumeObserver(VolumeObserver* observer) { 139 void AudioHandler::AddVolumeObserver(VolumeObserver* observer) {
156 volume_observers_.AddObserver(observer); 140 volume_observers_.AddObserver(observer);
157 } 141 }
158 142
159 void AudioHandler::RemoveVolumeObserver(VolumeObserver* observer) { 143 void AudioHandler::RemoveVolumeObserver(VolumeObserver* observer) {
160 volume_observers_.RemoveObserver(observer); 144 volume_observers_.RemoveObserver(observer);
161 } 145 }
162 146
163 AudioHandler::AudioHandler(AudioMixer* mixer) 147 void AudioHandler::OnAudioPolicyPrefChanged() {
148 ApplyAudioPolicy();
149 }
150
151 AudioHandler::AudioHandler(AudioMixer* mixer,
152 scoped_refptr<AudioPrefHandler> audio_pref_handler)
164 : mixer_(mixer), 153 : mixer_(mixer),
165 local_state_(g_browser_process->local_state()),
166 volume_percent_(0), 154 volume_percent_(0),
167 muted_(false) { 155 muted_(false),
168 InitializePrefObservers(); 156 audio_pref_handler_(audio_pref_handler) {
169 mixer_->Init(); 157 mixer_->Init();
158
159 DCHECK(audio_pref_handler_.get());
160 audio_pref_handler_->AddAudioPrefObserver(this);
170 ApplyAudioPolicy(); 161 ApplyAudioPolicy();
171 // Set initial state so that notifications are not triggered. 162 // Set initial state so that notifications are not triggered.
172 muted_ = (local_state_->GetInteger(prefs::kAudioMute) == kPrefMuteOn); 163 muted_ = audio_pref_handler_->GetOutputMuteValue();
173 volume_percent_ = local_state_->GetDouble(prefs::kAudioVolumePercent); 164 volume_percent_ = audio_pref_handler->GetOutputVolumeValue();
174 SetMuted(muted_); 165 SetMuted(muted_);
175 SetVolumePercentInternal(volume_percent_); 166 SetVolumePercentInternal(volume_percent_);
176 } 167 }
177 168
178 AudioHandler::~AudioHandler() { 169 AudioHandler::~AudioHandler() {
179 mixer_.reset(); 170 mixer_.reset();
171 audio_pref_handler_->RemoveAudioPrefObserver(this);
172 audio_pref_handler_ = NULL;
180 }; 173 };
181 174
182 void AudioHandler::InitializePrefObservers() {
183 pref_change_registrar_.Init(local_state_);
184 base::Closure callback = base::Bind(&AudioHandler::ApplyAudioPolicy,
185 base::Unretained(this));
186 pref_change_registrar_.Add(prefs::kAudioOutputAllowed, callback);
187 pref_change_registrar_.Add(prefs::kAudioCaptureAllowed, callback);
188 }
189
190 void AudioHandler::ApplyAudioPolicy() { 175 void AudioHandler::ApplyAudioPolicy() {
191 mixer_->SetMuteLocked(false); 176 mixer_->SetMuteLocked(false);
192 bool muted = false; 177 bool muted = false;
193 if (local_state_->GetBoolean(prefs::kAudioOutputAllowed)) { 178 if (audio_pref_handler_->GetAudioOutputAllowedValue()) {
194 muted = (local_state_->GetInteger(prefs::kAudioMute) == kPrefMuteOn); 179 muted = audio_pref_handler_->GetOutputMuteValue();
195 mixer_->SetMuted(muted); 180 mixer_->SetMuted(muted);
196 } else { 181 } else {
197 muted = true; 182 muted = true;
198 mixer_->SetMuted(muted); 183 mixer_->SetMuted(muted);
199 mixer_->SetMuteLocked(true); 184 mixer_->SetMuteLocked(true);
200 } 185 }
201 if (muted_ != muted) { 186 if (muted_ != muted) {
202 muted_ = muted; 187 muted_ = muted;
203 FOR_EACH_OBSERVER(VolumeObserver, volume_observers_, OnMuteToggled()); 188 FOR_EACH_OBSERVER(VolumeObserver, volume_observers_, OnMuteToggled());
204 } 189 }
205 mixer_->SetCaptureMuteLocked(false); 190 mixer_->SetCaptureMuteLocked(false);
206 if (local_state_->GetBoolean(prefs::kAudioCaptureAllowed)) { 191 if (audio_pref_handler_->GetAudioCaptureAllowedValue()) {
207 mixer_->SetCaptureMuted(false); 192 mixer_->SetCaptureMuted(false);
208 } else { 193 } else {
209 mixer_->SetCaptureMuted(true); 194 mixer_->SetCaptureMuted(true);
210 mixer_->SetCaptureMuteLocked(true); 195 mixer_->SetCaptureMuteLocked(true);
211 } 196 }
212 } 197 }
213 198
214 } // namespace chromeos 199 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698