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