2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/def.h>
32 #include <pulse/timeval.h>
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/pstream-util.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/hashmap.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/rtclock.h>
43 #define LATENCY_IPOL_INTERVAL_USEC (333*PA_USEC_PER_MSEC)
45 #define SMOOTHER_ADJUST_TIME (1000*PA_USEC_PER_MSEC)
46 #define SMOOTHER_HISTORY_TIME (5000*PA_USEC_PER_MSEC)
47 #define SMOOTHER_MIN_HISTORY (4)
49 pa_stream *pa_stream_new(pa_context *c, const char *name, const pa_sample_spec *ss, const pa_channel_map *map) {
50 return pa_stream_new_with_proplist(c, name, ss, map, NULL);
53 static void reset_callbacks(pa_stream *s) {
54 s->read_callback = NULL;
55 s->read_userdata = NULL;
56 s->write_callback = NULL;
57 s->write_userdata = NULL;
58 s->state_callback = NULL;
59 s->state_userdata = NULL;
60 s->overflow_callback = NULL;
61 s->overflow_userdata = NULL;
62 s->underflow_callback = NULL;
63 s->underflow_userdata = NULL;
64 s->latency_update_callback = NULL;
65 s->latency_update_userdata = NULL;
66 s->moved_callback = NULL;
67 s->moved_userdata = NULL;
68 s->suspended_callback = NULL;
69 s->suspended_userdata = NULL;
70 s->started_callback = NULL;
71 s->started_userdata = NULL;
74 pa_stream *pa_stream_new_with_proplist(
77 const pa_sample_spec *ss,
78 const pa_channel_map *map,
86 pa_assert(PA_REFCNT_VALUE(c) >= 1);
88 PA_CHECK_VALIDITY_RETURN_NULL(c, ss && pa_sample_spec_valid(ss), PA_ERR_INVALID);
89 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 12 || (ss->format != PA_SAMPLE_S32LE && ss->format != PA_SAMPLE_S32BE), PA_ERR_NOTSUPPORTED);
90 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 15 || (ss->format != PA_SAMPLE_S24LE && ss->format != PA_SAMPLE_S24BE), PA_ERR_NOTSUPPORTED);
91 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 15 || (ss->format != PA_SAMPLE_S24_32LE && ss->format != PA_SAMPLE_S24_32BE), PA_ERR_NOTSUPPORTED);
92 PA_CHECK_VALIDITY_RETURN_NULL(c, !map || (pa_channel_map_valid(map) && map->channels == ss->channels), PA_ERR_INVALID);
93 PA_CHECK_VALIDITY_RETURN_NULL(c, name || (p && pa_proplist_contains(p, PA_PROP_MEDIA_NAME)), PA_ERR_INVALID);
96 PA_CHECK_VALIDITY_RETURN_NULL(c, map = pa_channel_map_init_auto(&tmap, ss->channels, PA_CHANNEL_MAP_DEFAULT), PA_ERR_INVALID);
98 s = pa_xnew(pa_stream, 1);
101 s->mainloop = c->mainloop;
103 s->direction = PA_STREAM_NODIRECTION;
104 s->state = PA_STREAM_UNCONNECTED;
107 s->sample_spec = *ss;
108 s->channel_map = *map;
110 s->direct_on_input = PA_INVALID_INDEX;
112 s->proplist = p ? pa_proplist_copy(p) : pa_proplist_new();
114 pa_proplist_sets(s->proplist, PA_PROP_MEDIA_NAME, name);
117 s->channel_valid = FALSE;
118 s->syncid = c->csyncid++;
119 s->stream_index = PA_INVALID_INDEX;
121 s->requested_bytes = 0;
122 memset(&s->buffer_attr, 0, sizeof(s->buffer_attr));
124 /* We initialize der target length here, so that if the user
125 * passes no explicit buffering metrics the default is similar to
126 * what older PA versions provided. */
128 s->buffer_attr.maxlength = (uint32_t) -1;
129 s->buffer_attr.tlength = (uint32_t) pa_usec_to_bytes(250*PA_USEC_PER_MSEC, ss); /* 250ms of buffering */
130 s->buffer_attr.minreq = (uint32_t) -1;
131 s->buffer_attr.prebuf = (uint32_t) -1;
132 s->buffer_attr.fragsize = (uint32_t) -1;
134 s->device_index = PA_INVALID_INDEX;
135 s->device_name = NULL;
136 s->suspended = FALSE;
138 pa_memchunk_reset(&s->peek_memchunk);
141 s->record_memblockq = NULL;
145 memset(&s->timing_info, 0, sizeof(s->timing_info));
146 s->timing_info_valid = FALSE;
148 s->previous_time = 0;
150 s->read_index_not_before = 0;
151 s->write_index_not_before = 0;
152 for (i = 0; i < PA_MAX_WRITE_INDEX_CORRECTIONS; i++)
153 s->write_index_corrections[i].valid = 0;
154 s->current_write_index_correction = 0;
156 s->auto_timing_update_event = NULL;
157 s->auto_timing_update_requested = FALSE;
163 /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
164 PA_LLIST_PREPEND(pa_stream, c->streams, s);
170 static void stream_unlink(pa_stream *s) {
177 /* Detach from context */
179 /* Unref all operatio object that point to us */
180 for (o = s->context->operations; o; o = n) {
184 pa_operation_cancel(o);
187 /* Drop all outstanding replies for this stream */
188 if (s->context->pdispatch)
189 pa_pdispatch_unregister_reply(s->context->pdispatch, s);
191 if (s->channel_valid) {
192 pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, NULL);
194 s->channel_valid = FALSE;
197 PA_LLIST_REMOVE(pa_stream, s->context->streams, s);
202 if (s->auto_timing_update_event) {
203 pa_assert(s->mainloop);
204 s->mainloop->time_free(s->auto_timing_update_event);
210 static void stream_free(pa_stream *s) {
215 if (s->peek_memchunk.memblock) {
217 pa_memblock_release(s->peek_memchunk.memblock);
218 pa_memblock_unref(s->peek_memchunk.memblock);
221 if (s->record_memblockq)
222 pa_memblockq_free(s->record_memblockq);
225 pa_proplist_free(s->proplist);
228 pa_smoother_free(s->smoother);
230 pa_xfree(s->device_name);
234 void pa_stream_unref(pa_stream *s) {
236 pa_assert(PA_REFCNT_VALUE(s) >= 1);
238 if (PA_REFCNT_DEC(s) <= 0)
242 pa_stream* pa_stream_ref(pa_stream *s) {
244 pa_assert(PA_REFCNT_VALUE(s) >= 1);
250 pa_stream_state_t pa_stream_get_state(pa_stream *s) {
252 pa_assert(PA_REFCNT_VALUE(s) >= 1);
257 pa_context* pa_stream_get_context(pa_stream *s) {
259 pa_assert(PA_REFCNT_VALUE(s) >= 1);
264 uint32_t pa_stream_get_index(pa_stream *s) {
266 pa_assert(PA_REFCNT_VALUE(s) >= 1);
268 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
270 return s->stream_index;
273 void pa_stream_set_state(pa_stream *s, pa_stream_state_t st) {
275 pa_assert(PA_REFCNT_VALUE(s) >= 1);
284 if (s->state_callback)
285 s->state_callback(s, s->state_userdata);
287 if ((st == PA_STREAM_FAILED || st == PA_STREAM_TERMINATED))
293 static void request_auto_timing_update(pa_stream *s, pa_bool_t force) {
295 pa_assert(PA_REFCNT_VALUE(s) >= 1);
297 if (!(s->flags & PA_STREAM_AUTO_TIMING_UPDATE))
300 if (s->state == PA_STREAM_READY &&
301 (force || !s->auto_timing_update_requested)) {
304 /* pa_log("automatically requesting new timing data"); */
306 if ((o = pa_stream_update_timing_info(s, NULL, NULL))) {
307 pa_operation_unref(o);
308 s->auto_timing_update_requested = TRUE;
312 if (s->auto_timing_update_event) {
314 pa_gettimeofday(&next);
315 pa_timeval_add(&next, LATENCY_IPOL_INTERVAL_USEC);
316 s->mainloop->time_restart(s->auto_timing_update_event, &next);
320 void pa_command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
321 pa_context *c = userdata;
326 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_KILLED || command == PA_COMMAND_RECORD_STREAM_KILLED);
329 pa_assert(PA_REFCNT_VALUE(c) >= 1);
333 if (pa_tagstruct_getu32(t, &channel) < 0 ||
334 !pa_tagstruct_eof(t)) {
335 pa_context_fail(c, PA_ERR_PROTOCOL);
339 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel)))
342 if (s->state != PA_STREAM_READY)
345 pa_context_set_error(c, PA_ERR_KILLED);
346 pa_stream_set_state(s, PA_STREAM_FAILED);
352 static void check_smoother_status(pa_stream *s, pa_bool_t aposteriori, pa_bool_t force_start, pa_bool_t force_stop) {
356 pa_assert(!force_start || !force_stop);
361 x = pa_rtclock_usec();
363 if (s->timing_info_valid) {
365 x -= s->timing_info.transport_usec;
367 x += s->timing_info.transport_usec;
369 if (s->direction == PA_STREAM_PLAYBACK)
370 /* it takes a while until the pause/resume is actually
372 x += s->timing_info.sink_usec;
374 /* Data froma while back will be dropped */
375 x -= s->timing_info.source_usec;
378 if (s->suspended || s->corked || force_stop)
379 pa_smoother_pause(s->smoother, x);
380 else if (force_start || s->buffer_attr.prebuf == 0)
381 pa_smoother_resume(s->smoother, x);
383 /* Please note that we have no idea if playback actually started
384 * if prebuf is non-zero! */
387 void pa_command_stream_moved(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
388 pa_context *c = userdata;
395 uint32_t maxlength = 0, fragsize = 0, minreq = 0, tlength = 0, prebuf = 0;
398 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_MOVED || command == PA_COMMAND_RECORD_STREAM_MOVED);
401 pa_assert(PA_REFCNT_VALUE(c) >= 1);
405 if (c->version < 12) {
406 pa_context_fail(c, PA_ERR_PROTOCOL);
410 if (pa_tagstruct_getu32(t, &channel) < 0 ||
411 pa_tagstruct_getu32(t, &di) < 0 ||
412 pa_tagstruct_gets(t, &dn) < 0 ||
413 pa_tagstruct_get_boolean(t, &suspended) < 0) {
414 pa_context_fail(c, PA_ERR_PROTOCOL);
418 if (c->version >= 13) {
420 if (command == PA_COMMAND_RECORD_STREAM_MOVED) {
421 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
422 pa_tagstruct_getu32(t, &fragsize) < 0 ||
423 pa_tagstruct_get_usec(t, &usec) < 0) {
424 pa_context_fail(c, PA_ERR_PROTOCOL);
428 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
429 pa_tagstruct_getu32(t, &tlength) < 0 ||
430 pa_tagstruct_getu32(t, &prebuf) < 0 ||
431 pa_tagstruct_getu32(t, &minreq) < 0 ||
432 pa_tagstruct_get_usec(t, &usec) < 0) {
433 pa_context_fail(c, PA_ERR_PROTOCOL);
439 if (!pa_tagstruct_eof(t)) {
440 pa_context_fail(c, PA_ERR_PROTOCOL);
444 if (!dn || di == PA_INVALID_INDEX) {
445 pa_context_fail(c, PA_ERR_PROTOCOL);
449 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_MOVED ? c->playback_streams : c->record_streams, channel)))
452 if (s->state != PA_STREAM_READY)
455 if (c->version >= 13) {
456 if (s->direction == PA_STREAM_RECORD)
457 s->timing_info.configured_source_usec = usec;
459 s->timing_info.configured_sink_usec = usec;
461 s->buffer_attr.maxlength = maxlength;
462 s->buffer_attr.fragsize = fragsize;
463 s->buffer_attr.tlength = tlength;
464 s->buffer_attr.prebuf = prebuf;
465 s->buffer_attr.minreq = minreq;
468 pa_xfree(s->device_name);
469 s->device_name = pa_xstrdup(dn);
470 s->device_index = di;
472 s->suspended = suspended;
474 check_smoother_status(s, TRUE, FALSE, FALSE);
475 request_auto_timing_update(s, TRUE);
477 if (s->moved_callback)
478 s->moved_callback(s, s->moved_userdata);
484 void pa_command_stream_suspended(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
485 pa_context *c = userdata;
491 pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED || command == PA_COMMAND_RECORD_STREAM_SUSPENDED);
494 pa_assert(PA_REFCNT_VALUE(c) >= 1);
498 if (c->version < 12) {
499 pa_context_fail(c, PA_ERR_PROTOCOL);
503 if (pa_tagstruct_getu32(t, &channel) < 0 ||
504 pa_tagstruct_get_boolean(t, &suspended) < 0 ||
505 !pa_tagstruct_eof(t)) {
506 pa_context_fail(c, PA_ERR_PROTOCOL);
510 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED ? c->playback_streams : c->record_streams, channel)))
513 if (s->state != PA_STREAM_READY)
516 s->suspended = suspended;
518 check_smoother_status(s, TRUE, FALSE, FALSE);
519 request_auto_timing_update(s, TRUE);
521 if (s->suspended_callback)
522 s->suspended_callback(s, s->suspended_userdata);
528 void pa_command_stream_started(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
529 pa_context *c = userdata;
534 pa_assert(command == PA_COMMAND_STARTED);
537 pa_assert(PA_REFCNT_VALUE(c) >= 1);
541 if (c->version < 13) {
542 pa_context_fail(c, PA_ERR_PROTOCOL);
546 if (pa_tagstruct_getu32(t, &channel) < 0 ||
547 !pa_tagstruct_eof(t)) {
548 pa_context_fail(c, PA_ERR_PROTOCOL);
552 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
555 if (s->state != PA_STREAM_READY)
558 check_smoother_status(s, TRUE, TRUE, FALSE);
559 request_auto_timing_update(s, TRUE);
561 if (s->started_callback)
562 s->started_callback(s, s->started_userdata);
568 void pa_command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
570 pa_context *c = userdata;
571 uint32_t bytes, channel;
574 pa_assert(command == PA_COMMAND_REQUEST);
577 pa_assert(PA_REFCNT_VALUE(c) >= 1);
581 if (pa_tagstruct_getu32(t, &channel) < 0 ||
582 pa_tagstruct_getu32(t, &bytes) < 0 ||
583 !pa_tagstruct_eof(t)) {
584 pa_context_fail(c, PA_ERR_PROTOCOL);
588 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
591 if (s->state != PA_STREAM_READY)
594 s->requested_bytes += bytes;
596 if (s->requested_bytes > 0 && s->write_callback)
597 s->write_callback(s, s->requested_bytes, s->write_userdata);
603 void pa_command_overflow_or_underflow(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
605 pa_context *c = userdata;
609 pa_assert(command == PA_COMMAND_OVERFLOW || command == PA_COMMAND_UNDERFLOW);
612 pa_assert(PA_REFCNT_VALUE(c) >= 1);
616 if (pa_tagstruct_getu32(t, &channel) < 0 ||
617 !pa_tagstruct_eof(t)) {
618 pa_context_fail(c, PA_ERR_PROTOCOL);
622 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
625 if (s->state != PA_STREAM_READY)
628 if (s->buffer_attr.prebuf > 0)
629 check_smoother_status(s, TRUE, FALSE, TRUE);
631 request_auto_timing_update(s, TRUE);
633 if (command == PA_COMMAND_OVERFLOW) {
634 if (s->overflow_callback)
635 s->overflow_callback(s, s->overflow_userdata);
636 } else if (command == PA_COMMAND_UNDERFLOW) {
637 if (s->underflow_callback)
638 s->underflow_callback(s, s->underflow_userdata);
645 static void invalidate_indexes(pa_stream *s, pa_bool_t r, pa_bool_t w) {
647 pa_assert(PA_REFCNT_VALUE(s) >= 1);
649 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
651 if (s->state != PA_STREAM_READY)
655 s->write_index_not_before = s->context->ctag;
657 if (s->timing_info_valid)
658 s->timing_info.write_index_corrupt = TRUE;
660 /* pa_log("write_index invalidated"); */
664 s->read_index_not_before = s->context->ctag;
666 if (s->timing_info_valid)
667 s->timing_info.read_index_corrupt = TRUE;
669 /* pa_log("read_index invalidated"); */
672 request_auto_timing_update(s, TRUE);
675 static void auto_timing_update_callback(pa_mainloop_api *m, pa_time_event *e, const struct timeval *tv, void *userdata) {
676 pa_stream *s = userdata;
679 pa_assert(PA_REFCNT_VALUE(s) >= 1);
682 request_auto_timing_update(s, FALSE);
686 static void create_stream_complete(pa_stream *s) {
688 pa_assert(PA_REFCNT_VALUE(s) >= 1);
689 pa_assert(s->state == PA_STREAM_CREATING);
691 pa_stream_set_state(s, PA_STREAM_READY);
693 if (s->requested_bytes > 0 && s->write_callback)
694 s->write_callback(s, s->requested_bytes, s->write_userdata);
696 if (s->flags & PA_STREAM_AUTO_TIMING_UPDATE) {
698 pa_gettimeofday(&tv);
699 tv.tv_usec += (suseconds_t) LATENCY_IPOL_INTERVAL_USEC; /* every 100 ms */
700 pa_assert(!s->auto_timing_update_event);
701 s->auto_timing_update_event = s->mainloop->time_new(s->mainloop, &tv, &auto_timing_update_callback, s);
703 request_auto_timing_update(s, TRUE);
706 check_smoother_status(s, TRUE, FALSE, FALSE);
709 static void automatic_buffer_attr(pa_stream *s, pa_buffer_attr *attr, const pa_sample_spec *ss) {
714 if (s->context->version >= 13)
717 /* Version older than 0.9.10 didn't do server side buffer_attr
718 * selection, hence we have to fake it on the client side. */
720 /* We choose fairly conservative values here, to not confuse
721 * old clients with extremely large playback buffers */
723 if (attr->maxlength == (uint32_t) -1)
724 attr->maxlength = 4*1024*1024; /* 4MB is the maximum queue length PulseAudio <= 0.9.9 supported. */
726 if (attr->tlength == (uint32_t) -1)
727 attr->tlength = (uint32_t) pa_usec_to_bytes(250*PA_USEC_PER_MSEC, ss); /* 250ms of buffering */
729 if (attr->minreq == (uint32_t) -1)
730 attr->minreq = (attr->tlength)/5; /* Ask for more data when there are only 200ms left in the playback buffer */
732 if (attr->prebuf == (uint32_t) -1)
733 attr->prebuf = attr->tlength; /* Start to play only when the playback is fully filled up once */
735 if (attr->fragsize == (uint32_t) -1)
736 attr->fragsize = attr->tlength; /* Pass data to the app only when the buffer is filled up once */
739 void pa_create_stream_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
740 pa_stream *s = userdata;
744 pa_assert(PA_REFCNT_VALUE(s) >= 1);
745 pa_assert(s->state == PA_STREAM_CREATING);
749 if (command != PA_COMMAND_REPLY) {
750 if (pa_context_handle_error(s->context, command, t, FALSE) < 0)
753 pa_stream_set_state(s, PA_STREAM_FAILED);
757 if (pa_tagstruct_getu32(t, &s->channel) < 0 ||
758 s->channel == PA_INVALID_INDEX ||
759 ((s->direction != PA_STREAM_UPLOAD) && (pa_tagstruct_getu32(t, &s->stream_index) < 0 || s->stream_index == PA_INVALID_INDEX)) ||
760 ((s->direction != PA_STREAM_RECORD) && pa_tagstruct_getu32(t, &s->requested_bytes) < 0)) {
761 pa_context_fail(s->context, PA_ERR_PROTOCOL);
765 if (s->context->version >= 9) {
766 if (s->direction == PA_STREAM_PLAYBACK) {
767 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
768 pa_tagstruct_getu32(t, &s->buffer_attr.tlength) < 0 ||
769 pa_tagstruct_getu32(t, &s->buffer_attr.prebuf) < 0 ||
770 pa_tagstruct_getu32(t, &s->buffer_attr.minreq) < 0) {
771 pa_context_fail(s->context, PA_ERR_PROTOCOL);
774 } else if (s->direction == PA_STREAM_RECORD) {
775 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
776 pa_tagstruct_getu32(t, &s->buffer_attr.fragsize) < 0) {
777 pa_context_fail(s->context, PA_ERR_PROTOCOL);
783 if (s->context->version >= 12 && s->direction != PA_STREAM_UPLOAD) {
786 const char *dn = NULL;
789 if (pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
790 pa_tagstruct_get_channel_map(t, &cm) < 0 ||
791 pa_tagstruct_getu32(t, &s->device_index) < 0 ||
792 pa_tagstruct_gets(t, &dn) < 0 ||
793 pa_tagstruct_get_boolean(t, &suspended) < 0) {
794 pa_context_fail(s->context, PA_ERR_PROTOCOL);
798 if (!dn || s->device_index == PA_INVALID_INDEX ||
799 ss.channels != cm.channels ||
800 !pa_channel_map_valid(&cm) ||
801 !pa_sample_spec_valid(&ss) ||
802 (!(s->flags & PA_STREAM_FIX_FORMAT) && ss.format != s->sample_spec.format) ||
803 (!(s->flags & PA_STREAM_FIX_RATE) && ss.rate != s->sample_spec.rate) ||
804 (!(s->flags & PA_STREAM_FIX_CHANNELS) && !pa_channel_map_equal(&cm, &s->channel_map))) {
805 pa_context_fail(s->context, PA_ERR_PROTOCOL);
809 pa_xfree(s->device_name);
810 s->device_name = pa_xstrdup(dn);
811 s->suspended = suspended;
817 if (s->context->version >= 13 && s->direction != PA_STREAM_UPLOAD) {
820 if (pa_tagstruct_get_usec(t, &usec) < 0) {
821 pa_context_fail(s->context, PA_ERR_PROTOCOL);
825 if (s->direction == PA_STREAM_RECORD)
826 s->timing_info.configured_source_usec = usec;
828 s->timing_info.configured_sink_usec = usec;
831 if (!pa_tagstruct_eof(t)) {
832 pa_context_fail(s->context, PA_ERR_PROTOCOL);
836 if (s->direction == PA_STREAM_RECORD) {
837 pa_assert(!s->record_memblockq);
839 s->record_memblockq = pa_memblockq_new(
841 s->buffer_attr.maxlength,
843 pa_frame_size(&s->sample_spec),
850 s->channel_valid = TRUE;
851 pa_dynarray_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, s->channel, s);
853 create_stream_complete(s);
859 static int create_stream(
860 pa_stream_direction_t direction,
863 const pa_buffer_attr *attr,
864 pa_stream_flags_t flags,
865 const pa_cvolume *volume,
866 pa_stream *sync_stream) {
870 pa_bool_t volume_set = FALSE;
873 pa_assert(PA_REFCNT_VALUE(s) >= 1);
874 pa_assert(direction == PA_STREAM_PLAYBACK || direction == PA_STREAM_RECORD);
876 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
877 PA_CHECK_VALIDITY(s->context, s->direct_on_input == PA_INVALID_INDEX || direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
878 PA_CHECK_VALIDITY(s->context, !(flags & ~(PA_STREAM_START_CORKED|
879 PA_STREAM_INTERPOLATE_TIMING|
880 PA_STREAM_NOT_MONOTONIC|
881 PA_STREAM_AUTO_TIMING_UPDATE|
882 PA_STREAM_NO_REMAP_CHANNELS|
883 PA_STREAM_NO_REMIX_CHANNELS|
884 PA_STREAM_FIX_FORMAT|
886 PA_STREAM_FIX_CHANNELS|
888 PA_STREAM_VARIABLE_RATE|
889 PA_STREAM_PEAK_DETECT|
890 PA_STREAM_START_MUTED|
891 PA_STREAM_ADJUST_LATENCY|
892 PA_STREAM_EARLY_REQUESTS|
893 PA_STREAM_DONT_INHIBIT_AUTO_SUSPEND|
894 PA_STREAM_START_UNMUTED|
895 PA_STREAM_FAIL_ON_SUSPEND)), PA_ERR_INVALID);
897 PA_CHECK_VALIDITY(s->context, s->context->version >= 12 || !(flags & PA_STREAM_VARIABLE_RATE), PA_ERR_NOTSUPPORTED);
898 PA_CHECK_VALIDITY(s->context, s->context->version >= 13 || !(flags & PA_STREAM_PEAK_DETECT), PA_ERR_NOTSUPPORTED);
899 /* Althought some of the other flags are not supported on older
900 * version, we don't check for them here, because it doesn't hurt
901 * when they are passed but actually not supported. This makes
902 * client development easier */
904 PA_CHECK_VALIDITY(s->context, direction == PA_STREAM_PLAYBACK || !(flags & (PA_STREAM_START_MUTED)), PA_ERR_INVALID);
905 PA_CHECK_VALIDITY(s->context, direction == PA_STREAM_RECORD || !(flags & (PA_STREAM_PEAK_DETECT)), PA_ERR_INVALID);
906 PA_CHECK_VALIDITY(s->context, !volume || volume->channels == s->sample_spec.channels, PA_ERR_INVALID);
907 PA_CHECK_VALIDITY(s->context, !sync_stream || (direction == PA_STREAM_PLAYBACK && sync_stream->direction == PA_STREAM_PLAYBACK), PA_ERR_INVALID);
908 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);
912 s->direction = direction;
914 s->corked = !!(flags & PA_STREAM_START_CORKED);
917 s->syncid = sync_stream->syncid;
920 s->buffer_attr = *attr;
921 automatic_buffer_attr(s, &s->buffer_attr, &s->sample_spec);
923 if (flags & PA_STREAM_INTERPOLATE_TIMING) {
927 pa_smoother_free(s->smoother);
929 s->smoother = pa_smoother_new(SMOOTHER_ADJUST_TIME, SMOOTHER_HISTORY_TIME, !(flags & PA_STREAM_NOT_MONOTONIC), SMOOTHER_MIN_HISTORY);
931 x = pa_rtclock_usec();
932 pa_smoother_set_time_offset(s->smoother, x);
933 pa_smoother_pause(s->smoother, x);
937 dev = s->direction == PA_STREAM_PLAYBACK ? s->context->conf->default_sink : s->context->conf->default_source;
939 t = pa_tagstruct_command(
941 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM),
944 if (s->context->version < 13)
945 pa_tagstruct_puts(t, pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME));
949 PA_TAG_SAMPLE_SPEC, &s->sample_spec,
950 PA_TAG_CHANNEL_MAP, &s->channel_map,
951 PA_TAG_U32, PA_INVALID_INDEX,
953 PA_TAG_U32, s->buffer_attr.maxlength,
954 PA_TAG_BOOLEAN, s->corked,
957 if (s->direction == PA_STREAM_PLAYBACK) {
962 PA_TAG_U32, s->buffer_attr.tlength,
963 PA_TAG_U32, s->buffer_attr.prebuf,
964 PA_TAG_U32, s->buffer_attr.minreq,
965 PA_TAG_U32, s->syncid,
968 volume_set = !!volume;
971 volume = pa_cvolume_reset(&cv, s->sample_spec.channels);
973 pa_tagstruct_put_cvolume(t, volume);
975 pa_tagstruct_putu32(t, s->buffer_attr.fragsize);
977 if (s->context->version >= 12) {
980 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMAP_CHANNELS,
981 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMIX_CHANNELS,
982 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_FORMAT,
983 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_RATE,
984 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_CHANNELS,
985 PA_TAG_BOOLEAN, flags & PA_STREAM_DONT_MOVE,
986 PA_TAG_BOOLEAN, flags & PA_STREAM_VARIABLE_RATE,
990 if (s->context->version >= 13) {
992 if (s->direction == PA_STREAM_PLAYBACK)
993 pa_tagstruct_put_boolean(t, flags & PA_STREAM_START_MUTED);
995 pa_tagstruct_put_boolean(t, flags & PA_STREAM_PEAK_DETECT);
999 PA_TAG_BOOLEAN, flags & PA_STREAM_ADJUST_LATENCY,
1000 PA_TAG_PROPLIST, s->proplist,
1003 if (s->direction == PA_STREAM_RECORD)
1004 pa_tagstruct_putu32(t, s->direct_on_input);
1007 if (s->context->version >= 14) {
1009 if (s->direction == PA_STREAM_PLAYBACK)
1010 pa_tagstruct_put_boolean(t, volume_set);
1012 pa_tagstruct_put_boolean(t, flags & PA_STREAM_EARLY_REQUESTS);
1015 if (s->context->version >= 15) {
1017 if (s->direction == PA_STREAM_PLAYBACK)
1018 pa_tagstruct_put_boolean(t, flags & (PA_STREAM_START_MUTED|PA_STREAM_START_UNMUTED));
1020 pa_tagstruct_put_boolean(t, flags & PA_STREAM_DONT_INHIBIT_AUTO_SUSPEND);
1021 pa_tagstruct_put_boolean(t, flags & PA_STREAM_FAIL_ON_SUSPEND);
1024 pa_pstream_send_tagstruct(s->context->pstream, t);
1025 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s, NULL);
1027 pa_stream_set_state(s, PA_STREAM_CREATING);
1033 int pa_stream_connect_playback(
1036 const pa_buffer_attr *attr,
1037 pa_stream_flags_t flags,
1039 pa_stream *sync_stream) {
1042 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1044 return create_stream(PA_STREAM_PLAYBACK, s, dev, attr, flags, volume, sync_stream);
1047 int pa_stream_connect_record(
1050 const pa_buffer_attr *attr,
1051 pa_stream_flags_t flags) {
1054 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1056 return create_stream(PA_STREAM_RECORD, s, dev, attr, flags, NULL, NULL);
1059 int pa_stream_write(
1063 void (*free_cb)(void *p),
1065 pa_seek_mode_t seek) {
1068 pa_seek_mode_t t_seek;
1074 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1077 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1078 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || s->direction == PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1079 PA_CHECK_VALIDITY(s->context, seek <= PA_SEEK_RELATIVE_END, PA_ERR_INVALID);
1080 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || (seek == PA_SEEK_RELATIVE && offset == 0), PA_ERR_INVALID);
1090 while (t_length > 0) {
1094 if (free_cb && !pa_pstream_get_shm(s->context->pstream)) {
1095 chunk.memblock = pa_memblock_new_user(s->context->mempool, (void*) t_data, t_length, free_cb, 1);
1096 chunk.length = t_length;
1100 chunk.length = PA_MIN(t_length, pa_mempool_block_size_max(s->context->mempool));
1101 chunk.memblock = pa_memblock_new(s->context->mempool, chunk.length);
1103 d = pa_memblock_acquire(chunk.memblock);
1104 memcpy(d, t_data, chunk.length);
1105 pa_memblock_release(chunk.memblock);
1108 pa_pstream_send_memblock(s->context->pstream, s->channel, t_offset, t_seek, &chunk);
1111 t_seek = PA_SEEK_RELATIVE;
1113 t_data = (const uint8_t*) t_data + chunk.length;
1114 t_length -= chunk.length;
1116 pa_memblock_unref(chunk.memblock);
1119 if (free_cb && pa_pstream_get_shm(s->context->pstream))
1120 free_cb((void*) data);
1122 if (length < s->requested_bytes)
1123 s->requested_bytes -= (uint32_t) length;
1125 s->requested_bytes = 0;
1127 /* FIXME!!! ^^^ will break when offset is != 0 and mode is not RELATIVE*/
1129 if (s->direction == PA_STREAM_PLAYBACK) {
1131 /* Update latency request correction */
1132 if (s->write_index_corrections[s->current_write_index_correction].valid) {
1134 if (seek == PA_SEEK_ABSOLUTE) {
1135 s->write_index_corrections[s->current_write_index_correction].corrupt = FALSE;
1136 s->write_index_corrections[s->current_write_index_correction].absolute = TRUE;
1137 s->write_index_corrections[s->current_write_index_correction].value = offset + (int64_t) length;
1138 } else if (seek == PA_SEEK_RELATIVE) {
1139 if (!s->write_index_corrections[s->current_write_index_correction].corrupt)
1140 s->write_index_corrections[s->current_write_index_correction].value += offset + (int64_t) length;
1142 s->write_index_corrections[s->current_write_index_correction].corrupt = TRUE;
1145 /* Update the write index in the already available latency data */
1146 if (s->timing_info_valid) {
1148 if (seek == PA_SEEK_ABSOLUTE) {
1149 s->timing_info.write_index_corrupt = FALSE;
1150 s->timing_info.write_index = offset + (int64_t) length;
1151 } else if (seek == PA_SEEK_RELATIVE) {
1152 if (!s->timing_info.write_index_corrupt)
1153 s->timing_info.write_index += offset + (int64_t) length;
1155 s->timing_info.write_index_corrupt = TRUE;
1158 if (!s->timing_info_valid || s->timing_info.write_index_corrupt)
1159 request_auto_timing_update(s, TRUE);
1165 int pa_stream_peek(pa_stream *s, const void **data, size_t *length) {
1167 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1171 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1172 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1174 if (!s->peek_memchunk.memblock) {
1176 if (pa_memblockq_peek(s->record_memblockq, &s->peek_memchunk) < 0) {
1182 s->peek_data = pa_memblock_acquire(s->peek_memchunk.memblock);
1185 pa_assert(s->peek_data);
1186 *data = (uint8_t*) s->peek_data + s->peek_memchunk.index;
1187 *length = s->peek_memchunk.length;
1191 int pa_stream_drop(pa_stream *s) {
1193 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1195 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1196 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1197 PA_CHECK_VALIDITY(s->context, s->peek_memchunk.memblock, PA_ERR_BADSTATE);
1199 pa_memblockq_drop(s->record_memblockq, s->peek_memchunk.length);
1201 /* Fix the simulated local read index */
1202 if (s->timing_info_valid && !s->timing_info.read_index_corrupt)
1203 s->timing_info.read_index += (int64_t) s->peek_memchunk.length;
1205 pa_assert(s->peek_data);
1206 pa_memblock_release(s->peek_memchunk.memblock);
1207 pa_memblock_unref(s->peek_memchunk.memblock);
1208 pa_memchunk_reset(&s->peek_memchunk);
1213 size_t pa_stream_writable_size(pa_stream *s) {
1215 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1217 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
1218 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
1220 return s->requested_bytes;
1223 size_t pa_stream_readable_size(pa_stream *s) {
1225 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1227 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
1228 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
1230 return pa_memblockq_get_length(s->record_memblockq);
1233 pa_operation * pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1239 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1241 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1242 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1244 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1246 t = pa_tagstruct_command(s->context, PA_COMMAND_DRAIN_PLAYBACK_STREAM, &tag);
1247 pa_tagstruct_putu32(t, s->channel);
1248 pa_pstream_send_tagstruct(s->context->pstream, t);
1249 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);
1254 static pa_usec_t calc_time(pa_stream *s, pa_bool_t ignore_transport) {
1258 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1259 pa_assert(s->state == PA_STREAM_READY);
1260 pa_assert(s->direction != PA_STREAM_UPLOAD);
1261 pa_assert(s->timing_info_valid);
1262 pa_assert(s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt);
1263 pa_assert(s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt);
1265 if (s->direction == PA_STREAM_PLAYBACK) {
1266 /* The last byte that was written into the output device
1267 * had this time value associated */
1268 usec = pa_bytes_to_usec(s->timing_info.read_index < 0 ? 0 : (uint64_t) s->timing_info.read_index, &s->sample_spec);
1270 if (!s->corked && !s->suspended) {
1272 if (!ignore_transport)
1273 /* Because the latency info took a little time to come
1274 * to us, we assume that the real output time is actually
1276 usec += s->timing_info.transport_usec;
1278 /* However, the output device usually maintains a buffer
1279 too, hence the real sample currently played is a little
1281 if (s->timing_info.sink_usec >= usec)
1284 usec -= s->timing_info.sink_usec;
1288 pa_assert(s->direction == PA_STREAM_RECORD);
1290 /* The last byte written into the server side queue had
1291 * this time value associated */
1292 usec = pa_bytes_to_usec(s->timing_info.write_index < 0 ? 0 : (uint64_t) s->timing_info.write_index, &s->sample_spec);
1294 if (!s->corked && !s->suspended) {
1296 if (!ignore_transport)
1297 /* Add transport latency */
1298 usec += s->timing_info.transport_usec;
1300 /* Add latency of data in device buffer */
1301 usec += s->timing_info.source_usec;
1303 /* If this is a monitor source, we need to correct the
1304 * time by the playback device buffer */
1305 if (s->timing_info.sink_usec >= usec)
1308 usec -= s->timing_info.sink_usec;
1315 static void stream_get_timing_info_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1316 pa_operation *o = userdata;
1317 struct timeval local, remote, now;
1319 pa_bool_t playing = FALSE;
1320 uint64_t underrun_for = 0, playing_for = 0;
1324 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1326 if (!o->context || !o->stream)
1329 i = &o->stream->timing_info;
1331 o->stream->timing_info_valid = FALSE;
1332 i->write_index_corrupt = TRUE;
1333 i->read_index_corrupt = TRUE;
1335 if (command != PA_COMMAND_REPLY) {
1336 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
1341 if (pa_tagstruct_get_usec(t, &i->sink_usec) < 0 ||
1342 pa_tagstruct_get_usec(t, &i->source_usec) < 0 ||
1343 pa_tagstruct_get_boolean(t, &playing) < 0 ||
1344 pa_tagstruct_get_timeval(t, &local) < 0 ||
1345 pa_tagstruct_get_timeval(t, &remote) < 0 ||
1346 pa_tagstruct_gets64(t, &i->write_index) < 0 ||
1347 pa_tagstruct_gets64(t, &i->read_index) < 0) {
1349 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1353 if (o->context->version >= 13 &&
1354 o->stream->direction == PA_STREAM_PLAYBACK)
1355 if (pa_tagstruct_getu64(t, &underrun_for) < 0 ||
1356 pa_tagstruct_getu64(t, &playing_for) < 0) {
1358 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1363 if (!pa_tagstruct_eof(t)) {
1364 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1367 o->stream->timing_info_valid = TRUE;
1368 i->write_index_corrupt = FALSE;
1369 i->read_index_corrupt = FALSE;
1371 i->playing = (int) playing;
1372 i->since_underrun = (int64_t) (playing ? playing_for : underrun_for);
1374 pa_gettimeofday(&now);
1376 /* Calculcate timestamps */
1377 if (pa_timeval_cmp(&local, &remote) <= 0 && pa_timeval_cmp(&remote, &now) <= 0) {
1378 /* local and remote seem to have synchronized clocks */
1380 if (o->stream->direction == PA_STREAM_PLAYBACK)
1381 i->transport_usec = pa_timeval_diff(&remote, &local);
1383 i->transport_usec = pa_timeval_diff(&now, &remote);
1385 i->synchronized_clocks = TRUE;
1386 i->timestamp = remote;
1388 /* clocks are not synchronized, let's estimate latency then */
1389 i->transport_usec = pa_timeval_diff(&now, &local)/2;
1390 i->synchronized_clocks = FALSE;
1391 i->timestamp = local;
1392 pa_timeval_add(&i->timestamp, i->transport_usec);
1395 /* Invalidate read and write indexes if necessary */
1396 if (tag < o->stream->read_index_not_before)
1397 i->read_index_corrupt = TRUE;
1399 if (tag < o->stream->write_index_not_before)
1400 i->write_index_corrupt = TRUE;
1402 if (o->stream->direction == PA_STREAM_PLAYBACK) {
1403 /* Write index correction */
1406 uint32_t ctag = tag;
1408 /* Go through the saved correction values and add up the
1409 * total correction.*/
1410 for (n = 0, j = o->stream->current_write_index_correction+1;
1411 n < PA_MAX_WRITE_INDEX_CORRECTIONS;
1412 n++, j = (j + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS) {
1414 /* Step over invalid data or out-of-date data */
1415 if (!o->stream->write_index_corrections[j].valid ||
1416 o->stream->write_index_corrections[j].tag < ctag)
1419 /* Make sure that everything is in order */
1420 ctag = o->stream->write_index_corrections[j].tag+1;
1422 /* Now fix the write index */
1423 if (o->stream->write_index_corrections[j].corrupt) {
1424 /* A corrupting seek was made */
1425 i->write_index_corrupt = TRUE;
1426 } else if (o->stream->write_index_corrections[j].absolute) {
1427 /* An absolute seek was made */
1428 i->write_index = o->stream->write_index_corrections[j].value;
1429 i->write_index_corrupt = FALSE;
1430 } else if (!i->write_index_corrupt) {
1431 /* A relative seek was made */
1432 i->write_index += o->stream->write_index_corrections[j].value;
1436 /* Clear old correction entries */
1437 for (n = 0; n < PA_MAX_WRITE_INDEX_CORRECTIONS; n++) {
1438 if (!o->stream->write_index_corrections[n].valid)
1441 if (o->stream->write_index_corrections[n].tag <= tag)
1442 o->stream->write_index_corrections[n].valid = FALSE;
1446 if (o->stream->direction == PA_STREAM_RECORD) {
1447 /* Read index correction */
1449 if (!i->read_index_corrupt)
1450 i->read_index -= (int64_t) pa_memblockq_get_length(o->stream->record_memblockq);
1453 /* Update smoother */
1454 if (o->stream->smoother) {
1457 u = x = pa_rtclock_usec() - i->transport_usec;
1459 if (o->stream->direction == PA_STREAM_PLAYBACK && o->context->version >= 13) {
1462 /* If we weren't playing then it will take some time
1463 * until the audio will actually come out through the
1464 * speakers. Since we follow that timing here, we need
1465 * to try to fix this up */
1467 su = pa_bytes_to_usec((uint64_t) i->since_underrun, &o->stream->sample_spec);
1469 if (su < i->sink_usec)
1470 x += i->sink_usec - su;
1474 pa_smoother_pause(o->stream->smoother, x);
1476 /* Update the smoother */
1477 if ((o->stream->direction == PA_STREAM_PLAYBACK && !i->read_index_corrupt) ||
1478 (o->stream->direction == PA_STREAM_RECORD && !i->write_index_corrupt))
1479 pa_smoother_put(o->stream->smoother, u, calc_time(o->stream, TRUE));
1482 pa_smoother_resume(o->stream->smoother, x);
1486 o->stream->auto_timing_update_requested = FALSE;
1488 if (o->stream->latency_update_callback)
1489 o->stream->latency_update_callback(o->stream, o->stream->latency_update_userdata);
1491 if (o->callback && o->stream && o->stream->state == PA_STREAM_READY) {
1492 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1493 cb(o->stream, o->stream->timing_info_valid, o->userdata);
1498 pa_operation_done(o);
1499 pa_operation_unref(o);
1502 pa_operation* pa_stream_update_timing_info(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1510 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1512 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1513 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1515 if (s->direction == PA_STREAM_PLAYBACK) {
1516 /* Find a place to store the write_index correction data for this entry */
1517 cidx = (s->current_write_index_correction + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS;
1519 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
1520 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !s->write_index_corrections[cidx].valid, PA_ERR_INTERNAL);
1522 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1524 t = pa_tagstruct_command(
1526 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_GET_PLAYBACK_LATENCY : PA_COMMAND_GET_RECORD_LATENCY),
1528 pa_tagstruct_putu32(t, s->channel);
1529 pa_tagstruct_put_timeval(t, pa_gettimeofday(&now));
1531 pa_pstream_send_tagstruct(s->context->pstream, t);
1532 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);
1534 if (s->direction == PA_STREAM_PLAYBACK) {
1535 /* Fill in initial correction data */
1537 s->current_write_index_correction = cidx;
1539 s->write_index_corrections[cidx].valid = TRUE;
1540 s->write_index_corrections[cidx].absolute = FALSE;
1541 s->write_index_corrections[cidx].corrupt = FALSE;
1542 s->write_index_corrections[cidx].tag = tag;
1543 s->write_index_corrections[cidx].value = 0;
1549 void pa_stream_disconnect_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1550 pa_stream *s = userdata;
1554 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1558 if (command != PA_COMMAND_REPLY) {
1559 if (pa_context_handle_error(s->context, command, t, FALSE) < 0)
1562 pa_stream_set_state(s, PA_STREAM_FAILED);
1564 } else if (!pa_tagstruct_eof(t)) {
1565 pa_context_fail(s->context, PA_ERR_PROTOCOL);
1569 pa_stream_set_state(s, PA_STREAM_TERMINATED);
1575 int pa_stream_disconnect(pa_stream *s) {
1580 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1582 PA_CHECK_VALIDITY(s->context, s->channel_valid, PA_ERR_BADSTATE);
1583 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1587 t = pa_tagstruct_command(
1589 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_DELETE_PLAYBACK_STREAM :
1590 (s->direction == PA_STREAM_RECORD ? PA_COMMAND_DELETE_RECORD_STREAM : PA_COMMAND_DELETE_UPLOAD_STREAM)),
1592 pa_tagstruct_putu32(t, s->channel);
1593 pa_pstream_send_tagstruct(s->context->pstream, t);
1594 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s, NULL);
1600 void pa_stream_set_read_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1602 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1604 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1607 s->read_callback = cb;
1608 s->read_userdata = userdata;
1611 void pa_stream_set_write_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1613 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1615 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1618 s->write_callback = cb;
1619 s->write_userdata = userdata;
1622 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1624 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1626 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1629 s->state_callback = cb;
1630 s->state_userdata = userdata;
1633 void pa_stream_set_overflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1635 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1637 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1640 s->overflow_callback = cb;
1641 s->overflow_userdata = userdata;
1644 void pa_stream_set_underflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1646 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1648 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1651 s->underflow_callback = cb;
1652 s->underflow_userdata = userdata;
1655 void pa_stream_set_latency_update_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1657 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1659 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1662 s->latency_update_callback = cb;
1663 s->latency_update_userdata = userdata;
1666 void pa_stream_set_moved_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1668 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1670 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1673 s->moved_callback = cb;
1674 s->moved_userdata = userdata;
1677 void pa_stream_set_suspended_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1679 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1681 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1684 s->suspended_callback = cb;
1685 s->suspended_userdata = userdata;
1688 void pa_stream_set_started_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1690 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1692 if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1695 s->started_callback = cb;
1696 s->started_userdata = userdata;
1699 void pa_stream_simple_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1700 pa_operation *o = userdata;
1705 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1710 if (command != PA_COMMAND_REPLY) {
1711 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
1715 } else if (!pa_tagstruct_eof(t)) {
1716 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1721 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1722 cb(o->stream, success, o->userdata);
1726 pa_operation_done(o);
1727 pa_operation_unref(o);
1730 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata) {
1736 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1738 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1739 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1743 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1745 t = pa_tagstruct_command(
1747 (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CORK_PLAYBACK_STREAM : PA_COMMAND_CORK_RECORD_STREAM),
1749 pa_tagstruct_putu32(t, s->channel);
1750 pa_tagstruct_put_boolean(t, !!b);
1751 pa_pstream_send_tagstruct(s->context->pstream, t);
1752 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);
1754 check_smoother_status(s, FALSE, FALSE, FALSE);
1756 /* This might cause the indexes to hang/start again, hence
1757 * let's request a timing update */
1758 request_auto_timing_update(s, TRUE);
1763 static pa_operation* stream_send_simple_command(pa_stream *s, uint32_t command, pa_stream_success_cb_t cb, void *userdata) {
1769 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1771 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1773 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1775 t = pa_tagstruct_command(s->context, command, &tag);
1776 pa_tagstruct_putu32(t, s->channel);
1777 pa_pstream_send_tagstruct(s->context->pstream, t);
1778 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);
1783 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1787 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1789 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1790 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1792 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)))
1795 if (s->direction == PA_STREAM_PLAYBACK) {
1797 if (s->write_index_corrections[s->current_write_index_correction].valid)
1798 s->write_index_corrections[s->current_write_index_correction].corrupt = TRUE;
1800 if (s->buffer_attr.prebuf > 0)
1801 check_smoother_status(s, FALSE, FALSE, TRUE);
1803 /* This will change the write index, but leave the
1804 * read index untouched. */
1805 invalidate_indexes(s, FALSE, TRUE);
1808 /* For record streams this has no influence on the write
1809 * index, but the read index might jump. */
1810 invalidate_indexes(s, TRUE, FALSE);
1815 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1819 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1821 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1822 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1823 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1825 if (!(o = stream_send_simple_command(s, PA_COMMAND_PREBUF_PLAYBACK_STREAM, cb, userdata)))
1828 /* This might cause the read index to hang again, hence
1829 * let's request a timing update */
1830 request_auto_timing_update(s, TRUE);
1835 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1839 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1841 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1842 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1843 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1845 if (!(o = stream_send_simple_command(s, PA_COMMAND_TRIGGER_PLAYBACK_STREAM, cb, userdata)))
1848 /* This might cause the read index to start moving again, hence
1849 * let's request a timing update */
1850 request_auto_timing_update(s, TRUE);
1855 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata) {
1859 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1862 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1863 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1865 if (s->context->version >= 13) {
1866 pa_proplist *p = pa_proplist_new();
1868 pa_proplist_sets(p, PA_PROP_MEDIA_NAME, name);
1869 o = pa_stream_proplist_update(s, PA_UPDATE_REPLACE, p, cb, userdata);
1870 pa_proplist_free(p);
1875 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1876 t = pa_tagstruct_command(
1878 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_NAME : PA_COMMAND_SET_PLAYBACK_STREAM_NAME),
1880 pa_tagstruct_putu32(t, s->channel);
1881 pa_tagstruct_puts(t, name);
1882 pa_pstream_send_tagstruct(s->context->pstream, t);
1883 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);
1889 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec) {
1893 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1895 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1896 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1897 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1898 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1899 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1902 usec = pa_smoother_get(s->smoother, pa_rtclock_usec());
1904 usec = calc_time(s, FALSE);
1906 /* Make sure the time runs monotonically */
1907 if (!(s->flags & PA_STREAM_NOT_MONOTONIC)) {
1908 if (usec < s->previous_time)
1909 usec = s->previous_time;
1911 s->previous_time = usec;
1920 static pa_usec_t time_counter_diff(pa_stream *s, pa_usec_t a, pa_usec_t b, int *negative) {
1922 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1930 if (negative && s->direction == PA_STREAM_RECORD) {
1938 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative) {
1944 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1947 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1948 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1949 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1950 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1951 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1953 if ((r = pa_stream_get_time(s, &t)) < 0)
1956 if (s->direction == PA_STREAM_PLAYBACK)
1957 cindex = s->timing_info.write_index;
1959 cindex = s->timing_info.read_index;
1964 c = pa_bytes_to_usec((uint64_t) cindex, &s->sample_spec);
1966 if (s->direction == PA_STREAM_PLAYBACK)
1967 *r_usec = time_counter_diff(s, c, t, negative);
1969 *r_usec = time_counter_diff(s, t, c, negative);
1974 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s) {
1976 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1978 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1979 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1980 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->timing_info_valid, PA_ERR_NODATA);
1982 return &s->timing_info;
1985 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s) {
1987 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1989 return &s->sample_spec;
1992 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s) {
1994 pa_assert(PA_REFCNT_VALUE(s) >= 1);
1996 return &s->channel_map;
1999 const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s) {
2001 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2003 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2004 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2005 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 9, PA_ERR_NOTSUPPORTED);
2007 return &s->buffer_attr;
2010 static void stream_set_buffer_attr_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2011 pa_operation *o = userdata;
2016 pa_assert(PA_REFCNT_VALUE(o) >= 1);
2021 if (command != PA_COMMAND_REPLY) {
2022 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
2027 if (o->stream->direction == PA_STREAM_PLAYBACK) {
2028 if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
2029 pa_tagstruct_getu32(t, &o->stream->buffer_attr.tlength) < 0 ||
2030 pa_tagstruct_getu32(t, &o->stream->buffer_attr.prebuf) < 0 ||
2031 pa_tagstruct_getu32(t, &o->stream->buffer_attr.minreq) < 0) {
2032 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2035 } else if (o->stream->direction == PA_STREAM_RECORD) {
2036 if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
2037 pa_tagstruct_getu32(t, &o->stream->buffer_attr.fragsize) < 0) {
2038 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2043 if (o->stream->context->version >= 13) {
2046 if (pa_tagstruct_get_usec(t, &usec) < 0) {
2047 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2051 if (o->stream->direction == PA_STREAM_RECORD)
2052 o->stream->timing_info.configured_source_usec = usec;
2054 o->stream->timing_info.configured_sink_usec = usec;
2057 if (!pa_tagstruct_eof(t)) {
2058 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2064 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
2065 cb(o->stream, success, o->userdata);
2069 pa_operation_done(o);
2070 pa_operation_unref(o);
2074 pa_operation* pa_stream_set_buffer_attr(pa_stream *s, const pa_buffer_attr *attr, pa_stream_success_cb_t cb, void *userdata) {
2080 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2083 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2084 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2085 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2087 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2089 t = pa_tagstruct_command(
2091 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR : PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR),
2093 pa_tagstruct_putu32(t, s->channel);
2095 pa_tagstruct_putu32(t, attr->maxlength);
2097 if (s->direction == PA_STREAM_PLAYBACK)
2100 PA_TAG_U32, attr->tlength,
2101 PA_TAG_U32, attr->prebuf,
2102 PA_TAG_U32, attr->minreq,
2105 pa_tagstruct_putu32(t, attr->fragsize);
2107 if (s->context->version >= 13)
2108 pa_tagstruct_put_boolean(t, !!(s->flags & PA_STREAM_ADJUST_LATENCY));
2110 if (s->context->version >= 14)
2111 pa_tagstruct_put_boolean(t, !!(s->flags & PA_STREAM_EARLY_REQUESTS));
2113 pa_pstream_send_tagstruct(s->context->pstream, t);
2114 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);
2116 /* This might cause changes in the read/write indexex, hence let's
2117 * request a timing update */
2118 request_auto_timing_update(s, TRUE);
2123 uint32_t pa_stream_get_device_index(pa_stream *s) {
2125 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2127 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2128 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2129 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
2130 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->device_index != PA_INVALID_INDEX, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2132 return s->device_index;
2135 const char *pa_stream_get_device_name(pa_stream *s) {
2137 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2139 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2140 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2141 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2142 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->device_name, PA_ERR_BADSTATE);
2144 return s->device_name;
2147 int pa_stream_is_suspended(pa_stream *s) {
2149 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2151 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2152 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2153 PA_CHECK_VALIDITY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2155 return s->suspended;
2158 int pa_stream_is_corked(pa_stream *s) {
2160 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2162 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2163 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2168 static void stream_update_sample_rate_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2169 pa_operation *o = userdata;
2174 pa_assert(PA_REFCNT_VALUE(o) >= 1);
2179 if (command != PA_COMMAND_REPLY) {
2180 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
2186 if (!pa_tagstruct_eof(t)) {
2187 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2192 o->stream->sample_spec.rate = PA_PTR_TO_UINT(o->private);
2193 pa_assert(pa_sample_spec_valid(&o->stream->sample_spec));
2196 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
2197 cb(o->stream, success, o->userdata);
2201 pa_operation_done(o);
2202 pa_operation_unref(o);
2206 pa_operation *pa_stream_update_sample_rate(pa_stream *s, uint32_t rate, pa_stream_success_cb_t cb, void *userdata) {
2212 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2214 PA_CHECK_VALIDITY_RETURN_NULL(s->context, rate > 0 && rate <= PA_RATE_MAX, PA_ERR_INVALID);
2215 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2216 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2217 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->flags & PA_STREAM_VARIABLE_RATE, PA_ERR_BADSTATE);
2218 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2220 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2221 o->private = PA_UINT_TO_PTR(rate);
2223 t = pa_tagstruct_command(
2225 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE : PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE),
2227 pa_tagstruct_putu32(t, s->channel);
2228 pa_tagstruct_putu32(t, rate);
2230 pa_pstream_send_tagstruct(s->context->pstream, t);
2231 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);
2236 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) {
2242 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2244 PA_CHECK_VALIDITY_RETURN_NULL(s->context, mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE, PA_ERR_INVALID);
2245 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2246 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2247 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2249 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2251 t = pa_tagstruct_command(
2253 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST : PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST),
2255 pa_tagstruct_putu32(t, s->channel);
2256 pa_tagstruct_putu32(t, (uint32_t) mode);
2257 pa_tagstruct_put_proplist(t, p);
2259 pa_pstream_send_tagstruct(s->context->pstream, t);
2260 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);
2262 /* Please note that we don't update s->proplist here, because we
2263 * don't export that field */
2268 pa_operation *pa_stream_proplist_remove(pa_stream *s, const char *const keys[], pa_stream_success_cb_t cb, void *userdata) {
2272 const char * const*k;
2275 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2277 PA_CHECK_VALIDITY_RETURN_NULL(s->context, keys && keys[0], PA_ERR_INVALID);
2278 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2279 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2280 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2282 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2284 t = pa_tagstruct_command(
2286 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST : PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST),
2288 pa_tagstruct_putu32(t, s->channel);
2290 for (k = keys; *k; k++)
2291 pa_tagstruct_puts(t, *k);
2293 pa_tagstruct_puts(t, NULL);
2295 pa_pstream_send_tagstruct(s->context->pstream, t);
2296 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);
2298 /* Please note that we don't update s->proplist here, because we
2299 * don't export that field */
2304 int pa_stream_set_monitor_stream(pa_stream *s, uint32_t sink_input_idx) {
2306 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2308 PA_CHECK_VALIDITY(s->context, sink_input_idx != PA_INVALID_INDEX, PA_ERR_INVALID);
2309 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
2310 PA_CHECK_VALIDITY(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2312 s->direct_on_input = sink_input_idx;
2317 uint32_t pa_stream_get_monitor_stream(pa_stream *s) {
2319 pa_assert(PA_REFCNT_VALUE(s) >= 1);
2321 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direct_on_input != PA_INVALID_INDEX, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2322 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
2324 return s->direct_on_input;