sipxmedialib  Version 3.3
MpAlsa.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2007-2017 SIPez LLC. All rights reserved.
3 //
4 // $$
6 
7 
8 #ifndef _MpAlsa_h_
9 #define _MpAlsa_h_
10 
11 // SYSTEM INCLUDES
12 #include <pthread.h>
13 #include <semaphore.h>
14 
15 #include <sys/types.h>
16 #include <sys/soundcard.h>
17 #include <alsa/asoundlib.h>
18 #include <alsa/pcm_plugin.h>
19 
20 // APPLICATION INCLUDES
21 #include "mp/MpInputDeviceDriver.h"
23 #include "mp/MpAlsaContainer.h"
24 #include "utl/UtlVoidPtr.h"
25 
26 class MpidAlsa;
27 class MpodAlsa;
28 
32 class MpAlsa : public UtlVoidPtr
33 {
34  friend class MpidAlsa;
35  friend class MpodAlsa;
36  /* //////////////////////////// PUBLIC //////////////////////////////////// */
37 public:
38 
39  /* ============================ CREATORS ================================== */
41 
42 
44  MpAlsa();
45 
47  ~MpAlsa();
48 
50 
51  /* ============================ MANIPULATORS ============================== */
53 
54 
56  OsStatus setInputDevice(MpidAlsa* pIDD);
58  OsStatus setOutputDevice(MpodAlsa* pODD);
59 
61  OsStatus freeInputDevice();
63  OsStatus freeOutputDevice();
64 
66  OsStatus attachReader();
68  OsStatus attachWriter();
69 
71  OsStatus detachReader();
73  OsStatus detachWriter();
74 
76 
77  /* ============================ ACCESSORS ================================= */
79 
80  static int getDeviceNames(UtlContainer& deviceNames, bool capture);
90 
91  /* ============================ INQUIRY =================================== */
93 
94 
96  inline UtlBoolean isDeviceValid();
97 
99  inline UtlBoolean isReaderAttached();
100 
102  inline UtlBoolean isWriterAttached();
103  inline UtlBoolean isDevCapBatch();
104  inline UtlBoolean isDevCapDuplex();
105  inline UtlBoolean isDevCapMmap();
106  inline UtlBoolean isDevCapTrigger();
107 
109  inline UtlBoolean isNotUsed();
110 
112 
113  /* //////////////////////////// PROTECTED ///////////////////////////////// */
114 protected:
115  UtlBoolean mbReadCap;
116  UtlBoolean mbWriteCap;
117 
120 
122 
123  volatile UtlBoolean mStReader;
124  volatile UtlBoolean mStWriter;
125  volatile UtlBoolean mStShutdown;
126  volatile UtlBoolean mModeChanged;
127 
130 
132  UtlBoolean mStereoOps;
133 
134  pthread_t mIoThread;
135  sem_t mSleepSem;
136  sem_t mSignalSem;
137 
139  OsStatus initDevice(const char* devname, int samplesPerSecond);
141  OsStatus initDeviceFinal(unsigned samplesPerSec, unsigned samplerPerFrame);
142 
145  OsStatus setSampleRate(unsigned samplesPerSec, unsigned samplerPerFrame);
146 
148  OsStatus doInput(char* buffer, int size);
150  OsStatus doOutput(const char* buffer, int size);
151 
153  OsStatus doInputRs(MpAudioSample* buffer, unsigned size);
155  OsStatus doOutputRs(const MpAudioSample* buffer, unsigned size);
156 
158  void noMoreNeeded();
159 
161  void soundIoThread();
162 
163  // MER additions for ALSA devices
164  snd_pcm_t* pPcmOut;
165  snd_pcm_t* pPcmIn;
168 
169  /* //////////////////////////// PRIVATE /////////////////////////////////// */
170 private:
171 
172  void threadIoStatusChanged();
173  void threadWakeUp();
174  void threadKill();
175 
176  UtlBoolean alsaSetTrigger(bool turnOn);
177  UtlBoolean alsaReset();
178 
180  static void* soundCardIoWrapper(void* arg);
181  int alsaSetupPcmDevice(const char* devname, bool capture, unsigned& suggestedRate);
182  int alsaSetupWarning(const char* devname, int err, bool cap, const char* pLogString);
183 };
184 
185 /* ============================ INLINE METHODS ============================ */
186 
188 {
189  return (pPcmIn != NULL && pPcmOut != NULL);
190 }
191 
193 {
194  return (mReader != NULL);
195 }
196 
198 {
199  return (mWriter != NULL);
200 }
201 
203 {
204  return (mDeviceCap & DSP_CAP_BATCH);
205 }
206 
208 {
209  return (mDeviceCap & DSP_CAP_DUPLEX);
210 }
211 
213 {
214  return (mDeviceCap & DSP_CAP_MMAP);
215 }
216 
218 {
219  return (mDeviceCap & DSP_CAP_TRIGGER);
220 }
221 
222 UtlBoolean MpAlsa::isNotUsed()
223 {
224  return ((mReader == NULL) && (mWriter == NULL));
225 }
226 
227 
228 #endif // _MpAlsa_h_
229 
OsStatus doInput(char *buffer, int size)
Perform input operation of ALSA device.
OsStatus detachReader()
Disable input device.
UtlBoolean isDevCapMmap()
Definition: MpAlsa.h:212
UtlBoolean isDevCapDuplex()
Definition: MpAlsa.h:207
void noMoreNeeded()
Deinitialization and freeing sequences.
UtlBoolean alsaSetTrigger(bool turnOn)
static void * soundCardIoWrapper(void *arg)
Thread subroutine.
~MpAlsa()
Destructor.
int readBufSize
Definition: MpAlsa.h:167
UtlBoolean isDeviceValid()
Inquire if the device is valid.
Definition: MpAlsa.h:187
Container for device specific input ALSA driver.
Definition: MpodAlsa.h:41
OsStatus doInputRs(MpAudioSample *buffer, unsigned size)
Perform input operation of ALSA device with siutable resampler.
OsStatus doOutput(const char *buffer, int size)
Perform output operation of ALSA device.
OsStatus initDeviceFinal(unsigned samplesPerSec, unsigned samplerPerFrame)
Final initializations for ALSA device.
volatile UtlBoolean mStReader
Input device is enabled.
Definition: MpAlsa.h:123
int writeBufSize
Definition: MpAlsa.h:166
UtlBoolean isNotUsed()
Inquire if ALSA device is free, i.e. neither input nor output is connected.
Definition: MpAlsa.h:222
OsStatus setInputDevice(MpidAlsa *pIDD)
Connect MpidAlsa class driver.
sem_t mSleepSem
Control IO thread sleeping.
Definition: MpAlsa.h:135
OsStatus detachWriter()
Disable output device.
Wrapper for ALSA device that use file descriptor to communicate.
Definition: MpAlsa.h:32
int alsaSetupPcmDevice(const char *devname, bool capture, unsigned &suggestedRate)
OsStatus attachWriter()
Enable output device.
void threadKill()
MpAlsa()
Constructor.
UtlBoolean isDevCapBatch()
Definition: MpAlsa.h:202
int samplesPerSecond
int16_t MpAudioSample
Definition: MpTypes.h:44
unsigned mUsedSamplesPerFrame
Used frame size for IO.
Definition: MpAlsa.h:129
volatile UtlBoolean mStWriter
Output device is enabled.
Definition: MpAlsa.h:124
int mDeviceCap
Device capabilities.
Definition: MpAlsa.h:121
snd_pcm_t * pPcmIn
Definition: MpAlsa.h:165
OsStatus setSampleRate(unsigned samplesPerSec, unsigned samplerPerFrame)
Because ALSA device works in duplex mode we must ensure that input and output driver use one sample r...
OsStatus freeOutputDevice()
Disconnect Output driver.
MpAudioSample * mResamplerBuffer
Buffer for conversions.
Definition: MpAlsa.h:131
UtlBoolean isReaderAttached()
Inquire if the input device is connected.
Definition: MpAlsa.h:192
unsigned mUsedSamplesPerSec
Used samples rate either for IO.
Definition: MpAlsa.h:128
snd_pcm_t * pPcmOut
Definition: MpAlsa.h:164
int alsaSetupWarning(const char *devname, int err, bool cap, const char *pLogString)
void soundIoThread()
Thread subroutine.
UtlBoolean isDevCapTrigger()
Definition: MpAlsa.h:217
OsStatus doOutputRs(const MpAudioSample *buffer, unsigned size)
Perform output operation of ALSA device with siutable resampler.
MpidAlsa * mReader
Input driver.
Definition: MpAlsa.h:118
void threadWakeUp()
UtlBoolean mStereoOps
Use stereo output.
Definition: MpAlsa.h:132
OsStatus freeInputDevice()
Disconnect Input driver.
static int getDeviceNames(UtlContainer &deviceNames, bool capture)
get list of available input or output Alsa device names
OsStatus initDevice(const char *devname, int samplesPerSecond)
Pre initializations for ALSA device.
void threadIoStatusChanged()
UtlBoolean alsaReset()
pthread_t mIoThread
Internal IO thread.
Definition: MpAlsa.h:134
UtlBoolean mbWriteCap
Device is able to capture audio.
Definition: MpAlsa.h:116
volatile UtlBoolean mStShutdown
Thread exit flag.
Definition: MpAlsa.h:125
UtlBoolean mbReadCap
Device is able to play audio.
Definition: MpAlsa.h:115
OsStatus setOutputDevice(MpodAlsa *pODD)
Connect MpodAlsa class driver.
Container for device specific input ALSA driver.
Definition: MpidAlsa.h:36
UtlBoolean isWriterAttached()
Inquire if the output device is connected.
Definition: MpAlsa.h:197
MpodAlsa * mWriter
Output driver.
Definition: MpAlsa.h:119
volatile UtlBoolean mModeChanged
IO settings has changed.
Definition: MpAlsa.h:126
OsStatus attachReader()
Enable input device.
sem_t mSignalSem
Use for parameters synchronization.
Definition: MpAlsa.h:136