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)
41 #define COUNTER_HASHMAP_MAXSIZE (5)
43 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
);
72 s
->channel_map
= *map
;
74 pa_channel_map_init_auto(&s
->channel_map
, ss
->channels
);
78 s
->syncid
= c
->csyncid
++;
79 s
->device_index
= PA_INVALID_INDEX
;
80 s
->requested_bytes
= 0;
81 s
->state
= PA_STREAM_UNCONNECTED
;
82 memset(&s
->buffer_attr
, 0, sizeof(s
->buffer_attr
));
84 s
->peek_memchunk
.index
= 0;
85 s
->peek_memchunk
.length
= 0;
86 s
->peek_memchunk
.memblock
= NULL
;
88 s
->record_memblockq
= NULL
;
90 s
->counter_hashmap
= pa_hashmap_new(NULL
, NULL
);
94 s
->previous_ipol_time
= 0;
100 memset(&s
->ipol_timestamp
, 0, sizeof(s
->ipol_timestamp
));
101 s
->ipol_event
= NULL
;
102 s
->ipol_requested
= 0;
104 PA_LLIST_PREPEND(pa_stream
, c
->streams
, s
);
106 /* The context and stream will point at each other. We cannot ref count
107 both though since that will create a loop. */
108 pa_context_ref(s
->context
);
113 static void hashmap_free_func(void *p
, void *userdata
) {
117 static void stream_free(pa_stream
*s
) {
118 assert(s
&& s
->context
&& !s
->channel_valid
);
120 PA_LLIST_REMOVE(pa_stream
, s
->context
->streams
, s
);
122 pa_context_unref(s
->context
);
126 s
->mainloop
->time_free(s
->ipol_event
);
129 if (s
->peek_memchunk
.memblock
)
130 pa_memblock_unref(s
->peek_memchunk
.memblock
);
132 if (s
->record_memblockq
)
133 pa_memblockq_free(s
->record_memblockq
);
135 if (s
->counter_hashmap
)
136 pa_hashmap_free(s
->counter_hashmap
, hashmap_free_func
, NULL
);
142 void pa_stream_unref(pa_stream
*s
) {
150 pa_stream
* pa_stream_ref(pa_stream
*s
) {
158 pa_stream_state_t
pa_stream_get_state(pa_stream
*s
) {
165 pa_context
* pa_stream_get_context(pa_stream
*s
) {
172 uint32_t pa_stream_get_index(pa_stream
*s
) {
176 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
178 return s
->device_index
;
181 void pa_stream_set_state(pa_stream
*s
, pa_stream_state_t st
) {
192 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
) && s
->context
) {
193 /* Detach from context */
195 if (s
->channel_valid
)
196 pa_dynarray_put((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, s
->channel
, NULL
);
199 s
->channel_valid
= 0;
201 /* We keep a ref as long as we're connected */
205 if (s
->state_callback
)
206 s
->state_callback(s
, s
->state_userdata
);
211 void pa_command_stream_killed(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
212 pa_context
*c
= userdata
;
217 assert(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
);
223 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
224 !pa_tagstruct_eof(t
)) {
225 pa_context_fail(c
, PA_ERR_PROTOCOL
);
229 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, channel
)))
232 pa_context_set_error(c
, PA_ERR_KILLED
);
233 pa_stream_set_state(s
, PA_STREAM_FAILED
);
239 void pa_command_request(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
241 pa_context
*c
= userdata
;
242 uint32_t bytes
, channel
;
245 assert(command
== PA_COMMAND_REQUEST
);
251 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
252 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
253 !pa_tagstruct_eof(t
)) {
254 pa_context_fail(c
, PA_ERR_PROTOCOL
);
258 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
261 if (s
->state
== PA_STREAM_READY
) {
262 s
->requested_bytes
+= bytes
;
264 if (s
->requested_bytes
> 0 && s
->write_callback
)
265 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
272 void pa_command_overflow_or_underflow(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
274 pa_context
*c
= userdata
;
278 assert(command
== PA_COMMAND_OVERFLOW
|| command
== PA_COMMAND_UNDERFLOW
);
284 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
285 !pa_tagstruct_eof(t
)) {
286 pa_context_fail(c
, PA_ERR_PROTOCOL
);
290 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
293 if (s
->state
== PA_STREAM_READY
) {
295 if (command
== PA_COMMAND_OVERFLOW
) {
296 if (s
->overflow_callback
)
297 s
->overflow_callback(s
, s
->overflow_userdata
);
298 } else if (command
== PA_COMMAND_UNDERFLOW
) {
299 if (s
->underflow_callback
)
300 s
->underflow_callback(s
, s
->underflow_userdata
);
308 static void ipol_callback(pa_mainloop_api
*m
, pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
310 pa_stream
*s
= userdata
;
314 /* pa_log("requesting new ipol data"); */
316 if (s
->state
== PA_STREAM_READY
&& !s
->ipol_requested
) {
317 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
318 s
->ipol_requested
= 1;
321 pa_gettimeofday(&next
);
322 pa_timeval_add(&next
, LATENCY_IPOL_INTERVAL_USEC
);
323 m
->time_restart(e
, &next
);
329 void pa_create_stream_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
330 pa_stream
*s
= userdata
;
335 assert(s
->state
== PA_STREAM_CREATING
);
339 if (command
!= PA_COMMAND_REPLY
) {
340 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
343 pa_stream_set_state(s
, PA_STREAM_FAILED
);
347 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
348 ((s
->direction
!= PA_STREAM_UPLOAD
) && pa_tagstruct_getu32(t
, &s
->device_index
) < 0) ||
349 ((s
->direction
!= PA_STREAM_RECORD
) && pa_tagstruct_getu32(t
, &s
->requested_bytes
) < 0) ||
350 !pa_tagstruct_eof(t
)) {
351 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
355 if (s
->direction
== PA_STREAM_RECORD
) {
356 assert(!s
->record_memblockq
);
358 s
->record_memblockq
= pa_memblockq_new(
360 s
->buffer_attr
.maxlength
,
362 pa_frame_size(&s
->sample_spec
),
366 s
->context
->memblock_stat
);
369 s
->channel_valid
= 1;
370 pa_dynarray_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, s
->channel
, s
);
372 /* We add an extra ref as long as we're connected (i.e. in the dynarray) */
375 if (s
->interpolate
) {
377 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
379 pa_gettimeofday(&tv
);
380 tv
.tv_usec
+= LATENCY_IPOL_INTERVAL_USEC
; /* every 100 ms */
382 assert(!s
->ipol_event
);
383 s
->ipol_event
= s
->mainloop
->time_new(s
->mainloop
, &tv
, &ipol_callback
, s
);
386 pa_stream_set_state(s
, PA_STREAM_READY
);
388 if (s
->requested_bytes
> 0 && s
->ref
> 1 && s
->write_callback
)
389 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
395 static int create_stream(
396 pa_stream_direction_t direction
,
399 const pa_buffer_attr
*attr
,
400 pa_stream_flags_t flags
,
401 const pa_cvolume
*volume
,
402 pa_stream
*sync_stream
) {
410 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_UNCONNECTED
, PA_ERR_BADSTATE
);
411 PA_CHECK_VALIDITY(s
->context
, !(flags
& ~(PA_STREAM_START_CORKED
|PA_STREAM_INTERPOLATE_LATENCY
)), PA_ERR_INVALID
);
412 PA_CHECK_VALIDITY(s
->context
, direction
== PA_STREAM_PLAYBACK
|| flags
== 0, PA_ERR_INVALID
);
413 PA_CHECK_VALIDITY(s
->context
, !volume
|| volume
->channels
== s
->sample_spec
.channels
, PA_ERR_INVALID
);
414 PA_CHECK_VALIDITY(s
->context
, !sync_stream
|| (direction
== PA_STREAM_PLAYBACK
&& sync_stream
->direction
== PA_STREAM_PLAYBACK
), PA_ERR_INVALID
);
418 s
->direction
= direction
;
421 s
->syncid
= sync_stream
->syncid
;
423 s
->interpolate
= !!(flags
& PA_STREAM_INTERPOLATE_LATENCY
);
424 pa_stream_trash_ipol(s
);
427 s
->buffer_attr
= *attr
;
430 s
->buffer_attr
.tlength
= pa_bytes_per_second(&s
->sample_spec
)/2;
431 s
->buffer_attr
.maxlength
= (s
->buffer_attr
.tlength
*3)/2;
432 s
->buffer_attr
.minreq
= s
->buffer_attr
.tlength
/100;
433 s
->buffer_attr
.prebuf
= s
->buffer_attr
.tlength
- s
->buffer_attr
.minreq
;
434 s
->buffer_attr
.fragsize
= s
->buffer_attr
.tlength
/100;
438 dev
= s
->direction
== PA_STREAM_PLAYBACK
? s
->context
->conf
->default_sink
: s
->context
->conf
->default_source
;
440 t
= pa_tagstruct_command(
442 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
,
447 PA_TAG_STRING
, s
->name
,
448 PA_TAG_SAMPLE_SPEC
, &s
->sample_spec
,
449 PA_TAG_CHANNEL_MAP
, &s
->channel_map
,
450 PA_TAG_U32
, PA_INVALID_INDEX
,
452 PA_TAG_U32
, s
->buffer_attr
.maxlength
,
453 PA_TAG_BOOLEAN
, !!(flags
& PA_STREAM_START_CORKED
),
456 if (s
->direction
== PA_STREAM_PLAYBACK
) {
461 PA_TAG_U32
, s
->buffer_attr
.tlength
,
462 PA_TAG_U32
, s
->buffer_attr
.prebuf
,
463 PA_TAG_U32
, s
->buffer_attr
.minreq
,
464 PA_TAG_U32
, s
->syncid
,
468 volume
= pa_cvolume_reset(&cv
, s
->sample_spec
.channels
);
470 pa_tagstruct_put_cvolume(t
, volume
);
472 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
474 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
475 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
);
477 pa_stream_set_state(s
, PA_STREAM_CREATING
);
483 int pa_stream_connect_playback(
486 const pa_buffer_attr
*attr
,
487 pa_stream_flags_t flags
,
489 pa_stream
*sync_stream
) {
494 return create_stream(PA_STREAM_PLAYBACK
, s
, dev
, attr
, flags
, volume
, sync_stream
);
497 int pa_stream_connect_record(
500 const pa_buffer_attr
*attr
,
501 pa_stream_flags_t flags
) {
506 return create_stream(PA_STREAM_RECORD
, s
, dev
, attr
, flags
, NULL
, NULL
);
513 void (*free_cb
)(void *p
),
515 pa_seek_mode_t seek
) {
523 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
524 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| s
->direction
== PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
525 PA_CHECK_VALIDITY(s
->context
, seek
<= PA_SEEK_RELATIVE_END
, PA_ERR_INVALID
);
526 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| (seek
== PA_SEEK_RELATIVE
&& offset
== 0), PA_ERR_INVALID
);
532 chunk
.memblock
= pa_memblock_new_user((void*) data
, length
, free_cb
, 1, s
->context
->memblock_stat
);
534 chunk
.memblock
= pa_memblock_new(length
, s
->context
->memblock_stat
);
535 memcpy(chunk
.memblock
->data
, data
, length
);
539 chunk
.length
= length
;
541 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, offset
, seek
, &chunk
);
542 pa_memblock_unref(chunk
.memblock
);
544 if (length
< s
->requested_bytes
)
545 s
->requested_bytes
-= length
;
547 s
->requested_bytes
= 0;
549 s
->counter
+= length
;
553 int pa_stream_peek(pa_stream
*s
, const void **data
, size_t *length
) {
559 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
560 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
562 if (!s
->peek_memchunk
.memblock
) {
564 if (pa_memblockq_peek(s
->record_memblockq
, &s
->peek_memchunk
) < 0) {
571 *data
= (const char*) s
->peek_memchunk
.memblock
->data
+ s
->peek_memchunk
.index
;
572 *length
= s
->peek_memchunk
.length
;
576 int pa_stream_drop(pa_stream
*s
) {
580 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
581 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
582 PA_CHECK_VALIDITY(s
->context
, s
->peek_memchunk
.memblock
, PA_ERR_BADSTATE
);
584 pa_memblockq_drop(s
->record_memblockq
, &s
->peek_memchunk
, s
->peek_memchunk
.length
);
586 pa_memblock_unref(s
->peek_memchunk
.memblock
);
587 s
->peek_memchunk
.length
= 0;
588 s
->peek_memchunk
.index
= 0;
589 s
->peek_memchunk
.memblock
= NULL
;
591 s
->counter
+= s
->peek_memchunk
.length
;
595 size_t pa_stream_writable_size(pa_stream
*s
) {
599 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
600 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
, (size_t) -1);
602 return s
->requested_bytes
;
605 size_t pa_stream_readable_size(pa_stream
*s
) {
609 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
610 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
612 return pa_memblockq_get_length(s
->record_memblockq
);
615 pa_operation
* pa_stream_drain(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
623 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
624 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
626 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
628 t
= pa_tagstruct_command(s
->context
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
, &tag
);
629 pa_tagstruct_putu32(t
, s
->channel
);
630 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
631 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
633 return pa_operation_ref(o
);
636 static void stream_get_latency_info_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
637 pa_operation
*o
= userdata
;
638 pa_latency_info i
, *p
= NULL
;
639 struct timeval local
, remote
, now
;
646 i
.counter
= *(uint64_t*)pa_hashmap_get(o
->stream
->counter_hashmap
, (void*)(unsigned long)tag
);
647 pa_xfree(pa_hashmap_remove(o
->stream
->counter_hashmap
, (void*)(unsigned long)tag
));
649 if (command
!= PA_COMMAND_REPLY
) {
650 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
653 } else if (pa_tagstruct_get_usec(t
, &i
.buffer_usec
) < 0 ||
654 pa_tagstruct_get_usec(t
, &i
.sink_usec
) < 0 ||
655 pa_tagstruct_get_usec(t
, &i
.source_usec
) < 0 ||
656 pa_tagstruct_get_boolean(t
, &i
.playing
) < 0 ||
657 pa_tagstruct_getu32(t
, &i
.queue_length
) < 0 ||
658 pa_tagstruct_get_timeval(t
, &local
) < 0 ||
659 pa_tagstruct_get_timeval(t
, &remote
) < 0 ||
660 pa_tagstruct_gets64(t
, &i
.write_index
) < 0 ||
661 pa_tagstruct_gets64(t
, &i
.read_index
) < 0 ||
662 !pa_tagstruct_eof(t
)) {
663 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
666 pa_gettimeofday(&now
);
668 if (pa_timeval_cmp(&local
, &remote
) <= 0 && pa_timeval_cmp(&remote
, &now
) <= 0) {
669 /* local and remote seem to have synchronized clocks */
671 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
)
672 i
.transport_usec
= pa_timeval_diff(&remote
, &local
);
674 i
.transport_usec
= pa_timeval_diff(&now
, &remote
);
676 i
.synchronized_clocks
= 1;
677 i
.timestamp
= remote
;
679 /* clocks are not synchronized, let's estimate latency then */
680 i
.transport_usec
= pa_timeval_diff(&now
, &local
)/2;
681 i
.synchronized_clocks
= 0;
683 pa_timeval_add(&i
.timestamp
, i
.transport_usec
);
686 if (o
->stream
->interpolate
) {
687 /* pa_log("new interpol data"); */
688 o
->stream
->ipol_timestamp
= i
.timestamp
;
689 o
->stream
->ipol_usec
= pa_stream_get_time(o
->stream
, &i
);
690 o
->stream
->ipol_requested
= 0;
697 pa_stream_get_latency_info_cb_t cb
= (pa_stream_get_latency_info_cb_t
) o
->callback
;
698 cb(o
->stream
, p
, o
->userdata
);
702 pa_operation_done(o
);
703 pa_operation_unref(o
);
706 pa_operation
* pa_stream_get_latency_info(pa_stream
*s
, pa_stream_get_latency_info_cb_t cb
, void *userdata
) {
716 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
717 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
718 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, pa_hashmap_size(s
->counter_hashmap
) < COUNTER_HASHMAP_MAXSIZE
, PA_ERR_INTERNAL
);
720 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
722 t
= pa_tagstruct_command(
724 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_GET_PLAYBACK_LATENCY
: PA_COMMAND_GET_RECORD_LATENCY
,
726 pa_tagstruct_putu32(t
, s
->channel
);
728 pa_gettimeofday(&now
);
729 pa_tagstruct_put_timeval(t
, &now
);
731 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
732 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, stream_get_latency_info_callback
, o
);
734 counter
= pa_xmalloc(sizeof(uint64_t));
735 *counter
= s
->counter
;
736 pa_hashmap_put(s
->counter_hashmap
, (void*)(unsigned long)tag
, counter
);
738 return pa_operation_ref(o
);
741 void pa_stream_disconnect_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
742 pa_stream
*s
= userdata
;
750 if (command
!= PA_COMMAND_REPLY
) {
751 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
754 pa_stream_set_state(s
, PA_STREAM_FAILED
);
756 } else if (!pa_tagstruct_eof(t
)) {
757 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
761 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
767 int pa_stream_disconnect(pa_stream
*s
) {
774 PA_CHECK_VALIDITY(s
->context
, s
->channel_valid
, PA_ERR_BADSTATE
);
775 PA_CHECK_VALIDITY(s
->context
, s
->context
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
779 t
= pa_tagstruct_command(
781 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
782 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
),
784 pa_tagstruct_putu32(t
, s
->channel
);
785 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
786 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
);
792 void pa_stream_set_read_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
796 s
->read_callback
= cb
;
797 s
->read_userdata
= userdata
;
800 void pa_stream_set_write_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
804 s
->write_callback
= cb
;
805 s
->write_userdata
= userdata
;
808 void pa_stream_set_state_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
812 s
->state_callback
= cb
;
813 s
->state_userdata
= userdata
;
816 void pa_stream_set_overflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
820 s
->overflow_callback
= cb
;
821 s
->overflow_userdata
= userdata
;
824 void pa_stream_set_underflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
828 s
->underflow_callback
= cb
;
829 s
->underflow_userdata
= userdata
;
832 void pa_stream_simple_ack_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
833 pa_operation
*o
= userdata
;
841 if (command
!= PA_COMMAND_REPLY
) {
842 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
846 } else if (!pa_tagstruct_eof(t
)) {
847 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
852 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
853 cb(o
->stream
, success
, o
->userdata
);
857 pa_operation_done(o
);
858 pa_operation_unref(o
);
861 pa_operation
* pa_stream_cork(pa_stream
*s
, int b
, pa_stream_success_cb_t cb
, void *userdata
) {
869 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
870 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
872 if (s
->interpolate
) {
875 s
->ipol_usec
= pa_stream_get_interpolated_time(s
);
876 else if (s
->corked
&& !b
)
878 pa_gettimeofday(&s
->ipol_timestamp
);
883 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
885 t
= pa_tagstruct_command(
887 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CORK_PLAYBACK_STREAM
: PA_COMMAND_CORK_RECORD_STREAM
,
889 pa_tagstruct_putu32(t
, s
->channel
);
890 pa_tagstruct_put_boolean(t
, !!b
);
891 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
892 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
894 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
896 return pa_operation_ref(o
);
899 static pa_operation
* stream_send_simple_command(pa_stream
*s
, uint32_t command
, pa_stream_success_cb_t cb
, void *userdata
) {
907 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
909 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
911 t
= pa_tagstruct_command(s
->context
, command
, &tag
);
912 pa_tagstruct_putu32(t
, s
->channel
);
913 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
914 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
916 return pa_operation_ref(o
);
919 pa_operation
* pa_stream_flush(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
922 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
924 if ((o
= stream_send_simple_command(s
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_FLUSH_PLAYBACK_STREAM
: PA_COMMAND_FLUSH_RECORD_STREAM
, cb
, userdata
)))
925 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
930 pa_operation
* pa_stream_prebuf(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
933 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
935 if ((o
= stream_send_simple_command(s
, PA_COMMAND_PREBUF_PLAYBACK_STREAM
, cb
, userdata
)))
936 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
941 pa_operation
* pa_stream_trigger(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
944 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
946 if ((o
= stream_send_simple_command(s
, PA_COMMAND_TRIGGER_PLAYBACK_STREAM
, cb
, userdata
)))
947 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
952 pa_operation
* pa_stream_set_name(pa_stream
*s
, const char *name
, pa_stream_success_cb_t cb
, void *userdata
) {
961 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
962 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
964 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
966 t
= pa_tagstruct_command(
968 s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_NAME
: PA_COMMAND_SET_PLAYBACK_STREAM_NAME
,
970 pa_tagstruct_putu32(t
, s
->channel
);
971 pa_tagstruct_puts(t
, name
);
972 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
973 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
975 return pa_operation_ref(o
);
978 uint64_t pa_stream_get_counter(pa_stream
*s
) {
982 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (uint64_t) -1);
987 pa_usec_t
pa_stream_get_time(pa_stream
*s
, const pa_latency_info
*i
) {
993 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (pa_usec_t
) -1);
994 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
, (pa_usec_t
) -1);
996 usec
= pa_bytes_to_usec(i
->counter
, &s
->sample_spec
);
999 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1000 pa_usec_t latency
= i
->transport_usec
+ i
->buffer_usec
+ i
->sink_usec
;
1006 } else if (s
->direction
== PA_STREAM_RECORD
) {
1007 usec
+= i
->source_usec
+ i
->buffer_usec
+ i
->transport_usec
;
1009 if (usec
> i
->sink_usec
)
1010 usec
-= i
->sink_usec
;
1016 if (usec
< s
->previous_time
)
1017 usec
= s
->previous_time
;
1019 s
->previous_time
= usec
;
1024 static pa_usec_t
time_counter_diff(pa_stream
*s
, pa_usec_t t
, pa_usec_t c
, int *negative
) {
1026 assert(s
->ref
>= 1);
1032 if (s
->direction
== PA_STREAM_RECORD
) {
1043 pa_usec_t
pa_stream_get_latency(pa_stream
*s
, const pa_latency_info
*i
, int *negative
) {
1047 assert(s
->ref
>= 1);
1050 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (pa_usec_t
) -1);
1051 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
, (pa_usec_t
) -1);
1053 t
= pa_stream_get_time(s
, i
);
1054 c
= pa_bytes_to_usec(s
->counter
, &s
->sample_spec
);
1056 return time_counter_diff(s
, t
, c
, negative
);
1059 const pa_sample_spec
* pa_stream_get_sample_spec(pa_stream
*s
) {
1061 assert(s
->ref
>= 1);
1063 return &s
->sample_spec
;
1066 const pa_channel_map
* pa_stream_get_channel_map(pa_stream
*s
) {
1068 assert(s
->ref
>= 1);
1070 return &s
->channel_map
;
1073 void pa_stream_trash_ipol(pa_stream
*s
) {
1075 assert(s
->ref
>= 1);
1077 if (!s
->interpolate
)
1080 memset(&s
->ipol_timestamp
, 0, sizeof(s
->ipol_timestamp
));
1084 pa_usec_t
pa_stream_get_interpolated_time(pa_stream
*s
) {
1088 assert(s
->ref
>= 1);
1090 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (pa_usec_t
) -1);
1091 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
, (pa_usec_t
) -1);
1092 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->interpolate
, PA_ERR_BADSTATE
, (pa_usec_t
) -1);
1095 usec
= s
->ipol_usec
;
1097 if (s
->ipol_timestamp
.tv_sec
== 0)
1100 usec
= s
->ipol_usec
+ pa_timeval_age(&s
->ipol_timestamp
);
1103 if (usec
< s
->previous_ipol_time
)
1104 usec
= s
->previous_ipol_time
;
1106 s
->previous_ipol_time
= usec
;
1111 pa_usec_t
pa_stream_get_interpolated_latency(pa_stream
*s
, int *negative
) {
1115 assert(s
->ref
>= 1);
1117 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (pa_usec_t
) -1);
1118 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
, (pa_usec_t
) -1);
1119 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->interpolate
, PA_ERR_BADSTATE
, (pa_usec_t
) -1);
1121 t
= pa_stream_get_interpolated_time(s
);
1122 c
= pa_bytes_to_usec(s
->counter
, &s
->sample_spec
);
1123 return time_counter_diff(s
, t
, c
, negative
);