mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / evr / presenter.c
blob8d633124db20da1fe795be5a4c0992c4f5051fce
1 /*
2 * Copyright 2020 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 #define COBJMACROS
21 #include "evr.h"
22 #include "d3d9.h"
23 #include "mfapi.h"
24 #include "mferror.h"
25 #include "dxva2api.h"
27 #include "evr_classes.h"
28 #include "evr_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(evr);
34 enum presenter_state
36 PRESENTER_STATE_SHUT_DOWN = 0,
37 PRESENTER_STATE_STARTED,
38 PRESENTER_STATE_STOPPED,
39 PRESENTER_STATE_PAUSED,
42 enum presenter_flags
44 PRESENTER_MIXER_HAS_INPUT = 0x1,
47 enum streaming_thread_message
49 EVRM_STOP = WM_USER,
50 EVRM_PRESENT = WM_USER + 1,
51 EVRM_PROCESS_INPUT = WM_USER + 2,
54 struct sample_queue
56 IMFSample **samples;
57 unsigned int size;
58 unsigned int used;
59 unsigned int front;
60 unsigned int back;
63 struct streaming_thread
65 HANDLE hthread;
66 HANDLE ready_event;
67 DWORD tid;
68 struct sample_queue queue;
71 struct video_presenter
73 IMFVideoPresenter IMFVideoPresenter_iface;
74 IMFVideoDeviceID IMFVideoDeviceID_iface;
75 IMFTopologyServiceLookupClient IMFTopologyServiceLookupClient_iface;
76 IMFVideoDisplayControl IMFVideoDisplayControl_iface;
77 IMFRateSupport IMFRateSupport_iface;
78 IMFGetService IMFGetService_iface;
79 IMFVideoPositionMapper IMFVideoPositionMapper_iface;
80 IQualProp IQualProp_iface;
81 IMFQualityAdvise IMFQualityAdvise_iface;
82 IDirect3DDeviceManager9 IDirect3DDeviceManager9_iface;
83 IMFVideoSampleAllocatorNotify allocator_cb;
84 IUnknown IUnknown_inner;
85 IUnknown *outer_unk;
86 LONG refcount;
88 IMFTransform *mixer;
89 IMFClock *clock;
90 IMediaEventSink *event_sink;
92 IDirect3DDeviceManager9 *device_manager;
93 IDirect3DSwapChain9 *swapchain;
94 HANDLE hdevice;
96 IMFVideoSampleAllocator *allocator;
97 struct streaming_thread thread;
98 unsigned int allocator_capacity;
99 IMFMediaType *media_type;
100 LONGLONG frame_time_threshold;
101 UINT reset_token;
102 HWND video_window;
103 MFVideoNormalizedRect src_rect;
104 RECT dst_rect;
105 DWORD rendering_prefs;
106 SIZE native_size;
107 SIZE native_ratio;
108 unsigned int ar_mode;
109 unsigned int state;
110 unsigned int flags;
111 CRITICAL_SECTION cs;
114 static struct video_presenter *impl_from_IUnknown(IUnknown *iface)
116 return CONTAINING_RECORD(iface, struct video_presenter, IUnknown_inner);
119 static struct video_presenter *impl_from_IMFVideoPresenter(IMFVideoPresenter *iface)
121 return CONTAINING_RECORD(iface, struct video_presenter, IMFVideoPresenter_iface);
124 static struct video_presenter *impl_from_IMFVideoDeviceID(IMFVideoDeviceID *iface)
126 return CONTAINING_RECORD(iface, struct video_presenter, IMFVideoDeviceID_iface);
129 static struct video_presenter *impl_from_IMFTopologyServiceLookupClient(IMFTopologyServiceLookupClient *iface)
131 return CONTAINING_RECORD(iface, struct video_presenter, IMFTopologyServiceLookupClient_iface);
134 static struct video_presenter *impl_from_IMFVideoDisplayControl(IMFVideoDisplayControl *iface)
136 return CONTAINING_RECORD(iface, struct video_presenter, IMFVideoDisplayControl_iface);
139 static struct video_presenter *impl_from_IMFRateSupport(IMFRateSupport *iface)
141 return CONTAINING_RECORD(iface, struct video_presenter, IMFRateSupport_iface);
144 static struct video_presenter *impl_from_IMFGetService(IMFGetService *iface)
146 return CONTAINING_RECORD(iface, struct video_presenter, IMFGetService_iface);
149 static struct video_presenter *impl_from_IMFVideoPositionMapper(IMFVideoPositionMapper *iface)
151 return CONTAINING_RECORD(iface, struct video_presenter, IMFVideoPositionMapper_iface);
154 static struct video_presenter *impl_from_IMFVideoSampleAllocatorNotify(IMFVideoSampleAllocatorNotify *iface)
156 return CONTAINING_RECORD(iface, struct video_presenter, allocator_cb);
159 static struct video_presenter *impl_from_IQualProp(IQualProp *iface)
161 return CONTAINING_RECORD(iface, struct video_presenter, IQualProp_iface);
164 static struct video_presenter *impl_from_IMFQualityAdvise(IMFQualityAdvise *iface)
166 return CONTAINING_RECORD(iface, struct video_presenter, IMFQualityAdvise_iface);
169 static struct video_presenter *impl_from_IDirect3DDeviceManager9(IDirect3DDeviceManager9 *iface)
171 return CONTAINING_RECORD(iface, struct video_presenter, IDirect3DDeviceManager9_iface);
174 static void video_presenter_notify_renderer(struct video_presenter *presenter,
175 LONG event, LONG_PTR param1, LONG_PTR param2)
177 if (presenter->event_sink)
178 IMediaEventSink_Notify(presenter->event_sink, event, param1, param2);
181 static unsigned int get_gcd(unsigned int a, unsigned int b)
183 unsigned int m;
185 while (b)
187 m = a % b;
188 a = b;
189 b = m;
192 return a;
195 static HRESULT video_presenter_get_device(struct video_presenter *presenter, IDirect3DDevice9 **device)
197 HRESULT hr;
199 if (!presenter->hdevice)
201 if (FAILED(hr = IDirect3DDeviceManager9_OpenDeviceHandle(presenter->device_manager, &presenter->hdevice)))
202 return hr;
205 return IDirect3DDeviceManager9_LockDevice(presenter->device_manager, presenter->hdevice, device, TRUE);
208 static void video_presenter_get_native_video_size(struct video_presenter *presenter)
210 IMFMediaType *media_type;
211 UINT64 frame_size = 0;
213 memset(&presenter->native_size, 0, sizeof(presenter->native_size));
214 memset(&presenter->native_ratio, 0, sizeof(presenter->native_ratio));
216 if (!presenter->mixer)
217 return;
219 if (FAILED(IMFTransform_GetInputCurrentType(presenter->mixer, 0, &media_type)))
220 return;
222 if (SUCCEEDED(IMFMediaType_GetUINT64(media_type, &MF_MT_FRAME_SIZE, &frame_size)))
224 unsigned int gcd;
226 presenter->native_size.cx = frame_size >> 32;
227 presenter->native_size.cy = frame_size;
229 if ((gcd = get_gcd(presenter->native_size.cx, presenter->native_size.cy)))
231 presenter->native_ratio.cx = presenter->native_size.cx / gcd;
232 presenter->native_ratio.cy = presenter->native_size.cy / gcd;
236 IMFMediaType_Release(media_type);
239 /* It is important this is called to reset callback too to break circular referencing,
240 when allocator keeps a reference of its container, that created it. */
241 static void video_presenter_set_allocator_callback(struct video_presenter *presenter,
242 IMFVideoSampleAllocatorNotify *notify_cb)
244 IMFVideoSampleAllocatorCallback *cb;
246 IMFVideoSampleAllocator_QueryInterface(presenter->allocator, &IID_IMFVideoSampleAllocatorCallback, (void **)&cb);
247 IMFVideoSampleAllocatorCallback_SetCallback(cb, notify_cb);
248 IMFVideoSampleAllocatorCallback_Release(cb);
251 static void video_presenter_reset_media_type(struct video_presenter *presenter)
253 if (presenter->media_type)
254 IMFMediaType_Release(presenter->media_type);
255 presenter->media_type = NULL;
257 IMFVideoSampleAllocator_UninitializeSampleAllocator(presenter->allocator);
258 video_presenter_set_allocator_callback(presenter, NULL);
261 static HRESULT video_presenter_set_media_type(struct video_presenter *presenter, IMFMediaType *media_type)
263 unsigned int flags;
264 HRESULT hr;
266 if (!media_type)
268 video_presenter_reset_media_type(presenter);
269 return S_OK;
272 if (presenter->media_type && IMFMediaType_IsEqual(presenter->media_type, media_type, &flags) == S_OK)
273 return S_OK;
275 video_presenter_reset_media_type(presenter);
277 if (SUCCEEDED(hr = IMFVideoSampleAllocator_InitializeSampleAllocator(presenter->allocator,
278 presenter->allocator_capacity, media_type)))
280 MFRatio ratio;
281 UINT64 rate, frametime;
283 presenter->media_type = media_type;
284 IMFMediaType_AddRef(presenter->media_type);
286 if (SUCCEEDED(IMFMediaType_GetUINT64(presenter->media_type, &MF_MT_FRAME_RATE, &rate)))
288 ratio.Denominator = rate;
289 ratio.Numerator = rate >> 32;
291 else
293 ratio.Denominator = 1;
294 ratio.Numerator = 30;
297 MFFrameRateToAverageTimePerFrame(ratio.Numerator, ratio.Denominator, &frametime);
298 presenter->frame_time_threshold = frametime / 4;
300 else
301 WARN("Failed to initialize sample allocator, hr %#x.\n", hr);
303 return hr;
306 static HRESULT video_presenter_invalidate_media_type(struct video_presenter *presenter)
308 IMFMediaType *media_type, *candidate_type;
309 unsigned int idx = 0;
310 UINT64 frame_size;
311 MFVideoArea aperture;
312 RECT rect;
313 HRESULT hr;
315 if (FAILED(hr = MFCreateMediaType(&media_type)))
316 return hr;
318 video_presenter_get_native_video_size(presenter);
320 rect = presenter->dst_rect;
321 if (rect.left == 0 && rect.right == 0 && rect.bottom == 0 && rect.top == 0)
323 rect.right = presenter->native_size.cx;
324 rect.bottom = presenter->native_size.cy;
327 aperture.Area.cx = rect.right - rect.left;
328 aperture.Area.cy = rect.bottom - rect.top;
329 aperture.OffsetX.value = 0;
330 aperture.OffsetX.fract = 0;
331 aperture.OffsetY.value = 0;
332 aperture.OffsetY.fract = 0;
333 frame_size = (UINT64)aperture.Area.cx << 32 | aperture.Area.cy;
335 while (SUCCEEDED(hr = IMFTransform_GetOutputAvailableType(presenter->mixer, 0, idx++, &candidate_type)))
337 /* FIXME: check that d3d device supports this format */
339 if (FAILED(hr = IMFMediaType_CopyAllItems(candidate_type, (IMFAttributes *)media_type)))
340 WARN("Failed to clone a media type, hr %#x.\n", hr);
341 IMFMediaType_Release(candidate_type);
343 IMFMediaType_SetUINT64(media_type, &MF_MT_FRAME_SIZE, frame_size);
344 IMFMediaType_SetBlob(media_type, &MF_MT_GEOMETRIC_APERTURE, (UINT8 *)&aperture, sizeof(aperture));
346 hr = IMFTransform_SetOutputType(presenter->mixer, 0, media_type, MFT_SET_TYPE_TEST_ONLY);
348 if (SUCCEEDED(hr))
349 hr = video_presenter_set_media_type(presenter, media_type);
351 if (SUCCEEDED(hr))
352 hr = IMFTransform_SetOutputType(presenter->mixer, 0, media_type, 0);
354 if (SUCCEEDED(hr))
355 break;
358 IMFMediaType_Release(media_type);
360 return hr;
363 static void video_presenter_sample_queue_init(struct video_presenter *presenter)
365 struct sample_queue *queue = &presenter->thread.queue;
367 if (queue->size)
368 return;
370 memset(queue, 0, sizeof(*queue));
371 queue->samples = calloc(presenter->allocator_capacity, sizeof(*queue->samples));
372 queue->size = presenter->allocator_capacity;
373 queue->back = queue->size - 1;
376 static void video_presenter_sample_queue_push(struct video_presenter *presenter, IMFSample *sample)
378 struct sample_queue *queue = &presenter->thread.queue;
380 EnterCriticalSection(&presenter->cs);
381 if (queue->used != queue->size)
383 queue->back = (queue->back + 1) % queue->size;
384 queue->samples[queue->back] = sample;
385 queue->used++;
386 IMFSample_AddRef(sample);
388 LeaveCriticalSection(&presenter->cs);
391 static BOOL video_presenter_sample_queue_pop(struct video_presenter *presenter, IMFSample **sample)
393 struct sample_queue *queue = &presenter->thread.queue;
395 EnterCriticalSection(&presenter->cs);
396 if (queue->used)
398 *sample = queue->samples[queue->front];
399 queue->front = (queue->front + 1) % queue->size;
400 queue->used--;
402 else
403 *sample = NULL;
404 LeaveCriticalSection(&presenter->cs);
406 return *sample != NULL;
409 static HRESULT video_presenter_get_sample_surface(IMFSample *sample, IDirect3DSurface9 **surface)
411 IMFMediaBuffer *buffer;
412 IMFGetService *gs;
413 HRESULT hr;
415 if (FAILED(hr = IMFSample_GetBufferByIndex(sample, 0, &buffer)))
416 return hr;
418 hr = IMFMediaBuffer_QueryInterface(buffer, &IID_IMFGetService, (void **)&gs);
419 IMFMediaBuffer_Release(buffer);
420 if (FAILED(hr))
421 return hr;
423 hr = IMFGetService_GetService(gs, &MR_BUFFER_SERVICE, &IID_IDirect3DSurface9, (void **)surface);
424 IMFGetService_Release(gs);
425 return hr;
428 static void scale_rect(RECT *rect, unsigned int width, unsigned int height, const MFVideoNormalizedRect *scale)
430 if (rect->left == 0.0f && rect->top == 0.0f && rect->right == 1.0f && rect->bottom == 1.0f)
432 SetRect(rect, 0, 0, width, height);
434 else
436 rect->left = width * scale->left;
437 rect->right = width * scale->right;
438 rect->top = height * scale->top;
439 rect->bottom = height * scale->bottom;
443 static void video_presenter_sample_present(struct video_presenter *presenter, IMFSample *sample)
445 IDirect3DSurface9 *surface, *backbuffer;
446 IDirect3DDevice9 *device;
447 D3DSURFACE_DESC desc;
448 RECT dst, src;
449 HRESULT hr;
451 if (!presenter->swapchain)
452 return;
454 if (FAILED(hr = video_presenter_get_sample_surface(sample, &surface)))
456 WARN("Failed to get sample surface, hr %#x.\n", hr);
457 return;
460 if (FAILED(hr = IDirect3DSwapChain9_GetBackBuffer(presenter->swapchain, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer)))
462 WARN("Failed to get a backbuffer, hr %#x.\n", hr);
463 IDirect3DSurface9_Release(surface);
464 return;
467 IDirect3DSwapChain9_GetDevice(presenter->swapchain, &device);
468 IDirect3DDevice9_StretchRect(device, surface, NULL, backbuffer, NULL, D3DTEXF_POINT);
470 IDirect3DSurface9_GetDesc(surface, &desc);
471 scale_rect(&src, desc.Width, desc.Height, &presenter->src_rect);
473 IDirect3DSurface9_GetDesc(backbuffer, &desc);
474 SetRect(&dst, 0, 0, desc.Width, desc.Height);
476 if (presenter->ar_mode & MFVideoARMode_PreservePicture)
478 unsigned int src_width = src.right - src.left, src_height = src.bottom - src.top;
479 unsigned int dst_width = dst.right - dst.left, dst_height = dst.bottom - dst.top;
481 if (src_width * dst_height > dst_width * src_height)
483 /* src is "wider" than dst. */
484 unsigned int dst_center = (dst.top + dst.bottom) / 2;
485 unsigned int scaled_height = src_height * dst_width / src_width;
487 dst.top = dst_center - scaled_height / 2;
488 dst.bottom = dst.top + scaled_height;
490 else if (src_width * dst_height < dst_width * src_height)
492 /* src is "taller" than dst. */
493 unsigned int dst_center = (dst.left + dst.right) / 2;
494 unsigned int scaled_width = src_width * dst_height / src_height;
496 dst.left = dst_center - scaled_width / 2;
497 dst.right = dst.left + scaled_width;
501 IDirect3DSwapChain9_Present(presenter->swapchain, &src, &dst, NULL, NULL, 0);
503 IDirect3DDevice9_Release(device);
504 IDirect3DSurface9_Release(backbuffer);
505 IDirect3DSurface9_Release(surface);
508 static void video_presenter_check_queue(struct video_presenter *presenter,
509 unsigned int *next_wait)
511 LONGLONG pts, clocktime, delta;
512 unsigned int wait = 0;
513 BOOL present = TRUE;
514 IMFSample *sample;
515 MFTIME systime;
516 HRESULT hr;
518 while (video_presenter_sample_queue_pop(presenter, &sample))
520 wait = 0;
522 if (presenter->clock)
524 pts = clocktime = 0;
526 hr = IMFSample_GetSampleTime(sample, &pts);
527 if (SUCCEEDED(hr))
528 hr = IMFClock_GetCorrelatedTime(presenter->clock, 0, &clocktime, &systime);
530 delta = pts - clocktime;
531 if (delta > 3 * presenter->frame_time_threshold)
533 /* Convert 100ns -> msec */
534 wait = (delta - 3 * presenter->frame_time_threshold) / 100000;
535 present = FALSE;
539 if (present)
540 video_presenter_sample_present(presenter, sample);
541 else
542 video_presenter_sample_queue_push(presenter, sample);
544 IMFSample_Release(sample);
546 if (wait > 0)
547 break;
550 if (!wait)
551 wait = INFINITE;
553 *next_wait = wait;
556 static void video_presenter_schedule_sample(struct video_presenter *presenter, IMFSample *sample)
558 if (!presenter->thread.tid)
560 WARN("Streaming thread hasn't been started.\n");
561 return;
564 if (presenter->clock)
566 video_presenter_sample_queue_push(presenter, sample);
567 PostThreadMessageW(presenter->thread.tid, EVRM_PRESENT, 0, 0);
569 else
571 video_presenter_sample_present(presenter, sample);
575 static HRESULT video_presenter_process_input(struct video_presenter *presenter)
577 MFT_OUTPUT_DATA_BUFFER buffer;
578 HRESULT hr = S_OK;
579 IMFSample *sample;
580 DWORD status;
582 if (!presenter->media_type)
583 return S_OK;
585 while (hr == S_OK)
587 LONGLONG mixing_started, mixing_finished;
588 MFTIME systime;
590 if (!(presenter->flags & PRESENTER_MIXER_HAS_INPUT))
591 break;
593 if (FAILED(hr = IMFVideoSampleAllocator_AllocateSample(presenter->allocator, &sample)))
595 WARN("Failed to allocate a sample, hr %#x.\n", hr);
596 break;
599 memset(&buffer, 0, sizeof(buffer));
600 buffer.pSample = sample;
602 if (presenter->clock)
603 IMFClock_GetCorrelatedTime(presenter->clock, 0, &mixing_started, &systime);
605 if (FAILED(hr = IMFTransform_ProcessOutput(presenter->mixer, 0, 1, &buffer, &status)))
607 /* FIXME: failure path probably needs to handle some errors specifically */
608 presenter->flags &= ~PRESENTER_MIXER_HAS_INPUT;
609 IMFSample_Release(sample);
610 break;
612 else
614 if (presenter->clock)
616 LONGLONG latency;
618 IMFClock_GetCorrelatedTime(presenter->clock, 0, &mixing_finished, &systime);
619 latency = mixing_finished - mixing_started;
620 video_presenter_notify_renderer(presenter, EC_PROCESSING_LATENCY, (LONG_PTR)&latency, 0);
623 if (buffer.pEvents)
624 IMFCollection_Release(buffer.pEvents);
626 video_presenter_schedule_sample(presenter, sample);
628 IMFSample_Release(sample);
632 return S_OK;
635 static DWORD CALLBACK video_presenter_streaming_thread(void *arg)
637 struct video_presenter *presenter = arg;
638 unsigned int wait = INFINITE;
639 BOOL stop_thread = FALSE;
640 MSG msg;
642 PeekMessageW(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
644 SetEvent(presenter->thread.ready_event);
646 while (!stop_thread)
648 if (MsgWaitForMultipleObjects(0, NULL, FALSE, wait, QS_POSTMESSAGE) == WAIT_TIMEOUT)
649 video_presenter_check_queue(presenter, &wait);
651 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
653 BOOL peek = TRUE;
655 switch (msg.message)
657 case EVRM_STOP:
658 stop_thread = TRUE;
659 break;
661 case EVRM_PRESENT:
662 if (peek)
664 video_presenter_check_queue(presenter, &wait);
665 peek = wait != INFINITE;
667 break;
669 case EVRM_PROCESS_INPUT:
670 EnterCriticalSection(&presenter->cs);
671 video_presenter_process_input(presenter);
672 LeaveCriticalSection(&presenter->cs);
673 break;
674 default:
680 return 0;
683 static HRESULT video_presenter_start_streaming(struct video_presenter *presenter)
685 if (presenter->thread.hthread)
686 return S_OK;
688 video_presenter_sample_queue_init(presenter);
690 if (!(presenter->thread.ready_event = CreateEventW(NULL, FALSE, FALSE, NULL)))
691 return HRESULT_FROM_WIN32(GetLastError());
693 if (!(presenter->thread.hthread = CreateThread(NULL, 0, video_presenter_streaming_thread,
694 presenter, 0, &presenter->thread.tid)))
696 WARN("Failed to create streaming thread.\n");
697 CloseHandle(presenter->thread.ready_event);
698 presenter->thread.ready_event = NULL;
699 return E_FAIL;
702 video_presenter_set_allocator_callback(presenter, &presenter->allocator_cb);
704 WaitForSingleObject(presenter->thread.ready_event, INFINITE);
705 CloseHandle(presenter->thread.ready_event);
706 presenter->thread.ready_event = NULL;
708 TRACE("Started streaming thread, tid %#x.\n", presenter->thread.tid);
710 return S_OK;
713 static HRESULT video_presenter_end_streaming(struct video_presenter *presenter)
715 if (!presenter->thread.hthread)
716 return S_OK;
718 PostThreadMessageW(presenter->thread.tid, EVRM_STOP, 0, 0);
720 WaitForSingleObject(presenter->thread.hthread, INFINITE);
721 CloseHandle(presenter->thread.hthread);
723 TRACE("Terminated streaming thread tid %#x.\n", presenter->thread.tid);
725 memset(&presenter->thread, 0, sizeof(presenter->thread));
726 video_presenter_set_allocator_callback(presenter, NULL);
728 return S_OK;
731 static HRESULT WINAPI video_presenter_inner_QueryInterface(IUnknown *iface, REFIID riid, void **obj)
733 struct video_presenter *presenter = impl_from_IUnknown(iface);
735 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), obj);
737 if (IsEqualIID(riid, &IID_IUnknown))
739 *obj = iface;
741 else if (IsEqualIID(riid, &IID_IMFClockStateSink)
742 || IsEqualIID(riid, &IID_IMFVideoPresenter))
744 *obj = &presenter->IMFVideoPresenter_iface;
746 else if (IsEqualIID(riid, &IID_IMFVideoDeviceID))
748 *obj = &presenter->IMFVideoDeviceID_iface;
750 else if (IsEqualIID(riid, &IID_IMFTopologyServiceLookupClient))
752 *obj = &presenter->IMFTopologyServiceLookupClient_iface;
754 else if (IsEqualIID(riid, &IID_IMFVideoDisplayControl))
756 *obj = &presenter->IMFVideoDisplayControl_iface;
758 else if (IsEqualIID(riid, &IID_IMFRateSupport))
760 *obj = &presenter->IMFRateSupport_iface;
762 else if (IsEqualIID(riid, &IID_IMFGetService))
764 *obj = &presenter->IMFGetService_iface;
766 else if (IsEqualIID(riid, &IID_IMFVideoPositionMapper))
768 *obj = &presenter->IMFVideoPositionMapper_iface;
770 else if (IsEqualIID(riid, &IID_IQualProp))
772 *obj = &presenter->IQualProp_iface;
774 else if (IsEqualIID(riid, &IID_IMFQualityAdvise))
776 *obj = &presenter->IMFQualityAdvise_iface;
778 else if (IsEqualIID(riid, &IID_IDirect3DDeviceManager9))
780 *obj = &presenter->IDirect3DDeviceManager9_iface;
782 else
784 WARN("Unimplemented interface %s.\n", debugstr_guid(riid));
785 *obj = NULL;
786 return E_NOINTERFACE;
789 IUnknown_AddRef((IUnknown *)*obj);
790 return S_OK;
793 static ULONG WINAPI video_presenter_inner_AddRef(IUnknown *iface)
795 struct video_presenter *presenter = impl_from_IUnknown(iface);
796 ULONG refcount = InterlockedIncrement(&presenter->refcount);
798 TRACE("%p, refcount %u.\n", iface, refcount);
800 return refcount;
803 static void video_presenter_clear_container(struct video_presenter *presenter)
805 if (presenter->clock)
806 IMFClock_Release(presenter->clock);
807 if (presenter->mixer)
808 IMFTransform_Release(presenter->mixer);
809 if (presenter->event_sink)
810 IMediaEventSink_Release(presenter->event_sink);
811 presenter->clock = NULL;
812 presenter->mixer = NULL;
813 presenter->event_sink = NULL;
816 static ULONG WINAPI video_presenter_inner_Release(IUnknown *iface)
818 struct video_presenter *presenter = impl_from_IUnknown(iface);
819 ULONG refcount = InterlockedDecrement(&presenter->refcount);
821 TRACE("%p, refcount %u.\n", iface, refcount);
823 if (!refcount)
825 video_presenter_end_streaming(presenter);
826 video_presenter_clear_container(presenter);
827 video_presenter_reset_media_type(presenter);
828 DeleteCriticalSection(&presenter->cs);
829 if (presenter->swapchain)
830 IDirect3DSwapChain9_Release(presenter->swapchain);
831 if (presenter->device_manager)
833 IDirect3DDeviceManager9_CloseDeviceHandle(presenter->device_manager, presenter->hdevice);
834 IDirect3DDeviceManager9_Release(presenter->device_manager);
836 if (presenter->allocator)
837 IMFVideoSampleAllocator_Release(presenter->allocator);
838 free(presenter);
841 return refcount;
844 static const IUnknownVtbl video_presenter_inner_vtbl =
846 video_presenter_inner_QueryInterface,
847 video_presenter_inner_AddRef,
848 video_presenter_inner_Release,
851 static HRESULT WINAPI video_presenter_QueryInterface(IMFVideoPresenter *iface, REFIID riid, void **obj)
853 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
854 return IUnknown_QueryInterface(presenter->outer_unk, riid, obj);
857 static ULONG WINAPI video_presenter_AddRef(IMFVideoPresenter *iface)
859 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
860 return IUnknown_AddRef(presenter->outer_unk);
863 static ULONG WINAPI video_presenter_Release(IMFVideoPresenter *iface)
865 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
866 return IUnknown_Release(presenter->outer_unk);
869 static HRESULT WINAPI video_presenter_OnClockStart(IMFVideoPresenter *iface, MFTIME systime, LONGLONG offset)
871 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
873 TRACE("%p, %s, %s.\n", iface, debugstr_time(systime), wine_dbgstr_longlong(offset));
875 EnterCriticalSection(&presenter->cs);
876 presenter->state = PRESENTER_STATE_STARTED;
877 LeaveCriticalSection(&presenter->cs);
879 return S_OK;
882 static HRESULT WINAPI video_presenter_OnClockStop(IMFVideoPresenter *iface, MFTIME systime)
884 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
886 TRACE("%p, %s.\n", iface, debugstr_time(systime));
888 EnterCriticalSection(&presenter->cs);
889 presenter->state = PRESENTER_STATE_STOPPED;
890 LeaveCriticalSection(&presenter->cs);
892 return S_OK;
895 static HRESULT WINAPI video_presenter_OnClockPause(IMFVideoPresenter *iface, MFTIME systime)
897 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
899 TRACE("%p, %s.\n", iface, debugstr_time(systime));
901 EnterCriticalSection(&presenter->cs);
902 presenter->state = PRESENTER_STATE_PAUSED;
903 LeaveCriticalSection(&presenter->cs);
905 return S_OK;
908 static HRESULT WINAPI video_presenter_OnClockRestart(IMFVideoPresenter *iface, MFTIME systime)
910 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
912 TRACE("%p, %s.\n", iface, debugstr_time(systime));
914 EnterCriticalSection(&presenter->cs);
915 presenter->state = PRESENTER_STATE_STARTED;
916 LeaveCriticalSection(&presenter->cs);
918 return S_OK;
921 static HRESULT WINAPI video_presenter_OnClockSetRate(IMFVideoPresenter *iface, MFTIME systime, float rate)
923 FIXME("%p, %s, %f.\n", iface, debugstr_time(systime), rate);
925 return E_NOTIMPL;
928 static HRESULT WINAPI video_presenter_ProcessMessage(IMFVideoPresenter *iface, MFVP_MESSAGE_TYPE message, ULONG_PTR param)
930 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
931 HRESULT hr;
933 TRACE("%p, %d, %lu.\n", iface, message, param);
935 EnterCriticalSection(&presenter->cs);
937 switch (message)
939 case MFVP_MESSAGE_INVALIDATEMEDIATYPE:
940 hr = video_presenter_invalidate_media_type(presenter);
941 break;
942 case MFVP_MESSAGE_BEGINSTREAMING:
943 hr = video_presenter_start_streaming(presenter);
944 break;
945 case MFVP_MESSAGE_ENDSTREAMING:
946 hr = video_presenter_end_streaming(presenter);
947 break;
948 case MFVP_MESSAGE_PROCESSINPUTNOTIFY:
949 presenter->flags |= PRESENTER_MIXER_HAS_INPUT;
950 hr = video_presenter_process_input(presenter);
951 break;
952 default:
953 FIXME("Unsupported message %u.\n", message);
954 hr = E_NOTIMPL;
957 LeaveCriticalSection(&presenter->cs);
959 return hr;
962 static HRESULT WINAPI video_presenter_GetCurrentMediaType(IMFVideoPresenter *iface,
963 IMFVideoMediaType **media_type)
965 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
966 HRESULT hr;
968 TRACE("%p, %p.\n", iface, media_type);
970 EnterCriticalSection(&presenter->cs);
972 if (presenter->state == PRESENTER_STATE_SHUT_DOWN)
973 hr = MF_E_SHUTDOWN;
974 else if (!presenter->media_type)
975 hr = MF_E_NOT_INITIALIZED;
976 else
978 hr = IMFMediaType_QueryInterface(presenter->media_type, &IID_IMFVideoMediaType,
979 (void **)media_type);
982 LeaveCriticalSection(&presenter->cs);
984 return hr;
987 static const IMFVideoPresenterVtbl video_presenter_vtbl =
989 video_presenter_QueryInterface,
990 video_presenter_AddRef,
991 video_presenter_Release,
992 video_presenter_OnClockStart,
993 video_presenter_OnClockStop,
994 video_presenter_OnClockPause,
995 video_presenter_OnClockRestart,
996 video_presenter_OnClockSetRate,
997 video_presenter_ProcessMessage,
998 video_presenter_GetCurrentMediaType,
1001 static HRESULT WINAPI video_presenter_device_id_QueryInterface(IMFVideoDeviceID *iface, REFIID riid, void **obj)
1003 struct video_presenter *presenter = impl_from_IMFVideoDeviceID(iface);
1004 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1007 static ULONG WINAPI video_presenter_device_id_AddRef(IMFVideoDeviceID *iface)
1009 struct video_presenter *presenter = impl_from_IMFVideoDeviceID(iface);
1010 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1013 static ULONG WINAPI video_presenter_device_id_Release(IMFVideoDeviceID *iface)
1015 struct video_presenter *presenter = impl_from_IMFVideoDeviceID(iface);
1016 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1019 static HRESULT WINAPI video_presenter_device_id_GetDeviceID(IMFVideoDeviceID *iface, IID *device_id)
1021 TRACE("%p, %p.\n", iface, device_id);
1023 if (!device_id)
1024 return E_POINTER;
1026 memcpy(device_id, &IID_IDirect3DDevice9, sizeof(*device_id));
1028 return S_OK;
1031 static const IMFVideoDeviceIDVtbl video_presenter_device_id_vtbl =
1033 video_presenter_device_id_QueryInterface,
1034 video_presenter_device_id_AddRef,
1035 video_presenter_device_id_Release,
1036 video_presenter_device_id_GetDeviceID,
1039 static HRESULT WINAPI video_presenter_service_client_QueryInterface(IMFTopologyServiceLookupClient *iface,
1040 REFIID riid, void **obj)
1042 struct video_presenter *presenter = impl_from_IMFTopologyServiceLookupClient(iface);
1043 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1046 static ULONG WINAPI video_presenter_service_client_AddRef(IMFTopologyServiceLookupClient *iface)
1048 struct video_presenter *presenter = impl_from_IMFTopologyServiceLookupClient(iface);
1049 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1052 static ULONG WINAPI video_presenter_service_client_Release(IMFTopologyServiceLookupClient *iface)
1054 struct video_presenter *presenter = impl_from_IMFTopologyServiceLookupClient(iface);
1055 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1058 static void video_presenter_set_mixer_rect(struct video_presenter *presenter)
1060 IMFAttributes *attributes;
1061 HRESULT hr;
1063 if (!presenter->mixer)
1064 return;
1066 if (SUCCEEDED(IMFTransform_GetAttributes(presenter->mixer, &attributes)))
1068 if (FAILED(hr = IMFAttributes_SetBlob(attributes, &VIDEO_ZOOM_RECT, (const UINT8 *)&presenter->src_rect,
1069 sizeof(presenter->src_rect))))
1071 WARN("Failed to set zoom rectangle attribute, hr %#x.\n", hr);
1073 IMFAttributes_Release(attributes);
1077 static HRESULT video_presenter_attach_mixer(struct video_presenter *presenter, IMFTopologyServiceLookup *service_lookup)
1079 IMFVideoDeviceID *device_id;
1080 unsigned int count;
1081 GUID id = { 0 };
1082 HRESULT hr;
1084 count = 1;
1085 if (FAILED(hr = IMFTopologyServiceLookup_LookupService(service_lookup, MF_SERVICE_LOOKUP_GLOBAL, 0,
1086 &MR_VIDEO_MIXER_SERVICE, &IID_IMFTransform, (void **)&presenter->mixer, &count)))
1088 WARN("Failed to get mixer interface, hr %#x.\n", hr);
1089 return hr;
1092 if (SUCCEEDED(hr = IMFTransform_QueryInterface(presenter->mixer, &IID_IMFVideoDeviceID, (void **)&device_id)))
1094 if (SUCCEEDED(hr = IMFVideoDeviceID_GetDeviceID(device_id, &id)))
1096 if (!IsEqualGUID(&id, &IID_IDirect3DDevice9))
1097 hr = MF_E_INVALIDREQUEST;
1100 IMFVideoDeviceID_Release(device_id);
1103 if (FAILED(hr))
1105 IMFTransform_Release(presenter->mixer);
1106 presenter->mixer = NULL;
1109 video_presenter_set_mixer_rect(presenter);
1110 video_presenter_get_native_video_size(presenter);
1112 return hr;
1115 static HRESULT WINAPI video_presenter_service_client_InitServicePointers(IMFTopologyServiceLookupClient *iface,
1116 IMFTopologyServiceLookup *service_lookup)
1118 struct video_presenter *presenter = impl_from_IMFTopologyServiceLookupClient(iface);
1119 unsigned int count;
1120 HRESULT hr = S_OK;
1122 TRACE("%p, %p.\n", iface, service_lookup);
1124 if (!service_lookup)
1125 return E_POINTER;
1127 EnterCriticalSection(&presenter->cs);
1129 if (presenter->state == PRESENTER_STATE_STARTED ||
1130 presenter->state == PRESENTER_STATE_PAUSED)
1132 hr = MF_E_INVALIDREQUEST;
1134 else
1136 video_presenter_clear_container(presenter);
1138 count = 1;
1139 IMFTopologyServiceLookup_LookupService(service_lookup, MF_SERVICE_LOOKUP_GLOBAL, 0,
1140 &MR_VIDEO_RENDER_SERVICE, &IID_IMFClock, (void **)&presenter->clock, &count);
1142 hr = video_presenter_attach_mixer(presenter, service_lookup);
1144 if (SUCCEEDED(hr))
1146 count = 1;
1147 if (FAILED(hr = IMFTopologyServiceLookup_LookupService(service_lookup, MF_SERVICE_LOOKUP_GLOBAL, 0,
1148 &MR_VIDEO_RENDER_SERVICE, &IID_IMediaEventSink, (void **)&presenter->event_sink, &count)))
1150 WARN("Failed to get renderer event sink, hr %#x.\n", hr);
1154 if (SUCCEEDED(hr))
1155 presenter->state = PRESENTER_STATE_STOPPED;
1158 LeaveCriticalSection(&presenter->cs);
1160 return hr;
1163 static HRESULT WINAPI video_presenter_service_client_ReleaseServicePointers(IMFTopologyServiceLookupClient *iface)
1165 struct video_presenter *presenter = impl_from_IMFTopologyServiceLookupClient(iface);
1167 TRACE("%p.\n", iface);
1169 EnterCriticalSection(&presenter->cs);
1171 presenter->state = PRESENTER_STATE_SHUT_DOWN;
1172 video_presenter_clear_container(presenter);
1174 LeaveCriticalSection(&presenter->cs);
1176 return S_OK;
1179 static const IMFTopologyServiceLookupClientVtbl video_presenter_service_client_vtbl =
1181 video_presenter_service_client_QueryInterface,
1182 video_presenter_service_client_AddRef,
1183 video_presenter_service_client_Release,
1184 video_presenter_service_client_InitServicePointers,
1185 video_presenter_service_client_ReleaseServicePointers,
1188 static HRESULT WINAPI video_presenter_control_QueryInterface(IMFVideoDisplayControl *iface, REFIID riid, void **obj)
1190 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1191 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1194 static ULONG WINAPI video_presenter_control_AddRef(IMFVideoDisplayControl *iface)
1196 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1197 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1200 static ULONG WINAPI video_presenter_control_Release(IMFVideoDisplayControl *iface)
1202 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1203 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1206 static HRESULT WINAPI video_presenter_control_GetNativeVideoSize(IMFVideoDisplayControl *iface, SIZE *video_size,
1207 SIZE *aspect_ratio)
1209 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1211 TRACE("%p, %p, %p.\n", iface, video_size, aspect_ratio);
1213 if (!video_size && !aspect_ratio)
1214 return E_POINTER;
1216 EnterCriticalSection(&presenter->cs);
1217 if (video_size)
1218 *video_size = presenter->native_size;
1219 if (aspect_ratio)
1220 *aspect_ratio = presenter->native_ratio;
1221 LeaveCriticalSection(&presenter->cs);
1223 return S_OK;
1226 static HRESULT WINAPI video_presenter_control_GetIdealVideoSize(IMFVideoDisplayControl *iface, SIZE *min_size,
1227 SIZE *max_size)
1229 FIXME("%p, %p, %p.\n", iface, min_size, max_size);
1231 return E_NOTIMPL;
1234 static HRESULT WINAPI video_presenter_control_SetVideoPosition(IMFVideoDisplayControl *iface,
1235 const MFVideoNormalizedRect *src_rect, const RECT *dst_rect)
1237 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1238 HRESULT hr = S_OK;
1240 TRACE("%p, %p, %s.\n", iface, src_rect, wine_dbgstr_rect(dst_rect));
1242 if (!src_rect && !dst_rect)
1243 return E_POINTER;
1245 if (src_rect && (src_rect->left < 0.0f || src_rect->top < 0.0f ||
1246 src_rect->right > 1.0f || src_rect->bottom > 1.0f ||
1247 src_rect->left > src_rect->right ||
1248 src_rect->top > src_rect->bottom))
1250 return E_INVALIDARG;
1253 if (dst_rect && (dst_rect->left > dst_rect->right ||
1254 dst_rect->top > dst_rect->bottom))
1255 return E_INVALIDARG;
1257 EnterCriticalSection(&presenter->cs);
1258 if (!presenter->video_window)
1259 hr = E_POINTER;
1260 else
1262 if (src_rect)
1264 if (memcmp(&presenter->src_rect, src_rect, sizeof(*src_rect)))
1266 presenter->src_rect = *src_rect;
1267 video_presenter_set_mixer_rect(presenter);
1270 if (dst_rect)
1271 presenter->dst_rect = *dst_rect;
1273 LeaveCriticalSection(&presenter->cs);
1275 return hr;
1278 static HRESULT WINAPI video_presenter_control_GetVideoPosition(IMFVideoDisplayControl *iface, MFVideoNormalizedRect *src_rect,
1279 RECT *dst_rect)
1281 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1283 TRACE("%p, %p, %p.\n", iface, src_rect, dst_rect);
1285 if (!src_rect || !dst_rect)
1286 return E_POINTER;
1288 EnterCriticalSection(&presenter->cs);
1289 *src_rect = presenter->src_rect;
1290 *dst_rect = presenter->dst_rect;
1291 LeaveCriticalSection(&presenter->cs);
1293 return S_OK;
1296 static HRESULT WINAPI video_presenter_control_SetAspectRatioMode(IMFVideoDisplayControl *iface, DWORD mode)
1298 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1300 TRACE("%p, %#x.\n", iface, mode);
1302 if (mode & ~MFVideoARMode_Mask)
1303 return E_INVALIDARG;
1305 EnterCriticalSection(&presenter->cs);
1306 presenter->ar_mode = mode;
1307 LeaveCriticalSection(&presenter->cs);
1309 return S_OK;
1312 static HRESULT WINAPI video_presenter_control_GetAspectRatioMode(IMFVideoDisplayControl *iface, DWORD *mode)
1314 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1316 TRACE("%p, %p.\n", iface, mode);
1318 if (!mode)
1319 return E_POINTER;
1321 EnterCriticalSection(&presenter->cs);
1322 *mode = presenter->ar_mode;
1323 LeaveCriticalSection(&presenter->cs);
1325 return S_OK;
1328 static HRESULT video_presenter_create_swapchain(struct video_presenter *presenter)
1330 D3DPRESENT_PARAMETERS present_params = { 0 };
1331 IDirect3DDevice9 *d3d_device;
1332 HRESULT hr;
1334 if (SUCCEEDED(hr = video_presenter_get_device(presenter, &d3d_device)))
1336 present_params.hDeviceWindow = presenter->video_window;
1337 present_params.Windowed = TRUE;
1338 present_params.SwapEffect = D3DSWAPEFFECT_COPY;
1339 present_params.Flags = D3DPRESENTFLAG_VIDEO;
1340 present_params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
1341 hr = IDirect3DDevice9_CreateAdditionalSwapChain(d3d_device, &present_params, &presenter->swapchain);
1343 IDirect3DDevice9_Release(d3d_device);
1344 IDirect3DDeviceManager9_UnlockDevice(presenter->device_manager, presenter->hdevice, FALSE);
1347 return hr;
1350 static HRESULT WINAPI video_presenter_control_SetVideoWindow(IMFVideoDisplayControl *iface, HWND window)
1352 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1353 HRESULT hr = S_OK;
1355 TRACE("%p, %p.\n", iface, window);
1357 if (!IsWindow(window))
1358 return E_INVALIDARG;
1360 EnterCriticalSection(&presenter->cs);
1361 if (presenter->video_window != window)
1363 if (presenter->swapchain)
1364 IDirect3DSwapChain9_Release(presenter->swapchain);
1365 presenter->video_window = window;
1366 hr = video_presenter_create_swapchain(presenter);
1368 LeaveCriticalSection(&presenter->cs);
1370 return hr;
1373 static HRESULT WINAPI video_presenter_control_GetVideoWindow(IMFVideoDisplayControl *iface, HWND *window)
1375 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1377 TRACE("%p, %p.\n", iface, window);
1379 if (!window)
1380 return E_POINTER;
1382 EnterCriticalSection(&presenter->cs);
1383 *window = presenter->video_window;
1384 LeaveCriticalSection(&presenter->cs);
1386 return S_OK;
1389 static HRESULT WINAPI video_presenter_control_RepaintVideo(IMFVideoDisplayControl *iface)
1391 FIXME("%p.\n", iface);
1393 return E_NOTIMPL;
1396 static HRESULT WINAPI video_presenter_control_GetCurrentImage(IMFVideoDisplayControl *iface, BITMAPINFOHEADER *header,
1397 BYTE **dib, DWORD *dib_size, LONGLONG *timestamp)
1399 FIXME("%p, %p, %p, %p, %p.\n", iface, header, dib, dib_size, timestamp);
1401 return E_NOTIMPL;
1404 static HRESULT WINAPI video_presenter_control_SetBorderColor(IMFVideoDisplayControl *iface, COLORREF color)
1406 FIXME("%p, %#x.\n", iface, color);
1408 return E_NOTIMPL;
1411 static HRESULT WINAPI video_presenter_control_GetBorderColor(IMFVideoDisplayControl *iface, COLORREF *color)
1413 FIXME("%p, %p.\n", iface, color);
1415 return E_NOTIMPL;
1418 static HRESULT WINAPI video_presenter_control_SetRenderingPrefs(IMFVideoDisplayControl *iface, DWORD flags)
1420 FIXME("%p, %#x.\n", iface, flags);
1422 return E_NOTIMPL;
1425 static HRESULT WINAPI video_presenter_control_GetRenderingPrefs(IMFVideoDisplayControl *iface, DWORD *flags)
1427 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1429 TRACE("%p, %p.\n", iface, flags);
1431 if (!flags)
1432 return E_POINTER;
1434 EnterCriticalSection(&presenter->cs);
1435 *flags = presenter->rendering_prefs;
1436 LeaveCriticalSection(&presenter->cs);
1438 return S_OK;
1441 static HRESULT WINAPI video_presenter_control_SetFullscreen(IMFVideoDisplayControl *iface, BOOL fullscreen)
1443 FIXME("%p, %d.\n", iface, fullscreen);
1445 return E_NOTIMPL;
1448 static HRESULT WINAPI video_presenter_control_GetFullscreen(IMFVideoDisplayControl *iface, BOOL *fullscreen)
1450 FIXME("%p, %p.\n", iface, fullscreen);
1452 return E_NOTIMPL;
1455 static const IMFVideoDisplayControlVtbl video_presenter_control_vtbl =
1457 video_presenter_control_QueryInterface,
1458 video_presenter_control_AddRef,
1459 video_presenter_control_Release,
1460 video_presenter_control_GetNativeVideoSize,
1461 video_presenter_control_GetIdealVideoSize,
1462 video_presenter_control_SetVideoPosition,
1463 video_presenter_control_GetVideoPosition,
1464 video_presenter_control_SetAspectRatioMode,
1465 video_presenter_control_GetAspectRatioMode,
1466 video_presenter_control_SetVideoWindow,
1467 video_presenter_control_GetVideoWindow,
1468 video_presenter_control_RepaintVideo,
1469 video_presenter_control_GetCurrentImage,
1470 video_presenter_control_SetBorderColor,
1471 video_presenter_control_GetBorderColor,
1472 video_presenter_control_SetRenderingPrefs,
1473 video_presenter_control_GetRenderingPrefs,
1474 video_presenter_control_SetFullscreen,
1475 video_presenter_control_GetFullscreen,
1478 static HRESULT WINAPI video_presenter_rate_support_QueryInterface(IMFRateSupport *iface, REFIID riid, void **obj)
1480 struct video_presenter *presenter = impl_from_IMFRateSupport(iface);
1481 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1484 static ULONG WINAPI video_presenter_rate_support_AddRef(IMFRateSupport *iface)
1486 struct video_presenter *presenter = impl_from_IMFRateSupport(iface);
1487 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1490 static ULONG WINAPI video_presenter_rate_support_Release(IMFRateSupport *iface)
1492 struct video_presenter *presenter = impl_from_IMFRateSupport(iface);
1493 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1496 static HRESULT WINAPI video_presenter_rate_support_GetSlowestRate(IMFRateSupport *iface, MFRATE_DIRECTION direction,
1497 BOOL thin, float *rate)
1499 TRACE("%p, %d, %d, %p.\n", iface, direction, thin, rate);
1501 *rate = 0.0f;
1503 return S_OK;
1506 static HRESULT WINAPI video_presenter_rate_support_GetFastestRate(IMFRateSupport *iface, MFRATE_DIRECTION direction,
1507 BOOL thin, float *rate)
1509 return E_NOTIMPL;
1512 static HRESULT WINAPI video_presenter_rate_support_IsRateSupported(IMFRateSupport *iface, BOOL thin, float rate,
1513 float *nearest_supported_rate)
1515 return E_NOTIMPL;
1518 static const IMFRateSupportVtbl video_presenter_rate_support_vtbl =
1520 video_presenter_rate_support_QueryInterface,
1521 video_presenter_rate_support_AddRef,
1522 video_presenter_rate_support_Release,
1523 video_presenter_rate_support_GetSlowestRate,
1524 video_presenter_rate_support_GetFastestRate,
1525 video_presenter_rate_support_IsRateSupported,
1528 static HRESULT WINAPI video_presenter_getservice_QueryInterface(IMFGetService *iface, REFIID riid, void **obj)
1530 struct video_presenter *presenter = impl_from_IMFGetService(iface);
1531 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1534 static ULONG WINAPI video_presenter_getservice_AddRef(IMFGetService *iface)
1536 struct video_presenter *presenter = impl_from_IMFGetService(iface);
1537 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1540 static ULONG WINAPI video_presenter_getservice_Release(IMFGetService *iface)
1542 struct video_presenter *presenter = impl_from_IMFGetService(iface);
1543 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1546 static HRESULT WINAPI video_presenter_getservice_GetService(IMFGetService *iface, REFGUID service, REFIID riid, void **obj)
1548 struct video_presenter *presenter = impl_from_IMFGetService(iface);
1550 TRACE("%p, %s, %s, %p.\n", iface, debugstr_guid(service), debugstr_guid(riid), obj);
1552 if (IsEqualGUID(&MR_VIDEO_ACCELERATION_SERVICE, service))
1553 return IDirect3DDeviceManager9_QueryInterface(presenter->device_manager, riid, obj);
1555 if (IsEqualGUID(&MR_VIDEO_RENDER_SERVICE, service))
1556 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1558 FIXME("Unimplemented service %s.\n", debugstr_guid(service));
1560 return MF_E_UNSUPPORTED_SERVICE;
1563 static const IMFGetServiceVtbl video_presenter_getservice_vtbl =
1565 video_presenter_getservice_QueryInterface,
1566 video_presenter_getservice_AddRef,
1567 video_presenter_getservice_Release,
1568 video_presenter_getservice_GetService,
1571 static HRESULT WINAPI video_presenter_position_mapper_QueryInterface(IMFVideoPositionMapper *iface, REFIID riid, void **obj)
1573 struct video_presenter *presenter = impl_from_IMFVideoPositionMapper(iface);
1574 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1577 static ULONG WINAPI video_presenter_position_mapper_AddRef(IMFVideoPositionMapper *iface)
1579 struct video_presenter *presenter = impl_from_IMFVideoPositionMapper(iface);
1580 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1583 static ULONG WINAPI video_presenter_position_mapper_Release(IMFVideoPositionMapper *iface)
1585 struct video_presenter *presenter = impl_from_IMFVideoPositionMapper(iface);
1586 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1589 static HRESULT WINAPI video_presenter_position_mapper_MapOutputCoordinateToInputStream(IMFVideoPositionMapper *iface,
1590 float x_out, float y_out, DWORD output_stream, DWORD input_stream, float *x_in, float *y_in)
1592 FIXME("%p, %f, %f, %u, %u, %p, %p.\n", iface, x_out, y_out, output_stream, input_stream, x_in, y_in);
1594 return E_NOTIMPL;
1597 static const IMFVideoPositionMapperVtbl video_presenter_position_mapper_vtbl =
1599 video_presenter_position_mapper_QueryInterface,
1600 video_presenter_position_mapper_AddRef,
1601 video_presenter_position_mapper_Release,
1602 video_presenter_position_mapper_MapOutputCoordinateToInputStream,
1605 static HRESULT WINAPI video_presenter_allocator_cb_QueryInterface(IMFVideoSampleAllocatorNotify *iface,
1606 REFIID riid, void **obj)
1608 if (IsEqualIID(riid, &IID_IMFVideoSampleAllocatorNotify) ||
1609 IsEqualIID(riid, &IID_IUnknown))
1611 *obj = iface;
1612 IMFVideoSampleAllocatorNotify_AddRef(iface);
1613 return S_OK;
1616 WARN("Unsupported interface %s.\n", debugstr_guid(riid));
1617 *obj = NULL;
1618 return E_NOINTERFACE;
1621 static ULONG WINAPI video_presenter_allocator_cb_AddRef(IMFVideoSampleAllocatorNotify *iface)
1623 struct video_presenter *presenter = impl_from_IMFVideoSampleAllocatorNotify(iface);
1624 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1627 static ULONG WINAPI video_presenter_allocator_cb_Release(IMFVideoSampleAllocatorNotify *iface)
1629 struct video_presenter *presenter = impl_from_IMFVideoSampleAllocatorNotify(iface);
1630 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1633 static HRESULT WINAPI video_presenter_allocator_cb_NotifyRelease(IMFVideoSampleAllocatorNotify *iface)
1635 struct video_presenter *presenter = impl_from_IMFVideoSampleAllocatorNotify(iface);
1637 /* Release notification is executed under allocator lock, instead of processing samples here
1638 notify streaming thread. */
1639 PostThreadMessageW(presenter->thread.tid, EVRM_PROCESS_INPUT, 0, 0);
1641 return S_OK;
1644 static const IMFVideoSampleAllocatorNotifyVtbl video_presenter_allocator_cb_vtbl =
1646 video_presenter_allocator_cb_QueryInterface,
1647 video_presenter_allocator_cb_AddRef,
1648 video_presenter_allocator_cb_Release,
1649 video_presenter_allocator_cb_NotifyRelease,
1652 static HRESULT WINAPI video_presenter_qualprop_QueryInterface(IQualProp *iface, REFIID riid, void **obj)
1654 struct video_presenter *presenter = impl_from_IQualProp(iface);
1655 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1658 static ULONG WINAPI video_presenter_qualprop_AddRef(IQualProp *iface)
1660 struct video_presenter *presenter = impl_from_IQualProp(iface);
1661 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1664 static ULONG WINAPI video_presenter_qualprop_Release(IQualProp *iface)
1666 struct video_presenter *presenter = impl_from_IQualProp(iface);
1667 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1670 static HRESULT WINAPI video_presenter_qualprop_get_FramesDroppedInRenderer(IQualProp *iface, int *frames)
1672 FIXME("%p, %p stub.\n", iface, frames);
1674 return E_NOTIMPL;
1677 static HRESULT WINAPI video_presenter_qualprop_get_FramesDrawn(IQualProp *iface, int *frames)
1679 FIXME("%p, %p stub.\n", iface, frames);
1681 return E_NOTIMPL;
1684 static HRESULT WINAPI video_presenter_qualprop_get_AvgFrameRate(IQualProp *iface, int *avg_frame_rate)
1686 FIXME("%p, %p stub.\n", iface, avg_frame_rate);
1688 return E_NOTIMPL;
1691 static HRESULT WINAPI video_presenter_qualprop_get_Jitter(IQualProp *iface, int *jitter)
1693 FIXME("%p, %p stub.\n", iface, jitter);
1695 return E_NOTIMPL;
1698 static HRESULT WINAPI video_presenter_qualprop_get_AvgSyncOffset(IQualProp *iface, int *offset)
1700 FIXME("%p, %p stub.\n", iface, offset);
1702 return E_NOTIMPL;
1705 static HRESULT WINAPI video_presenter_qualprop_get_DevSyncOffset(IQualProp *iface, int *devoffset)
1707 FIXME("%p, %p stub.\n", iface, devoffset);
1709 return E_NOTIMPL;
1712 static const IQualPropVtbl video_presenter_qualprop_vtbl =
1714 video_presenter_qualprop_QueryInterface,
1715 video_presenter_qualprop_AddRef,
1716 video_presenter_qualprop_Release,
1717 video_presenter_qualprop_get_FramesDroppedInRenderer,
1718 video_presenter_qualprop_get_FramesDrawn,
1719 video_presenter_qualprop_get_AvgFrameRate,
1720 video_presenter_qualprop_get_Jitter,
1721 video_presenter_qualprop_get_AvgSyncOffset,
1722 video_presenter_qualprop_get_DevSyncOffset,
1725 static HRESULT WINAPI video_presenter_quality_advise_QueryInterface(IMFQualityAdvise *iface, REFIID riid, void **out)
1727 struct video_presenter *presenter = impl_from_IMFQualityAdvise(iface);
1728 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, out);
1731 static ULONG WINAPI video_presenter_quality_advise_AddRef(IMFQualityAdvise *iface)
1733 struct video_presenter *presenter = impl_from_IMFQualityAdvise(iface);
1734 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1737 static ULONG WINAPI video_presenter_quality_advise_Release(IMFQualityAdvise *iface)
1739 struct video_presenter *presenter = impl_from_IMFQualityAdvise(iface);
1740 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1743 static HRESULT WINAPI video_presenter_quality_advise_SetDropMode(IMFQualityAdvise *iface,
1744 MF_QUALITY_DROP_MODE mode)
1746 FIXME("%p, %u.\n", iface, mode);
1748 return E_NOTIMPL;
1751 static HRESULT WINAPI video_presenter_quality_advise_SetQualityLevel(IMFQualityAdvise *iface,
1752 MF_QUALITY_LEVEL level)
1754 FIXME("%p, %u.\n", iface, level);
1756 return E_NOTIMPL;
1759 static HRESULT WINAPI video_presenter_quality_advise_GetDropMode(IMFQualityAdvise *iface,
1760 MF_QUALITY_DROP_MODE *mode)
1762 FIXME("%p, %p.\n", iface, mode);
1764 return E_NOTIMPL;
1767 static HRESULT WINAPI video_presenter_quality_advise_GetQualityLevel(IMFQualityAdvise *iface,
1768 MF_QUALITY_LEVEL *level)
1770 FIXME("%p, %p.\n", iface, level);
1772 return E_NOTIMPL;
1775 static HRESULT WINAPI video_presenter_quality_advise_DropTime(IMFQualityAdvise *iface, LONGLONG interval)
1777 FIXME("%p, %s.\n", iface, wine_dbgstr_longlong(interval));
1779 return E_NOTIMPL;
1782 static const IMFQualityAdviseVtbl video_presenter_quality_advise_vtbl =
1784 video_presenter_quality_advise_QueryInterface,
1785 video_presenter_quality_advise_AddRef,
1786 video_presenter_quality_advise_Release,
1787 video_presenter_quality_advise_SetDropMode,
1788 video_presenter_quality_advise_SetQualityLevel,
1789 video_presenter_quality_advise_GetDropMode,
1790 video_presenter_quality_advise_GetQualityLevel,
1791 video_presenter_quality_advise_DropTime,
1794 static HRESULT WINAPI video_presenter_device_manager_QueryInterface(IDirect3DDeviceManager9 *iface,
1795 REFIID riid, void **obj)
1797 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1798 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1801 static ULONG WINAPI video_presenter_device_manager_AddRef(IDirect3DDeviceManager9 *iface)
1803 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1804 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1807 static ULONG WINAPI video_presenter_device_manager_Release(IDirect3DDeviceManager9 *iface)
1809 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1810 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1813 static HRESULT WINAPI video_presenter_device_manager_ResetDevice(IDirect3DDeviceManager9 *iface,
1814 IDirect3DDevice9 *device, UINT token)
1816 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1817 return IDirect3DDeviceManager9_ResetDevice(presenter->device_manager, device, token);
1820 static HRESULT WINAPI video_presenter_device_manager_OpenDeviceHandle(IDirect3DDeviceManager9 *iface, HANDLE *hdevice)
1822 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1823 return IDirect3DDeviceManager9_OpenDeviceHandle(presenter->device_manager, hdevice);
1826 static HRESULT WINAPI video_presenter_device_manager_CloseDeviceHandle(IDirect3DDeviceManager9 *iface, HANDLE hdevice)
1828 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1829 return IDirect3DDeviceManager9_CloseDeviceHandle(presenter->device_manager, hdevice);
1832 static HRESULT WINAPI video_presenter_device_manager_TestDevice(IDirect3DDeviceManager9 *iface, HANDLE hdevice)
1834 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1835 return IDirect3DDeviceManager9_TestDevice(presenter->device_manager, hdevice);
1838 static HRESULT WINAPI video_presenter_device_manager_LockDevice(IDirect3DDeviceManager9 *iface, HANDLE hdevice,
1839 IDirect3DDevice9 **device, BOOL block)
1841 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1842 return IDirect3DDeviceManager9_LockDevice(presenter->device_manager, hdevice, device, block);
1845 static HRESULT WINAPI video_presenter_device_manager_UnlockDevice(IDirect3DDeviceManager9 *iface, HANDLE hdevice,
1846 BOOL savestate)
1848 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1849 return IDirect3DDeviceManager9_UnlockDevice(presenter->device_manager, hdevice, savestate);
1852 static HRESULT WINAPI video_presenter_device_manager_GetVideoService(IDirect3DDeviceManager9 *iface, HANDLE hdevice,
1853 REFIID riid, void **service)
1855 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1856 return IDirect3DDeviceManager9_GetVideoService(presenter->device_manager, hdevice, riid, service);
1859 static const IDirect3DDeviceManager9Vtbl video_presenter_device_manager_vtbl =
1861 video_presenter_device_manager_QueryInterface,
1862 video_presenter_device_manager_AddRef,
1863 video_presenter_device_manager_Release,
1864 video_presenter_device_manager_ResetDevice,
1865 video_presenter_device_manager_OpenDeviceHandle,
1866 video_presenter_device_manager_CloseDeviceHandle,
1867 video_presenter_device_manager_TestDevice,
1868 video_presenter_device_manager_LockDevice,
1869 video_presenter_device_manager_UnlockDevice,
1870 video_presenter_device_manager_GetVideoService,
1873 HRESULT WINAPI MFCreateVideoPresenter(IUnknown *owner, REFIID riid_device, REFIID riid, void **obj)
1875 TRACE("%p, %s, %s, %p.\n", owner, debugstr_guid(riid_device), debugstr_guid(riid), obj);
1877 *obj = NULL;
1879 if (!IsEqualIID(riid_device, &IID_IDirect3DDevice9))
1880 return E_INVALIDARG;
1882 return CoCreateInstance(&CLSID_MFVideoPresenter9, owner, CLSCTX_INPROC_SERVER, riid, obj);
1885 static HRESULT video_presenter_init_d3d(struct video_presenter *presenter)
1887 D3DPRESENT_PARAMETERS present_params = { 0 };
1888 IDirect3DDevice9 *device;
1889 IDirect3D9 *d3d;
1890 HRESULT hr;
1892 d3d = Direct3DCreate9(D3D_SDK_VERSION);
1894 present_params.BackBufferCount = 1;
1895 present_params.SwapEffect = D3DSWAPEFFECT_COPY;
1896 present_params.hDeviceWindow = GetDesktopWindow();
1897 present_params.Windowed = TRUE;
1898 present_params.Flags = D3DPRESENTFLAG_VIDEO;
1899 present_params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
1900 hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, GetDesktopWindow(),
1901 0, &present_params, &device);
1903 IDirect3D9_Release(d3d);
1905 if (FAILED(hr))
1907 WARN("Failed to create d3d device, hr %#x.\n", hr);
1908 return hr;
1911 hr = IDirect3DDeviceManager9_ResetDevice(presenter->device_manager, device, presenter->reset_token);
1912 IDirect3DDevice9_Release(device);
1913 if (FAILED(hr))
1914 WARN("Failed to set new device for the manager, hr %#x.\n", hr);
1916 if (SUCCEEDED(hr = MFCreateVideoSampleAllocator(&IID_IMFVideoSampleAllocator, (void **)&presenter->allocator)))
1918 hr = IMFVideoSampleAllocator_SetDirectXManager(presenter->allocator, (IUnknown *)presenter->device_manager);
1921 return hr;
1924 HRESULT evr_presenter_create(IUnknown *outer, void **out)
1926 struct video_presenter *object;
1927 HRESULT hr;
1929 *out = NULL;
1931 if (!(object = calloc(1, sizeof(*object))))
1932 return E_OUTOFMEMORY;
1934 object->IMFVideoPresenter_iface.lpVtbl = &video_presenter_vtbl;
1935 object->IMFVideoDeviceID_iface.lpVtbl = &video_presenter_device_id_vtbl;
1936 object->IMFTopologyServiceLookupClient_iface.lpVtbl = &video_presenter_service_client_vtbl;
1937 object->IMFVideoDisplayControl_iface.lpVtbl = &video_presenter_control_vtbl;
1938 object->IMFRateSupport_iface.lpVtbl = &video_presenter_rate_support_vtbl;
1939 object->IMFGetService_iface.lpVtbl = &video_presenter_getservice_vtbl;
1940 object->IMFVideoPositionMapper_iface.lpVtbl = &video_presenter_position_mapper_vtbl;
1941 object->IQualProp_iface.lpVtbl = &video_presenter_qualprop_vtbl;
1942 object->IMFQualityAdvise_iface.lpVtbl = &video_presenter_quality_advise_vtbl;
1943 object->allocator_cb.lpVtbl = &video_presenter_allocator_cb_vtbl;
1944 object->IUnknown_inner.lpVtbl = &video_presenter_inner_vtbl;
1945 object->IDirect3DDeviceManager9_iface.lpVtbl = &video_presenter_device_manager_vtbl;
1946 object->outer_unk = outer ? outer : &object->IUnknown_inner;
1947 object->refcount = 1;
1948 object->src_rect.right = object->src_rect.bottom = 1.0f;
1949 object->ar_mode = MFVideoARMode_PreservePicture | MFVideoARMode_PreservePixel;
1950 object->allocator_capacity = 3;
1951 InitializeCriticalSection(&object->cs);
1953 if (FAILED(hr = DXVA2CreateDirect3DDeviceManager9(&object->reset_token, &object->device_manager)))
1954 goto failed;
1956 if (FAILED(hr = video_presenter_init_d3d(object)))
1957 goto failed;
1959 *out = &object->IUnknown_inner;
1961 return S_OK;
1963 failed:
1965 IUnknown_Release(&object->IUnknown_inner);
1967 return hr;