2 * Implementation of MedaType utility functions
4 * Copyright 2003 Robert Shearman
5 * Copyright 2010 Aric Stewart, CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/strmbase.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
31 HRESULT WINAPI CopyMediaType(AM_MEDIA_TYPE * pDest, const AM_MEDIA_TYPE *pSrc)
34 if (!pSrc->pbFormat) return S_OK;
35 if (!(pDest->pbFormat = CoTaskMemAlloc(pSrc->cbFormat)))
37 memcpy(pDest->pbFormat, pSrc->pbFormat, pSrc->cbFormat);
39 IUnknown_AddRef(pDest->pUnk);
43 void WINAPI FreeMediaType(AM_MEDIA_TYPE * pMediaType)
45 if (pMediaType->pbFormat)
47 CoTaskMemFree(pMediaType->pbFormat);
48 pMediaType->pbFormat = NULL;
52 IUnknown_Release(pMediaType->pUnk);
53 pMediaType->pUnk = NULL;
57 AM_MEDIA_TYPE * WINAPI CreateMediaType(AM_MEDIA_TYPE const * pSrc)
59 AM_MEDIA_TYPE * pDest;
61 pDest = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
65 if (FAILED(CopyMediaType(pDest, pSrc)))
74 void WINAPI DeleteMediaType(AM_MEDIA_TYPE * pMediaType)
76 FreeMediaType(pMediaType);
77 CoTaskMemFree(pMediaType);
80 typedef struct tagENUMEDIADETAILS
83 AM_MEDIA_TYPE * pMediaTypes;
86 typedef struct IEnumMediaTypesImpl
88 IEnumMediaTypes IEnumMediaTypes_iface;
91 BasePin_GetMediaType enumMediaFunction;
92 BasePin_GetMediaTypeVersion mediaVersionFunction;
94 ENUMMEDIADETAILS enumMediaDetails;
96 } IEnumMediaTypesImpl;
98 static inline IEnumMediaTypesImpl *impl_from_IEnumMediaTypes(IEnumMediaTypes *iface)
100 return CONTAINING_RECORD(iface, IEnumMediaTypesImpl, IEnumMediaTypes_iface);
103 static const struct IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl;
105 HRESULT WINAPI EnumMediaTypes_Construct(BasePin *basePin, BasePin_GetMediaType enumFunc, BasePin_GetMediaTypeVersion versionFunc, IEnumMediaTypes ** ppEnum)
108 IEnumMediaTypesImpl * pEnumMediaTypes = CoTaskMemAlloc(sizeof(IEnumMediaTypesImpl));
111 if (!pEnumMediaTypes)
114 return E_OUTOFMEMORY;
116 pEnumMediaTypes->IEnumMediaTypes_iface.lpVtbl = &IEnumMediaTypesImpl_Vtbl;
117 pEnumMediaTypes->refCount = 1;
118 pEnumMediaTypes->uIndex = 0;
119 pEnumMediaTypes->enumMediaFunction = enumFunc;
120 pEnumMediaTypes->mediaVersionFunction = versionFunc;
121 IPin_AddRef((IPin*)basePin);
122 pEnumMediaTypes->basePin = basePin;
125 while (enumFunc(basePin,i,&amt) == S_OK) i++;
127 pEnumMediaTypes->enumMediaDetails.cMediaTypes = i;
128 pEnumMediaTypes->enumMediaDetails.pMediaTypes = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * i);
129 for (i = 0; i < pEnumMediaTypes->enumMediaDetails.cMediaTypes; i++)
131 enumFunc(basePin,i,&amt);
132 if (FAILED(CopyMediaType(&pEnumMediaTypes->enumMediaDetails.pMediaTypes[i], &amt)))
135 CoTaskMemFree(pEnumMediaTypes->enumMediaDetails.pMediaTypes[i].pbFormat);
136 CoTaskMemFree(pEnumMediaTypes->enumMediaDetails.pMediaTypes);
137 return E_OUTOFMEMORY;
140 *ppEnum = &pEnumMediaTypes->IEnumMediaTypes_iface;
141 pEnumMediaTypes->currentVersion = versionFunc(basePin);
145 static HRESULT WINAPI IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes * iface, REFIID riid, LPVOID * ppv)
147 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
151 if (IsEqualIID(riid, &IID_IUnknown))
153 else if (IsEqualIID(riid, &IID_IEnumMediaTypes))
158 IUnknown_AddRef((IUnknown *)(*ppv));
162 FIXME("No interface for %s!\n", debugstr_guid(riid));
164 return E_NOINTERFACE;
167 static ULONG WINAPI IEnumMediaTypesImpl_AddRef(IEnumMediaTypes * iface)
169 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
170 ULONG refCount = InterlockedIncrement(&This->refCount);
172 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
177 static ULONG WINAPI IEnumMediaTypesImpl_Release(IEnumMediaTypes * iface)
179 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
180 ULONG refCount = InterlockedDecrement(&This->refCount);
182 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
187 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
188 if (This->enumMediaDetails.pMediaTypes[i].pbFormat)
189 CoTaskMemFree(This->enumMediaDetails.pMediaTypes[i].pbFormat);
190 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
191 IPin_Release((IPin*)This->basePin);
197 static HRESULT WINAPI IEnumMediaTypesImpl_Next(IEnumMediaTypes * iface, ULONG cMediaTypes, AM_MEDIA_TYPE ** ppMediaTypes, ULONG * pcFetched)
200 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
202 cFetched = min(This->enumMediaDetails.cMediaTypes, This->uIndex + cMediaTypes) - This->uIndex;
204 if (This->currentVersion != This->mediaVersionFunction(This->basePin))
205 return VFW_E_ENUM_OUT_OF_SYNC;
207 TRACE("(%u, %p, %p)\n", cMediaTypes, ppMediaTypes, pcFetched);
208 TRACE("Next uIndex: %u, cFetched: %u\n", This->uIndex, cFetched);
213 for (i = 0; i < cFetched; i++)
214 if (!(ppMediaTypes[i] = CreateMediaType(&This->enumMediaDetails.pMediaTypes[This->uIndex + i])))
217 DeleteMediaType(ppMediaTypes[i]);
219 return E_OUTOFMEMORY;
223 if ((cMediaTypes != 1) || pcFetched)
224 *pcFetched = cFetched;
226 This->uIndex += cFetched;
228 if (cFetched != cMediaTypes)
233 static HRESULT WINAPI IEnumMediaTypesImpl_Skip(IEnumMediaTypes * iface, ULONG cMediaTypes)
235 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
237 TRACE("(%u)\n", cMediaTypes);
238 if (This->currentVersion != This->mediaVersionFunction(This->basePin))
239 return VFW_E_ENUM_OUT_OF_SYNC;
241 if (This->uIndex + cMediaTypes < This->enumMediaDetails.cMediaTypes)
243 This->uIndex += cMediaTypes;
249 static HRESULT WINAPI IEnumMediaTypesImpl_Reset(IEnumMediaTypes * iface)
253 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
257 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
258 if (This->enumMediaDetails.pMediaTypes[i].pbFormat)
259 CoTaskMemFree(This->enumMediaDetails.pMediaTypes[i].pbFormat);
260 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
263 while (This->enumMediaFunction(This->basePin, i,&amt) == S_OK) i++;
265 This->enumMediaDetails.cMediaTypes = i;
266 This->enumMediaDetails.pMediaTypes = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * i);
267 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
269 This->enumMediaFunction(This->basePin, i,&amt);
270 if (FAILED(CopyMediaType(&This->enumMediaDetails.pMediaTypes[i], &amt)))
273 CoTaskMemFree(This->enumMediaDetails.pMediaTypes[i].pbFormat);
274 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
275 return E_OUTOFMEMORY;
279 This->currentVersion = This->mediaVersionFunction(This->basePin);
285 static HRESULT WINAPI IEnumMediaTypesImpl_Clone(IEnumMediaTypes * iface, IEnumMediaTypes ** ppEnum)
288 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
290 TRACE("(%p)\n", ppEnum);
292 hr = EnumMediaTypes_Construct(This->basePin, This->enumMediaFunction, This->mediaVersionFunction, ppEnum);
295 return IEnumMediaTypes_Skip(*ppEnum, This->uIndex);
298 static const IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl =
300 IEnumMediaTypesImpl_QueryInterface,
301 IEnumMediaTypesImpl_AddRef,
302 IEnumMediaTypesImpl_Release,
303 IEnumMediaTypesImpl_Next,
304 IEnumMediaTypesImpl_Skip,
305 IEnumMediaTypesImpl_Reset,
306 IEnumMediaTypesImpl_Clone