dwrite: Check for allocation failures of glyph buffers.
[wine.git] / dlls / mf / session.c
blob925a8c93d2003e3c9ae7abe2f43df6dedc571de1
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
19 #include <stdarg.h>
20 #include <math.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "mfidl.h"
27 #include "evr.h"
29 #include "wine/debug.h"
30 #include "wine/heap.h"
31 #include "wine/list.h"
33 #include "mf_private.h"
35 #include "initguid.h"
37 DEFINE_GUID(_MF_TOPONODE_IMFActivate, 0x33706f4a, 0x309a, 0x49be, 0xa8, 0xdd, 0xe7, 0xc0, 0x87, 0x5e, 0xb6, 0x79);
39 WINE_DEFAULT_DEBUG_CHANNEL(mfplat);
41 enum session_command
43 SESSION_CMD_CLEAR_TOPOLOGIES,
44 SESSION_CMD_CLOSE,
45 SESSION_CMD_SET_TOPOLOGY,
46 SESSION_CMD_START,
47 SESSION_CMD_PAUSE,
48 SESSION_CMD_STOP,
49 /* Internally used commands. */
50 SESSION_CMD_END,
51 SESSION_CMD_QM_NOTIFY_TOPOLOGY,
52 SESSION_CMD_SA_READY,
55 struct session_op
57 IUnknown IUnknown_iface;
58 LONG refcount;
59 enum session_command command;
60 union
62 struct
64 DWORD flags;
65 IMFTopology *topology;
66 } set_topology;
67 struct
69 GUID time_format;
70 PROPVARIANT start_position;
71 } start;
72 struct
74 IMFTopology *topology;
75 } notify_topology;
76 struct
78 TOPOID node_id;
79 } sa_ready;
80 } u;
81 struct list entry;
84 struct queued_topology
86 struct list entry;
87 IMFTopology *topology;
88 MF_TOPOSTATUS status;
91 enum session_state
93 SESSION_STATE_STOPPED = 0,
94 SESSION_STATE_STARTING_SOURCES,
95 SESSION_STATE_PREROLLING_SINKS,
96 SESSION_STATE_STARTING_SINKS,
97 SESSION_STATE_STARTED,
98 SESSION_STATE_PAUSING_SINKS,
99 SESSION_STATE_PAUSING_SOURCES,
100 SESSION_STATE_PAUSED,
101 SESSION_STATE_STOPPING_SINKS,
102 SESSION_STATE_STOPPING_SOURCES,
103 SESSION_STATE_FINALIZING_SINKS,
104 SESSION_STATE_CLOSED,
105 SESSION_STATE_SHUT_DOWN,
108 enum object_state
110 OBJ_STATE_STOPPED = 0,
111 OBJ_STATE_STARTED,
112 OBJ_STATE_PAUSED,
113 OBJ_STATE_PREROLLED,
114 OBJ_STATE_INVALID,
117 enum media_source_flags
119 SOURCE_FLAG_END_OF_PRESENTATION = 0x1,
122 struct media_source
124 struct list entry;
125 IMFMediaSource *source;
126 IMFPresentationDescriptor *pd;
127 enum object_state state;
128 unsigned int flags;
131 struct media_sink
133 struct list entry;
134 IMFMediaSink *sink;
135 IMFMediaSinkPreroll *preroll;
136 IMFMediaEventGenerator *event_generator;
137 BOOL finalized;
140 struct sample
142 struct list entry;
143 IMFSample *sample;
146 struct transform_stream
148 struct list samples;
149 unsigned int requests;
150 unsigned int min_buffer_size;
153 enum topo_node_flags
155 TOPO_NODE_END_OF_STREAM = 0x1,
158 struct topo_node
160 struct list entry;
161 struct media_session *session;
162 MF_TOPOLOGY_TYPE type;
163 TOPOID node_id;
164 IMFTopologyNode *node;
165 enum object_state state;
166 unsigned int flags;
167 union
169 IMFMediaStream *source_stream;
170 IMFStreamSink *sink_stream;
171 IMFTransform *transform;
172 IUnknown *object;
173 } object;
175 union
177 struct
179 IMFMediaSource *source;
180 unsigned int stream_id;
181 } source;
182 struct
184 unsigned int requests;
185 IMFVideoSampleAllocatorNotify notify_cb;
186 IMFVideoSampleAllocator *allocator;
187 IMFVideoSampleAllocatorCallback *allocator_cb;
188 } sink;
189 struct
191 struct transform_stream *inputs;
192 unsigned int *input_map;
193 unsigned int input_count;
195 struct transform_stream *outputs;
196 unsigned int *output_map;
197 unsigned int output_count;
198 } transform;
199 } u;
202 enum presentation_flags
204 SESSION_FLAG_SOURCES_SUBSCRIBED = 0x1,
205 SESSION_FLAG_PRESENTATION_CLOCK_SET = 0x2,
206 SESSION_FLAG_FINALIZE_SINKS = 0x4,
207 SESSION_FLAG_NEEDS_PREROLL = 0x8,
208 SESSION_FLAG_END_OF_PRESENTATION = 0x10,
211 struct media_session
213 IMFMediaSession IMFMediaSession_iface;
214 IMFGetService IMFGetService_iface;
215 IMFRateSupport IMFRateSupport_iface;
216 IMFRateControl IMFRateControl_iface;
217 IMFTopologyNodeAttributeEditor IMFTopologyNodeAttributeEditor_iface;
218 IMFAsyncCallback commands_callback;
219 IMFAsyncCallback events_callback;
220 IMFAsyncCallback sink_finalizer_callback;
221 LONG refcount;
222 IMFMediaEventQueue *event_queue;
223 IMFPresentationClock *clock;
224 IMFPresentationTimeSource *system_time_source;
225 IMFRateControl *clock_rate_control;
226 IMFTopoLoader *topo_loader;
227 IMFQualityManager *quality_manager;
228 struct
230 IMFTopology *current_topology;
231 MF_TOPOSTATUS topo_status;
232 MFTIME clock_stop_time;
233 unsigned int flags;
234 struct list sources;
235 struct list sinks;
236 struct list nodes;
238 /* Latest Start() arguments. */
239 GUID time_format;
240 PROPVARIANT start_position;
241 } presentation;
242 struct list topologies;
243 struct list commands;
244 enum session_state state;
245 DWORD caps;
246 CRITICAL_SECTION cs;
249 struct clock_sink
251 struct list entry;
252 IMFClockStateSink *state_sink;
255 enum clock_command
257 CLOCK_CMD_START = 0,
258 CLOCK_CMD_STOP,
259 CLOCK_CMD_PAUSE,
260 CLOCK_CMD_SET_RATE,
261 CLOCK_CMD_MAX,
264 enum clock_notification
266 CLOCK_NOTIFY_START,
267 CLOCK_NOTIFY_STOP,
268 CLOCK_NOTIFY_PAUSE,
269 CLOCK_NOTIFY_RESTART,
270 CLOCK_NOTIFY_SET_RATE,
273 struct clock_state_change_param
275 union
277 LONGLONG offset;
278 float rate;
279 } u;
282 struct sink_notification
284 IUnknown IUnknown_iface;
285 LONG refcount;
286 MFTIME system_time;
287 struct clock_state_change_param param;
288 enum clock_notification notification;
289 IMFClockStateSink *sink;
292 struct clock_timer
294 IUnknown IUnknown_iface;
295 LONG refcount;
296 IMFAsyncResult *result;
297 IMFAsyncCallback *callback;
298 MFWORKITEM_KEY key;
299 struct list entry;
302 struct presentation_clock
304 IMFPresentationClock IMFPresentationClock_iface;
305 IMFRateControl IMFRateControl_iface;
306 IMFTimer IMFTimer_iface;
307 IMFShutdown IMFShutdown_iface;
308 IMFAsyncCallback sink_callback;
309 IMFAsyncCallback timer_callback;
310 LONG refcount;
311 IMFPresentationTimeSource *time_source;
312 IMFClockStateSink *time_source_sink;
313 MFCLOCK_STATE state;
314 LONGLONG start_offset;
315 struct list sinks;
316 struct list timers;
317 float rate;
318 LONGLONG frequency;
319 CRITICAL_SECTION cs;
320 BOOL is_shut_down;
323 enum quality_manager_state
325 QUALITY_MANAGER_READY = 0,
326 QUALITY_MANAGER_SHUT_DOWN,
329 struct quality_manager
331 IMFQualityManager IMFQualityManager_iface;
332 IMFClockStateSink IMFClockStateSink_iface;
333 LONG refcount;
335 IMFTopology *topology;
336 IMFPresentationClock *clock;
337 unsigned int state;
338 CRITICAL_SECTION cs;
341 static inline struct media_session *impl_from_IMFMediaSession(IMFMediaSession *iface)
343 return CONTAINING_RECORD(iface, struct media_session, IMFMediaSession_iface);
346 static struct media_session *impl_from_commands_callback_IMFAsyncCallback(IMFAsyncCallback *iface)
348 return CONTAINING_RECORD(iface, struct media_session, commands_callback);
351 static struct media_session *impl_from_events_callback_IMFAsyncCallback(IMFAsyncCallback *iface)
353 return CONTAINING_RECORD(iface, struct media_session, events_callback);
356 static struct media_session *impl_from_sink_finalizer_callback_IMFAsyncCallback(IMFAsyncCallback *iface)
358 return CONTAINING_RECORD(iface, struct media_session, sink_finalizer_callback);
361 static struct media_session *impl_from_IMFGetService(IMFGetService *iface)
363 return CONTAINING_RECORD(iface, struct media_session, IMFGetService_iface);
366 static struct media_session *impl_session_from_IMFRateSupport(IMFRateSupport *iface)
368 return CONTAINING_RECORD(iface, struct media_session, IMFRateSupport_iface);
371 static struct media_session *impl_session_from_IMFRateControl(IMFRateControl *iface)
373 return CONTAINING_RECORD(iface, struct media_session, IMFRateControl_iface);
376 static struct media_session *impl_session_from_IMFTopologyNodeAttributeEditor(IMFTopologyNodeAttributeEditor *iface)
378 return CONTAINING_RECORD(iface, struct media_session, IMFTopologyNodeAttributeEditor_iface);
381 static struct session_op *impl_op_from_IUnknown(IUnknown *iface)
383 return CONTAINING_RECORD(iface, struct session_op, IUnknown_iface);
386 static struct presentation_clock *impl_from_IMFPresentationClock(IMFPresentationClock *iface)
388 return CONTAINING_RECORD(iface, struct presentation_clock, IMFPresentationClock_iface);
391 static struct presentation_clock *impl_from_IMFRateControl(IMFRateControl *iface)
393 return CONTAINING_RECORD(iface, struct presentation_clock, IMFRateControl_iface);
396 static struct presentation_clock *impl_from_IMFTimer(IMFTimer *iface)
398 return CONTAINING_RECORD(iface, struct presentation_clock, IMFTimer_iface);
401 static struct presentation_clock *impl_from_IMFShutdown(IMFShutdown *iface)
403 return CONTAINING_RECORD(iface, struct presentation_clock, IMFShutdown_iface);
406 static struct presentation_clock *impl_from_sink_callback_IMFAsyncCallback(IMFAsyncCallback *iface)
408 return CONTAINING_RECORD(iface, struct presentation_clock, sink_callback);
411 static struct presentation_clock *impl_from_timer_callback_IMFAsyncCallback(IMFAsyncCallback *iface)
413 return CONTAINING_RECORD(iface, struct presentation_clock, timer_callback);
416 static struct clock_timer *impl_clock_timer_from_IUnknown(IUnknown *iface)
418 return CONTAINING_RECORD(iface, struct clock_timer, IUnknown_iface);
421 static struct sink_notification *impl_sink_notification_from_IUnknown(IUnknown *iface)
423 return CONTAINING_RECORD(iface, struct sink_notification, IUnknown_iface);
426 static struct quality_manager *impl_from_IMFQualityManager(IMFQualityManager *iface)
428 return CONTAINING_RECORD(iface, struct quality_manager, IMFQualityManager_iface);
431 static struct quality_manager *impl_from_qm_IMFClockStateSink(IMFClockStateSink *iface)
433 return CONTAINING_RECORD(iface, struct quality_manager, IMFClockStateSink_iface);
436 static struct topo_node *impl_node_from_IMFVideoSampleAllocatorNotify(IMFVideoSampleAllocatorNotify *iface)
438 return CONTAINING_RECORD(iface, struct topo_node, u.sink.notify_cb);
441 /* IMFLocalMFTRegistration */
442 static HRESULT WINAPI local_mft_registration_QueryInterface(IMFLocalMFTRegistration *iface, REFIID riid, void **obj)
444 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), obj);
446 if (IsEqualIID(riid, &IID_IMFLocalMFTRegistration) ||
447 IsEqualIID(riid, &IID_IUnknown))
449 *obj = iface;
450 IMFLocalMFTRegistration_AddRef(iface);
451 return S_OK;
454 WARN("Unexpected %s.\n", debugstr_guid(riid));
455 *obj = NULL;
456 return E_NOINTERFACE;
459 static ULONG WINAPI local_mft_registration_AddRef(IMFLocalMFTRegistration *iface)
461 return 2;
464 static ULONG WINAPI local_mft_registration_Release(IMFLocalMFTRegistration *iface)
466 return 1;
469 static HRESULT WINAPI local_mft_registration_RegisterMFTs(IMFLocalMFTRegistration *iface, MFT_REGISTRATION_INFO *info,
470 DWORD count)
472 HRESULT hr = S_OK;
473 DWORD i;
475 TRACE("%p, %p, %u.\n", iface, info, count);
477 for (i = 0; i < count; ++i)
479 if (FAILED(hr = MFTRegisterLocalByCLSID(&info[i].clsid, &info[i].guidCategory, info[i].pszName,
480 info[i].uiFlags, info[i].cInTypes, info[i].pInTypes, info[i].cOutTypes, info[i].pOutTypes)))
482 break;
486 return hr;
489 static const IMFLocalMFTRegistrationVtbl local_mft_registration_vtbl =
491 local_mft_registration_QueryInterface,
492 local_mft_registration_AddRef,
493 local_mft_registration_Release,
494 local_mft_registration_RegisterMFTs,
497 static IMFLocalMFTRegistration local_mft_registration = { &local_mft_registration_vtbl };
499 static HRESULT WINAPI session_op_QueryInterface(IUnknown *iface, REFIID riid, void **obj)
501 if (IsEqualIID(riid, &IID_IUnknown))
503 *obj = iface;
504 IUnknown_AddRef(iface);
505 return S_OK;
508 *obj = NULL;
509 return E_NOINTERFACE;
512 static ULONG WINAPI session_op_AddRef(IUnknown *iface)
514 struct session_op *op = impl_op_from_IUnknown(iface);
515 ULONG refcount = InterlockedIncrement(&op->refcount);
517 TRACE("%p, refcount %u.\n", iface, refcount);
519 return refcount;
522 static ULONG WINAPI session_op_Release(IUnknown *iface)
524 struct session_op *op = impl_op_from_IUnknown(iface);
525 ULONG refcount = InterlockedDecrement(&op->refcount);
527 TRACE("%p, refcount %u.\n", iface, refcount);
529 if (!refcount)
531 switch (op->command)
533 case SESSION_CMD_SET_TOPOLOGY:
534 if (op->u.set_topology.topology)
535 IMFTopology_Release(op->u.set_topology.topology);
536 break;
537 case SESSION_CMD_START:
538 PropVariantClear(&op->u.start.start_position);
539 break;
540 case SESSION_CMD_QM_NOTIFY_TOPOLOGY:
541 if (op->u.notify_topology.topology)
542 IMFTopology_Release(op->u.notify_topology.topology);
543 break;
544 default:
547 heap_free(op);
550 return refcount;
553 static const IUnknownVtbl session_op_vtbl =
555 session_op_QueryInterface,
556 session_op_AddRef,
557 session_op_Release,
560 static HRESULT create_session_op(enum session_command command, struct session_op **ret)
562 struct session_op *op;
564 if (!(op = heap_alloc_zero(sizeof(*op))))
565 return E_OUTOFMEMORY;
567 op->IUnknown_iface.lpVtbl = &session_op_vtbl;
568 op->refcount = 1;
569 op->command = command;
571 *ret = op;
573 return S_OK;
576 static HRESULT session_is_shut_down(struct media_session *session)
578 return session->state == SESSION_STATE_SHUT_DOWN ? MF_E_SHUTDOWN : S_OK;
581 static void session_push_back_command(struct media_session *session, enum session_command command)
583 struct session_op *op;
585 if (SUCCEEDED(create_session_op(command, &op)))
586 list_add_head(&session->commands, &op->entry);
589 static HRESULT session_submit_command(struct media_session *session, struct session_op *op)
591 HRESULT hr;
593 EnterCriticalSection(&session->cs);
594 if (SUCCEEDED(hr = session_is_shut_down(session)))
596 if (list_empty(&session->commands))
597 hr = MFPutWorkItem(MFASYNC_CALLBACK_QUEUE_STANDARD, &session->commands_callback, &op->IUnknown_iface);
598 list_add_tail(&session->commands, &op->entry);
599 IUnknown_AddRef(&op->IUnknown_iface);
601 LeaveCriticalSection(&session->cs);
603 return hr;
606 static HRESULT session_submit_simple_command(struct media_session *session, enum session_command command)
608 struct session_op *op;
609 HRESULT hr;
611 if (FAILED(hr = create_session_op(command, &op)))
612 return hr;
614 hr = session_submit_command(session, op);
615 IUnknown_Release(&op->IUnknown_iface);
616 return hr;
619 static void session_clear_topologies(struct media_session *session)
621 struct queued_topology *ptr, *next;
623 LIST_FOR_EACH_ENTRY_SAFE(ptr, next, &session->topologies, struct queued_topology, entry)
625 list_remove(&ptr->entry);
626 IMFTopology_Release(ptr->topology);
627 heap_free(ptr);
631 static void session_set_topo_status(struct media_session *session, HRESULT status,
632 MF_TOPOSTATUS topo_status)
634 IMFMediaEvent *event;
635 PROPVARIANT param;
637 if (topo_status == MF_TOPOSTATUS_INVALID)
638 return;
640 if (list_empty(&session->topologies))
642 FIXME("Unexpectedly empty topology queue.\n");
643 return;
646 if (topo_status > session->presentation.topo_status)
648 struct queued_topology *topology = LIST_ENTRY(list_head(&session->topologies), struct queued_topology, entry);
650 param.vt = VT_UNKNOWN;
651 param.punkVal = (IUnknown *)topology->topology;
653 if (FAILED(MFCreateMediaEvent(MESessionTopologyStatus, &GUID_NULL, status, &param, &event)))
654 return;
656 session->presentation.topo_status = topo_status;
658 IMFMediaEvent_SetUINT32(event, &MF_EVENT_TOPOLOGY_STATUS, topo_status);
659 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
660 IMFMediaEvent_Release(event);
664 static HRESULT session_bind_output_nodes(IMFTopology *topology)
666 MF_TOPOLOGY_TYPE node_type;
667 IMFStreamSink *stream_sink;
668 IMFMediaSink *media_sink;
669 WORD node_count = 0, i;
670 IMFTopologyNode *node;
671 IMFActivate *activate;
672 UINT32 stream_id;
673 IUnknown *object;
674 HRESULT hr;
676 hr = IMFTopology_GetNodeCount(topology, &node_count);
678 for (i = 0; i < node_count; ++i)
680 if (FAILED(hr = IMFTopology_GetNode(topology, i, &node)))
681 break;
683 if (FAILED(hr = IMFTopologyNode_GetNodeType(node, &node_type)) || node_type != MF_TOPOLOGY_OUTPUT_NODE)
685 IMFTopologyNode_Release(node);
686 continue;
689 if (SUCCEEDED(hr = IMFTopologyNode_GetObject(node, &object)))
691 stream_sink = NULL;
692 if (FAILED(IUnknown_QueryInterface(object, &IID_IMFStreamSink, (void **)&stream_sink)))
694 if (SUCCEEDED(hr = IUnknown_QueryInterface(object, &IID_IMFActivate, (void **)&activate)))
696 if (SUCCEEDED(hr = IMFActivate_ActivateObject(activate, &IID_IMFMediaSink, (void **)&media_sink)))
698 if (FAILED(IMFTopologyNode_GetUINT32(node, &MF_TOPONODE_STREAMID, &stream_id)))
699 stream_id = 0;
701 stream_sink = NULL;
702 if (FAILED(IMFMediaSink_GetStreamSinkById(media_sink, stream_id, &stream_sink)))
703 hr = IMFMediaSink_AddStreamSink(media_sink, stream_id, NULL, &stream_sink);
705 if (stream_sink)
706 hr = IMFTopologyNode_SetObject(node, (IUnknown *)stream_sink);
708 IMFMediaSink_Release(media_sink);
711 if (SUCCEEDED(hr))
712 IMFTopologyNode_SetUnknown(node, &_MF_TOPONODE_IMFActivate, (IUnknown *)activate);
714 IMFActivate_Release(activate);
718 if (stream_sink)
719 IMFStreamSink_Release(stream_sink);
720 IUnknown_Release(object);
723 IMFTopologyNode_Release(node);
726 return hr;
729 static void session_set_caps(struct media_session *session, DWORD caps)
731 DWORD delta = session->caps ^ caps;
732 IMFMediaEvent *event;
734 /* Delta is documented to reflect rate value changes as well, but it's not clear what to compare
735 them to, since session always queries for current object rates. */
736 if (!delta)
737 return;
739 session->caps = caps;
741 if (FAILED(MFCreateMediaEvent(MESessionCapabilitiesChanged, &GUID_NULL, S_OK, NULL, &event)))
742 return;
744 IMFMediaEvent_SetUINT32(event, &MF_EVENT_SESSIONCAPS, caps);
745 IMFMediaEvent_SetUINT32(event, &MF_EVENT_SESSIONCAPS_DELTA, delta);
747 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
748 IMFMediaEvent_Release(event);
751 static void transform_release_sample(struct sample *sample)
753 list_remove(&sample->entry);
754 if (sample->sample)
755 IMFSample_Release(sample->sample);
756 heap_free(sample);
759 static void transform_stream_drop_samples(struct transform_stream *stream)
761 struct sample *sample, *sample2;
763 LIST_FOR_EACH_ENTRY_SAFE(sample, sample2, &stream->samples, struct sample, entry)
764 transform_release_sample(sample);
767 static void release_topo_node(struct topo_node *node)
769 unsigned int i;
771 switch (node->type)
773 case MF_TOPOLOGY_SOURCESTREAM_NODE:
774 if (node->u.source.source)
775 IMFMediaSource_Release(node->u.source.source);
776 break;
777 case MF_TOPOLOGY_TRANSFORM_NODE:
778 for (i = 0; i < node->u.transform.input_count; ++i)
779 transform_stream_drop_samples(&node->u.transform.inputs[i]);
780 for (i = 0; i < node->u.transform.output_count; ++i)
781 transform_stream_drop_samples(&node->u.transform.outputs[i]);
782 heap_free(node->u.transform.inputs);
783 heap_free(node->u.transform.outputs);
784 heap_free(node->u.transform.input_map);
785 heap_free(node->u.transform.output_map);
786 break;
787 case MF_TOPOLOGY_OUTPUT_NODE:
788 if (node->u.sink.allocator)
789 IMFVideoSampleAllocator_Release(node->u.sink.allocator);
790 if (node->u.sink.allocator_cb)
792 IMFVideoSampleAllocatorCallback_SetCallback(node->u.sink.allocator_cb, NULL);
793 IMFVideoSampleAllocatorCallback_Release(node->u.sink.allocator_cb);
795 break;
796 default:
800 if (node->object.object)
801 IUnknown_Release(node->object.object);
802 if (node->node)
803 IMFTopologyNode_Release(node->node);
804 heap_free(node);
807 static void session_shutdown_current_topology(struct media_session *session)
809 unsigned int shutdown, force_shutdown;
810 MF_TOPOLOGY_TYPE node_type;
811 IMFStreamSink *stream_sink;
812 IMFTopology *topology;
813 IMFTopologyNode *node;
814 IMFActivate *activate;
815 IMFMediaSink *sink;
816 WORD idx = 0;
817 HRESULT hr;
819 topology = session->presentation.current_topology;
820 force_shutdown = session->state == SESSION_STATE_SHUT_DOWN;
822 /* FIXME: should handle async MFTs, but these are not supported by the rest of the pipeline currently. */
824 while (SUCCEEDED(IMFTopology_GetNode(topology, idx++, &node)))
826 if (SUCCEEDED(IMFTopologyNode_GetNodeType(node, &node_type)) &&
827 node_type == MF_TOPOLOGY_OUTPUT_NODE)
829 shutdown = 1;
830 IMFTopologyNode_GetUINT32(node, &MF_TOPONODE_NOSHUTDOWN_ON_REMOVE, &shutdown);
832 if (force_shutdown || shutdown)
834 if (SUCCEEDED(IMFTopologyNode_GetUnknown(node, &_MF_TOPONODE_IMFActivate, &IID_IMFActivate,
835 (void **)&activate)))
837 if (FAILED(hr = IMFActivate_ShutdownObject(activate)))
838 WARN("Failed to shut down activation object for the sink, hr %#x.\n", hr);
839 IMFActivate_Release(activate);
841 else if (SUCCEEDED(topology_node_get_object(node, &IID_IMFStreamSink, (void **)&stream_sink)))
843 if (SUCCEEDED(IMFStreamSink_GetMediaSink(stream_sink, &sink)))
845 IMFMediaSink_Shutdown(sink);
846 IMFMediaSink_Release(sink);
849 IMFStreamSink_Release(stream_sink);
854 IMFTopologyNode_Release(node);
858 static void session_clear_presentation(struct media_session *session)
860 struct media_source *source, *source2;
861 struct media_sink *sink, *sink2;
862 struct topo_node *node, *node2;
863 struct session_op *op, *op2;
865 session_shutdown_current_topology(session);
867 IMFTopology_Clear(session->presentation.current_topology);
868 session->presentation.topo_status = MF_TOPOSTATUS_INVALID;
870 LIST_FOR_EACH_ENTRY_SAFE(source, source2, &session->presentation.sources, struct media_source, entry)
872 list_remove(&source->entry);
873 if (source->source)
874 IMFMediaSource_Release(source->source);
875 if (source->pd)
876 IMFPresentationDescriptor_Release(source->pd);
877 heap_free(source);
880 LIST_FOR_EACH_ENTRY_SAFE(node, node2, &session->presentation.nodes, struct topo_node, entry)
882 list_remove(&node->entry);
883 release_topo_node(node);
886 LIST_FOR_EACH_ENTRY_SAFE(sink, sink2, &session->presentation.sinks, struct media_sink, entry)
888 list_remove(&sink->entry);
890 if (sink->sink)
891 IMFMediaSink_Release(sink->sink);
892 if (sink->preroll)
893 IMFMediaSinkPreroll_Release(sink->preroll);
894 if (sink->event_generator)
895 IMFMediaEventGenerator_Release(sink->event_generator);
896 heap_free(sink);
899 LIST_FOR_EACH_ENTRY_SAFE(op, op2, &session->commands, struct session_op, entry)
901 list_remove(&op->entry);
902 IUnknown_Release(&op->IUnknown_iface);
906 static struct topo_node *session_get_node_by_id(const struct media_session *session, TOPOID id)
908 struct topo_node *node;
910 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
912 if (node->node_id == id)
913 return node;
916 return NULL;
919 static void session_start(struct media_session *session, const GUID *time_format, const PROPVARIANT *start_position)
921 struct media_source *source;
922 HRESULT hr;
924 switch (session->state)
926 case SESSION_STATE_STOPPED:
927 case SESSION_STATE_PAUSED:
929 session->presentation.time_format = *time_format;
930 session->presentation.start_position.vt = VT_EMPTY;
931 PropVariantCopy(&session->presentation.start_position, start_position);
933 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
935 if (!(session->presentation.flags & SESSION_FLAG_SOURCES_SUBSCRIBED))
937 if (FAILED(hr = IMFMediaSource_BeginGetEvent(source->source, &session->events_callback,
938 (IUnknown *)source->source)))
940 WARN("Failed to subscribe to source events, hr %#x.\n", hr);
944 if (FAILED(hr = IMFMediaSource_Start(source->source, source->pd, &GUID_NULL, start_position)))
945 WARN("Failed to start media source %p, hr %#x.\n", source->source, hr);
948 session->presentation.flags |= SESSION_FLAG_SOURCES_SUBSCRIBED;
949 session->state = SESSION_STATE_STARTING_SOURCES;
950 break;
951 case SESSION_STATE_STARTED:
952 FIXME("Seeking is not implemented.\n");
953 break;
954 case SESSION_STATE_CLOSED:
955 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MESessionStarted, &GUID_NULL,
956 MF_E_INVALIDREQUEST, NULL);
957 break;
958 default:
963 static void session_command_complete(struct media_session *session)
965 struct session_op *op;
966 struct list *e;
968 /* Pop current command, submit next. */
969 if ((e = list_head(&session->commands)))
971 op = LIST_ENTRY(e, struct session_op, entry);
972 list_remove(&op->entry);
973 IUnknown_Release(&op->IUnknown_iface);
976 if ((e = list_head(&session->commands)))
978 op = LIST_ENTRY(e, struct session_op, entry);
979 MFPutWorkItem(MFASYNC_CALLBACK_QUEUE_STANDARD, &session->commands_callback, &op->IUnknown_iface);
983 static void session_set_started(struct media_session *session)
985 struct media_source *source;
986 unsigned int caps, flags;
987 IMFMediaEvent *event;
989 session->state = SESSION_STATE_STARTED;
991 caps = session->caps | MFSESSIONCAP_PAUSE;
993 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
995 if (SUCCEEDED(IMFMediaSource_GetCharacteristics(source->source, &flags)))
997 if (!(flags & MFMEDIASOURCE_CAN_PAUSE))
999 caps &= ~MFSESSIONCAP_PAUSE;
1000 break;
1005 session_set_caps(session, caps);
1007 if (SUCCEEDED(MFCreateMediaEvent(MESessionStarted, &GUID_NULL, S_OK, NULL, &event)))
1009 IMFMediaEvent_SetUINT64(event, &MF_EVENT_PRESENTATION_TIME_OFFSET, 0);
1010 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
1011 IMFMediaEvent_Release(event);
1013 session_command_complete(session);
1016 static void session_set_paused(struct media_session *session, HRESULT status)
1018 session->state = SESSION_STATE_PAUSED;
1019 if (SUCCEEDED(status))
1020 session_set_caps(session, session->caps & ~MFSESSIONCAP_PAUSE);
1021 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MESessionPaused, &GUID_NULL, status, NULL);
1022 session_command_complete(session);
1025 static void session_set_closed(struct media_session *session, HRESULT status)
1027 session->state = SESSION_STATE_CLOSED;
1028 if (SUCCEEDED(status))
1029 session_set_caps(session, session->caps & ~(MFSESSIONCAP_START | MFSESSIONCAP_SEEK));
1030 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MESessionClosed, &GUID_NULL, status, NULL);
1031 session_command_complete(session);
1034 static void session_pause(struct media_session *session)
1036 HRESULT hr;
1038 switch (session->state)
1040 case SESSION_STATE_STARTED:
1042 /* Transition in two steps - pause the clock, wait for sinks, then pause sources. */
1043 if (SUCCEEDED(hr = IMFPresentationClock_Pause(session->clock)))
1044 session->state = SESSION_STATE_PAUSING_SINKS;
1046 break;
1047 default:
1048 hr = MF_E_INVALIDREQUEST;
1051 if (FAILED(hr))
1052 session_set_paused(session, hr);
1055 static void session_set_stopped(struct media_session *session, HRESULT status)
1057 MediaEventType event_type;
1058 IMFMediaEvent *event;
1060 session->state = SESSION_STATE_STOPPED;
1061 event_type = session->presentation.flags & SESSION_FLAG_END_OF_PRESENTATION ? MESessionEnded : MESessionStopped;
1063 if (SUCCEEDED(MFCreateMediaEvent(event_type, &GUID_NULL, status, NULL, &event)))
1065 IMFMediaEvent_SetUINT64(event, &MF_SESSION_APPROX_EVENT_OCCURRENCE_TIME, session->presentation.clock_stop_time);
1066 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
1067 IMFMediaEvent_Release(event);
1069 session_command_complete(session);
1072 static void session_stop(struct media_session *session)
1074 HRESULT hr = MF_E_INVALIDREQUEST;
1076 switch (session->state)
1078 case SESSION_STATE_STARTED:
1079 case SESSION_STATE_PAUSED:
1081 /* Transition in two steps - stop the clock, wait for sinks, then stop sources. */
1082 IMFPresentationClock_GetTime(session->clock, &session->presentation.clock_stop_time);
1083 if (SUCCEEDED(hr = IMFPresentationClock_Stop(session->clock)))
1084 session->state = SESSION_STATE_STOPPING_SINKS;
1085 else
1086 session_set_stopped(session, hr);
1088 break;
1089 case SESSION_STATE_STOPPED:
1090 hr = S_OK;
1091 /* fallthrough */
1092 default:
1093 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MESessionStopped, &GUID_NULL, hr, NULL);
1094 session_command_complete(session);
1095 break;
1099 static HRESULT session_finalize_sinks(struct media_session *session)
1101 IMFFinalizableMediaSink *fin_sink;
1102 BOOL sinks_finalized = TRUE;
1103 struct media_sink *sink;
1104 HRESULT hr = S_OK;
1106 session->presentation.flags &= ~SESSION_FLAG_FINALIZE_SINKS;
1107 session->state = SESSION_STATE_FINALIZING_SINKS;
1109 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
1111 if (SUCCEEDED(IMFMediaSink_QueryInterface(sink->sink, &IID_IMFFinalizableMediaSink, (void **)&fin_sink)))
1113 hr = IMFFinalizableMediaSink_BeginFinalize(fin_sink, &session->sink_finalizer_callback,
1114 (IUnknown *)fin_sink);
1115 IMFFinalizableMediaSink_Release(fin_sink);
1116 if (FAILED(hr))
1117 break;
1118 sinks_finalized = FALSE;
1120 else
1121 sink->finalized = TRUE;
1124 if (sinks_finalized)
1125 session_set_closed(session, hr);
1127 return hr;
1130 static void session_close(struct media_session *session)
1132 HRESULT hr = S_OK;
1134 switch (session->state)
1136 case SESSION_STATE_STOPPED:
1137 hr = session_finalize_sinks(session);
1138 break;
1139 case SESSION_STATE_STARTED:
1140 case SESSION_STATE_PAUSED:
1141 session->presentation.flags |= SESSION_FLAG_FINALIZE_SINKS;
1142 if (SUCCEEDED(hr = IMFPresentationClock_Stop(session->clock)))
1143 session->state = SESSION_STATE_STOPPING_SINKS;
1144 break;
1145 default:
1146 hr = MF_E_INVALIDREQUEST;
1147 break;
1150 if (FAILED(hr))
1151 session_set_closed(session, hr);
1154 static struct media_source *session_get_media_source(struct media_session *session, IMFMediaSource *source)
1156 struct media_source *cur;
1158 LIST_FOR_EACH_ENTRY(cur, &session->presentation.sources, struct media_source, entry)
1160 if (source == cur->source)
1161 return cur;
1164 return NULL;
1167 static void session_release_media_source(struct media_source *source)
1169 IMFMediaSource_Release(source->source);
1170 if (source->pd)
1171 IMFPresentationDescriptor_Release(source->pd);
1172 heap_free(source);
1175 static HRESULT session_add_media_source(struct media_session *session, IMFTopologyNode *node, IMFMediaSource *source)
1177 struct media_source *media_source;
1178 HRESULT hr;
1180 if (session_get_media_source(session, source))
1181 return S_FALSE;
1183 if (!(media_source = heap_alloc_zero(sizeof(*media_source))))
1184 return E_OUTOFMEMORY;
1186 media_source->source = source;
1187 IMFMediaSource_AddRef(media_source->source);
1189 hr = IMFTopologyNode_GetUnknown(node, &MF_TOPONODE_PRESENTATION_DESCRIPTOR, &IID_IMFPresentationDescriptor,
1190 (void **)&media_source->pd);
1192 if (SUCCEEDED(hr))
1193 list_add_tail(&session->presentation.sources, &media_source->entry);
1194 else
1195 session_release_media_source(media_source);
1197 return hr;
1200 static void session_raise_topology_set(struct media_session *session, IMFTopology *topology, HRESULT status)
1202 PROPVARIANT param;
1204 param.vt = topology ? VT_UNKNOWN : VT_EMPTY;
1205 param.punkVal = (IUnknown *)topology;
1207 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MESessionTopologySet, &GUID_NULL, status, &param);
1210 static DWORD session_get_object_rate_caps(IUnknown *object)
1212 IMFRateSupport *rate_support;
1213 DWORD caps = 0;
1214 float rate;
1216 if (SUCCEEDED(MFGetService(object, &MF_RATE_CONTROL_SERVICE, &IID_IMFRateSupport, (void **)&rate_support)))
1218 rate = 0.0f;
1219 if (SUCCEEDED(IMFRateSupport_GetFastestRate(rate_support, MFRATE_FORWARD, TRUE, &rate)) && rate != 0.0f)
1220 caps |= MFSESSIONCAP_RATE_FORWARD;
1222 rate = 0.0f;
1223 if (SUCCEEDED(IMFRateSupport_GetFastestRate(rate_support, MFRATE_REVERSE, TRUE, &rate)) && rate != 0.0f)
1224 caps |= MFSESSIONCAP_RATE_REVERSE;
1226 IMFRateSupport_Release(rate_support);
1229 return caps;
1232 static HRESULT session_add_media_sink(struct media_session *session, IMFTopologyNode *node, IMFMediaSink *sink)
1234 struct media_sink *media_sink;
1235 unsigned int disable_preroll = 0;
1236 DWORD flags;
1238 LIST_FOR_EACH_ENTRY(media_sink, &session->presentation.sinks, struct media_sink, entry)
1240 if (sink == media_sink->sink)
1241 return S_FALSE;
1244 if (!(media_sink = heap_alloc_zero(sizeof(*media_sink))))
1245 return E_OUTOFMEMORY;
1247 media_sink->sink = sink;
1248 IMFMediaSink_AddRef(media_sink->sink);
1250 IMFMediaSink_QueryInterface(media_sink->sink, &IID_IMFMediaEventGenerator, (void **)&media_sink->event_generator);
1252 IMFTopologyNode_GetUINT32(node, &MF_TOPONODE_DISABLE_PREROLL, &disable_preroll);
1253 if (SUCCEEDED(IMFMediaSink_GetCharacteristics(sink, &flags)) && flags & MEDIASINK_CAN_PREROLL && !disable_preroll)
1255 if (SUCCEEDED(IMFMediaSink_QueryInterface(media_sink->sink, &IID_IMFMediaSinkPreroll, (void **)&media_sink->preroll)))
1256 session->presentation.flags |= SESSION_FLAG_NEEDS_PREROLL;
1259 list_add_tail(&session->presentation.sinks, &media_sink->entry);
1261 return S_OK;
1264 static unsigned int transform_node_get_stream_id(struct topo_node *node, BOOL output, unsigned int index)
1266 unsigned int *map = output ? node->u.transform.output_map : node->u.transform.input_map;
1267 return map ? map[index] : index;
1270 static HRESULT session_set_transform_stream_info(struct topo_node *node)
1272 unsigned int *input_map = NULL, *output_map = NULL;
1273 unsigned int i, input_count, output_count, block_alignment;
1274 struct transform_stream *streams;
1275 IMFMediaType *media_type;
1276 GUID major = { 0 };
1277 HRESULT hr;
1279 hr = IMFTransform_GetStreamCount(node->object.transform, &input_count, &output_count);
1280 if (SUCCEEDED(hr) && (input_count > 1 || output_count > 1))
1282 input_map = heap_calloc(input_count, sizeof(*input_map));
1283 output_map = heap_calloc(output_count, sizeof(*output_map));
1284 if (FAILED(IMFTransform_GetStreamIDs(node->object.transform, input_count, input_map,
1285 output_count, output_map)))
1287 /* Assume sequential identifiers. */
1288 heap_free(input_map);
1289 heap_free(output_map);
1290 input_map = output_map = NULL;
1294 if (SUCCEEDED(hr))
1296 node->u.transform.input_map = input_map;
1297 node->u.transform.output_map = output_map;
1299 streams = heap_calloc(input_count, sizeof(*streams));
1300 for (i = 0; i < input_count; ++i)
1301 list_init(&streams[i].samples);
1302 node->u.transform.inputs = streams;
1303 node->u.transform.input_count = input_count;
1305 streams = heap_calloc(output_count, sizeof(*streams));
1306 for (i = 0; i < output_count; ++i)
1308 list_init(&streams[i].samples);
1310 if (SUCCEEDED(IMFTransform_GetOutputCurrentType(node->object.transform,
1311 transform_node_get_stream_id(node, TRUE, i), &media_type)))
1313 if (SUCCEEDED(IMFMediaType_GetMajorType(media_type, &major)) && IsEqualGUID(&major, &MFMediaType_Audio)
1314 && SUCCEEDED(IMFMediaType_GetUINT32(media_type, &MF_MT_AUDIO_BLOCK_ALIGNMENT, &block_alignment)))
1316 streams[i].min_buffer_size = block_alignment;
1318 IMFMediaType_Release(media_type);
1321 node->u.transform.outputs = streams;
1322 node->u.transform.output_count = output_count;
1325 return hr;
1328 static HRESULT session_get_stream_sink_type(IMFStreamSink *sink, IMFMediaType **media_type)
1330 IMFMediaTypeHandler *handler;
1331 HRESULT hr;
1333 if (SUCCEEDED(hr = IMFStreamSink_GetMediaTypeHandler(sink, &handler)))
1335 hr = IMFMediaTypeHandler_GetCurrentMediaType(handler, media_type);
1336 IMFMediaTypeHandler_Release(handler);
1339 return hr;
1342 static HRESULT WINAPI node_sample_allocator_cb_QueryInterface(IMFVideoSampleAllocatorNotify *iface,
1343 REFIID riid, void **obj)
1345 if (IsEqualIID(riid, &IID_IMFVideoSampleAllocatorNotify) ||
1346 IsEqualIID(riid, &IID_IUnknown))
1348 *obj = iface;
1349 IMFVideoSampleAllocatorNotify_AddRef(iface);
1350 return S_OK;
1353 *obj = NULL;
1354 return E_NOINTERFACE;
1357 static ULONG WINAPI node_sample_allocator_cb_AddRef(IMFVideoSampleAllocatorNotify *iface)
1359 return 2;
1362 static ULONG WINAPI node_sample_allocator_cb_Release(IMFVideoSampleAllocatorNotify *iface)
1364 return 1;
1367 static HRESULT session_request_sample_from_node(struct media_session *session, IMFTopologyNode *node, DWORD output);
1369 static HRESULT WINAPI node_sample_allocator_cb_NotifyRelease(IMFVideoSampleAllocatorNotify *iface)
1371 struct topo_node *topo_node = impl_node_from_IMFVideoSampleAllocatorNotify(iface);
1372 struct session_op *op;
1374 if (SUCCEEDED(create_session_op(SESSION_CMD_SA_READY, &op)))
1376 op->u.sa_ready.node_id = topo_node->node_id;
1377 MFPutWorkItem(MFASYNC_CALLBACK_QUEUE_STANDARD, &topo_node->session->commands_callback, &op->IUnknown_iface);
1378 IUnknown_Release(&op->IUnknown_iface);
1381 return S_OK;
1384 static const IMFVideoSampleAllocatorNotifyVtbl node_sample_allocator_cb_vtbl =
1386 node_sample_allocator_cb_QueryInterface,
1387 node_sample_allocator_cb_AddRef,
1388 node_sample_allocator_cb_Release,
1389 node_sample_allocator_cb_NotifyRelease,
1392 static HRESULT session_append_node(struct media_session *session, IMFTopologyNode *node)
1394 struct topo_node *topo_node;
1395 IMFMediaSink *media_sink;
1396 IMFMediaType *media_type;
1397 IMFStreamDescriptor *sd;
1398 HRESULT hr = S_OK;
1400 if (!(topo_node = heap_alloc_zero(sizeof(*topo_node))))
1401 return E_OUTOFMEMORY;
1403 IMFTopologyNode_GetNodeType(node, &topo_node->type);
1404 IMFTopologyNode_GetTopoNodeID(node, &topo_node->node_id);
1405 topo_node->node = node;
1406 IMFTopologyNode_AddRef(topo_node->node);
1407 topo_node->session = session;
1409 switch (topo_node->type)
1411 case MF_TOPOLOGY_OUTPUT_NODE:
1412 topo_node->u.sink.notify_cb.lpVtbl = &node_sample_allocator_cb_vtbl;
1414 if (FAILED(hr = topology_node_get_object(node, &IID_IMFStreamSink, (void **)&topo_node->object.object)))
1416 WARN("Failed to get stream sink interface, hr %#x.\n", hr);
1417 break;
1420 if (FAILED(hr = IMFStreamSink_GetMediaSink(topo_node->object.sink_stream, &media_sink)))
1421 break;
1423 if (SUCCEEDED(hr = session_add_media_sink(session, node, media_sink)))
1425 if (SUCCEEDED(session_get_stream_sink_type(topo_node->object.sink_stream, &media_type)))
1427 if (SUCCEEDED(MFGetService(topo_node->object.object, &MR_VIDEO_ACCELERATION_SERVICE,
1428 &IID_IMFVideoSampleAllocator, (void **)&topo_node->u.sink.allocator)))
1430 if (FAILED(hr = IMFVideoSampleAllocator_InitializeSampleAllocator(topo_node->u.sink.allocator,
1431 2, media_type)))
1433 WARN("Failed to initialize sample allocator for the stream, hr %#x.\n", hr);
1435 IMFVideoSampleAllocator_QueryInterface(topo_node->u.sink.allocator,
1436 &IID_IMFVideoSampleAllocatorCallback, (void **)&topo_node->u.sink.allocator_cb);
1437 IMFVideoSampleAllocatorCallback_SetCallback(topo_node->u.sink.allocator_cb,
1438 &topo_node->u.sink.notify_cb);
1440 IMFMediaType_Release(media_type);
1443 IMFMediaSink_Release(media_sink);
1445 break;
1446 case MF_TOPOLOGY_SOURCESTREAM_NODE:
1447 if (FAILED(IMFTopologyNode_GetUnknown(node, &MF_TOPONODE_SOURCE, &IID_IMFMediaSource,
1448 (void **)&topo_node->u.source.source)))
1450 WARN("Missing MF_TOPONODE_SOURCE, hr %#x.\n", hr);
1451 break;
1454 if (FAILED(hr = session_add_media_source(session, node, topo_node->u.source.source)))
1455 break;
1457 if (FAILED(hr = IMFTopologyNode_GetUnknown(node, &MF_TOPONODE_STREAM_DESCRIPTOR,
1458 &IID_IMFStreamDescriptor, (void **)&sd)))
1460 WARN("Missing MF_TOPONODE_STREAM_DESCRIPTOR, hr %#x.\n", hr);
1461 break;
1464 hr = IMFStreamDescriptor_GetStreamIdentifier(sd, &topo_node->u.source.stream_id);
1465 IMFStreamDescriptor_Release(sd);
1467 break;
1468 case MF_TOPOLOGY_TRANSFORM_NODE:
1470 if (SUCCEEDED(hr = topology_node_get_object(node, &IID_IMFTransform, (void **)&topo_node->object.transform)))
1472 hr = session_set_transform_stream_info(topo_node);
1474 else
1475 WARN("Failed to get IMFTransform for MFT node, hr %#x.\n", hr);
1477 break;
1478 case MF_TOPOLOGY_TEE_NODE:
1479 FIXME("Unsupported node type %d.\n", topo_node->type);
1481 break;
1482 default:
1486 if (SUCCEEDED(hr))
1487 list_add_tail(&session->presentation.nodes, &topo_node->entry);
1488 else
1489 release_topo_node(topo_node);
1491 return hr;
1494 static HRESULT session_collect_nodes(struct media_session *session)
1496 IMFTopology *topology = session->presentation.current_topology;
1497 IMFTopologyNode *node;
1498 WORD i, count = 0;
1499 HRESULT hr;
1501 if (!list_empty(&session->presentation.nodes))
1502 return S_OK;
1504 if (FAILED(hr = IMFTopology_GetNodeCount(topology, &count)))
1505 return hr;
1507 for (i = 0; i < count; ++i)
1509 if (FAILED(hr = IMFTopology_GetNode(topology, i, &node)))
1511 WARN("Failed to get node %u.\n", i);
1512 break;
1515 hr = session_append_node(session, node);
1516 IMFTopologyNode_Release(node);
1517 if (FAILED(hr))
1519 WARN("Failed to add node %u.\n", i);
1520 break;
1524 return hr;
1527 static HRESULT session_set_current_topology(struct media_session *session, IMFTopology *topology)
1529 struct media_source *source;
1530 DWORD caps, object_flags;
1531 struct media_sink *sink;
1532 struct topo_node *node;
1533 struct session_op *op;
1534 IMFMediaEvent *event;
1535 HRESULT hr;
1537 if (session->quality_manager)
1539 if (SUCCEEDED(create_session_op(SESSION_CMD_QM_NOTIFY_TOPOLOGY, &op)))
1541 op->u.notify_topology.topology = topology;
1542 IMFTopology_AddRef(op->u.notify_topology.topology);
1543 session_submit_command(session, op);
1544 IUnknown_Release(&op->IUnknown_iface);
1548 if (FAILED(hr = IMFTopology_CloneFrom(session->presentation.current_topology, topology)))
1550 WARN("Failed to clone topology, hr %#x.\n", hr);
1551 return hr;
1554 session_collect_nodes(session);
1556 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
1558 if (node->type == MF_TOPOLOGY_TRANSFORM_NODE)
1560 if (FAILED(hr = IMFTransform_ProcessMessage(node->object.transform,
1561 MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0)))
1562 return hr;
1566 /* FIXME: attributes are all zero for now */
1567 if (SUCCEEDED(MFCreateMediaEvent(MESessionNotifyPresentationTime, &GUID_NULL, S_OK, NULL, &event)))
1569 IMFMediaEvent_SetUINT64(event, &MF_EVENT_START_PRESENTATION_TIME, 0);
1570 IMFMediaEvent_SetUINT64(event, &MF_EVENT_PRESENTATION_TIME_OFFSET, 0);
1571 IMFMediaEvent_SetUINT64(event, &MF_EVENT_START_PRESENTATION_TIME_AT_OUTPUT, 0);
1573 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
1574 IMFMediaEvent_Release(event);
1577 /* Update session caps. */
1578 caps = MFSESSIONCAP_START | MFSESSIONCAP_SEEK | MFSESSIONCAP_RATE_FORWARD | MFSESSIONCAP_RATE_REVERSE |
1579 MFSESSIONCAP_DOES_NOT_USE_NETWORK;
1581 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
1583 if (!caps)
1584 break;
1586 object_flags = 0;
1587 if (SUCCEEDED(IMFMediaSource_GetCharacteristics(source->source, &object_flags)))
1589 if (!(object_flags & MFMEDIASOURCE_DOES_NOT_USE_NETWORK))
1590 caps &= ~MFSESSIONCAP_DOES_NOT_USE_NETWORK;
1591 if (!(object_flags & MFMEDIASOURCE_CAN_SEEK))
1592 caps &= ~MFSESSIONCAP_SEEK;
1595 /* Mask unsupported rate caps. */
1597 caps &= session_get_object_rate_caps((IUnknown *)source->source)
1598 | ~(MFSESSIONCAP_RATE_FORWARD | MFSESSIONCAP_RATE_REVERSE);
1601 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
1603 if (!caps)
1604 break;
1606 object_flags = 0;
1607 if (SUCCEEDED(IMFMediaSink_GetCharacteristics(sink->sink, &object_flags)))
1609 if (!(object_flags & MEDIASINK_RATELESS))
1610 caps &= session_get_object_rate_caps((IUnknown *)sink->sink)
1611 | ~(MFSESSIONCAP_RATE_FORWARD | MFSESSIONCAP_RATE_REVERSE);
1615 session_set_caps(session, caps);
1617 return S_OK;
1620 static void session_set_topology(struct media_session *session, DWORD flags, IMFTopology *topology)
1622 IMFTopology *resolved_topology = NULL;
1623 HRESULT hr = S_OK;
1625 /* Resolve unless claimed to be full. */
1626 if (!(flags & MFSESSION_SETTOPOLOGY_CLEAR_CURRENT) && topology)
1628 if (!(flags & MFSESSION_SETTOPOLOGY_NORESOLUTION))
1630 hr = session_bind_output_nodes(topology);
1632 if (SUCCEEDED(hr))
1633 hr = IMFTopoLoader_Load(session->topo_loader, topology, &resolved_topology, NULL /* FIXME? */);
1635 if (SUCCEEDED(hr))
1637 topology = resolved_topology;
1642 if (flags & MFSESSION_SETTOPOLOGY_CLEAR_CURRENT)
1644 if ((topology && topology == session->presentation.current_topology) || !topology)
1646 /* FIXME: stop current topology, queue next one. */
1647 session_clear_presentation(session);
1649 else
1650 hr = S_FALSE;
1652 topology = NULL;
1654 else if (topology && flags & MFSESSION_SETTOPOLOGY_IMMEDIATE)
1656 session_clear_topologies(session);
1657 session_clear_presentation(session);
1660 session_raise_topology_set(session, topology, hr);
1662 /* With no current topology set it right away, otherwise queue. */
1663 if (topology)
1665 struct queued_topology *queued_topology;
1667 if ((queued_topology = heap_alloc_zero(sizeof(*queued_topology))))
1669 queued_topology->topology = topology;
1670 IMFTopology_AddRef(queued_topology->topology);
1672 list_add_tail(&session->topologies, &queued_topology->entry);
1675 if (session->presentation.topo_status == MF_TOPOSTATUS_INVALID)
1677 hr = session_set_current_topology(session, topology);
1678 session_set_topo_status(session, hr, MF_TOPOSTATUS_READY);
1682 if (resolved_topology)
1683 IMFTopology_Release(resolved_topology);
1686 static HRESULT WINAPI mfsession_QueryInterface(IMFMediaSession *iface, REFIID riid, void **out)
1688 struct media_session *session = impl_from_IMFMediaSession(iface);
1690 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
1692 if (IsEqualIID(riid, &IID_IMFMediaSession) ||
1693 IsEqualIID(riid, &IID_IMFMediaEventGenerator) ||
1694 IsEqualIID(riid, &IID_IUnknown))
1696 *out = &session->IMFMediaSession_iface;
1697 IMFMediaSession_AddRef(iface);
1698 return S_OK;
1700 else if (IsEqualIID(riid, &IID_IMFGetService))
1702 *out = &session->IMFGetService_iface;
1703 IMFMediaSession_AddRef(iface);
1704 return S_OK;
1707 WARN("Unsupported %s.\n", debugstr_guid(riid));
1708 *out = NULL;
1709 return E_NOINTERFACE;
1712 static ULONG WINAPI mfsession_AddRef(IMFMediaSession *iface)
1714 struct media_session *session = impl_from_IMFMediaSession(iface);
1715 ULONG refcount = InterlockedIncrement(&session->refcount);
1717 TRACE("%p, refcount %u.\n", iface, refcount);
1719 return refcount;
1722 static ULONG WINAPI mfsession_Release(IMFMediaSession *iface)
1724 struct media_session *session = impl_from_IMFMediaSession(iface);
1725 ULONG refcount = InterlockedDecrement(&session->refcount);
1727 TRACE("%p, refcount %u.\n", iface, refcount);
1729 if (!refcount)
1731 session_clear_topologies(session);
1732 session_clear_presentation(session);
1733 if (session->presentation.current_topology)
1734 IMFTopology_Release(session->presentation.current_topology);
1735 if (session->event_queue)
1736 IMFMediaEventQueue_Release(session->event_queue);
1737 if (session->clock)
1738 IMFPresentationClock_Release(session->clock);
1739 if (session->system_time_source)
1740 IMFPresentationTimeSource_Release(session->system_time_source);
1741 if (session->clock_rate_control)
1742 IMFRateControl_Release(session->clock_rate_control);
1743 if (session->topo_loader)
1744 IMFTopoLoader_Release(session->topo_loader);
1745 if (session->quality_manager)
1746 IMFQualityManager_Release(session->quality_manager);
1747 DeleteCriticalSection(&session->cs);
1748 heap_free(session);
1751 return refcount;
1754 static HRESULT WINAPI mfsession_GetEvent(IMFMediaSession *iface, DWORD flags, IMFMediaEvent **event)
1756 struct media_session *session = impl_from_IMFMediaSession(iface);
1758 TRACE("%p, %#x, %p.\n", iface, flags, event);
1760 return IMFMediaEventQueue_GetEvent(session->event_queue, flags, event);
1763 static HRESULT WINAPI mfsession_BeginGetEvent(IMFMediaSession *iface, IMFAsyncCallback *callback, IUnknown *state)
1765 struct media_session *session = impl_from_IMFMediaSession(iface);
1767 TRACE("%p, %p, %p.\n", iface, callback, state);
1769 return IMFMediaEventQueue_BeginGetEvent(session->event_queue, callback, state);
1772 static HRESULT WINAPI mfsession_EndGetEvent(IMFMediaSession *iface, IMFAsyncResult *result, IMFMediaEvent **event)
1774 struct media_session *session = impl_from_IMFMediaSession(iface);
1776 TRACE("%p, %p, %p.\n", iface, result, event);
1778 return IMFMediaEventQueue_EndGetEvent(session->event_queue, result, event);
1781 static HRESULT WINAPI mfsession_QueueEvent(IMFMediaSession *iface, MediaEventType event_type, REFGUID ext_type,
1782 HRESULT hr, const PROPVARIANT *value)
1784 struct media_session *session = impl_from_IMFMediaSession(iface);
1786 TRACE("%p, %d, %s, %#x, %p.\n", iface, event_type, debugstr_guid(ext_type), hr, value);
1788 return IMFMediaEventQueue_QueueEventParamVar(session->event_queue, event_type, ext_type, hr, value);
1791 static HRESULT WINAPI mfsession_SetTopology(IMFMediaSession *iface, DWORD flags, IMFTopology *topology)
1793 struct media_session *session = impl_from_IMFMediaSession(iface);
1794 struct session_op *op;
1795 WORD node_count = 0;
1796 HRESULT hr;
1798 TRACE("%p, %#x, %p.\n", iface, flags, topology);
1800 if (topology)
1802 if (FAILED(IMFTopology_GetNodeCount(topology, &node_count)) || node_count == 0)
1803 return E_INVALIDARG;
1806 if (FAILED(hr = create_session_op(SESSION_CMD_SET_TOPOLOGY, &op)))
1807 return hr;
1809 op->u.set_topology.flags = flags;
1810 op->u.set_topology.topology = topology;
1811 if (op->u.set_topology.topology)
1812 IMFTopology_AddRef(op->u.set_topology.topology);
1814 hr = session_submit_command(session, op);
1815 IUnknown_Release(&op->IUnknown_iface);
1817 return hr;
1820 static HRESULT WINAPI mfsession_ClearTopologies(IMFMediaSession *iface)
1822 struct media_session *session = impl_from_IMFMediaSession(iface);
1824 TRACE("%p.\n", iface);
1826 return session_submit_simple_command(session, SESSION_CMD_CLEAR_TOPOLOGIES);
1829 static HRESULT WINAPI mfsession_Start(IMFMediaSession *iface, const GUID *format, const PROPVARIANT *start_position)
1831 struct media_session *session = impl_from_IMFMediaSession(iface);
1832 struct session_op *op;
1833 HRESULT hr;
1835 TRACE("%p, %s, %p.\n", iface, debugstr_guid(format), start_position);
1837 if (!start_position)
1838 return E_POINTER;
1840 if (FAILED(hr = create_session_op(SESSION_CMD_START, &op)))
1841 return hr;
1843 op->u.start.time_format = format ? *format : GUID_NULL;
1844 hr = PropVariantCopy(&op->u.start.start_position, start_position);
1846 if (SUCCEEDED(hr))
1847 hr = session_submit_command(session, op);
1849 IUnknown_Release(&op->IUnknown_iface);
1851 return hr;
1854 static HRESULT WINAPI mfsession_Pause(IMFMediaSession *iface)
1856 struct media_session *session = impl_from_IMFMediaSession(iface);
1858 TRACE("%p.\n", iface);
1860 return session_submit_simple_command(session, SESSION_CMD_PAUSE);
1863 static HRESULT WINAPI mfsession_Stop(IMFMediaSession *iface)
1865 struct media_session *session = impl_from_IMFMediaSession(iface);
1867 TRACE("%p.\n", iface);
1869 return session_submit_simple_command(session, SESSION_CMD_STOP);
1872 static HRESULT WINAPI mfsession_Close(IMFMediaSession *iface)
1874 struct media_session *session = impl_from_IMFMediaSession(iface);
1876 TRACE("%p.\n", iface);
1878 return session_submit_simple_command(session, SESSION_CMD_CLOSE);
1881 static HRESULT WINAPI mfsession_Shutdown(IMFMediaSession *iface)
1883 struct media_session *session = impl_from_IMFMediaSession(iface);
1884 HRESULT hr = S_OK;
1886 TRACE("%p.\n", iface);
1888 EnterCriticalSection(&session->cs);
1889 if (SUCCEEDED(hr = session_is_shut_down(session)))
1891 session->state = SESSION_STATE_SHUT_DOWN;
1892 IMFMediaEventQueue_Shutdown(session->event_queue);
1893 if (session->quality_manager)
1894 IMFQualityManager_Shutdown(session->quality_manager);
1895 MFShutdownObject((IUnknown *)session->clock);
1896 IMFPresentationClock_Release(session->clock);
1897 session->clock = NULL;
1898 session_clear_presentation(session);
1900 LeaveCriticalSection(&session->cs);
1902 return hr;
1905 static HRESULT WINAPI mfsession_GetClock(IMFMediaSession *iface, IMFClock **clock)
1907 struct media_session *session = impl_from_IMFMediaSession(iface);
1908 HRESULT hr;
1910 TRACE("%p, %p.\n", iface, clock);
1912 EnterCriticalSection(&session->cs);
1913 if (SUCCEEDED(hr = session_is_shut_down(session)))
1915 *clock = (IMFClock *)session->clock;
1916 IMFClock_AddRef(*clock);
1918 LeaveCriticalSection(&session->cs);
1920 return hr;
1923 static HRESULT WINAPI mfsession_GetSessionCapabilities(IMFMediaSession *iface, DWORD *caps)
1925 struct media_session *session = impl_from_IMFMediaSession(iface);
1926 HRESULT hr = S_OK;
1928 TRACE("%p, %p.\n", iface, caps);
1930 if (!caps)
1931 return E_POINTER;
1933 EnterCriticalSection(&session->cs);
1934 if (SUCCEEDED(hr = session_is_shut_down(session)))
1935 *caps = session->caps;
1936 LeaveCriticalSection(&session->cs);
1938 return hr;
1941 static HRESULT WINAPI mfsession_GetFullTopology(IMFMediaSession *iface, DWORD flags, TOPOID id, IMFTopology **topology)
1943 struct media_session *session = impl_from_IMFMediaSession(iface);
1944 struct queued_topology *queued;
1945 TOPOID topo_id;
1946 HRESULT hr;
1948 TRACE("%p, %#x, %s, %p.\n", iface, flags, wine_dbgstr_longlong(id), topology);
1950 *topology = NULL;
1952 EnterCriticalSection(&session->cs);
1954 if (SUCCEEDED(hr = session_is_shut_down(session)))
1956 if (flags & MFSESSION_GETFULLTOPOLOGY_CURRENT)
1958 if (session->presentation.topo_status != MF_TOPOSTATUS_INVALID)
1959 *topology = session->presentation.current_topology;
1960 else
1961 hr = MF_E_INVALIDREQUEST;
1963 else
1965 LIST_FOR_EACH_ENTRY(queued, &session->topologies, struct queued_topology, entry)
1967 if (SUCCEEDED(IMFTopology_GetTopologyID(queued->topology, &topo_id)) && topo_id == id)
1969 *topology = queued->topology;
1970 break;
1975 if (*topology)
1976 IMFTopology_AddRef(*topology);
1979 LeaveCriticalSection(&session->cs);
1981 return hr;
1984 static const IMFMediaSessionVtbl mfmediasessionvtbl =
1986 mfsession_QueryInterface,
1987 mfsession_AddRef,
1988 mfsession_Release,
1989 mfsession_GetEvent,
1990 mfsession_BeginGetEvent,
1991 mfsession_EndGetEvent,
1992 mfsession_QueueEvent,
1993 mfsession_SetTopology,
1994 mfsession_ClearTopologies,
1995 mfsession_Start,
1996 mfsession_Pause,
1997 mfsession_Stop,
1998 mfsession_Close,
1999 mfsession_Shutdown,
2000 mfsession_GetClock,
2001 mfsession_GetSessionCapabilities,
2002 mfsession_GetFullTopology,
2005 static HRESULT WINAPI session_get_service_QueryInterface(IMFGetService *iface, REFIID riid, void **obj)
2007 struct media_session *session = impl_from_IMFGetService(iface);
2008 return IMFMediaSession_QueryInterface(&session->IMFMediaSession_iface, riid, obj);
2011 static ULONG WINAPI session_get_service_AddRef(IMFGetService *iface)
2013 struct media_session *session = impl_from_IMFGetService(iface);
2014 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
2017 static ULONG WINAPI session_get_service_Release(IMFGetService *iface)
2019 struct media_session *session = impl_from_IMFGetService(iface);
2020 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
2023 static HRESULT session_get_video_render_service(struct media_session *session, REFGUID service,
2024 REFIID riid, void **obj)
2026 IMFStreamSink *stream_sink;
2027 IMFTopologyNode *node;
2028 IMFCollection *nodes;
2029 IMFMediaSink *sink;
2030 unsigned int i = 0;
2031 IUnknown *vr;
2032 HRESULT hr = E_FAIL;
2034 /* Use first sink to support IMFVideoRenderer. */
2035 if (session->presentation.current_topology)
2037 if (SUCCEEDED(IMFTopology_GetOutputNodeCollection(session->presentation.current_topology,
2038 &nodes)))
2040 while (IMFCollection_GetElement(nodes, i++, (IUnknown **)&node) == S_OK)
2042 if (SUCCEEDED(topology_node_get_object(node, &IID_IMFStreamSink, (void **)&stream_sink)))
2044 if (SUCCEEDED(IMFStreamSink_GetMediaSink(stream_sink, &sink)))
2046 if (SUCCEEDED(IMFMediaSink_QueryInterface(sink, &IID_IMFVideoRenderer, (void **)&vr)))
2048 if (FAILED(hr = MFGetService(vr, service, riid, obj)))
2049 WARN("Failed to get service from video renderer %#x.\n", hr);
2050 IUnknown_Release(vr);
2053 IMFStreamSink_Release(stream_sink);
2056 IMFTopologyNode_Release(node);
2058 if (*obj)
2059 break;
2062 IMFCollection_Release(nodes);
2066 return hr;
2069 static HRESULT WINAPI session_get_service_GetService(IMFGetService *iface, REFGUID service, REFIID riid, void **obj)
2071 struct media_session *session = impl_from_IMFGetService(iface);
2072 HRESULT hr = S_OK;
2074 TRACE("%p, %s, %s, %p.\n", iface, debugstr_guid(service), debugstr_guid(riid), obj);
2076 *obj = NULL;
2078 EnterCriticalSection(&session->cs);
2079 if (FAILED(hr = session_is_shut_down(session)))
2082 else if (IsEqualGUID(service, &MF_RATE_CONTROL_SERVICE))
2084 if (IsEqualIID(riid, &IID_IMFRateSupport))
2086 *obj = &session->IMFRateSupport_iface;
2088 else if (IsEqualIID(riid, &IID_IMFRateControl))
2090 *obj = &session->IMFRateControl_iface;
2092 else
2093 hr = E_NOINTERFACE;
2095 if (*obj)
2096 IUnknown_AddRef((IUnknown *)*obj);
2098 else if (IsEqualGUID(service, &MF_LOCAL_MFT_REGISTRATION_SERVICE))
2100 hr = IMFLocalMFTRegistration_QueryInterface(&local_mft_registration, riid, obj);
2102 else if (IsEqualGUID(service, &MF_TOPONODE_ATTRIBUTE_EDITOR_SERVICE))
2104 *obj = &session->IMFTopologyNodeAttributeEditor_iface;
2105 IUnknown_AddRef((IUnknown *)*obj);
2107 else if (IsEqualGUID(service, &MR_VIDEO_RENDER_SERVICE))
2109 hr = session_get_video_render_service(session, service, riid, obj);
2111 else
2112 FIXME("Unsupported service %s.\n", debugstr_guid(service));
2114 LeaveCriticalSection(&session->cs);
2116 return hr;
2119 static const IMFGetServiceVtbl session_get_service_vtbl =
2121 session_get_service_QueryInterface,
2122 session_get_service_AddRef,
2123 session_get_service_Release,
2124 session_get_service_GetService,
2127 static HRESULT WINAPI session_commands_callback_QueryInterface(IMFAsyncCallback *iface, REFIID riid, void **obj)
2129 if (IsEqualIID(riid, &IID_IMFAsyncCallback) ||
2130 IsEqualIID(riid, &IID_IUnknown))
2132 *obj = iface;
2133 IMFAsyncCallback_AddRef(iface);
2134 return S_OK;
2137 WARN("Unsupported %s.\n", debugstr_guid(riid));
2138 *obj = NULL;
2139 return E_NOINTERFACE;
2142 static ULONG WINAPI session_commands_callback_AddRef(IMFAsyncCallback *iface)
2144 struct media_session *session = impl_from_commands_callback_IMFAsyncCallback(iface);
2145 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
2148 static ULONG WINAPI session_commands_callback_Release(IMFAsyncCallback *iface)
2150 struct media_session *session = impl_from_commands_callback_IMFAsyncCallback(iface);
2151 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
2154 static HRESULT WINAPI session_commands_callback_GetParameters(IMFAsyncCallback *iface, DWORD *flags, DWORD *queue)
2156 return E_NOTIMPL;
2159 static HRESULT WINAPI session_commands_callback_Invoke(IMFAsyncCallback *iface, IMFAsyncResult *result)
2161 struct session_op *op = impl_op_from_IUnknown(IMFAsyncResult_GetStateNoAddRef(result));
2162 struct media_session *session = impl_from_commands_callback_IMFAsyncCallback(iface);
2163 struct topo_node *topo_node;
2164 IMFTopologyNode *upstream_node;
2165 unsigned int upstream_output;
2167 EnterCriticalSection(&session->cs);
2169 switch (op->command)
2171 case SESSION_CMD_CLEAR_TOPOLOGIES:
2172 session_clear_topologies(session);
2173 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MESessionTopologiesCleared, &GUID_NULL,
2174 S_OK, NULL);
2175 session_command_complete(session);
2176 break;
2177 case SESSION_CMD_SET_TOPOLOGY:
2178 session_set_topology(session, op->u.set_topology.flags, op->u.set_topology.topology);
2179 session_command_complete(session);
2180 break;
2181 case SESSION_CMD_START:
2182 session_start(session, &op->u.start.time_format, &op->u.start.start_position);
2183 break;
2184 case SESSION_CMD_PAUSE:
2185 session_pause(session);
2186 break;
2187 case SESSION_CMD_STOP:
2188 session_stop(session);
2189 break;
2190 case SESSION_CMD_CLOSE:
2191 session_close(session);
2192 break;
2193 case SESSION_CMD_QM_NOTIFY_TOPOLOGY:
2194 IMFQualityManager_NotifyTopology(session->quality_manager, op->u.notify_topology.topology);
2195 session_command_complete(session);
2196 break;
2197 case SESSION_CMD_SA_READY:
2198 topo_node = session_get_node_by_id(session, op->u.sa_ready.node_id);
2200 if (topo_node->u.sink.requests)
2202 if (SUCCEEDED(IMFTopologyNode_GetInput(topo_node->node, 0, &upstream_node, &upstream_output)))
2204 session_request_sample_from_node(session, upstream_node, upstream_output);
2205 IMFTopologyNode_Release(upstream_node);
2208 break;
2209 default:
2213 LeaveCriticalSection(&session->cs);
2215 return S_OK;
2218 static const IMFAsyncCallbackVtbl session_commands_callback_vtbl =
2220 session_commands_callback_QueryInterface,
2221 session_commands_callback_AddRef,
2222 session_commands_callback_Release,
2223 session_commands_callback_GetParameters,
2224 session_commands_callback_Invoke,
2227 static HRESULT WINAPI session_events_callback_QueryInterface(IMFAsyncCallback *iface, REFIID riid, void **obj)
2229 if (IsEqualIID(riid, &IID_IMFAsyncCallback) ||
2230 IsEqualIID(riid, &IID_IUnknown))
2232 *obj = iface;
2233 IMFAsyncCallback_AddRef(iface);
2234 return S_OK;
2237 WARN("Unsupported %s.\n", debugstr_guid(riid));
2238 *obj = NULL;
2239 return E_NOINTERFACE;
2242 static ULONG WINAPI session_events_callback_AddRef(IMFAsyncCallback *iface)
2244 struct media_session *session = impl_from_events_callback_IMFAsyncCallback(iface);
2245 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
2248 static ULONG WINAPI session_events_callback_Release(IMFAsyncCallback *iface)
2250 struct media_session *session = impl_from_events_callback_IMFAsyncCallback(iface);
2251 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
2254 static HRESULT WINAPI session_events_callback_GetParameters(IMFAsyncCallback *iface, DWORD *flags, DWORD *queue)
2256 return E_NOTIMPL;
2259 static HRESULT session_add_media_stream(struct media_session *session, IMFMediaSource *source, IMFMediaStream *stream)
2261 struct topo_node *node;
2262 IMFStreamDescriptor *sd;
2263 DWORD stream_id = 0;
2264 HRESULT hr;
2266 if (FAILED(hr = IMFMediaStream_GetStreamDescriptor(stream, &sd)))
2267 return hr;
2269 hr = IMFStreamDescriptor_GetStreamIdentifier(sd, &stream_id);
2270 IMFStreamDescriptor_Release(sd);
2271 if (FAILED(hr))
2272 return hr;
2274 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2276 if (node->type == MF_TOPOLOGY_SOURCESTREAM_NODE && node->u.source.source == source
2277 && node->u.source.stream_id == stream_id)
2279 if (node->object.source_stream)
2281 WARN("Node already has stream set.\n");
2282 return S_FALSE;
2285 node->object.source_stream = stream;
2286 IMFMediaStream_AddRef(node->object.source_stream);
2287 break;
2291 return S_OK;
2294 static BOOL session_is_source_nodes_state(struct media_session *session, enum object_state state)
2296 struct media_source *source;
2297 struct topo_node *node;
2299 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
2301 if (source->state != state)
2302 return FALSE;
2305 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2307 if (node->type == MF_TOPOLOGY_SOURCESTREAM_NODE && node->state != state)
2308 return FALSE;
2311 return TRUE;
2314 static BOOL session_is_output_nodes_state(struct media_session *session, enum object_state state)
2316 struct topo_node *node;
2318 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2320 if (node->type == MF_TOPOLOGY_OUTPUT_NODE && node->state != state)
2321 return FALSE;
2324 return TRUE;
2327 static enum object_state session_get_object_state_for_event(MediaEventType event)
2329 switch (event)
2331 case MESourceStarted:
2332 case MEStreamStarted:
2333 case MEStreamSinkStarted:
2334 return OBJ_STATE_STARTED;
2335 case MESourcePaused:
2336 case MEStreamPaused:
2337 case MEStreamSinkPaused:
2338 return OBJ_STATE_PAUSED;
2339 case MESourceStopped:
2340 case MEStreamStopped:
2341 case MEStreamSinkStopped:
2342 return OBJ_STATE_STOPPED;
2343 case MEStreamSinkPrerolled:
2344 return OBJ_STATE_PREROLLED;
2345 default:
2346 return OBJ_STATE_INVALID;
2350 static void session_set_consumed_clock(IUnknown *object, IMFPresentationClock *clock)
2352 IMFClockConsumer *consumer;
2354 if (SUCCEEDED(IUnknown_QueryInterface(object, &IID_IMFClockConsumer, (void **)&consumer)))
2356 IMFClockConsumer_SetPresentationClock(consumer, clock);
2357 IMFClockConsumer_Release(consumer);
2361 static void session_set_presentation_clock(struct media_session *session)
2363 IMFPresentationTimeSource *time_source = NULL;
2364 struct media_source *source;
2365 struct media_sink *sink;
2366 struct topo_node *node;
2367 HRESULT hr;
2369 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2371 if (node->type == MF_TOPOLOGY_TRANSFORM_NODE)
2372 IMFTransform_ProcessMessage(node->object.transform, MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0);
2375 if (!(session->presentation.flags & SESSION_FLAG_PRESENTATION_CLOCK_SET))
2377 /* Attempt to get time source from the sinks. */
2378 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
2380 if (SUCCEEDED(IMFMediaSink_QueryInterface(sink->sink, &IID_IMFPresentationTimeSource,
2381 (void **)&time_source)))
2382 break;
2385 if (time_source)
2387 hr = IMFPresentationClock_SetTimeSource(session->clock, time_source);
2388 IMFPresentationTimeSource_Release(time_source);
2390 else
2391 hr = IMFPresentationClock_SetTimeSource(session->clock, session->system_time_source);
2393 if (FAILED(hr))
2394 WARN("Failed to set time source, hr %#x.\n", hr);
2396 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2398 if (node->type != MF_TOPOLOGY_OUTPUT_NODE)
2399 continue;
2401 if (FAILED(hr = IMFStreamSink_BeginGetEvent(node->object.sink_stream, &session->events_callback,
2402 node->object.object)))
2404 WARN("Failed to subscribe to stream sink events, hr %#x.\n", hr);
2408 /* Set clock for all topology nodes. */
2409 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
2411 session_set_consumed_clock((IUnknown *)source->source, session->clock);
2414 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
2416 if (sink->event_generator && FAILED(hr = IMFMediaEventGenerator_BeginGetEvent(sink->event_generator,
2417 &session->events_callback, (IUnknown *)sink->event_generator)))
2419 WARN("Failed to subscribe to sink events, hr %#x.\n", hr);
2422 if (FAILED(hr = IMFMediaSink_SetPresentationClock(sink->sink, session->clock)))
2423 WARN("Failed to set presentation clock for the sink, hr %#x.\n", hr);
2426 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2428 if (node->type != MF_TOPOLOGY_TRANSFORM_NODE)
2429 continue;
2431 session_set_consumed_clock(node->object.object, session->clock);
2434 session->presentation.flags |= SESSION_FLAG_PRESENTATION_CLOCK_SET;
2438 static HRESULT session_start_clock(struct media_session *session)
2440 LONGLONG start_offset = 0;
2441 HRESULT hr;
2443 if (IsEqualGUID(&session->presentation.time_format, &GUID_NULL))
2445 if (session->presentation.start_position.vt == VT_EMPTY)
2446 start_offset = PRESENTATION_CURRENT_POSITION;
2447 else if (session->presentation.start_position.vt == VT_I8)
2448 start_offset = session->presentation.start_position.hVal.QuadPart;
2449 else
2450 FIXME("Unhandled position type %d.\n", session->presentation.start_position.vt);
2452 else
2453 FIXME("Unhandled time format %s.\n", debugstr_guid(&session->presentation.time_format));
2455 if (FAILED(hr = IMFPresentationClock_Start(session->clock, start_offset)))
2456 WARN("Failed to start session clock, hr %#x.\n", hr);
2458 return hr;
2461 static struct topo_node *session_get_node_object(struct media_session *session, IUnknown *object,
2462 MF_TOPOLOGY_TYPE node_type)
2464 struct topo_node *node = NULL;
2466 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2468 if (node->type == node_type && object == node->object.object)
2469 break;
2472 return node;
2475 static BOOL session_set_node_object_state(struct media_session *session, IUnknown *object,
2476 MF_TOPOLOGY_TYPE node_type, enum object_state state)
2478 struct topo_node *node;
2479 BOOL changed = FALSE;
2481 if ((node = session_get_node_object(session, object, node_type)))
2483 changed = node->state != state;
2484 node->state = state;
2487 return changed;
2490 static void session_set_source_object_state(struct media_session *session, IUnknown *object,
2491 MediaEventType event_type)
2493 IMFStreamSink *stream_sink;
2494 struct media_source *src;
2495 struct media_sink *sink;
2496 enum object_state state;
2497 struct topo_node *node;
2498 unsigned int i, count;
2499 BOOL changed = FALSE;
2500 HRESULT hr;
2502 if ((state = session_get_object_state_for_event(event_type)) == OBJ_STATE_INVALID)
2503 return;
2505 switch (event_type)
2507 case MESourceStarted:
2508 case MESourcePaused:
2509 case MESourceStopped:
2511 LIST_FOR_EACH_ENTRY(src, &session->presentation.sources, struct media_source, entry)
2513 if (object == (IUnknown *)src->source)
2515 changed = src->state != state;
2516 src->state = state;
2517 break;
2520 break;
2521 case MEStreamStarted:
2522 case MEStreamPaused:
2523 case MEStreamStopped:
2525 changed = session_set_node_object_state(session, object, MF_TOPOLOGY_SOURCESTREAM_NODE, state);
2526 default:
2530 if (!changed)
2531 return;
2533 switch (session->state)
2535 case SESSION_STATE_STARTING_SOURCES:
2536 if (!session_is_source_nodes_state(session, OBJ_STATE_STARTED))
2537 break;
2539 session_set_topo_status(session, S_OK, MF_TOPOSTATUS_STARTED_SOURCE);
2541 session_set_presentation_clock(session);
2543 if (session->presentation.flags & SESSION_FLAG_NEEDS_PREROLL)
2545 MFTIME preroll_time = 0;
2547 if (session->presentation.start_position.vt == VT_I8)
2548 preroll_time = session->presentation.start_position.hVal.QuadPart;
2550 /* Mark stream sinks without prerolling support as PREROLLED to keep state test logic generic. */
2551 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
2553 if (sink->preroll)
2555 /* FIXME: abort and enter error state on failure. */
2556 if (FAILED(hr = IMFMediaSinkPreroll_NotifyPreroll(sink->preroll, preroll_time)))
2557 WARN("Preroll notification failed, hr %#x.\n", hr);
2559 else
2561 if (SUCCEEDED(IMFMediaSink_GetStreamSinkCount(sink->sink, &count)))
2563 for (i = 0; i < count; ++i)
2565 if (SUCCEEDED(IMFMediaSink_GetStreamSinkByIndex(sink->sink, i, &stream_sink)))
2567 session_set_node_object_state(session, (IUnknown *)stream_sink, MF_TOPOLOGY_OUTPUT_NODE,
2568 OBJ_STATE_PREROLLED);
2569 IMFStreamSink_Release(stream_sink);
2575 session->state = SESSION_STATE_PREROLLING_SINKS;
2577 else if (SUCCEEDED(session_start_clock(session)))
2578 session->state = SESSION_STATE_STARTING_SINKS;
2580 break;
2581 case SESSION_STATE_PAUSING_SOURCES:
2582 if (!session_is_source_nodes_state(session, OBJ_STATE_PAUSED))
2583 break;
2585 session_set_paused(session, S_OK);
2586 break;
2587 case SESSION_STATE_STOPPING_SOURCES:
2588 if (!session_is_source_nodes_state(session, OBJ_STATE_STOPPED))
2589 break;
2591 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2593 switch (node->type)
2595 case MF_TOPOLOGY_OUTPUT_NODE:
2596 IMFStreamSink_Flush(node->object.sink_stream);
2597 break;
2598 case MF_TOPOLOGY_TRANSFORM_NODE:
2599 IMFTransform_ProcessMessage(node->object.transform, MFT_MESSAGE_COMMAND_FLUSH, 0);
2600 break;
2601 default:
2606 session_set_caps(session, session->caps & ~MFSESSIONCAP_PAUSE);
2608 if (session->presentation.flags & SESSION_FLAG_FINALIZE_SINKS)
2609 session_finalize_sinks(session);
2610 else
2611 session_set_stopped(session, S_OK);
2613 break;
2614 default:
2619 static void session_set_sink_stream_state(struct media_session *session, IMFStreamSink *stream,
2620 MediaEventType event_type)
2622 struct media_source *source;
2623 enum object_state state;
2624 HRESULT hr = S_OK;
2625 BOOL changed;
2627 if ((state = session_get_object_state_for_event(event_type)) == OBJ_STATE_INVALID)
2628 return;
2630 if (!(changed = session_set_node_object_state(session, (IUnknown *)stream, MF_TOPOLOGY_OUTPUT_NODE, state)))
2631 return;
2633 switch (session->state)
2635 case SESSION_STATE_PREROLLING_SINKS:
2636 if (!session_is_output_nodes_state(session, OBJ_STATE_PREROLLED))
2637 break;
2639 if (SUCCEEDED(session_start_clock(session)))
2640 session->state = SESSION_STATE_STARTING_SINKS;
2641 break;
2642 case SESSION_STATE_STARTING_SINKS:
2643 if (!session_is_output_nodes_state(session, OBJ_STATE_STARTED))
2644 break;
2646 session_set_started(session);
2647 break;
2648 case SESSION_STATE_PAUSING_SINKS:
2649 if (!session_is_output_nodes_state(session, OBJ_STATE_PAUSED))
2650 break;
2652 session->state = SESSION_STATE_PAUSING_SOURCES;
2654 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
2656 if (FAILED(hr = IMFMediaSource_Pause(source->source)))
2657 break;
2660 if (FAILED(hr))
2661 session_set_paused(session, hr);
2663 break;
2664 case SESSION_STATE_STOPPING_SINKS:
2665 if (!session_is_output_nodes_state(session, OBJ_STATE_STOPPED))
2666 break;
2668 session->state = SESSION_STATE_STOPPING_SOURCES;
2670 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
2672 if (session->presentation.flags & SESSION_FLAG_END_OF_PRESENTATION)
2673 IMFMediaSource_Stop(source->source);
2674 else if (FAILED(hr = IMFMediaSource_Stop(source->source)))
2675 break;
2678 if (session->presentation.flags & SESSION_FLAG_END_OF_PRESENTATION || FAILED(hr))
2679 session_set_stopped(session, hr);
2681 break;
2682 default:
2687 static struct sample *transform_create_sample(IMFSample *sample)
2689 struct sample *sample_entry = heap_alloc_zero(sizeof(*sample_entry));
2691 if (sample_entry)
2693 sample_entry->sample = sample;
2694 if (sample_entry->sample)
2695 IMFSample_AddRef(sample_entry->sample);
2698 return sample_entry;
2701 static HRESULT transform_get_external_output_sample(const struct media_session *session, struct topo_node *transform,
2702 unsigned int output_index, const MFT_OUTPUT_STREAM_INFO *stream_info, IMFSample **sample)
2704 unsigned int buffer_size, downstream_input;
2705 IMFTopologyNode *downstream_node;
2706 IMFMediaBuffer *buffer = NULL;
2707 struct topo_node *topo_node;
2708 TOPOID node_id;
2709 HRESULT hr;
2711 if (FAILED(IMFTopologyNode_GetOutput(transform->node, output_index, &downstream_node, &downstream_input)))
2713 WARN("Failed to get connected node for output %u.\n", output_index);
2714 return MF_E_UNEXPECTED;
2717 IMFTopologyNode_GetTopoNodeID(downstream_node, &node_id);
2718 IMFTopologyNode_Release(downstream_node);
2720 topo_node = session_get_node_by_id(session, node_id);
2722 if (topo_node->type == MF_TOPOLOGY_OUTPUT_NODE && topo_node->u.sink.allocator)
2724 hr = IMFVideoSampleAllocator_AllocateSample(topo_node->u.sink.allocator, sample);
2726 else
2728 buffer_size = max(stream_info->cbSize, transform->u.transform.outputs[output_index].min_buffer_size);
2730 hr = MFCreateAlignedMemoryBuffer(buffer_size, stream_info->cbAlignment, &buffer);
2731 if (SUCCEEDED(hr))
2732 hr = MFCreateSample(sample);
2734 if (SUCCEEDED(hr))
2735 hr = IMFSample_AddBuffer(*sample, buffer);
2737 if (buffer)
2738 IMFMediaBuffer_Release(buffer);
2741 return hr;
2744 static HRESULT transform_node_pull_samples(const struct media_session *session, struct topo_node *node)
2746 MFT_OUTPUT_STREAM_INFO stream_info;
2747 MFT_OUTPUT_DATA_BUFFER *buffers;
2748 struct sample *queued_sample;
2749 DWORD status = 0;
2750 unsigned int i;
2751 HRESULT hr = E_UNEXPECTED;
2753 if (!(buffers = heap_calloc(node->u.transform.output_count, sizeof(*buffers))))
2754 return E_OUTOFMEMORY;
2756 for (i = 0; i < node->u.transform.output_count; ++i)
2758 buffers[i].dwStreamID = transform_node_get_stream_id(node, TRUE, i);
2759 buffers[i].pSample = NULL;
2760 buffers[i].dwStatus = 0;
2761 buffers[i].pEvents = NULL;
2763 memset(&stream_info, 0, sizeof(stream_info));
2764 if (FAILED(hr = IMFTransform_GetOutputStreamInfo(node->object.transform, buffers[i].dwStreamID, &stream_info)))
2765 break;
2767 if (!(stream_info.dwFlags & (MFT_OUTPUT_STREAM_PROVIDES_SAMPLES | MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES)))
2769 if (FAILED(hr = transform_get_external_output_sample(session, node, i, &stream_info, &buffers[i].pSample)))
2770 break;
2774 if (SUCCEEDED(hr))
2775 hr = IMFTransform_ProcessOutput(node->object.transform, 0, node->u.transform.output_count, buffers, &status);
2777 /* Collect returned samples for all streams. */
2778 for (i = 0; i < node->u.transform.output_count; ++i)
2780 if (buffers[i].pEvents)
2781 IMFCollection_Release(buffers[i].pEvents);
2783 if (SUCCEEDED(hr) && !(buffers[i].dwStatus & MFT_OUTPUT_DATA_BUFFER_NO_SAMPLE))
2785 if (session->quality_manager)
2786 IMFQualityManager_NotifyProcessOutput(session->quality_manager, node->node, i, buffers[i].pSample);
2788 queued_sample = transform_create_sample(buffers[i].pSample);
2789 list_add_tail(&node->u.transform.outputs[i].samples, &queued_sample->entry);
2792 if (buffers[i].pSample)
2793 IMFSample_Release(buffers[i].pSample);
2796 heap_free(buffers);
2798 return hr;
2801 static void session_deliver_sample_to_node(struct media_session *session, IMFTopologyNode *node, unsigned int input,
2802 IMFSample *sample)
2804 struct sample *sample_entry, *sample_entry2;
2805 DWORD stream_id, downstream_input;
2806 IMFTopologyNode *downstream_node;
2807 struct topo_node *topo_node;
2808 MF_TOPOLOGY_TYPE node_type;
2809 BOOL drain = FALSE;
2810 TOPOID node_id;
2811 unsigned int i;
2812 HRESULT hr;
2814 if (session->quality_manager)
2815 IMFQualityManager_NotifyProcessInput(session->quality_manager, node, input, sample);
2817 IMFTopologyNode_GetNodeType(node, &node_type);
2818 IMFTopologyNode_GetTopoNodeID(node, &node_id);
2820 topo_node = session_get_node_by_id(session, node_id);
2822 switch (node_type)
2824 case MF_TOPOLOGY_OUTPUT_NODE:
2825 if (sample)
2827 if (topo_node->u.sink.requests)
2829 if (FAILED(hr = IMFStreamSink_ProcessSample(topo_node->object.sink_stream, sample)))
2830 WARN("Stream sink failed to process sample, hr %#x.\n", hr);
2831 topo_node->u.sink.requests--;
2834 else if (FAILED(hr = IMFStreamSink_PlaceMarker(topo_node->object.sink_stream, MFSTREAMSINK_MARKER_ENDOFSEGMENT,
2835 NULL, NULL)))
2837 WARN("Failed to place sink marker, hr %#x.\n", hr);
2839 break;
2840 case MF_TOPOLOGY_TRANSFORM_NODE:
2842 transform_node_pull_samples(session, topo_node);
2844 sample_entry = transform_create_sample(sample);
2845 list_add_tail(&topo_node->u.transform.inputs[input].samples, &sample_entry->entry);
2847 for (i = 0; i < topo_node->u.transform.input_count; ++i)
2849 stream_id = transform_node_get_stream_id(topo_node, FALSE, i);
2850 LIST_FOR_EACH_ENTRY_SAFE(sample_entry, sample_entry2, &topo_node->u.transform.inputs[i].samples,
2851 struct sample, entry)
2853 if (sample_entry->sample)
2855 if ((hr = IMFTransform_ProcessInput(topo_node->object.transform, stream_id,
2856 sample_entry->sample, 0)) == MF_E_NOTACCEPTING)
2857 break;
2858 if (FAILED(hr))
2859 WARN("Failed to process input for stream %u/%u, hr %#x.\n", i, stream_id, hr);
2860 transform_release_sample(sample_entry);
2862 else
2864 transform_stream_drop_samples(&topo_node->u.transform.inputs[i]);
2865 drain = TRUE;
2870 if (drain)
2872 if (FAILED(hr = IMFTransform_ProcessMessage(topo_node->object.transform, MFT_MESSAGE_COMMAND_DRAIN, 0)))
2873 WARN("Drain command failed for transform, hr %#x.\n", hr);
2876 transform_node_pull_samples(session, topo_node);
2878 /* Remaining unprocessed input has been discarded, now queue markers for every output. */
2879 if (drain)
2881 for (i = 0; i < topo_node->u.transform.output_count; ++i)
2883 if ((sample_entry = transform_create_sample(NULL)))
2884 list_add_tail(&topo_node->u.transform.outputs[i].samples, &sample_entry->entry);
2888 /* Push down all available output. */
2889 for (i = 0; i < topo_node->u.transform.output_count; ++i)
2891 if (FAILED(IMFTopologyNode_GetOutput(node, i, &downstream_node, &downstream_input)))
2893 WARN("Failed to get connected node for output %u.\n", i);
2894 continue;
2897 LIST_FOR_EACH_ENTRY_SAFE(sample_entry, sample_entry2, &topo_node->u.transform.outputs[i].samples,
2898 struct sample, entry)
2900 if (!topo_node->u.transform.outputs[i].requests)
2901 break;
2903 session_deliver_sample_to_node(session, downstream_node, downstream_input, sample_entry->sample);
2904 topo_node->u.transform.outputs[i].requests--;
2906 transform_release_sample(sample_entry);
2909 IMFTopologyNode_Release(downstream_node);
2912 break;
2913 case MF_TOPOLOGY_TEE_NODE:
2914 FIXME("Unhandled downstream node type %d.\n", node_type);
2915 break;
2916 default:
2921 static HRESULT session_request_sample_from_node(struct media_session *session, IMFTopologyNode *node, DWORD output)
2923 IMFTopologyNode *downstream_node, *upstream_node;
2924 unsigned int downstream_input, upstream_output;
2925 struct topo_node *topo_node;
2926 MF_TOPOLOGY_TYPE node_type;
2927 struct sample *sample;
2928 TOPOID node_id;
2929 HRESULT hr;
2931 IMFTopologyNode_GetNodeType(node, &node_type);
2932 IMFTopologyNode_GetTopoNodeID(node, &node_id);
2934 topo_node = session_get_node_by_id(session, node_id);
2936 switch (node_type)
2938 case MF_TOPOLOGY_SOURCESTREAM_NODE:
2939 if (FAILED(hr = IMFMediaStream_RequestSample(topo_node->object.source_stream, NULL)))
2940 WARN("Sample request failed, hr %#x.\n", hr);
2941 break;
2942 case MF_TOPOLOGY_TRANSFORM_NODE:
2944 if (list_empty(&topo_node->u.transform.outputs[output].samples))
2946 /* Forward request to upstream node. */
2947 if (SUCCEEDED(hr = IMFTopologyNode_GetInput(node, 0 /* FIXME */, &upstream_node, &upstream_output)))
2949 if (SUCCEEDED(hr = session_request_sample_from_node(session, upstream_node, upstream_output)))
2950 topo_node->u.transform.outputs[output].requests++;
2951 IMFTopologyNode_Release(upstream_node);
2954 else
2956 if (SUCCEEDED(hr = IMFTopologyNode_GetOutput(node, output, &downstream_node, &downstream_input)))
2958 sample = LIST_ENTRY(list_head(&topo_node->u.transform.outputs[output].samples), struct sample, entry);
2959 session_deliver_sample_to_node(session, downstream_node, downstream_input, sample->sample);
2960 transform_release_sample(sample);
2961 IMFTopologyNode_Release(downstream_node);
2965 break;
2966 case MF_TOPOLOGY_TEE_NODE:
2967 FIXME("Unhandled upstream node type %d.\n", node_type);
2968 default:
2969 hr = E_UNEXPECTED;
2972 return hr;
2975 static void session_request_sample(struct media_session *session, IMFStreamSink *sink_stream)
2977 struct topo_node *sink_node = NULL, *node;
2978 IMFTopologyNode *upstream_node;
2979 DWORD upstream_output;
2980 HRESULT hr;
2982 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2984 if (node->type == MF_TOPOLOGY_OUTPUT_NODE && node->object.sink_stream == sink_stream)
2986 sink_node = node;
2987 break;
2991 if (!sink_node)
2992 return;
2994 if (FAILED(hr = IMFTopologyNode_GetInput(sink_node->node, 0, &upstream_node, &upstream_output)))
2996 WARN("Failed to get upstream node connection, hr %#x.\n", hr);
2997 return;
3000 if (SUCCEEDED(session_request_sample_from_node(session, upstream_node, upstream_output)))
3001 sink_node->u.sink.requests++;
3002 IMFTopologyNode_Release(upstream_node);
3005 static void session_deliver_sample(struct media_session *session, IMFMediaStream *stream, const PROPVARIANT *value)
3007 struct topo_node *source_node = NULL, *node;
3008 IMFTopologyNode *downstream_node;
3009 DWORD downstream_input;
3010 HRESULT hr;
3012 if (value && (value->vt != VT_UNKNOWN || !value->punkVal))
3014 WARN("Unexpected value type %d.\n", value->vt);
3015 return;
3018 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
3020 if (node->type == MF_TOPOLOGY_SOURCESTREAM_NODE && node->object.source_stream == stream)
3022 source_node = node;
3023 break;
3027 if (!source_node)
3028 return;
3030 if (!value)
3031 source_node->flags |= TOPO_NODE_END_OF_STREAM;
3033 if (FAILED(hr = IMFTopologyNode_GetOutput(source_node->node, 0, &downstream_node, &downstream_input)))
3035 WARN("Failed to get downstream node connection, hr %#x.\n", hr);
3036 return;
3039 session_deliver_sample_to_node(session, downstream_node, downstream_input, value ? (IMFSample *)value->punkVal : NULL);
3040 IMFTopologyNode_Release(downstream_node);
3043 static void session_sink_invalidated(struct media_session *session, IMFMediaEvent *event, IMFStreamSink *sink)
3045 struct topo_node *node, *sink_node = NULL;
3046 HRESULT hr;
3048 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
3050 if (node->type == MF_TOPOLOGY_OUTPUT_NODE && node->object.sink_stream == sink)
3052 sink_node = node;
3053 break;
3057 if (!sink_node)
3058 return;
3060 if (!event)
3062 if (FAILED(hr = MFCreateMediaEvent(MESinkInvalidated, &GUID_NULL, S_OK, NULL, &event)))
3063 WARN("Failed to create event, hr %#x.\n", hr);
3066 if (!event)
3067 return;
3069 IMFMediaEvent_SetUINT64(event, &MF_EVENT_OUTPUT_NODE, sink_node->node_id);
3070 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
3072 session_set_topo_status(session, S_OK, MF_TOPOSTATUS_ENDED);
3075 static BOOL session_nodes_is_mask_set(struct media_session *session, MF_TOPOLOGY_TYPE node_type, unsigned int flags)
3077 struct media_source *source;
3078 struct topo_node *node;
3080 if (node_type == MF_TOPOLOGY_MAX)
3082 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
3084 if ((source->flags & flags) != flags)
3085 return FALSE;
3088 else
3090 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
3092 if (node->type == node_type && (node->flags & flags) != flags)
3093 return FALSE;
3097 return TRUE;
3100 static void session_raise_end_of_presentation(struct media_session *session)
3102 if (!(session_nodes_is_mask_set(session, MF_TOPOLOGY_SOURCESTREAM_NODE, TOPO_NODE_END_OF_STREAM)))
3103 return;
3105 if (!(session->presentation.flags & SESSION_FLAG_END_OF_PRESENTATION))
3107 if (session_nodes_is_mask_set(session, MF_TOPOLOGY_MAX, SOURCE_FLAG_END_OF_PRESENTATION))
3109 session->presentation.flags |= SESSION_FLAG_END_OF_PRESENTATION;
3110 session_push_back_command(session, SESSION_CMD_END);
3111 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MEEndOfPresentation, &GUID_NULL, S_OK, NULL);
3116 static void session_handle_end_of_stream(struct media_session *session, IMFMediaStream *stream)
3118 struct topo_node *node;
3120 if (!(node = session_get_node_object(session, (IUnknown *)stream, MF_TOPOLOGY_SOURCESTREAM_NODE))
3121 || node->flags & TOPO_NODE_END_OF_STREAM)
3123 return;
3126 session_deliver_sample(session, stream, NULL);
3128 session_raise_end_of_presentation(session);
3131 static void session_handle_end_of_presentation(struct media_session *session, IMFMediaSource *object)
3133 struct media_source *source;
3135 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
3137 if (source->source == object)
3139 if (!(source->flags & SOURCE_FLAG_END_OF_PRESENTATION))
3141 source->flags |= SOURCE_FLAG_END_OF_PRESENTATION;
3142 session_raise_end_of_presentation(session);
3145 break;
3150 static void session_sink_stream_marker(struct media_session *session, IMFStreamSink *stream_sink)
3152 struct topo_node *node;
3154 if (!(node = session_get_node_object(session, (IUnknown *)stream_sink, MF_TOPOLOGY_OUTPUT_NODE))
3155 || node->flags & TOPO_NODE_END_OF_STREAM)
3157 return;
3160 node->flags |= TOPO_NODE_END_OF_STREAM;
3162 if (session->presentation.flags & SESSION_FLAG_END_OF_PRESENTATION &&
3163 session_nodes_is_mask_set(session, MF_TOPOLOGY_OUTPUT_NODE, TOPO_NODE_END_OF_STREAM))
3165 session_set_topo_status(session, S_OK, MF_TOPOSTATUS_ENDED);
3166 session_set_caps(session, session->caps & ~MFSESSIONCAP_PAUSE);
3167 session_stop(session);
3171 static HRESULT WINAPI session_events_callback_Invoke(IMFAsyncCallback *iface, IMFAsyncResult *result)
3173 struct media_session *session = impl_from_events_callback_IMFAsyncCallback(iface);
3174 IMFMediaEventGenerator *event_source;
3175 IMFMediaEvent *event = NULL;
3176 MediaEventType event_type;
3177 IUnknown *object = NULL;
3178 IMFMediaSource *source;
3179 IMFMediaStream *stream;
3180 PROPVARIANT value;
3181 HRESULT hr;
3183 if (FAILED(hr = IMFAsyncResult_GetState(result, (IUnknown **)&event_source)))
3184 return hr;
3186 if (FAILED(hr = IMFMediaEventGenerator_EndGetEvent(event_source, result, &event)))
3188 WARN("Failed to get event from %p, hr %#x.\n", event_source, hr);
3189 goto failed;
3192 if (FAILED(hr = IMFMediaEvent_GetType(event, &event_type)))
3194 WARN("Failed to get event type, hr %#x.\n", hr);
3195 goto failed;
3198 value.vt = VT_EMPTY;
3199 if (FAILED(hr = IMFMediaEvent_GetValue(event, &value)))
3201 WARN("Failed to get event value, hr %#x.\n", hr);
3202 goto failed;
3205 switch (event_type)
3207 case MESourceStarted:
3208 case MESourcePaused:
3209 case MESourceStopped:
3210 case MEStreamStarted:
3211 case MEStreamPaused:
3212 case MEStreamStopped:
3214 EnterCriticalSection(&session->cs);
3215 session_set_source_object_state(session, (IUnknown *)event_source, event_type);
3216 LeaveCriticalSection(&session->cs);
3218 break;
3220 case MEBufferingStarted:
3221 case MEBufferingStopped:
3223 EnterCriticalSection(&session->cs);
3224 if (session_get_media_source(session, (IMFMediaSource *)event_source))
3226 if (event_type == MEBufferingStarted)
3227 IMFPresentationClock_Pause(session->clock);
3228 else
3229 IMFPresentationClock_Start(session->clock, PRESENTATION_CURRENT_POSITION);
3231 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
3233 LeaveCriticalSection(&session->cs);
3234 break;
3236 case MEReconnectStart:
3237 case MEReconnectEnd:
3239 EnterCriticalSection(&session->cs);
3240 if (session_get_media_source(session, (IMFMediaSource *)event_source))
3241 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
3242 LeaveCriticalSection(&session->cs);
3243 break;
3245 case MEExtendedType:
3246 case MERendererEvent:
3247 case MEStreamSinkFormatChanged:
3249 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
3250 break;
3252 case MENewStream:
3253 stream = (IMFMediaStream *)value.punkVal;
3255 if (value.vt != VT_UNKNOWN || !stream)
3257 WARN("Unexpected event value.\n");
3258 break;
3261 if (FAILED(hr = IMFMediaStream_GetMediaSource(stream, &source)))
3262 break;
3264 EnterCriticalSection(&session->cs);
3265 if (SUCCEEDED(hr = session_add_media_stream(session, source, stream)))
3266 hr = IMFMediaStream_BeginGetEvent(stream, &session->events_callback, (IUnknown *)stream);
3267 LeaveCriticalSection(&session->cs);
3269 IMFMediaSource_Release(source);
3271 break;
3272 case MEStreamSinkStarted:
3273 case MEStreamSinkPaused:
3274 case MEStreamSinkStopped:
3275 case MEStreamSinkPrerolled:
3277 EnterCriticalSection(&session->cs);
3278 session_set_sink_stream_state(session, (IMFStreamSink *)event_source, event_type);
3279 LeaveCriticalSection(&session->cs);
3281 break;
3282 case MEStreamSinkMarker:
3284 EnterCriticalSection(&session->cs);
3285 session_sink_stream_marker(session, (IMFStreamSink *)event_source);
3286 LeaveCriticalSection(&session->cs);
3288 break;
3289 case MEStreamSinkRequestSample:
3291 EnterCriticalSection(&session->cs);
3292 session_request_sample(session, (IMFStreamSink *)event_source);
3293 LeaveCriticalSection(&session->cs);
3295 break;
3296 case MEMediaSample:
3298 EnterCriticalSection(&session->cs);
3299 session_deliver_sample(session, (IMFMediaStream *)event_source, &value);
3300 LeaveCriticalSection(&session->cs);
3302 break;
3303 case MEEndOfStream:
3305 EnterCriticalSection(&session->cs);
3306 session_handle_end_of_stream(session, (IMFMediaStream *)event_source);
3307 LeaveCriticalSection(&session->cs);
3309 break;
3311 case MEEndOfPresentation:
3313 EnterCriticalSection(&session->cs);
3314 session_handle_end_of_presentation(session, (IMFMediaSource *)event_source);
3315 LeaveCriticalSection(&session->cs);
3317 break;
3318 case MEAudioSessionGroupingParamChanged:
3319 case MEAudioSessionIconChanged:
3320 case MEAudioSessionNameChanged:
3321 case MEAudioSessionVolumeChanged:
3323 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
3325 break;
3326 case MEAudioSessionDeviceRemoved:
3327 case MEAudioSessionDisconnected:
3328 case MEAudioSessionExclusiveModeOverride:
3329 case MEAudioSessionFormatChanged:
3330 case MEAudioSessionServerShutdown:
3332 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
3333 /* fallthrough */
3334 case MESinkInvalidated:
3336 EnterCriticalSection(&session->cs);
3337 session_sink_invalidated(session, event_type == MESinkInvalidated ? event : NULL,
3338 (IMFStreamSink *)event_source);
3339 LeaveCriticalSection(&session->cs);
3341 break;
3342 case MEQualityNotify:
3344 if (session->quality_manager)
3346 if (FAILED(IMFMediaEventGenerator_QueryInterface(event_source, &IID_IMFStreamSink, (void **)&object)))
3347 IMFMediaEventGenerator_QueryInterface(event_source, &IID_IMFTransform, (void **)&object);
3349 if (object)
3351 IMFQualityManager_NotifyQualityEvent(session->quality_manager, object, event);
3352 IUnknown_Release(object);
3356 break;
3357 default:
3361 PropVariantClear(&value);
3363 failed:
3364 if (event)
3365 IMFMediaEvent_Release(event);
3367 if (FAILED(hr = IMFMediaEventGenerator_BeginGetEvent(event_source, iface, (IUnknown *)event_source)))
3368 WARN("Failed to re-subscribe, hr %#x.\n", hr);
3370 IMFMediaEventGenerator_Release(event_source);
3372 return hr;
3375 static const IMFAsyncCallbackVtbl session_events_callback_vtbl =
3377 session_events_callback_QueryInterface,
3378 session_events_callback_AddRef,
3379 session_events_callback_Release,
3380 session_events_callback_GetParameters,
3381 session_events_callback_Invoke,
3384 static HRESULT WINAPI session_sink_finalizer_callback_QueryInterface(IMFAsyncCallback *iface, REFIID riid, void **obj)
3386 if (IsEqualIID(riid, &IID_IMFAsyncCallback) ||
3387 IsEqualIID(riid, &IID_IUnknown))
3389 *obj = iface;
3390 IMFAsyncCallback_AddRef(iface);
3391 return S_OK;
3394 WARN("Unsupported %s.\n", debugstr_guid(riid));
3395 *obj = NULL;
3396 return E_NOINTERFACE;
3399 static ULONG WINAPI session_sink_finalizer_callback_AddRef(IMFAsyncCallback *iface)
3401 struct media_session *session = impl_from_sink_finalizer_callback_IMFAsyncCallback(iface);
3402 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
3405 static ULONG WINAPI session_sink_finalizer_callback_Release(IMFAsyncCallback *iface)
3407 struct media_session *session = impl_from_sink_finalizer_callback_IMFAsyncCallback(iface);
3408 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
3411 static HRESULT WINAPI session_sink_finalizer_callback_GetParameters(IMFAsyncCallback *iface, DWORD *flags, DWORD *queue)
3413 return E_NOTIMPL;
3416 static HRESULT WINAPI session_sink_finalizer_callback_Invoke(IMFAsyncCallback *iface, IMFAsyncResult *result)
3418 struct media_session *session = impl_from_sink_finalizer_callback_IMFAsyncCallback(iface);
3419 IMFFinalizableMediaSink *fin_sink = NULL;
3420 BOOL sinks_finalized = TRUE;
3421 struct media_sink *sink;
3422 IUnknown *state;
3423 HRESULT hr;
3425 if (FAILED(hr = IMFAsyncResult_GetState(result, &state)))
3426 return hr;
3428 EnterCriticalSection(&session->cs);
3430 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
3432 if (state == (IUnknown *)sink->sink)
3434 if (FAILED(hr = IMFMediaSink_QueryInterface(sink->sink, &IID_IMFFinalizableMediaSink, (void **)&fin_sink)))
3435 WARN("Unexpected, missing IMFFinalizableSink, hr %#x.\n", hr);
3437 else
3439 sinks_finalized &= sink->finalized;
3440 if (!sinks_finalized)
3441 break;
3445 IUnknown_Release(state);
3447 if (fin_sink)
3449 /* Complete session transition, or close prematurely on error. */
3450 if (SUCCEEDED(hr = IMFFinalizableMediaSink_EndFinalize(fin_sink, result)))
3452 sink->finalized = TRUE;
3453 if (sinks_finalized)
3454 session_set_closed(session, hr);
3456 IMFFinalizableMediaSink_Release(fin_sink);
3459 LeaveCriticalSection(&session->cs);
3461 return S_OK;
3464 static const IMFAsyncCallbackVtbl session_sink_finalizer_callback_vtbl =
3466 session_sink_finalizer_callback_QueryInterface,
3467 session_sink_finalizer_callback_AddRef,
3468 session_sink_finalizer_callback_Release,
3469 session_sink_finalizer_callback_GetParameters,
3470 session_sink_finalizer_callback_Invoke,
3473 static HRESULT WINAPI session_rate_support_QueryInterface(IMFRateSupport *iface, REFIID riid, void **obj)
3475 struct media_session *session = impl_session_from_IMFRateSupport(iface);
3476 return IMFMediaSession_QueryInterface(&session->IMFMediaSession_iface, riid, obj);
3479 static ULONG WINAPI session_rate_support_AddRef(IMFRateSupport *iface)
3481 struct media_session *session = impl_session_from_IMFRateSupport(iface);
3482 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
3485 static ULONG WINAPI session_rate_support_Release(IMFRateSupport *iface)
3487 struct media_session *session = impl_session_from_IMFRateSupport(iface);
3488 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
3491 static HRESULT session_presentation_object_get_rate(IUnknown *object, MFRATE_DIRECTION direction,
3492 BOOL thin, BOOL fastest, float *result)
3494 IMFRateSupport *rate_support;
3495 float rate;
3496 HRESULT hr;
3498 /* For objects that don't support rate control, it's assumed that only forward direction is allowed, at 1.0f. */
3500 if (FAILED(hr = MFGetService(object, &MF_RATE_CONTROL_SERVICE, &IID_IMFRateSupport, (void **)&rate_support)))
3502 if (direction == MFRATE_FORWARD)
3504 *result = 1.0f;
3505 return S_OK;
3507 else
3508 return MF_E_REVERSE_UNSUPPORTED;
3511 rate = 0.0f;
3512 if (fastest)
3514 if (SUCCEEDED(hr = IMFRateSupport_GetFastestRate(rate_support, direction, thin, &rate)))
3515 *result = min(fabsf(rate), *result);
3517 else
3519 if (SUCCEEDED(hr = IMFRateSupport_GetSlowestRate(rate_support, direction, thin, &rate)))
3520 *result = max(fabsf(rate), *result);
3523 IMFRateSupport_Release(rate_support);
3525 return hr;
3528 static HRESULT session_get_presentation_rate(struct media_session *session, MFRATE_DIRECTION direction,
3529 BOOL thin, BOOL fastest, float *result)
3531 struct media_source *source;
3532 struct media_sink *sink;
3533 HRESULT hr = E_POINTER;
3535 *result = 0.0f;
3537 EnterCriticalSection(&session->cs);
3539 if (session->presentation.topo_status != MF_TOPOSTATUS_INVALID)
3541 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
3543 if (FAILED(hr = session_presentation_object_get_rate((IUnknown *)source->source, direction, thin, fastest, result)))
3544 break;
3547 if (SUCCEEDED(hr))
3549 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
3551 if (FAILED(hr = session_presentation_object_get_rate((IUnknown *)sink->sink, direction, thin, fastest, result)))
3552 break;
3557 LeaveCriticalSection(&session->cs);
3559 return hr;
3562 static HRESULT WINAPI session_rate_support_GetSlowestRate(IMFRateSupport *iface, MFRATE_DIRECTION direction,
3563 BOOL thin, float *rate)
3565 struct media_session *session = impl_session_from_IMFRateSupport(iface);
3567 TRACE("%p, %d, %d, %p.\n", iface, direction, thin, rate);
3569 return session_get_presentation_rate(session, direction, thin, FALSE, rate);
3572 static HRESULT WINAPI session_rate_support_GetFastestRate(IMFRateSupport *iface, MFRATE_DIRECTION direction,
3573 BOOL thin, float *rate)
3575 struct media_session *session = impl_session_from_IMFRateSupport(iface);
3577 TRACE("%p, %d, %d, %p.\n", iface, direction, thin, rate);
3579 return session_get_presentation_rate(session, direction, thin, TRUE, rate);
3582 static HRESULT WINAPI session_rate_support_IsRateSupported(IMFRateSupport *iface, BOOL thin, float rate,
3583 float *nearest_supported_rate)
3585 FIXME("%p, %d, %f, %p.\n", iface, thin, rate, nearest_supported_rate);
3587 return E_NOTIMPL;
3590 static const IMFRateSupportVtbl session_rate_support_vtbl =
3592 session_rate_support_QueryInterface,
3593 session_rate_support_AddRef,
3594 session_rate_support_Release,
3595 session_rate_support_GetSlowestRate,
3596 session_rate_support_GetFastestRate,
3597 session_rate_support_IsRateSupported,
3600 static HRESULT WINAPI session_rate_control_QueryInterface(IMFRateControl *iface, REFIID riid, void **obj)
3602 struct media_session *session = impl_session_from_IMFRateControl(iface);
3603 return IMFMediaSession_QueryInterface(&session->IMFMediaSession_iface, riid, obj);
3606 static ULONG WINAPI session_rate_control_AddRef(IMFRateControl *iface)
3608 struct media_session *session = impl_session_from_IMFRateControl(iface);
3609 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
3612 static ULONG WINAPI session_rate_control_Release(IMFRateControl *iface)
3614 struct media_session *session = impl_session_from_IMFRateControl(iface);
3615 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
3618 static HRESULT WINAPI session_rate_control_SetRate(IMFRateControl *iface, BOOL thin, float rate)
3620 FIXME("%p, %d, %f.\n", iface, thin, rate);
3622 return E_NOTIMPL;
3625 static HRESULT WINAPI session_rate_control_GetRate(IMFRateControl *iface, BOOL *thin, float *rate)
3627 struct media_session *session = impl_session_from_IMFRateControl(iface);
3629 TRACE("%p, %p, %p.\n", iface, thin, rate);
3631 return IMFRateControl_GetRate(session->clock_rate_control, thin, rate);
3634 static const IMFRateControlVtbl session_rate_control_vtbl =
3636 session_rate_control_QueryInterface,
3637 session_rate_control_AddRef,
3638 session_rate_control_Release,
3639 session_rate_control_SetRate,
3640 session_rate_control_GetRate,
3643 static HRESULT WINAPI node_attribute_editor_QueryInterface(IMFTopologyNodeAttributeEditor *iface,
3644 REFIID riid, void **obj)
3646 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), obj);
3648 if (IsEqualIID(riid, &IID_IMFTopologyNodeAttributeEditor) ||
3649 IsEqualIID(riid, &IID_IUnknown))
3651 *obj = iface;
3652 IMFTopologyNodeAttributeEditor_AddRef(iface);
3653 return S_OK;
3656 WARN("Unsupported interface %s.\n", debugstr_guid(riid));
3657 *obj = NULL;
3658 return E_NOINTERFACE;
3661 static ULONG WINAPI node_attribute_editor_AddRef(IMFTopologyNodeAttributeEditor *iface)
3663 struct media_session *session = impl_session_from_IMFTopologyNodeAttributeEditor(iface);
3664 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
3667 static ULONG WINAPI node_attribute_editor_Release(IMFTopologyNodeAttributeEditor *iface)
3669 struct media_session *session = impl_session_from_IMFTopologyNodeAttributeEditor(iface);
3670 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
3673 static HRESULT WINAPI node_attribute_editor_UpdateNodeAttributes(IMFTopologyNodeAttributeEditor *iface,
3674 TOPOID id, DWORD count, MFTOPONODE_ATTRIBUTE_UPDATE *updates)
3676 FIXME("%p, %s, %u, %p.\n", iface, wine_dbgstr_longlong(id), count, updates);
3678 return E_NOTIMPL;
3681 static const IMFTopologyNodeAttributeEditorVtbl node_attribute_editor_vtbl =
3683 node_attribute_editor_QueryInterface,
3684 node_attribute_editor_AddRef,
3685 node_attribute_editor_Release,
3686 node_attribute_editor_UpdateNodeAttributes,
3689 /***********************************************************************
3690 * MFCreateMediaSession (mf.@)
3692 HRESULT WINAPI MFCreateMediaSession(IMFAttributes *config, IMFMediaSession **session)
3694 BOOL without_quality_manager = FALSE;
3695 struct media_session *object;
3696 HRESULT hr;
3698 TRACE("%p, %p.\n", config, session);
3700 object = heap_alloc_zero(sizeof(*object));
3701 if (!object)
3702 return E_OUTOFMEMORY;
3704 object->IMFMediaSession_iface.lpVtbl = &mfmediasessionvtbl;
3705 object->IMFGetService_iface.lpVtbl = &session_get_service_vtbl;
3706 object->IMFRateSupport_iface.lpVtbl = &session_rate_support_vtbl;
3707 object->IMFRateControl_iface.lpVtbl = &session_rate_control_vtbl;
3708 object->IMFTopologyNodeAttributeEditor_iface.lpVtbl = &node_attribute_editor_vtbl;
3709 object->commands_callback.lpVtbl = &session_commands_callback_vtbl;
3710 object->events_callback.lpVtbl = &session_events_callback_vtbl;
3711 object->sink_finalizer_callback.lpVtbl = &session_sink_finalizer_callback_vtbl;
3712 object->refcount = 1;
3713 list_init(&object->topologies);
3714 list_init(&object->commands);
3715 list_init(&object->presentation.sources);
3716 list_init(&object->presentation.sinks);
3717 list_init(&object->presentation.nodes);
3718 InitializeCriticalSection(&object->cs);
3720 if (FAILED(hr = MFCreateTopology(&object->presentation.current_topology)))
3721 goto failed;
3723 if (FAILED(hr = MFCreateEventQueue(&object->event_queue)))
3724 goto failed;
3726 if (FAILED(hr = MFCreatePresentationClock(&object->clock)))
3727 goto failed;
3729 if (FAILED(hr = MFCreateSystemTimeSource(&object->system_time_source)))
3730 goto failed;
3732 if (FAILED(hr = IMFPresentationClock_QueryInterface(object->clock, &IID_IMFRateControl,
3733 (void **)&object->clock_rate_control)))
3735 goto failed;
3738 if (config)
3740 GUID clsid;
3742 if (SUCCEEDED(IMFAttributes_GetGUID(config, &MF_SESSION_TOPOLOADER, &clsid)))
3744 if (FAILED(hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IMFTopoLoader,
3745 (void **)&object->topo_loader)))
3747 WARN("Failed to create custom topology loader, hr %#x.\n", hr);
3751 if (SUCCEEDED(IMFAttributes_GetGUID(config, &MF_SESSION_QUALITY_MANAGER, &clsid)))
3753 if (!(without_quality_manager = IsEqualGUID(&clsid, &GUID_NULL)))
3755 if (FAILED(hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IMFQualityManager,
3756 (void **)&object->quality_manager)))
3758 WARN("Failed to create custom quality manager, hr %#x.\n", hr);
3764 if (!object->topo_loader && FAILED(hr = MFCreateTopoLoader(&object->topo_loader)))
3765 goto failed;
3767 if (!object->quality_manager && !without_quality_manager &&
3768 FAILED(hr = MFCreateStandardQualityManager(&object->quality_manager)))
3770 goto failed;
3773 if (object->quality_manager && FAILED(hr = IMFQualityManager_NotifyPresentationClock(object->quality_manager,
3774 object->clock)))
3776 goto failed;
3779 *session = &object->IMFMediaSession_iface;
3781 return S_OK;
3783 failed:
3784 IMFMediaSession_Release(&object->IMFMediaSession_iface);
3785 return hr;
3788 static HRESULT WINAPI sink_notification_QueryInterface(IUnknown *iface, REFIID riid, void **out)
3790 if (IsEqualIID(riid, &IID_IUnknown))
3792 *out = iface;
3793 IUnknown_AddRef(iface);
3794 return S_OK;
3797 WARN("Unsupported %s.\n", debugstr_guid(riid));
3798 *out = NULL;
3799 return E_NOINTERFACE;
3802 static ULONG WINAPI sink_notification_AddRef(IUnknown *iface)
3804 struct sink_notification *notification = impl_sink_notification_from_IUnknown(iface);
3805 ULONG refcount = InterlockedIncrement(&notification->refcount);
3807 TRACE("%p, refcount %u.\n", iface, refcount);
3809 return refcount;
3812 static ULONG WINAPI sink_notification_Release(IUnknown *iface)
3814 struct sink_notification *notification = impl_sink_notification_from_IUnknown(iface);
3815 ULONG refcount = InterlockedDecrement(&notification->refcount);
3817 TRACE("%p, refcount %u.\n", iface, refcount);
3819 if (!refcount)
3821 IMFClockStateSink_Release(notification->sink);
3822 heap_free(notification);
3825 return refcount;
3828 static const IUnknownVtbl sinknotificationvtbl =
3830 sink_notification_QueryInterface,
3831 sink_notification_AddRef,
3832 sink_notification_Release,
3835 static void clock_notify_async_sink(struct presentation_clock *clock, MFTIME system_time,
3836 struct clock_state_change_param param, enum clock_notification notification, IMFClockStateSink *sink)
3838 struct sink_notification *object;
3839 IMFAsyncResult *result;
3840 HRESULT hr;
3842 object = heap_alloc(sizeof(*object));
3843 if (!object)
3844 return;
3846 object->IUnknown_iface.lpVtbl = &sinknotificationvtbl;
3847 object->refcount = 1;
3848 object->system_time = system_time;
3849 object->param = param;
3850 object->notification = notification;
3851 object->sink = sink;
3852 IMFClockStateSink_AddRef(object->sink);
3854 hr = MFCreateAsyncResult(&object->IUnknown_iface, &clock->sink_callback, NULL, &result);
3855 IUnknown_Release(&object->IUnknown_iface);
3856 if (SUCCEEDED(hr))
3858 MFPutWorkItemEx(MFASYNC_CALLBACK_QUEUE_STANDARD, result);
3859 IMFAsyncResult_Release(result);
3863 static HRESULT WINAPI present_clock_QueryInterface(IMFPresentationClock *iface, REFIID riid, void **out)
3865 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
3867 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
3869 if (IsEqualIID(riid, &IID_IMFPresentationClock) ||
3870 IsEqualIID(riid, &IID_IMFClock) ||
3871 IsEqualIID(riid, &IID_IUnknown))
3873 *out = &clock->IMFPresentationClock_iface;
3875 else if (IsEqualIID(riid, &IID_IMFRateControl))
3877 *out = &clock->IMFRateControl_iface;
3879 else if (IsEqualIID(riid, &IID_IMFTimer))
3881 *out = &clock->IMFTimer_iface;
3883 else if (IsEqualIID(riid, &IID_IMFShutdown))
3885 *out = &clock->IMFShutdown_iface;
3887 else
3889 WARN("Unsupported %s.\n", debugstr_guid(riid));
3890 *out = NULL;
3891 return E_NOINTERFACE;
3894 IUnknown_AddRef((IUnknown *)*out);
3895 return S_OK;
3898 static ULONG WINAPI present_clock_AddRef(IMFPresentationClock *iface)
3900 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
3901 ULONG refcount = InterlockedIncrement(&clock->refcount);
3903 TRACE("%p, refcount %u.\n", iface, refcount);
3905 return refcount;
3908 static ULONG WINAPI present_clock_Release(IMFPresentationClock *iface)
3910 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
3911 ULONG refcount = InterlockedDecrement(&clock->refcount);
3912 struct clock_timer *timer, *timer2;
3913 struct clock_sink *sink, *sink2;
3915 TRACE("%p, refcount %u.\n", iface, refcount);
3917 if (!refcount)
3919 if (clock->time_source)
3920 IMFPresentationTimeSource_Release(clock->time_source);
3921 if (clock->time_source_sink)
3922 IMFClockStateSink_Release(clock->time_source_sink);
3923 LIST_FOR_EACH_ENTRY_SAFE(sink, sink2, &clock->sinks, struct clock_sink, entry)
3925 list_remove(&sink->entry);
3926 IMFClockStateSink_Release(sink->state_sink);
3927 heap_free(sink);
3929 LIST_FOR_EACH_ENTRY_SAFE(timer, timer2, &clock->timers, struct clock_timer, entry)
3931 list_remove(&timer->entry);
3932 IUnknown_Release(&timer->IUnknown_iface);
3934 DeleteCriticalSection(&clock->cs);
3935 heap_free(clock);
3938 return refcount;
3941 static HRESULT WINAPI present_clock_GetClockCharacteristics(IMFPresentationClock *iface, DWORD *flags)
3943 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
3944 HRESULT hr = MF_E_CLOCK_NO_TIME_SOURCE;
3946 TRACE("%p, %p.\n", iface, flags);
3948 EnterCriticalSection(&clock->cs);
3949 if (clock->time_source)
3950 hr = IMFPresentationTimeSource_GetClockCharacteristics(clock->time_source, flags);
3951 LeaveCriticalSection(&clock->cs);
3953 return hr;
3956 static HRESULT WINAPI present_clock_GetCorrelatedTime(IMFPresentationClock *iface, DWORD reserved,
3957 LONGLONG *clock_time, MFTIME *system_time)
3959 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
3960 HRESULT hr = MF_E_CLOCK_NO_TIME_SOURCE;
3962 TRACE("%p, %#x, %p, %p.\n", iface, reserved, clock_time, system_time);
3964 EnterCriticalSection(&clock->cs);
3965 if (clock->time_source)
3966 hr = IMFPresentationTimeSource_GetCorrelatedTime(clock->time_source, reserved, clock_time, system_time);
3967 LeaveCriticalSection(&clock->cs);
3969 return hr;
3972 static HRESULT WINAPI present_clock_GetContinuityKey(IMFPresentationClock *iface, DWORD *key)
3974 TRACE("%p, %p.\n", iface, key);
3976 *key = 0;
3978 return S_OK;
3981 static HRESULT WINAPI present_clock_GetState(IMFPresentationClock *iface, DWORD reserved, MFCLOCK_STATE *state)
3983 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
3985 TRACE("%p, %#x, %p.\n", iface, reserved, state);
3987 EnterCriticalSection(&clock->cs);
3988 *state = clock->state;
3989 LeaveCriticalSection(&clock->cs);
3991 return S_OK;
3994 static HRESULT WINAPI present_clock_GetProperties(IMFPresentationClock *iface, MFCLOCK_PROPERTIES *props)
3996 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
3997 HRESULT hr = MF_E_CLOCK_NO_TIME_SOURCE;
3999 TRACE("%p, %p.\n", iface, props);
4001 EnterCriticalSection(&clock->cs);
4002 if (clock->time_source)
4003 hr = IMFPresentationTimeSource_GetProperties(clock->time_source, props);
4004 LeaveCriticalSection(&clock->cs);
4006 return hr;
4009 static HRESULT WINAPI present_clock_SetTimeSource(IMFPresentationClock *iface,
4010 IMFPresentationTimeSource *time_source)
4012 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4013 MFCLOCK_PROPERTIES props;
4014 IMFClock *source_clock;
4015 HRESULT hr;
4017 TRACE("%p, %p.\n", iface, time_source);
4019 EnterCriticalSection(&clock->cs);
4021 if (clock->time_source)
4022 IMFPresentationTimeSource_Release(clock->time_source);
4023 if (clock->time_source_sink)
4024 IMFClockStateSink_Release(clock->time_source_sink);
4025 clock->time_source = NULL;
4026 clock->time_source_sink = NULL;
4028 hr = IMFPresentationTimeSource_QueryInterface(time_source, &IID_IMFClockStateSink, (void **)&clock->time_source_sink);
4029 if (SUCCEEDED(hr))
4031 clock->time_source = time_source;
4032 IMFPresentationTimeSource_AddRef(clock->time_source);
4035 if (SUCCEEDED(IMFPresentationTimeSource_GetUnderlyingClock(time_source, &source_clock)))
4037 if (SUCCEEDED(IMFClock_GetProperties(source_clock, &props)))
4038 clock->frequency = props.qwClockFrequency;
4039 IMFClock_Release(source_clock);
4042 if (!clock->frequency)
4043 clock->frequency = MFCLOCK_FREQUENCY_HNS;
4045 LeaveCriticalSection(&clock->cs);
4047 return hr;
4050 static HRESULT WINAPI present_clock_GetTimeSource(IMFPresentationClock *iface,
4051 IMFPresentationTimeSource **time_source)
4053 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4054 HRESULT hr = S_OK;
4056 TRACE("%p, %p.\n", iface, time_source);
4058 if (!time_source)
4059 return E_INVALIDARG;
4061 EnterCriticalSection(&clock->cs);
4062 if (clock->time_source)
4064 *time_source = clock->time_source;
4065 IMFPresentationTimeSource_AddRef(*time_source);
4067 else
4068 hr = MF_E_CLOCK_NO_TIME_SOURCE;
4069 LeaveCriticalSection(&clock->cs);
4071 return hr;
4074 static HRESULT WINAPI present_clock_GetTime(IMFPresentationClock *iface, MFTIME *time)
4076 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4077 HRESULT hr = MF_E_CLOCK_NO_TIME_SOURCE;
4078 MFTIME systime;
4080 TRACE("%p, %p.\n", iface, time);
4082 if (!time)
4083 return E_POINTER;
4085 EnterCriticalSection(&clock->cs);
4086 if (clock->time_source)
4087 hr = IMFPresentationTimeSource_GetCorrelatedTime(clock->time_source, 0, time, &systime);
4088 LeaveCriticalSection(&clock->cs);
4090 return hr;
4093 static HRESULT WINAPI present_clock_AddClockStateSink(IMFPresentationClock *iface, IMFClockStateSink *state_sink)
4095 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4096 struct clock_sink *sink, *cur;
4097 HRESULT hr = S_OK;
4099 TRACE("%p, %p.\n", iface, state_sink);
4101 if (!state_sink)
4102 return E_INVALIDARG;
4104 sink = heap_alloc(sizeof(*sink));
4105 if (!sink)
4106 return E_OUTOFMEMORY;
4108 sink->state_sink = state_sink;
4109 IMFClockStateSink_AddRef(sink->state_sink);
4111 EnterCriticalSection(&clock->cs);
4112 LIST_FOR_EACH_ENTRY(cur, &clock->sinks, struct clock_sink, entry)
4114 if (cur->state_sink == state_sink)
4116 hr = E_INVALIDARG;
4117 break;
4120 if (SUCCEEDED(hr))
4122 static const enum clock_notification notifications[MFCLOCK_STATE_PAUSED + 1] =
4124 /* MFCLOCK_STATE_INVALID */ 0, /* Does not apply */
4125 /* MFCLOCK_STATE_RUNNING */ CLOCK_NOTIFY_START,
4126 /* MFCLOCK_STATE_STOPPED */ CLOCK_NOTIFY_STOP,
4127 /* MFCLOCK_STATE_PAUSED */ CLOCK_NOTIFY_PAUSE,
4129 struct clock_state_change_param param;
4131 if (!clock->is_shut_down && clock->state != MFCLOCK_STATE_INVALID)
4133 param.u.offset = clock->start_offset;
4134 clock_notify_async_sink(clock, MFGetSystemTime(), param, notifications[clock->state], sink->state_sink);
4137 list_add_tail(&clock->sinks, &sink->entry);
4139 LeaveCriticalSection(&clock->cs);
4141 if (FAILED(hr))
4143 IMFClockStateSink_Release(sink->state_sink);
4144 heap_free(sink);
4147 return hr;
4150 static HRESULT WINAPI present_clock_RemoveClockStateSink(IMFPresentationClock *iface,
4151 IMFClockStateSink *state_sink)
4153 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4154 struct clock_sink *sink;
4156 TRACE("%p, %p.\n", iface, state_sink);
4158 if (!state_sink)
4159 return E_INVALIDARG;
4161 EnterCriticalSection(&clock->cs);
4162 LIST_FOR_EACH_ENTRY(sink, &clock->sinks, struct clock_sink, entry)
4164 if (sink->state_sink == state_sink)
4166 IMFClockStateSink_Release(sink->state_sink);
4167 list_remove(&sink->entry);
4168 heap_free(sink);
4169 break;
4172 LeaveCriticalSection(&clock->cs);
4174 return S_OK;
4177 static HRESULT clock_call_state_change(MFTIME system_time, struct clock_state_change_param param,
4178 enum clock_notification notification, IMFClockStateSink *sink)
4180 HRESULT hr = S_OK;
4182 switch (notification)
4184 case CLOCK_NOTIFY_START:
4185 hr = IMFClockStateSink_OnClockStart(sink, system_time, param.u.offset);
4186 break;
4187 case CLOCK_NOTIFY_STOP:
4188 hr = IMFClockStateSink_OnClockStop(sink, system_time);
4189 break;
4190 case CLOCK_NOTIFY_PAUSE:
4191 hr = IMFClockStateSink_OnClockPause(sink, system_time);
4192 break;
4193 case CLOCK_NOTIFY_RESTART:
4194 hr = IMFClockStateSink_OnClockRestart(sink, system_time);
4195 break;
4196 case CLOCK_NOTIFY_SET_RATE:
4197 /* System time source does not allow 0.0 rate, presentation clock allows it without raising errors. */
4198 IMFClockStateSink_OnClockSetRate(sink, system_time, param.u.rate);
4199 break;
4200 default:
4204 return hr;
4207 static HRESULT clock_change_state(struct presentation_clock *clock, enum clock_command command,
4208 struct clock_state_change_param param)
4210 static const BYTE state_change_is_allowed[MFCLOCK_STATE_PAUSED+1][CLOCK_CMD_MAX] =
4211 { /* S S* P, R */
4212 /* INVALID */ { 1, 1, 1, 1 },
4213 /* RUNNING */ { 1, 1, 1, 1 },
4214 /* STOPPED */ { 1, 1, 0, 1 },
4215 /* PAUSED */ { 1, 1, 0, 1 },
4217 static const MFCLOCK_STATE states[CLOCK_CMD_MAX] =
4219 /* CLOCK_CMD_START */ MFCLOCK_STATE_RUNNING,
4220 /* CLOCK_CMD_STOP */ MFCLOCK_STATE_STOPPED,
4221 /* CLOCK_CMD_PAUSE */ MFCLOCK_STATE_PAUSED,
4222 /* CLOCK_CMD_SET_RATE */ 0, /* Unused */
4224 static const enum clock_notification notifications[CLOCK_CMD_MAX] =
4226 /* CLOCK_CMD_START */ CLOCK_NOTIFY_START,
4227 /* CLOCK_CMD_STOP */ CLOCK_NOTIFY_STOP,
4228 /* CLOCK_CMD_PAUSE */ CLOCK_NOTIFY_PAUSE,
4229 /* CLOCK_CMD_SET_RATE */ CLOCK_NOTIFY_SET_RATE,
4231 enum clock_notification notification;
4232 struct clock_sink *sink;
4233 MFCLOCK_STATE old_state;
4234 IMFAsyncResult *result;
4235 MFTIME system_time;
4236 HRESULT hr;
4238 if (!clock->time_source)
4239 return MF_E_CLOCK_NO_TIME_SOURCE;
4241 if (command != CLOCK_CMD_SET_RATE && clock->state == states[command] && clock->state != MFCLOCK_STATE_RUNNING)
4242 return MF_E_CLOCK_STATE_ALREADY_SET;
4244 if (!state_change_is_allowed[clock->state][command])
4245 return MF_E_INVALIDREQUEST;
4247 system_time = MFGetSystemTime();
4249 if (command == CLOCK_CMD_START && clock->state == MFCLOCK_STATE_PAUSED &&
4250 param.u.offset == PRESENTATION_CURRENT_POSITION)
4252 notification = CLOCK_NOTIFY_RESTART;
4254 else
4255 notification = notifications[command];
4257 if (FAILED(hr = clock_call_state_change(system_time, param, notification, clock->time_source_sink)))
4258 return hr;
4260 old_state = clock->state;
4261 if (command != CLOCK_CMD_SET_RATE)
4262 clock->state = states[command];
4264 /* Dump all pending timer requests immediately on start; otherwise try to cancel scheduled items when
4265 transitioning from running state. */
4266 if ((clock->state == MFCLOCK_STATE_RUNNING) ^ (old_state == MFCLOCK_STATE_RUNNING))
4268 struct clock_timer *timer, *timer2;
4270 if (clock->state == MFCLOCK_STATE_RUNNING)
4272 LIST_FOR_EACH_ENTRY_SAFE(timer, timer2, &clock->timers, struct clock_timer, entry)
4274 list_remove(&timer->entry);
4275 hr = MFCreateAsyncResult(&timer->IUnknown_iface, &clock->timer_callback, NULL, &result);
4276 IUnknown_Release(&timer->IUnknown_iface);
4277 if (SUCCEEDED(hr))
4279 MFPutWorkItemEx(MFASYNC_CALLBACK_QUEUE_TIMER, result);
4280 IMFAsyncResult_Release(result);
4284 else
4286 LIST_FOR_EACH_ENTRY(timer, &clock->timers, struct clock_timer, entry)
4288 if (timer->key)
4290 MFCancelWorkItem(timer->key);
4291 timer->key = 0;
4297 LIST_FOR_EACH_ENTRY(sink, &clock->sinks, struct clock_sink, entry)
4299 clock_notify_async_sink(clock, system_time, param, notification, sink->state_sink);
4302 return S_OK;
4305 static HRESULT WINAPI present_clock_Start(IMFPresentationClock *iface, LONGLONG start_offset)
4307 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4308 struct clock_state_change_param param = {{0}};
4309 HRESULT hr;
4311 TRACE("%p, %s.\n", iface, debugstr_time(start_offset));
4313 EnterCriticalSection(&clock->cs);
4314 clock->start_offset = param.u.offset = start_offset;
4315 hr = clock_change_state(clock, CLOCK_CMD_START, param);
4316 LeaveCriticalSection(&clock->cs);
4318 return hr;
4321 static HRESULT WINAPI present_clock_Stop(IMFPresentationClock *iface)
4323 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4324 struct clock_state_change_param param = {{0}};
4325 HRESULT hr;
4327 TRACE("%p.\n", iface);
4329 EnterCriticalSection(&clock->cs);
4330 hr = clock_change_state(clock, CLOCK_CMD_STOP, param);
4331 LeaveCriticalSection(&clock->cs);
4333 return hr;
4336 static HRESULT WINAPI present_clock_Pause(IMFPresentationClock *iface)
4338 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4339 struct clock_state_change_param param = {{0}};
4340 HRESULT hr;
4342 TRACE("%p.\n", iface);
4344 EnterCriticalSection(&clock->cs);
4345 hr = clock_change_state(clock, CLOCK_CMD_PAUSE, param);
4346 LeaveCriticalSection(&clock->cs);
4348 return hr;
4351 static const IMFPresentationClockVtbl presentationclockvtbl =
4353 present_clock_QueryInterface,
4354 present_clock_AddRef,
4355 present_clock_Release,
4356 present_clock_GetClockCharacteristics,
4357 present_clock_GetCorrelatedTime,
4358 present_clock_GetContinuityKey,
4359 present_clock_GetState,
4360 present_clock_GetProperties,
4361 present_clock_SetTimeSource,
4362 present_clock_GetTimeSource,
4363 present_clock_GetTime,
4364 present_clock_AddClockStateSink,
4365 present_clock_RemoveClockStateSink,
4366 present_clock_Start,
4367 present_clock_Stop,
4368 present_clock_Pause,
4371 static HRESULT WINAPI present_clock_rate_control_QueryInterface(IMFRateControl *iface, REFIID riid, void **out)
4373 struct presentation_clock *clock = impl_from_IMFRateControl(iface);
4374 return IMFPresentationClock_QueryInterface(&clock->IMFPresentationClock_iface, riid, out);
4377 static ULONG WINAPI present_clock_rate_control_AddRef(IMFRateControl *iface)
4379 struct presentation_clock *clock = impl_from_IMFRateControl(iface);
4380 return IMFPresentationClock_AddRef(&clock->IMFPresentationClock_iface);
4383 static ULONG WINAPI present_clock_rate_control_Release(IMFRateControl *iface)
4385 struct presentation_clock *clock = impl_from_IMFRateControl(iface);
4386 return IMFPresentationClock_Release(&clock->IMFPresentationClock_iface);
4389 static HRESULT WINAPI present_clock_rate_SetRate(IMFRateControl *iface, BOOL thin, float rate)
4391 struct presentation_clock *clock = impl_from_IMFRateControl(iface);
4392 struct clock_state_change_param param;
4393 HRESULT hr;
4395 TRACE("%p, %d, %f.\n", iface, thin, rate);
4397 if (thin)
4398 return MF_E_THINNING_UNSUPPORTED;
4400 EnterCriticalSection(&clock->cs);
4401 param.u.rate = rate;
4402 if (SUCCEEDED(hr = clock_change_state(clock, CLOCK_CMD_SET_RATE, param)))
4403 clock->rate = rate;
4404 LeaveCriticalSection(&clock->cs);
4406 return hr;
4409 static HRESULT WINAPI present_clock_rate_GetRate(IMFRateControl *iface, BOOL *thin, float *rate)
4411 struct presentation_clock *clock = impl_from_IMFRateControl(iface);
4413 TRACE("%p, %p, %p.\n", iface, thin, rate);
4415 if (!rate)
4416 return E_INVALIDARG;
4418 if (thin)
4419 *thin = FALSE;
4421 EnterCriticalSection(&clock->cs);
4422 *rate = clock->rate;
4423 LeaveCriticalSection(&clock->cs);
4425 return S_OK;
4428 static const IMFRateControlVtbl presentclockratecontrolvtbl =
4430 present_clock_rate_control_QueryInterface,
4431 present_clock_rate_control_AddRef,
4432 present_clock_rate_control_Release,
4433 present_clock_rate_SetRate,
4434 present_clock_rate_GetRate,
4437 static HRESULT WINAPI present_clock_timer_QueryInterface(IMFTimer *iface, REFIID riid, void **out)
4439 struct presentation_clock *clock = impl_from_IMFTimer(iface);
4440 return IMFPresentationClock_QueryInterface(&clock->IMFPresentationClock_iface, riid, out);
4443 static ULONG WINAPI present_clock_timer_AddRef(IMFTimer *iface)
4445 struct presentation_clock *clock = impl_from_IMFTimer(iface);
4446 return IMFPresentationClock_AddRef(&clock->IMFPresentationClock_iface);
4449 static ULONG WINAPI present_clock_timer_Release(IMFTimer *iface)
4451 struct presentation_clock *clock = impl_from_IMFTimer(iface);
4452 return IMFPresentationClock_Release(&clock->IMFPresentationClock_iface);
4455 static HRESULT present_clock_schedule_timer(struct presentation_clock *clock, DWORD flags, LONGLONG time,
4456 struct clock_timer *timer)
4458 IMFAsyncResult *result;
4459 MFTIME systime, clocktime;
4460 LONGLONG frequency;
4461 HRESULT hr;
4463 if (!(flags & MFTIMER_RELATIVE))
4465 if (FAILED(hr = IMFPresentationTimeSource_GetCorrelatedTime(clock->time_source, 0, &clocktime, &systime)))
4467 WARN("Failed to get clock time, hr %#x.\n", hr);
4468 return hr;
4470 time -= clocktime;
4473 frequency = clock->frequency / 1000;
4474 time /= frequency;
4476 /* Scheduled item is using clock instance callback, with timer instance as an object. Clock callback will
4477 call user callback and cleanup timer list. */
4479 if (FAILED(hr = MFCreateAsyncResult(&timer->IUnknown_iface, &clock->timer_callback, NULL, &result)))
4480 return hr;
4482 hr = MFScheduleWorkItemEx(result, -time, &timer->key);
4483 IMFAsyncResult_Release(result);
4485 return hr;
4488 static HRESULT WINAPI clock_timer_QueryInterface(IUnknown *iface, REFIID riid, void **obj)
4490 if (IsEqualIID(riid, &IID_IUnknown))
4492 *obj = iface;
4493 IUnknown_AddRef(iface);
4494 return S_OK;
4497 *obj = NULL;
4498 return E_NOINTERFACE;
4501 static ULONG WINAPI clock_timer_AddRef(IUnknown *iface)
4503 struct clock_timer *timer = impl_clock_timer_from_IUnknown(iface);
4504 return InterlockedIncrement(&timer->refcount);
4507 static ULONG WINAPI clock_timer_Release(IUnknown *iface)
4509 struct clock_timer *timer = impl_clock_timer_from_IUnknown(iface);
4510 ULONG refcount = InterlockedDecrement(&timer->refcount);
4512 if (!refcount)
4514 IMFAsyncResult_Release(timer->result);
4515 IMFAsyncCallback_Release(timer->callback);
4516 heap_free(timer);
4519 return refcount;
4522 static const IUnknownVtbl clock_timer_vtbl =
4524 clock_timer_QueryInterface,
4525 clock_timer_AddRef,
4526 clock_timer_Release,
4529 static HRESULT WINAPI present_clock_timer_SetTimer(IMFTimer *iface, DWORD flags, LONGLONG time,
4530 IMFAsyncCallback *callback, IUnknown *state, IUnknown **cancel_key)
4532 struct presentation_clock *clock = impl_from_IMFTimer(iface);
4533 struct clock_timer *clock_timer;
4534 HRESULT hr;
4536 TRACE("%p, %#x, %s, %p, %p, %p.\n", iface, flags, debugstr_time(time), callback, state, cancel_key);
4538 if (!(clock_timer = heap_alloc_zero(sizeof(*clock_timer))))
4539 return E_OUTOFMEMORY;
4541 if (FAILED(hr = MFCreateAsyncResult(NULL, NULL, state, &clock_timer->result)))
4543 heap_free(clock_timer);
4544 return hr;
4547 clock_timer->IUnknown_iface.lpVtbl = &clock_timer_vtbl;
4548 clock_timer->refcount = 1;
4549 clock_timer->callback = callback;
4550 IMFAsyncCallback_AddRef(clock_timer->callback);
4552 EnterCriticalSection(&clock->cs);
4554 if (clock->state == MFCLOCK_STATE_RUNNING)
4555 hr = present_clock_schedule_timer(clock, flags, time, clock_timer);
4556 else if (clock->state == MFCLOCK_STATE_STOPPED)
4557 hr = MF_S_CLOCK_STOPPED;
4559 if (SUCCEEDED(hr))
4561 list_add_tail(&clock->timers, &clock_timer->entry);
4562 if (cancel_key)
4564 *cancel_key = &clock_timer->IUnknown_iface;
4565 IUnknown_AddRef(*cancel_key);
4569 LeaveCriticalSection(&clock->cs);
4571 if (FAILED(hr))
4572 IUnknown_Release(&clock_timer->IUnknown_iface);
4574 return hr;
4577 static HRESULT WINAPI present_clock_timer_CancelTimer(IMFTimer *iface, IUnknown *cancel_key)
4579 struct presentation_clock *clock = impl_from_IMFTimer(iface);
4580 struct clock_timer *timer;
4582 TRACE("%p, %p.\n", iface, cancel_key);
4584 EnterCriticalSection(&clock->cs);
4586 LIST_FOR_EACH_ENTRY(timer, &clock->timers, struct clock_timer, entry)
4588 if (&timer->IUnknown_iface == cancel_key)
4590 list_remove(&timer->entry);
4591 if (timer->key)
4593 MFCancelWorkItem(timer->key);
4594 timer->key = 0;
4596 IUnknown_Release(&timer->IUnknown_iface);
4597 break;
4601 LeaveCriticalSection(&clock->cs);
4603 return S_OK;
4606 static const IMFTimerVtbl presentclocktimervtbl =
4608 present_clock_timer_QueryInterface,
4609 present_clock_timer_AddRef,
4610 present_clock_timer_Release,
4611 present_clock_timer_SetTimer,
4612 present_clock_timer_CancelTimer,
4615 static HRESULT WINAPI present_clock_shutdown_QueryInterface(IMFShutdown *iface, REFIID riid, void **out)
4617 struct presentation_clock *clock = impl_from_IMFShutdown(iface);
4618 return IMFPresentationClock_QueryInterface(&clock->IMFPresentationClock_iface, riid, out);
4621 static ULONG WINAPI present_clock_shutdown_AddRef(IMFShutdown *iface)
4623 struct presentation_clock *clock = impl_from_IMFShutdown(iface);
4624 return IMFPresentationClock_AddRef(&clock->IMFPresentationClock_iface);
4627 static ULONG WINAPI present_clock_shutdown_Release(IMFShutdown *iface)
4629 struct presentation_clock *clock = impl_from_IMFShutdown(iface);
4630 return IMFPresentationClock_Release(&clock->IMFPresentationClock_iface);
4633 static HRESULT WINAPI present_clock_shutdown_Shutdown(IMFShutdown *iface)
4635 struct presentation_clock *clock = impl_from_IMFShutdown(iface);
4637 TRACE("%p.\n", iface);
4639 EnterCriticalSection(&clock->cs);
4640 clock->is_shut_down = TRUE;
4641 LeaveCriticalSection(&clock->cs);
4643 return S_OK;
4646 static HRESULT WINAPI present_clock_shutdown_GetShutdownStatus(IMFShutdown *iface, MFSHUTDOWN_STATUS *status)
4648 struct presentation_clock *clock = impl_from_IMFShutdown(iface);
4649 HRESULT hr = S_OK;
4651 TRACE("%p, %p.\n", iface, status);
4653 if (!status)
4654 return E_INVALIDARG;
4656 EnterCriticalSection(&clock->cs);
4657 if (clock->is_shut_down)
4658 *status = MFSHUTDOWN_COMPLETED;
4659 else
4660 hr = MF_E_INVALIDREQUEST;
4661 LeaveCriticalSection(&clock->cs);
4663 return hr;
4666 static const IMFShutdownVtbl presentclockshutdownvtbl =
4668 present_clock_shutdown_QueryInterface,
4669 present_clock_shutdown_AddRef,
4670 present_clock_shutdown_Release,
4671 present_clock_shutdown_Shutdown,
4672 present_clock_shutdown_GetShutdownStatus,
4675 static HRESULT WINAPI present_clock_callback_QueryInterface(IMFAsyncCallback *iface, REFIID riid, void **out)
4677 if (IsEqualIID(riid, &IID_IMFAsyncCallback) ||
4678 IsEqualIID(riid, &IID_IUnknown))
4680 *out = iface;
4681 IMFAsyncCallback_AddRef(iface);
4682 return S_OK;
4685 WARN("Unsupported %s.\n", wine_dbgstr_guid(riid));
4686 *out = NULL;
4687 return E_NOINTERFACE;
4690 static ULONG WINAPI present_clock_sink_callback_AddRef(IMFAsyncCallback *iface)
4692 struct presentation_clock *clock = impl_from_sink_callback_IMFAsyncCallback(iface);
4693 return IMFPresentationClock_AddRef(&clock->IMFPresentationClock_iface);
4696 static ULONG WINAPI present_clock_sink_callback_Release(IMFAsyncCallback *iface)
4698 struct presentation_clock *clock = impl_from_sink_callback_IMFAsyncCallback(iface);
4699 return IMFPresentationClock_Release(&clock->IMFPresentationClock_iface);
4702 static HRESULT WINAPI present_clock_callback_GetParameters(IMFAsyncCallback *iface, DWORD *flags, DWORD *queue)
4704 return E_NOTIMPL;
4707 static HRESULT WINAPI present_clock_sink_callback_Invoke(IMFAsyncCallback *iface, IMFAsyncResult *result)
4709 struct sink_notification *data;
4710 IUnknown *object;
4711 HRESULT hr;
4713 if (FAILED(hr = IMFAsyncResult_GetObject(result, &object)))
4714 return hr;
4716 data = impl_sink_notification_from_IUnknown(object);
4718 clock_call_state_change(data->system_time, data->param, data->notification, data->sink);
4720 IUnknown_Release(object);
4722 return S_OK;
4725 static const IMFAsyncCallbackVtbl presentclocksinkcallbackvtbl =
4727 present_clock_callback_QueryInterface,
4728 present_clock_sink_callback_AddRef,
4729 present_clock_sink_callback_Release,
4730 present_clock_callback_GetParameters,
4731 present_clock_sink_callback_Invoke,
4734 static ULONG WINAPI present_clock_timer_callback_AddRef(IMFAsyncCallback *iface)
4736 struct presentation_clock *clock = impl_from_timer_callback_IMFAsyncCallback(iface);
4737 return IMFPresentationClock_AddRef(&clock->IMFPresentationClock_iface);
4740 static ULONG WINAPI present_clock_timer_callback_Release(IMFAsyncCallback *iface)
4742 struct presentation_clock *clock = impl_from_timer_callback_IMFAsyncCallback(iface);
4743 return IMFPresentationClock_Release(&clock->IMFPresentationClock_iface);
4746 static HRESULT WINAPI present_clock_timer_callback_Invoke(IMFAsyncCallback *iface, IMFAsyncResult *result)
4748 struct presentation_clock *clock = impl_from_timer_callback_IMFAsyncCallback(iface);
4749 struct clock_timer *timer;
4750 IUnknown *object;
4751 HRESULT hr;
4753 if (FAILED(hr = IMFAsyncResult_GetObject(result, &object)))
4754 return hr;
4756 timer = impl_clock_timer_from_IUnknown(object);
4758 EnterCriticalSection(&clock->cs);
4759 list_remove(&timer->entry);
4760 IUnknown_Release(&timer->IUnknown_iface);
4761 LeaveCriticalSection(&clock->cs);
4763 IMFAsyncCallback_Invoke(timer->callback, timer->result);
4765 IUnknown_Release(object);
4767 return S_OK;
4770 static const IMFAsyncCallbackVtbl presentclocktimercallbackvtbl =
4772 present_clock_callback_QueryInterface,
4773 present_clock_timer_callback_AddRef,
4774 present_clock_timer_callback_Release,
4775 present_clock_callback_GetParameters,
4776 present_clock_timer_callback_Invoke,
4779 /***********************************************************************
4780 * MFCreatePresentationClock (mf.@)
4782 HRESULT WINAPI MFCreatePresentationClock(IMFPresentationClock **clock)
4784 struct presentation_clock *object;
4786 TRACE("%p.\n", clock);
4788 object = heap_alloc_zero(sizeof(*object));
4789 if (!object)
4790 return E_OUTOFMEMORY;
4792 object->IMFPresentationClock_iface.lpVtbl = &presentationclockvtbl;
4793 object->IMFRateControl_iface.lpVtbl = &presentclockratecontrolvtbl;
4794 object->IMFTimer_iface.lpVtbl = &presentclocktimervtbl;
4795 object->IMFShutdown_iface.lpVtbl = &presentclockshutdownvtbl;
4796 object->sink_callback.lpVtbl = &presentclocksinkcallbackvtbl;
4797 object->timer_callback.lpVtbl = &presentclocktimercallbackvtbl;
4798 object->refcount = 1;
4799 list_init(&object->sinks);
4800 list_init(&object->timers);
4801 object->rate = 1.0f;
4802 InitializeCriticalSection(&object->cs);
4804 *clock = &object->IMFPresentationClock_iface;
4806 return S_OK;
4809 static HRESULT WINAPI standard_quality_manager_QueryInterface(IMFQualityManager *iface, REFIID riid, void **out)
4811 struct quality_manager *manager = impl_from_IMFQualityManager(iface);
4813 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
4815 if (IsEqualIID(riid, &IID_IMFQualityManager) ||
4816 IsEqualIID(riid, &IID_IUnknown))
4818 *out = iface;
4820 else if (IsEqualIID(riid, &IID_IMFClockStateSink))
4822 *out = &manager->IMFClockStateSink_iface;
4824 else
4826 WARN("Unsupported %s.\n", debugstr_guid(riid));
4827 *out = NULL;
4828 return E_NOINTERFACE;
4831 IUnknown_AddRef((IUnknown *)*out);
4832 return S_OK;
4835 static ULONG WINAPI standard_quality_manager_AddRef(IMFQualityManager *iface)
4837 struct quality_manager *manager = impl_from_IMFQualityManager(iface);
4838 ULONG refcount = InterlockedIncrement(&manager->refcount);
4840 TRACE("%p, refcount %u.\n", iface, refcount);
4842 return refcount;
4845 static ULONG WINAPI standard_quality_manager_Release(IMFQualityManager *iface)
4847 struct quality_manager *manager = impl_from_IMFQualityManager(iface);
4848 ULONG refcount = InterlockedDecrement(&manager->refcount);
4850 TRACE("%p, refcount %u.\n", iface, refcount);
4852 if (!refcount)
4854 if (manager->clock)
4855 IMFPresentationClock_Release(manager->clock);
4856 if (manager->topology)
4857 IMFTopology_Release(manager->topology);
4858 DeleteCriticalSection(&manager->cs);
4859 heap_free(manager);
4862 return refcount;
4865 static void standard_quality_manager_set_topology(struct quality_manager *manager, IMFTopology *topology)
4867 if (manager->topology)
4868 IMFTopology_Release(manager->topology);
4869 manager->topology = topology;
4870 if (manager->topology)
4871 IMFTopology_AddRef(manager->topology);
4874 static HRESULT WINAPI standard_quality_manager_NotifyTopology(IMFQualityManager *iface, IMFTopology *topology)
4876 struct quality_manager *manager = impl_from_IMFQualityManager(iface);
4877 HRESULT hr = S_OK;
4879 TRACE("%p, %p.\n", iface, topology);
4881 EnterCriticalSection(&manager->cs);
4882 if (manager->state == QUALITY_MANAGER_SHUT_DOWN)
4883 hr = MF_E_SHUTDOWN;
4884 else
4886 standard_quality_manager_set_topology(manager, topology);
4888 LeaveCriticalSection(&manager->cs);
4890 return hr;
4893 static void standard_quality_manager_release_clock(struct quality_manager *manager)
4895 if (manager->clock)
4897 IMFPresentationClock_RemoveClockStateSink(manager->clock, &manager->IMFClockStateSink_iface);
4898 IMFPresentationClock_Release(manager->clock);
4900 manager->clock = NULL;
4903 static HRESULT WINAPI standard_quality_manager_NotifyPresentationClock(IMFQualityManager *iface,
4904 IMFPresentationClock *clock)
4906 struct quality_manager *manager = impl_from_IMFQualityManager(iface);
4907 HRESULT hr = S_OK;
4909 TRACE("%p, %p.\n", iface, clock);
4911 EnterCriticalSection(&manager->cs);
4912 if (manager->state == QUALITY_MANAGER_SHUT_DOWN)
4913 hr = MF_E_SHUTDOWN;
4914 else if (!clock)
4915 hr = E_POINTER;
4916 else
4918 standard_quality_manager_release_clock(manager);
4919 manager->clock = clock;
4920 IMFPresentationClock_AddRef(manager->clock);
4921 if (FAILED(IMFPresentationClock_AddClockStateSink(manager->clock, &manager->IMFClockStateSink_iface)))
4922 WARN("Failed to set state sink.\n");
4924 LeaveCriticalSection(&manager->cs);
4926 return hr;
4929 static HRESULT WINAPI standard_quality_manager_NotifyProcessInput(IMFQualityManager *iface, IMFTopologyNode *node,
4930 LONG input_index, IMFSample *sample)
4932 TRACE("%p, %p, %d, %p stub.\n", iface, node, input_index, sample);
4934 return E_NOTIMPL;
4937 static HRESULT WINAPI standard_quality_manager_NotifyProcessOutput(IMFQualityManager *iface, IMFTopologyNode *node,
4938 LONG output_index, IMFSample *sample)
4940 TRACE("%p, %p, %d, %p stub.\n", iface, node, output_index, sample);
4942 return E_NOTIMPL;
4945 static HRESULT WINAPI standard_quality_manager_NotifyQualityEvent(IMFQualityManager *iface, IUnknown *object,
4946 IMFMediaEvent *event)
4948 FIXME("%p, %p, %p stub.\n", iface, object, event);
4950 return E_NOTIMPL;
4953 static HRESULT WINAPI standard_quality_manager_Shutdown(IMFQualityManager *iface)
4955 struct quality_manager *manager = impl_from_IMFQualityManager(iface);
4957 TRACE("%p.\n", iface);
4959 EnterCriticalSection(&manager->cs);
4960 if (manager->state != QUALITY_MANAGER_SHUT_DOWN)
4962 standard_quality_manager_release_clock(manager);
4963 standard_quality_manager_set_topology(manager, NULL);
4964 manager->state = QUALITY_MANAGER_SHUT_DOWN;
4966 LeaveCriticalSection(&manager->cs);
4968 return S_OK;
4971 static const IMFQualityManagerVtbl standard_quality_manager_vtbl =
4973 standard_quality_manager_QueryInterface,
4974 standard_quality_manager_AddRef,
4975 standard_quality_manager_Release,
4976 standard_quality_manager_NotifyTopology,
4977 standard_quality_manager_NotifyPresentationClock,
4978 standard_quality_manager_NotifyProcessInput,
4979 standard_quality_manager_NotifyProcessOutput,
4980 standard_quality_manager_NotifyQualityEvent,
4981 standard_quality_manager_Shutdown,
4984 static HRESULT WINAPI standard_quality_manager_sink_QueryInterface(IMFClockStateSink *iface,
4985 REFIID riid, void **obj)
4987 struct quality_manager *manager = impl_from_qm_IMFClockStateSink(iface);
4988 return IMFQualityManager_QueryInterface(&manager->IMFQualityManager_iface, riid, obj);
4991 static ULONG WINAPI standard_quality_manager_sink_AddRef(IMFClockStateSink *iface)
4993 struct quality_manager *manager = impl_from_qm_IMFClockStateSink(iface);
4994 return IMFQualityManager_AddRef(&manager->IMFQualityManager_iface);
4997 static ULONG WINAPI standard_quality_manager_sink_Release(IMFClockStateSink *iface)
4999 struct quality_manager *manager = impl_from_qm_IMFClockStateSink(iface);
5000 return IMFQualityManager_Release(&manager->IMFQualityManager_iface);
5003 static HRESULT WINAPI standard_quality_manager_sink_OnClockStart(IMFClockStateSink *iface,
5004 MFTIME systime, LONGLONG offset)
5006 return S_OK;
5009 static HRESULT WINAPI standard_quality_manager_sink_OnClockStop(IMFClockStateSink *iface,
5010 MFTIME systime)
5012 return S_OK;
5015 static HRESULT WINAPI standard_quality_manager_sink_OnClockPause(IMFClockStateSink *iface,
5016 MFTIME systime)
5018 return S_OK;
5021 static HRESULT WINAPI standard_quality_manager_sink_OnClockRestart(IMFClockStateSink *iface,
5022 MFTIME systime)
5024 return S_OK;
5027 static HRESULT WINAPI standard_quality_manager_sink_OnClockSetRate(IMFClockStateSink *iface,
5028 MFTIME systime, float rate)
5030 return S_OK;
5033 static const IMFClockStateSinkVtbl standard_quality_manager_sink_vtbl =
5035 standard_quality_manager_sink_QueryInterface,
5036 standard_quality_manager_sink_AddRef,
5037 standard_quality_manager_sink_Release,
5038 standard_quality_manager_sink_OnClockStart,
5039 standard_quality_manager_sink_OnClockStop,
5040 standard_quality_manager_sink_OnClockPause,
5041 standard_quality_manager_sink_OnClockRestart,
5042 standard_quality_manager_sink_OnClockSetRate,
5045 HRESULT WINAPI MFCreateStandardQualityManager(IMFQualityManager **manager)
5047 struct quality_manager *object;
5049 TRACE("%p.\n", manager);
5051 object = heap_alloc_zero(sizeof(*object));
5052 if (!object)
5053 return E_OUTOFMEMORY;
5055 object->IMFQualityManager_iface.lpVtbl = &standard_quality_manager_vtbl;
5056 object->IMFClockStateSink_iface.lpVtbl = &standard_quality_manager_sink_vtbl;
5057 object->refcount = 1;
5058 InitializeCriticalSection(&object->cs);
5060 *manager = &object->IMFQualityManager_iface;
5062 return S_OK;