2 * Copyright (C) <2005> Julien Moutte <julien@moutte.net>
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.
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.
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.
25 #include "ximagesink.h"
27 /* Debugging category */
28 #include <gst/gstinfo.h>
30 GST_DEBUG_CATEGORY_EXTERN (gst_debug_ximagepool);
31 #define GST_CAT_DEFAULT gst_debug_ximagepool
33 static void gst_meta_ximage_free (GstMetaXImage * meta, GstBuffer * buffer);
37 gst_meta_ximage_get_info (void)
39 static const GstMetaInfo *meta_ximage_info = NULL;
41 if (meta_ximage_info == NULL) {
42 meta_ximage_info = gst_meta_register ("GstMetaXImage", "GstMetaXImage",
43 sizeof (GstMetaXImage),
44 (GstMetaInitFunction) NULL,
45 (GstMetaFreeFunction) gst_meta_ximage_free,
46 (GstMetaCopyFunction) NULL, (GstMetaTransformFunction) NULL);
48 return meta_ximage_info;
52 static gboolean error_caught = FALSE;
55 gst_ximagesink_handle_xerror (Display * display, XErrorEvent * xevent)
59 XGetErrorText (display, xevent->error_code, error_msg, 1024);
60 GST_DEBUG ("ximagesink triggered an XError. error: %s", error_msg);
66 gst_buffer_add_meta_ximage (GstBuffer * buffer, GstXImageSink * ximagesink,
67 gint width, gint height)
69 int (*handler) (Display *, XErrorEvent *);
70 gboolean success = FALSE;
71 GstXContext *xcontext;
74 xcontext = ximagesink->xcontext;
77 (GstMetaXImage *) gst_buffer_add_meta (buffer, GST_META_INFO_XIMAGE,
80 meta->SHMInfo.shmaddr = ((void *) -1);
81 meta->SHMInfo.shmid = -1;
84 meta->height = height;
85 meta->sink = gst_object_ref (ximagesink);
87 GST_DEBUG_OBJECT (ximagesink, "creating image %p (%dx%d)", buffer,
88 meta->width, meta->height);
90 g_mutex_lock (ximagesink->x_lock);
92 /* Setting an error handler to catch failure */
94 handler = XSetErrorHandler (gst_ximagesink_handle_xerror);
97 if (xcontext->use_xshm) {
98 meta->ximage = XShmCreateImage (xcontext->disp,
101 ZPixmap, NULL, &meta->SHMInfo, meta->width, meta->height);
102 if (!meta->ximage || error_caught)
105 /* we have to use the returned bytes_per_line for our shm size */
106 meta->size = meta->ximage->bytes_per_line * meta->ximage->height;
107 GST_LOG_OBJECT (ximagesink,
108 "XShm image size is %" G_GSIZE_FORMAT ", width %d, stride %d",
109 meta->size, meta->width, meta->ximage->bytes_per_line);
111 /* get shared memory */
112 meta->SHMInfo.shmid = shmget (IPC_PRIVATE, meta->size, IPC_CREAT | 0777);
113 if (meta->SHMInfo.shmid == -1)
117 meta->SHMInfo.shmaddr = shmat (meta->SHMInfo.shmid, NULL, 0);
118 if (meta->SHMInfo.shmaddr == ((void *) -1))
121 /* now we can set up the image data */
122 meta->ximage->data = meta->SHMInfo.shmaddr;
123 meta->SHMInfo.readOnly = FALSE;
125 if (XShmAttach (xcontext->disp, &meta->SHMInfo) == 0)
128 XSync (xcontext->disp, FALSE);
130 /* Now that everyone has attached, we can delete the shared memory segment.
131 * This way, it will be deleted as soon as we detach later, and not
132 * leaked if we crash. */
133 shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
135 GST_DEBUG_OBJECT (ximagesink, "XServer ShmAttached to 0x%x, id 0x%lx",
136 meta->SHMInfo.shmid, meta->SHMInfo.shmseg);
138 #endif /* HAVE_XSHM */
142 meta->ximage = XCreateImage (xcontext->disp,
145 ZPixmap, 0, NULL, meta->width, meta->height, xcontext->bpp, 0);
146 if (!meta->ximage || error_caught)
149 /* upstream will assume that rowstrides are multiples of 4, but this
150 * doesn't always seem to be the case with XCreateImage() */
151 if ((meta->ximage->bytes_per_line % 4) != 0) {
152 GST_WARNING_OBJECT (ximagesink, "returned stride not a multiple of 4 as "
156 /* we have to use the returned bytes_per_line for our image size */
157 meta->size = meta->ximage->bytes_per_line * meta->ximage->height;
159 /* alloc a bit more for unexpected strides to avoid crashes upstream.
160 * FIXME: if we get an unrounded stride, the image will be displayed
161 * distorted, since all upstream elements assume a rounded stride */
163 GST_ROUND_UP_4 (meta->ximage->bytes_per_line) * meta->ximage->height;
165 meta->ximage->data = g_malloc (allocsize);
166 GST_LOG_OBJECT (ximagesink,
167 "non-XShm image size is %" G_GSIZE_FORMAT " (alloced: %u), width %d, "
168 "stride %d", meta->size, allocsize, meta->width,
169 meta->ximage->bytes_per_line);
171 XSync (xcontext->disp, FALSE);
174 /* Reset error handler */
175 error_caught = FALSE;
176 XSetErrorHandler (handler);
178 gst_buffer_take_memory (buffer,
179 gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE, meta->ximage->data,
180 NULL, meta->size, 0, meta->size));
182 g_mutex_unlock (ximagesink->x_lock);
195 g_mutex_unlock (ximagesink->x_lock);
196 /* Reset error handler */
197 error_caught = FALSE;
198 XSetErrorHandler (handler);
200 GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
201 ("Failed to create output image buffer of %dx%d pixels",
202 meta->width, meta->height),
203 ("could not XShmCreateImage a %dx%d image", meta->width, meta->height));
208 g_mutex_unlock (ximagesink->x_lock);
209 GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
210 ("Failed to create output image buffer of %dx%d pixels",
211 meta->width, meta->height),
212 ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
218 g_mutex_unlock (ximagesink->x_lock);
219 GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
220 ("Failed to create output image buffer of %dx%d pixels",
221 meta->width, meta->height),
222 ("Failed to shmat: %s", g_strerror (errno)));
223 /* Clean up the shared memory segment */
224 shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
229 /* Clean up the shared memory segment */
230 shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
231 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), ("Failed to XShmAttach"));
241 gst_meta_ximage_free (GstMetaXImage * meta, GstBuffer * buffer)
243 GstXImageSink *ximagesink;
245 ximagesink = meta->sink;
247 GST_DEBUG_OBJECT (ximagesink, "free meta on buffer %p", buffer);
249 /* Hold the object lock to ensure the XContext doesn't disappear */
250 GST_OBJECT_LOCK (ximagesink);
251 /* We might have some buffers destroyed after changing state to NULL */
252 if (ximagesink->xcontext == NULL) {
253 GST_DEBUG_OBJECT (ximagesink, "Destroying XImage after XContext");
255 /* Need to free the shared memory segment even if the x context
256 * was already cleaned up */
257 if (meta->SHMInfo.shmaddr != ((void *) -1)) {
258 shmdt (meta->SHMInfo.shmaddr);
264 g_mutex_lock (ximagesink->x_lock);
267 if (ximagesink->xcontext->use_xshm) {
268 if (meta->SHMInfo.shmaddr != ((void *) -1)) {
269 GST_DEBUG_OBJECT (ximagesink, "XServer ShmDetaching from 0x%x id 0x%lx",
270 meta->SHMInfo.shmid, meta->SHMInfo.shmseg);
271 XShmDetach (ximagesink->xcontext->disp, &meta->SHMInfo);
272 XSync (ximagesink->xcontext->disp, FALSE);
273 shmdt (meta->SHMInfo.shmaddr);
274 meta->SHMInfo.shmaddr = (void *) -1;
277 XDestroyImage (meta->ximage);
279 #endif /* HAVE_XSHM */
282 XDestroyImage (meta->ximage);
286 XSync (ximagesink->xcontext->disp, FALSE);
288 g_mutex_unlock (ximagesink->x_lock);
291 GST_OBJECT_UNLOCK (ximagesink);
293 gst_object_unref (meta->sink);
297 gst_ximage_buffer_new (GstXImageSink * ximagesink, gint width, gint height)
302 buffer = gst_buffer_new ();
303 meta = gst_buffer_add_meta_ximage (buffer, ximagesink, width, height);
305 gst_buffer_unref (buffer);
312 /* This function checks that it is actually really possible to create an image
315 gst_ximagesink_check_xshm_calls (GstXImageSink * ximagesink,
316 GstXContext * xcontext)
319 XShmSegmentInfo SHMInfo;
321 int (*handler) (Display *, XErrorEvent *);
322 gboolean result = FALSE;
323 gboolean did_attach = FALSE;
325 g_return_val_if_fail (xcontext != NULL, FALSE);
327 /* Sync to ensure any older errors are already processed */
328 XSync (xcontext->disp, FALSE);
330 /* Set defaults so we don't free these later unnecessarily */
331 SHMInfo.shmaddr = ((void *) -1);
334 /* Setting an error handler to catch failure */
335 error_caught = FALSE;
336 handler = XSetErrorHandler (gst_ximagesink_handle_xerror);
338 /* Trying to create a 1x1 ximage */
339 GST_DEBUG ("XShmCreateImage of 1x1");
341 ximage = XShmCreateImage (xcontext->disp, xcontext->visual,
342 xcontext->depth, ZPixmap, NULL, &SHMInfo, 1, 1);
344 /* Might cause an error, sync to ensure it is noticed */
345 XSync (xcontext->disp, FALSE);
346 if (!ximage || error_caught) {
347 GST_WARNING ("could not XShmCreateImage a 1x1 image");
350 size = ximage->height * ximage->bytes_per_line;
352 SHMInfo.shmid = shmget (IPC_PRIVATE, size, IPC_CREAT | 0777);
353 if (SHMInfo.shmid == -1) {
354 GST_WARNING ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
359 SHMInfo.shmaddr = shmat (SHMInfo.shmid, NULL, 0);
360 if (SHMInfo.shmaddr == ((void *) -1)) {
361 GST_WARNING ("Failed to shmat: %s", g_strerror (errno));
362 /* Clean up the shared memory segment */
363 shmctl (SHMInfo.shmid, IPC_RMID, NULL);
367 ximage->data = SHMInfo.shmaddr;
368 SHMInfo.readOnly = FALSE;
370 if (XShmAttach (xcontext->disp, &SHMInfo) == 0) {
371 GST_WARNING ("Failed to XShmAttach");
372 /* Clean up the shared memory segment */
373 shmctl (SHMInfo.shmid, IPC_RMID, NULL);
377 /* Sync to ensure we see any errors we caused */
378 XSync (xcontext->disp, FALSE);
380 /* Delete the shared memory segment as soon as everyone is attached.
381 * This way, it will be deleted as soon as we detach later, and not
382 * leaked if we crash. */
383 shmctl (SHMInfo.shmid, IPC_RMID, NULL);
386 GST_DEBUG ("XServer ShmAttached to 0x%x, id 0x%lx", SHMInfo.shmid,
390 /* store whether we succeeded in result */
393 GST_WARNING ("MIT-SHM extension check failed at XShmAttach. "
394 "Not using shared memory.");
398 /* Sync to ensure we swallow any errors we caused and reset error_caught */
399 XSync (xcontext->disp, FALSE);
401 error_caught = FALSE;
402 XSetErrorHandler (handler);
405 GST_DEBUG ("XServer ShmDetaching from 0x%x id 0x%lx",
406 SHMInfo.shmid, SHMInfo.shmseg);
407 XShmDetach (xcontext->disp, &SHMInfo);
408 XSync (xcontext->disp, FALSE);
410 if (SHMInfo.shmaddr != ((void *) -1))
411 shmdt (SHMInfo.shmaddr);
413 XDestroyImage (ximage);
416 #endif /* HAVE_XSHM */
419 static void gst_ximage_buffer_pool_finalize (GObject * object);
421 #define GST_XIMAGE_BUFFER_POOL_GET_PRIVATE(obj) \
422 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_XIMAGE_BUFFER_POOL, GstXImageBufferPoolPrivate))
424 struct _GstXImageBufferPoolPrivate
430 G_DEFINE_TYPE (GstXImageBufferPool, gst_ximage_buffer_pool,
431 GST_TYPE_BUFFER_POOL);
434 ximage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
436 GstXImageBufferPool *xpool = GST_XIMAGE_BUFFER_POOL_CAST (pool);
437 GstXImageBufferPoolPrivate *priv = xpool->priv;
438 GstStructure *structure;
442 if (!gst_buffer_pool_config_get (config, &caps, NULL, NULL, NULL, NULL,
449 /* now parse the caps from the config */
450 structure = gst_caps_get_structure (caps, 0);
452 if (!gst_structure_get_int (structure, "width", &width) ||
453 !gst_structure_get_int (structure, "height", &height))
456 GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, width, height, caps);
458 /* keep track of the width and height and caps */
460 gst_caps_unref (priv->caps);
461 priv->caps = gst_caps_copy (caps);
463 priv->height = height;
470 GST_WARNING_OBJECT (pool, "invalid config");
475 GST_WARNING_OBJECT (pool, "no caps in config");
480 GST_WARNING_OBJECT (pool,
481 "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
486 /* This function handles GstXImageBuffer creation depending on XShm availability */
488 ximage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
489 GstBufferPoolParams * params)
491 GstXImageBufferPool *xpool = GST_XIMAGE_BUFFER_POOL_CAST (pool);
492 GstXImageBufferPoolPrivate *priv = xpool->priv;
495 ximage = gst_ximage_buffer_new (xpool->sink, priv->width, priv->height);
506 GST_WARNING_OBJECT (pool, "can't create image");
507 return GST_FLOW_ERROR;
512 ximage_buffer_pool_free (GstBufferPool * pool, GstBuffer * buffer)
514 gst_buffer_unref (buffer);
518 gst_ximage_buffer_pool_new (GstXImageSink * ximagesink)
520 GstXImageBufferPool *pool;
522 g_return_val_if_fail (GST_IS_XIMAGESINK (ximagesink), NULL);
524 pool = g_object_new (GST_TYPE_XIMAGE_BUFFER_POOL, NULL);
525 pool->sink = gst_object_ref (ximagesink);
527 GST_LOG_OBJECT (pool, "new XImage buffer pool %p", pool);
529 return GST_BUFFER_POOL_CAST (pool);
533 gst_ximage_buffer_pool_class_init (GstXImageBufferPoolClass * klass)
535 GObjectClass *gobject_class = (GObjectClass *) klass;
536 GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
538 g_type_class_add_private (klass, sizeof (GstXImageBufferPoolPrivate));
540 gobject_class->finalize = gst_ximage_buffer_pool_finalize;
542 gstbufferpool_class->set_config = ximage_buffer_pool_set_config;
543 gstbufferpool_class->alloc_buffer = ximage_buffer_pool_alloc;
544 gstbufferpool_class->free_buffer = ximage_buffer_pool_free;
548 gst_ximage_buffer_pool_init (GstXImageBufferPool * pool)
550 pool->priv = GST_XIMAGE_BUFFER_POOL_GET_PRIVATE (pool);
554 gst_ximage_buffer_pool_finalize (GObject * object)
556 GstXImageBufferPool *pool = GST_XIMAGE_BUFFER_POOL_CAST (object);
557 GstXImageBufferPoolPrivate *priv = pool->priv;
559 GST_LOG_OBJECT (pool, "finalize XImage buffer pool %p", pool);
562 gst_caps_unref (priv->caps);
563 gst_object_unref (pool->sink);
565 G_OBJECT_CLASS (gst_ximage_buffer_pool_parent_class)->finalize (object);