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/volume_state.h" 19 20 class PrefRegistrySimple; 21 class PrefService; 22 23 namespace { 24 25 // Default value for the volume pref, as a percent in the range [0.0, 100.0]. 26 const double kDefaultVolumeGainPercent = 75.0; 27 28 // Values used for muted preference. 29 const int kPrefMuteOff = 0; 30 const int kPrefMuteOn = 1; 31 32 } // namespace 33 34 namespace chromeos { 35 36 class AudioDevicesPrefHandler; 37 38 class CHROMEOS_EXPORT CrasAudioHandler : public CrasAudioClient::Observer, 39 public AudioPrefObserver { 40 public: 41 typedef std::priority_queue<AudioDevice, 42 std::vector<AudioDevice>, 43 AudioDeviceCompare> AudioDevicePriorityQueue; 44 45 class AudioObserver { 46 public: 47 // Called when output volume changed. 48 virtual void OnOutputVolumeChanged(); 49 50 // Called when output mute state changed. 51 virtual void OnOutputMuteChanged(); 52 53 // Called when input mute state changed. 54 virtual void OnInputGainChanged(); 55 56 // Called when input mute state changed. 57 virtual void OnInputMuteChanged(); 58 59 // Called when audio nodes changed. 60 virtual void OnAudioNodesChanged(); 61 62 // Called when active audio node changed. 63 virtual void OnActiveOutputNodeChanged(); 64 65 // Called when active audio input node changed. 66 virtual void OnActiveInputNodeChanged(); 67 68 protected: 69 AudioObserver(); 70 virtual ~AudioObserver(); 71 DISALLOW_COPY_AND_ASSIGN(AudioObserver); 72 }; 73 74 // Sets the global instance. Must be called before any calls to Get(). 75 static void Initialize( 76 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler); 77 78 // Sets the global instance for testing. 79 static void InitializeForTesting(); 80 81 // Destroys the global instance. 82 static void Shutdown(); 83 84 // Returns true if the global instance is initialized. 85 static bool IsInitialized(); 86 87 // Gets the global instance. Initialize must be called first. 88 static CrasAudioHandler* Get(); 89 90 // Adds an audio observer. 91 virtual void AddAudioObserver(AudioObserver* observer); 92 93 // Removes an audio observer. 94 virtual void RemoveAudioObserver(AudioObserver* observer); 95 96 // Returns true if audio output is muted. 97 virtual bool IsOutputMuted(); 98 99 // Returns true if audio output is muted for a device. 100 virtual bool IsOutputMutedForDevice(uint64 device_id); 101 102 // Returns true if audio input is muted. 103 virtual bool IsInputMuted(); 104 105 // Returns true if audio input is muted for a device. 106 virtual bool IsInputMutedForDevice(uint64 device_id); 107 108 // Returns true if the output volume is below the default mute volume level. 109 virtual bool IsOutputVolumeBelowDefaultMuteLvel(); 110 111 // Gets volume level in 0-100% range (0 being pure silence) for the current 112 // active node. 113 virtual int GetOutputVolumePercent(); 114 115 // Gets volume level in 0-100% range (0 being pure silence) for a device. 116 virtual int GetOutputVolumePercentForDevice(uint64 device_id); 117 118 // Gets gain level in 0-100% range (0 being pure silence) for the current 119 // active node. 120 virtual int GetInputGainPercent(); 121 122 // Gets volume level in 0-100% range (0 being pure silence) for a device. 123 virtual int GetInputGainPercentForDevice(uint64 device_id); 124 125 // Returns node_id of the active output node. 126 virtual uint64 GetActiveOutputNode() const; 127 128 // Returns the node_id of the active input node. 129 virtual uint64 GetActiveInputNode() const; 130 131 // Gets the audio devices back in |device_list|. 132 virtual void GetAudioDevices(AudioDeviceList* device_list) const; 133 134 virtual bool GetActiveOutputDevice(AudioDevice* device) const; 135 136 // Whether there is alternative input/output audio device. 137 virtual bool has_alternative_input() const; 138 virtual bool has_alternative_output() const; 139 140 // Sets volume level to |volume_percent|, whose range is from 0-100%. 141 virtual void SetOutputVolumePercent(int volume_percent); 142 143 // Sets gain level to |gain_percent|, whose range is from 0-100%. 144 virtual void SetInputGainPercent(int gain_percent); 145 146 // Adjusts volume up (positive percentage) or down (negative percentage). 147 virtual void AdjustOutputVolumeByPercent(int adjust_by_percent); 148 149 // Adjusts output volume to a minimum audible level if it is too low. 150 virtual void AdjustOutputVolumeToAudibleLevel(); 151 152 // Mutes or unmutes audio output device. 153 virtual void SetOutputMute(bool mute_on); 154 155 // Mutes or unmutes audio input device. 156 virtual void SetInputMute(bool mute_on); 157 158 // Switches active audio device to |device|. 159 virtual void SwitchToDevice(const AudioDevice& device); 160 161 // Sets volume/gain level for a device. 162 virtual void SetVolumeGainPercentForDevice(uint64 device_id, int value); 163 164 // Sets the mute for device. 165 virtual void SetMuteForDevice(uint64 device_id, bool mute_on); 166 167 protected: 168 explicit CrasAudioHandler( 169 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler); 170 virtual ~CrasAudioHandler(); 171 172 private: 173 // Overriden from CrasAudioClient::Observer. 174 virtual void AudioClientRestarted() OVERRIDE; 175 virtual void NodesChanged() OVERRIDE; 176 virtual void ActiveOutputNodeChanged(uint64 node_id) OVERRIDE; 177 virtual void ActiveInputNodeChanged(uint64 node_id) OVERRIDE; 178 179 // Overriden from AudioPrefObserver. 180 virtual void OnAudioPolicyPrefChanged() OVERRIDE; 181 182 // Sets the active audio output/input node to the node with |node_id|. 183 void SetActiveOutputNode(uint64 node_id); 184 void SetActiveInputNode(uint64 node_id); 185 186 // Sets up the audio device state based on audio policy and audio settings 187 // saved in prefs. 188 void SetupAudioInputState(); 189 void SetupAudioOutputState(); 190 191 const AudioDevice* GetDeviceFromId(uint64 device_id) const; 192 193 // Initializes audio state, which should only be called when CrasAudioHandler 194 // is created or cras audio client is restarted. 195 void InitializeAudioState(); 196 197 // Applies the audio muting policies whenever the user logs in or policy 198 // change notification is received. 199 void ApplyAudioPolicy(); 200 201 // Sets output volume of |node_id| to |volume|. 202 void SetOutputNodeVolume(uint64 node_id, int volume); 203 204 // Sets output mute state to |mute_on| internally, returns true if output mute 205 // is set. 206 bool SetOutputMuteInternal(bool mute_on); 207 208 // Sets input gain of |node_id| to |gain|. 209 void SetInputNodeGain(uint64 node_id, int gain); 210 211 // Sets input mute state to |mute_on| internally, returns true if input mute 212 // is set. 213 bool SetInputMuteInternal(bool mute_on); 214 215 // Calling dbus to get nodes data. 216 void GetNodes(); 217 218 // Updates the current audio nodes list and switches the active device 219 // if needed. 220 void UpdateDevicesAndSwitchActive(const AudioNodeList& nodes); 221 222 // Returns true if *|current_active_node_id| device is changed to 223 // |new_active_device|. 224 bool ChangeActiveDevice(const AudioDevice& new_active_device, 225 uint64* current_active_node_id); 226 227 // Returns true if the audio nodes change is caused by some non-active 228 // audio nodes unplugged. 229 bool NonActiveDeviceUnplugged(size_t old_devices_size, 230 size_t new_device_size, 231 uint64 current_active_node); 232 233 // Returns true if there is any device change for for input or output, 234 // specified by |is_input|. 235 bool HasDeviceChange(const AudioNodeList& new_nodes, bool is_input); 236 237 // Handles dbus callback for GetNodes. 238 void HandleGetNodes(const chromeos::AudioNodeList& node_list, bool success); 239 240 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler_; 241 base::WeakPtrFactory<CrasAudioHandler> weak_ptr_factory_; 242 ObserverList<AudioObserver> observers_; 243 244 // Audio data and state. 245 AudioDeviceMap audio_devices_; 246 247 AudioDevicePriorityQueue input_devices_pq_; 248 AudioDevicePriorityQueue output_devices_pq_; 249 250 bool output_mute_on_; 251 bool input_mute_on_; 252 int output_volume_; 253 int input_gain_; 254 uint64 active_output_node_id_; 255 uint64 active_input_node_id_; 256 bool has_alternative_input_; 257 bool has_alternative_output_; 258 259 bool output_mute_locked_; 260 bool input_mute_locked_; 261 262 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandler); 263 }; 264 265 } // namespace chromeos 266 267 #endif // CHROMEOS_AUDIO_CRAS_AUDIO_HANDLER_H_ 268