MMFMWK-8030 waylandsink: support video crop meta using viewporter protocol

use API wp_viewport_set_source() to support video crop meta handle

upstream status: Pending
https://bugzilla.gnome.org/show_bug.cgi?id=796541
diff --git a/ext/wayland/gstwaylandsink.c b/ext/wayland/gstwaylandsink.c
index 71001ce..bc56bf4 100644
--- a/ext/wayland/gstwaylandsink.c
+++ b/ext/wayland/gstwaylandsink.c
@@ -610,6 +610,7 @@
   alloc = gst_wl_shm_allocator_get ();
   gst_query_add_allocation_param (query, alloc, NULL);
   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
+  gst_query_add_allocation_meta (query, GST_VIDEO_CROP_META_API_TYPE, NULL);
   g_object_unref (alloc);
 
   return TRUE;
@@ -695,6 +696,8 @@
   /* make sure that the application has called set_render_rectangle() */
   if (G_UNLIKELY (sink->window->render_rectangle.w == 0))
     goto no_window_size;
+  
+  gst_wl_window_set_source_crop (sink->window, buffer);
 
   wlbuffer = gst_buffer_get_wl_buffer (buffer);
 
diff --git a/ext/wayland/wlwindow.c b/ext/wayland/wlwindow.c
index c64c77a..6e63ab7 100644
--- a/ext/wayland/wlwindow.c
+++ b/ext/wayland/wlwindow.c
@@ -79,6 +79,10 @@
 static void
 gst_wl_window_init (GstWlWindow * self)
 {
+  self->src_x = 0;
+  self->src_y = 0;
+  self->src_width = -1;
+  self->src_height = 0;
 }
 
 static void
@@ -229,6 +233,11 @@
   GstVideoRectangle dst = { 0, };
   GstVideoRectangle res;
 
+  wl_fixed_t src_x = wl_fixed_from_int (window->src_x);
+  wl_fixed_t src_y = wl_fixed_from_int (window->src_y);
+  wl_fixed_t src_width = wl_fixed_from_int (window->src_width);
+  wl_fixed_t src_height = wl_fixed_from_int (window->src_height);
+
   /* center the video_subsurface inside area_subsurface */
   src.w = window->video_width;
   src.h = window->video_height;
@@ -238,6 +247,9 @@
   if (window->video_viewport) {
     gst_video_sink_center_rect (src, dst, &res, TRUE);
     wp_viewport_set_destination (window->video_viewport, res.w, res.h);
+    if (src_width != wl_fixed_from_int(-1))
+      wp_viewport_set_source (window->video_viewport,
+          src_x, src_y, src_width, src_height);
   } else {
     gst_video_sink_center_rect (src, dst, &res, FALSE);
   }
@@ -403,3 +415,21 @@
   if (window->video_width != 0)
     wl_subsurface_set_desync (window->video_subsurface);
 }
+
+void
+gst_wl_window_set_source_crop (GstWlWindow * window, GstBuffer * buffer)
+{
+  GstVideoCropMeta *crop = NULL;
+  crop = gst_buffer_get_video_crop_meta(buffer);
+
+  if (crop) {
+    GST_DEBUG ("buffer crop x=%d y=%d width=%d height=%d\n",
+        crop->x, crop->y, crop->width, crop->height);
+    window->src_x = crop->x;
+    window->src_y = crop->y;
+    window->src_width = crop->width;
+    window->src_height = crop->height;
+  } else {
+    window->src_width = -1;
+  }
+}
diff --git a/ext/wayland/wlwindow.h b/ext/wayland/wlwindow.h
index e247b4e..2c176a1 100644
--- a/ext/wayland/wlwindow.h
+++ b/ext/wayland/wlwindow.h
@@ -61,6 +61,9 @@
   /* the size of the video in the buffers */
   gint video_width, video_height;
 
+  /* the coordinate of video crop */
+  gint src_x, src_y, src_width, src_height;
+
   /* this will be set when viewporter is available and black background has
    * already been set on the area_subsurface */
   gboolean no_border_update;
@@ -87,6 +90,7 @@
         const GstVideoInfo * info);
 void gst_wl_window_set_render_rectangle (GstWlWindow * window, gint x, gint y,
         gint w, gint h);
+void gst_wl_window_set_source_crop (GstWlWindow * window, GstBuffer * buffer);
 
 G_END_DECLS