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