2011-03-03 15:48:23 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <2005> Julien Moutte <julien@moutte.net>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 23:05:09 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2011-03-03 15:48:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Object header */
|
|
|
|
#include "ximagesink.h"
|
|
|
|
|
|
|
|
/* Debugging category */
|
|
|
|
#include <gst/gstinfo.h>
|
|
|
|
|
2011-06-17 15:54:52 +00:00
|
|
|
/* Helper functions */
|
|
|
|
#include <gst/video/video.h>
|
2011-10-31 01:23:21 +00:00
|
|
|
#include <gst/video/gstvideometa.h>
|
2011-07-29 15:15:39 +00:00
|
|
|
#include <gst/video/gstvideopool.h>
|
2011-06-17 15:54:52 +00:00
|
|
|
|
2021-03-29 12:09:16 +00:00
|
|
|
GST_DEBUG_CATEGORY (gst_debug_x_image_pool);
|
2015-07-03 19:48:52 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_debug_x_image_pool
|
2011-03-03 15:48:23 +00:00
|
|
|
|
|
|
|
/* X11 stuff */
|
2011-04-06 10:26:47 +00:00
|
|
|
static gboolean error_caught = FALSE;
|
2011-03-03 15:48:23 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
gst_ximagesink_handle_xerror (Display * display, XErrorEvent * xevent)
|
|
|
|
{
|
|
|
|
char error_msg[1024];
|
|
|
|
|
|
|
|
XGetErrorText (display, xevent->error_code, error_msg, 1024);
|
|
|
|
GST_DEBUG ("ximagesink triggered an XError. error: %s", error_msg);
|
|
|
|
error_caught = TRUE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-26 15:02:41 +00:00
|
|
|
static GstMemory *
|
|
|
|
gst_ximage_memory_alloc (GstAllocator * allocator, gsize size,
|
|
|
|
GstAllocationParams * params)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_ximage_memory_free (GstAllocator * allocator, GstMemory * gmem)
|
|
|
|
{
|
|
|
|
GstXImageMemory *mem = (GstXImageMemory *) gmem;
|
|
|
|
GstXImageSink *ximagesink;
|
|
|
|
|
|
|
|
if (gmem->parent)
|
|
|
|
goto sub_mem;
|
|
|
|
|
|
|
|
ximagesink = mem->sink;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (ximagesink, "free memory %p", mem);
|
|
|
|
|
|
|
|
/* Hold the object lock to ensure the XContext doesn't disappear */
|
|
|
|
GST_OBJECT_LOCK (ximagesink);
|
|
|
|
/* We might have some buffers destroyed after changing state to NULL */
|
|
|
|
if (ximagesink->xcontext == NULL) {
|
|
|
|
GST_DEBUG_OBJECT (ximagesink, "Destroying XImage after XContext");
|
|
|
|
#ifdef HAVE_XSHM
|
|
|
|
/* Need to free the shared memory segment even if the x context
|
|
|
|
* was already cleaned up */
|
|
|
|
if (mem->SHMInfo.shmaddr != ((void *) -1)) {
|
|
|
|
shmdt (mem->SHMInfo.shmaddr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_mutex_lock (&ximagesink->x_lock);
|
|
|
|
|
|
|
|
#ifdef HAVE_XSHM
|
|
|
|
if (ximagesink->xcontext->use_xshm) {
|
|
|
|
if (mem->SHMInfo.shmaddr != ((void *) -1)) {
|
|
|
|
GST_DEBUG_OBJECT (ximagesink, "XServer ShmDetaching from 0x%x id 0x%lx",
|
|
|
|
mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
|
|
|
|
XShmDetach (ximagesink->xcontext->disp, &mem->SHMInfo);
|
|
|
|
XSync (ximagesink->xcontext->disp, FALSE);
|
|
|
|
shmdt (mem->SHMInfo.shmaddr);
|
|
|
|
mem->SHMInfo.shmaddr = (void *) -1;
|
|
|
|
}
|
|
|
|
if (mem->ximage)
|
|
|
|
XDestroyImage (mem->ximage);
|
|
|
|
} else
|
|
|
|
#endif /* HAVE_XSHM */
|
|
|
|
{
|
|
|
|
if (mem->ximage) {
|
|
|
|
XDestroyImage (mem->ximage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XSync (ximagesink->xcontext->disp, FALSE);
|
|
|
|
|
|
|
|
g_mutex_unlock (&ximagesink->x_lock);
|
|
|
|
|
|
|
|
beach:
|
|
|
|
GST_OBJECT_UNLOCK (ximagesink);
|
|
|
|
|
|
|
|
gst_object_unref (mem->sink);
|
|
|
|
|
|
|
|
sub_mem:
|
|
|
|
g_slice_free (GstXImageMemory, mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
ximage_memory_map (GstXImageMemory * mem, gsize maxsize, GstMapFlags flags)
|
|
|
|
{
|
|
|
|
return mem->ximage->data + mem->parent.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
ximage_memory_unmap (GstXImageMemory * mem)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstXImageMemory *
|
|
|
|
ximage_memory_share (GstXImageMemory * mem, gssize offset, gsize size)
|
|
|
|
{
|
|
|
|
GstXImageMemory *sub;
|
|
|
|
GstMemory *parent;
|
|
|
|
|
|
|
|
/* We can only share the complete memory */
|
|
|
|
if (offset != 0)
|
|
|
|
return NULL;
|
|
|
|
if (size != -1 && size != mem->size)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* find the real parent */
|
|
|
|
if ((parent = mem->parent.parent) == NULL)
|
|
|
|
parent = (GstMemory *) mem;
|
|
|
|
|
|
|
|
if (size == -1)
|
|
|
|
size = mem->parent.size - offset;
|
|
|
|
|
|
|
|
/* the shared memory is always readonly */
|
|
|
|
sub = g_slice_new (GstXImageMemory);
|
|
|
|
|
|
|
|
gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
|
|
|
|
GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->parent.allocator,
|
|
|
|
&mem->parent, mem->parent.maxsize, mem->parent.align,
|
|
|
|
mem->parent.offset + offset, size);
|
|
|
|
sub->sink = mem->sink;
|
|
|
|
sub->ximage = mem->ximage;
|
2013-04-09 10:27:48 +00:00
|
|
|
#ifdef HAVE_XSHM
|
2013-02-26 15:02:41 +00:00
|
|
|
sub->SHMInfo = mem->SHMInfo;
|
2013-04-09 10:27:48 +00:00
|
|
|
#endif
|
2013-02-26 15:02:41 +00:00
|
|
|
sub->x = mem->x;
|
|
|
|
sub->y = mem->y;
|
|
|
|
sub->width = mem->width;
|
|
|
|
sub->height = mem->height;
|
|
|
|
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef GstAllocator GstXImageMemoryAllocator;
|
|
|
|
typedef GstAllocatorClass GstXImageMemoryAllocatorClass;
|
|
|
|
|
|
|
|
GType ximage_memory_allocator_get_type (void);
|
|
|
|
G_DEFINE_TYPE (GstXImageMemoryAllocator, ximage_memory_allocator,
|
|
|
|
GST_TYPE_ALLOCATOR);
|
|
|
|
|
|
|
|
#define GST_XIMAGE_ALLOCATOR_NAME "ximage"
|
|
|
|
#define GST_TYPE_XIMAGE_MEMORY_ALLOCATOR (ximage_memory_allocator_get_type())
|
|
|
|
#define GST_IS_XIMAGE_MEMORY_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_XIMAGE_MEMORY_ALLOCATOR))
|
|
|
|
|
|
|
|
static void
|
|
|
|
ximage_memory_allocator_class_init (GstXImageMemoryAllocatorClass * klass)
|
|
|
|
{
|
|
|
|
GstAllocatorClass *allocator_class;
|
|
|
|
|
|
|
|
allocator_class = (GstAllocatorClass *) klass;
|
|
|
|
|
|
|
|
allocator_class->alloc = gst_ximage_memory_alloc;
|
|
|
|
allocator_class->free = gst_ximage_memory_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ximage_memory_allocator_init (GstXImageMemoryAllocator * allocator)
|
|
|
|
{
|
|
|
|
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
|
|
|
|
|
|
|
|
alloc->mem_type = GST_XIMAGE_ALLOCATOR_NAME;
|
|
|
|
alloc->mem_map = (GstMemoryMapFunction) ximage_memory_map;
|
|
|
|
alloc->mem_unmap = (GstMemoryUnmapFunction) ximage_memory_unmap;
|
|
|
|
alloc->mem_share = (GstMemoryShareFunction) ximage_memory_share;
|
|
|
|
/* fallback copy and is_span */
|
|
|
|
|
|
|
|
GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstMemory *
|
|
|
|
ximage_memory_alloc (GstXImageBufferPool * xpool)
|
2011-03-03 15:48:23 +00:00
|
|
|
{
|
2011-08-01 17:04:47 +00:00
|
|
|
GstXImageSink *ximagesink;
|
2011-03-03 15:48:23 +00:00
|
|
|
int (*handler) (Display *, XErrorEvent *);
|
|
|
|
gboolean success = FALSE;
|
|
|
|
GstXContext *xcontext;
|
2014-04-04 11:40:14 +00:00
|
|
|
gint width, height, align, offset;
|
2013-02-26 15:02:41 +00:00
|
|
|
GstXImageMemory *mem;
|
2011-03-03 15:48:23 +00:00
|
|
|
|
2011-08-01 17:04:47 +00:00
|
|
|
ximagesink = xpool->sink;
|
2011-03-03 15:48:23 +00:00
|
|
|
xcontext = ximagesink->xcontext;
|
|
|
|
|
2014-09-27 14:53:43 +00:00
|
|
|
width = xpool->padded_width;
|
|
|
|
height = xpool->padded_height;
|
2011-08-01 17:04:47 +00:00
|
|
|
|
2013-02-26 15:02:41 +00:00
|
|
|
mem = g_slice_new (GstXImageMemory);
|
|
|
|
|
2011-03-03 15:48:23 +00:00
|
|
|
#ifdef HAVE_XSHM
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->SHMInfo.shmaddr = ((void *) -1);
|
|
|
|
mem->SHMInfo.shmid = -1;
|
2011-03-03 15:48:23 +00:00
|
|
|
#endif
|
2014-09-27 14:53:43 +00:00
|
|
|
mem->x = xpool->align.padding_left;
|
|
|
|
mem->y = xpool->align.padding_top;
|
|
|
|
mem->width = GST_VIDEO_INFO_WIDTH (&xpool->info);
|
|
|
|
mem->height = GST_VIDEO_INFO_HEIGHT (&xpool->info);
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->sink = gst_object_ref (ximagesink);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
2013-02-26 15:02:41 +00:00
|
|
|
GST_DEBUG_OBJECT (ximagesink, "creating image %p (%dx%d)", mem,
|
2011-08-01 17:04:47 +00:00
|
|
|
width, height);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
2012-09-10 00:03:52 +00:00
|
|
|
g_mutex_lock (&ximagesink->x_lock);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
|
|
|
/* Setting an error handler to catch failure */
|
|
|
|
error_caught = FALSE;
|
|
|
|
handler = XSetErrorHandler (gst_ximagesink_handle_xerror);
|
|
|
|
|
|
|
|
#ifdef HAVE_XSHM
|
|
|
|
if (xcontext->use_xshm) {
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->ximage = XShmCreateImage (xcontext->disp,
|
2011-03-03 15:48:23 +00:00
|
|
|
xcontext->visual,
|
2013-02-26 15:02:41 +00:00
|
|
|
xcontext->depth, ZPixmap, NULL, &mem->SHMInfo, width, height);
|
|
|
|
if (!mem->ximage || error_caught) {
|
2012-09-10 00:03:52 +00:00
|
|
|
g_mutex_unlock (&ximagesink->x_lock);
|
2011-05-26 11:47:24 +00:00
|
|
|
|
|
|
|
/* Reset error flag */
|
|
|
|
error_caught = FALSE;
|
|
|
|
|
|
|
|
/* Push a warning */
|
|
|
|
GST_ELEMENT_WARNING (ximagesink, RESOURCE, WRITE,
|
|
|
|
("Failed to create output image buffer of %dx%d pixels",
|
2011-08-01 17:04:47 +00:00
|
|
|
width, height),
|
|
|
|
("could not XShmCreateImage a %dx%d image", width, height));
|
2011-05-26 11:47:24 +00:00
|
|
|
|
|
|
|
/* Retry without XShm */
|
|
|
|
ximagesink->xcontext->use_xshm = FALSE;
|
|
|
|
|
|
|
|
/* Hold X mutex again to try without XShm */
|
2012-09-10 00:03:52 +00:00
|
|
|
g_mutex_lock (&ximagesink->x_lock);
|
2011-05-26 11:47:24 +00:00
|
|
|
|
|
|
|
goto no_xshm;
|
|
|
|
}
|
2011-03-03 15:48:23 +00:00
|
|
|
|
|
|
|
/* we have to use the returned bytes_per_line for our shm size */
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->size = mem->ximage->bytes_per_line * mem->ximage->height;
|
2011-03-03 15:48:23 +00:00
|
|
|
GST_LOG_OBJECT (ximagesink,
|
|
|
|
"XShm image size is %" G_GSIZE_FORMAT ", width %d, stride %d",
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->size, width, mem->ximage->bytes_per_line);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
|
|
|
/* get shared memory */
|
2014-04-04 11:40:14 +00:00
|
|
|
align = 0;
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->SHMInfo.shmid =
|
|
|
|
shmget (IPC_PRIVATE, mem->size + align, IPC_CREAT | 0777);
|
|
|
|
if (mem->SHMInfo.shmid == -1)
|
2011-03-03 15:48:23 +00:00
|
|
|
goto shmget_failed;
|
|
|
|
|
|
|
|
/* attach */
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->SHMInfo.shmaddr = shmat (mem->SHMInfo.shmid, NULL, 0);
|
|
|
|
if (mem->SHMInfo.shmaddr == ((void *) -1))
|
2011-03-03 15:48:23 +00:00
|
|
|
goto shmat_failed;
|
|
|
|
|
|
|
|
/* now we can set up the image data */
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->ximage->data = mem->SHMInfo.shmaddr;
|
|
|
|
mem->SHMInfo.readOnly = FALSE;
|
2011-03-03 15:48:23 +00:00
|
|
|
|
2013-02-26 15:02:41 +00:00
|
|
|
if (XShmAttach (xcontext->disp, &mem->SHMInfo) == 0)
|
2011-03-03 15:48:23 +00:00
|
|
|
goto xattach_failed;
|
|
|
|
|
|
|
|
XSync (xcontext->disp, FALSE);
|
|
|
|
|
|
|
|
/* Now that everyone has attached, we can delete the shared memory segment.
|
|
|
|
* This way, it will be deleted as soon as we detach later, and not
|
|
|
|
* leaked if we crash. */
|
2013-02-26 15:02:41 +00:00
|
|
|
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
|
2011-04-06 10:26:47 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (ximagesink, "XServer ShmAttached to 0x%x, id 0x%lx",
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
|
2011-03-03 15:48:23 +00:00
|
|
|
} else
|
2011-05-26 11:47:24 +00:00
|
|
|
no_xshm:
|
2011-03-03 15:48:23 +00:00
|
|
|
#endif /* HAVE_XSHM */
|
|
|
|
{
|
|
|
|
guint allocsize;
|
|
|
|
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->ximage = XCreateImage (xcontext->disp,
|
2011-03-03 15:48:23 +00:00
|
|
|
xcontext->visual,
|
2011-08-01 17:04:47 +00:00
|
|
|
xcontext->depth, ZPixmap, 0, NULL, width, height, xcontext->bpp, 0);
|
2013-02-26 15:02:41 +00:00
|
|
|
if (!mem->ximage || error_caught)
|
2011-03-03 15:48:23 +00:00
|
|
|
goto create_failed;
|
|
|
|
|
|
|
|
/* upstream will assume that rowstrides are multiples of 4, but this
|
|
|
|
* doesn't always seem to be the case with XCreateImage() */
|
2013-02-26 15:02:41 +00:00
|
|
|
if ((mem->ximage->bytes_per_line % 4) != 0) {
|
2011-03-03 15:48:23 +00:00
|
|
|
GST_WARNING_OBJECT (ximagesink, "returned stride not a multiple of 4 as "
|
|
|
|
"usually assumed");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we have to use the returned bytes_per_line for our image size */
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->size = mem->ximage->bytes_per_line * mem->ximage->height;
|
2011-03-03 15:48:23 +00:00
|
|
|
|
|
|
|
/* alloc a bit more for unexpected strides to avoid crashes upstream.
|
|
|
|
* FIXME: if we get an unrounded stride, the image will be displayed
|
|
|
|
* distorted, since all upstream elements assume a rounded stride */
|
|
|
|
allocsize =
|
2013-02-26 15:02:41 +00:00
|
|
|
GST_ROUND_UP_4 (mem->ximage->bytes_per_line) * mem->ximage->height;
|
2011-03-03 15:48:23 +00:00
|
|
|
|
2014-04-04 11:40:14 +00:00
|
|
|
/* we want 16 byte aligned memory, g_malloc may only give 8 */
|
|
|
|
align = 15;
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->ximage->data = g_malloc (allocsize + align);
|
2011-03-03 15:48:23 +00:00
|
|
|
GST_LOG_OBJECT (ximagesink,
|
2019-08-29 17:42:39 +00:00
|
|
|
"non-XShm image size is %" G_GSIZE_FORMAT " (allocated: %u), width %d, "
|
2013-02-26 15:02:41 +00:00
|
|
|
"stride %d", mem->size, allocsize, width, mem->ximage->bytes_per_line);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
|
|
|
XSync (xcontext->disp, FALSE);
|
|
|
|
}
|
|
|
|
|
2013-02-26 15:02:41 +00:00
|
|
|
if ((offset = ((guintptr) mem->ximage->data & align)))
|
2012-08-10 14:58:47 +00:00
|
|
|
offset = (align + 1) - offset;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (ximagesink, "memory %p, align %d, offset %d",
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->ximage->data, align, offset);
|
2012-08-10 14:58:47 +00:00
|
|
|
|
2011-03-03 15:48:23 +00:00
|
|
|
/* Reset error handler */
|
|
|
|
error_caught = FALSE;
|
|
|
|
XSetErrorHandler (handler);
|
|
|
|
|
2013-03-05 15:41:52 +00:00
|
|
|
gst_memory_init (GST_MEMORY_CAST (mem), GST_MEMORY_FLAG_NO_SHARE,
|
|
|
|
xpool->allocator, NULL, mem->size + align, align, offset, mem->size);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
2012-09-10 00:03:52 +00:00
|
|
|
g_mutex_unlock (&ximagesink->x_lock);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
|
|
|
success = TRUE;
|
|
|
|
|
|
|
|
beach:
|
2013-02-26 15:02:41 +00:00
|
|
|
if (!success) {
|
|
|
|
g_slice_free (GstXImageMemory, mem);
|
|
|
|
mem = NULL;
|
|
|
|
}
|
2011-03-04 17:32:53 +00:00
|
|
|
|
2013-02-26 15:02:41 +00:00
|
|
|
return GST_MEMORY_CAST (mem);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
create_failed:
|
|
|
|
{
|
2012-09-10 00:03:52 +00:00
|
|
|
g_mutex_unlock (&ximagesink->x_lock);
|
2011-03-03 15:48:23 +00:00
|
|
|
/* Reset error handler */
|
|
|
|
error_caught = FALSE;
|
|
|
|
XSetErrorHandler (handler);
|
|
|
|
/* Push an error */
|
|
|
|
GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
|
|
|
|
("Failed to create output image buffer of %dx%d pixels",
|
2011-08-01 17:04:47 +00:00
|
|
|
width, height),
|
|
|
|
("could not XShmCreateImage a %dx%d image", width, height));
|
2011-03-03 15:48:23 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
2012-04-02 09:37:43 +00:00
|
|
|
#ifdef HAVE_XSHM
|
2011-03-03 15:48:23 +00:00
|
|
|
shmget_failed:
|
|
|
|
{
|
2012-09-10 00:03:52 +00:00
|
|
|
g_mutex_unlock (&ximagesink->x_lock);
|
2011-03-03 15:48:23 +00:00
|
|
|
GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
|
|
|
|
("Failed to create output image buffer of %dx%d pixels",
|
2011-08-01 17:04:47 +00:00
|
|
|
width, height),
|
2011-03-03 15:48:23 +00:00
|
|
|
("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
|
2013-02-26 15:02:41 +00:00
|
|
|
mem->size));
|
2011-03-03 15:48:23 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
shmat_failed:
|
|
|
|
{
|
2012-09-10 00:03:52 +00:00
|
|
|
g_mutex_unlock (&ximagesink->x_lock);
|
2011-03-03 15:48:23 +00:00
|
|
|
GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
|
|
|
|
("Failed to create output image buffer of %dx%d pixels",
|
2011-08-01 17:04:47 +00:00
|
|
|
width, height), ("Failed to shmat: %s", g_strerror (errno)));
|
2011-03-03 15:48:23 +00:00
|
|
|
/* Clean up the shared memory segment */
|
2013-02-26 15:02:41 +00:00
|
|
|
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
|
2011-03-03 15:48:23 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
xattach_failed:
|
|
|
|
{
|
2011-04-06 10:26:47 +00:00
|
|
|
/* Clean up the shared memory segment */
|
2013-02-26 15:02:41 +00:00
|
|
|
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
|
2012-09-10 00:03:52 +00:00
|
|
|
g_mutex_unlock (&ximagesink->x_lock);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
|
|
|
GST_ELEMENT_ERROR (ximagesink, RESOURCE, WRITE,
|
|
|
|
("Failed to create output image buffer of %dx%d pixels",
|
2011-08-01 17:04:47 +00:00
|
|
|
width, height), ("Failed to XShmAttach"));
|
2011-03-03 15:48:23 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
2012-04-02 09:37:43 +00:00
|
|
|
#endif
|
2011-03-03 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-06 10:26:47 +00:00
|
|
|
#ifdef HAVE_XSHM
|
|
|
|
/* This function checks that it is actually really possible to create an image
|
|
|
|
using XShm */
|
2011-03-03 15:48:23 +00:00
|
|
|
gboolean
|
2015-07-03 19:48:52 +00:00
|
|
|
gst_x_image_sink_check_xshm_calls (GstXImageSink * ximagesink,
|
2011-03-03 15:48:23 +00:00
|
|
|
GstXContext * xcontext)
|
|
|
|
{
|
|
|
|
XImage *ximage;
|
|
|
|
XShmSegmentInfo SHMInfo;
|
|
|
|
size_t size;
|
|
|
|
int (*handler) (Display *, XErrorEvent *);
|
|
|
|
gboolean result = FALSE;
|
|
|
|
gboolean did_attach = FALSE;
|
|
|
|
|
|
|
|
g_return_val_if_fail (xcontext != NULL, FALSE);
|
|
|
|
|
|
|
|
/* Sync to ensure any older errors are already processed */
|
|
|
|
XSync (xcontext->disp, FALSE);
|
|
|
|
|
|
|
|
/* Set defaults so we don't free these later unnecessarily */
|
|
|
|
SHMInfo.shmaddr = ((void *) -1);
|
|
|
|
SHMInfo.shmid = -1;
|
|
|
|
|
|
|
|
/* Setting an error handler to catch failure */
|
|
|
|
error_caught = FALSE;
|
|
|
|
handler = XSetErrorHandler (gst_ximagesink_handle_xerror);
|
|
|
|
|
|
|
|
/* Trying to create a 1x1 ximage */
|
|
|
|
GST_DEBUG ("XShmCreateImage of 1x1");
|
|
|
|
|
|
|
|
ximage = XShmCreateImage (xcontext->disp, xcontext->visual,
|
|
|
|
xcontext->depth, ZPixmap, NULL, &SHMInfo, 1, 1);
|
|
|
|
|
|
|
|
/* Might cause an error, sync to ensure it is noticed */
|
|
|
|
XSync (xcontext->disp, FALSE);
|
|
|
|
if (!ximage || error_caught) {
|
|
|
|
GST_WARNING ("could not XShmCreateImage a 1x1 image");
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
size = ximage->height * ximage->bytes_per_line;
|
|
|
|
|
|
|
|
SHMInfo.shmid = shmget (IPC_PRIVATE, size, IPC_CREAT | 0777);
|
|
|
|
if (SHMInfo.shmid == -1) {
|
|
|
|
GST_WARNING ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
|
|
|
|
size);
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
SHMInfo.shmaddr = shmat (SHMInfo.shmid, NULL, 0);
|
|
|
|
if (SHMInfo.shmaddr == ((void *) -1)) {
|
|
|
|
GST_WARNING ("Failed to shmat: %s", g_strerror (errno));
|
2011-04-06 10:26:47 +00:00
|
|
|
/* Clean up the shared memory segment */
|
2011-03-03 15:48:23 +00:00
|
|
|
shmctl (SHMInfo.shmid, IPC_RMID, NULL);
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
ximage->data = SHMInfo.shmaddr;
|
|
|
|
SHMInfo.readOnly = FALSE;
|
|
|
|
|
|
|
|
if (XShmAttach (xcontext->disp, &SHMInfo) == 0) {
|
|
|
|
GST_WARNING ("Failed to XShmAttach");
|
2011-04-06 10:26:47 +00:00
|
|
|
/* Clean up the shared memory segment */
|
2011-03-03 15:48:23 +00:00
|
|
|
shmctl (SHMInfo.shmid, IPC_RMID, NULL);
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sync to ensure we see any errors we caused */
|
|
|
|
XSync (xcontext->disp, FALSE);
|
|
|
|
|
2011-04-06 10:26:47 +00:00
|
|
|
/* Delete the shared memory segment as soon as everyone is attached.
|
2011-03-03 15:48:23 +00:00
|
|
|
* This way, it will be deleted as soon as we detach later, and not
|
|
|
|
* leaked if we crash. */
|
|
|
|
shmctl (SHMInfo.shmid, IPC_RMID, NULL);
|
|
|
|
|
|
|
|
if (!error_caught) {
|
2011-04-06 10:26:47 +00:00
|
|
|
GST_DEBUG ("XServer ShmAttached to 0x%x, id 0x%lx", SHMInfo.shmid,
|
|
|
|
SHMInfo.shmseg);
|
|
|
|
|
2011-03-03 15:48:23 +00:00
|
|
|
did_attach = TRUE;
|
|
|
|
/* store whether we succeeded in result */
|
|
|
|
result = TRUE;
|
2011-04-06 10:26:47 +00:00
|
|
|
} else {
|
|
|
|
GST_WARNING ("MIT-SHM extension check failed at XShmAttach. "
|
|
|
|
"Not using shared memory.");
|
2011-03-03 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
beach:
|
|
|
|
/* Sync to ensure we swallow any errors we caused and reset error_caught */
|
|
|
|
XSync (xcontext->disp, FALSE);
|
2011-04-06 10:26:47 +00:00
|
|
|
|
2011-03-03 15:48:23 +00:00
|
|
|
error_caught = FALSE;
|
|
|
|
XSetErrorHandler (handler);
|
|
|
|
|
|
|
|
if (did_attach) {
|
2011-04-06 10:26:47 +00:00
|
|
|
GST_DEBUG ("XServer ShmDetaching from 0x%x id 0x%lx",
|
|
|
|
SHMInfo.shmid, SHMInfo.shmseg);
|
2011-03-03 15:48:23 +00:00
|
|
|
XShmDetach (xcontext->disp, &SHMInfo);
|
|
|
|
XSync (xcontext->disp, FALSE);
|
|
|
|
}
|
|
|
|
if (SHMInfo.shmaddr != ((void *) -1))
|
|
|
|
shmdt (SHMInfo.shmaddr);
|
|
|
|
if (ximage)
|
|
|
|
XDestroyImage (ximage);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_XSHM */
|
|
|
|
|
|
|
|
/* bufferpool */
|
|
|
|
static void gst_ximage_buffer_pool_finalize (GObject * object);
|
|
|
|
|
2011-07-18 16:46:01 +00:00
|
|
|
#define gst_ximage_buffer_pool_parent_class parent_class
|
2011-03-03 15:48:23 +00:00
|
|
|
G_DEFINE_TYPE (GstXImageBufferPool, gst_ximage_buffer_pool,
|
|
|
|
GST_TYPE_BUFFER_POOL);
|
|
|
|
|
2011-06-20 11:28:12 +00:00
|
|
|
static const gchar **
|
2011-07-29 15:15:39 +00:00
|
|
|
ximage_buffer_pool_get_options (GstBufferPool * pool)
|
2011-06-20 11:28:12 +00:00
|
|
|
{
|
2011-10-31 01:23:21 +00:00
|
|
|
static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
|
2011-08-01 17:04:47 +00:00
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT, NULL
|
|
|
|
};
|
2011-06-20 11:28:12 +00:00
|
|
|
|
2011-07-29 15:15:39 +00:00
|
|
|
return options;
|
2011-06-20 11:28:12 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 15:48:23 +00:00
|
|
|
static gboolean
|
|
|
|
ximage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
|
|
|
|
{
|
|
|
|
GstXImageBufferPool *xpool = GST_XIMAGE_BUFFER_POOL_CAST (pool);
|
2011-06-17 15:54:52 +00:00
|
|
|
GstVideoInfo info;
|
2012-03-27 10:44:02 +00:00
|
|
|
GstCaps *caps;
|
2014-12-16 11:10:53 +00:00
|
|
|
guint size, min_buffers, max_buffers;
|
2011-03-03 15:48:23 +00:00
|
|
|
|
2014-12-16 11:10:53 +00:00
|
|
|
if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
|
|
|
|
&max_buffers))
|
2011-03-03 15:48:23 +00:00
|
|
|
goto wrong_config;
|
|
|
|
|
|
|
|
if (caps == NULL)
|
|
|
|
goto no_caps;
|
|
|
|
|
|
|
|
/* now parse the caps from the config */
|
2011-06-17 15:54:52 +00:00
|
|
|
if (!gst_video_info_from_caps (&info, caps))
|
2011-03-03 15:48:23 +00:00
|
|
|
goto wrong_caps;
|
|
|
|
|
2011-06-17 15:54:52 +00:00
|
|
|
GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
|
|
|
|
caps);
|
2011-03-03 17:39:38 +00:00
|
|
|
|
2011-03-03 15:48:23 +00:00
|
|
|
/* keep track of the width and height and caps */
|
2014-09-27 14:53:43 +00:00
|
|
|
if (xpool->caps)
|
|
|
|
gst_caps_unref (xpool->caps);
|
|
|
|
xpool->caps = gst_caps_ref (caps);
|
2011-06-20 11:28:12 +00:00
|
|
|
|
|
|
|
/* check for the configured metadata */
|
2014-09-27 14:53:43 +00:00
|
|
|
xpool->add_metavideo =
|
2011-07-29 15:15:39 +00:00
|
|
|
gst_buffer_pool_config_has_option (config,
|
2011-10-31 01:23:21 +00:00
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_META);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
2011-08-01 17:04:47 +00:00
|
|
|
/* parse extra alignment info */
|
2014-09-27 14:53:43 +00:00
|
|
|
xpool->need_alignment = gst_buffer_pool_config_has_option (config,
|
2011-08-01 17:04:47 +00:00
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
|
|
|
|
|
2014-09-27 14:53:43 +00:00
|
|
|
if (xpool->need_alignment) {
|
|
|
|
gst_buffer_pool_config_get_video_alignment (config, &xpool->align);
|
2011-08-01 17:04:47 +00:00
|
|
|
|
2014-09-27 14:53:43 +00:00
|
|
|
GST_LOG_OBJECT (pool, "padding %u-%ux%u-%u", xpool->align.padding_top,
|
|
|
|
xpool->align.padding_left, xpool->align.padding_left,
|
|
|
|
xpool->align.padding_bottom);
|
2011-08-01 17:04:47 +00:00
|
|
|
|
2012-08-20 09:19:37 +00:00
|
|
|
/* do padding and alignment */
|
2014-09-27 14:53:43 +00:00
|
|
|
gst_video_info_align (&info, &xpool->align);
|
2012-08-20 09:19:37 +00:00
|
|
|
|
2014-12-22 14:25:04 +00:00
|
|
|
gst_buffer_pool_config_set_video_alignment (config, &xpool->align);
|
|
|
|
|
2011-08-01 17:04:47 +00:00
|
|
|
/* we need the video metadata too now */
|
2014-09-27 14:53:43 +00:00
|
|
|
xpool->add_metavideo = TRUE;
|
2011-12-21 17:14:45 +00:00
|
|
|
} else {
|
2014-09-27 14:53:43 +00:00
|
|
|
gst_video_alignment_reset (&xpool->align);
|
2011-08-01 17:04:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add the padding */
|
2014-09-27 14:53:43 +00:00
|
|
|
xpool->padded_width =
|
|
|
|
GST_VIDEO_INFO_WIDTH (&info) + xpool->align.padding_left +
|
|
|
|
xpool->align.padding_right;
|
|
|
|
xpool->padded_height =
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&info) + xpool->align.padding_top +
|
|
|
|
xpool->align.padding_bottom;
|
2011-08-01 17:04:47 +00:00
|
|
|
|
2014-09-27 14:53:43 +00:00
|
|
|
xpool->info = info;
|
2012-08-20 09:19:37 +00:00
|
|
|
|
2014-12-16 11:10:53 +00:00
|
|
|
gst_buffer_pool_config_set_params (config, caps, info.size, min_buffers,
|
|
|
|
max_buffers);
|
|
|
|
|
2011-07-18 16:46:01 +00:00
|
|
|
return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
wrong_config:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (pool, "invalid config");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
no_caps:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (pool, "no caps in config");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
wrong_caps:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (pool,
|
|
|
|
"failed getting geometry from caps %" GST_PTR_FORMAT, caps);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function handles GstXImageBuffer creation depending on XShm availability */
|
|
|
|
static GstFlowReturn
|
|
|
|
ximage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
|
2012-03-15 13:06:24 +00:00
|
|
|
GstBufferPoolAcquireParams * params)
|
2011-03-03 15:48:23 +00:00
|
|
|
{
|
|
|
|
GstXImageBufferPool *xpool = GST_XIMAGE_BUFFER_POOL_CAST (pool);
|
2011-07-04 08:19:13 +00:00
|
|
|
GstVideoInfo *info;
|
2011-03-03 15:48:23 +00:00
|
|
|
GstBuffer *ximage;
|
2013-02-26 15:02:41 +00:00
|
|
|
GstMemory *mem;
|
2011-03-03 15:48:23 +00:00
|
|
|
|
2014-09-27 14:53:43 +00:00
|
|
|
info = &xpool->info;
|
2011-07-04 08:19:13 +00:00
|
|
|
|
2011-06-17 15:27:22 +00:00
|
|
|
ximage = gst_buffer_new ();
|
2013-02-26 15:02:41 +00:00
|
|
|
mem = ximage_memory_alloc (xpool);
|
|
|
|
if (mem == NULL) {
|
2011-06-17 15:27:22 +00:00
|
|
|
gst_buffer_unref (ximage);
|
2011-03-03 15:48:23 +00:00
|
|
|
goto no_buffer;
|
2011-06-17 15:27:22 +00:00
|
|
|
}
|
2013-02-26 15:02:41 +00:00
|
|
|
gst_buffer_append_memory (ximage, mem);
|
|
|
|
|
2014-09-27 14:53:43 +00:00
|
|
|
if (xpool->add_metavideo) {
|
2019-07-22 11:25:04 +00:00
|
|
|
GstVideoMeta *meta;
|
|
|
|
|
2011-10-31 01:23:21 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
|
2011-06-17 15:54:52 +00:00
|
|
|
/* these are just the defaults for now */
|
2019-07-22 11:25:04 +00:00
|
|
|
meta = gst_buffer_add_video_meta_full (ximage, GST_VIDEO_FRAME_FLAG_NONE,
|
2012-08-20 14:13:00 +00:00
|
|
|
GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
|
|
|
|
info->offset, info->stride);
|
2019-07-22 11:25:04 +00:00
|
|
|
|
|
|
|
gst_video_meta_set_alignment (meta, xpool->align);
|
2011-06-17 15:54:52 +00:00
|
|
|
}
|
2011-03-03 15:48:23 +00:00
|
|
|
*buffer = ximage;
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
/* ERROR */
|
|
|
|
no_buffer:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (pool, "can't create image");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GstBufferPool *
|
|
|
|
gst_ximage_buffer_pool_new (GstXImageSink * ximagesink)
|
|
|
|
{
|
|
|
|
GstXImageBufferPool *pool;
|
|
|
|
|
2015-07-03 19:48:52 +00:00
|
|
|
g_return_val_if_fail (GST_IS_X_IMAGE_SINK (ximagesink), NULL);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
|
|
|
pool = g_object_new (GST_TYPE_XIMAGE_BUFFER_POOL, NULL);
|
2017-05-15 16:47:22 +00:00
|
|
|
gst_object_ref_sink (pool);
|
2011-03-03 15:48:23 +00:00
|
|
|
pool->sink = gst_object_ref (ximagesink);
|
2013-02-26 15:02:41 +00:00
|
|
|
pool->allocator = g_object_new (GST_TYPE_XIMAGE_MEMORY_ALLOCATOR, NULL);
|
2017-05-15 16:47:22 +00:00
|
|
|
gst_object_ref_sink (pool->allocator);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
2011-03-03 17:39:38 +00:00
|
|
|
GST_LOG_OBJECT (pool, "new XImage buffer pool %p", pool);
|
|
|
|
|
2011-03-03 15:48:23 +00:00
|
|
|
return GST_BUFFER_POOL_CAST (pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_ximage_buffer_pool_class_init (GstXImageBufferPoolClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = (GObjectClass *) klass;
|
|
|
|
GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
|
|
|
|
|
|
|
|
gobject_class->finalize = gst_ximage_buffer_pool_finalize;
|
|
|
|
|
2011-07-29 15:15:39 +00:00
|
|
|
gstbufferpool_class->get_options = ximage_buffer_pool_get_options;
|
2011-03-03 15:48:23 +00:00
|
|
|
gstbufferpool_class->set_config = ximage_buffer_pool_set_config;
|
|
|
|
gstbufferpool_class->alloc_buffer = ximage_buffer_pool_alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_ximage_buffer_pool_init (GstXImageBufferPool * pool)
|
|
|
|
{
|
2014-09-27 14:53:43 +00:00
|
|
|
/* nothing to do here */
|
2011-03-03 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_ximage_buffer_pool_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstXImageBufferPool *pool = GST_XIMAGE_BUFFER_POOL_CAST (object);
|
|
|
|
|
2011-03-03 17:39:38 +00:00
|
|
|
GST_LOG_OBJECT (pool, "finalize XImage buffer pool %p", pool);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
2014-09-27 14:53:43 +00:00
|
|
|
if (pool->caps)
|
|
|
|
gst_caps_unref (pool->caps);
|
2011-03-03 15:48:23 +00:00
|
|
|
gst_object_unref (pool->sink);
|
2013-02-26 15:02:41 +00:00
|
|
|
gst_object_unref (pool->allocator);
|
2011-03-03 15:48:23 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_ximage_buffer_pool_parent_class)->finalize (object);
|
|
|
|
}
|