1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef WEBRTC_AUDIO_DEVICE_AUDIO_DEVICE_ALSA_LINUX_H 12 #define WEBRTC_AUDIO_DEVICE_AUDIO_DEVICE_ALSA_LINUX_H 13 14 #include "webrtc/modules/audio_device/audio_device_generic.h" 15 #include "webrtc/modules/audio_device/linux/audio_mixer_manager_alsa_linux.h" 16 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" 17 18 #if defined(USE_X11) 19 #include <X11/Xlib.h> 20 #endif 21 #include <alsa/asoundlib.h> 22 #include <sys/ioctl.h> 23 #include <sys/soundcard.h> 24 25 26 namespace webrtc 27 { 28 class EventWrapper; 29 class ThreadWrapper; 30 31 class AudioDeviceLinuxALSA : public AudioDeviceGeneric 32 { 33 public: 34 AudioDeviceLinuxALSA(const int32_t id); 35 virtual ~AudioDeviceLinuxALSA(); 36 37 // Retrieve the currently utilized audio layer 38 virtual int32_t ActiveAudioLayer( 39 AudioDeviceModule::AudioLayer& audioLayer) const OVERRIDE; 40 41 // Main initializaton and termination 42 virtual int32_t Init() OVERRIDE; 43 virtual int32_t Terminate() OVERRIDE; 44 virtual bool Initialized() const OVERRIDE; 45 46 // Device enumeration 47 virtual int16_t PlayoutDevices() OVERRIDE; 48 virtual int16_t RecordingDevices() OVERRIDE; 49 virtual int32_t PlayoutDeviceName( 50 uint16_t index, 51 char name[kAdmMaxDeviceNameSize], 52 char guid[kAdmMaxGuidSize]) OVERRIDE; 53 virtual int32_t RecordingDeviceName( 54 uint16_t index, 55 char name[kAdmMaxDeviceNameSize], 56 char guid[kAdmMaxGuidSize]) OVERRIDE; 57 58 // Device selection 59 virtual int32_t SetPlayoutDevice(uint16_t index) OVERRIDE; 60 virtual int32_t SetPlayoutDevice( 61 AudioDeviceModule::WindowsDeviceType device) OVERRIDE; 62 virtual int32_t SetRecordingDevice(uint16_t index) OVERRIDE; 63 virtual int32_t SetRecordingDevice( 64 AudioDeviceModule::WindowsDeviceType device) OVERRIDE; 65 66 // Audio transport initialization 67 virtual int32_t PlayoutIsAvailable(bool& available) OVERRIDE; 68 virtual int32_t InitPlayout() OVERRIDE; 69 virtual bool PlayoutIsInitialized() const OVERRIDE; 70 virtual int32_t RecordingIsAvailable(bool& available) OVERRIDE; 71 virtual int32_t InitRecording() OVERRIDE; 72 virtual bool RecordingIsInitialized() const OVERRIDE; 73 74 // Audio transport control 75 virtual int32_t StartPlayout() OVERRIDE; 76 virtual int32_t StopPlayout() OVERRIDE; 77 virtual bool Playing() const OVERRIDE; 78 virtual int32_t StartRecording() OVERRIDE; 79 virtual int32_t StopRecording() OVERRIDE; 80 virtual bool Recording() const OVERRIDE; 81 82 // Microphone Automatic Gain Control (AGC) 83 virtual int32_t SetAGC(bool enable) OVERRIDE; 84 virtual bool AGC() const OVERRIDE; 85 86 // Volume control based on the Windows Wave API (Windows only) 87 virtual int32_t SetWaveOutVolume(uint16_t volumeLeft, 88 uint16_t volumeRight) OVERRIDE; 89 virtual int32_t WaveOutVolume(uint16_t& volumeLeft, 90 uint16_t& volumeRight) const OVERRIDE; 91 92 // Audio mixer initialization 93 virtual int32_t InitSpeaker() OVERRIDE; 94 virtual bool SpeakerIsInitialized() const OVERRIDE; 95 virtual int32_t InitMicrophone() OVERRIDE; 96 virtual bool MicrophoneIsInitialized() const OVERRIDE; 97 98 // Speaker volume controls 99 virtual int32_t SpeakerVolumeIsAvailable(bool& available) OVERRIDE; 100 virtual int32_t SetSpeakerVolume(uint32_t volume) OVERRIDE; 101 virtual int32_t SpeakerVolume(uint32_t& volume) const OVERRIDE; 102 virtual int32_t MaxSpeakerVolume(uint32_t& maxVolume) const OVERRIDE; 103 virtual int32_t MinSpeakerVolume(uint32_t& minVolume) const OVERRIDE; 104 virtual int32_t SpeakerVolumeStepSize(uint16_t& stepSize) const OVERRIDE; 105 106 // Microphone volume controls 107 virtual int32_t MicrophoneVolumeIsAvailable(bool& available) OVERRIDE; 108 virtual int32_t SetMicrophoneVolume(uint32_t volume) OVERRIDE; 109 virtual int32_t MicrophoneVolume(uint32_t& volume) const OVERRIDE; 110 virtual int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const OVERRIDE; 111 virtual int32_t MinMicrophoneVolume(uint32_t& minVolume) const OVERRIDE; 112 virtual int32_t MicrophoneVolumeStepSize( 113 uint16_t& stepSize) const OVERRIDE; 114 115 // Speaker mute control 116 virtual int32_t SpeakerMuteIsAvailable(bool& available) OVERRIDE; 117 virtual int32_t SetSpeakerMute(bool enable) OVERRIDE; 118 virtual int32_t SpeakerMute(bool& enabled) const OVERRIDE; 119 120 // Microphone mute control 121 virtual int32_t MicrophoneMuteIsAvailable(bool& available) OVERRIDE; 122 virtual int32_t SetMicrophoneMute(bool enable) OVERRIDE; 123 virtual int32_t MicrophoneMute(bool& enabled) const OVERRIDE; 124 125 // Microphone boost control 126 virtual int32_t MicrophoneBoostIsAvailable(bool& available) OVERRIDE; 127 virtual int32_t SetMicrophoneBoost(bool enable) OVERRIDE; 128 virtual int32_t MicrophoneBoost(bool& enabled) const OVERRIDE; 129 130 // Stereo support 131 virtual int32_t StereoPlayoutIsAvailable(bool& available) OVERRIDE; 132 virtual int32_t SetStereoPlayout(bool enable) OVERRIDE; 133 virtual int32_t StereoPlayout(bool& enabled) const OVERRIDE; 134 virtual int32_t StereoRecordingIsAvailable(bool& available) OVERRIDE; 135 virtual int32_t SetStereoRecording(bool enable) OVERRIDE; 136 virtual int32_t StereoRecording(bool& enabled) const OVERRIDE; 137 138 // Delay information and control 139 virtual int32_t SetPlayoutBuffer( 140 const AudioDeviceModule::BufferType type, 141 uint16_t sizeMS) OVERRIDE; 142 virtual int32_t PlayoutBuffer( 143 AudioDeviceModule::BufferType& type, 144 uint16_t& sizeMS) const OVERRIDE; 145 virtual int32_t PlayoutDelay(uint16_t& delayMS) const OVERRIDE; 146 virtual int32_t RecordingDelay(uint16_t& delayMS) const OVERRIDE; 147 148 // CPU load 149 virtual int32_t CPULoad(uint16_t& load) const OVERRIDE; 150 151 public: 152 virtual bool PlayoutWarning() const OVERRIDE; 153 virtual bool PlayoutError() const OVERRIDE; 154 virtual bool RecordingWarning() const OVERRIDE; 155 virtual bool RecordingError() const OVERRIDE; 156 virtual void ClearPlayoutWarning() OVERRIDE; 157 virtual void ClearPlayoutError() OVERRIDE; 158 virtual void ClearRecordingWarning() OVERRIDE; 159 virtual void ClearRecordingError() OVERRIDE; 160 161 public: 162 virtual void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) OVERRIDE; 163 164 private: 165 int32_t GetDevicesInfo(const int32_t function, 166 const bool playback, 167 const int32_t enumDeviceNo = 0, 168 char* enumDeviceName = NULL, 169 const int32_t ednLen = 0) const; 170 int32_t ErrorRecovery(int32_t error, snd_pcm_t* deviceHandle); 171 172 private: 173 bool KeyPressed() const; 174 175 private: 176 void Lock() EXCLUSIVE_LOCK_FUNCTION(_critSect) { _critSect.Enter(); }; 177 void UnLock() UNLOCK_FUNCTION(_critSect) { _critSect.Leave(); }; 178 private: 179 inline int32_t InputSanityCheckAfterUnlockedPeriod() const; 180 inline int32_t OutputSanityCheckAfterUnlockedPeriod() const; 181 182 private: 183 static bool RecThreadFunc(void*); 184 static bool PlayThreadFunc(void*); 185 bool RecThreadProcess(); 186 bool PlayThreadProcess(); 187 188 private: 189 AudioDeviceBuffer* _ptrAudioBuffer; 190 191 CriticalSectionWrapper& _critSect; 192 193 ThreadWrapper* _ptrThreadRec; 194 ThreadWrapper* _ptrThreadPlay; 195 uint32_t _recThreadID; 196 uint32_t _playThreadID; 197 198 int32_t _id; 199 200 AudioMixerManagerLinuxALSA _mixerManager; 201 202 uint16_t _inputDeviceIndex; 203 uint16_t _outputDeviceIndex; 204 bool _inputDeviceIsSpecified; 205 bool _outputDeviceIsSpecified; 206 207 snd_pcm_t* _handleRecord; 208 snd_pcm_t* _handlePlayout; 209 210 snd_pcm_uframes_t _recordingBuffersizeInFrame; 211 snd_pcm_uframes_t _recordingPeriodSizeInFrame; 212 snd_pcm_uframes_t _playoutBufferSizeInFrame; 213 snd_pcm_uframes_t _playoutPeriodSizeInFrame; 214 215 ssize_t _recordingBufferSizeIn10MS; 216 ssize_t _playoutBufferSizeIn10MS; 217 uint32_t _recordingFramesIn10MS; 218 uint32_t _playoutFramesIn10MS; 219 220 uint32_t _recordingFreq; 221 uint32_t _playoutFreq; 222 uint8_t _recChannels; 223 uint8_t _playChannels; 224 225 int8_t* _recordingBuffer; // in byte 226 int8_t* _playoutBuffer; // in byte 227 uint32_t _recordingFramesLeft; 228 uint32_t _playoutFramesLeft; 229 230 AudioDeviceModule::BufferType _playBufType; 231 232 private: 233 bool _initialized; 234 bool _recording; 235 bool _playing; 236 bool _recIsInitialized; 237 bool _playIsInitialized; 238 bool _AGC; 239 240 snd_pcm_sframes_t _recordingDelay; 241 snd_pcm_sframes_t _playoutDelay; 242 243 uint16_t _playWarning; 244 uint16_t _playError; 245 uint16_t _recWarning; 246 uint16_t _recError; 247 248 uint16_t _playBufDelay; // playback delay 249 uint16_t _playBufDelayFixed; // fixed playback delay 250 251 char _oldKeyState[32]; 252 #if defined(USE_X11) 253 Display* _XDisplay; 254 #endif 255 }; 256 257 } 258 259 #endif // MODULES_AUDIO_DEVICE_MAIN_SOURCE_LINUX_AUDIO_DEVICE_ALSA_LINUX_H_ 260