sipxmedialib  Version 3.3
dmaTask.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 SIPez LLC.
3 // Licensed to SIPfoundry under a Contributor Agreement.
4 //
5 // Copyright (C) 2004-2006 SIPfoundry Inc.
6 // Licensed by SIPfoundry under the LGPL license.
7 //
8 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
9 // Licensed to SIPfoundry under a Contributor Agreement.
10 //
11 // $$
13 
14 
15 #ifndef _INCLUDED_DMATASK_H /* [ */
16 #define _INCLUDED_DMATASK_H
17 
18 // SYSTEM INCLUDES
19 
20 // APPLICATION INCLUDES
21 #include "os/OsTask.h"
22 #include "os/OsBSem.h"
23 #include "mp/MpMisc.h"
24 
25 // DEFINES
26 /* the maximum number of buffers in input or output queue */
27 #define DMA_QLEN 10
28 
29 #ifdef _WIN32
30 # define N_BUFFERS 100
31 # define USER_BUFFER_MASK 0xff // if N_BUFFERS is greater than 256, must change USER_BUFFER_MASK!
32 
33 # define N_IN_BUFFERS N_BUFFERS
34 # define N_OUT_BUFFERS N_BUFFERS
35 
36 // N_OUT_PRIME was previously set to 8. however, we found that on windows
37 // vista this would cause a bad stutter. this is because after calling
38 // 8 waveOpenOut we got a batch of 8 WOM_DONE and then called another
39 // 8 waveOpenOut. hence there was a gap. by using 16 buffers we find
40 // that vista sends 5 WOM_DONE at a time. windows xp and windows 2000
41 // send WOM_DONE one at a time at regular intervals. this is surely
42 // a bug in windows vista! that said we can't fix vista and there
43 // doesn't appear to be any harm with setting N_OUT_PRIME to 16.
44 
45 # define N_IN_PRIME 32 // must not be more than N_IN_BUFFERS:
46 # define N_OUT_PRIME 16 // must not be more than N_OUT_BUFFERS:
47 
48 # define N_SAMPLES 80
49 
50 # define SAMPLES_PER_SEC 8000
51 # define MIN_SAMPLE_RATE 100
52 # define BITS_PER_SAMPLE 16
53 #endif
54 
55 
56 // MACROS
57 // EXTERNAL FUNCTIONS
58 int showFrameCount(int silent);
59 // EXTERNAL VARIABLES
60 // CONSTANTS
61 // STRUCTS
62 // TYPEDEFS
63 typedef void (*MuteListenerFuncPtr)(bool);
64 
65 // FORWARD DECLARATIONS
66 extern OsStatus dmaStartup(int samplesPerFrame);/* initialize the DMA driver */
67 extern void dmaShutdown(void); /* release the DMA driver */
68 #ifdef _WIN32
69 extern void dmaSignalMicDeviceChange(void); /* Signal a device change to the Mic Thread */
70 #endif
71 
72 extern int unMuteSpkr(void);
73 extern int muteSpkr(void);
74 extern int unMuteMic(void);
75 extern int muteMic(void);
76 
77 typedef enum {
82 
83 extern OsStatus MpDma_selectMic(MpDmaMicChoice choice);
84 extern MpDmaMicChoice MpDma_getMicMode(void);
85 
86 extern unsigned short gTableSize;
87 extern unsigned short shpAttenTable[];
88 
89 #ifdef WIN32 /* [ */
90 extern int DmaTask_setSpkrQPreload(int qlen);
91 extern int DmaTask_setMicQPreload(int qlen);
92 #endif /* WIN32 ] */
93 
94 class DmaTask : public OsTask
95 {
96 
97 /* //////////////////////////// PUBLIC //////////////////////////////////// */
98 public:
99 
100 /* ============================ CREATORS ================================== */
102 
103  static DmaTask* getDmaTask(int samplesPerFrame = 80);
104  //:Return a pointer to the DMA task, creating it if necessary
105 
106  virtual
107  ~DmaTask();
108  //:Destructor
109 
110 /* ============================ MANIPULATORS ============================== */
111  virtual int run(void* pArg);
112 
113 
115 
116 /* ============================ ACCESSORS ================================= */
118 
119 
120  static bool setRingerEnabled(bool enabled)
121  {
122  bool prevSetting = smIsRingerEnabled;
123  smIsRingerEnabled = enabled;
124  return prevSetting;
125  } ;
126  //: Maintain the "ringer" state for the device and return the previous setting.
127 
128  static bool isRingerEnabled(void) { return smIsRingerEnabled;} ;
129  //: Test whether the device's ringer is currently enabled.
130 
131  static void setMuteListener(MuteListenerFuncPtr pFunc) { smpMuteListener = pFunc; } ;
132  //: Set a listener for "mute" state changes. To remove the listener, set the
133  //: listener to NULL.
134 
135  static bool setMuteEnabled(bool enabled)
136  {
137  bool prevSetting = smIsMuted;
138  smIsMuted = enabled;
139  if (smpMuteListener)
140  {
141  smpMuteListener(enabled);
142  }
143  return prevSetting;
144  };
145  //: Set the "mute" state for the device and return the previous setting.
146 
147  static bool isMuteEnabled(void) { return smIsMuted; } ;
148  //: Test whether the device's ringer is currently enabled.
149 
150  static void setRingDevice(const char* szDevice)
151  {
152  mRingDeviceName = szDevice ;
153  mbOutputDevicesChanged = true ;
154 
155  };
156 
157  static void setCallDevice(const char* szDevice)
158  {
159  mCallDeviceName = szDevice ;
160  mbOutputDevicesChanged = true ;
161 
162  };
163 
164  static void setInputDevice(const char* szDevice)
165  {
166  mMicDeviceName = szDevice ;
167  mbInputDeviceChanged = true ;
168  };
169 
170  static UtlString& getRingDevice() { return mRingDeviceName ;} ;
171  static UtlString& getCallDevice() { return mCallDeviceName ;} ;
172  static UtlString& getMicDevice() { return mMicDeviceName ;} ;
173  static bool isOutputDeviceChanged() { return mbOutputDevicesChanged ;} ;
175  static bool isInputDeviceChanged() { return mbInputDeviceChanged ;} ;
176  static void clearInputDeviceChanged() { mbInputDeviceChanged = false ;} ;
177 
178 /* ============================ INQUIRY =================================== */
179 /* //////////////////////////// PROTECTED ///////////////////////////////// */
180 protected:
181 
182  DmaTask(MSG_Q_ID doneQ = NULL, int samplesPerFrame = 0,
183  int prio = DEF_DMA_TASK_PRIORITY, // default task priority
184  int options = DEF_DMA_TASK_OPTIONS, // default task options
185  int stack = DEF_DMA_TASK_STACKSIZE); // default task stack size
186  //:Default constructor
187 
188  static bool smIsRingerEnabled ;
189  static bool smIsMuted ;
191 
192 /* //////////////////////////// PRIVATE /////////////////////////////////// */
193 private:
194 
195  static const int DEF_DMA_TASK_PRIORITY; // default task priority
196  static const int DEF_DMA_TASK_OPTIONS; // default task options
197  static const int DEF_DMA_TASK_STACKSIZE; // default task stack size
198 
199  int mFrameSamples; // Number of samples per frame
200  MSG_Q_ID mDoneQ; // Message queue to wait on
201 
202  static UtlString mRingDeviceName; // Name of "ring" device (speaker)
203  static UtlString mCallDeviceName; // Name of in-call device (speaker)
204  static UtlString mMicDeviceName; // Name of in-call input device (microphone)
205  static bool mbOutputDevicesChanged; // Has the output device changed?
206  static bool mbInputDeviceChanged; // Has the output device changed?
207 
208  // Static data members used to enforce Singleton behavior
209  static DmaTask* spInstance; // pointer to the single instance of
210  // the MpDmaTask class
211  static OsBSem sLock; // semaphore used to ensure that there
212  // is only one instance of this class
213 
214  DmaTask(const DmaTask& rDmaTask);
215  //:Copy constructor (not implemented for this task)
216 
217  DmaTask& operator=(const DmaTask& rhs);
218  //:Assignment operator (not implemented for this task)
219 
220 };
221 
222 /*************************************************************************/
223 #endif /* _INCLUDED_DMATASK_H ] */
int mFrameSamples
Definition: dmaTask.h:199
static bool smIsMuted
Definition: dmaTask.h:189
int showFrameCount(int silent)
Definition: dmaTaskWnt.cpp:187
static bool mbInputDeviceChanged
Definition: dmaTask.h:206
void dmaSignalMicDeviceChange()
Definition: dmaTaskWnt.cpp:314
int samplesPerFrame
Definition: dmaTask.h:78
static void setCallDevice(const char *szDevice)
Definition: dmaTask.h:157
MpDmaMicChoice MpDma_getMicMode(void)
static DmaTask * getDmaTask(int samplesPerFrame=80)
unsigned short gTableSize
Definition: MprEchoSuppress.cpp:91
Definition: dmaTask.h:79
static UtlString mCallDeviceName
Definition: dmaTask.h:203
static void setMuteListener(MuteListenerFuncPtr pFunc)
Definition: dmaTask.h:131
static bool smIsRingerEnabled
Definition: dmaTask.h:188
int muteMic(void)
static bool mbOutputDevicesChanged
Definition: dmaTask.h:205
static void setRingDevice(const char *szDevice)
Definition: dmaTask.h:150
void(* MuteListenerFuncPtr)(bool)
Definition: dmaTask.h:63
MSG_Q_ID mDoneQ
Definition: dmaTask.h:200
DmaTask(MSG_Q_ID doneQ=NULL, int samplesPerFrame=0, int prio=DEF_DMA_TASK_PRIORITY, int options=DEF_DMA_TASK_OPTIONS, int stack=DEF_DMA_TASK_STACKSIZE)
int unMuteSpkr(void)
DmaTask & operator=(const DmaTask &rhs)
static const int DEF_DMA_TASK_STACKSIZE
Definition: dmaTask.h:197
static UtlString & getCallDevice()
Definition: dmaTask.h:171
static const int DEF_DMA_TASK_PRIORITY
Definition: dmaTask.h:195
Definition: dmaTask.h:94
static const int DEF_DMA_TASK_OPTIONS
Definition: dmaTask.h:196
static UtlString & getRingDevice()
Definition: dmaTask.h:170
int DmaTask_setSpkrQPreload(int qlen)
Definition: dmaTaskWnt.cpp:71
void dmaShutdown(void)
Definition: dmaTaskWnt.cpp:285
static bool isOutputDeviceChanged()
Definition: dmaTask.h:173
static UtlString mMicDeviceName
Definition: dmaTask.h:204
virtual ~DmaTask()
void * MSG_Q_ID
Definition: MpTypes.h:40
int DmaTask_setMicQPreload(int qlen)
Definition: dmaTaskWnt.cpp:81
virtual int run(void *pArg)
OsStatus MpDma_selectMic(MpDmaMicChoice choice)
static UtlString & getMicDevice()
Definition: dmaTask.h:172
unsigned short shpAttenTable[]
Definition: MprEchoSuppress.cpp:92
static UtlString mRingDeviceName
Definition: dmaTask.h:202
int muteSpkr(void)
static OsBSem sLock
Definition: dmaTask.h:211
static DmaTask * spInstance
Definition: dmaTask.h:209
static void clearOutputDeviceChanged()
Definition: dmaTask.h:174
static void clearInputDeviceChanged()
Definition: dmaTask.h:176
static MuteListenerFuncPtr smpMuteListener
Definition: dmaTask.h:190
static bool setRingerEnabled(bool enabled)
Definition: dmaTask.h:120
static bool isInputDeviceChanged()
Definition: dmaTask.h:175
Definition: dmaTask.h:80
static void setInputDevice(const char *szDevice)
Definition: dmaTask.h:164
MpDmaMicChoice
Definition: dmaTask.h:77
static bool isMuteEnabled(void)
Definition: dmaTask.h:147
static bool isRingerEnabled(void)
Definition: dmaTask.h:128
static bool setMuteEnabled(bool enabled)
Definition: dmaTask.h:135
OsStatus dmaStartup(int samplesPerFrame)
Definition: dmaTaskWnt.cpp:198
int unMuteMic(void)