3 * Copyright (C) 2016 Igalia
6 * Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
7 * Javier Martin <javiermartin@by.com.es>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
27 * SECTION:element-kmssink
29 * @short_description: A KMS/DRM based video sink
31 * kmssink is a simple video sink that renders video frames directly
32 * in a plane of a DRM device.
34 * In advance usage, the behaviour of kmssink can be change using the
35 * supported properties. Note that plane and connectors IDs and properties can
36 * be enumerated using the modetest command line tool.
38 * ## Example launch line
40 * gst-launch-1.0 videotestsrc ! kmssink
41 * gst-launch-1.0 videotestsrc ! kmssink plane-properties=s,rotation=4
50 #include <gst/video/video.h>
51 #include <gst/video/videooverlay.h>
52 #include <gst/allocators/gstdmabuf.h>
56 #include <xf86drmMode.h>
57 #include <drm_fourcc.h>
60 #include "gstkmssink.h"
61 #include "gstkmsutils.h"
62 #include "gstkmsbufferpool.h"
63 #include "gstkmsallocator.h"
65 #define GST_PLUGIN_NAME "kmssink"
66 #define GST_PLUGIN_DESC "Video sink using the Linux kernel mode setting API"
68 GST_DEBUG_CATEGORY_STATIC (gst_kms_sink_debug);
69 GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
70 #define GST_CAT_DEFAULT gst_kms_sink_debug
72 static GstFlowReturn gst_kms_sink_show_frame (GstVideoSink * vsink,
74 static void gst_kms_sink_video_overlay_init (GstVideoOverlayInterface * iface);
75 static void gst_kms_sink_drain (GstKMSSink * self);
77 #define parent_class gst_kms_sink_parent_class
78 G_DEFINE_TYPE_WITH_CODE (GstKMSSink, gst_kms_sink, GST_TYPE_VIDEO_SINK,
79 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, GST_PLUGIN_NAME, 0,
81 GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
82 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
83 gst_kms_sink_video_overlay_init));
91 PROP_FORCE_MODESETTING,
101 static GParamSpec *g_properties[PROP_N] = { NULL, };
104 gst_kms_sink_set_render_rectangle (GstVideoOverlay * overlay,
105 gint x, gint y, gint width, gint height)
107 GstKMSSink *self = GST_KMS_SINK (overlay);
109 GST_DEBUG_OBJECT (self, "Setting render rectangle to (%d,%d) %dx%d", x, y,
112 GST_OBJECT_LOCK (self);
114 if (width == -1 && height == -1) {
117 width = self->hdisplay;
118 height = self->vdisplay;
121 if (width <= 0 || height <= 0)
124 self->pending_rect.x = x;
125 self->pending_rect.y = y;
126 self->pending_rect.w = width;
127 self->pending_rect.h = height;
129 if (self->can_scale ||
130 (self->render_rect.w == width && self->render_rect.h == height)) {
131 self->render_rect = self->pending_rect;
133 self->reconfigure = TRUE;
134 GST_DEBUG_OBJECT (self, "Waiting for new caps to apply render rectangle");
138 GST_OBJECT_UNLOCK (self);
142 gst_kms_sink_expose (GstVideoOverlay * overlay)
144 GstKMSSink *self = GST_KMS_SINK (overlay);
146 GST_DEBUG_OBJECT (overlay, "Expose called by application");
148 if (!self->can_scale) {
149 GST_OBJECT_LOCK (self);
150 if (self->reconfigure) {
151 GST_OBJECT_UNLOCK (self);
152 GST_DEBUG_OBJECT (overlay, "Sending a reconfigure event");
153 gst_pad_push_event (GST_BASE_SINK_PAD (self),
154 gst_event_new_reconfigure ());
156 GST_DEBUG_OBJECT (overlay, "Applying new render rectangle");
157 /* size of the rectangle does not change, only the (x,y) position changes */
158 self->render_rect = self->pending_rect;
159 GST_OBJECT_UNLOCK (self);
163 gst_kms_sink_show_frame (GST_VIDEO_SINK (self), NULL);
167 gst_kms_sink_video_overlay_init (GstVideoOverlayInterface * iface)
169 iface->expose = gst_kms_sink_expose;
170 iface->set_render_rectangle = gst_kms_sink_set_render_rectangle;
174 kms_open (gchar ** driver)
176 static const char *drivers[] = { "i915", "radeon", "nouveau", "vmwgfx",
177 "exynos", "amdgpu", "imx-drm", "rockchip", "atmel-hlcdc", "msm",
178 "xlnx", "vc4", "meson", "sun4i-drm", "mxsfb-drm",
179 "xilinx_drm", /* DEPRECATED. Replaced by xlnx */
183 for (i = 0; i < G_N_ELEMENTS (drivers); i++) {
184 fd = drmOpen (drivers[i], NULL);
187 *driver = g_strdup (drivers[i]);
195 static drmModePlane *
196 find_plane_for_crtc (int fd, drmModeRes * res, drmModePlaneRes * pres,
204 for (i = 0; i < res->count_crtcs; i++) {
205 if (crtc_id == res->crtcs[i]) {
214 for (i = 0; i < pres->count_planes; i++) {
215 plane = drmModeGetPlane (fd, pres->planes[i]);
216 if (plane->possible_crtcs & (1 << pipe))
218 drmModeFreePlane (plane);
225 find_crtc_for_connector (int fd, drmModeRes * res, drmModeConnector * conn,
232 guint32 crtcs_for_connector = 0;
235 for (i = 0; i < res->count_encoders; i++) {
236 enc = drmModeGetEncoder (fd, res->encoders[i]);
238 if (enc->encoder_id == conn->encoder_id) {
239 crtc_id = enc->crtc_id;
240 drmModeFreeEncoder (enc);
243 drmModeFreeEncoder (enc);
247 /* If no active crtc was found, pick the first possible crtc */
249 for (i = 0; i < conn->count_encoders; i++) {
250 enc = drmModeGetEncoder (fd, conn->encoders[i]);
251 crtcs_for_connector |= enc->possible_crtcs;
252 drmModeFreeEncoder (enc);
255 if (crtcs_for_connector != 0)
256 crtc_id = res->crtcs[ffs (crtcs_for_connector) - 1];
262 for (i = 0; i < res->count_crtcs; i++) {
263 crtc = drmModeGetCrtc (fd, res->crtcs[i]);
265 if (crtc_id == crtc->crtc_id) {
270 drmModeFreeCrtc (crtc);
278 connector_is_used (int fd, drmModeRes * res, drmModeConnector * conn)
284 crtc = find_crtc_for_connector (fd, res, conn, NULL);
286 result = crtc->buffer_id != 0;
287 drmModeFreeCrtc (crtc);
293 static drmModeConnector *
294 find_used_connector_by_type (int fd, drmModeRes * res, int type)
297 drmModeConnector *conn;
300 for (i = 0; i < res->count_connectors; i++) {
301 conn = drmModeGetConnector (fd, res->connectors[i]);
303 if ((conn->connector_type == type) && connector_is_used (fd, res, conn))
305 drmModeFreeConnector (conn);
312 static drmModeConnector *
313 find_first_used_connector (int fd, drmModeRes * res)
316 drmModeConnector *conn;
319 for (i = 0; i < res->count_connectors; i++) {
320 conn = drmModeGetConnector (fd, res->connectors[i]);
322 if (connector_is_used (fd, res, conn))
324 drmModeFreeConnector (conn);
331 static drmModeConnector *
332 find_main_monitor (int fd, drmModeRes * res)
334 /* Find the LVDS and eDP connectors: those are the main screens. */
335 static const int priority[] = { DRM_MODE_CONNECTOR_LVDS,
336 DRM_MODE_CONNECTOR_eDP
339 drmModeConnector *conn;
342 for (i = 0; !conn && i < G_N_ELEMENTS (priority); i++)
343 conn = find_used_connector_by_type (fd, res, priority[i]);
345 /* if we didn't find a connector, grab the first one in use */
347 conn = find_first_used_connector (fd, res);
349 /* if no connector is used, grab the first one */
351 conn = drmModeGetConnector (fd, res->connectors[0]);
357 log_drm_version (GstKMSSink * self)
359 #ifndef GST_DISABLE_GST_DEBUG
362 v = drmGetVersion (self->fd);
364 GST_INFO_OBJECT (self, "DRM v%d.%d.%d [%s — %s — %s]", v->version_major,
365 v->version_minor, v->version_patchlevel, GST_STR_NULL (v->name),
366 GST_STR_NULL (v->desc), GST_STR_NULL (v->date));
369 GST_WARNING_OBJECT (self, "could not get driver information: %s",
370 GST_STR_NULL (self->devname));
377 get_drm_caps (GstKMSSink * self)
380 guint64 has_dumb_buffer;
382 guint64 has_async_page_flip;
385 ret = drmGetCap (self->fd, DRM_CAP_DUMB_BUFFER, &has_dumb_buffer);
387 GST_WARNING_OBJECT (self, "could not get dumb buffer capability");
388 if (has_dumb_buffer == 0) {
389 GST_ERROR_OBJECT (self, "driver cannot handle dumb buffers");
394 ret = drmGetCap (self->fd, DRM_CAP_PRIME, &has_prime);
396 GST_WARNING_OBJECT (self, "could not get prime capability");
398 self->has_prime_import = (gboolean) (has_prime & DRM_PRIME_CAP_IMPORT);
399 self->has_prime_export = (gboolean) (has_prime & DRM_PRIME_CAP_EXPORT);
402 has_async_page_flip = 0;
403 ret = drmGetCap (self->fd, DRM_CAP_ASYNC_PAGE_FLIP, &has_async_page_flip);
405 GST_WARNING_OBJECT (self, "could not get async page flip capability");
407 self->has_async_page_flip = (gboolean) has_async_page_flip;
409 GST_INFO_OBJECT (self,
410 "prime import (%s) / prime export (%s) / async page flip (%s)",
411 self->has_prime_import ? "✓" : "✗",
412 self->has_prime_export ? "✓" : "✗",
413 self->has_async_page_flip ? "✓" : "✗");
419 configure_mode_setting (GstKMSSink * self, GstVideoInfo * vinfo)
422 drmModeConnector *conn;
425 drmModeModeInfo *mode;
427 GstKMSMemory *kmsmem;
434 if (self->conn_id < 0)
437 GST_INFO_OBJECT (self, "configuring mode setting");
439 kmsmem = (GstKMSMemory *) gst_kms_allocator_bo_alloc (self->allocator, vinfo);
442 fb_id = kmsmem->fb_id;
444 conn = drmModeGetConnector (self->fd, self->conn_id);
446 goto connector_failed;
448 for (i = 0; i < conn->count_modes; i++) {
449 if (conn->modes[i].vdisplay == GST_VIDEO_INFO_HEIGHT (vinfo) &&
450 conn->modes[i].hdisplay == GST_VIDEO_INFO_WIDTH (vinfo)) {
451 mode = &conn->modes[i];
458 err = drmModeSetCrtc (self->fd, self->crtc_id, fb_id, 0, 0,
459 (uint32_t *) & self->conn_id, 1, mode);
461 goto modesetting_failed;
463 g_clear_pointer (&self->tmp_kmsmem, gst_memory_unref);
464 self->tmp_kmsmem = (GstMemory *) kmsmem;
470 drmModeFreeConnector (conn);
477 GST_ERROR_OBJECT (self,
478 "failed to allocate buffer object for mode setting");
483 GST_ERROR_OBJECT (self, "Could not find a valid monitor connector");
488 GST_ERROR_OBJECT (self, "cannot find appropriate mode");
493 GST_ERROR_OBJECT (self, "Failed to set mode: %s", g_strerror (errno));
499 ensure_allowed_caps (GstKMSSink * self, drmModeConnector * conn,
500 drmModePlane * plane, drmModeRes * res)
502 GstCaps *out_caps, *tmp_caps, *caps;
506 drmModeModeInfo *mode;
509 if (self->allowed_caps)
512 out_caps = gst_caps_new_empty ();
516 if (conn && self->modesetting_enabled)
517 count_modes = conn->count_modes;
521 for (i = 0; i < count_modes; i++) {
522 tmp_caps = gst_caps_new_empty ();
527 if (conn && self->modesetting_enabled)
528 mode = &conn->modes[i];
530 for (j = 0; j < plane->count_formats; j++) {
531 fmt = gst_video_format_from_drm (plane->formats[j]);
532 if (fmt == GST_VIDEO_FORMAT_UNKNOWN) {
533 GST_INFO_OBJECT (self, "ignoring format %" GST_FOURCC_FORMAT,
534 GST_FOURCC_ARGS (plane->formats[j]));
538 format = gst_video_format_to_string (fmt);
541 caps = gst_caps_new_simple ("video/x-raw",
542 "format", G_TYPE_STRING, format,
543 "width", G_TYPE_INT, mode->hdisplay,
544 "height", G_TYPE_INT, mode->vdisplay,
545 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
547 caps = gst_caps_new_simple ("video/x-raw",
548 "format", G_TYPE_STRING, format,
549 "width", GST_TYPE_INT_RANGE, res->min_width, res->max_width,
550 "height", GST_TYPE_INT_RANGE, res->min_height, res->max_height,
551 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
556 tmp_caps = gst_caps_merge (tmp_caps, caps);
559 out_caps = gst_caps_merge (out_caps, gst_caps_simplify (tmp_caps));
562 self->allowed_caps = gst_caps_simplify (out_caps);
564 GST_DEBUG_OBJECT (self, "allowed caps = %" GST_PTR_FORMAT,
567 return (self->allowed_caps && !gst_caps_is_empty (self->allowed_caps));
571 set_drm_property (gint fd, guint32 object, guint32 object_type,
572 drmModeObjectPropertiesPtr properties, const gchar * prop_name,
576 gboolean ret = FALSE;
578 for (i = 0; i < properties->count_props && !ret; i++) {
579 drmModePropertyPtr property;
581 property = drmModeGetProperty (fd, properties->props[i]);
583 /* GstStructure parser limits the set of supported character, so we
584 * replace the invalid characters with '-'. In DRM, this is generally
585 * replacing spaces into '-'. */
586 g_strcanon (property->name, G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "_",
589 GST_LOG ("found property %s (looking for %s)", property->name, prop_name);
591 if (!strcmp (property->name, prop_name)) {
592 drmModeObjectSetProperty (fd, object, object_type,
593 property->prop_id, value);
596 drmModeFreeProperty (property);
605 drmModeObjectPropertiesPtr properties;
608 const gchar *obj_type_str;
612 set_obj_prop (GQuark field_id, const GValue * value, gpointer user_data)
614 SetPropsIter *iter = user_data;
615 GstKMSSink *self = iter->self;
619 name = g_quark_to_string (field_id);
621 if (G_VALUE_HOLDS (value, G_TYPE_INT))
622 v = g_value_get_int (value);
623 else if (G_VALUE_HOLDS (value, G_TYPE_UINT))
624 v = g_value_get_uint (value);
625 else if (G_VALUE_HOLDS (value, G_TYPE_INT64))
626 v = g_value_get_int64 (value);
627 else if (G_VALUE_HOLDS (value, G_TYPE_UINT64))
628 v = g_value_get_uint64 (value);
630 GST_WARNING_OBJECT (self,
631 "'uint64' value expected for control '%s'.", name);
635 if (set_drm_property (self->fd, iter->obj_id, iter->obj_type,
636 iter->properties, name, v)) {
637 GST_DEBUG_OBJECT (self,
638 "Set %s property '%s' to %" G_GUINT64_FORMAT,
639 iter->obj_type_str, name, v);
641 GST_WARNING_OBJECT (self,
642 "Failed to set %s property '%s' to %" G_GUINT64_FORMAT,
643 iter->obj_type_str, name, v);
650 gst_kms_sink_update_properties (SetPropsIter * iter, GstStructure * props)
652 GstKMSSink *self = iter->self;
654 iter->properties = drmModeObjectGetProperties (self->fd, iter->obj_id,
657 gst_structure_foreach (props, set_obj_prop, iter);
659 drmModeFreeObjectProperties (iter->properties);
663 gst_kms_sink_update_connector_properties (GstKMSSink * self)
667 if (!self->connector_props)
671 iter.obj_id = self->conn_id;
672 iter.obj_type = DRM_MODE_OBJECT_CONNECTOR;
673 iter.obj_type_str = "connector";
675 gst_kms_sink_update_properties (&iter, self->connector_props);
679 gst_kms_sink_update_plane_properties (GstKMSSink * self)
683 if (!self->plane_props)
687 iter.obj_id = self->plane_id;
688 iter.obj_type = DRM_MODE_OBJECT_PLANE;
689 iter.obj_type_str = "plane";
691 gst_kms_sink_update_properties (&iter, self->plane_props);
695 gst_kms_sink_start (GstBaseSink * bsink)
699 drmModeConnector *conn;
701 drmModePlaneRes *pres;
703 gboolean universal_planes;
706 self = GST_KMS_SINK (bsink);
707 universal_planes = FALSE;
715 if (self->devname || self->bus_id)
716 self->fd = drmOpen (self->devname, self->bus_id);
718 self->fd = kms_open (&self->devname);
722 log_drm_version (self);
723 if (!get_drm_caps (self))
726 res = drmModeGetResources (self->fd);
728 goto resources_failed;
730 if (self->conn_id == -1)
731 conn = find_main_monitor (self->fd, res);
733 conn = drmModeGetConnector (self->fd, self->conn_id);
735 goto connector_failed;
737 crtc = find_crtc_for_connector (self->fd, res, conn, &self->pipe);
741 if (!crtc->mode_valid || self->modesetting_enabled) {
742 GST_DEBUG_OBJECT (self, "enabling modesetting");
743 self->modesetting_enabled = TRUE;
744 universal_planes = TRUE;
747 if (crtc->mode_valid && self->modesetting_enabled && self->restore_crtc) {
748 self->saved_crtc = (drmModeCrtc *) crtc;
752 if (universal_planes &&
753 drmSetClientCap (self->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1))
756 pres = drmModeGetPlaneResources (self->fd);
758 goto plane_resources_failed;
760 if (self->plane_id == -1)
761 plane = find_plane_for_crtc (self->fd, res, pres, crtc->crtc_id);
763 plane = drmModeGetPlane (self->fd, self->plane_id);
767 if (!ensure_allowed_caps (self, conn, plane, res))
768 goto allowed_caps_failed;
770 self->conn_id = conn->connector_id;
771 self->crtc_id = crtc->crtc_id;
772 self->plane_id = plane->plane_id;
774 GST_INFO_OBJECT (self, "connector id = %d / crtc id = %d / plane id = %d",
775 self->conn_id, self->crtc_id, self->plane_id);
777 GST_OBJECT_LOCK (self);
778 self->hdisplay = crtc->mode.hdisplay;
779 self->vdisplay = crtc->mode.vdisplay;
781 if (self->render_rect.w == 0 || self->render_rect.h == 0) {
782 self->render_rect.x = 0;
783 self->render_rect.y = 0;
784 self->render_rect.w = self->hdisplay;
785 self->render_rect.h = self->vdisplay;
788 self->pending_rect = self->render_rect;
789 GST_OBJECT_UNLOCK (self);
791 self->buffer_id = crtc->buffer_id;
793 self->mm_width = conn->mmWidth;
794 self->mm_height = conn->mmHeight;
796 GST_INFO_OBJECT (self, "display size: pixels = %dx%d / millimeters = %dx%d",
797 self->hdisplay, self->vdisplay, self->mm_width, self->mm_height);
799 self->pollfd.fd = self->fd;
800 gst_poll_add_fd (self->poll, &self->pollfd);
801 gst_poll_fd_ctl_read (self->poll, &self->pollfd, TRUE);
803 g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_WIDTH]);
804 g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_HEIGHT]);
806 gst_kms_sink_update_connector_properties (self);
807 gst_kms_sink_update_plane_properties (self);
813 drmModeFreePlane (plane);
815 drmModeFreePlaneResources (pres);
816 if (crtc != self->saved_crtc)
817 drmModeFreeCrtc (crtc);
819 drmModeFreeConnector (conn);
821 drmModeFreeResources (res);
823 if (!ret && self->fd >= 0) {
833 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ_WRITE,
834 ("Could not open DRM module %s", GST_STR_NULL (self->devname)),
835 ("reason: %s (%d)", g_strerror (errno), errno));
841 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
842 ("drmModeGetResources failed"),
843 ("reason: %s (%d)", g_strerror (errno), errno));
849 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
850 ("Could not find a valid monitor connector"), (NULL));
856 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
857 ("Could not find a crtc for connector"), (NULL));
863 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
864 ("Could not set universal planes capability bit"), (NULL));
868 plane_resources_failed:
870 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
871 ("drmModeGetPlaneResources failed"),
872 ("reason: %s (%d)", g_strerror (errno), errno));
878 if (universal_planes) {
879 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
880 ("Could not find a plane for crtc"), (NULL));
883 universal_planes = TRUE;
884 goto retry_find_plane;
890 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
891 ("Could not get allowed GstCaps of device"),
892 ("driver does not provide mode settings configuration"));
898 gst_kms_sink_stop (GstBaseSink * bsink)
903 self = GST_KMS_SINK (bsink);
906 gst_kms_allocator_clear_cache (self->allocator);
908 gst_buffer_replace (&self->last_buffer, NULL);
909 gst_caps_replace (&self->allowed_caps, NULL);
910 gst_object_replace ((GstObject **) & self->pool, NULL);
911 gst_object_replace ((GstObject **) & self->allocator, NULL);
913 gst_poll_remove_fd (self->poll, &self->pollfd);
914 gst_poll_restart (self->poll);
915 gst_poll_fd_init (&self->pollfd);
917 if (self->saved_crtc) {
918 drmModeCrtc *crtc = (drmModeCrtc *) self->saved_crtc;
920 err = drmModeSetCrtc (self->fd, crtc->crtc_id, crtc->buffer_id, crtc->x,
921 crtc->y, (uint32_t *) & self->conn_id, 1, &crtc->mode);
923 GST_ERROR_OBJECT (self, "Failed to restore previous CRTC mode: %s",
926 drmModeFreeCrtc (crtc);
927 self->saved_crtc = NULL;
935 GST_OBJECT_LOCK (bsink);
938 self->pending_rect.x = 0;
939 self->pending_rect.y = 0;
940 self->pending_rect.w = 0;
941 self->pending_rect.h = 0;
942 self->render_rect = self->pending_rect;
943 GST_OBJECT_UNLOCK (bsink);
945 g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_WIDTH]);
946 g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_HEIGHT]);
952 gst_kms_sink_get_allowed_caps (GstKMSSink * self)
954 if (!self->allowed_caps)
955 return NULL; /* base class will return the template caps */
956 return gst_caps_ref (self->allowed_caps);
960 gst_kms_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
963 GstCaps *caps, *out_caps;
965 guint dpy_par_n, dpy_par_d;
967 self = GST_KMS_SINK (bsink);
969 caps = gst_kms_sink_get_allowed_caps (self);
973 GST_OBJECT_LOCK (self);
975 if (!self->can_scale) {
976 out_caps = gst_caps_new_empty ();
977 gst_video_calculate_device_ratio (self->hdisplay, self->vdisplay,
978 self->mm_width, self->mm_height, &dpy_par_n, &dpy_par_d);
980 s = gst_structure_copy (gst_caps_get_structure (caps, 0));
981 gst_structure_set (s, "width", G_TYPE_INT, self->pending_rect.w,
982 "height", G_TYPE_INT, self->pending_rect.h,
983 "pixel-aspect-ratio", GST_TYPE_FRACTION, dpy_par_n, dpy_par_d, NULL);
985 gst_caps_append_structure (out_caps, s);
987 out_caps = gst_caps_merge (out_caps, caps);
990 /* enforce our display aspect ratio */
991 gst_caps_set_simple (out_caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
992 dpy_par_n, dpy_par_d, NULL);
994 out_caps = gst_caps_make_writable (caps);
998 GST_OBJECT_UNLOCK (self);
1000 GST_DEBUG_OBJECT (self, "Proposing caps %" GST_PTR_FORMAT, out_caps);
1004 out_caps = gst_caps_intersect_full (caps, filter, GST_CAPS_INTERSECT_FIRST);
1005 gst_caps_unref (caps);
1012 ensure_kms_allocator (GstKMSSink * self)
1014 if (self->allocator)
1016 self->allocator = gst_kms_allocator_new (self->fd);
1019 static GstBufferPool *
1020 gst_kms_sink_create_pool (GstKMSSink * self, GstCaps * caps, gsize size,
1023 GstBufferPool *pool;
1024 GstStructure *config;
1026 pool = gst_kms_buffer_pool_new ();
1030 config = gst_buffer_pool_get_config (pool);
1031 gst_buffer_pool_config_set_params (config, caps, size, min, 0);
1032 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
1034 ensure_kms_allocator (self);
1035 gst_buffer_pool_config_set_allocator (config, self->allocator, NULL);
1037 if (!gst_buffer_pool_set_config (pool, config))
1045 GST_ERROR_OBJECT (self, "failed to create buffer pool");
1050 GST_ERROR_OBJECT (self, "failed to set config");
1051 gst_object_unref (pool);
1057 gst_kms_sink_calculate_display_ratio (GstKMSSink * self, GstVideoInfo * vinfo)
1060 guint video_width, video_height;
1061 guint video_par_n, video_par_d;
1062 guint dpy_par_n, dpy_par_d;
1064 video_width = GST_VIDEO_INFO_WIDTH (vinfo);
1065 video_height = GST_VIDEO_INFO_HEIGHT (vinfo);
1066 video_par_n = GST_VIDEO_INFO_PAR_N (vinfo);
1067 video_par_d = GST_VIDEO_INFO_PAR_D (vinfo);
1069 if (self->can_scale) {
1070 gst_video_calculate_device_ratio (self->hdisplay, self->vdisplay,
1071 self->mm_width, self->mm_height, &dpy_par_n, &dpy_par_d);
1073 GST_VIDEO_SINK_WIDTH (self) = video_width;
1074 GST_VIDEO_SINK_HEIGHT (self) = video_height;
1078 if (!gst_video_calculate_display_ratio (&dar_n, &dar_d, video_width,
1079 video_height, video_par_n, video_par_d, dpy_par_n, dpy_par_d))
1082 GST_DEBUG_OBJECT (self, "video calculated display ratio: %d/%d", dar_n,
1085 /* now find a width x height that respects this display ratio.
1086 * prefer those that have one of w/h the same as the incoming video
1087 * using wd / hd = dar_n / dar_d */
1089 /* start with same height, because of interlaced video */
1090 /* check hd / dar_d is an integer scale factor, and scale wd with the PAR */
1091 if (video_height % dar_d == 0) {
1092 GST_DEBUG_OBJECT (self, "keeping video height");
1093 GST_VIDEO_SINK_WIDTH (self) = (guint)
1094 gst_util_uint64_scale_int (video_height, dar_n, dar_d);
1095 GST_VIDEO_SINK_HEIGHT (self) = video_height;
1096 } else if (video_width % dar_n == 0) {
1097 GST_DEBUG_OBJECT (self, "keeping video width");
1098 GST_VIDEO_SINK_WIDTH (self) = video_width;
1099 GST_VIDEO_SINK_HEIGHT (self) = (guint)
1100 gst_util_uint64_scale_int (video_width, dar_d, dar_n);
1102 GST_DEBUG_OBJECT (self, "approximating while keeping video height");
1103 GST_VIDEO_SINK_WIDTH (self) = (guint)
1104 gst_util_uint64_scale_int (video_height, dar_n, dar_d);
1105 GST_VIDEO_SINK_HEIGHT (self) = video_height;
1109 GST_DEBUG_OBJECT (self, "scaling to %dx%d", GST_VIDEO_SINK_WIDTH (self),
1110 GST_VIDEO_SINK_HEIGHT (self));
1116 gst_kms_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
1120 GstBufferPool *newpool, *oldpool;
1122 self = GST_KMS_SINK (bsink);
1124 /* We are going to change the internal buffer pool, which means it will no
1125 * longer be compatbile with the last_buffer size. Drain now, as we won't be
1126 * able to do that later on. */
1127 gst_kms_sink_drain (self);
1129 if (!gst_video_info_from_caps (&vinfo, caps))
1130 goto invalid_format;
1132 if (!gst_kms_sink_calculate_display_ratio (self, &vinfo))
1135 if (GST_VIDEO_SINK_WIDTH (self) <= 0 || GST_VIDEO_SINK_HEIGHT (self) <= 0)
1138 /* create a new pool for the new configuration */
1139 newpool = gst_kms_sink_create_pool (self, caps, GST_VIDEO_INFO_SIZE (&vinfo),
1144 /* we don't activate the internal pool yet as it may not be needed */
1145 oldpool = self->pool;
1146 self->pool = newpool;
1149 gst_buffer_pool_set_active (oldpool, FALSE);
1150 gst_object_unref (oldpool);
1153 if (self->modesetting_enabled && !configure_mode_setting (self, &vinfo))
1154 goto modesetting_failed;
1156 self->vinfo = vinfo;
1158 GST_OBJECT_LOCK (self);
1159 if (self->reconfigure) {
1160 self->reconfigure = FALSE;
1161 self->render_rect = self->pending_rect;
1163 GST_OBJECT_UNLOCK (self);
1165 GST_DEBUG_OBJECT (self, "negotiated caps = %" GST_PTR_FORMAT, caps);
1172 GST_ERROR_OBJECT (self, "caps invalid");
1178 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1179 ("Invalid image size."));
1185 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1186 ("Error calculating the output display ratio of the video."));
1191 /* Already warned in create_pool */
1197 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1198 ("failed to configure video mode"));
1205 gst_kms_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
1211 GstBufferPool *pool;
1214 self = GST_KMS_SINK (bsink);
1216 gst_query_parse_allocation (query, &caps, &need_pool);
1219 if (!gst_video_info_from_caps (&vinfo, caps))
1222 size = GST_VIDEO_INFO_SIZE (&vinfo);
1226 pool = gst_kms_sink_create_pool (self, caps, size, 0);
1230 /* Only export for pool used upstream */
1231 if (self->has_prime_export) {
1232 GstStructure *config = gst_buffer_pool_get_config (pool);
1233 gst_buffer_pool_config_add_option (config,
1234 GST_BUFFER_POOL_OPTION_KMS_PRIME_EXPORT);
1235 gst_buffer_pool_set_config (pool, config);
1239 /* we need at least 2 buffer because we hold on to the last one */
1240 gst_query_add_allocation_pool (query, pool, size, 2, 0);
1242 gst_object_unref (pool);
1244 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1245 gst_query_add_allocation_meta (query, GST_VIDEO_CROP_META_API_TYPE, NULL);
1252 GST_DEBUG_OBJECT (bsink, "no caps specified");
1257 GST_DEBUG_OBJECT (bsink, "invalid caps specified");
1262 /* Already warned in create_pool */
1268 sync_handler (gint fd, guint frame, guint sec, guint usec, gpointer data)
1277 gst_kms_sink_sync (GstKMSSink * self)
1281 drmEventContext evctxt = {
1282 .version = DRM_EVENT_CONTEXT_VERSION,
1283 .page_flip_handler = sync_handler,
1284 .vblank_handler = sync_handler,
1288 .type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1290 .signal = (gulong) & waiting,
1294 if (self->pipe == 1)
1295 vbl.request.type |= DRM_VBLANK_SECONDARY;
1296 else if (self->pipe > 1)
1297 vbl.request.type |= self->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT;
1300 if (!self->has_async_page_flip && !self->modesetting_enabled) {
1301 ret = drmWaitVBlank (self->fd, &vbl);
1305 ret = drmModePageFlip (self->fd, self->crtc_id, self->buffer_id,
1306 DRM_MODE_PAGE_FLIP_EVENT, &waiting);
1308 goto pageflip_failed;
1313 ret = gst_poll_wait (self->poll, 3 * GST_SECOND);
1314 } while (ret == -1 && (errno == EAGAIN || errno == EINTR));
1316 ret = drmHandleEvent (self->fd, &evctxt);
1326 GST_WARNING_OBJECT (self, "drmWaitVBlank failed: %s (%d)",
1327 g_strerror (errno), errno);
1332 GST_WARNING_OBJECT (self, "drmModePageFlip failed: %s (%d)",
1333 g_strerror (errno), errno);
1338 GST_ERROR_OBJECT (self, "drmHandleEvent failed: %s (%d)",
1339 g_strerror (errno), errno);
1345 gst_kms_sink_import_dmabuf (GstKMSSink * self, GstBuffer * inbuf,
1346 GstBuffer ** outbuf)
1348 gint prime_fds[GST_VIDEO_MAX_PLANES] = { 0, };
1350 guint i, n_mem, n_planes;
1351 GstKMSMemory *kmsmem;
1352 guint mems_idx[GST_VIDEO_MAX_PLANES];
1353 gsize mems_skip[GST_VIDEO_MAX_PLANES];
1354 GstMemory *mems[GST_VIDEO_MAX_PLANES];
1356 if (!self->has_prime_import)
1359 /* This will eliminate most non-dmabuf out there */
1360 if (!gst_is_dmabuf_memory (gst_buffer_peek_memory (inbuf, 0)))
1363 n_planes = GST_VIDEO_INFO_N_PLANES (&self->vinfo);
1364 n_mem = gst_buffer_n_memory (inbuf);
1365 meta = gst_buffer_get_video_meta (inbuf);
1367 GST_TRACE_OBJECT (self, "Found a dmabuf with %u planes and %u memories",
1370 /* We cannot have multiple dmabuf per plane */
1371 if (n_mem > n_planes)
1373 g_assert (n_planes != 0);
1375 /* Update video info based on video meta */
1377 GST_VIDEO_INFO_WIDTH (&self->vinfo) = meta->width;
1378 GST_VIDEO_INFO_HEIGHT (&self->vinfo) = meta->height;
1380 for (i = 0; i < meta->n_planes; i++) {
1381 GST_VIDEO_INFO_PLANE_OFFSET (&self->vinfo, i) = meta->offset[i];
1382 GST_VIDEO_INFO_PLANE_STRIDE (&self->vinfo, i) = meta->stride[i];
1386 /* Find and validate all memories */
1387 for (i = 0; i < n_planes; i++) {
1390 if (!gst_buffer_find_memory (inbuf,
1391 GST_VIDEO_INFO_PLANE_OFFSET (&self->vinfo, i), 1,
1392 &mems_idx[i], &length, &mems_skip[i]))
1395 mems[i] = gst_buffer_peek_memory (inbuf, mems_idx[i]);
1397 /* adjust for memory offset, in case data does not
1398 * start from byte 0 in the dmabuf fd */
1399 mems_skip[i] += mems[i]->offset;
1401 /* And all memory found must be dmabuf */
1402 if (!gst_is_dmabuf_memory (mems[i]))
1406 kmsmem = (GstKMSMemory *) gst_kms_allocator_get_cached (mems[0]);
1408 GST_LOG_OBJECT (self, "found KMS mem %p in DMABuf mem %p with fb id = %d",
1409 kmsmem, mems[0], kmsmem->fb_id);
1413 for (i = 0; i < n_planes; i++)
1414 prime_fds[i] = gst_dmabuf_memory_get_fd (mems[i]);
1416 GST_LOG_OBJECT (self, "found these prime ids: %d, %d, %d, %d", prime_fds[0],
1417 prime_fds[1], prime_fds[2], prime_fds[3]);
1419 kmsmem = gst_kms_allocator_dmabuf_import (self->allocator,
1420 prime_fds, n_planes, mems_skip, &self->vinfo);
1424 GST_LOG_OBJECT (self, "setting KMS mem %p to DMABuf mem %p with fb id = %d",
1425 kmsmem, mems[0], kmsmem->fb_id);
1426 gst_kms_allocator_cache (self->allocator, mems[0], GST_MEMORY_CAST (kmsmem));
1429 *outbuf = gst_buffer_new ();
1432 gst_buffer_append_memory (*outbuf, gst_memory_ref (GST_MEMORY_CAST (kmsmem)));
1433 gst_buffer_add_parent_buffer_meta (*outbuf, inbuf);
1439 gst_kms_sink_copy_to_dumb_buffer (GstKMSSink * self, GstBuffer * inbuf)
1442 GstVideoFrame inframe, outframe;
1444 GstBuffer *buf = NULL;
1446 if (!gst_buffer_pool_set_active (self->pool, TRUE))
1447 goto activate_pool_failed;
1449 ret = gst_buffer_pool_acquire_buffer (self->pool, &buf, NULL);
1450 if (ret != GST_FLOW_OK)
1451 goto create_buffer_failed;
1453 if (!gst_video_frame_map (&inframe, &self->vinfo, inbuf, GST_MAP_READ))
1454 goto error_map_src_buffer;
1456 if (!gst_video_frame_map (&outframe, &self->vinfo, buf, GST_MAP_WRITE))
1457 goto error_map_dst_buffer;
1459 success = gst_video_frame_copy (&outframe, &inframe);
1460 gst_video_frame_unmap (&outframe);
1461 gst_video_frame_unmap (&inframe);
1463 goto error_copy_buffer;
1470 gst_buffer_unref (buf);
1475 activate_pool_failed:
1477 GST_ELEMENT_ERROR (self, STREAM, FAILED, ("failed to activate buffer pool"),
1478 ("failed to activate buffer pool"));
1481 create_buffer_failed:
1483 GST_ELEMENT_ERROR (self, STREAM, FAILED, ("allocation failed"),
1484 ("failed to create buffer"));
1489 GST_WARNING_OBJECT (self, "failed to upload buffer");
1492 error_map_dst_buffer:
1494 gst_video_frame_unmap (&inframe);
1497 error_map_src_buffer:
1499 GST_WARNING_OBJECT (self, "failed to map buffer");
1505 gst_kms_sink_get_input_buffer (GstKMSSink * self, GstBuffer * inbuf)
1508 GstBuffer *buf = NULL;
1510 mem = gst_buffer_peek_memory (inbuf, 0);
1514 if (gst_is_kms_memory (mem))
1515 return gst_buffer_ref (inbuf);
1517 if (gst_kms_sink_import_dmabuf (self, inbuf, &buf))
1520 GST_CAT_INFO_OBJECT (CAT_PERFORMANCE, self, "frame copy");
1521 buf = gst_kms_sink_copy_to_dumb_buffer (self, inbuf);
1524 /* Copy all the non-memory related metas, this way CropMeta will be
1525 * available upon GstVideoOverlay::expose calls. */
1527 gst_buffer_copy_into (buf, inbuf, GST_BUFFER_COPY_METADATA, 0, -1);
1532 static GstFlowReturn
1533 gst_kms_sink_show_frame (GstVideoSink * vsink, GstBuffer * buf)
1536 GstBuffer *buffer = NULL;
1539 GstVideoCropMeta *crop;
1540 GstVideoRectangle src = { 0, };
1541 GstVideoRectangle dst = { 0, };
1542 GstVideoRectangle result;
1545 self = GST_KMS_SINK (vsink);
1547 res = GST_FLOW_ERROR;
1550 buffer = gst_kms_sink_get_input_buffer (self, buf);
1551 else if (self->last_buffer)
1552 buffer = gst_buffer_ref (self->last_buffer);
1554 /* Make sure buf is not used accidentally */
1558 return GST_FLOW_ERROR;
1559 fb_id = gst_kms_memory_get_fb_id (gst_buffer_peek_memory (buffer, 0));
1561 goto buffer_invalid;
1563 GST_TRACE_OBJECT (self, "displaying fb %d", fb_id);
1565 GST_OBJECT_LOCK (self);
1566 if (self->modesetting_enabled) {
1567 self->buffer_id = fb_id;
1571 if ((crop = gst_buffer_get_video_crop_meta (buffer))) {
1572 GstVideoInfo vinfo = self->vinfo;
1573 vinfo.width = crop->width;
1574 vinfo.height = crop->height;
1576 if (!gst_kms_sink_calculate_display_ratio (self, &vinfo))
1583 src.w = GST_VIDEO_SINK_WIDTH (self);
1584 src.h = GST_VIDEO_SINK_HEIGHT (self);
1586 dst.w = self->render_rect.w;
1587 dst.h = self->render_rect.h;
1590 gst_video_sink_center_rect (src, dst, &result, self->can_scale);
1592 result.x += self->render_rect.x;
1593 result.y += self->render_rect.y;
1596 src.w = crop->width;
1597 src.h = crop->height;
1599 src.w = GST_VIDEO_INFO_WIDTH (&self->vinfo);
1600 src.h = GST_VIDEO_INFO_HEIGHT (&self->vinfo);
1603 /* handle out of screen case */
1604 if ((result.x + result.w) > self->hdisplay)
1605 result.w = self->hdisplay - result.x;
1607 if ((result.y + result.h) > self->vdisplay)
1608 result.h = self->vdisplay - result.y;
1610 if (result.w <= 0 || result.h <= 0) {
1611 GST_WARNING_OBJECT (self, "video is out of display range");
1615 /* to make sure it can be show when driver don't support scale */
1616 if (!self->can_scale) {
1621 GST_TRACE_OBJECT (self,
1622 "drmModeSetPlane at (%i,%i) %ix%i sourcing at (%i,%i) %ix%i",
1623 result.x, result.y, result.w, result.h, src.x, src.y, src.w, src.h);
1625 ret = drmModeSetPlane (self->fd, self->plane_id, self->crtc_id, fb_id, 0,
1626 result.x, result.y, result.w, result.h,
1627 /* source/cropping coordinates are given in Q16 */
1628 src.x << 16, src.y << 16, src.w << 16, src.h << 16);
1630 if (self->can_scale) {
1631 self->can_scale = FALSE;
1632 goto retry_set_plane;
1634 goto set_plane_failed;
1638 /* Wait for the previous frame to complete redraw */
1639 if (!gst_kms_sink_sync (self)) {
1640 GST_OBJECT_UNLOCK (self);
1644 if (buffer != self->last_buffer)
1645 gst_buffer_replace (&self->last_buffer, buffer);
1646 g_clear_pointer (&self->tmp_kmsmem, gst_memory_unref);
1648 GST_OBJECT_UNLOCK (self);
1652 gst_buffer_unref (buffer);
1658 GST_ERROR_OBJECT (self, "invalid buffer: it doesn't have a fb id");
1663 GST_OBJECT_UNLOCK (self);
1664 GST_DEBUG_OBJECT (self, "result = { %d, %d, %d, %d} / "
1665 "src = { %d, %d, %d %d } / dst = { %d, %d, %d %d }", result.x, result.y,
1666 result.w, result.h, src.x, src.y, src.w, src.h, dst.x, dst.y, dst.w,
1668 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
1669 (NULL), ("drmModeSetPlane failed: %s (%d)", g_strerror (errno), errno));
1674 GST_OBJECT_UNLOCK (self);
1675 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1676 ("Error calculating the output display ratio of the video."));
1682 gst_kms_sink_drain (GstKMSSink * self)
1684 GstParentBufferMeta *parent_meta;
1686 GST_DEBUG_OBJECT (self, "draining");
1688 if (!self->last_buffer)
1691 /* We only need to return the last_buffer if it depends on upstream buffer.
1692 * In this case, the last_buffer will have a GstParentBufferMeta set. */
1693 parent_meta = gst_buffer_get_parent_buffer_meta (self->last_buffer);
1695 GstBuffer *dumb_buf;
1696 dumb_buf = gst_kms_sink_copy_to_dumb_buffer (self, parent_meta->buffer);
1697 gst_kms_allocator_clear_cache (self->allocator);
1698 gst_kms_sink_show_frame (GST_VIDEO_SINK (self), dumb_buf);
1699 gst_buffer_unref (dumb_buf);
1704 gst_kms_sink_query (GstBaseSink * bsink, GstQuery * query)
1706 GstKMSSink *self = GST_KMS_SINK (bsink);
1708 switch (GST_QUERY_TYPE (query)) {
1709 case GST_QUERY_ALLOCATION:
1710 case GST_QUERY_DRAIN:
1712 gst_kms_sink_drain (self);
1719 return GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
1723 gst_kms_sink_set_property (GObject * object, guint prop_id,
1724 const GValue * value, GParamSpec * pspec)
1728 sink = GST_KMS_SINK (object);
1731 case PROP_DRIVER_NAME:
1732 g_free (sink->devname);
1733 sink->devname = g_value_dup_string (value);
1736 g_free (sink->bus_id);
1737 sink->bus_id = g_value_dup_string (value);
1739 case PROP_CONNECTOR_ID:
1740 sink->conn_id = g_value_get_int (value);
1743 sink->plane_id = g_value_get_int (value);
1745 case PROP_FORCE_MODESETTING:
1746 sink->modesetting_enabled = g_value_get_boolean (value);
1748 case PROP_RESTORE_CRTC:
1749 sink->restore_crtc = g_value_get_boolean (value);
1751 case PROP_CAN_SCALE:
1752 sink->can_scale = g_value_get_boolean (value);
1754 case PROP_CONNECTOR_PROPS:{
1755 const GstStructure *s = gst_value_get_structure (value);
1757 g_clear_pointer (&sink->connector_props, gst_structure_free);
1760 sink->connector_props = gst_structure_copy (s);
1764 case PROP_PLANE_PROPS:{
1765 const GstStructure *s = gst_value_get_structure (value);
1767 g_clear_pointer (&sink->plane_props, gst_structure_free);
1770 sink->plane_props = gst_structure_copy (s);
1775 if (!gst_video_overlay_set_property (object, PROP_N, prop_id, value))
1776 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1782 gst_kms_sink_get_property (GObject * object, guint prop_id,
1783 GValue * value, GParamSpec * pspec)
1787 sink = GST_KMS_SINK (object);
1790 case PROP_DRIVER_NAME:
1791 g_value_set_string (value, sink->devname);
1794 g_value_set_string (value, sink->bus_id);
1796 case PROP_CONNECTOR_ID:
1797 g_value_set_int (value, sink->conn_id);
1800 g_value_set_int (value, sink->plane_id);
1802 case PROP_FORCE_MODESETTING:
1803 g_value_set_boolean (value, sink->modesetting_enabled);
1805 case PROP_RESTORE_CRTC:
1806 g_value_set_boolean (value, sink->restore_crtc);
1808 case PROP_CAN_SCALE:
1809 g_value_set_boolean (value, sink->can_scale);
1811 case PROP_DISPLAY_WIDTH:
1812 GST_OBJECT_LOCK (sink);
1813 g_value_set_int (value, sink->hdisplay);
1814 GST_OBJECT_UNLOCK (sink);
1816 case PROP_DISPLAY_HEIGHT:
1817 GST_OBJECT_LOCK (sink);
1818 g_value_set_int (value, sink->vdisplay);
1819 GST_OBJECT_UNLOCK (sink);
1821 case PROP_CONNECTOR_PROPS:
1822 gst_value_set_structure (value, sink->connector_props);
1824 case PROP_PLANE_PROPS:
1825 gst_value_set_structure (value, sink->plane_props);
1828 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1834 gst_kms_sink_finalize (GObject * object)
1838 sink = GST_KMS_SINK (object);
1839 g_clear_pointer (&sink->devname, g_free);
1840 g_clear_pointer (&sink->bus_id, g_free);
1841 gst_poll_free (sink->poll);
1842 g_clear_pointer (&sink->connector_props, gst_structure_free);
1843 g_clear_pointer (&sink->plane_props, gst_structure_free);
1844 g_clear_pointer (&sink->tmp_kmsmem, gst_memory_unref);
1846 G_OBJECT_CLASS (parent_class)->finalize (object);
1850 gst_kms_sink_init (GstKMSSink * sink)
1854 sink->plane_id = -1;
1855 sink->can_scale = TRUE;
1856 gst_poll_fd_init (&sink->pollfd);
1857 sink->poll = gst_poll_new (TRUE);
1858 gst_video_info_init (&sink->vinfo);
1862 gst_kms_sink_class_init (GstKMSSinkClass * klass)
1864 GObjectClass *gobject_class;
1865 GstElementClass *element_class;
1866 GstBaseSinkClass *basesink_class;
1867 GstVideoSinkClass *videosink_class;
1870 gobject_class = G_OBJECT_CLASS (klass);
1871 element_class = GST_ELEMENT_CLASS (klass);
1872 basesink_class = GST_BASE_SINK_CLASS (klass);
1873 videosink_class = GST_VIDEO_SINK_CLASS (klass);
1875 gst_element_class_set_static_metadata (element_class, "KMS video sink",
1876 "Sink/Video", GST_PLUGIN_DESC, "Víctor Jáquez <vjaquez@igalia.com>");
1878 caps = gst_kms_sink_caps_template_fill ();
1879 gst_element_class_add_pad_template (element_class,
1880 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps));
1881 gst_caps_unref (caps);
1883 basesink_class->start = GST_DEBUG_FUNCPTR (gst_kms_sink_start);
1884 basesink_class->stop = GST_DEBUG_FUNCPTR (gst_kms_sink_stop);
1885 basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_kms_sink_set_caps);
1886 basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_kms_sink_get_caps);
1887 basesink_class->propose_allocation = gst_kms_sink_propose_allocation;
1888 basesink_class->query = gst_kms_sink_query;
1890 videosink_class->show_frame = gst_kms_sink_show_frame;
1892 gobject_class->finalize = gst_kms_sink_finalize;
1893 gobject_class->set_property = gst_kms_sink_set_property;
1894 gobject_class->get_property = gst_kms_sink_get_property;
1897 * kmssink:driver-name:
1899 * If you have a system with multiple GPUs, you can choose which GPU
1900 * to use setting the DRM device driver name. Otherwise, the first
1901 * one from an internal list is used.
1903 g_properties[PROP_DRIVER_NAME] = g_param_spec_string ("driver-name",
1904 "device name", "DRM device driver name", NULL,
1905 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1910 * If you have a system with multiple displays for the same driver-name,
1911 * you can choose which display to use by setting the DRM bus ID. Otherwise,
1912 * the driver decides which one.
1914 g_properties[PROP_BUS_ID] = g_param_spec_string ("bus-id",
1915 "Bus ID", "DRM bus ID", NULL,
1916 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1919 * kmssink:connector-id:
1921 * A GPU has several output connectors, for example: LVDS, VGA,
1922 * HDMI, etc. By default the first LVDS is tried, then the first
1923 * eDP, and at the end, the first connected one.
1925 g_properties[PROP_CONNECTOR_ID] = g_param_spec_int ("connector-id",
1926 "Connector ID", "DRM connector id", -1, G_MAXINT32, -1,
1927 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1932 * There could be several planes associated with a CRTC.
1933 * By default the first plane that's possible to use with a given
1936 g_properties[PROP_PLANE_ID] = g_param_spec_int ("plane-id",
1937 "Plane ID", "DRM plane id", -1, G_MAXINT32, -1,
1938 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1941 * kmssink:force-modesetting:
1943 * If the output connector is already active, the sink automatically uses an
1944 * overlay plane. Enforce mode setting in the kms sink and output to the
1945 * base plane to override the automatic behavior.
1947 g_properties[PROP_FORCE_MODESETTING] =
1948 g_param_spec_boolean ("force-modesetting", "Force modesetting",
1949 "When enabled, the sink try to configure the display mode", FALSE,
1950 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1953 * kmssink:restore-crtc:
1955 * Restore previous CRTC setting if new CRTC mode was set forcefully.
1956 * By default this is enabled if user set CRTC with a new mode on an already
1957 * active CRTC wich was having a valid mode.
1959 g_properties[PROP_RESTORE_CRTC] =
1960 g_param_spec_boolean ("restore-crtc", "Restore CRTC mode",
1961 "When enabled and CRTC was set with a new mode, previous CRTC mode will"
1962 "be restored when going to NULL state.", TRUE,
1963 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1966 * kmssink:can-scale:
1968 * User can tell kmssink if the driver can support scale.
1970 g_properties[PROP_CAN_SCALE] =
1971 g_param_spec_boolean ("can-scale", "can scale",
1972 "User can tell kmssink if the driver can support scale", TRUE,
1973 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1976 * kmssink:display-width
1978 * Actual width of the display. This is read only and only available in
1979 * PAUSED and PLAYING state. It's meant to be used with
1980 * gst_video_overlay_set_render_rectangle() function.
1982 g_properties[PROP_DISPLAY_WIDTH] =
1983 g_param_spec_int ("display-width", "Display Width",
1984 "Width of the display surface in pixels", 0, G_MAXINT, 0,
1985 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1988 * kmssink:display-height
1990 * Actual height of the display. This is read only and only available in
1991 * PAUSED and PLAYING state. It's meant to be used with
1992 * gst_video_overlay_set_render_rectangle() function.
1994 g_properties[PROP_DISPLAY_HEIGHT] =
1995 g_param_spec_int ("display-height", "Display Height",
1996 "Height of the display surface in pixels", 0, G_MAXINT, 0,
1997 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
2000 * kmssink:connector-properties:
2002 * Additional properties for the connector. Keys are strings and values
2003 * unsigned 64 bits integers.
2007 g_properties[PROP_CONNECTOR_PROPS] =
2008 g_param_spec_boxed ("connector-properties", "Connector Properties",
2009 "Additionnal properties for the connector",
2010 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2013 * kmssink:plane-properties:
2015 * Additional properties for the plane. Keys are strings and values
2016 * unsigned 64 bits integers.
2020 g_properties[PROP_PLANE_PROPS] =
2021 g_param_spec_boxed ("plane-properties", "Connector Plane",
2022 "Additionnal properties for the plane",
2023 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2025 g_object_class_install_properties (gobject_class, PROP_N, g_properties);
2027 gst_video_overlay_install_properties (gobject_class, PROP_N);
2031 plugin_init (GstPlugin * plugin)
2033 if (!gst_element_register (plugin, GST_PLUGIN_NAME, GST_RANK_SECONDARY,
2040 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, kms,
2041 GST_PLUGIN_DESC, plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,