[STRMBASE]
[reactos.git] / reactos / lib / 3rdparty / strmbase / enumpins.c
1 /*
2 * Implementation of IEnumPins Interface
3 *
4 * Copyright 2003 Robert Shearman
5 * Copyright 2010 Aric Stewart, CodeWeavers
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "strmbase_private.h"
23
24 typedef struct IEnumPinsImpl
25 {
26 IEnumPins IEnumPins_iface;
27 LONG refCount;
28 ULONG uIndex;
29 BaseFilter *base;
30 BaseFilter_GetPin receive_pin;
31 BaseFilter_GetPinCount receive_pincount;
32 BaseFilter_GetPinVersion receive_version;
33 DWORD Version;
34 } IEnumPinsImpl;
35
36 static inline IEnumPinsImpl *impl_from_IEnumPins(IEnumPins *iface)
37 {
38 return CONTAINING_RECORD(iface, IEnumPinsImpl, IEnumPins_iface);
39 }
40
41 static const struct IEnumPinsVtbl IEnumPinsImpl_Vtbl;
42
43 HRESULT WINAPI EnumPins_Construct(BaseFilter *base, BaseFilter_GetPin receive_pin, BaseFilter_GetPinCount receive_pincount, BaseFilter_GetPinVersion receive_version, IEnumPins ** ppEnum)
44 {
45 IEnumPinsImpl * pEnumPins;
46
47 if (!ppEnum)
48 return E_POINTER;
49
50 pEnumPins = CoTaskMemAlloc(sizeof(IEnumPinsImpl));
51 if (!pEnumPins)
52 {
53 *ppEnum = NULL;
54 return E_OUTOFMEMORY;
55 }
56 pEnumPins->IEnumPins_iface.lpVtbl = &IEnumPinsImpl_Vtbl;
57 pEnumPins->refCount = 1;
58 pEnumPins->uIndex = 0;
59 pEnumPins->receive_pin = receive_pin;
60 pEnumPins->receive_pincount = receive_pincount;
61 pEnumPins->receive_version = receive_version;
62 pEnumPins->base = base;
63 IBaseFilter_AddRef(&base->IBaseFilter_iface);
64 *ppEnum = &pEnumPins->IEnumPins_iface;
65 pEnumPins->Version = receive_version(base);
66
67 TRACE("Created new enumerator (%p)\n", *ppEnum);
68 return S_OK;
69 }
70
71 static HRESULT WINAPI IEnumPinsImpl_QueryInterface(IEnumPins * iface, REFIID riid, LPVOID * ppv)
72 {
73 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
74
75 *ppv = NULL;
76
77 if (IsEqualIID(riid, &IID_IUnknown))
78 *ppv = iface;
79 else if (IsEqualIID(riid, &IID_IEnumPins))
80 *ppv = iface;
81
82 if (*ppv)
83 {
84 IUnknown_AddRef((IUnknown *)(*ppv));
85 return S_OK;
86 }
87
88 FIXME("No interface for %s!\n", debugstr_guid(riid));
89
90 return E_NOINTERFACE;
91 }
92
93 static ULONG WINAPI IEnumPinsImpl_AddRef(IEnumPins * iface)
94 {
95 IEnumPinsImpl *This = impl_from_IEnumPins(iface);
96 ULONG ref = InterlockedIncrement(&This->refCount);
97
98 TRACE("(%p)->(): new ref = %u\n", iface, ref);
99
100 return ref;
101 }
102
103 static ULONG WINAPI IEnumPinsImpl_Release(IEnumPins * iface)
104 {
105 IEnumPinsImpl *This = impl_from_IEnumPins(iface);
106 ULONG ref = InterlockedDecrement(&This->refCount);
107
108 TRACE("(%p)->(): new ref = %u\n", iface, ref);
109
110 if (!ref)
111 {
112 IBaseFilter_Release(&This->base->IBaseFilter_iface);
113 CoTaskMemFree(This);
114 }
115
116 return ref;
117 }
118
119 static HRESULT WINAPI IEnumPinsImpl_Next(IEnumPins * iface, ULONG cPins, IPin ** ppPins, ULONG * pcFetched)
120 {
121 IEnumPinsImpl *This = impl_from_IEnumPins(iface);
122 HRESULT hr = S_OK;
123 ULONG i = 0;
124
125 TRACE("(%p)->(%u, %p, %p)\n", iface, cPins, ppPins, pcFetched);
126
127 if (!ppPins)
128 return E_POINTER;
129
130 if (cPins > 1 && !pcFetched)
131 return E_INVALIDARG;
132
133 if (pcFetched)
134 *pcFetched = 0;
135
136 if (This->Version != This->receive_version(This->base))
137 return VFW_E_ENUM_OUT_OF_SYNC;
138
139 while (i < cPins && hr == S_OK)
140 {
141 IPin *pin;
142 pin = This->receive_pin(This->base, This->uIndex + i);
143
144 if (!pin)
145 break;
146 else
147 ppPins[i] = pin;
148 ++i;
149 }
150
151 if (pcFetched)
152 *pcFetched = i;
153 This->uIndex += i;
154
155 if (i < cPins)
156 return S_FALSE;
157 return S_OK;
158 }
159
160 static HRESULT WINAPI IEnumPinsImpl_Skip(IEnumPins * iface, ULONG cPins)
161 {
162 IEnumPinsImpl *This = impl_from_IEnumPins(iface);
163
164 TRACE("(%p)->(%u)\n", iface, cPins);
165
166 if (This->Version != This->receive_version(This->base))
167 return VFW_E_ENUM_OUT_OF_SYNC;
168
169 if (This->receive_pincount(This->base) >= This->uIndex + cPins)
170 return S_FALSE;
171
172 This->uIndex += cPins;
173 return S_OK;
174 }
175
176 static HRESULT WINAPI IEnumPinsImpl_Reset(IEnumPins * iface)
177 {
178 IEnumPinsImpl *This = impl_from_IEnumPins(iface);
179
180 TRACE("(%p)->()\n", iface);
181
182 This->Version = This->receive_version(This->base);
183
184 This->uIndex = 0;
185 return S_OK;
186 }
187
188 static HRESULT WINAPI IEnumPinsImpl_Clone(IEnumPins * iface, IEnumPins ** ppEnum)
189 {
190 HRESULT hr;
191 IEnumPinsImpl *This = impl_from_IEnumPins(iface);
192
193 TRACE("(%p)->(%p)\n", iface, ppEnum);
194
195 hr = EnumPins_Construct(This->base, This->receive_pin, This->receive_pincount, This->receive_version, ppEnum);
196 if (FAILED(hr))
197 return hr;
198 return IEnumPins_Skip(*ppEnum, This->uIndex);
199 }
200
201 static const IEnumPinsVtbl IEnumPinsImpl_Vtbl =
202 {
203 IEnumPinsImpl_QueryInterface,
204 IEnumPinsImpl_AddRef,
205 IEnumPinsImpl_Release,
206 IEnumPinsImpl_Next,
207 IEnumPinsImpl_Skip,
208 IEnumPinsImpl_Reset,
209 IEnumPinsImpl_Clone
210 };