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

Side by Side Diff: chromeos/audio/cras_audio_handler.cc

Issue 14801002: Switch Audio Preferences to per device. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "chromeos/audio/cras_audio_handler.h" 5 #include "chromeos/audio/cras_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 "chromeos/audio/audio_pref_handler.h" 13 #include "chromeos/audio/audio_devices_pref_handler.h"
14 #include "chromeos/audio/mock_cras_audio_handler.h" 14 #include "chromeos/audio/mock_cras_audio_handler.h"
15 #include "chromeos/dbus/dbus_thread_manager.h" 15 #include "chromeos/dbus/dbus_thread_manager.h"
16 16
17 using std::max; 17 using std::max;
18 using std::min; 18 using std::min;
19 19
20 namespace chromeos { 20 namespace chromeos {
21 21
22 namespace { 22 namespace {
23 23
(...skipping 27 matching lines...) Expand all
51 } 51 }
52 52
53 void CrasAudioHandler::AudioObserver::OnActiveOutputNodeChanged() { 53 void CrasAudioHandler::AudioObserver::OnActiveOutputNodeChanged() {
54 } 54 }
55 55
56 void CrasAudioHandler::AudioObserver::OnActiveInputNodeChanged() { 56 void CrasAudioHandler::AudioObserver::OnActiveInputNodeChanged() {
57 } 57 }
58 58
59 // static 59 // static
60 void CrasAudioHandler::Initialize( 60 void CrasAudioHandler::Initialize(
61 scoped_refptr<AudioPrefHandler> audio_pref_handler) { 61 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler) {
62 CHECK(!g_cras_audio_handler); 62 CHECK(!g_cras_audio_handler);
63 g_cras_audio_handler = new CrasAudioHandler(audio_pref_handler); 63 g_cras_audio_handler = new CrasAudioHandler(audio_pref_handler);
64 } 64 }
65 65
66 // static 66 // static
67 void CrasAudioHandler::InitializeForTesting() { 67 void CrasAudioHandler::InitializeForTesting() {
68 CHECK(!g_cras_audio_handler); 68 CHECK(!g_cras_audio_handler);
69 g_cras_audio_handler = new MockCrasAudioHandler(); 69 g_cras_audio_handler = new MockCrasAudioHandler();
70 } 70 }
71 71
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 for (size_t i = 0; i < audio_devices_.size(); ++i) 120 for (size_t i = 0; i < audio_devices_.size(); ++i)
121 device_list->push_back(audio_devices_[i]); 121 device_list->push_back(audio_devices_[i]);
122 } 122 }
123 123
124 bool CrasAudioHandler::GetActiveOutputDevice(AudioDevice* device) const { 124 bool CrasAudioHandler::GetActiveOutputDevice(AudioDevice* device) const {
125 for (size_t i = 0; i < audio_devices_.size(); ++i) { 125 for (size_t i = 0; i < audio_devices_.size(); ++i) {
126 if (audio_devices_[i].id == active_output_node_id_) { 126 if (audio_devices_[i].id == active_output_node_id_) {
127 *device = audio_devices_[i]; 127 *device = audio_devices_[i];
128 return true; 128 return true;
129 } 129 }
130 } 130 }
hshi1 2013/05/02 02:59:56 is the NOTREACHED() no longer needed?
rkc 2013/05/02 06:34:21 Yes, the NOTREACHED is no longer relevant. Since w
131 NOTREACHED() << "Can't find active output audio device";
132 return false; 131 return false;
133 } 132 }
134 133
135 bool CrasAudioHandler::has_alternative_input() const { 134 bool CrasAudioHandler::has_alternative_input() const {
136 return has_alternative_input_; 135 return has_alternative_input_;
137 } 136 }
138 137
139 bool CrasAudioHandler::has_alternative_output() const { 138 bool CrasAudioHandler::has_alternative_output() const {
140 return has_alternative_output_; 139 return has_alternative_output_;
141 } 140 }
142 141
143 void CrasAudioHandler::SetOutputVolumePercent(int volume_percent) { 142 void CrasAudioHandler::SetOutputVolumePercent(int volume_percent) {
144 volume_percent = min(max(volume_percent, 0), 100); 143 volume_percent = min(max(volume_percent, 0), 100);
145 if (volume_percent <= kMuteThresholdPercent) 144 if (volume_percent <= kMuteThresholdPercent)
146 volume_percent = 0; 145 volume_percent = 0;
147 SetOutputVolumeInternal(volume_percent); 146 SetOutputVolumeInternal(volume_percent);
148 if (IsOutputMuted() && volume_percent > 0) 147 if (IsOutputMuted() && volume_percent > 0)
149 SetOutputMute(false); 148 SetOutputMute(false);
150 if (!IsOutputMuted() && volume_percent == 0) 149 if (!IsOutputMuted() && volume_percent == 0)
151 SetOutputMute(true); 150 SetOutputMute(true);
152 } 151 }
153 152
154 void CrasAudioHandler::AdjustOutputVolumeByPercent(int adjust_by_percent) { 153 void CrasAudioHandler::AdjustOutputVolumeByPercent(int adjust_by_percent) {
155 SetOutputVolumePercent(output_volume_ + adjust_by_percent); 154 SetOutputVolumePercent(output_volume_ + adjust_by_percent);
156 } 155 }
157 156
158 void CrasAudioHandler::SetOutputMute(bool mute_on) { 157 void CrasAudioHandler::SetOutputMute(bool mute_on) {
159 if (output_mute_locked_) { 158 if (output_mute_locked_) {
160 NOTREACHED() << "Output mute has been locked"; 159 LOG(WARNING) << "Tried to set volume with output mute locked";
161 return; 160 return;
162 } 161 }
163 162
164 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> 163 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
165 SetOutputMute(mute_on); 164 SetOutputMute(mute_on);
166 165
167 if (!mute_on) { 166 if (!mute_on) {
168 if (output_volume_ <= kMuteThresholdPercent) { 167 if (output_volume_ <= kMuteThresholdPercent) {
169 // Avoid the situation when sound has been unmuted, but the volume 168 // Avoid the situation when sound has been unmuted, but the volume
170 // is set to a very low value, so user still can't hear any sound. 169 // is set to a very low value, so user still can't hear any sound.
171 SetOutputVolumeInternal(kDefaultUnmuteVolumePercent); 170 SetOutputVolumeInternal(kDefaultUnmuteVolumePercent);
172 } 171 }
173 } 172 }
174 } 173 }
175 174
176 void CrasAudioHandler::SetInputMute(bool mute_on) { 175 void CrasAudioHandler::SetInputMute(bool mute_on) {
177 if (input_mute_locked_) { 176 if (input_mute_locked_) {
178 NOTREACHED() << "Input mute has been locked"; 177 LOG(WARNING) << "Tried to set gain with input mute locked";
179 return; 178 return;
180 } 179 }
181 180
182 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> 181 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
183 SetInputMute(mute_on); 182 SetInputMute(mute_on);
184 } 183 }
185 184
186 void CrasAudioHandler::SetActiveOutputNode(uint64 node_id) { 185 void CrasAudioHandler::SetActiveOutputNode(uint64 node_id) {
187 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> 186 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
188 SetActiveOutputNode(node_id); 187 SetActiveOutputNode(node_id);
189 } 188 }
190 189
191 void CrasAudioHandler::SetActiveInputNode(uint64 node_id) { 190 void CrasAudioHandler::SetActiveInputNode(uint64 node_id) {
192 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> 191 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
193 SetActiveInputNode(node_id); 192 SetActiveInputNode(node_id);
194 } 193 }
195 194
196 CrasAudioHandler::CrasAudioHandler( 195 CrasAudioHandler::CrasAudioHandler(
197 scoped_refptr<AudioPrefHandler> audio_pref_handler) 196 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler)
198 : audio_pref_handler_(audio_pref_handler), 197 : audio_pref_handler_(audio_pref_handler),
199 weak_ptr_factory_(this), 198 weak_ptr_factory_(this),
200 output_mute_on_(false), 199 output_mute_on_(false),
201 input_mute_on_(false), 200 input_mute_on_(false),
202 output_volume_(0), 201 output_volume_(0),
203 active_output_node_id_(0), 202 active_output_node_id_(0),
204 active_input_node_id_(0), 203 active_input_node_id_(0),
205 has_alternative_input_(false), 204 has_alternative_input_(false),
206 has_alternative_output_(false), 205 has_alternative_output_(false),
207 output_mute_locked_(false), 206 output_mute_locked_(false),
208 input_mute_locked_(false) { 207 input_mute_locked_(false) {
209 if (!audio_pref_handler) 208 if (!audio_pref_handler)
210 return; 209 return;
211 // If the DBusThreadManager or the CrasAudioClient aren't available, there 210 // If the DBusThreadManager or the CrasAudioClient aren't available, there
212 // isn't much we can do. This should only happen when running tests. 211 // isn't much we can do. This should only happen when running tests.
213 if (!chromeos::DBusThreadManager::IsInitialized() || 212 if (!chromeos::DBusThreadManager::IsInitialized() ||
214 !chromeos::DBusThreadManager::Get() || 213 !chromeos::DBusThreadManager::Get() ||
215 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) 214 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient())
216 return; 215 return;
217 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->AddObserver(this); 216 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->AddObserver(this);
218 audio_pref_handler_->AddAudioPrefObserver(this); 217 audio_pref_handler_->AddAudioPrefObserver(this);
219 SetupInitialAudioState(); 218 GetNodes();
220 } 219 }
221 220
222 CrasAudioHandler::~CrasAudioHandler() { 221 CrasAudioHandler::~CrasAudioHandler() {
223 if (!chromeos::DBusThreadManager::IsInitialized() || 222 if (!chromeos::DBusThreadManager::IsInitialized() ||
224 !chromeos::DBusThreadManager::Get() || 223 !chromeos::DBusThreadManager::Get() ||
225 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) 224 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient())
226 return; 225 return;
227 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> 226 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
228 RemoveObserver(this); 227 RemoveObserver(this);
229 if (audio_pref_handler_) 228 if (audio_pref_handler_)
230 audio_pref_handler_->RemoveAudioPrefObserver(this); 229 audio_pref_handler_->RemoveAudioPrefObserver(this);
231 audio_pref_handler_ = NULL; 230 audio_pref_handler_ = NULL;
232 } 231 }
233 232
234 void CrasAudioHandler::AudioClientRestarted() { 233 void CrasAudioHandler::AudioClientRestarted() {
235 SetupInitialAudioState(); 234 GetNodes();
236 } 235 }
237 236
238 void CrasAudioHandler::OutputVolumeChanged(int volume) { 237 void CrasAudioHandler::OutputVolumeChanged(int volume) {
239 if (output_volume_ == volume) 238 if (output_volume_ == volume)
240 return; 239 return;
241 240
242 output_volume_ = volume; 241 output_volume_ = volume;
243 audio_pref_handler_->SetOutputVolumeValue(output_volume_); 242 audio_pref_handler_->SetOutputVolumeValue(output_volume_);
244 FOR_EACH_OBSERVER(AudioObserver, observers_, OnOutputVolumeChanged()); 243 FOR_EACH_OBSERVER(AudioObserver, observers_, OnOutputVolumeChanged());
245 } 244 }
(...skipping 18 matching lines...) Expand all
264 void CrasAudioHandler::NodesChanged() { 263 void CrasAudioHandler::NodesChanged() {
265 // Refresh audio nodes data. 264 // Refresh audio nodes data.
266 GetNodes(); 265 GetNodes();
267 } 266 }
268 267
269 void CrasAudioHandler::ActiveOutputNodeChanged(uint64 node_id) { 268 void CrasAudioHandler::ActiveOutputNodeChanged(uint64 node_id) {
270 if (active_output_node_id_ == node_id) 269 if (active_output_node_id_ == node_id)
271 return; 270 return;
272 271
273 active_output_node_id_ = node_id; 272 active_output_node_id_ = node_id;
274 GetNodes(); 273 SetupAudioState();
275 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveOutputNodeChanged()); 274 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveOutputNodeChanged());
276 } 275 }
277 276
278 void CrasAudioHandler::ActiveInputNodeChanged(uint64 node_id) { 277 void CrasAudioHandler::ActiveInputNodeChanged(uint64 node_id) {
279 if (active_input_node_id_ == node_id) 278 if (active_input_node_id_ == node_id)
280 return; 279 return;
281 280
282 active_input_node_id_ = node_id; 281 active_input_node_id_ = node_id;
283 GetNodes();
284 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveInputNodeChanged()); 282 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveInputNodeChanged());
285 } 283 }
286 284
287 void CrasAudioHandler::OnAudioPolicyPrefChanged() { 285 void CrasAudioHandler::OnAudioPolicyPrefChanged() {
288 ApplyAudioPolicy(); 286 ApplyAudioPolicy();
289 } 287 }
290 288
291 void CrasAudioHandler::SetupInitialAudioState() { 289 void CrasAudioHandler::SetupAudioState() {
292 ApplyAudioPolicy(); 290 ApplyAudioPolicy();
293 291
294 // Set the initial audio state to the ones read from audio prefs. 292 // Set the initial audio state to the ones read from audio prefs.
295 output_mute_on_ = audio_pref_handler_->GetOutputMuteValue(); 293 output_mute_on_ = audio_pref_handler_->GetOutputMuteValue();
296 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(); 294 output_volume_ = audio_pref_handler_->GetOutputVolumeValue();
297 SetOutputVolumeInternal(output_volume_); 295 SetOutputVolumeInternal(output_volume_);
298 SetOutputMute(output_mute_on_); 296 SetOutputMute(output_mute_on_);
299
300 // Get the initial audio data.
301 GetNodes();
302 } 297 }
303 298
304 void CrasAudioHandler::ApplyAudioPolicy() { 299 void CrasAudioHandler::ApplyAudioPolicy() {
305 output_mute_locked_ = false; 300 output_mute_locked_ = false;
306 if (!audio_pref_handler_->GetAudioOutputAllowedValue()) { 301 if (!audio_pref_handler_->GetAudioOutputAllowedValue()) {
307 SetOutputMute(true); 302 SetOutputMute(true);
308 output_mute_locked_ = true; 303 output_mute_locked_ = true;
309 } 304 }
310 305
311 input_mute_locked_ = false; 306 input_mute_locked_ = false;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 device.is_input && 347 device.is_input &&
353 device.type != AUDIO_TYPE_INTERNAL_MIC) { 348 device.type != AUDIO_TYPE_INTERNAL_MIC) {
354 has_alternative_input_ = true; 349 has_alternative_input_ = true;
355 } else if (!has_alternative_output_ && 350 } else if (!has_alternative_output_ &&
356 !device.is_input && 351 !device.is_input &&
357 device.type != AUDIO_TYPE_INTERNAL_SPEAKER) { 352 device.type != AUDIO_TYPE_INTERNAL_SPEAKER) {
358 has_alternative_output_ = true; 353 has_alternative_output_ = true;
359 } 354 }
360 } 355 }
361 356
357 SetupAudioState();
362 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); 358 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged());
363 } 359 }
364 360
365 } // namespace chromeos 361 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698