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
, !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
) {
124 pa_assert(!s
->context
);
125 pa_assert(!s
->channel_valid
);
127 if (s
->auto_timing_update_event
) {
128 pa_assert(s
->mainloop
);
129 s
->mainloop
->time_free(s
->auto_timing_update_event
);
132 if (s
->peek_memchunk
.memblock
) {
134 pa_memblock_release(s
->peek_memchunk
.memblock
);
135 pa_memblock_unref(s
->peek_memchunk
.memblock
);
138 if (s
->record_memblockq
)
139 pa_memblockq_free(s
->record_memblockq
);
145 void pa_stream_unref(pa_stream
*s
) {
147 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
149 if (PA_REFCNT_DEC(s
) <= 0)
153 pa_stream
* pa_stream_ref(pa_stream
*s
) {
155 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
161 pa_stream_state_t
pa_stream_get_state(pa_stream
*s
) {
163 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
168 pa_context
* pa_stream_get_context(pa_stream
*s
) {
170 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
175 uint32_t pa_stream_get_index(pa_stream
*s
) {
177 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
179 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
181 return s
->device_index
;
184 void pa_stream_set_state(pa_stream
*s
, pa_stream_state_t st
) {
186 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
194 if (s
->state_callback
)
195 s
->state_callback(s
, s
->state_userdata
);
197 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
) && s
->context
) {
199 /* Detach from context */
202 /* Unref all operatio object that point to us */
203 for (o
= s
->context
->operations
; o
; o
= n
) {
207 pa_operation_cancel(o
);
210 /* Drop all outstanding replies for this stream */
211 if (s
->context
->pdispatch
)
212 pa_pdispatch_unregister_reply(s
->context
->pdispatch
, s
);
214 if (s
->channel_valid
)
215 pa_dynarray_put((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, s
->channel
, NULL
);
217 PA_LLIST_REMOVE(pa_stream
, s
->context
->streams
, s
);
221 s
->channel_valid
= 0;
225 s
->read_callback
= NULL
;
226 s
->write_callback
= NULL
;
227 s
->state_callback
= NULL
;
228 s
->overflow_callback
= NULL
;
229 s
->underflow_callback
= NULL
;
230 s
->latency_update_callback
= NULL
;
236 void pa_command_stream_killed(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
237 pa_context
*c
= userdata
;
242 pa_assert(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
);
245 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
249 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
250 !pa_tagstruct_eof(t
)) {
251 pa_context_fail(c
, PA_ERR_PROTOCOL
);
255 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, channel
)))
258 pa_context_set_error(c
, PA_ERR_KILLED
);
259 pa_stream_set_state(s
, PA_STREAM_FAILED
);
265 void pa_command_request(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
267 pa_context
*c
= userdata
;
268 uint32_t bytes
, channel
;
271 pa_assert(command
== PA_COMMAND_REQUEST
);
274 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
278 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
279 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
280 !pa_tagstruct_eof(t
)) {
281 pa_context_fail(c
, PA_ERR_PROTOCOL
);
285 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
288 if (s
->state
== PA_STREAM_READY
) {
289 s
->requested_bytes
+= bytes
;
291 if (s
->requested_bytes
> 0 && s
->write_callback
)
292 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
299 void pa_command_overflow_or_underflow(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
301 pa_context
*c
= userdata
;
305 pa_assert(command
== PA_COMMAND_OVERFLOW
|| command
== PA_COMMAND_UNDERFLOW
);
308 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
312 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
313 !pa_tagstruct_eof(t
)) {
314 pa_context_fail(c
, PA_ERR_PROTOCOL
);
318 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
321 if (s
->state
== PA_STREAM_READY
) {
323 if (command
== PA_COMMAND_OVERFLOW
) {
324 if (s
->overflow_callback
)
325 s
->overflow_callback(s
, s
->overflow_userdata
);
326 } else if (command
== PA_COMMAND_UNDERFLOW
) {
327 if (s
->underflow_callback
)
328 s
->underflow_callback(s
, s
->underflow_userdata
);
336 static void request_auto_timing_update(pa_stream
*s
, int force
) {
338 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
340 if (!(s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
))
343 if (s
->state
== PA_STREAM_READY
&&
344 (force
|| !s
->auto_timing_update_requested
)) {
347 /* pa_log("automatically requesting new timing data"); */
349 if ((o
= pa_stream_update_timing_info(s
, NULL
, NULL
))) {
350 pa_operation_unref(o
);
351 s
->auto_timing_update_requested
= 1;
355 if (s
->auto_timing_update_event
) {
357 pa_gettimeofday(&next
);
358 pa_timeval_add(&next
, LATENCY_IPOL_INTERVAL_USEC
);
359 s
->mainloop
->time_restart(s
->auto_timing_update_event
, &next
);
363 static void invalidate_indexes(pa_stream
*s
, int r
, int w
) {
365 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
367 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
369 if (s
->state
!= PA_STREAM_READY
)
373 s
->write_index_not_before
= s
->context
->ctag
;
375 if (s
->timing_info_valid
)
376 s
->timing_info
.write_index_corrupt
= 1;
378 /* pa_log("write_index invalidated"); */
382 s
->read_index_not_before
= s
->context
->ctag
;
384 if (s
->timing_info_valid
)
385 s
->timing_info
.read_index_corrupt
= 1;
387 /* pa_log("read_index invalidated"); */
390 if ((s
->direction
== PA_STREAM_PLAYBACK
&& r
) ||
391 (s
->direction
== PA_STREAM_RECORD
&& w
))
392 s
->cached_time_valid
= 0;
394 request_auto_timing_update(s
, 1);
397 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
) {
398 pa_stream
*s
= userdata
;
401 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
403 /* pa_log("time event"); */
406 request_auto_timing_update(s
, 0);
410 static void create_stream_complete(pa_stream
*s
) {
412 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
413 pa_assert(s
->state
== PA_STREAM_CREATING
);
415 pa_stream_set_state(s
, PA_STREAM_READY
);
417 if (s
->requested_bytes
> 0 && s
->write_callback
)
418 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
420 if (s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) {
422 pa_gettimeofday(&tv
);
423 tv
.tv_usec
+= LATENCY_IPOL_INTERVAL_USEC
; /* every 100 ms */
424 pa_assert(!s
->auto_timing_update_event
);
425 s
->auto_timing_update_event
= s
->mainloop
->time_new(s
->mainloop
, &tv
, &auto_timing_update_callback
, s
);
429 void pa_create_stream_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
430 pa_stream
*s
= userdata
;
434 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
435 pa_assert(s
->state
== PA_STREAM_CREATING
);
439 if (command
!= PA_COMMAND_REPLY
) {
440 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
443 pa_stream_set_state(s
, PA_STREAM_FAILED
);
447 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
448 ((s
->direction
!= PA_STREAM_UPLOAD
) && pa_tagstruct_getu32(t
, &s
->device_index
) < 0) ||
449 ((s
->direction
!= PA_STREAM_RECORD
) && pa_tagstruct_getu32(t
, &s
->requested_bytes
) < 0)) {
450 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
454 if (pa_context_get_server_protocol_version(s
->context
) >= 9) {
455 if (s
->direction
== PA_STREAM_PLAYBACK
) {
456 if (pa_tagstruct_getu32(t
, &s
->buffer_attr
.maxlength
) < 0 ||
457 pa_tagstruct_getu32(t
, &s
->buffer_attr
.tlength
) < 0 ||
458 pa_tagstruct_getu32(t
, &s
->buffer_attr
.prebuf
) < 0 ||
459 pa_tagstruct_getu32(t
, &s
->buffer_attr
.minreq
) < 0) {
460 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
463 } else if (s
->direction
== PA_STREAM_RECORD
) {
464 if (pa_tagstruct_getu32(t
, &s
->buffer_attr
.maxlength
) < 0 ||
465 pa_tagstruct_getu32(t
, &s
->buffer_attr
.fragsize
) < 0) {
466 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
472 if (!pa_tagstruct_eof(t
)) {
473 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
477 if (s
->direction
== PA_STREAM_RECORD
) {
478 pa_assert(!s
->record_memblockq
);
480 s
->record_memblockq
= pa_memblockq_new(
482 s
->buffer_attr
.maxlength
,
484 pa_frame_size(&s
->sample_spec
),
490 s
->channel_valid
= 1;
491 pa_dynarray_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, s
->channel
, s
);
493 if (s
->direction
!= PA_STREAM_UPLOAD
&& s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) {
494 /* If automatic timing updates are active, we wait for the
495 * first timing update before going to PA_STREAM_READY
497 s
->state
= PA_STREAM_READY
;
498 request_auto_timing_update(s
, 1);
499 s
->state
= PA_STREAM_CREATING
;
502 create_stream_complete(s
);
508 static int create_stream(
509 pa_stream_direction_t direction
,
512 const pa_buffer_attr
*attr
,
513 pa_stream_flags_t flags
,
514 const pa_cvolume
*volume
,
515 pa_stream
*sync_stream
) {
521 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
523 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_UNCONNECTED
, PA_ERR_BADSTATE
);
524 PA_CHECK_VALIDITY(s
->context
, !(flags
& ~((direction
!= PA_STREAM_UPLOAD
?
525 PA_STREAM_START_CORKED
|
526 PA_STREAM_INTERPOLATE_TIMING
|
527 PA_STREAM_NOT_MONOTONOUS
|
528 PA_STREAM_AUTO_TIMING_UPDATE
: 0))), PA_ERR_INVALID
);
529 PA_CHECK_VALIDITY(s
->context
, !volume
|| volume
->channels
== s
->sample_spec
.channels
, PA_ERR_INVALID
);
530 PA_CHECK_VALIDITY(s
->context
, !sync_stream
|| (direction
== PA_STREAM_PLAYBACK
&& sync_stream
->direction
== PA_STREAM_PLAYBACK
), PA_ERR_INVALID
);
534 s
->direction
= direction
;
538 s
->syncid
= sync_stream
->syncid
;
541 s
->buffer_attr
= *attr
;
544 s
->buffer_attr
.tlength
= pa_bytes_per_second(&s
->sample_spec
)/2;
545 s
->buffer_attr
.maxlength
= (s
->buffer_attr
.tlength
*3)/2;
546 s
->buffer_attr
.minreq
= s
->buffer_attr
.tlength
/100;
547 s
->buffer_attr
.prebuf
= s
->buffer_attr
.tlength
- s
->buffer_attr
.minreq
;
548 s
->buffer_attr
.fragsize
= s
->buffer_attr
.tlength
/100;
552 dev
= s
->direction
== PA_STREAM_PLAYBACK
? s
->context
->conf
->default_sink
: s
->context
->conf
->default_source
;
554 t
= pa_tagstruct_command(
556 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
,
561 PA_TAG_STRING
, s
->name
,
562 PA_TAG_SAMPLE_SPEC
, &s
->sample_spec
,
563 PA_TAG_CHANNEL_MAP
, &s
->channel_map
,
564 PA_TAG_U32
, PA_INVALID_INDEX
,
566 PA_TAG_U32
, s
->buffer_attr
.maxlength
,
567 PA_TAG_BOOLEAN
, !!(flags
& PA_STREAM_START_CORKED
),
570 if (s
->direction
== PA_STREAM_PLAYBACK
) {
575 PA_TAG_U32
, s
->buffer_attr
.tlength
,
576 PA_TAG_U32
, s
->buffer_attr
.prebuf
,
577 PA_TAG_U32
, s
->buffer_attr
.minreq
,
578 PA_TAG_U32
, s
->syncid
,
582 volume
= pa_cvolume_reset(&cv
, s
->sample_spec
.channels
);
584 pa_tagstruct_put_cvolume(t
, volume
);
586 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
588 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
589 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
, NULL
);
591 pa_stream_set_state(s
, PA_STREAM_CREATING
);
597 int pa_stream_connect_playback(
600 const pa_buffer_attr
*attr
,
601 pa_stream_flags_t flags
,
603 pa_stream
*sync_stream
) {
606 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
608 return create_stream(PA_STREAM_PLAYBACK
, s
, dev
, attr
, flags
, volume
, sync_stream
);
611 int pa_stream_connect_record(
614 const pa_buffer_attr
*attr
,
615 pa_stream_flags_t flags
) {
618 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
620 return create_stream(PA_STREAM_RECORD
, s
, dev
, attr
, flags
, NULL
, NULL
);
627 void (*free_cb
)(void *p
),
629 pa_seek_mode_t seek
) {
634 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
637 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
638 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| s
->direction
== PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
639 PA_CHECK_VALIDITY(s
->context
, seek
<= PA_SEEK_RELATIVE_END
, PA_ERR_INVALID
);
640 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| (seek
== PA_SEEK_RELATIVE
&& offset
== 0), PA_ERR_INVALID
);
646 chunk
.memblock
= pa_memblock_new_user(s
->context
->mempool
, (void*) data
, length
, free_cb
, 1);
649 chunk
.memblock
= pa_memblock_new(s
->context
->mempool
, length
);
650 tdata
= pa_memblock_acquire(chunk
.memblock
);
651 memcpy(tdata
, data
, length
);
652 pa_memblock_release(chunk
.memblock
);
656 chunk
.length
= length
;
658 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, offset
, seek
, &chunk
);
659 pa_memblock_unref(chunk
.memblock
);
661 if (length
< s
->requested_bytes
)
662 s
->requested_bytes
-= length
;
664 s
->requested_bytes
= 0;
666 if (s
->direction
== PA_STREAM_PLAYBACK
) {
668 /* Update latency request correction */
669 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
) {
671 if (seek
== PA_SEEK_ABSOLUTE
) {
672 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 0;
673 s
->write_index_corrections
[s
->current_write_index_correction
].absolute
= 1;
674 s
->write_index_corrections
[s
->current_write_index_correction
].value
= offset
+ length
;
675 } else if (seek
== PA_SEEK_RELATIVE
) {
676 if (!s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
)
677 s
->write_index_corrections
[s
->current_write_index_correction
].value
+= offset
+ length
;
679 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 1;
682 /* Update the write index in the already available latency data */
683 if (s
->timing_info_valid
) {
685 if (seek
== PA_SEEK_ABSOLUTE
) {
686 s
->timing_info
.write_index_corrupt
= 0;
687 s
->timing_info
.write_index
= offset
+ length
;
688 } else if (seek
== PA_SEEK_RELATIVE
) {
689 if (!s
->timing_info
.write_index_corrupt
)
690 s
->timing_info
.write_index
+= offset
+ length
;
692 s
->timing_info
.write_index_corrupt
= 1;
695 if (!s
->timing_info_valid
|| s
->timing_info
.write_index_corrupt
)
696 request_auto_timing_update(s
, 1);
702 int pa_stream_peek(pa_stream
*s
, const void **data
, size_t *length
) {
704 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
708 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
709 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
711 if (!s
->peek_memchunk
.memblock
) {
713 if (pa_memblockq_peek(s
->record_memblockq
, &s
->peek_memchunk
) < 0) {
719 s
->peek_data
= pa_memblock_acquire(s
->peek_memchunk
.memblock
);
722 pa_assert(s
->peek_data
);
723 *data
= (uint8_t*) s
->peek_data
+ s
->peek_memchunk
.index
;
724 *length
= s
->peek_memchunk
.length
;
728 int pa_stream_drop(pa_stream
*s
) {
730 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
732 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
733 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
734 PA_CHECK_VALIDITY(s
->context
, s
->peek_memchunk
.memblock
, PA_ERR_BADSTATE
);
736 pa_memblockq_drop(s
->record_memblockq
, s
->peek_memchunk
.length
);
738 /* Fix the simulated local read index */
739 if (s
->timing_info_valid
&& !s
->timing_info
.read_index_corrupt
)
740 s
->timing_info
.read_index
+= s
->peek_memchunk
.length
;
742 pa_assert(s
->peek_data
);
743 pa_memblock_release(s
->peek_memchunk
.memblock
);
744 pa_memblock_unref(s
->peek_memchunk
.memblock
);
745 s
->peek_memchunk
.length
= 0;
746 s
->peek_memchunk
.index
= 0;
747 s
->peek_memchunk
.memblock
= NULL
;
752 size_t pa_stream_writable_size(pa_stream
*s
) {
754 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
756 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
757 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
759 return s
->requested_bytes
;
762 size_t pa_stream_readable_size(pa_stream
*s
) {
764 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
766 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
767 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
769 return pa_memblockq_get_length(s
->record_memblockq
);
772 pa_operation
* pa_stream_drain(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
778 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
780 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
781 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
783 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
785 t
= pa_tagstruct_command(s
->context
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
, &tag
);
786 pa_tagstruct_putu32(t
, s
->channel
);
787 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
788 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
);
793 static void stream_get_timing_info_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
794 pa_operation
*o
= userdata
;
795 struct timeval local
, remote
, now
;
800 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
802 if (!o
->context
|| !o
->stream
)
805 i
= &o
->stream
->timing_info
;
807 /* 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); */
809 o
->stream
->timing_info_valid
= 0;
810 i
->write_index_corrupt
= 0;
811 i
->read_index_corrupt
= 0;
813 /* pa_log("timing update %u\n", tag); */
815 if (command
!= PA_COMMAND_REPLY
) {
816 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
819 } else if (pa_tagstruct_get_usec(t
, &i
->sink_usec
) < 0 ||
820 pa_tagstruct_get_usec(t
, &i
->source_usec
) < 0 ||
821 pa_tagstruct_get_boolean(t
, &i
->playing
) < 0 ||
822 pa_tagstruct_get_timeval(t
, &local
) < 0 ||
823 pa_tagstruct_get_timeval(t
, &remote
) < 0 ||
824 pa_tagstruct_gets64(t
, &i
->write_index
) < 0 ||
825 pa_tagstruct_gets64(t
, &i
->read_index
) < 0 ||
826 !pa_tagstruct_eof(t
)) {
827 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
831 o
->stream
->timing_info_valid
= 1;
833 pa_gettimeofday(&now
);
835 /* Calculcate timestamps */
836 if (pa_timeval_cmp(&local
, &remote
) <= 0 && pa_timeval_cmp(&remote
, &now
) <= 0) {
837 /* local and remote seem to have synchronized clocks */
839 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
)
840 i
->transport_usec
= pa_timeval_diff(&remote
, &local
);
842 i
->transport_usec
= pa_timeval_diff(&now
, &remote
);
844 i
->synchronized_clocks
= 1;
845 i
->timestamp
= remote
;
847 /* clocks are not synchronized, let's estimate latency then */
848 i
->transport_usec
= pa_timeval_diff(&now
, &local
)/2;
849 i
->synchronized_clocks
= 0;
850 i
->timestamp
= local
;
851 pa_timeval_add(&i
->timestamp
, i
->transport_usec
);
854 /* Invalidate read and write indexes if necessary */
855 if (tag
< o
->stream
->read_index_not_before
)
856 i
->read_index_corrupt
= 1;
858 if (tag
< o
->stream
->write_index_not_before
)
859 i
->write_index_corrupt
= 1;
861 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
862 /* Write index correction */
867 /* Go through the saved correction values and add up the total correction.*/
869 for (n
= 0, j
= o
->stream
->current_write_index_correction
+1;
870 n
< PA_MAX_WRITE_INDEX_CORRECTIONS
;
871 n
++, j
= (j
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
) {
873 /* Step over invalid data or out-of-date data */
874 if (!o
->stream
->write_index_corrections
[j
].valid
||
875 o
->stream
->write_index_corrections
[j
].tag
< ctag
)
878 /* Make sure that everything is in order */
879 ctag
= o
->stream
->write_index_corrections
[j
].tag
+1;
881 /* Now fix the write index */
882 if (o
->stream
->write_index_corrections
[j
].corrupt
) {
883 /* A corrupting seek was made */
885 i
->write_index_corrupt
= 1;
886 } else if (o
->stream
->write_index_corrections
[j
].absolute
) {
887 /* An absolute seek was made */
888 i
->write_index
= o
->stream
->write_index_corrections
[j
].value
;
889 i
->write_index_corrupt
= 0;
890 } else if (!i
->write_index_corrupt
) {
891 /* A relative seek was made */
892 i
->write_index
+= o
->stream
->write_index_corrections
[j
].value
;
897 if (o
->stream
->direction
== PA_STREAM_RECORD
) {
898 /* Read index correction */
900 if (!i
->read_index_corrupt
)
901 i
->read_index
-= pa_memblockq_get_length(o
->stream
->record_memblockq
);
904 o
->stream
->cached_time_valid
= 0;
907 o
->stream
->auto_timing_update_requested
= 0;
908 /* 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); */
910 /* Clear old correction entries */
911 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
914 for (n
= 0; n
< PA_MAX_WRITE_INDEX_CORRECTIONS
; n
++) {
915 if (!o
->stream
->write_index_corrections
[n
].valid
)
918 if (o
->stream
->write_index_corrections
[n
].tag
<= tag
)
919 o
->stream
->write_index_corrections
[n
].valid
= 0;
923 /* First, let's complete the initialization, if necessary. */
924 if (o
->stream
->state
== PA_STREAM_CREATING
)
925 create_stream_complete(o
->stream
);
927 if (o
->stream
->latency_update_callback
)
928 o
->stream
->latency_update_callback(o
->stream
, o
->stream
->latency_update_userdata
);
930 if (o
->callback
&& o
->stream
&& o
->stream
->state
== PA_STREAM_READY
) {
931 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
932 cb(o
->stream
, o
->stream
->timing_info_valid
, o
->userdata
);
937 pa_operation_done(o
);
938 pa_operation_unref(o
);
941 pa_operation
* pa_stream_update_timing_info(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
949 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
951 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
952 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
954 if (s
->direction
== PA_STREAM_PLAYBACK
) {
955 /* Find a place to store the write_index correction data for this entry */
956 cidx
= (s
->current_write_index_correction
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
;
958 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
959 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !s
->write_index_corrections
[cidx
].valid
, PA_ERR_INTERNAL
);
961 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
963 t
= pa_tagstruct_command(
965 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_GET_PLAYBACK_LATENCY
: PA_COMMAND_GET_RECORD_LATENCY
,
967 pa_tagstruct_putu32(t
, s
->channel
);
968 pa_tagstruct_put_timeval(t
, pa_gettimeofday(&now
));
970 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
971 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
);
973 if (s
->direction
== PA_STREAM_PLAYBACK
) {
974 /* Fill in initial correction data */
975 o
->stream
->current_write_index_correction
= cidx
;
976 o
->stream
->write_index_corrections
[cidx
].valid
= 1;
977 o
->stream
->write_index_corrections
[cidx
].tag
= tag
;
978 o
->stream
->write_index_corrections
[cidx
].absolute
= 0;
979 o
->stream
->write_index_corrections
[cidx
].value
= 0;
980 o
->stream
->write_index_corrections
[cidx
].corrupt
= 0;
983 /* pa_log("requesting update %u\n", tag); */
988 void pa_stream_disconnect_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
989 pa_stream
*s
= userdata
;
993 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
997 if (command
!= PA_COMMAND_REPLY
) {
998 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
1001 pa_stream_set_state(s
, PA_STREAM_FAILED
);
1003 } else if (!pa_tagstruct_eof(t
)) {
1004 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
1008 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
1014 int pa_stream_disconnect(pa_stream
*s
) {
1019 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1021 PA_CHECK_VALIDITY(s
->context
, s
->channel_valid
, PA_ERR_BADSTATE
);
1022 PA_CHECK_VALIDITY(s
->context
, s
->context
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
1026 t
= pa_tagstruct_command(
1028 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
1029 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
),
1031 pa_tagstruct_putu32(t
, s
->channel
);
1032 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1033 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
, NULL
);
1039 void pa_stream_set_read_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
1041 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1043 s
->read_callback
= cb
;
1044 s
->read_userdata
= userdata
;
1047 void pa_stream_set_write_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
1049 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1051 s
->write_callback
= cb
;
1052 s
->write_userdata
= userdata
;
1055 void pa_stream_set_state_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1057 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1059 s
->state_callback
= cb
;
1060 s
->state_userdata
= userdata
;
1063 void pa_stream_set_overflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1065 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1067 s
->overflow_callback
= cb
;
1068 s
->overflow_userdata
= userdata
;
1071 void pa_stream_set_underflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1073 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1075 s
->underflow_callback
= cb
;
1076 s
->underflow_userdata
= userdata
;
1079 void pa_stream_set_latency_update_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1081 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1083 s
->latency_update_callback
= cb
;
1084 s
->latency_update_userdata
= userdata
;
1087 void pa_stream_simple_ack_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1088 pa_operation
*o
= userdata
;
1093 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
1098 if (command
!= PA_COMMAND_REPLY
) {
1099 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
1103 } else if (!pa_tagstruct_eof(t
)) {
1104 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1109 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
1110 cb(o
->stream
, success
, o
->userdata
);
1114 pa_operation_done(o
);
1115 pa_operation_unref(o
);
1118 pa_operation
* pa_stream_cork(pa_stream
*s
, int b
, pa_stream_success_cb_t cb
, void *userdata
) {
1124 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1126 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1127 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1131 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1133 t
= pa_tagstruct_command(
1135 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CORK_PLAYBACK_STREAM
: PA_COMMAND_CORK_RECORD_STREAM
,
1137 pa_tagstruct_putu32(t
, s
->channel
);
1138 pa_tagstruct_put_boolean(t
, !!b
);
1139 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1140 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
);
1142 if (s
->direction
== PA_STREAM_PLAYBACK
)
1143 invalidate_indexes(s
, 1, 0);
1148 static pa_operation
* stream_send_simple_command(pa_stream
*s
, uint32_t command
, pa_stream_success_cb_t cb
, void *userdata
) {
1154 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1156 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1158 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1160 t
= pa_tagstruct_command(s
->context
, command
, &tag
);
1161 pa_tagstruct_putu32(t
, s
->channel
);
1162 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1163 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
);
1168 pa_operation
* pa_stream_flush(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1172 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1174 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1176 if ((o
= stream_send_simple_command(s
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_FLUSH_PLAYBACK_STREAM
: PA_COMMAND_FLUSH_RECORD_STREAM
, cb
, userdata
))) {
1178 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1179 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
)
1180 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 1;
1182 if (s
->timing_info_valid
)
1183 s
->timing_info
.write_index_corrupt
= 1;
1185 if (s
->buffer_attr
.prebuf
> 0)
1186 invalidate_indexes(s
, 1, 0);
1188 request_auto_timing_update(s
, 1);
1190 invalidate_indexes(s
, 0, 1);
1196 pa_operation
* pa_stream_prebuf(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1200 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1202 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1203 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
1205 if ((o
= stream_send_simple_command(s
, PA_COMMAND_PREBUF_PLAYBACK_STREAM
, cb
, userdata
)))
1206 invalidate_indexes(s
, 1, 0);
1211 pa_operation
* pa_stream_trigger(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1215 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1217 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1218 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
1220 if ((o
= stream_send_simple_command(s
, PA_COMMAND_TRIGGER_PLAYBACK_STREAM
, cb
, userdata
)))
1221 invalidate_indexes(s
, 1, 0);
1226 pa_operation
* pa_stream_set_name(pa_stream
*s
, const char *name
, pa_stream_success_cb_t cb
, void *userdata
) {
1232 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1235 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1236 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1238 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1240 t
= pa_tagstruct_command(
1242 s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_NAME
: PA_COMMAND_SET_PLAYBACK_STREAM_NAME
,
1244 pa_tagstruct_putu32(t
, s
->channel
);
1245 pa_tagstruct_puts(t
, name
);
1246 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1247 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
);
1252 int pa_stream_get_time(pa_stream
*s
, pa_usec_t
*r_usec
) {
1256 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1258 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1259 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1260 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1261 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
1262 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
1264 if (s
->cached_time_valid
)
1265 /* We alredy calculated the time value for this timing info, so let's reuse it */
1266 usec
= s
->cached_time
;
1268 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1269 /* The last byte that was written into the output device
1270 * had this time value associated */
1271 usec
= pa_bytes_to_usec(s
->timing_info
.read_index
< 0 ? 0 : (uint64_t) s
->timing_info
.read_index
, &s
->sample_spec
);
1274 /* Because the latency info took a little time to come
1275 * to us, we assume that the real output time is actually
1277 usec
+= s
->timing_info
.transport_usec
;
1279 /* However, the output device usually maintains a buffer
1280 too, hence the real sample currently played is a little
1282 if (s
->timing_info
.sink_usec
>= usec
)
1285 usec
-= s
->timing_info
.sink_usec
;
1288 } else if (s
->direction
== PA_STREAM_RECORD
) {
1289 /* The last byte written into the server side queue had
1290 * this time value associated */
1291 usec
= pa_bytes_to_usec(s
->timing_info
.write_index
< 0 ? 0 : (uint64_t) s
->timing_info
.write_index
, &s
->sample_spec
);
1294 /* Add transport latency */
1295 usec
+= s
->timing_info
.transport_usec
;
1297 /* Add latency of data in device buffer */
1298 usec
+= s
->timing_info
.source_usec
;
1300 /* If this is a monitor source, we need to correct the
1301 * time by the playback device buffer */
1302 if (s
->timing_info
.sink_usec
>= usec
)
1305 usec
-= s
->timing_info
.sink_usec
;
1309 s
->cached_time
= usec
;
1310 s
->cached_time_valid
= 1;
1313 /* Interpolate if requested */
1314 if (s
->flags
& PA_STREAM_INTERPOLATE_TIMING
) {
1316 /* We just add the time that passed since the latency info was
1318 if (!s
->corked
&& s
->timing_info
.playing
) {
1320 usec
+= pa_timeval_diff(pa_gettimeofday(&now
), &s
->timing_info
.timestamp
);
1324 /* Make sure the time runs monotonically */
1325 if (!(s
->flags
& PA_STREAM_NOT_MONOTONOUS
)) {
1326 if (usec
< s
->previous_time
)
1327 usec
= s
->previous_time
;
1329 s
->previous_time
= usec
;
1338 static pa_usec_t
time_counter_diff(pa_stream
*s
, pa_usec_t a
, pa_usec_t b
, int *negative
) {
1340 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1348 if (negative
&& s
->direction
== PA_STREAM_RECORD
) {
1356 int pa_stream_get_latency(pa_stream
*s
, pa_usec_t
*r_usec
, int *negative
) {
1362 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1365 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1366 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1367 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1368 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
1369 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
1371 if ((r
= pa_stream_get_time(s
, &t
)) < 0)
1374 if (s
->direction
== PA_STREAM_PLAYBACK
)
1375 cindex
= s
->timing_info
.write_index
;
1377 cindex
= s
->timing_info
.read_index
;
1382 c
= pa_bytes_to_usec(cindex
, &s
->sample_spec
);
1384 if (s
->direction
== PA_STREAM_PLAYBACK
)
1385 *r_usec
= time_counter_diff(s
, c
, t
, negative
);
1387 *r_usec
= time_counter_diff(s
, t
, c
, negative
);
1392 const pa_timing_info
* pa_stream_get_timing_info(pa_stream
*s
) {
1394 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1396 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1397 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1398 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->timing_info_valid
, PA_ERR_BADSTATE
);
1400 return &s
->timing_info
;
1403 const pa_sample_spec
* pa_stream_get_sample_spec(pa_stream
*s
) {
1405 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1407 return &s
->sample_spec
;
1410 const pa_channel_map
* pa_stream_get_channel_map(pa_stream
*s
) {
1412 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1414 return &s
->channel_map
;
1417 const pa_buffer_attr
* pa_stream_get_buffer_attr(pa_stream
*s
) {
1419 pa_assert(PA_REFCNT_VALUE(s
) >= 1);
1421 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1422 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1423 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
,
1424 pa_context_get_server_protocol_version(s
->context
) >= 9, PA_ERR_NODATA
);
1426 return &s
->buffer_attr
;