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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 weak_ptr_factory_(this), | 191 weak_ptr_factory_(this), |
185 output_mute_on_(false), | 192 output_mute_on_(false), |
186 input_mute_on_(false), | 193 input_mute_on_(false), |
187 output_volume_(0), | 194 output_volume_(0), |
188 active_output_node_id_(0), | 195 active_output_node_id_(0), |
189 active_input_node_id_(0), | 196 active_input_node_id_(0), |
190 has_alternative_input_(false), | 197 has_alternative_input_(false), |
191 has_alternative_output_(false), | 198 has_alternative_output_(false), |
192 output_mute_locked_(false), | 199 output_mute_locked_(false), |
193 input_mute_locked_(false) { | 200 input_mute_locked_(false) { |
201 if (!audio_pref_handler) | |
202 return; | |
203 // If the DBusThreadManager or the CrasAudioClient aren't available, there | |
204 // isn't much we can do. This should only happen when running tests. | |
205 if (!chromeos::DBusThreadManager::IsInitialized() || | |
206 !chromeos::DBusThreadManager::Get() || | |
207 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) | |
208 return; | |
194 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->AddObserver(this); | 209 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->AddObserver(this); |
195 DCHECK(audio_pref_handler_.get()); | |
196 audio_pref_handler_->AddAudioPrefObserver(this); | 210 audio_pref_handler_->AddAudioPrefObserver(this); |
197 SetupInitialAudioState(); | 211 SetupInitialAudioState(); |
198 } | 212 } |
199 | 213 |
200 CrasAudioHandler::~CrasAudioHandler() { | 214 CrasAudioHandler::~CrasAudioHandler() { |
215 if (!chromeos::DBusThreadManager::IsInitialized() || | |
James Cook
2013/04/30 23:31:21
Do you need to check audio_pref_handler_ here?
rkc
2013/04/30 23:36:37
Yeah; in case we're running in a test, we also nee
| |
216 !chromeos::DBusThreadManager::Get() || | |
217 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) | |
218 return; | |
201 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 219 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
202 RemoveObserver(this); | 220 RemoveObserver(this); |
203 audio_pref_handler_->RemoveAudioPrefObserver(this); | 221 audio_pref_handler_->RemoveAudioPrefObserver(this); |
204 audio_pref_handler_ = NULL; | 222 audio_pref_handler_ = NULL; |
205 } | 223 } |
206 | 224 |
207 void CrasAudioHandler::AudioClientRestarted() { | 225 void CrasAudioHandler::AudioClientRestarted() { |
208 SetupInitialAudioState(); | 226 SetupInitialAudioState(); |
209 } | 227 } |
210 | 228 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
329 !device.is_input && | 347 !device.is_input && |
330 device.type != AUDIO_TYPE_INTERNAL_SPEAKER) { | 348 device.type != AUDIO_TYPE_INTERNAL_SPEAKER) { |
331 has_alternative_output_ = true; | 349 has_alternative_output_ = true; |
332 } | 350 } |
333 } | 351 } |
334 | 352 |
335 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); | 353 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); |
336 } | 354 } |
337 | 355 |
338 } // namespace chromeos | 356 } // namespace chromeos |
OLD | NEW |