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
34 #include <pulse/def.h>
35 #include <pulse/timeval.h>
36 #include <pulse/xmalloc.h>
38 #include <pulsecore/pstream-util.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/hashmap.h>
41 #include <pulsecore/macro.h>
45 #define LATENCY_IPOL_INTERVAL_USEC (100000L)
47 pa_stream
*pa_stream_new(pa_context
*c
, const char *name
, const pa_sample_spec
*ss
, const pa_channel_map
*map
) {
53 PA_CHECK_VALIDITY_RETURN_NULL(c
, ss
&& pa_sample_spec_valid(ss
), PA_ERR_INVALID
);
54 PA_CHECK_VALIDITY_RETURN_NULL(c
, !map
|| (pa_channel_map_valid(map
) && map
->channels
== ss
->channels
), PA_ERR_INVALID
);
56 s
= pa_xnew(pa_stream
, 1);
59 s
->mainloop
= c
->mainloop
;
61 s
->read_callback
= NULL
;
62 s
->read_userdata
= NULL
;
63 s
->write_callback
= NULL
;
64 s
->write_userdata
= NULL
;
65 s
->state_callback
= NULL
;
66 s
->state_userdata
= NULL
;
67 s
->overflow_callback
= NULL
;
68 s
->overflow_userdata
= NULL
;
69 s
->underflow_callback
= NULL
;
70 s
->underflow_userdata
= NULL
;
71 s
->latency_update_callback
= NULL
;
72 s
->latency_update_userdata
= NULL
;
74 s
->direction
= PA_STREAM_NODIRECTION
;
75 s
->name
= pa_xstrdup(name
);
80 s
->channel_map
= *map
;
82 pa_channel_map_init_auto(&s
->channel_map
, ss
->channels
, PA_CHANNEL_MAP_DEFAULT
);
86 s
->syncid
= c
->csyncid
++;
87 s
->device_index
= PA_INVALID_INDEX
;
88 s
->requested_bytes
= 0;
89 s
->state
= PA_STREAM_UNCONNECTED
;
90 memset(&s
->buffer_attr
, 0, sizeof(s
->buffer_attr
));
92 s
->peek_memchunk
.index
= 0;
93 s
->peek_memchunk
.length
= 0;
94 s
->peek_memchunk
.memblock
= NULL
;
97 s
->record_memblockq
= NULL
;
100 s
->timing_info_valid
= 0;
101 s
->read_index_not_before
= 0;
102 s
->write_index_not_before
= 0;
104 for (i
= 0; i
< PA_MAX_WRITE_INDEX_CORRECTIONS
; i
++)
105 s
->write_index_corrections
[i
].valid
= 0;
106 s
->current_write_index_correction
= 0;
110 s
->cached_time_valid
= 0;
112 s
->auto_timing_update_event
= NULL
;
113 s
->auto_timing_update_requested
= 0;
115 /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
116 PA_LLIST_PREPEND(pa_stream
, c
->streams
, s
);
122 static void stream_free(pa_stream
*s
) {
123 assert(s
&& !s
->context
&& !s
->channel_valid
);
125 if (s
->auto_timing_update_event
) {
127 s
->mainloop
->time_free(s
->auto_timing_update_event
);
130 if (s
->peek_memchunk
.memblock
) {
132 pa_memblock_release(s
->peek_memchunk
.memblock
);
133 pa_memblock_unref(s
->peek_memchunk
.memblock
);
136 if (s
->record_memblockq
)
137 pa_memblockq_free(s
->record_memblockq
);
143 void pa_stream_unref(pa_stream
*s
) {
151 pa_stream
* pa_stream_ref(pa_stream
*s
) {
159 pa_stream_state_t
pa_stream_get_state(pa_stream
*s
) {
166 pa_context
* pa_stream_get_context(pa_stream
*s
) {
173 uint32_t pa_stream_get_index(pa_stream
*s
) {
177 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
179 return s
->device_index
;
182 void pa_stream_set_state(pa_stream
*s
, pa_stream_state_t st
) {
192 if (s
->state_callback
)
193 s
->state_callback(s
, s
->state_userdata
);
195 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
) && s
->context
) {
197 /* Detach from context */
200 /* Unref all operatio object that point to us */
201 for (o
= s
->context
->operations
; o
; o
= n
) {
205 pa_operation_cancel(o
);
208 /* Drop all outstanding replies for this stream */
209 if (s
->context
->pdispatch
)
210 pa_pdispatch_unregister_reply(s
->context
->pdispatch
, s
);
212 if (s
->channel_valid
)
213 pa_dynarray_put((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, s
->channel
, NULL
);
215 PA_LLIST_REMOVE(pa_stream
, s
->context
->streams
, s
);
219 s
->channel_valid
= 0;
223 s
->read_callback
= NULL
;
224 s
->write_callback
= NULL
;
225 s
->state_callback
= NULL
;
226 s
->overflow_callback
= NULL
;
227 s
->underflow_callback
= NULL
;
228 s
->latency_update_callback
= NULL
;
234 void pa_command_stream_killed(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
235 pa_context
*c
= userdata
;
240 assert(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
);
246 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
247 !pa_tagstruct_eof(t
)) {
248 pa_context_fail(c
, PA_ERR_PROTOCOL
);
252 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, channel
)))
255 pa_context_set_error(c
, PA_ERR_KILLED
);
256 pa_stream_set_state(s
, PA_STREAM_FAILED
);
262 void pa_command_request(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
264 pa_context
*c
= userdata
;
265 uint32_t bytes
, channel
;
268 assert(command
== PA_COMMAND_REQUEST
);
274 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
275 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
276 !pa_tagstruct_eof(t
)) {
277 pa_context_fail(c
, PA_ERR_PROTOCOL
);
281 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
284 if (s
->state
== PA_STREAM_READY
) {
285 s
->requested_bytes
+= bytes
;
287 if (s
->requested_bytes
> 0 && s
->write_callback
)
288 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
295 void pa_command_overflow_or_underflow(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
297 pa_context
*c
= userdata
;
301 assert(command
== PA_COMMAND_OVERFLOW
|| command
== PA_COMMAND_UNDERFLOW
);
307 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
308 !pa_tagstruct_eof(t
)) {
309 pa_context_fail(c
, PA_ERR_PROTOCOL
);
313 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
316 if (s
->state
== PA_STREAM_READY
) {
318 if (command
== PA_COMMAND_OVERFLOW
) {
319 if (s
->overflow_callback
)
320 s
->overflow_callback(s
, s
->overflow_userdata
);
321 } else if (command
== PA_COMMAND_UNDERFLOW
) {
322 if (s
->underflow_callback
)
323 s
->underflow_callback(s
, s
->underflow_userdata
);
331 static void request_auto_timing_update(pa_stream
*s
, int force
) {
334 if (!(s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
))
337 if (s
->state
== PA_STREAM_READY
&&
338 (force
|| !s
->auto_timing_update_requested
)) {
341 /* pa_log("automatically requesting new timing data"); */
343 if ((o
= pa_stream_update_timing_info(s
, NULL
, NULL
))) {
344 pa_operation_unref(o
);
345 s
->auto_timing_update_requested
= 1;
349 if (s
->auto_timing_update_event
) {
351 pa_gettimeofday(&next
);
352 pa_timeval_add(&next
, LATENCY_IPOL_INTERVAL_USEC
);
353 s
->mainloop
->time_restart(s
->auto_timing_update_event
, &next
);
357 static void invalidate_indexes(pa_stream
*s
, int r
, int w
) {
360 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
362 if (s
->state
!= PA_STREAM_READY
)
366 s
->write_index_not_before
= s
->context
->ctag
;
368 if (s
->timing_info_valid
)
369 s
->timing_info
.write_index_corrupt
= 1;
371 /* pa_log("write_index invalidated"); */
375 s
->read_index_not_before
= s
->context
->ctag
;
377 if (s
->timing_info_valid
)
378 s
->timing_info
.read_index_corrupt
= 1;
380 /* pa_log("read_index invalidated"); */
383 if ((s
->direction
== PA_STREAM_PLAYBACK
&& r
) ||
384 (s
->direction
== PA_STREAM_RECORD
&& w
))
385 s
->cached_time_valid
= 0;
387 request_auto_timing_update(s
, 1);
390 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
) {
391 pa_stream
*s
= userdata
;
393 /* pa_log("time event"); */
396 request_auto_timing_update(s
, 0);
400 static void create_stream_complete(pa_stream
*s
) {
402 pa_assert(s
->state
== PA_STREAM_CREATING
);
404 pa_stream_set_state(s
, PA_STREAM_READY
);
406 if (s
->requested_bytes
> 0 && s
->write_callback
)
407 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
409 if (s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) {
411 pa_gettimeofday(&tv
);
412 tv
.tv_usec
+= LATENCY_IPOL_INTERVAL_USEC
; /* every 100 ms */
413 assert(!s
->auto_timing_update_event
);
414 s
->auto_timing_update_event
= s
->mainloop
->time_new(s
->mainloop
, &tv
, &auto_timing_update_callback
, s
);
418 void pa_create_stream_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
419 pa_stream
*s
= userdata
;
423 assert(s
->state
== PA_STREAM_CREATING
);
427 if (command
!= PA_COMMAND_REPLY
) {
428 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
431 pa_stream_set_state(s
, PA_STREAM_FAILED
);
435 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
436 ((s
->direction
!= PA_STREAM_UPLOAD
) && pa_tagstruct_getu32(t
, &s
->device_index
) < 0) ||
437 ((s
->direction
!= PA_STREAM_RECORD
) && pa_tagstruct_getu32(t
, &s
->requested_bytes
) < 0)) {
438 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
442 if (pa_context_get_server_protocol_version(s
->context
) >= 9) {
443 if (s
->direction
== PA_STREAM_PLAYBACK
) {
444 if (pa_tagstruct_getu32(t
, &s
->buffer_attr
.maxlength
) < 0 ||
445 pa_tagstruct_getu32(t
, &s
->buffer_attr
.tlength
) < 0 ||
446 pa_tagstruct_getu32(t
, &s
->buffer_attr
.prebuf
) < 0 ||
447 pa_tagstruct_getu32(t
, &s
->buffer_attr
.minreq
) < 0) {
448 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
451 } else if (s
->direction
== PA_STREAM_RECORD
) {
452 if (pa_tagstruct_getu32(t
, &s
->buffer_attr
.maxlength
) < 0 ||
453 pa_tagstruct_getu32(t
, &s
->buffer_attr
.fragsize
) < 0) {
454 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
460 if (!pa_tagstruct_eof(t
)) {
461 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
465 if (s
->direction
== PA_STREAM_RECORD
) {
466 assert(!s
->record_memblockq
);
468 s
->record_memblockq
= pa_memblockq_new(
470 s
->buffer_attr
.maxlength
,
472 pa_frame_size(&s
->sample_spec
),
478 s
->channel_valid
= 1;
479 pa_dynarray_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, s
->channel
, s
);
481 if (s
->direction
!= PA_STREAM_UPLOAD
&& s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) {
482 /* If automatic timing updates are active, we wait for the
483 * first timing update before going to PA_STREAM_READY
485 s
->state
= PA_STREAM_READY
;
486 request_auto_timing_update(s
, 1);
487 s
->state
= PA_STREAM_CREATING
;
490 create_stream_complete(s
);
496 static int create_stream(
497 pa_stream_direction_t direction
,
500 const pa_buffer_attr
*attr
,
501 pa_stream_flags_t flags
,
502 const pa_cvolume
*volume
,
503 pa_stream
*sync_stream
) {
511 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_UNCONNECTED
, PA_ERR_BADSTATE
);
512 PA_CHECK_VALIDITY(s
->context
, !(flags
& ~((direction
!= PA_STREAM_UPLOAD
?
513 PA_STREAM_START_CORKED
|
514 PA_STREAM_INTERPOLATE_TIMING
|
515 PA_STREAM_NOT_MONOTONOUS
|
516 PA_STREAM_AUTO_TIMING_UPDATE
: 0))), PA_ERR_INVALID
);
517 PA_CHECK_VALIDITY(s
->context
, !volume
|| volume
->channels
== s
->sample_spec
.channels
, PA_ERR_INVALID
);
518 PA_CHECK_VALIDITY(s
->context
, !sync_stream
|| (direction
== PA_STREAM_PLAYBACK
&& sync_stream
->direction
== PA_STREAM_PLAYBACK
), PA_ERR_INVALID
);
522 s
->direction
= direction
;
526 s
->syncid
= sync_stream
->syncid
;
529 s
->buffer_attr
= *attr
;
532 s
->buffer_attr
.tlength
= pa_bytes_per_second(&s
->sample_spec
)/2;
533 s
->buffer_attr
.maxlength
= (s
->buffer_attr
.tlength
*3)/2;
534 s
->buffer_attr
.minreq
= s
->buffer_attr
.tlength
/100;
535 s
->buffer_attr
.prebuf
= s
->buffer_attr
.tlength
- s
->buffer_attr
.minreq
;
536 s
->buffer_attr
.fragsize
= s
->buffer_attr
.tlength
/100;
540 dev
= s
->direction
== PA_STREAM_PLAYBACK
? s
->context
->conf
->default_sink
: s
->context
->conf
->default_source
;
542 t
= pa_tagstruct_command(
544 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
,
549 PA_TAG_STRING
, s
->name
,
550 PA_TAG_SAMPLE_SPEC
, &s
->sample_spec
,
551 PA_TAG_CHANNEL_MAP
, &s
->channel_map
,
552 PA_TAG_U32
, PA_INVALID_INDEX
,
554 PA_TAG_U32
, s
->buffer_attr
.maxlength
,
555 PA_TAG_BOOLEAN
, !!(flags
& PA_STREAM_START_CORKED
),
558 if (s
->direction
== PA_STREAM_PLAYBACK
) {
563 PA_TAG_U32
, s
->buffer_attr
.tlength
,
564 PA_TAG_U32
, s
->buffer_attr
.prebuf
,
565 PA_TAG_U32
, s
->buffer_attr
.minreq
,
566 PA_TAG_U32
, s
->syncid
,
570 volume
= pa_cvolume_reset(&cv
, s
->sample_spec
.channels
);
572 pa_tagstruct_put_cvolume(t
, volume
);
574 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
576 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
577 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
, NULL
);
579 pa_stream_set_state(s
, PA_STREAM_CREATING
);
585 int pa_stream_connect_playback(
588 const pa_buffer_attr
*attr
,
589 pa_stream_flags_t flags
,
591 pa_stream
*sync_stream
) {
596 return create_stream(PA_STREAM_PLAYBACK
, s
, dev
, attr
, flags
, volume
, sync_stream
);
599 int pa_stream_connect_record(
602 const pa_buffer_attr
*attr
,
603 pa_stream_flags_t flags
) {
608 return create_stream(PA_STREAM_RECORD
, s
, dev
, attr
, flags
, NULL
, NULL
);
615 void (*free_cb
)(void *p
),
617 pa_seek_mode_t seek
) {
625 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
626 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| s
->direction
== PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
627 PA_CHECK_VALIDITY(s
->context
, seek
<= PA_SEEK_RELATIVE_END
, PA_ERR_INVALID
);
628 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| (seek
== PA_SEEK_RELATIVE
&& offset
== 0), PA_ERR_INVALID
);
634 chunk
.memblock
= pa_memblock_new_user(s
->context
->mempool
, (void*) data
, length
, free_cb
, 1);
637 chunk
.memblock
= pa_memblock_new(s
->context
->mempool
, length
);
638 tdata
= pa_memblock_acquire(chunk
.memblock
);
639 memcpy(tdata
, data
, length
);
640 pa_memblock_release(chunk
.memblock
);
644 chunk
.length
= length
;
646 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, offset
, seek
, &chunk
);
647 pa_memblock_unref(chunk
.memblock
);
649 if (length
< s
->requested_bytes
)
650 s
->requested_bytes
-= length
;
652 s
->requested_bytes
= 0;
654 if (s
->direction
== PA_STREAM_PLAYBACK
) {
656 /* Update latency request correction */
657 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
) {
659 if (seek
== PA_SEEK_ABSOLUTE
) {
660 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 0;
661 s
->write_index_corrections
[s
->current_write_index_correction
].absolute
= 1;
662 s
->write_index_corrections
[s
->current_write_index_correction
].value
= offset
+ length
;
663 } else if (seek
== PA_SEEK_RELATIVE
) {
664 if (!s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
)
665 s
->write_index_corrections
[s
->current_write_index_correction
].value
+= offset
+ length
;
667 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 1;
670 /* Update the write index in the already available latency data */
671 if (s
->timing_info_valid
) {
673 if (seek
== PA_SEEK_ABSOLUTE
) {
674 s
->timing_info
.write_index_corrupt
= 0;
675 s
->timing_info
.write_index
= offset
+ length
;
676 } else if (seek
== PA_SEEK_RELATIVE
) {
677 if (!s
->timing_info
.write_index_corrupt
)
678 s
->timing_info
.write_index
+= offset
+ length
;
680 s
->timing_info
.write_index_corrupt
= 1;
683 if (!s
->timing_info_valid
|| s
->timing_info
.write_index_corrupt
)
684 request_auto_timing_update(s
, 1);
690 int pa_stream_peek(pa_stream
*s
, const void **data
, size_t *length
) {
696 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
697 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
699 if (!s
->peek_memchunk
.memblock
) {
701 if (pa_memblockq_peek(s
->record_memblockq
, &s
->peek_memchunk
) < 0) {
707 s
->peek_data
= pa_memblock_acquire(s
->peek_memchunk
.memblock
);
710 assert(s
->peek_data
);
711 *data
= (uint8_t*) s
->peek_data
+ s
->peek_memchunk
.index
;
712 *length
= s
->peek_memchunk
.length
;
716 int pa_stream_drop(pa_stream
*s
) {
720 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
721 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
722 PA_CHECK_VALIDITY(s
->context
, s
->peek_memchunk
.memblock
, PA_ERR_BADSTATE
);
724 pa_memblockq_drop(s
->record_memblockq
, s
->peek_memchunk
.length
);
726 /* Fix the simulated local read index */
727 if (s
->timing_info_valid
&& !s
->timing_info
.read_index_corrupt
)
728 s
->timing_info
.read_index
+= s
->peek_memchunk
.length
;
730 assert(s
->peek_data
);
731 pa_memblock_release(s
->peek_memchunk
.memblock
);
732 pa_memblock_unref(s
->peek_memchunk
.memblock
);
733 s
->peek_memchunk
.length
= 0;
734 s
->peek_memchunk
.index
= 0;
735 s
->peek_memchunk
.memblock
= NULL
;
740 size_t pa_stream_writable_size(pa_stream
*s
) {
744 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
745 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
747 return s
->requested_bytes
;
750 size_t pa_stream_readable_size(pa_stream
*s
) {
754 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
755 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
757 return pa_memblockq_get_length(s
->record_memblockq
);
760 pa_operation
* pa_stream_drain(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
768 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
769 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
771 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
773 t
= pa_tagstruct_command(s
->context
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
, &tag
);
774 pa_tagstruct_putu32(t
, s
->channel
);
775 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
776 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
);
781 static void stream_get_timing_info_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
782 pa_operation
*o
= userdata
;
783 struct timeval local
, remote
, now
;
789 if (!o
->context
|| !o
->stream
)
792 i
= &o
->stream
->timing_info
;
794 /* 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); */
796 o
->stream
->timing_info_valid
= 0;
797 i
->write_index_corrupt
= 0;
798 i
->read_index_corrupt
= 0;
800 /* pa_log("timing update %u\n", tag); */
802 if (command
!= PA_COMMAND_REPLY
) {
803 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
806 } else if (pa_tagstruct_get_usec(t
, &i
->sink_usec
) < 0 ||
807 pa_tagstruct_get_usec(t
, &i
->source_usec
) < 0 ||
808 pa_tagstruct_get_boolean(t
, &i
->playing
) < 0 ||
809 pa_tagstruct_get_timeval(t
, &local
) < 0 ||
810 pa_tagstruct_get_timeval(t
, &remote
) < 0 ||
811 pa_tagstruct_gets64(t
, &i
->write_index
) < 0 ||
812 pa_tagstruct_gets64(t
, &i
->read_index
) < 0 ||
813 !pa_tagstruct_eof(t
)) {
814 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
818 o
->stream
->timing_info_valid
= 1;
820 pa_gettimeofday(&now
);
822 /* Calculcate timestamps */
823 if (pa_timeval_cmp(&local
, &remote
) <= 0 && pa_timeval_cmp(&remote
, &now
) <= 0) {
824 /* local and remote seem to have synchronized clocks */
826 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
)
827 i
->transport_usec
= pa_timeval_diff(&remote
, &local
);
829 i
->transport_usec
= pa_timeval_diff(&now
, &remote
);
831 i
->synchronized_clocks
= 1;
832 i
->timestamp
= remote
;
834 /* clocks are not synchronized, let's estimate latency then */
835 i
->transport_usec
= pa_timeval_diff(&now
, &local
)/2;
836 i
->synchronized_clocks
= 0;
837 i
->timestamp
= local
;
838 pa_timeval_add(&i
->timestamp
, i
->transport_usec
);
841 /* Invalidate read and write indexes if necessary */
842 if (tag
< o
->stream
->read_index_not_before
)
843 i
->read_index_corrupt
= 1;
845 if (tag
< o
->stream
->write_index_not_before
)
846 i
->write_index_corrupt
= 1;
848 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
849 /* Write index correction */
854 /* Go through the saved correction values and add up the total correction.*/
856 for (n
= 0, j
= o
->stream
->current_write_index_correction
+1;
857 n
< PA_MAX_WRITE_INDEX_CORRECTIONS
;
858 n
++, j
= (j
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
) {
860 /* Step over invalid data or out-of-date data */
861 if (!o
->stream
->write_index_corrections
[j
].valid
||
862 o
->stream
->write_index_corrections
[j
].tag
< ctag
)
865 /* Make sure that everything is in order */
866 ctag
= o
->stream
->write_index_corrections
[j
].tag
+1;
868 /* Now fix the write index */
869 if (o
->stream
->write_index_corrections
[j
].corrupt
) {
870 /* A corrupting seek was made */
872 i
->write_index_corrupt
= 1;
873 } else if (o
->stream
->write_index_corrections
[j
].absolute
) {
874 /* An absolute seek was made */
875 i
->write_index
= o
->stream
->write_index_corrections
[j
].value
;
876 i
->write_index_corrupt
= 0;
877 } else if (!i
->write_index_corrupt
) {
878 /* A relative seek was made */
879 i
->write_index
+= o
->stream
->write_index_corrections
[j
].value
;
884 if (o
->stream
->direction
== PA_STREAM_RECORD
) {
885 /* Read index correction */
887 if (!i
->read_index_corrupt
)
888 i
->read_index
-= pa_memblockq_get_length(o
->stream
->record_memblockq
);
891 o
->stream
->cached_time_valid
= 0;
894 o
->stream
->auto_timing_update_requested
= 0;
895 /* 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); */
897 /* Clear old correction entries */
898 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
901 for (n
= 0; n
< PA_MAX_WRITE_INDEX_CORRECTIONS
; n
++) {
902 if (!o
->stream
->write_index_corrections
[n
].valid
)
905 if (o
->stream
->write_index_corrections
[n
].tag
<= tag
)
906 o
->stream
->write_index_corrections
[n
].valid
= 0;
910 /* First, let's complete the initialization, if necessary. */
911 if (o
->stream
->state
== PA_STREAM_CREATING
)
912 create_stream_complete(o
->stream
);
914 if (o
->stream
->latency_update_callback
)
915 o
->stream
->latency_update_callback(o
->stream
, o
->stream
->latency_update_userdata
);
917 if (o
->callback
&& o
->stream
&& o
->stream
->state
== PA_STREAM_READY
) {
918 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
919 cb(o
->stream
, o
->stream
->timing_info_valid
, o
->userdata
);
924 pa_operation_done(o
);
925 pa_operation_unref(o
);
928 pa_operation
* pa_stream_update_timing_info(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
938 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
939 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
941 if (s
->direction
== PA_STREAM_PLAYBACK
) {
942 /* Find a place to store the write_index correction data for this entry */
943 cidx
= (s
->current_write_index_correction
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
;
945 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
946 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !s
->write_index_corrections
[cidx
].valid
, PA_ERR_INTERNAL
);
948 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
950 t
= pa_tagstruct_command(
952 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_GET_PLAYBACK_LATENCY
: PA_COMMAND_GET_RECORD_LATENCY
,
954 pa_tagstruct_putu32(t
, s
->channel
);
955 pa_tagstruct_put_timeval(t
, pa_gettimeofday(&now
));
957 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
958 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
);
960 if (s
->direction
== PA_STREAM_PLAYBACK
) {
961 /* Fill in initial correction data */
962 o
->stream
->current_write_index_correction
= cidx
;
963 o
->stream
->write_index_corrections
[cidx
].valid
= 1;
964 o
->stream
->write_index_corrections
[cidx
].tag
= tag
;
965 o
->stream
->write_index_corrections
[cidx
].absolute
= 0;
966 o
->stream
->write_index_corrections
[cidx
].value
= 0;
967 o
->stream
->write_index_corrections
[cidx
].corrupt
= 0;
970 /* pa_log("requesting update %u\n", tag); */
975 void pa_stream_disconnect_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
976 pa_stream
*s
= userdata
;
984 if (command
!= PA_COMMAND_REPLY
) {
985 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
988 pa_stream_set_state(s
, PA_STREAM_FAILED
);
990 } else if (!pa_tagstruct_eof(t
)) {
991 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
995 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
1001 int pa_stream_disconnect(pa_stream
*s
) {
1006 assert(s
->ref
>= 1);
1008 PA_CHECK_VALIDITY(s
->context
, s
->channel_valid
, PA_ERR_BADSTATE
);
1009 PA_CHECK_VALIDITY(s
->context
, s
->context
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
1013 t
= pa_tagstruct_command(
1015 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
1016 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
),
1018 pa_tagstruct_putu32(t
, s
->channel
);
1019 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1020 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
, NULL
);
1026 void pa_stream_set_read_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
1028 assert(s
->ref
>= 1);
1030 s
->read_callback
= cb
;
1031 s
->read_userdata
= userdata
;
1034 void pa_stream_set_write_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
1036 assert(s
->ref
>= 1);
1038 s
->write_callback
= cb
;
1039 s
->write_userdata
= userdata
;
1042 void pa_stream_set_state_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1044 assert(s
->ref
>= 1);
1046 s
->state_callback
= cb
;
1047 s
->state_userdata
= userdata
;
1050 void pa_stream_set_overflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1052 assert(s
->ref
>= 1);
1054 s
->overflow_callback
= cb
;
1055 s
->overflow_userdata
= userdata
;
1058 void pa_stream_set_underflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1060 assert(s
->ref
>= 1);
1062 s
->underflow_callback
= cb
;
1063 s
->underflow_userdata
= userdata
;
1066 void pa_stream_set_latency_update_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1068 assert(s
->ref
>= 1);
1070 s
->latency_update_callback
= cb
;
1071 s
->latency_update_userdata
= userdata
;
1074 void pa_stream_simple_ack_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1075 pa_operation
*o
= userdata
;
1080 assert(o
->ref
>= 1);
1085 if (command
!= PA_COMMAND_REPLY
) {
1086 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
1090 } else if (!pa_tagstruct_eof(t
)) {
1091 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1096 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
1097 cb(o
->stream
, success
, o
->userdata
);
1101 pa_operation_done(o
);
1102 pa_operation_unref(o
);
1105 pa_operation
* pa_stream_cork(pa_stream
*s
, int b
, pa_stream_success_cb_t cb
, void *userdata
) {
1111 assert(s
->ref
>= 1);
1113 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1114 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1118 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1120 t
= pa_tagstruct_command(
1122 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CORK_PLAYBACK_STREAM
: PA_COMMAND_CORK_RECORD_STREAM
,
1124 pa_tagstruct_putu32(t
, s
->channel
);
1125 pa_tagstruct_put_boolean(t
, !!b
);
1126 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1127 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
);
1129 if (s
->direction
== PA_STREAM_PLAYBACK
)
1130 invalidate_indexes(s
, 1, 0);
1135 static pa_operation
* stream_send_simple_command(pa_stream
*s
, uint32_t command
, pa_stream_success_cb_t cb
, void *userdata
) {
1141 assert(s
->ref
>= 1);
1143 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1145 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1147 t
= pa_tagstruct_command(s
->context
, command
, &tag
);
1148 pa_tagstruct_putu32(t
, s
->channel
);
1149 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1150 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
);
1155 pa_operation
* pa_stream_flush(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1158 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1160 if ((o
= stream_send_simple_command(s
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_FLUSH_PLAYBACK_STREAM
: PA_COMMAND_FLUSH_RECORD_STREAM
, cb
, userdata
))) {
1162 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1163 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
)
1164 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 1;
1166 if (s
->timing_info_valid
)
1167 s
->timing_info
.write_index_corrupt
= 1;
1169 if (s
->buffer_attr
.prebuf
> 0)
1170 invalidate_indexes(s
, 1, 0);
1172 request_auto_timing_update(s
, 1);
1174 invalidate_indexes(s
, 0, 1);
1180 pa_operation
* pa_stream_prebuf(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1183 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1184 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
1186 if ((o
= stream_send_simple_command(s
, PA_COMMAND_PREBUF_PLAYBACK_STREAM
, cb
, userdata
)))
1187 invalidate_indexes(s
, 1, 0);
1192 pa_operation
* pa_stream_trigger(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1195 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1196 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
1198 if ((o
= stream_send_simple_command(s
, PA_COMMAND_TRIGGER_PLAYBACK_STREAM
, cb
, userdata
)))
1199 invalidate_indexes(s
, 1, 0);
1204 pa_operation
* pa_stream_set_name(pa_stream
*s
, const char *name
, pa_stream_success_cb_t cb
, void *userdata
) {
1210 assert(s
->ref
>= 1);
1213 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1214 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1216 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1218 t
= pa_tagstruct_command(
1220 s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_NAME
: PA_COMMAND_SET_PLAYBACK_STREAM_NAME
,
1222 pa_tagstruct_putu32(t
, s
->channel
);
1223 pa_tagstruct_puts(t
, name
);
1224 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1225 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
);
1230 int pa_stream_get_time(pa_stream
*s
, pa_usec_t
*r_usec
) {
1234 assert(s
->ref
>= 1);
1236 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1237 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1238 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1239 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
1240 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
1242 if (s
->cached_time_valid
)
1243 /* We alredy calculated the time value for this timing info, so let's reuse it */
1244 usec
= s
->cached_time
;
1246 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1247 /* The last byte that was written into the output device
1248 * had this time value associated */
1249 usec
= pa_bytes_to_usec(s
->timing_info
.read_index
< 0 ? 0 : (uint64_t) s
->timing_info
.read_index
, &s
->sample_spec
);
1252 /* Because the latency info took a little time to come
1253 * to us, we assume that the real output time is actually
1255 usec
+= s
->timing_info
.transport_usec
;
1257 /* However, the output device usually maintains a buffer
1258 too, hence the real sample currently played is a little
1260 if (s
->timing_info
.sink_usec
>= usec
)
1263 usec
-= s
->timing_info
.sink_usec
;
1266 } else if (s
->direction
== PA_STREAM_RECORD
) {
1267 /* The last byte written into the server side queue had
1268 * this time value associated */
1269 usec
= pa_bytes_to_usec(s
->timing_info
.write_index
< 0 ? 0 : (uint64_t) s
->timing_info
.write_index
, &s
->sample_spec
);
1272 /* Add transport latency */
1273 usec
+= s
->timing_info
.transport_usec
;
1275 /* Add latency of data in device buffer */
1276 usec
+= s
->timing_info
.source_usec
;
1278 /* If this is a monitor source, we need to correct the
1279 * time by the playback device buffer */
1280 if (s
->timing_info
.sink_usec
>= usec
)
1283 usec
-= s
->timing_info
.sink_usec
;
1287 s
->cached_time
= usec
;
1288 s
->cached_time_valid
= 1;
1291 /* Interpolate if requested */
1292 if (s
->flags
& PA_STREAM_INTERPOLATE_TIMING
) {
1294 /* We just add the time that passed since the latency info was
1296 if (!s
->corked
&& s
->timing_info
.playing
) {
1298 usec
+= pa_timeval_diff(pa_gettimeofday(&now
), &s
->timing_info
.timestamp
);
1302 /* Make sure the time runs monotonically */
1303 if (!(s
->flags
& PA_STREAM_NOT_MONOTONOUS
)) {
1304 if (usec
< s
->previous_time
)
1305 usec
= s
->previous_time
;
1307 s
->previous_time
= usec
;
1316 static pa_usec_t
time_counter_diff(pa_stream
*s
, pa_usec_t a
, pa_usec_t b
, int *negative
) {
1318 assert(s
->ref
>= 1);
1326 if (negative
&& s
->direction
== PA_STREAM_RECORD
) {
1334 int pa_stream_get_latency(pa_stream
*s
, pa_usec_t
*r_usec
, int *negative
) {
1340 assert(s
->ref
>= 1);
1343 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1344 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1345 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1346 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
1347 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
1349 if ((r
= pa_stream_get_time(s
, &t
)) < 0)
1352 if (s
->direction
== PA_STREAM_PLAYBACK
)
1353 cindex
= s
->timing_info
.write_index
;
1355 cindex
= s
->timing_info
.read_index
;
1360 c
= pa_bytes_to_usec(cindex
, &s
->sample_spec
);
1362 if (s
->direction
== PA_STREAM_PLAYBACK
)
1363 *r_usec
= time_counter_diff(s
, c
, t
, negative
);
1365 *r_usec
= time_counter_diff(s
, t
, c
, negative
);
1370 const pa_timing_info
* pa_stream_get_timing_info(pa_stream
*s
) {
1372 assert(s
->ref
>= 1);
1374 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1375 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1376 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->timing_info_valid
, PA_ERR_BADSTATE
);
1378 return &s
->timing_info
;
1381 const pa_sample_spec
* pa_stream_get_sample_spec(pa_stream
*s
) {
1383 assert(s
->ref
>= 1);
1385 return &s
->sample_spec
;
1388 const pa_channel_map
* pa_stream_get_channel_map(pa_stream
*s
) {
1390 assert(s
->ref
>= 1);
1392 return &s
->channel_map
;
1395 const pa_buffer_attr
* pa_stream_get_buffer_attr(pa_stream
*s
) {
1397 assert(s
->ref
>= 1);
1399 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1400 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1401 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
,
1402 pa_context_get_server_protocol_version(s
->context
) >= 9, PA_ERR_NODATA
);
1404 return &s
->buffer_attr
;