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 {
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_protocol_simple *protocol;
|
|
|
|
|
struct pa_iochannel *io;
|
|
|
|
|
struct pa_sink_input *sink_input;
|
|
|
|
|
struct pa_source_output *source_output;
|
|
|
|
|
struct pa_client *client;
|
|
|
|
|
struct pa_memblockq *input_memblockq, *output_memblockq;
|
2004-06-08 23:54:24 +00:00
|
|
|
};
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_protocol_simple {
|
|
|
|
|
struct pa_core *core;
|
|
|
|
|
struct pa_socket_server*server;
|
|
|
|
|
struct pa_idxset *connections;
|
|
|
|
|
enum pa_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
|
|
|
};
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
#define PLAYBACK_BUFFER_SECONDS (.5)
|
|
|
|
|
#define RECORD_BUFFER_SECONDS (5)
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 00:19:17 +00:00
|
|
|
static void connection_free(struct connection *c) {
|
|
|
|
|
assert(c);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
|
2004-07-03 00:19:17 +00:00
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
if (c->sink_input)
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_sink_input_free(c->sink_input);
|
2004-06-15 17:05:03 +00:00
|
|
|
if (c->source_output)
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_source_output_free(c->source_output);
|
2004-06-15 17:05:03 +00:00
|
|
|
if (c->client)
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_client_free(c->client);
|
2004-06-15 17:05:03 +00:00
|
|
|
if (c->io)
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_iochannel_free(c->io);
|
2004-06-15 17:05:03 +00:00
|
|
|
if (c->input_memblockq)
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_memblockq_free(c->input_memblockq);
|
2004-06-15 17:05:03 +00:00
|
|
|
if (c->output_memblockq)
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_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) {
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_memchunk chunk;
|
2004-06-15 00:29:01 +00:00
|
|
|
ssize_t r;
|
2004-07-07 00:22:46 +00:00
|
|
|
size_t l;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (!pa_iochannel_is_readable(c->io))
|
2004-06-15 00:29:01 +00:00
|
|
|
return 0;
|
|
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
if (!c->sink_input || !(l = pa_memblockq_missing(c->input_memblockq)))
|
2004-06-15 00:29:01 +00:00
|
|
|
return 0;
|
2004-07-07 00:22:46 +00:00
|
|
|
|
|
|
|
|
chunk.memblock = pa_memblock_new(l);
|
2004-06-15 00:29:01 +00:00
|
|
|
assert(chunk.memblock);
|
2004-06-15 15:18:33 +00:00
|
|
|
|
2004-07-07 00:22:46 +00:00
|
|
|
if ((r = pa_iochannel_read(c->io, chunk.memblock->data, l)) <= 0) {
|
2004-06-15 00:29:01 +00:00
|
|
|
fprintf(stderr, "read(): %s\n", r == 0 ? "EOF" : strerror(errno));
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_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 23:35:12 +00:00
|
|
|
pa_memblockq_push_align(c->input_memblockq, &chunk, 0);
|
|
|
|
|
pa_memblock_unref(chunk.memblock);
|
2004-06-20 01:12:13 +00:00
|
|
|
assert(c->sink_input);
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_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) {
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_memchunk chunk;
|
2004-06-15 00:29:01 +00:00
|
|
|
ssize_t r;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (!pa_iochannel_is_writable(c->io))
|
2004-06-15 00:29:01 +00:00
|
|
|
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-07-03 23:35:12 +00:00
|
|
|
if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
|
2004-06-16 00:05:30 +00:00
|
|
|
return 0;
|
|
|
|
|
|
2004-06-15 00:29:01 +00:00
|
|
|
assert(chunk.memblock && chunk.length);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if ((r = pa_iochannel_write(c->io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
|
2004-06-15 00:29:01 +00:00
|
|
|
fprintf(stderr, "write(): %s\n", strerror(errno));
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_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-07-03 23:35:12 +00:00
|
|
|
pa_memblockq_drop(c->output_memblockq, r);
|
|
|
|
|
pa_memblock_unref(chunk.memblock);
|
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
|
|
|
/*** sink_input callbacks ***/
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
|
2004-06-23 23:17:30 +00:00
|
|
|
struct connection*c;
|
|
|
|
|
assert(i && i->userdata && chunk);
|
|
|
|
|
c = i->userdata;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (pa_memblockq_peek(c->input_memblockq, chunk) < 0)
|
2004-06-15 17:05:03 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
static void sink_input_drop_cb(struct pa_sink_input *i, size_t length) {
|
2004-06-15 17:05:03 +00:00
|
|
|
struct connection*c = i->userdata;
|
|
|
|
|
assert(i && c && length);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_memblockq_drop(c->input_memblockq, length);
|
2004-06-15 17:05:03 +00:00
|
|
|
|
|
|
|
|
if (do_read(c) < 0)
|
2004-07-03 00:19:17 +00:00
|
|
|
connection_free(c);
|
2004-06-15 17:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
static void sink_input_kill_cb(struct pa_sink_input *i) {
|
2004-06-15 17:05:03 +00:00
|
|
|
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
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
|
2004-06-18 17:12:50 +00:00
|
|
|
struct connection*c = i->userdata;
|
|
|
|
|
assert(i && c);
|
2004-07-03 23:35:12 +00:00
|
|
|
return pa_samples_usec(pa_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 23:35:12 +00:00
|
|
|
static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
|
2004-06-15 17:05:03 +00:00
|
|
|
struct connection *c = o->userdata;
|
|
|
|
|
assert(o && c && chunk);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_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
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
static void source_output_kill_cb(struct pa_source_output *o) {
|
2004-06-15 17:05:03 +00:00
|
|
|
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 ***/
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
static void client_kill_cb(struct pa_client *c) {
|
2004-06-15 17:05:03 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
/*** pa_iochannel callbacks ***/
|
2004-06-15 17:05:03 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
static void io_callback(struct pa_iochannel*io, void *userdata) {
|
2004-06-15 00:29:01 +00:00
|
|
|
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
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
|
|
|
|
|
struct pa_protocol_simple *p = userdata;
|
2004-06-08 23:54:24 +00:00
|
|
|
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-07-03 23:35:12 +00:00
|
|
|
pa_iochannel_peer_to_string(io, cname, sizeof(cname));
|
|
|
|
|
c->client = pa_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-07-03 23:35:12 +00:00
|
|
|
if (p->mode & PA_PROTOCOL_SIMPLE_RECORD) {
|
|
|
|
|
struct pa_source *source;
|
2004-06-15 17:05:03 +00:00
|
|
|
size_t l;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (!(source = pa_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 23:35:12 +00:00
|
|
|
c->source_output = pa_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-07-07 00:22:46 +00:00
|
|
|
l = (size_t) (pa_bytes_per_second(&p->sample_spec)*RECORD_BUFFER_SECONDS);
|
|
|
|
|
c->output_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&p->sample_spec), l/2, 0);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (p->mode & PA_PROTOCOL_SIMPLE_PLAYBACK) {
|
|
|
|
|
struct pa_sink *sink;
|
2004-06-15 17:05:03 +00:00
|
|
|
size_t l;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (!(sink = pa_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 23:35:12 +00:00
|
|
|
c->sink_input = pa_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-07-07 00:22:46 +00:00
|
|
|
l = (size_t) (pa_bytes_per_second(&p->sample_spec)*PLAYBACK_BUFFER_SECONDS);
|
|
|
|
|
c->input_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&p->sample_spec), l/2, l/10);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_iochannel_set_callback(c->io, io_callback, c);
|
|
|
|
|
pa_idxset_put(p->connections, c, NULL);
|
2004-06-08 23:54:24 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
fail:
|
2004-07-03 00:19:17 +00:00
|
|
|
if (c)
|
|
|
|
|
connection_free(c);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_protocol_simple* pa_protocol_simple_new(struct pa_core *core, struct pa_socket_server *server, enum pa_protocol_simple_mode mode) {
|
|
|
|
|
struct pa_protocol_simple* p;
|
|
|
|
|
assert(core && server && mode <= PA_PROTOCOL_SIMPLE_DUPLEX && mode > 0);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
p = malloc(sizeof(struct pa_protocol_simple));
|
2004-06-08 23:54:24 +00:00
|
|
|
assert(p);
|
|
|
|
|
p->core = core;
|
|
|
|
|
p->server = server;
|
2004-07-03 23:35:12 +00:00
|
|
|
p->connections = pa_idxset_new(NULL, NULL);
|
2004-06-08 23:54:24 +00:00
|
|
|
p->mode = mode;
|
2004-07-03 23:35:12 +00:00
|
|
|
p->sample_spec = PA_DEFAULT_SAMPLE_SPEC;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_socket_server_set_callback(p->server, on_connection, p);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
void pa_protocol_simple_free(struct pa_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 23:35:12 +00:00
|
|
|
while((c = pa_idxset_first(p->connections, NULL)))
|
2004-07-03 00:19:17 +00:00
|
|
|
connection_free(c);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_idxset_free(p->connections, NULL, NULL);
|
2004-07-03 00:19:17 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_socket_server_free(p->server);
|
2004-06-08 23:54:24 +00:00
|
|
|
free(p);
|
|
|
|
|
}
|
2004-07-03 00:19:17 +00:00
|
|
|
|