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
*dest
, const AM_MEDIA_TYPE
*src
)
36 dest
->pbFormat
= CoTaskMemAlloc(src
->cbFormat
);
39 memcpy(dest
->pbFormat
, src
->pbFormat
, src
->cbFormat
);
42 IUnknown_AddRef(dest
->pUnk
);
46 void WINAPI
FreeMediaType(AM_MEDIA_TYPE
* pMediaType
)
48 if (pMediaType
->pbFormat
)
50 CoTaskMemFree(pMediaType
->pbFormat
);
51 pMediaType
->pbFormat
= NULL
;
55 IUnknown_Release(pMediaType
->pUnk
);
56 pMediaType
->pUnk
= NULL
;
60 AM_MEDIA_TYPE
* WINAPI
CreateMediaType(AM_MEDIA_TYPE
const * pSrc
)
62 AM_MEDIA_TYPE
* pDest
;
64 pDest
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
68 if (FAILED(CopyMediaType(pDest
, pSrc
)))
77 void WINAPI
DeleteMediaType(AM_MEDIA_TYPE
* pMediaType
)
79 FreeMediaType(pMediaType
);
80 CoTaskMemFree(pMediaType
);
83 typedef struct tagENUMEDIADETAILS
86 AM_MEDIA_TYPE
* pMediaTypes
;
89 typedef struct IEnumMediaTypesImpl
91 IEnumMediaTypes IEnumMediaTypes_iface
;
94 BasePin_GetMediaType enumMediaFunction
;
95 BasePin_GetMediaTypeVersion mediaVersionFunction
;
97 ENUMMEDIADETAILS enumMediaDetails
;
99 } IEnumMediaTypesImpl
;
101 static inline IEnumMediaTypesImpl
*impl_from_IEnumMediaTypes(IEnumMediaTypes
*iface
)
103 return CONTAINING_RECORD(iface
, IEnumMediaTypesImpl
, IEnumMediaTypes_iface
);
106 static const struct IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl
;
108 HRESULT WINAPI
EnumMediaTypes_Construct(BasePin
*basePin
, BasePin_GetMediaType enumFunc
, BasePin_GetMediaTypeVersion versionFunc
, IEnumMediaTypes
** ppEnum
)
111 IEnumMediaTypesImpl
* pEnumMediaTypes
= CoTaskMemAlloc(sizeof(IEnumMediaTypesImpl
));
116 if (!pEnumMediaTypes
)
117 return E_OUTOFMEMORY
;
119 pEnumMediaTypes
->IEnumMediaTypes_iface
.lpVtbl
= &IEnumMediaTypesImpl_Vtbl
;
120 pEnumMediaTypes
->refCount
= 1;
121 pEnumMediaTypes
->uIndex
= 0;
122 pEnumMediaTypes
->enumMediaFunction
= enumFunc
;
123 pEnumMediaTypes
->mediaVersionFunction
= versionFunc
;
124 IPin_AddRef(&basePin
->IPin_iface
);
125 pEnumMediaTypes
->basePin
= basePin
;
128 while (enumFunc(basePin
, i
, &amt
) == S_OK
)
134 pEnumMediaTypes
->enumMediaDetails
.cMediaTypes
= i
;
135 pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
) * i
);
136 memset(pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
, 0, sizeof(AM_MEDIA_TYPE
) * i
);
137 for (i
= 0; i
< pEnumMediaTypes
->enumMediaDetails
.cMediaTypes
; i
++)
141 if (FAILED(hr
= enumFunc(basePin
, i
, &pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
[i
])))
143 IEnumMediaTypes_Release(&pEnumMediaTypes
->IEnumMediaTypes_iface
);
147 *ppEnum
= &pEnumMediaTypes
->IEnumMediaTypes_iface
;
148 pEnumMediaTypes
->currentVersion
= versionFunc(basePin
);
152 static HRESULT WINAPI
IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes
* iface
, REFIID riid
, void ** ret_iface
)
154 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ret_iface
);
156 if (IsEqualIID(riid
, &IID_IUnknown
) ||
157 IsEqualIID(riid
, &IID_IEnumMediaTypes
))
159 IEnumMediaTypes_AddRef(iface
);
166 WARN("No interface for %s\n", debugstr_guid(riid
));
168 return E_NOINTERFACE
;
171 static ULONG WINAPI
IEnumMediaTypesImpl_AddRef(IEnumMediaTypes
* iface
)
173 IEnumMediaTypesImpl
*This
= impl_from_IEnumMediaTypes(iface
);
174 ULONG ref
= InterlockedIncrement(&This
->refCount
);
176 TRACE("(%p)->(): new ref = %u\n", iface
, ref
);
181 static ULONG WINAPI
IEnumMediaTypesImpl_Release(IEnumMediaTypes
* iface
)
183 IEnumMediaTypesImpl
*This
= impl_from_IEnumMediaTypes(iface
);
184 ULONG ref
= InterlockedDecrement(&This
->refCount
);
186 TRACE("(%p)->(): new ref = %u\n", iface
, ref
);
191 for (i
= 0; i
< This
->enumMediaDetails
.cMediaTypes
; i
++)
192 FreeMediaType(&This
->enumMediaDetails
.pMediaTypes
[i
]);
193 CoTaskMemFree(This
->enumMediaDetails
.pMediaTypes
);
194 IPin_Release(&This
->basePin
->IPin_iface
);
200 static HRESULT WINAPI
IEnumMediaTypesImpl_Next(IEnumMediaTypes
* iface
, ULONG cMediaTypes
, AM_MEDIA_TYPE
** ppMediaTypes
, ULONG
* pcFetched
)
203 IEnumMediaTypesImpl
*This
= impl_from_IEnumMediaTypes(iface
);
205 TRACE("(%p)->(%u, %p, %p)\n", iface
, cMediaTypes
, ppMediaTypes
, pcFetched
);
207 cFetched
= min(This
->enumMediaDetails
.cMediaTypes
, This
->uIndex
+ cMediaTypes
) - This
->uIndex
;
209 if (This
->currentVersion
!= This
->mediaVersionFunction(This
->basePin
))
210 return VFW_E_ENUM_OUT_OF_SYNC
;
212 TRACE("Next uIndex: %u, cFetched: %u\n", This
->uIndex
, cFetched
);
217 for (i
= 0; i
< cFetched
; i
++)
218 if (!(ppMediaTypes
[i
] = CreateMediaType(&This
->enumMediaDetails
.pMediaTypes
[This
->uIndex
+ i
])))
221 DeleteMediaType(ppMediaTypes
[i
]);
223 return E_OUTOFMEMORY
;
227 if ((cMediaTypes
!= 1) || pcFetched
)
228 *pcFetched
= cFetched
;
230 This
->uIndex
+= cFetched
;
232 if (cFetched
!= cMediaTypes
)
237 static HRESULT WINAPI
IEnumMediaTypesImpl_Skip(IEnumMediaTypes
* iface
, ULONG cMediaTypes
)
239 IEnumMediaTypesImpl
*This
= impl_from_IEnumMediaTypes(iface
);
241 TRACE("(%p)->(%u)\n", iface
, cMediaTypes
);
243 if (This
->currentVersion
!= This
->mediaVersionFunction(This
->basePin
))
244 return VFW_E_ENUM_OUT_OF_SYNC
;
246 if (This
->uIndex
+ cMediaTypes
< This
->enumMediaDetails
.cMediaTypes
)
248 This
->uIndex
+= cMediaTypes
;
254 static HRESULT WINAPI
IEnumMediaTypesImpl_Reset(IEnumMediaTypes
* iface
)
258 IEnumMediaTypesImpl
*This
= impl_from_IEnumMediaTypes(iface
);
260 TRACE("(%p)->()\n", iface
);
262 for (i
= 0; i
< This
->enumMediaDetails
.cMediaTypes
; i
++)
263 FreeMediaType(&This
->enumMediaDetails
.pMediaTypes
[i
]);
264 CoTaskMemFree(This
->enumMediaDetails
.pMediaTypes
);
267 while (This
->enumMediaFunction(This
->basePin
, i
, &amt
) == S_OK
) i
++;
269 This
->enumMediaDetails
.cMediaTypes
= i
;
270 This
->enumMediaDetails
.pMediaTypes
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
) * i
);
271 for (i
= 0; i
< This
->enumMediaDetails
.cMediaTypes
; i
++)
273 This
->enumMediaFunction(This
->basePin
, i
,&amt
);
274 if (FAILED(CopyMediaType(&This
->enumMediaDetails
.pMediaTypes
[i
], &amt
)))
277 FreeMediaType(&This
->enumMediaDetails
.pMediaTypes
[i
]);
278 CoTaskMemFree(This
->enumMediaDetails
.pMediaTypes
);
279 return E_OUTOFMEMORY
;
283 This
->currentVersion
= This
->mediaVersionFunction(This
->basePin
);
289 static HRESULT WINAPI
IEnumMediaTypesImpl_Clone(IEnumMediaTypes
* iface
, IEnumMediaTypes
** ppEnum
)
292 IEnumMediaTypesImpl
*This
= impl_from_IEnumMediaTypes(iface
);
294 TRACE("(%p)->(%p)\n", iface
, ppEnum
);
296 hr
= EnumMediaTypes_Construct(This
->basePin
, This
->enumMediaFunction
, This
->mediaVersionFunction
, ppEnum
);
299 return IEnumMediaTypes_Skip(*ppEnum
, This
->uIndex
);
302 static const IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl
=
304 IEnumMediaTypesImpl_QueryInterface
,
305 IEnumMediaTypesImpl_AddRef
,
306 IEnumMediaTypesImpl_Release
,
307 IEnumMediaTypesImpl_Next
,
308 IEnumMediaTypesImpl_Skip
,
309 IEnumMediaTypesImpl_Reset
,
310 IEnumMediaTypesImpl_Clone