Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gstreamer.git] / sys / ximage / ximagepool.c
1 /* GStreamer
2  * Copyright (C) <2005> Julien Moutte <julien@moutte.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 /* Object header */
25 #include "ximagesink.h"
26
27 /* Debugging category */
28 #include <gst/gstinfo.h>
29
30 /* Helper functions */
31 #include <gst/video/video.h>
32 #include <gst/video/gstmetavideo.h>
33
34 GST_DEBUG_CATEGORY_EXTERN (gst_debug_ximagepool);
35 #define GST_CAT_DEFAULT gst_debug_ximagepool
36
37 static void gst_meta_ximage_free (GstMetaXImage * meta, GstBuffer * buffer);
38
39 /* ximage metadata */
40 const GstMetaInfo *
41 gst_meta_ximage_get_info (void)
42 {
43   static const GstMetaInfo *meta_ximage_info = NULL;
44
45   if (meta_ximage_info == NULL) {
46     meta_ximage_info = gst_meta_register ("GstMetaXImage", "GstMetaXImage",
47         sizeof (GstMetaXImage),
48         (GstMetaInitFunction) NULL,
49         (GstMetaFreeFunction) gst_meta_ximage_free,
50         (GstMetaCopyFunction) NULL, (GstMetaTransformFunction) NULL);
51   }
52   return meta_ximage_info;
53 }
54
55 /* X11 stuff */
56 static gboolean error_caught = FALSE;
57
58 static int
59 gst_ximagesink_handle_xerror (Display * display, XErrorEvent * xevent)
60 {
61   char error_msg[1024];
62
63   XGetErrorText (display, xevent->error_code, error_msg, 1024);
64   GST_DEBUG ("ximagesink triggered an XError. error: %s", error_msg);
65   error_caught = TRUE;
66   return 0;
67 }
68
69 GstMetaXImage *
70 gst_buffer_add_meta_ximage (GstBuffer * buffer, GstXImageSink * ximagesink,
71     gint width, gint height)
72 {
73   int (*handler) (Display *, XErrorEvent *);
74   gboolean success = FALSE;
75   GstXContext *xcontext;
76   GstMetaXImage *meta;
77
78   xcontext = ximagesink->xcontext;
79
80   meta =
81       (GstMetaXImage *) gst_buffer_add_meta (buffer, GST_META_INFO_XIMAGE,
82       NULL);
83 #ifdef HAVE_XSHM
84   meta->SHMInfo.shmaddr = ((void *) -1);
85   meta->SHMInfo.shmid = -1;
86 #endif
87   meta->width = width;
88   meta->height = height;
89   meta->sink = gst_object_ref (ximagesink);
90
91   GST_DEBUG_OBJECT (ximagesink, "creating image %p (%dx%d)", buffer,
92       meta->width, meta->height);
93
94   g_mutex_lock (ximagesink->x_lock);
95
96   /* Setting an error handler to catch failure */
97   error_caught = FALSE;
98   handler = XSetErrorHandler (gst_ximagesink_handle_xerror);
99
100 #ifdef HAVE_XSHM
101   if (xcontext->use_xshm) {
102     meta->ximage = XShmCreateImage (xcontext->disp,
103         xcontext->visual,
104         xcontext->depth,
105         ZPixmap, NULL, &meta->SHMInfo, meta->width, meta->height);
106     if (!meta->ximage || error_caught) {
107       g_mutex_unlock (ximagesink->x_lock);
108
109       /* Reset error flag */
110       error_caught = FALSE;
111
112       /* Push a warning */
113       GST_ELEMENT_WARNING (ximagesink, RESOURCE, WRITE,
114           ("Failed to create output image buffer of %dx%d pixels",
115               meta->width, meta->height),
116           ("could not XShmCreateImage a %dx%d image",
117               meta->width, meta->height));
118
119       /* Retry without XShm */
120       ximagesink->xcontext->use_xshm = FALSE;
121
122       /* Hold X mutex again to try without XShm */
123       g_mutex_lock (ximagesink->x_lock);
124
125       goto no_xshm;
126     }
127
128     /* we have to use the returned bytes_per_line for our shm size */
129     meta->size = meta->ximage->bytes_per_line * meta->ximage->height;
130     GST_LOG_OBJECT (ximagesink,
131         "XShm image size is %" G_GSIZE_FORMAT ", width %d, stride %d",
132         meta->size, meta->width, meta->ximage->bytes_per_line);
133
134     /* get shared memory */
135     meta->SHMInfo.shmid = shmget (IPC_PRIVATE, meta->size, IPC_CREAT | 0777);
136     if (meta->SHMInfo.shmid == -1)
137       goto shmget_failed;
138
139     /* attach */
140     meta->SHMInfo.shmaddr = shmat (meta->SHMInfo.shmid, NULL, 0);
141     if (meta->SHMInfo.shmaddr == ((void *) -1))
142       goto shmat_failed;
143
144     /* now we can set up the image data */
145     meta->ximage->data = meta->SHMInfo.shmaddr;
146     meta->SHMInfo.readOnly = FALSE;
147
148     if (XShmAttach (xcontext->disp, &meta->SHMInfo) == 0)
149       goto xattach_failed;
150
151     XSync (xcontext->disp, FALSE);
152
153     /* Now that everyone has attached, we can delete the shared memory segment.
154      * This way, it will be deleted as soon as we detach later, and not
155      * leaked if we crash. */
156     shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
157
158     GST_DEBUG_OBJECT (ximagesink, "XServer ShmAttached to 0x%x, id 0x%lx",
159         meta->SHMInfo.shmid, meta->SHMInfo.shmseg);
160   } else
161   no_xshm:
162 #endif /* HAVE_XSHM */
163   {
164     guint allocsize;
165
166     meta->ximage = XCreateImage (xcontext->disp,
167         xcontext->visual,
168         xcontext->depth,
169         ZPixmap, 0, NULL, meta->width, meta->height, xcontext->bpp, 0);
170     if (!meta->ximage || error_caught)
171       goto create_failed;
172
173     /* upstream will assume that rowstrides are multiples of 4, but this
174      * doesn't always seem to be the case with XCreateImage() */
175     if ((meta->ximage->bytes_per_line % 4) != 0) {
176       GST_WARNING_OBJECT (ximagesink, "returned stride not a multiple of 4 as "
177           "usually assumed");
178     }
179
180     /* we have to use the returned bytes_per_line for our image size */
181     meta->size = meta->ximage->bytes_per_line * meta->ximage->height;
182
183     /* alloc a bit more for unexpected strides to avoid crashes upstream.
184      * FIXME: if we get an unrounded stride, the image will be displayed
185      * distorted, since all upstream elements assume a rounded stride */
186     allocsize =
187         GST_ROUND_UP_4 (meta->ximage->bytes_per_line) * meta->ximage->height;
188
189     meta->ximage->data = g_malloc (allocsize);
190     GST_LOG_OBJECT (ximagesink,
191         "non-XShm image size is %" G_GSIZE_FORMAT " (alloced: %u), width %d, "
192         "stride %d", meta->size, allocsize, meta->width,
193         meta->ximage->bytes_per_line);
194
195     XSync (xcontext->disp, FALSE);
196   }
197
198   /* Reset error handler */
199   error_caught = FALSE;
200   XSetErrorHandler (handler);
201
202   gst_buffer_take_memory (buffer, -1,
203       gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE, meta->ximage->data,
204           NULL, meta->size, 0, meta->size));
205
206   g_mutex_unlock (ximagesink->x_lock);
207
208   success = TRUE;
209
210 beach:
211   if (!success)
212     meta = NULL;
213
214   return meta;
215
216   /* ERRORS */
217 create_failed:
218   {
219     g_mutex_unlock (ximagesink->x_lock);
220     /* Reset error handler */
221     error_caught = FALSE;
222     XSetErrorHandler (handler);
223     /* Push an error */
224     GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
225         ("Failed to create output image buffer of %dx%d pixels",
226             meta->width, meta->height),
227         ("could not XShmCreateImage a %dx%d image", meta->width, meta->height));
228     goto beach;
229   }
230 shmget_failed:
231   {
232     g_mutex_unlock (ximagesink->x_lock);
233     GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
234         ("Failed to create output image buffer of %dx%d pixels",
235             meta->width, meta->height),
236         ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
237             meta->size));
238     goto beach;
239   }
240 shmat_failed:
241   {
242     g_mutex_unlock (ximagesink->x_lock);
243     GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
244         ("Failed to create output image buffer of %dx%d pixels",
245             meta->width, meta->height),
246         ("Failed to shmat: %s", g_strerror (errno)));
247     /* Clean up the shared memory segment */
248     shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
249     goto beach;
250   }
251 xattach_failed:
252   {
253     /* Clean up the shared memory segment */
254     shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
255     g_mutex_unlock (ximagesink->x_lock);
256
257     GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
258         ("Failed to create output image buffer of %dx%d pixels",
259             meta->width, meta->height), ("Failed to XShmAttach"));
260     goto beach;
261   }
262 }
263
264 static void
265 gst_meta_ximage_free (GstMetaXImage * meta, GstBuffer * buffer)
266 {
267   GstXImageSink *ximagesink;
268
269   ximagesink = meta->sink;
270
271   GST_DEBUG_OBJECT (ximagesink, "free meta on buffer %p", buffer);
272
273   /* Hold the object lock to ensure the XContext doesn't disappear */
274   GST_OBJECT_LOCK (ximagesink);
275   /* We might have some buffers destroyed after changing state to NULL */
276   if (ximagesink->xcontext == NULL) {
277     GST_DEBUG_OBJECT (ximagesink, "Destroying XImage after XContext");
278 #ifdef HAVE_XSHM
279     /* Need to free the shared memory segment even if the x context
280      * was already cleaned up */
281     if (meta->SHMInfo.shmaddr != ((void *) -1)) {
282       shmdt (meta->SHMInfo.shmaddr);
283     }
284 #endif
285     goto beach;
286   }
287
288   g_mutex_lock (ximagesink->x_lock);
289
290 #ifdef HAVE_XSHM
291   if (ximagesink->xcontext->use_xshm) {
292     if (meta->SHMInfo.shmaddr != ((void *) -1)) {
293       GST_DEBUG_OBJECT (ximagesink, "XServer ShmDetaching from 0x%x id 0x%lx",
294           meta->SHMInfo.shmid, meta->SHMInfo.shmseg);
295       XShmDetach (ximagesink->xcontext->disp, &meta->SHMInfo);
296       XSync (ximagesink->xcontext->disp, FALSE);
297       shmdt (meta->SHMInfo.shmaddr);
298       meta->SHMInfo.shmaddr = (void *) -1;
299     }
300     if (meta->ximage)
301       XDestroyImage (meta->ximage);
302   } else
303 #endif /* HAVE_XSHM */
304   {
305     if (meta->ximage) {
306       XDestroyImage (meta->ximage);
307     }
308   }
309
310   XSync (ximagesink->xcontext->disp, FALSE);
311
312   g_mutex_unlock (ximagesink->x_lock);
313
314 beach:
315   GST_OBJECT_UNLOCK (ximagesink);
316
317   gst_object_unref (meta->sink);
318 }
319
320 #ifdef HAVE_XSHM
321 /* This function checks that it is actually really possible to create an image
322    using XShm */
323 gboolean
324 gst_ximagesink_check_xshm_calls (GstXImageSink * ximagesink,
325     GstXContext * xcontext)
326 {
327   XImage *ximage;
328   XShmSegmentInfo SHMInfo;
329   size_t size;
330   int (*handler) (Display *, XErrorEvent *);
331   gboolean result = FALSE;
332   gboolean did_attach = FALSE;
333
334   g_return_val_if_fail (xcontext != NULL, FALSE);
335
336   /* Sync to ensure any older errors are already processed */
337   XSync (xcontext->disp, FALSE);
338
339   /* Set defaults so we don't free these later unnecessarily */
340   SHMInfo.shmaddr = ((void *) -1);
341   SHMInfo.shmid = -1;
342
343   /* Setting an error handler to catch failure */
344   error_caught = FALSE;
345   handler = XSetErrorHandler (gst_ximagesink_handle_xerror);
346
347   /* Trying to create a 1x1 ximage */
348   GST_DEBUG ("XShmCreateImage of 1x1");
349
350   ximage = XShmCreateImage (xcontext->disp, xcontext->visual,
351       xcontext->depth, ZPixmap, NULL, &SHMInfo, 1, 1);
352
353   /* Might cause an error, sync to ensure it is noticed */
354   XSync (xcontext->disp, FALSE);
355   if (!ximage || error_caught) {
356     GST_WARNING ("could not XShmCreateImage a 1x1 image");
357     goto beach;
358   }
359   size = ximage->height * ximage->bytes_per_line;
360
361   SHMInfo.shmid = shmget (IPC_PRIVATE, size, IPC_CREAT | 0777);
362   if (SHMInfo.shmid == -1) {
363     GST_WARNING ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
364         size);
365     goto beach;
366   }
367
368   SHMInfo.shmaddr = shmat (SHMInfo.shmid, NULL, 0);
369   if (SHMInfo.shmaddr == ((void *) -1)) {
370     GST_WARNING ("Failed to shmat: %s", g_strerror (errno));
371     /* Clean up the shared memory segment */
372     shmctl (SHMInfo.shmid, IPC_RMID, NULL);
373     goto beach;
374   }
375
376   ximage->data = SHMInfo.shmaddr;
377   SHMInfo.readOnly = FALSE;
378
379   if (XShmAttach (xcontext->disp, &SHMInfo) == 0) {
380     GST_WARNING ("Failed to XShmAttach");
381     /* Clean up the shared memory segment */
382     shmctl (SHMInfo.shmid, IPC_RMID, NULL);
383     goto beach;
384   }
385
386   /* Sync to ensure we see any errors we caused */
387   XSync (xcontext->disp, FALSE);
388
389   /* Delete the shared memory segment as soon as everyone is attached.
390    * This way, it will be deleted as soon as we detach later, and not
391    * leaked if we crash. */
392   shmctl (SHMInfo.shmid, IPC_RMID, NULL);
393
394   if (!error_caught) {
395     GST_DEBUG ("XServer ShmAttached to 0x%x, id 0x%lx", SHMInfo.shmid,
396         SHMInfo.shmseg);
397
398     did_attach = TRUE;
399     /* store whether we succeeded in result */
400     result = TRUE;
401   } else {
402     GST_WARNING ("MIT-SHM extension check failed at XShmAttach. "
403         "Not using shared memory.");
404   }
405
406 beach:
407   /* Sync to ensure we swallow any errors we caused and reset error_caught */
408   XSync (xcontext->disp, FALSE);
409
410   error_caught = FALSE;
411   XSetErrorHandler (handler);
412
413   if (did_attach) {
414     GST_DEBUG ("XServer ShmDetaching from 0x%x id 0x%lx",
415         SHMInfo.shmid, SHMInfo.shmseg);
416     XShmDetach (xcontext->disp, &SHMInfo);
417     XSync (xcontext->disp, FALSE);
418   }
419   if (SHMInfo.shmaddr != ((void *) -1))
420     shmdt (SHMInfo.shmaddr);
421   if (ximage)
422     XDestroyImage (ximage);
423   return result;
424 }
425 #endif /* HAVE_XSHM */
426
427 /* bufferpool */
428 static void gst_ximage_buffer_pool_finalize (GObject * object);
429
430 #define GST_XIMAGE_BUFFER_POOL_GET_PRIVATE(obj)  \
431    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_XIMAGE_BUFFER_POOL, GstXImageBufferPoolPrivate))
432
433 struct _GstXImageBufferPoolPrivate
434 {
435   GstCaps *caps;
436   GstVideoInfo info;
437   gboolean add_metavideo;
438 };
439
440 G_DEFINE_TYPE (GstXImageBufferPool, gst_ximage_buffer_pool,
441     GST_TYPE_BUFFER_POOL);
442
443 static const gchar **
444 ximage_buffer_pool_get_metas (GstBufferPool * pool)
445 {
446   static const gchar *metas[] = { "GstMetaVideo", NULL };
447
448   return metas;
449 }
450
451 static gboolean
452 ximage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
453 {
454   GstXImageBufferPool *xpool = GST_XIMAGE_BUFFER_POOL_CAST (pool);
455   GstXImageBufferPoolPrivate *priv = xpool->priv;
456   GstVideoInfo info;
457   const GstCaps *caps;
458
459   if (!gst_buffer_pool_config_get (config, &caps, NULL, NULL, NULL, NULL, NULL))
460     goto wrong_config;
461
462   if (caps == NULL)
463     goto no_caps;
464
465   /* now parse the caps from the config */
466   if (!gst_video_info_from_caps (&info, caps))
467     goto wrong_caps;
468
469   priv->info = info;
470
471   GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
472       caps);
473
474   /* keep track of the width and height and caps */
475   if (priv->caps)
476     gst_caps_unref (priv->caps);
477   priv->caps = gst_caps_copy (caps);
478
479   /* check for the configured metadata */
480   priv->add_metavideo =
481       gst_buffer_pool_config_has_meta (config, GST_META_API_VIDEO);
482
483   return TRUE;
484
485   /* ERRORS */
486 wrong_config:
487   {
488     GST_WARNING_OBJECT (pool, "invalid config");
489     return FALSE;
490   }
491 no_caps:
492   {
493     GST_WARNING_OBJECT (pool, "no caps in config");
494     return FALSE;
495   }
496 wrong_caps:
497   {
498     GST_WARNING_OBJECT (pool,
499         "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
500     return FALSE;
501   }
502 }
503
504 /* This function handles GstXImageBuffer creation depending on XShm availability */
505 static GstFlowReturn
506 ximage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
507     GstBufferPoolParams * params)
508 {
509   GstXImageBufferPool *xpool = GST_XIMAGE_BUFFER_POOL_CAST (pool);
510   GstXImageBufferPoolPrivate *priv = xpool->priv;
511   GstBuffer *ximage;
512   GstMetaXImage *meta;
513
514   ximage = gst_buffer_new ();
515   meta =
516       gst_buffer_add_meta_ximage (ximage, xpool->sink, priv->info.width,
517       priv->info.height);
518   if (meta == NULL) {
519     gst_buffer_unref (ximage);
520     goto no_buffer;
521   }
522   if (priv->add_metavideo) {
523     /* these are just the defaults for now */
524     gst_buffer_add_meta_video (ximage, 0, priv->info.format, priv->info.width,
525         priv->info.height);
526   }
527   *buffer = ximage;
528
529   return GST_FLOW_OK;
530
531   /* ERROR */
532 no_buffer:
533   {
534     GST_WARNING_OBJECT (pool, "can't create image");
535     return GST_FLOW_ERROR;
536   }
537 }
538
539 static void
540 ximage_buffer_pool_free (GstBufferPool * pool, GstBuffer * buffer)
541 {
542   gst_buffer_unref (buffer);
543 }
544
545 GstBufferPool *
546 gst_ximage_buffer_pool_new (GstXImageSink * ximagesink)
547 {
548   GstXImageBufferPool *pool;
549
550   g_return_val_if_fail (GST_IS_XIMAGESINK (ximagesink), NULL);
551
552   pool = g_object_new (GST_TYPE_XIMAGE_BUFFER_POOL, NULL);
553   pool->sink = gst_object_ref (ximagesink);
554
555   GST_LOG_OBJECT (pool, "new XImage buffer pool %p", pool);
556
557   return GST_BUFFER_POOL_CAST (pool);
558 }
559
560 static void
561 gst_ximage_buffer_pool_class_init (GstXImageBufferPoolClass * klass)
562 {
563   GObjectClass *gobject_class = (GObjectClass *) klass;
564   GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
565
566   g_type_class_add_private (klass, sizeof (GstXImageBufferPoolPrivate));
567
568   gobject_class->finalize = gst_ximage_buffer_pool_finalize;
569
570   gstbufferpool_class->get_metas = ximage_buffer_pool_get_metas;
571   gstbufferpool_class->set_config = ximage_buffer_pool_set_config;
572   gstbufferpool_class->alloc_buffer = ximage_buffer_pool_alloc;
573   gstbufferpool_class->free_buffer = ximage_buffer_pool_free;
574 }
575
576 static void
577 gst_ximage_buffer_pool_init (GstXImageBufferPool * pool)
578 {
579   pool->priv = GST_XIMAGE_BUFFER_POOL_GET_PRIVATE (pool);
580 }
581
582 static void
583 gst_ximage_buffer_pool_finalize (GObject * object)
584 {
585   GstXImageBufferPool *pool = GST_XIMAGE_BUFFER_POOL_CAST (object);
586   GstXImageBufferPoolPrivate *priv = pool->priv;
587
588   GST_LOG_OBJECT (pool, "finalize XImage buffer pool %p", pool);
589
590   if (priv->caps)
591     gst_caps_unref (priv->caps);
592   gst_object_unref (pool->sink);
593
594   G_OBJECT_CLASS (gst_ximage_buffer_pool_parent_class)->finalize (object);
595 }