avifil32: Update Italian translation.
[wine/multimedia.git] / dlls / qcap / capturegraph.c
blob7294d5d5a997d684dc3a1db85f4fc1591ead6b45
1 /* Capture Graph Builder, Minimal edition
3 * Copyright 2005 Maarten Lankhorst
4 * Copyright 2005 Rolf Kalbermatter
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
20 #include "config.h"
22 #include <stdio.h>
23 #include <stdarg.h>
25 #define COBJMACROS
26 #define NONAMELESSSTRUCT
27 #define NONAMELESSUNION
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winerror.h"
33 #include "objbase.h"
35 #include "evcode.h"
36 #include "strmif.h"
37 #include "control.h"
38 #include "vfwmsgs.h"
40 *#include "amvideo.h"
41 *#include "mmreg.h"
42 *#include "dshow.h"
43 *#include "ddraw.h"
45 #include "qcap_main.h"
47 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(qcap);
52 /***********************************************************************
53 * ICaptureGraphBuilder & ICaptureGraphBuilder2 implementation
55 typedef struct CaptureGraphImpl
57 const ICaptureGraphBuilder2Vtbl * lpVtbl2;
58 const ICaptureGraphBuilderVtbl * lpVtbl;
59 LONG ref;
60 IGraphBuilder *mygraph;
62 CRITICAL_SECTION csFilter;
63 } CaptureGraphImpl;
65 static const ICaptureGraphBuilderVtbl builder_Vtbl;
66 static const ICaptureGraphBuilder2Vtbl builder2_Vtbl;
68 static inline CaptureGraphImpl *impl_from_ICaptureGraphBuilder( ICaptureGraphBuilder *iface )
70 return (CaptureGraphImpl *)((char*)iface - FIELD_OFFSET(CaptureGraphImpl, lpVtbl));
73 static inline CaptureGraphImpl *impl_from_ICaptureGraphBuilder2( ICaptureGraphBuilder2 *iface )
75 return (CaptureGraphImpl *)((char*)iface - FIELD_OFFSET(CaptureGraphImpl, lpVtbl2));
79 converts This to an interface pointer
81 #define _IUnknown_(This) (&(This)->lpVtbl2)
82 #define _ICaptureGraphBuilder_(This) (&(This)->lpVtbl)
83 #define _ICaptureGraphBuilder2_(This) ((ICaptureGraphBuilder2*)&(This)->lpVtbl2)
86 IUnknown * CALLBACK QCAP_createCaptureGraphBuilder2(IUnknown *pUnkOuter,
87 HRESULT *phr)
89 CaptureGraphImpl * pCapture = NULL;
91 TRACE("(%p, %p)\n", pUnkOuter, phr);
93 *phr = CLASS_E_NOAGGREGATION;
94 if (pUnkOuter)
96 return NULL;
98 *phr = E_OUTOFMEMORY;
100 pCapture = CoTaskMemAlloc(sizeof(CaptureGraphImpl));
101 if (pCapture)
103 pCapture->lpVtbl2 = &builder2_Vtbl;
104 pCapture->lpVtbl = &builder_Vtbl;
105 pCapture->ref = 1;
106 pCapture->mygraph = NULL;
107 InitializeCriticalSection(&pCapture->csFilter);
108 pCapture->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": CaptureGraphImpl.csFilter");
109 *phr = S_OK;
110 ObjectRefCount(TRUE);
112 return (IUnknown *)pCapture;
115 static HRESULT WINAPI
116 fnCaptureGraphBuilder2_QueryInterface(ICaptureGraphBuilder2 * iface,
117 REFIID riid,
118 LPVOID * ppv)
120 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
122 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
124 *ppv = NULL;
125 if (IsEqualIID(riid, &IID_IUnknown))
126 *ppv = _IUnknown_(This);
127 else if (IsEqualIID(riid, &IID_ICaptureGraphBuilder))
128 *ppv = _ICaptureGraphBuilder_(This);
129 else if (IsEqualIID(riid, &IID_ICaptureGraphBuilder2))
130 *ppv = _ICaptureGraphBuilder2_(This);
132 if (*ppv)
134 IUnknown_AddRef((IUnknown *)(*ppv));
135 TRACE ("-- Interface = %p\n", *ppv);
136 return S_OK;
139 TRACE ("-- Interface: E_NOINTERFACE\n");
140 return E_NOINTERFACE;
143 static ULONG WINAPI
144 fnCaptureGraphBuilder2_AddRef(ICaptureGraphBuilder2 * iface)
146 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
147 DWORD ref = InterlockedIncrement(&This->ref);
149 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, ref - 1);
150 return ref;
153 static ULONG WINAPI
154 fnCaptureGraphBuilder2_Release(ICaptureGraphBuilder2 * iface)
156 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
157 DWORD ref = InterlockedDecrement(&This->ref);
159 TRACE("(%p/%p)->() Release from %d\n", This, iface, ref + 1);
161 if (!ref)
163 FIXME("Release IGraphFilter or w/e\n");
164 This->csFilter.DebugInfo->Spare[0] = 0;
165 DeleteCriticalSection(&This->csFilter);
166 This->lpVtbl = NULL;
167 This->lpVtbl2 = NULL;
168 if (This->mygraph != NULL)
169 IGraphBuilder_Release(This->mygraph);
170 CoTaskMemFree(This);
171 ObjectRefCount(FALSE);
173 return ref;
176 static HRESULT WINAPI
177 fnCaptureGraphBuilder2_SetFilterGraph(ICaptureGraphBuilder2 * iface,
178 IGraphBuilder *pfg)
180 /* The graph builder will automatically create a filter graph if you don't call
181 this method. If you call this method after the graph builder has created its
182 own filter graph, the call will fail. */
183 IMediaEvent *pmev;
184 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
186 TRACE("(%p/%p)->(%p)\n", This, iface, pfg);
188 if (This->mygraph)
189 return E_UNEXPECTED;
191 if (!pfg)
192 return E_POINTER;
194 This->mygraph = pfg;
195 IGraphBuilder_AddRef(This->mygraph);
196 if (SUCCEEDED(IUnknown_QueryInterface(This->mygraph,
197 &IID_IMediaEvent, (LPVOID *)&pmev)))
199 IMediaEvent_CancelDefaultHandling(pmev, EC_REPAINT);
200 IMediaEvent_Release(pmev);
202 return S_OK;
205 static HRESULT WINAPI
206 fnCaptureGraphBuilder2_GetFilterGraph(ICaptureGraphBuilder2 * iface,
207 IGraphBuilder **pfg)
209 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
211 TRACE("(%p/%p)->(%p)\n", This, iface, pfg);
213 if (!pfg)
214 return E_POINTER;
216 *pfg = This->mygraph;
217 if (!This->mygraph)
219 TRACE("(%p) Getting NULL filtergraph\n", iface);
220 return E_UNEXPECTED;
223 IGraphBuilder_AddRef(This->mygraph);
225 TRACE("(%p) return filtergraph %p\n", iface, *pfg);
226 return S_OK;
229 static HRESULT WINAPI
230 fnCaptureGraphBuilder2_SetOutputFileName(ICaptureGraphBuilder2 * iface,
231 const GUID *pType,
232 LPCOLESTR lpstrFile,
233 IBaseFilter **ppf,
234 IFileSinkFilter **ppSink)
236 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
238 FIXME("(%p/%p)->(%s, %s, %p, %p) Stub!\n", This, iface,
239 debugstr_guid(pType), debugstr_w(lpstrFile), ppf, ppSink);
241 return E_NOTIMPL;
244 static HRESULT WINAPI
245 fnCaptureGraphBuilder2_FindInterface(ICaptureGraphBuilder2 * iface,
246 const GUID *pCategory,
247 const GUID *pType,
248 IBaseFilter *pf,
249 REFIID riid,
250 void **ppint)
252 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
254 FIXME("(%p/%p)->(%s, %s, %p, %s, %p) - workaround stub!\n", This, iface,
255 debugstr_guid(pCategory), debugstr_guid(pType),
256 pf, debugstr_guid(riid), ppint);
258 return IBaseFilter_QueryInterface(pf, riid, ppint);
259 /* Looks for the specified interface on the filter, upstream and
260 * downstream from the filter, and, optionally, only on the output
261 * pin of the given category.
265 static HRESULT WINAPI
266 fnCaptureGraphBuilder2_RenderStream(ICaptureGraphBuilder2 * iface,
267 const GUID *pCategory,
268 const GUID *pType,
269 IUnknown *pSource,
270 IBaseFilter *pfCompressor,
271 IBaseFilter *pfRenderer)
273 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
274 IPin *pin_in = NULL;
275 IPin *pin_out = NULL;
276 HRESULT hr;
278 FIXME("(%p/%p)->(%s, %s, %p, %p, %p) Stub!\n", This, iface,
279 debugstr_guid(pCategory), debugstr_guid(pType),
280 pSource, pfCompressor, pfRenderer);
282 if (pfCompressor)
283 FIXME("Intermediate streams not supported yet\n");
285 if (!This->mygraph)
287 FIXME("Need a capture graph\n");
288 return E_UNEXPECTED;
291 ICaptureGraphBuilder2_FindPin(iface, pSource, PINDIR_OUTPUT, pCategory, pType, TRUE, 0, &pin_in);
292 if (!pin_in)
293 return E_FAIL;
294 ICaptureGraphBuilder2_FindPin(iface, (IUnknown*)pfRenderer, PINDIR_INPUT, pCategory, pType, TRUE, 0, &pin_out);
295 if (!pin_out)
297 IPin_Release(pin_in);
298 return E_FAIL;
301 /* Uses 'Intelligent Connect', so Connect, not ConnectDirect here */
302 hr = IFilterGraph2_Connect(This->mygraph, pin_in, pin_out);
303 IPin_Release(pin_in);
304 IPin_Release(pin_out);
305 return hr;
308 static HRESULT WINAPI
309 fnCaptureGraphBuilder2_ControlStream(ICaptureGraphBuilder2 * iface,
310 const GUID *pCategory,
311 const GUID *pType,
312 IBaseFilter *pFilter,
313 REFERENCE_TIME *pstart,
314 REFERENCE_TIME *pstop,
315 WORD wStartCookie,
316 WORD wStopCookie)
318 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
320 FIXME("(%p/%p)->(%s, %s, %p, %p, %p, %i, %i) Stub!\n", This, iface,
321 debugstr_guid(pCategory), debugstr_guid(pType),
322 pFilter, pstart, pstop, wStartCookie, wStopCookie);
324 return E_NOTIMPL;
327 static HRESULT WINAPI
328 fnCaptureGraphBuilder2_AllocCapFile(ICaptureGraphBuilder2 * iface,
329 LPCOLESTR lpwstr,
330 DWORDLONG dwlSize)
332 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
334 FIXME("(%p/%p)->(%s, 0x%s) Stub!\n", This, iface,
335 debugstr_w(lpwstr), wine_dbgstr_longlong(dwlSize));
337 return E_NOTIMPL;
340 static HRESULT WINAPI
341 fnCaptureGraphBuilder2_CopyCaptureFile(ICaptureGraphBuilder2 * iface,
342 LPOLESTR lpwstrOld,
343 LPOLESTR lpwstrNew,
344 int fAllowEscAbort,
345 IAMCopyCaptureFileProgress *pCallback)
347 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
349 FIXME("(%p/%p)->(%s, %s, %i, %p) Stub!\n", This, iface,
350 debugstr_w(lpwstrOld), debugstr_w(lpwstrNew),
351 fAllowEscAbort, pCallback);
353 return E_NOTIMPL;
356 static BOOL pin_matches(IPin *pin, PIN_DIRECTION direction, const GUID *cat, const GUID *type, BOOL unconnected)
358 IPin *partner;
359 PIN_DIRECTION pindir;
361 IPin_QueryDirection(pin, &pindir);
362 if (pindir != direction)
364 TRACE("No match, wrong direction\n");
365 return FALSE;
368 if (unconnected && IPin_ConnectedTo(pin, &partner) == S_OK)
370 IPin_Release(partner);
371 TRACE("No match, %p already connected to %p\n", pin, partner);
372 return FALSE;
375 if (cat || type)
376 FIXME("Ignoring category/type\n");
378 TRACE("Match made in heaven\n");
380 return TRUE;
383 static HRESULT WINAPI
384 fnCaptureGraphBuilder2_FindPin(ICaptureGraphBuilder2 * iface,
385 IUnknown *pSource,
386 PIN_DIRECTION pindir,
387 const GUID *pCategory,
388 const GUID *pType,
389 BOOL fUnconnected,
390 INT num,
391 IPin **ppPin)
393 HRESULT hr;
394 IEnumPins *enumpins = NULL;
395 IPin *pin;
396 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
398 TRACE("(%p/%p)->(%p, %x, %s, %s, %d, %i, %p)\n", This, iface,
399 pSource, pindir, debugstr_guid(pCategory), debugstr_guid(pType),
400 fUnconnected, num, ppPin);
402 pin = NULL;
404 hr = IUnknown_QueryInterface(pSource, &IID_IPin, (void**)&pin);
405 if (hr == E_NOINTERFACE)
407 IBaseFilter *filter = NULL;
408 int numcurrent = 0;
410 hr = IUnknown_QueryInterface(pSource, &IID_IBaseFilter, (void**)&filter);
411 if (hr == E_NOINTERFACE)
413 WARN("Input not filter or pin?!\n");
414 return E_FAIL;
417 hr = IBaseFilter_EnumPins(filter, &enumpins);
418 if (FAILED(hr))
420 WARN("Could not enumerate\n");
421 return hr;
424 IEnumPins_Reset(enumpins);
426 while (1)
428 hr = IEnumPins_Next(enumpins, 1, &pin, NULL);
429 if (hr == VFW_E_ENUM_OUT_OF_SYNC)
431 numcurrent = 0;
432 IEnumPins_Reset(enumpins);
433 pin = NULL;
434 continue;
437 if (hr != S_OK)
438 break;
439 TRACE("Testing match\n");
440 if (pin_matches(pin, pindir, pCategory, pType, fUnconnected) && numcurrent++ == num)
441 break;
442 IPin_Release(pin);
443 pin = NULL;
445 IEnumPins_Release(enumpins);
447 if (hr != S_OK)
449 WARN("Could not find %s pin # %d\n", (pindir == PINDIR_OUTPUT ? "output" : "input"), numcurrent);
450 return E_FAIL;
453 else if (!pin_matches(pin, pindir, pCategory, pType, fUnconnected))
455 IPin_Release(pin);
456 return E_FAIL;
459 *ppPin = pin;
460 return S_OK;
463 static const ICaptureGraphBuilder2Vtbl builder2_Vtbl =
465 fnCaptureGraphBuilder2_QueryInterface,
466 fnCaptureGraphBuilder2_AddRef,
467 fnCaptureGraphBuilder2_Release,
468 fnCaptureGraphBuilder2_SetFilterGraph,
469 fnCaptureGraphBuilder2_GetFilterGraph,
470 fnCaptureGraphBuilder2_SetOutputFileName,
471 fnCaptureGraphBuilder2_FindInterface,
472 fnCaptureGraphBuilder2_RenderStream,
473 fnCaptureGraphBuilder2_ControlStream,
474 fnCaptureGraphBuilder2_AllocCapFile,
475 fnCaptureGraphBuilder2_CopyCaptureFile,
476 fnCaptureGraphBuilder2_FindPin
480 static HRESULT WINAPI
481 fnCaptureGraphBuilder_QueryInterface(ICaptureGraphBuilder * iface,
482 REFIID riid, LPVOID * ppv)
484 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
485 TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
486 return IUnknown_QueryInterface(_ICaptureGraphBuilder2_(This), riid, ppv);
489 static ULONG WINAPI
490 fnCaptureGraphBuilder_AddRef(ICaptureGraphBuilder * iface)
492 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
493 TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
494 return IUnknown_AddRef(_ICaptureGraphBuilder2_(This));
497 static ULONG WINAPI
498 fnCaptureGraphBuilder_Release(ICaptureGraphBuilder * iface)
500 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
501 TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
502 return IUnknown_Release(_ICaptureGraphBuilder2_(This));
505 static HRESULT WINAPI
506 fnCaptureGraphBuilder_SetFiltergraph(ICaptureGraphBuilder * iface,
507 IGraphBuilder *pfg)
509 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
510 TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
511 return ICaptureGraphBuilder2_SetFiltergraph(_ICaptureGraphBuilder2_(This), pfg);
514 static HRESULT WINAPI
515 fnCaptureGraphBuilder_GetFiltergraph(ICaptureGraphBuilder * iface,
516 IGraphBuilder **pfg)
518 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
519 TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
520 return ICaptureGraphBuilder2_GetFiltergraph(_ICaptureGraphBuilder2_(This), pfg);
523 static HRESULT WINAPI
524 fnCaptureGraphBuilder_SetOutputFileName(ICaptureGraphBuilder * iface,
525 const GUID *pType, LPCOLESTR lpstrFile,
526 IBaseFilter **ppf, IFileSinkFilter **ppSink)
528 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
529 TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
530 return ICaptureGraphBuilder2_SetOutputFileName(_ICaptureGraphBuilder2_(This),
531 pType, lpstrFile, ppf, ppSink);
534 static HRESULT WINAPI
535 fnCaptureGraphBuilder_FindInterface(ICaptureGraphBuilder * iface,
536 const GUID *pCategory, IBaseFilter *pf,
537 REFIID riid, void **ppint)
539 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
540 TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
541 return ICaptureGraphBuilder2_FindInterface(_ICaptureGraphBuilder2_(This),
542 pCategory, NULL, pf, riid, ppint);
545 static HRESULT WINAPI
546 fnCaptureGraphBuilder_RenderStream(ICaptureGraphBuilder * iface,
547 const GUID *pCategory, IUnknown *pSource,
548 IBaseFilter *pfCompressor, IBaseFilter *pfRenderer)
550 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
551 TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
552 return ICaptureGraphBuilder2_RenderStream(_ICaptureGraphBuilder2_(This),
553 pCategory, NULL, pSource,
554 pfCompressor, pfRenderer);
557 static HRESULT WINAPI
558 fnCaptureGraphBuilder_ControlStream(ICaptureGraphBuilder * iface,
559 const GUID *pCategory, IBaseFilter *pFilter,
560 REFERENCE_TIME *pstart, REFERENCE_TIME *pstop,
561 WORD wStartCookie, WORD wStopCookie)
563 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
564 TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
565 return ICaptureGraphBuilder2_ControlStream(_ICaptureGraphBuilder2_(This),
566 pCategory, NULL, pFilter, pstart,
567 pstop, wStartCookie, wStopCookie);
570 static HRESULT WINAPI
571 fnCaptureGraphBuilder_AllocCapFile(ICaptureGraphBuilder * iface,
572 LPCOLESTR lpstr, DWORDLONG dwlSize)
574 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
575 TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
576 return ICaptureGraphBuilder2_AllocCapFile(_ICaptureGraphBuilder2_(This),
577 lpstr, dwlSize);
580 static HRESULT WINAPI
581 fnCaptureGraphBuilder_CopyCaptureFile(ICaptureGraphBuilder * iface,
582 LPOLESTR lpwstrOld, LPOLESTR lpwstrNew,
583 int fAllowEscAbort,
584 IAMCopyCaptureFileProgress *pCallback)
586 CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
587 TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
588 return ICaptureGraphBuilder2_CopyCaptureFile(_ICaptureGraphBuilder2_(This),
589 lpwstrOld, lpwstrNew,
590 fAllowEscAbort, pCallback);
593 static const ICaptureGraphBuilderVtbl builder_Vtbl =
595 fnCaptureGraphBuilder_QueryInterface,
596 fnCaptureGraphBuilder_AddRef,
597 fnCaptureGraphBuilder_Release,
598 fnCaptureGraphBuilder_SetFiltergraph,
599 fnCaptureGraphBuilder_GetFiltergraph,
600 fnCaptureGraphBuilder_SetOutputFileName,
601 fnCaptureGraphBuilder_FindInterface,
602 fnCaptureGraphBuilder_RenderStream,
603 fnCaptureGraphBuilder_ControlStream,
604 fnCaptureGraphBuilder_AllocCapFile,
605 fnCaptureGraphBuilder_CopyCaptureFile