2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/def.h>
32 #include <pulse/timeval.h>
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/pstream-util.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/hashmap.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/rtclock.h>
43 #define LATENCY_IPOL_INTERVAL_USEC (333*PA_USEC_PER_MSEC)
45 #define SMOOTHER_ADJUST_TIME (1000*PA_USEC_PER_MSEC)
46 #define SMOOTHER_HISTORY_TIME (5000*PA_USEC_PER_MSEC)
47 #define SMOOTHER_MIN_HISTORY (4)
49 pa_stream *pa_stream_new(pa_context *c, const char *name, const pa_sample_spec *ss, const pa_channel_map *map) {
50 return pa_stream_new_with_proplist(c, name, ss, map, NULL);
53 static void reset_callbacks(pa_stream *s) {
54 s->read_callback = NULL;
55 s->read_userdata = NULL;
56 s->write_callback = NULL;
57 s->write_userdata = NULL;
58 s->state_callback = NULL;
59 s->state_userdata = NULL;
60 s->overflow_callback = NULL;
61 s->overflow_userdata = NULL;
62 s->underflow_callback = NULL;
63 s->underflow_userdata = NULL;
64 s->latency_update_callback = NULL;
65 s->latency_update_userdata = NULL;
66 s->moved_callback = NULL;
67 s->moved_userdata = NULL;
68 s->suspended_callback = NULL;
69 s->suspended_userdata = NULL;
70 s->started_callback = NULL;
71 s->started_userdata = NULL;
74 pa_stream *pa_stream_new_with_proplist(
77 const pa_sample_spec *ss,
78 const pa_channel_map *map,
86 pa_assert(PA_REFCNT_VALUE(c) >= 1);
88 PA_CHECK_VALIDITY_RETURN_NULL(c, ss && pa_sample_spec_valid(ss), PA_ERR_INVALID);
89 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 12 || (ss->format != PA_SAMPLE_S32LE && ss->format != PA_SAMPLE_S32BE), PA_ERR_NOTSUPPORTED);
90 PA_CHECK_VALIDITY_RETURN_NULL(c, !map || (pa_channel_map_valid(map) && map->channels == ss->channels), PA_ERR_INVALID);
91 PA_CHECK_VALIDITY_RETURN_NULL(c, name || (p && pa_proplist_contains(p, PA_PROP_MEDIA_NAME)), PA_ERR_INVALID);
94 PA_CHECK_VALIDITY_RETURN_NULL(c, map = pa_channel_map_init_auto(&tmap, ss->channels, PA_CHANNEL_MAP_DEFAULT), PA_ERR_INVALID);
96 s = pa_xnew(pa_stream, 1);
99 s->mainloop = c->mainloop;
101 s->direction = PA_STREAM_NODIRECTION;
102 s->state = PA_STREAM_UNCONNECTED;
105 s->sample_spec = *ss;
106 s->channel_map = *map;
108 s->direct_on_input = PA_INVALID_INDEX;
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 /* We initialize der target length here, so that if the user
123 * passes no explicit buffering metrics the default is similar to
124 * what older PA versions provided. */
126 s->buffer_attr.maxlength = (uint32_t) -1;
127 s->buffer_attr.tlength = (uint32_t) pa_usec_to_bytes(250*PA_USEC_PER_MSEC, ss); /* 250ms of buffering */
128 s->buffer_attr.minreq = (uint32_t) -1;
129 s->buffer_attr.prebuf = (uint32_t) -1;
130 s->buffer_attr.fragsize = (uint32_t) -1;
132 s->device_index = PA_INVALID_INDEX;
133 s->device_name = NULL;
134 s->suspended = FALSE;
136 pa_memchunk_reset(&s->peek_memchunk);
139 s->record_memblockq = NULL;
143 memset(&s->timing_info, 0, sizeof(s->timing_info));
144 s->timing_info_valid = FALSE;
146 s->previous_time = 0;
148 s->read_index_not_before = 0;
149 s->write_index_not_before = 0;
150 for (i = 0; i < PA_MAX_WRITE_INDEX_CORRECTIONS; i++)
151 s->write_index_corrections[i].valid = 0;
152 s->current_write_index_correction = 0;
154 s->auto_timing_update_event = NULL;
155 s->auto_timing_update_requested = FALSE;
161 /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
162 PA_LLIST_PREPEND(pa_stream, c->streams, s);
168 static void stream_unlink(pa_stream *s) {
175 /* Detach from context */
177 /* Unref all operatio object that point to us */
178 for (o = s->context->operations; o; o = n) {
182 pa_operation_cancel(o);
185 /* Drop all outstanding replies for this stream */
186 if (s->context->pdispatch)
187 pa_pdispatch_unregister_reply(s->context->pdispatch, s);
189 if (s->channel_valid) {
190 pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, NULL);
192 s->channel_valid = FALSE;
195 PA_LLIST_REMOVE(pa_stream, s->context->streams, s);
200 if (s->auto_timing_update_event) {
201 pa_assert(s->mainloop);
202 s->mainloop->time_free(s->auto_timing_update_event);
208 static void stream_free(pa_stream *s) {
213 if (s->peek_memchunk.memblock) {
215 pa_memblock_release(s->peek_memchunk.memblock);
216 pa_memblock_unref(s->peek_memchunk.memblock);
219 if (s->record_memblockq)
220 pa_memblockq_free(s->record_memblockq);
223 pa_proplist_free(s->proplist);
226 pa_smoother_free(s->smoother);
228 pa_xfree(s->device_name);
232 void pa_stream_unref(pa_stream *s) {
234 pa_assert(PA_REFCNT_VALUE(s) >= 1);
236 if (PA_REFCNT_DEC(s) <= 0)
240 pa_stream* pa_stream_ref(pa_stream *s) {
242 pa_assert(PA_REFCNT_VALUE(s) >= 1);
248 pa_stream_state_t pa_stream_get_state(pa_stream *s) {
250 pa_assert(PA_REFCNT_VALUE(s) >= 1);
255 pa_context* pa_stream_get_context(pa_stream *s) {
257 pa_assert(PA_REFCNT_VALUE(s) >= 1);
262 uint32_t pa_stream_get_index(pa_stream *s) {
264 pa_assert(PA_REFCNT_VALUE(s) >= 1);
266 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
268 return s->stream_index;
271 void pa_stream_set_state(pa_stream *s, pa_stream_state_t st) {
273 pa_assert(PA_REFCNT_VALUE(s) >= 1);
282 if (s->state_callback)
283 s->state_callback(s, s->state_userdata);
285 if ((st == PA_STREAM_FAILED || st == PA_STREAM_TERMINATED))
291 static void request_auto_timing_update(pa_stream *s, pa_bool_t force) {
293 pa_assert(PA_REFCNT_VALUE(s) >= 1);
295 if (!(s->flags & PA_STREAM_AUTO_TIMING_UPDATE))
298 if (s->state == PA_STREAM_READY &&
299 (force || !s->auto_timing_update_requested)) {
302 /* pa_log("automatically requesting new timing data"); */
304 if ((o = pa_stream_update_timing_info(s, NULL, NULL))) {
305 pa_operation_unref(o);
306 s->auto_timing_update_requested = TRUE;
310 if (s->auto_timing_update_event) {
312 pa_gettimeofday(&next);
313 pa_timeval_add(&next, LATENCY_IPOL_INTERVAL_USEC);
314 s->mainloop->time_restart(s->auto_timing_update_event, &next);
318 void pa_command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
319 pa_context *c = userdata;
324 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_KILLED || command == PA_COMMAND_RECORD_STREAM_KILLED);
327 pa_assert(PA_REFCNT_VALUE(c) >= 1);
331 if (pa_tagstruct_getu32(t, &channel) < 0 ||
332 !pa_tagstruct_eof(t)) {
333 pa_context_fail(c, PA_ERR_PROTOCOL);
337 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel)))
340 if (s->state != PA_STREAM_READY)
343 pa_context_set_error(c, PA_ERR_KILLED);
344 pa_stream_set_state(s, PA_STREAM_FAILED);
350 static void check_smoother_status(pa_stream *s, pa_bool_t aposteriori, pa_bool_t force_start, pa_bool_t force_stop) {
354 pa_assert(!force_start || !force_stop);
359 x = pa_rtclock_usec();
361 if (s->timing_info_valid) {
363 x -= s->timing_info.transport_usec;
365 x += s->timing_info.transport_usec;
367 if (s->direction == PA_STREAM_PLAYBACK)
368 /* it takes a while until the pause/resume is actually
370 x += s->timing_info.sink_usec;
372 /* Data froma while back will be dropped */
373 x -= s->timing_info.source_usec;
376 if (s->suspended || s->corked || force_stop)
377 pa_smoother_pause(s->smoother, x);
378 else if (force_start || s->buffer_attr.prebuf == 0)
379 pa_smoother_resume(s->smoother, x);
381 /* Please note that we have no idea if playback actually started
382 * if prebuf is non-zero! */
385 void pa_command_stream_moved(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
386 pa_context *c = userdata;
393 uint32_t maxlength = 0, fragsize = 0, minreq = 0, tlength = 0, prebuf = 0;
396 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_MOVED || command == PA_COMMAND_RECORD_STREAM_MOVED);
399 pa_assert(PA_REFCNT_VALUE(c) >= 1);
403 if (c->version < 12) {
404 pa_context_fail(c, PA_ERR_PROTOCOL);
408 if (pa_tagstruct_getu32(t, &channel) < 0 ||
409 pa_tagstruct_getu32(t, &di) < 0 ||
410 pa_tagstruct_gets(t, &dn) < 0 ||
411 pa_tagstruct_get_boolean(t, &suspended) < 0) {
412 pa_context_fail(c, PA_ERR_PROTOCOL);
416 if (c->version >= 13) {
418 if (command == PA_COMMAND_RECORD_STREAM_MOVED) {
419 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
420 pa_tagstruct_getu32(t, &fragsize) < 0 ||
421 pa_tagstruct_get_usec(t, &usec) < 0) {
422 pa_context_fail(c, PA_ERR_PROTOCOL);
426 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
427 pa_tagstruct_getu32(t, &tlength) < 0 ||
428 pa_tagstruct_getu32(t, &prebuf) < 0 ||
429 pa_tagstruct_getu32(t, &minreq) < 0 ||
430 pa_tagstruct_get_usec(t, &usec) < 0) {
431 pa_context_fail(c, PA_ERR_PROTOCOL);
437 if (!pa_tagstruct_eof(t)) {
438 pa_context_fail(c, PA_ERR_PROTOCOL);
442 if (!dn || di == PA_INVALID_INDEX) {
443 pa_context_fail(c, PA_ERR_PROTOCOL);
447 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_MOVED ? c->playback_streams : c->record_streams, channel)))
450 if (s->state != PA_STREAM_READY)
453 if (c->version >= 13) {
454 if (s->direction == PA_STREAM_RECORD)
455 s->timing_info.configured_source_usec = usec;
457 s->timing_info.configured_sink_usec = usec;
459 s->buffer_attr.maxlength = maxlength;
460 s->buffer_attr.fragsize = fragsize;
461 s->buffer_attr.tlength = tlength;
462 s->buffer_attr.prebuf = prebuf;
463 s->buffer_attr.minreq = minreq;
466 pa_xfree(s->device_name);
467 s->device_name = pa_xstrdup(dn);
468 s->device_index = di;
470 s->suspended = suspended;
472 check_smoother_status(s, TRUE, FALSE, FALSE);
473 request_auto_timing_update(s, TRUE);
475 if (s->moved_callback)
476 s->moved_callback(s, s->moved_userdata);
482 void pa_command_stream_suspended(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
483 pa_context *c = userdata;
489 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED || command == PA_COMMAND_RECORD_STREAM_SUSPENDED);
492 pa_assert(PA_REFCNT_VALUE(c) >= 1);
496 if (c->version < 12) {
497 pa_context_fail(c, PA_ERR_PROTOCOL);
501 if (pa_tagstruct_getu32(t, &channel) < 0 ||
502 pa_tagstruct_get_boolean(t, &suspended) < 0 ||
503 !pa_tagstruct_eof(t)) {
504 pa_context_fail(c, PA_ERR_PROTOCOL);
508 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED ? c->playback_streams : c->record_streams, channel)))
511 if (s->state != PA_STREAM_READY)
514 s->suspended = suspended;
516 check_smoother_status(s, TRUE, FALSE, FALSE);
517 request_auto_timing_update(s, TRUE);
519 if (s->suspended_callback)
520 s->suspended_callback(s, s->suspended_userdata);
526 void pa_command_stream_started(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
527 pa_context *c = userdata;
532 pa_assert(command == PA_COMMAND_STARTED);
535 pa_assert(PA_REFCNT_VALUE(c) >= 1);
539 if (c->version < 13) {
540 pa_context_fail(c, PA_ERR_PROTOCOL);
544 if (pa_tagstruct_getu32(t, &channel) < 0 ||
545 !pa_tagstruct_eof(t)) {
546 pa_context_fail(c, PA_ERR_PROTOCOL);
550 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
553 if (s->state != PA_STREAM_READY)
556 check_smoother_status(s, TRUE, TRUE, FALSE);
557 request_auto_timing_update(s, TRUE);
559 if (s->started_callback)
560 s->started_callback(s, s->started_userdata);
566 void pa_command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
568 pa_context *c = userdata;
569 uint32_t bytes, channel;
572 pa_assert(command == PA_COMMAND_REQUEST);
575 pa_assert(PA_REFCNT_VALUE(c) >= 1);
579 if (pa_tagstruct_getu32(t, &channel) < 0 ||
580 pa_tagstruct_getu32(t, &bytes) < 0 ||
581 !pa_tagstruct_eof(t)) {
582 pa_context_fail(c, PA_ERR_PROTOCOL);
586 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
589 if (s->state != PA_STREAM_READY)
592 s->requested_bytes += bytes;
594 if (s->requested_bytes > 0 && s->write_callback)
595 s->write_callback(s, s->requested_bytes, s->write_userdata);
601 void pa_command_overflow_or_underflow(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
603 pa_context *c = userdata;
607 pa_assert(command == PA_COMMAND_OVERFLOW || command == PA_COMMAND_UNDERFLOW);
610 pa_assert(PA_REFCNT_VALUE(c) >= 1);
614 if (pa_tagstruct_getu32(t, &channel) < 0 ||
615 !pa_tagstruct_eof(t)) {
616 pa_context_fail(c, PA_ERR_PROTOCOL);
620 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
623 if (s->state != PA_STREAM_READY)
626 if (s->buffer_attr.prebuf > 0)
627 check_smoother_status(s, TRUE, FALSE, TRUE);
629 request_auto_timing_update(s, TRUE);
631 if (command == PA_COMMAND_OVERFLOW) {
632 if (s->overflow_callback)
633 s->overflow_callback(s, s->overflow_userdata);
634 } else if (command == PA_COMMAND_UNDERFLOW) {
635 if (s->underflow_callback)
636 s->underflow_callback(s, s->underflow_userdata);
643 static void invalidate_indexes(pa_stream *s, pa_bool_t r, pa_bool_t w) {
645 pa_assert(PA_REFCNT_VALUE(s) >= 1);
647 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
649 if (s->state != PA_STREAM_READY)
653 s->write_index_not_before = s->context->ctag;
655 if (s->timing_info_valid)
656 s->timing_info.write_index_corrupt = TRUE;
658 /* pa_log("write_index invalidated"); */
662 s->read_index_not_before = s->context->ctag;
664 if (s->timing_info_valid)
665 s->timing_info.read_index_corrupt = TRUE;
667 /* pa_log("read_index invalidated"); */
670 request_auto_timing_update(s, TRUE);
673 static void auto_timing_update_callback(pa_mainloop_api *m, pa_time_event *e, const struct timeval *tv, void *userdata) {
674 pa_stream *s = userdata;
677 pa_assert(PA_REFCNT_VALUE(s) >= 1);
680 request_auto_timing_update(s, FALSE);
684 static void create_stream_complete(pa_stream *s) {
686 pa_assert(PA_REFCNT_VALUE(s) >= 1);
687 pa_assert(s->state == PA_STREAM_CREATING);
689 pa_stream_set_state(s, PA_STREAM_READY);
691 if (s->requested_bytes > 0 && s->write_callback)
692 s->write_callback(s, s->requested_bytes, s->write_userdata);
694 if (s->flags & PA_STREAM_AUTO_TIMING_UPDATE) {
696 pa_gettimeofday(&tv);
697 tv.tv_usec += (suseconds_t) LATENCY_IPOL_INTERVAL_USEC; /* every 100 ms */
698 pa_assert(!s->auto_timing_update_event);
699 s->auto_timing_update_event = s->mainloop->time_new(s->mainloop, &tv, &auto_timing_update_callback, s);
701 request_auto_timing_update(s, TRUE);
704 check_smoother_status(s, TRUE, FALSE, FALSE);
707 static void automatic_buffer_attr(pa_stream *s, pa_buffer_attr *attr, const pa_sample_spec *ss) {
712 if (s->context->version >= 13)
715 /* Version older than 0.9.10 didn't do server side buffer_attr
716 * selection, hence we have to fake it on the client side. */
718 /* We choose fairly conservative values here, to not confuse
719 * old clients with extremely large playback buffers */
721 if (attr->maxlength == (uint32_t) -1)
722 attr->maxlength = 4*1024*1024; /* 4MB is the maximum queue length PulseAudio <= 0.9.9 supported. */
724 if (attr->tlength == (uint32_t) -1)
725 attr->tlength = (uint32_t) pa_usec_to_bytes(250*PA_USEC_PER_MSEC, ss); /* 250ms of buffering */
727 if (attr->minreq == (uint32_t) -1)
728 attr->minreq = (attr->tlength)/5; /* Ask for more data when there are only 200ms left in the playback buffer */
730 if (attr->prebuf == (uint32_t) -1)
731 attr->prebuf = attr->tlength; /* Start to play only when the playback is fully filled up once */
733 if (attr->fragsize == (uint32_t) -1)
734 attr->fragsize = attr->tlength; /* Pass data to the app only when the buffer is filled up once */
737 void pa_create_stream_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
738 pa_stream *s = userdata;
742 pa_assert(PA_REFCNT_VALUE(s) >= 1);
743 pa_assert(s->state == PA_STREAM_CREATING);
747 if (command != PA_COMMAND_REPLY) {
748 if (pa_context_handle_error(s->context, command, t, FALSE) < 0)
751 pa_stream_set_state(s, PA_STREAM_FAILED);
755 if (pa_tagstruct_getu32(t, &s->channel) < 0 ||
756 s->channel == PA_INVALID_INDEX ||
757 ((s->direction != PA_STREAM_UPLOAD) && (pa_tagstruct_getu32(t, &s->stream_index) < 0 || s->stream_index == PA_INVALID_INDEX)) ||
758 ((s->direction != PA_STREAM_RECORD) && pa_tagstruct_getu32(t, &s->requested_bytes) < 0)) {
759 pa_context_fail(s->context, PA_ERR_PROTOCOL);
763 if (s->context->version >= 9) {
764 if (s->direction == PA_STREAM_PLAYBACK) {
765 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
766 pa_tagstruct_getu32(t, &s->buffer_attr.tlength) < 0 ||
767 pa_tagstruct_getu32(t, &s->buffer_attr.prebuf) < 0 ||
768 pa_tagstruct_getu32(t, &s->buffer_attr.minreq) < 0) {
769 pa_context_fail(s->context, PA_ERR_PROTOCOL);
772 } else if (s->direction == PA_STREAM_RECORD) {
773 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
774 pa_tagstruct_getu32(t, &s->buffer_attr.fragsize) < 0) {
775 pa_context_fail(s->context, PA_ERR_PROTOCOL);
781 if (s->context->version >= 12 && s->direction != PA_STREAM_UPLOAD) {
784 const char *dn = NULL;
787 if (pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
788 pa_tagstruct_get_channel_map(t, &cm) < 0 ||
789 pa_tagstruct_getu32(t, &s->device_index) < 0 ||
790 pa_tagstruct_gets(t, &dn) < 0 ||
791 pa_tagstruct_get_boolean(t, &suspended) < 0) {
792 pa_context_fail(s->context, PA_ERR_PROTOCOL);
796 if (!dn || s->device_index == PA_INVALID_INDEX ||
797 ss.channels != cm.channels ||
798 !pa_channel_map_valid(&cm) ||
799 !pa_sample_spec_valid(&ss) ||
800 (!(s->flags & PA_STREAM_FIX_FORMAT) && ss.format != s->sample_spec.format) ||
801 (!(s->flags & PA_STREAM_FIX_RATE) && ss.rate != s->sample_spec.rate) ||
802 (!(s->flags & PA_STREAM_FIX_CHANNELS) && !pa_channel_map_equal(&cm, &s->channel_map))) {
803 pa_context_fail(s->context, PA_ERR_PROTOCOL);
807 pa_xfree(s->device_name);
808 s->device_name = pa_xstrdup(dn);
809 s->suspended = suspended;
815 if (s->context->version >= 13 && s->direction != PA_STREAM_UPLOAD) {
818 if (pa_tagstruct_get_usec(t, &usec) < 0) {
819 pa_context_fail(s->context, PA_ERR_PROTOCOL);
823 if (s->direction == PA_STREAM_RECORD)
824 s->timing_info.configured_source_usec = usec;
826 s->timing_info.configured_sink_usec = usec;
829 if (!pa_tagstruct_eof(t)) {
830 pa_context_fail(s->context, PA_ERR_PROTOCOL);
834 if (s->direction == PA_STREAM_RECORD) {
835 pa_assert(!s->record_memblockq);
837 s->record_memblockq = pa_memblockq_new(
839 s->buffer_attr.maxlength,
841 pa_frame_size(&s->sample_spec),
848 s->channel_valid = TRUE;
849 pa_dynarray_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, s->channel, s);
851 create_stream_complete(s);
857 static int create_stream(
858 pa_stream_direction_t direction,
861 const pa_buffer_attr *attr,
862 pa_stream_flags_t flags,
863 const pa_cvolume *volume,
864 pa_stream *sync_stream) {
868 pa_bool_t volume_set = FALSE;
871 pa_assert(PA_REFCNT_VALUE(s) >= 1);
872 pa_assert(direction == PA_STREAM_PLAYBACK || direction == PA_STREAM_RECORD);
874 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
875 PA_CHECK_VALIDITY(s->context, s->direct_on_input == PA_INVALID_INDEX || direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
876 PA_CHECK_VALIDITY(s->context, !(flags & ~(PA_STREAM_START_CORKED|
877 PA_STREAM_INTERPOLATE_TIMING|
878 PA_STREAM_NOT_MONOTONOUS|
879 PA_STREAM_AUTO_TIMING_UPDATE|
880 PA_STREAM_NO_REMAP_CHANNELS|
881 PA_STREAM_NO_REMIX_CHANNELS|
882 PA_STREAM_FIX_FORMAT|
884 PA_STREAM_FIX_CHANNELS|
886 PA_STREAM_VARIABLE_RATE|
887 PA_STREAM_PEAK_DETECT|
888 PA_STREAM_START_MUTED|
889 PA_STREAM_ADJUST_LATENCY|
890 PA_STREAM_EARLY_REQUESTS)), PA_ERR_INVALID);
892 PA_CHECK_VALIDITY(s->context, s->context->version >= 12 || !(flags & PA_STREAM_VARIABLE_RATE), PA_ERR_NOTSUPPORTED);
893 PA_CHECK_VALIDITY(s->context, s->context->version >= 13 || !(flags & PA_STREAM_PEAK_DETECT), PA_ERR_NOTSUPPORTED);
894 /* Althought some of the other flags are not supported on older
895 * version, we don't check for them here, because it doesn't hurt
896 * when they are passed but actually not supported. This makes
897 * client development easier */
899 PA_CHECK_VALIDITY(s->context, direction == PA_STREAM_PLAYBACK || !(flags & (PA_STREAM_START_MUTED)), PA_ERR_INVALID);
900 PA_CHECK_VALIDITY(s->context, direction == PA_STREAM_RECORD || !(flags & (PA_STREAM_PEAK_DETECT)), PA_ERR_INVALID);
901 PA_CHECK_VALIDITY(s->context, !volume || volume->channels == s->sample_spec.channels, PA_ERR_INVALID);
902 PA_CHECK_VALIDITY(s->context, !sync_stream || (direction == PA_STREAM_PLAYBACK && sync_stream->direction == PA_STREAM_PLAYBACK), PA_ERR_INVALID);
903 PA_CHECK_VALIDITY(s->context, (flags & (PA_STREAM_ADJUST_LATENCY|PA_STREAM_EARLY_REQUESTS)) != (PA_STREAM_ADJUST_LATENCY|PA_STREAM_EARLY_REQUESTS), PA_ERR_INVALID);
907 s->direction = direction;
909 s->corked = !!(flags & PA_STREAM_START_CORKED);
912 s->syncid = sync_stream->syncid;
915 s->buffer_attr = *attr;
916 automatic_buffer_attr(s, &s->buffer_attr, &s->sample_spec);
918 if (flags & PA_STREAM_INTERPOLATE_TIMING) {
922 pa_smoother_free(s->smoother);
924 s->smoother = pa_smoother_new(SMOOTHER_ADJUST_TIME, SMOOTHER_HISTORY_TIME, !(flags & PA_STREAM_NOT_MONOTONIC), SMOOTHER_MIN_HISTORY);
926 x = pa_rtclock_usec();
927 pa_smoother_set_time_offset(s->smoother, x);
928 pa_smoother_pause(s->smoother, x);
932 dev = s->direction == PA_STREAM_PLAYBACK ? s->context->conf->default_sink : s->context->conf->default_source;
934 t = pa_tagstruct_command(
936 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM),
939 if (s->context->version < 13)
940 pa_tagstruct_puts(t, pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME));
944 PA_TAG_SAMPLE_SPEC, &s->sample_spec,
945 PA_TAG_CHANNEL_MAP, &s->channel_map,
946 PA_TAG_U32, PA_INVALID_INDEX,
948 PA_TAG_U32, s->buffer_attr.maxlength,
949 PA_TAG_BOOLEAN, s->corked,
952 if (s->direction == PA_STREAM_PLAYBACK) {
957 PA_TAG_U32, s->buffer_attr.tlength,
958 PA_TAG_U32, s->buffer_attr.prebuf,
959 PA_TAG_U32, s->buffer_attr.minreq,
960 PA_TAG_U32, s->syncid,
963 volume_set = !!volume;
966 volume = pa_cvolume_reset(&cv, s->sample_spec.channels);
968 pa_tagstruct_put_cvolume(t, volume);
970 pa_tagstruct_putu32(t, s->buffer_attr.fragsize);
972 if (s->context->version >= 12) {
975 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMAP_CHANNELS,
976 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMIX_CHANNELS,
977 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_FORMAT,
978 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_RATE,
979 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_CHANNELS,
980 PA_TAG_BOOLEAN, flags & PA_STREAM_DONT_MOVE,
981 PA_TAG_BOOLEAN, flags & PA_STREAM_VARIABLE_RATE,
985 if (s->context->version >= 13) {
987 if (s->direction == PA_STREAM_PLAYBACK)
988 pa_tagstruct_put_boolean(t, flags & PA_STREAM_START_MUTED);
990 pa_tagstruct_put_boolean(t, flags & PA_STREAM_PEAK_DETECT);
994 PA_TAG_BOOLEAN, flags & PA_STREAM_ADJUST_LATENCY,
995 PA_TAG_PROPLIST, s->proplist,
998 if (s->direction == PA_STREAM_RECORD)
999 pa_tagstruct_putu32(t, s->direct_on_input);
1002 if (s->context->version >= 14) {
1004 if (s->direction == PA_STREAM_PLAYBACK)
1005 pa_tagstruct_put_boolean(t, volume_set);
1007 pa_tagstruct_put_boolean(t, flags & PA_STREAM_EARLY_REQUESTS);
1010 pa_pstream_send_tagstruct(s->context->pstream, t);
1011 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s, NULL);
1013 pa_stream_set_state(s, PA_STREAM_CREATING);
1019 int pa_stream_connect_playback(
1022 const pa_buffer_attr *attr,
1023 pa_stream_flags_t flags,
1025 pa_stream *sync_stream) {
1028 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1030 return create_stream(PA_STREAM_PLAYBACK, s, dev, attr, flags, volume, sync_stream);
1033 int pa_stream_connect_record(
1036 const pa_buffer_attr *attr,
1037 pa_stream_flags_t flags) {
1040 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1042 return create_stream(PA_STREAM_RECORD, s, dev, attr, flags, NULL, NULL);
1045 int pa_stream_write(
1049 void (*free_cb)(void *p),
1051 pa_seek_mode_t seek) {
1054 pa_seek_mode_t t_seek;
1060 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1063 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1064 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || s->direction == PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1065 PA_CHECK_VALIDITY(s->context, seek <= PA_SEEK_RELATIVE_END, PA_ERR_INVALID);
1066 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || (seek == PA_SEEK_RELATIVE && offset == 0), PA_ERR_INVALID);
1076 while (t_length > 0) {
1080 if (free_cb && !pa_pstream_get_shm(s->context->pstream)) {
1081 chunk.memblock = pa_memblock_new_user(s->context->mempool, (void*) t_data, t_length, free_cb, 1);
1082 chunk.length = t_length;
1086 chunk.length = PA_MIN(t_length, pa_mempool_block_size_max(s->context->mempool));
1087 chunk.memblock = pa_memblock_new(s->context->mempool, chunk.length);
1089 d = pa_memblock_acquire(chunk.memblock);
1090 memcpy(d, t_data, chunk.length);
1091 pa_memblock_release(chunk.memblock);
1094 pa_pstream_send_memblock(s->context->pstream, s->channel, t_offset, t_seek, &chunk);
1097 t_seek = PA_SEEK_RELATIVE;
1099 t_data = (const uint8_t*) t_data + chunk.length;
1100 t_length -= chunk.length;
1102 pa_memblock_unref(chunk.memblock);
1105 if (free_cb && pa_pstream_get_shm(s->context->pstream))
1106 free_cb((void*) data);
1108 if (length < s->requested_bytes)
1109 s->requested_bytes -= (uint32_t) length;
1111 s->requested_bytes = 0;
1113 /* FIXME!!! ^^^ will break when offset is != 0 and mode is not RELATIVE*/
1115 if (s->direction == PA_STREAM_PLAYBACK) {
1117 /* Update latency request correction */
1118 if (s->write_index_corrections[s->current_write_index_correction].valid) {
1120 if (seek == PA_SEEK_ABSOLUTE) {
1121 s->write_index_corrections[s->current_write_index_correction].corrupt = FALSE;
1122 s->write_index_corrections[s->current_write_index_correction].absolute = TRUE;
1123 s->write_index_corrections[s->current_write_index_correction].value = offset + (int64_t) length;
1124 } else if (seek == PA_SEEK_RELATIVE) {
1125 if (!s->write_index_corrections[s->current_write_index_correction].corrupt)
1126 s->write_index_corrections[s->current_write_index_correction].value += offset + (int64_t) length;
1128 s->write_index_corrections[s->current_write_index_correction].corrupt = TRUE;
1131 /* Update the write index in the already available latency data */
1132 if (s->timing_info_valid) {
1134 if (seek == PA_SEEK_ABSOLUTE) {
1135 s->timing_info.write_index_corrupt = FALSE;
1136 s->timing_info.write_index = offset + (int64_t) length;
1137 } else if (seek == PA_SEEK_RELATIVE) {
1138 if (!s->timing_info.write_index_corrupt)
1139 s->timing_info.write_index += offset + (int64_t) length;
1141 s->timing_info.write_index_corrupt = TRUE;
1144 if (!s->timing_info_valid || s->timing_info.write_index_corrupt)
1145 request_auto_timing_update(s, TRUE);
1151 int pa_stream_peek(pa_stream *s, const void **data, size_t *length) {
1153 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1157 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1158 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1160 if (!s->peek_memchunk.memblock) {
1162 if (pa_memblockq_peek(s->record_memblockq, &s->peek_memchunk) < 0) {
1168 s->peek_data = pa_memblock_acquire(s->peek_memchunk.memblock);
1171 pa_assert(s->peek_data);
1172 *data = (uint8_t*) s->peek_data + s->peek_memchunk.index;
1173 *length = s->peek_memchunk.length;
1177 int pa_stream_drop(pa_stream *s) {
1179 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1181 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1182 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1183 PA_CHECK_VALIDITY(s->context, s->peek_memchunk.memblock, PA_ERR_BADSTATE);
1185 pa_memblockq_drop(s->record_memblockq, s->peek_memchunk.length);
1187 /* Fix the simulated local read index */
1188 if (s->timing_info_valid && !s->timing_info.read_index_corrupt)
1189 s->timing_info.read_index += (int64_t) s->peek_memchunk.length;
1191 pa_assert(s->peek_data);
1192 pa_memblock_release(s->peek_memchunk.memblock);
1193 pa_memblock_unref(s->peek_memchunk.memblock);
1194 pa_memchunk_reset(&s->peek_memchunk);
1199 size_t pa_stream_writable_size(pa_stream *s) {
1201 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1203 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
1204 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
1206 return s->requested_bytes;
1209 size_t pa_stream_readable_size(pa_stream *s) {
1211 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1213 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
1214 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
1216 return pa_memblockq_get_length(s->record_memblockq);
1219 pa_operation * pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1225 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1227 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1228 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1230 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1232 t = pa_tagstruct_command(s->context, PA_COMMAND_DRAIN_PLAYBACK_STREAM, &tag);
1233 pa_tagstruct_putu32(t, s->channel);
1234 pa_pstream_send_tagstruct(s->context->pstream, t);
1235 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);
1240 static pa_usec_t calc_time(pa_stream *s, pa_bool_t ignore_transport) {
1244 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1245 pa_assert(s->state == PA_STREAM_READY);
1246 pa_assert(s->direction != PA_STREAM_UPLOAD);
1247 pa_assert(s->timing_info_valid);
1248 pa_assert(s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt);
1249 pa_assert(s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt);
1251 if (s->direction == PA_STREAM_PLAYBACK) {
1252 /* The last byte that was written into the output device
1253 * had this time value associated */
1254 usec = pa_bytes_to_usec(s->timing_info.read_index < 0 ? 0 : (uint64_t) s->timing_info.read_index, &s->sample_spec);
1256 if (!s->corked && !s->suspended) {
1258 if (!ignore_transport)
1259 /* Because the latency info took a little time to come
1260 * to us, we assume that the real output time is actually
1262 usec += s->timing_info.transport_usec;
1264 /* However, the output device usually maintains a buffer
1265 too, hence the real sample currently played is a little
1267 if (s->timing_info.sink_usec >= usec)
1270 usec -= s->timing_info.sink_usec;
1274 pa_assert(s->direction == PA_STREAM_RECORD);
1276 /* The last byte written into the server side queue had
1277 * this time value associated */
1278 usec = pa_bytes_to_usec(s->timing_info.write_index < 0 ? 0 : (uint64_t) s->timing_info.write_index, &s->sample_spec);
1280 if (!s->corked && !s->suspended) {
1282 if (!ignore_transport)
1283 /* Add transport latency */
1284 usec += s->timing_info.transport_usec;
1286 /* Add latency of data in device buffer */
1287 usec += s->timing_info.source_usec;
1289 /* If this is a monitor source, we need to correct the
1290 * time by the playback device buffer */
1291 if (s->timing_info.sink_usec >= usec)
1294 usec -= s->timing_info.sink_usec;
1301 static void stream_get_timing_info_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1302 pa_operation *o = userdata;
1303 struct timeval local, remote, now;
1305 pa_bool_t playing = FALSE;
1306 uint64_t underrun_for = 0, playing_for = 0;
1310 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1312 if (!o->context || !o->stream)
1315 i = &o->stream->timing_info;
1317 o->stream->timing_info_valid = FALSE;
1318 i->write_index_corrupt = TRUE;
1319 i->read_index_corrupt = TRUE;
1321 if (command != PA_COMMAND_REPLY) {
1322 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
1327 if (pa_tagstruct_get_usec(t, &i->sink_usec) < 0 ||
1328 pa_tagstruct_get_usec(t, &i->source_usec) < 0 ||
1329 pa_tagstruct_get_boolean(t, &playing) < 0 ||
1330 pa_tagstruct_get_timeval(t, &local) < 0 ||
1331 pa_tagstruct_get_timeval(t, &remote) < 0 ||
1332 pa_tagstruct_gets64(t, &i->write_index) < 0 ||
1333 pa_tagstruct_gets64(t, &i->read_index) < 0) {
1335 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1339 if (o->context->version >= 13 &&
1340 o->stream->direction == PA_STREAM_PLAYBACK)
1341 if (pa_tagstruct_getu64(t, &underrun_for) < 0 ||
1342 pa_tagstruct_getu64(t, &playing_for) < 0) {
1344 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1349 if (!pa_tagstruct_eof(t)) {
1350 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1353 o->stream->timing_info_valid = TRUE;
1354 i->write_index_corrupt = FALSE;
1355 i->read_index_corrupt = FALSE;
1357 i->playing = (int) playing;
1358 i->since_underrun = (int64_t) (playing ? playing_for : underrun_for);
1360 pa_gettimeofday(&now);
1362 /* Calculcate timestamps */
1363 if (pa_timeval_cmp(&local, &remote) <= 0 && pa_timeval_cmp(&remote, &now) <= 0) {
1364 /* local and remote seem to have synchronized clocks */
1366 if (o->stream->direction == PA_STREAM_PLAYBACK)
1367 i->transport_usec = pa_timeval_diff(&remote, &local);
1369 i->transport_usec = pa_timeval_diff(&now, &remote);
1371 i->synchronized_clocks = TRUE;
1372 i->timestamp = remote;
1374 /* clocks are not synchronized, let's estimate latency then */
1375 i->transport_usec = pa_timeval_diff(&now, &local)/2;
1376 i->synchronized_clocks = FALSE;
1377 i->timestamp = local;
1378 pa_timeval_add(&i->timestamp, i->transport_usec);
1381 /* Invalidate read and write indexes if necessary */
1382 if (tag < o->stream->read_index_not_before)
1383 i->read_index_corrupt = TRUE;
1385 if (tag < o->stream->write_index_not_before)
1386 i->write_index_corrupt = TRUE;
1388 if (o->stream->direction == PA_STREAM_PLAYBACK) {
1389 /* Write index correction */
1392 uint32_t ctag = tag;
1394 /* Go through the saved correction values and add up the
1395 * total correction.*/
1396 for (n = 0, j = o->stream->current_write_index_correction+1;
1397 n < PA_MAX_WRITE_INDEX_CORRECTIONS;
1398 n++, j = (j + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS) {
1400 /* Step over invalid data or out-of-date data */
1401 if (!o->stream->write_index_corrections[j].valid ||
1402 o->stream->write_index_corrections[j].tag < ctag)
1405 /* Make sure that everything is in order */
1406 ctag = o->stream->write_index_corrections[j].tag+1;
1408 /* Now fix the write index */
1409 if (o->stream->write_index_corrections[j].corrupt) {
1410 /* A corrupting seek was made */
1411 i->write_index_corrupt = TRUE;
1412 } else if (o->stream->write_index_corrections[j].absolute) {
1413 /* An absolute seek was made */
1414 i->write_index = o->stream->write_index_corrections[j].value;
1415 i->write_index_corrupt = FALSE;
1416 } else if (!i->write_index_corrupt) {
1417 /* A relative seek was made */
1418 i->write_index += o->stream->write_index_corrections[j].value;
1422 /* Clear old correction entries */
1423 for (n = 0; n < PA_MAX_WRITE_INDEX_CORRECTIONS; n++) {
1424 if (!o->stream->write_index_corrections[n].valid)
1427 if (o->stream->write_index_corrections[n].tag <= tag)
1428 o->stream->write_index_corrections[n].valid = FALSE;
1432 if (o->stream->direction == PA_STREAM_RECORD) {
1433 /* Read index correction */
1435 if (!i->read_index_corrupt)
1436 i->read_index -= (int64_t) pa_memblockq_get_length(o->stream->record_memblockq);
1439 /* Update smoother */
1440 if (o->stream->smoother) {
1443 u = x = pa_rtclock_usec() - i->transport_usec;
1445 if (o->stream->direction == PA_STREAM_PLAYBACK && o->context->version >= 13) {
1448 /* If we weren't playing then it will take some time
1449 * until the audio will actually come out through the
1450 * speakers. Since we follow that timing here, we need
1451 * to try to fix this up */
1453 su = pa_bytes_to_usec((uint64_t) i->since_underrun, &o->stream->sample_spec);
1455 if (su < i->sink_usec)
1456 x += i->sink_usec - su;
1460 pa_smoother_pause(o->stream->smoother, x);
1462 /* Update the smoother */
1463 if ((o->stream->direction == PA_STREAM_PLAYBACK && !i->read_index_corrupt) ||
1464 (o->stream->direction == PA_STREAM_RECORD && !i->write_index_corrupt))
1465 pa_smoother_put(o->stream->smoother, u, calc_time(o->stream, TRUE));
1468 pa_smoother_resume(o->stream->smoother, x);
1472 o->stream->auto_timing_update_requested = FALSE;
1474 if (o->stream->latency_update_callback)
1475 o->stream->latency_update_callback(o->stream, o->stream->latency_update_userdata);
1477 if (o->callback && o->stream && o->stream->state == PA_STREAM_READY) {
1478 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1479 cb(o->stream, o->stream->timing_info_valid, o->userdata);
1484 pa_operation_done(o);
1485 pa_operation_unref(o);
1488 pa_operation* pa_stream_update_timing_info(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1496 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1498 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1499 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1501 if (s->direction == PA_STREAM_PLAYBACK) {
1502 /* Find a place to store the write_index correction data for this entry */
1503 cidx = (s->current_write_index_correction + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS;
1505 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
1506 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !s->write_index_corrections[cidx].valid, PA_ERR_INTERNAL);
1508 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1510 t = pa_tagstruct_command(
1512 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_GET_PLAYBACK_LATENCY : PA_COMMAND_GET_RECORD_LATENCY),
1514 pa_tagstruct_putu32(t, s->channel);
1515 pa_tagstruct_put_timeval(t, pa_gettimeofday(&now));
1517 pa_pstream_send_tagstruct(s->context->pstream, t);
1518 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);
1520 if (s->direction == PA_STREAM_PLAYBACK) {
1521 /* Fill in initial correction data */
1523 s->current_write_index_correction = cidx;
1525 s->write_index_corrections[cidx].valid = TRUE;
1526 s->write_index_corrections[cidx].absolute = FALSE;
1527 s->write_index_corrections[cidx].corrupt = FALSE;
1528 s->write_index_corrections[cidx].tag = tag;
1529 s->write_index_corrections[cidx].value = 0;
1535 void pa_stream_disconnect_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1536 pa_stream *s = userdata;
1540 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1544 if (command != PA_COMMAND_REPLY) {
1545 if (pa_context_handle_error(s->context, command, t, FALSE) < 0)
1548 pa_stream_set_state(s, PA_STREAM_FAILED);
1550 } else if (!pa_tagstruct_eof(t)) {
1551 pa_context_fail(s->context, PA_ERR_PROTOCOL);
1555 pa_stream_set_state(s, PA_STREAM_TERMINATED);
1561 int pa_stream_disconnect(pa_stream *s) {
1566 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1568 PA_CHECK_VALIDITY(s->context, s->channel_valid, PA_ERR_BADSTATE);
1569 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1573 t = pa_tagstruct_command(
1575 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_DELETE_PLAYBACK_STREAM :
1576 (s->direction == PA_STREAM_RECORD ? PA_COMMAND_DELETE_RECORD_STREAM : PA_COMMAND_DELETE_UPLOAD_STREAM)),
1578 pa_tagstruct_putu32(t, s->channel);
1579 pa_pstream_send_tagstruct(s->context->pstream, t);
1580 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s, NULL);
1586 void pa_stream_set_read_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1588 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1590 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1593 s->read_callback = cb;
1594 s->read_userdata = userdata;
1597 void pa_stream_set_write_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1599 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1601 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1604 s->write_callback = cb;
1605 s->write_userdata = userdata;
1608 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1610 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1612 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1615 s->state_callback = cb;
1616 s->state_userdata = userdata;
1619 void pa_stream_set_overflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1621 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1623 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1626 s->overflow_callback = cb;
1627 s->overflow_userdata = userdata;
1630 void pa_stream_set_underflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1632 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1634 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1637 s->underflow_callback = cb;
1638 s->underflow_userdata = userdata;
1641 void pa_stream_set_latency_update_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1643 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1645 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1648 s->latency_update_callback = cb;
1649 s->latency_update_userdata = userdata;
1652 void pa_stream_set_moved_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1654 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1656 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1659 s->moved_callback = cb;
1660 s->moved_userdata = userdata;
1663 void pa_stream_set_suspended_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1665 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1667 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1670 s->suspended_callback = cb;
1671 s->suspended_userdata = userdata;
1674 void pa_stream_set_started_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1676 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1678 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1681 s->started_callback = cb;
1682 s->started_userdata = userdata;
1685 void pa_stream_simple_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1686 pa_operation *o = userdata;
1691 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1696 if (command != PA_COMMAND_REPLY) {
1697 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
1701 } else if (!pa_tagstruct_eof(t)) {
1702 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1707 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1708 cb(o->stream, success, o->userdata);
1712 pa_operation_done(o);
1713 pa_operation_unref(o);
1716 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata) {
1722 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1724 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1725 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1729 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1731 t = pa_tagstruct_command(
1733 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CORK_PLAYBACK_STREAM : PA_COMMAND_CORK_RECORD_STREAM),
1735 pa_tagstruct_putu32(t, s->channel);
1736 pa_tagstruct_put_boolean(t, !!b);
1737 pa_pstream_send_tagstruct(s->context->pstream, t);
1738 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);
1740 check_smoother_status(s, FALSE, FALSE, FALSE);
1742 /* This might cause the indexes to hang/start again, hence
1743 * let's request a timing update */
1744 request_auto_timing_update(s, TRUE);
1749 static pa_operation* stream_send_simple_command(pa_stream *s, uint32_t command, pa_stream_success_cb_t cb, void *userdata) {
1755 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1757 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1759 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1761 t = pa_tagstruct_command(s->context, command, &tag);
1762 pa_tagstruct_putu32(t, s->channel);
1763 pa_pstream_send_tagstruct(s->context->pstream, t);
1764 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);
1769 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1773 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1775 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1776 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1778 if (!(o = stream_send_simple_command(s, (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_FLUSH_PLAYBACK_STREAM : PA_COMMAND_FLUSH_RECORD_STREAM), cb, userdata)))
1781 if (s->direction == PA_STREAM_PLAYBACK) {
1783 if (s->write_index_corrections[s->current_write_index_correction].valid)
1784 s->write_index_corrections[s->current_write_index_correction].corrupt = TRUE;
1786 if (s->buffer_attr.prebuf > 0)
1787 check_smoother_status(s, FALSE, FALSE, TRUE);
1789 /* This will change the write index, but leave the
1790 * read index untouched. */
1791 invalidate_indexes(s, FALSE, TRUE);
1794 /* For record streams this has no influence on the write
1795 * index, but the read index might jump. */
1796 invalidate_indexes(s, TRUE, FALSE);
1801 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1805 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1807 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1808 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1809 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1811 if (!(o = stream_send_simple_command(s, PA_COMMAND_PREBUF_PLAYBACK_STREAM, cb, userdata)))
1814 /* This might cause the read index to hang again, hence
1815 * let's request a timing update */
1816 request_auto_timing_update(s, TRUE);
1821 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1825 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1827 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1828 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1829 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1831 if (!(o = stream_send_simple_command(s, PA_COMMAND_TRIGGER_PLAYBACK_STREAM, cb, userdata)))
1834 /* This might cause the read index to start moving again, hence
1835 * let's request a timing update */
1836 request_auto_timing_update(s, TRUE);
1841 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata) {
1845 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1848 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1849 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1851 if (s->context->version >= 13) {
1852 pa_proplist *p = pa_proplist_new();
1854 pa_proplist_sets(p, PA_PROP_MEDIA_NAME, name);
1855 o = pa_stream_proplist_update(s, PA_UPDATE_REPLACE, p, cb, userdata);
1856 pa_proplist_free(p);
1861 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1862 t = pa_tagstruct_command(
1864 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_NAME : PA_COMMAND_SET_PLAYBACK_STREAM_NAME),
1866 pa_tagstruct_putu32(t, s->channel);
1867 pa_tagstruct_puts(t, name);
1868 pa_pstream_send_tagstruct(s->context->pstream, t);
1869 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);
1875 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec) {
1879 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1881 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1882 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1883 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1884 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1885 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1888 usec = pa_smoother_get(s->smoother, pa_rtclock_usec());
1890 usec = calc_time(s, FALSE);
1892 /* Make sure the time runs monotonically */
1893 if (!(s->flags & PA_STREAM_NOT_MONOTONOUS)) {
1894 if (usec < s->previous_time)
1895 usec = s->previous_time;
1897 s->previous_time = usec;
1906 static pa_usec_t time_counter_diff(pa_stream *s, pa_usec_t a, pa_usec_t b, int *negative) {
1908 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1916 if (negative && s->direction == PA_STREAM_RECORD) {
1924 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative) {
1930 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1933 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1934 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1935 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1936 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1937 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1939 if ((r = pa_stream_get_time(s, &t)) < 0)
1942 if (s->direction == PA_STREAM_PLAYBACK)
1943 cindex = s->timing_info.write_index;
1945 cindex = s->timing_info.read_index;
1950 c = pa_bytes_to_usec((uint64_t) cindex, &s->sample_spec);
1952 if (s->direction == PA_STREAM_PLAYBACK)
1953 *r_usec = time_counter_diff(s, c, t, negative);
1955 *r_usec = time_counter_diff(s, t, c, negative);
1960 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s) {
1962 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1964 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1965 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1966 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->timing_info_valid, PA_ERR_NODATA);
1968 return &s->timing_info;
1971 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s) {
1973 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1975 return &s->sample_spec;
1978 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s) {
1980 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1982 return &s->channel_map;
1985 const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s) {
1987 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1989 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1990 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1991 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 9, PA_ERR_NOTSUPPORTED);
1993 return &s->buffer_attr;
1996 static void stream_set_buffer_attr_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1997 pa_operation *o = userdata;
2002 pa_assert(PA_REFCNT_VALUE(o) >= 1);
2007 if (command != PA_COMMAND_REPLY) {
2008 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
2014 if (o->stream->direction == PA_STREAM_PLAYBACK) {
2015 if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
2016 pa_tagstruct_getu32(t, &o->stream->buffer_attr.tlength) < 0 ||
2017 pa_tagstruct_getu32(t, &o->stream->buffer_attr.prebuf) < 0 ||
2018 pa_tagstruct_getu32(t, &o->stream->buffer_attr.minreq) < 0) {
2019 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2022 } else if (o->stream->direction == PA_STREAM_RECORD) {
2023 if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
2024 pa_tagstruct_getu32(t, &o->stream->buffer_attr.fragsize) < 0) {
2025 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2030 if (!pa_tagstruct_eof(t)) {
2031 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2037 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
2038 cb(o->stream, success, o->userdata);
2042 pa_operation_done(o);
2043 pa_operation_unref(o);
2047 pa_operation* pa_stream_set_buffer_attr(pa_stream *s, const pa_buffer_attr *attr, pa_stream_success_cb_t cb, void *userdata) {
2053 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2056 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2057 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2058 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2060 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2062 t = pa_tagstruct_command(
2064 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR : PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR),
2066 pa_tagstruct_putu32(t, s->channel);
2068 pa_tagstruct_putu32(t, attr->maxlength);
2070 if (s->direction == PA_STREAM_PLAYBACK)
2073 PA_TAG_U32, attr->tlength,
2074 PA_TAG_U32, attr->prebuf,
2075 PA_TAG_U32, attr->minreq,
2078 pa_tagstruct_putu32(t, attr->fragsize);
2080 if (s->context->version >= 13)
2081 pa_tagstruct_put_boolean(t, !!(s->flags & PA_STREAM_ADJUST_LATENCY));
2083 if (s->context->version >= 14)
2084 pa_tagstruct_put_boolean(t, !!(s->flags & PA_STREAM_EARLY_REQUESTS));
2086 pa_pstream_send_tagstruct(s->context->pstream, t);
2087 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);
2089 /* This might cause changes in the read/write indexex, hence let's
2090 * request a timing update */
2091 request_auto_timing_update(s, TRUE);
2096 uint32_t pa_stream_get_device_index(pa_stream *s) {
2098 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2100 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2101 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2102 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
2103 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->device_index != PA_INVALID_INDEX, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2105 return s->device_index;
2108 const char *pa_stream_get_device_name(pa_stream *s) {
2110 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2112 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2113 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2114 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2115 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->device_name, PA_ERR_BADSTATE);
2117 return s->device_name;
2120 int pa_stream_is_suspended(pa_stream *s) {
2122 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2124 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2125 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2126 PA_CHECK_VALIDITY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2128 return s->suspended;
2131 int pa_stream_is_corked(pa_stream *s) {
2133 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2135 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2136 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2141 static void stream_update_sample_rate_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2142 pa_operation *o = userdata;
2147 pa_assert(PA_REFCNT_VALUE(o) >= 1);
2152 if (command != PA_COMMAND_REPLY) {
2153 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
2159 if (!pa_tagstruct_eof(t)) {
2160 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2165 o->stream->sample_spec.rate = PA_PTR_TO_UINT(o->private);
2166 pa_assert(pa_sample_spec_valid(&o->stream->sample_spec));
2169 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
2170 cb(o->stream, success, o->userdata);
2174 pa_operation_done(o);
2175 pa_operation_unref(o);
2179 pa_operation *pa_stream_update_sample_rate(pa_stream *s, uint32_t rate, pa_stream_success_cb_t cb, void *userdata) {
2185 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2187 PA_CHECK_VALIDITY_RETURN_NULL(s->context, rate > 0 && rate <= PA_RATE_MAX, PA_ERR_INVALID);
2188 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2189 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2190 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->flags & PA_STREAM_VARIABLE_RATE, PA_ERR_BADSTATE);
2191 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2193 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2194 o->private = PA_UINT_TO_PTR(rate);
2196 t = pa_tagstruct_command(
2198 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE : PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE),
2200 pa_tagstruct_putu32(t, s->channel);
2201 pa_tagstruct_putu32(t, rate);
2203 pa_pstream_send_tagstruct(s->context->pstream, t);
2204 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);
2209 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) {
2215 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2217 PA_CHECK_VALIDITY_RETURN_NULL(s->context, mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE, PA_ERR_INVALID);
2218 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2219 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2220 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2222 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2224 t = pa_tagstruct_command(
2226 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST : PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST),
2228 pa_tagstruct_putu32(t, s->channel);
2229 pa_tagstruct_putu32(t, (uint32_t) mode);
2230 pa_tagstruct_put_proplist(t, p);
2232 pa_pstream_send_tagstruct(s->context->pstream, t);
2233 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);
2235 /* Please note that we don't update s->proplist here, because we
2236 * don't export that field */
2241 pa_operation *pa_stream_proplist_remove(pa_stream *s, const char *const keys[], pa_stream_success_cb_t cb, void *userdata) {
2245 const char * const*k;
2248 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2250 PA_CHECK_VALIDITY_RETURN_NULL(s->context, keys && keys[0], PA_ERR_INVALID);
2251 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2252 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2253 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2255 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2257 t = pa_tagstruct_command(
2259 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST : PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST),
2261 pa_tagstruct_putu32(t, s->channel);
2263 for (k = keys; *k; k++)
2264 pa_tagstruct_puts(t, *k);
2266 pa_tagstruct_puts(t, NULL);
2268 pa_pstream_send_tagstruct(s->context->pstream, t);
2269 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);
2271 /* Please note that we don't update s->proplist here, because we
2272 * don't export that field */
2277 int pa_stream_set_monitor_stream(pa_stream *s, uint32_t sink_input_idx) {
2279 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2281 PA_CHECK_VALIDITY(s->context, sink_input_idx != PA_INVALID_INDEX, PA_ERR_INVALID);
2282 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
2283 PA_CHECK_VALIDITY(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2285 s->direct_on_input = sink_input_idx;
2290 uint32_t pa_stream_get_monitor_stream(pa_stream *s) {
2292 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2294 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direct_on_input != PA_INVALID_INDEX, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2295 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
2297 return s->direct_on_input;