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
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>
41 #include <pulsecore/rtclock.h>
45 #define LATENCY_IPOL_INTERVAL_USEC (333*PA_USEC_PER_MSEC)
47 #define SMOOTHER_ADJUST_TIME (1000*PA_USEC_PER_MSEC)
48 #define SMOOTHER_HISTORY_TIME (5000*PA_USEC_PER_MSEC)
49 #define SMOOTHER_MIN_HISTORY (4)
51 pa_stream *pa_stream_new(pa_context *c, const char *name, const pa_sample_spec *ss, const pa_channel_map *map) {
52 return pa_stream_new_with_proplist(c, name, ss, map, NULL);
55 static void reset_callbacks(pa_stream *s) {
56 s->read_callback = NULL;
57 s->read_userdata = NULL;
58 s->write_callback = NULL;
59 s->write_userdata = NULL;
60 s->state_callback = NULL;
61 s->state_userdata = NULL;
62 s->overflow_callback = NULL;
63 s->overflow_userdata = NULL;
64 s->underflow_callback = NULL;
65 s->underflow_userdata = NULL;
66 s->latency_update_callback = NULL;
67 s->latency_update_userdata = NULL;
68 s->moved_callback = NULL;
69 s->moved_userdata = NULL;
70 s->suspended_callback = NULL;
71 s->suspended_userdata = NULL;
72 s->started_callback = NULL;
73 s->started_userdata = NULL;
76 pa_stream *pa_stream_new_with_proplist(
79 const pa_sample_spec *ss,
80 const pa_channel_map *map,
88 pa_assert(PA_REFCNT_VALUE(c) >= 1);
90 PA_CHECK_VALIDITY_RETURN_NULL(c, ss && pa_sample_spec_valid(ss), PA_ERR_INVALID);
91 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 12 || (ss->format != PA_SAMPLE_S32LE || ss->format != PA_SAMPLE_S32NE), PA_ERR_NOTSUPPORTED);
92 PA_CHECK_VALIDITY_RETURN_NULL(c, !map || (pa_channel_map_valid(map) && map->channels == ss->channels), PA_ERR_INVALID);
93 PA_CHECK_VALIDITY_RETURN_NULL(c, name || (p && pa_proplist_contains(p, PA_PROP_MEDIA_NAME)), PA_ERR_INVALID);
96 PA_CHECK_VALIDITY_RETURN_NULL(c, map = pa_channel_map_init_auto(&tmap, ss->channels, PA_CHANNEL_MAP_DEFAULT), PA_ERR_INVALID);
98 s = pa_xnew(pa_stream, 1);
101 s->mainloop = c->mainloop;
103 s->direction = PA_STREAM_NODIRECTION;
104 s->state = PA_STREAM_UNCONNECTED;
107 s->sample_spec = *ss;
108 s->channel_map = *map;
110 s->proplist = p ? pa_proplist_copy(p) : pa_proplist_new();
112 pa_proplist_sets(s->proplist, PA_PROP_MEDIA_NAME, name);
115 s->channel_valid = FALSE;
116 s->syncid = c->csyncid++;
117 s->stream_index = PA_INVALID_INDEX;
119 s->requested_bytes = 0;
120 memset(&s->buffer_attr, 0, sizeof(s->buffer_attr));
122 s->device_index = PA_INVALID_INDEX;
123 s->device_name = NULL;
124 s->suspended = FALSE;
126 pa_memchunk_reset(&s->peek_memchunk);
129 s->record_memblockq = NULL;
133 memset(&s->timing_info, 0, sizeof(s->timing_info));
134 s->timing_info_valid = FALSE;
136 s->previous_time = 0;
138 s->read_index_not_before = 0;
139 s->write_index_not_before = 0;
140 for (i = 0; i < PA_MAX_WRITE_INDEX_CORRECTIONS; i++)
141 s->write_index_corrections[i].valid = 0;
142 s->current_write_index_correction = 0;
144 s->auto_timing_update_event = NULL;
145 s->auto_timing_update_requested = FALSE;
151 /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
152 PA_LLIST_PREPEND(pa_stream, c->streams, s);
158 static void stream_unlink(pa_stream *s) {
165 /* Detach from context */
167 /* Unref all operatio object that point to us */
168 for (o = s->context->operations; o; o = n) {
172 pa_operation_cancel(o);
175 /* Drop all outstanding replies for this stream */
176 if (s->context->pdispatch)
177 pa_pdispatch_unregister_reply(s->context->pdispatch, s);
179 if (s->channel_valid) {
180 pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, NULL);
182 s->channel_valid = FALSE;
185 PA_LLIST_REMOVE(pa_stream, s->context->streams, s);
190 if (s->auto_timing_update_event) {
191 pa_assert(s->mainloop);
192 s->mainloop->time_free(s->auto_timing_update_event);
198 static void stream_free(pa_stream *s) {
203 if (s->peek_memchunk.memblock) {
205 pa_memblock_release(s->peek_memchunk.memblock);
206 pa_memblock_unref(s->peek_memchunk.memblock);
209 if (s->record_memblockq)
210 pa_memblockq_free(s->record_memblockq);
213 pa_proplist_free(s->proplist);
216 pa_smoother_free(s->smoother);
218 pa_xfree(s->device_name);
222 void pa_stream_unref(pa_stream *s) {
224 pa_assert(PA_REFCNT_VALUE(s) >= 1);
226 if (PA_REFCNT_DEC(s) <= 0)
230 pa_stream* pa_stream_ref(pa_stream *s) {
232 pa_assert(PA_REFCNT_VALUE(s) >= 1);
238 pa_stream_state_t pa_stream_get_state(pa_stream *s) {
240 pa_assert(PA_REFCNT_VALUE(s) >= 1);
245 pa_context* pa_stream_get_context(pa_stream *s) {
247 pa_assert(PA_REFCNT_VALUE(s) >= 1);
252 uint32_t pa_stream_get_index(pa_stream *s) {
254 pa_assert(PA_REFCNT_VALUE(s) >= 1);
256 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
258 return s->stream_index;
261 void pa_stream_set_state(pa_stream *s, pa_stream_state_t st) {
263 pa_assert(PA_REFCNT_VALUE(s) >= 1);
272 if (s->state_callback)
273 s->state_callback(s, s->state_userdata);
275 if ((st == PA_STREAM_FAILED || st == PA_STREAM_TERMINATED))
281 static void request_auto_timing_update(pa_stream *s, pa_bool_t force) {
283 pa_assert(PA_REFCNT_VALUE(s) >= 1);
285 if (!(s->flags & PA_STREAM_AUTO_TIMING_UPDATE))
288 if (s->state == PA_STREAM_READY &&
289 (force || !s->auto_timing_update_requested)) {
292 /* pa_log("automatically requesting new timing data"); */
294 if ((o = pa_stream_update_timing_info(s, NULL, NULL))) {
295 pa_operation_unref(o);
296 s->auto_timing_update_requested = TRUE;
300 if (s->auto_timing_update_event) {
302 pa_gettimeofday(&next);
303 pa_timeval_add(&next, LATENCY_IPOL_INTERVAL_USEC);
304 s->mainloop->time_restart(s->auto_timing_update_event, &next);
308 void pa_command_stream_killed(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
309 pa_context *c = userdata;
314 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_KILLED || command == PA_COMMAND_RECORD_STREAM_KILLED);
317 pa_assert(PA_REFCNT_VALUE(c) >= 1);
321 if (pa_tagstruct_getu32(t, &channel) < 0 ||
322 !pa_tagstruct_eof(t)) {
323 pa_context_fail(c, PA_ERR_PROTOCOL);
327 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel)))
330 if (s->state != PA_STREAM_READY)
333 pa_context_set_error(c, PA_ERR_KILLED);
334 pa_stream_set_state(s, PA_STREAM_FAILED);
340 void pa_command_stream_moved(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
341 pa_context *c = userdata;
348 uint32_t maxlength = 0, fragsize = 0, minreq = 0, tlength = 0, prebuf = 0;
351 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_MOVED || command == PA_COMMAND_RECORD_STREAM_MOVED);
354 pa_assert(PA_REFCNT_VALUE(c) >= 1);
358 if (c->version < 12) {
359 pa_context_fail(c, PA_ERR_PROTOCOL);
363 if (pa_tagstruct_getu32(t, &channel) < 0 ||
364 pa_tagstruct_getu32(t, &di) < 0 ||
365 pa_tagstruct_gets(t, &dn) < 0 ||
366 pa_tagstruct_get_boolean(t, &suspended) < 0) {
367 pa_context_fail(c, PA_ERR_PROTOCOL);
371 if (c->version >= 13) {
373 if (command == PA_COMMAND_RECORD_STREAM_MOVED) {
374 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
375 pa_tagstruct_getu32(t, &fragsize) < 0 ||
376 pa_tagstruct_get_usec(t, &usec) < 0) {
377 pa_context_fail(c, PA_ERR_PROTOCOL);
381 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
382 pa_tagstruct_getu32(t, &tlength) < 0 ||
383 pa_tagstruct_getu32(t, &prebuf) < 0 ||
384 pa_tagstruct_getu32(t, &minreq) < 0 ||
385 pa_tagstruct_get_usec(t, &usec) < 0) {
386 pa_context_fail(c, PA_ERR_PROTOCOL);
392 if (!pa_tagstruct_eof(t)) {
393 pa_context_fail(c, PA_ERR_PROTOCOL);
397 if (!dn || di == PA_INVALID_INDEX) {
398 pa_context_fail(c, PA_ERR_PROTOCOL);
402 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_MOVED ? c->playback_streams : c->record_streams, channel)))
405 if (s->state != PA_STREAM_READY)
408 if (c->version >= 13) {
409 if (s->direction == PA_STREAM_RECORD)
410 s->timing_info.configured_source_usec = usec;
412 s->timing_info.configured_sink_usec = usec;
414 s->buffer_attr.maxlength = maxlength;
415 s->buffer_attr.fragsize = fragsize;
416 s->buffer_attr.tlength = tlength;
417 s->buffer_attr.prebuf = prebuf;
418 s->buffer_attr.minreq = minreq;
421 pa_xfree(s->device_name);
422 s->device_name = pa_xstrdup(dn);
423 s->device_index = di;
425 s->suspended = suspended;
427 request_auto_timing_update(s, TRUE);
429 if (s->moved_callback)
430 s->moved_callback(s, s->moved_userdata);
436 void pa_command_stream_suspended(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
437 pa_context *c = userdata;
443 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED || command == PA_COMMAND_RECORD_STREAM_SUSPENDED);
446 pa_assert(PA_REFCNT_VALUE(c) >= 1);
450 if (c->version < 12) {
451 pa_context_fail(c, PA_ERR_PROTOCOL);
455 if (pa_tagstruct_getu32(t, &channel) < 0 ||
456 pa_tagstruct_get_boolean(t, &suspended) < 0 ||
457 !pa_tagstruct_eof(t)) {
458 pa_context_fail(c, PA_ERR_PROTOCOL);
462 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED ? c->playback_streams : c->record_streams, channel)))
465 if (s->state != PA_STREAM_READY)
468 s->suspended = suspended;
471 pa_usec_t x = pa_rtclock_usec();
473 if (s->timing_info_valid)
474 x -= s->timing_info.transport_usec;
476 if (s->suspended || s->corked)
477 pa_smoother_pause(s->smoother, x);
480 request_auto_timing_update(s, TRUE);
482 if (s->suspended_callback)
483 s->suspended_callback(s, s->suspended_userdata);
489 void pa_command_stream_started(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
490 pa_context *c = userdata;
495 pa_assert(command == PA_COMMAND_STARTED);
498 pa_assert(PA_REFCNT_VALUE(c) >= 1);
502 if (c->version < 13) {
503 pa_context_fail(c, PA_ERR_PROTOCOL);
507 if (pa_tagstruct_getu32(t, &channel) < 0 ||
508 !pa_tagstruct_eof(t)) {
509 pa_context_fail(c, PA_ERR_PROTOCOL);
513 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
516 if (s->state != PA_STREAM_READY)
519 request_auto_timing_update(s, TRUE);
521 if (s->started_callback)
522 s->started_callback(s, s->suspended_userdata);
528 void pa_command_request(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
530 pa_context *c = userdata;
531 uint32_t bytes, channel;
534 pa_assert(command == PA_COMMAND_REQUEST);
537 pa_assert(PA_REFCNT_VALUE(c) >= 1);
541 if (pa_tagstruct_getu32(t, &channel) < 0 ||
542 pa_tagstruct_getu32(t, &bytes) < 0 ||
543 !pa_tagstruct_eof(t)) {
544 pa_context_fail(c, PA_ERR_PROTOCOL);
548 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
551 if (s->state != PA_STREAM_READY)
554 s->requested_bytes += bytes;
556 if (s->requested_bytes > 0 && s->write_callback)
557 s->write_callback(s, s->requested_bytes, s->write_userdata);
563 void pa_command_overflow_or_underflow(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
565 pa_context *c = userdata;
569 pa_assert(command == PA_COMMAND_OVERFLOW || command == PA_COMMAND_UNDERFLOW);
572 pa_assert(PA_REFCNT_VALUE(c) >= 1);
576 if (pa_tagstruct_getu32(t, &channel) < 0 ||
577 !pa_tagstruct_eof(t)) {
578 pa_context_fail(c, PA_ERR_PROTOCOL);
582 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
585 if (s->state != PA_STREAM_READY)
589 if (s->direction == PA_STREAM_PLAYBACK && s->buffer_attr.prebuf > 0) {
590 pa_usec_t x = pa_rtclock_usec();
592 if (s->timing_info_valid)
593 x -= s->timing_info.transport_usec;
595 pa_smoother_pause(s->smoother, x);
598 request_auto_timing_update(s, TRUE);
600 if (s->state == PA_STREAM_READY) {
602 if (command == PA_COMMAND_OVERFLOW) {
603 if (s->overflow_callback)
604 s->overflow_callback(s, s->overflow_userdata);
605 } else if (command == PA_COMMAND_UNDERFLOW) {
606 if (s->underflow_callback)
607 s->underflow_callback(s, s->underflow_userdata);
615 static void invalidate_indexes(pa_stream *s, pa_bool_t r, pa_bool_t w) {
617 pa_assert(PA_REFCNT_VALUE(s) >= 1);
619 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
621 if (s->state != PA_STREAM_READY)
625 s->write_index_not_before = s->context->ctag;
627 if (s->timing_info_valid)
628 s->timing_info.write_index_corrupt = 1;
630 /* pa_log("write_index invalidated"); */
634 s->read_index_not_before = s->context->ctag;
636 if (s->timing_info_valid)
637 s->timing_info.read_index_corrupt = 1;
639 /* pa_log("read_index invalidated"); */
642 request_auto_timing_update(s, TRUE);
645 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) {
646 pa_stream *s = userdata;
649 pa_assert(PA_REFCNT_VALUE(s) >= 1);
652 request_auto_timing_update(s, FALSE);
656 static void create_stream_complete(pa_stream *s) {
658 pa_assert(PA_REFCNT_VALUE(s) >= 1);
659 pa_assert(s->state == PA_STREAM_CREATING);
661 pa_stream_set_state(s, PA_STREAM_READY);
663 if (s->requested_bytes > 0 && s->write_callback)
664 s->write_callback(s, s->requested_bytes, s->write_userdata);
666 if (s->flags & PA_STREAM_AUTO_TIMING_UPDATE) {
668 pa_gettimeofday(&tv);
669 tv.tv_usec += LATENCY_IPOL_INTERVAL_USEC; /* every 100 ms */
670 pa_assert(!s->auto_timing_update_event);
671 s->auto_timing_update_event = s->mainloop->time_new(s->mainloop, &tv, &auto_timing_update_callback, s);
673 request_auto_timing_update(s, TRUE);
677 static void automatic_buffer_attr(pa_stream *s, pa_buffer_attr *attr, const pa_sample_spec *ss) {
682 if (s->context->version >= 13)
685 /* Version older than 0.9.10 didn't do server side buffer_attr
686 * selection, hence we have to fake it on the client side */
688 if (!attr->maxlength <= 0)
689 attr->maxlength = 4*1024*1024; /* 4MB is the maximum queue length PulseAudio <= 0.9.9 supported. */
691 if (!attr->tlength <= 0)
692 attr->tlength = pa_bytes_per_second(ss)*2; /* 2s of buffering */
694 if (!attr->minreq <= 0)
695 attr->minreq = (2*attr->tlength)/10; /* Ask for more data when there are only 200ms left in the playback buffer */
698 attr->prebuf = attr->tlength; /* Start to play only when the playback is fully filled up once */
701 attr->fragsize = attr->tlength; /* Pass data to the app only when the buffer is filled up once */
704 void pa_create_stream_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
705 pa_stream *s = userdata;
709 pa_assert(PA_REFCNT_VALUE(s) >= 1);
710 pa_assert(s->state == PA_STREAM_CREATING);
714 if (command != PA_COMMAND_REPLY) {
715 if (pa_context_handle_error(s->context, command, t, FALSE) < 0)
718 pa_stream_set_state(s, PA_STREAM_FAILED);
722 if (pa_tagstruct_getu32(t, &s->channel) < 0 ||
723 s->channel == PA_INVALID_INDEX ||
724 ((s->direction != PA_STREAM_UPLOAD) && (pa_tagstruct_getu32(t, &s->stream_index) < 0 || s->stream_index == PA_INVALID_INDEX)) ||
725 ((s->direction != PA_STREAM_RECORD) && pa_tagstruct_getu32(t, &s->requested_bytes) < 0)) {
726 pa_context_fail(s->context, PA_ERR_PROTOCOL);
730 if (s->context->version >= 9) {
731 if (s->direction == PA_STREAM_PLAYBACK) {
732 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
733 pa_tagstruct_getu32(t, &s->buffer_attr.tlength) < 0 ||
734 pa_tagstruct_getu32(t, &s->buffer_attr.prebuf) < 0 ||
735 pa_tagstruct_getu32(t, &s->buffer_attr.minreq) < 0) {
736 pa_context_fail(s->context, PA_ERR_PROTOCOL);
739 } else if (s->direction == PA_STREAM_RECORD) {
740 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
741 pa_tagstruct_getu32(t, &s->buffer_attr.fragsize) < 0) {
742 pa_context_fail(s->context, PA_ERR_PROTOCOL);
748 if (s->context->version >= 12 && s->direction != PA_STREAM_UPLOAD) {
751 const char *dn = NULL;
754 if (pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
755 pa_tagstruct_get_channel_map(t, &cm) < 0 ||
756 pa_tagstruct_getu32(t, &s->device_index) < 0 ||
757 pa_tagstruct_gets(t, &dn) < 0 ||
758 pa_tagstruct_get_boolean(t, &suspended) < 0) {
759 pa_context_fail(s->context, PA_ERR_PROTOCOL);
763 if (!dn || s->device_index == PA_INVALID_INDEX ||
764 ss.channels != cm.channels ||
765 !pa_channel_map_valid(&cm) ||
766 !pa_sample_spec_valid(&ss) ||
767 (!(s->flags & PA_STREAM_FIX_FORMAT) && ss.format != s->sample_spec.format) ||
768 (!(s->flags & PA_STREAM_FIX_RATE) && ss.rate != s->sample_spec.rate) ||
769 (!(s->flags & PA_STREAM_FIX_CHANNELS) && !pa_channel_map_equal(&cm, &s->channel_map))) {
770 pa_context_fail(s->context, PA_ERR_PROTOCOL);
774 pa_xfree(s->device_name);
775 s->device_name = pa_xstrdup(dn);
776 s->suspended = suspended;
782 if (s->context->version >= 13 && s->direction != PA_STREAM_UPLOAD) {
785 if (pa_tagstruct_get_usec(t, &usec) < 0) {
786 pa_context_fail(s->context, PA_ERR_PROTOCOL);
790 if (s->direction == PA_STREAM_RECORD)
791 s->timing_info.configured_source_usec = usec;
793 s->timing_info.configured_sink_usec = usec;
796 if (!pa_tagstruct_eof(t)) {
797 pa_context_fail(s->context, PA_ERR_PROTOCOL);
801 if (s->direction == PA_STREAM_RECORD) {
802 pa_assert(!s->record_memblockq);
804 s->record_memblockq = pa_memblockq_new(
806 s->buffer_attr.maxlength,
808 pa_frame_size(&s->sample_spec),
815 s->channel_valid = TRUE;
816 pa_dynarray_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, s->channel, s);
818 create_stream_complete(s);
824 static int create_stream(
825 pa_stream_direction_t direction,
828 const pa_buffer_attr *attr,
829 pa_stream_flags_t flags,
830 const pa_cvolume *volume,
831 pa_stream *sync_stream) {
837 pa_assert(PA_REFCNT_VALUE(s) >= 1);
838 pa_assert(direction == PA_STREAM_PLAYBACK || direction == PA_STREAM_RECORD);
840 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
841 PA_CHECK_VALIDITY(s->context, !(flags & ~(PA_STREAM_START_CORKED|
842 PA_STREAM_INTERPOLATE_TIMING|
843 PA_STREAM_NOT_MONOTONOUS|
844 PA_STREAM_AUTO_TIMING_UPDATE|
845 PA_STREAM_NO_REMAP_CHANNELS|
846 PA_STREAM_NO_REMIX_CHANNELS|
847 PA_STREAM_FIX_FORMAT|
849 PA_STREAM_FIX_CHANNELS|
851 PA_STREAM_VARIABLE_RATE|
852 PA_STREAM_PEAK_DETECT|
853 PA_STREAM_START_MUTED|
854 PA_STREAM_ADJUST_LATENCY)), PA_ERR_INVALID);
856 PA_CHECK_VALIDITY(s->context, s->context->version >= 12 || !(flags & PA_STREAM_VARIABLE_RATE), PA_ERR_NOTSUPPORTED);
857 PA_CHECK_VALIDITY(s->context, s->context->version >= 13 || !(flags & PA_STREAM_PEAK_DETECT), PA_ERR_NOTSUPPORTED);
858 /* Althought some of the other flags are not supported on older
859 * version, we don't check for them here, because it doesn't hurt
860 * when they are passed but actually not supported. This makes
861 * client development easier */
863 PA_CHECK_VALIDITY(s->context, direction != PA_STREAM_PLAYBACK || !(flags & (PA_STREAM_START_MUTED)), PA_ERR_INVALID);
864 PA_CHECK_VALIDITY(s->context, direction != PA_STREAM_RECORD || !(flags & (PA_STREAM_PEAK_DETECT)), PA_ERR_INVALID);
865 PA_CHECK_VALIDITY(s->context, !volume || volume->channels == s->sample_spec.channels, PA_ERR_INVALID);
866 PA_CHECK_VALIDITY(s->context, !sync_stream || (direction == PA_STREAM_PLAYBACK && sync_stream->direction == PA_STREAM_PLAYBACK), PA_ERR_INVALID);
870 s->direction = direction;
874 s->syncid = sync_stream->syncid;
877 s->buffer_attr = *attr;
878 automatic_buffer_attr(s, &s->buffer_attr, &s->sample_spec);
880 if (flags & PA_STREAM_INTERPOLATE_TIMING) {
884 pa_smoother_free(s->smoother);
886 s->smoother = pa_smoother_new(SMOOTHER_ADJUST_TIME, SMOOTHER_HISTORY_TIME, !(flags & PA_STREAM_NOT_MONOTONOUS), SMOOTHER_MIN_HISTORY);
888 x = pa_rtclock_usec();
889 pa_smoother_set_time_offset(s->smoother, x);
890 pa_smoother_pause(s->smoother, x);
894 dev = s->direction == PA_STREAM_PLAYBACK ? s->context->conf->default_sink : s->context->conf->default_source;
896 t = pa_tagstruct_command(
898 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM,
901 if (s->context->version < 13)
902 pa_tagstruct_puts(t, pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME));
906 PA_TAG_SAMPLE_SPEC, &s->sample_spec,
907 PA_TAG_CHANNEL_MAP, &s->channel_map,
908 PA_TAG_U32, PA_INVALID_INDEX,
910 PA_TAG_U32, s->buffer_attr.maxlength,
911 PA_TAG_BOOLEAN, !!(flags & PA_STREAM_START_CORKED),
914 if (s->direction == PA_STREAM_PLAYBACK) {
919 PA_TAG_U32, s->buffer_attr.tlength,
920 PA_TAG_U32, s->buffer_attr.prebuf,
921 PA_TAG_U32, s->buffer_attr.minreq,
922 PA_TAG_U32, s->syncid,
926 volume = pa_cvolume_reset(&cv, s->sample_spec.channels);
928 pa_tagstruct_put_cvolume(t, volume);
930 pa_tagstruct_putu32(t, s->buffer_attr.fragsize);
932 if (s->context->version >= 12) {
935 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMAP_CHANNELS,
936 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMIX_CHANNELS,
937 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_FORMAT,
938 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_RATE,
939 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_CHANNELS,
940 PA_TAG_BOOLEAN, flags & PA_STREAM_DONT_MOVE,
941 PA_TAG_BOOLEAN, flags & PA_STREAM_VARIABLE_RATE,
945 if (s->context->version >= 13) {
947 if (s->direction == PA_STREAM_PLAYBACK)
948 pa_tagstruct_put_boolean(t, flags & PA_STREAM_START_MUTED);
950 pa_tagstruct_put_boolean(t, flags & PA_STREAM_PEAK_DETECT);
954 PA_TAG_BOOLEAN, flags & PA_STREAM_ADJUST_LATENCY,
955 PA_TAG_PROPLIST, s->proplist,
959 pa_pstream_send_tagstruct(s->context->pstream, t);
960 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s, NULL);
962 pa_stream_set_state(s, PA_STREAM_CREATING);
968 int pa_stream_connect_playback(
971 const pa_buffer_attr *attr,
972 pa_stream_flags_t flags,
974 pa_stream *sync_stream) {
977 pa_assert(PA_REFCNT_VALUE(s) >= 1);
979 return create_stream(PA_STREAM_PLAYBACK, s, dev, attr, flags, volume, sync_stream);
982 int pa_stream_connect_record(
985 const pa_buffer_attr *attr,
986 pa_stream_flags_t flags) {
989 pa_assert(PA_REFCNT_VALUE(s) >= 1);
991 return create_stream(PA_STREAM_RECORD, s, dev, attr, flags, NULL, NULL);
998 void (*free_cb)(void *p),
1000 pa_seek_mode_t seek) {
1003 pa_seek_mode_t t_seek;
1009 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1012 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1013 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || s->direction == PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1014 PA_CHECK_VALIDITY(s->context, seek <= PA_SEEK_RELATIVE_END, PA_ERR_INVALID);
1015 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || (seek == PA_SEEK_RELATIVE && offset == 0), PA_ERR_INVALID);
1025 while (t_length > 0) {
1029 if (free_cb && !pa_pstream_get_shm(s->context->pstream)) {
1030 chunk.memblock = pa_memblock_new_user(s->context->mempool, (void*) t_data, t_length, free_cb, 1);
1031 chunk.length = t_length;
1035 chunk.length = PA_MIN(t_length, pa_mempool_block_size_max(s->context->mempool));
1036 chunk.memblock = pa_memblock_new(s->context->mempool, chunk.length);
1038 d = pa_memblock_acquire(chunk.memblock);
1039 memcpy(d, t_data, chunk.length);
1040 pa_memblock_release(chunk.memblock);
1043 pa_pstream_send_memblock(s->context->pstream, s->channel, t_offset, t_seek, &chunk);
1046 t_seek = PA_SEEK_RELATIVE;
1048 t_data = (const uint8_t*) t_data + chunk.length;
1049 t_length -= chunk.length;
1051 pa_memblock_unref(chunk.memblock);
1054 if (free_cb && pa_pstream_get_shm(s->context->pstream))
1055 free_cb((void*) data);
1057 if (length < s->requested_bytes)
1058 s->requested_bytes -= length;
1060 s->requested_bytes = 0;
1062 if (s->direction == PA_STREAM_PLAYBACK) {
1064 /* Update latency request correction */
1065 if (s->write_index_corrections[s->current_write_index_correction].valid) {
1067 if (seek == PA_SEEK_ABSOLUTE) {
1068 s->write_index_corrections[s->current_write_index_correction].corrupt = FALSE;
1069 s->write_index_corrections[s->current_write_index_correction].absolute = TRUE;
1070 s->write_index_corrections[s->current_write_index_correction].value = offset + length;
1071 } else if (seek == PA_SEEK_RELATIVE) {
1072 if (!s->write_index_corrections[s->current_write_index_correction].corrupt)
1073 s->write_index_corrections[s->current_write_index_correction].value += offset + length;
1075 s->write_index_corrections[s->current_write_index_correction].corrupt = TRUE;
1078 /* Update the write index in the already available latency data */
1079 if (s->timing_info_valid) {
1081 if (seek == PA_SEEK_ABSOLUTE) {
1082 s->timing_info.write_index_corrupt = FALSE;
1083 s->timing_info.write_index = offset + length;
1084 } else if (seek == PA_SEEK_RELATIVE) {
1085 if (!s->timing_info.write_index_corrupt)
1086 s->timing_info.write_index += offset + length;
1088 s->timing_info.write_index_corrupt = TRUE;
1091 if (!s->timing_info_valid || s->timing_info.write_index_corrupt)
1092 request_auto_timing_update(s, TRUE);
1098 int pa_stream_peek(pa_stream *s, const void **data, size_t *length) {
1100 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1104 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1105 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1107 if (!s->peek_memchunk.memblock) {
1109 if (pa_memblockq_peek(s->record_memblockq, &s->peek_memchunk) < 0) {
1115 s->peek_data = pa_memblock_acquire(s->peek_memchunk.memblock);
1118 pa_assert(s->peek_data);
1119 *data = (uint8_t*) s->peek_data + s->peek_memchunk.index;
1120 *length = s->peek_memchunk.length;
1124 int pa_stream_drop(pa_stream *s) {
1126 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1128 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1129 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1130 PA_CHECK_VALIDITY(s->context, s->peek_memchunk.memblock, PA_ERR_BADSTATE);
1132 pa_memblockq_drop(s->record_memblockq, s->peek_memchunk.length);
1134 /* Fix the simulated local read index */
1135 if (s->timing_info_valid && !s->timing_info.read_index_corrupt)
1136 s->timing_info.read_index += s->peek_memchunk.length;
1138 pa_assert(s->peek_data);
1139 pa_memblock_release(s->peek_memchunk.memblock);
1140 pa_memblock_unref(s->peek_memchunk.memblock);
1141 pa_memchunk_reset(&s->peek_memchunk);
1146 size_t pa_stream_writable_size(pa_stream *s) {
1148 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1150 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
1151 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
1153 return s->requested_bytes;
1156 size_t pa_stream_readable_size(pa_stream *s) {
1158 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1160 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
1161 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
1163 return pa_memblockq_get_length(s->record_memblockq);
1166 pa_operation * pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1172 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1174 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1175 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1177 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1179 t = pa_tagstruct_command(s->context, PA_COMMAND_DRAIN_PLAYBACK_STREAM, &tag);
1180 pa_tagstruct_putu32(t, s->channel);
1181 pa_pstream_send_tagstruct(s->context->pstream, t);
1182 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);
1187 static pa_usec_t calc_time(pa_stream *s, pa_bool_t ignore_transport) {
1191 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1192 pa_assert(s->state == PA_STREAM_READY);
1193 pa_assert(s->direction != PA_STREAM_UPLOAD);
1194 pa_assert(s->timing_info_valid);
1195 pa_assert(s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt);
1196 pa_assert(s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt);
1198 if (s->direction == PA_STREAM_PLAYBACK) {
1199 /* The last byte that was written into the output device
1200 * had this time value associated */
1201 usec = pa_bytes_to_usec(s->timing_info.read_index < 0 ? 0 : (uint64_t) s->timing_info.read_index, &s->sample_spec);
1203 if (!s->corked && !s->suspended) {
1205 if (!ignore_transport)
1206 /* Because the latency info took a little time to come
1207 * to us, we assume that the real output time is actually
1209 usec += s->timing_info.transport_usec;
1211 /* However, the output device usually maintains a buffer
1212 too, hence the real sample currently played is a little
1214 if (s->timing_info.sink_usec >= usec)
1217 usec -= s->timing_info.sink_usec;
1220 } else if (s->direction == PA_STREAM_RECORD) {
1221 /* The last byte written into the server side queue had
1222 * this time value associated */
1223 usec = pa_bytes_to_usec(s->timing_info.write_index < 0 ? 0 : (uint64_t) s->timing_info.write_index, &s->sample_spec);
1225 if (!s->corked && !s->suspended) {
1227 if (!ignore_transport)
1228 /* Add transport latency */
1229 usec += s->timing_info.transport_usec;
1231 /* Add latency of data in device buffer */
1232 usec += s->timing_info.source_usec;
1234 /* If this is a monitor source, we need to correct the
1235 * time by the playback device buffer */
1236 if (s->timing_info.sink_usec >= usec)
1239 usec -= s->timing_info.sink_usec;
1246 static void stream_get_timing_info_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1247 pa_operation *o = userdata;
1248 struct timeval local, remote, now;
1250 pa_bool_t playing = FALSE;
1251 uint64_t underrun_for = 0, playing_for = 0;
1255 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1257 if (!o->context || !o->stream)
1260 i = &o->stream->timing_info;
1262 /* 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); */
1264 o->stream->timing_info_valid = FALSE;
1265 i->write_index_corrupt = FALSE;
1266 i->read_index_corrupt = FALSE;
1268 /* pa_log("timing update %u\n", tag); */
1270 if (command != PA_COMMAND_REPLY) {
1271 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
1276 if (pa_tagstruct_get_usec(t, &i->sink_usec) < 0 ||
1277 pa_tagstruct_get_usec(t, &i->source_usec) < 0 ||
1278 pa_tagstruct_get_boolean(t, &playing) < 0 ||
1279 pa_tagstruct_get_timeval(t, &local) < 0 ||
1280 pa_tagstruct_get_timeval(t, &remote) < 0 ||
1281 pa_tagstruct_gets64(t, &i->write_index) < 0 ||
1282 pa_tagstruct_gets64(t, &i->read_index) < 0) {
1284 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1288 if (o->context->version >= 13 &&
1289 o->stream->direction == PA_STREAM_PLAYBACK)
1290 if (pa_tagstruct_getu64(t, &underrun_for) < 0 ||
1291 pa_tagstruct_getu64(t, &playing_for) < 0) {
1293 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1298 if (!pa_tagstruct_eof(t)) {
1299 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1303 o->stream->timing_info_valid = TRUE;
1304 i->playing = (int) playing;
1305 i->since_underrun = playing ? playing_for : underrun_for;
1307 pa_gettimeofday(&now);
1309 /* Calculcate timestamps */
1310 if (pa_timeval_cmp(&local, &remote) <= 0 && pa_timeval_cmp(&remote, &now) <= 0) {
1311 /* local and remote seem to have synchronized clocks */
1313 if (o->stream->direction == PA_STREAM_PLAYBACK)
1314 i->transport_usec = pa_timeval_diff(&remote, &local);
1316 i->transport_usec = pa_timeval_diff(&now, &remote);
1318 i->synchronized_clocks = TRUE;
1319 i->timestamp = remote;
1321 /* clocks are not synchronized, let's estimate latency then */
1322 i->transport_usec = pa_timeval_diff(&now, &local)/2;
1323 i->synchronized_clocks = FALSE;
1324 i->timestamp = local;
1325 pa_timeval_add(&i->timestamp, i->transport_usec);
1328 /* Invalidate read and write indexes if necessary */
1329 if (tag < o->stream->read_index_not_before)
1330 i->read_index_corrupt = TRUE;
1332 if (tag < o->stream->write_index_not_before)
1333 i->write_index_corrupt = TRUE;
1335 if (o->stream->direction == PA_STREAM_PLAYBACK) {
1336 /* Write index correction */
1339 uint32_t ctag = tag;
1341 /* Go through the saved correction values and add up the total correction.*/
1343 for (n = 0, j = o->stream->current_write_index_correction+1;
1344 n < PA_MAX_WRITE_INDEX_CORRECTIONS;
1345 n++, j = (j + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS) {
1347 /* Step over invalid data or out-of-date data */
1348 if (!o->stream->write_index_corrections[j].valid ||
1349 o->stream->write_index_corrections[j].tag < ctag)
1352 /* Make sure that everything is in order */
1353 ctag = o->stream->write_index_corrections[j].tag+1;
1355 /* Now fix the write index */
1356 if (o->stream->write_index_corrections[j].corrupt) {
1357 /* A corrupting seek was made */
1359 i->write_index_corrupt = TRUE;
1360 } else if (o->stream->write_index_corrections[j].absolute) {
1361 /* An absolute seek was made */
1362 i->write_index = o->stream->write_index_corrections[j].value;
1363 i->write_index_corrupt = FALSE;
1364 } else if (!i->write_index_corrupt) {
1365 /* A relative seek was made */
1366 i->write_index += o->stream->write_index_corrections[j].value;
1371 if (o->stream->direction == PA_STREAM_RECORD) {
1372 /* Read index correction */
1374 if (!i->read_index_corrupt)
1375 i->read_index -= pa_memblockq_get_length(o->stream->record_memblockq);
1378 /* 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); */
1380 /* Clear old correction entries */
1381 if (o->stream->direction == PA_STREAM_PLAYBACK) {
1384 for (n = 0; n < PA_MAX_WRITE_INDEX_CORRECTIONS; n++) {
1385 if (!o->stream->write_index_corrections[n].valid)
1388 if (o->stream->write_index_corrections[n].tag <= tag)
1389 o->stream->write_index_corrections[n].valid = FALSE;
1393 /* Update smoother */
1394 if (o->stream->smoother) {
1397 u = x = pa_rtclock_usec() - i->transport_usec;
1399 if (o->stream->direction == PA_STREAM_PLAYBACK &&
1400 o->context->version >= 13) {
1403 /* If we weren't playing then it will take some time
1404 * until the audio will actually come out through the
1405 * speakers. Since we follow that timing here, we need
1406 * to try to fix this up */
1408 su = pa_bytes_to_usec(i->since_underrun, &o->stream->sample_spec);
1410 if (su < i->sink_usec)
1411 x += i->sink_usec - su;
1415 pa_smoother_pause(o->stream->smoother, x);
1417 /* Update the smoother */
1418 if ((o->stream->direction == PA_STREAM_PLAYBACK && !i->read_index_corrupt) ||
1419 (o->stream->direction == PA_STREAM_RECORD && !i->write_index_corrupt))
1420 pa_smoother_put(o->stream->smoother, u, calc_time(o->stream, TRUE));
1423 pa_smoother_resume(o->stream->smoother, x);
1427 o->stream->auto_timing_update_requested = FALSE;
1429 if (o->stream->latency_update_callback)
1430 o->stream->latency_update_callback(o->stream, o->stream->latency_update_userdata);
1432 if (o->callback && o->stream && o->stream->state == PA_STREAM_READY) {
1433 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1434 cb(o->stream, o->stream->timing_info_valid, o->userdata);
1439 pa_operation_done(o);
1440 pa_operation_unref(o);
1443 pa_operation* pa_stream_update_timing_info(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1451 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1453 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1454 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1456 if (s->direction == PA_STREAM_PLAYBACK) {
1457 /* Find a place to store the write_index correction data for this entry */
1458 cidx = (s->current_write_index_correction + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS;
1460 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
1461 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !s->write_index_corrections[cidx].valid, PA_ERR_INTERNAL);
1463 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1465 t = pa_tagstruct_command(
1467 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_GET_PLAYBACK_LATENCY : PA_COMMAND_GET_RECORD_LATENCY,
1469 pa_tagstruct_putu32(t, s->channel);
1470 pa_tagstruct_put_timeval(t, pa_gettimeofday(&now));
1472 pa_pstream_send_tagstruct(s->context->pstream, t);
1473 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);
1475 if (s->direction == PA_STREAM_PLAYBACK) {
1476 /* Fill in initial correction data */
1478 s->current_write_index_correction = cidx;
1480 s->write_index_corrections[cidx].valid = TRUE;
1481 s->write_index_corrections[cidx].absolute = FALSE;
1482 s->write_index_corrections[cidx].corrupt = FALSE;
1483 s->write_index_corrections[cidx].tag = tag;
1484 s->write_index_corrections[cidx].value = 0;
1490 void pa_stream_disconnect_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
1491 pa_stream *s = userdata;
1495 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1499 if (command != PA_COMMAND_REPLY) {
1500 if (pa_context_handle_error(s->context, command, t, FALSE) < 0)
1503 pa_stream_set_state(s, PA_STREAM_FAILED);
1505 } else if (!pa_tagstruct_eof(t)) {
1506 pa_context_fail(s->context, PA_ERR_PROTOCOL);
1510 pa_stream_set_state(s, PA_STREAM_TERMINATED);
1516 int pa_stream_disconnect(pa_stream *s) {
1521 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1523 PA_CHECK_VALIDITY(s->context, s->channel_valid, PA_ERR_BADSTATE);
1524 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1528 t = pa_tagstruct_command(
1530 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_DELETE_PLAYBACK_STREAM :
1531 (s->direction == PA_STREAM_RECORD ? PA_COMMAND_DELETE_RECORD_STREAM : PA_COMMAND_DELETE_UPLOAD_STREAM),
1533 pa_tagstruct_putu32(t, s->channel);
1534 pa_pstream_send_tagstruct(s->context->pstream, t);
1535 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s, NULL);
1541 void pa_stream_set_read_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1543 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1545 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1548 s->read_callback = cb;
1549 s->read_userdata = userdata;
1552 void pa_stream_set_write_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1554 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1556 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1559 s->write_callback = cb;
1560 s->write_userdata = userdata;
1563 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1565 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1567 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1570 s->state_callback = cb;
1571 s->state_userdata = userdata;
1574 void pa_stream_set_overflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1576 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1578 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1581 s->overflow_callback = cb;
1582 s->overflow_userdata = userdata;
1585 void pa_stream_set_underflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1587 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1589 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1592 s->underflow_callback = cb;
1593 s->underflow_userdata = userdata;
1596 void pa_stream_set_latency_update_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1598 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1600 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1603 s->latency_update_callback = cb;
1604 s->latency_update_userdata = userdata;
1607 void pa_stream_set_moved_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1609 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1611 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1614 s->moved_callback = cb;
1615 s->moved_userdata = userdata;
1618 void pa_stream_set_suspended_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1620 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1622 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1625 s->suspended_callback = cb;
1626 s->suspended_userdata = userdata;
1629 void pa_stream_set_started_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1631 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1633 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1636 s->started_callback = cb;
1637 s->started_userdata = userdata;
1640 void pa_stream_simple_ack_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
1641 pa_operation *o = userdata;
1646 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1651 if (command != PA_COMMAND_REPLY) {
1652 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
1656 } else if (!pa_tagstruct_eof(t)) {
1657 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1662 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1663 cb(o->stream, success, o->userdata);
1667 pa_operation_done(o);
1668 pa_operation_unref(o);
1671 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata) {
1677 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1679 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1680 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1684 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1686 t = pa_tagstruct_command(
1688 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CORK_PLAYBACK_STREAM : PA_COMMAND_CORK_RECORD_STREAM,
1690 pa_tagstruct_putu32(t, s->channel);
1691 pa_tagstruct_put_boolean(t, !!b);
1692 pa_pstream_send_tagstruct(s->context->pstream, t);
1693 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);
1696 pa_usec_t x = pa_rtclock_usec();
1698 if (s->timing_info_valid)
1699 x += s->timing_info.transport_usec;
1701 if (s->suspended || s->corked)
1702 pa_smoother_pause(s->smoother, x);
1705 if (s->direction == PA_STREAM_PLAYBACK)
1706 invalidate_indexes(s, TRUE, FALSE);
1711 static pa_operation* stream_send_simple_command(pa_stream *s, uint32_t command, pa_stream_success_cb_t cb, void *userdata) {
1717 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1719 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1721 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1723 t = pa_tagstruct_command(s->context, command, &tag);
1724 pa_tagstruct_putu32(t, s->channel);
1725 pa_pstream_send_tagstruct(s->context->pstream, t);
1726 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);
1731 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1735 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1737 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1738 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1740 if ((o = stream_send_simple_command(s, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_FLUSH_PLAYBACK_STREAM : PA_COMMAND_FLUSH_RECORD_STREAM, cb, userdata))) {
1742 if (s->direction == PA_STREAM_PLAYBACK) {
1743 if (s->write_index_corrections[s->current_write_index_correction].valid)
1744 s->write_index_corrections[s->current_write_index_correction].corrupt = TRUE;
1746 if (s->timing_info_valid)
1747 s->timing_info.write_index_corrupt = TRUE;
1749 if (s->buffer_attr.prebuf > 0)
1750 invalidate_indexes(s, TRUE, FALSE);
1752 request_auto_timing_update(s, TRUE);
1754 if (s->smoother && s->buffer_attr.prebuf > 0) {
1755 pa_usec_t x = pa_rtclock_usec();
1757 if (s->timing_info_valid)
1758 x += s->timing_info.transport_usec;
1760 pa_smoother_pause(s->smoother, x);
1764 invalidate_indexes(s, FALSE, TRUE);
1770 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1774 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1776 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1777 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1778 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1780 if ((o = stream_send_simple_command(s, PA_COMMAND_PREBUF_PLAYBACK_STREAM, cb, userdata)))
1781 invalidate_indexes(s, TRUE, FALSE);
1786 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1790 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1792 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1793 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1794 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1796 if ((o = stream_send_simple_command(s, PA_COMMAND_TRIGGER_PLAYBACK_STREAM, cb, userdata)))
1797 invalidate_indexes(s, TRUE, FALSE);
1802 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata) {
1806 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1809 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1810 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1812 if (s->context->version >= 13) {
1813 pa_proplist *p = pa_proplist_new();
1815 pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, name);
1816 o = pa_stream_proplist_update(s, PA_UPDATE_REPLACE, p, cb, userdata);
1817 pa_proplist_free(p);
1822 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1823 t = pa_tagstruct_command(
1825 s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_NAME : PA_COMMAND_SET_PLAYBACK_STREAM_NAME,
1827 pa_tagstruct_putu32(t, s->channel);
1828 pa_tagstruct_puts(t, name);
1829 pa_pstream_send_tagstruct(s->context->pstream, t);
1830 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);
1836 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec) {
1840 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1842 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1843 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1844 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1845 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1846 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1849 usec = pa_smoother_get(s->smoother, pa_rtclock_usec());
1851 usec = calc_time(s, FALSE);
1853 /* Make sure the time runs monotonically */
1854 if (!(s->flags & PA_STREAM_NOT_MONOTONOUS)) {
1855 if (usec < s->previous_time)
1856 usec = s->previous_time;
1858 s->previous_time = usec;
1867 static pa_usec_t time_counter_diff(pa_stream *s, pa_usec_t a, pa_usec_t b, int *negative) {
1869 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1877 if (negative && s->direction == PA_STREAM_RECORD) {
1885 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative) {
1891 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1894 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1895 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1896 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1897 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1898 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1900 if ((r = pa_stream_get_time(s, &t)) < 0)
1903 if (s->direction == PA_STREAM_PLAYBACK)
1904 cindex = s->timing_info.write_index;
1906 cindex = s->timing_info.read_index;
1911 c = pa_bytes_to_usec(cindex, &s->sample_spec);
1913 if (s->direction == PA_STREAM_PLAYBACK)
1914 *r_usec = time_counter_diff(s, c, t, negative);
1916 *r_usec = time_counter_diff(s, t, c, negative);
1921 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s) {
1923 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1925 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1926 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1927 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->timing_info_valid, PA_ERR_BADSTATE);
1929 return &s->timing_info;
1932 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s) {
1934 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1936 return &s->sample_spec;
1939 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s) {
1941 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1943 return &s->channel_map;
1946 const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s) {
1948 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1950 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1951 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1952 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 9, PA_ERR_NOTSUPPORTED);
1954 return &s->buffer_attr;
1957 static void stream_set_buffer_attr_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
1958 pa_operation *o = userdata;
1963 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1968 if (command != PA_COMMAND_REPLY) {
1969 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
1975 if (o->stream->direction == PA_STREAM_PLAYBACK) {
1976 if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
1977 pa_tagstruct_getu32(t, &o->stream->buffer_attr.tlength) < 0 ||
1978 pa_tagstruct_getu32(t, &o->stream->buffer_attr.prebuf) < 0 ||
1979 pa_tagstruct_getu32(t, &o->stream->buffer_attr.minreq) < 0) {
1980 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1983 } else if (o->stream->direction == PA_STREAM_RECORD) {
1984 if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
1985 pa_tagstruct_getu32(t, &o->stream->buffer_attr.fragsize) < 0) {
1986 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1991 if (!pa_tagstruct_eof(t)) {
1992 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1998 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1999 cb(o->stream, success, o->userdata);
2003 pa_operation_done(o);
2004 pa_operation_unref(o);
2008 pa_operation* pa_stream_set_buffer_attr(pa_stream *s, const pa_buffer_attr *attr, pa_stream_success_cb_t cb, void *userdata) {
2014 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2017 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2018 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2019 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2021 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2023 t = pa_tagstruct_command(
2025 s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR : PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR,
2027 pa_tagstruct_putu32(t, s->channel);
2029 pa_tagstruct_putu32(t, attr->maxlength);
2031 if (s->direction == PA_STREAM_PLAYBACK)
2034 PA_TAG_U32, attr->tlength,
2035 PA_TAG_U32, attr->prebuf,
2036 PA_TAG_U32, attr->minreq,
2039 pa_tagstruct_putu32(t, attr->fragsize);
2041 if (s->context->version >= 13)
2042 pa_tagstruct_put_boolean(t, !!(s->flags & PA_STREAM_ADJUST_LATENCY));
2044 pa_pstream_send_tagstruct(s->context->pstream, t);
2045 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);
2050 uint32_t pa_stream_get_device_index(pa_stream *s) {
2052 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2054 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2055 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2056 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
2057 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->device_index != PA_INVALID_INDEX, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2059 return s->device_index;
2062 const char *pa_stream_get_device_name(pa_stream *s) {
2064 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2066 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2067 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2068 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2069 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->device_name, PA_ERR_BADSTATE);
2071 return s->device_name;
2074 int pa_stream_is_suspended(pa_stream *s) {
2076 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2078 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2079 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2080 PA_CHECK_VALIDITY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2082 return s->suspended;
2085 int pa_stream_is_corked(pa_stream *s) {
2087 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2089 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2090 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2095 static void stream_update_sample_rate_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
2096 pa_operation *o = userdata;
2101 pa_assert(PA_REFCNT_VALUE(o) >= 1);
2106 if (command != PA_COMMAND_REPLY) {
2107 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
2113 if (!pa_tagstruct_eof(t)) {
2114 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2119 o->stream->sample_spec.rate = PA_PTR_TO_UINT(o->private);
2120 pa_assert(pa_sample_spec_valid(&o->stream->sample_spec));
2123 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
2124 cb(o->stream, success, o->userdata);
2128 pa_operation_done(o);
2129 pa_operation_unref(o);
2133 pa_operation *pa_stream_update_sample_rate(pa_stream *s, uint32_t rate, pa_stream_success_cb_t cb, void *userdata) {
2139 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2141 PA_CHECK_VALIDITY_RETURN_NULL(s->context, rate > 0 && rate <= PA_RATE_MAX, PA_ERR_INVALID);
2142 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2143 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2144 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->flags & PA_STREAM_VARIABLE_RATE, PA_ERR_BADSTATE);
2145 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2147 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2148 o->private = PA_UINT_TO_PTR(rate);
2150 t = pa_tagstruct_command(
2152 s->direction == PA_STREAM_RECORD ? PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE : PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE,
2154 pa_tagstruct_putu32(t, s->channel);
2155 pa_tagstruct_putu32(t, rate);
2157 pa_pstream_send_tagstruct(s->context->pstream, t);
2158 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);
2163 pa_operation *pa_stream_proplist_update(pa_stream *s, pa_update_mode_t mode, pa_proplist *p, pa_stream_success_cb_t cb, void *userdata) {
2169 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2171 PA_CHECK_VALIDITY_RETURN_NULL(s->context, mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE, PA_ERR_INVALID);
2172 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2173 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2174 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2176 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2178 t = pa_tagstruct_command(
2180 s->direction == PA_STREAM_RECORD ? PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST : PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST,
2182 pa_tagstruct_putu32(t, s->channel);
2183 pa_tagstruct_putu32(t, (uint32_t) mode);
2184 pa_tagstruct_put_proplist(t, p);
2186 pa_pstream_send_tagstruct(s->context->pstream, t);
2187 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);
2189 /* Please note that we don't update s->proplist here, because we
2190 * don't export that field */
2195 pa_operation *pa_stream_proplist_remove(pa_stream *s, const char *const keys[], pa_stream_success_cb_t cb, void *userdata) {
2199 const char * const*k;
2202 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2204 PA_CHECK_VALIDITY_RETURN_NULL(s->context, keys && keys[0], PA_ERR_INVALID);
2205 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2206 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2207 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2209 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2211 t = pa_tagstruct_command(
2213 s->direction == PA_STREAM_RECORD ? PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST : PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST,
2215 pa_tagstruct_putu32(t, s->channel);
2217 for (k = keys; *k; k++)
2218 pa_tagstruct_puts(t, *k);
2220 pa_tagstruct_puts(t, NULL);
2222 pa_pstream_send_tagstruct(s->context->pstream, t);
2223 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);
2225 /* Please note that we don't update s->proplist here, because we
2226 * don't export that field */