plugins: use g_clear_object() wherever applicable.
[vaapi:gstreamer-vaapi.git] / gst / vaapi / gstvaapisink.c
1 /*
2  *  gstvaapisink.c - VA-API video sink
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *  Copyright (C) 2011-2012 Intel Corporation
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public License
9  *  as published by the Free Software Foundation; either version 2.1
10  *  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  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free
19  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  *  Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * SECTION:gstvaapisink
25  * @short_description: A VA-API based videosink
26  *
27  * vaapisink renders video frames to a drawable (X #Window) on a local
28  * display using the Video Acceleration (VA) API. The element will
29  * create its own internal window and render into it.
30  */
31
32 #include "config.h"
33 #include <gst/gst.h>
34 #include <gst/gstutils_version.h>
35 #include <gst/video/video.h>
36 #include <gst/video/videocontext.h>
37 #include <gst/vaapi/gstvaapivideobuffer.h>
38 #include <gst/vaapi/gstvaapivideosink.h>
39 #include <gst/vaapi/gstvaapidisplay_x11.h>
40 #include <gst/vaapi/gstvaapiwindow_x11.h>
41 #if USE_VAAPISINK_GLX
42 #include <gst/vaapi/gstvaapidisplay_glx.h>
43 #include <gst/vaapi/gstvaapiwindow_glx.h>
44 #endif
45
46 /* Supported interfaces */
47 #include <gst/interfaces/xoverlay.h>
48
49 #include "gstvaapisink.h"
50 #include "gstvaapipluginutil.h"
51
52 #define HAVE_GST_XOVERLAY_SET_WINDOW_HANDLE \
53     GST_PLUGINS_BASE_CHECK_VERSION(0,10,31)
54
55 #define HAVE_GST_XOVERLAY_SET_RENDER_RECTANGLE \
56     GST_PLUGINS_BASE_CHECK_VERSION(0,10,29)
57
58 #define GST_PLUGIN_NAME "vaapisink"
59 #define GST_PLUGIN_DESC "A VA-API based videosink"
60
61 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapisink);
62 #define GST_CAT_DEFAULT gst_debug_vaapisink
63
64 /* ElementFactory information */
65 static const GstElementDetails gst_vaapisink_details =
66     GST_ELEMENT_DETAILS(
67         "VA-API sink",
68         "Sink/Video",
69         GST_PLUGIN_DESC,
70         "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
71
72 /* Default template */
73 static GstStaticPadTemplate gst_vaapisink_sink_factory =
74     GST_STATIC_PAD_TEMPLATE(
75         "sink",
76         GST_PAD_SINK,
77         GST_PAD_ALWAYS,
78         GST_STATIC_CAPS(GST_VAAPI_SURFACE_CAPS));
79
80 static void
81 gst_vaapisink_implements_iface_init(GstImplementsInterfaceClass *iface);
82
83 static void
84 gst_vaapisink_video_context_iface_init(GstVideoContextInterface *iface);
85
86 static void
87 gst_vaapisink_xoverlay_iface_init(GstXOverlayClass *iface);
88
89 G_DEFINE_TYPE_WITH_CODE(
90     GstVaapiSink,
91     gst_vaapisink,
92     GST_TYPE_VIDEO_SINK,
93     G_IMPLEMENT_INTERFACE(GST_TYPE_IMPLEMENTS_INTERFACE,
94                           gst_vaapisink_implements_iface_init);
95     G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_CONTEXT,
96                           gst_vaapisink_video_context_iface_init);
97     G_IMPLEMENT_INTERFACE(GST_TYPE_X_OVERLAY,
98                           gst_vaapisink_xoverlay_iface_init));
99
100 enum {
101     PROP_0,
102
103     PROP_USE_GLX,
104     PROP_FULLSCREEN,
105     PROP_SYNCHRONOUS,
106     PROP_USE_REFLECTION
107 };
108
109 /* GstImplementsInterface interface */
110
111 static gboolean
112 gst_vaapisink_implements_interface_supported(
113     GstImplementsInterface *iface,
114     GType                   type
115 )
116 {
117     return (type == GST_TYPE_VIDEO_CONTEXT ||
118             type == GST_TYPE_X_OVERLAY);
119 }
120
121 static void
122 gst_vaapisink_implements_iface_init(GstImplementsInterfaceClass *iface)
123 {
124     iface->supported = gst_vaapisink_implements_interface_supported;
125 }
126
127 /* GstVaapiVideoSink interface */
128
129 static void
130 gst_vaapisink_set_video_context(GstVideoContext *context, const gchar *type,
131     const GValue *value)
132 {
133   GstVaapiSink *sink = GST_VAAPISINK (context);
134   gst_vaapi_set_display (type, value, &sink->display);
135 }
136
137 static void
138 gst_vaapisink_video_context_iface_init(GstVideoContextInterface *iface)
139 {
140     iface->set_context = gst_vaapisink_set_video_context;
141 }
142
143 /* GstXOverlay interface */
144
145 static gboolean
146 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id);
147
148 static GstFlowReturn
149 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer);
150
151 static inline void
152 _gst_vaapisink_xoverlay_set_xid(GstXOverlay *overlay, guintptr window_id)
153 {
154     GstVaapiSink * const sink = GST_VAAPISINK(overlay);
155
156     /* Disable GLX rendering when vaapisink is using a foreign X
157        window. It's pretty much useless */
158     sink->use_glx = FALSE;
159
160     sink->foreign_window = TRUE;
161     gst_vaapisink_ensure_window_xid(sink, window_id);
162 }
163
164 #if HAVE_GST_XOVERLAY_SET_WINDOW_HANDLE
165 static void
166 gst_vaapisink_xoverlay_set_window_handle(GstXOverlay *overlay, guintptr window_id)
167 {
168     _gst_vaapisink_xoverlay_set_xid(overlay, window_id);
169 }
170 #else
171 static void
172 gst_vaapisink_xoverlay_set_xid(GstXOverlay *overlay, XID xid)
173 {
174     _gst_vaapisink_xoverlay_set_xid(overlay, xid);
175 }
176 #endif
177
178 #if HAVE_GST_XOVERLAY_SET_RENDER_RECTANGLE
179 static void
180 gst_vaapisink_xoverlay_set_render_rectangle(
181     GstXOverlay *overlay,
182     gint         x,
183     gint         y,
184     gint         width,
185     gint         height
186 )
187 {
188     GstVaapiSink * const sink = GST_VAAPISINK(overlay);
189     GstVaapiRectangle * const display_rect = &sink->display_rect;
190
191     display_rect->x      = x;
192     display_rect->y      = y;
193     display_rect->width  = width;
194     display_rect->height = height;
195     
196     GST_DEBUG("render rect (%d,%d):%ux%u",
197               display_rect->x, display_rect->y,
198               display_rect->width, display_rect->height);
199 }
200 #endif
201
202 static void
203 gst_vaapisink_xoverlay_expose(GstXOverlay *overlay)
204 {
205     GstBaseSink * const base_sink = GST_BASE_SINK(overlay);
206     GstBuffer *buffer;
207
208     buffer = gst_base_sink_get_last_buffer(base_sink);
209     if (buffer) {
210         gst_vaapisink_show_frame(base_sink, buffer);
211         gst_buffer_unref(buffer);
212     }
213 }
214
215 static void
216 gst_vaapisink_xoverlay_iface_init(GstXOverlayClass *iface)
217 {
218 #if HAVE_GST_XOVERLAY_SET_WINDOW_HANDLE
219     iface->set_window_handle    = gst_vaapisink_xoverlay_set_window_handle;
220 #else
221     iface->set_xwindow_id       = gst_vaapisink_xoverlay_set_xid;
222 #endif
223 #if HAVE_GST_XOVERLAY_SET_RENDER_RECTANGLE
224     iface->set_render_rectangle = gst_vaapisink_xoverlay_set_render_rectangle;
225 #endif
226     iface->expose               = gst_vaapisink_xoverlay_expose;
227 }
228
229 static void
230 gst_vaapisink_destroy(GstVaapiSink *sink)
231 {
232     g_clear_object(&sink->texture);
233     g_clear_object(&sink->display);
234
235     gst_caps_replace(&sink->caps, NULL);
236 }
237
238 /* Checks whether a ConfigureNotify event is in the queue */
239 typedef struct _ConfigureNotifyEventPendingArgs ConfigureNotifyEventPendingArgs;
240 struct _ConfigureNotifyEventPendingArgs {
241     Window      window;
242     guint       width;
243     guint       height;
244     gboolean    match;
245 };
246
247 static Bool
248 configure_notify_event_pending_cb(Display *dpy, XEvent *xev, XPointer arg)
249 {
250     ConfigureNotifyEventPendingArgs * const args =
251         (ConfigureNotifyEventPendingArgs *)arg;
252
253     if (xev->type == ConfigureNotify &&
254         xev->xconfigure.window == args->window &&
255         xev->xconfigure.width  == args->width  &&
256         xev->xconfigure.height == args->height)
257         args->match = TRUE;
258
259     /* XXX: this is a hack to traverse the whole queue because we
260        can't use XPeekIfEvent() since it could block */
261     return False;
262 }
263
264 static gboolean
265 configure_notify_event_pending(
266     GstVaapiSink *sink,
267     Window        window,
268     guint         width,
269     guint         height
270 )
271 {
272     ConfigureNotifyEventPendingArgs args;
273     XEvent xev;
274
275     args.window = window;
276     args.width  = width;
277     args.height = height;
278     args.match  = FALSE;
279
280     /* XXX: don't use XPeekIfEvent() because it might block */
281     XCheckIfEvent(
282         gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
283         &xev,
284         configure_notify_event_pending_cb, (XPointer)&args
285     );
286     return args.match;
287 }
288
289 static gboolean
290 gst_vaapisink_ensure_render_rect(GstVaapiSink *sink, guint width, guint height)
291 {
292     GstVaapiRectangle * const display_rect = &sink->display_rect;
293     guint num, den, display_par_n, display_par_d;
294     gboolean success;
295
296     /* Return success if caps are not set yet */
297     if (!sink->caps)
298         return TRUE;
299
300     GST_DEBUG("ensure render rect within %ux%u bounds", width, height);
301
302     gst_vaapi_display_get_pixel_aspect_ratio(
303         sink->display,
304         &display_par_n, &display_par_d
305     );
306     GST_DEBUG("display pixel-aspect-ratio %d/%d",
307               display_par_n, display_par_d);
308
309     success = gst_video_calculate_display_ratio(
310         &num, &den,
311         sink->video_width, sink->video_height,
312         sink->video_par_n, sink->video_par_d,
313         display_par_n, display_par_d
314     );
315     if (!success)
316         return FALSE;
317     GST_DEBUG("video size %dx%d, calculated ratio %d/%d",
318               sink->video_width, sink->video_height, num, den);
319
320     display_rect->width = gst_util_uint64_scale_int(height, num, den);
321     if (display_rect->width <= width) {
322         GST_DEBUG("keeping window height");
323         display_rect->height = height;
324     }
325     else {
326         GST_DEBUG("keeping window width");
327         display_rect->width  = width;
328         display_rect->height =
329             gst_util_uint64_scale_int(width, den, num);
330     }
331     GST_DEBUG("scaling video to %ux%u", display_rect->width, display_rect->height);
332
333     g_assert(display_rect->width  <= width);
334     g_assert(display_rect->height <= height);
335
336     display_rect->x = (width  - display_rect->width)  / 2;
337     display_rect->y = (height - display_rect->height) / 2;
338
339     GST_DEBUG("render rect (%d,%d):%ux%u",
340               display_rect->x, display_rect->y,
341               display_rect->width, display_rect->height);
342     return TRUE;
343 }
344
345 static inline gboolean
346 gst_vaapisink_ensure_window(GstVaapiSink *sink, guint width, guint height)
347 {
348     GstVaapiDisplay * const display = sink->display;
349
350     if (!sink->window) {
351 #if USE_VAAPISINK_GLX
352         if (sink->use_glx)
353             sink->window = gst_vaapi_window_glx_new(display, width, height);
354         else
355 #endif
356             sink->window = gst_vaapi_window_x11_new(display, width, height);
357         if (sink->window)
358             gst_x_overlay_got_window_handle(
359                 GST_X_OVERLAY(sink),
360                 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window))
361             );
362     }
363     return sink->window != NULL;
364 }
365
366 static gboolean
367 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id)
368 {
369     Window rootwin;
370     unsigned int width, height, border_width, depth;
371     int x, y;
372     XID xid = window_id;
373
374     if (!gst_vaapi_ensure_display(sink, &sink->display))
375         return FALSE;
376
377     gst_vaapi_display_lock(sink->display);
378     XGetGeometry(
379         gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
380         xid,
381         &rootwin,
382         &x, &y, &width, &height, &border_width, &depth
383     );
384     gst_vaapi_display_unlock(sink->display);
385
386     if ((width != sink->window_width || height != sink->window_height) &&
387         !configure_notify_event_pending(sink, xid, width, height)) {
388         if (!gst_vaapisink_ensure_render_rect(sink, width, height))
389             return FALSE;
390         sink->window_width  = width;
391         sink->window_height = height;
392     }
393
394     if (sink->window &&
395         gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window)) == xid)
396         return TRUE;
397
398     g_clear_object(&sink->window);
399
400 #if USE_VAAPISINK_GLX
401     if (sink->use_glx)
402         sink->window = gst_vaapi_window_glx_new_with_xid(sink->display, xid);
403     else
404 #endif
405         sink->window = gst_vaapi_window_x11_new_with_xid(sink->display, xid);
406     return sink->window != NULL;
407 }
408
409 static gboolean
410 gst_vaapisink_start(GstBaseSink *base_sink)
411 {
412     GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
413
414     return gst_vaapi_ensure_display(sink, &sink->display);
415 }
416
417 static gboolean
418 gst_vaapisink_stop(GstBaseSink *base_sink)
419 {
420     GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
421
422     g_clear_object(&sink->window);
423     g_clear_object(&sink->display);
424
425     return TRUE;
426 }
427
428 static gboolean
429 gst_vaapisink_set_caps(GstBaseSink *base_sink, GstCaps *caps)
430 {
431     GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
432     GstStructure * const structure = gst_caps_get_structure(caps, 0);
433     guint win_width, win_height, display_width, display_height;
434     gint video_width, video_height, video_par_n = 1, video_par_d = 1;
435
436     if (!structure)
437         return FALSE;
438     if (!gst_structure_get_int(structure, "width",  &video_width))
439         return FALSE;
440     if (!gst_structure_get_int(structure, "height", &video_height))
441         return FALSE;
442     sink->video_width  = video_width;
443     sink->video_height = video_height;
444
445     gst_video_parse_caps_pixel_aspect_ratio(caps, &video_par_n, &video_par_d);
446     sink->video_par_n  = video_par_n;
447     sink->video_par_d  = video_par_d;
448     GST_DEBUG("video pixel-aspect-ratio %d/%d", video_par_n, video_par_d);
449
450     gst_caps_replace(&sink->caps, caps);
451
452     if (!gst_vaapi_ensure_display(sink, &sink->display))
453         return FALSE;
454
455     gst_vaapi_display_get_size(sink->display, &display_width, &display_height);
456     if (sink->foreign_window) {
457         win_width  = sink->window_width;
458         win_height = sink->window_height;
459     }
460     else if (sink->fullscreen ||
461              video_width > display_width || video_height > display_height) {
462         win_width  = display_width;
463         win_height = display_height;
464     }
465     else {
466         win_width  = video_width;
467         win_height = video_height;
468     }
469
470     if (sink->window) {
471         if (!sink->foreign_window || sink->fullscreen)
472             gst_vaapi_window_set_size(sink->window, win_width, win_height);
473     }
474     else {
475         gst_vaapi_display_lock(sink->display);
476         gst_x_overlay_prepare_xwindow_id(GST_X_OVERLAY(sink));
477         gst_vaapi_display_unlock(sink->display);
478         if (sink->window)
479             return TRUE;
480         if (!gst_vaapisink_ensure_window(sink, win_width, win_height))
481             return FALSE;
482         gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
483         gst_vaapi_window_show(sink->window);
484         gst_vaapi_window_get_size(sink->window, &win_width, &win_height);
485     }
486     sink->window_width  = win_width;
487     sink->window_height = win_height;
488     GST_DEBUG("window size %ux%u", win_width, win_height);
489
490     return gst_vaapisink_ensure_render_rect(sink, win_width, win_height);
491 }
492
493 #if USE_VAAPISINK_GLX
494 static void
495 render_background(GstVaapiSink *sink)
496 {
497     /* Original code from Mirco Muller (MacSlow):
498        <http://cgit.freedesktop.org/~macslow/gl-gst-player/> */
499     GLfloat fStartX = 0.0f;
500     GLfloat fStartY = 0.0f;
501     GLfloat fWidth  = (GLfloat)sink->window_width;
502     GLfloat fHeight = (GLfloat)sink->window_height;
503
504     glClear(GL_COLOR_BUFFER_BIT);
505     glBegin(GL_QUADS);
506     {
507         /* top third, darker grey to white */
508         glColor3f(0.85f, 0.85f, 0.85f);
509         glVertex3f(fStartX, fStartY, 0.0f);
510         glColor3f(0.85f, 0.85f, 0.85f);
511         glVertex3f(fStartX + fWidth, fStartY, 0.0f);
512         glColor3f(1.0f, 1.0f, 1.0f);
513         glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
514         glColor3f(1.0f, 1.0f, 1.0f);
515         glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
516
517         /* middle third, just plain white */
518         glColor3f(1.0f, 1.0f, 1.0f);
519         glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
520         glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
521         glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
522         glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
523
524         /* bottom third, white to lighter grey */
525         glColor3f(1.0f, 1.0f, 1.0f);
526         glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
527         glColor3f(1.0f, 1.0f, 1.0f);
528         glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
529         glColor3f(0.62f, 0.66f, 0.69f);
530         glVertex3f(fStartX + fWidth, fStartY + fHeight, 0.0f);
531         glColor3f(0.62f, 0.66f, 0.69f);
532         glVertex3f(fStartX, fStartY + fHeight, 0.0f);
533     }
534     glEnd();
535 }
536
537 static void
538 render_frame(GstVaapiSink *sink)
539 {
540     const guint x1 = sink->display_rect.x;
541     const guint x2 = sink->display_rect.x + sink->display_rect.width;
542     const guint y1 = sink->display_rect.y;
543     const guint y2 = sink->display_rect.y + sink->display_rect.height;
544
545     glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
546     glBegin(GL_QUADS);
547     {
548         glTexCoord2f(0.0f, 0.0f); glVertex2i(x1, y1);
549         glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y2);
550         glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y2);
551         glTexCoord2f(1.0f, 0.0f); glVertex2i(x2, y1);
552     }
553     glEnd();
554 }
555
556 static void
557 render_reflection(GstVaapiSink *sink)
558 {
559     const guint x1 = sink->display_rect.x;
560     const guint x2 = sink->display_rect.x + sink->display_rect.width;
561     const guint y1 = sink->display_rect.y;
562     const guint rh = sink->display_rect.height / 5;
563     GLfloat     ry = 1.0f - (GLfloat)rh / (GLfloat)sink->display_rect.height;
564
565     glBegin(GL_QUADS);
566     {
567         glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
568         glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y1);
569         glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y1);
570
571         glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
572         glTexCoord2f(1.0f, ry); glVertex2i(x2, y1 + rh);
573         glTexCoord2f(0.0f, ry); glVertex2i(x1, y1 + rh);
574     }
575     glEnd();
576 }
577
578 static gboolean
579 gst_vaapisink_show_frame_glx(
580     GstVaapiSink    *sink,
581     GstVaapiSurface *surface,
582     guint            flags
583 )
584 {
585     GstVaapiWindowGLX * const window = GST_VAAPI_WINDOW_GLX(sink->window);
586     GLenum target;
587     GLuint texture;
588
589     gst_vaapi_window_glx_make_current(window);
590     if (!sink->texture) {
591         sink->texture = gst_vaapi_texture_new(
592             sink->display,
593             GL_TEXTURE_2D,
594             GL_BGRA,
595             sink->video_width,
596             sink->video_height
597         );
598         if (!sink->texture)
599             goto error_create_texture;
600     }
601     if (!gst_vaapi_texture_put_surface(sink->texture, surface, flags))
602         goto error_transfer_surface;
603
604     target  = gst_vaapi_texture_get_target(sink->texture);
605     texture = gst_vaapi_texture_get_id(sink->texture);
606     if (target != GL_TEXTURE_2D || !texture)
607         return FALSE;
608
609     if (sink->use_reflection)
610         render_background(sink);
611
612     glEnable(target);
613     glBindTexture(target, texture);
614     {
615         if (sink->use_reflection) {
616             glPushMatrix();
617             glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
618             glTranslatef(50.0f, 0.0f, 0.0f);
619         }
620         render_frame(sink);
621         if (sink->use_reflection) {
622             glPushMatrix();
623             glTranslatef(0.0, (GLfloat)sink->display_rect.height + 5.0f, 0.0f);
624             render_reflection(sink);
625             glPopMatrix();
626             glPopMatrix();
627         }
628     }
629     glBindTexture(target, 0);
630     glDisable(target);
631     gst_vaapi_window_glx_swap_buffers(window);
632     return TRUE;
633
634     /* ERRORS */
635 error_create_texture:
636     {
637         GST_DEBUG("could not create VA/GLX texture");
638         return FALSE;
639     }
640 error_transfer_surface:
641     {
642         GST_DEBUG("could not transfer VA surface to texture");
643         return FALSE;
644     }
645 }
646 #endif
647
648 static inline gboolean
649 gst_vaapisink_show_frame_x11(
650     GstVaapiSink    *sink,
651     GstVaapiSurface *surface,
652     guint            flags
653 )
654 {
655     if (!gst_vaapi_window_put_surface(sink->window, surface,
656                 NULL, &sink->display_rect, flags)) {
657         GST_DEBUG("could not render VA surface");
658         return FALSE;
659     }
660     return TRUE;
661 }
662
663 static GstFlowReturn
664 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer)
665 {
666     GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
667     GstVaapiVideoBuffer * const vbuffer = GST_VAAPI_VIDEO_BUFFER(buffer);
668     GstVaapiSurface *surface;
669     guint flags;
670     gboolean success;
671     GstVideoOverlayComposition * const composition =
672         gst_video_buffer_get_overlay_composition(buffer);
673
674     if (sink->display != gst_vaapi_video_buffer_get_display (vbuffer)) {
675       g_clear_object(&sink->display);
676       sink->display = g_object_ref (gst_vaapi_video_buffer_get_display (vbuffer));
677     }
678
679     if (!sink->window)
680         return GST_FLOW_UNEXPECTED;
681
682     surface = gst_vaapi_video_buffer_get_surface(vbuffer);
683     if (!surface)
684         return GST_FLOW_UNEXPECTED;
685
686     GST_DEBUG("render surface %" GST_VAAPI_ID_FORMAT,
687               GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
688
689     flags = gst_vaapi_video_buffer_get_render_flags(vbuffer);
690
691     if (!gst_vaapi_surface_set_subpictures_from_composition(surface,
692              composition, TRUE))
693         GST_WARNING("could not update subtitles");
694
695 #if USE_VAAPISINK_GLX
696     if (sink->use_glx)
697         success = gst_vaapisink_show_frame_glx(sink, surface, flags);
698     else
699 #endif
700         success = gst_vaapisink_show_frame_x11(sink, surface, flags);
701     return success ? GST_FLOW_OK : GST_FLOW_UNEXPECTED;
702 }
703
704 static gboolean
705 gst_vaapisink_query(GstBaseSink *base_sink, GstQuery *query)
706 {
707     GstVaapiSink *sink = GST_VAAPISINK(base_sink);
708     GST_DEBUG ("sharing display %p", sink->display);
709     return gst_vaapi_reply_to_query (query, sink->display);
710 }
711
712 static void
713 gst_vaapisink_finalize(GObject *object)
714 {
715     gst_vaapisink_destroy(GST_VAAPISINK(object));
716
717     G_OBJECT_CLASS(gst_vaapisink_parent_class)->finalize(object);
718 }
719
720 static void
721 gst_vaapisink_set_property(
722     GObject      *object,
723     guint         prop_id,
724     const GValue *value,
725     GParamSpec   *pspec
726 )
727 {
728     GstVaapiSink * const sink = GST_VAAPISINK(object);
729
730     switch (prop_id) {
731     case PROP_USE_GLX:
732         sink->use_glx = g_value_get_boolean(value);
733         break;
734     case PROP_FULLSCREEN:
735         sink->fullscreen = g_value_get_boolean(value);
736         break;
737     case PROP_SYNCHRONOUS:
738         sink->synchronous = g_value_get_boolean(value);
739         break;
740     case PROP_USE_REFLECTION:
741         sink->use_reflection = g_value_get_boolean(value);
742         break;
743     default:
744         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
745         break;
746     }
747 }
748
749 static void
750 gst_vaapisink_get_property(
751     GObject    *object,
752     guint       prop_id,
753     GValue     *value,
754     GParamSpec *pspec
755 )
756 {
757     GstVaapiSink * const sink = GST_VAAPISINK(object);
758
759     switch (prop_id) {
760     case PROP_USE_GLX:
761         g_value_set_boolean(value, sink->use_glx);
762         break;
763     case PROP_FULLSCREEN:
764         g_value_set_boolean(value, sink->fullscreen);
765         break;
766     case PROP_SYNCHRONOUS:
767         g_value_set_boolean(value, sink->synchronous);
768         break;
769     case PROP_USE_REFLECTION:
770         g_value_set_boolean(value, sink->use_reflection);
771         break;
772     default:
773         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
774         break;
775     }
776 }
777
778 static void
779 gst_vaapisink_class_init(GstVaapiSinkClass *klass)
780 {
781     GObjectClass * const     object_class   = G_OBJECT_CLASS(klass);
782     GstElementClass * const  element_class  = GST_ELEMENT_CLASS(klass);
783     GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
784     GstPadTemplate *pad_template;
785
786     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
787                             GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
788
789     object_class->finalize       = gst_vaapisink_finalize;
790     object_class->set_property   = gst_vaapisink_set_property;
791     object_class->get_property   = gst_vaapisink_get_property;
792
793     basesink_class->start        = gst_vaapisink_start;
794     basesink_class->stop         = gst_vaapisink_stop;
795     basesink_class->set_caps     = gst_vaapisink_set_caps;
796     basesink_class->preroll      = gst_vaapisink_show_frame;
797     basesink_class->render       = gst_vaapisink_show_frame;
798     basesink_class->query        = gst_vaapisink_query;
799
800     gst_element_class_set_details_simple(
801         element_class,
802         gst_vaapisink_details.longname,
803         gst_vaapisink_details.klass,
804         gst_vaapisink_details.description,
805         gst_vaapisink_details.author
806     );
807
808     pad_template = gst_static_pad_template_get(&gst_vaapisink_sink_factory);
809     gst_element_class_add_pad_template(element_class, pad_template);
810     gst_object_unref(pad_template);
811
812 #if USE_VAAPISINK_GLX
813     g_object_class_install_property
814         (object_class,
815          PROP_USE_GLX,
816          g_param_spec_boolean("use-glx",
817                               "GLX rendering",
818                               "Enables GLX rendering",
819                               TRUE,
820                               G_PARAM_READWRITE));
821
822     g_object_class_install_property
823         (object_class,
824          PROP_USE_REFLECTION,
825          g_param_spec_boolean("use-reflection",
826                               "Reflection effect",
827                               "Enables OpenGL reflection effect",
828                               FALSE,
829                               G_PARAM_READWRITE));
830 #endif
831
832     g_object_class_install_property
833         (object_class,
834          PROP_FULLSCREEN,
835          g_param_spec_boolean("fullscreen",
836                               "Fullscreen",
837                               "Requests window in fullscreen state",
838                               FALSE,
839                               G_PARAM_READWRITE));
840
841     /**
842      * GstVaapiSink:synchronous:
843      *
844      * When enabled, runs the X display in synchronous mode. Note that
845      * this is used only for debugging.
846      */
847     g_object_class_install_property
848         (object_class,
849          PROP_SYNCHRONOUS,
850          g_param_spec_boolean("synchronous",
851                               "Synchronous mode",
852                               "Toggles X display synchronous mode",
853                               FALSE,
854                               G_PARAM_READWRITE));
855 }
856
857 static void
858 gst_vaapisink_init(GstVaapiSink *sink)
859 {
860     sink->caps           = NULL;
861     sink->display        = NULL;
862     sink->window         = NULL;
863     sink->window_width   = 0;
864     sink->window_height  = 0;
865     sink->texture        = NULL;
866     sink->video_width    = 0;
867     sink->video_height   = 0;
868     sink->video_par_n    = 1;
869     sink->video_par_d    = 1;
870     sink->foreign_window = FALSE;
871     sink->fullscreen     = FALSE;
872     sink->synchronous    = FALSE;
873     sink->use_glx        = USE_VAAPISINK_GLX;
874     sink->use_reflection = FALSE;
875 }