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