enumpins.c 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Implementation of IEnumPins Interface
 *
 * Copyright 2003 Robert Shearman
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24 25 26 27 28 29
 */

#include "quartz_private.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(quartz);

typedef struct IEnumPinsImpl
{
    const IEnumPinsVtbl * lpVtbl;
30
    LONG refCount;
31
    ULONG uIndex;
32 33 34
    IBaseFilter *base;
    FNOBTAINPIN receive_pin;
    DWORD synctime;
35 36 37 38
} IEnumPinsImpl;

static const struct IEnumPinsVtbl IEnumPinsImpl_Vtbl;

39
HRESULT IEnumPinsImpl_Construct(IEnumPins ** ppEnum, FNOBTAINPIN receive_pin, IBaseFilter *base)
40
{
41 42 43 44 45 46
    IEnumPinsImpl * pEnumPins;

    if (!ppEnum)
        return E_POINTER;

    pEnumPins = CoTaskMemAlloc(sizeof(IEnumPinsImpl));
47 48 49 50 51 52 53 54
    if (!pEnumPins)
    {
        *ppEnum = NULL;
        return E_OUTOFMEMORY;
    }
    pEnumPins->lpVtbl = &IEnumPinsImpl_Vtbl;
    pEnumPins->refCount = 1;
    pEnumPins->uIndex = 0;
55 56 57
    pEnumPins->receive_pin = receive_pin;
    pEnumPins->base = base;
    IBaseFilter_AddRef(base);
58
    *ppEnum = (IEnumPins *)(&pEnumPins->lpVtbl);
59

60 61
    receive_pin(base, ~0, NULL, &pEnumPins->synctime);

62
    TRACE("Created new enumerator (%p)\n", *ppEnum);
63 64 65 66 67 68 69 70 71 72
    return S_OK;
}

static HRESULT WINAPI IEnumPinsImpl_QueryInterface(IEnumPins * iface, REFIID riid, LPVOID * ppv)
{
    TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);

    *ppv = NULL;

    if (IsEqualIID(riid, &IID_IUnknown))
73
        *ppv = iface;
74
    else if (IsEqualIID(riid, &IID_IEnumPins))
75
        *ppv = iface;
76 77 78 79 80 81 82 83 84 85 86 87 88 89

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

    FIXME("No interface for %s!\n", qzdebugstr_guid(riid));

    return E_NOINTERFACE;
}

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

static ULONG WINAPI IEnumPinsImpl_Release(IEnumPins * iface)
{
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 109 110 111
        CoTaskMemFree(This);
        return 0;
    }
    else
112
        return refCount;
113 114 115 116
}

static HRESULT WINAPI IEnumPinsImpl_Next(IEnumPins * iface, ULONG cPins, IPin ** ppPins, ULONG * pcFetched)
{
117
    IEnumPinsImpl *This = (IEnumPinsImpl *)iface;
118 119 120
    DWORD synctime = This->synctime;
    HRESULT hr = S_OK;
    ULONG i = 0;
121

122
    TRACE("(%u, %p, %p)\n", cPins, ppPins, pcFetched);
123

124 125 126 127 128 129
    if (!ppPins)
        return E_POINTER;

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

130 131 132 133
    if (pcFetched)
        *pcFetched = 0;

    while (i < cPins && hr == S_OK)
134
    {
135 136 137 138 139 140 141
        hr = This->receive_pin(This->base, This->uIndex + i, &ppPins[i], &synctime);

        if (hr == S_OK)
            ++i;

        if (synctime != This->synctime)
            break;
142 143
    }

144 145
    if (!i && synctime != This->synctime)
        return VFW_E_ENUM_OUT_OF_SYNC;
146

147 148 149
    if (pcFetched)
        *pcFetched = i;
    This->uIndex += i;
150

151
    if (i < cPins)
152 153 154 155 156 157
        return S_FALSE;
    return S_OK;
}

static HRESULT WINAPI IEnumPinsImpl_Skip(IEnumPins * iface, ULONG cPins)
{
158
    IEnumPinsImpl *This = (IEnumPinsImpl *)iface;
159 160 161
    DWORD synctime = This->synctime;
    HRESULT hr;
    IPin *pin = NULL;
162

163
    TRACE("(%u)\n", cPins);
164

165 166 167 168 169 170 171 172
    hr = This->receive_pin(This->base, This->uIndex + cPins, &pin, &synctime);
    if (pin)
        IPin_Release(pin);

    if (synctime != This->synctime)
        return VFW_E_ENUM_OUT_OF_SYNC;

    if (hr == S_OK)
173
        This->uIndex += cPins;
174 175

    return hr;
176 177 178 179
}

static HRESULT WINAPI IEnumPinsImpl_Reset(IEnumPins * iface)
{
180
    IEnumPinsImpl *This = (IEnumPinsImpl *)iface;
181 182

    TRACE("IEnumPinsImpl::Reset()\n");
183
    This->receive_pin(This->base, ~0, NULL, &This->synctime);
184 185 186 187 188 189 190 191

    This->uIndex = 0;
    return S_OK;
}

static HRESULT WINAPI IEnumPinsImpl_Clone(IEnumPins * iface, IEnumPins ** ppEnum)
{
    HRESULT hr;
192
    IEnumPinsImpl *This = (IEnumPinsImpl *)iface;
193 194 195

    TRACE("(%p)\n", ppEnum);

196
    hr = IEnumPinsImpl_Construct(ppEnum, This->receive_pin, This->base);
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    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
};