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));
84 GST_ELEMENT_REGISTER_DEFINE (kmssink, GST_PLUGIN_NAME, GST_RANK_SECONDARY,
93 PROP_FORCE_MODESETTING,
104 static GParamSpec *g_properties[PROP_N] = { NULL, };
107 gst_kms_sink_set_render_rectangle (GstVideoOverlay * overlay,
108 gint x, gint y, gint width, gint height)
110 GstKMSSink *self = GST_KMS_SINK (overlay);
112 GST_DEBUG_OBJECT (self, "Setting render rectangle to (%d,%d) %dx%d", x, y,
115 GST_OBJECT_LOCK (self);
117 if (width == -1 && height == -1) {
120 width = self->hdisplay;
121 height = self->vdisplay;
124 if (width <= 0 || height <= 0)
127 self->pending_rect.x = x;
128 self->pending_rect.y = y;
129 self->pending_rect.w = width;
130 self->pending_rect.h = height;
132 if (self->can_scale ||
133 (self->render_rect.w == width && self->render_rect.h == height)) {
134 self->render_rect = self->pending_rect;
136 self->reconfigure = TRUE;
137 GST_DEBUG_OBJECT (self, "Waiting for new caps to apply render rectangle");
141 GST_OBJECT_UNLOCK (self);
145 gst_kms_sink_expose (GstVideoOverlay * overlay)
147 GstKMSSink *self = GST_KMS_SINK (overlay);
149 GST_DEBUG_OBJECT (overlay, "Expose called by application");
151 if (!self->can_scale) {
152 GST_OBJECT_LOCK (self);
153 if (self->reconfigure) {
154 GST_OBJECT_UNLOCK (self);
155 GST_DEBUG_OBJECT (overlay, "Sending a reconfigure event");
156 gst_pad_push_event (GST_BASE_SINK_PAD (self),
157 gst_event_new_reconfigure ());
159 GST_DEBUG_OBJECT (overlay, "Applying new render rectangle");
160 /* size of the rectangle does not change, only the (x,y) position changes */
161 self->render_rect = self->pending_rect;
162 GST_OBJECT_UNLOCK (self);
166 gst_kms_sink_show_frame (GST_VIDEO_SINK (self), NULL);
170 gst_kms_sink_video_overlay_init (GstVideoOverlayInterface * iface)
172 iface->expose = gst_kms_sink_expose;
173 iface->set_render_rectangle = gst_kms_sink_set_render_rectangle;
177 kms_open (gchar ** driver)
179 static const char *drivers[] = { "i915", "radeon", "nouveau", "vmwgfx",
180 "exynos", "amdgpu", "imx-drm", "rockchip", "atmel-hlcdc", "msm",
181 "xlnx", "vc4", "meson", "sun4i-drm", "mxsfb-drm", "tegra",
182 "xilinx_drm", /* DEPRECATED. Replaced by xlnx */
186 for (i = 0; i < G_N_ELEMENTS (drivers); i++) {
187 fd = drmOpen (drivers[i], NULL);
190 *driver = g_strdup (drivers[i]);
198 static drmModePlane *
199 find_plane_for_crtc (int fd, drmModeRes * res, drmModePlaneRes * pres,
207 for (i = 0; i < res->count_crtcs; i++) {
208 if (crtc_id == res->crtcs[i]) {
217 for (i = 0; i < pres->count_planes; i++) {
218 plane = drmModeGetPlane (fd, pres->planes[i]);
219 if (plane->possible_crtcs & (1 << pipe))
221 drmModeFreePlane (plane);
228 find_crtc_for_connector (int fd, drmModeRes * res, drmModeConnector * conn,
235 guint32 crtcs_for_connector = 0;
238 for (i = 0; i < res->count_encoders; i++) {
239 enc = drmModeGetEncoder (fd, res->encoders[i]);
241 if (enc->encoder_id == conn->encoder_id) {
242 crtc_id = enc->crtc_id;
243 drmModeFreeEncoder (enc);
246 drmModeFreeEncoder (enc);
250 /* If no active crtc was found, pick the first possible crtc */
252 for (i = 0; i < conn->count_encoders; i++) {
253 enc = drmModeGetEncoder (fd, conn->encoders[i]);
254 crtcs_for_connector |= enc->possible_crtcs;
255 drmModeFreeEncoder (enc);
258 if (crtcs_for_connector != 0)
259 crtc_id = res->crtcs[ffs (crtcs_for_connector) - 1];
265 for (i = 0; i < res->count_crtcs; i++) {
266 crtc = drmModeGetCrtc (fd, res->crtcs[i]);
268 if (crtc_id == crtc->crtc_id) {
273 drmModeFreeCrtc (crtc);
281 connector_is_used (int fd, drmModeRes * res, drmModeConnector * conn)
287 crtc = find_crtc_for_connector (fd, res, conn, NULL);
289 result = crtc->buffer_id != 0;
290 drmModeFreeCrtc (crtc);
296 static drmModeConnector *
297 find_used_connector_by_type (int fd, drmModeRes * res, int type)
300 drmModeConnector *conn;
303 for (i = 0; i < res->count_connectors; i++) {
304 conn = drmModeGetConnector (fd, res->connectors[i]);
306 if ((conn->connector_type == type) && connector_is_used (fd, res, conn))
308 drmModeFreeConnector (conn);
315 static drmModeConnector *
316 find_first_used_connector (int fd, drmModeRes * res)
319 drmModeConnector *conn;
322 for (i = 0; i < res->count_connectors; i++) {
323 conn = drmModeGetConnector (fd, res->connectors[i]);
325 if (connector_is_used (fd, res, conn))
327 drmModeFreeConnector (conn);
334 static drmModeConnector *
335 find_main_monitor (int fd, drmModeRes * res)
337 /* Find the LVDS and eDP connectors: those are the main screens. */
338 static const int priority[] = { DRM_MODE_CONNECTOR_LVDS,
339 DRM_MODE_CONNECTOR_eDP
342 drmModeConnector *conn;
345 for (i = 0; !conn && i < G_N_ELEMENTS (priority); i++)
346 conn = find_used_connector_by_type (fd, res, priority[i]);
348 /* if we didn't find a connector, grab the first one in use */
350 conn = find_first_used_connector (fd, res);
352 /* if no connector is used, grab the first one */
354 conn = drmModeGetConnector (fd, res->connectors[0]);
360 log_drm_version (GstKMSSink * self)
362 #ifndef GST_DISABLE_GST_DEBUG
365 v = drmGetVersion (self->fd);
367 GST_INFO_OBJECT (self, "DRM v%d.%d.%d [%s — %s — %s]", v->version_major,
368 v->version_minor, v->version_patchlevel, GST_STR_NULL (v->name),
369 GST_STR_NULL (v->desc), GST_STR_NULL (v->date));
372 GST_WARNING_OBJECT (self, "could not get driver information: %s",
373 GST_STR_NULL (self->devname));
380 get_drm_caps (GstKMSSink * self)
383 guint64 has_dumb_buffer;
385 guint64 has_async_page_flip;
388 ret = drmGetCap (self->fd, DRM_CAP_DUMB_BUFFER, &has_dumb_buffer);
390 GST_WARNING_OBJECT (self, "could not get dumb buffer capability");
391 if (has_dumb_buffer == 0) {
392 GST_ERROR_OBJECT (self, "driver cannot handle dumb buffers");
397 ret = drmGetCap (self->fd, DRM_CAP_PRIME, &has_prime);
399 GST_WARNING_OBJECT (self, "could not get prime capability");
401 self->has_prime_import = (gboolean) (has_prime & DRM_PRIME_CAP_IMPORT);
402 self->has_prime_export = (gboolean) (has_prime & DRM_PRIME_CAP_EXPORT);
405 has_async_page_flip = 0;
406 ret = drmGetCap (self->fd, DRM_CAP_ASYNC_PAGE_FLIP, &has_async_page_flip);
408 GST_WARNING_OBJECT (self, "could not get async page flip capability");
410 self->has_async_page_flip = (gboolean) has_async_page_flip;
412 GST_INFO_OBJECT (self,
413 "prime import (%s) / prime export (%s) / async page flip (%s)",
414 self->has_prime_import ? "✓" : "✗",
415 self->has_prime_export ? "✓" : "✗",
416 self->has_async_page_flip ? "✓" : "✗");
422 ensure_kms_allocator (GstKMSSink * self)
426 self->allocator = gst_kms_allocator_new (self->fd);
430 configure_mode_setting (GstKMSSink * self, GstVideoInfo * vinfo)
433 drmModeConnector *conn;
436 drmModeModeInfo *mode;
438 GstKMSMemory *kmsmem;
445 if (self->conn_id < 0)
448 GST_INFO_OBJECT (self, "configuring mode setting");
450 ensure_kms_allocator (self);
451 kmsmem = (GstKMSMemory *) gst_kms_allocator_bo_alloc (self->allocator, vinfo);
454 fb_id = kmsmem->fb_id;
456 conn = drmModeGetConnector (self->fd, self->conn_id);
458 goto connector_failed;
460 for (i = 0; i < conn->count_modes; i++) {
461 if (conn->modes[i].vdisplay == GST_VIDEO_INFO_HEIGHT (vinfo) &&
462 conn->modes[i].hdisplay == GST_VIDEO_INFO_WIDTH (vinfo)) {
463 mode = &conn->modes[i];
470 err = drmModeSetCrtc (self->fd, self->crtc_id, fb_id, 0, 0,
471 (uint32_t *) & self->conn_id, 1, mode);
473 goto modesetting_failed;
475 g_clear_pointer (&self->tmp_kmsmem, gst_memory_unref);
476 self->tmp_kmsmem = (GstMemory *) kmsmem;
482 drmModeFreeConnector (conn);
489 GST_ERROR_OBJECT (self,
490 "failed to allocate buffer object for mode setting");
495 GST_ERROR_OBJECT (self, "Could not find a valid monitor connector");
500 GST_ERROR_OBJECT (self, "cannot find appropriate mode");
505 GST_ERROR_OBJECT (self, "Failed to set mode: %s", g_strerror (errno));
511 ensure_allowed_caps (GstKMSSink * self, drmModeConnector * conn,
512 drmModePlane * plane, drmModeRes * res)
514 GstCaps *out_caps, *tmp_caps, *caps;
518 drmModeModeInfo *mode;
521 if (self->allowed_caps)
524 out_caps = gst_caps_new_empty ();
528 if (conn && self->modesetting_enabled)
529 count_modes = conn->count_modes;
533 for (i = 0; i < count_modes; i++) {
534 tmp_caps = gst_caps_new_empty ();
539 if (conn && self->modesetting_enabled)
540 mode = &conn->modes[i];
542 for (j = 0; j < plane->count_formats; j++) {
543 fmt = gst_video_format_from_drm (plane->formats[j]);
544 if (fmt == GST_VIDEO_FORMAT_UNKNOWN) {
545 GST_INFO_OBJECT (self, "ignoring format %" GST_FOURCC_FORMAT,
546 GST_FOURCC_ARGS (plane->formats[j]));
550 format = gst_video_format_to_string (fmt);
553 caps = gst_caps_new_simple ("video/x-raw",
554 "format", G_TYPE_STRING, format,
555 "width", G_TYPE_INT, mode->hdisplay,
556 "height", G_TYPE_INT, mode->vdisplay,
557 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
559 caps = gst_caps_new_simple ("video/x-raw",
560 "format", G_TYPE_STRING, format,
561 "width", GST_TYPE_INT_RANGE, res->min_width, res->max_width,
562 "height", GST_TYPE_INT_RANGE, res->min_height, res->max_height,
563 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
568 tmp_caps = gst_caps_merge (tmp_caps, caps);
571 out_caps = gst_caps_merge (out_caps, gst_caps_simplify (tmp_caps));
574 if (gst_caps_is_empty (out_caps)) {
575 GST_DEBUG_OBJECT (self, "allowed caps is empty");
576 gst_caps_unref (out_caps);
580 self->allowed_caps = gst_caps_simplify (out_caps);
582 GST_DEBUG_OBJECT (self, "allowed caps = %" GST_PTR_FORMAT,
589 set_drm_property (gint fd, guint32 object, guint32 object_type,
590 drmModeObjectPropertiesPtr properties, const gchar * prop_name,
594 gboolean ret = FALSE;
596 for (i = 0; i < properties->count_props && !ret; i++) {
597 drmModePropertyPtr property;
599 property = drmModeGetProperty (fd, properties->props[i]);
601 /* GstStructure parser limits the set of supported character, so we
602 * replace the invalid characters with '-'. In DRM, this is generally
603 * replacing spaces into '-'. */
604 g_strcanon (property->name, G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "_",
607 GST_LOG ("found property %s (looking for %s)", property->name, prop_name);
609 if (!strcmp (property->name, prop_name)) {
610 drmModeObjectSetProperty (fd, object, object_type,
611 property->prop_id, value);
614 drmModeFreeProperty (property);
623 drmModeObjectPropertiesPtr properties;
626 const gchar *obj_type_str;
630 set_obj_prop (GQuark field_id, const GValue * value, gpointer user_data)
632 SetPropsIter *iter = user_data;
633 GstKMSSink *self = iter->self;
637 name = g_quark_to_string (field_id);
639 if (G_VALUE_HOLDS (value, G_TYPE_INT))
640 v = g_value_get_int (value);
641 else if (G_VALUE_HOLDS (value, G_TYPE_UINT))
642 v = g_value_get_uint (value);
643 else if (G_VALUE_HOLDS (value, G_TYPE_INT64))
644 v = g_value_get_int64 (value);
645 else if (G_VALUE_HOLDS (value, G_TYPE_UINT64))
646 v = g_value_get_uint64 (value);
648 GST_WARNING_OBJECT (self,
649 "'uint64' value expected for control '%s'.", name);
653 if (set_drm_property (self->fd, iter->obj_id, iter->obj_type,
654 iter->properties, name, v)) {
655 GST_DEBUG_OBJECT (self,
656 "Set %s property '%s' to %" G_GUINT64_FORMAT,
657 iter->obj_type_str, name, v);
659 GST_WARNING_OBJECT (self,
660 "Failed to set %s property '%s' to %" G_GUINT64_FORMAT,
661 iter->obj_type_str, name, v);
668 gst_kms_sink_update_properties (SetPropsIter * iter, GstStructure * props)
670 GstKMSSink *self = iter->self;
672 iter->properties = drmModeObjectGetProperties (self->fd, iter->obj_id,
675 gst_structure_foreach (props, set_obj_prop, iter);
677 drmModeFreeObjectProperties (iter->properties);
681 gst_kms_sink_update_connector_properties (GstKMSSink * self)
685 if (!self->connector_props)
689 iter.obj_id = self->conn_id;
690 iter.obj_type = DRM_MODE_OBJECT_CONNECTOR;
691 iter.obj_type_str = "connector";
693 gst_kms_sink_update_properties (&iter, self->connector_props);
697 gst_kms_sink_update_plane_properties (GstKMSSink * self)
701 if (!self->plane_props)
705 iter.obj_id = self->plane_id;
706 iter.obj_type = DRM_MODE_OBJECT_PLANE;
707 iter.obj_type_str = "plane";
709 gst_kms_sink_update_properties (&iter, self->plane_props);
713 gst_kms_sink_start (GstBaseSink * bsink)
717 drmModeConnector *conn;
719 drmModePlaneRes *pres;
721 gboolean universal_planes;
724 self = GST_KMS_SINK (bsink);
725 universal_planes = FALSE;
733 /* open our own internal device fd if application did not supply its own */
734 if (self->is_internal_fd) {
735 if (self->devname || self->bus_id)
736 self->fd = drmOpen (self->devname, self->bus_id);
738 self->fd = kms_open (&self->devname);
744 log_drm_version (self);
745 if (!get_drm_caps (self))
748 res = drmModeGetResources (self->fd);
750 goto resources_failed;
752 if (self->conn_id == -1)
753 conn = find_main_monitor (self->fd, res);
755 conn = drmModeGetConnector (self->fd, self->conn_id);
757 goto connector_failed;
759 crtc = find_crtc_for_connector (self->fd, res, conn, &self->pipe);
763 if (!crtc->mode_valid || self->modesetting_enabled) {
764 GST_DEBUG_OBJECT (self, "enabling modesetting");
765 self->modesetting_enabled = TRUE;
766 universal_planes = TRUE;
769 if (crtc->mode_valid && self->modesetting_enabled && self->restore_crtc) {
770 self->saved_crtc = (drmModeCrtc *) crtc;
774 if (universal_planes &&
775 drmSetClientCap (self->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1))
778 pres = drmModeGetPlaneResources (self->fd);
780 goto plane_resources_failed;
782 if (self->plane_id == -1)
783 plane = find_plane_for_crtc (self->fd, res, pres, crtc->crtc_id);
785 plane = drmModeGetPlane (self->fd, self->plane_id);
789 if (!ensure_allowed_caps (self, conn, plane, res))
790 goto allowed_caps_failed;
792 self->conn_id = conn->connector_id;
793 self->crtc_id = crtc->crtc_id;
794 self->plane_id = plane->plane_id;
796 GST_INFO_OBJECT (self, "connector id = %d / crtc id = %d / plane id = %d",
797 self->conn_id, self->crtc_id, self->plane_id);
799 GST_OBJECT_LOCK (self);
800 self->hdisplay = crtc->mode.hdisplay;
801 self->vdisplay = crtc->mode.vdisplay;
803 if (self->render_rect.w == 0 || self->render_rect.h == 0) {
804 self->render_rect.x = 0;
805 self->render_rect.y = 0;
806 self->render_rect.w = self->hdisplay;
807 self->render_rect.h = self->vdisplay;
810 self->pending_rect = self->render_rect;
811 GST_OBJECT_UNLOCK (self);
813 self->buffer_id = crtc->buffer_id;
815 self->mm_width = conn->mmWidth;
816 self->mm_height = conn->mmHeight;
818 GST_INFO_OBJECT (self, "display size: pixels = %dx%d / millimeters = %dx%d",
819 self->hdisplay, self->vdisplay, self->mm_width, self->mm_height);
821 self->pollfd.fd = self->fd;
822 gst_poll_add_fd (self->poll, &self->pollfd);
823 gst_poll_fd_ctl_read (self->poll, &self->pollfd, TRUE);
825 g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_WIDTH]);
826 g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_HEIGHT]);
828 gst_kms_sink_update_connector_properties (self);
829 gst_kms_sink_update_plane_properties (self);
835 drmModeFreePlane (plane);
837 drmModeFreePlaneResources (pres);
838 if (crtc != self->saved_crtc)
839 drmModeFreeCrtc (crtc);
841 drmModeFreeConnector (conn);
843 drmModeFreeResources (res);
845 if (!ret && self->fd >= 0) {
846 if (self->is_internal_fd)
856 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ_WRITE,
857 ("Could not open DRM module %s", GST_STR_NULL (self->devname)),
858 ("reason: %s (%d)", g_strerror (errno), errno));
864 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
865 ("drmModeGetResources failed"),
866 ("reason: %s (%d)", g_strerror (errno), errno));
872 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
873 ("Could not find a valid monitor connector"), (NULL));
879 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
880 ("Could not find a crtc for connector"), (NULL));
886 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
887 ("Could not set universal planes capability bit"), (NULL));
891 plane_resources_failed:
893 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
894 ("drmModeGetPlaneResources failed"),
895 ("reason: %s (%d)", g_strerror (errno), errno));
901 if (universal_planes) {
902 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
903 ("Could not find a plane for crtc"), (NULL));
906 universal_planes = TRUE;
907 goto retry_find_plane;
913 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
914 ("Could not get allowed GstCaps of device"),
915 ("driver does not provide mode settings configuration"));
921 gst_kms_sink_stop (GstBaseSink * bsink)
926 self = GST_KMS_SINK (bsink);
929 gst_kms_allocator_clear_cache (self->allocator);
931 gst_buffer_replace (&self->last_buffer, NULL);
932 gst_caps_replace (&self->allowed_caps, NULL);
933 gst_object_replace ((GstObject **) & self->pool, NULL);
934 gst_object_replace ((GstObject **) & self->allocator, NULL);
936 gst_poll_remove_fd (self->poll, &self->pollfd);
937 gst_poll_restart (self->poll);
938 gst_poll_fd_init (&self->pollfd);
940 if (self->saved_crtc) {
941 drmModeCrtc *crtc = (drmModeCrtc *) self->saved_crtc;
943 err = drmModeSetCrtc (self->fd, crtc->crtc_id, crtc->buffer_id, crtc->x,
944 crtc->y, (uint32_t *) & self->conn_id, 1, &crtc->mode);
946 GST_ERROR_OBJECT (self, "Failed to restore previous CRTC mode: %s",
949 drmModeFreeCrtc (crtc);
950 self->saved_crtc = NULL;
954 if (self->is_internal_fd)
959 GST_OBJECT_LOCK (bsink);
962 self->pending_rect.x = 0;
963 self->pending_rect.y = 0;
964 self->pending_rect.w = 0;
965 self->pending_rect.h = 0;
966 self->render_rect = self->pending_rect;
967 GST_OBJECT_UNLOCK (bsink);
969 g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_WIDTH]);
970 g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_HEIGHT]);
976 gst_kms_sink_get_allowed_caps (GstKMSSink * self)
978 if (!self->allowed_caps)
979 return NULL; /* base class will return the template caps */
980 return gst_caps_ref (self->allowed_caps);
984 gst_kms_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
987 GstCaps *caps, *out_caps;
989 guint dpy_par_n, dpy_par_d;
991 self = GST_KMS_SINK (bsink);
993 caps = gst_kms_sink_get_allowed_caps (self);
997 GST_OBJECT_LOCK (self);
999 if (!self->can_scale) {
1000 out_caps = gst_caps_new_empty ();
1001 gst_video_calculate_device_ratio (self->hdisplay, self->vdisplay,
1002 self->mm_width, self->mm_height, &dpy_par_n, &dpy_par_d);
1004 s = gst_structure_copy (gst_caps_get_structure (caps, 0));
1005 gst_structure_set (s, "width", G_TYPE_INT, self->pending_rect.w,
1006 "height", G_TYPE_INT, self->pending_rect.h,
1007 "pixel-aspect-ratio", GST_TYPE_FRACTION, dpy_par_n, dpy_par_d, NULL);
1009 gst_caps_append_structure (out_caps, s);
1011 out_caps = gst_caps_merge (out_caps, caps);
1014 /* enforce our display aspect ratio */
1015 gst_caps_set_simple (out_caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1016 dpy_par_n, dpy_par_d, NULL);
1018 out_caps = gst_caps_make_writable (caps);
1022 GST_OBJECT_UNLOCK (self);
1024 GST_DEBUG_OBJECT (self, "Proposing caps %" GST_PTR_FORMAT, out_caps);
1028 out_caps = gst_caps_intersect_full (caps, filter, GST_CAPS_INTERSECT_FIRST);
1029 gst_caps_unref (caps);
1035 static GstBufferPool *
1036 gst_kms_sink_create_pool (GstKMSSink * self, GstCaps * caps, gsize size,
1039 GstBufferPool *pool;
1040 GstStructure *config;
1042 pool = gst_kms_buffer_pool_new ();
1046 config = gst_buffer_pool_get_config (pool);
1047 gst_buffer_pool_config_set_params (config, caps, size, min, 0);
1048 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
1050 ensure_kms_allocator (self);
1051 gst_buffer_pool_config_set_allocator (config, self->allocator, NULL);
1053 if (!gst_buffer_pool_set_config (pool, config))
1061 GST_ERROR_OBJECT (self, "failed to create buffer pool");
1066 GST_ERROR_OBJECT (self, "failed to set config");
1067 gst_object_unref (pool);
1073 gst_kms_sink_calculate_display_ratio (GstKMSSink * self, GstVideoInfo * vinfo,
1074 gint * scaled_width, gint * scaled_height)
1077 guint video_width, video_height;
1078 guint video_par_n, video_par_d;
1079 guint dpy_par_n, dpy_par_d;
1081 video_width = GST_VIDEO_INFO_WIDTH (vinfo);
1082 video_height = GST_VIDEO_INFO_HEIGHT (vinfo);
1083 video_par_n = GST_VIDEO_INFO_PAR_N (vinfo);
1084 video_par_d = GST_VIDEO_INFO_PAR_D (vinfo);
1086 if (self->can_scale) {
1087 gst_video_calculate_device_ratio (self->hdisplay, self->vdisplay,
1088 self->mm_width, self->mm_height, &dpy_par_n, &dpy_par_d);
1090 *scaled_width = video_width;
1091 *scaled_height = video_height;
1095 if (!gst_video_calculate_display_ratio (&dar_n, &dar_d, video_width,
1096 video_height, video_par_n, video_par_d, dpy_par_n, dpy_par_d))
1099 GST_DEBUG_OBJECT (self, "video calculated display ratio: %d/%d", dar_n,
1102 /* now find a width x height that respects this display ratio.
1103 * prefer those that have one of w/h the same as the incoming video
1104 * using wd / hd = dar_n / dar_d */
1106 /* start with same height, because of interlaced video */
1107 /* check hd / dar_d is an integer scale factor, and scale wd with the PAR */
1108 if (video_height % dar_d == 0) {
1109 GST_DEBUG_OBJECT (self, "keeping video height");
1110 *scaled_width = (guint)
1111 gst_util_uint64_scale_int (video_height, dar_n, dar_d);
1112 *scaled_height = video_height;
1113 } else if (video_width % dar_n == 0) {
1114 GST_DEBUG_OBJECT (self, "keeping video width");
1115 *scaled_width = video_width;
1116 *scaled_height = (guint)
1117 gst_util_uint64_scale_int (video_width, dar_d, dar_n);
1119 GST_DEBUG_OBJECT (self, "approximating while keeping video height");
1120 *scaled_width = (guint)
1121 gst_util_uint64_scale_int (video_height, dar_n, dar_d);
1122 *scaled_height = video_height;
1126 GST_DEBUG_OBJECT (self, "scaling to %dx%d", *scaled_width, *scaled_height);
1132 gst_kms_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
1137 self = GST_KMS_SINK (bsink);
1139 if (!gst_video_info_from_caps (&vinfo, caps))
1140 goto invalid_format;
1141 self->vinfo = vinfo;
1143 if (!gst_kms_sink_calculate_display_ratio (self, &vinfo,
1144 &GST_VIDEO_SINK_WIDTH (self), &GST_VIDEO_SINK_HEIGHT (self)))
1147 if (GST_VIDEO_SINK_WIDTH (self) <= 0 || GST_VIDEO_SINK_HEIGHT (self) <= 0)
1150 /* discard dumb buffer pool */
1152 gst_buffer_pool_set_active (self->pool, FALSE);
1153 gst_object_unref (self->pool);
1157 if (self->modesetting_enabled && !configure_mode_setting (self, &vinfo))
1158 goto modesetting_failed;
1160 GST_OBJECT_LOCK (self);
1161 if (self->reconfigure) {
1162 self->reconfigure = FALSE;
1163 self->render_rect = self->pending_rect;
1165 GST_OBJECT_UNLOCK (self);
1167 GST_DEBUG_OBJECT (self, "negotiated caps = %" GST_PTR_FORMAT, caps);
1174 GST_ERROR_OBJECT (self, "caps invalid");
1180 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1181 ("Invalid image size."));
1187 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1188 ("Error calculating the output display ratio of the video."));
1194 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1195 ("failed to configure video mode"));
1202 gst_kms_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
1208 GstBufferPool *pool;
1211 self = GST_KMS_SINK (bsink);
1213 GST_DEBUG_OBJECT (self, "propose allocation");
1215 gst_query_parse_allocation (query, &caps, &need_pool);
1218 if (!gst_video_info_from_caps (&vinfo, caps))
1221 size = GST_VIDEO_INFO_SIZE (&vinfo);
1225 pool = gst_kms_sink_create_pool (self, caps, size, 0);
1229 /* Only export for pool used upstream */
1230 if (self->has_prime_export) {
1231 GstStructure *config = gst_buffer_pool_get_config (pool);
1232 gst_buffer_pool_config_add_option (config,
1233 GST_BUFFER_POOL_OPTION_KMS_PRIME_EXPORT);
1234 gst_buffer_pool_set_config (pool, config);
1238 /* we need at least 2 buffer because we hold on to the last one */
1239 gst_query_add_allocation_pool (query, pool, size, 2, 0);
1241 gst_object_unref (pool);
1243 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1244 gst_query_add_allocation_meta (query, GST_VIDEO_CROP_META_API_TYPE, NULL);
1251 GST_DEBUG_OBJECT (bsink, "no caps specified");
1256 GST_DEBUG_OBJECT (bsink, "invalid caps specified");
1261 /* Already warned in create_pool */
1267 sync_handler (gint fd, guint frame, guint sec, guint usec, gpointer data)
1276 gst_kms_sink_sync (GstKMSSink * self)
1280 drmEventContext evctxt = {
1281 .version = DRM_EVENT_CONTEXT_VERSION,
1282 .page_flip_handler = sync_handler,
1283 .vblank_handler = sync_handler,
1287 .type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1289 .signal = (gulong) & waiting,
1293 if (self->pipe == 1)
1294 vbl.request.type |= DRM_VBLANK_SECONDARY;
1295 else if (self->pipe > 1)
1296 vbl.request.type |= self->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT;
1299 if (!self->has_async_page_flip && !self->modesetting_enabled) {
1300 ret = drmWaitVBlank (self->fd, &vbl);
1304 ret = drmModePageFlip (self->fd, self->crtc_id, self->buffer_id,
1305 DRM_MODE_PAGE_FLIP_EVENT, &waiting);
1307 goto pageflip_failed;
1312 ret = gst_poll_wait (self->poll, 3 * GST_SECOND);
1313 } while (ret == -1 && (errno == EAGAIN || errno == EINTR));
1315 ret = drmHandleEvent (self->fd, &evctxt);
1325 GST_WARNING_OBJECT (self, "drmWaitVBlank failed: %s (%d)",
1326 g_strerror (errno), errno);
1331 GST_WARNING_OBJECT (self, "drmModePageFlip failed: %s (%d)",
1332 g_strerror (errno), errno);
1337 GST_ERROR_OBJECT (self, "drmHandleEvent failed: %s (%d)",
1338 g_strerror (errno), errno);
1344 gst_kms_sink_import_dmabuf (GstKMSSink * self, GstBuffer * inbuf,
1345 GstBuffer ** outbuf)
1347 gint prime_fds[GST_VIDEO_MAX_PLANES] = { 0, };
1349 guint i, n_mem, n_planes;
1350 GstKMSMemory *kmsmem;
1351 guint mems_idx[GST_VIDEO_MAX_PLANES];
1352 gsize mems_skip[GST_VIDEO_MAX_PLANES];
1353 GstMemory *mems[GST_VIDEO_MAX_PLANES];
1355 if (!self->has_prime_import)
1358 /* This will eliminate most non-dmabuf out there */
1359 if (!gst_is_dmabuf_memory (gst_buffer_peek_memory (inbuf, 0)))
1362 n_planes = GST_VIDEO_INFO_N_PLANES (&self->vinfo);
1363 n_mem = gst_buffer_n_memory (inbuf);
1364 meta = gst_buffer_get_video_meta (inbuf);
1366 GST_TRACE_OBJECT (self, "Found a dmabuf with %u planes and %u memories",
1369 /* We cannot have multiple dmabuf per plane */
1370 if (n_mem > n_planes)
1372 g_assert (n_planes != 0);
1374 /* Update video info based on video meta */
1376 GST_VIDEO_INFO_WIDTH (&self->vinfo) = meta->width;
1377 GST_VIDEO_INFO_HEIGHT (&self->vinfo) = meta->height;
1379 for (i = 0; i < meta->n_planes; i++) {
1380 GST_VIDEO_INFO_PLANE_OFFSET (&self->vinfo, i) = meta->offset[i];
1381 GST_VIDEO_INFO_PLANE_STRIDE (&self->vinfo, i) = meta->stride[i];
1385 /* Find and validate all memories */
1386 for (i = 0; i < n_planes; i++) {
1389 if (!gst_buffer_find_memory (inbuf,
1390 GST_VIDEO_INFO_PLANE_OFFSET (&self->vinfo, i), 1,
1391 &mems_idx[i], &length, &mems_skip[i]))
1394 mems[i] = gst_buffer_peek_memory (inbuf, mems_idx[i]);
1396 /* adjust for memory offset, in case data does not
1397 * start from byte 0 in the dmabuf fd */
1398 mems_skip[i] += mems[i]->offset;
1400 /* And all memory found must be dmabuf */
1401 if (!gst_is_dmabuf_memory (mems[i]))
1405 ensure_kms_allocator (self);
1407 kmsmem = (GstKMSMemory *) gst_kms_allocator_get_cached (mems[0]);
1409 GST_LOG_OBJECT (self, "found KMS mem %p in DMABuf mem %p with fb id = %d",
1410 kmsmem, mems[0], kmsmem->fb_id);
1414 for (i = 0; i < n_planes; i++)
1415 prime_fds[i] = gst_dmabuf_memory_get_fd (mems[i]);
1417 GST_LOG_OBJECT (self, "found these prime ids: %d, %d, %d, %d", prime_fds[0],
1418 prime_fds[1], prime_fds[2], prime_fds[3]);
1420 kmsmem = gst_kms_allocator_dmabuf_import (self->allocator,
1421 prime_fds, n_planes, mems_skip, &self->vinfo);
1425 GST_LOG_OBJECT (self, "setting KMS mem %p to DMABuf mem %p with fb id = %d",
1426 kmsmem, mems[0], kmsmem->fb_id);
1427 gst_kms_allocator_cache (self->allocator, mems[0], GST_MEMORY_CAST (kmsmem));
1430 *outbuf = gst_buffer_new ();
1433 gst_buffer_append_memory (*outbuf, gst_memory_ref (GST_MEMORY_CAST (kmsmem)));
1434 gst_buffer_add_parent_buffer_meta (*outbuf, inbuf);
1440 ensure_internal_pool (GstKMSSink * self, GstVideoInfo * in_vinfo,
1443 GstBufferPool *pool;
1444 GstVideoInfo vinfo = *in_vinfo;
1445 GstVideoMeta *vmeta;
1451 /* When cropping, the caps matches the cropped rectangle width/height, but
1452 * we can retrieve the padded width/height from the VideoMeta (which is kept
1453 * intact when adding crop meta */
1454 if ((vmeta = gst_buffer_get_video_meta (inbuf))) {
1455 vinfo.width = vmeta->width;
1456 vinfo.height = vmeta->height;
1459 caps = gst_video_info_to_caps (&vinfo);
1460 pool = gst_kms_sink_create_pool (self, caps, gst_buffer_get_size (inbuf), 2);
1461 gst_caps_unref (caps);
1466 if (!gst_buffer_pool_set_active (pool, TRUE))
1467 goto activate_pool_failed;
1472 activate_pool_failed:
1474 GST_ELEMENT_ERROR (self, STREAM, FAILED, ("failed to activate buffer pool"),
1475 ("failed to activate buffer pool"));
1476 gst_object_unref (pool);
1483 gst_kms_sink_copy_to_dumb_buffer (GstKMSSink * self, GstVideoInfo * vinfo,
1487 GstVideoFrame inframe, outframe;
1489 GstBuffer *buf = NULL;
1491 if (!ensure_internal_pool (self, vinfo, inbuf))
1494 ret = gst_buffer_pool_acquire_buffer (self->pool, &buf, NULL);
1495 if (ret != GST_FLOW_OK)
1496 goto create_buffer_failed;
1498 if (!gst_video_frame_map (&inframe, vinfo, inbuf, GST_MAP_READ))
1499 goto error_map_src_buffer;
1501 if (!gst_video_frame_map (&outframe, vinfo, buf, GST_MAP_WRITE))
1502 goto error_map_dst_buffer;
1504 success = gst_video_frame_copy (&outframe, &inframe);
1505 gst_video_frame_unmap (&outframe);
1506 gst_video_frame_unmap (&inframe);
1508 goto error_copy_buffer;
1515 gst_buffer_unref (buf);
1520 create_buffer_failed:
1522 GST_ELEMENT_ERROR (self, STREAM, FAILED, ("allocation failed"),
1523 ("failed to create buffer"));
1528 GST_WARNING_OBJECT (self, "failed to upload buffer");
1531 error_map_dst_buffer:
1533 gst_video_frame_unmap (&inframe);
1536 error_map_src_buffer:
1538 GST_WARNING_OBJECT (self, "failed to map buffer");
1544 gst_kms_sink_get_input_buffer (GstKMSSink * self, GstBuffer * inbuf)
1547 GstBuffer *buf = NULL;
1549 mem = gst_buffer_peek_memory (inbuf, 0);
1553 if (gst_is_kms_memory (mem))
1554 return gst_buffer_ref (inbuf);
1556 if (gst_kms_sink_import_dmabuf (self, inbuf, &buf))
1559 GST_CAT_INFO_OBJECT (CAT_PERFORMANCE, self, "frame copy");
1560 buf = gst_kms_sink_copy_to_dumb_buffer (self, &self->vinfo, inbuf);
1563 /* Copy all the non-memory related metas, this way CropMeta will be
1564 * available upon GstVideoOverlay::expose calls. */
1566 gst_buffer_copy_into (buf, inbuf, GST_BUFFER_COPY_METADATA, 0, -1);
1571 static GstFlowReturn
1572 gst_kms_sink_show_frame (GstVideoSink * vsink, GstBuffer * buf)
1575 GstBuffer *buffer = NULL;
1578 GstVideoInfo *vinfo;
1579 GstVideoCropMeta *crop;
1580 GstVideoRectangle src = { 0, };
1581 gint video_width, video_height;
1582 GstVideoRectangle dst = { 0, };
1583 GstVideoRectangle result;
1586 self = GST_KMS_SINK (vsink);
1588 res = GST_FLOW_ERROR;
1591 buffer = gst_kms_sink_get_input_buffer (self, buf);
1592 vinfo = &self->vinfo;
1593 video_width = src.w = GST_VIDEO_SINK_WIDTH (self);
1594 video_height = src.h = GST_VIDEO_SINK_HEIGHT (self);
1595 } else if (self->last_buffer) {
1596 buffer = gst_buffer_ref (self->last_buffer);
1597 vinfo = &self->last_vinfo;
1598 video_width = src.w = self->last_width;
1599 video_height = src.h = self->last_height;
1602 /* Make sure buf is not used accidentally */
1606 return GST_FLOW_ERROR;
1607 fb_id = gst_kms_memory_get_fb_id (gst_buffer_peek_memory (buffer, 0));
1609 goto buffer_invalid;
1611 GST_TRACE_OBJECT (self, "displaying fb %d", fb_id);
1613 GST_OBJECT_LOCK (self);
1614 if (self->modesetting_enabled) {
1615 self->buffer_id = fb_id;
1619 if ((crop = gst_buffer_get_video_crop_meta (buffer))) {
1620 GstVideoInfo cropped_vinfo = *vinfo;
1622 cropped_vinfo.width = crop->width;
1623 cropped_vinfo.height = crop->height;
1625 if (!gst_kms_sink_calculate_display_ratio (self, &cropped_vinfo, &src.w,
1633 dst.w = self->render_rect.w;
1634 dst.h = self->render_rect.h;
1637 gst_video_sink_center_rect (src, dst, &result, self->can_scale);
1639 result.x += self->render_rect.x;
1640 result.y += self->render_rect.y;
1643 src.w = crop->width;
1644 src.h = crop->height;
1646 src.w = video_width;
1647 src.h = video_height;
1650 /* handle out of screen case */
1651 if ((result.x + result.w) > self->hdisplay)
1652 result.w = self->hdisplay - result.x;
1654 if ((result.y + result.h) > self->vdisplay)
1655 result.h = self->vdisplay - result.y;
1657 if (result.w <= 0 || result.h <= 0) {
1658 GST_WARNING_OBJECT (self, "video is out of display range");
1662 /* to make sure it can be show when driver don't support scale */
1663 if (!self->can_scale) {
1668 GST_TRACE_OBJECT (self,
1669 "drmModeSetPlane at (%i,%i) %ix%i sourcing at (%i,%i) %ix%i",
1670 result.x, result.y, result.w, result.h, src.x, src.y, src.w, src.h);
1672 ret = drmModeSetPlane (self->fd, self->plane_id, self->crtc_id, fb_id, 0,
1673 result.x, result.y, result.w, result.h,
1674 /* source/cropping coordinates are given in Q16 */
1675 src.x << 16, src.y << 16, src.w << 16, src.h << 16);
1677 if (self->can_scale) {
1678 self->can_scale = FALSE;
1679 goto retry_set_plane;
1681 goto set_plane_failed;
1685 /* Wait for the previous frame to complete redraw */
1686 if (!gst_kms_sink_sync (self)) {
1687 GST_OBJECT_UNLOCK (self);
1691 /* Save the rendered buffer and its metadata in case a redraw is needed */
1692 if (buffer != self->last_buffer) {
1693 gst_buffer_replace (&self->last_buffer, buffer);
1694 self->last_width = GST_VIDEO_SINK_WIDTH (self);
1695 self->last_height = GST_VIDEO_SINK_HEIGHT (self);
1696 self->last_vinfo = self->vinfo;
1698 g_clear_pointer (&self->tmp_kmsmem, gst_memory_unref);
1700 GST_OBJECT_UNLOCK (self);
1704 gst_buffer_unref (buffer);
1710 GST_ERROR_OBJECT (self, "invalid buffer: it doesn't have a fb id");
1715 GST_OBJECT_UNLOCK (self);
1716 GST_DEBUG_OBJECT (self, "result = { %d, %d, %d, %d} / "
1717 "src = { %d, %d, %d %d } / dst = { %d, %d, %d %d }", result.x, result.y,
1718 result.w, result.h, src.x, src.y, src.w, src.h, dst.x, dst.y, dst.w,
1720 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
1721 (NULL), ("drmModeSetPlane failed: %s (%d)", g_strerror (errno), errno));
1726 GST_OBJECT_UNLOCK (self);
1727 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1728 ("Error calculating the output display ratio of the video."));
1734 gst_kms_sink_drain (GstKMSSink * self)
1736 GstParentBufferMeta *parent_meta;
1738 if (!self->last_buffer)
1741 /* We only need to return the last_buffer if it depends on upstream buffer.
1742 * In this case, the last_buffer will have a GstParentBufferMeta set. */
1743 parent_meta = gst_buffer_get_parent_buffer_meta (self->last_buffer);
1745 GstBuffer *dumb_buf, *last_buf;
1747 /* If this was imported from our dumb buffer pool we can safely skip the
1749 if (parent_meta->buffer->pool &&
1750 GST_IS_KMS_BUFFER_POOL (parent_meta->buffer->pool))
1753 GST_DEBUG_OBJECT (self, "draining");
1755 dumb_buf = gst_kms_sink_copy_to_dumb_buffer (self, &self->last_vinfo,
1756 parent_meta->buffer);
1757 last_buf = self->last_buffer;
1758 self->last_buffer = dumb_buf;
1760 gst_kms_allocator_clear_cache (self->allocator);
1761 gst_kms_sink_show_frame (GST_VIDEO_SINK (self), NULL);
1762 gst_buffer_unref (last_buf);
1767 gst_kms_sink_query (GstBaseSink * bsink, GstQuery * query)
1769 GstKMSSink *self = GST_KMS_SINK (bsink);
1771 switch (GST_QUERY_TYPE (query)) {
1772 case GST_QUERY_ALLOCATION:
1773 case GST_QUERY_DRAIN:
1775 gst_kms_sink_drain (self);
1782 return GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
1786 _validate_and_set_external_fd (GstKMSSink * self, gint fd)
1788 if (self->devname) {
1789 GST_WARNING_OBJECT (self, "Can't set fd... %s already set.",
1790 g_param_spec_get_name (g_properties[PROP_DRIVER_NAME]));
1795 GST_WARNING_OBJECT (self, "Can't set fd... %s already set.",
1796 g_param_spec_get_name (g_properties[PROP_BUS_ID]));
1800 if (self->fd >= 0) {
1801 GST_WARNING_OBJECT (self, "Can't set fd... it is already set.");
1806 self->devname = drmGetDeviceNameFromFd (fd);
1807 if (!self->devname) {
1808 GST_WARNING_OBJECT (self, "Failed to verify fd is a DRM fd.");
1813 self->is_internal_fd = FALSE;
1818 _invalidate_external_fd (GstKMSSink * self, GParamSpec * pspec)
1820 if (self->is_internal_fd)
1823 GST_WARNING_OBJECT (self, "Unsetting fd... %s has priority.",
1824 g_param_spec_get_name (pspec));
1827 self->is_internal_fd = TRUE;
1831 gst_kms_sink_set_property (GObject * object, guint prop_id,
1832 const GValue * value, GParamSpec * pspec)
1836 sink = GST_KMS_SINK (object);
1839 case PROP_DRIVER_NAME:
1840 _invalidate_external_fd (sink, pspec);
1841 g_free (sink->devname);
1842 sink->devname = g_value_dup_string (value);
1845 _invalidate_external_fd (sink, pspec);
1846 g_free (sink->bus_id);
1847 sink->bus_id = g_value_dup_string (value);
1849 case PROP_CONNECTOR_ID:
1850 sink->conn_id = g_value_get_int (value);
1853 sink->plane_id = g_value_get_int (value);
1855 case PROP_FORCE_MODESETTING:
1856 sink->modesetting_enabled = g_value_get_boolean (value);
1858 case PROP_RESTORE_CRTC:
1859 sink->restore_crtc = g_value_get_boolean (value);
1861 case PROP_CAN_SCALE:
1862 sink->can_scale = g_value_get_boolean (value);
1864 case PROP_CONNECTOR_PROPS:{
1865 const GstStructure *s = gst_value_get_structure (value);
1867 g_clear_pointer (&sink->connector_props, gst_structure_free);
1870 sink->connector_props = gst_structure_copy (s);
1874 case PROP_PLANE_PROPS:{
1875 const GstStructure *s = gst_value_get_structure (value);
1877 g_clear_pointer (&sink->plane_props, gst_structure_free);
1880 sink->plane_props = gst_structure_copy (s);
1885 _validate_and_set_external_fd (sink, g_value_get_int (value));
1888 if (!gst_video_overlay_set_property (object, PROP_N, prop_id, value))
1889 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1895 gst_kms_sink_get_property (GObject * object, guint prop_id,
1896 GValue * value, GParamSpec * pspec)
1900 sink = GST_KMS_SINK (object);
1903 case PROP_DRIVER_NAME:
1904 g_value_set_string (value, sink->devname);
1907 g_value_set_string (value, sink->bus_id);
1909 case PROP_CONNECTOR_ID:
1910 g_value_set_int (value, sink->conn_id);
1913 g_value_set_int (value, sink->plane_id);
1915 case PROP_FORCE_MODESETTING:
1916 g_value_set_boolean (value, sink->modesetting_enabled);
1918 case PROP_RESTORE_CRTC:
1919 g_value_set_boolean (value, sink->restore_crtc);
1921 case PROP_CAN_SCALE:
1922 g_value_set_boolean (value, sink->can_scale);
1924 case PROP_DISPLAY_WIDTH:
1925 GST_OBJECT_LOCK (sink);
1926 g_value_set_int (value, sink->hdisplay);
1927 GST_OBJECT_UNLOCK (sink);
1929 case PROP_DISPLAY_HEIGHT:
1930 GST_OBJECT_LOCK (sink);
1931 g_value_set_int (value, sink->vdisplay);
1932 GST_OBJECT_UNLOCK (sink);
1934 case PROP_CONNECTOR_PROPS:
1935 gst_value_set_structure (value, sink->connector_props);
1937 case PROP_PLANE_PROPS:
1938 gst_value_set_structure (value, sink->plane_props);
1941 g_value_set_int (value, sink->fd);
1944 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1950 gst_kms_sink_finalize (GObject * object)
1954 sink = GST_KMS_SINK (object);
1955 g_clear_pointer (&sink->devname, g_free);
1956 g_clear_pointer (&sink->bus_id, g_free);
1957 gst_poll_free (sink->poll);
1958 g_clear_pointer (&sink->connector_props, gst_structure_free);
1959 g_clear_pointer (&sink->plane_props, gst_structure_free);
1960 g_clear_pointer (&sink->tmp_kmsmem, gst_memory_unref);
1962 G_OBJECT_CLASS (parent_class)->finalize (object);
1966 gst_kms_sink_init (GstKMSSink * sink)
1969 sink->is_internal_fd = TRUE;
1971 sink->plane_id = -1;
1972 sink->can_scale = TRUE;
1973 gst_poll_fd_init (&sink->pollfd);
1974 sink->poll = gst_poll_new (TRUE);
1975 gst_video_info_init (&sink->vinfo);
1979 gst_kms_sink_class_init (GstKMSSinkClass * klass)
1981 GObjectClass *gobject_class;
1982 GstElementClass *element_class;
1983 GstBaseSinkClass *basesink_class;
1984 GstVideoSinkClass *videosink_class;
1987 gobject_class = G_OBJECT_CLASS (klass);
1988 element_class = GST_ELEMENT_CLASS (klass);
1989 basesink_class = GST_BASE_SINK_CLASS (klass);
1990 videosink_class = GST_VIDEO_SINK_CLASS (klass);
1992 gst_element_class_set_static_metadata (element_class, "KMS video sink",
1993 "Sink/Video", GST_PLUGIN_DESC, "Víctor Jáquez <vjaquez@igalia.com>");
1995 caps = gst_kms_sink_caps_template_fill ();
1996 gst_element_class_add_pad_template (element_class,
1997 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps));
1998 gst_caps_unref (caps);
2000 basesink_class->start = GST_DEBUG_FUNCPTR (gst_kms_sink_start);
2001 basesink_class->stop = GST_DEBUG_FUNCPTR (gst_kms_sink_stop);
2002 basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_kms_sink_set_caps);
2003 basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_kms_sink_get_caps);
2004 basesink_class->propose_allocation = gst_kms_sink_propose_allocation;
2005 basesink_class->query = gst_kms_sink_query;
2007 videosink_class->show_frame = gst_kms_sink_show_frame;
2009 gobject_class->finalize = gst_kms_sink_finalize;
2010 gobject_class->set_property = gst_kms_sink_set_property;
2011 gobject_class->get_property = gst_kms_sink_get_property;
2014 * kmssink:driver-name:
2016 * If you have a system with multiple GPUs, you can choose which GPU
2017 * to use setting the DRM device driver name. Otherwise, the first
2018 * one from an internal list is used.
2020 g_properties[PROP_DRIVER_NAME] = g_param_spec_string ("driver-name",
2021 "device name", "DRM device driver name", NULL,
2022 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
2027 * If you have a system with multiple displays for the same driver-name,
2028 * you can choose which display to use by setting the DRM bus ID. Otherwise,
2029 * the driver decides which one.
2031 g_properties[PROP_BUS_ID] = g_param_spec_string ("bus-id",
2032 "Bus ID", "DRM bus ID", NULL,
2033 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
2036 * kmssink:connector-id:
2038 * A GPU has several output connectors, for example: LVDS, VGA,
2039 * HDMI, etc. By default the first LVDS is tried, then the first
2040 * eDP, and at the end, the first connected one.
2042 g_properties[PROP_CONNECTOR_ID] = g_param_spec_int ("connector-id",
2043 "Connector ID", "DRM connector id", -1, G_MAXINT32, -1,
2044 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
2049 * There could be several planes associated with a CRTC.
2050 * By default the first plane that's possible to use with a given
2053 g_properties[PROP_PLANE_ID] = g_param_spec_int ("plane-id",
2054 "Plane ID", "DRM plane id", -1, G_MAXINT32, -1,
2055 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
2058 * kmssink:force-modesetting:
2060 * If the output connector is already active, the sink automatically uses an
2061 * overlay plane. Enforce mode setting in the kms sink and output to the
2062 * base plane to override the automatic behavior.
2064 g_properties[PROP_FORCE_MODESETTING] =
2065 g_param_spec_boolean ("force-modesetting", "Force modesetting",
2066 "When enabled, the sink try to configure the display mode", FALSE,
2067 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
2070 * kmssink:restore-crtc:
2072 * Restore previous CRTC setting if new CRTC mode was set forcefully.
2073 * By default this is enabled if user set CRTC with a new mode on an already
2074 * active CRTC wich was having a valid mode.
2076 g_properties[PROP_RESTORE_CRTC] =
2077 g_param_spec_boolean ("restore-crtc", "Restore CRTC mode",
2078 "When enabled and CRTC was set with a new mode, previous CRTC mode will"
2079 "be restored when going to NULL state.", TRUE,
2080 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
2083 * kmssink:can-scale:
2085 * User can tell kmssink if the driver can support scale.
2087 g_properties[PROP_CAN_SCALE] =
2088 g_param_spec_boolean ("can-scale", "can scale",
2089 "User can tell kmssink if the driver can support scale", TRUE,
2090 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
2093 * kmssink:display-width
2095 * Actual width of the display. This is read only and only available in
2096 * PAUSED and PLAYING state. It's meant to be used with
2097 * gst_video_overlay_set_render_rectangle() function.
2099 g_properties[PROP_DISPLAY_WIDTH] =
2100 g_param_spec_int ("display-width", "Display Width",
2101 "Width of the display surface in pixels", 0, G_MAXINT, 0,
2102 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
2105 * kmssink:display-height
2107 * Actual height of the display. This is read only and only available in
2108 * PAUSED and PLAYING state. It's meant to be used with
2109 * gst_video_overlay_set_render_rectangle() function.
2111 g_properties[PROP_DISPLAY_HEIGHT] =
2112 g_param_spec_int ("display-height", "Display Height",
2113 "Height of the display surface in pixels", 0, G_MAXINT, 0,
2114 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
2117 * kmssink:connector-properties:
2119 * Additional properties for the connector. Keys are strings and values
2120 * unsigned 64 bits integers.
2124 g_properties[PROP_CONNECTOR_PROPS] =
2125 g_param_spec_boxed ("connector-properties", "Connector Properties",
2126 "Additional properties for the connector",
2127 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2130 * kmssink:plane-properties:
2132 * Additional properties for the plane. Keys are strings and values
2133 * unsigned 64 bits integers.
2137 g_properties[PROP_PLANE_PROPS] =
2138 g_param_spec_boxed ("plane-properties", "Connector Plane",
2139 "Additional properties for the plane",
2140 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2145 * You can supply your own DRM file descriptor. By default, the sink will
2146 * open its own DRM file descriptor.
2150 g_properties[PROP_FD] =
2151 g_param_spec_int ("fd", "File Descriptor",
2152 "DRM file descriptor", -1, G_MAXINT, -1,
2153 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
2155 g_object_class_install_properties (gobject_class, PROP_N, g_properties);
2157 gst_video_overlay_install_properties (gobject_class, PROP_N);
2161 plugin_init (GstPlugin * plugin)
2163 return GST_ELEMENT_REGISTER (kmssink, plugin);
2166 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, kms,
2167 GST_PLUGIN_DESC, plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,