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 #ifndef CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_ | 5 #ifndef CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_ |
6 #define CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_ | 6 #define CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <queue> | 10 #include <queue> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
16 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
17 #include "chromeos/audio/audio_device.h" | 17 #include "chromeos/audio/audio_device.h" |
18 #include "chromeos/audio/audio_devices_pref_handler.h" | |
18 #include "chromeos/audio/audio_pref_observer.h" | 19 #include "chromeos/audio/audio_pref_observer.h" |
19 #include "chromeos/dbus/audio_node.h" | 20 #include "chromeos/dbus/audio_node.h" |
20 #include "chromeos/dbus/cras_audio_client.h" | 21 #include "chromeos/dbus/cras_audio_client.h" |
21 #include "chromeos/dbus/session_manager_client.h" | 22 #include "chromeos/dbus/session_manager_client.h" |
22 #include "chromeos/dbus/volume_state.h" | 23 #include "chromeos/dbus/volume_state.h" |
23 | 24 |
24 class PrefRegistrySimple; | 25 class PrefRegistrySimple; |
25 class PrefService; | 26 class PrefService; |
26 | 27 |
27 namespace chromeos { | 28 namespace chromeos { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 | 65 |
65 // Called when active audio input node changed. | 66 // Called when active audio input node changed. |
66 virtual void OnActiveInputNodeChanged(); | 67 virtual void OnActiveInputNodeChanged(); |
67 | 68 |
68 protected: | 69 protected: |
69 AudioObserver(); | 70 AudioObserver(); |
70 virtual ~AudioObserver(); | 71 virtual ~AudioObserver(); |
71 DISALLOW_COPY_AND_ASSIGN(AudioObserver); | 72 DISALLOW_COPY_AND_ASSIGN(AudioObserver); |
72 }; | 73 }; |
73 | 74 |
75 enum DeviceActivateType { | |
76 ACTIVATE_BY_PRIORITY = 0, | |
77 ACTIVATE_BY_USER, | |
78 ACTIVATE_BY_RESTORE_PREVIOUS_STATE, | |
79 }; | |
80 | |
74 // Sets the global instance. Must be called before any calls to Get(). | 81 // Sets the global instance. Must be called before any calls to Get(). |
75 static void Initialize( | 82 static void Initialize( |
76 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler); | 83 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler); |
77 | 84 |
78 // Sets the global instance for testing. | 85 // Sets the global instance for testing. |
79 static void InitializeForTesting(); | 86 static void InitializeForTesting(); |
80 | 87 |
81 // Destroys the global instance. | 88 // Destroys the global instance. |
82 static void Shutdown(); | 89 static void Shutdown(); |
83 | 90 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 // Adjusts all active output devices' volume to a minimum audible level if it | 167 // Adjusts all active output devices' volume to a minimum audible level if it |
161 // is too low. | 168 // is too low. |
162 virtual void AdjustOutputVolumeToAudibleLevel(); | 169 virtual void AdjustOutputVolumeToAudibleLevel(); |
163 | 170 |
164 // Mutes or unmutes audio output device. | 171 // Mutes or unmutes audio output device. |
165 virtual void SetOutputMute(bool mute_on); | 172 virtual void SetOutputMute(bool mute_on); |
166 | 173 |
167 // Mutes or unmutes audio input device. | 174 // Mutes or unmutes audio input device. |
168 virtual void SetInputMute(bool mute_on); | 175 virtual void SetInputMute(bool mute_on); |
169 | 176 |
170 // Switches active audio device to |device|. | 177 // Switches active audio device to |device|. |activate_by| indicates why |
171 virtual void SwitchToDevice(const AudioDevice& device, bool notify); | 178 // the device is switched to active: by user's manual choice, by priority, |
179 // or by restoring to its previous active state. | |
180 virtual void SwitchToDevice(const AudioDevice& device, | |
181 bool notify, | |
182 DeviceActivateType activate_by); | |
172 | 183 |
173 // Sets volume/gain level for a device. | 184 // Sets volume/gain level for a device. |
174 virtual void SetVolumeGainPercentForDevice(uint64_t device_id, int value); | 185 virtual void SetVolumeGainPercentForDevice(uint64_t device_id, int value); |
175 | 186 |
176 // Sets the mute for device. | 187 // Sets the mute for device. |
177 virtual void SetMuteForDevice(uint64_t device_id, bool mute_on); | 188 virtual void SetMuteForDevice(uint64_t device_id, bool mute_on); |
178 | 189 |
179 // Activates or deactivates keyboard mic if there's one. | 190 // Activates or deactivates keyboard mic if there's one. |
180 virtual void SetKeyboardMicActive(bool active); | 191 virtual void SetKeyboardMicActive(bool active); |
181 | 192 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 void NodesChanged() override; | 231 void NodesChanged() override; |
221 void ActiveOutputNodeChanged(uint64_t node_id) override; | 232 void ActiveOutputNodeChanged(uint64_t node_id) override; |
222 void ActiveInputNodeChanged(uint64_t node_id) override; | 233 void ActiveInputNodeChanged(uint64_t node_id) override; |
223 | 234 |
224 // AudioPrefObserver overrides. | 235 // AudioPrefObserver overrides. |
225 void OnAudioPolicyPrefChanged() override; | 236 void OnAudioPolicyPrefChanged() override; |
226 | 237 |
227 // SessionManagerClient::Observer overrides. | 238 // SessionManagerClient::Observer overrides. |
228 void EmitLoginPromptVisibleCalled() override; | 239 void EmitLoginPromptVisibleCalled() override; |
229 | 240 |
230 // Sets the active audio output/input node to the node with |node_id|. | 241 // Sets the |active_device| to be active. |
231 // If |notify|, notifies Active*NodeChange. | 242 // If |notify|, notifies Active*NodeChange. |
232 void SetActiveOutputNode(uint64_t node_id, bool notify); | 243 // Saves device active states in prefs. |activate_by| indicates how |
233 void SetActiveInputNode(uint64_t node_id, bool notify); | 244 // the device was activated. |
245 void SetActiveDevice(const AudioDevice& active_device, | |
246 bool notify, | |
247 DeviceActivateType activate_by); | |
248 | |
249 // Saves |device|'s state in pref. If |active| is true, |activate_by| | |
250 // indicates how |device| is activated. | |
251 void SaveDeviceState(const AudioDevice& device, | |
252 bool active, | |
253 DeviceActivateType activate_by); | |
234 | 254 |
235 // Sets up the audio device state based on audio policy and audio settings | 255 // Sets up the audio device state based on audio policy and audio settings |
236 // saved in prefs. | 256 // saved in prefs. |
237 void SetupAudioInputState(); | 257 void SetupAudioInputState(); |
238 void SetupAudioOutputState(); | 258 void SetupAudioOutputState(); |
239 | 259 |
240 // Sets up the additional active audio node's state. | 260 // Sets up the additional active audio node's state. |
241 void SetupAdditionalActiveAudioNodeState(uint64_t node_id); | 261 void SetupAdditionalActiveAudioNodeState(uint64_t node_id); |
242 | 262 |
243 const AudioDevice* GetDeviceFromId(uint64_t device_id) const; | 263 const AudioDevice* GetDeviceFromId(uint64_t device_id) const; |
264 const AudioDevice* GetDeviceFromStableDeviceId( | |
265 uint64_t stable_device_id) const; | |
244 const AudioDevice* GetKeyboardMic() const; | 266 const AudioDevice* GetKeyboardMic() const; |
245 | 267 |
246 // Initializes audio state, which should only be called when CrasAudioHandler | 268 // Initializes audio state, which should only be called when CrasAudioHandler |
247 // is created or cras audio client is restarted. | 269 // is created or cras audio client is restarted. |
248 void InitializeAudioState(); | 270 void InitializeAudioState(); |
249 | 271 |
272 void InitializeAudioAfterCrasServiceAvailable(bool service_is_available); | |
273 | |
250 // Applies the audio muting policies whenever the user logs in or policy | 274 // Applies the audio muting policies whenever the user logs in or policy |
251 // change notification is received. | 275 // change notification is received. |
252 void ApplyAudioPolicy(); | 276 void ApplyAudioPolicy(); |
253 | 277 |
254 // Sets output volume of |node_id| to |volume|. | 278 // Sets output volume of |node_id| to |volume|. |
255 void SetOutputNodeVolume(uint64_t node_id, int volume); | 279 void SetOutputNodeVolume(uint64_t node_id, int volume); |
256 | 280 |
257 void SetOutputNodeVolumePercent(uint64_t node_id, int volume_percent); | 281 void SetOutputNodeVolumePercent(uint64_t node_id, int volume_percent); |
258 | 282 |
259 // Sets output mute state to |mute_on| internally, returns true if output mute | 283 // Sets output mute state to |mute_on| internally, returns true if output mute |
260 // is set. | 284 // is set. |
261 bool SetOutputMuteInternal(bool mute_on); | 285 bool SetOutputMuteInternal(bool mute_on); |
262 | 286 |
263 // Sets input gain of |node_id| to |gain|. | 287 // Sets input gain of |node_id| to |gain|. |
264 void SetInputNodeGain(uint64_t node_id, int gain); | 288 void SetInputNodeGain(uint64_t node_id, int gain); |
265 | 289 |
266 void SetInputNodeGainPercent(uint64_t node_id, int gain_percent); | 290 void SetInputNodeGainPercent(uint64_t node_id, int gain_percent); |
267 | 291 |
268 // Sets input mute state to |mute_on| internally. | 292 // Sets input mute state to |mute_on| internally. |
269 void SetInputMuteInternal(bool mute_on); | 293 void SetInputMuteInternal(bool mute_on); |
270 | 294 |
271 // Calling dbus to get nodes data. | 295 // Calling dbus to get nodes data. |
272 void GetNodes(); | 296 void GetNodes(); |
273 | 297 |
274 // Updates the current audio nodes list and switches the active device | 298 // Updates the current audio nodes list and switches the active device |
275 // if needed. | 299 // if needed. |
276 void UpdateDevicesAndSwitchActive(const AudioNodeList& nodes); | 300 void UpdateDevicesAndSwitchActive(const AudioNodeList& nodes); |
277 | 301 |
278 // Returns true if *|current_active_node_id| device is changed to | 302 // Returns true if the current active device is changed to |
279 // |new_active_device|. | 303 // |new_active_device|. |
280 bool ChangeActiveDevice(const AudioDevice& new_active_device, | 304 bool ChangeActiveDevice(const AudioDevice& new_active_device); |
281 uint64_t* current_active_node_id); | |
282 | 305 |
283 // Returns true if the audio nodes change is caused by some non-active | 306 // Returns true if there are any device changes for input or output |
284 // audio nodes unplugged. | 307 // specified by |is_input|, by comparing |audio_devvices_| with |new_nodes|. |
hychao
2016/03/02 15:34:05
s/audio_devvices_/audio_devices_/
jennyz
2016/03/02 19:33:37
Done.
| |
285 bool NonActiveDeviceUnplugged(size_t old_devices_size, | 308 // Passes the new nodes discovered in *|new_discovered|. |
286 size_t new_device_size, | 309 // *|device_removed| indicates if any devices have been removed. |
287 uint64_t current_active_node); | 310 // *|active_device_removed| indicates if the current active device has been |
288 | 311 // removed. |
289 // Returns true if there is any device change for for input or output, | |
290 // specified by |is_input|. | |
291 // The new discovered nodes are returned in |new_discovered|. | |
292 bool HasDeviceChange(const AudioNodeList& new_nodes, | 312 bool HasDeviceChange(const AudioNodeList& new_nodes, |
293 bool is_input, | 313 bool is_input, |
294 AudioDevicePriorityQueue* new_discovered); | 314 AudioDevicePriorityQueue* new_discovered, |
315 bool* device_removed, | |
316 bool* active_device_removed); | |
295 | 317 |
296 // Handles dbus callback for GetNodes. | 318 // Handles dbus callback for GetNodes. |
297 void HandleGetNodes(const chromeos::AudioNodeList& node_list, bool success); | 319 void HandleGetNodes(const chromeos::AudioNodeList& node_list, bool success); |
298 | 320 |
299 // Handles the dbus error callback. | 321 // Handles the dbus error callback. |
300 void HandleGetNodesError(const std::string& error_name, | 322 void HandleGetNodesError(const std::string& error_name, |
301 const std::string& error_msg); | 323 const std::string& error_msg); |
302 | 324 |
303 // Adds an active node. | 325 // Adds an active node. |
304 // If there is no active node, |node_id| will be switched to become the | 326 // If there is no active node, |node_id| will be switched to become the |
(...skipping 22 matching lines...) Expand all Loading... | |
327 NEW_DEVICE, | 349 NEW_DEVICE, |
328 CHANGED_DEVICE, | 350 CHANGED_DEVICE, |
329 }; | 351 }; |
330 | 352 |
331 // Checks if |device| is a newly discovered, changed, or existing device for | 353 // Checks if |device| is a newly discovered, changed, or existing device for |
332 // the nodes sent from NodesChanged signal. | 354 // the nodes sent from NodesChanged signal. |
333 DeviceStatus CheckDeviceStatus(const AudioDevice& device); | 355 DeviceStatus CheckDeviceStatus(const AudioDevice& device); |
334 | 356 |
335 void NotifyActiveNodeChanged(bool is_input); | 357 void NotifyActiveNodeChanged(bool is_input); |
336 | 358 |
359 // Returns true if it retrieves an active audio device from user preference | |
360 // among the current |audio_devices_|. | |
361 bool GetActiveDeviceFromUserPref(bool is_input, AudioDevice* device); | |
362 | |
363 // Handles either input or output device changes, specified by |is_input|. | |
364 void HandleAudioDeviceChange(bool is_input, | |
365 const AudioDevicePriorityQueue& devices_pq, | |
366 const AudioDevicePriorityQueue& hotplug_nodes, | |
367 bool has_device_change, | |
368 bool has_device_removed, | |
369 bool active_device_removed); | |
370 | |
371 // Handles non-hotplug nodes change cases. | |
372 void HandleNonHotplugNodesChange( | |
373 bool is_input, | |
374 const AudioDevicePriorityQueue& hotplug_nodes, | |
375 bool has_device_change, | |
376 bool has_device_removed, | |
377 bool active_device_removed); | |
378 | |
379 // Handles the regular user hotplug case. | |
380 void HandleHotPlugDevice( | |
381 const AudioDevice& hotplug_device, | |
382 const AudioDevicePriorityQueue& device_priority_queue); | |
383 | |
384 void SwitchToTopPriorityDevice(bool is_input); | |
385 | |
386 // Switch to previous active device if it is found, otherwise, switch | |
387 // to the top priority device. | |
388 void SwitchToPreviousActiveDeviceIfAvailable(bool is_input); | |
389 | |
337 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler_; | 390 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler_; |
338 base::ObserverList<AudioObserver> observers_; | 391 base::ObserverList<AudioObserver> observers_; |
339 | 392 |
340 // Audio data and state. | 393 // Audio data and state. |
341 AudioDeviceMap audio_devices_; | 394 AudioDeviceMap audio_devices_; |
342 | 395 |
343 AudioDevicePriorityQueue input_devices_pq_; | 396 AudioDevicePriorityQueue input_devices_pq_; |
344 AudioDevicePriorityQueue output_devices_pq_; | 397 AudioDevicePriorityQueue output_devices_pq_; |
345 | 398 |
346 bool output_mute_on_; | 399 bool output_mute_on_; |
347 bool input_mute_on_; | 400 bool input_mute_on_; |
348 int output_volume_; | 401 int output_volume_; |
349 int input_gain_; | 402 int input_gain_; |
350 uint64_t active_output_node_id_; | 403 uint64_t active_output_node_id_; |
351 uint64_t active_input_node_id_; | 404 uint64_t active_input_node_id_; |
352 bool has_alternative_input_; | 405 bool has_alternative_input_; |
353 bool has_alternative_output_; | 406 bool has_alternative_output_; |
354 | 407 |
355 bool output_mute_locked_; | 408 bool output_mute_locked_; |
356 | 409 |
357 // Failures are not logged at startup, since CRAS may not be running yet. | 410 // Failures are not logged at startup, since CRAS may not be running yet. |
358 bool log_errors_; | 411 bool log_errors_; |
359 | 412 |
360 // Timer for HDMI re-discovering grace period. | 413 // Timer for HDMI re-discovering grace period. |
361 base::OneShotTimer hdmi_rediscover_timer_; | 414 base::OneShotTimer hdmi_rediscover_timer_; |
362 int hdmi_rediscover_grace_period_duration_in_ms_; | 415 int hdmi_rediscover_grace_period_duration_in_ms_; |
363 bool hdmi_rediscovering_; | 416 bool hdmi_rediscovering_; |
364 | 417 |
418 bool cras_service_available_ = false; | |
419 | |
365 base::WeakPtrFactory<CrasAudioHandler> weak_ptr_factory_; | 420 base::WeakPtrFactory<CrasAudioHandler> weak_ptr_factory_; |
366 | 421 |
367 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandler); | 422 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandler); |
368 }; | 423 }; |
369 | 424 |
370 } // namespace chromeos | 425 } // namespace chromeos |
371 | 426 |
372 #endif // CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_ | 427 #endif // CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_ |
OLD | NEW |