sipxmedialib  Version 3.3
MpodWinMM.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2007-2021 SIPez LLC. All rights reserved.
3 //
4 // Copyright (C) 2007 SIPfoundry Inc.
5 // Licensed by SIPfoundry under the LGPL license.
6 //
7 // $$
9 
10 // Author: Keith Kyzivat <kkyzivat AT SIPez DOT com>
11 
12 #ifndef _MpodWinMM_h_
13 #define _MpodWinMM_h_
14 
15 // SYSTEM INCLUDES
16 #define WIN32_LEAN_AND_MEAN
17 #include <Windows.h>
18 #include <malloc.h>
19 #include <MMSystem.h>
20 #include <os/OsMutex.h>
21 #include <os/OsCallback.h>
22 #include <utl/UtlSList.h>
23 #include <utl/UtlVoidPtr.h>
24 
25 // APPLICATION INCLUDES
27 
28 // APPLICATION INCLUDES
29 #include "mp/MpTypes.h"
30 
31 // DEFINES
32 #define DEFAULT_N_OUTPUT_BUFS 32
33 
34 //#define DONTUSE_SLIST
35 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0600)
36 # define DONTUSE_SLIST
37 #endif
38 
39 // MACROS
40 // EXTERNAL FUNCTIONS
41 // EXTERNAL VARIABLES
42 // CONSTANTS
43 // STRUCTS
44 // TYPEDEFS
45 // FORWARD DECLARATIONS
46 class OsNotification;
48 class MpMMTimer;
49 struct IMMNotificationClient;
50 
69 class MpodWinMM : public MpOutputDeviceDriver, OsCallback
70 {
71 /* //////////////////////////// PUBLIC //////////////////////////////////// */
72 public:
73 
74 /* ============================ CREATORS ================================== */
76 
77 
79  explicit
80  MpodWinMM(const UtlString& name,
81  MpOutputDeviceManager* outputManger,
82  unsigned nOutputBuffers = DEFAULT_N_OUTPUT_BUFS);
97  ~MpodWinMM();
99 
101 
102 /* ============================ MANIPULATORS ============================== */
104 
105 
107  OsStatus enableDevice(unsigned samplesPerFrame,
108  unsigned samplesPerSec,
109  MpFrameTime currentFrameTime,
110  OsCallback &frameTicker);
111 
113  OsStatus disableDevice();
114 
116  OsStatus pushFrame(unsigned int numSamples,
117  const MpAudioSample* samples,
118  MpFrameTime frameTime);
119 
121  OsStatus switchToMMTimer();
122 
124  virtual OsStatus signal(const intptr_t eventData);
125 
127 
128 /* ============================ ACCESSORS ================================= */
130 
131 
133  static UtlString getDefaultDeviceName();
134 
136 
137 /* ============================ INQUIRY =================================== */
139 
140 
142  inline UtlBoolean isDeviceValid();
143 
145 
146 /* //////////////////////////// PROTECTED ///////////////////////////////// */
147 protected:
148 
150  WAVEHDR* initWaveHeader(int n);
158  void finalizeProcessedHeader(WAVEHDR* pWaveHdr);
169  static void CALLBACK waveOutCallbackStatic(HWAVEOUT hwo, UINT uMsg,
171  void* dwInstance,
172  void* dwParam1, void* dwParam2);
177 protected:
179  OsStatus internalPushFrame(unsigned int numSamples,
180  const MpAudioSample* samples,
181  MpFrameTime frameTime);
182 
183 protected:
187  HWAVEOUT mDevHandle;
190  unsigned mNumOutBuffers;
191  unsigned mWaveBufSize;
193  WAVEHDR* mpWaveHeaders;
195  LPSTR* mpWaveBuffers;
196  UtlSList mEmptyHeaderList;
197  UtlSList mUnusedVPtrList;
199  int mUnderrunLength;
202  IMMNotificationClient* mWinAudioDeviceChangeCallback;
203  IMMDeviceEnumerator* mDeviceEnumeratorPtr;
206  HANDLE mCallbackEvent;
207  OsAtomicLightBool mExitFlag;
209 
211 #ifndef DONTUSE_SLIST
212  struct WinAudioDataChain
214  {
215  SLIST_ENTRY ItemEntry;
216  UINT mCbParamMsg;
217  WAVEHDR* mCbParamHdr;
218  };
219 
220  SLIST_HEADER mPoolSignaled;
221  SLIST_HEADER mPoolFree;
222 #else
223  class WinAudioData : public UtlVoidPtr
225  {
226  public:
227  WinAudioData() { setValue(this); }
228 
230  WAVEHDR* mCbParamHdr;
231  };
232 
233  UtlSList mListSignaled;
234  UtlSList mListFree;
235 #endif
236 
237 /* //////////////////////////// PRIVATE /////////////////////////////////// */
238 private:
239 
241  MpodWinMM(const MpodWinMM& rMpodWinMM);
242 
244  MpodWinMM& operator=(const MpodWinMM& rhs);
245 
247 
249  static DWORD WINAPI ThreadMMProc(LPVOID lpMessage);
257  OsStatus resetDevice();
258 };
259 
260 
261 /* ============================ INLINE METHODS ============================ */
262 
264 {
265  return mWinMMDeviceId >= 0;
266 }
267 
268 #endif // _MpodWinMM_h_
OsStatus enableDevice(unsigned samplesPerFrame, unsigned samplesPerSec, MpFrameTime currentFrameTime, OsCallback &frameTicker)
Definition: MpodWinMM.cpp:505
WAVEHDR * initWaveHeader(int n)
Zero out a wave header, so it is ready to be filled in by us.
Definition: MpodWinMM.cpp:1214
DWORD mTotSampleCount
A count of the samples coming in via pushFrame.
Definition: MpodWinMM.h:201
High-precision periodic timer (MultiMedia timer) base class.
Definition: MpMMTimer.h:44
int mUnderrunLength
Length of underrun, taking place now (in frames).
Definition: MpodWinMM.h:200
virtual OsStatus signal(const intptr_t eventData)
callback used by MpMMTimer when output device is not providing ticks
Definition: MpodWinMM.cpp:1127
int samplesPerFrame
WAVEHDR * mpWaveHeaders
Array of nNumInBuffers wave headers.
Definition: MpodWinMM.h:194
uint32_t MpFrameTime
Definition: MpTypes.h:53
Structure used to pass WMM message data to processing thread.
Definition: MpodWinMM.h:224
int mWinMMDeviceId
Definition: MpodWinMM.h:186
OsStatus pushFrame(unsigned int numSamples, const MpAudioSample *samples, MpFrameTime frameTime)
Send data to output device.
Definition: MpodWinMM.cpp:798
OsStatus switchToMMTimer()
Switch to use MMTimer for Media subsystem ticks instead of output device.
Definition: MpodWinMM.cpp:1040
MpMMTimer * mpTickerTimer
Definition: MpodWinMM.h:208
OsStatus resetDevice()
Definition: MpodWinMM.cpp:746
MpOutputDeviceManager * mpOutputManger
output manager this device will belong to.
Definition: MpodWinMM.h:184
OsAtomicLightBool mExitFlag
Should processing thread finish its execution?
Definition: MpodWinMM.h:207
UtlSList mListSignaled
Queue of WMM messages signaled for processing.
Definition: MpodWinMM.h:233
UtlSList mEmptyHeaderList
Definition: MpodWinMM.h:196
UtlSList mUnusedVPtrList
Definition: MpodWinMM.h:198
static void CALLBACK waveOutCallbackStatic(HWAVEOUT hwo, UINT uMsg, void *dwInstance, void *dwParam1, void *dwParam2)
callback used by wave output multimedia interface
Definition: MpodWinMM.cpp:1356
WAVEHDR * mCbParamHdr
Definition: MpodWinMM.h:230
int16_t MpAudioSample
Definition: MpTypes.h:44
MpodWinMM & operator=(const MpodWinMM &rhs)
Assignment operator (not implemented for this class)
unsigned mNumOutBuffers
Definition: MpodWinMM.h:190
MpodWinMM(const UtlString &name, MpOutputDeviceManager *outputManger, unsigned nOutputBuffers=DEFAULT_N_OUTPUT_BUFS)
Default constructor.
Definition: MpodWinMM.cpp:281
HANDLE mCallbackEvent
Event to signal that WMM message is available for processing.
Definition: MpodWinMM.h:206
static DWORD WINAPI ThreadMMProc(LPVOID lpMessage)
WMM messages processing thread.
Definition: MpodWinMM.cpp:1278
void finalizeProcessedHeader(WAVEHDR *pWaveHdr)
Adds a header/buffer to the empty header list and notifies for a new frame.
Definition: MpodWinMM.cpp:1238
Definition: MpodWinMM.cpp:73
OsMutex mEmptyHdrVPtrListsMutex
Mutex to serialize access to vptr and empty header lists.
Definition: MpodWinMM.h:185
LPSTR * mpWaveBuffers
Array of nNumInBuffers wave buffers.
Definition: MpodWinMM.h:195
UtlBoolean isDeviceValid()
Inquire if the windows device is valid.
Definition: MpodWinMM.h:263
IMMNotificationClient * mWinAudioDeviceChangeCallback
device state changes.
Definition: MpodWinMM.h:202
UtlSList mListFree
Pool of WMM messages.
Definition: MpodWinMM.h:234
OsStatus internalPushFrame(unsigned int numSamples, const MpAudioSample *samples, MpFrameTime frameTime)
internal method to handle final writing of audio to the output device
Definition: MpodWinMM.cpp:901
HANDLE mCallbackThread
Handle of thread which processes WMM messages.
Definition: MpodWinMM.h:205
Container for Microsoft Windows device specific output driver.
Definition: MpodWinMM.h:69
Container of output devices.
Definition: MpOutputDeviceManager.h:66
static UtlString getDefaultDeviceName()
get the windows name of the default wave output device.
Definition: MpodWinMM.cpp:1178
IMMDeviceEnumerator * mDeviceEnumeratorPtr
Definition: MpodWinMM.h:204
unsigned mWaveBufSize
Definition: MpodWinMM.h:192
UINT mCbParamMsg
Definition: MpodWinMM.h:229
~MpodWinMM()
Destructor.
Definition: MpodWinMM.cpp:399
MpFrameTime mCurFrameTime
The current frame time for this device.
Definition: MpodWinMM.h:189
OsStatus disableDevice()
Uninitialize device driver.
Definition: MpodWinMM.cpp:679
WinAudioData()
Definition: MpodWinMM.h:227
#define DEFAULT_N_OUTPUT_BUFS
Definition: MpodWinMM.h:32
Container for device specific output driver.
Definition: MpOutputDeviceDriver.h:53
HWAVEOUT mDevHandle
The Microsoft handle for this audio input device.
Definition: MpodWinMM.h:188