mem: remove memory stuff

Remove the memory stuff from the spa API, we can do thing more simple
and efficiently if we always allocate buffers outside of the plugins and
only implement an alloc function on the node when it can do something
clever.
Move serialize code to the props/format/buffer code
Make it possible to copy a format and properties.
This commit is contained in:
Wim Taymans 2016-09-29 18:18:59 +02:00
parent fe37e2bc1b
commit 24108e01c1
46 changed files with 901 additions and 1546 deletions

View file

@ -28,7 +28,6 @@
#include <spa/poll.h>
#include <spa/monitor.h>
#include <spa/memory.h>
#include <spa/debug.h>
extern const SpaHandleFactory spa_alsa_source_factory;

View file

@ -142,7 +142,7 @@ spa_alsa_sink_node_set_props (SpaNode *node,
return SPA_RESULT_OK;
}
res = spa_props_copy (props, &p->props);
res = spa_props_copy_values (props, &p->props);
return res;
}
@ -420,10 +420,6 @@ spa_alsa_sink_node_port_alloc_buffers (SpaNode *node,
uint32_t *n_buffers)
{
SpaALSASink *this;
SpaALSABuffer *b;
unsigned int i, n_bufs;
size_t buffer_size;
uint8_t *bufmem;
if (node == NULL || node->handle == NULL || buffers == NULL)
return SPA_RESULT_INVALID_ARGUMENTS;
@ -436,63 +432,7 @@ spa_alsa_sink_node_port_alloc_buffers (SpaNode *node,
if (!this->have_format)
return SPA_RESULT_NO_FORMAT;
n_bufs = *n_buffers;
buffer_size = this->buffer_frames * this->frame_size;
if (!this->alloc_bufmem)
this->alloc_bufmem = spa_memory_alloc_with_fd (SPA_MEMORY_POOL_SHARED, NULL, buffer_size * n_bufs);
bufmem = spa_memory_ensure_ptr (this->alloc_bufmem);
if (!this->alloc_mem)
this->alloc_mem = spa_memory_alloc_with_fd (SPA_MEMORY_POOL_SHARED, NULL, sizeof (SpaALSABuffer) * n_bufs);
this->alloc_buffers = spa_memory_ensure_ptr (this->alloc_mem);
this->n_buffers = n_bufs;
for (i = 0; i < n_bufs; i++) {
b = &this->alloc_buffers[i];
b->buffer.id = i;
b->buffer.mem.mem = this->alloc_mem->mem;
b->buffer.mem.offset = sizeof (SpaALSABuffer) * i;
b->buffer.mem.size = sizeof (SpaALSABuffer);
b->buffer.n_metas = 2;
b->buffer.metas = offsetof (SpaALSABuffer, metas);
b->buffer.n_datas = 1;
b->buffer.datas = offsetof (SpaALSABuffer, datas);
b->header.flags = 0;
b->header.seq = 0;
b->header.pts = 0;
b->header.dts_offset = 0;
b->metas[0].type = SPA_META_TYPE_HEADER;
b->metas[0].offset = offsetof (SpaALSABuffer, header);
b->metas[0].size = sizeof (b->header);
b->ringbuffer.readindex = 0;
b->ringbuffer.writeindex = 0;
b->ringbuffer.size = 0;
b->ringbuffer.size_mask = 0;
b->metas[1].type = SPA_META_TYPE_RINGBUFFER;
b->metas[1].offset = offsetof (SpaALSABuffer, ringbuffer);
b->metas[1].size = sizeof (b->ringbuffer);
b->datas[0].mem.mem = this->alloc_bufmem->mem;
b->datas[0].mem.offset = buffer_size * i;
b->datas[0].mem.size = buffer_size;
b->datas[0].stride = 0;
b->ptr = bufmem + buffer_size * i;
buffers[i] = &b->buffer;
}
*n_buffers = n_bufs;
this->have_buffers = true;
update_state (this, SPA_NODE_STATE_PAUSED);
return SPA_RESULT_OK;
return SPA_RESULT_NOT_IMPLEMENTED;
}
static SpaResult
@ -556,7 +496,7 @@ spa_alsa_sink_node_port_push_input (SpaNode *node,
have_enough = true;
continue;
}
SPA_QUEUE_PUSH_TAIL (&this->ready, SpaALSABuffer, next, &this->alloc_buffers[info[i].buffer_id]);
SPA_QUEUE_PUSH_TAIL (&this->ready, SpaALSABuffer, next, &this->buffers[info[i].buffer_id]);
}
info[i].status = SPA_RESULT_OK;
}

View file

@ -144,7 +144,7 @@ spa_alsa_source_node_set_props (SpaNode *node,
return SPA_RESULT_OK;
}
res = spa_props_copy (props, &p->props);
res = spa_props_copy_values (props, &p->props);
return res;
}
@ -301,38 +301,23 @@ recycle_buffer (SpaALSASource *this, uint32_t buffer_id)
{
SpaALSABuffer *b;
b = &this->alloc_buffers[buffer_id];
b = &this->buffers[buffer_id];
if (!b->outstanding)
return;
b->outstanding = false;
b->next = NULL;
SPA_QUEUE_PUSH_TAIL (&this->free, SpaALSABuffer, next, b);
}
static SpaResult
spa_alsa_clear_buffers (SpaALSASource *this)
{
int i;
if (!this->have_buffers)
return SPA_RESULT_OK;
for (i = 0; i < this->n_buffers; i++) {
SpaALSABuffer *b;
b = &this->alloc_buffers[i];
if (b->outstanding) {
fprintf (stderr, "queueing outstanding buffer %p\n", b);
recycle_buffer (this, i);
}
}
if (this->alloc_bufmem)
spa_memory_unref (&this->alloc_bufmem->mem);
if (this->alloc_mem)
spa_memory_unref (&this->alloc_mem->mem);
this->alloc_mem = NULL;
this->alloc_bufmem = NULL;
SPA_QUEUE_INIT (&this->free);
SPA_QUEUE_INIT (&this->ready);
this->n_buffers = 0;
this->have_buffers = false;
@ -459,6 +444,7 @@ spa_alsa_source_node_port_use_buffers (SpaNode *node,
{
SpaALSASource *this;
SpaResult res;
int i;
if (node == NULL || node->handle == NULL)
return SPA_RESULT_INVALID_ARGUMENTS;
@ -475,8 +461,17 @@ spa_alsa_source_node_port_use_buffers (SpaNode *node,
if ((res = spa_alsa_clear_buffers (this)) < 0)
return res;
}
if (buffers != NULL)
return SPA_RESULT_NOT_IMPLEMENTED;
if (n_buffers > 0) {
for (i = 0; i < n_buffers; i++) {
SpaALSABuffer *b = &this->buffers[i];
b->outbuf = buffers[i];
b->outstanding = false;
b->next = NULL;
SPA_QUEUE_PUSH_TAIL (&this->free, SpaALSABuffer, next, b);
}
this->n_buffers = n_buffers;
this->have_buffers = true;
}
if (this->have_buffers)
update_state (this, SPA_NODE_STATE_PAUSED);
@ -496,10 +491,6 @@ spa_alsa_source_node_port_alloc_buffers (SpaNode *node,
uint32_t *n_buffers)
{
SpaALSASource *this;
SpaALSABuffer *b;
unsigned int i, n_bufs;
uint8_t *bufmem;
size_t buffer_size;
if (node == NULL || node->handle == NULL || buffers == NULL)
return SPA_RESULT_INVALID_ARGUMENTS;
@ -512,73 +503,7 @@ spa_alsa_source_node_port_alloc_buffers (SpaNode *node,
if (!this->have_format)
return SPA_RESULT_NO_FORMAT;
n_bufs = *n_buffers;
SPA_QUEUE_INIT (&this->free);
SPA_QUEUE_INIT (&this->ready);
buffer_size = this->buffer_frames * this->frame_size;
if (!this->alloc_bufmem)
this->alloc_bufmem = spa_memory_alloc_with_fd (SPA_MEMORY_POOL_SHARED,
NULL,
buffer_size * n_bufs);
bufmem = spa_memory_ensure_ptr (this->alloc_bufmem);
if (!this->alloc_mem)
this->alloc_mem = spa_memory_alloc_with_fd (SPA_MEMORY_POOL_SHARED, NULL, sizeof (SpaALSABuffer) * n_bufs);
this->alloc_buffers = spa_memory_ensure_ptr (this->alloc_mem);
this->n_buffers = n_bufs;
for (i = 0; i < n_bufs; i++) {
b = &this->alloc_buffers[i];
b->buffer.id = i;
b->buffer.mem.mem = this->alloc_mem->mem;
b->buffer.mem.offset = sizeof (SpaALSABuffer) * i;
b->buffer.mem.size = sizeof (SpaALSABuffer);
b->buffer.n_metas = 2;
b->buffer.metas = offsetof (SpaALSABuffer, metas);
b->buffer.n_datas = 1;
b->buffer.datas = offsetof (SpaALSABuffer, datas);
b->header.flags = 0;
b->header.seq = 0;
b->header.pts = 0;
b->header.dts_offset = 0;
b->metas[0].type = SPA_META_TYPE_HEADER;
b->metas[0].offset = offsetof (SpaALSABuffer, header);
b->metas[0].size = sizeof (b->header);
b->ringbuffer.readindex = 0;
b->ringbuffer.writeindex = 0;
b->ringbuffer.size = 0;
b->ringbuffer.size_mask = 0;
b->metas[1].type = SPA_META_TYPE_RINGBUFFER;
b->metas[1].offset = offsetof (SpaALSABuffer, ringbuffer);
b->metas[1].size = sizeof (b->ringbuffer);
b->datas[0].mem.mem = this->alloc_bufmem->mem;
b->datas[0].mem.offset = buffer_size * i;
b->datas[0].mem.size = buffer_size;
b->datas[0].stride = 0;
b->ptr = bufmem + buffer_size * i;
b->h = &b->header;
buffers[i] = b->outbuf = &b->buffer;
b->outstanding = true;
recycle_buffer (this, b->outbuf->id);
}
*n_buffers = n_bufs;
this->have_buffers = true;
update_state (this, SPA_NODE_STATE_PAUSED);
return SPA_RESULT_OK;
return SPA_RESULT_NOT_IMPLEMENTED;
}
static SpaResult

View file

@ -309,7 +309,7 @@ mmap_read (SpaALSAState *state)
SpaALSABuffer *b;
snd_htimestamp_t htstamp = { 0, 0 };
int64_t now;
uint8_t *dest;
uint8_t *dest = NULL;
snd_pcm_status_alloca(&status);
@ -374,8 +374,8 @@ mmap_read (SpaALSAState *state)
SpaNodeEventHaveOutput ho;
SpaData *d;
d = SPA_BUFFER_DATAS (b->outbuf);
d[0].mem.size = avail * state->frame_size;
d = b->outbuf->datas;
d[0].size = avail * state->frame_size;
b->next = NULL;
SPA_QUEUE_PUSH_TAIL (&state->ready, SpaALSABuffer, next, b);

View file

@ -45,12 +45,9 @@ typedef struct {
bool period_event;
} SpaALSAProps;
#define MAX_BUFFERS 16
struct _SpaALSABuffer {
SpaBuffer buffer;
SpaMeta metas[2];
SpaMetaHeader header;
SpaMetaRingbuffer ringbuffer;
SpaData datas[1];
SpaBuffer *outbuf;
SpaMetaHeader *h;
void *ptr;
@ -90,9 +87,7 @@ struct _SpaALSAState {
SpaPortStatus status;
bool have_buffers;
SpaMemory *alloc_bufmem;
SpaMemory *alloc_mem;
SpaALSABuffer *alloc_buffers;
SpaALSABuffer buffers[MAX_BUFFERS];
unsigned int n_buffers;
SpaQueue free;