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 if (!s->timing_info_valid &&
383 s->context->version >= 13) {
385 /* If the server supports STARTED and UNDERFLOW events we take
386 * them as indications when audio really starts/stops playing,
387 * if we don't have any timing info yet -- instead of trying
388 * to be smart and guessing the server time. Otherwise the
389 * unknown transport delay we don't know might add too much
390 * noise to our time calculations. */
395 x = pa_rtclock_now();
397 if (s->timing_info_valid) {
399 x -= s->timing_info.transport_usec;
401 x += s->timing_info.transport_usec;
404 if (s->suspended || s->corked || force_stop)
405 pa_smoother_pause(s->smoother, x);
406 else if (force_start || s->buffer_attr.prebuf == 0)
407 pa_smoother_resume(s->smoother, x, TRUE);
409 /* Please note that we have no idea if playback actually started
410 * if prebuf is non-zero! */
413 void pa_command_stream_moved(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
414 pa_context *c = userdata;
421 uint32_t maxlength = 0, fragsize = 0, minreq = 0, tlength = 0, prebuf = 0;
424 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_MOVED || command == PA_COMMAND_RECORD_STREAM_MOVED);
427 pa_assert(PA_REFCNT_VALUE(c) >= 1);
431 if (c->version < 12) {
432 pa_context_fail(c, PA_ERR_PROTOCOL);
436 if (pa_tagstruct_getu32(t, &channel) < 0 ||
437 pa_tagstruct_getu32(t, &di) < 0 ||
438 pa_tagstruct_gets(t, &dn) < 0 ||
439 pa_tagstruct_get_boolean(t, &suspended) < 0) {
440 pa_context_fail(c, PA_ERR_PROTOCOL);
444 if (c->version >= 13) {
446 if (command == PA_COMMAND_RECORD_STREAM_MOVED) {
447 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
448 pa_tagstruct_getu32(t, &fragsize) < 0 ||
449 pa_tagstruct_get_usec(t, &usec) < 0) {
450 pa_context_fail(c, PA_ERR_PROTOCOL);
454 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
455 pa_tagstruct_getu32(t, &tlength) < 0 ||
456 pa_tagstruct_getu32(t, &prebuf) < 0 ||
457 pa_tagstruct_getu32(t, &minreq) < 0 ||
458 pa_tagstruct_get_usec(t, &usec) < 0) {
459 pa_context_fail(c, PA_ERR_PROTOCOL);
465 if (!pa_tagstruct_eof(t)) {
466 pa_context_fail(c, PA_ERR_PROTOCOL);
470 if (!dn || di == PA_INVALID_INDEX) {
471 pa_context_fail(c, PA_ERR_PROTOCOL);
475 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_MOVED ? c->playback_streams : c->record_streams, channel)))
478 if (s->state != PA_STREAM_READY)
481 if (c->version >= 13) {
482 if (s->direction == PA_STREAM_RECORD)
483 s->timing_info.configured_source_usec = usec;
485 s->timing_info.configured_sink_usec = usec;
487 s->buffer_attr.maxlength = maxlength;
488 s->buffer_attr.fragsize = fragsize;
489 s->buffer_attr.tlength = tlength;
490 s->buffer_attr.prebuf = prebuf;
491 s->buffer_attr.minreq = minreq;
494 pa_xfree(s->device_name);
495 s->device_name = pa_xstrdup(dn);
496 s->device_index = di;
498 s->suspended = suspended;
500 check_smoother_status(s, TRUE, FALSE, FALSE);
501 request_auto_timing_update(s, TRUE);
503 if (s->moved_callback)
504 s->moved_callback(s, s->moved_userdata);
510 void pa_command_stream_buffer_attr(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
511 pa_context *c = userdata;
515 uint32_t maxlength = 0, fragsize = 0, minreq = 0, tlength = 0, prebuf = 0;
518 pa_assert(command == PA_COMMAND_PLAYBACK_BUFFER_ATTR_CHANGED || command == PA_COMMAND_RECORD_BUFFER_ATTR_CHANGED);
521 pa_assert(PA_REFCNT_VALUE(c) >= 1);
525 if (c->version < 15) {
526 pa_context_fail(c, PA_ERR_PROTOCOL);
530 if (pa_tagstruct_getu32(t, &channel) < 0) {
531 pa_context_fail(c, PA_ERR_PROTOCOL);
535 if (command == PA_COMMAND_RECORD_STREAM_MOVED) {
536 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
537 pa_tagstruct_getu32(t, &fragsize) < 0 ||
538 pa_tagstruct_get_usec(t, &usec) < 0) {
539 pa_context_fail(c, PA_ERR_PROTOCOL);
543 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
544 pa_tagstruct_getu32(t, &tlength) < 0 ||
545 pa_tagstruct_getu32(t, &prebuf) < 0 ||
546 pa_tagstruct_getu32(t, &minreq) < 0 ||
547 pa_tagstruct_get_usec(t, &usec) < 0) {
548 pa_context_fail(c, PA_ERR_PROTOCOL);
553 if (!pa_tagstruct_eof(t)) {
554 pa_context_fail(c, PA_ERR_PROTOCOL);
558 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_BUFFER_ATTR_CHANGED ? c->playback_streams : c->record_streams, channel)))
561 if (s->state != PA_STREAM_READY)
564 if (s->direction == PA_STREAM_RECORD)
565 s->timing_info.configured_source_usec = usec;
567 s->timing_info.configured_sink_usec = usec;
569 s->buffer_attr.maxlength = maxlength;
570 s->buffer_attr.fragsize = fragsize;
571 s->buffer_attr.tlength = tlength;
572 s->buffer_attr.prebuf = prebuf;
573 s->buffer_attr.minreq = minreq;
575 request_auto_timing_update(s, TRUE);
577 if (s->buffer_attr_callback)
578 s->buffer_attr_callback(s, s->buffer_attr_userdata);
584 void pa_command_stream_suspended(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
585 pa_context *c = userdata;
591 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED || command == PA_COMMAND_RECORD_STREAM_SUSPENDED);
594 pa_assert(PA_REFCNT_VALUE(c) >= 1);
598 if (c->version < 12) {
599 pa_context_fail(c, PA_ERR_PROTOCOL);
603 if (pa_tagstruct_getu32(t, &channel) < 0 ||
604 pa_tagstruct_get_boolean(t, &suspended) < 0 ||
605 !pa_tagstruct_eof(t)) {
606 pa_context_fail(c, PA_ERR_PROTOCOL);
610 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED ? c->playback_streams : c->record_streams, channel)))
613 if (s->state != PA_STREAM_READY)
616 s->suspended = suspended;
618 check_smoother_status(s, TRUE, FALSE, FALSE);
619 request_auto_timing_update(s, TRUE);
621 if (s->suspended_callback)
622 s->suspended_callback(s, s->suspended_userdata);
628 void pa_command_stream_started(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
629 pa_context *c = userdata;
634 pa_assert(command == PA_COMMAND_STARTED);
637 pa_assert(PA_REFCNT_VALUE(c) >= 1);
641 if (c->version < 13) {
642 pa_context_fail(c, PA_ERR_PROTOCOL);
646 if (pa_tagstruct_getu32(t, &channel) < 0 ||
647 !pa_tagstruct_eof(t)) {
648 pa_context_fail(c, PA_ERR_PROTOCOL);
652 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
655 if (s->state != PA_STREAM_READY)
658 check_smoother_status(s, TRUE, TRUE, FALSE);
659 request_auto_timing_update(s, TRUE);
661 if (s->started_callback)
662 s->started_callback(s, s->started_userdata);
668 void pa_command_stream_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
669 pa_context *c = userdata;
672 pa_proplist *pl = NULL;
676 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_EVENT || command == PA_COMMAND_RECORD_STREAM_EVENT);
679 pa_assert(PA_REFCNT_VALUE(c) >= 1);
683 if (c->version < 15) {
684 pa_context_fail(c, PA_ERR_PROTOCOL);
688 pl = pa_proplist_new();
690 if (pa_tagstruct_getu32(t, &channel) < 0 ||
691 pa_tagstruct_gets(t, &event) < 0 ||
692 pa_tagstruct_get_proplist(t, pl) < 0 ||
693 !pa_tagstruct_eof(t) || !event) {
694 pa_context_fail(c, PA_ERR_PROTOCOL);
698 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_EVENT ? c->playback_streams : c->record_streams, channel)))
701 if (s->state != PA_STREAM_READY)
704 if (s->event_callback)
705 s->event_callback(s, event, pl, s->event_userdata);
711 pa_proplist_free(pl);
714 void pa_command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
716 pa_context *c = userdata;
717 uint32_t bytes, channel;
720 pa_assert(command == PA_COMMAND_REQUEST);
723 pa_assert(PA_REFCNT_VALUE(c) >= 1);
727 if (pa_tagstruct_getu32(t, &channel) < 0 ||
728 pa_tagstruct_getu32(t, &bytes) < 0 ||
729 !pa_tagstruct_eof(t)) {
730 pa_context_fail(c, PA_ERR_PROTOCOL);
734 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
737 if (s->state != PA_STREAM_READY)
740 s->requested_bytes += bytes;
742 if (s->requested_bytes > 0 && s->write_callback)
743 s->write_callback(s, (size_t) s->requested_bytes, s->write_userdata);
749 void pa_command_overflow_or_underflow(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
751 pa_context *c = userdata;
755 pa_assert(command == PA_COMMAND_OVERFLOW || command == PA_COMMAND_UNDERFLOW);
758 pa_assert(PA_REFCNT_VALUE(c) >= 1);
762 if (pa_tagstruct_getu32(t, &channel) < 0 ||
763 !pa_tagstruct_eof(t)) {
764 pa_context_fail(c, PA_ERR_PROTOCOL);
768 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
771 if (s->state != PA_STREAM_READY)
774 if (s->buffer_attr.prebuf > 0)
775 check_smoother_status(s, TRUE, FALSE, TRUE);
777 request_auto_timing_update(s, TRUE);
779 if (command == PA_COMMAND_OVERFLOW) {
780 if (s->overflow_callback)
781 s->overflow_callback(s, s->overflow_userdata);
782 } else if (command == PA_COMMAND_UNDERFLOW) {
783 if (s->underflow_callback)
784 s->underflow_callback(s, s->underflow_userdata);
791 static void invalidate_indexes(pa_stream *s, pa_bool_t r, pa_bool_t w) {
793 pa_assert(PA_REFCNT_VALUE(s) >= 1);
795 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
797 if (s->state != PA_STREAM_READY)
801 s->write_index_not_before = s->context->ctag;
803 if (s->timing_info_valid)
804 s->timing_info.write_index_corrupt = TRUE;
806 /* pa_log("write_index invalidated"); */
810 s->read_index_not_before = s->context->ctag;
812 if (s->timing_info_valid)
813 s->timing_info.read_index_corrupt = TRUE;
815 /* pa_log("read_index invalidated"); */
818 request_auto_timing_update(s, TRUE);
821 static void auto_timing_update_callback(pa_mainloop_api *m, pa_time_event *e, const struct timeval *t, void *userdata) {
822 pa_stream *s = userdata;
825 pa_assert(PA_REFCNT_VALUE(s) >= 1);
828 request_auto_timing_update(s, FALSE);
832 static void create_stream_complete(pa_stream *s) {
834 pa_assert(PA_REFCNT_VALUE(s) >= 1);
835 pa_assert(s->state == PA_STREAM_CREATING);
837 pa_stream_set_state(s, PA_STREAM_READY);
839 if (s->requested_bytes > 0 && s->write_callback)
840 s->write_callback(s, (size_t) s->requested_bytes, s->write_userdata);
842 if (s->flags & PA_STREAM_AUTO_TIMING_UPDATE) {
843 s->auto_timing_interval_usec = AUTO_TIMING_INTERVAL_START_USEC;
844 pa_assert(!s->auto_timing_update_event);
845 s->auto_timing_update_event = pa_context_rttime_new(s->context, pa_rtclock_now() + s->auto_timing_interval_usec, &auto_timing_update_callback, s);
847 request_auto_timing_update(s, TRUE);
850 check_smoother_status(s, TRUE, FALSE, FALSE);
853 static void automatic_buffer_attr(pa_stream *s, pa_buffer_attr *attr, const pa_sample_spec *ss) {
858 if (s->context->version >= 13)
861 /* Version older than 0.9.10 didn't do server side buffer_attr
862 * selection, hence we have to fake it on the client side. */
864 /* We choose fairly conservative values here, to not confuse
865 * old clients with extremely large playback buffers */
867 if (attr->maxlength == (uint32_t) -1)
868 attr->maxlength = 4*1024*1024; /* 4MB is the maximum queue length PulseAudio <= 0.9.9 supported. */
870 if (attr->tlength == (uint32_t) -1)
871 attr->tlength = (uint32_t) pa_usec_to_bytes(250*PA_USEC_PER_MSEC, ss); /* 250ms of buffering */
873 if (attr->minreq == (uint32_t) -1)
874 attr->minreq = (attr->tlength)/5; /* Ask for more data when there are only 200ms left in the playback buffer */
876 if (attr->prebuf == (uint32_t) -1)
877 attr->prebuf = attr->tlength; /* Start to play only when the playback is fully filled up once */
879 if (attr->fragsize == (uint32_t) -1)
880 attr->fragsize = attr->tlength; /* Pass data to the app only when the buffer is filled up once */
883 void pa_create_stream_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
884 pa_stream *s = userdata;
885 uint32_t requested_bytes = 0;
889 pa_assert(PA_REFCNT_VALUE(s) >= 1);
890 pa_assert(s->state == PA_STREAM_CREATING);
894 if (command != PA_COMMAND_REPLY) {
895 if (pa_context_handle_error(s->context, command, t, FALSE) < 0)
898 pa_stream_set_state(s, PA_STREAM_FAILED);
902 if (pa_tagstruct_getu32(t, &s->channel) < 0 ||
903 s->channel == PA_INVALID_INDEX ||
904 ((s->direction != PA_STREAM_UPLOAD) && (pa_tagstruct_getu32(t, &s->stream_index) < 0 || s->stream_index == PA_INVALID_INDEX)) ||
905 ((s->direction != PA_STREAM_RECORD) && pa_tagstruct_getu32(t, &requested_bytes) < 0)) {
906 pa_context_fail(s->context, PA_ERR_PROTOCOL);
910 s->requested_bytes = (int64_t) requested_bytes;
912 if (s->context->version >= 9) {
913 if (s->direction == PA_STREAM_PLAYBACK) {
914 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
915 pa_tagstruct_getu32(t, &s->buffer_attr.tlength) < 0 ||
916 pa_tagstruct_getu32(t, &s->buffer_attr.prebuf) < 0 ||
917 pa_tagstruct_getu32(t, &s->buffer_attr.minreq) < 0) {
918 pa_context_fail(s->context, PA_ERR_PROTOCOL);
921 } else if (s->direction == PA_STREAM_RECORD) {
922 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
923 pa_tagstruct_getu32(t, &s->buffer_attr.fragsize) < 0) {
924 pa_context_fail(s->context, PA_ERR_PROTOCOL);
930 if (s->context->version >= 12 && s->direction != PA_STREAM_UPLOAD) {
933 const char *dn = NULL;
936 if (pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
937 pa_tagstruct_get_channel_map(t, &cm) < 0 ||
938 pa_tagstruct_getu32(t, &s->device_index) < 0 ||
939 pa_tagstruct_gets(t, &dn) < 0 ||
940 pa_tagstruct_get_boolean(t, &suspended) < 0) {
941 pa_context_fail(s->context, PA_ERR_PROTOCOL);
945 if (!dn || s->device_index == PA_INVALID_INDEX ||
946 ss.channels != cm.channels ||
947 !pa_channel_map_valid(&cm) ||
948 !pa_sample_spec_valid(&ss) ||
949 (!(s->flags & PA_STREAM_FIX_FORMAT) && ss.format != s->sample_spec.format) ||
950 (!(s->flags & PA_STREAM_FIX_RATE) && ss.rate != s->sample_spec.rate) ||
951 (!(s->flags & PA_STREAM_FIX_CHANNELS) && !pa_channel_map_equal(&cm, &s->channel_map))) {
952 pa_context_fail(s->context, PA_ERR_PROTOCOL);
956 pa_xfree(s->device_name);
957 s->device_name = pa_xstrdup(dn);
958 s->suspended = suspended;
964 if (s->context->version >= 13 && s->direction != PA_STREAM_UPLOAD) {
967 if (pa_tagstruct_get_usec(t, &usec) < 0) {
968 pa_context_fail(s->context, PA_ERR_PROTOCOL);
972 if (s->direction == PA_STREAM_RECORD)
973 s->timing_info.configured_source_usec = usec;
975 s->timing_info.configured_sink_usec = usec;
978 if (!pa_tagstruct_eof(t)) {
979 pa_context_fail(s->context, PA_ERR_PROTOCOL);
983 if (s->direction == PA_STREAM_RECORD) {
984 pa_assert(!s->record_memblockq);
986 s->record_memblockq = pa_memblockq_new(
988 s->buffer_attr.maxlength,
990 pa_frame_size(&s->sample_spec),
997 s->channel_valid = TRUE;
998 pa_dynarray_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, s->channel, s);
1000 create_stream_complete(s);
1006 static int create_stream(
1007 pa_stream_direction_t direction,
1010 const pa_buffer_attr *attr,
1011 pa_stream_flags_t flags,
1012 const pa_cvolume *volume,
1013 pa_stream *sync_stream) {
1017 pa_bool_t volume_set = FALSE;
1020 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1021 pa_assert(direction == PA_STREAM_PLAYBACK || direction == PA_STREAM_RECORD);
1023 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1024 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
1025 PA_CHECK_VALIDITY(s->context, s->direct_on_input == PA_INVALID_INDEX || direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1026 PA_CHECK_VALIDITY(s->context, !(flags & ~(PA_STREAM_START_CORKED|
1027 PA_STREAM_INTERPOLATE_TIMING|
1028 PA_STREAM_NOT_MONOTONIC|
1029 PA_STREAM_AUTO_TIMING_UPDATE|
1030 PA_STREAM_NO_REMAP_CHANNELS|
1031 PA_STREAM_NO_REMIX_CHANNELS|
1032 PA_STREAM_FIX_FORMAT|
1034 PA_STREAM_FIX_CHANNELS|
1035 PA_STREAM_DONT_MOVE|
1036 PA_STREAM_VARIABLE_RATE|
1037 PA_STREAM_PEAK_DETECT|
1038 PA_STREAM_START_MUTED|
1039 PA_STREAM_ADJUST_LATENCY|
1040 PA_STREAM_EARLY_REQUESTS|
1041 PA_STREAM_DONT_INHIBIT_AUTO_SUSPEND|
1042 PA_STREAM_START_UNMUTED|
1043 PA_STREAM_FAIL_ON_SUSPEND|
1044 PA_STREAM_RELATIVE_VOLUME)), PA_ERR_INVALID);
1046 PA_CHECK_VALIDITY(s->context, s->context->version >= 12 || !(flags & PA_STREAM_VARIABLE_RATE), PA_ERR_NOTSUPPORTED);
1047 PA_CHECK_VALIDITY(s->context, s->context->version >= 13 || !(flags & PA_STREAM_PEAK_DETECT), PA_ERR_NOTSUPPORTED);
1048 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1049 /* Althought some of the other flags are not supported on older
1050 * version, we don't check for them here, because it doesn't hurt
1051 * when they are passed but actually not supported. This makes
1052 * client development easier */
1054 PA_CHECK_VALIDITY(s->context, direction == PA_STREAM_PLAYBACK || !(flags & (PA_STREAM_START_MUTED)), PA_ERR_INVALID);
1055 PA_CHECK_VALIDITY(s->context, direction == PA_STREAM_RECORD || !(flags & (PA_STREAM_PEAK_DETECT)), PA_ERR_INVALID);
1056 PA_CHECK_VALIDITY(s->context, !volume || volume->channels == s->sample_spec.channels, PA_ERR_INVALID);
1057 PA_CHECK_VALIDITY(s->context, !sync_stream || (direction == PA_STREAM_PLAYBACK && sync_stream->direction == PA_STREAM_PLAYBACK), PA_ERR_INVALID);
1058 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);
1062 s->direction = direction;
1064 s->corked = !!(flags & PA_STREAM_START_CORKED);
1067 s->syncid = sync_stream->syncid;
1070 s->buffer_attr = *attr;
1071 automatic_buffer_attr(s, &s->buffer_attr, &s->sample_spec);
1073 if (flags & PA_STREAM_INTERPOLATE_TIMING) {
1076 x = pa_rtclock_now();
1078 pa_assert(!s->smoother);
1079 s->smoother = pa_smoother_new(
1080 SMOOTHER_ADJUST_TIME,
1081 SMOOTHER_HISTORY_TIME,
1082 !(flags & PA_STREAM_NOT_MONOTONIC),
1084 SMOOTHER_MIN_HISTORY,
1090 dev = s->direction == PA_STREAM_PLAYBACK ? s->context->conf->default_sink : s->context->conf->default_source;
1092 t = pa_tagstruct_command(
1094 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM),
1097 if (s->context->version < 13)
1098 pa_tagstruct_puts(t, pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME));
1102 PA_TAG_SAMPLE_SPEC, &s->sample_spec,
1103 PA_TAG_CHANNEL_MAP, &s->channel_map,
1104 PA_TAG_U32, PA_INVALID_INDEX,
1106 PA_TAG_U32, s->buffer_attr.maxlength,
1107 PA_TAG_BOOLEAN, s->corked,
1110 if (s->direction == PA_STREAM_PLAYBACK) {
1115 PA_TAG_U32, s->buffer_attr.tlength,
1116 PA_TAG_U32, s->buffer_attr.prebuf,
1117 PA_TAG_U32, s->buffer_attr.minreq,
1118 PA_TAG_U32, s->syncid,
1121 volume_set = !!volume;
1124 volume = pa_cvolume_reset(&cv, s->sample_spec.channels);
1126 pa_tagstruct_put_cvolume(t, volume);
1128 pa_tagstruct_putu32(t, s->buffer_attr.fragsize);
1130 if (s->context->version >= 12) {
1133 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMAP_CHANNELS,
1134 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMIX_CHANNELS,
1135 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_FORMAT,
1136 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_RATE,
1137 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_CHANNELS,
1138 PA_TAG_BOOLEAN, flags & PA_STREAM_DONT_MOVE,
1139 PA_TAG_BOOLEAN, flags & PA_STREAM_VARIABLE_RATE,
1143 if (s->context->version >= 13) {
1145 if (s->direction == PA_STREAM_PLAYBACK)
1146 pa_tagstruct_put_boolean(t, flags & PA_STREAM_START_MUTED);
1148 pa_tagstruct_put_boolean(t, flags & PA_STREAM_PEAK_DETECT);
1152 PA_TAG_BOOLEAN, flags & PA_STREAM_ADJUST_LATENCY,
1153 PA_TAG_PROPLIST, s->proplist,
1156 if (s->direction == PA_STREAM_RECORD)
1157 pa_tagstruct_putu32(t, s->direct_on_input);
1160 if (s->context->version >= 14) {
1162 if (s->direction == PA_STREAM_PLAYBACK)
1163 pa_tagstruct_put_boolean(t, volume_set);
1165 pa_tagstruct_put_boolean(t, flags & PA_STREAM_EARLY_REQUESTS);
1168 if (s->context->version >= 15) {
1170 if (s->direction == PA_STREAM_PLAYBACK)
1171 pa_tagstruct_put_boolean(t, flags & (PA_STREAM_START_MUTED|PA_STREAM_START_UNMUTED));
1173 pa_tagstruct_put_boolean(t, flags & PA_STREAM_DONT_INHIBIT_AUTO_SUSPEND);
1174 pa_tagstruct_put_boolean(t, flags & PA_STREAM_FAIL_ON_SUSPEND);
1177 if (s->context->version >= 17) {
1179 if (s->direction == PA_STREAM_PLAYBACK)
1180 pa_tagstruct_put_boolean(t, flags & PA_STREAM_RELATIVE_VOLUME);
1184 pa_pstream_send_tagstruct(s->context->pstream, t);
1185 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s, NULL);
1187 pa_stream_set_state(s, PA_STREAM_CREATING);
1193 int pa_stream_connect_playback(
1196 const pa_buffer_attr *attr,
1197 pa_stream_flags_t flags,
1198 const pa_cvolume *volume,
1199 pa_stream *sync_stream) {
1202 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1204 return create_stream(PA_STREAM_PLAYBACK, s, dev, attr, flags, volume, sync_stream);
1207 int pa_stream_connect_record(
1210 const pa_buffer_attr *attr,
1211 pa_stream_flags_t flags) {
1214 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1216 return create_stream(PA_STREAM_RECORD, s, dev, attr, flags, NULL, NULL);
1219 int pa_stream_begin_write(
1225 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1227 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1228 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1229 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || s->direction == PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1230 PA_CHECK_VALIDITY(s->context, data, PA_ERR_INVALID);
1231 PA_CHECK_VALIDITY(s->context, nbytes && *nbytes != 0, PA_ERR_INVALID);
1233 if (*nbytes != (size_t) -1) {
1236 m = pa_mempool_block_size_max(s->context->mempool);
1237 fs = pa_frame_size(&s->sample_spec);
1244 if (!s->write_memblock) {
1245 s->write_memblock = pa_memblock_new(s->context->mempool, *nbytes);
1246 s->write_data = pa_memblock_acquire(s->write_memblock);
1249 *data = s->write_data;
1250 *nbytes = pa_memblock_get_length(s->write_memblock);
1255 int pa_stream_cancel_write(
1259 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1261 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1262 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1263 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || s->direction == PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1264 PA_CHECK_VALIDITY(s->context, s->write_memblock, PA_ERR_BADSTATE);
1266 pa_assert(s->write_data);
1268 pa_memblock_release(s->write_memblock);
1269 pa_memblock_unref(s->write_memblock);
1270 s->write_memblock = NULL;
1271 s->write_data = NULL;
1276 int pa_stream_write(
1280 pa_free_cb_t free_cb,
1282 pa_seek_mode_t seek) {
1285 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1288 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1289 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1290 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || s->direction == PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1291 PA_CHECK_VALIDITY(s->context, seek <= PA_SEEK_RELATIVE_END, PA_ERR_INVALID);
1292 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || (seek == PA_SEEK_RELATIVE && offset == 0), PA_ERR_INVALID);
1293 PA_CHECK_VALIDITY(s->context,
1294 !s->write_memblock ||
1295 ((data >= s->write_data) &&
1296 ((const char*) data + length <= (const char*) s->write_data + pa_memblock_get_length(s->write_memblock))),
1298 PA_CHECK_VALIDITY(s->context, !free_cb || !s->write_memblock, PA_ERR_INVALID);
1300 if (s->write_memblock) {
1303 /* pa_stream_write_begin() was called before */
1305 pa_memblock_release(s->write_memblock);
1307 chunk.memblock = s->write_memblock;
1308 chunk.index = (const char *) data - (const char *) s->write_data;
1309 chunk.length = length;
1311 s->write_memblock = NULL;
1312 s->write_data = NULL;
1314 pa_pstream_send_memblock(s->context->pstream, s->channel, offset, seek, &chunk);
1315 pa_memblock_unref(chunk.memblock);
1318 pa_seek_mode_t t_seek = seek;
1319 int64_t t_offset = offset;
1320 size_t t_length = length;
1321 const void *t_data = data;
1323 /* pa_stream_write_begin() was not called before */
1325 while (t_length > 0) {
1330 if (free_cb && !pa_pstream_get_shm(s->context->pstream)) {
1331 chunk.memblock = pa_memblock_new_user(s->context->mempool, (void*) t_data, t_length, free_cb, 1);
1332 chunk.length = t_length;
1336 chunk.length = PA_MIN(t_length, pa_mempool_block_size_max(s->context->mempool));
1337 chunk.memblock = pa_memblock_new(s->context->mempool, chunk.length);
1339 d = pa_memblock_acquire(chunk.memblock);
1340 memcpy(d, t_data, chunk.length);
1341 pa_memblock_release(chunk.memblock);
1344 pa_pstream_send_memblock(s->context->pstream, s->channel, t_offset, t_seek, &chunk);
1347 t_seek = PA_SEEK_RELATIVE;
1349 t_data = (const uint8_t*) t_data + chunk.length;
1350 t_length -= chunk.length;
1352 pa_memblock_unref(chunk.memblock);
1355 if (free_cb && pa_pstream_get_shm(s->context->pstream))
1356 free_cb((void*) data);
1359 /* This is obviously wrong since we ignore the seeking index . But
1360 * that's OK, the server side applies the same error */
1361 s->requested_bytes -= (seek == PA_SEEK_RELATIVE ? offset : 0) + (int64_t) length;
1363 if (s->direction == PA_STREAM_PLAYBACK) {
1365 /* Update latency request correction */
1366 if (s->write_index_corrections[s->current_write_index_correction].valid) {
1368 if (seek == PA_SEEK_ABSOLUTE) {
1369 s->write_index_corrections[s->current_write_index_correction].corrupt = FALSE;
1370 s->write_index_corrections[s->current_write_index_correction].absolute = TRUE;
1371 s->write_index_corrections[s->current_write_index_correction].value = offset + (int64_t) length;
1372 } else if (seek == PA_SEEK_RELATIVE) {
1373 if (!s->write_index_corrections[s->current_write_index_correction].corrupt)
1374 s->write_index_corrections[s->current_write_index_correction].value += offset + (int64_t) length;
1376 s->write_index_corrections[s->current_write_index_correction].corrupt = TRUE;
1379 /* Update the write index in the already available latency data */
1380 if (s->timing_info_valid) {
1382 if (seek == PA_SEEK_ABSOLUTE) {
1383 s->timing_info.write_index_corrupt = FALSE;
1384 s->timing_info.write_index = offset + (int64_t) length;
1385 } else if (seek == PA_SEEK_RELATIVE) {
1386 if (!s->timing_info.write_index_corrupt)
1387 s->timing_info.write_index += offset + (int64_t) length;
1389 s->timing_info.write_index_corrupt = TRUE;
1392 if (!s->timing_info_valid || s->timing_info.write_index_corrupt)
1393 request_auto_timing_update(s, TRUE);
1399 int pa_stream_peek(pa_stream *s, const void **data, size_t *length) {
1401 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1405 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1406 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1407 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1409 if (!s->peek_memchunk.memblock) {
1411 if (pa_memblockq_peek(s->record_memblockq, &s->peek_memchunk) < 0) {
1417 s->peek_data = pa_memblock_acquire(s->peek_memchunk.memblock);
1420 pa_assert(s->peek_data);
1421 *data = (uint8_t*) s->peek_data + s->peek_memchunk.index;
1422 *length = s->peek_memchunk.length;
1426 int pa_stream_drop(pa_stream *s) {
1428 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1430 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1431 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1432 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1433 PA_CHECK_VALIDITY(s->context, s->peek_memchunk.memblock, PA_ERR_BADSTATE);
1435 pa_memblockq_drop(s->record_memblockq, s->peek_memchunk.length);
1437 /* Fix the simulated local read index */
1438 if (s->timing_info_valid && !s->timing_info.read_index_corrupt)
1439 s->timing_info.read_index += (int64_t) s->peek_memchunk.length;
1441 pa_assert(s->peek_data);
1442 pa_memblock_release(s->peek_memchunk.memblock);
1443 pa_memblock_unref(s->peek_memchunk.memblock);
1444 pa_memchunk_reset(&s->peek_memchunk);
1449 size_t pa_stream_writable_size(pa_stream *s) {
1451 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1453 PA_CHECK_VALIDITY_RETURN_ANY(s->context, !pa_detect_fork(), PA_ERR_FORKED, (size_t) -1);
1454 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
1455 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
1457 return s->requested_bytes > 0 ? (size_t) s->requested_bytes : 0;
1460 size_t pa_stream_readable_size(pa_stream *s) {
1462 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1464 PA_CHECK_VALIDITY_RETURN_ANY(s->context, !pa_detect_fork(), PA_ERR_FORKED, (size_t) -1);
1465 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
1466 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
1468 return pa_memblockq_get_length(s->record_memblockq);
1471 pa_operation * pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1477 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1479 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1480 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1481 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1483 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1485 t = pa_tagstruct_command(s->context, PA_COMMAND_DRAIN_PLAYBACK_STREAM, &tag);
1486 pa_tagstruct_putu32(t, s->channel);
1487 pa_pstream_send_tagstruct(s->context->pstream, t);
1488 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);
1493 static pa_usec_t calc_time(pa_stream *s, pa_bool_t ignore_transport) {
1497 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1498 pa_assert(s->state == PA_STREAM_READY);
1499 pa_assert(s->direction != PA_STREAM_UPLOAD);
1500 pa_assert(s->timing_info_valid);
1501 pa_assert(s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt);
1502 pa_assert(s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt);
1504 if (s->direction == PA_STREAM_PLAYBACK) {
1505 /* The last byte that was written into the output device
1506 * had this time value associated */
1507 usec = pa_bytes_to_usec(s->timing_info.read_index < 0 ? 0 : (uint64_t) s->timing_info.read_index, &s->sample_spec);
1509 if (!s->corked && !s->suspended) {
1511 if (!ignore_transport)
1512 /* Because the latency info took a little time to come
1513 * to us, we assume that the real output time is actually
1515 usec += s->timing_info.transport_usec;
1517 /* However, the output device usually maintains a buffer
1518 too, hence the real sample currently played is a little
1520 if (s->timing_info.sink_usec >= usec)
1523 usec -= s->timing_info.sink_usec;
1527 pa_assert(s->direction == PA_STREAM_RECORD);
1529 /* The last byte written into the server side queue had
1530 * this time value associated */
1531 usec = pa_bytes_to_usec(s->timing_info.write_index < 0 ? 0 : (uint64_t) s->timing_info.write_index, &s->sample_spec);
1533 if (!s->corked && !s->suspended) {
1535 if (!ignore_transport)
1536 /* Add transport latency */
1537 usec += s->timing_info.transport_usec;
1539 /* Add latency of data in device buffer */
1540 usec += s->timing_info.source_usec;
1542 /* If this is a monitor source, we need to correct the
1543 * time by the playback device buffer */
1544 if (s->timing_info.sink_usec >= usec)
1547 usec -= s->timing_info.sink_usec;
1554 static void stream_get_timing_info_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1555 pa_operation *o = userdata;
1556 struct timeval local, remote, now;
1558 pa_bool_t playing = FALSE;
1559 uint64_t underrun_for = 0, playing_for = 0;
1563 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1565 if (!o->context || !o->stream)
1568 i = &o->stream->timing_info;
1570 o->stream->timing_info_valid = FALSE;
1571 i->write_index_corrupt = TRUE;
1572 i->read_index_corrupt = TRUE;
1574 if (command != PA_COMMAND_REPLY) {
1575 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
1580 if (pa_tagstruct_get_usec(t, &i->sink_usec) < 0 ||
1581 pa_tagstruct_get_usec(t, &i->source_usec) < 0 ||
1582 pa_tagstruct_get_boolean(t, &playing) < 0 ||
1583 pa_tagstruct_get_timeval(t, &local) < 0 ||
1584 pa_tagstruct_get_timeval(t, &remote) < 0 ||
1585 pa_tagstruct_gets64(t, &i->write_index) < 0 ||
1586 pa_tagstruct_gets64(t, &i->read_index) < 0) {
1588 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1592 if (o->context->version >= 13 &&
1593 o->stream->direction == PA_STREAM_PLAYBACK)
1594 if (pa_tagstruct_getu64(t, &underrun_for) < 0 ||
1595 pa_tagstruct_getu64(t, &playing_for) < 0) {
1597 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1602 if (!pa_tagstruct_eof(t)) {
1603 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1606 o->stream->timing_info_valid = TRUE;
1607 i->write_index_corrupt = FALSE;
1608 i->read_index_corrupt = FALSE;
1610 i->playing = (int) playing;
1611 i->since_underrun = (int64_t) (playing ? playing_for : underrun_for);
1613 pa_gettimeofday(&now);
1615 /* Calculcate timestamps */
1616 if (pa_timeval_cmp(&local, &remote) <= 0 && pa_timeval_cmp(&remote, &now) <= 0) {
1617 /* local and remote seem to have synchronized clocks */
1619 if (o->stream->direction == PA_STREAM_PLAYBACK)
1620 i->transport_usec = pa_timeval_diff(&remote, &local);
1622 i->transport_usec = pa_timeval_diff(&now, &remote);
1624 i->synchronized_clocks = TRUE;
1625 i->timestamp = remote;
1627 /* clocks are not synchronized, let's estimate latency then */
1628 i->transport_usec = pa_timeval_diff(&now, &local)/2;
1629 i->synchronized_clocks = FALSE;
1630 i->timestamp = local;
1631 pa_timeval_add(&i->timestamp, i->transport_usec);
1634 /* Invalidate read and write indexes if necessary */
1635 if (tag < o->stream->read_index_not_before)
1636 i->read_index_corrupt = TRUE;
1638 if (tag < o->stream->write_index_not_before)
1639 i->write_index_corrupt = TRUE;
1641 if (o->stream->direction == PA_STREAM_PLAYBACK) {
1642 /* Write index correction */
1645 uint32_t ctag = tag;
1647 /* Go through the saved correction values and add up the
1648 * total correction.*/
1649 for (n = 0, j = o->stream->current_write_index_correction+1;
1650 n < PA_MAX_WRITE_INDEX_CORRECTIONS;
1651 n++, j = (j + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS) {
1653 /* Step over invalid data or out-of-date data */
1654 if (!o->stream->write_index_corrections[j].valid ||
1655 o->stream->write_index_corrections[j].tag < ctag)
1658 /* Make sure that everything is in order */
1659 ctag = o->stream->write_index_corrections[j].tag+1;
1661 /* Now fix the write index */
1662 if (o->stream->write_index_corrections[j].corrupt) {
1663 /* A corrupting seek was made */
1664 i->write_index_corrupt = TRUE;
1665 } else if (o->stream->write_index_corrections[j].absolute) {
1666 /* An absolute seek was made */
1667 i->write_index = o->stream->write_index_corrections[j].value;
1668 i->write_index_corrupt = FALSE;
1669 } else if (!i->write_index_corrupt) {
1670 /* A relative seek was made */
1671 i->write_index += o->stream->write_index_corrections[j].value;
1675 /* Clear old correction entries */
1676 for (n = 0; n < PA_MAX_WRITE_INDEX_CORRECTIONS; n++) {
1677 if (!o->stream->write_index_corrections[n].valid)
1680 if (o->stream->write_index_corrections[n].tag <= tag)
1681 o->stream->write_index_corrections[n].valid = FALSE;
1685 if (o->stream->direction == PA_STREAM_RECORD) {
1686 /* Read index correction */
1688 if (!i->read_index_corrupt)
1689 i->read_index -= (int64_t) pa_memblockq_get_length(o->stream->record_memblockq);
1692 /* Update smoother */
1693 if (o->stream->smoother) {
1696 u = x = pa_rtclock_now() - i->transport_usec;
1698 if (o->stream->direction == PA_STREAM_PLAYBACK && o->context->version >= 13) {
1701 /* If we weren't playing then it will take some time
1702 * until the audio will actually come out through the
1703 * speakers. Since we follow that timing here, we need
1704 * to try to fix this up */
1706 su = pa_bytes_to_usec((uint64_t) i->since_underrun, &o->stream->sample_spec);
1708 if (su < i->sink_usec)
1709 x += i->sink_usec - su;
1713 pa_smoother_pause(o->stream->smoother, x);
1715 /* Update the smoother */
1716 if ((o->stream->direction == PA_STREAM_PLAYBACK && !i->read_index_corrupt) ||
1717 (o->stream->direction == PA_STREAM_RECORD && !i->write_index_corrupt))
1718 pa_smoother_put(o->stream->smoother, u, calc_time(o->stream, TRUE));
1721 pa_smoother_resume(o->stream->smoother, x, TRUE);
1725 o->stream->auto_timing_update_requested = FALSE;
1727 if (o->stream->latency_update_callback)
1728 o->stream->latency_update_callback(o->stream, o->stream->latency_update_userdata);
1730 if (o->callback && o->stream && o->stream->state == PA_STREAM_READY) {
1731 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1732 cb(o->stream, o->stream->timing_info_valid, o->userdata);
1737 pa_operation_done(o);
1738 pa_operation_unref(o);
1741 pa_operation* pa_stream_update_timing_info(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1749 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1751 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1752 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1753 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1755 if (s->direction == PA_STREAM_PLAYBACK) {
1756 /* Find a place to store the write_index correction data for this entry */
1757 cidx = (s->current_write_index_correction + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS;
1759 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
1760 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !s->write_index_corrections[cidx].valid, PA_ERR_INTERNAL);
1762 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1764 t = pa_tagstruct_command(
1766 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_GET_PLAYBACK_LATENCY : PA_COMMAND_GET_RECORD_LATENCY),
1768 pa_tagstruct_putu32(t, s->channel);
1769 pa_tagstruct_put_timeval(t, pa_gettimeofday(&now));
1771 pa_pstream_send_tagstruct(s->context->pstream, t);
1772 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);
1774 if (s->direction == PA_STREAM_PLAYBACK) {
1775 /* Fill in initial correction data */
1777 s->current_write_index_correction = cidx;
1779 s->write_index_corrections[cidx].valid = TRUE;
1780 s->write_index_corrections[cidx].absolute = FALSE;
1781 s->write_index_corrections[cidx].corrupt = FALSE;
1782 s->write_index_corrections[cidx].tag = tag;
1783 s->write_index_corrections[cidx].value = 0;
1789 void pa_stream_disconnect_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1790 pa_stream *s = userdata;
1794 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1798 if (command != PA_COMMAND_REPLY) {
1799 if (pa_context_handle_error(s->context, command, t, FALSE) < 0)
1802 pa_stream_set_state(s, PA_STREAM_FAILED);
1804 } else if (!pa_tagstruct_eof(t)) {
1805 pa_context_fail(s->context, PA_ERR_PROTOCOL);
1809 pa_stream_set_state(s, PA_STREAM_TERMINATED);
1815 int pa_stream_disconnect(pa_stream *s) {
1820 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1822 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
1823 PA_CHECK_VALIDITY(s->context, s->channel_valid, PA_ERR_BADSTATE);
1824 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1828 t = pa_tagstruct_command(
1830 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_DELETE_PLAYBACK_STREAM :
1831 (s->direction == PA_STREAM_RECORD ? PA_COMMAND_DELETE_RECORD_STREAM : PA_COMMAND_DELETE_UPLOAD_STREAM)),
1833 pa_tagstruct_putu32(t, s->channel);
1834 pa_pstream_send_tagstruct(s->context->pstream, t);
1835 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s, NULL);
1841 void pa_stream_set_read_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1843 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1845 if (pa_detect_fork())
1848 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1851 s->read_callback = cb;
1852 s->read_userdata = userdata;
1855 void pa_stream_set_write_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1857 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1859 if (pa_detect_fork())
1862 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1865 s->write_callback = cb;
1866 s->write_userdata = userdata;
1869 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1871 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1873 if (pa_detect_fork())
1876 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1879 s->state_callback = cb;
1880 s->state_userdata = userdata;
1883 void pa_stream_set_overflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1885 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1887 if (pa_detect_fork())
1890 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1893 s->overflow_callback = cb;
1894 s->overflow_userdata = userdata;
1897 void pa_stream_set_underflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1899 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1901 if (pa_detect_fork())
1904 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1907 s->underflow_callback = cb;
1908 s->underflow_userdata = userdata;
1911 void pa_stream_set_latency_update_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1913 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1915 if (pa_detect_fork())
1918 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1921 s->latency_update_callback = cb;
1922 s->latency_update_userdata = userdata;
1925 void pa_stream_set_moved_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1927 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1929 if (pa_detect_fork())
1932 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1935 s->moved_callback = cb;
1936 s->moved_userdata = userdata;
1939 void pa_stream_set_suspended_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1941 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1943 if (pa_detect_fork())
1946 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1949 s->suspended_callback = cb;
1950 s->suspended_userdata = userdata;
1953 void pa_stream_set_started_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1955 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1957 if (pa_detect_fork())
1960 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1963 s->started_callback = cb;
1964 s->started_userdata = userdata;
1967 void pa_stream_set_event_callback(pa_stream *s, pa_stream_event_cb_t cb, void *userdata) {
1969 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1971 if (pa_detect_fork())
1974 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1977 s->event_callback = cb;
1978 s->event_userdata = userdata;
1981 void pa_stream_set_buffer_attr_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1983 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1985 if (pa_detect_fork())
1988 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1991 s->buffer_attr_callback = cb;
1992 s->buffer_attr_userdata = userdata;
1995 void pa_stream_simple_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1996 pa_operation *o = userdata;
2001 pa_assert(PA_REFCNT_VALUE(o) >= 1);
2006 if (command != PA_COMMAND_REPLY) {
2007 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
2011 } else if (!pa_tagstruct_eof(t)) {
2012 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2017 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
2018 cb(o->stream, success, o->userdata);
2022 pa_operation_done(o);
2023 pa_operation_unref(o);
2026 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata) {
2032 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2034 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2035 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2036 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2040 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2042 t = pa_tagstruct_command(
2044 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CORK_PLAYBACK_STREAM : PA_COMMAND_CORK_RECORD_STREAM),
2046 pa_tagstruct_putu32(t, s->channel);
2047 pa_tagstruct_put_boolean(t, !!b);
2048 pa_pstream_send_tagstruct(s->context->pstream, t);
2049 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);
2051 check_smoother_status(s, FALSE, FALSE, FALSE);
2053 /* This might cause the indexes to hang/start again, hence
2054 * let's request a timing update */
2055 request_auto_timing_update(s, TRUE);
2060 static pa_operation* stream_send_simple_command(pa_stream *s, uint32_t command, pa_stream_success_cb_t cb, void *userdata) {
2066 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2068 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2069 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2071 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2073 t = pa_tagstruct_command(s->context, command, &tag);
2074 pa_tagstruct_putu32(t, s->channel);
2075 pa_pstream_send_tagstruct(s->context->pstream, t);
2076 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);
2081 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
2085 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2087 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2088 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2089 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2091 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)))
2094 if (s->direction == PA_STREAM_PLAYBACK) {
2096 if (s->write_index_corrections[s->current_write_index_correction].valid)
2097 s->write_index_corrections[s->current_write_index_correction].corrupt = TRUE;
2099 if (s->buffer_attr.prebuf > 0)
2100 check_smoother_status(s, FALSE, FALSE, TRUE);
2102 /* This will change the write index, but leave the
2103 * read index untouched. */
2104 invalidate_indexes(s, FALSE, TRUE);
2107 /* For record streams this has no influence on the write
2108 * index, but the read index might jump. */
2109 invalidate_indexes(s, TRUE, FALSE);
2114 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
2118 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2120 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2121 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2122 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
2123 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
2125 if (!(o = stream_send_simple_command(s, PA_COMMAND_PREBUF_PLAYBACK_STREAM, cb, userdata)))
2128 /* This might cause the read index to hang again, hence
2129 * let's request a timing update */
2130 request_auto_timing_update(s, TRUE);
2135 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
2139 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2141 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2142 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2143 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
2144 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
2146 if (!(o = stream_send_simple_command(s, PA_COMMAND_TRIGGER_PLAYBACK_STREAM, cb, userdata)))
2149 /* This might cause the read index to start moving again, hence
2150 * let's request a timing update */
2151 request_auto_timing_update(s, TRUE);
2156 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata) {
2160 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2163 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2164 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2165 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2167 if (s->context->version >= 13) {
2168 pa_proplist *p = pa_proplist_new();
2170 pa_proplist_sets(p, PA_PROP_MEDIA_NAME, name);
2171 o = pa_stream_proplist_update(s, PA_UPDATE_REPLACE, p, cb, userdata);
2172 pa_proplist_free(p);
2177 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2178 t = pa_tagstruct_command(
2180 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_NAME : PA_COMMAND_SET_PLAYBACK_STREAM_NAME),
2182 pa_tagstruct_putu32(t, s->channel);
2183 pa_tagstruct_puts(t, name);
2184 pa_pstream_send_tagstruct(s->context->pstream, t);
2185 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);
2191 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec) {
2195 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2197 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2198 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2199 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2200 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
2201 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
2202 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
2205 usec = pa_smoother_get(s->smoother, pa_rtclock_now());
2207 usec = calc_time(s, FALSE);
2209 /* Make sure the time runs monotonically */
2210 if (!(s->flags & PA_STREAM_NOT_MONOTONIC)) {
2211 if (usec < s->previous_time)
2212 usec = s->previous_time;
2214 s->previous_time = usec;
2223 static pa_usec_t time_counter_diff(pa_stream *s, pa_usec_t a, pa_usec_t b, int *negative) {
2225 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2233 if (negative && s->direction == PA_STREAM_RECORD) {
2241 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative) {
2247 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2250 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2251 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2252 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2253 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
2254 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
2255 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
2257 if ((r = pa_stream_get_time(s, &t)) < 0)
2260 if (s->direction == PA_STREAM_PLAYBACK)
2261 cindex = s->timing_info.write_index;
2263 cindex = s->timing_info.read_index;
2268 c = pa_bytes_to_usec((uint64_t) cindex, &s->sample_spec);
2270 if (s->direction == PA_STREAM_PLAYBACK)
2271 *r_usec = time_counter_diff(s, c, t, negative);
2273 *r_usec = time_counter_diff(s, t, c, negative);
2278 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s) {
2280 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2282 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2283 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2284 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2285 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->timing_info_valid, PA_ERR_NODATA);
2287 return &s->timing_info;
2290 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s) {
2292 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2294 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2296 return &s->sample_spec;
2299 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s) {
2301 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2303 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2305 return &s->channel_map;
2308 const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s) {
2310 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2312 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2313 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2314 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 9, PA_ERR_NOTSUPPORTED);
2316 return &s->buffer_attr;
2319 static void stream_set_buffer_attr_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2320 pa_operation *o = userdata;
2325 pa_assert(PA_REFCNT_VALUE(o) >= 1);
2330 if (command != PA_COMMAND_REPLY) {
2331 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
2336 if (o->stream->direction == PA_STREAM_PLAYBACK) {
2337 if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
2338 pa_tagstruct_getu32(t, &o->stream->buffer_attr.tlength) < 0 ||
2339 pa_tagstruct_getu32(t, &o->stream->buffer_attr.prebuf) < 0 ||
2340 pa_tagstruct_getu32(t, &o->stream->buffer_attr.minreq) < 0) {
2341 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2344 } else if (o->stream->direction == PA_STREAM_RECORD) {
2345 if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
2346 pa_tagstruct_getu32(t, &o->stream->buffer_attr.fragsize) < 0) {
2347 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2352 if (o->stream->context->version >= 13) {
2355 if (pa_tagstruct_get_usec(t, &usec) < 0) {
2356 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2360 if (o->stream->direction == PA_STREAM_RECORD)
2361 o->stream->timing_info.configured_source_usec = usec;
2363 o->stream->timing_info.configured_sink_usec = usec;
2366 if (!pa_tagstruct_eof(t)) {
2367 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2373 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
2374 cb(o->stream, success, o->userdata);
2378 pa_operation_done(o);
2379 pa_operation_unref(o);
2383 pa_operation* pa_stream_set_buffer_attr(pa_stream *s, const pa_buffer_attr *attr, pa_stream_success_cb_t cb, void *userdata) {
2389 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2392 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2393 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2394 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2395 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2397 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2399 t = pa_tagstruct_command(
2401 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR : PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR),
2403 pa_tagstruct_putu32(t, s->channel);
2405 pa_tagstruct_putu32(t, attr->maxlength);
2407 if (s->direction == PA_STREAM_PLAYBACK)
2410 PA_TAG_U32, attr->tlength,
2411 PA_TAG_U32, attr->prebuf,
2412 PA_TAG_U32, attr->minreq,
2415 pa_tagstruct_putu32(t, attr->fragsize);
2417 if (s->context->version >= 13)
2418 pa_tagstruct_put_boolean(t, !!(s->flags & PA_STREAM_ADJUST_LATENCY));
2420 if (s->context->version >= 14)
2421 pa_tagstruct_put_boolean(t, !!(s->flags & PA_STREAM_EARLY_REQUESTS));
2423 pa_pstream_send_tagstruct(s->context->pstream, t);
2424 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);
2426 /* This might cause changes in the read/write indexex, hence let's
2427 * request a timing update */
2428 request_auto_timing_update(s, TRUE);
2433 uint32_t pa_stream_get_device_index(pa_stream *s) {
2435 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2437 PA_CHECK_VALIDITY_RETURN_ANY(s->context, !pa_detect_fork(), PA_ERR_FORKED, PA_INVALID_INDEX);
2438 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2439 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2440 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
2441 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->device_index != PA_INVALID_INDEX, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2443 return s->device_index;
2446 const char *pa_stream_get_device_name(pa_stream *s) {
2448 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2450 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2451 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2452 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2453 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2454 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->device_name, PA_ERR_BADSTATE);
2456 return s->device_name;
2459 int pa_stream_is_suspended(pa_stream *s) {
2461 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2463 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2464 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2465 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2466 PA_CHECK_VALIDITY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2468 return s->suspended;
2471 int pa_stream_is_corked(pa_stream *s) {
2473 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2475 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2476 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2477 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2482 static void stream_update_sample_rate_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2483 pa_operation *o = userdata;
2488 pa_assert(PA_REFCNT_VALUE(o) >= 1);
2493 if (command != PA_COMMAND_REPLY) {
2494 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
2500 if (!pa_tagstruct_eof(t)) {
2501 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2506 o->stream->sample_spec.rate = PA_PTR_TO_UINT(o->private);
2507 pa_assert(pa_sample_spec_valid(&o->stream->sample_spec));
2510 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
2511 cb(o->stream, success, o->userdata);
2515 pa_operation_done(o);
2516 pa_operation_unref(o);
2520 pa_operation *pa_stream_update_sample_rate(pa_stream *s, uint32_t rate, pa_stream_success_cb_t cb, void *userdata) {
2526 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2528 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2529 PA_CHECK_VALIDITY_RETURN_NULL(s->context, rate > 0 && rate <= PA_RATE_MAX, PA_ERR_INVALID);
2530 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2531 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2532 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->flags & PA_STREAM_VARIABLE_RATE, PA_ERR_BADSTATE);
2533 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2535 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2536 o->private = PA_UINT_TO_PTR(rate);
2538 t = pa_tagstruct_command(
2540 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE : PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE),
2542 pa_tagstruct_putu32(t, s->channel);
2543 pa_tagstruct_putu32(t, rate);
2545 pa_pstream_send_tagstruct(s->context->pstream, t);
2546 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);
2551 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) {
2557 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2559 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2560 PA_CHECK_VALIDITY_RETURN_NULL(s->context, mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE, PA_ERR_INVALID);
2561 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2562 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2563 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2565 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2567 t = pa_tagstruct_command(
2569 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST : PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST),
2571 pa_tagstruct_putu32(t, s->channel);
2572 pa_tagstruct_putu32(t, (uint32_t) mode);
2573 pa_tagstruct_put_proplist(t, p);
2575 pa_pstream_send_tagstruct(s->context->pstream, t);
2576 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);
2578 /* Please note that we don't update s->proplist here, because we
2579 * don't export that field */
2584 pa_operation *pa_stream_proplist_remove(pa_stream *s, const char *const keys[], pa_stream_success_cb_t cb, void *userdata) {
2588 const char * const*k;
2591 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2593 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2594 PA_CHECK_VALIDITY_RETURN_NULL(s->context, keys && keys[0], PA_ERR_INVALID);
2595 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2596 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2597 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2599 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2601 t = pa_tagstruct_command(
2603 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST : PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST),
2605 pa_tagstruct_putu32(t, s->channel);
2607 for (k = keys; *k; k++)
2608 pa_tagstruct_puts(t, *k);
2610 pa_tagstruct_puts(t, NULL);
2612 pa_pstream_send_tagstruct(s->context->pstream, t);
2613 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);
2615 /* Please note that we don't update s->proplist here, because we
2616 * don't export that field */
2621 int pa_stream_set_monitor_stream(pa_stream *s, uint32_t sink_input_idx) {
2623 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2625 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED);
2626 PA_CHECK_VALIDITY(s->context, sink_input_idx != PA_INVALID_INDEX, PA_ERR_INVALID);
2627 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
2628 PA_CHECK_VALIDITY(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2630 s->direct_on_input = sink_input_idx;
2635 uint32_t pa_stream_get_monitor_stream(pa_stream *s) {
2637 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2639 PA_CHECK_VALIDITY_RETURN_ANY(s->context, !pa_detect_fork(), PA_ERR_FORKED, PA_INVALID_INDEX);
2640 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direct_on_input != PA_INVALID_INDEX, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2641 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
2643 return s->direct_on_input;