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;
96 s
->ipol_usec_valid
= 0;
97 s
->ipol_timestamp
.tv_sec
= 0;
98 s
->ipol_timestamp
.tv_usec
= 0;
100 s
->ipol_requested
= 0;
102 for (i
= 0; i
< PA_MAX_LATENCY_CORRECTIONS
; i
++)
103 s
->latency_corrections
[i
].valid
= 0;
104 s
->idx_latency_correction
= 0;
106 PA_LLIST_PREPEND(pa_stream
, c
->streams
, s
);
108 /* The context and stream will point at each other. We cannot ref count
109 both though since that will create a loop. */
110 pa_context_ref(s
->context
);
115 static void stream_free(pa_stream
*s
) {
116 assert(s
&& s
->context
&& !s
->channel_valid
);
118 PA_LLIST_REMOVE(pa_stream
, s
->context
->streams
, s
);
120 pa_context_unref(s
->context
);
124 s
->mainloop
->time_free(s
->ipol_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
) {
187 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
) && s
->context
) {
188 /* Detach from context */
190 if (s
->channel_valid
)
191 pa_dynarray_put((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, s
->channel
, NULL
);
194 s
->channel_valid
= 0;
196 /* We keep a ref as long as we're connected */
200 if (s
->state_callback
)
201 s
->state_callback(s
, s
->state_userdata
);
206 void pa_command_stream_killed(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
207 pa_context
*c
= userdata
;
212 assert(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
);
218 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
219 !pa_tagstruct_eof(t
)) {
220 pa_context_fail(c
, PA_ERR_PROTOCOL
);
224 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, channel
)))
227 pa_context_set_error(c
, PA_ERR_KILLED
);
228 pa_stream_set_state(s
, PA_STREAM_FAILED
);
234 void pa_command_request(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
236 pa_context
*c
= userdata
;
237 uint32_t bytes
, channel
;
240 assert(command
== PA_COMMAND_REQUEST
);
246 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
247 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
248 !pa_tagstruct_eof(t
)) {
249 pa_context_fail(c
, PA_ERR_PROTOCOL
);
253 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
256 if (s
->state
== PA_STREAM_READY
) {
257 s
->requested_bytes
+= bytes
;
259 if (s
->requested_bytes
> 0 && s
->write_callback
)
260 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
267 void pa_command_overflow_or_underflow(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
269 pa_context
*c
= userdata
;
273 assert(command
== PA_COMMAND_OVERFLOW
|| command
== PA_COMMAND_UNDERFLOW
);
279 if (pa_tagstruct_getu32(t
, &channel
) < 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
) {
290 if (command
== PA_COMMAND_OVERFLOW
) {
291 if (s
->overflow_callback
)
292 s
->overflow_callback(s
, s
->overflow_userdata
);
293 } else if (command
== PA_COMMAND_UNDERFLOW
) {
294 if (s
->underflow_callback
)
295 s
->underflow_callback(s
, s
->underflow_userdata
);
303 static void ipol_callback(pa_mainloop_api
*m
, pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
305 pa_stream
*s
= userdata
;
309 /* pa_log("requesting new ipol data"); */
311 if (s
->state
== PA_STREAM_READY
&& !s
->ipol_requested
) {
314 if ((o
= pa_stream_update_timing_info(s
, NULL
, NULL
))) {
315 pa_operation_unref(o
);
316 s
->ipol_requested
= 1;
320 pa_gettimeofday(&next
);
321 pa_timeval_add(&next
, LATENCY_IPOL_INTERVAL_USEC
);
322 m
->time_restart(e
, &next
);
328 void pa_create_stream_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
329 pa_stream
*s
= userdata
;
334 assert(s
->state
== PA_STREAM_CREATING
);
338 if (command
!= PA_COMMAND_REPLY
) {
339 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
342 pa_stream_set_state(s
, PA_STREAM_FAILED
);
346 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
347 ((s
->direction
!= PA_STREAM_UPLOAD
) && pa_tagstruct_getu32(t
, &s
->device_index
) < 0) ||
348 ((s
->direction
!= PA_STREAM_RECORD
) && pa_tagstruct_getu32(t
, &s
->requested_bytes
) < 0) ||
349 !pa_tagstruct_eof(t
)) {
350 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
354 if (s
->direction
== PA_STREAM_RECORD
) {
355 assert(!s
->record_memblockq
);
357 s
->record_memblockq
= pa_memblockq_new(
359 s
->buffer_attr
.maxlength
,
361 pa_frame_size(&s
->sample_spec
),
365 s
->context
->memblock_stat
);
368 s
->channel_valid
= 1;
369 pa_dynarray_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, s
->channel
, s
);
371 /* We add an extra ref as long as we're connected (i.e. in the dynarray) */
374 if (s
->direction
!= PA_STREAM_UPLOAD
&&
375 s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) {
378 pa_gettimeofday(&tv
);
379 tv
.tv_usec
+= LATENCY_IPOL_INTERVAL_USEC
; /* every 100 ms */
381 assert(!s
->ipol_event
);
382 s
->ipol_event
= s
->mainloop
->time_new(s
->mainloop
, &tv
, &ipol_callback
, s
);
385 pa_stream_set_state(s
, PA_STREAM_READY
);
387 if (s
->requested_bytes
> 0 && s
->ref
> 1 && s
->write_callback
)
388 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
394 static int create_stream(
395 pa_stream_direction_t direction
,
398 const pa_buffer_attr
*attr
,
399 pa_stream_flags_t flags
,
400 const pa_cvolume
*volume
,
401 pa_stream
*sync_stream
) {
409 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_UNCONNECTED
, PA_ERR_BADSTATE
);
410 PA_CHECK_VALIDITY(s
->context
, !(flags
& ~((direction
!= PA_STREAM_UPLOAD
?
411 PA_STREAM_START_CORKED
|
412 PA_STREAM_INTERPOLATE_TIMING
|
413 PA_STREAM_NOT_MONOTONOUS
|
414 PA_STREAM_AUTO_TIMING_UPDATE
: 0))), PA_ERR_INVALID
);
415 PA_CHECK_VALIDITY(s
->context
, direction
== PA_STREAM_PLAYBACK
|| flags
== 0, PA_ERR_INVALID
);
416 PA_CHECK_VALIDITY(s
->context
, !volume
|| volume
->channels
== s
->sample_spec
.channels
, PA_ERR_INVALID
);
417 PA_CHECK_VALIDITY(s
->context
, !sync_stream
|| (direction
== PA_STREAM_PLAYBACK
&& sync_stream
->direction
== PA_STREAM_PLAYBACK
), PA_ERR_INVALID
);
421 s
->direction
= direction
;
425 s
->syncid
= sync_stream
->syncid
;
428 s
->buffer_attr
= *attr
;
431 s
->buffer_attr
.tlength
= pa_bytes_per_second(&s
->sample_spec
)/2;
432 s
->buffer_attr
.maxlength
= (s
->buffer_attr
.tlength
*3)/2;
433 s
->buffer_attr
.minreq
= s
->buffer_attr
.tlength
/100;
434 s
->buffer_attr
.prebuf
= s
->buffer_attr
.tlength
- s
->buffer_attr
.minreq
;
435 s
->buffer_attr
.fragsize
= s
->buffer_attr
.tlength
/100;
439 dev
= s
->direction
== PA_STREAM_PLAYBACK
? s
->context
->conf
->default_sink
: s
->context
->conf
->default_source
;
441 t
= pa_tagstruct_command(
443 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
,
448 PA_TAG_STRING
, s
->name
,
449 PA_TAG_SAMPLE_SPEC
, &s
->sample_spec
,
450 PA_TAG_CHANNEL_MAP
, &s
->channel_map
,
451 PA_TAG_U32
, PA_INVALID_INDEX
,
453 PA_TAG_U32
, s
->buffer_attr
.maxlength
,
454 PA_TAG_BOOLEAN
, !!(flags
& PA_STREAM_START_CORKED
),
457 if (s
->direction
== PA_STREAM_PLAYBACK
) {
462 PA_TAG_U32
, s
->buffer_attr
.tlength
,
463 PA_TAG_U32
, s
->buffer_attr
.prebuf
,
464 PA_TAG_U32
, s
->buffer_attr
.minreq
,
465 PA_TAG_U32
, s
->syncid
,
469 volume
= pa_cvolume_reset(&cv
, s
->sample_spec
.channels
);
471 pa_tagstruct_put_cvolume(t
, volume
);
473 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
475 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
476 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
);
478 pa_stream_set_state(s
, PA_STREAM_CREATING
);
484 int pa_stream_connect_playback(
487 const pa_buffer_attr
*attr
,
488 pa_stream_flags_t flags
,
490 pa_stream
*sync_stream
) {
495 return create_stream(PA_STREAM_PLAYBACK
, s
, dev
, attr
, flags
, volume
, sync_stream
);
498 int pa_stream_connect_record(
501 const pa_buffer_attr
*attr
,
502 pa_stream_flags_t flags
) {
507 return create_stream(PA_STREAM_RECORD
, s
, dev
, attr
, flags
, NULL
, NULL
);
514 void (*free_cb
)(void *p
),
516 pa_seek_mode_t seek
) {
524 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
525 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| s
->direction
== PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
526 PA_CHECK_VALIDITY(s
->context
, seek
<= PA_SEEK_RELATIVE_END
, PA_ERR_INVALID
);
527 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| (seek
== PA_SEEK_RELATIVE
&& offset
== 0), PA_ERR_INVALID
);
533 chunk
.memblock
= pa_memblock_new_user((void*) data
, length
, free_cb
, 1, s
->context
->memblock_stat
);
535 chunk
.memblock
= pa_memblock_new(length
, s
->context
->memblock_stat
);
536 memcpy(chunk
.memblock
->data
, data
, length
);
540 chunk
.length
= length
;
542 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, offset
, seek
, &chunk
);
543 pa_memblock_unref(chunk
.memblock
);
545 if (length
< s
->requested_bytes
)
546 s
->requested_bytes
-= length
;
548 s
->requested_bytes
= 0;
550 if (s
->direction
== PA_STREAM_PLAYBACK
) {
551 /* Update latency request correction */
552 if (s
->latency_corrections
[s
->idx_latency_correction
].valid
) {
554 if (seek
== PA_SEEK_ABSOLUTE
) {
555 s
->latency_corrections
[s
->idx_latency_correction
].corrupt
= 0;
556 s
->latency_corrections
[s
->idx_latency_correction
].absolute
= 1;
557 s
->latency_corrections
[s
->idx_latency_correction
].value
= offset
+ length
;
558 } else if (seek
== PA_SEEK_RELATIVE
) {
559 if (!s
->latency_corrections
[s
->idx_latency_correction
].corrupt
)
560 s
->latency_corrections
[s
->idx_latency_correction
].value
+= offset
+ length
;
562 s
->latency_corrections
[s
->idx_latency_correction
].corrupt
= 1;
565 /* Update the write index in the already available latency data */
566 if (s
->timing_info_valid
) {
568 if (seek
== PA_SEEK_ABSOLUTE
) {
569 s
->timing_info
.write_index_corrupt
= 0;
570 s
->timing_info
.write_index
= offset
+ length
;
571 } else if (seek
== PA_SEEK_RELATIVE
) {
572 if (!s
->timing_info
.write_index_corrupt
)
573 s
->timing_info
.write_index
+= offset
+ length
;
575 s
->timing_info
.write_index_corrupt
= 1;
582 int pa_stream_peek(pa_stream
*s
, const void **data
, size_t *length
) {
588 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
589 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
591 if (!s
->peek_memchunk
.memblock
) {
593 if (pa_memblockq_peek(s
->record_memblockq
, &s
->peek_memchunk
) < 0) {
600 *data
= (const char*) s
->peek_memchunk
.memblock
->data
+ s
->peek_memchunk
.index
;
601 *length
= s
->peek_memchunk
.length
;
605 int pa_stream_drop(pa_stream
*s
) {
609 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
610 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
611 PA_CHECK_VALIDITY(s
->context
, s
->peek_memchunk
.memblock
, PA_ERR_BADSTATE
);
613 pa_memblockq_drop(s
->record_memblockq
, &s
->peek_memchunk
, s
->peek_memchunk
.length
);
615 pa_memblock_unref(s
->peek_memchunk
.memblock
);
616 s
->peek_memchunk
.length
= 0;
617 s
->peek_memchunk
.index
= 0;
618 s
->peek_memchunk
.memblock
= NULL
;
623 size_t pa_stream_writable_size(pa_stream
*s
) {
627 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
628 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
630 return s
->requested_bytes
;
633 size_t pa_stream_readable_size(pa_stream
*s
) {
637 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
638 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
640 return pa_memblockq_get_length(s
->record_memblockq
);
643 pa_operation
* pa_stream_drain(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
651 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
652 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
654 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
656 t
= pa_tagstruct_command(s
->context
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
, &tag
);
657 pa_tagstruct_putu32(t
, s
->channel
);
658 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
659 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
661 return pa_operation_ref(o
);
664 static void stream_get_timing_info_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
665 pa_operation
*o
= userdata
;
666 struct timeval local
, remote
, now
;
674 i
= &o
->stream
->timing_info
;
675 o
->stream
->timing_info_valid
= 0;
676 i
->write_index_corrupt
= 0;
678 if (command
!= PA_COMMAND_REPLY
) {
679 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
682 } else if (pa_tagstruct_get_usec(t
, &i
->buffer_usec
) < 0 ||
683 pa_tagstruct_get_usec(t
, &i
->sink_usec
) < 0 ||
684 pa_tagstruct_get_usec(t
, &i
->source_usec
) < 0 ||
685 pa_tagstruct_get_boolean(t
, &i
->playing
) < 0 ||
686 pa_tagstruct_get_timeval(t
, &local
) < 0 ||
687 pa_tagstruct_get_timeval(t
, &remote
) < 0 ||
688 pa_tagstruct_gets64(t
, &i
->write_index
) < 0 ||
689 pa_tagstruct_gets64(t
, &i
->read_index
) < 0 ||
690 !pa_tagstruct_eof(t
)) {
691 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
695 pa_gettimeofday(&now
);
697 if (pa_timeval_cmp(&local
, &remote
) <= 0 && pa_timeval_cmp(&remote
, &now
) <= 0) {
698 /* local and remote seem to have synchronized clocks */
700 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
)
701 i
->transport_usec
= pa_timeval_diff(&remote
, &local
);
703 i
->transport_usec
= pa_timeval_diff(&now
, &remote
);
705 i
->synchronized_clocks
= 1;
706 i
->timestamp
= remote
;
708 /* clocks are not synchronized, let's estimate latency then */
709 i
->transport_usec
= pa_timeval_diff(&now
, &local
)/2;
710 i
->synchronized_clocks
= 0;
711 i
->timestamp
= local
;
712 pa_timeval_add(&i
->timestamp
, i
->transport_usec
);
715 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
716 /* Write index correction */
721 /* Go through the saved correction values and add up the total correction.*/
723 for (n
= 0, j
= o
->stream
->idx_latency_correction
;
724 n
< PA_MAX_LATENCY_CORRECTIONS
;
725 n
++, j
= (j
+ 1) % PA_MAX_LATENCY_CORRECTIONS
) {
727 /* Step over invalid data or out-of-date data */
728 if (!o
->stream
->latency_corrections
[j
].valid
||
729 o
->stream
->latency_corrections
[j
].tag
< ctag
)
732 /* Make sure that everything is in order */
733 ctag
= o
->stream
->latency_corrections
[j
].tag
+1;
735 /* Now fix the write index */
736 if (o
->stream
->latency_corrections
[j
].corrupt
) {
737 /* A corrupting seek was made */
739 i
->write_index_corrupt
= 1;
740 } else if (o
->stream
->latency_corrections
[j
].absolute
) {
741 /* An absolute seek was made */
742 i
->write_index
= o
->stream
->latency_corrections
[j
].value
;
743 i
->write_index_corrupt
= 0;
744 } else if (!i
->write_index_corrupt
) {
745 /* A relative seek was made */
746 i
->write_index
+= o
->stream
->latency_corrections
[j
].value
;
751 o
->stream
->timing_info_valid
= 1;
753 o
->stream
->ipol_timestamp
= now
;
754 o
->stream
->ipol_usec_valid
= 0;
757 /* Clear old correction entries */
758 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
761 for (n
= 0; n
< PA_MAX_LATENCY_CORRECTIONS
; n
++) {
762 if (!o
->stream
->latency_corrections
[n
].valid
)
765 if (o
->stream
->latency_corrections
[n
].tag
<= tag
)
766 o
->stream
->latency_corrections
[n
].valid
= 0;
771 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
772 cb(o
->stream
, o
->stream
->timing_info_valid
, o
->userdata
);
777 pa_operation_done(o
);
778 pa_operation_unref(o
);
781 pa_operation
* pa_stream_update_timing_info(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
791 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
792 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
794 if (s
->direction
== PA_STREAM_PLAYBACK
) {
795 /* Find a place to store the write_index correction data for this entry */
796 cidx
= (s
->idx_latency_correction
+ 1) % PA_MAX_LATENCY_CORRECTIONS
;
798 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
799 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !s
->latency_corrections
[cidx
].valid
, PA_ERR_INTERNAL
);
802 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
804 t
= pa_tagstruct_command(
806 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_GET_PLAYBACK_LATENCY
: PA_COMMAND_GET_RECORD_LATENCY
,
808 pa_tagstruct_putu32(t
, s
->channel
);
809 pa_tagstruct_put_timeval(t
, pa_gettimeofday(&now
));
811 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
812 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, stream_get_timing_info_callback
, o
);
814 if (s
->direction
== PA_STREAM_PLAYBACK
) {
815 /* Fill in initial correction data */
816 o
->stream
->idx_latency_correction
= cidx
;
817 o
->stream
->latency_corrections
[cidx
].valid
= 1;
818 o
->stream
->latency_corrections
[cidx
].tag
= tag
;
819 o
->stream
->latency_corrections
[cidx
].absolute
= 0;
820 o
->stream
->latency_corrections
[cidx
].value
= 0;
821 o
->stream
->latency_corrections
[cidx
].corrupt
= 0;
824 return pa_operation_ref(o
);
827 void pa_stream_disconnect_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
828 pa_stream
*s
= userdata
;
836 if (command
!= PA_COMMAND_REPLY
) {
837 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
840 pa_stream_set_state(s
, PA_STREAM_FAILED
);
842 } else if (!pa_tagstruct_eof(t
)) {
843 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
847 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
853 int pa_stream_disconnect(pa_stream
*s
) {
860 PA_CHECK_VALIDITY(s
->context
, s
->channel_valid
, PA_ERR_BADSTATE
);
861 PA_CHECK_VALIDITY(s
->context
, s
->context
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
865 t
= pa_tagstruct_command(
867 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
868 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
),
870 pa_tagstruct_putu32(t
, s
->channel
);
871 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
872 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
);
878 void pa_stream_set_read_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
882 s
->read_callback
= cb
;
883 s
->read_userdata
= userdata
;
886 void pa_stream_set_write_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
890 s
->write_callback
= cb
;
891 s
->write_userdata
= userdata
;
894 void pa_stream_set_state_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
898 s
->state_callback
= cb
;
899 s
->state_userdata
= userdata
;
902 void pa_stream_set_overflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
906 s
->overflow_callback
= cb
;
907 s
->overflow_userdata
= userdata
;
910 void pa_stream_set_underflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
914 s
->underflow_callback
= cb
;
915 s
->underflow_userdata
= userdata
;
918 void pa_stream_simple_ack_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
919 pa_operation
*o
= userdata
;
927 if (command
!= PA_COMMAND_REPLY
) {
928 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
932 } else if (!pa_tagstruct_eof(t
)) {
933 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
938 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
939 cb(o
->stream
, success
, o
->userdata
);
943 pa_operation_done(o
);
944 pa_operation_unref(o
);
947 pa_operation
* pa_stream_cork(pa_stream
*s
, int b
, pa_stream_success_cb_t cb
, void *userdata
) {
956 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
957 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
959 if (s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) {
960 if (!s
->corked
&& b
) {
961 /* Refresh the interpolated data just befor pausing */
962 pa_stream_get_time(s
, NULL
);
963 } else if (s
->corked
&& !b
)
965 pa_gettimeofday(&s
->ipol_timestamp
);
970 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
972 t
= pa_tagstruct_command(
974 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CORK_PLAYBACK_STREAM
: PA_COMMAND_CORK_RECORD_STREAM
,
976 pa_tagstruct_putu32(t
, s
->channel
);
977 pa_tagstruct_put_boolean(t
, !!b
);
978 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
979 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
981 if ((lo
= pa_stream_update_timing_info(s
, NULL
, NULL
)))
982 pa_operation_unref(lo
);
984 return pa_operation_ref(o
);
987 static pa_operation
* stream_send_simple_command(pa_stream
*s
, uint32_t command
, pa_stream_success_cb_t cb
, void *userdata
) {
995 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
997 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
999 t
= pa_tagstruct_command(s
->context
, command
, &tag
);
1000 pa_tagstruct_putu32(t
, s
->channel
);
1001 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1002 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
1004 return pa_operation_ref(o
);
1007 pa_operation
* pa_stream_flush(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1010 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1012 if ((o
= stream_send_simple_command(s
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_FLUSH_PLAYBACK_STREAM
: PA_COMMAND_FLUSH_RECORD_STREAM
, cb
, userdata
))) {
1015 if ((lo
= pa_stream_update_timing_info(s
, NULL
, NULL
)))
1016 pa_operation_unref(lo
);
1022 pa_operation
* pa_stream_prebuf(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1025 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1027 if ((o
= stream_send_simple_command(s
, PA_COMMAND_PREBUF_PLAYBACK_STREAM
, cb
, userdata
))) {
1030 if ((lo
= pa_stream_update_timing_info(s
, NULL
, NULL
)))
1031 pa_operation_unref(lo
);
1037 pa_operation
* pa_stream_trigger(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1040 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1042 if ((o
= stream_send_simple_command(s
, PA_COMMAND_TRIGGER_PLAYBACK_STREAM
, cb
, userdata
))) {
1045 if ((lo
= pa_stream_update_timing_info(s
, NULL
, NULL
)))
1046 pa_operation_unref(lo
);
1052 pa_operation
* pa_stream_set_name(pa_stream
*s
, const char *name
, pa_stream_success_cb_t cb
, void *userdata
) {
1058 assert(s
->ref
>= 1);
1061 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1062 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1064 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1066 t
= pa_tagstruct_command(
1068 s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_NAME
: PA_COMMAND_SET_PLAYBACK_STREAM_NAME
,
1070 pa_tagstruct_putu32(t
, s
->channel
);
1071 pa_tagstruct_puts(t
, name
);
1072 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1073 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
1075 return pa_operation_ref(o
);
1078 int pa_stream_get_time(pa_stream
*s
, pa_usec_t
*r_usec
) {
1082 assert(s
->ref
>= 1);
1084 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1085 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1086 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1088 if (s
->flags
& PA_STREAM_INTERPOLATE_TIMING
&& s
->ipol_usec_valid
)
1089 usec
= s
->ipol_usec
;
1091 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1092 /* The last byte that was written into the output device
1093 * had this time value associated */
1094 usec
= pa_bytes_to_usec(s
->timing_info
.read_index
< 0 ? 0 : (uint64_t) s
->timing_info
.read_index
, &s
->sample_spec
);
1096 /* Because the latency info took a little time to come
1097 * to us, we assume that the real output time is actually
1099 usec
+= s
->timing_info
.transport_usec
;
1101 /* However, the output device usually maintains a buffer
1102 too, hence the real sample currently played is a little
1104 if (s
->timing_info
.sink_usec
>= usec
)
1107 usec
-= s
->timing_info
.sink_usec
;
1109 } else if (s
->direction
== PA_STREAM_RECORD
) {
1110 /* The last byte written into the server side queue had
1111 * this time value associated */
1112 usec
= pa_bytes_to_usec(s
->timing_info
.write_index
< 0 ? 0 : (uint64_t) s
->timing_info
.write_index
, &s
->sample_spec
);
1114 /* Add transport latency */
1115 usec
+= s
->timing_info
.transport_usec
;
1117 /* Add latency of data in device buffer */
1118 usec
+= s
->timing_info
.source_usec
;
1120 /* If this is a monitor source, we need to correct the
1121 * time by the playback device buffer */
1122 if (s
->timing_info
.sink_usec
>= usec
)
1125 usec
-= s
->timing_info
.sink_usec
;
1128 if (s
->flags
& PA_STREAM_INTERPOLATE_TIMING
) {
1129 s
->ipol_usec_valid
= 1;
1130 s
->ipol_usec
= usec
;
1134 /* Interpolate if requested */
1135 if (s
->flags
& PA_STREAM_INTERPOLATE_TIMING
) {
1137 /* We just add the time that passed since the latency info was
1142 usec
+= pa_timeval_diff(pa_gettimeofday(&now
), &s
->ipol_timestamp
);
1143 s
->ipol_timestamp
= now
;
1147 /* Make sure the time runs monotonically */
1148 if (!(s
->flags
& PA_STREAM_NOT_MONOTONOUS
)) {
1149 if (usec
< s
->previous_time
)
1150 usec
= s
->previous_time
;
1152 s
->previous_time
= usec
;
1161 static pa_usec_t
time_counter_diff(pa_stream
*s
, pa_usec_t a
, pa_usec_t b
, int *negative
) {
1163 assert(s
->ref
>= 1);
1171 if (negative
&& s
->direction
== PA_STREAM_RECORD
) {
1179 int pa_stream_get_latency(pa_stream
*s
, pa_usec_t
*r_usec
, int *negative
) {
1185 assert(s
->ref
>= 1);
1188 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1189 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1190 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1191 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
1193 if ((r
= pa_stream_get_time(s
, &t
)) < 0)
1196 if (s
->direction
== PA_STREAM_PLAYBACK
)
1197 cindex
= s
->timing_info
.write_index
;
1199 cindex
= s
->timing_info
.read_index
;
1204 c
= pa_bytes_to_usec(cindex
, &s
->sample_spec
);
1206 if (s
->direction
== PA_STREAM_PLAYBACK
)
1207 *r_usec
= time_counter_diff(s
, c
, t
, negative
);
1209 *r_usec
= time_counter_diff(s
, t
, c
, negative
);
1214 const pa_timing_info
* pa_stream_get_timing_info(pa_stream
*s
) {
1216 assert(s
->ref
>= 1);
1218 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1219 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1220 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->timing_info_valid
, PA_ERR_BADSTATE
);
1222 return &s
->timing_info
;
1225 const pa_sample_spec
* pa_stream_get_sample_spec(pa_stream
*s
) {
1227 assert(s
->ref
>= 1);
1229 return &s
->sample_spec
;
1232 const pa_channel_map
* pa_stream_get_channel_map(pa_stream
*s
) {
1234 assert(s
->ref
>= 1);
1236 return &s
->channel_map
;