2 * gstvaapisink.c - VA-API video sink
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Copyright (C) 2011-2012 Intel Corporation
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.
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.
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
24 * SECTION:gstvaapisink
25 * @short_description: A VA-API based videosink
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.
32 #include "gst/vaapi/sysdeps.h"
34 #include <gst/video/video.h>
35 #include <gst/video/videocontext.h>
36 #include <gst/vaapi/gstvaapivalue.h>
37 #include <gst/vaapi/gstvaapivideobuffer.h>
39 # include <gst/vaapi/gstvaapidisplay_drm.h>
42 # include <gst/vaapi/gstvaapidisplay_x11.h>
43 # include <gst/vaapi/gstvaapiwindow_x11.h>
46 # include <gst/vaapi/gstvaapidisplay_glx.h>
47 # include <gst/vaapi/gstvaapiwindow_glx.h>
50 # include <gst/vaapi/gstvaapidisplay_wayland.h>
51 # include <gst/vaapi/gstvaapiwindow_wayland.h>
54 /* Supported interfaces */
55 #include <gst/interfaces/xoverlay.h>
57 #include "gstvaapisink.h"
58 #include "gstvaapipluginutil.h"
60 #define GST_PLUGIN_NAME "vaapisink"
61 #define GST_PLUGIN_DESC "A VA-API based videosink"
63 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapisink);
64 #define GST_CAT_DEFAULT gst_debug_vaapisink
66 /* ElementFactory information */
67 static const GstElementDetails gst_vaapisink_details =
72 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
74 /* Default template */
75 static const char gst_vaapisink_sink_caps_str[] =
77 "width = (int) [ 1, MAX ], "
78 "height = (int) [ 1, MAX ]; "
79 GST_VAAPI_SURFACE_CAPS;
81 static GstStaticPadTemplate gst_vaapisink_sink_factory =
82 GST_STATIC_PAD_TEMPLATE(
86 GST_STATIC_CAPS(gst_vaapisink_sink_caps_str));
89 gst_vaapisink_implements_iface_init(GstImplementsInterfaceClass *iface);
92 gst_vaapisink_video_context_iface_init(GstVideoContextInterface *iface);
95 gst_vaapisink_xoverlay_iface_init(GstXOverlayClass *iface);
97 G_DEFINE_TYPE_WITH_CODE(
101 G_IMPLEMENT_INTERFACE(GST_TYPE_IMPLEMENTS_INTERFACE,
102 gst_vaapisink_implements_iface_init);
103 G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_CONTEXT,
104 gst_vaapisink_video_context_iface_init);
105 G_IMPLEMENT_INTERFACE(GST_TYPE_X_OVERLAY,
106 gst_vaapisink_xoverlay_iface_init))
118 #define DEFAULT_DISPLAY_TYPE GST_VAAPI_DISPLAY_TYPE_ANY
119 #define DEFAULT_ROTATION GST_VAAPI_ROTATION_0
121 /* GstImplementsInterface interface */
124 gst_vaapisink_implements_interface_supported(
125 GstImplementsInterface *iface,
129 return (type == GST_TYPE_VIDEO_CONTEXT ||
130 type == GST_TYPE_X_OVERLAY);
134 gst_vaapisink_implements_iface_init(GstImplementsInterfaceClass *iface)
136 iface->supported = gst_vaapisink_implements_interface_supported;
139 /* GstVideoContext interface */
142 gst_vaapisink_set_video_context(GstVideoContext *context, const gchar *type,
145 GstVaapiSink *sink = GST_VAAPISINK (context);
146 gst_vaapi_set_display (type, value, &sink->display);
150 gst_vaapisink_video_context_iface_init(GstVideoContextInterface *iface)
152 iface->set_context = gst_vaapisink_set_video_context;
155 /* GstXOverlay interface */
159 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id);
163 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer);
166 gst_vaapisink_xoverlay_set_window_handle(GstXOverlay *overlay, guintptr window)
168 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
170 /* Disable GLX rendering when vaapisink is using a foreign X
171 window. It's pretty much useless */
172 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_GLX)
173 sink->display_type = GST_VAAPI_DISPLAY_TYPE_X11;
175 sink->foreign_window = TRUE;
177 switch (sink->display_type) {
179 case GST_VAAPI_DISPLAY_TYPE_X11:
180 gst_vaapisink_ensure_window_xid(sink, window);
189 gst_vaapisink_xoverlay_set_render_rectangle(
190 GstXOverlay *overlay,
197 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
198 GstVaapiRectangle * const display_rect = &sink->display_rect;
202 display_rect->width = width;
203 display_rect->height = height;
205 GST_DEBUG("render rect (%d,%d):%ux%u",
206 display_rect->x, display_rect->y,
207 display_rect->width, display_rect->height);
211 gst_vaapisink_xoverlay_expose(GstXOverlay *overlay)
213 GstBaseSink * const base_sink = GST_BASE_SINK(overlay);
216 buffer = gst_base_sink_get_last_buffer(base_sink);
218 gst_vaapisink_show_frame(base_sink, buffer);
219 gst_buffer_unref(buffer);
224 gst_vaapisink_xoverlay_iface_init(GstXOverlayClass *iface)
226 iface->set_window_handle = gst_vaapisink_xoverlay_set_window_handle;
227 iface->set_render_rectangle = gst_vaapisink_xoverlay_set_render_rectangle;
228 iface->expose = gst_vaapisink_xoverlay_expose;
232 gst_vaapisink_destroy(GstVaapiSink *sink)
234 gst_buffer_replace(&sink->video_buffer, NULL);
235 g_clear_object(&sink->texture);
236 g_clear_object(&sink->display);
237 g_clear_object(&sink->uploader);
239 gst_caps_replace(&sink->caps, NULL);
243 /* Checks whether a ConfigureNotify event is in the queue */
244 typedef struct _ConfigureNotifyEventPendingArgs ConfigureNotifyEventPendingArgs;
245 struct _ConfigureNotifyEventPendingArgs {
253 configure_notify_event_pending_cb(Display *dpy, XEvent *xev, XPointer arg)
255 ConfigureNotifyEventPendingArgs * const args =
256 (ConfigureNotifyEventPendingArgs *)arg;
258 if (xev->type == ConfigureNotify &&
259 xev->xconfigure.window == args->window &&
260 xev->xconfigure.width == args->width &&
261 xev->xconfigure.height == args->height)
264 /* XXX: this is a hack to traverse the whole queue because we
265 can't use XPeekIfEvent() since it could block */
270 configure_notify_event_pending(
277 ConfigureNotifyEventPendingArgs args;
280 args.window = window;
282 args.height = height;
285 /* XXX: don't use XPeekIfEvent() because it might block */
287 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
289 configure_notify_event_pending_cb, (XPointer)&args
296 get_display_type_name(GstVaapiDisplayType display_type)
298 gpointer const klass = g_type_class_peek(GST_VAAPI_TYPE_DISPLAY_TYPE);
299 GEnumValue * const e = g_enum_get_value(klass, display_type);
302 return e->value_name;
303 return "<unknown-type>";
306 static inline gboolean
307 gst_vaapisink_ensure_display(GstVaapiSink *sink)
309 GstVaapiDisplayType display_type;
310 GstVaapiRenderMode render_mode;
312 if (!gst_vaapi_ensure_display(sink, sink->display_type, &sink->display))
315 display_type = gst_vaapi_display_get_display_type(sink->display);
316 if (display_type != sink->display_type) {
317 GST_INFO("created %s %p", get_display_type_name(display_type),
319 sink->display_type = display_type;
322 gst_vaapi_display_get_render_mode(sink->display, &render_mode) &&
323 render_mode == GST_VAAPI_RENDER_MODE_OVERLAY;
324 GST_DEBUG("use %s rendering mode", sink->use_overlay ? "overlay" : "texture");
326 sink->use_rotation = gst_vaapi_display_has_property(
327 sink->display, GST_VAAPI_DISPLAY_PROP_ROTATION);
333 gst_vaapisink_ensure_uploader(GstVaapiSink *sink)
335 if (!gst_vaapisink_ensure_display(sink))
338 if (!sink->uploader) {
339 sink->uploader = gst_vaapi_uploader_new(sink->display);
347 gst_vaapisink_ensure_render_rect(GstVaapiSink *sink, guint width, guint height)
349 GstVaapiRectangle * const display_rect = &sink->display_rect;
350 guint num, den, display_par_n, display_par_d;
353 /* Return success if caps are not set yet */
357 GST_DEBUG("ensure render rect within %ux%u bounds", width, height);
359 gst_vaapi_display_get_pixel_aspect_ratio(
361 &display_par_n, &display_par_d
363 GST_DEBUG("display pixel-aspect-ratio %d/%d",
364 display_par_n, display_par_d);
366 success = gst_video_calculate_display_ratio(
368 sink->video_width, sink->video_height,
369 sink->video_par_n, sink->video_par_d,
370 display_par_n, display_par_d
374 GST_DEBUG("video size %dx%d, calculated ratio %d/%d",
375 sink->video_width, sink->video_height, num, den);
377 display_rect->width = gst_util_uint64_scale_int(height, num, den);
378 if (display_rect->width <= width) {
379 GST_DEBUG("keeping window height");
380 display_rect->height = height;
383 GST_DEBUG("keeping window width");
384 display_rect->width = width;
385 display_rect->height =
386 gst_util_uint64_scale_int(width, den, num);
388 GST_DEBUG("scaling video to %ux%u", display_rect->width, display_rect->height);
390 g_assert(display_rect->width <= width);
391 g_assert(display_rect->height <= height);
393 display_rect->x = (width - display_rect->width) / 2;
394 display_rect->y = (height - display_rect->height) / 2;
396 GST_DEBUG("render rect (%d,%d):%ux%u",
397 display_rect->x, display_rect->y,
398 display_rect->width, display_rect->height);
403 gst_vaapisink_ensure_window_size(GstVaapiSink *sink, guint *pwidth, guint *pheight)
405 GstVideoRectangle src_rect, dst_rect, out_rect;
406 guint num, den, display_width, display_height, display_par_n, display_par_d;
407 gboolean success, scale;
409 if (sink->foreign_window) {
410 *pwidth = sink->window_width;
411 *pheight = sink->window_height;
415 gst_vaapi_display_get_size(sink->display, &display_width, &display_height);
416 if (sink->fullscreen) {
417 *pwidth = display_width;
418 *pheight = display_height;
422 gst_vaapi_display_get_pixel_aspect_ratio(
424 &display_par_n, &display_par_d
427 success = gst_video_calculate_display_ratio(
429 sink->video_width, sink->video_height,
430 sink->video_par_n, sink->video_par_d,
431 display_par_n, display_par_d
434 num = sink->video_par_n;
435 den = sink->video_par_d;
440 src_rect.w = gst_util_uint64_scale_int(sink->video_height, num, den);
441 src_rect.h = sink->video_height;
444 dst_rect.w = display_width;
445 dst_rect.h = display_height;
446 scale = (src_rect.w > dst_rect.w || src_rect.h > dst_rect.h);
447 gst_video_sink_center_rect(src_rect, dst_rect, &out_rect, scale);
448 *pwidth = out_rect.w;
449 *pheight = out_rect.h;
452 static inline gboolean
453 gst_vaapisink_ensure_window(GstVaapiSink *sink, guint width, guint height)
455 GstVaapiDisplay * const display = sink->display;
458 switch (sink->display_type) {
460 case GST_VAAPI_DISPLAY_TYPE_GLX:
461 sink->window = gst_vaapi_window_glx_new(display, width, height);
462 goto notify_xoverlay_interface;
465 case GST_VAAPI_DISPLAY_TYPE_X11:
466 sink->window = gst_vaapi_window_x11_new(display, width, height);
467 notify_xoverlay_interface:
470 gst_x_overlay_got_window_handle(
472 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window))
477 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
478 sink->window = gst_vaapi_window_wayland_new(display, width, height);
482 GST_ERROR("unsupported display type %d", sink->display_type);
486 return sink->window != NULL;
491 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id)
494 unsigned int width, height, border_width, depth;
498 if (!gst_vaapisink_ensure_display(sink))
501 gst_vaapi_display_lock(sink->display);
503 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
506 &x, &y, &width, &height, &border_width, &depth
508 gst_vaapi_display_unlock(sink->display);
510 if ((width != sink->window_width || height != sink->window_height) &&
511 !configure_notify_event_pending(sink, xid, width, height)) {
512 if (!gst_vaapisink_ensure_render_rect(sink, width, height))
514 sink->window_width = width;
515 sink->window_height = height;
519 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window)) == xid)
522 g_clear_object(&sink->window);
524 switch (sink->display_type) {
526 case GST_VAAPI_DISPLAY_TYPE_GLX:
527 sink->window = gst_vaapi_window_glx_new_with_xid(sink->display, xid);
530 case GST_VAAPI_DISPLAY_TYPE_X11:
531 sink->window = gst_vaapi_window_x11_new_with_xid(sink->display, xid);
534 GST_ERROR("unsupported display type %d", sink->display_type);
537 return sink->window != NULL;
542 gst_vaapisink_ensure_rotation(GstVaapiSink *sink, gboolean recalc_display_rect)
544 gboolean success = FALSE;
546 g_return_val_if_fail(sink->display, FALSE);
548 if (sink->rotation == sink->rotation_req)
551 if (!sink->use_rotation) {
552 GST_WARNING("VA display does not support rotation");
556 gst_vaapi_display_lock(sink->display);
557 success = gst_vaapi_display_set_rotation(sink->display, sink->rotation_req);
558 gst_vaapi_display_unlock(sink->display);
560 GST_ERROR("failed to change VA display rotation mode");
564 if (((sink->rotation + sink->rotation_req) % 180) == 90) {
565 /* Orientation changed */
566 G_PRIMITIVE_SWAP(guint, sink->video_width, sink->video_height);
567 G_PRIMITIVE_SWAP(gint, sink->video_par_n, sink->video_par_d);
570 if (recalc_display_rect && !sink->foreign_window)
571 gst_vaapisink_ensure_render_rect(sink, sink->window_width,
572 sink->window_height);
576 sink->rotation = sink->rotation_req;
581 gst_vaapisink_start(GstBaseSink *base_sink)
583 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
585 if (!gst_vaapisink_ensure_display(sink))
588 sink->uploader = gst_vaapi_uploader_new(sink->display);
595 gst_vaapisink_stop(GstBaseSink *base_sink)
597 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
599 gst_buffer_replace(&sink->video_buffer, NULL);
600 g_clear_object(&sink->window);
601 g_clear_object(&sink->display);
602 g_clear_object(&sink->uploader);
608 gst_vaapisink_get_caps(GstBaseSink *base_sink)
610 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
611 GstCaps *out_caps, *yuv_caps;
613 out_caps = gst_caps_from_string(GST_VAAPI_SURFACE_CAPS);
617 if (gst_vaapisink_ensure_uploader(sink)) {
618 yuv_caps = gst_vaapi_uploader_get_caps(sink->uploader);
620 gst_caps_append(out_caps, gst_caps_copy(yuv_caps));
626 gst_vaapisink_set_caps(GstBaseSink *base_sink, GstCaps *caps)
628 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
629 GstStructure * const structure = gst_caps_get_structure(caps, 0);
630 guint win_width, win_height;
631 gint video_width, video_height, video_par_n = 1, video_par_d = 1;
634 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_DRM)
640 if (!gst_structure_get_int(structure, "width", &video_width))
642 if (!gst_structure_get_int(structure, "height", &video_height))
644 sink->video_width = video_width;
645 sink->video_height = video_height;
647 if (gst_structure_has_name(structure, "video/x-raw-yuv"))
648 sink->use_video_raw = TRUE;
650 gst_video_parse_caps_pixel_aspect_ratio(caps, &video_par_n, &video_par_d);
651 sink->video_par_n = video_par_n;
652 sink->video_par_d = video_par_d;
653 GST_DEBUG("video pixel-aspect-ratio %d/%d", video_par_n, video_par_d);
655 gst_caps_replace(&sink->caps, caps);
657 if (!gst_vaapisink_ensure_display(sink))
660 gst_vaapisink_ensure_rotation(sink, FALSE);
662 gst_vaapisink_ensure_window_size(sink, &win_width, &win_height);
664 if (!sink->foreign_window || sink->fullscreen)
665 gst_vaapi_window_set_size(sink->window, win_width, win_height);
668 gst_vaapi_display_lock(sink->display);
669 gst_x_overlay_prepare_xwindow_id(GST_X_OVERLAY(sink));
670 gst_vaapi_display_unlock(sink->display);
673 if (!gst_vaapisink_ensure_window(sink, win_width, win_height))
675 gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
676 gst_vaapi_window_show(sink->window);
677 gst_vaapi_window_get_size(sink->window, &win_width, &win_height);
679 sink->window_width = win_width;
680 sink->window_height = win_height;
681 GST_DEBUG("window size %ux%u", win_width, win_height);
683 return gst_vaapisink_ensure_render_rect(sink, win_width, win_height);
688 render_background(GstVaapiSink *sink)
690 /* Original code from Mirco Muller (MacSlow):
691 <http://cgit.freedesktop.org/~macslow/gl-gst-player/> */
692 GLfloat fStartX = 0.0f;
693 GLfloat fStartY = 0.0f;
694 GLfloat fWidth = (GLfloat)sink->window_width;
695 GLfloat fHeight = (GLfloat)sink->window_height;
697 glClear(GL_COLOR_BUFFER_BIT);
700 /* top third, darker grey to white */
701 glColor3f(0.85f, 0.85f, 0.85f);
702 glVertex3f(fStartX, fStartY, 0.0f);
703 glColor3f(0.85f, 0.85f, 0.85f);
704 glVertex3f(fStartX + fWidth, fStartY, 0.0f);
705 glColor3f(1.0f, 1.0f, 1.0f);
706 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
707 glColor3f(1.0f, 1.0f, 1.0f);
708 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
710 /* middle third, just plain white */
711 glColor3f(1.0f, 1.0f, 1.0f);
712 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
713 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
714 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
715 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
717 /* bottom third, white to lighter grey */
718 glColor3f(1.0f, 1.0f, 1.0f);
719 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
720 glColor3f(1.0f, 1.0f, 1.0f);
721 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
722 glColor3f(0.62f, 0.66f, 0.69f);
723 glVertex3f(fStartX + fWidth, fStartY + fHeight, 0.0f);
724 glColor3f(0.62f, 0.66f, 0.69f);
725 glVertex3f(fStartX, fStartY + fHeight, 0.0f);
731 render_frame(GstVaapiSink *sink)
733 const guint x1 = sink->display_rect.x;
734 const guint x2 = sink->display_rect.x + sink->display_rect.width;
735 const guint y1 = sink->display_rect.y;
736 const guint y2 = sink->display_rect.y + sink->display_rect.height;
738 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
741 glTexCoord2f(0.0f, 0.0f); glVertex2i(x1, y1);
742 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y2);
743 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y2);
744 glTexCoord2f(1.0f, 0.0f); glVertex2i(x2, y1);
750 render_reflection(GstVaapiSink *sink)
752 const guint x1 = sink->display_rect.x;
753 const guint x2 = sink->display_rect.x + sink->display_rect.width;
754 const guint y1 = sink->display_rect.y;
755 const guint rh = sink->display_rect.height / 5;
756 GLfloat ry = 1.0f - (GLfloat)rh / (GLfloat)sink->display_rect.height;
760 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
761 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y1);
762 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y1);
764 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
765 glTexCoord2f(1.0f, ry); glVertex2i(x2, y1 + rh);
766 glTexCoord2f(0.0f, ry); glVertex2i(x1, y1 + rh);
772 gst_vaapisink_show_frame_glx(
774 GstVaapiSurface *surface,
778 GstVaapiWindowGLX * const window = GST_VAAPI_WINDOW_GLX(sink->window);
782 gst_vaapi_window_glx_make_current(window);
783 if (!sink->texture) {
784 sink->texture = gst_vaapi_texture_new(
792 goto error_create_texture;
794 if (!gst_vaapi_texture_put_surface(sink->texture, surface, flags))
795 goto error_transfer_surface;
797 target = gst_vaapi_texture_get_target(sink->texture);
798 texture = gst_vaapi_texture_get_id(sink->texture);
799 if (target != GL_TEXTURE_2D || !texture)
802 if (sink->use_reflection)
803 render_background(sink);
806 glBindTexture(target, texture);
808 if (sink->use_reflection) {
810 glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
811 glTranslatef(50.0f, 0.0f, 0.0f);
814 if (sink->use_reflection) {
816 glTranslatef(0.0, (GLfloat)sink->display_rect.height + 5.0f, 0.0f);
817 render_reflection(sink);
822 glBindTexture(target, 0);
824 gst_vaapi_window_glx_swap_buffers(window);
828 error_create_texture:
830 GST_DEBUG("could not create VA/GLX texture");
833 error_transfer_surface:
835 GST_DEBUG("could not transfer VA surface to texture");
841 static inline gboolean
842 gst_vaapisink_put_surface(
844 GstVaapiSurface *surface,
848 if (!gst_vaapi_window_put_surface(sink->window, surface,
849 NULL, &sink->display_rect, flags)) {
850 GST_DEBUG("could not render VA surface");
857 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *src_buffer)
859 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
860 GstVideoOverlayComposition *composition;
861 GstVaapiVideoBuffer *vbuffer;
862 GstVaapiSurface *surface;
867 if (GST_VAAPI_IS_VIDEO_BUFFER(src_buffer))
868 buffer = gst_buffer_ref(src_buffer);
869 else if (GST_VAAPI_IS_VIDEO_BUFFER(src_buffer->parent))
870 buffer = gst_buffer_ref(src_buffer->parent);
871 else if (sink->use_video_raw)
872 buffer = gst_vaapi_uploader_get_buffer(sink->uploader);
876 return GST_FLOW_UNEXPECTED;
878 if (sink->use_video_raw && !gst_vaapi_uploader_process(sink->uploader,
882 vbuffer = GST_VAAPI_VIDEO_BUFFER(buffer);
883 g_return_val_if_fail(vbuffer != NULL, GST_FLOW_UNEXPECTED);
885 if (sink->display != gst_vaapi_video_buffer_get_display (vbuffer)) {
886 g_clear_object(&sink->display);
887 sink->display = g_object_ref (gst_vaapi_video_buffer_get_display (vbuffer));
893 gst_vaapisink_ensure_rotation(sink, TRUE);
895 surface = gst_vaapi_video_buffer_get_surface(vbuffer);
899 GST_DEBUG("render surface %" GST_VAAPI_ID_FORMAT,
900 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
902 flags = gst_vaapi_video_buffer_get_render_flags(vbuffer);
904 composition = gst_video_buffer_get_overlay_composition(src_buffer);
905 if (!gst_vaapi_surface_set_subpictures_from_composition(surface,
907 GST_WARNING("could not update subtitles");
909 switch (sink->display_type) {
911 case GST_VAAPI_DISPLAY_TYPE_GLX:
912 success = gst_vaapisink_show_frame_glx(sink, surface, flags);
916 case GST_VAAPI_DISPLAY_TYPE_DRM:
921 case GST_VAAPI_DISPLAY_TYPE_X11:
922 success = gst_vaapisink_put_surface(sink, surface, flags);
926 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
927 success = gst_vaapisink_put_surface(sink, surface, flags);
931 GST_ERROR("unsupported display type %d", sink->display_type);
938 /* Retain VA surface until the next one is displayed */
939 if (sink->use_overlay)
940 gst_buffer_replace(&sink->video_buffer, buffer);
941 gst_buffer_unref(buffer);
945 gst_buffer_unref(buffer);
946 return GST_FLOW_UNEXPECTED;
950 gst_vaapisink_buffer_alloc(
951 GstBaseSink *base_sink,
958 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
959 GstStructure *structure;
964 structure = gst_caps_get_structure(caps, 0);
965 if (!gst_structure_has_name(structure, "video/x-raw-yuv"))
968 if (!gst_vaapi_uploader_ensure_display(sink->uploader, sink->display))
969 return GST_FLOW_NOT_SUPPORTED;
970 if (!gst_vaapi_uploader_ensure_caps(sink->uploader, caps, NULL))
971 return GST_FLOW_NOT_SUPPORTED;
973 buf = gst_vaapi_uploader_get_buffer(sink->uploader);
975 GST_WARNING("failed to allocate resources for raw YUV buffer");
976 return GST_FLOW_NOT_SUPPORTED;
984 gst_vaapisink_query(GstBaseSink *base_sink, GstQuery *query)
986 GstVaapiSink *sink = GST_VAAPISINK(base_sink);
987 GST_DEBUG ("sharing display %p", sink->display);
988 return gst_vaapi_reply_to_query (query, sink->display);
992 gst_vaapisink_finalize(GObject *object)
994 gst_vaapisink_destroy(GST_VAAPISINK(object));
996 G_OBJECT_CLASS(gst_vaapisink_parent_class)->finalize(object);
1000 gst_vaapisink_set_property(
1003 const GValue *value,
1007 GstVaapiSink * const sink = GST_VAAPISINK(object);
1010 case PROP_DISPLAY_TYPE:
1011 sink->display_type = g_value_get_enum(value);
1013 case PROP_FULLSCREEN:
1014 sink->fullscreen = g_value_get_boolean(value);
1016 case PROP_SYNCHRONOUS:
1017 sink->synchronous = g_value_get_boolean(value);
1019 case PROP_USE_REFLECTION:
1020 sink->use_reflection = g_value_get_boolean(value);
1023 sink->rotation_req = g_value_get_enum(value);
1026 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1032 gst_vaapisink_get_property(
1039 GstVaapiSink * const sink = GST_VAAPISINK(object);
1042 case PROP_DISPLAY_TYPE:
1043 g_value_set_enum(value, sink->display_type);
1045 case PROP_FULLSCREEN:
1046 g_value_set_boolean(value, sink->fullscreen);
1048 case PROP_SYNCHRONOUS:
1049 g_value_set_boolean(value, sink->synchronous);
1051 case PROP_USE_REFLECTION:
1052 g_value_set_boolean(value, sink->use_reflection);
1055 g_value_set_enum(value, sink->rotation);
1058 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1064 gst_vaapisink_class_init(GstVaapiSinkClass *klass)
1066 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
1067 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
1068 GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
1069 GstPadTemplate *pad_template;
1071 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
1072 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
1074 object_class->finalize = gst_vaapisink_finalize;
1075 object_class->set_property = gst_vaapisink_set_property;
1076 object_class->get_property = gst_vaapisink_get_property;
1078 basesink_class->start = gst_vaapisink_start;
1079 basesink_class->stop = gst_vaapisink_stop;
1080 basesink_class->get_caps = gst_vaapisink_get_caps;
1081 basesink_class->set_caps = gst_vaapisink_set_caps;
1082 basesink_class->preroll = gst_vaapisink_show_frame;
1083 basesink_class->render = gst_vaapisink_show_frame;
1084 basesink_class->query = gst_vaapisink_query;
1085 basesink_class->buffer_alloc = gst_vaapisink_buffer_alloc;
1087 gst_element_class_set_details_simple(
1089 gst_vaapisink_details.longname,
1090 gst_vaapisink_details.klass,
1091 gst_vaapisink_details.description,
1092 gst_vaapisink_details.author
1095 pad_template = gst_static_pad_template_get(&gst_vaapisink_sink_factory);
1096 gst_element_class_add_pad_template(element_class, pad_template);
1097 gst_object_unref(pad_template);
1099 g_object_class_install_property
1102 g_param_spec_enum("display",
1104 "display type to use",
1105 GST_VAAPI_TYPE_DISPLAY_TYPE,
1106 GST_VAAPI_DISPLAY_TYPE_ANY,
1107 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1110 g_object_class_install_property
1112 PROP_USE_REFLECTION,
1113 g_param_spec_boolean("use-reflection",
1114 "Reflection effect",
1115 "Enables OpenGL reflection effect",
1117 G_PARAM_READWRITE));
1120 g_object_class_install_property
1123 g_param_spec_boolean("fullscreen",
1125 "Requests window in fullscreen state",
1127 G_PARAM_READWRITE));
1130 * GstVaapiSink:synchronous:
1132 * When enabled, runs the X display in synchronous mode. Note that
1133 * this is used only for debugging.
1135 g_object_class_install_property
1138 g_param_spec_boolean("synchronous",
1140 "Toggles X display synchronous mode",
1142 G_PARAM_READWRITE));
1145 * GstVaapiSink:rotation:
1147 * The VA display rotation mode, expressed as a #GstVaapiRotation.
1149 g_object_class_install_property
1152 g_param_spec_enum(GST_VAAPI_DISPLAY_PROP_ROTATION,
1154 "The display rotation mode",
1155 GST_VAAPI_TYPE_ROTATION,
1157 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1161 gst_vaapisink_init(GstVaapiSink *sink)
1164 sink->display = NULL;
1165 sink->window = NULL;
1166 sink->window_width = 0;
1167 sink->window_height = 0;
1168 sink->texture = NULL;
1169 sink->video_buffer = NULL;
1170 sink->video_width = 0;
1171 sink->video_height = 0;
1172 sink->video_par_n = 1;
1173 sink->video_par_d = 1;
1174 sink->foreign_window = FALSE;
1175 sink->fullscreen = FALSE;
1176 sink->synchronous = FALSE;
1177 sink->display_type = DEFAULT_DISPLAY_TYPE;
1178 sink->rotation = DEFAULT_ROTATION;
1179 sink->rotation_req = DEFAULT_ROTATION;
1180 sink->use_reflection = FALSE;
1181 sink->use_overlay = FALSE;
1182 sink->use_rotation = FALSE;