488c5bcbf8da2e3f85daa43ff3309c1f2ed8e989
[platform/upstream/gstreamer.git] / sys / kms / gstkmssink.c
1 /* GStreamer
2  *
3  * Copyright (C) 2016 Igalia
4  *
5  * Authors:
6  *  Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
7  *  Javier Martin <javiermartin@by.com.es>
8  *
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.
13  *
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.
18  *
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.
23  *
24  */
25
26 /**
27  * SECTION:element-kmssink
28  * @title: kmssink
29  * @short_description: A KMS/DRM based video sink
30  *
31  * kmssink is a simple video sink that renders video frames directly
32  * in a plane of a DRM device.
33  *
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.
37  *
38  * ## Example launch line
39  * |[
40  * gst-launch-1.0 videotestsrc ! kmssink
41  * gst-launch-1.0 videotestsrc ! kmssink plane-properties=s,rotation=4
42  * ]|
43  *
44  */
45
46 #ifdef HAVE_CONFIG_H
47 #include "config.h"
48 #endif
49
50 #include <gst/video/video.h>
51 #include <gst/video/videooverlay.h>
52 #include <gst/allocators/gstdmabuf.h>
53
54 #include <drm.h>
55 #include <xf86drm.h>
56 #include <xf86drmMode.h>
57 #include <drm_fourcc.h>
58 #include <string.h>
59
60 #include "gstkmssink.h"
61 #include "gstkmsutils.h"
62 #include "gstkmsbufferpool.h"
63 #include "gstkmsallocator.h"
64
65 #define GST_PLUGIN_NAME "kmssink"
66 #define GST_PLUGIN_DESC "Video sink using the Linux kernel mode setting API"
67
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
71
72 static GstFlowReturn gst_kms_sink_show_frame (GstVideoSink * vsink,
73     GstBuffer * buf);
74 static void gst_kms_sink_video_overlay_init (GstVideoOverlayInterface * iface);
75 static void gst_kms_sink_drain (GstKMSSink * self);
76
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,
80         GST_PLUGIN_DESC);
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
85 enum
86 {
87   PROP_DRIVER_NAME = 1,
88   PROP_BUS_ID,
89   PROP_CONNECTOR_ID,
90   PROP_PLANE_ID,
91   PROP_FORCE_MODESETTING,
92   PROP_RESTORE_CRTC,
93   PROP_CAN_SCALE,
94   PROP_DISPLAY_WIDTH,
95   PROP_DISPLAY_HEIGHT,
96   PROP_CONNECTOR_PROPS,
97   PROP_PLANE_PROPS,
98   PROP_N,
99 };
100
101 static GParamSpec *g_properties[PROP_N] = { NULL, };
102
103 static void
104 gst_kms_sink_set_render_rectangle (GstVideoOverlay * overlay,
105     gint x, gint y, gint width, gint height)
106 {
107   GstKMSSink *self = GST_KMS_SINK (overlay);
108
109   GST_DEBUG_OBJECT (self, "Setting render rectangle to (%d,%d) %dx%d", x, y,
110       width, height);
111
112   GST_OBJECT_LOCK (self);
113
114   if (width == -1 && height == -1) {
115     x = 0;
116     y = 0;
117     width = self->hdisplay;
118     height = self->vdisplay;
119   }
120
121   if (width <= 0 || height <= 0)
122     goto done;
123
124   self->pending_rect.x = x;
125   self->pending_rect.y = y;
126   self->pending_rect.w = width;
127   self->pending_rect.h = height;
128
129   if (self->can_scale ||
130       (self->render_rect.w == width && self->render_rect.h == height)) {
131     self->render_rect = self->pending_rect;
132   } else {
133     self->reconfigure = TRUE;
134     GST_DEBUG_OBJECT (self, "Waiting for new caps to apply render rectangle");
135   }
136
137 done:
138   GST_OBJECT_UNLOCK (self);
139 }
140
141 static void
142 gst_kms_sink_expose (GstVideoOverlay * overlay)
143 {
144   GstKMSSink *self = GST_KMS_SINK (overlay);
145
146   GST_DEBUG_OBJECT (overlay, "Expose called by application");
147
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 ());
155     } else {
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);
160     }
161   }
162
163   gst_kms_sink_show_frame (GST_VIDEO_SINK (self), NULL);
164 }
165
166 static void
167 gst_kms_sink_video_overlay_init (GstVideoOverlayInterface * iface)
168 {
169   iface->expose = gst_kms_sink_expose;
170   iface->set_render_rectangle = gst_kms_sink_set_render_rectangle;
171 }
172
173 static int
174 kms_open (gchar ** driver)
175 {
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 */
180   };
181   int i, fd = -1;
182
183   for (i = 0; i < G_N_ELEMENTS (drivers); i++) {
184     fd = drmOpen (drivers[i], NULL);
185     if (fd >= 0) {
186       if (driver)
187         *driver = g_strdup (drivers[i]);
188       break;
189     }
190   }
191
192   return fd;
193 }
194
195 static drmModePlane *
196 find_plane_for_crtc (int fd, drmModeRes * res, drmModePlaneRes * pres,
197     int crtc_id)
198 {
199   drmModePlane *plane;
200   int i, pipe;
201
202   plane = NULL;
203   pipe = -1;
204   for (i = 0; i < res->count_crtcs; i++) {
205     if (crtc_id == res->crtcs[i]) {
206       pipe = i;
207       break;
208     }
209   }
210
211   if (pipe == -1)
212     return NULL;
213
214   for (i = 0; i < pres->count_planes; i++) {
215     plane = drmModeGetPlane (fd, pres->planes[i]);
216     if (plane->possible_crtcs & (1 << pipe))
217       return plane;
218     drmModeFreePlane (plane);
219   }
220
221   return NULL;
222 }
223
224 static drmModeCrtc *
225 find_crtc_for_connector (int fd, drmModeRes * res, drmModeConnector * conn,
226     guint * pipe)
227 {
228   int i;
229   int crtc_id;
230   drmModeEncoder *enc;
231   drmModeCrtc *crtc;
232   guint32 crtcs_for_connector = 0;
233
234   crtc_id = -1;
235   for (i = 0; i < res->count_encoders; i++) {
236     enc = drmModeGetEncoder (fd, res->encoders[i]);
237     if (enc) {
238       if (enc->encoder_id == conn->encoder_id) {
239         crtc_id = enc->crtc_id;
240         drmModeFreeEncoder (enc);
241         break;
242       }
243       drmModeFreeEncoder (enc);
244     }
245   }
246
247   /* If no active crtc was found, pick the first possible crtc */
248   if (crtc_id == -1) {
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);
253     }
254
255     if (crtcs_for_connector != 0)
256       crtc_id = res->crtcs[ffs (crtcs_for_connector) - 1];
257   }
258
259   if (crtc_id == -1)
260     return NULL;
261
262   for (i = 0; i < res->count_crtcs; i++) {
263     crtc = drmModeGetCrtc (fd, res->crtcs[i]);
264     if (crtc) {
265       if (crtc_id == crtc->crtc_id) {
266         if (pipe)
267           *pipe = i;
268         return crtc;
269       }
270       drmModeFreeCrtc (crtc);
271     }
272   }
273
274   return NULL;
275 }
276
277 static gboolean
278 connector_is_used (int fd, drmModeRes * res, drmModeConnector * conn)
279 {
280   gboolean result;
281   drmModeCrtc *crtc;
282
283   result = FALSE;
284   crtc = find_crtc_for_connector (fd, res, conn, NULL);
285   if (crtc) {
286     result = crtc->buffer_id != 0;
287     drmModeFreeCrtc (crtc);
288   }
289
290   return result;
291 }
292
293 static drmModeConnector *
294 find_used_connector_by_type (int fd, drmModeRes * res, int type)
295 {
296   int i;
297   drmModeConnector *conn;
298
299   conn = NULL;
300   for (i = 0; i < res->count_connectors; i++) {
301     conn = drmModeGetConnector (fd, res->connectors[i]);
302     if (conn) {
303       if ((conn->connector_type == type) && connector_is_used (fd, res, conn))
304         return conn;
305       drmModeFreeConnector (conn);
306     }
307   }
308
309   return NULL;
310 }
311
312 static drmModeConnector *
313 find_first_used_connector (int fd, drmModeRes * res)
314 {
315   int i;
316   drmModeConnector *conn;
317
318   conn = NULL;
319   for (i = 0; i < res->count_connectors; i++) {
320     conn = drmModeGetConnector (fd, res->connectors[i]);
321     if (conn) {
322       if (connector_is_used (fd, res, conn))
323         return conn;
324       drmModeFreeConnector (conn);
325     }
326   }
327
328   return NULL;
329 }
330
331 static drmModeConnector *
332 find_main_monitor (int fd, drmModeRes * res)
333 {
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
337   };
338   int i;
339   drmModeConnector *conn;
340
341   conn = NULL;
342   for (i = 0; !conn && i < G_N_ELEMENTS (priority); i++)
343     conn = find_used_connector_by_type (fd, res, priority[i]);
344
345   /* if we didn't find a connector, grab the first one in use */
346   if (!conn)
347     conn = find_first_used_connector (fd, res);
348
349   /* if no connector is used, grab the first one */
350   if (!conn)
351     conn = drmModeGetConnector (fd, res->connectors[0]);
352
353   return conn;
354 }
355
356 static void
357 log_drm_version (GstKMSSink * self)
358 {
359 #ifndef GST_DISABLE_GST_DEBUG
360   drmVersion *v;
361
362   v = drmGetVersion (self->fd);
363   if (v) {
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));
367     drmFreeVersion (v);
368   } else {
369     GST_WARNING_OBJECT (self, "could not get driver information: %s",
370         GST_STR_NULL (self->devname));
371   }
372 #endif
373   return;
374 }
375
376 static gboolean
377 get_drm_caps (GstKMSSink * self)
378 {
379   gint ret;
380   guint64 has_dumb_buffer;
381   guint64 has_prime;
382   guint64 has_async_page_flip;
383
384   has_dumb_buffer = 0;
385   ret = drmGetCap (self->fd, DRM_CAP_DUMB_BUFFER, &has_dumb_buffer);
386   if (ret)
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");
390     return FALSE;
391   }
392
393   has_prime = 0;
394   ret = drmGetCap (self->fd, DRM_CAP_PRIME, &has_prime);
395   if (ret)
396     GST_WARNING_OBJECT (self, "could not get prime capability");
397   else {
398     self->has_prime_import = (gboolean) (has_prime & DRM_PRIME_CAP_IMPORT);
399     self->has_prime_export = (gboolean) (has_prime & DRM_PRIME_CAP_EXPORT);
400   }
401
402   has_async_page_flip = 0;
403   ret = drmGetCap (self->fd, DRM_CAP_ASYNC_PAGE_FLIP, &has_async_page_flip);
404   if (ret)
405     GST_WARNING_OBJECT (self, "could not get async page flip capability");
406   else
407     self->has_async_page_flip = (gboolean) has_async_page_flip;
408
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 ? "✓" : "✗");
414
415   return TRUE;
416 }
417
418 static gboolean
419 configure_mode_setting (GstKMSSink * self, GstVideoInfo * vinfo)
420 {
421   gboolean ret;
422   drmModeConnector *conn;
423   int err;
424   gint i;
425   drmModeModeInfo *mode;
426   guint32 fb_id;
427   GstKMSMemory *kmsmem;
428
429   ret = FALSE;
430   conn = NULL;
431   mode = NULL;
432   kmsmem = NULL;
433
434   if (self->conn_id < 0)
435     goto bail;
436
437   GST_INFO_OBJECT (self, "configuring mode setting");
438
439   kmsmem = (GstKMSMemory *) gst_kms_allocator_bo_alloc (self->allocator, vinfo);
440   if (!kmsmem)
441     goto bo_failed;
442   fb_id = kmsmem->fb_id;
443
444   conn = drmModeGetConnector (self->fd, self->conn_id);
445   if (!conn)
446     goto connector_failed;
447
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];
452       break;
453     }
454   }
455   if (!mode)
456     goto mode_failed;
457
458   err = drmModeSetCrtc (self->fd, self->crtc_id, fb_id, 0, 0,
459       (uint32_t *) & self->conn_id, 1, mode);
460   if (err)
461     goto modesetting_failed;
462
463   g_clear_pointer (&self->tmp_kmsmem, gst_memory_unref);
464   self->tmp_kmsmem = (GstMemory *) kmsmem;
465
466   ret = TRUE;
467
468 bail:
469   if (conn)
470     drmModeFreeConnector (conn);
471
472   return ret;
473
474   /* ERRORS */
475 bo_failed:
476   {
477     GST_ERROR_OBJECT (self,
478         "failed to allocate buffer object for mode setting");
479     goto bail;
480   }
481 connector_failed:
482   {
483     GST_ERROR_OBJECT (self, "Could not find a valid monitor connector");
484     goto bail;
485   }
486 mode_failed:
487   {
488     GST_ERROR_OBJECT (self, "cannot find appropriate mode");
489     goto bail;
490   }
491 modesetting_failed:
492   {
493     GST_ERROR_OBJECT (self, "Failed to set mode: %s", g_strerror (errno));
494     goto bail;
495   }
496 }
497
498 static gboolean
499 ensure_allowed_caps (GstKMSSink * self, drmModeConnector * conn,
500     drmModePlane * plane, drmModeRes * res)
501 {
502   GstCaps *out_caps, *tmp_caps, *caps;
503   int i, j;
504   GstVideoFormat fmt;
505   const gchar *format;
506   drmModeModeInfo *mode;
507   gint count_modes;
508
509   if (self->allowed_caps)
510     return TRUE;
511
512   out_caps = gst_caps_new_empty ();
513   if (!out_caps)
514     return FALSE;
515
516   if (conn && self->modesetting_enabled)
517     count_modes = conn->count_modes;
518   else
519     count_modes = 1;
520
521   for (i = 0; i < count_modes; i++) {
522     tmp_caps = gst_caps_new_empty ();
523     if (!tmp_caps)
524       return FALSE;
525
526     mode = NULL;
527     if (conn && self->modesetting_enabled)
528       mode = &conn->modes[i];
529
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]));
535         continue;
536       }
537
538       format = gst_video_format_to_string (fmt);
539
540       if (mode) {
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);
546       } else {
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);
552       }
553       if (!caps)
554         continue;
555
556       tmp_caps = gst_caps_merge (tmp_caps, caps);
557     }
558
559     out_caps = gst_caps_merge (out_caps, gst_caps_simplify (tmp_caps));
560   }
561
562   self->allowed_caps = gst_caps_simplify (out_caps);
563
564   GST_DEBUG_OBJECT (self, "allowed caps = %" GST_PTR_FORMAT,
565       self->allowed_caps);
566
567   return (self->allowed_caps && !gst_caps_is_empty (self->allowed_caps));
568 }
569
570 static gboolean
571 set_drm_property (gint fd, guint32 object, guint32 object_type,
572     drmModeObjectPropertiesPtr properties, const gchar * prop_name,
573     guint64 value)
574 {
575   guint i;
576   gboolean ret = FALSE;
577
578   for (i = 0; i < properties->count_props && !ret; i++) {
579     drmModePropertyPtr property;
580
581     property = drmModeGetProperty (fd, properties->props[i]);
582
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 "_",
587         '-');
588
589     GST_LOG ("found property %s (looking for %s)", property->name, prop_name);
590
591     if (!strcmp (property->name, prop_name)) {
592       drmModeObjectSetProperty (fd, object, object_type,
593           property->prop_id, value);
594       ret = TRUE;
595     }
596     drmModeFreeProperty (property);
597   }
598
599   return ret;
600 }
601
602 typedef struct
603 {
604   GstKMSSink *self;
605   drmModeObjectPropertiesPtr properties;
606   guint obj_id;
607   guint obj_type;
608   const gchar *obj_type_str;
609 } SetPropsIter;
610
611 static gboolean
612 set_obj_prop (GQuark field_id, const GValue * value, gpointer user_data)
613 {
614   SetPropsIter *iter = user_data;
615   GstKMSSink *self = iter->self;
616   const gchar *name;
617   guint64 v;
618
619   name = g_quark_to_string (field_id);
620
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);
629   else {
630     GST_WARNING_OBJECT (self,
631         "'uint64' value expected for control '%s'.", name);
632     return TRUE;
633   }
634
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);
640   } else {
641     GST_WARNING_OBJECT (self,
642         "Failed to set %s property '%s' to %" G_GUINT64_FORMAT,
643         iter->obj_type_str, name, v);
644   }
645
646   return TRUE;
647 }
648
649 static void
650 gst_kms_sink_update_properties (SetPropsIter * iter, GstStructure * props)
651 {
652   GstKMSSink *self = iter->self;
653
654   iter->properties = drmModeObjectGetProperties (self->fd, iter->obj_id,
655       iter->obj_type);
656
657   gst_structure_foreach (props, set_obj_prop, iter);
658
659   drmModeFreeObjectProperties (iter->properties);
660 }
661
662 static void
663 gst_kms_sink_update_connector_properties (GstKMSSink * self)
664 {
665   SetPropsIter iter;
666
667   if (!self->connector_props)
668     return;
669
670   iter.self = self;
671   iter.obj_id = self->conn_id;
672   iter.obj_type = DRM_MODE_OBJECT_CONNECTOR;
673   iter.obj_type_str = "connector";
674
675   gst_kms_sink_update_properties (&iter, self->connector_props);
676 }
677
678 static void
679 gst_kms_sink_update_plane_properties (GstKMSSink * self)
680 {
681   SetPropsIter iter;
682
683   if (!self->plane_props)
684     return;
685
686   iter.self = self;
687   iter.obj_id = self->plane_id;
688   iter.obj_type = DRM_MODE_OBJECT_PLANE;
689   iter.obj_type_str = "plane";
690
691   gst_kms_sink_update_properties (&iter, self->plane_props);
692 }
693
694 static gboolean
695 gst_kms_sink_start (GstBaseSink * bsink)
696 {
697   GstKMSSink *self;
698   drmModeRes *res;
699   drmModeConnector *conn;
700   drmModeCrtc *crtc;
701   drmModePlaneRes *pres;
702   drmModePlane *plane;
703   gboolean universal_planes;
704   gboolean ret;
705
706   self = GST_KMS_SINK (bsink);
707   universal_planes = FALSE;
708   ret = FALSE;
709   res = NULL;
710   conn = NULL;
711   crtc = NULL;
712   pres = NULL;
713   plane = NULL;
714
715   if (self->devname || self->bus_id)
716     self->fd = drmOpen (self->devname, self->bus_id);
717   else
718     self->fd = kms_open (&self->devname);
719   if (self->fd < 0)
720     goto open_failed;
721
722   log_drm_version (self);
723   if (!get_drm_caps (self))
724     goto bail;
725
726   res = drmModeGetResources (self->fd);
727   if (!res)
728     goto resources_failed;
729
730   if (self->conn_id == -1)
731     conn = find_main_monitor (self->fd, res);
732   else
733     conn = drmModeGetConnector (self->fd, self->conn_id);
734   if (!conn)
735     goto connector_failed;
736
737   crtc = find_crtc_for_connector (self->fd, res, conn, &self->pipe);
738   if (!crtc)
739     goto crtc_failed;
740
741   if (!crtc->mode_valid || self->modesetting_enabled) {
742     GST_DEBUG_OBJECT (self, "enabling modesetting");
743     self->modesetting_enabled = TRUE;
744     universal_planes = TRUE;
745   }
746
747   if (crtc->mode_valid && self->modesetting_enabled && self->restore_crtc) {
748     self->saved_crtc = (drmModeCrtc *) crtc;
749   }
750
751 retry_find_plane:
752   if (universal_planes &&
753       drmSetClientCap (self->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1))
754     goto set_cap_failed;
755
756   pres = drmModeGetPlaneResources (self->fd);
757   if (!pres)
758     goto plane_resources_failed;
759
760   if (self->plane_id == -1)
761     plane = find_plane_for_crtc (self->fd, res, pres, crtc->crtc_id);
762   else
763     plane = drmModeGetPlane (self->fd, self->plane_id);
764   if (!plane)
765     goto plane_failed;
766
767   if (!ensure_allowed_caps (self, conn, plane, res))
768     goto allowed_caps_failed;
769
770   self->conn_id = conn->connector_id;
771   self->crtc_id = crtc->crtc_id;
772   self->plane_id = plane->plane_id;
773
774   GST_INFO_OBJECT (self, "connector id = %d / crtc id = %d / plane id = %d",
775       self->conn_id, self->crtc_id, self->plane_id);
776
777   GST_OBJECT_LOCK (self);
778   self->hdisplay = crtc->mode.hdisplay;
779   self->vdisplay = crtc->mode.vdisplay;
780
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;
786   }
787
788   self->pending_rect = self->render_rect;
789   GST_OBJECT_UNLOCK (self);
790
791   self->buffer_id = crtc->buffer_id;
792
793   self->mm_width = conn->mmWidth;
794   self->mm_height = conn->mmHeight;
795
796   GST_INFO_OBJECT (self, "display size: pixels = %dx%d / millimeters = %dx%d",
797       self->hdisplay, self->vdisplay, self->mm_width, self->mm_height);
798
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);
802
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]);
805
806   gst_kms_sink_update_connector_properties (self);
807   gst_kms_sink_update_plane_properties (self);
808
809   ret = TRUE;
810
811 bail:
812   if (plane)
813     drmModeFreePlane (plane);
814   if (pres)
815     drmModeFreePlaneResources (pres);
816   if (crtc != self->saved_crtc)
817     drmModeFreeCrtc (crtc);
818   if (conn)
819     drmModeFreeConnector (conn);
820   if (res)
821     drmModeFreeResources (res);
822
823   if (!ret && self->fd >= 0) {
824     drmClose (self->fd);
825     self->fd = -1;
826   }
827
828   return ret;
829
830   /* ERRORS */
831 open_failed:
832   {
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));
836     return FALSE;
837   }
838
839 resources_failed:
840   {
841     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
842         ("drmModeGetResources failed"),
843         ("reason: %s (%d)", g_strerror (errno), errno));
844     goto bail;
845   }
846
847 connector_failed:
848   {
849     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
850         ("Could not find a valid monitor connector"), (NULL));
851     goto bail;
852   }
853
854 crtc_failed:
855   {
856     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
857         ("Could not find a crtc for connector"), (NULL));
858     goto bail;
859   }
860
861 set_cap_failed:
862   {
863     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
864         ("Could not set universal planes capability bit"), (NULL));
865     goto bail;
866   }
867
868 plane_resources_failed:
869   {
870     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
871         ("drmModeGetPlaneResources failed"),
872         ("reason: %s (%d)", g_strerror (errno), errno));
873     goto bail;
874   }
875
876 plane_failed:
877   {
878     if (universal_planes) {
879       GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
880           ("Could not find a plane for crtc"), (NULL));
881       goto bail;
882     } else {
883       universal_planes = TRUE;
884       goto retry_find_plane;
885     }
886   }
887
888 allowed_caps_failed:
889   {
890     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
891         ("Could not get allowed GstCaps of device"),
892         ("driver does not provide mode settings configuration"));
893     goto bail;
894   }
895 }
896
897 static gboolean
898 gst_kms_sink_stop (GstBaseSink * bsink)
899 {
900   GstKMSSink *self;
901   int err;
902
903   self = GST_KMS_SINK (bsink);
904
905   if (self->allocator)
906     gst_kms_allocator_clear_cache (self->allocator);
907
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);
912
913   gst_poll_remove_fd (self->poll, &self->pollfd);
914   gst_poll_restart (self->poll);
915   gst_poll_fd_init (&self->pollfd);
916
917   if (self->saved_crtc) {
918     drmModeCrtc *crtc = (drmModeCrtc *) self->saved_crtc;
919
920     err = drmModeSetCrtc (self->fd, crtc->crtc_id, crtc->buffer_id, crtc->x,
921         crtc->y, (uint32_t *) & self->conn_id, 1, &crtc->mode);
922     if (err)
923       GST_ERROR_OBJECT (self, "Failed to restore previous CRTC mode: %s",
924           g_strerror (errno));
925
926     drmModeFreeCrtc (crtc);
927     self->saved_crtc = NULL;
928   }
929
930   if (self->fd >= 0) {
931     drmClose (self->fd);
932     self->fd = -1;
933   }
934
935   GST_OBJECT_LOCK (bsink);
936   self->hdisplay = 0;
937   self->vdisplay = 0;
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);
944
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]);
947
948   return TRUE;
949 }
950
951 static GstCaps *
952 gst_kms_sink_get_allowed_caps (GstKMSSink * self)
953 {
954   if (!self->allowed_caps)
955     return NULL;                /* base class will return the template caps */
956   return gst_caps_ref (self->allowed_caps);
957 }
958
959 static GstCaps *
960 gst_kms_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
961 {
962   GstKMSSink *self;
963   GstCaps *caps, *out_caps;
964   GstStructure *s;
965   guint dpy_par_n, dpy_par_d;
966
967   self = GST_KMS_SINK (bsink);
968
969   caps = gst_kms_sink_get_allowed_caps (self);
970   if (!caps)
971     return NULL;
972
973   GST_OBJECT_LOCK (self);
974
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);
979
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);
984
985     gst_caps_append_structure (out_caps, s);
986
987     out_caps = gst_caps_merge (out_caps, caps);
988     caps = NULL;
989
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);
993   } else {
994     out_caps = gst_caps_make_writable (caps);
995     caps = NULL;
996   }
997
998   GST_OBJECT_UNLOCK (self);
999
1000   GST_DEBUG_OBJECT (self, "Proposing caps %" GST_PTR_FORMAT, out_caps);
1001
1002   if (filter) {
1003     caps = out_caps;
1004     out_caps = gst_caps_intersect_full (caps, filter, GST_CAPS_INTERSECT_FIRST);
1005     gst_caps_unref (caps);
1006   }
1007
1008   return out_caps;
1009 }
1010
1011 static void
1012 ensure_kms_allocator (GstKMSSink * self)
1013 {
1014   if (self->allocator)
1015     return;
1016   self->allocator = gst_kms_allocator_new (self->fd);
1017 }
1018
1019 static GstBufferPool *
1020 gst_kms_sink_create_pool (GstKMSSink * self, GstCaps * caps, gsize size,
1021     gint min)
1022 {
1023   GstBufferPool *pool;
1024   GstStructure *config;
1025
1026   pool = gst_kms_buffer_pool_new ();
1027   if (!pool)
1028     goto pool_failed;
1029
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);
1033
1034   ensure_kms_allocator (self);
1035   gst_buffer_pool_config_set_allocator (config, self->allocator, NULL);
1036
1037   if (!gst_buffer_pool_set_config (pool, config))
1038     goto config_failed;
1039
1040   return pool;
1041
1042   /* ERRORS */
1043 pool_failed:
1044   {
1045     GST_ERROR_OBJECT (self, "failed to create buffer pool");
1046     return NULL;
1047   }
1048 config_failed:
1049   {
1050     GST_ERROR_OBJECT (self, "failed to set config");
1051     gst_object_unref (pool);
1052     return NULL;
1053   }
1054 }
1055
1056 static gboolean
1057 gst_kms_sink_calculate_display_ratio (GstKMSSink * self, GstVideoInfo * vinfo)
1058 {
1059   guint dar_n, dar_d;
1060   guint video_width, video_height;
1061   guint video_par_n, video_par_d;
1062   guint dpy_par_n, dpy_par_d;
1063
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);
1068
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);
1072   } else {
1073     GST_VIDEO_SINK_WIDTH (self) = video_width;
1074     GST_VIDEO_SINK_HEIGHT (self) = video_height;
1075     goto out;
1076   }
1077
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))
1080     return FALSE;
1081
1082   GST_DEBUG_OBJECT (self, "video calculated display ratio: %d/%d", dar_n,
1083       dar_d);
1084
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 */
1088
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);
1101   } else {
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;
1106   }
1107
1108 out:
1109   GST_DEBUG_OBJECT (self, "scaling to %dx%d", GST_VIDEO_SINK_WIDTH (self),
1110       GST_VIDEO_SINK_HEIGHT (self));
1111
1112   return TRUE;
1113 }
1114
1115 static gboolean
1116 gst_kms_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
1117 {
1118   GstKMSSink *self;
1119   GstVideoInfo vinfo;
1120   GstBufferPool *newpool, *oldpool;
1121
1122   self = GST_KMS_SINK (bsink);
1123
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);
1128
1129   if (!gst_video_info_from_caps (&vinfo, caps))
1130     goto invalid_format;
1131
1132   if (!gst_kms_sink_calculate_display_ratio (self, &vinfo))
1133     goto no_disp_ratio;
1134
1135   if (GST_VIDEO_SINK_WIDTH (self) <= 0 || GST_VIDEO_SINK_HEIGHT (self) <= 0)
1136     goto invalid_size;
1137
1138   /* create a new pool for the new configuration */
1139   newpool = gst_kms_sink_create_pool (self, caps, GST_VIDEO_INFO_SIZE (&vinfo),
1140       2);
1141   if (!newpool)
1142     goto no_pool;
1143
1144   /* we don't activate the internal pool yet as it may not be needed */
1145   oldpool = self->pool;
1146   self->pool = newpool;
1147
1148   if (oldpool) {
1149     gst_buffer_pool_set_active (oldpool, FALSE);
1150     gst_object_unref (oldpool);
1151   }
1152
1153   if (self->modesetting_enabled && !configure_mode_setting (self, &vinfo))
1154     goto modesetting_failed;
1155
1156   self->vinfo = vinfo;
1157
1158   GST_OBJECT_LOCK (self);
1159   if (self->reconfigure) {
1160     self->reconfigure = FALSE;
1161     self->render_rect = self->pending_rect;
1162   }
1163   GST_OBJECT_UNLOCK (self);
1164
1165   GST_DEBUG_OBJECT (self, "negotiated caps = %" GST_PTR_FORMAT, caps);
1166
1167   return TRUE;
1168
1169   /* ERRORS */
1170 invalid_format:
1171   {
1172     GST_ERROR_OBJECT (self, "caps invalid");
1173     return FALSE;
1174   }
1175
1176 invalid_size:
1177   {
1178     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1179         ("Invalid image size."));
1180     return FALSE;
1181   }
1182
1183 no_disp_ratio:
1184   {
1185     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1186         ("Error calculating the output display ratio of the video."));
1187     return FALSE;
1188   }
1189 no_pool:
1190   {
1191     /* Already warned in create_pool */
1192     return FALSE;
1193   }
1194
1195 modesetting_failed:
1196   {
1197     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1198         ("failed to configure video mode"));
1199     return FALSE;
1200   }
1201
1202 }
1203
1204 static gboolean
1205 gst_kms_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
1206 {
1207   GstKMSSink *self;
1208   GstCaps *caps;
1209   gboolean need_pool;
1210   GstVideoInfo vinfo;
1211   GstBufferPool *pool;
1212   gsize size;
1213
1214   self = GST_KMS_SINK (bsink);
1215
1216   gst_query_parse_allocation (query, &caps, &need_pool);
1217   if (!caps)
1218     goto no_caps;
1219   if (!gst_video_info_from_caps (&vinfo, caps))
1220     goto invalid_caps;
1221
1222   size = GST_VIDEO_INFO_SIZE (&vinfo);
1223
1224   pool = NULL;
1225   if (need_pool) {
1226     pool = gst_kms_sink_create_pool (self, caps, size, 0);
1227     if (!pool)
1228       goto no_pool;
1229
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);
1236     }
1237   }
1238
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);
1241   if (pool)
1242     gst_object_unref (pool);
1243
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);
1246
1247   return TRUE;
1248
1249   /* ERRORS */
1250 no_caps:
1251   {
1252     GST_DEBUG_OBJECT (bsink, "no caps specified");
1253     return FALSE;
1254   }
1255 invalid_caps:
1256   {
1257     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
1258     return FALSE;
1259   }
1260 no_pool:
1261   {
1262     /* Already warned in create_pool */
1263     return FALSE;
1264   }
1265 }
1266
1267 static void
1268 sync_handler (gint fd, guint frame, guint sec, guint usec, gpointer data)
1269 {
1270   gboolean *waiting;
1271
1272   waiting = data;
1273   *waiting = FALSE;
1274 }
1275
1276 static gboolean
1277 gst_kms_sink_sync (GstKMSSink * self)
1278 {
1279   gint ret;
1280   gboolean waiting;
1281   drmEventContext evctxt = {
1282     .version = DRM_EVENT_CONTEXT_VERSION,
1283     .page_flip_handler = sync_handler,
1284     .vblank_handler = sync_handler,
1285   };
1286   drmVBlank vbl = {
1287     .request = {
1288           .type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1289           .sequence = 1,
1290           .signal = (gulong) & waiting,
1291         },
1292   };
1293
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;
1298
1299   waiting = TRUE;
1300   if (!self->has_async_page_flip && !self->modesetting_enabled) {
1301     ret = drmWaitVBlank (self->fd, &vbl);
1302     if (ret)
1303       goto vblank_failed;
1304   } else {
1305     ret = drmModePageFlip (self->fd, self->crtc_id, self->buffer_id,
1306         DRM_MODE_PAGE_FLIP_EVENT, &waiting);
1307     if (ret)
1308       goto pageflip_failed;
1309   }
1310
1311   while (waiting) {
1312     do {
1313       ret = gst_poll_wait (self->poll, 3 * GST_SECOND);
1314     } while (ret == -1 && (errno == EAGAIN || errno == EINTR));
1315
1316     ret = drmHandleEvent (self->fd, &evctxt);
1317     if (ret)
1318       goto event_failed;
1319   }
1320
1321   return TRUE;
1322
1323   /* ERRORS */
1324 vblank_failed:
1325   {
1326     GST_WARNING_OBJECT (self, "drmWaitVBlank failed: %s (%d)",
1327         g_strerror (errno), errno);
1328     return FALSE;
1329   }
1330 pageflip_failed:
1331   {
1332     GST_WARNING_OBJECT (self, "drmModePageFlip failed: %s (%d)",
1333         g_strerror (errno), errno);
1334     return FALSE;
1335   }
1336 event_failed:
1337   {
1338     GST_ERROR_OBJECT (self, "drmHandleEvent failed: %s (%d)",
1339         g_strerror (errno), errno);
1340     return FALSE;
1341   }
1342 }
1343
1344 static gboolean
1345 gst_kms_sink_import_dmabuf (GstKMSSink * self, GstBuffer * inbuf,
1346     GstBuffer ** outbuf)
1347 {
1348   gint prime_fds[GST_VIDEO_MAX_PLANES] = { 0, };
1349   GstVideoMeta *meta;
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];
1355
1356   if (!self->has_prime_import)
1357     return FALSE;
1358
1359   /* This will eliminate most non-dmabuf out there */
1360   if (!gst_is_dmabuf_memory (gst_buffer_peek_memory (inbuf, 0)))
1361     return FALSE;
1362
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);
1366
1367   GST_TRACE_OBJECT (self, "Found a dmabuf with %u planes and %u memories",
1368       n_planes, n_mem);
1369
1370   /* We cannot have multiple dmabuf per plane */
1371   if (n_mem > n_planes)
1372     return FALSE;
1373   g_assert (n_planes != 0);
1374
1375   /* Update video info based on video meta */
1376   if (meta) {
1377     GST_VIDEO_INFO_WIDTH (&self->vinfo) = meta->width;
1378     GST_VIDEO_INFO_HEIGHT (&self->vinfo) = meta->height;
1379
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];
1383     }
1384   }
1385
1386   /* Find and validate all memories */
1387   for (i = 0; i < n_planes; i++) {
1388     guint length;
1389
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]))
1393       return FALSE;
1394
1395     mems[i] = gst_buffer_peek_memory (inbuf, mems_idx[i]);
1396
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;
1400
1401     /* And all memory found must be dmabuf */
1402     if (!gst_is_dmabuf_memory (mems[i]))
1403       return FALSE;
1404   }
1405
1406   kmsmem = (GstKMSMemory *) gst_kms_allocator_get_cached (mems[0]);
1407   if (kmsmem) {
1408     GST_LOG_OBJECT (self, "found KMS mem %p in DMABuf mem %p with fb id = %d",
1409         kmsmem, mems[0], kmsmem->fb_id);
1410     goto wrap_mem;
1411   }
1412
1413   for (i = 0; i < n_planes; i++)
1414     prime_fds[i] = gst_dmabuf_memory_get_fd (mems[i]);
1415
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]);
1418
1419   kmsmem = gst_kms_allocator_dmabuf_import (self->allocator,
1420       prime_fds, n_planes, mems_skip, &self->vinfo);
1421   if (!kmsmem)
1422     return FALSE;
1423
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));
1427
1428 wrap_mem:
1429   *outbuf = gst_buffer_new ();
1430   if (!*outbuf)
1431     return FALSE;
1432   gst_buffer_append_memory (*outbuf, gst_memory_ref (GST_MEMORY_CAST (kmsmem)));
1433   gst_buffer_add_parent_buffer_meta (*outbuf, inbuf);
1434
1435   return TRUE;
1436 }
1437
1438 static GstBuffer *
1439 gst_kms_sink_copy_to_dumb_buffer (GstKMSSink * self, GstBuffer * inbuf)
1440 {
1441   GstFlowReturn ret;
1442   GstVideoFrame inframe, outframe;
1443   gboolean success;
1444   GstBuffer *buf = NULL;
1445
1446   if (!gst_buffer_pool_set_active (self->pool, TRUE))
1447     goto activate_pool_failed;
1448
1449   ret = gst_buffer_pool_acquire_buffer (self->pool, &buf, NULL);
1450   if (ret != GST_FLOW_OK)
1451     goto create_buffer_failed;
1452
1453   if (!gst_video_frame_map (&inframe, &self->vinfo, inbuf, GST_MAP_READ))
1454     goto error_map_src_buffer;
1455
1456   if (!gst_video_frame_map (&outframe, &self->vinfo, buf, GST_MAP_WRITE))
1457     goto error_map_dst_buffer;
1458
1459   success = gst_video_frame_copy (&outframe, &inframe);
1460   gst_video_frame_unmap (&outframe);
1461   gst_video_frame_unmap (&inframe);
1462   if (!success)
1463     goto error_copy_buffer;
1464
1465   return buf;
1466
1467 bail:
1468   {
1469     if (buf)
1470       gst_buffer_unref (buf);
1471     return NULL;
1472   }
1473
1474   /* ERRORS */
1475 activate_pool_failed:
1476   {
1477     GST_ELEMENT_ERROR (self, STREAM, FAILED, ("failed to activate buffer pool"),
1478         ("failed to activate buffer pool"));
1479     return NULL;
1480   }
1481 create_buffer_failed:
1482   {
1483     GST_ELEMENT_ERROR (self, STREAM, FAILED, ("allocation failed"),
1484         ("failed to create buffer"));
1485     return NULL;
1486   }
1487 error_copy_buffer:
1488   {
1489     GST_WARNING_OBJECT (self, "failed to upload buffer");
1490     goto bail;
1491   }
1492 error_map_dst_buffer:
1493   {
1494     gst_video_frame_unmap (&inframe);
1495     /* fall-through */
1496   }
1497 error_map_src_buffer:
1498   {
1499     GST_WARNING_OBJECT (self, "failed to map buffer");
1500     goto bail;
1501   }
1502 }
1503
1504 static GstBuffer *
1505 gst_kms_sink_get_input_buffer (GstKMSSink * self, GstBuffer * inbuf)
1506 {
1507   GstMemory *mem;
1508   GstBuffer *buf = NULL;
1509
1510   mem = gst_buffer_peek_memory (inbuf, 0);
1511   if (!mem)
1512     return NULL;
1513
1514   if (gst_is_kms_memory (mem))
1515     return gst_buffer_ref (inbuf);
1516
1517   if (gst_kms_sink_import_dmabuf (self, inbuf, &buf))
1518     goto done;
1519
1520   GST_CAT_INFO_OBJECT (CAT_PERFORMANCE, self, "frame copy");
1521   buf = gst_kms_sink_copy_to_dumb_buffer (self, inbuf);
1522
1523 done:
1524   /* Copy all the non-memory related metas, this way CropMeta will be
1525    * available upon GstVideoOverlay::expose calls. */
1526   if (buf)
1527     gst_buffer_copy_into (buf, inbuf, GST_BUFFER_COPY_METADATA, 0, -1);
1528
1529   return buf;
1530 }
1531
1532 static GstFlowReturn
1533 gst_kms_sink_show_frame (GstVideoSink * vsink, GstBuffer * buf)
1534 {
1535   gint ret;
1536   GstBuffer *buffer = NULL;
1537   guint32 fb_id;
1538   GstKMSSink *self;
1539   GstVideoCropMeta *crop;
1540   GstVideoRectangle src = { 0, };
1541   GstVideoRectangle dst = { 0, };
1542   GstVideoRectangle result;
1543   GstFlowReturn res;
1544
1545   self = GST_KMS_SINK (vsink);
1546
1547   res = GST_FLOW_ERROR;
1548
1549   if (buf)
1550     buffer = gst_kms_sink_get_input_buffer (self, buf);
1551   else if (self->last_buffer)
1552     buffer = gst_buffer_ref (self->last_buffer);
1553
1554   /* Make sure buf is not used accidentally */
1555   buf = NULL;
1556
1557   if (!buffer)
1558     return GST_FLOW_ERROR;
1559   fb_id = gst_kms_memory_get_fb_id (gst_buffer_peek_memory (buffer, 0));
1560   if (fb_id == 0)
1561     goto buffer_invalid;
1562
1563   GST_TRACE_OBJECT (self, "displaying fb %d", fb_id);
1564
1565   GST_OBJECT_LOCK (self);
1566   if (self->modesetting_enabled) {
1567     self->buffer_id = fb_id;
1568     goto sync_frame;
1569   }
1570
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;
1575
1576     if (!gst_kms_sink_calculate_display_ratio (self, &vinfo))
1577       goto no_disp_ratio;
1578
1579     src.x = crop->x;
1580     src.y = crop->y;
1581   }
1582
1583   src.w = GST_VIDEO_SINK_WIDTH (self);
1584   src.h = GST_VIDEO_SINK_HEIGHT (self);
1585
1586   dst.w = self->render_rect.w;
1587   dst.h = self->render_rect.h;
1588
1589 retry_set_plane:
1590   gst_video_sink_center_rect (src, dst, &result, self->can_scale);
1591
1592   result.x += self->render_rect.x;
1593   result.y += self->render_rect.y;
1594
1595   if (crop) {
1596     src.w = crop->width;
1597     src.h = crop->height;
1598   } else {
1599     src.w = GST_VIDEO_INFO_WIDTH (&self->vinfo);
1600     src.h = GST_VIDEO_INFO_HEIGHT (&self->vinfo);
1601   }
1602
1603   /* handle out of screen case */
1604   if ((result.x + result.w) > self->hdisplay)
1605     result.w = self->hdisplay - result.x;
1606
1607   if ((result.y + result.h) > self->vdisplay)
1608     result.h = self->vdisplay - result.y;
1609
1610   if (result.w <= 0 || result.h <= 0) {
1611     GST_WARNING_OBJECT (self, "video is out of display range");
1612     goto sync_frame;
1613   }
1614
1615   /* to make sure it can be show when driver don't support scale */
1616   if (!self->can_scale) {
1617     src.w = result.w;
1618     src.h = result.h;
1619   }
1620
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);
1624
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);
1629   if (ret) {
1630     if (self->can_scale) {
1631       self->can_scale = FALSE;
1632       goto retry_set_plane;
1633     }
1634     goto set_plane_failed;
1635   }
1636
1637 sync_frame:
1638   /* Wait for the previous frame to complete redraw */
1639   if (!gst_kms_sink_sync (self)) {
1640     GST_OBJECT_UNLOCK (self);
1641     goto bail;
1642   }
1643
1644   if (buffer != self->last_buffer)
1645     gst_buffer_replace (&self->last_buffer, buffer);
1646   g_clear_pointer (&self->tmp_kmsmem, gst_memory_unref);
1647
1648   GST_OBJECT_UNLOCK (self);
1649   res = GST_FLOW_OK;
1650
1651 bail:
1652   gst_buffer_unref (buffer);
1653   return res;
1654
1655   /* ERRORS */
1656 buffer_invalid:
1657   {
1658     GST_ERROR_OBJECT (self, "invalid buffer: it doesn't have a fb id");
1659     goto bail;
1660   }
1661 set_plane_failed:
1662   {
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,
1667         dst.h);
1668     GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
1669         (NULL), ("drmModeSetPlane failed: %s (%d)", g_strerror (errno), errno));
1670     goto bail;
1671   }
1672 no_disp_ratio:
1673   {
1674     GST_OBJECT_UNLOCK (self);
1675     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1676         ("Error calculating the output display ratio of the video."));
1677     goto bail;
1678   }
1679 }
1680
1681 static void
1682 gst_kms_sink_drain (GstKMSSink * self)
1683 {
1684   GstParentBufferMeta *parent_meta;
1685
1686   GST_DEBUG_OBJECT (self, "draining");
1687
1688   if (!self->last_buffer)
1689     return;
1690
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);
1694   if (parent_meta) {
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);
1700   }
1701 }
1702
1703 static gboolean
1704 gst_kms_sink_query (GstBaseSink * bsink, GstQuery * query)
1705 {
1706   GstKMSSink *self = GST_KMS_SINK (bsink);
1707
1708   switch (GST_QUERY_TYPE (query)) {
1709     case GST_QUERY_ALLOCATION:
1710     case GST_QUERY_DRAIN:
1711     {
1712       gst_kms_sink_drain (self);
1713       break;
1714     }
1715     default:
1716       break;
1717   }
1718
1719   return GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
1720 }
1721
1722 static void
1723 gst_kms_sink_set_property (GObject * object, guint prop_id,
1724     const GValue * value, GParamSpec * pspec)
1725 {
1726   GstKMSSink *sink;
1727
1728   sink = GST_KMS_SINK (object);
1729
1730   switch (prop_id) {
1731     case PROP_DRIVER_NAME:
1732       g_free (sink->devname);
1733       sink->devname = g_value_dup_string (value);
1734       break;
1735     case PROP_BUS_ID:
1736       g_free (sink->bus_id);
1737       sink->bus_id = g_value_dup_string (value);
1738       break;
1739     case PROP_CONNECTOR_ID:
1740       sink->conn_id = g_value_get_int (value);
1741       break;
1742     case PROP_PLANE_ID:
1743       sink->plane_id = g_value_get_int (value);
1744       break;
1745     case PROP_FORCE_MODESETTING:
1746       sink->modesetting_enabled = g_value_get_boolean (value);
1747       break;
1748     case PROP_RESTORE_CRTC:
1749       sink->restore_crtc = g_value_get_boolean (value);
1750       break;
1751     case PROP_CAN_SCALE:
1752       sink->can_scale = g_value_get_boolean (value);
1753       break;
1754     case PROP_CONNECTOR_PROPS:{
1755       const GstStructure *s = gst_value_get_structure (value);
1756
1757       g_clear_pointer (&sink->connector_props, gst_structure_free);
1758
1759       if (s)
1760         sink->connector_props = gst_structure_copy (s);
1761
1762       break;
1763     }
1764     case PROP_PLANE_PROPS:{
1765       const GstStructure *s = gst_value_get_structure (value);
1766
1767       g_clear_pointer (&sink->plane_props, gst_structure_free);
1768
1769       if (s)
1770         sink->plane_props = gst_structure_copy (s);
1771
1772       break;
1773     }
1774     default:
1775       if (!gst_video_overlay_set_property (object, PROP_N, prop_id, value))
1776         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1777       break;
1778   }
1779 }
1780
1781 static void
1782 gst_kms_sink_get_property (GObject * object, guint prop_id,
1783     GValue * value, GParamSpec * pspec)
1784 {
1785   GstKMSSink *sink;
1786
1787   sink = GST_KMS_SINK (object);
1788
1789   switch (prop_id) {
1790     case PROP_DRIVER_NAME:
1791       g_value_set_string (value, sink->devname);
1792       break;
1793     case PROP_BUS_ID:
1794       g_value_set_string (value, sink->bus_id);
1795       break;
1796     case PROP_CONNECTOR_ID:
1797       g_value_set_int (value, sink->conn_id);
1798       break;
1799     case PROP_PLANE_ID:
1800       g_value_set_int (value, sink->plane_id);
1801       break;
1802     case PROP_FORCE_MODESETTING:
1803       g_value_set_boolean (value, sink->modesetting_enabled);
1804       break;
1805     case PROP_RESTORE_CRTC:
1806       g_value_set_boolean (value, sink->restore_crtc);
1807       break;
1808     case PROP_CAN_SCALE:
1809       g_value_set_boolean (value, sink->can_scale);
1810       break;
1811     case PROP_DISPLAY_WIDTH:
1812       GST_OBJECT_LOCK (sink);
1813       g_value_set_int (value, sink->hdisplay);
1814       GST_OBJECT_UNLOCK (sink);
1815       break;
1816     case PROP_DISPLAY_HEIGHT:
1817       GST_OBJECT_LOCK (sink);
1818       g_value_set_int (value, sink->vdisplay);
1819       GST_OBJECT_UNLOCK (sink);
1820       break;
1821     case PROP_CONNECTOR_PROPS:
1822       gst_value_set_structure (value, sink->connector_props);
1823       break;
1824     case PROP_PLANE_PROPS:
1825       gst_value_set_structure (value, sink->plane_props);
1826       break;
1827     default:
1828       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1829       break;
1830   }
1831 }
1832
1833 static void
1834 gst_kms_sink_finalize (GObject * object)
1835 {
1836   GstKMSSink *sink;
1837
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);
1845
1846   G_OBJECT_CLASS (parent_class)->finalize (object);
1847 }
1848
1849 static void
1850 gst_kms_sink_init (GstKMSSink * sink)
1851 {
1852   sink->fd = -1;
1853   sink->conn_id = -1;
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);
1859 }
1860
1861 static void
1862 gst_kms_sink_class_init (GstKMSSinkClass * klass)
1863 {
1864   GObjectClass *gobject_class;
1865   GstElementClass *element_class;
1866   GstBaseSinkClass *basesink_class;
1867   GstVideoSinkClass *videosink_class;
1868   GstCaps *caps;
1869
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);
1874
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>");
1877
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);
1882
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;
1889
1890   videosink_class->show_frame = gst_kms_sink_show_frame;
1891
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;
1895
1896   /**
1897    * kmssink:driver-name:
1898    *
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.
1902    */
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);
1906
1907   /**
1908    * kmssink:bus-id:
1909    *
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.
1913    */
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);
1917
1918   /**
1919    * kmssink:connector-id:
1920    *
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.
1924    */
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);
1928
1929    /**
1930    * kmssink:plane-id:
1931    *
1932    * There could be several planes associated with a CRTC.
1933    * By default the first plane that's possible to use with a given
1934    * CRTC is tried.
1935    */
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);
1939
1940   /**
1941    * kmssink:force-modesetting:
1942    *
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.
1946    */
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);
1951
1952   /**
1953    * kmssink:restore-crtc:
1954    *
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.
1958    */
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);
1964
1965   /**
1966    * kmssink:can-scale:
1967    *
1968    * User can tell kmssink if the driver can support scale.
1969    */
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);
1974
1975   /**
1976    * kmssink:display-width
1977    *
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.
1981    */
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);
1986
1987   /**
1988    * kmssink:display-height
1989    *
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.
1993    */
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);
1998
1999   /**
2000    * kmssink:connector-properties:
2001    *
2002    * Additional properties for the connector. Keys are strings and values
2003    * unsigned 64 bits integers.
2004    *
2005    * Since: 1.16
2006    */
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);
2011
2012   /**
2013    * kmssink:plane-properties:
2014    *
2015    * Additional properties for the plane. Keys are strings and values
2016    * unsigned 64 bits integers.
2017    *
2018    * Since: 1.16
2019    */
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);
2024
2025   g_object_class_install_properties (gobject_class, PROP_N, g_properties);
2026
2027   gst_video_overlay_install_properties (gobject_class, PROP_N);
2028 }
2029
2030 static gboolean
2031 plugin_init (GstPlugin * plugin)
2032 {
2033   if (!gst_element_register (plugin, GST_PLUGIN_NAME, GST_RANK_SECONDARY,
2034           GST_TYPE_KMS_SINK))
2035     return FALSE;
2036
2037   return TRUE;
2038 }
2039
2040 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, kms,
2041     GST_PLUGIN_DESC, plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
2042     GST_PACKAGE_ORIGIN)