yikes, cvs didn't like my vim swap file.
authorThomas Vander Stichele <thomas@apestaart.org>
Tue, 11 Nov 2003 11:32:27 +0000 (11:32 +0000)
committerThomas Vander Stichele <thomas@apestaart.org>
Tue, 11 Nov 2003 11:32:27 +0000 (11:32 +0000)
Original commit message from CVS:
yikes, cvs didn't like my vim swap file.
here's the proper revert + commit

sys/ximage/ximagesink.c
sys/ximage/ximagesink.h

index 0b4fb07..01d1b68 100644 (file)
@@ -36,12 +36,6 @@ static GstElementDetails gst_ximagesink_details = GST_ELEMENT_DETAILS (
   "Julien Moutte <julien@moutte.net>"
 );
 
-enum
-{
-  ARG_0,
-  ARG_DISPLAY_NAME
-};
-
 /* Default template - initiated with class struct to allow gst-register to work
    without X running */
 GST_PAD_TEMPLATE_FACTORY (gst_ximagesink_sink_template_factory,
@@ -80,18 +74,30 @@ gst_ximagesink_interface_init (GstInterfaceClass *klass)
 }
 
 static void
-gst_ximagesink_navigation_send_event (GstNavigation *navigation, GstCaps *caps)
+gst_ximagesink_navigation_send_event (GstNavigation *navigation, GstStructure *structure)
 {
   GstXImageSink *ximagesink = GST_XIMAGESINK (navigation);
+  XWindowAttributes attr;
   GstEvent *event;
+  double x, y;
 
-  event = gst_event_new (GST_EVENT_NAVIGATION);
-  /*GST_EVENT_TIMESTAMP (event) = 0;*/
-  event->event_data.caps.caps = caps;
+  g_mutex_lock (ximagesink->x_lock);
+  XGetWindowAttributes (ximagesink->xcontext->disp,
+      ximagesink->xwindow->win, &attr);
+  g_mutex_unlock (ximagesink->x_lock);
 
-  /* FIXME 
-   * Obviously, the pointer x,y coordinates need to be adjusted by the
-   * window size and relation to the bounding window. */
+  event = gst_event_new (GST_EVENT_NAVIGATION);
+  event->event_data.structure.structure = structure;
+  if(gst_structure_get_double(structure, "pointer_x", &x)){
+    x *= ximagesink->width;
+    x /= attr.width;
+    gst_structure_set(structure, "pointer_x", G_TYPE_DOUBLE, x, NULL);
+  }
+  if(gst_structure_get_double(structure, "pointer_y", &y)){
+    y *= ximagesink->height;
+    y /= attr.height;
+    gst_structure_set(structure, "pointer_y", G_TYPE_DOUBLE, y, NULL);
+  }
 
   gst_pad_send_event (gst_pad_get_peer (ximagesink->sinkpad), event);
 }
@@ -104,6 +110,7 @@ gst_ximagesink_navigation_init (GstNavigationInterface *iface)
 
 /* X11 stuff */
 
+/* This function handles GstXImage creation depending on XShm availability */
 static GstXImage *
 gst_ximagesink_ximage_new (GstXImageSink *ximagesink, gint width, gint height)
 {
@@ -120,20 +127,7 @@ gst_ximagesink_ximage_new (GstXImageSink *ximagesink, gint width, gint height)
   
   g_mutex_lock (ximagesink->x_lock);
 
-  GstXImage *ximage = NULL;
-  
-  g_return_val_if_fail (ximagesink != NULL, NULL);
-  g_return_val_if_fail (GST_IS_XIMAGESINK (ximagesink), NULL);
-  
-  ximage = g_new0 (GstXImage, 1);
-  
-  ximage->width = width;
-  ximage->height = height;
-  ximage->data = NULL;
-  
-  g_mutex_lock (ximagesink->x_lock);
-
-  ximage->size = (ximagesink->xcontext->bpp / 8) * ximage->width * ximage->height;
+  ximage->size =  (ximagesink->xcontext->bpp / 8) * ximage->width * ximage->height;
   
 #ifdef HAVE_SHM
   if (ximagesink->xcontext->use_xshm)
@@ -176,8 +170,6 @@ gst_ximagesink_ximage_new (GstXImageSink *ximagesink, gint width, gint height)
                                  ximage->width, ximage->height,
                                  ximagesink->xcontext->bpp,
                                  ximage->width * (ximagesink->xcontext->bpp / 8));
-    
-  GST_DEBUG ("ximagesink creating an image without XShm");
 #endif /* HAVE_SHM */
   
   if (ximage->ximage)
@@ -279,7 +271,7 @@ gst_ximagesink_ximage_put (GstXImageSink *ximagesink, GstXImage *ximage)
              0, 0, x, y, ximage->width, ximage->height);
 #endif /* HAVE_SHM */
   
-  XSync (ximagesink->xcontext->disp, FALSE);
+  XSync(ximagesink->xcontext->disp, FALSE);
   
   g_mutex_unlock (ximagesink->x_lock);
 }
@@ -458,10 +450,10 @@ gst_ximagesink_handle_xevents (GstXImageSink *ximagesink, GstPad *pad)
   g_mutex_unlock (ximagesink->x_lock);
 }
 
-/* This function gets the X Display and global infos about it. Everything is
+/* This function get the X Display and global infos about it. Everything is
    stored in our object and will be cleaned when the object is disposed. Note
    here that caps for supported format are generated without any window or 
-   image creation. */
+   image creation */
 static GstXContext *
 gst_ximagesink_xcontext_get (GstXImageSink *ximagesink)
 {
@@ -572,133 +564,116 @@ gst_ximagesink_xcontext_clear (GstXImageSink *ximagesink)
   
   XCloseDisplay (ximagesink->xcontext->disp);
   
-    GstClockID id = gst_clock_new_single_shot_id (ximagesink->clock, time);
-    gst_element_clock_wait (GST_ELEMENT (ximagesink), id, NULL);
-    gst_clock_id_free (id);
-  }
-  
-  /* If we have a pool and the image is from this pool, simply put it. */
-  if ( (ximagesink->bufferpool) &&
-       (GST_BUFFER_BUFFERPOOL (buf) == ximagesink->bufferpool) )
-    gst_ximagesink_ximage_put (ximagesink, GST_BUFFER_POOL_PRIVATE (buf));
-  else /* Else we have to copy the data into our private image, */
-    {  /* if we have one... */
-      if (ximagesink->ximage)
-        {
-          memcpy (ximagesink->ximage->ximage->data, 
-                  GST_BUFFER_DATA (buf), 
-                  MIN (GST_BUFFER_SIZE (buf), ximagesink->ximage->size));
-          gst_ximagesink_ximage_put (ximagesink, ximagesink->ximage);
-        }
-      else /* No image available. Something went wrong during capsnego ! */
-        {
-          gst_buffer_unref (buf);
-          gst_element_error (GST_ELEMENT (ximagesink), "no image to draw");
-          return;
-        }
-    }
+  g_mutex_unlock (ximagesink->x_lock);
   
-  gst_buffer_unref (buf);
+  ximagesink->xcontext = NULL;
 }
 
-static void
-gst_ximagesink_get_property (GObject *object, guint prop_id,
-                             GValue *value, GParamSpec *pspec)
+/* Element stuff */
+
+static GstCaps *
+gst_ximagesink_getcaps (GstPad *pad, GstCaps *caps)
 {
   GstXImageSink *ximagesink;
-                                                                                
-  g_return_if_fail (GST_IS_XIMAGESINK (object));
-                                                                                
-  ximagesink = GST_XIMAGESINK (object);
+  
+  ximagesink = GST_XIMAGESINK (gst_pad_get_parent (pad));
+  
+  if (ximagesink->xcontext)
+    return gst_caps_copy (ximagesink->xcontext->caps);
 
-  switch (prop_id)
+  return GST_CAPS_NEW ("ximagesink_rgbsink", "video/x-raw-rgb",
+                       "framerate", GST_PROPS_FLOAT_RANGE (0, G_MAXFLOAT),
+                       "width", GST_PROPS_INT_RANGE (0, G_MAXINT),
+                       "height", GST_PROPS_INT_RANGE (0, G_MAXINT));
 }
 
-static void
-gst_ximagesink_set_clock (GstElement *element, GstClock *clock)
+static GstPadLinkReturn
+gst_ximagesink_sinkconnect (GstPad *pad, GstCaps *caps)
 {
   GstXImageSink *ximagesink;
 
-  ximagesink = GST_XIMAGESINK (element);
-  
-  ximagesink->clock = clock;
-}
+  ximagesink = GST_XIMAGESINK (gst_pad_get_parent (pad));
 
-static GstBuffer*
-gst_ximagesink_buffer_new (GstBufferPool *pool,  
-                          gint64 location, guint size, gpointer user_data)
-{
-  GstXImageSink *ximagesink;
-  GstBuffer *buffer;
-  GstXImage *ximage = NULL;
-  gboolean not_found = TRUE;
-  
-  ximagesink = GST_XIMAGESINK (user_data);
-  
-  g_mutex_lock (ximagesink->pool_lock);
+  /* we are not going to act on variable caps */
+  if (!GST_CAPS_IS_FIXED (caps))
+    return GST_PAD_LINK_DELAYED;
+  if (GST_CAPS_IS_CHAINED (caps))
+    return GST_PAD_LINK_DELAYED;
+  
+  GST_DEBUG ("sinkconnect %s with %s", gst_caps_to_string(caps),
+             gst_caps_to_string(ximagesink->xcontext->caps));
+  
+  if (!gst_caps_get_int (caps, "width", &ximagesink->width))
+    return GST_PAD_LINK_REFUSED;
+  if (!gst_caps_get_int (caps, "height", &ximagesink->height))
+    return GST_PAD_LINK_REFUSED;
+  
+  if (gst_caps_has_fixed_property (caps, "pixel_width"))
+    gst_caps_get_int (caps, "pixel_width", &ximagesink->pixel_width);
+  else
+    ximagesink->pixel_width = 1;
+
+  if (gst_caps_has_fixed_property (caps, "pixel_height"))
+    gst_caps_get_int (caps, "pixel_height", &ximagesink->pixel_height);
+  else
+    ximagesink->pixel_height = 1;
   
-  /* Walking through the pool cleaning unsuable images and searching for a
-     suitable one */
-  while (not_found && ximagesink->image_pool)
-    {
-      ximage = ximagesink->image_pool->data;
-      
-      if (ximage)
-        {
-          /* Removing from the pool */
-          ximagesink->image_pool = g_slist_delete_link (ximagesink->image_pool,
-                                                        ximagesink->image_pool);
-          
-          if ( (ximage->width != ximagesink->width) ||
-               (ximage->height != ximagesink->height) )
-            { /* This image is unusable. Destroying... */
-              gst_ximagesink_ximage_destroy (ximagesink, ximage);
-              ximage = NULL;
-            }
-          else /* We found a suitable image */
-            break;
-        }
+  /* Creating our window and our image */
+  if (!ximagesink->xwindow)
+    ximagesink->xwindow = gst_ximagesink_xwindow_new (ximagesink,
+                                                      ximagesink->width,
+                                                      ximagesink->height);
+  else
+    { /* We resize our window only if size has changed, preventing us from
+         infinite loops with XConfigure events.
+      if ( (ximagesink->width != ximagesink->xwindow->width) ||
+           (ximagesink->height != ximagesink->xwindow->height) )
+        gst_ximagesink_xwindow_resize (ximagesink, ximagesink->xwindow,
+                                       ximagesink->width, ximagesink->height);*/
     }
-   
-  g_mutex_unlock (ximagesink->pool_lock);
   
-  if (!ximage) /* We didn't find a suitable image in the pool. Creating... */
-    ximage = gst_ximagesink_ximage_new (ximagesink,
-                                        ximagesink->width,
-                                        ximagesink->height);
+  if ( (ximagesink->ximage) &&
+       ( (ximagesink->width != ximagesink->ximage->width) ||
+         (ximagesink->height != ximagesink->ximage->height) ) )
+    { /* We renew our ximage only if size changed */
+      gst_ximagesink_ximage_destroy (ximagesink, ximagesink->ximage);
   
-  if (ximage)
-    {
-      buffer = gst_buffer_new ();
-      GST_BUFFER_POOL_PRIVATE (buffer) = ximage;
-      GST_BUFFER_DATA (buffer) = ximage->ximage->data;
-      GST_BUFFER_SIZE (buffer) = ximage->size;
-      return buffer;
+      ximagesink->ximage = gst_ximagesink_ximage_new (ximagesink,
+                                                      ximagesink->width,
+                                                      ximagesink->height);
     }
-  else
-    return NULL;
+  else if (!ximagesink->ximage) /* If no ximage, creating one */
+    ximagesink->ximage = gst_ximagesink_ximage_new (ximagesink,
+                                                    ximagesink->width,
+                                                    ximagesink->height);
+  
+  return GST_PAD_LINK_OK;
 }
 
-static void
-gst_ximagesink_buffer_free (GstBufferPool *pool,
-                            GstBuffer *buffer, gpointer user_data)
+static GstElementStateReturn
+gst_ximagesink_change_state (GstElement *element)
 {
   GstXImageSink *ximagesink;
-  GstXImage *ximage;
-  
-  ximagesink = GST_XIMAGESINK (user_data);
-  
-  ximage = GST_BUFFER_POOL_PRIVATE (buffer);
-  
-  /* If our geometry changed we can't reuse that image. */
-  if ( (ximage->width != ximagesink->width) ||
-       (ximage->height != ximagesink->height) )
-    {
-      gst_ximagesink_ximage_destroy (ximagesink, ximage);
-    }
-  else /* If it didn't we can reuse the image and add it to our image pool. */
-    {
-      g_mutex_lock (ximagesink->pool_lock);
+
+  ximagesink = GST_XIMAGESINK (element);
+
+  switch (GST_STATE_TRANSITION (element)) {
+    case GST_STATE_NULL_TO_READY:
+      /* Initializing the XContext */
+      ximagesink->xcontext = gst_ximagesink_xcontext_get (ximagesink);
+      if (!ximagesink->xcontext)
+        return GST_STATE_FAILURE;
+      break;
+    case GST_STATE_READY_TO_PAUSED:
+      break;
+    case GST_STATE_PAUSED_TO_PLAYING:
+      break;
+    case GST_STATE_PLAYING_TO_PAUSED:
+      break;
+    case GST_STATE_PAUSED_TO_READY:
+      break;
+    case GST_STATE_READY_TO_NULL:
+      break;
   }
 
   if (parent_class->change_state)
@@ -819,6 +794,161 @@ gst_ximagesink_buffer_new (GstBufferPool *pool,
         }
     }
    
+  g_mutex_unlock (ximagesink->pool_lock);
+  
+  if (!ximage) /* We found no suitable image in the pool. Creating... */
+    ximage = gst_ximagesink_ximage_new (ximagesink,
+                                        ximagesink->width,
+                                        ximagesink->height);
+  
+  if (ximage)
+    {
+      buffer = gst_buffer_new ();
+      GST_BUFFER_POOL_PRIVATE (buffer) = ximage;
+      GST_BUFFER_DATA (buffer) = ximage->ximage->data;
+      GST_BUFFER_SIZE (buffer) = ximage->size;
+      return buffer;
+    }
+  else
+    return NULL;
+}
+
+static void
+gst_ximagesink_buffer_free (GstBufferPool *pool,
+                            GstBuffer *buffer, gpointer user_data)
+{
+  GstXImageSink *ximagesink;
+  GstXImage *ximage;
+  
+  ximagesink = GST_XIMAGESINK (user_data);
+  
+  ximage = GST_BUFFER_POOL_PRIVATE (buffer);
+  
+  /* If our geometry changed we can't reuse that image. */
+  if ( (ximage->width != ximagesink->width) ||
+       (ximage->height != ximagesink->height) )
+    gst_ximagesink_ximage_destroy (ximagesink, ximage);
+  else /* In that case we can reuse the image and add it to our image pool. */
+    {
+      g_mutex_lock (ximagesink->pool_lock);
+      ximagesink->image_pool = g_slist_prepend (ximagesink->image_pool, ximage);
+      g_mutex_unlock (ximagesink->pool_lock);
+    }
+    
+  GST_BUFFER_DATA (buffer) = NULL;
+
+  gst_buffer_default_free (buffer);
+}
+
+static void
+gst_ximagesink_imagepool_clear (GstXImageSink *ximagesink)
+{
+  g_mutex_lock(ximagesink->pool_lock);
+  
+  while (ximagesink->image_pool)
+    {
+      GstXImage *ximage = ximagesink->image_pool->data;
+      ximagesink->image_pool = g_slist_delete_link (ximagesink->image_pool,
+                                                    ximagesink->image_pool);
+      gst_ximagesink_ximage_destroy (ximagesink, ximage);
+    }
+  
+  g_mutex_unlock(ximagesink->pool_lock);
+}
+
+static GstBufferPool*
+gst_ximagesink_get_bufferpool (GstPad *pad)
+{
+  GstXImageSink *ximagesink;
+  
+  ximagesink = GST_XIMAGESINK (gst_pad_get_parent (pad));
+
+  if (!ximagesink->bufferpool) {
+    ximagesink->bufferpool = gst_buffer_pool_new (
+                NULL,          /* free */
+                NULL,          /* copy */
+                (GstBufferPoolBufferNewFunction) gst_ximagesink_buffer_new,
+                NULL,          /* buffer copy, the default is fine */
+                (GstBufferPoolBufferFreeFunction) gst_ximagesink_buffer_free,
+                ximagesink);
+
+    ximagesink->image_pool = NULL;
+  }
+
+  gst_buffer_pool_ref (ximagesink->bufferpool);
+
+  return ximagesink->bufferpool;
+}
+
+/* =========================================== */
+/*                                             */
+/*              Init & Class init              */
+/*                                             */
+/* =========================================== */
+
+static void
+gst_ximagesink_dispose (GObject *object)
+{
+  GstXImageSink *ximagesink;
+
+  ximagesink = GST_XIMAGESINK (object);
+
+  if (ximagesink->ximage)
+    {
+      gst_ximagesink_ximage_destroy (ximagesink, ximagesink->ximage);
+      ximagesink->ximage = NULL;
+    }
+    
+  if (ximagesink->image_pool)
+    gst_ximagesink_imagepool_clear (ximagesink);
+  
+  if (ximagesink->xwindow)
+    {
+      gst_ximagesink_xwindow_destroy (ximagesink, ximagesink->xwindow);
+      ximagesink->xwindow = NULL;
+    }
+  
+  if (ximagesink->xcontext)
+    gst_ximagesink_xcontext_clear (ximagesink);
+    
+  g_mutex_free (ximagesink->x_lock);
+  g_mutex_free (ximagesink->pool_lock);
+
+  if (ximagesink->bufferpool) 
+    gst_buffer_pool_free (ximagesink->bufferpool);
+
+  G_OBJECT_CLASS (parent_class)->dispose (object);
+}
+
+static void
+gst_ximagesink_init (GstXImageSink *ximagesink)
+{
+  ximagesink->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (
+                                         gst_ximagesink_sink_template_factory),
+                                         "sink");
+  gst_element_add_pad (GST_ELEMENT (ximagesink), ximagesink->sinkpad);
+
+  gst_pad_set_chain_function (ximagesink->sinkpad, gst_ximagesink_chain);
+  gst_pad_set_link_function (ximagesink->sinkpad, gst_ximagesink_sinkconnect);
+  gst_pad_set_getcaps_function (ximagesink->sinkpad, gst_ximagesink_getcaps);
+  gst_pad_set_bufferpool_function (ximagesink->sinkpad,
+                                   gst_ximagesink_get_bufferpool);
+
+  ximagesink->xcontext = NULL;
+  ximagesink->xwindow = NULL;
+  ximagesink->ximage = NULL;
+  ximagesink->clock = NULL;
+  
+  ximagesink->width = ximagesink->height = 0;
+  
+  ximagesink->x_lock = g_mutex_new ();
+  
+  ximagesink->pixel_width = ximagesink->pixel_height = 1;
+  
+  ximagesink->image_pool = NULL;
+  ximagesink->pool_lock = g_mutex_new ();
+
+  GST_FLAG_SET(ximagesink, GST_ELEMENT_THREAD_SUGGESTED);
   GST_FLAG_SET(ximagesink, GST_ELEMENT_EVENT_AWARE);
 }
 
@@ -843,13 +973,9 @@ gst_ximagesink_class_init (GstXImageSinkClass *klass)
   gstelement_class = (GstElementClass *) klass;
 
   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DISPLAY_NAME,
-    g_param_spec_string ("displayname", "displayname", "Name of display to use",
-            NULL, G_PARAM_READABLE));
-                                                                                
-  gobject_class->set_property = gst_ximagesink_set_property;
-  gobject_class->get_property = gst_ximagesink_get_property;
 
+  gobject_class->dispose = gst_ximagesink_dispose;
+  
   gstelement_class->change_state = gst_ximagesink_change_state;
   gstelement_class->set_clock    = gst_ximagesink_set_clock;
 }
@@ -900,10 +1026,9 @@ gst_ximagesink_get_type (void)
         NULL,
       };*/
       
-      ximagesink_type = g_type_register_static(GST_TYPE_ELEMENT,
-                                               "GstXImageSink",
-                                               &ximagesink_info,
-                                               0);
+      ximagesink_type = g_type_register_static (GST_TYPE_ELEMENT,
+                                                "GstXImageSink",
+                                                &ximagesink_info, 0);
       
       g_type_add_interface_static (ximagesink_type, GST_TYPE_INTERFACE,
         &iface_info);
@@ -919,7 +1044,8 @@ gst_ximagesink_get_type (void)
 static gboolean
 plugin_init (GstPlugin *plugin)
 {
-  if (!gst_element_register (plugin, "ximagesink", GST_RANK_NONE, GST_TYPE_XIMAGESINK))
+  if (!gst_element_register (plugin, "ximagesink",
+                             GST_RANK_SECONDARY, GST_TYPE_XIMAGESINK))
     return FALSE;
 
   return TRUE;
index edbd00e..1f1af6f 100644 (file)
@@ -22,6 +22,9 @@
 
 #include <gst/gst.h>
 
+/* FIXME : We should have a configure test for shm support */
+#define HAVE_SHM
+
 #ifdef HAVE_SHM
 #include <sys/ipc.h>
 #include <sys/shm.h>
@@ -125,7 +128,7 @@ struct _GstXImageSinkClass {
   GstElementClass parent_class;
 };
 
-GType gst_ximagesink_get_type (void);
+GType gst_ximagesink_get_type(void);
 
 G_END_DECLS