Add a missing pa_xfree.
[pulseaudio.git] / src / pulse / stream.c
blobcd70cdcb8581d98243ffd9873d290a3ef98c0059
1 /* $Id$ */
3 /***
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include <string.h>
30 #include <stdio.h>
31 #include <string.h>
33 #include <pulse/def.h>
34 #include <pulse/timeval.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/pstream-util.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/hashmap.h>
40 #include <pulsecore/macro.h>
42 #include "internal.h"
44 #define LATENCY_IPOL_INTERVAL_USEC (100000L)
46 pa_stream *pa_stream_new(pa_context *c, const char *name, const pa_sample_spec *ss, const pa_channel_map *map) {
47 pa_stream *s;
48 int i;
50 pa_assert(c);
51 pa_assert(PA_REFCNT_VALUE(c) >= 1);
53 PA_CHECK_VALIDITY_RETURN_NULL(c, ss && pa_sample_spec_valid(ss), PA_ERR_INVALID);
54 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 12 || (ss->format != PA_SAMPLE_S32LE || ss->format != PA_SAMPLE_S32NE), PA_ERR_NOTSUPPORTED);
55 PA_CHECK_VALIDITY_RETURN_NULL(c, !map || (pa_channel_map_valid(map) && map->channels == ss->channels), PA_ERR_INVALID);
57 s = pa_xnew(pa_stream, 1);
58 PA_REFCNT_INIT(s);
59 s->context = c;
60 s->mainloop = c->mainloop;
62 s->buffer_attr_not_ready = s->timing_info_not_ready = FALSE;
64 s->read_callback = NULL;
65 s->read_userdata = NULL;
66 s->write_callback = NULL;
67 s->write_userdata = NULL;
68 s->state_callback = NULL;
69 s->state_userdata = NULL;
70 s->overflow_callback = NULL;
71 s->overflow_userdata = NULL;
72 s->underflow_callback = NULL;
73 s->underflow_userdata = NULL;
74 s->latency_update_callback = NULL;
75 s->latency_update_userdata = NULL;
76 s->moved_callback = NULL;
77 s->moved_userdata = NULL;
78 s->suspended_callback = NULL;
79 s->suspended_userdata = NULL;
81 s->direction = PA_STREAM_NODIRECTION;
82 s->name = pa_xstrdup(name);
83 s->sample_spec = *ss;
84 s->flags = 0;
86 if (map)
87 s->channel_map = *map;
88 else
89 pa_channel_map_init_auto(&s->channel_map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
91 s->channel = 0;
92 s->channel_valid = 0;
93 s->syncid = c->csyncid++;
94 s->stream_index = PA_INVALID_INDEX;
95 s->requested_bytes = 0;
96 s->state = PA_STREAM_UNCONNECTED;
98 s->manual_buffer_attr = FALSE;
99 memset(&s->buffer_attr, 0, sizeof(s->buffer_attr));
101 s->device_index = PA_INVALID_INDEX;
102 s->device_name = NULL;
103 s->suspended = FALSE;
105 s->peek_memchunk.index = 0;
106 s->peek_memchunk.length = 0;
107 s->peek_memchunk.memblock = NULL;
108 s->peek_data = NULL;
110 s->record_memblockq = NULL;
112 s->previous_time = 0;
113 s->timing_info_valid = 0;
114 s->read_index_not_before = 0;
115 s->write_index_not_before = 0;
117 for (i = 0; i < PA_MAX_WRITE_INDEX_CORRECTIONS; i++)
118 s->write_index_corrections[i].valid = 0;
119 s->current_write_index_correction = 0;
121 s->corked = 0;
123 s->cached_time_valid = 0;
125 s->auto_timing_update_event = NULL;
126 s->auto_timing_update_requested = 0;
128 /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
129 PA_LLIST_PREPEND(pa_stream, c->streams, s);
130 pa_stream_ref(s);
132 return s;
135 static void stream_free(pa_stream *s) {
136 pa_assert(s);
137 pa_assert(!s->context);
138 pa_assert(!s->channel_valid);
140 if (s->auto_timing_update_event) {
141 pa_assert(s->mainloop);
142 s->mainloop->time_free(s->auto_timing_update_event);
145 if (s->peek_memchunk.memblock) {
146 if (s->peek_data)
147 pa_memblock_release(s->peek_memchunk.memblock);
148 pa_memblock_unref(s->peek_memchunk.memblock);
151 if (s->record_memblockq)
152 pa_memblockq_free(s->record_memblockq);
154 pa_xfree(s->name);
155 pa_xfree(s->device_name);
156 pa_xfree(s);
159 void pa_stream_unref(pa_stream *s) {
160 pa_assert(s);
161 pa_assert(PA_REFCNT_VALUE(s) >= 1);
163 if (PA_REFCNT_DEC(s) <= 0)
164 stream_free(s);
167 pa_stream* pa_stream_ref(pa_stream *s) {
168 pa_assert(s);
169 pa_assert(PA_REFCNT_VALUE(s) >= 1);
171 PA_REFCNT_INC(s);
172 return s;
175 pa_stream_state_t pa_stream_get_state(pa_stream *s) {
176 pa_assert(s);
177 pa_assert(PA_REFCNT_VALUE(s) >= 1);
179 return s->state;
182 pa_context* pa_stream_get_context(pa_stream *s) {
183 pa_assert(s);
184 pa_assert(PA_REFCNT_VALUE(s) >= 1);
186 return s->context;
189 uint32_t pa_stream_get_index(pa_stream *s) {
190 pa_assert(s);
191 pa_assert(PA_REFCNT_VALUE(s) >= 1);
193 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
195 return s->stream_index;
198 void pa_stream_set_state(pa_stream *s, pa_stream_state_t st) {
199 pa_assert(s);
200 pa_assert(PA_REFCNT_VALUE(s) >= 1);
202 if (s->state == st)
203 return;
205 pa_stream_ref(s);
207 s->state = st;
208 if (s->state_callback)
209 s->state_callback(s, s->state_userdata);
211 if ((st == PA_STREAM_FAILED || st == PA_STREAM_TERMINATED) && s->context) {
213 /* Detach from context */
214 pa_operation *o, *n;
216 /* Unref all operatio object that point to us */
217 for (o = s->context->operations; o; o = n) {
218 n = o->next;
220 if (o->stream == s)
221 pa_operation_cancel(o);
224 /* Drop all outstanding replies for this stream */
225 if (s->context->pdispatch)
226 pa_pdispatch_unregister_reply(s->context->pdispatch, s);
228 if (s->channel_valid)
229 pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, NULL);
231 PA_LLIST_REMOVE(pa_stream, s->context->streams, s);
232 pa_stream_unref(s);
234 s->channel = 0;
235 s->channel_valid = 0;
237 s->context = NULL;
239 s->read_callback = NULL;
240 s->write_callback = NULL;
241 s->state_callback = NULL;
242 s->overflow_callback = NULL;
243 s->underflow_callback = NULL;
244 s->latency_update_callback = NULL;
247 pa_stream_unref(s);
250 void pa_command_stream_killed(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
251 pa_context *c = userdata;
252 pa_stream *s;
253 uint32_t channel;
255 pa_assert(pd);
256 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_KILLED || command == PA_COMMAND_RECORD_STREAM_KILLED);
257 pa_assert(t);
258 pa_assert(c);
259 pa_assert(PA_REFCNT_VALUE(c) >= 1);
261 pa_context_ref(c);
263 if (pa_tagstruct_getu32(t, &channel) < 0 ||
264 !pa_tagstruct_eof(t)) {
265 pa_context_fail(c, PA_ERR_PROTOCOL);
266 goto finish;
269 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel)))
270 goto finish;
272 pa_context_set_error(c, PA_ERR_KILLED);
273 pa_stream_set_state(s, PA_STREAM_FAILED);
275 finish:
276 pa_context_unref(c);
279 void pa_command_stream_moved(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
280 pa_context *c = userdata;
281 pa_stream *s;
282 uint32_t channel;
283 const char *dn;
284 int suspended;
285 uint32_t di;
287 pa_assert(pd);
288 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_MOVED || command == PA_COMMAND_RECORD_STREAM_MOVED);
289 pa_assert(t);
290 pa_assert(c);
291 pa_assert(PA_REFCNT_VALUE(c) >= 1);
293 pa_context_ref(c);
295 if (c->version < 12) {
296 pa_context_fail(c, PA_ERR_PROTOCOL);
297 goto finish;
300 if (pa_tagstruct_getu32(t, &channel) < 0 ||
301 pa_tagstruct_getu32(t, &di) < 0 ||
302 pa_tagstruct_gets(t, &dn) < 0 ||
303 pa_tagstruct_get_boolean(t, &suspended) < 0 ||
304 !pa_tagstruct_eof(t)) {
305 pa_context_fail(c, PA_ERR_PROTOCOL);
306 goto finish;
309 if (!dn || di == PA_INVALID_INDEX) {
310 pa_context_fail(c, PA_ERR_PROTOCOL);
311 goto finish;
314 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_MOVED ? c->playback_streams : c->record_streams, channel)))
315 goto finish;
317 pa_xfree(s->device_name);
318 s->device_name = pa_xstrdup(dn);
319 s->device_index = di;
321 s->suspended = suspended;
323 if (s->moved_callback)
324 s->moved_callback(s, s->moved_userdata);
326 finish:
327 pa_context_unref(c);
330 void pa_command_stream_suspended(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
331 pa_context *c = userdata;
332 pa_stream *s;
333 uint32_t channel;
334 int suspended;
336 pa_assert(pd);
337 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED || command == PA_COMMAND_RECORD_STREAM_SUSPENDED);
338 pa_assert(t);
339 pa_assert(c);
340 pa_assert(PA_REFCNT_VALUE(c) >= 1);
342 pa_context_ref(c);
344 if (c->version < 12) {
345 pa_context_fail(c, PA_ERR_PROTOCOL);
346 goto finish;
349 if (pa_tagstruct_getu32(t, &channel) < 0 ||
350 pa_tagstruct_get_boolean(t, &suspended) < 0 ||
351 !pa_tagstruct_eof(t)) {
352 pa_context_fail(c, PA_ERR_PROTOCOL);
353 goto finish;
356 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED ? c->playback_streams : c->record_streams, channel)))
357 goto finish;
359 s->suspended = suspended;
361 if (s->suspended_callback)
362 s->suspended_callback(s, s->suspended_userdata);
364 finish:
365 pa_context_unref(c);
368 void pa_command_request(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
369 pa_stream *s;
370 pa_context *c = userdata;
371 uint32_t bytes, channel;
373 pa_assert(pd);
374 pa_assert(command == PA_COMMAND_REQUEST);
375 pa_assert(t);
376 pa_assert(c);
377 pa_assert(PA_REFCNT_VALUE(c) >= 1);
379 pa_context_ref(c);
381 if (pa_tagstruct_getu32(t, &channel) < 0 ||
382 pa_tagstruct_getu32(t, &bytes) < 0 ||
383 !pa_tagstruct_eof(t)) {
384 pa_context_fail(c, PA_ERR_PROTOCOL);
385 goto finish;
388 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
389 goto finish;
391 if (s->state == PA_STREAM_READY) {
392 s->requested_bytes += bytes;
394 if (s->requested_bytes > 0 && s->write_callback)
395 s->write_callback(s, s->requested_bytes, s->write_userdata);
398 finish:
399 pa_context_unref(c);
402 void pa_command_overflow_or_underflow(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
403 pa_stream *s;
404 pa_context *c = userdata;
405 uint32_t channel;
407 pa_assert(pd);
408 pa_assert(command == PA_COMMAND_OVERFLOW || command == PA_COMMAND_UNDERFLOW);
409 pa_assert(t);
410 pa_assert(c);
411 pa_assert(PA_REFCNT_VALUE(c) >= 1);
413 pa_context_ref(c);
415 if (pa_tagstruct_getu32(t, &channel) < 0 ||
416 !pa_tagstruct_eof(t)) {
417 pa_context_fail(c, PA_ERR_PROTOCOL);
418 goto finish;
421 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
422 goto finish;
424 if (s->state == PA_STREAM_READY) {
426 if (command == PA_COMMAND_OVERFLOW) {
427 if (s->overflow_callback)
428 s->overflow_callback(s, s->overflow_userdata);
429 } else if (command == PA_COMMAND_UNDERFLOW) {
430 if (s->underflow_callback)
431 s->underflow_callback(s, s->underflow_userdata);
435 finish:
436 pa_context_unref(c);
439 static void request_auto_timing_update(pa_stream *s, int force) {
440 pa_assert(s);
441 pa_assert(PA_REFCNT_VALUE(s) >= 1);
443 if (!(s->flags & PA_STREAM_AUTO_TIMING_UPDATE))
444 return;
446 if (s->state == PA_STREAM_READY &&
447 (force || !s->auto_timing_update_requested)) {
448 pa_operation *o;
450 /* pa_log("automatically requesting new timing data"); */
452 if ((o = pa_stream_update_timing_info(s, NULL, NULL))) {
453 pa_operation_unref(o);
454 s->auto_timing_update_requested = 1;
458 if (s->auto_timing_update_event) {
459 struct timeval next;
460 pa_gettimeofday(&next);
461 pa_timeval_add(&next, LATENCY_IPOL_INTERVAL_USEC);
462 s->mainloop->time_restart(s->auto_timing_update_event, &next);
466 static void invalidate_indexes(pa_stream *s, int r, int w) {
467 pa_assert(s);
468 pa_assert(PA_REFCNT_VALUE(s) >= 1);
470 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
472 if (s->state != PA_STREAM_READY)
473 return;
475 if (w) {
476 s->write_index_not_before = s->context->ctag;
478 if (s->timing_info_valid)
479 s->timing_info.write_index_corrupt = 1;
481 /* pa_log("write_index invalidated"); */
484 if (r) {
485 s->read_index_not_before = s->context->ctag;
487 if (s->timing_info_valid)
488 s->timing_info.read_index_corrupt = 1;
490 /* pa_log("read_index invalidated"); */
493 if ((s->direction == PA_STREAM_PLAYBACK && r) ||
494 (s->direction == PA_STREAM_RECORD && w))
495 s->cached_time_valid = 0;
497 request_auto_timing_update(s, 1);
500 static void auto_timing_update_callback(PA_GCC_UNUSED pa_mainloop_api *m, PA_GCC_UNUSED pa_time_event *e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
501 pa_stream *s = userdata;
503 pa_assert(s);
504 pa_assert(PA_REFCNT_VALUE(s) >= 1);
506 /* pa_log("time event"); */
508 pa_stream_ref(s);
509 request_auto_timing_update(s, 0);
510 pa_stream_unref(s);
513 static void create_stream_complete(pa_stream *s) {
514 pa_assert(s);
515 pa_assert(PA_REFCNT_VALUE(s) >= 1);
516 pa_assert(s->state == PA_STREAM_CREATING);
518 if (s->buffer_attr_not_ready || s->timing_info_not_ready)
519 return;
521 pa_stream_set_state(s, PA_STREAM_READY);
523 if (s->requested_bytes > 0 && s->write_callback)
524 s->write_callback(s, s->requested_bytes, s->write_userdata);
526 if (s->flags & PA_STREAM_AUTO_TIMING_UPDATE) {
527 struct timeval tv;
528 pa_gettimeofday(&tv);
529 tv.tv_usec += LATENCY_IPOL_INTERVAL_USEC; /* every 100 ms */
530 pa_assert(!s->auto_timing_update_event);
531 s->auto_timing_update_event = s->mainloop->time_new(s->mainloop, &tv, &auto_timing_update_callback, s);
535 static void automatic_buffer_attr(pa_buffer_attr *attr, pa_sample_spec *ss) {
536 pa_assert(attr);
537 pa_assert(ss);
539 attr->tlength = pa_bytes_per_second(ss)/2;
540 attr->maxlength = (attr->tlength*3)/2;
541 attr->minreq = attr->tlength/50;
542 attr->prebuf = attr->tlength - attr->minreq;
543 attr->fragsize = attr->tlength/50;
546 void pa_create_stream_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
547 pa_stream *s = userdata;
549 pa_assert(pd);
550 pa_assert(s);
551 pa_assert(PA_REFCNT_VALUE(s) >= 1);
552 pa_assert(s->state == PA_STREAM_CREATING);
554 pa_stream_ref(s);
556 if (command != PA_COMMAND_REPLY) {
557 if (pa_context_handle_error(s->context, command, t) < 0)
558 goto finish;
560 pa_stream_set_state(s, PA_STREAM_FAILED);
561 goto finish;
564 if (pa_tagstruct_getu32(t, &s->channel) < 0 ||
565 ((s->direction != PA_STREAM_UPLOAD) && pa_tagstruct_getu32(t, &s->stream_index) < 0) ||
566 ((s->direction != PA_STREAM_RECORD) && pa_tagstruct_getu32(t, &s->requested_bytes) < 0)) {
567 pa_context_fail(s->context, PA_ERR_PROTOCOL);
568 goto finish;
571 if (s->context->version >= 9) {
572 if (s->direction == PA_STREAM_PLAYBACK) {
573 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
574 pa_tagstruct_getu32(t, &s->buffer_attr.tlength) < 0 ||
575 pa_tagstruct_getu32(t, &s->buffer_attr.prebuf) < 0 ||
576 pa_tagstruct_getu32(t, &s->buffer_attr.minreq) < 0) {
577 pa_context_fail(s->context, PA_ERR_PROTOCOL);
578 goto finish;
580 } else if (s->direction == PA_STREAM_RECORD) {
581 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
582 pa_tagstruct_getu32(t, &s->buffer_attr.fragsize) < 0) {
583 pa_context_fail(s->context, PA_ERR_PROTOCOL);
584 goto finish;
589 if (s->context->version >= 12 && s->direction != PA_STREAM_UPLOAD) {
590 pa_sample_spec ss;
591 pa_channel_map cm;
592 const char *dn = NULL;
593 int suspended;
595 if (pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
596 pa_tagstruct_get_channel_map(t, &cm) < 0 ||
597 pa_tagstruct_getu32(t, &s->device_index) < 0 ||
598 pa_tagstruct_gets(t, &dn) < 0 ||
599 pa_tagstruct_get_boolean(t, &suspended) < 0) {
600 pa_context_fail(s->context, PA_ERR_PROTOCOL);
601 goto finish;
604 if (!dn || s->device_index == PA_INVALID_INDEX ||
605 ss.channels != cm.channels ||
606 !pa_channel_map_valid(&cm) ||
607 !pa_sample_spec_valid(&ss) ||
608 (!(s->flags & PA_STREAM_FIX_FORMAT) && ss.format != s->sample_spec.format) ||
609 (!(s->flags & PA_STREAM_FIX_RATE) && ss.rate != s->sample_spec.rate) ||
610 (!(s->flags & PA_STREAM_FIX_CHANNELS) && !pa_channel_map_equal(&cm, &s->channel_map))) {
611 pa_context_fail(s->context, PA_ERR_PROTOCOL);
612 goto finish;
615 pa_xfree(s->device_name);
616 s->device_name = pa_xstrdup(dn);
617 s->suspended = suspended;
619 if (!s->manual_buffer_attr && pa_bytes_per_second(&ss) != pa_bytes_per_second(&s->sample_spec)) {
620 pa_buffer_attr attr;
621 pa_operation *o;
623 automatic_buffer_attr(&attr, &ss);
625 /* If we need to update the buffer metrics, we wait for
626 * the the OK for that call before we go to
627 * PA_STREAM_READY */
629 s->state = PA_STREAM_READY;
630 pa_assert_se(o = pa_stream_set_buffer_attr(s, &attr, NULL, NULL));
631 pa_operation_unref(o);
632 s->state = PA_STREAM_CREATING;
634 s->buffer_attr_not_ready = TRUE;
637 s->channel_map = cm;
638 s->sample_spec = ss;
641 if (!pa_tagstruct_eof(t)) {
642 pa_context_fail(s->context, PA_ERR_PROTOCOL);
643 goto finish;
646 if (s->direction == PA_STREAM_RECORD) {
647 pa_assert(!s->record_memblockq);
649 s->record_memblockq = pa_memblockq_new(
651 s->buffer_attr.maxlength,
653 pa_frame_size(&s->sample_spec),
656 NULL);
659 s->channel_valid = 1;
660 pa_dynarray_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, s->channel, s);
662 if (s->direction != PA_STREAM_UPLOAD && s->flags & PA_STREAM_AUTO_TIMING_UPDATE) {
664 /* If automatic timing updates are active, we wait for the
665 * first timing update before going to PA_STREAM_READY
666 * state */
668 s->state = PA_STREAM_READY;
669 request_auto_timing_update(s, 1);
670 s->state = PA_STREAM_CREATING;
672 s->timing_info_not_ready = TRUE;
675 create_stream_complete(s);
677 finish:
678 pa_stream_unref(s);
681 static int create_stream(
682 pa_stream_direction_t direction,
683 pa_stream *s,
684 const char *dev,
685 const pa_buffer_attr *attr,
686 pa_stream_flags_t flags,
687 const pa_cvolume *volume,
688 pa_stream *sync_stream) {
690 pa_tagstruct *t;
691 uint32_t tag;
693 pa_assert(s);
694 pa_assert(PA_REFCNT_VALUE(s) >= 1);
696 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
697 PA_CHECK_VALIDITY(s->context, !(flags & ~((direction != PA_STREAM_UPLOAD ?
698 PA_STREAM_START_CORKED|
699 PA_STREAM_INTERPOLATE_TIMING|
700 PA_STREAM_NOT_MONOTONOUS|
701 PA_STREAM_AUTO_TIMING_UPDATE|
702 PA_STREAM_NO_REMAP_CHANNELS|
703 PA_STREAM_NO_REMIX_CHANNELS|
704 PA_STREAM_FIX_FORMAT|
705 PA_STREAM_FIX_RATE|
706 PA_STREAM_FIX_CHANNELS|
707 PA_STREAM_DONT_MOVE : 0))), PA_ERR_INVALID);
708 PA_CHECK_VALIDITY(s->context, !volume || volume->channels == s->sample_spec.channels, PA_ERR_INVALID);
709 PA_CHECK_VALIDITY(s->context, !sync_stream || (direction == PA_STREAM_PLAYBACK && sync_stream->direction == PA_STREAM_PLAYBACK), PA_ERR_INVALID);
711 pa_stream_ref(s);
713 s->direction = direction;
714 s->flags = flags;
716 if (sync_stream)
717 s->syncid = sync_stream->syncid;
719 if (attr) {
720 s->buffer_attr = *attr;
721 s->manual_buffer_attr = TRUE;
722 } else {
723 /* half a second, with minimum request of 10 ms */
724 s->buffer_attr.tlength = pa_bytes_per_second(&s->sample_spec)/2;
725 s->buffer_attr.maxlength = (s->buffer_attr.tlength*3)/2;
726 s->buffer_attr.minreq = s->buffer_attr.tlength/50;
727 s->buffer_attr.prebuf = s->buffer_attr.tlength - s->buffer_attr.minreq;
728 s->buffer_attr.fragsize = s->buffer_attr.tlength/50;
729 s->manual_buffer_attr = FALSE;
732 if (!dev)
733 dev = s->direction == PA_STREAM_PLAYBACK ? s->context->conf->default_sink : s->context->conf->default_source;
735 t = pa_tagstruct_command(
736 s->context,
737 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM,
738 &tag);
740 pa_tagstruct_put(
742 PA_TAG_STRING, s->name,
743 PA_TAG_SAMPLE_SPEC, &s->sample_spec,
744 PA_TAG_CHANNEL_MAP, &s->channel_map,
745 PA_TAG_U32, PA_INVALID_INDEX,
746 PA_TAG_STRING, dev,
747 PA_TAG_U32, s->buffer_attr.maxlength,
748 PA_TAG_BOOLEAN, !!(flags & PA_STREAM_START_CORKED),
749 PA_TAG_INVALID);
751 if (s->direction == PA_STREAM_PLAYBACK) {
752 pa_cvolume cv;
754 pa_tagstruct_put(
756 PA_TAG_U32, s->buffer_attr.tlength,
757 PA_TAG_U32, s->buffer_attr.prebuf,
758 PA_TAG_U32, s->buffer_attr.minreq,
759 PA_TAG_U32, s->syncid,
760 PA_TAG_INVALID);
762 if (!volume)
763 volume = pa_cvolume_reset(&cv, s->sample_spec.channels);
765 pa_tagstruct_put_cvolume(t, volume);
766 } else
767 pa_tagstruct_putu32(t, s->buffer_attr.fragsize);
769 if (s->context->version >= 12 && s->direction != PA_STREAM_UPLOAD) {
770 pa_tagstruct_put(
772 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMAP_CHANNELS,
773 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMIX_CHANNELS,
774 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_FORMAT,
775 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_RATE,
776 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_CHANNELS,
777 PA_TAG_BOOLEAN, flags & PA_STREAM_DONT_MOVE,
778 PA_TAG_BOOLEAN, flags & PA_STREAM_VARIABLE_RATE,
779 PA_TAG_INVALID);
782 pa_pstream_send_tagstruct(s->context->pstream, t);
783 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s, NULL);
785 pa_stream_set_state(s, PA_STREAM_CREATING);
787 pa_stream_unref(s);
788 return 0;
791 int pa_stream_connect_playback(
792 pa_stream *s,
793 const char *dev,
794 const pa_buffer_attr *attr,
795 pa_stream_flags_t flags,
796 pa_cvolume *volume,
797 pa_stream *sync_stream) {
799 pa_assert(s);
800 pa_assert(PA_REFCNT_VALUE(s) >= 1);
802 return create_stream(PA_STREAM_PLAYBACK, s, dev, attr, flags, volume, sync_stream);
805 int pa_stream_connect_record(
806 pa_stream *s,
807 const char *dev,
808 const pa_buffer_attr *attr,
809 pa_stream_flags_t flags) {
811 pa_assert(s);
812 pa_assert(PA_REFCNT_VALUE(s) >= 1);
814 return create_stream(PA_STREAM_RECORD, s, dev, attr, flags, NULL, NULL);
817 int pa_stream_write(
818 pa_stream *s,
819 const void *data,
820 size_t length,
821 void (*free_cb)(void *p),
822 int64_t offset,
823 pa_seek_mode_t seek) {
825 pa_memchunk chunk;
827 pa_assert(s);
828 pa_assert(PA_REFCNT_VALUE(s) >= 1);
829 pa_assert(data);
831 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
832 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || s->direction == PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
833 PA_CHECK_VALIDITY(s->context, seek <= PA_SEEK_RELATIVE_END, PA_ERR_INVALID);
834 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || (seek == PA_SEEK_RELATIVE && offset == 0), PA_ERR_INVALID);
836 if (length <= 0)
837 return 0;
839 if (free_cb)
840 chunk.memblock = pa_memblock_new_user(s->context->mempool, (void*) data, length, free_cb, 1);
841 else {
842 void *tdata;
843 chunk.memblock = pa_memblock_new(s->context->mempool, length);
844 tdata = pa_memblock_acquire(chunk.memblock);
845 memcpy(tdata, data, length);
846 pa_memblock_release(chunk.memblock);
849 chunk.index = 0;
850 chunk.length = length;
852 pa_pstream_send_memblock(s->context->pstream, s->channel, offset, seek, &chunk);
853 pa_memblock_unref(chunk.memblock);
855 if (length < s->requested_bytes)
856 s->requested_bytes -= length;
857 else
858 s->requested_bytes = 0;
860 if (s->direction == PA_STREAM_PLAYBACK) {
862 /* Update latency request correction */
863 if (s->write_index_corrections[s->current_write_index_correction].valid) {
865 if (seek == PA_SEEK_ABSOLUTE) {
866 s->write_index_corrections[s->current_write_index_correction].corrupt = 0;
867 s->write_index_corrections[s->current_write_index_correction].absolute = 1;
868 s->write_index_corrections[s->current_write_index_correction].value = offset + length;
869 } else if (seek == PA_SEEK_RELATIVE) {
870 if (!s->write_index_corrections[s->current_write_index_correction].corrupt)
871 s->write_index_corrections[s->current_write_index_correction].value += offset + length;
872 } else
873 s->write_index_corrections[s->current_write_index_correction].corrupt = 1;
876 /* Update the write index in the already available latency data */
877 if (s->timing_info_valid) {
879 if (seek == PA_SEEK_ABSOLUTE) {
880 s->timing_info.write_index_corrupt = 0;
881 s->timing_info.write_index = offset + length;
882 } else if (seek == PA_SEEK_RELATIVE) {
883 if (!s->timing_info.write_index_corrupt)
884 s->timing_info.write_index += offset + length;
885 } else
886 s->timing_info.write_index_corrupt = 1;
889 if (!s->timing_info_valid || s->timing_info.write_index_corrupt)
890 request_auto_timing_update(s, 1);
893 return 0;
896 int pa_stream_peek(pa_stream *s, const void **data, size_t *length) {
897 pa_assert(s);
898 pa_assert(PA_REFCNT_VALUE(s) >= 1);
899 pa_assert(data);
900 pa_assert(length);
902 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
903 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
905 if (!s->peek_memchunk.memblock) {
907 if (pa_memblockq_peek(s->record_memblockq, &s->peek_memchunk) < 0) {
908 *data = NULL;
909 *length = 0;
910 return 0;
913 s->peek_data = pa_memblock_acquire(s->peek_memchunk.memblock);
916 pa_assert(s->peek_data);
917 *data = (uint8_t*) s->peek_data + s->peek_memchunk.index;
918 *length = s->peek_memchunk.length;
919 return 0;
922 int pa_stream_drop(pa_stream *s) {
923 pa_assert(s);
924 pa_assert(PA_REFCNT_VALUE(s) >= 1);
926 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
927 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
928 PA_CHECK_VALIDITY(s->context, s->peek_memchunk.memblock, PA_ERR_BADSTATE);
930 pa_memblockq_drop(s->record_memblockq, s->peek_memchunk.length);
932 /* Fix the simulated local read index */
933 if (s->timing_info_valid && !s->timing_info.read_index_corrupt)
934 s->timing_info.read_index += s->peek_memchunk.length;
936 pa_assert(s->peek_data);
937 pa_memblock_release(s->peek_memchunk.memblock);
938 pa_memblock_unref(s->peek_memchunk.memblock);
939 s->peek_memchunk.length = 0;
940 s->peek_memchunk.index = 0;
941 s->peek_memchunk.memblock = NULL;
943 return 0;
946 size_t pa_stream_writable_size(pa_stream *s) {
947 pa_assert(s);
948 pa_assert(PA_REFCNT_VALUE(s) >= 1);
950 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
951 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
953 return s->requested_bytes;
956 size_t pa_stream_readable_size(pa_stream *s) {
957 pa_assert(s);
958 pa_assert(PA_REFCNT_VALUE(s) >= 1);
960 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
961 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
963 return pa_memblockq_get_length(s->record_memblockq);
966 pa_operation * pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
967 pa_operation *o;
968 pa_tagstruct *t;
969 uint32_t tag;
971 pa_assert(s);
972 pa_assert(PA_REFCNT_VALUE(s) >= 1);
974 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
975 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
977 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
979 t = pa_tagstruct_command(s->context, PA_COMMAND_DRAIN_PLAYBACK_STREAM, &tag);
980 pa_tagstruct_putu32(t, s->channel);
981 pa_pstream_send_tagstruct(s->context->pstream, t);
982 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
984 return o;
987 static void stream_get_timing_info_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
988 pa_operation *o = userdata;
989 struct timeval local, remote, now;
990 pa_timing_info *i;
992 pa_assert(pd);
993 pa_assert(o);
994 pa_assert(PA_REFCNT_VALUE(o) >= 1);
996 if (!o->context || !o->stream)
997 goto finish;
999 i = &o->stream->timing_info;
1001 /* pa_log("pre corrupt w:%u r:%u\n", !o->stream->timing_info_valid || i->write_index_corrupt,!o->stream->timing_info_valid || i->read_index_corrupt); */
1003 o->stream->timing_info_valid = 0;
1004 i->write_index_corrupt = 0;
1005 i->read_index_corrupt = 0;
1007 /* pa_log("timing update %u\n", tag); */
1009 if (command != PA_COMMAND_REPLY) {
1010 if (pa_context_handle_error(o->context, command, t) < 0)
1011 goto finish;
1013 } else if (pa_tagstruct_get_usec(t, &i->sink_usec) < 0 ||
1014 pa_tagstruct_get_usec(t, &i->source_usec) < 0 ||
1015 pa_tagstruct_get_boolean(t, &i->playing) < 0 ||
1016 pa_tagstruct_get_timeval(t, &local) < 0 ||
1017 pa_tagstruct_get_timeval(t, &remote) < 0 ||
1018 pa_tagstruct_gets64(t, &i->write_index) < 0 ||
1019 pa_tagstruct_gets64(t, &i->read_index) < 0 ||
1020 !pa_tagstruct_eof(t)) {
1021 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1022 goto finish;
1024 } else {
1025 o->stream->timing_info_valid = 1;
1027 pa_gettimeofday(&now);
1029 /* Calculcate timestamps */
1030 if (pa_timeval_cmp(&local, &remote) <= 0 && pa_timeval_cmp(&remote, &now) <= 0) {
1031 /* local and remote seem to have synchronized clocks */
1033 if (o->stream->direction == PA_STREAM_PLAYBACK)
1034 i->transport_usec = pa_timeval_diff(&remote, &local);
1035 else
1036 i->transport_usec = pa_timeval_diff(&now, &remote);
1038 i->synchronized_clocks = 1;
1039 i->timestamp = remote;
1040 } else {
1041 /* clocks are not synchronized, let's estimate latency then */
1042 i->transport_usec = pa_timeval_diff(&now, &local)/2;
1043 i->synchronized_clocks = 0;
1044 i->timestamp = local;
1045 pa_timeval_add(&i->timestamp, i->transport_usec);
1048 /* Invalidate read and write indexes if necessary */
1049 if (tag < o->stream->read_index_not_before)
1050 i->read_index_corrupt = 1;
1052 if (tag < o->stream->write_index_not_before)
1053 i->write_index_corrupt = 1;
1055 if (o->stream->direction == PA_STREAM_PLAYBACK) {
1056 /* Write index correction */
1058 int n, j;
1059 uint32_t ctag = tag;
1061 /* Go through the saved correction values and add up the total correction.*/
1063 for (n = 0, j = o->stream->current_write_index_correction+1;
1064 n < PA_MAX_WRITE_INDEX_CORRECTIONS;
1065 n++, j = (j + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS) {
1067 /* Step over invalid data or out-of-date data */
1068 if (!o->stream->write_index_corrections[j].valid ||
1069 o->stream->write_index_corrections[j].tag < ctag)
1070 continue;
1072 /* Make sure that everything is in order */
1073 ctag = o->stream->write_index_corrections[j].tag+1;
1075 /* Now fix the write index */
1076 if (o->stream->write_index_corrections[j].corrupt) {
1077 /* A corrupting seek was made */
1078 i->write_index = 0;
1079 i->write_index_corrupt = 1;
1080 } else if (o->stream->write_index_corrections[j].absolute) {
1081 /* An absolute seek was made */
1082 i->write_index = o->stream->write_index_corrections[j].value;
1083 i->write_index_corrupt = 0;
1084 } else if (!i->write_index_corrupt) {
1085 /* A relative seek was made */
1086 i->write_index += o->stream->write_index_corrections[j].value;
1091 if (o->stream->direction == PA_STREAM_RECORD) {
1092 /* Read index correction */
1094 if (!i->read_index_corrupt)
1095 i->read_index -= pa_memblockq_get_length(o->stream->record_memblockq);
1098 o->stream->cached_time_valid = 0;
1101 o->stream->auto_timing_update_requested = 0;
1102 /* pa_log("post corrupt w:%u r:%u\n", i->write_index_corrupt || !o->stream->timing_info_valid, i->read_index_corrupt || !o->stream->timing_info_valid); */
1104 /* Clear old correction entries */
1105 if (o->stream->direction == PA_STREAM_PLAYBACK) {
1106 int n;
1108 for (n = 0; n < PA_MAX_WRITE_INDEX_CORRECTIONS; n++) {
1109 if (!o->stream->write_index_corrections[n].valid)
1110 continue;
1112 if (o->stream->write_index_corrections[n].tag <= tag)
1113 o->stream->write_index_corrections[n].valid = 0;
1117 /* First, let's complete the initialization, if necessary. */
1118 if (o->stream->state == PA_STREAM_CREATING) {
1119 o->stream->timing_info_not_ready = FALSE;
1120 create_stream_complete(o->stream);
1123 if (o->stream->latency_update_callback)
1124 o->stream->latency_update_callback(o->stream, o->stream->latency_update_userdata);
1126 if (o->callback && o->stream && o->stream->state == PA_STREAM_READY) {
1127 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1128 cb(o->stream, o->stream->timing_info_valid, o->userdata);
1131 finish:
1133 pa_operation_done(o);
1134 pa_operation_unref(o);
1137 pa_operation* pa_stream_update_timing_info(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1138 uint32_t tag;
1139 pa_operation *o;
1140 pa_tagstruct *t;
1141 struct timeval now;
1142 int cidx = 0;
1144 pa_assert(s);
1145 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1147 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1148 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1150 if (s->direction == PA_STREAM_PLAYBACK) {
1151 /* Find a place to store the write_index correction data for this entry */
1152 cidx = (s->current_write_index_correction + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS;
1154 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
1155 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !s->write_index_corrections[cidx].valid, PA_ERR_INTERNAL);
1157 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1159 t = pa_tagstruct_command(
1160 s->context,
1161 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_GET_PLAYBACK_LATENCY : PA_COMMAND_GET_RECORD_LATENCY,
1162 &tag);
1163 pa_tagstruct_putu32(t, s->channel);
1164 pa_tagstruct_put_timeval(t, pa_gettimeofday(&now));
1166 pa_pstream_send_tagstruct(s->context->pstream, t);
1167 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_timing_info_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
1169 if (s->direction == PA_STREAM_PLAYBACK) {
1170 /* Fill in initial correction data */
1171 o->stream->current_write_index_correction = cidx;
1172 o->stream->write_index_corrections[cidx].valid = 1;
1173 o->stream->write_index_corrections[cidx].tag = tag;
1174 o->stream->write_index_corrections[cidx].absolute = 0;
1175 o->stream->write_index_corrections[cidx].value = 0;
1176 o->stream->write_index_corrections[cidx].corrupt = 0;
1179 /* pa_log("requesting update %u\n", tag); */
1181 return o;
1184 void pa_stream_disconnect_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
1185 pa_stream *s = userdata;
1187 pa_assert(pd);
1188 pa_assert(s);
1189 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1191 pa_stream_ref(s);
1193 if (command != PA_COMMAND_REPLY) {
1194 if (pa_context_handle_error(s->context, command, t) < 0)
1195 goto finish;
1197 pa_stream_set_state(s, PA_STREAM_FAILED);
1198 goto finish;
1199 } else if (!pa_tagstruct_eof(t)) {
1200 pa_context_fail(s->context, PA_ERR_PROTOCOL);
1201 goto finish;
1204 pa_stream_set_state(s, PA_STREAM_TERMINATED);
1206 finish:
1207 pa_stream_unref(s);
1210 int pa_stream_disconnect(pa_stream *s) {
1211 pa_tagstruct *t;
1212 uint32_t tag;
1214 pa_assert(s);
1215 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1217 PA_CHECK_VALIDITY(s->context, s->channel_valid, PA_ERR_BADSTATE);
1218 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1220 pa_stream_ref(s);
1222 t = pa_tagstruct_command(
1223 s->context,
1224 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_DELETE_PLAYBACK_STREAM :
1225 (s->direction == PA_STREAM_RECORD ? PA_COMMAND_DELETE_RECORD_STREAM : PA_COMMAND_DELETE_UPLOAD_STREAM),
1226 &tag);
1227 pa_tagstruct_putu32(t, s->channel);
1228 pa_pstream_send_tagstruct(s->context->pstream, t);
1229 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s, NULL);
1231 pa_stream_unref(s);
1232 return 0;
1235 void pa_stream_set_read_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1236 pa_assert(s);
1237 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1239 s->read_callback = cb;
1240 s->read_userdata = userdata;
1243 void pa_stream_set_write_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1244 pa_assert(s);
1245 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1247 s->write_callback = cb;
1248 s->write_userdata = userdata;
1251 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1252 pa_assert(s);
1253 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1255 s->state_callback = cb;
1256 s->state_userdata = userdata;
1259 void pa_stream_set_overflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1260 pa_assert(s);
1261 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1263 s->overflow_callback = cb;
1264 s->overflow_userdata = userdata;
1267 void pa_stream_set_underflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1268 pa_assert(s);
1269 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1271 s->underflow_callback = cb;
1272 s->underflow_userdata = userdata;
1275 void pa_stream_set_latency_update_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1276 pa_assert(s);
1277 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1279 s->latency_update_callback = cb;
1280 s->latency_update_userdata = userdata;
1283 void pa_stream_set_moved_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1284 pa_assert(s);
1285 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1287 s->moved_callback = cb;
1288 s->moved_userdata = userdata;
1291 void pa_stream_set_suspended_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1292 pa_assert(s);
1293 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1295 s->suspended_callback = cb;
1296 s->suspended_userdata = userdata;
1299 void pa_stream_simple_ack_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
1300 pa_operation *o = userdata;
1301 int success = 1;
1303 pa_assert(pd);
1304 pa_assert(o);
1305 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1307 if (!o->context)
1308 goto finish;
1310 if (command != PA_COMMAND_REPLY) {
1311 if (pa_context_handle_error(o->context, command, t) < 0)
1312 goto finish;
1314 success = 0;
1315 } else if (!pa_tagstruct_eof(t)) {
1316 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1317 goto finish;
1320 if (o->callback) {
1321 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1322 cb(o->stream, success, o->userdata);
1325 finish:
1326 pa_operation_done(o);
1327 pa_operation_unref(o);
1330 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata) {
1331 pa_operation *o;
1332 pa_tagstruct *t;
1333 uint32_t tag;
1335 pa_assert(s);
1336 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1338 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1339 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1341 s->corked = b;
1343 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1345 t = pa_tagstruct_command(
1346 s->context,
1347 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CORK_PLAYBACK_STREAM : PA_COMMAND_CORK_RECORD_STREAM,
1348 &tag);
1349 pa_tagstruct_putu32(t, s->channel);
1350 pa_tagstruct_put_boolean(t, !!b);
1351 pa_pstream_send_tagstruct(s->context->pstream, t);
1352 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
1354 if (s->direction == PA_STREAM_PLAYBACK)
1355 invalidate_indexes(s, 1, 0);
1357 return o;
1360 static pa_operation* stream_send_simple_command(pa_stream *s, uint32_t command, pa_stream_success_cb_t cb, void *userdata) {
1361 pa_tagstruct *t;
1362 pa_operation *o;
1363 uint32_t tag;
1365 pa_assert(s);
1366 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1368 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1370 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1372 t = pa_tagstruct_command(s->context, command, &tag);
1373 pa_tagstruct_putu32(t, s->channel);
1374 pa_pstream_send_tagstruct(s->context->pstream, t);
1375 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
1377 return o;
1380 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1381 pa_operation *o;
1383 pa_assert(s);
1384 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1386 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1388 if ((o = stream_send_simple_command(s, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_FLUSH_PLAYBACK_STREAM : PA_COMMAND_FLUSH_RECORD_STREAM, cb, userdata))) {
1390 if (s->direction == PA_STREAM_PLAYBACK) {
1391 if (s->write_index_corrections[s->current_write_index_correction].valid)
1392 s->write_index_corrections[s->current_write_index_correction].corrupt = 1;
1394 if (s->timing_info_valid)
1395 s->timing_info.write_index_corrupt = 1;
1397 if (s->buffer_attr.prebuf > 0)
1398 invalidate_indexes(s, 1, 0);
1399 else
1400 request_auto_timing_update(s, 1);
1401 } else
1402 invalidate_indexes(s, 0, 1);
1405 return o;
1408 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1409 pa_operation *o;
1411 pa_assert(s);
1412 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1414 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1415 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1417 if ((o = stream_send_simple_command(s, PA_COMMAND_PREBUF_PLAYBACK_STREAM, cb, userdata)))
1418 invalidate_indexes(s, 1, 0);
1420 return o;
1423 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1424 pa_operation *o;
1426 pa_assert(s);
1427 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1429 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1430 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1432 if ((o = stream_send_simple_command(s, PA_COMMAND_TRIGGER_PLAYBACK_STREAM, cb, userdata)))
1433 invalidate_indexes(s, 1, 0);
1435 return o;
1438 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata) {
1439 pa_operation *o;
1440 pa_tagstruct *t;
1441 uint32_t tag;
1443 pa_assert(s);
1444 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1445 pa_assert(name);
1447 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1448 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1450 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1452 t = pa_tagstruct_command(
1453 s->context,
1454 s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_NAME : PA_COMMAND_SET_PLAYBACK_STREAM_NAME,
1455 &tag);
1456 pa_tagstruct_putu32(t, s->channel);
1457 pa_tagstruct_puts(t, name);
1458 pa_pstream_send_tagstruct(s->context->pstream, t);
1459 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
1461 return o;
1464 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec) {
1465 pa_usec_t usec = 0;
1467 pa_assert(s);
1468 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1470 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1471 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1472 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1473 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1474 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1476 if (s->cached_time_valid)
1477 /* We alredy calculated the time value for this timing info, so let's reuse it */
1478 usec = s->cached_time;
1479 else {
1480 if (s->direction == PA_STREAM_PLAYBACK) {
1481 /* The last byte that was written into the output device
1482 * had this time value associated */
1483 usec = pa_bytes_to_usec(s->timing_info.read_index < 0 ? 0 : (uint64_t) s->timing_info.read_index, &s->sample_spec);
1485 if (!s->corked) {
1486 /* Because the latency info took a little time to come
1487 * to us, we assume that the real output time is actually
1488 * a little ahead */
1489 usec += s->timing_info.transport_usec;
1491 /* However, the output device usually maintains a buffer
1492 too, hence the real sample currently played is a little
1493 back */
1494 if (s->timing_info.sink_usec >= usec)
1495 usec = 0;
1496 else
1497 usec -= s->timing_info.sink_usec;
1500 } else if (s->direction == PA_STREAM_RECORD) {
1501 /* The last byte written into the server side queue had
1502 * this time value associated */
1503 usec = pa_bytes_to_usec(s->timing_info.write_index < 0 ? 0 : (uint64_t) s->timing_info.write_index, &s->sample_spec);
1505 if (!s->corked) {
1506 /* Add transport latency */
1507 usec += s->timing_info.transport_usec;
1509 /* Add latency of data in device buffer */
1510 usec += s->timing_info.source_usec;
1512 /* If this is a monitor source, we need to correct the
1513 * time by the playback device buffer */
1514 if (s->timing_info.sink_usec >= usec)
1515 usec = 0;
1516 else
1517 usec -= s->timing_info.sink_usec;
1521 s->cached_time = usec;
1522 s->cached_time_valid = 1;
1525 /* Interpolate if requested */
1526 if (s->flags & PA_STREAM_INTERPOLATE_TIMING) {
1528 /* We just add the time that passed since the latency info was
1529 * current */
1530 if (!s->corked && s->timing_info.playing) {
1531 struct timeval now;
1532 usec += pa_timeval_diff(pa_gettimeofday(&now), &s->timing_info.timestamp);
1536 /* Make sure the time runs monotonically */
1537 if (!(s->flags & PA_STREAM_NOT_MONOTONOUS)) {
1538 if (usec < s->previous_time)
1539 usec = s->previous_time;
1540 else
1541 s->previous_time = usec;
1544 if (r_usec)
1545 *r_usec = usec;
1547 return 0;
1550 static pa_usec_t time_counter_diff(pa_stream *s, pa_usec_t a, pa_usec_t b, int *negative) {
1551 pa_assert(s);
1552 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1554 if (negative)
1555 *negative = 0;
1557 if (a >= b)
1558 return a-b;
1559 else {
1560 if (negative && s->direction == PA_STREAM_RECORD) {
1561 *negative = 1;
1562 return b-a;
1563 } else
1564 return 0;
1568 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative) {
1569 pa_usec_t t, c;
1570 int r;
1571 int64_t cindex;
1573 pa_assert(s);
1574 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1575 pa_assert(r_usec);
1577 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1578 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1579 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1580 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1581 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1583 if ((r = pa_stream_get_time(s, &t)) < 0)
1584 return r;
1586 if (s->direction == PA_STREAM_PLAYBACK)
1587 cindex = s->timing_info.write_index;
1588 else
1589 cindex = s->timing_info.read_index;
1591 if (cindex < 0)
1592 cindex = 0;
1594 c = pa_bytes_to_usec(cindex, &s->sample_spec);
1596 if (s->direction == PA_STREAM_PLAYBACK)
1597 *r_usec = time_counter_diff(s, c, t, negative);
1598 else
1599 *r_usec = time_counter_diff(s, t, c, negative);
1601 return 0;
1604 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s) {
1605 pa_assert(s);
1606 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1608 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1609 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1610 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->timing_info_valid, PA_ERR_BADSTATE);
1612 return &s->timing_info;
1615 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s) {
1616 pa_assert(s);
1617 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1619 return &s->sample_spec;
1622 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s) {
1623 pa_assert(s);
1624 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1626 return &s->channel_map;
1629 const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s) {
1630 pa_assert(s);
1631 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1633 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1634 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1635 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 9, PA_ERR_NODATA);
1637 return &s->buffer_attr;
1640 static void stream_set_buffer_attr_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
1641 pa_operation *o = userdata;
1642 int success = 1;
1644 pa_assert(pd);
1645 pa_assert(o);
1646 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1648 if (!o->context)
1649 goto finish;
1651 if (command != PA_COMMAND_REPLY) {
1652 if (pa_context_handle_error(o->context, command, t) < 0)
1653 goto finish;
1655 success = 0;
1656 } else {
1658 if (o->stream->direction == PA_STREAM_PLAYBACK) {
1659 if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
1660 pa_tagstruct_getu32(t, &o->stream->buffer_attr.tlength) < 0 ||
1661 pa_tagstruct_getu32(t, &o->stream->buffer_attr.prebuf) < 0 ||
1662 pa_tagstruct_getu32(t, &o->stream->buffer_attr.minreq) < 0) {
1663 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1664 goto finish;
1666 } else if (o->stream->direction == PA_STREAM_RECORD) {
1667 if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
1668 pa_tagstruct_getu32(t, &o->stream->buffer_attr.fragsize) < 0) {
1669 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1670 goto finish;
1674 if (!pa_tagstruct_eof(t)) {
1675 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1676 goto finish;
1679 o->stream->manual_buffer_attr = TRUE;
1682 if (o->stream->state == PA_STREAM_CREATING) {
1683 o->stream->buffer_attr_not_ready = FALSE;
1684 create_stream_complete(o->stream);
1687 if (o->callback) {
1688 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1689 cb(o->stream, success, o->userdata);
1692 finish:
1693 pa_operation_done(o);
1694 pa_operation_unref(o);
1698 pa_operation* pa_stream_set_buffer_attr(pa_stream *s, const pa_buffer_attr *attr, pa_stream_success_cb_t cb, void *userdata) {
1699 pa_operation *o;
1700 pa_tagstruct *t;
1701 uint32_t tag;
1703 pa_assert(s);
1704 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1705 pa_assert(attr);
1707 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1708 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1709 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
1711 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1713 t = pa_tagstruct_command(
1714 s->context,
1715 s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR : PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR,
1716 &tag);
1717 pa_tagstruct_putu32(t, s->channel);
1719 pa_tagstruct_putu32(t, attr->maxlength);
1721 if (s->direction == PA_STREAM_PLAYBACK)
1722 pa_tagstruct_put(
1724 PA_TAG_U32, attr->tlength,
1725 PA_TAG_U32, attr->prebuf,
1726 PA_TAG_U32, attr->minreq,
1727 PA_TAG_INVALID);
1728 else
1729 pa_tagstruct_putu32(t, attr->fragsize);
1731 pa_pstream_send_tagstruct(s->context->pstream, t);
1732 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_set_buffer_attr_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
1734 return o;
1737 uint32_t pa_stream_get_device_index(pa_stream *s) {
1738 pa_assert(s);
1739 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1741 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
1742 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE, PA_INVALID_INDEX);
1743 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
1744 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->device_index != PA_INVALID_INDEX, PA_ERR_BADSTATE, PA_INVALID_INDEX);
1746 return s->device_index;
1749 const char *pa_stream_get_device_name(pa_stream *s) {
1750 pa_assert(s);
1751 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1753 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1754 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1755 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
1756 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->device_name, PA_ERR_BADSTATE);
1758 return s->device_name;
1761 int pa_stream_is_suspended(pa_stream *s) {
1762 pa_assert(s);
1763 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1765 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1766 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1767 PA_CHECK_VALIDITY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
1769 return s->suspended;
1772 static void stream_update_sample_rate_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
1773 pa_operation *o = userdata;
1774 int success = 1;
1776 pa_assert(pd);
1777 pa_assert(o);
1778 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1780 if (!o->context)
1781 goto finish;
1783 if (command != PA_COMMAND_REPLY) {
1784 if (pa_context_handle_error(o->context, command, t) < 0)
1785 goto finish;
1787 success = 0;
1788 } else {
1790 if (!pa_tagstruct_eof(t)) {
1791 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1792 goto finish;
1796 o->stream->sample_spec.rate = PA_PTR_TO_UINT(o->private);
1797 pa_assert(pa_sample_spec_valid(&o->stream->sample_spec));
1799 if (o->callback) {
1800 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1801 cb(o->stream, success, o->userdata);
1804 finish:
1805 pa_operation_done(o);
1806 pa_operation_unref(o);
1810 pa_operation *pa_stream_update_sample_rate(pa_stream *s, uint32_t rate, pa_stream_success_cb_t cb, void *userdata) {
1811 pa_operation *o;
1812 pa_tagstruct *t;
1813 uint32_t tag;
1815 pa_assert(s);
1816 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1818 PA_CHECK_VALIDITY_RETURN_NULL(s->context, rate > 0 && rate <= PA_RATE_MAX, PA_ERR_INVALID);
1819 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1820 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1821 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->flags & PA_STREAM_VARIABLE_RATE, PA_ERR_BADSTATE);
1822 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
1824 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1825 o->private = PA_UINT_TO_PTR(rate);
1827 t = pa_tagstruct_command(
1828 s->context,
1829 s->direction == PA_STREAM_RECORD ? PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE : PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE,
1830 &tag);
1831 pa_tagstruct_putu32(t, s->channel);
1832 pa_tagstruct_putu32(t, rate);
1834 pa_pstream_send_tagstruct(s->context->pstream, t);
1835 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_update_sample_rate_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
1837 return o;