filter.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Generic Implementation of IBaseFilter Interface
  3. *
  4. * Copyright 2010 Aric Stewart, CodeWeavers
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "strmbase_private.h"
  21. WINE_DEFAULT_DEBUG_CHANNEL(quartz);
  22. struct enum_pins
  23. {
  24. IEnumPins IEnumPins_iface;
  25. LONG refcount;
  26. unsigned int index, count;
  27. int version;
  28. struct strmbase_filter *filter;
  29. };
  30. static const IEnumPinsVtbl enum_pins_vtbl;
  31. static HRESULT enum_pins_create(struct strmbase_filter *filter, IEnumPins **out)
  32. {
  33. struct enum_pins *object;
  34. if (!out)
  35. return E_POINTER;
  36. if (!(object = heap_alloc_zero(sizeof(*object))))
  37. {
  38. *out = NULL;
  39. return E_OUTOFMEMORY;
  40. }
  41. object->IEnumPins_iface.lpVtbl = &enum_pins_vtbl;
  42. object->refcount = 1;
  43. object->filter = filter;
  44. IBaseFilter_AddRef(&filter->IBaseFilter_iface);
  45. object->version = filter->pin_version;
  46. while (filter->ops->filter_get_pin(filter, object->count))
  47. ++object->count;
  48. TRACE("Created enumerator %p.\n", object);
  49. *out = &object->IEnumPins_iface;
  50. return S_OK;
  51. }
  52. static inline struct enum_pins *impl_from_IEnumPins(IEnumPins *iface)
  53. {
  54. return CONTAINING_RECORD(iface, struct enum_pins, IEnumPins_iface);
  55. }
  56. static HRESULT WINAPI enum_pins_QueryInterface(IEnumPins *iface, REFIID iid, void **out)
  57. {
  58. TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
  59. if (IsEqualGUID(iid, &IID_IUnknown) || IsEqualGUID(iid, &IID_IEnumPins))
  60. {
  61. IEnumPins_AddRef(iface);
  62. *out = iface;
  63. return S_OK;
  64. }
  65. WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
  66. *out = NULL;
  67. return E_NOINTERFACE;
  68. }
  69. static ULONG WINAPI enum_pins_AddRef(IEnumPins *iface)
  70. {
  71. struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
  72. ULONG refcount = InterlockedIncrement(&enum_pins->refcount);
  73. TRACE("%p increasing refcount to %u.\n", enum_pins, refcount);
  74. return refcount;
  75. }
  76. static ULONG WINAPI enum_pins_Release(IEnumPins *iface)
  77. {
  78. struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
  79. ULONG refcount = InterlockedDecrement(&enum_pins->refcount);
  80. TRACE("%p decreasing refcount to %u.\n", enum_pins, refcount);
  81. if (!refcount)
  82. {
  83. IBaseFilter_Release(&enum_pins->filter->IBaseFilter_iface);
  84. heap_free(enum_pins);
  85. }
  86. return refcount;
  87. }
  88. static HRESULT WINAPI enum_pins_Next(IEnumPins *iface, ULONG count, IPin **pins, ULONG *ret_count)
  89. {
  90. struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
  91. unsigned int i;
  92. TRACE("iface %p, count %u, pins %p, ret_count %p.\n", iface, count, pins, ret_count);
  93. if (!pins)
  94. return E_POINTER;
  95. if (count > 1 && !ret_count)
  96. return E_INVALIDARG;
  97. if (ret_count)
  98. *ret_count = 0;
  99. if (enum_pins->version != enum_pins->filter->pin_version)
  100. return S_FALSE;
  101. for (i = 0; i < count; ++i)
  102. {
  103. struct strmbase_pin *pin = enum_pins->filter->ops->filter_get_pin(enum_pins->filter, enum_pins->index + i);
  104. if (!pin)
  105. break;
  106. IPin_AddRef(pins[i] = &pin->IPin_iface);
  107. }
  108. if (ret_count)
  109. *ret_count = i;
  110. enum_pins->index += i;
  111. return i == count ? S_OK : S_FALSE;
  112. }
  113. static HRESULT WINAPI enum_pins_Skip(IEnumPins *iface, ULONG count)
  114. {
  115. struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
  116. TRACE("iface %p, count %u.\n", iface, count);
  117. if (enum_pins->version != enum_pins->filter->pin_version)
  118. return VFW_E_ENUM_OUT_OF_SYNC;
  119. if (enum_pins->index + count > enum_pins->count)
  120. return S_FALSE;
  121. enum_pins->index += count;
  122. return S_OK;
  123. }
  124. static HRESULT WINAPI enum_pins_Reset(IEnumPins *iface)
  125. {
  126. struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
  127. TRACE("iface %p.\n", iface);
  128. if (enum_pins->version != enum_pins->filter->pin_version)
  129. {
  130. enum_pins->count = 0;
  131. while (enum_pins->filter->ops->filter_get_pin(enum_pins->filter, enum_pins->count))
  132. ++enum_pins->count;
  133. }
  134. enum_pins->version = enum_pins->filter->pin_version;
  135. enum_pins->index = 0;
  136. return S_OK;
  137. }
  138. static HRESULT WINAPI enum_pins_Clone(IEnumPins *iface, IEnumPins **out)
  139. {
  140. struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
  141. HRESULT hr;
  142. TRACE("iface %p, out %p.\n", iface, out);
  143. if (FAILED(hr = enum_pins_create(enum_pins->filter, out)))
  144. return hr;
  145. return IEnumPins_Skip(*out, enum_pins->index);
  146. }
  147. static const IEnumPinsVtbl enum_pins_vtbl =
  148. {
  149. enum_pins_QueryInterface,
  150. enum_pins_AddRef,
  151. enum_pins_Release,
  152. enum_pins_Next,
  153. enum_pins_Skip,
  154. enum_pins_Reset,
  155. enum_pins_Clone,
  156. };
  157. static inline struct strmbase_filter *impl_from_IUnknown(IUnknown *iface)
  158. {
  159. return CONTAINING_RECORD(iface, struct strmbase_filter, IUnknown_inner);
  160. }
  161. static HRESULT WINAPI filter_inner_QueryInterface(IUnknown *iface, REFIID iid, void **out)
  162. {
  163. struct strmbase_filter *filter = impl_from_IUnknown(iface);
  164. HRESULT hr;
  165. TRACE("filter %p %s, iid %s, out %p.\n", filter, debugstr_w(filter->name), debugstr_guid(iid), out);
  166. *out = NULL;
  167. if (filter->ops->filter_query_interface
  168. && SUCCEEDED(hr = filter->ops->filter_query_interface(filter, iid, out)))
  169. {
  170. return hr;
  171. }
  172. if (IsEqualIID(iid, &IID_IUnknown))
  173. *out = iface;
  174. else if (IsEqualIID(iid, &IID_IPersist)
  175. || IsEqualIID(iid, &IID_IMediaFilter)
  176. || IsEqualIID(iid, &IID_IBaseFilter))
  177. {
  178. *out = &filter->IBaseFilter_iface;
  179. }
  180. else
  181. {
  182. WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
  183. return E_NOINTERFACE;
  184. }
  185. IUnknown_AddRef((IUnknown *)*out);
  186. return S_OK;
  187. }
  188. static ULONG WINAPI filter_inner_AddRef(IUnknown *iface)
  189. {
  190. struct strmbase_filter *filter = impl_from_IUnknown(iface);
  191. ULONG refcount = InterlockedIncrement(&filter->refcount);
  192. TRACE("%p increasing refcount to %u.\n", filter, refcount);
  193. return refcount;
  194. }
  195. static ULONG WINAPI filter_inner_Release(IUnknown *iface)
  196. {
  197. struct strmbase_filter *filter = impl_from_IUnknown(iface);
  198. ULONG refcount = InterlockedDecrement(&filter->refcount);
  199. TRACE("%p decreasing refcount to %u.\n", filter, refcount);
  200. if (!refcount)
  201. filter->ops->filter_destroy(filter);
  202. return refcount;
  203. }
  204. static const IUnknownVtbl filter_inner_vtbl =
  205. {
  206. filter_inner_QueryInterface,
  207. filter_inner_AddRef,
  208. filter_inner_Release,
  209. };
  210. static inline struct strmbase_filter *impl_from_IBaseFilter(IBaseFilter *iface)
  211. {
  212. return CONTAINING_RECORD(iface, struct strmbase_filter, IBaseFilter_iface);
  213. }
  214. static HRESULT WINAPI filter_QueryInterface(IBaseFilter *iface, REFIID iid, void **out)
  215. {
  216. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  217. return IUnknown_QueryInterface(filter->outer_unk, iid, out);
  218. }
  219. static ULONG WINAPI filter_AddRef(IBaseFilter *iface)
  220. {
  221. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  222. return IUnknown_AddRef(filter->outer_unk);
  223. }
  224. static ULONG WINAPI filter_Release(IBaseFilter *iface)
  225. {
  226. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  227. return IUnknown_Release(filter->outer_unk);
  228. }
  229. static HRESULT WINAPI filter_GetClassID(IBaseFilter *iface, CLSID *clsid)
  230. {
  231. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  232. TRACE("filter %p %s, clsid %p.\n", filter, debugstr_w(filter->name), clsid);
  233. *clsid = filter->clsid;
  234. return S_OK;
  235. }
  236. static HRESULT WINAPI filter_Stop(IBaseFilter *iface)
  237. {
  238. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  239. HRESULT hr = S_OK;
  240. TRACE("filter %p %s.\n", filter, debugstr_w(filter->name));
  241. EnterCriticalSection(&filter->filter_cs);
  242. if (filter->state == State_Running && filter->ops->filter_stop_stream)
  243. hr = filter->ops->filter_stop_stream(filter);
  244. if (SUCCEEDED(hr) && filter->ops->filter_cleanup_stream)
  245. hr = filter->ops->filter_cleanup_stream(filter);
  246. if (SUCCEEDED(hr))
  247. filter->state = State_Stopped;
  248. LeaveCriticalSection(&filter->filter_cs);
  249. return hr;
  250. }
  251. static HRESULT WINAPI filter_Pause(IBaseFilter *iface)
  252. {
  253. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  254. HRESULT hr = S_OK;
  255. TRACE("filter %p %s.\n", filter, debugstr_w(filter->name));
  256. EnterCriticalSection(&filter->filter_cs);
  257. if (filter->state == State_Stopped && filter->ops->filter_init_stream)
  258. hr = filter->ops->filter_init_stream(filter);
  259. else if (filter->state == State_Running && filter->ops->filter_stop_stream)
  260. hr = filter->ops->filter_stop_stream(filter);
  261. if (SUCCEEDED(hr))
  262. filter->state = State_Paused;
  263. LeaveCriticalSection(&filter->filter_cs);
  264. return hr;
  265. }
  266. static HRESULT WINAPI filter_Run(IBaseFilter *iface, REFERENCE_TIME start)
  267. {
  268. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  269. HRESULT hr = S_OK;
  270. TRACE("filter %p %s, start %s.\n", filter, debugstr_w(filter->name), debugstr_time(start));
  271. EnterCriticalSection(&filter->filter_cs);
  272. if (filter->state == State_Stopped && filter->ops->filter_init_stream)
  273. hr = filter->ops->filter_init_stream(filter);
  274. if (SUCCEEDED(hr) && filter->ops->filter_start_stream)
  275. hr = filter->ops->filter_start_stream(filter, start);
  276. if (SUCCEEDED(hr))
  277. filter->state = State_Running;
  278. LeaveCriticalSection(&filter->filter_cs);
  279. return hr;
  280. }
  281. static HRESULT WINAPI filter_GetState(IBaseFilter *iface, DWORD timeout, FILTER_STATE *state)
  282. {
  283. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  284. HRESULT hr = S_OK;
  285. TRACE("filter %p %s, timeout %u, state %p.\n", filter, debugstr_w(filter->name), timeout, state);
  286. EnterCriticalSection(&filter->filter_cs);
  287. if (filter->ops->filter_wait_state)
  288. hr = filter->ops->filter_wait_state(filter, timeout);
  289. *state = filter->state;
  290. LeaveCriticalSection(&filter->filter_cs);
  291. return hr;
  292. }
  293. static HRESULT WINAPI filter_SetSyncSource(IBaseFilter *iface, IReferenceClock *clock)
  294. {
  295. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  296. TRACE("filter %p %s, clock %p.\n", filter, debugstr_w(filter->name), clock);
  297. EnterCriticalSection(&filter->filter_cs);
  298. if (filter->clock)
  299. IReferenceClock_Release(filter->clock);
  300. filter->clock = clock;
  301. if (filter->clock)
  302. IReferenceClock_AddRef(filter->clock);
  303. LeaveCriticalSection(&filter->filter_cs);
  304. return S_OK;
  305. }
  306. static HRESULT WINAPI filter_GetSyncSource(IBaseFilter *iface, IReferenceClock **clock)
  307. {
  308. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  309. TRACE("filter %p %s, clock %p.\n", filter, debugstr_w(filter->name), clock);
  310. EnterCriticalSection(&filter->filter_cs);
  311. *clock = filter->clock;
  312. if (filter->clock)
  313. IReferenceClock_AddRef(filter->clock);
  314. LeaveCriticalSection(&filter->filter_cs);
  315. return S_OK;
  316. }
  317. static HRESULT WINAPI filter_EnumPins(IBaseFilter *iface, IEnumPins **enum_pins)
  318. {
  319. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  320. TRACE("filter %p %s, enum_pins %p.\n", filter, debugstr_w(filter->name), enum_pins);
  321. return enum_pins_create(filter, enum_pins);
  322. }
  323. static HRESULT WINAPI filter_FindPin(IBaseFilter *iface, const WCHAR *id, IPin **ret)
  324. {
  325. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  326. struct strmbase_pin *pin;
  327. unsigned int i;
  328. TRACE("filter %p %s, id %s, ret %p.\n", filter, debugstr_w(filter->name), debugstr_w(id), ret);
  329. for (i = 0; (pin = filter->ops->filter_get_pin(filter, i)); ++i)
  330. {
  331. if (!lstrcmpW(id, pin->name))
  332. {
  333. IPin_AddRef(*ret = &pin->IPin_iface);
  334. return S_OK;
  335. }
  336. }
  337. return VFW_E_NOT_FOUND;
  338. }
  339. static HRESULT WINAPI filter_QueryFilterInfo(IBaseFilter *iface, FILTER_INFO *info)
  340. {
  341. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  342. TRACE("filter %p %s, info %p.\n", filter, debugstr_w(filter->name), info);
  343. lstrcpyW(info->achName, filter->name);
  344. info->pGraph = filter->graph;
  345. if (info->pGraph)
  346. IFilterGraph_AddRef(info->pGraph);
  347. return S_OK;
  348. }
  349. static HRESULT WINAPI filter_JoinFilterGraph(IBaseFilter *iface, IFilterGraph *graph, const WCHAR *name)
  350. {
  351. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  352. TRACE("filter %p %s, graph %p, name %s.\n", filter, debugstr_w(filter->name), graph, debugstr_w(name));
  353. EnterCriticalSection(&filter->filter_cs);
  354. if (name)
  355. lstrcpynW(filter->name, name, ARRAY_SIZE(filter->name));
  356. else
  357. filter->name[0] = 0;
  358. /* The graph references us, so we cannot also reference the graph. */
  359. filter->graph = graph;
  360. LeaveCriticalSection(&filter->filter_cs);
  361. return S_OK;
  362. }
  363. static HRESULT WINAPI filter_QueryVendorInfo(IBaseFilter *iface, WCHAR **info)
  364. {
  365. struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
  366. TRACE("filter %p %s, info %p.\n", filter, debugstr_w(filter->name), info);
  367. return E_NOTIMPL;
  368. }
  369. static const IBaseFilterVtbl filter_vtbl =
  370. {
  371. filter_QueryInterface,
  372. filter_AddRef,
  373. filter_Release,
  374. filter_GetClassID,
  375. filter_Stop,
  376. filter_Pause,
  377. filter_Run,
  378. filter_GetState,
  379. filter_SetSyncSource,
  380. filter_GetSyncSource,
  381. filter_EnumPins,
  382. filter_FindPin,
  383. filter_QueryFilterInfo,
  384. filter_JoinFilterGraph,
  385. filter_QueryVendorInfo,
  386. };
  387. VOID WINAPI BaseFilterImpl_IncrementPinVersion(struct strmbase_filter *filter)
  388. {
  389. InterlockedIncrement(&filter->pin_version);
  390. }
  391. void strmbase_filter_init(struct strmbase_filter *filter, IUnknown *outer,
  392. const CLSID *clsid, const struct strmbase_filter_ops *ops)
  393. {
  394. memset(filter, 0, sizeof(*filter));
  395. filter->IBaseFilter_iface.lpVtbl = &filter_vtbl;
  396. filter->IUnknown_inner.lpVtbl = &filter_inner_vtbl;
  397. filter->outer_unk = outer ? outer : &filter->IUnknown_inner;
  398. filter->refcount = 1;
  399. InitializeCriticalSection(&filter->filter_cs);
  400. if (filter->filter_cs.DebugInfo != (RTL_CRITICAL_SECTION_DEBUG *)-1)
  401. filter->filter_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": strmbase_filter.filter_cs");
  402. InitializeCriticalSection(&filter->stream_cs);
  403. if (filter->stream_cs.DebugInfo != (RTL_CRITICAL_SECTION_DEBUG *)-1)
  404. filter->stream_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": strmbase_filter.stream_cs");
  405. filter->clsid = *clsid;
  406. filter->pin_version = 1;
  407. filter->ops = ops;
  408. }
  409. void strmbase_filter_cleanup(struct strmbase_filter *filter)
  410. {
  411. if (filter->clock)
  412. IReferenceClock_Release(filter->clock);
  413. filter->IBaseFilter_iface.lpVtbl = NULL;
  414. if (filter->filter_cs.DebugInfo != (RTL_CRITICAL_SECTION_DEBUG *)-1)
  415. filter->filter_cs.DebugInfo->Spare[0] = 0;
  416. DeleteCriticalSection(&filter->filter_cs);
  417. if (filter->stream_cs.DebugInfo != (RTL_CRITICAL_SECTION_DEBUG *)-1)
  418. filter->stream_cs.DebugInfo->Spare[0] = 0;
  419. DeleteCriticalSection(&filter->stream_cs);
  420. }