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 /* 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);
631 guint win_width, win_height;
634 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_DRM)
638 if (!gst_video_info_from_caps(&vi, caps))
640 sink->video_width = GST_VIDEO_INFO_WIDTH(&vi);
641 sink->video_height = GST_VIDEO_INFO_HEIGHT(&vi);
642 sink->video_par_n = GST_VIDEO_INFO_PAR_N(&vi);
643 sink->video_par_d = GST_VIDEO_INFO_PAR_D(&vi);
644 GST_DEBUG("video pixel-aspect-ratio %d/%d",
645 sink->video_par_n, sink->video_par_d);
647 if (gst_structure_has_name(structure, "video/x-raw-yuv"))
648 sink->use_video_raw = TRUE;
650 gst_caps_replace(&sink->caps, caps);
652 if (!gst_vaapisink_ensure_display(sink))
655 gst_vaapisink_ensure_rotation(sink, FALSE);
657 gst_vaapisink_ensure_window_size(sink, &win_width, &win_height);
659 if (!sink->foreign_window || sink->fullscreen)
660 gst_vaapi_window_set_size(sink->window, win_width, win_height);
663 gst_vaapi_display_lock(sink->display);
664 gst_x_overlay_prepare_xwindow_id(GST_X_OVERLAY(sink));
665 gst_vaapi_display_unlock(sink->display);
668 if (!gst_vaapisink_ensure_window(sink, win_width, win_height))
670 gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
671 gst_vaapi_window_show(sink->window);
672 gst_vaapi_window_get_size(sink->window, &win_width, &win_height);
674 sink->window_width = win_width;
675 sink->window_height = win_height;
676 GST_DEBUG("window size %ux%u", win_width, win_height);
678 return gst_vaapisink_ensure_render_rect(sink, win_width, win_height);
683 render_background(GstVaapiSink *sink)
685 /* Original code from Mirco Muller (MacSlow):
686 <http://cgit.freedesktop.org/~macslow/gl-gst-player/> */
687 GLfloat fStartX = 0.0f;
688 GLfloat fStartY = 0.0f;
689 GLfloat fWidth = (GLfloat)sink->window_width;
690 GLfloat fHeight = (GLfloat)sink->window_height;
692 glClear(GL_COLOR_BUFFER_BIT);
695 /* top third, darker grey to white */
696 glColor3f(0.85f, 0.85f, 0.85f);
697 glVertex3f(fStartX, fStartY, 0.0f);
698 glColor3f(0.85f, 0.85f, 0.85f);
699 glVertex3f(fStartX + fWidth, fStartY, 0.0f);
700 glColor3f(1.0f, 1.0f, 1.0f);
701 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
702 glColor3f(1.0f, 1.0f, 1.0f);
703 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
705 /* middle third, just plain white */
706 glColor3f(1.0f, 1.0f, 1.0f);
707 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
708 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
709 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
710 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
712 /* bottom third, white to lighter grey */
713 glColor3f(1.0f, 1.0f, 1.0f);
714 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
715 glColor3f(1.0f, 1.0f, 1.0f);
716 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
717 glColor3f(0.62f, 0.66f, 0.69f);
718 glVertex3f(fStartX + fWidth, fStartY + fHeight, 0.0f);
719 glColor3f(0.62f, 0.66f, 0.69f);
720 glVertex3f(fStartX, fStartY + fHeight, 0.0f);
726 render_frame(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 y2 = sink->display_rect.y + sink->display_rect.height;
733 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
736 glTexCoord2f(0.0f, 0.0f); glVertex2i(x1, y1);
737 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y2);
738 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y2);
739 glTexCoord2f(1.0f, 0.0f); glVertex2i(x2, y1);
745 render_reflection(GstVaapiSink *sink)
747 const guint x1 = sink->display_rect.x;
748 const guint x2 = sink->display_rect.x + sink->display_rect.width;
749 const guint y1 = sink->display_rect.y;
750 const guint rh = sink->display_rect.height / 5;
751 GLfloat ry = 1.0f - (GLfloat)rh / (GLfloat)sink->display_rect.height;
755 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
756 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y1);
757 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y1);
759 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
760 glTexCoord2f(1.0f, ry); glVertex2i(x2, y1 + rh);
761 glTexCoord2f(0.0f, ry); glVertex2i(x1, y1 + rh);
767 gst_vaapisink_show_frame_glx(
769 GstVaapiSurface *surface,
773 GstVaapiWindowGLX * const window = GST_VAAPI_WINDOW_GLX(sink->window);
777 gst_vaapi_window_glx_make_current(window);
778 if (!sink->texture) {
779 sink->texture = gst_vaapi_texture_new(
787 goto error_create_texture;
789 if (!gst_vaapi_texture_put_surface(sink->texture, surface, flags))
790 goto error_transfer_surface;
792 target = gst_vaapi_texture_get_target(sink->texture);
793 texture = gst_vaapi_texture_get_id(sink->texture);
794 if (target != GL_TEXTURE_2D || !texture)
797 if (sink->use_reflection)
798 render_background(sink);
801 glBindTexture(target, texture);
803 if (sink->use_reflection) {
805 glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
806 glTranslatef(50.0f, 0.0f, 0.0f);
809 if (sink->use_reflection) {
811 glTranslatef(0.0, (GLfloat)sink->display_rect.height + 5.0f, 0.0f);
812 render_reflection(sink);
817 glBindTexture(target, 0);
819 gst_vaapi_window_glx_swap_buffers(window);
823 error_create_texture:
825 GST_DEBUG("could not create VA/GLX texture");
828 error_transfer_surface:
830 GST_DEBUG("could not transfer VA surface to texture");
836 static inline gboolean
837 gst_vaapisink_put_surface(
839 GstVaapiSurface *surface,
843 if (!gst_vaapi_window_put_surface(sink->window, surface,
844 NULL, &sink->display_rect, flags)) {
845 GST_DEBUG("could not render VA surface");
852 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *src_buffer)
854 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
855 GstVideoOverlayComposition *composition;
856 GstVaapiVideoMeta *meta;
857 GstVaapiSurface *surface;
862 meta = gst_buffer_get_vaapi_video_meta(src_buffer);
864 buffer = gst_buffer_ref(src_buffer);
865 else if (sink->use_video_raw) {
866 buffer = gst_vaapi_uploader_get_buffer(sink->uploader);
868 return GST_FLOW_UNEXPECTED;
869 if (!gst_vaapi_uploader_process(sink->uploader, src_buffer, buffer))
871 meta = gst_buffer_get_vaapi_video_meta(buffer);
876 return GST_FLOW_UNEXPECTED;
878 if (sink->display != gst_vaapi_video_meta_get_display(meta)) {
879 g_clear_object(&sink->display);
880 sink->display = g_object_ref(gst_vaapi_video_meta_get_display(meta));
886 gst_vaapisink_ensure_rotation(sink, TRUE);
888 surface = gst_vaapi_video_meta_get_surface(meta);
892 GST_DEBUG("render surface %" GST_VAAPI_ID_FORMAT,
893 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
895 flags = gst_vaapi_video_meta_get_render_flags(meta);
897 composition = gst_video_buffer_get_overlay_composition(src_buffer);
898 if (!gst_vaapi_surface_set_subpictures_from_composition(surface,
900 GST_WARNING("could not update subtitles");
902 switch (sink->display_type) {
904 case GST_VAAPI_DISPLAY_TYPE_GLX:
905 success = gst_vaapisink_show_frame_glx(sink, surface, flags);
909 case GST_VAAPI_DISPLAY_TYPE_DRM:
914 case GST_VAAPI_DISPLAY_TYPE_X11:
915 success = gst_vaapisink_put_surface(sink, surface, flags);
919 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
920 success = gst_vaapisink_put_surface(sink, surface, flags);
924 GST_ERROR("unsupported display type %d", sink->display_type);
931 /* Retain VA surface until the next one is displayed */
932 if (sink->use_overlay)
933 gst_buffer_replace(&sink->video_buffer, buffer);
934 gst_buffer_unref(buffer);
938 gst_buffer_unref(buffer);
939 return GST_FLOW_UNEXPECTED;
943 gst_vaapisink_buffer_alloc(
944 GstBaseSink *base_sink,
951 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
952 GstStructure *structure;
957 structure = gst_caps_get_structure(caps, 0);
958 if (!gst_structure_has_name(structure, "video/x-raw-yuv"))
961 if (!gst_vaapi_uploader_ensure_display(sink->uploader, sink->display))
962 return GST_FLOW_NOT_SUPPORTED;
963 if (!gst_vaapi_uploader_ensure_caps(sink->uploader, caps, NULL))
964 return GST_FLOW_NOT_SUPPORTED;
966 buf = gst_vaapi_uploader_get_buffer(sink->uploader);
968 GST_WARNING("failed to allocate resources for raw YUV buffer");
969 return GST_FLOW_NOT_SUPPORTED;
977 gst_vaapisink_query(GstBaseSink *base_sink, GstQuery *query)
979 GstVaapiSink *sink = GST_VAAPISINK(base_sink);
980 GST_DEBUG ("sharing display %p", sink->display);
981 return gst_vaapi_reply_to_query (query, sink->display);
985 gst_vaapisink_finalize(GObject *object)
987 gst_vaapisink_destroy(GST_VAAPISINK(object));
989 G_OBJECT_CLASS(gst_vaapisink_parent_class)->finalize(object);
993 gst_vaapisink_set_property(
1000 GstVaapiSink * const sink = GST_VAAPISINK(object);
1003 case PROP_DISPLAY_TYPE:
1004 sink->display_type = g_value_get_enum(value);
1006 case PROP_FULLSCREEN:
1007 sink->fullscreen = g_value_get_boolean(value);
1009 case PROP_SYNCHRONOUS:
1010 sink->synchronous = g_value_get_boolean(value);
1012 case PROP_USE_REFLECTION:
1013 sink->use_reflection = g_value_get_boolean(value);
1016 sink->rotation_req = g_value_get_enum(value);
1019 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1025 gst_vaapisink_get_property(
1032 GstVaapiSink * const sink = GST_VAAPISINK(object);
1035 case PROP_DISPLAY_TYPE:
1036 g_value_set_enum(value, sink->display_type);
1038 case PROP_FULLSCREEN:
1039 g_value_set_boolean(value, sink->fullscreen);
1041 case PROP_SYNCHRONOUS:
1042 g_value_set_boolean(value, sink->synchronous);
1044 case PROP_USE_REFLECTION:
1045 g_value_set_boolean(value, sink->use_reflection);
1048 g_value_set_enum(value, sink->rotation);
1051 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1057 gst_vaapisink_class_init(GstVaapiSinkClass *klass)
1059 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
1060 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
1061 GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
1062 GstPadTemplate *pad_template;
1064 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
1065 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
1067 object_class->finalize = gst_vaapisink_finalize;
1068 object_class->set_property = gst_vaapisink_set_property;
1069 object_class->get_property = gst_vaapisink_get_property;
1071 basesink_class->start = gst_vaapisink_start;
1072 basesink_class->stop = gst_vaapisink_stop;
1073 basesink_class->get_caps = gst_vaapisink_get_caps;
1074 basesink_class->set_caps = gst_vaapisink_set_caps;
1075 basesink_class->preroll = gst_vaapisink_show_frame;
1076 basesink_class->render = gst_vaapisink_show_frame;
1077 basesink_class->query = gst_vaapisink_query;
1078 basesink_class->buffer_alloc = gst_vaapisink_buffer_alloc;
1080 gst_element_class_set_details_simple(
1082 gst_vaapisink_details.longname,
1083 gst_vaapisink_details.klass,
1084 gst_vaapisink_details.description,
1085 gst_vaapisink_details.author
1088 pad_template = gst_static_pad_template_get(&gst_vaapisink_sink_factory);
1089 gst_element_class_add_pad_template(element_class, pad_template);
1090 gst_object_unref(pad_template);
1092 g_object_class_install_property
1095 g_param_spec_enum("display",
1097 "display type to use",
1098 GST_VAAPI_TYPE_DISPLAY_TYPE,
1099 GST_VAAPI_DISPLAY_TYPE_ANY,
1100 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1103 g_object_class_install_property
1105 PROP_USE_REFLECTION,
1106 g_param_spec_boolean("use-reflection",
1107 "Reflection effect",
1108 "Enables OpenGL reflection effect",
1110 G_PARAM_READWRITE));
1113 g_object_class_install_property
1116 g_param_spec_boolean("fullscreen",
1118 "Requests window in fullscreen state",
1120 G_PARAM_READWRITE));
1123 * GstVaapiSink:synchronous:
1125 * When enabled, runs the X display in synchronous mode. Note that
1126 * this is used only for debugging.
1128 g_object_class_install_property
1131 g_param_spec_boolean("synchronous",
1133 "Toggles X display synchronous mode",
1135 G_PARAM_READWRITE));
1138 * GstVaapiSink:rotation:
1140 * The VA display rotation mode, expressed as a #GstVaapiRotation.
1142 g_object_class_install_property
1145 g_param_spec_enum(GST_VAAPI_DISPLAY_PROP_ROTATION,
1147 "The display rotation mode",
1148 GST_VAAPI_TYPE_ROTATION,
1150 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1154 gst_vaapisink_init(GstVaapiSink *sink)
1157 sink->display = NULL;
1158 sink->window = NULL;
1159 sink->window_width = 0;
1160 sink->window_height = 0;
1161 sink->texture = NULL;
1162 sink->video_buffer = NULL;
1163 sink->video_width = 0;
1164 sink->video_height = 0;
1165 sink->video_par_n = 1;
1166 sink->video_par_d = 1;
1167 sink->foreign_window = FALSE;
1168 sink->fullscreen = FALSE;
1169 sink->synchronous = FALSE;
1170 sink->display_type = DEFAULT_DISPLAY_TYPE;
1171 sink->rotation = DEFAULT_ROTATION;
1172 sink->rotation_req = DEFAULT_ROTATION;
1173 sink->use_reflection = FALSE;
1174 sink->use_overlay = FALSE;
1175 sink->use_rotation = FALSE;