ctlutil.h 27 KB

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