]> code.delx.au - pulseaudio/blob - src/protocol-simple.c
initial commit
[pulseaudio] / src / protocol-simple.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <string.h>
7
8 #include "inputstream.h"
9 #include "outputstream.h"
10 #include "protocol-simple.h"
11 #include "client.h"
12
13 struct connection {
14 struct protocol_simple *protocol;
15 struct iochannel *io;
16 struct input_stream *istream;
17 struct output_stream *ostream;
18 struct client *client;
19 };
20
21 struct protocol_simple {
22 struct core *core;
23 struct socket_server*server;
24 struct idxset *connections;
25 enum protocol_simple_mode mode;
26 };
27
28 #define BUFSIZE PIPE_BUF
29
30 static void free_connection(void *data, void *userdata) {
31 struct connection *c = data;
32 assert(data);
33
34 if (c->istream)
35 input_stream_free(c->istream);
36 if (c->ostream)
37 output_stream_free(c->ostream);
38
39 client_free(c->client);
40
41 iochannel_free(c->io);
42 free(c);
43 }
44
45 static void io_callback(struct iochannel*io, void *userdata) {
46 struct connection *c = userdata;
47 assert(io && c);
48
49 if (c->istream && iochannel_is_readable(io)) {
50 struct memchunk chunk;
51 ssize_t r;
52
53 chunk.memblock = memblock_new(BUFSIZE);
54 assert(chunk.memblock);
55
56 if ((r = iochannel_read(io, chunk.memblock->data, BUFSIZE)) <= 0) {
57 fprintf(stderr, "read(): %s\n", r == 0 ? "EOF" : strerror(errno));
58 memblock_unref(chunk.memblock);
59 goto fail;
60 }
61
62 chunk.memblock->length = r;
63 chunk.length = r;
64 chunk.index = 0;
65
66 memblockq_push(c->istream->memblockq, &chunk, 0);
67 input_stream_notify(c->istream);
68 memblock_unref(chunk.memblock);
69 }
70
71 if (c->ostream && iochannel_is_writable(io)) {
72 struct memchunk chunk;
73 ssize_t r;
74
75 memblockq_peek(c->ostream->memblockq, &chunk);
76 assert(chunk.memblock && chunk.length);
77
78 if ((r = iochannel_write(io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
79 fprintf(stderr, "write(): %s\n", strerror(errno));
80 memblock_unref(chunk.memblock);
81 goto fail;
82 }
83
84 memblockq_drop(c->ostream->memblockq, r);
85 memblock_unref(chunk.memblock);
86 }
87
88 return;
89
90 fail:
91 idxset_remove_by_data(c->protocol->connections, c, NULL);
92 free_connection(c, NULL);
93 }
94
95 static void on_connection(struct socket_server*s, struct iochannel *io, void *userdata) {
96 struct protocol_simple *p = userdata;
97 struct connection *c = NULL;
98 assert(s && io && p);
99
100 c = malloc(sizeof(struct connection));
101 assert(c);
102 c->io = io;
103 c->istream = NULL;
104 c->ostream = NULL;
105 c->protocol = p;
106
107 if (p->mode & PROTOCOL_SIMPLE_RECORD) {
108 struct source *source;
109
110 if (!(source = core_get_default_source(p->core))) {
111 fprintf(stderr, "Failed to get default source.\n");
112 goto fail;
113 }
114
115 c->ostream = output_stream_new(source, &DEFAULT_SAMPLE_SPEC, c->client->name);
116 assert(c->ostream);
117 }
118
119 if (p->mode & PROTOCOL_SIMPLE_PLAYBACK) {
120 struct sink *sink;
121
122 if (!(sink = core_get_default_sink(p->core))) {
123 fprintf(stderr, "Failed to get default sink.\n");
124 goto fail;
125 }
126
127 c->istream = input_stream_new(sink, &DEFAULT_SAMPLE_SPEC, c->client->name);
128 assert(c->istream);
129 }
130
131 c->client = client_new(p->core, "SIMPLE", "Client");
132 assert(c->client);
133
134 iochannel_set_callback(c->io, io_callback, c);
135 idxset_put(p->connections, c, NULL);
136 return;
137
138 fail:
139 if (c) {
140 if (c->istream)
141 input_stream_free(c->istream);
142 if (c->ostream)
143 output_stream_free(c->ostream);
144
145 iochannel_free(c->io);
146 free(c);
147 }
148 }
149
150 struct protocol_simple* protocol_simple_new(struct core *core, struct socket_server *server, enum protocol_simple_mode mode) {
151 struct protocol_simple* p;
152 assert(core && server && mode <= PROTOCOL_SIMPLE_DUPLEX && mode > 0);
153
154 p = malloc(sizeof(struct protocol_simple));
155 assert(p);
156 p->core = core;
157 p->server = server;
158 p->connections = idxset_new(NULL, NULL);
159 p->mode = mode;
160
161 socket_server_set_callback(p->server, on_connection, p);
162
163 return p;
164 }
165
166
167 void protocol_simple_free(struct protocol_simple *p) {
168 assert(p);
169
170 idxset_free(p->connections, free_connection, NULL);
171 socket_server_free(p->server);
172 free(p);
173 }