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