4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <pulse/def.h>
34 #include <pulse/timeval.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/pstream-util.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/hashmap.h>
40 #include <pulsecore/macro.h>
44 #define LATENCY_IPOL_INTERVAL_USEC (100000L)
46 pa_stream
*pa_stream_new(pa_context
*c
, const char *name
, const pa_sample_spec
*ss
, const pa_channel_map
*map
) {
51 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
53 PA_CHECK_VALIDITY_RETURN_NULL(c
, ss
&& pa_sample_spec_valid(ss
), PA_ERR_INVALID
);
54 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->version
>= 12 || (ss
->format
!= PA_SAMPLE_S32LE
|| ss
->format
!= PA_SAMPLE_S32NE
), PA_ERR_NOTSUPPORTED
);
55 PA_CHECK_VALIDITY_RETURN_NULL(c
, !map
|| (pa_channel_map_valid(map
) && map
->channels
== ss
->channels
), PA_ERR_INVALID
);
57 s
= pa_xnew(pa_stream
, 1);
60 s
->mainloop
= c
->mainloop
;
62 s
->buffer_attr_not_ready
= s
->timing_info_not_ready
= FALSE
;
64 s
->read_callback
= NULL
;
65 s
->read_userdata
= NULL
;
66 s
->write_callback
= NULL
;
67 s
->write_userdata
= NULL
;
68 s
->state_callback
= NULL
;
69 s
->state_userdata
= NULL
;
70 s
->overflow_callback
= NULL
;
71 s
->overflow_userdata
= NULL
;
72 s
->underflow_callback
= NULL
;
73 s
->underflow_userdata
= NULL
;
74 s
->latency_update_callback
= NULL
;
75 s
->latency_update_userdata
= NULL
;
76 s
->moved_callback
= NULL
;
77 s
->moved_userdata
= NULL
;
78 s
->suspended_callback
= NULL
;
79 s
->suspended_userdata
= NULL
;
81 s
->direction
= PA_STREAM_NODIRECTION
;
82 s
->name
= pa_xstrdup(name
);
87 s
->channel_map
= *map
;
89 pa_channel_map_init_auto(&s
->channel_map
, ss
->channels
, PA_CHANNEL_MAP_DEFAULT
);
93 s
->syncid
= c
->csyncid
++;
94 s
->stream_index
= PA_INVALID_INDEX
;
95 s
->requested_bytes
= 0;
96 s
->state
= PA_STREAM_UNCONNECTED
;
98 s
->manual_buffer_attr
= FALSE
;
99 memset(&s
->buffer_attr
, 0, sizeof(s
->buffer_attr
));
101 s
->device_index
= PA_INVALID_INDEX
;
102 s
->device_name
= NULL
;
103 s
->suspended
= FALSE
;
105 s
->peek_memchunk
.index
= 0;
106 s
->peek_memchunk
.length
= 0;
107 s
->peek_memchunk
.memblock
= NULL
;
110 s
->record_memblockq
= NULL
;
112 s
->previous_time
= 0;
113 s
->timing_info_valid
= 0;
114 s
->read_index_not_before
= 0;
115 s
->write_index_not_before
= 0;
117 for (i
= 0; i
< PA_MAX_WRITE_INDEX_CORRECTIONS
; i
++)
118 s
->write_index_corrections
[i
].valid
= 0;
119 s
->current_write_index_correction
= 0;
123 s
->cached_time_valid
= 0;
125 s
->auto_timing_update_event
= NULL
;
126 s
->auto_timing_update_requested
= 0;
128 /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
129 PA_LLIST_PREPEND(pa_stream
, c
->streams
, s
);
135 static void stream_free(pa_stream
*s
) {
137 pa_assert(!s
->context
);
138 pa_assert(!s
->channel_valid
);
140 if (s
->auto_timing_update_event
) {
141 pa_assert(s
->mainloop
);
142 s
->mainloop
->time_free(s
->auto_timing_update_event
);
145 if (s
->peek_memchunk
.memblock
) {
147 pa_memblock_release(s
->peek_memchunk
.memblock
);
148 pa_memblock_unref(s
->peek_memchunk
.memblock
);
151 if (s
->record_memblockq
)
152 pa_memblockq_free(s
->record_memblockq
);
155 pa_xfree(s
->device_name
);
159 void pa_stream_unref(pa_stream
*s
) {
161 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
163 if (PA_REFCNT_DEC(s
) <= 0)
167 pa_stream
* pa_stream_ref(pa_stream
*s
) {
169 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
175 pa_stream_state_t
pa_stream_get_state(pa_stream
*s
) {
177 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
182 pa_context
* pa_stream_get_context(pa_stream
*s
) {
184 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
189 uint32_t pa_stream_get_index(pa_stream
*s
) {
191 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
193 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
195 return s
->stream_index
;
198 void pa_stream_set_state(pa_stream
*s
, pa_stream_state_t st
) {
200 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
208 if (s
->state_callback
)
209 s
->state_callback(s
, s
->state_userdata
);
211 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
) && s
->context
) {
213 /* Detach from context */
216 /* Unref all operatio object that point to us */
217 for (o
= s
->context
->operations
; o
; o
= n
) {
221 pa_operation_cancel(o
);
224 /* Drop all outstanding replies for this stream */
225 if (s
->context
->pdispatch
)
226 pa_pdispatch_unregister_reply(s
->context
->pdispatch
, s
);
228 if (s
->channel_valid
)
229 pa_dynarray_put((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, s
->channel
, NULL
);
231 PA_LLIST_REMOVE(pa_stream
, s
->context
->streams
, s
);
235 s
->channel_valid
= 0;
239 s
->read_callback
= NULL
;
240 s
->write_callback
= NULL
;
241 s
->state_callback
= NULL
;
242 s
->overflow_callback
= NULL
;
243 s
->underflow_callback
= NULL
;
244 s
->latency_update_callback
= NULL
;
250 void pa_command_stream_killed(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
251 pa_context
*c
= userdata
;
256 pa_assert(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
);
259 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
263 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
264 !pa_tagstruct_eof(t
)) {
265 pa_context_fail(c
, PA_ERR_PROTOCOL
);
269 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, channel
)))
272 pa_context_set_error(c
, PA_ERR_KILLED
);
273 pa_stream_set_state(s
, PA_STREAM_FAILED
);
279 void pa_command_stream_moved(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
280 pa_context
*c
= userdata
;
288 pa_assert(command
== PA_COMMAND_PLAYBACK_STREAM_MOVED
|| command
== PA_COMMAND_RECORD_STREAM_MOVED
);
291 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
295 if (c
->version
< 12) {
296 pa_context_fail(c
, PA_ERR_PROTOCOL
);
300 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
301 pa_tagstruct_getu32(t
, &di
) < 0 ||
302 pa_tagstruct_gets(t
, &dn
) < 0 ||
303 pa_tagstruct_get_boolean(t
, &suspended
) < 0 ||
304 !pa_tagstruct_eof(t
)) {
305 pa_context_fail(c
, PA_ERR_PROTOCOL
);
309 if (!dn
|| di
== PA_INVALID_INDEX
) {
310 pa_context_fail(c
, PA_ERR_PROTOCOL
);
314 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_MOVED
? c
->playback_streams
: c
->record_streams
, channel
)))
317 pa_xfree(s
->device_name
);
318 s
->device_name
= pa_xstrdup(dn
);
319 s
->device_index
= di
;
321 s
->suspended
= suspended
;
323 if (s
->moved_callback
)
324 s
->moved_callback(s
, s
->moved_userdata
);
330 void pa_command_stream_suspended(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
331 pa_context
*c
= userdata
;
337 pa_assert(command
== PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
|| command
== PA_COMMAND_RECORD_STREAM_SUSPENDED
);
340 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
344 if (c
->version
< 12) {
345 pa_context_fail(c
, PA_ERR_PROTOCOL
);
349 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
350 pa_tagstruct_get_boolean(t
, &suspended
) < 0 ||
351 !pa_tagstruct_eof(t
)) {
352 pa_context_fail(c
, PA_ERR_PROTOCOL
);
356 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_SUSPENDED
? c
->playback_streams
: c
->record_streams
, channel
)))
359 s
->suspended
= suspended
;
361 if (s
->suspended_callback
)
362 s
->suspended_callback(s
, s
->suspended_userdata
);
368 void pa_command_request(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
370 pa_context
*c
= userdata
;
371 uint32_t bytes
, channel
;
374 pa_assert(command
== PA_COMMAND_REQUEST
);
377 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
381 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
382 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
383 !pa_tagstruct_eof(t
)) {
384 pa_context_fail(c
, PA_ERR_PROTOCOL
);
388 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
391 if (s
->state
== PA_STREAM_READY
) {
392 s
->requested_bytes
+= bytes
;
394 if (s
->requested_bytes
> 0 && s
->write_callback
)
395 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
402 void pa_command_overflow_or_underflow(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
404 pa_context
*c
= userdata
;
408 pa_assert(command
== PA_COMMAND_OVERFLOW
|| command
== PA_COMMAND_UNDERFLOW
);
411 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
415 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
416 !pa_tagstruct_eof(t
)) {
417 pa_context_fail(c
, PA_ERR_PROTOCOL
);
421 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
424 if (s
->state
== PA_STREAM_READY
) {
426 if (command
== PA_COMMAND_OVERFLOW
) {
427 if (s
->overflow_callback
)
428 s
->overflow_callback(s
, s
->overflow_userdata
);
429 } else if (command
== PA_COMMAND_UNDERFLOW
) {
430 if (s
->underflow_callback
)
431 s
->underflow_callback(s
, s
->underflow_userdata
);
439 static void request_auto_timing_update(pa_stream
*s
, int force
) {
441 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
443 if (!(s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
))
446 if (s
->state
== PA_STREAM_READY
&&
447 (force
|| !s
->auto_timing_update_requested
)) {
450 /* pa_log("automatically requesting new timing data"); */
452 if ((o
= pa_stream_update_timing_info(s
, NULL
, NULL
))) {
453 pa_operation_unref(o
);
454 s
->auto_timing_update_requested
= 1;
458 if (s
->auto_timing_update_event
) {
460 pa_gettimeofday(&next
);
461 pa_timeval_add(&next
, LATENCY_IPOL_INTERVAL_USEC
);
462 s
->mainloop
->time_restart(s
->auto_timing_update_event
, &next
);
466 static void invalidate_indexes(pa_stream
*s
, int r
, int w
) {
468 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
470 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
472 if (s
->state
!= PA_STREAM_READY
)
476 s
->write_index_not_before
= s
->context
->ctag
;
478 if (s
->timing_info_valid
)
479 s
->timing_info
.write_index_corrupt
= 1;
481 /* pa_log("write_index invalidated"); */
485 s
->read_index_not_before
= s
->context
->ctag
;
487 if (s
->timing_info_valid
)
488 s
->timing_info
.read_index_corrupt
= 1;
490 /* pa_log("read_index invalidated"); */
493 if ((s
->direction
== PA_STREAM_PLAYBACK
&& r
) ||
494 (s
->direction
== PA_STREAM_RECORD
&& w
))
495 s
->cached_time_valid
= 0;
497 request_auto_timing_update(s
, 1);
500 static void auto_timing_update_callback(PA_GCC_UNUSED pa_mainloop_api
*m
, PA_GCC_UNUSED pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
501 pa_stream
*s
= userdata
;
504 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
506 /* pa_log("time event"); */
509 request_auto_timing_update(s
, 0);
513 static void create_stream_complete(pa_stream
*s
) {
515 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
516 pa_assert(s
->state
== PA_STREAM_CREATING
);
518 if (s
->buffer_attr_not_ready
|| s
->timing_info_not_ready
)
521 pa_stream_set_state(s
, PA_STREAM_READY
);
523 if (s
->requested_bytes
> 0 && s
->write_callback
)
524 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
526 if (s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) {
528 pa_gettimeofday(&tv
);
529 tv
.tv_usec
+= LATENCY_IPOL_INTERVAL_USEC
; /* every 100 ms */
530 pa_assert(!s
->auto_timing_update_event
);
531 s
->auto_timing_update_event
= s
->mainloop
->time_new(s
->mainloop
, &tv
, &auto_timing_update_callback
, s
);
535 static void automatic_buffer_attr(pa_buffer_attr
*attr
, pa_sample_spec
*ss
) {
539 attr
->tlength
= pa_bytes_per_second(ss
)/2;
540 attr
->maxlength
= (attr
->tlength
*3)/2;
541 attr
->minreq
= attr
->tlength
/50;
542 attr
->prebuf
= attr
->tlength
- attr
->minreq
;
543 attr
->fragsize
= attr
->tlength
/50;
546 void pa_create_stream_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
547 pa_stream
*s
= userdata
;
551 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
552 pa_assert(s
->state
== PA_STREAM_CREATING
);
556 if (command
!= PA_COMMAND_REPLY
) {
557 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
560 pa_stream_set_state(s
, PA_STREAM_FAILED
);
564 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
565 ((s
->direction
!= PA_STREAM_UPLOAD
) && pa_tagstruct_getu32(t
, &s
->stream_index
) < 0) ||
566 ((s
->direction
!= PA_STREAM_RECORD
) && pa_tagstruct_getu32(t
, &s
->requested_bytes
) < 0)) {
567 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
571 if (s
->context
->version
>= 9) {
572 if (s
->direction
== PA_STREAM_PLAYBACK
) {
573 if (pa_tagstruct_getu32(t
, &s
->buffer_attr
.maxlength
) < 0 ||
574 pa_tagstruct_getu32(t
, &s
->buffer_attr
.tlength
) < 0 ||
575 pa_tagstruct_getu32(t
, &s
->buffer_attr
.prebuf
) < 0 ||
576 pa_tagstruct_getu32(t
, &s
->buffer_attr
.minreq
) < 0) {
577 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
580 } else if (s
->direction
== PA_STREAM_RECORD
) {
581 if (pa_tagstruct_getu32(t
, &s
->buffer_attr
.maxlength
) < 0 ||
582 pa_tagstruct_getu32(t
, &s
->buffer_attr
.fragsize
) < 0) {
583 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
589 if (s
->context
->version
>= 12 && s
->direction
!= PA_STREAM_UPLOAD
) {
592 const char *dn
= NULL
;
595 if (pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
596 pa_tagstruct_get_channel_map(t
, &cm
) < 0 ||
597 pa_tagstruct_getu32(t
, &s
->device_index
) < 0 ||
598 pa_tagstruct_gets(t
, &dn
) < 0 ||
599 pa_tagstruct_get_boolean(t
, &suspended
) < 0) {
600 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
604 if (!dn
|| s
->device_index
== PA_INVALID_INDEX
||
605 ss
.channels
!= cm
.channels
||
606 !pa_channel_map_valid(&cm
) ||
607 !pa_sample_spec_valid(&ss
) ||
608 (!(s
->flags
& PA_STREAM_FIX_FORMAT
) && ss
.format
!= s
->sample_spec
.format
) ||
609 (!(s
->flags
& PA_STREAM_FIX_RATE
) && ss
.rate
!= s
->sample_spec
.rate
) ||
610 (!(s
->flags
& PA_STREAM_FIX_CHANNELS
) && !pa_channel_map_equal(&cm
, &s
->channel_map
))) {
611 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
615 pa_xfree(s
->device_name
);
616 s
->device_name
= pa_xstrdup(dn
);
617 s
->suspended
= suspended
;
619 if (!s
->manual_buffer_attr
&& pa_bytes_per_second(&ss
) != pa_bytes_per_second(&s
->sample_spec
)) {
623 automatic_buffer_attr(&attr
, &ss
);
625 /* If we need to update the buffer metrics, we wait for
626 * the the OK for that call before we go to
629 s
->state
= PA_STREAM_READY
;
630 pa_assert_se(o
= pa_stream_set_buffer_attr(s
, &attr
, NULL
, NULL
));
631 pa_operation_unref(o
);
632 s
->state
= PA_STREAM_CREATING
;
634 s
->buffer_attr_not_ready
= TRUE
;
641 if (!pa_tagstruct_eof(t
)) {
642 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
646 if (s
->direction
== PA_STREAM_RECORD
) {
647 pa_assert(!s
->record_memblockq
);
649 s
->record_memblockq
= pa_memblockq_new(
651 s
->buffer_attr
.maxlength
,
653 pa_frame_size(&s
->sample_spec
),
659 s
->channel_valid
= 1;
660 pa_dynarray_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, s
->channel
, s
);
662 if (s
->direction
!= PA_STREAM_UPLOAD
&& s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) {
664 /* If automatic timing updates are active, we wait for the
665 * first timing update before going to PA_STREAM_READY
668 s
->state
= PA_STREAM_READY
;
669 request_auto_timing_update(s
, 1);
670 s
->state
= PA_STREAM_CREATING
;
672 s
->timing_info_not_ready
= TRUE
;
675 create_stream_complete(s
);
681 static int create_stream(
682 pa_stream_direction_t direction
,
685 const pa_buffer_attr
*attr
,
686 pa_stream_flags_t flags
,
687 const pa_cvolume
*volume
,
688 pa_stream
*sync_stream
) {
694 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
696 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_UNCONNECTED
, PA_ERR_BADSTATE
);
697 PA_CHECK_VALIDITY(s
->context
, !(flags
& ~((direction
!= PA_STREAM_UPLOAD
?
698 PA_STREAM_START_CORKED
|
699 PA_STREAM_INTERPOLATE_TIMING
|
700 PA_STREAM_NOT_MONOTONOUS
|
701 PA_STREAM_AUTO_TIMING_UPDATE
|
702 PA_STREAM_NO_REMAP_CHANNELS
|
703 PA_STREAM_NO_REMIX_CHANNELS
|
704 PA_STREAM_FIX_FORMAT
|
706 PA_STREAM_FIX_CHANNELS
|
707 PA_STREAM_DONT_MOVE
: 0))), PA_ERR_INVALID
);
708 PA_CHECK_VALIDITY(s
->context
, !volume
|| volume
->channels
== s
->sample_spec
.channels
, PA_ERR_INVALID
);
709 PA_CHECK_VALIDITY(s
->context
, !sync_stream
|| (direction
== PA_STREAM_PLAYBACK
&& sync_stream
->direction
== PA_STREAM_PLAYBACK
), PA_ERR_INVALID
);
713 s
->direction
= direction
;
717 s
->syncid
= sync_stream
->syncid
;
720 s
->buffer_attr
= *attr
;
721 s
->manual_buffer_attr
= TRUE
;
723 /* half a second, with minimum request of 10 ms */
724 s
->buffer_attr
.tlength
= pa_bytes_per_second(&s
->sample_spec
)/2;
725 s
->buffer_attr
.maxlength
= (s
->buffer_attr
.tlength
*3)/2;
726 s
->buffer_attr
.minreq
= s
->buffer_attr
.tlength
/50;
727 s
->buffer_attr
.prebuf
= s
->buffer_attr
.tlength
- s
->buffer_attr
.minreq
;
728 s
->buffer_attr
.fragsize
= s
->buffer_attr
.tlength
/50;
729 s
->manual_buffer_attr
= FALSE
;
733 dev
= s
->direction
== PA_STREAM_PLAYBACK
? s
->context
->conf
->default_sink
: s
->context
->conf
->default_source
;
735 t
= pa_tagstruct_command(
737 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
,
742 PA_TAG_STRING
, s
->name
,
743 PA_TAG_SAMPLE_SPEC
, &s
->sample_spec
,
744 PA_TAG_CHANNEL_MAP
, &s
->channel_map
,
745 PA_TAG_U32
, PA_INVALID_INDEX
,
747 PA_TAG_U32
, s
->buffer_attr
.maxlength
,
748 PA_TAG_BOOLEAN
, !!(flags
& PA_STREAM_START_CORKED
),
751 if (s
->direction
== PA_STREAM_PLAYBACK
) {
756 PA_TAG_U32
, s
->buffer_attr
.tlength
,
757 PA_TAG_U32
, s
->buffer_attr
.prebuf
,
758 PA_TAG_U32
, s
->buffer_attr
.minreq
,
759 PA_TAG_U32
, s
->syncid
,
763 volume
= pa_cvolume_reset(&cv
, s
->sample_spec
.channels
);
765 pa_tagstruct_put_cvolume(t
, volume
);
767 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
769 if (s
->context
->version
>= 12 && s
->direction
!= PA_STREAM_UPLOAD
) {
772 PA_TAG_BOOLEAN
, flags
& PA_STREAM_NO_REMAP_CHANNELS
,
773 PA_TAG_BOOLEAN
, flags
& PA_STREAM_NO_REMIX_CHANNELS
,
774 PA_TAG_BOOLEAN
, flags
& PA_STREAM_FIX_FORMAT
,
775 PA_TAG_BOOLEAN
, flags
& PA_STREAM_FIX_RATE
,
776 PA_TAG_BOOLEAN
, flags
& PA_STREAM_FIX_CHANNELS
,
777 PA_TAG_BOOLEAN
, flags
& PA_STREAM_DONT_MOVE
,
778 PA_TAG_BOOLEAN
, flags
& PA_STREAM_VARIABLE_RATE
,
782 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
783 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
, NULL
);
785 pa_stream_set_state(s
, PA_STREAM_CREATING
);
791 int pa_stream_connect_playback(
794 const pa_buffer_attr
*attr
,
795 pa_stream_flags_t flags
,
797 pa_stream
*sync_stream
) {
800 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
802 return create_stream(PA_STREAM_PLAYBACK
, s
, dev
, attr
, flags
, volume
, sync_stream
);
805 int pa_stream_connect_record(
808 const pa_buffer_attr
*attr
,
809 pa_stream_flags_t flags
) {
812 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
814 return create_stream(PA_STREAM_RECORD
, s
, dev
, attr
, flags
, NULL
, NULL
);
821 void (*free_cb
)(void *p
),
823 pa_seek_mode_t seek
) {
828 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
831 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
832 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| s
->direction
== PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
833 PA_CHECK_VALIDITY(s
->context
, seek
<= PA_SEEK_RELATIVE_END
, PA_ERR_INVALID
);
834 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| (seek
== PA_SEEK_RELATIVE
&& offset
== 0), PA_ERR_INVALID
);
840 chunk
.memblock
= pa_memblock_new_user(s
->context
->mempool
, (void*) data
, length
, free_cb
, 1);
843 chunk
.memblock
= pa_memblock_new(s
->context
->mempool
, length
);
844 tdata
= pa_memblock_acquire(chunk
.memblock
);
845 memcpy(tdata
, data
, length
);
846 pa_memblock_release(chunk
.memblock
);
850 chunk
.length
= length
;
852 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, offset
, seek
, &chunk
);
853 pa_memblock_unref(chunk
.memblock
);
855 if (length
< s
->requested_bytes
)
856 s
->requested_bytes
-= length
;
858 s
->requested_bytes
= 0;
860 if (s
->direction
== PA_STREAM_PLAYBACK
) {
862 /* Update latency request correction */
863 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
) {
865 if (seek
== PA_SEEK_ABSOLUTE
) {
866 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 0;
867 s
->write_index_corrections
[s
->current_write_index_correction
].absolute
= 1;
868 s
->write_index_corrections
[s
->current_write_index_correction
].value
= offset
+ length
;
869 } else if (seek
== PA_SEEK_RELATIVE
) {
870 if (!s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
)
871 s
->write_index_corrections
[s
->current_write_index_correction
].value
+= offset
+ length
;
873 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 1;
876 /* Update the write index in the already available latency data */
877 if (s
->timing_info_valid
) {
879 if (seek
== PA_SEEK_ABSOLUTE
) {
880 s
->timing_info
.write_index_corrupt
= 0;
881 s
->timing_info
.write_index
= offset
+ length
;
882 } else if (seek
== PA_SEEK_RELATIVE
) {
883 if (!s
->timing_info
.write_index_corrupt
)
884 s
->timing_info
.write_index
+= offset
+ length
;
886 s
->timing_info
.write_index_corrupt
= 1;
889 if (!s
->timing_info_valid
|| s
->timing_info
.write_index_corrupt
)
890 request_auto_timing_update(s
, 1);
896 int pa_stream_peek(pa_stream
*s
, const void **data
, size_t *length
) {
898 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
902 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
903 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
905 if (!s
->peek_memchunk
.memblock
) {
907 if (pa_memblockq_peek(s
->record_memblockq
, &s
->peek_memchunk
) < 0) {
913 s
->peek_data
= pa_memblock_acquire(s
->peek_memchunk
.memblock
);
916 pa_assert(s
->peek_data
);
917 *data
= (uint8_t*) s
->peek_data
+ s
->peek_memchunk
.index
;
918 *length
= s
->peek_memchunk
.length
;
922 int pa_stream_drop(pa_stream
*s
) {
924 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
926 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
927 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
928 PA_CHECK_VALIDITY(s
->context
, s
->peek_memchunk
.memblock
, PA_ERR_BADSTATE
);
930 pa_memblockq_drop(s
->record_memblockq
, s
->peek_memchunk
.length
);
932 /* Fix the simulated local read index */
933 if (s
->timing_info_valid
&& !s
->timing_info
.read_index_corrupt
)
934 s
->timing_info
.read_index
+= s
->peek_memchunk
.length
;
936 pa_assert(s
->peek_data
);
937 pa_memblock_release(s
->peek_memchunk
.memblock
);
938 pa_memblock_unref(s
->peek_memchunk
.memblock
);
939 s
->peek_memchunk
.length
= 0;
940 s
->peek_memchunk
.index
= 0;
941 s
->peek_memchunk
.memblock
= NULL
;
946 size_t pa_stream_writable_size(pa_stream
*s
) {
948 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
950 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
951 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
953 return s
->requested_bytes
;
956 size_t pa_stream_readable_size(pa_stream
*s
) {
958 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
960 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
961 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
963 return pa_memblockq_get_length(s
->record_memblockq
);
966 pa_operation
* pa_stream_drain(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
972 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
974 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
975 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
977 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
979 t
= pa_tagstruct_command(s
->context
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
, &tag
);
980 pa_tagstruct_putu32(t
, s
->channel
);
981 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
982 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
);
987 static void stream_get_timing_info_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
988 pa_operation
*o
= userdata
;
989 struct timeval local
, remote
, now
;
994 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
996 if (!o
->context
|| !o
->stream
)
999 i
= &o
->stream
->timing_info
;
1001 /* pa_log("pre corrupt w:%u r:%u\n", !o->stream->timing_info_valid || i->write_index_corrupt,!o->stream->timing_info_valid || i->read_index_corrupt); */
1003 o
->stream
->timing_info_valid
= 0;
1004 i
->write_index_corrupt
= 0;
1005 i
->read_index_corrupt
= 0;
1007 /* pa_log("timing update %u\n", tag); */
1009 if (command
!= PA_COMMAND_REPLY
) {
1010 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
1013 } else if (pa_tagstruct_get_usec(t
, &i
->sink_usec
) < 0 ||
1014 pa_tagstruct_get_usec(t
, &i
->source_usec
) < 0 ||
1015 pa_tagstruct_get_boolean(t
, &i
->playing
) < 0 ||
1016 pa_tagstruct_get_timeval(t
, &local
) < 0 ||
1017 pa_tagstruct_get_timeval(t
, &remote
) < 0 ||
1018 pa_tagstruct_gets64(t
, &i
->write_index
) < 0 ||
1019 pa_tagstruct_gets64(t
, &i
->read_index
) < 0 ||
1020 !pa_tagstruct_eof(t
)) {
1021 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1025 o
->stream
->timing_info_valid
= 1;
1027 pa_gettimeofday(&now
);
1029 /* Calculcate timestamps */
1030 if (pa_timeval_cmp(&local
, &remote
) <= 0 && pa_timeval_cmp(&remote
, &now
) <= 0) {
1031 /* local and remote seem to have synchronized clocks */
1033 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
)
1034 i
->transport_usec
= pa_timeval_diff(&remote
, &local
);
1036 i
->transport_usec
= pa_timeval_diff(&now
, &remote
);
1038 i
->synchronized_clocks
= 1;
1039 i
->timestamp
= remote
;
1041 /* clocks are not synchronized, let's estimate latency then */
1042 i
->transport_usec
= pa_timeval_diff(&now
, &local
)/2;
1043 i
->synchronized_clocks
= 0;
1044 i
->timestamp
= local
;
1045 pa_timeval_add(&i
->timestamp
, i
->transport_usec
);
1048 /* Invalidate read and write indexes if necessary */
1049 if (tag
< o
->stream
->read_index_not_before
)
1050 i
->read_index_corrupt
= 1;
1052 if (tag
< o
->stream
->write_index_not_before
)
1053 i
->write_index_corrupt
= 1;
1055 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
1056 /* Write index correction */
1059 uint32_t ctag
= tag
;
1061 /* Go through the saved correction values and add up the total correction.*/
1063 for (n
= 0, j
= o
->stream
->current_write_index_correction
+1;
1064 n
< PA_MAX_WRITE_INDEX_CORRECTIONS
;
1065 n
++, j
= (j
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
) {
1067 /* Step over invalid data or out-of-date data */
1068 if (!o
->stream
->write_index_corrections
[j
].valid
||
1069 o
->stream
->write_index_corrections
[j
].tag
< ctag
)
1072 /* Make sure that everything is in order */
1073 ctag
= o
->stream
->write_index_corrections
[j
].tag
+1;
1075 /* Now fix the write index */
1076 if (o
->stream
->write_index_corrections
[j
].corrupt
) {
1077 /* A corrupting seek was made */
1079 i
->write_index_corrupt
= 1;
1080 } else if (o
->stream
->write_index_corrections
[j
].absolute
) {
1081 /* An absolute seek was made */
1082 i
->write_index
= o
->stream
->write_index_corrections
[j
].value
;
1083 i
->write_index_corrupt
= 0;
1084 } else if (!i
->write_index_corrupt
) {
1085 /* A relative seek was made */
1086 i
->write_index
+= o
->stream
->write_index_corrections
[j
].value
;
1091 if (o
->stream
->direction
== PA_STREAM_RECORD
) {
1092 /* Read index correction */
1094 if (!i
->read_index_corrupt
)
1095 i
->read_index
-= pa_memblockq_get_length(o
->stream
->record_memblockq
);
1098 o
->stream
->cached_time_valid
= 0;
1101 o
->stream
->auto_timing_update_requested
= 0;
1102 /* pa_log("post corrupt w:%u r:%u\n", i->write_index_corrupt || !o->stream->timing_info_valid, i->read_index_corrupt || !o->stream->timing_info_valid); */
1104 /* Clear old correction entries */
1105 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
1108 for (n
= 0; n
< PA_MAX_WRITE_INDEX_CORRECTIONS
; n
++) {
1109 if (!o
->stream
->write_index_corrections
[n
].valid
)
1112 if (o
->stream
->write_index_corrections
[n
].tag
<= tag
)
1113 o
->stream
->write_index_corrections
[n
].valid
= 0;
1117 /* First, let's complete the initialization, if necessary. */
1118 if (o
->stream
->state
== PA_STREAM_CREATING
) {
1119 o
->stream
->timing_info_not_ready
= FALSE
;
1120 create_stream_complete(o
->stream
);
1123 if (o
->stream
->latency_update_callback
)
1124 o
->stream
->latency_update_callback(o
->stream
, o
->stream
->latency_update_userdata
);
1126 if (o
->callback
&& o
->stream
&& o
->stream
->state
== PA_STREAM_READY
) {
1127 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
1128 cb(o
->stream
, o
->stream
->timing_info_valid
, o
->userdata
);
1133 pa_operation_done(o
);
1134 pa_operation_unref(o
);
1137 pa_operation
* pa_stream_update_timing_info(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1145 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1147 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1148 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1150 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1151 /* Find a place to store the write_index correction data for this entry */
1152 cidx
= (s
->current_write_index_correction
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
;
1154 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
1155 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !s
->write_index_corrections
[cidx
].valid
, PA_ERR_INTERNAL
);
1157 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1159 t
= pa_tagstruct_command(
1161 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_GET_PLAYBACK_LATENCY
: PA_COMMAND_GET_RECORD_LATENCY
,
1163 pa_tagstruct_putu32(t
, s
->channel
);
1164 pa_tagstruct_put_timeval(t
, pa_gettimeofday(&now
));
1166 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1167 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
);
1169 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1170 /* Fill in initial correction data */
1171 o
->stream
->current_write_index_correction
= cidx
;
1172 o
->stream
->write_index_corrections
[cidx
].valid
= 1;
1173 o
->stream
->write_index_corrections
[cidx
].tag
= tag
;
1174 o
->stream
->write_index_corrections
[cidx
].absolute
= 0;
1175 o
->stream
->write_index_corrections
[cidx
].value
= 0;
1176 o
->stream
->write_index_corrections
[cidx
].corrupt
= 0;
1179 /* pa_log("requesting update %u\n", tag); */
1184 void pa_stream_disconnect_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1185 pa_stream
*s
= userdata
;
1189 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1193 if (command
!= PA_COMMAND_REPLY
) {
1194 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
1197 pa_stream_set_state(s
, PA_STREAM_FAILED
);
1199 } else if (!pa_tagstruct_eof(t
)) {
1200 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
1204 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
1210 int pa_stream_disconnect(pa_stream
*s
) {
1215 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1217 PA_CHECK_VALIDITY(s
->context
, s
->channel_valid
, PA_ERR_BADSTATE
);
1218 PA_CHECK_VALIDITY(s
->context
, s
->context
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
1222 t
= pa_tagstruct_command(
1224 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
1225 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
),
1227 pa_tagstruct_putu32(t
, s
->channel
);
1228 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1229 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
, NULL
);
1235 void pa_stream_set_read_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
1237 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1239 s
->read_callback
= cb
;
1240 s
->read_userdata
= userdata
;
1243 void pa_stream_set_write_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
1245 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1247 s
->write_callback
= cb
;
1248 s
->write_userdata
= userdata
;
1251 void pa_stream_set_state_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1253 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1255 s
->state_callback
= cb
;
1256 s
->state_userdata
= userdata
;
1259 void pa_stream_set_overflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1261 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1263 s
->overflow_callback
= cb
;
1264 s
->overflow_userdata
= userdata
;
1267 void pa_stream_set_underflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1269 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1271 s
->underflow_callback
= cb
;
1272 s
->underflow_userdata
= userdata
;
1275 void pa_stream_set_latency_update_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1277 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1279 s
->latency_update_callback
= cb
;
1280 s
->latency_update_userdata
= userdata
;
1283 void pa_stream_set_moved_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1285 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1287 s
->moved_callback
= cb
;
1288 s
->moved_userdata
= userdata
;
1291 void pa_stream_set_suspended_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1293 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1295 s
->suspended_callback
= cb
;
1296 s
->suspended_userdata
= userdata
;
1299 void pa_stream_simple_ack_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1300 pa_operation
*o
= userdata
;
1305 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
1310 if (command
!= PA_COMMAND_REPLY
) {
1311 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
1315 } else if (!pa_tagstruct_eof(t
)) {
1316 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1321 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
1322 cb(o
->stream
, success
, o
->userdata
);
1326 pa_operation_done(o
);
1327 pa_operation_unref(o
);
1330 pa_operation
* pa_stream_cork(pa_stream
*s
, int b
, pa_stream_success_cb_t cb
, void *userdata
) {
1336 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1338 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1339 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1343 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1345 t
= pa_tagstruct_command(
1347 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CORK_PLAYBACK_STREAM
: PA_COMMAND_CORK_RECORD_STREAM
,
1349 pa_tagstruct_putu32(t
, s
->channel
);
1350 pa_tagstruct_put_boolean(t
, !!b
);
1351 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1352 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
);
1354 if (s
->direction
== PA_STREAM_PLAYBACK
)
1355 invalidate_indexes(s
, 1, 0);
1360 static pa_operation
* stream_send_simple_command(pa_stream
*s
, uint32_t command
, pa_stream_success_cb_t cb
, void *userdata
) {
1366 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1368 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1370 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1372 t
= pa_tagstruct_command(s
->context
, command
, &tag
);
1373 pa_tagstruct_putu32(t
, s
->channel
);
1374 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1375 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
);
1380 pa_operation
* pa_stream_flush(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1384 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1386 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1388 if ((o
= stream_send_simple_command(s
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_FLUSH_PLAYBACK_STREAM
: PA_COMMAND_FLUSH_RECORD_STREAM
, cb
, userdata
))) {
1390 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1391 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
)
1392 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 1;
1394 if (s
->timing_info_valid
)
1395 s
->timing_info
.write_index_corrupt
= 1;
1397 if (s
->buffer_attr
.prebuf
> 0)
1398 invalidate_indexes(s
, 1, 0);
1400 request_auto_timing_update(s
, 1);
1402 invalidate_indexes(s
, 0, 1);
1408 pa_operation
* pa_stream_prebuf(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1412 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1414 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1415 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
1417 if ((o
= stream_send_simple_command(s
, PA_COMMAND_PREBUF_PLAYBACK_STREAM
, cb
, userdata
)))
1418 invalidate_indexes(s
, 1, 0);
1423 pa_operation
* pa_stream_trigger(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1427 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1429 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1430 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
1432 if ((o
= stream_send_simple_command(s
, PA_COMMAND_TRIGGER_PLAYBACK_STREAM
, cb
, userdata
)))
1433 invalidate_indexes(s
, 1, 0);
1438 pa_operation
* pa_stream_set_name(pa_stream
*s
, const char *name
, pa_stream_success_cb_t cb
, void *userdata
) {
1444 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1447 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1448 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1450 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1452 t
= pa_tagstruct_command(
1454 s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_NAME
: PA_COMMAND_SET_PLAYBACK_STREAM_NAME
,
1456 pa_tagstruct_putu32(t
, s
->channel
);
1457 pa_tagstruct_puts(t
, name
);
1458 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1459 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
);
1464 int pa_stream_get_time(pa_stream
*s
, pa_usec_t
*r_usec
) {
1468 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1470 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1471 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1472 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1473 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
1474 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
1476 if (s
->cached_time_valid
)
1477 /* We alredy calculated the time value for this timing info, so let's reuse it */
1478 usec
= s
->cached_time
;
1480 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1481 /* The last byte that was written into the output device
1482 * had this time value associated */
1483 usec
= pa_bytes_to_usec(s
->timing_info
.read_index
< 0 ? 0 : (uint64_t) s
->timing_info
.read_index
, &s
->sample_spec
);
1486 /* Because the latency info took a little time to come
1487 * to us, we assume that the real output time is actually
1489 usec
+= s
->timing_info
.transport_usec
;
1491 /* However, the output device usually maintains a buffer
1492 too, hence the real sample currently played is a little
1494 if (s
->timing_info
.sink_usec
>= usec
)
1497 usec
-= s
->timing_info
.sink_usec
;
1500 } else if (s
->direction
== PA_STREAM_RECORD
) {
1501 /* The last byte written into the server side queue had
1502 * this time value associated */
1503 usec
= pa_bytes_to_usec(s
->timing_info
.write_index
< 0 ? 0 : (uint64_t) s
->timing_info
.write_index
, &s
->sample_spec
);
1506 /* Add transport latency */
1507 usec
+= s
->timing_info
.transport_usec
;
1509 /* Add latency of data in device buffer */
1510 usec
+= s
->timing_info
.source_usec
;
1512 /* If this is a monitor source, we need to correct the
1513 * time by the playback device buffer */
1514 if (s
->timing_info
.sink_usec
>= usec
)
1517 usec
-= s
->timing_info
.sink_usec
;
1521 s
->cached_time
= usec
;
1522 s
->cached_time_valid
= 1;
1525 /* Interpolate if requested */
1526 if (s
->flags
& PA_STREAM_INTERPOLATE_TIMING
) {
1528 /* We just add the time that passed since the latency info was
1530 if (!s
->corked
&& s
->timing_info
.playing
) {
1532 usec
+= pa_timeval_diff(pa_gettimeofday(&now
), &s
->timing_info
.timestamp
);
1536 /* Make sure the time runs monotonically */
1537 if (!(s
->flags
& PA_STREAM_NOT_MONOTONOUS
)) {
1538 if (usec
< s
->previous_time
)
1539 usec
= s
->previous_time
;
1541 s
->previous_time
= usec
;
1550 static pa_usec_t
time_counter_diff(pa_stream
*s
, pa_usec_t a
, pa_usec_t b
, int *negative
) {
1552 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1560 if (negative
&& s
->direction
== PA_STREAM_RECORD
) {
1568 int pa_stream_get_latency(pa_stream
*s
, pa_usec_t
*r_usec
, int *negative
) {
1574 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1577 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1578 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1579 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1580 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
1581 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
1583 if ((r
= pa_stream_get_time(s
, &t
)) < 0)
1586 if (s
->direction
== PA_STREAM_PLAYBACK
)
1587 cindex
= s
->timing_info
.write_index
;
1589 cindex
= s
->timing_info
.read_index
;
1594 c
= pa_bytes_to_usec(cindex
, &s
->sample_spec
);
1596 if (s
->direction
== PA_STREAM_PLAYBACK
)
1597 *r_usec
= time_counter_diff(s
, c
, t
, negative
);
1599 *r_usec
= time_counter_diff(s
, t
, c
, negative
);
1604 const pa_timing_info
* pa_stream_get_timing_info(pa_stream
*s
) {
1606 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1608 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1609 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1610 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->timing_info_valid
, PA_ERR_BADSTATE
);
1612 return &s
->timing_info
;
1615 const pa_sample_spec
* pa_stream_get_sample_spec(pa_stream
*s
) {
1617 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1619 return &s
->sample_spec
;
1622 const pa_channel_map
* pa_stream_get_channel_map(pa_stream
*s
) {
1624 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1626 return &s
->channel_map
;
1629 const pa_buffer_attr
* pa_stream_get_buffer_attr(pa_stream
*s
) {
1631 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1633 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1634 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1635 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->context
->version
>= 9, PA_ERR_NODATA
);
1637 return &s
->buffer_attr
;
1640 static void stream_set_buffer_attr_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1641 pa_operation
*o
= userdata
;
1646 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
1651 if (command
!= PA_COMMAND_REPLY
) {
1652 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
1658 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
1659 if (pa_tagstruct_getu32(t
, &o
->stream
->buffer_attr
.maxlength
) < 0 ||
1660 pa_tagstruct_getu32(t
, &o
->stream
->buffer_attr
.tlength
) < 0 ||
1661 pa_tagstruct_getu32(t
, &o
->stream
->buffer_attr
.prebuf
) < 0 ||
1662 pa_tagstruct_getu32(t
, &o
->stream
->buffer_attr
.minreq
) < 0) {
1663 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1666 } else if (o
->stream
->direction
== PA_STREAM_RECORD
) {
1667 if (pa_tagstruct_getu32(t
, &o
->stream
->buffer_attr
.maxlength
) < 0 ||
1668 pa_tagstruct_getu32(t
, &o
->stream
->buffer_attr
.fragsize
) < 0) {
1669 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1674 if (!pa_tagstruct_eof(t
)) {
1675 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1679 o
->stream
->manual_buffer_attr
= TRUE
;
1682 if (o
->stream
->state
== PA_STREAM_CREATING
) {
1683 o
->stream
->buffer_attr_not_ready
= FALSE
;
1684 create_stream_complete(o
->stream
);
1688 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
1689 cb(o
->stream
, success
, o
->userdata
);
1693 pa_operation_done(o
);
1694 pa_operation_unref(o
);
1698 pa_operation
* pa_stream_set_buffer_attr(pa_stream
*s
, const pa_buffer_attr
*attr
, pa_stream_success_cb_t cb
, void *userdata
) {
1704 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1707 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1708 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1709 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->context
->version
>= 12, PA_ERR_NOTSUPPORTED
);
1711 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1713 t
= pa_tagstruct_command(
1715 s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR
: PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR
,
1717 pa_tagstruct_putu32(t
, s
->channel
);
1719 pa_tagstruct_putu32(t
, attr
->maxlength
);
1721 if (s
->direction
== PA_STREAM_PLAYBACK
)
1724 PA_TAG_U32
, attr
->tlength
,
1725 PA_TAG_U32
, attr
->prebuf
,
1726 PA_TAG_U32
, attr
->minreq
,
1729 pa_tagstruct_putu32(t
, attr
->fragsize
);
1731 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1732 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
);
1737 uint32_t pa_stream_get_device_index(pa_stream
*s
) {
1739 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1741 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
1742 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
1743 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->context
->version
>= 12, PA_ERR_NOTSUPPORTED
, PA_INVALID_INDEX
);
1744 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->device_index
!= PA_INVALID_INDEX
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
1746 return s
->device_index
;
1749 const char *pa_stream_get_device_name(pa_stream
*s
) {
1751 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1753 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1754 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1755 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->context
->version
>= 12, PA_ERR_NOTSUPPORTED
);
1756 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->device_name
, PA_ERR_BADSTATE
);
1758 return s
->device_name
;
1761 int pa_stream_is_suspended(pa_stream
*s
) {
1763 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1765 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1766 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1767 PA_CHECK_VALIDITY(s
->context
, s
->context
->version
>= 12, PA_ERR_NOTSUPPORTED
);
1769 return s
->suspended
;
1772 static void stream_update_sample_rate_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1773 pa_operation
*o
= userdata
;
1778 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
1783 if (command
!= PA_COMMAND_REPLY
) {
1784 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
1790 if (!pa_tagstruct_eof(t
)) {
1791 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1796 o
->stream
->sample_spec
.rate
= PA_PTR_TO_UINT(o
->private);
1797 pa_assert(pa_sample_spec_valid(&o
->stream
->sample_spec
));
1800 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
1801 cb(o
->stream
, success
, o
->userdata
);
1805 pa_operation_done(o
);
1806 pa_operation_unref(o
);
1810 pa_operation
*pa_stream_update_sample_rate(pa_stream
*s
, uint32_t rate
, pa_stream_success_cb_t cb
, void *userdata
) {
1816 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1818 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, rate
> 0 && rate
<= PA_RATE_MAX
, PA_ERR_INVALID
);
1819 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1820 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1821 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->flags
& PA_STREAM_VARIABLE_RATE
, PA_ERR_BADSTATE
);
1822 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->context
->version
>= 12, PA_ERR_NOTSUPPORTED
);
1824 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1825 o
->private = PA_UINT_TO_PTR(rate
);
1827 t
= pa_tagstruct_command(
1829 s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE
: PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE
,
1831 pa_tagstruct_putu32(t
, s
->channel
);
1832 pa_tagstruct_putu32(t
, rate
);
1834 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1835 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
);