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_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "chromeos/audio/audio_device.h"
15 #include "chromeos/audio/audio_pref_observer.h"
16 #include "chromeos/dbus/audio_node.h"
17 #include "chromeos/dbus/cras_audio_client.h"
18 #include "chromeos/dbus/session_manager_client.h"
19 #include "chromeos/dbus/volume_state.h"
21 class PrefRegistrySimple
;
26 class AudioDevicesPrefHandler
;
28 class CHROMEOS_EXPORT CrasAudioHandler
: public CrasAudioClient::Observer
,
29 public AudioPrefObserver
,
30 public SessionManagerClient::Observer
{
32 typedef std::priority_queue
<AudioDevice
,
33 std::vector
<AudioDevice
>,
34 AudioDeviceCompare
> AudioDevicePriorityQueue
;
35 typedef std::vector
<uint64
> NodeIdList
;
39 // Called when output volume changed.
40 virtual void OnOutputVolumeChanged();
42 // Called when output mute state changed.
43 virtual void OnOutputMuteChanged();
45 // Called when input mute state changed.
46 virtual void OnInputGainChanged();
48 // Called when input mute state changed.
49 virtual void OnInputMuteChanged();
51 // Called when audio nodes changed.
52 virtual void OnAudioNodesChanged();
54 // Called when active audio node changed.
55 virtual void OnActiveOutputNodeChanged();
57 // Called when active audio input node changed.
58 virtual void OnActiveInputNodeChanged();
62 virtual ~AudioObserver();
63 DISALLOW_COPY_AND_ASSIGN(AudioObserver
);
66 // Sets the global instance. Must be called before any calls to Get().
67 static void Initialize(
68 scoped_refptr
<AudioDevicesPrefHandler
> audio_pref_handler
);
70 // Sets the global instance for testing.
71 static void InitializeForTesting();
73 // Destroys the global instance.
74 static void Shutdown();
76 // Returns true if the global instance is initialized.
77 static bool IsInitialized();
79 // Gets the global instance. Initialize must be called first.
80 static CrasAudioHandler
* Get();
82 // Adds an audio observer.
83 virtual void AddAudioObserver(AudioObserver
* observer
);
85 // Removes an audio observer.
86 virtual void RemoveAudioObserver(AudioObserver
* observer
);
88 // Returns true if keyboard mic exists.
89 virtual bool HasKeyboardMic();
91 // Returns true if audio output is muted for the system.
92 virtual bool IsOutputMuted();
94 // Returns true if audio output is muted for a device.
95 virtual bool IsOutputMutedForDevice(uint64 device_id
);
97 // Returns true if audio input is muted.
98 virtual bool IsInputMuted();
100 // Returns true if audio input is muted for a device.
101 virtual bool IsInputMutedForDevice(uint64 device_id
);
103 // Returns true if the output volume is below the default mute volume level.
104 virtual bool IsOutputVolumeBelowDefaultMuteLevel();
106 // Returns volume level in 0-100% range at which the volume should be muted.
107 virtual int GetOutputDefaultVolumeMuteThreshold();
109 // Gets volume level in 0-100% range (0 being pure silence) for the current
111 virtual int GetOutputVolumePercent();
113 // Gets volume level in 0-100% range (0 being pure silence) for a device.
114 virtual int GetOutputVolumePercentForDevice(uint64 device_id
);
116 // Gets gain level in 0-100% range (0 being pure silence) for the current
118 virtual int GetInputGainPercent();
120 // Gets volume level in 0-100% range (0 being pure silence) for a device.
121 virtual int GetInputGainPercentForDevice(uint64 device_id
);
123 // Returns node_id of the primary active output node.
124 virtual uint64
GetPrimaryActiveOutputNode() const;
126 // Returns the node_id of the primary active input node.
127 virtual uint64
GetPrimaryActiveInputNode() const;
129 // Gets the audio devices back in |device_list|.
130 virtual void GetAudioDevices(AudioDeviceList
* device_list
) const;
132 virtual bool GetPrimaryActiveOutputDevice(AudioDevice
* device
) const;
134 // Whether there is alternative input/output audio device.
135 virtual bool has_alternative_input() const;
136 virtual bool has_alternative_output() const;
138 // Sets all active output devices' volume level to |volume_percent|, whose
139 // range is from 0-100%.
140 virtual void SetOutputVolumePercent(int volume_percent
);
142 // Sets all active input devices' gain level to |gain_percent|, whose range is
144 virtual void SetInputGainPercent(int gain_percent
);
146 // Adjusts all active output devices' volume up (positive percentage) or down
147 // (negative percentage).
148 virtual void AdjustOutputVolumeByPercent(int adjust_by_percent
);
150 // Adjusts all active output devices' volume to a minimum audible level if it
152 virtual void AdjustOutputVolumeToAudibleLevel();
154 // Mutes or unmutes audio output device.
155 virtual void SetOutputMute(bool mute_on
);
157 // Mutes or unmutes audio input device.
158 virtual void SetInputMute(bool mute_on
);
160 // Switches active audio device to |device|.
161 virtual void SwitchToDevice(const AudioDevice
& device
, bool notify
);
163 // Sets volume/gain level for a device.
164 virtual void SetVolumeGainPercentForDevice(uint64 device_id
, int value
);
166 // Sets the mute for device.
167 virtual void SetMuteForDevice(uint64 device_id
, bool mute_on
);
169 // Activates or deactivates keyboard mic if there's one.
170 virtual void SetKeyboardMicActive(bool active
);
172 // Changes the active nodes to the nodes specified by |new_active_ids|.
173 // The caller can pass in the "complete" active node list of either input
174 // nodes, or output nodes, or both. If only input nodes are passed in,
175 // it will only change the input nodes' active status, output nodes will NOT
176 // be changed; similarly for the case if only output nodes are passed.
177 // If the nodes specified in |new_active_ids| are already active, they will
178 // remain active. Otherwise, the old active nodes will be de-activated before
179 // we activate the new nodes with the same type(input/output).
180 virtual void ChangeActiveNodes(const NodeIdList
& new_active_ids
);
182 // Swaps the left and right channel of the internal speaker.
183 // Swap the left and right channel if |swap| is true; otherwise, swap the left
184 // and right channel back to the normal mode.
185 // If the feature is not supported on the device, nothing happens.
186 virtual void SwapInternalSpeakerLeftRightChannel(bool swap
);
188 // Enables error logging.
189 virtual void LogErrors();
192 explicit CrasAudioHandler(
193 scoped_refptr
<AudioDevicesPrefHandler
> audio_pref_handler
);
194 ~CrasAudioHandler() override
;
197 friend class CrasAudioHandlerTest
;
199 // CrasAudioClient::Observer overrides.
200 void AudioClientRestarted() override
;
201 void NodesChanged() override
;
202 void ActiveOutputNodeChanged(uint64 node_id
) override
;
203 void ActiveInputNodeChanged(uint64 node_id
) override
;
205 // AudioPrefObserver overrides.
206 void OnAudioPolicyPrefChanged() override
;
208 // SessionManagerClient::Observer overrides.
209 void EmitLoginPromptVisibleCalled() override
;
211 // Sets the active audio output/input node to the node with |node_id|.
212 // If |notify|, notifies Active*NodeChange.
213 void SetActiveOutputNode(uint64 node_id
, bool notify
);
214 void SetActiveInputNode(uint64 node_id
, bool notify
);
216 // Sets up the audio device state based on audio policy and audio settings
218 void SetupAudioInputState();
219 void SetupAudioOutputState();
221 // Sets up the additional active audio node's state.
222 void SetupAdditionalActiveAudioNodeState(uint64 node_id
);
224 const AudioDevice
* GetDeviceFromId(uint64 device_id
) const;
225 const AudioDevice
* GetKeyboardMic() const;
227 // Initializes audio state, which should only be called when CrasAudioHandler
228 // is created or cras audio client is restarted.
229 void InitializeAudioState();
231 // Applies the audio muting policies whenever the user logs in or policy
232 // change notification is received.
233 void ApplyAudioPolicy();
235 // Sets output volume of |node_id| to |volume|.
236 void SetOutputNodeVolume(uint64 node_id
, int volume
);
238 void SetOutputNodeVolumePercent(uint64 node_id
, int volume_percent
);
240 // Sets output mute state to |mute_on| internally, returns true if output mute
242 bool SetOutputMuteInternal(bool mute_on
);
244 // Sets input gain of |node_id| to |gain|.
245 void SetInputNodeGain(uint64 node_id
, int gain
);
247 void SetInputNodeGainPercent(uint64 node_id
, int gain_percent
);
249 // Sets input mute state to |mute_on| internally.
250 void SetInputMuteInternal(bool mute_on
);
252 // Calling dbus to get nodes data.
255 // Updates the current audio nodes list and switches the active device
257 void UpdateDevicesAndSwitchActive(const AudioNodeList
& nodes
);
259 // Returns true if *|current_active_node_id| device is changed to
260 // |new_active_device|.
261 bool ChangeActiveDevice(const AudioDevice
& new_active_device
,
262 uint64
* current_active_node_id
);
264 // Returns true if the audio nodes change is caused by some non-active
265 // audio nodes unplugged.
266 bool NonActiveDeviceUnplugged(size_t old_devices_size
,
267 size_t new_device_size
,
268 uint64 current_active_node
);
270 // Returns true if there is any device change for for input or output,
271 // specified by |is_input|.
272 // The new discovered nodes are returned in |new_discovered|.
273 bool HasDeviceChange(const AudioNodeList
& new_nodes
,
275 AudioNodeList
* new_discovered
);
277 // Handles dbus callback for GetNodes.
278 void HandleGetNodes(const chromeos::AudioNodeList
& node_list
, bool success
);
280 // Handles the dbus error callback.
281 void HandleGetNodesError(const std::string
& error_name
,
282 const std::string
& error_msg
);
284 // Adds an active node.
285 // If there is no active node, |node_id| will be switched to become the
286 // primary active node. Otherwise, it will be added as an additional active
288 void AddActiveNode(uint64 node_id
, bool notify
);
290 // Adds |node_id| into additional active nodes.
291 void AddAdditionalActiveNode(uint64 node_id
, bool notify
);
293 // Removes |node_id| from additional active nodes.
294 void RemoveActiveNodeInternal(uint64 node_id
, bool notify
);
302 // Checks if |device| is a newly discovered, changed, or existing device for
303 // the nodes sent from NodesChanged signal.
304 DeviceStatus
CheckDeviceStatus(const AudioDevice
& device
);
306 void NotifyActiveNodeChanged(bool is_input
);
308 scoped_refptr
<AudioDevicesPrefHandler
> audio_pref_handler_
;
309 ObserverList
<AudioObserver
> observers_
;
311 // Audio data and state.
312 AudioDeviceMap audio_devices_
;
314 AudioDevicePriorityQueue input_devices_pq_
;
315 AudioDevicePriorityQueue output_devices_pq_
;
317 bool output_mute_on_
;
321 uint64 active_output_node_id_
;
322 uint64 active_input_node_id_
;
323 bool has_alternative_input_
;
324 bool has_alternative_output_
;
326 bool output_mute_locked_
;
328 // Failures are not logged at startup, since CRAS may not be running yet.
331 base::WeakPtrFactory
<CrasAudioHandler
> weak_ptr_factory_
;
333 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandler
);
336 } // namespace chromeos
338 #endif // CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_