filter.c 28.8 KB
Newer Older
1 2 3
/*
 * Implementation of MediaStream Filter
 *
4
 * Copyright 2008, 2012 Christian Costa
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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
 */

#define COBJMACROS
#include "amstream_private.h"
23
#include "wine/debug.h"
24
#include "wine/list.h"
25

26
WINE_DEFAULT_DEBUG_CHANNEL(quartz);
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
struct enum_pins
{
    IEnumPins IEnumPins_iface;
    LONG refcount;

    IPin **pins;
    unsigned int count, index;
};

static const IEnumPinsVtbl enum_pins_vtbl;

static struct enum_pins *impl_from_IEnumPins(IEnumPins *iface)
{
    return CONTAINING_RECORD(iface, struct enum_pins, IEnumPins_iface);
}

static HRESULT WINAPI enum_pins_QueryInterface(IEnumPins *iface, REFIID iid, void **out)
{
    TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);

    if (IsEqualGUID(iid, &IID_IUnknown) || IsEqualGUID(iid, &IID_IEnumPins))
    {
        IEnumPins_AddRef(iface);
        *out = iface;
        return S_OK;
    }

    WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
    *out = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI enum_pins_AddRef(IEnumPins *iface)
{
    struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
    ULONG refcount = InterlockedIncrement(&enum_pins->refcount);
64
    TRACE("%p increasing refcount to %lu.\n", enum_pins, refcount);
65 66 67 68 69 70 71 72 73
    return refcount;
}

static ULONG WINAPI enum_pins_Release(IEnumPins *iface)
{
    struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
    ULONG refcount = InterlockedDecrement(&enum_pins->refcount);
    unsigned int i;

74
    TRACE("%p decreasing refcount to %lu.\n", enum_pins, refcount);
75 76 77 78
    if (!refcount)
    {
        for (i = 0; i < enum_pins->count; ++i)
            IPin_Release(enum_pins->pins[i]);
79 80
        free(enum_pins->pins);
        free(enum_pins);
81 82 83 84 85 86 87 88 89
    }
    return refcount;
}

static HRESULT WINAPI enum_pins_Next(IEnumPins *iface, ULONG count, IPin **pins, ULONG *ret_count)
{
    struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
    unsigned int i;

90
    TRACE("iface %p, count %lu, pins %p, ret_count %p.\n", iface, count, pins, ret_count);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

    if (!pins || (count > 1 && !ret_count))
        return E_POINTER;

    for (i = 0; i < count && enum_pins->index < enum_pins->count; ++i)
    {
        IPin_AddRef(pins[i] = enum_pins->pins[i]);
        enum_pins->index++;
    }

    if (ret_count) *ret_count = i;
    return i == count ? S_OK : S_FALSE;
}

static HRESULT WINAPI enum_pins_Skip(IEnumPins *iface, ULONG count)
{
    struct enum_pins *enum_pins = impl_from_IEnumPins(iface);

109
    TRACE("iface %p, count %lu.\n", iface, count);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

    enum_pins->index += count;

    return enum_pins->index >= enum_pins->count ? S_FALSE : S_OK;
}

static HRESULT WINAPI enum_pins_Reset(IEnumPins *iface)
{
    struct enum_pins *enum_pins = impl_from_IEnumPins(iface);

    TRACE("iface %p.\n", iface);

    enum_pins->index = 0;
    return S_OK;
}

static HRESULT WINAPI enum_pins_Clone(IEnumPins *iface, IEnumPins **out)
{
    struct enum_pins *enum_pins = impl_from_IEnumPins(iface);
    struct enum_pins *object;
    unsigned int i;

    TRACE("iface %p, out %p.\n", iface, out);

134
    if (!(object = calloc(1, sizeof(*object))))
135 136 137 138 139 140
        return E_OUTOFMEMORY;

    object->IEnumPins_iface.lpVtbl = &enum_pins_vtbl;
    object->refcount = 1;
    object->count = enum_pins->count;
    object->index = enum_pins->index;
141
    if (!(object->pins = malloc(enum_pins->count * sizeof(*object->pins))))
142
    {
143
        free(object);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
        return E_OUTOFMEMORY;
    }
    for (i = 0; i < enum_pins->count; ++i)
        IPin_AddRef(object->pins[i] = enum_pins->pins[i]);

    *out = &object->IEnumPins_iface;
    return S_OK;
}

static const IEnumPinsVtbl enum_pins_vtbl =
{
    enum_pins_QueryInterface,
    enum_pins_AddRef,
    enum_pins_Release,
    enum_pins_Next,
    enum_pins_Skip,
    enum_pins_Reset,
    enum_pins_Clone,
};

164 165
struct filter
{
166
    IMediaStreamFilter IMediaStreamFilter_iface;
167
    IMediaSeeking IMediaSeeking_iface;
168 169 170 171 172 173
    LONG refcount;
    CRITICAL_SECTION cs;

    IReferenceClock *clock;
    WCHAR name[128];
    IFilterGraph *graph;
174
    ULONG nb_streams;
175
    IAMMediaStream **streams;
176
    IAMMediaStream *seekable_stream;
177
    FILTER_STATE state;
178
    REFERENCE_TIME start_time;
179 180
    struct list free_events;
    struct list used_events;
181
    LONG eos_count;
182 183 184 185 186 187 188 189
};

struct event
{
    struct list entry;
    HANDLE event;
    DWORD_PTR cookie;
    BOOL interrupted;
190
};
191

192
static inline struct filter *impl_from_IMediaStreamFilter(IMediaStreamFilter *iface)
193
{
194
    return CONTAINING_RECORD(iface, struct filter, IMediaStreamFilter_iface);
195 196
}

197
static HRESULT WINAPI filter_QueryInterface(IMediaStreamFilter *iface, REFIID iid, void **out)
198
{
199
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
200

201
    TRACE("filter %p, iid %s, out %p.\n", filter, debugstr_guid(iid), out);
202

203
    *out = NULL;
204

205 206 207 208 209 210 211 212 213 214
    if (IsEqualGUID(iid, &IID_IUnknown)
            || IsEqualGUID(iid, &IID_IPersist)
            || IsEqualGUID(iid, &IID_IMediaFilter)
            || IsEqualGUID(iid, &IID_IBaseFilter)
            || IsEqualGUID(iid, &IID_IMediaStreamFilter))
        *out = iface;
    else if (IsEqualGUID(iid, &IID_IMediaSeeking) && filter->seekable_stream)
        *out = &filter->IMediaSeeking_iface;
    else
        return E_NOINTERFACE;
215

216 217
    IUnknown_AddRef((IUnknown *)*out);
    return S_OK;
218 219
}

220
static ULONG WINAPI filter_AddRef(IMediaStreamFilter *iface)
221
{
222
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
223
    ULONG refcount = InterlockedIncrement(&filter->refcount);
224

225
    TRACE("%p increasing refcount to %lu.\n", iface, refcount);
226

227
    return refcount;
228 229
}

230
static ULONG WINAPI filter_Release(IMediaStreamFilter *iface)
231
{
232
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
233 234
    ULONG refcount = InterlockedDecrement(&filter->refcount);
    unsigned int i;
235

236
    TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
237

238
    if (!refcount)
239
    {
240 241 242 243 244 245 246 247 248
        struct list *entry;

        while ((entry = list_head(&filter->free_events)))
        {
            struct event *event = LIST_ENTRY(entry, struct event, entry);
            list_remove(entry);
            CloseHandle(event->event);
            free(event);
        }
249
        for (i = 0; i < filter->nb_streams; ++i)
250
        {
251 252
            IAMMediaStream_JoinFilter(filter->streams[i], NULL);
            IAMMediaStream_Release(filter->streams[i]);
253
        }
254
        free(filter->streams);
255 256 257
        if (filter->clock)
            IReferenceClock_Release(filter->clock);
        DeleteCriticalSection(&filter->cs);
258
        free(filter);
259
    }
260

261
    return refcount;
262 263
}

264
static HRESULT WINAPI filter_GetClassID(IMediaStreamFilter *iface, CLSID *clsid)
265
{
266 267
    *clsid = CLSID_MediaStreamFilter;
    return S_OK;
268 269
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
static void send_ec_complete(struct filter *filter)
{
    IMediaEventSink *event_sink;

    if (!filter->graph)
        return;

    if (FAILED(IFilterGraph_QueryInterface(filter->graph, &IID_IMediaEventSink, (void **)&event_sink)))
        return;

    IMediaEventSink_Notify(event_sink, EC_COMPLETE, S_OK,
            (LONG_PTR)&filter->IMediaStreamFilter_iface);

    IMediaEventSink_Release(event_sink);
}

286 287 288 289 290 291 292 293 294 295 296 297
static void set_state(struct filter *filter, FILTER_STATE state)
{
    if (filter->state != state)
    {
        ULONG i;

        for (i = 0; i < filter->nb_streams; ++i)
            IAMMediaStream_SetState(filter->streams[i], state);
        filter->state = state;
    }
}

298
static HRESULT WINAPI filter_Stop(IMediaStreamFilter *iface)
299
{
300
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
301
    struct event *event;
302

303 304 305 306
    TRACE("iface %p.\n", iface);

    EnterCriticalSection(&filter->cs);

307 308 309
    if (filter->state != State_Stopped)
        filter->eos_count = 0;

310 311
    set_state(filter, State_Stopped);

312 313 314 315 316 317 318 319 320 321
    LIST_FOR_EACH_ENTRY(event, &filter->used_events, struct event, entry)
    {
        if (!event->interrupted)
        {
            event->interrupted = TRUE;
            IReferenceClock_Unadvise(filter->clock, event->cookie);
            SetEvent(event->event);
        }
    }

322 323 324
    LeaveCriticalSection(&filter->cs);

    return S_OK;
325 326
}

327
static HRESULT WINAPI filter_Pause(IMediaStreamFilter *iface)
328
{
329
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
330

331 332 333 334 335 336 337 338 339
    TRACE("iface %p.\n", iface);

    EnterCriticalSection(&filter->cs);

    set_state(filter, State_Paused);

    LeaveCriticalSection(&filter->cs);

    return S_OK;
340 341
}

342
static HRESULT WINAPI filter_Run(IMediaStreamFilter *iface, REFERENCE_TIME start)
343
{
344 345 346 347 348
    struct filter *filter = impl_from_IMediaStreamFilter(iface);

    TRACE("iface %p, start %s.\n", iface, wine_dbgstr_longlong(start));

    EnterCriticalSection(&filter->cs);
349

350 351 352 353
    if (filter->state != State_Running && filter->seekable_stream
            && filter->eos_count == (LONG)filter->nb_streams)
        send_ec_complete(filter);

354
    filter->start_time = start;
355 356 357 358 359
    set_state(filter, State_Running);

    LeaveCriticalSection(&filter->cs);

    return S_OK;
360 361
}

362
static HRESULT WINAPI filter_GetState(IMediaStreamFilter *iface, DWORD timeout, FILTER_STATE *state)
363
{
364 365
    struct filter *filter = impl_from_IMediaStreamFilter(iface);

366
    TRACE("iface %p, timeout %lu, state %p.\n", iface, timeout, state);
367 368 369 370 371 372 373 374 375

    if (!state)
        return E_POINTER;

    EnterCriticalSection(&filter->cs);

    *state = filter->state;

    LeaveCriticalSection(&filter->cs);
376 377

    return S_OK;
378 379
}

380
static HRESULT WINAPI filter_SetSyncSource(IMediaStreamFilter *iface, IReferenceClock *clock)
381
{
382
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
383 384 385 386 387 388 389 390 391 392 393 394 395 396

    TRACE("iface %p, clock %p.\n", iface, clock);

    EnterCriticalSection(&filter->cs);

    if (clock)
        IReferenceClock_AddRef(clock);
    if (filter->clock)
        IReferenceClock_Release(filter->clock);
    filter->clock = clock;

    LeaveCriticalSection(&filter->cs);

    return S_OK;
397 398
}

399
static HRESULT WINAPI filter_GetSyncSource(IMediaStreamFilter *iface, IReferenceClock **clock)
400
{
401
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
402 403 404 405 406 407 408 409 410 411 412 413

    TRACE("iface %p, clock %p.\n", iface, clock);

    EnterCriticalSection(&filter->cs);

    if (filter->clock)
        IReferenceClock_AddRef(filter->clock);
    *clock = filter->clock;

    LeaveCriticalSection(&filter->cs);

    return S_OK;
414 415
}

416
static HRESULT WINAPI filter_EnumPins(IMediaStreamFilter *iface, IEnumPins **enum_pins)
417
{
418
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
419 420 421 422 423 424 425 426
    struct enum_pins *object;
    unsigned int i;

    TRACE("iface %p, enum_pins %p.\n", iface, enum_pins);

    if (!enum_pins)
        return E_POINTER;

427
    if (!(object = calloc(1, sizeof(*object))))
428 429
        return E_OUTOFMEMORY;

430 431
    EnterCriticalSection(&filter->cs);

432 433 434 435
    object->IEnumPins_iface.lpVtbl = &enum_pins_vtbl;
    object->refcount = 1;
    object->count = filter->nb_streams;
    object->index = 0;
436
    if (!(object->pins = malloc(filter->nb_streams * sizeof(*object->pins))))
437
    {
438
        free(object);
439
        LeaveCriticalSection(&filter->cs);
440 441 442 443 444 445 446 447
        return E_OUTOFMEMORY;
    }
    for (i = 0; i < filter->nb_streams; ++i)
    {
        if (FAILED(IAMMediaStream_QueryInterface(filter->streams[i], &IID_IPin, (void **)&object->pins[i])))
            WARN("Stream %p does not support IPin.\n", filter->streams[i]);
    }

448 449
    LeaveCriticalSection(&filter->cs);

450 451
    *enum_pins = &object->IEnumPins_iface;
    return S_OK;
452 453
}

454
static HRESULT WINAPI filter_FindPin(IMediaStreamFilter *iface, const WCHAR *id, IPin **out)
455
{
456
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    unsigned int i;
    WCHAR *ret_id;
    IPin *pin;

    TRACE("iface %p, id %s, out %p.\n", iface, debugstr_w(id), out);

    EnterCriticalSection(&filter->cs);

    for (i = 0; i < filter->nb_streams; ++i)
    {
        if (FAILED(IAMMediaStream_QueryInterface(filter->streams[i], &IID_IPin, (void **)&pin)))
        {
            WARN("Stream %p does not support IPin.\n", filter->streams[i]);
            continue;
        }

        if (SUCCEEDED(IPin_QueryId(pin, &ret_id)))
        {
475
            if (!wcscmp(id, ret_id))
476 477 478
            {
                CoTaskMemFree(ret_id);
                *out = pin;
479
                LeaveCriticalSection(&filter->cs);
480 481 482 483 484 485 486 487 488 489
                return S_OK;
            }
            CoTaskMemFree(ret_id);
        }
        IPin_Release(pin);
    }

    LeaveCriticalSection(&filter->cs);

    return VFW_E_NOT_FOUND;
490 491
}

492
static HRESULT WINAPI filter_QueryFilterInfo(IMediaStreamFilter *iface, FILTER_INFO *info)
493
{
494
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
495 496 497 498 499

    TRACE("iface %p, info %p.\n", iface, info);

    EnterCriticalSection(&filter->cs);

500
    wcscpy(info->achName, filter->name);
501 502 503 504 505 506 507
    if (filter->graph)
        IFilterGraph_AddRef(filter->graph);
    info->pGraph = filter->graph;

    LeaveCriticalSection(&filter->cs);

    return S_OK;
508 509
}

510
static HRESULT WINAPI filter_JoinFilterGraph(IMediaStreamFilter *iface,
511
        IFilterGraph *graph, const WCHAR *name)
512
{
513
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
514 515 516 517 518 519

    TRACE("iface %p, graph %p, name.%s.\n", iface, graph, debugstr_w(name));

    EnterCriticalSection(&filter->cs);

    if (name)
520
        lstrcpynW(filter->name, name, ARRAY_SIZE(filter->name));
521 522 523 524 525 526 527
    else
        filter->name[0] = 0;
    filter->graph = graph;

    LeaveCriticalSection(&filter->cs);

    return S_OK;
528 529
}

530
static HRESULT WINAPI filter_QueryVendorInfo(IMediaStreamFilter *iface, LPWSTR *vendor_info)
531
{
532 533
    WARN("iface %p, vendor_info %p, stub!\n", iface, vendor_info);
    return E_NOTIMPL;
534 535 536 537
}

/*** IMediaStreamFilter methods ***/

538
static HRESULT WINAPI filter_AddMediaStream(IMediaStreamFilter *iface, IAMMediaStream *pAMMediaStream)
539
{
540
    struct filter *This = impl_from_IMediaStreamFilter(iface);
541
    IAMMediaStream** streams;
542
    HRESULT hr;
543

544 545
    TRACE("(%p)->(%p)\n", iface, pAMMediaStream);

546
    streams = realloc(This->streams, (This->nb_streams + 1) * sizeof(*streams));
547 548 549
    if (!streams)
        return E_OUTOFMEMORY;
    This->streams = streams;
550 551

    hr = IAMMediaStream_JoinFilter(pAMMediaStream, iface);
552
    if (FAILED(hr))
553 554 555 556
        return hr;

    hr = IAMMediaStream_JoinFilterGraph(pAMMediaStream, This->graph);
    if (FAILED(hr))
557 558
        return hr;

559
    This->streams[This->nb_streams] = pAMMediaStream;
560 561
    This->nb_streams++;

562
    IAMMediaStream_AddRef(pAMMediaStream);
563 564

    return S_OK;
565 566
}

567
static HRESULT WINAPI filter_GetMediaStream(IMediaStreamFilter *iface, REFMSPID idPurpose, IMediaStream **ppMediaStream)
568
{
569
    struct filter *This = impl_from_IMediaStreamFilter(iface);
570 571
    MSPID purpose_id;
    unsigned int i;
572

573 574
    TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(idPurpose), ppMediaStream);

575 576 577
    if (!ppMediaStream)
        return E_POINTER;

578 579
    for (i = 0; i < This->nb_streams; i++)
    {
580
        IAMMediaStream_GetInformation(This->streams[i], &purpose_id, NULL);
581 582
        if (IsEqualIID(&purpose_id, idPurpose))
        {
583
            *ppMediaStream = (IMediaStream *)This->streams[i];
584 585 586 587 588 589
            IMediaStream_AddRef(*ppMediaStream);
            return S_OK;
        }
    }

    return MS_E_NOSTREAM;
590 591
}

592
static HRESULT WINAPI filter_EnumMediaStreams(IMediaStreamFilter *iface, LONG index, IMediaStream **stream)
593
{
594
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
595

596
    TRACE("filter %p, index %ld, stream %p.\n", filter, index, stream);
597 598 599 600 601 602 603 604 605

    if (index >= filter->nb_streams)
        return S_FALSE;

    if (!stream)
        return E_POINTER;

    IMediaStream_AddRef(*stream = (IMediaStream *)filter->streams[index]);
    return S_OK;
606 607
}

608
static IMediaSeeking *get_seeking(IAMMediaStream *stream)
609
{
610 611 612
    IMediaSeeking *seeking;
    IPin *pin, *peer;
    HRESULT hr;
613

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
    if (FAILED(IAMMediaStream_QueryInterface(stream, &IID_IPin, (void **)&pin)))
    {
        WARN("Stream %p does not support IPin.\n", stream);
        return NULL;
    }

    hr = IPin_ConnectedTo(pin, &peer);
    IPin_Release(pin);
    if (FAILED(hr))
        return NULL;

    hr = IPin_QueryInterface(peer, &IID_IMediaSeeking, (void **)&seeking);
    IPin_Release(peer);
    if (FAILED(hr))
        return NULL;

    return seeking;
}

static HRESULT WINAPI filter_SupportSeeking(IMediaStreamFilter *iface, BOOL renderer)
{
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
    unsigned int i;

    TRACE("filter %p, renderer %d\n", iface, renderer);

    if (!renderer)
        FIXME("Non-renderer filter support is not yet implemented.\n");

    EnterCriticalSection(&filter->cs);

    if (filter->seekable_stream)
    {
        LeaveCriticalSection(&filter->cs);
        return HRESULT_FROM_WIN32(ERROR_ALREADY_INITIALIZED);
    }

    for (i = 0; i < filter->nb_streams; ++i)
    {
        IMediaSeeking *seeking = get_seeking(filter->streams[i]);
        LONGLONG duration;

        if (!seeking)
            continue;

        if (SUCCEEDED(IMediaSeeking_GetDuration(seeking, &duration)))
        {
            filter->seekable_stream = filter->streams[i];
            IMediaSeeking_Release(seeking);
            LeaveCriticalSection(&filter->cs);
            return S_OK;
        }

        IMediaSeeking_Release(seeking);
    }

    LeaveCriticalSection(&filter->cs);
    return E_NOINTERFACE;
672 673
}

674
static HRESULT WINAPI filter_ReferenceTimeToStreamTime(IMediaStreamFilter *iface, REFERENCE_TIME *time)
675
{
676
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
677

678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
    TRACE("filter %p, time %p.\n", filter, time);

    EnterCriticalSection(&filter->cs);

    if (!filter->clock)
    {
        LeaveCriticalSection(&filter->cs);
        return S_FALSE;
    }

    *time -= filter->start_time;

    LeaveCriticalSection(&filter->cs);

    return S_OK;
693 694
}

695
static HRESULT WINAPI filter_GetCurrentStreamTime(IMediaStreamFilter *iface, REFERENCE_TIME *time)
696
{
697
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
698

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
    TRACE("filter %p, time %p.\n", filter, time);

    if (!time)
        return E_POINTER;

    EnterCriticalSection(&filter->cs);

    if (filter->state != State_Running || !filter->clock)
    {
        *time = 0;
        LeaveCriticalSection(&filter->cs);
        return S_FALSE;
    }

    IReferenceClock_GetTime(filter->clock, time);

    *time -= filter->start_time;

    LeaveCriticalSection(&filter->cs);

    return S_OK;
720 721
}

722
static HRESULT WINAPI filter_WaitUntil(IMediaStreamFilter *iface, REFERENCE_TIME time)
723
{
724 725 726 727
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
    struct event *event;
    struct list *entry;
    HRESULT hr;
728

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
    TRACE("filter %p, time %s.\n", iface, wine_dbgstr_longlong(time));

    EnterCriticalSection(&filter->cs);

    if (!filter->clock)
    {
        LeaveCriticalSection(&filter->cs);
        return E_FAIL;
    }

    if ((entry = list_head(&filter->free_events)))
    {
        list_remove(entry);
        event = LIST_ENTRY(entry, struct event, entry);
    }
    else
    {
        event = calloc(1, sizeof(struct event));
        event->event = CreateEventW(NULL, FALSE, FALSE, NULL);

        entry = &event->entry;
    }

    hr = IReferenceClock_AdviseTime(filter->clock, time, filter->start_time, (HEVENT)event->event, &event->cookie);
    if (FAILED(hr))
    {
        list_add_tail(&filter->free_events, entry);
        LeaveCriticalSection(&filter->cs);
        return hr;
    }

    event->interrupted = FALSE;
    list_add_tail(&filter->used_events, entry);

    LeaveCriticalSection(&filter->cs);
    WaitForSingleObject(event->event, INFINITE);
    EnterCriticalSection(&filter->cs);

    hr = event->interrupted ? S_FALSE : S_OK;

    list_remove(entry);
    list_add_tail(&filter->free_events, entry);

    LeaveCriticalSection(&filter->cs);

    return hr;
775 776
}

777
static HRESULT WINAPI filter_Flush(IMediaStreamFilter *iface, BOOL cancel_eos)
778
{
779 780
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
    struct event *event;
781

782 783 784 785 786 787 788 789 790 791 792 793 794 795
    TRACE("filter %p, cancel_eos %d.\n", iface, cancel_eos);

    EnterCriticalSection(&filter->cs);

    LIST_FOR_EACH_ENTRY(event, &filter->used_events, struct event, entry)
    {
        if (!event->interrupted)
        {
            event->interrupted = TRUE;
            IReferenceClock_Unadvise(filter->clock, event->cookie);
            SetEvent(event->event);
        }
    }

796 797 798
    if (cancel_eos)
        --filter->eos_count;

799 800 801
    LeaveCriticalSection(&filter->cs);

    return S_OK;
802 803
}

804
static HRESULT WINAPI filter_EndOfStream(IMediaStreamFilter *iface)
805
{
806
    struct filter *filter = impl_from_IMediaStreamFilter(iface);
807

808 809 810 811 812 813 814 815 816 817 818 819
    TRACE("filter %p.\n", filter);

    EnterCriticalSection(&filter->cs);

    ++filter->eos_count;
    if (filter->state == State_Running && filter->seekable_stream &&
            filter->eos_count == (LONG)filter->nb_streams)
        send_ec_complete(filter);

    LeaveCriticalSection(&filter->cs);

    return S_OK;
820 821
}

822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
static const IMediaStreamFilterVtbl filter_vtbl =
{
    filter_QueryInterface,
    filter_AddRef,
    filter_Release,
    filter_GetClassID,
    filter_Stop,
    filter_Pause,
    filter_Run,
    filter_GetState,
    filter_SetSyncSource,
    filter_GetSyncSource,
    filter_EnumPins,
    filter_FindPin,
    filter_QueryFilterInfo,
    filter_JoinFilterGraph,
    filter_QueryVendorInfo,
    filter_AddMediaStream,
    filter_GetMediaStream,
    filter_EnumMediaStreams,
    filter_SupportSeeking,
    filter_ReferenceTimeToStreamTime,
    filter_GetCurrentStreamTime,
    filter_WaitUntil,
    filter_Flush,
    filter_EndOfStream
848
};
849

850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
static inline struct filter *impl_from_IMediaSeeking(IMediaSeeking *iface)
{
    return CONTAINING_RECORD(iface, struct filter, IMediaSeeking_iface);
}

static HRESULT WINAPI filter_seeking_QueryInterface(IMediaSeeking *iface, REFIID iid, void **out)
{
    struct filter *filter = impl_from_IMediaSeeking(iface);
    return IMediaStreamFilter_QueryInterface(&filter->IMediaStreamFilter_iface, iid, out);
}

static ULONG WINAPI filter_seeking_AddRef(IMediaSeeking *iface)
{
    struct filter *filter = impl_from_IMediaSeeking(iface);
    return IMediaStreamFilter_AddRef(&filter->IMediaStreamFilter_iface);
}

static ULONG WINAPI filter_seeking_Release(IMediaSeeking *iface)
{
    struct filter *filter = impl_from_IMediaSeeking(iface);
    return IMediaStreamFilter_Release(&filter->IMediaStreamFilter_iface);
}

static HRESULT WINAPI filter_seeking_GetCapabilities(IMediaSeeking *iface, DWORD *capabilities)
{
    FIXME("iface %p, capabilities %p, stub!\n", iface, capabilities);

    return E_NOTIMPL;
}

static HRESULT WINAPI filter_seeking_CheckCapabilities(IMediaSeeking *iface, DWORD *capabilities)
{
    FIXME("iface %p, capabilities %p, stub!\n", iface, capabilities);

    return E_NOTIMPL;
}

static HRESULT WINAPI filter_seeking_IsFormatSupported(IMediaSeeking *iface, const GUID *format)
{
889 890 891
    struct filter *filter = impl_from_IMediaSeeking(iface);
    IMediaSeeking *seeking;
    HRESULT hr;
892

893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
    TRACE("filter %p, format %s.\n", filter, debugstr_guid(format));

    EnterCriticalSection(&filter->cs);

    seeking = get_seeking(filter->seekable_stream);

    LeaveCriticalSection(&filter->cs);

    if (!seeking)
        return E_NOTIMPL;

    hr = IMediaSeeking_IsFormatSupported(seeking, format);
    IMediaSeeking_Release(seeking);

    return hr;
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
}

static HRESULT WINAPI filter_seeking_QueryPreferredFormat(IMediaSeeking *iface, GUID *format)
{
    FIXME("iface %p, format %p, stub!\n", iface, format);

    return E_NOTIMPL;
}

static HRESULT WINAPI filter_seeking_GetTimeFormat(IMediaSeeking *iface, GUID *format)
{
    FIXME("iface %p, format %p, stub!\n", iface, format);

    return E_NOTIMPL;
}

static HRESULT WINAPI filter_seeking_IsUsingTimeFormat(IMediaSeeking *iface, const GUID *format)
{
    FIXME("iface %p, format %s, stub!\n", iface, debugstr_guid(format));

    return E_NOTIMPL;
}

static HRESULT WINAPI filter_seeking_SetTimeFormat(IMediaSeeking *iface, const GUID *format)
{
    FIXME("iface %p, format %s, stub!\n", iface, debugstr_guid(format));

    return E_NOTIMPL;
}

static HRESULT WINAPI filter_seeking_GetDuration(IMediaSeeking *iface, LONGLONG *duration)
{
940 941 942
    struct filter *filter = impl_from_IMediaSeeking(iface);
    IMediaSeeking *seeking;
    HRESULT hr;
943

944 945 946 947
    TRACE("filter %p, duration %p.\n", filter, duration);

    EnterCriticalSection(&filter->cs);

948 949 950 951 952
    seeking = get_seeking(filter->seekable_stream);

    LeaveCriticalSection(&filter->cs);

    if (!seeking)
953
        return E_NOTIMPL;
954

955 956 957 958
    hr = IMediaSeeking_GetDuration(seeking, duration);
    IMediaSeeking_Release(seeking);

    return hr;
959 960 961 962
}

static HRESULT WINAPI filter_seeking_GetStopPosition(IMediaSeeking *iface, LONGLONG *stop)
{
963 964 965
    struct filter *filter = impl_from_IMediaSeeking(iface);
    IMediaSeeking *seeking;
    HRESULT hr;
966

967 968 969 970
    TRACE("filter %p, stop %p.\n", filter, stop);

    EnterCriticalSection(&filter->cs);

971 972 973 974 975
    seeking = get_seeking(filter->seekable_stream);

    LeaveCriticalSection(&filter->cs);

    if (!seeking)
976
        return E_NOTIMPL;
977

978 979 980 981
    hr = IMediaSeeking_GetStopPosition(seeking, stop);
    IMediaSeeking_Release(seeking);

    return hr;
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
}

static HRESULT WINAPI filter_seeking_GetCurrentPosition(IMediaSeeking *iface, LONGLONG *current)
{
    FIXME("iface %p, current %p, stub!\n", iface, current);

    return E_NOTIMPL;
}

static HRESULT WINAPI filter_seeking_ConvertTimeFormat(IMediaSeeking *iface, LONGLONG *target,
        const GUID *target_format, LONGLONG source, const GUID *source_format)
{
    FIXME("iface %p, target %p, target_format %s, source 0x%s, source_format %s, stub!\n", iface, target, debugstr_guid(target_format),
            wine_dbgstr_longlong(source), debugstr_guid(source_format));

    return E_NOTIMPL;
}

static HRESULT WINAPI filter_seeking_SetPositions(IMediaSeeking *iface, LONGLONG *current_ptr, DWORD current_flags,
        LONGLONG *stop_ptr, DWORD stop_flags)
{
1003 1004 1005 1006
    struct filter *filter = impl_from_IMediaSeeking(iface);
    IMediaSeeking *seeking;
    HRESULT hr;

1007
    TRACE("iface %p, current %s, current_flags %#lx, stop %s, stop_flags %#lx.\n", iface,
1008 1009 1010
            current_ptr ? wine_dbgstr_longlong(*current_ptr) : "<null>", current_flags,
            stop_ptr ? wine_dbgstr_longlong(*stop_ptr): "<null>", stop_flags);

1011 1012 1013 1014
    EnterCriticalSection(&filter->cs);

    seeking = get_seeking(filter->seekable_stream);

1015 1016
    LeaveCriticalSection(&filter->cs);

1017 1018 1019 1020 1021 1022 1023 1024
    if (!seeking)
        return E_NOTIMPL;

    hr = IMediaSeeking_SetPositions(seeking, current_ptr, current_flags, stop_ptr, stop_flags);

    IMediaSeeking_Release(seeking);

    return hr;
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
}

static HRESULT WINAPI filter_seeking_GetPositions(IMediaSeeking *iface, LONGLONG *current, LONGLONG *stop)
{
    FIXME("iface %p, current %p, stop %p, stub!\n", iface, current, stop);

    return E_NOTIMPL;
}

static HRESULT WINAPI filter_seeking_GetAvailable(IMediaSeeking *iface, LONGLONG *earliest, LONGLONG *latest)
{
    FIXME("iface %p, earliest %p, latest %p, stub!\n", iface, earliest, latest);

    return E_NOTIMPL;
}

static HRESULT WINAPI filter_seeking_SetRate(IMediaSeeking *iface, double rate)
{
    FIXME("iface %p, rate %f, stub!\n", iface, rate);

    return E_NOTIMPL;
}

static HRESULT WINAPI filter_seeking_GetRate(IMediaSeeking *iface, double *rate)
{
    FIXME("iface %p, rate %p, stub!\n", iface, rate);

    return E_NOTIMPL;
}

static HRESULT WINAPI filter_seeking_GetPreroll(IMediaSeeking *iface, LONGLONG *preroll)
{
    FIXME("iface %p, preroll %p, stub!\n", iface, preroll);

    return E_NOTIMPL;
}

static const IMediaSeekingVtbl filter_seeking_vtbl =
{
    filter_seeking_QueryInterface,
    filter_seeking_AddRef,
    filter_seeking_Release,
    filter_seeking_GetCapabilities,
    filter_seeking_CheckCapabilities,
    filter_seeking_IsFormatSupported,
    filter_seeking_QueryPreferredFormat,
    filter_seeking_GetTimeFormat,
    filter_seeking_IsUsingTimeFormat,
    filter_seeking_SetTimeFormat,
    filter_seeking_GetDuration,
    filter_seeking_GetStopPosition,
    filter_seeking_GetCurrentPosition,
    filter_seeking_ConvertTimeFormat,
    filter_seeking_SetPositions,
    filter_seeking_GetPositions,
    filter_seeking_GetAvailable,
    filter_seeking_SetRate,
    filter_seeking_GetRate,
    filter_seeking_GetPreroll,
};

1086
HRESULT filter_create(IUnknown *outer, void **out)
1087
{
1088
    struct filter *object;
1089

1090
    TRACE("outer %p, out %p.\n", outer, out);
1091

1092
    if (outer)
1093 1094
        return CLASS_E_NOAGGREGATION;

1095
    if (!(object = calloc(1, sizeof(*object))))
1096 1097
        return E_OUTOFMEMORY;

1098
    object->IMediaStreamFilter_iface.lpVtbl = &filter_vtbl;
1099
    object->IMediaSeeking_iface.lpVtbl = &filter_seeking_vtbl;
1100
    object->refcount = 1;
1101 1102
    list_init(&object->free_events);
    list_init(&object->used_events);
1103 1104
    InitializeCriticalSection(&object->cs);
    object->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MediaStreamFilter.cs");
1105

1106 1107
    TRACE("Created media stream filter %p.\n", object);
    *out = &object->IMediaStreamFilter_iface;
1108 1109
    return S_OK;
}