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
);
69 s
->previous_ipol_time
= 0;
75 memset(&s
->ipol_timestamp
, 0, sizeof(s
->ipol_timestamp
));
77 s
->ipol_requested
= 0;
79 PA_LLIST_PREPEND(struct pa_stream
, c
->streams
, s
);
81 return pa_stream_ref(s
);
84 static void stream_free(struct pa_stream
*s
) {
89 s
->mainloop
->time_free(s
->ipol_event
);
92 pa_mcalign_free(s
->mcalign
);
98 void pa_stream_unref(struct pa_stream
*s
) {
99 assert(s
&& s
->ref
>= 1);
105 struct pa_stream
* pa_stream_ref(struct pa_stream
*s
) {
106 assert(s
&& s
->ref
>= 1);
111 enum pa_stream_state
pa_stream_get_state(struct pa_stream
*s
) {
112 assert(s
&& s
->ref
>= 1);
116 struct pa_context
* pa_stream_get_context(struct pa_stream
*s
) {
117 assert(s
&& s
->ref
>= 1);
121 uint32_t pa_stream_get_index(struct pa_stream
*s
) {
122 assert(s
&& s
->ref
>= 1);
123 return s
->device_index
;
126 void pa_stream_set_state(struct pa_stream
*s
, enum pa_stream_state st
) {
127 assert(s
&& s
->ref
>= 1);
136 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
) && s
->context
) {
137 if (s
->channel_valid
)
138 pa_dynarray_put((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, s
->channel
, NULL
);
140 PA_LLIST_REMOVE(struct pa_stream
, s
->context
->streams
, s
);
144 if (s
->state_callback
)
145 s
->state_callback(s
, s
->state_userdata
);
150 void pa_command_stream_killed(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
151 struct pa_context
*c
= userdata
;
154 assert(pd
&& (command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
) && t
&& c
);
158 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
159 !pa_tagstruct_eof(t
)) {
160 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
164 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, channel
)))
167 c
->error
= PA_ERROR_KILLED
;
168 pa_stream_set_state(s
, PA_STREAM_FAILED
);
174 void pa_command_request(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
176 struct pa_context
*c
= userdata
;
177 uint32_t bytes
, channel
;
178 assert(pd
&& command
== PA_COMMAND_REQUEST
&& t
&& c
);
182 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
183 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
184 !pa_tagstruct_eof(t
)) {
185 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
189 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
192 if (s
->state
!= PA_STREAM_READY
)
197 s
->requested_bytes
+= bytes
;
199 if (s
->requested_bytes
&& s
->write_callback
)
200 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
208 static void ipol_callback(struct pa_mainloop_api
*m
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
210 struct pa_stream
*s
= userdata
;
214 /* pa_log("requesting new ipol data\n"); */
216 if (s
->state
== PA_STREAM_READY
&& !s
->ipol_requested
) {
217 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
218 s
->ipol_requested
= 1;
221 pa_gettimeofday(&tv2
);
222 pa_timeval_add(&tv2
, LATENCY_IPOL_INTERVAL_USEC
);
224 m
->time_restart(e
, &tv2
);
230 void pa_create_stream_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
231 struct pa_stream
*s
= userdata
;
232 assert(pd
&& s
&& s
->state
== PA_STREAM_CREATING
);
236 if (command
!= PA_COMMAND_REPLY
) {
237 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
240 pa_stream_set_state(s
, PA_STREAM_FAILED
);
244 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
245 ((s
->direction
!= PA_STREAM_UPLOAD
) && pa_tagstruct_getu32(t
, &s
->device_index
) < 0) ||
246 ((s
->direction
!= PA_STREAM_RECORD
) && pa_tagstruct_getu32(t
, &s
->requested_bytes
) < 0) ||
247 !pa_tagstruct_eof(t
)) {
248 pa_context_fail(s
->context
, PA_ERROR_PROTOCOL
);
252 s
->channel_valid
= 1;
253 pa_dynarray_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, s
->channel
, s
);
254 pa_stream_set_state(s
, PA_STREAM_READY
);
256 if (s
->interpolate
) {
258 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
260 pa_gettimeofday(&tv
);
261 tv
.tv_usec
+= LATENCY_IPOL_INTERVAL_USEC
; /* every 100 ms */
263 assert(!s
->ipol_event
);
264 s
->ipol_event
= s
->mainloop
->time_new(s
->mainloop
, &tv
, &ipol_callback
, s
);
267 if (s
->requested_bytes
&& s
->ref
> 1 && s
->write_callback
)
268 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
274 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
) {
275 struct pa_tagstruct
*t
;
277 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_DISCONNECTED
);
281 s
->interpolate
= !!(flags
& PA_STREAM_INTERPOLATE_LATENCY
);
282 pa_stream_trash_ipol(s
);
285 s
->buffer_attr
= *attr
;
288 s
->buffer_attr
.tlength
= pa_bytes_per_second(&s
->sample_spec
)/2;
289 s
->buffer_attr
.maxlength
= (s
->buffer_attr
.tlength
*3)/2;
290 s
->buffer_attr
.minreq
= s
->buffer_attr
.tlength
/100;
291 s
->buffer_attr
.prebuf
= s
->buffer_attr
.tlength
- s
->buffer_attr
.minreq
;
292 s
->buffer_attr
.fragsize
= s
->buffer_attr
.tlength
/100;
295 pa_stream_set_state(s
, PA_STREAM_CREATING
);
297 t
= pa_tagstruct_new(NULL
, 0);
301 if (s
->direction
== PA_STREAM_PLAYBACK
)
302 dev
= s
->context
->conf
->default_sink
;
304 dev
= s
->context
->conf
->default_source
;
307 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
);
308 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
309 pa_tagstruct_puts(t
, s
->name
);
310 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
311 pa_tagstruct_putu32(t
, PA_INVALID_INDEX
);
312 pa_tagstruct_puts(t
, dev
);
313 pa_tagstruct_putu32(t
, s
->buffer_attr
.maxlength
);
314 pa_tagstruct_put_boolean(t
, !!(flags
& PA_STREAM_START_CORKED
));
315 if (s
->direction
== PA_STREAM_PLAYBACK
) {
316 pa_tagstruct_putu32(t
, s
->buffer_attr
.tlength
);
317 pa_tagstruct_putu32(t
, s
->buffer_attr
.prebuf
);
318 pa_tagstruct_putu32(t
, s
->buffer_attr
.minreq
);
319 pa_tagstruct_putu32(t
, volume
);
321 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
323 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
324 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
);
329 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
) {
330 assert(s
&& s
->context
->state
== PA_CONTEXT_READY
&& s
->ref
>= 1);
331 s
->direction
= PA_STREAM_PLAYBACK
;
332 create_stream(s
, dev
, attr
, flags
, volume
);
335 void pa_stream_connect_record(struct pa_stream
*s
, const char *dev
, const struct pa_buffer_attr
*attr
, enum pa_stream_flags flags
) {
336 assert(s
&& s
->context
->state
== PA_CONTEXT_READY
&& s
->ref
>= 1);
337 s
->direction
= PA_STREAM_RECORD
;
338 create_stream(s
, dev
, attr
, flags
, 0);
341 void pa_stream_write(struct pa_stream
*s
, const void *data
, size_t length
, void (*free_cb
)(void *p
), size_t delta
) {
342 struct pa_memchunk chunk
;
343 assert(s
&& s
->context
&& data
&& length
&& s
->state
== PA_STREAM_READY
&& s
->ref
>= 1);
346 chunk
.memblock
= pa_memblock_new_user((void*) data
, length
, free_cb
, 1, s
->context
->memblock_stat
);
347 assert(chunk
.memblock
&& chunk
.memblock
->data
);
349 chunk
.memblock
= pa_memblock_new(length
, s
->context
->memblock_stat
);
350 assert(chunk
.memblock
&& chunk
.memblock
->data
);
351 memcpy(chunk
.memblock
->data
, data
, length
);
354 chunk
.length
= length
;
356 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, delta
, &chunk
);
357 pa_memblock_unref(chunk
.memblock
);
359 if (length
< s
->requested_bytes
)
360 s
->requested_bytes
-= length
;
362 s
->requested_bytes
= 0;
364 s
->counter
+= length
;
367 size_t pa_stream_writable_size(struct pa_stream
*s
) {
368 assert(s
&& s
->ref
>= 1);
369 return s
->state
== PA_STREAM_READY
? s
->requested_bytes
: 0;
372 struct pa_operation
* pa_stream_drain(struct pa_stream
*s
, void (*cb
) (struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
373 struct pa_operation
*o
;
374 struct pa_tagstruct
*t
;
376 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
);
378 o
= pa_operation_new(s
->context
, s
);
381 o
->userdata
= userdata
;
383 t
= pa_tagstruct_new(NULL
, 0);
385 pa_tagstruct_putu32(t
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
);
386 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
387 pa_tagstruct_putu32(t
, s
->channel
);
388 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
389 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
391 return pa_operation_ref(o
);
394 static void stream_get_latency_info_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
395 struct pa_operation
*o
= userdata
;
396 struct pa_latency_info i
, *p
= NULL
;
397 struct timeval local
, remote
, now
;
398 assert(pd
&& o
&& o
->stream
&& o
->context
);
400 if (command
!= PA_COMMAND_REPLY
) {
401 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
404 } else if (pa_tagstruct_get_usec(t
, &i
.buffer_usec
) < 0 ||
405 pa_tagstruct_get_usec(t
, &i
.sink_usec
) < 0 ||
406 pa_tagstruct_get_usec(t
, &i
.source_usec
) < 0 ||
407 pa_tagstruct_get_boolean(t
, &i
.playing
) < 0 ||
408 pa_tagstruct_getu32(t
, &i
.queue_length
) < 0 ||
409 pa_tagstruct_get_timeval(t
, &local
) < 0 ||
410 pa_tagstruct_get_timeval(t
, &remote
) < 0 ||
411 pa_tagstruct_getu64(t
, &i
.counter
) < 0 ||
412 !pa_tagstruct_eof(t
)) {
413 pa_context_fail(o
->context
, PA_ERROR_PROTOCOL
);
416 pa_gettimeofday(&now
);
418 if (pa_timeval_cmp(&local
, &remote
) <= 0 && pa_timeval_cmp(&remote
, &now
) <= 0) {
419 /* local and remote seem to have synchronized clocks */
421 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
)
422 i
.transport_usec
= pa_timeval_diff(&remote
, &local
);
424 i
.transport_usec
= pa_timeval_diff(&now
, &remote
);
426 i
.synchronized_clocks
= 1;
427 i
.timestamp
= remote
;
429 /* clocks are not synchronized, let's estimate latency then */
430 i
.transport_usec
= pa_timeval_diff(&now
, &local
)/2;
431 i
.synchronized_clocks
= 0;
433 pa_timeval_add(&i
.timestamp
, i
.transport_usec
);
436 if (o
->stream
->interpolate
) {
437 /* pa_log("new interpol data\n"); */
438 o
->stream
->ipol_timestamp
= i
.timestamp
;
439 o
->stream
->ipol_usec
= pa_stream_get_time(o
->stream
, &i
);
440 o
->stream
->ipol_requested
= 0;
447 void (*cb
)(struct pa_stream
*s
, const struct pa_latency_info
*i
, void *userdata
) = o
->callback
;
448 cb(o
->stream
, p
, o
->userdata
);
452 pa_operation_done(o
);
453 pa_operation_unref(o
);
456 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
) {
458 struct pa_operation
*o
;
459 struct pa_tagstruct
*t
;
461 assert(s
&& s
->direction
!= PA_STREAM_UPLOAD
);
463 o
= pa_operation_new(s
->context
, s
);
466 o
->userdata
= userdata
;
468 t
= pa_tagstruct_new(NULL
, 0);
470 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_GET_PLAYBACK_LATENCY
: PA_COMMAND_GET_RECORD_LATENCY
);
471 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
472 pa_tagstruct_putu32(t
, s
->channel
);
474 pa_gettimeofday(&now
);
475 pa_tagstruct_put_timeval(t
, &now
);
476 pa_tagstruct_putu64(t
, s
->counter
);
478 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
479 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, stream_get_latency_info_callback
, o
);
481 return pa_operation_ref(o
);
484 void pa_stream_disconnect_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
485 struct pa_stream
*s
= userdata
;
486 assert(pd
&& s
&& s
->ref
>= 1);
490 if (command
!= PA_COMMAND_REPLY
) {
491 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
494 pa_stream_set_state(s
, PA_STREAM_FAILED
);
496 } else if (!pa_tagstruct_eof(t
)) {
497 pa_context_fail(s
->context
, PA_ERROR_PROTOCOL
);
501 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
507 void pa_stream_disconnect(struct pa_stream
*s
) {
508 struct pa_tagstruct
*t
;
510 assert(s
&& s
->ref
>= 1);
512 if (!s
->channel_valid
|| !s
->context
->state
== PA_CONTEXT_READY
)
517 t
= pa_tagstruct_new(NULL
, 0);
520 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
521 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
));
522 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
523 pa_tagstruct_putu32(t
, s
->channel
);
524 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
525 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
);
530 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
) {
531 assert(s
&& s
->ref
>= 1);
532 s
->read_callback
= cb
;
533 s
->read_userdata
= userdata
;
536 void pa_stream_set_write_callback(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*p
, size_t length
, void *userdata
), void *userdata
) {
537 assert(s
&& s
->ref
>= 1);
538 s
->write_callback
= cb
;
539 s
->write_userdata
= userdata
;
542 void pa_stream_set_state_callback(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, void *userdata
), void *userdata
) {
543 assert(s
&& s
->ref
>= 1);
544 s
->state_callback
= cb
;
545 s
->state_userdata
= userdata
;
548 void pa_stream_simple_ack_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
549 struct pa_operation
*o
= userdata
;
551 assert(pd
&& o
&& o
->context
&& o
->ref
>= 1);
553 if (command
!= PA_COMMAND_REPLY
) {
554 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
558 } else if (!pa_tagstruct_eof(t
)) {
559 pa_context_fail(o
->context
, PA_ERROR_PROTOCOL
);
564 void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
) = o
->callback
;
565 cb(o
->stream
, success
, o
->userdata
);
569 pa_operation_done(o
);
570 pa_operation_unref(o
);
573 struct pa_operation
* pa_stream_cork(struct pa_stream
*s
, int b
, void (*cb
) (struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
574 struct pa_operation
*o
;
575 struct pa_tagstruct
*t
;
577 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
);
579 if (s
->interpolate
) {
582 s
->ipol_usec
= pa_stream_get_interpolated_time(s
);
583 else if (s
->corked
&& !b
)
585 pa_gettimeofday(&s
->ipol_timestamp
);
590 o
= pa_operation_new(s
->context
, s
);
593 o
->userdata
= userdata
;
595 t
= pa_tagstruct_new(NULL
, 0);
597 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CORK_PLAYBACK_STREAM
: PA_COMMAND_CORK_RECORD_STREAM
);
598 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
599 pa_tagstruct_putu32(t
, s
->channel
);
600 pa_tagstruct_put_boolean(t
, !!b
);
601 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
602 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
604 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
606 return pa_operation_ref(o
);
609 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
) {
610 struct pa_tagstruct
*t
;
611 struct pa_operation
*o
;
613 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
);
615 o
= pa_operation_new(s
->context
, s
);
617 o
->userdata
= userdata
;
619 t
= pa_tagstruct_new(NULL
, 0);
620 pa_tagstruct_putu32(t
, command
);
621 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
622 pa_tagstruct_putu32(t
, s
->channel
);
623 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
624 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
626 return pa_operation_ref(o
);
629 struct pa_operation
* pa_stream_flush(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
630 struct pa_operation
*o
;
631 o
= pa_stream_send_simple_command(s
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_FLUSH_PLAYBACK_STREAM
: PA_COMMAND_FLUSH_RECORD_STREAM
, cb
, userdata
);
632 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
636 struct pa_operation
* pa_stream_prebuf(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
637 struct pa_operation
*o
;
638 o
= pa_stream_send_simple_command(s
, PA_COMMAND_PREBUF_PLAYBACK_STREAM
, cb
, userdata
);
639 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
643 struct pa_operation
* pa_stream_trigger(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
644 struct pa_operation
*o
;
645 o
= pa_stream_send_simple_command(s
, PA_COMMAND_TRIGGER_PLAYBACK_STREAM
, cb
, userdata
);
646 pa_operation_unref(pa_stream_get_latency_info(s
, NULL
, NULL
));
650 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
) {
651 struct pa_operation
*o
;
652 struct pa_tagstruct
*t
;
654 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
&& name
&& s
->direction
!= PA_STREAM_UPLOAD
);
656 o
= pa_operation_new(s
->context
, s
);
659 o
->userdata
= userdata
;
661 t
= pa_tagstruct_new(NULL
, 0);
663 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_NAME
: PA_COMMAND_SET_PLAYBACK_STREAM_NAME
);
664 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
665 pa_tagstruct_putu32(t
, s
->channel
);
666 pa_tagstruct_puts(t
, name
);
667 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
668 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
670 return pa_operation_ref(o
);
673 uint64_t pa_stream_get_counter(struct pa_stream
*s
) {
678 pa_usec_t
pa_stream_get_time(struct pa_stream
*s
, const struct pa_latency_info
*i
) {
682 usec
= pa_bytes_to_usec(i
->counter
, &s
->sample_spec
);
685 if (s
->direction
== PA_STREAM_PLAYBACK
) {
686 pa_usec_t latency
= i
->transport_usec
+ i
->buffer_usec
+ i
->sink_usec
;
692 } else if (s
->direction
== PA_STREAM_RECORD
) {
693 usec
+= i
->source_usec
+ i
->buffer_usec
+ i
->transport_usec
;
695 if (usec
> i
->sink_usec
)
696 usec
-= i
->sink_usec
;
702 if (usec
< s
->previous_time
)
703 usec
= s
->previous_time
;
705 s
->previous_time
= usec
;
710 static pa_usec_t
time_counter_diff(struct pa_stream
*s
, pa_usec_t t
, pa_usec_t c
, int *negative
) {
717 if (s
->direction
== PA_STREAM_RECORD
) {
728 pa_usec_t
pa_stream_get_latency(struct pa_stream
*s
, const struct pa_latency_info
*i
, int *negative
) {
732 t
= pa_stream_get_time(s
, i
);
733 c
= pa_bytes_to_usec(s
->counter
, &s
->sample_spec
);
735 return time_counter_diff(s
, t
, c
, negative
);
738 const struct pa_sample_spec
* pa_stream_get_sample_spec(struct pa_stream
*s
) {
740 return &s
->sample_spec
;
743 void pa_stream_trash_ipol(struct pa_stream
*s
) {
749 memset(&s
->ipol_timestamp
, 0, sizeof(s
->ipol_timestamp
));
753 pa_usec_t
pa_stream_get_interpolated_time(struct pa_stream
*s
) {
755 assert(s
&& s
->interpolate
);
760 if (s
->ipol_timestamp
.tv_sec
== 0)
763 usec
= s
->ipol_usec
+ pa_timeval_age(&s
->ipol_timestamp
);
766 if (usec
< s
->previous_ipol_time
)
767 usec
= s
->previous_ipol_time
;
769 s
->previous_ipol_time
= usec
;
774 pa_usec_t
pa_stream_get_interpolated_latency(struct pa_stream
*s
, int *negative
) {
776 assert(s
&& s
->interpolate
);
778 t
= pa_stream_get_interpolated_time(s
);
779 c
= pa_bytes_to_usec(s
->counter
, &s
->sample_spec
);
780 return time_counter_diff(s
, t
, c
, negative
);