]> code.delx.au - pulseaudio/blob - src/protocol-simple.c
oss output works
[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 destroy_connection(struct connection *c) {
46 assert(c && c->protocol);
47 idxset_remove_by_data(c->protocol->connections, c, NULL);
48 free_connection(c, NULL);
49 }
50
51 static void istream_kill_cb(struct input_stream *i, void *userdata) {
52 struct connection *c = userdata;
53 assert(i && c);
54 destroy_connection(c);
55 }
56
57 static void ostream_kill_cb(struct output_stream *o, void *userdata) {
58 struct connection *c = userdata;
59 assert(o && c);
60 destroy_connection(c);
61 }
62
63 static void client_kill_cb(struct client *client, void*userdata) {
64 struct connection *c= userdata;
65 assert(client && c);
66 destroy_connection(c);
67 }
68
69 static int do_read(struct connection *c) {
70 struct memchunk chunk;
71 ssize_t r;
72
73 if (!iochannel_is_readable(c->io))
74 return 0;
75
76 if (!c->istream || !memblockq_is_writable(c->istream->memblockq, BUFSIZE))
77 return 0;
78
79 chunk.memblock = memblock_new(BUFSIZE);
80 assert(chunk.memblock);
81
82 if ((r = iochannel_read(c->io, chunk.memblock->data, BUFSIZE)) <= 0) {
83 fprintf(stderr, "read(): %s\n", r == 0 ? "EOF" : strerror(errno));
84 memblock_unref(chunk.memblock);
85 return -1;
86 }
87
88 chunk.memblock->length = r;
89 chunk.length = r;
90 chunk.index = 0;
91
92 memblockq_push(c->istream->memblockq, &chunk, 0);
93 input_stream_notify_sink(c->istream);
94 memblock_unref(chunk.memblock);
95 return 0;
96 }
97
98 static int do_write(struct connection *c) {
99 struct memchunk chunk;
100 ssize_t r;
101
102 if (!iochannel_is_writable(c->io))
103 return 0;
104
105 if (!c->ostream)
106 return 0;
107
108 memblockq_peek(c->ostream->memblockq, &chunk);
109 assert(chunk.memblock && chunk.length);
110
111 if ((r = iochannel_write(c->io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
112 fprintf(stderr, "write(): %s\n", strerror(errno));
113 memblock_unref(chunk.memblock);
114 return -1;
115 }
116
117 memblockq_drop(c->ostream->memblockq, r);
118 memblock_unref(chunk.memblock);
119 return 0;
120 }
121
122 static void io_callback(struct iochannel*io, void *userdata) {
123 struct connection *c = userdata;
124 assert(io && c && c->io == io);
125
126 if (do_read(c) < 0 || do_write(c) < 0)
127 destroy_connection(c);
128 }
129
130 static void istream_notify_cb(struct input_stream *i, void *userdata) {
131 struct connection*c = userdata;
132 assert(i && c && c->istream == i);
133
134 if (do_read(c) < 0)
135 destroy_connection(c);
136 }
137
138 static void on_connection(struct socket_server*s, struct iochannel *io, void *userdata) {
139 struct protocol_simple *p = userdata;
140 struct connection *c = NULL;
141 assert(s && io && p);
142
143 c = malloc(sizeof(struct connection));
144 assert(c);
145 c->io = io;
146 c->istream = NULL;
147 c->ostream = NULL;
148 c->protocol = p;
149
150 c->client = client_new(p->core, "SIMPLE", "Client");
151 assert(c->client);
152 client_set_kill_callback(c->client, client_kill_cb, c);
153
154 if (p->mode & PROTOCOL_SIMPLE_RECORD) {
155 struct source *source;
156
157 if (!(source = core_get_default_source(p->core))) {
158 fprintf(stderr, "Failed to get default source.\n");
159 goto fail;
160 }
161
162 c->ostream = output_stream_new(source, &DEFAULT_SAMPLE_SPEC, c->client->name);
163 assert(c->ostream);
164 output_stream_set_kill_callback(c->ostream, ostream_kill_cb, c);
165 }
166
167 if (p->mode & PROTOCOL_SIMPLE_PLAYBACK) {
168 struct sink *sink;
169
170 if (!(sink = core_get_default_sink(p->core))) {
171 fprintf(stderr, "Failed to get default sink.\n");
172 goto fail;
173 }
174
175 c->istream = input_stream_new(sink, &DEFAULT_SAMPLE_SPEC, c->client->name);
176 assert(c->istream);
177 input_stream_set_kill_callback(c->istream, istream_kill_cb, c);
178 input_stream_set_notify_callback(c->istream, istream_notify_cb, c);
179 }
180
181
182 iochannel_set_callback(c->io, io_callback, c);
183 idxset_put(p->connections, c, NULL);
184 return;
185
186 fail:
187 if (c) {
188 if (c->client)
189 client_free(c->client);
190 if (c->istream)
191 input_stream_free(c->istream);
192 if (c->ostream)
193 output_stream_free(c->ostream);
194
195 iochannel_free(c->io);
196 free(c);
197 }
198 }
199
200 struct protocol_simple* protocol_simple_new(struct core *core, struct socket_server *server, enum protocol_simple_mode mode) {
201 struct protocol_simple* p;
202 assert(core && server && mode <= PROTOCOL_SIMPLE_DUPLEX && mode > 0);
203
204 p = malloc(sizeof(struct protocol_simple));
205 assert(p);
206 p->core = core;
207 p->server = server;
208 p->connections = idxset_new(NULL, NULL);
209 p->mode = mode;
210
211 socket_server_set_callback(p->server, on_connection, p);
212
213 return p;
214 }
215
216
217 void protocol_simple_free(struct protocol_simple *p) {
218 assert(p);
219
220 idxset_free(p->connections, free_connection, NULL);
221 socket_server_free(p->server);
222 free(p);
223 }