quartz: Fix converting between the media time and REFTIME.
[wine] / dlls / quartz / main.c
1 /*              DirectShow Base Functions (QUARTZ.DLL)
2  *
3  * Copyright 2002 Lionel Ulmer
4  *
5  * This file contains the (internal) driver registration functions,
6  * driver enumeration APIs and DirectDraw creation functions.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/debug.h"
25
26 #include "quartz_private.h"
27 #include "wine/unicode.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
30
31 extern HRESULT WINAPI QUARTZ_DllGetClassObject(REFCLSID, REFIID, LPVOID *) DECLSPEC_HIDDEN;
32 extern HRESULT WINAPI QUARTZ_DllCanUnloadNow(void) DECLSPEC_HIDDEN;
33 extern BOOL WINAPI QUARTZ_DllMain(HINSTANCE, DWORD, LPVOID) DECLSPEC_HIDDEN;
34
35 static DWORD dll_ref = 0;
36
37 /* For the moment, do nothing here. */
38 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
39 {
40     if (fdwReason == DLL_PROCESS_DETACH)
41         video_unregister_windowclass();
42     return QUARTZ_DllMain( hInstDLL, fdwReason, lpv );
43 }
44
45 /******************************************************************************
46  * DirectShow ClassFactory
47  */
48 typedef struct {
49     IClassFactory ITF_IClassFactory;
50
51     LONG ref;
52     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
53 } IClassFactoryImpl;
54
55 struct object_creation_info
56 {
57     const CLSID *clsid;
58     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
59 };
60
61 static const struct object_creation_info object_creation[] =
62 {
63     { &CLSID_SeekingPassThru, SeekingPassThru_create },
64     { &CLSID_FilterGraph, FilterGraph_create },
65     { &CLSID_FilterGraphNoThread, FilterGraphNoThread_create },
66     { &CLSID_FilterMapper, FilterMapper_create },
67     { &CLSID_FilterMapper2, FilterMapper2_create },
68     { &CLSID_AsyncReader, AsyncReader_create },
69     { &CLSID_MemoryAllocator, StdMemAllocator_create },
70     { &CLSID_AviSplitter, AVISplitter_create },
71     { &CLSID_MPEG1Splitter, MPEGSplitter_create },
72     { &CLSID_VideoRenderer, VideoRenderer_create },
73     { &CLSID_NullRenderer, NullRenderer_create },
74     { &CLSID_VideoRendererDefault, VideoRendererDefault_create },
75     { &CLSID_DSoundRender, DSoundRender_create },
76     { &CLSID_AudioRender, DSoundRender_create },
77     { &CLSID_AVIDec, AVIDec_create },
78     { &CLSID_SystemClock, QUARTZ_CreateSystemClock },
79     { &CLSID_ACMWrapper, ACMWrapper_create },
80     { &CLSID_WAVEParser, WAVEParser_create }
81 };
82
83 static HRESULT WINAPI
84 DSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
85 {
86     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
87
88     if (IsEqualGUID(riid, &IID_IUnknown)
89         || IsEqualGUID(riid, &IID_IClassFactory))
90     {
91         IClassFactory_AddRef(iface);
92         *ppobj = This;
93         return S_OK;
94     }
95
96     *ppobj = NULL;
97     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
98     return E_NOINTERFACE;
99 }
100
101 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
102 {
103     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
104     return InterlockedIncrement(&This->ref);
105 }
106
107 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
108 {
109     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
110
111     ULONG ref = InterlockedDecrement(&This->ref);
112
113     if (ref == 0)
114         CoTaskMemFree(This);
115
116     return ref;
117 }
118
119
120 static HRESULT WINAPI DSCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter,
121                                           REFIID riid, LPVOID *ppobj)
122 {
123     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
124     HRESULT hres;
125     LPUNKNOWN punk;
126     
127     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
128
129     *ppobj = NULL;
130     hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk);
131     if (SUCCEEDED(hres)) {
132         hres = IUnknown_QueryInterface(punk, riid, ppobj);
133         IUnknown_Release(punk);
134     }
135     return hres;
136 }
137
138 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
139 {
140     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
141     FIXME("(%p)->(%d),stub!\n",This,dolock);
142     return S_OK;
143 }
144
145 static const IClassFactoryVtbl DSCF_Vtbl =
146 {
147     DSCF_QueryInterface,
148     DSCF_AddRef,
149     DSCF_Release,
150     DSCF_CreateInstance,
151     DSCF_LockServer
152 };
153
154 /*******************************************************************************
155  * DllGetClassObject [QUARTZ.@]
156  * Retrieves class object from a DLL object
157  *
158  * NOTES
159  *    Docs say returns STDAPI
160  *
161  * PARAMS
162  *    rclsid [I] CLSID for the class object
163  *    riid   [I] Reference to identifier of interface for class object
164  *    ppv    [O] Address of variable to receive interface pointer for riid
165  *
166  * RETURNS
167  *    Success: S_OK
168  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
169  *             E_UNEXPECTED
170  */
171 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
172 {
173     unsigned int i;
174
175     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
176
177     if (IsEqualGUID( &IID_IClassFactory, riid ) || IsEqualGUID( &IID_IUnknown, riid))
178     {
179         for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
180         {
181             if (IsEqualGUID(object_creation[i].clsid, rclsid))
182             {
183                 IClassFactoryImpl *factory = CoTaskMemAlloc(sizeof(*factory));
184                 if (factory == NULL) return E_OUTOFMEMORY;
185
186                 factory->ITF_IClassFactory.lpVtbl = &DSCF_Vtbl;
187                 factory->ref = 1;
188
189                 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
190
191                 *ppv = &factory->ITF_IClassFactory;
192                 return S_OK;
193             }
194         }
195     }
196     return QUARTZ_DllGetClassObject( rclsid, riid, ppv );
197 }
198
199 /***********************************************************************
200  *              DllCanUnloadNow (QUARTZ.@)
201  */
202 HRESULT WINAPI DllCanUnloadNow(void)
203 {
204     if (dll_ref) return S_FALSE;
205     return QUARTZ_DllCanUnloadNow();
206 }
207
208
209 #define OUR_GUID_ENTRY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
210     { { l, w1, w2, { b1, b2,  b3,  b4,  b5,  b6,  b7,  b8 } } , #name },
211
212 static const struct {
213         const GUID      riid;
214         const char      *name;
215 } InterfaceDesc[] =
216 {
217 #include "uuids.h"
218     { { 0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0} }, NULL }
219 };
220
221 /***********************************************************************
222  *              proxies
223  */
224 HRESULT CALLBACK ICaptureGraphBuilder_FindInterface_Proxy( ICaptureGraphBuilder *This,
225                                                            const GUID *pCategory,
226                                                            IBaseFilter *pf,
227                                                            REFIID riid,
228                                                            void **ppint )
229 {
230     return ICaptureGraphBuilder_RemoteFindInterface_Proxy( This, pCategory, pf,
231                                                            riid, (IUnknown **)ppint );
232 }
233
234 HRESULT __RPC_STUB ICaptureGraphBuilder_FindInterface_Stub( ICaptureGraphBuilder *This,
235                                                             const GUID *pCategory,
236                                                             IBaseFilter *pf,
237                                                             REFIID riid,
238                                                             IUnknown **ppint )
239 {
240     return ICaptureGraphBuilder_FindInterface( This, pCategory, pf, riid, (void **)ppint );
241 }
242
243 HRESULT CALLBACK ICaptureGraphBuilder2_FindInterface_Proxy( ICaptureGraphBuilder2 *This,
244                                                             const GUID *pCategory,
245                                                             const GUID *pType,
246                                                             IBaseFilter *pf,
247                                                             REFIID riid,
248                                                             void **ppint )
249 {
250     return ICaptureGraphBuilder2_RemoteFindInterface_Proxy( This, pCategory, pType,
251                                                             pf, riid, (IUnknown **)ppint );
252 }
253
254 HRESULT __RPC_STUB ICaptureGraphBuilder2_FindInterface_Stub( ICaptureGraphBuilder2 *This,
255                                                              const GUID *pCategory,
256                                                              const GUID *pType,
257                                                              IBaseFilter *pf,
258                                                              REFIID riid,
259                                                              IUnknown **ppint )
260 {
261     return ICaptureGraphBuilder2_FindInterface( This, pCategory, pType, pf, riid, (void **)ppint );
262 }
263
264 /***********************************************************************
265  *              qzdebugstr_guid (internal)
266  *
267  * Gives a text version of DirectShow GUIDs
268  */
269 const char * qzdebugstr_guid( const GUID * id )
270 {
271     int i;
272     char * name = NULL;
273
274     for (i=0;InterfaceDesc[i].name && !name;i++) {
275         if (IsEqualGUID(&InterfaceDesc[i].riid, id)) return InterfaceDesc[i].name;
276     }
277     return debugstr_guid(id);
278 }
279
280 LONG WINAPI AmpFactorToDB(LONG ampfactor)
281 {
282     FIXME("(%d) Stub!\n", ampfactor);
283     return 0;
284 }
285
286 LONG WINAPI DBToAmpFactor(LONG db)
287 {
288     FIXME("(%d) Stub!\n", db);
289     /* Avoid divide by zero (probably during range computation) in Windows Media Player 6.4 */
290     if (db < -1000)
291         return 0;
292     return 100;
293 }
294
295 /***********************************************************************
296  *              AMGetErrorTextA (QUARTZ.@)
297  */
298 DWORD WINAPI AMGetErrorTextA(HRESULT hr, LPSTR buffer, DWORD maxlen)
299 {
300     DWORD res;
301     WCHAR errorW[MAX_ERROR_TEXT_LEN];
302
303     TRACE("(%x,%p,%d)\n", hr, buffer, maxlen);
304     if (!buffer)
305         return 0;
306
307     res = AMGetErrorTextW(hr, errorW, sizeof(errorW)/sizeof(*errorW));
308     return WideCharToMultiByte(CP_ACP, 0, errorW, res, buffer, maxlen, 0, 0);
309 }
310
311 /***********************************************************************
312  *              AMGetErrorTextW (QUARTZ.@)
313  */
314 DWORD WINAPI AMGetErrorTextW(HRESULT hr, LPWSTR buffer, DWORD maxlen)
315 {
316     unsigned int len;
317     static const WCHAR format[] = {'E','r','r','o','r',':',' ','0','x','%','l','x',0};
318     WCHAR error[MAX_ERROR_TEXT_LEN];
319
320     FIXME("(%x,%p,%d) stub\n", hr, buffer, maxlen);
321
322     if (!buffer) return 0;
323     wsprintfW(error, format, hr);
324     if ((len = strlenW(error)) >= maxlen) return 0;
325     lstrcpyW(buffer, error);
326     return len;
327 }