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 const IEnumMediaTypesVtbl
* lpVtbl
;
91 BasePin_GetMediaType enumMediaFunction
;
92 BasePin_GetMediaTypeVersion mediaVersionFunction
;
94 ENUMMEDIADETAILS enumMediaDetails
;
96 } IEnumMediaTypesImpl
;
98 static const struct IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl
;
100 HRESULT WINAPI
EnumMediaTypes_Construct(BasePin
*basePin
, BasePin_GetMediaType enumFunc
, BasePin_GetMediaTypeVersion versionFunc
, IEnumMediaTypes
** ppEnum
)
103 IEnumMediaTypesImpl
* pEnumMediaTypes
= CoTaskMemAlloc(sizeof(IEnumMediaTypesImpl
));
106 if (!pEnumMediaTypes
)
109 return E_OUTOFMEMORY
;
111 pEnumMediaTypes
->lpVtbl
= &IEnumMediaTypesImpl_Vtbl
;
112 pEnumMediaTypes
->refCount
= 1;
113 pEnumMediaTypes
->uIndex
= 0;
114 pEnumMediaTypes
->enumMediaFunction
= enumFunc
;
115 pEnumMediaTypes
->mediaVersionFunction
= versionFunc
;
116 IPin_AddRef((IPin
*)basePin
);
117 pEnumMediaTypes
->basePin
= basePin
;
120 while (enumFunc(basePin
,i
,&amt
) == S_OK
) i
++;
122 pEnumMediaTypes
->enumMediaDetails
.cMediaTypes
= i
;
123 pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
) * i
);
124 for (i
= 0; i
< pEnumMediaTypes
->enumMediaDetails
.cMediaTypes
; i
++)
126 enumFunc(basePin
,i
,&amt
);
127 if (FAILED(CopyMediaType(&pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
[i
], &amt
)))
130 CoTaskMemFree(pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
[i
].pbFormat
);
131 CoTaskMemFree(pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
);
132 return E_OUTOFMEMORY
;
135 *ppEnum
= (IEnumMediaTypes
*)(&pEnumMediaTypes
->lpVtbl
);
136 pEnumMediaTypes
->currentVersion
= versionFunc(basePin
);
140 static HRESULT WINAPI
IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes
* iface
, REFIID riid
, LPVOID
* ppv
)
142 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
146 if (IsEqualIID(riid
, &IID_IUnknown
))
148 else if (IsEqualIID(riid
, &IID_IEnumMediaTypes
))
153 IUnknown_AddRef((IUnknown
*)(*ppv
));
157 FIXME("No interface for %s!\n", debugstr_guid(riid
));
159 return E_NOINTERFACE
;
162 static ULONG WINAPI
IEnumMediaTypesImpl_AddRef(IEnumMediaTypes
* iface
)
164 IEnumMediaTypesImpl
*This
= (IEnumMediaTypesImpl
*)iface
;
165 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
167 TRACE("(%p)->() AddRef from %d\n", iface
, refCount
- 1);
172 static ULONG WINAPI
IEnumMediaTypesImpl_Release(IEnumMediaTypes
* iface
)
174 IEnumMediaTypesImpl
*This
= (IEnumMediaTypesImpl
*)iface
;
175 ULONG refCount
= InterlockedDecrement(&This
->refCount
);
177 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
182 for (i
= 0; i
< This
->enumMediaDetails
.cMediaTypes
; i
++)
183 if (This
->enumMediaDetails
.pMediaTypes
[i
].pbFormat
)
184 CoTaskMemFree(This
->enumMediaDetails
.pMediaTypes
[i
].pbFormat
);
185 CoTaskMemFree(This
->enumMediaDetails
.pMediaTypes
);
186 IPin_Release((IPin
*)This
->basePin
);
192 static HRESULT WINAPI
IEnumMediaTypesImpl_Next(IEnumMediaTypes
* iface
, ULONG cMediaTypes
, AM_MEDIA_TYPE
** ppMediaTypes
, ULONG
* pcFetched
)
195 IEnumMediaTypesImpl
*This
= (IEnumMediaTypesImpl
*)iface
;
197 cFetched
= min(This
->enumMediaDetails
.cMediaTypes
, This
->uIndex
+ cMediaTypes
) - This
->uIndex
;
199 if (This
->currentVersion
!= This
->mediaVersionFunction(This
->basePin
))
200 return VFW_E_ENUM_OUT_OF_SYNC
;
202 TRACE("(%u, %p, %p)\n", cMediaTypes
, ppMediaTypes
, pcFetched
);
203 TRACE("Next uIndex: %u, cFetched: %u\n", This
->uIndex
, cFetched
);
208 for (i
= 0; i
< cFetched
; i
++)
209 if (!(ppMediaTypes
[i
] = CreateMediaType(&This
->enumMediaDetails
.pMediaTypes
[This
->uIndex
+ i
])))
212 DeleteMediaType(ppMediaTypes
[i
]);
214 return E_OUTOFMEMORY
;
218 if ((cMediaTypes
!= 1) || pcFetched
)
219 *pcFetched
= cFetched
;
221 This
->uIndex
+= cFetched
;
223 if (cFetched
!= cMediaTypes
)
228 static HRESULT WINAPI
IEnumMediaTypesImpl_Skip(IEnumMediaTypes
* iface
, ULONG cMediaTypes
)
230 IEnumMediaTypesImpl
*This
= (IEnumMediaTypesImpl
*)iface
;
232 TRACE("(%u)\n", cMediaTypes
);
233 if (This
->currentVersion
!= This
->mediaVersionFunction(This
->basePin
))
234 return VFW_E_ENUM_OUT_OF_SYNC
;
236 if (This
->uIndex
+ cMediaTypes
< This
->enumMediaDetails
.cMediaTypes
)
238 This
->uIndex
+= cMediaTypes
;
244 static HRESULT WINAPI
IEnumMediaTypesImpl_Reset(IEnumMediaTypes
* iface
)
248 IEnumMediaTypesImpl
*This
= (IEnumMediaTypesImpl
*)iface
;
252 for (i
= 0; i
< This
->enumMediaDetails
.cMediaTypes
; i
++)
253 if (This
->enumMediaDetails
.pMediaTypes
[i
].pbFormat
)
254 CoTaskMemFree(This
->enumMediaDetails
.pMediaTypes
[i
].pbFormat
);
255 CoTaskMemFree(This
->enumMediaDetails
.pMediaTypes
);
258 while (This
->enumMediaFunction(This
->basePin
, i
,&amt
) == S_OK
) i
++;
260 This
->enumMediaDetails
.cMediaTypes
= i
;
261 This
->enumMediaDetails
.pMediaTypes
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
) * i
);
262 for (i
= 0; i
< This
->enumMediaDetails
.cMediaTypes
; i
++)
264 This
->enumMediaFunction(This
->basePin
, i
,&amt
);
265 if (FAILED(CopyMediaType(&This
->enumMediaDetails
.pMediaTypes
[i
], &amt
)))
268 CoTaskMemFree(This
->enumMediaDetails
.pMediaTypes
[i
].pbFormat
);
269 CoTaskMemFree(This
->enumMediaDetails
.pMediaTypes
);
270 return E_OUTOFMEMORY
;
274 This
->currentVersion
= This
->mediaVersionFunction(This
->basePin
);
280 static HRESULT WINAPI
IEnumMediaTypesImpl_Clone(IEnumMediaTypes
* iface
, IEnumMediaTypes
** ppEnum
)
283 IEnumMediaTypesImpl
*This
= (IEnumMediaTypesImpl
*)iface
;
285 TRACE("(%p)\n", ppEnum
);
287 hr
= EnumMediaTypes_Construct(This
->basePin
, This
->enumMediaFunction
, This
->mediaVersionFunction
, ppEnum
);
290 return IEnumMediaTypes_Skip(*ppEnum
, This
->uIndex
);
293 static const IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl
=
295 IEnumMediaTypesImpl_QueryInterface
,
296 IEnumMediaTypesImpl_AddRef
,
297 IEnumMediaTypesImpl_Release
,
298 IEnumMediaTypesImpl_Next
,
299 IEnumMediaTypesImpl_Skip
,
300 IEnumMediaTypesImpl_Reset
,
301 IEnumMediaTypesImpl_Clone