2 * gstvaapisink.c - VA-API video sink
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Copyright (C) 2011-2012 Intel Corporation
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1
10 * of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA
24 * SECTION:gstvaapisink
25 * @short_description: A VA-API based videosink
27 * vaapisink renders video frames to a drawable (X #Window) on a local
28 * display using the Video Acceleration (VA) API. The element will
29 * create its own internal window and render into it.
34 #include <gst/video/video.h>
35 #include <gst/video/videocontext.h>
36 #include <gst/vaapi/gstvaapivalue.h>
37 #include <gst/vaapi/gstvaapivideobuffer.h>
38 #include <gst/vaapi/gstvaapipluginbuffer.h>
39 #include <gst/vaapi/gstvaapidebug.h>
42 # include <gst/vaapi/gstvaapidisplay_drm.h>
45 # include <gst/vaapi/gstvaapidisplay_x11.h>
46 # include <gst/vaapi/gstvaapiwindow_x11.h>
49 # include <gst/vaapi/gstvaapidisplay_glx.h>
50 # include <gst/vaapi/gstvaapiwindow_glx.h>
53 # include <gst/vaapi/gstvaapidisplay_wayland.h>
54 # include <gst/vaapi/gstvaapiwindow_wayland.h>
57 /* Supported interfaces */
58 #include <gst/interfaces/xoverlay.h>
60 #include "gstvaapisink.h"
61 #include "gstvaapipluginutil.h"
63 #define GST_PLUGIN_NAME "vaapisink"
64 #define GST_PLUGIN_DESC "A VA-API based videosink"
66 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapisink);
67 #define GST_CAT_DEFAULT gst_debug_vaapisink
69 /* ElementFactory information */
70 static const GstElementDetails gst_vaapisink_details =
75 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
77 static const char gst_vaapisink_sink_caps_str[] =
78 GST_VAAPI_SURFACE_CAPS "; "
79 GST_VAAPI_BUFFER_SHARING_CAPS;
81 /* Default template */
82 static GstStaticPadTemplate gst_vaapisink_sink_factory =
83 GST_STATIC_PAD_TEMPLATE(
87 GST_STATIC_CAPS(gst_vaapisink_sink_caps_str));
90 gst_vaapisink_implements_iface_init(GstImplementsInterfaceClass *iface);
93 gst_vaapisink_video_context_iface_init(GstVideoContextInterface *iface);
96 gst_vaapisink_xoverlay_iface_init(GstXOverlayClass *iface);
98 G_DEFINE_TYPE_WITH_CODE(
102 G_IMPLEMENT_INTERFACE(GST_TYPE_IMPLEMENTS_INTERFACE,
103 gst_vaapisink_implements_iface_init);
104 G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_CONTEXT,
105 gst_vaapisink_video_context_iface_init);
106 G_IMPLEMENT_INTERFACE(GST_TYPE_X_OVERLAY,
107 gst_vaapisink_xoverlay_iface_init));
120 #define DEFAULT_DISPLAY_TYPE GST_VAAPI_DISPLAY_TYPE_ANY
121 #define DEFAULT_ROTATION GST_VAAPI_ROTATION_0
123 /* GstImplementsInterface interface */
125 * gst_vaapisink_prepare_xid:
126 * @overlay: a #GstXOverlay which does not yet have an XWindow or XPixmap.
128 * This will post a "prepare-xid" element message with video size and display size on the bus
129 * to give applications an opportunity to call
130 * gst_x_overlay_set_xwindow_id() before a plugin creates its own
133 * This function should only be used by video overlay plugin developers.
136 gst_vaapisink_prepare_xid (GstXOverlay * overlay)
141 g_return_if_fail (overlay != NULL);
142 g_return_if_fail (GST_IS_X_OVERLAY (overlay));
145 sink = GST_VAAPISINK (GST_OBJECT (overlay));
147 GST_DEBUG ("post \"prepare-xid\" element message with video-width(%d), video-height(%d), display-width(%d), display-height(%d)",
148 sink->video_width, sink->video_height, sink->window_width, sink->window_height);
150 GST_LOG_OBJECT (GST_OBJECT (overlay), "prepare xid");
151 s = gst_structure_new ("prepare-xid",
152 "video-width", G_TYPE_INT, sink->video_width,
153 "video-height", G_TYPE_INT, sink->video_height,
154 "display-width", G_TYPE_INT, sink->window_width,
155 "display-height", G_TYPE_INT, sink->window_height,
157 msg = gst_message_new_element (GST_OBJECT (overlay), s);
158 gst_element_post_message (GST_ELEMENT (overlay), msg);
162 gst_vaapisink_implements_interface_supported(
163 GstImplementsInterface *iface,
167 return (type == GST_TYPE_VIDEO_CONTEXT ||
168 type == GST_TYPE_X_OVERLAY);
172 gst_vaapisink_implements_iface_init(GstImplementsInterfaceClass *iface)
174 iface->supported = gst_vaapisink_implements_interface_supported;
177 /* GstVideoContext interface */
180 gst_vaapisink_set_video_context(GstVideoContext *context, const gchar *type,
183 GstVaapiSink *sink = GST_VAAPISINK (context);
184 gst_vaapi_set_display (type, value, &sink->display);
188 gst_vaapisink_video_context_iface_init(GstVideoContextInterface *iface)
190 iface->set_context = gst_vaapisink_set_video_context;
193 /* GstXOverlay interface */
197 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id);
201 gst_vaapisink_ensure_display_rect(GstVaapiSink *sink, guint width, guint height);
204 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer);
207 gst_vaapisink_xoverlay_set_window_handle(GstXOverlay *overlay, guintptr window)
209 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
211 /* Disable GLX rendering when vaapisink is using a foreign X
212 window. It's pretty much useless */
213 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_GLX)
214 sink->display_type = GST_VAAPI_DISPLAY_TYPE_X11;
216 sink->foreign_window = TRUE;
218 switch (sink->display_type) {
220 case GST_VAAPI_DISPLAY_TYPE_X11:
221 gst_vaapisink_ensure_window_xid(sink, window);
230 gst_vaapisink_xoverlay_set_render_rectangle(
231 GstXOverlay *overlay,
238 GstVaapiSink * const sink = GST_VAAPISINK(overlay);
239 GstVaapiRectangle * const render_rect = &sink->render_rect;
243 render_rect->width = width;
244 render_rect->height = height;
246 GST_DEBUG("render rect (%d,%d):%ux%u",
247 render_rect->x, render_rect->y,
248 render_rect->width, render_rect->height);
250 gst_vaapisink_ensure_display_rect(
252 sink->render_rect.width,
253 sink->render_rect.height);
257 gst_vaapisink_xoverlay_expose(GstXOverlay *overlay)
259 GstBaseSink * const base_sink = GST_BASE_SINK(overlay);
262 buffer = gst_base_sink_get_last_buffer(base_sink);
264 gst_vaapisink_show_frame(base_sink, buffer);
265 gst_buffer_unref(buffer);
270 gst_vaapisink_xoverlay_iface_init(GstXOverlayClass *iface)
272 iface->set_window_handle = gst_vaapisink_xoverlay_set_window_handle;
273 iface->set_render_rectangle = gst_vaapisink_xoverlay_set_render_rectangle;
274 iface->expose = gst_vaapisink_xoverlay_expose;
278 gst_vaapisink_destroy(GstVaapiSink *sink)
280 gst_buffer_replace(&sink->video_buffer, NULL);
281 g_clear_object(&sink->texture);
282 g_clear_object(&sink->display);
284 gst_caps_replace(&sink->caps, NULL);
286 if (sink->video_pool) {
287 g_object_unref(sink->video_pool);
288 sink->video_pool = NULL;
293 /* Checks whether a ConfigureNotify event is in the queue */
294 typedef struct _ConfigureNotifyEventPendingArgs ConfigureNotifyEventPendingArgs;
295 struct _ConfigureNotifyEventPendingArgs {
303 configure_notify_event_pending_cb(Display *dpy, XEvent *xev, XPointer arg)
305 ConfigureNotifyEventPendingArgs * const args =
306 (ConfigureNotifyEventPendingArgs *)arg;
308 if (xev->type == ConfigureNotify &&
309 xev->xconfigure.window == args->window &&
310 xev->xconfigure.width == args->width &&
311 xev->xconfigure.height == args->height)
314 /* XXX: this is a hack to traverse the whole queue because we
315 can't use XPeekIfEvent() since it could block */
320 configure_notify_event_pending(
327 ConfigureNotifyEventPendingArgs args;
330 args.window = window;
332 args.height = height;
335 /* XXX: don't use XPeekIfEvent() because it might block */
337 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
339 configure_notify_event_pending_cb, (XPointer)&args
346 get_display_type_name(GstVaapiDisplayType display_type)
348 gpointer const klass = g_type_class_peek(GST_VAAPI_TYPE_DISPLAY_TYPE);
349 GEnumValue * const e = g_enum_get_value(klass, display_type);
352 return e->value_name;
353 return "<unknown-type>";
356 static inline gboolean
357 gst_vaapisink_ensure_display(GstVaapiSink *sink)
359 GstVaapiDisplayType display_type;
360 GstVaapiRenderMode render_mode;
361 GstVaapiDisplay *old_display = sink->display;
363 if (!gst_vaapi_ensure_display(sink, sink->display_type, &sink->display))
366 display_type = gst_vaapi_display_get_display_type(sink->display);
367 if (display_type != sink->display_type || old_display != sink->display) {
368 GST_INFO("created %s %p", get_display_type_name(display_type),
370 sink->display_type = display_type;
373 gst_vaapi_display_get_render_mode(sink->display, &render_mode) &&
374 render_mode == GST_VAAPI_RENDER_MODE_OVERLAY;
375 GST_DEBUG("use %s rendering mode", sink->use_overlay ? "overlay" : "texture");
377 sink->use_rotation = gst_vaapi_display_has_property(
378 sink->display, GST_VAAPI_DISPLAY_PROP_ROTATION);
384 gst_vaapisink_ensure_display_rect(GstVaapiSink *sink, guint width, guint height)
386 GstVaapiRectangle * const display_rect = &sink->display_rect;
387 guint num, den, display_par_n, display_par_d;
390 /* Return success if caps are not set yet */
394 GST_DEBUG("ensure render rect within %ux%u bounds", width, height);
396 gst_vaapi_display_get_pixel_aspect_ratio(
398 &display_par_n, &display_par_d
400 GST_DEBUG("display pixel-aspect-ratio %d/%d",
401 display_par_n, display_par_d);
403 success = gst_video_calculate_display_ratio(
405 sink->video_width, sink->video_height,
406 sink->video_par_n, sink->video_par_d,
407 display_par_n, display_par_d
411 GST_DEBUG("video size %dx%d, calculated ratio %d/%d",
412 sink->video_width, sink->video_height, num, den);
414 display_rect->width = gst_util_uint64_scale_int(height, num, den);
415 if (display_rect->width <= width) {
416 GST_DEBUG("keeping window height");
417 display_rect->height = height;
420 GST_DEBUG("keeping window width");
421 display_rect->width = width;
422 display_rect->height =
423 gst_util_uint64_scale_int(width, den, num);
425 GST_DEBUG("scaling video to %ux%u", display_rect->width, display_rect->height);
427 g_assert(display_rect->width <= width);
428 g_assert(display_rect->height <= height);
430 display_rect->x = (width - display_rect->width) / 2;
431 display_rect->y = (height - display_rect->height) / 2;
433 display_rect->x += sink->render_rect.x;
434 display_rect->y += sink->render_rect.y;
436 GST_DEBUG("render rect (%d,%d):%ux%u",
437 display_rect->x, display_rect->y,
438 display_rect->width, display_rect->height);
443 gst_vaapisink_ensure_window_size(GstVaapiSink *sink, guint *pwidth, guint *pheight)
445 GstVideoRectangle src_rect, dst_rect, out_rect;
446 guint num, den, display_width, display_height, display_par_n, display_par_d;
447 gboolean success, scale;
449 if (sink->foreign_window) {
450 *pwidth = sink->window_width;
451 *pheight = sink->window_height;
455 gst_vaapi_display_get_size(sink->display, &display_width, &display_height);
456 if (sink->fullscreen) {
457 *pwidth = display_width;
458 *pheight = display_height;
462 gst_vaapi_display_get_pixel_aspect_ratio(
464 &display_par_n, &display_par_d
467 success = gst_video_calculate_display_ratio(
469 sink->video_width, sink->video_height,
470 sink->video_par_n, sink->video_par_d,
471 display_par_n, display_par_d
474 num = sink->video_par_n;
475 den = sink->video_par_d;
480 src_rect.w = gst_util_uint64_scale_int(sink->video_height, num, den);
481 src_rect.h = sink->video_height;
484 dst_rect.w = display_width;
485 dst_rect.h = display_height;
486 scale = (src_rect.w > dst_rect.w || src_rect.h > dst_rect.h);
487 gst_video_sink_center_rect(src_rect, dst_rect, &out_rect, scale);
488 *pwidth = out_rect.w;
489 *pheight = out_rect.h;
492 static inline gboolean
493 gst_vaapisink_ensure_window(GstVaapiSink *sink, guint width, guint height)
495 GstVaapiDisplay * const display = sink->display;
498 sink->render_rect.x = 0;
499 sink->render_rect.y = 0;
500 sink->render_rect.width = width;
501 sink->render_rect.height = height;
503 switch (sink->display_type) {
505 case GST_VAAPI_DISPLAY_TYPE_GLX:
506 sink->window = gst_vaapi_window_glx_new(display, width, height);
507 goto notify_xoverlay_interface;
510 case GST_VAAPI_DISPLAY_TYPE_X11:
511 sink->window = gst_vaapi_window_x11_new(display, width, height);
512 notify_xoverlay_interface:
515 gst_x_overlay_got_window_handle(
517 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window))
522 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
523 sink->window = gst_vaapi_window_wayland_new(display, width, height);
527 GST_ERROR("unsupported display type %d", sink->display_type);
531 return sink->window != NULL;
536 gst_vaapisink_ensure_window_xid(GstVaapiSink *sink, guintptr window_id)
539 unsigned int width, height, border_width, depth;
543 if (!gst_vaapisink_ensure_display(sink))
546 gst_vaapi_display_lock(sink->display);
548 gst_vaapi_display_x11_get_display(GST_VAAPI_DISPLAY_X11(sink->display)),
551 &x, &y, &width, &height, &border_width, &depth
553 gst_vaapi_display_unlock(sink->display);
555 sink->render_rect.x = 0;
556 sink->render_rect.y = 0;
557 sink->render_rect.width = width;
558 sink->render_rect.height = height;
560 if ((width != sink->window_width || height != sink->window_height) &&
561 !configure_notify_event_pending(sink, xid, width, height)) {
562 if (!gst_vaapisink_ensure_display_rect(sink, width, height))
564 sink->window_width = width;
565 sink->window_height = height;
568 if (sink->is_pixmap) {
569 for (i = 0; i < MAX_PIXMAP_COUNT; i++) {
570 if (sink->pixmap_pool[i])
571 if (sink->pixmap_pool[i] &&
572 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->pixmap_pool[i])) == xid) {
573 sink->window = sink->pixmap_pool[i];
580 gst_vaapi_window_x11_get_xid(GST_VAAPI_WINDOW_X11(sink->window)) == xid)
582 g_clear_object(&sink->window);
585 switch (sink->display_type) {
587 case GST_VAAPI_DISPLAY_TYPE_GLX:
588 sink->window = gst_vaapi_window_glx_new_with_xid(sink->display, xid);
591 case GST_VAAPI_DISPLAY_TYPE_X11:
592 sink->window = gst_vaapi_window_x11_new_with_xid(sink->display, xid, sink->is_pixmap);
595 GST_ERROR("unsupported display type %d", sink->display_type);
599 gboolean save_pixmap_to_pool = FALSE;
600 for (i = 0; i < MAX_PIXMAP_COUNT; i++) {
601 if (!sink->pixmap_pool[i]) {
602 sink->pixmap_pool[i] = sink->window;
603 save_pixmap_to_pool = TRUE;
608 if (!save_pixmap_to_pool) {
609 g_clear_object(&sink->pixmap_pool[MAX_PIXMAP_COUNT-1]);
610 sink->pixmap_pool[MAX_PIXMAP_COUNT-1] = sink->window;
611 GST_WARNING("exceed MAX_PIXMAP_COUNT: %d", MAX_PIXMAP_COUNT);
614 return sink->window != NULL;
619 gst_vaapisink_ensure_rotation(GstVaapiSink *sink, gboolean recalc_display_rect)
621 gboolean success = FALSE;
623 g_return_val_if_fail(sink->display, FALSE);
625 if (sink->rotation == sink->rotation_req)
628 if (!sink->use_rotation) {
629 GST_WARNING("VA display does not support rotation");
633 gst_vaapi_display_lock(sink->display);
634 success = gst_vaapi_display_set_rotation(sink->display, sink->rotation_req);
635 gst_vaapi_display_unlock(sink->display);
637 GST_ERROR("failed to change VA display rotation mode");
641 if (((sink->rotation + sink->rotation_req) % 180) == 90) {
642 /* Orientation changed */
643 G_PRIMITIVE_SWAP(guint, sink->video_width, sink->video_height);
644 G_PRIMITIVE_SWAP(gint, sink->video_par_n, sink->video_par_d);
647 if (recalc_display_rect )
648 gst_vaapisink_ensure_display_rect(sink, sink->render_rect.width,
649 sink->render_rect.height);
653 sink->rotation = sink->rotation_req;
658 gst_vaapisink_start(GstBaseSink *base_sink)
660 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
662 return gst_vaapisink_ensure_display(sink);
666 gst_vaapisink_stop(GstBaseSink *base_sink)
669 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
671 gst_buffer_replace(&sink->video_buffer, NULL);
672 if (sink->is_pixmap) {
673 for (i=0; i<MAX_PIXMAP_COUNT; i++) {
674 if (sink->pixmap_pool[i]) {
675 g_clear_object(&sink->pixmap_pool[i]);
680 g_clear_object(&sink->window);
683 g_clear_object(&sink->display);
685 if (sink->video_pool) {
686 g_object_unref(sink->video_pool);
687 sink->video_pool = NULL;
689 sink->buffer_sharing = FALSE;
695 gst_vaapisink_set_caps(GstBaseSink *base_sink, GstCaps *caps)
697 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
698 GstStructure * const structure = gst_caps_get_structure(caps, 0);
699 guint win_width, win_height;
700 gint video_width, video_height, video_par_n = 1, video_par_d = 1;
703 if (sink->display_type == GST_VAAPI_DISPLAY_TYPE_DRM)
709 if (gst_structure_has_name(structure, GST_VAAPI_BUFFER_SHARING_CAPS_NAME))
710 sink->buffer_sharing = TRUE;
712 sink->buffer_sharing = FALSE;
713 if (!gst_structure_get_int(structure, "width", &video_width))
715 if (!gst_structure_get_int(structure, "height", &video_height))
717 sink->video_width = video_width;
718 sink->video_height = video_height;
720 gst_video_parse_caps_pixel_aspect_ratio(caps, &video_par_n, &video_par_d);
721 sink->video_par_n = video_par_n;
722 sink->video_par_d = video_par_d;
723 GST_DEBUG("video pixel-aspect-ratio %d/%d", video_par_n, video_par_d);
725 gst_caps_replace(&sink->caps, caps);
727 if (!gst_vaapisink_ensure_display(sink))
730 gst_vaapisink_ensure_rotation(sink, FALSE);
732 gst_vaapisink_ensure_window_size(sink, &win_width, &win_height);
734 if (!sink->foreign_window || sink->fullscreen)
735 gst_vaapi_window_set_size(sink->window, win_width, win_height);
738 gst_vaapi_display_lock(sink->display);
739 gst_x_overlay_prepare_xwindow_id(GST_X_OVERLAY(sink));
741 gst_vaapisink_prepare_xid(GST_X_OVERLAY(sink)); // tizen customized interface/message for webkit-efl
742 gst_vaapi_display_unlock(sink->display);
745 if (!gst_vaapisink_ensure_window(sink, win_width, win_height))
747 gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
748 gst_vaapi_window_show(sink->window);
749 gst_vaapi_window_get_size(sink->window, &win_width, &win_height);
751 sink->window_width = win_width;
752 sink->window_height = win_height;
753 if (!sink->render_rect.width || !sink->render_rect.height) {
754 sink->render_rect.width = win_width;
755 sink->render_rect.height = win_height;
757 GST_DEBUG("window size %ux%u", win_width, win_height);
759 return gst_vaapisink_ensure_display_rect(sink,
760 sink->render_rect.width,
761 sink->render_rect.height);
766 render_background(GstVaapiSink *sink)
768 /* Original code from Mirco Muller (MacSlow):
769 <http://cgit.freedesktop.org/~macslow/gl-gst-player/> */
770 GLfloat fStartX = 0.0f;
771 GLfloat fStartY = 0.0f;
772 GLfloat fWidth = (GLfloat)sink->window_width;
773 GLfloat fHeight = (GLfloat)sink->window_height;
775 glClear(GL_COLOR_BUFFER_BIT);
778 /* top third, darker grey to white */
779 glColor3f(0.85f, 0.85f, 0.85f);
780 glVertex3f(fStartX, fStartY, 0.0f);
781 glColor3f(0.85f, 0.85f, 0.85f);
782 glVertex3f(fStartX + fWidth, fStartY, 0.0f);
783 glColor3f(1.0f, 1.0f, 1.0f);
784 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
785 glColor3f(1.0f, 1.0f, 1.0f);
786 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
788 /* middle third, just plain white */
789 glColor3f(1.0f, 1.0f, 1.0f);
790 glVertex3f(fStartX, fStartY + fHeight / 3.0f, 0.0f);
791 glVertex3f(fStartX + fWidth, fStartY + fHeight / 3.0f, 0.0f);
792 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
793 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
795 /* bottom third, white to lighter grey */
796 glColor3f(1.0f, 1.0f, 1.0f);
797 glVertex3f(fStartX, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
798 glColor3f(1.0f, 1.0f, 1.0f);
799 glVertex3f(fStartX + fWidth, fStartY + 2.0f * fHeight / 3.0f, 0.0f);
800 glColor3f(0.62f, 0.66f, 0.69f);
801 glVertex3f(fStartX + fWidth, fStartY + fHeight, 0.0f);
802 glColor3f(0.62f, 0.66f, 0.69f);
803 glVertex3f(fStartX, fStartY + fHeight, 0.0f);
809 render_frame(GstVaapiSink *sink)
811 const guint x1 = sink->display_rect.x;
812 const guint x2 = sink->display_rect.x + sink->display_rect.width;
813 const guint y1 = sink->display_rect.y;
814 const guint y2 = sink->display_rect.y + sink->display_rect.height;
816 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
819 glTexCoord2f(0.0f, 0.0f); glVertex2i(x1, y1);
820 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y2);
821 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y2);
822 glTexCoord2f(1.0f, 0.0f); glVertex2i(x2, y1);
828 render_reflection(GstVaapiSink *sink)
830 const guint x1 = sink->display_rect.x;
831 const guint x2 = sink->display_rect.x + sink->display_rect.width;
832 const guint y1 = sink->display_rect.y;
833 const guint rh = sink->display_rect.height / 5;
834 GLfloat ry = 1.0f - (GLfloat)rh / (GLfloat)sink->display_rect.height;
838 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
839 glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y1);
840 glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y1);
842 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
843 glTexCoord2f(1.0f, ry); glVertex2i(x2, y1 + rh);
844 glTexCoord2f(0.0f, ry); glVertex2i(x1, y1 + rh);
850 gst_vaapisink_show_frame_glx(
852 GstVaapiSurface *surface,
856 GstVaapiWindowGLX * const window = GST_VAAPI_WINDOW_GLX(sink->window);
860 gst_vaapi_window_glx_make_current(window);
861 if (!sink->texture) {
862 sink->texture = gst_vaapi_texture_new(
870 goto error_create_texture;
872 if (!gst_vaapi_texture_put_surface(sink->texture, surface, flags))
873 goto error_transfer_surface;
875 target = gst_vaapi_texture_get_target(sink->texture);
876 texture = gst_vaapi_texture_get_id(sink->texture);
877 if (target != GL_TEXTURE_2D || !texture)
880 if (sink->use_reflection)
881 render_background(sink);
884 glBindTexture(target, texture);
886 if (sink->use_reflection) {
888 glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
889 glTranslatef(50.0f, 0.0f, 0.0f);
892 if (sink->use_reflection) {
894 glTranslatef(0.0, (GLfloat)sink->display_rect.height + 5.0f, 0.0f);
895 render_reflection(sink);
900 glBindTexture(target, 0);
902 gst_vaapi_window_glx_swap_buffers(window);
906 error_create_texture:
908 GST_DEBUG("could not create VA/GLX texture");
911 error_transfer_surface:
913 GST_DEBUG("could not transfer VA surface to texture");
919 static inline gboolean
920 gst_vaapisink_put_surface(
922 GstVaapiSurface *surface,
926 if (!gst_vaapi_window_put_surface(sink->window, surface,
927 NULL, &sink->display_rect, flags)) {
928 GST_DEBUG("could not render VA surface");
935 gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer)
937 GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
938 GstVaapiVideoBuffer * vbuffer;
939 GstVaapiSurface *surface;
942 GstVideoOverlayComposition * const composition =
943 gst_video_buffer_get_overlay_composition(buffer);
945 if (sink->buffer_sharing)
946 vbuffer = GST_VAAPI_VIDEO_BUFFER(buffer->data);
948 vbuffer = GST_VAAPI_VIDEO_BUFFER(buffer);
950 if (sink->display != gst_vaapi_video_buffer_get_display (vbuffer)) {
951 g_clear_object(&sink->display);
952 sink->display = g_object_ref (gst_vaapi_video_buffer_get_display (vbuffer));
956 return GST_FLOW_UNEXPECTED;
958 gst_vaapisink_ensure_rotation(sink, TRUE);
960 surface = gst_vaapi_video_buffer_get_surface(vbuffer);
962 return GST_FLOW_UNEXPECTED;
964 GST_DEBUG("render surface %" GST_VAAPI_ID_FORMAT,
965 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
967 flags = gst_vaapi_video_buffer_get_render_flags(vbuffer);
969 if (!gst_vaapi_surface_set_subpictures_from_composition(surface,
971 GST_WARNING("could not update subtitles");
973 switch (sink->display_type) {
975 case GST_VAAPI_DISPLAY_TYPE_GLX:
976 success = gst_vaapisink_show_frame_glx(sink, surface, flags);
980 case GST_VAAPI_DISPLAY_TYPE_DRM:
985 case GST_VAAPI_DISPLAY_TYPE_X11:
986 success = gst_vaapisink_put_surface(sink, surface, flags);
990 case GST_VAAPI_DISPLAY_TYPE_WAYLAND:
991 success = gst_vaapisink_put_surface(sink, surface, flags);
995 GST_ERROR("unsupported display type %d", sink->display_type);
1000 return GST_FLOW_UNEXPECTED;
1002 /* Retain VA surface until the next one is displayed */
1003 if (sink->use_overlay)
1004 gst_buffer_replace(&sink->video_buffer, buffer);
1006 FPS_CALCULATION(vaapisink);
1011 gst_vaapisink_query(GstBaseSink *base_sink, GstQuery *query)
1013 GstVaapiSink *sink = GST_VAAPISINK(base_sink);
1014 GST_DEBUG ("sharing display %p", sink->display);
1015 return gst_vaapi_reply_to_query (query, sink->display);
1019 gst_vaapisink_ensure_video_pool(
1023 GstStructure *structure;
1024 gint width = 0, height = 0;
1026 g_return_val_if_fail(caps, FALSE);
1028 structure = gst_caps_get_structure(caps, 0);
1029 g_return_val_if_fail(structure, FALSE);
1031 if (sink->video_pool)
1034 if (!gst_structure_get_int(structure, "width", &width) ||
1035 !gst_structure_get_int(structure, "height", &height))
1038 sink->video_pool = gst_vaapi_surface_pool_new(sink->display, caps);
1039 if (!sink->video_pool) {
1046 static GstFlowReturn
1047 gst_vaapisink_pad_buffer_alloc(
1054 GstVaapiSink * const sink = GST_VAAPISINK(GST_OBJECT_PARENT(pad));
1055 GstStructure *structure = NULL;
1056 GstBuffer *video_buffer = NULL;
1061 structure = gst_caps_get_structure(caps, 0);
1063 (!gst_structure_has_name(structure, GST_VAAPI_SURFACE_CAPS_NAME) &&
1064 !gst_structure_has_name(structure, GST_VAAPI_BUFFER_SHARING_CAPS_NAME)))
1067 if (!gst_vaapisink_ensure_display(sink))
1070 if (!gst_vaapisink_ensure_video_pool(sink, caps))
1073 video_buffer = gst_vaapi_video_buffer_new_from_pool(sink->video_pool);
1075 if (gst_structure_has_name(structure, GST_VAAPI_BUFFER_SHARING_CAPS_NAME) &&
1076 !gst_vaapi_video_buffer_ensure_pointer(GST_VAAPI_VIDEO_BUFFER(video_buffer)))
1079 *buf = video_buffer;
1083 GST_ERROR("vaapisink allocate buffer failed.");
1084 return GST_FLOW_ERROR;
1088 gst_vaapisink_finalize(GObject *object)
1090 gst_vaapisink_destroy(GST_VAAPISINK(object));
1092 G_OBJECT_CLASS(gst_vaapisink_parent_class)->finalize(object);
1096 gst_vaapisink_set_property(
1099 const GValue *value,
1103 GstVaapiSink * const sink = GST_VAAPISINK(object);
1106 case PROP_DISPLAY_TYPE:
1107 sink->display_type = g_value_get_enum(value);
1109 case PROP_FULLSCREEN:
1110 sink->fullscreen = g_value_get_boolean(value);
1112 case PROP_SYNCHRONOUS:
1113 sink->synchronous = g_value_get_boolean(value);
1115 case PROP_USE_REFLECTION:
1116 sink->use_reflection = g_value_get_boolean(value);
1119 sink->rotation_req = g_value_get_enum(value);
1121 case PROP_IS_PIXMAP:
1122 sink->is_pixmap = g_value_get_boolean(value);
1125 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1131 gst_vaapisink_get_property(
1138 GstVaapiSink * const sink = GST_VAAPISINK(object);
1141 case PROP_DISPLAY_TYPE:
1142 g_value_set_enum(value, sink->display_type);
1144 case PROP_FULLSCREEN:
1145 g_value_set_boolean(value, sink->fullscreen);
1147 case PROP_SYNCHRONOUS:
1148 g_value_set_boolean(value, sink->synchronous);
1150 case PROP_USE_REFLECTION:
1151 g_value_set_boolean(value, sink->use_reflection);
1154 g_value_set_enum(value, sink->rotation);
1156 case PROP_IS_PIXMAP:
1157 g_value_set_boolean(value, sink->is_pixmap);
1160 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1166 gst_vaapisink_class_init(GstVaapiSinkClass *klass)
1168 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
1169 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
1170 GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
1171 GstPadTemplate *pad_template;
1173 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
1174 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
1176 object_class->finalize = gst_vaapisink_finalize;
1177 object_class->set_property = gst_vaapisink_set_property;
1178 object_class->get_property = gst_vaapisink_get_property;
1180 basesink_class->start = gst_vaapisink_start;
1181 basesink_class->stop = gst_vaapisink_stop;
1182 basesink_class->set_caps = gst_vaapisink_set_caps;
1183 basesink_class->preroll = gst_vaapisink_show_frame;
1184 basesink_class->render = gst_vaapisink_show_frame;
1185 basesink_class->query = gst_vaapisink_query;
1187 gst_element_class_set_details_simple(
1189 gst_vaapisink_details.longname,
1190 gst_vaapisink_details.klass,
1191 gst_vaapisink_details.description,
1192 gst_vaapisink_details.author
1195 pad_template = gst_static_pad_template_get(&gst_vaapisink_sink_factory);
1196 gst_element_class_add_pad_template(element_class, pad_template);
1197 gst_object_unref(pad_template);
1199 g_object_class_install_property
1202 g_param_spec_enum("display",
1204 "display type to use",
1205 GST_VAAPI_TYPE_DISPLAY_TYPE,
1206 GST_VAAPI_DISPLAY_TYPE_ANY,
1207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1210 g_object_class_install_property
1212 PROP_USE_REFLECTION,
1213 g_param_spec_boolean("use-reflection",
1214 "Reflection effect",
1215 "Enables OpenGL reflection effect",
1217 G_PARAM_READWRITE));
1220 g_object_class_install_property
1223 g_param_spec_boolean("fullscreen",
1225 "Requests window in fullscreen state",
1227 G_PARAM_READWRITE));
1230 * GstVaapiSink:synchronous:
1232 * When enabled, runs the X display in synchronous mode. Note that
1233 * this is used only for debugging.
1235 g_object_class_install_property
1238 g_param_spec_boolean("synchronous",
1240 "Toggles X display synchronous mode",
1242 G_PARAM_READWRITE));
1245 * GstVaapiSink:rotation:
1247 * The VA display rotation mode, expressed as a #GstVaapiRotation.
1249 g_object_class_install_property
1252 g_param_spec_enum(GST_VAAPI_DISPLAY_PROP_ROTATION,
1254 "The display rotation mode",
1255 GST_VAAPI_TYPE_ROTATION,
1257 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1259 g_object_class_install_property
1262 g_param_spec_boolean("is-pixmap",
1264 "the X drawable is Pixmap or not",
1266 G_PARAM_READWRITE));
1271 gst_vaapisink_init(GstVaapiSink *sink)
1274 GstPad *sinkpad = GST_BASE_SINK_PAD(sink);
1276 gst_pad_set_bufferalloc_function(sinkpad,
1277 gst_vaapisink_pad_buffer_alloc);
1280 sink->display = NULL;
1281 sink->window = NULL;
1282 sink->window_width = 0;
1283 sink->window_height = 0;
1284 sink->texture = NULL;
1285 sink->video_buffer = NULL;
1286 sink->video_width = 0;
1287 sink->video_height = 0;
1288 sink->video_par_n = 1;
1289 sink->video_par_d = 1;
1290 sink->foreign_window = FALSE;
1291 sink->fullscreen = FALSE;
1292 sink->synchronous = FALSE;
1293 sink->display_type = DEFAULT_DISPLAY_TYPE;
1294 sink->rotation = DEFAULT_ROTATION;
1295 sink->rotation_req = DEFAULT_ROTATION;
1296 sink->use_reflection = FALSE;
1297 sink->use_overlay = FALSE;
1298 sink->use_rotation = FALSE;
1299 sink->render_rect.x = 0;
1300 sink->render_rect.y = 0;
1301 sink->render_rect.width = 0;
1302 sink->render_rect.height = 0;
1303 sink->video_pool = NULL;
1304 sink->buffer_sharing = FALSE;
1305 sink->is_pixmap = FALSE;
1307 for (i=0; i<MAX_PIXMAP_COUNT; i++) {
1308 sink->pixmap_pool[i] = NULL;