Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chromeos / audio / cras_audio_handler.h
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.
4
5 #ifndef CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_
6 #define CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_
7
8 #include <queue>
9
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"
20
21 class PrefRegistrySimple;
22 class PrefService;
23
24 namespace chromeos {
25
26 class AudioDevicesPrefHandler;
27
28 class CHROMEOS_EXPORT CrasAudioHandler : public CrasAudioClient::Observer,
29                                          public AudioPrefObserver,
30                                          public SessionManagerClient::Observer {
31  public:
32   typedef std::priority_queue<AudioDevice,
33                               std::vector<AudioDevice>,
34                               AudioDeviceCompare> AudioDevicePriorityQueue;
35   typedef std::vector<uint64> NodeIdList;
36
37   class AudioObserver {
38    public:
39     // Called when output volume changed.
40     virtual void OnOutputVolumeChanged();
41
42     // Called when output mute state changed.
43     virtual void OnOutputMuteChanged();
44
45     // Called when input mute state changed.
46     virtual void OnInputGainChanged();
47
48     // Called when input mute state changed.
49     virtual void OnInputMuteChanged();
50
51     // Called when audio nodes changed.
52     virtual void OnAudioNodesChanged();
53
54     // Called when active audio node changed.
55     virtual void OnActiveOutputNodeChanged();
56
57     // Called when active audio input node changed.
58     virtual void OnActiveInputNodeChanged();
59
60    protected:
61     AudioObserver();
62     virtual ~AudioObserver();
63     DISALLOW_COPY_AND_ASSIGN(AudioObserver);
64   };
65
66   // Sets the global instance. Must be called before any calls to Get().
67   static void Initialize(
68       scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler);
69
70   // Sets the global instance for testing.
71   static void InitializeForTesting();
72
73   // Destroys the global instance.
74   static void Shutdown();
75
76   // Returns true if the global instance is initialized.
77   static bool IsInitialized();
78
79   // Gets the global instance. Initialize must be called first.
80   static CrasAudioHandler* Get();
81
82   // Adds an audio observer.
83   virtual void AddAudioObserver(AudioObserver* observer);
84
85   // Removes an audio observer.
86   virtual void RemoveAudioObserver(AudioObserver* observer);
87
88   // Returns true if keyboard mic exists.
89   virtual bool HasKeyboardMic();
90
91   // Returns true if audio output is muted for the system.
92   virtual bool IsOutputMuted();
93
94   // Returns true if audio output is muted for a device.
95   virtual bool IsOutputMutedForDevice(uint64 device_id);
96
97   // Returns true if audio input is muted.
98   virtual bool IsInputMuted();
99
100   // Returns true if audio input is muted for a device.
101   virtual bool IsInputMutedForDevice(uint64 device_id);
102
103   // Returns true if the output volume is below the default mute volume level.
104   virtual bool IsOutputVolumeBelowDefaultMuteLevel();
105
106   // Returns volume level in 0-100% range at which the volume should be muted.
107   virtual int GetOutputDefaultVolumeMuteThreshold();
108
109   // Gets volume level in 0-100% range (0 being pure silence) for the current
110   // active node.
111   virtual int GetOutputVolumePercent();
112
113   // Gets volume level in 0-100% range (0 being pure silence) for a device.
114   virtual int GetOutputVolumePercentForDevice(uint64 device_id);
115
116   // Gets gain level in 0-100% range (0 being pure silence) for the current
117   // active node.
118   virtual int GetInputGainPercent();
119
120   // Gets volume level in 0-100% range (0 being pure silence) for a device.
121   virtual int GetInputGainPercentForDevice(uint64 device_id);
122
123   // Returns node_id of the primary active output node.
124   virtual uint64 GetPrimaryActiveOutputNode() const;
125
126   // Returns the node_id of the primary active input node.
127   virtual uint64 GetPrimaryActiveInputNode() const;
128
129   // Gets the audio devices back in |device_list|.
130   virtual void GetAudioDevices(AudioDeviceList* device_list) const;
131
132   virtual bool GetPrimaryActiveOutputDevice(AudioDevice* device) const;
133
134   // Whether there is alternative input/output audio device.
135   virtual bool has_alternative_input() const;
136   virtual bool has_alternative_output() const;
137
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);
141
142   // Sets all active input devices' gain level to |gain_percent|, whose range is
143   // from 0-100%.
144   virtual void SetInputGainPercent(int gain_percent);
145
146   // Adjusts all active output devices' volume up (positive percentage) or down
147   // (negative percentage).
148   virtual void AdjustOutputVolumeByPercent(int adjust_by_percent);
149
150   // Adjusts all active output devices' volume to a minimum audible level if it
151   // is too low.
152   virtual void AdjustOutputVolumeToAudibleLevel();
153
154   // Mutes or unmutes audio output device.
155   virtual void SetOutputMute(bool mute_on);
156
157   // Mutes or unmutes audio input device.
158   virtual void SetInputMute(bool mute_on);
159
160   // Switches active audio device to |device|.
161   virtual void SwitchToDevice(const AudioDevice& device, bool notify);
162
163   // Sets volume/gain level for a device.
164   virtual void SetVolumeGainPercentForDevice(uint64 device_id, int value);
165
166   // Sets the mute for device.
167   virtual void SetMuteForDevice(uint64 device_id, bool mute_on);
168
169   // Activates or deactivates keyboard mic if there's one.
170   virtual void SetKeyboardMicActive(bool active);
171
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);
181
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);
187
188   // Enables error logging.
189   virtual void LogErrors();
190
191  protected:
192   explicit CrasAudioHandler(
193       scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler);
194   virtual ~CrasAudioHandler();
195
196  private:
197   friend class CrasAudioHandlerTest;
198
199   // CrasAudioClient::Observer overrides.
200   virtual void AudioClientRestarted() override;
201   virtual void NodesChanged() override;
202   virtual void ActiveOutputNodeChanged(uint64 node_id) override;
203   virtual void ActiveInputNodeChanged(uint64 node_id) override;
204
205   // AudioPrefObserver overrides.
206   virtual void OnAudioPolicyPrefChanged() override;
207
208   // SessionManagerClient::Observer overrides.
209   virtual void EmitLoginPromptVisibleCalled() override;
210
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);
215
216   // Sets up the audio device state based on audio policy and audio settings
217   // saved in prefs.
218   void SetupAudioInputState();
219   void SetupAudioOutputState();
220
221   // Sets up the additional active audio node's state.
222   void SetupAdditionalActiveAudioNodeState(uint64 node_id);
223
224   const AudioDevice* GetDeviceFromId(uint64 device_id) const;
225   const AudioDevice* GetKeyboardMic() const;
226
227   // Initializes audio state, which should only be called when CrasAudioHandler
228   // is created or cras audio client is restarted.
229   void InitializeAudioState();
230
231   // Applies the audio muting policies whenever the user logs in or policy
232   // change notification is received.
233   void ApplyAudioPolicy();
234
235   // Sets output volume of |node_id| to |volume|.
236   void SetOutputNodeVolume(uint64 node_id, int volume);
237
238   void SetOutputNodeVolumePercent(uint64 node_id, int volume_percent);
239
240   // Sets output mute state to |mute_on| internally, returns true if output mute
241   // is set.
242   bool SetOutputMuteInternal(bool mute_on);
243
244   // Sets input gain of |node_id| to |gain|.
245   void SetInputNodeGain(uint64 node_id, int gain);
246
247   void SetInputNodeGainPercent(uint64 node_id, int gain_percent);
248
249   // Sets input mute state to |mute_on| internally, returns true if input mute
250   // is set.
251   bool SetInputMuteInternal(bool mute_on);
252
253   // Calling dbus to get nodes data.
254   void GetNodes();
255
256   // Updates the current audio nodes list and switches the active device
257   // if needed.
258   void UpdateDevicesAndSwitchActive(const AudioNodeList& nodes);
259
260   // Returns true if *|current_active_node_id| device is changed to
261   // |new_active_device|.
262   bool ChangeActiveDevice(const AudioDevice& new_active_device,
263                           uint64* current_active_node_id);
264
265   // Returns true if the audio nodes change is caused by some non-active
266   // audio nodes unplugged.
267   bool NonActiveDeviceUnplugged(size_t old_devices_size,
268                                 size_t new_device_size,
269                                 uint64 current_active_node);
270
271   // Returns true if there is any device change for for input or output,
272   // specified by |is_input|.
273   bool HasDeviceChange(const AudioNodeList& new_nodes, bool is_input);
274
275   // Handles dbus callback for GetNodes.
276   void HandleGetNodes(const chromeos::AudioNodeList& node_list, bool success);
277
278   // Handles the dbus error callback.
279   void HandleGetNodesError(const std::string& error_name,
280                            const std::string& error_msg);
281
282   // Adds an active node.
283   // If there is no active node, |node_id| will be switched to become the
284   // primary active node. Otherwise, it will be added as an additional active
285   // node.
286   void AddActiveNode(uint64 node_id, bool notify);
287
288   // Adds |node_id| into additional active nodes.
289   void AddAdditionalActiveNode(uint64 node_id, bool notify);
290
291   // Removes |node_id| from additional active nodes.
292   void RemoveActiveNodeInternal(uint64 node_id, bool notify);
293
294   // Returns true if |device| is not found in audio_devices_, or it is found
295   // but changed its |active| property.
296   bool FoundNewOrChangedDevice(const AudioDevice& device);
297
298   void NotifyActiveNodeChanged(bool is_input);
299
300   scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler_;
301   ObserverList<AudioObserver> observers_;
302
303   // Audio data and state.
304   AudioDeviceMap audio_devices_;
305
306   AudioDevicePriorityQueue input_devices_pq_;
307   AudioDevicePriorityQueue output_devices_pq_;
308
309   bool output_mute_on_;
310   bool input_mute_on_;
311   int output_volume_;
312   int input_gain_;
313   uint64 active_output_node_id_;
314   uint64 active_input_node_id_;
315   bool has_alternative_input_;
316   bool has_alternative_output_;
317
318   bool output_mute_locked_;
319   bool input_mute_locked_;
320
321   // Failures are not logged at startup, since CRAS may not be running yet.
322   bool log_errors_;
323
324   base::WeakPtrFactory<CrasAudioHandler> weak_ptr_factory_;
325
326   DISALLOW_COPY_AND_ASSIGN(CrasAudioHandler);
327 };
328
329 }  // namespace chromeos
330
331 #endif  // CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_