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 #if GST_CHECK_VERSION(1,0,0)
55 # include <gst/video/videooverlay.h>
57 # include <gst/interfaces/xoverlay.h>
59 # define GST_TYPE_VIDEO_OVERLAY GST_TYPE_X_OVERLAY
60 # define GST_VIDEO_OVERLAY GST_X_OVERLAY
61 # define GstVideoOverlay GstXOverlay
62 # define GstVideoOverlayInterface GstXOverlayClass
64 # define gst_video_overlay_prepare_window_handle(sink) \
65 gst_x_overlay_prepare_xwindow_id(sink)
66 # define gst_video_overlay_got_window_handle(sink, window_handle) \
67 gst_x_overlay_got_window_handle(sink, window_handle)
70 #include "gstvaapisink.h"
71 #include "gstvaapipluginutil.h"
72 #include "gstvaapivideometa.h"
73 #if GST_CHECK_VERSION(1,0,0)
74 #include "gstvaapivideobufferpool.h"
75 #include "gstvaapivideomemory.h"
78 #define GST_PLUGIN_NAME "vaapisink"
79 #define GST_PLUGIN_DESC "A VA-API based videosink"
81 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapisink);
82 #define GST_CAT_DEFAULT gst_debug_vaapisink
84 /* Default template */
85 static const char gst_vaapisink_sink_caps_str[] =
86 #if GST_CHECK_VERSION(1,0,0)
87 GST_VIDEO_CAPS_MAKE(GST_VIDEO_FORMATS_ALL) "; "
90 "width = (int) [ 1, MAX ], "
91 "height = (int) [ 1, MAX ]; "
93 GST_VAAPI_SURFACE_CAPS;
95 static GstStaticPadTemplate gst_vaapisink_sink_factory =
96 GST_STATIC_PAD_TEMPLATE(
100 GST_STATIC_CAPS(gst_vaapisink_sink_caps_str));
102 /* GstImplementsInterface interface */
103 #if !GST_CHECK_VERSION(1,0,0)
105 gst_vaapisink_implements_interface_supported(
106 GstImplementsInterface *iface,
110 return (type == GST_TYPE_VIDEO_CONTEXT ||
111 type == GST_TYPE_VIDEO_OVERLAY);
115 gst_vaapisink_implements_iface_init(GstImplementsInterfaceClass *iface)
117 iface->supported = gst_vaapisink_implements_interface_supported;
121 /* GstVideoContext interface */
123 gst_vaapisink_set_video_context(GstVideoContext *context, const gchar *type,
126 GstVaapiSink *sink = GST_VAAPISINK (context);
127 gst_vaapi_set_display (type, value, &sink->display);
131 gst_vaapisink_video_context_iface_init(GstVideoContextInterface *iface)
133 iface->set_context = gst_vaapisink_set_video_context;
137 gst_vaapisink_video_overlay_iface_init(GstVideoOverlayInterface *iface);
139 G_DEFINE_TYPE_WITH_CODE(
143 #if !GST_CHECK_VERSION(1,0,0)
144 G_IMPLEMENT_INTERFACE(GST_TYPE_IMPLEMENTS_INTERFACE,
145 gst_vaapisink_implements_iface_init);
147 G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_CONTEXT,
148 gst_vaapisink_video_context_iface_init);
149 G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_OVERLAY,
150 gst_vaapisink_video_overlay_iface_init))
163 #define DEFAULT_DISPLAY_TYPE GST_VAAPI_DISPLAY_TYPE_ANY
164 #define DEFAULT_ROTATION GST_VAAPI_ROTATION_0
166 /* GstVideoOverlay interface */
170 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id);
174 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer);
177 gst_vaapisink_video_overlay_set_window_handle(GstVideoOverlay *overlay, guintptr window)
179 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
181 /* Disable GLX rendering when vaapisink is using a foreign X
182 window. It's pretty much useless */
183 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_GLX)
184 sink->display_type = GST_VAAPI_DISPLAY_TYPE_X11;
186 sink->foreign_window = TRUE;
188 switch (sink->display_type) {
190 case GST_VAAPI_DISPLAY_TYPE_X11:
191 gst_vaapisink_ensure_window_xid(sink, window);
200 gst_vaapisink_video_overlay_set_render_rectangle(
201 GstVideoOverlay *overlay,
208 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
209 GstVaapiRectangle * const display_rect = &sink->display_rect;
213 display_rect->width = width;
214 display_rect->height = height;
216 GST_DEBUG("render rect (%d,%d):%ux%u",
217 display_rect->x, display_rect->y,
218 display_rect->width, display_rect->height);
222 gst_vaapisink_video_overlay_expose(GstVideoOverlay *overlay)
224 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
225 GstBaseSink * const base_sink = GST_BASE_SINK(overlay);
228 if (sink->use_overlay)
229 buffer = sink->video_buffer ? gst_buffer_ref(sink->video_buffer) : NULL;
231 #if GST_CHECK_VERSION(1,0,0)
232 GstSample * const sample = gst_base_sink_get_last_sample(base_sink);
235 buffer = gst_buffer_ref(gst_sample_get_buffer(sample));
236 gst_sample_unref(sample);
238 buffer = gst_base_sink_get_last_buffer(base_sink);
242 gst_vaapisink_show_frame(base_sink, buffer);
243 gst_buffer_unref(buffer);
248 gst_vaapisink_video_overlay_iface_init(GstVideoOverlayInterface *iface)
250 iface->set_window_handle = gst_vaapisink_video_overlay_set_window_handle;
251 iface->set_render_rectangle = gst_vaapisink_video_overlay_set_render_rectangle;
252 iface->expose = gst_vaapisink_video_overlay_expose;
256 gst_vaapisink_destroy(GstVaapiSink *sink)
258 gst_buffer_replace(&sink->video_buffer, NULL);
260 gst_vaapi_texture_replace(&sink->texture, NULL);
262 gst_vaapi_display_replace(&sink->display, NULL);
263 g_clear_object(&sink->uploader);
265 gst_caps_replace(&sink->caps, NULL);
269 /* Checks whether a ConfigureNotify event is in the queue */
270 typedef struct _ConfigureNotifyEventPendingArgs ConfigureNotifyEventPendingArgs;
271 struct _ConfigureNotifyEventPendingArgs {
279 configure_notify_event_pending_cb(Display *dpy, XEvent *xev, XPointer arg)
281 ConfigureNotifyEventPendingArgs * const args =
282 (ConfigureNotifyEventPendingArgs *)arg;
284 if (xev->type == ConfigureNotify &&
285 xev->xconfigure.window == args->window &&
286 xev->xconfigure.width == args->width &&
287 xev->xconfigure.height == args->height)
290 /* XXX: this is a hack to traverse the whole queue because we
291 can't use XPeekIfEvent() since it could block */
296 configure_notify_event_pending(
303 ConfigureNotifyEventPendingArgs args;
306 args.window = window;
308 args.height = height;
311 /* XXX: don't use XPeekIfEvent() because it might block */
313 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
315 configure_notify_event_pending_cb, (XPointer)&args
322 get_display_type_name(GstVaapiDisplayType display_type)
324 gpointer const klass = g_type_class_peek(GST_VAAPI_TYPE_DISPLAY_TYPE);
325 GEnumValue * const e = g_enum_get_value(klass, display_type);
328 return e->value_name;
329 return "<unknown-type>";
332 static inline gboolean
333 gst_vaapisink_ensure_display(GstVaapiSink *sink)
335 GstVaapiDisplayType display_type;
336 GstVaapiRenderMode render_mode;
337 const gboolean had_display = sink->display != NULL;
339 if (!gst_vaapi_ensure_display(sink, sink->display_type, &sink->display))
342 display_type = gst_vaapi_display_get_display_type(sink->display);
343 if (display_type != sink->display_type || (!had_display && sink->display)) {
344 GST_INFO("created %s %p", get_display_type_name(display_type),
346 sink->display_type = display_type;
349 gst_vaapi_display_get_render_mode(sink->display, &render_mode) &&
350 render_mode == GST_VAAPI_RENDER_MODE_OVERLAY;
351 GST_DEBUG("use %s rendering mode", sink->use_overlay ? "overlay" : "texture");
353 sink->use_rotation = gst_vaapi_display_has_property(
354 sink->display, GST_VAAPI_DISPLAY_PROP_ROTATION);
360 gst_vaapisink_ensure_uploader(GstVaapiSink *sink)
362 if (!gst_vaapisink_ensure_display(sink))
365 if (!sink->uploader) {
366 sink->uploader = gst_vaapi_uploader_new(sink->display);
374 gst_vaapisink_ensure_render_rect(GstVaapiSink *sink, guint width, guint height)
376 GstVaapiRectangle * const display_rect = &sink->display_rect;
377 guint num, den, display_par_n, display_par_d;
380 /* Return success if caps are not set yet */
384 GST_DEBUG("ensure render rect within %ux%u bounds", width, height);
386 gst_vaapi_display_get_pixel_aspect_ratio(
388 &display_par_n, &display_par_d
390 GST_DEBUG("display pixel-aspect-ratio %d/%d",
391 display_par_n, display_par_d);
393 success = gst_video_calculate_display_ratio(
395 sink->video_width, sink->video_height,
396 sink->video_par_n, sink->video_par_d,
397 display_par_n, display_par_d
401 GST_DEBUG("video size %dx%d, calculated ratio %d/%d",
402 sink->video_width, sink->video_height, num, den);
404 display_rect->width = gst_util_uint64_scale_int(height, num, den);
405 if (display_rect->width <= width) {
406 GST_DEBUG("keeping window height");
407 display_rect->height = height;
410 GST_DEBUG("keeping window width");
411 display_rect->width = width;
412 display_rect->height =
413 gst_util_uint64_scale_int(width, den, num);
415 GST_DEBUG("scaling video to %ux%u", display_rect->width, display_rect->height);
417 g_assert(display_rect->width <= width);
418 g_assert(display_rect->height <= height);
420 display_rect->x = (width - display_rect->width) / 2;
421 display_rect->y = (height - display_rect->height) / 2;
423 GST_DEBUG("render rect (%d,%d):%ux%u",
424 display_rect->x, display_rect->y,
425 display_rect->width, display_rect->height);
430 gst_vaapisink_ensure_window_size(GstVaapiSink *sink, guint *pwidth, guint *pheight)
432 GstVideoRectangle src_rect, dst_rect, out_rect;
433 guint num, den, display_width, display_height, display_par_n, display_par_d;
434 gboolean success, scale;
436 if (sink->foreign_window) {
437 *pwidth = sink->window_width;
438 *pheight = sink->window_height;
442 gst_vaapi_display_get_size(sink->display, &display_width, &display_height);
443 if (sink->fullscreen) {
444 *pwidth = display_width;
445 *pheight = display_height;
449 gst_vaapi_display_get_pixel_aspect_ratio(
451 &display_par_n, &display_par_d
454 success = gst_video_calculate_display_ratio(
456 sink->video_width, sink->video_height,
457 sink->video_par_n, sink->video_par_d,
458 display_par_n, display_par_d
461 num = sink->video_par_n;
462 den = sink->video_par_d;
467 src_rect.w = gst_util_uint64_scale_int(sink->video_height, num, den);
468 src_rect.h = sink->video_height;
471 dst_rect.w = display_width;
472 dst_rect.h = display_height;
473 scale = (src_rect.w > dst_rect.w || src_rect.h > dst_rect.h);
474 gst_video_sink_center_rect(src_rect, dst_rect, &out_rect, scale);
475 *pwidth = out_rect.w;
476 *pheight = out_rect.h;
479 static inline gboolean
480 gst_vaapisink_ensure_window(GstVaapiSink *sink, guint width, guint height)
482 GstVaapiDisplay * const display = sink->display;
485 switch (sink->display_type) {
487 case GST_VAAPI_DISPLAY_TYPE_GLX:
488 sink->window = gst_vaapi_window_glx_new(display, width, height);
489 goto notify_video_overlay_interface;
492 case GST_VAAPI_DISPLAY_TYPE_X11:
493 sink->window = gst_vaapi_window_x11_new(display, width, height);
494 notify_video_overlay_interface:
497 gst_video_overlay_got_window_handle(
498 GST_VIDEO_OVERLAY(sink),
499 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window))
504 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
505 sink->window = gst_vaapi_window_wayland_new(display, width, height);
509 GST_ERROR("unsupported display type %d", sink->display_type);
513 return sink->window != NULL;
518 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id)
521 unsigned int width, height, border_width, depth;
525 if (!gst_vaapisink_ensure_display(sink))
528 gst_vaapi_display_lock(sink->display);
530 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
533 &x, &y, &width, &height, &border_width, &depth
535 gst_vaapi_display_unlock(sink->display);
537 if ((width != sink->window_width || height != sink->window_height) &&
538 !configure_notify_event_pending(sink, xid, width, height)) {
539 if (!gst_vaapisink_ensure_render_rect(sink, width, height))
541 sink->window_width = width;
542 sink->window_height = height;
546 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window)) == xid)
549 gst_vaapi_window_replace(&sink->window, NULL);
551 switch (sink->display_type) {
553 case GST_VAAPI_DISPLAY_TYPE_GLX:
554 sink->window = gst_vaapi_window_glx_new_with_xid(sink->display, xid);
557 case GST_VAAPI_DISPLAY_TYPE_X11:
558 sink->window = gst_vaapi_window_x11_new_with_xid(sink->display, xid);
561 GST_ERROR("unsupported display type %d", sink->display_type);
564 return sink->window != NULL;
569 gst_vaapisink_ensure_rotation(GstVaapiSink *sink, gboolean recalc_display_rect)
571 gboolean success = FALSE;
573 g_return_val_if_fail(sink->display, FALSE);
575 if (sink->rotation == sink->rotation_req)
578 if (!sink->use_rotation) {
579 GST_WARNING("VA display does not support rotation");
583 gst_vaapi_display_lock(sink->display);
584 success = gst_vaapi_display_set_rotation(sink->display, sink->rotation_req);
585 gst_vaapi_display_unlock(sink->display);
587 GST_ERROR("failed to change VA display rotation mode");
591 if (((sink->rotation + sink->rotation_req) % 180) == 90) {
592 /* Orientation changed */
593 G_PRIMITIVE_SWAP(guint, sink->video_width, sink->video_height);
594 G_PRIMITIVE_SWAP(gint, sink->video_par_n, sink->video_par_d);
597 if (recalc_display_rect && !sink->foreign_window)
598 gst_vaapisink_ensure_render_rect(sink, sink->window_width,
599 sink->window_height);
603 sink->rotation = sink->rotation_req;
608 gst_vaapisink_ensure_video_buffer_pool(GstVaapiSink *sink, GstCaps *caps)
610 #if GST_CHECK_VERSION(1,0,0)
613 GstStructure *config;
617 if (!gst_vaapisink_ensure_display(sink))
620 if (sink->video_buffer_pool) {
621 config = gst_buffer_pool_get_config(sink->video_buffer_pool);
622 gst_buffer_pool_config_get_params(config, &pool_caps, NULL, NULL, NULL);
623 need_pool = !gst_caps_is_equal(caps, pool_caps);
624 gst_structure_free(config);
627 g_clear_object(&sink->video_buffer_pool);
628 sink->video_buffer_size = 0;
631 pool = gst_vaapi_video_buffer_pool_new(sink->display);
633 goto error_create_pool;
635 gst_video_info_init(&vi);
636 gst_video_info_from_caps(&vi, caps);
637 if (GST_VIDEO_INFO_FORMAT(&vi) == GST_VIDEO_FORMAT_ENCODED) {
638 GST_DEBUG("assume video buffer pool format is NV12");
639 gst_video_info_set_format(&vi, GST_VIDEO_FORMAT_NV12,
640 GST_VIDEO_INFO_WIDTH(&vi), GST_VIDEO_INFO_HEIGHT(&vi));
642 sink->video_buffer_size = vi.size;
644 config = gst_buffer_pool_get_config(pool);
645 gst_buffer_pool_config_set_params(config, caps, sink->video_buffer_size,
647 gst_buffer_pool_config_add_option(config,
648 GST_BUFFER_POOL_OPTION_VAAPI_VIDEO_META);
649 if (!gst_buffer_pool_set_config(pool, config))
650 goto error_pool_config;
651 sink->video_buffer_pool = pool;
657 GST_ERROR("failed to create buffer pool");
662 GST_ERROR("failed to reset buffer pool config");
663 gst_object_unref(pool);
672 gst_vaapisink_start(GstBaseSink *base_sink)
674 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
676 if (!gst_vaapisink_ensure_display(sink))
679 sink->uploader = gst_vaapi_uploader_new(sink->display);
686 gst_vaapisink_stop(GstBaseSink *base_sink)
688 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
690 gst_buffer_replace(&sink->video_buffer, NULL);
691 #if GST_CHECK_VERSION(1,0,0)
692 g_clear_object(&sink->video_buffer_pool);
694 gst_vaapi_window_replace(&sink->window, NULL);
695 gst_vaapi_display_replace(&sink->display, NULL);
696 g_clear_object(&sink->uploader);
702 gst_vaapisink_get_caps_impl(GstBaseSink *base_sink)
704 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
705 GstCaps *out_caps, *yuv_caps;
707 out_caps = gst_caps_from_string(GST_VAAPI_SURFACE_CAPS);
711 if (gst_vaapisink_ensure_uploader(sink)) {
712 yuv_caps = gst_vaapi_uploader_get_caps(sink->uploader);
714 gst_caps_append(out_caps, gst_caps_copy(yuv_caps));
719 #if GST_CHECK_VERSION(1,0,0)
720 static inline GstCaps *
721 gst_vaapisink_get_caps(GstBaseSink *base_sink, GstCaps *filter)
723 return gst_vaapisink_get_caps_impl(base_sink);
726 #define gst_vaapisink_get_caps gst_vaapisink_get_caps_impl
730 gst_vaapisink_set_caps(GstBaseSink *base_sink, GstCaps *caps)
732 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
734 guint win_width, win_height;
737 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_DRM)
741 if (!gst_vaapisink_ensure_video_buffer_pool(sink, caps))
744 if (!gst_video_info_from_caps(&vi, caps))
746 sink->use_video_raw = GST_VIDEO_INFO_IS_YUV(&vi);
747 sink->video_width = GST_VIDEO_INFO_WIDTH(&vi);
748 sink->video_height = GST_VIDEO_INFO_HEIGHT(&vi);
749 sink->video_par_n = GST_VIDEO_INFO_PAR_N(&vi);
750 sink->video_par_d = GST_VIDEO_INFO_PAR_D(&vi);
751 GST_DEBUG("video pixel-aspect-ratio %d/%d",
752 sink->video_par_n, sink->video_par_d);
754 gst_caps_replace(&sink->caps, caps);
756 if (!gst_vaapisink_ensure_display(sink))
759 gst_vaapisink_ensure_rotation(sink, FALSE);
761 gst_vaapisink_ensure_window_size(sink, &win_width, &win_height);
763 if (!sink->foreign_window || sink->fullscreen)
764 gst_vaapi_window_set_size(sink->window, win_width, win_height);
767 gst_vaapi_display_lock(sink->display);
768 gst_video_overlay_prepare_window_handle(GST_VIDEO_OVERLAY(sink));
769 gst_vaapi_display_unlock(sink->display);
772 if (!gst_vaapisink_ensure_window(sink, win_width, win_height))
774 gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
775 gst_vaapi_window_show(sink->window);
776 gst_vaapi_window_get_size(sink->window, &win_width, &win_height);
778 sink->window_width = win_width;
779 sink->window_height = win_height;
780 GST_DEBUG("window size %ux%u", win_width, win_height);
782 return gst_vaapisink_ensure_render_rect(sink, win_width, win_height);
787 render_background(GstVaapiSink *sink)
789 /* Original code from Mirco Muller (MacSlow):
790 <http://cgit.freedesktop.org/~macslow/gl-gst-player/> */
791 GLfloat fStartX = 0.0f;
792 GLfloat fStartY = 0.0f;
793 GLfloat fWidth = (GLfloat)sink->window_width;
794 GLfloat fHeight = (GLfloat)sink->window_height;
796 glClear(GL_COLOR_BUFFER_BIT);
799 /* top third, darker grey to white */
800 glColor3f(0.85f, 0.85f, 0.85f);
801 glVertex3f(fStartX, fStartY, 0.0f);
802 glColor3f(0.85f, 0.85f, 0.85f);
803 glVertex3f(fStartX + fWidth, fStartY, 0.0f);
804 glColor3f(1.0f, 1.0f, 1.0f);
805 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
806 glColor3f(1.0f, 1.0f, 1.0f);
807 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
809 /* middle third, just plain white */
810 glColor3f(1.0f, 1.0f, 1.0f);
811 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
812 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
813 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
814 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
816 /* bottom third, white to lighter grey */
817 glColor3f(1.0f, 1.0f, 1.0f);
818 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
819 glColor3f(1.0f, 1.0f, 1.0f);
820 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
821 glColor3f(0.62f, 0.66f, 0.69f);
822 glVertex3f(fStartX + fWidth, fStartY + fHeight, 0.0f);
823 glColor3f(0.62f, 0.66f, 0.69f);
824 glVertex3f(fStartX, fStartY + fHeight, 0.0f);
830 render_frame(GstVaapiSink *sink)
832 const guint x1 = sink->display_rect.x;
833 const guint x2 = sink->display_rect.x + sink->display_rect.width;
834 const guint y1 = sink->display_rect.y;
835 const guint y2 = sink->display_rect.y + sink->display_rect.height;
837 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
840 glTexCoord2f(0.0f, 0.0f); glVertex2i(x1, y1);
841 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y2);
842 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y2);
843 glTexCoord2f(1.0f, 0.0f); glVertex2i(x2, y1);
849 render_reflection(GstVaapiSink *sink)
851 const guint x1 = sink->display_rect.x;
852 const guint x2 = sink->display_rect.x + sink->display_rect.width;
853 const guint y1 = sink->display_rect.y;
854 const guint rh = sink->display_rect.height / 5;
855 GLfloat ry = 1.0f - (GLfloat)rh / (GLfloat)sink->display_rect.height;
859 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
860 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y1);
861 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y1);
863 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
864 glTexCoord2f(1.0f, ry); glVertex2i(x2, y1 + rh);
865 glTexCoord2f(0.0f, ry); glVertex2i(x1, y1 + rh);
871 gst_vaapisink_show_frame_glx(
873 GstVaapiSurface *surface,
877 GstVaapiWindowGLX * const window = GST_VAAPI_WINDOW_GLX(sink->window);
881 gst_vaapi_window_glx_make_current(window);
882 if (!sink->texture) {
883 sink->texture = gst_vaapi_texture_new(
891 goto error_create_texture;
893 if (!gst_vaapi_texture_put_surface(sink->texture, surface, flags))
894 goto error_transfer_surface;
896 target = gst_vaapi_texture_get_target(sink->texture);
897 texture = gst_vaapi_texture_get_id(sink->texture);
898 if (target != GL_TEXTURE_2D || !texture)
901 if (sink->use_reflection)
902 render_background(sink);
905 glBindTexture(target, texture);
907 if (sink->use_reflection) {
909 glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
910 glTranslatef(50.0f, 0.0f, 0.0f);
913 if (sink->use_reflection) {
915 glTranslatef(0.0, (GLfloat)sink->display_rect.height + 5.0f, 0.0f);
916 render_reflection(sink);
921 glBindTexture(target, 0);
923 gst_vaapi_window_glx_swap_buffers(window);
927 error_create_texture:
929 GST_DEBUG("could not create VA/GLX texture");
932 error_transfer_surface:
934 GST_DEBUG("could not transfer VA surface to texture");
940 static inline gboolean
941 gst_vaapisink_put_surface(
943 GstVaapiSurface *surface,
947 if (!gst_vaapi_window_put_surface(sink->window, surface,
948 NULL, &sink->display_rect, flags)) {
949 GST_DEBUG("could not render VA surface");
956 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *src_buffer)
958 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
959 GstVaapiVideoMeta *meta;
960 GstVaapiSurface *surface;
965 meta = gst_buffer_get_vaapi_video_meta(src_buffer);
966 #if GST_CHECK_VERSION(1,0,0)
969 buffer = gst_buffer_ref(src_buffer);
972 buffer = gst_buffer_ref(src_buffer);
973 else if (sink->use_video_raw) {
974 buffer = gst_vaapi_uploader_get_buffer(sink->uploader);
977 meta = gst_buffer_get_vaapi_video_meta(buffer);
984 if (sink->use_video_raw &&
985 !gst_vaapi_uploader_process(sink->uploader, src_buffer, buffer)) {
986 GST_WARNING("failed to process raw YUV buffer");
991 if (sink->display != gst_vaapi_video_meta_get_display(meta))
992 gst_vaapi_display_replace(&sink->display,
993 gst_vaapi_video_meta_get_display(meta));
998 gst_vaapisink_ensure_rotation(sink, TRUE);
1000 surface = gst_vaapi_video_meta_get_surface(meta);
1004 GST_DEBUG("render surface %" GST_VAAPI_ID_FORMAT,
1005 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
1007 flags = gst_vaapi_video_meta_get_render_flags(meta);
1009 if (!gst_vaapi_apply_composition(surface, src_buffer))
1010 GST_WARNING("could not update subtitles");
1012 switch (sink->display_type) {
1014 case GST_VAAPI_DISPLAY_TYPE_DRM:
1019 case GST_VAAPI_DISPLAY_TYPE_GLX:
1021 goto put_surface_x11;
1022 success = gst_vaapisink_show_frame_glx(sink, surface, flags);
1026 case GST_VAAPI_DISPLAY_TYPE_X11:
1028 success = gst_vaapisink_put_surface(sink, surface, flags);
1032 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
1033 success = gst_vaapisink_put_surface(sink, surface, flags);
1037 GST_ERROR("unsupported display type %d", sink->display_type);
1044 /* Retain VA surface until the next one is displayed */
1045 if (sink->use_overlay)
1046 gst_buffer_replace(&sink->video_buffer, buffer);
1047 gst_buffer_unref(buffer);
1051 gst_buffer_unref(buffer);
1052 return GST_FLOW_EOS;
1055 #if GST_CHECK_VERSION(1,0,0)
1057 gst_vaapisink_propose_allocation(GstBaseSink *base_sink, GstQuery *query)
1059 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1060 GstCaps *caps = NULL;
1063 gst_query_parse_allocation(query, &caps, &need_pool);
1068 if (!gst_vaapisink_ensure_video_buffer_pool(sink, caps))
1070 gst_query_add_allocation_pool(query, sink->video_buffer_pool,
1071 sink->video_buffer_size, 0, 0);
1074 gst_query_add_allocation_meta(query,
1075 GST_VAAPI_VIDEO_META_API_TYPE, NULL);
1076 gst_query_add_allocation_meta(query,
1077 GST_VIDEO_META_API_TYPE, NULL);
1078 gst_query_add_allocation_meta(query,
1079 GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, NULL);
1085 GST_ERROR("no caps specified");
1090 static GstFlowReturn
1091 gst_vaapisink_buffer_alloc(
1092 GstBaseSink *base_sink,
1099 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1105 if (!sink->use_video_raw) {
1106 /* Note: this code path is rarely used but for raw YUV formats
1107 from custom pipeline. Otherwise, GstBaseSink::set_caps() is
1108 called first, and GstBaseSink::buffer_alloc() is not called
1109 in VA surface format mode */
1110 if (!gst_video_info_from_caps(&vi, caps))
1111 return GST_FLOW_NOT_SUPPORTED;
1112 if (!GST_VIDEO_INFO_IS_YUV(&vi))
1116 if (!gst_vaapi_uploader_ensure_display(sink->uploader, sink->display))
1117 return GST_FLOW_NOT_SUPPORTED;
1118 if (!gst_vaapi_uploader_ensure_caps(sink->uploader, caps, NULL))
1119 return GST_FLOW_NOT_SUPPORTED;
1121 buf = gst_vaapi_uploader_get_buffer(sink->uploader);
1123 GST_WARNING("failed to allocate resources for raw YUV buffer");
1124 return GST_FLOW_NOT_SUPPORTED;
1133 gst_vaapisink_query(GstBaseSink *base_sink, GstQuery *query)
1135 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
1137 if (gst_vaapi_reply_to_query(query, sink->display)) {
1138 GST_DEBUG("sharing display %p", sink->display);
1141 return GST_BASE_SINK_CLASS(gst_vaapisink_parent_class)->query(base_sink,
1146 gst_vaapisink_finalize(GObject *object)
1148 gst_vaapisink_destroy(GST_VAAPISINK(object));
1150 G_OBJECT_CLASS(gst_vaapisink_parent_class)->finalize(object);
1154 gst_vaapisink_set_property(
1157 const GValue *value,
1161 GstVaapiSink * const sink = GST_VAAPISINK(object);
1164 case PROP_DISPLAY_TYPE:
1165 sink->display_type = g_value_get_enum(value);
1167 case PROP_FULLSCREEN:
1168 sink->fullscreen = g_value_get_boolean(value);
1170 case PROP_SYNCHRONOUS:
1171 sink->synchronous = g_value_get_boolean(value);
1174 sink->use_glx = g_value_get_boolean(value);
1176 case PROP_USE_REFLECTION:
1177 sink->use_reflection = g_value_get_boolean(value);
1180 sink->rotation_req = g_value_get_enum(value);
1183 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1189 gst_vaapisink_get_property(
1196 GstVaapiSink * const sink = GST_VAAPISINK(object);
1199 case PROP_DISPLAY_TYPE:
1200 g_value_set_enum(value, sink->display_type);
1202 case PROP_FULLSCREEN:
1203 g_value_set_boolean(value, sink->fullscreen);
1205 case PROP_SYNCHRONOUS:
1206 g_value_set_boolean(value, sink->synchronous);
1209 g_value_set_boolean(value, sink->use_glx);
1211 case PROP_USE_REFLECTION:
1212 g_value_set_boolean(value, sink->use_reflection);
1215 g_value_set_enum(value, sink->rotation);
1218 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1224 gst_vaapisink_class_init(GstVaapiSinkClass *klass)
1226 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
1227 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
1228 GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
1229 GstPadTemplate *pad_template;
1231 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
1232 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
1234 object_class->finalize = gst_vaapisink_finalize;
1235 object_class->set_property = gst_vaapisink_set_property;
1236 object_class->get_property = gst_vaapisink_get_property;
1238 basesink_class->start = gst_vaapisink_start;
1239 basesink_class->stop = gst_vaapisink_stop;
1240 basesink_class->get_caps = gst_vaapisink_get_caps;
1241 basesink_class->set_caps = gst_vaapisink_set_caps;
1242 basesink_class->preroll = gst_vaapisink_show_frame;
1243 basesink_class->render = gst_vaapisink_show_frame;
1244 basesink_class->query = gst_vaapisink_query;
1245 #if GST_CHECK_VERSION(1,0,0)
1246 basesink_class->propose_allocation = gst_vaapisink_propose_allocation;
1248 basesink_class->buffer_alloc = gst_vaapisink_buffer_alloc;
1251 gst_element_class_set_static_metadata(element_class,
1255 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
1257 pad_template = gst_static_pad_template_get(&gst_vaapisink_sink_factory);
1258 gst_element_class_add_pad_template(element_class, pad_template);
1260 g_object_class_install_property
1263 g_param_spec_enum("display",
1265 "display type to use",
1266 GST_VAAPI_TYPE_DISPLAY_TYPE,
1267 GST_VAAPI_DISPLAY_TYPE_ANY,
1268 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1271 g_object_class_install_property
1274 g_param_spec_boolean("use-glx",
1276 "Enables OpenGL rendering",
1278 G_PARAM_READWRITE));
1280 g_object_class_install_property
1282 PROP_USE_REFLECTION,
1283 g_param_spec_boolean("use-reflection",
1284 "Reflection effect",
1285 "Enables OpenGL reflection effect",
1287 G_PARAM_READWRITE));
1290 g_object_class_install_property
1293 g_param_spec_boolean("fullscreen",
1295 "Requests window in fullscreen state",
1297 G_PARAM_READWRITE));
1300 * GstVaapiSink:synchronous:
1302 * When enabled, runs the X display in synchronous mode. Note that
1303 * this is used only for debugging.
1305 g_object_class_install_property
1308 g_param_spec_boolean("synchronous",
1310 "Toggles X display synchronous mode",
1312 G_PARAM_READWRITE));
1315 * GstVaapiSink:rotation:
1317 * The VA display rotation mode, expressed as a #GstVaapiRotation.
1319 g_object_class_install_property
1322 g_param_spec_enum(GST_VAAPI_DISPLAY_PROP_ROTATION,
1324 "The display rotation mode",
1325 GST_VAAPI_TYPE_ROTATION,
1327 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1331 gst_vaapisink_init(GstVaapiSink *sink)
1334 sink->display = NULL;
1335 sink->window = NULL;
1336 sink->window_width = 0;
1337 sink->window_height = 0;
1338 sink->texture = NULL;
1339 sink->video_buffer = NULL;
1340 sink->video_width = 0;
1341 sink->video_height = 0;
1342 sink->video_par_n = 1;
1343 sink->video_par_d = 1;
1344 sink->foreign_window = FALSE;
1345 sink->fullscreen = FALSE;
1346 sink->synchronous = FALSE;
1347 sink->display_type = DEFAULT_DISPLAY_TYPE;
1348 sink->rotation = DEFAULT_ROTATION;
1349 sink->rotation_req = DEFAULT_ROTATION;
1350 sink->use_reflection = FALSE;
1351 sink->use_overlay = FALSE;
1352 sink->use_rotation = FALSE;