audio_device_alsa_linux.h (7575B)
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 AUDIO_DEVICE_AUDIO_DEVICE_ALSA_LINUX_H_ 12 #define AUDIO_DEVICE_AUDIO_DEVICE_ALSA_LINUX_H_ 13 14 #include <alsa/asoundlib.h> 15 #include <sys/soundcard.h> 16 #include <unistd.h> 17 18 #include <cstddef> 19 #include <cstdint> 20 #include <cstdio> 21 22 #include "api/audio/audio_device.h" 23 #include "api/audio/audio_device_defines.h" 24 #include "modules/audio_device/audio_device_buffer.h" 25 #include "modules/audio_device/audio_device_generic.h" 26 #include "modules/audio_device/linux/alsasymboltable_linux.h" 27 #include "modules/audio_device/linux/audio_mixer_manager_alsa_linux.h" 28 #include "rtc_base/platform_thread.h" 29 #include "rtc_base/synchronization/mutex.h" 30 #include "rtc_base/thread_annotations.h" 31 32 #if defined(WEBRTC_USE_X11) 33 #include <X11/Xlib.h> 34 #endif 35 36 typedef webrtc::adm_linux_alsa::AlsaSymbolTable WebRTCAlsaSymbolTable; 37 WebRTCAlsaSymbolTable* GetAlsaSymbolTable(); 38 39 namespace webrtc { 40 41 class AudioDeviceLinuxALSA : public AudioDeviceGeneric { 42 public: 43 AudioDeviceLinuxALSA(); 44 virtual ~AudioDeviceLinuxALSA(); 45 46 // Retrieve the currently utilized audio layer 47 int32_t ActiveAudioLayer( 48 AudioDeviceModule::AudioLayer& audioLayer) const override; 49 50 // Main initializaton and termination 51 InitStatus Init() RTC_LOCKS_EXCLUDED(mutex_) override; 52 int32_t Terminate() RTC_LOCKS_EXCLUDED(mutex_) override; 53 bool Initialized() const override; 54 55 // Device enumeration 56 int16_t PlayoutDevices() override; 57 int16_t RecordingDevices() override; 58 int32_t PlayoutDeviceName(uint16_t index, 59 char name[kAdmMaxDeviceNameSize], 60 char guid[kAdmMaxGuidSize]) override; 61 int32_t RecordingDeviceName(uint16_t index, 62 char name[kAdmMaxDeviceNameSize], 63 char guid[kAdmMaxGuidSize]) override; 64 65 // Device selection 66 int32_t SetPlayoutDevice(uint16_t index) override; 67 int32_t SetPlayoutDevice( 68 AudioDeviceModule::WindowsDeviceType device) override; 69 int32_t SetRecordingDevice(uint16_t index) override; 70 int32_t SetRecordingDevice( 71 AudioDeviceModule::WindowsDeviceType device) override; 72 73 // Audio transport initialization 74 int32_t PlayoutIsAvailable(bool& available) override; 75 int32_t InitPlayout() RTC_LOCKS_EXCLUDED(mutex_) override; 76 bool PlayoutIsInitialized() const override; 77 int32_t RecordingIsAvailable(bool& available) override; 78 int32_t InitRecording() RTC_LOCKS_EXCLUDED(mutex_) override; 79 bool RecordingIsInitialized() const override; 80 81 // Audio transport control 82 int32_t StartPlayout() override; 83 int32_t StopPlayout() RTC_LOCKS_EXCLUDED(mutex_) override; 84 bool Playing() const override; 85 int32_t StartRecording() override; 86 int32_t StopRecording() RTC_LOCKS_EXCLUDED(mutex_) override; 87 bool Recording() const override; 88 89 // Audio mixer initialization 90 int32_t InitSpeaker() RTC_LOCKS_EXCLUDED(mutex_) override; 91 bool SpeakerIsInitialized() const override; 92 int32_t InitMicrophone() RTC_LOCKS_EXCLUDED(mutex_) override; 93 bool MicrophoneIsInitialized() const override; 94 95 // Speaker volume controls 96 int32_t SpeakerVolumeIsAvailable(bool& available) override; 97 int32_t SetSpeakerVolume(uint32_t volume) override; 98 int32_t SpeakerVolume(uint32_t& volume) const override; 99 int32_t MaxSpeakerVolume(uint32_t& maxVolume) const override; 100 int32_t MinSpeakerVolume(uint32_t& minVolume) const override; 101 102 // Microphone volume controls 103 int32_t MicrophoneVolumeIsAvailable(bool& available) override; 104 int32_t SetMicrophoneVolume(uint32_t volume) override; 105 int32_t MicrophoneVolume(uint32_t& volume) const override; 106 int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const override; 107 int32_t MinMicrophoneVolume(uint32_t& minVolume) const override; 108 109 // Speaker mute control 110 int32_t SpeakerMuteIsAvailable(bool& available) override; 111 int32_t SetSpeakerMute(bool enable) override; 112 int32_t SpeakerMute(bool& enabled) const override; 113 114 // Microphone mute control 115 int32_t MicrophoneMuteIsAvailable(bool& available) override; 116 int32_t SetMicrophoneMute(bool enable) override; 117 int32_t MicrophoneMute(bool& enabled) const override; 118 119 // Stereo support 120 int32_t StereoPlayoutIsAvailable(bool& available) 121 RTC_LOCKS_EXCLUDED(mutex_) override; 122 int32_t SetStereoPlayout(bool enable) override; 123 int32_t StereoPlayout(bool& enabled) const override; 124 int32_t StereoRecordingIsAvailable(bool& available) 125 RTC_LOCKS_EXCLUDED(mutex_) override; 126 int32_t SetStereoRecording(bool enable) override; 127 int32_t StereoRecording(bool& enabled) const override; 128 129 // Delay information and control 130 int32_t PlayoutDelay(uint16_t& delayMS) const override; 131 132 void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) 133 RTC_LOCKS_EXCLUDED(mutex_) override; 134 135 private: 136 int32_t InitRecordingLocked() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 137 int32_t StopRecordingLocked() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 138 int32_t StopPlayoutLocked() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 139 int32_t InitPlayoutLocked() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 140 int32_t InitSpeakerLocked() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 141 int32_t InitMicrophoneLocked() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 142 int32_t GetDevicesInfo(int32_t function, 143 bool playback, 144 int32_t enumDeviceNo = 0, 145 char* enumDeviceName = NULL, 146 int32_t ednLen = 0) const; 147 int32_t ErrorRecovery(int32_t error, snd_pcm_t* deviceHandle); 148 149 bool KeyPressed() const; 150 151 void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION(mutex_) { mutex_.Lock(); } 152 void UnLock() RTC_UNLOCK_FUNCTION(mutex_) { mutex_.Unlock(); } 153 154 inline int32_t InputSanityCheckAfterUnlockedPeriod() const; 155 inline int32_t OutputSanityCheckAfterUnlockedPeriod() const; 156 157 static void RecThreadFunc(void*); 158 static void PlayThreadFunc(void*); 159 bool RecThreadProcess(); 160 bool PlayThreadProcess(); 161 162 AudioDeviceBuffer* _ptrAudioBuffer; 163 164 Mutex mutex_; 165 166 PlatformThread _ptrThreadRec; 167 PlatformThread _ptrThreadPlay; 168 169 AudioMixerManagerLinuxALSA _mixerManager; 170 171 uint16_t _inputDeviceIndex; 172 uint16_t _outputDeviceIndex; 173 bool _inputDeviceIsSpecified; 174 bool _outputDeviceIsSpecified; 175 176 snd_pcm_t* _handleRecord; 177 snd_pcm_t* _handlePlayout; 178 179 snd_pcm_uframes_t _recordingBuffersizeInFrame; 180 snd_pcm_uframes_t _recordingPeriodSizeInFrame; 181 snd_pcm_uframes_t _playoutBufferSizeInFrame; 182 snd_pcm_uframes_t _playoutPeriodSizeInFrame; 183 184 ssize_t _recordingBufferSizeIn10MS; 185 ssize_t _playoutBufferSizeIn10MS; 186 uint32_t _recordingFramesIn10MS; 187 uint32_t _playoutFramesIn10MS; 188 189 uint32_t _recordingFreq; 190 uint32_t _playoutFreq; 191 uint8_t _recChannels; 192 uint8_t _playChannels; 193 194 int8_t* _recordingBuffer; // in byte 195 int8_t* _playoutBuffer; // in byte 196 uint32_t _recordingFramesLeft; 197 uint32_t _playoutFramesLeft; 198 199 bool _initialized; 200 bool _recording; 201 bool _playing; 202 bool _recIsInitialized; 203 bool _playIsInitialized; 204 205 snd_pcm_sframes_t _recordingDelay; 206 snd_pcm_sframes_t _playoutDelay; 207 208 char _oldKeyState[32]; 209 #if defined(WEBRTC_USE_X11) 210 Display* _XDisplay; 211 #endif 212 }; 213 214 } // namespace webrtc 215 216 #endif // MODULES_AUDIO_DEVICE_MAIN_SOURCE_LINUX_AUDIO_DEVICE_ALSA_LINUX_H_