wxdebug.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. //------------------------------------------------------------------------------
  2. // File: WXDebug.cpp
  3. //
  4. // Desc: DirectShow base classes - implements ActiveX system debugging
  5. // facilities.
  6. //
  7. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  8. //------------------------------------------------------------------------------
  9. #include <pjmedia-videodev/config.h>
  10. #if defined(PJMEDIA_VIDEO_DEV_HAS_DSHOW) && PJMEDIA_VIDEO_DEV_HAS_DSHOW != 0
  11. #define _WINDLL
  12. #include <streams.h>
  13. #include <stdarg.h>
  14. #include <stdio.h>
  15. #include <dvdmedia.h>
  16. #ifdef DEBUG
  17. #ifdef UNICODE
  18. #ifndef _UNICODE
  19. #define _UNICODE
  20. #endif // _UNICODE
  21. #endif // UNICODE
  22. #endif // DEBUG
  23. #include <tchar.h>
  24. #include <strsafe.h>
  25. #ifdef DEBUG
  26. static void DisplayBITMAPINFO(const BITMAPINFOHEADER* pbmi);
  27. static void DisplayRECT(LPCTSTR szLabel, const RECT& rc);
  28. // The Win32 wsprintf() function writes a maximum of 1024 characters to it's output buffer.
  29. // See the documentation for wsprintf()'s lpOut parameter for more information.
  30. const INT iDEBUGINFO = 1024; // Used to format strings
  31. /* For every module and executable we store a debugging level for each of
  32. the five categories (eg LOG_ERROR and LOG_TIMING). This makes it easy
  33. to isolate and debug individual modules without seeing everybody elses
  34. spurious debug output. The keys are stored in the registry under the
  35. HKEY_LOCAL_MACHINE\SOFTWARE\Debug\<Module Name>\<KeyName> key values
  36. NOTE these must be in the same order as their enumeration definition */
  37. const LPCTSTR pKeyNames[] = {
  38. TEXT("TIMING"), // Timing and performance measurements
  39. TEXT("TRACE"), // General step point call tracing
  40. TEXT("MEMORY"), // Memory and object allocation/destruction
  41. TEXT("LOCKING"), // Locking/unlocking of critical sections
  42. TEXT("ERROR"), // Debug error notification
  43. TEXT("CUSTOM1"),
  44. TEXT("CUSTOM2"),
  45. TEXT("CUSTOM3"),
  46. TEXT("CUSTOM4"),
  47. TEXT("CUSTOM5")
  48. };
  49. const TCHAR CAutoTrace::_szEntering[] = TEXT("->: %s");
  50. const TCHAR CAutoTrace::_szLeaving[] = TEXT("<-: %s");
  51. const INT iMAXLEVELS = NUMELMS(pKeyNames); // Maximum debug categories
  52. HINSTANCE m_hInst; // Module instance handle
  53. TCHAR m_ModuleName[iDEBUGINFO]; // Cut down module name
  54. DWORD m_Levels[iMAXLEVELS]; // Debug level per category
  55. CRITICAL_SECTION m_CSDebug; // Controls access to list
  56. DWORD m_dwNextCookie; // Next active object ID
  57. ObjectDesc *pListHead = NULL; // First active object
  58. DWORD m_dwObjectCount; // Active object count
  59. BOOL m_bInit = FALSE; // Have we been initialised
  60. HANDLE m_hOutput = INVALID_HANDLE_VALUE; // Optional output written here
  61. DWORD dwWaitTimeout = INFINITE; // Default timeout value
  62. DWORD dwTimeOffset; // Time of first DbgLog call
  63. bool g_fUseKASSERT = false; // don't create messagebox
  64. bool g_fDbgInDllEntryPoint = false;
  65. bool g_fAutoRefreshLevels = false;
  66. LPCTSTR pBaseKey = TEXT("SOFTWARE\\Microsoft\\DirectShow\\Debug");
  67. LPCTSTR pGlobalKey = TEXT("GLOBAL");
  68. static CHAR *pUnknownName = "UNKNOWN";
  69. LPCTSTR TimeoutName = TEXT("TIMEOUT");
  70. /* This sets the instance handle that the debug library uses to find
  71. the module's file name from the Win32 GetModuleFileName function */
  72. void WINAPI DbgInitialise(HINSTANCE hInst)
  73. {
  74. InitializeCriticalSection(&m_CSDebug);
  75. m_bInit = TRUE;
  76. m_hInst = hInst;
  77. DbgInitModuleName();
  78. if (GetProfileInt(m_ModuleName, TEXT("BreakOnLoad"), 0))
  79. DebugBreak();
  80. DbgInitModuleSettings(false);
  81. DbgInitGlobalSettings(true);
  82. dwTimeOffset = timeGetTime();
  83. }
  84. /* This is called to clear up any resources the debug library uses - at the
  85. moment we delete our critical section and the object list. The values we
  86. retrieve from the registry are all done during initialisation but we don't
  87. go looking for update notifications while we are running, if the values
  88. are changed then the application has to be restarted to pick them up */
  89. void WINAPI DbgTerminate()
  90. {
  91. if (m_hOutput != INVALID_HANDLE_VALUE) {
  92. EXECUTE_ASSERT(CloseHandle(m_hOutput));
  93. m_hOutput = INVALID_HANDLE_VALUE;
  94. }
  95. DeleteCriticalSection(&m_CSDebug);
  96. m_bInit = FALSE;
  97. }
  98. /* This is called by DbgInitLogLevels to read the debug settings
  99. for each logging category for this module from the registry */
  100. void WINAPI DbgInitKeyLevels(HKEY hKey, bool fTakeMax)
  101. {
  102. LONG lReturn; // Create key return value
  103. LONG lKeyPos; // Current key category
  104. DWORD dwKeySize; // Size of the key value
  105. DWORD dwKeyType; // Receives it's type
  106. DWORD dwKeyValue; // This fields value
  107. /* Try and read a value for each key position in turn */
  108. for (lKeyPos = 0;lKeyPos < iMAXLEVELS;lKeyPos++) {
  109. dwKeySize = sizeof(DWORD);
  110. lReturn = RegQueryValueEx(
  111. hKey, // Handle to an open key
  112. pKeyNames[lKeyPos], // Subkey name derivation
  113. NULL, // Reserved field
  114. &dwKeyType, // Returns the field type
  115. (LPBYTE) &dwKeyValue, // Returns the field's value
  116. &dwKeySize ); // Number of bytes transferred
  117. /* If either the key was not available or it was not a DWORD value
  118. then we ensure only the high priority debug logging is output
  119. but we try and update the field to a zero filled DWORD value */
  120. if (lReturn != ERROR_SUCCESS || dwKeyType != REG_DWORD) {
  121. dwKeyValue = 0;
  122. lReturn = RegSetValueEx(
  123. hKey, // Handle of an open key
  124. pKeyNames[lKeyPos], // Address of subkey name
  125. (DWORD) 0, // Reserved field
  126. REG_DWORD, // Type of the key field
  127. (PBYTE) &dwKeyValue, // Value for the field
  128. sizeof(DWORD)); // Size of the field buffer
  129. if (lReturn != ERROR_SUCCESS) {
  130. DbgLog((LOG_ERROR,1,TEXT("Could not create subkey %s"),pKeyNames[lKeyPos]));
  131. dwKeyValue = 0;
  132. }
  133. }
  134. if(fTakeMax)
  135. {
  136. m_Levels[lKeyPos] = max(dwKeyValue,m_Levels[lKeyPos]);
  137. }
  138. else
  139. {
  140. if((m_Levels[lKeyPos] & LOG_FORCIBLY_SET) == 0) {
  141. m_Levels[lKeyPos] = dwKeyValue;
  142. }
  143. }
  144. }
  145. /* Read the timeout value for catching hangs */
  146. dwKeySize = sizeof(DWORD);
  147. lReturn = RegQueryValueEx(
  148. hKey, // Handle to an open key
  149. TimeoutName, // Subkey name derivation
  150. NULL, // Reserved field
  151. &dwKeyType, // Returns the field type
  152. (LPBYTE) &dwWaitTimeout, // Returns the field's value
  153. &dwKeySize ); // Number of bytes transferred
  154. /* If either the key was not available or it was not a DWORD value
  155. then we ensure only the high priority debug logging is output
  156. but we try and update the field to a zero filled DWORD value */
  157. if (lReturn != ERROR_SUCCESS || dwKeyType != REG_DWORD) {
  158. dwWaitTimeout = INFINITE;
  159. lReturn = RegSetValueEx(
  160. hKey, // Handle of an open key
  161. TimeoutName, // Address of subkey name
  162. (DWORD) 0, // Reserved field
  163. REG_DWORD, // Type of the key field
  164. (PBYTE) &dwWaitTimeout, // Value for the field
  165. sizeof(DWORD)); // Size of the field buffer
  166. if (lReturn != ERROR_SUCCESS) {
  167. DbgLog((LOG_ERROR,1,TEXT("Could not create subkey %s"),pKeyNames[lKeyPos]));
  168. dwWaitTimeout = INFINITE;
  169. }
  170. }
  171. }
  172. void WINAPI DbgOutString(LPCTSTR psz)
  173. {
  174. if (m_hOutput != INVALID_HANDLE_VALUE) {
  175. UINT cb = lstrlen(psz);
  176. DWORD dw;
  177. #ifdef UNICODE
  178. CHAR szDest[2048];
  179. WideCharToMultiByte(CP_ACP, 0, psz, -1, szDest, NUMELMS(szDest), 0, 0);
  180. WriteFile (m_hOutput, szDest, cb, &dw, NULL);
  181. #else
  182. WriteFile (m_hOutput, psz, cb, &dw, NULL);
  183. #endif
  184. } else {
  185. OutputDebugString (psz);
  186. }
  187. }
  188. HRESULT DbgUniqueProcessName(LPCTSTR inName, LPTSTR outName)
  189. {
  190. HRESULT hr = S_OK;
  191. const TCHAR *pIn = inName;
  192. int dotPos = -1;
  193. //scan the input and record the last '.' position
  194. while (*pIn && (pIn - inName) < MAX_PATH)
  195. {
  196. if ( TEXT('.') == *pIn )
  197. dotPos = (int)(pIn-inName);
  198. ++pIn;
  199. }
  200. if (*pIn) //input should be zero-terminated within MAX_PATH
  201. return E_INVALIDARG;
  202. DWORD dwProcessId = GetCurrentProcessId();
  203. if (dotPos < 0)
  204. {
  205. //no extension in the input, appending process id to the input
  206. hr = StringCchPrintf(outName, MAX_PATH, TEXT("%s_%d"), inName, dwProcessId);
  207. }
  208. else
  209. {
  210. TCHAR pathAndBasename[MAX_PATH] = {0};
  211. //there's an extension - zero-terminate the path and basename first by copying
  212. hr = StringCchCopyN(pathAndBasename, MAX_PATH, inName, (size_t)dotPos);
  213. //re-combine path, basename and extension with processId appended to a basename
  214. if (SUCCEEDED(hr))
  215. hr = StringCchPrintf(outName, MAX_PATH, TEXT("%s_%d%s"), pathAndBasename, dwProcessId, inName + dotPos);
  216. }
  217. return hr;
  218. }
  219. /* Called by DbgInitGlobalSettings to setup alternate logging destinations
  220. */
  221. void WINAPI DbgInitLogTo (
  222. HKEY hKey)
  223. {
  224. LONG lReturn;
  225. DWORD dwKeyType;
  226. DWORD dwKeySize;
  227. TCHAR szFile[MAX_PATH] = {0};
  228. static const TCHAR cszKey[] = TEXT("LogToFile");
  229. dwKeySize = MAX_PATH;
  230. lReturn = RegQueryValueEx(
  231. hKey, // Handle to an open key
  232. cszKey, // Subkey name derivation
  233. NULL, // Reserved field
  234. &dwKeyType, // Returns the field type
  235. (LPBYTE) szFile, // Returns the field's value
  236. &dwKeySize); // Number of bytes transferred
  237. // create an empty key if it does not already exist
  238. //
  239. if (lReturn != ERROR_SUCCESS || dwKeyType != REG_SZ)
  240. {
  241. dwKeySize = sizeof(TCHAR);
  242. lReturn = RegSetValueEx(
  243. hKey, // Handle of an open key
  244. cszKey, // Address of subkey name
  245. (DWORD) 0, // Reserved field
  246. REG_SZ, // Type of the key field
  247. (PBYTE)szFile, // Value for the field
  248. dwKeySize); // Size of the field buffer
  249. }
  250. // if an output-to was specified. try to open it.
  251. //
  252. if (m_hOutput != INVALID_HANDLE_VALUE) {
  253. EXECUTE_ASSERT(CloseHandle (m_hOutput));
  254. m_hOutput = INVALID_HANDLE_VALUE;
  255. }
  256. if (szFile[0] != 0)
  257. {
  258. if (!lstrcmpi(szFile, TEXT("Console"))) {
  259. m_hOutput = GetStdHandle (STD_OUTPUT_HANDLE);
  260. if (m_hOutput == INVALID_HANDLE_VALUE) {
  261. AllocConsole ();
  262. m_hOutput = GetStdHandle (STD_OUTPUT_HANDLE);
  263. }
  264. SetConsoleTitle (TEXT("ActiveX Debug Output"));
  265. } else if (szFile[0] &&
  266. lstrcmpi(szFile, TEXT("Debug")) &&
  267. lstrcmpi(szFile, TEXT("Debugger")) &&
  268. lstrcmpi(szFile, TEXT("Deb")))
  269. {
  270. m_hOutput = CreateFile(szFile, GENERIC_WRITE,
  271. FILE_SHARE_READ,
  272. NULL, OPEN_ALWAYS,
  273. FILE_ATTRIBUTE_NORMAL,
  274. NULL);
  275. if (INVALID_HANDLE_VALUE == m_hOutput &&
  276. GetLastError() == ERROR_SHARING_VIOLATION)
  277. {
  278. TCHAR uniqueName[MAX_PATH] = {0};
  279. if (SUCCEEDED(DbgUniqueProcessName(szFile, uniqueName)))
  280. {
  281. m_hOutput = CreateFile(uniqueName, GENERIC_WRITE,
  282. FILE_SHARE_READ,
  283. NULL, OPEN_ALWAYS,
  284. FILE_ATTRIBUTE_NORMAL,
  285. NULL);
  286. }
  287. }
  288. if (INVALID_HANDLE_VALUE != m_hOutput)
  289. {
  290. static const TCHAR cszBar[] = TEXT("\r\n\r\n=====DbgInitialize()=====\r\n\r\n");
  291. SetFilePointer (m_hOutput, 0, NULL, FILE_END);
  292. DbgOutString (cszBar);
  293. }
  294. }
  295. }
  296. }
  297. /* This is called by DbgInitLogLevels to read the global debug settings for
  298. each logging category for this module from the registry. Normally each
  299. module has it's own values set for it's different debug categories but
  300. setting the global SOFTWARE\Debug\Global applies them to ALL modules */
  301. void WINAPI DbgInitGlobalSettings(bool fTakeMax)
  302. {
  303. LONG lReturn; // Create key return value
  304. TCHAR szInfo[iDEBUGINFO]; // Constructs key names
  305. HKEY hGlobalKey; // Global override key
  306. /* Construct the global base key name */
  307. (void)StringCchPrintf(szInfo,NUMELMS(szInfo),TEXT("%s\\%s"),pBaseKey,pGlobalKey);
  308. /* Create or open the key for this module */
  309. lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, // Handle of an open key
  310. szInfo, // Address of subkey name
  311. (DWORD) 0, // Reserved value
  312. NULL, // Address of class name
  313. (DWORD) 0, // Special options flags
  314. GENERIC_READ | GENERIC_WRITE, // Desired security access
  315. NULL, // Key security descriptor
  316. &hGlobalKey, // Opened handle buffer
  317. NULL); // What really happened
  318. if (lReturn != ERROR_SUCCESS) {
  319. lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, // Handle of an open key
  320. szInfo, // Address of subkey name
  321. (DWORD) 0, // Reserved value
  322. NULL, // Address of class name
  323. (DWORD) 0, // Special options flags
  324. GENERIC_READ, // Desired security access
  325. NULL, // Key security descriptor
  326. &hGlobalKey, // Opened handle buffer
  327. NULL); // What really happened
  328. if (lReturn != ERROR_SUCCESS) {
  329. DbgLog((LOG_ERROR,1,TEXT("Could not access GLOBAL module key")));
  330. }
  331. return;
  332. }
  333. DbgInitKeyLevels(hGlobalKey, fTakeMax);
  334. RegCloseKey(hGlobalKey);
  335. }
  336. /* This sets the debugging log levels for the different categories. We start
  337. by opening (or creating if not already available) the SOFTWARE\Debug key
  338. that all these settings live under. We then look at the global values
  339. set under SOFTWARE\Debug\Global which apply on top of the individual
  340. module settings. We then load the individual module registry settings */
  341. void WINAPI DbgInitModuleSettings(bool fTakeMax)
  342. {
  343. LONG lReturn; // Create key return value
  344. TCHAR szInfo[iDEBUGINFO]; // Constructs key names
  345. HKEY hModuleKey; // Module key handle
  346. /* Construct the base key name */
  347. (void)StringCchPrintf(szInfo,NUMELMS(szInfo),TEXT("%s\\%s"),pBaseKey,m_ModuleName);
  348. /* Create or open the key for this module */
  349. lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, // Handle of an open key
  350. szInfo, // Address of subkey name
  351. (DWORD) 0, // Reserved value
  352. NULL, // Address of class name
  353. (DWORD) 0, // Special options flags
  354. GENERIC_READ | GENERIC_WRITE, // Desired security access
  355. NULL, // Key security descriptor
  356. &hModuleKey, // Opened handle buffer
  357. NULL); // What really happened
  358. if (lReturn != ERROR_SUCCESS) {
  359. lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, // Handle of an open key
  360. szInfo, // Address of subkey name
  361. (DWORD) 0, // Reserved value
  362. NULL, // Address of class name
  363. (DWORD) 0, // Special options flags
  364. GENERIC_READ, // Desired security access
  365. NULL, // Key security descriptor
  366. &hModuleKey, // Opened handle buffer
  367. NULL); // What really happened
  368. if (lReturn != ERROR_SUCCESS) {
  369. DbgLog((LOG_ERROR,1,TEXT("Could not access module key")));
  370. }
  371. return;
  372. }
  373. DbgInitLogTo(hModuleKey);
  374. DbgInitKeyLevels(hModuleKey, fTakeMax);
  375. RegCloseKey(hModuleKey);
  376. }
  377. /* Initialise the module file name */
  378. void WINAPI DbgInitModuleName()
  379. {
  380. TCHAR FullName[iDEBUGINFO]; // Load the full path and module name
  381. LPTSTR pName; // Searches from the end for a backslash
  382. GetModuleFileName(m_hInst,FullName,iDEBUGINFO);
  383. pName = _tcsrchr(FullName,'\\');
  384. if (pName == NULL) {
  385. pName = FullName;
  386. } else {
  387. pName++;
  388. }
  389. (void)StringCchCopy(m_ModuleName,NUMELMS(m_ModuleName), pName);
  390. }
  391. struct MsgBoxMsg
  392. {
  393. HWND hwnd;
  394. LPCTSTR szTitle;
  395. LPCTSTR szMessage;
  396. DWORD dwFlags;
  397. INT iResult;
  398. };
  399. //
  400. // create a thread to call MessageBox(). calling MessageBox() on
  401. // random threads at bad times can confuse the host (eg IE).
  402. //
  403. DWORD WINAPI MsgBoxThread(
  404. __inout LPVOID lpParameter // thread data
  405. )
  406. {
  407. MsgBoxMsg *pmsg = (MsgBoxMsg *)lpParameter;
  408. pmsg->iResult = MessageBox(
  409. pmsg->hwnd,
  410. pmsg->szTitle,
  411. pmsg->szMessage,
  412. pmsg->dwFlags);
  413. return 0;
  414. }
  415. INT MessageBoxOtherThread(
  416. HWND hwnd,
  417. LPCTSTR szTitle,
  418. LPCTSTR szMessage,
  419. DWORD dwFlags)
  420. {
  421. if(g_fDbgInDllEntryPoint)
  422. {
  423. // can't wait on another thread because we have the loader
  424. // lock held in the dll entry point.
  425. // This can crash sometimes so just skip it
  426. // return MessageBox(hwnd, szTitle, szMessage, dwFlags);
  427. return IDCANCEL;
  428. }
  429. else
  430. {
  431. MsgBoxMsg msg = {hwnd, szTitle, szMessage, dwFlags, 0};
  432. DWORD dwid;
  433. HANDLE hThread = CreateThread(
  434. 0, // security
  435. 0, // stack size
  436. MsgBoxThread,
  437. (void *)&msg, // arg
  438. 0, // flags
  439. &dwid);
  440. if(hThread)
  441. {
  442. WaitForSingleObject(hThread, INFINITE);
  443. CloseHandle(hThread);
  444. return msg.iResult;
  445. }
  446. // break into debugger on failure.
  447. return IDCANCEL;
  448. }
  449. }
  450. /* Displays a message box if the condition evaluated to FALSE */
  451. void WINAPI DbgAssert(LPCTSTR pCondition,LPCTSTR pFileName,INT iLine)
  452. {
  453. if(g_fUseKASSERT)
  454. {
  455. DbgKernelAssert(pCondition, pFileName, iLine);
  456. }
  457. else
  458. {
  459. TCHAR szInfo[iDEBUGINFO];
  460. (void)StringCchPrintf(szInfo, NUMELMS(szInfo),TEXT("%s \nAt line %d of %s\nContinue? (Cancel to debug)"),
  461. pCondition, iLine, pFileName);
  462. INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("ASSERT Failed"),
  463. MB_SYSTEMMODAL |
  464. MB_ICONHAND |
  465. MB_YESNOCANCEL |
  466. MB_SETFOREGROUND);
  467. switch (MsgId)
  468. {
  469. case IDNO: /* Kill the application */
  470. FatalAppExit(FALSE, TEXT("Application terminated"));
  471. break;
  472. case IDCANCEL: /* Break into the debugger */
  473. DebugBreak();
  474. break;
  475. case IDYES: /* Ignore assertion continue execution */
  476. break;
  477. }
  478. }
  479. }
  480. /* Displays a message box at a break point */
  481. void WINAPI DbgBreakPoint(LPCTSTR pCondition,LPCTSTR pFileName,INT iLine)
  482. {
  483. if(g_fUseKASSERT)
  484. {
  485. DbgKernelAssert(pCondition, pFileName, iLine);
  486. }
  487. else
  488. {
  489. TCHAR szInfo[iDEBUGINFO];
  490. (void)StringCchPrintf(szInfo, NUMELMS(szInfo),TEXT("%s \nAt line %d of %s\nContinue? (Cancel to debug)"),
  491. pCondition, iLine, pFileName);
  492. INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("Hard coded break point"),
  493. MB_SYSTEMMODAL |
  494. MB_ICONHAND |
  495. MB_YESNOCANCEL |
  496. MB_SETFOREGROUND);
  497. switch (MsgId)
  498. {
  499. case IDNO: /* Kill the application */
  500. FatalAppExit(FALSE, TEXT("Application terminated"));
  501. break;
  502. case IDCANCEL: /* Break into the debugger */
  503. DebugBreak();
  504. break;
  505. case IDYES: /* Ignore break point continue execution */
  506. break;
  507. }
  508. }
  509. }
  510. void WINAPI DbgBreakPoint(LPCTSTR pFileName,INT iLine,__format_string LPCTSTR szFormatString,...)
  511. {
  512. // A debug break point message can have at most 2000 characters if
  513. // ANSI or UNICODE characters are being used. A debug break point message
  514. // can have between 1000 and 2000 double byte characters in it. If a
  515. // particular message needs more characters, then the value of this constant
  516. // should be increased.
  517. const DWORD MAX_BREAK_POINT_MESSAGE_SIZE = 2000;
  518. TCHAR szBreakPointMessage[MAX_BREAK_POINT_MESSAGE_SIZE];
  519. va_list va;
  520. va_start( va, szFormatString );
  521. HRESULT hr = StringCchVPrintf( szBreakPointMessage, NUMELMS(szBreakPointMessage), szFormatString, va );
  522. va_end(va);
  523. if( FAILED(hr) ) {
  524. DbgBreak( "ERROR in DbgBreakPoint(). The variable length debug message could not be displayed because StringCchVPrintf() failed." );
  525. return;
  526. }
  527. ::DbgBreakPoint( szBreakPointMessage, pFileName, iLine );
  528. }
  529. /* When we initialised the library we stored in the m_Levels array the current
  530. debug output level for this module for each of the five categories. When
  531. some debug logging is sent to us it can be sent with a combination of the
  532. categories (if it is applicable to many for example) in which case we map
  533. the type's categories into their current debug levels and see if any of
  534. them can be accepted. The function looks at each bit position in turn from
  535. the input type field and then compares it's debug level with the modules.
  536. A level of 0 means that output is always sent to the debugger. This is
  537. due to producing output if the input level is <= m_Levels.
  538. */
  539. BOOL WINAPI DbgCheckModuleLevel(DWORD Type,DWORD Level)
  540. {
  541. if(g_fAutoRefreshLevels)
  542. {
  543. // re-read the registry every second. We cannot use RegNotify() to
  544. // notice registry changes because it's not available on win9x.
  545. static DWORD g_dwLastRefresh = 0;
  546. DWORD dwTime = timeGetTime();
  547. if(dwTime - g_dwLastRefresh > 1000) {
  548. g_dwLastRefresh = dwTime;
  549. // there's a race condition: multiple threads could update the
  550. // values. plus read and write not synchronized. no harm
  551. // though.
  552. DbgInitModuleSettings(false);
  553. }
  554. }
  555. DWORD Mask = 0x01;
  556. // If no valid bits are set return FALSE
  557. if ((Type & ((1<<iMAXLEVELS)-1))) {
  558. // speed up unconditional output.
  559. if (0==Level)
  560. return(TRUE);
  561. for (LONG lKeyPos = 0;lKeyPos < iMAXLEVELS;lKeyPos++) {
  562. if (Type & Mask) {
  563. if (Level <= (m_Levels[lKeyPos] & ~LOG_FORCIBLY_SET)) {
  564. return TRUE;
  565. }
  566. }
  567. Mask <<= 1;
  568. }
  569. }
  570. return FALSE;
  571. }
  572. /* Set debug levels to a given value */
  573. void WINAPI DbgSetModuleLevel(DWORD Type, DWORD Level)
  574. {
  575. DWORD Mask = 0x01;
  576. for (LONG lKeyPos = 0;lKeyPos < iMAXLEVELS;lKeyPos++) {
  577. if (Type & Mask) {
  578. m_Levels[lKeyPos] = Level | LOG_FORCIBLY_SET;
  579. }
  580. Mask <<= 1;
  581. }
  582. }
  583. /* whether to check registry values periodically. this isn't turned
  584. automatically because of the potential performance hit. */
  585. void WINAPI DbgSetAutoRefreshLevels(bool fAuto)
  586. {
  587. g_fAutoRefreshLevels = fAuto;
  588. }
  589. #ifdef UNICODE
  590. //
  591. // warning -- this function is implemented twice for ansi applications
  592. // linking to the unicode library
  593. //
  594. void WINAPI DbgLogInfo(DWORD Type,DWORD Level,__format_string LPCSTR pFormat,...)
  595. {
  596. /* Check the current level for this type combination */
  597. BOOL bAccept = DbgCheckModuleLevel(Type,Level);
  598. if (bAccept == FALSE) {
  599. return;
  600. }
  601. TCHAR szInfo[2000];
  602. /* Format the variable length parameter list */
  603. va_list va;
  604. va_start(va, pFormat);
  605. (void)StringCchPrintf(szInfo, NUMELMS(szInfo),
  606. TEXT("%s(tid %x) %8d : "),
  607. m_ModuleName,
  608. GetCurrentThreadId(), timeGetTime() - dwTimeOffset);
  609. CHAR szInfoA[2000];
  610. WideCharToMultiByte(CP_ACP, 0, szInfo, -1, szInfoA, NUMELMS(szInfoA), 0, 0);
  611. (void)StringCchVPrintfA(szInfoA + lstrlenA(szInfoA), NUMELMS(szInfoA) - lstrlenA(szInfoA), pFormat, va);
  612. (void)StringCchCatA(szInfoA, NUMELMS(szInfoA), "\r\n");
  613. WCHAR wszOutString[2000];
  614. MultiByteToWideChar(CP_ACP, 0, szInfoA, -1, wszOutString, NUMELMS(wszOutString));
  615. DbgOutString(wszOutString);
  616. va_end(va);
  617. }
  618. void WINAPI DbgAssert(LPCSTR pCondition,LPCSTR pFileName,INT iLine)
  619. {
  620. if(g_fUseKASSERT)
  621. {
  622. DbgKernelAssert(pCondition, pFileName, iLine);
  623. }
  624. else
  625. {
  626. TCHAR szInfo[iDEBUGINFO];
  627. (void)StringCchPrintf(szInfo, NUMELMS(szInfo), TEXT("%hs \nAt line %d of %hs\nContinue? (Cancel to debug)"),
  628. pCondition, iLine, pFileName);
  629. INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("ASSERT Failed"),
  630. MB_SYSTEMMODAL |
  631. MB_ICONHAND |
  632. MB_YESNOCANCEL |
  633. MB_SETFOREGROUND);
  634. switch (MsgId)
  635. {
  636. case IDNO: /* Kill the application */
  637. FatalAppExit(FALSE, TEXT("Application terminated"));
  638. break;
  639. case IDCANCEL: /* Break into the debugger */
  640. DebugBreak();
  641. break;
  642. case IDYES: /* Ignore assertion continue execution */
  643. break;
  644. }
  645. }
  646. }
  647. /* Displays a message box at a break point */
  648. void WINAPI DbgBreakPoint(LPCSTR pCondition,LPCSTR pFileName,INT iLine)
  649. {
  650. if(g_fUseKASSERT)
  651. {
  652. DbgKernelAssert(pCondition, pFileName, iLine);
  653. }
  654. else
  655. {
  656. TCHAR szInfo[iDEBUGINFO];
  657. (void)StringCchPrintf(szInfo, NUMELMS(szInfo),TEXT("%hs \nAt line %d of %hs\nContinue? (Cancel to debug)"),
  658. pCondition, iLine, pFileName);
  659. INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("Hard coded break point"),
  660. MB_SYSTEMMODAL |
  661. MB_ICONHAND |
  662. MB_YESNOCANCEL |
  663. MB_SETFOREGROUND);
  664. switch (MsgId)
  665. {
  666. case IDNO: /* Kill the application */
  667. FatalAppExit(FALSE, TEXT("Application terminated"));
  668. break;
  669. case IDCANCEL: /* Break into the debugger */
  670. DebugBreak();
  671. break;
  672. case IDYES: /* Ignore break point continue execution */
  673. break;
  674. }
  675. }
  676. }
  677. void WINAPI DbgKernelAssert(LPCSTR pCondition,LPCSTR pFileName,INT iLine)
  678. {
  679. DbgLog((LOG_ERROR,0,TEXT("Assertion FAILED (%hs) at line %d in file %hs"),
  680. pCondition, iLine, pFileName));
  681. DebugBreak();
  682. }
  683. #endif
  684. /* Print a formatted string to the debugger prefixed with this module's name
  685. Because the COMBASE classes are linked statically every module loaded will
  686. have their own copy of this code. It therefore helps if the module name is
  687. included on the output so that the offending code can be easily found */
  688. //
  689. // warning -- this function is implemented twice for ansi applications
  690. // linking to the unicode library
  691. //
  692. void WINAPI DbgLogInfo(DWORD Type,DWORD Level,LPCTSTR pFormat,...)
  693. {
  694. /* Check the current level for this type combination */
  695. BOOL bAccept = DbgCheckModuleLevel(Type,Level);
  696. if (bAccept == FALSE) {
  697. return;
  698. }
  699. TCHAR szInfo[2000];
  700. /* Format the variable length parameter list */
  701. va_list va;
  702. va_start(va, pFormat);
  703. (void)StringCchPrintf(szInfo, NUMELMS(szInfo),
  704. TEXT("%s(tid %x) %8d : "),
  705. m_ModuleName,
  706. GetCurrentThreadId(), timeGetTime() - dwTimeOffset);
  707. (void)StringCchVPrintf(szInfo + lstrlen(szInfo), NUMELMS(szInfo) - lstrlen(szInfo), pFormat, va);
  708. (void)StringCchCat(szInfo, NUMELMS(szInfo), TEXT("\r\n"));
  709. DbgOutString(szInfo);
  710. va_end(va);
  711. }
  712. /* If we are executing as a pure kernel filter we cannot display message
  713. boxes to the user, this provides an alternative which puts the error
  714. condition on the debugger output with a suitable eye catching message */
  715. void WINAPI DbgKernelAssert(LPCTSTR pCondition,LPCTSTR pFileName,INT iLine)
  716. {
  717. DbgLog((LOG_ERROR,0,TEXT("Assertion FAILED (%s) at line %d in file %s"),
  718. pCondition, iLine, pFileName));
  719. DebugBreak();
  720. }
  721. /* Each time we create an object derived from CBaseObject the constructor will
  722. call us to register the creation of the new object. We are passed a string
  723. description which we store away. We return a cookie that the constructor
  724. uses to identify the object when it is destroyed later on. We update the
  725. total number of active objects in the DLL mainly for debugging purposes */
  726. DWORD WINAPI DbgRegisterObjectCreation(LPCSTR szObjectName,
  727. LPCWSTR wszObjectName)
  728. {
  729. /* If this fires you have a mixed DEBUG/RETAIL build */
  730. ASSERT(!!szObjectName ^ !!wszObjectName);
  731. /* Create a place holder for this object description */
  732. ObjectDesc *pObject = new ObjectDesc;
  733. ASSERT(pObject);
  734. /* It is valid to pass a NULL object name */
  735. if (pObject == NULL) {
  736. return FALSE;
  737. }
  738. /* Check we have been initialised - we may not be initialised when we are
  739. being pulled in from an executable which has globally defined objects
  740. as they are created by the C++ run time before WinMain is called */
  741. if (m_bInit == FALSE) {
  742. DbgInitialise(GetModuleHandle(NULL));
  743. }
  744. /* Grab the list critical section */
  745. EnterCriticalSection(&m_CSDebug);
  746. /* If no name then default to UNKNOWN */
  747. if (!szObjectName && !wszObjectName) {
  748. szObjectName = pUnknownName;
  749. }
  750. /* Put the new description at the head of the list */
  751. pObject->m_szName = szObjectName;
  752. pObject->m_wszName = wszObjectName;
  753. pObject->m_dwCookie = ++m_dwNextCookie;
  754. pObject->m_pNext = pListHead;
  755. pListHead = pObject;
  756. m_dwObjectCount++;
  757. DWORD ObjectCookie = pObject->m_dwCookie;
  758. ASSERT(ObjectCookie);
  759. if(wszObjectName) {
  760. DbgLog((LOG_MEMORY,2,TEXT("Object created %d (%ls) %d Active"),
  761. pObject->m_dwCookie, wszObjectName, m_dwObjectCount));
  762. } else {
  763. DbgLog((LOG_MEMORY,2,TEXT("Object created %d (%hs) %d Active"),
  764. pObject->m_dwCookie, szObjectName, m_dwObjectCount));
  765. }
  766. LeaveCriticalSection(&m_CSDebug);
  767. return ObjectCookie;
  768. }
  769. /* This is called by the CBaseObject destructor when an object is about to be
  770. destroyed, we are passed the cookie we returned during construction that
  771. identifies this object. We scan the object list for a matching cookie and
  772. remove the object if successful. We also update the active object count */
  773. BOOL WINAPI DbgRegisterObjectDestruction(DWORD dwCookie)
  774. {
  775. /* Grab the list critical section */
  776. EnterCriticalSection(&m_CSDebug);
  777. ObjectDesc *pObject = pListHead;
  778. ObjectDesc *pPrevious = NULL;
  779. /* Scan the object list looking for a cookie match */
  780. while (pObject) {
  781. if (pObject->m_dwCookie == dwCookie) {
  782. break;
  783. }
  784. pPrevious = pObject;
  785. pObject = pObject->m_pNext;
  786. }
  787. if (pObject == NULL) {
  788. DbgBreak("Apparently destroying a bogus object");
  789. LeaveCriticalSection(&m_CSDebug);
  790. return FALSE;
  791. }
  792. /* Is the object at the head of the list */
  793. if (pPrevious == NULL) {
  794. pListHead = pObject->m_pNext;
  795. } else {
  796. pPrevious->m_pNext = pObject->m_pNext;
  797. }
  798. /* Delete the object and update the housekeeping information */
  799. m_dwObjectCount--;
  800. if(pObject->m_wszName) {
  801. DbgLog((LOG_MEMORY,2,TEXT("Object destroyed %d (%ls) %d Active"),
  802. pObject->m_dwCookie, pObject->m_wszName, m_dwObjectCount));
  803. } else {
  804. DbgLog((LOG_MEMORY,2,TEXT("Object destroyed %d (%hs) %d Active"),
  805. pObject->m_dwCookie, pObject->m_szName, m_dwObjectCount));
  806. }
  807. delete pObject;
  808. LeaveCriticalSection(&m_CSDebug);
  809. return TRUE;
  810. }
  811. /* This runs through the active object list displaying their details */
  812. void WINAPI DbgDumpObjectRegister()
  813. {
  814. TCHAR szInfo[iDEBUGINFO];
  815. /* Grab the list critical section */
  816. EnterCriticalSection(&m_CSDebug);
  817. ObjectDesc *pObject = pListHead;
  818. /* Scan the object list displaying the name and cookie */
  819. DbgLog((LOG_MEMORY,2,TEXT("")));
  820. DbgLog((LOG_MEMORY,2,TEXT(" ID Object Description")));
  821. DbgLog((LOG_MEMORY,2,TEXT("")));
  822. while (pObject) {
  823. if(pObject->m_wszName) {
  824. (void)StringCchPrintf(szInfo,NUMELMS(szInfo),TEXT("%5d (%p) %30ls"),pObject->m_dwCookie, &pObject, pObject->m_wszName);
  825. } else {
  826. (void)StringCchPrintf(szInfo,NUMELMS(szInfo),TEXT("%5d (%p) %30hs"),pObject->m_dwCookie, &pObject, pObject->m_szName);
  827. }
  828. DbgLog((LOG_MEMORY,2,szInfo));
  829. pObject = pObject->m_pNext;
  830. }
  831. (void)StringCchPrintf(szInfo,NUMELMS(szInfo),TEXT("Total object count %5d"),m_dwObjectCount);
  832. DbgLog((LOG_MEMORY,2,TEXT("")));
  833. DbgLog((LOG_MEMORY,1,szInfo));
  834. LeaveCriticalSection(&m_CSDebug);
  835. }
  836. /* Debug infinite wait stuff */
  837. DWORD WINAPI DbgWaitForSingleObject(HANDLE h)
  838. {
  839. DWORD dwWaitResult;
  840. do {
  841. dwWaitResult = WaitForSingleObject(h, dwWaitTimeout);
  842. ASSERT(dwWaitResult == WAIT_OBJECT_0);
  843. } while (dwWaitResult == WAIT_TIMEOUT);
  844. return dwWaitResult;
  845. }
  846. DWORD WINAPI DbgWaitForMultipleObjects(DWORD nCount,
  847. __in_ecount(nCount) CONST HANDLE *lpHandles,
  848. BOOL bWaitAll)
  849. {
  850. DWORD dwWaitResult;
  851. do {
  852. dwWaitResult = WaitForMultipleObjects(nCount,
  853. lpHandles,
  854. bWaitAll,
  855. dwWaitTimeout);
  856. ASSERT((DWORD)(dwWaitResult - WAIT_OBJECT_0) < MAXIMUM_WAIT_OBJECTS);
  857. } while (dwWaitResult == WAIT_TIMEOUT);
  858. return dwWaitResult;
  859. }
  860. void WINAPI DbgSetWaitTimeout(DWORD dwTimeout)
  861. {
  862. dwWaitTimeout = dwTimeout;
  863. }
  864. #endif /* DEBUG */
  865. #ifdef _OBJBASE_H_
  866. /* Stuff for printing out our GUID names */
  867. GUID_STRING_ENTRY g_GuidNames[] = {
  868. #define OUR_GUID_ENTRY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
  869. { #name, { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } } },
  870. #include <uuids.h>
  871. };
  872. CGuidNameList GuidNames;
  873. int g_cGuidNames = sizeof(g_GuidNames) / sizeof(g_GuidNames[0]);
  874. char *CGuidNameList::operator [] (const GUID &guid)
  875. {
  876. for (int i = 0; i < g_cGuidNames; i++) {
  877. if (g_GuidNames[i].guid == guid) {
  878. return g_GuidNames[i].szName;
  879. }
  880. }
  881. if (guid == GUID_NULL) {
  882. return "GUID_NULL";
  883. }
  884. // !!! add something to print FOURCC guids?
  885. // shouldn't this print the hex CLSID?
  886. return "Unknown GUID Name";
  887. }
  888. #endif /* _OBJBASE_H_ */
  889. /* CDisp class - display our data types */
  890. // clashes with REFERENCE_TIME
  891. CDisp::CDisp(LONGLONG ll, int Format)
  892. {
  893. // note: this could be combined with CDisp(LONGLONG) by
  894. // introducing a default format of CDISP_REFTIME
  895. LARGE_INTEGER li;
  896. li.QuadPart = ll;
  897. switch (Format) {
  898. case CDISP_DEC:
  899. {
  900. TCHAR temp[20];
  901. int pos=20;
  902. temp[--pos] = 0;
  903. int digit;
  904. // always output at least one digit
  905. do {
  906. // Get the rightmost digit - we only need the low word
  907. digit = li.LowPart % 10;
  908. li.QuadPart /= 10;
  909. temp[--pos] = (TCHAR) digit+L'0';
  910. } while (li.QuadPart);
  911. (void)StringCchCopy(m_String, NUMELMS(m_String), temp+pos);
  912. break;
  913. }
  914. case CDISP_HEX:
  915. default:
  916. (void)StringCchPrintf(m_String, NUMELMS(m_String), TEXT("0x%X%8.8X"), li.HighPart, li.LowPart);
  917. }
  918. };
  919. CDisp::CDisp(REFCLSID clsid)
  920. {
  921. #ifdef UNICODE
  922. (void)StringFromGUID2(clsid, m_String, NUMELMS(m_String));
  923. #else
  924. WCHAR wszTemp[50];
  925. (void)StringFromGUID2(clsid, wszTemp, NUMELMS(wszTemp));
  926. (void)StringCchPrintf(m_String, NUMELMS(m_String), TEXT("%S"), wszTemp);
  927. #endif
  928. };
  929. #ifdef __STREAMS__
  930. /* Display stuff */
  931. CDisp::CDisp(CRefTime llTime)
  932. {
  933. LONGLONG llDiv;
  934. if (llTime < 0) {
  935. llTime = -llTime;
  936. (void)StringCchCopy(m_String, NUMELMS(m_String), TEXT("-"));
  937. }
  938. llDiv = (LONGLONG)24 * 3600 * 10000000;
  939. if (llTime >= llDiv) {
  940. (void)StringCchPrintf(m_String + lstrlen(m_String), NUMELMS(m_String) - lstrlen(m_String), TEXT("%d days "), (LONG)(llTime / llDiv));
  941. llTime = llTime % llDiv;
  942. }
  943. llDiv = (LONGLONG)3600 * 10000000;
  944. if (llTime >= llDiv) {
  945. (void)StringCchPrintf(m_String + lstrlen(m_String), NUMELMS(m_String) - lstrlen(m_String), TEXT("%d hrs "), (LONG)(llTime / llDiv));
  946. llTime = llTime % llDiv;
  947. }
  948. llDiv = (LONGLONG)60 * 10000000;
  949. if (llTime >= llDiv) {
  950. (void)StringCchPrintf(m_String + lstrlen(m_String), NUMELMS(m_String) - lstrlen(m_String), TEXT("%d mins "), (LONG)(llTime / llDiv));
  951. llTime = llTime % llDiv;
  952. }
  953. (void)StringCchPrintf(m_String + lstrlen(m_String), NUMELMS(m_String) - lstrlen(m_String), TEXT("%d.%3.3d sec"),
  954. (LONG)llTime / 10000000,
  955. (LONG)((llTime % 10000000) / 10000));
  956. };
  957. #endif // __STREAMS__
  958. /* Display pin */
  959. CDisp::CDisp(IPin *pPin)
  960. {
  961. PIN_INFO pi;
  962. TCHAR str[MAX_PIN_NAME];
  963. CLSID clsid;
  964. if (pPin) {
  965. pPin->QueryPinInfo(&pi);
  966. pi.pFilter->GetClassID(&clsid);
  967. QueryPinInfoReleaseFilter(pi);
  968. #ifndef UNICODE
  969. WideCharToMultiByte(GetACP(), 0, pi.achName, lstrlenW(pi.achName) + 1,
  970. str, MAX_PIN_NAME, NULL, NULL);
  971. #else
  972. (void)StringCchCopy(str, NUMELMS(str), pi.achName);
  973. #endif
  974. } else {
  975. (void)StringCchCopy(str, NUMELMS(str), TEXT("NULL IPin"));
  976. }
  977. m_pString = (PTCHAR) new TCHAR[lstrlen(str)+64];
  978. if (!m_pString) {
  979. return;
  980. }
  981. (void)StringCchPrintf(m_pString, lstrlen(str) + 64, TEXT("%hs(%s)"), GuidNames[clsid], str);
  982. }
  983. /* Display filter or pin */
  984. CDisp::CDisp(IUnknown *pUnk)
  985. {
  986. IBaseFilter *pf;
  987. HRESULT hr = pUnk->QueryInterface(IID_IBaseFilter, (void **)&pf);
  988. if(SUCCEEDED(hr))
  989. {
  990. FILTER_INFO fi;
  991. hr = pf->QueryFilterInfo(&fi);
  992. if(SUCCEEDED(hr))
  993. {
  994. QueryFilterInfoReleaseGraph(fi);
  995. size_t len = lstrlenW(fi.achName) + 1;
  996. m_pString = new TCHAR[len];
  997. if(m_pString)
  998. {
  999. #ifdef UNICODE
  1000. (void)StringCchCopy(m_pString, len, fi.achName);
  1001. #else
  1002. (void)StringCchPrintf(m_pString, len, "%S", fi.achName);
  1003. #endif
  1004. }
  1005. }
  1006. pf->Release();
  1007. return;
  1008. }
  1009. IPin *pp;
  1010. hr = pUnk->QueryInterface(IID_IPin, (void **)&pp);
  1011. if(SUCCEEDED(hr))
  1012. {
  1013. CDisp::CDisp(pp);
  1014. pp->Release();
  1015. return;
  1016. }
  1017. }
  1018. CDisp::~CDisp()
  1019. {
  1020. }
  1021. CDispBasic::~CDispBasic()
  1022. {
  1023. if (m_pString != m_String) {
  1024. delete [] m_pString;
  1025. }
  1026. }
  1027. CDisp::CDisp(double d)
  1028. {
  1029. (void)StringCchPrintf(m_String, NUMELMS(m_String), TEXT("%d.%03d"), (int) d, (int) ((d - (int) d) * 1000));
  1030. }
  1031. /* If built for debug this will display the media type details. We convert the
  1032. major and subtypes into strings and also ask the base classes for a string
  1033. description of the subtype, so MEDIASUBTYPE_RGB565 becomes RGB 565 16 bit
  1034. We also display the fields in the BITMAPINFOHEADER structure, this should
  1035. succeed as we do not accept input types unless the format is big enough */
  1036. #ifdef DEBUG
  1037. void WINAPI DisplayType(LPCTSTR label, const AM_MEDIA_TYPE *pmtIn)
  1038. {
  1039. /* Dump the GUID types and a short description */
  1040. DbgLog((LOG_TRACE,5,TEXT("")));
  1041. DbgLog((LOG_TRACE,2,TEXT("%s M type %hs S type %hs"), label,
  1042. GuidNames[pmtIn->majortype],
  1043. GuidNames[pmtIn->subtype]));
  1044. DbgLog((LOG_TRACE,5,TEXT("Subtype description %s"),GetSubtypeName(&pmtIn->subtype)));
  1045. /* Dump the generic media types */
  1046. if (pmtIn->bTemporalCompression) {
  1047. DbgLog((LOG_TRACE,5,TEXT("Temporally compressed")));
  1048. } else {
  1049. DbgLog((LOG_TRACE,5,TEXT("Not temporally compressed")));
  1050. }
  1051. if (pmtIn->bFixedSizeSamples) {
  1052. DbgLog((LOG_TRACE,5,TEXT("Sample size %d"),pmtIn->lSampleSize));
  1053. } else {
  1054. DbgLog((LOG_TRACE,5,TEXT("Variable size samples")));
  1055. }
  1056. if (pmtIn->formattype == FORMAT_VideoInfo) {
  1057. VIDEOINFOHEADER *pVideoInfo = (VIDEOINFOHEADER *)pmtIn->pbFormat;
  1058. DisplayRECT(TEXT("Source rectangle"),pVideoInfo->rcSource);
  1059. DisplayRECT(TEXT("Target rectangle"),pVideoInfo->rcTarget);
  1060. DisplayBITMAPINFO(HEADER(pmtIn->pbFormat));
  1061. } if (pmtIn->formattype == FORMAT_VideoInfo2) {
  1062. VIDEOINFOHEADER2 *pVideoInfo2 = (VIDEOINFOHEADER2 *)pmtIn->pbFormat;
  1063. DisplayRECT(TEXT("Source rectangle"),pVideoInfo2->rcSource);
  1064. DisplayRECT(TEXT("Target rectangle"),pVideoInfo2->rcTarget);
  1065. DbgLog((LOG_TRACE, 5, TEXT("Aspect Ratio: %d:%d"),
  1066. pVideoInfo2->dwPictAspectRatioX,
  1067. pVideoInfo2->dwPictAspectRatioY));
  1068. DisplayBITMAPINFO(&pVideoInfo2->bmiHeader);
  1069. } else if (pmtIn->majortype == MEDIATYPE_Audio) {
  1070. DbgLog((LOG_TRACE,2,TEXT(" Format type %hs"),
  1071. GuidNames[pmtIn->formattype]));
  1072. DbgLog((LOG_TRACE,2,TEXT(" Subtype %hs"),
  1073. GuidNames[pmtIn->subtype]));
  1074. if ((pmtIn->subtype != MEDIASUBTYPE_MPEG1Packet)
  1075. && (pmtIn->cbFormat >= sizeof(PCMWAVEFORMAT)))
  1076. {
  1077. /* Dump the contents of the WAVEFORMATEX type-specific format structure */
  1078. WAVEFORMATEX *pwfx = (WAVEFORMATEX *) pmtIn->pbFormat;
  1079. DbgLog((LOG_TRACE,2,TEXT("wFormatTag %u"), pwfx->wFormatTag));
  1080. DbgLog((LOG_TRACE,2,TEXT("nChannels %u"), pwfx->nChannels));
  1081. DbgLog((LOG_TRACE,2,TEXT("nSamplesPerSec %lu"), pwfx->nSamplesPerSec));
  1082. DbgLog((LOG_TRACE,2,TEXT("nAvgBytesPerSec %lu"), pwfx->nAvgBytesPerSec));
  1083. DbgLog((LOG_TRACE,2,TEXT("nBlockAlign %u"), pwfx->nBlockAlign));
  1084. DbgLog((LOG_TRACE,2,TEXT("wBitsPerSample %u"), pwfx->wBitsPerSample));
  1085. /* PCM uses a WAVEFORMAT and does not have the extra size field */
  1086. if (pmtIn->cbFormat >= sizeof(WAVEFORMATEX)) {
  1087. DbgLog((LOG_TRACE,2,TEXT("cbSize %u"), pwfx->cbSize));
  1088. }
  1089. } else {
  1090. }
  1091. } else {
  1092. DbgLog((LOG_TRACE,2,TEXT(" Format type %hs"),
  1093. GuidNames[pmtIn->formattype]));
  1094. }
  1095. }
  1096. void DisplayBITMAPINFO(const BITMAPINFOHEADER* pbmi)
  1097. {
  1098. DbgLog((LOG_TRACE,5,TEXT("Size of BITMAPINFO structure %d"),pbmi->biSize));
  1099. if (pbmi->biCompression < 256) {
  1100. DbgLog((LOG_TRACE,2,TEXT("%dx%dx%d bit (%d)"),
  1101. pbmi->biWidth, pbmi->biHeight,
  1102. pbmi->biBitCount, pbmi->biCompression));
  1103. } else {
  1104. DbgLog((LOG_TRACE,2,TEXT("%dx%dx%d bit '%4.4hs'"),
  1105. pbmi->biWidth, pbmi->biHeight,
  1106. pbmi->biBitCount, &pbmi->biCompression));
  1107. }
  1108. DbgLog((LOG_TRACE,2,TEXT("Image size %d"),pbmi->biSizeImage));
  1109. DbgLog((LOG_TRACE,5,TEXT("Planes %d"),pbmi->biPlanes));
  1110. DbgLog((LOG_TRACE,5,TEXT("X Pels per metre %d"),pbmi->biXPelsPerMeter));
  1111. DbgLog((LOG_TRACE,5,TEXT("Y Pels per metre %d"),pbmi->biYPelsPerMeter));
  1112. DbgLog((LOG_TRACE,5,TEXT("Colours used %d"),pbmi->biClrUsed));
  1113. }
  1114. void DisplayRECT(LPCTSTR szLabel, const RECT& rc)
  1115. {
  1116. DbgLog((LOG_TRACE,5,TEXT("%s (Left %d Top %d Right %d Bottom %d)"),
  1117. szLabel,
  1118. rc.left,
  1119. rc.top,
  1120. rc.right,
  1121. rc.bottom));
  1122. }
  1123. void WINAPI DumpGraph(IFilterGraph *pGraph, DWORD dwLevel)
  1124. {
  1125. if( !pGraph )
  1126. {
  1127. return;
  1128. }
  1129. IEnumFilters *pFilters;
  1130. DbgLog((LOG_TRACE,dwLevel,TEXT("DumpGraph [%x]"), pGraph));
  1131. if (FAILED(pGraph->EnumFilters(&pFilters))) {
  1132. DbgLog((LOG_TRACE,dwLevel,TEXT("EnumFilters failed!")));
  1133. }
  1134. IBaseFilter *pFilter;
  1135. ULONG n;
  1136. while (pFilters->Next(1, &pFilter, &n) == S_OK) {
  1137. FILTER_INFO info;
  1138. if (FAILED(pFilter->QueryFilterInfo(&info))) {
  1139. DbgLog((LOG_TRACE,dwLevel,TEXT(" Filter [%p] -- failed QueryFilterInfo"), pFilter));
  1140. } else {
  1141. QueryFilterInfoReleaseGraph(info);
  1142. // !!! should QueryVendorInfo here!
  1143. DbgLog((LOG_TRACE,dwLevel,TEXT(" Filter [%p] '%ls'"), pFilter, info.achName));
  1144. IEnumPins *pins;
  1145. if (FAILED(pFilter->EnumPins(&pins))) {
  1146. DbgLog((LOG_TRACE,dwLevel,TEXT("EnumPins failed!")));
  1147. } else {
  1148. IPin *pPin;
  1149. while (pins->Next(1, &pPin, &n) == S_OK) {
  1150. PIN_INFO pinInfo;
  1151. if (FAILED(pPin->QueryPinInfo(&pinInfo))) {
  1152. DbgLog((LOG_TRACE,dwLevel,TEXT(" Pin [%x] -- failed QueryPinInfo"), pPin));
  1153. } else {
  1154. QueryPinInfoReleaseFilter(pinInfo);
  1155. IPin *pPinConnected = NULL;
  1156. HRESULT hr = pPin->ConnectedTo(&pPinConnected);
  1157. if (pPinConnected) {
  1158. DbgLog((LOG_TRACE,dwLevel,TEXT(" Pin [%p] '%ls' [%sput]")
  1159. TEXT(" Connected to pin [%p]"),
  1160. pPin, pinInfo.achName,
  1161. pinInfo.dir == PINDIR_INPUT ? TEXT("In") : TEXT("Out"),
  1162. pPinConnected));
  1163. pPinConnected->Release();
  1164. // perhaps we should really dump the type both ways as a sanity
  1165. // check?
  1166. if (pinInfo.dir == PINDIR_OUTPUT) {
  1167. AM_MEDIA_TYPE mt;
  1168. hr = pPin->ConnectionMediaType(&mt);
  1169. if (SUCCEEDED(hr)) {
  1170. DisplayType(TEXT("Connection type"), &mt);
  1171. FreeMediaType(mt);
  1172. }
  1173. }
  1174. } else {
  1175. DbgLog((LOG_TRACE,dwLevel,
  1176. TEXT(" Pin [%x] '%ls' [%sput]"),
  1177. pPin, pinInfo.achName,
  1178. pinInfo.dir == PINDIR_INPUT ? TEXT("In") : TEXT("Out")));
  1179. }
  1180. }
  1181. pPin->Release();
  1182. }
  1183. pins->Release();
  1184. }
  1185. }
  1186. pFilter->Release();
  1187. }
  1188. pFilters->Release();
  1189. }
  1190. #endif
  1191. #endif /* PJMEDIA_VIDEO_DEV_HAS_DSHOW */