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
3 // found in the LICENSE file.
5 #ifndef CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_
6 #define CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/timer/timer.h"
15 #include "chromeos/audio/audio_device.h"
16 #include "chromeos/audio/audio_pref_observer.h"
17 #include "chromeos/dbus/audio_node.h"
18 #include "chromeos/dbus/cras_audio_client.h"
19 #include "chromeos/dbus/session_manager_client.h"
20 #include "chromeos/dbus/volume_state.h"
22 class PrefRegistrySimple
;
27 class AudioDevicesPrefHandler
;
29 class CHROMEOS_EXPORT CrasAudioHandler
: public CrasAudioClient::Observer
,
30 public AudioPrefObserver
,
31 public SessionManagerClient::Observer
{
33 typedef std::priority_queue
<AudioDevice
,
34 std::vector
<AudioDevice
>,
35 AudioDeviceCompare
> AudioDevicePriorityQueue
;
36 typedef std::vector
<uint64_t> NodeIdList
;
40 // Called when an active output volume changed.
41 virtual void OnOutputNodeVolumeChanged(uint64_t node_id
, int volume
);
43 // Called when output mute state changed.
44 virtual void OnOutputMuteChanged(bool mute_on
);
46 // Called when active input node's gain changed.
47 virtual void OnInputNodeGainChanged(uint64_t node_id
, int gain
);
49 // Called when input mute state changed.
50 virtual void OnInputMuteChanged(bool mute_on
);
52 // Called when audio nodes changed.
53 virtual void OnAudioNodesChanged();
55 // Called when active audio node changed.
56 virtual void OnActiveOutputNodeChanged();
58 // Called when active audio input node changed.
59 virtual void OnActiveInputNodeChanged();
63 virtual ~AudioObserver();
64 DISALLOW_COPY_AND_ASSIGN(AudioObserver
);
67 // Sets the global instance. Must be called before any calls to Get().
68 static void Initialize(
69 scoped_refptr
<AudioDevicesPrefHandler
> audio_pref_handler
);
71 // Sets the global instance for testing.
72 static void InitializeForTesting();
74 // Destroys the global instance.
75 static void Shutdown();
77 // Returns true if the global instance is initialized.
78 static bool IsInitialized();
80 // Gets the global instance. Initialize must be called first.
81 static CrasAudioHandler
* Get();
83 // Adds an audio observer.
84 virtual void AddAudioObserver(AudioObserver
* observer
);
86 // Removes an audio observer.
87 virtual void RemoveAudioObserver(AudioObserver
* observer
);
89 // Returns true if keyboard mic exists.
90 virtual bool HasKeyboardMic();
92 // Returns true if audio output is muted for the system.
93 virtual bool IsOutputMuted();
95 // Returns true if audio output is muted for a device.
96 virtual bool IsOutputMutedForDevice(uint64_t device_id
);
98 // Returns true if audio input is muted.
99 virtual bool IsInputMuted();
101 // Returns true if audio input is muted for a device.
102 virtual bool IsInputMutedForDevice(uint64_t device_id
);
104 // Returns true if the output volume is below the default mute volume level.
105 virtual bool IsOutputVolumeBelowDefaultMuteLevel();
107 // Returns volume level in 0-100% range at which the volume should be muted.
108 virtual int GetOutputDefaultVolumeMuteThreshold();
110 // Gets volume level in 0-100% range (0 being pure silence) for the current
112 virtual int GetOutputVolumePercent();
114 // Gets volume level in 0-100% range (0 being pure silence) for a device.
115 virtual int GetOutputVolumePercentForDevice(uint64_t device_id
);
117 // Gets gain level in 0-100% range (0 being pure silence) for the current
119 virtual int GetInputGainPercent();
121 // Gets volume level in 0-100% range (0 being pure silence) for a device.
122 virtual int GetInputGainPercentForDevice(uint64_t device_id
);
124 // Returns node_id of the primary active output node.
125 virtual uint64_t GetPrimaryActiveOutputNode() const;
127 // Returns the node_id of the primary active input node.
128 virtual uint64_t GetPrimaryActiveInputNode() const;
130 // Gets the audio devices back in |device_list|.
131 virtual void GetAudioDevices(AudioDeviceList
* device_list
) const;
133 virtual bool GetPrimaryActiveOutputDevice(AudioDevice
* device
) const;
135 // Whether there is alternative input/output audio device.
136 virtual bool has_alternative_input() const;
137 virtual bool has_alternative_output() const;
139 // Sets all active output devices' volume level to |volume_percent|, whose
140 // range is from 0-100%.
141 virtual void SetOutputVolumePercent(int volume_percent
);
143 // Sets all active input devices' gain level to |gain_percent|, whose range is
145 virtual void SetInputGainPercent(int gain_percent
);
147 // Adjusts all active output devices' volume up (positive percentage) or down
148 // (negative percentage).
149 virtual void AdjustOutputVolumeByPercent(int adjust_by_percent
);
151 // Adjusts all active output devices' volume to a minimum audible level if it
153 virtual void AdjustOutputVolumeToAudibleLevel();
155 // Mutes or unmutes audio output device.
156 virtual void SetOutputMute(bool mute_on
);
158 // Mutes or unmutes audio input device.
159 virtual void SetInputMute(bool mute_on
);
161 // Switches active audio device to |device|.
162 virtual void SwitchToDevice(const AudioDevice
& device
, bool notify
);
164 // Sets volume/gain level for a device.
165 virtual void SetVolumeGainPercentForDevice(uint64_t device_id
, int value
);
167 // Sets the mute for device.
168 virtual void SetMuteForDevice(uint64_t device_id
, bool mute_on
);
170 // Activates or deactivates keyboard mic if there's one.
171 virtual void SetKeyboardMicActive(bool active
);
173 // Changes the active nodes to the nodes specified by |new_active_ids|.
174 // The caller can pass in the "complete" active node list of either input
175 // nodes, or output nodes, or both. If only input nodes are passed in,
176 // it will only change the input nodes' active status, output nodes will NOT
177 // be changed; similarly for the case if only output nodes are passed.
178 // If the nodes specified in |new_active_ids| are already active, they will
179 // remain active. Otherwise, the old active nodes will be de-activated before
180 // we activate the new nodes with the same type(input/output).
181 virtual void ChangeActiveNodes(const NodeIdList
& new_active_ids
);
183 // Swaps the left and right channel of the internal speaker.
184 // Swap the left and right channel if |swap| is true; otherwise, swap the left
185 // and right channel back to the normal mode.
186 // If the feature is not supported on the device, nothing happens.
187 virtual void SwapInternalSpeakerLeftRightChannel(bool swap
);
189 // Enables error logging.
190 virtual void LogErrors();
192 // If necessary, sets the starting point for re-discovering the active HDMI
193 // output device caused by device entering/exiting docking mode, HDMI display
194 // changing resolution, or chromeos device suspend/resume. If
195 // |force_rediscovering| is true, it will force to set the starting point for
196 // re-discovering the active HDMI output device again if it has been in the
197 // middle of rediscovering the HDMI active output device.
198 virtual void SetActiveHDMIOutoutRediscoveringIfNecessary(
199 bool force_rediscovering
);
202 explicit CrasAudioHandler(
203 scoped_refptr
<AudioDevicesPrefHandler
> audio_pref_handler
);
204 ~CrasAudioHandler() override
;
207 friend class CrasAudioHandlerTest
;
209 // CrasAudioClient::Observer overrides.
210 void AudioClientRestarted() override
;
211 void NodesChanged() override
;
212 void ActiveOutputNodeChanged(uint64_t node_id
) override
;
213 void ActiveInputNodeChanged(uint64_t node_id
) override
;
215 // AudioPrefObserver overrides.
216 void OnAudioPolicyPrefChanged() override
;
218 // SessionManagerClient::Observer overrides.
219 void EmitLoginPromptVisibleCalled() override
;
221 // Sets the active audio output/input node to the node with |node_id|.
222 // If |notify|, notifies Active*NodeChange.
223 void SetActiveOutputNode(uint64_t node_id
, bool notify
);
224 void SetActiveInputNode(uint64_t node_id
, bool notify
);
226 // Sets up the audio device state based on audio policy and audio settings
228 void SetupAudioInputState();
229 void SetupAudioOutputState();
231 // Sets up the additional active audio node's state.
232 void SetupAdditionalActiveAudioNodeState(uint64_t node_id
);
234 const AudioDevice
* GetDeviceFromId(uint64_t device_id
) const;
235 const AudioDevice
* GetKeyboardMic() const;
237 // Initializes audio state, which should only be called when CrasAudioHandler
238 // is created or cras audio client is restarted.
239 void InitializeAudioState();
241 // Applies the audio muting policies whenever the user logs in or policy
242 // change notification is received.
243 void ApplyAudioPolicy();
245 // Sets output volume of |node_id| to |volume|.
246 void SetOutputNodeVolume(uint64_t node_id
, int volume
);
248 void SetOutputNodeVolumePercent(uint64_t node_id
, int volume_percent
);
250 // Sets output mute state to |mute_on| internally, returns true if output mute
252 bool SetOutputMuteInternal(bool mute_on
);
254 // Sets input gain of |node_id| to |gain|.
255 void SetInputNodeGain(uint64_t node_id
, int gain
);
257 void SetInputNodeGainPercent(uint64_t node_id
, int gain_percent
);
259 // Sets input mute state to |mute_on| internally.
260 void SetInputMuteInternal(bool mute_on
);
262 // Calling dbus to get nodes data.
265 // Updates the current audio nodes list and switches the active device
267 void UpdateDevicesAndSwitchActive(const AudioNodeList
& nodes
);
269 // Returns true if *|current_active_node_id| device is changed to
270 // |new_active_device|.
271 bool ChangeActiveDevice(const AudioDevice
& new_active_device
,
272 uint64_t* current_active_node_id
);
274 // Returns true if the audio nodes change is caused by some non-active
275 // audio nodes unplugged.
276 bool NonActiveDeviceUnplugged(size_t old_devices_size
,
277 size_t new_device_size
,
278 uint64_t current_active_node
);
280 // Returns true if there is any device change for for input or output,
281 // specified by |is_input|.
282 // The new discovered nodes are returned in |new_discovered|.
283 bool HasDeviceChange(const AudioNodeList
& new_nodes
,
285 AudioNodeList
* new_discovered
);
287 // Handles dbus callback for GetNodes.
288 void HandleGetNodes(const chromeos::AudioNodeList
& node_list
, bool success
);
290 // Handles the dbus error callback.
291 void HandleGetNodesError(const std::string
& error_name
,
292 const std::string
& error_msg
);
294 // Adds an active node.
295 // If there is no active node, |node_id| will be switched to become the
296 // primary active node. Otherwise, it will be added as an additional active
298 void AddActiveNode(uint64_t node_id
, bool notify
);
300 // Adds |node_id| into additional active nodes.
301 void AddAdditionalActiveNode(uint64_t node_id
, bool notify
);
303 // Removes |node_id| from additional active nodes.
304 void RemoveActiveNodeInternal(uint64_t node_id
, bool notify
);
306 void UpdateAudioAfterHDMIRediscoverGracePeriod();
308 bool IsHDMIPrimaryOutputDevice() const;
310 void StartHDMIRediscoverGracePeriod();
312 bool hdmi_rediscovering() const { return hdmi_rediscovering_
; }
314 void SetHDMIRediscoverGracePeriodForTesting(int duration_in_ms
);
322 // Checks if |device| is a newly discovered, changed, or existing device for
323 // the nodes sent from NodesChanged signal.
324 DeviceStatus
CheckDeviceStatus(const AudioDevice
& device
);
326 void NotifyActiveNodeChanged(bool is_input
);
328 scoped_refptr
<AudioDevicesPrefHandler
> audio_pref_handler_
;
329 base::ObserverList
<AudioObserver
> observers_
;
331 // Audio data and state.
332 AudioDeviceMap audio_devices_
;
334 AudioDevicePriorityQueue input_devices_pq_
;
335 AudioDevicePriorityQueue output_devices_pq_
;
337 bool output_mute_on_
;
341 uint64_t active_output_node_id_
;
342 uint64_t active_input_node_id_
;
343 bool has_alternative_input_
;
344 bool has_alternative_output_
;
346 bool output_mute_locked_
;
348 // Failures are not logged at startup, since CRAS may not be running yet.
351 // Timer for HDMI re-discovering grace period.
352 base::OneShotTimer
<CrasAudioHandler
> hdmi_rediscover_timer_
;
353 int hdmi_rediscover_grace_period_duration_in_ms_
;
354 bool hdmi_rediscovering_
;
356 base::WeakPtrFactory
<CrasAudioHandler
> weak_ptr_factory_
;
358 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandler
);
361 } // namespace chromeos
363 #endif // CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_