ctlutil.cpp 54 KB

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