wxutil.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. //------------------------------------------------------------------------------
  2. // File: WXUtil.h
  3. //
  4. // Desc: DirectShow base classes - defines helper classes and functions for
  5. // building multimedia filters.
  6. //
  7. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  8. //------------------------------------------------------------------------------
  9. #ifndef __WXUTIL__
  10. #define __WXUTIL__
  11. // eliminate spurious "statement has no effect" warnings.
  12. #pragma warning(disable: 4705)
  13. // wrapper for whatever critical section we have
  14. class CCritSec
  15. {
  16. // make copy constructor and assignment operator inaccessible
  17. CCritSec(const CCritSec &refCritSec);
  18. CCritSec &operator=(const CCritSec &refCritSec);
  19. CRITICAL_SECTION m_CritSec;
  20. #ifdef DEBUG
  21. public:
  22. DWORD m_currentOwner;
  23. DWORD m_lockCount;
  24. BOOL m_fTrace; // Trace this one
  25. public:
  26. CCritSec();
  27. ~CCritSec();
  28. void Lock();
  29. void Unlock();
  30. #else
  31. public:
  32. CCritSec() {
  33. InitializeCriticalSection(&m_CritSec);
  34. };
  35. ~CCritSec() {
  36. DeleteCriticalSection(&m_CritSec);
  37. };
  38. void Lock() {
  39. EnterCriticalSection(&m_CritSec);
  40. };
  41. void Unlock() {
  42. LeaveCriticalSection(&m_CritSec);
  43. };
  44. #endif
  45. };
  46. //
  47. // To make deadlocks easier to track it is useful to insert in the
  48. // code an assertion that says whether we own a critical section or
  49. // not. We make the routines that do the checking globals to avoid
  50. // having different numbers of member functions in the debug and
  51. // retail class implementations of CCritSec. In addition we provide
  52. // a routine that allows usage of specific critical sections to be
  53. // traced. This is NOT on by default - there are far too many.
  54. //
  55. #ifdef DEBUG
  56. BOOL WINAPI CritCheckIn(CCritSec * pcCrit);
  57. BOOL WINAPI CritCheckIn(const CCritSec * pcCrit);
  58. BOOL WINAPI CritCheckOut(CCritSec * pcCrit);
  59. BOOL WINAPI CritCheckOut(const CCritSec * pcCrit);
  60. void WINAPI DbgLockTrace(CCritSec * pcCrit, BOOL fTrace);
  61. #else
  62. #define CritCheckIn(x) TRUE
  63. #define CritCheckOut(x) TRUE
  64. #define DbgLockTrace(pc, fT)
  65. #endif
  66. // locks a critical section, and unlocks it automatically
  67. // when the lock goes out of scope
  68. class CAutoLock
  69. {
  70. // make copy constructor and assignment operator inaccessible
  71. CAutoLock(const CAutoLock &refAutoLock);
  72. CAutoLock &operator=(const CAutoLock &refAutoLock);
  73. protected:
  74. CCritSec * m_pLock;
  75. public:
  76. CAutoLock(CCritSec * plock) {
  77. m_pLock = plock;
  78. m_pLock->Lock();
  79. };
  80. ~CAutoLock() {
  81. m_pLock->Unlock();
  82. };
  83. };
  84. // wrapper for event objects
  85. class CAMEvent
  86. {
  87. // make copy constructor and assignment operator inaccessible
  88. CAMEvent(const CAMEvent &refEvent);
  89. CAMEvent &operator=(const CAMEvent &refEvent);
  90. protected:
  91. HANDLE m_hEvent;
  92. public:
  93. CAMEvent(BOOL fManualReset = FALSE, __inout_opt HRESULT *phr = NULL);
  94. CAMEvent(__inout_opt HRESULT *phr);
  95. ~CAMEvent();
  96. // Cast to HANDLE - we don't support this as an lvalue
  97. operator HANDLE () const {
  98. return m_hEvent;
  99. };
  100. void Set() {
  101. EXECUTE_ASSERT(SetEvent(m_hEvent));
  102. };
  103. BOOL Wait(DWORD dwTimeout = INFINITE) {
  104. return (WaitForSingleObject(m_hEvent, dwTimeout) == WAIT_OBJECT_0);
  105. };
  106. void Reset() {
  107. ResetEvent(m_hEvent);
  108. };
  109. BOOL Check() {
  110. return Wait(0);
  111. };
  112. };
  113. // wrapper for event objects that do message processing
  114. // This adds ONE method to the CAMEvent object to allow sent
  115. // messages to be processed while waiting
  116. class CAMMsgEvent : public CAMEvent
  117. {
  118. public:
  119. CAMMsgEvent(__inout_opt HRESULT *phr = NULL);
  120. // Allow SEND messages to be processed while waiting
  121. BOOL WaitMsg(DWORD dwTimeout = INFINITE);
  122. };
  123. // old name supported for the time being
  124. #define CTimeoutEvent CAMEvent
  125. // support for a worker thread
  126. #ifdef AM_NOVTABLE
  127. // simple thread class supports creation of worker thread, synchronization
  128. // and communication. Can be derived to simplify parameter passing
  129. class AM_NOVTABLE CAMThread
  130. {
  131. // make copy constructor and assignment operator inaccessible
  132. CAMThread(const CAMThread &refThread);
  133. CAMThread &operator=(const CAMThread &refThread);
  134. CAMEvent m_EventSend;
  135. CAMEvent m_EventComplete;
  136. DWORD m_dwParam;
  137. DWORD m_dwReturnVal;
  138. protected:
  139. HANDLE m_hThread;
  140. // thread will run this function on startup
  141. // must be supplied by derived class
  142. virtual DWORD ThreadProc() = 0;
  143. public:
  144. CAMThread(__inout_opt HRESULT *phr = NULL);
  145. virtual ~CAMThread();
  146. CCritSec m_AccessLock; // locks access by client threads
  147. CCritSec m_WorkerLock; // locks access to shared objects
  148. // thread initially runs this. param is actually 'this'. function
  149. // just gets this and calls ThreadProc
  150. static DWORD WINAPI InitialThreadProc(__inout LPVOID pv);
  151. // start thread running - error if already running
  152. BOOL Create();
  153. // signal the thread, and block for a response
  154. //
  155. DWORD CallWorker(DWORD);
  156. // accessor thread calls this when done with thread (having told thread
  157. // to exit)
  158. void Close() {
  159. // Disable warning: Conversion from LONG to PVOID of greater size
  160. #pragma warning(push)
  161. #pragma warning(disable: 4312)
  162. HANDLE hThread = (HANDLE)InterlockedExchangePointer(&m_hThread, 0);
  163. #pragma warning(pop)
  164. if (hThread) {
  165. WaitForSingleObject(hThread, INFINITE);
  166. CloseHandle(hThread);
  167. }
  168. };
  169. // ThreadExists
  170. // Return TRUE if the thread exists. FALSE otherwise
  171. BOOL ThreadExists(void) const {
  172. if (m_hThread == 0) {
  173. return FALSE;
  174. }
  175. else {
  176. return TRUE;
  177. }
  178. }
  179. // wait for the next request
  180. DWORD GetRequest();
  181. // is there a request?
  182. BOOL CheckRequest(__out_opt DWORD * pParam);
  183. // reply to the request
  184. void Reply(DWORD);
  185. // If you want to do WaitForMultipleObjects you'll need to include
  186. // this handle in your wait list or you won't be responsive
  187. HANDLE GetRequestHandle() const {
  188. return m_EventSend;
  189. };
  190. // Find out what the request was
  191. DWORD GetRequestParam() const {
  192. return m_dwParam;
  193. };
  194. // call CoInitializeEx (COINIT_DISABLE_OLE1DDE) if
  195. // available. S_FALSE means it's not available.
  196. static HRESULT CoInitializeHelper();
  197. };
  198. #endif // AM_NOVTABLE
  199. // CQueue
  200. //
  201. // Implements a simple Queue ADT. The queue contains a finite number of
  202. // objects, access to which is controlled by a semaphore. The semaphore
  203. // is created with an initial count (N). Each time an object is added
  204. // a call to WaitForSingleObject is made on the semaphore's handle. When
  205. // this function returns a slot has been reserved in the queue for the new
  206. // object. If no slots are available the function blocks until one becomes
  207. // available. Each time an object is removed from the queue ReleaseSemaphore
  208. // is called on the semaphore's handle, thus freeing a slot in the queue.
  209. // If no objects are present in the queue the function blocks until an
  210. // object has been added.
  211. #define DEFAULT_QUEUESIZE 2
  212. template <class T> class CQueue
  213. {
  214. private:
  215. HANDLE hSemPut; // Semaphore controlling queue "putting"
  216. HANDLE hSemGet; // Semaphore controlling queue "getting"
  217. CRITICAL_SECTION CritSect; // Thread seriallization
  218. int nMax; // Max objects allowed in queue
  219. int iNextPut; // Array index of next "PutMsg"
  220. int iNextGet; // Array index of next "GetMsg"
  221. T *QueueObjects; // Array of objects (ptr's to void)
  222. void Initialize(int n) {
  223. iNextPut = iNextGet = 0;
  224. nMax = n;
  225. InitializeCriticalSection(&CritSect);
  226. hSemPut = CreateSemaphore(NULL, n, n, NULL);
  227. hSemGet = CreateSemaphore(NULL, 0, n, NULL);
  228. QueueObjects = new T[n];
  229. }
  230. public:
  231. CQueue(int n) {
  232. Initialize(n);
  233. }
  234. CQueue() {
  235. Initialize(DEFAULT_QUEUESIZE);
  236. }
  237. ~CQueue() {
  238. delete [] QueueObjects;
  239. DeleteCriticalSection(&CritSect);
  240. CloseHandle(hSemPut);
  241. CloseHandle(hSemGet);
  242. }
  243. T GetQueueObject() {
  244. int iSlot;
  245. T Object;
  246. LONG lPrevious;
  247. // Wait for someone to put something on our queue, returns straight
  248. // away is there is already an object on the queue.
  249. //
  250. WaitForSingleObject(hSemGet, INFINITE);
  251. EnterCriticalSection(&CritSect);
  252. iSlot = iNextGet++ % nMax;
  253. Object = QueueObjects[iSlot];
  254. LeaveCriticalSection(&CritSect);
  255. // Release anyone waiting to put an object onto our queue as there
  256. // is now space available in the queue.
  257. //
  258. ReleaseSemaphore(hSemPut, 1L, &lPrevious);
  259. return Object;
  260. }
  261. void PutQueueObject(T Object) {
  262. int iSlot;
  263. LONG lPrevious;
  264. // Wait for someone to get something from our queue, returns straight
  265. // away is there is already an empty slot on the queue.
  266. //
  267. WaitForSingleObject(hSemPut, INFINITE);
  268. EnterCriticalSection(&CritSect);
  269. iSlot = iNextPut++ % nMax;
  270. QueueObjects[iSlot] = Object;
  271. LeaveCriticalSection(&CritSect);
  272. // Release anyone waiting to remove an object from our queue as there
  273. // is now an object available to be removed.
  274. //
  275. ReleaseSemaphore(hSemGet, 1L, &lPrevious);
  276. }
  277. };
  278. // Ensures that memory is not read past the length source buffer
  279. // and that memory is not written past the length of the dst buffer
  280. // dst - buffer to copy to
  281. // dst_size - total size of destination buffer
  282. // cb_dst_offset - offset, first byte copied to dst+cb_dst_offset
  283. // src - buffer to copy from
  284. // src_size - total size of source buffer
  285. // cb_src_offset - offset, first byte copied from src+cb_src_offset
  286. // count - number of bytes to copy
  287. //
  288. // Returns:
  289. // S_OK - no error
  290. // E_INVALIDARG - values passed would lead to overrun
  291. HRESULT AMSafeMemMoveOffset(
  292. __in_bcount(dst_size) void * dst,
  293. __in size_t dst_size,
  294. __in DWORD cb_dst_offset,
  295. __in_bcount(src_size) const void * src,
  296. __in size_t src_size,
  297. __in DWORD cb_src_offset,
  298. __in size_t count);
  299. extern "C"
  300. void * __stdcall memmoveInternal(void *, const void *, size_t);
  301. inline void * __cdecl memchrInternal(const void *buf, int chr, size_t cnt)
  302. {
  303. #ifdef _X86_
  304. void *pRet = NULL;
  305. _asm {
  306. cld // make sure we get the direction right
  307. mov ecx, cnt // num of bytes to scan
  308. mov edi, buf // pointer byte stream
  309. mov eax, chr // byte to scan for
  310. repne scasb // look for the byte in the byte stream
  311. jnz exit_memchr // Z flag set if byte found
  312. dec edi // scasb always increments edi even when it
  313. // finds the required byte
  314. mov pRet, edi
  315. exit_memchr:
  316. }
  317. return pRet;
  318. #else
  319. while ( cnt && (*(unsigned char *)buf != (unsigned char)chr) ) {
  320. buf = (unsigned char *)buf + 1;
  321. cnt--;
  322. }
  323. return(cnt ? (void *)buf : NULL);
  324. #endif
  325. }
  326. void WINAPI IntToWstr(int i, __out_ecount(12) LPWSTR wstr);
  327. #define WstrToInt(sz) _wtoi(sz)
  328. #define atoiW(sz) _wtoi(sz)
  329. #define atoiA(sz) atoi(sz)
  330. // These are available to help managing bitmap VIDEOINFOHEADER media structures
  331. extern const DWORD bits555[3];
  332. extern const DWORD bits565[3];
  333. extern const DWORD bits888[3];
  334. // These help convert between VIDEOINFOHEADER and BITMAPINFO structures
  335. STDAPI_(const GUID) GetTrueColorType(const BITMAPINFOHEADER *pbmiHeader);
  336. STDAPI_(const GUID) GetBitmapSubtype(const BITMAPINFOHEADER *pbmiHeader);
  337. STDAPI_(WORD) GetBitCount(const GUID *pSubtype);
  338. // strmbase.lib implements this for compatibility with people who
  339. // managed to link to this directly. we don't want to advertise it.
  340. //
  341. // STDAPI_(/* T */ CHAR *) GetSubtypeName(const GUID *pSubtype);
  342. STDAPI_(CHAR *) GetSubtypeNameA(const GUID *pSubtype);
  343. STDAPI_(WCHAR *) GetSubtypeNameW(const GUID *pSubtype);
  344. #ifdef UNICODE
  345. #define GetSubtypeName GetSubtypeNameW
  346. #else
  347. #define GetSubtypeName GetSubtypeNameA
  348. #endif
  349. STDAPI_(LONG) GetBitmapFormatSize(const BITMAPINFOHEADER *pHeader);
  350. STDAPI_(DWORD) GetBitmapSize(const BITMAPINFOHEADER *pHeader);
  351. #ifdef __AMVIDEO__
  352. STDAPI_(BOOL) ContainsPalette(const VIDEOINFOHEADER *pVideoInfo);
  353. STDAPI_(const RGBQUAD *) GetBitmapPalette(const VIDEOINFOHEADER *pVideoInfo);
  354. #endif // __AMVIDEO__
  355. // Compares two interfaces and returns TRUE if they are on the same object
  356. BOOL WINAPI IsEqualObject(IUnknown *pFirst, IUnknown *pSecond);
  357. // This is for comparing pins
  358. #define EqualPins(pPin1, pPin2) IsEqualObject(pPin1, pPin2)
  359. // Arithmetic helper functions
  360. // Compute (a * b + rnd) / c
  361. LONGLONG WINAPI llMulDiv(LONGLONG a, LONGLONG b, LONGLONG c, LONGLONG rnd);
  362. LONGLONG WINAPI Int64x32Div32(LONGLONG a, LONG b, LONG c, LONG rnd);
  363. // Avoids us dyna-linking to SysAllocString to copy BSTR strings
  364. STDAPI WriteBSTR(__deref_out BSTR * pstrDest, LPCWSTR szSrc);
  365. STDAPI FreeBSTR(__deref_in BSTR* pstr);
  366. // Return a wide string - allocating memory for it
  367. // Returns:
  368. // S_OK - no error
  369. // E_POINTER - ppszReturn == NULL
  370. // E_OUTOFMEMORY - can't allocate memory for returned string
  371. STDAPI AMGetWideString(LPCWSTR pszString, __deref_out LPWSTR *ppszReturn);
  372. // Special wait for objects owning windows
  373. DWORD WINAPI WaitDispatchingMessages(
  374. HANDLE hObject,
  375. DWORD dwWait,
  376. HWND hwnd = NULL,
  377. UINT uMsg = 0,
  378. HANDLE hEvent = NULL);
  379. // HRESULT_FROM_WIN32 converts ERROR_SUCCESS to a success code, but in
  380. // our use of HRESULT_FROM_WIN32, it typically means a function failed
  381. // to call SetLastError(), and we still want a failure code.
  382. //
  383. #define AmHresultFromWin32(x) (MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, x))
  384. // call GetLastError and return an HRESULT value that will fail the
  385. // SUCCEEDED() macro.
  386. HRESULT AmGetLastErrorToHResult(void);
  387. // duplicate of ATL's CComPtr to avoid linker conflicts.
  388. IUnknown* QzAtlComPtrAssign(__deref_inout_opt IUnknown** pp, __in_opt IUnknown* lp);
  389. template <class T>
  390. class QzCComPtr
  391. {
  392. public:
  393. typedef T _PtrClass;
  394. QzCComPtr() {
  395. p=NULL;
  396. }
  397. QzCComPtr(T* lp) {
  398. if ((p = lp) != NULL) {
  399. p->AddRef();
  400. }
  401. }
  402. QzCComPtr(const QzCComPtr<T>& lp) {
  403. if ((p = lp.p) != NULL) {
  404. p->AddRef();
  405. }
  406. }
  407. ~QzCComPtr() {
  408. if (p) {
  409. p->Release();
  410. }
  411. }
  412. void Release() {
  413. if (p) {
  414. p->Release();
  415. }
  416. p=NULL;
  417. }
  418. operator T*() {
  419. return (T*)p;
  420. }
  421. T& operator*() {
  422. ASSERT(p!=NULL);
  423. return *p;
  424. }
  425. //The assert on operator& usually indicates a bug. If this is really
  426. //what is needed, however, take the address of the p member explicitly.
  427. T** operator&() {
  428. ASSERT(p==NULL);
  429. return &p;
  430. }
  431. T* operator->() {
  432. ASSERT(p!=NULL);
  433. return p;
  434. }
  435. T* operator=(T* lp) {
  436. return (T*)QzAtlComPtrAssign((IUnknown**)&p, lp);
  437. }
  438. T* operator=(const QzCComPtr<T>& lp) {
  439. return (T*)QzAtlComPtrAssign((IUnknown**)&p, lp.p);
  440. }
  441. #if _MSC_VER>1020
  442. bool operator!() {
  443. return (p == NULL);
  444. }
  445. #else
  446. BOOL operator!() {
  447. return (p == NULL) ? TRUE : FALSE;
  448. }
  449. #endif
  450. T* p;
  451. };
  452. MMRESULT CompatibleTimeSetEvent( UINT uDelay, UINT uResolution, __in LPTIMECALLBACK lpTimeProc, DWORD_PTR dwUser, UINT fuEvent );
  453. bool TimeKillSynchronousFlagAvailable( void );
  454. // Helper to replace lstrcpmi
  455. __inline int lstrcmpiLocaleIndependentW(LPCWSTR lpsz1, LPCWSTR lpsz2)
  456. {
  457. return CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, lpsz1, -1, lpsz2, -1) - CSTR_EQUAL;
  458. }
  459. __inline int lstrcmpiLocaleIndependentA(LPCSTR lpsz1, LPCSTR lpsz2)
  460. {
  461. return CompareStringA(LOCALE_INVARIANT, NORM_IGNORECASE, lpsz1, -1, lpsz2, -1) - CSTR_EQUAL;
  462. }
  463. #endif /* __WXUTIL__ */