4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <polyp/def.h>
32 #include <polypcore/xmalloc.h>
33 #include <polypcore/pstream-util.h>
34 #include <polypcore/util.h>
35 #include <polypcore/log.h>
36 #include <polypcore/hashmap.h>
40 #define LATENCY_IPOL_INTERVAL_USEC (10000L)
42 pa_stream
*pa_stream_new(pa_context
*c
, const char *name
, const pa_sample_spec
*ss
, const pa_channel_map
*map
) {
48 PA_CHECK_VALIDITY_RETURN_NULL(c
, ss
&& pa_sample_spec_valid(ss
), PA_ERR_INVALID
);
49 PA_CHECK_VALIDITY_RETURN_NULL(c
, !map
|| (pa_channel_map_valid(map
) && map
->channels
== ss
->channels
), PA_ERR_INVALID
);
51 s
= pa_xnew(pa_stream
, 1);
54 s
->mainloop
= c
->mainloop
;
56 s
->read_callback
= NULL
;
57 s
->read_userdata
= NULL
;
58 s
->write_callback
= NULL
;
59 s
->write_userdata
= NULL
;
60 s
->state_callback
= NULL
;
61 s
->state_userdata
= NULL
;
62 s
->overflow_callback
= NULL
;
63 s
->overflow_userdata
= NULL
;
64 s
->underflow_callback
= NULL
;
65 s
->underflow_userdata
= NULL
;
67 s
->direction
= PA_STREAM_NODIRECTION
;
68 s
->name
= pa_xstrdup(name
);
73 s
->channel_map
= *map
;
75 pa_channel_map_init_auto(&s
->channel_map
, ss
->channels
);
79 s
->syncid
= c
->csyncid
++;
80 s
->device_index
= PA_INVALID_INDEX
;
81 s
->requested_bytes
= 0;
82 s
->state
= PA_STREAM_UNCONNECTED
;
83 memset(&s
->buffer_attr
, 0, sizeof(s
->buffer_attr
));
85 s
->peek_memchunk
.index
= 0;
86 s
->peek_memchunk
.length
= 0;
87 s
->peek_memchunk
.memblock
= NULL
;
89 s
->record_memblockq
= NULL
;
92 s
->timing_info_valid
= 0;
93 s
->read_index_not_before
= 0;
94 s
->write_index_not_before
= 0;
96 for (i
= 0; i
< PA_MAX_WRITE_INDEX_CORRECTIONS
; i
++)
97 s
->write_index_corrections
[i
].valid
= 0;
98 s
->current_write_index_correction
= 0;
102 s
->ipol_usec_valid
= 0;
103 s
->ipol_timestamp
.tv_sec
= 0;
104 s
->ipol_timestamp
.tv_usec
= 0;
106 s
->auto_timing_update_event
= NULL
;
107 s
->auto_timing_update_requested
= 0;
109 PA_LLIST_PREPEND(pa_stream
, c
->streams
, s
);
111 /* The context and stream will point at each other. We cannot ref count
112 both though since that will create a loop. */
113 pa_context_ref(s
->context
);
118 static void stream_free(pa_stream
*s
) {
119 assert(s
&& s
->context
&& !s
->channel_valid
);
121 PA_LLIST_REMOVE(pa_stream
, s
->context
->streams
, s
);
123 pa_context_unref(s
->context
);
125 if (s
->auto_timing_update_event
) {
127 s
->mainloop
->time_free(s
->auto_timing_update_event
);
130 if (s
->peek_memchunk
.memblock
)
131 pa_memblock_unref(s
->peek_memchunk
.memblock
);
133 if (s
->record_memblockq
)
134 pa_memblockq_free(s
->record_memblockq
);
140 void pa_stream_unref(pa_stream
*s
) {
148 pa_stream
* pa_stream_ref(pa_stream
*s
) {
156 pa_stream_state_t
pa_stream_get_state(pa_stream
*s
) {
163 pa_context
* pa_stream_get_context(pa_stream
*s
) {
170 uint32_t pa_stream_get_index(pa_stream
*s
) {
174 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
176 return s
->device_index
;
179 void pa_stream_set_state(pa_stream
*s
, pa_stream_state_t st
) {
190 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
) && s
->context
) {
191 /* Detach from context */
193 if (s
->channel_valid
)
194 pa_dynarray_put((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, s
->channel
, NULL
);
197 s
->channel_valid
= 0;
199 /* We keep a ref as long as we're connected */
203 if (s
->state_callback
)
204 s
->state_callback(s
, s
->state_userdata
);
209 void pa_command_stream_killed(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
210 pa_context
*c
= userdata
;
215 assert(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
);
221 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
222 !pa_tagstruct_eof(t
)) {
223 pa_context_fail(c
, PA_ERR_PROTOCOL
);
227 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, channel
)))
230 pa_context_set_error(c
, PA_ERR_KILLED
);
231 pa_stream_set_state(s
, PA_STREAM_FAILED
);
237 void pa_command_request(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
239 pa_context
*c
= userdata
;
240 uint32_t bytes
, channel
;
243 assert(command
== PA_COMMAND_REQUEST
);
249 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
250 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
251 !pa_tagstruct_eof(t
)) {
252 pa_context_fail(c
, PA_ERR_PROTOCOL
);
256 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
259 if (s
->state
== PA_STREAM_READY
) {
260 s
->requested_bytes
+= bytes
;
262 if (s
->requested_bytes
> 0 && s
->write_callback
)
263 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
270 void pa_command_overflow_or_underflow(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
272 pa_context
*c
= userdata
;
276 assert(command
== PA_COMMAND_OVERFLOW
|| command
== PA_COMMAND_UNDERFLOW
);
282 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
283 !pa_tagstruct_eof(t
)) {
284 pa_context_fail(c
, PA_ERR_PROTOCOL
);
288 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
291 if (s
->state
== PA_STREAM_READY
) {
293 if (command
== PA_COMMAND_OVERFLOW
) {
294 if (s
->overflow_callback
)
295 s
->overflow_callback(s
, s
->overflow_userdata
);
296 } else if (command
== PA_COMMAND_UNDERFLOW
) {
297 if (s
->underflow_callback
)
298 s
->underflow_callback(s
, s
->underflow_userdata
);
306 static void request_auto_timing_update(pa_stream
*s
, int force
) {
310 if (!(s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
))
313 if (s
->state
== PA_STREAM_READY
&&
314 (force
|| !s
->auto_timing_update_requested
)) {
317 /* pa_log("automatically requesting new timing data"); */
319 if ((o
= pa_stream_update_timing_info(s
, NULL
, NULL
))) {
320 pa_operation_unref(o
);
321 s
->auto_timing_update_requested
= 1;
325 pa_gettimeofday(&next
);
326 pa_timeval_add(&next
, LATENCY_IPOL_INTERVAL_USEC
);
327 s
->mainloop
->time_restart(s
->auto_timing_update_event
, &next
);
330 static void invalidate_indexes(pa_stream
*s
, int r
, int w
) {
333 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
335 if (s
->state
!= PA_STREAM_READY
)
339 s
->write_index_not_before
= s
->context
->ctag
;
341 if (s
->timing_info_valid
)
342 s
->timing_info
.write_index_corrupt
= 1;
344 /* pa_log("write_index invalidated"); */
348 s
->read_index_not_before
= s
->context
->ctag
;
350 if (s
->timing_info_valid
)
351 s
->timing_info
.read_index_corrupt
= 1;
353 /* pa_log("read_index invalidated"); */
356 if ((s
->direction
== PA_STREAM_PLAYBACK
&& r
) ||
357 (s
->direction
== PA_STREAM_RECORD
&& w
))
358 s
->ipol_usec_valid
= 0;
360 request_auto_timing_update(s
, 1);
363 static void auto_timing_update_callback(pa_mainloop_api
*m
, pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
364 pa_stream
*s
= userdata
;
366 /* pa_log("time event"); */
369 request_auto_timing_update(s
, 0);
373 void pa_create_stream_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
374 pa_stream
*s
= userdata
;
379 assert(s
->state
== PA_STREAM_CREATING
);
383 if (command
!= PA_COMMAND_REPLY
) {
384 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
387 pa_stream_set_state(s
, PA_STREAM_FAILED
);
391 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
392 ((s
->direction
!= PA_STREAM_UPLOAD
) && pa_tagstruct_getu32(t
, &s
->device_index
) < 0) ||
393 ((s
->direction
!= PA_STREAM_RECORD
) && pa_tagstruct_getu32(t
, &s
->requested_bytes
) < 0) ||
394 !pa_tagstruct_eof(t
)) {
395 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
399 if (s
->direction
== PA_STREAM_RECORD
) {
400 assert(!s
->record_memblockq
);
402 s
->record_memblockq
= pa_memblockq_new(
404 s
->buffer_attr
.maxlength
,
406 pa_frame_size(&s
->sample_spec
),
410 s
->context
->memblock_stat
);
413 s
->channel_valid
= 1;
414 pa_dynarray_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, s
->channel
, s
);
416 /* We add an extra ref as long as we're connected (i.e. in the dynarray) */
419 pa_stream_set_state(s
, PA_STREAM_READY
);
421 if (s
->direction
!= PA_STREAM_UPLOAD
&&
422 s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) {
425 pa_gettimeofday(&tv
);
426 tv
.tv_usec
+= LATENCY_IPOL_INTERVAL_USEC
; /* every 100 ms */
428 assert(!s
->auto_timing_update_event
);
429 s
->auto_timing_update_event
= s
->mainloop
->time_new(s
->mainloop
, &tv
, &auto_timing_update_callback
, s
);
431 request_auto_timing_update(s
, 1);
434 if (s
->requested_bytes
> 0 && s
->ref
> 1 && s
->write_callback
)
435 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
441 static int create_stream(
442 pa_stream_direction_t direction
,
445 const pa_buffer_attr
*attr
,
446 pa_stream_flags_t flags
,
447 const pa_cvolume
*volume
,
448 pa_stream
*sync_stream
) {
456 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_UNCONNECTED
, PA_ERR_BADSTATE
);
457 PA_CHECK_VALIDITY(s
->context
, !(flags
& ~((direction
!= PA_STREAM_UPLOAD
?
458 PA_STREAM_START_CORKED
|
459 PA_STREAM_INTERPOLATE_TIMING
|
460 PA_STREAM_NOT_MONOTONOUS
|
461 PA_STREAM_AUTO_TIMING_UPDATE
: 0))), PA_ERR_INVALID
);
462 PA_CHECK_VALIDITY(s
->context
, direction
== PA_STREAM_PLAYBACK
|| flags
== 0, PA_ERR_INVALID
);
463 PA_CHECK_VALIDITY(s
->context
, !volume
|| volume
->channels
== s
->sample_spec
.channels
, PA_ERR_INVALID
);
464 PA_CHECK_VALIDITY(s
->context
, !sync_stream
|| (direction
== PA_STREAM_PLAYBACK
&& sync_stream
->direction
== PA_STREAM_PLAYBACK
), PA_ERR_INVALID
);
468 s
->direction
= direction
;
472 s
->syncid
= sync_stream
->syncid
;
475 s
->buffer_attr
= *attr
;
478 s
->buffer_attr
.tlength
= pa_bytes_per_second(&s
->sample_spec
)/2;
479 s
->buffer_attr
.maxlength
= (s
->buffer_attr
.tlength
*3)/2;
480 s
->buffer_attr
.minreq
= s
->buffer_attr
.tlength
/100;
481 s
->buffer_attr
.prebuf
= s
->buffer_attr
.tlength
- s
->buffer_attr
.minreq
;
482 s
->buffer_attr
.fragsize
= s
->buffer_attr
.tlength
/100;
486 dev
= s
->direction
== PA_STREAM_PLAYBACK
? s
->context
->conf
->default_sink
: s
->context
->conf
->default_source
;
488 t
= pa_tagstruct_command(
490 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
,
495 PA_TAG_STRING
, s
->name
,
496 PA_TAG_SAMPLE_SPEC
, &s
->sample_spec
,
497 PA_TAG_CHANNEL_MAP
, &s
->channel_map
,
498 PA_TAG_U32
, PA_INVALID_INDEX
,
500 PA_TAG_U32
, s
->buffer_attr
.maxlength
,
501 PA_TAG_BOOLEAN
, !!(flags
& PA_STREAM_START_CORKED
),
504 if (s
->direction
== PA_STREAM_PLAYBACK
) {
509 PA_TAG_U32
, s
->buffer_attr
.tlength
,
510 PA_TAG_U32
, s
->buffer_attr
.prebuf
,
511 PA_TAG_U32
, s
->buffer_attr
.minreq
,
512 PA_TAG_U32
, s
->syncid
,
516 volume
= pa_cvolume_reset(&cv
, s
->sample_spec
.channels
);
518 pa_tagstruct_put_cvolume(t
, volume
);
520 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
522 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
523 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
);
525 pa_stream_set_state(s
, PA_STREAM_CREATING
);
531 int pa_stream_connect_playback(
534 const pa_buffer_attr
*attr
,
535 pa_stream_flags_t flags
,
537 pa_stream
*sync_stream
) {
542 return create_stream(PA_STREAM_PLAYBACK
, s
, dev
, attr
, flags
, volume
, sync_stream
);
545 int pa_stream_connect_record(
548 const pa_buffer_attr
*attr
,
549 pa_stream_flags_t flags
) {
554 return create_stream(PA_STREAM_RECORD
, s
, dev
, attr
, flags
, NULL
, NULL
);
561 void (*free_cb
)(void *p
),
563 pa_seek_mode_t seek
) {
571 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
572 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| s
->direction
== PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
573 PA_CHECK_VALIDITY(s
->context
, seek
<= PA_SEEK_RELATIVE_END
, PA_ERR_INVALID
);
574 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| (seek
== PA_SEEK_RELATIVE
&& offset
== 0), PA_ERR_INVALID
);
580 chunk
.memblock
= pa_memblock_new_user((void*) data
, length
, free_cb
, 1, s
->context
->memblock_stat
);
582 chunk
.memblock
= pa_memblock_new(length
, s
->context
->memblock_stat
);
583 memcpy(chunk
.memblock
->data
, data
, length
);
587 chunk
.length
= length
;
589 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, offset
, seek
, &chunk
);
590 pa_memblock_unref(chunk
.memblock
);
592 if (length
< s
->requested_bytes
)
593 s
->requested_bytes
-= length
;
595 s
->requested_bytes
= 0;
597 if (s
->direction
== PA_STREAM_PLAYBACK
) {
599 /* Update latency request correction */
600 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
) {
602 if (seek
== PA_SEEK_ABSOLUTE
) {
603 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 0;
604 s
->write_index_corrections
[s
->current_write_index_correction
].absolute
= 1;
605 s
->write_index_corrections
[s
->current_write_index_correction
].value
= offset
+ length
;
606 } else if (seek
== PA_SEEK_RELATIVE
) {
607 if (!s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
)
608 s
->write_index_corrections
[s
->current_write_index_correction
].value
+= offset
+ length
;
610 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 1;
613 /* Update the write index in the already available latency data */
614 if (s
->timing_info_valid
) {
616 if (seek
== PA_SEEK_ABSOLUTE
) {
617 s
->timing_info
.write_index_corrupt
= 0;
618 s
->timing_info
.write_index
= offset
+ length
;
619 } else if (seek
== PA_SEEK_RELATIVE
) {
620 if (!s
->timing_info
.write_index_corrupt
)
621 s
->timing_info
.write_index
+= offset
+ length
;
623 s
->timing_info
.write_index_corrupt
= 1;
626 if (!s
->timing_info_valid
|| s
->timing_info
.write_index_corrupt
)
627 request_auto_timing_update(s
, 1);
633 int pa_stream_peek(pa_stream
*s
, const void **data
, size_t *length
) {
639 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
640 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
642 if (!s
->peek_memchunk
.memblock
) {
644 if (pa_memblockq_peek(s
->record_memblockq
, &s
->peek_memchunk
) < 0) {
651 *data
= (const char*) s
->peek_memchunk
.memblock
->data
+ s
->peek_memchunk
.index
;
652 *length
= s
->peek_memchunk
.length
;
656 int pa_stream_drop(pa_stream
*s
) {
660 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
661 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
662 PA_CHECK_VALIDITY(s
->context
, s
->peek_memchunk
.memblock
, PA_ERR_BADSTATE
);
664 pa_memblockq_drop(s
->record_memblockq
, &s
->peek_memchunk
, s
->peek_memchunk
.length
);
666 pa_memblock_unref(s
->peek_memchunk
.memblock
);
667 s
->peek_memchunk
.length
= 0;
668 s
->peek_memchunk
.index
= 0;
669 s
->peek_memchunk
.memblock
= NULL
;
674 size_t pa_stream_writable_size(pa_stream
*s
) {
678 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
679 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
681 return s
->requested_bytes
;
684 size_t pa_stream_readable_size(pa_stream
*s
) {
688 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
689 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
691 return pa_memblockq_get_length(s
->record_memblockq
);
694 pa_operation
* pa_stream_drain(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
702 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
703 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
705 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
707 t
= pa_tagstruct_command(s
->context
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
, &tag
);
708 pa_tagstruct_putu32(t
, s
->channel
);
709 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
710 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
712 return pa_operation_ref(o
);
715 static void stream_get_timing_info_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
716 pa_operation
*o
= userdata
;
717 struct timeval local
, remote
, now
;
725 i
= &o
->stream
->timing_info
;
727 /* 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); */
729 o
->stream
->timing_info_valid
= 0;
730 i
->write_index_corrupt
= 0;
731 i
->read_index_corrupt
= 0;
733 /* pa_log("timing update %u\n", tag); */
735 if (command
!= PA_COMMAND_REPLY
) {
736 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
739 } else if (pa_tagstruct_get_usec(t
, &i
->sink_usec
) < 0 ||
740 pa_tagstruct_get_usec(t
, &i
->source_usec
) < 0 ||
741 pa_tagstruct_get_boolean(t
, &i
->playing
) < 0 ||
742 pa_tagstruct_get_timeval(t
, &local
) < 0 ||
743 pa_tagstruct_get_timeval(t
, &remote
) < 0 ||
744 pa_tagstruct_gets64(t
, &i
->write_index
) < 0 ||
745 pa_tagstruct_gets64(t
, &i
->read_index
) < 0 ||
746 !pa_tagstruct_eof(t
)) {
747 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
751 o
->stream
->timing_info_valid
= 1;
753 pa_gettimeofday(&now
);
755 /* Calculcate timestamps */
756 if (pa_timeval_cmp(&local
, &remote
) <= 0 && pa_timeval_cmp(&remote
, &now
) <= 0) {
757 /* local and remote seem to have synchronized clocks */
759 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
)
760 i
->transport_usec
= pa_timeval_diff(&remote
, &local
);
762 i
->transport_usec
= pa_timeval_diff(&now
, &remote
);
764 i
->synchronized_clocks
= 1;
765 i
->timestamp
= remote
;
767 /* clocks are not synchronized, let's estimate latency then */
768 i
->transport_usec
= pa_timeval_diff(&now
, &local
)/2;
769 i
->synchronized_clocks
= 0;
770 i
->timestamp
= local
;
771 pa_timeval_add(&i
->timestamp
, i
->transport_usec
);
774 /* Invalidate read and write indexes if necessary */
775 if (tag
< o
->stream
->read_index_not_before
)
776 i
->read_index_corrupt
= 1;
778 if (tag
< o
->stream
->write_index_not_before
)
779 i
->write_index_corrupt
= 1;
781 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
782 /* Write index correction */
787 /* Go through the saved correction values and add up the total correction.*/
789 for (n
= 0, j
= o
->stream
->current_write_index_correction
+1;
790 n
< PA_MAX_WRITE_INDEX_CORRECTIONS
;
791 n
++, j
= (j
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
) {
793 /* Step over invalid data or out-of-date data */
794 if (!o
->stream
->write_index_corrections
[j
].valid
||
795 o
->stream
->write_index_corrections
[j
].tag
< ctag
)
798 /* Make sure that everything is in order */
799 ctag
= o
->stream
->write_index_corrections
[j
].tag
+1;
801 /* Now fix the write index */
802 if (o
->stream
->write_index_corrections
[j
].corrupt
) {
803 /* A corrupting seek was made */
805 i
->write_index_corrupt
= 1;
806 } else if (o
->stream
->write_index_corrections
[j
].absolute
) {
807 /* An absolute seek was made */
808 i
->write_index
= o
->stream
->write_index_corrections
[j
].value
;
809 i
->write_index_corrupt
= 0;
810 } else if (!i
->write_index_corrupt
) {
811 /* A relative seek was made */
812 i
->write_index
+= o
->stream
->write_index_corrections
[j
].value
;
817 o
->stream
->ipol_timestamp
= now
;
818 o
->stream
->ipol_usec_valid
= 0;
821 o
->stream
->auto_timing_update_requested
= 0;
822 /* 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); */
824 /* Clear old correction entries */
825 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
828 for (n
= 0; n
< PA_MAX_WRITE_INDEX_CORRECTIONS
; n
++) {
829 if (!o
->stream
->write_index_corrections
[n
].valid
)
832 if (o
->stream
->write_index_corrections
[n
].tag
<= tag
)
833 o
->stream
->write_index_corrections
[n
].valid
= 0;
838 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
839 cb(o
->stream
, o
->stream
->timing_info_valid
, o
->userdata
);
844 pa_operation_done(o
);
845 pa_operation_unref(o
);
848 pa_operation
* pa_stream_update_timing_info(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
858 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
859 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
861 if (s
->direction
== PA_STREAM_PLAYBACK
) {
862 /* Find a place to store the write_index correction data for this entry */
863 cidx
= (s
->current_write_index_correction
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
;
865 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
866 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !s
->write_index_corrections
[cidx
].valid
, PA_ERR_INTERNAL
);
868 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
870 t
= pa_tagstruct_command(
872 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_GET_PLAYBACK_LATENCY
: PA_COMMAND_GET_RECORD_LATENCY
,
874 pa_tagstruct_putu32(t
, s
->channel
);
875 pa_tagstruct_put_timeval(t
, pa_gettimeofday(&now
));
877 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
878 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, stream_get_timing_info_callback
, o
);
880 if (s
->direction
== PA_STREAM_PLAYBACK
) {
881 /* Fill in initial correction data */
882 o
->stream
->current_write_index_correction
= cidx
;
883 o
->stream
->write_index_corrections
[cidx
].valid
= 1;
884 o
->stream
->write_index_corrections
[cidx
].tag
= tag
;
885 o
->stream
->write_index_corrections
[cidx
].absolute
= 0;
886 o
->stream
->write_index_corrections
[cidx
].value
= 0;
887 o
->stream
->write_index_corrections
[cidx
].corrupt
= 0;
890 /* pa_log("requesting update %u\n", tag); */
892 return pa_operation_ref(o
);
895 void pa_stream_disconnect_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
896 pa_stream
*s
= userdata
;
904 if (command
!= PA_COMMAND_REPLY
) {
905 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
908 pa_stream_set_state(s
, PA_STREAM_FAILED
);
910 } else if (!pa_tagstruct_eof(t
)) {
911 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
915 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
921 int pa_stream_disconnect(pa_stream
*s
) {
928 PA_CHECK_VALIDITY(s
->context
, s
->channel_valid
, PA_ERR_BADSTATE
);
929 PA_CHECK_VALIDITY(s
->context
, s
->context
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
933 t
= pa_tagstruct_command(
935 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
936 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
),
938 pa_tagstruct_putu32(t
, s
->channel
);
939 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
940 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
);
946 void pa_stream_set_read_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
950 s
->read_callback
= cb
;
951 s
->read_userdata
= userdata
;
954 void pa_stream_set_write_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
958 s
->write_callback
= cb
;
959 s
->write_userdata
= userdata
;
962 void pa_stream_set_state_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
966 s
->state_callback
= cb
;
967 s
->state_userdata
= userdata
;
970 void pa_stream_set_overflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
974 s
->overflow_callback
= cb
;
975 s
->overflow_userdata
= userdata
;
978 void pa_stream_set_underflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
982 s
->underflow_callback
= cb
;
983 s
->underflow_userdata
= userdata
;
986 void pa_stream_simple_ack_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
987 pa_operation
*o
= userdata
;
995 if (command
!= PA_COMMAND_REPLY
) {
996 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
1000 } else if (!pa_tagstruct_eof(t
)) {
1001 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1006 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
1007 cb(o
->stream
, success
, o
->userdata
);
1011 pa_operation_done(o
);
1012 pa_operation_unref(o
);
1015 pa_operation
* pa_stream_cork(pa_stream
*s
, int b
, pa_stream_success_cb_t cb
, void *userdata
) {
1021 assert(s
->ref
>= 1);
1023 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1024 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1028 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1030 t
= pa_tagstruct_command(
1032 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CORK_PLAYBACK_STREAM
: PA_COMMAND_CORK_RECORD_STREAM
,
1034 pa_tagstruct_putu32(t
, s
->channel
);
1035 pa_tagstruct_put_boolean(t
, !!b
);
1036 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1037 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
1039 if (s
->direction
== PA_STREAM_PLAYBACK
)
1040 invalidate_indexes(s
, 1, 0);
1042 return pa_operation_ref(o
);
1045 static pa_operation
* stream_send_simple_command(pa_stream
*s
, uint32_t command
, pa_stream_success_cb_t cb
, void *userdata
) {
1051 assert(s
->ref
>= 1);
1053 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1055 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1057 t
= pa_tagstruct_command(s
->context
, command
, &tag
);
1058 pa_tagstruct_putu32(t
, s
->channel
);
1059 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1060 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
1062 return pa_operation_ref(o
);
1065 pa_operation
* pa_stream_flush(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1068 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1070 if ((o
= stream_send_simple_command(s
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_FLUSH_PLAYBACK_STREAM
: PA_COMMAND_FLUSH_RECORD_STREAM
, cb
, userdata
))) {
1072 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1073 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
)
1074 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 1;
1076 if (s
->timing_info_valid
)
1077 s
->timing_info
.write_index_corrupt
= 1;
1079 if (s
->buffer_attr
.prebuf
> 0)
1080 invalidate_indexes(s
, 1, 0);
1082 request_auto_timing_update(s
, 1);
1084 invalidate_indexes(s
, 0, 1);
1090 pa_operation
* pa_stream_prebuf(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1093 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1094 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
1096 if ((o
= stream_send_simple_command(s
, PA_COMMAND_PREBUF_PLAYBACK_STREAM
, cb
, userdata
)))
1097 invalidate_indexes(s
, 1, 0);
1102 pa_operation
* pa_stream_trigger(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1105 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1106 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
1108 if ((o
= stream_send_simple_command(s
, PA_COMMAND_TRIGGER_PLAYBACK_STREAM
, cb
, userdata
)))
1109 invalidate_indexes(s
, 1, 0);
1114 pa_operation
* pa_stream_set_name(pa_stream
*s
, const char *name
, pa_stream_success_cb_t cb
, void *userdata
) {
1120 assert(s
->ref
>= 1);
1123 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1124 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1126 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1128 t
= pa_tagstruct_command(
1130 s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_NAME
: PA_COMMAND_SET_PLAYBACK_STREAM_NAME
,
1132 pa_tagstruct_putu32(t
, s
->channel
);
1133 pa_tagstruct_puts(t
, name
);
1134 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1135 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
1137 return pa_operation_ref(o
);
1140 int pa_stream_get_time(pa_stream
*s
, pa_usec_t
*r_usec
) {
1144 assert(s
->ref
>= 1);
1146 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1147 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1148 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1149 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
1150 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
1152 if (s
->flags
& PA_STREAM_INTERPOLATE_TIMING
&& s
->ipol_usec_valid
)
1153 usec
= s
->ipol_usec
;
1155 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1156 /* The last byte that was written into the output device
1157 * had this time value associated */
1158 usec
= pa_bytes_to_usec(s
->timing_info
.read_index
< 0 ? 0 : (uint64_t) s
->timing_info
.read_index
, &s
->sample_spec
);
1161 /* Because the latency info took a little time to come
1162 * to us, we assume that the real output time is actually
1164 usec
+= s
->timing_info
.transport_usec
;
1166 /* However, the output device usually maintains a buffer
1167 too, hence the real sample currently played is a little
1169 if (s
->timing_info
.sink_usec
>= usec
)
1172 usec
-= s
->timing_info
.sink_usec
;
1175 } else if (s
->direction
== PA_STREAM_RECORD
) {
1176 /* The last byte written into the server side queue had
1177 * this time value associated */
1178 usec
= pa_bytes_to_usec(s
->timing_info
.write_index
< 0 ? 0 : (uint64_t) s
->timing_info
.write_index
, &s
->sample_spec
);
1181 /* Add transport latency */
1182 usec
+= s
->timing_info
.transport_usec
;
1184 /* Add latency of data in device buffer */
1185 usec
+= s
->timing_info
.source_usec
;
1187 /* If this is a monitor source, we need to correct the
1188 * time by the playback device buffer */
1189 if (s
->timing_info
.sink_usec
>= usec
)
1192 usec
-= s
->timing_info
.sink_usec
;
1196 if (s
->flags
& PA_STREAM_INTERPOLATE_TIMING
) {
1197 s
->ipol_usec
= usec
;
1198 s
->ipol_usec_valid
= 1;
1202 /* Interpolate if requested */
1203 if (s
->flags
& PA_STREAM_INTERPOLATE_TIMING
) {
1205 /* We just add the time that passed since the latency info was
1210 usec
+= pa_timeval_diff(pa_gettimeofday(&now
), &s
->ipol_timestamp
);
1211 s
->ipol_timestamp
= now
;
1215 /* Make sure the time runs monotonically */
1216 if (!(s
->flags
& PA_STREAM_NOT_MONOTONOUS
)) {
1217 if (usec
< s
->previous_time
)
1218 usec
= s
->previous_time
;
1220 s
->previous_time
= usec
;
1229 static pa_usec_t
time_counter_diff(pa_stream
*s
, pa_usec_t a
, pa_usec_t b
, int *negative
) {
1231 assert(s
->ref
>= 1);
1239 if (negative
&& s
->direction
== PA_STREAM_RECORD
) {
1247 int pa_stream_get_latency(pa_stream
*s
, pa_usec_t
*r_usec
, int *negative
) {
1253 assert(s
->ref
>= 1);
1256 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1257 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1258 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1259 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
1260 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
1262 if ((r
= pa_stream_get_time(s
, &t
)) < 0)
1265 if (s
->direction
== PA_STREAM_PLAYBACK
)
1266 cindex
= s
->timing_info
.write_index
;
1268 cindex
= s
->timing_info
.read_index
;
1273 c
= pa_bytes_to_usec(cindex
, &s
->sample_spec
);
1275 if (s
->direction
== PA_STREAM_PLAYBACK
)
1276 *r_usec
= time_counter_diff(s
, c
, t
, negative
);
1278 *r_usec
= time_counter_diff(s
, t
, c
, negative
);
1283 const pa_timing_info
* pa_stream_get_timing_info(pa_stream
*s
) {
1285 assert(s
->ref
>= 1);
1287 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1288 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1289 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->timing_info_valid
, PA_ERR_BADSTATE
);
1291 return &s
->timing_info
;
1294 const pa_sample_spec
* pa_stream_get_sample_spec(pa_stream
*s
) {
1296 assert(s
->ref
>= 1);
1298 return &s
->sample_spec
;
1301 const pa_channel_map
* pa_stream_get_channel_map(pa_stream
*s
) {
1303 assert(s
->ref
>= 1);
1305 return &s
->channel_map
;