home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / dump.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-08  |  9.5 KB  |  379 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (c) 1992 - 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11.  
  12. /*
  13.  * Dump Filter - A renderer that dumps its output into a file
  14.  */
  15.  
  16. #include <windows.h>
  17. #include <commdlg.h>
  18. #include <streams.h>
  19.  
  20. #include <initguid.h>
  21.  
  22. #include "dumpuids.h"
  23. #include "dump.h"
  24.  
  25.  
  26. // setup data
  27.  
  28. AMOVIESETUP_MEDIATYPE sudPinTypes =     { &MEDIATYPE_NULL                // clsMajorType
  29.                                            , &MEDIASUBTYPE_NULL }  ;       // clsMinorType
  30.  
  31. AMOVIESETUP_PIN sudPins   =    { L"Input"            // strName
  32.                                , FALSE               // bRendered
  33.                                , FALSE               // bOutput
  34.                                , FALSE               // bZero
  35.                                , FALSE               // bMany
  36.                                , &CLSID_NULL         // clsConnectsToFilter
  37.                                , L"Output"           // strConnectsToPin
  38.                                , 1                   // nTypes
  39.                                , &sudPinTypes } ;    // lpTypes
  40.  
  41.  
  42. AMOVIESETUP_FILTER sudDump     = { &CLSID_Dump                      // clsID
  43.                                   , L"Dump filter"                  // strName
  44.                                   , MERIT_DO_NOT_USE                // dwMerit
  45.                                   , 1                               // nPins
  46.                                   , &sudPins };                     // lpPin
  47.  
  48. //
  49. //  Object creation stuff
  50. //
  51.  
  52. CFactoryTemplate g_Templates[]= {L"Dump filter", &CLSID_Dump, CDump::CreateInstance};
  53. int g_cTemplates = 1;
  54.  
  55. //
  56. //  Definition of CDumpFilter
  57. //
  58.  
  59.  
  60. CDumpFilter::CDumpFilter(CDump *pDump,
  61.                          LPUNKNOWN pUnk,
  62.                          CCritSec *pLock,
  63.                          HRESULT *phr) :
  64.     CBaseFilter(NAME("CDumpFilter"), pUnk, pLock, CLSID_Dump, phr),
  65.     m_pDump(pDump)
  66. {
  67. }
  68.  
  69. CBasePin * CDumpFilter::GetPin(int n)
  70. {
  71.     if (n == 0) {
  72.         return m_pDump->m_pPin;
  73.     } else {
  74.         return NULL;
  75.     }
  76. }
  77.  
  78.  
  79. int CDumpFilter::GetPinCount()
  80. {
  81.     return 1;
  82. }
  83.  
  84. //
  85. //  Definition of CDumpInputPin
  86. //
  87.  
  88. CDumpInputPin::CDumpInputPin(CDump *pDump,
  89.                              LPUNKNOWN pUnk,
  90.                              CBaseFilter *pFilter,
  91.                              CCritSec *pLock,
  92.                              CCritSec *pReceiveLock,
  93.                              HRESULT *phr) :
  94.     CRenderedInputPin(NAME("CDumpInputPin"),
  95.                   pFilter,                   // Filter
  96.                   pLock,                     // Locking
  97.                   phr,                       // Return code
  98.                   L"Input"),                 // Pin name
  99.     m_pReceiveLock(pReceiveLock),
  100.     m_pDump(pDump),
  101.     m_tLast(0)
  102. {
  103. }
  104.  
  105. // check if the pin can support this specific proposed type and format
  106. HRESULT CDumpInputPin::CheckMediaType(const CMediaType *)
  107. {
  108.     return S_OK;
  109. }
  110.  
  111. // Break a connection
  112. HRESULT CDumpInputPin::BreakConnect()
  113. {
  114.     if (m_pDump->m_pPosition != NULL) {
  115.         m_pDump->m_pPosition->ForceRefresh();
  116.     }
  117.     return CBaseInputPin::BreakConnect();
  118. }
  119.  
  120. // do something with this media sample
  121. STDMETHODIMP CDumpInputPin::Receive(IMediaSample *pSample)
  122. {
  123.     CAutoLock lck(m_pReceiveLock);
  124. #if 0
  125.     if (m_pFilter->IsStopped()) {
  126.         return VFW_E_WRONG_STATE;
  127.     }
  128. #endif
  129.  
  130.     REFERENCE_TIME tStart, tStop;
  131.     pSample->GetTime(&tStart, &tStop);
  132.     DbgLog((LOG_TRACE, 1, TEXT("tStart(%s), tStop(%s), Diff(%d ms), Bytes(%d)"),
  133.             (LPCTSTR)CDisp(tStart),
  134.             (LPCTSTR)CDisp(tStop),
  135.             (LONG)((tStart - m_tLast) / 10000),
  136.             pSample->GetActualDataLength()));
  137.     m_tLast = tStart;
  138.  
  139.  
  140.     /*  Copy the data to the file */
  141.     PBYTE pbData;
  142.     HRESULT hr = pSample->GetPointer(&pbData);
  143.     if (FAILED(hr)) {
  144.         return hr;
  145.     }
  146.     return m_pDump->Write(pbData, pSample->GetActualDataLength());
  147. }
  148.  
  149. STDMETHODIMP CDumpInputPin::EndOfStream(void)
  150. {
  151.     CAutoLock lck(m_pReceiveLock);
  152.     return CRenderedInputPin::EndOfStream();
  153. }
  154.  
  155. STDMETHODIMP CDumpInputPin::NewSegment(
  156.                 REFERENCE_TIME tStart,
  157.                 REFERENCE_TIME tStop,
  158.                 double dRate)
  159. {
  160.     m_tLast = 0;
  161.     return S_OK;
  162. }
  163.  
  164. //
  165. //  CDump class
  166. //
  167.  
  168. CDump::CDump(LPUNKNOWN pUnk, HRESULT *phr) :
  169.     CUnknown(NAME("CDump"), pUnk, phr),
  170.     m_pFilter(NULL),
  171.     m_pPin(NULL),
  172.     m_pPosition(NULL),
  173.     m_hFile(INVALID_HANDLE_VALUE),
  174.     m_pFileName(0)
  175. {
  176.     /*  Filter */
  177.     m_pFilter = new CDumpFilter(this, GetOwner(), &m_Lock, phr);
  178.     if (m_pFilter == NULL) {
  179.         *phr = E_OUTOFMEMORY;
  180.         return;
  181.     }
  182.     m_pPin = new CDumpInputPin(this,
  183.                                GetOwner(),
  184.                                m_pFilter,
  185.                                &m_Lock,
  186.                                &m_ReceiveLock,
  187.                                phr);
  188.     if (m_pPin == NULL) {
  189.         *phr = E_OUTOFMEMORY;
  190.         return;
  191.     }
  192. }
  193.  
  194. STDMETHODIMP CDump::SetFileName(LPCOLESTR pszFileName,const AM_MEDIA_TYPE *pmt)
  195. {
  196.   if(wcslen(pszFileName) > MAX_PATH)
  197.     return ERROR_FILENAME_EXCED_RANGE;
  198.  
  199.   m_pFileName = new WCHAR[1+lstrlenW(pszFileName)];
  200.   if (m_pFileName == 0)
  201.     return E_OUTOFMEMORY;
  202.   lstrcpyW(m_pFileName, pszFileName);
  203.  
  204.   HRESULT hr = S_OK;
  205.  
  206. # if defined(WIN32) && !defined(UNICODE)
  207.   {
  208.     char bufA[MAX_PATH];
  209.     if(!WideCharToMultiByte(CP_ACP, 0, pszFileName, -1, bufA, MAX_PATH, 0, 0))
  210.       return ERROR_INVALID_NAME;
  211.  
  212.   hr = Open (bufA) ;
  213.  
  214.   }
  215. # else
  216.   {
  217.     hr = Open (pszFileName, &hr);
  218.   }
  219. # endif
  220.  
  221.   if(FAILED(hr))
  222.   {
  223.     DbgLog(( LOG_ERROR, 2,
  224.              TEXT("CDump:: Open file failed")));
  225.     return hr;
  226.   }
  227.   return NOERROR ;
  228.  
  229. }
  230.  
  231. STDMETHODIMP CDump::GetCurFile(
  232.   LPOLESTR * ppszFileName,
  233.   AM_MEDIA_TYPE *pmt)
  234. {
  235.     CheckPointer(ppszFileName, E_POINTER);
  236.   *ppszFileName = NULL;
  237.   if (m_pFileName!=NULL)
  238.   {
  239.     *ppszFileName = (LPOLESTR)
  240.       QzTaskMemAlloc(sizeof(WCHAR) * (1+lstrlenW(m_pFileName)));
  241.     if (*ppszFileName!=NULL) {
  242.       lstrcpyW(*ppszFileName, m_pFileName);
  243.     }
  244.   }
  245.  
  246.   if(pmt)
  247.   {
  248.     ZeroMemory(pmt, sizeof(*pmt));
  249.     pmt->majortype = MEDIATYPE_NULL;
  250.     pmt->subtype = MEDIASUBTYPE_NULL;
  251.   }
  252.  
  253.   return S_OK;
  254. }
  255.  
  256.  
  257. //
  258. // GetSetupData
  259. //
  260. LPAMOVIESETUP_FILTER CDump::GetSetupData()
  261. {
  262.   return &sudDump;
  263. }
  264.  
  265. CDump::~CDump()
  266. {
  267.     if (m_hFile != INVALID_HANDLE_VALUE) {
  268.         CloseHandle(m_hFile);
  269.     }
  270.     delete m_pPin;
  271.     delete m_pFilter;
  272.     delete m_pPosition;
  273.     delete m_pFileName;
  274. }
  275.  
  276. //
  277. // CreateInstance
  278. //
  279. // Provide the way for COM to create a CDump object
  280. CUnknown *CDump::CreateInstance(LPUNKNOWN punk, HRESULT *phr) {
  281.  
  282.     CDump *pNewObject = new CDump(punk, phr);
  283.     if (pNewObject == NULL) {
  284.         *phr = E_OUTOFMEMORY;
  285.     }
  286.  
  287.     return pNewObject;
  288. }
  289.  
  290.  
  291. // override this to say what interfaces we support where
  292. STDMETHODIMP CDump::NonDelegatingQueryInterface(REFIID riid, void ** ppv)
  293. {
  294.     CheckPointer(ppv,E_POINTER);
  295.     CAutoLock lck(&m_Lock);
  296.  
  297.     /* Do we have this interface */
  298.  
  299.     if (riid == IID_IFileSinkFilter)
  300.     {
  301.         return GetInterface((IFileSinkFilter *) this, ppv);
  302.     }
  303.  
  304.     else if (riid == IID_IFilter ||
  305.         riid == IID_IMediaFilter ||
  306.         riid == IID_IPersist) {
  307.     return m_pFilter->NonDelegatingQueryInterface(riid, ppv);
  308.     } else if (riid == IID_IMediaPosition || riid == IID_IMediaSelection) {
  309.         if (m_pPosition == NULL) {
  310.  
  311.             HRESULT hr = S_OK;
  312.             m_pPosition = new CPosPassThru(NAME("CDump CPosPassThru"),
  313.                                            GetOwner(),
  314.                                            &hr,
  315.                                            m_pPin);
  316.             if (m_pPosition == NULL) {
  317.                 return E_OUTOFMEMORY;
  318.             }
  319.  
  320.             if (FAILED(hr)) {
  321.                 delete m_pPosition;
  322.                 m_pPosition = NULL;
  323.                 return hr;
  324.             }
  325.         }
  326.         return m_pPosition->NonDelegatingQueryInterface(riid, ppv);
  327.     } else {
  328.     return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  329.     }
  330. }
  331.  
  332.  
  333. HRESULT CDump::Open(TCHAR *szFile)
  334. {
  335.     // Try to open the file
  336.     m_hFile = CreateFile(szFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
  337.     if (m_hFile == INVALID_HANDLE_VALUE) {
  338.         DWORD dwErr = GetLastError();
  339.         return HRESULT_FROM_WIN32(dwErr);
  340.     }
  341.     return S_OK;
  342. }
  343.  
  344.  
  345. // Write stuff to the file
  346. HRESULT CDump::Write(PBYTE pbData, LONG lData)
  347. {
  348.     DWORD dwWritten;
  349.  
  350.     if (!WriteFile(m_hFile, (PVOID)pbData, (DWORD)lData, &dwWritten, NULL)) {
  351.         DWORD dwErr = GetLastError();
  352.         return HRESULT_FROM_WIN32(dwErr);
  353.     }
  354.     return S_OK;
  355. }
  356.  
  357. /******************************Public*Routine******************************\
  358. * exported entry points for registration and
  359. * unregistration (in this case they only call
  360. * through to default implmentations).
  361. *
  362. *
  363. *
  364. * History:
  365. *
  366. \**************************************************************************/
  367. HRESULT
  368. DllRegisterServer()
  369. {
  370.   return AMovieDllRegisterServer();
  371. }
  372.  
  373. HRESULT
  374. DllUnregisterServer()
  375. {
  376.   return AMovieDllUnregisterServer();
  377. }
  378.  
  379.