]> code.delx.au - pulseaudio/blob - src/protocol-simple.c
got mmap oss output working
[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 "sinkinput.h"
9 #include "sourceoutput.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 sink_input *sink_input;
17 struct source_output *source_output;
18 struct client *client;
19 struct memblockq *input_memblockq, *output_memblockq;
20 };
21
22 struct protocol_simple {
23 struct core *core;
24 struct socket_server*server;
25 struct idxset *connections;
26 enum protocol_simple_mode mode;
27 };
28
29 #define BUFSIZE PIPE_BUF
30
31 static void free_connection(void *data, void *userdata) {
32 struct connection *c = data;
33 assert(data);
34
35 if (c->sink_input)
36 sink_input_free(c->sink_input);
37 if (c->source_output)
38 source_output_free(c->source_output);
39 if (c->client)
40 client_free(c->client);
41 if (c->io)
42 iochannel_free(c->io);
43 if (c->input_memblockq)
44 memblockq_free(c->input_memblockq);
45 if (c->output_memblockq)
46 memblockq_free(c->output_memblockq);
47 free(c);
48 }
49
50 static void destroy_connection(struct connection *c) {
51 assert(c && c->protocol);
52 idxset_remove_by_data(c->protocol->connections, c, NULL);
53 free_connection(c, NULL);
54 }
55
56 static int do_read(struct connection *c) {
57 struct memchunk chunk;
58 ssize_t r;
59 uint32_t u1, u2;
60
61 if (!iochannel_is_readable(c->io))
62 return 0;
63
64 if (!c->sink_input || !memblockq_is_writable(c->input_memblockq, BUFSIZE))
65 return 0;
66
67 chunk.memblock = memblock_new(BUFSIZE);
68 assert(chunk.memblock);
69
70 if ((r = iochannel_read(c->io, chunk.memblock->data, BUFSIZE)) <= 0) {
71 fprintf(stderr, "read(): %s\n", r == 0 ? "EOF" : strerror(errno));
72 memblock_unref(chunk.memblock);
73 return -1;
74 }
75
76 chunk.memblock->length = r;
77 chunk.length = r;
78 chunk.index = 0;
79
80 assert(c->input_memblockq);
81 memblockq_push(c->input_memblockq, &chunk, 0);
82 memblock_unref(chunk.memblock);
83 sink_notify(c->sink_input->sink);
84
85
86 u1 = memblockq_get_latency(c->input_memblockq);
87 u2 = sink_get_latency(c->sink_input->sink);
88
89 fprintf(stderr, "latency: %u+%u=%u\r", u1, u2, u1+u2);
90
91 return 0;
92 }
93
94 static int do_write(struct connection *c) {
95 struct memchunk chunk;
96 ssize_t r;
97
98 if (!iochannel_is_writable(c->io))
99 return 0;
100
101 if (!c->source_output)
102 return 0;
103
104 assert(c->output_memblockq);
105 if (memblockq_peek(c->output_memblockq, &chunk) < 0)
106 return 0;
107
108 assert(chunk.memblock && chunk.length);
109
110 if ((r = iochannel_write(c->io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
111 fprintf(stderr, "write(): %s\n", strerror(errno));
112 memblock_unref(chunk.memblock);
113 return -1;
114 }
115
116 memblockq_drop(c->output_memblockq, r);
117 memblock_unref(chunk.memblock);
118 return 0;
119 }
120
121 /*** sink_input callbacks ***/
122
123 static int sink_input_peek_cb(struct sink_input *i, struct memchunk *chunk, uint8_t *volume) {
124 struct connection*c = i->userdata;
125 assert(i && c && chunk && volume);
126
127 if (memblockq_peek(c->input_memblockq, chunk) < 0)
128 return -1;
129
130 *volume = 0xFF;
131 return 0;
132 }
133
134 static void sink_input_drop_cb(struct sink_input *i, size_t length) {
135 struct connection*c = i->userdata;
136 assert(i && c && length);
137
138 memblockq_drop(c->input_memblockq, length);
139
140 if (do_read(c) < 0)
141 destroy_connection(c);
142 }
143
144 static void sink_input_kill_cb(struct sink_input *i) {
145 assert(i && i->userdata);
146 destroy_connection((struct connection *) i->userdata);
147 }
148
149 /*** source_output callbacks ***/
150
151 static void source_output_push_cb(struct source_output *o, struct memchunk *chunk) {
152 struct connection *c = o->userdata;
153 assert(o && c && chunk);
154
155 memblockq_push(c->output_memblockq, chunk, 0);
156
157 if (do_write(c) < 0)
158 destroy_connection(c);
159 }
160
161 static void source_output_kill_cb(struct source_output *o) {
162 assert(o && o->userdata);
163 destroy_connection((struct connection *) o->userdata);
164 }
165
166
167 /*** client callbacks ***/
168
169 static void client_kill_cb(struct client *c) {
170 assert(c && c->userdata);
171 destroy_connection((struct connection *) c->userdata);
172 }
173
174 /*** iochannel callbacks ***/
175
176 static void io_callback(struct iochannel*io, void *userdata) {
177 struct connection *c = userdata;
178 assert(io && c && c->io == io);
179
180 if (do_read(c) < 0 || do_write(c) < 0)
181 destroy_connection(c);
182 }
183
184 /*** socket_server callbacks */
185
186 static void on_connection(struct socket_server*s, struct iochannel *io, void *userdata) {
187 struct protocol_simple *p = userdata;
188 struct connection *c = NULL;
189 assert(s && io && p);
190
191 c = malloc(sizeof(struct connection));
192 assert(c);
193 c->io = io;
194 c->sink_input = NULL;
195 c->source_output = NULL;
196 c->input_memblockq = c->output_memblockq = NULL;
197 c->protocol = p;
198
199 c->client = client_new(p->core, "SIMPLE", "Client");
200 assert(c->client);
201 c->client->kill = client_kill_cb;
202 c->client->userdata = c;
203
204 if (p->mode & PROTOCOL_SIMPLE_RECORD) {
205 struct source *source;
206 size_t l;
207
208 if (!(source = core_get_default_source(p->core))) {
209 fprintf(stderr, "Failed to get default source.\n");
210 goto fail;
211 }
212
213 c->source_output = source_output_new(source, &DEFAULT_SAMPLE_SPEC, c->client->name);
214 assert(c->source_output);
215 c->source_output->push = source_output_push_cb;
216 c->source_output->kill = source_output_kill_cb;
217 c->source_output->userdata = c;
218
219 l = 5*bytes_per_second(&DEFAULT_SAMPLE_SPEC); /* 5s */
220 c->output_memblockq = memblockq_new(l, sample_size(&DEFAULT_SAMPLE_SPEC), l/2);
221 }
222
223 if (p->mode & PROTOCOL_SIMPLE_PLAYBACK) {
224 struct sink *sink;
225 size_t l;
226
227 if (!(sink = core_get_default_sink(p->core))) {
228 fprintf(stderr, "Failed to get default sink.\n");
229 goto fail;
230 }
231
232 c->sink_input = sink_input_new(sink, &DEFAULT_SAMPLE_SPEC, c->client->name);
233 assert(c->sink_input);
234 c->sink_input->peek = sink_input_peek_cb;
235 c->sink_input->drop = sink_input_drop_cb;
236 c->sink_input->kill = sink_input_kill_cb;
237 c->sink_input->userdata = c;
238
239 l = bytes_per_second(&DEFAULT_SAMPLE_SPEC)/2; /* half a second */
240 c->input_memblockq = memblockq_new(l, sample_size(&DEFAULT_SAMPLE_SPEC), l/2);
241 }
242
243
244 iochannel_set_callback(c->io, io_callback, c);
245 idxset_put(p->connections, c, NULL);
246 return;
247
248 fail:
249 if (c) {
250 free_connection(c, NULL);
251 iochannel_free(c->io);
252 free(c);
253 }
254 }
255
256 struct protocol_simple* protocol_simple_new(struct core *core, struct socket_server *server, enum protocol_simple_mode mode) {
257 struct protocol_simple* p;
258 assert(core && server && mode <= PROTOCOL_SIMPLE_DUPLEX && mode > 0);
259
260 p = malloc(sizeof(struct protocol_simple));
261 assert(p);
262 p->core = core;
263 p->server = server;
264 p->connections = idxset_new(NULL, NULL);
265 p->mode = mode;
266
267 socket_server_set_callback(p->server, on_connection, p);
268
269 return p;
270 }
271
272
273 void protocol_simple_free(struct protocol_simple *p) {
274 assert(p);
275
276 idxset_free(p->connections, free_connection, NULL);
277 socket_server_free(p->server);
278 free(p);
279 }