1 /*
2 * Implementation of IEnumMediaTypes Interface
3 *
4 * Copyright 2003 Robert Shearman
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 <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wtypes.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "dshow.h"
31
32 #include "qcap_main.h"
33
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(qcap);
37
38 HRESULT CopyMediaType(AM_MEDIA_TYPE * pDest, const AM_MEDIA_TYPE *pSrc)
39 {
40 *pDest = *pSrc;
41 if (!pSrc->pbFormat) return S_OK;
42 if (!(pDest->pbFormat = CoTaskMemAlloc(pSrc->cbFormat)))
43 return E_OUTOFMEMORY;
44 memcpy(pDest->pbFormat, pSrc->pbFormat, pSrc->cbFormat);
45 return S_OK;
46 }
47
48 static void FreeMediaType(AM_MEDIA_TYPE * pMediaType)
49 {
50 CoTaskMemFree(pMediaType->pbFormat);
51 pMediaType->pbFormat = NULL;
52
53 if (pMediaType->pUnk)
54 {
55 IUnknown_Release(pMediaType->pUnk);
56 pMediaType->pUnk = NULL;
57 }
58 }
59
60 void DeleteMediaType(AM_MEDIA_TYPE * pMediaType)
61 {
62 FreeMediaType(pMediaType);
63 CoTaskMemFree(pMediaType);
64 }
65
66 BOOL CompareMediaTypes(const AM_MEDIA_TYPE * pmt1, const AM_MEDIA_TYPE * pmt2,
67 BOOL bWildcards)
68 {
69 TRACE("pmt1: ");
70 dump_AM_MEDIA_TYPE(pmt1);
71 TRACE("pmt2: ");
72 dump_AM_MEDIA_TYPE(pmt2);
73 return (((bWildcards && (IsEqualGUID(&pmt1->majortype, &GUID_NULL) ||
74 IsEqualGUID(&pmt2->majortype, &GUID_NULL))) ||
75 IsEqualGUID(&pmt1->majortype, &pmt2->majortype)) &&
76 ((bWildcards && (IsEqualGUID(&pmt1->subtype, &GUID_NULL) ||
77 IsEqualGUID(&pmt2->subtype, &GUID_NULL))) ||
78 IsEqualGUID(&pmt1->subtype, &pmt2->subtype)));
79 }
80
81 void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE * pmt)
82 {
83 if (!pmt)
84 return;
85 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt->majortype),
86 debugstr_guid(&pmt->subtype), debugstr_guid(&pmt->formattype));
87 }
88
89 typedef struct IEnumMediaTypesImpl
90 {
91 const IEnumMediaTypesVtbl * lpVtbl;
92 LONG refCount;
93 ENUMMEDIADETAILS enumMediaDetails;
94 ULONG uIndex;
95 } IEnumMediaTypesImpl;
96
97 static const struct IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl;
98
99 HRESULT IEnumMediaTypesImpl_Construct(const ENUMMEDIADETAILS * pDetails,
100 IEnumMediaTypes ** ppEnum)
101 {
102 ULONG i;
103 IEnumMediaTypesImpl * pEnumMediaTypes = CoTaskMemAlloc(sizeof(IEnumMediaTypesImpl));
104
105 if (!pEnumMediaTypes)
106 {
107 *ppEnum = NULL;
108 return E_OUTOFMEMORY;
109 }
110 ObjectRefCount(TRUE);
111 pEnumMediaTypes->lpVtbl = &IEnumMediaTypesImpl_Vtbl;
112 pEnumMediaTypes->refCount = 1;
113 pEnumMediaTypes->uIndex = 0;
114 pEnumMediaTypes->enumMediaDetails.cMediaTypes = pDetails->cMediaTypes;
115 pEnumMediaTypes->enumMediaDetails.pMediaTypes =
116 CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * pDetails->cMediaTypes);
117 for (i = 0; i < pDetails->cMediaTypes; i++)
118 if (FAILED(CopyMediaType(&pEnumMediaTypes->enumMediaDetails.pMediaTypes[i], &pDetails->pMediaTypes[i]))) {
119 while (i--) CoTaskMemFree(pEnumMediaTypes->enumMediaDetails.pMediaTypes[i].pbFormat);
120 CoTaskMemFree(pEnumMediaTypes->enumMediaDetails.pMediaTypes);
121 return E_OUTOFMEMORY;
122 }
123 *ppEnum = (IEnumMediaTypes *)(&pEnumMediaTypes->lpVtbl);
124 return S_OK;
125 }
126
127 static HRESULT WINAPI IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes * iface,
128 REFIID riid,
129 LPVOID * ppv)
130 {
131 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
132
133 *ppv = NULL;
134
135 if (IsEqualIID(riid, &IID_IUnknown))
136 *ppv = iface;
137 else if (IsEqualIID(riid, &IID_IEnumMediaTypes))
138 *ppv = iface;
139
140 if (*ppv)
141 {
142 IUnknown_AddRef((IUnknown *)(*ppv));
143 return S_OK;
144 }
145
146 FIXME("No interface for %s!\n", debugstr_guid(riid));
147
148 return E_NOINTERFACE;
149 }
150
151 static ULONG WINAPI IEnumMediaTypesImpl_AddRef(IEnumMediaTypes * iface)
152 {
153 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
154 ULONG refCount = InterlockedIncrement(&This->refCount);
155
156 TRACE("()\n");
157
158 return refCount;
159 }
160
161 static ULONG WINAPI IEnumMediaTypesImpl_Release(IEnumMediaTypes * iface)
162 {
163 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
164 ULONG refCount = InterlockedDecrement(&This->refCount);
165
166 TRACE("()\n");
167
168 if (!refCount)
169 {
170 ULONG i;
171 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
172 if (This->enumMediaDetails.pMediaTypes[i].pbFormat)
173 CoTaskMemFree(This->enumMediaDetails.pMediaTypes[i].pbFormat);
174 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
175 CoTaskMemFree(This);
176 ObjectRefCount(FALSE);
177 }
178 return refCount;
179 }
180
181 static HRESULT WINAPI IEnumMediaTypesImpl_Next(IEnumMediaTypes * iface,
182 ULONG cMediaTypes,
183 AM_MEDIA_TYPE ** ppMediaTypes,
184 ULONG * pcFetched)
185 {
186 ULONG cFetched;
187 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
188
189 cFetched = min(This->enumMediaDetails.cMediaTypes,
190 This->uIndex + cMediaTypes) - This->uIndex;
191
192 TRACE("(%u, %p, %p)\n", cMediaTypes, ppMediaTypes, pcFetched);
193 TRACE("Next uIndex: %u, cFetched: %u\n", This->uIndex, cFetched);
194
195 if (cFetched > 0)
196 {
197 ULONG i;
198 *ppMediaTypes = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * cFetched);
199 for (i = 0; i < cFetched; i++)
200 if (FAILED(CopyMediaType(&(*ppMediaTypes)[i], &This->enumMediaDetails.pMediaTypes[This->uIndex + i]))) {
201 while (i--)
202 CoTaskMemFree((*ppMediaTypes)[i].pbFormat);
203 CoTaskMemFree(*ppMediaTypes);
204 *ppMediaTypes = NULL;
205 return E_OUTOFMEMORY;
206 }
207 }
208
209 if ((cMediaTypes != 1) || pcFetched)
210 *pcFetched = cFetched;
211
212 This->uIndex += cFetched;
213
214 if (cFetched != cMediaTypes)
215 return S_FALSE;
216 return S_OK;
217 }
218
219 static HRESULT WINAPI IEnumMediaTypesImpl_Skip(IEnumMediaTypes * iface,
220 ULONG cMediaTypes)
221 {
222 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
223
224 TRACE("(%u)\n", cMediaTypes);
225
226 if (This->uIndex + cMediaTypes < This->enumMediaDetails.cMediaTypes)
227 {
228 This->uIndex += cMediaTypes;
229 return S_OK;
230 }
231 return S_FALSE;
232 }
233
234 static HRESULT WINAPI IEnumMediaTypesImpl_Reset(IEnumMediaTypes * iface)
235 {
236 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
237
238 TRACE("()\n");
239
240 This->uIndex = 0;
241 return S_OK;
242 }
243
244 static HRESULT WINAPI IEnumMediaTypesImpl_Clone(IEnumMediaTypes * iface,
245 IEnumMediaTypes ** ppEnum)
246 {
247 HRESULT hr;
248 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
249
250 TRACE("(%p)\n", ppEnum);
251
252 hr = IEnumMediaTypesImpl_Construct(&This->enumMediaDetails, ppEnum);
253 if (FAILED(hr))
254 return hr;
255 return IEnumMediaTypes_Skip(*ppEnum, This->uIndex);
256 }
257
258 static const IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl =
259 {
260 IEnumMediaTypesImpl_QueryInterface,
261 IEnumMediaTypesImpl_AddRef,
262 IEnumMediaTypesImpl_Release,
263 IEnumMediaTypesImpl_Next,
264 IEnumMediaTypesImpl_Skip,
265 IEnumMediaTypesImpl_Reset,
266 IEnumMediaTypesImpl_Clone
267 };
268
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.