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
21 #include "quartz_private.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
27 HRESULT CopyMediaType(AM_MEDIA_TYPE * pDest, const AM_MEDIA_TYPE *pSrc)
30 if (!pSrc->pbFormat) return S_OK;
31 if (!(pDest->pbFormat = CoTaskMemAlloc(pSrc->cbFormat)))
33 memcpy(pDest->pbFormat, pSrc->pbFormat, pSrc->cbFormat);
35 IUnknown_AddRef(pDest->pUnk);
39 void FreeMediaType(AM_MEDIA_TYPE * pMediaType)
41 if (pMediaType->pbFormat)
43 CoTaskMemFree(pMediaType->pbFormat);
44 pMediaType->pbFormat = NULL;
48 IUnknown_Release(pMediaType->pUnk);
49 pMediaType->pUnk = NULL;
53 static AM_MEDIA_TYPE * CreateMediaType(AM_MEDIA_TYPE const * pSrc)
55 AM_MEDIA_TYPE * pDest;
57 pDest = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
61 if (FAILED(CopyMediaType(pDest, pSrc)))
70 void DeleteMediaType(AM_MEDIA_TYPE * pMediaType)
72 FreeMediaType(pMediaType);
73 CoTaskMemFree(pMediaType);
76 BOOL CompareMediaTypes(const AM_MEDIA_TYPE * pmt1, const AM_MEDIA_TYPE * pmt2, BOOL bWildcards)
79 dump_AM_MEDIA_TYPE(pmt1);
81 dump_AM_MEDIA_TYPE(pmt2);
82 return (((bWildcards && (IsEqualGUID(&pmt1->majortype, &GUID_NULL) || IsEqualGUID(&pmt2->majortype, &GUID_NULL))) || IsEqualGUID(&pmt1->majortype, &pmt2->majortype)) &&
83 ((bWildcards && (IsEqualGUID(&pmt1->subtype, &GUID_NULL) || IsEqualGUID(&pmt2->subtype, &GUID_NULL))) || IsEqualGUID(&pmt1->subtype, &pmt2->subtype)));
86 void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE * pmt)
90 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", qzdebugstr_guid(&pmt->majortype), qzdebugstr_guid(&pmt->subtype), qzdebugstr_guid(&pmt->formattype));
93 typedef struct IEnumMediaTypesImpl
95 const IEnumMediaTypesVtbl * lpVtbl;
97 ENUMMEDIADETAILS enumMediaDetails;
99 } IEnumMediaTypesImpl;
101 static const struct IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl;
103 HRESULT IEnumMediaTypesImpl_Construct(const ENUMMEDIADETAILS * pDetails, IEnumMediaTypes ** ppEnum)
106 IEnumMediaTypesImpl * pEnumMediaTypes = CoTaskMemAlloc(sizeof(IEnumMediaTypesImpl));
108 if (!pEnumMediaTypes)
111 return E_OUTOFMEMORY;
113 pEnumMediaTypes->lpVtbl = &IEnumMediaTypesImpl_Vtbl;
114 pEnumMediaTypes->refCount = 1;
115 pEnumMediaTypes->uIndex = 0;
116 pEnumMediaTypes->enumMediaDetails.cMediaTypes = pDetails->cMediaTypes;
117 pEnumMediaTypes->enumMediaDetails.pMediaTypes = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * pDetails->cMediaTypes);
118 for (i = 0; i < pDetails->cMediaTypes; i++)
119 if (FAILED(CopyMediaType(&pEnumMediaTypes->enumMediaDetails.pMediaTypes[i], &pDetails->pMediaTypes[i])))
122 CoTaskMemFree(pEnumMediaTypes->enumMediaDetails.pMediaTypes[i].pbFormat);
123 CoTaskMemFree(pEnumMediaTypes->enumMediaDetails.pMediaTypes);
124 return E_OUTOFMEMORY;
126 *ppEnum = (IEnumMediaTypes *)(&pEnumMediaTypes->lpVtbl);
130 static HRESULT WINAPI IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes * iface, REFIID riid, LPVOID * ppv)
132 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
136 if (IsEqualIID(riid, &IID_IUnknown))
137 *ppv = (LPVOID)iface;
138 else if (IsEqualIID(riid, &IID_IEnumMediaTypes))
139 *ppv = (LPVOID)iface;
143 IUnknown_AddRef((IUnknown *)(*ppv));
147 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
149 return E_NOINTERFACE;
152 static ULONG WINAPI IEnumMediaTypesImpl_AddRef(IEnumMediaTypes * iface)
154 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
155 ULONG refCount = InterlockedIncrement(&This->refCount);
157 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
162 static ULONG WINAPI IEnumMediaTypesImpl_Release(IEnumMediaTypes * iface)
164 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
165 ULONG refCount = InterlockedDecrement(&This->refCount);
167 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
172 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
173 if (This->enumMediaDetails.pMediaTypes[i].pbFormat)
174 CoTaskMemFree(This->enumMediaDetails.pMediaTypes[i].pbFormat);
175 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
181 static HRESULT WINAPI IEnumMediaTypesImpl_Next(IEnumMediaTypes * iface, ULONG cMediaTypes, AM_MEDIA_TYPE ** ppMediaTypes, ULONG * pcFetched)
184 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
186 cFetched = min(This->enumMediaDetails.cMediaTypes, This->uIndex + cMediaTypes) - This->uIndex;
188 TRACE("(%u, %p, %p)\n", cMediaTypes, ppMediaTypes, pcFetched);
189 TRACE("Next uIndex: %u, cFetched: %u\n", This->uIndex, cFetched);
194 for (i = 0; i < cFetched; i++)
195 if (!(ppMediaTypes[i] = CreateMediaType(&This->enumMediaDetails.pMediaTypes[This->uIndex + i])))
198 DeleteMediaType(ppMediaTypes[i]);
200 return E_OUTOFMEMORY;
204 if ((cMediaTypes != 1) || pcFetched)
205 *pcFetched = cFetched;
207 This->uIndex += cFetched;
209 if (cFetched != cMediaTypes)
214 static HRESULT WINAPI IEnumMediaTypesImpl_Skip(IEnumMediaTypes * iface, ULONG cMediaTypes)
216 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
218 TRACE("(%u)\n", cMediaTypes);
220 if (This->uIndex + cMediaTypes < This->enumMediaDetails.cMediaTypes)
222 This->uIndex += cMediaTypes;
228 static HRESULT WINAPI IEnumMediaTypesImpl_Reset(IEnumMediaTypes * iface)
230 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
238 static HRESULT WINAPI IEnumMediaTypesImpl_Clone(IEnumMediaTypes * iface, IEnumMediaTypes ** ppEnum)
241 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
243 TRACE("(%p)\n", ppEnum);
245 hr = IEnumMediaTypesImpl_Construct(&This->enumMediaDetails, ppEnum);
248 return IEnumMediaTypes_Skip(*ppEnum, This->uIndex);
251 static const IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl =
253 IEnumMediaTypesImpl_QueryInterface,
254 IEnumMediaTypesImpl_AddRef,
255 IEnumMediaTypesImpl_Release,
256 IEnumMediaTypesImpl_Next,
257 IEnumMediaTypesImpl_Skip,
258 IEnumMediaTypesImpl_Reset,
259 IEnumMediaTypesImpl_Clone