2004-06-08 23:54:24 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
#include "sinkinput.h"
|
|
|
|
|
#include "sourceoutput.h"
|
2004-06-08 23:54:24 +00:00
|
|
|
#include "protocol-simple.h"
|
|
|
|
|
#include "client.h"
|
2004-06-23 23:17:30 +00:00
|
|
|
#include "sample-util.h"
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
struct connection {
|
|
|
|
|
struct protocol_simple *protocol;
|
|
|
|
|
struct iochannel *io;
|
2004-06-15 17:05:03 +00:00
|
|
|
struct sink_input *sink_input;
|
|
|
|
|
struct source_output *source_output;
|
2004-06-08 23:54:24 +00:00
|
|
|
struct client *client;
|
2004-06-15 17:05:03 +00:00
|
|
|
struct memblockq *input_memblockq, *output_memblockq;
|
2004-06-08 23:54:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct protocol_simple {
|
|
|
|
|
struct core *core;
|
|
|
|
|
struct socket_server*server;
|
|
|
|
|
struct idxset *connections;
|
|
|
|
|
enum protocol_simple_mode mode;
|
2004-07-03 00:19:17 +00:00
|
|
|
struct pa_sample_spec sample_spec;
|
2004-06-08 23:54:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define BUFSIZE PIPE_BUF
|
|
|
|
|
|
2004-07-03 00:19:17 +00:00
|
|
|
static void connection_free(struct connection *c) {
|
|
|
|
|
assert(c);
|
|
|
|
|
|
|
|
|
|
idxset_remove_by_data(c->protocol->connections, c, NULL);
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
if (c->sink_input)
|
|
|
|
|
sink_input_free(c->sink_input);
|
|
|
|
|
if (c->source_output)
|
|
|
|
|
source_output_free(c->source_output);
|
|
|
|
|
if (c->client)
|
|
|
|
|
client_free(c->client);
|
|
|
|
|
if (c->io)
|
|
|
|
|
iochannel_free(c->io);
|
|
|
|
|
if (c->input_memblockq)
|
|
|
|
|
memblockq_free(c->input_memblockq);
|
|
|
|
|
if (c->output_memblockq)
|
|
|
|
|
memblockq_free(c->output_memblockq);
|
2004-06-08 23:54:24 +00:00
|
|
|
free(c);
|
|
|
|
|
}
|
2004-06-15 00:29:01 +00:00
|
|
|
static int do_read(struct connection *c) {
|
|
|
|
|
struct memchunk chunk;
|
|
|
|
|
ssize_t r;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-06-15 00:29:01 +00:00
|
|
|
if (!iochannel_is_readable(c->io))
|
|
|
|
|
return 0;
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
if (!c->sink_input || !memblockq_is_writable(c->input_memblockq, BUFSIZE))
|
2004-06-15 00:29:01 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
chunk.memblock = memblock_new(BUFSIZE);
|
|
|
|
|
assert(chunk.memblock);
|
2004-06-15 15:18:33 +00:00
|
|
|
|
2004-06-15 00:29:01 +00:00
|
|
|
if ((r = iochannel_read(c->io, chunk.memblock->data, BUFSIZE)) <= 0) {
|
|
|
|
|
fprintf(stderr, "read(): %s\n", r == 0 ? "EOF" : strerror(errno));
|
2004-06-08 23:54:24 +00:00
|
|
|
memblock_unref(chunk.memblock);
|
2004-06-15 00:29:01 +00:00
|
|
|
return -1;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
2004-06-15 15:18:33 +00:00
|
|
|
|
2004-06-29 16:48:37 +00:00
|
|
|
chunk.memblock->length = chunk.length = r;
|
2004-06-15 00:29:01 +00:00
|
|
|
chunk.index = 0;
|
2004-06-15 17:05:03 +00:00
|
|
|
|
|
|
|
|
assert(c->input_memblockq);
|
2004-07-03 00:19:17 +00:00
|
|
|
memblockq_push_align(c->input_memblockq, &chunk, 0);
|
2004-06-15 00:29:01 +00:00
|
|
|
memblock_unref(chunk.memblock);
|
2004-06-20 01:12:13 +00:00
|
|
|
assert(c->sink_input);
|
2004-06-15 17:05:03 +00:00
|
|
|
sink_notify(c->sink_input->sink);
|
2004-06-16 00:05:30 +00:00
|
|
|
|
2004-06-15 00:29:01 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-06-15 00:29:01 +00:00
|
|
|
static int do_write(struct connection *c) {
|
|
|
|
|
struct memchunk chunk;
|
|
|
|
|
ssize_t r;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-06-15 00:29:01 +00:00
|
|
|
if (!iochannel_is_writable(c->io))
|
|
|
|
|
return 0;
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
if (!c->source_output)
|
2004-06-15 00:29:01 +00:00
|
|
|
return 0;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
assert(c->output_memblockq);
|
2004-06-16 00:05:30 +00:00
|
|
|
if (memblockq_peek(c->output_memblockq, &chunk) < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2004-06-15 00:29:01 +00:00
|
|
|
assert(chunk.memblock && chunk.length);
|
|
|
|
|
|
|
|
|
|
if ((r = iochannel_write(c->io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
|
|
|
|
|
fprintf(stderr, "write(): %s\n", strerror(errno));
|
2004-06-08 23:54:24 +00:00
|
|
|
memblock_unref(chunk.memblock);
|
2004-06-15 00:29:01 +00:00
|
|
|
return -1;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
2004-06-15 00:29:01 +00:00
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
memblockq_drop(c->output_memblockq, r);
|
2004-06-15 00:29:01 +00:00
|
|
|
memblock_unref(chunk.memblock);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
/*** sink_input callbacks ***/
|
|
|
|
|
|
2004-06-18 17:12:50 +00:00
|
|
|
static int sink_input_peek_cb(struct sink_input *i, struct memchunk *chunk) {
|
2004-06-23 23:17:30 +00:00
|
|
|
struct connection*c;
|
|
|
|
|
assert(i && i->userdata && chunk);
|
|
|
|
|
c = i->userdata;
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
if (memblockq_peek(c->input_memblockq, chunk) < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sink_input_drop_cb(struct sink_input *i, size_t length) {
|
|
|
|
|
struct connection*c = i->userdata;
|
|
|
|
|
assert(i && c && length);
|
|
|
|
|
|
|
|
|
|
memblockq_drop(c->input_memblockq, length);
|
|
|
|
|
|
|
|
|
|
if (do_read(c) < 0)
|
2004-07-03 00:19:17 +00:00
|
|
|
connection_free(c);
|
2004-06-15 17:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sink_input_kill_cb(struct sink_input *i) {
|
|
|
|
|
assert(i && i->userdata);
|
2004-07-03 00:19:17 +00:00
|
|
|
connection_free((struct connection *) i->userdata);
|
2004-06-15 17:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
2004-06-18 17:12:50 +00:00
|
|
|
|
|
|
|
|
static uint32_t sink_input_get_latency_cb(struct sink_input *i) {
|
|
|
|
|
struct connection*c = i->userdata;
|
|
|
|
|
assert(i && c);
|
2004-06-23 23:17:30 +00:00
|
|
|
return pa_samples_usec(memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
|
2004-06-18 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
/*** source_output callbacks ***/
|
|
|
|
|
|
2004-07-03 00:19:17 +00:00
|
|
|
static void source_output_push_cb(struct source_output *o, const struct memchunk *chunk) {
|
2004-06-15 17:05:03 +00:00
|
|
|
struct connection *c = o->userdata;
|
|
|
|
|
assert(o && c && chunk);
|
|
|
|
|
|
|
|
|
|
memblockq_push(c->output_memblockq, chunk, 0);
|
2004-06-16 00:05:30 +00:00
|
|
|
|
|
|
|
|
if (do_write(c) < 0)
|
2004-07-03 00:19:17 +00:00
|
|
|
connection_free(c);
|
2004-06-15 17:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void source_output_kill_cb(struct source_output *o) {
|
|
|
|
|
assert(o && o->userdata);
|
2004-07-03 00:19:17 +00:00
|
|
|
connection_free((struct connection *) o->userdata);
|
2004-06-15 17:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*** client callbacks ***/
|
|
|
|
|
|
|
|
|
|
static void client_kill_cb(struct client *c) {
|
|
|
|
|
assert(c && c->userdata);
|
2004-07-03 00:19:17 +00:00
|
|
|
connection_free((struct connection *) c->userdata);
|
2004-06-15 17:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*** iochannel callbacks ***/
|
|
|
|
|
|
2004-06-15 00:29:01 +00:00
|
|
|
static void io_callback(struct iochannel*io, void *userdata) {
|
|
|
|
|
struct connection *c = userdata;
|
|
|
|
|
assert(io && c && c->io == io);
|
|
|
|
|
|
|
|
|
|
if (do_read(c) < 0 || do_write(c) < 0)
|
2004-07-03 00:19:17 +00:00
|
|
|
connection_free(c);
|
2004-06-15 00:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
/*** socket_server callbacks */
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
static void on_connection(struct socket_server*s, struct iochannel *io, void *userdata) {
|
|
|
|
|
struct protocol_simple *p = userdata;
|
|
|
|
|
struct connection *c = NULL;
|
2004-06-23 23:17:30 +00:00
|
|
|
char cname[256];
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(s && io && p);
|
|
|
|
|
|
|
|
|
|
c = malloc(sizeof(struct connection));
|
|
|
|
|
assert(c);
|
|
|
|
|
c->io = io;
|
2004-06-15 17:05:03 +00:00
|
|
|
c->sink_input = NULL;
|
|
|
|
|
c->source_output = NULL;
|
|
|
|
|
c->input_memblockq = c->output_memblockq = NULL;
|
2004-06-08 23:54:24 +00:00
|
|
|
c->protocol = p;
|
2004-06-14 18:38:50 +00:00
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
iochannel_peer_to_string(io, cname, sizeof(cname));
|
|
|
|
|
c->client = client_new(p->core, "SIMPLE", cname);
|
2004-06-14 18:38:50 +00:00
|
|
|
assert(c->client);
|
2004-06-15 17:05:03 +00:00
|
|
|
c->client->kill = client_kill_cb;
|
|
|
|
|
c->client->userdata = c;
|
2004-06-14 18:38:50 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
if (p->mode & PROTOCOL_SIMPLE_RECORD) {
|
|
|
|
|
struct source *source;
|
2004-06-15 17:05:03 +00:00
|
|
|
size_t l;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-06-18 00:22:37 +00:00
|
|
|
if (!(source = source_get_default(p->core))) {
|
2004-06-08 23:54:24 +00:00
|
|
|
fprintf(stderr, "Failed to get default source.\n");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-03 00:19:17 +00:00
|
|
|
c->source_output = source_output_new(source, c->client->name, &p->sample_spec);
|
2004-06-15 17:05:03 +00:00
|
|
|
assert(c->source_output);
|
|
|
|
|
c->source_output->push = source_output_push_cb;
|
|
|
|
|
c->source_output->kill = source_output_kill_cb;
|
|
|
|
|
c->source_output->userdata = c;
|
|
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
l = 5*pa_bytes_per_second(&DEFAULT_SAMPLE_SPEC); /* 5s */
|
2004-07-03 00:19:17 +00:00
|
|
|
c->output_memblockq = memblockq_new(l, pa_sample_size(&p->sample_spec), l/2);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (p->mode & PROTOCOL_SIMPLE_PLAYBACK) {
|
|
|
|
|
struct sink *sink;
|
2004-06-15 17:05:03 +00:00
|
|
|
size_t l;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-06-18 00:22:37 +00:00
|
|
|
if (!(sink = sink_get_default(p->core))) {
|
2004-06-08 23:54:24 +00:00
|
|
|
fprintf(stderr, "Failed to get default sink.\n");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-03 00:19:17 +00:00
|
|
|
c->sink_input = sink_input_new(sink, c->client->name, &p->sample_spec);
|
2004-06-15 17:05:03 +00:00
|
|
|
assert(c->sink_input);
|
|
|
|
|
c->sink_input->peek = sink_input_peek_cb;
|
|
|
|
|
c->sink_input->drop = sink_input_drop_cb;
|
|
|
|
|
c->sink_input->kill = sink_input_kill_cb;
|
2004-06-18 17:12:50 +00:00
|
|
|
c->sink_input->get_latency = sink_input_get_latency_cb;
|
2004-06-15 17:05:03 +00:00
|
|
|
c->sink_input->userdata = c;
|
|
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
l = pa_bytes_per_second(&DEFAULT_SAMPLE_SPEC)/2; /* half a second */
|
2004-07-03 00:19:17 +00:00
|
|
|
c->input_memblockq = memblockq_new(l, pa_sample_size(&p->sample_spec), l/2);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iochannel_set_callback(c->io, io_callback, c);
|
|
|
|
|
idxset_put(p->connections, c, NULL);
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
fail:
|
2004-07-03 00:19:17 +00:00
|
|
|
if (c)
|
|
|
|
|
connection_free(c);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct protocol_simple* protocol_simple_new(struct core *core, struct socket_server *server, enum protocol_simple_mode mode) {
|
|
|
|
|
struct protocol_simple* p;
|
|
|
|
|
assert(core && server && mode <= PROTOCOL_SIMPLE_DUPLEX && mode > 0);
|
|
|
|
|
|
|
|
|
|
p = malloc(sizeof(struct protocol_simple));
|
|
|
|
|
assert(p);
|
|
|
|
|
p->core = core;
|
|
|
|
|
p->server = server;
|
|
|
|
|
p->connections = idxset_new(NULL, NULL);
|
|
|
|
|
p->mode = mode;
|
2004-07-03 00:19:17 +00:00
|
|
|
p->sample_spec = DEFAULT_SAMPLE_SPEC;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
socket_server_set_callback(p->server, on_connection, p);
|
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void protocol_simple_free(struct protocol_simple *p) {
|
2004-07-03 00:19:17 +00:00
|
|
|
struct connection *c;
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(p);
|
|
|
|
|
|
2004-07-03 00:19:17 +00:00
|
|
|
while((c = idxset_first(p->connections, NULL)))
|
|
|
|
|
connection_free(c);
|
|
|
|
|
|
|
|
|
|
idxset_free(p->connections, NULL, NULL);
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
socket_server_free(p->server);
|
|
|
|
|
free(p);
|
|
|
|
|
}
|
2004-07-03 00:19:17 +00:00
|
|
|
|