source: trunk/Utilities/DumpMediaSamples/DumpMediaSamples.cpp @ 152

Last change on this file since 152 was 152, checked in by roman, 11 years ago
  • Property svn:keywords set to Id
File size: 20.2 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2006-2012
3// Created by Roman Ryltsov roman@alax.info
4//
5// A permission to use the source code is granted as long as reference to
6// source website http://alax.info is retained.
7
8#include "stdafx.h"
9//#include <qedit.h>
10#include "rodshow.h"
11#include "Handler.h"
12
13#pragma region Re-Adding Removed from Windows SDK qedit.h
14
15struct __declspec(uuid("0579154a-2b53-4994-b0d0-e773148eff85"))
16ISampleGrabberCB : IUnknown
17{
18    //
19    // Raw methods provided by interface
20    //
21
22      virtual HRESULT __stdcall SampleCB (
23        double SampleTime,
24        struct IMediaSample * pSample ) = 0;
25      virtual HRESULT __stdcall BufferCB (
26        double SampleTime,
27        unsigned char * pBuffer,
28        long BufferLen ) = 0;
29};
30
31struct __declspec(uuid("6b652fff-11fe-4fce-92ad-0266b5d7c78f"))
32ISampleGrabber : IUnknown
33{
34    //
35    // Raw methods provided by interface
36    //
37
38      virtual HRESULT __stdcall SetOneShot (
39        long OneShot ) = 0;
40      virtual HRESULT __stdcall SetMediaType (
41        struct _AMMediaType * pType ) = 0;
42      virtual HRESULT __stdcall GetConnectedMediaType (
43        struct _AMMediaType * pType ) = 0;
44      virtual HRESULT __stdcall SetBufferSamples (
45        long BufferThem ) = 0;
46      virtual HRESULT __stdcall GetCurrentBuffer (
47        /*[in,out]*/ long * pBufferSize,
48        /*[out]*/ long * pBuffer ) = 0;
49      virtual HRESULT __stdcall GetCurrentSample (
50        /*[out,retval]*/ struct IMediaSample * * ppSample ) = 0;
51      virtual HRESULT __stdcall SetCallback (
52        struct ISampleGrabberCB * pCallback,
53        long WhichMethodToCallback ) = 0;
54};
55
56struct __declspec(uuid("c1f400a0-3f08-11d3-9f0b-006008039e37"))
57SampleGrabber;
58    // [ default ] interface ISampleGrabber
59
60#pragma endregion
61
62////////////////////////////////////////////////////////////
63// CFormatFlagHelper
64
65class CFormatFlagHelper
66{
67public:
68
69        ////////////////////////////////////////////////////
70        // FLAGNAME
71
72        typedef struct _FLAGNAME
73        {
74                DWORD nValue;
75                LPCTSTR pszName;
76        } FLAGNAME;
77
78public:
79// CFormatFlagHelper
80        static CString StringFromFlags(DWORD nValue, const FLAGNAME* pFlagNames, SIZE_T nFlagNameCount)
81        {
82                CString sText;
83                DWORD nKnownValues = 0;
84                for(SIZE_T nIndex = 0; nIndex < nFlagNameCount; nIndex++)
85                {
86                        nKnownValues |= pFlagNames[nIndex].nValue;
87                        if(!(nValue & pFlagNames[nIndex].nValue))
88                                continue;
89                        if(!sText.IsEmpty())
90                                sText.Append(_T(" | "));
91                        sText.Append(pFlagNames[nIndex].pszName);
92                }
93                DWORD nUnnamedValue = nValue & ~nKnownValues;
94                if(nUnnamedValue)
95                {
96                        if(!sText.IsEmpty())
97                                sText.Append(_T(" | "));
98                        sText.AppendFormat(_T("0x%08x"), nUnnamedValue);
99                }
100                return sText;
101        }
102        template <SIZE_T t_nCount>
103        static CString StringFromFlags(DWORD nValue, const FLAGNAME (&pFlagNames)[t_nCount])
104        {
105                return StringFromFlags(nValue, pFlagNames, t_nCount);
106        }
107        static CString StringFromTypeSpecificFlags(DWORD nValue)
108        {
109                static const FLAGNAME g_pNames[] =
110                {
111                        //{ AM_VIDEO_FLAG_FIELD_MASK
112                        //{ AM_VIDEO_FLAG_INTERLEAVED_FRAME
113                        { AM_VIDEO_FLAG_FIELD1, _T("AM_VIDEO_FLAG_FIELD1") },
114                        { AM_VIDEO_FLAG_FIELD2, _T("AM_VIDEO_FLAG_FIELD2") },
115                        { AM_VIDEO_FLAG_FIELD1FIRST, _T("AM_VIDEO_FLAG_FIELD1FIRST") },
116                        { AM_VIDEO_FLAG_WEAVE, _T("AM_VIDEO_FLAG_WEAVE") },
117                        //{ AM_VIDEO_FLAG_IPB_MASK
118                        //{ AM_VIDEO_FLAG_I_SAMPLE
119                        { AM_VIDEO_FLAG_P_SAMPLE, _T("AM_VIDEO_FLAG_P_SAMPLE") },
120                        { AM_VIDEO_FLAG_B_SAMPLE, _T("AM_VIDEO_FLAG_B_SAMPLE") },
121                        { AM_VIDEO_FLAG_REPEAT_FIELD, _T("AM_VIDEO_FLAG_REPEAT_FIELD") },
122                };
123                return StringFromFlags(nValue, g_pNames);
124        }
125        static CString StringFromSampleFlags(DWORD nValue)
126        {
127                static const FLAGNAME g_pNames[] =
128                {
129                        { AM_SAMPLE_SPLICEPOINT, _T("AM_SAMPLE_SPLICEPOINT") },
130                        { AM_SAMPLE_PREROLL, _T("AM_SAMPLE_PREROLL") },
131                        { AM_SAMPLE_DATADISCONTINUITY, _T("AM_SAMPLE_DATADISCONTINUITY") },
132                        { AM_SAMPLE_TYPECHANGED, _T("AM_SAMPLE_TYPECHANGED") },
133                        { AM_SAMPLE_TIMEVALID, _T("AM_SAMPLE_TIMEVALID") },
134                        { AM_SAMPLE_TIMEDISCONTINUITY, _T("AM_SAMPLE_TIMEDISCONTINUITY") },
135                        { AM_SAMPLE_FLUSH_ON_PAUSE, _T("AM_SAMPLE_FLUSH_ON_PAUSE") },
136                        { AM_SAMPLE_STOPVALID, _T("AM_SAMPLE_STOPVALID") },
137                        { AM_SAMPLE_ENDOFSTREAM, _T("AM_SAMPLE_ENDOFSTREAM") },
138                        //{ AM_STREAM_MEDIA
139                        //{ AM_STREAM_CONTROL
140                };
141                return StringFromFlags(nValue, g_pNames);
142        }
143};
144
145////////////////////////////////////////////////////////////
146// CModule
147
148class CModule :
149        public CAtlExeModuleT<CModule>
150{
151public:
152
153        ////////////////////////////////////////////////////////
154        // CSampleGrabberCallback
155
156        class ATL_NO_VTABLE CSampleGrabberCallback :
157                public CComObjectRootEx<CComMultiThreadModel>,
158                public ISampleGrabberCB,
159                public CFormatFlagHelper
160        {
161        public:
162
163        BEGIN_COM_MAP(CSampleGrabberCallback)
164                COM_INTERFACE_ENTRY(ISampleGrabberCB)
165        END_COM_MAP()
166
167        private:
168                CModule* m_pModule;
169                CString m_sName;
170                CString m_sNamePrefix;
171                mutable CRoCriticalSection m_DataCriticalSection;
172                CMediaType m_pMediaType;
173                CComPtr<CAbstractHandler> m_pHandler;
174
175        public:
176        // CSampleGrabberCallback
177                CSampleGrabberCallback() :
178                        m_pModule(NULL)
179                {
180                        _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
181                }
182                ~CSampleGrabberCallback()
183                {
184                        _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
185                }
186                VOID Initialize(CModule* pModule)
187                {
188                        _A(!m_pModule && pModule);
189                        m_pModule = pModule;
190                }
191                VOID SetName(const CString& sName)
192                {
193                        _A(m_sName.IsEmpty());
194                        m_sName = sName;
195                        m_sNamePrefix = AtlFormatString(_T("[%s] "), sName);
196                }
197                VOID SetMediaType(const AM_MEDIA_TYPE* pMediaType)
198                {
199                        CRoCriticalSectionLock DataLock(m_DataCriticalSection);
200                        PrintMediaType(pMediaType);
201                        m_pMediaType = pMediaType;
202                }
203                VOID PrintMediaType(const CMediaType& pMediaType)
204                {
205                        _tprintf(_T("%s") _T("Media Type:\n\n"), m_sNamePrefix);
206                        _tprintf(_T("majortype %ls, subtype %ls, pUnk 0x%08x\n"), _PersistHelper::StringFromIdentifier(pMediaType->majortype), _PersistHelper::StringFromIdentifier(pMediaType->subtype), (LONG) (LONG_PTR) pMediaType->pUnk);
207                        _tprintf(_T("bFixedSizeSamples %d, bTemporalCompression %d, lSampleSize %d\n"), pMediaType->bFixedSizeSamples, pMediaType->bTemporalCompression, pMediaType->lSampleSize);
208                        _tprintf(_T("formattype %ls, cbFormat %d, pbFormat 0x%08x\n"), _PersistHelper::StringFromIdentifier(pMediaType->formattype), pMediaType->cbFormat, (UINT) (UINT_PTR) pMediaType->pbFormat);
209                        if(pMediaType->formattype == FORMAT_VideoInfo)
210                        {
211                                const VIDEOINFOHEADER* pVideoInfoHeader = (const VIDEOINFOHEADER*) (pMediaType->pbFormat ? pMediaType->pbFormat : (BYTE*) (pMediaType + 1));
212                                _tprintf(_T("pbFormat as VIDEOINFOHEADER:\n"));
213                                _tprintf(_T("  rcSource { %d, %d, %d, %d ), rcTarget { %d, %d, %d, %d }\n"), pVideoInfoHeader->rcSource.left, pVideoInfoHeader->rcSource.top, pVideoInfoHeader->rcSource.right, pVideoInfoHeader->rcSource.bottom, pVideoInfoHeader->rcTarget.left, pVideoInfoHeader->rcTarget.top, pVideoInfoHeader->rcTarget.right, pVideoInfoHeader->rcTarget.bottom);
214                                _tprintf(_T("  dwBitRate %d, dwBitErrorRate %d, AvgTimePerFrame %s\n"), pVideoInfoHeader->dwBitRate, pVideoInfoHeader->dwBitErrorRate, _FilterGraphHelper::FormatReferenceTime(pVideoInfoHeader->AvgTimePerFrame));
215                                _tprintf(_T("  bmiHeader.biSize %d, bmiHeader.biWidth %d, bmiHeader.biHeight %d, bmiHeader.biPlanes %d, bmiHeader.biBitCount %d, bmiHeader.biCompression %s\n"), pVideoInfoHeader->bmiHeader.biSize, pVideoInfoHeader->bmiHeader.biWidth, pVideoInfoHeader->bmiHeader.biHeight, pVideoInfoHeader->bmiHeader.biPlanes, pVideoInfoHeader->bmiHeader.biBitCount, _FilterGraphHelper::GetFourccCodeString(pVideoInfoHeader->bmiHeader.biCompression));
216                                _tprintf(_T("  bmiHeader.biSizeImage %d, bmiHeader.biXPelsPerMeter %d, bmiHeader.biYPelsPerMeter %d, bmiHeader.biClrUsed %d, bmiHeader.biClrImportant %d\n"), pVideoInfoHeader->bmiHeader.biSizeImage, pVideoInfoHeader->bmiHeader.biXPelsPerMeter, pVideoInfoHeader->bmiHeader.biYPelsPerMeter, pVideoInfoHeader->bmiHeader.biClrUsed, pVideoInfoHeader->bmiHeader.biClrImportant);
217                        } else
218                        if(pMediaType->formattype == FORMAT_VideoInfo2)
219                        {
220                                const VIDEOINFOHEADER2* pVideoInfoHeader2 = (const VIDEOINFOHEADER2*) (pMediaType->pbFormat ? pMediaType->pbFormat : (BYTE*) (pMediaType + 1));
221                                _tprintf(_T("pbFormat as VIDEOINFOHEADER2:\n"));
222                                _tprintf(_T("  rcSource { %d, %d, %d, %d ), rcTarget { %d, %d, %d, %d }\n"), pVideoInfoHeader2->rcSource.left, pVideoInfoHeader2->rcSource.top, pVideoInfoHeader2->rcSource.right, pVideoInfoHeader2->rcSource.bottom, pVideoInfoHeader2->rcTarget.left, pVideoInfoHeader2->rcTarget.top, pVideoInfoHeader2->rcTarget.right, pVideoInfoHeader2->rcTarget.bottom);
223                                _tprintf(_T("  dwBitRate %d, dwBitErrorRate %d, AvgTimePerFrame %s\n"), pVideoInfoHeader2->dwBitRate, pVideoInfoHeader2->dwBitErrorRate, _FilterGraphHelper::FormatReferenceTime(pVideoInfoHeader2->AvgTimePerFrame));
224                                _tprintf(_T("  dwInterlaceFlags 0x%x, dwCopyProtectFlags 0x%x, dwPictAspectRatioX %d, dwPictAspectRatioY %d, dwControlFlags 0x%x\n"), pVideoInfoHeader2->dwInterlaceFlags, pVideoInfoHeader2->dwCopyProtectFlags, pVideoInfoHeader2->dwPictAspectRatioX, pVideoInfoHeader2->dwPictAspectRatioY, pVideoInfoHeader2->dwControlFlags);
225                                _tprintf(_T("  bmiHeader.biSize %d, bmiHeader.biWidth %d, bmiHeader.biHeight %d, bmiHeader.biPlanes %d, bmiHeader.biBitCount %d, bmiHeader.biCompression %s\n"), pVideoInfoHeader2->bmiHeader.biSize, pVideoInfoHeader2->bmiHeader.biWidth, pVideoInfoHeader2->bmiHeader.biHeight, pVideoInfoHeader2->bmiHeader.biPlanes, pVideoInfoHeader2->bmiHeader.biBitCount, _FilterGraphHelper::GetFourccCodeString(pVideoInfoHeader2->bmiHeader.biCompression));
226                                _tprintf(_T("  bmiHeader.biSizeImage %d, bmiHeader.biXPelsPerMeter %d, bmiHeader.biYPelsPerMeter %d, bmiHeader.biClrUsed %d, bmiHeader.biClrImportant %d\n"), pVideoInfoHeader2->bmiHeader.biSizeImage, pVideoInfoHeader2->bmiHeader.biXPelsPerMeter, pVideoInfoHeader2->bmiHeader.biYPelsPerMeter, pVideoInfoHeader2->bmiHeader.biClrUsed, pVideoInfoHeader2->bmiHeader.biClrImportant);
227                        } else
228                        if(pMediaType->formattype == FORMAT_WaveFormatEx)
229                        {
230                                const WAVEFORMATEX* pWaveFormatEx = (const WAVEFORMATEX*) (pMediaType->pbFormat ? pMediaType->pbFormat : (BYTE*) (pMediaType + 1));
231                                _tprintf(_T("pbFormat as WAVEFORMATEX:\n"));
232                                _tprintf(_T("  wFormatTag %d\n"), pWaveFormatEx->wFormatTag);
233                                _tprintf(_T("  nChannels %d\n"), pWaveFormatEx->nChannels);
234                                _tprintf(_T("  nSamplesPerSec %d\n"), pWaveFormatEx->nSamplesPerSec);
235                                _tprintf(_T("  nAvgBytesPerSec %d\n"), pWaveFormatEx->nAvgBytesPerSec);
236                                _tprintf(_T("  nBlockAlign %d\n"), pWaveFormatEx->nBlockAlign);
237                                _tprintf(_T("  wBitsPerSample %d\n"), pWaveFormatEx->wBitsPerSample);
238                                _tprintf(_T("  cbSize %d\n"), pWaveFormatEx->cbSize);
239                                if(pWaveFormatEx->cbSize > 0)
240                                {
241                                        const BYTE* pnExtraData = (const BYTE*) (pWaveFormatEx + 1);
242                                        const SIZE_T nExtraDataSize = pWaveFormatEx->cbSize;
243                                        for(SIZE_T nIndex1 = 0; nIndex1 < nExtraDataSize; nIndex1 += 0x10)
244                                        {
245                                                CString sText;
246                                                for(SIZE_T nIndex2 = nIndex1; nIndex2 < min(nIndex1 + 0x10, nExtraDataSize); nIndex2++)
247                                                        sText.AppendFormat(_T("%02X "), pnExtraData[nIndex2]);
248                                                sText.TrimRight(_T(" "));
249                                                _tprintf(_T("  pnExtraData[0x%04x] %s\n"), nIndex1, sText);
250                                        }
251                                }
252                        } else
253                                ;
254                        _tprintf(_T("\n"));
255                }
256                VOID SetHandler(CAbstractHandler* pHandler)
257                {
258                        CRoCriticalSectionLock DataLock(m_DataCriticalSection);
259                        m_pHandler = pHandler;
260                }
261
262        // ISampleGrabberCB
263        STDMETHOD(SampleCB)(DOUBLE fSampleTime, IMediaSample* pMediaSample)
264                {
265                        _A(pMediaSample);
266                        _ATLTRY
267                        {
268                                CMediaSampleProperties Properties(pMediaSample);
269                                _A(!Properties.pMediaType);
270                                CRoCriticalSectionLock PrintLock(m_pModule->m_PrintCriticalSection);
271                                _tprintf(_T("%s") _T("fSampleTime %s, .dwTypeSpecificFlags 0x%08x%s, .dwSampleFlags 0x%08x%s, .tStart %s, .tStop %s, .dwStreamId %d\n"), 
272                                        m_sNamePrefix,
273                                        _StringHelper::FormatNumber(fSampleTime, 3),
274                                        Properties.dwTypeSpecificFlags, Properties.dwTypeSpecificFlags ? (LPCTSTR) AtlFormatString(_T(" (%s)"), StringFromTypeSpecificFlags(Properties.dwTypeSpecificFlags)) : _T(""),
275                                        Properties.dwSampleFlags, Properties.dwSampleFlags ? (LPCTSTR) AtlFormatString(_T(" (%s)"), StringFromSampleFlags(Properties.dwSampleFlags)) : _T(""),
276                                        _FilterGraphHelper::FormatReferenceTime(Properties.tStart), 
277                                        _FilterGraphHelper::FormatReferenceTime(Properties.tStop),
278                                        Properties.dwStreamId,
279                                        0);
280                                CRoCriticalSectionLock DataLock(m_DataCriticalSection);
281                                BOOL bBufferHandled = FALSE;
282                                if(!bBufferHandled)
283                                {
284                                        CString sBuffer;
285                                        static const SIZE_T g_nMaximalPrintSize = 48;
286                                        SIZE_T nIndex;
287                                        for(nIndex = 0; nIndex < (SIZE_T) Properties.lActual && nIndex < g_nMaximalPrintSize; nIndex++)
288                                                sBuffer.AppendFormat(_T("%02X "), Properties.pbBuffer[nIndex]);
289                                        if(nIndex > g_nMaximalPrintSize)
290                                                sBuffer.Append(_T("..."));
291                                        _tprintf(_T("%s") _T(".cbBuffer %d, .lActual %d, pbBuffer %s\n"), 
292                                                m_sNamePrefix,
293                                                Properties.cbBuffer,
294                                                Properties.lActual,
295                                                sBuffer,
296                                                0);
297                                }
298                                if(m_pHandler)
299                                        m_pHandler->HandleSample(Properties);
300                                _tprintf(_T("\n"));
301                        }
302                        _ATLCATCH(Exception)
303                        {
304                                _C(Exception);
305                        }
306                        return S_OK;
307                }
308        STDMETHOD(BufferCB)(DOUBLE fSampleTime, BYTE* pnBuffer, LONG nBufferSize)
309                {
310                        return S_OK;
311                }
312        };
313
314private:
315
316public:
317        CPath m_sPath;
318        BOOL m_bNoReferenceClock;
319        mutable CRoCriticalSection m_PrintCriticalSection;
320
321public:
322// CModule
323        static VOID LoadGraphBuilderFromFile(IGraphBuilder* pGraphBuilder, LPCTSTR pszPath)
324        {
325                _A(pGraphBuilder && pszPath);
326                CComQIPtr<IPersistStream> pPersistStream = pGraphBuilder;
327                __D(pPersistStream, E_NOINTERFACE);
328                CStringW sPathW(pszPath);
329                __C(StgIsStorageFile(sPathW));
330                CComPtr<IStorage> pStorage;
331                __C(StgOpenStorage(sPathW, 0, STGM_TRANSACTED | STGM_READ | STGM_SHARE_DENY_WRITE, 0, 0, &pStorage));
332                CComPtr<IStream> pStream;
333                __C(pStorage->OpenStream(L"ActiveMovieGraph", 0, STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pStream));
334                __C(pPersistStream->Load(pStream));
335        }
336        static CPath GetDefaultPath()
337        {
338                TCHAR pszPath[MAX_PATH] = { 0 };
339                _W(GetModuleFileName(_AtlBaseModule.GetModuleInstance(), pszPath, DIM(pszPath)));
340                _W(RemoveFileSpec(pszPath));
341                _W(RemoveFileSpec(pszPath));
342                _W(Combine(pszPath, pszPath, _T("Debug.grf")));
343                return pszPath;
344        }
345        CModule()
346        {
347                //m_sPath = GetDefaultPath();
348                m_bNoReferenceClock = FALSE;
349        }
350        ~CModule()
351        {
352        }
353        HRESULT PreMessageLoop(INT nShowCommand)
354        {
355                __C(__super::PreMessageLoop(nShowCommand));
356                return S_OK;
357        }
358        VOID RunMessageLoop()
359        {
360                CGenericFilterGraph FilterGraph;
361                FilterGraph.CoCreateInstance();
362                LoadGraphBuilderFromFile(FilterGraph.m_pFilterGraph, m_sPath);
363                #pragma region Sample Grabbers
364                _FilterGraphHelper::CFilterArray FilterArray;
365                _FilterGraphHelper::GetGraphFilters(FilterGraph.m_pFilterGraph, FilterArray);
366                __D(!FilterArray.IsEmpty(), E_UNNAMED);
367                SIZE_T nSampleGrabberIndex = 0;
368                for(SIZE_T nIndex = 0; nIndex < FilterArray.GetCount(); nIndex++)
369                {
370                        const CComQIPtr<ISampleGrabber> pSampleGrabber = FilterArray[nIndex];
371                        if(!pSampleGrabber)
372                                continue;
373                        CObjectPtr<CSampleGrabberCallback> pSampleGrabberCallback;
374                        pSampleGrabberCallback.Construct();
375                        pSampleGrabberCallback->Initialize(this);
376                        if(FilterArray.GetCount() > 1)
377                                pSampleGrabberCallback->SetName(AtlFormatString(_T("%c"), 'A' + nSampleGrabberIndex));
378                        __C(pSampleGrabber->SetCallback(pSampleGrabberCallback, 0));
379                        nSampleGrabberIndex++;
380                        const CMediaType pMediaType = _FilterGraphHelper::GetPinMediaType(_FilterGraphHelper::GetFilterPin(CComQIPtr<IBaseFilter>(pSampleGrabber), PINDIR_INPUT));
381                        pSampleGrabberCallback->SetMediaType(pMediaType);
382                        //typedef CHdycInterlacingHandler CHandler;
383                        //CObjectPtr<CHandler> pHandler;
384                        //pHandler.Construct()->Initialize(pMediaType);
385                        //pSampleGrabberCallback->SetHandler(pHandler);
386                }
387                #pragma endregion
388                if(m_bNoReferenceClock)
389                        __C(FilterGraph.m_pMediaFilter->SetSyncSource(NULL));
390                _tprintf(_T("Media Samples:\n\n"));
391                __C(FilterGraph.m_pMediaControl->Run());
392                #pragma region Wait for Completion
393                #if TRUE
394                        const CComPtr<IMediaEventEx>& pMediaEvent = FilterGraph.m_pMediaEventEx;
395                        for(; ; )
396                        {
397                                // SUGG: Replace Sleep/GetMessage with MsgWaitForMultipleObjects/PeekMessage to wait for graph event and window messages
398                                static const ULONG g_nTimeout = 10;
399                                if(pMediaEvent)
400                                {
401                                        LONG nCompletionEventCode = 0;
402                                        const HRESULT nWaitForCompletionResult = pMediaEvent->WaitForCompletion(g_nTimeout, &nCompletionEventCode);
403                                        for(; ; )
404                                        {
405                                                LONG nEventCode = 0;
406                                                LONG_PTR nParameter1 = 0, nParameter2 = 0;
407                                                const HRESULT nGetEventResult = pMediaEvent->GetEvent(&nEventCode, &nParameter1, &nParameter2, 0);
408                                                if(nGetEventResult == E_ABORT)
409                                                        break;
410                                                __C(nGetEventResult);
411                                                _ATLTRY
412                                                {
413                                                        switch(nEventCode)
414                                                        {
415                                                        case EC_COMPLETE:
416                                                                _tprintf(_T("Event: EC_COMPLETE (0x%x), Error Code 0x%08x (%s), Parameter2 0x%08x\n"), nEventCode, (HRESULT) nParameter1, AtlFormatSystemMessage((HRESULT) nParameter1).TrimRight(_T("\t\n\r .")), nParameter2);
417                                                                break;
418                                                        case EC_USERABORT:
419                                                                _tprintf(_T("Event: EC_USERABORT (0x%x), Parameter1 0x%08x, Parameter2 0x%08x\n"), nEventCode, nParameter1, nParameter2);
420                                                                break;
421                                                        case EC_ERRORABORT:
422                                                                _tprintf(_T("Event: EC_ERRORABORT (0x%x), Error Code 0x%08x (%s), Parameter2 0x%08x\n"), nEventCode, nParameter1, AtlFormatSystemMessage((HRESULT) nParameter1).TrimRight(_T("\t\n\r .")), nParameter2);
423                                                                break;
424                                                        default:
425                                                                _tprintf(_T("Event: Code 0x%x, Parameter1 0x%08x, Parameter2 0x%08x\n"), nEventCode, nParameter1, nParameter2);
426                                                        }
427                                                }
428                                                _ATLCATCHALL()
429                                                {
430                                                        _V(pMediaEvent->FreeEventParams(nEventCode, nParameter1, nParameter2));
431                                                        _ATLRETHROW;
432                                                }
433                                                _V(pMediaEvent->FreeEventParams(nEventCode, nParameter1, nParameter2));
434                                        }
435                                        if(nWaitForCompletionResult != E_ABORT)
436                                        {
437                                                __C(nWaitForCompletionResult);
438                                                _tprintf(_T("\nCompleted: Event Code 0x%x\n"), nCompletionEventCode);
439                                                break;
440                                        }
441                                } else
442                                        Sleep(g_nTimeout);
443                                MSG Message;
444                                while(PeekMessage(&Message, NULL, WM_NULL, WM_NULL, PM_REMOVE))
445                                {
446                                        TranslateMessage(&Message);
447                                        DispatchMessage(&Message);
448                                }
449                        }
450                #else
451                        MSG Message;
452                        while(GetMessage(&Message, NULL, WM_NULL, WM_NULL) > 0)
453                        {
454                                TranslateMessage(&Message);
455                                DispatchMessage(&Message);
456                        }
457                #endif
458                #pragma endregion
459        }
460};
461
462////////////////////////////////////////////////////////////
463// Main
464
465int _tmain(int argc, _TCHAR* argv[])
466{
467        INT nResult = 0;
468        _ATLTRY
469        {
470                CModule Module;
471                #pragma region Parse Command Line
472                for(INT nIndex = 1; nIndex < argc; nIndex++)
473                {
474                        CString sArgument = argv[nIndex];
475                        _A(!sArgument.IsEmpty());
476                        #pragma region Switches
477                        if(_tcschr(_T("-/"), sArgument[0]))
478                        {
479                                sArgument.Delete(0);
480                                #pragma region Switch Value/Specification
481                                CString sArgumentValue;
482                                if(sArgument.GetLength() > 1)
483                                {
484                                        SIZE_T nIndex = 1;
485                                        if(sArgument[1] == _T(':'))
486                                                nIndex++;
487                                        sArgumentValue = (LPCTSTR) sArgument + nIndex;
488                                }
489                                INT nIntegerArgumentValue = 0;
490                                const BOOL bIntegerArgumentValueValid = !sArgumentValue.IsEmpty() ? AtlStringToInteger(sArgumentValue, nIntegerArgumentValue) : FALSE;
491                                #pragma endregion
492                                if(_tcschr(_T("Cc"), sArgument[0])) // No Reference Clock
493                                {
494                                        Module.m_bNoReferenceClock = TRUE;
495                                }
496                                continue;
497                        }
498                        #pragma endregion
499                        if(sArgument.GetLength() >= 2 && sArgument[0] == _T('"') && sArgument[sArgument.GetLength() - 1] == _T('"'))
500                                sArgument = sArgument.Mid(1, sArgument.GetLength() - 2);
501                        __D(!_tcslen(Module.m_sPath), E_UNNAMED);
502                        Module.m_sPath = (LPCTSTR) sArgument;
503                }
504                #pragma endregion
505                __D(_tcslen(Module.m_sPath), E_UNNAMED);
506                nResult = Module.WinMain(SW_SHOWNORMAL);
507        }
508        _ATLCATCH(Exception)
509        {
510                _tprintf(_T("Fatal: Error 0x%08x\n"), (HRESULT) Exception);
511        }
512        _ATLCATCHALL()
513        {
514                _tprintf(_T("Fatal: Fatal error\n"));
515        }
516        return nResult;
517}
518
519
Note: See TracBrowser for help on using the repository browser.