seeking.c 9.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Filter Seeking and Control Interfaces
 *
 * Copyright 2003 Robert Shearman
 * Copyright 2010 Aric Stewart, CodeWeavers
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

22
#include "strmbase_private.h"
23

24
WINE_DEFAULT_DEBUG_CHANNEL(quartz);
25

26 27 28 29 30
static inline SourceSeeking *impl_from_IMediaSeeking(IMediaSeeking *iface)
{
    return CONTAINING_RECORD(iface, SourceSeeking, IMediaSeeking_iface);
}

31 32 33
HRESULT strmbase_seeking_init(SourceSeeking *pSeeking, const IMediaSeekingVtbl *Vtbl,
        SourceSeeking_ChangeStop fnChangeStop, SourceSeeking_ChangeStart fnChangeStart,
        SourceSeeking_ChangeRate fnChangeRate)
34 35 36
{
    assert(fnChangeStop && fnChangeStart && fnChangeRate);

37
    pSeeking->IMediaSeeking_iface.lpVtbl = Vtbl;
38 39 40 41 42 43 44 45 46 47 48 49 50 51
    pSeeking->refCount = 1;
    pSeeking->fnChangeRate = fnChangeRate;
    pSeeking->fnChangeStop = fnChangeStop;
    pSeeking->fnChangeStart = fnChangeStart;
    pSeeking->dwCapabilities = AM_SEEKING_CanSeekForwards |
        AM_SEEKING_CanSeekBackwards |
        AM_SEEKING_CanSeekAbsolute |
        AM_SEEKING_CanGetStopPos |
        AM_SEEKING_CanGetDuration;
    pSeeking->llCurrent = 0;
    pSeeking->llStop = ((ULONGLONG)0x80000000) << 32;
    pSeeking->llDuration = pSeeking->llStop;
    pSeeking->dRate = 1.0;
    pSeeking->timeformat = TIME_FORMAT_MEDIA_TIME;
52 53
    InitializeCriticalSection(&pSeeking->cs);
    pSeeking->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SourceSeeking.cs");
54 55 56
    return S_OK;
}

57 58 59 60 61
void strmbase_seeking_cleanup(SourceSeeking *seeking)
{
    DeleteCriticalSection(&seeking->cs);
}

62 63
HRESULT WINAPI SourceSeekingImpl_GetCapabilities(IMediaSeeking * iface, DWORD * pCapabilities)
{
64
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
65 66 67 68 69 70 71 72 73 74

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

    *pCapabilities = This->dwCapabilities;

    return S_OK;
}

HRESULT WINAPI SourceSeekingImpl_CheckCapabilities(IMediaSeeking * iface, DWORD * pCapabilities)
{
75
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    HRESULT hr;
    DWORD dwCommonCaps;

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

    if (!pCapabilities)
        return E_POINTER;

    dwCommonCaps = *pCapabilities & This->dwCapabilities;

    if (!dwCommonCaps)
        hr = E_FAIL;
    else
        hr = (*pCapabilities == dwCommonCaps) ?  S_OK : S_FALSE;
    *pCapabilities = dwCommonCaps;
    return hr;
}

HRESULT WINAPI SourceSeekingImpl_IsFormatSupported(IMediaSeeking * iface, const GUID * pFormat)
{
    TRACE("(%s)\n", debugstr_guid(pFormat));

    return (IsEqualIID(pFormat, &TIME_FORMAT_MEDIA_TIME) ? S_OK : S_FALSE);
}

HRESULT WINAPI SourceSeekingImpl_QueryPreferredFormat(IMediaSeeking * iface, GUID * pFormat)
{
    TRACE("(%s)\n", debugstr_guid(pFormat));

    *pFormat = TIME_FORMAT_MEDIA_TIME;
    return S_OK;
}

HRESULT WINAPI SourceSeekingImpl_GetTimeFormat(IMediaSeeking * iface, GUID * pFormat)
{
111
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
112 113
    TRACE("(%s)\n", debugstr_guid(pFormat));

114
    EnterCriticalSection(&This->cs);
115
    *pFormat = This->timeformat;
116
    LeaveCriticalSection(&This->cs);
117 118 119 120 121 122

    return S_OK;
}

HRESULT WINAPI SourceSeekingImpl_IsUsingTimeFormat(IMediaSeeking * iface, const GUID * pFormat)
{
123
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
124 125 126 127
    HRESULT hr = S_OK;

    TRACE("(%s)\n", debugstr_guid(pFormat));

128
    EnterCriticalSection(&This->cs);
129 130
    if (!IsEqualIID(pFormat, &This->timeformat))
        hr = S_FALSE;
131
    LeaveCriticalSection(&This->cs);
132 133 134 135 136 137

    return hr;
}

HRESULT WINAPI SourceSeekingImpl_SetTimeFormat(IMediaSeeking * iface, const GUID * pFormat)
{
138
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
139 140 141 142 143 144 145
    TRACE("%p %s\n", This, debugstr_guid(pFormat));
    return (IsEqualIID(pFormat, &TIME_FORMAT_MEDIA_TIME) ? S_OK : E_INVALIDARG);
}


HRESULT WINAPI SourceSeekingImpl_GetDuration(IMediaSeeking * iface, LONGLONG * pDuration)
{
146
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
147 148 149

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

150
    EnterCriticalSection(&This->cs);
151
    *pDuration = This->llDuration;
152
    LeaveCriticalSection(&This->cs);
153 154 155 156 157 158

    return S_OK;
}

HRESULT WINAPI SourceSeekingImpl_GetStopPosition(IMediaSeeking * iface, LONGLONG * pStop)
{
159
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
160 161 162

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

163
    EnterCriticalSection(&This->cs);
164
    *pStop = This->llStop;
165
    LeaveCriticalSection(&This->cs);
166 167 168 169 170 171 172

    return S_OK;
}

/* FIXME: Make use of the info the filter should expose */
HRESULT WINAPI SourceSeekingImpl_GetCurrentPosition(IMediaSeeking * iface, LONGLONG * pCurrent)
{
173
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
174 175 176

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

177
    EnterCriticalSection(&This->cs);
178
    *pCurrent = This->llCurrent;
179
    LeaveCriticalSection(&This->cs);
180 181 182 183 184 185

    return S_OK;
}

HRESULT WINAPI SourceSeekingImpl_ConvertTimeFormat(IMediaSeeking * iface, LONGLONG * pTarget, const GUID * pTargetFormat, LONGLONG Source, const GUID * pSourceFormat)
{
186
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
187 188 189 190
    if (!pTargetFormat)
        pTargetFormat = &This->timeformat;
    if (!pSourceFormat)
        pSourceFormat = &This->timeformat;
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    if (IsEqualIID(pTargetFormat, &TIME_FORMAT_MEDIA_TIME) && IsEqualIID(pSourceFormat, &TIME_FORMAT_MEDIA_TIME))
    {
        *pTarget = Source;
        return S_OK;
    }
    /* FIXME: clear pTarget? */
    return E_INVALIDARG;
}

static inline LONGLONG Adjust(LONGLONG value, const LONGLONG * pModifier, DWORD dwFlags)
{
    switch (dwFlags & AM_SEEKING_PositioningBitsMask)
    {
    case AM_SEEKING_NoPositioning:
        return value;
    case AM_SEEKING_AbsolutePositioning:
        return *pModifier;
    case AM_SEEKING_RelativePositioning:
    case AM_SEEKING_IncrementalPositioning:
        return value + *pModifier;
    default:
        assert(FALSE);
        return 0;
    }
}

HRESULT WINAPI SourceSeekingImpl_SetPositions(IMediaSeeking * iface, LONGLONG * pCurrent, DWORD dwCurrentFlags, LONGLONG * pStop, DWORD dwStopFlags)
{
219
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
220 221 222
    BOOL bChangeCurrent = FALSE, bChangeStop = FALSE;
    LONGLONG llNewCurrent, llNewStop;

223 224 225 226
    TRACE("iface %p, current %s, current_flags %#lx, stop %s, stop_flags %#lx.\n", iface,
            pCurrent ? debugstr_time(*pCurrent) : "<null>", dwCurrentFlags,
            pStop ? debugstr_time(*pStop): "<null>", dwStopFlags);

227
    EnterCriticalSection(&This->cs);
228 229 230 231 232 233 234 235 236

    llNewCurrent = Adjust(This->llCurrent, pCurrent, dwCurrentFlags);
    llNewStop = Adjust(This->llStop, pStop, dwStopFlags);

    if (pCurrent)
        bChangeCurrent = TRUE;
    if (llNewStop != This->llStop)
        bChangeStop = TRUE;

237
    TRACE("Seeking from %s to %s.\n", debugstr_time(This->llCurrent), debugstr_time(llNewCurrent));
238 239 240 241 242 243 244 245

    This->llCurrent = llNewCurrent;
    This->llStop = llNewStop;

    if (pCurrent && (dwCurrentFlags & AM_SEEKING_ReturnTime))
        *pCurrent = llNewCurrent;
    if (pStop && (dwStopFlags & AM_SEEKING_ReturnTime))
        *pStop = llNewStop;
246
    LeaveCriticalSection(&This->cs);
247 248 249 250 251 252 253 254 255 256 257

    if (bChangeCurrent)
        This->fnChangeStart(iface);
    if (bChangeStop)
        This->fnChangeStop(iface);

    return S_OK;
}

HRESULT WINAPI SourceSeekingImpl_GetPositions(IMediaSeeking * iface, LONGLONG * pCurrent, LONGLONG * pStop)
{
258
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
259 260 261

    TRACE("(%p, %p)\n", pCurrent, pStop);

262
    EnterCriticalSection(&This->cs);
263 264
    IMediaSeeking_GetCurrentPosition(iface, pCurrent);
    IMediaSeeking_GetStopPosition(iface, pStop);
265
    LeaveCriticalSection(&This->cs);
266 267 268 269 270 271

    return S_OK;
}

HRESULT WINAPI SourceSeekingImpl_GetAvailable(IMediaSeeking * iface, LONGLONG * pEarliest, LONGLONG * pLatest)
{
272
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
273 274 275

    TRACE("(%p, %p)\n", pEarliest, pLatest);

276
    EnterCriticalSection(&This->cs);
277 278
    *pEarliest = 0;
    *pLatest = This->llDuration;
279
    LeaveCriticalSection(&This->cs);
280 281 282 283 284 285

    return S_OK;
}

HRESULT WINAPI SourceSeekingImpl_SetRate(IMediaSeeking * iface, double dRate)
{
286
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
287 288 289 290 291 292 293 294 295 296 297
    BOOL bChangeRate = (dRate != This->dRate);
    HRESULT hr = S_OK;

    TRACE("(%e)\n", dRate);

    if (dRate > 100 || dRate < .001)
    {
        FIXME("Excessive rate %e, ignoring\n", dRate);
        return VFW_E_UNSUPPORTED_AUDIO;
    }

298
    EnterCriticalSection(&This->cs);
299 300 301
    This->dRate = dRate;
    if (bChangeRate)
        hr = This->fnChangeRate(iface);
302
    LeaveCriticalSection(&This->cs);
303 304 305 306 307 308

    return hr;
}

HRESULT WINAPI SourceSeekingImpl_GetRate(IMediaSeeking * iface, double * dRate)
{
309
    SourceSeeking *This = impl_from_IMediaSeeking(iface);
310 311 312

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

313
    EnterCriticalSection(&This->cs);
314 315
    /* Forward? */
    *dRate = This->dRate;
316
    LeaveCriticalSection(&This->cs);
317 318 319 320 321 322 323 324 325 326 327

    return S_OK;
}

HRESULT WINAPI SourceSeekingImpl_GetPreroll(IMediaSeeking * iface, LONGLONG * pPreroll)
{
    TRACE("(%p)\n", pPreroll);

    *pPreroll = 0;
    return S_OK;
}