4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulse/def.h>
35 #include <pulse/timeval.h>
36 #include <pulse/xmalloc.h>
38 #include <pulsecore/pstream-util.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/hashmap.h>
44 #define LATENCY_IPOL_INTERVAL_USEC (100000L)
46 pa_stream *pa_stream_new(pa_context *c, const char *name, const pa_sample_spec *ss, const pa_channel_map *map) {
52 PA_CHECK_VALIDITY_RETURN_NULL(c, ss && pa_sample_spec_valid(ss), PA_ERR_INVALID);
53 PA_CHECK_VALIDITY_RETURN_NULL(c, !map || (pa_channel_map_valid(map) && map->channels == ss->channels), PA_ERR_INVALID);
55 s = pa_xnew(pa_stream, 1);
58 s->mainloop = c->mainloop;
60 s->read_callback = NULL;
61 s->read_userdata = NULL;
62 s->write_callback = NULL;
63 s->write_userdata = NULL;
64 s->state_callback = NULL;
65 s->state_userdata = NULL;
66 s->overflow_callback = NULL;
67 s->overflow_userdata = NULL;
68 s->underflow_callback = NULL;
69 s->underflow_userdata = NULL;
70 s->latency_update_callback = NULL;
71 s->latency_update_userdata = NULL;
73 s->direction = PA_STREAM_NODIRECTION;
74 s->name = pa_xstrdup(name);
79 s->channel_map = *map;
81 pa_channel_map_init_auto(&s->channel_map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
85 s->syncid = c->csyncid++;
86 s->device_index = PA_INVALID_INDEX;
87 s->requested_bytes = 0;
88 s->state = PA_STREAM_UNCONNECTED;
89 memset(&s->buffer_attr, 0, sizeof(s->buffer_attr));
91 s->peek_memchunk.index = 0;
92 s->peek_memchunk.length = 0;
93 s->peek_memchunk.memblock = NULL;
96 s->record_memblockq = NULL;
99 s->timing_info_valid = 0;
100 s->read_index_not_before = 0;
101 s->write_index_not_before = 0;
103 for (i = 0; i < PA_MAX_WRITE_INDEX_CORRECTIONS; i++)
104 s->write_index_corrections[i].valid = 0;
105 s->current_write_index_correction = 0;
109 s->cached_time_valid = 0;
111 s->auto_timing_update_event = NULL;
112 s->auto_timing_update_requested = 0;
114 /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
115 PA_LLIST_PREPEND(pa_stream, c->streams, s);
121 static void stream_free(pa_stream *s) {
122 assert(s && !s->context && !s->channel_valid);
124 if (s->auto_timing_update_event) {
126 s->mainloop->time_free(s->auto_timing_update_event);
129 if (s->peek_memchunk.memblock) {
131 pa_memblock_release(s->peek_memchunk.memblock);
132 pa_memblock_unref(s->peek_memchunk.memblock);
135 if (s->record_memblockq)
136 pa_memblockq_free(s->record_memblockq);
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) {
191 if (s->state_callback)
192 s->state_callback(s, s->state_userdata);
194 if ((st == PA_STREAM_FAILED || st == PA_STREAM_TERMINATED) && s->context) {
196 /* Detach from context */
199 /* Unref all operatio object that point to us */
200 for (o = s->context->operations; o; o = n) {
204 pa_operation_cancel(o);
207 /* Drop all outstanding replies for this stream */
208 if (s->context->pdispatch)
209 pa_pdispatch_unregister_reply(s->context->pdispatch, s);
211 if (s->channel_valid)
212 pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, NULL);
214 PA_LLIST_REMOVE(pa_stream, s->context->streams, s);
218 s->channel_valid = 0;
226 void pa_command_stream_killed(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
227 pa_context *c = userdata;
232 assert(command == PA_COMMAND_PLAYBACK_STREAM_KILLED || command == PA_COMMAND_RECORD_STREAM_KILLED);
238 if (pa_tagstruct_getu32(t, &channel) < 0 ||
239 !pa_tagstruct_eof(t)) {
240 pa_context_fail(c, PA_ERR_PROTOCOL);
244 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel)))
247 pa_context_set_error(c, PA_ERR_KILLED);
248 pa_stream_set_state(s, PA_STREAM_FAILED);
254 void pa_command_request(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
256 pa_context *c = userdata;
257 uint32_t bytes, channel;
260 assert(command == PA_COMMAND_REQUEST);
266 if (pa_tagstruct_getu32(t, &channel) < 0 ||
267 pa_tagstruct_getu32(t, &bytes) < 0 ||
268 !pa_tagstruct_eof(t)) {
269 pa_context_fail(c, PA_ERR_PROTOCOL);
273 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
276 if (s->state == PA_STREAM_READY) {
277 s->requested_bytes += bytes;
279 if (s->requested_bytes > 0 && s->write_callback)
280 s->write_callback(s, s->requested_bytes, s->write_userdata);
287 void pa_command_overflow_or_underflow(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
289 pa_context *c = userdata;
293 assert(command == PA_COMMAND_OVERFLOW || command == PA_COMMAND_UNDERFLOW);
299 if (pa_tagstruct_getu32(t, &channel) < 0 ||
300 !pa_tagstruct_eof(t)) {
301 pa_context_fail(c, PA_ERR_PROTOCOL);
305 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
308 if (s->state == PA_STREAM_READY) {
310 if (command == PA_COMMAND_OVERFLOW) {
311 if (s->overflow_callback)
312 s->overflow_callback(s, s->overflow_userdata);
313 } else if (command == PA_COMMAND_UNDERFLOW) {
314 if (s->underflow_callback)
315 s->underflow_callback(s, s->underflow_userdata);
323 static void request_auto_timing_update(pa_stream *s, int force) {
327 if (!(s->flags & PA_STREAM_AUTO_TIMING_UPDATE))
330 if (s->state == PA_STREAM_READY &&
331 (force || !s->auto_timing_update_requested)) {
334 /* pa_log("automatically requesting new timing data"); */
336 if ((o = pa_stream_update_timing_info(s, NULL, NULL))) {
337 pa_operation_unref(o);
338 s->auto_timing_update_requested = 1;
342 pa_gettimeofday(&next);
343 pa_timeval_add(&next, LATENCY_IPOL_INTERVAL_USEC);
344 s->mainloop->time_restart(s->auto_timing_update_event, &next);
347 static void invalidate_indexes(pa_stream *s, int r, int w) {
350 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
352 if (s->state != PA_STREAM_READY)
356 s->write_index_not_before = s->context->ctag;
358 if (s->timing_info_valid)
359 s->timing_info.write_index_corrupt = 1;
361 /* pa_log("write_index invalidated"); */
365 s->read_index_not_before = s->context->ctag;
367 if (s->timing_info_valid)
368 s->timing_info.read_index_corrupt = 1;
370 /* pa_log("read_index invalidated"); */
373 if ((s->direction == PA_STREAM_PLAYBACK && r) ||
374 (s->direction == PA_STREAM_RECORD && w))
375 s->cached_time_valid = 0;
377 request_auto_timing_update(s, 1);
380 static void auto_timing_update_callback(PA_GCC_UNUSED pa_mainloop_api *m, PA_GCC_UNUSED pa_time_event *e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
381 pa_stream *s = userdata;
383 /* pa_log("time event"); */
386 request_auto_timing_update(s, 0);
390 void pa_create_stream_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
391 pa_stream *s = userdata;
395 assert(s->state == PA_STREAM_CREATING);
399 if (command != PA_COMMAND_REPLY) {
400 if (pa_context_handle_error(s->context, command, t) < 0)
403 pa_stream_set_state(s, PA_STREAM_FAILED);
407 if (pa_tagstruct_getu32(t, &s->channel) < 0 ||
408 ((s->direction != PA_STREAM_UPLOAD) && pa_tagstruct_getu32(t, &s->device_index) < 0) ||
409 ((s->direction != PA_STREAM_RECORD) && pa_tagstruct_getu32(t, &s->requested_bytes) < 0)) {
410 pa_context_fail(s->context, PA_ERR_PROTOCOL);
414 if (pa_context_get_server_protocol_version(s->context) >= 9) {
415 if (s->direction == PA_STREAM_PLAYBACK) {
416 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
417 pa_tagstruct_getu32(t, &s->buffer_attr.tlength) < 0 ||
418 pa_tagstruct_getu32(t, &s->buffer_attr.prebuf) < 0 ||
419 pa_tagstruct_getu32(t, &s->buffer_attr.minreq) < 0) {
420 pa_context_fail(s->context, PA_ERR_PROTOCOL);
423 } else if (s->direction == PA_STREAM_RECORD) {
424 if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
425 pa_tagstruct_getu32(t, &s->buffer_attr.fragsize) < 0) {
426 pa_context_fail(s->context, PA_ERR_PROTOCOL);
432 if (!pa_tagstruct_eof(t)) {
433 pa_context_fail(s->context, PA_ERR_PROTOCOL);
437 if (s->direction == PA_STREAM_RECORD) {
438 assert(!s->record_memblockq);
440 s->record_memblockq = pa_memblockq_new(
442 s->buffer_attr.maxlength,
444 pa_frame_size(&s->sample_spec),
450 s->channel_valid = 1;
451 pa_dynarray_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, s->channel, s);
453 pa_stream_set_state(s, PA_STREAM_READY);
455 if (s->direction != PA_STREAM_UPLOAD &&
456 s->flags & PA_STREAM_AUTO_TIMING_UPDATE) {
459 pa_gettimeofday(&tv);
460 tv.tv_usec += LATENCY_IPOL_INTERVAL_USEC; /* every 100 ms */
462 assert(!s->auto_timing_update_event);
463 s->auto_timing_update_event = s->mainloop->time_new(s->mainloop, &tv, &auto_timing_update_callback, s);
465 request_auto_timing_update(s, 1);
468 if (s->requested_bytes > 0 && s->ref > 1 && s->write_callback)
469 s->write_callback(s, s->requested_bytes, s->write_userdata);
475 static int create_stream(
476 pa_stream_direction_t direction,
479 const pa_buffer_attr *attr,
480 pa_stream_flags_t flags,
481 const pa_cvolume *volume,
482 pa_stream *sync_stream) {
490 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
491 PA_CHECK_VALIDITY(s->context, !(flags & ~((direction != PA_STREAM_UPLOAD ?
492 PA_STREAM_START_CORKED|
493 PA_STREAM_INTERPOLATE_TIMING|
494 PA_STREAM_NOT_MONOTONOUS|
495 PA_STREAM_AUTO_TIMING_UPDATE : 0))), PA_ERR_INVALID);
496 PA_CHECK_VALIDITY(s->context, !volume || volume->channels == s->sample_spec.channels, PA_ERR_INVALID);
497 PA_CHECK_VALIDITY(s->context, !sync_stream || (direction == PA_STREAM_PLAYBACK && sync_stream->direction == PA_STREAM_PLAYBACK), PA_ERR_INVALID);
501 s->direction = direction;
505 s->syncid = sync_stream->syncid;
508 s->buffer_attr = *attr;
511 s->buffer_attr.tlength = pa_bytes_per_second(&s->sample_spec)/2;
512 s->buffer_attr.maxlength = (s->buffer_attr.tlength*3)/2;
513 s->buffer_attr.minreq = s->buffer_attr.tlength/100;
514 s->buffer_attr.prebuf = s->buffer_attr.tlength - s->buffer_attr.minreq;
515 s->buffer_attr.fragsize = s->buffer_attr.tlength/100;
519 dev = s->direction == PA_STREAM_PLAYBACK ? s->context->conf->default_sink : s->context->conf->default_source;
521 t = pa_tagstruct_command(
523 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM,
528 PA_TAG_STRING, s->name,
529 PA_TAG_SAMPLE_SPEC, &s->sample_spec,
530 PA_TAG_CHANNEL_MAP, &s->channel_map,
531 PA_TAG_U32, PA_INVALID_INDEX,
533 PA_TAG_U32, s->buffer_attr.maxlength,
534 PA_TAG_BOOLEAN, !!(flags & PA_STREAM_START_CORKED),
537 if (s->direction == PA_STREAM_PLAYBACK) {
542 PA_TAG_U32, s->buffer_attr.tlength,
543 PA_TAG_U32, s->buffer_attr.prebuf,
544 PA_TAG_U32, s->buffer_attr.minreq,
545 PA_TAG_U32, s->syncid,
549 volume = pa_cvolume_reset(&cv, s->sample_spec.channels);
551 pa_tagstruct_put_cvolume(t, volume);
553 pa_tagstruct_putu32(t, s->buffer_attr.fragsize);
555 pa_pstream_send_tagstruct(s->context->pstream, t);
556 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s, NULL);
558 pa_stream_set_state(s, PA_STREAM_CREATING);
564 int pa_stream_connect_playback(
567 const pa_buffer_attr *attr,
568 pa_stream_flags_t flags,
570 pa_stream *sync_stream) {
575 return create_stream(PA_STREAM_PLAYBACK, s, dev, attr, flags, volume, sync_stream);
578 int pa_stream_connect_record(
581 const pa_buffer_attr *attr,
582 pa_stream_flags_t flags) {
587 return create_stream(PA_STREAM_RECORD, s, dev, attr, flags, NULL, NULL);
594 void (*free_cb)(void *p),
596 pa_seek_mode_t seek) {
604 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
605 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || s->direction == PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
606 PA_CHECK_VALIDITY(s->context, seek <= PA_SEEK_RELATIVE_END, PA_ERR_INVALID);
607 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || (seek == PA_SEEK_RELATIVE && offset == 0), PA_ERR_INVALID);
613 chunk.memblock = pa_memblock_new_user(s->context->mempool, (void*) data, length, free_cb, 1);
616 chunk.memblock = pa_memblock_new(s->context->mempool, length);
617 tdata = pa_memblock_acquire(chunk.memblock);
618 memcpy(tdata, data, length);
619 pa_memblock_release(chunk.memblock);
623 chunk.length = length;
625 pa_pstream_send_memblock(s->context->pstream, s->channel, offset, seek, &chunk);
626 pa_memblock_unref(chunk.memblock);
628 if (length < s->requested_bytes)
629 s->requested_bytes -= length;
631 s->requested_bytes = 0;
633 if (s->direction == PA_STREAM_PLAYBACK) {
635 /* Update latency request correction */
636 if (s->write_index_corrections[s->current_write_index_correction].valid) {
638 if (seek == PA_SEEK_ABSOLUTE) {
639 s->write_index_corrections[s->current_write_index_correction].corrupt = 0;
640 s->write_index_corrections[s->current_write_index_correction].absolute = 1;
641 s->write_index_corrections[s->current_write_index_correction].value = offset + length;
642 } else if (seek == PA_SEEK_RELATIVE) {
643 if (!s->write_index_corrections[s->current_write_index_correction].corrupt)
644 s->write_index_corrections[s->current_write_index_correction].value += offset + length;
646 s->write_index_corrections[s->current_write_index_correction].corrupt = 1;
649 /* Update the write index in the already available latency data */
650 if (s->timing_info_valid) {
652 if (seek == PA_SEEK_ABSOLUTE) {
653 s->timing_info.write_index_corrupt = 0;
654 s->timing_info.write_index = offset + length;
655 } else if (seek == PA_SEEK_RELATIVE) {
656 if (!s->timing_info.write_index_corrupt)
657 s->timing_info.write_index += offset + length;
659 s->timing_info.write_index_corrupt = 1;
662 if (!s->timing_info_valid || s->timing_info.write_index_corrupt)
663 request_auto_timing_update(s, 1);
669 int pa_stream_peek(pa_stream *s, const void **data, size_t *length) {
675 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
676 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
678 if (!s->peek_memchunk.memblock) {
680 if (pa_memblockq_peek(s->record_memblockq, &s->peek_memchunk) < 0) {
686 s->peek_data = pa_memblock_acquire(s->peek_memchunk.memblock);
689 assert(s->peek_data);
690 *data = (uint8_t*) s->peek_data + s->peek_memchunk.index;
691 *length = s->peek_memchunk.length;
695 int pa_stream_drop(pa_stream *s) {
699 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
700 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
701 PA_CHECK_VALIDITY(s->context, s->peek_memchunk.memblock, PA_ERR_BADSTATE);
703 pa_memblockq_drop(s->record_memblockq, &s->peek_memchunk, s->peek_memchunk.length);
705 /* Fix the simulated local read index */
706 if (s->timing_info_valid && !s->timing_info.read_index_corrupt)
707 s->timing_info.read_index += s->peek_memchunk.length;
709 assert(s->peek_data);
710 pa_memblock_release(s->peek_memchunk.memblock);
711 pa_memblock_unref(s->peek_memchunk.memblock);
712 s->peek_memchunk.length = 0;
713 s->peek_memchunk.index = 0;
714 s->peek_memchunk.memblock = NULL;
719 size_t pa_stream_writable_size(pa_stream *s) {
723 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
724 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
726 return s->requested_bytes;
729 size_t pa_stream_readable_size(pa_stream *s) {
733 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
734 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
736 return pa_memblockq_get_length(s->record_memblockq);
739 pa_operation * pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
747 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
748 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
750 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
752 t = pa_tagstruct_command(s->context, PA_COMMAND_DRAIN_PLAYBACK_STREAM, &tag);
753 pa_tagstruct_putu32(t, s->channel);
754 pa_pstream_send_tagstruct(s->context->pstream, t);
755 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
760 static void stream_get_timing_info_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
761 pa_operation *o = userdata;
762 struct timeval local, remote, now;
768 if (!o->context || !o->stream)
771 i = &o->stream->timing_info;
773 /* pa_log("pre corrupt w:%u r:%u\n", !o->stream->timing_info_valid || i->write_index_corrupt,!o->stream->timing_info_valid || i->read_index_corrupt); */
775 o->stream->timing_info_valid = 0;
776 i->write_index_corrupt = 0;
777 i->read_index_corrupt = 0;
779 /* pa_log("timing update %u\n", tag); */
781 if (command != PA_COMMAND_REPLY) {
782 if (pa_context_handle_error(o->context, command, t) < 0)
785 } else if (pa_tagstruct_get_usec(t, &i->sink_usec) < 0 ||
786 pa_tagstruct_get_usec(t, &i->source_usec) < 0 ||
787 pa_tagstruct_get_boolean(t, &i->playing) < 0 ||
788 pa_tagstruct_get_timeval(t, &local) < 0 ||
789 pa_tagstruct_get_timeval(t, &remote) < 0 ||
790 pa_tagstruct_gets64(t, &i->write_index) < 0 ||
791 pa_tagstruct_gets64(t, &i->read_index) < 0 ||
792 !pa_tagstruct_eof(t)) {
793 pa_context_fail(o->context, PA_ERR_PROTOCOL);
797 o->stream->timing_info_valid = 1;
799 pa_gettimeofday(&now);
801 /* Calculcate timestamps */
802 if (pa_timeval_cmp(&local, &remote) <= 0 && pa_timeval_cmp(&remote, &now) <= 0) {
803 /* local and remote seem to have synchronized clocks */
805 if (o->stream->direction == PA_STREAM_PLAYBACK)
806 i->transport_usec = pa_timeval_diff(&remote, &local);
808 i->transport_usec = pa_timeval_diff(&now, &remote);
810 i->synchronized_clocks = 1;
811 i->timestamp = remote;
813 /* clocks are not synchronized, let's estimate latency then */
814 i->transport_usec = pa_timeval_diff(&now, &local)/2;
815 i->synchronized_clocks = 0;
816 i->timestamp = local;
817 pa_timeval_add(&i->timestamp, i->transport_usec);
820 /* Invalidate read and write indexes if necessary */
821 if (tag < o->stream->read_index_not_before)
822 i->read_index_corrupt = 1;
824 if (tag < o->stream->write_index_not_before)
825 i->write_index_corrupt = 1;
827 if (o->stream->direction == PA_STREAM_PLAYBACK) {
828 /* Write index correction */
833 /* Go through the saved correction values and add up the total correction.*/
835 for (n = 0, j = o->stream->current_write_index_correction+1;
836 n < PA_MAX_WRITE_INDEX_CORRECTIONS;
837 n++, j = (j + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS) {
839 /* Step over invalid data or out-of-date data */
840 if (!o->stream->write_index_corrections[j].valid ||
841 o->stream->write_index_corrections[j].tag < ctag)
844 /* Make sure that everything is in order */
845 ctag = o->stream->write_index_corrections[j].tag+1;
847 /* Now fix the write index */
848 if (o->stream->write_index_corrections[j].corrupt) {
849 /* A corrupting seek was made */
851 i->write_index_corrupt = 1;
852 } else if (o->stream->write_index_corrections[j].absolute) {
853 /* An absolute seek was made */
854 i->write_index = o->stream->write_index_corrections[j].value;
855 i->write_index_corrupt = 0;
856 } else if (!i->write_index_corrupt) {
857 /* A relative seek was made */
858 i->write_index += o->stream->write_index_corrections[j].value;
863 if (o->stream->direction == PA_STREAM_RECORD) {
864 /* Read index correction */
866 if (!i->read_index_corrupt)
867 i->read_index -= pa_memblockq_get_length(o->stream->record_memblockq);
870 o->stream->cached_time_valid = 0;
873 o->stream->auto_timing_update_requested = 0;
874 /* pa_log("post corrupt w:%u r:%u\n", i->write_index_corrupt || !o->stream->timing_info_valid, i->read_index_corrupt || !o->stream->timing_info_valid); */
876 /* Clear old correction entries */
877 if (o->stream->direction == PA_STREAM_PLAYBACK) {
880 for (n = 0; n < PA_MAX_WRITE_INDEX_CORRECTIONS; n++) {
881 if (!o->stream->write_index_corrections[n].valid)
884 if (o->stream->write_index_corrections[n].tag <= tag)
885 o->stream->write_index_corrections[n].valid = 0;
889 if (o->stream->latency_update_callback)
890 o->stream->latency_update_callback(o->stream, o->stream->latency_update_userdata);
892 if (o->callback && o->stream && o->stream->state == PA_STREAM_READY) {
893 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
894 cb(o->stream, o->stream->timing_info_valid, o->userdata);
899 pa_operation_done(o);
900 pa_operation_unref(o);
903 pa_operation* pa_stream_update_timing_info(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
913 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
914 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
916 if (s->direction == PA_STREAM_PLAYBACK) {
917 /* Find a place to store the write_index correction data for this entry */
918 cidx = (s->current_write_index_correction + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS;
920 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
921 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !s->write_index_corrections[cidx].valid, PA_ERR_INTERNAL);
923 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
925 t = pa_tagstruct_command(
927 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_GET_PLAYBACK_LATENCY : PA_COMMAND_GET_RECORD_LATENCY,
929 pa_tagstruct_putu32(t, s->channel);
930 pa_tagstruct_put_timeval(t, pa_gettimeofday(&now));
932 pa_pstream_send_tagstruct(s->context->pstream, t);
933 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_timing_info_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
935 if (s->direction == PA_STREAM_PLAYBACK) {
936 /* Fill in initial correction data */
937 o->stream->current_write_index_correction = cidx;
938 o->stream->write_index_corrections[cidx].valid = 1;
939 o->stream->write_index_corrections[cidx].tag = tag;
940 o->stream->write_index_corrections[cidx].absolute = 0;
941 o->stream->write_index_corrections[cidx].value = 0;
942 o->stream->write_index_corrections[cidx].corrupt = 0;
945 /* pa_log("requesting update %u\n", tag); */
950 void pa_stream_disconnect_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
951 pa_stream *s = userdata;
959 if (command != PA_COMMAND_REPLY) {
960 if (pa_context_handle_error(s->context, command, t) < 0)
963 pa_stream_set_state(s, PA_STREAM_FAILED);
965 } else if (!pa_tagstruct_eof(t)) {
966 pa_context_fail(s->context, PA_ERR_PROTOCOL);
970 pa_stream_set_state(s, PA_STREAM_TERMINATED);
976 int pa_stream_disconnect(pa_stream *s) {
983 PA_CHECK_VALIDITY(s->context, s->channel_valid, PA_ERR_BADSTATE);
984 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
988 t = pa_tagstruct_command(
990 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_DELETE_PLAYBACK_STREAM :
991 (s->direction == PA_STREAM_RECORD ? PA_COMMAND_DELETE_RECORD_STREAM : PA_COMMAND_DELETE_UPLOAD_STREAM),
993 pa_tagstruct_putu32(t, s->channel);
994 pa_pstream_send_tagstruct(s->context->pstream, t);
995 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s, NULL);
1001 void pa_stream_set_read_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1003 assert(s->ref >= 1);
1005 s->read_callback = cb;
1006 s->read_userdata = userdata;
1009 void pa_stream_set_write_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1011 assert(s->ref >= 1);
1013 s->write_callback = cb;
1014 s->write_userdata = userdata;
1017 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1019 assert(s->ref >= 1);
1021 s->state_callback = cb;
1022 s->state_userdata = userdata;
1025 void pa_stream_set_overflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1027 assert(s->ref >= 1);
1029 s->overflow_callback = cb;
1030 s->overflow_userdata = userdata;
1033 void pa_stream_set_underflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1035 assert(s->ref >= 1);
1037 s->underflow_callback = cb;
1038 s->underflow_userdata = userdata;
1041 void pa_stream_set_latency_update_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1043 assert(s->ref >= 1);
1045 s->latency_update_callback = cb;
1046 s->latency_update_userdata = userdata;
1049 void pa_stream_simple_ack_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
1050 pa_operation *o = userdata;
1055 assert(o->ref >= 1);
1060 if (command != PA_COMMAND_REPLY) {
1061 if (pa_context_handle_error(o->context, command, t) < 0)
1065 } else if (!pa_tagstruct_eof(t)) {
1066 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1071 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1072 cb(o->stream, success, o->userdata);
1076 pa_operation_done(o);
1077 pa_operation_unref(o);
1080 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata) {
1086 assert(s->ref >= 1);
1088 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1089 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1093 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1095 t = pa_tagstruct_command(
1097 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CORK_PLAYBACK_STREAM : PA_COMMAND_CORK_RECORD_STREAM,
1099 pa_tagstruct_putu32(t, s->channel);
1100 pa_tagstruct_put_boolean(t, !!b);
1101 pa_pstream_send_tagstruct(s->context->pstream, t);
1102 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
1104 if (s->direction == PA_STREAM_PLAYBACK)
1105 invalidate_indexes(s, 1, 0);
1110 static pa_operation* stream_send_simple_command(pa_stream *s, uint32_t command, pa_stream_success_cb_t cb, void *userdata) {
1116 assert(s->ref >= 1);
1118 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1120 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1122 t = pa_tagstruct_command(s->context, command, &tag);
1123 pa_tagstruct_putu32(t, s->channel);
1124 pa_pstream_send_tagstruct(s->context->pstream, t);
1125 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
1130 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1133 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1135 if ((o = stream_send_simple_command(s, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_FLUSH_PLAYBACK_STREAM : PA_COMMAND_FLUSH_RECORD_STREAM, cb, userdata))) {
1137 if (s->direction == PA_STREAM_PLAYBACK) {
1138 if (s->write_index_corrections[s->current_write_index_correction].valid)
1139 s->write_index_corrections[s->current_write_index_correction].corrupt = 1;
1141 if (s->timing_info_valid)
1142 s->timing_info.write_index_corrupt = 1;
1144 if (s->buffer_attr.prebuf > 0)
1145 invalidate_indexes(s, 1, 0);
1147 request_auto_timing_update(s, 1);
1149 invalidate_indexes(s, 0, 1);
1155 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1158 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1159 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1161 if ((o = stream_send_simple_command(s, PA_COMMAND_PREBUF_PLAYBACK_STREAM, cb, userdata)))
1162 invalidate_indexes(s, 1, 0);
1167 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1170 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1171 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1173 if ((o = stream_send_simple_command(s, PA_COMMAND_TRIGGER_PLAYBACK_STREAM, cb, userdata)))
1174 invalidate_indexes(s, 1, 0);
1179 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata) {
1185 assert(s->ref >= 1);
1188 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1189 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1191 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1193 t = pa_tagstruct_command(
1195 s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_NAME : PA_COMMAND_SET_PLAYBACK_STREAM_NAME,
1197 pa_tagstruct_putu32(t, s->channel);
1198 pa_tagstruct_puts(t, name);
1199 pa_pstream_send_tagstruct(s->context->pstream, t);
1200 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
1205 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec) {
1209 assert(s->ref >= 1);
1211 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1212 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1213 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1214 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1215 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1217 if (s->cached_time_valid)
1218 /* We alredy calculated the time value for this timing info, so let's reuse it */
1219 usec = s->cached_time;
1221 if (s->direction == PA_STREAM_PLAYBACK) {
1222 /* The last byte that was written into the output device
1223 * had this time value associated */
1224 usec = pa_bytes_to_usec(s->timing_info.read_index < 0 ? 0 : (uint64_t) s->timing_info.read_index, &s->sample_spec);
1227 /* Because the latency info took a little time to come
1228 * to us, we assume that the real output time is actually
1230 usec += s->timing_info.transport_usec;
1232 /* However, the output device usually maintains a buffer
1233 too, hence the real sample currently played is a little
1235 if (s->timing_info.sink_usec >= usec)
1238 usec -= s->timing_info.sink_usec;
1241 } else if (s->direction == PA_STREAM_RECORD) {
1242 /* The last byte written into the server side queue had
1243 * this time value associated */
1244 usec = pa_bytes_to_usec(s->timing_info.write_index < 0 ? 0 : (uint64_t) s->timing_info.write_index, &s->sample_spec);
1247 /* Add transport latency */
1248 usec += s->timing_info.transport_usec;
1250 /* Add latency of data in device buffer */
1251 usec += s->timing_info.source_usec;
1253 /* If this is a monitor source, we need to correct the
1254 * time by the playback device buffer */
1255 if (s->timing_info.sink_usec >= usec)
1258 usec -= s->timing_info.sink_usec;
1262 s->cached_time = usec;
1263 s->cached_time_valid = 1;
1266 /* Interpolate if requested */
1267 if (s->flags & PA_STREAM_INTERPOLATE_TIMING) {
1269 /* We just add the time that passed since the latency info was
1271 if (!s->corked && s->timing_info.playing) {
1273 usec += pa_timeval_diff(pa_gettimeofday(&now), &s->timing_info.timestamp);
1277 /* Make sure the time runs monotonically */
1278 if (!(s->flags & PA_STREAM_NOT_MONOTONOUS)) {
1279 if (usec < s->previous_time)
1280 usec = s->previous_time;
1282 s->previous_time = usec;
1291 static pa_usec_t time_counter_diff(pa_stream *s, pa_usec_t a, pa_usec_t b, int *negative) {
1293 assert(s->ref >= 1);
1301 if (negative && s->direction == PA_STREAM_RECORD) {
1309 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative) {
1315 assert(s->ref >= 1);
1318 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1319 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1320 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1321 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1322 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1324 if ((r = pa_stream_get_time(s, &t)) < 0)
1327 if (s->direction == PA_STREAM_PLAYBACK)
1328 cindex = s->timing_info.write_index;
1330 cindex = s->timing_info.read_index;
1335 c = pa_bytes_to_usec(cindex, &s->sample_spec);
1337 if (s->direction == PA_STREAM_PLAYBACK)
1338 *r_usec = time_counter_diff(s, c, t, negative);
1340 *r_usec = time_counter_diff(s, t, c, negative);
1345 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s) {
1347 assert(s->ref >= 1);
1349 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1350 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1351 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->timing_info_valid, PA_ERR_BADSTATE);
1353 return &s->timing_info;
1356 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s) {
1358 assert(s->ref >= 1);
1360 return &s->sample_spec;
1363 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s) {
1365 assert(s->ref >= 1);
1367 return &s->channel_map;
1370 const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s) {
1372 assert(s->ref >= 1);
1374 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1375 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1376 PA_CHECK_VALIDITY_RETURN_NULL(s->context,
1377 pa_context_get_server_protocol_version(s->context) >= 9, PA_ERR_NODATA);
1379 return &s->buffer_attr;