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.1 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/rtclock.h>
34 #include <pulse/xmalloc.h>
36 #include <pulsecore/pstream-util.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/hashmap.h>
39 #include <pulsecore/macro.h>
40 #include <pulsecore/core-rtclock.h>
42 #include "fork-detect.h"
45 #define AUTO_TIMING_INTERVAL_START_USEC (10*PA_USEC_PER_MSEC)
46 #define AUTO_TIMING_INTERVAL_END_USEC (1500*PA_USEC_PER_MSEC)
48 #define SMOOTHER_ADJUST_TIME (1000*PA_USEC_PER_MSEC)
49 #define SMOOTHER_HISTORY_TIME (5000*PA_USEC_PER_MSEC)
50 #define SMOOTHER_MIN_HISTORY (4)
52 pa_stream *pa_stream_new(pa_context *c, const char *name, const pa_sample_spec *ss, const pa_channel_map *map) {
53 return pa_stream_new_with_proplist(c, name, ss, map, NULL);
56 static void reset_callbacks(pa_stream *s) {
57 s->read_callback = NULL;
58 s->read_userdata = NULL;
59 s->write_callback = NULL;
60 s->write_userdata = NULL;
61 s->state_callback = NULL;
62 s->state_userdata = NULL;
63 s->overflow_callback = NULL;
64 s->overflow_userdata = NULL;
65 s->underflow_callback = NULL;
66 s->underflow_userdata = NULL;
67 s->latency_update_callback = NULL;
68 s->latency_update_userdata = NULL;
69 s->moved_callback = NULL;
70 s->moved_userdata = NULL;
71 s->suspended_callback = NULL;
72 s->suspended_userdata = NULL;
73 s->started_callback = NULL;
74 s->started_userdata = NULL;
75 s->event_callback = NULL;
76 s->event_userdata = NULL;
77 s->buffer_attr_callback = NULL;
78 s->buffer_attr_userdata = NULL;
81 pa_stream *pa_stream_new_with_proplist(
84 const pa_sample_spec *ss,
85 const pa_channel_map *map,
93 pa_assert(PA_REFCNT_VALUE(c) >= 1);
95 PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
96 PA_CHECK_VALIDITY_RETURN_NULL(c, ss && pa_sample_spec_valid(ss), PA_ERR_INVALID);
97 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 12 || (ss->format != PA_SAMPLE_S32LE && ss->format != PA_SAMPLE_S32BE), PA_ERR_NOTSUPPORTED);
98 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 15 || (ss->format != PA_SAMPLE_S24LE && ss->format != PA_SAMPLE_S24BE), PA_ERR_NOTSUPPORTED);
99 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 15 || (ss->format != PA_SAMPLE_S24_32LE && ss->format != PA_SAMPLE_S24_32BE), PA_ERR_NOTSUPPORTED);
100 PA_CHECK_VALIDITY_RETURN_NULL(c, !map || (pa_channel_map_valid(map) && map->channels == ss->channels), PA_ERR_INVALID);
101 PA_CHECK_VALIDITY_RETURN_NULL(c, name || (p && pa_proplist_contains(p, PA_PROP_MEDIA_NAME)), PA_ERR_INVALID);
104 PA_CHECK_VALIDITY_RETURN_NULL(c, map = pa_channel_map_init_auto(&tmap, ss->channels, PA_CHANNEL_MAP_DEFAULT), PA_ERR_INVALID);
106 s = pa_xnew(pa_stream, 1);
109 s->mainloop = c->mainloop;
111 s->direction = PA_STREAM_NODIRECTION;
112 s->state = PA_STREAM_UNCONNECTED;
115 s->sample_spec = *ss;
116 s->channel_map = *map;
118 s->direct_on_input = PA_INVALID_INDEX;
120 s->proplist = p ? pa_proplist_copy(p) : pa_proplist_new();
122 pa_proplist_sets(s->proplist, PA_PROP_MEDIA_NAME, name);
125 s->channel_valid = FALSE;
126 s->syncid = c->csyncid++;
127 s->stream_index = PA_INVALID_INDEX;
129 s->requested_bytes = 0;
130 memset(&s->buffer_attr, 0, sizeof(s->buffer_attr));
132 /* We initialize der target length here, so that if the user
133 * passes no explicit buffering metrics the default is similar to
134 * what older PA versions provided. */
136 s->buffer_attr.maxlength = (uint32_t) -1;
137 s->buffer_attr.tlength = (uint32_t) pa_usec_to_bytes(250*PA_USEC_PER_MSEC, ss); /* 250ms of buffering */
138 s->buffer_attr.minreq = (uint32_t) -1;
139 s->buffer_attr.prebuf = (uint32_t) -1;
140 s->buffer_attr.fragsize = (uint32_t) -1;
142 s->device_index = PA_INVALID_INDEX;
143 s->device_name = NULL;
144 s->suspended = FALSE;
147 s->write_memblock = NULL;
148 s->write_data = NULL;
150 pa_memchunk_reset(&s->peek_memchunk);
152 s->record_memblockq = NULL;
154 memset(&s->timing_info, 0, sizeof(s->timing_info));
155 s->timing_info_valid = FALSE;
157 s->previous_time = 0;
159 s->read_index_not_before = 0;
160 s->write_index_not_before = 0;
161 for (i = 0; i < PA_MAX_WRITE_INDEX_CORRECTIONS; i++)
162 s->write_index_corrections[i].valid = 0;
163 s->current_write_index_correction = 0;
165 s->auto_timing_update_event = NULL;
166 s->auto_timing_update_requested = FALSE;
167 s->auto_timing_interval_usec = AUTO_TIMING_INTERVAL_START_USEC;
173 /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
174 PA_LLIST_PREPEND(pa_stream, c->streams, s);
180 static void stream_unlink(pa_stream *s) {
187 /* Detach from context */
189 /* Unref all operatio object that point to us */
190 for (o = s->context->operations; o; o = n) {
194 pa_operation_cancel(o);
197 /* Drop all outstanding replies for this stream */
198 if (s->context->pdispatch)
199 pa_pdispatch_unregister_reply(s->context->pdispatch, s);
201 if (s->channel_valid) {
202 pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, NULL);
204 s->channel_valid = FALSE;
207 PA_LLIST_REMOVE(pa_stream, s->context->streams, s);
212 if (s->auto_timing_update_event) {
213 pa_assert(s->mainloop);
214 s->mainloop->time_free(s->auto_timing_update_event);
220 static void stream_free(pa_stream *s) {
225 if (s->write_memblock) {
226 pa_memblock_release(s->write_memblock);
227 pa_memblock_unref(s->write_data);
230 if (s->peek_memchunk.memblock) {
232 pa_memblock_release(s->peek_memchunk.memblock);
233 pa_memblock_unref(s->peek_memchunk.memblock);
236 if (s->record_memblockq)
237 pa_memblockq_free(s->record_memblockq);
240 pa_proplist_free(s->proplist);
243 pa_smoother_free(s->smoother);
245 pa_xfree(s->device_name);
249 void pa_stream_unref(pa_stream *s) {
251 pa_assert(PA_REFCNT_VALUE(s) >= 1);
253 if (PA_REFCNT_DEC(s) <= 0)
257 pa_stream* pa_stream_ref(pa_stream *s) {
259 pa_assert(PA_REFCNT_VALUE(s) >= 1);
265 pa_stream_state_t pa_stream_get_state(pa_stream *s) {
267 pa_assert(PA_REFCNT_VALUE(s) >= 1);
272 pa_context* pa_stream_get_context(pa_stream *s) {
274 pa_assert(PA_REFCNT_VALUE(s) >= 1);
279 uint32_t pa_stream_get_index(pa_stream *s) {
281 pa_assert(PA_REFCNT_VALUE(s) >= 1);
283 PA_CHECK_VALIDITY_RETURN_ANY(s->context, !pa_detect_fork(), PA_ERR_FORKED, PA_INVALID_INDEX);
284 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
286 return s->stream_index;
289 void pa_stream_set_state(pa_stream *s, pa_stream_state_t st) {
291 pa_assert(PA_REFCNT_VALUE(s) >= 1);
300 if (s->state_callback)
301 s->state_callback(s, s->state_userdata);
303 if ((st == PA_STREAM_FAILED || st == PA_STREAM_TERMINATED))
309 static void request_auto_timing_update(pa_stream *s, pa_bool_t force) {
311 pa_assert(PA_REFCNT_VALUE(s) >= 1);
313 if (!(s->flags & PA_STREAM_AUTO_TIMING_UPDATE))
316 if (s->state == PA_STREAM_READY &&
317 (force || !s->auto_timing_update_requested)) {
320 /* pa_log("Automatically requesting new timing data"); */
322 if ((o = pa_stream_update_timing_info(s, NULL, NULL))) {
323 pa_operation_unref(o);
324 s->auto_timing_update_requested = TRUE;
328 if (s->auto_timing_update_event) {
330 s->auto_timing_interval_usec = AUTO_TIMING_INTERVAL_START_USEC;
332 pa_context_rttime_restart(s->context, s->auto_timing_update_event, pa_rtclock_now() + s->auto_timing_interval_usec);
334 s->auto_timing_interval_usec = PA_MIN(AUTO_TIMING_INTERVAL_END_USEC, s->auto_timing_interval_usec*2);
338 void pa_command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
339 pa_context *c = userdata;
344 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_KILLED || command == PA_COMMAND_RECORD_STREAM_KILLED);
347 pa_assert(PA_REFCNT_VALUE(c) >= 1);
351 if (pa_tagstruct_getu32(t, &channel) < 0 ||
352 !pa_tagstruct_eof(t)) {
353 pa_context_fail(c, PA_ERR_PROTOCOL);
357 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel)))
360 if (s->state != PA_STREAM_READY)
363 pa_context_set_error(c, PA_ERR_KILLED);
364 pa_stream_set_state(s, PA_STREAM_FAILED);
370 static void check_smoother_status(pa_stream *s, pa_bool_t aposteriori, pa_bool_t force_start, pa_bool_t force_stop) {
374 pa_assert(!force_start || !force_stop);
379 x = pa_rtclock_now();
381 if (s->timing_info_valid) {
383 x -= s->timing_info.transport_usec;
385 x += s->timing_info.transport_usec;
388 if (s->suspended || s->corked || force_stop)
389 pa_smoother_pause(s->smoother, x);
390 else if (force_start || s->buffer_attr.prebuf == 0) {
392 if (!s->timing_info_valid &&
396 s->context->version >= 13) {
398 /* If the server supports STARTED events we take them as
399 * indications when audio really starts/stops playing, if
400 * we don't have any timing info yet -- instead of trying
401 * to be smart and guessing the server time. Otherwise the
402 * unknown transport delay add too much noise to our time
408 pa_smoother_resume(s->smoother, x, TRUE);
411 /* Please note that we have no idea if playback actually started
412 * if prebuf is non-zero! */
415 void pa_command_stream_moved(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
416 pa_context *c = userdata;
423 uint32_t maxlength = 0, fragsize = 0, minreq = 0, tlength = 0, prebuf = 0;
426 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_MOVED || command == PA_COMMAND_RECORD_STREAM_MOVED);
429 pa_assert(PA_REFCNT_VALUE(c) >= 1);
433 if (c->version < 12) {
434 pa_context_fail(c, PA_ERR_PROTOCOL);
438 if (pa_tagstruct_getu32(t, &channel) < 0 ||
439 pa_tagstruct_getu32(t, &di) < 0 ||
440 pa_tagstruct_gets(t, &dn) < 0 ||
441 pa_tagstruct_get_boolean(t, &suspended) < 0) {
442 pa_context_fail(c, PA_ERR_PROTOCOL);
446 if (c->version >= 13) {
448 if (command == PA_COMMAND_RECORD_STREAM_MOVED) {
449 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
450 pa_tagstruct_getu32(t, &fragsize) < 0 ||
451 pa_tagstruct_get_usec(t, &usec) < 0) {
452 pa_context_fail(c, PA_ERR_PROTOCOL);
456 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
457 pa_tagstruct_getu32(t, &tlength) < 0 ||
458 pa_tagstruct_getu32(t, &prebuf) < 0 ||
459 pa_tagstruct_getu32(t, &minreq) < 0 ||
460 pa_tagstruct_get_usec(t, &usec) < 0) {
461 pa_context_fail(c, PA_ERR_PROTOCOL);
467 if (!pa_tagstruct_eof(t)) {
468 pa_context_fail(c, PA_ERR_PROTOCOL);
472 if (!dn || di == PA_INVALID_INDEX) {
473 pa_context_fail(c, PA_ERR_PROTOCOL);
477 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_MOVED ? c->playback_streams : c->record_streams, channel)))
480 if (s->state != PA_STREAM_READY)
483 if (c->version >= 13) {
484 if (s->direction == PA_STREAM_RECORD)
485 s->timing_info.configured_source_usec = usec;
487 s->timing_info.configured_sink_usec = usec;
489 s->buffer_attr.maxlength = maxlength;
490 s->buffer_attr.fragsize = fragsize;
491 s->buffer_attr.tlength = tlength;
492 s->buffer_attr.prebuf = prebuf;
493 s->buffer_attr.minreq = minreq;
496 pa_xfree(s->device_name);
497 s->device_name = pa_xstrdup(dn);
498 s->device_index = di;
500 s->suspended = suspended;
502 check_smoother_status(s, TRUE, FALSE, FALSE);
503 request_auto_timing_update(s, TRUE);
505 if (s->moved_callback)
506 s->moved_callback(s, s->moved_userdata);
512 void pa_command_stream_buffer_attr(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
513 pa_context *c = userdata;
517 uint32_t maxlength = 0, fragsize = 0, minreq = 0, tlength = 0, prebuf = 0;
520 pa_assert(command == PA_COMMAND_PLAYBACK_BUFFER_ATTR_CHANGED || command == PA_COMMAND_RECORD_BUFFER_ATTR_CHANGED);
523 pa_assert(PA_REFCNT_VALUE(c) >= 1);
527 if (c->version < 15) {
528 pa_context_fail(c, PA_ERR_PROTOCOL);
532 if (pa_tagstruct_getu32(t, &channel) < 0) {
533 pa_context_fail(c, PA_ERR_PROTOCOL);
537 if (command == PA_COMMAND_RECORD_STREAM_MOVED) {
538 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
539 pa_tagstruct_getu32(t, &fragsize) < 0 ||
540 pa_tagstruct_get_usec(t, &usec) < 0) {
541 pa_context_fail(c, PA_ERR_PROTOCOL);
545 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
546 pa_tagstruct_getu32(t, &tlength) < 0 ||
547 pa_tagstruct_getu32(t, &prebuf) < 0 ||
548 pa_tagstruct_getu32(t, &minreq) < 0 ||
549 pa_tagstruct_get_usec(t, &usec) < 0) {
550 pa_context_fail(c, PA_ERR_PROTOCOL);
555 if (!pa_tagstruct_eof(t)) {
556 pa_context_fail(c, PA_ERR_PROTOCOL);
560 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_BUFFER_ATTR_CHANGED ? c->playback_streams : c->record_streams, channel)))
563 if (s->state != PA_STREAM_READY)
566 if (s->direction == PA_STREAM_RECORD)
567 s->timing_info.configured_source_usec = usec;
569 s->timing_info.configured_sink_usec = usec;
571 s->buffer_attr.maxlength = maxlength;
572 s->buffer_attr.fragsize = fragsize;
573 s->buffer_attr.tlength = tlength;
574 s->buffer_attr.prebuf = prebuf;
575 s->buffer_attr.minreq = minreq;
577 request_auto_timing_update(s, TRUE);
579 if (s->buffer_attr_callback)
580 s->buffer_attr_callback(s, s->buffer_attr_userdata);
586 void pa_command_stream_suspended(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
587 pa_context *c = userdata;
593 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED || command == PA_COMMAND_RECORD_STREAM_SUSPENDED);
596 pa_assert(PA_REFCNT_VALUE(c) >= 1);
600 if (c->version < 12) {
601 pa_context_fail(c, PA_ERR_PROTOCOL);
605 if (pa_tagstruct_getu32(t, &channel) < 0 ||
606 pa_tagstruct_get_boolean(t, &suspended) < 0 ||
607 !pa_tagstruct_eof(t)) {
608 pa_context_fail(c, PA_ERR_PROTOCOL);
612 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED ? c->playback_streams : c->record_streams, channel)))
615 if (s->state != PA_STREAM_READY)
618 s->suspended = suspended;
620 check_smoother_status(s, TRUE, FALSE, FALSE);
621 request_auto_timing_update(s, TRUE);
623 if (s->suspended_callback)
624 s->suspended_callback(s, s->suspended_userdata);
630 void pa_command_stream_started(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
631 pa_context *c = userdata;
636 pa_assert(command == PA_COMMAND_STARTED);
639 pa_assert(PA_REFCNT_VALUE(c) >= 1);
643 if (c->version < 13) {
644 pa_context_fail(c, PA_ERR_PROTOCOL);
648 if (pa_tagstruct_getu32(t, &channel) < 0 ||
649 !pa_tagstruct_eof(t)) {
650 pa_context_fail(c, PA_ERR_PROTOCOL);
654 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
657 if (s->state != PA_STREAM_READY)
660 check_smoother_status(s, TRUE, TRUE, FALSE);
661 request_auto_timing_update(s, TRUE);
663 if (s->started_callback)
664 s->started_callback(s, s->started_userdata);
670 void pa_command_stream_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
671 pa_context *c = userdata;
674 pa_proplist *pl = NULL;
678 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_EVENT || command == PA_COMMAND_RECORD_STREAM_EVENT);
681 pa_assert(PA_REFCNT_VALUE(c) >= 1);
685 if (c->version < 15) {
686 pa_context_fail(c, PA_ERR_PROTOCOL);
690 pl = pa_proplist_new();
692 if (pa_tagstruct_getu32(t, &channel) < 0 ||
693 pa_tagstruct_gets(t, &event) < 0 ||
694 pa_tagstruct_get_proplist(t, pl) < 0 ||
695 !pa_tagstruct_eof(t) || !event) {
696 pa_context_fail(c, PA_ERR_PROTOCOL);
700 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_EVENT ? c->playback_streams : c->record_streams, channel)))
703 if (s->state != PA_STREAM_READY)
706 if (s->event_callback)
707 s->event_callback(s, event, pl, s->event_userdata);
713 pa_proplist_free(pl);
716 void pa_command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
718 pa_context *c = userdata;
719 uint32_t bytes, channel;
722 pa_assert(command == PA_COMMAND_REQUEST);
725 pa_assert(PA_REFCNT_VALUE(c) >= 1);
729 if (pa_tagstruct_getu32(t, &channel) < 0 ||
730 pa_tagstruct_getu32(t, &bytes) < 0 ||
731 !pa_tagstruct_eof(t)) {
732 pa_context_fail(c, PA_ERR_PROTOCOL);
736 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
739 if (s->state != PA_STREAM_READY)
742 s->requested_bytes += bytes;
744 if (s->requested_bytes > 0 && s->write_callback)
745 s->write_callback(s, (size_t) s->requested_bytes, s->write_userdata);
751 void pa_command_overflow_or_underflow(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
753 pa_context *c = userdata;
757 pa_assert(command == PA_COMMAND_OVERFLOW || command == PA_COMMAND_UNDERFLOW);
760 pa_assert(PA_REFCNT_VALUE(c) >= 1);
764 if (pa_tagstruct_getu32(t, &channel) < 0 ||
765 !pa_tagstruct_eof(t)) {
766 pa_context_fail(c, PA_ERR_PROTOCOL);
770 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
773 if (s->state != PA_STREAM_READY)
776 if (s->buffer_attr.prebuf > 0)
777 check_smoother_status(s, TRUE, FALSE, TRUE);
779 request_auto_timing_update(s, TRUE);
781 if (command == PA_COMMAND_OVERFLOW) {
782 if (s->overflow_callback)
783 s->overflow_callback(s, s->overflow_userdata);
784 } else if (command == PA_COMMAND_UNDERFLOW) {
785 if (s->underflow_callback)
786 s->underflow_callback(s, s->underflow_userdata);
793 static void invalidate_indexes(pa_stream *s, pa_bool_t r, pa_bool_t w) {
795 pa_assert(PA_REFCNT_VALUE(s) >= 1);
797 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
799 if (s->state != PA_STREAM_READY)
803 s->write_index_not_before = s->context->ctag;
805 if (s->timing_info_valid)
806 s->timing_info.write_index_corrupt = TRUE;
808 /* pa_log("write_index invalidated"); */
812 s->read_index_not_before = s->context->ctag;
814 if (s->timing_info_valid)
815 s->timing_info.read_index_corrupt = TRUE;
817 /* pa_log("read_index invalidated"); */
820 request_auto_timing_update(s, TRUE);
823 static void auto_timing_update_callback(pa_mainloop_api *m, pa_time_event *e, const struct timeval *t, void *userdata) {
824 pa_stream *s = userdata;
827 pa_assert(PA_REFCNT_VALUE(s) >= 1);
830 request_auto_timing_update(s, FALSE);
834 static void create_stream_complete(pa_stream *s) {
836 pa_assert(PA_REFCNT_VALUE(s) >= 1);
837 pa_assert(s->state == PA_STREAM_CREATING);
839 pa_stream_set_state(s, PA_STREAM_READY);
841 if (s->requested_bytes > 0 && s->write_callback)
842 s->write_callback(s, (size_t) s->requested_bytes, s->write_userdata);
844 if (s->flags & PA_STREAM_AUTO_TIMING_UPDATE) {
845 s->auto_timing_interval_usec = AUTO_TIMING_INTERVAL_START_USEC;
846 pa_assert(!s->auto_timing_update_event);
847 s->auto_timing_update_event = pa_context_rttime_new(s->context, pa_rtclock_now() + s->auto_timing_interval_usec, &auto_timing_update_callback, s);
849 request_auto_timing_update(s, TRUE);
852 check_smoother_status(s, TRUE, FALSE, FALSE);
855 static void automatic_buffer_attr(pa_stream *s, pa_buffer_attr *attr, const pa_sample_spec *ss) {
860 if (s->context->version >= 13)
863 /* Version older than 0.9.10 didn't do server side buffer_attr
864 * selection, hence we have to fake it on the client side. */
866 /* We choose fairly conservative values here, to not confuse
867 * old clients with extremely large playback buffers */
869 if (attr->maxlength == (uint32_t) -1)
870 attr->maxlength = 4*1024*1024; /* 4MB is the maximum queue length PulseAudio <= 0.9.9 supported. */
872 if (attr->tlength == (uint32_t) -1)
873 attr->tlength = (uint32_t) pa_usec_to_bytes(250*PA_USEC_PER_MSEC, ss); /* 250ms of buffering */
875 if (attr->minreq == (uint32_t) -1)
876 attr->minreq = (attr->tlength)/5; /* Ask for more data when there are only 200ms left in the playback buffer */
878 if (attr->prebuf == (uint32_t) -1)
879 attr->prebuf = attr->tlength; /* Start to play only when the playback is fully filled up once */
881 if (attr->fragsize == (uint32_t) -1)
882 attr->fragsize = attr->tlength; /* Pass data to the app only when the buffer is filled up once */
885 void pa_create_stream_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
886 pa_stream *s = userdata;
887 uint32_t requested_bytes = 0;
891 pa_assert(PA_REFCNT_VALUE(s) >= 1);
892 pa_assert(s->state == PA_STREAM_CREATING);
896 if (command != PA_COMMAND_REPLY) {
897 if (pa_context_handle_error(s->context, command, t, FALSE) < 0)
900 pa_stream_set_state(s, PA_STREAM_FAILED);
904 if (pa_tagstruct_getu32(t, &s->channel) < 0 ||
905 s->channel == PA_INVALID_INDEX ||
906 ((s->direction != PA_STREAM_UPLOAD) && (pa_tagstruct_getu32(t, &s->stream_index) < 0 || s->stream_index == PA_INVALID_INDEX)) ||
907 ((s->direction != PA_STREAM_RECORD) && pa_tagstruct_getu32(t, &requested_bytes) < 0)) {
908 pa_context_fail(s->context, PA_ERR_PROTOCOL);
912 s->requested_bytes = (int64_t) requested_bytes;
914 if (s->context->version >= 9) {
915 if (s->direction == PA_STREAM_PLAYBACK) {
916 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
917 pa_tagstruct_getu32(t, &s->buffer_attr.tlength) < 0 ||
918 pa_tagstruct_getu32(t, &s->buffer_attr.prebuf) < 0 ||
919 pa_tagstruct_getu32(t, &s->buffer_attr.minreq) < 0) {
920 pa_context_fail(s->context, PA_ERR_PROTOCOL);
923 } else if (s->direction == PA_STREAM_RECORD) {
924 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
925 pa_tagstruct_getu32(t, &s->buffer_attr.fragsize) < 0) {
926 pa_context_fail(s->context, PA_ERR_PROTOCOL);
932 if (s->context->version >= 12 && s->direction != PA_STREAM_UPLOAD) {
935 const char *dn = NULL;
938 if (pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
939 pa_tagstruct_get_channel_map(t, &cm) < 0 ||
940 pa_tagstruct_getu32(t, &s->device_index) < 0 ||
941 pa_tagstruct_gets(t, &dn) < 0 ||
942 pa_tagstruct_get_boolean(t, &suspended) < 0) {
943 pa_context_fail(s->context, PA_ERR_PROTOCOL);
947 if (!dn || s->device_index == PA_INVALID_INDEX ||
948 ss.channels != cm.channels ||
949 !pa_channel_map_valid(&cm) ||
950 !pa_sample_spec_valid(&ss) ||
951 (!(s->flags & PA_STREAM_FIX_FORMAT) && ss.format != s->sample_spec.format) ||
952 (!(s->flags & PA_STREAM_FIX_RATE) && ss.rate != s->sample_spec.rate) ||
953 (!(s->flags & PA_STREAM_FIX_CHANNELS) && !pa_channel_map_equal(&cm, &s->channel_map))) {
954 pa_context_fail(s->context, PA_ERR_PROTOCOL);
958 pa_xfree(s->device_name);
959 s->device_name = pa_xstrdup(dn);
960 s->suspended = suspended;
966 if (s->context->version >= 13 && s->direction != PA_STREAM_UPLOAD) {
969 if (pa_tagstruct_get_usec(t, &usec) < 0) {
970 pa_context_fail(s->context, PA_ERR_PROTOCOL);
974 if (s->direction == PA_STREAM_RECORD)
975 s->timing_info.configured_source_usec = usec;
977 s->timing_info.configured_sink_usec = usec;
980 if (!pa_tagstruct_eof(t)) {
981 pa_context_fail(s->context, PA_ERR_PROTOCOL);
985 if (s->direction == PA_STREAM_RECORD) {
986 pa_assert(!s->record_memblockq);
988 s->record_memblockq = pa_memblockq_new(
990 s->buffer_attr.maxlength,
992 pa_frame_size(&s->sample_spec),
999 s->channel_valid = TRUE;
1000 pa_dynarray_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, s->channel, s);
1002 create_stream_complete(s);
1008 static int create_stream(
1009 pa_stream_direction_t direction,
1012 const pa_buffer_attr *attr,
1013 pa_stream_flags_t flags,
1014 const pa_cvolume *volume,
1015 pa_stream *sync_stream) {
1019 pa_bool_t volume_set = FALSE;
1022 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1023 pa_assert(direction == PA_STREAM_PLAYBACK || direction == PA_STREAM_RECORD);
1025 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1026 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
1027 PA_CHECK_VALIDITY(s->context, s->direct_on_input == PA_INVALID_INDEX || direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1028 PA_CHECK_VALIDITY(s->context, !(flags & ~(PA_STREAM_START_CORKED|
1029 PA_STREAM_INTERPOLATE_TIMING|
1030 PA_STREAM_NOT_MONOTONIC|
1031 PA_STREAM_AUTO_TIMING_UPDATE|
1032 PA_STREAM_NO_REMAP_CHANNELS|
1033 PA_STREAM_NO_REMIX_CHANNELS|
1034 PA_STREAM_FIX_FORMAT|
1036 PA_STREAM_FIX_CHANNELS|
1037 PA_STREAM_DONT_MOVE|
1038 PA_STREAM_VARIABLE_RATE|
1039 PA_STREAM_PEAK_DETECT|
1040 PA_STREAM_START_MUTED|
1041 PA_STREAM_ADJUST_LATENCY|
1042 PA_STREAM_EARLY_REQUESTS|
1043 PA_STREAM_DONT_INHIBIT_AUTO_SUSPEND|
1044 PA_STREAM_START_UNMUTED|
1045 PA_STREAM_FAIL_ON_SUSPEND|
1046 PA_STREAM_RELATIVE_VOLUME)), PA_ERR_INVALID);
1048 PA_CHECK_VALIDITY(s->context, s->context->version >= 12 || !(flags & PA_STREAM_VARIABLE_RATE), PA_ERR_NOTSUPPORTED);
1049 PA_CHECK_VALIDITY(s->context, s->context->version >= 13 || !(flags & PA_STREAM_PEAK_DETECT), PA_ERR_NOTSUPPORTED);
1050 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1051 /* Althought some of the other flags are not supported on older
1052 * version, we don't check for them here, because it doesn't hurt
1053 * when they are passed but actually not supported. This makes
1054 * client development easier */
1056 PA_CHECK_VALIDITY(s->context, direction == PA_STREAM_PLAYBACK || !(flags & (PA_STREAM_START_MUTED)), PA_ERR_INVALID);
1057 PA_CHECK_VALIDITY(s->context, direction == PA_STREAM_RECORD || !(flags & (PA_STREAM_PEAK_DETECT)), PA_ERR_INVALID);
1058 PA_CHECK_VALIDITY(s->context, !volume || volume->channels == s->sample_spec.channels, PA_ERR_INVALID);
1059 PA_CHECK_VALIDITY(s->context, !sync_stream || (direction == PA_STREAM_PLAYBACK && sync_stream->direction == PA_STREAM_PLAYBACK), PA_ERR_INVALID);
1060 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);
1064 s->direction = direction;
1066 s->corked = !!(flags & PA_STREAM_START_CORKED);
1069 s->syncid = sync_stream->syncid;
1072 s->buffer_attr = *attr;
1073 automatic_buffer_attr(s, &s->buffer_attr, &s->sample_spec);
1075 if (flags & PA_STREAM_INTERPOLATE_TIMING) {
1078 x = pa_rtclock_now();
1080 pa_assert(!s->smoother);
1081 s->smoother = pa_smoother_new(
1082 SMOOTHER_ADJUST_TIME,
1083 SMOOTHER_HISTORY_TIME,
1084 !(flags & PA_STREAM_NOT_MONOTONIC),
1086 SMOOTHER_MIN_HISTORY,
1092 dev = s->direction == PA_STREAM_PLAYBACK ? s->context->conf->default_sink : s->context->conf->default_source;
1094 t = pa_tagstruct_command(
1096 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM),
1099 if (s->context->version < 13)
1100 pa_tagstruct_puts(t, pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME));
1104 PA_TAG_SAMPLE_SPEC, &s->sample_spec,
1105 PA_TAG_CHANNEL_MAP, &s->channel_map,
1106 PA_TAG_U32, PA_INVALID_INDEX,
1108 PA_TAG_U32, s->buffer_attr.maxlength,
1109 PA_TAG_BOOLEAN, s->corked,
1112 if (s->direction == PA_STREAM_PLAYBACK) {
1117 PA_TAG_U32, s->buffer_attr.tlength,
1118 PA_TAG_U32, s->buffer_attr.prebuf,
1119 PA_TAG_U32, s->buffer_attr.minreq,
1120 PA_TAG_U32, s->syncid,
1123 volume_set = !!volume;
1126 volume = pa_cvolume_reset(&cv, s->sample_spec.channels);
1128 pa_tagstruct_put_cvolume(t, volume);
1130 pa_tagstruct_putu32(t, s->buffer_attr.fragsize);
1132 if (s->context->version >= 12) {
1135 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMAP_CHANNELS,
1136 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMIX_CHANNELS,
1137 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_FORMAT,
1138 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_RATE,
1139 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_CHANNELS,
1140 PA_TAG_BOOLEAN, flags & PA_STREAM_DONT_MOVE,
1141 PA_TAG_BOOLEAN, flags & PA_STREAM_VARIABLE_RATE,
1145 if (s->context->version >= 13) {
1147 if (s->direction == PA_STREAM_PLAYBACK)
1148 pa_tagstruct_put_boolean(t, flags & PA_STREAM_START_MUTED);
1150 pa_tagstruct_put_boolean(t, flags & PA_STREAM_PEAK_DETECT);
1154 PA_TAG_BOOLEAN, flags & PA_STREAM_ADJUST_LATENCY,
1155 PA_TAG_PROPLIST, s->proplist,
1158 if (s->direction == PA_STREAM_RECORD)
1159 pa_tagstruct_putu32(t, s->direct_on_input);
1162 if (s->context->version >= 14) {
1164 if (s->direction == PA_STREAM_PLAYBACK)
1165 pa_tagstruct_put_boolean(t, volume_set);
1167 pa_tagstruct_put_boolean(t, flags & PA_STREAM_EARLY_REQUESTS);
1170 if (s->context->version >= 15) {
1172 if (s->direction == PA_STREAM_PLAYBACK)
1173 pa_tagstruct_put_boolean(t, flags & (PA_STREAM_START_MUTED|PA_STREAM_START_UNMUTED));
1175 pa_tagstruct_put_boolean(t, flags & PA_STREAM_DONT_INHIBIT_AUTO_SUSPEND);
1176 pa_tagstruct_put_boolean(t, flags & PA_STREAM_FAIL_ON_SUSPEND);
1179 if (s->context->version >= 17) {
1181 if (s->direction == PA_STREAM_PLAYBACK)
1182 pa_tagstruct_put_boolean(t, flags & PA_STREAM_RELATIVE_VOLUME);
1186 pa_pstream_send_tagstruct(s->context->pstream, t);
1187 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s, NULL);
1189 pa_stream_set_state(s, PA_STREAM_CREATING);
1195 int pa_stream_connect_playback(
1198 const pa_buffer_attr *attr,
1199 pa_stream_flags_t flags,
1200 const pa_cvolume *volume,
1201 pa_stream *sync_stream) {
1204 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1206 return create_stream(PA_STREAM_PLAYBACK, s, dev, attr, flags, volume, sync_stream);
1209 int pa_stream_connect_record(
1212 const pa_buffer_attr *attr,
1213 pa_stream_flags_t flags) {
1216 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1218 return create_stream(PA_STREAM_RECORD, s, dev, attr, flags, NULL, NULL);
1221 int pa_stream_begin_write(
1227 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1229 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1230 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1231 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || s->direction == PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1232 PA_CHECK_VALIDITY(s->context, data, PA_ERR_INVALID);
1233 PA_CHECK_VALIDITY(s->context, nbytes && *nbytes != 0, PA_ERR_INVALID);
1235 if (*nbytes != (size_t) -1) {
1238 m = pa_mempool_block_size_max(s->context->mempool);
1239 fs = pa_frame_size(&s->sample_spec);
1246 if (!s->write_memblock) {
1247 s->write_memblock = pa_memblock_new(s->context->mempool, *nbytes);
1248 s->write_data = pa_memblock_acquire(s->write_memblock);
1251 *data = s->write_data;
1252 *nbytes = pa_memblock_get_length(s->write_memblock);
1257 int pa_stream_cancel_write(
1261 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1263 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1264 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1265 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || s->direction == PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1266 PA_CHECK_VALIDITY(s->context, s->write_memblock, PA_ERR_BADSTATE);
1268 pa_assert(s->write_data);
1270 pa_memblock_release(s->write_memblock);
1271 pa_memblock_unref(s->write_memblock);
1272 s->write_memblock = NULL;
1273 s->write_data = NULL;
1278 int pa_stream_write(
1282 pa_free_cb_t free_cb,
1284 pa_seek_mode_t seek) {
1287 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1290 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1291 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1292 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || s->direction == PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1293 PA_CHECK_VALIDITY(s->context, seek <= PA_SEEK_RELATIVE_END, PA_ERR_INVALID);
1294 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || (seek == PA_SEEK_RELATIVE && offset == 0), PA_ERR_INVALID);
1295 PA_CHECK_VALIDITY(s->context,
1296 !s->write_memblock ||
1297 ((data >= s->write_data) &&
1298 ((const char*) data + length <= (const char*) s->write_data + pa_memblock_get_length(s->write_memblock))),
1300 PA_CHECK_VALIDITY(s->context, !free_cb || !s->write_memblock, PA_ERR_INVALID);
1302 if (s->write_memblock) {
1305 /* pa_stream_write_begin() was called before */
1307 pa_memblock_release(s->write_memblock);
1309 chunk.memblock = s->write_memblock;
1310 chunk.index = (const char *) data - (const char *) s->write_data;
1311 chunk.length = length;
1313 s->write_memblock = NULL;
1314 s->write_data = NULL;
1316 pa_pstream_send_memblock(s->context->pstream, s->channel, offset, seek, &chunk);
1317 pa_memblock_unref(chunk.memblock);
1320 pa_seek_mode_t t_seek = seek;
1321 int64_t t_offset = offset;
1322 size_t t_length = length;
1323 const void *t_data = data;
1325 /* pa_stream_write_begin() was not called before */
1327 while (t_length > 0) {
1332 if (free_cb && !pa_pstream_get_shm(s->context->pstream)) {
1333 chunk.memblock = pa_memblock_new_user(s->context->mempool, (void*) t_data, t_length, free_cb, 1);
1334 chunk.length = t_length;
1338 chunk.length = PA_MIN(t_length, pa_mempool_block_size_max(s->context->mempool));
1339 chunk.memblock = pa_memblock_new(s->context->mempool, chunk.length);
1341 d = pa_memblock_acquire(chunk.memblock);
1342 memcpy(d, t_data, chunk.length);
1343 pa_memblock_release(chunk.memblock);
1346 pa_pstream_send_memblock(s->context->pstream, s->channel, t_offset, t_seek, &chunk);
1349 t_seek = PA_SEEK_RELATIVE;
1351 t_data = (const uint8_t*) t_data + chunk.length;
1352 t_length -= chunk.length;
1354 pa_memblock_unref(chunk.memblock);
1357 if (free_cb && pa_pstream_get_shm(s->context->pstream))
1358 free_cb((void*) data);
1361 /* This is obviously wrong since we ignore the seeking index . But
1362 * that's OK, the server side applies the same error */
1363 s->requested_bytes -= (seek == PA_SEEK_RELATIVE ? offset : 0) + (int64_t) length;
1365 if (s->direction == PA_STREAM_PLAYBACK) {
1367 /* Update latency request correction */
1368 if (s->write_index_corrections[s->current_write_index_correction].valid) {
1370 if (seek == PA_SEEK_ABSOLUTE) {
1371 s->write_index_corrections[s->current_write_index_correction].corrupt = FALSE;
1372 s->write_index_corrections[s->current_write_index_correction].absolute = TRUE;
1373 s->write_index_corrections[s->current_write_index_correction].value = offset + (int64_t) length;
1374 } else if (seek == PA_SEEK_RELATIVE) {
1375 if (!s->write_index_corrections[s->current_write_index_correction].corrupt)
1376 s->write_index_corrections[s->current_write_index_correction].value += offset + (int64_t) length;
1378 s->write_index_corrections[s->current_write_index_correction].corrupt = TRUE;
1381 /* Update the write index in the already available latency data */
1382 if (s->timing_info_valid) {
1384 if (seek == PA_SEEK_ABSOLUTE) {
1385 s->timing_info.write_index_corrupt = FALSE;
1386 s->timing_info.write_index = offset + (int64_t) length;
1387 } else if (seek == PA_SEEK_RELATIVE) {
1388 if (!s->timing_info.write_index_corrupt)
1389 s->timing_info.write_index += offset + (int64_t) length;
1391 s->timing_info.write_index_corrupt = TRUE;
1394 if (!s->timing_info_valid || s->timing_info.write_index_corrupt)
1395 request_auto_timing_update(s, TRUE);
1401 int pa_stream_peek(pa_stream *s, const void **data, size_t *length) {
1403 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1407 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1408 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1409 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1411 if (!s->peek_memchunk.memblock) {
1413 if (pa_memblockq_peek(s->record_memblockq, &s->peek_memchunk) < 0) {
1419 s->peek_data = pa_memblock_acquire(s->peek_memchunk.memblock);
1422 pa_assert(s->peek_data);
1423 *data = (uint8_t*) s->peek_data + s->peek_memchunk.index;
1424 *length = s->peek_memchunk.length;
1428 int pa_stream_drop(pa_stream *s) {
1430 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1432 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1433 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1434 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1435 PA_CHECK_VALIDITY(s->context, s->peek_memchunk.memblock, PA_ERR_BADSTATE);
1437 pa_memblockq_drop(s->record_memblockq, s->peek_memchunk.length);
1439 /* Fix the simulated local read index */
1440 if (s->timing_info_valid && !s->timing_info.read_index_corrupt)
1441 s->timing_info.read_index += (int64_t) s->peek_memchunk.length;
1443 pa_assert(s->peek_data);
1444 pa_memblock_release(s->peek_memchunk.memblock);
1445 pa_memblock_unref(s->peek_memchunk.memblock);
1446 pa_memchunk_reset(&s->peek_memchunk);
1451 size_t pa_stream_writable_size(pa_stream *s) {
1453 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1455 PA_CHECK_VALIDITY_RETURN_ANY(s->context, !pa_detect_fork(), PA_ERR_FORKED, (size_t) -1);
1456 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
1457 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
1459 return s->requested_bytes > 0 ? (size_t) s->requested_bytes : 0;
1462 size_t pa_stream_readable_size(pa_stream *s) {
1464 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1466 PA_CHECK_VALIDITY_RETURN_ANY(s->context, !pa_detect_fork(), PA_ERR_FORKED, (size_t) -1);
1467 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
1468 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
1470 return pa_memblockq_get_length(s->record_memblockq);
1473 pa_operation * pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1479 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1481 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1482 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1483 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1485 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1487 t = pa_tagstruct_command(s->context, PA_COMMAND_DRAIN_PLAYBACK_STREAM, &tag);
1488 pa_tagstruct_putu32(t, s->channel);
1489 pa_pstream_send_tagstruct(s->context->pstream, t);
1490 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);
1495 static pa_usec_t calc_time(pa_stream *s, pa_bool_t ignore_transport) {
1499 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1500 pa_assert(s->state == PA_STREAM_READY);
1501 pa_assert(s->direction != PA_STREAM_UPLOAD);
1502 pa_assert(s->timing_info_valid);
1503 pa_assert(s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt);
1504 pa_assert(s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt);
1506 if (s->direction == PA_STREAM_PLAYBACK) {
1507 /* The last byte that was written into the output device
1508 * had this time value associated */
1509 usec = pa_bytes_to_usec(s->timing_info.read_index < 0 ? 0 : (uint64_t) s->timing_info.read_index, &s->sample_spec);
1511 if (!s->corked && !s->suspended) {
1513 if (!ignore_transport)
1514 /* Because the latency info took a little time to come
1515 * to us, we assume that the real output time is actually
1517 usec += s->timing_info.transport_usec;
1519 /* However, the output device usually maintains a buffer
1520 too, hence the real sample currently played is a little
1522 if (s->timing_info.sink_usec >= usec)
1525 usec -= s->timing_info.sink_usec;
1529 pa_assert(s->direction == PA_STREAM_RECORD);
1531 /* The last byte written into the server side queue had
1532 * this time value associated */
1533 usec = pa_bytes_to_usec(s->timing_info.write_index < 0 ? 0 : (uint64_t) s->timing_info.write_index, &s->sample_spec);
1535 if (!s->corked && !s->suspended) {
1537 if (!ignore_transport)
1538 /* Add transport latency */
1539 usec += s->timing_info.transport_usec;
1541 /* Add latency of data in device buffer */
1542 usec += s->timing_info.source_usec;
1544 /* If this is a monitor source, we need to correct the
1545 * time by the playback device buffer */
1546 if (s->timing_info.sink_usec >= usec)
1549 usec -= s->timing_info.sink_usec;
1556 static void stream_get_timing_info_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1557 pa_operation *o = userdata;
1558 struct timeval local, remote, now;
1560 pa_bool_t playing = FALSE;
1561 uint64_t underrun_for = 0, playing_for = 0;
1565 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1567 if (!o->context || !o->stream)
1570 i = &o->stream->timing_info;
1572 o->stream->timing_info_valid = FALSE;
1573 i->write_index_corrupt = TRUE;
1574 i->read_index_corrupt = TRUE;
1576 if (command != PA_COMMAND_REPLY) {
1577 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
1582 if (pa_tagstruct_get_usec(t, &i->sink_usec) < 0 ||
1583 pa_tagstruct_get_usec(t, &i->source_usec) < 0 ||
1584 pa_tagstruct_get_boolean(t, &playing) < 0 ||
1585 pa_tagstruct_get_timeval(t, &local) < 0 ||
1586 pa_tagstruct_get_timeval(t, &remote) < 0 ||
1587 pa_tagstruct_gets64(t, &i->write_index) < 0 ||
1588 pa_tagstruct_gets64(t, &i->read_index) < 0) {
1590 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1594 if (o->context->version >= 13 &&
1595 o->stream->direction == PA_STREAM_PLAYBACK)
1596 if (pa_tagstruct_getu64(t, &underrun_for) < 0 ||
1597 pa_tagstruct_getu64(t, &playing_for) < 0) {
1599 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1604 if (!pa_tagstruct_eof(t)) {
1605 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1608 o->stream->timing_info_valid = TRUE;
1609 i->write_index_corrupt = FALSE;
1610 i->read_index_corrupt = FALSE;
1612 i->playing = (int) playing;
1613 i->since_underrun = (int64_t) (playing ? playing_for : underrun_for);
1615 pa_gettimeofday(&now);
1617 /* Calculcate timestamps */
1618 if (pa_timeval_cmp(&local, &remote) <= 0 && pa_timeval_cmp(&remote, &now) <= 0) {
1619 /* local and remote seem to have synchronized clocks */
1621 if (o->stream->direction == PA_STREAM_PLAYBACK)
1622 i->transport_usec = pa_timeval_diff(&remote, &local);
1624 i->transport_usec = pa_timeval_diff(&now, &remote);
1626 i->synchronized_clocks = TRUE;
1627 i->timestamp = remote;
1629 /* clocks are not synchronized, let's estimate latency then */
1630 i->transport_usec = pa_timeval_diff(&now, &local)/2;
1631 i->synchronized_clocks = FALSE;
1632 i->timestamp = local;
1633 pa_timeval_add(&i->timestamp, i->transport_usec);
1636 /* Invalidate read and write indexes if necessary */
1637 if (tag < o->stream->read_index_not_before)
1638 i->read_index_corrupt = TRUE;
1640 if (tag < o->stream->write_index_not_before)
1641 i->write_index_corrupt = TRUE;
1643 if (o->stream->direction == PA_STREAM_PLAYBACK) {
1644 /* Write index correction */
1647 uint32_t ctag = tag;
1649 /* Go through the saved correction values and add up the
1650 * total correction.*/
1651 for (n = 0, j = o->stream->current_write_index_correction+1;
1652 n < PA_MAX_WRITE_INDEX_CORRECTIONS;
1653 n++, j = (j + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS) {
1655 /* Step over invalid data or out-of-date data */
1656 if (!o->stream->write_index_corrections[j].valid ||
1657 o->stream->write_index_corrections[j].tag < ctag)
1660 /* Make sure that everything is in order */
1661 ctag = o->stream->write_index_corrections[j].tag+1;
1663 /* Now fix the write index */
1664 if (o->stream->write_index_corrections[j].corrupt) {
1665 /* A corrupting seek was made */
1666 i->write_index_corrupt = TRUE;
1667 } else if (o->stream->write_index_corrections[j].absolute) {
1668 /* An absolute seek was made */
1669 i->write_index = o->stream->write_index_corrections[j].value;
1670 i->write_index_corrupt = FALSE;
1671 } else if (!i->write_index_corrupt) {
1672 /* A relative seek was made */
1673 i->write_index += o->stream->write_index_corrections[j].value;
1677 /* Clear old correction entries */
1678 for (n = 0; n < PA_MAX_WRITE_INDEX_CORRECTIONS; n++) {
1679 if (!o->stream->write_index_corrections[n].valid)
1682 if (o->stream->write_index_corrections[n].tag <= tag)
1683 o->stream->write_index_corrections[n].valid = FALSE;
1687 if (o->stream->direction == PA_STREAM_RECORD) {
1688 /* Read index correction */
1690 if (!i->read_index_corrupt)
1691 i->read_index -= (int64_t) pa_memblockq_get_length(o->stream->record_memblockq);
1694 /* Update smoother */
1695 if (o->stream->smoother) {
1698 u = x = pa_rtclock_now() - i->transport_usec;
1700 if (o->stream->direction == PA_STREAM_PLAYBACK && o->context->version >= 13) {
1703 /* If we weren't playing then it will take some time
1704 * until the audio will actually come out through the
1705 * speakers. Since we follow that timing here, we need
1706 * to try to fix this up */
1708 su = pa_bytes_to_usec((uint64_t) i->since_underrun, &o->stream->sample_spec);
1710 if (su < i->sink_usec)
1711 x += i->sink_usec - su;
1715 pa_smoother_pause(o->stream->smoother, x);
1717 /* Update the smoother */
1718 if ((o->stream->direction == PA_STREAM_PLAYBACK && !i->read_index_corrupt) ||
1719 (o->stream->direction == PA_STREAM_RECORD && !i->write_index_corrupt))
1720 pa_smoother_put(o->stream->smoother, u, calc_time(o->stream, TRUE));
1723 pa_smoother_resume(o->stream->smoother, x, TRUE);
1727 o->stream->auto_timing_update_requested = FALSE;
1729 if (o->stream->latency_update_callback)
1730 o->stream->latency_update_callback(o->stream, o->stream->latency_update_userdata);
1732 if (o->callback && o->stream && o->stream->state == PA_STREAM_READY) {
1733 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1734 cb(o->stream, o->stream->timing_info_valid, o->userdata);
1739 pa_operation_done(o);
1740 pa_operation_unref(o);
1743 pa_operation* pa_stream_update_timing_info(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1751 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1753 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1754 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1755 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1757 if (s->direction == PA_STREAM_PLAYBACK) {
1758 /* Find a place to store the write_index correction data for this entry */
1759 cidx = (s->current_write_index_correction + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS;
1761 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
1762 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !s->write_index_corrections[cidx].valid, PA_ERR_INTERNAL);
1764 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1766 t = pa_tagstruct_command(
1768 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_GET_PLAYBACK_LATENCY : PA_COMMAND_GET_RECORD_LATENCY),
1770 pa_tagstruct_putu32(t, s->channel);
1771 pa_tagstruct_put_timeval(t, pa_gettimeofday(&now));
1773 pa_pstream_send_tagstruct(s->context->pstream, t);
1774 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);
1776 if (s->direction == PA_STREAM_PLAYBACK) {
1777 /* Fill in initial correction data */
1779 s->current_write_index_correction = cidx;
1781 s->write_index_corrections[cidx].valid = TRUE;
1782 s->write_index_corrections[cidx].absolute = FALSE;
1783 s->write_index_corrections[cidx].corrupt = FALSE;
1784 s->write_index_corrections[cidx].tag = tag;
1785 s->write_index_corrections[cidx].value = 0;
1791 void pa_stream_disconnect_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1792 pa_stream *s = userdata;
1796 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1800 if (command != PA_COMMAND_REPLY) {
1801 if (pa_context_handle_error(s->context, command, t, FALSE) < 0)
1804 pa_stream_set_state(s, PA_STREAM_FAILED);
1806 } else if (!pa_tagstruct_eof(t)) {
1807 pa_context_fail(s->context, PA_ERR_PROTOCOL);
1811 pa_stream_set_state(s, PA_STREAM_TERMINATED);
1817 int pa_stream_disconnect(pa_stream *s) {
1822 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1824 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1825 PA_CHECK_VALIDITY(s->context, s->channel_valid, PA_ERR_BADSTATE);
1826 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1830 t = pa_tagstruct_command(
1832 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_DELETE_PLAYBACK_STREAM :
1833 (s->direction == PA_STREAM_RECORD ? PA_COMMAND_DELETE_RECORD_STREAM : PA_COMMAND_DELETE_UPLOAD_STREAM)),
1835 pa_tagstruct_putu32(t, s->channel);
1836 pa_pstream_send_tagstruct(s->context->pstream, t);
1837 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s, NULL);
1843 void pa_stream_set_read_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1845 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1847 if (pa_detect_fork())
1850 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1853 s->read_callback = cb;
1854 s->read_userdata = userdata;
1857 void pa_stream_set_write_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1859 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1861 if (pa_detect_fork())
1864 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1867 s->write_callback = cb;
1868 s->write_userdata = userdata;
1871 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1873 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1875 if (pa_detect_fork())
1878 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1881 s->state_callback = cb;
1882 s->state_userdata = userdata;
1885 void pa_stream_set_overflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1887 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1889 if (pa_detect_fork())
1892 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1895 s->overflow_callback = cb;
1896 s->overflow_userdata = userdata;
1899 void pa_stream_set_underflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1901 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1903 if (pa_detect_fork())
1906 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1909 s->underflow_callback = cb;
1910 s->underflow_userdata = userdata;
1913 void pa_stream_set_latency_update_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1915 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1917 if (pa_detect_fork())
1920 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1923 s->latency_update_callback = cb;
1924 s->latency_update_userdata = userdata;
1927 void pa_stream_set_moved_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1929 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1931 if (pa_detect_fork())
1934 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1937 s->moved_callback = cb;
1938 s->moved_userdata = userdata;
1941 void pa_stream_set_suspended_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1943 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1945 if (pa_detect_fork())
1948 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1951 s->suspended_callback = cb;
1952 s->suspended_userdata = userdata;
1955 void pa_stream_set_started_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1957 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1959 if (pa_detect_fork())
1962 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1965 s->started_callback = cb;
1966 s->started_userdata = userdata;
1969 void pa_stream_set_event_callback(pa_stream *s, pa_stream_event_cb_t cb, void *userdata) {
1971 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1973 if (pa_detect_fork())
1976 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1979 s->event_callback = cb;
1980 s->event_userdata = userdata;
1983 void pa_stream_set_buffer_attr_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1985 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1987 if (pa_detect_fork())
1990 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1993 s->buffer_attr_callback = cb;
1994 s->buffer_attr_userdata = userdata;
1997 void pa_stream_simple_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1998 pa_operation *o = userdata;
2003 pa_assert(PA_REFCNT_VALUE(o) >= 1);
2008 if (command != PA_COMMAND_REPLY) {
2009 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
2013 } else if (!pa_tagstruct_eof(t)) {
2014 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2019 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
2020 cb(o->stream, success, o->userdata);
2024 pa_operation_done(o);
2025 pa_operation_unref(o);
2028 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata) {
2034 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2036 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2037 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2038 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2042 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2044 t = pa_tagstruct_command(
2046 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CORK_PLAYBACK_STREAM : PA_COMMAND_CORK_RECORD_STREAM),
2048 pa_tagstruct_putu32(t, s->channel);
2049 pa_tagstruct_put_boolean(t, !!b);
2050 pa_pstream_send_tagstruct(s->context->pstream, t);
2051 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);
2053 check_smoother_status(s, FALSE, FALSE, FALSE);
2055 /* This might cause the indexes to hang/start again, hence
2056 * let's request a timing update */
2057 request_auto_timing_update(s, TRUE);
2062 static pa_operation* stream_send_simple_command(pa_stream *s, uint32_t command, pa_stream_success_cb_t cb, void *userdata) {
2068 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2070 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2071 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2073 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2075 t = pa_tagstruct_command(s->context, command, &tag);
2076 pa_tagstruct_putu32(t, s->channel);
2077 pa_pstream_send_tagstruct(s->context->pstream, t);
2078 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);
2083 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
2087 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2089 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2090 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2091 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2093 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)))
2096 if (s->direction == PA_STREAM_PLAYBACK) {
2098 if (s->write_index_corrections[s->current_write_index_correction].valid)
2099 s->write_index_corrections[s->current_write_index_correction].corrupt = TRUE;
2101 if (s->buffer_attr.prebuf > 0)
2102 check_smoother_status(s, FALSE, FALSE, TRUE);
2104 /* This will change the write index, but leave the
2105 * read index untouched. */
2106 invalidate_indexes(s, FALSE, TRUE);
2109 /* For record streams this has no influence on the write
2110 * index, but the read index might jump. */
2111 invalidate_indexes(s, TRUE, FALSE);
2116 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
2120 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2122 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2123 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2124 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
2125 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
2127 if (!(o = stream_send_simple_command(s, PA_COMMAND_PREBUF_PLAYBACK_STREAM, cb, userdata)))
2130 /* This might cause the read index to hang again, hence
2131 * let's request a timing update */
2132 request_auto_timing_update(s, TRUE);
2137 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
2141 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2143 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2144 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2145 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
2146 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
2148 if (!(o = stream_send_simple_command(s, PA_COMMAND_TRIGGER_PLAYBACK_STREAM, cb, userdata)))
2151 /* This might cause the read index to start moving again, hence
2152 * let's request a timing update */
2153 request_auto_timing_update(s, TRUE);
2158 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata) {
2162 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2165 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2166 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2167 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2169 if (s->context->version >= 13) {
2170 pa_proplist *p = pa_proplist_new();
2172 pa_proplist_sets(p, PA_PROP_MEDIA_NAME, name);
2173 o = pa_stream_proplist_update(s, PA_UPDATE_REPLACE, p, cb, userdata);
2174 pa_proplist_free(p);
2179 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2180 t = pa_tagstruct_command(
2182 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_NAME : PA_COMMAND_SET_PLAYBACK_STREAM_NAME),
2184 pa_tagstruct_putu32(t, s->channel);
2185 pa_tagstruct_puts(t, name);
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);
2193 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec) {
2197 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2199 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2200 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2201 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2202 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
2203 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
2204 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
2207 usec = pa_smoother_get(s->smoother, pa_rtclock_now());
2209 usec = calc_time(s, FALSE);
2211 /* Make sure the time runs monotonically */
2212 if (!(s->flags & PA_STREAM_NOT_MONOTONIC)) {
2213 if (usec < s->previous_time)
2214 usec = s->previous_time;
2216 s->previous_time = usec;
2225 static pa_usec_t time_counter_diff(pa_stream *s, pa_usec_t a, pa_usec_t b, int *negative) {
2227 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2235 if (negative && s->direction == PA_STREAM_RECORD) {
2243 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative) {
2249 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2252 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2253 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2254 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2255 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
2256 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
2257 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
2259 if ((r = pa_stream_get_time(s, &t)) < 0)
2262 if (s->direction == PA_STREAM_PLAYBACK)
2263 cindex = s->timing_info.write_index;
2265 cindex = s->timing_info.read_index;
2270 c = pa_bytes_to_usec((uint64_t) cindex, &s->sample_spec);
2272 if (s->direction == PA_STREAM_PLAYBACK)
2273 *r_usec = time_counter_diff(s, c, t, negative);
2275 *r_usec = time_counter_diff(s, t, c, negative);
2280 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s) {
2282 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2284 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2285 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2286 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2287 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->timing_info_valid, PA_ERR_NODATA);
2289 return &s->timing_info;
2292 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s) {
2294 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2296 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2298 return &s->sample_spec;
2301 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s) {
2303 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2305 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2307 return &s->channel_map;
2310 const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s) {
2312 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2314 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2315 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2316 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 9, PA_ERR_NOTSUPPORTED);
2318 return &s->buffer_attr;
2321 static void stream_set_buffer_attr_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2322 pa_operation *o = userdata;
2327 pa_assert(PA_REFCNT_VALUE(o) >= 1);
2332 if (command != PA_COMMAND_REPLY) {
2333 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
2338 if (o->stream->direction == PA_STREAM_PLAYBACK) {
2339 if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
2340 pa_tagstruct_getu32(t, &o->stream->buffer_attr.tlength) < 0 ||
2341 pa_tagstruct_getu32(t, &o->stream->buffer_attr.prebuf) < 0 ||
2342 pa_tagstruct_getu32(t, &o->stream->buffer_attr.minreq) < 0) {
2343 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2346 } else if (o->stream->direction == PA_STREAM_RECORD) {
2347 if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
2348 pa_tagstruct_getu32(t, &o->stream->buffer_attr.fragsize) < 0) {
2349 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2354 if (o->stream->context->version >= 13) {
2357 if (pa_tagstruct_get_usec(t, &usec) < 0) {
2358 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2362 if (o->stream->direction == PA_STREAM_RECORD)
2363 o->stream->timing_info.configured_source_usec = usec;
2365 o->stream->timing_info.configured_sink_usec = usec;
2368 if (!pa_tagstruct_eof(t)) {
2369 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2375 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
2376 cb(o->stream, success, o->userdata);
2380 pa_operation_done(o);
2381 pa_operation_unref(o);
2385 pa_operation* pa_stream_set_buffer_attr(pa_stream *s, const pa_buffer_attr *attr, pa_stream_success_cb_t cb, void *userdata) {
2391 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2394 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2395 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2396 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2397 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2399 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2401 t = pa_tagstruct_command(
2403 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR : PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR),
2405 pa_tagstruct_putu32(t, s->channel);
2407 pa_tagstruct_putu32(t, attr->maxlength);
2409 if (s->direction == PA_STREAM_PLAYBACK)
2412 PA_TAG_U32, attr->tlength,
2413 PA_TAG_U32, attr->prebuf,
2414 PA_TAG_U32, attr->minreq,
2417 pa_tagstruct_putu32(t, attr->fragsize);
2419 if (s->context->version >= 13)
2420 pa_tagstruct_put_boolean(t, !!(s->flags & PA_STREAM_ADJUST_LATENCY));
2422 if (s->context->version >= 14)
2423 pa_tagstruct_put_boolean(t, !!(s->flags & PA_STREAM_EARLY_REQUESTS));
2425 pa_pstream_send_tagstruct(s->context->pstream, t);
2426 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);
2428 /* This might cause changes in the read/write indexex, hence let's
2429 * request a timing update */
2430 request_auto_timing_update(s, TRUE);
2435 uint32_t pa_stream_get_device_index(pa_stream *s) {
2437 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2439 PA_CHECK_VALIDITY_RETURN_ANY(s->context, !pa_detect_fork(), PA_ERR_FORKED, PA_INVALID_INDEX);
2440 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2441 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2442 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
2443 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->device_index != PA_INVALID_INDEX, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2445 return s->device_index;
2448 const char *pa_stream_get_device_name(pa_stream *s) {
2450 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2452 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2453 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2454 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2455 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2456 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->device_name, PA_ERR_BADSTATE);
2458 return s->device_name;
2461 int pa_stream_is_suspended(pa_stream *s) {
2463 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2465 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2466 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2467 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2468 PA_CHECK_VALIDITY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2470 return s->suspended;
2473 int pa_stream_is_corked(pa_stream *s) {
2475 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2477 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2478 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2479 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2484 static void stream_update_sample_rate_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2485 pa_operation *o = userdata;
2490 pa_assert(PA_REFCNT_VALUE(o) >= 1);
2495 if (command != PA_COMMAND_REPLY) {
2496 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
2502 if (!pa_tagstruct_eof(t)) {
2503 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2508 o->stream->sample_spec.rate = PA_PTR_TO_UINT(o->private);
2509 pa_assert(pa_sample_spec_valid(&o->stream->sample_spec));
2512 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
2513 cb(o->stream, success, o->userdata);
2517 pa_operation_done(o);
2518 pa_operation_unref(o);
2522 pa_operation *pa_stream_update_sample_rate(pa_stream *s, uint32_t rate, pa_stream_success_cb_t cb, void *userdata) {
2528 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2530 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2531 PA_CHECK_VALIDITY_RETURN_NULL(s->context, rate > 0 && rate <= PA_RATE_MAX, PA_ERR_INVALID);
2532 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2533 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2534 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->flags & PA_STREAM_VARIABLE_RATE, PA_ERR_BADSTATE);
2535 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2537 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2538 o->private = PA_UINT_TO_PTR(rate);
2540 t = pa_tagstruct_command(
2542 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE : PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE),
2544 pa_tagstruct_putu32(t, s->channel);
2545 pa_tagstruct_putu32(t, rate);
2547 pa_pstream_send_tagstruct(s->context->pstream, t);
2548 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);
2553 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) {
2559 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2561 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2562 PA_CHECK_VALIDITY_RETURN_NULL(s->context, mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE, PA_ERR_INVALID);
2563 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2564 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2565 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2567 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2569 t = pa_tagstruct_command(
2571 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST : PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST),
2573 pa_tagstruct_putu32(t, s->channel);
2574 pa_tagstruct_putu32(t, (uint32_t) mode);
2575 pa_tagstruct_put_proplist(t, p);
2577 pa_pstream_send_tagstruct(s->context->pstream, t);
2578 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);
2580 /* Please note that we don't update s->proplist here, because we
2581 * don't export that field */
2586 pa_operation *pa_stream_proplist_remove(pa_stream *s, const char *const keys[], pa_stream_success_cb_t cb, void *userdata) {
2590 const char * const*k;
2593 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2595 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2596 PA_CHECK_VALIDITY_RETURN_NULL(s->context, keys && keys[0], PA_ERR_INVALID);
2597 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2598 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2599 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2601 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2603 t = pa_tagstruct_command(
2605 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST : PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST),
2607 pa_tagstruct_putu32(t, s->channel);
2609 for (k = keys; *k; k++)
2610 pa_tagstruct_puts(t, *k);
2612 pa_tagstruct_puts(t, NULL);
2614 pa_pstream_send_tagstruct(s->context->pstream, t);
2615 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);
2617 /* Please note that we don't update s->proplist here, because we
2618 * don't export that field */
2623 int pa_stream_set_monitor_stream(pa_stream *s, uint32_t sink_input_idx) {
2625 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2627 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2628 PA_CHECK_VALIDITY(s->context, sink_input_idx != PA_INVALID_INDEX, PA_ERR_INVALID);
2629 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
2630 PA_CHECK_VALIDITY(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2632 s->direct_on_input = sink_input_idx;
2637 uint32_t pa_stream_get_monitor_stream(pa_stream *s) {
2639 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2641 PA_CHECK_VALIDITY_RETURN_ANY(s->context, !pa_detect_fork(), PA_ERR_FORKED, PA_INVALID_INDEX);
2642 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direct_on_input != PA_INVALID_INDEX, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2643 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
2645 return s->direct_on_input;