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>
35 #include <pulse/fork-detect.h>
37 #include <pulsecore/pstream-util.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/hashmap.h>
40 #include <pulsecore/macro.h>
41 #include <pulsecore/core-rtclock.h>
42 #include <pulsecore/core-util.h>
47 #define AUTO_TIMING_INTERVAL_START_USEC (10*PA_USEC_PER_MSEC)
48 #define AUTO_TIMING_INTERVAL_END_USEC (1500*PA_USEC_PER_MSEC)
50 #define SMOOTHER_ADJUST_TIME (1000*PA_USEC_PER_MSEC)
51 #define SMOOTHER_HISTORY_TIME (5000*PA_USEC_PER_MSEC)
52 #define SMOOTHER_MIN_HISTORY (4)
54 pa_stream
*pa_stream_new(pa_context
*c
, const char *name
, const pa_sample_spec
*ss
, const pa_channel_map
*map
) {
55 return pa_stream_new_with_proplist(c
, name
, ss
, map
, NULL
);
58 static void reset_callbacks(pa_stream
*s
) {
59 s
->read_callback
= NULL
;
60 s
->read_userdata
= NULL
;
61 s
->write_callback
= NULL
;
62 s
->write_userdata
= NULL
;
63 s
->state_callback
= NULL
;
64 s
->state_userdata
= NULL
;
65 s
->overflow_callback
= NULL
;
66 s
->overflow_userdata
= NULL
;
67 s
->underflow_callback
= NULL
;
68 s
->underflow_userdata
= NULL
;
69 s
->latency_update_callback
= NULL
;
70 s
->latency_update_userdata
= NULL
;
71 s
->moved_callback
= NULL
;
72 s
->moved_userdata
= NULL
;
73 s
->suspended_callback
= NULL
;
74 s
->suspended_userdata
= NULL
;
75 s
->started_callback
= NULL
;
76 s
->started_userdata
= NULL
;
77 s
->event_callback
= NULL
;
78 s
->event_userdata
= NULL
;
79 s
->buffer_attr_callback
= NULL
;
80 s
->buffer_attr_userdata
= NULL
;
83 static pa_stream
*pa_stream_new_with_proplist_internal(
86 const pa_sample_spec
*ss
,
87 const pa_channel_map
*map
,
88 pa_format_info
* const *formats
,
89 unsigned int n_formats
,
96 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
97 pa_assert((ss
== NULL
&& map
== NULL
) || (formats
== NULL
&& n_formats
== 0));
98 pa_assert(n_formats
< PA_MAX_FORMATS
);
100 PA_CHECK_VALIDITY_RETURN_NULL(c
, !pa_detect_fork(), PA_ERR_FORKED
);
101 PA_CHECK_VALIDITY_RETURN_NULL(c
, name
|| (p
&& pa_proplist_contains(p
, PA_PROP_MEDIA_NAME
)), PA_ERR_INVALID
);
103 s
= pa_xnew(pa_stream
, 1);
106 s
->mainloop
= c
->mainloop
;
108 s
->direction
= PA_STREAM_NODIRECTION
;
109 s
->state
= PA_STREAM_UNCONNECTED
;
113 s
->sample_spec
= *ss
;
115 s
->sample_spec
.format
= PA_SAMPLE_INVALID
;
118 s
->channel_map
= *map
;
120 pa_channel_map_init(&s
->channel_map
);
124 s
->n_formats
= n_formats
;
125 for (i
= 0; i
< n_formats
; i
++)
126 s
->req_formats
[i
] = pa_format_info_copy(formats
[i
]);
129 /* We'll get the final negotiated format after connecting */
132 s
->direct_on_input
= PA_INVALID_INDEX
;
134 s
->proplist
= p
? pa_proplist_copy(p
) : pa_proplist_new();
136 pa_proplist_sets(s
->proplist
, PA_PROP_MEDIA_NAME
, name
);
139 s
->channel_valid
= FALSE
;
140 s
->syncid
= c
->csyncid
++;
141 s
->stream_index
= PA_INVALID_INDEX
;
143 s
->requested_bytes
= 0;
144 memset(&s
->buffer_attr
, 0, sizeof(s
->buffer_attr
));
146 /* We initialize der target length here, so that if the user
147 * passes no explicit buffering metrics the default is similar to
148 * what older PA versions provided. */
150 s
->buffer_attr
.maxlength
= (uint32_t) -1;
152 s
->buffer_attr
.tlength
= (uint32_t) pa_usec_to_bytes(250*PA_USEC_PER_MSEC
, ss
); /* 250ms of buffering */
154 /* FIXME: We assume a worst-case compressed format corresponding to
155 * 48000 Hz, 2 ch, S16 PCM, but this can very well be incorrect */
156 pa_sample_spec tmp_ss
= {
157 .format
= PA_SAMPLE_S16NE
,
161 s
->buffer_attr
.tlength
= (uint32_t) pa_usec_to_bytes(250*PA_USEC_PER_MSEC
, &tmp_ss
); /* 250ms of buffering */
163 s
->buffer_attr
.minreq
= (uint32_t) -1;
164 s
->buffer_attr
.prebuf
= (uint32_t) -1;
165 s
->buffer_attr
.fragsize
= (uint32_t) -1;
167 s
->device_index
= PA_INVALID_INDEX
;
168 s
->device_name
= NULL
;
169 s
->suspended
= FALSE
;
172 s
->write_memblock
= NULL
;
173 s
->write_data
= NULL
;
175 pa_memchunk_reset(&s
->peek_memchunk
);
177 s
->record_memblockq
= NULL
;
179 memset(&s
->timing_info
, 0, sizeof(s
->timing_info
));
180 s
->timing_info_valid
= FALSE
;
182 s
->previous_time
= 0;
184 s
->read_index_not_before
= 0;
185 s
->write_index_not_before
= 0;
186 for (i
= 0; i
< PA_MAX_WRITE_INDEX_CORRECTIONS
; i
++)
187 s
->write_index_corrections
[i
].valid
= 0;
188 s
->current_write_index_correction
= 0;
190 s
->auto_timing_update_event
= NULL
;
191 s
->auto_timing_update_requested
= FALSE
;
192 s
->auto_timing_interval_usec
= AUTO_TIMING_INTERVAL_START_USEC
;
198 /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
199 PA_LLIST_PREPEND(pa_stream
, c
->streams
, s
);
205 pa_stream
*pa_stream_new_with_proplist(
208 const pa_sample_spec
*ss
,
209 const pa_channel_map
*map
,
214 PA_CHECK_VALIDITY_RETURN_NULL(c
, ss
&& pa_sample_spec_valid(ss
), PA_ERR_INVALID
);
215 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->version
>= 12 || (ss
->format
!= PA_SAMPLE_S32LE
&& ss
->format
!= PA_SAMPLE_S32BE
), PA_ERR_NOTSUPPORTED
);
216 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->version
>= 15 || (ss
->format
!= PA_SAMPLE_S24LE
&& ss
->format
!= PA_SAMPLE_S24BE
), PA_ERR_NOTSUPPORTED
);
217 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->version
>= 15 || (ss
->format
!= PA_SAMPLE_S24_32LE
&& ss
->format
!= PA_SAMPLE_S24_32BE
), PA_ERR_NOTSUPPORTED
);
218 PA_CHECK_VALIDITY_RETURN_NULL(c
, !map
|| (pa_channel_map_valid(map
) && map
->channels
== ss
->channels
), PA_ERR_INVALID
);
221 PA_CHECK_VALIDITY_RETURN_NULL(c
, map
= pa_channel_map_init_auto(&tmap
, ss
->channels
, PA_CHANNEL_MAP_DEFAULT
), PA_ERR_INVALID
);
223 return pa_stream_new_with_proplist_internal(c
, name
, ss
, map
, NULL
, 0, p
);
226 pa_stream
*pa_stream_new_extended(
229 pa_format_info
* const *formats
,
230 unsigned int n_formats
,
233 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->version
>= 21, PA_ERR_NOTSUPPORTED
);
235 return pa_stream_new_with_proplist_internal(c
, name
, NULL
, NULL
, formats
, n_formats
, p
);
238 static void stream_unlink(pa_stream
*s
) {
245 /* Detach from context */
247 /* Unref all operatio object that point to us */
248 for (o
= s
->context
->operations
; o
; o
= n
) {
252 pa_operation_cancel(o
);
255 /* Drop all outstanding replies for this stream */
256 if (s
->context
->pdispatch
)
257 pa_pdispatch_unregister_reply(s
->context
->pdispatch
, s
);
259 if (s
->channel_valid
) {
260 pa_hashmap_remove((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, PA_UINT32_TO_PTR(s
->channel
));
262 s
->channel_valid
= FALSE
;
265 PA_LLIST_REMOVE(pa_stream
, s
->context
->streams
, s
);
270 if (s
->auto_timing_update_event
) {
271 pa_assert(s
->mainloop
);
272 s
->mainloop
->time_free(s
->auto_timing_update_event
);
278 static void stream_free(pa_stream
*s
) {
285 if (s
->write_memblock
) {
286 pa_memblock_release(s
->write_memblock
);
287 pa_memblock_unref(s
->write_data
);
290 if (s
->peek_memchunk
.memblock
) {
292 pa_memblock_release(s
->peek_memchunk
.memblock
);
293 pa_memblock_unref(s
->peek_memchunk
.memblock
);
296 if (s
->record_memblockq
)
297 pa_memblockq_free(s
->record_memblockq
);
300 pa_proplist_free(s
->proplist
);
303 pa_smoother_free(s
->smoother
);
305 for (i
= 0; i
< s
->n_formats
; i
++)
306 pa_format_info_free(s
->req_formats
[i
]);
309 pa_format_info_free(s
->format
);
311 pa_xfree(s
->device_name
);
315 void pa_stream_unref(pa_stream
*s
) {
317 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
319 if (PA_REFCNT_DEC(s
) <= 0)
323 pa_stream
* pa_stream_ref(pa_stream
*s
) {
325 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
331 pa_stream_state_t
pa_stream_get_state(pa_stream
*s
) {
333 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
338 pa_context
* pa_stream_get_context(pa_stream
*s
) {
340 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
345 uint32_t pa_stream_get_index(pa_stream
*s
) {
347 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
349 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
, PA_INVALID_INDEX
);
350 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
352 return s
->stream_index
;
355 void pa_stream_set_state(pa_stream
*s
, pa_stream_state_t st
) {
357 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
366 if (s
->state_callback
)
367 s
->state_callback(s
, s
->state_userdata
);
369 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
))
375 static void request_auto_timing_update(pa_stream
*s
, pa_bool_t force
) {
377 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
379 if (!(s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
))
382 if (s
->state
== PA_STREAM_READY
&&
383 (force
|| !s
->auto_timing_update_requested
)) {
386 /* pa_log("Automatically requesting new timing data"); */
388 if ((o
= pa_stream_update_timing_info(s
, NULL
, NULL
))) {
389 pa_operation_unref(o
);
390 s
->auto_timing_update_requested
= TRUE
;
394 if (s
->auto_timing_update_event
) {
395 if (s
->suspended
&& !force
) {
396 pa_assert(s
->mainloop
);
397 s
->mainloop
->time_free(s
->auto_timing_update_event
);
398 s
->auto_timing_update_event
= NULL
;
401 s
->auto_timing_interval_usec
= AUTO_TIMING_INTERVAL_START_USEC
;
403 pa_context_rttime_restart(s
->context
, s
->auto_timing_update_event
, pa_rtclock_now() + s
->auto_timing_interval_usec
);
405 s
->auto_timing_interval_usec
= PA_MIN(AUTO_TIMING_INTERVAL_END_USEC
, s
->auto_timing_interval_usec
*2);
410 void pa_command_stream_killed(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
411 pa_context
*c
= userdata
;
416 pa_assert(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
);
419 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
423 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
424 !pa_tagstruct_eof(t
)) {
425 pa_context_fail(c
, PA_ERR_PROTOCOL
);
429 if (!(s
= pa_hashmap_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, PA_UINT32_TO_PTR(channel
))))
432 if (s
->state
!= PA_STREAM_READY
)
435 pa_context_set_error(c
, PA_ERR_KILLED
);
436 pa_stream_set_state(s
, PA_STREAM_FAILED
);
442 static void check_smoother_status(pa_stream
*s
, pa_bool_t aposteriori
, pa_bool_t force_start
, pa_bool_t force_stop
) {
446 pa_assert(!force_start
|| !force_stop
);
451 x
= pa_rtclock_now();
453 if (s
->timing_info_valid
) {
455 x
-= s
->timing_info
.transport_usec
;
457 x
+= s
->timing_info
.transport_usec
;
460 if (s
->suspended
|| s
->corked
|| force_stop
)
461 pa_smoother_pause(s
->smoother
, x
);
462 else if (force_start
|| s
->buffer_attr
.prebuf
== 0) {
464 if (!s
->timing_info_valid
&&
468 s
->context
->version
>= 13) {
470 /* If the server supports STARTED events we take them as
471 * indications when audio really starts/stops playing, if
472 * we don't have any timing info yet -- instead of trying
473 * to be smart and guessing the server time. Otherwise the
474 * unknown transport delay add too much noise to our time
480 pa_smoother_resume(s
->smoother
, x
, TRUE
);
483 /* Please note that we have no idea if playback actually started
484 * if prebuf is non-zero! */
487 static void auto_timing_update_callback(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*t
, void *userdata
);
489 void pa_command_stream_moved(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
490 pa_context
*c
= userdata
;
497 uint32_t maxlength
= 0, fragsize
= 0, minreq
= 0, tlength
= 0, prebuf
= 0;
500 pa_assert(command
== PA_COMMAND_PLAYBACK_STREAM_MOVED
|| command
== PA_COMMAND_RECORD_STREAM_MOVED
);
503 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
507 if (c
->version
< 12) {
508 pa_context_fail(c
, PA_ERR_PROTOCOL
);
512 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
513 pa_tagstruct_getu32(t
, &di
) < 0 ||
514 pa_tagstruct_gets(t
, &dn
) < 0 ||
515 pa_tagstruct_get_boolean(t
, &suspended
) < 0) {
516 pa_context_fail(c
, PA_ERR_PROTOCOL
);
520 if (c
->version
>= 13) {
522 if (command
== PA_COMMAND_RECORD_STREAM_MOVED
) {
523 if (pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
524 pa_tagstruct_getu32(t
, &fragsize
) < 0 ||
525 pa_tagstruct_get_usec(t
, &usec
) < 0) {
526 pa_context_fail(c
, PA_ERR_PROTOCOL
);
530 if (pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
531 pa_tagstruct_getu32(t
, &tlength
) < 0 ||
532 pa_tagstruct_getu32(t
, &prebuf
) < 0 ||
533 pa_tagstruct_getu32(t
, &minreq
) < 0 ||
534 pa_tagstruct_get_usec(t
, &usec
) < 0) {
535 pa_context_fail(c
, PA_ERR_PROTOCOL
);
541 if (!pa_tagstruct_eof(t
)) {
542 pa_context_fail(c
, PA_ERR_PROTOCOL
);
546 if (!dn
|| di
== PA_INVALID_INDEX
) {
547 pa_context_fail(c
, PA_ERR_PROTOCOL
);
551 if (!(s
= pa_hashmap_get(command
== PA_COMMAND_PLAYBACK_STREAM_MOVED
? c
->playback_streams
: c
->record_streams
, PA_UINT32_TO_PTR(channel
))))
554 if (s
->state
!= PA_STREAM_READY
)
557 if (c
->version
>= 13) {
558 if (s
->direction
== PA_STREAM_RECORD
)
559 s
->timing_info
.configured_source_usec
= usec
;
561 s
->timing_info
.configured_sink_usec
= usec
;
563 s
->buffer_attr
.maxlength
= maxlength
;
564 s
->buffer_attr
.fragsize
= fragsize
;
565 s
->buffer_attr
.tlength
= tlength
;
566 s
->buffer_attr
.prebuf
= prebuf
;
567 s
->buffer_attr
.minreq
= minreq
;
570 pa_xfree(s
->device_name
);
571 s
->device_name
= pa_xstrdup(dn
);
572 s
->device_index
= di
;
574 s
->suspended
= suspended
;
576 if ((s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) && !suspended
&& !s
->auto_timing_update_event
) {
577 s
->auto_timing_interval_usec
= AUTO_TIMING_INTERVAL_START_USEC
;
578 s
->auto_timing_update_event
= pa_context_rttime_new(s
->context
, pa_rtclock_now() + s
->auto_timing_interval_usec
, &auto_timing_update_callback
, s
);
579 request_auto_timing_update(s
, TRUE
);
582 check_smoother_status(s
, TRUE
, FALSE
, FALSE
);
583 request_auto_timing_update(s
, TRUE
);
585 if (s
->moved_callback
)
586 s
->moved_callback(s
, s
->moved_userdata
);
592 void pa_command_stream_buffer_attr(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
593 pa_context
*c
= userdata
;
597 uint32_t maxlength
= 0, fragsize
= 0, minreq
= 0, tlength
= 0, prebuf
= 0;
600 pa_assert(command
== PA_COMMAND_PLAYBACK_BUFFER_ATTR_CHANGED
|| command
== PA_COMMAND_RECORD_BUFFER_ATTR_CHANGED
);
603 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
607 if (c
->version
< 15) {
608 pa_context_fail(c
, PA_ERR_PROTOCOL
);
612 if (pa_tagstruct_getu32(t
, &channel
) < 0) {
613 pa_context_fail(c
, PA_ERR_PROTOCOL
);
617 if (command
== PA_COMMAND_RECORD_STREAM_MOVED
) {
618 if (pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
619 pa_tagstruct_getu32(t
, &fragsize
) < 0 ||
620 pa_tagstruct_get_usec(t
, &usec
) < 0) {
621 pa_context_fail(c
, PA_ERR_PROTOCOL
);
625 if (pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
626 pa_tagstruct_getu32(t
, &tlength
) < 0 ||
627 pa_tagstruct_getu32(t
, &prebuf
) < 0 ||
628 pa_tagstruct_getu32(t
, &minreq
) < 0 ||
629 pa_tagstruct_get_usec(t
, &usec
) < 0) {
630 pa_context_fail(c
, PA_ERR_PROTOCOL
);
635 if (!pa_tagstruct_eof(t
)) {
636 pa_context_fail(c
, PA_ERR_PROTOCOL
);
640 if (!(s
= pa_hashmap_get(command
== PA_COMMAND_PLAYBACK_BUFFER_ATTR_CHANGED
? c
->playback_streams
: c
->record_streams
, PA_UINT32_TO_PTR(channel
))))
643 if (s
->state
!= PA_STREAM_READY
)
646 if (s
->direction
== PA_STREAM_RECORD
)
647 s
->timing_info
.configured_source_usec
= usec
;
649 s
->timing_info
.configured_sink_usec
= usec
;
651 s
->buffer_attr
.maxlength
= maxlength
;
652 s
->buffer_attr
.fragsize
= fragsize
;
653 s
->buffer_attr
.tlength
= tlength
;
654 s
->buffer_attr
.prebuf
= prebuf
;
655 s
->buffer_attr
.minreq
= minreq
;
657 request_auto_timing_update(s
, TRUE
);
659 if (s
->buffer_attr_callback
)
660 s
->buffer_attr_callback(s
, s
->buffer_attr_userdata
);
666 void pa_command_stream_suspended(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
667 pa_context
*c
= userdata
;
673 pa_assert(command
== PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
|| command
== PA_COMMAND_RECORD_STREAM_SUSPENDED
);
676 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
680 if (c
->version
< 12) {
681 pa_context_fail(c
, PA_ERR_PROTOCOL
);
685 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
686 pa_tagstruct_get_boolean(t
, &suspended
) < 0 ||
687 !pa_tagstruct_eof(t
)) {
688 pa_context_fail(c
, PA_ERR_PROTOCOL
);
692 if (!(s
= pa_hashmap_get(command
== PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
? c
->playback_streams
: c
->record_streams
, PA_UINT32_TO_PTR(channel
))))
695 if (s
->state
!= PA_STREAM_READY
)
698 s
->suspended
= suspended
;
700 if ((s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) && !suspended
&& !s
->auto_timing_update_event
) {
701 s
->auto_timing_interval_usec
= AUTO_TIMING_INTERVAL_START_USEC
;
702 s
->auto_timing_update_event
= pa_context_rttime_new(s
->context
, pa_rtclock_now() + s
->auto_timing_interval_usec
, &auto_timing_update_callback
, s
);
703 request_auto_timing_update(s
, TRUE
);
706 check_smoother_status(s
, TRUE
, FALSE
, FALSE
);
707 request_auto_timing_update(s
, TRUE
);
709 if (s
->suspended_callback
)
710 s
->suspended_callback(s
, s
->suspended_userdata
);
716 void pa_command_stream_started(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
717 pa_context
*c
= userdata
;
722 pa_assert(command
== PA_COMMAND_STARTED
);
725 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
729 if (c
->version
< 13) {
730 pa_context_fail(c
, PA_ERR_PROTOCOL
);
734 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
735 !pa_tagstruct_eof(t
)) {
736 pa_context_fail(c
, PA_ERR_PROTOCOL
);
740 if (!(s
= pa_hashmap_get(c
->playback_streams
, PA_UINT32_TO_PTR(channel
))))
743 if (s
->state
!= PA_STREAM_READY
)
746 check_smoother_status(s
, TRUE
, TRUE
, FALSE
);
747 request_auto_timing_update(s
, TRUE
);
749 if (s
->started_callback
)
750 s
->started_callback(s
, s
->started_userdata
);
756 void pa_command_stream_event(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
757 pa_context
*c
= userdata
;
760 pa_proplist
*pl
= NULL
;
764 pa_assert(command
== PA_COMMAND_PLAYBACK_STREAM_EVENT
|| command
== PA_COMMAND_RECORD_STREAM_EVENT
);
767 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
771 if (c
->version
< 15) {
772 pa_context_fail(c
, PA_ERR_PROTOCOL
);
776 pl
= pa_proplist_new();
778 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
779 pa_tagstruct_gets(t
, &event
) < 0 ||
780 pa_tagstruct_get_proplist(t
, pl
) < 0 ||
781 !pa_tagstruct_eof(t
) || !event
) {
782 pa_context_fail(c
, PA_ERR_PROTOCOL
);
786 if (!(s
= pa_hashmap_get(command
== PA_COMMAND_PLAYBACK_STREAM_EVENT
? c
->playback_streams
: c
->record_streams
, PA_UINT32_TO_PTR(channel
))))
789 if (s
->state
!= PA_STREAM_READY
)
792 if (pa_streq(event
, PA_STREAM_EVENT_FORMAT_LOST
)) {
793 /* Let client know what the running time was when the stream had to be
796 if (pa_stream_get_time(s
, &time
) == 0)
797 pa_proplist_setf(pl
, "stream-time", "%llu", (unsigned long long) time
);
800 if (s
->event_callback
)
801 s
->event_callback(s
, event
, pl
, s
->event_userdata
);
807 pa_proplist_free(pl
);
810 void pa_command_request(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
812 pa_context
*c
= userdata
;
813 uint32_t bytes
, channel
;
816 pa_assert(command
== PA_COMMAND_REQUEST
);
819 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
823 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
824 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
825 !pa_tagstruct_eof(t
)) {
826 pa_context_fail(c
, PA_ERR_PROTOCOL
);
830 if (!(s
= pa_hashmap_get(c
->playback_streams
, PA_UINT32_TO_PTR(channel
))))
833 if (s
->state
!= PA_STREAM_READY
)
836 s
->requested_bytes
+= bytes
;
838 /* pa_log("got request for %lli, now at %lli", (long long) bytes, (long long) s->requested_bytes); */
840 if (s
->requested_bytes
> 0 && s
->write_callback
)
841 s
->write_callback(s
, (size_t) s
->requested_bytes
, s
->write_userdata
);
847 void pa_command_overflow_or_underflow(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
849 pa_context
*c
= userdata
;
853 pa_assert(command
== PA_COMMAND_OVERFLOW
|| command
== PA_COMMAND_UNDERFLOW
);
856 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
860 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
861 !pa_tagstruct_eof(t
)) {
862 pa_context_fail(c
, PA_ERR_PROTOCOL
);
866 if (!(s
= pa_hashmap_get(c
->playback_streams
, PA_UINT32_TO_PTR(channel
))))
869 if (s
->state
!= PA_STREAM_READY
)
872 if (s
->buffer_attr
.prebuf
> 0)
873 check_smoother_status(s
, TRUE
, FALSE
, TRUE
);
875 request_auto_timing_update(s
, TRUE
);
877 if (command
== PA_COMMAND_OVERFLOW
) {
878 if (s
->overflow_callback
)
879 s
->overflow_callback(s
, s
->overflow_userdata
);
880 } else if (command
== PA_COMMAND_UNDERFLOW
) {
881 if (s
->underflow_callback
)
882 s
->underflow_callback(s
, s
->underflow_userdata
);
889 static void invalidate_indexes(pa_stream
*s
, pa_bool_t r
, pa_bool_t w
) {
891 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
893 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
895 if (s
->state
!= PA_STREAM_READY
)
899 s
->write_index_not_before
= s
->context
->ctag
;
901 if (s
->timing_info_valid
)
902 s
->timing_info
.write_index_corrupt
= TRUE
;
904 /* pa_log("write_index invalidated"); */
908 s
->read_index_not_before
= s
->context
->ctag
;
910 if (s
->timing_info_valid
)
911 s
->timing_info
.read_index_corrupt
= TRUE
;
913 /* pa_log("read_index invalidated"); */
916 request_auto_timing_update(s
, TRUE
);
919 static void auto_timing_update_callback(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*t
, void *userdata
) {
920 pa_stream
*s
= userdata
;
923 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
926 request_auto_timing_update(s
, FALSE
);
930 static void create_stream_complete(pa_stream
*s
) {
932 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
933 pa_assert(s
->state
== PA_STREAM_CREATING
);
935 pa_stream_set_state(s
, PA_STREAM_READY
);
937 if (s
->requested_bytes
> 0 && s
->write_callback
)
938 s
->write_callback(s
, (size_t) s
->requested_bytes
, s
->write_userdata
);
940 if (s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) {
941 s
->auto_timing_interval_usec
= AUTO_TIMING_INTERVAL_START_USEC
;
942 pa_assert(!s
->auto_timing_update_event
);
943 s
->auto_timing_update_event
= pa_context_rttime_new(s
->context
, pa_rtclock_now() + s
->auto_timing_interval_usec
, &auto_timing_update_callback
, s
);
945 request_auto_timing_update(s
, TRUE
);
948 check_smoother_status(s
, TRUE
, FALSE
, FALSE
);
951 static void patch_buffer_attr(pa_stream
*s
, pa_buffer_attr
*attr
, pa_stream_flags_t
*flags
) {
957 if ((e
= getenv("PULSE_LATENCY_MSEC"))) {
960 if (pa_atou(e
, &ms
) < 0 || ms
<= 0)
961 pa_log_debug("Failed to parse $PULSE_LATENCY_MSEC: %s", e
);
963 attr
->maxlength
= (uint32_t) -1;
964 attr
->tlength
= pa_usec_to_bytes(ms
* PA_USEC_PER_MSEC
, &s
->sample_spec
);
965 attr
->minreq
= (uint32_t) -1;
966 attr
->prebuf
= (uint32_t) -1;
967 attr
->fragsize
= attr
->tlength
;
971 *flags
|= PA_STREAM_ADJUST_LATENCY
;
974 if (s
->context
->version
>= 13)
977 /* Version older than 0.9.10 didn't do server side buffer_attr
978 * selection, hence we have to fake it on the client side. */
980 /* We choose fairly conservative values here, to not confuse
981 * old clients with extremely large playback buffers */
983 if (attr
->maxlength
== (uint32_t) -1)
984 attr
->maxlength
= 4*1024*1024; /* 4MB is the maximum queue length PulseAudio <= 0.9.9 supported. */
986 if (attr
->tlength
== (uint32_t) -1)
987 attr
->tlength
= (uint32_t) pa_usec_to_bytes(250*PA_USEC_PER_MSEC
, &s
->sample_spec
); /* 250ms of buffering */
989 if (attr
->minreq
== (uint32_t) -1)
990 attr
->minreq
= (attr
->tlength
)/5; /* Ask for more data when there are only 200ms left in the playback buffer */
992 if (attr
->prebuf
== (uint32_t) -1)
993 attr
->prebuf
= attr
->tlength
; /* Start to play only when the playback is fully filled up once */
995 if (attr
->fragsize
== (uint32_t) -1)
996 attr
->fragsize
= attr
->tlength
; /* Pass data to the app only when the buffer is filled up once */
999 void pa_create_stream_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1000 pa_stream
*s
= userdata
;
1001 uint32_t requested_bytes
= 0;
1005 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1006 pa_assert(s
->state
== PA_STREAM_CREATING
);
1010 if (command
!= PA_COMMAND_REPLY
) {
1011 if (pa_context_handle_error(s
->context
, command
, t
, FALSE
) < 0)
1014 pa_stream_set_state(s
, PA_STREAM_FAILED
);
1018 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
1019 s
->channel
== PA_INVALID_INDEX
||
1020 ((s
->direction
!= PA_STREAM_UPLOAD
) && (pa_tagstruct_getu32(t
, &s
->stream_index
) < 0 || s
->stream_index
== PA_INVALID_INDEX
)) ||
1021 ((s
->direction
!= PA_STREAM_RECORD
) && pa_tagstruct_getu32(t
, &requested_bytes
) < 0)) {
1022 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
1026 s
->requested_bytes
= (int64_t) requested_bytes
;
1028 if (s
->context
->version
>= 9) {
1029 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1030 if (pa_tagstruct_getu32(t
, &s
->buffer_attr
.maxlength
) < 0 ||
1031 pa_tagstruct_getu32(t
, &s
->buffer_attr
.tlength
) < 0 ||
1032 pa_tagstruct_getu32(t
, &s
->buffer_attr
.prebuf
) < 0 ||
1033 pa_tagstruct_getu32(t
, &s
->buffer_attr
.minreq
) < 0) {
1034 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
1037 } else if (s
->direction
== PA_STREAM_RECORD
) {
1038 if (pa_tagstruct_getu32(t
, &s
->buffer_attr
.maxlength
) < 0 ||
1039 pa_tagstruct_getu32(t
, &s
->buffer_attr
.fragsize
) < 0) {
1040 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
1046 if (s
->context
->version
>= 12 && s
->direction
!= PA_STREAM_UPLOAD
) {
1049 const char *dn
= NULL
;
1050 pa_bool_t suspended
;
1052 if (pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
1053 pa_tagstruct_get_channel_map(t
, &cm
) < 0 ||
1054 pa_tagstruct_getu32(t
, &s
->device_index
) < 0 ||
1055 pa_tagstruct_gets(t
, &dn
) < 0 ||
1056 pa_tagstruct_get_boolean(t
, &suspended
) < 0) {
1057 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
1061 if (!dn
|| s
->device_index
== PA_INVALID_INDEX
||
1062 ss
.channels
!= cm
.channels
||
1063 !pa_channel_map_valid(&cm
) ||
1064 !pa_sample_spec_valid(&ss
) ||
1065 (s
->n_formats
== 0 && (
1066 (!(s
->flags
& PA_STREAM_FIX_FORMAT
) && ss
.format
!= s
->sample_spec
.format
) ||
1067 (!(s
->flags
& PA_STREAM_FIX_RATE
) && ss
.rate
!= s
->sample_spec
.rate
) ||
1068 (!(s
->flags
& PA_STREAM_FIX_CHANNELS
) && !pa_channel_map_equal(&cm
, &s
->channel_map
))))) {
1069 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
1073 pa_xfree(s
->device_name
);
1074 s
->device_name
= pa_xstrdup(dn
);
1075 s
->suspended
= suspended
;
1077 s
->channel_map
= cm
;
1078 s
->sample_spec
= ss
;
1081 if (s
->context
->version
>= 13 && s
->direction
!= PA_STREAM_UPLOAD
) {
1084 if (pa_tagstruct_get_usec(t
, &usec
) < 0) {
1085 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
1089 if (s
->direction
== PA_STREAM_RECORD
)
1090 s
->timing_info
.configured_source_usec
= usec
;
1092 s
->timing_info
.configured_sink_usec
= usec
;
1095 if ((s
->context
->version
>= 21 && s
->direction
== PA_STREAM_PLAYBACK
)
1096 || s
->context
->version
>= 22) {
1098 pa_format_info
*f
= pa_format_info_new();
1099 pa_tagstruct_get_format_info(t
, f
);
1101 if (pa_format_info_valid(f
))
1104 pa_format_info_free(f
);
1105 if (s
->n_formats
> 0) {
1106 /* We used the extended API, so we should have got back a proper format */
1107 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
1113 if (!pa_tagstruct_eof(t
)) {
1114 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
1118 if (s
->direction
== PA_STREAM_RECORD
) {
1119 pa_assert(!s
->record_memblockq
);
1121 s
->record_memblockq
= pa_memblockq_new(
1123 s
->buffer_attr
.maxlength
,
1125 pa_frame_size(&s
->sample_spec
),
1132 s
->channel_valid
= TRUE
;
1133 pa_hashmap_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, PA_UINT32_TO_PTR(s
->channel
), s
);
1135 create_stream_complete(s
);
1141 static int create_stream(
1142 pa_stream_direction_t direction
,
1145 const pa_buffer_attr
*attr
,
1146 pa_stream_flags_t flags
,
1147 const pa_cvolume
*volume
,
1148 pa_stream
*sync_stream
) {
1152 pa_bool_t volume_set
= !!volume
;
1157 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1158 pa_assert(direction
== PA_STREAM_PLAYBACK
|| direction
== PA_STREAM_RECORD
);
1160 PA_CHECK_VALIDITY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
1161 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_UNCONNECTED
, PA_ERR_BADSTATE
);
1162 PA_CHECK_VALIDITY(s
->context
, s
->direct_on_input
== PA_INVALID_INDEX
|| direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
1163 PA_CHECK_VALIDITY(s
->context
, !(flags
& ~(PA_STREAM_START_CORKED
|
1164 PA_STREAM_INTERPOLATE_TIMING
|
1165 PA_STREAM_NOT_MONOTONIC
|
1166 PA_STREAM_AUTO_TIMING_UPDATE
|
1167 PA_STREAM_NO_REMAP_CHANNELS
|
1168 PA_STREAM_NO_REMIX_CHANNELS
|
1169 PA_STREAM_FIX_FORMAT
|
1171 PA_STREAM_FIX_CHANNELS
|
1172 PA_STREAM_DONT_MOVE
|
1173 PA_STREAM_VARIABLE_RATE
|
1174 PA_STREAM_PEAK_DETECT
|
1175 PA_STREAM_START_MUTED
|
1176 PA_STREAM_ADJUST_LATENCY
|
1177 PA_STREAM_EARLY_REQUESTS
|
1178 PA_STREAM_DONT_INHIBIT_AUTO_SUSPEND
|
1179 PA_STREAM_START_UNMUTED
|
1180 PA_STREAM_FAIL_ON_SUSPEND
|
1181 PA_STREAM_RELATIVE_VOLUME
|
1182 PA_STREAM_PASSTHROUGH
)), PA_ERR_INVALID
);
1185 PA_CHECK_VALIDITY(s
->context
, s
->context
->version
>= 12 || !(flags
& PA_STREAM_VARIABLE_RATE
), PA_ERR_NOTSUPPORTED
);
1186 PA_CHECK_VALIDITY(s
->context
, s
->context
->version
>= 13 || !(flags
& PA_STREAM_PEAK_DETECT
), PA_ERR_NOTSUPPORTED
);
1187 PA_CHECK_VALIDITY(s
->context
, s
->context
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
1188 /* Althought some of the other flags are not supported on older
1189 * version, we don't check for them here, because it doesn't hurt
1190 * when they are passed but actually not supported. This makes
1191 * client development easier */
1193 PA_CHECK_VALIDITY(s
->context
, direction
== PA_STREAM_PLAYBACK
|| !(flags
& (PA_STREAM_START_MUTED
)), PA_ERR_INVALID
);
1194 PA_CHECK_VALIDITY(s
->context
, direction
== PA_STREAM_RECORD
|| !(flags
& (PA_STREAM_PEAK_DETECT
)), PA_ERR_INVALID
);
1195 PA_CHECK_VALIDITY(s
->context
, !volume
|| (pa_sample_spec_valid(&s
->sample_spec
) && volume
->channels
== s
->sample_spec
.channels
), PA_ERR_INVALID
);
1196 PA_CHECK_VALIDITY(s
->context
, !sync_stream
|| (direction
== PA_STREAM_PLAYBACK
&& sync_stream
->direction
== PA_STREAM_PLAYBACK
), PA_ERR_INVALID
);
1197 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
);
1201 s
->direction
= direction
;
1204 s
->syncid
= sync_stream
->syncid
;
1207 s
->buffer_attr
= *attr
;
1208 patch_buffer_attr(s
, &s
->buffer_attr
, &flags
);
1211 s
->corked
= !!(flags
& PA_STREAM_START_CORKED
);
1213 if (flags
& PA_STREAM_INTERPOLATE_TIMING
) {
1216 x
= pa_rtclock_now();
1218 pa_assert(!s
->smoother
);
1219 s
->smoother
= pa_smoother_new(
1220 SMOOTHER_ADJUST_TIME
,
1221 SMOOTHER_HISTORY_TIME
,
1222 !(flags
& PA_STREAM_NOT_MONOTONIC
),
1224 SMOOTHER_MIN_HISTORY
,
1230 dev
= s
->direction
== PA_STREAM_PLAYBACK
? s
->context
->conf
->default_sink
: s
->context
->conf
->default_source
;
1232 t
= pa_tagstruct_command(
1234 (uint32_t) (s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
),
1237 if (s
->context
->version
< 13)
1238 pa_tagstruct_puts(t
, pa_proplist_gets(s
->proplist
, PA_PROP_MEDIA_NAME
));
1242 PA_TAG_SAMPLE_SPEC
, &s
->sample_spec
,
1243 PA_TAG_CHANNEL_MAP
, &s
->channel_map
,
1244 PA_TAG_U32
, PA_INVALID_INDEX
,
1246 PA_TAG_U32
, s
->buffer_attr
.maxlength
,
1247 PA_TAG_BOOLEAN
, s
->corked
,
1251 if (pa_sample_spec_valid(&s
->sample_spec
))
1252 volume
= pa_cvolume_reset(&cv
, s
->sample_spec
.channels
);
1254 /* This is not really relevant, since no volume was set, and
1255 * the real number of channels is embedded in the format_info
1257 volume
= pa_cvolume_reset(&cv
, PA_CHANNELS_MAX
);
1261 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1264 PA_TAG_U32
, s
->buffer_attr
.tlength
,
1265 PA_TAG_U32
, s
->buffer_attr
.prebuf
,
1266 PA_TAG_U32
, s
->buffer_attr
.minreq
,
1267 PA_TAG_U32
, s
->syncid
,
1270 pa_tagstruct_put_cvolume(t
, volume
);
1272 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
1274 if (s
->context
->version
>= 12) {
1277 PA_TAG_BOOLEAN
, flags
& PA_STREAM_NO_REMAP_CHANNELS
,
1278 PA_TAG_BOOLEAN
, flags
& PA_STREAM_NO_REMIX_CHANNELS
,
1279 PA_TAG_BOOLEAN
, flags
& PA_STREAM_FIX_FORMAT
,
1280 PA_TAG_BOOLEAN
, flags
& PA_STREAM_FIX_RATE
,
1281 PA_TAG_BOOLEAN
, flags
& PA_STREAM_FIX_CHANNELS
,
1282 PA_TAG_BOOLEAN
, flags
& PA_STREAM_DONT_MOVE
,
1283 PA_TAG_BOOLEAN
, flags
& PA_STREAM_VARIABLE_RATE
,
1287 if (s
->context
->version
>= 13) {
1289 if (s
->direction
== PA_STREAM_PLAYBACK
)
1290 pa_tagstruct_put_boolean(t
, flags
& PA_STREAM_START_MUTED
);
1292 pa_tagstruct_put_boolean(t
, flags
& PA_STREAM_PEAK_DETECT
);
1296 PA_TAG_BOOLEAN
, flags
& PA_STREAM_ADJUST_LATENCY
,
1297 PA_TAG_PROPLIST
, s
->proplist
,
1300 if (s
->direction
== PA_STREAM_RECORD
)
1301 pa_tagstruct_putu32(t
, s
->direct_on_input
);
1304 if (s
->context
->version
>= 14) {
1306 if (s
->direction
== PA_STREAM_PLAYBACK
)
1307 pa_tagstruct_put_boolean(t
, volume_set
);
1309 pa_tagstruct_put_boolean(t
, flags
& PA_STREAM_EARLY_REQUESTS
);
1312 if (s
->context
->version
>= 15) {
1314 if (s
->direction
== PA_STREAM_PLAYBACK
)
1315 pa_tagstruct_put_boolean(t
, flags
& (PA_STREAM_START_MUTED
|PA_STREAM_START_UNMUTED
));
1317 pa_tagstruct_put_boolean(t
, flags
& PA_STREAM_DONT_INHIBIT_AUTO_SUSPEND
);
1318 pa_tagstruct_put_boolean(t
, flags
& PA_STREAM_FAIL_ON_SUSPEND
);
1321 if (s
->context
->version
>= 17 && s
->direction
== PA_STREAM_PLAYBACK
)
1322 pa_tagstruct_put_boolean(t
, flags
& PA_STREAM_RELATIVE_VOLUME
);
1324 if (s
->context
->version
>= 18 && s
->direction
== PA_STREAM_PLAYBACK
)
1325 pa_tagstruct_put_boolean(t
, flags
& (PA_STREAM_PASSTHROUGH
));
1327 if ((s
->context
->version
>= 21 && s
->direction
== PA_STREAM_PLAYBACK
)
1328 || s
->context
->version
>= 22) {
1330 pa_tagstruct_putu8(t
, s
->n_formats
);
1331 for (i
= 0; i
< s
->n_formats
; i
++)
1332 pa_tagstruct_put_format_info(t
, s
->req_formats
[i
]);
1335 if (s
->context
->version
>= 22 && s
->direction
== PA_STREAM_RECORD
) {
1336 pa_tagstruct_put_cvolume(t
, volume
);
1337 pa_tagstruct_put_boolean(t
, flags
& PA_STREAM_START_MUTED
);
1338 pa_tagstruct_put_boolean(t
, volume_set
);
1339 pa_tagstruct_put_boolean(t
, flags
& (PA_STREAM_START_MUTED
|PA_STREAM_START_UNMUTED
));
1340 pa_tagstruct_put_boolean(t
, flags
& PA_STREAM_RELATIVE_VOLUME
);
1341 pa_tagstruct_put_boolean(t
, flags
& (PA_STREAM_PASSTHROUGH
));
1344 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1345 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
, NULL
);
1347 pa_stream_set_state(s
, PA_STREAM_CREATING
);
1353 int pa_stream_connect_playback(
1356 const pa_buffer_attr
*attr
,
1357 pa_stream_flags_t flags
,
1358 const pa_cvolume
*volume
,
1359 pa_stream
*sync_stream
) {
1362 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1364 return create_stream(PA_STREAM_PLAYBACK
, s
, dev
, attr
, flags
, volume
, sync_stream
);
1367 int pa_stream_connect_record(
1370 const pa_buffer_attr
*attr
,
1371 pa_stream_flags_t flags
) {
1374 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1376 return create_stream(PA_STREAM_RECORD
, s
, dev
, attr
, flags
, NULL
, NULL
);
1379 int pa_stream_begin_write(
1385 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1387 PA_CHECK_VALIDITY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
1388 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1389 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| s
->direction
== PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1390 PA_CHECK_VALIDITY(s
->context
, data
, PA_ERR_INVALID
);
1391 PA_CHECK_VALIDITY(s
->context
, nbytes
&& *nbytes
!= 0, PA_ERR_INVALID
);
1393 if (*nbytes
!= (size_t) -1) {
1396 m
= pa_mempool_block_size_max(s
->context
->mempool
);
1397 fs
= pa_frame_size(&s
->sample_spec
);
1404 if (!s
->write_memblock
) {
1405 s
->write_memblock
= pa_memblock_new(s
->context
->mempool
, *nbytes
);
1406 s
->write_data
= pa_memblock_acquire(s
->write_memblock
);
1409 *data
= s
->write_data
;
1410 *nbytes
= pa_memblock_get_length(s
->write_memblock
);
1415 int pa_stream_cancel_write(
1419 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1421 PA_CHECK_VALIDITY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
1422 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1423 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| s
->direction
== PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1424 PA_CHECK_VALIDITY(s
->context
, s
->write_memblock
, PA_ERR_BADSTATE
);
1426 pa_assert(s
->write_data
);
1428 pa_memblock_release(s
->write_memblock
);
1429 pa_memblock_unref(s
->write_memblock
);
1430 s
->write_memblock
= NULL
;
1431 s
->write_data
= NULL
;
1436 int pa_stream_write(
1440 pa_free_cb_t free_cb
,
1442 pa_seek_mode_t seek
) {
1445 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1448 PA_CHECK_VALIDITY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
1449 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1450 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| s
->direction
== PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1451 PA_CHECK_VALIDITY(s
->context
, seek
<= PA_SEEK_RELATIVE_END
, PA_ERR_INVALID
);
1452 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| (seek
== PA_SEEK_RELATIVE
&& offset
== 0), PA_ERR_INVALID
);
1453 PA_CHECK_VALIDITY(s
->context
,
1454 !s
->write_memblock
||
1455 ((data
>= s
->write_data
) &&
1456 ((const char*) data
+ length
<= (const char*) s
->write_data
+ pa_memblock_get_length(s
->write_memblock
))),
1458 PA_CHECK_VALIDITY(s
->context
, !free_cb
|| !s
->write_memblock
, PA_ERR_INVALID
);
1460 if (s
->write_memblock
) {
1463 /* pa_stream_write_begin() was called before */
1465 pa_memblock_release(s
->write_memblock
);
1467 chunk
.memblock
= s
->write_memblock
;
1468 chunk
.index
= (const char *) data
- (const char *) s
->write_data
;
1469 chunk
.length
= length
;
1471 s
->write_memblock
= NULL
;
1472 s
->write_data
= NULL
;
1474 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, offset
, seek
, &chunk
);
1475 pa_memblock_unref(chunk
.memblock
);
1478 pa_seek_mode_t t_seek
= seek
;
1479 int64_t t_offset
= offset
;
1480 size_t t_length
= length
;
1481 const void *t_data
= data
;
1483 /* pa_stream_write_begin() was not called before */
1485 while (t_length
> 0) {
1490 if (free_cb
&& !pa_pstream_get_shm(s
->context
->pstream
)) {
1491 chunk
.memblock
= pa_memblock_new_user(s
->context
->mempool
, (void*) t_data
, t_length
, free_cb
, 1);
1492 chunk
.length
= t_length
;
1496 chunk
.length
= PA_MIN(t_length
, pa_mempool_block_size_max(s
->context
->mempool
));
1497 chunk
.memblock
= pa_memblock_new(s
->context
->mempool
, chunk
.length
);
1499 d
= pa_memblock_acquire(chunk
.memblock
);
1500 memcpy(d
, t_data
, chunk
.length
);
1501 pa_memblock_release(chunk
.memblock
);
1504 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, t_offset
, t_seek
, &chunk
);
1507 t_seek
= PA_SEEK_RELATIVE
;
1509 t_data
= (const uint8_t*) t_data
+ chunk
.length
;
1510 t_length
-= chunk
.length
;
1512 pa_memblock_unref(chunk
.memblock
);
1515 if (free_cb
&& pa_pstream_get_shm(s
->context
->pstream
))
1516 free_cb((void*) data
);
1519 /* This is obviously wrong since we ignore the seeking index . But
1520 * that's OK, the server side applies the same error */
1521 s
->requested_bytes
-= (seek
== PA_SEEK_RELATIVE
? offset
: 0) + (int64_t) length
;
1523 /* pa_log("wrote %lli, now at %lli", (long long) length, (long long) s->requested_bytes); */
1525 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1527 /* Update latency request correction */
1528 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
) {
1530 if (seek
== PA_SEEK_ABSOLUTE
) {
1531 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= FALSE
;
1532 s
->write_index_corrections
[s
->current_write_index_correction
].absolute
= TRUE
;
1533 s
->write_index_corrections
[s
->current_write_index_correction
].value
= offset
+ (int64_t) length
;
1534 } else if (seek
== PA_SEEK_RELATIVE
) {
1535 if (!s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
)
1536 s
->write_index_corrections
[s
->current_write_index_correction
].value
+= offset
+ (int64_t) length
;
1538 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= TRUE
;
1541 /* Update the write index in the already available latency data */
1542 if (s
->timing_info_valid
) {
1544 if (seek
== PA_SEEK_ABSOLUTE
) {
1545 s
->timing_info
.write_index_corrupt
= FALSE
;
1546 s
->timing_info
.write_index
= offset
+ (int64_t) length
;
1547 } else if (seek
== PA_SEEK_RELATIVE
) {
1548 if (!s
->timing_info
.write_index_corrupt
)
1549 s
->timing_info
.write_index
+= offset
+ (int64_t) length
;
1551 s
->timing_info
.write_index_corrupt
= TRUE
;
1554 if (!s
->timing_info_valid
|| s
->timing_info
.write_index_corrupt
)
1555 request_auto_timing_update(s
, TRUE
);
1561 int pa_stream_peek(pa_stream
*s
, const void **data
, size_t *length
) {
1563 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1567 PA_CHECK_VALIDITY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
1568 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1569 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
1571 if (!s
->peek_memchunk
.memblock
) {
1573 if (pa_memblockq_peek(s
->record_memblockq
, &s
->peek_memchunk
) < 0) {
1579 s
->peek_data
= pa_memblock_acquire(s
->peek_memchunk
.memblock
);
1582 pa_assert(s
->peek_data
);
1583 *data
= (uint8_t*) s
->peek_data
+ s
->peek_memchunk
.index
;
1584 *length
= s
->peek_memchunk
.length
;
1588 int pa_stream_drop(pa_stream
*s
) {
1590 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1592 PA_CHECK_VALIDITY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
1593 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1594 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
1595 PA_CHECK_VALIDITY(s
->context
, s
->peek_memchunk
.memblock
, PA_ERR_BADSTATE
);
1597 pa_memblockq_drop(s
->record_memblockq
, s
->peek_memchunk
.length
);
1599 /* Fix the simulated local read index */
1600 if (s
->timing_info_valid
&& !s
->timing_info
.read_index_corrupt
)
1601 s
->timing_info
.read_index
+= (int64_t) s
->peek_memchunk
.length
;
1603 pa_assert(s
->peek_data
);
1604 pa_memblock_release(s
->peek_memchunk
.memblock
);
1605 pa_memblock_unref(s
->peek_memchunk
.memblock
);
1606 pa_memchunk_reset(&s
->peek_memchunk
);
1611 size_t pa_stream_writable_size(pa_stream
*s
) {
1613 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1615 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
, (size_t) -1);
1616 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
1617 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
1619 return s
->requested_bytes
> 0 ? (size_t) s
->requested_bytes
: 0;
1622 size_t pa_stream_readable_size(pa_stream
*s
) {
1624 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1626 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
, (size_t) -1);
1627 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
1628 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
1630 return pa_memblockq_get_length(s
->record_memblockq
);
1633 pa_operation
* pa_stream_drain(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1639 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1641 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
1642 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1643 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1645 /* Ask for a timing update before we cork/uncork to get the best
1646 * accuracy for the transport latency suitable for the
1647 * check_smoother_status() call in the started callback */
1648 request_auto_timing_update(s
, TRUE
);
1650 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1652 t
= pa_tagstruct_command(s
->context
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
, &tag
);
1653 pa_tagstruct_putu32(t
, s
->channel
);
1654 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1655 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
);
1657 /* This might cause the read index to conitnue again, hence
1658 * let's request a timing update */
1659 request_auto_timing_update(s
, TRUE
);
1664 static pa_usec_t
calc_time(pa_stream
*s
, pa_bool_t ignore_transport
) {
1668 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1669 pa_assert(s
->state
== PA_STREAM_READY
);
1670 pa_assert(s
->direction
!= PA_STREAM_UPLOAD
);
1671 pa_assert(s
->timing_info_valid
);
1672 pa_assert(s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.read_index_corrupt
);
1673 pa_assert(s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.write_index_corrupt
);
1675 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1676 /* The last byte that was written into the output device
1677 * had this time value associated */
1678 usec
= pa_bytes_to_usec(s
->timing_info
.read_index
< 0 ? 0 : (uint64_t) s
->timing_info
.read_index
, &s
->sample_spec
);
1680 if (!s
->corked
&& !s
->suspended
) {
1682 if (!ignore_transport
)
1683 /* Because the latency info took a little time to come
1684 * to us, we assume that the real output time is actually
1686 usec
+= s
->timing_info
.transport_usec
;
1688 /* However, the output device usually maintains a buffer
1689 too, hence the real sample currently played is a little
1691 if (s
->timing_info
.sink_usec
>= usec
)
1694 usec
-= s
->timing_info
.sink_usec
;
1698 pa_assert(s
->direction
== PA_STREAM_RECORD
);
1700 /* The last byte written into the server side queue had
1701 * this time value associated */
1702 usec
= pa_bytes_to_usec(s
->timing_info
.write_index
< 0 ? 0 : (uint64_t) s
->timing_info
.write_index
, &s
->sample_spec
);
1704 if (!s
->corked
&& !s
->suspended
) {
1706 if (!ignore_transport
)
1707 /* Add transport latency */
1708 usec
+= s
->timing_info
.transport_usec
;
1710 /* Add latency of data in device buffer */
1711 usec
+= s
->timing_info
.source_usec
;
1713 /* If this is a monitor source, we need to correct the
1714 * time by the playback device buffer */
1715 if (s
->timing_info
.sink_usec
>= usec
)
1718 usec
-= s
->timing_info
.sink_usec
;
1725 static void stream_get_timing_info_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1726 pa_operation
*o
= userdata
;
1727 struct timeval local
, remote
, now
;
1729 pa_bool_t playing
= FALSE
;
1730 uint64_t underrun_for
= 0, playing_for
= 0;
1734 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
1736 if (!o
->context
|| !o
->stream
)
1739 i
= &o
->stream
->timing_info
;
1741 o
->stream
->timing_info_valid
= FALSE
;
1742 i
->write_index_corrupt
= TRUE
;
1743 i
->read_index_corrupt
= TRUE
;
1745 if (command
!= PA_COMMAND_REPLY
) {
1746 if (pa_context_handle_error(o
->context
, command
, t
, FALSE
) < 0)
1751 if (pa_tagstruct_get_usec(t
, &i
->sink_usec
) < 0 ||
1752 pa_tagstruct_get_usec(t
, &i
->source_usec
) < 0 ||
1753 pa_tagstruct_get_boolean(t
, &playing
) < 0 ||
1754 pa_tagstruct_get_timeval(t
, &local
) < 0 ||
1755 pa_tagstruct_get_timeval(t
, &remote
) < 0 ||
1756 pa_tagstruct_gets64(t
, &i
->write_index
) < 0 ||
1757 pa_tagstruct_gets64(t
, &i
->read_index
) < 0) {
1759 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1763 if (o
->context
->version
>= 13 &&
1764 o
->stream
->direction
== PA_STREAM_PLAYBACK
)
1765 if (pa_tagstruct_getu64(t
, &underrun_for
) < 0 ||
1766 pa_tagstruct_getu64(t
, &playing_for
) < 0) {
1768 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1773 if (!pa_tagstruct_eof(t
)) {
1774 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1777 o
->stream
->timing_info_valid
= TRUE
;
1778 i
->write_index_corrupt
= FALSE
;
1779 i
->read_index_corrupt
= FALSE
;
1781 i
->playing
= (int) playing
;
1782 i
->since_underrun
= (int64_t) (playing
? playing_for
: underrun_for
);
1784 pa_gettimeofday(&now
);
1786 /* Calculcate timestamps */
1787 if (pa_timeval_cmp(&local
, &remote
) <= 0 && pa_timeval_cmp(&remote
, &now
) <= 0) {
1788 /* local and remote seem to have synchronized clocks */
1790 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
)
1791 i
->transport_usec
= pa_timeval_diff(&remote
, &local
);
1793 i
->transport_usec
= pa_timeval_diff(&now
, &remote
);
1795 i
->synchronized_clocks
= TRUE
;
1796 i
->timestamp
= remote
;
1798 /* clocks are not synchronized, let's estimate latency then */
1799 i
->transport_usec
= pa_timeval_diff(&now
, &local
)/2;
1800 i
->synchronized_clocks
= FALSE
;
1801 i
->timestamp
= local
;
1802 pa_timeval_add(&i
->timestamp
, i
->transport_usec
);
1805 /* Invalidate read and write indexes if necessary */
1806 if (tag
< o
->stream
->read_index_not_before
)
1807 i
->read_index_corrupt
= TRUE
;
1809 if (tag
< o
->stream
->write_index_not_before
)
1810 i
->write_index_corrupt
= TRUE
;
1812 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
1813 /* Write index correction */
1816 uint32_t ctag
= tag
;
1818 /* Go through the saved correction values and add up the
1819 * total correction.*/
1820 for (n
= 0, j
= o
->stream
->current_write_index_correction
+1;
1821 n
< PA_MAX_WRITE_INDEX_CORRECTIONS
;
1822 n
++, j
= (j
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
) {
1824 /* Step over invalid data or out-of-date data */
1825 if (!o
->stream
->write_index_corrections
[j
].valid
||
1826 o
->stream
->write_index_corrections
[j
].tag
< ctag
)
1829 /* Make sure that everything is in order */
1830 ctag
= o
->stream
->write_index_corrections
[j
].tag
+1;
1832 /* Now fix the write index */
1833 if (o
->stream
->write_index_corrections
[j
].corrupt
) {
1834 /* A corrupting seek was made */
1835 i
->write_index_corrupt
= TRUE
;
1836 } else if (o
->stream
->write_index_corrections
[j
].absolute
) {
1837 /* An absolute seek was made */
1838 i
->write_index
= o
->stream
->write_index_corrections
[j
].value
;
1839 i
->write_index_corrupt
= FALSE
;
1840 } else if (!i
->write_index_corrupt
) {
1841 /* A relative seek was made */
1842 i
->write_index
+= o
->stream
->write_index_corrections
[j
].value
;
1846 /* Clear old correction entries */
1847 for (n
= 0; n
< PA_MAX_WRITE_INDEX_CORRECTIONS
; n
++) {
1848 if (!o
->stream
->write_index_corrections
[n
].valid
)
1851 if (o
->stream
->write_index_corrections
[n
].tag
<= tag
)
1852 o
->stream
->write_index_corrections
[n
].valid
= FALSE
;
1856 if (o
->stream
->direction
== PA_STREAM_RECORD
) {
1857 /* Read index correction */
1859 if (!i
->read_index_corrupt
)
1860 i
->read_index
-= (int64_t) pa_memblockq_get_length(o
->stream
->record_memblockq
);
1863 /* Update smoother if we're not corked */
1864 if (o
->stream
->smoother
&& !o
->stream
->corked
) {
1867 u
= x
= pa_rtclock_now() - i
->transport_usec
;
1869 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
&& o
->context
->version
>= 13) {
1872 /* If we weren't playing then it will take some time
1873 * until the audio will actually come out through the
1874 * speakers. Since we follow that timing here, we need
1875 * to try to fix this up */
1877 su
= pa_bytes_to_usec((uint64_t) i
->since_underrun
, &o
->stream
->sample_spec
);
1879 if (su
< i
->sink_usec
)
1880 x
+= i
->sink_usec
- su
;
1884 pa_smoother_pause(o
->stream
->smoother
, x
);
1886 /* Update the smoother */
1887 if ((o
->stream
->direction
== PA_STREAM_PLAYBACK
&& !i
->read_index_corrupt
) ||
1888 (o
->stream
->direction
== PA_STREAM_RECORD
&& !i
->write_index_corrupt
))
1889 pa_smoother_put(o
->stream
->smoother
, u
, calc_time(o
->stream
, TRUE
));
1892 pa_smoother_resume(o
->stream
->smoother
, x
, TRUE
);
1896 o
->stream
->auto_timing_update_requested
= FALSE
;
1898 if (o
->stream
->latency_update_callback
)
1899 o
->stream
->latency_update_callback(o
->stream
, o
->stream
->latency_update_userdata
);
1901 if (o
->callback
&& o
->stream
&& o
->stream
->state
== PA_STREAM_READY
) {
1902 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
1903 cb(o
->stream
, o
->stream
->timing_info_valid
, o
->userdata
);
1908 pa_operation_done(o
);
1909 pa_operation_unref(o
);
1912 pa_operation
* pa_stream_update_timing_info(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1920 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1922 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
1923 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1924 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1926 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1927 /* Find a place to store the write_index correction data for this entry */
1928 cidx
= (s
->current_write_index_correction
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
;
1930 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
1931 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !s
->write_index_corrections
[cidx
].valid
, PA_ERR_INTERNAL
);
1933 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1935 t
= pa_tagstruct_command(
1937 (uint32_t) (s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_GET_PLAYBACK_LATENCY
: PA_COMMAND_GET_RECORD_LATENCY
),
1939 pa_tagstruct_putu32(t
, s
->channel
);
1940 pa_tagstruct_put_timeval(t
, pa_gettimeofday(&now
));
1942 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1943 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
);
1945 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1946 /* Fill in initial correction data */
1948 s
->current_write_index_correction
= cidx
;
1950 s
->write_index_corrections
[cidx
].valid
= TRUE
;
1951 s
->write_index_corrections
[cidx
].absolute
= FALSE
;
1952 s
->write_index_corrections
[cidx
].corrupt
= FALSE
;
1953 s
->write_index_corrections
[cidx
].tag
= tag
;
1954 s
->write_index_corrections
[cidx
].value
= 0;
1960 void pa_stream_disconnect_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1961 pa_stream
*s
= userdata
;
1965 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1969 if (command
!= PA_COMMAND_REPLY
) {
1970 if (pa_context_handle_error(s
->context
, command
, t
, FALSE
) < 0)
1973 pa_stream_set_state(s
, PA_STREAM_FAILED
);
1975 } else if (!pa_tagstruct_eof(t
)) {
1976 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
1980 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
1986 int pa_stream_disconnect(pa_stream
*s
) {
1991 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1993 PA_CHECK_VALIDITY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
1994 PA_CHECK_VALIDITY(s
->context
, s
->channel_valid
, PA_ERR_BADSTATE
);
1995 PA_CHECK_VALIDITY(s
->context
, s
->context
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
1999 t
= pa_tagstruct_command(
2001 (uint32_t) (s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
2002 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
)),
2004 pa_tagstruct_putu32(t
, s
->channel
);
2005 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
2006 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
, NULL
);
2012 void pa_stream_set_read_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
2014 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2016 if (pa_detect_fork())
2019 if (s
->state
== PA_STREAM_TERMINATED
|| s
->state
== PA_STREAM_FAILED
)
2022 s
->read_callback
= cb
;
2023 s
->read_userdata
= userdata
;
2026 void pa_stream_set_write_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
2028 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2030 if (pa_detect_fork())
2033 if (s
->state
== PA_STREAM_TERMINATED
|| s
->state
== PA_STREAM_FAILED
)
2036 s
->write_callback
= cb
;
2037 s
->write_userdata
= userdata
;
2040 void pa_stream_set_state_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
2042 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2044 if (pa_detect_fork())
2047 if (s
->state
== PA_STREAM_TERMINATED
|| s
->state
== PA_STREAM_FAILED
)
2050 s
->state_callback
= cb
;
2051 s
->state_userdata
= userdata
;
2054 void pa_stream_set_overflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
2056 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2058 if (pa_detect_fork())
2061 if (s
->state
== PA_STREAM_TERMINATED
|| s
->state
== PA_STREAM_FAILED
)
2064 s
->overflow_callback
= cb
;
2065 s
->overflow_userdata
= userdata
;
2068 void pa_stream_set_underflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
2070 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2072 if (pa_detect_fork())
2075 if (s
->state
== PA_STREAM_TERMINATED
|| s
->state
== PA_STREAM_FAILED
)
2078 s
->underflow_callback
= cb
;
2079 s
->underflow_userdata
= userdata
;
2082 void pa_stream_set_latency_update_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
2084 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2086 if (pa_detect_fork())
2089 if (s
->state
== PA_STREAM_TERMINATED
|| s
->state
== PA_STREAM_FAILED
)
2092 s
->latency_update_callback
= cb
;
2093 s
->latency_update_userdata
= userdata
;
2096 void pa_stream_set_moved_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
2098 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2100 if (pa_detect_fork())
2103 if (s
->state
== PA_STREAM_TERMINATED
|| s
->state
== PA_STREAM_FAILED
)
2106 s
->moved_callback
= cb
;
2107 s
->moved_userdata
= userdata
;
2110 void pa_stream_set_suspended_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
2112 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2114 if (pa_detect_fork())
2117 if (s
->state
== PA_STREAM_TERMINATED
|| s
->state
== PA_STREAM_FAILED
)
2120 s
->suspended_callback
= cb
;
2121 s
->suspended_userdata
= userdata
;
2124 void pa_stream_set_started_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
2126 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2128 if (pa_detect_fork())
2131 if (s
->state
== PA_STREAM_TERMINATED
|| s
->state
== PA_STREAM_FAILED
)
2134 s
->started_callback
= cb
;
2135 s
->started_userdata
= userdata
;
2138 void pa_stream_set_event_callback(pa_stream
*s
, pa_stream_event_cb_t cb
, void *userdata
) {
2140 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2142 if (pa_detect_fork())
2145 if (s
->state
== PA_STREAM_TERMINATED
|| s
->state
== PA_STREAM_FAILED
)
2148 s
->event_callback
= cb
;
2149 s
->event_userdata
= userdata
;
2152 void pa_stream_set_buffer_attr_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
2154 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2156 if (pa_detect_fork())
2159 if (s
->state
== PA_STREAM_TERMINATED
|| s
->state
== PA_STREAM_FAILED
)
2162 s
->buffer_attr_callback
= cb
;
2163 s
->buffer_attr_userdata
= userdata
;
2166 void pa_stream_simple_ack_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2167 pa_operation
*o
= userdata
;
2172 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
2177 if (command
!= PA_COMMAND_REPLY
) {
2178 if (pa_context_handle_error(o
->context
, command
, t
, FALSE
) < 0)
2182 } else if (!pa_tagstruct_eof(t
)) {
2183 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
2188 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
2189 cb(o
->stream
, success
, o
->userdata
);
2193 pa_operation_done(o
);
2194 pa_operation_unref(o
);
2197 pa_operation
* pa_stream_cork(pa_stream
*s
, int b
, pa_stream_success_cb_t cb
, void *userdata
) {
2203 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2205 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2206 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2207 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2209 /* Ask for a timing update before we cork/uncork to get the best
2210 * accuracy for the transport latency suitable for the
2211 * check_smoother_status() call in the started callback */
2212 request_auto_timing_update(s
, TRUE
);
2216 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
2218 t
= pa_tagstruct_command(
2220 (uint32_t) (s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CORK_PLAYBACK_STREAM
: PA_COMMAND_CORK_RECORD_STREAM
),
2222 pa_tagstruct_putu32(t
, s
->channel
);
2223 pa_tagstruct_put_boolean(t
, !!b
);
2224 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
2225 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
);
2227 check_smoother_status(s
, FALSE
, FALSE
, FALSE
);
2229 /* This might cause the indexes to hang/start again, hence let's
2230 * request a timing update, after the cork/uncork, too */
2231 request_auto_timing_update(s
, TRUE
);
2236 static pa_operation
* stream_send_simple_command(pa_stream
*s
, uint32_t command
, pa_stream_success_cb_t cb
, void *userdata
) {
2242 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2244 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2245 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2247 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
2249 t
= pa_tagstruct_command(s
->context
, command
, &tag
);
2250 pa_tagstruct_putu32(t
, s
->channel
);
2251 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
2252 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
);
2257 pa_operation
* pa_stream_flush(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
2261 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2263 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2264 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2265 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2267 /* Ask for a timing update *before* the flush, so that the
2268 * transport usec is as up to date as possible when we get the
2269 * underflow message and update the smoother status*/
2270 request_auto_timing_update(s
, TRUE
);
2272 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
)))
2275 if (s
->direction
== PA_STREAM_PLAYBACK
) {
2277 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
)
2278 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= TRUE
;
2280 if (s
->buffer_attr
.prebuf
> 0)
2281 check_smoother_status(s
, FALSE
, FALSE
, TRUE
);
2283 /* This will change the write index, but leave the
2284 * read index untouched. */
2285 invalidate_indexes(s
, FALSE
, TRUE
);
2288 /* For record streams this has no influence on the write
2289 * index, but the read index might jump. */
2290 invalidate_indexes(s
, TRUE
, FALSE
);
2292 /* Note that we do not update requested_bytes here. This is
2293 * because we cannot really know how data actually was dropped
2294 * from the write index due to this. This 'error' will be applied
2295 * by both client and server and hence we should be fine. */
2300 pa_operation
* pa_stream_prebuf(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
2304 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2306 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2307 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2308 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
2309 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
2311 /* Ask for a timing update before we cork/uncork to get the best
2312 * accuracy for the transport latency suitable for the
2313 * check_smoother_status() call in the started callback */
2314 request_auto_timing_update(s
, TRUE
);
2316 if (!(o
= stream_send_simple_command(s
, PA_COMMAND_PREBUF_PLAYBACK_STREAM
, cb
, userdata
)))
2319 /* This might cause the read index to hang again, hence
2320 * let's request a timing update */
2321 request_auto_timing_update(s
, TRUE
);
2326 pa_operation
* pa_stream_trigger(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
2330 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2332 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2333 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2334 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
2335 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
2337 /* Ask for a timing update before we cork/uncork to get the best
2338 * accuracy for the transport latency suitable for the
2339 * check_smoother_status() call in the started callback */
2340 request_auto_timing_update(s
, TRUE
);
2342 if (!(o
= stream_send_simple_command(s
, PA_COMMAND_TRIGGER_PLAYBACK_STREAM
, cb
, userdata
)))
2345 /* This might cause the read index to start moving again, hence
2346 * let's request a timing update */
2347 request_auto_timing_update(s
, TRUE
);
2352 pa_operation
* pa_stream_set_name(pa_stream
*s
, const char *name
, pa_stream_success_cb_t cb
, void *userdata
) {
2356 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2359 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2360 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2361 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2363 if (s
->context
->version
>= 13) {
2364 pa_proplist
*p
= pa_proplist_new();
2366 pa_proplist_sets(p
, PA_PROP_MEDIA_NAME
, name
);
2367 o
= pa_stream_proplist_update(s
, PA_UPDATE_REPLACE
, p
, cb
, userdata
);
2368 pa_proplist_free(p
);
2373 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
2374 t
= pa_tagstruct_command(
2376 (uint32_t) (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_NAME
: PA_COMMAND_SET_PLAYBACK_STREAM_NAME
),
2378 pa_tagstruct_putu32(t
, s
->channel
);
2379 pa_tagstruct_puts(t
, name
);
2380 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
2381 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
);
2387 int pa_stream_get_time(pa_stream
*s
, pa_usec_t
*r_usec
) {
2391 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2393 PA_CHECK_VALIDITY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2394 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2395 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2396 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
2397 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
2398 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
2401 usec
= pa_smoother_get(s
->smoother
, pa_rtclock_now());
2403 usec
= calc_time(s
, FALSE
);
2405 /* Make sure the time runs monotonically */
2406 if (!(s
->flags
& PA_STREAM_NOT_MONOTONIC
)) {
2407 if (usec
< s
->previous_time
)
2408 usec
= s
->previous_time
;
2410 s
->previous_time
= usec
;
2419 static pa_usec_t
time_counter_diff(pa_stream
*s
, pa_usec_t a
, pa_usec_t b
, int *negative
) {
2421 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2429 if (negative
&& s
->direction
== PA_STREAM_RECORD
) {
2437 int pa_stream_get_latency(pa_stream
*s
, pa_usec_t
*r_usec
, int *negative
) {
2443 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2446 PA_CHECK_VALIDITY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2447 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2448 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2449 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
2450 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
2451 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
2453 if ((r
= pa_stream_get_time(s
, &t
)) < 0)
2456 if (s
->direction
== PA_STREAM_PLAYBACK
)
2457 cindex
= s
->timing_info
.write_index
;
2459 cindex
= s
->timing_info
.read_index
;
2464 c
= pa_bytes_to_usec((uint64_t) cindex
, &s
->sample_spec
);
2466 if (s
->direction
== PA_STREAM_PLAYBACK
)
2467 *r_usec
= time_counter_diff(s
, c
, t
, negative
);
2469 *r_usec
= time_counter_diff(s
, t
, c
, negative
);
2474 const pa_timing_info
* pa_stream_get_timing_info(pa_stream
*s
) {
2476 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2478 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2479 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2480 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2481 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
2483 return &s
->timing_info
;
2486 const pa_sample_spec
* pa_stream_get_sample_spec(pa_stream
*s
) {
2488 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2490 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2492 return &s
->sample_spec
;
2495 const pa_channel_map
* pa_stream_get_channel_map(pa_stream
*s
) {
2497 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2499 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2501 return &s
->channel_map
;
2504 const pa_format_info
* pa_stream_get_format_info(pa_stream
*s
) {
2506 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2508 /* We don't have the format till routing is done */
2509 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2510 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2514 const pa_buffer_attr
* pa_stream_get_buffer_attr(pa_stream
*s
) {
2516 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2518 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2519 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2520 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->context
->version
>= 9, PA_ERR_NOTSUPPORTED
);
2522 return &s
->buffer_attr
;
2525 static void stream_set_buffer_attr_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2526 pa_operation
*o
= userdata
;
2531 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
2536 if (command
!= PA_COMMAND_REPLY
) {
2537 if (pa_context_handle_error(o
->context
, command
, t
, FALSE
) < 0)
2542 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
2543 if (pa_tagstruct_getu32(t
, &o
->stream
->buffer_attr
.maxlength
) < 0 ||
2544 pa_tagstruct_getu32(t
, &o
->stream
->buffer_attr
.tlength
) < 0 ||
2545 pa_tagstruct_getu32(t
, &o
->stream
->buffer_attr
.prebuf
) < 0 ||
2546 pa_tagstruct_getu32(t
, &o
->stream
->buffer_attr
.minreq
) < 0) {
2547 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
2550 } else if (o
->stream
->direction
== PA_STREAM_RECORD
) {
2551 if (pa_tagstruct_getu32(t
, &o
->stream
->buffer_attr
.maxlength
) < 0 ||
2552 pa_tagstruct_getu32(t
, &o
->stream
->buffer_attr
.fragsize
) < 0) {
2553 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
2558 if (o
->stream
->context
->version
>= 13) {
2561 if (pa_tagstruct_get_usec(t
, &usec
) < 0) {
2562 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
2566 if (o
->stream
->direction
== PA_STREAM_RECORD
)
2567 o
->stream
->timing_info
.configured_source_usec
= usec
;
2569 o
->stream
->timing_info
.configured_sink_usec
= usec
;
2572 if (!pa_tagstruct_eof(t
)) {
2573 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
2579 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
2580 cb(o
->stream
, success
, o
->userdata
);
2584 pa_operation_done(o
);
2585 pa_operation_unref(o
);
2589 pa_operation
* pa_stream_set_buffer_attr(pa_stream
*s
, const pa_buffer_attr
*attr
, pa_stream_success_cb_t cb
, void *userdata
) {
2593 pa_buffer_attr copy
;
2596 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2599 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2600 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2601 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2602 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->context
->version
>= 12, PA_ERR_NOTSUPPORTED
);
2604 /* Ask for a timing update before we cork/uncork to get the best
2605 * accuracy for the transport latency suitable for the
2606 * check_smoother_status() call in the started callback */
2607 request_auto_timing_update(s
, TRUE
);
2609 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
2611 t
= pa_tagstruct_command(
2613 (uint32_t) (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
: PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
),
2615 pa_tagstruct_putu32(t
, s
->channel
);
2618 patch_buffer_attr(s
, ©
, NULL
);
2621 pa_tagstruct_putu32(t
, attr
->maxlength
);
2623 if (s
->direction
== PA_STREAM_PLAYBACK
)
2626 PA_TAG_U32
, attr
->tlength
,
2627 PA_TAG_U32
, attr
->prebuf
,
2628 PA_TAG_U32
, attr
->minreq
,
2631 pa_tagstruct_putu32(t
, attr
->fragsize
);
2633 if (s
->context
->version
>= 13)
2634 pa_tagstruct_put_boolean(t
, !!(s
->flags
& PA_STREAM_ADJUST_LATENCY
));
2636 if (s
->context
->version
>= 14)
2637 pa_tagstruct_put_boolean(t
, !!(s
->flags
& PA_STREAM_EARLY_REQUESTS
));
2639 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
2640 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
);
2642 /* This might cause changes in the read/write indexex, hence let's
2643 * request a timing update */
2644 request_auto_timing_update(s
, TRUE
);
2649 uint32_t pa_stream_get_device_index(pa_stream
*s
) {
2651 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2653 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
, PA_INVALID_INDEX
);
2654 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
2655 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
2656 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->context
->version
>= 12, PA_ERR_NOTSUPPORTED
, PA_INVALID_INDEX
);
2657 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->device_index
!= PA_INVALID_INDEX
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
2659 return s
->device_index
;
2662 const char *pa_stream_get_device_name(pa_stream
*s
) {
2664 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2666 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2667 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2668 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2669 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->context
->version
>= 12, PA_ERR_NOTSUPPORTED
);
2670 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->device_name
, PA_ERR_BADSTATE
);
2672 return s
->device_name
;
2675 int pa_stream_is_suspended(pa_stream
*s
) {
2677 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2679 PA_CHECK_VALIDITY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2680 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2681 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2682 PA_CHECK_VALIDITY(s
->context
, s
->context
->version
>= 12, PA_ERR_NOTSUPPORTED
);
2684 return s
->suspended
;
2687 int pa_stream_is_corked(pa_stream
*s
) {
2689 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2691 PA_CHECK_VALIDITY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2692 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2693 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2698 static void stream_update_sample_rate_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
2699 pa_operation
*o
= userdata
;
2704 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
2709 if (command
!= PA_COMMAND_REPLY
) {
2710 if (pa_context_handle_error(o
->context
, command
, t
, FALSE
) < 0)
2716 if (!pa_tagstruct_eof(t
)) {
2717 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
2722 o
->stream
->sample_spec
.rate
= PA_PTR_TO_UINT(o
->private);
2723 pa_assert(pa_sample_spec_valid(&o
->stream
->sample_spec
));
2726 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
2727 cb(o
->stream
, success
, o
->userdata
);
2731 pa_operation_done(o
);
2732 pa_operation_unref(o
);
2736 pa_operation
*pa_stream_update_sample_rate(pa_stream
*s
, uint32_t rate
, pa_stream_success_cb_t cb
, void *userdata
) {
2742 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2744 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2745 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, rate
> 0 && rate
<= PA_RATE_MAX
, PA_ERR_INVALID
);
2746 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2747 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2748 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->flags
& PA_STREAM_VARIABLE_RATE
, PA_ERR_BADSTATE
);
2749 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->context
->version
>= 12, PA_ERR_NOTSUPPORTED
);
2751 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
2752 o
->private = PA_UINT_TO_PTR(rate
);
2754 t
= pa_tagstruct_command(
2756 (uint32_t) (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
: PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
),
2758 pa_tagstruct_putu32(t
, s
->channel
);
2759 pa_tagstruct_putu32(t
, rate
);
2761 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
2762 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
);
2767 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
) {
2773 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2775 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2776 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
, PA_ERR_INVALID
);
2777 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2778 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2779 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->context
->version
>= 13, PA_ERR_NOTSUPPORTED
);
2781 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
2783 t
= pa_tagstruct_command(
2785 (uint32_t) (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST
: PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST
),
2787 pa_tagstruct_putu32(t
, s
->channel
);
2788 pa_tagstruct_putu32(t
, (uint32_t) mode
);
2789 pa_tagstruct_put_proplist(t
, p
);
2791 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
2792 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
);
2794 /* Please note that we don't update s->proplist here, because we
2795 * don't export that field */
2800 pa_operation
*pa_stream_proplist_remove(pa_stream
*s
, const char *const keys
[], pa_stream_success_cb_t cb
, void *userdata
) {
2804 const char * const*k
;
2807 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2809 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2810 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, keys
&& keys
[0], PA_ERR_INVALID
);
2811 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
2812 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
2813 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->context
->version
>= 13, PA_ERR_NOTSUPPORTED
);
2815 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
2817 t
= pa_tagstruct_command(
2819 (uint32_t) (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST
: PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST
),
2821 pa_tagstruct_putu32(t
, s
->channel
);
2823 for (k
= keys
; *k
; k
++)
2824 pa_tagstruct_puts(t
, *k
);
2826 pa_tagstruct_puts(t
, NULL
);
2828 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
2829 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
);
2831 /* Please note that we don't update s->proplist here, because we
2832 * don't export that field */
2837 int pa_stream_set_monitor_stream(pa_stream
*s
, uint32_t sink_input_idx
) {
2839 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2841 PA_CHECK_VALIDITY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
);
2842 PA_CHECK_VALIDITY(s
->context
, sink_input_idx
!= PA_INVALID_INDEX
, PA_ERR_INVALID
);
2843 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_UNCONNECTED
, PA_ERR_BADSTATE
);
2844 PA_CHECK_VALIDITY(s
->context
, s
->context
->version
>= 13, PA_ERR_NOTSUPPORTED
);
2846 s
->direct_on_input
= sink_input_idx
;
2851 uint32_t pa_stream_get_monitor_stream(pa_stream
*s
) {
2853 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
2855 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, !pa_detect_fork(), PA_ERR_FORKED
, PA_INVALID_INDEX
);
2856 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direct_on_input
!= PA_INVALID_INDEX
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
2857 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->context
->version
>= 13, PA_ERR_NOTSUPPORTED
, PA_INVALID_INDEX
);
2859 return s
->direct_on_input
;