diff --git a/gst-libs/gst/allocators/gstdmabuf.c b/gst-libs/gst/allocators/gstdmabuf.c index 7dbceffe11..785a07634a 100644 --- a/gst-libs/gst/allocators/gstdmabuf.c +++ b/gst-libs/gst/allocators/gstdmabuf.c @@ -106,16 +106,9 @@ gst_dmabuf_allocator_new (void) GstMemory * gst_dmabuf_allocator_alloc (GstAllocator * allocator, gint fd, gsize size) { - GstFdAllocator *alloc = GST_FD_ALLOCATOR_CAST (allocator); - GstFdAllocatorClass *klass = GST_FD_ALLOCATOR_GET_CLASS (alloc); + g_return_val_if_fail (GST_IS_DMABUF_ALLOCATOR (allocator), NULL); - if (!GST_IS_DMABUF_ALLOCATOR (allocator)) { - GST_WARNING ("it isn't the correct allocator for dmabuf"); - return NULL; - } - - GST_DEBUG ("alloc from allocator %p", allocator); - return klass->alloc (alloc, fd, size, GST_FD_MEMORY_FLAG_NONE); + return gst_fd_allocator_alloc (allocator, fd, size, GST_FD_MEMORY_FLAG_NONE); } /** diff --git a/gst-libs/gst/allocators/gstfdmemory.c b/gst-libs/gst/allocators/gstfdmemory.c index 2a0e4f4a9a..95f544f859 100644 --- a/gst-libs/gst/allocators/gstfdmemory.c +++ b/gst-libs/gst/allocators/gstfdmemory.c @@ -177,13 +177,75 @@ gst_fd_mem_share (GstMemory * gmem, gssize offset, gssize size) #endif } -static GstMemory * -gst_fd_allocator_alloc (GstFdAllocator * allocator, gint fd, gsize size, +G_DEFINE_TYPE (GstFdAllocator, gst_fd_allocator, GST_TYPE_ALLOCATOR); + +static void +gst_fd_allocator_class_init (GstFdAllocatorClass * klass) +{ + GstAllocatorClass *allocator_class; + + allocator_class = (GstAllocatorClass *) klass; + + allocator_class->alloc = NULL; + allocator_class->free = gst_fd_mem_free; + +} + +static void +gst_fd_allocator_init (GstFdAllocator * allocator) +{ + GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator); + + alloc->mem_type = GST_ALLOCATOR_FD; + + alloc->mem_map = gst_fd_mem_map; + alloc->mem_unmap = gst_fd_mem_unmap; + alloc->mem_share = gst_fd_mem_share; + + GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC); +} + +/** + * gst_fd_allocator_new: + * + * Return a new fd allocator. + * + * Returns: (transfer full): a new fd allocator, or NULL if the allocator + * isn't available. Use gst_object_unref() to release the allocator after + * usage + * + * Since: 1.6 + */ +GstAllocator * +gst_fd_allocator_new (void) +{ + return g_object_new (GST_TYPE_FD_ALLOCATOR, NULL); +} + +/** + * gst_fd_allocator_alloc: + * @allocator: (allow-none): allocator to be used for this memory + * @fd: file descriptor + * @size: memory size + * @flags: extra #GstFdMemoryFlags + * + * Return a %GstMemory that wraps a generic file descriptor. + * + * Returns: (transfer full): a GstMemory based on @allocator. + * When the buffer will be released the allocator will close the @fd. + * The memory is only mmapped on gst_buffer_mmap() request. + * + * Since: 1.6 + */ +GstMemory * +gst_fd_allocator_alloc (GstAllocator * allocator, gint fd, gsize size, GstFdMemoryFlags flags) { #ifdef HAVE_MMAP GstFdMemory *mem; + g_return_val_if_fail (GST_IS_FD_ALLOCATOR (allocator), NULL); + mem = g_slice_new0 (GstFdMemory); gst_memory_init (GST_MEMORY_CAST (mem), 0, GST_ALLOCATOR_CAST (allocator), NULL, size, 0, 0, size); @@ -201,33 +263,6 @@ gst_fd_allocator_alloc (GstFdAllocator * allocator, gint fd, gsize size, #endif } -G_DEFINE_ABSTRACT_TYPE (GstFdAllocator, gst_fd_allocator, GST_TYPE_ALLOCATOR); - -static void -gst_fd_allocator_class_init (GstFdAllocatorClass * klass) -{ - GstAllocatorClass *allocator_class; - - allocator_class = (GstAllocatorClass *) klass; - - allocator_class->alloc = NULL; - allocator_class->free = gst_fd_mem_free; - - klass->alloc = gst_fd_allocator_alloc; -} - -static void -gst_fd_allocator_init (GstFdAllocator * allocator) -{ - GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator); - - alloc->mem_map = gst_fd_mem_map; - alloc->mem_unmap = gst_fd_mem_unmap; - alloc->mem_share = gst_fd_mem_share; - - GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC); -} - /** * gst_is_fd_memory: * @mem: #GstMemory diff --git a/gst-libs/gst/allocators/gstfdmemory.h b/gst-libs/gst/allocators/gstfdmemory.h index fdcc677e1b..178698b2f4 100644 --- a/gst-libs/gst/allocators/gstfdmemory.h +++ b/gst-libs/gst/allocators/gstfdmemory.h @@ -28,6 +28,8 @@ G_BEGIN_DECLS typedef struct _GstFdAllocator GstFdAllocator; typedef struct _GstFdAllocatorClass GstFdAllocatorClass; +#define GST_ALLOCATOR_FD "fd" + #define GST_TYPE_FD_ALLOCATOR (gst_fd_allocator_get_type()) #define GST_IS_FD_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_FD_ALLOCATOR)) #define GST_IS_FD_ALLOCATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_FD_ALLOCATOR)) @@ -68,16 +70,16 @@ struct _GstFdAllocator struct _GstFdAllocatorClass { GstAllocatorClass parent_class; - - /*< protected >*/ - GstMemory * (*alloc) (GstFdAllocator *alloc, gint fd, - gsize size, GstFdMemoryFlags flags); }; GType gst_fd_allocator_get_type (void); -gboolean gst_is_fd_memory (GstMemory *mem); -gint gst_fd_memory_get_fd (GstMemory *mem); +GstAllocator * gst_fd_allocator_new (void); +GstMemory * gst_fd_allocator_alloc (GstAllocator * allocator, gint fd, + gsize size, GstFdMemoryFlags flags); + +gboolean gst_is_fd_memory (GstMemory *mem); +gint gst_fd_memory_get_fd (GstMemory *mem); G_END_DECLS