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 "polyplib-internal.h"
33 #include "pstream-util.h"
37 #define LATENCY_IPOL_INTERVAL_USEC (10000L)
39 struct pa_stream
*pa_stream_new(struct pa_context
*c
, const char *name
, const struct pa_sample_spec
*ss
) {
43 s
= pa_xmalloc(sizeof(struct pa_stream
));
46 s
->mainloop
= c
->mainloop
;
48 s
->read_callback
= NULL
;
49 s
->read_userdata
= NULL
;
50 s
->write_callback
= NULL
;
51 s
->write_userdata
= NULL
;
52 s
->state_callback
= NULL
;
53 s
->state_userdata
= NULL
;
55 s
->direction
= PA_STREAM_NODIRECTION
;
56 s
->name
= pa_xstrdup(name
);
60 s
->device_index
= PA_INVALID_INDEX
;
61 s
->requested_bytes
= 0;
62 s
->state
= PA_STREAM_DISCONNECTED
;
63 memset(&s
->buffer_attr
, 0, sizeof(s
->buffer_attr
));
65 s
->mcalign
= pa_mcalign_new(pa_frame_size(ss
), c
->memblock_stat
);
74 memset(&s
->ipol_timestamp
, 0, sizeof(s
->ipol_timestamp
));
76 s
->ipol_requested
= 0;
78 PA_LLIST_PREPEND(struct pa_stream
, c
->streams
, s
);
80 return pa_stream_ref(s
);
83 static void stream_free(struct pa_stream
*s
) {
88 s
->mainloop
->time_free(s
->ipol_event
);
91 pa_mcalign_free(s
->mcalign
);
97 void pa_stream_unref(struct pa_stream
*s
) {
98 assert(s
&& s
->ref
>= 1);
104 struct pa_stream
* pa_stream_ref(struct pa_stream
*s
) {
105 assert(s
&& s
->ref
>= 1);
110 enum pa_stream_state
pa_stream_get_state(struct pa_stream
*s
) {
111 assert(s
&& s
->ref
>= 1);
115 struct pa_context
* pa_stream_get_context(struct pa_stream
*s
) {
116 assert(s
&& s
->ref
>= 1);
120 uint32_t pa_stream_get_index(struct pa_stream
*s
) {
121 assert(s
&& s
->ref
>= 1);
122 return s
->device_index
;
125 void pa_stream_set_state(struct pa_stream
*s
, enum pa_stream_state st
) {
126 assert(s
&& s
->ref
>= 1);
135 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
) && s
->context
) {
136 if (s
->channel_valid
)
137 pa_dynarray_put((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, s
->channel
, NULL
);
139 PA_LLIST_REMOVE(struct pa_stream
, s
->context
->streams
, s
);
143 if (s
->state_callback
)
144 s
->state_callback(s
, s
->state_userdata
);
149 void pa_command_stream_killed(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
150 struct pa_context
*c
= userdata
;
153 assert(pd
&& (command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
) && t
&& c
);
157 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
158 !pa_tagstruct_eof(t
)) {
159 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
163 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, channel
)))
166 c
->error
= PA_ERROR_KILLED
;
167 pa_stream_set_state(s
, PA_STREAM_FAILED
);
173 void pa_command_request(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
175 struct pa_context
*c
= userdata
;
176 uint32_t bytes
, channel
;
177 assert(pd
&& command
== PA_COMMAND_REQUEST
&& t
&& c
);
181 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
182 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
183 !pa_tagstruct_eof(t
)) {
184 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
188 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
191 if (s
->state
!= PA_STREAM_READY
)
196 s
->requested_bytes
+= bytes
;
198 if (s
->requested_bytes
&& s
->write_callback
)
199 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
207 static void ipol_callback(struct pa_mainloop_api
*m
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
209 struct pa_stream
*s
= userdata
;
213 /* pa_log("requesting new ipol data\n"); */
215 if (s
->state
== PA_STREAM_READY
&& !s
->ipol_requested
) {
216 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
217 s
->ipol_requested
= 1;
220 gettimeofday(&tv2
, NULL
);
221 pa_timeval_add(&tv2
, LATENCY_IPOL_INTERVAL_USEC
);
223 m
->time_restart(e
, &tv2
);
229 void pa_create_stream_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
230 struct pa_stream
*s
= userdata
;
231 assert(pd
&& s
&& s
->state
== PA_STREAM_CREATING
);
235 if (command
!= PA_COMMAND_REPLY
) {
236 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
239 pa_stream_set_state(s
, PA_STREAM_FAILED
);
243 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
244 ((s
->direction
!= PA_STREAM_UPLOAD
) && pa_tagstruct_getu32(t
, &s
->device_index
) < 0) ||
245 ((s
->direction
!= PA_STREAM_RECORD
) && pa_tagstruct_getu32(t
, &s
->requested_bytes
) < 0) ||
246 !pa_tagstruct_eof(t
)) {
247 pa_context_fail(s
->context
, PA_ERROR_PROTOCOL
);
251 s
->channel_valid
= 1;
252 pa_dynarray_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, s
->channel
, s
);
253 pa_stream_set_state(s
, PA_STREAM_READY
);
255 if (s
->interpolate
) {
257 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
259 gettimeofday(&tv
, NULL
);
260 tv
.tv_usec
+= LATENCY_IPOL_INTERVAL_USEC
; /* every 100 ms */
262 assert(!s
->ipol_event
);
263 s
->ipol_event
= s
->mainloop
->time_new(s
->mainloop
, &tv
, &ipol_callback
, s
);
266 if (s
->requested_bytes
&& s
->ref
> 1 && s
->write_callback
)
267 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
273 static void create_stream(struct pa_stream
*s
, const char *dev
, const struct pa_buffer_attr
*attr
, enum pa_stream_flags flags
, pa_volume_t volume
) {
274 struct pa_tagstruct
*t
;
276 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_DISCONNECTED
);
280 s
->interpolate
= !!(flags
& PA_STREAM_INTERPOLATE_LATENCY
);
281 pa_stream_trash_ipol(s
);
284 s
->buffer_attr
= *attr
;
287 s
->buffer_attr
.tlength
= pa_bytes_per_second(&s
->sample_spec
)/2;
288 s
->buffer_attr
.maxlength
= (s
->buffer_attr
.tlength
*3)/2;
289 s
->buffer_attr
.minreq
= s
->buffer_attr
.tlength
/100;
290 s
->buffer_attr
.prebuf
= s
->buffer_attr
.tlength
- s
->buffer_attr
.minreq
;
291 s
->buffer_attr
.fragsize
= s
->buffer_attr
.tlength
/100;
294 pa_stream_set_state(s
, PA_STREAM_CREATING
);
296 t
= pa_tagstruct_new(NULL
, 0);
300 if (s
->direction
== PA_STREAM_PLAYBACK
)
301 dev
= s
->context
->conf
->default_sink
;
303 dev
= s
->context
->conf
->default_source
;
306 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
);
307 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
308 pa_tagstruct_puts(t
, s
->name
);
309 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
310 pa_tagstruct_putu32(t
, PA_INVALID_INDEX
);
311 pa_tagstruct_puts(t
, dev
);
312 pa_tagstruct_putu32(t
, s
->buffer_attr
.maxlength
);
313 pa_tagstruct_put_boolean(t
, !!(flags
& PA_STREAM_START_CORKED
));
314 if (s
->direction
== PA_STREAM_PLAYBACK
) {
315 pa_tagstruct_putu32(t
, s
->buffer_attr
.tlength
);
316 pa_tagstruct_putu32(t
, s
->buffer_attr
.prebuf
);
317 pa_tagstruct_putu32(t
, s
->buffer_attr
.minreq
);
318 pa_tagstruct_putu32(t
, volume
);
320 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
322 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
323 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
);
328 void pa_stream_connect_playback(struct pa_stream
*s
, const char *dev
, const struct pa_buffer_attr
*attr
, enum pa_stream_flags flags
, pa_volume_t volume
) {
329 assert(s
&& s
->context
->state
== PA_CONTEXT_READY
&& s
->ref
>= 1);
330 s
->direction
= PA_STREAM_PLAYBACK
;
331 create_stream(s
, dev
, attr
, flags
, volume
);
334 void pa_stream_connect_record(struct pa_stream
*s
, const char *dev
, const struct pa_buffer_attr
*attr
, enum pa_stream_flags flags
) {
335 assert(s
&& s
->context
->state
== PA_CONTEXT_READY
&& s
->ref
>= 1);
336 s
->direction
= PA_STREAM_RECORD
;
337 create_stream(s
, dev
, attr
, flags
, 0);
340 void pa_stream_write(struct pa_stream
*s
, const void *data
, size_t length
, void (*free_cb
)(void *p
), size_t delta
) {
341 struct pa_memchunk chunk
;
342 assert(s
&& s
->context
&& data
&& length
&& s
->state
== PA_STREAM_READY
&& s
->ref
>= 1);
345 chunk
.memblock
= pa_memblock_new_user((void*) data
, length
, free_cb
, 1, s
->context
->memblock_stat
);
346 assert(chunk
.memblock
&& chunk
.memblock
->data
);
348 chunk
.memblock
= pa_memblock_new(length
, s
->context
->memblock_stat
);
349 assert(chunk
.memblock
&& chunk
.memblock
->data
);
350 memcpy(chunk
.memblock
->data
, data
, length
);
353 chunk
.length
= length
;
355 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, delta
, &chunk
);
356 pa_memblock_unref(chunk
.memblock
);
358 if (length
< s
->requested_bytes
)
359 s
->requested_bytes
-= length
;
361 s
->requested_bytes
= 0;
363 s
->counter
+= length
;
366 size_t pa_stream_writable_size(struct pa_stream
*s
) {
367 assert(s
&& s
->ref
>= 1);
368 return s
->state
== PA_STREAM_READY
? s
->requested_bytes
: 0;
371 struct pa_operation
* pa_stream_drain(struct pa_stream
*s
, void (*cb
) (struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
372 struct pa_operation
*o
;
373 struct pa_tagstruct
*t
;
375 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
);
377 o
= pa_operation_new(s
->context
, s
);
380 o
->userdata
= userdata
;
382 t
= pa_tagstruct_new(NULL
, 0);
384 pa_tagstruct_putu32(t
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
);
385 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
386 pa_tagstruct_putu32(t
, s
->channel
);
387 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
388 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
390 return pa_operation_ref(o
);
393 static void stream_get_latency_info_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
394 struct pa_operation
*o
= userdata
;
395 struct pa_latency_info i
, *p
= NULL
;
396 struct timeval local
, remote
, now
;
397 assert(pd
&& o
&& o
->stream
&& o
->context
);
399 if (command
!= PA_COMMAND_REPLY
) {
400 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
403 } else if (pa_tagstruct_get_usec(t
, &i
.buffer_usec
) < 0 ||
404 pa_tagstruct_get_usec(t
, &i
.sink_usec
) < 0 ||
405 pa_tagstruct_get_usec(t
, &i
.source_usec
) < 0 ||
406 pa_tagstruct_get_boolean(t
, &i
.playing
) < 0 ||
407 pa_tagstruct_getu32(t
, &i
.queue_length
) < 0 ||
408 pa_tagstruct_get_timeval(t
, &local
) < 0 ||
409 pa_tagstruct_get_timeval(t
, &remote
) < 0 ||
410 pa_tagstruct_getu64(t
, &i
.counter
) < 0 ||
411 !pa_tagstruct_eof(t
)) {
412 pa_context_fail(o
->context
, PA_ERROR_PROTOCOL
);
415 gettimeofday(&now
, NULL
);
417 if (pa_timeval_cmp(&local
, &remote
) <= 0 && pa_timeval_cmp(&remote
, &now
) <= 0) {
418 /* local and remote seem to have synchronized clocks */
420 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
)
421 i
.transport_usec
= pa_timeval_diff(&remote
, &local
);
423 i
.transport_usec
= pa_timeval_diff(&now
, &remote
);
425 i
.synchronized_clocks
= 1;
426 i
.timestamp
= remote
;
428 /* clocks are not synchronized, let's estimate latency then */
429 i
.transport_usec
= pa_timeval_diff(&now
, &local
)/2;
430 i
.synchronized_clocks
= 0;
432 pa_timeval_add(&i
.timestamp
, i
.transport_usec
);
435 if (o
->stream
->interpolate
) {
436 /* pa_log("new interpol data\n"); */
437 o
->stream
->ipol_timestamp
= i
.timestamp
;
438 o
->stream
->ipol_usec
= pa_stream_get_time(o
->stream
, &i
);
439 o
->stream
->ipol_requested
= 0;
446 void (*cb
)(struct pa_stream
*s
, const struct pa_latency_info
*i
, void *userdata
) = o
->callback
;
447 cb(o
->stream
, p
, o
->userdata
);
451 pa_operation_done(o
);
452 pa_operation_unref(o
);
455 struct pa_operation
* pa_stream_get_latency_info(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*p
, const struct pa_latency_info
*i
, void *userdata
), void *userdata
) {
457 struct pa_operation
*o
;
458 struct pa_tagstruct
*t
;
460 assert(s
&& s
->direction
!= PA_STREAM_UPLOAD
);
462 o
= pa_operation_new(s
->context
, s
);
465 o
->userdata
= userdata
;
467 t
= pa_tagstruct_new(NULL
, 0);
469 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_GET_PLAYBACK_LATENCY
: PA_COMMAND_GET_RECORD_LATENCY
);
470 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
471 pa_tagstruct_putu32(t
, s
->channel
);
473 gettimeofday(&now
, NULL
);
474 pa_tagstruct_put_timeval(t
, &now
);
475 pa_tagstruct_putu64(t
, s
->counter
);
477 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
478 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, stream_get_latency_info_callback
, o
);
480 return pa_operation_ref(o
);
483 void pa_stream_disconnect_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
484 struct pa_stream
*s
= userdata
;
485 assert(pd
&& s
&& s
->ref
>= 1);
489 if (command
!= PA_COMMAND_REPLY
) {
490 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
493 pa_stream_set_state(s
, PA_STREAM_FAILED
);
495 } else if (!pa_tagstruct_eof(t
)) {
496 pa_context_fail(s
->context
, PA_ERROR_PROTOCOL
);
500 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
506 void pa_stream_disconnect(struct pa_stream
*s
) {
507 struct pa_tagstruct
*t
;
509 assert(s
&& s
->ref
>= 1);
511 if (!s
->channel_valid
|| !s
->context
->state
== PA_CONTEXT_READY
)
516 t
= pa_tagstruct_new(NULL
, 0);
519 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
520 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
));
521 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
522 pa_tagstruct_putu32(t
, s
->channel
);
523 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
524 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
);
529 void pa_stream_set_read_callback(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*p
, const void*data
, size_t length
, void *userdata
), void *userdata
) {
530 assert(s
&& s
->ref
>= 1);
531 s
->read_callback
= cb
;
532 s
->read_userdata
= userdata
;
535 void pa_stream_set_write_callback(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*p
, size_t length
, void *userdata
), void *userdata
) {
536 assert(s
&& s
->ref
>= 1);
537 s
->write_callback
= cb
;
538 s
->write_userdata
= userdata
;
541 void pa_stream_set_state_callback(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, void *userdata
), void *userdata
) {
542 assert(s
&& s
->ref
>= 1);
543 s
->state_callback
= cb
;
544 s
->state_userdata
= userdata
;
547 void pa_stream_simple_ack_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
548 struct pa_operation
*o
= userdata
;
550 assert(pd
&& o
&& o
->context
&& o
->ref
>= 1);
552 if (command
!= PA_COMMAND_REPLY
) {
553 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
557 } else if (!pa_tagstruct_eof(t
)) {
558 pa_context_fail(o
->context
, PA_ERROR_PROTOCOL
);
563 void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
) = o
->callback
;
564 cb(o
->stream
, success
, o
->userdata
);
568 pa_operation_done(o
);
569 pa_operation_unref(o
);
572 struct pa_operation
* pa_stream_cork(struct pa_stream
*s
, int b
, void (*cb
) (struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
573 struct pa_operation
*o
;
574 struct pa_tagstruct
*t
;
576 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
);
578 if (s
->interpolate
) {
581 s
->ipol_usec
= pa_stream_get_interpolated_time(s
);
582 else if (s
->corked
&& !b
)
584 gettimeofday(&s
->ipol_timestamp
, NULL
);
589 o
= pa_operation_new(s
->context
, s
);
592 o
->userdata
= userdata
;
594 t
= pa_tagstruct_new(NULL
, 0);
596 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CORK_PLAYBACK_STREAM
: PA_COMMAND_CORK_RECORD_STREAM
);
597 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
598 pa_tagstruct_putu32(t
, s
->channel
);
599 pa_tagstruct_put_boolean(t
, !!b
);
600 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
601 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
603 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
605 return pa_operation_ref(o
);
608 struct pa_operation
* pa_stream_send_simple_command(struct pa_stream
*s
, uint32_t command
, void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
609 struct pa_tagstruct
*t
;
610 struct pa_operation
*o
;
612 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
);
614 o
= pa_operation_new(s
->context
, s
);
616 o
->userdata
= userdata
;
618 t
= pa_tagstruct_new(NULL
, 0);
619 pa_tagstruct_putu32(t
, command
);
620 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
621 pa_tagstruct_putu32(t
, s
->channel
);
622 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
623 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
625 return pa_operation_ref(o
);
628 struct pa_operation
* pa_stream_flush(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
629 struct pa_operation
*o
;
630 o
= pa_stream_send_simple_command(s
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_FLUSH_PLAYBACK_STREAM
: PA_COMMAND_FLUSH_RECORD_STREAM
, cb
, userdata
);
631 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
635 struct pa_operation
* pa_stream_prebuf(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
636 struct pa_operation
*o
;
637 o
= pa_stream_send_simple_command(s
, PA_COMMAND_PREBUF_PLAYBACK_STREAM
, cb
, userdata
);
638 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
642 struct pa_operation
* pa_stream_trigger(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
643 struct pa_operation
*o
;
644 o
= pa_stream_send_simple_command(s
, PA_COMMAND_TRIGGER_PLAYBACK_STREAM
, cb
, userdata
);
645 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
649 struct pa_operation
* pa_stream_set_name(struct pa_stream
*s
, const char *name
, void(*cb
)(struct pa_stream
*c
, int success
, void *userdata
), void *userdata
) {
650 struct pa_operation
*o
;
651 struct pa_tagstruct
*t
;
653 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
&& name
&& s
->direction
!= PA_STREAM_UPLOAD
);
655 o
= pa_operation_new(s
->context
, s
);
658 o
->userdata
= userdata
;
660 t
= pa_tagstruct_new(NULL
, 0);
662 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_NAME
: PA_COMMAND_SET_PLAYBACK_STREAM_NAME
);
663 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
664 pa_tagstruct_putu32(t
, s
->channel
);
665 pa_tagstruct_puts(t
, name
);
666 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
667 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
669 return pa_operation_ref(o
);
672 uint64_t pa_stream_get_counter(struct pa_stream
*s
) {
677 pa_usec_t
pa_stream_get_time(struct pa_stream
*s
, const struct pa_latency_info
*i
) {
681 usec
= pa_bytes_to_usec(i
->counter
, &s
->sample_spec
);
684 if (s
->direction
== PA_STREAM_PLAYBACK
) {
685 pa_usec_t latency
= i
->transport_usec
+ i
->buffer_usec
+ i
->sink_usec
;
691 } else if (s
->direction
== PA_STREAM_RECORD
) {
692 usec
+= i
->source_usec
+ i
->buffer_usec
+ i
->transport_usec
;
694 if (usec
> i
->sink_usec
)
695 usec
-= i
->sink_usec
;
701 if (usec
< s
->previous_time
)
702 usec
= s
->previous_time
;
704 s
->previous_time
= usec
;
709 static pa_usec_t
time_counter_diff(struct pa_stream
*s
, pa_usec_t t
, pa_usec_t c
, int *negative
) {
716 if (s
->direction
== PA_STREAM_RECORD
) {
727 pa_usec_t
pa_stream_get_latency(struct pa_stream
*s
, const struct pa_latency_info
*i
, int *negative
) {
731 t
= pa_stream_get_time(s
, i
);
732 c
= pa_bytes_to_usec(s
->counter
, &s
->sample_spec
);
734 return time_counter_diff(s
, t
, c
, negative
);
737 const struct pa_sample_spec
* pa_stream_get_sample_spec(struct pa_stream
*s
) {
739 return &s
->sample_spec
;
742 void pa_stream_trash_ipol(struct pa_stream
*s
) {
748 memset(&s
->ipol_timestamp
, 0, sizeof(s
->ipol_timestamp
));
752 pa_usec_t
pa_stream_get_interpolated_time(struct pa_stream
*s
) {
754 assert(s
&& s
->interpolate
);
759 if (s
->ipol_timestamp
.tv_sec
== 0)
762 usec
= s
->ipol_usec
+ pa_timeval_age(&s
->ipol_timestamp
);
765 if (usec
< s
->previous_time
)
766 usec
= s
->previous_time
;
768 s
->previous_time
= usec
;
772 pa_usec_t
pa_stream_get_interpolated_latency(struct pa_stream
*s
, int *negative
) {
774 assert(s
&& s
->interpolate
);
776 t
= pa_stream_get_interpolated_time(s
);
777 c
= pa_bytes_to_usec(s
->counter
, &s
->sample_spec
);
778 return time_counter_diff(s
, t
, c
, negative
);