strmbase: Implement OLE registration in AMovieDllRegisterServer2.
[wine] / dlls / qcap / qcap_main.c
1 /*
2  * Qcap implementation, dllentry points
3  *
4  * Copyright (C) 2003 Dominik Strasser
5  * Copyright (C) 2005 Rolf Kalbermatter
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 "config.h"
22
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26
27 #define COBJMACROS
28 #define NONAMELESSSTRUCT
29 #define NONAMELESSUNION
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winerror.h"
35 #include "objbase.h"
36 #include "uuids.h"
37 #include "strmif.h"
38
39 #include "qcap_main.h"
40
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(qcap);
45
46 static LONG objects_ref = 0;
47 static LONG server_locks = 0;
48
49 static const WCHAR wAudioCaptFilter[] =
50 {'A','u','d','i','o',' ','C','a','p','t','u','r','e',' ','F','i','l','t','e','r',0};
51 static const WCHAR wAVICompressor[] =
52 {'A','V','I',' ','C','o','m','p','r','e','s','s','o','r',0};
53 static const WCHAR wVFWCaptFilter[] =
54 {'V','F','W',' ','C','a','p','t','u','r','e',' ','F','i','l','t','e','r',0};
55 static const WCHAR wVFWCaptFilterProp[] =
56 {'V','F','W',' ','C','a','p','t','u','r','e',' ','F','i','l','t','e','r',' ',
57  'P','r','o','p','e','r','t','y',' ','P','a','g','e',0};
58 static const WCHAR wAVIMux[] =
59 {'A','V','I',' ','m','u','x',0};
60 static const WCHAR wAVIMuxPropPage[] =
61 {'A','V','I',' ','m','u','x',' ','P','r','o','p','e','r','t','y',' ','P','a','g','e',0};
62 static const WCHAR wAVIMuxPropPage1[] =
63 {'A','V','I',' ','m','u','x',' ','P','r','o','p','e','r','t','y',' ','P','a','g','e','1',0};
64 static const WCHAR wFileWriter[] =
65 {'F','i','l','e',' ','W','r','i','t','e','r',0};
66 static const WCHAR wCaptGraphBuilder[] =
67 {'C','a','p','t','u','r','e',' ','G','r','a','p','h',' ','B','u','i','l','d','e','r',0};
68 static const WCHAR wCaptGraphBuilder2[] =
69 {'C','a','p','t','u','r','e',' ','G','r','a','p','h',' ','B','u','i','l','d','e','r','2',0};
70 static const WCHAR wInfPinTeeFilter[] =
71 {'I','n','f','i','n','i','t','e',' ','P','i','n',' ','T','e','e',' ','F','i',
72  'l','t','e','r',0};
73 static const WCHAR wSmartTeeFilter[] =
74 {'S','m','a','r','t',' ','T','e','e',' ','F','i','l','t','e','r',0};
75 static const WCHAR wAudioInMixerProp[] =
76 {'A','u','d','i','o','I','n','p','u','t','M','i','x','e','r',' ','P','r','o',
77  'p','e','r','t','y',' ','P','a','g','e',0};
78  
79 FactoryTemplate const g_Templates[] = {
80 /*
81     {
82         wAudioCaptureFilter, 
83         &CLSID_AudioCaptureFilter,
84         QCAP_createAudioCaptureFilter,
85         NULL
86     },{
87         wAVICompressor, 
88         &CLSID_AVICompressor, 
89         QCAP_createAVICompressor,
90         NULL
91     },*/{
92         wVFWCaptFilter,
93         &CLSID_VfwCapture,
94         QCAP_createVFWCaptureFilter,
95         NULL
96     },/*{
97         wVFWCaptFilterProp,
98         &CLSID_VFWCaptureFilterPropertyPage,
99         QCAP_createVFWCaptureFilterPropertyPage,
100         NULL
101     },{
102         wAVIMux,
103         &CLSID_AVImux,
104         QCAP_createAVImux,
105         NULL
106     },{
107         wAVIMuxPropPage,
108         &CLSID_AVImuxPropertyPage,
109         QCAP_createAVImuxPropertyPage,
110         NULL
111     },{
112         wAVIMuxPropPage1,
113         &CLSID_AVImuxPropertyPage1,
114         QCAP_createAVImuxPropertyPage1,
115         NULL
116     },{
117         wFileWriter,
118         &CLSID_FileWriter,
119         QCAP_createFileWriter,
120         NULL
121     },*/{
122         wCaptGraphBuilder,
123         &CLSID_CaptureGraphBuilder,
124         QCAP_createCaptureGraphBuilder2,
125         NULL
126     },{
127         wCaptGraphBuilder2,
128         &CLSID_CaptureGraphBuilder2,
129         QCAP_createCaptureGraphBuilder2,
130         NULL
131     }/*,{
132         wInfPinTeeFilter, 
133         &CLSID_InfinitePinTeeFilter, 
134         QCAP_createInfinitePinTeeFilter,
135         NULL
136     },{
137         wSmartTeeFilter,
138         &CLSID_SmartTeeFilter,
139         QCAP_createSmartTeeFilter,
140         NULL
141     },{
142         wAudioInMixerProp,
143         &CLSID_AudioInputMixerPropertyPage,
144         QCAP_createAudioInputMixerPropertyPage,
145         NULL
146     }*/
147 };
148
149 int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
150
151 /***********************************************************************
152  *    Dll EntryPoint (QCAP.@)
153  */
154 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
155 {
156     return STRMBASE_DllMain(hInstDLL,fdwReason,lpv);
157 }
158
159 /***********************************************************************
160  *    DllRegisterServer (QCAP.@)
161  */
162 HRESULT WINAPI DllRegisterServer(void)
163 {
164     TRACE("()\n");
165     return AMovieDllRegisterServer2(TRUE);
166 }
167
168 /***********************************************************************
169  *    DllUnregisterServer (QCAP.@)
170  */
171 HRESULT WINAPI DllUnregisterServer(void)
172 {
173     TRACE("\n");
174     return AMovieDllRegisterServer2(FALSE);
175 }
176
177 /***********************************************************************
178  *    DllCanUnloadNow (QCAP.@)
179  */
180 HRESULT WINAPI DllCanUnloadNow(void)
181 {
182     TRACE("\n");
183
184     if (objects_ref == 0 && server_locks == 0)
185         return S_OK;
186     return S_FALSE;     
187 }
188
189 /******************************************************************************
190  * DLL ClassFactory
191  */
192 typedef struct {
193     IClassFactory ITF_IClassFactory;
194
195     LONG ref;
196     LPFNNewCOMObject pfnCreateInstance;
197 } IClassFactoryImpl;
198
199 static HRESULT WINAPI
200 DSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
201 {
202     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
203
204     if (IsEqualGUID(riid, &IID_IUnknown) ||
205         IsEqualGUID(riid, &IID_IClassFactory))
206     {
207         IClassFactory_AddRef(iface);
208         *ppobj = This;
209         return S_OK;
210     }
211
212     WARN("(%p)->(%s,%p), not found\n", This, debugstr_guid(riid), ppobj);
213     return E_NOINTERFACE;
214 }
215
216 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
217 {
218     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
219     return InterlockedIncrement(&This->ref);
220 }
221
222 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
223 {
224     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
225
226     ULONG ref = InterlockedDecrement(&This->ref);
227
228     if (ref == 0)
229         HeapFree(GetProcessHeap(), 0, This);
230
231     return ref;
232 }
233
234 static HRESULT WINAPI DSCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter,
235                                           REFIID riid, LPVOID *ppobj)
236 {
237     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
238     HRESULT hres = ERROR_SUCCESS;
239     LPUNKNOWN punk;
240
241     TRACE("(%p)->(%p,%s,%p)\n", This, pOuter, debugstr_guid(riid), ppobj);
242
243     if (!ppobj)
244         return E_POINTER;
245
246     /* Enforce the normal OLE rules regarding interfaces and delegation */
247     if (pOuter && !IsEqualGUID(riid, &IID_IUnknown))
248         return E_NOINTERFACE;
249
250     *ppobj = NULL;
251     punk = This->pfnCreateInstance(pOuter, &hres);
252     if (!punk)
253     {
254         /* No object created, update error if it isn't done already and return */
255         if (SUCCEEDED(hres))
256             hres = E_OUTOFMEMORY;
257     return hres;
258     }
259
260     if (SUCCEEDED(hres))
261     {
262         hres = IUnknown_QueryInterface(punk, riid, ppobj);
263     }
264     /* Releasing the object. If everything was successful, QueryInterface
265        should have incremented the refcount once more, otherwise this will
266        purge the object. */
267     IUnknown_Release(punk);
268     return hres;
269 }
270
271 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
272 {
273     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
274     TRACE("(%p)->(%d)\n",This, dolock);
275
276     if (dolock)
277         InterlockedIncrement(&server_locks);
278     else
279         InterlockedDecrement(&server_locks);
280     return S_OK;
281 }
282
283 static const IClassFactoryVtbl DSCF_Vtbl =
284 {
285     DSCF_QueryInterface,
286     DSCF_AddRef,
287     DSCF_Release,
288     DSCF_CreateInstance,
289     DSCF_LockServer
290 };
291
292 /***********************************************************************
293  *    DllGetClassObject (QCAP.@)
294  */
295 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
296 {
297     const FactoryTemplate *pList = g_Templates;
298     IClassFactoryImpl *factory;
299     int i;
300
301     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
302
303     if (!ppv)
304         return E_POINTER;
305
306     *ppv = NULL;
307
308     if (!IsEqualGUID(&IID_IClassFactory, riid) &&
309         !IsEqualGUID(&IID_IUnknown, riid))
310         return E_NOINTERFACE;
311
312     for (i = 0; i < g_cTemplates; i++, pList++)
313     {
314         if (IsEqualGUID(pList->m_ClsID, rclsid))
315             break;
316     }
317
318     if (i == g_cTemplates)
319     {
320         FIXME("%s: no class found.\n", debugstr_guid(rclsid));
321         return CLASS_E_CLASSNOTAVAILABLE;
322     }
323
324     factory = HeapAlloc(GetProcessHeap(), 0, sizeof(IClassFactoryImpl));
325     if (!factory)
326         return E_OUTOFMEMORY;
327
328     factory->ITF_IClassFactory.lpVtbl = &DSCF_Vtbl;
329     factory->ref = 1;
330
331     factory->pfnCreateInstance = pList->m_lpfnNew;
332
333     *ppv = &(factory->ITF_IClassFactory);
334     return S_OK;
335 }
336
337 DWORD ObjectRefCount(BOOL increment)
338 {
339     if (increment)
340         return InterlockedIncrement(&objects_ref);
341     return InterlockedDecrement(&objects_ref);
342 }