Add new video interface for id of wl_surface
[platform/upstream/gstreamer.git] / ext / wayland / gstwaylandsink.c
1 /* GStreamer Wayland video sink
2  *
3  * Copyright (C) 2011 Intel Corporation
4  * Copyright (C) 2011 Sreerenj Balachandran <sreerenj.balachandran@intel.com>
5  * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.com>
6  * Copyright (C) 2014 Collabora Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * SECTION:element-waylandsink
26  *
27  *  The waylandsink is creating its own window and render the decoded video frames to that.
28  *  Setup the Wayland environment as described in
29  *  <ulink url="http://wayland.freedesktop.org/building.html">Wayland</ulink> home page.
30  *  The current implementaion is based on weston compositor.
31  *
32  * <refsect2>
33  * <title>Example pipelines</title>
34  * |[
35  * gst-launch -v videotestsrc ! waylandsink
36  * ]| test the video rendering in wayland
37  * </refsect2>
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include "gstwaylandsink.h"
45 #ifdef GST_WLSINK_ENHANCEMENT
46 #include <mm_types.h>
47 #endif
48 #include "wlvideoformat.h"
49 #include "wlbuffer.h"
50 #include "wlshmallocator.h"
51
52 #include <gst/wayland/wayland.h>
53 #include <gst/video/videooverlay.h>
54
55
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59
60 //#define DUMP_BUFFER
61 #ifdef GST_WLSINK_ENHANCEMENT
62 #define GST_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD (gst_waylandsink_display_geometry_method_get_type())
63 #define GST_TYPE_WAYLANDSINK_ROTATE_ANGLE (gst_waylandsink_rotate_angle_get_type())
64 #define GST_TYPE_WAYLANDSINK_FLIP (gst_waylandsink_flip_get_type())
65
66 static GType
67 gst_waylandsink_rotate_angle_get_type (void)
68 {
69   static GType waylandsink_rotate_angle_type = 0;
70   static const GEnumValue rotate_angle_type[] = {
71     {0, "No rotate", "DEGREE_0"},
72     {1, "Rotate 90 degree", "DEGREE_90"},
73     {2, "Rotate 180 degree", "DEGREE_180"},
74     {3, "Rotate 270 degree", "DEGREE_270"},
75     {4, NULL, NULL},
76   };
77
78   if (!waylandsink_rotate_angle_type) {
79     waylandsink_rotate_angle_type =
80         g_enum_register_static ("GstWaylandSinkRotateAngleType",
81         rotate_angle_type);
82   }
83
84   return waylandsink_rotate_angle_type;
85 }
86
87
88 static GType
89 gst_waylandsink_display_geometry_method_get_type (void)
90 {
91   static GType waylandsink_display_geometry_method_type = 0;
92   static const GEnumValue display_geometry_method_type[] = {
93     {0, "Letter box", "LETTER_BOX"},
94     {1, "Origin size", "ORIGIN_SIZE"},
95     {2, "Full-screen", "FULL_SCREEN"},
96     {3, "Cropped full-screen", "CROPPED_FULL_SCREEN"},
97     {4, "Origin size(if screen size is larger than video size(width/height)) or Letter box(if video size(width/height) is larger than screen size)", "ORIGIN_SIZE_OR_LETTER_BOX"},
98     {5, NULL, NULL},
99   };
100
101   if (!waylandsink_display_geometry_method_type) {
102     waylandsink_display_geometry_method_type =
103         g_enum_register_static ("GstWaylandSinkDisplayGeometryMethodType",
104         display_geometry_method_type);
105   }
106   return waylandsink_display_geometry_method_type;
107 }
108
109 static GType
110 gst_waylandsink_flip_get_type (void)
111 {
112   static GType waylandsink_flip_type = 0;
113   static const GEnumValue flip_type[] = {
114     {FLIP_NONE, "Flip NONE", "FLIP_NONE"},
115     {FLIP_HORIZONTAL, "Flip HORIZONTAL", "FLIP_HORIZONTAL"},
116     {FLIP_VERTICAL, "Flip VERTICAL", "FLIP_VERTICAL"},
117     {FLIP_BOTH, "Flip BOTH", "FLIP_BOTH"},
118     {FLIP_NUM, NULL, NULL},
119   };
120
121   if (!waylandsink_flip_type) {
122     waylandsink_flip_type =
123         g_enum_register_static ("GstWaylandSinkFlipType", flip_type);
124   }
125
126   return waylandsink_flip_type;
127 }
128
129 #endif
130
131 /* signals */
132 enum
133 {
134   SIGNAL_0,
135   LAST_SIGNAL
136 };
137
138 /* Properties */
139 enum
140 {
141   PROP_0,
142   PROP_DISPLAY,
143 #ifdef GST_WLSINK_ENHANCEMENT
144   PROP_USE_TBM,
145   PROP_ROTATE_ANGLE,
146   PROP_DISPLAY_GEOMETRY_METHOD,
147   PROP_ORIENTATION,
148   PROP_FLIP
149 #endif
150 };
151 int dump__cnt = 0;
152
153 GST_DEBUG_CATEGORY (gstwayland_debug);
154 #define GST_CAT_DEFAULT gstwayland_debug
155
156 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
157     GST_PAD_SINK,
158     GST_PAD_ALWAYS,
159     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
160         ("{ BGRx, BGRA, RGBx, xBGR, xRGB, RGBA, ABGR, ARGB, RGB, BGR, "
161             "RGB16, BGR16, YUY2, YVYU, UYVY, AYUV, NV12, NV21, NV16, "
162 #ifdef GST_WLSINK_ENHANCEMENT
163             "SN12, ST12, "
164 #endif
165             "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"))
166     );
167
168 static void gst_wayland_sink_get_property (GObject * object,
169     guint prop_id, GValue * value, GParamSpec * pspec);
170 static void gst_wayland_sink_set_property (GObject * object,
171     guint prop_id, const GValue * value, GParamSpec * pspec);
172 static void gst_wayland_sink_finalize (GObject * object);
173
174 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
175     GstStateChange transition);
176 static void gst_wayland_sink_set_context (GstElement * element,
177     GstContext * context);
178
179 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
180     GstCaps * filter);
181 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
182 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
183     GstBuffer * buffer);
184 static gboolean
185 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
186 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
187     GstBuffer * buffer);
188
189 /* VideoOverlay interface */
190 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
191     iface);
192 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
193     guintptr handle);
194 static void
195 gst_wayland_sink_set_wl_window_wl_surface_id (GstVideoOverlay * overlay,
196     guintptr wl_surface_id);
197 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
198     gint x, gint y, gint w, gint h);
199 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
200
201 /* WaylandVideo interface */
202 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
203     iface);
204 static void gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video);
205 static void gst_wayland_sink_end_geometry_change (GstWaylandVideo * video);
206 #ifdef GST_WLSINK_ENHANCEMENT
207 static void gst_wayland_sink_update_window_geometry (GstWaylandSink * sink);
208 static void render_last_buffer (GstWaylandSink * sink);
209 #endif
210 #define gst_wayland_sink_parent_class parent_class
211 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
212     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
213         gst_wayland_sink_videooverlay_init)
214     G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
215         gst_wayland_sink_waylandvideo_init));
216
217 static void
218 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
219 {
220   FUNCTION;
221   GObjectClass *gobject_class;
222   GstElementClass *gstelement_class;
223   GstBaseSinkClass *gstbasesink_class;
224
225   gobject_class = (GObjectClass *) klass;
226   gstelement_class = (GstElementClass *) klass;
227   gstbasesink_class = (GstBaseSinkClass *) klass;
228
229   gobject_class->set_property = gst_wayland_sink_set_property;
230   gobject_class->get_property = gst_wayland_sink_get_property;
231   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
232
233   gst_element_class_add_pad_template (gstelement_class,
234       gst_static_pad_template_get (&sink_template));
235
236   gst_element_class_set_static_metadata (gstelement_class,
237       "wayland video sink", "Sink/Video",
238       "Output to wayland surface",
239       "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
240       "George Kiagiadakis <george.kiagiadakis@collabora.com>");
241
242   gstelement_class->change_state =
243       GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
244   gstelement_class->set_context =
245       GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
246
247   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
248   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
249   gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
250   gstbasesink_class->propose_allocation =
251       GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
252   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
253
254   g_object_class_install_property (gobject_class, PROP_DISPLAY,
255       g_param_spec_string ("display", "Wayland Display name", "Wayland "
256           "display name to connect to, if not supplied via the GstContext",
257           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258 #ifdef GST_WLSINK_ENHANCEMENT
259   g_object_class_install_property (gobject_class, PROP_USE_TBM,
260       g_param_spec_boolean ("use-tbm",
261           "Use Tizen Buffer Memory insted of Shared memory",
262           "When enabled, Memory is alloced by TBM insted of SHM ", TRUE,
263           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
264
265   g_object_class_install_property (gobject_class, PROP_ROTATE_ANGLE,
266       g_param_spec_enum ("rotate", "Rotate angle",
267           "Rotate angle of display output",
268           GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
269           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270
271   g_object_class_install_property (gobject_class, PROP_DISPLAY_GEOMETRY_METHOD,
272       g_param_spec_enum ("display-geometry-method", "Display geometry method",
273           "Geometrical method for display",
274           GST_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD,
275           DEF_DISPLAY_GEOMETRY_METHOD,
276           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
277
278   g_object_class_install_property (gobject_class, PROP_ORIENTATION,
279       g_param_spec_enum ("orientation",
280           "Orientation information used for ROI/ZOOM",
281           "Orientation information for display",
282           GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
283           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284
285   g_object_class_install_property (gobject_class, PROP_FLIP,
286       g_param_spec_enum ("flip", "Display flip",
287           "Flip for display",
288           GST_TYPE_WAYLANDSINK_FLIP, DEF_DISPLAY_FLIP,
289           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
290
291 #endif
292 }
293
294 #ifdef DUMP_BUFFER
295 int
296 __write_rawdata (const char *file, const void *data, unsigned int size)
297 {
298   FILE *fp;
299
300   fp = fopen (file, "wb");
301   if (fp == NULL)
302     return -1;
303
304   fwrite ((char *) data, sizeof (char), size, fp);
305   fclose (fp);
306
307   return 0;
308 }
309 #endif
310 static void
311 gst_wayland_sink_init (GstWaylandSink * sink)
312 {
313   FUNCTION;
314 #ifdef GST_WLSINK_ENHANCEMENT
315   sink->USE_TBM = TRUE;
316   sink->display_geometry_method = DEF_DISPLAY_GEOMETRY_METHOD;
317   sink->flip = DEF_DISPLAY_FLIP;
318   sink->rotate_angle = DEGREE_0;
319   sink->orientation = DEGREE_0;
320 #endif
321   g_mutex_init (&sink->display_lock);
322   g_mutex_init (&sink->render_lock);
323 }
324
325 static void
326 gst_wayland_sink_get_property (GObject * object,
327     guint prop_id, GValue * value, GParamSpec * pspec)
328 {
329   FUNCTION;
330   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
331
332   switch (prop_id) {
333     case PROP_DISPLAY:
334       GST_OBJECT_LOCK (sink);
335       g_value_set_string (value, sink->display_name);
336       GST_OBJECT_UNLOCK (sink);
337       break;
338 #ifdef GST_WLSINK_ENHANCEMENT
339     case PROP_USE_TBM:
340       g_value_set_boolean (value, sink->USE_TBM);
341       break;
342     case PROP_ROTATE_ANGLE:
343       g_value_set_enum (value, sink->rotate_angle);
344       break;
345     case PROP_DISPLAY_GEOMETRY_METHOD:
346       g_value_set_enum (value, sink->display_geometry_method);
347       break;
348     case PROP_ORIENTATION:
349       g_value_set_enum (value, sink->orientation);
350       break;
351     case PROP_FLIP:
352       g_value_set_enum (value, sink->flip);
353       break;
354 #endif
355     default:
356       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
357       break;
358   }
359 }
360
361 static void
362 gst_wayland_sink_set_property (GObject * object,
363     guint prop_id, const GValue * value, GParamSpec * pspec)
364 {
365   FUNCTION;
366   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
367
368   switch (prop_id) {
369     case PROP_DISPLAY:
370       GST_OBJECT_LOCK (sink);
371       sink->display_name = g_value_dup_string (value);
372       GST_OBJECT_UNLOCK (sink);
373       break;
374 #ifdef GST_WLSINK_ENHANCEMENT
375     case PROP_USE_TBM:
376       sink->USE_TBM = g_value_get_boolean (value);
377       GST_LOG ("1:USE TBM 0: USE SHM set(%d)", sink->USE_TBM);
378       break;
379     case PROP_ROTATE_ANGLE:
380       sink->rotate_angle = g_value_get_enum (value);
381       GST_WARNING_OBJECT (sink, "Rotate angle is set (%d)", sink->rotate_angle);
382       if (sink->window) {
383         gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
384       }
385       sink->video_info_changed = TRUE;
386       if (GST_STATE (sink) == GST_STATE_PAUSED) {
387         /*need to render last buffer */
388         g_mutex_lock (&sink->render_lock);
389         render_last_buffer (sink);
390         g_mutex_unlock (&sink->render_lock);
391       }
392       break;
393     case PROP_DISPLAY_GEOMETRY_METHOD:
394       sink->display_geometry_method = g_value_get_enum (value);
395       GST_WARNING_OBJECT (sink, "Display geometry method is set (%d)",
396           sink->display_geometry_method);
397       if (sink->window) {
398         gst_wl_window_set_disp_geo_method (sink->window,
399             sink->display_geometry_method);
400       }
401       sink->video_info_changed = TRUE;
402       if (GST_STATE (sink) == GST_STATE_PAUSED) {
403         /*need to render last buffer */
404         g_mutex_lock (&sink->render_lock);
405         render_last_buffer (sink);
406         g_mutex_unlock (&sink->render_lock);
407       }
408       break;
409     case PROP_ORIENTATION:
410       sink->orientation = g_value_get_enum (value);
411       GST_WARNING_OBJECT (sink, "Orientation is set (%d)", sink->orientation);
412       if (sink->window) {
413         gst_wl_window_set_orientation (sink->window, sink->orientation);
414       }
415       sink->video_info_changed = TRUE;
416       if (GST_STATE (sink) == GST_STATE_PAUSED) {
417         /*need to render last buffer */
418         g_mutex_lock (&sink->render_lock);
419         render_last_buffer (sink);
420         g_mutex_unlock (&sink->render_lock);
421       }
422       break;
423     case PROP_FLIP:
424       sink->flip = g_value_get_enum (value);
425       GST_WARNING_OBJECT (sink, "flip is set (%d)", sink->flip);
426       if (sink->flip) {
427         gst_wl_window_set_flip (sink->window, sink->flip);
428       }
429       sink->video_info_changed = TRUE;
430       if (GST_STATE (sink) == GST_STATE_PAUSED) {
431         /*need to render last buffer */
432         g_mutex_lock (&sink->render_lock);
433         render_last_buffer (sink);
434         g_mutex_unlock (&sink->render_lock);
435       }
436       break;
437 #endif
438     default:
439       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
440       break;
441   }
442 }
443
444 static void
445 gst_wayland_sink_finalize (GObject * object)
446 {
447   FUNCTION;
448   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
449
450   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
451
452   if (sink->last_buffer)
453     gst_buffer_unref (sink->last_buffer);
454   if (sink->display)
455     g_object_unref (sink->display);
456   if (sink->window)
457     g_object_unref (sink->window);
458   if (sink->pool)
459     gst_object_unref (sink->pool);
460
461   if (sink->display_name)
462     g_free (sink->display_name);
463
464   g_mutex_clear (&sink->display_lock);
465   g_mutex_clear (&sink->render_lock);
466
467   G_OBJECT_CLASS (parent_class)->finalize (object);
468 }
469
470 /* must be called with the display_lock */
471 static void
472 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
473     GstContext * context)
474 {
475   FUNCTION;
476   struct wl_display *display;
477   GError *error = NULL;
478
479   display = gst_wayland_display_handle_context_get_handle (context);
480   sink->display = gst_wl_display_new_existing (display, FALSE, &error);
481
482   if (error) {
483     GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
484         ("Could not set display handle"),
485         ("Failed to use the external wayland display: '%s'", error->message));
486     g_error_free (error);
487   }
488 #ifdef GST_WLSINK_ENHANCEMENT
489   sink->display->USE_TBM = sink->USE_TBM;
490 #endif
491 }
492
493 static gboolean
494 gst_wayland_sink_find_display (GstWaylandSink * sink)
495 {
496   FUNCTION;
497   GstQuery *query;
498   GstMessage *msg;
499   GstContext *context = NULL;
500   GError *error = NULL;
501   gboolean ret = TRUE;
502
503   g_mutex_lock (&sink->display_lock);
504
505   if (!sink->display) {
506     /* first query upstream for the needed display handle */
507     query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
508     if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
509       gst_query_parse_context (query, &context);
510       gst_wayland_sink_set_display_from_context (sink, context);
511     }
512     gst_query_unref (query);
513
514     if (G_LIKELY (!sink->display)) {
515       /* now ask the application to set the display handle */
516       msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
517           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
518
519       g_mutex_unlock (&sink->display_lock);
520       gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
521       /* at this point we expect gst_wayland_sink_set_context
522        * to get called and fill sink->display */
523       g_mutex_lock (&sink->display_lock);
524
525       if (!sink->display) {
526         /* if the application didn't set a display, let's create it ourselves */
527         GST_OBJECT_LOCK (sink);
528         sink->display = gst_wl_display_new (sink->display_name, &error);
529         GST_OBJECT_UNLOCK (sink);
530
531         if (error) {
532           GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
533               ("Could not initialise Wayland output"),
534               ("Failed to create GstWlDisplay: '%s'", error->message));
535           g_error_free (error);
536           ret = FALSE;
537         }
538 #ifdef GST_WLSINK_ENHANCEMENT
539         sink->display->USE_TBM = sink->USE_TBM;
540 #endif
541       }
542     }
543   }
544
545   g_mutex_unlock (&sink->display_lock);
546
547   return ret;
548 }
549
550 static GstStateChangeReturn
551 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
552 {
553   FUNCTION;
554   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
555   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
556
557   switch (transition) {
558     case GST_STATE_CHANGE_NULL_TO_READY:
559       if (!gst_wayland_sink_find_display (sink))
560         return GST_STATE_CHANGE_FAILURE;
561       break;
562     default:
563       break;
564   }
565
566   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
567   if (ret == GST_STATE_CHANGE_FAILURE)
568     return ret;
569
570   switch (transition) {
571     case GST_STATE_CHANGE_PAUSED_TO_READY:
572       gst_buffer_replace (&sink->last_buffer, NULL);
573       if (sink->window) {
574         if (gst_wl_window_is_toplevel (sink->window)) {
575           g_clear_object (&sink->window);
576         } else {
577           /* remove buffer from surface, show nothing */
578           gst_wl_window_render (sink->window, NULL, NULL);
579         }
580       }
581       break;
582     case GST_STATE_CHANGE_READY_TO_NULL:
583       g_mutex_lock (&sink->display_lock);
584       /* If we had a toplevel window, we most likely have our own connection
585        * to the display too, and it is a good idea to disconnect and allow
586        * potentially the application to embed us with GstVideoOverlay
587        * (which requires to re-use the same display connection as the parent
588        * surface). If we didn't have a toplevel window, then the display
589        * connection that we have is definitely shared with the application
590        * and it's better to keep it around (together with the window handle)
591        * to avoid requesting them again from the application if/when we are
592        * restarted (GstVideoOverlay behaves like that in other sinks)
593        */
594       if (sink->display && !sink->window) {     /* -> the window was toplevel */
595         g_clear_object (&sink->display);
596       }
597       g_mutex_unlock (&sink->display_lock);
598       g_clear_object (&sink->pool);
599       break;
600     default:
601       break;
602   }
603
604   return ret;
605 }
606
607 static void
608 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
609 {
610   FUNCTION;
611   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
612
613   if (gst_context_has_context_type (context,
614           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
615     g_mutex_lock (&sink->display_lock);
616     if (G_LIKELY (!sink->display))
617       gst_wayland_sink_set_display_from_context (sink, context);
618     else {
619       GST_WARNING_OBJECT (element, "changing display handle is not supported");
620 #ifdef GST_WLSINK_ENHANCEMENT
621       g_mutex_unlock (&sink->display_lock);
622       return;
623 #endif
624     }
625     g_mutex_unlock (&sink->display_lock);
626   }
627
628   if (GST_ELEMENT_CLASS (parent_class)->set_context)
629     GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
630 }
631
632 static GstCaps *
633 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
634 {
635   FUNCTION;
636   GstWaylandSink *sink;
637   GstCaps *caps;
638
639   sink = GST_WAYLAND_SINK (bsink);
640
641   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
642
643   g_mutex_lock (&sink->display_lock);
644
645   if (sink->display) {
646     GValue list = G_VALUE_INIT;
647     GValue value = G_VALUE_INIT;
648     GArray *formats;
649     gint i;
650 #ifdef GST_WLSINK_ENHANCEMENT
651     uint32_t tbm_fmt;
652 #endif
653     enum wl_shm_format fmt;
654
655     g_value_init (&list, GST_TYPE_LIST);
656     g_value_init (&value, G_TYPE_STRING);
657 #ifdef GST_WLSINK_ENHANCEMENT
658     if (sink->display->USE_TBM)
659       formats = sink->display->tbm_formats;
660     else                        /* SHM */
661 #endif
662       formats = sink->display->formats;
663
664     for (i = 0; i < formats->len; i++) {
665 #ifdef GST_WLSINK_ENHANCEMENT
666       if (sink->USE_TBM) {
667         tbm_fmt = g_array_index (formats, uint32_t, i);
668         g_value_set_string (&value, gst_wl_tbm_format_to_string (tbm_fmt));
669         gst_value_list_append_value (&list, &value);
670         /* TBM doesn't support SN12. So we add SN12 manually as supported format.
671          * SN12 is exactly same with NV12.
672          */
673         if (tbm_fmt == TBM_FORMAT_NV12) {
674           g_value_set_string (&value,
675               gst_video_format_to_string (GST_VIDEO_FORMAT_SN12));
676           gst_value_list_append_value (&list, &value);
677         }
678       } else {                  /* USE SHM */
679         fmt = g_array_index (formats, uint32_t, i);
680         g_value_set_string (&value, gst_wl_shm_format_to_string (fmt));
681         gst_value_list_append_value (&list, &value);
682       }
683 #else /* open source */
684       fmt = g_array_index (formats, uint32_t, i);
685       g_value_set_string (&value, gst_wl_shm_format_to_string (fmt));
686       gst_value_list_append_value (&list, &value);
687 #endif
688     }
689
690     caps = gst_caps_make_writable (caps);
691     gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
692
693     GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
694   }
695
696   g_mutex_unlock (&sink->display_lock);
697
698   if (filter) {
699     GstCaps *intersection;
700
701     intersection =
702         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
703     gst_caps_unref (caps);
704     caps = intersection;
705   }
706
707   return caps;
708 }
709
710 static gboolean
711 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
712 {
713   FUNCTION;
714   GstWaylandSink *sink;
715   GstBufferPool *newpool;
716   GstVideoInfo info;
717 #ifdef GST_WLSINK_ENHANCEMENT
718   uint32_t tbm_format;
719 #endif
720   enum wl_shm_format format;
721
722   GArray *formats;
723   gint i;
724   GstStructure *structure;
725
726   sink = GST_WAYLAND_SINK (bsink);
727
728   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
729
730   /* extract info from caps */
731   if (!gst_video_info_from_caps (&info, caps))
732     goto invalid_format;
733 #ifdef GST_WLSINK_ENHANCEMENT
734   sink->caps = gst_caps_copy (caps);
735   if (sink->USE_TBM) {
736     tbm_format =
737         gst_video_format_to_wl_tbm_format (GST_VIDEO_INFO_FORMAT (&info));
738     if ((gint) tbm_format == -1)
739       goto invalid_format;
740   } else {
741     format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info));
742     if ((gint) format == -1)
743       goto invalid_format;
744   }
745 #else /* open source */
746   format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info));
747
748   if ((gint) format == -1)
749     goto invalid_format;
750 #endif
751
752   /* verify we support the requested format */
753 #ifdef GST_WLSINK_ENHANCEMENT
754   if (sink->display->USE_TBM) {
755     GST_ERROR ("USE TBM FORMAT");
756     formats = sink->display->tbm_formats;
757     for (i = 0; i < formats->len; i++) {
758       if (g_array_index (formats, uint32_t, i) == tbm_format)
759         break;
760     }
761   } else {                      /* USE SHM */
762     GST_ERROR ("USE SHM FORMAT");
763     formats = sink->display->formats;
764     for (i = 0; i < formats->len; i++) {
765       if (g_array_index (formats, uint32_t, i) == format)
766         break;
767     }
768   }
769 #else /* open source */
770
771   formats = sink->display->formats;
772   for (i = 0; i < formats->len; i++) {
773     if (g_array_index (formats, uint32_t, i) == format)
774       break;
775   }
776 #endif
777   if (i >= formats->len)
778     goto unsupported_format;
779
780 #ifdef GST_WLSINK_ENHANCEMENT
781   if (sink->USE_TBM) {
782     if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_SN12 ||
783         GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ST12) {
784       sink->display->is_native_format = TRUE;
785
786       /* store the video info */
787       sink->video_info = info;
788       sink->video_info_changed = TRUE;
789     } else {
790       sink->display->is_native_format = FALSE;
791       GstWlShmAllocator *self =
792           GST_WL_SHM_ALLOCATOR (gst_wl_shm_allocator_get ());
793       self->display = sink->display;
794       /* create a new pool for the new configuration */
795       newpool = gst_video_buffer_pool_new ();
796       if (!newpool)
797         goto pool_failed;
798
799       structure = gst_buffer_pool_get_config (newpool);
800       gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
801       gst_buffer_pool_config_set_allocator (structure,
802           gst_wl_shm_allocator_get (), NULL);
803       if (!gst_buffer_pool_set_config (newpool, structure))
804         goto config_failed;
805
806       /* store the video info */
807       sink->video_info = info;
808       sink->video_info_changed = TRUE;
809
810       gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
811       gst_object_unref (newpool);
812     }
813   } else {                      /* USE SHM */
814
815     GstWlShmAllocator *self =
816         GST_WL_SHM_ALLOCATOR (gst_wl_shm_allocator_get ());
817     self->display = sink->display;
818
819     /* create a new pool for the new configuration */
820     newpool = gst_video_buffer_pool_new ();
821     if (!newpool)
822       goto pool_failed;
823
824     structure = gst_buffer_pool_get_config (newpool);
825     gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
826     gst_buffer_pool_config_set_allocator (structure,
827         gst_wl_shm_allocator_get (), NULL);
828     if (!gst_buffer_pool_set_config (newpool, structure))
829       goto config_failed;
830
831     /* store the video info */
832     sink->video_info = info;
833     sink->video_info_changed = TRUE;
834
835     gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
836     gst_object_unref (newpool);
837   }
838 #else /*open source */
839   /* create a new pool for the new configuration */
840   newpool = gst_video_buffer_pool_new ();
841   if (!newpool)
842     goto pool_failed;
843
844   structure = gst_buffer_pool_get_config (newpool);
845   gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
846   gst_buffer_pool_config_set_allocator (structure,
847       gst_wl_shm_allocator_get (), NULL);
848   if (!gst_buffer_pool_set_config (newpool, structure))
849     goto config_failed;
850
851   /* store the video info */
852   sink->video_info = info;
853   sink->video_info_changed = TRUE;
854
855   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
856   gst_object_unref (newpool);
857 #endif
858
859   return TRUE;
860
861 invalid_format:
862   {
863     GST_DEBUG_OBJECT (sink,
864         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
865     return FALSE;
866   }
867 unsupported_format:
868   {
869 #ifdef GST_WLSINK_ENHANCEMENT
870     if (sink->USE_TBM)
871       GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
872           gst_wl_tbm_format_to_string (tbm_format));
873     else                        /*USE SHM */
874       GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
875           gst_wl_shm_format_to_string (format));
876 #else /*open source */
877     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
878         gst_wl_shm_format_to_string (format));
879 #endif
880     return FALSE;
881   }
882 pool_failed:
883   {
884     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
885     return FALSE;
886   }
887 config_failed:
888   {
889     GST_DEBUG_OBJECT (bsink, "failed setting config");
890     gst_object_unref (newpool);
891     return FALSE;
892   }
893 }
894
895 static gboolean
896 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
897 {
898   FUNCTION;
899   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
900   GstStructure *config;
901   guint size, min_bufs, max_bufs;
902 #ifdef GST_WLSINK_ENHANCEMENT
903   gboolean need_pool;
904   GstCaps *caps;
905   if (sink->USE_TBM) {
906     if (sink->display->is_native_format == TRUE)
907       return TRUE;
908
909     gst_query_parse_allocation (query, &caps, &need_pool);
910
911     if (caps == NULL) {
912       GST_DEBUG_OBJECT (bsink, "no caps specified");
913       return FALSE;
914     }
915   }
916 #endif
917   config = gst_buffer_pool_get_config (sink->pool);
918   gst_buffer_pool_config_get_params (config, NULL, &size, &min_bufs, &max_bufs);
919
920   /* we do have a pool for sure (created in set_caps),
921    * so let's propose it anyway, but also propose the allocator on its own */
922   gst_query_add_allocation_pool (query, sink->pool, size, min_bufs, max_bufs);
923   gst_query_add_allocation_param (query, gst_wl_shm_allocator_get (), NULL);
924
925   gst_structure_free (config);
926
927   return TRUE;
928 }
929
930 static GstFlowReturn
931 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
932 {
933   FUNCTION;
934   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
935   return gst_wayland_sink_render (bsink, buffer);
936 }
937
938 static void
939 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
940 {
941   FUNCTION;
942   GstWaylandSink *sink = data;
943
944   GST_LOG ("frame_redraw_cb");
945
946   g_atomic_int_set (&sink->redraw_pending, FALSE);
947   wl_callback_destroy (callback);
948 }
949
950 static const struct wl_callback_listener frame_callback_listener = {
951   frame_redraw_callback
952 };
953
954 #ifdef GST_WLSINK_ENHANCEMENT
955 static void
956 gst_wayland_sink_update_window_geometry (GstWaylandSink * sink)
957 {
958   FUNCTION;
959   g_return_if_fail (sink != NULL);
960   g_return_if_fail (sink->window != NULL);
961
962   gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
963   gst_wl_window_set_disp_geo_method (sink->window,
964       sink->display_geometry_method);
965   gst_wl_window_set_orientation (sink->window, sink->orientation);
966   gst_wl_window_set_flip (sink->window, sink->flip);
967 }
968 #endif
969 /* must be called with the render lock */
970 static void
971 render_last_buffer (GstWaylandSink * sink)
972 {
973   FUNCTION;
974   GstWlBuffer *wlbuffer;
975   const GstVideoInfo *info = NULL;
976   struct wl_surface *surface;
977   struct wl_callback *callback;
978
979   wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
980   surface = gst_wl_window_get_wl_surface (sink->window);
981
982   g_atomic_int_set (&sink->redraw_pending, TRUE);
983   callback = wl_surface_frame (surface);
984   /* frame_callback_listener is called when wayland-client finish rendering the wl_buffer */
985   wl_callback_add_listener (callback, &frame_callback_listener, sink);
986
987   if (G_UNLIKELY (sink->video_info_changed)) {
988     info = &sink->video_info;
989     sink->video_info_changed = FALSE;
990   }
991 #ifdef GST_WLSINK_ENHANCEMENT
992   if (sink->last_buffer)
993     gst_wl_window_render (sink->window, wlbuffer, info);
994   else {
995     if (G_UNLIKELY (info)) {
996       gst_wl_window_set_video_info (sink->window, &info);
997     }
998   }
999 #else
1000   gst_wl_window_render (sink->window, wlbuffer, info);
1001 #endif
1002 }
1003
1004 static GstFlowReturn
1005 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
1006 {
1007   FUNCTION;
1008   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
1009   GstBuffer *to_render;
1010   GstWlBuffer *wlbuffer;
1011   GstFlowReturn ret = GST_FLOW_OK;
1012
1013   g_mutex_lock (&sink->render_lock);
1014
1015   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
1016
1017   if (G_UNLIKELY (!sink->window)) {
1018     /* ask for window handle. Unlock render_lock while doing that because
1019      * set_window_handle & friends will lock it in this context */
1020     g_mutex_unlock (&sink->render_lock);
1021     gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
1022     g_mutex_lock (&sink->render_lock);
1023
1024     if (!sink->window) {
1025       /* if we were not provided a window, create one ourselves */
1026       sink->window =
1027           gst_wl_window_new_toplevel (sink->display, &sink->video_info);
1028     }
1029   }
1030 #ifdef GST_WLSINK_ENHANCEMENT
1031   gst_wayland_sink_update_window_geometry (sink);
1032   sink->video_info_changed = TRUE;
1033 #endif
1034   /* drop buffers until we get a frame callback */
1035   if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
1036     goto done;
1037   /* make sure that the application has called set_render_rectangle() */
1038   if (G_UNLIKELY (sink->window->render_rectangle.w == 0))
1039     goto no_window_size;
1040
1041 #ifdef GST_WLSINK_ENHANCEMENT
1042
1043   wlbuffer = gst_buffer_get_wl_buffer (buffer);
1044   if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
1045     GST_LOG_OBJECT (sink, "buffer %p has a wl_buffer from our display, " "writing directly", buffer);   //buffer is from our  pool and have wl_buffer
1046     GST_INFO ("wl_buffer (%p)", wlbuffer->wlbuffer);
1047     to_render = buffer;
1048 #ifdef DUMP_BUFFER
1049     GstMemory *mem;
1050     GstMapInfo mem_info = GST_MAP_INFO_INIT;
1051     int size = GST_VIDEO_INFO_SIZE (&sink->video_info);
1052     mem = gst_buffer_peek_memory (to_render, 0);
1053     gst_memory_map (mem, &mem_info, GST_MAP_READ);
1054     void *data;
1055     data = mem_info.data;
1056     int ret;
1057     char file_name[128];
1058
1059     sprintf (file_name, "/home/owner/DUMP/_WLSINK_OUT_DUMP_%2.2d.dump",
1060         dump__cnt++);
1061     ret = __write_rawdata (file_name, data, size);
1062     if (ret) {
1063       GST_ERROR ("_write_rawdata() failed");
1064     }
1065     GST_ERROR ("DUMP IMAGE %d, size (%d)", dump__cnt, size);
1066     gst_memory_unmap (mem, &mem_info);
1067 #endif
1068   } else {
1069     GstMemory *mem;
1070     struct wl_buffer *wbuf = NULL;
1071
1072     GST_LOG_OBJECT (sink, "buffer %p does not have a wl_buffer from our " "display, creating it", buffer);      //buffer is from our pool but have not wl_buffer
1073     mem = gst_buffer_peek_memory (buffer, 0);
1074     if (gst_is_wl_shm_memory (mem)) {
1075       FUNCTION;
1076       wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1077           &sink->video_info);
1078       if (wbuf) {
1079         gst_buffer_add_wl_buffer (buffer, wbuf, sink->display); //careat GstWlBuffer and add  gstbuffer, wlbuffer, display and etc
1080         to_render = buffer;
1081       }
1082     } else {                    //buffer is not from our pool and have not wl_buffer
1083       GstMapInfo src;
1084       /* we don't know how to create a wl_buffer directly from the provided
1085        * memory, so we have to copy the data to a memory that we know how
1086        * to handle... */
1087
1088       GST_LOG_OBJECT (sink, "buffer %p is not from our pool", buffer);
1089       GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, " "copying",
1090           buffer);
1091
1092       if (sink->USE_TBM && sink->display->is_native_format) {
1093         /* in case of SN12 or ST12 */
1094         GstMemory *mem;
1095         struct wl_buffer *wbuf = NULL;
1096         GstMapInfo mem_info = GST_MAP_INFO_INIT;
1097         MMVideoBuffer *mm_video_buf = NULL;
1098
1099         mem = gst_buffer_peek_memory (buffer, 1);
1100         gst_memory_map (mem, &mem_info, GST_MAP_READ);
1101         mm_video_buf = (MMVideoBuffer *) mem_info.data;
1102         gst_memory_unmap (mem, &mem_info);
1103
1104         if (mm_video_buf == NULL) {
1105           GST_WARNING_OBJECT (sink, "mm_video_buf is NULL. Skip rendering");
1106           return ret;
1107         }
1108         /* assign mm_video_buf info */
1109         if (mm_video_buf->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
1110           GST_DEBUG_OBJECT (sink, "TBM bo %p %p %p", mm_video_buf->handle.bo[0],
1111               mm_video_buf->handle.bo[1], mm_video_buf->handle.bo[2]);
1112           sink->display->native_video_size = 0;
1113           for (int i = 0; i < NV_BUF_PLANE_NUM; i++) {
1114             if (mm_video_buf->handle.bo[i] != NULL) {
1115               sink->display->bo[i] = mm_video_buf->handle.bo[i];
1116             } else {
1117               sink->display->bo[i] = 0;
1118             }
1119             sink->display->plane_size[i] = mm_video_buf->size[i];
1120             sink->display->stride_width[i] = mm_video_buf->stride_width[i];
1121             sink->display->stride_height[i] = mm_video_buf->stride_height[i];
1122             sink->display->native_video_size += sink->display->plane_size[i];
1123           }
1124         } else {
1125           GST_ERROR_OBJECT (sink, "Buffer type is not TBM");
1126           return ret;
1127         }
1128         wlbuffer = gst_buffer_get_wl_buffer (buffer);
1129         if (G_UNLIKELY (!wlbuffer)) {
1130           wbuf =
1131               gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1132               &sink->video_info);
1133           if (G_UNLIKELY (!wbuf))
1134             goto no_wl_buffer;
1135
1136           gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
1137         }
1138       }
1139
1140       else if (sink->USE_TBM && !sink->display->is_native_format) {
1141
1142         /* sink->pool always exists (created in set_caps), but it may not
1143          * be active if upstream is not using it */
1144         if (!gst_buffer_pool_is_active (sink->pool)
1145             && !gst_buffer_pool_set_active (sink->pool, TRUE))
1146           goto activate_failed;
1147
1148         ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1149         if (ret != GST_FLOW_OK)
1150           goto no_buffer;
1151
1152         //GstMemory *mem;
1153         //mem = gst_buffer_peek_memory (to_render, 0);
1154         //if (gst_is_wl_shm_memory (mem)) {
1155         GST_INFO ("to_render buffer is our buffer");
1156         //}
1157         /* the first time we acquire a buffer,
1158          * we need to attach a wl_buffer on it */
1159         wlbuffer = gst_buffer_get_wl_buffer (buffer);
1160         if (G_UNLIKELY (!wlbuffer)) {
1161           mem = gst_buffer_peek_memory (to_render, 0);
1162           wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1163               &sink->video_info);
1164           if (G_UNLIKELY (!wbuf))
1165             goto no_wl_buffer;
1166
1167           wlbuffer = gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1168         }
1169
1170         gst_buffer_map (buffer, &src, GST_MAP_READ);
1171         gst_buffer_fill (to_render, 0, src.data, src.size);
1172         gst_buffer_unmap (buffer, &src);
1173       } else {                  /* USE SHM */
1174         /* sink->pool always exists (created in set_caps), but it may not
1175          * be active if upstream is not using it */
1176         if (!gst_buffer_pool_is_active (sink->pool) &&
1177             !gst_buffer_pool_set_active (sink->pool, TRUE))
1178           goto activate_failed;
1179         ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1180         if (ret != GST_FLOW_OK)
1181           goto no_buffer;
1182         /* the first time we acquire a buffer,
1183          * we need to attach a wl_buffer on it */
1184         wlbuffer = gst_buffer_get_wl_buffer (buffer);
1185         if (G_UNLIKELY (!wlbuffer)) {
1186           mem = gst_buffer_peek_memory (to_render, 0);
1187           wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1188               &sink->video_info);
1189           if (G_UNLIKELY (!wbuf))
1190             goto no_wl_buffer;
1191
1192           gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1193
1194         }
1195
1196         gst_buffer_map (buffer, &src, GST_MAP_READ);
1197         gst_buffer_fill (to_render, 0, src.data, src.size);
1198         gst_buffer_unmap (buffer, &src);
1199       }
1200     }
1201   }
1202
1203   if (sink->USE_TBM && sink->display->is_native_format) {
1204     if (G_UNLIKELY (buffer == sink->last_buffer)) {
1205       GST_LOG_OBJECT (sink, "Buffer already being rendered");
1206       goto done;
1207     }
1208     gst_buffer_replace (&sink->last_buffer, buffer);
1209     render_last_buffer (sink);
1210
1211     goto done;
1212   } else {                      /* USE SHM or normal format */
1213     /* drop double rendering */
1214     if (G_UNLIKELY (buffer == sink->last_buffer)) {
1215       GST_LOG_OBJECT (sink, "Buffer already being rendered");
1216       goto done;
1217     }
1218     gst_buffer_replace (&sink->last_buffer, to_render);
1219     render_last_buffer (sink);
1220
1221     if (buffer != to_render)
1222       gst_buffer_unref (to_render);
1223
1224     goto done;
1225   }
1226
1227 #else /* open source */
1228
1229   wlbuffer = gst_buffer_get_wl_buffer (buffer);
1230
1231   if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
1232     GST_LOG_OBJECT (sink,
1233         "buffer %p has a wl_buffer from our display, " "writing directly",
1234         buffer);
1235     GST_INFO ("wl_buffer (%p)", wlbuffer->wlbuffer);
1236     to_render = buffer;
1237
1238   } else {
1239     GstMemory *mem;
1240     struct wl_buffer *wbuf = NULL;
1241
1242     GST_LOG_OBJECT (sink,
1243         "buffer %p does not have a wl_buffer from our " "display, creating it",
1244         buffer);
1245     mem = gst_buffer_peek_memory (buffer, 0);
1246     if (gst_is_wl_shm_memory (mem)) {
1247       FUNCTION;
1248       wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1249           &sink->video_info);
1250     }
1251     if (wbuf) {
1252       gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
1253       to_render = buffer;
1254
1255     } else {
1256       GstMapInfo src;
1257       /* we don't know how to create a wl_buffer directly from the provided
1258        * memory, so we have to copy the data to a memory that we know how
1259        * to handle... */
1260
1261       GST_LOG_OBJECT (sink, "buffer %p is not from our pool", buffer);
1262       GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, " "copying",
1263           buffer);
1264       /* sink->pool always exists (created in set_caps), but it may not
1265        * be active if upstream is not using it */
1266       if (!gst_buffer_pool_is_active (sink->pool) &&
1267           !gst_buffer_pool_set_active (sink->pool, TRUE))
1268         goto activate_failed;
1269
1270       ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1271       if (ret != GST_FLOW_OK)
1272         goto no_buffer;
1273
1274       /* the first time we acquire a buffer,
1275        * we need to attach a wl_buffer on it */
1276       wlbuffer = gst_buffer_get_wl_buffer (buffer);
1277       if (G_UNLIKELY (!wlbuffer)) {
1278         mem = gst_buffer_peek_memory (to_render, 0);
1279         wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1280             &sink->video_info);
1281         if (G_UNLIKELY (!wbuf))
1282           goto no_wl_buffer;
1283
1284         gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1285       }
1286
1287       gst_buffer_map (buffer, &src, GST_MAP_READ);
1288       gst_buffer_fill (to_render, 0, src.data, src.size);
1289       gst_buffer_unmap (buffer, &src);
1290     }
1291   }
1292   /* drop double rendering */
1293   if (G_UNLIKELY (buffer == sink->last_buffer)) {
1294     GST_LOG_OBJECT (sink, "Buffer already being rendered");
1295     goto done;
1296   }
1297
1298   gst_buffer_replace (&sink->last_buffer, to_render);
1299   render_last_buffer (sink);
1300
1301   if (buffer != to_render)
1302     gst_buffer_unref (to_render);
1303
1304   goto done;
1305
1306 #endif /* GST_WLSINK_ENHANCEMENT */
1307
1308 no_window_size:
1309   {
1310     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1311         ("Window has no size set"),
1312         ("Make sure you set the size after calling set_window_handle"));
1313     ret = GST_FLOW_ERROR;
1314     goto done;
1315   }
1316 no_buffer:
1317   {
1318     GST_WARNING_OBJECT (sink, "could not create buffer");
1319     goto done;
1320   }
1321 no_wl_buffer:
1322   {
1323     GST_ERROR_OBJECT (sink, "could not create wl_buffer out of wl_shm memory");
1324     ret = GST_FLOW_ERROR;
1325     goto done;
1326   }
1327 activate_failed:
1328   {
1329     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
1330     ret = GST_FLOW_ERROR;
1331     goto done;
1332   }
1333 done:
1334   {
1335     g_mutex_unlock (&sink->render_lock);
1336     return ret;
1337   }
1338 }
1339
1340 static void
1341 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
1342 {
1343   iface->set_window_handle = gst_wayland_sink_set_window_handle;
1344   iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
1345   iface->expose = gst_wayland_sink_expose;
1346 #ifdef GST_WLSINK_ENHANCEMENT   /* use  unique_id */
1347   iface->set_wl_window_wl_surface_id =
1348       gst_wayland_sink_set_wl_window_wl_surface_id;
1349 #endif
1350 }
1351
1352 #ifdef GST_WLSINK_ENHANCEMENT   /* use  unique_id */
1353 static void
1354 gst_wayland_sink_set_wl_window_wl_surface_id (GstVideoOverlay * overlay,
1355     guintptr wl_surface_id)
1356 {
1357   FUNCTION;
1358
1359   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1360   g_return_if_fail (sink != NULL);
1361
1362   if (sink->window != NULL) {
1363     GST_WARNING_OBJECT (sink, "changing window handle is not supported");
1364     return;
1365   }
1366   g_mutex_lock (&sink->render_lock);
1367   g_clear_object (&sink->window);
1368
1369   GST_INFO ("wl_surface_id %d %p", (int) wl_surface_id, wl_surface_id);
1370
1371   if (wl_surface_id) {
1372     if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
1373       /* we cannot use our own display with an external window handle */
1374       if (G_UNLIKELY (sink->display->own_display)) {
1375         sink->display->wl_surface_id = (int) wl_surface_id;
1376         sink->window = gst_wl_window_new_in_surface (sink->display, NULL);
1377       }
1378     } else {
1379       GST_ERROR_OBJECT (sink, "Failed to find display handle, "
1380           "ignoring window handle");
1381     }
1382   }
1383   gst_wayland_sink_update_window_geometry (sink);
1384
1385   g_mutex_unlock (&sink->render_lock);
1386
1387 }
1388 #endif
1389
1390 static void
1391 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
1392 {
1393   FUNCTION;
1394
1395   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1396   struct wl_surface *surface = (struct wl_surface *) handle;
1397
1398   g_return_if_fail (sink != NULL);
1399
1400 #ifdef GST_WLSINK_ENHANCEMENT   /* use  unique_id */
1401   if (sink->window != NULL) {
1402     GST_WARNING_OBJECT (sink, "changing window handle is not supported");
1403     return;
1404   }
1405 #endif
1406   g_mutex_lock (&sink->render_lock);
1407
1408   GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
1409       (void *) handle);
1410
1411   g_clear_object (&sink->window);
1412
1413   if (handle) {
1414     if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
1415       /* we cannot use our own display with an external window handle */
1416       if (G_UNLIKELY (sink->display->own_display)) {
1417       } else {
1418         GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
1419             ("Application did not provide a wayland display handle"),
1420             ("Now waylandsink use internal display handle "
1421                 "which is created ourselves. Consider providing a "
1422                 "display handle from your application with GstContext"));
1423         sink->window = gst_wl_window_new_in_surface (sink->display, surface);
1424       }
1425     } else {
1426       GST_ERROR_OBJECT (sink, "Failed to find display handle, "
1427           "ignoring window handle");
1428     }
1429   }
1430   g_mutex_unlock (&sink->render_lock);
1431
1432 }
1433
1434 static void
1435 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
1436     gint x, gint y, gint w, gint h)
1437 {
1438   FUNCTION;
1439   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1440
1441   g_return_if_fail (sink != NULL);
1442
1443   g_mutex_lock (&sink->render_lock);
1444   if (!sink->window) {
1445     g_mutex_unlock (&sink->render_lock);
1446     GST_WARNING_OBJECT (sink,
1447         "set_render_rectangle called without window, ignoring");
1448     return;
1449   }
1450
1451   GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
1452       x, y, w, h);
1453   gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
1454
1455   g_mutex_unlock (&sink->render_lock);
1456 }
1457
1458 static void
1459 gst_wayland_sink_expose (GstVideoOverlay * overlay)
1460 {
1461   FUNCTION;
1462   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1463
1464   g_return_if_fail (sink != NULL);
1465
1466   GST_DEBUG_OBJECT (sink, "expose");
1467
1468   g_mutex_lock (&sink->render_lock);
1469   if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
1470     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
1471     render_last_buffer (sink);
1472   }
1473   g_mutex_unlock (&sink->render_lock);
1474 }
1475
1476 static void
1477 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
1478 {
1479   iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
1480   iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
1481 }
1482
1483 static void
1484 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
1485 {
1486   FUNCTION;
1487   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1488   g_return_if_fail (sink != NULL);
1489
1490   g_mutex_lock (&sink->render_lock);
1491   if (!sink->window || !sink->window->area_subsurface) {
1492     g_mutex_unlock (&sink->render_lock);
1493     GST_INFO_OBJECT (sink,
1494         "begin_geometry_change called without window, ignoring");
1495     return;
1496   }
1497
1498   wl_subsurface_set_sync (sink->window->area_subsurface);
1499   g_mutex_unlock (&sink->render_lock);
1500 }
1501
1502 static void
1503 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
1504 {
1505   FUNCTION;
1506   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1507   g_return_if_fail (sink != NULL);
1508
1509   g_mutex_lock (&sink->render_lock);
1510   if (!sink->window || !sink->window->area_subsurface) {
1511     g_mutex_unlock (&sink->render_lock);
1512     GST_INFO_OBJECT (sink,
1513         "end_geometry_change called without window, ignoring");
1514     return;
1515   }
1516
1517   wl_subsurface_set_desync (sink->window->area_subsurface);
1518   g_mutex_unlock (&sink->render_lock);
1519 }
1520
1521 static gboolean
1522 plugin_init (GstPlugin * plugin)
1523 {
1524   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
1525       " wayland video sink");
1526
1527   gst_wl_shm_allocator_register ();
1528
1529   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
1530       GST_TYPE_WAYLAND_SINK);
1531 }
1532
1533 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1534     GST_VERSION_MINOR,
1535     waylandsink,
1536     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1537     GST_PACKAGE_ORIGIN)