[AMSTREAM]
[reactos.git] / reactos / dll / directx / wine / amstream / main.c
1 /*
2 * MultiMedia Streams Base Functions (AMSTREAM.DLL)
3 *
4 * Copyright 2004 Christian Costa
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
21 #include "amstream_private.h"
22
23 #include <rpcproxy.h>
24
25 static HINSTANCE instance;
26 static DWORD dll_ref = 0;
27
28 /* For the moment, do nothing here. */
29 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
30 {
31 switch(fdwReason) {
32 case DLL_PROCESS_ATTACH:
33 instance = hInstDLL;
34 DisableThreadLibraryCalls(hInstDLL);
35 break;
36 }
37 return TRUE;
38 }
39
40 /******************************************************************************
41 * Multimedia Streams ClassFactory
42 */
43 typedef struct {
44 IClassFactory IClassFactory_iface;
45 LONG ref;
46 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
47 } IClassFactoryImpl;
48
49 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
50 {
51 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
52 }
53
54 struct object_creation_info
55 {
56 const CLSID *clsid;
57 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
58 };
59
60 static const struct object_creation_info object_creation[] =
61 {
62 { &CLSID_AMMultiMediaStream, AM_create },
63 { &CLSID_AMDirectDrawStream, AM_create },
64 { &CLSID_AMAudioData, AMAudioData_create },
65 { &CLSID_MediaStreamFilter, MediaStreamFilter_create }
66 };
67
68 static HRESULT WINAPI AMCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppobj)
69 {
70 if (IsEqualGUID(riid, &IID_IUnknown)
71 || IsEqualGUID(riid, &IID_IClassFactory))
72 {
73 IClassFactory_AddRef(iface);
74 *ppobj = iface;
75 return S_OK;
76 }
77
78 *ppobj = NULL;
79 WARN("(%p)->(%s,%p), not found\n", iface, debugstr_guid(riid), ppobj);
80 return E_NOINTERFACE;
81 }
82
83 static ULONG WINAPI AMCF_AddRef(IClassFactory *iface)
84 {
85 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
86 return InterlockedIncrement(&This->ref);
87 }
88
89 static ULONG WINAPI AMCF_Release(IClassFactory *iface)
90 {
91 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
92 ULONG ref = InterlockedDecrement(&This->ref);
93
94 if (ref == 0)
95 HeapFree(GetProcessHeap(), 0, This);
96
97 return ref;
98 }
99
100
101 static HRESULT WINAPI AMCF_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
102 REFIID riid, void **ppobj)
103 {
104 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
105 HRESULT hres;
106 LPUNKNOWN punk;
107
108 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
109
110 *ppobj = NULL;
111 hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk);
112 if (SUCCEEDED(hres)) {
113 hres = IUnknown_QueryInterface(punk, riid, ppobj);
114 IUnknown_Release(punk);
115 }
116 return hres;
117 }
118
119 static HRESULT WINAPI AMCF_LockServer(IClassFactory *iface, BOOL dolock)
120 {
121 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
122 FIXME("(%p)->(%d),stub!\n",This,dolock);
123 return S_OK;
124 }
125
126 static const IClassFactoryVtbl DSCF_Vtbl =
127 {
128 AMCF_QueryInterface,
129 AMCF_AddRef,
130 AMCF_Release,
131 AMCF_CreateInstance,
132 AMCF_LockServer
133 };
134
135 /*******************************************************************************
136 * DllGetClassObject [AMSTREAM.@]
137 * Retrieves class object from a DLL object
138 *
139 * NOTES
140 * Docs say returns STDAPI
141 *
142 * PARAMS
143 * rclsid [I] CLSID for the class object
144 * riid [I] Reference to identifier of interface for class object
145 * ppv [O] Address of variable to receive interface pointer for riid
146 *
147 * RETURNS
148 * Success: S_OK
149 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
150 * E_UNEXPECTED
151 */
152 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
153 {
154 unsigned int i;
155 IClassFactoryImpl *factory;
156
157 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
158
159 if ( !IsEqualGUID( &IID_IClassFactory, riid )
160 && ! IsEqualGUID( &IID_IUnknown, riid) )
161 return E_NOINTERFACE;
162
163 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
164 {
165 if (IsEqualGUID(object_creation[i].clsid, rclsid))
166 break;
167 }
168
169 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
170 {
171 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
172 return CLASS_E_CLASSNOTAVAILABLE;
173 }
174
175 factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
176 if (factory == NULL) return E_OUTOFMEMORY;
177
178 factory->IClassFactory_iface.lpVtbl = &DSCF_Vtbl;
179 factory->ref = 1;
180
181 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
182
183 *ppv = &factory->IClassFactory_iface;
184 return S_OK;
185 }
186
187 /***********************************************************************
188 * DllCanUnloadNow (AMSTREAM.@)
189 */
190 HRESULT WINAPI DllCanUnloadNow(void)
191 {
192 return dll_ref != 0 ? S_FALSE : S_OK;
193 }
194
195 /***********************************************************************
196 * DllRegisterServer (AMSTREAM.@)
197 */
198 HRESULT WINAPI DllRegisterServer(void)
199 {
200 return __wine_register_resources( instance );
201 }
202
203 /***********************************************************************
204 * DllUnregisterServer (AMSTREAM.@)
205 */
206 HRESULT WINAPI DllUnregisterServer(void)
207 {
208 return __wine_unregister_resources( instance );
209 }