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"
36 #define LATENCY_IPOL_INTERVAL_USEC (100000L)
38 struct pa_stream
*pa_stream_new(struct pa_context
*c
, const char *name
, const struct pa_sample_spec
*ss
) {
42 s
= pa_xmalloc(sizeof(struct pa_stream
));
45 s
->mainloop
= c
->mainloop
;
47 s
->read_callback
= NULL
;
48 s
->read_userdata
= NULL
;
49 s
->write_callback
= NULL
;
50 s
->write_userdata
= NULL
;
51 s
->state_callback
= NULL
;
52 s
->state_userdata
= NULL
;
54 s
->direction
= PA_STREAM_NODIRECTION
;
55 s
->name
= pa_xstrdup(name
);
59 s
->device_index
= PA_INVALID_INDEX
;
60 s
->requested_bytes
= 0;
61 s
->state
= PA_STREAM_DISCONNECTED
;
62 memset(&s
->buffer_attr
, 0, sizeof(s
->buffer_attr
));
71 memset(&s
->ipol_timestamp
, 0, sizeof(s
->ipol_timestamp
));
74 PA_LLIST_PREPEND(struct pa_stream
, c
->streams
, s
);
76 return pa_stream_ref(s
);
79 static void stream_free(struct pa_stream
*s
) {
84 s
->mainloop
->time_free(s
->ipol_event
);
91 void pa_stream_unref(struct pa_stream
*s
) {
92 assert(s
&& s
->ref
>= 1);
98 struct pa_stream
* pa_stream_ref(struct pa_stream
*s
) {
99 assert(s
&& s
->ref
>= 1);
104 enum pa_stream_state
pa_stream_get_state(struct pa_stream
*s
) {
105 assert(s
&& s
->ref
>= 1);
109 struct pa_context
* pa_stream_get_context(struct pa_stream
*s
) {
110 assert(s
&& s
->ref
>= 1);
114 uint32_t pa_stream_get_index(struct pa_stream
*s
) {
115 assert(s
&& s
->ref
>= 1);
116 return s
->device_index
;
119 void pa_stream_set_state(struct pa_stream
*s
, enum pa_stream_state st
) {
120 assert(s
&& s
->ref
>= 1);
129 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
) && s
->context
) {
130 if (s
->channel_valid
)
131 pa_dynarray_put((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, s
->channel
, NULL
);
133 PA_LLIST_REMOVE(struct pa_stream
, s
->context
->streams
, s
);
137 if (s
->state_callback
)
138 s
->state_callback(s
, s
->state_userdata
);
143 void pa_command_stream_killed(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
144 struct pa_context
*c
= userdata
;
147 assert(pd
&& (command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
) && t
&& c
);
151 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
152 !pa_tagstruct_eof(t
)) {
153 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
157 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, channel
)))
160 c
->error
= PA_ERROR_KILLED
;
161 pa_stream_set_state(s
, PA_STREAM_FAILED
);
167 void pa_command_request(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
169 struct pa_context
*c
= userdata
;
170 uint32_t bytes
, channel
;
171 assert(pd
&& command
== PA_COMMAND_REQUEST
&& t
&& c
);
175 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
176 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
177 !pa_tagstruct_eof(t
)) {
178 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
182 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
185 if (s
->state
!= PA_STREAM_READY
)
190 s
->requested_bytes
+= bytes
;
192 if (s
->requested_bytes
&& s
->write_callback
)
193 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
201 static void ipol_callback(struct pa_mainloop_api
*m
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
203 struct pa_stream
*s
= userdata
;
206 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
208 gettimeofday(&tv2
, NULL
);
209 tv2
.tv_usec
+= LATENCY_IPOL_INTERVAL_USEC
;
211 m
->time_restart(e
, &tv2
);
217 void pa_create_stream_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
218 struct pa_stream
*s
= userdata
;
219 assert(pd
&& s
&& s
->state
== PA_STREAM_CREATING
);
223 if (command
!= PA_COMMAND_REPLY
) {
224 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
227 pa_stream_set_state(s
, PA_STREAM_FAILED
);
231 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
232 ((s
->direction
!= PA_STREAM_UPLOAD
) && pa_tagstruct_getu32(t
, &s
->device_index
) < 0) ||
233 ((s
->direction
!= PA_STREAM_RECORD
) && pa_tagstruct_getu32(t
, &s
->requested_bytes
) < 0) ||
234 !pa_tagstruct_eof(t
)) {
235 pa_context_fail(s
->context
, PA_ERROR_PROTOCOL
);
239 s
->channel_valid
= 1;
240 pa_dynarray_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, s
->channel
, s
);
241 pa_stream_set_state(s
, PA_STREAM_READY
);
243 if (s
->interpolate
) {
245 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
247 gettimeofday(&tv
, NULL
);
248 tv
.tv_usec
+= LATENCY_IPOL_INTERVAL_USEC
; /* every 100 ms */
250 assert(!s
->ipol_event
);
251 s
->ipol_event
= s
->mainloop
->time_new(s
->mainloop
, &tv
, &ipol_callback
, s
);
254 if (s
->requested_bytes
&& s
->ref
> 1 && s
->write_callback
)
255 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
261 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
) {
262 struct pa_tagstruct
*t
;
264 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_DISCONNECTED
);
268 s
->interpolate
= !!(flags
& PA_STREAM_INTERPOLATE_LATENCY
);
269 pa_stream_trash_ipol(s
);
272 s
->buffer_attr
= *attr
;
274 s
->buffer_attr
.maxlength
= DEFAULT_MAXLENGTH
;
275 s
->buffer_attr
.tlength
= DEFAULT_TLENGTH
;
276 s
->buffer_attr
.prebuf
= DEFAULT_PREBUF
;
277 s
->buffer_attr
.minreq
= DEFAULT_MINREQ
;
278 s
->buffer_attr
.fragsize
= DEFAULT_FRAGSIZE
;
281 pa_stream_set_state(s
, PA_STREAM_CREATING
);
283 t
= pa_tagstruct_new(NULL
, 0);
287 if (s
->direction
== PA_STREAM_PLAYBACK
)
288 dev
= s
->context
->conf
->default_sink
;
290 dev
= s
->context
->conf
->default_source
;
293 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
);
294 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
295 pa_tagstruct_puts(t
, s
->name
);
296 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
297 pa_tagstruct_putu32(t
, PA_INVALID_INDEX
);
298 pa_tagstruct_puts(t
, dev
);
299 pa_tagstruct_putu32(t
, s
->buffer_attr
.maxlength
);
300 pa_tagstruct_put_boolean(t
, !!(flags
& PA_STREAM_START_CORKED
));
301 if (s
->direction
== PA_STREAM_PLAYBACK
) {
302 pa_tagstruct_putu32(t
, s
->buffer_attr
.tlength
);
303 pa_tagstruct_putu32(t
, s
->buffer_attr
.prebuf
);
304 pa_tagstruct_putu32(t
, s
->buffer_attr
.minreq
);
305 pa_tagstruct_putu32(t
, volume
);
307 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
309 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
310 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
);
315 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
) {
316 assert(s
&& s
->context
->state
== PA_CONTEXT_READY
&& s
->ref
>= 1);
317 s
->direction
= PA_STREAM_PLAYBACK
;
318 create_stream(s
, dev
, attr
, flags
, volume
);
321 void pa_stream_connect_record(struct pa_stream
*s
, const char *dev
, const struct pa_buffer_attr
*attr
, enum pa_stream_flags flags
) {
322 assert(s
&& s
->context
->state
== PA_CONTEXT_READY
&& s
->ref
>= 1);
323 s
->direction
= PA_STREAM_RECORD
;
324 create_stream(s
, dev
, attr
, flags
, 0);
327 void pa_stream_write(struct pa_stream
*s
, const void *data
, size_t length
, void (*free_cb
)(void *p
), size_t delta
) {
328 struct pa_memchunk chunk
;
329 assert(s
&& s
->context
&& data
&& length
&& s
->state
== PA_STREAM_READY
&& s
->ref
>= 1);
332 chunk
.memblock
= pa_memblock_new_user((void*) data
, length
, free_cb
, s
->context
->memblock_stat
);
333 assert(chunk
.memblock
&& chunk
.memblock
->data
);
335 chunk
.memblock
= pa_memblock_new(length
, s
->context
->memblock_stat
);
336 assert(chunk
.memblock
&& chunk
.memblock
->data
);
337 memcpy(chunk
.memblock
->data
, data
, length
);
340 chunk
.length
= length
;
342 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, delta
, &chunk
);
343 pa_memblock_unref(chunk
.memblock
);
345 if (length
< s
->requested_bytes
)
346 s
->requested_bytes
-= length
;
348 s
->requested_bytes
= 0;
350 s
->counter
+= length
;
353 size_t pa_stream_writable_size(struct pa_stream
*s
) {
354 assert(s
&& s
->ref
>= 1);
355 return s
->state
== PA_STREAM_READY
? s
->requested_bytes
: 0;
358 struct pa_operation
* pa_stream_drain(struct pa_stream
*s
, void (*cb
) (struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
359 struct pa_operation
*o
;
360 struct pa_tagstruct
*t
;
362 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
);
364 o
= pa_operation_new(s
->context
, s
);
367 o
->userdata
= userdata
;
369 t
= pa_tagstruct_new(NULL
, 0);
371 pa_tagstruct_putu32(t
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
);
372 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
373 pa_tagstruct_putu32(t
, s
->channel
);
374 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
375 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
377 return pa_operation_ref(o
);
380 static void stream_get_latency_info_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
381 struct pa_operation
*o
= userdata
;
382 struct pa_latency_info i
, *p
= NULL
;
383 struct timeval local
, remote
, now
;
384 assert(pd
&& o
&& o
->stream
&& o
->context
);
386 if (command
!= PA_COMMAND_REPLY
) {
387 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
390 } else if (pa_tagstruct_get_usec(t
, &i
.buffer_usec
) < 0 ||
391 pa_tagstruct_get_usec(t
, &i
.sink_usec
) < 0 ||
392 pa_tagstruct_get_usec(t
, &i
.source_usec
) < 0 ||
393 pa_tagstruct_get_boolean(t
, &i
.playing
) < 0 ||
394 pa_tagstruct_getu32(t
, &i
.queue_length
) < 0 ||
395 pa_tagstruct_get_timeval(t
, &local
) < 0 ||
396 pa_tagstruct_get_timeval(t
, &remote
) < 0 ||
397 pa_tagstruct_getu64(t
, &i
.counter
) < 0 ||
398 !pa_tagstruct_eof(t
)) {
399 pa_context_fail(o
->context
, PA_ERROR_PROTOCOL
);
402 gettimeofday(&now
, NULL
);
404 if (pa_timeval_cmp(&local
, &remote
) < 0 && pa_timeval_cmp(&remote
, &now
)) {
405 /* local and remote seem to have synchronized clocks */
407 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
)
408 i
.transport_usec
= pa_timeval_diff(&remote
, &local
);
410 i
.transport_usec
= pa_timeval_diff(&now
, &remote
);
412 i
.synchronized_clocks
= 1;
413 i
.timestamp
= remote
;
415 /* clocks are not synchronized, let's estimate latency then */
416 i
.transport_usec
= pa_timeval_diff(&now
, &local
)/2;
417 i
.synchronized_clocks
= 0;
419 pa_timeval_add(&i
.timestamp
, i
.transport_usec
);
422 if (o
->stream
->interpolate
) {
423 o
->stream
->ipol_timestamp
= i
.timestamp
;
424 o
->stream
->ipol_usec
= pa_stream_get_time(o
->stream
, &i
);
431 void (*cb
)(struct pa_stream
*s
, const struct pa_latency_info
*i
, void *userdata
) = o
->callback
;
432 cb(o
->stream
, p
, o
->userdata
);
436 pa_operation_done(o
);
437 pa_operation_unref(o
);
440 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
) {
442 struct pa_operation
*o
;
443 struct pa_tagstruct
*t
;
445 assert(s
&& s
->direction
!= PA_STREAM_UPLOAD
);
447 o
= pa_operation_new(s
->context
, s
);
450 o
->userdata
= userdata
;
452 t
= pa_tagstruct_new(NULL
, 0);
454 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_GET_PLAYBACK_LATENCY
: PA_COMMAND_GET_RECORD_LATENCY
);
455 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
456 pa_tagstruct_putu32(t
, s
->channel
);
458 gettimeofday(&now
, NULL
);
459 pa_tagstruct_put_timeval(t
, &now
);
460 pa_tagstruct_putu64(t
, s
->counter
);
462 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
463 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, stream_get_latency_info_callback
, o
);
465 return pa_operation_ref(o
);
468 void pa_stream_disconnect_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
469 struct pa_stream
*s
= userdata
;
470 assert(pd
&& s
&& s
->ref
>= 1);
474 if (command
!= PA_COMMAND_REPLY
) {
475 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
478 pa_stream_set_state(s
, PA_STREAM_FAILED
);
480 } else if (!pa_tagstruct_eof(t
)) {
481 pa_context_fail(s
->context
, PA_ERROR_PROTOCOL
);
485 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
491 void pa_stream_disconnect(struct pa_stream
*s
) {
492 struct pa_tagstruct
*t
;
494 assert(s
&& s
->ref
>= 1);
496 if (!s
->channel_valid
|| !s
->context
->state
== PA_CONTEXT_READY
)
501 t
= pa_tagstruct_new(NULL
, 0);
504 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
505 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
));
506 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
507 pa_tagstruct_putu32(t
, s
->channel
);
508 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
509 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
);
514 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
) {
515 assert(s
&& s
->ref
>= 1);
516 s
->read_callback
= cb
;
517 s
->read_userdata
= userdata
;
520 void pa_stream_set_write_callback(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*p
, size_t length
, void *userdata
), void *userdata
) {
521 assert(s
&& s
->ref
>= 1);
522 s
->write_callback
= cb
;
523 s
->write_userdata
= userdata
;
526 void pa_stream_set_state_callback(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, void *userdata
), void *userdata
) {
527 assert(s
&& s
->ref
>= 1);
528 s
->state_callback
= cb
;
529 s
->state_userdata
= userdata
;
532 void pa_stream_simple_ack_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
533 struct pa_operation
*o
= userdata
;
535 assert(pd
&& o
&& o
->context
&& o
->ref
>= 1);
537 if (command
!= PA_COMMAND_REPLY
) {
538 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
542 } else if (!pa_tagstruct_eof(t
)) {
543 pa_context_fail(o
->context
, PA_ERROR_PROTOCOL
);
548 void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
) = o
->callback
;
549 cb(o
->stream
, success
, o
->userdata
);
553 pa_operation_done(o
);
554 pa_operation_unref(o
);
557 struct pa_operation
* pa_stream_cork(struct pa_stream
*s
, int b
, void (*cb
) (struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
558 struct pa_operation
*o
;
559 struct pa_tagstruct
*t
;
561 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
);
563 if (s
->interpolate
) {
565 s
->ipol_usec
= pa_stream_get_interpolated_time(s
);
566 else if (s
->corked
&& !b
)
567 gettimeofday(&s
->ipol_timestamp
, NULL
);
572 o
= pa_operation_new(s
->context
, s
);
575 o
->userdata
= userdata
;
577 t
= pa_tagstruct_new(NULL
, 0);
579 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CORK_PLAYBACK_STREAM
: PA_COMMAND_CORK_RECORD_STREAM
);
580 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
581 pa_tagstruct_putu32(t
, s
->channel
);
582 pa_tagstruct_put_boolean(t
, !!b
);
583 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
584 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
586 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
588 return pa_operation_ref(o
);
591 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
) {
592 struct pa_tagstruct
*t
;
593 struct pa_operation
*o
;
595 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
);
597 o
= pa_operation_new(s
->context
, s
);
599 o
->userdata
= userdata
;
601 t
= pa_tagstruct_new(NULL
, 0);
602 pa_tagstruct_putu32(t
, command
);
603 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
604 pa_tagstruct_putu32(t
, s
->channel
);
605 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
606 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
608 return pa_operation_ref(o
);
611 struct pa_operation
* pa_stream_flush(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
612 struct pa_operation
*o
;
613 o
= pa_stream_send_simple_command(s
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_FLUSH_PLAYBACK_STREAM
: PA_COMMAND_FLUSH_RECORD_STREAM
, cb
, userdata
);
614 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
618 struct pa_operation
* pa_stream_prebuf(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
619 struct pa_operation
*o
;
620 o
= pa_stream_send_simple_command(s
, PA_COMMAND_PREBUF_PLAYBACK_STREAM
, cb
, userdata
);
621 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
625 struct pa_operation
* pa_stream_trigger(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
626 struct pa_operation
*o
;
627 o
= pa_stream_send_simple_command(s
, PA_COMMAND_TRIGGER_PLAYBACK_STREAM
, cb
, userdata
);
628 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
632 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
) {
633 struct pa_operation
*o
;
634 struct pa_tagstruct
*t
;
636 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
&& name
&& s
->direction
!= PA_STREAM_UPLOAD
);
638 o
= pa_operation_new(s
->context
, s
);
641 o
->userdata
= userdata
;
643 t
= pa_tagstruct_new(NULL
, 0);
645 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_NAME
: PA_COMMAND_SET_PLAYBACK_STREAM_NAME
);
646 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
647 pa_tagstruct_putu32(t
, s
->channel
);
648 pa_tagstruct_puts(t
, name
);
649 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
650 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
652 return pa_operation_ref(o
);
655 uint64_t pa_stream_get_counter(struct pa_stream
*s
) {
660 pa_usec_t
pa_stream_get_time(struct pa_stream
*s
, const struct pa_latency_info
*i
) {
664 usec
= pa_bytes_to_usec(i
->counter
, &s
->sample_spec
);
667 if (s
->direction
== PA_STREAM_PLAYBACK
) {
668 pa_usec_t latency
= i
->transport_usec
+ i
->buffer_usec
+ i
->sink_usec
;
674 } else if (s
->direction
== PA_STREAM_RECORD
) {
675 usec
+= i
->source_usec
+ i
->buffer_usec
+ i
->transport_usec
;
677 if (usec
> i
->sink_usec
)
678 usec
-= i
->sink_usec
;
684 if (usec
< s
->previous_time
)
685 usec
= s
->previous_time
;
687 s
->previous_time
= usec
;
692 static pa_usec_t
time_counter_diff(struct pa_stream
*s
, pa_usec_t t
, pa_usec_t c
, int *negative
) {
699 if (s
->direction
== PA_STREAM_RECORD
) {
710 pa_usec_t
pa_stream_get_latency(struct pa_stream
*s
, const struct pa_latency_info
*i
, int *negative
) {
714 t
= pa_stream_get_time(s
, i
);
715 c
= pa_bytes_to_usec(s
->counter
, &s
->sample_spec
);
717 return time_counter_diff(s
, t
, c
, negative
);
720 const struct pa_sample_spec
* pa_stream_get_sample_spec(struct pa_stream
*s
) {
722 return &s
->sample_spec
;
725 void pa_stream_trash_ipol(struct pa_stream
*s
) {
731 memset(&s
->ipol_timestamp
, 0, sizeof(s
->ipol_timestamp
));
735 pa_usec_t
pa_stream_get_interpolated_time(struct pa_stream
*s
) {
737 assert(s
&& s
->interpolate
);
742 if (s
->ipol_timestamp
.tv_sec
== 0)
745 usec
= s
->ipol_usec
+ pa_timeval_age(&s
->ipol_timestamp
);
748 if (usec
< s
->previous_time
)
749 usec
= s
->previous_time
;
751 s
->previous_time
= usec
;
755 pa_usec_t
pa_stream_get_interpolated_latency(struct pa_stream
*s
, int *negative
) {
757 assert(s
&& s
->interpolate
);
759 t
= pa_stream_get_interpolated_time(s
);
760 c
= pa_bytes_to_usec(s
->counter
, &s
->sample_spec
);
762 return time_counter_diff(s
, t
, c
, negative
);