ctlutil.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. //------------------------------------------------------------------------------
  2. // File: CtlUtil.h
  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. #ifndef __CTLUTIL__
  13. #define __CTLUTIL__
  14. // OLE Automation has different ideas of TRUE and FALSE
  15. #define OATRUE (-1)
  16. #define OAFALSE (0)
  17. // It's possible that we could replace this class with CreateStdDispatch
  18. class CBaseDispatch
  19. {
  20. ITypeInfo * m_pti;
  21. public:
  22. CBaseDispatch() : m_pti(NULL) {}
  23. ~CBaseDispatch();
  24. /* IDispatch methods */
  25. STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
  26. STDMETHODIMP GetTypeInfo(
  27. REFIID riid,
  28. UINT itinfo,
  29. LCID lcid,
  30. __deref_out ITypeInfo ** pptinfo);
  31. STDMETHODIMP GetIDsOfNames(
  32. REFIID riid,
  33. __in_ecount(cNames) LPOLESTR * rgszNames,
  34. UINT cNames,
  35. LCID lcid,
  36. __out_ecount(cNames) DISPID * rgdispid);
  37. };
  38. class AM_NOVTABLE CMediaControl :
  39. public IMediaControl,
  40. public CUnknown
  41. {
  42. CBaseDispatch m_basedisp;
  43. public:
  44. CMediaControl(const TCHAR *, LPUNKNOWN);
  45. DECLARE_IUNKNOWN
  46. // override this to publicise our interfaces
  47. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
  48. /* IDispatch methods */
  49. STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
  50. STDMETHODIMP GetTypeInfo(
  51. UINT itinfo,
  52. LCID lcid,
  53. __deref_out ITypeInfo ** pptinfo);
  54. STDMETHODIMP GetIDsOfNames(
  55. REFIID riid,
  56. __in_ecount(cNames) LPOLESTR * rgszNames,
  57. UINT cNames,
  58. LCID lcid,
  59. __out_ecount(cNames) DISPID * rgdispid);
  60. STDMETHODIMP Invoke(
  61. DISPID dispidMember,
  62. REFIID riid,
  63. LCID lcid,
  64. WORD wFlags,
  65. __in DISPPARAMS * pdispparams,
  66. __out_opt VARIANT * pvarResult,
  67. __out_opt EXCEPINFO * pexcepinfo,
  68. __out_opt UINT * puArgErr);
  69. };
  70. class AM_NOVTABLE CMediaEvent :
  71. public IMediaEventEx,
  72. public CUnknown
  73. {
  74. CBaseDispatch m_basedisp;
  75. public:
  76. CMediaEvent(__in_opt LPCTSTR, __in_opt LPUNKNOWN);
  77. DECLARE_IUNKNOWN
  78. // override this to publicise our interfaces
  79. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
  80. /* IDispatch methods */
  81. STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
  82. STDMETHODIMP GetTypeInfo(
  83. UINT itinfo,
  84. LCID lcid,
  85. __deref_out ITypeInfo ** pptinfo);
  86. STDMETHODIMP GetIDsOfNames(
  87. REFIID riid,
  88. __in_ecount(cNames) LPOLESTR * rgszNames,
  89. UINT cNames,
  90. LCID lcid,
  91. __out_ecount(cNames) DISPID * rgdispid);
  92. STDMETHODIMP Invoke(
  93. DISPID dispidMember,
  94. REFIID riid,
  95. LCID lcid,
  96. WORD wFlags,
  97. __in DISPPARAMS * pdispparams,
  98. __out_opt VARIANT * pvarResult,
  99. __out_opt EXCEPINFO * pexcepinfo,
  100. __out_opt UINT * puArgErr);
  101. };
  102. class AM_NOVTABLE CMediaPosition :
  103. public IMediaPosition,
  104. public CUnknown
  105. {
  106. CBaseDispatch m_basedisp;
  107. public:
  108. CMediaPosition(__in_opt LPCTSTR, __in_opt LPUNKNOWN);
  109. CMediaPosition(__in_opt LPCTSTR, __in_opt LPUNKNOWN, __inout HRESULT *phr);
  110. DECLARE_IUNKNOWN
  111. // override this to publicise our interfaces
  112. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
  113. /* IDispatch methods */
  114. STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
  115. STDMETHODIMP GetTypeInfo(
  116. UINT itinfo,
  117. LCID lcid,
  118. __deref_out ITypeInfo ** pptinfo);
  119. STDMETHODIMP GetIDsOfNames(
  120. REFIID riid,
  121. __in_ecount(cNames) LPOLESTR * rgszNames,
  122. UINT cNames,
  123. LCID lcid,
  124. __out_ecount(cNames) DISPID * rgdispid);
  125. STDMETHODIMP Invoke(
  126. DISPID dispidMember,
  127. REFIID riid,
  128. LCID lcid,
  129. WORD wFlags,
  130. __in DISPPARAMS * pdispparams,
  131. __out_opt VARIANT * pvarResult,
  132. __out_opt EXCEPINFO * pexcepinfo,
  133. __out_opt UINT * puArgErr);
  134. };
  135. // OA-compatibility means that we must use double as the RefTime value,
  136. // and REFERENCE_TIME (essentially a LONGLONG) within filters.
  137. // this class converts between the two
  138. class COARefTime : public CRefTime {
  139. public:
  140. COARefTime() {
  141. };
  142. COARefTime(CRefTime t)
  143. : CRefTime(t)
  144. {
  145. };
  146. COARefTime(REFERENCE_TIME t)
  147. : CRefTime(t)
  148. {
  149. };
  150. COARefTime(double d) {
  151. m_time = (LONGLONG) (d * 10000000);
  152. };
  153. operator double() {
  154. return double(m_time) / 10000000;
  155. };
  156. operator REFERENCE_TIME() {
  157. return m_time;
  158. };
  159. COARefTime& operator=(const double& rd) {
  160. m_time = (LONGLONG) (rd * 10000000);
  161. return *this;
  162. }
  163. COARefTime& operator=(const REFERENCE_TIME& rt) {
  164. m_time = rt;
  165. return *this;
  166. }
  167. inline BOOL operator==(const COARefTime& rt)
  168. {
  169. return m_time == rt.m_time;
  170. };
  171. inline BOOL operator!=(const COARefTime& rt)
  172. {
  173. return m_time != rt.m_time;
  174. };
  175. inline BOOL operator < (const COARefTime& rt)
  176. {
  177. return m_time < rt.m_time;
  178. };
  179. inline BOOL operator > (const COARefTime& rt)
  180. {
  181. return m_time > rt.m_time;
  182. };
  183. inline BOOL operator >= (const COARefTime& rt)
  184. {
  185. return m_time >= rt.m_time;
  186. };
  187. inline BOOL operator <= (const COARefTime& rt)
  188. {
  189. return m_time <= rt.m_time;
  190. };
  191. inline COARefTime operator+(const COARefTime& rt)
  192. {
  193. return COARefTime(m_time + rt.m_time);
  194. };
  195. inline COARefTime operator-(const COARefTime& rt)
  196. {
  197. return COARefTime(m_time - rt.m_time);
  198. };
  199. inline COARefTime operator*(LONG l)
  200. {
  201. return COARefTime(m_time * l);
  202. };
  203. inline COARefTime operator/(LONG l)
  204. {
  205. return COARefTime(m_time / l);
  206. };
  207. private:
  208. // Prevent bugs from constructing from LONG (which gets
  209. // converted to double and then multiplied by 10000000
  210. COARefTime(LONG);
  211. LONG operator=(LONG);
  212. };
  213. // A utility class that handles IMediaPosition and IMediaSeeking on behalf
  214. // of single-input pin renderers, or transform filters.
  215. //
  216. // Renderers will expose this from the filter; transform filters will
  217. // expose it from the output pin and not the renderer.
  218. //
  219. // Create one of these, giving it your IPin* for your input pin, and delegate
  220. // all IMediaPosition methods to it. It will query the input pin for
  221. // IMediaPosition and respond appropriately.
  222. //
  223. // Call ForceRefresh if the pin connection changes.
  224. //
  225. // This class no longer caches the upstream IMediaPosition or IMediaSeeking
  226. // it acquires it on each method call. This means ForceRefresh is not needed.
  227. // The method is kept for source compatibility and to minimise the changes
  228. // if we need to put it back later for performance reasons.
  229. class CPosPassThru : public IMediaSeeking, public CMediaPosition
  230. {
  231. IPin *m_pPin;
  232. HRESULT GetPeer(__deref_out IMediaPosition **ppMP);
  233. HRESULT GetPeerSeeking(__deref_out IMediaSeeking **ppMS);
  234. public:
  235. CPosPassThru(__in_opt LPCTSTR, __in_opt LPUNKNOWN, __inout HRESULT*, IPin *);
  236. DECLARE_IUNKNOWN
  237. HRESULT ForceRefresh() {
  238. return S_OK;
  239. };
  240. // override to return an accurate current position
  241. virtual HRESULT GetMediaTime(__out LONGLONG *pStartTime, __out_opt LONGLONG *pEndTime) {
  242. return E_FAIL;
  243. }
  244. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid,__deref_out void **ppv);
  245. // IMediaSeeking methods
  246. STDMETHODIMP GetCapabilities( __out DWORD * pCapabilities );
  247. STDMETHODIMP CheckCapabilities( __inout DWORD * pCapabilities );
  248. STDMETHODIMP SetTimeFormat(const GUID * pFormat);
  249. STDMETHODIMP GetTimeFormat(__out GUID *pFormat);
  250. STDMETHODIMP IsUsingTimeFormat(const GUID * pFormat);
  251. STDMETHODIMP IsFormatSupported( const GUID * pFormat);
  252. STDMETHODIMP QueryPreferredFormat( __out GUID *pFormat);
  253. STDMETHODIMP ConvertTimeFormat(__out LONGLONG * pTarget,
  254. __in_opt const GUID * pTargetFormat,
  255. LONGLONG Source,
  256. __in_opt const GUID * pSourceFormat );
  257. STDMETHODIMP SetPositions( __inout_opt LONGLONG * pCurrent, DWORD CurrentFlags
  258. , __inout_opt LONGLONG * pStop, DWORD StopFlags );
  259. STDMETHODIMP GetPositions( __out_opt LONGLONG * pCurrent, __out_opt LONGLONG * pStop );
  260. STDMETHODIMP GetCurrentPosition( __out LONGLONG * pCurrent );
  261. STDMETHODIMP GetStopPosition( __out LONGLONG * pStop );
  262. STDMETHODIMP SetRate( double dRate);
  263. STDMETHODIMP GetRate( __out double * pdRate);
  264. STDMETHODIMP GetDuration( __out LONGLONG *pDuration);
  265. STDMETHODIMP GetAvailable( __out_opt LONGLONG *pEarliest, __out_opt LONGLONG *pLatest );
  266. STDMETHODIMP GetPreroll( __out LONGLONG *pllPreroll );
  267. // IMediaPosition properties
  268. STDMETHODIMP get_Duration(__out REFTIME * plength);
  269. STDMETHODIMP put_CurrentPosition(REFTIME llTime);
  270. STDMETHODIMP get_StopTime(__out REFTIME * pllTime);
  271. STDMETHODIMP put_StopTime(REFTIME llTime);
  272. STDMETHODIMP get_PrerollTime(__out REFTIME * pllTime);
  273. STDMETHODIMP put_PrerollTime(REFTIME llTime);
  274. STDMETHODIMP get_Rate(__out double * pdRate);
  275. STDMETHODIMP put_Rate(double dRate);
  276. STDMETHODIMP get_CurrentPosition(__out REFTIME * pllTime);
  277. STDMETHODIMP CanSeekForward(__out LONG *pCanSeekForward);
  278. STDMETHODIMP CanSeekBackward(__out LONG *pCanSeekBackward);
  279. private:
  280. HRESULT GetSeekingLongLong( HRESULT (__stdcall IMediaSeeking::*pMethod)( LONGLONG * ),
  281. __out LONGLONG * pll );
  282. };
  283. // Adds the ability to return a current position
  284. class CRendererPosPassThru : public CPosPassThru
  285. {
  286. CCritSec m_PositionLock; // Locks access to our position
  287. LONGLONG m_StartMedia; // Start media time last seen
  288. LONGLONG m_EndMedia; // And likewise the end media
  289. BOOL m_bReset; // Have media times been set
  290. public:
  291. // Used to help with passing media times through graph
  292. CRendererPosPassThru(__in_opt LPCTSTR, __in_opt LPUNKNOWN, __inout HRESULT*, IPin *);
  293. HRESULT RegisterMediaTime(IMediaSample *pMediaSample);
  294. HRESULT RegisterMediaTime(LONGLONG StartTime,LONGLONG EndTime);
  295. HRESULT GetMediaTime(__out LONGLONG *pStartTime,__out_opt LONGLONG *pEndTime);
  296. HRESULT ResetMediaTime();
  297. HRESULT EOS();
  298. };
  299. STDAPI CreatePosPassThru(
  300. __in_opt LPUNKNOWN pAgg,
  301. BOOL bRenderer,
  302. IPin *pPin,
  303. __deref_out IUnknown **ppPassThru
  304. );
  305. // A class that handles the IDispatch part of IBasicAudio and leaves the
  306. // properties and methods themselves pure virtual.
  307. class AM_NOVTABLE CBasicAudio : public IBasicAudio, public CUnknown
  308. {
  309. CBaseDispatch m_basedisp;
  310. public:
  311. CBasicAudio(__in_opt LPCTSTR, __in_opt LPUNKNOWN);
  312. DECLARE_IUNKNOWN
  313. // override this to publicise our interfaces
  314. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
  315. /* IDispatch methods */
  316. STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
  317. STDMETHODIMP GetTypeInfo(
  318. UINT itinfo,
  319. LCID lcid,
  320. __deref_out ITypeInfo ** pptinfo);
  321. STDMETHODIMP GetIDsOfNames(
  322. REFIID riid,
  323. __in_ecount(cNames) LPOLESTR * rgszNames,
  324. UINT cNames,
  325. LCID lcid,
  326. __out_ecount(cNames) DISPID * rgdispid);
  327. STDMETHODIMP Invoke(
  328. DISPID dispidMember,
  329. REFIID riid,
  330. LCID lcid,
  331. WORD wFlags,
  332. __in DISPPARAMS * pdispparams,
  333. __out_opt VARIANT * pvarResult,
  334. __out_opt EXCEPINFO * pexcepinfo,
  335. __out_opt UINT * puArgErr);
  336. };
  337. // A class that handles the IDispatch part of IBasicVideo and leaves the
  338. // properties and methods themselves pure virtual.
  339. class AM_NOVTABLE CBaseBasicVideo : public IBasicVideo2, public CUnknown
  340. {
  341. CBaseDispatch m_basedisp;
  342. public:
  343. CBaseBasicVideo(__in_opt LPCTSTR, __in_opt LPUNKNOWN);
  344. DECLARE_IUNKNOWN
  345. // override this to publicise our interfaces
  346. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
  347. /* IDispatch methods */
  348. STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
  349. STDMETHODIMP GetTypeInfo(
  350. UINT itinfo,
  351. LCID lcid,
  352. __deref_out ITypeInfo ** pptinfo);
  353. STDMETHODIMP GetIDsOfNames(
  354. REFIID riid,
  355. __in_ecount(cNames) LPOLESTR * rgszNames,
  356. UINT cNames,
  357. LCID lcid,
  358. __out_ecount(cNames) DISPID * rgdispid);
  359. STDMETHODIMP Invoke(
  360. DISPID dispidMember,
  361. REFIID riid,
  362. LCID lcid,
  363. WORD wFlags,
  364. __in DISPPARAMS * pdispparams,
  365. __out_opt VARIANT * pvarResult,
  366. __out_opt EXCEPINFO * pexcepinfo,
  367. __out_opt UINT * puArgErr);
  368. STDMETHODIMP GetPreferredAspectRatio(
  369. __out long *plAspectX,
  370. __out long *plAspectY)
  371. {
  372. return E_NOTIMPL;
  373. }
  374. };
  375. // A class that handles the IDispatch part of IVideoWindow and leaves the
  376. // properties and methods themselves pure virtual.
  377. class AM_NOVTABLE CBaseVideoWindow : public IVideoWindow, public CUnknown
  378. {
  379. CBaseDispatch m_basedisp;
  380. public:
  381. CBaseVideoWindow(__in_opt LPCTSTR, __in_opt LPUNKNOWN);
  382. DECLARE_IUNKNOWN
  383. // override this to publicise our interfaces
  384. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
  385. /* IDispatch methods */
  386. STDMETHODIMP GetTypeInfoCount(__out UINT * pctinfo);
  387. STDMETHODIMP GetTypeInfo(
  388. UINT itinfo,
  389. LCID lcid,
  390. __deref_out ITypeInfo ** pptinfo);
  391. STDMETHODIMP GetIDsOfNames(
  392. REFIID riid,
  393. __in_ecount(cNames) LPOLESTR * rgszNames,
  394. UINT cNames,
  395. LCID lcid,
  396. __out_ecount(cNames) DISPID * rgdispid);
  397. STDMETHODIMP Invoke(
  398. DISPID dispidMember,
  399. REFIID riid,
  400. LCID lcid,
  401. WORD wFlags,
  402. __in DISPPARAMS * pdispparams,
  403. __out_opt VARIANT * pvarResult,
  404. __out_opt EXCEPINFO * pexcepinfo,
  405. __out_opt UINT * puArgErr);
  406. };
  407. // abstract class to help source filters with their implementation
  408. // of IMediaPosition. Derive from this and set the duration (and stop
  409. // position). Also override NotifyChange to do something when the properties
  410. // change.
  411. class AM_NOVTABLE CSourcePosition : public CMediaPosition
  412. {
  413. public:
  414. CSourcePosition(__in_opt LPCTSTR, __in_opt LPUNKNOWN, __inout HRESULT*, __in CCritSec *);
  415. // IMediaPosition methods
  416. STDMETHODIMP get_Duration(__out REFTIME * plength);
  417. STDMETHODIMP put_CurrentPosition(REFTIME llTime);
  418. STDMETHODIMP get_StopTime(__out REFTIME * pllTime);
  419. STDMETHODIMP put_StopTime(REFTIME llTime);
  420. STDMETHODIMP get_PrerollTime(__out REFTIME * pllTime);
  421. STDMETHODIMP put_PrerollTime(REFTIME llTime);
  422. STDMETHODIMP get_Rate(__out double * pdRate);
  423. STDMETHODIMP put_Rate(double dRate);
  424. STDMETHODIMP CanSeekForward(__out LONG *pCanSeekForward);
  425. STDMETHODIMP CanSeekBackward(__out LONG *pCanSeekBackward);
  426. // override if you can return the data you are actually working on
  427. STDMETHODIMP get_CurrentPosition(__out REFTIME * pllTime) {
  428. return E_NOTIMPL;
  429. };
  430. protected:
  431. // we call this to notify changes. Override to handle them
  432. virtual HRESULT ChangeStart() PURE;
  433. virtual HRESULT ChangeStop() PURE;
  434. virtual HRESULT ChangeRate() PURE;
  435. COARefTime m_Duration;
  436. COARefTime m_Start;
  437. COARefTime m_Stop;
  438. double m_Rate;
  439. CCritSec * m_pLock;
  440. };
  441. class AM_NOVTABLE CSourceSeeking :
  442. public IMediaSeeking,
  443. public CUnknown
  444. {
  445. public:
  446. DECLARE_IUNKNOWN;
  447. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
  448. // IMediaSeeking methods
  449. STDMETHODIMP IsFormatSupported(const GUID * pFormat);
  450. STDMETHODIMP QueryPreferredFormat(__out GUID *pFormat);
  451. STDMETHODIMP SetTimeFormat(const GUID * pFormat);
  452. STDMETHODIMP IsUsingTimeFormat(const GUID * pFormat);
  453. STDMETHODIMP GetTimeFormat(__out GUID *pFormat);
  454. STDMETHODIMP GetDuration(__out LONGLONG *pDuration);
  455. STDMETHODIMP GetStopPosition(__out LONGLONG *pStop);
  456. STDMETHODIMP GetCurrentPosition(__out LONGLONG *pCurrent);
  457. STDMETHODIMP GetCapabilities( __out DWORD * pCapabilities );
  458. STDMETHODIMP CheckCapabilities( __inout DWORD * pCapabilities );
  459. STDMETHODIMP ConvertTimeFormat( __out LONGLONG * pTarget,
  460. __in_opt const GUID * pTargetFormat,
  461. LONGLONG Source,
  462. __in_opt const GUID * pSourceFormat );
  463. STDMETHODIMP SetPositions( __inout_opt LONGLONG * pCurrent, DWORD CurrentFlags
  464. , __inout_opt LONGLONG * pStop, DWORD StopFlags );
  465. STDMETHODIMP GetPositions( __out_opt LONGLONG * pCurrent, __out_opt LONGLONG * pStop );
  466. STDMETHODIMP GetAvailable( __out_opt LONGLONG * pEarliest, __out_opt LONGLONG * pLatest );
  467. STDMETHODIMP SetRate( double dRate);
  468. STDMETHODIMP GetRate( __out double * pdRate);
  469. STDMETHODIMP GetPreroll(__out LONGLONG *pPreroll);
  470. protected:
  471. // ctor
  472. CSourceSeeking(__in_opt LPCTSTR, __in_opt LPUNKNOWN, __inout HRESULT*, __in CCritSec *);
  473. // we call this to notify changes. Override to handle them
  474. virtual HRESULT ChangeStart() PURE;
  475. virtual HRESULT ChangeStop() PURE;
  476. virtual HRESULT ChangeRate() PURE;
  477. CRefTime m_rtDuration; // length of stream
  478. CRefTime m_rtStart; // source will start here
  479. CRefTime m_rtStop; // source will stop here
  480. double m_dRateSeeking;
  481. // seeking capabilities
  482. DWORD m_dwSeekingCaps;
  483. CCritSec * m_pLock;
  484. };
  485. // Base classes supporting Deferred commands.
  486. // Deferred commands are queued by calls to methods on the IQueueCommand
  487. // interface, exposed by the filtergraph and by some filters. A successful
  488. // call to one of these methods will return an IDeferredCommand interface
  489. // representing the queued command.
  490. //
  491. // A CDeferredCommand object represents a single deferred command, and exposes
  492. // the IDeferredCommand interface as well as other methods permitting time
  493. // checks and actual execution. It contains a reference to the CCommandQueue
  494. // object on which it is queued.
  495. //
  496. // CCommandQueue is a base class providing a queue of CDeferredCommand
  497. // objects, and methods to add, remove, check status and invoke the queued
  498. // commands. A CCommandQueue object would be part of an object that
  499. // implemented IQueueCommand.
  500. class CCmdQueue;
  501. // take a copy of the params and store them. Release any allocated
  502. // memory in destructor
  503. class CDispParams : public DISPPARAMS
  504. {
  505. public:
  506. CDispParams(UINT nArgs, __in_ecount(nArgs) VARIANT* pArgs, __inout_opt HRESULT *phr = NULL);
  507. ~CDispParams();
  508. };
  509. // CDeferredCommand lifetime is controlled by refcounts. Caller of
  510. // InvokeAt.. gets a refcounted interface pointer, and the CCmdQueue
  511. // object also holds a refcount on us. Calling Cancel or Invoke takes
  512. // us off the CCmdQueue and thus reduces the refcount by 1. Once taken
  513. // off the queue we cannot be put back on the queue.
  514. class CDeferredCommand
  515. : public CUnknown,
  516. public IDeferredCommand
  517. {
  518. public:
  519. CDeferredCommand(
  520. __inout CCmdQueue * pQ,
  521. __in_opt LPUNKNOWN pUnk, // aggregation outer unk
  522. __inout HRESULT * phr,
  523. __in LPUNKNOWN pUnkExecutor, // object that will execute this cmd
  524. REFTIME time,
  525. __in GUID* iid,
  526. long dispidMethod,
  527. short wFlags,
  528. long cArgs,
  529. __in_ecount(cArgs) VARIANT* pDispParams,
  530. __out VARIANT* pvarResult,
  531. __out short* puArgErr,
  532. BOOL bStream
  533. );
  534. DECLARE_IUNKNOWN
  535. // override this to publicise our interfaces
  536. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __out void **ppv);
  537. // IDeferredCommand methods
  538. STDMETHODIMP Cancel();
  539. STDMETHODIMP Confidence(
  540. __out LONG* pConfidence);
  541. STDMETHODIMP Postpone(
  542. REFTIME newtime);
  543. STDMETHODIMP GetHResult(
  544. __out HRESULT* phrResult);
  545. // other public methods
  546. HRESULT Invoke();
  547. // access methods
  548. // returns TRUE if streamtime, FALSE if presentation time
  549. BOOL IsStreamTime() {
  550. return m_bStream;
  551. };
  552. CRefTime GetTime() {
  553. return m_time;
  554. };
  555. REFIID GetIID() {
  556. return *m_iid;
  557. };
  558. long GetMethod() {
  559. return m_dispidMethod;
  560. };
  561. short GetFlags() {
  562. return m_wFlags;
  563. };
  564. DISPPARAMS* GetParams() {
  565. return &m_DispParams;
  566. };
  567. VARIANT* GetResult() {
  568. return m_pvarResult;
  569. };
  570. protected:
  571. CCmdQueue* m_pQueue;
  572. // pUnk for the interface that we will execute the command on
  573. LPUNKNOWN m_pUnk;
  574. // stored command data
  575. REFERENCE_TIME m_time;
  576. GUID* m_iid;
  577. long m_dispidMethod;
  578. short m_wFlags;
  579. VARIANT* m_pvarResult;
  580. BOOL m_bStream;
  581. CDispParams m_DispParams;
  582. DISPID m_DispId; // For get and put
  583. // we use this for ITypeInfo access
  584. CBaseDispatch m_Dispatch;
  585. // save retval here
  586. HRESULT m_hrResult;
  587. };
  588. // a list of CDeferredCommand objects. this is a base class providing
  589. // the basics of access to the list. If you want to use CDeferredCommand
  590. // objects then your queue needs to be derived from this class.
  591. class AM_NOVTABLE CCmdQueue
  592. {
  593. public:
  594. CCmdQueue(__inout_opt HRESULT *phr = NULL);
  595. virtual ~CCmdQueue();
  596. // returns a new CDeferredCommand object that will be initialised with
  597. // the parameters and will be added to the queue during construction.
  598. // returns S_OK if successfully created otherwise an error and
  599. // no object has been queued.
  600. virtual HRESULT New(
  601. __out CDeferredCommand **ppCmd,
  602. __in LPUNKNOWN pUnk,
  603. REFTIME time,
  604. __in GUID* iid,
  605. long dispidMethod,
  606. short wFlags,
  607. long cArgs,
  608. __in_ecount(cArgs) VARIANT* pDispParams,
  609. __out VARIANT* pvarResult,
  610. __out short* puArgErr,
  611. BOOL bStream
  612. );
  613. // called by the CDeferredCommand object to add and remove itself
  614. // from the queue
  615. virtual HRESULT Insert(__in CDeferredCommand* pCmd);
  616. virtual HRESULT Remove(__in CDeferredCommand* pCmd);
  617. // Command-Due Checking
  618. //
  619. // There are two schemes of synchronisation: coarse and accurate. In
  620. // coarse mode, you wait till the time arrives and then execute the cmd.
  621. // In accurate mode, you wait until you are processing the sample that
  622. // will appear at the time, and then execute the command. It's up to the
  623. // filter which one it will implement. The filtergraph will always
  624. // implement coarse mode for commands queued at the filtergraph.
  625. //
  626. // If you want coarse sync, you probably want to wait until there is a
  627. // command due, and then execute it. You can do this by calling
  628. // GetDueCommand. If you have several things to wait for, get the
  629. // event handle from GetDueHandle() and when this is signalled then call
  630. // GetDueCommand. Stream time will only advance between calls to Run and
  631. // EndRun. Note that to avoid an extra thread there is no guarantee that
  632. // if the handle is set there will be a command ready. Each time the
  633. // event is signalled, call GetDueCommand (probably with a 0 timeout);
  634. // This may return E_ABORT.
  635. //
  636. // If you want accurate sync, you must call GetCommandDueFor, passing
  637. // as a parameter the stream time of the samples you are about to process.
  638. // This will return:
  639. // -- a stream-time command due at or before that stream time
  640. // -- a presentation-time command due at or before the
  641. // time that stream time will be presented (only between Run
  642. // and EndRun calls, since outside of this, the mapping from
  643. // stream time to presentation time is not known.
  644. // -- any presentation-time command due now.
  645. // This means that if you want accurate synchronisation on samples that
  646. // might be processed during Paused mode, you need to use
  647. // stream-time commands.
  648. //
  649. // In all cases, commands remain queued until Invoked or Cancelled. The
  650. // setting and resetting of the event handle is managed entirely by this
  651. // queue object.
  652. // set the clock used for timing
  653. virtual HRESULT SetSyncSource(__in_opt IReferenceClock*);
  654. // switch to run mode. Streamtime to Presentation time mapping known.
  655. virtual HRESULT Run(REFERENCE_TIME tStreamTimeOffset);
  656. // switch to Stopped or Paused mode. Time mapping not known.
  657. virtual HRESULT EndRun();
  658. // return a pointer to the next due command. Blocks for msTimeout
  659. // milliseconds until there is a due command.
  660. // Stream-time commands will only become due between Run and Endrun calls.
  661. // The command remains queued until invoked or cancelled.
  662. // Returns E_ABORT if timeout occurs, otherwise S_OK (or other error).
  663. // Returns an AddRef-ed object
  664. virtual HRESULT GetDueCommand(__out CDeferredCommand ** ppCmd, long msTimeout);
  665. // return the event handle that will be signalled whenever
  666. // there are deferred commands due for execution (when GetDueCommand
  667. // will not block).
  668. HANDLE GetDueHandle() {
  669. return HANDLE(m_evDue);
  670. };
  671. // return a pointer to a command that will be due for a given time.
  672. // Pass in a stream time here. The stream time offset will be passed
  673. // in via the Run method.
  674. // Commands remain queued until invoked or cancelled.
  675. // This method will not block. It will report VFW_E_NOT_FOUND if there
  676. // are no commands due yet.
  677. // Returns an AddRef-ed object
  678. virtual HRESULT GetCommandDueFor(REFERENCE_TIME tStream, __out CDeferredCommand**ppCmd);
  679. // check if a given time is due (TRUE if it is due yet)
  680. BOOL CheckTime(CRefTime time, BOOL bStream) {
  681. // if no clock, nothing is due!
  682. if (!m_pClock) {
  683. return FALSE;
  684. }
  685. // stream time
  686. if (bStream) {
  687. // not valid if not running
  688. if (!m_bRunning) {
  689. return FALSE;
  690. }
  691. // add on known stream time offset to get presentation time
  692. time += m_StreamTimeOffset;
  693. }
  694. CRefTime Now;
  695. m_pClock->GetTime((REFERENCE_TIME*)&Now);
  696. return (time <= Now);
  697. };
  698. protected:
  699. // protect access to lists etc
  700. CCritSec m_Lock;
  701. // commands queued in presentation time are stored here
  702. CGenericList<CDeferredCommand> m_listPresentation;
  703. // commands queued in stream time are stored here
  704. CGenericList<CDeferredCommand> m_listStream;
  705. // set when any commands are due
  706. CAMEvent m_evDue;
  707. // creates an advise for the earliest time required, if any
  708. void SetTimeAdvise(void);
  709. // advise id from reference clock (0 if no outstanding advise)
  710. DWORD_PTR m_dwAdvise;
  711. // advise time is for this presentation time
  712. CRefTime m_tCurrentAdvise;
  713. // the reference clock we are using (addrefed)
  714. IReferenceClock* m_pClock;
  715. // true when running
  716. BOOL m_bRunning;
  717. // contains stream time offset when m_bRunning is true
  718. CRefTime m_StreamTimeOffset;
  719. };
  720. #endif // __CTLUTIL__