67f7c9ff226c801aa97c3a4bd5c61a7c4f123820
[reactos.git] / reactos / lib / 3rdparty / strmbase / filter.c
1 /*
2 * Generic Implementation of IBaseFilter Interface
3 *
4 * Copyright 2010 Aric Stewart, CodeWeavers
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 #define COBJMACROS
22
23 #include "dshow.h"
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
26 #include "wine/strmbase.h"
27 #include "uuids.h"
28 #include <assert.h>
29
30 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
31
32 static inline BaseFilter *impl_from_IBaseFilter(IBaseFilter *iface)
33 {
34 return CONTAINING_RECORD(iface, BaseFilter, IBaseFilter_iface);
35 }
36
37 HRESULT WINAPI BaseFilterImpl_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
38 {
39 BaseFilter *This = impl_from_IBaseFilter(iface);
40 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
41
42 *ppv = NULL;
43
44 if (IsEqualIID(riid, &IID_IUnknown))
45 *ppv = This;
46 else if (IsEqualIID(riid, &IID_IPersist))
47 *ppv = This;
48 else if (IsEqualIID(riid, &IID_IMediaFilter))
49 *ppv = This;
50 else if (IsEqualIID(riid, &IID_IBaseFilter))
51 *ppv = This;
52
53 if (*ppv)
54 {
55 IUnknown_AddRef((IUnknown *)(*ppv));
56 return S_OK;
57 }
58
59 return E_NOINTERFACE;
60 }
61
62 ULONG WINAPI BaseFilterImpl_AddRef(IBaseFilter * iface)
63 {
64 BaseFilter *This = impl_from_IBaseFilter(iface);
65 ULONG refCount = InterlockedIncrement(&This->refCount);
66
67 TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
68
69 return refCount;
70 }
71
72 ULONG WINAPI BaseFilterImpl_Release(IBaseFilter * iface)
73 {
74 BaseFilter *This = impl_from_IBaseFilter(iface);
75 ULONG refCount = InterlockedDecrement(&This->refCount);
76
77 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
78
79 if (!refCount)
80 {
81 if (This->pClock)
82 IReferenceClock_Release(This->pClock);
83
84 This->IBaseFilter_iface.lpVtbl = NULL;
85 This->csFilter.DebugInfo->Spare[0] = 0;
86 DeleteCriticalSection(&This->csFilter);
87 }
88
89 return refCount;
90 }
91
92 HRESULT WINAPI BaseFilterImpl_GetClassID(IBaseFilter * iface, CLSID * pClsid)
93 {
94 BaseFilter *This = impl_from_IBaseFilter(iface);
95 TRACE("(%p)->(%p)\n", This, pClsid);
96
97 *pClsid = This->clsid;
98
99 return S_OK;
100 }
101
102 HRESULT WINAPI BaseFilterImpl_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState )
103 {
104 BaseFilter *This = impl_from_IBaseFilter(iface);
105 TRACE("(%p)->(%d, %p)\n", This, dwMilliSecsTimeout, pState);
106
107 EnterCriticalSection(&This->csFilter);
108 {
109 *pState = This->state;
110 }
111 LeaveCriticalSection(&This->csFilter);
112
113 return S_OK;
114 }
115
116 HRESULT WINAPI BaseFilterImpl_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
117 {
118 BaseFilter *This = impl_from_IBaseFilter(iface);
119 TRACE("(%p)->(%p)\n", This, pClock);
120
121 EnterCriticalSection(&This->csFilter);
122 {
123 if (This->pClock)
124 IReferenceClock_Release(This->pClock);
125 This->pClock = pClock;
126 if (This->pClock)
127 IReferenceClock_AddRef(This->pClock);
128 }
129 LeaveCriticalSection(&This->csFilter);
130
131 return S_OK;
132 }
133
134 HRESULT WINAPI BaseFilterImpl_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
135 {
136 BaseFilter *This = impl_from_IBaseFilter(iface);
137 TRACE("(%p)->(%p)\n", This, ppClock);
138
139 EnterCriticalSection(&This->csFilter);
140 {
141 *ppClock = This->pClock;
142 if (This->pClock)
143 IReferenceClock_AddRef(This->pClock);
144 }
145 LeaveCriticalSection(&This->csFilter);
146
147 return S_OK;
148 }
149
150 HRESULT WINAPI BaseFilterImpl_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
151 {
152 BaseFilter *This = impl_from_IBaseFilter(iface);
153
154 TRACE("(%p)->(%p)\n", iface, ppEnum);
155
156 return EnumPins_Construct(This, This->pFuncsTable->pfnGetPin, This->pFuncsTable->pfnGetPinCount, BaseFilterImpl_GetPinVersion, ppEnum);
157 }
158
159
160 HRESULT WINAPI BaseFilterImpl_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
161 {
162 BaseFilter *This = impl_from_IBaseFilter(iface);
163 TRACE("(%p)->(%p)\n", This, pInfo);
164
165 strcpyW(pInfo->achName, This->filterInfo.achName);
166 pInfo->pGraph = This->filterInfo.pGraph;
167
168 if (pInfo->pGraph)
169 IFilterGraph_AddRef(pInfo->pGraph);
170
171 return S_OK;
172 }
173
174 HRESULT WINAPI BaseFilterImpl_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName )
175 {
176 HRESULT hr = S_OK;
177 BaseFilter *This = impl_from_IBaseFilter(iface);
178
179 TRACE("(%p)->(%p, %s)\n", This, pGraph, debugstr_w(pName));
180
181 EnterCriticalSection(&This->csFilter);
182 {
183 if (pName)
184 strcpyW(This->filterInfo.achName, pName);
185 else
186 *This->filterInfo.achName = '\0';
187 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
188 }
189 LeaveCriticalSection(&This->csFilter);
190
191 return hr;
192 }
193
194 HRESULT WINAPI BaseFilterImpl_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
195 {
196 TRACE("(%p)->(%p)\n", iface, pVendorInfo);
197 return E_NOTIMPL;
198 }
199
200 LONG WINAPI BaseFilterImpl_GetPinVersion(BaseFilter * This)
201 {
202 TRACE("(%p)\n", This);
203 return This->pinVersion;
204 }
205
206 VOID WINAPI BaseFilterImpl_IncrementPinVersion(BaseFilter * This)
207 {
208 InterlockedIncrement(&This->pinVersion);
209 TRACE("(%p) -> New pinVersion %i\n", This,This->pinVersion);
210 }
211
212 HRESULT WINAPI BaseFilter_Init(BaseFilter * This, const IBaseFilterVtbl *Vtbl, const CLSID *pClsid, DWORD_PTR DebugInfo, const BaseFilterFuncTable* pBaseFuncsTable)
213 {
214 This->IBaseFilter_iface.lpVtbl = Vtbl;
215 This->refCount = 1;
216 InitializeCriticalSection(&This->csFilter);
217 This->state = State_Stopped;
218 This->rtStreamStart = 0;
219 This->pClock = NULL;
220 ZeroMemory(&This->filterInfo, sizeof(FILTER_INFO));
221 This->clsid = *pClsid;
222 This->csFilter.DebugInfo->Spare[0] = DebugInfo;
223 This->pinVersion = 1;
224
225 This->pFuncsTable = pBaseFuncsTable;
226
227 return S_OK;
228 }