1
0

ctlutil.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541
  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(__out 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. __deref_out ITypeLib FAR* FAR* pptlib);
  36. typedef HRESULT (STDAPICALLTYPE *LPLOADREGTYPELIB)(REFGUID rguid,
  37. WORD wVerMajor,
  38. WORD wVerMinor,
  39. LCID lcid,
  40. __deref_out 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. __deref_out 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. __in_ecount(cNames) LPOLESTR * rgszNames,
  114. UINT cNames,
  115. LCID lcid,
  116. __out_ecount(cNames) 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, __deref_out 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(__out 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. __deref_out 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. __in_ecount(cNames) LPOLESTR * rgszNames,
  167. UINT cNames,
  168. LCID lcid,
  169. __out_ecount(cNames) 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. __in DISPPARAMS * pdispparams,
  185. __out_opt VARIANT * pvarResult,
  186. __out_opt EXCEPINFO * pexcepinfo,
  187. __out_opt 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(__in_opt LPCTSTR name,__in_opt LPUNKNOWN pUnk) :
  211. CUnknown(name, pUnk)
  212. {
  213. }
  214. // expose our interfaces IMediaEvent and IUnknown
  215. STDMETHODIMP
  216. CMediaEvent::NonDelegatingQueryInterface(REFIID riid, __deref_out 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(__out 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. __deref_out 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. __in_ecount(cNames) LPOLESTR * rgszNames,
  248. UINT cNames,
  249. LCID lcid,
  250. __out_ecount(cNames) 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. __in DISPPARAMS * pdispparams,
  266. __out_opt VARIANT * pvarResult,
  267. __out_opt EXCEPINFO * pexcepinfo,
  268. __out_opt 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(__in_opt LPCTSTR name,__in_opt LPUNKNOWN pUnk) :
  292. CUnknown(name, pUnk)
  293. {
  294. }
  295. CMediaPosition::CMediaPosition(__in_opt LPCTSTR name,
  296. __in_opt LPUNKNOWN pUnk,
  297. __inout 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, __deref_out 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(__out 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. __deref_out 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. __in_ecount(cNames) LPOLESTR * rgszNames,
  336. UINT cNames,
  337. LCID lcid,
  338. __out_ecount(cNames) 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. __in DISPPARAMS * pdispparams,
  354. __out_opt VARIANT * pvarResult,
  355. __out_opt EXCEPINFO * pexcepinfo,
  356. __out_opt 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(__in_opt LPCTSTR pName,
  380. __in_opt LPUNKNOWN pUnk,
  381. __inout 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,__deref_out 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(__deref_out 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(__out 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(__inout 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(__out 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(__out 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(__out LONGLONG * pTarget,
  527. __in_opt const GUID * pTargetFormat,
  528. LONGLONG Source,
  529. __in_opt const GUID * pSourceFormat )
  530. {
  531. IMediaSeeking* pMS;
  532. HRESULT hr = GetPeerSeeking(&pMS);
  533. if (FAILED(hr)) {
  534. return hr;
  535. }
  536. hr = pMS->ConvertTimeFormat(pTarget, pTargetFormat, Source, pSourceFormat );
  537. pMS->Release();
  538. return hr;
  539. }
  540. STDMETHODIMP
  541. CPosPassThru::SetPositions( __inout_opt LONGLONG * pCurrent,
  542. DWORD CurrentFlags,
  543. __inout_opt LONGLONG * pStop,
  544. DWORD StopFlags )
  545. {
  546. IMediaSeeking* pMS;
  547. HRESULT hr = GetPeerSeeking(&pMS);
  548. if (FAILED(hr)) {
  549. return hr;
  550. }
  551. hr = pMS->SetPositions(pCurrent, CurrentFlags, pStop, StopFlags );
  552. pMS->Release();
  553. return hr;
  554. }
  555. STDMETHODIMP
  556. CPosPassThru::GetPositions(__out_opt LONGLONG *pCurrent, __out_opt LONGLONG * pStop)
  557. {
  558. IMediaSeeking* pMS;
  559. HRESULT hr = GetPeerSeeking(&pMS);
  560. if (FAILED(hr)) {
  561. return hr;
  562. }
  563. hr = pMS->GetPositions(pCurrent,pStop);
  564. pMS->Release();
  565. return hr;
  566. }
  567. HRESULT
  568. CPosPassThru::GetSeekingLongLong
  569. ( HRESULT (__stdcall IMediaSeeking::*pMethod)( __out LONGLONG * )
  570. , LONGLONG * pll
  571. )
  572. {
  573. IMediaSeeking* pMS;
  574. HRESULT hr = GetPeerSeeking(&pMS);
  575. if (SUCCEEDED(hr))
  576. {
  577. hr = (pMS->*pMethod)(pll);
  578. pMS->Release();
  579. }
  580. return hr;
  581. }
  582. // If we don't have a current position then ask upstream
  583. STDMETHODIMP
  584. CPosPassThru::GetCurrentPosition(__out LONGLONG *pCurrent)
  585. {
  586. // Can we report the current position
  587. HRESULT hr = GetMediaTime(pCurrent,NULL);
  588. if (SUCCEEDED(hr)) hr = NOERROR;
  589. else hr = GetSeekingLongLong( &IMediaSeeking::GetCurrentPosition, pCurrent );
  590. return hr;
  591. }
  592. STDMETHODIMP
  593. CPosPassThru::GetStopPosition(__out LONGLONG *pStop)
  594. {
  595. return GetSeekingLongLong( &IMediaSeeking::GetStopPosition, pStop );;
  596. }
  597. STDMETHODIMP
  598. CPosPassThru::GetDuration(__out LONGLONG *pDuration)
  599. {
  600. return GetSeekingLongLong( &IMediaSeeking::GetDuration, pDuration );;
  601. }
  602. STDMETHODIMP
  603. CPosPassThru::GetPreroll(__out LONGLONG *pllPreroll)
  604. {
  605. return GetSeekingLongLong( &IMediaSeeking::GetPreroll, pllPreroll );;
  606. }
  607. STDMETHODIMP
  608. CPosPassThru::GetAvailable( __out_opt LONGLONG *pEarliest, __out_opt LONGLONG *pLatest )
  609. {
  610. IMediaSeeking* pMS;
  611. HRESULT hr = GetPeerSeeking(&pMS);
  612. if (FAILED(hr)) {
  613. return hr;
  614. }
  615. hr = pMS->GetAvailable( pEarliest, pLatest );
  616. pMS->Release();
  617. return hr;
  618. }
  619. STDMETHODIMP
  620. CPosPassThru::GetRate(__out double * pdRate)
  621. {
  622. IMediaSeeking* pMS;
  623. HRESULT hr = GetPeerSeeking(&pMS);
  624. if (FAILED(hr)) {
  625. return hr;
  626. }
  627. hr = pMS->GetRate(pdRate);
  628. pMS->Release();
  629. return hr;
  630. }
  631. STDMETHODIMP
  632. CPosPassThru::SetRate(double dRate)
  633. {
  634. if (0.0 == dRate) {
  635. return E_INVALIDARG;
  636. }
  637. IMediaSeeking* pMS;
  638. HRESULT hr = GetPeerSeeking(&pMS);
  639. if (FAILED(hr)) {
  640. return hr;
  641. }
  642. hr = pMS->SetRate(dRate);
  643. pMS->Release();
  644. return hr;
  645. }
  646. // --- IMediaPosition methods ----------
  647. STDMETHODIMP
  648. CPosPassThru::get_Duration(__out REFTIME * plength)
  649. {
  650. IMediaPosition* pMP;
  651. HRESULT hr = GetPeer(&pMP);
  652. if (FAILED(hr)) {
  653. return hr;
  654. }
  655. hr = pMP->get_Duration(plength);
  656. pMP->Release();
  657. return hr;
  658. }
  659. STDMETHODIMP
  660. CPosPassThru::get_CurrentPosition(__out REFTIME * pllTime)
  661. {
  662. IMediaPosition* pMP;
  663. HRESULT hr = GetPeer(&pMP);
  664. if (FAILED(hr)) {
  665. return hr;
  666. }
  667. hr = pMP->get_CurrentPosition(pllTime);
  668. pMP->Release();
  669. return hr;
  670. }
  671. STDMETHODIMP
  672. CPosPassThru::put_CurrentPosition(REFTIME llTime)
  673. {
  674. IMediaPosition* pMP;
  675. HRESULT hr = GetPeer(&pMP);
  676. if (FAILED(hr)) {
  677. return hr;
  678. }
  679. hr = pMP->put_CurrentPosition(llTime);
  680. pMP->Release();
  681. return hr;
  682. }
  683. STDMETHODIMP
  684. CPosPassThru::get_StopTime(__out REFTIME * pllTime)
  685. {
  686. IMediaPosition* pMP;
  687. HRESULT hr = GetPeer(&pMP);
  688. if (FAILED(hr)) {
  689. return hr;
  690. }
  691. hr = pMP->get_StopTime(pllTime);
  692. pMP->Release();
  693. return hr;
  694. }
  695. STDMETHODIMP
  696. CPosPassThru::put_StopTime(REFTIME llTime)
  697. {
  698. IMediaPosition* pMP;
  699. HRESULT hr = GetPeer(&pMP);
  700. if (FAILED(hr)) {
  701. return hr;
  702. }
  703. hr = pMP->put_StopTime(llTime);
  704. pMP->Release();
  705. return hr;
  706. }
  707. STDMETHODIMP
  708. CPosPassThru::get_PrerollTime(__out REFTIME * pllTime)
  709. {
  710. IMediaPosition* pMP;
  711. HRESULT hr = GetPeer(&pMP);
  712. if (FAILED(hr)) {
  713. return hr;
  714. }
  715. hr = pMP->get_PrerollTime(pllTime);
  716. pMP->Release();
  717. return hr;
  718. }
  719. STDMETHODIMP
  720. CPosPassThru::put_PrerollTime(REFTIME llTime)
  721. {
  722. IMediaPosition* pMP;
  723. HRESULT hr = GetPeer(&pMP);
  724. if (FAILED(hr)) {
  725. return hr;
  726. }
  727. hr = pMP->put_PrerollTime(llTime);
  728. pMP->Release();
  729. return hr;
  730. }
  731. STDMETHODIMP
  732. CPosPassThru::get_Rate(__out double * pdRate)
  733. {
  734. IMediaPosition* pMP;
  735. HRESULT hr = GetPeer(&pMP);
  736. if (FAILED(hr)) {
  737. return hr;
  738. }
  739. hr = pMP->get_Rate(pdRate);
  740. pMP->Release();
  741. return hr;
  742. }
  743. STDMETHODIMP
  744. CPosPassThru::put_Rate(double dRate)
  745. {
  746. if (0.0 == dRate) {
  747. return E_INVALIDARG;
  748. }
  749. IMediaPosition* pMP;
  750. HRESULT hr = GetPeer(&pMP);
  751. if (FAILED(hr)) {
  752. return hr;
  753. }
  754. hr = pMP->put_Rate(dRate);
  755. pMP->Release();
  756. return hr;
  757. }
  758. STDMETHODIMP
  759. CPosPassThru::CanSeekForward(__out LONG *pCanSeekForward)
  760. {
  761. IMediaPosition* pMP;
  762. HRESULT hr = GetPeer(&pMP);
  763. if (FAILED(hr)) {
  764. return hr;
  765. }
  766. hr = pMP->CanSeekForward(pCanSeekForward);
  767. pMP->Release();
  768. return hr;
  769. }
  770. STDMETHODIMP
  771. CPosPassThru::CanSeekBackward(__out LONG *pCanSeekBackward)
  772. {
  773. IMediaPosition* pMP;
  774. HRESULT hr = GetPeer(&pMP);
  775. if (FAILED(hr)) {
  776. return hr;
  777. }
  778. hr = pMP->CanSeekBackward(pCanSeekBackward);
  779. pMP->Release();
  780. return hr;
  781. }
  782. // --- Implements the CRendererPosPassThru class ----------
  783. // Media times (eg current frame, field, sample etc) are passed through the
  784. // filtergraph in media samples. When a renderer gets a sample with media
  785. // times in it, it will call one of the RegisterMediaTime methods we expose
  786. // (one takes an IMediaSample, the other takes the media times direct). We
  787. // store the media times internally and return them in GetCurrentPosition.
  788. CRendererPosPassThru::CRendererPosPassThru(__in_opt LPCTSTR pName,
  789. __in_opt LPUNKNOWN pUnk,
  790. __inout HRESULT *phr,
  791. IPin *pPin) :
  792. CPosPassThru(pName,pUnk,phr,pPin),
  793. m_StartMedia(0),
  794. m_EndMedia(0),
  795. m_bReset(TRUE)
  796. {
  797. }
  798. // Sets the media times the object should report
  799. HRESULT
  800. CRendererPosPassThru::RegisterMediaTime(IMediaSample *pMediaSample)
  801. {
  802. ASSERT(pMediaSample);
  803. LONGLONG StartMedia;
  804. LONGLONG EndMedia;
  805. CAutoLock cAutoLock(&m_PositionLock);
  806. // Get the media times from the sample
  807. HRESULT hr = pMediaSample->GetTime(&StartMedia,&EndMedia);
  808. if (FAILED(hr))
  809. {
  810. ASSERT(hr == VFW_E_SAMPLE_TIME_NOT_SET);
  811. return hr;
  812. }
  813. m_StartMedia = StartMedia;
  814. m_EndMedia = EndMedia;
  815. m_bReset = FALSE;
  816. return NOERROR;
  817. }
  818. // Sets the media times the object should report
  819. HRESULT
  820. CRendererPosPassThru::RegisterMediaTime(LONGLONG StartTime,LONGLONG EndTime)
  821. {
  822. CAutoLock cAutoLock(&m_PositionLock);
  823. m_StartMedia = StartTime;
  824. m_EndMedia = EndTime;
  825. m_bReset = FALSE;
  826. return NOERROR;
  827. }
  828. // Return the current media times registered in the object
  829. HRESULT
  830. CRendererPosPassThru::GetMediaTime(__out LONGLONG *pStartTime, __out_opt LONGLONG *pEndTime)
  831. {
  832. ASSERT(pStartTime);
  833. CAutoLock cAutoLock(&m_PositionLock);
  834. if (m_bReset == TRUE) {
  835. return E_FAIL;
  836. }
  837. // We don't have to return the end time
  838. HRESULT hr = ConvertTimeFormat( pStartTime, 0, m_StartMedia, &TIME_FORMAT_MEDIA_TIME );
  839. if (pEndTime && SUCCEEDED(hr)) {
  840. hr = ConvertTimeFormat( pEndTime, 0, m_EndMedia, &TIME_FORMAT_MEDIA_TIME );
  841. }
  842. return hr;
  843. }
  844. // Resets the media times we hold
  845. HRESULT
  846. CRendererPosPassThru::ResetMediaTime()
  847. {
  848. CAutoLock cAutoLock(&m_PositionLock);
  849. m_StartMedia = 0;
  850. m_EndMedia = 0;
  851. m_bReset = TRUE;
  852. return NOERROR;
  853. }
  854. // Intended to be called by the owing filter during EOS processing so
  855. // that the media times can be adjusted to the stop time. This ensures
  856. // that the GetCurrentPosition will actully get to the stop position.
  857. HRESULT
  858. CRendererPosPassThru::EOS()
  859. {
  860. HRESULT hr;
  861. if ( m_bReset == TRUE ) hr = E_FAIL;
  862. else
  863. {
  864. LONGLONG llStop;
  865. if SUCCEEDED(hr=GetStopPosition(&llStop))
  866. {
  867. CAutoLock cAutoLock(&m_PositionLock);
  868. m_StartMedia =
  869. m_EndMedia = llStop;
  870. }
  871. }
  872. return hr;
  873. }
  874. // -- CSourceSeeking implementation ------------
  875. CSourceSeeking::CSourceSeeking(
  876. __in_opt LPCTSTR pName,
  877. __in_opt LPUNKNOWN pUnk,
  878. __inout HRESULT* phr,
  879. __in CCritSec * pLock) :
  880. CUnknown(pName, pUnk),
  881. m_pLock(pLock),
  882. m_rtStart((long)0)
  883. {
  884. m_rtStop = _I64_MAX / 2;
  885. m_rtDuration = m_rtStop;
  886. m_dRateSeeking = 1.0;
  887. m_dwSeekingCaps = AM_SEEKING_CanSeekForwards
  888. | AM_SEEKING_CanSeekBackwards
  889. | AM_SEEKING_CanSeekAbsolute
  890. | AM_SEEKING_CanGetStopPos
  891. | AM_SEEKING_CanGetDuration;
  892. }
  893. HRESULT CSourceSeeking::NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv)
  894. {
  895. if(riid == IID_IMediaSeeking) {
  896. CheckPointer(ppv, E_POINTER);
  897. return GetInterface(static_cast<IMediaSeeking *>(this), ppv);
  898. }
  899. else {
  900. return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  901. }
  902. }
  903. HRESULT CSourceSeeking::IsFormatSupported(const GUID * pFormat)
  904. {
  905. CheckPointer(pFormat, E_POINTER);
  906. // only seeking in time (REFERENCE_TIME units) is supported
  907. return *pFormat == TIME_FORMAT_MEDIA_TIME ? S_OK : S_FALSE;
  908. }
  909. HRESULT CSourceSeeking::QueryPreferredFormat(__out GUID *pFormat)
  910. {
  911. CheckPointer(pFormat, E_POINTER);
  912. *pFormat = TIME_FORMAT_MEDIA_TIME;
  913. return S_OK;
  914. }
  915. HRESULT CSourceSeeking::SetTimeFormat(const GUID * pFormat)
  916. {
  917. CheckPointer(pFormat, E_POINTER);
  918. // nothing to set; just check that it's TIME_FORMAT_TIME
  919. return *pFormat == TIME_FORMAT_MEDIA_TIME ? S_OK : E_INVALIDARG;
  920. }
  921. HRESULT CSourceSeeking::IsUsingTimeFormat(const GUID * pFormat)
  922. {
  923. CheckPointer(pFormat, E_POINTER);
  924. return *pFormat == TIME_FORMAT_MEDIA_TIME ? S_OK : S_FALSE;
  925. }
  926. HRESULT CSourceSeeking::GetTimeFormat(__out GUID *pFormat)
  927. {
  928. CheckPointer(pFormat, E_POINTER);
  929. *pFormat = TIME_FORMAT_MEDIA_TIME;
  930. return S_OK;
  931. }
  932. HRESULT CSourceSeeking::GetDuration(__out LONGLONG *pDuration)
  933. {
  934. CheckPointer(pDuration, E_POINTER);
  935. CAutoLock lock(m_pLock);
  936. *pDuration = m_rtDuration;
  937. return S_OK;
  938. }
  939. HRESULT CSourceSeeking::GetStopPosition(__out LONGLONG *pStop)
  940. {
  941. CheckPointer(pStop, E_POINTER);
  942. CAutoLock lock(m_pLock);
  943. *pStop = m_rtStop;
  944. return S_OK;
  945. }
  946. HRESULT CSourceSeeking::GetCurrentPosition(__out LONGLONG *pCurrent)
  947. {
  948. // GetCurrentPosition is typically supported only in renderers and
  949. // not in source filters.
  950. return E_NOTIMPL;
  951. }
  952. HRESULT CSourceSeeking::GetCapabilities( __out DWORD * pCapabilities )
  953. {
  954. CheckPointer(pCapabilities, E_POINTER);
  955. *pCapabilities = m_dwSeekingCaps;
  956. return S_OK;
  957. }
  958. HRESULT CSourceSeeking::CheckCapabilities( __inout DWORD * pCapabilities )
  959. {
  960. CheckPointer(pCapabilities, E_POINTER);
  961. // make sure all requested capabilities are in our mask
  962. return (~m_dwSeekingCaps & *pCapabilities) ? S_FALSE : S_OK;
  963. }
  964. HRESULT CSourceSeeking::ConvertTimeFormat( __out LONGLONG * pTarget,
  965. __in_opt const GUID * pTargetFormat,
  966. LONGLONG Source,
  967. __in_opt const GUID * pSourceFormat )
  968. {
  969. CheckPointer(pTarget, E_POINTER);
  970. // format guids can be null to indicate current format
  971. // since we only support TIME_FORMAT_MEDIA_TIME, we don't really
  972. // offer any conversions.
  973. if(pTargetFormat == 0 || *pTargetFormat == TIME_FORMAT_MEDIA_TIME)
  974. {
  975. if(pSourceFormat == 0 || *pSourceFormat == TIME_FORMAT_MEDIA_TIME)
  976. {
  977. *pTarget = Source;
  978. return S_OK;
  979. }
  980. }
  981. return E_INVALIDARG;
  982. }
  983. HRESULT CSourceSeeking::SetPositions( __inout_opt LONGLONG * pCurrent,
  984. DWORD CurrentFlags,
  985. __inout_opt LONGLONG * pStop,
  986. DWORD StopFlags )
  987. {
  988. DWORD StopPosBits = StopFlags & AM_SEEKING_PositioningBitsMask;
  989. DWORD StartPosBits = CurrentFlags & AM_SEEKING_PositioningBitsMask;
  990. if(StopFlags) {
  991. CheckPointer(pStop, E_POINTER);
  992. // accept only relative, incremental, or absolute positioning
  993. if(StopPosBits != StopFlags) {
  994. return E_INVALIDARG;
  995. }
  996. }
  997. if(CurrentFlags) {
  998. CheckPointer(pCurrent, E_POINTER);
  999. if(StartPosBits != AM_SEEKING_AbsolutePositioning &&
  1000. StartPosBits != AM_SEEKING_RelativePositioning) {
  1001. return E_INVALIDARG;
  1002. }
  1003. }
  1004. // scope for autolock
  1005. {
  1006. CAutoLock lock(m_pLock);
  1007. // set start position
  1008. if(StartPosBits == AM_SEEKING_AbsolutePositioning)
  1009. {
  1010. m_rtStart = *pCurrent;
  1011. }
  1012. else if(StartPosBits == AM_SEEKING_RelativePositioning)
  1013. {
  1014. m_rtStart += *pCurrent;
  1015. }
  1016. // set stop position
  1017. if(StopPosBits == AM_SEEKING_AbsolutePositioning)
  1018. {
  1019. m_rtStop = *pStop;
  1020. }
  1021. else if(StopPosBits == AM_SEEKING_IncrementalPositioning)
  1022. {
  1023. m_rtStop = m_rtStart + *pStop;
  1024. }
  1025. else if(StopPosBits == AM_SEEKING_RelativePositioning)
  1026. {
  1027. m_rtStop = m_rtStop + *pStop;
  1028. }
  1029. }
  1030. HRESULT hr = S_OK;
  1031. if(SUCCEEDED(hr) && StopPosBits) {
  1032. hr = ChangeStop();
  1033. }
  1034. if(StartPosBits) {
  1035. hr = ChangeStart();
  1036. }
  1037. return hr;
  1038. }
  1039. HRESULT CSourceSeeking::GetPositions( __out_opt LONGLONG * pCurrent, __out_opt LONGLONG * pStop )
  1040. {
  1041. if(pCurrent) {
  1042. *pCurrent = m_rtStart;
  1043. }
  1044. if(pStop) {
  1045. *pStop = m_rtStop;
  1046. }
  1047. return S_OK;;
  1048. }
  1049. HRESULT CSourceSeeking::GetAvailable( __out_opt LONGLONG * pEarliest, __out_opt LONGLONG * pLatest )
  1050. {
  1051. if(pEarliest) {
  1052. *pEarliest = 0;
  1053. }
  1054. if(pLatest) {
  1055. CAutoLock lock(m_pLock);
  1056. *pLatest = m_rtDuration;
  1057. }
  1058. return S_OK;
  1059. }
  1060. HRESULT CSourceSeeking::SetRate( double dRate)
  1061. {
  1062. {
  1063. CAutoLock lock(m_pLock);
  1064. m_dRateSeeking = dRate;
  1065. }
  1066. return ChangeRate();
  1067. }
  1068. HRESULT CSourceSeeking::GetRate( __out double * pdRate)
  1069. {
  1070. CheckPointer(pdRate, E_POINTER);
  1071. CAutoLock lock(m_pLock);
  1072. *pdRate = m_dRateSeeking;
  1073. return S_OK;
  1074. }
  1075. HRESULT CSourceSeeking::GetPreroll(__out LONGLONG *pPreroll)
  1076. {
  1077. CheckPointer(pPreroll, E_POINTER);
  1078. *pPreroll = 0;
  1079. return S_OK;
  1080. }
  1081. // --- CSourcePosition implementation ----------
  1082. CSourcePosition::CSourcePosition(__in_opt LPCTSTR pName,
  1083. __in_opt LPUNKNOWN pUnk,
  1084. __inout HRESULT* phr,
  1085. __in CCritSec * pLock) :
  1086. CMediaPosition(pName, pUnk),
  1087. m_pLock(pLock),
  1088. m_Start(CRefTime((LONGLONG)0))
  1089. {
  1090. m_Stop = _I64_MAX;
  1091. m_Rate = 1.0;
  1092. }
  1093. STDMETHODIMP
  1094. CSourcePosition::get_Duration(__out REFTIME * plength)
  1095. {
  1096. CheckPointer(plength,E_POINTER);
  1097. ValidateReadWritePtr(plength,sizeof(REFTIME));
  1098. CAutoLock lock(m_pLock);
  1099. *plength = m_Duration;
  1100. return S_OK;
  1101. }
  1102. STDMETHODIMP
  1103. CSourcePosition::put_CurrentPosition(REFTIME llTime)
  1104. {
  1105. m_pLock->Lock();
  1106. m_Start = llTime;
  1107. m_pLock->Unlock();
  1108. return ChangeStart();
  1109. }
  1110. STDMETHODIMP
  1111. CSourcePosition::get_StopTime(__out REFTIME * pllTime)
  1112. {
  1113. CheckPointer(pllTime,E_POINTER);
  1114. ValidateReadWritePtr(pllTime,sizeof(REFTIME));
  1115. CAutoLock lock(m_pLock);
  1116. *pllTime = m_Stop;
  1117. return S_OK;
  1118. }
  1119. STDMETHODIMP
  1120. CSourcePosition::put_StopTime(REFTIME llTime)
  1121. {
  1122. m_pLock->Lock();
  1123. m_Stop = llTime;
  1124. m_pLock->Unlock();
  1125. return ChangeStop();
  1126. }
  1127. STDMETHODIMP
  1128. CSourcePosition::get_PrerollTime(__out REFTIME * pllTime)
  1129. {
  1130. CheckPointer(pllTime,E_POINTER);
  1131. ValidateReadWritePtr(pllTime,sizeof(REFTIME));
  1132. return E_NOTIMPL;
  1133. }
  1134. STDMETHODIMP
  1135. CSourcePosition::put_PrerollTime(REFTIME llTime)
  1136. {
  1137. return E_NOTIMPL;
  1138. }
  1139. STDMETHODIMP
  1140. CSourcePosition::get_Rate(__out double * pdRate)
  1141. {
  1142. CheckPointer(pdRate,E_POINTER);
  1143. ValidateReadWritePtr(pdRate,sizeof(double));
  1144. CAutoLock lock(m_pLock);
  1145. *pdRate = m_Rate;
  1146. return S_OK;
  1147. }
  1148. STDMETHODIMP
  1149. CSourcePosition::put_Rate(double dRate)
  1150. {
  1151. m_pLock->Lock();
  1152. m_Rate = dRate;
  1153. m_pLock->Unlock();
  1154. return ChangeRate();
  1155. }
  1156. // By default we can seek forwards
  1157. STDMETHODIMP
  1158. CSourcePosition::CanSeekForward(__out LONG *pCanSeekForward)
  1159. {
  1160. CheckPointer(pCanSeekForward,E_POINTER);
  1161. *pCanSeekForward = OATRUE;
  1162. return S_OK;
  1163. }
  1164. // By default we can seek backwards
  1165. STDMETHODIMP
  1166. CSourcePosition::CanSeekBackward(__out LONG *pCanSeekBackward)
  1167. {
  1168. CheckPointer(pCanSeekBackward,E_POINTER);
  1169. *pCanSeekBackward = OATRUE;
  1170. return S_OK;
  1171. }
  1172. // --- Implementation of CBasicAudio class ----------
  1173. CBasicAudio::CBasicAudio(__in_opt LPCTSTR pName,__in_opt LPUNKNOWN punk) :
  1174. CUnknown(pName, punk)
  1175. {
  1176. }
  1177. // overriden to publicise our interfaces
  1178. STDMETHODIMP
  1179. CBasicAudio::NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv)
  1180. {
  1181. ValidateReadWritePtr(ppv,sizeof(PVOID));
  1182. if (riid == IID_IBasicAudio) {
  1183. return GetInterface( (IBasicAudio *) this, ppv);
  1184. } else {
  1185. return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  1186. }
  1187. }
  1188. STDMETHODIMP
  1189. CBasicAudio::GetTypeInfoCount(__out UINT * pctinfo)
  1190. {
  1191. return m_basedisp.GetTypeInfoCount(pctinfo);
  1192. }
  1193. STDMETHODIMP
  1194. CBasicAudio::GetTypeInfo(
  1195. UINT itinfo,
  1196. LCID lcid,
  1197. __deref_out ITypeInfo ** pptinfo)
  1198. {
  1199. return m_basedisp.GetTypeInfo(
  1200. IID_IBasicAudio,
  1201. itinfo,
  1202. lcid,
  1203. pptinfo);
  1204. }
  1205. STDMETHODIMP
  1206. CBasicAudio::GetIDsOfNames(
  1207. REFIID riid,
  1208. __in_ecount(cNames) LPOLESTR * rgszNames,
  1209. UINT cNames,
  1210. LCID lcid,
  1211. __out_ecount(cNames) DISPID * rgdispid)
  1212. {
  1213. return m_basedisp.GetIDsOfNames(
  1214. IID_IBasicAudio,
  1215. rgszNames,
  1216. cNames,
  1217. lcid,
  1218. rgdispid);
  1219. }
  1220. STDMETHODIMP
  1221. CBasicAudio::Invoke(
  1222. DISPID dispidMember,
  1223. REFIID riid,
  1224. LCID lcid,
  1225. WORD wFlags,
  1226. __in DISPPARAMS * pdispparams,
  1227. __out_opt VARIANT * pvarResult,
  1228. __out_opt EXCEPINFO * pexcepinfo,
  1229. __out_opt UINT * puArgErr)
  1230. {
  1231. // this parameter is a dead leftover from an earlier interface
  1232. if (IID_NULL != riid) {
  1233. return DISP_E_UNKNOWNINTERFACE;
  1234. }
  1235. ITypeInfo * pti;
  1236. HRESULT hr = GetTypeInfo(0, lcid, &pti);
  1237. if (FAILED(hr)) {
  1238. return hr;
  1239. }
  1240. hr = pti->Invoke(
  1241. (IBasicAudio *)this,
  1242. dispidMember,
  1243. wFlags,
  1244. pdispparams,
  1245. pvarResult,
  1246. pexcepinfo,
  1247. puArgErr);
  1248. pti->Release();
  1249. return hr;
  1250. }
  1251. // --- IVideoWindow implementation ----------
  1252. CBaseVideoWindow::CBaseVideoWindow(__in_opt LPCTSTR pName,__in_opt LPUNKNOWN punk) :
  1253. CUnknown(pName, punk)
  1254. {
  1255. }
  1256. // overriden to publicise our interfaces
  1257. STDMETHODIMP
  1258. CBaseVideoWindow::NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv)
  1259. {
  1260. ValidateReadWritePtr(ppv,sizeof(PVOID));
  1261. if (riid == IID_IVideoWindow) {
  1262. return GetInterface( (IVideoWindow *) this, ppv);
  1263. } else {
  1264. return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  1265. }
  1266. }
  1267. STDMETHODIMP
  1268. CBaseVideoWindow::GetTypeInfoCount(__out UINT * pctinfo)
  1269. {
  1270. return m_basedisp.GetTypeInfoCount(pctinfo);
  1271. }
  1272. STDMETHODIMP
  1273. CBaseVideoWindow::GetTypeInfo(
  1274. UINT itinfo,
  1275. LCID lcid,
  1276. __deref_out ITypeInfo ** pptinfo)
  1277. {
  1278. return m_basedisp.GetTypeInfo(
  1279. IID_IVideoWindow,
  1280. itinfo,
  1281. lcid,
  1282. pptinfo);
  1283. }
  1284. STDMETHODIMP
  1285. CBaseVideoWindow::GetIDsOfNames(
  1286. REFIID riid,
  1287. __in_ecount(cNames) LPOLESTR * rgszNames,
  1288. UINT cNames,
  1289. LCID lcid,
  1290. __out_ecount(cNames) DISPID * rgdispid)
  1291. {
  1292. return m_basedisp.GetIDsOfNames(
  1293. IID_IVideoWindow,
  1294. rgszNames,
  1295. cNames,
  1296. lcid,
  1297. rgdispid);
  1298. }
  1299. STDMETHODIMP
  1300. CBaseVideoWindow::Invoke(
  1301. DISPID dispidMember,
  1302. REFIID riid,
  1303. LCID lcid,
  1304. WORD wFlags,
  1305. __in DISPPARAMS * pdispparams,
  1306. __out_opt VARIANT * pvarResult,
  1307. __out_opt EXCEPINFO * pexcepinfo,
  1308. __out_opt UINT * puArgErr)
  1309. {
  1310. // this parameter is a dead leftover from an earlier interface
  1311. if (IID_NULL != riid) {
  1312. return DISP_E_UNKNOWNINTERFACE;
  1313. }
  1314. ITypeInfo * pti;
  1315. HRESULT hr = GetTypeInfo(0, lcid, &pti);
  1316. if (FAILED(hr)) {
  1317. return hr;
  1318. }
  1319. hr = pti->Invoke(
  1320. (IVideoWindow *)this,
  1321. dispidMember,
  1322. wFlags,
  1323. pdispparams,
  1324. pvarResult,
  1325. pexcepinfo,
  1326. puArgErr);
  1327. pti->Release();
  1328. return hr;
  1329. }
  1330. // --- IBasicVideo implementation ----------
  1331. CBaseBasicVideo::CBaseBasicVideo(__in_opt LPCTSTR pName,__in_opt LPUNKNOWN punk) :
  1332. CUnknown(pName, punk)
  1333. {
  1334. }
  1335. // overriden to publicise our interfaces
  1336. STDMETHODIMP
  1337. CBaseBasicVideo::NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv)
  1338. {
  1339. ValidateReadWritePtr(ppv,sizeof(PVOID));
  1340. if (riid == IID_IBasicVideo || riid == IID_IBasicVideo2) {
  1341. return GetInterface( static_cast<IBasicVideo2 *>(this), ppv);
  1342. } else {
  1343. return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  1344. }
  1345. }
  1346. STDMETHODIMP
  1347. CBaseBasicVideo::GetTypeInfoCount(__out UINT * pctinfo)
  1348. {
  1349. return m_basedisp.GetTypeInfoCount(pctinfo);
  1350. }
  1351. STDMETHODIMP
  1352. CBaseBasicVideo::GetTypeInfo(
  1353. UINT itinfo,
  1354. LCID lcid,
  1355. __deref_out ITypeInfo ** pptinfo)
  1356. {
  1357. return m_basedisp.GetTypeInfo(
  1358. IID_IBasicVideo,
  1359. itinfo,
  1360. lcid,
  1361. pptinfo);
  1362. }
  1363. STDMETHODIMP
  1364. CBaseBasicVideo::GetIDsOfNames(
  1365. REFIID riid,
  1366. __in_ecount(cNames) LPOLESTR * rgszNames,
  1367. UINT cNames,
  1368. LCID lcid,
  1369. __out_ecount(cNames) DISPID * rgdispid)
  1370. {
  1371. return m_basedisp.GetIDsOfNames(
  1372. IID_IBasicVideo,
  1373. rgszNames,
  1374. cNames,
  1375. lcid,
  1376. rgdispid);
  1377. }
  1378. STDMETHODIMP
  1379. CBaseBasicVideo::Invoke(
  1380. DISPID dispidMember,
  1381. REFIID riid,
  1382. LCID lcid,
  1383. WORD wFlags,
  1384. __in DISPPARAMS * pdispparams,
  1385. __out_opt VARIANT * pvarResult,
  1386. __out_opt EXCEPINFO * pexcepinfo,
  1387. __out_opt UINT * puArgErr)
  1388. {
  1389. // this parameter is a dead leftover from an earlier interface
  1390. if (IID_NULL != riid) {
  1391. return DISP_E_UNKNOWNINTERFACE;
  1392. }
  1393. ITypeInfo * pti;
  1394. HRESULT hr = GetTypeInfo(0, lcid, &pti);
  1395. if (FAILED(hr)) {
  1396. return hr;
  1397. }
  1398. hr = pti->Invoke(
  1399. (IBasicVideo *)this,
  1400. dispidMember,
  1401. wFlags,
  1402. pdispparams,
  1403. pvarResult,
  1404. pexcepinfo,
  1405. puArgErr);
  1406. pti->Release();
  1407. return hr;
  1408. }
  1409. // --- Implementation of Deferred Commands ----------
  1410. CDispParams::CDispParams(UINT nArgs, __in_ecount(nArgs) VARIANT* pArgs, __inout_opt HRESULT *phr)
  1411. {
  1412. cNamedArgs = 0;
  1413. rgdispidNamedArgs = NULL;
  1414. cArgs = nArgs;
  1415. if (cArgs) {
  1416. rgvarg = new VARIANT[cArgs];
  1417. if (NULL == rgvarg) {
  1418. cArgs = 0;
  1419. if (phr) {
  1420. *phr = E_OUTOFMEMORY;
  1421. }
  1422. return;
  1423. }
  1424. for (UINT i = 0; i < cArgs; i++) {
  1425. // Why aren't we using VariantCopy?
  1426. VARIANT * pDest = &rgvarg[i];
  1427. VARIANT * pSrc = &pArgs[i];
  1428. pDest->vt = pSrc->vt;
  1429. switch(pDest->vt) {
  1430. case VT_I4:
  1431. pDest->lVal = pSrc->lVal;
  1432. break;
  1433. case VT_UI1:
  1434. pDest->bVal = pSrc->bVal;
  1435. break;
  1436. case VT_I2:
  1437. pDest->iVal = pSrc->iVal;
  1438. break;
  1439. case VT_R4:
  1440. pDest->fltVal = pSrc->fltVal;
  1441. break;
  1442. case VT_R8:
  1443. pDest->dblVal = pSrc->dblVal;
  1444. break;
  1445. case VT_BOOL:
  1446. pDest->boolVal = pSrc->boolVal;
  1447. break;
  1448. case VT_ERROR:
  1449. pDest->scode = pSrc->scode;
  1450. break;
  1451. case VT_CY:
  1452. pDest->cyVal = pSrc->cyVal;
  1453. break;
  1454. case VT_DATE:
  1455. pDest->date = pSrc->date;
  1456. break;
  1457. case VT_BSTR:
  1458. if ((PVOID)pSrc->bstrVal == NULL) {
  1459. pDest->bstrVal = NULL;
  1460. } else {
  1461. // a BSTR is a WORD followed by a UNICODE string.
  1462. // the pointer points just after the WORD
  1463. WORD len = * (WORD*) (pSrc->bstrVal - (sizeof(WORD) / sizeof(OLECHAR)));
  1464. OLECHAR* pch = new OLECHAR[len + (sizeof(WORD)/sizeof(OLECHAR))];
  1465. if (pch) {
  1466. WORD *pui = (WORD*)pch;
  1467. *pui = len;
  1468. pDest->bstrVal = pch + (sizeof(WORD)/sizeof(OLECHAR));
  1469. CopyMemory(pDest->bstrVal, pSrc->bstrVal, len*sizeof(OLECHAR));
  1470. } else {
  1471. cArgs = i;
  1472. if (phr) {
  1473. *phr = E_OUTOFMEMORY;
  1474. }
  1475. }
  1476. }
  1477. break;
  1478. case VT_UNKNOWN:
  1479. pDest->punkVal = pSrc->punkVal;
  1480. pDest->punkVal->AddRef();
  1481. break;
  1482. case VT_DISPATCH:
  1483. pDest->pdispVal = pSrc->pdispVal;
  1484. pDest->pdispVal->AddRef();
  1485. break;
  1486. default:
  1487. // a type we haven't got round to adding yet!
  1488. ASSERT(0);
  1489. break;
  1490. }
  1491. }
  1492. } else {
  1493. rgvarg = NULL;
  1494. }
  1495. }
  1496. CDispParams::~CDispParams()
  1497. {
  1498. for (UINT i = 0; i < cArgs; i++) {
  1499. switch(rgvarg[i].vt) {
  1500. case VT_BSTR:
  1501. // Explicitly cast BSTR to PVOID to tell code scanning tools we really mean to test the pointer
  1502. if ((PVOID)rgvarg[i].bstrVal != NULL) {
  1503. OLECHAR * pch = rgvarg[i].bstrVal - (sizeof(WORD)/sizeof(OLECHAR));
  1504. delete pch;
  1505. }
  1506. break;
  1507. case VT_UNKNOWN:
  1508. rgvarg[i].punkVal->Release();
  1509. break;
  1510. case VT_DISPATCH:
  1511. rgvarg[i].pdispVal->Release();
  1512. break;
  1513. }
  1514. }
  1515. delete[] rgvarg;
  1516. }
  1517. // lifetime is controlled by refcounts (see defer.h)
  1518. CDeferredCommand::CDeferredCommand(
  1519. __inout CCmdQueue * pQ,
  1520. __in_opt LPUNKNOWN pUnk,
  1521. __inout HRESULT * phr,
  1522. __in LPUNKNOWN pUnkExecutor,
  1523. REFTIME time,
  1524. __in GUID* iid,
  1525. long dispidMethod,
  1526. short wFlags,
  1527. long nArgs,
  1528. __in_ecount(nArgs) VARIANT* pDispParams,
  1529. __out VARIANT* pvarResult,
  1530. __out short* puArgErr,
  1531. BOOL bStream
  1532. ) :
  1533. CUnknown(NAME("DeferredCommand"), pUnk),
  1534. m_pQueue(pQ),
  1535. m_pUnk(pUnkExecutor),
  1536. m_iid(iid),
  1537. m_dispidMethod(dispidMethod),
  1538. m_wFlags(wFlags),
  1539. m_DispParams(nArgs, pDispParams, phr),
  1540. m_pvarResult(pvarResult),
  1541. m_bStream(bStream),
  1542. m_hrResult(E_ABORT)
  1543. {
  1544. // convert REFTIME to REFERENCE_TIME
  1545. COARefTime convertor(time);
  1546. m_time = convertor;
  1547. // no check of time validity - it's ok to queue a command that's
  1548. // already late
  1549. // check iid is supportable on pUnk by QueryInterface for it
  1550. IUnknown * pInterface;
  1551. HRESULT hr = m_pUnk->QueryInterface(GetIID(), (void**) &pInterface);
  1552. if (FAILED(hr)) {
  1553. *phr = hr;
  1554. return;
  1555. }
  1556. pInterface->Release();
  1557. // !!! check dispidMethod and param/return types using typelib
  1558. ITypeInfo *pti;
  1559. hr = m_Dispatch.GetTypeInfo(*iid, 0, 0, &pti);
  1560. if (FAILED(hr)) {
  1561. *phr = hr;
  1562. return;
  1563. }
  1564. // !!! some sort of ITypeInfo validity check here
  1565. pti->Release();
  1566. // Fix up the dispid for put and get
  1567. if (wFlags == DISPATCH_PROPERTYPUT) {
  1568. m_DispParams.cNamedArgs = 1;
  1569. m_DispId = DISPID_PROPERTYPUT;
  1570. m_DispParams.rgdispidNamedArgs = &m_DispId;
  1571. }
  1572. // all checks ok - add to queue
  1573. hr = pQ->Insert(this);
  1574. if (FAILED(hr)) {
  1575. *phr = hr;
  1576. }
  1577. }
  1578. // refcounts are held by caller of InvokeAt... and by list. So if
  1579. // we get here, we can't be on the list
  1580. #if 0
  1581. CDeferredCommand::~CDeferredCommand()
  1582. {
  1583. // this assert is invalid since if the queue is deleted while we are
  1584. // still on the queue, we will have been removed by the queue and this
  1585. // m_pQueue will not have been modified.
  1586. // ASSERT(m_pQueue == NULL);
  1587. // we don't hold a ref count on pUnk, which is the object that should
  1588. // execute the command.
  1589. // This is because there would otherwise be a circular refcount problem
  1590. // since pUnk probably owns the CmdQueue object that has a refcount
  1591. // on us.
  1592. // The lifetime of pUnk is guaranteed by it being part of, or lifetime
  1593. // controlled by, our parent object. As long as we are on the list, pUnk
  1594. // must be valid. Once we are off the list, we do not use pUnk.
  1595. }
  1596. #endif
  1597. // overriden to publicise our interfaces
  1598. STDMETHODIMP
  1599. CDeferredCommand::NonDelegatingQueryInterface(REFIID riid, __out void **ppv)
  1600. {
  1601. ValidateReadWritePtr(ppv,sizeof(PVOID));
  1602. if (riid == IID_IDeferredCommand) {
  1603. return GetInterface( (IDeferredCommand *) this, ppv);
  1604. } else {
  1605. return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  1606. }
  1607. }
  1608. // remove from q. this will reduce the refcount by one (since the q
  1609. // holds a count) but can't make us go away since he must have a
  1610. // refcount in order to call this method.
  1611. STDMETHODIMP
  1612. CDeferredCommand::Cancel()
  1613. {
  1614. if (m_pQueue == NULL) {
  1615. return VFW_E_ALREADY_CANCELLED;
  1616. }
  1617. HRESULT hr = m_pQueue->Remove(this);
  1618. if (FAILED(hr)) {
  1619. return hr;
  1620. }
  1621. m_pQueue = NULL;
  1622. return S_OK;
  1623. }
  1624. STDMETHODIMP
  1625. CDeferredCommand::Confidence(__out LONG* pConfidence)
  1626. {
  1627. return E_NOTIMPL;
  1628. }
  1629. STDMETHODIMP
  1630. CDeferredCommand::GetHResult(__out HRESULT * phrResult)
  1631. {
  1632. CheckPointer(phrResult,E_POINTER);
  1633. ValidateReadWritePtr(phrResult,sizeof(HRESULT));
  1634. if (m_pQueue != NULL) {
  1635. return E_ABORT;
  1636. }
  1637. *phrResult = m_hrResult;
  1638. return S_OK;
  1639. }
  1640. // set the time to be a new time (checking that it is valid) and
  1641. // then requeue
  1642. STDMETHODIMP
  1643. CDeferredCommand::Postpone(REFTIME newtime)
  1644. {
  1645. // check that this time is not past
  1646. // convert REFTIME to REFERENCE_TIME
  1647. COARefTime convertor(newtime);
  1648. // check that the time has not passed
  1649. if (m_pQueue->CheckTime(convertor, IsStreamTime())) {
  1650. return VFW_E_TIME_ALREADY_PASSED;
  1651. }
  1652. // extract from list
  1653. HRESULT hr = m_pQueue->Remove(this);
  1654. if (FAILED(hr)) {
  1655. return hr;
  1656. }
  1657. // change time
  1658. m_time = convertor;
  1659. // requeue
  1660. hr = m_pQueue->Insert(this);
  1661. return hr;
  1662. }
  1663. HRESULT
  1664. CDeferredCommand::Invoke()
  1665. {
  1666. // check that we are still outstanding
  1667. if (m_pQueue == NULL) {
  1668. return VFW_E_ALREADY_CANCELLED;
  1669. }
  1670. // get the type info
  1671. ITypeInfo* pti;
  1672. HRESULT hr = m_Dispatch.GetTypeInfo(GetIID(), 0, 0, &pti);
  1673. if (FAILED(hr)) {
  1674. return hr;
  1675. }
  1676. // qi for the expected interface and then invoke it. Note that we have to
  1677. // treat the returned interface as IUnknown since we don't know its type.
  1678. IUnknown* pInterface;
  1679. hr = m_pUnk->QueryInterface(GetIID(), (void**) &pInterface);
  1680. if (FAILED(hr)) {
  1681. pti->Release();
  1682. return hr;
  1683. }
  1684. EXCEPINFO expinfo;
  1685. UINT uArgErr;
  1686. m_hrResult = pti->Invoke(
  1687. pInterface,
  1688. GetMethod(),
  1689. GetFlags(),
  1690. GetParams(),
  1691. GetResult(),
  1692. &expinfo,
  1693. &uArgErr);
  1694. // release the interface we QI'd for
  1695. pInterface->Release();
  1696. pti->Release();
  1697. // remove from list whether or not successful
  1698. // or we loop indefinitely
  1699. hr = m_pQueue->Remove(this);
  1700. m_pQueue = NULL;
  1701. return hr;
  1702. }
  1703. // --- CCmdQueue methods ----------
  1704. CCmdQueue::CCmdQueue(__inout_opt HRESULT *phr) :
  1705. m_listPresentation(NAME("Presentation time command list")),
  1706. m_listStream(NAME("Stream time command list")),
  1707. m_evDue(TRUE, phr), // manual reset
  1708. m_dwAdvise(0),
  1709. m_pClock(NULL),
  1710. m_bRunning(FALSE)
  1711. {
  1712. }
  1713. CCmdQueue::~CCmdQueue()
  1714. {
  1715. // empty all our lists
  1716. // we hold a refcount on each, so traverse and Release each
  1717. // entry then RemoveAll to empty the list
  1718. POSITION pos = m_listPresentation.GetHeadPosition();
  1719. while(pos) {
  1720. CDeferredCommand* pCmd = m_listPresentation.GetNext(pos);
  1721. pCmd->Release();
  1722. }
  1723. m_listPresentation.RemoveAll();
  1724. pos = m_listStream.GetHeadPosition();
  1725. while(pos) {
  1726. CDeferredCommand* pCmd = m_listStream.GetNext(pos);
  1727. pCmd->Release();
  1728. }
  1729. m_listStream.RemoveAll();
  1730. if (m_pClock) {
  1731. if (m_dwAdvise) {
  1732. m_pClock->Unadvise(m_dwAdvise);
  1733. m_dwAdvise = 0;
  1734. }
  1735. m_pClock->Release();
  1736. }
  1737. }
  1738. // returns a new CDeferredCommand object that will be initialised with
  1739. // the parameters and will be added to the queue during construction.
  1740. // returns S_OK if successfully created otherwise an error and
  1741. // no object has been queued.
  1742. HRESULT
  1743. CCmdQueue::New(
  1744. __out CDeferredCommand **ppCmd,
  1745. __in LPUNKNOWN pUnk, // this object will execute command
  1746. REFTIME time,
  1747. __in GUID* iid,
  1748. long dispidMethod,
  1749. short wFlags,
  1750. long cArgs,
  1751. __in_ecount(cArgs) VARIANT* pDispParams,
  1752. __out VARIANT* pvarResult,
  1753. __out short* puArgErr,
  1754. BOOL bStream
  1755. )
  1756. {
  1757. CAutoLock lock(&m_Lock);
  1758. HRESULT hr = S_OK;
  1759. *ppCmd = NULL;
  1760. CDeferredCommand* pCmd;
  1761. pCmd = new CDeferredCommand(
  1762. this,
  1763. NULL, // not aggregated
  1764. &hr,
  1765. pUnk, // this guy will execute
  1766. time,
  1767. iid,
  1768. dispidMethod,
  1769. wFlags,
  1770. cArgs,
  1771. pDispParams,
  1772. pvarResult,
  1773. puArgErr,
  1774. bStream);
  1775. if (pCmd == NULL) {
  1776. hr = E_OUTOFMEMORY;
  1777. } else {
  1778. *ppCmd = pCmd;
  1779. }
  1780. return hr;
  1781. }
  1782. HRESULT
  1783. CCmdQueue::Insert(__in CDeferredCommand* pCmd)
  1784. {
  1785. CAutoLock lock(&m_Lock);
  1786. // addref the item
  1787. pCmd->AddRef();
  1788. CGenericList<CDeferredCommand> * pList;
  1789. if (pCmd->IsStreamTime()) {
  1790. pList = &m_listStream;
  1791. } else {
  1792. pList = &m_listPresentation;
  1793. }
  1794. POSITION pos = pList->GetHeadPosition();
  1795. // seek past all items that are before us
  1796. while (pos &&
  1797. (pList->GetValid(pos)->GetTime() <= pCmd->GetTime())) {
  1798. pList->GetNext(pos);
  1799. }
  1800. // now at end of list or in front of items that come later
  1801. if (!pos) {
  1802. pList->AddTail(pCmd);
  1803. } else {
  1804. pList->AddBefore(pos, pCmd);
  1805. }
  1806. SetTimeAdvise();
  1807. return S_OK;
  1808. }
  1809. HRESULT
  1810. CCmdQueue::Remove(__in CDeferredCommand* pCmd)
  1811. {
  1812. CAutoLock lock(&m_Lock);
  1813. HRESULT hr = S_OK;
  1814. CGenericList<CDeferredCommand> * pList;
  1815. if (pCmd->IsStreamTime()) {
  1816. pList = &m_listStream;
  1817. } else {
  1818. pList = &m_listPresentation;
  1819. }
  1820. POSITION pos = pList->GetHeadPosition();
  1821. // traverse the list
  1822. while (pos && (pList->GetValid(pos) != pCmd)) {
  1823. pList->GetNext(pos);
  1824. }
  1825. // did we drop off the end?
  1826. if (!pos) {
  1827. hr = VFW_E_NOT_FOUND;
  1828. } else {
  1829. // found it - now take off list
  1830. pList->Remove(pos);
  1831. // Insert did an AddRef, so release it
  1832. pCmd->Release();
  1833. // check that timer request is still for earliest time
  1834. SetTimeAdvise();
  1835. }
  1836. return hr;
  1837. }
  1838. // set the clock used for timing
  1839. HRESULT
  1840. CCmdQueue::SetSyncSource(__in_opt IReferenceClock* pClock)
  1841. {
  1842. CAutoLock lock(&m_Lock);
  1843. // addref the new clock first in case they are the same
  1844. if (pClock) {
  1845. pClock->AddRef();
  1846. }
  1847. // kill any advise on the old clock
  1848. if (m_pClock) {
  1849. if (m_dwAdvise) {
  1850. m_pClock->Unadvise(m_dwAdvise);
  1851. m_dwAdvise = 0;
  1852. }
  1853. m_pClock->Release();
  1854. }
  1855. m_pClock = pClock;
  1856. // set up a new advise
  1857. SetTimeAdvise();
  1858. return S_OK;
  1859. }
  1860. // set up a timer event with the reference clock
  1861. void
  1862. CCmdQueue::SetTimeAdvise(void)
  1863. {
  1864. // make sure we have a clock to use
  1865. if (!m_pClock) {
  1866. return;
  1867. }
  1868. // reset the event whenever we are requesting a new signal
  1869. m_evDue.Reset();
  1870. // time 0 is earliest
  1871. CRefTime current;
  1872. // find the earliest presentation time
  1873. POSITION pos = m_listPresentation.GetHeadPosition();
  1874. if (pos != NULL) {
  1875. current = m_listPresentation.GetValid(pos)->GetTime();
  1876. }
  1877. // if we're running, check the stream times too
  1878. if (m_bRunning) {
  1879. CRefTime t;
  1880. pos = m_listStream.GetHeadPosition();
  1881. if (NULL != pos) {
  1882. t = m_listStream.GetValid(pos)->GetTime();
  1883. // add on stream time offset to get presentation time
  1884. t += m_StreamTimeOffset;
  1885. // is this earlier?
  1886. if ((current == TimeZero) || (t < current)) {
  1887. current = t;
  1888. }
  1889. }
  1890. }
  1891. // need to change?
  1892. if ((current > TimeZero) && (current != m_tCurrentAdvise)) {
  1893. if (m_dwAdvise) {
  1894. m_pClock->Unadvise(m_dwAdvise);
  1895. // reset the event whenever we are requesting a new signal
  1896. m_evDue.Reset();
  1897. }
  1898. // ask for time advice - the first two params are either
  1899. // stream time offset and stream time or
  1900. // presentation time and 0. we always use the latter
  1901. HRESULT hr = m_pClock->AdviseTime(
  1902. (REFERENCE_TIME)current,
  1903. TimeZero,
  1904. (HEVENT) HANDLE(m_evDue),
  1905. &m_dwAdvise);
  1906. ASSERT(SUCCEEDED(hr));
  1907. m_tCurrentAdvise = current;
  1908. }
  1909. }
  1910. // switch to run mode. Streamtime to Presentation time mapping known.
  1911. HRESULT
  1912. CCmdQueue::Run(REFERENCE_TIME tStreamTimeOffset)
  1913. {
  1914. CAutoLock lock(&m_Lock);
  1915. m_StreamTimeOffset = tStreamTimeOffset;
  1916. m_bRunning = TRUE;
  1917. // ensure advise is accurate
  1918. SetTimeAdvise();
  1919. return S_OK;
  1920. }
  1921. // switch to Stopped or Paused mode. Time mapping not known.
  1922. HRESULT
  1923. CCmdQueue::EndRun()
  1924. {
  1925. CAutoLock lock(&m_Lock);
  1926. m_bRunning = FALSE;
  1927. // check timer setting - stream times
  1928. SetTimeAdvise();
  1929. return S_OK;
  1930. }
  1931. // return a pointer to the next due command. Blocks for msTimeout
  1932. // milliseconds until there is a due command.
  1933. // Stream-time commands will only become due between Run and Endrun calls.
  1934. // The command remains queued until invoked or cancelled.
  1935. // Returns E_ABORT if timeout occurs, otherwise S_OK (or other error).
  1936. //
  1937. // returns an AddRef'd object
  1938. HRESULT
  1939. CCmdQueue::GetDueCommand(__out CDeferredCommand ** ppCmd, long msTimeout)
  1940. {
  1941. // loop until we timeout or find a due command
  1942. for (;;) {
  1943. {
  1944. CAutoLock lock(&m_Lock);
  1945. // find the earliest command
  1946. CDeferredCommand * pCmd = NULL;
  1947. // check the presentation time and the
  1948. // stream time list to find the earliest
  1949. POSITION pos = m_listPresentation.GetHeadPosition();
  1950. if (NULL != pos) {
  1951. pCmd = m_listPresentation.GetValid(pos);
  1952. }
  1953. if (m_bRunning) {
  1954. pos = m_listStream.GetHeadPosition();
  1955. if (NULL != pos) {
  1956. CDeferredCommand* pStrm = m_listStream.GetValid(pos);
  1957. CRefTime t = pStrm->GetTime() + m_StreamTimeOffset;
  1958. if (!pCmd || (t < pCmd->GetTime())) {
  1959. pCmd = pStrm;
  1960. }
  1961. }
  1962. }
  1963. // if we have found one, is it due?
  1964. if (pCmd) {
  1965. if (CheckTime(pCmd->GetTime(), pCmd->IsStreamTime())) {
  1966. // yes it's due - addref it
  1967. pCmd->AddRef();
  1968. *ppCmd = pCmd;
  1969. return S_OK;
  1970. }
  1971. }
  1972. }
  1973. // block until the advise is signalled
  1974. if (WaitForSingleObject(m_evDue, msTimeout) != WAIT_OBJECT_0) {
  1975. return E_ABORT;
  1976. }
  1977. }
  1978. }
  1979. // return a pointer to a command that will be due for a given time.
  1980. // Pass in a stream time here. The stream time offset will be passed
  1981. // in via the Run method.
  1982. // Commands remain queued until invoked or cancelled.
  1983. // This method will not block. It will report E_ABORT if there are no
  1984. // commands due yet.
  1985. //
  1986. // returns an AddRef'd object
  1987. HRESULT
  1988. CCmdQueue::GetCommandDueFor(REFERENCE_TIME rtStream, __out CDeferredCommand**ppCmd)
  1989. {
  1990. CAutoLock lock(&m_Lock);
  1991. CRefTime tStream(rtStream);
  1992. // find the earliest stream and presentation time commands
  1993. CDeferredCommand* pStream = NULL;
  1994. POSITION pos = m_listStream.GetHeadPosition();
  1995. if (NULL != pos) {
  1996. pStream = m_listStream.GetValid(pos);
  1997. }
  1998. CDeferredCommand* pPresent = NULL;
  1999. pos = m_listPresentation.GetHeadPosition();
  2000. if (NULL != pos) {
  2001. pPresent = m_listPresentation.GetValid(pos);
  2002. }
  2003. // is there a presentation time that has passed already
  2004. if (pPresent && CheckTime(pPresent->GetTime(), FALSE)) {
  2005. pPresent->AddRef();
  2006. *ppCmd = pPresent;
  2007. return S_OK;
  2008. }
  2009. // is there a stream time command due before this stream time
  2010. if (pStream && (pStream->GetTime() <= tStream)) {
  2011. pStream->AddRef();
  2012. *ppCmd = pStream;
  2013. return S_OK;
  2014. }
  2015. // if we are running, we can map presentation times to
  2016. // stream time. In this case, is there a presentation time command
  2017. // that will be due before this stream time is presented?
  2018. if (m_bRunning && pPresent) {
  2019. // this stream time will appear at...
  2020. tStream += m_StreamTimeOffset;
  2021. // due before that?
  2022. if (pPresent->GetTime() <= tStream) {
  2023. *ppCmd = pPresent;
  2024. return S_OK;
  2025. }
  2026. }
  2027. // no commands due yet
  2028. return VFW_E_NOT_FOUND;
  2029. }