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
));
114 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
) i
++;
130 pEnumMediaTypes
->enumMediaDetails
.cMediaTypes
= i
;
131 pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
) * i
);
132 for (i
= 0; i
< pEnumMediaTypes
->enumMediaDetails
.cMediaTypes
; i
++)
134 enumFunc(basePin
,i
,&amt
);
135 if (FAILED(CopyMediaType(&pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
[i
], &amt
)))
138 FreeMediaType(&pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
[i
]);
139 CoTaskMemFree(pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
);
140 return E_OUTOFMEMORY
;
143 *ppEnum
= &pEnumMediaTypes
->IEnumMediaTypes_iface
;
144 pEnumMediaTypes
->currentVersion
= versionFunc(basePin
);
148 static HRESULT WINAPI
IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes
* iface
, REFIID riid
, void ** ret_iface
)
150 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ret_iface
);
152 if (IsEqualIID(riid
, &IID_IUnknown
) ||
153 IsEqualIID(riid
, &IID_IEnumMediaTypes
))
155 IEnumMediaTypes_AddRef(iface
);
162 WARN("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 ref
= InterlockedIncrement(&This
->refCount
);
172 TRACE("(%p)->(): new ref = %u\n", iface
, ref
);
177 static ULONG WINAPI
IEnumMediaTypesImpl_Release(IEnumMediaTypes
* iface
)
179 IEnumMediaTypesImpl
*This
= impl_from_IEnumMediaTypes(iface
);
180 ULONG ref
= InterlockedDecrement(&This
->refCount
);
182 TRACE("(%p)->(): new ref = %u\n", iface
, ref
);
187 for (i
= 0; i
< This
->enumMediaDetails
.cMediaTypes
; i
++)
188 FreeMediaType(&This
->enumMediaDetails
.pMediaTypes
[i
]);
189 CoTaskMemFree(This
->enumMediaDetails
.pMediaTypes
);
190 IPin_Release(&This
->basePin
->IPin_iface
);
196 static HRESULT WINAPI
IEnumMediaTypesImpl_Next(IEnumMediaTypes
* iface
, ULONG cMediaTypes
, AM_MEDIA_TYPE
** ppMediaTypes
, ULONG
* pcFetched
)
199 IEnumMediaTypesImpl
*This
= impl_from_IEnumMediaTypes(iface
);
201 TRACE("(%p)->(%u, %p, %p)\n", iface
, cMediaTypes
, ppMediaTypes
, pcFetched
);
203 cFetched
= min(This
->enumMediaDetails
.cMediaTypes
, This
->uIndex
+ cMediaTypes
) - This
->uIndex
;
205 if (This
->currentVersion
!= This
->mediaVersionFunction(This
->basePin
))
206 return VFW_E_ENUM_OUT_OF_SYNC
;
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("(%p)->(%u)\n", iface
, cMediaTypes
);
239 if (This
->currentVersion
!= This
->mediaVersionFunction(This
->basePin
))
240 return VFW_E_ENUM_OUT_OF_SYNC
;
242 if (This
->uIndex
+ cMediaTypes
< This
->enumMediaDetails
.cMediaTypes
)
244 This
->uIndex
+= cMediaTypes
;
250 static HRESULT WINAPI
IEnumMediaTypesImpl_Reset(IEnumMediaTypes
* iface
)
254 IEnumMediaTypesImpl
*This
= impl_from_IEnumMediaTypes(iface
);
256 TRACE("(%p)->()\n", iface
);
258 for (i
= 0; i
< This
->enumMediaDetails
.cMediaTypes
; i
++)
259 FreeMediaType(&This
->enumMediaDetails
.pMediaTypes
[i
]);
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 FreeMediaType(&This
->enumMediaDetails
.pMediaTypes
[i
]);
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)->(%p)\n", iface
, 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