home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8a_sdk.exe / samples / multimedia / directshow / baseclasses / ctlutil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-02  |  52.2 KB  |  2,532 lines

  1. //------------------------------------------------------------------------------
  2. // File: CtlUtil.cpp
  3. //
  4. // Desc: DirectShow base classes.
  5. //
  6. // Copyright (c) 1992 - 2000, Microsoft Corporation.  All rights reserved.
  7. //------------------------------------------------------------------------------
  8.  
  9.  
  10. // Base classes implementing IDispatch parsing for the basic control dual
  11. // interfaces. Derive from these and implement just the custom method and
  12. // property methods. We also implement CPosPassThru that can be used by
  13. // renderers and transforms to pass by IMediaPosition and IMediaSeeking
  14.  
  15.  
  16. #include <streams.h>
  17. #include <limits.h>
  18. #include "seekpt.h"
  19.  
  20. // 'bool' non standard reserved word
  21. #pragma warning(disable:4237)
  22.  
  23.  
  24. // --- CBaseDispatch implementation ----------
  25. CBaseDispatch::~CBaseDispatch()
  26. {
  27.     if (m_pti) {
  28.     m_pti->Release();
  29.     }
  30. }
  31.  
  32.  
  33. // return 1 if we support GetTypeInfo
  34.  
  35. STDMETHODIMP
  36. CBaseDispatch::GetTypeInfoCount(UINT * pctinfo)
  37. {
  38.     CheckPointer(pctinfo,E_POINTER);
  39.     ValidateReadWritePtr(pctinfo,sizeof(UINT *));
  40.     *pctinfo = 1;
  41.     return S_OK;
  42. }
  43.  
  44.  
  45. typedef HRESULT (STDAPICALLTYPE *LPLOADTYPELIB)(
  46.                 const OLECHAR FAR *szFile,
  47.                 ITypeLib FAR* FAR* pptlib);
  48.  
  49. typedef HRESULT (STDAPICALLTYPE *LPLOADREGTYPELIB)(REFGUID rguid,
  50.                 WORD wVerMajor,
  51.                 WORD wVerMinor,
  52.                 LCID lcid,
  53.                 ITypeLib FAR* FAR* pptlib);
  54.  
  55. // attempt to find our type library
  56.  
  57. STDMETHODIMP
  58. CBaseDispatch::GetTypeInfo(
  59.   REFIID riid,
  60.   UINT itinfo,
  61.   LCID lcid,
  62.   ITypeInfo ** pptinfo)
  63. {
  64.     CheckPointer(pptinfo,E_POINTER);
  65.     ValidateReadWritePtr(pptinfo,sizeof(ITypeInfo *));
  66.     HRESULT hr;
  67.  
  68.     *pptinfo = NULL;
  69.  
  70.     // we only support one type element
  71.     if (0 != itinfo) {
  72.     return TYPE_E_ELEMENTNOTFOUND;
  73.     }
  74.  
  75.     if (NULL == pptinfo) {
  76.     return E_POINTER;
  77.     }
  78.  
  79.     // always look for neutral
  80.     if (NULL == m_pti) {
  81.  
  82.     LPLOADTYPELIB        lpfnLoadTypeLib;
  83.     LPLOADREGTYPELIB    lpfnLoadRegTypeLib;
  84.     ITypeLib        *ptlib;
  85.     HINSTANCE        hInst;
  86.  
  87.     static const char  szTypeLib[]      = "LoadTypeLib";
  88.     static const char  szRegTypeLib[] = "LoadRegTypeLib";
  89.     static const WCHAR szControl[]      = L"control.tlb";
  90.  
  91.     //
  92.     // Try to get the Ole32Aut.dll module handle.
  93.     //
  94.  
  95.     hInst = LoadOLEAut32();
  96.     if (hInst == NULL) {
  97.         DWORD dwError = GetLastError();
  98.         return AmHresultFromWin32(dwError);
  99.     }
  100.     lpfnLoadRegTypeLib = (LPLOADREGTYPELIB)GetProcAddress(hInst,
  101.                                   szRegTypeLib);
  102.     if (lpfnLoadRegTypeLib == NULL) {
  103.         DWORD dwError = GetLastError();
  104.         return AmHresultFromWin32(dwError);
  105.     }
  106.  
  107.     hr = (*lpfnLoadRegTypeLib)(LIBID_QuartzTypeLib, 1, 0, // version 1.0
  108.                    lcid, &ptlib);
  109.  
  110.     if (FAILED(hr)) {
  111.  
  112.         // attempt to load directly - this will fill the
  113.         // registry in if it finds it
  114.  
  115.         lpfnLoadTypeLib = (LPLOADTYPELIB)GetProcAddress(hInst, szTypeLib);
  116.         if (lpfnLoadTypeLib == NULL) {
  117.         DWORD dwError = GetLastError();
  118.         return AmHresultFromWin32(dwError);
  119.         }
  120.  
  121.         hr = (*lpfnLoadTypeLib)(szControl, &ptlib);
  122.         if (FAILED(hr)) {
  123.         return hr;
  124.         }
  125.     }
  126.  
  127.     hr = ptlib->GetTypeInfoOfGuid(
  128.             riid,
  129.             &m_pti);
  130.  
  131.     ptlib->Release();
  132.  
  133.     if (FAILED(hr)) {
  134.         return hr;
  135.     }
  136.     }
  137.  
  138.     *pptinfo = m_pti;
  139.     m_pti->AddRef();
  140.     return S_OK;
  141. }
  142.  
  143.  
  144. STDMETHODIMP
  145. CBaseDispatch::GetIDsOfNames(
  146.   REFIID riid,
  147.   OLECHAR  ** rgszNames,
  148.   UINT cNames,
  149.   LCID lcid,
  150.   DISPID * rgdispid)
  151. {
  152.     // although the IDispatch riid is dead, we use this to pass from
  153.     // the interface implementation class to us the iid we are talking about.
  154.  
  155.     ITypeInfo * pti;
  156.     HRESULT hr = GetTypeInfo(riid, 0, lcid, &pti);
  157.  
  158.     if (SUCCEEDED(hr)) {
  159.     hr = pti->GetIDsOfNames(rgszNames, cNames, rgdispid);
  160.  
  161.     pti->Release();
  162.     }
  163.     return hr;
  164. }
  165.  
  166.  
  167. // --- CMediaControl implementation ---------
  168.  
  169. CMediaControl::CMediaControl(const TCHAR * name,LPUNKNOWN pUnk) :
  170.     CUnknown(name, pUnk)
  171. {
  172. }
  173.  
  174. // expose our interfaces IMediaControl and IUnknown
  175.  
  176. STDMETHODIMP
  177. CMediaControl::NonDelegatingQueryInterface(REFIID riid, void **ppv)
  178. {
  179.     ValidateReadWritePtr(ppv,sizeof(PVOID));
  180.     if (riid == IID_IMediaControl) {
  181.     return GetInterface( (IMediaControl *) this, ppv);
  182.     } else {
  183.     return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  184.     }
  185. }
  186.  
  187.  
  188. // return 1 if we support GetTypeInfo
  189.  
  190. STDMETHODIMP
  191. CMediaControl::GetTypeInfoCount(UINT * pctinfo)
  192. {
  193.     return m_basedisp.GetTypeInfoCount(pctinfo);
  194. }
  195.  
  196.  
  197. // attempt to find our type library
  198.  
  199. STDMETHODIMP
  200. CMediaControl::GetTypeInfo(
  201.   UINT itinfo,
  202.   LCID lcid,
  203.   ITypeInfo ** pptinfo)
  204. {
  205.     return m_basedisp.GetTypeInfo(
  206.         IID_IMediaControl,
  207.         itinfo,
  208.         lcid,
  209.         pptinfo);
  210. }
  211.  
  212.  
  213. STDMETHODIMP
  214. CMediaControl::GetIDsOfNames(
  215.   REFIID riid,
  216.   OLECHAR  ** rgszNames,
  217.   UINT cNames,
  218.   LCID lcid,
  219.   DISPID * rgdispid)
  220. {
  221.     return m_basedisp.GetIDsOfNames(
  222.             IID_IMediaControl,
  223.             rgszNames,
  224.             cNames,
  225.             lcid,
  226.             rgdispid);
  227. }
  228.  
  229.  
  230. STDMETHODIMP
  231. CMediaControl::Invoke(
  232.   DISPID dispidMember,
  233.   REFIID riid,
  234.   LCID lcid,
  235.   WORD wFlags,
  236.   DISPPARAMS * pdispparams,
  237.   VARIANT * pvarResult,
  238.   EXCEPINFO * pexcepinfo,
  239.   UINT * puArgErr)
  240. {
  241.     // this parameter is a dead leftover from an earlier interface
  242.     if (IID_NULL != riid) {
  243.     return DISP_E_UNKNOWNINTERFACE;
  244.     }
  245.  
  246.     ITypeInfo * pti;
  247.     HRESULT hr = GetTypeInfo(0, lcid, &pti);
  248.  
  249.     if (FAILED(hr)) {
  250.     return hr;
  251.     }
  252.  
  253.     hr = pti->Invoke(
  254.         (IMediaControl *)this,
  255.         dispidMember,
  256.         wFlags,
  257.         pdispparams,
  258.         pvarResult,
  259.         pexcepinfo,
  260.         puArgErr);
  261.  
  262.     pti->Release();
  263.     return hr;
  264. }
  265.  
  266.  
  267. // --- CMediaEvent implementation ----------
  268.  
  269.  
  270. CMediaEvent::CMediaEvent(const TCHAR * name,LPUNKNOWN pUnk) :
  271.     CUnknown(name, pUnk)
  272. {
  273. }
  274.  
  275.  
  276. // expose our interfaces IMediaEvent and IUnknown
  277.  
  278. STDMETHODIMP
  279. CMediaEvent::NonDelegatingQueryInterface(REFIID riid, void **ppv)
  280. {
  281.     ValidateReadWritePtr(ppv,sizeof(PVOID));
  282.     if (riid == IID_IMediaEvent || riid == IID_IMediaEventEx) {
  283.     return GetInterface( (IMediaEventEx *) this, ppv);
  284.     } else {
  285.     return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  286.     }
  287. }
  288.  
  289.  
  290. // return 1 if we support GetTypeInfo
  291.  
  292. STDMETHODIMP
  293. CMediaEvent::GetTypeInfoCount(UINT * pctinfo)
  294. {
  295.     return m_basedisp.GetTypeInfoCount(pctinfo);
  296. }
  297.  
  298.  
  299. // attempt to find our type library
  300.  
  301. STDMETHODIMP
  302. CMediaEvent::GetTypeInfo(
  303.   UINT itinfo,
  304.   LCID lcid,
  305.   ITypeInfo ** pptinfo)
  306. {
  307.     return m_basedisp.GetTypeInfo(
  308.         IID_IMediaEvent,
  309.         itinfo,
  310.         lcid,
  311.         pptinfo);
  312. }
  313.  
  314.  
  315. STDMETHODIMP
  316. CMediaEvent::GetIDsOfNames(
  317.   REFIID riid,
  318.   OLECHAR  ** rgszNames,
  319.   UINT cNames,
  320.   LCID lcid,
  321.   DISPID * rgdispid)
  322. {
  323.     return m_basedisp.GetIDsOfNames(
  324.             IID_IMediaEvent,
  325.             rgszNames,
  326.             cNames,
  327.             lcid,
  328.             rgdispid);
  329. }
  330.  
  331.  
  332. STDMETHODIMP
  333. CMediaEvent::Invoke(
  334.   DISPID dispidMember,
  335.   REFIID riid,
  336.   LCID lcid,
  337.   WORD wFlags,
  338.   DISPPARAMS * pdispparams,
  339.   VARIANT * pvarResult,
  340.   EXCEPINFO * pexcepinfo,
  341.   UINT * puArgErr)
  342. {
  343.     // this parameter is a dead leftover from an earlier interface
  344.     if (IID_NULL != riid) {
  345.     return DISP_E_UNKNOWNINTERFACE;
  346.     }
  347.  
  348.     ITypeInfo * pti;
  349.     HRESULT hr = GetTypeInfo(0, lcid, &pti);
  350.  
  351.     if (FAILED(hr)) {
  352.     return hr;
  353.     }
  354.  
  355.     hr = pti->Invoke(
  356.         (IMediaEvent *)this,
  357.         dispidMember,
  358.         wFlags,
  359.         pdispparams,
  360.         pvarResult,
  361.         pexcepinfo,
  362.         puArgErr);
  363.  
  364.     pti->Release();
  365.     return hr;
  366. }
  367.  
  368.  
  369. // --- CMediaPosition implementation ----------
  370.  
  371.  
  372. CMediaPosition::CMediaPosition(const TCHAR * name,LPUNKNOWN pUnk) :
  373.     CUnknown(name, pUnk)
  374. {
  375. }
  376.  
  377. CMediaPosition::CMediaPosition(const TCHAR * name,
  378.                                LPUNKNOWN pUnk,
  379.                                HRESULT * phr) :
  380.     CUnknown(name, pUnk)
  381. {
  382.     UNREFERENCED_PARAMETER(phr);
  383. }
  384.  
  385.  
  386. // expose our interfaces IMediaPosition and IUnknown
  387.  
  388. STDMETHODIMP
  389. CMediaPosition::NonDelegatingQueryInterface(REFIID riid, void **ppv)
  390. {
  391.     ValidateReadWritePtr(ppv,sizeof(PVOID));
  392.     if (riid == IID_IMediaPosition) {
  393.     return GetInterface( (IMediaPosition *) this, ppv);
  394.     } else {
  395.     return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  396.     }
  397. }
  398.  
  399.  
  400. // return 1 if we support GetTypeInfo
  401.  
  402. STDMETHODIMP
  403. CMediaPosition::GetTypeInfoCount(UINT * pctinfo)
  404. {
  405.     return m_basedisp.GetTypeInfoCount(pctinfo);
  406. }
  407.  
  408.  
  409. // attempt to find our type library
  410.  
  411. STDMETHODIMP
  412. CMediaPosition::GetTypeInfo(
  413.   UINT itinfo,
  414.   LCID lcid,
  415.   ITypeInfo ** pptinfo)
  416. {
  417.     return m_basedisp.GetTypeInfo(
  418.         IID_IMediaPosition,
  419.         itinfo,
  420.         lcid,
  421.         pptinfo);
  422. }
  423.  
  424.  
  425. STDMETHODIMP
  426. CMediaPosition::GetIDsOfNames(
  427.   REFIID riid,
  428.   OLECHAR  ** rgszNames,
  429.   UINT cNames,
  430.   LCID lcid,
  431.   DISPID * rgdispid)
  432. {
  433.     return m_basedisp.GetIDsOfNames(
  434.             IID_IMediaPosition,
  435.             rgszNames,
  436.             cNames,
  437.             lcid,
  438.             rgdispid);
  439. }
  440.  
  441.  
  442. STDMETHODIMP
  443. CMediaPosition::Invoke(
  444.   DISPID dispidMember,
  445.   REFIID riid,
  446.   LCID lcid,
  447.   WORD wFlags,
  448.   DISPPARAMS * pdispparams,
  449.   VARIANT * pvarResult,
  450.   EXCEPINFO * pexcepinfo,
  451.   UINT * puArgErr)
  452. {
  453.     // this parameter is a dead leftover from an earlier interface
  454.     if (IID_NULL != riid) {
  455.     return DISP_E_UNKNOWNINTERFACE;
  456.     }
  457.  
  458.     ITypeInfo * pti;
  459.     HRESULT hr = GetTypeInfo(0, lcid, &pti);
  460.  
  461.     if (FAILED(hr)) {
  462.     return hr;
  463.     }
  464.  
  465.     hr = pti->Invoke(
  466.         (IMediaPosition *)this,
  467.         dispidMember,
  468.         wFlags,
  469.         pdispparams,
  470.         pvarResult,
  471.         pexcepinfo,
  472.         puArgErr);
  473.  
  474.     pti->Release();
  475.     return hr;
  476. }
  477.  
  478.  
  479. // --- IMediaPosition and IMediaSeeking pass through class ----------
  480.  
  481.  
  482. CPosPassThru::CPosPassThru(const TCHAR *pName,
  483.                LPUNKNOWN pUnk,
  484.                HRESULT *phr,
  485.                IPin *pPin) :
  486.     CMediaPosition(pName,pUnk),
  487.     m_pPin(pPin)
  488. {
  489.     if (pPin == NULL) {
  490.     *phr = E_POINTER;
  491.     return;
  492.     }
  493. }
  494.  
  495.  
  496. // Expose our IMediaSeeking and IMediaPosition interfaces
  497.  
  498. STDMETHODIMP
  499. CPosPassThru::NonDelegatingQueryInterface(REFIID riid,void **ppv)
  500. {
  501.     CheckPointer(ppv,E_POINTER);
  502.     *ppv = NULL;
  503.  
  504.     if (riid == IID_IMediaSeeking) {
  505.     return GetInterface( static_cast<IMediaSeeking *>(this), ppv);
  506.     }
  507.     return CMediaPosition::NonDelegatingQueryInterface(riid,ppv);
  508. }
  509.  
  510.  
  511. // Return the IMediaPosition interface from our peer
  512.  
  513. HRESULT
  514. CPosPassThru::GetPeer(IMediaPosition ** ppMP)
  515. {
  516.     *ppMP = NULL;
  517.  
  518.     IPin *pConnected;
  519.     HRESULT hr = m_pPin->ConnectedTo(&pConnected);
  520.     if (FAILED(hr)) {
  521.     return E_NOTIMPL;
  522.     }
  523.     IMediaPosition * pMP;
  524.     hr = pConnected->QueryInterface(IID_IMediaPosition, (void **) &pMP);
  525.     pConnected->Release();
  526.     if (FAILED(hr)) {
  527.     return E_NOTIMPL;
  528.     }
  529.  
  530.     *ppMP = pMP;
  531.     return S_OK;
  532. }
  533.  
  534.  
  535. // Return the IMediaSeeking interface from our peer
  536.  
  537. HRESULT
  538. CPosPassThru::GetPeerSeeking(IMediaSeeking ** ppMS)
  539. {
  540.     *ppMS = NULL;
  541.  
  542.     IPin *pConnected;
  543.     HRESULT hr = m_pPin->ConnectedTo(&pConnected);
  544.     if (FAILED(hr)) {
  545.     return E_NOTIMPL;
  546.     }
  547.     IMediaSeeking * pMS;
  548.     hr = pConnected->QueryInterface(IID_IMediaSeeking, (void **) &pMS);
  549.     pConnected->Release();
  550.     if (FAILED(hr)) {
  551.     return E_NOTIMPL;
  552.     }
  553.  
  554.     *ppMS = pMS;
  555.     return S_OK;
  556. }
  557.  
  558.  
  559. // --- IMediaSeeking methods ----------
  560.  
  561.  
  562. STDMETHODIMP
  563. CPosPassThru::GetCapabilities(DWORD * pCaps)
  564. {
  565.     IMediaSeeking* pMS;
  566.     HRESULT hr = GetPeerSeeking(&pMS);
  567.     if (FAILED(hr)) {
  568.     return hr;
  569.     }
  570.  
  571.     hr = pMS->GetCapabilities(pCaps);
  572.     pMS->Release();
  573.     return hr;
  574. }
  575.  
  576. STDMETHODIMP
  577. CPosPassThru::CheckCapabilities(DWORD * pCaps)
  578. {
  579.     IMediaSeeking* pMS;
  580.     HRESULT hr = GetPeerSeeking(&pMS);
  581.     if (FAILED(hr)) {
  582.     return hr;
  583.     }
  584.  
  585.     hr = pMS->CheckCapabilities(pCaps);
  586.     pMS->Release();
  587.     return hr;
  588. }
  589.  
  590. STDMETHODIMP
  591. CPosPassThru::IsFormatSupported(const GUID * pFormat)
  592. {
  593.     IMediaSeeking* pMS;
  594.     HRESULT hr = GetPeerSeeking(&pMS);
  595.     if (FAILED(hr)) {
  596.     return hr;
  597.     }
  598.  
  599.     hr = pMS->IsFormatSupported(pFormat);
  600.     pMS->Release();
  601.     return hr;
  602. }
  603.  
  604.  
  605. STDMETHODIMP
  606. CPosPassThru::QueryPreferredFormat(GUID *pFormat)
  607. {
  608.     IMediaSeeking* pMS;
  609.     HRESULT hr = GetPeerSeeking(&pMS);
  610.     if (FAILED(hr)) {
  611.     return hr;
  612.     }
  613.  
  614.     hr = pMS->QueryPreferredFormat(pFormat);
  615.     pMS->Release();
  616.     return hr;
  617. }
  618.  
  619.  
  620. STDMETHODIMP
  621. CPosPassThru::SetTimeFormat(const GUID * pFormat)
  622. {
  623.     IMediaSeeking* pMS;
  624.     HRESULT hr = GetPeerSeeking(&pMS);
  625.     if (FAILED(hr)) {
  626.     return hr;
  627.     }
  628.  
  629.     hr = pMS->SetTimeFormat(pFormat);
  630.     pMS->Release();
  631.     return hr;
  632. }
  633.  
  634.  
  635. STDMETHODIMP
  636. CPosPassThru::GetTimeFormat(GUID *pFormat)
  637. {
  638.     IMediaSeeking* pMS;
  639.     HRESULT hr = GetPeerSeeking(&pMS);
  640.     if (FAILED(hr)) {
  641.     return hr;
  642.     }
  643.  
  644.     hr = pMS->GetTimeFormat(pFormat);
  645.     pMS->Release();
  646.     return hr;
  647. }
  648.  
  649.  
  650. STDMETHODIMP
  651. CPosPassThru::IsUsingTimeFormat(const GUID * pFormat)
  652. {
  653.     IMediaSeeking* pMS;
  654.     HRESULT hr = GetPeerSeeking(&pMS);
  655.     if (FAILED(hr)) {
  656.     return hr;
  657.     }
  658.  
  659.     hr = pMS->IsUsingTimeFormat(pFormat);
  660.     pMS->Release();
  661.     return hr;
  662. }
  663.  
  664.  
  665. STDMETHODIMP
  666. CPosPassThru::ConvertTimeFormat(LONGLONG * pTarget, const GUID * pTargetFormat,
  667.                 LONGLONG    Source, const GUID * pSourceFormat )
  668. {
  669.     IMediaSeeking* pMS;
  670.     HRESULT hr = GetPeerSeeking(&pMS);
  671.     if (FAILED(hr)) {
  672.     return hr;
  673.     }
  674.  
  675.     hr = pMS->ConvertTimeFormat(pTarget, pTargetFormat, Source, pSourceFormat );
  676.     pMS->Release();
  677.     return hr;
  678. }
  679.  
  680.  
  681. STDMETHODIMP
  682. CPosPassThru::SetPositions( LONGLONG * pCurrent, DWORD CurrentFlags
  683.               , LONGLONG * pStop, DWORD StopFlags )
  684. {
  685.     IMediaSeeking* pMS;
  686.     HRESULT hr = GetPeerSeeking(&pMS);
  687.     if (FAILED(hr)) {
  688.     return hr;
  689.     }
  690.  
  691.     hr = pMS->SetPositions(pCurrent, CurrentFlags, pStop, StopFlags );
  692.     pMS->Release();
  693.     return hr;
  694. }
  695.  
  696. STDMETHODIMP
  697. CPosPassThru::GetPositions(LONGLONG *pCurrent, LONGLONG * pStop)
  698. {
  699.     IMediaSeeking* pMS;
  700.     HRESULT hr = GetPeerSeeking(&pMS);
  701.     if (FAILED(hr)) {
  702.     return hr;
  703.     }
  704.  
  705.     hr = pMS->GetPositions(pCurrent,pStop);
  706.     pMS->Release();
  707.     return hr;
  708. }
  709.  
  710. HRESULT
  711. CPosPassThru::GetSeekingLongLong
  712. ( HRESULT (__stdcall IMediaSeeking::*pMethod)( LONGLONG * )
  713. , LONGLONG * pll
  714. )
  715. {
  716.     IMediaSeeking* pMS;
  717.     HRESULT hr = GetPeerSeeking(&pMS);
  718.     if (SUCCEEDED(hr))
  719.     {
  720.     hr = (pMS->*pMethod)(pll);
  721.     pMS->Release();
  722.     }
  723.     return hr;
  724. }
  725.  
  726. // If we don't have a current position then ask upstream
  727.  
  728. STDMETHODIMP
  729. CPosPassThru::GetCurrentPosition(LONGLONG *pCurrent)
  730. {
  731.     // Can we report the current position
  732.     HRESULT hr = GetMediaTime(pCurrent,NULL);
  733.     if (SUCCEEDED(hr)) hr = NOERROR;
  734.     else hr = GetSeekingLongLong( &IMediaSeeking::GetCurrentPosition, pCurrent );
  735.     return hr;
  736. }
  737.  
  738.  
  739. STDMETHODIMP
  740. CPosPassThru::GetStopPosition(LONGLONG *pStop)
  741. {
  742.     return GetSeekingLongLong( &IMediaSeeking::GetStopPosition, pStop );;
  743. }
  744.  
  745. STDMETHODIMP
  746. CPosPassThru::GetDuration(LONGLONG *pDuration)
  747. {
  748.     return GetSeekingLongLong( &IMediaSeeking::GetDuration, pDuration );;
  749. }
  750.  
  751.  
  752. STDMETHODIMP
  753. CPosPassThru::GetPreroll(LONGLONG *pllPreroll)
  754. {
  755.     return GetSeekingLongLong( &IMediaSeeking::GetPreroll, pllPreroll );;
  756. }
  757.  
  758.  
  759. STDMETHODIMP
  760. CPosPassThru::GetAvailable( LONGLONG *pEarliest, LONGLONG *pLatest )
  761. {
  762.     IMediaSeeking* pMS;
  763.     HRESULT hr = GetPeerSeeking(&pMS);
  764.     if (FAILED(hr)) {
  765.     return hr;
  766.     }
  767.  
  768.     hr = pMS->GetAvailable( pEarliest, pLatest );
  769.     pMS->Release();
  770.     return hr;
  771. }
  772.  
  773.  
  774. STDMETHODIMP
  775. CPosPassThru::GetRate(double * pdRate)
  776. {
  777.     IMediaSeeking* pMS;
  778.     HRESULT hr = GetPeerSeeking(&pMS);
  779.     if (FAILED(hr)) {
  780.     return hr;
  781.     }
  782.     hr = pMS->GetRate(pdRate);
  783.     pMS->Release();
  784.     return hr;
  785. }
  786.  
  787.  
  788. STDMETHODIMP
  789. CPosPassThru::SetRate(double dRate)
  790. {
  791.     if (0.0 == dRate) {
  792.         return E_INVALIDARG;
  793.     }
  794.  
  795.     IMediaSeeking* pMS;
  796.     HRESULT hr = GetPeerSeeking(&pMS);
  797.     if (FAILED(hr)) {
  798.     return hr;
  799.     }
  800.     hr = pMS->SetRate(dRate);
  801.     pMS->Release();
  802.     return hr;
  803. }
  804.  
  805.  
  806.  
  807.  
  808. // --- IMediaPosition methods ----------
  809.  
  810.  
  811. STDMETHODIMP
  812. CPosPassThru::get_Duration(REFTIME * plength)
  813. {
  814.     IMediaPosition* pMP;
  815.     HRESULT hr = GetPeer(&pMP);
  816.     if (FAILED(hr)) {
  817.     return hr;
  818.     }
  819.  
  820.     hr = pMP->get_Duration(plength);
  821.     pMP->Release();
  822.     return hr;
  823. }
  824.  
  825.  
  826. STDMETHODIMP
  827. CPosPassThru::get_CurrentPosition(REFTIME * pllTime)
  828. {
  829.     IMediaPosition* pMP;
  830.     HRESULT hr = GetPeer(&pMP);
  831.     if (FAILED(hr)) {
  832.     return hr;
  833.     }
  834.     hr = pMP->get_CurrentPosition(pllTime);
  835.     pMP->Release();
  836.     return hr;
  837. }
  838.  
  839.  
  840. STDMETHODIMP
  841. CPosPassThru::put_CurrentPosition(REFTIME llTime)
  842. {
  843.     IMediaPosition* pMP;
  844.     HRESULT hr = GetPeer(&pMP);
  845.     if (FAILED(hr)) {
  846.     return hr;
  847.     }
  848.     hr = pMP->put_CurrentPosition(llTime);
  849.     pMP->Release();
  850.     return hr;
  851. }
  852.  
  853.  
  854. STDMETHODIMP
  855. CPosPassThru::get_StopTime(REFTIME * pllTime)
  856. {
  857.     IMediaPosition* pMP;
  858.     HRESULT hr = GetPeer(&pMP);
  859.     if (FAILED(hr)) {
  860.     return hr;
  861.     }
  862.     hr = pMP->get_StopTime(pllTime);
  863.     pMP->Release();
  864.     return hr;
  865. }
  866.  
  867.  
  868. STDMETHODIMP
  869. CPosPassThru::put_StopTime(REFTIME llTime)
  870. {
  871.     IMediaPosition* pMP;
  872.     HRESULT hr = GetPeer(&pMP);
  873.     if (FAILED(hr)) {
  874.     return hr;
  875.     }
  876.     hr = pMP->put_StopTime(llTime);
  877.     pMP->Release();
  878.     return hr;
  879. }
  880.  
  881.  
  882. STDMETHODIMP
  883. CPosPassThru::get_PrerollTime(REFTIME * pllTime)
  884. {
  885.     IMediaPosition* pMP;
  886.     HRESULT hr = GetPeer(&pMP);
  887.     if (FAILED(hr)) {
  888.     return hr;
  889.     }
  890.     hr = pMP->get_PrerollTime(pllTime);
  891.     pMP->Release();
  892.     return hr;
  893. }
  894.  
  895.  
  896. STDMETHODIMP
  897. CPosPassThru::put_PrerollTime(REFTIME llTime)
  898. {
  899.     IMediaPosition* pMP;
  900.     HRESULT hr = GetPeer(&pMP);
  901.     if (FAILED(hr)) {
  902.     return hr;
  903.     }
  904.     hr = pMP->put_PrerollTime(llTime);
  905.     pMP->Release();
  906.     return hr;
  907. }
  908.  
  909.  
  910. STDMETHODIMP
  911. CPosPassThru::get_Rate(double * pdRate)
  912. {
  913.     IMediaPosition* pMP;
  914.     HRESULT hr = GetPeer(&pMP);
  915.     if (FAILED(hr)) {
  916.     return hr;
  917.     }
  918.     hr = pMP->get_Rate(pdRate);
  919.     pMP->Release();
  920.     return hr;
  921. }
  922.  
  923.  
  924. STDMETHODIMP
  925. CPosPassThru::put_Rate(double dRate)
  926. {
  927.     if (0.0 == dRate) {
  928.         return E_INVALIDARG;
  929.     }
  930.  
  931.     IMediaPosition* pMP;
  932.     HRESULT hr = GetPeer(&pMP);
  933.     if (FAILED(hr)) {
  934.     return hr;
  935.     }
  936.     hr = pMP->put_Rate(dRate);
  937.     pMP->Release();
  938.     return hr;
  939. }
  940.  
  941.  
  942. STDMETHODIMP
  943. CPosPassThru::CanSeekForward(LONG *pCanSeekForward)
  944. {
  945.     IMediaPosition* pMP;
  946.     HRESULT hr = GetPeer(&pMP);
  947.     if (FAILED(hr)) {
  948.     return hr;
  949.     }
  950.     hr = pMP->CanSeekForward(pCanSeekForward);
  951.     pMP->Release();
  952.     return hr;
  953. }
  954.  
  955.  
  956. STDMETHODIMP
  957. CPosPassThru::CanSeekBackward(LONG *pCanSeekBackward)
  958. {
  959.     IMediaPosition* pMP;
  960.     HRESULT hr = GetPeer(&pMP);
  961.     if (FAILED(hr)) {
  962.     return hr;
  963.     }
  964.     hr = pMP->CanSeekBackward(pCanSeekBackward);
  965.     pMP->Release();
  966.     return hr;
  967. }
  968.  
  969.  
  970. // --- Implements the CRendererPosPassThru class ----------
  971.  
  972.  
  973. // Media times (eg current frame, field, sample etc) are passed through the
  974. // filtergraph in media samples. When a renderer gets a sample with media
  975. // times in it, it will call one of the RegisterMediaTime methods we expose
  976. // (one takes an IMediaSample, the other takes the media times direct). We
  977. // store the media times internally and return them in GetCurrentPosition.
  978.  
  979. CRendererPosPassThru::CRendererPosPassThru(const TCHAR *pName,
  980.                        LPUNKNOWN pUnk,
  981.                        HRESULT *phr,
  982.                        IPin *pPin) :
  983.     CPosPassThru(pName,pUnk,phr,pPin),
  984.     m_StartMedia(0),
  985.     m_EndMedia(0),
  986.     m_bReset(TRUE)
  987. {
  988. }
  989.  
  990.  
  991. // Sets the media times the object should report
  992.  
  993. HRESULT
  994. CRendererPosPassThru::RegisterMediaTime(IMediaSample *pMediaSample)
  995. {
  996.     ASSERT(pMediaSample);
  997.     LONGLONG StartMedia;
  998.     LONGLONG EndMedia;
  999.  
  1000.     CAutoLock cAutoLock(&m_PositionLock);
  1001.  
  1002.     // Get the media times from the sample
  1003.  
  1004.     HRESULT hr = pMediaSample->GetTime(&StartMedia,&EndMedia);
  1005.     if (FAILED(hr))
  1006.     {
  1007.     ASSERT(hr == VFW_E_SAMPLE_TIME_NOT_SET);
  1008.     return hr;
  1009.     }
  1010.  
  1011.     m_StartMedia = StartMedia;
  1012.     m_EndMedia = EndMedia;
  1013.     m_bReset = FALSE;
  1014.     return NOERROR;
  1015. }
  1016.  
  1017.  
  1018. // Sets the media times the object should report
  1019.  
  1020. HRESULT
  1021. CRendererPosPassThru::RegisterMediaTime(LONGLONG StartTime,LONGLONG EndTime)
  1022. {
  1023.     CAutoLock cAutoLock(&m_PositionLock);
  1024.     m_StartMedia = StartTime;
  1025.     m_EndMedia = EndTime;
  1026.     m_bReset = FALSE;
  1027.     return NOERROR;
  1028. }
  1029.  
  1030.  
  1031. // Return the current media times registered in the object
  1032.  
  1033. HRESULT
  1034. CRendererPosPassThru::GetMediaTime(LONGLONG *pStartTime,LONGLONG *pEndTime)
  1035. {
  1036.     ASSERT(pStartTime);
  1037.  
  1038.     CAutoLock cAutoLock(&m_PositionLock);
  1039.     if (m_bReset == TRUE) {
  1040.     return E_FAIL;
  1041.     }
  1042.  
  1043.     // We don't have to return the end time
  1044.  
  1045.     HRESULT hr = ConvertTimeFormat( pStartTime, 0, m_StartMedia, &TIME_FORMAT_MEDIA_TIME );
  1046.     if (pEndTime && SUCCEEDED(hr)) {
  1047.     hr = ConvertTimeFormat( pEndTime, 0, m_EndMedia, &TIME_FORMAT_MEDIA_TIME );
  1048.     }
  1049.     return hr;
  1050. }
  1051.  
  1052.  
  1053. // Resets the media times we hold
  1054.  
  1055. HRESULT
  1056. CRendererPosPassThru::ResetMediaTime()
  1057. {
  1058.     CAutoLock cAutoLock(&m_PositionLock);
  1059.     m_StartMedia = 0;
  1060.     m_EndMedia = 0;
  1061.     m_bReset = TRUE;
  1062.     return NOERROR;
  1063. }
  1064.  
  1065. // Intended to be called by the owing filter during EOS processing so
  1066. // that the media times can be adjusted to the stop time.  This ensures
  1067. // that the GetCurrentPosition will actully get to the stop position.
  1068. HRESULT
  1069. CRendererPosPassThru::EOS()
  1070. {
  1071.     HRESULT hr;
  1072.  
  1073.     if ( m_bReset == TRUE ) hr = E_FAIL;
  1074.     else
  1075.     {
  1076.     LONGLONG llStop;
  1077.     if SUCCEEDED(hr=GetStopPosition(&llStop))
  1078.     {
  1079.         CAutoLock cAutoLock(&m_PositionLock);
  1080.         m_StartMedia =
  1081.         m_EndMedia     = llStop;
  1082.     }
  1083.     }
  1084.     return hr;
  1085. }
  1086.  
  1087. // -- CSourceSeeking implementation ------------
  1088.  
  1089. CSourceSeeking::CSourceSeeking(
  1090.     const TCHAR * pName,
  1091.     LPUNKNOWN pUnk,
  1092.     HRESULT* phr,
  1093.     CCritSec * pLock) :
  1094.         CUnknown(pName, pUnk),
  1095.         m_pLock(pLock),
  1096.         m_rtStart((long)0)
  1097. {
  1098.     m_rtStop = _I64_MAX / 2;
  1099.     m_rtDuration = m_rtStop;
  1100.     m_dRateSeeking = 1.0;
  1101.  
  1102.     m_dwSeekingCaps = AM_SEEKING_CanSeekForwards
  1103.         | AM_SEEKING_CanSeekBackwards
  1104.         | AM_SEEKING_CanSeekAbsolute
  1105.         | AM_SEEKING_CanGetStopPos
  1106.         | AM_SEEKING_CanGetDuration;
  1107. }
  1108.  
  1109. HRESULT CSourceSeeking::NonDelegatingQueryInterface(REFIID riid, void **ppv)
  1110. {
  1111.     if(riid == IID_IMediaSeeking) {
  1112.         CheckPointer(ppv, E_POINTER);
  1113.         return GetInterface(static_cast<IMediaSeeking *>(this), ppv);
  1114.     }
  1115.     else {
  1116.         return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  1117.     }
  1118. }
  1119.  
  1120.  
  1121. HRESULT CSourceSeeking::IsFormatSupported(const GUID * pFormat)
  1122. {
  1123.     CheckPointer(pFormat, E_POINTER);
  1124.     // only seeking in time (REFERENCE_TIME units) is supported
  1125.     return *pFormat == TIME_FORMAT_MEDIA_TIME ? S_OK : S_FALSE;
  1126. }
  1127.  
  1128. HRESULT CSourceSeeking::QueryPreferredFormat(GUID *pFormat)
  1129. {
  1130.     CheckPointer(pFormat, E_POINTER);
  1131.     *pFormat = TIME_FORMAT_MEDIA_TIME;
  1132.     return S_OK;
  1133. }
  1134.  
  1135. HRESULT CSourceSeeking::SetTimeFormat(const GUID * pFormat)
  1136. {
  1137.     CheckPointer(pFormat, E_POINTER);
  1138.  
  1139.     // nothing to set; just check that it's TIME_FORMAT_TIME
  1140.     return *pFormat == TIME_FORMAT_MEDIA_TIME ? S_OK : E_INVALIDARG;
  1141. }
  1142.  
  1143. HRESULT CSourceSeeking::IsUsingTimeFormat(const GUID * pFormat)
  1144. {
  1145.     CheckPointer(pFormat, E_POINTER);
  1146.     return *pFormat == TIME_FORMAT_MEDIA_TIME ? S_OK : S_FALSE;
  1147. }
  1148.  
  1149. HRESULT CSourceSeeking::GetTimeFormat(GUID *pFormat)
  1150. {
  1151.     CheckPointer(pFormat, E_POINTER);
  1152.     *pFormat = TIME_FORMAT_MEDIA_TIME;
  1153.     return S_OK;
  1154. }
  1155.  
  1156. HRESULT CSourceSeeking::GetDuration(LONGLONG *pDuration)
  1157. {
  1158.     CheckPointer(pDuration, E_POINTER);
  1159.     CAutoLock lock(m_pLock);
  1160.     *pDuration = m_rtDuration;
  1161.     return S_OK;
  1162. }
  1163.  
  1164. HRESULT CSourceSeeking::GetStopPosition(LONGLONG *pStop)
  1165. {
  1166.     CheckPointer(pStop, E_POINTER);
  1167.     CAutoLock lock(m_pLock);
  1168.     *pStop = m_rtStop;
  1169.     return S_OK;
  1170. }
  1171.  
  1172. HRESULT CSourceSeeking::GetCurrentPosition(LONGLONG *pCurrent)
  1173. {
  1174.     // GetCurrentPosition is typically supported only in renderers and
  1175.     // not in source filters.
  1176.     return E_NOTIMPL;
  1177. }
  1178.  
  1179. HRESULT CSourceSeeking::GetCapabilities( DWORD * pCapabilities )
  1180. {
  1181.     CheckPointer(pCapabilities, E_POINTER);
  1182.     *pCapabilities = m_dwSeekingCaps;
  1183.     return S_OK;
  1184. }
  1185.  
  1186. HRESULT CSourceSeeking::CheckCapabilities( DWORD * pCapabilities )
  1187. {
  1188.     CheckPointer(pCapabilities, E_POINTER);
  1189.  
  1190.     // make sure all requested capabilities are in our mask
  1191.     return (~m_dwSeekingCaps & *pCapabilities) ? S_FALSE : S_OK;
  1192. }
  1193.  
  1194. HRESULT CSourceSeeking::ConvertTimeFormat( LONGLONG * pTarget, const GUID * pTargetFormat,
  1195.                            LONGLONG    Source, const GUID * pSourceFormat )
  1196. {
  1197.     CheckPointer(pTarget, E_POINTER);
  1198.     // format guids can be null to indicate current format
  1199.  
  1200.     // since we only support TIME_FORMAT_MEDIA_TIME, we don't really
  1201.     // offer any conversions.
  1202.     if(pTargetFormat == 0 || *pTargetFormat == TIME_FORMAT_MEDIA_TIME)
  1203.     {
  1204.         if(pSourceFormat == 0 || *pSourceFormat == TIME_FORMAT_MEDIA_TIME)
  1205.         {
  1206.             *pTarget = Source;
  1207.             return S_OK;
  1208.         }
  1209.     }
  1210.  
  1211.     return E_INVALIDARG;
  1212. }
  1213.  
  1214.  
  1215. HRESULT CSourceSeeking::SetPositions( LONGLONG * pCurrent,  DWORD CurrentFlags
  1216.                       , LONGLONG * pStop,  DWORD StopFlags )
  1217. {
  1218.     DWORD StopPosBits = StopFlags & AM_SEEKING_PositioningBitsMask;
  1219.     DWORD StartPosBits = CurrentFlags & AM_SEEKING_PositioningBitsMask;
  1220.  
  1221.     if(StopFlags) {
  1222.         CheckPointer(pStop, E_POINTER);
  1223.  
  1224.         // accept only relative, incremental, or absolute positioning
  1225.         if(StopPosBits != StopFlags) {
  1226.             return E_INVALIDARG;
  1227.         }
  1228.     }
  1229.  
  1230.     if(CurrentFlags) {
  1231.         CheckPointer(pCurrent, E_POINTER);
  1232.         if(StartPosBits != AM_SEEKING_AbsolutePositioning &&
  1233.            StartPosBits != AM_SEEKING_RelativePositioning) {
  1234.             return E_INVALIDARG;
  1235.         }
  1236.     }
  1237.  
  1238.  
  1239.     // scope for autolock
  1240.     {
  1241.         CAutoLock lock(m_pLock);
  1242.  
  1243.         // set start position
  1244.         if(StartPosBits == AM_SEEKING_AbsolutePositioning)
  1245.         {
  1246.             m_rtStart = *pCurrent;
  1247.         }
  1248.         else if(StartPosBits == AM_SEEKING_RelativePositioning)
  1249.         {
  1250.             m_rtStart += *pCurrent;
  1251.         }
  1252.  
  1253.         // set stop position
  1254.         if(StopPosBits == AM_SEEKING_AbsolutePositioning)
  1255.         {
  1256.             m_rtStop = *pStop;
  1257.         }
  1258.         else if(StopPosBits == AM_SEEKING_IncrementalPositioning)
  1259.         {
  1260.             m_rtStop = m_rtStart + *pStop;
  1261.         }
  1262.         else if(StopPosBits == AM_SEEKING_RelativePositioning)
  1263.         {
  1264.             m_rtStop = m_rtStop + *pStop;
  1265.         }
  1266.     }
  1267.  
  1268.  
  1269.     HRESULT hr = S_OK;
  1270.     if(SUCCEEDED(hr) && StopPosBits) {
  1271.         hr = ChangeStop();
  1272.     }
  1273.     if(StartPosBits) {
  1274.         hr = ChangeStart();
  1275.     }
  1276.  
  1277.     return hr;
  1278. }
  1279.  
  1280.  
  1281. HRESULT CSourceSeeking::GetPositions( LONGLONG * pCurrent, LONGLONG * pStop )
  1282. {
  1283.     if(pCurrent) {
  1284.         *pCurrent = m_rtStart;
  1285.     }
  1286.     if(pStop) {
  1287.         *pStop = m_rtStop;
  1288.     }
  1289.  
  1290.     return S_OK;;
  1291. }
  1292.  
  1293.  
  1294. HRESULT CSourceSeeking::GetAvailable( LONGLONG * pEarliest, LONGLONG * pLatest )
  1295. {
  1296.     if(pEarliest) {
  1297.         *pEarliest = 0;
  1298.     }
  1299.     if(pLatest) {
  1300.         CAutoLock lock(m_pLock);
  1301.         *pLatest = m_rtDuration;
  1302.     }
  1303.     return S_OK;
  1304. }
  1305.  
  1306. HRESULT CSourceSeeking::SetRate( double dRate)
  1307. {
  1308.     {
  1309.         CAutoLock lock(m_pLock);
  1310.         m_dRateSeeking = dRate;
  1311.     }
  1312.     return ChangeRate();
  1313. }
  1314.  
  1315. HRESULT CSourceSeeking::GetRate( double * pdRate)
  1316. {
  1317.     CheckPointer(pdRate, E_POINTER);
  1318.     CAutoLock lock(m_pLock);
  1319.     *pdRate = m_dRateSeeking;
  1320.     return S_OK;
  1321. }
  1322.  
  1323. HRESULT CSourceSeeking::GetPreroll(LONGLONG *pPreroll)
  1324. {
  1325.     CheckPointer(pPreroll, E_POINTER);
  1326.     *pPreroll = 0;
  1327.     return S_OK;
  1328. }
  1329.  
  1330.  
  1331.  
  1332.  
  1333.  
  1334. // --- CSourcePosition implementation ----------
  1335.  
  1336.  
  1337. CSourcePosition::CSourcePosition(const TCHAR * pName,
  1338.                  LPUNKNOWN pUnk,
  1339.                  HRESULT* phr,
  1340.                  CCritSec * pLock) :
  1341.     CMediaPosition(pName, pUnk),
  1342.     m_pLock(pLock),
  1343.     m_Start(CRefTime((LONGLONG)0))
  1344. {
  1345.     m_Stop = _I64_MAX;
  1346.     m_Rate = 1.0;
  1347. }
  1348.  
  1349.  
  1350. STDMETHODIMP
  1351. CSourcePosition::get_Duration(REFTIME * plength)
  1352. {
  1353.     CheckPointer(plength,E_POINTER);
  1354.     ValidateReadWritePtr(plength,sizeof(REFTIME));
  1355.     CAutoLock lock(m_pLock);
  1356.  
  1357.     *plength = m_Duration;
  1358.     return S_OK;
  1359. }
  1360.  
  1361.  
  1362. STDMETHODIMP
  1363. CSourcePosition::put_CurrentPosition(REFTIME llTime)
  1364. {
  1365.     m_pLock->Lock();
  1366.     m_Start = llTime;
  1367.     m_pLock->Unlock();
  1368.  
  1369.     return ChangeStart();
  1370. }
  1371.  
  1372.  
  1373. STDMETHODIMP
  1374. CSourcePosition::get_StopTime(REFTIME * pllTime)
  1375. {
  1376.     CheckPointer(pllTime,E_POINTER);
  1377.     ValidateReadWritePtr(pllTime,sizeof(REFTIME));
  1378.     CAutoLock lock(m_pLock);
  1379.  
  1380.     *pllTime = m_Stop;
  1381.     return S_OK;
  1382. }
  1383.  
  1384.  
  1385. STDMETHODIMP
  1386. CSourcePosition::put_StopTime(REFTIME llTime)
  1387. {
  1388.     m_pLock->Lock();
  1389.     m_Stop = llTime;
  1390.     m_pLock->Unlock();
  1391.  
  1392.     return ChangeStop();
  1393. }
  1394.  
  1395.  
  1396. STDMETHODIMP
  1397. CSourcePosition::get_PrerollTime(REFTIME * pllTime)
  1398. {
  1399.     CheckPointer(pllTime,E_POINTER);
  1400.     ValidateReadWritePtr(pllTime,sizeof(REFTIME));
  1401.     return E_NOTIMPL;
  1402. }
  1403.  
  1404.  
  1405. STDMETHODIMP
  1406. CSourcePosition::put_PrerollTime(REFTIME llTime)
  1407. {
  1408.     return E_NOTIMPL;
  1409. }
  1410.  
  1411.  
  1412. STDMETHODIMP
  1413. CSourcePosition::get_Rate(double * pdRate)
  1414. {
  1415.     CheckPointer(pdRate,E_POINTER);
  1416.     ValidateReadWritePtr(pdRate,sizeof(double));
  1417.     CAutoLock lock(m_pLock);
  1418.  
  1419.     *pdRate = m_Rate;
  1420.     return S_OK;
  1421. }
  1422.  
  1423.  
  1424. STDMETHODIMP
  1425. CSourcePosition::put_Rate(double dRate)
  1426. {
  1427.     m_pLock->Lock();
  1428.     m_Rate = dRate;
  1429.     m_pLock->Unlock();
  1430.  
  1431.     return ChangeRate();
  1432. }
  1433.  
  1434.  
  1435. // By default we can seek forwards
  1436.  
  1437. STDMETHODIMP
  1438. CSourcePosition::CanSeekForward(LONG *pCanSeekForward)
  1439. {
  1440.     CheckPointer(pCanSeekForward,E_POINTER);
  1441.     *pCanSeekForward = OATRUE;
  1442.     return S_OK;
  1443. }
  1444.  
  1445.  
  1446. // By default we can seek backwards
  1447.  
  1448. STDMETHODIMP
  1449. CSourcePosition::CanSeekBackward(LONG *pCanSeekBackward)
  1450. {
  1451.     CheckPointer(pCanSeekBackward,E_POINTER);
  1452.     *pCanSeekBackward = OATRUE;
  1453.     return S_OK;
  1454. }
  1455.  
  1456.  
  1457. // --- Implementation of CBasicAudio class ----------
  1458.  
  1459.  
  1460. CBasicAudio::CBasicAudio(const TCHAR * pName,LPUNKNOWN punk) :
  1461.     CUnknown(pName, punk)
  1462. {
  1463. }
  1464.  
  1465. // overriden to publicise our interfaces
  1466.  
  1467. STDMETHODIMP
  1468. CBasicAudio::NonDelegatingQueryInterface(REFIID riid, void **ppv)
  1469. {
  1470.     ValidateReadWritePtr(ppv,sizeof(PVOID));
  1471.     if (riid == IID_IBasicAudio) {
  1472.     return GetInterface( (IBasicAudio *) this, ppv);
  1473.     } else {
  1474.     return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  1475.     }
  1476. }
  1477.  
  1478.  
  1479. STDMETHODIMP
  1480. CBasicAudio::GetTypeInfoCount(UINT * pctinfo)
  1481. {
  1482.     return m_basedisp.GetTypeInfoCount(pctinfo);
  1483. }
  1484.  
  1485.  
  1486. STDMETHODIMP
  1487. CBasicAudio::GetTypeInfo(
  1488.   UINT itinfo,
  1489.   LCID lcid,
  1490.   ITypeInfo ** pptinfo)
  1491. {
  1492.     return m_basedisp.GetTypeInfo(
  1493.         IID_IBasicAudio,
  1494.         itinfo,
  1495.         lcid,
  1496.         pptinfo);
  1497. }
  1498.  
  1499.  
  1500. STDMETHODIMP
  1501. CBasicAudio::GetIDsOfNames(
  1502.   REFIID riid,
  1503.   OLECHAR  ** rgszNames,
  1504.   UINT cNames,
  1505.   LCID lcid,
  1506.   DISPID * rgdispid)
  1507. {
  1508.     return m_basedisp.GetIDsOfNames(
  1509.             IID_IBasicAudio,
  1510.             rgszNames,
  1511.             cNames,
  1512.             lcid,
  1513.             rgdispid);
  1514. }
  1515.  
  1516.  
  1517. STDMETHODIMP
  1518. CBasicAudio::Invoke(
  1519.   DISPID dispidMember,
  1520.   REFIID riid,
  1521.   LCID lcid,
  1522.   WORD wFlags,
  1523.   DISPPARAMS * pdispparams,
  1524.   VARIANT * pvarResult,
  1525.   EXCEPINFO * pexcepinfo,
  1526.   UINT * puArgErr)
  1527. {
  1528.     // this parameter is a dead leftover from an earlier interface
  1529.     if (IID_NULL != riid) {
  1530.     return DISP_E_UNKNOWNINTERFACE;
  1531.     }
  1532.  
  1533.     ITypeInfo * pti;
  1534.     HRESULT hr = GetTypeInfo(0, lcid, &pti);
  1535.  
  1536.     if (FAILED(hr)) {
  1537.     return hr;
  1538.     }
  1539.  
  1540.     hr = pti->Invoke(
  1541.         (IBasicAudio *)this,
  1542.         dispidMember,
  1543.         wFlags,
  1544.         pdispparams,
  1545.         pvarResult,
  1546.         pexcepinfo,
  1547.         puArgErr);
  1548.  
  1549.     pti->Release();
  1550.     return hr;
  1551. }
  1552.  
  1553.  
  1554. // --- IVideoWindow implementation ----------
  1555.  
  1556. CBaseVideoWindow::CBaseVideoWindow(const TCHAR * pName,LPUNKNOWN punk) :
  1557.     CUnknown(pName, punk)
  1558. {
  1559. }
  1560.  
  1561.  
  1562. // overriden to publicise our interfaces
  1563.  
  1564. STDMETHODIMP
  1565. CBaseVideoWindow::NonDelegatingQueryInterface(REFIID riid, void **ppv)
  1566. {
  1567.     ValidateReadWritePtr(ppv,sizeof(PVOID));
  1568.     if (riid == IID_IVideoWindow) {
  1569.     return GetInterface( (IVideoWindow *) this, ppv);
  1570.     } else {
  1571.     return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  1572.     }
  1573. }
  1574.  
  1575.  
  1576. STDMETHODIMP
  1577. CBaseVideoWindow::GetTypeInfoCount(UINT * pctinfo)
  1578. {
  1579.     return m_basedisp.GetTypeInfoCount(pctinfo);
  1580. }
  1581.  
  1582.  
  1583. STDMETHODIMP
  1584. CBaseVideoWindow::GetTypeInfo(
  1585.   UINT itinfo,
  1586.   LCID lcid,
  1587.   ITypeInfo ** pptinfo)
  1588. {
  1589.     return m_basedisp.GetTypeInfo(
  1590.         IID_IVideoWindow,
  1591.         itinfo,
  1592.         lcid,
  1593.         pptinfo);
  1594. }
  1595.  
  1596.  
  1597. STDMETHODIMP
  1598. CBaseVideoWindow::GetIDsOfNames(
  1599.   REFIID riid,
  1600.   OLECHAR  ** rgszNames,
  1601.   UINT cNames,
  1602.   LCID lcid,
  1603.   DISPID * rgdispid)
  1604. {
  1605.     return m_basedisp.GetIDsOfNames(
  1606.             IID_IVideoWindow,
  1607.             rgszNames,
  1608.             cNames,
  1609.             lcid,
  1610.             rgdispid);
  1611. }
  1612.  
  1613.  
  1614. STDMETHODIMP
  1615. CBaseVideoWindow::Invoke(
  1616.   DISPID dispidMember,
  1617.   REFIID riid,
  1618.   LCID lcid,
  1619.   WORD wFlags,
  1620.   DISPPARAMS * pdispparams,
  1621.   VARIANT * pvarResult,
  1622.   EXCEPINFO * pexcepinfo,
  1623.   UINT * puArgErr)
  1624. {
  1625.     // this parameter is a dead leftover from an earlier interface
  1626.     if (IID_NULL != riid) {
  1627.     return DISP_E_UNKNOWNINTERFACE;
  1628.     }
  1629.  
  1630.     ITypeInfo * pti;
  1631.     HRESULT hr = GetTypeInfo(0, lcid, &pti);
  1632.  
  1633.     if (FAILED(hr)) {
  1634.     return hr;
  1635.     }
  1636.  
  1637.     hr = pti->Invoke(
  1638.         (IVideoWindow *)this,
  1639.         dispidMember,
  1640.         wFlags,
  1641.         pdispparams,
  1642.         pvarResult,
  1643.         pexcepinfo,
  1644.         puArgErr);
  1645.  
  1646.     pti->Release();
  1647.     return hr;
  1648. }
  1649.  
  1650.  
  1651. // --- IBasicVideo implementation ----------
  1652.  
  1653.  
  1654. CBaseBasicVideo::CBaseBasicVideo(const TCHAR * pName,LPUNKNOWN punk) :
  1655.     CUnknown(pName, punk)
  1656. {
  1657. }
  1658.  
  1659.  
  1660. // overriden to publicise our interfaces
  1661.  
  1662. STDMETHODIMP
  1663. CBaseBasicVideo::NonDelegatingQueryInterface(REFIID riid, void **ppv)
  1664. {
  1665.     ValidateReadWritePtr(ppv,sizeof(PVOID));
  1666.     if (riid == IID_IBasicVideo || riid == IID_IBasicVideo2) {
  1667.     return GetInterface( static_cast<IBasicVideo2 *>(this), ppv);
  1668.     } else {
  1669.     return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  1670.     }
  1671. }
  1672.  
  1673.  
  1674. STDMETHODIMP
  1675. CBaseBasicVideo::GetTypeInfoCount(UINT * pctinfo)
  1676. {
  1677.     return m_basedisp.GetTypeInfoCount(pctinfo);
  1678. }
  1679.  
  1680.  
  1681. STDMETHODIMP
  1682. CBaseBasicVideo::GetTypeInfo(
  1683.   UINT itinfo,
  1684.   LCID lcid,
  1685.   ITypeInfo ** pptinfo)
  1686. {
  1687.     return m_basedisp.GetTypeInfo(
  1688.         IID_IBasicVideo,
  1689.         itinfo,
  1690.         lcid,
  1691.         pptinfo);
  1692. }
  1693.  
  1694.  
  1695. STDMETHODIMP
  1696. CBaseBasicVideo::GetIDsOfNames(
  1697.   REFIID riid,
  1698.   OLECHAR  ** rgszNames,
  1699.   UINT cNames,
  1700.   LCID lcid,
  1701.   DISPID * rgdispid)
  1702. {
  1703.     return m_basedisp.GetIDsOfNames(
  1704.             IID_IBasicVideo,
  1705.             rgszNames,
  1706.             cNames,
  1707.             lcid,
  1708.             rgdispid);
  1709. }
  1710.  
  1711.  
  1712. STDMETHODIMP
  1713. CBaseBasicVideo::Invoke(
  1714.   DISPID dispidMember,
  1715.   REFIID riid,
  1716.   LCID lcid,
  1717.   WORD wFlags,
  1718.   DISPPARAMS * pdispparams,
  1719.   VARIANT * pvarResult,
  1720.   EXCEPINFO * pexcepinfo,
  1721.   UINT * puArgErr)
  1722. {
  1723.     // this parameter is a dead leftover from an earlier interface
  1724.     if (IID_NULL != riid) {
  1725.     return DISP_E_UNKNOWNINTERFACE;
  1726.     }
  1727.  
  1728.     ITypeInfo * pti;
  1729.     HRESULT hr = GetTypeInfo(0, lcid, &pti);
  1730.  
  1731.     if (FAILED(hr)) {
  1732.     return hr;
  1733.     }
  1734.  
  1735.     hr = pti->Invoke(
  1736.         (IBasicVideo *)this,
  1737.         dispidMember,
  1738.         wFlags,
  1739.         pdispparams,
  1740.         pvarResult,
  1741.         pexcepinfo,
  1742.         puArgErr);
  1743.  
  1744.     pti->Release();
  1745.     return hr;
  1746. }
  1747.  
  1748.  
  1749. // --- Implementation of Deferred Commands ----------
  1750.  
  1751.  
  1752. CDispParams::CDispParams(UINT nArgs, VARIANT* pArgs, HRESULT *phr)
  1753. {
  1754.    cNamedArgs = 0;
  1755.    rgdispidNamedArgs = NULL;
  1756.    cArgs = nArgs;
  1757.  
  1758.     if (cArgs) {
  1759.     rgvarg = new VARIANT[cArgs];
  1760.         if (NULL == rgvarg) {
  1761.             cArgs = 0;
  1762.             if (phr) {
  1763.                 *phr = E_OUTOFMEMORY;
  1764.             }
  1765.             return;
  1766.         }
  1767.  
  1768.     for (UINT i = 0; i < cArgs; i++) {
  1769.  
  1770.         VARIANT * pDest = &rgvarg[i];
  1771.         VARIANT * pSrc = &pArgs[i];
  1772.  
  1773.         pDest->vt = pSrc->vt;
  1774.         switch(pDest->vt) {
  1775.  
  1776.         case VT_I4:
  1777.         pDest->lVal = pSrc->lVal;
  1778.         break;
  1779.  
  1780.         case VT_UI1:
  1781.         pDest->bVal = pSrc->bVal;
  1782.         break;
  1783.  
  1784.         case VT_I2:
  1785.         pDest->iVal = pSrc->iVal;
  1786.         break;
  1787.  
  1788.         case VT_R4:
  1789.         pDest->fltVal = pSrc->fltVal;
  1790.         break;
  1791.  
  1792.         case VT_R8:
  1793.         pDest->dblVal = pSrc->dblVal;
  1794.         break;
  1795.  
  1796.         case VT_BOOL:
  1797.         pDest->boolVal = pSrc->boolVal;
  1798.         break;
  1799.  
  1800.         case VT_ERROR:
  1801.         pDest->scode = pSrc->scode;
  1802.         break;
  1803.  
  1804.         case VT_CY:
  1805.         pDest->cyVal = pSrc->cyVal;
  1806.         break;
  1807.  
  1808.         case VT_DATE:
  1809.         pDest->date = pSrc->date;
  1810.         break;
  1811.  
  1812.         case VT_BSTR:
  1813.         if (pSrc->bstrVal == NULL) {
  1814.             pDest->bstrVal = NULL;
  1815.         } else {
  1816.  
  1817.             // a BSTR is a WORD followed by a UNICODE string.
  1818.             // the pointer points just after the WORD
  1819.  
  1820.             WORD len = * (WORD*) (pSrc->bstrVal - (sizeof(WORD) / sizeof(OLECHAR)));
  1821.             OLECHAR* pch = new OLECHAR[len + (sizeof(WORD)/sizeof(OLECHAR))];
  1822.                     if (pch) {
  1823.                 WORD *pui = (WORD*)pch;
  1824.                 *pui = len;
  1825.                      pDest->bstrVal = pch + (sizeof(WORD)/sizeof(OLECHAR));
  1826.                  CopyMemory(pDest->bstrVal, pSrc->bstrVal, len*sizeof(OLECHAR));
  1827.                     } else {
  1828.                         cArgs = i;
  1829.                         if (phr) {
  1830.                             *phr = E_OUTOFMEMORY;
  1831.                         }
  1832.                     }
  1833.         }
  1834.         pDest->bstrVal = pSrc->bstrVal;
  1835.         break;
  1836.  
  1837.         case VT_UNKNOWN:
  1838.         pDest->punkVal = pSrc->punkVal;
  1839.         pDest->punkVal->AddRef();
  1840.         break;
  1841.  
  1842.         case VT_DISPATCH:
  1843.         pDest->pdispVal = pSrc->pdispVal;
  1844.         pDest->pdispVal->AddRef();
  1845.         break;
  1846.  
  1847.         default:
  1848.         // a type we haven't got round to adding yet!
  1849.         ASSERT(0);
  1850.         break;
  1851.         }
  1852.     }
  1853.  
  1854.     } else {
  1855.     rgvarg = NULL;
  1856.     }
  1857.  
  1858. }
  1859.  
  1860.  
  1861. CDispParams::~CDispParams()
  1862. {
  1863.     for (UINT i = 0; i < cArgs; i++) {
  1864.     switch(rgvarg[i].vt) {
  1865.     case VT_BSTR:
  1866.         if (rgvarg[i].bstrVal != NULL) {
  1867.         OLECHAR * pch = rgvarg[i].bstrVal - (sizeof(WORD)/sizeof(OLECHAR));
  1868.         delete pch;
  1869.         }
  1870.         break;
  1871.  
  1872.     case VT_UNKNOWN:
  1873.         rgvarg[i].punkVal->Release();
  1874.         break;
  1875.  
  1876.     case VT_DISPATCH:
  1877.         rgvarg[i].pdispVal->Release();
  1878.         break;
  1879.     }
  1880.     }
  1881.     delete[] rgvarg;
  1882. }
  1883.  
  1884.  
  1885. // lifetime is controlled by refcounts (see defer.h)
  1886.  
  1887. CDeferredCommand::CDeferredCommand(
  1888.     CCmdQueue * pQ,
  1889.     LPUNKNOWN    pUnk,
  1890.     HRESULT *    phr,
  1891.     LPUNKNOWN    pUnkExecutor,
  1892.     REFTIME    time,
  1893.     GUID*    iid,
  1894.     long    dispidMethod,
  1895.     short    wFlags,
  1896.     long    nArgs,
  1897.     VARIANT*    pDispParams,
  1898.     VARIANT*    pvarResult,
  1899.     short*    puArgErr,
  1900.     BOOL    bStream
  1901.     ) :
  1902.     CUnknown(NAME("DeferredCommand"), pUnk),
  1903.     m_pQueue(pQ),
  1904.     m_pUnk(pUnkExecutor),
  1905.     m_iid(iid),
  1906.     m_dispidMethod(dispidMethod),
  1907.     m_wFlags(wFlags),
  1908.     m_DispParams(nArgs, pDispParams, phr),
  1909.     m_pvarResult(pvarResult),
  1910.     m_bStream(bStream),
  1911.     m_hrResult(E_ABORT)
  1912.  
  1913. {
  1914.     // convert REFTIME to REFERENCE_TIME
  1915.     COARefTime convertor(time);
  1916.     m_time = convertor;
  1917.  
  1918.     // no check of time validity - it's ok to queue a command that's
  1919.     // already late
  1920.  
  1921.     // check iid is supportable on pUnk by QueryInterface for it
  1922.     IUnknown * pInterface;
  1923.     HRESULT hr = m_pUnk->QueryInterface(GetIID(), (void**) &pInterface);
  1924.     if (FAILED(hr)) {
  1925.     *phr = hr;
  1926.     return;
  1927.     }
  1928.     pInterface->Release();
  1929.  
  1930.  
  1931.     // !!! check dispidMethod and param/return types using typelib
  1932.     ITypeInfo *pti;
  1933.     hr = m_Dispatch.GetTypeInfo(*iid, 0, 0, &pti);
  1934.     if (FAILED(hr)) {
  1935.     *phr = hr;
  1936.     return;
  1937.     }
  1938.     // !!! some sort of ITypeInfo validity check here
  1939.     pti->Release();
  1940.  
  1941.  
  1942.     // Fix up the dispid for put and get
  1943.     if (wFlags == DISPATCH_PROPERTYPUT) {
  1944.         m_DispParams.cNamedArgs = 1;
  1945.         m_DispId = DISPID_PROPERTYPUT;
  1946.         m_DispParams.rgdispidNamedArgs = &m_DispId;
  1947.     }
  1948.  
  1949.     // all checks ok - add to queue
  1950.     hr = pQ->Insert(this);
  1951.     if (FAILED(hr)) {
  1952.     *phr = hr;
  1953.     }
  1954. }
  1955.  
  1956.  
  1957. // refcounts are held by caller of InvokeAt... and by list. So if
  1958. // we get here, we can't be on the list
  1959.  
  1960. #if 0
  1961. CDeferredCommand::~CDeferredCommand()
  1962. {
  1963.     // this assert is invalid since if the queue is deleted while we are
  1964.     // still on the queue, we will have been removed by the queue and this
  1965.     // m_pQueue will not have been modified.
  1966.     // ASSERT(m_pQueue == NULL);
  1967.  
  1968.     // we don't hold a ref count on pUnk, which is the object that should
  1969.     // execute the command.
  1970.     // This is because there would otherwise be a circular refcount problem
  1971.     // since pUnk probably owns the CmdQueue object that has a refcount
  1972.     // on us.
  1973.     // The lifetime of pUnk is guaranteed by it being part of, or lifetime
  1974.     // controlled by, our parent object. As long as we are on the list, pUnk
  1975.     // must be valid. Once we are off the list, we do not use pUnk.
  1976.  
  1977. }
  1978. #endif
  1979.  
  1980.  
  1981. // overriden to publicise our interfaces
  1982.  
  1983. STDMETHODIMP
  1984. CDeferredCommand::NonDelegatingQueryInterface(REFIID riid, void **ppv)
  1985. {
  1986.     ValidateReadWritePtr(ppv,sizeof(PVOID));
  1987.     if (riid == IID_IDeferredCommand) {
  1988.     return GetInterface( (IDeferredCommand *) this, ppv);
  1989.     } else {
  1990.     return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  1991.     }
  1992. }
  1993.  
  1994.  
  1995. // remove from q. this will reduce the refcount by one (since the q
  1996. // holds a count) but can't make us go away since he must have a
  1997. // refcount in order to call this method.
  1998.  
  1999. STDMETHODIMP
  2000. CDeferredCommand::Cancel()
  2001. {
  2002.     if (m_pQueue == NULL) {
  2003.     return VFW_E_ALREADY_CANCELLED;
  2004.     }
  2005.  
  2006.     HRESULT hr = m_pQueue->Remove(this);
  2007.     if (FAILED(hr)) {
  2008.     return hr;
  2009.     }
  2010.  
  2011.     m_pQueue = NULL;
  2012.     return S_OK;
  2013. }
  2014.  
  2015.  
  2016. STDMETHODIMP
  2017. CDeferredCommand::Confidence(LONG* pConfidence)
  2018. {
  2019.     return E_NOTIMPL;
  2020. }
  2021.  
  2022.  
  2023. STDMETHODIMP
  2024. CDeferredCommand::GetHResult(HRESULT * phrResult)
  2025. {
  2026.     CheckPointer(phrResult,E_POINTER);
  2027.     ValidateReadWritePtr(phrResult,sizeof(HRESULT));
  2028.  
  2029.     if (m_pQueue != NULL) {
  2030.     return E_ABORT;
  2031.     }
  2032.     *phrResult = m_hrResult;
  2033.     return S_OK;
  2034. }
  2035.  
  2036.  
  2037. // set the time to be a new time (checking that it is valid) and
  2038. // then requeue
  2039.  
  2040. STDMETHODIMP
  2041. CDeferredCommand::Postpone(REFTIME newtime)
  2042. {
  2043.  
  2044.     // check that this time is not past
  2045.     // convert REFTIME to REFERENCE_TIME
  2046.     COARefTime convertor(newtime);
  2047.  
  2048.     // check that the time has not passed
  2049.     if (m_pQueue->CheckTime(convertor, IsStreamTime())) {
  2050.     return VFW_E_TIME_ALREADY_PASSED;
  2051.     }
  2052.  
  2053.     // extract from list
  2054.     HRESULT hr = m_pQueue->Remove(this);
  2055.     if (FAILED(hr)) {
  2056.     return hr;
  2057.     }
  2058.  
  2059.     // change time
  2060.     m_time = convertor;
  2061.  
  2062.     // requeue
  2063.     hr = m_pQueue->Insert(this);
  2064.  
  2065.     return hr;
  2066. }
  2067.  
  2068.  
  2069. HRESULT
  2070. CDeferredCommand::Invoke()
  2071. {
  2072.     // check that we are still outstanding
  2073.     if (m_pQueue == NULL) {
  2074.     return VFW_E_ALREADY_CANCELLED;
  2075.     }
  2076.  
  2077.     // get the type info
  2078.     ITypeInfo* pti;
  2079.     HRESULT hr = m_Dispatch.GetTypeInfo(GetIID(), 0, 0, &pti);
  2080.     if (FAILED(hr)) {
  2081.     return hr;
  2082.     }
  2083.  
  2084.     // qi for the expected interface and then invoke it. Note that we have to
  2085.     // treat the returned interface as IUnknown since we don't know its type.
  2086.     IUnknown* pInterface;
  2087.  
  2088.     hr = m_pUnk->QueryInterface(GetIID(), (void**) &pInterface);
  2089.     if (FAILED(hr)) {
  2090.     pti->Release();
  2091.     return hr;
  2092.     }
  2093.  
  2094.     EXCEPINFO expinfo;
  2095.     UINT uArgErr;
  2096.     m_hrResult = pti->Invoke(
  2097.     pInterface,
  2098.     GetMethod(),
  2099.     GetFlags(),
  2100.     GetParams(),
  2101.     GetResult(),
  2102.     &expinfo,
  2103.     &uArgErr);
  2104.  
  2105.     // release the interface we QI'd for
  2106.     pInterface->Release();
  2107.     pti->Release();
  2108.  
  2109.  
  2110.     // remove from list whether or not successful
  2111.     // or we loop indefinitely
  2112.     hr = m_pQueue->Remove(this);
  2113.     m_pQueue = NULL;
  2114.     return hr;
  2115. }
  2116.  
  2117.  
  2118.  
  2119. // --- CCmdQueue methods ----------
  2120.  
  2121.  
  2122. CCmdQueue::CCmdQueue() :
  2123.     m_listPresentation(NAME("Presentation time command list")),
  2124.     m_listStream(NAME("Stream time command list")),
  2125.     m_evDue(TRUE),    // manual reset
  2126.     m_dwAdvise(0),
  2127.     m_pClock(NULL),
  2128.     m_bRunning(FALSE)
  2129. {
  2130. }
  2131.  
  2132.  
  2133. CCmdQueue::~CCmdQueue()
  2134. {
  2135.     // empty all our lists
  2136.  
  2137.     // we hold a refcount on each, so traverse and Release each
  2138.     // entry then RemoveAll to empty the list
  2139.     POSITION pos = m_listPresentation.GetHeadPosition();
  2140.  
  2141.     while(pos) {
  2142.     CDeferredCommand* pCmd = m_listPresentation.GetNext(pos);
  2143.     pCmd->Release();
  2144.     }
  2145.     m_listPresentation.RemoveAll();
  2146.  
  2147.     pos = m_listStream.GetHeadPosition();
  2148.  
  2149.     while(pos) {
  2150.     CDeferredCommand* pCmd = m_listStream.GetNext(pos);
  2151.     pCmd->Release();
  2152.     }
  2153.     m_listStream.RemoveAll();
  2154.  
  2155.     if (m_pClock) {
  2156.     if (m_dwAdvise) {
  2157.         m_pClock->Unadvise(m_dwAdvise);
  2158.         m_dwAdvise = 0;
  2159.     }
  2160.     m_pClock->Release();
  2161.     }
  2162. }
  2163.  
  2164.  
  2165. // returns a new CDeferredCommand object that will be initialised with
  2166. // the parameters and will be added to the queue during construction.
  2167. // returns S_OK if successfully created otherwise an error and
  2168. // no object has been queued.
  2169.  
  2170. HRESULT
  2171. CCmdQueue::New(
  2172.     CDeferredCommand **ppCmd,
  2173.     LPUNKNOWN    pUnk,        // this object will execute command
  2174.     REFTIME    time,
  2175.     GUID*    iid,
  2176.     long    dispidMethod,
  2177.     short    wFlags,
  2178.     long    cArgs,
  2179.     VARIANT*    pDispParams,
  2180.     VARIANT*    pvarResult,
  2181.     short*    puArgErr,
  2182.     BOOL    bStream
  2183. )
  2184. {
  2185.     CAutoLock lock(&m_Lock);
  2186.  
  2187.     HRESULT hr = S_OK;
  2188.     *ppCmd = NULL;
  2189.  
  2190.     CDeferredCommand* pCmd;
  2191.     pCmd = new CDeferredCommand(
  2192.             this,
  2193.             NULL,        // not aggregated
  2194.             &hr,
  2195.             pUnk,        // this guy will execute
  2196.             time,
  2197.             iid,
  2198.             dispidMethod,
  2199.             wFlags,
  2200.             cArgs,
  2201.             pDispParams,
  2202.             pvarResult,
  2203.             puArgErr,
  2204.             bStream);
  2205.  
  2206.     if (pCmd == NULL) {
  2207.     hr = E_OUTOFMEMORY;
  2208.     } else {
  2209.     *ppCmd = pCmd;
  2210.     }
  2211.     return hr;
  2212. }
  2213.  
  2214.  
  2215. HRESULT
  2216. CCmdQueue::Insert(CDeferredCommand* pCmd)
  2217. {
  2218.     CAutoLock lock(&m_Lock);
  2219.  
  2220.     // addref the item
  2221.     pCmd->AddRef();
  2222.  
  2223.     CGenericList<CDeferredCommand> * pList;
  2224.     if (pCmd->IsStreamTime()) {
  2225.     pList = &m_listStream;
  2226.     } else {
  2227.     pList = &m_listPresentation;
  2228.     }
  2229.     POSITION pos = pList->GetHeadPosition();
  2230.  
  2231.     // seek past all items that are before us
  2232.     while (pos &&
  2233.     (pList->Get(pos)->GetTime() <= pCmd->GetTime())) {
  2234.  
  2235.     pList->GetNext(pos);
  2236.     }
  2237.  
  2238.     // now at end of list or in front of items that come later
  2239.     if (!pos) {
  2240.     pList->AddTail(pCmd);
  2241.     } else {
  2242.     pList->AddBefore(pos, pCmd);
  2243.     }
  2244.  
  2245.     SetTimeAdvise();
  2246.     return S_OK;
  2247. }
  2248.  
  2249.  
  2250. HRESULT
  2251. CCmdQueue::Remove(CDeferredCommand* pCmd)
  2252. {
  2253.     CAutoLock lock(&m_Lock);
  2254.     HRESULT hr = S_OK;
  2255.  
  2256.     CGenericList<CDeferredCommand> * pList;
  2257.     if (pCmd->IsStreamTime()) {
  2258.     pList = &m_listStream;
  2259.     } else {
  2260.     pList = &m_listPresentation;
  2261.     }
  2262.     POSITION pos = pList->GetHeadPosition();
  2263.  
  2264.     // traverse the list
  2265.     while (pos && (pList->Get(pos) != pCmd)) {
  2266.     pList->GetNext(pos);
  2267.     }
  2268.  
  2269.     // did we drop off the end?
  2270.     if (!pos) {
  2271.     hr = VFW_E_NOT_FOUND;
  2272.     } else {
  2273.  
  2274.     // found it - now take off list
  2275.     pList->Remove(pos);
  2276.  
  2277.     // Insert did an AddRef, so release it
  2278.     pCmd->Release();
  2279.  
  2280.     // check that timer request is still for earliest time
  2281.     SetTimeAdvise();
  2282.     }
  2283.     return hr;
  2284. }
  2285.  
  2286.  
  2287. // set the clock used for timing
  2288.  
  2289. HRESULT
  2290. CCmdQueue::SetSyncSource(IReferenceClock* pClock)
  2291. {
  2292.     CAutoLock lock(&m_Lock);
  2293.  
  2294.     // addref the new clock first in case they are the same
  2295.     if (pClock) {
  2296.     pClock->AddRef();
  2297.     }
  2298.  
  2299.     // kill any advise on the old clock
  2300.     if (m_pClock) {
  2301.     if (m_dwAdvise) {
  2302.         m_pClock->Unadvise(m_dwAdvise);
  2303.         m_dwAdvise = 0;
  2304.     }
  2305.     m_pClock->Release();
  2306.     }
  2307.     m_pClock = pClock;
  2308.  
  2309.     // set up a new advise
  2310.     SetTimeAdvise();
  2311.     return S_OK;
  2312. }
  2313.  
  2314.  
  2315. // set up a timer event with the reference clock
  2316.  
  2317. void
  2318. CCmdQueue::SetTimeAdvise(void)
  2319. {
  2320.     // make sure we have a clock to use
  2321.     if (!m_pClock) {
  2322.     return;
  2323.     }
  2324.  
  2325.     // reset the event whenever we are requesting a new signal
  2326.     m_evDue.Reset();
  2327.  
  2328.     // time 0 is earliest
  2329.     CRefTime current;
  2330.  
  2331.     // find the earliest presentation time
  2332.     if (m_listPresentation.GetCount() > 0) {
  2333.  
  2334.     POSITION pos = m_listPresentation.GetHeadPosition();
  2335.     current = m_listPresentation.Get(pos)->GetTime();
  2336.     }
  2337.  
  2338.     // if we're running, check the stream times too
  2339.     if (m_bRunning) {
  2340.  
  2341.     CRefTime t;
  2342.  
  2343.     if (m_listStream.GetCount() > 0) {
  2344.  
  2345.         POSITION pos = m_listStream.GetHeadPosition();
  2346.         t = m_listStream.Get(pos)->GetTime();
  2347.  
  2348.         // add on stream time offset to get presentation time
  2349.         t += m_StreamTimeOffset;
  2350.  
  2351.         // is this earlier?
  2352.         if ((current == TimeZero) || (t < current)) {
  2353.         current = t;
  2354.         }
  2355.     }
  2356.     }
  2357.  
  2358.     // need to change?
  2359.     if ((current > TimeZero) && (current != m_tCurrentAdvise)) {
  2360.     if (m_dwAdvise) {
  2361.         m_pClock->Unadvise(m_dwAdvise);
  2362.         // reset the event whenever we are requesting a new signal
  2363.         m_evDue.Reset();
  2364.     }
  2365.  
  2366.     // ask for time advice - the first two params are either
  2367.     // stream time offset and stream time or
  2368.     // presentation time and 0. we always use the latter
  2369.     HRESULT hr = m_pClock->AdviseTime(
  2370.             (REFERENCE_TIME)current,
  2371.             TimeZero,
  2372.             (HEVENT) HANDLE(m_evDue),
  2373.             &m_dwAdvise);
  2374.  
  2375.     ASSERT(SUCCEEDED(hr));
  2376.     m_tCurrentAdvise = current;
  2377.     }
  2378. }
  2379.  
  2380.  
  2381. // switch to run mode. Streamtime to Presentation time mapping known.
  2382.  
  2383. HRESULT
  2384. CCmdQueue::Run(REFERENCE_TIME tStreamTimeOffset)
  2385. {
  2386.     CAutoLock lock(&m_Lock);
  2387.  
  2388.     m_StreamTimeOffset = tStreamTimeOffset;
  2389.     m_bRunning = TRUE;
  2390.  
  2391.     // ensure advise is accurate
  2392.     SetTimeAdvise();
  2393.     return S_OK;
  2394. }
  2395.  
  2396.  
  2397. // switch to Stopped or Paused mode. Time mapping not known.
  2398.  
  2399. HRESULT
  2400. CCmdQueue::EndRun()
  2401. {
  2402.     CAutoLock lock(&m_Lock);
  2403.  
  2404.     m_bRunning = FALSE;
  2405.  
  2406.     // check timer setting - stream times
  2407.     SetTimeAdvise();
  2408.     return S_OK;
  2409. }
  2410.  
  2411.  
  2412. // return a pointer to the next due command. Blocks for msTimeout
  2413. // milliseconds until there is a due command.
  2414. // Stream-time commands will only become due between Run and Endrun calls.
  2415. // The command remains queued until invoked or cancelled.
  2416. // Returns E_ABORT if timeout occurs, otherwise S_OK (or other error).
  2417. //
  2418. // returns an AddRef'd object
  2419.  
  2420. HRESULT
  2421. CCmdQueue::GetDueCommand(CDeferredCommand ** ppCmd, long msTimeout)
  2422. {
  2423.     // loop until we timeout or find a due command
  2424.     for (;;) {
  2425.  
  2426.     {
  2427.         CAutoLock lock(&m_Lock);
  2428.  
  2429.  
  2430.         // find the earliest command
  2431.         CDeferredCommand * pCmd = NULL;
  2432.  
  2433.         // check the presentation time and the
  2434.         // stream time list to find the earliest
  2435.  
  2436.         if (m_listPresentation.GetCount() > 0) {
  2437.         POSITION pos = m_listPresentation.GetHeadPosition();
  2438.         pCmd = m_listPresentation.Get(pos);
  2439.         }
  2440.  
  2441.         if (m_bRunning && (m_listStream.GetCount() > 0)) {
  2442.         POSITION pos = m_listStream.GetHeadPosition();
  2443.         CDeferredCommand* pStrm = m_listStream.Get(pos);
  2444.  
  2445.         CRefTime t = pStrm->GetTime() + m_StreamTimeOffset;
  2446.         if (!pCmd || (t < pCmd->GetTime())) {
  2447.             pCmd = pStrm;
  2448.         }
  2449.         }
  2450.  
  2451.         //    if we have found one, is it due?
  2452.         if (pCmd) {
  2453.         if (CheckTime(pCmd->GetTime(), pCmd->IsStreamTime())) {
  2454.  
  2455.             // yes it's due - addref it
  2456.             pCmd->AddRef();
  2457.             *ppCmd = pCmd;
  2458.             return S_OK;
  2459.         }
  2460.         }
  2461.     }
  2462.  
  2463.     // block until the advise is signalled
  2464.     if (WaitForSingleObject(m_evDue, msTimeout) != WAIT_OBJECT_0) {
  2465.         return E_ABORT;
  2466.     }
  2467.     }
  2468. }
  2469.  
  2470.  
  2471. // return a pointer to a command that will be due for a given time.
  2472. // Pass in a stream time here. The stream time offset will be passed
  2473. // in via the Run method.
  2474. // Commands remain queued until invoked or cancelled.
  2475. // This method will not block. It will report E_ABORT if there are no
  2476. // commands due yet.
  2477. //
  2478. // returns an AddRef'd object
  2479.  
  2480. HRESULT
  2481. CCmdQueue::GetCommandDueFor(REFERENCE_TIME rtStream, CDeferredCommand**ppCmd)
  2482. {
  2483.     CAutoLock lock(&m_Lock);
  2484.  
  2485.     CRefTime tStream(rtStream);
  2486.  
  2487.     // find the earliest stream and presentation time commands
  2488.     CDeferredCommand* pStream = NULL;
  2489.     if (m_listStream.GetCount() > 0) {
  2490.     POSITION pos = m_listStream.GetHeadPosition();
  2491.     pStream = m_listStream.Get(pos);
  2492.     }
  2493.     CDeferredCommand* pPresent = NULL;
  2494.     if (m_listPresentation.GetCount() > 0) {
  2495.     POSITION pos = m_listPresentation.GetHeadPosition();
  2496.     pPresent = m_listPresentation.Get(pos);
  2497.     }
  2498.  
  2499.     // is there a presentation time that has passed already
  2500.     if (pPresent && CheckTime(pPresent->GetTime(), FALSE)) {
  2501.     pPresent->AddRef();
  2502.     *ppCmd = pPresent;
  2503.     return S_OK;
  2504.     }
  2505.  
  2506.     // is there a stream time command due before this stream time
  2507.     if (pStream && (pStream->GetTime() <= tStream)) {
  2508.     pPresent->AddRef();
  2509.     *ppCmd = pStream;
  2510.     return S_OK;
  2511.     }
  2512.  
  2513.     // if we are running, we can map presentation times to
  2514.     // stream time. In this case, is there a presentation time command
  2515.     // that will be due before this stream time is presented?
  2516.     if (m_bRunning && pPresent) {
  2517.  
  2518.     // this stream time will appear at...
  2519.     tStream += m_StreamTimeOffset;
  2520.  
  2521.     // due before that?
  2522.     if (pPresent->GetTime() <= tStream) {
  2523.         *ppCmd = pPresent;
  2524.         return S_OK;
  2525.     }
  2526.     }
  2527.  
  2528.     // no commands due yet
  2529.     return VFW_E_NOT_FOUND;
  2530. }
  2531.  
  2532.