1 /* GStreamer
2  * Copyright (C) 2012 Roland Krikava <info@bluedigits.com>
3  * Copyright (C) 2010-2011 David Hoyt <dhoyt@hoytsoft.org>
4  * Copyright (C) 2010 Andoni Morales <ylatuya@gmail.com>
5  * Copyright (C) 2012 Sebastian Dröge <sebastian.droege@collabora.co.uk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include "d3dvideosink.h"
27 
28 #define ELEMENT_NAME  "d3dvideosink"
29 
30 enum
31 {
32   PROP_0,
33   PROP_FORCE_ASPECT_RATIO,
34   PROP_CREATE_RENDER_WINDOW,
35   PROP_STREAM_STOP_ON_CLOSE,
36   PROP_ENABLE_NAVIGATION_EVENTS,
37   PROP_LAST
38 };
39 
40 #define DEFAULT_FORCE_ASPECT_RATIO       TRUE
41 #define DEFAULT_CREATE_RENDER_WINDOW     TRUE
42 #define DEFAULT_STREAM_STOP_ON_CLOSE     TRUE
43 #define DEFAULT_ENABLE_NAVIGATION_EVENTS TRUE
44 
45 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("video/x-raw, "
49         "format = (string) { I420, YV12, UYVY, YUY2, NV12, BGRx, RGBx, RGBA, BGRA, BGR, RGB16, RGB15 }, "
50         "framerate = (fraction) [ 0, MAX ], "
51         "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]")
52     );
53 
54 GST_DEBUG_CATEGORY (gst_d3dvideosink_debug);
55 #define GST_CAT_DEFAULT gst_d3dvideosink_debug
56 
57 /* FWD DECLS */
58 /* GstXOverlay Interface */
59 static void
60 gst_d3dvideosink_video_overlay_interface_init (GstVideoOverlayInterface *
61     iface);
62 static void gst_d3dvideosink_set_window_handle (GstVideoOverlay * overlay,
63     guintptr window_id);
64 static void gst_d3dvideosink_set_render_rectangle (GstVideoOverlay * overlay,
65     gint x, gint y, gint width, gint height);
66 static void gst_d3dvideosink_expose (GstVideoOverlay * overlay);
67 /* GstNavigation Interface */
68 static void gst_d3dvideosink_navigation_interface_init (GstNavigationInterface *
69     iface);
70 static void gst_d3dvideosink_navigation_send_event (GstNavigation * navigation,
71     GstStructure * structure);
72 /* GObject */
73 static void gst_d3dvideosink_set_property (GObject * object, guint prop_id,
74     const GValue * value, GParamSpec * pspec);
75 static void gst_d3dvideosink_get_property (GObject * object, guint prop_id,
76     GValue * value, GParamSpec * pspec);
77 static void gst_d3dvideosink_finalize (GObject * gobject);
78 /* GstBaseSink */
79 static GstCaps *gst_d3dvideosink_get_caps (GstBaseSink * basesink,
80     GstCaps * filter);
81 static gboolean gst_d3dvideosink_set_caps (GstBaseSink * bsink, GstCaps * caps);
82 static gboolean gst_d3dvideosink_start (GstBaseSink * sink);
83 static gboolean gst_d3dvideosink_stop (GstBaseSink * sink);
84 static gboolean gst_d3dvideosink_propose_allocation (GstBaseSink * bsink,
85     GstQuery * query);
86 /* GstVideoSink */
87 static GstFlowReturn gst_d3dvideosink_show_frame (GstVideoSink * vsink,
88     GstBuffer * buffer);
89 
90 #define _do_init \
91   G_IMPLEMENT_INTERFACE (GST_TYPE_NAVIGATION, gst_d3dvideosink_navigation_interface_init); \
92   G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY, gst_d3dvideosink_video_overlay_interface_init); \
93   GST_DEBUG_CATEGORY_INIT (gst_d3dvideosink_debug, ELEMENT_NAME, 0, "Direct3D Video");
94 
95 G_DEFINE_TYPE_WITH_CODE (GstD3DVideoSink, gst_d3dvideosink, GST_TYPE_VIDEO_SINK,
96     _do_init);
97 
98 static void
gst_d3dvideosink_class_init(GstD3DVideoSinkClass * klass)99 gst_d3dvideosink_class_init (GstD3DVideoSinkClass * klass)
100 {
101   GObjectClass *gobject_class;
102   GstElementClass *gstelement_class;
103   GstVideoSinkClass *gstvideosink_class;
104   GstBaseSinkClass *gstbasesink_class;
105 
106   gobject_class = (GObjectClass *) klass;
107   gstelement_class = (GstElementClass *) klass;
108   gstvideosink_class = (GstVideoSinkClass *) klass;
109   gstbasesink_class = (GstBaseSinkClass *) klass;
110 
111   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_d3dvideosink_finalize);
112   gobject_class->set_property =
113       GST_DEBUG_FUNCPTR (gst_d3dvideosink_set_property);
114   gobject_class->get_property =
115       GST_DEBUG_FUNCPTR (gst_d3dvideosink_get_property);
116 
117   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_d3dvideosink_get_caps);
118   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_d3dvideosink_set_caps);
119   gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_d3dvideosink_start);
120   gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_d3dvideosink_stop);
121   gstbasesink_class->propose_allocation =
122       GST_DEBUG_FUNCPTR (gst_d3dvideosink_propose_allocation);
123 
124   gstvideosink_class->show_frame =
125       GST_DEBUG_FUNCPTR (gst_d3dvideosink_show_frame);
126 
127   /* Add properties */
128   g_object_class_install_property (G_OBJECT_CLASS (klass),
129       PROP_FORCE_ASPECT_RATIO, g_param_spec_boolean ("force-aspect-ratio",
130           "Force aspect ratio",
131           "When enabled, scaling will respect original aspect ratio",
132           DEFAULT_FORCE_ASPECT_RATIO,
133           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
134   g_object_class_install_property (G_OBJECT_CLASS (klass),
135       PROP_CREATE_RENDER_WINDOW, g_param_spec_boolean ("create-render-window",
136           "Create render window",
137           "If no window ID is given, a new render window is created",
138           DEFAULT_CREATE_RENDER_WINDOW,
139           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
140   g_object_class_install_property (G_OBJECT_CLASS (klass),
141       PROP_STREAM_STOP_ON_CLOSE, g_param_spec_boolean ("stream-stop-on-close",
142           "Stop streaming on window close",
143           "If the render window is closed stop stream",
144           DEFAULT_STREAM_STOP_ON_CLOSE,
145           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
146   g_object_class_install_property (G_OBJECT_CLASS (klass),
147       PROP_ENABLE_NAVIGATION_EVENTS,
148       g_param_spec_boolean ("enable-navigation-events",
149           "Enable navigation events",
150           "When enabled, navigation events are sent upstream",
151           DEFAULT_ENABLE_NAVIGATION_EVENTS,
152           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153 
154   gst_element_class_set_static_metadata (gstelement_class,
155       "Direct3D video sink", "Sink/Video",
156       "Display data using a Direct3D video renderer",
157       "David Hoyt <dhoyt@hoytsoft.org>, Roland Krikava <info@bluedigits.com>");
158 
159   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
160 
161   g_rec_mutex_init (&klass->lock);
162 }
163 
164 static void
gst_d3dvideosink_init(GstD3DVideoSink * sink)165 gst_d3dvideosink_init (GstD3DVideoSink * sink)
166 {
167   GST_DEBUG_OBJECT (sink, " ");
168 
169   /* Init Properties */
170   sink->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
171   sink->create_internal_window = DEFAULT_CREATE_RENDER_WINDOW;
172   sink->stream_stop_on_close = DEFAULT_STREAM_STOP_ON_CLOSE;
173   sink->enable_navigation_events = DEFAULT_ENABLE_NAVIGATION_EVENTS;
174   sink->d3d.surface = NULL;
175 
176   g_rec_mutex_init (&sink->lock);
177 }
178 
179 /* GObject Functions */
180 
181 static void
gst_d3dvideosink_finalize(GObject * gobject)182 gst_d3dvideosink_finalize (GObject * gobject)
183 {
184   GstD3DVideoSink *sink = GST_D3DVIDEOSINK (gobject);
185 
186   GST_DEBUG_OBJECT (sink, " ");
187 
188   gst_object_replace ((GstObject **) & sink->pool, NULL);
189   gst_object_replace ((GstObject **) & sink->fallback_pool, NULL);
190 
191   gst_caps_replace (&sink->supported_caps, NULL);
192 
193   g_rec_mutex_clear (&sink->lock);
194 
195   G_OBJECT_CLASS (gst_d3dvideosink_parent_class)->finalize (gobject);
196 }
197 
198 static void
gst_d3dvideosink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)199 gst_d3dvideosink_set_property (GObject * object, guint prop_id,
200     const GValue * value, GParamSpec * pspec)
201 {
202   GstD3DVideoSink *sink = GST_D3DVIDEOSINK (object);
203 
204   switch (prop_id) {
205     case PROP_FORCE_ASPECT_RATIO:
206       sink->force_aspect_ratio = g_value_get_boolean (value);
207       break;
208     case PROP_CREATE_RENDER_WINDOW:
209       sink->create_internal_window = g_value_get_boolean (value);
210       break;
211     case PROP_STREAM_STOP_ON_CLOSE:
212       sink->stream_stop_on_close = g_value_get_boolean (value);
213       break;
214     case PROP_ENABLE_NAVIGATION_EVENTS:
215       sink->enable_navigation_events = g_value_get_boolean (value);
216       break;
217     default:
218       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219       break;
220   }
221 }
222 
223 static void
gst_d3dvideosink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)224 gst_d3dvideosink_get_property (GObject * object, guint prop_id, GValue * value,
225     GParamSpec * pspec)
226 {
227   GstD3DVideoSink *sink = GST_D3DVIDEOSINK (object);
228 
229   switch (prop_id) {
230     case PROP_FORCE_ASPECT_RATIO:
231       g_value_set_boolean (value, sink->force_aspect_ratio);
232       break;
233     case PROP_CREATE_RENDER_WINDOW:
234       g_value_set_boolean (value, sink->create_internal_window);
235       break;
236     case PROP_STREAM_STOP_ON_CLOSE:
237       g_value_set_boolean (value, sink->stream_stop_on_close);
238       break;
239     case PROP_ENABLE_NAVIGATION_EVENTS:
240       g_value_set_boolean (value, sink->enable_navigation_events);
241       break;
242     default:
243       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244       break;
245   }
246 }
247 
248 /* GstBaseSinkClass Functions */
249 
250 static GstCaps *
gst_d3dvideosink_get_caps(GstBaseSink * basesink,GstCaps * filter)251 gst_d3dvideosink_get_caps (GstBaseSink * basesink, GstCaps * filter)
252 {
253   GstD3DVideoSink *sink = GST_D3DVIDEOSINK (basesink);
254   GstCaps *caps;
255 
256   caps = d3d_supported_caps (sink);
257   if (!caps)
258     caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
259 
260   if (caps && filter) {
261     GstCaps *isect;
262     isect = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
263     gst_caps_unref (caps);
264     caps = isect;
265   }
266 
267   return caps;
268 }
269 
270 static gboolean
gst_d3dvideosink_set_caps(GstBaseSink * bsink,GstCaps * caps)271 gst_d3dvideosink_set_caps (GstBaseSink * bsink, GstCaps * caps)
272 {
273   GstD3DVideoSink *sink;
274   GstCaps *sink_caps;
275   gint video_width, video_height;
276   gint video_par_n, video_par_d;        /* video's PAR */
277   gint display_par_n = 1, display_par_d = 1;    /* display's PAR */
278   guint num, den;
279   GstBufferPool *newpool, *oldpool;
280   GstBufferPool *newfbpool, *oldfbpool;
281   GstStructure *config;
282 
283   GST_DEBUG_OBJECT (bsink, "Caps: %" GST_PTR_FORMAT, caps);
284   sink = GST_D3DVIDEOSINK (bsink);
285 
286   sink_caps = d3d_supported_caps (sink);
287 
288   if (!gst_caps_can_intersect (sink_caps, caps))
289     goto incompatible_caps;
290   gst_caps_replace (&sink_caps, NULL);
291 
292   memset (&sink->info, 0, sizeof (GstVideoInfo));
293   if (!gst_video_info_from_caps (&sink->info, caps))
294     goto invalid_format;
295 
296   sink->format = sink->info.finfo->format;
297   video_width = sink->info.width;
298   video_height = sink->info.height;
299   video_par_n = sink->info.par_n;
300   video_par_d = sink->info.par_d;
301 
302   GST_DEBUG_OBJECT (bsink, "Set Caps Format: %s",
303       gst_video_format_to_string (sink->format));
304 
305   /* get aspect ratio from caps if it's present, and
306    * convert video width and height to a display width and height
307    * using wd / hd = wv / hv * PARv / PARd */
308 
309   /* TODO: Get display PAR */
310 
311   if (!gst_video_calculate_display_ratio (&num, &den, video_width,
312           video_height, video_par_n, video_par_d, display_par_n, display_par_d))
313     goto no_disp_ratio;
314 
315   GST_DEBUG_OBJECT (sink,
316       "video width/height: %dx%d, calculated display ratio: %d/%d format: %u",
317       video_width, video_height, num, den, sink->format);
318 
319   /* now find a width x height that respects this display ratio.
320    * prefer those that have one of w/h the same as the incoming video
321    * using wd / hd = num / den
322    */
323 
324   /* start with same height, because of interlaced video
325    * check hd / den is an integer scale factor, and scale wd with the PAR
326    */
327   if (video_height % den == 0) {
328     GST_DEBUG_OBJECT (sink, "keeping video height");
329     GST_VIDEO_SINK_WIDTH (sink) = (guint)
330         gst_util_uint64_scale_int (video_height, num, den);
331     GST_VIDEO_SINK_HEIGHT (sink) = video_height;
332   } else if (video_width % num == 0) {
333     GST_DEBUG_OBJECT (sink, "keeping video width");
334     GST_VIDEO_SINK_WIDTH (sink) = video_width;
335     GST_VIDEO_SINK_HEIGHT (sink) = (guint)
336         gst_util_uint64_scale_int (video_width, den, num);
337   } else {
338     GST_DEBUG_OBJECT (sink, "approximating while keeping video height");
339     GST_VIDEO_SINK_WIDTH (sink) = (guint)
340         gst_util_uint64_scale_int (video_height, num, den);
341     GST_VIDEO_SINK_HEIGHT (sink) = video_height;
342   }
343   GST_DEBUG_OBJECT (sink, "scaling to %dx%d",
344       GST_VIDEO_SINK_WIDTH (sink), GST_VIDEO_SINK_HEIGHT (sink));
345 
346   if (GST_VIDEO_SINK_WIDTH (sink) <= 0 || GST_VIDEO_SINK_HEIGHT (sink) <= 0)
347     goto no_display_size;
348 
349   memset (&sink->crop_rect, 0, sizeof (sink->crop_rect));
350   sink->crop_rect.w = sink->info.width;
351   sink->crop_rect.h = sink->info.height;
352 
353   sink->width = video_width;
354   sink->height = video_height;
355 
356   GST_DEBUG_OBJECT (bsink, "Selected caps: %" GST_PTR_FORMAT, caps);
357 
358   if (!d3d_set_render_format (sink))
359     goto incompatible_caps;
360 
361   /* Create a window (or start using an application-supplied one, then connect the graph */
362   d3d_prepare_window (sink);
363 
364   newpool = gst_d3dsurface_buffer_pool_new (sink);
365   config = gst_buffer_pool_get_config (newpool);
366   /* we need at least 2 buffer because we hold on to the last one */
367   gst_buffer_pool_config_set_params (config, caps, sink->info.size, 2, 0);
368   if (!gst_buffer_pool_set_config (newpool, config)) {
369     gst_object_unref (newpool);
370     GST_ERROR_OBJECT (sink, "Failed to set buffer pool configuration");
371     return FALSE;
372   }
373 
374   newfbpool = gst_d3dsurface_buffer_pool_new (sink);
375   config = gst_buffer_pool_get_config (newfbpool);
376   /* we need at least 2 buffer because we hold on to the last one */
377   gst_buffer_pool_config_set_params (config, caps, sink->info.size, 2, 0);
378   /* Fallback pool must use videometa */
379   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
380   if (!gst_buffer_pool_set_config (newfbpool, config)) {
381     gst_object_unref (newfbpool);
382     GST_ERROR_OBJECT (sink, "Failed to set buffer pool configuration");
383     return FALSE;
384   }
385 
386   GST_OBJECT_LOCK (sink);
387   oldpool = sink->pool;
388   sink->pool = newpool;
389   oldfbpool = sink->fallback_pool;
390   sink->fallback_pool = newfbpool;
391   GST_OBJECT_UNLOCK (sink);
392 
393   if (oldpool)
394     gst_object_unref (oldpool);
395   if (oldfbpool) {
396     gst_buffer_pool_set_active (oldfbpool, FALSE);
397     gst_object_unref (oldfbpool);
398   }
399 
400   return TRUE;
401   /* ERRORS */
402 incompatible_caps:
403   {
404     GST_ERROR_OBJECT (sink, "caps incompatible");
405     gst_caps_unref (sink_caps);
406     return FALSE;
407   }
408 invalid_format:
409   {
410     GST_DEBUG_OBJECT (sink,
411         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
412     return FALSE;
413   }
414 no_disp_ratio:
415   {
416     GST_ELEMENT_ERROR (sink, CORE, NEGOTIATION, (NULL),
417         ("Error calculating the output display ratio of the video."));
418     return FALSE;
419   }
420 no_display_size:
421   {
422     GST_ELEMENT_ERROR (sink, CORE, NEGOTIATION, (NULL),
423         ("Error calculating the output display ratio of the video."));
424     return FALSE;
425   }
426 }
427 
428 static gboolean
gst_d3dvideosink_start(GstBaseSink * bsink)429 gst_d3dvideosink_start (GstBaseSink * bsink)
430 {
431   GstD3DVideoSink *sink = GST_D3DVIDEOSINK (bsink);
432 
433   GST_DEBUG_OBJECT (bsink, "Start() called");
434 
435   return d3d_class_init (sink);
436 }
437 
438 static gboolean
gst_d3dvideosink_stop(GstBaseSink * bsink)439 gst_d3dvideosink_stop (GstBaseSink * bsink)
440 {
441   GstD3DVideoSink *sink = GST_D3DVIDEOSINK (bsink);
442 
443   GST_DEBUG_OBJECT (bsink, "Stop() called");
444   d3d_stop (sink);
445   d3d_class_destroy (sink);
446 
447   return TRUE;
448 }
449 
450 static gboolean
gst_d3dvideosink_propose_allocation(GstBaseSink * bsink,GstQuery * query)451 gst_d3dvideosink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
452 {
453   GstD3DVideoSink *sink = GST_D3DVIDEOSINK (bsink);
454   GstBufferPool *pool;
455   GstStructure *config;
456   GstCaps *caps;
457   guint size;
458   gboolean need_pool;
459 
460   gst_query_parse_allocation (query, &caps, &need_pool);
461   if (!caps) {
462     GST_DEBUG_OBJECT (sink, "no caps specified");
463     return FALSE;
464   }
465 
466   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
467   gst_query_add_allocation_meta (query, GST_VIDEO_CROP_META_API_TYPE, NULL);
468 
469 #ifdef DISABLE_BUFFER_POOL
470   return TRUE;
471 #endif
472 
473   /* FIXME re-using buffer pool breaks renegotiation */
474   GST_OBJECT_LOCK (sink);
475   pool = sink->pool ? gst_object_ref (sink->pool) : NULL;
476   GST_OBJECT_UNLOCK (sink);
477 
478   if (pool) {
479     GstCaps *pcaps;
480 
481     /* we had a pool, check caps */
482     GST_DEBUG_OBJECT (sink, "check existing pool caps");
483     config = gst_buffer_pool_get_config (pool);
484     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
485 
486     if (!gst_caps_is_equal (caps, pcaps)) {
487       GST_DEBUG_OBJECT (sink, "pool has different caps");
488       /* different caps, we can't use this pool */
489       gst_object_unref (pool);
490       pool = NULL;
491     }
492     gst_structure_free (config);
493   } else {
494     GstVideoInfo info;
495 
496     if (!gst_video_info_from_caps (&info, caps)) {
497       GST_ERROR_OBJECT (sink, "allocation query has invalid caps %"
498           GST_PTR_FORMAT, caps);
499       return FALSE;
500     }
501 
502     /* the normal size of a frame */
503     size = info.size;
504   }
505 
506   if (pool == NULL && need_pool) {
507     GST_DEBUG_OBJECT (sink, "create new pool");
508     pool = gst_d3dsurface_buffer_pool_new (sink);
509 
510     config = gst_buffer_pool_get_config (pool);
511     /* we need at least 2 buffer because we hold on to the last one */
512     gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
513     if (!gst_buffer_pool_set_config (pool, config)) {
514       gst_object_unref (pool);
515       GST_ERROR_OBJECT (sink, "failed to set pool configuration");
516       return FALSE;
517     }
518   }
519 
520   /* we need at least 2 buffer because we hold on to the last one */
521   gst_query_add_allocation_pool (query, pool, size, 2, 0);
522   if (pool)
523     gst_object_unref (pool);
524 
525   return TRUE;
526 }
527 
528 /* PUBLIC FUNCTIONS */
529 
530 /* Iterface Registrations */
531 
532 static void
gst_d3dvideosink_video_overlay_interface_init(GstVideoOverlayInterface * iface)533 gst_d3dvideosink_video_overlay_interface_init (GstVideoOverlayInterface * iface)
534 {
535   iface->set_window_handle = gst_d3dvideosink_set_window_handle;
536   iface->set_render_rectangle = gst_d3dvideosink_set_render_rectangle;
537   iface->expose = gst_d3dvideosink_expose;
538 }
539 
540 static void
gst_d3dvideosink_navigation_interface_init(GstNavigationInterface * iface)541 gst_d3dvideosink_navigation_interface_init (GstNavigationInterface * iface)
542 {
543   iface->send_event = gst_d3dvideosink_navigation_send_event;
544 }
545 
546 /* Video Render Code */
547 
548 static void
gst_d3dvideosink_set_window_handle(GstVideoOverlay * overlay,guintptr window_id)549 gst_d3dvideosink_set_window_handle (GstVideoOverlay * overlay,
550     guintptr window_id)
551 {
552   d3d_set_window_handle (GST_D3DVIDEOSINK (overlay), window_id, FALSE);
553 }
554 
555 static void
gst_d3dvideosink_set_render_rectangle(GstVideoOverlay * overlay,gint x,gint y,gint width,gint height)556 gst_d3dvideosink_set_render_rectangle (GstVideoOverlay * overlay, gint x,
557     gint y, gint width, gint height)
558 {
559   GstD3DVideoSink *sink = GST_D3DVIDEOSINK (overlay);
560   sink->render_rect.x = x;
561   sink->render_rect.y = y;
562   sink->render_rect.w = width;
563   sink->render_rect.h = height;
564   d3d_set_render_rectangle (sink);
565 }
566 
567 static void
gst_d3dvideosink_expose(GstVideoOverlay * overlay)568 gst_d3dvideosink_expose (GstVideoOverlay * overlay)
569 {
570   GstD3DVideoSink *sink = GST_D3DVIDEOSINK (overlay);
571   d3d_expose_window (sink);
572 }
573 
574 static GstFlowReturn
gst_d3dvideosink_show_frame(GstVideoSink * vsink,GstBuffer * buffer)575 gst_d3dvideosink_show_frame (GstVideoSink * vsink, GstBuffer * buffer)
576 {
577   GstD3DVideoSink *sink = GST_D3DVIDEOSINK (vsink);
578   return d3d_render_buffer (sink, buffer);
579 }
580 
581 /* Video Navigation Events */
582 
583 static void
gst_d3dvideosink_navigation_send_event(GstNavigation * navigation,GstStructure * structure)584 gst_d3dvideosink_navigation_send_event (GstNavigation * navigation,
585     GstStructure * structure)
586 {
587   GstD3DVideoSink *sink = GST_D3DVIDEOSINK (navigation);
588   GstEvent *e;
589 
590   if ((e = gst_event_new_navigation (structure))) {
591     GstPad *pad;
592     if ((pad = gst_pad_get_peer (GST_VIDEO_SINK_PAD (sink)))) {
593       if (!gst_pad_send_event (pad, gst_event_ref (e))) {
594         /* If upstream didn't handle the event we'll post a message with it
595          * for the application in case it wants to do something with it */
596         gst_element_post_message (GST_ELEMENT_CAST (sink),
597             gst_navigation_message_new_event (GST_OBJECT_CAST (sink), e));
598       }
599       gst_event_unref (e);
600       gst_object_unref (pad);
601     }
602   }
603 }
604 
605 /* PRIVATE FUNCTIONS */
606 
607 
608 /* Plugin entry point */
609 static gboolean
plugin_init(GstPlugin * plugin)610 plugin_init (GstPlugin * plugin)
611 {
612   /* PRIMARY: this is the best videosink to use on windows */
613   if (!gst_element_register (plugin, ELEMENT_NAME,
614           GST_RANK_PRIMARY, GST_TYPE_D3DVIDEOSINK))
615     return FALSE;
616 
617   return TRUE;
618 }
619 
620 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
621     GST_VERSION_MINOR,
622     d3d,
623     "Direct3D plugin",
624     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
625