2 * gstvaapisink.c - VA-API video sink
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Copyright (C) 2011-2013 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/gstvaapivideometa.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 /* Default template */
67 static const char gst_vaapisink_sink_caps_str[] =
69 "width = (int) [ 1, MAX ], "
70 "height = (int) [ 1, MAX ]; "
71 GST_VAAPI_SURFACE_CAPS;
73 static GstStaticPadTemplate gst_vaapisink_sink_factory =
74 GST_STATIC_PAD_TEMPLATE(
78 GST_STATIC_CAPS(gst_vaapisink_sink_caps_str));
80 /* GstImplementsInterface interface */
82 gst_vaapisink_implements_interface_supported(
83 GstImplementsInterface *iface,
87 return (type == GST_TYPE_VIDEO_CONTEXT ||
88 type == GST_TYPE_X_OVERLAY);
92 gst_vaapisink_implements_iface_init(GstImplementsInterfaceClass *iface)
94 iface->supported = gst_vaapisink_implements_interface_supported;
97 /* GstVideoContext interface */
99 gst_vaapisink_set_video_context(GstVideoContext *context, const gchar *type,
102 GstVaapiSink *sink = GST_VAAPISINK (context);
103 gst_vaapi_set_display (type, value, &sink->display);
107 gst_vaapisink_video_context_iface_init(GstVideoContextInterface *iface)
109 iface->set_context = gst_vaapisink_set_video_context;
113 gst_vaapisink_xoverlay_iface_init(GstXOverlayClass *iface);
115 G_DEFINE_TYPE_WITH_CODE(
119 G_IMPLEMENT_INTERFACE(GST_TYPE_IMPLEMENTS_INTERFACE,
120 gst_vaapisink_implements_iface_init);
121 G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_CONTEXT,
122 gst_vaapisink_video_context_iface_init);
123 G_IMPLEMENT_INTERFACE(GST_TYPE_X_OVERLAY,
124 gst_vaapisink_xoverlay_iface_init))
136 #define DEFAULT_DISPLAY_TYPE GST_VAAPI_DISPLAY_TYPE_ANY
137 #define DEFAULT_ROTATION GST_VAAPI_ROTATION_0
139 /* GstXOverlay interface */
143 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id);
147 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer);
150 gst_vaapisink_xoverlay_set_window_handle(GstXOverlay *overlay, guintptr window)
152 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
154 /* Disable GLX rendering when vaapisink is using a foreign X
155 window. It's pretty much useless */
156 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_GLX)
157 sink->display_type = GST_VAAPI_DISPLAY_TYPE_X11;
159 sink->foreign_window = TRUE;
161 switch (sink->display_type) {
163 case GST_VAAPI_DISPLAY_TYPE_X11:
164 gst_vaapisink_ensure_window_xid(sink, window);
173 gst_vaapisink_xoverlay_set_render_rectangle(
174 GstXOverlay *overlay,
181 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
182 GstVaapiRectangle * const display_rect = &sink->display_rect;
186 display_rect->width = width;
187 display_rect->height = height;
189 GST_DEBUG("render rect (%d,%d):%ux%u",
190 display_rect->x, display_rect->y,
191 display_rect->width, display_rect->height);
195 gst_vaapisink_xoverlay_expose(GstXOverlay *overlay)
197 GstBaseSink * const base_sink = GST_BASE_SINK(overlay);
200 buffer = gst_base_sink_get_last_buffer(base_sink);
202 gst_vaapisink_show_frame(base_sink, buffer);
203 gst_buffer_unref(buffer);
208 gst_vaapisink_xoverlay_iface_init(GstXOverlayClass *iface)
210 iface->set_window_handle = gst_vaapisink_xoverlay_set_window_handle;
211 iface->set_render_rectangle = gst_vaapisink_xoverlay_set_render_rectangle;
212 iface->expose = gst_vaapisink_xoverlay_expose;
216 gst_vaapisink_destroy(GstVaapiSink *sink)
218 gst_buffer_replace(&sink->video_buffer, NULL);
219 g_clear_object(&sink->texture);
220 g_clear_object(&sink->display);
221 g_clear_object(&sink->uploader);
223 gst_caps_replace(&sink->caps, NULL);
227 /* Checks whether a ConfigureNotify event is in the queue */
228 typedef struct _ConfigureNotifyEventPendingArgs ConfigureNotifyEventPendingArgs;
229 struct _ConfigureNotifyEventPendingArgs {
237 configure_notify_event_pending_cb(Display *dpy, XEvent *xev, XPointer arg)
239 ConfigureNotifyEventPendingArgs * const args =
240 (ConfigureNotifyEventPendingArgs *)arg;
242 if (xev->type == ConfigureNotify &&
243 xev->xconfigure.window == args->window &&
244 xev->xconfigure.width == args->width &&
245 xev->xconfigure.height == args->height)
248 /* XXX: this is a hack to traverse the whole queue because we
249 can't use XPeekIfEvent() since it could block */
254 configure_notify_event_pending(
261 ConfigureNotifyEventPendingArgs args;
264 args.window = window;
266 args.height = height;
269 /* XXX: don't use XPeekIfEvent() because it might block */
271 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
273 configure_notify_event_pending_cb, (XPointer)&args
280 get_display_type_name(GstVaapiDisplayType display_type)
282 gpointer const klass = g_type_class_peek(GST_VAAPI_TYPE_DISPLAY_TYPE);
283 GEnumValue * const e = g_enum_get_value(klass, display_type);
286 return e->value_name;
287 return "<unknown-type>";
290 static inline gboolean
291 gst_vaapisink_ensure_display(GstVaapiSink *sink)
293 GstVaapiDisplayType display_type;
294 GstVaapiRenderMode render_mode;
296 if (!gst_vaapi_ensure_display(sink, sink->display_type, &sink->display))
299 display_type = gst_vaapi_display_get_display_type(sink->display);
300 if (display_type != sink->display_type) {
301 GST_INFO("created %s %p", get_display_type_name(display_type),
303 sink->display_type = display_type;
306 gst_vaapi_display_get_render_mode(sink->display, &render_mode) &&
307 render_mode == GST_VAAPI_RENDER_MODE_OVERLAY;
308 GST_DEBUG("use %s rendering mode", sink->use_overlay ? "overlay" : "texture");
310 sink->use_rotation = gst_vaapi_display_has_property(
311 sink->display, GST_VAAPI_DISPLAY_PROP_ROTATION);
317 gst_vaapisink_ensure_uploader(GstVaapiSink *sink)
319 if (!gst_vaapisink_ensure_display(sink))
322 if (!sink->uploader) {
323 sink->uploader = gst_vaapi_uploader_new(sink->display);
331 gst_vaapisink_ensure_render_rect(GstVaapiSink *sink, guint width, guint height)
333 GstVaapiRectangle * const display_rect = &sink->display_rect;
334 guint num, den, display_par_n, display_par_d;
337 /* Return success if caps are not set yet */
341 GST_DEBUG("ensure render rect within %ux%u bounds", width, height);
343 gst_vaapi_display_get_pixel_aspect_ratio(
345 &display_par_n, &display_par_d
347 GST_DEBUG("display pixel-aspect-ratio %d/%d",
348 display_par_n, display_par_d);
350 success = gst_video_calculate_display_ratio(
352 sink->video_width, sink->video_height,
353 sink->video_par_n, sink->video_par_d,
354 display_par_n, display_par_d
358 GST_DEBUG("video size %dx%d, calculated ratio %d/%d",
359 sink->video_width, sink->video_height, num, den);
361 display_rect->width = gst_util_uint64_scale_int(height, num, den);
362 if (display_rect->width <= width) {
363 GST_DEBUG("keeping window height");
364 display_rect->height = height;
367 GST_DEBUG("keeping window width");
368 display_rect->width = width;
369 display_rect->height =
370 gst_util_uint64_scale_int(width, den, num);
372 GST_DEBUG("scaling video to %ux%u", display_rect->width, display_rect->height);
374 g_assert(display_rect->width <= width);
375 g_assert(display_rect->height <= height);
377 display_rect->x = (width - display_rect->width) / 2;
378 display_rect->y = (height - display_rect->height) / 2;
380 GST_DEBUG("render rect (%d,%d):%ux%u",
381 display_rect->x, display_rect->y,
382 display_rect->width, display_rect->height);
387 gst_vaapisink_ensure_window_size(GstVaapiSink *sink, guint *pwidth, guint *pheight)
389 GstVideoRectangle src_rect, dst_rect, out_rect;
390 guint num, den, display_width, display_height, display_par_n, display_par_d;
391 gboolean success, scale;
393 if (sink->foreign_window) {
394 *pwidth = sink->window_width;
395 *pheight = sink->window_height;
399 gst_vaapi_display_get_size(sink->display, &display_width, &display_height);
400 if (sink->fullscreen) {
401 *pwidth = display_width;
402 *pheight = display_height;
406 gst_vaapi_display_get_pixel_aspect_ratio(
408 &display_par_n, &display_par_d
411 success = gst_video_calculate_display_ratio(
413 sink->video_width, sink->video_height,
414 sink->video_par_n, sink->video_par_d,
415 display_par_n, display_par_d
418 num = sink->video_par_n;
419 den = sink->video_par_d;
424 src_rect.w = gst_util_uint64_scale_int(sink->video_height, num, den);
425 src_rect.h = sink->video_height;
428 dst_rect.w = display_width;
429 dst_rect.h = display_height;
430 scale = (src_rect.w > dst_rect.w || src_rect.h > dst_rect.h);
431 gst_video_sink_center_rect(src_rect, dst_rect, &out_rect, scale);
432 *pwidth = out_rect.w;
433 *pheight = out_rect.h;
436 static inline gboolean
437 gst_vaapisink_ensure_window(GstVaapiSink *sink, guint width, guint height)
439 GstVaapiDisplay * const display = sink->display;
442 switch (sink->display_type) {
444 case GST_VAAPI_DISPLAY_TYPE_GLX:
445 sink->window = gst_vaapi_window_glx_new(display, width, height);
446 goto notify_xoverlay_interface;
449 case GST_VAAPI_DISPLAY_TYPE_X11:
450 sink->window = gst_vaapi_window_x11_new(display, width, height);
451 notify_xoverlay_interface:
454 gst_x_overlay_got_window_handle(
456 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window))
461 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
462 sink->window = gst_vaapi_window_wayland_new(display, width, height);
466 GST_ERROR("unsupported display type %d", sink->display_type);
470 return sink->window != NULL;
475 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id)
478 unsigned int width, height, border_width, depth;
482 if (!gst_vaapisink_ensure_display(sink))
485 gst_vaapi_display_lock(sink->display);
487 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
490 &x, &y, &width, &height, &border_width, &depth
492 gst_vaapi_display_unlock(sink->display);
494 if ((width != sink->window_width || height != sink->window_height) &&
495 !configure_notify_event_pending(sink, xid, width, height)) {
496 if (!gst_vaapisink_ensure_render_rect(sink, width, height))
498 sink->window_width = width;
499 sink->window_height = height;
503 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window)) == xid)
506 g_clear_object(&sink->window);
508 switch (sink->display_type) {
510 case GST_VAAPI_DISPLAY_TYPE_GLX:
511 sink->window = gst_vaapi_window_glx_new_with_xid(sink->display, xid);
514 case GST_VAAPI_DISPLAY_TYPE_X11:
515 sink->window = gst_vaapi_window_x11_new_with_xid(sink->display, xid);
518 GST_ERROR("unsupported display type %d", sink->display_type);
521 return sink->window != NULL;
526 gst_vaapisink_ensure_rotation(GstVaapiSink *sink, gboolean recalc_display_rect)
528 gboolean success = FALSE;
530 g_return_val_if_fail(sink->display, FALSE);
532 if (sink->rotation == sink->rotation_req)
535 if (!sink->use_rotation) {
536 GST_WARNING("VA display does not support rotation");
540 gst_vaapi_display_lock(sink->display);
541 success = gst_vaapi_display_set_rotation(sink->display, sink->rotation_req);
542 gst_vaapi_display_unlock(sink->display);
544 GST_ERROR("failed to change VA display rotation mode");
548 if (((sink->rotation + sink->rotation_req) % 180) == 90) {
549 /* Orientation changed */
550 G_PRIMITIVE_SWAP(guint, sink->video_width, sink->video_height);
551 G_PRIMITIVE_SWAP(gint, sink->video_par_n, sink->video_par_d);
554 if (recalc_display_rect && !sink->foreign_window)
555 gst_vaapisink_ensure_render_rect(sink, sink->window_width,
556 sink->window_height);
560 sink->rotation = sink->rotation_req;
565 gst_vaapisink_start(GstBaseSink *base_sink)
567 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
569 if (!gst_vaapisink_ensure_display(sink))
572 sink->uploader = gst_vaapi_uploader_new(sink->display);
579 gst_vaapisink_stop(GstBaseSink *base_sink)
581 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
583 gst_buffer_replace(&sink->video_buffer, NULL);
584 g_clear_object(&sink->window);
585 g_clear_object(&sink->display);
586 g_clear_object(&sink->uploader);
592 gst_vaapisink_get_caps(GstBaseSink *base_sink)
594 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
595 GstCaps *out_caps, *yuv_caps;
597 out_caps = gst_caps_from_string(GST_VAAPI_SURFACE_CAPS);
601 if (gst_vaapisink_ensure_uploader(sink)) {
602 yuv_caps = gst_vaapi_uploader_get_caps(sink->uploader);
604 gst_caps_append(out_caps, gst_caps_copy(yuv_caps));
610 gst_vaapisink_set_caps(GstBaseSink *base_sink, GstCaps *caps)
612 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
613 GstStructure * const structure = gst_caps_get_structure(caps, 0);
615 guint win_width, win_height;
618 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_DRM)
622 if (!gst_video_info_from_caps(&vi, caps))
624 sink->video_width = GST_VIDEO_INFO_WIDTH(&vi);
625 sink->video_height = GST_VIDEO_INFO_HEIGHT(&vi);
626 sink->video_par_n = GST_VIDEO_INFO_PAR_N(&vi);
627 sink->video_par_d = GST_VIDEO_INFO_PAR_D(&vi);
628 GST_DEBUG("video pixel-aspect-ratio %d/%d",
629 sink->video_par_n, sink->video_par_d);
631 if (gst_structure_has_name(structure, "video/x-raw-yuv"))
632 sink->use_video_raw = TRUE;
634 gst_caps_replace(&sink->caps, caps);
636 if (!gst_vaapisink_ensure_display(sink))
639 gst_vaapisink_ensure_rotation(sink, FALSE);
641 gst_vaapisink_ensure_window_size(sink, &win_width, &win_height);
643 if (!sink->foreign_window || sink->fullscreen)
644 gst_vaapi_window_set_size(sink->window, win_width, win_height);
647 gst_vaapi_display_lock(sink->display);
648 gst_x_overlay_prepare_xwindow_id(GST_X_OVERLAY(sink));
649 gst_vaapi_display_unlock(sink->display);
652 if (!gst_vaapisink_ensure_window(sink, win_width, win_height))
654 gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
655 gst_vaapi_window_show(sink->window);
656 gst_vaapi_window_get_size(sink->window, &win_width, &win_height);
658 sink->window_width = win_width;
659 sink->window_height = win_height;
660 GST_DEBUG("window size %ux%u", win_width, win_height);
662 return gst_vaapisink_ensure_render_rect(sink, win_width, win_height);
667 render_background(GstVaapiSink *sink)
669 /* Original code from Mirco Muller (MacSlow):
670 <http://cgit.freedesktop.org/~macslow/gl-gst-player/> */
671 GLfloat fStartX = 0.0f;
672 GLfloat fStartY = 0.0f;
673 GLfloat fWidth = (GLfloat)sink->window_width;
674 GLfloat fHeight = (GLfloat)sink->window_height;
676 glClear(GL_COLOR_BUFFER_BIT);
679 /* top third, darker grey to white */
680 glColor3f(0.85f, 0.85f, 0.85f);
681 glVertex3f(fStartX, fStartY, 0.0f);
682 glColor3f(0.85f, 0.85f, 0.85f);
683 glVertex3f(fStartX + fWidth, fStartY, 0.0f);
684 glColor3f(1.0f, 1.0f, 1.0f);
685 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
686 glColor3f(1.0f, 1.0f, 1.0f);
687 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
689 /* middle third, just plain white */
690 glColor3f(1.0f, 1.0f, 1.0f);
691 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
692 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
693 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
694 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
696 /* bottom third, white to lighter grey */
697 glColor3f(1.0f, 1.0f, 1.0f);
698 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
699 glColor3f(1.0f, 1.0f, 1.0f);
700 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
701 glColor3f(0.62f, 0.66f, 0.69f);
702 glVertex3f(fStartX + fWidth, fStartY + fHeight, 0.0f);
703 glColor3f(0.62f, 0.66f, 0.69f);
704 glVertex3f(fStartX, fStartY + fHeight, 0.0f);
710 render_frame(GstVaapiSink *sink)
712 const guint x1 = sink->display_rect.x;
713 const guint x2 = sink->display_rect.x + sink->display_rect.width;
714 const guint y1 = sink->display_rect.y;
715 const guint y2 = sink->display_rect.y + sink->display_rect.height;
717 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
720 glTexCoord2f(0.0f, 0.0f); glVertex2i(x1, y1);
721 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y2);
722 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y2);
723 glTexCoord2f(1.0f, 0.0f); glVertex2i(x2, y1);
729 render_reflection(GstVaapiSink *sink)
731 const guint x1 = sink->display_rect.x;
732 const guint x2 = sink->display_rect.x + sink->display_rect.width;
733 const guint y1 = sink->display_rect.y;
734 const guint rh = sink->display_rect.height / 5;
735 GLfloat ry = 1.0f - (GLfloat)rh / (GLfloat)sink->display_rect.height;
739 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
740 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y1);
741 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y1);
743 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
744 glTexCoord2f(1.0f, ry); glVertex2i(x2, y1 + rh);
745 glTexCoord2f(0.0f, ry); glVertex2i(x1, y1 + rh);
751 gst_vaapisink_show_frame_glx(
753 GstVaapiSurface *surface,
757 GstVaapiWindowGLX * const window = GST_VAAPI_WINDOW_GLX(sink->window);
761 gst_vaapi_window_glx_make_current(window);
762 if (!sink->texture) {
763 sink->texture = gst_vaapi_texture_new(
771 goto error_create_texture;
773 if (!gst_vaapi_texture_put_surface(sink->texture, surface, flags))
774 goto error_transfer_surface;
776 target = gst_vaapi_texture_get_target(sink->texture);
777 texture = gst_vaapi_texture_get_id(sink->texture);
778 if (target != GL_TEXTURE_2D || !texture)
781 if (sink->use_reflection)
782 render_background(sink);
785 glBindTexture(target, texture);
787 if (sink->use_reflection) {
789 glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
790 glTranslatef(50.0f, 0.0f, 0.0f);
793 if (sink->use_reflection) {
795 glTranslatef(0.0, (GLfloat)sink->display_rect.height + 5.0f, 0.0f);
796 render_reflection(sink);
801 glBindTexture(target, 0);
803 gst_vaapi_window_glx_swap_buffers(window);
807 error_create_texture:
809 GST_DEBUG("could not create VA/GLX texture");
812 error_transfer_surface:
814 GST_DEBUG("could not transfer VA surface to texture");
820 static inline gboolean
821 gst_vaapisink_put_surface(
823 GstVaapiSurface *surface,
827 if (!gst_vaapi_window_put_surface(sink->window, surface,
828 NULL, &sink->display_rect, flags)) {
829 GST_DEBUG("could not render VA surface");
836 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *src_buffer)
838 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
839 GstVideoOverlayComposition *composition;
840 GstVaapiVideoMeta *meta;
841 GstVaapiSurface *surface;
846 meta = gst_buffer_get_vaapi_video_meta(src_buffer);
848 buffer = gst_buffer_ref(src_buffer);
849 else if (sink->use_video_raw) {
850 buffer = gst_vaapi_uploader_get_buffer(sink->uploader);
852 return GST_FLOW_UNEXPECTED;
853 if (!gst_vaapi_uploader_process(sink->uploader, src_buffer, buffer))
855 meta = gst_buffer_get_vaapi_video_meta(buffer);
860 return GST_FLOW_UNEXPECTED;
862 if (sink->display != gst_vaapi_video_meta_get_display(meta)) {
863 g_clear_object(&sink->display);
864 sink->display = g_object_ref(gst_vaapi_video_meta_get_display(meta));
870 gst_vaapisink_ensure_rotation(sink, TRUE);
872 surface = gst_vaapi_video_meta_get_surface(meta);
876 GST_DEBUG("render surface %" GST_VAAPI_ID_FORMAT,
877 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
879 flags = gst_vaapi_video_meta_get_render_flags(meta);
881 composition = gst_video_buffer_get_overlay_composition(src_buffer);
882 if (!gst_vaapi_surface_set_subpictures_from_composition(surface,
884 GST_WARNING("could not update subtitles");
886 switch (sink->display_type) {
888 case GST_VAAPI_DISPLAY_TYPE_GLX:
889 success = gst_vaapisink_show_frame_glx(sink, surface, flags);
893 case GST_VAAPI_DISPLAY_TYPE_DRM:
898 case GST_VAAPI_DISPLAY_TYPE_X11:
899 success = gst_vaapisink_put_surface(sink, surface, flags);
903 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
904 success = gst_vaapisink_put_surface(sink, surface, flags);
908 GST_ERROR("unsupported display type %d", sink->display_type);
915 /* Retain VA surface until the next one is displayed */
916 if (sink->use_overlay)
917 gst_buffer_replace(&sink->video_buffer, buffer);
918 gst_buffer_unref(buffer);
922 gst_buffer_unref(buffer);
923 return GST_FLOW_UNEXPECTED;
927 gst_vaapisink_buffer_alloc(
928 GstBaseSink *base_sink,
935 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
936 GstStructure *structure;
941 structure = gst_caps_get_structure(caps, 0);
942 if (!gst_structure_has_name(structure, "video/x-raw-yuv"))
945 if (!gst_vaapi_uploader_ensure_display(sink->uploader, sink->display))
946 return GST_FLOW_NOT_SUPPORTED;
947 if (!gst_vaapi_uploader_ensure_caps(sink->uploader, caps, NULL))
948 return GST_FLOW_NOT_SUPPORTED;
950 buf = gst_vaapi_uploader_get_buffer(sink->uploader);
952 GST_WARNING("failed to allocate resources for raw YUV buffer");
953 return GST_FLOW_NOT_SUPPORTED;
961 gst_vaapisink_query(GstBaseSink *base_sink, GstQuery *query)
963 GstVaapiSink *sink = GST_VAAPISINK(base_sink);
964 GST_DEBUG ("sharing display %p", sink->display);
965 return gst_vaapi_reply_to_query (query, sink->display);
969 gst_vaapisink_finalize(GObject *object)
971 gst_vaapisink_destroy(GST_VAAPISINK(object));
973 G_OBJECT_CLASS(gst_vaapisink_parent_class)->finalize(object);
977 gst_vaapisink_set_property(
984 GstVaapiSink * const sink = GST_VAAPISINK(object);
987 case PROP_DISPLAY_TYPE:
988 sink->display_type = g_value_get_enum(value);
990 case PROP_FULLSCREEN:
991 sink->fullscreen = g_value_get_boolean(value);
993 case PROP_SYNCHRONOUS:
994 sink->synchronous = g_value_get_boolean(value);
996 case PROP_USE_REFLECTION:
997 sink->use_reflection = g_value_get_boolean(value);
1000 sink->rotation_req = g_value_get_enum(value);
1003 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1009 gst_vaapisink_get_property(
1016 GstVaapiSink * const sink = GST_VAAPISINK(object);
1019 case PROP_DISPLAY_TYPE:
1020 g_value_set_enum(value, sink->display_type);
1022 case PROP_FULLSCREEN:
1023 g_value_set_boolean(value, sink->fullscreen);
1025 case PROP_SYNCHRONOUS:
1026 g_value_set_boolean(value, sink->synchronous);
1028 case PROP_USE_REFLECTION:
1029 g_value_set_boolean(value, sink->use_reflection);
1032 g_value_set_enum(value, sink->rotation);
1035 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1041 gst_vaapisink_class_init(GstVaapiSinkClass *klass)
1043 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
1044 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
1045 GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
1046 GstPadTemplate *pad_template;
1048 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
1049 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
1051 object_class->finalize = gst_vaapisink_finalize;
1052 object_class->set_property = gst_vaapisink_set_property;
1053 object_class->get_property = gst_vaapisink_get_property;
1055 basesink_class->start = gst_vaapisink_start;
1056 basesink_class->stop = gst_vaapisink_stop;
1057 basesink_class->get_caps = gst_vaapisink_get_caps;
1058 basesink_class->set_caps = gst_vaapisink_set_caps;
1059 basesink_class->preroll = gst_vaapisink_show_frame;
1060 basesink_class->render = gst_vaapisink_show_frame;
1061 basesink_class->query = gst_vaapisink_query;
1062 basesink_class->buffer_alloc = gst_vaapisink_buffer_alloc;
1064 gst_element_class_set_static_metadata(element_class,
1068 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
1070 pad_template = gst_static_pad_template_get(&gst_vaapisink_sink_factory);
1071 gst_element_class_add_pad_template(element_class, pad_template);
1072 gst_object_unref(pad_template);
1074 g_object_class_install_property
1077 g_param_spec_enum("display",
1079 "display type to use",
1080 GST_VAAPI_TYPE_DISPLAY_TYPE,
1081 GST_VAAPI_DISPLAY_TYPE_ANY,
1082 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1085 g_object_class_install_property
1087 PROP_USE_REFLECTION,
1088 g_param_spec_boolean("use-reflection",
1089 "Reflection effect",
1090 "Enables OpenGL reflection effect",
1092 G_PARAM_READWRITE));
1095 g_object_class_install_property
1098 g_param_spec_boolean("fullscreen",
1100 "Requests window in fullscreen state",
1102 G_PARAM_READWRITE));
1105 * GstVaapiSink:synchronous:
1107 * When enabled, runs the X display in synchronous mode. Note that
1108 * this is used only for debugging.
1110 g_object_class_install_property
1113 g_param_spec_boolean("synchronous",
1115 "Toggles X display synchronous mode",
1117 G_PARAM_READWRITE));
1120 * GstVaapiSink:rotation:
1122 * The VA display rotation mode, expressed as a #GstVaapiRotation.
1124 g_object_class_install_property
1127 g_param_spec_enum(GST_VAAPI_DISPLAY_PROP_ROTATION,
1129 "The display rotation mode",
1130 GST_VAAPI_TYPE_ROTATION,
1132 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1136 gst_vaapisink_init(GstVaapiSink *sink)
1139 sink->display = NULL;
1140 sink->window = NULL;
1141 sink->window_width = 0;
1142 sink->window_height = 0;
1143 sink->texture = NULL;
1144 sink->video_buffer = NULL;
1145 sink->video_width = 0;
1146 sink->video_height = 0;
1147 sink->video_par_n = 1;
1148 sink->video_par_d = 1;
1149 sink->foreign_window = FALSE;
1150 sink->fullscreen = FALSE;
1151 sink->synchronous = FALSE;
1152 sink->display_type = DEFAULT_DISPLAY_TYPE;
1153 sink->rotation = DEFAULT_ROTATION;
1154 sink->rotation_req = DEFAULT_ROTATION;
1155 sink->use_reflection = FALSE;
1156 sink->use_overlay = FALSE;
1157 sink->use_rotation = FALSE;