OLD | NEW |
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_pref_handler.h" |
| 14 #include "chromeos/audio/mock_cras_audio_handler.h" |
14 #include "chromeos/dbus/dbus_thread_manager.h" | 15 #include "chromeos/dbus/dbus_thread_manager.h" |
15 | 16 |
16 using std::max; | 17 using std::max; |
17 using std::min; | 18 using std::min; |
18 | 19 |
19 namespace chromeos { | 20 namespace chromeos { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 // Default value for unmuting, as a percent in the range [0, 100]. | 24 // Default value for unmuting, as a percent in the range [0, 100]. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 } | 57 } |
57 | 58 |
58 // static | 59 // static |
59 void CrasAudioHandler::Initialize( | 60 void CrasAudioHandler::Initialize( |
60 scoped_refptr<AudioPrefHandler> audio_pref_handler) { | 61 scoped_refptr<AudioPrefHandler> audio_pref_handler) { |
61 CHECK(!g_cras_audio_handler); | 62 CHECK(!g_cras_audio_handler); |
62 g_cras_audio_handler = new CrasAudioHandler(audio_pref_handler); | 63 g_cras_audio_handler = new CrasAudioHandler(audio_pref_handler); |
63 } | 64 } |
64 | 65 |
65 // static | 66 // static |
| 67 void CrasAudioHandler::InitializeForTesting() { |
| 68 CHECK(!g_cras_audio_handler); |
| 69 g_cras_audio_handler = new MockCrasAudioHandler(); |
| 70 } |
| 71 |
| 72 // static |
66 void CrasAudioHandler::Shutdown() { | 73 void CrasAudioHandler::Shutdown() { |
67 CHECK(g_cras_audio_handler); | 74 CHECK(g_cras_audio_handler); |
68 delete g_cras_audio_handler; | 75 delete g_cras_audio_handler; |
69 g_cras_audio_handler = NULL; | 76 g_cras_audio_handler = NULL; |
70 } | 77 } |
71 | 78 |
72 // static | 79 // static |
73 bool CrasAudioHandler::IsInitialized() { | 80 bool CrasAudioHandler::IsInitialized() { |
74 return g_cras_audio_handler != NULL; | 81 return g_cras_audio_handler != NULL; |
75 } | 82 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 for (size_t i = 0; i < audio_devices_.size(); ++i) { | 125 for (size_t i = 0; i < audio_devices_.size(); ++i) { |
119 if (audio_devices_[i].id == active_output_node_id_) { | 126 if (audio_devices_[i].id == active_output_node_id_) { |
120 *device = audio_devices_[i]; | 127 *device = audio_devices_[i]; |
121 return true; | 128 return true; |
122 } | 129 } |
123 } | 130 } |
124 NOTREACHED() << "Can't find active output audio device"; | 131 NOTREACHED() << "Can't find active output audio device"; |
125 return false; | 132 return false; |
126 } | 133 } |
127 | 134 |
| 135 bool CrasAudioHandler::has_alternative_input() const { |
| 136 return has_alternative_input_; |
| 137 } |
| 138 |
| 139 bool CrasAudioHandler::has_alternative_output() const { |
| 140 return has_alternative_output_; |
| 141 } |
| 142 |
128 void CrasAudioHandler::SetOutputVolumePercent(int volume_percent) { | 143 void CrasAudioHandler::SetOutputVolumePercent(int volume_percent) { |
129 volume_percent = min(max(volume_percent, 0), 100); | 144 volume_percent = min(max(volume_percent, 0), 100); |
130 if (volume_percent <= kMuteThresholdPercent) | 145 if (volume_percent <= kMuteThresholdPercent) |
131 volume_percent = 0; | 146 volume_percent = 0; |
132 SetOutputVolumeInternal(volume_percent); | 147 SetOutputVolumeInternal(volume_percent); |
133 if (IsOutputMuted() && volume_percent > 0) | 148 if (IsOutputMuted() && volume_percent > 0) |
134 SetOutputMute(false); | 149 SetOutputMute(false); |
135 if (!IsOutputMuted() && volume_percent == 0) | 150 if (!IsOutputMuted() && volume_percent == 0) |
136 SetOutputMute(true); | 151 SetOutputMute(true); |
137 } | 152 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 weak_ptr_factory_(this), | 199 weak_ptr_factory_(this), |
185 output_mute_on_(false), | 200 output_mute_on_(false), |
186 input_mute_on_(false), | 201 input_mute_on_(false), |
187 output_volume_(0), | 202 output_volume_(0), |
188 active_output_node_id_(0), | 203 active_output_node_id_(0), |
189 active_input_node_id_(0), | 204 active_input_node_id_(0), |
190 has_alternative_input_(false), | 205 has_alternative_input_(false), |
191 has_alternative_output_(false), | 206 has_alternative_output_(false), |
192 output_mute_locked_(false), | 207 output_mute_locked_(false), |
193 input_mute_locked_(false) { | 208 input_mute_locked_(false) { |
| 209 if (!audio_pref_handler) |
| 210 return; |
| 211 // If the DBusThreadManager or the CrasAudioClient aren't available, there |
| 212 // isn't much we can do. This should only happen when running tests. |
| 213 if (!chromeos::DBusThreadManager::IsInitialized() || |
| 214 !chromeos::DBusThreadManager::Get() || |
| 215 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) |
| 216 return; |
194 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->AddObserver(this); | 217 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->AddObserver(this); |
195 DCHECK(audio_pref_handler_.get()); | |
196 audio_pref_handler_->AddAudioPrefObserver(this); | 218 audio_pref_handler_->AddAudioPrefObserver(this); |
197 SetupInitialAudioState(); | 219 SetupInitialAudioState(); |
198 } | 220 } |
199 | 221 |
200 CrasAudioHandler::~CrasAudioHandler() { | 222 CrasAudioHandler::~CrasAudioHandler() { |
| 223 if (!chromeos::DBusThreadManager::IsInitialized() || |
| 224 !chromeos::DBusThreadManager::Get() || |
| 225 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) |
| 226 return; |
201 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 227 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
202 RemoveObserver(this); | 228 RemoveObserver(this); |
203 audio_pref_handler_->RemoveAudioPrefObserver(this); | 229 if (audio_pref_handler_) |
| 230 audio_pref_handler_->RemoveAudioPrefObserver(this); |
204 audio_pref_handler_ = NULL; | 231 audio_pref_handler_ = NULL; |
205 } | 232 } |
206 | 233 |
207 void CrasAudioHandler::AudioClientRestarted() { | 234 void CrasAudioHandler::AudioClientRestarted() { |
208 SetupInitialAudioState(); | 235 SetupInitialAudioState(); |
209 } | 236 } |
210 | 237 |
211 void CrasAudioHandler::OutputVolumeChanged(int volume) { | 238 void CrasAudioHandler::OutputVolumeChanged(int volume) { |
212 if (output_volume_ == volume) | 239 if (output_volume_ == volume) |
213 return; | 240 return; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 !device.is_input && | 356 !device.is_input && |
330 device.type != AUDIO_TYPE_INTERNAL_SPEAKER) { | 357 device.type != AUDIO_TYPE_INTERNAL_SPEAKER) { |
331 has_alternative_output_ = true; | 358 has_alternative_output_ = true; |
332 } | 359 } |
333 } | 360 } |
334 | 361 |
335 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); | 362 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); |
336 } | 363 } |
337 | 364 |
338 } // namespace chromeos | 365 } // namespace chromeos |
OLD | NEW |