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