winex11.drv: Map coordinates before calling send_mouse_input.
[wine/zf.git] / dlls / strmbase / filter.c
blob93593e4c1890d93fb12db315f7af83941d548408
1 /*
2 * Generic Implementation of IBaseFilter Interface
4 * Copyright 2010 Aric Stewart, CodeWeavers
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.
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.
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
21 #include "strmbase_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
25 struct enum_pins
27 IEnumPins IEnumPins_iface;
28 LONG refcount;
30 unsigned int index, count;
31 int version;
32 struct strmbase_filter *filter;
35 static const IEnumPinsVtbl enum_pins_vtbl;
37 static HRESULT enum_pins_create(struct strmbase_filter *filter, IEnumPins **out)
39 struct enum_pins *object;
41 if (!out)
42 return E_POINTER;
44 if (!(object = heap_alloc_zero(sizeof(*object))))
46 *out = NULL;
47 return E_OUTOFMEMORY;
50 object->IEnumPins_iface.lpVtbl = &enum_pins_vtbl;
51 object->refcount = 1;
52 object->filter = filter;
53 IBaseFilter_AddRef(&filter->IBaseFilter_iface);
54 object->version = filter->pin_version;
56 while (filter->ops->filter_get_pin(filter, object->count))
57 ++object->count;
59 TRACE("Created enumerator %p.\n", object);
60 *out = &object->IEnumPins_iface;
62 return S_OK;
65 static inline struct enum_pins *impl_from_IEnumPins(IEnumPins *iface)
67 return CONTAINING_RECORD(iface, struct enum_pins, IEnumPins_iface);
70 static HRESULT WINAPI enum_pins_QueryInterface(IEnumPins *iface, REFIID iid, void **out)
72 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
74 if (IsEqualGUID(iid, &IID_IUnknown) || IsEqualGUID(iid, &IID_IEnumPins))
76 IEnumPins_AddRef(iface);
77 *out = iface;
78 return S_OK;
81 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
82 *out = NULL;
83 return E_NOINTERFACE;
86 static ULONG WINAPI enum_pins_AddRef(IEnumPins *iface)
88 struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
89 ULONG refcount = InterlockedIncrement(&enum_pins->refcount);
90 TRACE("%p increasing refcount to %u.\n", enum_pins, refcount);
91 return refcount;
94 static ULONG WINAPI enum_pins_Release(IEnumPins *iface)
96 struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
97 ULONG refcount = InterlockedDecrement(&enum_pins->refcount);
99 TRACE("%p decreasing refcount to %u.\n", enum_pins, refcount);
100 if (!refcount)
102 IBaseFilter_Release(&enum_pins->filter->IBaseFilter_iface);
103 heap_free(enum_pins);
105 return refcount;
108 static HRESULT WINAPI enum_pins_Next(IEnumPins *iface, ULONG count, IPin **pins, ULONG *ret_count)
110 struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
111 unsigned int i;
113 TRACE("iface %p, count %u, pins %p, ret_count %p.\n", iface, count, pins, ret_count);
115 if (!pins)
116 return E_POINTER;
118 if (count > 1 && !ret_count)
119 return E_INVALIDARG;
121 if (ret_count)
122 *ret_count = 0;
124 if (enum_pins->version != enum_pins->filter->pin_version)
125 return S_FALSE;
127 for (i = 0; i < count; ++i)
129 struct strmbase_pin *pin = enum_pins->filter->ops->filter_get_pin(enum_pins->filter, enum_pins->index + i);
131 if (!pin)
132 break;
134 IPin_AddRef(pins[i] = &pin->IPin_iface);
137 if (ret_count)
138 *ret_count = i;
139 enum_pins->index += i;
140 return i == count ? S_OK : S_FALSE;
143 static HRESULT WINAPI enum_pins_Skip(IEnumPins *iface, ULONG count)
145 struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
147 TRACE("iface %p, count %u.\n", iface, count);
149 if (enum_pins->version != enum_pins->filter->pin_version)
150 return VFW_E_ENUM_OUT_OF_SYNC;
152 if (enum_pins->index + count > enum_pins->count)
153 return S_FALSE;
155 enum_pins->index += count;
156 return S_OK;
159 static HRESULT WINAPI enum_pins_Reset(IEnumPins *iface)
161 struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
163 TRACE("iface %p.\n", iface);
165 if (enum_pins->version != enum_pins->filter->pin_version)
167 enum_pins->count = 0;
168 while (enum_pins->filter->ops->filter_get_pin(enum_pins->filter, enum_pins->count))
169 ++enum_pins->count;
172 enum_pins->version = enum_pins->filter->pin_version;
173 enum_pins->index = 0;
175 return S_OK;
178 static HRESULT WINAPI enum_pins_Clone(IEnumPins *iface, IEnumPins **out)
180 struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
181 HRESULT hr;
183 TRACE("iface %p, out %p.\n", iface, out);
185 if (FAILED(hr = enum_pins_create(enum_pins->filter, out)))
186 return hr;
187 return IEnumPins_Skip(*out, enum_pins->index);
190 static const IEnumPinsVtbl enum_pins_vtbl =
192 enum_pins_QueryInterface,
193 enum_pins_AddRef,
194 enum_pins_Release,
195 enum_pins_Next,
196 enum_pins_Skip,
197 enum_pins_Reset,
198 enum_pins_Clone,
201 static inline struct strmbase_filter *impl_from_IUnknown(IUnknown *iface)
203 return CONTAINING_RECORD(iface, struct strmbase_filter, IUnknown_inner);
206 static HRESULT WINAPI filter_inner_QueryInterface(IUnknown *iface, REFIID iid, void **out)
208 struct strmbase_filter *filter = impl_from_IUnknown(iface);
209 HRESULT hr;
211 TRACE("filter %p %s, iid %s, out %p.\n", filter, debugstr_w(filter->name), debugstr_guid(iid), out);
213 *out = NULL;
215 if (filter->ops->filter_query_interface
216 && SUCCEEDED(hr = filter->ops->filter_query_interface(filter, iid, out)))
218 return hr;
221 if (IsEqualIID(iid, &IID_IUnknown))
222 *out = iface;
223 else if (IsEqualIID(iid, &IID_IPersist)
224 || IsEqualIID(iid, &IID_IMediaFilter)
225 || IsEqualIID(iid, &IID_IBaseFilter))
227 *out = &filter->IBaseFilter_iface;
229 else
231 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
232 return E_NOINTERFACE;
235 IUnknown_AddRef((IUnknown *)*out);
236 return S_OK;
239 static ULONG WINAPI filter_inner_AddRef(IUnknown *iface)
241 struct strmbase_filter *filter = impl_from_IUnknown(iface);
242 ULONG refcount = InterlockedIncrement(&filter->refcount);
244 TRACE("%p increasing refcount to %u.\n", filter, refcount);
246 return refcount;
249 static ULONG WINAPI filter_inner_Release(IUnknown *iface)
251 struct strmbase_filter *filter = impl_from_IUnknown(iface);
252 ULONG refcount = InterlockedDecrement(&filter->refcount);
254 TRACE("%p decreasing refcount to %u.\n", filter, refcount);
256 if (!refcount)
257 filter->ops->filter_destroy(filter);
259 return refcount;
262 static const IUnknownVtbl filter_inner_vtbl =
264 filter_inner_QueryInterface,
265 filter_inner_AddRef,
266 filter_inner_Release,
269 static inline struct strmbase_filter *impl_from_IBaseFilter(IBaseFilter *iface)
271 return CONTAINING_RECORD(iface, struct strmbase_filter, IBaseFilter_iface);
274 static HRESULT WINAPI filter_QueryInterface(IBaseFilter *iface, REFIID iid, void **out)
276 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
277 return IUnknown_QueryInterface(filter->outer_unk, iid, out);
280 static ULONG WINAPI filter_AddRef(IBaseFilter *iface)
282 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
283 return IUnknown_AddRef(filter->outer_unk);
286 static ULONG WINAPI filter_Release(IBaseFilter *iface)
288 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
289 return IUnknown_Release(filter->outer_unk);
292 static HRESULT WINAPI filter_GetClassID(IBaseFilter *iface, CLSID *clsid)
294 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
296 TRACE("filter %p %s, clsid %p.\n", filter, debugstr_w(filter->name), clsid);
298 *clsid = filter->clsid;
300 return S_OK;
303 static HRESULT WINAPI filter_Stop(IBaseFilter *iface)
305 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
306 HRESULT hr = S_OK;
308 TRACE("filter %p %s.\n", filter, debugstr_w(filter->name));
310 EnterCriticalSection(&filter->filter_cs);
312 if (filter->state == State_Running && filter->ops->filter_stop_stream)
313 hr = filter->ops->filter_stop_stream(filter);
314 if (SUCCEEDED(hr) && filter->ops->filter_cleanup_stream)
315 hr = filter->ops->filter_cleanup_stream(filter);
316 if (SUCCEEDED(hr))
317 filter->state = State_Stopped;
319 LeaveCriticalSection(&filter->filter_cs);
321 return hr;
324 static HRESULT WINAPI filter_Pause(IBaseFilter *iface)
326 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
327 HRESULT hr = S_OK;
329 TRACE("filter %p %s.\n", filter, debugstr_w(filter->name));
331 EnterCriticalSection(&filter->filter_cs);
333 if (filter->state == State_Stopped && filter->ops->filter_init_stream)
334 hr = filter->ops->filter_init_stream(filter);
335 else if (filter->state == State_Running && filter->ops->filter_stop_stream)
336 hr = filter->ops->filter_stop_stream(filter);
337 if (SUCCEEDED(hr))
338 filter->state = State_Paused;
340 LeaveCriticalSection(&filter->filter_cs);
342 return hr;
345 static HRESULT WINAPI filter_Run(IBaseFilter *iface, REFERENCE_TIME start)
347 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
348 HRESULT hr = S_OK;
350 TRACE("filter %p %s, start %s.\n", filter, debugstr_w(filter->name), debugstr_time(start));
352 EnterCriticalSection(&filter->filter_cs);
354 if (filter->state == State_Stopped && filter->ops->filter_init_stream)
355 hr = filter->ops->filter_init_stream(filter);
356 if (SUCCEEDED(hr) && filter->ops->filter_start_stream)
357 hr = filter->ops->filter_start_stream(filter, start);
358 if (SUCCEEDED(hr))
359 filter->state = State_Running;
361 LeaveCriticalSection(&filter->filter_cs);
363 return hr;
366 static HRESULT WINAPI filter_GetState(IBaseFilter *iface, DWORD timeout, FILTER_STATE *state)
368 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
369 HRESULT hr = S_OK;
371 TRACE("filter %p %s, timeout %u, state %p.\n", filter, debugstr_w(filter->name), timeout, state);
373 EnterCriticalSection(&filter->filter_cs);
375 if (filter->ops->filter_wait_state)
376 hr = filter->ops->filter_wait_state(filter, timeout);
377 *state = filter->state;
379 LeaveCriticalSection(&filter->filter_cs);
381 return hr;
384 static HRESULT WINAPI filter_SetSyncSource(IBaseFilter *iface, IReferenceClock *clock)
386 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
388 TRACE("filter %p %s, clock %p.\n", filter, debugstr_w(filter->name), clock);
390 EnterCriticalSection(&filter->filter_cs);
392 if (filter->clock)
393 IReferenceClock_Release(filter->clock);
394 filter->clock = clock;
395 if (filter->clock)
396 IReferenceClock_AddRef(filter->clock);
398 LeaveCriticalSection(&filter->filter_cs);
400 return S_OK;
403 static HRESULT WINAPI filter_GetSyncSource(IBaseFilter *iface, IReferenceClock **clock)
405 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
407 TRACE("filter %p %s, clock %p.\n", filter, debugstr_w(filter->name), clock);
409 EnterCriticalSection(&filter->filter_cs);
411 *clock = filter->clock;
412 if (filter->clock)
413 IReferenceClock_AddRef(filter->clock);
415 LeaveCriticalSection(&filter->filter_cs);
417 return S_OK;
420 static HRESULT WINAPI filter_EnumPins(IBaseFilter *iface, IEnumPins **enum_pins)
422 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
424 TRACE("filter %p %s, enum_pins %p.\n", filter, debugstr_w(filter->name), enum_pins);
426 return enum_pins_create(filter, enum_pins);
429 static HRESULT WINAPI filter_FindPin(IBaseFilter *iface, const WCHAR *id, IPin **ret)
431 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
432 struct strmbase_pin *pin;
433 unsigned int i;
435 TRACE("filter %p %s, id %s, ret %p.\n", filter, debugstr_w(filter->name), debugstr_w(id), ret);
437 for (i = 0; (pin = filter->ops->filter_get_pin(filter, i)); ++i)
439 if (!lstrcmpW(id, pin->name))
441 IPin_AddRef(*ret = &pin->IPin_iface);
442 return S_OK;
446 return VFW_E_NOT_FOUND;
449 static HRESULT WINAPI filter_QueryFilterInfo(IBaseFilter *iface, FILTER_INFO *info)
451 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
453 TRACE("filter %p %s, info %p.\n", filter, debugstr_w(filter->name), info);
455 lstrcpyW(info->achName, filter->name);
456 info->pGraph = filter->graph;
458 if (info->pGraph)
459 IFilterGraph_AddRef(info->pGraph);
461 return S_OK;
464 static HRESULT WINAPI filter_JoinFilterGraph(IBaseFilter *iface, IFilterGraph *graph, const WCHAR *name)
466 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
468 TRACE("filter %p %s, graph %p, name %s.\n", filter, debugstr_w(filter->name), graph, debugstr_w(name));
470 EnterCriticalSection(&filter->filter_cs);
472 if (name)
473 lstrcpynW(filter->name, name, ARRAY_SIZE(filter->name));
474 else
475 filter->name[0] = 0;
476 /* The graph references us, so we cannot also reference the graph. */
477 filter->graph = graph;
479 LeaveCriticalSection(&filter->filter_cs);
481 return S_OK;
484 static HRESULT WINAPI filter_QueryVendorInfo(IBaseFilter *iface, WCHAR **info)
486 struct strmbase_filter *filter = impl_from_IBaseFilter(iface);
488 TRACE("filter %p %s, info %p.\n", filter, debugstr_w(filter->name), info);
490 return E_NOTIMPL;
493 static const IBaseFilterVtbl filter_vtbl =
495 filter_QueryInterface,
496 filter_AddRef,
497 filter_Release,
498 filter_GetClassID,
499 filter_Stop,
500 filter_Pause,
501 filter_Run,
502 filter_GetState,
503 filter_SetSyncSource,
504 filter_GetSyncSource,
505 filter_EnumPins,
506 filter_FindPin,
507 filter_QueryFilterInfo,
508 filter_JoinFilterGraph,
509 filter_QueryVendorInfo,
512 VOID WINAPI BaseFilterImpl_IncrementPinVersion(struct strmbase_filter *filter)
514 InterlockedIncrement(&filter->pin_version);
517 void strmbase_filter_init(struct strmbase_filter *filter, IUnknown *outer,
518 const CLSID *clsid, const struct strmbase_filter_ops *ops)
520 memset(filter, 0, sizeof(*filter));
522 filter->IBaseFilter_iface.lpVtbl = &filter_vtbl;
523 filter->IUnknown_inner.lpVtbl = &filter_inner_vtbl;
524 filter->outer_unk = outer ? outer : &filter->IUnknown_inner;
525 filter->refcount = 1;
527 InitializeCriticalSection(&filter->filter_cs);
528 if (filter->filter_cs.DebugInfo != (RTL_CRITICAL_SECTION_DEBUG *)-1)
529 filter->filter_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": strmbase_filter.filter_cs");
530 InitializeCriticalSection(&filter->stream_cs);
531 if (filter->stream_cs.DebugInfo != (RTL_CRITICAL_SECTION_DEBUG *)-1)
532 filter->stream_cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": strmbase_filter.stream_cs");
533 filter->clsid = *clsid;
534 filter->pin_version = 1;
535 filter->ops = ops;
538 void strmbase_filter_cleanup(struct strmbase_filter *filter)
540 if (filter->clock)
541 IReferenceClock_Release(filter->clock);
543 filter->IBaseFilter_iface.lpVtbl = NULL;
544 if (filter->filter_cs.DebugInfo != (RTL_CRITICAL_SECTION_DEBUG *)-1)
545 filter->filter_cs.DebugInfo->Spare[0] = 0;
546 DeleteCriticalSection(&filter->filter_cs);
547 if (filter->stream_cs.DebugInfo != (RTL_CRITICAL_SECTION_DEBUG *)-1)
548 filter->stream_cs.DebugInfo->Spare[0] = 0;
549 DeleteCriticalSection(&filter->stream_cs);