enumpins.c 5.49 KB
Newer Older
1 2 3 4
/*
 * Implementation of IEnumPins Interface
 *
 * Copyright 2003 Robert Shearman
5
 * Copyright 2010 Aric Stewart, CodeWeavers
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 25
#include "dshow.h"
#include "wine/strmbase.h"
26 27
#include "wine/debug.h"

28
WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
29 30 31

typedef struct IEnumPinsImpl
{
32
    IEnumPins IEnumPins_iface;
33
    LONG refCount;
34
    ULONG uIndex;
35
    BaseFilter *base;
36 37 38 39
    BaseFilter_GetPin receive_pin;
    BaseFilter_GetPinCount receive_pincount;
    BaseFilter_GetPinVersion receive_version;
    DWORD Version;
40 41
} IEnumPinsImpl;

42 43 44 45 46
static inline IEnumPinsImpl *impl_from_IEnumPins(IEnumPins *iface)
{
    return CONTAINING_RECORD(iface, IEnumPinsImpl, IEnumPins_iface);
}

47 48
static const struct IEnumPinsVtbl IEnumPinsImpl_Vtbl;

49
HRESULT WINAPI EnumPins_Construct(BaseFilter *base,  BaseFilter_GetPin receive_pin, BaseFilter_GetPinCount receive_pincount, BaseFilter_GetPinVersion receive_version, IEnumPins ** ppEnum)
50
{
51 52 53 54 55 56
    IEnumPinsImpl * pEnumPins;

    if (!ppEnum)
        return E_POINTER;

    pEnumPins = CoTaskMemAlloc(sizeof(IEnumPinsImpl));
57 58 59 60 61
    if (!pEnumPins)
    {
        *ppEnum = NULL;
        return E_OUTOFMEMORY;
    }
62
    pEnumPins->IEnumPins_iface.lpVtbl = &IEnumPinsImpl_Vtbl;
63 64
    pEnumPins->refCount = 1;
    pEnumPins->uIndex = 0;
65
    pEnumPins->receive_pin = receive_pin;
66 67
    pEnumPins->receive_pincount = receive_pincount;
    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 75 76 77 78
    return S_OK;
}

static HRESULT WINAPI IEnumPinsImpl_QueryInterface(IEnumPins * iface, REFIID riid, LPVOID * ppv)
{
79
    TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
80 81 82 83

    *ppv = NULL;

    if (IsEqualIID(riid, &IID_IUnknown))
84
        *ppv = iface;
85
    else if (IsEqualIID(riid, &IID_IEnumPins))
86
        *ppv = iface;
87 88 89 90 91 92 93

    if (*ppv)
    {
        IUnknown_AddRef((IUnknown *)(*ppv));
        return S_OK;
    }

94
    FIXME("No interface for %s!\n", debugstr_guid(riid));
95 96 97 98 99 100

    return E_NOINTERFACE;
}

static ULONG WINAPI IEnumPinsImpl_AddRef(IEnumPins * iface)
{
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 110
}

static ULONG WINAPI IEnumPinsImpl_Release(IEnumPins * iface)
{
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 120
        CoTaskMemFree(This);
    }
121 122

    return ref;
123 124 125 126
}

static HRESULT WINAPI IEnumPinsImpl_Next(IEnumPins * iface, ULONG cPins, IPin ** ppPins, ULONG * pcFetched)
{
127
    IEnumPinsImpl *This = impl_from_IEnumPins(iface);
128
    ULONG i = 0;
129

130
    TRACE("(%p)->(%u, %p, %p)\n", iface, cPins, ppPins, pcFetched);
131

132 133 134 135 136 137
    if (!ppPins)
        return E_POINTER;

    if (cPins > 1 && !pcFetched)
        return E_INVALIDARG;

138 139 140
    if (pcFetched)
        *pcFetched = 0;

141 142 143
    if (This->Version != This->receive_version(This->base))
        return VFW_E_ENUM_OUT_OF_SYNC;

144
    while (i < cPins)
145
    {
146 147 148 149 150 151 152 153
       IPin *pin;
       pin = This->receive_pin(This->base, This->uIndex + i);

       if (!pin)
         break;
       else
         ppPins[i] = pin;
       ++i;
154 155
    }

156 157 158
    if (pcFetched)
        *pcFetched = i;
    This->uIndex += i;
159

160
    if (i < cPins)
161 162 163 164 165 166
        return S_FALSE;
    return S_OK;
}

static HRESULT WINAPI IEnumPinsImpl_Skip(IEnumPins * iface, ULONG cPins)
{
167
    IEnumPinsImpl *This = impl_from_IEnumPins(iface);
168

169
    TRACE("(%p)->(%u)\n", iface, cPins);
170

171
    if (This->Version != This->receive_version(This->base))
172 173
        return VFW_E_ENUM_OUT_OF_SYNC;

174 175
    if (This->receive_pincount(This->base) >= This->uIndex + cPins)
        return S_FALSE;
176

177 178
    This->uIndex += cPins;
    return S_OK;
179 180 181 182
}

static HRESULT WINAPI IEnumPinsImpl_Reset(IEnumPins * iface)
{
183
    IEnumPinsImpl *This = impl_from_IEnumPins(iface);
184

185 186
    TRACE("(%p)->()\n", iface);

187
    This->Version = This->receive_version(This->base);
188 189 190 191 192 193 194 195

    This->uIndex = 0;
    return S_OK;
}

static HRESULT WINAPI IEnumPinsImpl_Clone(IEnumPins * iface, IEnumPins ** ppEnum)
{
    HRESULT hr;
196
    IEnumPinsImpl *This = impl_from_IEnumPins(iface);
197

198
    TRACE("(%p)->(%p)\n", iface, ppEnum);
199

200
    hr = EnumPins_Construct(This->base, This->receive_pin, This->receive_pincount, This->receive_version, ppEnum);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    if (FAILED(hr))
        return hr;
    return IEnumPins_Skip(*ppEnum, This->uIndex);
}

static const IEnumPinsVtbl IEnumPinsImpl_Vtbl =
{
    IEnumPinsImpl_QueryInterface,
    IEnumPinsImpl_AddRef,
    IEnumPinsImpl_Release,
    IEnumPinsImpl_Next,
    IEnumPinsImpl_Skip,
    IEnumPinsImpl_Reset,
    IEnumPinsImpl_Clone
};