ximagesink: implement ALLOCATION query
[platform/upstream/gstreamer.git] / sys / ximage / ximagesink.c
index 0374b81..51205dc 100644 (file)
 #include <gst/interfaces/navigation.h>
 #include <gst/interfaces/xoverlay.h>
 
+#include <gst/video/gstmetavideo.h>
+
 /* Object header */
 #include "ximagesink.h"
 
@@ -475,6 +477,7 @@ static void
 gst_ximagesink_xwindow_update_geometry (GstXImageSink * ximagesink)
 {
   XWindowAttributes attr;
+  gboolean reconfigure;
 
   g_return_if_fail (GST_IS_XIMAGESINK (ximagesink));
 
@@ -488,10 +491,17 @@ gst_ximagesink_xwindow_update_geometry (GstXImageSink * ximagesink)
   XGetWindowAttributes (ximagesink->xcontext->disp,
       ximagesink->xwindow->win, &attr);
 
+  /* Check if we would suggest a different width/height now */
+  reconfigure = (ximagesink->xwindow->width != attr.width)
+      || (ximagesink->xwindow->height != attr.height);
   ximagesink->xwindow->width = attr.width;
   ximagesink->xwindow->height = attr.height;
 
   g_mutex_unlock (ximagesink->x_lock);
+
+  if (reconfigure)
+    gst_pad_push_event (GST_BASE_SINK (ximagesink)->sinkpad,
+        gst_event_new_reconfigure ());
 }
 
 static void
@@ -966,7 +976,7 @@ gst_ximagesink_xcontext_clear (GstXImageSink * ximagesink)
 /* Element stuff */
 
 static GstCaps *
-gst_ximagesink_getcaps (GstBaseSink * bsink)
+gst_ximagesink_getcaps (GstBaseSink * bsink, GstCaps * filter)
 {
   GstXImageSink *ximagesink;
   GstCaps *caps;
@@ -974,17 +984,59 @@ gst_ximagesink_getcaps (GstBaseSink * bsink)
 
   ximagesink = GST_XIMAGESINK (bsink);
 
-  if (ximagesink->xcontext)
-    return gst_caps_ref (ximagesink->xcontext->caps);
+  g_mutex_lock (ximagesink->x_lock);
+  if (ximagesink->xcontext) {
+    GstCaps *caps;
+
+    caps = gst_caps_ref (ximagesink->xcontext->caps);
 
-  /* get a template copy and add the pixel aspect ratio */
-  caps =
-      gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK
-          (ximagesink)->sinkpad));
-  for (i = 0; i < gst_caps_get_size (caps); ++i) {
-    GstStructure *structure = gst_caps_get_structure (caps, i);
+    if (filter) {
+      GstCaps *intersection;
 
-    if (ximagesink->par) {
+      intersection =
+          gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
+      gst_caps_unref (caps);
+      caps = intersection;
+    }
+
+    if (ximagesink->xwindow && ximagesink->xwindow->width) {
+      GstStructure *s0, *s1;
+
+      caps = gst_caps_make_writable (caps);
+
+      /* There can only be a single structure because the xcontext
+       * caps only have a single structure */
+      s0 = gst_caps_get_structure (caps, 0);
+      s1 = gst_structure_copy (gst_caps_get_structure (caps, 0));
+
+      gst_structure_set (s0, "width", G_TYPE_INT, ximagesink->xwindow->width,
+          "height", G_TYPE_INT, ximagesink->xwindow->height, NULL);
+      gst_caps_append_structure (caps, s1);
+
+      /* This will not change the order but will remove the
+       * fixed width/height caps again if not possible
+       * upstream */
+      if (filter) {
+        GstCaps *intersection;
+
+        intersection =
+            gst_caps_intersect_full (caps, filter, GST_CAPS_INTERSECT_FIRST);
+        gst_caps_unref (caps);
+        caps = intersection;
+      }
+    }
+
+    g_mutex_unlock (ximagesink->x_lock);
+    return caps;
+  }
+  g_mutex_unlock (ximagesink->x_lock);
+
+  /* get a template copy and add the pixel aspect ratio */
+  caps = gst_pad_get_pad_template_caps (GST_BASE_SINK (ximagesink)->sinkpad);
+  if (ximagesink->par) {
+    caps = gst_caps_make_writable (caps);
+    for (i = 0; i < gst_caps_get_size (caps); ++i) {
+      GstStructure *structure = gst_caps_get_structure (caps, i);
       int nom, den;
 
       nom = gst_value_get_fraction_numerator (ximagesink->par);
@@ -994,6 +1046,15 @@ gst_ximagesink_getcaps (GstBaseSink * bsink)
     }
   }
 
+  if (filter) {
+    GstCaps *intersection;
+
+    intersection =
+        gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
+    gst_caps_unref (caps);
+    caps = intersection;
+  }
+
   return caps;
 }
 
@@ -1341,186 +1402,96 @@ gst_ximagesink_event (GstBaseSink * sink, GstEvent * event)
     return TRUE;
 }
 
-/* Buffer management
- *
- * The buffer_alloc function must either return a buffer with given size and
- * caps or create a buffer with different caps attached to the buffer. This
- * last option is called reverse negotiation, ie, where the sink suggests a
- * different format from the upstream peer. 
- *
- * We try to do reverse negotiation when our geometry changes and we like a
- * resized buffer.
- */
-static GstFlowReturn
-gst_ximagesink_buffer_alloc (GstBaseSink * bsink, guint64 offset, guint size,
-    GstCaps * caps, GstBuffer ** buf)
+static gboolean
+gst_ximagesink_sink_query (GstPad * sinkpad, GstQuery * query)
 {
-  GstXImageSink *ximagesink;
-  GstBuffer *ximage = NULL;
-  GstStructure *structure = NULL;
-  GstFlowReturn ret = GST_FLOW_OK;
-  GstCaps *alloc_caps;
-  gboolean alloc_unref = FALSE;
-  gint width, height;
-  GstVideoRectangle dst, src, result;
-  gboolean caps_accepted = FALSE;
-
-  ximagesink = GST_XIMAGESINK (bsink);
-
-  if (G_UNLIKELY (!caps)) {
-    GST_WARNING_OBJECT (ximagesink, "have no caps, doing fallback allocation");
-    *buf = NULL;
-    ret = GST_FLOW_OK;
-    goto beach;
-  }
-
-  /* This shouldn't really happen because state changes will fail
-   * if the xcontext can't be allocated */
-  if (!ximagesink->xcontext)
-    return GST_FLOW_ERROR;
-
-  GST_LOG_OBJECT (ximagesink,
-      "a buffer of %d bytes was requested with caps %" GST_PTR_FORMAT
-      " and offset %" G_GUINT64_FORMAT, size, caps, offset);
-
-  /* assume we're going to alloc what was requested, keep track of
-   * wheter we need to unref or not. When we suggest a new format 
-   * upstream we will create a new caps that we need to unref. */
-  alloc_caps = caps;
-  alloc_unref = FALSE;
-
-  /* get struct to see what is requested */
-  structure = gst_caps_get_structure (caps, 0);
-  if (!gst_structure_get_int (structure, "width", &width) ||
-      !gst_structure_get_int (structure, "height", &height)) {
-    GST_WARNING_OBJECT (ximagesink, "invalid caps for buffer allocation %"
-        GST_PTR_FORMAT, caps);
-    ret = GST_FLOW_NOT_NEGOTIATED;
-    goto beach;
-  }
-
-  src.w = width;
-  src.h = height;
-
-  /* We take the flow_lock because the window might go away */
-  g_mutex_lock (ximagesink->flow_lock);
-  if (!ximagesink->xwindow) {
-    g_mutex_unlock (ximagesink->flow_lock);
-    goto alloc;
-  }
+  GstXImageSink *ximagesink = GST_XIMAGESINK (GST_PAD_PARENT (sinkpad));
+  gboolean res = TRUE;
 
-  /* What is our geometry */
-  dst.w = ximagesink->xwindow->width;
-  dst.h = ximagesink->xwindow->height;
+  switch (GST_QUERY_TYPE (query)) {
+    case GST_QUERY_ALLOCATION:
+    {
+      GstBufferPool *pool;
+      GstStructure *config;
+      GstCaps *caps;
+      guint size;
+      gboolean need_pool;
 
-  g_mutex_unlock (ximagesink->flow_lock);
+      gst_query_parse_allocation (query, &caps, &need_pool);
 
-  if (ximagesink->keep_aspect) {
-    GST_LOG_OBJECT (ximagesink, "enforcing aspect ratio in reverse caps "
-        "negotiation");
-    gst_video_sink_center_rect (src, dst, &result, TRUE);
-  } else {
-    GST_LOG_OBJECT (ximagesink, "trying to resize to window geometry "
-        "ignoring aspect ratio");
-    result.x = result.y = 0;
-    result.w = dst.w;
-    result.h = dst.h;
-  }
+      if (caps == NULL)
+        goto no_caps;
 
-  /* We would like another geometry */
-  if (width != result.w || height != result.h) {
-    int nom, den;
-    GstCaps *desired_caps;
-    GstStructure *desired_struct;
+      g_mutex_lock (ximagesink->flow_lock);
+      if ((pool = ximagesink->pool))
+        gst_object_ref (pool);
+      g_mutex_unlock (ximagesink->flow_lock);
 
-    /* make a copy of the incomming caps to create the new
-     * suggestion. We can't use make_writable because we might
-     * then destroy the original caps which we still need when the
-     * peer does not accept the suggestion. */
-    desired_caps = gst_caps_copy (caps);
-    desired_struct = gst_caps_get_structure (desired_caps, 0);
+      if (pool != NULL) {
+        const GstCaps *pcaps;
+
+        /* we had a pool, check caps */
+        config = gst_buffer_pool_get_config (pool);
+        gst_buffer_pool_config_get (config, &pcaps, &size, NULL, NULL, NULL,
+            NULL, NULL);
+
+        GST_DEBUG_OBJECT (ximagesink,
+            "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
+        if (!gst_caps_is_equal (caps, pcaps)) {
+          /* different caps, we can't use this pool */
+          GST_DEBUG_OBJECT (ximagesink, "pool has different caps");
+          gst_object_unref (pool);
+          pool = NULL;
+        }
+      }
+      if (pool == NULL && need_pool) {
+        GstVideoFormat format;
+        gint width, height;
 
-    GST_DEBUG ("we would love to receive a %dx%d video", result.w, result.h);
-    gst_structure_set (desired_struct, "width", G_TYPE_INT, result.w, NULL);
-    gst_structure_set (desired_struct, "height", G_TYPE_INT, result.h, NULL);
+        GST_DEBUG_OBJECT (ximagesink, "create new pool");
+        pool = gst_ximage_buffer_pool_new (ximagesink);
 
-    /* PAR property overrides the X calculated one */
-    if (ximagesink->par) {
-      nom = gst_value_get_fraction_numerator (ximagesink->par);
-      den = gst_value_get_fraction_denominator (ximagesink->par);
-      gst_structure_set (desired_struct, "pixel-aspect-ratio",
-          GST_TYPE_FRACTION, nom, den, NULL);
-    } else if (ximagesink->xcontext->par) {
-      nom = gst_value_get_fraction_numerator (ximagesink->xcontext->par);
-      den = gst_value_get_fraction_denominator (ximagesink->xcontext->par);
-      gst_structure_set (desired_struct, "pixel-aspect-ratio",
-          GST_TYPE_FRACTION, nom, den, NULL);
-    }
+        if (!gst_video_format_parse_caps (caps, &format, &width, &height))
+          goto invalid_caps;
 
+        /* the normal size of a frame */
+        size = gst_video_format_get_size (format, width, height);
 
-    /* see if peer accepts our new suggestion, if there is no peer, this 
-     * function returns true. */
-    if (!ximagesink->xcontext->last_caps ||
-        !gst_caps_is_equal (desired_caps, ximagesink->xcontext->last_caps)) {
-      caps_accepted =
-          gst_pad_peer_accept_caps (GST_VIDEO_SINK_PAD (ximagesink),
-          desired_caps);
+        config = gst_buffer_pool_get_config (pool);
+        gst_buffer_pool_config_set (config, caps, size, 0, 0, 0, 0, 16);
+        if (!gst_buffer_pool_set_config (pool, config))
+          goto config_failed;
+      }
+      gst_query_set_allocation_params (query, size, 0, 0, 0, 16, pool);
 
-      /* Suggestion failed, prevent future attempts for the same caps
-       * to fail as well. */
-      if (!caps_accepted)
-        gst_caps_replace (&ximagesink->xcontext->last_caps, desired_caps);
-    }
+      /* we also support various metadata */
+      gst_query_add_allocation_meta (query, GST_META_API_VIDEO);
 
-    if (caps_accepted) {
-      /* we will not alloc a buffer of the new suggested caps. Make sure
-       * we also unref this new caps after we set it on the buffer. */
-      alloc_caps = desired_caps;
-      alloc_unref = TRUE;
-      width = result.w;
-      height = result.h;
-      GST_DEBUG ("peer pad accepts our desired caps %" GST_PTR_FORMAT,
-          desired_caps);
-    } else {
-      GST_DEBUG ("peer pad does not accept our desired caps %" GST_PTR_FORMAT,
-          desired_caps);
-      /* we alloc a buffer with the original incomming caps already in the
-       * width and height variables */
-      gst_caps_unref (desired_caps);
+      gst_object_unref (pool);
+      break;
     }
+    default:
+      res = FALSE;
+      break;
   }
+  return res;
 
-alloc:
-
-  if (gst_caps_is_equal (GST_PAD_CAPS (bsink->sinkpad), alloc_caps)) {
-    /* we negotiated this format before, use the pool */
-    if (ximagesink->pool) {
-      GST_LOG_OBJECT (ximagesink, "retrieving buffer from pool");
-      ret = gst_buffer_pool_acquire_buffer (ximagesink->pool, &ximage, NULL);
-    }
+  /* ERRORS */
+no_caps:
+  {
+    GST_DEBUG_OBJECT (sinkpad, "no caps specified");
+    return FALSE;
   }
-
-  if (ximage == NULL) {
-    /* Something new make a new image a new one */
-    GST_LOG_OBJECT (ximagesink, "allocating new image");
-    ximage = gst_ximage_buffer_new (ximagesink, width, height);
+invalid_caps:
+  {
+    GST_DEBUG_OBJECT (sinkpad, "invalid caps specified");
+    return FALSE;
   }
-
-  /* Now we should have a ximage, set appropriate caps on it */
-  if (ximage) {
-    /* Make sure the buffer is cleared of any previously used flags */
-    GST_MINI_OBJECT_CAST (ximage)->flags = 0;
-    gst_buffer_set_caps (ximage, alloc_caps);
+config_failed:
+  {
+    GST_DEBUG_OBJECT (sinkpad, "failed setting config");
+    return FALSE;
   }
-
-  /* could be our new reffed suggestion or the original unreffed caps */
-  if (alloc_unref)
-    gst_caps_unref (alloc_caps);
-
-  *buf = ximage;
-
-beach:
-  return ret;
 }
 
 /* Interfaces stuff */
@@ -1904,6 +1875,10 @@ gst_ximagesink_finalize (GObject * object)
 static void
 gst_ximagesink_init (GstXImageSink * ximagesink)
 {
+  /* for the ALLOCATION query */
+  gst_pad_set_query_function (GST_BASE_SINK (ximagesink)->sinkpad,
+      gst_ximagesink_sink_query);
+
   ximagesink->display_name = NULL;
   ximagesink->xcontext = NULL;
   ximagesink->xwindow = NULL;
@@ -2006,8 +1981,6 @@ gst_ximagesink_class_init (GstXImageSinkClass * klass)
 
   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_ximagesink_getcaps);
   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_ximagesink_setcaps);
-  gstbasesink_class->buffer_alloc =
-      GST_DEBUG_FUNCPTR (gst_ximagesink_buffer_alloc);
   gstbasesink_class->get_times = GST_DEBUG_FUNCPTR (gst_ximagesink_get_times);
   gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_ximagesink_event);