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>
38 # include <gst/vaapi/gstvaapidisplay_drm.h>
41 # include <gst/vaapi/gstvaapidisplay_x11.h>
42 # include <gst/vaapi/gstvaapiwindow_x11.h>
45 # include <gst/vaapi/gstvaapidisplay_glx.h>
46 # include <gst/vaapi/gstvaapiwindow_glx.h>
49 # include <gst/vaapi/gstvaapidisplay_wayland.h>
50 # include <gst/vaapi/gstvaapiwindow_wayland.h>
53 /* Supported interfaces */
54 #include <gst/interfaces/xoverlay.h>
56 #include "gstvaapisink.h"
57 #include "gstvaapipluginutil.h"
58 #include "gstvaapivideometa.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);
614 guint win_width, win_height;
617 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_DRM)
621 if (!gst_video_info_from_caps(&vi, caps))
623 sink->use_video_raw = GST_VIDEO_INFO_IS_YUV(&vi);
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 gst_caps_replace(&sink->caps, caps);
633 if (!gst_vaapisink_ensure_display(sink))
636 gst_vaapisink_ensure_rotation(sink, FALSE);
638 gst_vaapisink_ensure_window_size(sink, &win_width, &win_height);
640 if (!sink->foreign_window || sink->fullscreen)
641 gst_vaapi_window_set_size(sink->window, win_width, win_height);
644 gst_vaapi_display_lock(sink->display);
645 gst_x_overlay_prepare_xwindow_id(GST_X_OVERLAY(sink));
646 gst_vaapi_display_unlock(sink->display);
649 if (!gst_vaapisink_ensure_window(sink, win_width, win_height))
651 gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
652 gst_vaapi_window_show(sink->window);
653 gst_vaapi_window_get_size(sink->window, &win_width, &win_height);
655 sink->window_width = win_width;
656 sink->window_height = win_height;
657 GST_DEBUG("window size %ux%u", win_width, win_height);
659 return gst_vaapisink_ensure_render_rect(sink, win_width, win_height);
664 render_background(GstVaapiSink *sink)
666 /* Original code from Mirco Muller (MacSlow):
667 <http://cgit.freedesktop.org/~macslow/gl-gst-player/> */
668 GLfloat fStartX = 0.0f;
669 GLfloat fStartY = 0.0f;
670 GLfloat fWidth = (GLfloat)sink->window_width;
671 GLfloat fHeight = (GLfloat)sink->window_height;
673 glClear(GL_COLOR_BUFFER_BIT);
676 /* top third, darker grey to white */
677 glColor3f(0.85f, 0.85f, 0.85f);
678 glVertex3f(fStartX, fStartY, 0.0f);
679 glColor3f(0.85f, 0.85f, 0.85f);
680 glVertex3f(fStartX + fWidth, fStartY, 0.0f);
681 glColor3f(1.0f, 1.0f, 1.0f);
682 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
683 glColor3f(1.0f, 1.0f, 1.0f);
684 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
686 /* middle third, just plain white */
687 glColor3f(1.0f, 1.0f, 1.0f);
688 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
689 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
690 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
691 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
693 /* bottom third, white to lighter grey */
694 glColor3f(1.0f, 1.0f, 1.0f);
695 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
696 glColor3f(1.0f, 1.0f, 1.0f);
697 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
698 glColor3f(0.62f, 0.66f, 0.69f);
699 glVertex3f(fStartX + fWidth, fStartY + fHeight, 0.0f);
700 glColor3f(0.62f, 0.66f, 0.69f);
701 glVertex3f(fStartX, fStartY + fHeight, 0.0f);
707 render_frame(GstVaapiSink *sink)
709 const guint x1 = sink->display_rect.x;
710 const guint x2 = sink->display_rect.x + sink->display_rect.width;
711 const guint y1 = sink->display_rect.y;
712 const guint y2 = sink->display_rect.y + sink->display_rect.height;
714 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
717 glTexCoord2f(0.0f, 0.0f); glVertex2i(x1, y1);
718 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y2);
719 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y2);
720 glTexCoord2f(1.0f, 0.0f); glVertex2i(x2, y1);
726 render_reflection(GstVaapiSink *sink)
728 const guint x1 = sink->display_rect.x;
729 const guint x2 = sink->display_rect.x + sink->display_rect.width;
730 const guint y1 = sink->display_rect.y;
731 const guint rh = sink->display_rect.height / 5;
732 GLfloat ry = 1.0f - (GLfloat)rh / (GLfloat)sink->display_rect.height;
736 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
737 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y1);
738 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y1);
740 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
741 glTexCoord2f(1.0f, ry); glVertex2i(x2, y1 + rh);
742 glTexCoord2f(0.0f, ry); glVertex2i(x1, y1 + rh);
748 gst_vaapisink_show_frame_glx(
750 GstVaapiSurface *surface,
754 GstVaapiWindowGLX * const window = GST_VAAPI_WINDOW_GLX(sink->window);
758 gst_vaapi_window_glx_make_current(window);
759 if (!sink->texture) {
760 sink->texture = gst_vaapi_texture_new(
768 goto error_create_texture;
770 if (!gst_vaapi_texture_put_surface(sink->texture, surface, flags))
771 goto error_transfer_surface;
773 target = gst_vaapi_texture_get_target(sink->texture);
774 texture = gst_vaapi_texture_get_id(sink->texture);
775 if (target != GL_TEXTURE_2D || !texture)
778 if (sink->use_reflection)
779 render_background(sink);
782 glBindTexture(target, texture);
784 if (sink->use_reflection) {
786 glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
787 glTranslatef(50.0f, 0.0f, 0.0f);
790 if (sink->use_reflection) {
792 glTranslatef(0.0, (GLfloat)sink->display_rect.height + 5.0f, 0.0f);
793 render_reflection(sink);
798 glBindTexture(target, 0);
800 gst_vaapi_window_glx_swap_buffers(window);
804 error_create_texture:
806 GST_DEBUG("could not create VA/GLX texture");
809 error_transfer_surface:
811 GST_DEBUG("could not transfer VA surface to texture");
817 static inline gboolean
818 gst_vaapisink_put_surface(
820 GstVaapiSurface *surface,
824 if (!gst_vaapi_window_put_surface(sink->window, surface,
825 NULL, &sink->display_rect, flags)) {
826 GST_DEBUG("could not render VA surface");
833 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *src_buffer)
835 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
836 GstVaapiVideoMeta *meta;
837 GstVaapiSurface *surface;
842 meta = gst_buffer_get_vaapi_video_meta(src_buffer);
844 buffer = gst_buffer_ref(src_buffer);
845 else if (sink->use_video_raw) {
846 buffer = gst_vaapi_uploader_get_buffer(sink->uploader);
848 return GST_FLOW_UNEXPECTED;
849 meta = gst_buffer_get_vaapi_video_meta(buffer);
854 return GST_FLOW_UNEXPECTED;
856 if (sink->use_video_raw &&
857 !gst_vaapi_uploader_process(sink->uploader, src_buffer, buffer)) {
858 GST_WARNING("failed to process raw YUV buffer");
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 if (!gst_vaapi_apply_composition(surface, src_buffer))
882 GST_WARNING("could not update subtitles");
884 switch (sink->display_type) {
886 case GST_VAAPI_DISPLAY_TYPE_GLX:
887 success = gst_vaapisink_show_frame_glx(sink, surface, flags);
891 case GST_VAAPI_DISPLAY_TYPE_DRM:
896 case GST_VAAPI_DISPLAY_TYPE_X11:
897 success = gst_vaapisink_put_surface(sink, surface, flags);
901 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
902 success = gst_vaapisink_put_surface(sink, surface, flags);
906 GST_ERROR("unsupported display type %d", sink->display_type);
913 /* Retain VA surface until the next one is displayed */
914 if (sink->use_overlay)
915 gst_buffer_replace(&sink->video_buffer, buffer);
916 gst_buffer_unref(buffer);
920 gst_buffer_unref(buffer);
921 return GST_FLOW_UNEXPECTED;
925 gst_vaapisink_buffer_alloc(
926 GstBaseSink *base_sink,
933 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
939 if (!sink->use_video_raw) {
940 /* Note: this code path is rarely used but for raw YUV formats
941 from custom pipeline. Otherwise, GstBaseSink::set_caps() is
942 called first, and GstBaseSink::buffer_alloc() is not called
943 in VA surface format mode */
944 if (!gst_video_info_from_caps(&vi, caps))
945 return GST_FLOW_NOT_SUPPORTED;
946 if (!GST_VIDEO_INFO_IS_YUV(&vi))
950 if (!gst_vaapi_uploader_ensure_display(sink->uploader, sink->display))
951 return GST_FLOW_NOT_SUPPORTED;
952 if (!gst_vaapi_uploader_ensure_caps(sink->uploader, caps, NULL))
953 return GST_FLOW_NOT_SUPPORTED;
955 buf = gst_vaapi_uploader_get_buffer(sink->uploader);
957 GST_WARNING("failed to allocate resources for raw YUV buffer");
958 return GST_FLOW_NOT_SUPPORTED;
966 gst_vaapisink_query(GstBaseSink *base_sink, GstQuery *query)
968 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
970 if (gst_vaapi_reply_to_query(query, sink->display)) {
971 GST_DEBUG("sharing display %p", sink->display);
974 return GST_BASE_SINK_CLASS(gst_vaapisink_parent_class)->query(base_sink,
979 gst_vaapisink_finalize(GObject *object)
981 gst_vaapisink_destroy(GST_VAAPISINK(object));
983 G_OBJECT_CLASS(gst_vaapisink_parent_class)->finalize(object);
987 gst_vaapisink_set_property(
994 GstVaapiSink * const sink = GST_VAAPISINK(object);
997 case PROP_DISPLAY_TYPE:
998 sink->display_type = g_value_get_enum(value);
1000 case PROP_FULLSCREEN:
1001 sink->fullscreen = g_value_get_boolean(value);
1003 case PROP_SYNCHRONOUS:
1004 sink->synchronous = g_value_get_boolean(value);
1006 case PROP_USE_REFLECTION:
1007 sink->use_reflection = g_value_get_boolean(value);
1010 sink->rotation_req = g_value_get_enum(value);
1013 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1019 gst_vaapisink_get_property(
1026 GstVaapiSink * const sink = GST_VAAPISINK(object);
1029 case PROP_DISPLAY_TYPE:
1030 g_value_set_enum(value, sink->display_type);
1032 case PROP_FULLSCREEN:
1033 g_value_set_boolean(value, sink->fullscreen);
1035 case PROP_SYNCHRONOUS:
1036 g_value_set_boolean(value, sink->synchronous);
1038 case PROP_USE_REFLECTION:
1039 g_value_set_boolean(value, sink->use_reflection);
1042 g_value_set_enum(value, sink->rotation);
1045 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1051 gst_vaapisink_class_init(GstVaapiSinkClass *klass)
1053 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
1054 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
1055 GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
1056 GstPadTemplate *pad_template;
1058 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
1059 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
1061 object_class->finalize = gst_vaapisink_finalize;
1062 object_class->set_property = gst_vaapisink_set_property;
1063 object_class->get_property = gst_vaapisink_get_property;
1065 basesink_class->start = gst_vaapisink_start;
1066 basesink_class->stop = gst_vaapisink_stop;
1067 basesink_class->get_caps = gst_vaapisink_get_caps;
1068 basesink_class->set_caps = gst_vaapisink_set_caps;
1069 basesink_class->preroll = gst_vaapisink_show_frame;
1070 basesink_class->render = gst_vaapisink_show_frame;
1071 basesink_class->query = gst_vaapisink_query;
1072 basesink_class->buffer_alloc = gst_vaapisink_buffer_alloc;
1074 gst_element_class_set_static_metadata(element_class,
1078 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
1080 pad_template = gst_static_pad_template_get(&gst_vaapisink_sink_factory);
1081 gst_element_class_add_pad_template(element_class, pad_template);
1082 gst_object_unref(pad_template);
1084 g_object_class_install_property
1087 g_param_spec_enum("display",
1089 "display type to use",
1090 GST_VAAPI_TYPE_DISPLAY_TYPE,
1091 GST_VAAPI_DISPLAY_TYPE_ANY,
1092 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1095 g_object_class_install_property
1097 PROP_USE_REFLECTION,
1098 g_param_spec_boolean("use-reflection",
1099 "Reflection effect",
1100 "Enables OpenGL reflection effect",
1102 G_PARAM_READWRITE));
1105 g_object_class_install_property
1108 g_param_spec_boolean("fullscreen",
1110 "Requests window in fullscreen state",
1112 G_PARAM_READWRITE));
1115 * GstVaapiSink:synchronous:
1117 * When enabled, runs the X display in synchronous mode. Note that
1118 * this is used only for debugging.
1120 g_object_class_install_property
1123 g_param_spec_boolean("synchronous",
1125 "Toggles X display synchronous mode",
1127 G_PARAM_READWRITE));
1130 * GstVaapiSink:rotation:
1132 * The VA display rotation mode, expressed as a #GstVaapiRotation.
1134 g_object_class_install_property
1137 g_param_spec_enum(GST_VAAPI_DISPLAY_PROP_ROTATION,
1139 "The display rotation mode",
1140 GST_VAAPI_TYPE_ROTATION,
1142 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1146 gst_vaapisink_init(GstVaapiSink *sink)
1149 sink->display = NULL;
1150 sink->window = NULL;
1151 sink->window_width = 0;
1152 sink->window_height = 0;
1153 sink->texture = NULL;
1154 sink->video_buffer = NULL;
1155 sink->video_width = 0;
1156 sink->video_height = 0;
1157 sink->video_par_n = 1;
1158 sink->video_par_d = 1;
1159 sink->foreign_window = FALSE;
1160 sink->fullscreen = FALSE;
1161 sink->synchronous = FALSE;
1162 sink->display_type = DEFAULT_DISPLAY_TYPE;
1163 sink->rotation = DEFAULT_ROTATION;
1164 sink->rotation_req = DEFAULT_ROTATION;
1165 sink->use_reflection = FALSE;
1166 sink->use_overlay = FALSE;
1167 sink->use_rotation = FALSE;