advapi32: Add a bunch of file access tests.
[wine.git] / dlls / strmbase / mediatype.c
blob00934355783cfafcc81c82658666bb3affd41f9e
1 /*
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
21 #include <stdarg.h>
23 #define COBJMACROS
24 #include "dshow.h"
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)
33 *pDest = *pSrc;
34 if (!pSrc->pbFormat) return S_OK;
35 if (!(pDest->pbFormat = CoTaskMemAlloc(pSrc->cbFormat)))
36 return E_OUTOFMEMORY;
37 memcpy(pDest->pbFormat, pSrc->pbFormat, pSrc->cbFormat);
38 if (pDest->pUnk)
39 IUnknown_AddRef(pDest->pUnk);
40 return S_OK;
43 void WINAPI FreeMediaType(AM_MEDIA_TYPE * pMediaType)
45 if (pMediaType->pbFormat)
47 CoTaskMemFree(pMediaType->pbFormat);
48 pMediaType->pbFormat = NULL;
50 if (pMediaType->pUnk)
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));
62 if (!pDest)
63 return NULL;
65 if (FAILED(CopyMediaType(pDest, pSrc)))
67 CoTaskMemFree(pDest);
68 return NULL;
71 return pDest;
74 void WINAPI DeleteMediaType(AM_MEDIA_TYPE * pMediaType)
76 FreeMediaType(pMediaType);
77 CoTaskMemFree(pMediaType);
80 typedef struct tagENUMEDIADETAILS
82 ULONG cMediaTypes;
83 AM_MEDIA_TYPE * pMediaTypes;
84 } ENUMMEDIADETAILS;
86 typedef struct IEnumMediaTypesImpl
88 IEnumMediaTypes IEnumMediaTypes_iface;
89 LONG refCount;
90 BasePin *basePin;
91 BasePin_GetMediaType enumMediaFunction;
92 BasePin_GetMediaTypeVersion mediaVersionFunction;
93 LONG currentVersion;
94 ENUMMEDIADETAILS enumMediaDetails;
95 ULONG uIndex;
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)
107 ULONG i;
108 IEnumMediaTypesImpl * pEnumMediaTypes = CoTaskMemAlloc(sizeof(IEnumMediaTypesImpl));
109 AM_MEDIA_TYPE amt;
111 if (!pEnumMediaTypes)
113 *ppEnum = NULL;
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;
124 i = 0;
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)))
134 while (i--)
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);
142 return S_OK;
145 static HRESULT WINAPI IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes * iface, REFIID riid, LPVOID * ppv)
147 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
149 *ppv = NULL;
151 if (IsEqualIID(riid, &IID_IUnknown))
152 *ppv = iface;
153 else if (IsEqualIID(riid, &IID_IEnumMediaTypes))
154 *ppv = iface;
156 if (*ppv)
158 IUnknown_AddRef((IUnknown *)(*ppv));
159 return S_OK;
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);
174 return refCount;
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);
184 if (!refCount)
186 ULONG i;
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);
192 CoTaskMemFree(This);
194 return refCount;
197 static HRESULT WINAPI IEnumMediaTypesImpl_Next(IEnumMediaTypes * iface, ULONG cMediaTypes, AM_MEDIA_TYPE ** ppMediaTypes, ULONG * pcFetched)
199 ULONG cFetched;
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);
210 if (cFetched > 0)
212 ULONG i;
213 for (i = 0; i < cFetched; i++)
214 if (!(ppMediaTypes[i] = CreateMediaType(&This->enumMediaDetails.pMediaTypes[This->uIndex + i])))
216 while (i--)
217 DeleteMediaType(ppMediaTypes[i]);
218 *pcFetched = 0;
219 return E_OUTOFMEMORY;
223 if ((cMediaTypes != 1) || pcFetched)
224 *pcFetched = cFetched;
226 This->uIndex += cFetched;
228 if (cFetched != cMediaTypes)
229 return S_FALSE;
230 return S_OK;
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;
244 return S_OK;
246 return S_FALSE;
249 static HRESULT WINAPI IEnumMediaTypesImpl_Reset(IEnumMediaTypes * iface)
251 ULONG i;
252 AM_MEDIA_TYPE amt;
253 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
255 TRACE("()\n");
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);
262 i = 0;
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)))
272 while (i--)
273 CoTaskMemFree(This->enumMediaDetails.pMediaTypes[i].pbFormat);
274 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
275 return E_OUTOFMEMORY;
279 This->currentVersion = This->mediaVersionFunction(This->basePin);
280 This->uIndex = 0;
282 return S_OK;
285 static HRESULT WINAPI IEnumMediaTypesImpl_Clone(IEnumMediaTypes * iface, IEnumMediaTypes ** ppEnum)
287 HRESULT hr;
288 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
290 TRACE("(%p)\n", ppEnum);
292 hr = EnumMediaTypes_Construct(This->basePin, This->enumMediaFunction, This->mediaVersionFunction, ppEnum);
293 if (FAILED(hr))
294 return hr;
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