server: Move most of the duplicate_token request to a new function, token_duplicate...
[wine] / dlls / qcap / capturegraph.c
1 /* Capture Graph Builder, Minimal edition
2  *
3  * Copyright 2005 Maarten Lankhorst
4  * Copyright 2005 Rolf Kalbermatter
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 "config.h"
21
22 #include <stdio.h>
23 #include <stdarg.h>
24
25 #define COBJMACROS
26 #define NONAMELESSSTRUCT
27 #define NONAMELESSUNION
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "objbase.h"
33
34 #include "evcode.h"
35 #include "strmif.h"
36 #include "control.h"
37 /*
38  *#include "amvideo.h"
39  *#include "mmreg.h"
40  *#include "vfwmsgs.h"
41  *#include "dshow.h"
42  *#include "ddraw.h"
43  */
44 #include "qcap_main.h"
45
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(qcap);
50
51 /***********************************************************************
52 *   ICaptureGraphBuilder & ICaptureGraphBuilder2 implementation
53 */
54 typedef struct CaptureGraphImpl
55 {
56     const ICaptureGraphBuilder2Vtbl * lpVtbl2;
57     const ICaptureGraphBuilderVtbl * lpVtbl;
58     LONG ref;
59     IGraphBuilder *mygraph;
60
61     CRITICAL_SECTION csFilter;
62 } CaptureGraphImpl;
63
64 static const ICaptureGraphBuilderVtbl builder_Vtbl;
65 static const ICaptureGraphBuilder2Vtbl builder2_Vtbl;
66
67 static inline CaptureGraphImpl *impl_from_ICaptureGraphBuilder( ICaptureGraphBuilder *iface )
68 {
69     return (CaptureGraphImpl *)((char*)iface - FIELD_OFFSET(CaptureGraphImpl, lpVtbl));
70 }
71
72 static inline CaptureGraphImpl *impl_from_ICaptureGraphBuilder2( ICaptureGraphBuilder2 *iface )
73 {
74     return (CaptureGraphImpl *)((char*)iface - FIELD_OFFSET(CaptureGraphImpl, lpVtbl2));
75 }
76
77 /*
78   converts This to an interface pointer
79 */
80 #define _IUnknown_(This)                (IUnknown*)&(This->lpVtbl2)
81 #define _ICaptureGraphBuilder_(This)    (ICaptureGraphBuilder*)&(This->lpVtbl)
82 #define _ICaptureGraphBuilder2_(This)   (ICaptureGraphBuilder2*)&(This->lpVtbl2)
83
84
85 IUnknown * CALLBACK QCAP_createCaptureGraphBuilder2(IUnknown *pUnkOuter,
86                                                     HRESULT *phr)
87 {
88     CaptureGraphImpl * pCapture = NULL;
89
90     TRACE("(%p, %p)\n", pUnkOuter, phr);
91
92     *phr = CLASS_E_NOAGGREGATION;
93     if (pUnkOuter)
94     {
95         return NULL;
96     }
97     *phr = E_OUTOFMEMORY;
98
99     pCapture = CoTaskMemAlloc(sizeof(CaptureGraphImpl));
100     if (pCapture)
101     {
102         pCapture->lpVtbl2 = &builder2_Vtbl;
103         pCapture->lpVtbl = &builder_Vtbl;
104         pCapture->ref = 1;
105         pCapture->mygraph = NULL;
106         InitializeCriticalSection(&pCapture->csFilter);
107         pCapture->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": CaptureGraphImpl.csFilter");
108         *phr = S_OK;
109         ObjectRefCount(TRUE);
110     }
111     return (IUnknown *)pCapture;
112 }
113
114 static HRESULT WINAPI
115 fnCaptureGraphBuilder2_QueryInterface(ICaptureGraphBuilder2 * iface,
116                                       REFIID riid,
117                                       LPVOID * ppv)
118 {
119     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
120
121     TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
122
123     *ppv = NULL;
124     if (IsEqualIID(riid, &IID_IUnknown))
125         *ppv = _IUnknown_(This);
126     else if (IsEqualIID(riid, &IID_ICaptureGraphBuilder))
127         *ppv = _ICaptureGraphBuilder_(This);
128     else if (IsEqualIID(riid, &IID_ICaptureGraphBuilder2))
129         *ppv = _ICaptureGraphBuilder2_(This);
130
131     if (*ppv)
132     {
133         IUnknown_AddRef((IUnknown *)(*ppv));
134         TRACE ("-- Interface = %p\n", *ppv);
135         return S_OK;
136     }
137
138     TRACE ("-- Interface: E_NOINTERFACE\n");
139     return E_NOINTERFACE;
140 }
141
142 static ULONG WINAPI
143 fnCaptureGraphBuilder2_AddRef(ICaptureGraphBuilder2 * iface)
144 {
145     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
146     DWORD ref = InterlockedIncrement(&This->ref);
147
148     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, ref - 1);
149     return ref;
150 }
151
152 static ULONG WINAPI
153 fnCaptureGraphBuilder2_Release(ICaptureGraphBuilder2 * iface)
154 {
155     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
156     DWORD ref = InterlockedDecrement(&This->ref);
157
158     TRACE("(%p/%p)->() Release from %d\n", This, iface, ref + 1);
159
160     if (!ref)
161     {
162         FIXME("Release IGraphFilter or w/e\n");
163         This->csFilter.DebugInfo->Spare[0] = 0;
164         DeleteCriticalSection(&This->csFilter);
165         This->lpVtbl = NULL;
166         This->lpVtbl2 = NULL;
167         if (This->mygraph != NULL)
168             IGraphBuilder_Release((IGraphBuilder *)This->mygraph);
169         CoTaskMemFree(This);
170         ObjectRefCount(FALSE);
171     }
172     return ref;
173 }
174
175 static HRESULT WINAPI
176 fnCaptureGraphBuilder2_SetFilterGraph(ICaptureGraphBuilder2 * iface,
177                                       IGraphBuilder *pfg)
178 {
179 /* The graph builder will automatically create a filter graph if you don't call
180    this method. If you call this method after the graph builder has created its
181    own filter graph, the call will fail. */
182     IMediaEvent *pmev;
183     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
184
185     TRACE("(%p/%p)->(%p)\n", This, iface, pfg);
186
187     if (This->mygraph)
188         return E_UNEXPECTED;
189
190     if (!pfg)
191         return E_POINTER;
192
193     This->mygraph = pfg;
194     IGraphBuilder_AddRef((IGraphBuilder *)This->mygraph);
195     if (SUCCEEDED(IUnknown_QueryInterface(This->mygraph,
196                                           &IID_IMediaEvent, (LPVOID *)&pmev)))
197     {
198         IMediaEvent_CancelDefaultHandling(pmev, EC_REPAINT);
199         IMediaEvent_Release(pmev);
200     }
201     return S_OK;
202 }
203
204 static HRESULT WINAPI
205 fnCaptureGraphBuilder2_GetFilterGraph(ICaptureGraphBuilder2 * iface,
206                                       IGraphBuilder **pfg)
207 {
208     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
209
210     TRACE("(%p/%p)->(%p)\n", This, iface, pfg);
211
212     if (!pfg)
213         return E_POINTER;
214
215     *pfg = This->mygraph;
216     if (!This->mygraph)
217     {
218         TRACE("(%p) Getting NULL filtergraph\n", iface);
219         return E_UNEXPECTED;
220     }
221
222     IGraphBuilder_AddRef((IGraphBuilder *)This->mygraph);
223    
224     TRACE("(%p) return filtergraph %p\n", iface, *pfg);
225     return S_OK;
226 }
227
228 static HRESULT WINAPI
229 fnCaptureGraphBuilder2_SetOutputFileName(ICaptureGraphBuilder2 * iface,
230                                          const GUID *pType,
231                                          LPCOLESTR lpstrFile,
232                                          IBaseFilter **ppf,
233                                          IFileSinkFilter **ppSink)
234 {
235     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
236
237     FIXME("(%p/%p)->(%s, %s, %p, %p) Stub!\n", This, iface,
238           debugstr_guid(pType), debugstr_w(lpstrFile), ppf, ppSink);
239
240     return E_NOTIMPL;
241 }
242
243 static HRESULT WINAPI
244 fnCaptureGraphBuilder2_FindInterface(ICaptureGraphBuilder2 * iface,
245                                      const GUID *pCategory,
246                                      const GUID *pType,
247                                      IBaseFilter *pf,
248                                      REFIID riid,
249                                      void **ppint)
250 {
251     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
252
253     FIXME("(%p/%p)->(%s, %s, %p, %s, %p) - workaround stub!\n", This, iface,
254           debugstr_guid(pCategory), debugstr_guid(pType),
255           pf, debugstr_guid(riid), ppint);
256
257     return IBaseFilter_QueryInterface(pf, riid, ppint);
258     /* Looks for the specified interface on the filter, upstream and
259      * downstream from the filter, and, optionally, only on the output
260      * pin of the given category.
261      */
262 }
263
264 static HRESULT WINAPI
265 fnCaptureGraphBuilder2_RenderStream(ICaptureGraphBuilder2 * iface,
266                                     const GUID *pCategory,
267                                     const GUID *pType,
268                                     IUnknown *pSource,
269                                     IBaseFilter *pfCompressor,
270                                     IBaseFilter *pfRenderer)
271 {
272     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
273
274     FIXME("(%p/%p)->(%s, %s, %p, %p, %p) Stub!\n", This, iface,
275           debugstr_guid(pCategory), debugstr_guid(pType),
276           pSource, pfCompressor, pfRenderer);
277
278     return E_NOTIMPL;
279 }
280
281 static HRESULT WINAPI
282 fnCaptureGraphBuilder2_ControlStream(ICaptureGraphBuilder2 * iface,
283                                      const GUID *pCategory,
284                                      const GUID *pType,
285                                      IBaseFilter *pFilter,
286                                      REFERENCE_TIME *pstart,
287                                      REFERENCE_TIME *pstop,
288                                      WORD wStartCookie,
289                                      WORD wStopCookie)
290 {
291     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
292
293     FIXME("(%p/%p)->(%s, %s, %p, %p, %p, %i, %i) Stub!\n", This, iface,
294           debugstr_guid(pCategory), debugstr_guid(pType),
295           pFilter, pstart, pstop, wStartCookie, wStopCookie);
296
297     return E_NOTIMPL;
298 }
299
300 static HRESULT WINAPI
301 fnCaptureGraphBuilder2_AllocCapFile(ICaptureGraphBuilder2 * iface,
302                                     LPCOLESTR lpwstr,
303                                     DWORDLONG dwlSize)
304 {
305     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
306
307     FIXME("(%p/%p)->(%s, 0x%s) Stub!\n", This, iface,
308           debugstr_w(lpwstr), wine_dbgstr_longlong(dwlSize));
309
310     return E_NOTIMPL;
311 }
312
313 static HRESULT WINAPI
314 fnCaptureGraphBuilder2_CopyCaptureFile(ICaptureGraphBuilder2 * iface,
315                                        LPOLESTR lpwstrOld,
316                                        LPOLESTR lpwstrNew,
317                                        int fAllowEscAbort,
318                                        IAMCopyCaptureFileProgress *pCallback)
319 {
320     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
321
322     FIXME("(%p/%p)->(%s, %s, %i, %p) Stub!\n", This, iface,
323           debugstr_w(lpwstrOld), debugstr_w(lpwstrNew),
324           fAllowEscAbort, pCallback);
325
326     return E_NOTIMPL;
327 }
328
329 static HRESULT WINAPI
330 fnCaptureGraphBuilder2_FindPin(ICaptureGraphBuilder2 * iface,
331                                IUnknown *pSource,
332                                PIN_DIRECTION pindir,
333                                const GUID *pCategory,
334                                const GUID *pType,
335                                BOOL fUnconnected,
336                                int num,
337                                IPin **ppPin)
338 {
339     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder2(iface);
340
341     FIXME("(%p/%p)->(%p, %x, %s, %s, %d, %i, %p) Stub!\n", This, iface,
342           pSource, pindir, debugstr_guid(pCategory), debugstr_guid(pType),
343           fUnconnected, num, ppPin);
344
345     return E_NOTIMPL;
346 }
347
348 static const ICaptureGraphBuilder2Vtbl builder2_Vtbl =
349 {   
350     fnCaptureGraphBuilder2_QueryInterface,
351     fnCaptureGraphBuilder2_AddRef,
352     fnCaptureGraphBuilder2_Release,
353     fnCaptureGraphBuilder2_SetFilterGraph,
354     fnCaptureGraphBuilder2_GetFilterGraph,
355     fnCaptureGraphBuilder2_SetOutputFileName,
356     fnCaptureGraphBuilder2_FindInterface,
357     fnCaptureGraphBuilder2_RenderStream,
358     fnCaptureGraphBuilder2_ControlStream,
359     fnCaptureGraphBuilder2_AllocCapFile,
360     fnCaptureGraphBuilder2_CopyCaptureFile,
361     fnCaptureGraphBuilder2_FindPin
362 };
363
364
365 static HRESULT WINAPI
366 fnCaptureGraphBuilder_QueryInterface(ICaptureGraphBuilder * iface,
367                                      REFIID riid, LPVOID * ppv)
368 {
369     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
370     TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
371     return IUnknown_QueryInterface(_ICaptureGraphBuilder2_(This), riid, ppv);
372 }
373
374 static ULONG WINAPI
375 fnCaptureGraphBuilder_AddRef(ICaptureGraphBuilder * iface)
376 {
377     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
378     TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
379     return IUnknown_AddRef(_ICaptureGraphBuilder2_(This));
380 }
381
382 static ULONG WINAPI
383 fnCaptureGraphBuilder_Release(ICaptureGraphBuilder * iface)
384 {
385     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
386     TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
387     return IUnknown_Release(_ICaptureGraphBuilder2_(This));
388 }
389
390 static HRESULT WINAPI
391 fnCaptureGraphBuilder_SetFiltergraph(ICaptureGraphBuilder * iface,
392                                      IGraphBuilder *pfg)
393 {
394     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
395     TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
396     return ICaptureGraphBuilder2_SetFiltergraph(_ICaptureGraphBuilder2_(This), pfg);
397 }
398
399 static HRESULT WINAPI
400 fnCaptureGraphBuilder_GetFiltergraph(ICaptureGraphBuilder * iface,
401                                      IGraphBuilder **pfg)
402 {
403     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
404     TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
405     return ICaptureGraphBuilder2_GetFiltergraph(_ICaptureGraphBuilder2_(This), pfg);
406 }
407
408 static HRESULT WINAPI
409 fnCaptureGraphBuilder_SetOutputFileName(ICaptureGraphBuilder * iface,
410                                         const GUID *pType, LPCOLESTR lpstrFile,
411                                         IBaseFilter **ppf, IFileSinkFilter **ppSink)
412 {
413     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
414     TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
415     return ICaptureGraphBuilder2_SetOutputFileName(_ICaptureGraphBuilder2_(This),
416                                                    pType, lpstrFile, ppf, ppSink);
417 }
418
419 static HRESULT WINAPI
420 fnCaptureGraphBuilder_FindInterface(ICaptureGraphBuilder * iface,
421                                     const GUID *pCategory, IBaseFilter *pf,
422                                     REFIID riid, void **ppint)
423 {
424     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
425     TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
426     return ICaptureGraphBuilder2_FindInterface(_ICaptureGraphBuilder2_(This),
427                                                pCategory, NULL, pf, riid, ppint);
428 }
429
430 static HRESULT WINAPI
431 fnCaptureGraphBuilder_RenderStream(ICaptureGraphBuilder * iface,
432                                    const GUID *pCategory, IUnknown *pSource,
433                                    IBaseFilter *pfCompressor, IBaseFilter *pfRenderer)
434 {
435     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
436     TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
437     return ICaptureGraphBuilder2_RenderStream(_ICaptureGraphBuilder2_(This),
438                                               pCategory, NULL, pSource,
439                                               pfCompressor, pfRenderer);
440 }
441
442 static HRESULT WINAPI
443 fnCaptureGraphBuilder_ControlStream(ICaptureGraphBuilder * iface,
444                                     const GUID *pCategory, IBaseFilter *pFilter,
445                                     REFERENCE_TIME *pstart, REFERENCE_TIME *pstop,
446                                     WORD wStartCookie, WORD wStopCookie)
447 {
448     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
449     TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
450     return ICaptureGraphBuilder2_ControlStream(_ICaptureGraphBuilder2_(This),
451                                                pCategory, NULL, pFilter, pstart, 
452                                                pstop, wStartCookie, wStopCookie);
453 }
454
455 static HRESULT WINAPI
456 fnCaptureGraphBuilder_AllocCapFile(ICaptureGraphBuilder * iface,
457                                    LPCOLESTR lpstr, DWORDLONG dwlSize)
458 {
459     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
460     TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
461     return ICaptureGraphBuilder2_AllocCapFile(_ICaptureGraphBuilder2_(This),
462                                               lpstr, dwlSize);
463 }
464
465 static HRESULT WINAPI
466 fnCaptureGraphBuilder_CopyCaptureFile(ICaptureGraphBuilder * iface,
467                                       LPOLESTR lpwstrOld, LPOLESTR lpwstrNew,
468                                       int fAllowEscAbort,
469                                       IAMCopyCaptureFileProgress *pCallback)
470 {
471     CaptureGraphImpl *This = impl_from_ICaptureGraphBuilder(iface);
472     TRACE("%p --> Forwarding to v2 (%p)\n", iface, This);
473     return ICaptureGraphBuilder2_CopyCaptureFile(_ICaptureGraphBuilder2_(This),
474                                                  lpwstrOld, lpwstrNew,
475                                                  fAllowEscAbort, pCallback);
476 }
477
478 static const ICaptureGraphBuilderVtbl builder_Vtbl =
479 {   
480    fnCaptureGraphBuilder_QueryInterface,
481    fnCaptureGraphBuilder_AddRef,
482    fnCaptureGraphBuilder_Release,
483    fnCaptureGraphBuilder_SetFiltergraph,
484    fnCaptureGraphBuilder_GetFiltergraph,
485    fnCaptureGraphBuilder_SetOutputFileName,
486    fnCaptureGraphBuilder_FindInterface,
487    fnCaptureGraphBuilder_RenderStream,
488    fnCaptureGraphBuilder_ControlStream,
489    fnCaptureGraphBuilder_AllocCapFile,
490    fnCaptureGraphBuilder_CopyCaptureFile
491 };