pin.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. /*
  2. * Generic Implementation of IPin Interface
  3. *
  4. * Copyright 2003 Robert Shearman
  5. * Copyright 2010 Aric Stewart, CodeWeavers
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include "strmbase_private.h"
  22. WINE_DEFAULT_DEBUG_CHANNEL(quartz);
  23. static const IMemInputPinVtbl MemInputPin_Vtbl;
  24. typedef HRESULT (*SendPinFunc)( IPin *to, LPVOID arg );
  25. struct enum_media_types
  26. {
  27. IEnumMediaTypes IEnumMediaTypes_iface;
  28. LONG refcount;
  29. unsigned int index, count;
  30. struct strmbase_pin *pin;
  31. };
  32. static const IEnumMediaTypesVtbl enum_media_types_vtbl;
  33. static HRESULT enum_media_types_create(struct strmbase_pin *pin, IEnumMediaTypes **out)
  34. {
  35. struct enum_media_types *object;
  36. AM_MEDIA_TYPE mt;
  37. if (!out)
  38. return E_POINTER;
  39. if (!(object = heap_alloc_zero(sizeof(*object))))
  40. {
  41. *out = NULL;
  42. return E_OUTOFMEMORY;
  43. }
  44. object->IEnumMediaTypes_iface.lpVtbl = &enum_media_types_vtbl;
  45. object->refcount = 1;
  46. object->pin = pin;
  47. IPin_AddRef(&pin->IPin_iface);
  48. if (pin->ops->pin_get_media_type)
  49. {
  50. while (pin->ops->pin_get_media_type(pin, object->count, &mt) == S_OK)
  51. {
  52. FreeMediaType(&mt);
  53. ++object->count;
  54. }
  55. }
  56. TRACE("Created enumerator %p.\n", object);
  57. *out = &object->IEnumMediaTypes_iface;
  58. return S_OK;
  59. }
  60. static struct enum_media_types *impl_from_IEnumMediaTypes(IEnumMediaTypes *iface)
  61. {
  62. return CONTAINING_RECORD(iface, struct enum_media_types, IEnumMediaTypes_iface);
  63. }
  64. static HRESULT WINAPI enum_media_types_QueryInterface(IEnumMediaTypes *iface, REFIID iid, void **out)
  65. {
  66. TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
  67. if (IsEqualGUID(iid, &IID_IUnknown) || IsEqualGUID(iid, &IID_IEnumMediaTypes))
  68. {
  69. IEnumMediaTypes_AddRef(iface);
  70. *out = iface;
  71. return S_OK;
  72. }
  73. WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
  74. *out = NULL;
  75. return E_NOINTERFACE;
  76. }
  77. static ULONG WINAPI enum_media_types_AddRef(IEnumMediaTypes *iface)
  78. {
  79. struct enum_media_types *enummt = impl_from_IEnumMediaTypes(iface);
  80. ULONG refcount = InterlockedIncrement(&enummt->refcount);
  81. TRACE("%p increasing refcount to %lu.\n", enummt, refcount);
  82. return refcount;
  83. }
  84. static ULONG WINAPI enum_media_types_Release(IEnumMediaTypes *iface)
  85. {
  86. struct enum_media_types *enummt = impl_from_IEnumMediaTypes(iface);
  87. ULONG refcount = InterlockedDecrement(&enummt->refcount);
  88. TRACE("%p decreasing refcount to %lu.\n", enummt, refcount);
  89. if (!refcount)
  90. {
  91. IPin_Release(&enummt->pin->IPin_iface);
  92. heap_free(enummt);
  93. }
  94. return refcount;
  95. }
  96. static HRESULT WINAPI enum_media_types_Next(IEnumMediaTypes *iface, ULONG count,
  97. AM_MEDIA_TYPE **mts, ULONG *ret_count)
  98. {
  99. struct enum_media_types *enummt = impl_from_IEnumMediaTypes(iface);
  100. AM_MEDIA_TYPE mt;
  101. unsigned int i;
  102. HRESULT hr;
  103. TRACE("enummt %p, count %lu, mts %p, ret_count %p.\n", enummt, count, mts, ret_count);
  104. if (!enummt->pin->ops->pin_get_media_type)
  105. {
  106. if (ret_count)
  107. *ret_count = 0;
  108. return count ? S_FALSE : S_OK;
  109. }
  110. for (i = 0; i < count; ++i)
  111. {
  112. hr = enummt->pin->ops->pin_get_media_type(enummt->pin, enummt->index + i, &mt);
  113. if (hr == S_OK)
  114. {
  115. if ((mts[i] = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE))))
  116. *mts[i] = mt;
  117. else
  118. hr = E_OUTOFMEMORY;
  119. }
  120. if (FAILED(hr))
  121. {
  122. while (i--)
  123. DeleteMediaType(mts[i]);
  124. *ret_count = 0;
  125. return E_OUTOFMEMORY;
  126. }
  127. else if (hr != S_OK)
  128. break;
  129. if (TRACE_ON(quartz))
  130. {
  131. TRACE("Returning media type %u:\n", enummt->index + i);
  132. strmbase_dump_media_type(mts[i]);
  133. }
  134. }
  135. if (count != 1 || ret_count)
  136. *ret_count = i;
  137. enummt->index += i;
  138. return i == count ? S_OK : S_FALSE;
  139. }
  140. static HRESULT WINAPI enum_media_types_Skip(IEnumMediaTypes *iface, ULONG count)
  141. {
  142. struct enum_media_types *enummt = impl_from_IEnumMediaTypes(iface);
  143. TRACE("enummt %p, count %lu.\n", enummt, count);
  144. enummt->index += count;
  145. return enummt->index > enummt->count ? S_FALSE : S_OK;
  146. }
  147. static HRESULT WINAPI enum_media_types_Reset(IEnumMediaTypes *iface)
  148. {
  149. struct enum_media_types *enummt = impl_from_IEnumMediaTypes(iface);
  150. AM_MEDIA_TYPE mt;
  151. TRACE("enummt %p.\n", enummt);
  152. enummt->count = 0;
  153. if (enummt->pin->ops->pin_get_media_type)
  154. {
  155. while (enummt->pin->ops->pin_get_media_type(enummt->pin, enummt->count, &mt) == S_OK)
  156. {
  157. FreeMediaType(&mt);
  158. ++enummt->count;
  159. }
  160. }
  161. enummt->index = 0;
  162. return S_OK;
  163. }
  164. static HRESULT WINAPI enum_media_types_Clone(IEnumMediaTypes *iface, IEnumMediaTypes **out)
  165. {
  166. struct enum_media_types *enummt = impl_from_IEnumMediaTypes(iface);
  167. HRESULT hr;
  168. TRACE("enummt %p, out %p.\n", enummt, out);
  169. if (FAILED(hr = enum_media_types_create(enummt->pin, out)))
  170. return hr;
  171. return IEnumMediaTypes_Skip(*out, enummt->index);
  172. }
  173. static const IEnumMediaTypesVtbl enum_media_types_vtbl =
  174. {
  175. enum_media_types_QueryInterface,
  176. enum_media_types_AddRef,
  177. enum_media_types_Release,
  178. enum_media_types_Next,
  179. enum_media_types_Skip,
  180. enum_media_types_Reset,
  181. enum_media_types_Clone,
  182. };
  183. static inline struct strmbase_pin *impl_from_IPin(IPin *iface)
  184. {
  185. return CONTAINING_RECORD(iface, struct strmbase_pin, IPin_iface);
  186. }
  187. /** Helper function, there are a lot of places where the error code is inherited
  188. * The following rules apply:
  189. *
  190. * Return the first received error code (E_NOTIMPL is ignored)
  191. * If no errors occur: return the first received non-error-code that isn't S_OK
  192. */
  193. static HRESULT updatehres( HRESULT original, HRESULT new )
  194. {
  195. if (FAILED( original ) || new == E_NOTIMPL)
  196. return original;
  197. if (FAILED( new ) || original == S_OK)
  198. return new;
  199. return original;
  200. }
  201. /** Sends a message from a pin further to other, similar pins
  202. * fnMiddle is called on each pin found further on the stream.
  203. * fnEnd (can be NULL) is called when the message can't be sent any further (this is a renderer or source)
  204. *
  205. * If the pin given is an input pin, the message will be sent downstream to other input pins
  206. * If the pin given is an output pin, the message will be sent upstream to other output pins
  207. */
  208. static HRESULT SendFurther(struct strmbase_sink *sink, SendPinFunc func, void *arg)
  209. {
  210. struct strmbase_pin *pin;
  211. HRESULT hr = S_OK;
  212. unsigned int i;
  213. for (i = 0; (pin = sink->pin.filter->ops->filter_get_pin(sink->pin.filter, i)); ++i)
  214. {
  215. if (pin->dir == PINDIR_OUTPUT && pin->peer)
  216. hr = updatehres(hr, func(pin->peer, arg));
  217. }
  218. return hr;
  219. }
  220. static HRESULT WINAPI pin_QueryInterface(IPin *iface, REFIID iid, void **out)
  221. {
  222. struct strmbase_pin *pin = impl_from_IPin(iface);
  223. HRESULT hr;
  224. TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
  225. *out = NULL;
  226. if (pin->ops->pin_query_interface && SUCCEEDED(hr = pin->ops->pin_query_interface(pin, iid, out)))
  227. return hr;
  228. if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IPin))
  229. *out = iface;
  230. else
  231. {
  232. WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
  233. return E_NOINTERFACE;
  234. }
  235. IUnknown_AddRef((IUnknown *)*out);
  236. return S_OK;
  237. }
  238. static ULONG WINAPI pin_AddRef(IPin *iface)
  239. {
  240. struct strmbase_pin *pin = impl_from_IPin(iface);
  241. return IBaseFilter_AddRef(&pin->filter->IBaseFilter_iface);
  242. }
  243. static ULONG WINAPI pin_Release(IPin *iface)
  244. {
  245. struct strmbase_pin *pin = impl_from_IPin(iface);
  246. return IBaseFilter_Release(&pin->filter->IBaseFilter_iface);
  247. }
  248. static HRESULT WINAPI pin_ConnectedTo(IPin * iface, IPin ** ppPin)
  249. {
  250. struct strmbase_pin *This = impl_from_IPin(iface);
  251. HRESULT hr;
  252. TRACE("pin %p %s:%s, peer %p.\n", This, debugstr_w(This->filter->name), debugstr_w(This->name), ppPin);
  253. EnterCriticalSection(&This->filter->filter_cs);
  254. {
  255. if (This->peer)
  256. {
  257. *ppPin = This->peer;
  258. IPin_AddRef(*ppPin);
  259. hr = S_OK;
  260. }
  261. else
  262. {
  263. hr = VFW_E_NOT_CONNECTED;
  264. *ppPin = NULL;
  265. }
  266. }
  267. LeaveCriticalSection(&This->filter->filter_cs);
  268. return hr;
  269. }
  270. static HRESULT WINAPI pin_ConnectionMediaType(IPin *iface, AM_MEDIA_TYPE *pmt)
  271. {
  272. struct strmbase_pin *This = impl_from_IPin(iface);
  273. HRESULT hr;
  274. TRACE("pin %p %s:%s, pmt %p.\n", This, debugstr_w(This->filter->name), debugstr_w(This->name), pmt);
  275. EnterCriticalSection(&This->filter->filter_cs);
  276. {
  277. if (This->peer)
  278. {
  279. CopyMediaType(pmt, &This->mt);
  280. strmbase_dump_media_type(pmt);
  281. hr = S_OK;
  282. }
  283. else
  284. {
  285. ZeroMemory(pmt, sizeof(*pmt));
  286. hr = VFW_E_NOT_CONNECTED;
  287. }
  288. }
  289. LeaveCriticalSection(&This->filter->filter_cs);
  290. return hr;
  291. }
  292. static HRESULT WINAPI pin_QueryPinInfo(IPin *iface, PIN_INFO *info)
  293. {
  294. struct strmbase_pin *pin = impl_from_IPin(iface);
  295. TRACE("pin %p %s:%s, info %p.\n", pin, debugstr_w(pin->filter->name), debugstr_w(pin->name), info);
  296. info->dir = pin->dir;
  297. IBaseFilter_AddRef(info->pFilter = &pin->filter->IBaseFilter_iface);
  298. lstrcpyW(info->achName, pin->name);
  299. return S_OK;
  300. }
  301. static HRESULT WINAPI pin_QueryDirection(IPin *iface, PIN_DIRECTION *dir)
  302. {
  303. struct strmbase_pin *pin = impl_from_IPin(iface);
  304. TRACE("pin %p %s:%s, dir %p.\n", pin, debugstr_w(pin->filter->name), debugstr_w(pin->name), dir);
  305. *dir = pin->dir;
  306. return S_OK;
  307. }
  308. static HRESULT WINAPI pin_QueryId(IPin *iface, WCHAR **id)
  309. {
  310. struct strmbase_pin *pin = impl_from_IPin(iface);
  311. TRACE("pin %p %s:%s, id %p.\n", pin, debugstr_w(pin->filter->name), debugstr_w(pin->name), id);
  312. if (!(*id = CoTaskMemAlloc((lstrlenW(pin->id) + 1) * sizeof(WCHAR))))
  313. return E_OUTOFMEMORY;
  314. lstrcpyW(*id, pin->id);
  315. return S_OK;
  316. }
  317. static BOOL query_accept(struct strmbase_pin *pin, const AM_MEDIA_TYPE *mt)
  318. {
  319. if (pin->ops->pin_query_accept && pin->ops->pin_query_accept(pin, mt) != S_OK)
  320. return FALSE;
  321. return TRUE;
  322. }
  323. static HRESULT WINAPI pin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *mt)
  324. {
  325. struct strmbase_pin *pin = impl_from_IPin(iface);
  326. TRACE("pin %p %s:%s, mt %p.\n", pin, debugstr_w(pin->filter->name), debugstr_w(pin->name), mt);
  327. strmbase_dump_media_type(mt);
  328. return query_accept(pin, mt) ? S_OK : S_FALSE;
  329. }
  330. static HRESULT WINAPI pin_EnumMediaTypes(IPin *iface, IEnumMediaTypes **enum_media_types)
  331. {
  332. struct strmbase_pin *pin = impl_from_IPin(iface);
  333. AM_MEDIA_TYPE mt;
  334. HRESULT hr;
  335. TRACE("pin %p %s:%s, enum_media_types %p.\n", pin, debugstr_w(pin->filter->name),
  336. debugstr_w(pin->name), enum_media_types);
  337. if (pin->ops->pin_get_media_type)
  338. {
  339. if (FAILED(hr = pin->ops->pin_get_media_type(pin, 0, &mt)))
  340. return hr;
  341. if (hr == S_OK)
  342. FreeMediaType(&mt);
  343. }
  344. return enum_media_types_create(pin, enum_media_types);
  345. }
  346. static HRESULT WINAPI pin_QueryInternalConnections(IPin *iface, IPin **pins, ULONG *count)
  347. {
  348. struct strmbase_pin *pin = impl_from_IPin(iface);
  349. TRACE("pin %p %s:%s, pins %p, count %p.\n", pin, debugstr_w(pin->filter->name),
  350. debugstr_w(pin->name), pins, count);
  351. return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
  352. }
  353. /*** OutputPin implementation ***/
  354. static inline struct strmbase_source *impl_source_from_IPin( IPin *iface )
  355. {
  356. return CONTAINING_RECORD(iface, struct strmbase_source, pin.IPin_iface);
  357. }
  358. static BOOL compare_media_types(const AM_MEDIA_TYPE *req_mt, const AM_MEDIA_TYPE *pin_mt)
  359. {
  360. if (!req_mt)
  361. return TRUE;
  362. if (!IsEqualGUID(&req_mt->majortype, &pin_mt->majortype)
  363. && !IsEqualGUID(&req_mt->majortype, &GUID_NULL))
  364. return FALSE;
  365. if (!IsEqualGUID(&req_mt->subtype, &pin_mt->subtype)
  366. && !IsEqualGUID(&req_mt->subtype, &GUID_NULL))
  367. return FALSE;
  368. if (!IsEqualGUID(&req_mt->formattype, &pin_mt->formattype)
  369. && !IsEqualGUID(&req_mt->formattype, &GUID_NULL))
  370. return FALSE;
  371. return TRUE;
  372. }
  373. static HRESULT WINAPI source_Connect(IPin *iface, IPin *peer, const AM_MEDIA_TYPE *mt)
  374. {
  375. struct strmbase_source *pin = impl_source_from_IPin(iface);
  376. AM_MEDIA_TYPE candidate, *candidate_ptr;
  377. IEnumMediaTypes *enummt;
  378. PIN_DIRECTION dir;
  379. unsigned int i;
  380. ULONG count;
  381. HRESULT hr;
  382. TRACE("pin %p %s:%s, peer %p, mt %p.\n", pin, debugstr_w(pin->pin.filter->name),
  383. debugstr_w(pin->pin.name), peer, mt);
  384. strmbase_dump_media_type(mt);
  385. if (!peer)
  386. return E_POINTER;
  387. IPin_QueryDirection(peer, &dir);
  388. if (dir != PINDIR_INPUT)
  389. {
  390. WARN("Attempt to connect to another source pin, returning VFW_E_INVALID_DIRECTION.\n");
  391. return VFW_E_INVALID_DIRECTION;
  392. }
  393. EnterCriticalSection(&pin->pin.filter->filter_cs);
  394. if (pin->pin.peer)
  395. {
  396. LeaveCriticalSection(&pin->pin.filter->filter_cs);
  397. WARN("Pin is already connected, returning VFW_E_ALREADY_CONNECTED.\n");
  398. return VFW_E_ALREADY_CONNECTED;
  399. }
  400. if (pin->pin.filter->state != State_Stopped)
  401. {
  402. LeaveCriticalSection(&pin->pin.filter->filter_cs);
  403. WARN("Filter is not stopped; returning VFW_E_NOT_STOPPED.\n");
  404. return VFW_E_NOT_STOPPED;
  405. }
  406. /* We don't check the subtype here. The rationale (as given by the DirectX
  407. * documentation) is that the format type is supposed to provide at least
  408. * as much information as the subtype. */
  409. if (mt && !IsEqualGUID(&mt->majortype, &GUID_NULL)
  410. && !IsEqualGUID(&mt->formattype, &GUID_NULL))
  411. {
  412. hr = pin->pFuncsTable->pfnAttemptConnection(pin, peer, mt);
  413. LeaveCriticalSection(&pin->pin.filter->filter_cs);
  414. return hr;
  415. }
  416. if (SUCCEEDED(IPin_EnumMediaTypes(peer, &enummt)))
  417. {
  418. while (IEnumMediaTypes_Next(enummt, 1, &candidate_ptr, &count) == S_OK)
  419. {
  420. if (compare_media_types(mt, candidate_ptr)
  421. && pin->pFuncsTable->pfnAttemptConnection(pin, peer, candidate_ptr) == S_OK)
  422. {
  423. LeaveCriticalSection(&pin->pin.filter->filter_cs);
  424. DeleteMediaType(candidate_ptr);
  425. IEnumMediaTypes_Release(enummt);
  426. return S_OK;
  427. }
  428. DeleteMediaType(candidate_ptr);
  429. }
  430. IEnumMediaTypes_Release(enummt);
  431. }
  432. if (pin->pFuncsTable->base.pin_get_media_type)
  433. {
  434. for (i = 0; pin->pFuncsTable->base.pin_get_media_type(&pin->pin, i, &candidate) == S_OK; ++i)
  435. {
  436. strmbase_dump_media_type(&candidate);
  437. if (compare_media_types(mt, &candidate)
  438. && pin->pFuncsTable->pfnAttemptConnection(pin, peer, &candidate) == S_OK)
  439. {
  440. LeaveCriticalSection(&pin->pin.filter->filter_cs);
  441. FreeMediaType(&candidate);
  442. return S_OK;
  443. }
  444. FreeMediaType(&candidate);
  445. }
  446. }
  447. LeaveCriticalSection(&pin->pin.filter->filter_cs);
  448. return VFW_E_NO_ACCEPTABLE_TYPES;
  449. }
  450. static HRESULT WINAPI source_ReceiveConnection(IPin *iface, IPin *peer, const AM_MEDIA_TYPE *mt)
  451. {
  452. struct strmbase_source *pin = impl_source_from_IPin(iface);
  453. WARN("pin %p %s:%s, peer %p, mt %p, unexpected call.\n", pin,
  454. debugstr_w(pin->pin.filter->name), debugstr_w(pin->pin.name), peer, mt);
  455. return E_UNEXPECTED;
  456. }
  457. static HRESULT WINAPI source_Disconnect(IPin *iface)
  458. {
  459. HRESULT hr;
  460. struct strmbase_source *This = impl_source_from_IPin(iface);
  461. TRACE("pin %p %s:%s.\n", This, debugstr_w(This->pin.filter->name), debugstr_w(This->pin.name));
  462. EnterCriticalSection(&This->pin.filter->filter_cs);
  463. {
  464. if (This->pin.filter->state != State_Stopped)
  465. {
  466. LeaveCriticalSection(&This->pin.filter->filter_cs);
  467. WARN("Filter is not stopped; returning VFW_E_NOT_STOPPED.\n");
  468. return VFW_E_NOT_STOPPED;
  469. }
  470. if (This->pFuncsTable->source_disconnect)
  471. This->pFuncsTable->source_disconnect(This);
  472. if (This->pMemInputPin)
  473. {
  474. IMemInputPin_Release(This->pMemInputPin);
  475. This->pMemInputPin = NULL;
  476. }
  477. if (This->pAllocator)
  478. {
  479. IMemAllocator_Release(This->pAllocator);
  480. This->pAllocator = NULL;
  481. }
  482. if (This->pin.peer)
  483. {
  484. IPin_Release(This->pin.peer);
  485. This->pin.peer = NULL;
  486. FreeMediaType(&This->pin.mt);
  487. ZeroMemory(&This->pin.mt, sizeof(This->pin.mt));
  488. hr = S_OK;
  489. }
  490. else
  491. hr = S_FALSE;
  492. }
  493. LeaveCriticalSection(&This->pin.filter->filter_cs);
  494. return hr;
  495. }
  496. static HRESULT WINAPI source_EndOfStream(IPin *iface)
  497. {
  498. struct strmbase_source *pin = impl_source_from_IPin(iface);
  499. WARN("pin %p %s:%s, unexpected call.\n", pin, debugstr_w(pin->pin.filter->name), debugstr_w(pin->pin.name));
  500. /* not supposed to do anything in an output pin */
  501. return E_UNEXPECTED;
  502. }
  503. static HRESULT WINAPI source_BeginFlush(IPin *iface)
  504. {
  505. struct strmbase_source *pin = impl_source_from_IPin(iface);
  506. WARN("pin %p %s:%s, unexpected call.\n", pin, debugstr_w(pin->pin.filter->name), debugstr_w(pin->pin.name));
  507. /* not supposed to do anything in an output pin */
  508. return E_UNEXPECTED;
  509. }
  510. static HRESULT WINAPI source_EndFlush(IPin *iface)
  511. {
  512. struct strmbase_source *pin = impl_source_from_IPin(iface);
  513. WARN("pin %p %s:%s, unexpected call.\n", pin, debugstr_w(pin->pin.filter->name), debugstr_w(pin->pin.name));
  514. /* not supposed to do anything in an output pin */
  515. return E_UNEXPECTED;
  516. }
  517. static HRESULT WINAPI source_NewSegment(IPin * iface, REFERENCE_TIME start, REFERENCE_TIME stop, double rate)
  518. {
  519. struct strmbase_source *pin = impl_source_from_IPin(iface);
  520. TRACE("pin %p %s:%s, start %s, stop %s, rate %.16e.\n", pin, debugstr_w(pin->pin.filter->name),
  521. debugstr_w(pin->pin.name), debugstr_time(start), debugstr_time(stop), rate);
  522. return S_OK;
  523. }
  524. static const IPinVtbl source_vtbl =
  525. {
  526. pin_QueryInterface,
  527. pin_AddRef,
  528. pin_Release,
  529. source_Connect,
  530. source_ReceiveConnection,
  531. source_Disconnect,
  532. pin_ConnectedTo,
  533. pin_ConnectionMediaType,
  534. pin_QueryPinInfo,
  535. pin_QueryDirection,
  536. pin_QueryId,
  537. pin_QueryAccept,
  538. pin_EnumMediaTypes,
  539. pin_QueryInternalConnections,
  540. source_EndOfStream,
  541. source_BeginFlush,
  542. source_EndFlush,
  543. source_NewSegment,
  544. };
  545. HRESULT WINAPI BaseOutputPinImpl_DecideAllocator(struct strmbase_source *This,
  546. IMemInputPin *pPin, IMemAllocator **pAlloc)
  547. {
  548. HRESULT hr;
  549. hr = IMemInputPin_GetAllocator(pPin, pAlloc);
  550. if (hr == VFW_E_NO_ALLOCATOR)
  551. hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL,
  552. CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (void **)pAlloc);
  553. if (SUCCEEDED(hr))
  554. {
  555. ALLOCATOR_PROPERTIES rProps;
  556. ZeroMemory(&rProps, sizeof(ALLOCATOR_PROPERTIES));
  557. IMemInputPin_GetAllocatorRequirements(pPin, &rProps);
  558. hr = This->pFuncsTable->pfnDecideBufferSize(This, *pAlloc, &rProps);
  559. }
  560. if (SUCCEEDED(hr))
  561. hr = IMemInputPin_NotifyAllocator(pPin, *pAlloc, FALSE);
  562. return hr;
  563. }
  564. /*** The Construct functions ***/
  565. /* Function called as a helper to IPin_Connect */
  566. /* specific AM_MEDIA_TYPE - it cannot be NULL */
  567. HRESULT WINAPI BaseOutputPinImpl_AttemptConnection(struct strmbase_source *This,
  568. IPin *pReceivePin, const AM_MEDIA_TYPE *pmt)
  569. {
  570. HRESULT hr;
  571. IMemAllocator * pMemAlloc = NULL;
  572. TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
  573. if (!query_accept(&This->pin, pmt))
  574. return VFW_E_TYPE_NOT_ACCEPTED;
  575. This->pin.peer = pReceivePin;
  576. IPin_AddRef(pReceivePin);
  577. CopyMediaType(&This->pin.mt, pmt);
  578. hr = IPin_ReceiveConnection(pReceivePin, &This->pin.IPin_iface, pmt);
  579. /* get the IMemInputPin interface we will use to deliver samples to the
  580. * connected pin */
  581. if (SUCCEEDED(hr))
  582. {
  583. This->pMemInputPin = NULL;
  584. hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
  585. if (SUCCEEDED(hr))
  586. {
  587. hr = This->pFuncsTable->pfnDecideAllocator(This, This->pMemInputPin, &pMemAlloc);
  588. if (SUCCEEDED(hr))
  589. This->pAllocator = pMemAlloc;
  590. else if (pMemAlloc)
  591. IMemAllocator_Release(pMemAlloc);
  592. }
  593. /* break connection if we couldn't get the allocator */
  594. if (FAILED(hr))
  595. {
  596. if (This->pMemInputPin)
  597. IMemInputPin_Release(This->pMemInputPin);
  598. This->pMemInputPin = NULL;
  599. IPin_Disconnect(pReceivePin);
  600. }
  601. }
  602. if (FAILED(hr))
  603. {
  604. IPin_Release(This->pin.peer);
  605. This->pin.peer = NULL;
  606. FreeMediaType(&This->pin.mt);
  607. }
  608. TRACE("Returning %#lx.\n", hr);
  609. return hr;
  610. }
  611. void strmbase_source_init(struct strmbase_source *pin, struct strmbase_filter *filter,
  612. const WCHAR *name, const struct strmbase_source_ops *func_table)
  613. {
  614. memset(pin, 0, sizeof(*pin));
  615. pin->pin.IPin_iface.lpVtbl = &source_vtbl;
  616. pin->pin.filter = filter;
  617. pin->pin.dir = PINDIR_OUTPUT;
  618. lstrcpyW(pin->pin.name, name);
  619. lstrcpyW(pin->pin.id, name);
  620. pin->pin.ops = &func_table->base;
  621. pin->pFuncsTable = func_table;
  622. }
  623. void strmbase_source_cleanup(struct strmbase_source *pin)
  624. {
  625. FreeMediaType(&pin->pin.mt);
  626. if (pin->pAllocator)
  627. IMemAllocator_Release(pin->pAllocator);
  628. pin->pAllocator = NULL;
  629. }
  630. static struct strmbase_sink *impl_sink_from_IPin(IPin *iface)
  631. {
  632. return CONTAINING_RECORD(iface, struct strmbase_sink, pin.IPin_iface);
  633. }
  634. static HRESULT WINAPI sink_Connect(IPin *iface, IPin *peer, const AM_MEDIA_TYPE *mt)
  635. {
  636. struct strmbase_sink *pin = impl_sink_from_IPin(iface);
  637. WARN("pin %p %s:%s, peer %p, mt %p, unexpected call.\n", pin, debugstr_w(pin->pin.name),
  638. debugstr_w(pin->pin.filter->name), peer, mt);
  639. return E_UNEXPECTED;
  640. }
  641. static HRESULT WINAPI sink_ReceiveConnection(IPin *iface, IPin *pReceivePin, const AM_MEDIA_TYPE *pmt)
  642. {
  643. struct strmbase_sink *This = impl_sink_from_IPin(iface);
  644. PIN_DIRECTION pindirReceive;
  645. HRESULT hr = S_OK;
  646. TRACE("pin %p %s:%s, peer %p, mt %p.\n", This, debugstr_w(This->pin.filter->name),
  647. debugstr_w(This->pin.name), pReceivePin, pmt);
  648. strmbase_dump_media_type(pmt);
  649. if (!pmt)
  650. return E_POINTER;
  651. EnterCriticalSection(&This->pin.filter->filter_cs);
  652. {
  653. if (This->pin.filter->state != State_Stopped)
  654. {
  655. LeaveCriticalSection(&This->pin.filter->filter_cs);
  656. WARN("Filter is not stopped; returning VFW_E_NOT_STOPPED.\n");
  657. return VFW_E_NOT_STOPPED;
  658. }
  659. if (This->pin.peer)
  660. hr = VFW_E_ALREADY_CONNECTED;
  661. if (SUCCEEDED(hr) && !query_accept(&This->pin, pmt))
  662. hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
  663. * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
  664. if (SUCCEEDED(hr))
  665. {
  666. IPin_QueryDirection(pReceivePin, &pindirReceive);
  667. if (pindirReceive != PINDIR_OUTPUT)
  668. {
  669. ERR("Can't connect from non-output pin\n");
  670. hr = VFW_E_INVALID_DIRECTION;
  671. }
  672. }
  673. if (SUCCEEDED(hr) && This->pFuncsTable->sink_connect)
  674. hr = This->pFuncsTable->sink_connect(This, pReceivePin, pmt);
  675. if (SUCCEEDED(hr))
  676. {
  677. CopyMediaType(&This->pin.mt, pmt);
  678. This->pin.peer = pReceivePin;
  679. IPin_AddRef(pReceivePin);
  680. }
  681. }
  682. LeaveCriticalSection(&This->pin.filter->filter_cs);
  683. return hr;
  684. }
  685. static HRESULT WINAPI sink_Disconnect(IPin *iface)
  686. {
  687. struct strmbase_sink *pin = impl_sink_from_IPin(iface);
  688. HRESULT hr;
  689. TRACE("pin %p %s:%s.\n", pin, debugstr_w(pin->pin.filter->name), debugstr_w(pin->pin.name));
  690. EnterCriticalSection(&pin->pin.filter->filter_cs);
  691. if (pin->pin.filter->state != State_Stopped)
  692. {
  693. LeaveCriticalSection(&pin->pin.filter->filter_cs);
  694. WARN("Filter is not stopped; returning VFW_E_NOT_STOPPED.\n");
  695. return VFW_E_NOT_STOPPED;
  696. }
  697. if (pin->pin.peer)
  698. {
  699. if (pin->pFuncsTable->sink_disconnect)
  700. pin->pFuncsTable->sink_disconnect(pin);
  701. if (pin->pAllocator)
  702. {
  703. IMemAllocator_Release(pin->pAllocator);
  704. pin->pAllocator = NULL;
  705. }
  706. IPin_Release(pin->pin.peer);
  707. pin->pin.peer = NULL;
  708. FreeMediaType(&pin->pin.mt);
  709. memset(&pin->pin.mt, 0, sizeof(AM_MEDIA_TYPE));
  710. hr = S_OK;
  711. }
  712. else
  713. hr = S_FALSE;
  714. LeaveCriticalSection(&pin->pin.filter->filter_cs);
  715. return hr;
  716. }
  717. static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
  718. {
  719. return IPin_EndOfStream( pin );
  720. }
  721. static HRESULT WINAPI sink_EndOfStream(IPin *iface)
  722. {
  723. struct strmbase_sink *pin = impl_sink_from_IPin(iface);
  724. HRESULT hr = S_OK;
  725. TRACE("pin %p %s:%s.\n", pin, debugstr_w(pin->pin.filter->name), debugstr_w(pin->pin.name));
  726. if (pin->pFuncsTable->sink_eos)
  727. {
  728. EnterCriticalSection(&pin->pin.filter->stream_cs);
  729. hr = pin->pFuncsTable->sink_eos(pin);
  730. LeaveCriticalSection(&pin->pin.filter->stream_cs);
  731. return hr;
  732. }
  733. EnterCriticalSection(&pin->pin.filter->filter_cs);
  734. if (pin->flushing)
  735. hr = S_FALSE;
  736. LeaveCriticalSection(&pin->pin.filter->filter_cs);
  737. if (hr == S_OK)
  738. hr = SendFurther(pin, deliver_endofstream, NULL);
  739. return hr;
  740. }
  741. static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
  742. {
  743. return IPin_BeginFlush( pin );
  744. }
  745. static HRESULT WINAPI sink_BeginFlush(IPin *iface)
  746. {
  747. struct strmbase_sink *pin = impl_sink_from_IPin(iface);
  748. HRESULT hr;
  749. TRACE("pin %p %s:%s.\n", pin, debugstr_w(pin->pin.filter->name), debugstr_w(pin->pin.name));
  750. EnterCriticalSection(&pin->pin.filter->filter_cs);
  751. pin->flushing = TRUE;
  752. if (pin->pFuncsTable->sink_begin_flush)
  753. hr = pin->pFuncsTable->sink_begin_flush(pin);
  754. else
  755. hr = SendFurther(pin, deliver_beginflush, NULL);
  756. LeaveCriticalSection(&pin->pin.filter->filter_cs);
  757. return hr;
  758. }
  759. static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
  760. {
  761. return IPin_EndFlush( pin );
  762. }
  763. static HRESULT WINAPI sink_EndFlush(IPin * iface)
  764. {
  765. struct strmbase_sink *pin = impl_sink_from_IPin(iface);
  766. HRESULT hr;
  767. TRACE("pin %p %s:%s.\n", pin, debugstr_w(pin->pin.filter->name), debugstr_w(pin->pin.name));
  768. EnterCriticalSection(&pin->pin.filter->filter_cs);
  769. pin->flushing = FALSE;
  770. if (pin->pFuncsTable->sink_end_flush)
  771. hr = pin->pFuncsTable->sink_end_flush(pin);
  772. else
  773. hr = SendFurther(pin, deliver_endflush, NULL);
  774. LeaveCriticalSection(&pin->pin.filter->filter_cs);
  775. return hr;
  776. }
  777. typedef struct newsegmentargs
  778. {
  779. REFERENCE_TIME tStart, tStop;
  780. double rate;
  781. } newsegmentargs;
  782. static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
  783. {
  784. newsegmentargs *args = data;
  785. return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
  786. }
  787. static HRESULT WINAPI sink_NewSegment(IPin *iface, REFERENCE_TIME start, REFERENCE_TIME stop, double rate)
  788. {
  789. struct strmbase_sink *pin = impl_sink_from_IPin(iface);
  790. newsegmentargs args;
  791. TRACE("pin %p %s:%s, start %s, stop %s, rate %.16e.\n", pin, debugstr_w(pin->pin.filter->name),
  792. debugstr_w(pin->pin.name), debugstr_time(start), debugstr_time(stop), rate);
  793. if (pin->pFuncsTable->sink_new_segment)
  794. return pin->pFuncsTable->sink_new_segment(pin, start, stop, rate);
  795. args.tStart = start;
  796. args.tStop = stop;
  797. args.rate = rate;
  798. return SendFurther(pin, deliver_newsegment, &args);
  799. }
  800. static const IPinVtbl sink_vtbl =
  801. {
  802. pin_QueryInterface,
  803. pin_AddRef,
  804. pin_Release,
  805. sink_Connect,
  806. sink_ReceiveConnection,
  807. sink_Disconnect,
  808. pin_ConnectedTo,
  809. pin_ConnectionMediaType,
  810. pin_QueryPinInfo,
  811. pin_QueryDirection,
  812. pin_QueryId,
  813. pin_QueryAccept,
  814. pin_EnumMediaTypes,
  815. pin_QueryInternalConnections,
  816. sink_EndOfStream,
  817. sink_BeginFlush,
  818. sink_EndFlush,
  819. sink_NewSegment,
  820. };
  821. /*** IMemInputPin implementation ***/
  822. static inline struct strmbase_sink *impl_from_IMemInputPin(IMemInputPin *iface)
  823. {
  824. return CONTAINING_RECORD(iface, struct strmbase_sink, IMemInputPin_iface);
  825. }
  826. static HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
  827. {
  828. struct strmbase_sink *This = impl_from_IMemInputPin(iface);
  829. return IPin_QueryInterface(&This->pin.IPin_iface, riid, ppv);
  830. }
  831. static ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
  832. {
  833. struct strmbase_sink *This = impl_from_IMemInputPin(iface);
  834. return IPin_AddRef(&This->pin.IPin_iface);
  835. }
  836. static ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
  837. {
  838. struct strmbase_sink *This = impl_from_IMemInputPin(iface);
  839. return IPin_Release(&This->pin.IPin_iface);
  840. }
  841. static HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
  842. {
  843. struct strmbase_sink *This = impl_from_IMemInputPin(iface);
  844. TRACE("pin %p %s:%s, allocator %p.\n", This, debugstr_w(This->pin.filter->name),
  845. debugstr_w(This->pin.name), ppAllocator);
  846. *ppAllocator = This->pAllocator;
  847. if (*ppAllocator)
  848. IMemAllocator_AddRef(*ppAllocator);
  849. return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
  850. }
  851. static HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
  852. {
  853. struct strmbase_sink *This = impl_from_IMemInputPin(iface);
  854. TRACE("pin %p %s:%s, allocator %p, read_only %d.\n", This, debugstr_w(This->pin.filter->name),
  855. debugstr_w(This->pin.name), pAllocator, bReadOnly);
  856. if (bReadOnly)
  857. FIXME("Read only flag not handled yet!\n");
  858. /* FIXME: Should we release the allocator on disconnection? */
  859. if (!pAllocator)
  860. {
  861. WARN("Null allocator\n");
  862. return E_POINTER;
  863. }
  864. if (This->preferred_allocator && pAllocator != This->preferred_allocator)
  865. return E_FAIL;
  866. if (This->pAllocator)
  867. IMemAllocator_Release(This->pAllocator);
  868. This->pAllocator = pAllocator;
  869. if (This->pAllocator)
  870. IMemAllocator_AddRef(This->pAllocator);
  871. return S_OK;
  872. }
  873. static HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin *iface, ALLOCATOR_PROPERTIES *props)
  874. {
  875. struct strmbase_sink *pin = impl_from_IMemInputPin(iface);
  876. TRACE("pin %p %s:%s, props %p.\n", pin, debugstr_w(pin->pin.filter->name),
  877. debugstr_w(pin->pin.name), props);
  878. /* override this method if you have any specific requirements */
  879. return E_NOTIMPL;
  880. }
  881. static HRESULT WINAPI MemInputPin_Receive(IMemInputPin *iface, IMediaSample *sample)
  882. {
  883. struct strmbase_sink *pin = impl_from_IMemInputPin(iface);
  884. HRESULT hr = S_FALSE;
  885. TRACE("pin %p %s:%s, sample %p.\n", pin, debugstr_w(pin->pin.filter->name),
  886. debugstr_w(pin->pin.name), sample);
  887. if (pin->pFuncsTable->pfnReceive)
  888. {
  889. EnterCriticalSection(&pin->pin.filter->stream_cs);
  890. hr = pin->pFuncsTable->pfnReceive(pin, sample);
  891. LeaveCriticalSection(&pin->pin.filter->stream_cs);
  892. }
  893. return hr;
  894. }
  895. static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
  896. {
  897. HRESULT hr = S_OK;
  898. for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
  899. {
  900. hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
  901. if (hr != S_OK)
  902. break;
  903. }
  904. return hr;
  905. }
  906. static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
  907. {
  908. struct strmbase_sink *pin = impl_from_IMemInputPin(iface);
  909. TRACE("pin %p %s:%s.\n", pin, debugstr_w(pin->pin.filter->name), debugstr_w(pin->pin.name));
  910. return S_OK;
  911. }
  912. static const IMemInputPinVtbl MemInputPin_Vtbl =
  913. {
  914. MemInputPin_QueryInterface,
  915. MemInputPin_AddRef,
  916. MemInputPin_Release,
  917. MemInputPin_GetAllocator,
  918. MemInputPin_NotifyAllocator,
  919. MemInputPin_GetAllocatorRequirements,
  920. MemInputPin_Receive,
  921. MemInputPin_ReceiveMultiple,
  922. MemInputPin_ReceiveCanBlock
  923. };
  924. void strmbase_sink_init(struct strmbase_sink *pin, struct strmbase_filter *filter,
  925. const WCHAR *name, const struct strmbase_sink_ops *func_table, IMemAllocator *allocator)
  926. {
  927. memset(pin, 0, sizeof(*pin));
  928. pin->pin.IPin_iface.lpVtbl = &sink_vtbl;
  929. pin->pin.filter = filter;
  930. pin->pin.dir = PINDIR_INPUT;
  931. lstrcpyW(pin->pin.name, name);
  932. lstrcpyW(pin->pin.id, name);
  933. pin->pin.ops = &func_table->base;
  934. pin->pFuncsTable = func_table;
  935. pin->pAllocator = pin->preferred_allocator = allocator;
  936. if (pin->preferred_allocator)
  937. IMemAllocator_AddRef(pin->preferred_allocator);
  938. pin->IMemInputPin_iface.lpVtbl = &MemInputPin_Vtbl;
  939. }
  940. void strmbase_sink_cleanup(struct strmbase_sink *pin)
  941. {
  942. FreeMediaType(&pin->pin.mt);
  943. if (pin->pAllocator)
  944. IMemAllocator_Release(pin->pAllocator);
  945. pin->pAllocator = NULL;
  946. pin->pin.IPin_iface.lpVtbl = NULL;
  947. }