]> code.delx.au - pulseaudio/blob - src/pstream.c
implement recording in native API
[pulseaudio] / src / pstream.c
1 #include <stdlib.h>
2 #include <assert.h>
3 #include <netinet/in.h>
4
5 #include "pstream.h"
6 #include "queue.h"
7
8 enum pa_pstream_descriptor_index {
9 PA_PSTREAM_DESCRIPTOR_LENGTH,
10 PA_PSTREAM_DESCRIPTOR_CHANNEL,
11 PA_PSTREAM_DESCRIPTOR_DELTA,
12 PA_PSTREAM_DESCRIPTOR_MAX
13 };
14
15 typedef uint32_t pa_pstream_descriptor[PA_PSTREAM_DESCRIPTOR_MAX];
16
17 #define PA_PSTREAM_DESCRIPTOR_SIZE (PA_PSTREAM_DESCRIPTOR_MAX*sizeof(uint32_t))
18 #define FRAME_SIZE_MAX (1024*64)
19
20 struct item_info {
21 enum { PA_PSTREAM_ITEM_PACKET, PA_PSTREAM_ITEM_MEMBLOCK } type;
22
23 /* memblock info */
24 struct pa_memchunk chunk;
25 uint32_t channel;
26 int32_t delta;
27
28 /* packet info */
29 struct pa_packet *packet;
30 };
31
32 struct pa_pstream {
33 struct pa_mainloop_api *mainloop;
34 struct mainloop_source *mainloop_source;
35 struct pa_iochannel *io;
36 struct pa_queue *send_queue;
37
38 int in_use, shall_free;
39
40 int dead;
41 void (*die_callback) (struct pa_pstream *p, void *userdata);
42 void *die_callback_userdata;
43
44 struct {
45 struct item_info* current;
46 pa_pstream_descriptor descriptor;
47 void *data;
48 size_t index;
49 } write;
50
51 struct {
52 struct pa_memblock *memblock;
53 struct pa_packet *packet;
54 pa_pstream_descriptor descriptor;
55 void *data;
56 size_t index;
57 } read;
58
59 void (*recieve_packet_callback) (struct pa_pstream *p, struct pa_packet *packet, void *userdata);
60 void *recieve_packet_callback_userdata;
61
62 void (*recieve_memblock_callback) (struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata);
63 void *recieve_memblock_callback_userdata;
64
65 void (*drain_callback)(struct pa_pstream *p, void *userdata);
66 void *drain_userdata;
67 };
68
69 static void do_write(struct pa_pstream *p);
70 static void do_read(struct pa_pstream *p);
71
72 static void do_something(struct pa_pstream *p) {
73 assert(p && !p->shall_free);
74 p->mainloop->enable_fixed(p->mainloop, p->mainloop_source, 0);
75
76 if (p->dead)
77 return;
78
79 if (pa_iochannel_is_hungup(p->io)) {
80 p->dead = 1;
81 if (p->die_callback)
82 p->die_callback(p, p->die_callback_userdata);
83
84 return;
85 }
86
87 if (pa_iochannel_is_writable(p->io)) {
88 p->in_use = 1;
89 do_write(p);
90 p->in_use = 0;
91
92 if (p->shall_free) {
93 pa_pstream_free(p);
94 return;
95 }
96 }
97
98 if (pa_iochannel_is_readable(p->io)) {
99 p->in_use = 1;
100 do_read(p);
101 p->in_use = 0;
102 if (p->shall_free) {
103 pa_pstream_free(p);
104 return;
105 }
106 }
107 }
108
109 static void io_callback(struct pa_iochannel*io, void *userdata) {
110 struct pa_pstream *p = userdata;
111 assert(p && p->io == io);
112 do_something(p);
113 }
114
115 static void fixed_callback(struct pa_mainloop_api *m, void *id, void*userdata) {
116 struct pa_pstream *p = userdata;
117 assert(p && p->mainloop_source == id && p->mainloop == m);
118 do_something(p);
119 }
120
121 struct pa_pstream *pa_pstream_new(struct pa_mainloop_api *m, struct pa_iochannel *io) {
122 struct pa_pstream *p;
123 assert(io);
124
125 p = malloc(sizeof(struct pa_pstream));
126 assert(p);
127
128 p->io = io;
129 pa_iochannel_set_callback(io, io_callback, p);
130
131 p->dead = 0;
132 p->die_callback = NULL;
133 p->die_callback_userdata = NULL;
134
135 p->mainloop = m;
136 p->mainloop_source = m->source_fixed(m, fixed_callback, p);
137 m->enable_fixed(m, p->mainloop_source, 0);
138
139 p->send_queue = pa_queue_new();
140 assert(p->send_queue);
141
142 p->write.current = NULL;
143 p->write.index = 0;
144
145 p->read.memblock = NULL;
146 p->read.packet = NULL;
147 p->read.index = 0;
148
149 p->recieve_packet_callback = NULL;
150 p->recieve_packet_callback_userdata = NULL;
151
152 p->recieve_memblock_callback = NULL;
153 p->recieve_memblock_callback_userdata = NULL;
154
155 p->drain_callback = NULL;
156 p->drain_userdata = NULL;
157
158 p->in_use = p->shall_free = 0;
159
160 return p;
161 }
162
163 static void item_free(void *item, void *p) {
164 struct item_info *i = item;
165 assert(i);
166
167 if (i->type == PA_PSTREAM_ITEM_MEMBLOCK) {
168 assert(i->chunk.memblock);
169 pa_memblock_unref(i->chunk.memblock);
170 } else {
171 assert(i->type == PA_PSTREAM_ITEM_PACKET);
172 assert(i->packet);
173 pa_packet_unref(i->packet);
174 }
175
176 free(i);
177 }
178
179 void pa_pstream_free(struct pa_pstream *p) {
180 assert(p);
181
182 if (p->in_use) {
183 /* If this pstream object is used by someone else on the call stack, we have to postpone the freeing */
184 p->dead = p->shall_free = 1;
185 return;
186 }
187
188 pa_iochannel_free(p->io);
189 pa_queue_free(p->send_queue, item_free, NULL);
190
191 if (p->write.current)
192 item_free(p->write.current, NULL);
193
194 if (p->read.memblock)
195 pa_memblock_unref(p->read.memblock);
196
197 if (p->read.packet)
198 pa_packet_unref(p->read.packet);
199
200 p->mainloop->cancel_fixed(p->mainloop, p->mainloop_source);
201 free(p);
202 }
203
204 void pa_pstream_send_packet(struct pa_pstream*p, struct pa_packet *packet) {
205 struct item_info *i;
206 assert(p && packet);
207
208 i = malloc(sizeof(struct item_info));
209 assert(i);
210 i->type = PA_PSTREAM_ITEM_PACKET;
211 i->packet = pa_packet_ref(packet);
212
213 pa_queue_push(p->send_queue, i);
214 p->mainloop->enable_fixed(p->mainloop, p->mainloop_source, 1);
215 }
216
217 void pa_pstream_send_memblock(struct pa_pstream*p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk) {
218 struct item_info *i;
219 assert(p && channel != (uint32_t) -1 && chunk);
220
221 i = malloc(sizeof(struct item_info));
222 assert(i);
223 i->type = PA_PSTREAM_ITEM_MEMBLOCK;
224 i->chunk = *chunk;
225 i->channel = channel;
226 i->delta = delta;
227
228 pa_memblock_ref(i->chunk.memblock);
229
230 pa_queue_push(p->send_queue, i);
231 p->mainloop->enable_fixed(p->mainloop, p->mainloop_source, 1);
232 }
233
234 void pa_pstream_set_recieve_packet_callback(struct pa_pstream *p, void (*callback) (struct pa_pstream *p, struct pa_packet *packet, void *userdata), void *userdata) {
235 assert(p && callback);
236
237 p->recieve_packet_callback = callback;
238 p->recieve_packet_callback_userdata = userdata;
239 }
240
241 void pa_pstream_set_recieve_memblock_callback(struct pa_pstream *p, void (*callback) (struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata), void *userdata) {
242 assert(p && callback);
243
244 p->recieve_memblock_callback = callback;
245 p->recieve_memblock_callback_userdata = userdata;
246 }
247
248 static void prepare_next_write_item(struct pa_pstream *p) {
249 assert(p);
250
251 if (!(p->write.current = pa_queue_pop(p->send_queue)))
252 return;
253
254 p->write.index = 0;
255
256 if (p->write.current->type == PA_PSTREAM_ITEM_PACKET) {
257 assert(p->write.current->packet);
258 p->write.data = p->write.current->packet->data;
259 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH] = htonl(p->write.current->packet->length);
260 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL] = htonl((uint32_t) -1);
261 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_DELTA] = 0;
262 } else {
263 assert(p->write.current->type == PA_PSTREAM_ITEM_MEMBLOCK && p->write.current->chunk.memblock);
264 p->write.data = p->write.current->chunk.memblock->data + p->write.current->chunk.index;
265 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH] = htonl(p->write.current->chunk.length);
266 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL] = htonl(p->write.current->channel);
267 p->write.descriptor[PA_PSTREAM_DESCRIPTOR_DELTA] = htonl(p->write.current->delta);
268 }
269 }
270
271 static void do_write(struct pa_pstream *p) {
272 void *d;
273 size_t l;
274 ssize_t r;
275 assert(p);
276
277 if (!p->write.current)
278 prepare_next_write_item(p);
279
280 if (!p->write.current)
281 return;
282
283 assert(p->write.data);
284
285 if (p->write.index < PA_PSTREAM_DESCRIPTOR_SIZE) {
286 d = (void*) p->write.descriptor + p->write.index;
287 l = PA_PSTREAM_DESCRIPTOR_SIZE - p->write.index;
288 } else {
289 d = (void*) p->write.data + p->write.index - PA_PSTREAM_DESCRIPTOR_SIZE;
290 l = ntohl(p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) - (p->write.index - PA_PSTREAM_DESCRIPTOR_SIZE);
291 }
292
293 if ((r = pa_iochannel_write(p->io, d, l)) < 0)
294 goto die;
295
296 p->write.index += r;
297
298 if (p->write.index >= PA_PSTREAM_DESCRIPTOR_SIZE+ntohl(p->write.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH])) {
299 assert(p->write.current);
300 item_free(p->write.current, (void *) 1);
301 p->write.current = NULL;
302
303 if (p->drain_callback && !pa_pstream_is_pending(p))
304 p->drain_callback(p, p->drain_userdata);
305 }
306
307 return;
308
309 die:
310 p->dead = 1;
311 if (p->die_callback)
312 p->die_callback(p, p->die_callback_userdata);
313 }
314
315 static void do_read(struct pa_pstream *p) {
316 void *d;
317 size_t l;
318 ssize_t r;
319 assert(p);
320
321 if (p->read.index < PA_PSTREAM_DESCRIPTOR_SIZE) {
322 d = (void*) p->read.descriptor + p->read.index;
323 l = PA_PSTREAM_DESCRIPTOR_SIZE - p->read.index;
324 } else {
325 assert(p->read.data);
326 d = (void*) p->read.data + p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE;
327 l = ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) - (p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE);
328 }
329
330 if ((r = pa_iochannel_read(p->io, d, l)) <= 0)
331 goto die;
332
333 p->read.index += r;
334
335 if (p->read.index == PA_PSTREAM_DESCRIPTOR_SIZE) {
336 /* Reading of frame descriptor complete */
337
338 /* Frame size too large */
339 if (ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) > FRAME_SIZE_MAX)
340 goto die;
341
342 assert(!p->read.packet && !p->read.memblock);
343
344 if (ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL]) == (uint32_t) -1) {
345 /* Frame is a packet frame */
346 p->read.packet = pa_packet_new(ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]));
347 assert(p->read.packet);
348 p->read.data = p->read.packet->data;
349 } else {
350 /* Frame is a memblock frame */
351 p->read.memblock = pa_memblock_new(ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]));
352 assert(p->read.memblock);
353 p->read.data = p->read.memblock->data;
354 }
355
356 } else if (p->read.index > PA_PSTREAM_DESCRIPTOR_SIZE) {
357 /* Frame payload available */
358
359 if (p->read.memblock && p->recieve_memblock_callback) { /* Is this memblock data? Than pass it to the user */
360 size_t l;
361
362 l = (p->read.index - r) < PA_PSTREAM_DESCRIPTOR_SIZE ? p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE : (size_t) r;
363
364 if (l > 0) {
365 struct pa_memchunk chunk;
366
367 chunk.memblock = p->read.memblock;
368 chunk.index = p->read.index - PA_PSTREAM_DESCRIPTOR_SIZE - l;
369 chunk.length = l;
370
371 if (p->recieve_memblock_callback)
372 p->recieve_memblock_callback(
373 p,
374 ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_CHANNEL]),
375 (int32_t) ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_DELTA]),
376 &chunk,
377 p->recieve_memblock_callback_userdata);
378 }
379 }
380
381 /* Frame complete */
382 if (p->read.index >= ntohl(p->read.descriptor[PA_PSTREAM_DESCRIPTOR_LENGTH]) + PA_PSTREAM_DESCRIPTOR_SIZE) {
383 if (p->read.memblock) {
384 assert(!p->read.packet);
385
386 pa_memblock_unref(p->read.memblock);
387 p->read.memblock = NULL;
388 } else {
389 assert(p->read.packet);
390
391 if (p->recieve_packet_callback)
392 p->recieve_packet_callback(p, p->read.packet, p->recieve_packet_callback_userdata);
393
394 pa_packet_unref(p->read.packet);
395 p->read.packet = NULL;
396 }
397
398 p->read.index = 0;
399 }
400 }
401
402 return;
403
404 die:
405 p->dead = 1;
406 if (p->die_callback)
407 p->die_callback(p, p->die_callback_userdata);
408
409 }
410
411 void pa_pstream_set_die_callback(struct pa_pstream *p, void (*callback)(struct pa_pstream *p, void *userdata), void *userdata) {
412 assert(p && callback);
413 p->die_callback = callback;
414 p->die_callback_userdata = userdata;
415 }
416
417 int pa_pstream_is_pending(struct pa_pstream *p) {
418 assert(p);
419
420 if (p->dead)
421 return 0;
422
423 return p->write.current || !pa_queue_is_empty(p->send_queue);
424 }
425
426 void pa_pstream_set_drain_callback(struct pa_pstream *p, void (*cb)(struct pa_pstream *p, void *userdata), void *userdata) {
427 assert(p);
428
429 p->drain_callback = cb;
430 p->drain_userdata = userdata;
431 }
432