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);
}
/**
#endif
}
-static GstMemory *
-gst_fd_allocator_alloc (GstFdAllocator * allocator, gint fd, gsize size,
- GstFdMemoryFlags flags)
-{
-#ifdef HAVE_MMAP
- GstFdMemory *mem;
-
- mem = g_slice_new0 (GstFdMemory);
- gst_memory_init (GST_MEMORY_CAST (mem), 0, GST_ALLOCATOR_CAST (allocator),
- NULL, size, 0, 0, size);
-
- mem->flags = flags;
- mem->fd = fd;
- g_mutex_init (&mem->lock);
-
- GST_DEBUG ("%p: fd: %d size %" G_GSIZE_FORMAT, mem, mem->fd,
- mem->mem.maxsize);
-
- return (GstMemory *) mem;
-#else /* !HAVE_MMAP */
- return NULL;
-#endif
-}
-
-G_DEFINE_ABSTRACT_TYPE (GstFdAllocator, gst_fd_allocator, GST_TYPE_ALLOCATOR);
+G_DEFINE_TYPE (GstFdAllocator, gst_fd_allocator, GST_TYPE_ALLOCATOR);
static void
gst_fd_allocator_class_init (GstFdAllocatorClass * klass)
allocator_class->alloc = NULL;
allocator_class->free = gst_fd_mem_free;
- klass->alloc = gst_fd_allocator_alloc;
}
static void
{
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);
+
+ mem->flags = flags;
+ mem->fd = fd;
+ g_mutex_init (&mem->lock);
+
+ GST_DEBUG ("%p: fd: %d size %" G_GSIZE_FORMAT, mem, mem->fd,
+ mem->mem.maxsize);
+
+ return (GstMemory *) mem;
+#else /* !HAVE_MMAP */
+ return NULL;
+#endif
+}
+
/**
* gst_is_fd_memory:
* @mem: #GstMemory
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))
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