2 * Implementation of IEnumMediaTypes Interface
4 * Copyright 2003 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "qcap_main.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(qcap
);
38 HRESULT
CopyMediaType(AM_MEDIA_TYPE
* pDest
, const AM_MEDIA_TYPE
*pSrc
)
40 memcpy(pDest
, pSrc
, sizeof(AM_MEDIA_TYPE
));
41 if (!pSrc
->pbFormat
) return S_OK
;
42 if (!(pDest
->pbFormat
= CoTaskMemAlloc(pSrc
->cbFormat
)))
44 memcpy(pDest
->pbFormat
, pSrc
->pbFormat
, pSrc
->cbFormat
);
48 void FreeMediaType(AM_MEDIA_TYPE
* pMediaType
)
50 if (pMediaType
->pbFormat
)
52 CoTaskMemFree(pMediaType
->pbFormat
);
53 pMediaType
->pbFormat
= NULL
;
57 IUnknown_Release(pMediaType
->pUnk
);
58 pMediaType
->pUnk
= NULL
;
62 void DeleteMediaType(AM_MEDIA_TYPE
* pMediaType
)
64 FreeMediaType(pMediaType
);
65 CoTaskMemFree(pMediaType
);
68 BOOL
CompareMediaTypes(const AM_MEDIA_TYPE
* pmt1
, const AM_MEDIA_TYPE
* pmt2
,
72 dump_AM_MEDIA_TYPE(pmt1
);
74 dump_AM_MEDIA_TYPE(pmt2
);
75 return (((bWildcards
&& (IsEqualGUID(&pmt1
->majortype
, &GUID_NULL
) ||
76 IsEqualGUID(&pmt2
->majortype
, &GUID_NULL
))) ||
77 IsEqualGUID(&pmt1
->majortype
, &pmt2
->majortype
)) &&
78 ((bWildcards
&& (IsEqualGUID(&pmt1
->subtype
, &GUID_NULL
) ||
79 IsEqualGUID(&pmt2
->subtype
, &GUID_NULL
))) ||
80 IsEqualGUID(&pmt1
->subtype
, &pmt2
->subtype
)));
83 void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE
* pmt
)
87 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt
->majortype
),
88 debugstr_guid(&pmt
->subtype
), debugstr_guid(&pmt
->formattype
));
91 typedef struct IEnumMediaTypesImpl
93 const IEnumMediaTypesVtbl
* lpVtbl
;
95 ENUMMEDIADETAILS enumMediaDetails
;
97 } IEnumMediaTypesImpl
;
99 static const struct IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl
;
101 HRESULT
IEnumMediaTypesImpl_Construct(const ENUMMEDIADETAILS
* pDetails
,
102 IEnumMediaTypes
** ppEnum
)
105 IEnumMediaTypesImpl
* pEnumMediaTypes
= CoTaskMemAlloc(sizeof(IEnumMediaTypesImpl
));
107 if (!pEnumMediaTypes
)
110 return E_OUTOFMEMORY
;
112 ObjectRefCount(TRUE
);
113 pEnumMediaTypes
->lpVtbl
= &IEnumMediaTypesImpl_Vtbl
;
114 pEnumMediaTypes
->refCount
= 1;
115 pEnumMediaTypes
->uIndex
= 0;
116 pEnumMediaTypes
->enumMediaDetails
.cMediaTypes
= pDetails
->cMediaTypes
;
117 pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
=
118 CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
) * pDetails
->cMediaTypes
);
119 for (i
= 0; i
< pDetails
->cMediaTypes
; i
++)
120 if (FAILED(CopyMediaType(&pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
[i
], &pDetails
->pMediaTypes
[i
]))) {
121 while (i
--) CoTaskMemFree(pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
[i
].pbFormat
);
122 CoTaskMemFree(pEnumMediaTypes
->enumMediaDetails
.pMediaTypes
);
123 return E_OUTOFMEMORY
;
125 *ppEnum
= (IEnumMediaTypes
*)(&pEnumMediaTypes
->lpVtbl
);
129 static HRESULT WINAPI
IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes
* iface
,
133 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
137 if (IsEqualIID(riid
, &IID_IUnknown
))
138 *ppv
= (LPVOID
)iface
;
139 else if (IsEqualIID(riid
, &IID_IEnumMediaTypes
))
140 *ppv
= (LPVOID
)iface
;
144 IUnknown_AddRef((IUnknown
*)(*ppv
));
148 FIXME("No interface for %s!\n", debugstr_guid(riid
));
150 return E_NOINTERFACE
;
153 static ULONG WINAPI
IEnumMediaTypesImpl_AddRef(IEnumMediaTypes
* iface
)
155 IEnumMediaTypesImpl
*This
= (IEnumMediaTypesImpl
*)iface
;
156 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
163 static ULONG WINAPI
IEnumMediaTypesImpl_Release(IEnumMediaTypes
* iface
)
165 IEnumMediaTypesImpl
*This
= (IEnumMediaTypesImpl
*)iface
;
166 ULONG refCount
= InterlockedDecrement(&This
->refCount
);
173 for (i
= 0; i
< This
->enumMediaDetails
.cMediaTypes
; i
++)
174 if (This
->enumMediaDetails
.pMediaTypes
[i
].pbFormat
)
175 CoTaskMemFree(This
->enumMediaDetails
.pMediaTypes
[i
].pbFormat
);
176 CoTaskMemFree(This
->enumMediaDetails
.pMediaTypes
);
178 ObjectRefCount(FALSE
);
183 static HRESULT WINAPI
IEnumMediaTypesImpl_Next(IEnumMediaTypes
* iface
,
185 AM_MEDIA_TYPE
** ppMediaTypes
,
189 IEnumMediaTypesImpl
*This
= (IEnumMediaTypesImpl
*)iface
;
191 cFetched
= min(This
->enumMediaDetails
.cMediaTypes
,
192 This
->uIndex
+ cMediaTypes
) - This
->uIndex
;
194 TRACE("(%lu, %p, %p)\n", cMediaTypes
, ppMediaTypes
, pcFetched
);
195 TRACE("Next uIndex: %lu, cFetched: %lu\n", This
->uIndex
, cFetched
);
200 *ppMediaTypes
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
) * cFetched
);
201 for (i
= 0; i
< cFetched
; i
++)
202 if (FAILED(CopyMediaType(&(*ppMediaTypes
)[i
], &This
->enumMediaDetails
.pMediaTypes
[This
->uIndex
+ i
]))) {
204 CoTaskMemFree((*ppMediaTypes
)[i
].pbFormat
);
205 CoTaskMemFree(*ppMediaTypes
);
206 *ppMediaTypes
= NULL
;
207 return E_OUTOFMEMORY
;
211 if ((cMediaTypes
!= 1) || pcFetched
)
212 *pcFetched
= cFetched
;
214 This
->uIndex
+= cFetched
;
216 if (cFetched
!= cMediaTypes
)
221 static HRESULT WINAPI
IEnumMediaTypesImpl_Skip(IEnumMediaTypes
* iface
,
224 IEnumMediaTypesImpl
*This
= (IEnumMediaTypesImpl
*)iface
;
226 TRACE("(%lu)\n", cMediaTypes
);
228 if (This
->uIndex
+ cMediaTypes
< This
->enumMediaDetails
.cMediaTypes
)
230 This
->uIndex
+= cMediaTypes
;
236 static HRESULT WINAPI
IEnumMediaTypesImpl_Reset(IEnumMediaTypes
* iface
)
238 IEnumMediaTypesImpl
*This
= (IEnumMediaTypesImpl
*)iface
;
246 static HRESULT WINAPI
IEnumMediaTypesImpl_Clone(IEnumMediaTypes
* iface
,
247 IEnumMediaTypes
** ppEnum
)
250 IEnumMediaTypesImpl
*This
= (IEnumMediaTypesImpl
*)iface
;
252 TRACE("(%p)\n", ppEnum
);
254 hr
= IEnumMediaTypesImpl_Construct(&This
->enumMediaDetails
, ppEnum
);
257 return IEnumMediaTypes_Skip(*ppEnum
, This
->uIndex
);
260 static const IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl
=
262 IEnumMediaTypesImpl_QueryInterface
,
263 IEnumMediaTypesImpl_AddRef
,
264 IEnumMediaTypesImpl_Release
,
265 IEnumMediaTypesImpl_Next
,
266 IEnumMediaTypesImpl_Skip
,
267 IEnumMediaTypesImpl_Reset
,
268 IEnumMediaTypesImpl_Clone