]> code.delx.au - pulseaudio/blob - polyp/polyplib-stream.c
Make the whole stuff LGPL only
[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 Lesser 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 Lesser 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 #include <string.h>
30
31 #include "polyplib-internal.h"
32 #include "xmalloc.h"
33 #include "pstream-util.h"
34 #include "util.h"
35
36 #define LATENCY_IPOL_INTERVAL_USEC (100000L)
37
38 struct pa_stream *pa_stream_new(struct pa_context *c, const char *name, const struct pa_sample_spec *ss) {
39 struct pa_stream *s;
40 assert(c && ss);
41
42 s = pa_xmalloc(sizeof(struct pa_stream));
43 s->ref = 1;
44 s->context = c;
45 s->mainloop = c->mainloop;
46
47 s->read_callback = NULL;
48 s->read_userdata = NULL;
49 s->write_callback = NULL;
50 s->write_userdata = NULL;
51 s->state_callback = NULL;
52 s->state_userdata = NULL;
53
54 s->direction = PA_STREAM_NODIRECTION;
55 s->name = pa_xstrdup(name);
56 s->sample_spec = *ss;
57 s->channel = 0;
58 s->channel_valid = 0;
59 s->device_index = PA_INVALID_INDEX;
60 s->requested_bytes = 0;
61 s->state = PA_STREAM_DISCONNECTED;
62 memset(&s->buffer_attr, 0, sizeof(s->buffer_attr));
63
64 s->counter = 0;
65 s->previous_time = 0;
66
67 s->corked = 0;
68 s->interpolate = 0;
69
70 s->ipol_usec = 0;
71 memset(&s->ipol_timestamp, 0, sizeof(s->ipol_timestamp));
72 s->ipol_event = NULL;
73
74 PA_LLIST_PREPEND(struct pa_stream, c->streams, s);
75
76 return pa_stream_ref(s);
77 }
78
79 static void stream_free(struct pa_stream *s) {
80 assert(s);
81
82 if (s->ipol_event) {
83 assert(s->mainloop);
84 s->mainloop->time_free(s->ipol_event);
85 }
86
87 pa_xfree(s->name);
88 pa_xfree(s);
89 }
90
91 void pa_stream_unref(struct pa_stream *s) {
92 assert(s && s->ref >= 1);
93
94 if (--(s->ref) == 0)
95 stream_free(s);
96 }
97
98 struct pa_stream* pa_stream_ref(struct pa_stream *s) {
99 assert(s && s->ref >= 1);
100 s->ref++;
101 return s;
102 }
103
104 enum pa_stream_state pa_stream_get_state(struct pa_stream *s) {
105 assert(s && s->ref >= 1);
106 return s->state;
107 }
108
109 struct pa_context* pa_stream_get_context(struct pa_stream *s) {
110 assert(s && s->ref >= 1);
111 return s->context;
112 }
113
114 uint32_t pa_stream_get_index(struct pa_stream *s) {
115 assert(s && s->ref >= 1);
116 return s->device_index;
117 }
118
119 void pa_stream_set_state(struct pa_stream *s, enum pa_stream_state st) {
120 assert(s && s->ref >= 1);
121
122 if (s->state == st)
123 return;
124
125 pa_stream_ref(s);
126
127 s->state = st;
128
129 if ((st == PA_STREAM_FAILED || st == PA_STREAM_TERMINATED) && s->context) {
130 if (s->channel_valid)
131 pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, NULL);
132
133 PA_LLIST_REMOVE(struct pa_stream, s->context->streams, s);
134 pa_stream_unref(s);
135 }
136
137 if (s->state_callback)
138 s->state_callback(s, s->state_userdata);
139
140 pa_stream_unref(s);
141 }
142
143 void pa_command_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
144 struct pa_context *c = userdata;
145 struct pa_stream *s;
146 uint32_t channel;
147 assert(pd && (command == PA_COMMAND_PLAYBACK_STREAM_KILLED || command == PA_COMMAND_RECORD_STREAM_KILLED) && t && c);
148
149 pa_context_ref(c);
150
151 if (pa_tagstruct_getu32(t, &channel) < 0 ||
152 !pa_tagstruct_eof(t)) {
153 pa_context_fail(c, PA_ERROR_PROTOCOL);
154 goto finish;
155 }
156
157 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel)))
158 goto finish;
159
160 c->error = PA_ERROR_KILLED;
161 pa_stream_set_state(s, PA_STREAM_FAILED);
162
163 finish:
164 pa_context_unref(c);
165 }
166
167 void pa_command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
168 struct pa_stream *s;
169 struct pa_context *c = userdata;
170 uint32_t bytes, channel;
171 assert(pd && command == PA_COMMAND_REQUEST && t && c);
172
173 pa_context_ref(c);
174
175 if (pa_tagstruct_getu32(t, &channel) < 0 ||
176 pa_tagstruct_getu32(t, &bytes) < 0 ||
177 !pa_tagstruct_eof(t)) {
178 pa_context_fail(c, PA_ERROR_PROTOCOL);
179 goto finish;
180 }
181
182 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
183 goto finish;
184
185 if (s->state != PA_STREAM_READY)
186 goto finish;
187
188 pa_stream_ref(s);
189
190 s->requested_bytes += bytes;
191
192 if (s->requested_bytes && s->write_callback)
193 s->write_callback(s, s->requested_bytes, s->write_userdata);
194
195 pa_stream_unref(s);
196
197 finish:
198 pa_context_unref(c);
199 }
200
201 static void ipol_callback(struct pa_mainloop_api *m, struct pa_time_event *e, const struct timeval *tv, void *userdata) {
202 struct timeval tv2;
203 struct pa_stream *s = userdata;
204
205 pa_stream_ref(s);
206 pa_operation_unref(pa_stream_get_latency_info(s, NULL, NULL));
207
208 gettimeofday(&tv2, NULL);
209 tv2.tv_usec += LATENCY_IPOL_INTERVAL_USEC;
210
211 m->time_restart(e, &tv2);
212
213 pa_stream_unref(s);
214 }
215
216
217 void pa_create_stream_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
218 struct pa_stream *s = userdata;
219 assert(pd && s && s->state == PA_STREAM_CREATING);
220
221 pa_stream_ref(s);
222
223 if (command != PA_COMMAND_REPLY) {
224 if (pa_context_handle_error(s->context, command, t) < 0)
225 goto finish;
226
227 pa_stream_set_state(s, PA_STREAM_FAILED);
228 goto finish;
229 }
230
231 if (pa_tagstruct_getu32(t, &s->channel) < 0 ||
232 ((s->direction != PA_STREAM_UPLOAD) && pa_tagstruct_getu32(t, &s->device_index) < 0) ||
233 ((s->direction != PA_STREAM_RECORD) && pa_tagstruct_getu32(t, &s->requested_bytes) < 0) ||
234 !pa_tagstruct_eof(t)) {
235 pa_context_fail(s->context, PA_ERROR_PROTOCOL);
236 goto finish;
237 }
238
239 s->channel_valid = 1;
240 pa_dynarray_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, s->channel, s);
241 pa_stream_set_state(s, PA_STREAM_READY);
242
243 if (s->interpolate) {
244 struct timeval tv;
245 pa_operation_unref(pa_stream_get_latency_info(s, NULL, NULL));
246
247 gettimeofday(&tv, NULL);
248 tv.tv_usec += LATENCY_IPOL_INTERVAL_USEC; /* every 100 ms */
249
250 assert(!s->ipol_event);
251 s->ipol_event = s->mainloop->time_new(s->mainloop, &tv, &ipol_callback, s);
252 }
253
254 if (s->requested_bytes && s->ref > 1 && s->write_callback)
255 s->write_callback(s, s->requested_bytes, s->write_userdata);
256
257 finish:
258 pa_stream_unref(s);
259 }
260
261 static void create_stream(struct pa_stream *s, const char *dev, const struct pa_buffer_attr *attr, enum pa_stream_flags flags, pa_volume_t volume) {
262 struct pa_tagstruct *t;
263 uint32_t tag;
264 assert(s && s->ref >= 1 && s->state == PA_STREAM_DISCONNECTED);
265
266 pa_stream_ref(s);
267
268 s->interpolate = !!(flags & PA_STREAM_INTERPOLATE_LATENCY);
269 pa_stream_trash_ipol(s);
270
271 if (attr)
272 s->buffer_attr = *attr;
273 else {
274 s->buffer_attr.maxlength = DEFAULT_MAXLENGTH;
275 s->buffer_attr.tlength = DEFAULT_TLENGTH;
276 s->buffer_attr.prebuf = DEFAULT_PREBUF;
277 s->buffer_attr.minreq = DEFAULT_MINREQ;
278 s->buffer_attr.fragsize = DEFAULT_FRAGSIZE;
279 }
280
281 pa_stream_set_state(s, PA_STREAM_CREATING);
282
283 t = pa_tagstruct_new(NULL, 0);
284 assert(t);
285
286 if (!dev) {
287 if (s->direction == PA_STREAM_PLAYBACK)
288 dev = s->context->conf->default_sink;
289 else
290 dev = s->context->conf->default_source;
291 }
292
293 pa_tagstruct_putu32(t, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM);
294 pa_tagstruct_putu32(t, tag = s->context->ctag++);
295 pa_tagstruct_puts(t, s->name);
296 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
297 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
298 pa_tagstruct_puts(t, dev);
299 pa_tagstruct_putu32(t, s->buffer_attr.maxlength);
300 pa_tagstruct_put_boolean(t, !!(flags & PA_STREAM_START_CORKED));
301 if (s->direction == PA_STREAM_PLAYBACK) {
302 pa_tagstruct_putu32(t, s->buffer_attr.tlength);
303 pa_tagstruct_putu32(t, s->buffer_attr.prebuf);
304 pa_tagstruct_putu32(t, s->buffer_attr.minreq);
305 pa_tagstruct_putu32(t, volume);
306 } else
307 pa_tagstruct_putu32(t, s->buffer_attr.fragsize);
308
309 pa_pstream_send_tagstruct(s->context->pstream, t);
310 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s);
311
312 pa_stream_unref(s);
313 }
314
315 void pa_stream_connect_playback(struct pa_stream *s, const char *dev, const struct pa_buffer_attr *attr, enum pa_stream_flags flags, pa_volume_t volume) {
316 assert(s && s->context->state == PA_CONTEXT_READY && s->ref >= 1);
317 s->direction = PA_STREAM_PLAYBACK;
318 create_stream(s, dev, attr, flags, volume);
319 }
320
321 void pa_stream_connect_record(struct pa_stream *s, const char *dev, const struct pa_buffer_attr *attr, enum pa_stream_flags flags) {
322 assert(s && s->context->state == PA_CONTEXT_READY && s->ref >= 1);
323 s->direction = PA_STREAM_RECORD;
324 create_stream(s, dev, attr, flags, 0);
325 }
326
327 void pa_stream_write(struct pa_stream *s, const void *data, size_t length, void (*free_cb)(void *p), size_t delta) {
328 struct pa_memchunk chunk;
329 assert(s && s->context && data && length && s->state == PA_STREAM_READY && s->ref >= 1);
330
331 if (free_cb) {
332 chunk.memblock = pa_memblock_new_user((void*) data, length, free_cb, s->context->memblock_stat);
333 assert(chunk.memblock && chunk.memblock->data);
334 } else {
335 chunk.memblock = pa_memblock_new(length, s->context->memblock_stat);
336 assert(chunk.memblock && chunk.memblock->data);
337 memcpy(chunk.memblock->data, data, length);
338 }
339 chunk.index = 0;
340 chunk.length = length;
341
342 pa_pstream_send_memblock(s->context->pstream, s->channel, delta, &chunk);
343 pa_memblock_unref(chunk.memblock);
344
345 if (length < s->requested_bytes)
346 s->requested_bytes -= length;
347 else
348 s->requested_bytes = 0;
349
350 s->counter += length;
351 }
352
353 size_t pa_stream_writable_size(struct pa_stream *s) {
354 assert(s && s->ref >= 1);
355 return s->state == PA_STREAM_READY ? s->requested_bytes : 0;
356 }
357
358 struct pa_operation * pa_stream_drain(struct pa_stream *s, void (*cb) (struct pa_stream*s, int success, void *userdata), void *userdata) {
359 struct pa_operation *o;
360 struct pa_tagstruct *t;
361 uint32_t tag;
362 assert(s && s->ref >= 1 && s->state == PA_STREAM_READY);
363
364 o = pa_operation_new(s->context, s);
365 assert(o);
366 o->callback = cb;
367 o->userdata = userdata;
368
369 t = pa_tagstruct_new(NULL, 0);
370 assert(t);
371 pa_tagstruct_putu32(t, PA_COMMAND_DRAIN_PLAYBACK_STREAM);
372 pa_tagstruct_putu32(t, tag = s->context->ctag++);
373 pa_tagstruct_putu32(t, s->channel);
374 pa_pstream_send_tagstruct(s->context->pstream, t);
375 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, o);
376
377 return pa_operation_ref(o);
378 }
379
380 static void stream_get_latency_info_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
381 struct pa_operation *o = userdata;
382 struct pa_latency_info i, *p = NULL;
383 struct timeval local, remote, now;
384 assert(pd && o && o->stream && o->context);
385
386 if (command != PA_COMMAND_REPLY) {
387 if (pa_context_handle_error(o->context, command, t) < 0)
388 goto finish;
389
390 } else if (pa_tagstruct_get_usec(t, &i.buffer_usec) < 0 ||
391 pa_tagstruct_get_usec(t, &i.sink_usec) < 0 ||
392 pa_tagstruct_get_usec(t, &i.source_usec) < 0 ||
393 pa_tagstruct_get_boolean(t, &i.playing) < 0 ||
394 pa_tagstruct_getu32(t, &i.queue_length) < 0 ||
395 pa_tagstruct_get_timeval(t, &local) < 0 ||
396 pa_tagstruct_get_timeval(t, &remote) < 0 ||
397 pa_tagstruct_getu64(t, &i.counter) < 0 ||
398 !pa_tagstruct_eof(t)) {
399 pa_context_fail(o->context, PA_ERROR_PROTOCOL);
400 goto finish;
401 } else {
402 gettimeofday(&now, NULL);
403
404 if (pa_timeval_cmp(&local, &remote) < 0 && pa_timeval_cmp(&remote, &now)) {
405 /* local and remote seem to have synchronized clocks */
406
407 if (o->stream->direction == PA_STREAM_PLAYBACK)
408 i.transport_usec = pa_timeval_diff(&remote, &local);
409 else
410 i.transport_usec = pa_timeval_diff(&now, &remote);
411
412 i.synchronized_clocks = 1;
413 i.timestamp = remote;
414 } else {
415 /* clocks are not synchronized, let's estimate latency then */
416 i.transport_usec = pa_timeval_diff(&now, &local)/2;
417 i.synchronized_clocks = 0;
418 i.timestamp = local;
419 pa_timeval_add(&i.timestamp, i.transport_usec);
420 }
421
422 if (o->stream->interpolate) {
423 o->stream->ipol_timestamp = i.timestamp;
424 o->stream->ipol_usec = pa_stream_get_time(o->stream, &i);
425 }
426
427 p = &i;
428 }
429
430 if (o->callback) {
431 void (*cb)(struct pa_stream *s, const struct pa_latency_info *i, void *userdata) = o->callback;
432 cb(o->stream, p, o->userdata);
433 }
434
435 finish:
436 pa_operation_done(o);
437 pa_operation_unref(o);
438 }
439
440 struct pa_operation* pa_stream_get_latency_info(struct pa_stream *s, void (*cb)(struct pa_stream *p, const struct pa_latency_info*i, void *userdata), void *userdata) {
441 uint32_t tag;
442 struct pa_operation *o;
443 struct pa_tagstruct *t;
444 struct timeval now;
445 assert(s && s->direction != PA_STREAM_UPLOAD);
446
447 o = pa_operation_new(s->context, s);
448 assert(o);
449 o->callback = cb;
450 o->userdata = userdata;
451
452 t = pa_tagstruct_new(NULL, 0);
453 assert(t);
454 pa_tagstruct_putu32(t, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_GET_PLAYBACK_LATENCY : PA_COMMAND_GET_RECORD_LATENCY);
455 pa_tagstruct_putu32(t, tag = s->context->ctag++);
456 pa_tagstruct_putu32(t, s->channel);
457
458 gettimeofday(&now, NULL);
459 pa_tagstruct_put_timeval(t, &now);
460 pa_tagstruct_putu64(t, s->counter);
461
462 pa_pstream_send_tagstruct(s->context->pstream, t);
463 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_info_callback, o);
464
465 return pa_operation_ref(o);
466 }
467
468 void pa_stream_disconnect_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
469 struct pa_stream *s = userdata;
470 assert(pd && s && s->ref >= 1);
471
472 pa_stream_ref(s);
473
474 if (command != PA_COMMAND_REPLY) {
475 if (pa_context_handle_error(s->context, command, t) < 0)
476 goto finish;
477
478 pa_stream_set_state(s, PA_STREAM_FAILED);
479 goto finish;
480 } else if (!pa_tagstruct_eof(t)) {
481 pa_context_fail(s->context, PA_ERROR_PROTOCOL);
482 goto finish;
483 }
484
485 pa_stream_set_state(s, PA_STREAM_TERMINATED);
486
487 finish:
488 pa_stream_unref(s);
489 }
490
491 void pa_stream_disconnect(struct pa_stream *s) {
492 struct pa_tagstruct *t;
493 uint32_t tag;
494 assert(s && s->ref >= 1);
495
496 if (!s->channel_valid || !s->context->state == PA_CONTEXT_READY)
497 return;
498
499 pa_stream_ref(s);
500
501 t = pa_tagstruct_new(NULL, 0);
502 assert(t);
503
504 pa_tagstruct_putu32(t, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_DELETE_PLAYBACK_STREAM :
505 (s->direction == PA_STREAM_RECORD ? PA_COMMAND_DELETE_RECORD_STREAM : PA_COMMAND_DELETE_UPLOAD_STREAM));
506 pa_tagstruct_putu32(t, tag = s->context->ctag++);
507 pa_tagstruct_putu32(t, s->channel);
508 pa_pstream_send_tagstruct(s->context->pstream, t);
509 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s);
510
511 pa_stream_unref(s);
512 }
513
514 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) {
515 assert(s && s->ref >= 1);
516 s->read_callback = cb;
517 s->read_userdata = userdata;
518 }
519
520 void pa_stream_set_write_callback(struct pa_stream *s, void (*cb)(struct pa_stream *p, size_t length, void *userdata), void *userdata) {
521 assert(s && s->ref >= 1);
522 s->write_callback = cb;
523 s->write_userdata = userdata;
524 }
525
526 void pa_stream_set_state_callback(struct pa_stream *s, void (*cb)(struct pa_stream *s, void *userdata), void *userdata) {
527 assert(s && s->ref >= 1);
528 s->state_callback = cb;
529 s->state_userdata = userdata;
530 }
531
532 void pa_stream_simple_ack_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
533 struct pa_operation *o = userdata;
534 int success = 1;
535 assert(pd && o && o->context && o->ref >= 1);
536
537 if (command != PA_COMMAND_REPLY) {
538 if (pa_context_handle_error(o->context, command, t) < 0)
539 goto finish;
540
541 success = 0;
542 } else if (!pa_tagstruct_eof(t)) {
543 pa_context_fail(o->context, PA_ERROR_PROTOCOL);
544 goto finish;
545 }
546
547 if (o->callback) {
548 void (*cb)(struct pa_stream *s, int success, void *userdata) = o->callback;
549 cb(o->stream, success, o->userdata);
550 }
551
552 finish:
553 pa_operation_done(o);
554 pa_operation_unref(o);
555 }
556
557 struct pa_operation* pa_stream_cork(struct pa_stream *s, int b, void (*cb) (struct pa_stream*s, int success, void *userdata), void *userdata) {
558 struct pa_operation *o;
559 struct pa_tagstruct *t;
560 uint32_t tag;
561 assert(s && s->ref >= 1 && s->state == PA_STREAM_READY);
562
563 if (s->interpolate) {
564 if (!s->corked && b)
565 s->ipol_usec = pa_stream_get_interpolated_time(s);
566 else if (s->corked && !b)
567 gettimeofday(&s->ipol_timestamp, NULL);
568 }
569
570 s->corked = b;
571
572 o = pa_operation_new(s->context, s);
573 assert(o);
574 o->callback = cb;
575 o->userdata = userdata;
576
577 t = pa_tagstruct_new(NULL, 0);
578 assert(t);
579 pa_tagstruct_putu32(t, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CORK_PLAYBACK_STREAM : PA_COMMAND_CORK_RECORD_STREAM);
580 pa_tagstruct_putu32(t, tag = s->context->ctag++);
581 pa_tagstruct_putu32(t, s->channel);
582 pa_tagstruct_put_boolean(t, !!b);
583 pa_pstream_send_tagstruct(s->context->pstream, t);
584 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, o);
585
586 pa_operation_unref(pa_stream_get_latency_info(s, NULL, NULL));
587
588 return pa_operation_ref(o);
589 }
590
591 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) {
592 struct pa_tagstruct *t;
593 struct pa_operation *o;
594 uint32_t tag;
595 assert(s && s->ref >= 1 && s->state == PA_STREAM_READY);
596
597 o = pa_operation_new(s->context, s);
598 o->callback = cb;
599 o->userdata = userdata;
600
601 t = pa_tagstruct_new(NULL, 0);
602 pa_tagstruct_putu32(t, command);
603 pa_tagstruct_putu32(t, tag = s->context->ctag++);
604 pa_tagstruct_putu32(t, s->channel);
605 pa_pstream_send_tagstruct(s->context->pstream, t);
606 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, o);
607
608 return pa_operation_ref(o);
609 }
610
611 struct pa_operation* pa_stream_flush(struct pa_stream *s, void (*cb)(struct pa_stream *s, int success, void *userdata), void *userdata) {
612 struct pa_operation *o;
613 o = pa_stream_send_simple_command(s, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_FLUSH_PLAYBACK_STREAM : PA_COMMAND_FLUSH_RECORD_STREAM, cb, userdata);
614 pa_operation_unref(pa_stream_get_latency_info(s, NULL, NULL));
615 return o;
616 }
617
618 struct pa_operation* pa_stream_prebuf(struct pa_stream *s, void (*cb)(struct pa_stream *s, int success, void *userdata), void *userdata) {
619 struct pa_operation *o;
620 o = pa_stream_send_simple_command(s, PA_COMMAND_PREBUF_PLAYBACK_STREAM, cb, userdata);
621 pa_operation_unref(pa_stream_get_latency_info(s, NULL, NULL));
622 return o;
623 }
624
625 struct pa_operation* pa_stream_trigger(struct pa_stream *s, void (*cb)(struct pa_stream *s, int success, void *userdata), void *userdata) {
626 struct pa_operation *o;
627 o = pa_stream_send_simple_command(s, PA_COMMAND_TRIGGER_PLAYBACK_STREAM, cb, userdata);
628 pa_operation_unref(pa_stream_get_latency_info(s, NULL, NULL));
629 return o;
630 }
631
632 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) {
633 struct pa_operation *o;
634 struct pa_tagstruct *t;
635 uint32_t tag;
636 assert(s && s->ref >= 1 && s->state == PA_STREAM_READY && name && s->direction != PA_STREAM_UPLOAD);
637
638 o = pa_operation_new(s->context, s);
639 assert(o);
640 o->callback = cb;
641 o->userdata = userdata;
642
643 t = pa_tagstruct_new(NULL, 0);
644 assert(t);
645 pa_tagstruct_putu32(t, s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_NAME : PA_COMMAND_SET_PLAYBACK_STREAM_NAME);
646 pa_tagstruct_putu32(t, tag = s->context->ctag++);
647 pa_tagstruct_putu32(t, s->channel);
648 pa_tagstruct_puts(t, name);
649 pa_pstream_send_tagstruct(s->context->pstream, t);
650 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, o);
651
652 return pa_operation_ref(o);
653 }
654
655 uint64_t pa_stream_get_counter(struct pa_stream *s) {
656 assert(s);
657 return s->counter;
658 }
659
660 pa_usec_t pa_stream_get_time(struct pa_stream *s, const struct pa_latency_info *i) {
661 pa_usec_t usec;
662 assert(s);
663
664 usec = pa_bytes_to_usec(i->counter, &s->sample_spec);
665
666 if (i) {
667 if (s->direction == PA_STREAM_PLAYBACK) {
668 pa_usec_t latency = i->transport_usec + i->buffer_usec + i->sink_usec;
669 if (usec < latency)
670 usec = 0;
671 else
672 usec -= latency;
673
674 } else if (s->direction == PA_STREAM_RECORD) {
675 usec += i->source_usec + i->buffer_usec + i->transport_usec;
676
677 if (usec > i->sink_usec)
678 usec -= i->sink_usec;
679 else
680 usec = 0;
681 }
682 }
683
684 if (usec < s->previous_time)
685 usec = s->previous_time;
686
687 s->previous_time = usec;
688
689 return usec;
690 }
691
692 static pa_usec_t time_counter_diff(struct pa_stream *s, pa_usec_t t, pa_usec_t c, int *negative) {
693 assert(s);
694
695 if (negative)
696 *negative = 0;
697
698 if (c < t) {
699 if (s->direction == PA_STREAM_RECORD) {
700 if (negative)
701 *negative = 1;
702
703 return t-c;
704 } else
705 return 0;
706 } else
707 return c-t;
708 }
709
710 pa_usec_t pa_stream_get_latency(struct pa_stream *s, const struct pa_latency_info *i, int *negative) {
711 pa_usec_t t, c;
712 assert(s && i);
713
714 t = pa_stream_get_time(s, i);
715 c = pa_bytes_to_usec(s->counter, &s->sample_spec);
716
717 return time_counter_diff(s, t, c, negative);
718 }
719
720 const struct pa_sample_spec* pa_stream_get_sample_spec(struct pa_stream *s) {
721 assert(s);
722 return &s->sample_spec;
723 }
724
725 void pa_stream_trash_ipol(struct pa_stream *s) {
726 assert(s);
727
728 if (!s->interpolate)
729 return;
730
731 memset(&s->ipol_timestamp, 0, sizeof(s->ipol_timestamp));
732 s->ipol_usec = 0;
733 }
734
735 pa_usec_t pa_stream_get_interpolated_time(struct pa_stream *s) {
736 pa_usec_t usec;
737 assert(s && s->interpolate);
738
739 if (s->corked)
740 usec = s->ipol_usec;
741 else {
742 if (s->ipol_timestamp.tv_sec == 0)
743 usec = 0;
744 else
745 usec = s->ipol_usec + pa_timeval_age(&s->ipol_timestamp);
746 }
747
748 if (usec < s->previous_time)
749 usec = s->previous_time;
750
751 s->previous_time = usec;
752 return usec;
753 }
754
755 pa_usec_t pa_stream_get_interpolated_latency(struct pa_stream *s, int *negative) {
756 pa_usec_t t, c;
757 assert(s && s->interpolate);
758
759 t = pa_stream_get_interpolated_time(s);
760 c = pa_bytes_to_usec(s->counter, &s->sample_spec);
761
762 return time_counter_diff(s, t, c, negative);
763 }