xvimagesink: use xvcontext for allocation

Make a new refcounted xvcontext object that handles the X connection.
Use the xvcontext to allocate images and windows. Move some code
around so that all X calls are made from the xvcontext object.
Make a GstXvImageAllocator object that allocates images from the xvcontext. We
can implement a copy function now for these memory objects now.
Make the bufferpool use the xvimageallocator object for its images.
This commit is contained in:
Wim Taymans 2013-03-13 11:13:58 +01:00
parent 5127d31cce
commit 0cf392837d
10 changed files with 2360 additions and 1921 deletions

View file

@ -1,6 +1,6 @@
plugin_LTLIBRARIES = libgstxvimagesink.la plugin_LTLIBRARIES = libgstxvimagesink.la
libgstxvimagesink_la_SOURCES = xvimagesink.c xvimage.c xvimagepool.c libgstxvimagesink_la_SOURCES = xvimagesink.c xvimage.c xvimagepool.c xvimageallocator.c xvcontext.c
libgstxvimagesink_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) $(GST_CFLAGS) $(X_CFLAGS) libgstxvimagesink_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) $(GST_CFLAGS) $(X_CFLAGS)
libgstxvimagesink_la_LIBADD = \ libgstxvimagesink_la_LIBADD = \
$(top_builddir)/gst-libs/gst/video/libgstvideo-$(GST_API_VERSION).la \ $(top_builddir)/gst-libs/gst/video/libgstvideo-$(GST_API_VERSION).la \
@ -11,4 +11,4 @@ libgstxvimagesink_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
libgstxvimagesink_la_DEPENDENCIES = $(top_builddir)/gst-libs/gst/video/libgstvideo-$(GST_API_VERSION).la libgstxvimagesink_la_DEPENDENCIES = $(top_builddir)/gst-libs/gst/video/libgstvideo-$(GST_API_VERSION).la
libgstxvimagesink_la_LIBTOOLFLAGS = --tag=disable-static libgstxvimagesink_la_LIBTOOLFLAGS = --tag=disable-static
noinst_HEADERS = xvimagesink.h xvimagepool.h noinst_HEADERS = xvimagesink.h xvimagepool.h xvimageallocator.h xvcontext.h

1119
sys/xvimage/xvcontext.c Normal file

File diff suppressed because it is too large Load diff

247
sys/xvimage/xvcontext.h Normal file
View file

@ -0,0 +1,247 @@
/* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_XVCONTEXT_H__
#define __GST_XVCONTEXT_H__
#ifdef HAVE_XSHM
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#endif /* HAVE_XSHM */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef HAVE_XSHM
#include <X11/extensions/XShm.h>
#endif /* HAVE_XSHM */
#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvlib.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <gst/video/video.h>
G_BEGIN_DECLS
typedef struct _GstXvContextConfig GstXvContextConfig;
typedef struct _GstXvImageFormat GstXvImageFormat;
typedef struct _GstXvContext GstXvContext;
/**
* GstXvContextConfig:
*
* current configuration of the context
*/
struct _GstXvContextConfig
{
gchar *display_name;
guint adaptor_nr;
/* port attributes */
gboolean autopaint_colorkey;
gint colorkey;
gboolean double_buffer;
gint brightness;
gint contrast;
gint hue;
gint saturation;
gboolean cb_changed;
};
/**
* GstXvImageFormat:
* @format: the image format
* @caps: generated #GstCaps for this image format
*
* Structure storing image format to #GstCaps association.
*/
struct _GstXvImageFormat
{
gint format;
GstVideoFormat vformat;
GstCaps *caps;
};
#define GST_TYPE_XVCONTEXT (gst_xvcontext_get_type())
#define GST_IS_XVCONTEXT(obj) (GST_IS_MINI_OBJECT_TYPE(obj, GST_TYPE_XVCONTEXT))
#define GST_XVCONTEXT_CAST(obj) ((GstXvContext *)obj)
#define GST_XVCONTEXT(obj) (GST_XVCONTEXT_CAST(obj))
/*
* GstXvContext:
* @disp: the X11 Display of this context
* @screen: the default Screen of Display @disp
* @screen_num: the Screen number of @screen
* @visual: the default Visual of Screen @screen
* @root: the root Window of Display @disp
* @white: the value of a white pixel on Screen @screen
* @black: the value of a black pixel on Screen @screen
* @depth: the color depth of Display @disp
* @bpp: the number of bits per pixel on Display @disp
* @endianness: the endianness of image bytes on Display @disp
* @width: the width in pixels of Display @disp
* @height: the height in pixels of Display @disp
* @widthmm: the width in millimeters of Display @disp
* @heightmm: the height in millimeters of Display @disp
* @par: the pixel aspect ratio calculated from @width, @widthmm and @height,
* @heightmm ratio
* @use_xshm: used to known wether of not XShm extension is usable or not even
* if the Extension is present
* @xv_port_id: the XVideo port ID
* @im_format: used to store at least a valid format for XShm calls checks
* @formats_list: list of supported image formats on @xv_port_id
* @channels_list: list of #GstColorBalanceChannels
* @caps: the #GstCaps that Display @disp can accept
*
* Structure used to store various informations collected/calculated for a
* Display.
*/
struct _GstXvContext
{
GstMiniObject parent;
GMutex lock;
Display *disp;
Screen *screen;
gint screen_num;
Visual *visual;
Window root;
gulong white, black;
gint depth;
gint bpp;
gint endianness;
gint width, height;
gint widthmm, heightmm;
GValue *par; /* calculated pixel aspect ratio */
gboolean use_xshm;
XvPortID xv_port_id;
guint nb_adaptors;
gchar **adaptors;
guint adaptor_nr;
gint im_format;
/* port features */
gboolean have_autopaint_colorkey;
gboolean have_colorkey;
gboolean have_double_buffer;
GList *formats_list;
GList *channels_list;
GstCaps *caps;
/* Optimisation storage for buffer_alloc return */
GstCaps *last_caps;
gint last_format;
gint last_width;
gint last_height;
};
GType gst_xvcontext_get_type (void);
void gst_xvcontext_config_clear (GstXvContextConfig *config);
GstXvContext * gst_xvcontext_new (GstXvContextConfig *config, GError **error);
/* refcounting */
static inline GstXvContext *
gst_xvcontext_ref (GstXvContext * xvcontext)
{
return GST_XVCONTEXT_CAST (gst_mini_object_ref (GST_MINI_OBJECT_CAST (
xvcontext)));
}
static inline void
gst_xvcontext_unref (GstXvContext * xvcontext)
{
gst_mini_object_unref (GST_MINI_OBJECT_CAST (xvcontext));
}
gint gst_xvcontext_get_format_from_info (GstXvContext * xvcontext,
GstVideoInfo * info);
void gst_xvcontext_set_synchronous (GstXvContext * xvcontext,
gboolean synchronous);
void gst_xvcontext_update_colorbalance (GstXvContext * xvcontext,
GstXvContextConfig * config);
typedef struct _GstXWindow GstXWindow;
/*
* GstXWindow:
* @win: the Window ID of this X11 window
* @width: the width in pixels of Window @win
* @height: the height in pixels of Window @win
* @internal: used to remember if Window @win was created internally or passed
* through the #GstVideoOverlay interface
* @gc: the Graphical Context of Window @win
*
* Structure used to store informations about a Window.
*/
struct _GstXWindow
{
GstXvContext *context;
Window win;
gint width, height;
gboolean have_render_rect;
GstVideoRectangle render_rect;
gboolean internal;
GC gc;
};
G_END_DECLS
GstXWindow * gst_xvcontext_create_xwindow (GstXvContext * context,
gint width, gint height);
GstXWindow * gst_xvcontext_create_xwindow_from_xid (GstXvContext * context, XID xid);
void gst_xwindow_destroy (GstXWindow * window);
void gst_xwindow_set_event_handling (GstXWindow * window, gboolean handle_events);
void gst_xwindow_set_title (GstXWindow * window, const gchar * title);
void gst_xwindow_update_geometry (GstXWindow * window);
void gst_xwindow_clear (GstXWindow * window);
void gst_xwindow_set_render_rectangle (GstXWindow * window,
gint x, gint y, gint width, gint height);
#endif /* __GST_XVCONTEXT_H__ */

View file

@ -23,6 +23,7 @@
#include "xvimagesink.h" #include "xvimagesink.h"
GST_DEBUG_CATEGORY (gst_debug_xvcontext);
GST_DEBUG_CATEGORY (gst_debug_xvimagepool); GST_DEBUG_CATEGORY (gst_debug_xvimagepool);
GST_DEBUG_CATEGORY (gst_debug_xvimagesink); GST_DEBUG_CATEGORY (gst_debug_xvimagesink);
GST_DEBUG_CATEGORY_STATIC (GST_CAT_PERFORMANCE); GST_DEBUG_CATEGORY_STATIC (GST_CAT_PERFORMANCE);
@ -34,6 +35,8 @@ plugin_init (GstPlugin * plugin)
GST_RANK_PRIMARY, GST_TYPE_XVIMAGESINK)) GST_RANK_PRIMARY, GST_TYPE_XVIMAGESINK))
return FALSE; return FALSE;
GST_DEBUG_CATEGORY_INIT (gst_debug_xvcontext, "xcontext", 0,
"xcontext miniobject");
GST_DEBUG_CATEGORY_INIT (gst_debug_xvimagesink, "xvimagesink", 0, GST_DEBUG_CATEGORY_INIT (gst_debug_xvimagesink, "xvimagesink", 0,
"xvimagesink element"); "xvimagesink element");
GST_DEBUG_CATEGORY_INIT (gst_debug_xvimagepool, "xvimagepool", 0, GST_DEBUG_CATEGORY_INIT (gst_debug_xvimagepool, "xvimagepool", 0,

View file

@ -0,0 +1,651 @@
/* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_XSHM
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#endif /* HAVE_XSHM */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef HAVE_XSHM
#include <X11/extensions/XShm.h>
#endif /* HAVE_XSHM */
#include <string.h>
#include <math.h>
/* Object header */
#include "xvimageallocator.h"
/* Debugging category */
#include <gst/gstinfo.h>
/* Helper functions */
#include <gst/video/video.h>
GST_DEBUG_CATEGORY_STATIC (gst_debug_xvimageallocator);
#define GST_CAT_DEFAULT gst_debug_xvimageallocator
struct _GstXvImageMemory
{
GstMemory parent;
gint im_format;
GstVideoRectangle crop;
XvImage *xvimage;
#ifdef HAVE_XSHM
XShmSegmentInfo SHMInfo;
#endif /* HAVE_XSHM */
};
struct _GstXvImageAllocator
{
GstAllocator parent;
GstXvContext *context;
};
struct _GstXvImageAllocatorClass
{
GstAllocatorClass parent_class;
};
gboolean
gst_xvimage_memory_is_from_context (GstMemory * mem, GstXvContext * context)
{
GstXvImageAllocator *alloc;
if (!GST_IS_XVIMAGE_ALLOCATOR (mem->allocator))
return FALSE;
alloc = GST_XVIMAGE_ALLOCATOR_CAST (mem->allocator);
if (alloc->context != context)
return FALSE;
return TRUE;
}
gint
gst_xvimage_memory_get_format (GstXvImageMemory * xvmem)
{
g_return_val_if_fail (xvmem != NULL, FALSE);
return xvmem->im_format;
}
XvImage *
gst_xvimage_memory_get_xvimage (GstXvImageMemory * xvmem)
{
g_return_val_if_fail (xvmem != NULL, FALSE);
return xvmem->xvimage;
}
gboolean
gst_xvimage_memory_get_crop (GstXvImageMemory * xvmem, GstVideoRectangle * crop)
{
g_return_val_if_fail (xvmem != NULL, FALSE);
if (crop)
*crop = xvmem->crop;
return TRUE;
}
/* X11 stuff */
static gboolean error_caught = FALSE;
static int
gst_xvimage_handle_xerror (Display * display, XErrorEvent * xevent)
{
char error_msg[1024];
XGetErrorText (display, xevent->error_code, error_msg, 1024);
GST_DEBUG ("xvimage triggered an XError. error: %s", error_msg);
error_caught = TRUE;
return 0;
}
static GstMemory *
gst_xvimage_allocator_dummy_alloc (GstAllocator * allocator, gsize size,
GstAllocationParams * params)
{
return NULL;
}
static void
gst_xvimage_allocator_free (GstAllocator * allocator, GstMemory * gmem)
{
GstXvImageMemory *mem = (GstXvImageMemory *) gmem;
GstXvImageAllocator *alloc = (GstXvImageAllocator *) allocator;
GstXvContext *context;
if (gmem->parent)
goto sub_mem;
context = alloc->context;
GST_DEBUG_OBJECT (allocator, "free memory %p", mem);
g_mutex_lock (&context->lock);
#ifdef HAVE_XSHM
if (context->use_xshm) {
if (mem->SHMInfo.shmaddr != ((void *) -1)) {
GST_DEBUG_OBJECT (allocator, "XServer ShmDetaching from 0x%x id 0x%lx",
mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
XShmDetach (context->disp, &mem->SHMInfo);
XSync (context->disp, FALSE);
shmdt (mem->SHMInfo.shmaddr);
mem->SHMInfo.shmaddr = (void *) -1;
}
if (mem->xvimage)
XFree (mem->xvimage);
} else
#endif /* HAVE_XSHM */
{
if (mem->xvimage) {
g_free (mem->xvimage->data);
XFree (mem->xvimage);
}
}
XSync (context->disp, FALSE);
g_mutex_unlock (&context->lock);
sub_mem:
g_slice_free (GstXvImageMemory, mem);
}
static gpointer
gst_xvimage_memory_map (GstXvImageMemory * mem, gsize maxsize,
GstMapFlags flags)
{
return mem->xvimage->data + mem->parent.offset;
}
static gboolean
gst_xvimage_memory_unmap (GstXvImageMemory * mem)
{
return TRUE;
}
static GstXvImageMemory *
gst_xvimage_memory_share (GstXvImageMemory * mem, gssize offset, gsize size)
{
GstXvImageMemory *sub;
GstMemory *parent;
/* We can only share the complete memory */
if (offset != 0)
return NULL;
if (size != -1 && size != mem->xvimage->data_size)
return NULL;
GST_DEBUG ("share memory %p", mem);
/* 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 (GstXvImageMemory);
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->im_format = mem->im_format;
sub->crop = mem->crop;
sub->xvimage = mem->xvimage;
#ifdef HAVE_XSHM
sub->SHMInfo = mem->SHMInfo;
#endif
return sub;
}
static GstXvImageMemory *
gst_xvimage_memory_copy (GstMemory * gmem, gssize offset, gsize size)
{
GstXvImageMemory *mem, *copy;
mem = (GstXvImageMemory *) gmem;
/* We can only copy the complete memory */
if (offset != 0)
return NULL;
if (size != -1 && size != mem->xvimage->data_size)
return NULL;
GST_DEBUG ("copy memory %p", mem);
copy = (GstXvImageMemory *)
gst_xvimage_allocator_alloc (GST_XVIMAGE_ALLOCATOR_CAST (gmem->allocator),
mem->im_format, mem->xvimage->width, mem->xvimage->height, &mem->crop,
NULL);
memcpy (copy->xvimage->data + copy->parent.offset,
mem->xvimage->data + mem->parent.offset, mem->xvimage->data_size);
return copy;
}
#define gst_xvimage_allocator_parent_class parent_class
G_DEFINE_TYPE (GstXvImageAllocator, gst_xvimage_allocator, GST_TYPE_ALLOCATOR);
static void gst_xvimage_allocator_finalize (GObject * object);
#define GST_XVIMAGE_ALLOCATOR_NAME "xvimage"
static void
gst_xvimage_allocator_class_init (GstXvImageAllocatorClass * klass)
{
GObjectClass *gobject_class;
GstAllocatorClass *allocator_class;
gobject_class = (GObjectClass *) klass;
allocator_class = (GstAllocatorClass *) klass;
gobject_class->finalize = gst_xvimage_allocator_finalize;
allocator_class->alloc = gst_xvimage_allocator_dummy_alloc;
allocator_class->free = gst_xvimage_allocator_free;
GST_DEBUG_CATEGORY_INIT (gst_debug_xvimageallocator, "xvimageallocator", 0,
"xvimageallocator object");
}
static void
gst_xvimage_allocator_init (GstXvImageAllocator * allocator)
{
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
alloc->mem_type = GST_XVIMAGE_ALLOCATOR_NAME;
alloc->mem_map = (GstMemoryMapFunction) gst_xvimage_memory_map;
alloc->mem_unmap = (GstMemoryUnmapFunction) gst_xvimage_memory_unmap;
alloc->mem_share = (GstMemoryShareFunction) gst_xvimage_memory_share;
alloc->mem_copy = (GstMemoryShareFunction) gst_xvimage_memory_copy;
/* fallback is_span */
GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
}
static void
gst_xvimage_allocator_finalize (GObject * object)
{
GstXvImageAllocator *alloc = GST_XVIMAGE_ALLOCATOR (object);
GST_DEBUG_OBJECT (object, "finalize");
gst_xvcontext_unref (alloc->context);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
GstXvImageAllocator *
gst_xvimage_allocator_new (GstXvContext * context)
{
GstXvImageAllocator *alloc;
g_return_val_if_fail (GST_IS_XVCONTEXT (context), NULL);
alloc = g_object_new (GST_TYPE_XVIMAGE_ALLOCATOR, NULL);
alloc->context = gst_xvcontext_ref (context);
return alloc;
}
GstXvContext *
gst_xvimage_allocator_peek_context (GstXvImageAllocator * allocator)
{
g_return_val_if_fail (GST_IS_XVIMAGE_ALLOCATOR (allocator), NULL);
return allocator->context;
}
GstMemory *
gst_xvimage_allocator_alloc (GstXvImageAllocator * allocator, gint im_format,
gint padded_width, gint padded_height, GstVideoRectangle * crop,
GError ** error)
{
int (*handler) (Display *, XErrorEvent *);
gboolean success = FALSE;
GstXvContext *context;
gint align = 15, offset;
GstXvImageMemory *mem;
context = allocator->context;
mem = g_slice_new (GstXvImageMemory);
mem->im_format = im_format;
#ifdef HAVE_XSHM
mem->SHMInfo.shmaddr = ((void *) -1);
mem->SHMInfo.shmid = -1;
#endif
mem->crop = *crop;
GST_DEBUG_OBJECT (allocator, "creating image %p (%dx%d) cropped %dx%d-%dx%d",
mem, padded_width, padded_height, crop->x, crop->y, crop->w, crop->h);
g_mutex_lock (&context->lock);
/* Setting an error handler to catch failure */
error_caught = FALSE;
handler = XSetErrorHandler (gst_xvimage_handle_xerror);
#ifdef HAVE_XSHM
if (context->use_xshm) {
int expected_size;
mem->xvimage = XvShmCreateImage (context->disp,
context->xv_port_id, im_format, NULL, padded_width, padded_height,
&mem->SHMInfo);
if (!mem->xvimage || error_caught) {
g_mutex_unlock (&context->lock);
/* Reset error flag */
error_caught = FALSE;
/* Push a warning */
GST_WARNING_OBJECT (allocator,
"could not XShmCreateImage a %dx%d image", padded_width,
padded_height);
/* Retry without XShm */
context->use_xshm = FALSE;
/* Hold X mutex again to try without XShm */
g_mutex_lock (&context->lock);
goto no_xshm;
}
/* we have to use the returned data_size for our shm size */
GST_LOG_OBJECT (allocator, "XShm image size is %d",
mem->xvimage->data_size);
/* calculate the expected size. This is only for sanity checking the
* number we get from X. */
switch (im_format) {
case GST_MAKE_FOURCC ('I', '4', '2', '0'):
case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
{
gint pitches[3];
gint offsets[3];
guint plane;
offsets[0] = 0;
pitches[0] = GST_ROUND_UP_4 (padded_width);
offsets[1] = offsets[0] + pitches[0] * GST_ROUND_UP_2 (padded_height);
pitches[1] = GST_ROUND_UP_8 (padded_width) / 2;
offsets[2] =
offsets[1] + pitches[1] * GST_ROUND_UP_2 (padded_height) / 2;
pitches[2] = GST_ROUND_UP_8 (pitches[0]) / 2;
expected_size =
offsets[2] + pitches[2] * GST_ROUND_UP_2 (padded_height) / 2;
for (plane = 0; plane < mem->xvimage->num_planes; plane++) {
GST_DEBUG_OBJECT (allocator,
"Plane %u has a expected pitch of %d bytes, " "offset of %d",
plane, pitches[plane], offsets[plane]);
}
break;
}
case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
expected_size = padded_height * GST_ROUND_UP_4 (padded_width * 2);
break;
default:
expected_size = 0;
break;
}
if (expected_size != 0 && mem->xvimage->data_size != expected_size) {
GST_WARNING_OBJECT (allocator,
"unexpected XShm image size (got %" G_GSIZE_FORMAT ", expected %d)",
mem->xvimage->data_size, expected_size);
}
/* Be verbose about our XvImage stride */
{
guint plane;
for (plane = 0; plane < mem->xvimage->num_planes; plane++) {
GST_DEBUG_OBJECT (allocator, "Plane %u has a pitch of %d bytes, "
"offset of %d", plane, mem->xvimage->pitches[plane],
mem->xvimage->offsets[plane]);
}
}
/* get shared memory */
mem->SHMInfo.shmid =
shmget (IPC_PRIVATE, mem->xvimage->data_size + align, IPC_CREAT | 0777);
if (mem->SHMInfo.shmid == -1)
goto shmget_failed;
/* attach */
mem->SHMInfo.shmaddr = shmat (mem->SHMInfo.shmid, NULL, 0);
if (mem->SHMInfo.shmaddr == ((void *) -1))
goto shmat_failed;
/* now we can set up the image data */
mem->xvimage->data = mem->SHMInfo.shmaddr;
mem->SHMInfo.readOnly = FALSE;
if (XShmAttach (context->disp, &mem->SHMInfo) == 0)
goto xattach_failed;
XSync (context->disp, FALSE);
/* Delete the shared memory segment as soon as we everyone is attached.
* This way, it will be deleted as soon as we detach later, and not
* leaked if we crash. */
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
GST_DEBUG_OBJECT (allocator, "XServer ShmAttached to 0x%x, id 0x%lx",
mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
} else
no_xshm:
#endif /* HAVE_XSHM */
{
mem->xvimage = XvCreateImage (context->disp,
context->xv_port_id, im_format, NULL, padded_width, padded_height);
if (!mem->xvimage || error_caught)
goto create_failed;
/* we have to use the returned data_size for our image size */
mem->xvimage->data = g_malloc (mem->xvimage->data_size + align);
XSync (context->disp, FALSE);
}
if ((offset = ((guintptr) mem->xvimage->data & align)))
offset = (align + 1) - offset;
GST_DEBUG_OBJECT (allocator, "memory %p, align %d, offset %d",
mem->xvimage->data, align, offset);
/* Reset error handler */
error_caught = FALSE;
XSetErrorHandler (handler);
gst_memory_init (GST_MEMORY_CAST (mem), 0,
GST_ALLOCATOR_CAST (allocator), NULL, mem->xvimage->data_size + align,
align, offset, mem->xvimage->data_size);
g_mutex_unlock (&context->lock);
success = TRUE;
beach:
if (!success) {
g_slice_free (GstXvImageMemory, mem);
mem = NULL;
}
return GST_MEMORY_CAST (mem);
/* ERRORS */
create_failed:
{
g_mutex_unlock (&context->lock);
/* Reset error handler */
error_caught = FALSE;
XSetErrorHandler (handler);
/* Push an error */
g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
"could not XvShmCreateImage a %dx%d image", padded_width,
padded_height);
goto beach;
}
#ifdef HAVE_XSHM
shmget_failed:
{
g_mutex_unlock (&context->lock);
g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
"could not get shared memory of %d bytes", mem->xvimage->data_size);
goto beach;
}
shmat_failed:
{
g_mutex_unlock (&context->lock);
g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
"Failed to shmat: %s", g_strerror (errno));
/* Clean up the shared memory segment */
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
goto beach;
}
xattach_failed:
{
/* Clean up the shared memory segment */
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
g_mutex_unlock (&context->lock);
g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
"Failed to XShmAttach");
goto beach;
}
#endif
}
/* We are called with the x_lock taken */
static void
gst_xwindow_draw_borders (GstXWindow * window, GstVideoRectangle * rect)
{
gint t1, t2;
GstXvContext *context;
g_return_if_fail (window != NULL);
g_return_if_fail (rect != NULL);
context = window->context;
XSetForeground (context->disp, window->gc, context->black);
/* Left border */
if (rect->x > window->render_rect.x) {
XFillRectangle (context->disp, window->win, window->gc,
window->render_rect.x, window->render_rect.y,
rect->x - window->render_rect.x, window->render_rect.h);
}
/* Right border */
t1 = rect->x + rect->w;
t2 = window->render_rect.x + window->render_rect.w;
if (t1 < t2) {
XFillRectangle (context->disp, window->win, window->gc,
t1, window->render_rect.y, t2 - t1, window->render_rect.h);
}
/* Top border */
if (rect->y > window->render_rect.y) {
XFillRectangle (context->disp, window->win, window->gc,
window->render_rect.x, window->render_rect.y,
window->render_rect.w, rect->y - window->render_rect.y);
}
/* Bottom border */
t1 = rect->y + rect->h;
t2 = window->render_rect.y + window->render_rect.h;
if (t1 < t2) {
XFillRectangle (context->disp, window->win, window->gc,
window->render_rect.x, t1, window->render_rect.w, t2 - t1);
}
}
void
gst_xvimage_memory_render (GstXvImageMemory * mem, GstVideoRectangle * src_crop,
GstXWindow * window, GstVideoRectangle * dst_crop, gboolean draw_border)
{
GstXvContext *context;
XvImage *xvimage;
context = window->context;
g_mutex_lock (&context->lock);
xvimage = gst_xvimage_memory_get_xvimage (mem);
if (draw_border) {
gst_xwindow_draw_borders (window, dst_crop);
}
#ifdef HAVE_XSHM
if (context->use_xshm) {
GST_LOG ("XvShmPutImage with image %dx%d and window %dx%d, from xvimage %"
GST_PTR_FORMAT, src_crop->w, src_crop->h,
window->render_rect.w, window->render_rect.h, mem);
XvShmPutImage (context->disp,
context->xv_port_id,
window->win,
window->gc, xvimage,
src_crop->x, src_crop->y, src_crop->w, src_crop->h,
dst_crop->x, dst_crop->y, dst_crop->w, dst_crop->h, FALSE);
} else
#endif /* HAVE_XSHM */
{
XvPutImage (context->disp,
context->xv_port_id,
window->win,
window->gc, xvimage,
src_crop->x, src_crop->y, src_crop->w, src_crop->h,
dst_crop->x, dst_crop->y, dst_crop->w, dst_crop->h);
}
XSync (context->disp, FALSE);
g_mutex_unlock (&context->lock);
}

View file

@ -0,0 +1,69 @@
/* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_XVIMAGEALLOCATOR_H__
#define __GST_XVIMAGEALLOCATOR_H__
typedef struct _GstXvImageMemory GstXvImageMemory;
typedef struct _GstXvImageAllocator GstXvImageAllocator;
typedef struct _GstXvImageAllocatorClass GstXvImageAllocatorClass;
#include "xvcontext.h"
G_BEGIN_DECLS
/* allocator functions */
#define GST_TYPE_XVIMAGE_ALLOCATOR (gst_xvimage_allocator_get_type())
#define GST_IS_XVIMAGE_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_XVIMAGE_ALLOCATOR))
#define GST_XVIMAGE_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_XVIMAGE_ALLOCATOR, GstXvImageAllocator))
#define GST_XVIMAGE_ALLOCATOR_CAST(obj) ((GstXvImageAllocator*)(obj))
GType gst_xvimage_allocator_get_type (void);
/* the allocator */
GstXvImageAllocator * gst_xvimage_allocator_new (GstXvContext * context);
GstXvContext * gst_xvimage_allocator_peek_context (GstXvImageAllocator * allocator);
GstMemory * gst_xvimage_allocator_alloc (GstXvImageAllocator * allocator,
gint im_format,
gint padded_width,
gint padded_height,
GstVideoRectangle *crop,
GError ** error);
/* memory from the allocator */
gboolean gst_xvimage_memory_is_from_context (GstMemory *mem,
GstXvContext * context);
gint gst_xvimage_memory_get_format (GstXvImageMemory *mem);
XvImage * gst_xvimage_memory_get_xvimage (GstXvImageMemory *mem);
gboolean gst_xvimage_memory_get_crop (GstXvImageMemory *mem,
GstVideoRectangle *crop);
void gst_xvimage_memory_render (GstXvImageMemory *mem,
GstVideoRectangle *src_crop,
GstXWindow *window,
GstVideoRectangle *dst_crop,
gboolean draw_border);
G_END_DECLS
#endif /*__GST_XVIMAGEALLOCATOR_H__*/

View file

@ -22,7 +22,8 @@
#endif #endif
/* Object header */ /* Object header */
#include "xvimagesink.h" #include "xvimagepool.h"
#include "xvimageallocator.h"
/* Debugging category */ /* Debugging category */
#include <gst/gstinfo.h> #include <gst/gstinfo.h>
@ -39,8 +40,11 @@ GST_DEBUG_CATEGORY_EXTERN (gst_debug_xvimagepool);
struct _GstXvImageBufferPoolPrivate struct _GstXvImageBufferPoolPrivate
{ {
GstXvImageAllocator *allocator;
GstCaps *caps; GstCaps *caps;
gint im_format; gint im_format;
GstVideoRectangle crop;
GstVideoInfo info; GstVideoInfo info;
GstVideoAlignment align; GstVideoAlignment align;
guint padded_width; guint padded_width;
@ -49,524 +53,6 @@ struct _GstXvImageBufferPoolPrivate
gboolean need_alignment; gboolean need_alignment;
}; };
/* X11 stuff */
static gboolean error_caught = FALSE;
static int
gst_xvimagesink_handle_xerror (Display * display, XErrorEvent * xevent)
{
char error_msg[1024];
XGetErrorText (display, xevent->error_code, error_msg, 1024);
GST_DEBUG ("xvimagesink triggered an XError. error: %s", error_msg);
error_caught = TRUE;
return 0;
}
static GstMemory *
gst_xvimage_memory_alloc (GstAllocator * allocator, gsize size,
GstAllocationParams * params)
{
return NULL;
}
static void
gst_xvimage_memory_free (GstAllocator * allocator, GstMemory * gmem)
{
GstXvImageMemory *mem = (GstXvImageMemory *) gmem;
GstXvImageSink *xvimagesink;
if (gmem->parent)
goto sub_mem;
xvimagesink = mem->sink;
GST_DEBUG_OBJECT (xvimagesink, "free memory %p", mem);
/* Hold the object lock to ensure the XContext doesn't disappear */
GST_OBJECT_LOCK (xvimagesink);
/* We might have some buffers destroyed after changing state to NULL */
if (xvimagesink->xcontext == NULL) {
GST_DEBUG_OBJECT (xvimagesink, "Destroying XvImage 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
if (mem->xvimage)
XFree (mem->xvimage);
goto beach;
}
g_mutex_lock (&xvimagesink->x_lock);
#ifdef HAVE_XSHM
if (xvimagesink->xcontext->use_xshm) {
if (mem->SHMInfo.shmaddr != ((void *) -1)) {
GST_DEBUG_OBJECT (xvimagesink, "XServer ShmDetaching from 0x%x id 0x%lx",
mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
XShmDetach (xvimagesink->xcontext->disp, &mem->SHMInfo);
XSync (xvimagesink->xcontext->disp, FALSE);
shmdt (mem->SHMInfo.shmaddr);
mem->SHMInfo.shmaddr = (void *) -1;
}
if (mem->xvimage)
XFree (mem->xvimage);
} else
#endif /* HAVE_XSHM */
{
if (mem->xvimage) {
g_free (mem->xvimage->data);
XFree (mem->xvimage);
}
}
XSync (xvimagesink->xcontext->disp, FALSE);
g_mutex_unlock (&xvimagesink->x_lock);
beach:
GST_OBJECT_UNLOCK (xvimagesink);
gst_object_unref (mem->sink);
sub_mem:
g_slice_free (GstXvImageMemory, mem);
}
static gpointer
xvimage_memory_map (GstXvImageMemory * mem, gsize maxsize, GstMapFlags flags)
{
return mem->xvimage->data + mem->parent.offset;
}
static gboolean
xvimage_memory_unmap (GstXvImageMemory * mem)
{
return TRUE;
}
static GstXvImageMemory *
xvimage_memory_share (GstXvImageMemory * mem, gssize offset, gsize size)
{
GstXvImageMemory *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 (GstXvImageMemory);
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->xvimage = mem->xvimage;
sub->SHMInfo = mem->SHMInfo;
sub->x = mem->x;
sub->y = mem->y;
sub->width = mem->width;
sub->height = mem->height;
return sub;
}
typedef GstAllocator GstXvImageMemoryAllocator;
typedef GstAllocatorClass GstXvImageMemoryAllocatorClass;
GType xvimage_memory_allocator_get_type (void);
G_DEFINE_TYPE (GstXvImageMemoryAllocator, xvimage_memory_allocator,
GST_TYPE_ALLOCATOR);
#define GST_XVIMAGE_ALLOCATOR_NAME "xvimage"
#define GST_TYPE_XVIMAGE_MEMORY_ALLOCATOR (xvimage_memory_allocator_get_type())
#define GST_IS_XVIMAGE_MEMORY_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_XVIMAGE_MEMORY_ALLOCATOR))
static void
xvimage_memory_allocator_class_init (GstXvImageMemoryAllocatorClass * klass)
{
GstAllocatorClass *allocator_class;
allocator_class = (GstAllocatorClass *) klass;
allocator_class->alloc = gst_xvimage_memory_alloc;
allocator_class->free = gst_xvimage_memory_free;
}
static void
xvimage_memory_allocator_init (GstXvImageMemoryAllocator * allocator)
{
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
alloc->mem_type = GST_XVIMAGE_ALLOCATOR_NAME;
alloc->mem_map = (GstMemoryMapFunction) xvimage_memory_map;
alloc->mem_unmap = (GstMemoryUnmapFunction) xvimage_memory_unmap;
alloc->mem_share = (GstMemoryShareFunction) xvimage_memory_share;
/* fallback copy and is_span */
GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
}
static GstMemory *
xvimage_memory_alloc (GstXvImageBufferPool * xvpool)
{
GstXvImageSink *xvimagesink;
int (*handler) (Display *, XErrorEvent *);
gboolean success = FALSE;
GstXContext *xcontext;
gint width, height, im_format, align = 15, offset;
GstXvImageBufferPoolPrivate *priv;
GstXvImageMemory *mem;
priv = xvpool->priv;
xvimagesink = xvpool->sink;
xcontext = xvimagesink->xcontext;
width = priv->padded_width;
height = priv->padded_height;
im_format = priv->im_format;
mem = g_slice_new (GstXvImageMemory);
#ifdef HAVE_XSHM
mem->SHMInfo.shmaddr = ((void *) -1);
mem->SHMInfo.shmid = -1;
#endif
mem->x = priv->align.padding_left;
mem->y = priv->align.padding_top;
mem->width = priv->info.width;
mem->height = priv->info.height;
mem->sink = gst_object_ref (xvimagesink);
mem->im_format = im_format;
GST_DEBUG_OBJECT (xvimagesink, "creating image %p (%dx%d)", mem,
width, height);
g_mutex_lock (&xvimagesink->x_lock);
/* Setting an error handler to catch failure */
error_caught = FALSE;
handler = XSetErrorHandler (gst_xvimagesink_handle_xerror);
#ifdef HAVE_XSHM
if (xcontext->use_xshm) {
int expected_size;
mem->xvimage = XvShmCreateImage (xcontext->disp,
xcontext->xv_port_id, im_format, NULL, width, height, &mem->SHMInfo);
if (!mem->xvimage || error_caught) {
g_mutex_unlock (&xvimagesink->x_lock);
/* Reset error flag */
error_caught = FALSE;
/* Push a warning */
GST_ELEMENT_WARNING (xvimagesink, RESOURCE, WRITE,
("Failed to create output image buffer of %dx%d pixels",
width, height),
("could not XShmCreateImage a %dx%d image", width, height));
/* Retry without XShm */
xvimagesink->xcontext->use_xshm = FALSE;
/* Hold X mutex again to try without XShm */
g_mutex_lock (&xvimagesink->x_lock);
goto no_xshm;
}
/* we have to use the returned data_size for our shm size */
mem->size = mem->xvimage->data_size;
GST_LOG_OBJECT (xvimagesink, "XShm image size is %" G_GSIZE_FORMAT,
mem->size);
/* calculate the expected size. This is only for sanity checking the
* number we get from X. */
switch (im_format) {
case GST_MAKE_FOURCC ('I', '4', '2', '0'):
case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
{
gint pitches[3];
gint offsets[3];
guint plane;
offsets[0] = 0;
pitches[0] = GST_ROUND_UP_4 (width);
offsets[1] = offsets[0] + pitches[0] * GST_ROUND_UP_2 (height);
pitches[1] = GST_ROUND_UP_8 (width) / 2;
offsets[2] = offsets[1] + pitches[1] * GST_ROUND_UP_2 (height) / 2;
pitches[2] = GST_ROUND_UP_8 (pitches[0]) / 2;
expected_size = offsets[2] + pitches[2] * GST_ROUND_UP_2 (height) / 2;
for (plane = 0; plane < mem->xvimage->num_planes; plane++) {
GST_DEBUG_OBJECT (xvimagesink,
"Plane %u has a expected pitch of %d bytes, " "offset of %d",
plane, pitches[plane], offsets[plane]);
}
break;
}
case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
expected_size = height * GST_ROUND_UP_4 (width * 2);
break;
default:
expected_size = 0;
break;
}
if (expected_size != 0 && mem->size != expected_size) {
GST_WARNING_OBJECT (xvimagesink,
"unexpected XShm image size (got %" G_GSIZE_FORMAT ", expected %d)",
mem->size, expected_size);
}
/* Be verbose about our XvImage stride */
{
guint plane;
for (plane = 0; plane < mem->xvimage->num_planes; plane++) {
GST_DEBUG_OBJECT (xvimagesink, "Plane %u has a pitch of %d bytes, "
"offset of %d", plane, mem->xvimage->pitches[plane],
mem->xvimage->offsets[plane]);
}
}
/* get shared memory */
mem->SHMInfo.shmid =
shmget (IPC_PRIVATE, mem->size + align, IPC_CREAT | 0777);
if (mem->SHMInfo.shmid == -1)
goto shmget_failed;
/* attach */
mem->SHMInfo.shmaddr = shmat (mem->SHMInfo.shmid, NULL, 0);
if (mem->SHMInfo.shmaddr == ((void *) -1))
goto shmat_failed;
/* now we can set up the image data */
mem->xvimage->data = mem->SHMInfo.shmaddr;
mem->SHMInfo.readOnly = FALSE;
if (XShmAttach (xcontext->disp, &mem->SHMInfo) == 0)
goto xattach_failed;
XSync (xcontext->disp, FALSE);
/* Delete the shared memory segment as soon as we everyone is attached.
* This way, it will be deleted as soon as we detach later, and not
* leaked if we crash. */
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
GST_DEBUG_OBJECT (xvimagesink, "XServer ShmAttached to 0x%x, id 0x%lx",
mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
} else
no_xshm:
#endif /* HAVE_XSHM */
{
mem->xvimage = XvCreateImage (xcontext->disp,
xcontext->xv_port_id, im_format, NULL, width, height);
if (!mem->xvimage || error_caught)
goto create_failed;
/* we have to use the returned data_size for our image size */
mem->size = mem->xvimage->data_size;
mem->xvimage->data = g_malloc (mem->size + align);
XSync (xcontext->disp, FALSE);
}
if ((offset = ((guintptr) mem->xvimage->data & align)))
offset = (align + 1) - offset;
GST_DEBUG_OBJECT (xvimagesink, "memory %p, align %d, offset %d",
mem->xvimage->data, align, offset);
/* Reset error handler */
error_caught = FALSE;
XSetErrorHandler (handler);
gst_memory_init (GST_MEMORY_CAST (mem), GST_MEMORY_FLAG_NO_SHARE,
xvpool->allocator, NULL, mem->size + align, align, offset, mem->size);
g_mutex_unlock (&xvimagesink->x_lock);
success = TRUE;
beach:
if (!success) {
g_slice_free (GstXvImageMemory, mem);
mem = NULL;
}
return GST_MEMORY_CAST (mem);
/* ERRORS */
create_failed:
{
g_mutex_unlock (&xvimagesink->x_lock);
/* Reset error handler */
error_caught = FALSE;
XSetErrorHandler (handler);
/* Push an error */
GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
("Failed to create output image buffer of %dx%d pixels",
width, height),
("could not XvShmCreateImage a %dx%d image", width, height));
goto beach;
}
#ifdef HAVE_XSHM
shmget_failed:
{
g_mutex_unlock (&xvimagesink->x_lock);
GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
("Failed to create output image buffer of %dx%d pixels",
width, height),
("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
mem->size));
goto beach;
}
shmat_failed:
{
g_mutex_unlock (&xvimagesink->x_lock);
GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
("Failed to create output image buffer of %dx%d pixels",
width, height), ("Failed to shmat: %s", g_strerror (errno)));
/* Clean up the shared memory segment */
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
goto beach;
}
xattach_failed:
{
/* Clean up the shared memory segment */
shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
g_mutex_unlock (&xvimagesink->x_lock);
GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
("Failed to create output image buffer of %dx%d pixels",
width, height), ("Failed to XShmAttach"));
goto beach;
}
#endif
}
#ifdef HAVE_XSHM
/* This function checks that it is actually really possible to create an image
using XShm */
gboolean
gst_xvimagesink_check_xshm_calls (GstXvImageSink * xvimagesink,
GstXContext * xcontext)
{
XvImage *xvimage;
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_xvimagesink_handle_xerror);
/* Trying to create a 1x1 picture */
GST_DEBUG ("XvShmCreateImage of 1x1");
xvimage = XvShmCreateImage (xcontext->disp, xcontext->xv_port_id,
xcontext->im_format, NULL, 1, 1, &SHMInfo);
/* Might cause an error, sync to ensure it is noticed */
XSync (xcontext->disp, FALSE);
if (!xvimage || error_caught) {
GST_WARNING ("could not XvShmCreateImage a 1x1 image");
goto beach;
}
size = xvimage->data_size;
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));
/* Clean up the shared memory segment */
shmctl (SHMInfo.shmid, IPC_RMID, NULL);
goto beach;
}
xvimage->data = SHMInfo.shmaddr;
SHMInfo.readOnly = FALSE;
if (XShmAttach (xcontext->disp, &SHMInfo) == 0) {
GST_WARNING ("Failed to XShmAttach");
/* Clean up the shared memory segment */
shmctl (SHMInfo.shmid, IPC_RMID, NULL);
goto beach;
}
/* Sync to ensure we see any errors we caused */
XSync (xcontext->disp, FALSE);
/* Delete the shared memory segment as soon as everyone is attached.
* 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) {
GST_DEBUG ("XServer ShmAttached to 0x%x, id 0x%lx", SHMInfo.shmid,
SHMInfo.shmseg);
did_attach = TRUE;
/* store whether we succeeded in result */
result = TRUE;
} else {
GST_WARNING ("MIT-SHM extension check failed at XShmAttach. "
"Not using shared memory.");
}
beach:
/* Sync to ensure we swallow any errors we caused and reset error_caught */
XSync (xcontext->disp, FALSE);
error_caught = FALSE;
XSetErrorHandler (handler);
if (did_attach) {
GST_DEBUG ("XServer ShmDetaching from 0x%x id 0x%lx",
SHMInfo.shmid, SHMInfo.shmseg);
XShmDetach (xcontext->disp, &SHMInfo);
XSync (xcontext->disp, FALSE);
}
if (SHMInfo.shmaddr != ((void *) -1))
shmdt (SHMInfo.shmaddr);
if (xvimage)
XFree (xvimage);
return result;
}
#endif /* HAVE_XSHM */
/* bufferpool */ /* bufferpool */
static void gst_xvimage_buffer_pool_finalize (GObject * object); static void gst_xvimage_buffer_pool_finalize (GObject * object);
@ -594,6 +80,7 @@ xvimage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
GstXvImageBufferPoolPrivate *priv = xvpool->priv; GstXvImageBufferPoolPrivate *priv = xvpool->priv;
GstVideoInfo info; GstVideoInfo info;
GstCaps *caps; GstCaps *caps;
GstXvContext *context;
if (!gst_buffer_pool_config_get_params (config, &caps, NULL, NULL, NULL)) if (!gst_buffer_pool_config_get_params (config, &caps, NULL, NULL, NULL))
goto wrong_config; goto wrong_config;
@ -608,7 +95,9 @@ xvimage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height, GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
caps); caps);
priv->im_format = gst_xvimagesink_get_format_from_info (xvpool->sink, &info); context = gst_xvimage_allocator_peek_context (priv->allocator);
priv->im_format = gst_xvcontext_get_format_from_info (context, &info);
if (priv->im_format == -1) if (priv->im_format == -1)
goto unknown_format; goto unknown_format;
@ -650,6 +139,10 @@ xvimage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
priv->align.padding_bottom; priv->align.padding_bottom;
priv->info = info; priv->info = info;
priv->crop.x = priv->align.padding_left;
priv->crop.y = priv->align.padding_top;
priv->crop.w = priv->info.width;
priv->crop.h = priv->info.height;
return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config); return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
@ -672,12 +165,8 @@ wrong_caps:
} }
unknown_format: unknown_format:
{ {
GST_WARNING_OBJECT (xvpool->sink, "failed to get format from caps %" GST_WARNING_OBJECT (pool, "failed to get format from caps %"
GST_PTR_FORMAT, caps); GST_PTR_FORMAT, caps);
GST_ELEMENT_ERROR (xvpool->sink, RESOURCE, WRITE,
("Failed to create output image buffer of %dx%d pixels",
priv->info.width, priv->info.height),
("Invalid input caps %" GST_PTR_FORMAT, caps));
return FALSE;; return FALSE;;
} }
} }
@ -696,7 +185,10 @@ xvimage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
info = &priv->info; info = &priv->info;
xvimage = gst_buffer_new (); xvimage = gst_buffer_new ();
mem = xvimage_memory_alloc (xvpool);
mem = gst_xvimage_allocator_alloc (priv->allocator, priv->im_format,
priv->padded_width, priv->padded_height, &priv->crop, NULL);
if (mem == NULL) { if (mem == NULL) {
gst_buffer_unref (xvimage); gst_buffer_unref (xvimage);
goto no_buffer; goto no_buffer;
@ -724,15 +216,12 @@ no_buffer:
} }
GstBufferPool * GstBufferPool *
gst_xvimage_buffer_pool_new (GstXvImageSink * xvimagesink) gst_xvimage_buffer_pool_new (GstXvImageAllocator * allocator)
{ {
GstXvImageBufferPool *pool; GstXvImageBufferPool *pool;
g_return_val_if_fail (GST_IS_XVIMAGESINK (xvimagesink), NULL);
pool = g_object_new (GST_TYPE_XVIMAGE_BUFFER_POOL, NULL); pool = g_object_new (GST_TYPE_XVIMAGE_BUFFER_POOL, NULL);
pool->sink = gst_object_ref (xvimagesink); pool->priv->allocator = gst_object_ref (allocator);
pool->allocator = g_object_new (GST_TYPE_XVIMAGE_MEMORY_ALLOCATOR, NULL);
GST_LOG_OBJECT (pool, "new XvImage buffer pool %p", pool); GST_LOG_OBJECT (pool, "new XvImage buffer pool %p", pool);
@ -770,32 +259,8 @@ gst_xvimage_buffer_pool_finalize (GObject * object)
if (priv->caps) if (priv->caps)
gst_caps_unref (priv->caps); gst_caps_unref (priv->caps);
gst_object_unref (pool->sink); if (priv->allocator)
g_object_unref (pool->allocator); gst_object_unref (priv->allocator);
G_OBJECT_CLASS (gst_xvimage_buffer_pool_parent_class)->finalize (object); G_OBJECT_CLASS (gst_xvimage_buffer_pool_parent_class)->finalize (object);
} }
/* This function tries to get a format matching with a given caps in the
supported list of formats we generated in gst_xvimagesink_get_xv_support */
gint
gst_xvimagesink_get_format_from_info (GstXvImageSink * xvimagesink,
GstVideoInfo * info)
{
GList *list = NULL;
g_return_val_if_fail (GST_IS_XVIMAGESINK (xvimagesink), 0);
list = xvimagesink->xcontext->formats_list;
while (list) {
GstXvImageFormat *format = list->data;
if (format && format->vformat == GST_VIDEO_INFO_FORMAT (info))
return format->format;
list = g_list_next (list);
}
return -1;
}

View file

@ -20,63 +20,16 @@
#ifndef __GST_XVIMAGEPOOL_H__ #ifndef __GST_XVIMAGEPOOL_H__
#define __GST_XVIMAGEPOOL_H__ #define __GST_XVIMAGEPOOL_H__
#ifdef HAVE_XSHM #include <gst/gst.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#endif /* HAVE_XSHM */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef HAVE_XSHM
#include <X11/extensions/XShm.h>
#endif /* HAVE_XSHM */
#include <string.h>
#include <math.h>
#include "xvimageallocator.h"
G_BEGIN_DECLS G_BEGIN_DECLS
typedef struct _GstXvImageMemory GstXvImageMemory;
typedef struct _GstXvImageBufferPool GstXvImageBufferPool; typedef struct _GstXvImageBufferPool GstXvImageBufferPool;
typedef struct _GstXvImageBufferPoolClass GstXvImageBufferPoolClass; typedef struct _GstXvImageBufferPoolClass GstXvImageBufferPoolClass;
typedef struct _GstXvImageBufferPoolPrivate GstXvImageBufferPoolPrivate; typedef struct _GstXvImageBufferPoolPrivate GstXvImageBufferPoolPrivate;
#include "xvimagesink.h"
/**
* GstXvImageMemory:
* @sink: a reference to the our #GstXvImageSink
* @xvimage: the XvImage of this buffer
* @width: the width in pixels of XvImage @xvimage
* @height: the height in pixels of XvImage @xvimage
* @im_format: the format of XvImage @xvimage
* @size: the size in bytes of XvImage @xvimage
*
* Subclass of #GstMemory containing additional information about an XvImage.
*/
struct _GstXvImageMemory
{
GstMemory parent;
/* Reference to the xvimagesink we belong to */
GstXvImageSink *sink;
XvImage *xvimage;
#ifdef HAVE_XSHM
XShmSegmentInfo SHMInfo;
#endif /* HAVE_XSHM */
gint x, y;
gint width, height;
gint im_format;
size_t size;
};
/* buffer pool functions */ /* buffer pool functions */
#define GST_TYPE_XVIMAGE_BUFFER_POOL (gst_xvimage_buffer_pool_get_type()) #define GST_TYPE_XVIMAGE_BUFFER_POOL (gst_xvimage_buffer_pool_get_type())
#define GST_IS_XVIMAGE_BUFFER_POOL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_XVIMAGE_BUFFER_POOL)) #define GST_IS_XVIMAGE_BUFFER_POOL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_XVIMAGE_BUFFER_POOL))
@ -87,9 +40,6 @@ struct _GstXvImageBufferPool
{ {
GstBufferPool bufferpool; GstBufferPool bufferpool;
GstXvImageSink *sink;
GstAllocator *allocator;
GstXvImageBufferPoolPrivate *priv; GstXvImageBufferPoolPrivate *priv;
}; };
@ -100,13 +50,7 @@ struct _GstXvImageBufferPoolClass
GType gst_xvimage_buffer_pool_get_type (void); GType gst_xvimage_buffer_pool_get_type (void);
GstBufferPool *gst_xvimage_buffer_pool_new (GstXvImageSink * xvimagesink); GstBufferPool * gst_xvimage_buffer_pool_new (GstXvImageAllocator *allocator);
gboolean gst_xvimagesink_check_xshm_calls (GstXvImageSink * xvimagesink,
GstXContext * xcontext);
gint gst_xvimagesink_get_format_from_info (GstXvImageSink * xvimagesink,
GstVideoInfo * info);
G_END_DECLS G_END_DECLS

File diff suppressed because it is too large Load diff

View file

@ -22,26 +22,6 @@
#include <gst/video/gstvideosink.h> #include <gst/video/gstvideosink.h>
#ifdef HAVE_XSHM
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#endif /* HAVE_XSHM */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef HAVE_XSHM
#include <X11/extensions/XShm.h>
#endif /* HAVE_XSHM */
#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvlib.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
/* Helper functions */ /* Helper functions */
#include <gst/video/video.h> #include <gst/video/video.h>
@ -56,122 +36,18 @@ G_BEGIN_DECLS
(G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_XVIMAGESINK)) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_XVIMAGESINK))
#define GST_IS_XVIMAGESINK_CLASS(klass) \ #define GST_IS_XVIMAGESINK_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_XVIMAGESINK)) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_XVIMAGESINK))
typedef struct _GstXContext GstXContext;
typedef struct _GstXWindow GstXWindow;
typedef struct _GstXvImageFormat GstXvImageFormat;
typedef struct _GstXvImageSink GstXvImageSink; typedef struct _GstXvImageSink GstXvImageSink;
typedef struct _GstXvImageSinkClass GstXvImageSinkClass; typedef struct _GstXvImageSinkClass GstXvImageSinkClass;
#include "xvimageallocator.h"
#include "xvimagepool.h" #include "xvimagepool.h"
#include "xvcontext.h"
/*
* GstXContext:
* @disp: the X11 Display of this context
* @screen: the default Screen of Display @disp
* @screen_num: the Screen number of @screen
* @visual: the default Visual of Screen @screen
* @root: the root Window of Display @disp
* @white: the value of a white pixel on Screen @screen
* @black: the value of a black pixel on Screen @screen
* @depth: the color depth of Display @disp
* @bpp: the number of bits per pixel on Display @disp
* @endianness: the endianness of image bytes on Display @disp
* @width: the width in pixels of Display @disp
* @height: the height in pixels of Display @disp
* @widthmm: the width in millimeters of Display @disp
* @heightmm: the height in millimeters of Display @disp
* @par: the pixel aspect ratio calculated from @width, @widthmm and @height,
* @heightmm ratio
* @use_xshm: used to known wether of not XShm extension is usable or not even
* if the Extension is present
* @xv_port_id: the XVideo port ID
* @im_format: used to store at least a valid format for XShm calls checks
* @formats_list: list of supported image formats on @xv_port_id
* @channels_list: list of #GstColorBalanceChannels
* @caps: the #GstCaps that Display @disp can accept
*
* Structure used to store various informations collected/calculated for a
* Display.
*/
struct _GstXContext
{
Display *disp;
Screen *screen;
gint screen_num;
Visual *visual;
Window root;
gulong white, black;
gint depth;
gint bpp;
gint endianness;
gint width, height;
gint widthmm, heightmm;
GValue *par; /* calculated pixel aspect ratio */
gboolean use_xshm;
XvPortID xv_port_id;
guint nb_adaptors;
gchar **adaptors;
gint im_format;
GList *formats_list;
GList *channels_list;
GstCaps *caps;
/* Optimisation storage for buffer_alloc return */
GstCaps *last_caps;
gint last_format;
gint last_width;
gint last_height;
};
/*
* GstXWindow:
* @win: the Window ID of this X11 window
* @width: the width in pixels of Window @win
* @height: the height in pixels of Window @win
* @internal: used to remember if Window @win was created internally or passed
* through the #GstVideoOverlay interface
* @gc: the Graphical Context of Window @win
*
* Structure used to store informations about a Window.
*/
struct _GstXWindow
{
Window win;
gint width, height;
gboolean internal;
GC gc;
};
/**
* GstXvImageFormat:
* @format: the image format
* @caps: generated #GstCaps for this image format
*
* Structure storing image format to #GstCaps association.
*/
struct _GstXvImageFormat
{
gint format;
GstVideoFormat vformat;
GstCaps *caps;
};
/** /**
* GstXvImageSink: * GstXvImageSink:
* @display_name: the name of the Display we want to render to * @display_name: the name of the Display we want to render to
* @xcontext: our instance's #GstXContext * @xvcontext: our instance's #GstXvContext
* @xwindow: the #GstXWindow we are rendering to * @xwindow: the #GstXWindow we are rendering to
* @cur_image: a reference to the last #GstXvImage that was put to @xwindow. It * @cur_image: a reference to the last #GstXvImage that was put to @xwindow. It
* is used when Expose events are received to redraw the latest video frame * is used when Expose events are received to redraw the latest video frame
@ -183,7 +59,7 @@ struct _GstXvImageFormat
* mode * mode
* @flow_lock: used to protect data flow routines from external calls such as * @flow_lock: used to protect data flow routines from external calls such as
* events from @event_thread or methods from the #GstVideoOverlay interface * events from @event_thread or methods from the #GstVideoOverlay interface
* @par: used to override calculated pixel aspect ratio from @xcontext * @par: used to override calculated pixel aspect ratio from @xvcontext
* @pool_lock: used to protect the buffer pool * @pool_lock: used to protect the buffer pool
* @image_pool: a list of #GstXvImageBuffer that could be reused at next buffer * @image_pool: a list of #GstXvImageBuffer that could be reused at next buffer
* allocation call * allocation call
@ -207,10 +83,9 @@ struct _GstXvImageSink
/* Our element stuff */ /* Our element stuff */
GstVideoSink videosink; GstVideoSink videosink;
char *display_name; GstXvContextConfig config;
guint adaptor_no; GstXvContext *context;
GstXvImageAllocator *allocator;
GstXContext *xcontext;
GstXWindow *xwindow; GstXWindow *xwindow;
GstBuffer *cur_image; GstBuffer *cur_image;
@ -223,7 +98,6 @@ struct _GstXvImageSink
gint fps_n; gint fps_n;
gint fps_d; gint fps_d;
GMutex x_lock;
GMutex flow_lock; GMutex flow_lock;
/* object-set pixel aspect ratio */ /* object-set pixel aspect ratio */
@ -239,32 +113,13 @@ struct _GstXvImageSink
gboolean handle_events; gboolean handle_events;
gboolean handle_expose; gboolean handle_expose;
gint brightness;
gint contrast;
gint hue;
gint saturation;
gboolean cb_changed;
/* size of incoming video, used as the size for XvImage */ /* size of incoming video, used as the size for XvImage */
guint video_width, video_height; guint video_width, video_height;
/* port attributes */
gboolean autopaint_colorkey;
gint colorkey;
gboolean draw_borders; gboolean draw_borders;
/* port features */
gboolean have_autopaint_colorkey;
gboolean have_colorkey;
gboolean have_double_buffer;
/* stream metadata */ /* stream metadata */
gchar *media_title; gchar *media_title;
/* target video rectangle */
GstVideoRectangle render_rect;
gboolean have_render_rect;
}; };
struct _GstXvImageSinkClass struct _GstXvImageSinkClass