wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / evr / presenter.c
blob9b7fb5f6f670fd85f7131da564e475d7f7fb65ca
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"
31 #include "wine/heap.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(evr);
35 enum presenter_state
37 PRESENTER_STATE_SHUT_DOWN = 0,
38 PRESENTER_STATE_STARTED,
39 PRESENTER_STATE_STOPPED,
40 PRESENTER_STATE_PAUSED,
43 enum presenter_flags
45 PRESENTER_MIXER_HAS_INPUT = 0x1,
48 enum streaming_thread_message
50 EVRM_STOP = WM_USER,
51 EVRM_PRESENT = WM_USER + 1,
52 EVRM_PROCESS_INPUT = WM_USER + 2,
55 struct sample_queue
57 IMFSample **samples;
58 unsigned int size;
59 unsigned int used;
60 unsigned int front;
61 unsigned int back;
64 struct streaming_thread
66 HANDLE hthread;
67 HANDLE ready_event;
68 DWORD tid;
69 struct sample_queue queue;
72 struct video_presenter
74 IMFVideoPresenter IMFVideoPresenter_iface;
75 IMFVideoDeviceID IMFVideoDeviceID_iface;
76 IMFTopologyServiceLookupClient IMFTopologyServiceLookupClient_iface;
77 IMFVideoDisplayControl IMFVideoDisplayControl_iface;
78 IMFRateSupport IMFRateSupport_iface;
79 IMFGetService IMFGetService_iface;
80 IMFVideoPositionMapper IMFVideoPositionMapper_iface;
81 IQualProp IQualProp_iface;
82 IMFQualityAdvise IMFQualityAdvise_iface;
83 IDirect3DDeviceManager9 IDirect3DDeviceManager9_iface;
84 IMFVideoSampleAllocatorNotify allocator_cb;
85 IUnknown IUnknown_inner;
86 IUnknown *outer_unk;
87 LONG refcount;
89 IMFTransform *mixer;
90 IMFClock *clock;
91 IMediaEventSink *event_sink;
93 IDirect3DDeviceManager9 *device_manager;
94 IDirect3DSwapChain9 *swapchain;
95 HANDLE hdevice;
97 IMFVideoSampleAllocator *allocator;
98 struct streaming_thread thread;
99 unsigned int allocator_capacity;
100 IMFMediaType *media_type;
101 LONGLONG frame_time_threshold;
102 UINT reset_token;
103 HWND video_window;
104 MFVideoNormalizedRect src_rect;
105 RECT dst_rect;
106 DWORD rendering_prefs;
107 SIZE native_size;
108 SIZE native_ratio;
109 unsigned int ar_mode;
110 unsigned int state;
111 unsigned int flags;
112 CRITICAL_SECTION cs;
115 static struct video_presenter *impl_from_IUnknown(IUnknown *iface)
117 return CONTAINING_RECORD(iface, struct video_presenter, IUnknown_inner);
120 static struct video_presenter *impl_from_IMFVideoPresenter(IMFVideoPresenter *iface)
122 return CONTAINING_RECORD(iface, struct video_presenter, IMFVideoPresenter_iface);
125 static struct video_presenter *impl_from_IMFVideoDeviceID(IMFVideoDeviceID *iface)
127 return CONTAINING_RECORD(iface, struct video_presenter, IMFVideoDeviceID_iface);
130 static struct video_presenter *impl_from_IMFTopologyServiceLookupClient(IMFTopologyServiceLookupClient *iface)
132 return CONTAINING_RECORD(iface, struct video_presenter, IMFTopologyServiceLookupClient_iface);
135 static struct video_presenter *impl_from_IMFVideoDisplayControl(IMFVideoDisplayControl *iface)
137 return CONTAINING_RECORD(iface, struct video_presenter, IMFVideoDisplayControl_iface);
140 static struct video_presenter *impl_from_IMFRateSupport(IMFRateSupport *iface)
142 return CONTAINING_RECORD(iface, struct video_presenter, IMFRateSupport_iface);
145 static struct video_presenter *impl_from_IMFGetService(IMFGetService *iface)
147 return CONTAINING_RECORD(iface, struct video_presenter, IMFGetService_iface);
150 static struct video_presenter *impl_from_IMFVideoPositionMapper(IMFVideoPositionMapper *iface)
152 return CONTAINING_RECORD(iface, struct video_presenter, IMFVideoPositionMapper_iface);
155 static struct video_presenter *impl_from_IMFVideoSampleAllocatorNotify(IMFVideoSampleAllocatorNotify *iface)
157 return CONTAINING_RECORD(iface, struct video_presenter, allocator_cb);
160 static struct video_presenter *impl_from_IQualProp(IQualProp *iface)
162 return CONTAINING_RECORD(iface, struct video_presenter, IQualProp_iface);
165 static struct video_presenter *impl_from_IMFQualityAdvise(IMFQualityAdvise *iface)
167 return CONTAINING_RECORD(iface, struct video_presenter, IMFQualityAdvise_iface);
170 static struct video_presenter *impl_from_IDirect3DDeviceManager9(IDirect3DDeviceManager9 *iface)
172 return CONTAINING_RECORD(iface, struct video_presenter, IDirect3DDeviceManager9_iface);
175 static void video_presenter_notify_renderer(struct video_presenter *presenter,
176 LONG event, LONG_PTR param1, LONG_PTR param2)
178 if (presenter->event_sink)
179 IMediaEventSink_Notify(presenter->event_sink, event, param1, param2);
182 static unsigned int get_gcd(unsigned int a, unsigned int b)
184 unsigned int m;
186 while (b)
188 m = a % b;
189 a = b;
190 b = m;
193 return a;
196 static HRESULT video_presenter_get_device(struct video_presenter *presenter, IDirect3DDevice9 **device)
198 HRESULT hr;
200 if (!presenter->hdevice)
202 if (FAILED(hr = IDirect3DDeviceManager9_OpenDeviceHandle(presenter->device_manager, &presenter->hdevice)))
203 return hr;
206 return IDirect3DDeviceManager9_LockDevice(presenter->device_manager, presenter->hdevice, device, TRUE);
209 static void video_presenter_get_native_video_size(struct video_presenter *presenter)
211 IMFMediaType *media_type;
212 UINT64 frame_size = 0;
214 memset(&presenter->native_size, 0, sizeof(presenter->native_size));
215 memset(&presenter->native_ratio, 0, sizeof(presenter->native_ratio));
217 if (!presenter->mixer)
218 return;
220 if (FAILED(IMFTransform_GetInputCurrentType(presenter->mixer, 0, &media_type)))
221 return;
223 if (SUCCEEDED(IMFMediaType_GetUINT64(media_type, &MF_MT_FRAME_SIZE, &frame_size)))
225 unsigned int gcd;
227 presenter->native_size.cx = frame_size >> 32;
228 presenter->native_size.cy = frame_size;
230 if ((gcd = get_gcd(presenter->native_size.cx, presenter->native_size.cy)))
232 presenter->native_ratio.cx = presenter->native_size.cx / gcd;
233 presenter->native_ratio.cy = presenter->native_size.cy / gcd;
237 IMFMediaType_Release(media_type);
240 /* It is important this is called to reset callback too to break circular referencing,
241 when allocator keeps a reference of its container, that created it. */
242 static void video_presenter_set_allocator_callback(struct video_presenter *presenter,
243 IMFVideoSampleAllocatorNotify *notify_cb)
245 IMFVideoSampleAllocatorCallback *cb;
247 IMFVideoSampleAllocator_QueryInterface(presenter->allocator, &IID_IMFVideoSampleAllocatorCallback, (void **)&cb);
248 IMFVideoSampleAllocatorCallback_SetCallback(cb, notify_cb);
249 IMFVideoSampleAllocatorCallback_Release(cb);
252 static void video_presenter_reset_media_type(struct video_presenter *presenter)
254 if (presenter->media_type)
255 IMFMediaType_Release(presenter->media_type);
256 presenter->media_type = NULL;
258 IMFVideoSampleAllocator_UninitializeSampleAllocator(presenter->allocator);
259 video_presenter_set_allocator_callback(presenter, NULL);
262 static HRESULT video_presenter_set_media_type(struct video_presenter *presenter, IMFMediaType *media_type)
264 unsigned int flags;
265 HRESULT hr;
267 if (!media_type)
269 video_presenter_reset_media_type(presenter);
270 return S_OK;
273 if (presenter->media_type && IMFMediaType_IsEqual(presenter->media_type, media_type, &flags) == S_OK)
274 return S_OK;
276 video_presenter_reset_media_type(presenter);
278 if (SUCCEEDED(hr = IMFVideoSampleAllocator_InitializeSampleAllocator(presenter->allocator,
279 presenter->allocator_capacity, media_type)))
281 MFRatio ratio;
282 UINT64 rate, frametime;
284 presenter->media_type = media_type;
285 IMFMediaType_AddRef(presenter->media_type);
287 if (SUCCEEDED(IMFMediaType_GetUINT64(presenter->media_type, &MF_MT_FRAME_RATE, &rate)))
289 ratio.Denominator = rate;
290 ratio.Numerator = rate >> 32;
292 else
294 ratio.Denominator = 1;
295 ratio.Numerator = 30;
298 MFFrameRateToAverageTimePerFrame(ratio.Numerator, ratio.Denominator, &frametime);
299 presenter->frame_time_threshold = frametime / 4;
301 else
302 WARN("Failed to initialize sample allocator, hr %#x.\n", hr);
304 return hr;
307 static HRESULT video_presenter_invalidate_media_type(struct video_presenter *presenter)
309 IMFMediaType *media_type, *candidate_type;
310 unsigned int idx = 0;
311 UINT64 frame_size;
312 MFVideoArea aperture;
313 RECT rect;
314 HRESULT hr;
316 if (FAILED(hr = MFCreateMediaType(&media_type)))
317 return hr;
319 video_presenter_get_native_video_size(presenter);
321 rect = presenter->dst_rect;
322 if (rect.left == 0 && rect.right == 0 && rect.bottom == 0 && rect.top == 0)
324 rect.right = presenter->native_size.cx;
325 rect.bottom = presenter->native_size.cy;
328 aperture.Area.cx = rect.right - rect.left;
329 aperture.Area.cy = rect.bottom - rect.top;
330 aperture.OffsetX.value = 0;
331 aperture.OffsetX.fract = 0;
332 aperture.OffsetY.value = 0;
333 aperture.OffsetY.fract = 0;
334 frame_size = (UINT64)aperture.Area.cx << 32 | aperture.Area.cy;
336 while (SUCCEEDED(hr = IMFTransform_GetOutputAvailableType(presenter->mixer, 0, idx++, &candidate_type)))
338 /* FIXME: check that d3d device supports this format */
340 if (FAILED(hr = IMFMediaType_CopyAllItems(candidate_type, (IMFAttributes *)media_type)))
341 WARN("Failed to clone a media type, hr %#x.\n", hr);
342 IMFMediaType_Release(candidate_type);
344 IMFMediaType_SetUINT64(media_type, &MF_MT_FRAME_SIZE, frame_size);
345 IMFMediaType_SetBlob(media_type, &MF_MT_GEOMETRIC_APERTURE, (UINT8 *)&aperture, sizeof(aperture));
347 hr = IMFTransform_SetOutputType(presenter->mixer, 0, media_type, MFT_SET_TYPE_TEST_ONLY);
349 if (SUCCEEDED(hr))
350 hr = video_presenter_set_media_type(presenter, media_type);
352 if (SUCCEEDED(hr))
353 hr = IMFTransform_SetOutputType(presenter->mixer, 0, media_type, 0);
355 if (SUCCEEDED(hr))
356 break;
359 IMFMediaType_Release(media_type);
361 return hr;
364 static void video_presenter_sample_queue_init(struct video_presenter *presenter)
366 struct sample_queue *queue = &presenter->thread.queue;
368 if (queue->size)
369 return;
371 memset(queue, 0, sizeof(*queue));
372 queue->samples = heap_calloc(presenter->allocator_capacity, sizeof(*queue->samples));
373 queue->size = presenter->allocator_capacity;
374 queue->back = queue->size - 1;
377 static void video_presenter_sample_queue_push(struct video_presenter *presenter, IMFSample *sample)
379 struct sample_queue *queue = &presenter->thread.queue;
381 EnterCriticalSection(&presenter->cs);
382 if (queue->used != queue->size)
384 queue->back = (queue->back + 1) % queue->size;
385 queue->samples[queue->back] = sample;
386 queue->used++;
387 IMFSample_AddRef(sample);
389 LeaveCriticalSection(&presenter->cs);
392 static BOOL video_presenter_sample_queue_pop(struct video_presenter *presenter, IMFSample **sample)
394 struct sample_queue *queue = &presenter->thread.queue;
396 EnterCriticalSection(&presenter->cs);
397 if (queue->used)
399 *sample = queue->samples[queue->front];
400 queue->front = (queue->front + 1) % queue->size;
401 queue->used--;
403 else
404 *sample = NULL;
405 LeaveCriticalSection(&presenter->cs);
407 return *sample != NULL;
410 static HRESULT video_presenter_get_sample_surface(IMFSample *sample, IDirect3DSurface9 **surface)
412 IMFMediaBuffer *buffer;
413 IMFGetService *gs;
414 HRESULT hr;
416 if (FAILED(hr = IMFSample_GetBufferByIndex(sample, 0, &buffer)))
417 return hr;
419 hr = IMFMediaBuffer_QueryInterface(buffer, &IID_IMFGetService, (void **)&gs);
420 IMFMediaBuffer_Release(buffer);
421 if (FAILED(hr))
422 return hr;
424 hr = IMFGetService_GetService(gs, &MR_BUFFER_SERVICE, &IID_IDirect3DSurface9, (void **)surface);
425 IMFGetService_Release(gs);
426 return hr;
429 static void scale_rect(RECT *rect, unsigned int width, unsigned int height, const MFVideoNormalizedRect *scale)
431 if (rect->left == 0.0f && rect->top == 0.0f && rect->right == 1.0f && rect->bottom == 1.0f)
433 SetRect(rect, 0, 0, width, height);
435 else
437 rect->left = width * scale->left;
438 rect->right = width * scale->right;
439 rect->top = height * scale->top;
440 rect->bottom = height * scale->bottom;
444 static void video_presenter_sample_present(struct video_presenter *presenter, IMFSample *sample)
446 IDirect3DSurface9 *surface, *backbuffer;
447 IDirect3DDevice9 *device;
448 D3DSURFACE_DESC desc;
449 RECT dst, src;
450 HRESULT hr;
452 if (!presenter->swapchain)
453 return;
455 if (FAILED(hr = video_presenter_get_sample_surface(sample, &surface)))
457 WARN("Failed to get sample surface, hr %#x.\n", hr);
458 return;
461 if (FAILED(hr = IDirect3DSwapChain9_GetBackBuffer(presenter->swapchain, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer)))
463 WARN("Failed to get a backbuffer, hr %#x.\n", hr);
464 IDirect3DSurface9_Release(surface);
465 return;
468 IDirect3DSwapChain9_GetDevice(presenter->swapchain, &device);
469 IDirect3DDevice9_StretchRect(device, surface, NULL, backbuffer, NULL, D3DTEXF_POINT);
471 IDirect3DSurface9_GetDesc(surface, &desc);
472 scale_rect(&src, desc.Width, desc.Height, &presenter->src_rect);
474 IDirect3DSurface9_GetDesc(backbuffer, &desc);
475 SetRect(&dst, 0, 0, desc.Width, desc.Height);
477 if (presenter->ar_mode & MFVideoARMode_PreservePicture)
479 unsigned int src_width = src.right - src.left, src_height = src.bottom - src.top;
480 unsigned int dst_width = dst.right - dst.left, dst_height = dst.bottom - dst.top;
482 if (src_width * dst_height > dst_width * src_height)
484 /* src is "wider" than dst. */
485 unsigned int dst_center = (dst.top + dst.bottom) / 2;
486 unsigned int scaled_height = src_height * dst_width / src_width;
488 dst.top = dst_center - scaled_height / 2;
489 dst.bottom = dst.top + scaled_height;
491 else if (src_width * dst_height < dst_width * src_height)
493 /* src is "taller" than dst. */
494 unsigned int dst_center = (dst.left + dst.right) / 2;
495 unsigned int scaled_width = src_width * dst_height / src_height;
497 dst.left = dst_center - scaled_width / 2;
498 dst.right = dst.left + scaled_width;
502 IDirect3DSwapChain9_Present(presenter->swapchain, &src, &dst, NULL, NULL, 0);
504 IDirect3DDevice9_Release(device);
505 IDirect3DSurface9_Release(backbuffer);
506 IDirect3DSurface9_Release(surface);
509 static void video_presenter_check_queue(struct video_presenter *presenter,
510 unsigned int *next_wait)
512 LONGLONG pts, clocktime, delta;
513 unsigned int wait = 0;
514 BOOL present = TRUE;
515 IMFSample *sample;
516 MFTIME systime;
517 HRESULT hr;
519 while (video_presenter_sample_queue_pop(presenter, &sample))
521 wait = 0;
523 if (presenter->clock)
525 pts = clocktime = 0;
527 hr = IMFSample_GetSampleTime(sample, &pts);
528 if (SUCCEEDED(hr))
529 hr = IMFClock_GetCorrelatedTime(presenter->clock, 0, &clocktime, &systime);
531 delta = pts - clocktime;
532 if (delta > 3 * presenter->frame_time_threshold)
534 /* Convert 100ns -> msec */
535 wait = (delta - 3 * presenter->frame_time_threshold) / 100000;
536 present = FALSE;
540 if (present)
541 video_presenter_sample_present(presenter, sample);
542 else
543 video_presenter_sample_queue_push(presenter, sample);
545 IMFSample_Release(sample);
547 if (wait > 0)
548 break;
551 if (!wait)
552 wait = INFINITE;
554 *next_wait = wait;
557 static void video_presenter_schedule_sample(struct video_presenter *presenter, IMFSample *sample)
559 if (!presenter->thread.tid)
561 WARN("Streaming thread hasn't been started.\n");
562 return;
565 if (presenter->clock)
567 video_presenter_sample_queue_push(presenter, sample);
568 PostThreadMessageW(presenter->thread.tid, EVRM_PRESENT, 0, 0);
570 else
572 video_presenter_sample_present(presenter, sample);
576 static HRESULT video_presenter_process_input(struct video_presenter *presenter)
578 MFT_OUTPUT_DATA_BUFFER buffer;
579 HRESULT hr = S_OK;
580 IMFSample *sample;
581 DWORD status;
583 if (!presenter->media_type)
584 return S_OK;
586 while (hr == S_OK)
588 LONGLONG mixing_started, mixing_finished;
589 MFTIME systime;
591 if (!(presenter->flags & PRESENTER_MIXER_HAS_INPUT))
592 break;
594 if (FAILED(hr = IMFVideoSampleAllocator_AllocateSample(presenter->allocator, &sample)))
596 WARN("Failed to allocate a sample, hr %#x.\n", hr);
597 break;
600 memset(&buffer, 0, sizeof(buffer));
601 buffer.pSample = sample;
603 if (presenter->clock)
604 IMFClock_GetCorrelatedTime(presenter->clock, 0, &mixing_started, &systime);
606 if (FAILED(hr = IMFTransform_ProcessOutput(presenter->mixer, 0, 1, &buffer, &status)))
608 /* FIXME: failure path probably needs to handle some errors specifically */
609 presenter->flags &= ~PRESENTER_MIXER_HAS_INPUT;
610 IMFSample_Release(sample);
611 break;
613 else
615 if (presenter->clock)
617 LONGLONG latency;
619 IMFClock_GetCorrelatedTime(presenter->clock, 0, &mixing_finished, &systime);
620 latency = mixing_finished - mixing_started;
621 video_presenter_notify_renderer(presenter, EC_PROCESSING_LATENCY, (LONG_PTR)&latency, 0);
624 if (buffer.pEvents)
625 IMFCollection_Release(buffer.pEvents);
627 video_presenter_schedule_sample(presenter, sample);
629 IMFSample_Release(sample);
633 return S_OK;
636 static DWORD CALLBACK video_presenter_streaming_thread(void *arg)
638 struct video_presenter *presenter = arg;
639 unsigned int wait = INFINITE;
640 BOOL stop_thread = FALSE;
641 MSG msg;
643 PeekMessageW(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
645 SetEvent(presenter->thread.ready_event);
647 while (!stop_thread)
649 if (MsgWaitForMultipleObjects(0, NULL, FALSE, wait, QS_POSTMESSAGE) == WAIT_TIMEOUT)
650 video_presenter_check_queue(presenter, &wait);
652 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
654 BOOL peek = TRUE;
656 switch (msg.message)
658 case EVRM_STOP:
659 stop_thread = TRUE;
660 break;
662 case EVRM_PRESENT:
663 if (peek)
665 video_presenter_check_queue(presenter, &wait);
666 peek = wait != INFINITE;
668 break;
670 case EVRM_PROCESS_INPUT:
671 EnterCriticalSection(&presenter->cs);
672 video_presenter_process_input(presenter);
673 LeaveCriticalSection(&presenter->cs);
674 break;
675 default:
681 return 0;
684 static HRESULT video_presenter_start_streaming(struct video_presenter *presenter)
686 if (presenter->thread.hthread)
687 return S_OK;
689 video_presenter_sample_queue_init(presenter);
691 if (!(presenter->thread.ready_event = CreateEventW(NULL, FALSE, FALSE, NULL)))
692 return HRESULT_FROM_WIN32(GetLastError());
694 if (!(presenter->thread.hthread = CreateThread(NULL, 0, video_presenter_streaming_thread,
695 presenter, 0, &presenter->thread.tid)))
697 WARN("Failed to create streaming thread.\n");
698 CloseHandle(presenter->thread.ready_event);
699 presenter->thread.ready_event = NULL;
700 return E_FAIL;
703 video_presenter_set_allocator_callback(presenter, &presenter->allocator_cb);
705 WaitForSingleObject(presenter->thread.ready_event, INFINITE);
706 CloseHandle(presenter->thread.ready_event);
707 presenter->thread.ready_event = NULL;
709 TRACE("Started streaming thread, tid %#x.\n", presenter->thread.tid);
711 return S_OK;
714 static HRESULT video_presenter_end_streaming(struct video_presenter *presenter)
716 if (!presenter->thread.hthread)
717 return S_OK;
719 PostThreadMessageW(presenter->thread.tid, EVRM_STOP, 0, 0);
721 WaitForSingleObject(presenter->thread.hthread, INFINITE);
722 CloseHandle(presenter->thread.hthread);
724 TRACE("Terminated streaming thread tid %#x.\n", presenter->thread.tid);
726 memset(&presenter->thread, 0, sizeof(presenter->thread));
727 video_presenter_set_allocator_callback(presenter, NULL);
729 return S_OK;
732 static HRESULT WINAPI video_presenter_inner_QueryInterface(IUnknown *iface, REFIID riid, void **obj)
734 struct video_presenter *presenter = impl_from_IUnknown(iface);
736 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), obj);
738 if (IsEqualIID(riid, &IID_IUnknown))
740 *obj = iface;
742 else if (IsEqualIID(riid, &IID_IMFClockStateSink)
743 || IsEqualIID(riid, &IID_IMFVideoPresenter))
745 *obj = &presenter->IMFVideoPresenter_iface;
747 else if (IsEqualIID(riid, &IID_IMFVideoDeviceID))
749 *obj = &presenter->IMFVideoDeviceID_iface;
751 else if (IsEqualIID(riid, &IID_IMFTopologyServiceLookupClient))
753 *obj = &presenter->IMFTopologyServiceLookupClient_iface;
755 else if (IsEqualIID(riid, &IID_IMFVideoDisplayControl))
757 *obj = &presenter->IMFVideoDisplayControl_iface;
759 else if (IsEqualIID(riid, &IID_IMFRateSupport))
761 *obj = &presenter->IMFRateSupport_iface;
763 else if (IsEqualIID(riid, &IID_IMFGetService))
765 *obj = &presenter->IMFGetService_iface;
767 else if (IsEqualIID(riid, &IID_IMFVideoPositionMapper))
769 *obj = &presenter->IMFVideoPositionMapper_iface;
771 else if (IsEqualIID(riid, &IID_IQualProp))
773 *obj = &presenter->IQualProp_iface;
775 else if (IsEqualIID(riid, &IID_IMFQualityAdvise))
777 *obj = &presenter->IMFQualityAdvise_iface;
779 else if (IsEqualIID(riid, &IID_IDirect3DDeviceManager9))
781 *obj = &presenter->IDirect3DDeviceManager9_iface;
783 else
785 WARN("Unimplemented interface %s.\n", debugstr_guid(riid));
786 *obj = NULL;
787 return E_NOINTERFACE;
790 IUnknown_AddRef((IUnknown *)*obj);
791 return S_OK;
794 static ULONG WINAPI video_presenter_inner_AddRef(IUnknown *iface)
796 struct video_presenter *presenter = impl_from_IUnknown(iface);
797 ULONG refcount = InterlockedIncrement(&presenter->refcount);
799 TRACE("%p, refcount %u.\n", iface, refcount);
801 return refcount;
804 static void video_presenter_clear_container(struct video_presenter *presenter)
806 if (presenter->clock)
807 IMFClock_Release(presenter->clock);
808 if (presenter->mixer)
809 IMFTransform_Release(presenter->mixer);
810 if (presenter->event_sink)
811 IMediaEventSink_Release(presenter->event_sink);
812 presenter->clock = NULL;
813 presenter->mixer = NULL;
814 presenter->event_sink = NULL;
817 static ULONG WINAPI video_presenter_inner_Release(IUnknown *iface)
819 struct video_presenter *presenter = impl_from_IUnknown(iface);
820 ULONG refcount = InterlockedDecrement(&presenter->refcount);
822 TRACE("%p, refcount %u.\n", iface, refcount);
824 if (!refcount)
826 video_presenter_end_streaming(presenter);
827 video_presenter_clear_container(presenter);
828 video_presenter_reset_media_type(presenter);
829 DeleteCriticalSection(&presenter->cs);
830 if (presenter->swapchain)
831 IDirect3DSwapChain9_Release(presenter->swapchain);
832 if (presenter->device_manager)
834 IDirect3DDeviceManager9_CloseDeviceHandle(presenter->device_manager, presenter->hdevice);
835 IDirect3DDeviceManager9_Release(presenter->device_manager);
837 if (presenter->allocator)
838 IMFVideoSampleAllocator_Release(presenter->allocator);
839 heap_free(presenter);
842 return refcount;
845 static const IUnknownVtbl video_presenter_inner_vtbl =
847 video_presenter_inner_QueryInterface,
848 video_presenter_inner_AddRef,
849 video_presenter_inner_Release,
852 static HRESULT WINAPI video_presenter_QueryInterface(IMFVideoPresenter *iface, REFIID riid, void **obj)
854 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
855 return IUnknown_QueryInterface(presenter->outer_unk, riid, obj);
858 static ULONG WINAPI video_presenter_AddRef(IMFVideoPresenter *iface)
860 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
861 return IUnknown_AddRef(presenter->outer_unk);
864 static ULONG WINAPI video_presenter_Release(IMFVideoPresenter *iface)
866 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
867 return IUnknown_Release(presenter->outer_unk);
870 static HRESULT WINAPI video_presenter_OnClockStart(IMFVideoPresenter *iface, MFTIME systime, LONGLONG offset)
872 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
874 TRACE("%p, %s, %s.\n", iface, debugstr_time(systime), wine_dbgstr_longlong(offset));
876 EnterCriticalSection(&presenter->cs);
877 presenter->state = PRESENTER_STATE_STARTED;
878 LeaveCriticalSection(&presenter->cs);
880 return S_OK;
883 static HRESULT WINAPI video_presenter_OnClockStop(IMFVideoPresenter *iface, MFTIME systime)
885 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
887 TRACE("%p, %s.\n", iface, debugstr_time(systime));
889 EnterCriticalSection(&presenter->cs);
890 presenter->state = PRESENTER_STATE_STOPPED;
891 LeaveCriticalSection(&presenter->cs);
893 return S_OK;
896 static HRESULT WINAPI video_presenter_OnClockPause(IMFVideoPresenter *iface, MFTIME systime)
898 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
900 TRACE("%p, %s.\n", iface, debugstr_time(systime));
902 EnterCriticalSection(&presenter->cs);
903 presenter->state = PRESENTER_STATE_PAUSED;
904 LeaveCriticalSection(&presenter->cs);
906 return S_OK;
909 static HRESULT WINAPI video_presenter_OnClockRestart(IMFVideoPresenter *iface, MFTIME systime)
911 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
913 TRACE("%p, %s.\n", iface, debugstr_time(systime));
915 EnterCriticalSection(&presenter->cs);
916 presenter->state = PRESENTER_STATE_STARTED;
917 LeaveCriticalSection(&presenter->cs);
919 return S_OK;
922 static HRESULT WINAPI video_presenter_OnClockSetRate(IMFVideoPresenter *iface, MFTIME systime, float rate)
924 FIXME("%p, %s, %f.\n", iface, debugstr_time(systime), rate);
926 return E_NOTIMPL;
929 static HRESULT WINAPI video_presenter_ProcessMessage(IMFVideoPresenter *iface, MFVP_MESSAGE_TYPE message, ULONG_PTR param)
931 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
932 HRESULT hr;
934 TRACE("%p, %d, %lu.\n", iface, message, param);
936 EnterCriticalSection(&presenter->cs);
938 switch (message)
940 case MFVP_MESSAGE_INVALIDATEMEDIATYPE:
941 hr = video_presenter_invalidate_media_type(presenter);
942 break;
943 case MFVP_MESSAGE_BEGINSTREAMING:
944 hr = video_presenter_start_streaming(presenter);
945 break;
946 case MFVP_MESSAGE_ENDSTREAMING:
947 hr = video_presenter_end_streaming(presenter);
948 break;
949 case MFVP_MESSAGE_PROCESSINPUTNOTIFY:
950 presenter->flags |= PRESENTER_MIXER_HAS_INPUT;
951 hr = video_presenter_process_input(presenter);
952 break;
953 default:
954 FIXME("Unsupported message %u.\n", message);
955 hr = E_NOTIMPL;
958 LeaveCriticalSection(&presenter->cs);
960 return hr;
963 static HRESULT WINAPI video_presenter_GetCurrentMediaType(IMFVideoPresenter *iface,
964 IMFVideoMediaType **media_type)
966 struct video_presenter *presenter = impl_from_IMFVideoPresenter(iface);
967 HRESULT hr;
969 TRACE("%p, %p.\n", iface, media_type);
971 EnterCriticalSection(&presenter->cs);
973 if (presenter->state == PRESENTER_STATE_SHUT_DOWN)
974 hr = MF_E_SHUTDOWN;
975 else if (!presenter->media_type)
976 hr = MF_E_NOT_INITIALIZED;
977 else
979 hr = IMFMediaType_QueryInterface(presenter->media_type, &IID_IMFVideoMediaType,
980 (void **)media_type);
983 LeaveCriticalSection(&presenter->cs);
985 return hr;
988 static const IMFVideoPresenterVtbl video_presenter_vtbl =
990 video_presenter_QueryInterface,
991 video_presenter_AddRef,
992 video_presenter_Release,
993 video_presenter_OnClockStart,
994 video_presenter_OnClockStop,
995 video_presenter_OnClockPause,
996 video_presenter_OnClockRestart,
997 video_presenter_OnClockSetRate,
998 video_presenter_ProcessMessage,
999 video_presenter_GetCurrentMediaType,
1002 static HRESULT WINAPI video_presenter_device_id_QueryInterface(IMFVideoDeviceID *iface, REFIID riid, void **obj)
1004 struct video_presenter *presenter = impl_from_IMFVideoDeviceID(iface);
1005 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1008 static ULONG WINAPI video_presenter_device_id_AddRef(IMFVideoDeviceID *iface)
1010 struct video_presenter *presenter = impl_from_IMFVideoDeviceID(iface);
1011 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1014 static ULONG WINAPI video_presenter_device_id_Release(IMFVideoDeviceID *iface)
1016 struct video_presenter *presenter = impl_from_IMFVideoDeviceID(iface);
1017 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1020 static HRESULT WINAPI video_presenter_device_id_GetDeviceID(IMFVideoDeviceID *iface, IID *device_id)
1022 TRACE("%p, %p.\n", iface, device_id);
1024 if (!device_id)
1025 return E_POINTER;
1027 memcpy(device_id, &IID_IDirect3DDevice9, sizeof(*device_id));
1029 return S_OK;
1032 static const IMFVideoDeviceIDVtbl video_presenter_device_id_vtbl =
1034 video_presenter_device_id_QueryInterface,
1035 video_presenter_device_id_AddRef,
1036 video_presenter_device_id_Release,
1037 video_presenter_device_id_GetDeviceID,
1040 static HRESULT WINAPI video_presenter_service_client_QueryInterface(IMFTopologyServiceLookupClient *iface,
1041 REFIID riid, void **obj)
1043 struct video_presenter *presenter = impl_from_IMFTopologyServiceLookupClient(iface);
1044 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1047 static ULONG WINAPI video_presenter_service_client_AddRef(IMFTopologyServiceLookupClient *iface)
1049 struct video_presenter *presenter = impl_from_IMFTopologyServiceLookupClient(iface);
1050 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1053 static ULONG WINAPI video_presenter_service_client_Release(IMFTopologyServiceLookupClient *iface)
1055 struct video_presenter *presenter = impl_from_IMFTopologyServiceLookupClient(iface);
1056 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1059 static void video_presenter_set_mixer_rect(struct video_presenter *presenter)
1061 IMFAttributes *attributes;
1062 HRESULT hr;
1064 if (!presenter->mixer)
1065 return;
1067 if (SUCCEEDED(IMFTransform_GetAttributes(presenter->mixer, &attributes)))
1069 if (FAILED(hr = IMFAttributes_SetBlob(attributes, &VIDEO_ZOOM_RECT, (const UINT8 *)&presenter->src_rect,
1070 sizeof(presenter->src_rect))))
1072 WARN("Failed to set zoom rectangle attribute, hr %#x.\n", hr);
1074 IMFAttributes_Release(attributes);
1078 static HRESULT video_presenter_attach_mixer(struct video_presenter *presenter, IMFTopologyServiceLookup *service_lookup)
1080 IMFVideoDeviceID *device_id;
1081 unsigned int count;
1082 GUID id = { 0 };
1083 HRESULT hr;
1085 count = 1;
1086 if (FAILED(hr = IMFTopologyServiceLookup_LookupService(service_lookup, MF_SERVICE_LOOKUP_GLOBAL, 0,
1087 &MR_VIDEO_MIXER_SERVICE, &IID_IMFTransform, (void **)&presenter->mixer, &count)))
1089 WARN("Failed to get mixer interface, hr %#x.\n", hr);
1090 return hr;
1093 if (SUCCEEDED(hr = IMFTransform_QueryInterface(presenter->mixer, &IID_IMFVideoDeviceID, (void **)&device_id)))
1095 if (SUCCEEDED(hr = IMFVideoDeviceID_GetDeviceID(device_id, &id)))
1097 if (!IsEqualGUID(&id, &IID_IDirect3DDevice9))
1098 hr = MF_E_INVALIDREQUEST;
1101 IMFVideoDeviceID_Release(device_id);
1104 if (FAILED(hr))
1106 IMFTransform_Release(presenter->mixer);
1107 presenter->mixer = NULL;
1110 video_presenter_set_mixer_rect(presenter);
1111 video_presenter_get_native_video_size(presenter);
1113 return hr;
1116 static HRESULT WINAPI video_presenter_service_client_InitServicePointers(IMFTopologyServiceLookupClient *iface,
1117 IMFTopologyServiceLookup *service_lookup)
1119 struct video_presenter *presenter = impl_from_IMFTopologyServiceLookupClient(iface);
1120 unsigned int count;
1121 HRESULT hr = S_OK;
1123 TRACE("%p, %p.\n", iface, service_lookup);
1125 if (!service_lookup)
1126 return E_POINTER;
1128 EnterCriticalSection(&presenter->cs);
1130 if (presenter->state == PRESENTER_STATE_STARTED ||
1131 presenter->state == PRESENTER_STATE_PAUSED)
1133 hr = MF_E_INVALIDREQUEST;
1135 else
1137 video_presenter_clear_container(presenter);
1139 count = 1;
1140 IMFTopologyServiceLookup_LookupService(service_lookup, MF_SERVICE_LOOKUP_GLOBAL, 0,
1141 &MR_VIDEO_RENDER_SERVICE, &IID_IMFClock, (void **)&presenter->clock, &count);
1143 hr = video_presenter_attach_mixer(presenter, service_lookup);
1145 if (SUCCEEDED(hr))
1147 count = 1;
1148 if (FAILED(hr = IMFTopologyServiceLookup_LookupService(service_lookup, MF_SERVICE_LOOKUP_GLOBAL, 0,
1149 &MR_VIDEO_RENDER_SERVICE, &IID_IMediaEventSink, (void **)&presenter->event_sink, &count)))
1151 WARN("Failed to get renderer event sink, hr %#x.\n", hr);
1155 if (SUCCEEDED(hr))
1156 presenter->state = PRESENTER_STATE_STOPPED;
1159 LeaveCriticalSection(&presenter->cs);
1161 return hr;
1164 static HRESULT WINAPI video_presenter_service_client_ReleaseServicePointers(IMFTopologyServiceLookupClient *iface)
1166 struct video_presenter *presenter = impl_from_IMFTopologyServiceLookupClient(iface);
1168 TRACE("%p.\n", iface);
1170 EnterCriticalSection(&presenter->cs);
1172 presenter->state = PRESENTER_STATE_SHUT_DOWN;
1173 video_presenter_clear_container(presenter);
1175 LeaveCriticalSection(&presenter->cs);
1177 return S_OK;
1180 static const IMFTopologyServiceLookupClientVtbl video_presenter_service_client_vtbl =
1182 video_presenter_service_client_QueryInterface,
1183 video_presenter_service_client_AddRef,
1184 video_presenter_service_client_Release,
1185 video_presenter_service_client_InitServicePointers,
1186 video_presenter_service_client_ReleaseServicePointers,
1189 static HRESULT WINAPI video_presenter_control_QueryInterface(IMFVideoDisplayControl *iface, REFIID riid, void **obj)
1191 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1192 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1195 static ULONG WINAPI video_presenter_control_AddRef(IMFVideoDisplayControl *iface)
1197 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1198 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1201 static ULONG WINAPI video_presenter_control_Release(IMFVideoDisplayControl *iface)
1203 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1204 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1207 static HRESULT WINAPI video_presenter_control_GetNativeVideoSize(IMFVideoDisplayControl *iface, SIZE *video_size,
1208 SIZE *aspect_ratio)
1210 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1212 TRACE("%p, %p, %p.\n", iface, video_size, aspect_ratio);
1214 if (!video_size && !aspect_ratio)
1215 return E_POINTER;
1217 EnterCriticalSection(&presenter->cs);
1218 if (video_size)
1219 *video_size = presenter->native_size;
1220 if (aspect_ratio)
1221 *aspect_ratio = presenter->native_ratio;
1222 LeaveCriticalSection(&presenter->cs);
1224 return S_OK;
1227 static HRESULT WINAPI video_presenter_control_GetIdealVideoSize(IMFVideoDisplayControl *iface, SIZE *min_size,
1228 SIZE *max_size)
1230 FIXME("%p, %p, %p.\n", iface, min_size, max_size);
1232 return E_NOTIMPL;
1235 static HRESULT WINAPI video_presenter_control_SetVideoPosition(IMFVideoDisplayControl *iface,
1236 const MFVideoNormalizedRect *src_rect, const RECT *dst_rect)
1238 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1239 HRESULT hr = S_OK;
1241 TRACE("%p, %p, %s.\n", iface, src_rect, wine_dbgstr_rect(dst_rect));
1243 if (!src_rect && !dst_rect)
1244 return E_POINTER;
1246 if (src_rect && (src_rect->left < 0.0f || src_rect->top < 0.0f ||
1247 src_rect->right > 1.0f || src_rect->bottom > 1.0f ||
1248 src_rect->left > src_rect->right ||
1249 src_rect->top > src_rect->bottom))
1251 return E_INVALIDARG;
1254 if (dst_rect && (dst_rect->left > dst_rect->right ||
1255 dst_rect->top > dst_rect->bottom))
1256 return E_INVALIDARG;
1258 EnterCriticalSection(&presenter->cs);
1259 if (!presenter->video_window)
1260 hr = E_POINTER;
1261 else
1263 if (src_rect)
1265 if (memcmp(&presenter->src_rect, src_rect, sizeof(*src_rect)))
1267 presenter->src_rect = *src_rect;
1268 video_presenter_set_mixer_rect(presenter);
1271 if (dst_rect)
1272 presenter->dst_rect = *dst_rect;
1274 LeaveCriticalSection(&presenter->cs);
1276 return hr;
1279 static HRESULT WINAPI video_presenter_control_GetVideoPosition(IMFVideoDisplayControl *iface, MFVideoNormalizedRect *src_rect,
1280 RECT *dst_rect)
1282 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1284 TRACE("%p, %p, %p.\n", iface, src_rect, dst_rect);
1286 if (!src_rect || !dst_rect)
1287 return E_POINTER;
1289 EnterCriticalSection(&presenter->cs);
1290 *src_rect = presenter->src_rect;
1291 *dst_rect = presenter->dst_rect;
1292 LeaveCriticalSection(&presenter->cs);
1294 return S_OK;
1297 static HRESULT WINAPI video_presenter_control_SetAspectRatioMode(IMFVideoDisplayControl *iface, DWORD mode)
1299 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1301 TRACE("%p, %#x.\n", iface, mode);
1303 if (mode & ~MFVideoARMode_Mask)
1304 return E_INVALIDARG;
1306 EnterCriticalSection(&presenter->cs);
1307 presenter->ar_mode = mode;
1308 LeaveCriticalSection(&presenter->cs);
1310 return S_OK;
1313 static HRESULT WINAPI video_presenter_control_GetAspectRatioMode(IMFVideoDisplayControl *iface, DWORD *mode)
1315 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1317 TRACE("%p, %p.\n", iface, mode);
1319 if (!mode)
1320 return E_POINTER;
1322 EnterCriticalSection(&presenter->cs);
1323 *mode = presenter->ar_mode;
1324 LeaveCriticalSection(&presenter->cs);
1326 return S_OK;
1329 static HRESULT video_presenter_create_swapchain(struct video_presenter *presenter)
1331 D3DPRESENT_PARAMETERS present_params = { 0 };
1332 IDirect3DDevice9 *d3d_device;
1333 HRESULT hr;
1335 if (SUCCEEDED(hr = video_presenter_get_device(presenter, &d3d_device)))
1337 present_params.hDeviceWindow = presenter->video_window;
1338 present_params.Windowed = TRUE;
1339 present_params.SwapEffect = D3DSWAPEFFECT_COPY;
1340 present_params.Flags = D3DPRESENTFLAG_VIDEO;
1341 present_params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
1342 hr = IDirect3DDevice9_CreateAdditionalSwapChain(d3d_device, &present_params, &presenter->swapchain);
1344 IDirect3DDevice9_Release(d3d_device);
1345 IDirect3DDeviceManager9_UnlockDevice(presenter->device_manager, presenter->hdevice, FALSE);
1348 return hr;
1351 static HRESULT WINAPI video_presenter_control_SetVideoWindow(IMFVideoDisplayControl *iface, HWND window)
1353 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1354 HRESULT hr = S_OK;
1356 TRACE("%p, %p.\n", iface, window);
1358 if (!IsWindow(window))
1359 return E_INVALIDARG;
1361 EnterCriticalSection(&presenter->cs);
1362 if (presenter->video_window != window)
1364 if (presenter->swapchain)
1365 IDirect3DSwapChain9_Release(presenter->swapchain);
1366 presenter->video_window = window;
1367 hr = video_presenter_create_swapchain(presenter);
1369 LeaveCriticalSection(&presenter->cs);
1371 return hr;
1374 static HRESULT WINAPI video_presenter_control_GetVideoWindow(IMFVideoDisplayControl *iface, HWND *window)
1376 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1378 TRACE("%p, %p.\n", iface, window);
1380 if (!window)
1381 return E_POINTER;
1383 EnterCriticalSection(&presenter->cs);
1384 *window = presenter->video_window;
1385 LeaveCriticalSection(&presenter->cs);
1387 return S_OK;
1390 static HRESULT WINAPI video_presenter_control_RepaintVideo(IMFVideoDisplayControl *iface)
1392 FIXME("%p.\n", iface);
1394 return E_NOTIMPL;
1397 static HRESULT WINAPI video_presenter_control_GetCurrentImage(IMFVideoDisplayControl *iface, BITMAPINFOHEADER *header,
1398 BYTE **dib, DWORD *dib_size, LONGLONG *timestamp)
1400 FIXME("%p, %p, %p, %p, %p.\n", iface, header, dib, dib_size, timestamp);
1402 return E_NOTIMPL;
1405 static HRESULT WINAPI video_presenter_control_SetBorderColor(IMFVideoDisplayControl *iface, COLORREF color)
1407 FIXME("%p, %#x.\n", iface, color);
1409 return E_NOTIMPL;
1412 static HRESULT WINAPI video_presenter_control_GetBorderColor(IMFVideoDisplayControl *iface, COLORREF *color)
1414 FIXME("%p, %p.\n", iface, color);
1416 return E_NOTIMPL;
1419 static HRESULT WINAPI video_presenter_control_SetRenderingPrefs(IMFVideoDisplayControl *iface, DWORD flags)
1421 FIXME("%p, %#x.\n", iface, flags);
1423 return E_NOTIMPL;
1426 static HRESULT WINAPI video_presenter_control_GetRenderingPrefs(IMFVideoDisplayControl *iface, DWORD *flags)
1428 struct video_presenter *presenter = impl_from_IMFVideoDisplayControl(iface);
1430 TRACE("%p, %p.\n", iface, flags);
1432 if (!flags)
1433 return E_POINTER;
1435 EnterCriticalSection(&presenter->cs);
1436 *flags = presenter->rendering_prefs;
1437 LeaveCriticalSection(&presenter->cs);
1439 return S_OK;
1442 static HRESULT WINAPI video_presenter_control_SetFullscreen(IMFVideoDisplayControl *iface, BOOL fullscreen)
1444 FIXME("%p, %d.\n", iface, fullscreen);
1446 return E_NOTIMPL;
1449 static HRESULT WINAPI video_presenter_control_GetFullscreen(IMFVideoDisplayControl *iface, BOOL *fullscreen)
1451 FIXME("%p, %p.\n", iface, fullscreen);
1453 return E_NOTIMPL;
1456 static const IMFVideoDisplayControlVtbl video_presenter_control_vtbl =
1458 video_presenter_control_QueryInterface,
1459 video_presenter_control_AddRef,
1460 video_presenter_control_Release,
1461 video_presenter_control_GetNativeVideoSize,
1462 video_presenter_control_GetIdealVideoSize,
1463 video_presenter_control_SetVideoPosition,
1464 video_presenter_control_GetVideoPosition,
1465 video_presenter_control_SetAspectRatioMode,
1466 video_presenter_control_GetAspectRatioMode,
1467 video_presenter_control_SetVideoWindow,
1468 video_presenter_control_GetVideoWindow,
1469 video_presenter_control_RepaintVideo,
1470 video_presenter_control_GetCurrentImage,
1471 video_presenter_control_SetBorderColor,
1472 video_presenter_control_GetBorderColor,
1473 video_presenter_control_SetRenderingPrefs,
1474 video_presenter_control_GetRenderingPrefs,
1475 video_presenter_control_SetFullscreen,
1476 video_presenter_control_GetFullscreen,
1479 static HRESULT WINAPI video_presenter_rate_support_QueryInterface(IMFRateSupport *iface, REFIID riid, void **obj)
1481 struct video_presenter *presenter = impl_from_IMFRateSupport(iface);
1482 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1485 static ULONG WINAPI video_presenter_rate_support_AddRef(IMFRateSupport *iface)
1487 struct video_presenter *presenter = impl_from_IMFRateSupport(iface);
1488 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1491 static ULONG WINAPI video_presenter_rate_support_Release(IMFRateSupport *iface)
1493 struct video_presenter *presenter = impl_from_IMFRateSupport(iface);
1494 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1497 static HRESULT WINAPI video_presenter_rate_support_GetSlowestRate(IMFRateSupport *iface, MFRATE_DIRECTION direction,
1498 BOOL thin, float *rate)
1500 TRACE("%p, %d, %d, %p.\n", iface, direction, thin, rate);
1502 *rate = 0.0f;
1504 return S_OK;
1507 static HRESULT WINAPI video_presenter_rate_support_GetFastestRate(IMFRateSupport *iface, MFRATE_DIRECTION direction,
1508 BOOL thin, float *rate)
1510 return E_NOTIMPL;
1513 static HRESULT WINAPI video_presenter_rate_support_IsRateSupported(IMFRateSupport *iface, BOOL thin, float rate,
1514 float *nearest_supported_rate)
1516 return E_NOTIMPL;
1519 static const IMFRateSupportVtbl video_presenter_rate_support_vtbl =
1521 video_presenter_rate_support_QueryInterface,
1522 video_presenter_rate_support_AddRef,
1523 video_presenter_rate_support_Release,
1524 video_presenter_rate_support_GetSlowestRate,
1525 video_presenter_rate_support_GetFastestRate,
1526 video_presenter_rate_support_IsRateSupported,
1529 static HRESULT WINAPI video_presenter_getservice_QueryInterface(IMFGetService *iface, REFIID riid, void **obj)
1531 struct video_presenter *presenter = impl_from_IMFGetService(iface);
1532 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1535 static ULONG WINAPI video_presenter_getservice_AddRef(IMFGetService *iface)
1537 struct video_presenter *presenter = impl_from_IMFGetService(iface);
1538 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1541 static ULONG WINAPI video_presenter_getservice_Release(IMFGetService *iface)
1543 struct video_presenter *presenter = impl_from_IMFGetService(iface);
1544 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1547 static HRESULT WINAPI video_presenter_getservice_GetService(IMFGetService *iface, REFGUID service, REFIID riid, void **obj)
1549 struct video_presenter *presenter = impl_from_IMFGetService(iface);
1551 TRACE("%p, %s, %s, %p.\n", iface, debugstr_guid(service), debugstr_guid(riid), obj);
1553 if (IsEqualGUID(&MR_VIDEO_ACCELERATION_SERVICE, service))
1554 return IDirect3DDeviceManager9_QueryInterface(presenter->device_manager, riid, obj);
1556 if (IsEqualGUID(&MR_VIDEO_RENDER_SERVICE, service))
1557 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1559 FIXME("Unimplemented service %s.\n", debugstr_guid(service));
1561 return MF_E_UNSUPPORTED_SERVICE;
1564 static const IMFGetServiceVtbl video_presenter_getservice_vtbl =
1566 video_presenter_getservice_QueryInterface,
1567 video_presenter_getservice_AddRef,
1568 video_presenter_getservice_Release,
1569 video_presenter_getservice_GetService,
1572 static HRESULT WINAPI video_presenter_position_mapper_QueryInterface(IMFVideoPositionMapper *iface, REFIID riid, void **obj)
1574 struct video_presenter *presenter = impl_from_IMFVideoPositionMapper(iface);
1575 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1578 static ULONG WINAPI video_presenter_position_mapper_AddRef(IMFVideoPositionMapper *iface)
1580 struct video_presenter *presenter = impl_from_IMFVideoPositionMapper(iface);
1581 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1584 static ULONG WINAPI video_presenter_position_mapper_Release(IMFVideoPositionMapper *iface)
1586 struct video_presenter *presenter = impl_from_IMFVideoPositionMapper(iface);
1587 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1590 static HRESULT WINAPI video_presenter_position_mapper_MapOutputCoordinateToInputStream(IMFVideoPositionMapper *iface,
1591 float x_out, float y_out, DWORD output_stream, DWORD input_stream, float *x_in, float *y_in)
1593 FIXME("%p, %f, %f, %u, %u, %p, %p.\n", iface, x_out, y_out, output_stream, input_stream, x_in, y_in);
1595 return E_NOTIMPL;
1598 static const IMFVideoPositionMapperVtbl video_presenter_position_mapper_vtbl =
1600 video_presenter_position_mapper_QueryInterface,
1601 video_presenter_position_mapper_AddRef,
1602 video_presenter_position_mapper_Release,
1603 video_presenter_position_mapper_MapOutputCoordinateToInputStream,
1606 static HRESULT WINAPI video_presenter_allocator_cb_QueryInterface(IMFVideoSampleAllocatorNotify *iface,
1607 REFIID riid, void **obj)
1609 if (IsEqualIID(riid, &IID_IMFVideoSampleAllocatorNotify) ||
1610 IsEqualIID(riid, &IID_IUnknown))
1612 *obj = iface;
1613 IMFVideoSampleAllocatorNotify_AddRef(iface);
1614 return S_OK;
1617 WARN("Unsupported interface %s.\n", debugstr_guid(riid));
1618 *obj = NULL;
1619 return E_NOINTERFACE;
1622 static ULONG WINAPI video_presenter_allocator_cb_AddRef(IMFVideoSampleAllocatorNotify *iface)
1624 struct video_presenter *presenter = impl_from_IMFVideoSampleAllocatorNotify(iface);
1625 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1628 static ULONG WINAPI video_presenter_allocator_cb_Release(IMFVideoSampleAllocatorNotify *iface)
1630 struct video_presenter *presenter = impl_from_IMFVideoSampleAllocatorNotify(iface);
1631 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1634 static HRESULT WINAPI video_presenter_allocator_cb_NotifyRelease(IMFVideoSampleAllocatorNotify *iface)
1636 struct video_presenter *presenter = impl_from_IMFVideoSampleAllocatorNotify(iface);
1638 /* Release notification is executed under allocator lock, instead of processing samples here
1639 notify streaming thread. */
1640 PostThreadMessageW(presenter->thread.tid, EVRM_PROCESS_INPUT, 0, 0);
1642 return S_OK;
1645 static const IMFVideoSampleAllocatorNotifyVtbl video_presenter_allocator_cb_vtbl =
1647 video_presenter_allocator_cb_QueryInterface,
1648 video_presenter_allocator_cb_AddRef,
1649 video_presenter_allocator_cb_Release,
1650 video_presenter_allocator_cb_NotifyRelease,
1653 static HRESULT WINAPI video_presenter_qualprop_QueryInterface(IQualProp *iface, REFIID riid, void **obj)
1655 struct video_presenter *presenter = impl_from_IQualProp(iface);
1656 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1659 static ULONG WINAPI video_presenter_qualprop_AddRef(IQualProp *iface)
1661 struct video_presenter *presenter = impl_from_IQualProp(iface);
1662 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1665 static ULONG WINAPI video_presenter_qualprop_Release(IQualProp *iface)
1667 struct video_presenter *presenter = impl_from_IQualProp(iface);
1668 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1671 static HRESULT WINAPI video_presenter_qualprop_get_FramesDroppedInRenderer(IQualProp *iface, int *frames)
1673 FIXME("%p, %p stub.\n", iface, frames);
1675 return E_NOTIMPL;
1678 static HRESULT WINAPI video_presenter_qualprop_get_FramesDrawn(IQualProp *iface, int *frames)
1680 FIXME("%p, %p stub.\n", iface, frames);
1682 return E_NOTIMPL;
1685 static HRESULT WINAPI video_presenter_qualprop_get_AvgFrameRate(IQualProp *iface, int *avg_frame_rate)
1687 FIXME("%p, %p stub.\n", iface, avg_frame_rate);
1689 return E_NOTIMPL;
1692 static HRESULT WINAPI video_presenter_qualprop_get_Jitter(IQualProp *iface, int *jitter)
1694 FIXME("%p, %p stub.\n", iface, jitter);
1696 return E_NOTIMPL;
1699 static HRESULT WINAPI video_presenter_qualprop_get_AvgSyncOffset(IQualProp *iface, int *offset)
1701 FIXME("%p, %p stub.\n", iface, offset);
1703 return E_NOTIMPL;
1706 static HRESULT WINAPI video_presenter_qualprop_get_DevSyncOffset(IQualProp *iface, int *devoffset)
1708 FIXME("%p, %p stub.\n", iface, devoffset);
1710 return E_NOTIMPL;
1713 static const IQualPropVtbl video_presenter_qualprop_vtbl =
1715 video_presenter_qualprop_QueryInterface,
1716 video_presenter_qualprop_AddRef,
1717 video_presenter_qualprop_Release,
1718 video_presenter_qualprop_get_FramesDroppedInRenderer,
1719 video_presenter_qualprop_get_FramesDrawn,
1720 video_presenter_qualprop_get_AvgFrameRate,
1721 video_presenter_qualprop_get_Jitter,
1722 video_presenter_qualprop_get_AvgSyncOffset,
1723 video_presenter_qualprop_get_DevSyncOffset,
1726 static HRESULT WINAPI video_presenter_quality_advise_QueryInterface(IMFQualityAdvise *iface, REFIID riid, void **out)
1728 struct video_presenter *presenter = impl_from_IMFQualityAdvise(iface);
1729 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, out);
1732 static ULONG WINAPI video_presenter_quality_advise_AddRef(IMFQualityAdvise *iface)
1734 struct video_presenter *presenter = impl_from_IMFQualityAdvise(iface);
1735 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1738 static ULONG WINAPI video_presenter_quality_advise_Release(IMFQualityAdvise *iface)
1740 struct video_presenter *presenter = impl_from_IMFQualityAdvise(iface);
1741 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1744 static HRESULT WINAPI video_presenter_quality_advise_SetDropMode(IMFQualityAdvise *iface,
1745 MF_QUALITY_DROP_MODE mode)
1747 FIXME("%p, %u.\n", iface, mode);
1749 return E_NOTIMPL;
1752 static HRESULT WINAPI video_presenter_quality_advise_SetQualityLevel(IMFQualityAdvise *iface,
1753 MF_QUALITY_LEVEL level)
1755 FIXME("%p, %u.\n", iface, level);
1757 return E_NOTIMPL;
1760 static HRESULT WINAPI video_presenter_quality_advise_GetDropMode(IMFQualityAdvise *iface,
1761 MF_QUALITY_DROP_MODE *mode)
1763 FIXME("%p, %p.\n", iface, mode);
1765 return E_NOTIMPL;
1768 static HRESULT WINAPI video_presenter_quality_advise_GetQualityLevel(IMFQualityAdvise *iface,
1769 MF_QUALITY_LEVEL *level)
1771 FIXME("%p, %p.\n", iface, level);
1773 return E_NOTIMPL;
1776 static HRESULT WINAPI video_presenter_quality_advise_DropTime(IMFQualityAdvise *iface, LONGLONG interval)
1778 FIXME("%p, %s.\n", iface, wine_dbgstr_longlong(interval));
1780 return E_NOTIMPL;
1783 static const IMFQualityAdviseVtbl video_presenter_quality_advise_vtbl =
1785 video_presenter_quality_advise_QueryInterface,
1786 video_presenter_quality_advise_AddRef,
1787 video_presenter_quality_advise_Release,
1788 video_presenter_quality_advise_SetDropMode,
1789 video_presenter_quality_advise_SetQualityLevel,
1790 video_presenter_quality_advise_GetDropMode,
1791 video_presenter_quality_advise_GetQualityLevel,
1792 video_presenter_quality_advise_DropTime,
1795 static HRESULT WINAPI video_presenter_device_manager_QueryInterface(IDirect3DDeviceManager9 *iface,
1796 REFIID riid, void **obj)
1798 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1799 return IMFVideoPresenter_QueryInterface(&presenter->IMFVideoPresenter_iface, riid, obj);
1802 static ULONG WINAPI video_presenter_device_manager_AddRef(IDirect3DDeviceManager9 *iface)
1804 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1805 return IMFVideoPresenter_AddRef(&presenter->IMFVideoPresenter_iface);
1808 static ULONG WINAPI video_presenter_device_manager_Release(IDirect3DDeviceManager9 *iface)
1810 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1811 return IMFVideoPresenter_Release(&presenter->IMFVideoPresenter_iface);
1814 static HRESULT WINAPI video_presenter_device_manager_ResetDevice(IDirect3DDeviceManager9 *iface,
1815 IDirect3DDevice9 *device, UINT token)
1817 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1818 return IDirect3DDeviceManager9_ResetDevice(presenter->device_manager, device, token);
1821 static HRESULT WINAPI video_presenter_device_manager_OpenDeviceHandle(IDirect3DDeviceManager9 *iface, HANDLE *hdevice)
1823 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1824 return IDirect3DDeviceManager9_OpenDeviceHandle(presenter->device_manager, hdevice);
1827 static HRESULT WINAPI video_presenter_device_manager_CloseDeviceHandle(IDirect3DDeviceManager9 *iface, HANDLE hdevice)
1829 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1830 return IDirect3DDeviceManager9_CloseDeviceHandle(presenter->device_manager, hdevice);
1833 static HRESULT WINAPI video_presenter_device_manager_TestDevice(IDirect3DDeviceManager9 *iface, HANDLE hdevice)
1835 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1836 return IDirect3DDeviceManager9_TestDevice(presenter->device_manager, hdevice);
1839 static HRESULT WINAPI video_presenter_device_manager_LockDevice(IDirect3DDeviceManager9 *iface, HANDLE hdevice,
1840 IDirect3DDevice9 **device, BOOL block)
1842 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1843 return IDirect3DDeviceManager9_LockDevice(presenter->device_manager, hdevice, device, block);
1846 static HRESULT WINAPI video_presenter_device_manager_UnlockDevice(IDirect3DDeviceManager9 *iface, HANDLE hdevice,
1847 BOOL savestate)
1849 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1850 return IDirect3DDeviceManager9_UnlockDevice(presenter->device_manager, hdevice, savestate);
1853 static HRESULT WINAPI video_presenter_device_manager_GetVideoService(IDirect3DDeviceManager9 *iface, HANDLE hdevice,
1854 REFIID riid, void **service)
1856 struct video_presenter *presenter = impl_from_IDirect3DDeviceManager9(iface);
1857 return IDirect3DDeviceManager9_GetVideoService(presenter->device_manager, hdevice, riid, service);
1860 static const IDirect3DDeviceManager9Vtbl video_presenter_device_manager_vtbl =
1862 video_presenter_device_manager_QueryInterface,
1863 video_presenter_device_manager_AddRef,
1864 video_presenter_device_manager_Release,
1865 video_presenter_device_manager_ResetDevice,
1866 video_presenter_device_manager_OpenDeviceHandle,
1867 video_presenter_device_manager_CloseDeviceHandle,
1868 video_presenter_device_manager_TestDevice,
1869 video_presenter_device_manager_LockDevice,
1870 video_presenter_device_manager_UnlockDevice,
1871 video_presenter_device_manager_GetVideoService,
1874 HRESULT WINAPI MFCreateVideoPresenter(IUnknown *owner, REFIID riid_device, REFIID riid, void **obj)
1876 TRACE("%p, %s, %s, %p.\n", owner, debugstr_guid(riid_device), debugstr_guid(riid), obj);
1878 *obj = NULL;
1880 if (!IsEqualIID(riid_device, &IID_IDirect3DDevice9))
1881 return E_INVALIDARG;
1883 return CoCreateInstance(&CLSID_MFVideoPresenter9, owner, CLSCTX_INPROC_SERVER, riid, obj);
1886 static HRESULT video_presenter_init_d3d(struct video_presenter *presenter)
1888 D3DPRESENT_PARAMETERS present_params = { 0 };
1889 IDirect3DDevice9 *device;
1890 IDirect3D9 *d3d;
1891 HRESULT hr;
1893 d3d = Direct3DCreate9(D3D_SDK_VERSION);
1895 present_params.BackBufferCount = 1;
1896 present_params.SwapEffect = D3DSWAPEFFECT_COPY;
1897 present_params.hDeviceWindow = GetDesktopWindow();
1898 present_params.Windowed = TRUE;
1899 present_params.Flags = D3DPRESENTFLAG_VIDEO;
1900 present_params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
1901 hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, GetDesktopWindow(),
1902 0, &present_params, &device);
1904 IDirect3D9_Release(d3d);
1906 if (FAILED(hr))
1908 WARN("Failed to create d3d device, hr %#x.\n", hr);
1909 return hr;
1912 hr = IDirect3DDeviceManager9_ResetDevice(presenter->device_manager, device, presenter->reset_token);
1913 IDirect3DDevice9_Release(device);
1914 if (FAILED(hr))
1915 WARN("Failed to set new device for the manager, hr %#x.\n", hr);
1917 if (SUCCEEDED(hr = MFCreateVideoSampleAllocator(&IID_IMFVideoSampleAllocator, (void **)&presenter->allocator)))
1919 hr = IMFVideoSampleAllocator_SetDirectXManager(presenter->allocator, (IUnknown *)presenter->device_manager);
1922 return hr;
1925 HRESULT evr_presenter_create(IUnknown *outer, void **out)
1927 struct video_presenter *object;
1928 HRESULT hr;
1930 *out = NULL;
1932 if (!(object = heap_alloc_zero(sizeof(*object))))
1933 return E_OUTOFMEMORY;
1935 object->IMFVideoPresenter_iface.lpVtbl = &video_presenter_vtbl;
1936 object->IMFVideoDeviceID_iface.lpVtbl = &video_presenter_device_id_vtbl;
1937 object->IMFTopologyServiceLookupClient_iface.lpVtbl = &video_presenter_service_client_vtbl;
1938 object->IMFVideoDisplayControl_iface.lpVtbl = &video_presenter_control_vtbl;
1939 object->IMFRateSupport_iface.lpVtbl = &video_presenter_rate_support_vtbl;
1940 object->IMFGetService_iface.lpVtbl = &video_presenter_getservice_vtbl;
1941 object->IMFVideoPositionMapper_iface.lpVtbl = &video_presenter_position_mapper_vtbl;
1942 object->IQualProp_iface.lpVtbl = &video_presenter_qualprop_vtbl;
1943 object->IMFQualityAdvise_iface.lpVtbl = &video_presenter_quality_advise_vtbl;
1944 object->allocator_cb.lpVtbl = &video_presenter_allocator_cb_vtbl;
1945 object->IUnknown_inner.lpVtbl = &video_presenter_inner_vtbl;
1946 object->IDirect3DDeviceManager9_iface.lpVtbl = &video_presenter_device_manager_vtbl;
1947 object->outer_unk = outer ? outer : &object->IUnknown_inner;
1948 object->refcount = 1;
1949 object->src_rect.right = object->src_rect.bottom = 1.0f;
1950 object->ar_mode = MFVideoARMode_PreservePicture | MFVideoARMode_PreservePixel;
1951 object->allocator_capacity = 3;
1952 InitializeCriticalSection(&object->cs);
1954 if (FAILED(hr = DXVA2CreateDirect3DDeviceManager9(&object->reset_token, &object->device_manager)))
1955 goto failed;
1957 if (FAILED(hr = video_presenter_init_d3d(object)))
1958 goto failed;
1960 *out = &object->IUnknown_inner;
1962 return S_OK;
1964 failed:
1966 IUnknown_Release(&object->IUnknown_inner);
1968 return hr;