mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-10-29 05:40:23 -04:00
implement recording in native API
fix a memory leak in memblock.c git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@55 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
parent
cffc7768bd
commit
70bb8165ec
11 changed files with 349 additions and 107 deletions
|
|
@ -69,12 +69,13 @@ void pa_memblock_unref_fixed(struct pa_memblock *b) {
|
|||
if (b->ref == 1) {
|
||||
pa_memblock_unref(b);
|
||||
return;
|
||||
} else {
|
||||
d = malloc(b->length);
|
||||
assert(d);
|
||||
memcpy(d, b->data, b->length);
|
||||
b->data = d;
|
||||
b->type = PA_MEMBLOCK_DYNAMIC;
|
||||
b->ref--;
|
||||
}
|
||||
|
||||
d = malloc(b->length);
|
||||
assert(d);
|
||||
memcpy(d, b->data, b->length);
|
||||
b->data = d;
|
||||
b->type = PA_MEMBLOCK_DYNAMIC;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -39,26 +39,6 @@ struct userdata {
|
|||
|
||||
void module_done(struct pa_core *c, struct pa_module*m);
|
||||
|
||||
static void out_clear_memblocks(struct userdata*u, unsigned n) {
|
||||
unsigned i = u->out_current;
|
||||
assert(u && u->out_memblocks);
|
||||
|
||||
if (n > u->out_fragments)
|
||||
n = u->out_fragments;
|
||||
|
||||
while (n > 0) {
|
||||
if (u->out_memblocks[i]) {
|
||||
pa_memblock_unref_fixed(u->out_memblocks[i]);
|
||||
u->out_memblocks[i] = NULL;
|
||||
}
|
||||
|
||||
i++;
|
||||
while (i >= u->out_fragments)
|
||||
i -= u->out_fragments;
|
||||
|
||||
n--;
|
||||
}
|
||||
}
|
||||
|
||||
static void out_fill_memblocks(struct userdata *u, unsigned n) {
|
||||
assert(u && u->out_memblocks);
|
||||
|
|
@ -66,14 +46,15 @@ static void out_fill_memblocks(struct userdata *u, unsigned n) {
|
|||
while (n > 0) {
|
||||
struct pa_memchunk chunk;
|
||||
|
||||
if (!u->out_memblocks[u->out_current]) {
|
||||
|
||||
chunk.memblock = u->out_memblocks[u->out_current] = pa_memblock_new_fixed(u->out_mmap+u->out_fragment_size*u->out_current, u->out_fragment_size);
|
||||
chunk.length = chunk.memblock->length;
|
||||
chunk.index = 0;
|
||||
|
||||
pa_sink_render_into_full(u->sink, &chunk);
|
||||
}
|
||||
if (u->out_memblocks[u->out_current])
|
||||
pa_memblock_unref_fixed(u->out_memblocks[u->out_current]);
|
||||
|
||||
chunk.memblock = u->out_memblocks[u->out_current] = pa_memblock_new_fixed(u->out_mmap+u->out_fragment_size*u->out_current, u->out_fragment_size);
|
||||
assert(chunk.memblock);
|
||||
chunk.length = chunk.memblock->length;
|
||||
chunk.index = 0;
|
||||
|
||||
pa_sink_render_into_full(u->sink, &chunk);
|
||||
|
||||
u->out_current++;
|
||||
while (u->out_current >= u->out_fragments)
|
||||
|
|
@ -97,9 +78,7 @@ static void do_write(struct userdata *u) {
|
|||
if (!info.blocks)
|
||||
return;
|
||||
|
||||
out_clear_memblocks(u, info.blocks);
|
||||
out_fill_memblocks(u, info.blocks);
|
||||
|
||||
}
|
||||
|
||||
static void in_post_memblocks(struct userdata *u, unsigned n) {
|
||||
|
|
@ -109,7 +88,7 @@ static void in_post_memblocks(struct userdata *u, unsigned n) {
|
|||
struct pa_memchunk chunk;
|
||||
|
||||
if (!u->in_memblocks[u->in_current]) {
|
||||
u->in_memblocks[u->in_current] = chunk.memblock = pa_memblock_new_fixed(u->in_mmap+u->in_fragment_size*u->in_current, u->in_fragment_size);
|
||||
chunk.memblock = u->in_memblocks[u->in_current] = pa_memblock_new_fixed(u->in_mmap+u->in_fragment_size*u->in_current, u->in_fragment_size);
|
||||
chunk.length = chunk.memblock->length;
|
||||
chunk.index = 0;
|
||||
|
||||
|
|
@ -262,7 +241,7 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) {
|
|||
}
|
||||
} else {
|
||||
|
||||
u->source = pa_source_new(c, "dsp", 0, &u->sample_spec);
|
||||
u->source = pa_source_new(c, "oss_input", 0, &u->sample_spec);
|
||||
assert(u->source);
|
||||
u->source->userdata = u;
|
||||
|
||||
|
|
@ -293,7 +272,7 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) {
|
|||
} else {
|
||||
pa_silence_memory(u->out_mmap, u->out_mmap_length, &u->sample_spec);
|
||||
|
||||
u->sink = pa_sink_new(c, "dsp", 0, &u->sample_spec);
|
||||
u->sink = pa_sink_new(c, "oss_output", 0, &u->sample_spec);
|
||||
assert(u->sink);
|
||||
u->sink->get_latency = sink_get_latency_cb;
|
||||
u->sink->userdata = u;
|
||||
|
|
@ -337,12 +316,18 @@ void pa_module_done(struct pa_core *c, struct pa_module*m) {
|
|||
assert(u);
|
||||
|
||||
if (u->out_memblocks) {
|
||||
out_clear_memblocks(u, u->out_fragments);
|
||||
unsigned i;
|
||||
for (i = 0; i < u->out_fragments; i++)
|
||||
if (u->out_memblocks[i])
|
||||
pa_memblock_unref_fixed(u->out_memblocks[i]);
|
||||
free(u->out_memblocks);
|
||||
}
|
||||
|
||||
if (u->in_memblocks) {
|
||||
in_clear_memblocks(u, u->in_fragments);
|
||||
unsigned i;
|
||||
for (i = 0; i < u->in_fragments; i++)
|
||||
if (u->in_memblocks[i])
|
||||
pa_memblock_unref_fixed(u->in_memblocks[i]);
|
||||
free(u->in_memblocks);
|
||||
}
|
||||
|
||||
|
|
|
|||
89
src/pacat.c
89
src/pacat.c
|
|
@ -11,6 +11,8 @@
|
|||
#include "mainloop.h"
|
||||
#include "mainloop-signal.h"
|
||||
|
||||
static enum { RECORD, PLAYBACK } mode = PLAYBACK;
|
||||
|
||||
static struct pa_context *context = NULL;
|
||||
static struct pa_stream *stream = NULL;
|
||||
static struct pa_mainloop_api *mainloop_api = NULL;
|
||||
|
|
@ -18,7 +20,7 @@ static struct pa_mainloop_api *mainloop_api = NULL;
|
|||
static void *buffer = NULL;
|
||||
static size_t buffer_length = 0, buffer_index = 0;
|
||||
|
||||
static void* stdin_source = NULL;
|
||||
static void* stdio_source = NULL;
|
||||
|
||||
static void quit(int ret) {
|
||||
assert(mainloop_api);
|
||||
|
|
@ -37,7 +39,7 @@ static void stream_die_callback(struct pa_stream *s, void *userdata) {
|
|||
quit(1);
|
||||
}
|
||||
|
||||
static void do_write(size_t length) {
|
||||
static void do_stream_write(size_t length) {
|
||||
size_t l;
|
||||
assert(length);
|
||||
|
||||
|
|
@ -62,13 +64,30 @@ static void do_write(size_t length) {
|
|||
static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
|
||||
assert(s && length);
|
||||
|
||||
if (stdin_source)
|
||||
mainloop_api->enable_io(mainloop_api, stdin_source, PA_MAINLOOP_API_IO_EVENT_INPUT);
|
||||
if (stdio_source)
|
||||
mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_INPUT);
|
||||
|
||||
if (!buffer)
|
||||
return;
|
||||
|
||||
do_write(length);
|
||||
do_stream_write(length);
|
||||
}
|
||||
|
||||
static void stream_read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) {
|
||||
assert(s && data && length);
|
||||
|
||||
if (stdio_source)
|
||||
mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_OUTPUT);
|
||||
|
||||
if (buffer) {
|
||||
fprintf(stderr, "Buffer overrrun, dropping incoming data\n");
|
||||
return;
|
||||
}
|
||||
|
||||
buffer = malloc(buffer_length = length);
|
||||
assert(buffer);
|
||||
memcpy(buffer, data, length);
|
||||
buffer_index = 0;
|
||||
}
|
||||
|
||||
static void stream_complete_callback(struct pa_stream*s, int success, void *userdata) {
|
||||
|
|
@ -99,13 +118,14 @@ static void context_complete_callback(struct pa_context *c, int success, void *u
|
|||
|
||||
fprintf(stderr, "Connection established.\n");
|
||||
|
||||
if (!(stream = pa_stream_new(c, PA_STREAM_PLAYBACK, NULL, "pacat", &ss, NULL, stream_complete_callback, NULL))) {
|
||||
if (!(stream = pa_stream_new(c, mode == PLAYBACK ? PA_STREAM_PLAYBACK : PA_STREAM_RECORD, NULL, "pacat", &ss, NULL, stream_complete_callback, NULL))) {
|
||||
fprintf(stderr, "pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(c)));
|
||||
goto fail;
|
||||
}
|
||||
|
||||
pa_stream_set_die_callback(stream, stream_die_callback, NULL);
|
||||
pa_stream_set_write_callback(stream, stream_write_callback, NULL);
|
||||
pa_stream_set_read_callback(stream, stream_read_callback, NULL);
|
||||
|
||||
return;
|
||||
|
||||
|
|
@ -132,10 +152,10 @@ static void stream_drain_complete(struct pa_stream*s, void *userdata) {
|
|||
static void stdin_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
|
||||
size_t l, w = 0;
|
||||
ssize_t r;
|
||||
assert(a == mainloop_api && id && fd == STDIN_FILENO && stdin_source == id);
|
||||
assert(a == mainloop_api && id && stdio_source == id);
|
||||
|
||||
if (buffer) {
|
||||
mainloop_api->enable_io(mainloop_api, stdin_source, PA_MAINLOOP_API_IO_EVENT_NULL);
|
||||
mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -153,8 +173,8 @@ static void stdin_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_m
|
|||
quit(1);
|
||||
}
|
||||
|
||||
mainloop_api->cancel_io(mainloop_api, stdin_source);
|
||||
stdin_source = NULL;
|
||||
mainloop_api->cancel_io(mainloop_api, stdio_source);
|
||||
stdio_source = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -162,9 +182,38 @@ static void stdin_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_m
|
|||
buffer_index = 0;
|
||||
|
||||
if (w)
|
||||
do_write(w);
|
||||
do_stream_write(w);
|
||||
}
|
||||
|
||||
static void stdout_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
|
||||
ssize_t r;
|
||||
assert(a == mainloop_api && id && stdio_source == id);
|
||||
|
||||
if (!buffer) {
|
||||
mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
assert(buffer_length);
|
||||
|
||||
if ((r = write(fd, buffer+buffer_index, buffer_length)) <= 0) {
|
||||
fprintf(stderr, "write() failed: %s\n", strerror(errno));
|
||||
quit(1);
|
||||
|
||||
mainloop_api->cancel_io(mainloop_api, stdio_source);
|
||||
stdio_source = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
buffer_length -= r;
|
||||
buffer_index += r;
|
||||
|
||||
if (!buffer_length) {
|
||||
free(buffer);
|
||||
buffer = NULL;
|
||||
buffer_length = buffer_index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void exit_signal_callback(void *id, int sig, void *userdata) {
|
||||
fprintf(stderr, "Got SIGINT, exiting.\n");
|
||||
|
|
@ -175,7 +224,18 @@ static void exit_signal_callback(void *id, int sig, void *userdata) {
|
|||
int main(int argc, char *argv[]) {
|
||||
struct pa_mainloop* m;
|
||||
int ret = 1, r;
|
||||
char *bn;
|
||||
|
||||
if (!(bn = strrchr(argv[0], '/')))
|
||||
bn = argv[0];
|
||||
|
||||
if (strstr(bn, "rec") || strstr(bn, "mon"))
|
||||
mode = RECORD;
|
||||
else if (strstr(bn, "cat") || strstr(bn, "play"))
|
||||
mode = PLAYBACK;
|
||||
|
||||
fprintf(stderr, "Opening a %s stream.\n", mode == RECORD ? "recording" : "playback");
|
||||
|
||||
if (!(m = pa_mainloop_new())) {
|
||||
fprintf(stderr, "pa_mainloop_new() failed.\n");
|
||||
goto quit;
|
||||
|
|
@ -188,7 +248,10 @@ int main(int argc, char *argv[]) {
|
|||
pa_signal_register(SIGINT, exit_signal_callback, NULL);
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
|
||||
if (!(stdin_source = mainloop_api->source_io(mainloop_api, STDIN_FILENO, PA_MAINLOOP_API_IO_EVENT_INPUT, stdin_callback, NULL))) {
|
||||
if (!(stdio_source = mainloop_api->source_io(mainloop_api,
|
||||
mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
|
||||
mode == PLAYBACK ? PA_MAINLOOP_API_IO_EVENT_INPUT : PA_MAINLOOP_API_IO_EVENT_OUTPUT,
|
||||
mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
|
||||
fprintf(stderr, "source_io() failed.\n");
|
||||
goto quit;
|
||||
}
|
||||
|
|
@ -215,6 +278,8 @@ quit:
|
|||
pa_stream_free(stream);
|
||||
if (context)
|
||||
pa_context_free(context);
|
||||
|
||||
pa_signal_done();
|
||||
if (m)
|
||||
pa_mainloop_free(m);
|
||||
if (buffer)
|
||||
|
|
|
|||
21
src/polyp.c
21
src/polyp.c
|
|
@ -13,10 +13,11 @@
|
|||
#include "authkey.h"
|
||||
#include "util.h"
|
||||
|
||||
#define DEFAULT_MAXLENGTH 20480
|
||||
#define DEFAULT_MAXLENGTH 204800
|
||||
#define DEFAULT_TLENGTH 10240
|
||||
#define DEFAULT_PREBUF 4096
|
||||
#define DEFAULT_MINREQ 1024
|
||||
#define DEFAULT_FRAGSIZE 1024
|
||||
|
||||
#define DEFAULT_TIMEOUT (5*60)
|
||||
#define DEFAULT_SERVER "/tmp/polypaudio/native"
|
||||
|
|
@ -187,7 +188,7 @@ static void context_dead(struct pa_context *c) {
|
|||
if (c->die_callback)
|
||||
c->die_callback(c, c->die_userdata);
|
||||
} else
|
||||
s->state = CONTEXT_DEAD;
|
||||
c->state = CONTEXT_DEAD;
|
||||
}
|
||||
|
||||
static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
|
||||
|
|
@ -206,7 +207,7 @@ static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *pack
|
|||
}
|
||||
}
|
||||
|
||||
static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, int32_t delta, struct pa_memchunk *chunk, void *userdata) {
|
||||
static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata) {
|
||||
struct pa_context *c = userdata;
|
||||
struct pa_stream *s;
|
||||
assert(p && chunk && c && chunk->memblock && chunk->memblock->data);
|
||||
|
|
@ -378,7 +379,7 @@ static void command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t
|
|||
s->write_callback(s, s->requested_bytes, s->write_userdata);
|
||||
}
|
||||
|
||||
static void create_playback_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
|
||||
static void create_stream_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
|
||||
struct pa_stream *s = userdata;
|
||||
assert(pd && s && s->state == STREAM_CREATING);
|
||||
|
||||
|
|
@ -427,12 +428,15 @@ static void create_stream(struct pa_stream *s, uint32_t tdev_index) {
|
|||
pa_tagstruct_put_sample_spec(t, &s->sample_spec);
|
||||
pa_tagstruct_putu32(t, tdev_index);
|
||||
pa_tagstruct_putu32(t, s->buffer_attr.maxlength);
|
||||
pa_tagstruct_putu32(t, s->buffer_attr.tlength);
|
||||
pa_tagstruct_putu32(t, s->buffer_attr.prebuf);
|
||||
pa_tagstruct_putu32(t, s->buffer_attr.minreq);
|
||||
if (s->direction == PA_STREAM_PLAYBACK) {
|
||||
pa_tagstruct_putu32(t, s->buffer_attr.tlength);
|
||||
pa_tagstruct_putu32(t, s->buffer_attr.prebuf);
|
||||
pa_tagstruct_putu32(t, s->buffer_attr.minreq);
|
||||
} else
|
||||
pa_tagstruct_putu32(t, s->buffer_attr.fragsize);
|
||||
|
||||
pa_pstream_send_tagstruct(s->context->pstream, t);
|
||||
pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, create_playback_callback, s);
|
||||
pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, s);
|
||||
}
|
||||
|
||||
static void lookup_device_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
|
||||
|
|
@ -522,6 +526,7 @@ struct pa_stream* pa_stream_new(
|
|||
s->buffer_attr.tlength = DEFAULT_TLENGTH;
|
||||
s->buffer_attr.prebuf = DEFAULT_PREBUF;
|
||||
s->buffer_attr.minreq = DEFAULT_MINREQ;
|
||||
s->buffer_attr.fragsize = DEFAULT_FRAGSIZE;
|
||||
}
|
||||
|
||||
s->next = c->first_stream;
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ struct pa_buffer_attr {
|
|||
uint32_t tlength;
|
||||
uint32_t prebuf;
|
||||
uint32_t minreq;
|
||||
uint32_t fragsize;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -580,9 +580,8 @@ static int do_read(struct connection *c) {
|
|||
assert(c->sink_input);
|
||||
pa_sink_notify(c->sink_input->sink);
|
||||
|
||||
} else
|
||||
assert(0);
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ struct record_stream {
|
|||
uint32_t index;
|
||||
struct pa_source_output *source_output;
|
||||
struct pa_memblockq *memblockq;
|
||||
size_t fragment_size;
|
||||
};
|
||||
|
||||
struct playback_stream {
|
||||
|
|
@ -43,6 +44,7 @@ struct connection {
|
|||
struct pa_pstream *pstream;
|
||||
struct pa_pdispatch *pdispatch;
|
||||
struct pa_idxset *record_streams, *playback_streams;
|
||||
uint32_t rrobin_index;
|
||||
};
|
||||
|
||||
struct pa_protocol_native {
|
||||
|
|
@ -60,10 +62,15 @@ static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i);
|
|||
|
||||
static void request_bytes(struct playback_stream*s);
|
||||
|
||||
static void source_output_kill_cb(struct pa_source_output *o);
|
||||
static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk);
|
||||
|
||||
static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
|
||||
static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
|
||||
static void command_delete_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
|
||||
static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
|
||||
static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
|
||||
static void command_delete_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
|
||||
static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
|
||||
static void command_set_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
|
||||
static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
|
||||
|
|
@ -75,8 +82,8 @@ static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
|
|||
[PA_COMMAND_CREATE_PLAYBACK_STREAM] = { command_create_playback_stream },
|
||||
[PA_COMMAND_DELETE_PLAYBACK_STREAM] = { command_delete_playback_stream },
|
||||
[PA_COMMAND_DRAIN_PLAYBACK_STREAM] = { command_drain_playback_stream },
|
||||
[PA_COMMAND_CREATE_RECORD_STREAM] = { NULL },
|
||||
[PA_COMMAND_DELETE_RECORD_STREAM] = { NULL },
|
||||
[PA_COMMAND_CREATE_RECORD_STREAM] = { command_create_record_stream },
|
||||
[PA_COMMAND_DELETE_RECORD_STREAM] = { command_delete_record_stream },
|
||||
[PA_COMMAND_AUTH] = { command_auth },
|
||||
[PA_COMMAND_REQUEST] = { NULL },
|
||||
[PA_COMMAND_EXIT] = { command_exit },
|
||||
|
|
@ -87,6 +94,34 @@ static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
|
|||
|
||||
/* structure management */
|
||||
|
||||
static struct record_stream* record_stream_new(struct connection *c, struct pa_source *source, struct pa_sample_spec *ss, const char *name, size_t maxlength, size_t fragment_size) {
|
||||
struct record_stream *s;
|
||||
struct pa_source_output *source_output;
|
||||
size_t base;
|
||||
assert(c && source && ss && name && maxlength);
|
||||
|
||||
if (!(source_output = pa_source_output_new(source, name, ss)))
|
||||
return NULL;
|
||||
|
||||
s = malloc(sizeof(struct record_stream));
|
||||
assert(s);
|
||||
s->connection = c;
|
||||
s->source_output = source_output;
|
||||
s->source_output->push = source_output_push_cb;
|
||||
s->source_output->kill = source_output_kill_cb;
|
||||
s->source_output->userdata = s;
|
||||
|
||||
s->memblockq = pa_memblockq_new(maxlength, 0, base = pa_sample_size(ss), 0, 0);
|
||||
assert(s->memblockq);
|
||||
|
||||
s->fragment_size = (fragment_size/base)*base;
|
||||
if (!s->fragment_size)
|
||||
s->fragment_size = base;
|
||||
|
||||
pa_idxset_put(c->record_streams, s, &s->index);
|
||||
return s;
|
||||
}
|
||||
|
||||
static void record_stream_free(struct record_stream* r) {
|
||||
assert(r && r->connection);
|
||||
|
||||
|
|
@ -102,14 +137,17 @@ static struct playback_stream* playback_stream_new(struct connection *c, struct
|
|||
size_t prebuf,
|
||||
size_t minreq) {
|
||||
struct playback_stream *s;
|
||||
struct pa_sink_input *sink_input;
|
||||
assert(c && sink && ss && name && maxlength);
|
||||
|
||||
if (!(sink_input = pa_sink_input_new(sink, name, ss)))
|
||||
return NULL;
|
||||
|
||||
s = malloc(sizeof(struct playback_stream));
|
||||
assert (s);
|
||||
s->connection = c;
|
||||
s->sink_input = sink_input;
|
||||
|
||||
s->sink_input = pa_sink_input_new(sink, name, ss);
|
||||
assert(s->sink_input);
|
||||
s->sink_input->peek = sink_input_peek_cb;
|
||||
s->sink_input->drop = sink_input_drop_cb;
|
||||
s->sink_input->kill = sink_input_kill_cb;
|
||||
|
|
@ -187,6 +225,35 @@ static void request_bytes(struct playback_stream *s) {
|
|||
/*fprintf(stderr, "Requesting %u bytes\n", l);*/
|
||||
}
|
||||
|
||||
static void send_memblock(struct connection *c) {
|
||||
uint32_t start;
|
||||
struct record_stream *r;
|
||||
|
||||
start = PA_IDXSET_INVALID;
|
||||
for (;;) {
|
||||
struct pa_memchunk chunk;
|
||||
|
||||
if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
|
||||
return;
|
||||
|
||||
if (start == PA_IDXSET_INVALID)
|
||||
start = c->rrobin_index;
|
||||
else if (start == c->rrobin_index)
|
||||
return;
|
||||
|
||||
if (pa_memblockq_peek(r->memblockq, &chunk) >= 0) {
|
||||
if (chunk.length > r->fragment_size)
|
||||
chunk.length = r->fragment_size;
|
||||
|
||||
pa_pstream_send_memblock(c->pstream, r->index, 0, &chunk);
|
||||
pa_memblockq_drop(r->memblockq, chunk.length);
|
||||
pa_memblock_unref(chunk.memblock);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*** sinkinput callbacks ***/
|
||||
|
||||
static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
|
||||
|
|
@ -215,11 +282,8 @@ static void sink_input_drop_cb(struct pa_sink_input *i, size_t length) {
|
|||
}
|
||||
|
||||
static void sink_input_kill_cb(struct pa_sink_input *i) {
|
||||
struct playback_stream *s;
|
||||
assert(i && i->userdata);
|
||||
s = i->userdata;
|
||||
|
||||
playback_stream_free(s);
|
||||
playback_stream_free((struct playback_stream *) i->userdata);
|
||||
}
|
||||
|
||||
static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
|
||||
|
|
@ -230,6 +294,23 @@ static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
|
|||
return pa_samples_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
|
||||
}
|
||||
|
||||
/*** source_output callbacks ***/
|
||||
|
||||
static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
|
||||
struct record_stream *s;
|
||||
assert(o && o->userdata && chunk);
|
||||
s = o->userdata;
|
||||
|
||||
pa_memblockq_push(s->memblockq, chunk, 0);
|
||||
if (!pa_pstream_is_pending(s->connection->pstream))
|
||||
send_memblock(s->connection);
|
||||
}
|
||||
|
||||
static void source_output_kill_cb(struct pa_source_output *o) {
|
||||
assert(o && o->userdata);
|
||||
record_stream_free((struct record_stream *) o->userdata);
|
||||
}
|
||||
|
||||
/*** pdispatch callbacks ***/
|
||||
|
||||
static void protocol_error(struct connection *c) {
|
||||
|
|
@ -313,6 +394,84 @@ static void command_delete_playback_stream(struct pa_pdispatch *pd, uint32_t com
|
|||
return;
|
||||
}
|
||||
|
||||
playback_stream_free(s);
|
||||
pa_pstream_send_simple_ack(c->pstream, tag);
|
||||
}
|
||||
|
||||
static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
|
||||
struct connection *c = userdata;
|
||||
struct record_stream *s;
|
||||
size_t maxlength, fragment_size;
|
||||
uint32_t source_index;
|
||||
const char *name;
|
||||
struct pa_sample_spec ss;
|
||||
struct pa_tagstruct *reply;
|
||||
struct pa_source *source;
|
||||
assert(c && t && c->protocol && c->protocol->core);
|
||||
|
||||
if (pa_tagstruct_gets(t, &name) < 0 ||
|
||||
pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
|
||||
pa_tagstruct_getu32(t, &source_index) < 0 ||
|
||||
pa_tagstruct_getu32(t, &maxlength) < 0 ||
|
||||
pa_tagstruct_getu32(t, &fragment_size) < 0 ||
|
||||
!pa_tagstruct_eof(t)) {
|
||||
protocol_error(c);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!c->authorized) {
|
||||
pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
|
||||
return;
|
||||
}
|
||||
|
||||
if (source_index == (uint32_t) -1)
|
||||
source = pa_source_get_default(c->protocol->core);
|
||||
else
|
||||
source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
|
||||
|
||||
if (!source) {
|
||||
pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(s = record_stream_new(c, source, &ss, name, maxlength, fragment_size))) {
|
||||
pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
|
||||
return;
|
||||
}
|
||||
|
||||
reply = pa_tagstruct_new(NULL, 0);
|
||||
assert(reply);
|
||||
pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
|
||||
pa_tagstruct_putu32(reply, tag);
|
||||
pa_tagstruct_putu32(reply, s->index);
|
||||
assert(s->source_output);
|
||||
pa_tagstruct_putu32(reply, s->source_output->index);
|
||||
pa_pstream_send_tagstruct(c->pstream, reply);
|
||||
}
|
||||
|
||||
static void command_delete_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
|
||||
struct connection *c = userdata;
|
||||
uint32_t channel;
|
||||
struct record_stream *s;
|
||||
assert(c && t);
|
||||
|
||||
if (pa_tagstruct_getu32(t, &channel) < 0 ||
|
||||
!pa_tagstruct_eof(t)) {
|
||||
protocol_error(c);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!c->authorized) {
|
||||
pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
|
||||
pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
|
||||
return;
|
||||
}
|
||||
|
||||
record_stream_free(s);
|
||||
pa_pstream_send_simple_ack(c->pstream, tag);
|
||||
}
|
||||
|
||||
|
|
@ -449,7 +608,7 @@ static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t comm
|
|||
|
||||
/*** pstream callbacks ***/
|
||||
|
||||
static void packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
|
||||
static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
|
||||
struct connection *c = userdata;
|
||||
assert(p && packet && packet->data && c);
|
||||
|
||||
|
|
@ -459,7 +618,7 @@ static void packet_callback(struct pa_pstream *p, struct pa_packet *packet, void
|
|||
}
|
||||
}
|
||||
|
||||
static void memblock_callback(struct pa_pstream *p, uint32_t channel, int32_t delta, struct pa_memchunk *chunk, void *userdata) {
|
||||
static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata) {
|
||||
struct connection *c = userdata;
|
||||
struct playback_stream *stream;
|
||||
assert(p && chunk && userdata);
|
||||
|
|
@ -482,7 +641,7 @@ static void memblock_callback(struct pa_pstream *p, uint32_t channel, int32_t de
|
|||
/*fprintf(stderr, "Recieved %u bytes.\n", chunk->length);*/
|
||||
}
|
||||
|
||||
static void die_callback(struct pa_pstream *p, void *userdata) {
|
||||
static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
|
||||
struct connection *c = userdata;
|
||||
assert(p && c);
|
||||
connection_free(c);
|
||||
|
|
@ -490,6 +649,14 @@ static void die_callback(struct pa_pstream *p, void *userdata) {
|
|||
fprintf(stderr, "protocol-native: connection died.\n");
|
||||
}
|
||||
|
||||
|
||||
static void pstream_drain_callback(struct pa_pstream *p, void *userdata) {
|
||||
struct connection *c = userdata;
|
||||
assert(p && c);
|
||||
|
||||
send_memblock(c);
|
||||
}
|
||||
|
||||
/*** client callbacks ***/
|
||||
|
||||
static void client_kill_cb(struct pa_client *c) {
|
||||
|
|
@ -516,9 +683,10 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo
|
|||
c->pstream = pa_pstream_new(p->core->mainloop, io);
|
||||
assert(c->pstream);
|
||||
|
||||
pa_pstream_set_recieve_packet_callback(c->pstream, packet_callback, c);
|
||||
pa_pstream_set_recieve_memblock_callback(c->pstream, memblock_callback, c);
|
||||
pa_pstream_set_die_callback(c->pstream, die_callback, c);
|
||||
pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
|
||||
pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
|
||||
pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
|
||||
pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
|
||||
|
||||
c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
|
||||
assert(c->pdispatch);
|
||||
|
|
@ -527,6 +695,8 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo
|
|||
c->playback_streams = pa_idxset_new(NULL, NULL);
|
||||
assert(c->record_streams && c->playback_streams);
|
||||
|
||||
c->rrobin_index = PA_IDXSET_INVALID;
|
||||
|
||||
pa_idxset_put(p->connections, c, NULL);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -271,7 +271,11 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo
|
|||
}
|
||||
|
||||
c->sink_input = pa_sink_input_new(sink, c->client->name, &p->sample_spec);
|
||||
assert(c->sink_input);
|
||||
if (!c->sink_input) {
|
||||
fprintf(stderr, "Failed to create sink input.\n");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
c->sink_input->peek = sink_input_peek_cb;
|
||||
c->sink_input->drop = sink_input_drop_cb;
|
||||
c->sink_input->kill = sink_input_kill_cb;
|
||||
|
|
@ -296,7 +300,11 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo
|
|||
}
|
||||
|
||||
c->source_output = pa_source_output_new(source, c->client->name, &p->sample_spec);
|
||||
assert(c->source_output);
|
||||
if (!c->source_output) {
|
||||
fprintf(stderr, "Failed to create source output.\n");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
c->source_output->push = source_output_push_cb;
|
||||
c->source_output->kill = source_output_kill_cb;
|
||||
c->source_output->userdata = c;
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ struct pa_pstream {
|
|||
int in_use, shall_free;
|
||||
|
||||
int dead;
|
||||
void (*die_callback) (struct pa_pstream *p, void *userdad);
|
||||
void (*die_callback) (struct pa_pstream *p, void *userdata);
|
||||
void *die_callback_userdata;
|
||||
|
||||
struct {
|
||||
|
|
@ -59,7 +59,7 @@ struct pa_pstream {
|
|||
void (*recieve_packet_callback) (struct pa_pstream *p, struct pa_packet *packet, void *userdata);
|
||||
void *recieve_packet_callback_userdata;
|
||||
|
||||
void (*recieve_memblock_callback) (struct pa_pstream *p, uint32_t channel, int32_t delta, struct pa_memchunk *chunk, void *userdata);
|
||||
void (*recieve_memblock_callback) (struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata);
|
||||
void *recieve_memblock_callback_userdata;
|
||||
|
||||
void (*drain_callback)(struct pa_pstream *p, void *userdata);
|
||||
|
|
@ -73,21 +73,36 @@ static void do_something(struct pa_pstream *p) {
|
|||
assert(p && !p->shall_free);
|
||||
p->mainloop->enable_fixed(p->mainloop, p->mainloop_source, 0);
|
||||
|
||||
p->in_use = 1;
|
||||
do_write(p);
|
||||
p->in_use = 0;
|
||||
if (p->dead)
|
||||
return;
|
||||
|
||||
if (pa_iochannel_is_hungup(p->io)) {
|
||||
p->dead = 1;
|
||||
if (p->die_callback)
|
||||
p->die_callback(p, p->die_callback_userdata);
|
||||
|
||||
if (p->shall_free) {
|
||||
pa_pstream_free(p);
|
||||
return;
|
||||
}
|
||||
|
||||
p->in_use = 1;
|
||||
do_read(p);
|
||||
p->in_use = 0;
|
||||
if (p->shall_free) {
|
||||
pa_pstream_free(p);
|
||||
return;
|
||||
|
||||
if (pa_iochannel_is_writable(p->io)) {
|
||||
p->in_use = 1;
|
||||
do_write(p);
|
||||
p->in_use = 0;
|
||||
|
||||
if (p->shall_free) {
|
||||
pa_pstream_free(p);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (pa_iochannel_is_readable(p->io)) {
|
||||
p->in_use = 1;
|
||||
do_read(p);
|
||||
p->in_use = 0;
|
||||
if (p->shall_free) {
|
||||
pa_pstream_free(p);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -199,7 +214,7 @@ void pa_pstream_send_packet(struct pa_pstream*p, struct pa_packet *packet) {
|
|||
p->mainloop->enable_fixed(p->mainloop, p->mainloop_source, 1);
|
||||
}
|
||||
|
||||
void pa_pstream_send_memblock(struct pa_pstream*p, uint32_t channel, int32_t delta, struct pa_memchunk *chunk) {
|
||||
void pa_pstream_send_memblock(struct pa_pstream*p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk) {
|
||||
struct item_info *i;
|
||||
assert(p && channel != (uint32_t) -1 && chunk);
|
||||
|
||||
|
|
@ -223,7 +238,7 @@ void pa_pstream_set_recieve_packet_callback(struct pa_pstream *p, void (*callbac
|
|||
p->recieve_packet_callback_userdata = userdata;
|
||||
}
|
||||
|
||||
void pa_pstream_set_recieve_memblock_callback(struct pa_pstream *p, void (*callback) (struct pa_pstream *p, uint32_t channel, int32_t delta, struct pa_memchunk *chunk, void *userdata), void *userdata) {
|
||||
void pa_pstream_set_recieve_memblock_callback(struct pa_pstream *p, void (*callback) (struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata), void *userdata) {
|
||||
assert(p && callback);
|
||||
|
||||
p->recieve_memblock_callback = callback;
|
||||
|
|
@ -259,9 +274,6 @@ static void do_write(struct pa_pstream *p) {
|
|||
ssize_t r;
|
||||
assert(p);
|
||||
|
||||
if (p->dead || !pa_iochannel_is_writable(p->io))
|
||||
return;
|
||||
|
||||
if (!p->write.current)
|
||||
prepare_next_write_item(p);
|
||||
|
||||
|
|
@ -306,9 +318,6 @@ static void do_read(struct pa_pstream *p) {
|
|||
ssize_t r;
|
||||
assert(p);
|
||||
|
||||
if (p->dead || !pa_iochannel_is_readable(p->io))
|
||||
return;
|
||||
|
||||
if (p->read.index < PA_PSTREAM_DESCRIPTOR_SIZE) {
|
||||
d = (void*) p->read.descriptor + p->read.index;
|
||||
l = PA_PSTREAM_DESCRIPTOR_SIZE - p->read.index;
|
||||
|
|
@ -416,7 +425,6 @@ int pa_pstream_is_pending(struct pa_pstream *p) {
|
|||
|
||||
void pa_pstream_set_drain_callback(struct pa_pstream *p, void (*cb)(struct pa_pstream *p, void *userdata), void *userdata) {
|
||||
assert(p);
|
||||
assert(!cb || pa_pstream_is_pending(p));
|
||||
|
||||
p->drain_callback = cb;
|
||||
p->drain_userdata = userdata;
|
||||
|
|
|
|||
|
|
@ -17,10 +17,10 @@ struct pa_pstream* pa_pstream_new(struct pa_mainloop_api *m, struct pa_iochannel
|
|||
void pa_pstream_free(struct pa_pstream*p);
|
||||
|
||||
void pa_pstream_send_packet(struct pa_pstream*p, struct pa_packet *packet);
|
||||
void pa_pstream_send_memblock(struct pa_pstream*p, uint32_t channel, int32_t delta, struct pa_memchunk *chunk);
|
||||
void pa_pstream_send_memblock(struct pa_pstream*p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk);
|
||||
|
||||
void pa_pstream_set_recieve_packet_callback(struct pa_pstream *p, void (*callback) (struct pa_pstream *p, struct pa_packet *packet, void *userdata), void *userdata);
|
||||
void pa_pstream_set_recieve_memblock_callback(struct pa_pstream *p, void (*callback) (struct pa_pstream *p, uint32_t channel, int32_t delta, struct pa_memchunk *chunk, void *userdata), void *userdata);
|
||||
void pa_pstream_set_recieve_memblock_callback(struct pa_pstream *p, void (*callback) (struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata), void *userdata);
|
||||
void pa_pstream_set_drain_callback(struct pa_pstream *p, void (*cb)(struct pa_pstream *p, void *userdata), void *userdata);
|
||||
|
||||
void pa_pstream_set_die_callback(struct pa_pstream *p, void (*callback)(struct pa_pstream *p, void *userdata), void *userdata);
|
||||
|
|
|
|||
4
src/todo
4
src/todo
|
|
@ -1,4 +1,4 @@
|
|||
- recording (esound, native)
|
||||
- implement parec-simple
|
||||
- native library/protocol:
|
||||
more functions (esp. latency)
|
||||
|
||||
|
|
@ -9,7 +9,6 @@
|
|||
- module field for all entities
|
||||
|
||||
- move more stuff from module-oss[-dma] to liboss-util
|
||||
- in module-oss: create source first, than sink
|
||||
|
||||
- merge memchunks in memblockq
|
||||
|
||||
|
|
@ -41,3 +40,4 @@ drivers:
|
|||
|
||||
modules:
|
||||
- alsa?
|
||||
- http
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue