]> code.delx.au - pulseaudio/blob - polyp/polyplib-stream.c
remove obnoxious assert from module-combine
[pulseaudio] / polyp / polyplib-stream.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <string.h>
28 #include <stdio.h>
29
30 #include "polyplib-internal.h"
31 #include "xmalloc.h"
32 #include "pstream-util.h"
33 #include "util.h"
34
35 struct pa_stream *pa_stream_new(struct pa_context *c, const char *name, const struct pa_sample_spec *ss) {
36 struct pa_stream *s;
37 assert(c && ss);
38
39 s = pa_xmalloc(sizeof(struct pa_stream));
40 s->ref = 1;
41 s->context = c;
42
43 s->read_callback = NULL;
44 s->read_userdata = NULL;
45 s->write_callback = NULL;
46 s->write_userdata = NULL;
47 s->state_callback = NULL;
48 s->state_userdata = NULL;
49
50 s->state = PA_STREAM_NODIRECTION;
51 s->name = pa_xstrdup(name);
52 s->sample_spec = *ss;
53 s->channel = 0;
54 s->channel_valid = 0;
55 s->device_index = PA_INVALID_INDEX;
56 s->requested_bytes = 0;
57 s->state = PA_STREAM_DISCONNECTED;
58 memset(&s->buffer_attr, 0, sizeof(s->buffer_attr));
59
60 PA_LLIST_PREPEND(struct pa_stream, c->streams, s);
61
62 return pa_stream_ref(s);
63 }
64
65 static void stream_free(struct pa_stream *s) {
66 assert(s);
67 pa_xfree(s->name);
68 pa_xfree(s);
69 }
70
71 void pa_stream_unref(struct pa_stream *s) {
72 assert(s && s->ref >= 1);
73
74 if (--(s->ref) == 0)
75 stream_free(s);
76 }
77
78 struct pa_stream* pa_stream_ref(struct pa_stream *s) {
79 assert(s && s->ref >= 1);
80 s->ref++;
81 return s;
82 }
83
84 enum pa_stream_state pa_stream_get_state(struct pa_stream *s) {
85 assert(s && s->ref >= 1);
86 return s->state;
87 }
88
89 struct pa_context* pa_stream_get_context(struct pa_stream *s) {
90 assert(s && s->ref >= 1);
91 return s->context;
92 }
93
94 uint32_t pa_stream_get_index(struct pa_stream *s) {
95 assert(s && s->ref >= 1);
96 return s->device_index;
97 }
98
99 void pa_stream_set_state(struct pa_stream *s, enum pa_stream_state st) {
100 assert(s && s->ref >= 1);
101
102 if (s->state == st)
103 return;
104
105 pa_stream_ref(s);
106
107 s->state = st;
108
109 if ((st == PA_STREAM_FAILED || st == PA_STREAM_TERMINATED) && s->context) {
110 if (s->channel_valid)
111 pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, NULL);
112
113 PA_LLIST_REMOVE(struct pa_stream, s->context->streams, s);
114 pa_stream_unref(s);
115 }
116
117 if (s->state_callback)
118 s->state_callback(s, s->state_userdata);
119
120 pa_stream_unref(s);
121 }
122
123 void pa_command_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
124 struct pa_context *c = userdata;
125 struct pa_stream *s;
126 uint32_t channel;
127 assert(pd && (command == PA_COMMAND_PLAYBACK_STREAM_KILLED || command == PA_COMMAND_RECORD_STREAM_KILLED) && t && c);
128
129 pa_context_ref(c);
130
131 if (pa_tagstruct_getu32(t, &channel) < 0 ||
132 !pa_tagstruct_eof(t)) {
133 pa_context_fail(c, PA_ERROR_PROTOCOL);
134 goto finish;
135 }
136
137 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel)))
138 goto finish;
139
140 c->error = PA_ERROR_KILLED;
141 pa_stream_set_state(s, PA_STREAM_FAILED);
142
143 finish:
144 pa_context_unref(c);
145 }
146
147 void pa_command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
148 struct pa_stream *s;
149 struct pa_context *c = userdata;
150 uint32_t bytes, channel;
151 assert(pd && command == PA_COMMAND_REQUEST && t && c);
152
153 pa_context_ref(c);
154
155 if (pa_tagstruct_getu32(t, &channel) < 0 ||
156 pa_tagstruct_getu32(t, &bytes) < 0 ||
157 !pa_tagstruct_eof(t)) {
158 pa_context_fail(c, PA_ERROR_PROTOCOL);
159 goto finish;
160 }
161
162 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
163 goto finish;
164
165 if (s->state != PA_STREAM_READY)
166 goto finish;
167
168 pa_stream_ref(s);
169
170 s->requested_bytes += bytes;
171
172 if (s->requested_bytes && s->write_callback)
173 s->write_callback(s, s->requested_bytes, s->write_userdata);
174
175 pa_stream_unref(s);
176
177 finish:
178 pa_context_unref(c);
179 }
180
181 void pa_create_stream_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
182 struct pa_stream *s = userdata;
183 assert(pd && s && s->state == PA_STREAM_CREATING);
184
185 pa_stream_ref(s);
186
187 if (command != PA_COMMAND_REPLY) {
188 if (pa_context_handle_error(s->context, command, t) < 0)
189 goto finish;
190
191 pa_stream_set_state(s, PA_STREAM_FAILED);
192 goto finish;
193 }
194
195 if (pa_tagstruct_getu32(t, &s->channel) < 0 ||
196 ((s->direction != PA_STREAM_UPLOAD) && pa_tagstruct_getu32(t, &s->device_index) < 0) ||
197 ((s->direction != PA_STREAM_RECORD) && pa_tagstruct_getu32(t, &s->requested_bytes) < 0) ||
198 !pa_tagstruct_eof(t)) {
199 pa_context_fail(s->context, PA_ERROR_PROTOCOL);
200 goto finish;
201 }
202
203 s->channel_valid = 1;
204 pa_dynarray_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, s->channel, s);
205 pa_stream_set_state(s, PA_STREAM_READY);
206
207 if (s->requested_bytes && s->ref > 1 && s->write_callback)
208 s->write_callback(s, s->requested_bytes, s->write_userdata);
209
210 finish:
211 pa_stream_unref(s);
212 }
213
214 static void create_stream(struct pa_stream *s, const char *dev, const struct pa_buffer_attr *attr, pa_volume_t volume) {
215 struct pa_tagstruct *t;
216 uint32_t tag;
217 assert(s && s->ref >= 1 && s->state == PA_STREAM_DISCONNECTED);
218
219 pa_stream_ref(s);
220
221 if (attr)
222 s->buffer_attr = *attr;
223 else {
224 s->buffer_attr.maxlength = DEFAULT_MAXLENGTH;
225 s->buffer_attr.tlength = DEFAULT_TLENGTH;
226 s->buffer_attr.prebuf = DEFAULT_PREBUF;
227 s->buffer_attr.minreq = DEFAULT_MINREQ;
228 s->buffer_attr.fragsize = DEFAULT_FRAGSIZE;
229 }
230
231 pa_stream_set_state(s, PA_STREAM_CREATING);
232
233 t = pa_tagstruct_new(NULL, 0);
234 assert(t);
235
236 if (!dev) {
237 if (s->direction == PA_STREAM_PLAYBACK)
238 dev = s->context->conf->default_sink;
239 else
240 dev = s->context->conf->default_source;
241 }
242
243 pa_tagstruct_putu32(t, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM);
244 pa_tagstruct_putu32(t, tag = s->context->ctag++);
245 pa_tagstruct_puts(t, s->name);
246 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
247 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
248 pa_tagstruct_puts(t, dev);
249 pa_tagstruct_putu32(t, s->buffer_attr.maxlength);
250 if (s->direction == PA_STREAM_PLAYBACK) {
251 pa_tagstruct_putu32(t, s->buffer_attr.tlength);
252 pa_tagstruct_putu32(t, s->buffer_attr.prebuf);
253 pa_tagstruct_putu32(t, s->buffer_attr.minreq);
254 pa_tagstruct_putu32(t, volume);
255 } else
256 pa_tagstruct_putu32(t, s->buffer_attr.fragsize);
257
258 pa_pstream_send_tagstruct(s->context->pstream, t);
259 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s);
260
261 pa_stream_unref(s);
262 }
263
264 void pa_stream_connect_playback(struct pa_stream *s, const char *dev, const struct pa_buffer_attr *attr, pa_volume_t volume) {
265 assert(s && s->context->state == PA_CONTEXT_READY && s->ref >= 1);
266 s->direction = PA_STREAM_PLAYBACK;
267 create_stream(s, dev, attr, volume);
268 }
269
270 void pa_stream_connect_record(struct pa_stream *s, const char *dev, const struct pa_buffer_attr *attr) {
271 assert(s && s->context->state == PA_CONTEXT_READY && s->ref >= 1);
272 s->direction = PA_STREAM_RECORD;
273 create_stream(s, dev, attr, 0);
274 }
275
276 void pa_stream_write(struct pa_stream *s, const void *data, size_t length, void (*free_cb)(void *p), size_t delta) {
277 struct pa_memchunk chunk;
278 assert(s && s->context && data && length && s->state == PA_STREAM_READY && s->ref >= 1);
279
280 if (free_cb) {
281 chunk.memblock = pa_memblock_new_user((void*) data, length, free_cb, s->context->memblock_stat);
282 assert(chunk.memblock && chunk.memblock->data);
283 } else {
284 chunk.memblock = pa_memblock_new(length, s->context->memblock_stat);
285 assert(chunk.memblock && chunk.memblock->data);
286 memcpy(chunk.memblock->data, data, length);
287 }
288 chunk.index = 0;
289 chunk.length = length;
290
291 pa_pstream_send_memblock(s->context->pstream, s->channel, delta, &chunk);
292 pa_memblock_unref(chunk.memblock);
293
294 if (length < s->requested_bytes)
295 s->requested_bytes -= length;
296 else
297 s->requested_bytes = 0;
298 }
299
300 size_t pa_stream_writable_size(struct pa_stream *s) {
301 assert(s && s->state == PA_STREAM_READY && s->ref >= 1);
302 return s->requested_bytes;
303 }
304
305 struct pa_operation * pa_stream_drain(struct pa_stream *s, void (*cb) (struct pa_stream*s, int success, void *userdata), void *userdata) {
306 struct pa_operation *o;
307 struct pa_tagstruct *t;
308 uint32_t tag;
309 assert(s && s->ref >= 1 && s->state == PA_STREAM_READY);
310
311 o = pa_operation_new(s->context, s);
312 assert(o);
313 o->callback = cb;
314 o->userdata = userdata;
315
316 t = pa_tagstruct_new(NULL, 0);
317 assert(t);
318 pa_tagstruct_putu32(t, PA_COMMAND_DRAIN_PLAYBACK_STREAM);
319 pa_tagstruct_putu32(t, tag = s->context->ctag++);
320 pa_tagstruct_putu32(t, s->channel);
321 pa_pstream_send_tagstruct(s->context->pstream, t);
322 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, o);
323
324 return pa_operation_ref(o);
325 }
326
327 static void stream_get_latency_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
328 struct pa_operation *o = userdata;
329 struct pa_latency_info i, *p = NULL;
330 struct timeval local, remote, now;
331 assert(pd && o && o->stream && o->context);
332
333 if (command != PA_COMMAND_REPLY) {
334 if (pa_context_handle_error(o->context, command, t) < 0)
335 goto finish;
336
337 } else if (pa_tagstruct_get_usec(t, &i.buffer_usec) < 0 ||
338 pa_tagstruct_get_usec(t, &i.sink_usec) < 0 ||
339 pa_tagstruct_get_usec(t, &i.source_usec) < 0 ||
340 pa_tagstruct_get_boolean(t, &i.playing) < 0 ||
341 pa_tagstruct_getu32(t, &i.queue_length) < 0 ||
342 pa_tagstruct_get_timeval(t, &local) < 0 ||
343 pa_tagstruct_get_timeval(t, &remote) < 0 ||
344 !pa_tagstruct_eof(t)) {
345 pa_context_fail(o->context, PA_ERROR_PROTOCOL);
346 goto finish;
347 } else
348 p = &i;
349
350 gettimeofday(&now, NULL);
351
352 if (pa_timeval_cmp(&local, &remote) < 0 && pa_timeval_cmp(&remote, &now)) {
353 /* local and remote seem to have synchronized clocks */
354
355 if (o->stream->direction == PA_STREAM_PLAYBACK)
356 i.transport_usec = pa_timeval_diff(&remote, &local);
357 else
358 i.transport_usec = pa_timeval_diff(&now, &remote);
359
360 i.synchronized_clocks = 1;
361 i.timestamp = remote;
362 } else {
363 /* clocks are not synchronized, let's estimate latency then */
364 i.transport_usec = pa_timeval_diff(&now, &local)/2;
365 i.synchronized_clocks = 0;
366 i.timestamp = local;
367 pa_timeval_add(&i.timestamp, i.transport_usec);
368 }
369
370 if (o->callback) {
371 void (*cb)(struct pa_stream *s, const struct pa_latency_info *i, void *userdata) = o->callback;
372 cb(o->stream, p, o->userdata);
373 }
374
375 finish:
376 pa_operation_done(o);
377 pa_operation_unref(o);
378 }
379
380 struct pa_operation* pa_stream_get_latency(struct pa_stream *s, void (*cb)(struct pa_stream *p, const struct pa_latency_info*i, void *userdata), void *userdata) {
381 uint32_t tag;
382 struct pa_operation *o;
383 struct pa_tagstruct *t;
384 struct timeval now;
385 assert(s && s->direction != PA_STREAM_UPLOAD);
386
387 o = pa_operation_new(s->context, s);
388 assert(o);
389 o->callback = cb;
390 o->userdata = userdata;
391
392 t = pa_tagstruct_new(NULL, 0);
393 assert(t);
394 pa_tagstruct_putu32(t, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_GET_PLAYBACK_LATENCY : PA_COMMAND_GET_RECORD_LATENCY);
395 pa_tagstruct_putu32(t, tag = s->context->ctag++);
396 pa_tagstruct_putu32(t, s->channel);
397
398 gettimeofday(&now, NULL);
399 pa_tagstruct_put_timeval(t, &now);
400
401 pa_pstream_send_tagstruct(s->context->pstream, t);
402 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, o);
403
404 return pa_operation_ref(o);
405 }
406
407 void pa_stream_disconnect_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
408 struct pa_stream *s = userdata;
409 assert(pd && s && s->ref >= 1);
410
411 pa_stream_ref(s);
412
413 if (command != PA_COMMAND_REPLY) {
414 if (pa_context_handle_error(s->context, command, t) < 0)
415 goto finish;
416
417 pa_stream_set_state(s, PA_STREAM_FAILED);
418 goto finish;
419 } else if (!pa_tagstruct_eof(t)) {
420 pa_context_fail(s->context, PA_ERROR_PROTOCOL);
421 goto finish;
422 }
423
424 pa_stream_set_state(s, PA_STREAM_TERMINATED);
425
426 finish:
427 pa_stream_unref(s);
428 }
429
430 void pa_stream_disconnect(struct pa_stream *s) {
431 struct pa_tagstruct *t;
432 uint32_t tag;
433 assert(s && s->ref >= 1);
434
435 if (!s->channel_valid || !s->context->state == PA_CONTEXT_READY)
436 return;
437
438 pa_stream_ref(s);
439
440 t = pa_tagstruct_new(NULL, 0);
441 assert(t);
442
443 pa_tagstruct_putu32(t, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_DELETE_PLAYBACK_STREAM :
444 (s->direction == PA_STREAM_RECORD ? PA_COMMAND_DELETE_RECORD_STREAM : PA_COMMAND_DELETE_UPLOAD_STREAM));
445 pa_tagstruct_putu32(t, tag = s->context->ctag++);
446 pa_tagstruct_putu32(t, s->channel);
447 pa_pstream_send_tagstruct(s->context->pstream, t);
448 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s);
449
450 pa_stream_unref(s);
451 }
452
453 void pa_stream_set_read_callback(struct pa_stream *s, void (*cb)(struct pa_stream *p, const void*data, size_t length, void *userdata), void *userdata) {
454 assert(s && s->ref >= 1);
455 s->read_callback = cb;
456 s->read_userdata = userdata;
457 }
458
459 void pa_stream_set_write_callback(struct pa_stream *s, void (*cb)(struct pa_stream *p, size_t length, void *userdata), void *userdata) {
460 assert(s && s->ref >= 1);
461 s->write_callback = cb;
462 s->write_userdata = userdata;
463 }
464
465 void pa_stream_set_state_callback(struct pa_stream *s, void (*cb)(struct pa_stream *s, void *userdata), void *userdata) {
466 assert(s && s->ref >= 1);
467 s->state_callback = cb;
468 s->state_userdata = userdata;
469 }
470
471 void pa_stream_simple_ack_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
472 struct pa_operation *o = userdata;
473 int success = 1;
474 assert(pd && o && o->context && o->ref >= 1);
475
476 if (command != PA_COMMAND_REPLY) {
477 if (pa_context_handle_error(o->context, command, t) < 0)
478 goto finish;
479
480 success = 0;
481 } else if (!pa_tagstruct_eof(t)) {
482 pa_context_fail(o->context, PA_ERROR_PROTOCOL);
483 goto finish;
484 }
485
486 if (o->callback) {
487 void (*cb)(struct pa_stream *s, int success, void *userdata) = o->callback;
488 cb(o->stream, success, o->userdata);
489 }
490
491 finish:
492 pa_operation_done(o);
493 pa_operation_unref(o);
494 }
495
496 struct pa_operation* pa_stream_cork(struct pa_stream *s, int b, void (*cb) (struct pa_stream*s, int success, void *userdata), void *userdata) {
497 struct pa_operation *o;
498 struct pa_tagstruct *t;
499 uint32_t tag;
500 assert(s && s->ref >= 1 && s->state == PA_STREAM_READY);
501
502 o = pa_operation_new(s->context, s);
503 assert(o);
504 o->callback = cb;
505 o->userdata = userdata;
506
507 t = pa_tagstruct_new(NULL, 0);
508 assert(t);
509 pa_tagstruct_putu32(t, PA_COMMAND_CORK_PLAYBACK_STREAM);
510 pa_tagstruct_putu32(t, tag = s->context->ctag++);
511 pa_tagstruct_putu32(t, s->channel);
512 pa_tagstruct_putu32(t, !!b);
513 pa_pstream_send_tagstruct(s->context->pstream, t);
514 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, o);
515
516 return pa_operation_ref(o);
517 }
518
519 struct pa_operation* pa_stream_send_simple_command(struct pa_stream *s, uint32_t command, void (*cb)(struct pa_stream *s, int success, void *userdata), void *userdata) {
520 struct pa_tagstruct *t;
521 struct pa_operation *o;
522 uint32_t tag;
523 assert(s && s->ref >= 1 && s->state == PA_STREAM_READY);
524
525 o = pa_operation_new(s->context, s);
526 o->callback = cb;
527 o->userdata = userdata;
528
529 t = pa_tagstruct_new(NULL, 0);
530 pa_tagstruct_putu32(t, command);
531 pa_tagstruct_putu32(t, tag = s->context->ctag++);
532 pa_tagstruct_putu32(t, s->channel);
533 pa_pstream_send_tagstruct(s->context->pstream, t);
534 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, o);
535
536 return pa_operation_ref(o);
537 }
538
539 struct pa_operation* pa_stream_flush(struct pa_stream *s, void (*cb)(struct pa_stream *s, int success, void *userdata), void *userdata) {
540 return pa_stream_send_simple_command(s, PA_COMMAND_FLUSH_PLAYBACK_STREAM, cb, userdata);
541 }
542
543 struct pa_operation* pa_stream_trigger(struct pa_stream *s, void (*cb)(struct pa_stream *s, int success, void *userdata), void *userdata) {
544 return pa_stream_send_simple_command(s, PA_COMMAND_TRIGGER_PLAYBACK_STREAM, cb, userdata);
545 }
546
547 struct pa_operation* pa_stream_set_name(struct pa_stream *s, const char *name, void(*cb)(struct pa_stream*c, int success, void *userdata), void *userdata) {
548 struct pa_operation *o;
549 struct pa_tagstruct *t;
550 uint32_t tag;
551 assert(s && s->ref >= 1 && s->state == PA_STREAM_READY && name && s->direction != PA_STREAM_UPLOAD);
552
553 o = pa_operation_new(s->context, s);
554 assert(o);
555 o->callback = cb;
556 o->userdata = userdata;
557
558 t = pa_tagstruct_new(NULL, 0);
559 assert(t);
560 pa_tagstruct_putu32(t, s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_NAME : PA_COMMAND_SET_PLAYBACK_STREAM_NAME);
561 pa_tagstruct_putu32(t, tag = s->context->ctag++);
562 pa_tagstruct_putu32(t, s->channel);
563 pa_tagstruct_puts(t, name);
564 pa_pstream_send_tagstruct(s->context->pstream, t);
565 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, o);
566
567 return pa_operation_ref(o);
568 }