mf/session: Do not drop pending commands when clearing current presentation.
[wine.git] / dlls / mf / session.c
blob41caf02d0592c187ba3df02290fedb28a05bf4bd
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_command_list(struct media_session *session)
860 struct session_op *op, *op2;
862 LIST_FOR_EACH_ENTRY_SAFE(op, op2, &session->commands, struct session_op, entry)
864 list_remove(&op->entry);
865 IUnknown_Release(&op->IUnknown_iface);
869 static void session_clear_presentation(struct media_session *session)
871 struct media_source *source, *source2;
872 struct media_sink *sink, *sink2;
873 struct topo_node *node, *node2;
875 session_shutdown_current_topology(session);
877 IMFTopology_Clear(session->presentation.current_topology);
878 session->presentation.topo_status = MF_TOPOSTATUS_INVALID;
880 LIST_FOR_EACH_ENTRY_SAFE(source, source2, &session->presentation.sources, struct media_source, entry)
882 list_remove(&source->entry);
883 if (source->source)
884 IMFMediaSource_Release(source->source);
885 if (source->pd)
886 IMFPresentationDescriptor_Release(source->pd);
887 heap_free(source);
890 LIST_FOR_EACH_ENTRY_SAFE(node, node2, &session->presentation.nodes, struct topo_node, entry)
892 list_remove(&node->entry);
893 release_topo_node(node);
896 LIST_FOR_EACH_ENTRY_SAFE(sink, sink2, &session->presentation.sinks, struct media_sink, entry)
898 list_remove(&sink->entry);
900 if (sink->sink)
901 IMFMediaSink_Release(sink->sink);
902 if (sink->preroll)
903 IMFMediaSinkPreroll_Release(sink->preroll);
904 if (sink->event_generator)
905 IMFMediaEventGenerator_Release(sink->event_generator);
906 heap_free(sink);
910 static struct topo_node *session_get_node_by_id(const struct media_session *session, TOPOID id)
912 struct topo_node *node;
914 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
916 if (node->node_id == id)
917 return node;
920 return NULL;
923 static void session_start(struct media_session *session, const GUID *time_format, const PROPVARIANT *start_position)
925 struct media_source *source;
926 HRESULT hr;
928 switch (session->state)
930 case SESSION_STATE_STOPPED:
931 case SESSION_STATE_PAUSED:
933 session->presentation.time_format = *time_format;
934 session->presentation.start_position.vt = VT_EMPTY;
935 PropVariantCopy(&session->presentation.start_position, start_position);
937 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
939 if (!(session->presentation.flags & SESSION_FLAG_SOURCES_SUBSCRIBED))
941 if (FAILED(hr = IMFMediaSource_BeginGetEvent(source->source, &session->events_callback,
942 (IUnknown *)source->source)))
944 WARN("Failed to subscribe to source events, hr %#x.\n", hr);
948 if (FAILED(hr = IMFMediaSource_Start(source->source, source->pd, &GUID_NULL, start_position)))
949 WARN("Failed to start media source %p, hr %#x.\n", source->source, hr);
952 session->presentation.flags |= SESSION_FLAG_SOURCES_SUBSCRIBED;
953 session->state = SESSION_STATE_STARTING_SOURCES;
954 break;
955 case SESSION_STATE_STARTED:
956 FIXME("Seeking is not implemented.\n");
957 break;
958 case SESSION_STATE_CLOSED:
959 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MESessionStarted, &GUID_NULL,
960 MF_E_INVALIDREQUEST, NULL);
961 break;
962 default:
967 static void session_command_complete(struct media_session *session)
969 struct session_op *op;
970 struct list *e;
972 /* Pop current command, submit next. */
973 if ((e = list_head(&session->commands)))
975 op = LIST_ENTRY(e, struct session_op, entry);
976 list_remove(&op->entry);
977 IUnknown_Release(&op->IUnknown_iface);
980 if ((e = list_head(&session->commands)))
982 op = LIST_ENTRY(e, struct session_op, entry);
983 MFPutWorkItem(MFASYNC_CALLBACK_QUEUE_STANDARD, &session->commands_callback, &op->IUnknown_iface);
987 static void session_set_started(struct media_session *session)
989 struct media_source *source;
990 unsigned int caps, flags;
991 IMFMediaEvent *event;
993 session->state = SESSION_STATE_STARTED;
995 caps = session->caps | MFSESSIONCAP_PAUSE;
997 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
999 if (SUCCEEDED(IMFMediaSource_GetCharacteristics(source->source, &flags)))
1001 if (!(flags & MFMEDIASOURCE_CAN_PAUSE))
1003 caps &= ~MFSESSIONCAP_PAUSE;
1004 break;
1009 session_set_caps(session, caps);
1011 if (SUCCEEDED(MFCreateMediaEvent(MESessionStarted, &GUID_NULL, S_OK, NULL, &event)))
1013 IMFMediaEvent_SetUINT64(event, &MF_EVENT_PRESENTATION_TIME_OFFSET, 0);
1014 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
1015 IMFMediaEvent_Release(event);
1017 session_command_complete(session);
1020 static void session_set_paused(struct media_session *session, HRESULT status)
1022 session->state = SESSION_STATE_PAUSED;
1023 if (SUCCEEDED(status))
1024 session_set_caps(session, session->caps & ~MFSESSIONCAP_PAUSE);
1025 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MESessionPaused, &GUID_NULL, status, NULL);
1026 session_command_complete(session);
1029 static void session_set_closed(struct media_session *session, HRESULT status)
1031 session->state = SESSION_STATE_CLOSED;
1032 if (SUCCEEDED(status))
1033 session_set_caps(session, session->caps & ~(MFSESSIONCAP_START | MFSESSIONCAP_SEEK));
1034 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MESessionClosed, &GUID_NULL, status, NULL);
1035 session_command_complete(session);
1038 static void session_pause(struct media_session *session)
1040 HRESULT hr;
1042 switch (session->state)
1044 case SESSION_STATE_STARTED:
1046 /* Transition in two steps - pause the clock, wait for sinks, then pause sources. */
1047 if (SUCCEEDED(hr = IMFPresentationClock_Pause(session->clock)))
1048 session->state = SESSION_STATE_PAUSING_SINKS;
1050 break;
1051 default:
1052 hr = MF_E_INVALIDREQUEST;
1055 if (FAILED(hr))
1056 session_set_paused(session, hr);
1059 static void session_set_stopped(struct media_session *session, HRESULT status)
1061 MediaEventType event_type;
1062 IMFMediaEvent *event;
1064 session->state = SESSION_STATE_STOPPED;
1065 event_type = session->presentation.flags & SESSION_FLAG_END_OF_PRESENTATION ? MESessionEnded : MESessionStopped;
1067 if (SUCCEEDED(MFCreateMediaEvent(event_type, &GUID_NULL, status, NULL, &event)))
1069 IMFMediaEvent_SetUINT64(event, &MF_SESSION_APPROX_EVENT_OCCURRENCE_TIME, session->presentation.clock_stop_time);
1070 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
1071 IMFMediaEvent_Release(event);
1073 session_command_complete(session);
1076 static void session_stop(struct media_session *session)
1078 HRESULT hr = MF_E_INVALIDREQUEST;
1080 switch (session->state)
1082 case SESSION_STATE_STARTED:
1083 case SESSION_STATE_PAUSED:
1085 /* Transition in two steps - stop the clock, wait for sinks, then stop sources. */
1086 IMFPresentationClock_GetTime(session->clock, &session->presentation.clock_stop_time);
1087 if (SUCCEEDED(hr = IMFPresentationClock_Stop(session->clock)))
1088 session->state = SESSION_STATE_STOPPING_SINKS;
1089 else
1090 session_set_stopped(session, hr);
1092 break;
1093 case SESSION_STATE_STOPPED:
1094 hr = S_OK;
1095 /* fallthrough */
1096 default:
1097 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MESessionStopped, &GUID_NULL, hr, NULL);
1098 session_command_complete(session);
1099 break;
1103 static HRESULT session_finalize_sinks(struct media_session *session)
1105 IMFFinalizableMediaSink *fin_sink;
1106 BOOL sinks_finalized = TRUE;
1107 struct media_sink *sink;
1108 HRESULT hr = S_OK;
1110 session->presentation.flags &= ~SESSION_FLAG_FINALIZE_SINKS;
1111 session->state = SESSION_STATE_FINALIZING_SINKS;
1113 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
1115 if (SUCCEEDED(IMFMediaSink_QueryInterface(sink->sink, &IID_IMFFinalizableMediaSink, (void **)&fin_sink)))
1117 hr = IMFFinalizableMediaSink_BeginFinalize(fin_sink, &session->sink_finalizer_callback,
1118 (IUnknown *)fin_sink);
1119 IMFFinalizableMediaSink_Release(fin_sink);
1120 if (FAILED(hr))
1121 break;
1122 sinks_finalized = FALSE;
1124 else
1125 sink->finalized = TRUE;
1128 if (sinks_finalized)
1129 session_set_closed(session, hr);
1131 return hr;
1134 static void session_close(struct media_session *session)
1136 HRESULT hr = S_OK;
1138 switch (session->state)
1140 case SESSION_STATE_STOPPED:
1141 hr = session_finalize_sinks(session);
1142 break;
1143 case SESSION_STATE_STARTED:
1144 case SESSION_STATE_PAUSED:
1145 session->presentation.flags |= SESSION_FLAG_FINALIZE_SINKS;
1146 if (SUCCEEDED(hr = IMFPresentationClock_Stop(session->clock)))
1147 session->state = SESSION_STATE_STOPPING_SINKS;
1148 break;
1149 default:
1150 hr = MF_E_INVALIDREQUEST;
1151 break;
1154 if (FAILED(hr))
1155 session_set_closed(session, hr);
1158 static struct media_source *session_get_media_source(struct media_session *session, IMFMediaSource *source)
1160 struct media_source *cur;
1162 LIST_FOR_EACH_ENTRY(cur, &session->presentation.sources, struct media_source, entry)
1164 if (source == cur->source)
1165 return cur;
1168 return NULL;
1171 static void session_release_media_source(struct media_source *source)
1173 IMFMediaSource_Release(source->source);
1174 if (source->pd)
1175 IMFPresentationDescriptor_Release(source->pd);
1176 heap_free(source);
1179 static HRESULT session_add_media_source(struct media_session *session, IMFTopologyNode *node, IMFMediaSource *source)
1181 struct media_source *media_source;
1182 HRESULT hr;
1184 if (session_get_media_source(session, source))
1185 return S_FALSE;
1187 if (!(media_source = heap_alloc_zero(sizeof(*media_source))))
1188 return E_OUTOFMEMORY;
1190 media_source->source = source;
1191 IMFMediaSource_AddRef(media_source->source);
1193 hr = IMFTopologyNode_GetUnknown(node, &MF_TOPONODE_PRESENTATION_DESCRIPTOR, &IID_IMFPresentationDescriptor,
1194 (void **)&media_source->pd);
1196 if (SUCCEEDED(hr))
1197 list_add_tail(&session->presentation.sources, &media_source->entry);
1198 else
1199 session_release_media_source(media_source);
1201 return hr;
1204 static void session_raise_topology_set(struct media_session *session, IMFTopology *topology, HRESULT status)
1206 PROPVARIANT param;
1208 param.vt = topology ? VT_UNKNOWN : VT_EMPTY;
1209 param.punkVal = (IUnknown *)topology;
1211 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MESessionTopologySet, &GUID_NULL, status, &param);
1214 static DWORD session_get_object_rate_caps(IUnknown *object)
1216 IMFRateSupport *rate_support;
1217 DWORD caps = 0;
1218 float rate;
1220 if (SUCCEEDED(MFGetService(object, &MF_RATE_CONTROL_SERVICE, &IID_IMFRateSupport, (void **)&rate_support)))
1222 rate = 0.0f;
1223 if (SUCCEEDED(IMFRateSupport_GetFastestRate(rate_support, MFRATE_FORWARD, TRUE, &rate)) && rate != 0.0f)
1224 caps |= MFSESSIONCAP_RATE_FORWARD;
1226 rate = 0.0f;
1227 if (SUCCEEDED(IMFRateSupport_GetFastestRate(rate_support, MFRATE_REVERSE, TRUE, &rate)) && rate != 0.0f)
1228 caps |= MFSESSIONCAP_RATE_REVERSE;
1230 IMFRateSupport_Release(rate_support);
1233 return caps;
1236 static HRESULT session_add_media_sink(struct media_session *session, IMFTopologyNode *node, IMFMediaSink *sink)
1238 struct media_sink *media_sink;
1239 unsigned int disable_preroll = 0;
1240 DWORD flags;
1242 LIST_FOR_EACH_ENTRY(media_sink, &session->presentation.sinks, struct media_sink, entry)
1244 if (sink == media_sink->sink)
1245 return S_FALSE;
1248 if (!(media_sink = heap_alloc_zero(sizeof(*media_sink))))
1249 return E_OUTOFMEMORY;
1251 media_sink->sink = sink;
1252 IMFMediaSink_AddRef(media_sink->sink);
1254 IMFMediaSink_QueryInterface(media_sink->sink, &IID_IMFMediaEventGenerator, (void **)&media_sink->event_generator);
1256 IMFTopologyNode_GetUINT32(node, &MF_TOPONODE_DISABLE_PREROLL, &disable_preroll);
1257 if (SUCCEEDED(IMFMediaSink_GetCharacteristics(sink, &flags)) && flags & MEDIASINK_CAN_PREROLL && !disable_preroll)
1259 if (SUCCEEDED(IMFMediaSink_QueryInterface(media_sink->sink, &IID_IMFMediaSinkPreroll, (void **)&media_sink->preroll)))
1260 session->presentation.flags |= SESSION_FLAG_NEEDS_PREROLL;
1263 list_add_tail(&session->presentation.sinks, &media_sink->entry);
1265 return S_OK;
1268 static unsigned int transform_node_get_stream_id(struct topo_node *node, BOOL output, unsigned int index)
1270 unsigned int *map = output ? node->u.transform.output_map : node->u.transform.input_map;
1271 return map ? map[index] : index;
1274 static HRESULT session_set_transform_stream_info(struct topo_node *node)
1276 unsigned int *input_map = NULL, *output_map = NULL;
1277 unsigned int i, input_count, output_count, block_alignment;
1278 struct transform_stream *streams;
1279 IMFMediaType *media_type;
1280 GUID major = { 0 };
1281 HRESULT hr;
1283 hr = IMFTransform_GetStreamCount(node->object.transform, &input_count, &output_count);
1284 if (SUCCEEDED(hr) && (input_count > 1 || output_count > 1))
1286 input_map = heap_calloc(input_count, sizeof(*input_map));
1287 output_map = heap_calloc(output_count, sizeof(*output_map));
1288 if (FAILED(IMFTransform_GetStreamIDs(node->object.transform, input_count, input_map,
1289 output_count, output_map)))
1291 /* Assume sequential identifiers. */
1292 heap_free(input_map);
1293 heap_free(output_map);
1294 input_map = output_map = NULL;
1298 if (SUCCEEDED(hr))
1300 node->u.transform.input_map = input_map;
1301 node->u.transform.output_map = output_map;
1303 streams = heap_calloc(input_count, sizeof(*streams));
1304 for (i = 0; i < input_count; ++i)
1305 list_init(&streams[i].samples);
1306 node->u.transform.inputs = streams;
1307 node->u.transform.input_count = input_count;
1309 streams = heap_calloc(output_count, sizeof(*streams));
1310 for (i = 0; i < output_count; ++i)
1312 list_init(&streams[i].samples);
1314 if (SUCCEEDED(IMFTransform_GetOutputCurrentType(node->object.transform,
1315 transform_node_get_stream_id(node, TRUE, i), &media_type)))
1317 if (SUCCEEDED(IMFMediaType_GetMajorType(media_type, &major)) && IsEqualGUID(&major, &MFMediaType_Audio)
1318 && SUCCEEDED(IMFMediaType_GetUINT32(media_type, &MF_MT_AUDIO_BLOCK_ALIGNMENT, &block_alignment)))
1320 streams[i].min_buffer_size = block_alignment;
1322 IMFMediaType_Release(media_type);
1325 node->u.transform.outputs = streams;
1326 node->u.transform.output_count = output_count;
1329 return hr;
1332 static HRESULT session_get_stream_sink_type(IMFStreamSink *sink, IMFMediaType **media_type)
1334 IMFMediaTypeHandler *handler;
1335 HRESULT hr;
1337 if (SUCCEEDED(hr = IMFStreamSink_GetMediaTypeHandler(sink, &handler)))
1339 hr = IMFMediaTypeHandler_GetCurrentMediaType(handler, media_type);
1340 IMFMediaTypeHandler_Release(handler);
1343 return hr;
1346 static HRESULT WINAPI node_sample_allocator_cb_QueryInterface(IMFVideoSampleAllocatorNotify *iface,
1347 REFIID riid, void **obj)
1349 if (IsEqualIID(riid, &IID_IMFVideoSampleAllocatorNotify) ||
1350 IsEqualIID(riid, &IID_IUnknown))
1352 *obj = iface;
1353 IMFVideoSampleAllocatorNotify_AddRef(iface);
1354 return S_OK;
1357 *obj = NULL;
1358 return E_NOINTERFACE;
1361 static ULONG WINAPI node_sample_allocator_cb_AddRef(IMFVideoSampleAllocatorNotify *iface)
1363 return 2;
1366 static ULONG WINAPI node_sample_allocator_cb_Release(IMFVideoSampleAllocatorNotify *iface)
1368 return 1;
1371 static HRESULT session_request_sample_from_node(struct media_session *session, IMFTopologyNode *node, DWORD output);
1373 static HRESULT WINAPI node_sample_allocator_cb_NotifyRelease(IMFVideoSampleAllocatorNotify *iface)
1375 struct topo_node *topo_node = impl_node_from_IMFVideoSampleAllocatorNotify(iface);
1376 struct session_op *op;
1378 if (SUCCEEDED(create_session_op(SESSION_CMD_SA_READY, &op)))
1380 op->u.sa_ready.node_id = topo_node->node_id;
1381 MFPutWorkItem(MFASYNC_CALLBACK_QUEUE_STANDARD, &topo_node->session->commands_callback, &op->IUnknown_iface);
1382 IUnknown_Release(&op->IUnknown_iface);
1385 return S_OK;
1388 static const IMFVideoSampleAllocatorNotifyVtbl node_sample_allocator_cb_vtbl =
1390 node_sample_allocator_cb_QueryInterface,
1391 node_sample_allocator_cb_AddRef,
1392 node_sample_allocator_cb_Release,
1393 node_sample_allocator_cb_NotifyRelease,
1396 static HRESULT session_append_node(struct media_session *session, IMFTopologyNode *node)
1398 struct topo_node *topo_node;
1399 IMFMediaSink *media_sink;
1400 IMFMediaType *media_type;
1401 IMFStreamDescriptor *sd;
1402 HRESULT hr = S_OK;
1404 if (!(topo_node = heap_alloc_zero(sizeof(*topo_node))))
1405 return E_OUTOFMEMORY;
1407 IMFTopologyNode_GetNodeType(node, &topo_node->type);
1408 IMFTopologyNode_GetTopoNodeID(node, &topo_node->node_id);
1409 topo_node->node = node;
1410 IMFTopologyNode_AddRef(topo_node->node);
1411 topo_node->session = session;
1413 switch (topo_node->type)
1415 case MF_TOPOLOGY_OUTPUT_NODE:
1416 topo_node->u.sink.notify_cb.lpVtbl = &node_sample_allocator_cb_vtbl;
1418 if (FAILED(hr = topology_node_get_object(node, &IID_IMFStreamSink, (void **)&topo_node->object.object)))
1420 WARN("Failed to get stream sink interface, hr %#x.\n", hr);
1421 break;
1424 if (FAILED(hr = IMFStreamSink_GetMediaSink(topo_node->object.sink_stream, &media_sink)))
1425 break;
1427 if (SUCCEEDED(hr = session_add_media_sink(session, node, media_sink)))
1429 if (SUCCEEDED(session_get_stream_sink_type(topo_node->object.sink_stream, &media_type)))
1431 if (SUCCEEDED(MFGetService(topo_node->object.object, &MR_VIDEO_ACCELERATION_SERVICE,
1432 &IID_IMFVideoSampleAllocator, (void **)&topo_node->u.sink.allocator)))
1434 if (FAILED(hr = IMFVideoSampleAllocator_InitializeSampleAllocator(topo_node->u.sink.allocator,
1435 2, media_type)))
1437 WARN("Failed to initialize sample allocator for the stream, hr %#x.\n", hr);
1439 IMFVideoSampleAllocator_QueryInterface(topo_node->u.sink.allocator,
1440 &IID_IMFVideoSampleAllocatorCallback, (void **)&topo_node->u.sink.allocator_cb);
1441 IMFVideoSampleAllocatorCallback_SetCallback(topo_node->u.sink.allocator_cb,
1442 &topo_node->u.sink.notify_cb);
1444 IMFMediaType_Release(media_type);
1447 IMFMediaSink_Release(media_sink);
1449 break;
1450 case MF_TOPOLOGY_SOURCESTREAM_NODE:
1451 if (FAILED(IMFTopologyNode_GetUnknown(node, &MF_TOPONODE_SOURCE, &IID_IMFMediaSource,
1452 (void **)&topo_node->u.source.source)))
1454 WARN("Missing MF_TOPONODE_SOURCE, hr %#x.\n", hr);
1455 break;
1458 if (FAILED(hr = session_add_media_source(session, node, topo_node->u.source.source)))
1459 break;
1461 if (FAILED(hr = IMFTopologyNode_GetUnknown(node, &MF_TOPONODE_STREAM_DESCRIPTOR,
1462 &IID_IMFStreamDescriptor, (void **)&sd)))
1464 WARN("Missing MF_TOPONODE_STREAM_DESCRIPTOR, hr %#x.\n", hr);
1465 break;
1468 hr = IMFStreamDescriptor_GetStreamIdentifier(sd, &topo_node->u.source.stream_id);
1469 IMFStreamDescriptor_Release(sd);
1471 break;
1472 case MF_TOPOLOGY_TRANSFORM_NODE:
1474 if (SUCCEEDED(hr = topology_node_get_object(node, &IID_IMFTransform, (void **)&topo_node->object.transform)))
1476 hr = session_set_transform_stream_info(topo_node);
1478 else
1479 WARN("Failed to get IMFTransform for MFT node, hr %#x.\n", hr);
1481 break;
1482 case MF_TOPOLOGY_TEE_NODE:
1483 FIXME("Unsupported node type %d.\n", topo_node->type);
1485 break;
1486 default:
1490 if (SUCCEEDED(hr))
1491 list_add_tail(&session->presentation.nodes, &topo_node->entry);
1492 else
1493 release_topo_node(topo_node);
1495 return hr;
1498 static HRESULT session_collect_nodes(struct media_session *session)
1500 IMFTopology *topology = session->presentation.current_topology;
1501 IMFTopologyNode *node;
1502 WORD i, count = 0;
1503 HRESULT hr;
1505 if (!list_empty(&session->presentation.nodes))
1506 return S_OK;
1508 if (FAILED(hr = IMFTopology_GetNodeCount(topology, &count)))
1509 return hr;
1511 for (i = 0; i < count; ++i)
1513 if (FAILED(hr = IMFTopology_GetNode(topology, i, &node)))
1515 WARN("Failed to get node %u.\n", i);
1516 break;
1519 hr = session_append_node(session, node);
1520 IMFTopologyNode_Release(node);
1521 if (FAILED(hr))
1523 WARN("Failed to add node %u.\n", i);
1524 break;
1528 return hr;
1531 static HRESULT session_set_current_topology(struct media_session *session, IMFTopology *topology)
1533 struct media_source *source;
1534 DWORD caps, object_flags;
1535 struct media_sink *sink;
1536 struct topo_node *node;
1537 struct session_op *op;
1538 IMFMediaEvent *event;
1539 HRESULT hr;
1541 if (session->quality_manager)
1543 if (SUCCEEDED(create_session_op(SESSION_CMD_QM_NOTIFY_TOPOLOGY, &op)))
1545 op->u.notify_topology.topology = topology;
1546 IMFTopology_AddRef(op->u.notify_topology.topology);
1547 session_submit_command(session, op);
1548 IUnknown_Release(&op->IUnknown_iface);
1552 if (FAILED(hr = IMFTopology_CloneFrom(session->presentation.current_topology, topology)))
1554 WARN("Failed to clone topology, hr %#x.\n", hr);
1555 return hr;
1558 session_collect_nodes(session);
1560 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
1562 if (node->type == MF_TOPOLOGY_TRANSFORM_NODE)
1564 if (FAILED(hr = IMFTransform_ProcessMessage(node->object.transform,
1565 MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0)))
1566 return hr;
1570 /* FIXME: attributes are all zero for now */
1571 if (SUCCEEDED(MFCreateMediaEvent(MESessionNotifyPresentationTime, &GUID_NULL, S_OK, NULL, &event)))
1573 IMFMediaEvent_SetUINT64(event, &MF_EVENT_START_PRESENTATION_TIME, 0);
1574 IMFMediaEvent_SetUINT64(event, &MF_EVENT_PRESENTATION_TIME_OFFSET, 0);
1575 IMFMediaEvent_SetUINT64(event, &MF_EVENT_START_PRESENTATION_TIME_AT_OUTPUT, 0);
1577 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
1578 IMFMediaEvent_Release(event);
1581 /* Update session caps. */
1582 caps = MFSESSIONCAP_START | MFSESSIONCAP_SEEK | MFSESSIONCAP_RATE_FORWARD | MFSESSIONCAP_RATE_REVERSE |
1583 MFSESSIONCAP_DOES_NOT_USE_NETWORK;
1585 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
1587 if (!caps)
1588 break;
1590 object_flags = 0;
1591 if (SUCCEEDED(IMFMediaSource_GetCharacteristics(source->source, &object_flags)))
1593 if (!(object_flags & MFMEDIASOURCE_DOES_NOT_USE_NETWORK))
1594 caps &= ~MFSESSIONCAP_DOES_NOT_USE_NETWORK;
1595 if (!(object_flags & MFMEDIASOURCE_CAN_SEEK))
1596 caps &= ~MFSESSIONCAP_SEEK;
1599 /* Mask unsupported rate caps. */
1601 caps &= session_get_object_rate_caps((IUnknown *)source->source)
1602 | ~(MFSESSIONCAP_RATE_FORWARD | MFSESSIONCAP_RATE_REVERSE);
1605 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
1607 if (!caps)
1608 break;
1610 object_flags = 0;
1611 if (SUCCEEDED(IMFMediaSink_GetCharacteristics(sink->sink, &object_flags)))
1613 if (!(object_flags & MEDIASINK_RATELESS))
1614 caps &= session_get_object_rate_caps((IUnknown *)sink->sink)
1615 | ~(MFSESSIONCAP_RATE_FORWARD | MFSESSIONCAP_RATE_REVERSE);
1619 session_set_caps(session, caps);
1621 return S_OK;
1624 static void session_set_topology(struct media_session *session, DWORD flags, IMFTopology *topology)
1626 IMFTopology *resolved_topology = NULL;
1627 HRESULT hr = S_OK;
1629 /* Resolve unless claimed to be full. */
1630 if (!(flags & MFSESSION_SETTOPOLOGY_CLEAR_CURRENT) && topology)
1632 if (!(flags & MFSESSION_SETTOPOLOGY_NORESOLUTION))
1634 hr = session_bind_output_nodes(topology);
1636 if (SUCCEEDED(hr))
1637 hr = IMFTopoLoader_Load(session->topo_loader, topology, &resolved_topology, NULL /* FIXME? */);
1639 if (SUCCEEDED(hr))
1641 topology = resolved_topology;
1646 if (flags & MFSESSION_SETTOPOLOGY_CLEAR_CURRENT)
1648 if ((topology && topology == session->presentation.current_topology) || !topology)
1650 /* FIXME: stop current topology, queue next one. */
1651 session_clear_presentation(session);
1653 else
1654 hr = S_FALSE;
1656 topology = NULL;
1658 else if (topology && flags & MFSESSION_SETTOPOLOGY_IMMEDIATE)
1660 session_clear_topologies(session);
1661 session_clear_presentation(session);
1664 session_raise_topology_set(session, topology, hr);
1666 /* With no current topology set it right away, otherwise queue. */
1667 if (topology)
1669 struct queued_topology *queued_topology;
1671 if ((queued_topology = heap_alloc_zero(sizeof(*queued_topology))))
1673 queued_topology->topology = topology;
1674 IMFTopology_AddRef(queued_topology->topology);
1676 list_add_tail(&session->topologies, &queued_topology->entry);
1679 if (session->presentation.topo_status == MF_TOPOSTATUS_INVALID)
1681 hr = session_set_current_topology(session, topology);
1682 session_set_topo_status(session, hr, MF_TOPOSTATUS_READY);
1686 if (resolved_topology)
1687 IMFTopology_Release(resolved_topology);
1690 static HRESULT WINAPI mfsession_QueryInterface(IMFMediaSession *iface, REFIID riid, void **out)
1692 struct media_session *session = impl_from_IMFMediaSession(iface);
1694 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
1696 if (IsEqualIID(riid, &IID_IMFMediaSession) ||
1697 IsEqualIID(riid, &IID_IMFMediaEventGenerator) ||
1698 IsEqualIID(riid, &IID_IUnknown))
1700 *out = &session->IMFMediaSession_iface;
1701 IMFMediaSession_AddRef(iface);
1702 return S_OK;
1704 else if (IsEqualIID(riid, &IID_IMFGetService))
1706 *out = &session->IMFGetService_iface;
1707 IMFMediaSession_AddRef(iface);
1708 return S_OK;
1711 WARN("Unsupported %s.\n", debugstr_guid(riid));
1712 *out = NULL;
1713 return E_NOINTERFACE;
1716 static ULONG WINAPI mfsession_AddRef(IMFMediaSession *iface)
1718 struct media_session *session = impl_from_IMFMediaSession(iface);
1719 ULONG refcount = InterlockedIncrement(&session->refcount);
1721 TRACE("%p, refcount %u.\n", iface, refcount);
1723 return refcount;
1726 static ULONG WINAPI mfsession_Release(IMFMediaSession *iface)
1728 struct media_session *session = impl_from_IMFMediaSession(iface);
1729 ULONG refcount = InterlockedDecrement(&session->refcount);
1731 TRACE("%p, refcount %u.\n", iface, refcount);
1733 if (!refcount)
1735 session_clear_topologies(session);
1736 session_clear_presentation(session);
1737 session_clear_command_list(session);
1738 if (session->presentation.current_topology)
1739 IMFTopology_Release(session->presentation.current_topology);
1740 if (session->event_queue)
1741 IMFMediaEventQueue_Release(session->event_queue);
1742 if (session->clock)
1743 IMFPresentationClock_Release(session->clock);
1744 if (session->system_time_source)
1745 IMFPresentationTimeSource_Release(session->system_time_source);
1746 if (session->clock_rate_control)
1747 IMFRateControl_Release(session->clock_rate_control);
1748 if (session->topo_loader)
1749 IMFTopoLoader_Release(session->topo_loader);
1750 if (session->quality_manager)
1751 IMFQualityManager_Release(session->quality_manager);
1752 DeleteCriticalSection(&session->cs);
1753 heap_free(session);
1756 return refcount;
1759 static HRESULT WINAPI mfsession_GetEvent(IMFMediaSession *iface, DWORD flags, IMFMediaEvent **event)
1761 struct media_session *session = impl_from_IMFMediaSession(iface);
1763 TRACE("%p, %#x, %p.\n", iface, flags, event);
1765 return IMFMediaEventQueue_GetEvent(session->event_queue, flags, event);
1768 static HRESULT WINAPI mfsession_BeginGetEvent(IMFMediaSession *iface, IMFAsyncCallback *callback, IUnknown *state)
1770 struct media_session *session = impl_from_IMFMediaSession(iface);
1772 TRACE("%p, %p, %p.\n", iface, callback, state);
1774 return IMFMediaEventQueue_BeginGetEvent(session->event_queue, callback, state);
1777 static HRESULT WINAPI mfsession_EndGetEvent(IMFMediaSession *iface, IMFAsyncResult *result, IMFMediaEvent **event)
1779 struct media_session *session = impl_from_IMFMediaSession(iface);
1781 TRACE("%p, %p, %p.\n", iface, result, event);
1783 return IMFMediaEventQueue_EndGetEvent(session->event_queue, result, event);
1786 static HRESULT WINAPI mfsession_QueueEvent(IMFMediaSession *iface, MediaEventType event_type, REFGUID ext_type,
1787 HRESULT hr, const PROPVARIANT *value)
1789 struct media_session *session = impl_from_IMFMediaSession(iface);
1791 TRACE("%p, %d, %s, %#x, %p.\n", iface, event_type, debugstr_guid(ext_type), hr, value);
1793 return IMFMediaEventQueue_QueueEventParamVar(session->event_queue, event_type, ext_type, hr, value);
1796 static HRESULT WINAPI mfsession_SetTopology(IMFMediaSession *iface, DWORD flags, IMFTopology *topology)
1798 struct media_session *session = impl_from_IMFMediaSession(iface);
1799 struct session_op *op;
1800 WORD node_count = 0;
1801 HRESULT hr;
1803 TRACE("%p, %#x, %p.\n", iface, flags, topology);
1805 if (topology)
1807 if (FAILED(IMFTopology_GetNodeCount(topology, &node_count)) || node_count == 0)
1808 return E_INVALIDARG;
1811 if (FAILED(hr = create_session_op(SESSION_CMD_SET_TOPOLOGY, &op)))
1812 return hr;
1814 op->u.set_topology.flags = flags;
1815 op->u.set_topology.topology = topology;
1816 if (op->u.set_topology.topology)
1817 IMFTopology_AddRef(op->u.set_topology.topology);
1819 hr = session_submit_command(session, op);
1820 IUnknown_Release(&op->IUnknown_iface);
1822 return hr;
1825 static HRESULT WINAPI mfsession_ClearTopologies(IMFMediaSession *iface)
1827 struct media_session *session = impl_from_IMFMediaSession(iface);
1829 TRACE("%p.\n", iface);
1831 return session_submit_simple_command(session, SESSION_CMD_CLEAR_TOPOLOGIES);
1834 static HRESULT WINAPI mfsession_Start(IMFMediaSession *iface, const GUID *format, const PROPVARIANT *start_position)
1836 struct media_session *session = impl_from_IMFMediaSession(iface);
1837 struct session_op *op;
1838 HRESULT hr;
1840 TRACE("%p, %s, %p.\n", iface, debugstr_guid(format), start_position);
1842 if (!start_position)
1843 return E_POINTER;
1845 if (FAILED(hr = create_session_op(SESSION_CMD_START, &op)))
1846 return hr;
1848 op->u.start.time_format = format ? *format : GUID_NULL;
1849 hr = PropVariantCopy(&op->u.start.start_position, start_position);
1851 if (SUCCEEDED(hr))
1852 hr = session_submit_command(session, op);
1854 IUnknown_Release(&op->IUnknown_iface);
1855 return hr;
1858 static HRESULT WINAPI mfsession_Pause(IMFMediaSession *iface)
1860 struct media_session *session = impl_from_IMFMediaSession(iface);
1862 TRACE("%p.\n", iface);
1864 return session_submit_simple_command(session, SESSION_CMD_PAUSE);
1867 static HRESULT WINAPI mfsession_Stop(IMFMediaSession *iface)
1869 struct media_session *session = impl_from_IMFMediaSession(iface);
1871 TRACE("%p.\n", iface);
1873 return session_submit_simple_command(session, SESSION_CMD_STOP);
1876 static HRESULT WINAPI mfsession_Close(IMFMediaSession *iface)
1878 struct media_session *session = impl_from_IMFMediaSession(iface);
1880 TRACE("%p.\n", iface);
1882 return session_submit_simple_command(session, SESSION_CMD_CLOSE);
1885 static HRESULT WINAPI mfsession_Shutdown(IMFMediaSession *iface)
1887 struct media_session *session = impl_from_IMFMediaSession(iface);
1888 HRESULT hr = S_OK;
1890 TRACE("%p.\n", iface);
1892 EnterCriticalSection(&session->cs);
1893 if (SUCCEEDED(hr = session_is_shut_down(session)))
1895 session->state = SESSION_STATE_SHUT_DOWN;
1896 IMFMediaEventQueue_Shutdown(session->event_queue);
1897 if (session->quality_manager)
1898 IMFQualityManager_Shutdown(session->quality_manager);
1899 MFShutdownObject((IUnknown *)session->clock);
1900 IMFPresentationClock_Release(session->clock);
1901 session->clock = NULL;
1902 session_clear_presentation(session);
1903 session_clear_command_list(session);
1905 LeaveCriticalSection(&session->cs);
1907 return hr;
1910 static HRESULT WINAPI mfsession_GetClock(IMFMediaSession *iface, IMFClock **clock)
1912 struct media_session *session = impl_from_IMFMediaSession(iface);
1913 HRESULT hr;
1915 TRACE("%p, %p.\n", iface, clock);
1917 EnterCriticalSection(&session->cs);
1918 if (SUCCEEDED(hr = session_is_shut_down(session)))
1920 *clock = (IMFClock *)session->clock;
1921 IMFClock_AddRef(*clock);
1923 LeaveCriticalSection(&session->cs);
1925 return hr;
1928 static HRESULT WINAPI mfsession_GetSessionCapabilities(IMFMediaSession *iface, DWORD *caps)
1930 struct media_session *session = impl_from_IMFMediaSession(iface);
1931 HRESULT hr = S_OK;
1933 TRACE("%p, %p.\n", iface, caps);
1935 if (!caps)
1936 return E_POINTER;
1938 EnterCriticalSection(&session->cs);
1939 if (SUCCEEDED(hr = session_is_shut_down(session)))
1940 *caps = session->caps;
1941 LeaveCriticalSection(&session->cs);
1943 return hr;
1946 static HRESULT WINAPI mfsession_GetFullTopology(IMFMediaSession *iface, DWORD flags, TOPOID id, IMFTopology **topology)
1948 struct media_session *session = impl_from_IMFMediaSession(iface);
1949 struct queued_topology *queued;
1950 TOPOID topo_id;
1951 HRESULT hr;
1953 TRACE("%p, %#x, %s, %p.\n", iface, flags, wine_dbgstr_longlong(id), topology);
1955 *topology = NULL;
1957 EnterCriticalSection(&session->cs);
1959 if (SUCCEEDED(hr = session_is_shut_down(session)))
1961 if (flags & MFSESSION_GETFULLTOPOLOGY_CURRENT)
1963 if (session->presentation.topo_status != MF_TOPOSTATUS_INVALID)
1964 *topology = session->presentation.current_topology;
1965 else
1966 hr = MF_E_INVALIDREQUEST;
1968 else
1970 LIST_FOR_EACH_ENTRY(queued, &session->topologies, struct queued_topology, entry)
1972 if (SUCCEEDED(IMFTopology_GetTopologyID(queued->topology, &topo_id)) && topo_id == id)
1974 *topology = queued->topology;
1975 break;
1980 if (*topology)
1981 IMFTopology_AddRef(*topology);
1984 LeaveCriticalSection(&session->cs);
1986 return hr;
1989 static const IMFMediaSessionVtbl mfmediasessionvtbl =
1991 mfsession_QueryInterface,
1992 mfsession_AddRef,
1993 mfsession_Release,
1994 mfsession_GetEvent,
1995 mfsession_BeginGetEvent,
1996 mfsession_EndGetEvent,
1997 mfsession_QueueEvent,
1998 mfsession_SetTopology,
1999 mfsession_ClearTopologies,
2000 mfsession_Start,
2001 mfsession_Pause,
2002 mfsession_Stop,
2003 mfsession_Close,
2004 mfsession_Shutdown,
2005 mfsession_GetClock,
2006 mfsession_GetSessionCapabilities,
2007 mfsession_GetFullTopology,
2010 static HRESULT WINAPI session_get_service_QueryInterface(IMFGetService *iface, REFIID riid, void **obj)
2012 struct media_session *session = impl_from_IMFGetService(iface);
2013 return IMFMediaSession_QueryInterface(&session->IMFMediaSession_iface, riid, obj);
2016 static ULONG WINAPI session_get_service_AddRef(IMFGetService *iface)
2018 struct media_session *session = impl_from_IMFGetService(iface);
2019 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
2022 static ULONG WINAPI session_get_service_Release(IMFGetService *iface)
2024 struct media_session *session = impl_from_IMFGetService(iface);
2025 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
2028 static HRESULT session_get_video_render_service(struct media_session *session, REFGUID service,
2029 REFIID riid, void **obj)
2031 IMFStreamSink *stream_sink;
2032 IMFTopologyNode *node;
2033 IMFCollection *nodes;
2034 IMFMediaSink *sink;
2035 unsigned int i = 0;
2036 IUnknown *vr;
2037 HRESULT hr = E_FAIL;
2039 /* Use first sink to support IMFVideoRenderer. */
2040 if (session->presentation.current_topology)
2042 if (SUCCEEDED(IMFTopology_GetOutputNodeCollection(session->presentation.current_topology,
2043 &nodes)))
2045 while (IMFCollection_GetElement(nodes, i++, (IUnknown **)&node) == S_OK)
2047 if (SUCCEEDED(topology_node_get_object(node, &IID_IMFStreamSink, (void **)&stream_sink)))
2049 if (SUCCEEDED(IMFStreamSink_GetMediaSink(stream_sink, &sink)))
2051 if (SUCCEEDED(IMFMediaSink_QueryInterface(sink, &IID_IMFVideoRenderer, (void **)&vr)))
2053 if (FAILED(hr = MFGetService(vr, service, riid, obj)))
2054 WARN("Failed to get service from video renderer %#x.\n", hr);
2055 IUnknown_Release(vr);
2058 IMFStreamSink_Release(stream_sink);
2061 IMFTopologyNode_Release(node);
2063 if (*obj)
2064 break;
2067 IMFCollection_Release(nodes);
2071 return hr;
2074 static HRESULT WINAPI session_get_service_GetService(IMFGetService *iface, REFGUID service, REFIID riid, void **obj)
2076 struct media_session *session = impl_from_IMFGetService(iface);
2077 HRESULT hr = S_OK;
2079 TRACE("%p, %s, %s, %p.\n", iface, debugstr_guid(service), debugstr_guid(riid), obj);
2081 *obj = NULL;
2083 EnterCriticalSection(&session->cs);
2084 if (FAILED(hr = session_is_shut_down(session)))
2087 else if (IsEqualGUID(service, &MF_RATE_CONTROL_SERVICE))
2089 if (IsEqualIID(riid, &IID_IMFRateSupport))
2091 *obj = &session->IMFRateSupport_iface;
2093 else if (IsEqualIID(riid, &IID_IMFRateControl))
2095 *obj = &session->IMFRateControl_iface;
2097 else
2098 hr = E_NOINTERFACE;
2100 if (*obj)
2101 IUnknown_AddRef((IUnknown *)*obj);
2103 else if (IsEqualGUID(service, &MF_LOCAL_MFT_REGISTRATION_SERVICE))
2105 hr = IMFLocalMFTRegistration_QueryInterface(&local_mft_registration, riid, obj);
2107 else if (IsEqualGUID(service, &MF_TOPONODE_ATTRIBUTE_EDITOR_SERVICE))
2109 *obj = &session->IMFTopologyNodeAttributeEditor_iface;
2110 IUnknown_AddRef((IUnknown *)*obj);
2112 else if (IsEqualGUID(service, &MR_VIDEO_RENDER_SERVICE))
2114 hr = session_get_video_render_service(session, service, riid, obj);
2116 else
2117 FIXME("Unsupported service %s.\n", debugstr_guid(service));
2119 LeaveCriticalSection(&session->cs);
2121 return hr;
2124 static const IMFGetServiceVtbl session_get_service_vtbl =
2126 session_get_service_QueryInterface,
2127 session_get_service_AddRef,
2128 session_get_service_Release,
2129 session_get_service_GetService,
2132 static HRESULT WINAPI session_commands_callback_QueryInterface(IMFAsyncCallback *iface, REFIID riid, void **obj)
2134 if (IsEqualIID(riid, &IID_IMFAsyncCallback) ||
2135 IsEqualIID(riid, &IID_IUnknown))
2137 *obj = iface;
2138 IMFAsyncCallback_AddRef(iface);
2139 return S_OK;
2142 WARN("Unsupported %s.\n", debugstr_guid(riid));
2143 *obj = NULL;
2144 return E_NOINTERFACE;
2147 static ULONG WINAPI session_commands_callback_AddRef(IMFAsyncCallback *iface)
2149 struct media_session *session = impl_from_commands_callback_IMFAsyncCallback(iface);
2150 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
2153 static ULONG WINAPI session_commands_callback_Release(IMFAsyncCallback *iface)
2155 struct media_session *session = impl_from_commands_callback_IMFAsyncCallback(iface);
2156 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
2159 static HRESULT WINAPI session_commands_callback_GetParameters(IMFAsyncCallback *iface, DWORD *flags, DWORD *queue)
2161 return E_NOTIMPL;
2164 static HRESULT WINAPI session_commands_callback_Invoke(IMFAsyncCallback *iface, IMFAsyncResult *result)
2166 struct session_op *op = impl_op_from_IUnknown(IMFAsyncResult_GetStateNoAddRef(result));
2167 struct media_session *session = impl_from_commands_callback_IMFAsyncCallback(iface);
2168 struct topo_node *topo_node;
2169 IMFTopologyNode *upstream_node;
2170 unsigned int upstream_output;
2172 EnterCriticalSection(&session->cs);
2174 switch (op->command)
2176 case SESSION_CMD_CLEAR_TOPOLOGIES:
2177 session_clear_topologies(session);
2178 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MESessionTopologiesCleared, &GUID_NULL,
2179 S_OK, NULL);
2180 session_command_complete(session);
2181 break;
2182 case SESSION_CMD_SET_TOPOLOGY:
2183 session_set_topology(session, op->u.set_topology.flags, op->u.set_topology.topology);
2184 session_command_complete(session);
2185 break;
2186 case SESSION_CMD_START:
2187 session_start(session, &op->u.start.time_format, &op->u.start.start_position);
2188 break;
2189 case SESSION_CMD_PAUSE:
2190 session_pause(session);
2191 break;
2192 case SESSION_CMD_STOP:
2193 session_stop(session);
2194 break;
2195 case SESSION_CMD_CLOSE:
2196 session_close(session);
2197 break;
2198 case SESSION_CMD_QM_NOTIFY_TOPOLOGY:
2199 IMFQualityManager_NotifyTopology(session->quality_manager, op->u.notify_topology.topology);
2200 session_command_complete(session);
2201 break;
2202 case SESSION_CMD_SA_READY:
2203 topo_node = session_get_node_by_id(session, op->u.sa_ready.node_id);
2205 if (topo_node->u.sink.requests)
2207 if (SUCCEEDED(IMFTopologyNode_GetInput(topo_node->node, 0, &upstream_node, &upstream_output)))
2209 session_request_sample_from_node(session, upstream_node, upstream_output);
2210 IMFTopologyNode_Release(upstream_node);
2213 break;
2214 default:
2218 LeaveCriticalSection(&session->cs);
2220 return S_OK;
2223 static const IMFAsyncCallbackVtbl session_commands_callback_vtbl =
2225 session_commands_callback_QueryInterface,
2226 session_commands_callback_AddRef,
2227 session_commands_callback_Release,
2228 session_commands_callback_GetParameters,
2229 session_commands_callback_Invoke,
2232 static HRESULT WINAPI session_events_callback_QueryInterface(IMFAsyncCallback *iface, REFIID riid, void **obj)
2234 if (IsEqualIID(riid, &IID_IMFAsyncCallback) ||
2235 IsEqualIID(riid, &IID_IUnknown))
2237 *obj = iface;
2238 IMFAsyncCallback_AddRef(iface);
2239 return S_OK;
2242 WARN("Unsupported %s.\n", debugstr_guid(riid));
2243 *obj = NULL;
2244 return E_NOINTERFACE;
2247 static ULONG WINAPI session_events_callback_AddRef(IMFAsyncCallback *iface)
2249 struct media_session *session = impl_from_events_callback_IMFAsyncCallback(iface);
2250 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
2253 static ULONG WINAPI session_events_callback_Release(IMFAsyncCallback *iface)
2255 struct media_session *session = impl_from_events_callback_IMFAsyncCallback(iface);
2256 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
2259 static HRESULT WINAPI session_events_callback_GetParameters(IMFAsyncCallback *iface, DWORD *flags, DWORD *queue)
2261 return E_NOTIMPL;
2264 static HRESULT session_add_media_stream(struct media_session *session, IMFMediaSource *source, IMFMediaStream *stream)
2266 struct topo_node *node;
2267 IMFStreamDescriptor *sd;
2268 DWORD stream_id = 0;
2269 HRESULT hr;
2271 if (FAILED(hr = IMFMediaStream_GetStreamDescriptor(stream, &sd)))
2272 return hr;
2274 hr = IMFStreamDescriptor_GetStreamIdentifier(sd, &stream_id);
2275 IMFStreamDescriptor_Release(sd);
2276 if (FAILED(hr))
2277 return hr;
2279 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2281 if (node->type == MF_TOPOLOGY_SOURCESTREAM_NODE && node->u.source.source == source
2282 && node->u.source.stream_id == stream_id)
2284 if (node->object.source_stream)
2286 WARN("Node already has stream set.\n");
2287 return S_FALSE;
2290 node->object.source_stream = stream;
2291 IMFMediaStream_AddRef(node->object.source_stream);
2292 break;
2296 return S_OK;
2299 static BOOL session_is_source_nodes_state(struct media_session *session, enum object_state state)
2301 struct media_source *source;
2302 struct topo_node *node;
2304 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
2306 if (source->state != state)
2307 return FALSE;
2310 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2312 if (node->type == MF_TOPOLOGY_SOURCESTREAM_NODE && node->state != state)
2313 return FALSE;
2316 return TRUE;
2319 static BOOL session_is_output_nodes_state(struct media_session *session, enum object_state state)
2321 struct topo_node *node;
2323 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2325 if (node->type == MF_TOPOLOGY_OUTPUT_NODE && node->state != state)
2326 return FALSE;
2329 return TRUE;
2332 static enum object_state session_get_object_state_for_event(MediaEventType event)
2334 switch (event)
2336 case MESourceStarted:
2337 case MEStreamStarted:
2338 case MEStreamSinkStarted:
2339 return OBJ_STATE_STARTED;
2340 case MESourcePaused:
2341 case MEStreamPaused:
2342 case MEStreamSinkPaused:
2343 return OBJ_STATE_PAUSED;
2344 case MESourceStopped:
2345 case MEStreamStopped:
2346 case MEStreamSinkStopped:
2347 return OBJ_STATE_STOPPED;
2348 case MEStreamSinkPrerolled:
2349 return OBJ_STATE_PREROLLED;
2350 default:
2351 return OBJ_STATE_INVALID;
2355 static void session_set_consumed_clock(IUnknown *object, IMFPresentationClock *clock)
2357 IMFClockConsumer *consumer;
2359 if (SUCCEEDED(IUnknown_QueryInterface(object, &IID_IMFClockConsumer, (void **)&consumer)))
2361 IMFClockConsumer_SetPresentationClock(consumer, clock);
2362 IMFClockConsumer_Release(consumer);
2366 static void session_set_presentation_clock(struct media_session *session)
2368 IMFPresentationTimeSource *time_source = NULL;
2369 struct media_source *source;
2370 struct media_sink *sink;
2371 struct topo_node *node;
2372 HRESULT hr;
2374 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2376 if (node->type == MF_TOPOLOGY_TRANSFORM_NODE)
2377 IMFTransform_ProcessMessage(node->object.transform, MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0);
2380 if (!(session->presentation.flags & SESSION_FLAG_PRESENTATION_CLOCK_SET))
2382 /* Attempt to get time source from the sinks. */
2383 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
2385 if (SUCCEEDED(IMFMediaSink_QueryInterface(sink->sink, &IID_IMFPresentationTimeSource,
2386 (void **)&time_source)))
2387 break;
2390 if (time_source)
2392 hr = IMFPresentationClock_SetTimeSource(session->clock, time_source);
2393 IMFPresentationTimeSource_Release(time_source);
2395 else
2396 hr = IMFPresentationClock_SetTimeSource(session->clock, session->system_time_source);
2398 if (FAILED(hr))
2399 WARN("Failed to set time source, hr %#x.\n", hr);
2401 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2403 if (node->type != MF_TOPOLOGY_OUTPUT_NODE)
2404 continue;
2406 if (FAILED(hr = IMFStreamSink_BeginGetEvent(node->object.sink_stream, &session->events_callback,
2407 node->object.object)))
2409 WARN("Failed to subscribe to stream sink events, hr %#x.\n", hr);
2413 /* Set clock for all topology nodes. */
2414 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
2416 session_set_consumed_clock((IUnknown *)source->source, session->clock);
2419 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
2421 if (sink->event_generator && FAILED(hr = IMFMediaEventGenerator_BeginGetEvent(sink->event_generator,
2422 &session->events_callback, (IUnknown *)sink->event_generator)))
2424 WARN("Failed to subscribe to sink events, hr %#x.\n", hr);
2427 if (FAILED(hr = IMFMediaSink_SetPresentationClock(sink->sink, session->clock)))
2428 WARN("Failed to set presentation clock for the sink, hr %#x.\n", hr);
2431 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2433 if (node->type != MF_TOPOLOGY_TRANSFORM_NODE)
2434 continue;
2436 session_set_consumed_clock(node->object.object, session->clock);
2439 session->presentation.flags |= SESSION_FLAG_PRESENTATION_CLOCK_SET;
2443 static HRESULT session_start_clock(struct media_session *session)
2445 LONGLONG start_offset = 0;
2446 HRESULT hr;
2448 if (IsEqualGUID(&session->presentation.time_format, &GUID_NULL))
2450 if (session->presentation.start_position.vt == VT_EMPTY)
2451 start_offset = PRESENTATION_CURRENT_POSITION;
2452 else if (session->presentation.start_position.vt == VT_I8)
2453 start_offset = session->presentation.start_position.hVal.QuadPart;
2454 else
2455 FIXME("Unhandled position type %d.\n", session->presentation.start_position.vt);
2457 else
2458 FIXME("Unhandled time format %s.\n", debugstr_guid(&session->presentation.time_format));
2460 if (FAILED(hr = IMFPresentationClock_Start(session->clock, start_offset)))
2461 WARN("Failed to start session clock, hr %#x.\n", hr);
2463 return hr;
2466 static struct topo_node *session_get_node_object(struct media_session *session, IUnknown *object,
2467 MF_TOPOLOGY_TYPE node_type)
2469 struct topo_node *node = NULL;
2471 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2473 if (node->type == node_type && object == node->object.object)
2474 break;
2477 return node;
2480 static BOOL session_set_node_object_state(struct media_session *session, IUnknown *object,
2481 MF_TOPOLOGY_TYPE node_type, enum object_state state)
2483 struct topo_node *node;
2484 BOOL changed = FALSE;
2486 if ((node = session_get_node_object(session, object, node_type)))
2488 changed = node->state != state;
2489 node->state = state;
2492 return changed;
2495 static void session_set_source_object_state(struct media_session *session, IUnknown *object,
2496 MediaEventType event_type)
2498 IMFStreamSink *stream_sink;
2499 struct media_source *src;
2500 struct media_sink *sink;
2501 enum object_state state;
2502 struct topo_node *node;
2503 unsigned int i, count;
2504 BOOL changed = FALSE;
2505 HRESULT hr;
2507 if ((state = session_get_object_state_for_event(event_type)) == OBJ_STATE_INVALID)
2508 return;
2510 switch (event_type)
2512 case MESourceStarted:
2513 case MESourcePaused:
2514 case MESourceStopped:
2516 LIST_FOR_EACH_ENTRY(src, &session->presentation.sources, struct media_source, entry)
2518 if (object == (IUnknown *)src->source)
2520 changed = src->state != state;
2521 src->state = state;
2522 break;
2525 break;
2526 case MEStreamStarted:
2527 case MEStreamPaused:
2528 case MEStreamStopped:
2530 changed = session_set_node_object_state(session, object, MF_TOPOLOGY_SOURCESTREAM_NODE, state);
2531 default:
2535 if (!changed)
2536 return;
2538 switch (session->state)
2540 case SESSION_STATE_STARTING_SOURCES:
2541 if (!session_is_source_nodes_state(session, OBJ_STATE_STARTED))
2542 break;
2544 session_set_topo_status(session, S_OK, MF_TOPOSTATUS_STARTED_SOURCE);
2546 session_set_presentation_clock(session);
2548 if (session->presentation.flags & SESSION_FLAG_NEEDS_PREROLL)
2550 MFTIME preroll_time = 0;
2552 if (session->presentation.start_position.vt == VT_I8)
2553 preroll_time = session->presentation.start_position.hVal.QuadPart;
2555 /* Mark stream sinks without prerolling support as PREROLLED to keep state test logic generic. */
2556 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
2558 if (sink->preroll)
2560 /* FIXME: abort and enter error state on failure. */
2561 if (FAILED(hr = IMFMediaSinkPreroll_NotifyPreroll(sink->preroll, preroll_time)))
2562 WARN("Preroll notification failed, hr %#x.\n", hr);
2564 else
2566 if (SUCCEEDED(IMFMediaSink_GetStreamSinkCount(sink->sink, &count)))
2568 for (i = 0; i < count; ++i)
2570 if (SUCCEEDED(IMFMediaSink_GetStreamSinkByIndex(sink->sink, i, &stream_sink)))
2572 session_set_node_object_state(session, (IUnknown *)stream_sink, MF_TOPOLOGY_OUTPUT_NODE,
2573 OBJ_STATE_PREROLLED);
2574 IMFStreamSink_Release(stream_sink);
2580 session->state = SESSION_STATE_PREROLLING_SINKS;
2582 else if (SUCCEEDED(session_start_clock(session)))
2583 session->state = SESSION_STATE_STARTING_SINKS;
2585 break;
2586 case SESSION_STATE_PAUSING_SOURCES:
2587 if (!session_is_source_nodes_state(session, OBJ_STATE_PAUSED))
2588 break;
2590 session_set_paused(session, S_OK);
2591 break;
2592 case SESSION_STATE_STOPPING_SOURCES:
2593 if (!session_is_source_nodes_state(session, OBJ_STATE_STOPPED))
2594 break;
2596 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2598 switch (node->type)
2600 case MF_TOPOLOGY_OUTPUT_NODE:
2601 IMFStreamSink_Flush(node->object.sink_stream);
2602 break;
2603 case MF_TOPOLOGY_TRANSFORM_NODE:
2604 IMFTransform_ProcessMessage(node->object.transform, MFT_MESSAGE_COMMAND_FLUSH, 0);
2605 break;
2606 default:
2611 session_set_caps(session, session->caps & ~MFSESSIONCAP_PAUSE);
2613 if (session->presentation.flags & SESSION_FLAG_FINALIZE_SINKS)
2614 session_finalize_sinks(session);
2615 else
2616 session_set_stopped(session, S_OK);
2618 break;
2619 default:
2624 static void session_set_sink_stream_state(struct media_session *session, IMFStreamSink *stream,
2625 MediaEventType event_type)
2627 struct media_source *source;
2628 enum object_state state;
2629 HRESULT hr = S_OK;
2630 BOOL changed;
2632 if ((state = session_get_object_state_for_event(event_type)) == OBJ_STATE_INVALID)
2633 return;
2635 if (!(changed = session_set_node_object_state(session, (IUnknown *)stream, MF_TOPOLOGY_OUTPUT_NODE, state)))
2636 return;
2638 switch (session->state)
2640 case SESSION_STATE_PREROLLING_SINKS:
2641 if (!session_is_output_nodes_state(session, OBJ_STATE_PREROLLED))
2642 break;
2644 if (SUCCEEDED(session_start_clock(session)))
2645 session->state = SESSION_STATE_STARTING_SINKS;
2646 break;
2647 case SESSION_STATE_STARTING_SINKS:
2648 if (!session_is_output_nodes_state(session, OBJ_STATE_STARTED))
2649 break;
2651 session_set_started(session);
2652 break;
2653 case SESSION_STATE_PAUSING_SINKS:
2654 if (!session_is_output_nodes_state(session, OBJ_STATE_PAUSED))
2655 break;
2657 session->state = SESSION_STATE_PAUSING_SOURCES;
2659 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
2661 if (FAILED(hr = IMFMediaSource_Pause(source->source)))
2662 break;
2665 if (FAILED(hr))
2666 session_set_paused(session, hr);
2668 break;
2669 case SESSION_STATE_STOPPING_SINKS:
2670 if (!session_is_output_nodes_state(session, OBJ_STATE_STOPPED))
2671 break;
2673 session->state = SESSION_STATE_STOPPING_SOURCES;
2675 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
2677 if (session->presentation.flags & SESSION_FLAG_END_OF_PRESENTATION)
2678 IMFMediaSource_Stop(source->source);
2679 else if (FAILED(hr = IMFMediaSource_Stop(source->source)))
2680 break;
2683 if (session->presentation.flags & SESSION_FLAG_END_OF_PRESENTATION || FAILED(hr))
2684 session_set_stopped(session, hr);
2686 break;
2687 default:
2692 static struct sample *transform_create_sample(IMFSample *sample)
2694 struct sample *sample_entry = heap_alloc_zero(sizeof(*sample_entry));
2696 if (sample_entry)
2698 sample_entry->sample = sample;
2699 if (sample_entry->sample)
2700 IMFSample_AddRef(sample_entry->sample);
2703 return sample_entry;
2706 static HRESULT transform_get_external_output_sample(const struct media_session *session, struct topo_node *transform,
2707 unsigned int output_index, const MFT_OUTPUT_STREAM_INFO *stream_info, IMFSample **sample)
2709 unsigned int buffer_size, downstream_input;
2710 IMFTopologyNode *downstream_node;
2711 IMFMediaBuffer *buffer = NULL;
2712 struct topo_node *topo_node;
2713 TOPOID node_id;
2714 HRESULT hr;
2716 if (FAILED(IMFTopologyNode_GetOutput(transform->node, output_index, &downstream_node, &downstream_input)))
2718 WARN("Failed to get connected node for output %u.\n", output_index);
2719 return MF_E_UNEXPECTED;
2722 IMFTopologyNode_GetTopoNodeID(downstream_node, &node_id);
2723 IMFTopologyNode_Release(downstream_node);
2725 topo_node = session_get_node_by_id(session, node_id);
2727 if (topo_node->type == MF_TOPOLOGY_OUTPUT_NODE && topo_node->u.sink.allocator)
2729 hr = IMFVideoSampleAllocator_AllocateSample(topo_node->u.sink.allocator, sample);
2731 else
2733 buffer_size = max(stream_info->cbSize, transform->u.transform.outputs[output_index].min_buffer_size);
2735 hr = MFCreateAlignedMemoryBuffer(buffer_size, stream_info->cbAlignment, &buffer);
2736 if (SUCCEEDED(hr))
2737 hr = MFCreateSample(sample);
2739 if (SUCCEEDED(hr))
2740 hr = IMFSample_AddBuffer(*sample, buffer);
2742 if (buffer)
2743 IMFMediaBuffer_Release(buffer);
2746 return hr;
2749 static HRESULT transform_node_pull_samples(const struct media_session *session, struct topo_node *node)
2751 MFT_OUTPUT_STREAM_INFO stream_info;
2752 MFT_OUTPUT_DATA_BUFFER *buffers;
2753 struct sample *queued_sample;
2754 DWORD status = 0;
2755 unsigned int i;
2756 HRESULT hr = E_UNEXPECTED;
2758 if (!(buffers = heap_calloc(node->u.transform.output_count, sizeof(*buffers))))
2759 return E_OUTOFMEMORY;
2761 for (i = 0; i < node->u.transform.output_count; ++i)
2763 buffers[i].dwStreamID = transform_node_get_stream_id(node, TRUE, i);
2764 buffers[i].pSample = NULL;
2765 buffers[i].dwStatus = 0;
2766 buffers[i].pEvents = NULL;
2768 memset(&stream_info, 0, sizeof(stream_info));
2769 if (FAILED(hr = IMFTransform_GetOutputStreamInfo(node->object.transform, buffers[i].dwStreamID, &stream_info)))
2770 break;
2772 if (!(stream_info.dwFlags & (MFT_OUTPUT_STREAM_PROVIDES_SAMPLES | MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES)))
2774 if (FAILED(hr = transform_get_external_output_sample(session, node, i, &stream_info, &buffers[i].pSample)))
2775 break;
2779 if (SUCCEEDED(hr))
2780 hr = IMFTransform_ProcessOutput(node->object.transform, 0, node->u.transform.output_count, buffers, &status);
2782 /* Collect returned samples for all streams. */
2783 for (i = 0; i < node->u.transform.output_count; ++i)
2785 if (buffers[i].pEvents)
2786 IMFCollection_Release(buffers[i].pEvents);
2788 if (SUCCEEDED(hr) && !(buffers[i].dwStatus & MFT_OUTPUT_DATA_BUFFER_NO_SAMPLE))
2790 if (session->quality_manager)
2791 IMFQualityManager_NotifyProcessOutput(session->quality_manager, node->node, i, buffers[i].pSample);
2793 queued_sample = transform_create_sample(buffers[i].pSample);
2794 list_add_tail(&node->u.transform.outputs[i].samples, &queued_sample->entry);
2797 if (buffers[i].pSample)
2798 IMFSample_Release(buffers[i].pSample);
2801 heap_free(buffers);
2803 return hr;
2806 static void session_deliver_sample_to_node(struct media_session *session, IMFTopologyNode *node, unsigned int input,
2807 IMFSample *sample)
2809 struct sample *sample_entry, *sample_entry2;
2810 DWORD stream_id, downstream_input;
2811 IMFTopologyNode *downstream_node;
2812 struct topo_node *topo_node;
2813 MF_TOPOLOGY_TYPE node_type;
2814 BOOL drain = FALSE;
2815 TOPOID node_id;
2816 unsigned int i;
2817 HRESULT hr;
2819 if (session->quality_manager)
2820 IMFQualityManager_NotifyProcessInput(session->quality_manager, node, input, sample);
2822 IMFTopologyNode_GetNodeType(node, &node_type);
2823 IMFTopologyNode_GetTopoNodeID(node, &node_id);
2825 topo_node = session_get_node_by_id(session, node_id);
2827 switch (node_type)
2829 case MF_TOPOLOGY_OUTPUT_NODE:
2830 if (sample)
2832 if (topo_node->u.sink.requests)
2834 if (FAILED(hr = IMFStreamSink_ProcessSample(topo_node->object.sink_stream, sample)))
2835 WARN("Stream sink failed to process sample, hr %#x.\n", hr);
2836 topo_node->u.sink.requests--;
2839 else if (FAILED(hr = IMFStreamSink_PlaceMarker(topo_node->object.sink_stream, MFSTREAMSINK_MARKER_ENDOFSEGMENT,
2840 NULL, NULL)))
2842 WARN("Failed to place sink marker, hr %#x.\n", hr);
2844 break;
2845 case MF_TOPOLOGY_TRANSFORM_NODE:
2847 transform_node_pull_samples(session, topo_node);
2849 sample_entry = transform_create_sample(sample);
2850 list_add_tail(&topo_node->u.transform.inputs[input].samples, &sample_entry->entry);
2852 for (i = 0; i < topo_node->u.transform.input_count; ++i)
2854 stream_id = transform_node_get_stream_id(topo_node, FALSE, i);
2855 LIST_FOR_EACH_ENTRY_SAFE(sample_entry, sample_entry2, &topo_node->u.transform.inputs[i].samples,
2856 struct sample, entry)
2858 if (sample_entry->sample)
2860 if ((hr = IMFTransform_ProcessInput(topo_node->object.transform, stream_id,
2861 sample_entry->sample, 0)) == MF_E_NOTACCEPTING)
2862 break;
2863 if (FAILED(hr))
2864 WARN("Failed to process input for stream %u/%u, hr %#x.\n", i, stream_id, hr);
2865 transform_release_sample(sample_entry);
2867 else
2869 transform_stream_drop_samples(&topo_node->u.transform.inputs[i]);
2870 drain = TRUE;
2875 if (drain)
2877 if (FAILED(hr = IMFTransform_ProcessMessage(topo_node->object.transform, MFT_MESSAGE_COMMAND_DRAIN, 0)))
2878 WARN("Drain command failed for transform, hr %#x.\n", hr);
2881 transform_node_pull_samples(session, topo_node);
2883 /* Remaining unprocessed input has been discarded, now queue markers for every output. */
2884 if (drain)
2886 for (i = 0; i < topo_node->u.transform.output_count; ++i)
2888 if ((sample_entry = transform_create_sample(NULL)))
2889 list_add_tail(&topo_node->u.transform.outputs[i].samples, &sample_entry->entry);
2893 /* Push down all available output. */
2894 for (i = 0; i < topo_node->u.transform.output_count; ++i)
2896 if (FAILED(IMFTopologyNode_GetOutput(node, i, &downstream_node, &downstream_input)))
2898 WARN("Failed to get connected node for output %u.\n", i);
2899 continue;
2902 LIST_FOR_EACH_ENTRY_SAFE(sample_entry, sample_entry2, &topo_node->u.transform.outputs[i].samples,
2903 struct sample, entry)
2905 if (!topo_node->u.transform.outputs[i].requests)
2906 break;
2908 session_deliver_sample_to_node(session, downstream_node, downstream_input, sample_entry->sample);
2909 topo_node->u.transform.outputs[i].requests--;
2911 transform_release_sample(sample_entry);
2914 IMFTopologyNode_Release(downstream_node);
2917 break;
2918 case MF_TOPOLOGY_TEE_NODE:
2919 FIXME("Unhandled downstream node type %d.\n", node_type);
2920 break;
2921 default:
2926 static HRESULT session_request_sample_from_node(struct media_session *session, IMFTopologyNode *node, DWORD output)
2928 IMFTopologyNode *downstream_node, *upstream_node;
2929 unsigned int downstream_input, upstream_output;
2930 struct topo_node *topo_node;
2931 MF_TOPOLOGY_TYPE node_type;
2932 struct sample *sample;
2933 TOPOID node_id;
2934 HRESULT hr;
2936 IMFTopologyNode_GetNodeType(node, &node_type);
2937 IMFTopologyNode_GetTopoNodeID(node, &node_id);
2939 topo_node = session_get_node_by_id(session, node_id);
2941 switch (node_type)
2943 case MF_TOPOLOGY_SOURCESTREAM_NODE:
2944 if (FAILED(hr = IMFMediaStream_RequestSample(topo_node->object.source_stream, NULL)))
2945 WARN("Sample request failed, hr %#x.\n", hr);
2946 break;
2947 case MF_TOPOLOGY_TRANSFORM_NODE:
2949 if (list_empty(&topo_node->u.transform.outputs[output].samples))
2951 /* Forward request to upstream node. */
2952 if (SUCCEEDED(hr = IMFTopologyNode_GetInput(node, 0 /* FIXME */, &upstream_node, &upstream_output)))
2954 if (SUCCEEDED(hr = session_request_sample_from_node(session, upstream_node, upstream_output)))
2955 topo_node->u.transform.outputs[output].requests++;
2956 IMFTopologyNode_Release(upstream_node);
2959 else
2961 if (SUCCEEDED(hr = IMFTopologyNode_GetOutput(node, output, &downstream_node, &downstream_input)))
2963 sample = LIST_ENTRY(list_head(&topo_node->u.transform.outputs[output].samples), struct sample, entry);
2964 session_deliver_sample_to_node(session, downstream_node, downstream_input, sample->sample);
2965 transform_release_sample(sample);
2966 IMFTopologyNode_Release(downstream_node);
2970 break;
2971 case MF_TOPOLOGY_TEE_NODE:
2972 FIXME("Unhandled upstream node type %d.\n", node_type);
2973 default:
2974 hr = E_UNEXPECTED;
2977 return hr;
2980 static void session_request_sample(struct media_session *session, IMFStreamSink *sink_stream)
2982 struct topo_node *sink_node = NULL, *node;
2983 IMFTopologyNode *upstream_node;
2984 DWORD upstream_output;
2985 HRESULT hr;
2987 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
2989 if (node->type == MF_TOPOLOGY_OUTPUT_NODE && node->object.sink_stream == sink_stream)
2991 sink_node = node;
2992 break;
2996 if (!sink_node)
2997 return;
2999 if (FAILED(hr = IMFTopologyNode_GetInput(sink_node->node, 0, &upstream_node, &upstream_output)))
3001 WARN("Failed to get upstream node connection, hr %#x.\n", hr);
3002 return;
3005 if (SUCCEEDED(session_request_sample_from_node(session, upstream_node, upstream_output)))
3006 sink_node->u.sink.requests++;
3007 IMFTopologyNode_Release(upstream_node);
3010 static void session_deliver_sample(struct media_session *session, IMFMediaStream *stream, const PROPVARIANT *value)
3012 struct topo_node *source_node = NULL, *node;
3013 IMFTopologyNode *downstream_node;
3014 DWORD downstream_input;
3015 HRESULT hr;
3017 if (value && (value->vt != VT_UNKNOWN || !value->punkVal))
3019 WARN("Unexpected value type %d.\n", value->vt);
3020 return;
3023 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
3025 if (node->type == MF_TOPOLOGY_SOURCESTREAM_NODE && node->object.source_stream == stream)
3027 source_node = node;
3028 break;
3032 if (!source_node)
3033 return;
3035 if (!value)
3036 source_node->flags |= TOPO_NODE_END_OF_STREAM;
3038 if (FAILED(hr = IMFTopologyNode_GetOutput(source_node->node, 0, &downstream_node, &downstream_input)))
3040 WARN("Failed to get downstream node connection, hr %#x.\n", hr);
3041 return;
3044 session_deliver_sample_to_node(session, downstream_node, downstream_input, value ? (IMFSample *)value->punkVal : NULL);
3045 IMFTopologyNode_Release(downstream_node);
3048 static void session_sink_invalidated(struct media_session *session, IMFMediaEvent *event, IMFStreamSink *sink)
3050 struct topo_node *node, *sink_node = NULL;
3051 HRESULT hr;
3053 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
3055 if (node->type == MF_TOPOLOGY_OUTPUT_NODE && node->object.sink_stream == sink)
3057 sink_node = node;
3058 break;
3062 if (!sink_node)
3063 return;
3065 if (!event)
3067 if (FAILED(hr = MFCreateMediaEvent(MESinkInvalidated, &GUID_NULL, S_OK, NULL, &event)))
3068 WARN("Failed to create event, hr %#x.\n", hr);
3071 if (!event)
3072 return;
3074 IMFMediaEvent_SetUINT64(event, &MF_EVENT_OUTPUT_NODE, sink_node->node_id);
3075 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
3077 session_set_topo_status(session, S_OK, MF_TOPOSTATUS_ENDED);
3080 static BOOL session_nodes_is_mask_set(struct media_session *session, MF_TOPOLOGY_TYPE node_type, unsigned int flags)
3082 struct media_source *source;
3083 struct topo_node *node;
3085 if (node_type == MF_TOPOLOGY_MAX)
3087 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
3089 if ((source->flags & flags) != flags)
3090 return FALSE;
3093 else
3095 LIST_FOR_EACH_ENTRY(node, &session->presentation.nodes, struct topo_node, entry)
3097 if (node->type == node_type && (node->flags & flags) != flags)
3098 return FALSE;
3102 return TRUE;
3105 static void session_raise_end_of_presentation(struct media_session *session)
3107 if (!(session_nodes_is_mask_set(session, MF_TOPOLOGY_SOURCESTREAM_NODE, TOPO_NODE_END_OF_STREAM)))
3108 return;
3110 if (!(session->presentation.flags & SESSION_FLAG_END_OF_PRESENTATION))
3112 if (session_nodes_is_mask_set(session, MF_TOPOLOGY_MAX, SOURCE_FLAG_END_OF_PRESENTATION))
3114 session->presentation.flags |= SESSION_FLAG_END_OF_PRESENTATION;
3115 session_push_back_command(session, SESSION_CMD_END);
3116 IMFMediaEventQueue_QueueEventParamVar(session->event_queue, MEEndOfPresentation, &GUID_NULL, S_OK, NULL);
3121 static void session_handle_end_of_stream(struct media_session *session, IMFMediaStream *stream)
3123 struct topo_node *node;
3125 if (!(node = session_get_node_object(session, (IUnknown *)stream, MF_TOPOLOGY_SOURCESTREAM_NODE))
3126 || node->flags & TOPO_NODE_END_OF_STREAM)
3128 return;
3131 session_deliver_sample(session, stream, NULL);
3133 session_raise_end_of_presentation(session);
3136 static void session_handle_end_of_presentation(struct media_session *session, IMFMediaSource *object)
3138 struct media_source *source;
3140 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
3142 if (source->source == object)
3144 if (!(source->flags & SOURCE_FLAG_END_OF_PRESENTATION))
3146 source->flags |= SOURCE_FLAG_END_OF_PRESENTATION;
3147 session_raise_end_of_presentation(session);
3150 break;
3155 static void session_sink_stream_marker(struct media_session *session, IMFStreamSink *stream_sink)
3157 struct topo_node *node;
3159 if (!(node = session_get_node_object(session, (IUnknown *)stream_sink, MF_TOPOLOGY_OUTPUT_NODE))
3160 || node->flags & TOPO_NODE_END_OF_STREAM)
3162 return;
3165 node->flags |= TOPO_NODE_END_OF_STREAM;
3167 if (session->presentation.flags & SESSION_FLAG_END_OF_PRESENTATION &&
3168 session_nodes_is_mask_set(session, MF_TOPOLOGY_OUTPUT_NODE, TOPO_NODE_END_OF_STREAM))
3170 session_set_topo_status(session, S_OK, MF_TOPOSTATUS_ENDED);
3171 session_set_caps(session, session->caps & ~MFSESSIONCAP_PAUSE);
3172 session_stop(session);
3176 static HRESULT WINAPI session_events_callback_Invoke(IMFAsyncCallback *iface, IMFAsyncResult *result)
3178 struct media_session *session = impl_from_events_callback_IMFAsyncCallback(iface);
3179 IMFMediaEventGenerator *event_source;
3180 IMFMediaEvent *event = NULL;
3181 MediaEventType event_type;
3182 IUnknown *object = NULL;
3183 IMFMediaSource *source;
3184 IMFMediaStream *stream;
3185 PROPVARIANT value;
3186 HRESULT hr;
3188 if (FAILED(hr = IMFAsyncResult_GetState(result, (IUnknown **)&event_source)))
3189 return hr;
3191 if (FAILED(hr = IMFMediaEventGenerator_EndGetEvent(event_source, result, &event)))
3193 WARN("Failed to get event from %p, hr %#x.\n", event_source, hr);
3194 goto failed;
3197 if (FAILED(hr = IMFMediaEvent_GetType(event, &event_type)))
3199 WARN("Failed to get event type, hr %#x.\n", hr);
3200 goto failed;
3203 value.vt = VT_EMPTY;
3204 if (FAILED(hr = IMFMediaEvent_GetValue(event, &value)))
3206 WARN("Failed to get event value, hr %#x.\n", hr);
3207 goto failed;
3210 switch (event_type)
3212 case MESourceStarted:
3213 case MESourcePaused:
3214 case MESourceStopped:
3215 case MEStreamStarted:
3216 case MEStreamPaused:
3217 case MEStreamStopped:
3219 EnterCriticalSection(&session->cs);
3220 session_set_source_object_state(session, (IUnknown *)event_source, event_type);
3221 LeaveCriticalSection(&session->cs);
3223 break;
3225 case MEBufferingStarted:
3226 case MEBufferingStopped:
3228 EnterCriticalSection(&session->cs);
3229 if (session_get_media_source(session, (IMFMediaSource *)event_source))
3231 if (event_type == MEBufferingStarted)
3232 IMFPresentationClock_Pause(session->clock);
3233 else
3234 IMFPresentationClock_Start(session->clock, PRESENTATION_CURRENT_POSITION);
3236 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
3238 LeaveCriticalSection(&session->cs);
3239 break;
3241 case MEReconnectStart:
3242 case MEReconnectEnd:
3244 EnterCriticalSection(&session->cs);
3245 if (session_get_media_source(session, (IMFMediaSource *)event_source))
3246 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
3247 LeaveCriticalSection(&session->cs);
3248 break;
3250 case MEExtendedType:
3251 case MERendererEvent:
3252 case MEStreamSinkFormatChanged:
3254 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
3255 break;
3257 case MENewStream:
3258 stream = (IMFMediaStream *)value.punkVal;
3260 if (value.vt != VT_UNKNOWN || !stream)
3262 WARN("Unexpected event value.\n");
3263 break;
3266 if (FAILED(hr = IMFMediaStream_GetMediaSource(stream, &source)))
3267 break;
3269 EnterCriticalSection(&session->cs);
3270 if (SUCCEEDED(hr = session_add_media_stream(session, source, stream)))
3271 hr = IMFMediaStream_BeginGetEvent(stream, &session->events_callback, (IUnknown *)stream);
3272 LeaveCriticalSection(&session->cs);
3274 IMFMediaSource_Release(source);
3276 break;
3277 case MEStreamSinkStarted:
3278 case MEStreamSinkPaused:
3279 case MEStreamSinkStopped:
3280 case MEStreamSinkPrerolled:
3282 EnterCriticalSection(&session->cs);
3283 session_set_sink_stream_state(session, (IMFStreamSink *)event_source, event_type);
3284 LeaveCriticalSection(&session->cs);
3286 break;
3287 case MEStreamSinkMarker:
3289 EnterCriticalSection(&session->cs);
3290 session_sink_stream_marker(session, (IMFStreamSink *)event_source);
3291 LeaveCriticalSection(&session->cs);
3293 break;
3294 case MEStreamSinkRequestSample:
3296 EnterCriticalSection(&session->cs);
3297 session_request_sample(session, (IMFStreamSink *)event_source);
3298 LeaveCriticalSection(&session->cs);
3300 break;
3301 case MEMediaSample:
3303 EnterCriticalSection(&session->cs);
3304 session_deliver_sample(session, (IMFMediaStream *)event_source, &value);
3305 LeaveCriticalSection(&session->cs);
3307 break;
3308 case MEEndOfStream:
3310 EnterCriticalSection(&session->cs);
3311 session_handle_end_of_stream(session, (IMFMediaStream *)event_source);
3312 LeaveCriticalSection(&session->cs);
3314 break;
3316 case MEEndOfPresentation:
3318 EnterCriticalSection(&session->cs);
3319 session_handle_end_of_presentation(session, (IMFMediaSource *)event_source);
3320 LeaveCriticalSection(&session->cs);
3322 break;
3323 case MEAudioSessionGroupingParamChanged:
3324 case MEAudioSessionIconChanged:
3325 case MEAudioSessionNameChanged:
3326 case MEAudioSessionVolumeChanged:
3328 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
3330 break;
3331 case MEAudioSessionDeviceRemoved:
3332 case MEAudioSessionDisconnected:
3333 case MEAudioSessionExclusiveModeOverride:
3334 case MEAudioSessionFormatChanged:
3335 case MEAudioSessionServerShutdown:
3337 IMFMediaEventQueue_QueueEvent(session->event_queue, event);
3338 /* fallthrough */
3339 case MESinkInvalidated:
3341 EnterCriticalSection(&session->cs);
3342 session_sink_invalidated(session, event_type == MESinkInvalidated ? event : NULL,
3343 (IMFStreamSink *)event_source);
3344 LeaveCriticalSection(&session->cs);
3346 break;
3347 case MEQualityNotify:
3349 if (session->quality_manager)
3351 if (FAILED(IMFMediaEventGenerator_QueryInterface(event_source, &IID_IMFStreamSink, (void **)&object)))
3352 IMFMediaEventGenerator_QueryInterface(event_source, &IID_IMFTransform, (void **)&object);
3354 if (object)
3356 IMFQualityManager_NotifyQualityEvent(session->quality_manager, object, event);
3357 IUnknown_Release(object);
3361 break;
3362 default:
3366 PropVariantClear(&value);
3368 failed:
3369 if (event)
3370 IMFMediaEvent_Release(event);
3372 if (FAILED(hr = IMFMediaEventGenerator_BeginGetEvent(event_source, iface, (IUnknown *)event_source)))
3373 WARN("Failed to re-subscribe, hr %#x.\n", hr);
3375 IMFMediaEventGenerator_Release(event_source);
3377 return hr;
3380 static const IMFAsyncCallbackVtbl session_events_callback_vtbl =
3382 session_events_callback_QueryInterface,
3383 session_events_callback_AddRef,
3384 session_events_callback_Release,
3385 session_events_callback_GetParameters,
3386 session_events_callback_Invoke,
3389 static HRESULT WINAPI session_sink_finalizer_callback_QueryInterface(IMFAsyncCallback *iface, REFIID riid, void **obj)
3391 if (IsEqualIID(riid, &IID_IMFAsyncCallback) ||
3392 IsEqualIID(riid, &IID_IUnknown))
3394 *obj = iface;
3395 IMFAsyncCallback_AddRef(iface);
3396 return S_OK;
3399 WARN("Unsupported %s.\n", debugstr_guid(riid));
3400 *obj = NULL;
3401 return E_NOINTERFACE;
3404 static ULONG WINAPI session_sink_finalizer_callback_AddRef(IMFAsyncCallback *iface)
3406 struct media_session *session = impl_from_sink_finalizer_callback_IMFAsyncCallback(iface);
3407 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
3410 static ULONG WINAPI session_sink_finalizer_callback_Release(IMFAsyncCallback *iface)
3412 struct media_session *session = impl_from_sink_finalizer_callback_IMFAsyncCallback(iface);
3413 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
3416 static HRESULT WINAPI session_sink_finalizer_callback_GetParameters(IMFAsyncCallback *iface, DWORD *flags, DWORD *queue)
3418 return E_NOTIMPL;
3421 static HRESULT WINAPI session_sink_finalizer_callback_Invoke(IMFAsyncCallback *iface, IMFAsyncResult *result)
3423 struct media_session *session = impl_from_sink_finalizer_callback_IMFAsyncCallback(iface);
3424 IMFFinalizableMediaSink *fin_sink = NULL;
3425 BOOL sinks_finalized = TRUE;
3426 struct media_sink *sink;
3427 IUnknown *state;
3428 HRESULT hr;
3430 if (FAILED(hr = IMFAsyncResult_GetState(result, &state)))
3431 return hr;
3433 EnterCriticalSection(&session->cs);
3435 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
3437 if (state == (IUnknown *)sink->sink)
3439 if (FAILED(hr = IMFMediaSink_QueryInterface(sink->sink, &IID_IMFFinalizableMediaSink, (void **)&fin_sink)))
3440 WARN("Unexpected, missing IMFFinalizableSink, hr %#x.\n", hr);
3442 else
3444 sinks_finalized &= sink->finalized;
3445 if (!sinks_finalized)
3446 break;
3450 IUnknown_Release(state);
3452 if (fin_sink)
3454 /* Complete session transition, or close prematurely on error. */
3455 if (SUCCEEDED(hr = IMFFinalizableMediaSink_EndFinalize(fin_sink, result)))
3457 sink->finalized = TRUE;
3458 if (sinks_finalized)
3459 session_set_closed(session, hr);
3461 IMFFinalizableMediaSink_Release(fin_sink);
3464 LeaveCriticalSection(&session->cs);
3466 return S_OK;
3469 static const IMFAsyncCallbackVtbl session_sink_finalizer_callback_vtbl =
3471 session_sink_finalizer_callback_QueryInterface,
3472 session_sink_finalizer_callback_AddRef,
3473 session_sink_finalizer_callback_Release,
3474 session_sink_finalizer_callback_GetParameters,
3475 session_sink_finalizer_callback_Invoke,
3478 static HRESULT WINAPI session_rate_support_QueryInterface(IMFRateSupport *iface, REFIID riid, void **obj)
3480 struct media_session *session = impl_session_from_IMFRateSupport(iface);
3481 return IMFMediaSession_QueryInterface(&session->IMFMediaSession_iface, riid, obj);
3484 static ULONG WINAPI session_rate_support_AddRef(IMFRateSupport *iface)
3486 struct media_session *session = impl_session_from_IMFRateSupport(iface);
3487 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
3490 static ULONG WINAPI session_rate_support_Release(IMFRateSupport *iface)
3492 struct media_session *session = impl_session_from_IMFRateSupport(iface);
3493 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
3496 static HRESULT session_presentation_object_get_rate(IUnknown *object, MFRATE_DIRECTION direction,
3497 BOOL thin, BOOL fastest, float *result)
3499 IMFRateSupport *rate_support;
3500 float rate;
3501 HRESULT hr;
3503 /* For objects that don't support rate control, it's assumed that only forward direction is allowed, at 1.0f. */
3505 if (FAILED(hr = MFGetService(object, &MF_RATE_CONTROL_SERVICE, &IID_IMFRateSupport, (void **)&rate_support)))
3507 if (direction == MFRATE_FORWARD)
3509 *result = 1.0f;
3510 return S_OK;
3512 else
3513 return MF_E_REVERSE_UNSUPPORTED;
3516 rate = 0.0f;
3517 if (fastest)
3519 if (SUCCEEDED(hr = IMFRateSupport_GetFastestRate(rate_support, direction, thin, &rate)))
3520 *result = min(fabsf(rate), *result);
3522 else
3524 if (SUCCEEDED(hr = IMFRateSupport_GetSlowestRate(rate_support, direction, thin, &rate)))
3525 *result = max(fabsf(rate), *result);
3528 IMFRateSupport_Release(rate_support);
3530 return hr;
3533 static HRESULT session_get_presentation_rate(struct media_session *session, MFRATE_DIRECTION direction,
3534 BOOL thin, BOOL fastest, float *result)
3536 struct media_source *source;
3537 struct media_sink *sink;
3538 HRESULT hr = E_POINTER;
3540 *result = 0.0f;
3542 EnterCriticalSection(&session->cs);
3544 if (session->presentation.topo_status != MF_TOPOSTATUS_INVALID)
3546 LIST_FOR_EACH_ENTRY(source, &session->presentation.sources, struct media_source, entry)
3548 if (FAILED(hr = session_presentation_object_get_rate((IUnknown *)source->source, direction, thin, fastest, result)))
3549 break;
3552 if (SUCCEEDED(hr))
3554 LIST_FOR_EACH_ENTRY(sink, &session->presentation.sinks, struct media_sink, entry)
3556 if (FAILED(hr = session_presentation_object_get_rate((IUnknown *)sink->sink, direction, thin, fastest, result)))
3557 break;
3562 LeaveCriticalSection(&session->cs);
3564 return hr;
3567 static HRESULT WINAPI session_rate_support_GetSlowestRate(IMFRateSupport *iface, MFRATE_DIRECTION direction,
3568 BOOL thin, float *rate)
3570 struct media_session *session = impl_session_from_IMFRateSupport(iface);
3572 TRACE("%p, %d, %d, %p.\n", iface, direction, thin, rate);
3574 return session_get_presentation_rate(session, direction, thin, FALSE, rate);
3577 static HRESULT WINAPI session_rate_support_GetFastestRate(IMFRateSupport *iface, MFRATE_DIRECTION direction,
3578 BOOL thin, float *rate)
3580 struct media_session *session = impl_session_from_IMFRateSupport(iface);
3582 TRACE("%p, %d, %d, %p.\n", iface, direction, thin, rate);
3584 return session_get_presentation_rate(session, direction, thin, TRUE, rate);
3587 static HRESULT WINAPI session_rate_support_IsRateSupported(IMFRateSupport *iface, BOOL thin, float rate,
3588 float *nearest_supported_rate)
3590 FIXME("%p, %d, %f, %p.\n", iface, thin, rate, nearest_supported_rate);
3592 return E_NOTIMPL;
3595 static const IMFRateSupportVtbl session_rate_support_vtbl =
3597 session_rate_support_QueryInterface,
3598 session_rate_support_AddRef,
3599 session_rate_support_Release,
3600 session_rate_support_GetSlowestRate,
3601 session_rate_support_GetFastestRate,
3602 session_rate_support_IsRateSupported,
3605 static HRESULT WINAPI session_rate_control_QueryInterface(IMFRateControl *iface, REFIID riid, void **obj)
3607 struct media_session *session = impl_session_from_IMFRateControl(iface);
3608 return IMFMediaSession_QueryInterface(&session->IMFMediaSession_iface, riid, obj);
3611 static ULONG WINAPI session_rate_control_AddRef(IMFRateControl *iface)
3613 struct media_session *session = impl_session_from_IMFRateControl(iface);
3614 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
3617 static ULONG WINAPI session_rate_control_Release(IMFRateControl *iface)
3619 struct media_session *session = impl_session_from_IMFRateControl(iface);
3620 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
3623 static HRESULT WINAPI session_rate_control_SetRate(IMFRateControl *iface, BOOL thin, float rate)
3625 FIXME("%p, %d, %f.\n", iface, thin, rate);
3627 return E_NOTIMPL;
3630 static HRESULT WINAPI session_rate_control_GetRate(IMFRateControl *iface, BOOL *thin, float *rate)
3632 struct media_session *session = impl_session_from_IMFRateControl(iface);
3634 TRACE("%p, %p, %p.\n", iface, thin, rate);
3636 return IMFRateControl_GetRate(session->clock_rate_control, thin, rate);
3639 static const IMFRateControlVtbl session_rate_control_vtbl =
3641 session_rate_control_QueryInterface,
3642 session_rate_control_AddRef,
3643 session_rate_control_Release,
3644 session_rate_control_SetRate,
3645 session_rate_control_GetRate,
3648 static HRESULT WINAPI node_attribute_editor_QueryInterface(IMFTopologyNodeAttributeEditor *iface,
3649 REFIID riid, void **obj)
3651 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), obj);
3653 if (IsEqualIID(riid, &IID_IMFTopologyNodeAttributeEditor) ||
3654 IsEqualIID(riid, &IID_IUnknown))
3656 *obj = iface;
3657 IMFTopologyNodeAttributeEditor_AddRef(iface);
3658 return S_OK;
3661 WARN("Unsupported interface %s.\n", debugstr_guid(riid));
3662 *obj = NULL;
3663 return E_NOINTERFACE;
3666 static ULONG WINAPI node_attribute_editor_AddRef(IMFTopologyNodeAttributeEditor *iface)
3668 struct media_session *session = impl_session_from_IMFTopologyNodeAttributeEditor(iface);
3669 return IMFMediaSession_AddRef(&session->IMFMediaSession_iface);
3672 static ULONG WINAPI node_attribute_editor_Release(IMFTopologyNodeAttributeEditor *iface)
3674 struct media_session *session = impl_session_from_IMFTopologyNodeAttributeEditor(iface);
3675 return IMFMediaSession_Release(&session->IMFMediaSession_iface);
3678 static HRESULT WINAPI node_attribute_editor_UpdateNodeAttributes(IMFTopologyNodeAttributeEditor *iface,
3679 TOPOID id, DWORD count, MFTOPONODE_ATTRIBUTE_UPDATE *updates)
3681 FIXME("%p, %s, %u, %p.\n", iface, wine_dbgstr_longlong(id), count, updates);
3683 return E_NOTIMPL;
3686 static const IMFTopologyNodeAttributeEditorVtbl node_attribute_editor_vtbl =
3688 node_attribute_editor_QueryInterface,
3689 node_attribute_editor_AddRef,
3690 node_attribute_editor_Release,
3691 node_attribute_editor_UpdateNodeAttributes,
3694 /***********************************************************************
3695 * MFCreateMediaSession (mf.@)
3697 HRESULT WINAPI MFCreateMediaSession(IMFAttributes *config, IMFMediaSession **session)
3699 BOOL without_quality_manager = FALSE;
3700 struct media_session *object;
3701 HRESULT hr;
3703 TRACE("%p, %p.\n", config, session);
3705 object = heap_alloc_zero(sizeof(*object));
3706 if (!object)
3707 return E_OUTOFMEMORY;
3709 object->IMFMediaSession_iface.lpVtbl = &mfmediasessionvtbl;
3710 object->IMFGetService_iface.lpVtbl = &session_get_service_vtbl;
3711 object->IMFRateSupport_iface.lpVtbl = &session_rate_support_vtbl;
3712 object->IMFRateControl_iface.lpVtbl = &session_rate_control_vtbl;
3713 object->IMFTopologyNodeAttributeEditor_iface.lpVtbl = &node_attribute_editor_vtbl;
3714 object->commands_callback.lpVtbl = &session_commands_callback_vtbl;
3715 object->events_callback.lpVtbl = &session_events_callback_vtbl;
3716 object->sink_finalizer_callback.lpVtbl = &session_sink_finalizer_callback_vtbl;
3717 object->refcount = 1;
3718 list_init(&object->topologies);
3719 list_init(&object->commands);
3720 list_init(&object->presentation.sources);
3721 list_init(&object->presentation.sinks);
3722 list_init(&object->presentation.nodes);
3723 InitializeCriticalSection(&object->cs);
3725 if (FAILED(hr = MFCreateTopology(&object->presentation.current_topology)))
3726 goto failed;
3728 if (FAILED(hr = MFCreateEventQueue(&object->event_queue)))
3729 goto failed;
3731 if (FAILED(hr = MFCreatePresentationClock(&object->clock)))
3732 goto failed;
3734 if (FAILED(hr = MFCreateSystemTimeSource(&object->system_time_source)))
3735 goto failed;
3737 if (FAILED(hr = IMFPresentationClock_QueryInterface(object->clock, &IID_IMFRateControl,
3738 (void **)&object->clock_rate_control)))
3740 goto failed;
3743 if (config)
3745 GUID clsid;
3747 if (SUCCEEDED(IMFAttributes_GetGUID(config, &MF_SESSION_TOPOLOADER, &clsid)))
3749 if (FAILED(hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IMFTopoLoader,
3750 (void **)&object->topo_loader)))
3752 WARN("Failed to create custom topology loader, hr %#x.\n", hr);
3756 if (SUCCEEDED(IMFAttributes_GetGUID(config, &MF_SESSION_QUALITY_MANAGER, &clsid)))
3758 if (!(without_quality_manager = IsEqualGUID(&clsid, &GUID_NULL)))
3760 if (FAILED(hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IMFQualityManager,
3761 (void **)&object->quality_manager)))
3763 WARN("Failed to create custom quality manager, hr %#x.\n", hr);
3769 if (!object->topo_loader && FAILED(hr = MFCreateTopoLoader(&object->topo_loader)))
3770 goto failed;
3772 if (!object->quality_manager && !without_quality_manager &&
3773 FAILED(hr = MFCreateStandardQualityManager(&object->quality_manager)))
3775 goto failed;
3778 if (object->quality_manager && FAILED(hr = IMFQualityManager_NotifyPresentationClock(object->quality_manager,
3779 object->clock)))
3781 goto failed;
3784 *session = &object->IMFMediaSession_iface;
3786 return S_OK;
3788 failed:
3789 IMFMediaSession_Release(&object->IMFMediaSession_iface);
3790 return hr;
3793 static HRESULT WINAPI sink_notification_QueryInterface(IUnknown *iface, REFIID riid, void **out)
3795 if (IsEqualIID(riid, &IID_IUnknown))
3797 *out = iface;
3798 IUnknown_AddRef(iface);
3799 return S_OK;
3802 WARN("Unsupported %s.\n", debugstr_guid(riid));
3803 *out = NULL;
3804 return E_NOINTERFACE;
3807 static ULONG WINAPI sink_notification_AddRef(IUnknown *iface)
3809 struct sink_notification *notification = impl_sink_notification_from_IUnknown(iface);
3810 ULONG refcount = InterlockedIncrement(&notification->refcount);
3812 TRACE("%p, refcount %u.\n", iface, refcount);
3814 return refcount;
3817 static ULONG WINAPI sink_notification_Release(IUnknown *iface)
3819 struct sink_notification *notification = impl_sink_notification_from_IUnknown(iface);
3820 ULONG refcount = InterlockedDecrement(&notification->refcount);
3822 TRACE("%p, refcount %u.\n", iface, refcount);
3824 if (!refcount)
3826 IMFClockStateSink_Release(notification->sink);
3827 heap_free(notification);
3830 return refcount;
3833 static const IUnknownVtbl sinknotificationvtbl =
3835 sink_notification_QueryInterface,
3836 sink_notification_AddRef,
3837 sink_notification_Release,
3840 static void clock_notify_async_sink(struct presentation_clock *clock, MFTIME system_time,
3841 struct clock_state_change_param param, enum clock_notification notification, IMFClockStateSink *sink)
3843 struct sink_notification *object;
3844 IMFAsyncResult *result;
3845 HRESULT hr;
3847 object = heap_alloc(sizeof(*object));
3848 if (!object)
3849 return;
3851 object->IUnknown_iface.lpVtbl = &sinknotificationvtbl;
3852 object->refcount = 1;
3853 object->system_time = system_time;
3854 object->param = param;
3855 object->notification = notification;
3856 object->sink = sink;
3857 IMFClockStateSink_AddRef(object->sink);
3859 hr = MFCreateAsyncResult(&object->IUnknown_iface, &clock->sink_callback, NULL, &result);
3860 IUnknown_Release(&object->IUnknown_iface);
3861 if (SUCCEEDED(hr))
3863 MFPutWorkItemEx(MFASYNC_CALLBACK_QUEUE_STANDARD, result);
3864 IMFAsyncResult_Release(result);
3868 static HRESULT WINAPI present_clock_QueryInterface(IMFPresentationClock *iface, REFIID riid, void **out)
3870 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
3872 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
3874 if (IsEqualIID(riid, &IID_IMFPresentationClock) ||
3875 IsEqualIID(riid, &IID_IMFClock) ||
3876 IsEqualIID(riid, &IID_IUnknown))
3878 *out = &clock->IMFPresentationClock_iface;
3880 else if (IsEqualIID(riid, &IID_IMFRateControl))
3882 *out = &clock->IMFRateControl_iface;
3884 else if (IsEqualIID(riid, &IID_IMFTimer))
3886 *out = &clock->IMFTimer_iface;
3888 else if (IsEqualIID(riid, &IID_IMFShutdown))
3890 *out = &clock->IMFShutdown_iface;
3892 else
3894 WARN("Unsupported %s.\n", debugstr_guid(riid));
3895 *out = NULL;
3896 return E_NOINTERFACE;
3899 IUnknown_AddRef((IUnknown *)*out);
3900 return S_OK;
3903 static ULONG WINAPI present_clock_AddRef(IMFPresentationClock *iface)
3905 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
3906 ULONG refcount = InterlockedIncrement(&clock->refcount);
3908 TRACE("%p, refcount %u.\n", iface, refcount);
3910 return refcount;
3913 static ULONG WINAPI present_clock_Release(IMFPresentationClock *iface)
3915 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
3916 ULONG refcount = InterlockedDecrement(&clock->refcount);
3917 struct clock_timer *timer, *timer2;
3918 struct clock_sink *sink, *sink2;
3920 TRACE("%p, refcount %u.\n", iface, refcount);
3922 if (!refcount)
3924 if (clock->time_source)
3925 IMFPresentationTimeSource_Release(clock->time_source);
3926 if (clock->time_source_sink)
3927 IMFClockStateSink_Release(clock->time_source_sink);
3928 LIST_FOR_EACH_ENTRY_SAFE(sink, sink2, &clock->sinks, struct clock_sink, entry)
3930 list_remove(&sink->entry);
3931 IMFClockStateSink_Release(sink->state_sink);
3932 heap_free(sink);
3934 LIST_FOR_EACH_ENTRY_SAFE(timer, timer2, &clock->timers, struct clock_timer, entry)
3936 list_remove(&timer->entry);
3937 IUnknown_Release(&timer->IUnknown_iface);
3939 DeleteCriticalSection(&clock->cs);
3940 heap_free(clock);
3943 return refcount;
3946 static HRESULT WINAPI present_clock_GetClockCharacteristics(IMFPresentationClock *iface, DWORD *flags)
3948 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
3949 HRESULT hr = MF_E_CLOCK_NO_TIME_SOURCE;
3951 TRACE("%p, %p.\n", iface, flags);
3953 EnterCriticalSection(&clock->cs);
3954 if (clock->time_source)
3955 hr = IMFPresentationTimeSource_GetClockCharacteristics(clock->time_source, flags);
3956 LeaveCriticalSection(&clock->cs);
3958 return hr;
3961 static HRESULT WINAPI present_clock_GetCorrelatedTime(IMFPresentationClock *iface, DWORD reserved,
3962 LONGLONG *clock_time, MFTIME *system_time)
3964 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
3965 HRESULT hr = MF_E_CLOCK_NO_TIME_SOURCE;
3967 TRACE("%p, %#x, %p, %p.\n", iface, reserved, clock_time, system_time);
3969 EnterCriticalSection(&clock->cs);
3970 if (clock->time_source)
3971 hr = IMFPresentationTimeSource_GetCorrelatedTime(clock->time_source, reserved, clock_time, system_time);
3972 LeaveCriticalSection(&clock->cs);
3974 return hr;
3977 static HRESULT WINAPI present_clock_GetContinuityKey(IMFPresentationClock *iface, DWORD *key)
3979 TRACE("%p, %p.\n", iface, key);
3981 *key = 0;
3983 return S_OK;
3986 static HRESULT WINAPI present_clock_GetState(IMFPresentationClock *iface, DWORD reserved, MFCLOCK_STATE *state)
3988 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
3990 TRACE("%p, %#x, %p.\n", iface, reserved, state);
3992 EnterCriticalSection(&clock->cs);
3993 *state = clock->state;
3994 LeaveCriticalSection(&clock->cs);
3996 return S_OK;
3999 static HRESULT WINAPI present_clock_GetProperties(IMFPresentationClock *iface, MFCLOCK_PROPERTIES *props)
4001 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4002 HRESULT hr = MF_E_CLOCK_NO_TIME_SOURCE;
4004 TRACE("%p, %p.\n", iface, props);
4006 EnterCriticalSection(&clock->cs);
4007 if (clock->time_source)
4008 hr = IMFPresentationTimeSource_GetProperties(clock->time_source, props);
4009 LeaveCriticalSection(&clock->cs);
4011 return hr;
4014 static HRESULT WINAPI present_clock_SetTimeSource(IMFPresentationClock *iface,
4015 IMFPresentationTimeSource *time_source)
4017 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4018 MFCLOCK_PROPERTIES props;
4019 IMFClock *source_clock;
4020 HRESULT hr;
4022 TRACE("%p, %p.\n", iface, time_source);
4024 EnterCriticalSection(&clock->cs);
4026 if (clock->time_source)
4027 IMFPresentationTimeSource_Release(clock->time_source);
4028 if (clock->time_source_sink)
4029 IMFClockStateSink_Release(clock->time_source_sink);
4030 clock->time_source = NULL;
4031 clock->time_source_sink = NULL;
4033 hr = IMFPresentationTimeSource_QueryInterface(time_source, &IID_IMFClockStateSink, (void **)&clock->time_source_sink);
4034 if (SUCCEEDED(hr))
4036 clock->time_source = time_source;
4037 IMFPresentationTimeSource_AddRef(clock->time_source);
4040 if (SUCCEEDED(IMFPresentationTimeSource_GetUnderlyingClock(time_source, &source_clock)))
4042 if (SUCCEEDED(IMFClock_GetProperties(source_clock, &props)))
4043 clock->frequency = props.qwClockFrequency;
4044 IMFClock_Release(source_clock);
4047 if (!clock->frequency)
4048 clock->frequency = MFCLOCK_FREQUENCY_HNS;
4050 LeaveCriticalSection(&clock->cs);
4052 return hr;
4055 static HRESULT WINAPI present_clock_GetTimeSource(IMFPresentationClock *iface,
4056 IMFPresentationTimeSource **time_source)
4058 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4059 HRESULT hr = S_OK;
4061 TRACE("%p, %p.\n", iface, time_source);
4063 if (!time_source)
4064 return E_INVALIDARG;
4066 EnterCriticalSection(&clock->cs);
4067 if (clock->time_source)
4069 *time_source = clock->time_source;
4070 IMFPresentationTimeSource_AddRef(*time_source);
4072 else
4073 hr = MF_E_CLOCK_NO_TIME_SOURCE;
4074 LeaveCriticalSection(&clock->cs);
4076 return hr;
4079 static HRESULT WINAPI present_clock_GetTime(IMFPresentationClock *iface, MFTIME *time)
4081 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4082 HRESULT hr = MF_E_CLOCK_NO_TIME_SOURCE;
4083 MFTIME systime;
4085 TRACE("%p, %p.\n", iface, time);
4087 if (!time)
4088 return E_POINTER;
4090 EnterCriticalSection(&clock->cs);
4091 if (clock->time_source)
4092 hr = IMFPresentationTimeSource_GetCorrelatedTime(clock->time_source, 0, time, &systime);
4093 LeaveCriticalSection(&clock->cs);
4095 return hr;
4098 static HRESULT WINAPI present_clock_AddClockStateSink(IMFPresentationClock *iface, IMFClockStateSink *state_sink)
4100 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4101 struct clock_sink *sink, *cur;
4102 HRESULT hr = S_OK;
4104 TRACE("%p, %p.\n", iface, state_sink);
4106 if (!state_sink)
4107 return E_INVALIDARG;
4109 sink = heap_alloc(sizeof(*sink));
4110 if (!sink)
4111 return E_OUTOFMEMORY;
4113 sink->state_sink = state_sink;
4114 IMFClockStateSink_AddRef(sink->state_sink);
4116 EnterCriticalSection(&clock->cs);
4117 LIST_FOR_EACH_ENTRY(cur, &clock->sinks, struct clock_sink, entry)
4119 if (cur->state_sink == state_sink)
4121 hr = E_INVALIDARG;
4122 break;
4125 if (SUCCEEDED(hr))
4127 static const enum clock_notification notifications[MFCLOCK_STATE_PAUSED + 1] =
4129 /* MFCLOCK_STATE_INVALID */ 0, /* Does not apply */
4130 /* MFCLOCK_STATE_RUNNING */ CLOCK_NOTIFY_START,
4131 /* MFCLOCK_STATE_STOPPED */ CLOCK_NOTIFY_STOP,
4132 /* MFCLOCK_STATE_PAUSED */ CLOCK_NOTIFY_PAUSE,
4134 struct clock_state_change_param param;
4136 if (!clock->is_shut_down && clock->state != MFCLOCK_STATE_INVALID)
4138 param.u.offset = clock->start_offset;
4139 clock_notify_async_sink(clock, MFGetSystemTime(), param, notifications[clock->state], sink->state_sink);
4142 list_add_tail(&clock->sinks, &sink->entry);
4144 LeaveCriticalSection(&clock->cs);
4146 if (FAILED(hr))
4148 IMFClockStateSink_Release(sink->state_sink);
4149 heap_free(sink);
4152 return hr;
4155 static HRESULT WINAPI present_clock_RemoveClockStateSink(IMFPresentationClock *iface,
4156 IMFClockStateSink *state_sink)
4158 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4159 struct clock_sink *sink;
4161 TRACE("%p, %p.\n", iface, state_sink);
4163 if (!state_sink)
4164 return E_INVALIDARG;
4166 EnterCriticalSection(&clock->cs);
4167 LIST_FOR_EACH_ENTRY(sink, &clock->sinks, struct clock_sink, entry)
4169 if (sink->state_sink == state_sink)
4171 IMFClockStateSink_Release(sink->state_sink);
4172 list_remove(&sink->entry);
4173 heap_free(sink);
4174 break;
4177 LeaveCriticalSection(&clock->cs);
4179 return S_OK;
4182 static HRESULT clock_call_state_change(MFTIME system_time, struct clock_state_change_param param,
4183 enum clock_notification notification, IMFClockStateSink *sink)
4185 HRESULT hr = S_OK;
4187 switch (notification)
4189 case CLOCK_NOTIFY_START:
4190 hr = IMFClockStateSink_OnClockStart(sink, system_time, param.u.offset);
4191 break;
4192 case CLOCK_NOTIFY_STOP:
4193 hr = IMFClockStateSink_OnClockStop(sink, system_time);
4194 break;
4195 case CLOCK_NOTIFY_PAUSE:
4196 hr = IMFClockStateSink_OnClockPause(sink, system_time);
4197 break;
4198 case CLOCK_NOTIFY_RESTART:
4199 hr = IMFClockStateSink_OnClockRestart(sink, system_time);
4200 break;
4201 case CLOCK_NOTIFY_SET_RATE:
4202 /* System time source does not allow 0.0 rate, presentation clock allows it without raising errors. */
4203 IMFClockStateSink_OnClockSetRate(sink, system_time, param.u.rate);
4204 break;
4205 default:
4209 return hr;
4212 static HRESULT clock_change_state(struct presentation_clock *clock, enum clock_command command,
4213 struct clock_state_change_param param)
4215 static const BYTE state_change_is_allowed[MFCLOCK_STATE_PAUSED+1][CLOCK_CMD_MAX] =
4216 { /* S S* P, R */
4217 /* INVALID */ { 1, 1, 1, 1 },
4218 /* RUNNING */ { 1, 1, 1, 1 },
4219 /* STOPPED */ { 1, 1, 0, 1 },
4220 /* PAUSED */ { 1, 1, 0, 1 },
4222 static const MFCLOCK_STATE states[CLOCK_CMD_MAX] =
4224 /* CLOCK_CMD_START */ MFCLOCK_STATE_RUNNING,
4225 /* CLOCK_CMD_STOP */ MFCLOCK_STATE_STOPPED,
4226 /* CLOCK_CMD_PAUSE */ MFCLOCK_STATE_PAUSED,
4227 /* CLOCK_CMD_SET_RATE */ 0, /* Unused */
4229 static const enum clock_notification notifications[CLOCK_CMD_MAX] =
4231 /* CLOCK_CMD_START */ CLOCK_NOTIFY_START,
4232 /* CLOCK_CMD_STOP */ CLOCK_NOTIFY_STOP,
4233 /* CLOCK_CMD_PAUSE */ CLOCK_NOTIFY_PAUSE,
4234 /* CLOCK_CMD_SET_RATE */ CLOCK_NOTIFY_SET_RATE,
4236 enum clock_notification notification;
4237 struct clock_sink *sink;
4238 MFCLOCK_STATE old_state;
4239 IMFAsyncResult *result;
4240 MFTIME system_time;
4241 HRESULT hr;
4243 if (!clock->time_source)
4244 return MF_E_CLOCK_NO_TIME_SOURCE;
4246 if (command != CLOCK_CMD_SET_RATE && clock->state == states[command] && clock->state != MFCLOCK_STATE_RUNNING)
4247 return MF_E_CLOCK_STATE_ALREADY_SET;
4249 if (!state_change_is_allowed[clock->state][command])
4250 return MF_E_INVALIDREQUEST;
4252 system_time = MFGetSystemTime();
4254 if (command == CLOCK_CMD_START && clock->state == MFCLOCK_STATE_PAUSED &&
4255 param.u.offset == PRESENTATION_CURRENT_POSITION)
4257 notification = CLOCK_NOTIFY_RESTART;
4259 else
4260 notification = notifications[command];
4262 if (FAILED(hr = clock_call_state_change(system_time, param, notification, clock->time_source_sink)))
4263 return hr;
4265 old_state = clock->state;
4266 if (command != CLOCK_CMD_SET_RATE)
4267 clock->state = states[command];
4269 /* Dump all pending timer requests immediately on start; otherwise try to cancel scheduled items when
4270 transitioning from running state. */
4271 if ((clock->state == MFCLOCK_STATE_RUNNING) ^ (old_state == MFCLOCK_STATE_RUNNING))
4273 struct clock_timer *timer, *timer2;
4275 if (clock->state == MFCLOCK_STATE_RUNNING)
4277 LIST_FOR_EACH_ENTRY_SAFE(timer, timer2, &clock->timers, struct clock_timer, entry)
4279 list_remove(&timer->entry);
4280 hr = MFCreateAsyncResult(&timer->IUnknown_iface, &clock->timer_callback, NULL, &result);
4281 IUnknown_Release(&timer->IUnknown_iface);
4282 if (SUCCEEDED(hr))
4284 MFPutWorkItemEx(MFASYNC_CALLBACK_QUEUE_TIMER, result);
4285 IMFAsyncResult_Release(result);
4289 else
4291 LIST_FOR_EACH_ENTRY(timer, &clock->timers, struct clock_timer, entry)
4293 if (timer->key)
4295 MFCancelWorkItem(timer->key);
4296 timer->key = 0;
4302 LIST_FOR_EACH_ENTRY(sink, &clock->sinks, struct clock_sink, entry)
4304 clock_notify_async_sink(clock, system_time, param, notification, sink->state_sink);
4307 return S_OK;
4310 static HRESULT WINAPI present_clock_Start(IMFPresentationClock *iface, LONGLONG start_offset)
4312 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4313 struct clock_state_change_param param = {{0}};
4314 HRESULT hr;
4316 TRACE("%p, %s.\n", iface, debugstr_time(start_offset));
4318 EnterCriticalSection(&clock->cs);
4319 clock->start_offset = param.u.offset = start_offset;
4320 hr = clock_change_state(clock, CLOCK_CMD_START, param);
4321 LeaveCriticalSection(&clock->cs);
4323 return hr;
4326 static HRESULT WINAPI present_clock_Stop(IMFPresentationClock *iface)
4328 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4329 struct clock_state_change_param param = {{0}};
4330 HRESULT hr;
4332 TRACE("%p.\n", iface);
4334 EnterCriticalSection(&clock->cs);
4335 hr = clock_change_state(clock, CLOCK_CMD_STOP, param);
4336 LeaveCriticalSection(&clock->cs);
4338 return hr;
4341 static HRESULT WINAPI present_clock_Pause(IMFPresentationClock *iface)
4343 struct presentation_clock *clock = impl_from_IMFPresentationClock(iface);
4344 struct clock_state_change_param param = {{0}};
4345 HRESULT hr;
4347 TRACE("%p.\n", iface);
4349 EnterCriticalSection(&clock->cs);
4350 hr = clock_change_state(clock, CLOCK_CMD_PAUSE, param);
4351 LeaveCriticalSection(&clock->cs);
4353 return hr;
4356 static const IMFPresentationClockVtbl presentationclockvtbl =
4358 present_clock_QueryInterface,
4359 present_clock_AddRef,
4360 present_clock_Release,
4361 present_clock_GetClockCharacteristics,
4362 present_clock_GetCorrelatedTime,
4363 present_clock_GetContinuityKey,
4364 present_clock_GetState,
4365 present_clock_GetProperties,
4366 present_clock_SetTimeSource,
4367 present_clock_GetTimeSource,
4368 present_clock_GetTime,
4369 present_clock_AddClockStateSink,
4370 present_clock_RemoveClockStateSink,
4371 present_clock_Start,
4372 present_clock_Stop,
4373 present_clock_Pause,
4376 static HRESULT WINAPI present_clock_rate_control_QueryInterface(IMFRateControl *iface, REFIID riid, void **out)
4378 struct presentation_clock *clock = impl_from_IMFRateControl(iface);
4379 return IMFPresentationClock_QueryInterface(&clock->IMFPresentationClock_iface, riid, out);
4382 static ULONG WINAPI present_clock_rate_control_AddRef(IMFRateControl *iface)
4384 struct presentation_clock *clock = impl_from_IMFRateControl(iface);
4385 return IMFPresentationClock_AddRef(&clock->IMFPresentationClock_iface);
4388 static ULONG WINAPI present_clock_rate_control_Release(IMFRateControl *iface)
4390 struct presentation_clock *clock = impl_from_IMFRateControl(iface);
4391 return IMFPresentationClock_Release(&clock->IMFPresentationClock_iface);
4394 static HRESULT WINAPI present_clock_rate_SetRate(IMFRateControl *iface, BOOL thin, float rate)
4396 struct presentation_clock *clock = impl_from_IMFRateControl(iface);
4397 struct clock_state_change_param param;
4398 HRESULT hr;
4400 TRACE("%p, %d, %f.\n", iface, thin, rate);
4402 if (thin)
4403 return MF_E_THINNING_UNSUPPORTED;
4405 EnterCriticalSection(&clock->cs);
4406 param.u.rate = rate;
4407 if (SUCCEEDED(hr = clock_change_state(clock, CLOCK_CMD_SET_RATE, param)))
4408 clock->rate = rate;
4409 LeaveCriticalSection(&clock->cs);
4411 return hr;
4414 static HRESULT WINAPI present_clock_rate_GetRate(IMFRateControl *iface, BOOL *thin, float *rate)
4416 struct presentation_clock *clock = impl_from_IMFRateControl(iface);
4418 TRACE("%p, %p, %p.\n", iface, thin, rate);
4420 if (!rate)
4421 return E_INVALIDARG;
4423 if (thin)
4424 *thin = FALSE;
4426 EnterCriticalSection(&clock->cs);
4427 *rate = clock->rate;
4428 LeaveCriticalSection(&clock->cs);
4430 return S_OK;
4433 static const IMFRateControlVtbl presentclockratecontrolvtbl =
4435 present_clock_rate_control_QueryInterface,
4436 present_clock_rate_control_AddRef,
4437 present_clock_rate_control_Release,
4438 present_clock_rate_SetRate,
4439 present_clock_rate_GetRate,
4442 static HRESULT WINAPI present_clock_timer_QueryInterface(IMFTimer *iface, REFIID riid, void **out)
4444 struct presentation_clock *clock = impl_from_IMFTimer(iface);
4445 return IMFPresentationClock_QueryInterface(&clock->IMFPresentationClock_iface, riid, out);
4448 static ULONG WINAPI present_clock_timer_AddRef(IMFTimer *iface)
4450 struct presentation_clock *clock = impl_from_IMFTimer(iface);
4451 return IMFPresentationClock_AddRef(&clock->IMFPresentationClock_iface);
4454 static ULONG WINAPI present_clock_timer_Release(IMFTimer *iface)
4456 struct presentation_clock *clock = impl_from_IMFTimer(iface);
4457 return IMFPresentationClock_Release(&clock->IMFPresentationClock_iface);
4460 static HRESULT present_clock_schedule_timer(struct presentation_clock *clock, DWORD flags, LONGLONG time,
4461 struct clock_timer *timer)
4463 IMFAsyncResult *result;
4464 MFTIME systime, clocktime;
4465 LONGLONG frequency;
4466 HRESULT hr;
4468 if (!(flags & MFTIMER_RELATIVE))
4470 if (FAILED(hr = IMFPresentationTimeSource_GetCorrelatedTime(clock->time_source, 0, &clocktime, &systime)))
4472 WARN("Failed to get clock time, hr %#x.\n", hr);
4473 return hr;
4475 time -= clocktime;
4478 frequency = clock->frequency / 1000;
4479 time /= frequency;
4481 /* Scheduled item is using clock instance callback, with timer instance as an object. Clock callback will
4482 call user callback and cleanup timer list. */
4484 if (FAILED(hr = MFCreateAsyncResult(&timer->IUnknown_iface, &clock->timer_callback, NULL, &result)))
4485 return hr;
4487 hr = MFScheduleWorkItemEx(result, -time, &timer->key);
4488 IMFAsyncResult_Release(result);
4490 return hr;
4493 static HRESULT WINAPI clock_timer_QueryInterface(IUnknown *iface, REFIID riid, void **obj)
4495 if (IsEqualIID(riid, &IID_IUnknown))
4497 *obj = iface;
4498 IUnknown_AddRef(iface);
4499 return S_OK;
4502 *obj = NULL;
4503 return E_NOINTERFACE;
4506 static ULONG WINAPI clock_timer_AddRef(IUnknown *iface)
4508 struct clock_timer *timer = impl_clock_timer_from_IUnknown(iface);
4509 return InterlockedIncrement(&timer->refcount);
4512 static ULONG WINAPI clock_timer_Release(IUnknown *iface)
4514 struct clock_timer *timer = impl_clock_timer_from_IUnknown(iface);
4515 ULONG refcount = InterlockedDecrement(&timer->refcount);
4517 if (!refcount)
4519 IMFAsyncResult_Release(timer->result);
4520 IMFAsyncCallback_Release(timer->callback);
4521 heap_free(timer);
4524 return refcount;
4527 static const IUnknownVtbl clock_timer_vtbl =
4529 clock_timer_QueryInterface,
4530 clock_timer_AddRef,
4531 clock_timer_Release,
4534 static HRESULT WINAPI present_clock_timer_SetTimer(IMFTimer *iface, DWORD flags, LONGLONG time,
4535 IMFAsyncCallback *callback, IUnknown *state, IUnknown **cancel_key)
4537 struct presentation_clock *clock = impl_from_IMFTimer(iface);
4538 struct clock_timer *clock_timer;
4539 HRESULT hr;
4541 TRACE("%p, %#x, %s, %p, %p, %p.\n", iface, flags, debugstr_time(time), callback, state, cancel_key);
4543 if (!(clock_timer = heap_alloc_zero(sizeof(*clock_timer))))
4544 return E_OUTOFMEMORY;
4546 if (FAILED(hr = MFCreateAsyncResult(NULL, NULL, state, &clock_timer->result)))
4548 heap_free(clock_timer);
4549 return hr;
4552 clock_timer->IUnknown_iface.lpVtbl = &clock_timer_vtbl;
4553 clock_timer->refcount = 1;
4554 clock_timer->callback = callback;
4555 IMFAsyncCallback_AddRef(clock_timer->callback);
4557 EnterCriticalSection(&clock->cs);
4559 if (clock->state == MFCLOCK_STATE_RUNNING)
4560 hr = present_clock_schedule_timer(clock, flags, time, clock_timer);
4561 else if (clock->state == MFCLOCK_STATE_STOPPED)
4562 hr = MF_S_CLOCK_STOPPED;
4564 if (SUCCEEDED(hr))
4566 list_add_tail(&clock->timers, &clock_timer->entry);
4567 if (cancel_key)
4569 *cancel_key = &clock_timer->IUnknown_iface;
4570 IUnknown_AddRef(*cancel_key);
4574 LeaveCriticalSection(&clock->cs);
4576 if (FAILED(hr))
4577 IUnknown_Release(&clock_timer->IUnknown_iface);
4579 return hr;
4582 static HRESULT WINAPI present_clock_timer_CancelTimer(IMFTimer *iface, IUnknown *cancel_key)
4584 struct presentation_clock *clock = impl_from_IMFTimer(iface);
4585 struct clock_timer *timer;
4587 TRACE("%p, %p.\n", iface, cancel_key);
4589 EnterCriticalSection(&clock->cs);
4591 LIST_FOR_EACH_ENTRY(timer, &clock->timers, struct clock_timer, entry)
4593 if (&timer->IUnknown_iface == cancel_key)
4595 list_remove(&timer->entry);
4596 if (timer->key)
4598 MFCancelWorkItem(timer->key);
4599 timer->key = 0;
4601 IUnknown_Release(&timer->IUnknown_iface);
4602 break;
4606 LeaveCriticalSection(&clock->cs);
4608 return S_OK;
4611 static const IMFTimerVtbl presentclocktimervtbl =
4613 present_clock_timer_QueryInterface,
4614 present_clock_timer_AddRef,
4615 present_clock_timer_Release,
4616 present_clock_timer_SetTimer,
4617 present_clock_timer_CancelTimer,
4620 static HRESULT WINAPI present_clock_shutdown_QueryInterface(IMFShutdown *iface, REFIID riid, void **out)
4622 struct presentation_clock *clock = impl_from_IMFShutdown(iface);
4623 return IMFPresentationClock_QueryInterface(&clock->IMFPresentationClock_iface, riid, out);
4626 static ULONG WINAPI present_clock_shutdown_AddRef(IMFShutdown *iface)
4628 struct presentation_clock *clock = impl_from_IMFShutdown(iface);
4629 return IMFPresentationClock_AddRef(&clock->IMFPresentationClock_iface);
4632 static ULONG WINAPI present_clock_shutdown_Release(IMFShutdown *iface)
4634 struct presentation_clock *clock = impl_from_IMFShutdown(iface);
4635 return IMFPresentationClock_Release(&clock->IMFPresentationClock_iface);
4638 static HRESULT WINAPI present_clock_shutdown_Shutdown(IMFShutdown *iface)
4640 struct presentation_clock *clock = impl_from_IMFShutdown(iface);
4642 TRACE("%p.\n", iface);
4644 EnterCriticalSection(&clock->cs);
4645 clock->is_shut_down = TRUE;
4646 LeaveCriticalSection(&clock->cs);
4648 return S_OK;
4651 static HRESULT WINAPI present_clock_shutdown_GetShutdownStatus(IMFShutdown *iface, MFSHUTDOWN_STATUS *status)
4653 struct presentation_clock *clock = impl_from_IMFShutdown(iface);
4654 HRESULT hr = S_OK;
4656 TRACE("%p, %p.\n", iface, status);
4658 if (!status)
4659 return E_INVALIDARG;
4661 EnterCriticalSection(&clock->cs);
4662 if (clock->is_shut_down)
4663 *status = MFSHUTDOWN_COMPLETED;
4664 else
4665 hr = MF_E_INVALIDREQUEST;
4666 LeaveCriticalSection(&clock->cs);
4668 return hr;
4671 static const IMFShutdownVtbl presentclockshutdownvtbl =
4673 present_clock_shutdown_QueryInterface,
4674 present_clock_shutdown_AddRef,
4675 present_clock_shutdown_Release,
4676 present_clock_shutdown_Shutdown,
4677 present_clock_shutdown_GetShutdownStatus,
4680 static HRESULT WINAPI present_clock_callback_QueryInterface(IMFAsyncCallback *iface, REFIID riid, void **out)
4682 if (IsEqualIID(riid, &IID_IMFAsyncCallback) ||
4683 IsEqualIID(riid, &IID_IUnknown))
4685 *out = iface;
4686 IMFAsyncCallback_AddRef(iface);
4687 return S_OK;
4690 WARN("Unsupported %s.\n", wine_dbgstr_guid(riid));
4691 *out = NULL;
4692 return E_NOINTERFACE;
4695 static ULONG WINAPI present_clock_sink_callback_AddRef(IMFAsyncCallback *iface)
4697 struct presentation_clock *clock = impl_from_sink_callback_IMFAsyncCallback(iface);
4698 return IMFPresentationClock_AddRef(&clock->IMFPresentationClock_iface);
4701 static ULONG WINAPI present_clock_sink_callback_Release(IMFAsyncCallback *iface)
4703 struct presentation_clock *clock = impl_from_sink_callback_IMFAsyncCallback(iface);
4704 return IMFPresentationClock_Release(&clock->IMFPresentationClock_iface);
4707 static HRESULT WINAPI present_clock_callback_GetParameters(IMFAsyncCallback *iface, DWORD *flags, DWORD *queue)
4709 return E_NOTIMPL;
4712 static HRESULT WINAPI present_clock_sink_callback_Invoke(IMFAsyncCallback *iface, IMFAsyncResult *result)
4714 struct sink_notification *data;
4715 IUnknown *object;
4716 HRESULT hr;
4718 if (FAILED(hr = IMFAsyncResult_GetObject(result, &object)))
4719 return hr;
4721 data = impl_sink_notification_from_IUnknown(object);
4723 clock_call_state_change(data->system_time, data->param, data->notification, data->sink);
4725 IUnknown_Release(object);
4727 return S_OK;
4730 static const IMFAsyncCallbackVtbl presentclocksinkcallbackvtbl =
4732 present_clock_callback_QueryInterface,
4733 present_clock_sink_callback_AddRef,
4734 present_clock_sink_callback_Release,
4735 present_clock_callback_GetParameters,
4736 present_clock_sink_callback_Invoke,
4739 static ULONG WINAPI present_clock_timer_callback_AddRef(IMFAsyncCallback *iface)
4741 struct presentation_clock *clock = impl_from_timer_callback_IMFAsyncCallback(iface);
4742 return IMFPresentationClock_AddRef(&clock->IMFPresentationClock_iface);
4745 static ULONG WINAPI present_clock_timer_callback_Release(IMFAsyncCallback *iface)
4747 struct presentation_clock *clock = impl_from_timer_callback_IMFAsyncCallback(iface);
4748 return IMFPresentationClock_Release(&clock->IMFPresentationClock_iface);
4751 static HRESULT WINAPI present_clock_timer_callback_Invoke(IMFAsyncCallback *iface, IMFAsyncResult *result)
4753 struct presentation_clock *clock = impl_from_timer_callback_IMFAsyncCallback(iface);
4754 struct clock_timer *timer;
4755 IUnknown *object;
4756 HRESULT hr;
4758 if (FAILED(hr = IMFAsyncResult_GetObject(result, &object)))
4759 return hr;
4761 timer = impl_clock_timer_from_IUnknown(object);
4763 EnterCriticalSection(&clock->cs);
4764 list_remove(&timer->entry);
4765 IUnknown_Release(&timer->IUnknown_iface);
4766 LeaveCriticalSection(&clock->cs);
4768 IMFAsyncCallback_Invoke(timer->callback, timer->result);
4770 IUnknown_Release(object);
4772 return S_OK;
4775 static const IMFAsyncCallbackVtbl presentclocktimercallbackvtbl =
4777 present_clock_callback_QueryInterface,
4778 present_clock_timer_callback_AddRef,
4779 present_clock_timer_callback_Release,
4780 present_clock_callback_GetParameters,
4781 present_clock_timer_callback_Invoke,
4784 /***********************************************************************
4785 * MFCreatePresentationClock (mf.@)
4787 HRESULT WINAPI MFCreatePresentationClock(IMFPresentationClock **clock)
4789 struct presentation_clock *object;
4791 TRACE("%p.\n", clock);
4793 object = heap_alloc_zero(sizeof(*object));
4794 if (!object)
4795 return E_OUTOFMEMORY;
4797 object->IMFPresentationClock_iface.lpVtbl = &presentationclockvtbl;
4798 object->IMFRateControl_iface.lpVtbl = &presentclockratecontrolvtbl;
4799 object->IMFTimer_iface.lpVtbl = &presentclocktimervtbl;
4800 object->IMFShutdown_iface.lpVtbl = &presentclockshutdownvtbl;
4801 object->sink_callback.lpVtbl = &presentclocksinkcallbackvtbl;
4802 object->timer_callback.lpVtbl = &presentclocktimercallbackvtbl;
4803 object->refcount = 1;
4804 list_init(&object->sinks);
4805 list_init(&object->timers);
4806 object->rate = 1.0f;
4807 InitializeCriticalSection(&object->cs);
4809 *clock = &object->IMFPresentationClock_iface;
4811 return S_OK;
4814 static HRESULT WINAPI standard_quality_manager_QueryInterface(IMFQualityManager *iface, REFIID riid, void **out)
4816 struct quality_manager *manager = impl_from_IMFQualityManager(iface);
4818 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);
4820 if (IsEqualIID(riid, &IID_IMFQualityManager) ||
4821 IsEqualIID(riid, &IID_IUnknown))
4823 *out = iface;
4825 else if (IsEqualIID(riid, &IID_IMFClockStateSink))
4827 *out = &manager->IMFClockStateSink_iface;
4829 else
4831 WARN("Unsupported %s.\n", debugstr_guid(riid));
4832 *out = NULL;
4833 return E_NOINTERFACE;
4836 IUnknown_AddRef((IUnknown *)*out);
4837 return S_OK;
4840 static ULONG WINAPI standard_quality_manager_AddRef(IMFQualityManager *iface)
4842 struct quality_manager *manager = impl_from_IMFQualityManager(iface);
4843 ULONG refcount = InterlockedIncrement(&manager->refcount);
4845 TRACE("%p, refcount %u.\n", iface, refcount);
4847 return refcount;
4850 static ULONG WINAPI standard_quality_manager_Release(IMFQualityManager *iface)
4852 struct quality_manager *manager = impl_from_IMFQualityManager(iface);
4853 ULONG refcount = InterlockedDecrement(&manager->refcount);
4855 TRACE("%p, refcount %u.\n", iface, refcount);
4857 if (!refcount)
4859 if (manager->clock)
4860 IMFPresentationClock_Release(manager->clock);
4861 if (manager->topology)
4862 IMFTopology_Release(manager->topology);
4863 DeleteCriticalSection(&manager->cs);
4864 heap_free(manager);
4867 return refcount;
4870 static void standard_quality_manager_set_topology(struct quality_manager *manager, IMFTopology *topology)
4872 if (manager->topology)
4873 IMFTopology_Release(manager->topology);
4874 manager->topology = topology;
4875 if (manager->topology)
4876 IMFTopology_AddRef(manager->topology);
4879 static HRESULT WINAPI standard_quality_manager_NotifyTopology(IMFQualityManager *iface, IMFTopology *topology)
4881 struct quality_manager *manager = impl_from_IMFQualityManager(iface);
4882 HRESULT hr = S_OK;
4884 TRACE("%p, %p.\n", iface, topology);
4886 EnterCriticalSection(&manager->cs);
4887 if (manager->state == QUALITY_MANAGER_SHUT_DOWN)
4888 hr = MF_E_SHUTDOWN;
4889 else
4891 standard_quality_manager_set_topology(manager, topology);
4893 LeaveCriticalSection(&manager->cs);
4895 return hr;
4898 static void standard_quality_manager_release_clock(struct quality_manager *manager)
4900 if (manager->clock)
4902 IMFPresentationClock_RemoveClockStateSink(manager->clock, &manager->IMFClockStateSink_iface);
4903 IMFPresentationClock_Release(manager->clock);
4905 manager->clock = NULL;
4908 static HRESULT WINAPI standard_quality_manager_NotifyPresentationClock(IMFQualityManager *iface,
4909 IMFPresentationClock *clock)
4911 struct quality_manager *manager = impl_from_IMFQualityManager(iface);
4912 HRESULT hr = S_OK;
4914 TRACE("%p, %p.\n", iface, clock);
4916 EnterCriticalSection(&manager->cs);
4917 if (manager->state == QUALITY_MANAGER_SHUT_DOWN)
4918 hr = MF_E_SHUTDOWN;
4919 else if (!clock)
4920 hr = E_POINTER;
4921 else
4923 standard_quality_manager_release_clock(manager);
4924 manager->clock = clock;
4925 IMFPresentationClock_AddRef(manager->clock);
4926 if (FAILED(IMFPresentationClock_AddClockStateSink(manager->clock, &manager->IMFClockStateSink_iface)))
4927 WARN("Failed to set state sink.\n");
4929 LeaveCriticalSection(&manager->cs);
4931 return hr;
4934 static HRESULT WINAPI standard_quality_manager_NotifyProcessInput(IMFQualityManager *iface, IMFTopologyNode *node,
4935 LONG input_index, IMFSample *sample)
4937 TRACE("%p, %p, %d, %p stub.\n", iface, node, input_index, sample);
4939 return E_NOTIMPL;
4942 static HRESULT WINAPI standard_quality_manager_NotifyProcessOutput(IMFQualityManager *iface, IMFTopologyNode *node,
4943 LONG output_index, IMFSample *sample)
4945 TRACE("%p, %p, %d, %p stub.\n", iface, node, output_index, sample);
4947 return E_NOTIMPL;
4950 static HRESULT WINAPI standard_quality_manager_NotifyQualityEvent(IMFQualityManager *iface, IUnknown *object,
4951 IMFMediaEvent *event)
4953 FIXME("%p, %p, %p stub.\n", iface, object, event);
4955 return E_NOTIMPL;
4958 static HRESULT WINAPI standard_quality_manager_Shutdown(IMFQualityManager *iface)
4960 struct quality_manager *manager = impl_from_IMFQualityManager(iface);
4962 TRACE("%p.\n", iface);
4964 EnterCriticalSection(&manager->cs);
4965 if (manager->state != QUALITY_MANAGER_SHUT_DOWN)
4967 standard_quality_manager_release_clock(manager);
4968 standard_quality_manager_set_topology(manager, NULL);
4969 manager->state = QUALITY_MANAGER_SHUT_DOWN;
4971 LeaveCriticalSection(&manager->cs);
4973 return S_OK;
4976 static const IMFQualityManagerVtbl standard_quality_manager_vtbl =
4978 standard_quality_manager_QueryInterface,
4979 standard_quality_manager_AddRef,
4980 standard_quality_manager_Release,
4981 standard_quality_manager_NotifyTopology,
4982 standard_quality_manager_NotifyPresentationClock,
4983 standard_quality_manager_NotifyProcessInput,
4984 standard_quality_manager_NotifyProcessOutput,
4985 standard_quality_manager_NotifyQualityEvent,
4986 standard_quality_manager_Shutdown,
4989 static HRESULT WINAPI standard_quality_manager_sink_QueryInterface(IMFClockStateSink *iface,
4990 REFIID riid, void **obj)
4992 struct quality_manager *manager = impl_from_qm_IMFClockStateSink(iface);
4993 return IMFQualityManager_QueryInterface(&manager->IMFQualityManager_iface, riid, obj);
4996 static ULONG WINAPI standard_quality_manager_sink_AddRef(IMFClockStateSink *iface)
4998 struct quality_manager *manager = impl_from_qm_IMFClockStateSink(iface);
4999 return IMFQualityManager_AddRef(&manager->IMFQualityManager_iface);
5002 static ULONG WINAPI standard_quality_manager_sink_Release(IMFClockStateSink *iface)
5004 struct quality_manager *manager = impl_from_qm_IMFClockStateSink(iface);
5005 return IMFQualityManager_Release(&manager->IMFQualityManager_iface);
5008 static HRESULT WINAPI standard_quality_manager_sink_OnClockStart(IMFClockStateSink *iface,
5009 MFTIME systime, LONGLONG offset)
5011 return S_OK;
5014 static HRESULT WINAPI standard_quality_manager_sink_OnClockStop(IMFClockStateSink *iface,
5015 MFTIME systime)
5017 return S_OK;
5020 static HRESULT WINAPI standard_quality_manager_sink_OnClockPause(IMFClockStateSink *iface,
5021 MFTIME systime)
5023 return S_OK;
5026 static HRESULT WINAPI standard_quality_manager_sink_OnClockRestart(IMFClockStateSink *iface,
5027 MFTIME systime)
5029 return S_OK;
5032 static HRESULT WINAPI standard_quality_manager_sink_OnClockSetRate(IMFClockStateSink *iface,
5033 MFTIME systime, float rate)
5035 return S_OK;
5038 static const IMFClockStateSinkVtbl standard_quality_manager_sink_vtbl =
5040 standard_quality_manager_sink_QueryInterface,
5041 standard_quality_manager_sink_AddRef,
5042 standard_quality_manager_sink_Release,
5043 standard_quality_manager_sink_OnClockStart,
5044 standard_quality_manager_sink_OnClockStop,
5045 standard_quality_manager_sink_OnClockPause,
5046 standard_quality_manager_sink_OnClockRestart,
5047 standard_quality_manager_sink_OnClockSetRate,
5050 HRESULT WINAPI MFCreateStandardQualityManager(IMFQualityManager **manager)
5052 struct quality_manager *object;
5054 TRACE("%p.\n", manager);
5056 object = heap_alloc_zero(sizeof(*object));
5057 if (!object)
5058 return E_OUTOFMEMORY;
5060 object->IMFQualityManager_iface.lpVtbl = &standard_quality_manager_vtbl;
5061 object->IMFClockStateSink_iface.lpVtbl = &standard_quality_manager_sink_vtbl;
5062 object->refcount = 1;
5063 InitializeCriticalSection(&object->cs);
5065 *manager = &object->IMFQualityManager_iface;
5067 return S_OK;