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 "xvimagesink.h"
27 /* Debugging category */
28 #include <gst/gstinfo.h>
30 GST_DEBUG_CATEGORY_EXTERN (gst_debug_xvimagepool);
31 #define GST_CAT_DEFAULT gst_debug_xvimagepool
33 static void gst_meta_xvimage_free (GstMetaXvImage * meta, GstBuffer * buffer);
35 /* xvimage metadata */
37 gst_meta_xvimage_get_info (void)
39 static const GstMetaInfo *meta_xvimage_info = NULL;
41 if (meta_xvimage_info == NULL) {
42 meta_xvimage_info = gst_meta_register ("GstMetaXvImage", "GstMetaXvImage",
43 sizeof (GstMetaXvImage),
44 (GstMetaInitFunction) NULL,
45 (GstMetaFreeFunction) gst_meta_xvimage_free,
46 (GstMetaCopyFunction) NULL, (GstMetaTransformFunction) NULL);
48 return meta_xvimage_info;
52 static gboolean error_caught = FALSE;
55 gst_xvimagesink_handle_xerror (Display * display, XErrorEvent * xevent)
59 XGetErrorText (display, xevent->error_code, error_msg, 1024);
60 GST_DEBUG ("xvimagesink triggered an XError. error: %s", error_msg);
66 gst_buffer_add_meta_xvimage (GstBuffer * buffer, GstXvImageSink * xvimagesink,
67 gint width, gint height, gint im_format)
69 int (*handler) (Display *, XErrorEvent *);
70 gboolean success = FALSE;
71 GstXContext *xcontext;
74 xcontext = xvimagesink->xcontext;
77 (GstMetaXvImage *) gst_buffer_add_meta (buffer, GST_META_INFO_XVIMAGE,
80 meta->SHMInfo.shmaddr = ((void *) -1);
81 meta->SHMInfo.shmid = -1;
84 meta->height = height;
85 meta->sink = gst_object_ref (xvimagesink);
86 meta->im_format = im_format;
88 GST_DEBUG_OBJECT (xvimagesink, "creating image %p (%dx%d)", buffer,
89 meta->width, meta->height);
91 g_mutex_lock (xvimagesink->x_lock);
93 /* Setting an error handler to catch failure */
95 handler = XSetErrorHandler (gst_xvimagesink_handle_xerror);
98 if (xcontext->use_xshm) {
101 meta->xvimage = XvShmCreateImage (xcontext->disp,
102 xcontext->xv_port_id,
103 meta->im_format, NULL, meta->width, meta->height, &meta->SHMInfo);
104 if (!meta->xvimage || error_caught) {
105 g_mutex_unlock (xvimagesink->x_lock);
107 /* Reset error flag */
108 error_caught = FALSE;
111 GST_ELEMENT_WARNING (xvimagesink, RESOURCE, WRITE,
112 ("Failed to create output image buffer of %dx%d pixels",
113 meta->width, meta->height),
114 ("could not XShmCreateImage a %dx%d image",
115 meta->width, meta->height));
117 /* Retry without XShm */
118 xvimagesink->xcontext->use_xshm = FALSE;
120 /* Hold X mutex again to try without XShm */
121 g_mutex_lock (xvimagesink->x_lock);
125 /* we have to use the returned data_size for our shm size */
126 meta->size = meta->xvimage->data_size;
127 GST_LOG_OBJECT (xvimagesink, "XShm image size is %" G_GSIZE_FORMAT,
130 /* calculate the expected size. This is only for sanity checking the
131 * number we get from X. */
132 switch (meta->im_format) {
133 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
134 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
141 pitches[0] = GST_ROUND_UP_4 (meta->width);
142 offsets[1] = offsets[0] + pitches[0] * GST_ROUND_UP_2 (meta->height);
143 pitches[1] = GST_ROUND_UP_8 (meta->width) / 2;
145 offsets[1] + pitches[1] * GST_ROUND_UP_2 (meta->height) / 2;
146 pitches[2] = GST_ROUND_UP_8 (pitches[0]) / 2;
149 offsets[2] + pitches[2] * GST_ROUND_UP_2 (meta->height) / 2;
151 for (plane = 0; plane < meta->xvimage->num_planes; plane++) {
152 GST_DEBUG_OBJECT (xvimagesink,
153 "Plane %u has a expected pitch of %d bytes, " "offset of %d",
154 plane, pitches[plane], offsets[plane]);
158 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
159 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
160 expected_size = meta->height * GST_ROUND_UP_4 (meta->width * 2);
166 if (expected_size != 0 && meta->size != expected_size) {
167 GST_WARNING_OBJECT (xvimagesink,
168 "unexpected XShm image size (got %" G_GSIZE_FORMAT ", expected %d)",
169 meta->size, expected_size);
172 /* Be verbose about our XvImage stride */
176 for (plane = 0; plane < meta->xvimage->num_planes; plane++) {
177 GST_DEBUG_OBJECT (xvimagesink, "Plane %u has a pitch of %d bytes, "
178 "offset of %d", plane, meta->xvimage->pitches[plane],
179 meta->xvimage->offsets[plane]);
183 /* get shared memory */
184 meta->SHMInfo.shmid = shmget (IPC_PRIVATE, meta->size, IPC_CREAT | 0777);
185 if (meta->SHMInfo.shmid == -1)
189 meta->SHMInfo.shmaddr = shmat (meta->SHMInfo.shmid, NULL, 0);
190 if (meta->SHMInfo.shmaddr == ((void *) -1))
193 /* now we can set up the image data */
194 meta->xvimage->data = meta->SHMInfo.shmaddr;
195 meta->SHMInfo.readOnly = FALSE;
197 if (XShmAttach (xcontext->disp, &meta->SHMInfo) == 0)
200 XSync (xcontext->disp, FALSE);
202 /* Delete the shared memory segment as soon as we everyone is attached.
203 * This way, it will be deleted as soon as we detach later, and not
204 * leaked if we crash. */
205 shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
207 GST_DEBUG_OBJECT (xvimagesink, "XServer ShmAttached to 0x%x, id 0x%lx",
208 meta->SHMInfo.shmid, meta->SHMInfo.shmseg);
211 #endif /* HAVE_XSHM */
213 meta->xvimage = XvCreateImage (xcontext->disp,
214 xcontext->xv_port_id, meta->im_format, NULL, meta->width, meta->height);
215 if (!meta->xvimage || error_caught)
218 /* we have to use the returned data_size for our image size */
219 meta->size = meta->xvimage->data_size;
220 meta->xvimage->data = g_malloc (meta->size);
222 XSync (xcontext->disp, FALSE);
225 /* Reset error handler */
226 error_caught = FALSE;
227 XSetErrorHandler (handler);
229 gst_buffer_take_memory (buffer,
230 gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE, meta->xvimage->data,
231 NULL, meta->size, 0, meta->size));
233 g_mutex_unlock (xvimagesink->x_lock);
246 g_mutex_unlock (xvimagesink->x_lock);
247 /* Reset error handler */
248 error_caught = FALSE;
249 XSetErrorHandler (handler);
251 GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
252 ("Failed to create output image buffer of %dx%d pixels",
253 meta->width, meta->height),
254 ("could not XvShmCreateImage a %dx%d image", meta->width,
260 g_mutex_unlock (xvimagesink->x_lock);
261 GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
262 ("Failed to create output image buffer of %dx%d pixels",
263 meta->width, meta->height),
264 ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
270 g_mutex_unlock (xvimagesink->x_lock);
271 GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
272 ("Failed to create output image buffer of %dx%d pixels",
273 meta->width, meta->height),
274 ("Failed to shmat: %s", g_strerror (errno)));
275 /* Clean up the shared memory segment */
276 shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
281 /* Clean up the shared memory segment */
282 shmctl (meta->SHMInfo.shmid, IPC_RMID, NULL);
283 g_mutex_unlock (xvimagesink->x_lock);
285 GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
286 ("Failed to create output image buffer of %dx%d pixels",
287 meta->width, meta->height), ("Failed to XShmAttach"));
293 gst_meta_xvimage_free (GstMetaXvImage * meta, GstBuffer * buffer)
295 GstXvImageSink *xvimagesink;
297 xvimagesink = meta->sink;
299 GST_DEBUG_OBJECT (xvimagesink, "free meta on buffer %p", buffer);
301 /* Hold the object lock to ensure the XContext doesn't disappear */
302 GST_OBJECT_LOCK (xvimagesink);
303 /* We might have some buffers destroyed after changing state to NULL */
304 if (xvimagesink->xcontext == NULL) {
305 GST_DEBUG_OBJECT (xvimagesink, "Destroying XvImage after Xcontext");
307 /* Need to free the shared memory segment even if the x context
308 * was already cleaned up */
309 if (meta->SHMInfo.shmaddr != ((void *) -1)) {
310 shmdt (meta->SHMInfo.shmaddr);
316 g_mutex_lock (xvimagesink->x_lock);
319 if (xvimagesink->xcontext->use_xshm) {
320 if (meta->SHMInfo.shmaddr != ((void *) -1)) {
321 GST_DEBUG_OBJECT (xvimagesink, "XServer ShmDetaching from 0x%x id 0x%lx",
322 meta->SHMInfo.shmid, meta->SHMInfo.shmseg);
323 XShmDetach (xvimagesink->xcontext->disp, &meta->SHMInfo);
324 XSync (xvimagesink->xcontext->disp, FALSE);
325 shmdt (meta->SHMInfo.shmaddr);
326 meta->SHMInfo.shmaddr = (void *) -1;
329 XFree (meta->xvimage);
331 #endif /* HAVE_XSHM */
334 g_free (meta->xvimage->data);
335 XFree (meta->xvimage);
339 XSync (xvimagesink->xcontext->disp, FALSE);
341 g_mutex_unlock (xvimagesink->x_lock);
344 GST_OBJECT_UNLOCK (xvimagesink);
346 gst_object_unref (meta->sink);
350 gst_xvimage_buffer_new (GstXvImageSink * xvimagesink, gint width, gint height,
354 GstMetaXvImage *meta;
356 buffer = gst_buffer_new ();
358 gst_buffer_add_meta_xvimage (buffer, xvimagesink, width, height,
361 gst_buffer_unref (buffer);
368 /* This function checks that it is actually really possible to create an image
371 gst_xvimagesink_check_xshm_calls (GstXvImageSink * xvimagesink,
372 GstXContext * xcontext)
375 XShmSegmentInfo SHMInfo;
377 int (*handler) (Display *, XErrorEvent *);
378 gboolean result = FALSE;
379 gboolean did_attach = FALSE;
381 g_return_val_if_fail (xcontext != NULL, FALSE);
383 /* Sync to ensure any older errors are already processed */
384 XSync (xcontext->disp, FALSE);
386 /* Set defaults so we don't free these later unnecessarily */
387 SHMInfo.shmaddr = ((void *) -1);
390 /* Setting an error handler to catch failure */
391 error_caught = FALSE;
392 handler = XSetErrorHandler (gst_xvimagesink_handle_xerror);
394 /* Trying to create a 1x1 picture */
395 GST_DEBUG ("XvShmCreateImage of 1x1");
396 xvimage = XvShmCreateImage (xcontext->disp, xcontext->xv_port_id,
397 xcontext->im_format, NULL, 1, 1, &SHMInfo);
399 /* Might cause an error, sync to ensure it is noticed */
400 XSync (xcontext->disp, FALSE);
401 if (!xvimage || error_caught) {
402 GST_WARNING ("could not XvShmCreateImage a 1x1 image");
405 size = xvimage->data_size;
407 SHMInfo.shmid = shmget (IPC_PRIVATE, size, IPC_CREAT | 0777);
408 if (SHMInfo.shmid == -1) {
409 GST_WARNING ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
414 SHMInfo.shmaddr = shmat (SHMInfo.shmid, NULL, 0);
415 if (SHMInfo.shmaddr == ((void *) -1)) {
416 GST_WARNING ("Failed to shmat: %s", g_strerror (errno));
417 /* Clean up the shared memory segment */
418 shmctl (SHMInfo.shmid, IPC_RMID, NULL);
422 xvimage->data = SHMInfo.shmaddr;
423 SHMInfo.readOnly = FALSE;
425 if (XShmAttach (xcontext->disp, &SHMInfo) == 0) {
426 GST_WARNING ("Failed to XShmAttach");
427 /* Clean up the shared memory segment */
428 shmctl (SHMInfo.shmid, IPC_RMID, NULL);
432 /* Sync to ensure we see any errors we caused */
433 XSync (xcontext->disp, FALSE);
435 /* Delete the shared memory segment as soon as everyone is attached.
436 * This way, it will be deleted as soon as we detach later, and not
437 * leaked if we crash. */
438 shmctl (SHMInfo.shmid, IPC_RMID, NULL);
441 GST_DEBUG ("XServer ShmAttached to 0x%x, id 0x%lx", SHMInfo.shmid,
445 /* store whether we succeeded in result */
448 GST_WARNING ("MIT-SHM extension check failed at XShmAttach. "
449 "Not using shared memory.");
453 /* Sync to ensure we swallow any errors we caused and reset error_caught */
454 XSync (xcontext->disp, FALSE);
456 error_caught = FALSE;
457 XSetErrorHandler (handler);
460 GST_DEBUG ("XServer ShmDetaching from 0x%x id 0x%lx",
461 SHMInfo.shmid, SHMInfo.shmseg);
462 XShmDetach (xcontext->disp, &SHMInfo);
463 XSync (xcontext->disp, FALSE);
465 if (SHMInfo.shmaddr != ((void *) -1))
466 shmdt (SHMInfo.shmaddr);
471 #endif /* HAVE_XSHM */
474 static void gst_xvimage_buffer_pool_finalize (GObject * object);
476 #define GST_XVIMAGE_BUFFER_POOL_GET_PRIVATE(obj) \
477 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_XVIMAGE_BUFFER_POOL, GstXvImageBufferPoolPrivate))
479 struct _GstXvImageBufferPoolPrivate
486 G_DEFINE_TYPE (GstXvImageBufferPool, gst_xvimage_buffer_pool,
487 GST_TYPE_BUFFER_POOL);
490 xvimage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
492 GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
493 GstXvImageBufferPoolPrivate *priv = xvpool->priv;
494 GstStructure *structure;
498 if (!gst_buffer_pool_config_get (config, &caps, NULL, NULL, NULL, NULL,
505 /* now parse the caps from the config */
506 structure = gst_caps_get_structure (caps, 0);
508 if (!gst_structure_get_int (structure, "width", &width) ||
509 !gst_structure_get_int (structure, "height", &height))
512 GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, width, height, caps);
514 /* keep track of the width and height and caps */
516 gst_caps_unref (priv->caps);
517 priv->caps = gst_caps_copy (caps);
519 priv->height = height;
521 gst_xvimagesink_get_format_from_caps (xvpool->sink, (GstCaps *) caps);
523 if (priv->im_format == -1) {
524 GST_WARNING_OBJECT (xvpool->sink, "failed to get format from caps %"
525 GST_PTR_FORMAT, caps);
526 GST_ELEMENT_ERROR (xvpool->sink, RESOURCE, WRITE,
527 ("Failed to create output image buffer of %dx%d pixels",
528 priv->width, priv->height), ("Invalid input caps"));
537 GST_WARNING_OBJECT (pool, "invalid config");
542 GST_WARNING_OBJECT (pool, "no caps in config");
547 GST_WARNING_OBJECT (pool,
548 "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
553 /* This function handles GstXImageBuffer creation depending on XShm availability */
555 xvimage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
556 GstBufferPoolParams * params)
558 GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
559 GstXvImageBufferPoolPrivate *priv = xvpool->priv;
563 gst_xvimage_buffer_new (xvpool->sink, priv->width, priv->height,
575 GST_WARNING_OBJECT (pool, "can't create image");
576 return GST_FLOW_ERROR;
581 xvimage_buffer_pool_free (GstBufferPool * pool, GstBuffer * buffer)
583 gst_buffer_unref (buffer);
587 gst_xvimage_buffer_pool_new (GstXvImageSink * xvimagesink)
589 GstXvImageBufferPool *pool;
591 g_return_val_if_fail (GST_IS_XVIMAGESINK (xvimagesink), NULL);
593 pool = g_object_new (GST_TYPE_XVIMAGE_BUFFER_POOL, NULL);
594 pool->sink = gst_object_ref (xvimagesink);
596 GST_LOG_OBJECT (pool, "new XvImage buffer pool %p", pool);
598 return GST_BUFFER_POOL_CAST (pool);
602 gst_xvimage_buffer_pool_class_init (GstXvImageBufferPoolClass * klass)
604 GObjectClass *gobject_class = (GObjectClass *) klass;
605 GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
607 g_type_class_add_private (klass, sizeof (GstXvImageBufferPoolPrivate));
609 gobject_class->finalize = gst_xvimage_buffer_pool_finalize;
611 gstbufferpool_class->set_config = xvimage_buffer_pool_set_config;
612 gstbufferpool_class->alloc_buffer = xvimage_buffer_pool_alloc;
613 gstbufferpool_class->free_buffer = xvimage_buffer_pool_free;
617 gst_xvimage_buffer_pool_init (GstXvImageBufferPool * pool)
619 pool->priv = GST_XVIMAGE_BUFFER_POOL_GET_PRIVATE (pool);
623 gst_xvimage_buffer_pool_finalize (GObject * object)
625 GstXvImageBufferPool *pool = GST_XVIMAGE_BUFFER_POOL_CAST (object);
626 GstXvImageBufferPoolPrivate *priv = pool->priv;
628 GST_LOG_OBJECT (pool, "finalize XvImage buffer pool %p", pool);
631 gst_caps_unref (priv->caps);
632 gst_object_unref (pool->sink);
634 G_OBJECT_CLASS (gst_xvimage_buffer_pool_parent_class)->finalize (object);
637 /* This function tries to get a format matching with a given caps in the
638 supported list of formats we generated in gst_xvimagesink_get_xv_support */
640 gst_xvimagesink_get_format_from_caps (GstXvImageSink * xvimagesink,
645 g_return_val_if_fail (GST_IS_XVIMAGESINK (xvimagesink), 0);
647 list = xvimagesink->xcontext->formats_list;
650 GstXvImageFormat *format = list->data;
653 if (gst_caps_can_intersect (caps, format->caps)) {
654 return format->format;
657 list = g_list_next (list);