Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions gst/tmpfile/gstfddepay.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "gstfddepay.h"
#include "wire-protocol.h"
#include "../gstnetcontrolmessagemeta.h"
#include "gsttmpfileallocator.h"

#include <gst/gst.h>
#include <gst/base/gstbasetransform.h>
Expand Down Expand Up @@ -201,6 +202,14 @@ gst_fddepay_set_clock (GstElement * element, GstClock * clock)
clock);
}

static int
steal_fd (int *p_fd)
{
int fd = *p_fd;
*p_fd = -1;
return fd;
}

static GstFlowReturn
gst_fddepay_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
{
Expand Down Expand Up @@ -263,11 +272,34 @@ gst_fddepay_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
fd, (ssize_t) statbuf.st_size, msg.offset, msg.size);
goto error;
}
fdmem = gst_fd_allocator_alloc (fddepay->fd_allocator, fd,
msg.offset + msg.size, GST_FD_MEMORY_FLAG_KEEP_MAPPED);
fd = -1;
gst_memory_resize (fdmem, msg.offset, msg.size);
GST_MINI_OBJECT_FLAG_SET (fdmem, GST_MEMORY_FLAG_READONLY);
if (msg.size >= tmpfile_mmap_threshold ()) {
fdmem = gst_fd_allocator_alloc (fddepay->fd_allocator, steal_fd(&fd),
msg.offset + msg.size, GST_FD_MEMORY_FLAG_KEEP_MAPPED);
gst_memory_resize (fdmem, msg.offset, msg.size);
GST_MINI_OBJECT_FLAG_SET (fdmem, GST_MEMORY_FLAG_READONLY);
} else {
g_autofree char *buf = g_malloc (msg.size);
size_t off = 0;
while (off < msg.size) {
ssize_t res = pread (fd, buf + off, msg.size - off, msg.offset + off);
if (res < 0) {
if (errno == EINTR)
continue;
GST_WARNING_OBJECT (fddepay, "fddepay: Reading from fd %i failed: %s",
fd, g_strerror(errno));
goto error;
} else if (res == 0) {
GST_WARNING_OBJECT (fddepay, "fddepay: Reading from fd %i failed: "
"Unexpected EOS", fd);
goto error;
} else {
off += res;
}
}
fdmem = gst_memory_new_wrapped (0, g_steal_pointer(&buf), msg.size,
msg.offset, msg.size, buf, g_free);
close (steal_fd(&fd));
}

gst_buffer_remove_all_memory (buf);
gst_buffer_remove_meta (buf,
Expand Down
26 changes: 26 additions & 0 deletions gst/tmpfile/gsttmpfileallocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <unistd.h>

#define PAGE_ALIGN 4095
#define DEFAULT_MMAP_THRESHOLD (128 * 1024)

GST_DEBUG_CATEGORY_STATIC (gst_tmpfileallocator_debug);
#define GST_CAT_DEFAULT gst_tmpfileallocator_debug
Expand All @@ -42,6 +43,7 @@ typedef struct
GstAllocator *fd_allocator;
uint32_t frame_count;
uint32_t pid;
gsize mmap_threshold;
} GstTmpFileAllocator;

typedef struct
Expand Down Expand Up @@ -81,6 +83,24 @@ gst_tmpfile_allocator_init (GstTmpFileAllocator * alloc)
alloc->fd_allocator = gst_fd_allocator_new ();
alloc->frame_count = 0;
alloc->pid = getpid();
alloc->mmap_threshold = tmpfile_mmap_threshold ();
}

gsize tmpfile_mmap_threshold (void)
{
static gsize mmap_threshold = 0;
if (g_once_init_enter (&mmap_threshold))
{
const char* st = getenv("TMPFILE_MMAP_THRESHOLD");
if (st == NULL)
st = "";
gsize mt = atol(st);
if (mt == 0)
mt = DEFAULT_MMAP_THRESHOLD;

g_once_init_leave (&mmap_threshold, mt);
}
return mmap_threshold;
}

static void
Expand Down Expand Up @@ -157,6 +177,12 @@ gst_tmpfile_allocator_alloc (GstAllocator * allocator, gsize size,
pad (size + pad (params->prefix, params->align) + params->padding,
PAGE_ALIGN);

if (maxsize < alloc->mmap_threshold) {
/* Below a certain size the cost of mmap outweighs the cost of copying the
* data */
return gst_allocator_alloc (NULL, size, params);
}

if (alloc->fd_allocator == NULL)
return NULL;

Expand Down
2 changes: 2 additions & 0 deletions gst/tmpfile/gsttmpfileallocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ GstAllocator* gst_tmpfile_allocator_new (void);
GstMemory * gst_tmpfile_allocator_copy_alloc (GstAllocator * alloc,
const void * data, size_t n);

gsize tmpfile_mmap_threshold (void);

G_END_DECLS
#endif