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 <polyp/timeval.h>
33 #include <polyp/xmalloc.h>
35 #include <polypcore/pstream-util.h>
36 #include <polypcore/log.h>
37 #include <polypcore/hashmap.h>
41 #define LATENCY_IPOL_INTERVAL_USEC (10000L)
43 pa_stream
*pa_stream_new(pa_context
*c
, const char *name
, const pa_sample_spec
*ss
, const pa_channel_map
*map
) {
49 PA_CHECK_VALIDITY_RETURN_NULL(c
, ss
&& pa_sample_spec_valid(ss
), PA_ERR_INVALID
);
50 PA_CHECK_VALIDITY_RETURN_NULL(c
, !map
|| (pa_channel_map_valid(map
) && map
->channels
== ss
->channels
), PA_ERR_INVALID
);
52 s
= pa_xnew(pa_stream
, 1);
55 s
->mainloop
= c
->mainloop
;
57 s
->read_callback
= NULL
;
58 s
->read_userdata
= NULL
;
59 s
->write_callback
= NULL
;
60 s
->write_userdata
= NULL
;
61 s
->state_callback
= NULL
;
62 s
->state_userdata
= NULL
;
63 s
->overflow_callback
= NULL
;
64 s
->overflow_userdata
= NULL
;
65 s
->underflow_callback
= NULL
;
66 s
->underflow_userdata
= NULL
;
67 s
->latency_update_callback
= NULL
;
68 s
->latency_update_userdata
= NULL
;
70 s
->direction
= PA_STREAM_NODIRECTION
;
71 s
->name
= pa_xstrdup(name
);
76 s
->channel_map
= *map
;
78 pa_channel_map_init_auto(&s
->channel_map
, ss
->channels
, PA_CHANNEL_MAP_DEFAULT
);
82 s
->syncid
= c
->csyncid
++;
83 s
->device_index
= PA_INVALID_INDEX
;
84 s
->requested_bytes
= 0;
85 s
->state
= PA_STREAM_UNCONNECTED
;
86 memset(&s
->buffer_attr
, 0, sizeof(s
->buffer_attr
));
88 s
->peek_memchunk
.index
= 0;
89 s
->peek_memchunk
.length
= 0;
90 s
->peek_memchunk
.memblock
= NULL
;
92 s
->record_memblockq
= NULL
;
95 s
->timing_info_valid
= 0;
96 s
->read_index_not_before
= 0;
97 s
->write_index_not_before
= 0;
99 for (i
= 0; i
< PA_MAX_WRITE_INDEX_CORRECTIONS
; i
++)
100 s
->write_index_corrections
[i
].valid
= 0;
101 s
->current_write_index_correction
= 0;
105 s
->ipol_usec_valid
= 0;
106 s
->ipol_timestamp
.tv_sec
= 0;
107 s
->ipol_timestamp
.tv_usec
= 0;
109 s
->auto_timing_update_event
= NULL
;
110 s
->auto_timing_update_requested
= 0;
112 /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
113 PA_LLIST_PREPEND(pa_stream
, c
->streams
, s
);
119 static void stream_free(pa_stream
*s
) {
120 assert(s
&& !s
->context
&& !s
->channel_valid
);
122 if (s
->auto_timing_update_event
) {
124 s
->mainloop
->time_free(s
->auto_timing_update_event
);
127 if (s
->peek_memchunk
.memblock
)
128 pa_memblock_unref(s
->peek_memchunk
.memblock
);
130 if (s
->record_memblockq
)
131 pa_memblockq_free(s
->record_memblockq
);
137 void pa_stream_unref(pa_stream
*s
) {
145 pa_stream
* pa_stream_ref(pa_stream
*s
) {
153 pa_stream_state_t
pa_stream_get_state(pa_stream
*s
) {
160 pa_context
* pa_stream_get_context(pa_stream
*s
) {
167 uint32_t pa_stream_get_index(pa_stream
*s
) {
171 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
173 return s
->device_index
;
176 void pa_stream_set_state(pa_stream
*s
, pa_stream_state_t st
) {
186 if (s
->state_callback
)
187 s
->state_callback(s
, s
->state_userdata
);
189 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
) && s
->context
) {
191 /* Detach from context */
194 /* Unref all operatio object that point to us */
195 for (o
= s
->context
->operations
; o
; o
= n
) {
199 pa_operation_cancel(o
);
202 /* Drop all outstanding replies for this stream */
203 if (s
->context
->pdispatch
)
204 pa_pdispatch_unregister_reply(s
->context
->pdispatch
, s
);
206 if (s
->channel_valid
)
207 pa_dynarray_put((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, s
->channel
, NULL
);
209 PA_LLIST_REMOVE(pa_stream
, s
->context
->streams
, s
);
213 s
->channel_valid
= 0;
221 void pa_command_stream_killed(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
222 pa_context
*c
= userdata
;
227 assert(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
);
233 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
234 !pa_tagstruct_eof(t
)) {
235 pa_context_fail(c
, PA_ERR_PROTOCOL
);
239 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, channel
)))
242 pa_context_set_error(c
, PA_ERR_KILLED
);
243 pa_stream_set_state(s
, PA_STREAM_FAILED
);
249 void pa_command_request(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
251 pa_context
*c
= userdata
;
252 uint32_t bytes
, channel
;
255 assert(command
== PA_COMMAND_REQUEST
);
261 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
262 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
263 !pa_tagstruct_eof(t
)) {
264 pa_context_fail(c
, PA_ERR_PROTOCOL
);
268 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
271 if (s
->state
== PA_STREAM_READY
) {
272 s
->requested_bytes
+= bytes
;
274 if (s
->requested_bytes
> 0 && s
->write_callback
)
275 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
282 void pa_command_overflow_or_underflow(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
284 pa_context
*c
= userdata
;
288 assert(command
== PA_COMMAND_OVERFLOW
|| command
== PA_COMMAND_UNDERFLOW
);
294 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
295 !pa_tagstruct_eof(t
)) {
296 pa_context_fail(c
, PA_ERR_PROTOCOL
);
300 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
303 if (s
->state
== PA_STREAM_READY
) {
305 if (command
== PA_COMMAND_OVERFLOW
) {
306 if (s
->overflow_callback
)
307 s
->overflow_callback(s
, s
->overflow_userdata
);
308 } else if (command
== PA_COMMAND_UNDERFLOW
) {
309 if (s
->underflow_callback
)
310 s
->underflow_callback(s
, s
->underflow_userdata
);
318 static void request_auto_timing_update(pa_stream
*s
, int force
) {
322 if (!(s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
))
325 if (s
->state
== PA_STREAM_READY
&&
326 (force
|| !s
->auto_timing_update_requested
)) {
329 /* pa_log("automatically requesting new timing data"); */
331 if ((o
= pa_stream_update_timing_info(s
, NULL
, NULL
))) {
332 pa_operation_unref(o
);
333 s
->auto_timing_update_requested
= 1;
337 pa_gettimeofday(&next
);
338 pa_timeval_add(&next
, LATENCY_IPOL_INTERVAL_USEC
);
339 s
->mainloop
->time_restart(s
->auto_timing_update_event
, &next
);
342 static void invalidate_indexes(pa_stream
*s
, int r
, int w
) {
345 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
347 if (s
->state
!= PA_STREAM_READY
)
351 s
->write_index_not_before
= s
->context
->ctag
;
353 if (s
->timing_info_valid
)
354 s
->timing_info
.write_index_corrupt
= 1;
356 /* pa_log("write_index invalidated"); */
360 s
->read_index_not_before
= s
->context
->ctag
;
362 if (s
->timing_info_valid
)
363 s
->timing_info
.read_index_corrupt
= 1;
365 /* pa_log("read_index invalidated"); */
368 if ((s
->direction
== PA_STREAM_PLAYBACK
&& r
) ||
369 (s
->direction
== PA_STREAM_RECORD
&& w
))
370 s
->ipol_usec_valid
= 0;
372 request_auto_timing_update(s
, 1);
375 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
) {
376 pa_stream
*s
= userdata
;
378 /* pa_log("time event"); */
381 request_auto_timing_update(s
, 0);
385 void pa_create_stream_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
386 pa_stream
*s
= userdata
;
390 assert(s
->state
== PA_STREAM_CREATING
);
394 if (command
!= PA_COMMAND_REPLY
) {
395 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
398 pa_stream_set_state(s
, PA_STREAM_FAILED
);
402 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
403 ((s
->direction
!= PA_STREAM_UPLOAD
) && pa_tagstruct_getu32(t
, &s
->device_index
) < 0) ||
404 ((s
->direction
!= PA_STREAM_RECORD
) && pa_tagstruct_getu32(t
, &s
->requested_bytes
) < 0) ||
405 !pa_tagstruct_eof(t
)) {
406 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
410 if (s
->direction
== PA_STREAM_RECORD
) {
411 assert(!s
->record_memblockq
);
413 s
->record_memblockq
= pa_memblockq_new(
415 s
->buffer_attr
.maxlength
,
417 pa_frame_size(&s
->sample_spec
),
421 s
->context
->memblock_stat
);
424 s
->channel_valid
= 1;
425 pa_dynarray_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, s
->channel
, s
);
427 pa_stream_set_state(s
, PA_STREAM_READY
);
429 if (s
->direction
!= PA_STREAM_UPLOAD
&&
430 s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) {
433 pa_gettimeofday(&tv
);
434 tv
.tv_usec
+= LATENCY_IPOL_INTERVAL_USEC
; /* every 100 ms */
436 assert(!s
->auto_timing_update_event
);
437 s
->auto_timing_update_event
= s
->mainloop
->time_new(s
->mainloop
, &tv
, &auto_timing_update_callback
, s
);
439 request_auto_timing_update(s
, 1);
442 if (s
->requested_bytes
> 0 && s
->ref
> 1 && s
->write_callback
)
443 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
449 static int create_stream(
450 pa_stream_direction_t direction
,
453 const pa_buffer_attr
*attr
,
454 pa_stream_flags_t flags
,
455 const pa_cvolume
*volume
,
456 pa_stream
*sync_stream
) {
464 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_UNCONNECTED
, PA_ERR_BADSTATE
);
465 PA_CHECK_VALIDITY(s
->context
, !(flags
& ~((direction
!= PA_STREAM_UPLOAD
?
466 PA_STREAM_START_CORKED
|
467 PA_STREAM_INTERPOLATE_TIMING
|
468 PA_STREAM_NOT_MONOTONOUS
|
469 PA_STREAM_AUTO_TIMING_UPDATE
: 0))), PA_ERR_INVALID
);
470 PA_CHECK_VALIDITY(s
->context
, !volume
|| volume
->channels
== s
->sample_spec
.channels
, PA_ERR_INVALID
);
471 PA_CHECK_VALIDITY(s
->context
, !sync_stream
|| (direction
== PA_STREAM_PLAYBACK
&& sync_stream
->direction
== PA_STREAM_PLAYBACK
), PA_ERR_INVALID
);
475 s
->direction
= direction
;
479 s
->syncid
= sync_stream
->syncid
;
482 s
->buffer_attr
= *attr
;
485 s
->buffer_attr
.tlength
= pa_bytes_per_second(&s
->sample_spec
)/2;
486 s
->buffer_attr
.maxlength
= (s
->buffer_attr
.tlength
*3)/2;
487 s
->buffer_attr
.minreq
= s
->buffer_attr
.tlength
/100;
488 s
->buffer_attr
.prebuf
= s
->buffer_attr
.tlength
- s
->buffer_attr
.minreq
;
489 s
->buffer_attr
.fragsize
= s
->buffer_attr
.tlength
/100;
493 dev
= s
->direction
== PA_STREAM_PLAYBACK
? s
->context
->conf
->default_sink
: s
->context
->conf
->default_source
;
495 t
= pa_tagstruct_command(
497 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
,
502 PA_TAG_STRING
, s
->name
,
503 PA_TAG_SAMPLE_SPEC
, &s
->sample_spec
,
504 PA_TAG_CHANNEL_MAP
, &s
->channel_map
,
505 PA_TAG_U32
, PA_INVALID_INDEX
,
507 PA_TAG_U32
, s
->buffer_attr
.maxlength
,
508 PA_TAG_BOOLEAN
, !!(flags
& PA_STREAM_START_CORKED
),
511 if (s
->direction
== PA_STREAM_PLAYBACK
) {
516 PA_TAG_U32
, s
->buffer_attr
.tlength
,
517 PA_TAG_U32
, s
->buffer_attr
.prebuf
,
518 PA_TAG_U32
, s
->buffer_attr
.minreq
,
519 PA_TAG_U32
, s
->syncid
,
523 volume
= pa_cvolume_reset(&cv
, s
->sample_spec
.channels
);
525 pa_tagstruct_put_cvolume(t
, volume
);
527 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
529 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
530 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
, NULL
);
532 pa_stream_set_state(s
, PA_STREAM_CREATING
);
538 int pa_stream_connect_playback(
541 const pa_buffer_attr
*attr
,
542 pa_stream_flags_t flags
,
544 pa_stream
*sync_stream
) {
549 return create_stream(PA_STREAM_PLAYBACK
, s
, dev
, attr
, flags
, volume
, sync_stream
);
552 int pa_stream_connect_record(
555 const pa_buffer_attr
*attr
,
556 pa_stream_flags_t flags
) {
561 return create_stream(PA_STREAM_RECORD
, s
, dev
, attr
, flags
, NULL
, NULL
);
568 void (*free_cb
)(void *p
),
570 pa_seek_mode_t seek
) {
578 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
579 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| s
->direction
== PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
580 PA_CHECK_VALIDITY(s
->context
, seek
<= PA_SEEK_RELATIVE_END
, PA_ERR_INVALID
);
581 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| (seek
== PA_SEEK_RELATIVE
&& offset
== 0), PA_ERR_INVALID
);
587 chunk
.memblock
= pa_memblock_new_user((void*) data
, length
, free_cb
, 1, s
->context
->memblock_stat
);
589 chunk
.memblock
= pa_memblock_new(length
, s
->context
->memblock_stat
);
590 memcpy(chunk
.memblock
->data
, data
, length
);
594 chunk
.length
= length
;
596 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, offset
, seek
, &chunk
);
597 pa_memblock_unref(chunk
.memblock
);
599 if (length
< s
->requested_bytes
)
600 s
->requested_bytes
-= length
;
602 s
->requested_bytes
= 0;
604 if (s
->direction
== PA_STREAM_PLAYBACK
) {
606 /* Update latency request correction */
607 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
) {
609 if (seek
== PA_SEEK_ABSOLUTE
) {
610 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 0;
611 s
->write_index_corrections
[s
->current_write_index_correction
].absolute
= 1;
612 s
->write_index_corrections
[s
->current_write_index_correction
].value
= offset
+ length
;
613 } else if (seek
== PA_SEEK_RELATIVE
) {
614 if (!s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
)
615 s
->write_index_corrections
[s
->current_write_index_correction
].value
+= offset
+ length
;
617 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 1;
620 /* Update the write index in the already available latency data */
621 if (s
->timing_info_valid
) {
623 if (seek
== PA_SEEK_ABSOLUTE
) {
624 s
->timing_info
.write_index_corrupt
= 0;
625 s
->timing_info
.write_index
= offset
+ length
;
626 } else if (seek
== PA_SEEK_RELATIVE
) {
627 if (!s
->timing_info
.write_index_corrupt
)
628 s
->timing_info
.write_index
+= offset
+ length
;
630 s
->timing_info
.write_index_corrupt
= 1;
633 if (!s
->timing_info_valid
|| s
->timing_info
.write_index_corrupt
)
634 request_auto_timing_update(s
, 1);
640 int pa_stream_peek(pa_stream
*s
, const void **data
, size_t *length
) {
646 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
647 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
649 if (!s
->peek_memchunk
.memblock
) {
651 if (pa_memblockq_peek(s
->record_memblockq
, &s
->peek_memchunk
) < 0) {
658 *data
= (const char*) s
->peek_memchunk
.memblock
->data
+ s
->peek_memchunk
.index
;
659 *length
= s
->peek_memchunk
.length
;
663 int pa_stream_drop(pa_stream
*s
) {
667 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
668 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
669 PA_CHECK_VALIDITY(s
->context
, s
->peek_memchunk
.memblock
, PA_ERR_BADSTATE
);
671 pa_memblockq_drop(s
->record_memblockq
, &s
->peek_memchunk
, s
->peek_memchunk
.length
);
673 /* Fix the simulated local read index */
674 if (s
->timing_info_valid
&& !s
->timing_info
.read_index_corrupt
)
675 s
->timing_info
.read_index
+= s
->peek_memchunk
.length
;
677 pa_memblock_unref(s
->peek_memchunk
.memblock
);
678 s
->peek_memchunk
.length
= 0;
679 s
->peek_memchunk
.index
= 0;
680 s
->peek_memchunk
.memblock
= NULL
;
685 size_t pa_stream_writable_size(pa_stream
*s
) {
689 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
690 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
692 return s
->requested_bytes
;
695 size_t pa_stream_readable_size(pa_stream
*s
) {
699 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
700 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
702 return pa_memblockq_get_length(s
->record_memblockq
);
705 pa_operation
* pa_stream_drain(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
713 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
714 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
716 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
718 t
= pa_tagstruct_command(s
->context
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
, &tag
);
719 pa_tagstruct_putu32(t
, s
->channel
);
720 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
721 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
);
726 static void stream_get_timing_info_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
727 pa_operation
*o
= userdata
;
728 struct timeval local
, remote
, now
;
734 if (!o
->context
|| !o
->stream
)
737 i
= &o
->stream
->timing_info
;
739 /* 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); */
741 o
->stream
->timing_info_valid
= 0;
742 i
->write_index_corrupt
= 0;
743 i
->read_index_corrupt
= 0;
745 /* pa_log("timing update %u\n", tag); */
747 if (command
!= PA_COMMAND_REPLY
) {
748 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
751 } else if (pa_tagstruct_get_usec(t
, &i
->sink_usec
) < 0 ||
752 pa_tagstruct_get_usec(t
, &i
->source_usec
) < 0 ||
753 pa_tagstruct_get_boolean(t
, &i
->playing
) < 0 ||
754 pa_tagstruct_get_timeval(t
, &local
) < 0 ||
755 pa_tagstruct_get_timeval(t
, &remote
) < 0 ||
756 pa_tagstruct_gets64(t
, &i
->write_index
) < 0 ||
757 pa_tagstruct_gets64(t
, &i
->read_index
) < 0 ||
758 !pa_tagstruct_eof(t
)) {
759 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
763 o
->stream
->timing_info_valid
= 1;
765 pa_gettimeofday(&now
);
767 /* Calculcate timestamps */
768 if (pa_timeval_cmp(&local
, &remote
) <= 0 && pa_timeval_cmp(&remote
, &now
) <= 0) {
769 /* local and remote seem to have synchronized clocks */
771 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
)
772 i
->transport_usec
= pa_timeval_diff(&remote
, &local
);
774 i
->transport_usec
= pa_timeval_diff(&now
, &remote
);
776 i
->synchronized_clocks
= 1;
777 i
->timestamp
= remote
;
779 /* clocks are not synchronized, let's estimate latency then */
780 i
->transport_usec
= pa_timeval_diff(&now
, &local
)/2;
781 i
->synchronized_clocks
= 0;
782 i
->timestamp
= local
;
783 pa_timeval_add(&i
->timestamp
, i
->transport_usec
);
786 /* Invalidate read and write indexes if necessary */
787 if (tag
< o
->stream
->read_index_not_before
)
788 i
->read_index_corrupt
= 1;
790 if (tag
< o
->stream
->write_index_not_before
)
791 i
->write_index_corrupt
= 1;
793 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
794 /* Write index correction */
799 /* Go through the saved correction values and add up the total correction.*/
801 for (n
= 0, j
= o
->stream
->current_write_index_correction
+1;
802 n
< PA_MAX_WRITE_INDEX_CORRECTIONS
;
803 n
++, j
= (j
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
) {
805 /* Step over invalid data or out-of-date data */
806 if (!o
->stream
->write_index_corrections
[j
].valid
||
807 o
->stream
->write_index_corrections
[j
].tag
< ctag
)
810 /* Make sure that everything is in order */
811 ctag
= o
->stream
->write_index_corrections
[j
].tag
+1;
813 /* Now fix the write index */
814 if (o
->stream
->write_index_corrections
[j
].corrupt
) {
815 /* A corrupting seek was made */
817 i
->write_index_corrupt
= 1;
818 } else if (o
->stream
->write_index_corrections
[j
].absolute
) {
819 /* An absolute seek was made */
820 i
->write_index
= o
->stream
->write_index_corrections
[j
].value
;
821 i
->write_index_corrupt
= 0;
822 } else if (!i
->write_index_corrupt
) {
823 /* A relative seek was made */
824 i
->write_index
+= o
->stream
->write_index_corrections
[j
].value
;
829 if (o
->stream
->direction
== PA_STREAM_RECORD
) {
830 /* Read index correction */
832 if (!i
->read_index_corrupt
)
833 i
->read_index
-= pa_memblockq_get_length(o
->stream
->record_memblockq
);
836 o
->stream
->ipol_timestamp
= now
;
837 o
->stream
->ipol_usec_valid
= 0;
840 o
->stream
->auto_timing_update_requested
= 0;
841 /* 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); */
843 /* Clear old correction entries */
844 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
847 for (n
= 0; n
< PA_MAX_WRITE_INDEX_CORRECTIONS
; n
++) {
848 if (!o
->stream
->write_index_corrections
[n
].valid
)
851 if (o
->stream
->write_index_corrections
[n
].tag
<= tag
)
852 o
->stream
->write_index_corrections
[n
].valid
= 0;
856 if (o
->stream
->latency_update_callback
)
857 o
->stream
->latency_update_callback(o
->stream
, o
->stream
->latency_update_userdata
);
859 if (o
->callback
&& o
->stream
&& o
->stream
->state
== PA_STREAM_READY
) {
860 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
861 cb(o
->stream
, o
->stream
->timing_info_valid
, o
->userdata
);
866 pa_operation_done(o
);
867 pa_operation_unref(o
);
870 pa_operation
* pa_stream_update_timing_info(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
880 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
881 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
883 if (s
->direction
== PA_STREAM_PLAYBACK
) {
884 /* Find a place to store the write_index correction data for this entry */
885 cidx
= (s
->current_write_index_correction
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
;
887 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
888 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !s
->write_index_corrections
[cidx
].valid
, PA_ERR_INTERNAL
);
890 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
892 t
= pa_tagstruct_command(
894 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_GET_PLAYBACK_LATENCY
: PA_COMMAND_GET_RECORD_LATENCY
,
896 pa_tagstruct_putu32(t
, s
->channel
);
897 pa_tagstruct_put_timeval(t
, pa_gettimeofday(&now
));
899 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
900 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
);
902 if (s
->direction
== PA_STREAM_PLAYBACK
) {
903 /* Fill in initial correction data */
904 o
->stream
->current_write_index_correction
= cidx
;
905 o
->stream
->write_index_corrections
[cidx
].valid
= 1;
906 o
->stream
->write_index_corrections
[cidx
].tag
= tag
;
907 o
->stream
->write_index_corrections
[cidx
].absolute
= 0;
908 o
->stream
->write_index_corrections
[cidx
].value
= 0;
909 o
->stream
->write_index_corrections
[cidx
].corrupt
= 0;
912 /* pa_log("requesting update %u\n", tag); */
917 void pa_stream_disconnect_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
918 pa_stream
*s
= userdata
;
926 if (command
!= PA_COMMAND_REPLY
) {
927 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
930 pa_stream_set_state(s
, PA_STREAM_FAILED
);
932 } else if (!pa_tagstruct_eof(t
)) {
933 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
937 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
943 int pa_stream_disconnect(pa_stream
*s
) {
950 PA_CHECK_VALIDITY(s
->context
, s
->channel_valid
, PA_ERR_BADSTATE
);
951 PA_CHECK_VALIDITY(s
->context
, s
->context
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
955 t
= pa_tagstruct_command(
957 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
958 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
),
960 pa_tagstruct_putu32(t
, s
->channel
);
961 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
962 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
, NULL
);
968 void pa_stream_set_read_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
972 s
->read_callback
= cb
;
973 s
->read_userdata
= userdata
;
976 void pa_stream_set_write_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
980 s
->write_callback
= cb
;
981 s
->write_userdata
= userdata
;
984 void pa_stream_set_state_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
988 s
->state_callback
= cb
;
989 s
->state_userdata
= userdata
;
992 void pa_stream_set_overflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
996 s
->overflow_callback
= cb
;
997 s
->overflow_userdata
= userdata
;
1000 void pa_stream_set_underflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1002 assert(s
->ref
>= 1);
1004 s
->underflow_callback
= cb
;
1005 s
->underflow_userdata
= userdata
;
1008 void pa_stream_set_latency_update_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1010 assert(s
->ref
>= 1);
1012 s
->latency_update_callback
= cb
;
1013 s
->latency_update_userdata
= userdata
;
1016 void pa_stream_simple_ack_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1017 pa_operation
*o
= userdata
;
1022 assert(o
->ref
>= 1);
1027 if (command
!= PA_COMMAND_REPLY
) {
1028 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
1032 } else if (!pa_tagstruct_eof(t
)) {
1033 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1038 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
1039 cb(o
->stream
, success
, o
->userdata
);
1043 pa_operation_done(o
);
1044 pa_operation_unref(o
);
1047 pa_operation
* pa_stream_cork(pa_stream
*s
, int b
, pa_stream_success_cb_t cb
, void *userdata
) {
1053 assert(s
->ref
>= 1);
1055 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1056 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1060 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1062 t
= pa_tagstruct_command(
1064 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CORK_PLAYBACK_STREAM
: PA_COMMAND_CORK_RECORD_STREAM
,
1066 pa_tagstruct_putu32(t
, s
->channel
);
1067 pa_tagstruct_put_boolean(t
, !!b
);
1068 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1069 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
);
1071 if (s
->direction
== PA_STREAM_PLAYBACK
)
1072 invalidate_indexes(s
, 1, 0);
1077 static pa_operation
* stream_send_simple_command(pa_stream
*s
, uint32_t command
, pa_stream_success_cb_t cb
, void *userdata
) {
1083 assert(s
->ref
>= 1);
1085 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1087 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1089 t
= pa_tagstruct_command(s
->context
, command
, &tag
);
1090 pa_tagstruct_putu32(t
, s
->channel
);
1091 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1092 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
);
1097 pa_operation
* pa_stream_flush(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1100 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1102 if ((o
= stream_send_simple_command(s
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_FLUSH_PLAYBACK_STREAM
: PA_COMMAND_FLUSH_RECORD_STREAM
, cb
, userdata
))) {
1104 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1105 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
)
1106 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 1;
1108 if (s
->timing_info_valid
)
1109 s
->timing_info
.write_index_corrupt
= 1;
1111 if (s
->buffer_attr
.prebuf
> 0)
1112 invalidate_indexes(s
, 1, 0);
1114 request_auto_timing_update(s
, 1);
1116 invalidate_indexes(s
, 0, 1);
1122 pa_operation
* pa_stream_prebuf(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1125 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1126 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
1128 if ((o
= stream_send_simple_command(s
, PA_COMMAND_PREBUF_PLAYBACK_STREAM
, cb
, userdata
)))
1129 invalidate_indexes(s
, 1, 0);
1134 pa_operation
* pa_stream_trigger(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1137 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1138 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
1140 if ((o
= stream_send_simple_command(s
, PA_COMMAND_TRIGGER_PLAYBACK_STREAM
, cb
, userdata
)))
1141 invalidate_indexes(s
, 1, 0);
1146 pa_operation
* pa_stream_set_name(pa_stream
*s
, const char *name
, pa_stream_success_cb_t cb
, void *userdata
) {
1152 assert(s
->ref
>= 1);
1155 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1156 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1158 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1160 t
= pa_tagstruct_command(
1162 s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_NAME
: PA_COMMAND_SET_PLAYBACK_STREAM_NAME
,
1164 pa_tagstruct_putu32(t
, s
->channel
);
1165 pa_tagstruct_puts(t
, name
);
1166 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1167 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
);
1172 int pa_stream_get_time(pa_stream
*s
, pa_usec_t
*r_usec
) {
1176 assert(s
->ref
>= 1);
1178 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1179 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1180 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1181 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
1182 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
1184 if (s
->flags
& PA_STREAM_INTERPOLATE_TIMING
&& s
->ipol_usec_valid
)
1185 usec
= s
->ipol_usec
;
1187 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1188 /* The last byte that was written into the output device
1189 * had this time value associated */
1190 usec
= pa_bytes_to_usec(s
->timing_info
.read_index
< 0 ? 0 : (uint64_t) s
->timing_info
.read_index
, &s
->sample_spec
);
1193 /* Because the latency info took a little time to come
1194 * to us, we assume that the real output time is actually
1196 usec
+= s
->timing_info
.transport_usec
;
1198 /* However, the output device usually maintains a buffer
1199 too, hence the real sample currently played is a little
1201 if (s
->timing_info
.sink_usec
>= usec
)
1204 usec
-= s
->timing_info
.sink_usec
;
1207 } else if (s
->direction
== PA_STREAM_RECORD
) {
1208 /* The last byte written into the server side queue had
1209 * this time value associated */
1210 usec
= pa_bytes_to_usec(s
->timing_info
.write_index
< 0 ? 0 : (uint64_t) s
->timing_info
.write_index
, &s
->sample_spec
);
1213 /* Add transport latency */
1214 usec
+= s
->timing_info
.transport_usec
;
1216 /* Add latency of data in device buffer */
1217 usec
+= s
->timing_info
.source_usec
;
1219 /* If this is a monitor source, we need to correct the
1220 * time by the playback device buffer */
1221 if (s
->timing_info
.sink_usec
>= usec
)
1224 usec
-= s
->timing_info
.sink_usec
;
1228 if (s
->flags
& PA_STREAM_INTERPOLATE_TIMING
) {
1229 s
->ipol_usec
= usec
;
1230 s
->ipol_usec_valid
= 1;
1234 /* Interpolate if requested */
1235 if (s
->flags
& PA_STREAM_INTERPOLATE_TIMING
) {
1237 /* We just add the time that passed since the latency info was
1242 usec
+= pa_timeval_diff(pa_gettimeofday(&now
), &s
->ipol_timestamp
);
1243 s
->ipol_timestamp
= now
;
1247 /* Make sure the time runs monotonically */
1248 if (!(s
->flags
& PA_STREAM_NOT_MONOTONOUS
)) {
1249 if (usec
< s
->previous_time
)
1250 usec
= s
->previous_time
;
1252 s
->previous_time
= usec
;
1261 static pa_usec_t
time_counter_diff(pa_stream
*s
, pa_usec_t a
, pa_usec_t b
, int *negative
) {
1263 assert(s
->ref
>= 1);
1271 if (negative
&& s
->direction
== PA_STREAM_RECORD
) {
1279 int pa_stream_get_latency(pa_stream
*s
, pa_usec_t
*r_usec
, int *negative
) {
1285 assert(s
->ref
>= 1);
1288 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1289 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1290 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1291 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
1292 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
1294 if ((r
= pa_stream_get_time(s
, &t
)) < 0)
1297 if (s
->direction
== PA_STREAM_PLAYBACK
)
1298 cindex
= s
->timing_info
.write_index
;
1300 cindex
= s
->timing_info
.read_index
;
1305 c
= pa_bytes_to_usec(cindex
, &s
->sample_spec
);
1307 if (s
->direction
== PA_STREAM_PLAYBACK
)
1308 *r_usec
= time_counter_diff(s
, c
, t
, negative
);
1310 *r_usec
= time_counter_diff(s
, t
, c
, negative
);
1315 const pa_timing_info
* pa_stream_get_timing_info(pa_stream
*s
) {
1317 assert(s
->ref
>= 1);
1319 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1320 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1321 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->timing_info_valid
, PA_ERR_BADSTATE
);
1323 return &s
->timing_info
;
1326 const pa_sample_spec
* pa_stream_get_sample_spec(pa_stream
*s
) {
1328 assert(s
->ref
>= 1);
1330 return &s
->sample_spec
;
1333 const pa_channel_map
* pa_stream_get_channel_map(pa_stream
*s
) {
1335 assert(s
->ref
>= 1);
1337 return &s
->channel_map
;