topology.c 65.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright 2017 Nikolay Sivov
 *
 * 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
 */
18

19 20 21 22 23 24 25

#include <stdarg.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
26 27 28

#undef INITGUID
#include <guiddef.h>
29 30 31
#include "mfidl.h"

#include "wine/debug.h"
32 33

#include "mf_private.h"
34 35 36

WINE_DEFAULT_DEBUG_CHANNEL(mfplat);

37
static LONG next_node_id;
38
static TOPOID next_topology_id;
39

40 41 42
struct node_stream
{
    IMFMediaType *preferred_type;
43 44 45 46 47 48 49 50 51
    struct topology_node *connection;
    DWORD connection_stream;
};

struct node_streams
{
    struct node_stream *streams;
    size_t size;
    size_t count;
52 53
};

54 55 56 57 58 59
struct topology_node
{
    IMFTopologyNode IMFTopologyNode_iface;
    LONG refcount;
    IMFAttributes *attributes;
    MF_TOPOLOGY_TYPE node_type;
60
    TOPOID id;
61
    IUnknown *object;
62
    IMFMediaType *input_type; /* Only for tee nodes. */
63 64
    struct node_streams inputs;
    struct node_streams outputs;
65
    CRITICAL_SECTION cs;
66 67
};

68 69 70 71 72 73 74 75 76 77 78 79 80 81
struct topology
{
    IMFTopology IMFTopology_iface;
    LONG refcount;
    IMFAttributes *attributes;
    struct
    {
        struct topology_node **nodes;
        size_t size;
        size_t count;
    } nodes;
    TOPOID id;
};

82 83 84
struct seq_source
{
    IMFSequencerSource IMFSequencerSource_iface;
85
    IMFMediaSourceTopologyProvider IMFMediaSourceTopologyProvider_iface;
86 87 88
    LONG refcount;
};

89
static inline struct topology *impl_from_IMFTopology(IMFTopology *iface)
90
{
91
    return CONTAINING_RECORD(iface, struct topology, IMFTopology_iface);
92 93
}

94 95 96 97 98
static struct topology_node *impl_from_IMFTopologyNode(IMFTopologyNode *iface)
{
    return CONTAINING_RECORD(iface, struct topology_node, IMFTopologyNode_iface);
}

99 100 101 102 103 104 105 106 107
static const IMFTopologyNodeVtbl topologynodevtbl;

static struct topology_node *unsafe_impl_from_IMFTopologyNode(IMFTopologyNode *iface)
{
    if (!iface || iface->lpVtbl != &topologynodevtbl)
        return NULL;
    return impl_from_IMFTopologyNode(iface);
}

108 109 110 111
static HRESULT create_topology_node(MF_TOPOLOGY_TYPE node_type, struct topology_node **node);
static HRESULT topology_node_connect_output(struct topology_node *node, DWORD output_index,
        struct topology_node *connection, DWORD input_index);

112 113
static struct topology *unsafe_impl_from_IMFTopology(IMFTopology *iface);

114 115 116 117 118
static struct seq_source *impl_from_IMFSequencerSource(IMFSequencerSource *iface)
{
    return CONTAINING_RECORD(iface, struct seq_source, IMFSequencerSource_iface);
}

119 120 121 122 123
static struct seq_source *impl_from_IMFMediaSourceTopologyProvider(IMFMediaSourceTopologyProvider *iface)
{
    return CONTAINING_RECORD(iface, struct seq_source, IMFMediaSourceTopologyProvider_iface);
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137
static HRESULT topology_node_reserve_streams(struct node_streams *streams, DWORD index)
{
    if (!mf_array_reserve((void **)&streams->streams, &streams->size, index + 1, sizeof(*streams->streams)))
        return E_OUTOFMEMORY;

    if (index >= streams->count)
    {
        memset(&streams->streams[streams->count], 0, (index - streams->count + 1) * sizeof(*streams->streams));
        streams->count = index + 1;
    }

    return S_OK;
}

138
static HRESULT WINAPI topology_QueryInterface(IMFTopology *iface, REFIID riid, void **out)
139
{
140
    struct topology *topology = impl_from_IMFTopology(iface);
141

142
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
143 144 145 146 147

    if (IsEqualIID(riid, &IID_IMFTopology) ||
            IsEqualIID(riid, &IID_IMFAttributes) ||
            IsEqualIID(riid, &IID_IUnknown))
    {
148
        *out = &topology->IMFTopology_iface;
149 150 151 152 153 154 155 156 157 158 159 160
    }
    else
    {
        FIXME("(%s, %p)\n", debugstr_guid(riid), out);
        *out = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*out);
    return S_OK;
}

161
static ULONG WINAPI topology_AddRef(IMFTopology *iface)
162
{
163 164
    struct topology *topology = impl_from_IMFTopology(iface);
    ULONG refcount = InterlockedIncrement(&topology->refcount);
165

166
    TRACE("%p, refcount %lu.\n", iface, refcount);
167

168
    return refcount;
169 170
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
static HRESULT topology_node_disconnect_output(struct topology_node *node, DWORD output_index)
{
    struct topology_node *connection = NULL;
    struct node_stream *stream;
    DWORD connection_stream;
    HRESULT hr = S_OK;

    EnterCriticalSection(&node->cs);

    if (output_index < node->outputs.count)
    {
        stream = &node->outputs.streams[output_index];

        if (stream->connection)
        {
            connection = stream->connection;
            connection_stream = stream->connection_stream;
            stream->connection = NULL;
            stream->connection_stream = 0;
        }
        else
            hr = MF_E_NOT_FOUND;
    }
    else
        hr = E_INVALIDARG;

    LeaveCriticalSection(&node->cs);

    if (connection)
    {
        EnterCriticalSection(&connection->cs);

        if (connection_stream < connection->inputs.count)
        {
            stream = &connection->inputs.streams[connection_stream];

            if (stream->connection)
            {
                stream->connection = NULL;
                stream->connection_stream = 0;
            }
        }

        LeaveCriticalSection(&connection->cs);

        IMFTopologyNode_Release(&connection->IMFTopologyNode_iface);
        IMFTopologyNode_Release(&node->IMFTopologyNode_iface);
    }

    return hr;
}

static void topology_node_disconnect(struct topology_node *node)
{
    struct node_stream *stream;
    size_t i;

    for (i = 0; i < node->outputs.count; ++i)
        topology_node_disconnect_output(node, i);

    for (i = 0; i < node->inputs.count; ++i)
    {
        stream = &node->inputs.streams[i];
        if (stream->connection)
            topology_node_disconnect_output(stream->connection, stream->connection_stream);
    }
}

239 240 241 242 243 244
static void topology_clear(struct topology *topology)
{
    size_t i;

    for (i = 0; i < topology->nodes.count; ++i)
    {
245
        topology_node_disconnect(topology->nodes.nodes[i]);
246 247
        IMFTopologyNode_Release(&topology->nodes.nodes[i]->IMFTopologyNode_iface);
    }
248
    free(topology->nodes.nodes);
249 250 251 252 253
    topology->nodes.nodes = NULL;
    topology->nodes.count = 0;
    topology->nodes.size = 0;
}

254
static ULONG WINAPI topology_Release(IMFTopology *iface)
255
{
256 257
    struct topology *topology = impl_from_IMFTopology(iface);
    ULONG refcount = InterlockedDecrement(&topology->refcount);
258

259
    TRACE("%p, refcount %lu.\n", iface, refcount);
260

261
    if (!refcount)
262
    {
263 264
        if (topology->attributes)
            IMFAttributes_Release(topology->attributes);
265
        topology_clear(topology);
266
        free(topology);
267 268
    }

269
    return refcount;
270 271
}

272
static HRESULT WINAPI topology_GetItem(IMFTopology *iface, REFGUID key, PROPVARIANT *value)
273
{
274
    struct topology *topology = impl_from_IMFTopology(iface);
275

276
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
277

278
    return IMFAttributes_GetItem(topology->attributes, key, value);
279 280
}

281
static HRESULT WINAPI topology_GetItemType(IMFTopology *iface, REFGUID key, MF_ATTRIBUTE_TYPE *type)
282
{
283
    struct topology *topology = impl_from_IMFTopology(iface);
284

285
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), type);
286

287
    return IMFAttributes_GetItemType(topology->attributes, key, type);
288 289
}

290
static HRESULT WINAPI topology_CompareItem(IMFTopology *iface, REFGUID key, REFPROPVARIANT value, BOOL *result)
291
{
292
    struct topology *topology = impl_from_IMFTopology(iface);
293

294
    TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), value, result);
295

296
    return IMFAttributes_CompareItem(topology->attributes, key, value, result);
297 298
}

299 300
static HRESULT WINAPI topology_Compare(IMFTopology *iface, IMFAttributes *theirs, MF_ATTRIBUTES_MATCH_TYPE type,
        BOOL *result)
301
{
302
    struct topology *topology = impl_from_IMFTopology(iface);
303

304
    TRACE("%p, %p, %d, %p.\n", iface, theirs, type, result);
305

306
    return IMFAttributes_Compare(topology->attributes, theirs, type, result);
307 308
}

309
static HRESULT WINAPI topology_GetUINT32(IMFTopology *iface, REFGUID key, UINT32 *value)
310
{
311
    struct topology *topology = impl_from_IMFTopology(iface);
312

313
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
314

315
    return IMFAttributes_GetUINT32(topology->attributes, key, value);
316 317
}

318
static HRESULT WINAPI topology_GetUINT64(IMFTopology *iface, REFGUID key, UINT64 *value)
319
{
320
    struct topology *topology = impl_from_IMFTopology(iface);
321

322
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
323

324
    return IMFAttributes_GetUINT64(topology->attributes, key, value);
325 326
}

327
static HRESULT WINAPI topology_GetDouble(IMFTopology *iface, REFGUID key, double *value)
328
{
329
    struct topology *topology = impl_from_IMFTopology(iface);
330

331
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
332

333
    return IMFAttributes_GetDouble(topology->attributes, key, value);
334 335
}

336
static HRESULT WINAPI topology_GetGUID(IMFTopology *iface, REFGUID key, GUID *value)
337
{
338
    struct topology *topology = impl_from_IMFTopology(iface);
339

340
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
341

342
    return IMFAttributes_GetGUID(topology->attributes, key, value);
343 344
}

345
static HRESULT WINAPI topology_GetStringLength(IMFTopology *iface, REFGUID key, UINT32 *length)
346
{
347
    struct topology *topology = impl_from_IMFTopology(iface);
348

349
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), length);
350

351
    return IMFAttributes_GetStringLength(topology->attributes, key, length);
352 353
}

354 355
static HRESULT WINAPI topology_GetString(IMFTopology *iface, REFGUID key, WCHAR *value,
        UINT32 size, UINT32 *length)
356
{
357
    struct topology *topology = impl_from_IMFTopology(iface);
358

359
    TRACE("%p, %s, %p, %d, %p.\n", iface, debugstr_guid(key), value, size, length);
360

361
    return IMFAttributes_GetString(topology->attributes, key, value, size, length);
362 363
}

364 365
static HRESULT WINAPI topology_GetAllocatedString(IMFTopology *iface, REFGUID key,
        WCHAR **value, UINT32 *length)
366
{
367
    struct topology *topology = impl_from_IMFTopology(iface);
368

369
    TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), value, length);
370

371
    return IMFAttributes_GetAllocatedString(topology->attributes, key, value, length);
372 373
}

374
static HRESULT WINAPI topology_GetBlobSize(IMFTopology *iface, REFGUID key, UINT32 *size)
375
{
376
    struct topology *topology = impl_from_IMFTopology(iface);
377

378
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), size);
379

380
    return IMFAttributes_GetBlobSize(topology->attributes, key, size);
381 382
}

383 384
static HRESULT WINAPI topology_GetBlob(IMFTopology *iface, REFGUID key, UINT8 *buf,
        UINT32 bufsize, UINT32 *blobsize)
385
{
386
    struct topology *topology = impl_from_IMFTopology(iface);
387

388
    TRACE("%p, %s, %p, %d, %p.\n", iface, debugstr_guid(key), buf, bufsize, blobsize);
389

390
    return IMFAttributes_GetBlob(topology->attributes, key, buf, bufsize, blobsize);
391 392
}

393
static HRESULT WINAPI topology_GetAllocatedBlob(IMFTopology *iface, REFGUID key, UINT8 **buf, UINT32 *size)
394
{
395
    struct topology *topology = impl_from_IMFTopology(iface);
396

397
    TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), buf, size);
398

399
    return IMFAttributes_GetAllocatedBlob(topology->attributes, key, buf, size);
400 401
}

402
static HRESULT WINAPI topology_GetUnknown(IMFTopology *iface, REFGUID key, REFIID riid, void **ppv)
403
{
404
    struct topology *topology = impl_from_IMFTopology(iface);
405

406
    TRACE("%p, %s, %s, %p.\n", iface, debugstr_guid(key), debugstr_guid(riid), ppv);
407

408
    return IMFAttributes_GetUnknown(topology->attributes, key, riid, ppv);
409 410
}

411
static HRESULT WINAPI topology_SetItem(IMFTopology *iface, REFGUID key, REFPROPVARIANT value)
412
{
413
    struct topology *topology = impl_from_IMFTopology(iface);
414

415
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
416

417
    return IMFAttributes_SetItem(topology->attributes, key, value);
418 419
}

420
static HRESULT WINAPI topology_DeleteItem(IMFTopology *iface, REFGUID key)
421
{
422
    struct topology *topology = impl_from_IMFTopology(iface);
423

424
    TRACE("%p, %s.\n", topology, debugstr_guid(key));
425

426
    return IMFAttributes_DeleteItem(topology->attributes, key);
427 428
}

429
static HRESULT WINAPI topology_DeleteAllItems(IMFTopology *iface)
430
{
431
    struct topology *topology = impl_from_IMFTopology(iface);
432

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

435
    return IMFAttributes_DeleteAllItems(topology->attributes);
436 437
}

438
static HRESULT WINAPI topology_SetUINT32(IMFTopology *iface, REFGUID key, UINT32 value)
439
{
440
    struct topology *topology = impl_from_IMFTopology(iface);
441

442
    TRACE("%p, %s, %d.\n", iface, debugstr_guid(key), value);
443

444
    return IMFAttributes_SetUINT32(topology->attributes, key, value);
445 446
}

447
static HRESULT WINAPI topology_SetUINT64(IMFTopology *iface, REFGUID key, UINT64 value)
448
{
449
    struct topology *topology = impl_from_IMFTopology(iface);
450

451
    TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), wine_dbgstr_longlong(value));
452

453
    return IMFAttributes_SetUINT64(topology->attributes, key, value);
454 455
}

456
static HRESULT WINAPI topology_SetDouble(IMFTopology *iface, REFGUID key, double value)
457
{
458
    struct topology *topology = impl_from_IMFTopology(iface);
459

460
    TRACE("%p, %s, %f.\n", iface, debugstr_guid(key), value);
461

462
    return IMFAttributes_SetDouble(topology->attributes, key, value);
463 464
}

465
static HRESULT WINAPI topology_SetGUID(IMFTopology *iface, REFGUID key, REFGUID value)
466
{
467
    struct topology *topology = impl_from_IMFTopology(iface);
468

469
    TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), debugstr_guid(value));
470

471
    return IMFAttributes_SetGUID(topology->attributes, key, value);
472 473
}

474
static HRESULT WINAPI topology_SetString(IMFTopology *iface, REFGUID key, const WCHAR *value)
475
{
476
    struct topology *topology = impl_from_IMFTopology(iface);
477

478
    TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), debugstr_w(value));
479

480
    return IMFAttributes_SetString(topology->attributes, key, value);
481 482
}

483
static HRESULT WINAPI topology_SetBlob(IMFTopology *iface, REFGUID key, const UINT8 *buf, UINT32 size)
484
{
485
    struct topology *topology = impl_from_IMFTopology(iface);
486

487
    TRACE("%p, %s, %p, %d.\n", iface, debugstr_guid(key), buf, size);
488

489
    return IMFAttributes_SetBlob(topology->attributes, key, buf, size);
490 491
}

492
static HRESULT WINAPI topology_SetUnknown(IMFTopology *iface, REFGUID key, IUnknown *unknown)
493
{
494
    struct topology *topology = impl_from_IMFTopology(iface);
495

496
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), unknown);
497

498
    return IMFAttributes_SetUnknown(topology->attributes, key, unknown);
499 500
}

501
static HRESULT WINAPI topology_LockStore(IMFTopology *iface)
502
{
503
    struct topology *topology = impl_from_IMFTopology(iface);
504

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

507
    return IMFAttributes_LockStore(topology->attributes);
508 509
}

510
static HRESULT WINAPI topology_UnlockStore(IMFTopology *iface)
511
{
512
    struct topology *topology = impl_from_IMFTopology(iface);
513

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

516
    return IMFAttributes_UnlockStore(topology->attributes);
517 518
}

519
static HRESULT WINAPI topology_GetCount(IMFTopology *iface, UINT32 *count)
520
{
521
    struct topology *topology = impl_from_IMFTopology(iface);
522

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

525
    return IMFAttributes_GetCount(topology->attributes, count);
526 527
}

528
static HRESULT WINAPI topology_GetItemByIndex(IMFTopology *iface, UINT32 index, GUID *key, PROPVARIANT *value)
529
{
530
    struct topology *topology = impl_from_IMFTopology(iface);
531

532
    TRACE("%p, %u, %p, %p.\n", iface, index, key, value);
533

534
    return IMFAttributes_GetItemByIndex(topology->attributes, index, key, value);
535 536
}

537
static HRESULT WINAPI topology_CopyAllItems(IMFTopology *iface, IMFAttributes *dest)
538
{
539
    struct topology *topology = impl_from_IMFTopology(iface);
540

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

543
    return IMFAttributes_CopyAllItems(topology->attributes, dest);
544 545
}

546
static HRESULT WINAPI topology_GetTopologyID(IMFTopology *iface, TOPOID *id)
547
{
548
    struct topology *topology = impl_from_IMFTopology(iface);
549

550
    TRACE("%p, %p.\n", iface, id);
551 552 553 554 555 556 557

    if (!id)
        return E_POINTER;

    *id = topology->id;

    return S_OK;
558 559
}

560
static HRESULT topology_get_node_by_id(const struct topology *topology, TOPOID id, struct topology_node **node)
561
{
562
    size_t i = 0;
563

564
    for (i = 0; i < topology->nodes.count; ++i)
565
    {
566
        if (topology->nodes.nodes[i]->id == id)
567
        {
568
            *node = topology->nodes.nodes[i];
569 570 571 572 573 574 575
            return S_OK;
        }
    }

    return MF_E_NOT_FOUND;
}

576
static HRESULT topology_add_node(struct topology *topology, struct topology_node *node)
577
{
578
    struct topology_node *match;
579

580 581 582
    if (!node)
        return E_POINTER;

583 584
    if (SUCCEEDED(topology_get_node_by_id(topology, node->id, &match)))
        return E_INVALIDARG;
585

586 587 588 589 590
    if (!mf_array_reserve((void **)&topology->nodes.nodes, &topology->nodes.size, topology->nodes.count + 1,
            sizeof(*topology->nodes.nodes)))
    {
        return E_OUTOFMEMORY;
    }
591

592 593
    topology->nodes.nodes[topology->nodes.count++] = node;
    IMFTopologyNode_AddRef(&node->IMFTopologyNode_iface);
594

595
    return S_OK;
596 597
}

598
static HRESULT WINAPI topology_AddNode(IMFTopology *iface, IMFTopologyNode *node_iface)
599 600
{
    struct topology *topology = impl_from_IMFTopology(iface);
601
    struct topology_node *node = unsafe_impl_from_IMFTopologyNode(node_iface);
602

603
    TRACE("%p, %p.\n", iface, node_iface);
604 605 606 607

    return topology_add_node(topology, node);
}

608
static HRESULT WINAPI topology_RemoveNode(IMFTopology *iface, IMFTopologyNode *node)
609
{
610
    struct topology *topology = impl_from_IMFTopology(iface);
611
    size_t i, count;
612

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

615
    for (i = 0; i < topology->nodes.count; ++i)
616
    {
617
        if (&topology->nodes.nodes[i]->IMFTopologyNode_iface == node)
618
        {
619 620
            topology_node_disconnect(topology->nodes.nodes[i]);
            IMFTopologyNode_Release(&topology->nodes.nodes[i]->IMFTopologyNode_iface);
621 622 623 624 625 626 627
            count = topology->nodes.count - i - 1;
            if (count)
            {
                memmove(&topology->nodes.nodes[i], &topology->nodes.nodes[i + 1],
                        count * sizeof(*topology->nodes.nodes));
            }
            topology->nodes.count--;
628 629 630 631 632
            return S_OK;
        }
    }

    return E_INVALIDARG;
633 634
}

635
static HRESULT WINAPI topology_GetNodeCount(IMFTopology *iface, WORD *count)
636
{
637
    struct topology *topology = impl_from_IMFTopology(iface);
638

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

641 642
    if (!count)
        return E_POINTER;
643

644 645 646
    *count = topology->nodes.count;

    return S_OK;
647 648
}

649
static HRESULT WINAPI topology_GetNode(IMFTopology *iface, WORD index, IMFTopologyNode **node)
650
{
651
    struct topology *topology = impl_from_IMFTopology(iface);
652

653
    TRACE("%p, %u, %p.\n", iface, index, node);
654 655 656 657

    if (!node)
        return E_POINTER;

658 659 660 661 662 663 664
    if (index >= topology->nodes.count)
        return MF_E_INVALIDINDEX;

    *node = &topology->nodes.nodes[index]->IMFTopologyNode_iface;
    IMFTopologyNode_AddRef(*node);

    return S_OK;
665 666
}

667
static HRESULT WINAPI topology_Clear(IMFTopology *iface)
668
{
669
    struct topology *topology = impl_from_IMFTopology(iface);
670

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

673 674
    topology_clear(topology);
    return S_OK;
675 676
}

677
static HRESULT WINAPI topology_CloneFrom(IMFTopology *iface, IMFTopology *src)
678
{
679 680
    struct topology *topology = impl_from_IMFTopology(iface);
    struct topology *src_topology = unsafe_impl_from_IMFTopology(src);
681 682
    struct topology_node *node;
    size_t i, j;
683
    HRESULT hr;
684

685 686 687 688 689 690 691
    TRACE("%p, %p.\n", iface, src);

    topology_clear(topology);

    /* Clone nodes. */
    for (i = 0; i < src_topology->nodes.count; ++i)
    {
692
        if (FAILED(hr = create_topology_node(src_topology->nodes.nodes[i]->node_type, &node)))
693
        {
694
            WARN("Failed to create a node, hr %#lx.\n", hr);
695 696 697
            break;
        }

698 699 700
        if (SUCCEEDED(hr = IMFTopologyNode_CloneFrom(&node->IMFTopologyNode_iface,
                &src_topology->nodes.nodes[i]->IMFTopologyNode_iface)))
        {
701
            topology_add_node(topology, node);
702
        }
703

704
        IMFTopologyNode_Release(&node->IMFTopologyNode_iface);
705 706
    }

707 708 709 710 711 712 713 714
    /* Clone connections. */
    for (i = 0; i < src_topology->nodes.count; ++i)
    {
        const struct node_streams *outputs = &src_topology->nodes.nodes[i]->outputs;

        for (j = 0; j < outputs->count; ++j)
        {
            DWORD input_index = outputs->streams[j].connection_stream;
715 716 717 718 719 720
            TOPOID id;

            if (!outputs->streams[j].connection)
                continue;

            id = outputs->streams[j].connection->id;
721 722 723 724 725 726

            /* Skip node lookup in destination topology, assuming same node order. */
            if (SUCCEEDED(hr = topology_get_node_by_id(topology, id, &node)))
                topology_node_connect_output(topology->nodes.nodes[i], j, node, input_index);
        }
    }
727 728 729 730 731 732 733

    /* Copy attributes and id. */
    hr = IMFTopology_CopyAllItems(src, (IMFAttributes *)&topology->IMFTopology_iface);
    if (SUCCEEDED(hr))
        topology->id = src_topology->id;

    return S_OK;
734 735
}

736
static HRESULT WINAPI topology_GetNodeByID(IMFTopology *iface, TOPOID id, IMFTopologyNode **ret)
737
{
738
    struct topology *topology = impl_from_IMFTopology(iface);
739
    struct topology_node *node;
740 741
    HRESULT hr;

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

744
    if (SUCCEEDED(hr = topology_get_node_by_id(topology, id, &node)))
745
    {
746
        *ret = &node->IMFTopologyNode_iface;
747 748
        IMFTopologyNode_AddRef(*ret);
    }
749 750
    else
        *ret = NULL;
751

752
    return hr;
753 754 755 756 757 758
}

static HRESULT topology_get_node_collection(const struct topology *topology, MF_TOPOLOGY_TYPE node_type,
        IMFCollection **collection)
{
    HRESULT hr;
759
    size_t i;
760 761 762 763 764 765 766

    if (!collection)
        return E_POINTER;

    if (FAILED(hr = MFCreateCollection(collection)))
        return hr;

767
    for (i = 0; i < topology->nodes.count; ++i)
768
    {
769
        if (topology->nodes.nodes[i]->node_type == node_type)
770
        {
771 772 773 774 775 776 777
            if (FAILED(hr = IMFCollection_AddElement(*collection,
                    (IUnknown *)&topology->nodes.nodes[i]->IMFTopologyNode_iface)))
            {
                IMFCollection_Release(*collection);
                *collection = NULL;
                break;
            }
778 779 780 781 782 783
        }
    }

    return hr;
}

784
static HRESULT WINAPI topology_GetSourceNodeCollection(IMFTopology *iface, IMFCollection **collection)
785
{
786
    struct topology *topology = impl_from_IMFTopology(iface);
787

788
    TRACE("%p, %p.\n", iface, collection);
789 790

    return topology_get_node_collection(topology, MF_TOPOLOGY_SOURCESTREAM_NODE, collection);
791 792
}

793
static HRESULT WINAPI topology_GetOutputNodeCollection(IMFTopology *iface, IMFCollection **collection)
794
{
795
    struct topology *topology = impl_from_IMFTopology(iface);
796

797
    TRACE("%p, %p.\n", iface, collection);
798 799

    return topology_get_node_collection(topology, MF_TOPOLOGY_OUTPUT_NODE, collection);
800 801
}

802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 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
static const IMFTopologyVtbl topologyvtbl =
{
    topology_QueryInterface,
    topology_AddRef,
    topology_Release,
    topology_GetItem,
    topology_GetItemType,
    topology_CompareItem,
    topology_Compare,
    topology_GetUINT32,
    topology_GetUINT64,
    topology_GetDouble,
    topology_GetGUID,
    topology_GetStringLength,
    topology_GetString,
    topology_GetAllocatedString,
    topology_GetBlobSize,
    topology_GetBlob,
    topology_GetAllocatedBlob,
    topology_GetUnknown,
    topology_SetItem,
    topology_DeleteItem,
    topology_DeleteAllItems,
    topology_SetUINT32,
    topology_SetUINT64,
    topology_SetDouble,
    topology_SetGUID,
    topology_SetString,
    topology_SetBlob,
    topology_SetUnknown,
    topology_LockStore,
    topology_UnlockStore,
    topology_GetCount,
    topology_GetItemByIndex,
    topology_CopyAllItems,
    topology_GetTopologyID,
    topology_AddNode,
    topology_RemoveNode,
    topology_GetNodeCount,
    topology_GetNode,
    topology_Clear,
    topology_CloneFrom,
    topology_GetNodeByID,
    topology_GetSourceNodeCollection,
    topology_GetOutputNodeCollection,
847 848
};

849 850 851 852 853 854 855
static struct topology *unsafe_impl_from_IMFTopology(IMFTopology *iface)
{
    if (!iface || iface->lpVtbl != &topologyvtbl)
        return NULL;
    return impl_from_IMFTopology(iface);
}

856 857 858 859 860 861 862 863
static TOPOID topology_generate_id(void)
{
    TOPOID old;

    do
    {
        old = next_topology_id;
    }
864
    while (InterlockedCompareExchange64((LONG64 *)&next_topology_id, old + 1, old) != old);
865 866 867 868

    return next_topology_id;
}

869 870 871 872 873
/***********************************************************************
 *      MFCreateTopology (mf.@)
 */
HRESULT WINAPI MFCreateTopology(IMFTopology **topology)
{
874 875
    struct topology *object;
    HRESULT hr;
876

877
    TRACE("%p.\n", topology);
878

879 880 881
    if (!topology)
        return E_POINTER;

882
    if (!(object = calloc(1, sizeof(*object))))
883 884
        return E_OUTOFMEMORY;

885 886
    object->IMFTopology_iface.lpVtbl = &topologyvtbl;
    object->refcount = 1;
887

888 889 890
    hr = MFCreateAttributes(&object->attributes, 0);
    if (FAILED(hr))
    {
891
        IMFTopology_Release(&object->IMFTopology_iface);
892 893
        return hr;
    }
894

895 896
    object->id = topology_generate_id();

897 898 899 900
    *topology = &object->IMFTopology_iface;

    return S_OK;
}
901 902 903 904 905

static HRESULT WINAPI topology_node_QueryInterface(IMFTopologyNode *iface, REFIID riid, void **out)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

906
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
907 908 909 910 911 912 913 914 915 916

    if (IsEqualIID(riid, &IID_IMFTopologyNode) ||
            IsEqualIID(riid, &IID_IMFAttributes) ||
            IsEqualIID(riid, &IID_IUnknown))
    {
        *out = &node->IMFTopologyNode_iface;
        IMFTopologyNode_AddRef(iface);
        return S_OK;
    }

917
    WARN("Unsupported interface %s.\n", debugstr_guid(riid));
918 919 920 921 922 923 924 925 926 927
    *out = NULL;

    return E_NOINTERFACE;
}

static ULONG WINAPI topology_node_AddRef(IMFTopologyNode *iface)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
    ULONG refcount = InterlockedIncrement(&node->refcount);

928
    TRACE("%p, refcount %lu.\n", iface, refcount);
929 930 931 932 933 934 935 936

    return refcount;
}

static ULONG WINAPI topology_node_Release(IMFTopologyNode *iface)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
    ULONG refcount = InterlockedDecrement(&node->refcount);
937
    unsigned int i;
938

939
    TRACE("%p, refcount %lu.\n", iface, refcount);
940 941 942

    if (!refcount)
    {
943 944
        if (node->object)
            IUnknown_Release(node->object);
945 946
        if (node->input_type)
            IMFMediaType_Release(node->input_type);
947 948 949 950 951 952 953 954 955 956
        for (i = 0; i < node->inputs.count; ++i)
        {
            if (node->inputs.streams[i].preferred_type)
                IMFMediaType_Release(node->inputs.streams[i].preferred_type);
        }
        for (i = 0; i < node->outputs.count; ++i)
        {
            if (node->outputs.streams[i].preferred_type)
                IMFMediaType_Release(node->outputs.streams[i].preferred_type);
        }
957 958
        free(node->inputs.streams);
        free(node->outputs.streams);
959
        IMFAttributes_Release(node->attributes);
960
        DeleteCriticalSection(&node->cs);
961
        free(node);
962 963 964 965 966 967 968 969 970
    }

    return refcount;
}

static HRESULT WINAPI topology_node_GetItem(IMFTopologyNode *iface, REFGUID key, PROPVARIANT *value)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

971
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
972 973 974 975 976 977 978 979

    return IMFAttributes_GetItem(node->attributes, key, value);
}

static HRESULT WINAPI topology_node_GetItemType(IMFTopologyNode *iface, REFGUID key, MF_ATTRIBUTE_TYPE *type)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

980
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), type);
981 982 983 984 985 986 987 988

    return IMFAttributes_GetItemType(node->attributes, key, type);
}

static HRESULT WINAPI topology_node_CompareItem(IMFTopologyNode *iface, REFGUID key, REFPROPVARIANT value, BOOL *result)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

989
    TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), value, result);
990 991 992 993 994 995 996 997 998

    return IMFAttributes_CompareItem(node->attributes, key, value, result);
}

static HRESULT WINAPI topology_node_Compare(IMFTopologyNode *iface, IMFAttributes *theirs,
        MF_ATTRIBUTES_MATCH_TYPE type, BOOL *result)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

999
    TRACE("%p, %p, %d, %p.\n", iface, theirs, type, result);
1000 1001 1002 1003 1004 1005 1006 1007

    return IMFAttributes_Compare(node->attributes, theirs, type, result);
}

static HRESULT WINAPI topology_node_GetUINT32(IMFTopologyNode *iface, REFGUID key, UINT32 *value)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1008
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
1009 1010 1011 1012 1013 1014 1015 1016

    return IMFAttributes_GetUINT32(node->attributes, key, value);
}

static HRESULT WINAPI topology_node_GetUINT64(IMFTopologyNode *iface, REFGUID key, UINT64 *value)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1017
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
1018 1019 1020 1021 1022 1023 1024 1025

    return IMFAttributes_GetUINT64(node->attributes, key, value);
}

static HRESULT WINAPI topology_node_GetDouble(IMFTopologyNode *iface, REFGUID key, double *value)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1026
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
1027 1028 1029 1030 1031 1032 1033 1034

    return IMFAttributes_GetDouble(node->attributes, key, value);
}

static HRESULT WINAPI topology_node_GetGUID(IMFTopologyNode *iface, REFGUID key, GUID *value)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1035
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
1036 1037 1038 1039 1040 1041 1042 1043

    return IMFAttributes_GetGUID(node->attributes, key, value);
}

static HRESULT WINAPI topology_node_GetStringLength(IMFTopologyNode *iface, REFGUID key, UINT32 *length)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1044
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), length);
1045 1046 1047 1048 1049 1050 1051 1052 1053

    return IMFAttributes_GetStringLength(node->attributes, key, length);
}

static HRESULT WINAPI topology_node_GetString(IMFTopologyNode *iface, REFGUID key, WCHAR *value,
        UINT32 size, UINT32 *length)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1054
    TRACE("%p, %s, %p, %d, %p.\n", iface, debugstr_guid(key), value, size, length);
1055 1056 1057 1058 1059 1060 1061 1062 1063

    return IMFAttributes_GetString(node->attributes, key, value, size, length);
}

static HRESULT WINAPI topology_node_GetAllocatedString(IMFTopologyNode *iface, REFGUID key,
        WCHAR **value, UINT32 *length)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1064
    TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), value, length);
1065 1066 1067 1068 1069 1070 1071 1072

    return IMFAttributes_GetAllocatedString(node->attributes, key, value, length);
}

static HRESULT WINAPI topology_node_GetBlobSize(IMFTopologyNode *iface, REFGUID key, UINT32 *size)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1073
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), size);
1074 1075 1076 1077 1078 1079 1080 1081 1082

    return IMFAttributes_GetBlobSize(node->attributes, key, size);
}

static HRESULT WINAPI topology_node_GetBlob(IMFTopologyNode *iface, REFGUID key, UINT8 *buf,
        UINT32 bufsize, UINT32 *blobsize)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1083
    TRACE("%p, %s, %p, %d, %p.\n", iface, debugstr_guid(key), buf, bufsize, blobsize);
1084 1085 1086 1087 1088 1089 1090 1091

    return IMFAttributes_GetBlob(node->attributes, key, buf, bufsize, blobsize);
}

static HRESULT WINAPI topology_node_GetAllocatedBlob(IMFTopologyNode *iface, REFGUID key, UINT8 **buf, UINT32 *size)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1092
    TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), buf, size);
1093 1094 1095 1096 1097 1098 1099 1100

    return IMFAttributes_GetAllocatedBlob(node->attributes, key, buf, size);
}

static HRESULT WINAPI topology_node_GetUnknown(IMFTopologyNode *iface, REFGUID key, REFIID riid, void **ppv)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1101
    TRACE("%p, %s, %s, %p.\n", iface, debugstr_guid(key), debugstr_guid(riid), ppv);
1102 1103 1104 1105 1106 1107 1108 1109

    return IMFAttributes_GetUnknown(node->attributes, key, riid, ppv);
}

static HRESULT WINAPI topology_node_SetItem(IMFTopologyNode *iface, REFGUID key, REFPROPVARIANT value)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1110
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
1111 1112 1113 1114 1115 1116 1117 1118

    return IMFAttributes_SetItem(node->attributes, key, value);
}

static HRESULT WINAPI topology_node_DeleteItem(IMFTopologyNode *iface, REFGUID key)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1119
    TRACE("%p, %s.\n", iface, debugstr_guid(key));
1120 1121 1122 1123 1124 1125 1126 1127

    return IMFAttributes_DeleteItem(node->attributes, key);
}

static HRESULT WINAPI topology_node_DeleteAllItems(IMFTopologyNode *iface)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1128
    TRACE("%p.\n", iface);
1129 1130 1131 1132 1133 1134 1135 1136

    return IMFAttributes_DeleteAllItems(node->attributes);
}

static HRESULT WINAPI topology_node_SetUINT32(IMFTopologyNode *iface, REFGUID key, UINT32 value)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1137
    TRACE("%p, %s, %d.\n", iface, debugstr_guid(key), value);
1138 1139 1140 1141 1142 1143 1144 1145

    return IMFAttributes_SetUINT32(node->attributes, key, value);
}

static HRESULT WINAPI topology_node_SetUINT64(IMFTopologyNode *iface, REFGUID key, UINT64 value)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1146
    TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), wine_dbgstr_longlong(value));
1147 1148 1149 1150 1151 1152 1153 1154

    return IMFAttributes_SetUINT64(node->attributes, key, value);
}

static HRESULT WINAPI topology_node_SetDouble(IMFTopologyNode *iface, REFGUID key, double value)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1155
    TRACE("%p, %s, %f.\n", iface, debugstr_guid(key), value);
1156 1157 1158 1159 1160 1161 1162 1163

    return IMFAttributes_SetDouble(node->attributes, key, value);
}

static HRESULT WINAPI topology_node_SetGUID(IMFTopologyNode *iface, REFGUID key, REFGUID value)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1164
    TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), debugstr_guid(value));
1165 1166 1167 1168 1169 1170 1171 1172

    return IMFAttributes_SetGUID(node->attributes, key, value);
}

static HRESULT WINAPI topology_node_SetString(IMFTopologyNode *iface, REFGUID key, const WCHAR *value)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1173
    TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), debugstr_w(value));
1174 1175 1176 1177 1178 1179 1180 1181

    return IMFAttributes_SetString(node->attributes, key, value);
}

static HRESULT WINAPI topology_node_SetBlob(IMFTopologyNode *iface, REFGUID key, const UINT8 *buf, UINT32 size)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1182
    TRACE("%p, %s, %p, %d.\n", iface, debugstr_guid(key), buf, size);
1183 1184 1185 1186 1187 1188 1189 1190

    return IMFAttributes_SetBlob(node->attributes, key, buf, size);
}

static HRESULT WINAPI topology_node_SetUnknown(IMFTopologyNode *iface, REFGUID key, IUnknown *unknown)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1191
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), unknown);
1192 1193 1194 1195 1196 1197 1198 1199

    return IMFAttributes_SetUnknown(node->attributes, key, unknown);
}

static HRESULT WINAPI topology_node_LockStore(IMFTopologyNode *iface)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1200
    TRACE("%p.\n", iface);
1201 1202 1203 1204 1205 1206 1207 1208

    return IMFAttributes_LockStore(node->attributes);
}

static HRESULT WINAPI topology_node_UnlockStore(IMFTopologyNode *iface)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1209
    TRACE("%p.\n", iface);
1210 1211 1212 1213 1214 1215 1216 1217

    return IMFAttributes_UnlockStore(node->attributes);
}

static HRESULT WINAPI topology_node_GetCount(IMFTopologyNode *iface, UINT32 *count)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1218
    TRACE("%p, %p.\n", iface, count);
1219 1220 1221 1222 1223 1224 1225 1226

    return IMFAttributes_GetCount(node->attributes, count);
}

static HRESULT WINAPI topology_node_GetItemByIndex(IMFTopologyNode *iface, UINT32 index, GUID *key, PROPVARIANT *value)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1227
    TRACE("%p, %u, %p, %p.\n", iface, index, key, value);
1228 1229 1230 1231 1232 1233 1234 1235

    return IMFAttributes_GetItemByIndex(node->attributes, index, key, value);
}

static HRESULT WINAPI topology_node_CopyAllItems(IMFTopologyNode *iface, IMFAttributes *dest)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1236
    TRACE("%p, %p.\n", iface, dest);
1237 1238 1239 1240

    return IMFAttributes_CopyAllItems(node->attributes, dest);
}

1241
static HRESULT topology_node_set_object(struct topology_node *node, IUnknown *object)
1242
{
1243 1244 1245 1246 1247 1248
    static const GUID *iids[3] = { &IID_IPersist, &IID_IPersistStorage, &IID_IPersistPropertyBag };
    IPersist *persist = NULL;
    BOOL has_object_id;
    GUID object_id;
    unsigned int i;
    HRESULT hr;
1249

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
    has_object_id = IMFAttributes_GetGUID(node->attributes, &MF_TOPONODE_TRANSFORM_OBJECTID, &object_id) == S_OK;

    if (object && !has_object_id)
    {
        for (i = 0; i < ARRAY_SIZE(iids); ++i)
        {
            persist = NULL;
            if (SUCCEEDED(hr = IUnknown_QueryInterface(object, iids[i], (void **)&persist)))
                break;
        }

        if (persist)
        {
            if (FAILED(hr = IPersist_GetClassID(persist, &object_id)))
            {
                IPersist_Release(persist);
                persist = NULL;
            }
        }
    }

    EnterCriticalSection(&node->cs);

    if (node->object)
        IUnknown_Release(node->object);
    node->object = object;
    if (node->object)
        IUnknown_AddRef(node->object);

    if (persist)
        IMFAttributes_SetGUID(node->attributes, &MF_TOPONODE_TRANSFORM_OBJECTID, &object_id);

    LeaveCriticalSection(&node->cs);

    if (persist)
        IPersist_Release(persist);

    return S_OK;
1288 1289
}

1290 1291 1292 1293 1294 1295 1296 1297 1298
static HRESULT WINAPI topology_node_SetObject(IMFTopologyNode *iface, IUnknown *object)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

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

    return topology_node_set_object(node, object);
}

1299 1300
static HRESULT WINAPI topology_node_GetObject(IMFTopologyNode *iface, IUnknown **object)
{
1301
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
1302

1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
    TRACE("%p, %p.\n", iface, object);

    if (!object)
        return E_POINTER;

    EnterCriticalSection(&node->cs);

    *object = node->object;
    if (*object)
        IUnknown_AddRef(*object);

    LeaveCriticalSection(&node->cs);

    return *object ? S_OK : E_FAIL;
1317 1318 1319 1320 1321 1322
}

static HRESULT WINAPI topology_node_GetNodeType(IMFTopologyNode *iface, MF_TOPOLOGY_TYPE *node_type)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);

1323
    TRACE("%p, %p.\n", iface, node_type);
1324 1325 1326 1327 1328 1329 1330 1331

    *node_type = node->node_type;

    return S_OK;
}

static HRESULT WINAPI topology_node_GetTopoNodeID(IMFTopologyNode *iface, TOPOID *id)
{
1332
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
1333

1334
    TRACE("%p, %p.\n", iface, id);
1335 1336 1337 1338

    *id = node->id;

    return S_OK;
1339 1340 1341 1342
}

static HRESULT WINAPI topology_node_SetTopoNodeID(IMFTopologyNode *iface, TOPOID id)
{
1343
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
1344

1345
    TRACE("%p, %s.\n", iface, wine_dbgstr_longlong(id));
1346 1347 1348 1349

    node->id = id;

    return S_OK;
1350 1351 1352 1353
}

static HRESULT WINAPI topology_node_GetInputCount(IMFTopologyNode *iface, DWORD *count)
{
1354
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
1355

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

1358
    *count = node->inputs.count;
1359 1360

    return S_OK;
1361 1362 1363 1364
}

static HRESULT WINAPI topology_node_GetOutputCount(IMFTopologyNode *iface, DWORD *count)
{
1365
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
1366

1367 1368 1369 1370 1371
    TRACE("%p, %p.\n", iface, count);

    *count = node->outputs.count;

    return S_OK;
1372 1373
}

1374 1375 1376 1377 1378 1379 1380 1381 1382
static void topology_node_set_stream_type(struct node_stream *stream, IMFMediaType *mediatype)
{
    if (stream->preferred_type)
        IMFMediaType_Release(stream->preferred_type);
    stream->preferred_type = mediatype;
    if (stream->preferred_type)
        IMFMediaType_AddRef(stream->preferred_type);
}

1383 1384
static HRESULT topology_node_connect_output(struct topology_node *node, DWORD output_index,
        struct topology_node *connection, DWORD input_index)
1385
{
1386 1387
    struct node_stream *stream;
    HRESULT hr;
1388

1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
    if (node->node_type == MF_TOPOLOGY_OUTPUT_NODE || connection->node_type == MF_TOPOLOGY_SOURCESTREAM_NODE)
         return E_FAIL;

    EnterCriticalSection(&node->cs);
    EnterCriticalSection(&connection->cs);

    topology_node_disconnect_output(node, output_index);
    if (input_index < connection->inputs.count)
    {
        stream = &connection->inputs.streams[input_index];
        if (stream->connection)
            topology_node_disconnect_output(stream->connection, stream->connection_stream);
    }

    hr = topology_node_reserve_streams(&node->outputs, output_index);
    if (SUCCEEDED(hr))
1405 1406
    {
        size_t old_count = connection->inputs.count;
1407
        hr = topology_node_reserve_streams(&connection->inputs, input_index);
1408 1409 1410 1411 1412 1413 1414
        if (SUCCEEDED(hr) && !old_count && connection->input_type)
        {
            topology_node_set_stream_type(connection->inputs.streams, connection->input_type);
            IMFMediaType_Release(connection->input_type);
            connection->input_type = NULL;
        }
    }
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429

    if (SUCCEEDED(hr))
    {
        node->outputs.streams[output_index].connection = connection;
        IMFTopologyNode_AddRef(&node->outputs.streams[output_index].connection->IMFTopologyNode_iface);
        node->outputs.streams[output_index].connection_stream = input_index;
        connection->inputs.streams[input_index].connection = node;
        IMFTopologyNode_AddRef(&connection->inputs.streams[input_index].connection->IMFTopologyNode_iface);
        connection->inputs.streams[input_index].connection_stream = output_index;
    }

    LeaveCriticalSection(&connection->cs);
    LeaveCriticalSection(&node->cs);

    return hr;
1430 1431
}

1432 1433 1434 1435 1436 1437
static HRESULT WINAPI topology_node_ConnectOutput(IMFTopologyNode *iface, DWORD output_index,
        IMFTopologyNode *peer, DWORD input_index)
{
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
    struct topology_node *connection = unsafe_impl_from_IMFTopologyNode(peer);

1438
    TRACE("%p, %lu, %p, %lu.\n", iface, output_index, peer, input_index);
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448

    if (!connection)
    {
        WARN("External node implementations are not supported.\n");
        return E_UNEXPECTED;
    }

    return topology_node_connect_output(node, output_index, connection, input_index);
}

1449
static HRESULT WINAPI topology_node_DisconnectOutput(IMFTopologyNode *iface, DWORD output_index)
1450
{
1451
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
1452

1453
    TRACE("%p, %lu.\n", iface, output_index);
1454 1455

    return topology_node_disconnect_output(node, output_index);
1456 1457
}

1458
static HRESULT WINAPI topology_node_GetInput(IMFTopologyNode *iface, DWORD input_index, IMFTopologyNode **ret,
1459 1460
        DWORD *output_index)
{
1461 1462
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
    HRESULT hr = S_OK;
1463

1464
    TRACE("%p, %lu, %p, %p.\n", iface, input_index, ret, output_index);
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486

    EnterCriticalSection(&node->cs);

    if (input_index < node->inputs.count)
    {
        const struct node_stream *stream = &node->inputs.streams[input_index];

        if (stream->connection)
        {
            *ret = &stream->connection->IMFTopologyNode_iface;
            IMFTopologyNode_AddRef(*ret);
            *output_index = stream->connection_stream;
        }
        else
            hr = MF_E_NOT_FOUND;
    }
    else
        hr = E_INVALIDARG;

    LeaveCriticalSection(&node->cs);

    return hr;
1487 1488
}

1489
static HRESULT WINAPI topology_node_GetOutput(IMFTopologyNode *iface, DWORD output_index, IMFTopologyNode **ret,
1490 1491
        DWORD *input_index)
{
1492 1493
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
    HRESULT hr = S_OK;
1494

1495
    TRACE("%p, %lu, %p, %p.\n", iface, output_index, ret, input_index);
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517

    EnterCriticalSection(&node->cs);

    if (output_index < node->outputs.count)
    {
        const struct node_stream *stream = &node->outputs.streams[output_index];

        if (stream->connection)
        {
            *ret = &stream->connection->IMFTopologyNode_iface;
            IMFTopologyNode_AddRef(*ret);
            *input_index = stream->connection_stream;
        }
        else
            hr = MF_E_NOT_FOUND;
    }
    else
        hr = E_INVALIDARG;

    LeaveCriticalSection(&node->cs);

    return hr;
1518 1519
}

1520
static HRESULT WINAPI topology_node_SetOutputPrefType(IMFTopologyNode *iface, DWORD index, IMFMediaType *mediatype)
1521
{
1522 1523
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
    HRESULT hr = S_OK;
1524

1525
    TRACE("%p, %lu, %p.\n", iface, index, mediatype);
1526 1527 1528 1529 1530

    EnterCriticalSection(&node->cs);

    if (node->node_type != MF_TOPOLOGY_OUTPUT_NODE)
    {
1531
        if (SUCCEEDED(hr = topology_node_reserve_streams(&node->outputs, index)))
1532
            topology_node_set_stream_type(&node->outputs.streams[index], mediatype);
1533 1534 1535 1536 1537 1538 1539
    }
    else
        hr = E_NOTIMPL;

    LeaveCriticalSection(&node->cs);

    return hr;
1540 1541
}

1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
static HRESULT topology_node_get_pref_type(struct node_streams *streams, unsigned int index, IMFMediaType **mediatype)
{
    *mediatype = streams->streams[index].preferred_type;
    if (*mediatype)
    {
        IMFMediaType_AddRef(*mediatype);
        return S_OK;
    }

    return E_FAIL;
}

1554
static HRESULT WINAPI topology_node_GetOutputPrefType(IMFTopologyNode *iface, DWORD index, IMFMediaType **mediatype)
1555
{
1556 1557
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
    HRESULT hr = S_OK;
1558

1559
    TRACE("%p, %lu, %p.\n", iface, index, mediatype);
1560 1561 1562 1563

    EnterCriticalSection(&node->cs);

    if (index < node->outputs.count)
1564
        hr = topology_node_get_pref_type(&node->outputs, index, mediatype);
1565 1566 1567 1568 1569 1570
    else
        hr = E_INVALIDARG;

    LeaveCriticalSection(&node->cs);

    return hr;
1571 1572
}

1573
static HRESULT WINAPI topology_node_SetInputPrefType(IMFTopologyNode *iface, DWORD index, IMFMediaType *mediatype)
1574
{
1575 1576
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
    HRESULT hr = S_OK;
1577

1578
    TRACE("%p, %lu, %p.\n", iface, index, mediatype);
1579 1580 1581

    EnterCriticalSection(&node->cs);

1582
    switch (node->node_type)
1583
    {
1584 1585
        case MF_TOPOLOGY_TEE_NODE:
            if (index)
1586
            {
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
                hr = MF_E_INVALIDTYPE;
                break;
            }
            if (node->inputs.count)
                topology_node_set_stream_type(&node->inputs.streams[index], mediatype);
            else
            {
                if (node->input_type)
                    IMFMediaType_Release(node->input_type);
                node->input_type = mediatype;
                if (node->input_type)
                    IMFMediaType_AddRef(node->input_type);
            }
            break;
        case MF_TOPOLOGY_SOURCESTREAM_NODE:
            hr = E_NOTIMPL;
            break;
        default:
            if (SUCCEEDED(hr = topology_node_reserve_streams(&node->inputs, index)))
                topology_node_set_stream_type(&node->inputs.streams[index], mediatype);
1607 1608 1609 1610 1611
    }

    LeaveCriticalSection(&node->cs);

    return hr;
1612 1613
}

1614
static HRESULT WINAPI topology_node_GetInputPrefType(IMFTopologyNode *iface, DWORD index, IMFMediaType **mediatype)
1615
{
1616 1617
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
    HRESULT hr = S_OK;
1618

1619
    TRACE("%p, %lu, %p.\n", iface, index, mediatype);
1620 1621 1622 1623 1624

    EnterCriticalSection(&node->cs);

    if (index < node->inputs.count)
    {
1625 1626 1627 1628 1629 1630
        hr = topology_node_get_pref_type(&node->inputs, index, mediatype);
    }
    else if (node->node_type == MF_TOPOLOGY_TEE_NODE && node->input_type)
    {
        *mediatype = node->input_type;
        IMFMediaType_AddRef(*mediatype);
1631 1632 1633 1634 1635 1636 1637
    }
    else
        hr = E_INVALIDARG;

    LeaveCriticalSection(&node->cs);

    return hr;
1638 1639
}

1640
static HRESULT WINAPI topology_node_CloneFrom(IMFTopologyNode *iface, IMFTopologyNode *src_node)
1641
{
1642 1643
    struct topology_node *node = impl_from_IMFTopologyNode(iface);
    MF_TOPOLOGY_TYPE node_type;
1644
    IMFMediaType *mediatype;
1645
    IUnknown *object;
1646
    DWORD count, i;
1647 1648
    TOPOID topoid;
    HRESULT hr;
1649

1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
    TRACE("%p, %p.\n", iface, src_node);

    if (FAILED(hr = IMFTopologyNode_GetNodeType(src_node, &node_type)))
        return hr;

    if (node->node_type != node_type)
        return MF_E_INVALIDREQUEST;

    if (FAILED(hr = IMFTopologyNode_GetTopoNodeID(src_node, &topoid)))
        return hr;

    object = NULL;
    IMFTopologyNode_GetObject(src_node, &object);

    EnterCriticalSection(&node->cs);

    hr = IMFTopologyNode_CopyAllItems(src_node, node->attributes);

    if (SUCCEEDED(hr))
        hr = topology_node_set_object(node, object);

    if (SUCCEEDED(hr))
        node->id = topoid;

1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
    if (SUCCEEDED(IMFTopologyNode_GetInputCount(src_node, &count)))
    {
        for (i = 0; i < count; ++i)
        {
            if (SUCCEEDED(IMFTopologyNode_GetInputPrefType(src_node, i, &mediatype)))
            {
                IMFTopologyNode_SetInputPrefType(iface, i, mediatype);
                IMFMediaType_Release(mediatype);
            }
        }
    }

    if (SUCCEEDED(IMFTopologyNode_GetOutputCount(src_node, &count)))
    {
        for (i = 0; i < count; ++i)
        {
            if (SUCCEEDED(IMFTopologyNode_GetOutputPrefType(src_node, i, &mediatype)))
            {
                IMFTopologyNode_SetOutputPrefType(iface, i, mediatype);
                IMFMediaType_Release(mediatype);
            }
        }
    }

1698 1699 1700 1701 1702 1703
    LeaveCriticalSection(&node->cs);

    if (object)
        IUnknown_Release(object);

    return hr;
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
}

static const IMFTopologyNodeVtbl topologynodevtbl =
{
    topology_node_QueryInterface,
    topology_node_AddRef,
    topology_node_Release,
    topology_node_GetItem,
    topology_node_GetItemType,
    topology_node_CompareItem,
    topology_node_Compare,
    topology_node_GetUINT32,
    topology_node_GetUINT64,
    topology_node_GetDouble,
    topology_node_GetGUID,
    topology_node_GetStringLength,
    topology_node_GetString,
    topology_node_GetAllocatedString,
    topology_node_GetBlobSize,
    topology_node_GetBlob,
    topology_node_GetAllocatedBlob,
    topology_node_GetUnknown,
    topology_node_SetItem,
    topology_node_DeleteItem,
    topology_node_DeleteAllItems,
    topology_node_SetUINT32,
    topology_node_SetUINT64,
    topology_node_SetDouble,
    topology_node_SetGUID,
    topology_node_SetString,
    topology_node_SetBlob,
    topology_node_SetUnknown,
    topology_node_LockStore,
    topology_node_UnlockStore,
    topology_node_GetCount,
    topology_node_GetItemByIndex,
    topology_node_CopyAllItems,
    topology_node_SetObject,
    topology_node_GetObject,
    topology_node_GetNodeType,
    topology_node_GetTopoNodeID,
    topology_node_SetTopoNodeID,
    topology_node_GetInputCount,
    topology_node_GetOutputCount,
    topology_node_ConnectOutput,
    topology_node_DisconnectOutput,
    topology_node_GetInput,
    topology_node_GetOutput,
    topology_node_SetOutputPrefType,
    topology_node_GetOutputPrefType,
    topology_node_SetInputPrefType,
    topology_node_GetInputPrefType,
    topology_node_CloneFrom,
};

1759 1760 1761 1762
static HRESULT create_topology_node(MF_TOPOLOGY_TYPE node_type, struct topology_node **node)
{
    HRESULT hr;

1763
    if (!(*node = calloc(1, sizeof(**node))))
1764 1765 1766 1767 1768 1769 1770 1771
        return E_OUTOFMEMORY;

    (*node)->IMFTopologyNode_iface.lpVtbl = &topologynodevtbl;
    (*node)->refcount = 1;
    (*node)->node_type = node_type;
    hr = MFCreateAttributes(&(*node)->attributes, 0);
    if (FAILED(hr))
    {
1772
        free(*node);
1773 1774 1775 1776 1777 1778 1779 1780
        return hr;
    }
    (*node)->id = ((TOPOID)GetCurrentProcessId() << 32) | InterlockedIncrement(&next_node_id);
    InitializeCriticalSection(&(*node)->cs);

    return S_OK;
}

1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
HRESULT topology_node_get_object(IMFTopologyNode *node, REFIID riid, void **obj)
{
    IUnknown *unk;
    HRESULT hr;

    *obj = NULL;

    if (SUCCEEDED(hr = IMFTopologyNode_GetObject(node, &unk)))
    {
        hr = IUnknown_QueryInterface(unk, riid, obj);
        IUnknown_Release(unk);
    }

    return hr;
}

1797 1798 1799 1800 1801 1802 1803 1804
/***********************************************************************
 *      MFCreateTopologyNode (mf.@)
 */
HRESULT WINAPI MFCreateTopologyNode(MF_TOPOLOGY_TYPE node_type, IMFTopologyNode **node)
{
    struct topology_node *object;
    HRESULT hr;

1805
    TRACE("%d, %p.\n", node_type, node);
1806 1807 1808 1809

    if (!node)
        return E_POINTER;

1810 1811 1812
    hr = create_topology_node(node_type, &object);
    if (SUCCEEDED(hr))
        *node = &object->IMFTopologyNode_iface;
1813

1814
    return hr;
1815
}
1816

1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
/* private helper for node types without an actual IMFMediaTypeHandler */
struct type_handler
{
    IMFMediaTypeHandler IMFMediaTypeHandler_iface;
    LONG refcount;

    IMFTopologyNode *node;
    DWORD stream;
    BOOL output;

    IMFTransform *transform;
};

static struct type_handler *impl_from_IMFMediaTypeHandler(IMFMediaTypeHandler *iface)
{
    return CONTAINING_RECORD(iface, struct type_handler, IMFMediaTypeHandler_iface);
}

static HRESULT WINAPI type_handler_QueryInterface(IMFMediaTypeHandler *iface, REFIID riid, void **obj)
{
    if (IsEqualIID(riid, &IID_IMFMediaTypeHandler)
            || IsEqualIID(riid, &IID_IUnknown))
    {
        IMFMediaTypeHandler_AddRef((*obj = iface));
        return S_OK;
    }

    *obj = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI type_handler_AddRef(IMFMediaTypeHandler *iface)
{
    struct type_handler *handler = impl_from_IMFMediaTypeHandler(iface);
    ULONG refcount = InterlockedIncrement(&handler->refcount);
    return refcount;
}

static ULONG WINAPI type_handler_Release(IMFMediaTypeHandler *iface)
{
    struct type_handler *handler = impl_from_IMFMediaTypeHandler(iface);
    ULONG refcount = InterlockedDecrement(&handler->refcount);

    if (!refcount)
    {
        if (handler->transform)
            IMFTransform_Release(handler->transform);
        IMFTopologyNode_Release(handler->node);
        free(handler);
    }

    return refcount;
}

static HRESULT WINAPI type_handler_IsMediaTypeSupported(IMFMediaTypeHandler *iface, IMFMediaType *in_type,
        IMFMediaType **out_type)
{
    struct type_handler *handler = impl_from_IMFMediaTypeHandler(iface);
    IMFMediaType *type;
    DWORD flags;
    HRESULT hr;

    if (out_type)
        *out_type = NULL;

    if (handler->transform)
    {
        if (handler->output)
            return IMFTransform_SetOutputType(handler->transform, handler->stream, in_type, MFT_SET_TYPE_TEST_ONLY);
        else
            return IMFTransform_SetInputType(handler->transform, handler->stream, in_type, MFT_SET_TYPE_TEST_ONLY);
    }

    if (FAILED(hr = IMFMediaTypeHandler_GetCurrentMediaType(iface, &type)))
        return hr;

    hr = IMFMediaType_IsEqual(type, in_type, &flags);
    IMFMediaType_Release(type);
    return hr;
}

static HRESULT WINAPI type_handler_GetMediaTypeCount(IMFMediaTypeHandler *iface, DWORD *count)
{
    return E_NOTIMPL;
}

static HRESULT WINAPI type_handler_GetMediaTypeByIndex(IMFMediaTypeHandler *iface, DWORD index,
        IMFMediaType **type)
{
    struct type_handler *handler = impl_from_IMFMediaTypeHandler(iface);

    if (handler->transform)
    {
        if (handler->output)
            return IMFTransform_GetOutputAvailableType(handler->transform, handler->stream, index, type);
        else
            return IMFTransform_GetInputAvailableType(handler->transform, handler->stream, index, type);
    }

    if (index)
        return MF_E_NO_MORE_TYPES;

    return IMFMediaTypeHandler_GetCurrentMediaType(iface, type);
}

static HRESULT WINAPI type_handler_SetCurrentMediaType(IMFMediaTypeHandler *iface, IMFMediaType *type)
{
    struct type_handler *handler = impl_from_IMFMediaTypeHandler(iface);

    if (handler->transform)
    {
        if (handler->output)
            return IMFTransform_SetOutputType(handler->transform, handler->stream, type, 0);
        else
            return IMFTransform_SetInputType(handler->transform, handler->stream, type, 0);
    }

    return IMFTopologyNode_SetInputPrefType(handler->node, handler->stream, type);
}

static HRESULT WINAPI type_handler_GetCurrentMediaType(IMFMediaTypeHandler *iface, IMFMediaType **type)
{
    struct type_handler *handler = impl_from_IMFMediaTypeHandler(iface);
    UINT32 output;

    if (handler->transform)
    {
        if (handler->output)
            return IMFTransform_GetOutputCurrentType(handler->transform, handler->stream, type);
        else
            return IMFTransform_GetInputCurrentType(handler->transform, handler->stream, type);
    }

    if (SUCCEEDED(IMFTopologyNode_GetInputPrefType(handler->node, 0, type)))
        return S_OK;

    if (FAILED(IMFTopologyNode_GetUINT32(handler->node, &MF_TOPONODE_PRIMARYOUTPUT, &output)))
        output = 0;

    return IMFTopologyNode_GetOutputPrefType(handler->node, output, type);
}

static HRESULT WINAPI type_handler_GetMajorType(IMFMediaTypeHandler *iface, GUID *type)
{
    IMFMediaType *media_type;
    HRESULT hr;

    if (FAILED(hr = IMFMediaTypeHandler_GetCurrentMediaType(iface, &media_type)))
        return hr;

    hr = IMFMediaType_GetMajorType(media_type, type);
    IMFMediaType_Release(media_type);
    return hr;
}

static const IMFMediaTypeHandlerVtbl type_handler_vtbl =
{
    type_handler_QueryInterface,
    type_handler_AddRef,
    type_handler_Release,
    type_handler_IsMediaTypeSupported,
    type_handler_GetMediaTypeCount,
    type_handler_GetMediaTypeByIndex,
    type_handler_SetCurrentMediaType,
    type_handler_GetCurrentMediaType,
    type_handler_GetMajorType,
};

static HRESULT type_handler_create(IMFTopologyNode *node, DWORD stream, BOOL output, IMFTransform *transform, IMFMediaTypeHandler **out)
{
    struct type_handler *handler;

    if (!(handler = calloc(1, sizeof(*handler)))) return E_OUTOFMEMORY;
    handler->IMFMediaTypeHandler_iface.lpVtbl = &type_handler_vtbl;
    handler->refcount = 1;
    handler->stream = stream;
    handler->output = output;
    IMFTopologyNode_AddRef((handler->node = node));
    if (transform)
        IMFTransform_AddRef((handler->transform = transform));

    *out = &handler->IMFMediaTypeHandler_iface;
    return S_OK;
}

2002
HRESULT topology_node_get_type_handler(IMFTopologyNode *node, DWORD stream,
2003
        BOOL output, IMFMediaTypeHandler **handler)
2004 2005 2006 2007
{
    MF_TOPOLOGY_TYPE node_type;
    IMFStreamSink *stream_sink;
    IMFStreamDescriptor *sd;
2008
    IMFTransform *transform;
2009 2010 2011 2012 2013 2014 2015 2016
    HRESULT hr;

    if (FAILED(hr = IMFTopologyNode_GetNodeType(node, &node_type)))
        return hr;

    switch (node_type)
    {
        case MF_TOPOLOGY_OUTPUT_NODE:
2017 2018 2019
            if (output || stream)
                return MF_E_INVALIDSTREAMNUMBER;

2020 2021 2022 2023 2024 2025 2026
            if (SUCCEEDED(hr = topology_node_get_object(node, &IID_IMFStreamSink, (void **)&stream_sink)))
            {
                hr = IMFStreamSink_GetMediaTypeHandler(stream_sink, handler);
                IMFStreamSink_Release(stream_sink);
            }
            break;
        case MF_TOPOLOGY_SOURCESTREAM_NODE:
2027 2028 2029
            if (!output || stream)
                return MF_E_INVALIDSTREAMNUMBER;

2030 2031 2032 2033 2034 2035 2036
            if (SUCCEEDED(hr = IMFTopologyNode_GetUnknown(node, &MF_TOPONODE_STREAM_DESCRIPTOR,
                    &IID_IMFStreamDescriptor, (void **)&sd)))
            {
                hr = IMFStreamDescriptor_GetMediaTypeHandler(sd, handler);
                IMFStreamDescriptor_Release(sd);
            }
            break;
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
        case MF_TOPOLOGY_TRANSFORM_NODE:
            if (SUCCEEDED(hr = topology_node_get_object(node, &IID_IMFTransform, (void **)&transform)))
            {
                hr = type_handler_create(node, stream, output, transform, handler);
                IMFTransform_Release(transform);
            }
            break;
        case MF_TOPOLOGY_TEE_NODE:
            hr = type_handler_create(node, stream, output, NULL, handler);
            break;
2047 2048 2049 2050 2051 2052 2053 2054
        default:
            WARN("Unexpected node type %u.\n", node_type);
            return MF_E_UNEXPECTED;
    }

    return hr;
}

2055 2056 2057 2058 2059
/***********************************************************************
 *      MFGetTopoNodeCurrentType (mf.@)
 */
HRESULT WINAPI MFGetTopoNodeCurrentType(IMFTopologyNode *node, DWORD stream, BOOL output, IMFMediaType **type)
{
2060
    IMFMediaTypeHandler *handler;
2061 2062
    HRESULT hr;

2063
    TRACE("%p, %lu, %d, %p.\n", node, stream, output, type);
2064

2065
    if (FAILED(hr = topology_node_get_type_handler(node, stream, output, &handler)))
2066
        return hr;
2067

2068 2069
    hr = IMFMediaTypeHandler_GetCurrentMediaType(handler, type);
    IMFMediaTypeHandler_Release(handler);
2070 2071 2072
    return hr;
}

2073 2074 2075 2076
static HRESULT WINAPI seq_source_QueryInterface(IMFSequencerSource *iface, REFIID riid, void **out)
{
    struct seq_source *seq_source = impl_from_IMFSequencerSource(iface);

2077 2078 2079
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);

    *out = NULL;
2080 2081 2082 2083 2084

    if (IsEqualIID(riid, &IID_IMFSequencerSource) ||
            IsEqualIID(riid, &IID_IUnknown))
    {
        *out = &seq_source->IMFSequencerSource_iface;
2085 2086 2087 2088 2089 2090 2091 2092 2093
    }
    else if (IsEqualIID(riid, &IID_IMFMediaSourceTopologyProvider))
    {
        *out = &seq_source->IMFMediaSourceTopologyProvider_iface;
    }
    else
    {
        WARN("Unimplemented %s.\n", debugstr_guid(riid));
        return E_NOINTERFACE;
2094 2095
    }

2096 2097
    if (*out)
        IUnknown_AddRef((IUnknown *)*out);
2098

2099
    return S_OK;
2100 2101 2102 2103 2104 2105 2106
}

static ULONG WINAPI seq_source_AddRef(IMFSequencerSource *iface)
{
    struct seq_source *seq_source = impl_from_IMFSequencerSource(iface);
    ULONG refcount = InterlockedIncrement(&seq_source->refcount);

2107
    TRACE("%p, refcount %lu.\n", iface, refcount);
2108 2109 2110 2111 2112 2113 2114 2115 2116

    return refcount;
}

static ULONG WINAPI seq_source_Release(IMFSequencerSource *iface)
{
    struct seq_source *seq_source = impl_from_IMFSequencerSource(iface);
    ULONG refcount = InterlockedDecrement(&seq_source->refcount);

2117
    TRACE("%p, refcount %lu.\n", iface, refcount);
2118 2119

    if (!refcount)
2120
        free(seq_source);
2121 2122 2123 2124 2125 2126 2127

    return refcount;
}

static HRESULT WINAPI seq_source_AppendTopology(IMFSequencerSource *iface, IMFTopology *topology,
        DWORD flags, MFSequencerElementId *id)
{
2128
    FIXME("%p, %p, %lx, %p.\n", iface, topology, flags, id);
2129 2130 2131 2132 2133 2134

    return E_NOTIMPL;
}

static HRESULT WINAPI seq_source_DeleteTopology(IMFSequencerSource *iface, MFSequencerElementId id)
{
2135
    FIXME("%p, %#lx.\n", iface, id);
2136 2137 2138 2139 2140 2141 2142

    return E_NOTIMPL;
}

static HRESULT WINAPI seq_source_GetPresentationContext(IMFSequencerSource *iface,
        IMFPresentationDescriptor *descriptor, MFSequencerElementId *id, IMFTopology **topology)
{
2143
    FIXME("%p, %p, %p, %p.\n", iface, descriptor, id, topology);
2144 2145 2146 2147 2148 2149 2150

    return E_NOTIMPL;
}

static HRESULT WINAPI seq_source_UpdateTopology(IMFSequencerSource *iface, MFSequencerElementId id,
        IMFTopology *topology)
{
2151
    FIXME("%p, %#lx, %p.\n", iface, id, topology);
2152 2153 2154 2155 2156 2157

    return E_NOTIMPL;
}

static HRESULT WINAPI seq_source_UpdateTopologyFlags(IMFSequencerSource *iface, MFSequencerElementId id, DWORD flags)
{
2158
    FIXME("%p, %#lx, %#lx.\n", iface, id, flags);
2159 2160 2161 2162

    return E_NOTIMPL;
}

2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197
static HRESULT WINAPI seq_source_topology_provider_QueryInterface(IMFMediaSourceTopologyProvider *iface, REFIID riid,
        void **obj)
{
    struct seq_source *seq_source = impl_from_IMFMediaSourceTopologyProvider(iface);
    return IMFSequencerSource_QueryInterface(&seq_source->IMFSequencerSource_iface, riid, obj);
}

static ULONG WINAPI seq_source_topology_provider_AddRef(IMFMediaSourceTopologyProvider *iface)
{
    struct seq_source *seq_source = impl_from_IMFMediaSourceTopologyProvider(iface);
    return IMFSequencerSource_AddRef(&seq_source->IMFSequencerSource_iface);
}

static ULONG WINAPI seq_source_topology_provider_Release(IMFMediaSourceTopologyProvider *iface)
{
    struct seq_source *seq_source = impl_from_IMFMediaSourceTopologyProvider(iface);
    return IMFSequencerSource_Release(&seq_source->IMFSequencerSource_iface);
}

static HRESULT WINAPI seq_source_topology_provider_GetMediaSourceTopology(IMFMediaSourceTopologyProvider *iface,
        IMFPresentationDescriptor *pd, IMFTopology **topology)
{
    FIXME("%p, %p, %p.\n", iface, pd, topology);

    return E_NOTIMPL;
}

static const IMFMediaSourceTopologyProviderVtbl seq_source_topology_provider_vtbl =
{
    seq_source_topology_provider_QueryInterface,
    seq_source_topology_provider_AddRef,
    seq_source_topology_provider_Release,
    seq_source_topology_provider_GetMediaSourceTopology,
};

2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
static const IMFSequencerSourceVtbl seqsourcevtbl =
{
    seq_source_QueryInterface,
    seq_source_AddRef,
    seq_source_Release,
    seq_source_AppendTopology,
    seq_source_DeleteTopology,
    seq_source_GetPresentationContext,
    seq_source_UpdateTopology,
    seq_source_UpdateTopologyFlags,
};

/***********************************************************************
 *      MFCreateSequencerSource (mf.@)
 */
HRESULT WINAPI MFCreateSequencerSource(IUnknown *reserved, IMFSequencerSource **seq_source)
{
    struct seq_source *object;

2217
    TRACE("%p, %p.\n", reserved, seq_source);
2218 2219 2220 2221

    if (!seq_source)
        return E_POINTER;

2222
    if (!(object = calloc(1, sizeof(*object))))
2223 2224 2225
        return E_OUTOFMEMORY;

    object->IMFSequencerSource_iface.lpVtbl = &seqsourcevtbl;
2226
    object->IMFMediaSourceTopologyProvider_iface.lpVtbl = &seq_source_topology_provider_vtbl;
2227 2228 2229 2230 2231 2232
    object->refcount = 1;

    *seq_source = &object->IMFSequencerSource_iface;

    return S_OK;
}
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308

struct segment_offset
{
    IUnknown IUnknown_iface;
    LONG refcount;
    MFSequencerElementId id;
    MFTIME timeoffset;
};

static inline struct segment_offset *impl_offset_from_IUnknown(IUnknown *iface)
{
    return CONTAINING_RECORD(iface, struct segment_offset, IUnknown_iface);
}

static HRESULT WINAPI segment_offset_QueryInterface(IUnknown *iface, REFIID riid, void **obj)
{
    if (IsEqualIID(riid, &IID_IUnknown))
    {
        *obj = iface;
        IUnknown_AddRef(iface);
        return S_OK;
    }

    *obj = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI segment_offset_AddRef(IUnknown *iface)
{
    struct segment_offset *offset = impl_offset_from_IUnknown(iface);
    return InterlockedIncrement(&offset->refcount);
}

static ULONG WINAPI segment_offset_Release(IUnknown *iface)
{
    struct segment_offset *offset = impl_offset_from_IUnknown(iface);
    ULONG refcount = InterlockedDecrement(&offset->refcount);

    if (!refcount)
        free(offset);

    return refcount;
}

static const IUnknownVtbl segment_offset_vtbl =
{
    segment_offset_QueryInterface,
    segment_offset_AddRef,
    segment_offset_Release,
};

/***********************************************************************
 *      MFCreateSequencerSegmentOffset (mf.@)
 */
HRESULT WINAPI MFCreateSequencerSegmentOffset(MFSequencerElementId id, MFTIME timeoffset, PROPVARIANT *ret)
{
    struct segment_offset *offset;

    TRACE("%#lx, %s, %p.\n", id, debugstr_time(timeoffset), ret);

    if (!ret)
        return E_POINTER;

    if (!(offset = calloc(1, sizeof(*offset))))
        return E_OUTOFMEMORY;

    offset->IUnknown_iface.lpVtbl = &segment_offset_vtbl;
    offset->refcount = 1;
    offset->id = id;
    offset->timeoffset = timeoffset;

    V_VT(ret) = VT_UNKNOWN;
    V_UNKNOWN(ret) = &offset->IUnknown_iface;

    return S_OK;
}