GstVideoOverlay: Add gst_video_overlay_set_wl_window_exported_shell_handle
[platform/upstream/gst-plugins-base.git] / gst-libs / gst / video / videooverlay.c
1 /* GStreamer Video Overlay interface
2  * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  * Copyright (C) 2011 Tim-Philipp Müller <tim@centricular.net>
4  *
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.
9  *
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.
14  *
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.
19  */
20 /**
21  * SECTION:gstvideooverlay
22  * @title: GstVideoOverlay
23  * @short_description: Interface for setting/getting a window system resource
24  *    on elements supporting it to configure a window into which to render a
25  *    video.
26  *
27  * The #GstVideoOverlay interface is used for 2 main purposes :
28  *
29  * * To get a grab on the Window where the video sink element is going to render.
30  *   This is achieved by either being informed about the Window identifier that
31  *   the video sink element generated, or by forcing the video sink element to use
32  *   a specific Window identifier for rendering.
33  * * To force a redrawing of the latest video frame the video sink element
34  *   displayed on the Window. Indeed if the #GstPipeline is in #GST_STATE_PAUSED
35  *   state, moving the Window around will damage its content. Application
36  *   developers will want to handle the Expose events themselves and force the
37  *   video sink element to refresh the Window's content.
38  *
39  * Using the Window created by the video sink is probably the simplest scenario,
40  * in some cases, though, it might not be flexible enough for application
41  * developers if they need to catch events such as mouse moves and button
42  * clicks.
43  *
44  * Setting a specific Window identifier on the video sink element is the most
45  * flexible solution but it has some issues. Indeed the application needs to set
46  * its Window identifier at the right time to avoid internal Window creation
47  * from the video sink element. To solve this issue a #GstMessage is posted on
48  * the bus to inform the application that it should set the Window identifier
49  * immediately. Here is an example on how to do that correctly:
50  * |[
51  * static GstBusSyncReply
52  * create_window (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
53  * {
54  *  // ignore anything but 'prepare-window-handle' element messages
55  *  if (!gst_is_video_overlay_prepare_window_handle_message (message))
56  *    return GST_BUS_PASS;
57  *
58  *  win = XCreateSimpleWindow (disp, root, 0, 0, 320, 240, 0, 0, 0);
59  *
60  *  XSetWindowBackgroundPixmap (disp, win, None);
61  *
62  *  XMapRaised (disp, win);
63  *
64  *  XSync (disp, FALSE);
65  *
66  *  gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (GST_MESSAGE_SRC (message)),
67  *      win);
68  *
69  *  gst_message_unref (message);
70  *
71  *  return GST_BUS_DROP;
72  * }
73  * ...
74  * int
75  * main (int argc, char **argv)
76  * {
77  * ...
78  *  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
79  *  gst_bus_set_sync_handler (bus, (GstBusSyncHandler) create_window, pipeline,
80         NULL);
81  * ...
82  * }
83  * ]|
84  *
85  * ## Two basic usage scenarios
86  *
87  * There are two basic usage scenarios: in the simplest case, the application
88  * uses #playbin or #plasink or knows exactly what particular element is used
89  * for video output, which is usually the case when the application creates
90  * the videosink to use (e.g. #xvimagesink, #ximagesink, etc.) itself; in this
91  * case, the application can just create the videosink element, create and
92  * realize the window to render the video on and then
93  * call gst_video_overlay_set_window_handle() directly with the XID or native
94  * window handle, before starting up the pipeline.
95  * As #playbin and #playsink implement the video overlay interface and proxy
96  * it transparently to the actual video sink even if it is created later, this
97  * case also applies when using these elements.
98  *
99  * In the other and more common case, the application does not know in advance
100  * what GStreamer video sink element will be used for video output. This is
101  * usually the case when an element such as #autovideosink is used.
102  * In this case, the video sink element itself is created
103  * asynchronously from a GStreamer streaming thread some time after the
104  * pipeline has been started up. When that happens, however, the video sink
105  * will need to know right then whether to render onto an already existing
106  * application window or whether to create its own window. This is when it
107  * posts a prepare-window-handle message, and that is also why this message needs
108  * to be handled in a sync bus handler which will be called from the streaming
109  * thread directly (because the video sink will need an answer right then).
110  *
111  * As response to the prepare-window-handle element message in the bus sync
112  * handler, the application may use gst_video_overlay_set_window_handle() to tell
113  * the video sink to render onto an existing window surface. At this point the
114  * application should already have obtained the window handle / XID, so it
115  * just needs to set it. It is generally not advisable to call any GUI toolkit
116  * functions or window system functions from the streaming thread in which the
117  * prepare-window-handle message is handled, because most GUI toolkits and
118  * windowing systems are not thread-safe at all and a lot of care would be
119  * required to co-ordinate the toolkit and window system calls of the
120  * different threads (Gtk+ users please note: prior to Gtk+ 2.18
121  * GDK_WINDOW_XID() was just a simple structure access, so generally fine to do
122  * within the bus sync handler; this macro was changed to a function call in
123  * Gtk+ 2.18 and later, which is likely to cause problems when called from a
124  * sync handler; see below for a better approach without GDK_WINDOW_XID()
125  * used in the callback).
126  *
127  * ## GstVideoOverlay and Gtk+
128  *
129  * |[
130  * #include &lt;gst/video/videooverlay.h&gt;
131  * #include &lt;gtk/gtk.h&gt;
132  * #ifdef GDK_WINDOWING_X11
133  * #include &lt;gdk/gdkx.h&gt;  // for GDK_WINDOW_XID
134  * #endif
135  * #ifdef GDK_WINDOWING_WIN32
136  * #include &lt;gdk/gdkwin32.h&gt;  // for GDK_WINDOW_HWND
137  * #endif
138  * ...
139  * static guintptr video_window_handle = 0;
140  * ...
141  * static GstBusSyncReply
142  * bus_sync_handler (GstBus * bus, GstMessage * message, gpointer user_data)
143  * {
144  *  // ignore anything but 'prepare-window-handle' element messages
145  *  if (!gst_is_video_overlay_prepare_window_handle_message (message))
146  *    return GST_BUS_PASS;
147  *
148  *  if (video_window_handle != 0) {
149  *    GstVideoOverlay *overlay;
150  *
151  *    // GST_MESSAGE_SRC (message) will be the video sink element
152  *    overlay = GST_VIDEO_OVERLAY (GST_MESSAGE_SRC (message));
153  *    gst_video_overlay_set_window_handle (overlay, video_window_handle);
154  *  } else {
155  *    g_warning ("Should have obtained video_window_handle by now!");
156  *  }
157  *
158  *  gst_message_unref (message);
159  *  return GST_BUS_DROP;
160  * }
161  * ...
162  * static void
163  * video_widget_realize_cb (GtkWidget * widget, gpointer data)
164  * {
165  * #if GTK_CHECK_VERSION(2,18,0)
166  *   // Tell Gtk+/Gdk to create a native window for this widget instead of
167  *   // drawing onto the parent widget.
168  *   // This is here just for pedagogical purposes, GDK_WINDOW_XID will call
169  *   // it as well in newer Gtk versions
170  *   if (!gdk_window_ensure_native (widget->window))
171  *     g_error ("Couldn't create native window needed for GstVideoOverlay!");
172  * #endif
173  *
174  * #ifdef GDK_WINDOWING_X11
175  *   {
176  *     gulong xid = GDK_WINDOW_XID (gtk_widget_get_window (video_window));
177  *     video_window_handle = xid;
178  *   }
179  * #endif
180  * #ifdef GDK_WINDOWING_WIN32
181  *   {
182  *     HWND wnd = GDK_WINDOW_HWND (gtk_widget_get_window (video_window));
183  *     video_window_handle = (guintptr) wnd;
184  *   }
185  * #endif
186  * }
187  * ...
188  * int
189  * main (int argc, char **argv)
190  * {
191  *   GtkWidget *video_window;
192  *   GtkWidget *app_window;
193  *   ...
194  *   app_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
195  *   ...
196  *   video_window = gtk_drawing_area_new ();
197  *   g_signal_connect (video_window, "realize",
198  *       G_CALLBACK (video_widget_realize_cb), NULL);
199  *   gtk_widget_set_double_buffered (video_window, FALSE);
200  *   ...
201  *   // usually the video_window will not be directly embedded into the
202  *   // application window like this, but there will be many other widgets
203  *   // and the video window will be embedded in one of them instead
204  *   gtk_container_add (GTK_CONTAINER (ap_window), video_window);
205  *   ...
206  *   // show the GUI
207  *   gtk_widget_show_all (app_window);
208  *
209  *   // realize window now so that the video window gets created and we can
210  *   // obtain its XID/HWND before the pipeline is started up and the videosink
211  *   // asks for the XID/HWND of the window to render onto
212  *   gtk_widget_realize (video_window);
213  *
214  *   // we should have the XID/HWND now
215  *   g_assert (video_window_handle != 0);
216  *   ...
217  *   // set up sync handler for setting the xid once the pipeline is started
218  *   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
219  *   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler, NULL,
220  *       NULL);
221  *   gst_object_unref (bus);
222  *   ...
223  *   gst_element_set_state (pipeline, GST_STATE_PLAYING);
224  *   ...
225  * }
226  * ]|
227  *
228  * ## GstVideoOverlay and Qt
229  *
230  * |[
231  * #include &lt;glib.h&gt;
232  * #include &lt;gst/gst.h&gt;
233  * #include &lt;gst/video/videooverlay.h&gt;
234  *
235  * #include &lt;QApplication&gt;
236  * #include &lt;QTimer&gt;
237  * #include &lt;QWidget&gt;
238  *
239  * int main(int argc, char *argv[])
240  * {
241  *   if (!g_thread_supported ())
242  *     g_thread_init (NULL);
243  *
244  *   gst_init (&argc, &argv);
245  *   QApplication app(argc, argv);
246  *   app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit ()));
247  *
248  *   // prepare the pipeline
249  *
250  *   GstElement *pipeline = gst_pipeline_new ("xvoverlay");
251  *   GstElement *src = gst_element_factory_make ("videotestsrc", NULL);
252  *   GstElement *sink = gst_element_factory_make ("xvimagesink", NULL);
253  *   gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
254  *   gst_element_link (src, sink);
255  *
256  *   // prepare the ui
257  *
258  *   QWidget window;
259  *   window.resize(320, 240);
260  *   window.show();
261  *
262  *   WId xwinid = window.winId();
263  *   gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), xwinid);
264  *
265  *   // run the pipeline
266  *
267  *   GstStateChangeReturn sret = gst_element_set_state (pipeline,
268  *       GST_STATE_PLAYING);
269  *   if (sret == GST_STATE_CHANGE_FAILURE) {
270  *     gst_element_set_state (pipeline, GST_STATE_NULL);
271  *     gst_object_unref (pipeline);
272  *     // Exit application
273  *     QTimer::singleShot(0, QApplication::activeWindow(), SLOT(quit()));
274  *   }
275  *
276  *   int ret = app.exec();
277  *
278  *   window.hide();
279  *   gst_element_set_state (pipeline, GST_STATE_NULL);
280  *   gst_object_unref (pipeline);
281  *
282  *   return ret;
283  * }
284  * ]|
285  *
286  */
287
288 #ifdef HAVE_CONFIG_H
289 #include "config.h"
290 #endif
291
292 #include "videooverlay.h"
293
294 enum
295 {
296   PROP_RENDER_RECTANGLE,
297 };
298
299 GST_DEBUG_CATEGORY_STATIC (gst_video_overlay_debug);
300 #define GST_CAT_DEFAULT gst_video_overlay_debug
301
302 GType
303 gst_video_overlay_get_type (void)
304 {
305   static GType gst_video_overlay_type = 0;
306
307   if (!gst_video_overlay_type) {
308     static const GTypeInfo gst_video_overlay_info = {
309       sizeof (GstVideoOverlayInterface),
310       NULL,
311       NULL,
312       NULL,
313       NULL,
314       NULL,
315       0,
316       0,
317       NULL,
318     };
319
320     gst_video_overlay_type = g_type_register_static (G_TYPE_INTERFACE,
321         "GstVideoOverlay", &gst_video_overlay_info, 0);
322
323     GST_DEBUG_CATEGORY_INIT (gst_video_overlay_debug, "videooverlay", 0,
324         "videooverlay interface");
325   }
326
327   return gst_video_overlay_type;
328 }
329
330 #ifdef TIZEN_FEATURE_WAYLAND_ENHANCEMENT
331 /**
332 * gst_video_overlay_set_wl_window_wl_surface_id:
333 * @overlay: a #GstVideoOverlay to set the window on.
334 * @wl_surface_id: a global resource id of wl_surface referencing the wayland window.
335
336 * This will call the video overlay's set_wl_window_wl_surface_id method.  You
337 * should use this medtod  to tell to an overlay to display video output to a
338 * specific window(e.g. an Wayland Window on Wayland).
339 * But you can also set handle to wayland videosink with gst_video_overlay_set_wl_window_wl_surface_id().
340 */
341 void
342 gst_video_overlay_set_wl_window_wl_surface_id (GstVideoOverlay * overlay,
343     gint wl_surface_id)
344 {
345   GstVideoOverlayInterface *iface;
346
347   g_return_if_fail (overlay != NULL);
348   g_return_if_fail (GST_IS_VIDEO_OVERLAY (overlay));
349
350   iface = GST_VIDEO_OVERLAY_GET_INTERFACE (overlay);
351
352   if (iface->set_wl_window_wl_surface_id) {
353     iface->set_wl_window_wl_surface_id (overlay, wl_surface_id);
354   }
355 }
356
357 /**
358 * gst_video_overlay_set_wl_window_exported_shell_handle:
359 * @overlay: a #GstVideoOverlay to set the window on.
360 * @exported_shell_handle: a shell handle exported by wayland window for synchronization between UI and video
361
362 * This will call the video overlay's set_wl_window_exported_shell_handle method. You
363 * should use this medtod to tell to an overlay to display video output to a
364 * specific window(e.g. an Wayland Window on Wayland).
365 * But you can also set handle to wayland videosink with gst_video_overlay_set_wl_window_exported_shell_handle().
366 */
367 void
368 gst_video_overlay_set_wl_window_exported_shell_handle (GstVideoOverlay * overlay,
369     const char *exported_shell_handle)
370 {
371   GstVideoOverlayInterface *iface;
372
373   g_return_if_fail (overlay != NULL);
374   g_return_if_fail (GST_IS_VIDEO_OVERLAY (overlay));
375
376   iface = GST_VIDEO_OVERLAY_GET_INTERFACE (overlay);
377
378   if (iface->set_wl_window_exported_shell_handle) {
379     iface->set_wl_window_exported_shell_handle (overlay, exported_shell_handle);
380   }
381 }
382
383
384
385 /**
386  * gst_video_overlay_set_display_roi_area:
387  * @overlay: a #GstVideoOverlay
388  * @x: the horizontal offset of the render area inside the window
389  * @y: the vertical offset of the render area inside the window
390  * @width: the width of the render area inside the window
391  * @height: the height of the render area inside the window
392  *
393  * Set the ROI(Region of Interest) area of wayland window.
394  * Returns: %FALSE if not supported by the sink.
395  */
396 gboolean
397 gst_video_overlay_set_display_roi_area (GstVideoOverlay * overlay,
398     gint x, gint y, gint width, gint height)
399 {
400   GstVideoOverlayInterface *iface;
401
402   g_return_val_if_fail (overlay != NULL, FALSE);
403   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY (overlay), FALSE);
404   g_return_val_if_fail (width > 0 && height > 0, FALSE);
405
406   iface = GST_VIDEO_OVERLAY_GET_INTERFACE (overlay);
407
408   if (iface->set_display_roi_area) {
409     iface->set_display_roi_area (overlay, x, y, width, height);
410     return TRUE;
411   }
412   return FALSE;
413 }
414
415 /**
416  * gst_video_overlay_set_video_roi_area:
417  * @overlay: a #GstVideoOverlay
418  * @x_scale: x coordinate ratio value of video source area
419  *           based on video width size, valid range is from 0.0 to 1.0.
420  * @y_scale: y coordinate ratio value of video source area
421  *           based on video width size, valid range is from 0.0 to 1.0.
422  * @w_scale: width ratio value of the video source area
423  *           based on video width size, valid range is from greater than 0.0 to 1.0.
424  * @h_scale: height ratio value of the video source area
425  *           based on video width size, valid range is from greater than 0.0 to 1.0.
426  *
427  * Sets the ROI(Region Of Interest) area of video source.
428  * Returns: %FALSE if not supported by the sink.
429 **/
430 gboolean
431 gst_video_overlay_set_video_roi_area (GstVideoOverlay * overlay,
432     gdouble x_scale, gdouble y_scale, gdouble w_scale, gdouble h_scale)
433 {
434   GstVideoOverlayInterface *iface;
435
436   g_return_val_if_fail (overlay != NULL, FALSE);
437   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY (overlay), FALSE);
438   g_return_val_if_fail (x_scale >= 0.0 && x_scale <= 1.0, FALSE);
439   g_return_val_if_fail (y_scale >= 0.0 && y_scale <= 1.0, FALSE);
440   g_return_val_if_fail (w_scale > 0.0 && w_scale <= 1.0, FALSE);
441   g_return_val_if_fail (h_scale > 0.0 && h_scale <= 1.0, FALSE);
442
443   iface = GST_VIDEO_OVERLAY_GET_INTERFACE (overlay);
444
445   if (iface->set_video_roi_area) {
446     iface->set_video_roi_area (overlay, x_scale, y_scale, w_scale, h_scale);
447     return TRUE;
448   }
449   return FALSE;
450 }
451 #endif
452 /**
453  * gst_video_overlay_set_window_handle:
454  * @overlay: a #GstVideoOverlay to set the window on.
455  * @handle: a handle referencing the window.
456  *
457  * This will call the video overlay's set_window_handle method. You
458  * should use this method to tell to an overlay to display video output to a
459  * specific window (e.g. an XWindow on X11). Passing 0 as the  @handle will
460  * tell the overlay to stop using that window and create an internal one.
461  */
462 void
463 gst_video_overlay_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
464 {
465   GstVideoOverlayInterface *iface;
466
467   g_return_if_fail (overlay != NULL);
468   g_return_if_fail (GST_IS_VIDEO_OVERLAY (overlay));
469
470   iface = GST_VIDEO_OVERLAY_GET_INTERFACE (overlay);
471
472   if (iface->set_window_handle) {
473     iface->set_window_handle (overlay, handle);
474   }
475 }
476
477 /**
478  * gst_video_overlay_got_window_handle:
479  * @overlay: a #GstVideoOverlay which got a window
480  * @handle: a platform-specific handle referencing the window
481  *
482  * This will post a "have-window-handle" element message on the bus.
483  *
484  * This function should only be used by video overlay plugin developers.
485  */
486 void
487 gst_video_overlay_got_window_handle (GstVideoOverlay * overlay, guintptr handle)
488 {
489   GstStructure *s;
490   GstMessage *msg;
491
492   g_return_if_fail (overlay != NULL);
493   g_return_if_fail (GST_IS_VIDEO_OVERLAY (overlay));
494
495   GST_LOG_OBJECT (GST_OBJECT (overlay), "window_handle = %p", (gpointer)
496       handle);
497   s = gst_structure_new ("have-window-handle",
498       "window-handle", G_TYPE_UINT64, (guint64) handle, NULL);
499   msg = gst_message_new_element (GST_OBJECT (overlay), s);
500   gst_element_post_message (GST_ELEMENT (overlay), msg);
501 }
502
503 /**
504  * gst_video_overlay_prepare_window_handle:
505  * @overlay: a #GstVideoOverlay which does not yet have an Window handle set
506  *
507  * This will post a "prepare-window-handle" element message on the bus
508  * to give applications an opportunity to call
509  * gst_video_overlay_set_window_handle() before a plugin creates its own
510  * window.
511  *
512  * This function should only be used by video overlay plugin developers.
513  */
514 void
515 gst_video_overlay_prepare_window_handle (GstVideoOverlay * overlay)
516 {
517   GstStructure *s;
518   GstMessage *msg;
519
520   g_return_if_fail (overlay != NULL);
521   g_return_if_fail (GST_IS_VIDEO_OVERLAY (overlay));
522
523   GST_LOG_OBJECT (GST_OBJECT (overlay), "prepare window handle");
524   s = gst_structure_new_empty ("prepare-window-handle");
525   msg = gst_message_new_element (GST_OBJECT (overlay), s);
526   gst_element_post_message (GST_ELEMENT (overlay), msg);
527 }
528
529 /**
530  * gst_video_overlay_expose:
531  * @overlay: a #GstVideoOverlay to expose.
532  *
533  * Tell an overlay that it has been exposed. This will redraw the current frame
534  * in the drawable even if the pipeline is PAUSED.
535  */
536 void
537 gst_video_overlay_expose (GstVideoOverlay * overlay)
538 {
539   GstVideoOverlayInterface *iface;
540
541   g_return_if_fail (overlay != NULL);
542   g_return_if_fail (GST_IS_VIDEO_OVERLAY (overlay));
543
544   iface = GST_VIDEO_OVERLAY_GET_INTERFACE (overlay);
545
546   if (iface->expose) {
547     iface->expose (overlay);
548   }
549 }
550
551 /**
552  * gst_video_overlay_handle_events:
553  * @overlay: a #GstVideoOverlay to expose.
554  * @handle_events: a #gboolean indicating if events should be handled or not.
555  *
556  * Tell an overlay that it should handle events from the window system. These
557  * events are forwarded upstream as navigation events. In some window system,
558  * events are not propagated in the window hierarchy if a client is listening
559  * for them. This method allows you to disable events handling completely
560  * from the #GstVideoOverlay.
561  */
562 void
563 gst_video_overlay_handle_events (GstVideoOverlay * overlay,
564     gboolean handle_events)
565 {
566   GstVideoOverlayInterface *iface;
567
568   g_return_if_fail (overlay != NULL);
569   g_return_if_fail (GST_IS_VIDEO_OVERLAY (overlay));
570
571   iface = GST_VIDEO_OVERLAY_GET_INTERFACE (overlay);
572
573   if (iface->handle_events) {
574     iface->handle_events (overlay, handle_events);
575   }
576 }
577
578 /**
579  * gst_video_overlay_set_render_rectangle:
580  * @overlay: a #GstVideoOverlay
581  * @x: the horizontal offset of the render area inside the window
582  * @y: the vertical offset of the render area inside the window
583  * @width: the width of the render area inside the window
584  * @height: the height of the render area inside the window
585  *
586  * Configure a subregion as a video target within the window set by
587  * gst_video_overlay_set_window_handle(). If this is not used or not supported
588  * the video will fill the area of the window set as the overlay to 100%.
589  * By specifying the rectangle, the video can be overlayed to a specific region
590  * of that window only. After setting the new rectangle one should call
591  * gst_video_overlay_expose() to force a redraw. To unset the region pass -1 for
592  * the @width and @height parameters.
593  *
594  * This method is needed for non fullscreen video overlay in UI toolkits that
595  * do not support subwindows.
596  *
597  * Returns: %FALSE if not supported by the sink.
598  */
599 gboolean
600 gst_video_overlay_set_render_rectangle (GstVideoOverlay * overlay,
601     gint x, gint y, gint width, gint height)
602 {
603   GstVideoOverlayInterface *iface;
604
605   g_return_val_if_fail (overlay != NULL, FALSE);
606   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY (overlay), FALSE);
607   g_return_val_if_fail ((width == -1 && height == -1) ||
608       (width > 0 && height > 0), FALSE);
609
610   iface = GST_VIDEO_OVERLAY_GET_INTERFACE (overlay);
611
612   if (iface->set_render_rectangle) {
613     iface->set_render_rectangle (overlay, x, y, width, height);
614     return TRUE;
615   }
616   return FALSE;
617 }
618
619 /**
620  * gst_is_video_overlay_prepare_window_handle_message:
621  * @msg: a #GstMessage
622  *
623  * Convenience function to check if the given message is a
624  * "prepare-window-handle" message from a #GstVideoOverlay.
625  *
626  * Returns: whether @msg is a "prepare-window-handle" message
627  */
628 gboolean
629 gst_is_video_overlay_prepare_window_handle_message (GstMessage * msg)
630 {
631   g_return_val_if_fail (msg != NULL, FALSE);
632
633   if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ELEMENT)
634     return FALSE;
635
636   return gst_message_has_name (msg, "prepare-window-handle");
637 }
638
639
640 /**
641  * gst_video_overlay_install_properties:
642  * @oclass: The class on which the properties will be installed
643  * @last_prop_id: The first free property ID to use
644  *
645  * This helper shall be used by classes implementing the #GstVideoOverlay
646  * interface that want the render rectangle to be controllable using
647  * properties. This helper will install "render-rectangle" property into the
648  * class.
649  *
650  * Since: 1.14
651  */
652 void
653 gst_video_overlay_install_properties (GObjectClass * oclass, gint last_prop_id)
654 {
655   g_object_class_install_property (oclass, last_prop_id + PROP_RENDER_RECTANGLE,
656       gst_param_spec_array ("render-rectangle", "Render Rectangle",
657           "The render rectangle ('<x, y, width, height>')",
658           g_param_spec_int ("rect-value", "Rectangle Value",
659               "One of x, y, width or height value.", G_MININT, G_MAXINT, -1,
660               G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS),
661           G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
662 }
663
664 /**
665  * gst_video_overlay_set_property:
666  * @object: The instance on which the property is set
667  * @last_prop_id: The highest property ID.
668  * @property_id: The property ID
669  * @value: The #GValue to be set
670  *
671  * This helper shall be used by classes implementing the #GstVideoOverlay
672  * interface that want the render rectangle to be controllable using
673  * properties. This helper will parse and set the render rectangle calling
674  * gst_video_overlay_set_render_rectangle().
675  *
676  * Returns: %TRUE if the @property_id matches the GstVideoOverlay property
677  *
678  * Since: 1.14
679  */
680 gboolean
681 gst_video_overlay_set_property (GObject * object, gint last_prop_id,
682     guint property_id, const GValue * value)
683 {
684   gboolean ret = FALSE;
685
686   if (property_id == last_prop_id) {
687     const GValue *v;
688     gint rect[4], i;
689
690     ret = TRUE;
691
692     if (gst_value_array_get_size (value) != 4)
693       goto wrong_format;
694
695     for (i = 0; i < 4; i++) {
696       v = gst_value_array_get_value (value, i);
697       if (!G_VALUE_HOLDS_INT (v))
698         goto wrong_format;
699
700       rect[i] = g_value_get_int (v);
701     }
702
703     gst_video_overlay_set_render_rectangle (GST_VIDEO_OVERLAY (object),
704         rect[0], rect[1], rect[2], rect[3]);
705   }
706
707   return ret;
708
709 wrong_format:
710   {
711     GValue string = G_VALUE_INIT;
712
713     g_value_init (&string, G_TYPE_STRING);
714     g_value_transform (value, &string);
715
716     g_critical ("Badly formated rectangle, must contains four gint (got '%s')",
717         g_value_get_string (&string));
718
719     g_value_unset (&string);
720     return TRUE;
721   }
722 }