2 * Implementation of IEnumMediaTypes Interface
4 * Copyright 2003 Robert Shearman
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.
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.
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
32 #include "qcap_main.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(qcap);
38 HRESULT CopyMediaType(AM_MEDIA_TYPE * pDest, const AM_MEDIA_TYPE *pSrc)
41 if (!pSrc->pbFormat) return S_OK;
42 if (!(pDest->pbFormat = CoTaskMemAlloc(pSrc->cbFormat)))
44 memcpy(pDest->pbFormat, pSrc->pbFormat, pSrc->cbFormat);
48 static void FreeMediaType(AM_MEDIA_TYPE * pMediaType)
50 CoTaskMemFree(pMediaType->pbFormat);
51 pMediaType->pbFormat = NULL;
55 IUnknown_Release(pMediaType->pUnk);
56 pMediaType->pUnk = NULL;
60 void DeleteMediaType(AM_MEDIA_TYPE * pMediaType)
62 FreeMediaType(pMediaType);
63 CoTaskMemFree(pMediaType);
66 BOOL CompareMediaTypes(const AM_MEDIA_TYPE * pmt1, const AM_MEDIA_TYPE * pmt2,
70 dump_AM_MEDIA_TYPE(pmt1);
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)));
81 void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE * pmt)
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));
89 typedef struct IEnumMediaTypesImpl
91 const IEnumMediaTypesVtbl * lpVtbl;
93 ENUMMEDIADETAILS enumMediaDetails;
95 } IEnumMediaTypesImpl;
97 static const struct IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl;
99 HRESULT IEnumMediaTypesImpl_Construct(const ENUMMEDIADETAILS * pDetails,
100 IEnumMediaTypes ** ppEnum)
103 IEnumMediaTypesImpl * pEnumMediaTypes = CoTaskMemAlloc(sizeof(IEnumMediaTypesImpl));
105 if (!pEnumMediaTypes)
108 return E_OUTOFMEMORY;
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;
123 *ppEnum = (IEnumMediaTypes *)(&pEnumMediaTypes->lpVtbl);
127 static HRESULT WINAPI IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes * iface,
131 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
135 if (IsEqualIID(riid, &IID_IUnknown))
137 else if (IsEqualIID(riid, &IID_IEnumMediaTypes))
142 IUnknown_AddRef((IUnknown *)(*ppv));
146 FIXME("No interface for %s!\n", debugstr_guid(riid));
148 return E_NOINTERFACE;
151 static ULONG WINAPI IEnumMediaTypesImpl_AddRef(IEnumMediaTypes * iface)
153 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
154 ULONG refCount = InterlockedIncrement(&This->refCount);
161 static ULONG WINAPI IEnumMediaTypesImpl_Release(IEnumMediaTypes * iface)
163 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
164 ULONG refCount = InterlockedDecrement(&This->refCount);
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);
176 ObjectRefCount(FALSE);
181 static HRESULT WINAPI IEnumMediaTypesImpl_Next(IEnumMediaTypes * iface,
183 AM_MEDIA_TYPE ** ppMediaTypes,
187 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
189 cFetched = min(This->enumMediaDetails.cMediaTypes,
190 This->uIndex + cMediaTypes) - This->uIndex;
192 TRACE("(%u, %p, %p)\n", cMediaTypes, ppMediaTypes, pcFetched);
193 TRACE("Next uIndex: %u, cFetched: %u\n", This->uIndex, cFetched);
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]))) {
202 CoTaskMemFree((*ppMediaTypes)[i].pbFormat);
203 CoTaskMemFree(*ppMediaTypes);
204 *ppMediaTypes = NULL;
205 return E_OUTOFMEMORY;
209 if ((cMediaTypes != 1) || pcFetched)
210 *pcFetched = cFetched;
212 This->uIndex += cFetched;
214 if (cFetched != cMediaTypes)
219 static HRESULT WINAPI IEnumMediaTypesImpl_Skip(IEnumMediaTypes * iface,
222 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
224 TRACE("(%u)\n", cMediaTypes);
226 if (This->uIndex + cMediaTypes < This->enumMediaDetails.cMediaTypes)
228 This->uIndex += cMediaTypes;
234 static HRESULT WINAPI IEnumMediaTypesImpl_Reset(IEnumMediaTypes * iface)
236 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
244 static HRESULT WINAPI IEnumMediaTypesImpl_Clone(IEnumMediaTypes * iface,
245 IEnumMediaTypes ** ppEnum)
248 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
250 TRACE("(%p)\n", ppEnum);
252 hr = IEnumMediaTypesImpl_Construct(&This->enumMediaDetails, ppEnum);
255 return IEnumMediaTypes_Skip(*ppEnum, This->uIndex);
258 static const IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl =
260 IEnumMediaTypesImpl_QueryInterface,
261 IEnumMediaTypesImpl_AddRef,
262 IEnumMediaTypesImpl_Release,
263 IEnumMediaTypesImpl_Next,
264 IEnumMediaTypesImpl_Skip,
265 IEnumMediaTypesImpl_Reset,
266 IEnumMediaTypesImpl_Clone