| 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 <queue> | 8 #include <queue> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 | 80 |
| 81 // Adds an audio observer. | 81 // Adds an audio observer. |
| 82 virtual void AddAudioObserver(AudioObserver* observer); | 82 virtual void AddAudioObserver(AudioObserver* observer); |
| 83 | 83 |
| 84 // Removes an audio observer. | 84 // Removes an audio observer. |
| 85 virtual void RemoveAudioObserver(AudioObserver* observer); | 85 virtual void RemoveAudioObserver(AudioObserver* observer); |
| 86 | 86 |
| 87 // Returns true if keyboard mic exists. | 87 // Returns true if keyboard mic exists. |
| 88 virtual bool HasKeyboardMic(); | 88 virtual bool HasKeyboardMic(); |
| 89 | 89 |
| 90 // Returns true if audio output is muted. | 90 // Returns true if audio output is muted for the system. |
| 91 virtual bool IsOutputMuted(); | 91 virtual bool IsOutputMuted(); |
| 92 | 92 |
| 93 // Returns true if audio output is muted for a device. | 93 // Returns true if audio output is muted for a device. |
| 94 virtual bool IsOutputMutedForDevice(uint64 device_id); | 94 virtual bool IsOutputMutedForDevice(uint64 device_id); |
| 95 | 95 |
| 96 // Returns true if audio input is muted. | 96 // Returns true if audio input is muted. |
| 97 virtual bool IsInputMuted(); | 97 virtual bool IsInputMuted(); |
| 98 | 98 |
| 99 // Returns true if audio input is muted for a device. | 99 // Returns true if audio input is muted for a device. |
| 100 virtual bool IsInputMutedForDevice(uint64 device_id); | 100 virtual bool IsInputMutedForDevice(uint64 device_id); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 112 // Gets volume level in 0-100% range (0 being pure silence) for a device. | 112 // Gets volume level in 0-100% range (0 being pure silence) for a device. |
| 113 virtual int GetOutputVolumePercentForDevice(uint64 device_id); | 113 virtual int GetOutputVolumePercentForDevice(uint64 device_id); |
| 114 | 114 |
| 115 // Gets gain level in 0-100% range (0 being pure silence) for the current | 115 // Gets gain level in 0-100% range (0 being pure silence) for the current |
| 116 // active node. | 116 // active node. |
| 117 virtual int GetInputGainPercent(); | 117 virtual int GetInputGainPercent(); |
| 118 | 118 |
| 119 // Gets volume level in 0-100% range (0 being pure silence) for a device. | 119 // Gets volume level in 0-100% range (0 being pure silence) for a device. |
| 120 virtual int GetInputGainPercentForDevice(uint64 device_id); | 120 virtual int GetInputGainPercentForDevice(uint64 device_id); |
| 121 | 121 |
| 122 // Returns node_id of the active output node. | 122 // Returns node_id of the primary active output node. |
| 123 virtual uint64 GetActiveOutputNode() const; | 123 virtual uint64 GetPrimaryActiveOutputNode() const; |
| 124 | 124 |
| 125 // Returns the node_id of the active input node. | 125 // Returns the node_id of the primary active input node. |
| 126 virtual uint64 GetActiveInputNode() const; | 126 virtual uint64 GetPrimaryActiveInputNode() const; |
| 127 | 127 |
| 128 // Gets the audio devices back in |device_list|. | 128 // Gets the audio devices back in |device_list|. |
| 129 virtual void GetAudioDevices(AudioDeviceList* device_list) const; | 129 virtual void GetAudioDevices(AudioDeviceList* device_list) const; |
| 130 | 130 |
| 131 virtual bool GetActiveOutputDevice(AudioDevice* device) const; | 131 virtual bool GetPrimaryActiveOutputDevice(AudioDevice* device) const; |
| 132 | 132 |
| 133 // Whether there is alternative input/output audio device. | 133 // Whether there is alternative input/output audio device. |
| 134 virtual bool has_alternative_input() const; | 134 virtual bool has_alternative_input() const; |
| 135 virtual bool has_alternative_output() const; | 135 virtual bool has_alternative_output() const; |
| 136 | 136 |
| 137 // Sets volume level to |volume_percent|, whose range is from 0-100%. | 137 // Sets all active output devices' volume level to |volume_percent|, whose |
| 138 // range is from 0-100%. |
| 138 virtual void SetOutputVolumePercent(int volume_percent); | 139 virtual void SetOutputVolumePercent(int volume_percent); |
| 139 | 140 |
| 140 // Sets gain level to |gain_percent|, whose range is from 0-100%. | 141 // Sets all active input devices' gain level to |gain_percent|, whose range is |
| 142 // from 0-100%. |
| 141 virtual void SetInputGainPercent(int gain_percent); | 143 virtual void SetInputGainPercent(int gain_percent); |
| 142 | 144 |
| 143 // Adjusts volume up (positive percentage) or down (negative percentage). | 145 // Adjusts all active output devices' volume up (positive percentage) or down |
| 146 // (negative percentage). |
| 144 virtual void AdjustOutputVolumeByPercent(int adjust_by_percent); | 147 virtual void AdjustOutputVolumeByPercent(int adjust_by_percent); |
| 145 | 148 |
| 146 // Adjusts output volume to a minimum audible level if it is too low. | 149 // Adjusts all active output devices' volume to a minimum audible level if it |
| 150 // is too low. |
| 147 virtual void AdjustOutputVolumeToAudibleLevel(); | 151 virtual void AdjustOutputVolumeToAudibleLevel(); |
| 148 | 152 |
| 149 // Mutes or unmutes audio output device. | 153 // Mutes or unmutes audio output device. |
| 150 virtual void SetOutputMute(bool mute_on); | 154 virtual void SetOutputMute(bool mute_on); |
| 151 | 155 |
| 152 // Mutes or unmutes audio input device. | 156 // Mutes or unmutes audio input device. |
| 153 virtual void SetInputMute(bool mute_on); | 157 virtual void SetInputMute(bool mute_on); |
| 154 | 158 |
| 155 // Switches active audio device to |device|. | 159 // Switches active audio device to |device|. |
| 156 virtual void SwitchToDevice(const AudioDevice& device); | 160 virtual void SwitchToDevice(const AudioDevice& device); |
| 157 | 161 |
| 158 // Sets volume/gain level for a device. | 162 // Sets volume/gain level for a device. |
| 159 virtual void SetVolumeGainPercentForDevice(uint64 device_id, int value); | 163 virtual void SetVolumeGainPercentForDevice(uint64 device_id, int value); |
| 160 | 164 |
| 161 // Sets the mute for device. | 165 // Sets the mute for device. |
| 162 virtual void SetMuteForDevice(uint64 device_id, bool mute_on); | 166 virtual void SetMuteForDevice(uint64 device_id, bool mute_on); |
| 163 | 167 |
| 164 // Activates or deactivates keyboard mic if there's one. | 168 // Activates or deactivates keyboard mic if there's one. |
| 165 virtual void SetKeyboardMicActive(bool active); | 169 virtual void SetKeyboardMicActive(bool active); |
| 166 | 170 |
| 171 // Adds an active node. |
| 172 // If there is no active node, |node_id| will be switched to become the |
| 173 // primary active node. Otherwise, it will be added as an additional active |
| 174 // node. |
| 175 virtual void AddActiveNode(uint64 node_id); |
| 176 |
| 177 // Removes an active audio node. |
| 178 // If |node_id| is the only active input/output node, or is an additional |
| 179 // active input/output node, it will be removed and becomes inactive. |
| 180 // Note: It is not proper call this api to remove the primary active node |
| 181 // while there are additional active nodes. |
| 182 virtual void RemoveActiveNode(uint64 node_id); |
| 183 |
| 184 // Removes all active audio nodes, including the primary active ones. |
| 185 virtual void RemoveAllActiveNodes(); |
| 186 |
| 167 // Enables error logging. | 187 // Enables error logging. |
| 168 virtual void LogErrors(); | 188 virtual void LogErrors(); |
| 169 | 189 |
| 170 protected: | 190 protected: |
| 171 explicit CrasAudioHandler( | 191 explicit CrasAudioHandler( |
| 172 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler); | 192 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler); |
| 173 virtual ~CrasAudioHandler(); | 193 virtual ~CrasAudioHandler(); |
| 174 | 194 |
| 175 private: | 195 private: |
| 196 friend class CrasAudioHandlerTest; |
| 197 |
| 176 // CrasAudioClient::Observer overrides. | 198 // CrasAudioClient::Observer overrides. |
| 177 virtual void AudioClientRestarted() OVERRIDE; | 199 virtual void AudioClientRestarted() OVERRIDE; |
| 178 virtual void NodesChanged() OVERRIDE; | 200 virtual void NodesChanged() OVERRIDE; |
| 179 virtual void ActiveOutputNodeChanged(uint64 node_id) OVERRIDE; | 201 virtual void ActiveOutputNodeChanged(uint64 node_id) OVERRIDE; |
| 180 virtual void ActiveInputNodeChanged(uint64 node_id) OVERRIDE; | 202 virtual void ActiveInputNodeChanged(uint64 node_id) OVERRIDE; |
| 181 | 203 |
| 182 // AudioPrefObserver overrides. | 204 // AudioPrefObserver overrides. |
| 183 virtual void OnAudioPolicyPrefChanged() OVERRIDE; | 205 virtual void OnAudioPolicyPrefChanged() OVERRIDE; |
| 184 | 206 |
| 185 // SessionManagerClient::Observer overrides. | 207 // SessionManagerClient::Observer overrides. |
| 186 virtual void EmitLoginPromptVisibleCalled() OVERRIDE; | 208 virtual void EmitLoginPromptVisibleCalled() OVERRIDE; |
| 187 | 209 |
| 188 // Sets the active audio output/input node to the node with |node_id|. | 210 // Sets the active audio output/input node to the node with |node_id|. |
| 189 void SetActiveOutputNode(uint64 node_id); | 211 void SetActiveOutputNode(uint64 node_id); |
| 190 void SetActiveInputNode(uint64 node_id); | 212 void SetActiveInputNode(uint64 node_id); |
| 191 | 213 |
| 192 // Sets up the audio device state based on audio policy and audio settings | 214 // Sets up the audio device state based on audio policy and audio settings |
| 193 // saved in prefs. | 215 // saved in prefs. |
| 194 void SetupAudioInputState(); | 216 void SetupAudioInputState(); |
| 195 void SetupAudioOutputState(); | 217 void SetupAudioOutputState(); |
| 196 | 218 |
| 219 // Sets up the additional active audio node's state. |
| 220 void SetupAdditionalActiveAudioNodeState(uint64 node_id); |
| 221 |
| 197 const AudioDevice* GetDeviceFromId(uint64 device_id) const; | 222 const AudioDevice* GetDeviceFromId(uint64 device_id) const; |
| 198 const AudioDevice* GetKeyboardMic() const; | 223 const AudioDevice* GetKeyboardMic() const; |
| 199 | 224 |
| 200 // Initializes audio state, which should only be called when CrasAudioHandler | 225 // Initializes audio state, which should only be called when CrasAudioHandler |
| 201 // is created or cras audio client is restarted. | 226 // is created or cras audio client is restarted. |
| 202 void InitializeAudioState(); | 227 void InitializeAudioState(); |
| 203 | 228 |
| 204 // Applies the audio muting policies whenever the user logs in or policy | 229 // Applies the audio muting policies whenever the user logs in or policy |
| 205 // change notification is received. | 230 // change notification is received. |
| 206 void ApplyAudioPolicy(); | 231 void ApplyAudioPolicy(); |
| 207 | 232 |
| 208 // Sets output volume of |node_id| to |volume|. | 233 // Sets output volume of |node_id| to |volume|. |
| 209 void SetOutputNodeVolume(uint64 node_id, int volume); | 234 void SetOutputNodeVolume(uint64 node_id, int volume); |
| 210 | 235 |
| 236 void SetOutputNodeVolumePercent(uint64 node_id, int volume_percent); |
| 237 |
| 211 // Sets output mute state to |mute_on| internally, returns true if output mute | 238 // Sets output mute state to |mute_on| internally, returns true if output mute |
| 212 // is set. | 239 // is set. |
| 213 bool SetOutputMuteInternal(bool mute_on); | 240 bool SetOutputMuteInternal(bool mute_on); |
| 214 | 241 |
| 215 // Sets input gain of |node_id| to |gain|. | 242 // Sets input gain of |node_id| to |gain|. |
| 216 void SetInputNodeGain(uint64 node_id, int gain); | 243 void SetInputNodeGain(uint64 node_id, int gain); |
| 217 | 244 |
| 245 void SetInputNodeGainPercent(uint64 node_id, int gain_percent); |
| 246 |
| 218 // Sets input mute state to |mute_on| internally, returns true if input mute | 247 // Sets input mute state to |mute_on| internally, returns true if input mute |
| 219 // is set. | 248 // is set. |
| 220 bool SetInputMuteInternal(bool mute_on); | 249 bool SetInputMuteInternal(bool mute_on); |
| 221 | 250 |
| 222 // Calling dbus to get nodes data. | 251 // Calling dbus to get nodes data. |nodes_changed| is true if we are calling |
| 223 void GetNodes(); | 252 // GetNodes due to receiving NodesChanged signal. |
| 253 void GetNodes(bool nodes_changed); |
| 224 | 254 |
| 225 // Updates the current audio nodes list and switches the active device | 255 // Updates the current audio nodes list and switches the active device |
| 226 // if needed. | 256 // if needed. |
| 227 void UpdateDevicesAndSwitchActive(const AudioNodeList& nodes); | 257 void UpdateDevicesAndSwitchActive(const AudioNodeList& nodes); |
| 228 | 258 |
| 229 // Returns true if *|current_active_node_id| device is changed to | 259 // Returns true if *|current_active_node_id| device is changed to |
| 230 // |new_active_device|. | 260 // |new_active_device|. |
| 231 bool ChangeActiveDevice(const AudioDevice& new_active_device, | 261 bool ChangeActiveDevice(const AudioDevice& new_active_device, |
| 232 uint64* current_active_node_id); | 262 uint64* current_active_node_id); |
| 233 | 263 |
| 234 // Returns true if the audio nodes change is caused by some non-active | 264 // Returns true if the audio nodes change is caused by some non-active |
| 235 // audio nodes unplugged. | 265 // audio nodes unplugged. |
| 236 bool NonActiveDeviceUnplugged(size_t old_devices_size, | 266 bool NonActiveDeviceUnplugged(size_t old_devices_size, |
| 237 size_t new_device_size, | 267 size_t new_device_size, |
| 238 uint64 current_active_node); | 268 uint64 current_active_node); |
| 239 | 269 |
| 240 // Returns true if there is any device change for for input or output, | 270 // Returns true if there is any device change for for input or output, |
| 241 // specified by |is_input|. | 271 // specified by |is_input|. |
| 242 bool HasDeviceChange(const AudioNodeList& new_nodes, bool is_input); | 272 bool HasDeviceChange(const AudioNodeList& new_nodes, bool is_input); |
| 243 | 273 |
| 244 // Handles dbus callback for GetNodes. | 274 // Handles dbus callback for GetNodes. |
| 245 void HandleGetNodes(const chromeos::AudioNodeList& node_list, bool success); | 275 void HandleGetNodes(const chromeos::AudioNodeList& node_list, bool success); |
| 246 | 276 |
| 277 // Handles GetNodes Callback NOT triggered by NodesChanged signal. |
| 278 void HandleGetNodesWithoutNodesChange( |
| 279 const chromeos::AudioNodeList& node_list, |
| 280 bool success); |
| 281 |
| 247 // Handles the dbus error callback. | 282 // Handles the dbus error callback. |
| 248 void HandleGetNodesError(const std::string& error_name, | 283 void HandleGetNodesError(const std::string& error_name, |
| 249 const std::string& error_msg); | 284 const std::string& error_msg); |
| 250 | 285 |
| 251 // Returns true if |device| is not found in audio_devices_. | 286 // Adds |node_id| into additional active nodes. |
| 252 bool FoundNewDevice(const AudioDevice& device); | 287 void AddAdditionalActiveNode(uint64 node_id); |
| 253 | 288 |
| 254 // Returns a sanitized AudioDevice from |node|. | 289 // Removes |node_id| from additional active nodes. |
| 255 AudioDevice GetSanitizedAudioDevice(const AudioNode& node); | 290 void RemoveActiveNodeInternal(uint64 node_id); |
| 291 |
| 292 // Returns true if |device| is not found in audio_devices_, or it is found |
| 293 // but changed its |active| property. |
| 294 bool FoundNewOrChangedDevice(const AudioDevice& device); |
| 295 |
| 296 // Returns true if there are additional active output audio nodes. |
| 297 bool HasAdditionalActiveOutputNode() const; |
| 298 |
| 299 // Returns true if there are additional active input audio nodes. |
| 300 bool HasAdditionalActiveInputNode() const; |
| 256 | 301 |
| 257 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler_; | 302 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler_; |
| 258 ObserverList<AudioObserver> observers_; | 303 ObserverList<AudioObserver> observers_; |
| 259 | 304 |
| 260 // Audio data and state. | 305 // Audio data and state. |
| 261 AudioDeviceMap audio_devices_; | 306 AudioDeviceMap audio_devices_; |
| 262 | 307 |
| 263 AudioDevicePriorityQueue input_devices_pq_; | 308 AudioDevicePriorityQueue input_devices_pq_; |
| 264 AudioDevicePriorityQueue output_devices_pq_; | 309 AudioDevicePriorityQueue output_devices_pq_; |
| 265 | 310 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 279 bool log_errors_; | 324 bool log_errors_; |
| 280 | 325 |
| 281 base::WeakPtrFactory<CrasAudioHandler> weak_ptr_factory_; | 326 base::WeakPtrFactory<CrasAudioHandler> weak_ptr_factory_; |
| 282 | 327 |
| 283 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandler); | 328 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandler); |
| 284 }; | 329 }; |
| 285 | 330 |
| 286 } // namespace chromeos | 331 } // namespace chromeos |
| 287 | 332 |
| 288 #endif // CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_ | 333 #endif // CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_ |
| OLD | NEW |