Enable header metadata

Enable the header metadata so that we have timestamps again.
Rework allocation in the link a little. Reuse previously allocated
buffers when possible. Keep track of where the buffers was allocated.
Make some more convenience functions to initialize buffer headers. Make
it so that we move allocations to the app.
Make some helpers to make memfd memory in pinos now that we will be able
to do all allocation there.
This commit is contained in:
Wim Taymans 2016-09-30 17:07:38 +02:00
parent 24108e01c1
commit 9d4048e73a
15 changed files with 504 additions and 157 deletions

View file

@ -164,21 +164,18 @@ size_t spa_buffer_get_size (const SpaBuffer *buffer);
size_t spa_buffer_serialize (void *dest, const SpaBuffer *buffer);
SpaBuffer * spa_buffer_deserialize (void *src, off_t offset);
/**
* SpaBufferAllocFlags:
* @SPA_BUFFER_ALLOC_FLAG_NONE: no flags
* @SPA_BUFFER_ALLOC_FLAG_NO_MEM: don't allocate memory
*/
typedef enum {
SPA_BUFFER_ALLOC_FLAG_NONE = 0,
SPA_BUFFER_ALLOC_FLAG_NO_MEM,
} SpaBufferAllocFlags;
SpaResult spa_buffer_alloc (SpaBufferAllocFlags flags,
SpaAllocParam **params,
unsigned int n_params,
SpaBuffer **buffers,
unsigned int *n_buffers);
SpaResult spa_alloc_params_get_header_size (SpaAllocParam **params,
unsigned int n_params,
unsigned int n_datas,
size_t *size);
SpaResult spa_buffer_init_headers (SpaAllocParam **params,
unsigned int n_params,
unsigned int n_datas,
SpaBuffer **buffers,
unsigned int n_buffers);
#ifdef __cplusplus
} /* extern "C" */

View file

@ -346,7 +346,9 @@ struct _SpaNode {
* the READY state or to CONFIGURE when @format is NULL.
*
* Returns: #SPA_RESULT_OK on success
* #SPA_RESULT_OK_RECHECK on success
* #SPA_RESULT_OK_RECHECK on success, the value of @format might have been
* changed depending on @flags and the final value can be found by
* doing SpaNode::get_format.
* #SPA_RESULT_INVALID_ARGUMENTS when node is %NULL
* #SPA_RESULT_INVALID_PORT when port_id is not valid
* #SPA_RESULT_INVALID_MEDIA_TYPE when the media type is not valid
@ -411,8 +413,8 @@ struct _SpaNode {
* on the buffers.
*
* Upon completion, this function might change the state of the
* node to PAUSED, when the node has enough buffers, or READY when
* @buffers are %NULL.
* node to PAUSED, when the node has enough buffers on all ports, or READY
* when @buffers are %NULL.
*
* Returns: #SPA_RESULT_OK on success
* #SPA_RESULT_ASYNC the function is executed asynchronously
@ -432,6 +434,10 @@ struct _SpaNode {
*
* Tell the port to allocate memory for @buffers.
*
* @params should contain an array of pointers to buffers. The data
* in the buffers should point to an array of at least 1 SPA_DATA_TYPE_INVALID
* data pointers that will be filled by this function.
*
* For input ports, the buffers will be dequeued and ready to be filled
* and pushed into the port. A notify should be configured so that you can
* know when a buffer can be reused.
@ -439,7 +445,15 @@ struct _SpaNode {
* For output ports, the buffers remain queued. port_reuse_buffer() should
* be called when a buffer can be reused.
*
* Upon completion, this function might change the state of the
* node to PAUSED, when the node has enough buffers on all ports.
*
* Once the port has allocated buffers, the memory of the buffers can be
* released again by calling SpaNode::port_use_buffers with %NULL.
*
* Returns: #SPA_RESULT_OK on success
* #SPA_RESULT_ERROR when the node already has allocated buffers.
* #SPA_RESULT_ASYNC the function is executed asynchronously
*/
SpaResult (*port_alloc_buffers) (SpaNode *node,
uint32_t port_id,

View file

@ -25,62 +25,35 @@
#include <spa/buffer.h>
#include <spa/port.h>
typedef struct {
SpaBuffer buffer;
SpaMeta metas[3];
SpaMetaHeader header;
SpaMetaRingbuffer ringbuffer;
SpaMetaVideoCrop crop;
SpaData datas[4];
} Buffer;
static const size_t header_sizes[] = {
0,
sizeof (SpaMetaHeader),
sizeof (SpaMetaPointer),
sizeof (SpaMetaVideoCrop),
sizeof (SpaMetaRingbuffer),
};
SpaResult
spa_buffer_alloc (SpaBufferAllocFlags flags,
SpaAllocParam **params,
unsigned int n_params,
SpaBuffer **buffers,
unsigned int *n_buffers)
spa_alloc_params_get_header_size (SpaAllocParam **params,
unsigned int n_params,
unsigned int n_datas,
size_t *size)
{
unsigned int i, nbufs;
size_t size = 0, stride = 0;
void *mem;
Buffer *bufs;
bool add_header = false;
int n_metas = 0;
unsigned int i, n_metas = 0;
nbufs = *n_buffers;
if (nbufs == 0)
return SPA_RESULT_ERROR;
*size = sizeof (SpaBuffer);
for (i = 0; i < n_params; i++) {
SpaAllocParam *p = params[i];
switch (p->type) {
case SPA_ALLOC_PARAM_TYPE_BUFFERS:
{
SpaAllocParamBuffers *b = (SpaAllocParamBuffers *) p;
size = SPA_MAX (size, b->minsize);
break;
}
case SPA_ALLOC_PARAM_TYPE_META_ENABLE:
{
SpaAllocParamMetaEnable *b = (SpaAllocParamMetaEnable *) p;
switch (b->type) {
case SPA_META_TYPE_HEADER:
if (!add_header)
n_metas++;
add_header = true;
break;
case SPA_META_TYPE_POINTER:
break;
case SPA_META_TYPE_VIDEO_CROP:
break;
case SPA_META_TYPE_RINGBUFFER:
break;
default:
break;
if (b->type > 0 && b->type < SPA_N_ELEMENTS (header_sizes)) {
n_metas++;
*size += header_sizes[b->type];
}
break;
}
@ -88,52 +61,71 @@ spa_buffer_alloc (SpaBufferAllocFlags flags,
break;
}
}
*size += n_metas * sizeof (SpaMeta);
*size += n_datas * sizeof (SpaData);
*n_buffers = nbufs;
return SPA_RESULT_OK;
}
if (flags & SPA_BUFFER_ALLOC_FLAG_NO_MEM)
size = 0;
SpaResult
spa_buffer_init_headers (SpaAllocParam **params,
unsigned int n_params,
unsigned int n_datas,
SpaBuffer **buffers,
unsigned int n_buffers)
{
unsigned int i;
int n_metas = 0;
mem = calloc (nbufs, sizeof (Buffer) + size);
for (i = 0; i < n_params; i++) {
SpaAllocParam *p = params[i];
bufs = mem;
for (i = 0; i < nbufs; i++) {
int mi = 0;
Buffer *b;
b = &bufs[i];
b->buffer.id = i;
buffers[i] = &b->buffer;
b->buffer.n_metas = n_metas;
b->buffer.metas = b->metas;
b->buffer.n_datas = 1;
b->buffer.datas = b->datas;
if (add_header) {
b->header.flags = 0;
b->header.seq = 0;
b->header.pts = 0;
b->header.dts_offset = 0;
b->metas[mi].type = SPA_META_TYPE_HEADER;
b->metas[mi].data = &b->header;
b->metas[mi].size = sizeof (b->header);
mi++;
switch (p->type) {
case SPA_ALLOC_PARAM_TYPE_META_ENABLE:
{
SpaAllocParamMetaEnable *b = (SpaAllocParamMetaEnable *) p;
if (b->type > 0 && b->type <= SPA_N_ELEMENTS (header_sizes))
n_metas++;
}
default:
break;
}
}
if (size == 0) {
b->datas[0].type = SPA_DATA_TYPE_INVALID;
b->datas[0].data = NULL;
} else {
b->datas[0].type = SPA_DATA_TYPE_MEMPTR;
b->datas[0].data = mem + sizeof (Buffer);
for (i = 0; i < n_buffers; i++) {
int mi = 0, j;
SpaBuffer *b;
void *p;
b = buffers[i];
b->id = i;
b->n_metas = n_metas;
b->metas = SPA_MEMBER (b, sizeof (SpaBuffer), SpaMeta);
b->n_datas = n_datas;
b->datas = SPA_MEMBER (b->metas, sizeof (SpaMeta) * n_metas, SpaData);
p = SPA_MEMBER (b->datas, sizeof (SpaData) * n_datas, void);
for (j = 0, mi = 0; j < n_params; j++) {
SpaAllocParam *prm = params[j];
switch (prm->type) {
case SPA_ALLOC_PARAM_TYPE_META_ENABLE:
{
SpaAllocParamMetaEnable *pme = (SpaAllocParamMetaEnable *) prm;
if (pme->type > 0 && pme->type <= SPA_N_ELEMENTS (header_sizes)) {
b->metas[mi].type = pme->type;
b->metas[mi].data = p;
b->metas[mi].size = header_sizes[pme->type];
p = SPA_MEMBER (p, header_sizes[pme->type], void);
mi++;
}
break;
}
default:
break;
}
}
b->datas[0].offset = size * i;
b->datas[0].size = size;
b->datas[0].stride = stride;
}
return SPA_RESULT_OK;
}
@ -150,7 +142,7 @@ spa_buffer_get_size (const SpaBuffer *buffer)
size = sizeof (SpaBuffer);
for (i = 0; i < buffer->n_metas; i++)
size += buffer->metas[i].size * sizeof (SpaMeta);
size += sizeof (SpaMeta) + buffer->metas[i].size;
for (i = 0; i < buffer->n_datas; i++)
size += sizeof (SpaData);
return size;

View file

@ -51,8 +51,8 @@ typedef struct _ProxyBuffer ProxyBuffer;
struct _ProxyBuffer {
SpaBuffer *outbuf;
SpaBuffer buffer;
SpaMeta metas[4];
SpaData datas[4];
off_t offset;
size_t size;
@ -172,6 +172,8 @@ clear_buffers (SpaProxy *this, SpaProxyPort *port)
if (port->n_buffers) {
fprintf (stderr, "proxy %p: clear buffers\n", this);
munmap (port->buffer_mem_ptr, port->buffer_mem_size);
close (port->buffer_mem_fd);
port->n_buffers = 0;
SPA_QUEUE_INIT (&port->ready);
@ -400,7 +402,7 @@ do_update_port (SpaProxy *this,
if (pu->change_mask & SPA_CONTROL_CMD_PORT_UPDATE_PROPS) {
}
if (pu->change_mask & SPA_CONTROL_CMD_PORT_UPDATE_INFO) {
if (pu->change_mask & SPA_CONTROL_CMD_PORT_UPDATE_INFO && pu->info) {
port->info = *pu->info;
}
@ -691,12 +693,17 @@ spa_proxy_node_port_use_buffers (SpaNode *node,
b->outbuf = buffers[i];
memcpy (&b->buffer, buffers[i], sizeof (SpaBuffer));
b->buffer.datas = b->datas;
b->buffer.metas = b->metas;
b->size = spa_buffer_get_size (buffers[i]);
b->offset = size;
size += b->size;
for (j = 0; j < buffers[i]->n_datas; j++) {
memcpy (&b->buffer.metas[j], &buffers[i]->metas[j], sizeof (SpaMeta));
}
for (j = 0; j < buffers[i]->n_datas; j++) {
SpaData *d = &buffers[i]->datas[j];
@ -723,18 +730,32 @@ spa_proxy_node_port_use_buffers (SpaNode *node,
if (ftruncate (port->buffer_mem_fd, size) < 0) {
fprintf (stderr, "Failed to truncate temporary file: %s\n", strerror (errno));
close (port->buffer_mem_fd);
return SPA_RESULT_ERROR;
}
{
unsigned int seals = F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL;
if (fcntl (port->buffer_mem_fd, F_ADD_SEALS, seals) == -1) {
fprintf (stderr, "Failed to add seals: %s\n", strerror (errno));
close (port->buffer_mem_fd);
}
}
p = port->buffer_mem_ptr = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, port->buffer_mem_fd, 0);
for (i = 0; i < n_buffers; i++)
p += spa_buffer_serialize (p, &port->buffers[i].buffer);
for (i = 0; i < n_buffers; i++) {
ProxyBuffer *b = &port->buffers[i];
size_t len;
SpaBuffer *sb;
SpaMeta *sbm;
len = spa_buffer_serialize (p, &b->buffer);
sb = p;
sbm = SPA_MEMBER (sb, SPA_PTR_TO_INT (sb->metas), SpaMeta);
for (j = 0; j < b->buffer.n_metas; j++)
b->metas[j].data = SPA_MEMBER (sb, SPA_PTR_TO_INT (sbm[j].data), void);
p += len;
}
am.port_id = port_id;
am.mem_id = port->buffer_mem_id;
@ -837,6 +858,19 @@ spa_proxy_node_port_reuse_buffer (SpaNode *node,
return res;
}
static void
copy_meta (SpaProxy *this, SpaProxyPort *port, uint32_t buffer_id)
{
ProxyBuffer *b = &port->buffers[buffer_id];
unsigned int i;
for (i = 0; i < b->outbuf->n_metas; i++) {
SpaMeta *sm = &b->outbuf->metas[i];
SpaMeta *dm = &b->buffer.metas[i];
memcpy (dm->data, sm->data, dm->size);
}
}
static SpaResult
spa_proxy_node_port_push_input (SpaNode *node,
unsigned int n_info,
@ -883,6 +917,8 @@ spa_proxy_node_port_push_input (SpaNode *node,
continue;
}
copy_meta (this, port, info[i].buffer_id);
pb.port_id = info[i].port_id;
pb.buffer_id = info[i].buffer_id;
spa_control_builder_add_cmd (&builder, SPA_CONTROL_CMD_PROCESS_BUFFER, &pb);

View file

@ -78,7 +78,6 @@ struct _V4l2Format {
typedef struct {
bool export_buf;
bool have_buffers;
bool started;
bool next_fmtdesc;
@ -98,16 +97,17 @@ typedef struct {
enum v4l2_buf_type type;
enum v4l2_memory memtype;
struct v4l2_requestbuffers reqbuf;
V4l2Buffer buffers[MAX_BUFFERS];
SpaQueue ready;
V4l2Buffer buffers[MAX_BUFFERS];
unsigned int n_buffers;
SpaQueue ready;
SpaPollFd fds[1];
SpaPollItem poll;
SpaPortInfo info;
SpaAllocParam *params[1];
SpaAllocParam *params[2];
SpaAllocParamBuffers param_buffers;
SpaAllocParamMetaEnable param_meta;
SpaPortStatus status;
int64_t last_ticks;
@ -228,7 +228,7 @@ spa_v4l2_source_node_send_command (SpaNode *node,
if (state->current_format == NULL)
return SPA_RESULT_NO_FORMAT;
if (!state->have_buffers)
if (state->n_buffers == 0)
return SPA_RESULT_NO_BUFFERS;
if ((res = spa_v4l2_start (this)) < 0)
@ -243,7 +243,7 @@ spa_v4l2_source_node_send_command (SpaNode *node,
if (state->current_format == NULL)
return SPA_RESULT_NO_FORMAT;
if (!state->have_buffers)
if (state->n_buffers == 0)
return SPA_RESULT_NO_BUFFERS;
if ((res = spa_v4l2_pause (this)) < 0)
@ -533,7 +533,7 @@ spa_v4l2_source_node_port_use_buffers (SpaNode *node,
if (state->current_format == NULL)
return SPA_RESULT_NO_FORMAT;
if (state->have_buffers) {
if (state->n_buffers) {
if ((res = spa_v4l2_clear_buffers (this)) < 0)
return res;
}
@ -542,12 +542,12 @@ spa_v4l2_source_node_port_use_buffers (SpaNode *node,
return res;
}
if (state->have_buffers)
if (state->n_buffers)
update_state (this, SPA_NODE_STATE_PAUSED);
else
update_state (this, SPA_NODE_STATE_READY);
return res;
return SPA_RESULT_OK;
}
static SpaResult
@ -577,7 +577,7 @@ spa_v4l2_source_node_port_alloc_buffers (SpaNode *node,
res = spa_v4l2_alloc_buffers (this, params, n_params, buffers, n_buffers);
if (state->have_buffers) {
if (state->n_buffers) {
if (state->started)
update_state (this, SPA_NODE_STATE_STREAMING);
else
@ -606,10 +606,10 @@ spa_v4l2_source_node_port_reuse_buffer (SpaNode *node,
state = &this->state[port_id];
if (!state->have_buffers)
if (state->n_buffers == 0)
return SPA_RESULT_NO_BUFFERS;
if (buffer_id >= state->reqbuf.count)
if (buffer_id >= state->n_buffers)
return SPA_RESULT_INVALID_BUFFER_ID;
res = spa_v4l2_buffer_recycle (this, buffer_id);

View file

@ -92,12 +92,13 @@ static SpaResult
spa_v4l2_clear_buffers (SpaV4l2Source *this)
{
SpaV4l2State *state = &this->state[0];
struct v4l2_requestbuffers reqbuf;
int i;
if (!state->have_buffers)
if (state->n_buffers == 0)
return SPA_RESULT_OK;
for (i = 0; i < state->reqbuf.count; i++) {
for (i = 0; i < state->n_buffers; i++) {
V4l2Buffer *b;
b = &state->buffers[i];
@ -105,9 +106,20 @@ spa_v4l2_clear_buffers (SpaV4l2Source *this)
fprintf (stderr, "queueing outstanding buffer %p\n", b);
spa_v4l2_buffer_recycle (this, i);
}
if (state->export_buf) {
close (SPA_PTR_TO_INT (b->outbuf->datas[0].data));
}
}
state->have_buffers = false;
CLEAR(reqbuf);
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbuf.memory = state->memtype;
reqbuf.count = 0;
if (xioctl (state->fd, VIDIOC_REQBUFS, &reqbuf) < 0) {
perror ("VIDIOC_REQBUFS");
}
state->n_buffers = 0;
return SPA_RESULT_OK;
}
@ -120,7 +132,7 @@ spa_v4l2_close (SpaV4l2Source *this)
if (!state->opened)
return 0;
if (state->have_buffers)
if (state->n_buffers > 0)
return 0;
fprintf (stderr, "close\n");
@ -786,7 +798,7 @@ spa_v4l2_set_format (SpaV4l2Source *this, V4l2Format *f, bool try_only)
state->info.latency = (streamparm.parm.capture.timeperframe.numerator * SPA_NSEC_PER_SEC) /
streamparm.parm.capture.timeperframe.denominator;
state->info.n_params = 1;
state->info.n_params = 2;
state->info.params = state->params;
state->params[0] = &state->param_buffers.param;
state->param_buffers.param.type = SPA_ALLOC_PARAM_TYPE_BUFFERS;
@ -796,6 +808,11 @@ spa_v4l2_set_format (SpaV4l2Source *this, V4l2Format *f, bool try_only)
state->param_buffers.min_buffers = 2;
state->param_buffers.max_buffers = MAX_BUFFERS;
state->param_buffers.align = 16;
state->params[1] = &state->param_meta.param;
state->param_meta.param.type = SPA_ALLOC_PARAM_TYPE_META_ENABLE;
state->param_meta.param.size = sizeof (state->param_meta);
state->param_meta.type = SPA_META_TYPE_HEADER;
state->info.features = NULL;
return 0;
@ -873,6 +890,18 @@ v4l2_on_fd_events (SpaPollNotifyData *data)
return 0;
}
static void *
find_meta_data (SpaBuffer *b, SpaMetaType type)
{
unsigned int i;
for (i = 0; i < b->n_metas; i++)
if (b->metas[i].type == type)
return b->metas[i].data;
return NULL;
}
static SpaResult
spa_v4l2_use_buffers (SpaV4l2Source *this, SpaBuffer **buffers, uint32_t n_buffers)
{
@ -896,7 +925,6 @@ spa_v4l2_use_buffers (SpaV4l2Source *this, SpaBuffer **buffers, uint32_t n_buffe
fprintf (stderr, "can't allocate enough buffers\n");
return SPA_RESULT_ERROR;
}
state->reqbuf = reqbuf;
for (i = 0; i < reqbuf.count; i++) {
V4l2Buffer *b;
@ -905,6 +933,7 @@ spa_v4l2_use_buffers (SpaV4l2Source *this, SpaBuffer **buffers, uint32_t n_buffe
b = &state->buffers[i];
b->outbuf = buffers[i];
b->outstanding = true;
b->h = find_meta_data (b->outbuf, SPA_META_TYPE_HEADER);
fprintf (stderr, "import buffer %p\n", buffers[i]);
@ -923,7 +952,7 @@ spa_v4l2_use_buffers (SpaV4l2Source *this, SpaBuffer **buffers, uint32_t n_buffe
spa_v4l2_buffer_recycle (this, buffers[i]->id);
}
state->have_buffers = true;
state->n_buffers = reqbuf.count;
return SPA_RESULT_OK;
}
@ -961,8 +990,6 @@ mmap_init (SpaV4l2Source *this,
if (state->export_buf)
fprintf (stderr, "using EXPBUF\n");
state->reqbuf = reqbuf;
for (i = 0; i < reqbuf.count; i++) {
V4l2Buffer *b;
SpaData *d;
@ -975,6 +1002,7 @@ mmap_init (SpaV4l2Source *this,
b = &state->buffers[i];
b->outbuf = buffers[i];
b->outstanding = true;
b->h = find_meta_data (b->outbuf, SPA_META_TYPE_HEADER);
CLEAR (b->v4l2_buffer);
b->v4l2_buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
@ -997,17 +1025,19 @@ mmap_init (SpaV4l2Source *this,
CLEAR (expbuf);
expbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
expbuf.index = i;
expbuf.flags = O_CLOEXEC | O_RDONLY;
if (xioctl (state->fd, VIDIOC_EXPBUF, &expbuf) < 0) {
perror("VIDIOC_EXPBUF");
continue;
}
fprintf (stderr, "expbuf %d\n", expbuf.fd);
d[0].type = SPA_DATA_TYPE_FD;
d[0].data = SPA_INT_TO_PTR (expbuf.fd);
} else {
d[0].type = SPA_DATA_TYPE_MEMPTR;
d[0].data = mmap (NULL,
b->v4l2_buffer.length,
PROT_READ | PROT_WRITE,
PROT_READ,
MAP_SHARED,
state->fd,
b->v4l2_buffer.m.offset);
@ -1018,7 +1048,7 @@ mmap_init (SpaV4l2Source *this,
}
spa_v4l2_buffer_recycle (this, i);
}
state->have_buffers = true;
state->n_buffers = reqbuf.count;
return SPA_RESULT_OK;
}
@ -1045,7 +1075,7 @@ spa_v4l2_alloc_buffers (SpaV4l2Source *this,
SpaResult res;
SpaV4l2State *state = &this->state[0];
if (state->have_buffers)
if (state->n_buffers > 0)
return SPA_RESULT_ERROR;
if (state->cap.capabilities & V4L2_CAP_STREAMING) {
@ -1123,7 +1153,7 @@ spa_v4l2_pause (SpaV4l2Source *this)
perror ("VIDIOC_STREAMOFF");
return SPA_RESULT_ERROR;
}
for (i = 0; i < state->reqbuf.count; i++) {
for (i = 0; i < state->n_buffers; i++) {
V4l2Buffer *b;
b = &state->buffers[i];