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