2 * Copyright (C) 2008 Pioneers of the Inevitable <songbird@songbirdnest.com>
3 * 2010 FLUENDO S.A. <support@fluendo.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 #include "dshowvideosink.h"
26 #include "dshowvideofakesrc.h"
28 #include <gst/video/video.h>
29 #include <gst/video/videooverlay.h>
30 #include <gst/video/navigation.h>
34 #define WM_GRAPH_NOTIFY WM_APP + 1 /* Private message */
36 GST_DEBUG_CATEGORY (dshowvideosink_debug);
37 #define GST_CAT_DEFAULT dshowvideosink_debug
39 static GstCaps * gst_directshow_media_type_to_caps (AM_MEDIA_TYPE *mediatype);
40 static gboolean gst_caps_to_directshow_media_type (GstDshowVideoSink * sink,
41 GstCaps *caps, AM_MEDIA_TYPE *mediatype);
43 /* TODO: Support RGB! */
44 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
49 "width = (int) [ 1, MAX ],"
50 "height = (int) [ 1, MAX ],"
51 "framerate = (fraction) [ 0, MAX ],"
52 "format = {(string)YUY2, (string)UYVY, (string)YV12 }")
55 static void gst_dshowvideosink_init_interfaces (GType type);
57 static void gst_dshowvideosink_videooverlay_init (GstVideoOverlayInterface *iface);
59 gst_dshowvideosink_navigation_interface_init (GstNavigationInterface * iface);
61 #define gst_dshowvideosink_parent_class parent_class
62 G_DEFINE_TYPE_WITH_CODE (GstDshowVideoSink, gst_dshowvideosink,
64 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY, gst_dshowvideosink_videooverlay_init);
65 G_IMPLEMENT_INTERFACE (GST_TYPE_NAVIGATION, gst_dshowvideosink_navigation_interface_init))
70 PROP_KEEP_ASPECT_RATIO,
76 static void gst_dshowvideosink_finalize (GObject * gobject);
77 static void gst_dshowvideosink_set_property (GObject * object, guint prop_id,
78 const GValue * value, GParamSpec * pspec);
79 static void gst_dshowvideosink_get_property (GObject * object, guint prop_id,
80 GValue * value, GParamSpec * pspec);
82 /* GstElement methods */
83 static GstStateChangeReturn gst_dshowvideosink_change_state (GstElement * element, GstStateChange transition);
85 /* GstBaseSink methods */
86 static gboolean gst_dshowvideosink_start (GstBaseSink * bsink);
87 static gboolean gst_dshowvideosink_stop (GstBaseSink * bsink);
88 static gboolean gst_dshowvideosink_unlock (GstBaseSink * bsink);
89 static gboolean gst_dshowvideosink_unlock_stop (GstBaseSink * bsink);
90 static gboolean gst_dshowvideosink_set_caps (GstBaseSink * bsink, GstCaps * caps);
91 static GstCaps *gst_dshowvideosink_get_caps (GstBaseSink * bsink, GstCaps * filter);
92 static GstFlowReturn gst_dshowvideosink_show_frame (GstVideoSink *sink, GstBuffer *buffer);
93 static void gst_dshowvideosink_set_window_for_renderer (GstDshowVideoSink *sink);
95 /* COM initialization/uninitialization thread */
96 static void gst_dshowvideosink_com_thread (GstDshowVideoSink * sink);
97 /* TODO: event, preroll, buffer_alloc?
98 * buffer_alloc won't generally be all that useful because the renderers require a
99 * different stride to GStreamer's implicit values.
103 gst_dshowvideosink_set_window_handle (GstVideoOverlay * overlay, guintptr window_id)
106 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (overlay);
107 HWND previous_window = sink->window_id;
108 HWND videowindow = (HWND)window_id;
110 if (videowindow == sink->window_id) {
111 GST_DEBUG_OBJECT (sink, "Window already set");
115 sink->window_id = videowindow;
117 /* Update window if we're already playing. */
118 if (sink->connected && sink->filter_media_event) {
121 if (sink->is_new_window) {
122 /* If we created a new window */
123 SendMessage (previous_window, WM_CLOSE, NULL, NULL);
124 sink->is_new_window = FALSE;
125 sink->window_closed = FALSE;
127 /* Return control of application window */
128 SetWindowLongPtr (previous_window, GWLP_WNDPROC, (LONG_PTR)sink->prevWndProc);
129 SetWindowPos (previous_window, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
132 gst_dshowvideosink_set_window_for_renderer (sink);
134 hres = sink->filter_media_event->SetNotifyWindow ((OAHWND)sink->window_id, WM_GRAPH_NOTIFY, 0);
135 GST_DEBUG_OBJECT (sink, "SetNotifyWindow(%p) returned %x", sink->window_id, hres);
140 gst_dshowvideosink_expose (GstVideoOverlay * overlay)
142 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (overlay);
144 if (sink->renderersupport) {
145 sink->renderersupport->PaintWindow ();
150 gst_dshowvideosink_videooverlay_init (GstVideoOverlayInterface * iface)
152 iface->set_window_handle = gst_dshowvideosink_set_window_handle;
153 iface->expose = gst_dshowvideosink_expose;
157 gst_dshowvideosink_navigation_send_event (GstNavigation * navigation,
158 GstStructure * structure)
160 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (navigation);
161 GstEvent *event = NULL;
164 event = gst_event_new_navigation (structure);
166 /* FXIME: handle aspect ratio. */
168 pad = gst_pad_get_peer (GST_VIDEO_SINK_PAD (sink));
170 if (GST_IS_PAD (pad) && GST_IS_EVENT (event)) {
171 gst_pad_send_event (pad, event);
173 gst_object_unref (pad);
178 gst_dshowvideosink_navigation_interface_init (GstNavigationInterface * iface)
180 /* FIXME: navigation interface partially implemented.
181 * Need to call gst_navigation_send_mouse_event and
182 * gst_navigation_send_key_event like in directdrawsink.
184 iface->send_event = gst_dshowvideosink_navigation_send_event;
188 gst_dshowvideosink_class_init (GstDshowVideoSinkClass * klass)
190 GObjectClass *o_class;
191 GstElementClass *e_class;
192 GstBaseSinkClass *bs_class;
193 GstVideoSinkClass *vs_class;
195 o_class = (GObjectClass *) klass;
196 e_class = (GstElementClass *) klass;
197 bs_class = (GstBaseSinkClass *) klass;
198 vs_class = (GstVideoSinkClass *) klass;
200 o_class->finalize = gst_dshowvideosink_finalize;
201 o_class->set_property = gst_dshowvideosink_set_property;
202 o_class->get_property = gst_dshowvideosink_get_property;
204 gst_element_class_set_static_metadata (e_class, "DirectShow video sink",
205 "Sink/Video", "Display data using a DirectShow video renderer",
206 "Pioneers of the Inevitable <songbird@songbirdnest.com>, " \
207 "FLUENDO S.A. <support@fluendo.com>");
209 gst_element_class_add_static_pad_template (e_class, &sink_template);
211 e_class->change_state = GST_DEBUG_FUNCPTR (gst_dshowvideosink_change_state);
213 bs_class->get_caps = GST_DEBUG_FUNCPTR (gst_dshowvideosink_get_caps);
214 bs_class->set_caps = GST_DEBUG_FUNCPTR (gst_dshowvideosink_set_caps);
215 bs_class->start = GST_DEBUG_FUNCPTR (gst_dshowvideosink_start);
216 bs_class->stop = GST_DEBUG_FUNCPTR (gst_dshowvideosink_stop);
217 bs_class->unlock = GST_DEBUG_FUNCPTR (gst_dshowvideosink_unlock);
218 bs_class->unlock_stop =
219 GST_DEBUG_FUNCPTR (gst_dshowvideosink_unlock_stop);
221 vs_class->show_frame = GST_DEBUG_FUNCPTR (gst_dshowvideosink_show_frame);
224 g_object_class_install_property (G_OBJECT_CLASS (klass),
225 PROP_KEEP_ASPECT_RATIO, g_param_spec_boolean ("force-aspect-ratio",
226 "Force aspect ratio",
227 "When enabled, scaling will respect original aspect ratio", TRUE,
228 (GParamFlags)G_PARAM_READWRITE));
229 g_object_class_install_property (G_OBJECT_CLASS (klass),
230 PROP_FULL_SCREEN, g_param_spec_boolean ("fullscreen",
232 "Use full-screen mode (not available when using XOverlay)", FALSE,
233 (GParamFlags)G_PARAM_READWRITE));
235 g_object_class_install_property (G_OBJECT_CLASS (klass),
236 PROP_RENDERER, g_param_spec_string ("renderer", "Renderer",
237 "Force usage of specific DirectShow renderer (EVR, VMR9 or VMR7)",
238 NULL, (GParamFlags)G_PARAM_READWRITE));
242 gst_dshowvideosink_clear (GstDshowVideoSink *sink)
244 sink->renderersupport = NULL;
245 sink->fakesrc = NULL;
246 sink->filter_graph = NULL;
247 sink->filter_media_event = NULL;
249 sink->keep_aspect_ratio = FALSE;
250 sink->full_screen = FALSE;
252 sink->window_closed = FALSE;
253 sink->window_id = NULL;
254 sink->is_new_window = FALSE;
256 sink->connected = FALSE;
257 sink->graph_running = FALSE;
261 gst_dshowvideosink_init (GstDshowVideoSink * sink)
263 gst_dshowvideosink_clear (sink);
265 g_mutex_init (&sink->graph_lock);
266 g_mutex_init (&sink->com_init_lock);
267 g_mutex_init (&sink->com_deinit_lock);
268 g_cond_init (&sink->com_initialized);
269 g_cond_init (&sink->com_uninitialize);
270 g_cond_init (&sink->com_uninitialized);
272 g_mutex_lock (&sink->com_init_lock);
274 /* create the COM initialization thread */
275 g_thread_new ("gstdshowvideosinkcomthread", (GThreadFunc)gst_dshowvideosink_com_thread, sink);
277 /* wait until the COM thread signals that COM has been initialized */
278 g_cond_wait (&sink->com_initialized, &sink->com_init_lock);
279 g_mutex_unlock (&sink->com_init_lock);
283 gst_dshowvideosink_finalize (GObject * gobject)
285 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (gobject);
287 g_free (sink->preferredrenderer);
289 /* signal the COM thread that it sould uninitialize COM */
290 if (sink->comInitialized) {
291 g_mutex_lock (&sink->com_deinit_lock);
292 g_cond_signal (&sink->com_uninitialize);
293 g_cond_wait (&sink->com_uninitialized, &sink->com_deinit_lock);
294 g_mutex_unlock (&sink->com_deinit_lock);
297 g_mutex_clear (&sink->com_init_lock);
298 g_mutex_clear (&sink->com_deinit_lock);
299 g_cond_clear (&sink->com_initialized);
300 g_cond_clear (&sink->com_uninitialize);
301 g_cond_clear (&sink->com_uninitialized);
303 g_mutex_clear (&sink->graph_lock);
305 G_OBJECT_CLASS (parent_class)->finalize (gobject);
309 gst_dshowvideosink_set_property (GObject * object, guint prop_id,
310 const GValue * value, GParamSpec * pspec)
312 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (object);
316 g_free (sink->preferredrenderer);
317 sink->preferredrenderer = g_value_dup_string (value);
319 case PROP_KEEP_ASPECT_RATIO:
320 sink->keep_aspect_ratio = g_value_get_boolean (value);
321 if (sink->renderersupport)
322 sink->renderersupport->SetAspectRatioMode();
324 case PROP_FULL_SCREEN:
325 sink->full_screen = g_value_get_boolean (value);
328 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
334 gst_dshowvideosink_get_property (GObject * object, guint prop_id,
335 GValue * value, GParamSpec * pspec)
337 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (object);
341 g_value_take_string (value, sink->preferredrenderer);
343 case PROP_KEEP_ASPECT_RATIO:
344 g_value_set_boolean (value, sink->keep_aspect_ratio);
346 case PROP_FULL_SCREEN:
347 g_value_set_boolean (value, sink->full_screen);
350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
356 gst_dshowvideosink_com_thread (GstDshowVideoSink * sink)
360 g_mutex_lock (&sink->com_init_lock);
362 /* Initialize COM with a MTA for this process. This thread will
363 * be the first one to enter the apartement and the last one to leave
364 * it, unitializing COM properly */
366 res = CoInitializeEx (0, COINIT_MULTITHREADED);
368 GST_WARNING_OBJECT (sink, "COM has been already initialized in the same process");
369 else if (res == RPC_E_CHANGED_MODE)
370 GST_WARNING_OBJECT (sink, "The concurrency model of COM has changed.");
372 GST_INFO_OBJECT (sink, "COM initialized successfully");
374 sink->comInitialized = TRUE;
376 /* Signal other threads waiting on this condition that COM was initialized */
377 g_cond_signal (&sink->com_initialized);
379 g_mutex_unlock (&sink->com_init_lock);
381 /* Wait until the uninitialize condition is met to leave the COM apartement */
382 g_mutex_lock (&sink->com_deinit_lock);
383 g_cond_wait (&sink->com_uninitialize, &sink->com_deinit_lock);
386 GST_INFO_OBJECT (sink, "COM uninitialized successfully");
387 sink->comInitialized = FALSE;
388 g_cond_signal (&sink->com_uninitialized);
389 g_mutex_unlock (&sink->com_deinit_lock);
393 gst_dshowvideosink_get_caps (GstBaseSink * basesink, GstCaps * filter)
395 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (basesink);
401 static void dump_available_media_types (IPin *pin)
403 /* Enumerate all media types on this pin, output info about them */
404 IEnumMediaTypes *enumerator = NULL;
409 GST_INFO ("Enumerating media types on pin %p", pin);
411 pin->EnumMediaTypes (&enumerator);
413 while (enumerator->Next (1, &type, NULL) == S_OK) {
415 caps = gst_directshow_media_type_to_caps (type);
418 gchar *str = gst_caps_to_string (caps);
419 GST_INFO ("Type %d: converted to caps \"%s\"", i, str);
422 gst_caps_unref (caps);
425 GST_INFO ("Failed to convert type to GstCaps");
427 DeleteMediaType (type);
429 GST_INFO ("Enumeration complete");
431 enumerator->Release();
435 dump_all_pin_media_types (IBaseFilter *filter)
437 IEnumPins *enumpins = NULL;
441 hres = filter->EnumPins (&enumpins);
443 GST_WARNING ("Cannot enumerate pins on filter");
447 GST_INFO ("Enumerating pins on filter %p", filter);
448 while (enumpins->Next (1, &pin, NULL) == S_OK)
450 IMemInputPin *meminputpin;
451 PIN_DIRECTION pindir;
452 hres = pin->QueryDirection (&pindir);
454 GST_INFO ("Found a pin with direction: %s", (pindir == PINDIR_INPUT)? "input": "output");
455 dump_available_media_types (pin);
457 hres = pin->QueryInterface (
458 IID_IMemInputPin, (void **) &meminputpin);
460 GST_INFO ("Pin is a MemInputPin (push mode): %p", meminputpin);
461 meminputpin->Release();
464 GST_INFO ("Pin is not a MemInputPin (pull mode?): %p", pin);
472 gst_dshow_get_pin_from_filter (IBaseFilter *filter, PIN_DIRECTION pindir, IPin **pin)
474 gboolean ret = FALSE;
475 IEnumPins *enumpins = NULL;
480 hres = filter->EnumPins (&enumpins);
485 while (enumpins->Next (1, &pintmp, NULL) == S_OK)
487 PIN_DIRECTION pindirtmp;
488 hres = pintmp->QueryDirection (&pindirtmp);
489 if (hres == S_OK && pindir == pindirtmp) {
496 enumpins->Release ();
502 gst_dshowvideosink_handle_event (GstDshowVideoSink *sink)
504 if (sink->filter_media_event) {
506 LONG_PTR param1, param2;
507 while (SUCCEEDED (sink->filter_media_event->GetEvent(&evCode, ¶m1, ¶m2, 0)))
509 GST_INFO_OBJECT (sink, "Received DirectShow graph event code 0x%x", evCode);
510 sink->filter_media_event->FreeEventParams(evCode, param1, param2);
515 /* WNDPROC for application-supplied windows */
516 LRESULT APIENTRY WndProcHook (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
518 /* Handle certain actions specially on the window passed to us.
519 * Then forward back to the original window.
521 GstDshowVideoSink *sink = (GstDshowVideoSink *)GetProp (hWnd, (LPCSTR)"GstDShowVideoSink");
522 g_assert (sink != NULL);
525 case WM_GRAPH_NOTIFY:
526 gst_dshowvideosink_handle_event (sink);
529 sink->renderersupport->PaintWindow ();
533 sink->renderersupport->MoveWindow ();
535 case WM_DISPLAYCHANGE:
536 sink->renderersupport->DisplayModeChanged();
539 /* DirectShow docs recommend ignoring this message to avoid flicker */
542 sink->window_closed = TRUE;
544 return CallWindowProc (sink->prevWndProc, hWnd, message, wParam, lParam);
547 /* WndProc for our default window, if the application didn't supply one */
549 WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
551 GstDshowVideoSink *sink = (GstDshowVideoSink *)GetWindowLongPtr (hWnd, GWLP_USERDATA);
554 /* I think these happen before we have a chance to set our userdata pointer */
555 GST_DEBUG ("No sink!");
556 return DefWindowProc (hWnd, message, wParam, lParam);
559 //GST_DEBUG_OBJECT (sink, "Got a window message for %x, %x", hWnd, message);
562 case WM_GRAPH_NOTIFY:
563 GST_LOG_OBJECT (sink, "GRAPH_NOTIFY WINDOW MESSAGE");
564 gst_dshowvideosink_handle_event (sink);
567 sink->renderersupport->PaintWindow ();
571 sink->renderersupport->MoveWindow ();
573 case WM_DISPLAYCHANGE:
574 sink->renderersupport->DisplayModeChanged();
577 /* DirectShow docs recommend ignoring this message */
580 sink->renderersupport->DestroyWindow ();
581 sink->window_closed = TRUE;
582 PostQuitMessage (WM_QUIT);
586 return DefWindowProc (hWnd, message, wParam, lParam);
590 gst_dshowvideosink_window_thread (GstDshowVideoSink * sink)
595 DWORD exstyle, style;
597 memset (&WndClass, 0, sizeof (WNDCLASS));
598 WndClass.style = CS_HREDRAW | CS_VREDRAW;
599 WndClass.hInstance = GetModuleHandle (NULL);
600 WndClass.lpszClassName = (LPCSTR)"GST-DShowSink";
601 WndClass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
602 WndClass.cbClsExtra = 0;
603 WndClass.cbWndExtra = 0;
604 WndClass.lpfnWndProc = WndProc;
605 WndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
606 RegisterClass (&WndClass);
608 if (sink->full_screen) {
609 /* This doesn't seem to work, it returns the wrong values! But when we
610 * later use ShowWindow to show it maximized, it goes to full-screen
611 * anyway. TODO: Figure out why. */
612 width = GetSystemMetrics (SM_CXFULLSCREEN);
613 height = GetSystemMetrics (SM_CYFULLSCREEN);
617 style = WS_POPUP; /* No window decorations */
621 /* By default, create a normal top-level window, the size
625 AM_MEDIA_TYPE pmt = (AM_MEDIA_TYPE)sink->mediatype;
626 VIDEOINFOHEADER *vi = (VIDEOINFOHEADER *)pmt.pbFormat;
630 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, ("Unknown media format"), (NULL));
634 /* rcTarget is the aspect-ratio-corrected size of the video. */
635 width = vi->rcTarget.right + GetSystemMetrics (SM_CXSIZEFRAME) * 2;
636 height = vi->rcTarget.bottom + GetSystemMetrics (SM_CYCAPTION) +
637 (GetSystemMetrics (SM_CYSIZEFRAME) * 2);
639 SystemParametersInfo (SPI_GETWORKAREA, NULL, &rect, 0);
640 int screenwidth = rect.right - rect.left;
641 int screenheight = rect.bottom - rect.top;
645 /* Make it fit into the screen without changing the
647 if (width > screenwidth) {
648 double ratio = (double)screenwidth/(double)width;
650 height = (int)(height * ratio);
652 if (height > screenheight) {
653 double ratio = (double)screenheight/(double)height;
654 height = screenheight;
655 width = (int)(width * ratio);
658 style = WS_OVERLAPPEDWINDOW; /* Normal top-level window */
662 HWND video_window = CreateWindowEx (exstyle, (LPCSTR)"GST-DShowSink",
663 (LPCSTR)"GStreamer DirectShow sink default window",
664 style, offx, offy, width, height, NULL, NULL,
665 WndClass.hInstance, NULL);
666 if (video_window == NULL) {
667 GST_ERROR_OBJECT (sink, "Failed to create window!");
671 sink->is_new_window = TRUE;
673 SetWindowLongPtr (video_window, GWLP_USERDATA, (LONG_PTR)sink);
675 sink->window_id = video_window;
677 /* signal application we created a window */
678 gst_video_overlay_got_window_handle (GST_VIDEO_OVERLAY (sink),
679 (gulong)video_window);
681 /* Set the renderer's clipping window */
682 if (!sink->renderersupport->SetRendererWindow (video_window)) {
683 GST_WARNING_OBJECT (sink, "Failed to set video clipping window on filter %p", sink->renderersupport);
686 /* Now show the window, as appropriate */
687 if (sink->full_screen) {
688 ShowWindow (video_window, SW_SHOWMAXIMIZED);
692 ShowWindow (video_window, SW_SHOWNORMAL);
694 /* Trigger the initial paint of the window */
695 UpdateWindow (video_window);
697 ReleaseSemaphore (sink->window_created_signal, 1, NULL);
699 /* start message loop processing our default window messages */
703 if (GetMessage (&msg, video_window, 0, 0) <= 0) {
704 GST_LOG_OBJECT (sink, "our window received WM_QUIT or error.");
707 DispatchMessage (&msg);
714 gst_dshowvideosink_create_default_window (GstDshowVideoSink * sink)
716 sink->window_created_signal = CreateSemaphore (NULL, 0, 1, NULL);
717 if (sink->window_created_signal == NULL)
720 sink -> window_thread = g_thread_new ("windowthread",
721 (GThreadFunc) gst_dshowvideosink_window_thread,
724 /* wait maximum 10 seconds for window to be created */
725 if (WaitForSingleObject (sink->window_created_signal,
726 10000) != WAIT_OBJECT_0)
729 CloseHandle (sink->window_created_signal);
733 CloseHandle (sink->window_created_signal);
734 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
735 ("Error creating our default window"), (NULL));
740 static void gst_dshowvideosink_set_window_for_renderer (GstDshowVideoSink *sink)
742 WNDPROC prevWndProc = (WNDPROC)GetWindowLongPtr (sink->window_id, GWLP_WNDPROC);
743 if (prevWndProc == WndProcHook) {
744 /* The WndProc already points to our hook. Something has gone wrong
745 * somewhere else and this safety net prevents an infinite recursion */
749 /* Application has requested a specific window ID */
750 sink->prevWndProc = (WNDPROC) SetWindowLongPtr (sink->window_id, GWLP_WNDPROC, (LONG_PTR)WndProcHook);
751 GST_DEBUG_OBJECT (sink, "Set wndproc to %p from %p", WndProcHook, sink->prevWndProc);
752 SetProp (sink->window_id, (LPCSTR)"GstDShowVideoSink", sink);
753 /* This causes the new WNDPROC to become active */
754 SetWindowPos (sink->window_id, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
756 if (!sink->renderersupport->SetRendererWindow (sink->window_id)) {
757 GST_WARNING_OBJECT (sink, "Failed to set HWND %x on renderer", sink->window_id);
760 sink->is_new_window = FALSE;
762 /* This tells the renderer where the window is located, needed to
763 * start drawing in the right place. */
764 sink->renderersupport->MoveWindow();
765 GST_INFO_OBJECT (sink, "Set renderer window to %x", sink->window_id);
769 gst_dshowvideosink_prepare_window (GstDshowVideoSink *sink)
773 /* Give the app a last chance to supply a window id */
774 if (!sink->window_id) {
775 gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
778 /* If the app supplied one, use it. Otherwise, go ahead
779 * and create (and use) our own window */
780 if (sink->window_id) {
781 gst_dshowvideosink_set_window_for_renderer (sink);
784 gst_dshowvideosink_create_default_window (sink);
787 if (sink->filter_media_event) {
788 sink->filter_media_event->Release();
789 sink->filter_media_event = NULL;
792 hres = sink->filter_graph->QueryInterface(
793 IID_IMediaEventEx, (void **) &sink->filter_media_event);
796 GST_WARNING_OBJECT (sink, "Failed to get IMediaEventEx");
799 hres = sink->filter_media_event->SetNotifyWindow ((OAHWND)sink->window_id,
801 GST_DEBUG_OBJECT (sink, "SetNotifyWindow(%p) returned %x", sink->window_id, hres);
806 gst_dshowvideosink_connect_graph (GstDshowVideoSink *sink)
812 GST_INFO_OBJECT (sink, "Connecting DirectShow pins");
814 srcpin = sink->fakesrc->GetOutputPin();
816 gst_dshow_get_pin_from_filter (sink->renderersupport->GetFilter(), PINDIR_INPUT,
819 GST_WARNING_OBJECT (sink, "Cannot get input pin from Renderer");
823 /* Be warned that this call WILL deadlock unless you call it from
824 * the main thread. Thus, we call this from the state change, not from
825 * setcaps (which happens in a streaming thread).
827 hres = sink->filter_graph->ConnectDirect (
828 srcpin, sinkpin, NULL);
830 GST_WARNING_OBJECT (sink, "Could not connect pins: %x", hres);
838 static GstStateChangeReturn
839 gst_dshowvideosink_start_graph (GstDshowVideoSink *sink)
841 IMediaControl *control = NULL;
843 GstStateChangeReturn ret;
845 GST_DEBUG_OBJECT (sink, "Connecting and starting DirectShow graph");
847 hres = sink->filter_graph->QueryInterface(
848 IID_IMediaControl, (void **) &control);
851 GST_WARNING_OBJECT (sink, "Failed to get IMediaControl interface");
852 ret = GST_STATE_CHANGE_FAILURE;
856 GST_INFO_OBJECT (sink, "Running DirectShow graph");
857 hres = control->Run();
859 GST_ERROR_OBJECT (sink,
860 "Failed to run the directshow graph (error=%x)", hres);
861 ret = GST_STATE_CHANGE_FAILURE;
865 GST_DEBUG_OBJECT (sink, "DirectShow graph is now running");
866 ret = GST_STATE_CHANGE_SUCCESS;
874 static GstStateChangeReturn
875 gst_dshowvideosink_pause_graph (GstDshowVideoSink *sink)
877 IMediaControl *control = NULL;
878 GstStateChangeReturn ret;
881 hres = sink->filter_graph->QueryInterface(
882 IID_IMediaControl, (void **) &control);
884 GST_WARNING_OBJECT (sink, "Failed to get IMediaControl interface");
885 ret = GST_STATE_CHANGE_FAILURE;
889 GST_INFO_OBJECT (sink, "Pausing DirectShow graph");
890 hres = control->Pause();
892 GST_WARNING_OBJECT (sink,
893 "Can't pause the directshow graph (error=%x)", hres);
894 ret = GST_STATE_CHANGE_FAILURE;
898 ret = GST_STATE_CHANGE_SUCCESS;
907 static GstStateChangeReturn
908 gst_dshowvideosink_stop_graph (GstDshowVideoSink *sink)
910 IMediaControl *control = NULL;
911 GstStateChangeReturn ret;
915 hres = sink->filter_graph->QueryInterface(
916 IID_IMediaControl, (void **) &control);
918 GST_WARNING_OBJECT (sink, "Failed to get IMediaControl interface");
919 ret = GST_STATE_CHANGE_FAILURE;
923 GST_INFO_OBJECT (sink, "Stopping DirectShow graph");
924 hres = control->Stop();
926 GST_WARNING_OBJECT (sink,
927 "Can't stop the directshow graph (error=%x)", hres);
928 ret = GST_STATE_CHANGE_FAILURE;
932 sink->filter_graph->Disconnect(sink->fakesrc->GetOutputPin());
934 gst_dshow_get_pin_from_filter (sink->renderersupport->GetFilter(), PINDIR_INPUT,
936 sink->filter_graph->Disconnect(sinkpin);
939 GST_DEBUG_OBJECT (sink, "DirectShow graph has stopped");
941 if (sink->window_id) {
942 /* Return control of application window */
943 SetWindowLongPtr (sink->window_id, GWLP_WNDPROC, (LONG_PTR)sink->prevWndProc);
944 RemoveProp (sink->window_id, (LPCSTR)"GstDShowVideoSink");
945 SetWindowPos (sink->window_id, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
946 sink->prevWndProc = NULL;
948 sink->connected = FALSE;
950 ret = GST_STATE_CHANGE_SUCCESS;
959 static GstStateChangeReturn
960 gst_dshowvideosink_change_state (GstElement * element, GstStateChange transition)
962 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (element);
963 GstStateChangeReturn ret, rettmp;
965 switch (transition) {
966 case GST_STATE_CHANGE_NULL_TO_READY:
968 case GST_STATE_CHANGE_READY_TO_PAUSED:
970 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
971 ret = gst_dshowvideosink_start_graph (sink);
972 if (ret == GST_STATE_CHANGE_FAILURE)
974 sink->graph_running = TRUE;
980 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
982 switch (transition) {
983 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
984 GST_DSHOWVIDEOSINK_GRAPH_LOCK(sink);
985 rettmp = gst_dshowvideosink_pause_graph (sink);
986 if (rettmp == GST_STATE_CHANGE_FAILURE)
988 sink->graph_running = FALSE;
989 GST_DSHOWVIDEOSINK_GRAPH_UNLOCK(sink);
991 case GST_STATE_CHANGE_PAUSED_TO_READY:
992 GST_DSHOWVIDEOSINK_GRAPH_LOCK(sink);
993 rettmp = gst_dshowvideosink_stop_graph (sink);
994 if (rettmp == GST_STATE_CHANGE_FAILURE)
996 sink->graph_running = FALSE;
997 GST_DSHOWVIDEOSINK_GRAPH_UNLOCK(sink);
999 case GST_STATE_CHANGE_READY_TO_NULL:
1000 gst_dshowvideosink_clear (sink);
1009 class EVRSupport : public RendererSupport
1012 GstDshowVideoSink *sink;
1013 IBaseFilter *filter;
1014 IMFGetService *service;
1015 IMFVideoDisplayControl *control;
1019 EVRSupport (GstDshowVideoSink *sink) :
1036 const char *GetName() {
1037 return "EnhancedVideoRenderer";
1040 IBaseFilter *GetFilter() {
1044 gboolean CheckOS () {
1046 info.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
1047 GetVersionEx (&info);
1049 if (info.dwMajorVersion < 6) {
1057 gboolean Configure() {
1060 if (!this->CheckOS ()) {
1061 GST_DEBUG_OBJECT (sink, "Windows Vista is required at least for EVR to work");
1065 hres = CoCreateInstance (CLSID_EnhancedVideoRenderer, NULL, CLSCTX_INPROC,
1066 IID_IBaseFilter, (LPVOID *) &filter);
1067 GST_DEBUG_OBJECT (sink, "cocreateinstance returned %d", hres);
1068 if (FAILED (hres)) {
1069 GST_ERROR_OBJECT (sink,
1070 "Can't create an instance of renderer (error=%x)",
1075 hres = filter->QueryInterface (IID_IMFGetService,
1076 (void **) &service);
1077 if (FAILED (hres)) {
1078 GST_WARNING_OBJECT (sink, "EVR service interface missing: %x", hres);
1082 hres = service->GetService (MR_VIDEO_RENDER_SERVICE,
1083 IID_IMFVideoDisplayControl, (void **) &control);
1084 if (FAILED (hres)) {
1085 GST_WARNING_OBJECT (sink, "EVR control service missing: %x", hres);
1089 SetAspectRatioMode();
1093 void SetAspectRatioMode() {
1094 if (sink->keep_aspect_ratio) {
1095 control->SetAspectRatioMode(MFVideoARMode_PreservePicture);
1098 control->SetAspectRatioMode(MFVideoARMode_None);
1102 gboolean SetRendererWindow(HWND window) {
1103 video_window = window;
1104 HRESULT hres = control->SetVideoWindow (video_window);
1105 if (FAILED (hres)) {
1106 GST_WARNING_OBJECT (sink, "Failed to set video clipping window on filter %p: %x", filter, hres);
1119 GetClientRect(video_window, &rcClient);
1120 hdc = BeginPaint(video_window, &ps);
1122 hr = control->RepaintVideo();
1124 EndPaint(video_window, &ps);
1132 // Track the movement of the container window and resize as needed
1133 GetClientRect(video_window, &rect);
1134 hr = control->SetVideoPosition(NULL, &rect);
1137 void DisplayModeChanged() {
1140 void DestroyWindow() {
1141 ::DestroyWindow (video_window);
1145 class VMR9Support : public RendererSupport
1148 GstDshowVideoSink *sink;
1149 IBaseFilter *filter;
1150 IVMRWindowlessControl9 *control;
1151 IVMRFilterConfig9 *config;
1155 VMR9Support (GstDshowVideoSink *sink) :
1172 const char *GetName() {
1173 return "VideoMixingRenderer9";
1176 IBaseFilter *GetFilter() {
1180 gboolean Configure() {
1183 hres = CoCreateInstance (CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC,
1184 IID_IBaseFilter, (LPVOID *) &filter);
1185 if (FAILED (hres)) {
1186 GST_ERROR_OBJECT (sink,
1187 "Can't create an instance of renderer (error=%x)",
1192 hres = filter->QueryInterface (
1193 IID_IVMRFilterConfig9, (void **) &config);
1194 if (FAILED (hres)) {
1195 GST_WARNING_OBJECT (sink, "VMR9 filter config interface missing: %x", hres);
1199 hres = config->SetRenderingMode (VMR9Mode_Windowless);
1200 if (FAILED (hres)) {
1201 GST_WARNING_OBJECT (sink, "VMR9 couldn't be set to windowless mode: %x", hres);
1205 GST_DEBUG_OBJECT (sink, "Set VMR9 (%p) to windowless mode!", filter);
1208 /* We can't QI to this until _after_ we've been set to windowless mode.
1209 * Apparently this is against the rules in COM, but that's how it is... */
1210 hres = filter->QueryInterface (
1211 IID_IVMRWindowlessControl9, (void **) &control);
1212 if (FAILED (hres)) {
1213 GST_WARNING_OBJECT (sink, "VMR9 windowless control interface missing: %x", hres);
1217 SetAspectRatioMode();
1221 void SetAspectRatioMode() {
1222 if (sink->keep_aspect_ratio) {
1223 control->SetAspectRatioMode(VMR9ARMode_LetterBox);
1226 control->SetAspectRatioMode(VMR9ARMode_None);
1230 gboolean SetRendererWindow(HWND window) {
1231 video_window = window;
1232 HRESULT hres = control->SetVideoClippingWindow (video_window);
1233 if (FAILED (hres)) {
1234 GST_WARNING_OBJECT (sink, "Failed to set video clipping window on filter %p: %x", filter, hres);
1247 GetClientRect(video_window, &rcClient);
1248 hdc = BeginPaint(video_window, &ps);
1250 hr = control->RepaintVideo(video_window, hdc);
1252 EndPaint(video_window, &ps);
1260 // Track the movement of the container window and resize as needed
1261 GetClientRect(video_window, &rect);
1262 hr = control->SetVideoPosition(NULL, &rect);
1265 void DisplayModeChanged() {
1266 control->DisplayModeChanged();
1269 void DestroyWindow() {
1270 ::DestroyWindow (video_window);
1274 class VMR7Support : public RendererSupport
1277 GstDshowVideoSink *sink;
1278 IBaseFilter *filter;
1279 IVMRWindowlessControl *control;
1280 IVMRFilterConfig *config;
1284 VMR7Support (GstDshowVideoSink *sink) :
1301 const char *GetName() {
1302 return "VideoMixingRenderer";
1305 IBaseFilter *GetFilter() {
1309 gboolean Configure() {
1312 hres = CoCreateInstance (CLSID_VideoMixingRenderer, NULL, CLSCTX_INPROC,
1313 IID_IBaseFilter, (LPVOID *) &filter);
1314 if (FAILED (hres)) {
1315 GST_ERROR_OBJECT (sink,
1316 "Can't create an instance of renderer (error=%x)",
1321 hres = filter->QueryInterface (
1322 IID_IVMRFilterConfig, (void **) &config);
1323 if (FAILED (hres)) {
1324 GST_WARNING_OBJECT (sink, "VMR filter config interface missing: %x", hres);
1328 hres = config->SetRenderingMode (VMRMode_Windowless);
1329 if (FAILED (hres)) {
1330 GST_WARNING_OBJECT (sink, "VMR couldn't be set to windowless mode: %x", hres);
1334 GST_DEBUG_OBJECT (sink, "Set VMR (%p) to windowless mode!", filter);
1337 hres = filter->QueryInterface (
1338 IID_IVMRWindowlessControl, (void **) &control);
1339 if (FAILED (hres)) {
1340 GST_WARNING_OBJECT (sink, "VMR windowless control interface missing: %x", hres);
1344 SetAspectRatioMode();
1348 void SetAspectRatioMode() {
1349 if (sink->keep_aspect_ratio) {
1350 control->SetAspectRatioMode(VMR_ARMODE_LETTER_BOX);
1353 control->SetAspectRatioMode(VMR_ARMODE_NONE);
1357 gboolean SetRendererWindow(HWND window) {
1358 video_window = window;
1359 HRESULT hres = control->SetVideoClippingWindow (video_window);
1360 if (FAILED (hres)) {
1361 GST_WARNING_OBJECT (sink, "Failed to set video clipping window on filter %p: %x", filter, hres);
1374 GetClientRect(video_window, &rcClient);
1375 hdc = BeginPaint(video_window, &ps);
1377 hr = control->RepaintVideo(video_window, hdc);
1379 EndPaint(video_window, &ps);
1387 // Track the movement of the container window and resize as needed
1388 GetClientRect(video_window, &rect);
1389 hr = control->SetVideoPosition(NULL, &rect);
1392 void DisplayModeChanged() {
1393 control->DisplayModeChanged();
1396 void DestroyWindow() {
1397 ::DestroyWindow (video_window);
1402 gst_dshowvideosink_create_renderer (GstDshowVideoSink *sink)
1404 GST_DEBUG_OBJECT (sink, "Trying to create renderer '%s'", "EVR");
1406 RendererSupport *support = NULL;
1408 if (sink->preferredrenderer) {
1409 if (!strcmp (sink->preferredrenderer, "EVR")) {
1410 GST_INFO_OBJECT (sink, "Forcing use of EVR");
1411 support = new EVRSupport (sink);
1413 else if (!strcmp (sink->preferredrenderer, "VMR9")) {
1414 GST_INFO_OBJECT (sink, "Forcing use of VMR9");
1415 support = new VMR9Support (sink);
1417 else if (!strcmp (sink->preferredrenderer, "VMR")) {
1418 GST_INFO_OBJECT (sink, "Forcing use of VMR");
1419 support = new VMR7Support (sink);
1422 GST_ERROR_OBJECT (sink, "Unknown sink type '%s'", sink->preferredrenderer);
1426 if (!support->Configure()) {
1427 GST_ERROR_OBJECT (sink, "Couldn't configure selected renderer");
1434 support = new EVRSupport (sink);
1435 if (!support->Configure ()) {
1436 GST_INFO_OBJECT (sink, "Failed to configure EVR, trying VMR9");
1438 support = new VMR9Support (sink);
1439 if (!support->Configure()) {
1440 GST_INFO_OBJECT (sink, "Failed to configure VMR9, trying VMR7");
1442 support = new VMR7Support (sink);
1443 if (!support->Configure()) {
1444 GST_ERROR_OBJECT (sink, "Failed to configure VMR9 or VMR7");
1452 sink->renderersupport = support;
1457 gst_dshowvideosink_build_filtergraph (GstDshowVideoSink *sink)
1461 /* Build our DirectShow FilterGraph, looking like:
1463 * [ fakesrc ] -> [ sink filter ]
1465 * so we can feed data in through the fakesrc.
1467 * The sink filter can be one of our supported filters: VMR9 (VMR7?, EMR?)
1470 hres = CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC,
1471 IID_IFilterGraph, (LPVOID *) & sink->filter_graph);
1472 if (FAILED (hres)) {
1473 GST_ERROR_OBJECT (sink,
1474 "Can't create an instance of the dshow graph manager (error=%x)", hres);
1478 sink->fakesrc = new VideoFakeSrc();
1480 IBaseFilter *filter;
1481 hres = sink->fakesrc->QueryInterface (
1482 IID_IBaseFilter, (void **) &filter);
1483 if (FAILED (hres)) {
1484 GST_ERROR_OBJECT (sink, "Could not QI fakesrc to IBaseFilter");
1488 hres = sink->filter_graph->AddFilter (filter, L"fakesrc");
1489 if (FAILED (hres)) {
1490 GST_ERROR_OBJECT (sink,
1491 "Can't add our fakesrc filter to the graph (error=%x)", hres);
1495 if (!gst_dshowvideosink_create_renderer (sink)) {
1496 GST_ERROR_OBJECT (sink, "Could not create a video renderer");
1500 /* dump_all_pin_media_types (sink->renderer); */
1503 sink->filter_graph->AddFilter (sink->renderersupport->GetFilter(),
1505 if (FAILED (hres)) {
1506 GST_ERROR_OBJECT (sink,
1507 "Can't add renderer to the graph (error=%x)", hres);
1514 if (sink->fakesrc) {
1515 sink->fakesrc->Release();
1516 sink->fakesrc = NULL;
1519 if (sink->filter_graph) {
1520 sink->filter_graph->Release();
1521 sink->filter_graph = NULL;
1524 if (sink->filter_media_event) {
1525 sink->filter_media_event->Release();
1526 sink->filter_media_event = NULL;
1533 gst_dshowvideosink_start (GstBaseSink * bsink)
1535 HRESULT hres = S_FALSE;
1536 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (bsink);
1538 /* Just build the filtergraph; we don't link or otherwise configure it yet */
1539 return gst_dshowvideosink_build_filtergraph (sink);
1543 gst_dshowvideosink_set_caps (GstBaseSink * bsink, GstCaps * caps)
1545 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (bsink);
1547 if (sink->connected) {
1549 sink->filter_graph->Disconnect(sink->fakesrc->GetOutputPin());
1550 gst_dshow_get_pin_from_filter (sink->renderersupport->GetFilter(), PINDIR_INPUT,
1552 sink->filter_graph->Disconnect(sinkpin);
1556 if (!gst_caps_to_directshow_media_type (sink, caps, &sink->mediatype)) {
1557 GST_WARNING_OBJECT (sink, "Cannot convert caps to AM_MEDIA_TYPE, rejecting");
1561 GST_DEBUG_OBJECT (sink, "Configuring output pin media type");
1562 /* Now we have an AM_MEDIA_TYPE describing what we're going to send.
1563 * We set this on our DirectShow fakesrc's output pin.
1565 sink->fakesrc->GetOutputPin()->SetMediaType (&sink->mediatype);
1566 GST_DEBUG_OBJECT (sink, "Configured output pin media type");
1568 /* We have configured the output pin media type.
1569 * So, create a window (or start using an application-supplied
1570 * one, then connect the graph */
1571 gst_dshowvideosink_prepare_window (sink);
1572 if (!gst_dshowvideosink_connect_graph (sink)) {
1573 GST_ELEMENT_ERROR (sink, CORE, NEGOTIATION,
1574 ("Failed to initialize DirectShow graph with the input caps"), (NULL));
1577 sink->connected = TRUE;
1583 gst_dshowvideosink_stop (GstBaseSink * bsink)
1585 IPin *input_pin = NULL, *output_pin = NULL;
1586 HRESULT hres = S_FALSE;
1587 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (bsink);
1589 if (!sink->filter_graph) {
1590 GST_WARNING_OBJECT (sink, "Cannot destroy filter graph; it doesn't exist");
1594 /* If we created a new window, send the close message and wait until
1595 * it's closed in the window thread */
1596 if (sink->is_new_window) {
1597 SendMessage (sink->window_id, WM_CLOSE, NULL, NULL);
1598 while (!sink->window_closed);
1599 sink->is_new_window = FALSE;
1602 /* Release the renderer */
1603 if (sink->renderersupport) {
1604 delete sink->renderersupport;
1605 sink->renderersupport = NULL;
1608 /* Release our dshow fakesrc */
1609 if (sink->fakesrc) {
1610 sink->fakesrc->Release();
1611 sink->fakesrc = NULL;
1614 /* Release the filter graph manager */
1615 if (sink->filter_graph) {
1616 sink->filter_graph->Release();
1617 sink->filter_graph = NULL;
1620 if (sink->filter_media_event) {
1621 sink->filter_media_event->Release();
1622 sink->filter_media_event = NULL;
1628 static GstFlowReturn
1629 gst_dshowvideosink_show_frame (GstVideoSink *vsink, GstBuffer *buffer)
1631 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (vsink);
1633 GstStateChangeReturn retst;
1635 if (sink->window_closed) {
1636 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, ("Output window was closed"), (NULL));
1637 return GST_FLOW_ERROR;
1640 GST_DEBUG_OBJECT (sink, "Pushing buffer through fakesrc->renderer");
1641 GST_DSHOWVIDEOSINK_GRAPH_LOCK(sink);
1642 if (!sink->graph_running){
1643 retst = gst_dshowvideosink_start_graph(sink);
1644 if (retst == GST_STATE_CHANGE_FAILURE)
1645 return GST_FLOW_FLUSHING;
1647 ret = sink->fakesrc->GetOutputPin()->PushBuffer (buffer);
1648 if (!sink->graph_running){
1649 retst = gst_dshowvideosink_pause_graph(sink);
1650 if (retst == GST_STATE_CHANGE_FAILURE)
1651 return GST_FLOW_FLUSHING;
1653 GST_DSHOWVIDEOSINK_GRAPH_UNLOCK(sink);
1654 GST_DEBUG_OBJECT (sink, "Done pushing buffer through fakesrc->renderer: %s", gst_flow_get_name(ret));
1659 /* TODO: How can we implement these? Figure that out... */
1661 gst_dshowvideosink_unlock (GstBaseSink * bsink)
1663 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (bsink);
1669 gst_dshowvideosink_unlock_stop (GstBaseSink * bsink)
1671 GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (bsink);
1676 /* TODO: Move all of this into generic code? */
1678 /* Helpers to format GUIDs the same way we find them in the source */
1679 #define GUID_FORMAT "{%.8x, %.4x, %.4x, { %.2x, %.2x, %.2x, %.2x, %.2x, %.2x, %.2x, %.2x }}"
1680 #define GUID_ARGS(guid) \
1681 guid.Data1, guid.Data2, guid.Data3, \
1682 guid.Data4[0], guid.Data4[1], guid.Data4[3], guid.Data4[4], \
1683 guid.Data4[5], guid.Data4[6], guid.Data4[7], guid.Data4[8]
1686 audio_media_type_to_caps (AM_MEDIA_TYPE *mediatype)
1692 video_media_type_to_caps (AM_MEDIA_TYPE *mediatype)
1694 GstCaps *caps = NULL;
1696 /* TODO: Add RGB types. */
1697 if (IsEqualGUID (mediatype->subtype, MEDIASUBTYPE_YUY2))
1698 caps = gst_caps_new_simple ("video/x-raw",
1699 "format", GST_TYPE_VIDEO_FORMAT, GST_VIDEO_FORMAT_YUY2, NULL);
1700 else if (IsEqualGUID (mediatype->subtype, MEDIASUBTYPE_UYVY))
1701 caps = gst_caps_new_simple ("video/x-raw",
1702 "format", GST_TYPE_VIDEO_FORMAT, GST_VIDEO_FORMAT_UYVY, NULL);
1703 else if (IsEqualGUID (mediatype->subtype, MEDIASUBTYPE_YV12))
1704 caps = gst_caps_new_simple ("video/x-raw",
1705 "format", GST_TYPE_VIDEO_FORMAT, GST_VIDEO_FORMAT_YV12, NULL);
1708 GST_DEBUG ("No subtype known; cannot continue");
1712 if (IsEqualGUID (mediatype->formattype, FORMAT_VideoInfo) &&
1713 mediatype->cbFormat >= sizeof(VIDEOINFOHEADER))
1715 VIDEOINFOHEADER *vh = (VIDEOINFOHEADER *)mediatype->pbFormat;
1717 /* TODO: Set PAR here. Based on difference between source and target RECTs?
1718 * Do we want framerate? Based on AvgTimePerFrame? */
1719 gst_caps_set_simple (caps,
1720 "width", G_TYPE_INT, vh->bmiHeader.biWidth,
1721 "height", G_TYPE_INT, vh->bmiHeader.biHeight,
1729 /* Create a GstCaps object representing the same media type as
1730 * this AM_MEDIA_TYPE.
1732 * Returns NULL if no corresponding GStreamer type is known.
1734 * May modify mediatype.
1737 gst_directshow_media_type_to_caps (AM_MEDIA_TYPE *mediatype)
1739 GstCaps *caps = NULL;
1741 if (IsEqualGUID (mediatype->majortype, MEDIATYPE_Video))
1742 caps = video_media_type_to_caps (mediatype);
1743 else if (IsEqualGUID (mediatype->majortype, MEDIATYPE_Audio))
1744 caps = audio_media_type_to_caps (mediatype);
1746 GST_DEBUG ("Non audio/video media types not yet " \
1747 "recognised, please add me: " GUID_FORMAT,
1748 GUID_ARGS(mediatype->majortype));
1752 gchar *capsstring = gst_caps_to_string (caps);
1753 GST_DEBUG ("Converted AM_MEDIA_TYPE to \"%s\"", capsstring);
1754 g_free (capsstring);
1757 GST_WARNING ("Failed to convert AM_MEDIA_TYPE to caps");
1763 /* Fill in a DirectShow AM_MEDIA_TYPE structure representing the same media
1764 * type as this GstCaps object.
1766 * Returns FALSE if no corresponding type is known.
1768 * Only operates on simple (single structure) caps.
1771 gst_caps_to_directshow_media_type (GstDshowVideoSink * sink, GstCaps *caps,
1772 AM_MEDIA_TYPE *mediatype)
1778 gst_video_info_init (&info);
1779 if (!gst_video_info_from_caps (&info, caps))
1781 GST_WARNING_OBJECT (sink, "Couldn't parse caps");
1784 memset (mediatype, 0, sizeof (AM_MEDIA_TYPE));
1786 if (GST_VIDEO_FORMAT_INFO_IS_YUV (info.finfo))
1789 GST_VIDEO_SINK_WIDTH (sink) = info.width;
1790 GST_VIDEO_SINK_HEIGHT (sink) = info.height;
1792 height = info.height;
1793 mediatype->majortype = MEDIATYPE_Video;
1795 switch (GST_VIDEO_INFO_FORMAT (&info))
1797 case GST_VIDEO_FORMAT_YUY2:
1798 mediatype->subtype = MEDIASUBTYPE_YUY2;
1799 fourcc = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
1802 case GST_VIDEO_FORMAT_UYVY:
1803 mediatype->subtype = MEDIASUBTYPE_UYVY;
1804 fourcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
1807 case GST_VIDEO_FORMAT_YV12:
1808 mediatype->subtype = MEDIASUBTYPE_YV12;
1809 fourcc = GST_MAKE_FOURCC ('Y', 'V', '1', '2');
1813 GST_WARNING_OBJECT (sink, "Couldn't parse caps");
1817 mediatype->bFixedSizeSamples = TRUE; /* Always true for raw video */
1818 mediatype->bTemporalCompression = FALSE; /* Likewise, always false */
1822 VIDEOINFOHEADER *vi = (VIDEOINFOHEADER *)CoTaskMemAlloc (sizeof (VIDEOINFOHEADER));
1823 memset (vi, 0, sizeof (VIDEOINFOHEADER));
1825 mediatype->formattype = FORMAT_VideoInfo;
1826 mediatype->cbFormat = sizeof (VIDEOINFOHEADER);
1827 mediatype->pbFormat = (BYTE *)vi;
1829 mediatype->lSampleSize = width * height * bpp / 8;
1831 GST_INFO_OBJECT (sink, "Set mediatype format: size %d, sample size %d",
1832 mediatype->cbFormat, mediatype->lSampleSize);
1834 vi->rcSource.top = 0;
1835 vi->rcSource.left = 0;
1836 vi->rcSource.bottom = height;
1837 vi->rcSource.right = width;
1839 vi->rcTarget.top = 0;
1840 vi->rcTarget.left = 0;
1841 if (sink->keep_aspect_ratio) {
1842 par_n = GST_VIDEO_INFO_PAR_N (&info);
1843 par_d = GST_VIDEO_INFO_PAR_D (&info);
1844 /* To handle non-square pixels, we set the target rectangle to a
1845 * different size than the source rectangle.
1846 * There might be a better way, but this seems to work. */
1847 vi->rcTarget.bottom = height;
1848 vi->rcTarget.right = width * par_n / par_d;
1849 GST_DEBUG_OBJECT (sink, "Got PAR: set target right to %d from width %d",
1850 vi->rcTarget.right, width);
1853 vi->rcTarget.bottom = height;
1854 vi->rcTarget.right = width;
1857 vi->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
1858 vi->bmiHeader.biWidth = width;
1859 vi->bmiHeader.biHeight = -height; /* Required to be negative. */
1860 vi->bmiHeader.biPlanes = 1; /* Required to be 1 */
1861 vi->bmiHeader.biBitCount = bpp;
1862 vi->bmiHeader.biCompression = fourcc;
1863 vi->bmiHeader.biSizeImage = width * height * bpp / 8;
1865 /* We can safely zero these; they don't matter for our uses */
1866 vi->bmiHeader.biXPelsPerMeter = 0;
1867 vi->bmiHeader.biYPelsPerMeter = 0;
1868 vi->bmiHeader.biClrUsed = 0;
1869 vi->bmiHeader.biClrImportant = 0;
1872 GST_DEBUG_OBJECT (sink, "Successfully built AM_MEDIA_TYPE from caps");
1876 GST_WARNING_OBJECT (sink, "Failed to convert caps, not a known caps type");
1877 /* Only YUV supported so far */
1882 /* Plugin entry point */
1883 extern "C" static gboolean
1884 plugin_init (GstPlugin * plugin)
1886 /* PRIMARY: this is the best videosink to use on windows */
1887 if (!gst_element_register (plugin, "dshowvideosink",
1888 GST_RANK_SECONDARY, GST_TYPE_DSHOWVIDEOSINK))
1894 extern "C" GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1897 "DirectShow sink wrapper plugin",
1898 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)