mf/topology: Copy input topology attributes on Load().
[wine/zf.git] / dlls / mf / topology.c
blobbbed2cba8938cce4bc0ffa1b2b6c60e638c4da25
1 /*
2 * Copyright 2017 Nikolay Sivov
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
27 #undef INITGUID
28 #include <guiddef.h>
29 #include "mfidl.h"
31 #include "wine/debug.h"
33 #include "mf_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(mfplat);
37 static LONG next_node_id;
38 static TOPOID next_topology_id;
40 struct node_stream
42 IMFMediaType *preferred_type;
43 struct topology_node *connection;
44 DWORD connection_stream;
47 struct node_streams
49 struct node_stream *streams;
50 size_t size;
51 size_t count;
54 struct topology_node
56 IMFTopologyNode IMFTopologyNode_iface;
57 LONG refcount;
58 IMFAttributes *attributes;
59 MF_TOPOLOGY_TYPE node_type;
60 TOPOID id;
61 IUnknown *object;
62 IMFMediaType *input_type; /* Only for tee nodes. */
63 struct node_streams inputs;
64 struct node_streams outputs;
65 CRITICAL_SECTION cs;
68 struct topology
70 IMFTopology IMFTopology_iface;
71 LONG refcount;
72 IMFAttributes *attributes;
73 struct
75 struct topology_node **nodes;
76 size_t size;
77 size_t count;
78 } nodes;
79 TOPOID id;
82 struct topology_loader
84 IMFTopoLoader IMFTopoLoader_iface;
85 LONG refcount;
88 struct seq_source
90 IMFSequencerSource IMFSequencerSource_iface;
91 IMFMediaSourceTopologyProvider IMFMediaSourceTopologyProvider_iface;
92 LONG refcount;
95 static inline struct topology *impl_from_IMFTopology(IMFTopology *iface)
97 return CONTAINING_RECORD(iface, struct topology, IMFTopology_iface);
100 static struct topology_node *impl_from_IMFTopologyNode(IMFTopologyNode *iface)
102 return CONTAINING_RECORD(iface, struct topology_node, IMFTopologyNode_iface);
105 static const IMFTopologyNodeVtbl topologynodevtbl;
107 static struct topology_node *unsafe_impl_from_IMFTopologyNode(IMFTopologyNode *iface)
109 if (!iface || iface->lpVtbl != &topologynodevtbl)
110 return NULL;
111 return impl_from_IMFTopologyNode(iface);
114 static HRESULT create_topology_node(MF_TOPOLOGY_TYPE node_type, struct topology_node **node);
115 static HRESULT topology_node_connect_output(struct topology_node *node, DWORD output_index,
116 struct topology_node *connection, DWORD input_index);
118 static struct topology *unsafe_impl_from_IMFTopology(IMFTopology *iface);
120 static struct topology_loader *impl_from_IMFTopoLoader(IMFTopoLoader *iface)
122 return CONTAINING_RECORD(iface, struct topology_loader, IMFTopoLoader_iface);
125 static struct seq_source *impl_from_IMFSequencerSource(IMFSequencerSource *iface)
127 return CONTAINING_RECORD(iface, struct seq_source, IMFSequencerSource_iface);
130 static struct seq_source *impl_from_IMFMediaSourceTopologyProvider(IMFMediaSourceTopologyProvider *iface)
132 return CONTAINING_RECORD(iface, struct seq_source, IMFMediaSourceTopologyProvider_iface);
135 static HRESULT topology_node_reserve_streams(struct node_streams *streams, DWORD index)
137 if (!mf_array_reserve((void **)&streams->streams, &streams->size, index + 1, sizeof(*streams->streams)))
138 return E_OUTOFMEMORY;
140 if (index >= streams->count)
142 memset(&streams->streams[streams->count], 0, (index - streams->count + 1) * sizeof(*streams->streams));
143 streams->count = index + 1;
146 return S_OK;
149 static HRESULT WINAPI topology_QueryInterface(IMFTopology *iface, REFIID riid, void **out)
151 struct topology *topology = impl_from_IMFTopology(iface);
153 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
155 if (IsEqualIID(riid, &IID_IMFTopology) ||
156 IsEqualIID(riid, &IID_IMFAttributes) ||
157 IsEqualIID(riid, &IID_IUnknown))
159 *out = &topology->IMFTopology_iface;
161 else
163 FIXME("(%s, %p)\n", debugstr_guid(riid), out);
164 *out = NULL;
165 return E_NOINTERFACE;
168 IUnknown_AddRef((IUnknown*)*out);
169 return S_OK;
172 static ULONG WINAPI topology_AddRef(IMFTopology *iface)
174 struct topology *topology = impl_from_IMFTopology(iface);
175 ULONG refcount = InterlockedIncrement(&topology->refcount);
177 TRACE("%p, refcount %u.\n", iface, refcount);
179 return refcount;
182 static HRESULT topology_node_disconnect_output(struct topology_node *node, DWORD output_index)
184 struct topology_node *connection = NULL;
185 struct node_stream *stream;
186 DWORD connection_stream;
187 HRESULT hr = S_OK;
189 EnterCriticalSection(&node->cs);
191 if (output_index < node->outputs.count)
193 stream = &node->outputs.streams[output_index];
195 if (stream->connection)
197 connection = stream->connection;
198 connection_stream = stream->connection_stream;
199 stream->connection = NULL;
200 stream->connection_stream = 0;
202 else
203 hr = MF_E_NOT_FOUND;
205 else
206 hr = E_INVALIDARG;
208 LeaveCriticalSection(&node->cs);
210 if (connection)
212 EnterCriticalSection(&connection->cs);
214 if (connection_stream < connection->inputs.count)
216 stream = &connection->inputs.streams[connection_stream];
218 if (stream->connection)
220 stream->connection = NULL;
221 stream->connection_stream = 0;
225 LeaveCriticalSection(&connection->cs);
227 IMFTopologyNode_Release(&connection->IMFTopologyNode_iface);
228 IMFTopologyNode_Release(&node->IMFTopologyNode_iface);
231 return hr;
234 static void topology_node_disconnect(struct topology_node *node)
236 struct node_stream *stream;
237 size_t i;
239 for (i = 0; i < node->outputs.count; ++i)
240 topology_node_disconnect_output(node, i);
242 for (i = 0; i < node->inputs.count; ++i)
244 stream = &node->inputs.streams[i];
245 if (stream->connection)
246 topology_node_disconnect_output(stream->connection, stream->connection_stream);
250 static void topology_clear(struct topology *topology)
252 size_t i;
254 for (i = 0; i < topology->nodes.count; ++i)
256 topology_node_disconnect(topology->nodes.nodes[i]);
257 IMFTopologyNode_Release(&topology->nodes.nodes[i]->IMFTopologyNode_iface);
259 heap_free(topology->nodes.nodes);
260 topology->nodes.nodes = NULL;
261 topology->nodes.count = 0;
262 topology->nodes.size = 0;
265 static ULONG WINAPI topology_Release(IMFTopology *iface)
267 struct topology *topology = impl_from_IMFTopology(iface);
268 ULONG refcount = InterlockedDecrement(&topology->refcount);
270 TRACE("%p, refcount %u.\n", iface, refcount);
272 if (!refcount)
274 if (topology->attributes)
275 IMFAttributes_Release(topology->attributes);
276 topology_clear(topology);
277 heap_free(topology);
280 return refcount;
283 static HRESULT WINAPI topology_GetItem(IMFTopology *iface, REFGUID key, PROPVARIANT *value)
285 struct topology *topology = impl_from_IMFTopology(iface);
287 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
289 return IMFAttributes_GetItem(topology->attributes, key, value);
292 static HRESULT WINAPI topology_GetItemType(IMFTopology *iface, REFGUID key, MF_ATTRIBUTE_TYPE *type)
294 struct topology *topology = impl_from_IMFTopology(iface);
296 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), type);
298 return IMFAttributes_GetItemType(topology->attributes, key, type);
301 static HRESULT WINAPI topology_CompareItem(IMFTopology *iface, REFGUID key, REFPROPVARIANT value, BOOL *result)
303 struct topology *topology = impl_from_IMFTopology(iface);
305 TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), value, result);
307 return IMFAttributes_CompareItem(topology->attributes, key, value, result);
310 static HRESULT WINAPI topology_Compare(IMFTopology *iface, IMFAttributes *theirs, MF_ATTRIBUTES_MATCH_TYPE type,
311 BOOL *result)
313 struct topology *topology = impl_from_IMFTopology(iface);
315 TRACE("%p, %p, %d, %p.\n", iface, theirs, type, result);
317 return IMFAttributes_Compare(topology->attributes, theirs, type, result);
320 static HRESULT WINAPI topology_GetUINT32(IMFTopology *iface, REFGUID key, UINT32 *value)
322 struct topology *topology = impl_from_IMFTopology(iface);
324 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
326 return IMFAttributes_GetUINT32(topology->attributes, key, value);
329 static HRESULT WINAPI topology_GetUINT64(IMFTopology *iface, REFGUID key, UINT64 *value)
331 struct topology *topology = impl_from_IMFTopology(iface);
333 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
335 return IMFAttributes_GetUINT64(topology->attributes, key, value);
338 static HRESULT WINAPI topology_GetDouble(IMFTopology *iface, REFGUID key, double *value)
340 struct topology *topology = impl_from_IMFTopology(iface);
342 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
344 return IMFAttributes_GetDouble(topology->attributes, key, value);
347 static HRESULT WINAPI topology_GetGUID(IMFTopology *iface, REFGUID key, GUID *value)
349 struct topology *topology = impl_from_IMFTopology(iface);
351 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
353 return IMFAttributes_GetGUID(topology->attributes, key, value);
356 static HRESULT WINAPI topology_GetStringLength(IMFTopology *iface, REFGUID key, UINT32 *length)
358 struct topology *topology = impl_from_IMFTopology(iface);
360 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), length);
362 return IMFAttributes_GetStringLength(topology->attributes, key, length);
365 static HRESULT WINAPI topology_GetString(IMFTopology *iface, REFGUID key, WCHAR *value,
366 UINT32 size, UINT32 *length)
368 struct topology *topology = impl_from_IMFTopology(iface);
370 TRACE("%p, %s, %p, %d, %p.\n", iface, debugstr_guid(key), value, size, length);
372 return IMFAttributes_GetString(topology->attributes, key, value, size, length);
375 static HRESULT WINAPI topology_GetAllocatedString(IMFTopology *iface, REFGUID key,
376 WCHAR **value, UINT32 *length)
378 struct topology *topology = impl_from_IMFTopology(iface);
380 TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), value, length);
382 return IMFAttributes_GetAllocatedString(topology->attributes, key, value, length);
385 static HRESULT WINAPI topology_GetBlobSize(IMFTopology *iface, REFGUID key, UINT32 *size)
387 struct topology *topology = impl_from_IMFTopology(iface);
389 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), size);
391 return IMFAttributes_GetBlobSize(topology->attributes, key, size);
394 static HRESULT WINAPI topology_GetBlob(IMFTopology *iface, REFGUID key, UINT8 *buf,
395 UINT32 bufsize, UINT32 *blobsize)
397 struct topology *topology = impl_from_IMFTopology(iface);
399 TRACE("%p, %s, %p, %d, %p.\n", iface, debugstr_guid(key), buf, bufsize, blobsize);
401 return IMFAttributes_GetBlob(topology->attributes, key, buf, bufsize, blobsize);
404 static HRESULT WINAPI topology_GetAllocatedBlob(IMFTopology *iface, REFGUID key, UINT8 **buf, UINT32 *size)
406 struct topology *topology = impl_from_IMFTopology(iface);
408 TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), buf, size);
410 return IMFAttributes_GetAllocatedBlob(topology->attributes, key, buf, size);
413 static HRESULT WINAPI topology_GetUnknown(IMFTopology *iface, REFGUID key, REFIID riid, void **ppv)
415 struct topology *topology = impl_from_IMFTopology(iface);
417 TRACE("%p, %s, %s, %p.\n", iface, debugstr_guid(key), debugstr_guid(riid), ppv);
419 return IMFAttributes_GetUnknown(topology->attributes, key, riid, ppv);
422 static HRESULT WINAPI topology_SetItem(IMFTopology *iface, REFGUID key, REFPROPVARIANT value)
424 struct topology *topology = impl_from_IMFTopology(iface);
426 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
428 return IMFAttributes_SetItem(topology->attributes, key, value);
431 static HRESULT WINAPI topology_DeleteItem(IMFTopology *iface, REFGUID key)
433 struct topology *topology = impl_from_IMFTopology(iface);
435 TRACE("%p, %s.\n", topology, debugstr_guid(key));
437 return IMFAttributes_DeleteItem(topology->attributes, key);
440 static HRESULT WINAPI topology_DeleteAllItems(IMFTopology *iface)
442 struct topology *topology = impl_from_IMFTopology(iface);
444 TRACE("%p.\n", iface);
446 return IMFAttributes_DeleteAllItems(topology->attributes);
449 static HRESULT WINAPI topology_SetUINT32(IMFTopology *iface, REFGUID key, UINT32 value)
451 struct topology *topology = impl_from_IMFTopology(iface);
453 TRACE("%p, %s, %d.\n", iface, debugstr_guid(key), value);
455 return IMFAttributes_SetUINT32(topology->attributes, key, value);
458 static HRESULT WINAPI topology_SetUINT64(IMFTopology *iface, REFGUID key, UINT64 value)
460 struct topology *topology = impl_from_IMFTopology(iface);
462 TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), wine_dbgstr_longlong(value));
464 return IMFAttributes_SetUINT64(topology->attributes, key, value);
467 static HRESULT WINAPI topology_SetDouble(IMFTopology *iface, REFGUID key, double value)
469 struct topology *topology = impl_from_IMFTopology(iface);
471 TRACE("%p, %s, %f.\n", iface, debugstr_guid(key), value);
473 return IMFAttributes_SetDouble(topology->attributes, key, value);
476 static HRESULT WINAPI topology_SetGUID(IMFTopology *iface, REFGUID key, REFGUID value)
478 struct topology *topology = impl_from_IMFTopology(iface);
480 TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), debugstr_guid(value));
482 return IMFAttributes_SetGUID(topology->attributes, key, value);
485 static HRESULT WINAPI topology_SetString(IMFTopology *iface, REFGUID key, const WCHAR *value)
487 struct topology *topology = impl_from_IMFTopology(iface);
489 TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), debugstr_w(value));
491 return IMFAttributes_SetString(topology->attributes, key, value);
494 static HRESULT WINAPI topology_SetBlob(IMFTopology *iface, REFGUID key, const UINT8 *buf, UINT32 size)
496 struct topology *topology = impl_from_IMFTopology(iface);
498 TRACE("%p, %s, %p, %d.\n", iface, debugstr_guid(key), buf, size);
500 return IMFAttributes_SetBlob(topology->attributes, key, buf, size);
503 static HRESULT WINAPI topology_SetUnknown(IMFTopology *iface, REFGUID key, IUnknown *unknown)
505 struct topology *topology = impl_from_IMFTopology(iface);
507 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), unknown);
509 return IMFAttributes_SetUnknown(topology->attributes, key, unknown);
512 static HRESULT WINAPI topology_LockStore(IMFTopology *iface)
514 struct topology *topology = impl_from_IMFTopology(iface);
516 TRACE("%p.\n", iface);
518 return IMFAttributes_LockStore(topology->attributes);
521 static HRESULT WINAPI topology_UnlockStore(IMFTopology *iface)
523 struct topology *topology = impl_from_IMFTopology(iface);
525 TRACE("%p.\n", iface);
527 return IMFAttributes_UnlockStore(topology->attributes);
530 static HRESULT WINAPI topology_GetCount(IMFTopology *iface, UINT32 *count)
532 struct topology *topology = impl_from_IMFTopology(iface);
534 TRACE("%p, %p.\n", iface, count);
536 return IMFAttributes_GetCount(topology->attributes, count);
539 static HRESULT WINAPI topology_GetItemByIndex(IMFTopology *iface, UINT32 index, GUID *key, PROPVARIANT *value)
541 struct topology *topology = impl_from_IMFTopology(iface);
543 TRACE("%p, %u, %p, %p.\n", iface, index, key, value);
545 return IMFAttributes_GetItemByIndex(topology->attributes, index, key, value);
548 static HRESULT WINAPI topology_CopyAllItems(IMFTopology *iface, IMFAttributes *dest)
550 struct topology *topology = impl_from_IMFTopology(iface);
552 TRACE("%p, %p.\n", iface, dest);
554 return IMFAttributes_CopyAllItems(topology->attributes, dest);
557 static HRESULT WINAPI topology_GetTopologyID(IMFTopology *iface, TOPOID *id)
559 struct topology *topology = impl_from_IMFTopology(iface);
561 TRACE("%p, %p.\n", iface, id);
563 if (!id)
564 return E_POINTER;
566 *id = topology->id;
568 return S_OK;
571 static HRESULT topology_get_node_by_id(const struct topology *topology, TOPOID id, struct topology_node **node)
573 size_t i = 0;
575 for (i = 0; i < topology->nodes.count; ++i)
577 if (topology->nodes.nodes[i]->id == id)
579 *node = topology->nodes.nodes[i];
580 return S_OK;
584 return MF_E_NOT_FOUND;
587 static HRESULT topology_add_node(struct topology *topology, struct topology_node *node)
589 struct topology_node *match;
591 if (!node)
592 return E_POINTER;
594 if (SUCCEEDED(topology_get_node_by_id(topology, node->id, &match)))
595 return E_INVALIDARG;
597 if (!mf_array_reserve((void **)&topology->nodes.nodes, &topology->nodes.size, topology->nodes.count + 1,
598 sizeof(*topology->nodes.nodes)))
600 return E_OUTOFMEMORY;
603 topology->nodes.nodes[topology->nodes.count++] = node;
604 IMFTopologyNode_AddRef(&node->IMFTopologyNode_iface);
606 return S_OK;
609 static HRESULT WINAPI topology_AddNode(IMFTopology *iface, IMFTopologyNode *node_iface)
611 struct topology *topology = impl_from_IMFTopology(iface);
612 struct topology_node *node = unsafe_impl_from_IMFTopologyNode(node_iface);
614 TRACE("%p, %p.\n", iface, node_iface);
616 return topology_add_node(topology, node);
619 static HRESULT WINAPI topology_RemoveNode(IMFTopology *iface, IMFTopologyNode *node)
621 struct topology *topology = impl_from_IMFTopology(iface);
622 size_t i, count;
624 TRACE("%p, %p.\n", iface, node);
626 for (i = 0; i < topology->nodes.count; ++i)
628 if (&topology->nodes.nodes[i]->IMFTopologyNode_iface == node)
630 topology_node_disconnect(topology->nodes.nodes[i]);
631 IMFTopologyNode_Release(&topology->nodes.nodes[i]->IMFTopologyNode_iface);
632 count = topology->nodes.count - i - 1;
633 if (count)
635 memmove(&topology->nodes.nodes[i], &topology->nodes.nodes[i + 1],
636 count * sizeof(*topology->nodes.nodes));
638 topology->nodes.count--;
639 return S_OK;
643 return E_INVALIDARG;
646 static HRESULT WINAPI topology_GetNodeCount(IMFTopology *iface, WORD *count)
648 struct topology *topology = impl_from_IMFTopology(iface);
650 TRACE("%p, %p.\n", iface, count);
652 if (!count)
653 return E_POINTER;
655 *count = topology->nodes.count;
657 return S_OK;
660 static HRESULT WINAPI topology_GetNode(IMFTopology *iface, WORD index, IMFTopologyNode **node)
662 struct topology *topology = impl_from_IMFTopology(iface);
664 TRACE("%p, %u, %p.\n", iface, index, node);
666 if (!node)
667 return E_POINTER;
669 if (index >= topology->nodes.count)
670 return MF_E_INVALIDINDEX;
672 *node = &topology->nodes.nodes[index]->IMFTopologyNode_iface;
673 IMFTopologyNode_AddRef(*node);
675 return S_OK;
678 static HRESULT WINAPI topology_Clear(IMFTopology *iface)
680 struct topology *topology = impl_from_IMFTopology(iface);
682 TRACE("%p.\n", iface);
684 topology_clear(topology);
685 return S_OK;
688 static HRESULT WINAPI topology_CloneFrom(IMFTopology *iface, IMFTopology *src)
690 struct topology *topology = impl_from_IMFTopology(iface);
691 struct topology *src_topology = unsafe_impl_from_IMFTopology(src);
692 struct topology_node *node;
693 size_t i, j;
694 HRESULT hr;
696 TRACE("%p, %p.\n", iface, src);
698 topology_clear(topology);
700 /* Clone nodes. */
701 for (i = 0; i < src_topology->nodes.count; ++i)
703 if (FAILED(hr = create_topology_node(src_topology->nodes.nodes[i]->node_type, &node)))
705 WARN("Failed to create a node, hr %#x.\n", hr);
706 break;
709 if (SUCCEEDED(hr = IMFTopologyNode_CloneFrom(&node->IMFTopologyNode_iface,
710 &src_topology->nodes.nodes[i]->IMFTopologyNode_iface)))
712 topology_add_node(topology, node);
715 IMFTopologyNode_Release(&node->IMFTopologyNode_iface);
718 /* Clone connections. */
719 for (i = 0; i < src_topology->nodes.count; ++i)
721 const struct node_streams *outputs = &src_topology->nodes.nodes[i]->outputs;
723 for (j = 0; j < outputs->count; ++j)
725 DWORD input_index = outputs->streams[j].connection_stream;
726 TOPOID id = outputs->streams[j].connection->id;
728 /* Skip node lookup in destination topology, assuming same node order. */
729 if (SUCCEEDED(hr = topology_get_node_by_id(topology, id, &node)))
730 topology_node_connect_output(topology->nodes.nodes[i], j, node, input_index);
734 /* Copy attributes and id. */
735 hr = IMFTopology_CopyAllItems(src, (IMFAttributes *)&topology->IMFTopology_iface);
736 if (SUCCEEDED(hr))
737 topology->id = src_topology->id;
739 return S_OK;
742 static HRESULT WINAPI topology_GetNodeByID(IMFTopology *iface, TOPOID id, IMFTopologyNode **ret)
744 struct topology *topology = impl_from_IMFTopology(iface);
745 struct topology_node *node;
746 HRESULT hr;
748 TRACE("%p, %p.\n", iface, ret);
750 if (SUCCEEDED(hr = topology_get_node_by_id(topology, id, &node)))
752 *ret = &node->IMFTopologyNode_iface;
753 IMFTopologyNode_AddRef(*ret);
755 else
756 *ret = NULL;
758 return hr;
761 static HRESULT topology_get_node_collection(const struct topology *topology, MF_TOPOLOGY_TYPE node_type,
762 IMFCollection **collection)
764 HRESULT hr;
765 size_t i;
767 if (!collection)
768 return E_POINTER;
770 if (FAILED(hr = MFCreateCollection(collection)))
771 return hr;
773 for (i = 0; i < topology->nodes.count; ++i)
775 if (topology->nodes.nodes[i]->node_type == node_type)
777 if (FAILED(hr = IMFCollection_AddElement(*collection,
778 (IUnknown *)&topology->nodes.nodes[i]->IMFTopologyNode_iface)))
780 IMFCollection_Release(*collection);
781 *collection = NULL;
782 break;
787 return hr;
790 static HRESULT WINAPI topology_GetSourceNodeCollection(IMFTopology *iface, IMFCollection **collection)
792 struct topology *topology = impl_from_IMFTopology(iface);
794 TRACE("%p, %p.\n", iface, collection);
796 return topology_get_node_collection(topology, MF_TOPOLOGY_SOURCESTREAM_NODE, collection);
799 static HRESULT WINAPI topology_GetOutputNodeCollection(IMFTopology *iface, IMFCollection **collection)
801 struct topology *topology = impl_from_IMFTopology(iface);
803 TRACE("%p, %p.\n", iface, collection);
805 return topology_get_node_collection(topology, MF_TOPOLOGY_OUTPUT_NODE, collection);
808 static const IMFTopologyVtbl topologyvtbl =
810 topology_QueryInterface,
811 topology_AddRef,
812 topology_Release,
813 topology_GetItem,
814 topology_GetItemType,
815 topology_CompareItem,
816 topology_Compare,
817 topology_GetUINT32,
818 topology_GetUINT64,
819 topology_GetDouble,
820 topology_GetGUID,
821 topology_GetStringLength,
822 topology_GetString,
823 topology_GetAllocatedString,
824 topology_GetBlobSize,
825 topology_GetBlob,
826 topology_GetAllocatedBlob,
827 topology_GetUnknown,
828 topology_SetItem,
829 topology_DeleteItem,
830 topology_DeleteAllItems,
831 topology_SetUINT32,
832 topology_SetUINT64,
833 topology_SetDouble,
834 topology_SetGUID,
835 topology_SetString,
836 topology_SetBlob,
837 topology_SetUnknown,
838 topology_LockStore,
839 topology_UnlockStore,
840 topology_GetCount,
841 topology_GetItemByIndex,
842 topology_CopyAllItems,
843 topology_GetTopologyID,
844 topology_AddNode,
845 topology_RemoveNode,
846 topology_GetNodeCount,
847 topology_GetNode,
848 topology_Clear,
849 topology_CloneFrom,
850 topology_GetNodeByID,
851 topology_GetSourceNodeCollection,
852 topology_GetOutputNodeCollection,
855 static struct topology *unsafe_impl_from_IMFTopology(IMFTopology *iface)
857 if (!iface || iface->lpVtbl != &topologyvtbl)
858 return NULL;
859 return impl_from_IMFTopology(iface);
862 static TOPOID topology_generate_id(void)
864 TOPOID old;
868 old = next_topology_id;
870 while (InterlockedCompareExchange64((LONG64 *)&next_topology_id, old + 1, old) != old);
872 return next_topology_id;
875 /***********************************************************************
876 * MFCreateTopology (mf.@)
878 HRESULT WINAPI MFCreateTopology(IMFTopology **topology)
880 struct topology *object;
881 HRESULT hr;
883 TRACE("%p.\n", topology);
885 if (!topology)
886 return E_POINTER;
888 object = heap_alloc_zero(sizeof(*object));
889 if (!object)
890 return E_OUTOFMEMORY;
892 object->IMFTopology_iface.lpVtbl = &topologyvtbl;
893 object->refcount = 1;
895 hr = MFCreateAttributes(&object->attributes, 0);
896 if (FAILED(hr))
898 IMFTopology_Release(&object->IMFTopology_iface);
899 return hr;
902 object->id = topology_generate_id();
904 *topology = &object->IMFTopology_iface;
906 return S_OK;
909 static HRESULT WINAPI topology_node_QueryInterface(IMFTopologyNode *iface, REFIID riid, void **out)
911 struct topology_node *node = impl_from_IMFTopologyNode(iface);
913 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
915 if (IsEqualIID(riid, &IID_IMFTopologyNode) ||
916 IsEqualIID(riid, &IID_IMFAttributes) ||
917 IsEqualIID(riid, &IID_IUnknown))
919 *out = &node->IMFTopologyNode_iface;
920 IMFTopologyNode_AddRef(iface);
921 return S_OK;
924 WARN("Unsupported interface %s.\n", debugstr_guid(riid));
925 *out = NULL;
927 return E_NOINTERFACE;
930 static ULONG WINAPI topology_node_AddRef(IMFTopologyNode *iface)
932 struct topology_node *node = impl_from_IMFTopologyNode(iface);
933 ULONG refcount = InterlockedIncrement(&node->refcount);
935 TRACE("%p, refcount %u.\n", iface, refcount);
937 return refcount;
940 static ULONG WINAPI topology_node_Release(IMFTopologyNode *iface)
942 struct topology_node *node = impl_from_IMFTopologyNode(iface);
943 ULONG refcount = InterlockedDecrement(&node->refcount);
944 unsigned int i;
946 TRACE("%p, refcount %u.\n", iface, refcount);
948 if (!refcount)
950 if (node->object)
951 IUnknown_Release(node->object);
952 if (node->input_type)
953 IMFMediaType_Release(node->input_type);
954 for (i = 0; i < node->inputs.count; ++i)
956 if (node->inputs.streams[i].preferred_type)
957 IMFMediaType_Release(node->inputs.streams[i].preferred_type);
959 for (i = 0; i < node->outputs.count; ++i)
961 if (node->outputs.streams[i].preferred_type)
962 IMFMediaType_Release(node->outputs.streams[i].preferred_type);
964 heap_free(node->inputs.streams);
965 heap_free(node->outputs.streams);
966 IMFAttributes_Release(node->attributes);
967 DeleteCriticalSection(&node->cs);
968 heap_free(node);
971 return refcount;
974 static HRESULT WINAPI topology_node_GetItem(IMFTopologyNode *iface, REFGUID key, PROPVARIANT *value)
976 struct topology_node *node = impl_from_IMFTopologyNode(iface);
978 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
980 return IMFAttributes_GetItem(node->attributes, key, value);
983 static HRESULT WINAPI topology_node_GetItemType(IMFTopologyNode *iface, REFGUID key, MF_ATTRIBUTE_TYPE *type)
985 struct topology_node *node = impl_from_IMFTopologyNode(iface);
987 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), type);
989 return IMFAttributes_GetItemType(node->attributes, key, type);
992 static HRESULT WINAPI topology_node_CompareItem(IMFTopologyNode *iface, REFGUID key, REFPROPVARIANT value, BOOL *result)
994 struct topology_node *node = impl_from_IMFTopologyNode(iface);
996 TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), value, result);
998 return IMFAttributes_CompareItem(node->attributes, key, value, result);
1001 static HRESULT WINAPI topology_node_Compare(IMFTopologyNode *iface, IMFAttributes *theirs,
1002 MF_ATTRIBUTES_MATCH_TYPE type, BOOL *result)
1004 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1006 TRACE("%p, %p, %d, %p.\n", iface, theirs, type, result);
1008 return IMFAttributes_Compare(node->attributes, theirs, type, result);
1011 static HRESULT WINAPI topology_node_GetUINT32(IMFTopologyNode *iface, REFGUID key, UINT32 *value)
1013 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1015 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
1017 return IMFAttributes_GetUINT32(node->attributes, key, value);
1020 static HRESULT WINAPI topology_node_GetUINT64(IMFTopologyNode *iface, REFGUID key, UINT64 *value)
1022 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1024 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
1026 return IMFAttributes_GetUINT64(node->attributes, key, value);
1029 static HRESULT WINAPI topology_node_GetDouble(IMFTopologyNode *iface, REFGUID key, double *value)
1031 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1033 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
1035 return IMFAttributes_GetDouble(node->attributes, key, value);
1038 static HRESULT WINAPI topology_node_GetGUID(IMFTopologyNode *iface, REFGUID key, GUID *value)
1040 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1042 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
1044 return IMFAttributes_GetGUID(node->attributes, key, value);
1047 static HRESULT WINAPI topology_node_GetStringLength(IMFTopologyNode *iface, REFGUID key, UINT32 *length)
1049 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1051 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), length);
1053 return IMFAttributes_GetStringLength(node->attributes, key, length);
1056 static HRESULT WINAPI topology_node_GetString(IMFTopologyNode *iface, REFGUID key, WCHAR *value,
1057 UINT32 size, UINT32 *length)
1059 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1061 TRACE("%p, %s, %p, %d, %p.\n", iface, debugstr_guid(key), value, size, length);
1063 return IMFAttributes_GetString(node->attributes, key, value, size, length);
1066 static HRESULT WINAPI topology_node_GetAllocatedString(IMFTopologyNode *iface, REFGUID key,
1067 WCHAR **value, UINT32 *length)
1069 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1071 TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), value, length);
1073 return IMFAttributes_GetAllocatedString(node->attributes, key, value, length);
1076 static HRESULT WINAPI topology_node_GetBlobSize(IMFTopologyNode *iface, REFGUID key, UINT32 *size)
1078 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1080 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), size);
1082 return IMFAttributes_GetBlobSize(node->attributes, key, size);
1085 static HRESULT WINAPI topology_node_GetBlob(IMFTopologyNode *iface, REFGUID key, UINT8 *buf,
1086 UINT32 bufsize, UINT32 *blobsize)
1088 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1090 TRACE("%p, %s, %p, %d, %p.\n", iface, debugstr_guid(key), buf, bufsize, blobsize);
1092 return IMFAttributes_GetBlob(node->attributes, key, buf, bufsize, blobsize);
1095 static HRESULT WINAPI topology_node_GetAllocatedBlob(IMFTopologyNode *iface, REFGUID key, UINT8 **buf, UINT32 *size)
1097 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1099 TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), buf, size);
1101 return IMFAttributes_GetAllocatedBlob(node->attributes, key, buf, size);
1104 static HRESULT WINAPI topology_node_GetUnknown(IMFTopologyNode *iface, REFGUID key, REFIID riid, void **ppv)
1106 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1108 TRACE("%p, %s, %s, %p.\n", iface, debugstr_guid(key), debugstr_guid(riid), ppv);
1110 return IMFAttributes_GetUnknown(node->attributes, key, riid, ppv);
1113 static HRESULT WINAPI topology_node_SetItem(IMFTopologyNode *iface, REFGUID key, REFPROPVARIANT value)
1115 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1117 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);
1119 return IMFAttributes_SetItem(node->attributes, key, value);
1122 static HRESULT WINAPI topology_node_DeleteItem(IMFTopologyNode *iface, REFGUID key)
1124 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1126 TRACE("%p, %s.\n", iface, debugstr_guid(key));
1128 return IMFAttributes_DeleteItem(node->attributes, key);
1131 static HRESULT WINAPI topology_node_DeleteAllItems(IMFTopologyNode *iface)
1133 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1135 TRACE("%p.\n", iface);
1137 return IMFAttributes_DeleteAllItems(node->attributes);
1140 static HRESULT WINAPI topology_node_SetUINT32(IMFTopologyNode *iface, REFGUID key, UINT32 value)
1142 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1144 TRACE("%p, %s, %d.\n", iface, debugstr_guid(key), value);
1146 return IMFAttributes_SetUINT32(node->attributes, key, value);
1149 static HRESULT WINAPI topology_node_SetUINT64(IMFTopologyNode *iface, REFGUID key, UINT64 value)
1151 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1153 TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), wine_dbgstr_longlong(value));
1155 return IMFAttributes_SetUINT64(node->attributes, key, value);
1158 static HRESULT WINAPI topology_node_SetDouble(IMFTopologyNode *iface, REFGUID key, double value)
1160 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1162 TRACE("%p, %s, %f.\n", iface, debugstr_guid(key), value);
1164 return IMFAttributes_SetDouble(node->attributes, key, value);
1167 static HRESULT WINAPI topology_node_SetGUID(IMFTopologyNode *iface, REFGUID key, REFGUID value)
1169 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1171 TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), debugstr_guid(value));
1173 return IMFAttributes_SetGUID(node->attributes, key, value);
1176 static HRESULT WINAPI topology_node_SetString(IMFTopologyNode *iface, REFGUID key, const WCHAR *value)
1178 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1180 TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), debugstr_w(value));
1182 return IMFAttributes_SetString(node->attributes, key, value);
1185 static HRESULT WINAPI topology_node_SetBlob(IMFTopologyNode *iface, REFGUID key, const UINT8 *buf, UINT32 size)
1187 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1189 TRACE("%p, %s, %p, %d.\n", iface, debugstr_guid(key), buf, size);
1191 return IMFAttributes_SetBlob(node->attributes, key, buf, size);
1194 static HRESULT WINAPI topology_node_SetUnknown(IMFTopologyNode *iface, REFGUID key, IUnknown *unknown)
1196 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1198 TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), unknown);
1200 return IMFAttributes_SetUnknown(node->attributes, key, unknown);
1203 static HRESULT WINAPI topology_node_LockStore(IMFTopologyNode *iface)
1205 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1207 TRACE("%p.\n", iface);
1209 return IMFAttributes_LockStore(node->attributes);
1212 static HRESULT WINAPI topology_node_UnlockStore(IMFTopologyNode *iface)
1214 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1216 TRACE("%p.\n", iface);
1218 return IMFAttributes_UnlockStore(node->attributes);
1221 static HRESULT WINAPI topology_node_GetCount(IMFTopologyNode *iface, UINT32 *count)
1223 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1225 TRACE("%p, %p.\n", iface, count);
1227 return IMFAttributes_GetCount(node->attributes, count);
1230 static HRESULT WINAPI topology_node_GetItemByIndex(IMFTopologyNode *iface, UINT32 index, GUID *key, PROPVARIANT *value)
1232 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1234 TRACE("%p, %u, %p, %p.\n", iface, index, key, value);
1236 return IMFAttributes_GetItemByIndex(node->attributes, index, key, value);
1239 static HRESULT WINAPI topology_node_CopyAllItems(IMFTopologyNode *iface, IMFAttributes *dest)
1241 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1243 TRACE("%p, %p.\n", iface, dest);
1245 return IMFAttributes_CopyAllItems(node->attributes, dest);
1248 static HRESULT topology_node_set_object(struct topology_node *node, IUnknown *object)
1250 static const GUID *iids[3] = { &IID_IPersist, &IID_IPersistStorage, &IID_IPersistPropertyBag };
1251 IPersist *persist = NULL;
1252 BOOL has_object_id;
1253 GUID object_id;
1254 unsigned int i;
1255 HRESULT hr;
1257 has_object_id = IMFAttributes_GetGUID(node->attributes, &MF_TOPONODE_TRANSFORM_OBJECTID, &object_id) == S_OK;
1259 if (object && !has_object_id)
1261 for (i = 0; i < ARRAY_SIZE(iids); ++i)
1263 persist = NULL;
1264 if (SUCCEEDED(hr = IUnknown_QueryInterface(object, iids[i], (void **)&persist)))
1265 break;
1268 if (persist)
1270 if (FAILED(hr = IPersist_GetClassID(persist, &object_id)))
1272 IPersist_Release(persist);
1273 persist = NULL;
1278 EnterCriticalSection(&node->cs);
1280 if (node->object)
1281 IUnknown_Release(node->object);
1282 node->object = object;
1283 if (node->object)
1284 IUnknown_AddRef(node->object);
1286 if (persist)
1287 IMFAttributes_SetGUID(node->attributes, &MF_TOPONODE_TRANSFORM_OBJECTID, &object_id);
1289 LeaveCriticalSection(&node->cs);
1291 if (persist)
1292 IPersist_Release(persist);
1294 return S_OK;
1297 static HRESULT WINAPI topology_node_SetObject(IMFTopologyNode *iface, IUnknown *object)
1299 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1301 TRACE("%p, %p.\n", iface, object);
1303 return topology_node_set_object(node, object);
1306 static HRESULT WINAPI topology_node_GetObject(IMFTopologyNode *iface, IUnknown **object)
1308 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1310 TRACE("%p, %p.\n", iface, object);
1312 if (!object)
1313 return E_POINTER;
1315 EnterCriticalSection(&node->cs);
1317 *object = node->object;
1318 if (*object)
1319 IUnknown_AddRef(*object);
1321 LeaveCriticalSection(&node->cs);
1323 return *object ? S_OK : E_FAIL;
1326 static HRESULT WINAPI topology_node_GetNodeType(IMFTopologyNode *iface, MF_TOPOLOGY_TYPE *node_type)
1328 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1330 TRACE("%p, %p.\n", iface, node_type);
1332 *node_type = node->node_type;
1334 return S_OK;
1337 static HRESULT WINAPI topology_node_GetTopoNodeID(IMFTopologyNode *iface, TOPOID *id)
1339 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1341 TRACE("%p, %p.\n", iface, id);
1343 *id = node->id;
1345 return S_OK;
1348 static HRESULT WINAPI topology_node_SetTopoNodeID(IMFTopologyNode *iface, TOPOID id)
1350 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1352 TRACE("%p, %s.\n", iface, wine_dbgstr_longlong(id));
1354 node->id = id;
1356 return S_OK;
1359 static HRESULT WINAPI topology_node_GetInputCount(IMFTopologyNode *iface, DWORD *count)
1361 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1363 TRACE("%p, %p.\n", iface, count);
1365 *count = node->inputs.count;
1367 return S_OK;
1370 static HRESULT WINAPI topology_node_GetOutputCount(IMFTopologyNode *iface, DWORD *count)
1372 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1374 TRACE("%p, %p.\n", iface, count);
1376 *count = node->outputs.count;
1378 return S_OK;
1381 static void topology_node_set_stream_type(struct node_stream *stream, IMFMediaType *mediatype)
1383 if (stream->preferred_type)
1384 IMFMediaType_Release(stream->preferred_type);
1385 stream->preferred_type = mediatype;
1386 if (stream->preferred_type)
1387 IMFMediaType_AddRef(stream->preferred_type);
1390 static HRESULT topology_node_connect_output(struct topology_node *node, DWORD output_index,
1391 struct topology_node *connection, DWORD input_index)
1393 struct node_stream *stream;
1394 HRESULT hr;
1396 if (node->node_type == MF_TOPOLOGY_OUTPUT_NODE || connection->node_type == MF_TOPOLOGY_SOURCESTREAM_NODE)
1397 return E_FAIL;
1399 EnterCriticalSection(&node->cs);
1400 EnterCriticalSection(&connection->cs);
1402 topology_node_disconnect_output(node, output_index);
1403 if (input_index < connection->inputs.count)
1405 stream = &connection->inputs.streams[input_index];
1406 if (stream->connection)
1407 topology_node_disconnect_output(stream->connection, stream->connection_stream);
1410 hr = topology_node_reserve_streams(&node->outputs, output_index);
1411 if (SUCCEEDED(hr))
1413 size_t old_count = connection->inputs.count;
1414 hr = topology_node_reserve_streams(&connection->inputs, input_index);
1415 if (SUCCEEDED(hr) && !old_count && connection->input_type)
1417 topology_node_set_stream_type(connection->inputs.streams, connection->input_type);
1418 IMFMediaType_Release(connection->input_type);
1419 connection->input_type = NULL;
1423 if (SUCCEEDED(hr))
1425 node->outputs.streams[output_index].connection = connection;
1426 IMFTopologyNode_AddRef(&node->outputs.streams[output_index].connection->IMFTopologyNode_iface);
1427 node->outputs.streams[output_index].connection_stream = input_index;
1428 connection->inputs.streams[input_index].connection = node;
1429 IMFTopologyNode_AddRef(&connection->inputs.streams[input_index].connection->IMFTopologyNode_iface);
1430 connection->inputs.streams[input_index].connection_stream = output_index;
1433 LeaveCriticalSection(&connection->cs);
1434 LeaveCriticalSection(&node->cs);
1436 return hr;
1439 static HRESULT WINAPI topology_node_ConnectOutput(IMFTopologyNode *iface, DWORD output_index,
1440 IMFTopologyNode *peer, DWORD input_index)
1442 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1443 struct topology_node *connection = unsafe_impl_from_IMFTopologyNode(peer);
1445 TRACE("%p, %u, %p, %u.\n", iface, output_index, peer, input_index);
1447 if (!connection)
1449 WARN("External node implementations are not supported.\n");
1450 return E_UNEXPECTED;
1453 return topology_node_connect_output(node, output_index, connection, input_index);
1456 static HRESULT WINAPI topology_node_DisconnectOutput(IMFTopologyNode *iface, DWORD output_index)
1458 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1460 TRACE("%p, %u.\n", iface, output_index);
1462 return topology_node_disconnect_output(node, output_index);
1465 static HRESULT WINAPI topology_node_GetInput(IMFTopologyNode *iface, DWORD input_index, IMFTopologyNode **ret,
1466 DWORD *output_index)
1468 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1469 HRESULT hr = S_OK;
1471 TRACE("%p, %u, %p, %p.\n", iface, input_index, ret, output_index);
1473 EnterCriticalSection(&node->cs);
1475 if (input_index < node->inputs.count)
1477 const struct node_stream *stream = &node->inputs.streams[input_index];
1479 if (stream->connection)
1481 *ret = &stream->connection->IMFTopologyNode_iface;
1482 IMFTopologyNode_AddRef(*ret);
1483 *output_index = stream->connection_stream;
1485 else
1486 hr = MF_E_NOT_FOUND;
1488 else
1489 hr = E_INVALIDARG;
1491 LeaveCriticalSection(&node->cs);
1493 return hr;
1496 static HRESULT WINAPI topology_node_GetOutput(IMFTopologyNode *iface, DWORD output_index, IMFTopologyNode **ret,
1497 DWORD *input_index)
1499 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1500 HRESULT hr = S_OK;
1502 TRACE("%p, %u, %p, %p.\n", iface, output_index, ret, input_index);
1504 EnterCriticalSection(&node->cs);
1506 if (output_index < node->outputs.count)
1508 const struct node_stream *stream = &node->outputs.streams[output_index];
1510 if (stream->connection)
1512 *ret = &stream->connection->IMFTopologyNode_iface;
1513 IMFTopologyNode_AddRef(*ret);
1514 *input_index = stream->connection_stream;
1516 else
1517 hr = MF_E_NOT_FOUND;
1519 else
1520 hr = E_INVALIDARG;
1522 LeaveCriticalSection(&node->cs);
1524 return hr;
1527 static HRESULT WINAPI topology_node_SetOutputPrefType(IMFTopologyNode *iface, DWORD index, IMFMediaType *mediatype)
1529 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1530 HRESULT hr = S_OK;
1532 TRACE("%p, %u, %p.\n", iface, index, mediatype);
1534 EnterCriticalSection(&node->cs);
1536 if (node->node_type != MF_TOPOLOGY_OUTPUT_NODE)
1538 if (SUCCEEDED(hr = topology_node_reserve_streams(&node->outputs, index)))
1539 topology_node_set_stream_type(&node->outputs.streams[index], mediatype);
1541 else
1542 hr = E_NOTIMPL;
1544 LeaveCriticalSection(&node->cs);
1546 return hr;
1549 static HRESULT topology_node_get_pref_type(struct node_streams *streams, unsigned int index, IMFMediaType **mediatype)
1551 *mediatype = streams->streams[index].preferred_type;
1552 if (*mediatype)
1554 IMFMediaType_AddRef(*mediatype);
1555 return S_OK;
1558 return E_FAIL;
1561 static HRESULT WINAPI topology_node_GetOutputPrefType(IMFTopologyNode *iface, DWORD index, IMFMediaType **mediatype)
1563 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1564 HRESULT hr = S_OK;
1566 TRACE("%p, %u, %p.\n", iface, index, mediatype);
1568 EnterCriticalSection(&node->cs);
1570 if (index < node->outputs.count)
1571 hr = topology_node_get_pref_type(&node->outputs, index, mediatype);
1572 else
1573 hr = E_INVALIDARG;
1575 LeaveCriticalSection(&node->cs);
1577 return hr;
1580 static HRESULT WINAPI topology_node_SetInputPrefType(IMFTopologyNode *iface, DWORD index, IMFMediaType *mediatype)
1582 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1583 HRESULT hr = S_OK;
1585 TRACE("%p, %u, %p.\n", iface, index, mediatype);
1587 EnterCriticalSection(&node->cs);
1589 switch (node->node_type)
1591 case MF_TOPOLOGY_TEE_NODE:
1592 if (index)
1594 hr = MF_E_INVALIDTYPE;
1595 break;
1597 if (node->inputs.count)
1598 topology_node_set_stream_type(&node->inputs.streams[index], mediatype);
1599 else
1601 if (node->input_type)
1602 IMFMediaType_Release(node->input_type);
1603 node->input_type = mediatype;
1604 if (node->input_type)
1605 IMFMediaType_AddRef(node->input_type);
1607 break;
1608 case MF_TOPOLOGY_SOURCESTREAM_NODE:
1609 hr = E_NOTIMPL;
1610 break;
1611 default:
1612 if (SUCCEEDED(hr = topology_node_reserve_streams(&node->inputs, index)))
1613 topology_node_set_stream_type(&node->inputs.streams[index], mediatype);
1616 LeaveCriticalSection(&node->cs);
1618 return hr;
1621 static HRESULT WINAPI topology_node_GetInputPrefType(IMFTopologyNode *iface, DWORD index, IMFMediaType **mediatype)
1623 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1624 HRESULT hr = S_OK;
1626 TRACE("%p, %u, %p.\n", iface, index, mediatype);
1628 EnterCriticalSection(&node->cs);
1630 if (index < node->inputs.count)
1632 hr = topology_node_get_pref_type(&node->inputs, index, mediatype);
1634 else if (node->node_type == MF_TOPOLOGY_TEE_NODE && node->input_type)
1636 *mediatype = node->input_type;
1637 IMFMediaType_AddRef(*mediatype);
1639 else
1640 hr = E_INVALIDARG;
1642 LeaveCriticalSection(&node->cs);
1644 return hr;
1647 static HRESULT WINAPI topology_node_CloneFrom(IMFTopologyNode *iface, IMFTopologyNode *src_node)
1649 struct topology_node *node = impl_from_IMFTopologyNode(iface);
1650 MF_TOPOLOGY_TYPE node_type;
1651 IMFMediaType *mediatype;
1652 IUnknown *object;
1653 DWORD count, i;
1654 TOPOID topoid;
1655 HRESULT hr;
1657 TRACE("%p, %p.\n", iface, src_node);
1659 if (FAILED(hr = IMFTopologyNode_GetNodeType(src_node, &node_type)))
1660 return hr;
1662 if (node->node_type != node_type)
1663 return MF_E_INVALIDREQUEST;
1665 if (FAILED(hr = IMFTopologyNode_GetTopoNodeID(src_node, &topoid)))
1666 return hr;
1668 object = NULL;
1669 IMFTopologyNode_GetObject(src_node, &object);
1671 EnterCriticalSection(&node->cs);
1673 hr = IMFTopologyNode_CopyAllItems(src_node, node->attributes);
1675 if (SUCCEEDED(hr))
1676 hr = topology_node_set_object(node, object);
1678 if (SUCCEEDED(hr))
1679 node->id = topoid;
1681 if (SUCCEEDED(IMFTopologyNode_GetInputCount(src_node, &count)))
1683 for (i = 0; i < count; ++i)
1685 if (SUCCEEDED(IMFTopologyNode_GetInputPrefType(src_node, i, &mediatype)))
1687 IMFTopologyNode_SetInputPrefType(iface, i, mediatype);
1688 IMFMediaType_Release(mediatype);
1693 if (SUCCEEDED(IMFTopologyNode_GetOutputCount(src_node, &count)))
1695 for (i = 0; i < count; ++i)
1697 if (SUCCEEDED(IMFTopologyNode_GetOutputPrefType(src_node, i, &mediatype)))
1699 IMFTopologyNode_SetOutputPrefType(iface, i, mediatype);
1700 IMFMediaType_Release(mediatype);
1705 LeaveCriticalSection(&node->cs);
1707 if (object)
1708 IUnknown_Release(object);
1710 return hr;
1713 static const IMFTopologyNodeVtbl topologynodevtbl =
1715 topology_node_QueryInterface,
1716 topology_node_AddRef,
1717 topology_node_Release,
1718 topology_node_GetItem,
1719 topology_node_GetItemType,
1720 topology_node_CompareItem,
1721 topology_node_Compare,
1722 topology_node_GetUINT32,
1723 topology_node_GetUINT64,
1724 topology_node_GetDouble,
1725 topology_node_GetGUID,
1726 topology_node_GetStringLength,
1727 topology_node_GetString,
1728 topology_node_GetAllocatedString,
1729 topology_node_GetBlobSize,
1730 topology_node_GetBlob,
1731 topology_node_GetAllocatedBlob,
1732 topology_node_GetUnknown,
1733 topology_node_SetItem,
1734 topology_node_DeleteItem,
1735 topology_node_DeleteAllItems,
1736 topology_node_SetUINT32,
1737 topology_node_SetUINT64,
1738 topology_node_SetDouble,
1739 topology_node_SetGUID,
1740 topology_node_SetString,
1741 topology_node_SetBlob,
1742 topology_node_SetUnknown,
1743 topology_node_LockStore,
1744 topology_node_UnlockStore,
1745 topology_node_GetCount,
1746 topology_node_GetItemByIndex,
1747 topology_node_CopyAllItems,
1748 topology_node_SetObject,
1749 topology_node_GetObject,
1750 topology_node_GetNodeType,
1751 topology_node_GetTopoNodeID,
1752 topology_node_SetTopoNodeID,
1753 topology_node_GetInputCount,
1754 topology_node_GetOutputCount,
1755 topology_node_ConnectOutput,
1756 topology_node_DisconnectOutput,
1757 topology_node_GetInput,
1758 topology_node_GetOutput,
1759 topology_node_SetOutputPrefType,
1760 topology_node_GetOutputPrefType,
1761 topology_node_SetInputPrefType,
1762 topology_node_GetInputPrefType,
1763 topology_node_CloneFrom,
1766 static HRESULT create_topology_node(MF_TOPOLOGY_TYPE node_type, struct topology_node **node)
1768 HRESULT hr;
1770 *node = heap_alloc_zero(sizeof(**node));
1771 if (!*node)
1772 return E_OUTOFMEMORY;
1774 (*node)->IMFTopologyNode_iface.lpVtbl = &topologynodevtbl;
1775 (*node)->refcount = 1;
1776 (*node)->node_type = node_type;
1777 hr = MFCreateAttributes(&(*node)->attributes, 0);
1778 if (FAILED(hr))
1780 heap_free(*node);
1781 return hr;
1783 (*node)->id = ((TOPOID)GetCurrentProcessId() << 32) | InterlockedIncrement(&next_node_id);
1784 InitializeCriticalSection(&(*node)->cs);
1786 return S_OK;
1789 HRESULT topology_node_get_object(IMFTopologyNode *node, REFIID riid, void **obj)
1791 IUnknown *unk;
1792 HRESULT hr;
1794 *obj = NULL;
1796 if (SUCCEEDED(hr = IMFTopologyNode_GetObject(node, &unk)))
1798 hr = IUnknown_QueryInterface(unk, riid, obj);
1799 IUnknown_Release(unk);
1802 return hr;
1805 /***********************************************************************
1806 * MFCreateTopologyNode (mf.@)
1808 HRESULT WINAPI MFCreateTopologyNode(MF_TOPOLOGY_TYPE node_type, IMFTopologyNode **node)
1810 struct topology_node *object;
1811 HRESULT hr;
1813 TRACE("%d, %p.\n", node_type, node);
1815 if (!node)
1816 return E_POINTER;
1818 hr = create_topology_node(node_type, &object);
1819 if (SUCCEEDED(hr))
1820 *node = &object->IMFTopologyNode_iface;
1822 return hr;
1825 /***********************************************************************
1826 * MFGetTopoNodeCurrentType (mf.@)
1828 HRESULT WINAPI MFGetTopoNodeCurrentType(IMFTopologyNode *node, DWORD stream, BOOL output, IMFMediaType **type)
1830 IMFMediaTypeHandler *type_handler;
1831 MF_TOPOLOGY_TYPE node_type;
1832 IMFStreamSink *stream_sink;
1833 IMFStreamDescriptor *sd;
1834 IMFTransform *transform;
1835 UINT32 primary_output;
1836 HRESULT hr;
1838 TRACE("%p, %u, %d, %p.\n", node, stream, output, type);
1840 if (FAILED(hr = IMFTopologyNode_GetNodeType(node, &node_type)))
1841 return hr;
1843 switch (node_type)
1845 case MF_TOPOLOGY_OUTPUT_NODE:
1846 if (SUCCEEDED(topology_node_get_object(node, &IID_IMFStreamSink, (void **)&stream_sink)))
1848 hr = IMFStreamSink_GetMediaTypeHandler(stream_sink, &type_handler);
1849 IMFStreamSink_Release(stream_sink);
1851 if (SUCCEEDED(hr))
1853 hr = IMFMediaTypeHandler_GetCurrentMediaType(type_handler, type);
1854 IMFMediaTypeHandler_Release(type_handler);
1857 break;
1858 case MF_TOPOLOGY_SOURCESTREAM_NODE:
1859 if (FAILED(hr = IMFTopologyNode_GetUnknown(node, &MF_TOPONODE_STREAM_DESCRIPTOR, &IID_IMFStreamDescriptor,
1860 (void **)&sd)))
1862 return hr;
1865 hr = IMFStreamDescriptor_GetMediaTypeHandler(sd, &type_handler);
1866 IMFStreamDescriptor_Release(sd);
1867 if (SUCCEEDED(hr))
1869 hr = IMFMediaTypeHandler_GetCurrentMediaType(type_handler, type);
1870 IMFMediaTypeHandler_Release(type_handler);
1872 break;
1873 case MF_TOPOLOGY_TRANSFORM_NODE:
1874 if (SUCCEEDED(hr = topology_node_get_object(node, &IID_IMFTransform, (void **)&transform)))
1876 if (output)
1877 hr = IMFTransform_GetOutputCurrentType(transform, stream, type);
1878 else
1879 hr = IMFTransform_GetInputCurrentType(transform, stream, type);
1880 IMFTransform_Release(transform);
1882 break;
1883 case MF_TOPOLOGY_TEE_NODE:
1884 if (SUCCEEDED(hr = IMFTopologyNode_GetInputPrefType(node, 0, type)))
1885 break;
1887 if (FAILED(IMFTopologyNode_GetUINT32(node, &MF_TOPONODE_PRIMARYOUTPUT, &primary_output)))
1888 primary_output = 0;
1890 hr = IMFTopologyNode_GetOutputPrefType(node, primary_output, type);
1891 break;
1892 default:
1896 return hr;
1899 static HRESULT WINAPI topology_loader_QueryInterface(IMFTopoLoader *iface, REFIID riid, void **out)
1901 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
1903 if (IsEqualIID(riid, &IID_IMFTopoLoader) ||
1904 IsEqualIID(riid, &IID_IUnknown))
1906 *out = iface;
1907 IMFTopoLoader_AddRef(iface);
1908 return S_OK;
1911 WARN("Unsupported %s.\n", debugstr_guid(riid));
1912 *out = NULL;
1913 return E_NOINTERFACE;
1916 static ULONG WINAPI topology_loader_AddRef(IMFTopoLoader *iface)
1918 struct topology_loader *loader = impl_from_IMFTopoLoader(iface);
1919 ULONG refcount = InterlockedIncrement(&loader->refcount);
1921 TRACE("%p, refcount %u.\n", iface, refcount);
1923 return refcount;
1926 static ULONG WINAPI topology_loader_Release(IMFTopoLoader *iface)
1928 struct topology_loader *loader = impl_from_IMFTopoLoader(iface);
1929 ULONG refcount = InterlockedDecrement(&loader->refcount);
1931 TRACE("%p, refcount %u.\n", iface, refcount);
1933 if (!refcount)
1935 heap_free(loader);
1938 return refcount;
1941 struct topoloader_context
1943 IMFTopology *input_topology;
1944 IMFTopology *output_topology;
1945 unsigned int marker;
1946 GUID key;
1949 static IMFTopologyNode *topology_loader_get_node_for_marker(struct topoloader_context *context, TOPOID *id)
1951 IMFTopologyNode *node;
1952 unsigned short i = 0;
1953 unsigned int value;
1955 while (SUCCEEDED(IMFTopology_GetNode(context->output_topology, i++, &node)))
1957 if (SUCCEEDED(IMFTopologyNode_GetUINT32(node, &context->key, &value)) && value == context->marker)
1959 IMFTopologyNode_GetTopoNodeID(node, id);
1960 return node;
1962 IMFTopologyNode_Release(node);
1965 *id = 0;
1966 return NULL;
1969 static HRESULT topology_loader_clone_node(struct topoloader_context *context, IMFTopologyNode *node,
1970 IMFTopologyNode **ret, unsigned int marker)
1972 IMFTopologyNode *cloned_node;
1973 MF_TOPOLOGY_TYPE node_type;
1974 HRESULT hr;
1976 if (ret) *ret = NULL;
1978 IMFTopologyNode_GetNodeType(node, &node_type);
1980 if (FAILED(hr = MFCreateTopologyNode(node_type, &cloned_node)))
1981 return hr;
1983 if (SUCCEEDED(hr = IMFTopologyNode_CloneFrom(cloned_node, node)))
1984 hr = IMFTopologyNode_SetUINT32(cloned_node, &context->key, marker);
1986 if (SUCCEEDED(hr))
1987 hr = IMFTopology_AddNode(context->output_topology, cloned_node);
1989 if (SUCCEEDED(hr) && ret)
1991 *ret = cloned_node;
1992 IMFTopologyNode_AddRef(*ret);
1995 IMFTopologyNode_Release(cloned_node);
1997 return hr;
2000 struct transform_output_type
2002 IMFMediaType *type;
2003 IMFTransform *transform;
2004 IMFActivate *activate;
2007 struct connect_context
2009 struct topoloader_context *context;
2010 IMFTopologyNode *upstream_node;
2011 IMFTopologyNode *sink;
2012 IMFMediaTypeHandler *sink_handler;
2013 const GUID *converter_category;
2016 typedef HRESULT (*p_connect_func)(struct transform_output_type *output_type, struct connect_context *context);
2018 static void topology_loader_release_transforms(IMFActivate **activates, unsigned int count)
2020 unsigned int i;
2022 for (i = 0; i < count; ++i)
2023 IMFActivate_Release(activates[i]);
2024 CoTaskMemFree(activates);
2027 static HRESULT topology_loader_enumerate_output_types(const GUID *category, IMFMediaType *input_type,
2028 p_connect_func connect_func, struct connect_context *context)
2030 MFT_REGISTER_TYPE_INFO mft_typeinfo;
2031 IMFActivate **activates;
2032 unsigned int i, count;
2033 HRESULT hr;
2035 if (FAILED(hr = IMFMediaType_GetMajorType(input_type, &mft_typeinfo.guidMajorType)))
2036 return hr;
2038 if (FAILED(hr = IMFMediaType_GetGUID(input_type, &MF_MT_SUBTYPE, &mft_typeinfo.guidSubtype)))
2039 return hr;
2041 if (FAILED(hr = MFTEnumEx(*category, MFT_ENUM_FLAG_ALL, &mft_typeinfo, NULL, &activates, &count)))
2042 return hr;
2044 hr = E_FAIL;
2046 for (i = 0; i < count; ++i)
2048 IMFTransform *transform;
2050 if (FAILED(IMFActivate_ActivateObject(activates[i], &IID_IMFTransform, (void **)&transform)))
2052 WARN("Failed to create a transform.\n");
2053 continue;
2056 if (SUCCEEDED(hr = IMFTransform_SetInputType(transform, 0, input_type, 0)))
2058 struct transform_output_type output_type;
2059 unsigned int output_count = 0;
2061 output_type.transform = transform;
2062 output_type.activate = activates[i];
2063 while (SUCCEEDED(IMFTransform_GetOutputAvailableType(transform, 0, output_count++, &output_type.type)))
2065 hr = connect_func(&output_type, context);
2066 IMFMediaType_Release(output_type.type);
2067 if (SUCCEEDED(hr))
2069 topology_loader_release_transforms(activates, count);
2070 return hr;
2075 IMFActivate_ShutdownObject(activates[i]);
2078 topology_loader_release_transforms(activates, count);
2080 return hr;
2083 static HRESULT topology_loader_create_transform(const struct transform_output_type *output_type,
2084 IMFTopologyNode **node)
2086 HRESULT hr;
2087 GUID guid;
2089 if (FAILED(hr = MFCreateTopologyNode(MF_TOPOLOGY_TRANSFORM_NODE, node)))
2090 return hr;
2092 IMFTopologyNode_SetObject(*node, (IUnknown *)output_type->transform);
2094 if (SUCCEEDED(IMFActivate_GetGUID(output_type->activate, &MF_TRANSFORM_CATEGORY_Attribute, &guid)) &&
2095 (IsEqualGUID(&guid, &MFT_CATEGORY_AUDIO_DECODER) || IsEqualGUID(&guid, &MFT_CATEGORY_VIDEO_DECODER)))
2097 IMFTopologyNode_SetUINT32(*node, &MF_TOPONODE_DECODER, 1);
2100 if (SUCCEEDED(IMFActivate_GetGUID(output_type->activate, &MFT_TRANSFORM_CLSID_Attribute, &guid)))
2101 IMFTopologyNode_SetGUID(*node, &MF_TOPONODE_TRANSFORM_OBJECTID, &guid);
2103 return hr;
2106 static HRESULT connect_to_sink(struct transform_output_type *output_type, struct connect_context *context)
2108 IMFTopologyNode *node;
2109 HRESULT hr;
2111 if (FAILED(IMFMediaTypeHandler_IsMediaTypeSupported(context->sink_handler, output_type->type, NULL)))
2112 return MF_E_TRANSFORM_NOT_POSSIBLE_FOR_CURRENT_MEDIATYPE_COMBINATION;
2114 if (FAILED(hr = topology_loader_create_transform(output_type, &node)))
2115 return hr;
2117 IMFTopology_AddNode(context->context->output_topology, node);
2118 IMFTopologyNode_ConnectOutput(context->upstream_node, 0, node, 0);
2119 IMFTopologyNode_ConnectOutput(node, 0, context->sink, 0);
2121 IMFTopologyNode_Release(node);
2123 hr = IMFMediaTypeHandler_SetCurrentMediaType(context->sink_handler, output_type->type);
2124 if (SUCCEEDED(hr))
2125 hr = IMFTransform_SetOutputType(output_type->transform, 0, output_type->type, 0);
2127 return S_OK;
2130 static HRESULT connect_to_converter(struct transform_output_type *output_type, struct connect_context *context)
2132 struct connect_context sink_ctx;
2133 IMFTopologyNode *node;
2134 HRESULT hr;
2136 if (SUCCEEDED(connect_to_sink(output_type, context)))
2137 return S_OK;
2139 if (FAILED(hr = topology_loader_create_transform(output_type, &node)))
2140 return hr;
2142 sink_ctx = *context;
2143 sink_ctx.upstream_node = node;
2145 if (SUCCEEDED(hr = topology_loader_enumerate_output_types(context->converter_category, output_type->type,
2146 connect_to_sink, &sink_ctx)))
2148 hr = IMFTopology_AddNode(context->context->output_topology, node);
2150 IMFTopologyNode_Release(node);
2152 if (SUCCEEDED(hr))
2154 IMFTopology_AddNode(context->context->output_topology, node);
2155 IMFTopologyNode_ConnectOutput(context->upstream_node, 0, node, 0);
2157 hr = IMFTransform_SetOutputType(output_type->transform, 0, output_type->type, 0);
2160 return hr;
2163 typedef HRESULT (*p_topology_loader_connect_func)(struct topoloader_context *context, IMFTopologyNode *upstream_node,
2164 unsigned int output_index, IMFTopologyNode *downstream_node, unsigned int input_index);
2166 static HRESULT topology_loader_get_node_type_handler(IMFTopologyNode *node, IMFMediaTypeHandler **handler)
2168 MF_TOPOLOGY_TYPE node_type;
2169 IMFStreamSink *stream_sink;
2170 IMFStreamDescriptor *sd;
2171 HRESULT hr;
2173 if (FAILED(hr = IMFTopologyNode_GetNodeType(node, &node_type)))
2174 return hr;
2176 switch (node_type)
2178 case MF_TOPOLOGY_OUTPUT_NODE:
2179 if (SUCCEEDED(hr = topology_node_get_object(node, &IID_IMFStreamSink, (void **)&stream_sink)))
2181 hr = IMFStreamSink_GetMediaTypeHandler(stream_sink, handler);
2182 IMFStreamSink_Release(stream_sink);
2184 break;
2185 case MF_TOPOLOGY_SOURCESTREAM_NODE:
2186 if (SUCCEEDED(hr = IMFTopologyNode_GetUnknown(node, &MF_TOPONODE_STREAM_DESCRIPTOR,
2187 &IID_IMFStreamDescriptor, (void **)&sd)))
2189 hr = IMFStreamDescriptor_GetMediaTypeHandler(sd, handler);
2190 IMFStreamDescriptor_Release(sd);
2192 break;
2193 default:
2194 WARN("Unexpected node type %u.\n", node_type);
2195 return MF_E_UNEXPECTED;
2198 return hr;
2201 static HRESULT topology_loader_get_mft_categories(IMFMediaTypeHandler *handler, GUID *decode_cat, GUID *convert_cat)
2203 GUID major;
2204 HRESULT hr;
2206 if (FAILED(hr = IMFMediaTypeHandler_GetMajorType(handler, &major)))
2207 return hr;
2209 if (IsEqualGUID(&major, &MFMediaType_Audio))
2211 *decode_cat = MFT_CATEGORY_AUDIO_DECODER;
2212 *convert_cat = MFT_CATEGORY_AUDIO_EFFECT;
2214 else if (IsEqualGUID(&major, &MFMediaType_Video))
2216 *decode_cat = MFT_CATEGORY_VIDEO_DECODER;
2217 *convert_cat = MFT_CATEGORY_VIDEO_EFFECT;
2219 else
2221 WARN("Unexpected major type %s.\n", debugstr_guid(&major));
2222 return MF_E_INVALIDTYPE;
2225 return S_OK;
2228 static HRESULT topology_loader_connect_source_to_sink(struct topoloader_context *context, IMFTopologyNode *source,
2229 unsigned int output_index, IMFTopologyNode *sink, unsigned int input_index)
2231 IMFMediaTypeHandler *source_handler = NULL, *sink_handler = NULL;
2232 struct connect_context convert_ctx, sink_ctx;
2233 MF_CONNECT_METHOD source_method, sink_method;
2234 GUID decode_cat, convert_cat;
2235 IMFMediaType *media_type;
2236 HRESULT hr;
2238 TRACE("attempting to connect %p:%u to %p:%u\n", source, output_index, sink, input_index);
2240 if (FAILED(hr = topology_loader_get_node_type_handler(source, &source_handler)))
2241 goto done;
2243 if (FAILED(hr = topology_loader_get_node_type_handler(sink, &sink_handler)))
2244 goto done;
2246 if (FAILED(IMFTopologyNode_GetUINT32(source, &MF_TOPONODE_CONNECT_METHOD, &source_method)))
2247 source_method = MF_CONNECT_DIRECT;
2248 if (FAILED(IMFTopologyNode_GetUINT32(sink, &MF_TOPONODE_CONNECT_METHOD, &sink_method)))
2249 sink_method = MF_CONNECT_ALLOW_DECODER;
2251 if (FAILED(hr = topology_loader_get_mft_categories(source_handler, &decode_cat, &convert_cat)))
2252 goto done;
2254 sink_ctx.context = context;
2255 sink_ctx.upstream_node = source;
2256 sink_ctx.sink = sink;
2257 sink_ctx.sink_handler = sink_handler;
2259 convert_ctx = sink_ctx;
2260 convert_ctx.converter_category = &convert_cat;
2262 if (SUCCEEDED(hr = IMFMediaTypeHandler_GetCurrentMediaType(source_handler, &media_type)))
2264 /* Direct connection. */
2265 if (SUCCEEDED(hr = IMFMediaTypeHandler_IsMediaTypeSupported(sink_handler, media_type, NULL))
2266 && SUCCEEDED(hr = IMFMediaTypeHandler_SetCurrentMediaType(sink_handler, media_type)))
2268 hr = IMFTopologyNode_ConnectOutput(source, output_index, sink, input_index);
2270 if (FAILED(hr) && sink_method & MF_CONNECT_ALLOW_CONVERTER)
2272 hr = topology_loader_enumerate_output_types(&convert_cat, media_type, connect_to_sink, &sink_ctx);
2274 if (FAILED(hr) && sink_method & MF_CONNECT_ALLOW_DECODER)
2276 hr = topology_loader_enumerate_output_types(&decode_cat, media_type, connect_to_converter, &convert_ctx);
2279 IMFMediaType_Release(media_type);
2282 done:
2283 if (source_handler)
2284 IMFMediaTypeHandler_Release(source_handler);
2285 if (sink_handler)
2286 IMFMediaTypeHandler_Release(sink_handler);
2288 return hr;
2291 static HRESULT topology_loader_resolve_branch(struct topoloader_context *context, IMFTopologyNode *upstream_node,
2292 unsigned int output_index, IMFTopologyNode *downstream_node, unsigned input_index)
2294 static const p_topology_loader_connect_func connectors[MF_TOPOLOGY_TEE_NODE+1][MF_TOPOLOGY_TEE_NODE+1] =
2296 /* OUTPUT */ { NULL },
2297 /* SOURCESTREAM */ { topology_loader_connect_source_to_sink, NULL, NULL, NULL },
2298 /* TRANSFORM */ { NULL },
2299 /* TEE */ { NULL },
2301 MF_TOPOLOGY_TYPE u_type, d_type;
2302 IMFTopologyNode *node;
2303 TOPOID id;
2305 /* Downstream node might have already been cloned. */
2306 IMFTopologyNode_GetTopoNodeID(downstream_node, &id);
2307 if (FAILED(IMFTopology_GetNodeByID(context->output_topology, id, &node)))
2308 topology_loader_clone_node(context, downstream_node, &node, context->marker + 1);
2310 IMFTopologyNode_GetNodeType(upstream_node, &u_type);
2311 IMFTopologyNode_GetNodeType(downstream_node, &d_type);
2313 if (!connectors[u_type][d_type])
2315 WARN("Unsupported branch kind %d -> %d.\n", u_type, d_type);
2316 return E_FAIL;
2319 return connectors[u_type][d_type](context, upstream_node, output_index, node, input_index);
2322 static HRESULT topology_loader_resolve_nodes(struct topoloader_context *context, unsigned int *layer_size)
2324 IMFTopologyNode *downstream_node, *node, *orig_node;
2325 unsigned int input_index, size = 0;
2326 MF_TOPOLOGY_TYPE node_type;
2327 HRESULT hr = S_OK;
2328 TOPOID id;
2330 while ((node = topology_loader_get_node_for_marker(context, &id)))
2332 ++size;
2334 IMFTopology_GetNodeByID(context->input_topology, id, &orig_node);
2336 IMFTopologyNode_GetNodeType(node, &node_type);
2337 switch (node_type)
2339 case MF_TOPOLOGY_SOURCESTREAM_NODE:
2340 if (FAILED(IMFTopologyNode_GetOutput(orig_node, 0, &downstream_node, &input_index)))
2342 IMFTopology_RemoveNode(context->output_topology, node);
2343 continue;
2346 hr = topology_loader_resolve_branch(context, node, 0, downstream_node, input_index);
2347 break;
2348 case MF_TOPOLOGY_TRANSFORM_NODE:
2349 case MF_TOPOLOGY_TEE_NODE:
2350 FIXME("Unsupported node type %d.\n", node_type);
2351 break;
2352 default:
2353 WARN("Unexpected node type %d.\n", node_type);
2356 IMFTopologyNode_DeleteItem(node, &context->key);
2358 if (FAILED(hr))
2359 break;
2362 *layer_size = size;
2364 return hr;
2367 static BOOL topology_loader_is_node_d3d_aware(IMFTopologyNode *node)
2369 IMFAttributes *attributes;
2370 unsigned int d3d_aware = 0;
2371 IMFTransform *transform;
2373 if (FAILED(topology_node_get_object(node, &IID_IMFAttributes, (void **)&attributes)))
2374 return FALSE;
2376 IMFAttributes_GetUINT32(attributes, &MF_SA_D3D_AWARE, &d3d_aware);
2377 IMFAttributes_Release(attributes);
2379 if (!d3d_aware && SUCCEEDED(topology_node_get_object(node, &IID_IMFTransform, (void **)&transform)))
2381 d3d_aware = mf_is_sample_copier_transform(transform);
2382 IMFTransform_Release(transform);
2385 return !!d3d_aware;
2388 static HRESULT topology_loader_create_copier(IMFTopologyNode *upstream_node, unsigned int upstream_output,
2389 IMFTopologyNode *downstream_node, unsigned int downstream_input, IMFTransform **copier)
2391 IMFMediaType *input_type = NULL, *output_type = NULL;
2392 IMFTransform *transform;
2393 HRESULT hr;
2395 if (FAILED(hr = MFCreateSampleCopierMFT(&transform)))
2396 return hr;
2398 if (FAILED(hr = MFGetTopoNodeCurrentType(upstream_node, upstream_output, TRUE, &input_type)))
2399 WARN("Failed to get upstream media type hr %#x.\n", hr);
2401 if (SUCCEEDED(hr) && FAILED(hr = MFGetTopoNodeCurrentType(downstream_node, downstream_input, FALSE, &output_type)))
2402 WARN("Failed to get downstream media type hr %#x.\n", hr);
2404 if (SUCCEEDED(hr) && FAILED(hr = IMFTransform_SetInputType(transform, 0, input_type, 0)))
2405 WARN("Input type wasn't accepted, hr %#x.\n", hr);
2407 if (SUCCEEDED(hr) && FAILED(hr = IMFTransform_SetOutputType(transform, 0, output_type, 0)))
2408 WARN("Output type wasn't accepted, hr %#x.\n", hr);
2410 if (SUCCEEDED(hr))
2412 *copier = transform;
2413 IMFTransform_AddRef(*copier);
2416 if (input_type)
2417 IMFMediaType_Release(input_type);
2418 if (output_type)
2419 IMFMediaType_Release(output_type);
2421 IMFTransform_Release(transform);
2423 return hr;
2426 static HRESULT topology_loader_connect_copier(struct topoloader_context *context, IMFTopologyNode *upstream_node,
2427 unsigned int upstream_output, IMFTopologyNode *downstream_node, unsigned int downstream_input, IMFTransform *copier)
2429 IMFTopologyNode *copier_node;
2430 HRESULT hr;
2432 if (FAILED(hr = MFCreateTopologyNode(MF_TOPOLOGY_TRANSFORM_NODE, &copier_node)))
2433 return hr;
2435 IMFTopologyNode_SetObject(copier_node, (IUnknown *)copier);
2436 IMFTopology_AddNode(context->output_topology, copier_node);
2437 IMFTopologyNode_ConnectOutput(upstream_node, upstream_output, copier_node, 0);
2438 IMFTopologyNode_ConnectOutput(copier_node, 0, downstream_node, downstream_input);
2440 IMFTopologyNode_Release(copier_node);
2442 return S_OK;
2445 /* Right now this should be used for output nodes only. */
2446 static HRESULT topology_loader_connect_d3d_aware_input(struct topoloader_context *context,
2447 IMFTopologyNode *node)
2449 IMFTopologyNode *upstream_node;
2450 unsigned int upstream_output;
2451 IMFStreamSink *stream_sink;
2452 IMFTransform *copier = NULL;
2453 HRESULT hr;
2455 if (FAILED(hr = topology_node_get_object(node, &IID_IMFStreamSink, (void **)&stream_sink)))
2456 return hr;
2458 if (topology_loader_is_node_d3d_aware(node))
2460 if (SUCCEEDED(IMFTopologyNode_GetInput(node, 0, &upstream_node, &upstream_output)))
2462 if (!topology_loader_is_node_d3d_aware(upstream_node))
2464 if (SUCCEEDED(hr = topology_loader_create_copier(upstream_node, upstream_output, node, 0, &copier)))
2466 hr = topology_loader_connect_copier(context, upstream_node, upstream_output, node, 0, copier);
2467 IMFTransform_Release(copier);
2470 IMFTopologyNode_Release(upstream_node);
2474 IMFStreamSink_Release(stream_sink);
2476 return hr;
2479 static void topology_loader_resolve_complete(struct topoloader_context *context)
2481 MF_TOPOLOGY_TYPE node_type;
2482 IMFTopologyNode *node;
2483 WORD i, node_count;
2484 HRESULT hr;
2486 IMFTopology_GetNodeCount(context->output_topology, &node_count);
2488 for (i = 0; i < node_count; ++i)
2490 if (SUCCEEDED(IMFTopology_GetNode(context->output_topology, i, &node)))
2492 IMFTopologyNode_GetNodeType(node, &node_type);
2494 if (node_type == MF_TOPOLOGY_OUTPUT_NODE)
2496 /* Set MF_TOPONODE_STREAMID for all outputs. */
2497 if (FAILED(IMFTopologyNode_GetItem(node, &MF_TOPONODE_STREAMID, NULL)))
2498 IMFTopologyNode_SetUINT32(node, &MF_TOPONODE_STREAMID, 0);
2500 if (FAILED(hr = topology_loader_connect_d3d_aware_input(context, node)))
2501 WARN("Failed to connect D3D-aware input, hr %#x.\n", hr);
2503 else if (node_type == MF_TOPOLOGY_SOURCESTREAM_NODE)
2505 /* Set MF_TOPONODE_MEDIASTART for all sources. */
2506 if (FAILED(IMFTopologyNode_GetItem(node, &MF_TOPONODE_MEDIASTART, NULL)))
2507 IMFTopologyNode_SetUINT64(node, &MF_TOPONODE_MEDIASTART, 0);
2510 IMFTopologyNode_Release(node);
2515 static HRESULT WINAPI topology_loader_Load(IMFTopoLoader *iface, IMFTopology *input_topology,
2516 IMFTopology **ret_topology, IMFTopology *current_topology)
2518 struct topoloader_context context = { 0 };
2519 IMFTopology *output_topology;
2520 MF_TOPOLOGY_TYPE node_type;
2521 unsigned int layer_size;
2522 IMFTopologyNode *node;
2523 unsigned short i = 0;
2524 IMFStreamSink *sink;
2525 IUnknown *object;
2526 HRESULT hr = E_FAIL;
2528 FIXME("%p, %p, %p, %p.\n", iface, input_topology, ret_topology, current_topology);
2530 if (current_topology)
2531 FIXME("Current topology instance is ignored.\n");
2533 /* Basic sanity checks for input topology:
2535 - source nodes must have stream descriptor set;
2536 - sink nodes must be resolved to stream sink objects;
2538 while (SUCCEEDED(IMFTopology_GetNode(input_topology, i++, &node)))
2540 IMFTopologyNode_GetNodeType(node, &node_type);
2542 switch (node_type)
2544 case MF_TOPOLOGY_OUTPUT_NODE:
2545 if (SUCCEEDED(hr = IMFTopologyNode_GetObject(node, &object)))
2547 /* Sinks must be bound beforehand. */
2548 if (FAILED(IUnknown_QueryInterface(object, &IID_IMFStreamSink, (void **)&sink)))
2549 hr = MF_E_TOPO_SINK_ACTIVATES_UNSUPPORTED;
2550 else if (sink)
2551 IMFStreamSink_Release(sink);
2552 IUnknown_Release(object);
2554 break;
2555 case MF_TOPOLOGY_SOURCESTREAM_NODE:
2556 hr = IMFTopologyNode_GetItem(node, &MF_TOPONODE_STREAM_DESCRIPTOR, NULL);
2557 break;
2558 default:
2562 IMFTopologyNode_Release(node);
2563 if (FAILED(hr))
2564 return hr;
2567 if (FAILED(hr = MFCreateTopology(&output_topology)))
2568 return hr;
2570 IMFTopology_CopyAllItems(input_topology, (IMFAttributes *)output_topology);
2572 context.input_topology = input_topology;
2573 context.output_topology = output_topology;
2574 memset(&context.key, 0xff, sizeof(context.key));
2576 /* Clone source nodes, use initial marker value. */
2577 i = 0;
2578 while (SUCCEEDED(IMFTopology_GetNode(input_topology, i++, &node)))
2580 IMFTopologyNode_GetNodeType(node, &node_type);
2582 if (node_type == MF_TOPOLOGY_SOURCESTREAM_NODE)
2584 if (FAILED(hr = topology_loader_clone_node(&context, node, NULL, 0)))
2585 WARN("Failed to clone source node, hr %#x.\n", hr);
2588 IMFTopologyNode_Release(node);
2591 for (context.marker = 0;; ++context.marker)
2593 if (FAILED(hr = topology_loader_resolve_nodes(&context, &layer_size)))
2595 WARN("Failed to resolve for marker %u, hr %#x.\n", context.marker, hr);
2596 break;
2599 /* Reached last marker value. */
2600 if (!layer_size)
2601 break;
2604 if (SUCCEEDED(hr))
2605 topology_loader_resolve_complete(&context);
2607 *ret_topology = output_topology;
2609 return hr;
2612 static const IMFTopoLoaderVtbl topologyloadervtbl =
2614 topology_loader_QueryInterface,
2615 topology_loader_AddRef,
2616 topology_loader_Release,
2617 topology_loader_Load,
2620 /***********************************************************************
2621 * MFCreateTopoLoader (mf.@)
2623 HRESULT WINAPI MFCreateTopoLoader(IMFTopoLoader **loader)
2625 struct topology_loader *object;
2627 TRACE("%p.\n", loader);
2629 if (!loader)
2630 return E_POINTER;
2632 object = heap_alloc(sizeof(*object));
2633 if (!object)
2634 return E_OUTOFMEMORY;
2636 object->IMFTopoLoader_iface.lpVtbl = &topologyloadervtbl;
2637 object->refcount = 1;
2639 *loader = &object->IMFTopoLoader_iface;
2641 return S_OK;
2644 static HRESULT WINAPI seq_source_QueryInterface(IMFSequencerSource *iface, REFIID riid, void **out)
2646 struct seq_source *seq_source = impl_from_IMFSequencerSource(iface);
2648 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
2650 *out = NULL;
2652 if (IsEqualIID(riid, &IID_IMFSequencerSource) ||
2653 IsEqualIID(riid, &IID_IUnknown))
2655 *out = &seq_source->IMFSequencerSource_iface;
2657 else if (IsEqualIID(riid, &IID_IMFMediaSourceTopologyProvider))
2659 *out = &seq_source->IMFMediaSourceTopologyProvider_iface;
2661 else
2663 WARN("Unimplemented %s.\n", debugstr_guid(riid));
2664 return E_NOINTERFACE;
2667 if (*out)
2668 IUnknown_AddRef((IUnknown *)*out);
2670 return S_OK;
2673 static ULONG WINAPI seq_source_AddRef(IMFSequencerSource *iface)
2675 struct seq_source *seq_source = impl_from_IMFSequencerSource(iface);
2676 ULONG refcount = InterlockedIncrement(&seq_source->refcount);
2678 TRACE("%p, refcount %u.\n", iface, refcount);
2680 return refcount;
2683 static ULONG WINAPI seq_source_Release(IMFSequencerSource *iface)
2685 struct seq_source *seq_source = impl_from_IMFSequencerSource(iface);
2686 ULONG refcount = InterlockedDecrement(&seq_source->refcount);
2688 TRACE("%p, refcount %u.\n", iface, refcount);
2690 if (!refcount)
2692 heap_free(seq_source);
2695 return refcount;
2698 static HRESULT WINAPI seq_source_AppendTopology(IMFSequencerSource *iface, IMFTopology *topology,
2699 DWORD flags, MFSequencerElementId *id)
2701 FIXME("%p, %p, %x, %p.\n", iface, topology, flags, id);
2703 return E_NOTIMPL;
2706 static HRESULT WINAPI seq_source_DeleteTopology(IMFSequencerSource *iface, MFSequencerElementId id)
2708 FIXME("%p, %#x.\n", iface, id);
2710 return E_NOTIMPL;
2713 static HRESULT WINAPI seq_source_GetPresentationContext(IMFSequencerSource *iface,
2714 IMFPresentationDescriptor *descriptor, MFSequencerElementId *id, IMFTopology **topology)
2716 FIXME("%p, %p, %p, %p.\n", iface, descriptor, id, topology);
2718 return E_NOTIMPL;
2721 static HRESULT WINAPI seq_source_UpdateTopology(IMFSequencerSource *iface, MFSequencerElementId id,
2722 IMFTopology *topology)
2724 FIXME("%p, %#x, %p.\n", iface, id, topology);
2726 return E_NOTIMPL;
2729 static HRESULT WINAPI seq_source_UpdateTopologyFlags(IMFSequencerSource *iface, MFSequencerElementId id, DWORD flags)
2731 FIXME("%p, %#x, %#x.\n", iface, id, flags);
2733 return E_NOTIMPL;
2736 static HRESULT WINAPI seq_source_topology_provider_QueryInterface(IMFMediaSourceTopologyProvider *iface, REFIID riid,
2737 void **obj)
2739 struct seq_source *seq_source = impl_from_IMFMediaSourceTopologyProvider(iface);
2740 return IMFSequencerSource_QueryInterface(&seq_source->IMFSequencerSource_iface, riid, obj);
2743 static ULONG WINAPI seq_source_topology_provider_AddRef(IMFMediaSourceTopologyProvider *iface)
2745 struct seq_source *seq_source = impl_from_IMFMediaSourceTopologyProvider(iface);
2746 return IMFSequencerSource_AddRef(&seq_source->IMFSequencerSource_iface);
2749 static ULONG WINAPI seq_source_topology_provider_Release(IMFMediaSourceTopologyProvider *iface)
2751 struct seq_source *seq_source = impl_from_IMFMediaSourceTopologyProvider(iface);
2752 return IMFSequencerSource_Release(&seq_source->IMFSequencerSource_iface);
2755 static HRESULT WINAPI seq_source_topology_provider_GetMediaSourceTopology(IMFMediaSourceTopologyProvider *iface,
2756 IMFPresentationDescriptor *pd, IMFTopology **topology)
2758 FIXME("%p, %p, %p.\n", iface, pd, topology);
2760 return E_NOTIMPL;
2763 static const IMFMediaSourceTopologyProviderVtbl seq_source_topology_provider_vtbl =
2765 seq_source_topology_provider_QueryInterface,
2766 seq_source_topology_provider_AddRef,
2767 seq_source_topology_provider_Release,
2768 seq_source_topology_provider_GetMediaSourceTopology,
2771 static const IMFSequencerSourceVtbl seqsourcevtbl =
2773 seq_source_QueryInterface,
2774 seq_source_AddRef,
2775 seq_source_Release,
2776 seq_source_AppendTopology,
2777 seq_source_DeleteTopology,
2778 seq_source_GetPresentationContext,
2779 seq_source_UpdateTopology,
2780 seq_source_UpdateTopologyFlags,
2783 /***********************************************************************
2784 * MFCreateSequencerSource (mf.@)
2786 HRESULT WINAPI MFCreateSequencerSource(IUnknown *reserved, IMFSequencerSource **seq_source)
2788 struct seq_source *object;
2790 TRACE("%p, %p.\n", reserved, seq_source);
2792 if (!seq_source)
2793 return E_POINTER;
2795 object = heap_alloc(sizeof(*object));
2796 if (!object)
2797 return E_OUTOFMEMORY;
2799 object->IMFSequencerSource_iface.lpVtbl = &seqsourcevtbl;
2800 object->IMFMediaSourceTopologyProvider_iface.lpVtbl = &seq_source_topology_provider_vtbl;
2801 object->refcount = 1;
2803 *seq_source = &object->IMFSequencerSource_iface;
2805 return S_OK;