Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2531 lines
50 KiB

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