Merge commit 'origin/master-tx'
[profile/ivi/pulseaudio.git] / src / pulse / stream.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2004-2006 Lennart Poettering
5   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7   PulseAudio is free software; you can redistribute it and/or modify
8   it under the terms of the GNU Lesser General Public License as published
9   by the Free Software Foundation; either version 2 of the License,
10   or (at your option) any later version.
11
12   PulseAudio is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   General Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public License
18   along with PulseAudio; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20   USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <pulse/def.h>
32 #include <pulse/timeval.h>
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/pstream-util.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/hashmap.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/rtclock.h>
40
41 #include "internal.h"
42
43 #define LATENCY_IPOL_INTERVAL_USEC (333*PA_USEC_PER_MSEC)
44
45 #define SMOOTHER_ADJUST_TIME (1000*PA_USEC_PER_MSEC)
46 #define SMOOTHER_HISTORY_TIME (5000*PA_USEC_PER_MSEC)
47 #define SMOOTHER_MIN_HISTORY (4)
48
49 pa_stream *pa_stream_new(pa_context *c, const char *name, const pa_sample_spec *ss, const pa_channel_map *map) {
50     return pa_stream_new_with_proplist(c, name, ss, map, NULL);
51 }
52
53 static void reset_callbacks(pa_stream *s) {
54     s->read_callback = NULL;
55     s->read_userdata = NULL;
56     s->write_callback = NULL;
57     s->write_userdata = NULL;
58     s->state_callback = NULL;
59     s->state_userdata = NULL;
60     s->overflow_callback = NULL;
61     s->overflow_userdata = NULL;
62     s->underflow_callback = NULL;
63     s->underflow_userdata = NULL;
64     s->latency_update_callback = NULL;
65     s->latency_update_userdata = NULL;
66     s->moved_callback = NULL;
67     s->moved_userdata = NULL;
68     s->suspended_callback = NULL;
69     s->suspended_userdata = NULL;
70     s->started_callback = NULL;
71     s->started_userdata = NULL;
72 }
73
74 pa_stream *pa_stream_new_with_proplist(
75         pa_context *c,
76         const char *name,
77         const pa_sample_spec *ss,
78         const pa_channel_map *map,
79         pa_proplist *p) {
80
81     pa_stream *s;
82     int i;
83     pa_channel_map tmap;
84
85     pa_assert(c);
86     pa_assert(PA_REFCNT_VALUE(c) >= 1);
87
88     PA_CHECK_VALIDITY_RETURN_NULL(c, ss && pa_sample_spec_valid(ss), PA_ERR_INVALID);
89     PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 12 || (ss->format != PA_SAMPLE_S32LE && ss->format != PA_SAMPLE_S32BE), PA_ERR_NOTSUPPORTED);
90     PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 15 || (ss->format != PA_SAMPLE_S24LE && ss->format != PA_SAMPLE_S24BE), PA_ERR_NOTSUPPORTED);
91     PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 15 || (ss->format != PA_SAMPLE_S24_32LE && ss->format != PA_SAMPLE_S24_32BE), PA_ERR_NOTSUPPORTED);
92     PA_CHECK_VALIDITY_RETURN_NULL(c, !map || (pa_channel_map_valid(map) && map->channels == ss->channels), PA_ERR_INVALID);
93     PA_CHECK_VALIDITY_RETURN_NULL(c, name || (p && pa_proplist_contains(p, PA_PROP_MEDIA_NAME)), PA_ERR_INVALID);
94
95     if (!map)
96         PA_CHECK_VALIDITY_RETURN_NULL(c, map = pa_channel_map_init_auto(&tmap, ss->channels, PA_CHANNEL_MAP_DEFAULT), PA_ERR_INVALID);
97
98     s = pa_xnew(pa_stream, 1);
99     PA_REFCNT_INIT(s);
100     s->context = c;
101     s->mainloop = c->mainloop;
102
103     s->direction = PA_STREAM_NODIRECTION;
104     s->state = PA_STREAM_UNCONNECTED;
105     s->flags = 0;
106
107     s->sample_spec = *ss;
108     s->channel_map = *map;
109
110     s->direct_on_input = PA_INVALID_INDEX;
111
112     s->proplist = p ? pa_proplist_copy(p) : pa_proplist_new();
113     if (name)
114         pa_proplist_sets(s->proplist, PA_PROP_MEDIA_NAME, name);
115
116     s->channel = 0;
117     s->channel_valid = FALSE;
118     s->syncid = c->csyncid++;
119     s->stream_index = PA_INVALID_INDEX;
120
121     s->requested_bytes = 0;
122     memset(&s->buffer_attr, 0, sizeof(s->buffer_attr));
123
124     /* We initialize der target length here, so that if the user
125      * passes no explicit buffering metrics the default is similar to
126      * what older PA versions provided. */
127
128     s->buffer_attr.maxlength = (uint32_t) -1;
129     s->buffer_attr.tlength = (uint32_t) pa_usec_to_bytes(250*PA_USEC_PER_MSEC, ss); /* 250ms of buffering */
130     s->buffer_attr.minreq = (uint32_t) -1;
131     s->buffer_attr.prebuf = (uint32_t) -1;
132     s->buffer_attr.fragsize = (uint32_t) -1;
133
134     s->device_index = PA_INVALID_INDEX;
135     s->device_name = NULL;
136     s->suspended = FALSE;
137
138     pa_memchunk_reset(&s->peek_memchunk);
139     s->peek_data = NULL;
140
141     s->record_memblockq = NULL;
142
143     s->corked = FALSE;
144
145     memset(&s->timing_info, 0, sizeof(s->timing_info));
146     s->timing_info_valid = FALSE;
147
148     s->previous_time = 0;
149
150     s->read_index_not_before = 0;
151     s->write_index_not_before = 0;
152     for (i = 0; i < PA_MAX_WRITE_INDEX_CORRECTIONS; i++)
153         s->write_index_corrections[i].valid = 0;
154     s->current_write_index_correction = 0;
155
156     s->auto_timing_update_event = NULL;
157     s->auto_timing_update_requested = FALSE;
158
159     reset_callbacks(s);
160
161     s->smoother = NULL;
162
163     /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
164     PA_LLIST_PREPEND(pa_stream, c->streams, s);
165     pa_stream_ref(s);
166
167     return s;
168 }
169
170 static void stream_unlink(pa_stream *s) {
171     pa_operation *o, *n;
172     pa_assert(s);
173
174     if (!s->context)
175         return;
176
177     /* Detach from context */
178
179     /* Unref all operatio object that point to us */
180     for (o = s->context->operations; o; o = n) {
181         n = o->next;
182
183         if (o->stream == s)
184             pa_operation_cancel(o);
185     }
186
187     /* Drop all outstanding replies for this stream */
188     if (s->context->pdispatch)
189         pa_pdispatch_unregister_reply(s->context->pdispatch, s);
190
191     if (s->channel_valid) {
192         pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, NULL);
193         s->channel = 0;
194         s->channel_valid = FALSE;
195     }
196
197     PA_LLIST_REMOVE(pa_stream, s->context->streams, s);
198     pa_stream_unref(s);
199
200     s->context = NULL;
201
202     if (s->auto_timing_update_event) {
203         pa_assert(s->mainloop);
204         s->mainloop->time_free(s->auto_timing_update_event);
205     }
206
207     reset_callbacks(s);
208 }
209
210 static void stream_free(pa_stream *s) {
211     pa_assert(s);
212
213     stream_unlink(s);
214
215     if (s->peek_memchunk.memblock) {
216         if (s->peek_data)
217             pa_memblock_release(s->peek_memchunk.memblock);
218         pa_memblock_unref(s->peek_memchunk.memblock);
219     }
220
221     if (s->record_memblockq)
222         pa_memblockq_free(s->record_memblockq);
223
224     if (s->proplist)
225         pa_proplist_free(s->proplist);
226
227     if (s->smoother)
228         pa_smoother_free(s->smoother);
229
230     pa_xfree(s->device_name);
231     pa_xfree(s);
232 }
233
234 void pa_stream_unref(pa_stream *s) {
235     pa_assert(s);
236     pa_assert(PA_REFCNT_VALUE(s) >= 1);
237
238     if (PA_REFCNT_DEC(s) <= 0)
239         stream_free(s);
240 }
241
242 pa_stream* pa_stream_ref(pa_stream *s) {
243     pa_assert(s);
244     pa_assert(PA_REFCNT_VALUE(s) >= 1);
245
246     PA_REFCNT_INC(s);
247     return s;
248 }
249
250 pa_stream_state_t pa_stream_get_state(pa_stream *s) {
251     pa_assert(s);
252     pa_assert(PA_REFCNT_VALUE(s) >= 1);
253
254     return s->state;
255 }
256
257 pa_context* pa_stream_get_context(pa_stream *s) {
258     pa_assert(s);
259     pa_assert(PA_REFCNT_VALUE(s) >= 1);
260
261     return s->context;
262 }
263
264 uint32_t pa_stream_get_index(pa_stream *s) {
265     pa_assert(s);
266     pa_assert(PA_REFCNT_VALUE(s) >= 1);
267
268     PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
269
270     return s->stream_index;
271 }
272
273 void pa_stream_set_state(pa_stream *s, pa_stream_state_t st) {
274     pa_assert(s);
275     pa_assert(PA_REFCNT_VALUE(s) >= 1);
276
277     if (s->state == st)
278         return;
279
280     pa_stream_ref(s);
281
282     s->state = st;
283
284     if (s->state_callback)
285         s->state_callback(s, s->state_userdata);
286
287     if ((st == PA_STREAM_FAILED || st == PA_STREAM_TERMINATED))
288         stream_unlink(s);
289
290     pa_stream_unref(s);
291 }
292
293 static void request_auto_timing_update(pa_stream *s, pa_bool_t force) {
294     pa_assert(s);
295     pa_assert(PA_REFCNT_VALUE(s) >= 1);
296
297     if (!(s->flags & PA_STREAM_AUTO_TIMING_UPDATE))
298         return;
299
300     if (s->state == PA_STREAM_READY &&
301         (force || !s->auto_timing_update_requested)) {
302         pa_operation *o;
303
304 /*         pa_log("automatically requesting new timing data"); */
305
306         if ((o = pa_stream_update_timing_info(s, NULL, NULL))) {
307             pa_operation_unref(o);
308             s->auto_timing_update_requested = TRUE;
309         }
310     }
311
312     if (s->auto_timing_update_event) {
313         struct timeval next;
314         pa_gettimeofday(&next);
315         pa_timeval_add(&next, LATENCY_IPOL_INTERVAL_USEC);
316         s->mainloop->time_restart(s->auto_timing_update_event, &next);
317     }
318 }
319
320 void pa_command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
321     pa_context *c = userdata;
322     pa_stream *s;
323     uint32_t channel;
324
325     pa_assert(pd);
326     pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_KILLED || command == PA_COMMAND_RECORD_STREAM_KILLED);
327     pa_assert(t);
328     pa_assert(c);
329     pa_assert(PA_REFCNT_VALUE(c) >= 1);
330
331     pa_context_ref(c);
332
333     if (pa_tagstruct_getu32(t, &channel) < 0 ||
334         !pa_tagstruct_eof(t)) {
335         pa_context_fail(c, PA_ERR_PROTOCOL);
336         goto finish;
337     }
338
339     if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel)))
340         goto finish;
341
342     if (s->state != PA_STREAM_READY)
343         goto finish;
344
345     pa_context_set_error(c, PA_ERR_KILLED);
346     pa_stream_set_state(s, PA_STREAM_FAILED);
347
348 finish:
349     pa_context_unref(c);
350 }
351
352 static void check_smoother_status(pa_stream *s, pa_bool_t aposteriori, pa_bool_t force_start, pa_bool_t force_stop) {
353     pa_usec_t x;
354
355     pa_assert(s);
356     pa_assert(!force_start || !force_stop);
357
358     if (!s->smoother)
359         return;
360
361     x = pa_rtclock_usec();
362
363     if (s->timing_info_valid) {
364         if (aposteriori)
365             x -= s->timing_info.transport_usec;
366         else
367             x += s->timing_info.transport_usec;
368
369         if (s->direction == PA_STREAM_PLAYBACK)
370             /* it takes a while until the pause/resume is actually
371              * audible */
372             x += s->timing_info.sink_usec;
373         else
374             /* Data froma  while back will be dropped */
375             x -= s->timing_info.source_usec;
376     }
377
378     if (s->suspended || s->corked || force_stop)
379         pa_smoother_pause(s->smoother, x);
380     else if (force_start || s->buffer_attr.prebuf == 0)
381         pa_smoother_resume(s->smoother, x);
382
383     /* Please note that we have no idea if playback actually started
384      * if prebuf is non-zero! */
385 }
386
387 void pa_command_stream_moved(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
388     pa_context *c = userdata;
389     pa_stream *s;
390     uint32_t channel;
391     const char *dn;
392     pa_bool_t suspended;
393     uint32_t di;
394     pa_usec_t usec;
395     uint32_t maxlength = 0, fragsize = 0, minreq = 0, tlength = 0, prebuf = 0;
396
397     pa_assert(pd);
398     pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_MOVED || command == PA_COMMAND_RECORD_STREAM_MOVED);
399     pa_assert(t);
400     pa_assert(c);
401     pa_assert(PA_REFCNT_VALUE(c) >= 1);
402
403     pa_context_ref(c);
404
405     if (c->version < 12) {
406         pa_context_fail(c, PA_ERR_PROTOCOL);
407         goto finish;
408     }
409
410     if (pa_tagstruct_getu32(t, &channel) < 0 ||
411         pa_tagstruct_getu32(t, &di) < 0 ||
412         pa_tagstruct_gets(t, &dn) < 0 ||
413         pa_tagstruct_get_boolean(t, &suspended) < 0) {
414         pa_context_fail(c, PA_ERR_PROTOCOL);
415         goto finish;
416     }
417
418     if (c->version >= 13) {
419
420         if (command == PA_COMMAND_RECORD_STREAM_MOVED) {
421             if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
422                 pa_tagstruct_getu32(t, &fragsize) < 0 ||
423                 pa_tagstruct_get_usec(t, &usec) < 0) {
424                 pa_context_fail(c, PA_ERR_PROTOCOL);
425                 goto finish;
426             }
427         } else {
428             if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
429                 pa_tagstruct_getu32(t, &tlength) < 0 ||
430                 pa_tagstruct_getu32(t, &prebuf) < 0 ||
431                 pa_tagstruct_getu32(t, &minreq) < 0 ||
432                 pa_tagstruct_get_usec(t, &usec) < 0) {
433                 pa_context_fail(c, PA_ERR_PROTOCOL);
434                 goto finish;
435             }
436         }
437     }
438
439     if (!pa_tagstruct_eof(t)) {
440         pa_context_fail(c, PA_ERR_PROTOCOL);
441         goto finish;
442     }
443
444     if (!dn || di == PA_INVALID_INDEX) {
445         pa_context_fail(c, PA_ERR_PROTOCOL);
446         goto finish;
447     }
448
449     if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_MOVED ? c->playback_streams : c->record_streams, channel)))
450         goto finish;
451
452     if (s->state != PA_STREAM_READY)
453         goto finish;
454
455     if (c->version >= 13) {
456         if (s->direction == PA_STREAM_RECORD)
457             s->timing_info.configured_source_usec = usec;
458         else
459             s->timing_info.configured_sink_usec = usec;
460
461         s->buffer_attr.maxlength = maxlength;
462         s->buffer_attr.fragsize = fragsize;
463         s->buffer_attr.tlength = tlength;
464         s->buffer_attr.prebuf = prebuf;
465         s->buffer_attr.minreq = minreq;
466     }
467
468     pa_xfree(s->device_name);
469     s->device_name = pa_xstrdup(dn);
470     s->device_index = di;
471
472     s->suspended = suspended;
473
474     check_smoother_status(s, TRUE, FALSE, FALSE);
475     request_auto_timing_update(s, TRUE);
476
477     if (s->moved_callback)
478         s->moved_callback(s, s->moved_userdata);
479
480 finish:
481     pa_context_unref(c);
482 }
483
484 void pa_command_stream_suspended(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
485     pa_context *c = userdata;
486     pa_stream *s;
487     uint32_t channel;
488     pa_bool_t suspended;
489
490     pa_assert(pd);
491     pa_assert(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED || command == PA_COMMAND_RECORD_STREAM_SUSPENDED);
492     pa_assert(t);
493     pa_assert(c);
494     pa_assert(PA_REFCNT_VALUE(c) >= 1);
495
496     pa_context_ref(c);
497
498     if (c->version < 12) {
499         pa_context_fail(c, PA_ERR_PROTOCOL);
500         goto finish;
501     }
502
503     if (pa_tagstruct_getu32(t, &channel) < 0 ||
504         pa_tagstruct_get_boolean(t, &suspended) < 0 ||
505         !pa_tagstruct_eof(t)) {
506         pa_context_fail(c, PA_ERR_PROTOCOL);
507         goto finish;
508     }
509
510     if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED ? c->playback_streams : c->record_streams, channel)))
511         goto finish;
512
513     if (s->state != PA_STREAM_READY)
514         goto finish;
515
516     s->suspended = suspended;
517
518     check_smoother_status(s, TRUE, FALSE, FALSE);
519     request_auto_timing_update(s, TRUE);
520
521     if (s->suspended_callback)
522         s->suspended_callback(s, s->suspended_userdata);
523
524 finish:
525     pa_context_unref(c);
526 }
527
528 void pa_command_stream_started(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
529     pa_context *c = userdata;
530     pa_stream *s;
531     uint32_t channel;
532
533     pa_assert(pd);
534     pa_assert(command == PA_COMMAND_STARTED);
535     pa_assert(t);
536     pa_assert(c);
537     pa_assert(PA_REFCNT_VALUE(c) >= 1);
538
539     pa_context_ref(c);
540
541     if (c->version < 13) {
542         pa_context_fail(c, PA_ERR_PROTOCOL);
543         goto finish;
544     }
545
546     if (pa_tagstruct_getu32(t, &channel) < 0 ||
547         !pa_tagstruct_eof(t)) {
548         pa_context_fail(c, PA_ERR_PROTOCOL);
549         goto finish;
550     }
551
552     if (!(s = pa_dynarray_get(c->playback_streams, channel)))
553         goto finish;
554
555     if (s->state != PA_STREAM_READY)
556         goto finish;
557
558     check_smoother_status(s, TRUE, TRUE, FALSE);
559     request_auto_timing_update(s, TRUE);
560
561     if (s->started_callback)
562         s->started_callback(s, s->started_userdata);
563
564 finish:
565     pa_context_unref(c);
566 }
567
568 void pa_command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
569     pa_stream *s;
570     pa_context *c = userdata;
571     uint32_t bytes, channel;
572
573     pa_assert(pd);
574     pa_assert(command == PA_COMMAND_REQUEST);
575     pa_assert(t);
576     pa_assert(c);
577     pa_assert(PA_REFCNT_VALUE(c) >= 1);
578
579     pa_context_ref(c);
580
581     if (pa_tagstruct_getu32(t, &channel) < 0 ||
582         pa_tagstruct_getu32(t, &bytes) < 0 ||
583         !pa_tagstruct_eof(t)) {
584         pa_context_fail(c, PA_ERR_PROTOCOL);
585         goto finish;
586     }
587
588     if (!(s = pa_dynarray_get(c->playback_streams, channel)))
589         goto finish;
590
591     if (s->state != PA_STREAM_READY)
592         goto finish;
593
594     s->requested_bytes += bytes;
595
596     if (s->requested_bytes > 0 && s->write_callback)
597         s->write_callback(s, s->requested_bytes, s->write_userdata);
598
599 finish:
600     pa_context_unref(c);
601 }
602
603 void pa_command_overflow_or_underflow(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
604     pa_stream *s;
605     pa_context *c = userdata;
606     uint32_t channel;
607
608     pa_assert(pd);
609     pa_assert(command == PA_COMMAND_OVERFLOW || command == PA_COMMAND_UNDERFLOW);
610     pa_assert(t);
611     pa_assert(c);
612     pa_assert(PA_REFCNT_VALUE(c) >= 1);
613
614     pa_context_ref(c);
615
616     if (pa_tagstruct_getu32(t, &channel) < 0 ||
617         !pa_tagstruct_eof(t)) {
618         pa_context_fail(c, PA_ERR_PROTOCOL);
619         goto finish;
620     }
621
622     if (!(s = pa_dynarray_get(c->playback_streams, channel)))
623         goto finish;
624
625     if (s->state != PA_STREAM_READY)
626         goto finish;
627
628     if (s->buffer_attr.prebuf > 0)
629         check_smoother_status(s, TRUE, FALSE, TRUE);
630
631     request_auto_timing_update(s, TRUE);
632
633     if (command == PA_COMMAND_OVERFLOW) {
634         if (s->overflow_callback)
635             s->overflow_callback(s, s->overflow_userdata);
636     } else if (command == PA_COMMAND_UNDERFLOW) {
637         if (s->underflow_callback)
638             s->underflow_callback(s, s->underflow_userdata);
639     }
640
641  finish:
642     pa_context_unref(c);
643 }
644
645 static void invalidate_indexes(pa_stream *s, pa_bool_t r, pa_bool_t w) {
646     pa_assert(s);
647     pa_assert(PA_REFCNT_VALUE(s) >= 1);
648
649 /*     pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
650
651     if (s->state != PA_STREAM_READY)
652         return;
653
654     if (w) {
655         s->write_index_not_before = s->context->ctag;
656
657         if (s->timing_info_valid)
658             s->timing_info.write_index_corrupt = TRUE;
659
660 /*         pa_log("write_index invalidated"); */
661     }
662
663     if (r) {
664         s->read_index_not_before = s->context->ctag;
665
666         if (s->timing_info_valid)
667             s->timing_info.read_index_corrupt = TRUE;
668
669 /*         pa_log("read_index invalidated"); */
670     }
671
672     request_auto_timing_update(s, TRUE);
673 }
674
675 static void auto_timing_update_callback(pa_mainloop_api *m, pa_time_event *e, const struct timeval *tv, void *userdata) {
676     pa_stream *s = userdata;
677
678     pa_assert(s);
679     pa_assert(PA_REFCNT_VALUE(s) >= 1);
680
681     pa_stream_ref(s);
682     request_auto_timing_update(s, FALSE);
683     pa_stream_unref(s);
684 }
685
686 static void create_stream_complete(pa_stream *s) {
687     pa_assert(s);
688     pa_assert(PA_REFCNT_VALUE(s) >= 1);
689     pa_assert(s->state == PA_STREAM_CREATING);
690
691     pa_stream_set_state(s, PA_STREAM_READY);
692
693     if (s->requested_bytes > 0 && s->write_callback)
694         s->write_callback(s, s->requested_bytes, s->write_userdata);
695
696     if (s->flags & PA_STREAM_AUTO_TIMING_UPDATE) {
697         struct timeval tv;
698         pa_gettimeofday(&tv);
699         tv.tv_usec += (suseconds_t) LATENCY_IPOL_INTERVAL_USEC; /* every 100 ms */
700         pa_assert(!s->auto_timing_update_event);
701         s->auto_timing_update_event = s->mainloop->time_new(s->mainloop, &tv, &auto_timing_update_callback, s);
702
703         request_auto_timing_update(s, TRUE);
704     }
705
706     check_smoother_status(s, TRUE, FALSE, FALSE);
707 }
708
709 static void automatic_buffer_attr(pa_stream *s, pa_buffer_attr *attr, const pa_sample_spec *ss) {
710     pa_assert(s);
711     pa_assert(attr);
712     pa_assert(ss);
713
714     if (s->context->version >= 13)
715         return;
716
717     /* Version older than 0.9.10 didn't do server side buffer_attr
718      * selection, hence we have to fake it on the client side. */
719
720     /* We choose fairly conservative values here, to not confuse
721      * old clients with extremely large playback buffers */
722
723     if (attr->maxlength == (uint32_t) -1)
724         attr->maxlength = 4*1024*1024; /* 4MB is the maximum queue length PulseAudio <= 0.9.9 supported. */
725
726     if (attr->tlength == (uint32_t) -1)
727         attr->tlength = (uint32_t) pa_usec_to_bytes(250*PA_USEC_PER_MSEC, ss); /* 250ms of buffering */
728
729     if (attr->minreq == (uint32_t) -1)
730         attr->minreq = (attr->tlength)/5; /* Ask for more data when there are only 200ms left in the playback buffer */
731
732     if (attr->prebuf == (uint32_t) -1)
733         attr->prebuf = attr->tlength; /* Start to play only when the playback is fully filled up once */
734
735     if (attr->fragsize == (uint32_t) -1)
736         attr->fragsize = attr->tlength; /* Pass data to the app only when the buffer is filled up once */
737 }
738
739 void pa_create_stream_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
740     pa_stream *s = userdata;
741
742     pa_assert(pd);
743     pa_assert(s);
744     pa_assert(PA_REFCNT_VALUE(s) >= 1);
745     pa_assert(s->state == PA_STREAM_CREATING);
746
747     pa_stream_ref(s);
748
749     if (command != PA_COMMAND_REPLY) {
750         if (pa_context_handle_error(s->context, command, t, FALSE) < 0)
751             goto finish;
752
753         pa_stream_set_state(s, PA_STREAM_FAILED);
754         goto finish;
755     }
756
757     if (pa_tagstruct_getu32(t, &s->channel) < 0 ||
758         s->channel == PA_INVALID_INDEX ||
759         ((s->direction != PA_STREAM_UPLOAD) && (pa_tagstruct_getu32(t, &s->stream_index) < 0 ||  s->stream_index == PA_INVALID_INDEX)) ||
760         ((s->direction != PA_STREAM_RECORD) && pa_tagstruct_getu32(t, &s->requested_bytes) < 0)) {
761         pa_context_fail(s->context, PA_ERR_PROTOCOL);
762         goto finish;
763     }
764
765     if (s->context->version >= 9) {
766         if (s->direction == PA_STREAM_PLAYBACK) {
767             if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
768                 pa_tagstruct_getu32(t, &s->buffer_attr.tlength) < 0 ||
769                 pa_tagstruct_getu32(t, &s->buffer_attr.prebuf) < 0 ||
770                 pa_tagstruct_getu32(t, &s->buffer_attr.minreq) < 0) {
771                 pa_context_fail(s->context, PA_ERR_PROTOCOL);
772                 goto finish;
773             }
774         } else if (s->direction == PA_STREAM_RECORD) {
775             if (pa_tagstruct_getu32(t, &s->buffer_attr.maxlength) < 0 ||
776                 pa_tagstruct_getu32(t, &s->buffer_attr.fragsize) < 0) {
777                 pa_context_fail(s->context, PA_ERR_PROTOCOL);
778                 goto finish;
779             }
780         }
781     }
782
783     if (s->context->version >= 12 && s->direction != PA_STREAM_UPLOAD) {
784         pa_sample_spec ss;
785         pa_channel_map cm;
786         const char *dn = NULL;
787         pa_bool_t suspended;
788
789         if (pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
790             pa_tagstruct_get_channel_map(t, &cm) < 0 ||
791             pa_tagstruct_getu32(t, &s->device_index) < 0 ||
792             pa_tagstruct_gets(t, &dn) < 0 ||
793             pa_tagstruct_get_boolean(t, &suspended) < 0) {
794             pa_context_fail(s->context, PA_ERR_PROTOCOL);
795             goto finish;
796         }
797
798         if (!dn || s->device_index == PA_INVALID_INDEX ||
799             ss.channels != cm.channels ||
800             !pa_channel_map_valid(&cm) ||
801             !pa_sample_spec_valid(&ss) ||
802             (!(s->flags & PA_STREAM_FIX_FORMAT) && ss.format != s->sample_spec.format) ||
803             (!(s->flags & PA_STREAM_FIX_RATE) && ss.rate != s->sample_spec.rate) ||
804             (!(s->flags & PA_STREAM_FIX_CHANNELS) && !pa_channel_map_equal(&cm, &s->channel_map))) {
805             pa_context_fail(s->context, PA_ERR_PROTOCOL);
806             goto finish;
807         }
808
809         pa_xfree(s->device_name);
810         s->device_name = pa_xstrdup(dn);
811         s->suspended = suspended;
812
813         s->channel_map = cm;
814         s->sample_spec = ss;
815     }
816
817     if (s->context->version >= 13 && s->direction != PA_STREAM_UPLOAD) {
818         pa_usec_t usec;
819
820         if (pa_tagstruct_get_usec(t, &usec) < 0) {
821             pa_context_fail(s->context, PA_ERR_PROTOCOL);
822             goto finish;
823         }
824
825         if (s->direction == PA_STREAM_RECORD)
826             s->timing_info.configured_source_usec = usec;
827         else
828             s->timing_info.configured_sink_usec = usec;
829     }
830
831     if (!pa_tagstruct_eof(t)) {
832         pa_context_fail(s->context, PA_ERR_PROTOCOL);
833         goto finish;
834     }
835
836     if (s->direction == PA_STREAM_RECORD) {
837         pa_assert(!s->record_memblockq);
838
839         s->record_memblockq = pa_memblockq_new(
840                 0,
841                 s->buffer_attr.maxlength,
842                 0,
843                 pa_frame_size(&s->sample_spec),
844                 1,
845                 0,
846                 0,
847                 NULL);
848     }
849
850     s->channel_valid = TRUE;
851     pa_dynarray_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, s->channel, s);
852
853     create_stream_complete(s);
854
855 finish:
856     pa_stream_unref(s);
857 }
858
859 static int create_stream(
860         pa_stream_direction_t direction,
861         pa_stream *s,
862         const char *dev,
863         const pa_buffer_attr *attr,
864         pa_stream_flags_t flags,
865         const pa_cvolume *volume,
866         pa_stream *sync_stream) {
867
868     pa_tagstruct *t;
869     uint32_t tag;
870     pa_bool_t volume_set = FALSE;
871
872     pa_assert(s);
873     pa_assert(PA_REFCNT_VALUE(s) >= 1);
874     pa_assert(direction == PA_STREAM_PLAYBACK || direction == PA_STREAM_RECORD);
875
876     PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
877     PA_CHECK_VALIDITY(s->context, s->direct_on_input == PA_INVALID_INDEX || direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
878     PA_CHECK_VALIDITY(s->context, !(flags & ~(PA_STREAM_START_CORKED|
879                                               PA_STREAM_INTERPOLATE_TIMING|
880                                               PA_STREAM_NOT_MONOTONOUS|
881                                               PA_STREAM_AUTO_TIMING_UPDATE|
882                                               PA_STREAM_NO_REMAP_CHANNELS|
883                                               PA_STREAM_NO_REMIX_CHANNELS|
884                                               PA_STREAM_FIX_FORMAT|
885                                               PA_STREAM_FIX_RATE|
886                                               PA_STREAM_FIX_CHANNELS|
887                                               PA_STREAM_DONT_MOVE|
888                                               PA_STREAM_VARIABLE_RATE|
889                                               PA_STREAM_PEAK_DETECT|
890                                               PA_STREAM_START_MUTED|
891                                               PA_STREAM_ADJUST_LATENCY|
892                                               PA_STREAM_EARLY_REQUESTS|
893                                               PA_STREAM_DONT_INHIBIT_AUTO_SUSPEND|
894                                               PA_STREAM_START_UNMUTED)), PA_ERR_INVALID);
895
896     PA_CHECK_VALIDITY(s->context, s->context->version >= 12 || !(flags & PA_STREAM_VARIABLE_RATE), PA_ERR_NOTSUPPORTED);
897     PA_CHECK_VALIDITY(s->context, s->context->version >= 13 || !(flags & PA_STREAM_PEAK_DETECT), PA_ERR_NOTSUPPORTED);
898     /* Althought some of the other flags are not supported on older
899      * version, we don't check for them here, because it doesn't hurt
900      * when they are passed but actually not supported. This makes
901      * client development easier */
902
903     PA_CHECK_VALIDITY(s->context, direction == PA_STREAM_PLAYBACK || !(flags & (PA_STREAM_START_MUTED)), PA_ERR_INVALID);
904     PA_CHECK_VALIDITY(s->context, direction == PA_STREAM_RECORD || !(flags & (PA_STREAM_PEAK_DETECT)), PA_ERR_INVALID);
905     PA_CHECK_VALIDITY(s->context, !volume || volume->channels == s->sample_spec.channels, PA_ERR_INVALID);
906     PA_CHECK_VALIDITY(s->context, !sync_stream || (direction == PA_STREAM_PLAYBACK && sync_stream->direction == PA_STREAM_PLAYBACK), PA_ERR_INVALID);
907     PA_CHECK_VALIDITY(s->context, (flags & (PA_STREAM_ADJUST_LATENCY|PA_STREAM_EARLY_REQUESTS)) != (PA_STREAM_ADJUST_LATENCY|PA_STREAM_EARLY_REQUESTS), PA_ERR_INVALID);
908
909     pa_stream_ref(s);
910
911     s->direction = direction;
912     s->flags = flags;
913     s->corked = !!(flags & PA_STREAM_START_CORKED);
914
915     if (sync_stream)
916         s->syncid = sync_stream->syncid;
917
918     if (attr)
919         s->buffer_attr = *attr;
920     automatic_buffer_attr(s, &s->buffer_attr, &s->sample_spec);
921
922     if (flags & PA_STREAM_INTERPOLATE_TIMING) {
923         pa_usec_t x;
924
925         if (s->smoother)
926             pa_smoother_free(s->smoother);
927
928         s->smoother = pa_smoother_new(SMOOTHER_ADJUST_TIME, SMOOTHER_HISTORY_TIME, !(flags & PA_STREAM_NOT_MONOTONIC), SMOOTHER_MIN_HISTORY);
929
930         x = pa_rtclock_usec();
931         pa_smoother_set_time_offset(s->smoother, x);
932         pa_smoother_pause(s->smoother, x);
933     }
934
935     if (!dev)
936         dev = s->direction == PA_STREAM_PLAYBACK ? s->context->conf->default_sink : s->context->conf->default_source;
937
938     t = pa_tagstruct_command(
939             s->context,
940             (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM),
941             &tag);
942
943     if (s->context->version < 13)
944         pa_tagstruct_puts(t, pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME));
945
946     pa_tagstruct_put(
947             t,
948             PA_TAG_SAMPLE_SPEC, &s->sample_spec,
949             PA_TAG_CHANNEL_MAP, &s->channel_map,
950             PA_TAG_U32, PA_INVALID_INDEX,
951             PA_TAG_STRING, dev,
952             PA_TAG_U32, s->buffer_attr.maxlength,
953             PA_TAG_BOOLEAN, s->corked,
954             PA_TAG_INVALID);
955
956     if (s->direction == PA_STREAM_PLAYBACK) {
957         pa_cvolume cv;
958
959         pa_tagstruct_put(
960                 t,
961                 PA_TAG_U32, s->buffer_attr.tlength,
962                 PA_TAG_U32, s->buffer_attr.prebuf,
963                 PA_TAG_U32, s->buffer_attr.minreq,
964                 PA_TAG_U32, s->syncid,
965                 PA_TAG_INVALID);
966
967         volume_set = !!volume;
968
969         if (!volume)
970             volume = pa_cvolume_reset(&cv, s->sample_spec.channels);
971
972         pa_tagstruct_put_cvolume(t, volume);
973     } else
974         pa_tagstruct_putu32(t, s->buffer_attr.fragsize);
975
976     if (s->context->version >= 12) {
977         pa_tagstruct_put(
978                 t,
979                 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMAP_CHANNELS,
980                 PA_TAG_BOOLEAN, flags & PA_STREAM_NO_REMIX_CHANNELS,
981                 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_FORMAT,
982                 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_RATE,
983                 PA_TAG_BOOLEAN, flags & PA_STREAM_FIX_CHANNELS,
984                 PA_TAG_BOOLEAN, flags & PA_STREAM_DONT_MOVE,
985                 PA_TAG_BOOLEAN, flags & PA_STREAM_VARIABLE_RATE,
986                 PA_TAG_INVALID);
987     }
988
989     if (s->context->version >= 13) {
990
991         if (s->direction == PA_STREAM_PLAYBACK)
992             pa_tagstruct_put_boolean(t, flags & PA_STREAM_START_MUTED);
993         else
994             pa_tagstruct_put_boolean(t, flags & PA_STREAM_PEAK_DETECT);
995
996         pa_tagstruct_put(
997                 t,
998                 PA_TAG_BOOLEAN, flags & PA_STREAM_ADJUST_LATENCY,
999                 PA_TAG_PROPLIST, s->proplist,
1000                 PA_TAG_INVALID);
1001
1002         if (s->direction == PA_STREAM_RECORD)
1003             pa_tagstruct_putu32(t, s->direct_on_input);
1004     }
1005
1006     if (s->context->version >= 14) {
1007
1008         if (s->direction == PA_STREAM_PLAYBACK)
1009             pa_tagstruct_put_boolean(t, volume_set);
1010
1011         pa_tagstruct_put_boolean(t, flags & PA_STREAM_EARLY_REQUESTS);
1012     }
1013
1014     if (s->context->version >= 15) {
1015
1016         if (s->direction == PA_STREAM_PLAYBACK)
1017             pa_tagstruct_put_boolean(t, flags & (PA_STREAM_START_MUTED|PA_STREAM_START_UNMUTED));
1018
1019         pa_tagstruct_put_boolean(t, flags & PA_STREAM_DONT_INHIBIT_AUTO_SUSPEND);
1020     }
1021
1022     pa_pstream_send_tagstruct(s->context->pstream, t);
1023     pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s, NULL);
1024
1025     pa_stream_set_state(s, PA_STREAM_CREATING);
1026
1027     pa_stream_unref(s);
1028     return 0;
1029 }
1030
1031 int pa_stream_connect_playback(
1032         pa_stream *s,
1033         const char *dev,
1034         const pa_buffer_attr *attr,
1035         pa_stream_flags_t flags,
1036         pa_cvolume *volume,
1037         pa_stream *sync_stream) {
1038
1039     pa_assert(s);
1040     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1041
1042     return create_stream(PA_STREAM_PLAYBACK, s, dev, attr, flags, volume, sync_stream);
1043 }
1044
1045 int pa_stream_connect_record(
1046         pa_stream *s,
1047         const char *dev,
1048         const pa_buffer_attr *attr,
1049         pa_stream_flags_t flags) {
1050
1051     pa_assert(s);
1052     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1053
1054     return create_stream(PA_STREAM_RECORD, s, dev, attr, flags, NULL, NULL);
1055 }
1056
1057 int pa_stream_write(
1058         pa_stream *s,
1059         const void *data,
1060         size_t length,
1061         void (*free_cb)(void *p),
1062         int64_t offset,
1063         pa_seek_mode_t seek) {
1064
1065     pa_memchunk chunk;
1066     pa_seek_mode_t t_seek;
1067     int64_t t_offset;
1068     size_t t_length;
1069     const void *t_data;
1070
1071     pa_assert(s);
1072     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1073     pa_assert(data);
1074
1075     PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1076     PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || s->direction == PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1077     PA_CHECK_VALIDITY(s->context, seek <= PA_SEEK_RELATIVE_END, PA_ERR_INVALID);
1078     PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || (seek == PA_SEEK_RELATIVE && offset == 0), PA_ERR_INVALID);
1079
1080     if (length <= 0)
1081         return 0;
1082
1083     t_seek = seek;
1084     t_offset = offset;
1085     t_length = length;
1086     t_data = data;
1087
1088     while (t_length > 0) {
1089
1090         chunk.index = 0;
1091
1092         if (free_cb && !pa_pstream_get_shm(s->context->pstream)) {
1093             chunk.memblock = pa_memblock_new_user(s->context->mempool, (void*) t_data, t_length, free_cb, 1);
1094             chunk.length = t_length;
1095         } else {
1096             void *d;
1097
1098             chunk.length = PA_MIN(t_length, pa_mempool_block_size_max(s->context->mempool));
1099             chunk.memblock = pa_memblock_new(s->context->mempool, chunk.length);
1100
1101             d = pa_memblock_acquire(chunk.memblock);
1102             memcpy(d, t_data, chunk.length);
1103             pa_memblock_release(chunk.memblock);
1104         }
1105
1106         pa_pstream_send_memblock(s->context->pstream, s->channel, t_offset, t_seek, &chunk);
1107
1108         t_offset = 0;
1109         t_seek = PA_SEEK_RELATIVE;
1110
1111         t_data = (const uint8_t*) t_data + chunk.length;
1112         t_length -= chunk.length;
1113
1114         pa_memblock_unref(chunk.memblock);
1115     }
1116
1117     if (free_cb && pa_pstream_get_shm(s->context->pstream))
1118         free_cb((void*) data);
1119
1120     if (length < s->requested_bytes)
1121         s->requested_bytes -= (uint32_t) length;
1122     else
1123         s->requested_bytes = 0;
1124
1125     /* FIXME!!! ^^^ will break when offset is != 0 and mode is not RELATIVE*/
1126
1127     if (s->direction == PA_STREAM_PLAYBACK) {
1128
1129         /* Update latency request correction */
1130         if (s->write_index_corrections[s->current_write_index_correction].valid) {
1131
1132             if (seek == PA_SEEK_ABSOLUTE) {
1133                 s->write_index_corrections[s->current_write_index_correction].corrupt = FALSE;
1134                 s->write_index_corrections[s->current_write_index_correction].absolute = TRUE;
1135                 s->write_index_corrections[s->current_write_index_correction].value = offset + (int64_t) length;
1136             } else if (seek == PA_SEEK_RELATIVE) {
1137                 if (!s->write_index_corrections[s->current_write_index_correction].corrupt)
1138                     s->write_index_corrections[s->current_write_index_correction].value += offset + (int64_t) length;
1139             } else
1140                 s->write_index_corrections[s->current_write_index_correction].corrupt = TRUE;
1141         }
1142
1143         /* Update the write index in the already available latency data */
1144         if (s->timing_info_valid) {
1145
1146             if (seek == PA_SEEK_ABSOLUTE) {
1147                 s->timing_info.write_index_corrupt = FALSE;
1148                 s->timing_info.write_index = offset + (int64_t) length;
1149             } else if (seek == PA_SEEK_RELATIVE) {
1150                 if (!s->timing_info.write_index_corrupt)
1151                     s->timing_info.write_index += offset + (int64_t) length;
1152             } else
1153                 s->timing_info.write_index_corrupt = TRUE;
1154         }
1155
1156         if (!s->timing_info_valid || s->timing_info.write_index_corrupt)
1157             request_auto_timing_update(s, TRUE);
1158     }
1159
1160     return 0;
1161 }
1162
1163 int pa_stream_peek(pa_stream *s, const void **data, size_t *length) {
1164     pa_assert(s);
1165     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1166     pa_assert(data);
1167     pa_assert(length);
1168
1169     PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1170     PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1171
1172     if (!s->peek_memchunk.memblock) {
1173
1174         if (pa_memblockq_peek(s->record_memblockq, &s->peek_memchunk) < 0) {
1175             *data = NULL;
1176             *length = 0;
1177             return 0;
1178         }
1179
1180         s->peek_data = pa_memblock_acquire(s->peek_memchunk.memblock);
1181     }
1182
1183     pa_assert(s->peek_data);
1184     *data = (uint8_t*) s->peek_data + s->peek_memchunk.index;
1185     *length = s->peek_memchunk.length;
1186     return 0;
1187 }
1188
1189 int pa_stream_drop(pa_stream *s) {
1190     pa_assert(s);
1191     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1192
1193     PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1194     PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
1195     PA_CHECK_VALIDITY(s->context, s->peek_memchunk.memblock, PA_ERR_BADSTATE);
1196
1197     pa_memblockq_drop(s->record_memblockq, s->peek_memchunk.length);
1198
1199     /* Fix the simulated local read index */
1200     if (s->timing_info_valid && !s->timing_info.read_index_corrupt)
1201         s->timing_info.read_index += (int64_t) s->peek_memchunk.length;
1202
1203     pa_assert(s->peek_data);
1204     pa_memblock_release(s->peek_memchunk.memblock);
1205     pa_memblock_unref(s->peek_memchunk.memblock);
1206     pa_memchunk_reset(&s->peek_memchunk);
1207
1208     return 0;
1209 }
1210
1211 size_t pa_stream_writable_size(pa_stream *s) {
1212     pa_assert(s);
1213     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1214
1215     PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
1216     PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
1217
1218     return s->requested_bytes;
1219 }
1220
1221 size_t pa_stream_readable_size(pa_stream *s) {
1222     pa_assert(s);
1223     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1224
1225     PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
1226     PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
1227
1228     return pa_memblockq_get_length(s->record_memblockq);
1229 }
1230
1231 pa_operation * pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1232     pa_operation *o;
1233     pa_tagstruct *t;
1234     uint32_t tag;
1235
1236     pa_assert(s);
1237     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1238
1239     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1240     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1241
1242     o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1243
1244     t = pa_tagstruct_command(s->context, PA_COMMAND_DRAIN_PLAYBACK_STREAM, &tag);
1245     pa_tagstruct_putu32(t, s->channel);
1246     pa_pstream_send_tagstruct(s->context->pstream, t);
1247     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);
1248
1249     return o;
1250 }
1251
1252 static pa_usec_t calc_time(pa_stream *s, pa_bool_t ignore_transport) {
1253     pa_usec_t usec;
1254
1255     pa_assert(s);
1256     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1257     pa_assert(s->state == PA_STREAM_READY);
1258     pa_assert(s->direction != PA_STREAM_UPLOAD);
1259     pa_assert(s->timing_info_valid);
1260     pa_assert(s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt);
1261     pa_assert(s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt);
1262
1263     if (s->direction == PA_STREAM_PLAYBACK) {
1264         /* The last byte that was written into the output device
1265          * had this time value associated */
1266         usec = pa_bytes_to_usec(s->timing_info.read_index < 0 ? 0 : (uint64_t) s->timing_info.read_index, &s->sample_spec);
1267
1268         if (!s->corked && !s->suspended) {
1269
1270             if (!ignore_transport)
1271                 /* Because the latency info took a little time to come
1272                  * to us, we assume that the real output time is actually
1273                  * a little ahead */
1274                 usec += s->timing_info.transport_usec;
1275
1276             /* However, the output device usually maintains a buffer
1277                too, hence the real sample currently played is a little
1278                back  */
1279             if (s->timing_info.sink_usec >= usec)
1280                 usec = 0;
1281             else
1282                 usec -= s->timing_info.sink_usec;
1283         }
1284
1285     } else {
1286         pa_assert(s->direction == PA_STREAM_RECORD);
1287
1288         /* The last byte written into the server side queue had
1289          * this time value associated */
1290         usec = pa_bytes_to_usec(s->timing_info.write_index < 0 ? 0 : (uint64_t) s->timing_info.write_index, &s->sample_spec);
1291
1292         if (!s->corked && !s->suspended) {
1293
1294             if (!ignore_transport)
1295                 /* Add transport latency */
1296                 usec += s->timing_info.transport_usec;
1297
1298             /* Add latency of data in device buffer */
1299             usec += s->timing_info.source_usec;
1300
1301             /* If this is a monitor source, we need to correct the
1302              * time by the playback device buffer */
1303             if (s->timing_info.sink_usec >= usec)
1304                 usec = 0;
1305             else
1306                 usec -= s->timing_info.sink_usec;
1307         }
1308     }
1309
1310     return usec;
1311 }
1312
1313 static void stream_get_timing_info_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1314     pa_operation *o = userdata;
1315     struct timeval local, remote, now;
1316     pa_timing_info *i;
1317     pa_bool_t playing = FALSE;
1318     uint64_t underrun_for = 0, playing_for = 0;
1319
1320     pa_assert(pd);
1321     pa_assert(o);
1322     pa_assert(PA_REFCNT_VALUE(o) >= 1);
1323
1324     if (!o->context || !o->stream)
1325         goto finish;
1326
1327     i = &o->stream->timing_info;
1328
1329     o->stream->timing_info_valid = FALSE;
1330     i->write_index_corrupt = TRUE;
1331     i->read_index_corrupt = TRUE;
1332
1333     if (command != PA_COMMAND_REPLY) {
1334         if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
1335             goto finish;
1336
1337     } else {
1338
1339         if (pa_tagstruct_get_usec(t, &i->sink_usec) < 0 ||
1340             pa_tagstruct_get_usec(t, &i->source_usec) < 0 ||
1341             pa_tagstruct_get_boolean(t, &playing) < 0 ||
1342             pa_tagstruct_get_timeval(t, &local) < 0 ||
1343             pa_tagstruct_get_timeval(t, &remote) < 0 ||
1344             pa_tagstruct_gets64(t, &i->write_index) < 0 ||
1345             pa_tagstruct_gets64(t, &i->read_index) < 0) {
1346
1347             pa_context_fail(o->context, PA_ERR_PROTOCOL);
1348             goto finish;
1349         }
1350
1351         if (o->context->version >= 13 &&
1352             o->stream->direction == PA_STREAM_PLAYBACK)
1353             if (pa_tagstruct_getu64(t, &underrun_for) < 0 ||
1354                 pa_tagstruct_getu64(t, &playing_for) < 0) {
1355
1356                 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1357                 goto finish;
1358             }
1359
1360
1361         if (!pa_tagstruct_eof(t)) {
1362             pa_context_fail(o->context, PA_ERR_PROTOCOL);
1363             goto finish;
1364         }
1365         o->stream->timing_info_valid = TRUE;
1366         i->write_index_corrupt = FALSE;
1367         i->read_index_corrupt = FALSE;
1368
1369         i->playing = (int) playing;
1370         i->since_underrun = (int64_t) (playing ? playing_for : underrun_for);
1371
1372         pa_gettimeofday(&now);
1373
1374         /* Calculcate timestamps */
1375         if (pa_timeval_cmp(&local, &remote) <= 0 && pa_timeval_cmp(&remote, &now) <= 0) {
1376             /* local and remote seem to have synchronized clocks */
1377
1378             if (o->stream->direction == PA_STREAM_PLAYBACK)
1379                 i->transport_usec = pa_timeval_diff(&remote, &local);
1380             else
1381                 i->transport_usec = pa_timeval_diff(&now, &remote);
1382
1383             i->synchronized_clocks = TRUE;
1384             i->timestamp = remote;
1385         } else {
1386             /* clocks are not synchronized, let's estimate latency then */
1387             i->transport_usec = pa_timeval_diff(&now, &local)/2;
1388             i->synchronized_clocks = FALSE;
1389             i->timestamp = local;
1390             pa_timeval_add(&i->timestamp, i->transport_usec);
1391         }
1392
1393         /* Invalidate read and write indexes if necessary */
1394         if (tag < o->stream->read_index_not_before)
1395             i->read_index_corrupt = TRUE;
1396
1397         if (tag < o->stream->write_index_not_before)
1398             i->write_index_corrupt = TRUE;
1399
1400         if (o->stream->direction == PA_STREAM_PLAYBACK) {
1401             /* Write index correction */
1402
1403             int n, j;
1404             uint32_t ctag = tag;
1405
1406             /* Go through the saved correction values and add up the
1407              * total correction.*/
1408             for (n = 0, j = o->stream->current_write_index_correction+1;
1409                  n < PA_MAX_WRITE_INDEX_CORRECTIONS;
1410                  n++, j = (j + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS) {
1411
1412                 /* Step over invalid data or out-of-date data */
1413                 if (!o->stream->write_index_corrections[j].valid ||
1414                     o->stream->write_index_corrections[j].tag < ctag)
1415                     continue;
1416
1417                 /* Make sure that everything is in order */
1418                 ctag = o->stream->write_index_corrections[j].tag+1;
1419
1420                 /* Now fix the write index */
1421                 if (o->stream->write_index_corrections[j].corrupt) {
1422                     /* A corrupting seek was made */
1423                     i->write_index_corrupt = TRUE;
1424                 } else if (o->stream->write_index_corrections[j].absolute) {
1425                     /* An absolute seek was made */
1426                     i->write_index = o->stream->write_index_corrections[j].value;
1427                     i->write_index_corrupt = FALSE;
1428                 } else if (!i->write_index_corrupt) {
1429                     /* A relative seek was made */
1430                     i->write_index += o->stream->write_index_corrections[j].value;
1431                 }
1432             }
1433
1434             /* Clear old correction entries */
1435             for (n = 0; n < PA_MAX_WRITE_INDEX_CORRECTIONS; n++) {
1436                 if (!o->stream->write_index_corrections[n].valid)
1437                     continue;
1438
1439                 if (o->stream->write_index_corrections[n].tag <= tag)
1440                     o->stream->write_index_corrections[n].valid = FALSE;
1441             }
1442         }
1443
1444         if (o->stream->direction == PA_STREAM_RECORD) {
1445             /* Read index correction */
1446
1447             if (!i->read_index_corrupt)
1448                 i->read_index -= (int64_t) pa_memblockq_get_length(o->stream->record_memblockq);
1449         }
1450
1451         /* Update smoother */
1452         if (o->stream->smoother) {
1453             pa_usec_t u, x;
1454
1455             u = x = pa_rtclock_usec() - i->transport_usec;
1456
1457             if (o->stream->direction == PA_STREAM_PLAYBACK && o->context->version >= 13) {
1458                 pa_usec_t su;
1459
1460                 /* If we weren't playing then it will take some time
1461                  * until the audio will actually come out through the
1462                  * speakers. Since we follow that timing here, we need
1463                  * to try to fix this up */
1464
1465                 su = pa_bytes_to_usec((uint64_t) i->since_underrun, &o->stream->sample_spec);
1466
1467                 if (su < i->sink_usec)
1468                     x += i->sink_usec - su;
1469             }
1470
1471             if (!i->playing)
1472                 pa_smoother_pause(o->stream->smoother, x);
1473
1474             /* Update the smoother */
1475             if ((o->stream->direction == PA_STREAM_PLAYBACK && !i->read_index_corrupt) ||
1476                 (o->stream->direction == PA_STREAM_RECORD && !i->write_index_corrupt))
1477                 pa_smoother_put(o->stream->smoother, u, calc_time(o->stream, TRUE));
1478
1479             if (i->playing)
1480                 pa_smoother_resume(o->stream->smoother, x);
1481         }
1482     }
1483
1484     o->stream->auto_timing_update_requested = FALSE;
1485
1486     if (o->stream->latency_update_callback)
1487         o->stream->latency_update_callback(o->stream, o->stream->latency_update_userdata);
1488
1489     if (o->callback && o->stream && o->stream->state == PA_STREAM_READY) {
1490         pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1491         cb(o->stream, o->stream->timing_info_valid, o->userdata);
1492     }
1493
1494 finish:
1495
1496     pa_operation_done(o);
1497     pa_operation_unref(o);
1498 }
1499
1500 pa_operation* pa_stream_update_timing_info(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1501     uint32_t tag;
1502     pa_operation *o;
1503     pa_tagstruct *t;
1504     struct timeval now;
1505     int cidx = 0;
1506
1507     pa_assert(s);
1508     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1509
1510     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1511     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1512
1513     if (s->direction == PA_STREAM_PLAYBACK) {
1514         /* Find a place to store the write_index correction data for this entry */
1515         cidx = (s->current_write_index_correction + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS;
1516
1517         /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
1518         PA_CHECK_VALIDITY_RETURN_NULL(s->context, !s->write_index_corrections[cidx].valid, PA_ERR_INTERNAL);
1519     }
1520     o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1521
1522     t = pa_tagstruct_command(
1523             s->context,
1524             (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_GET_PLAYBACK_LATENCY : PA_COMMAND_GET_RECORD_LATENCY),
1525             &tag);
1526     pa_tagstruct_putu32(t, s->channel);
1527     pa_tagstruct_put_timeval(t, pa_gettimeofday(&now));
1528
1529     pa_pstream_send_tagstruct(s->context->pstream, t);
1530     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);
1531
1532     if (s->direction == PA_STREAM_PLAYBACK) {
1533         /* Fill in initial correction data */
1534
1535         s->current_write_index_correction = cidx;
1536
1537         s->write_index_corrections[cidx].valid = TRUE;
1538         s->write_index_corrections[cidx].absolute = FALSE;
1539         s->write_index_corrections[cidx].corrupt = FALSE;
1540         s->write_index_corrections[cidx].tag = tag;
1541         s->write_index_corrections[cidx].value = 0;
1542     }
1543
1544     return o;
1545 }
1546
1547 void pa_stream_disconnect_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1548     pa_stream *s = userdata;
1549
1550     pa_assert(pd);
1551     pa_assert(s);
1552     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1553
1554     pa_stream_ref(s);
1555
1556     if (command != PA_COMMAND_REPLY) {
1557         if (pa_context_handle_error(s->context, command, t, FALSE) < 0)
1558             goto finish;
1559
1560         pa_stream_set_state(s, PA_STREAM_FAILED);
1561         goto finish;
1562     } else if (!pa_tagstruct_eof(t)) {
1563         pa_context_fail(s->context, PA_ERR_PROTOCOL);
1564         goto finish;
1565     }
1566
1567     pa_stream_set_state(s, PA_STREAM_TERMINATED);
1568
1569 finish:
1570     pa_stream_unref(s);
1571 }
1572
1573 int pa_stream_disconnect(pa_stream *s) {
1574     pa_tagstruct *t;
1575     uint32_t tag;
1576
1577     pa_assert(s);
1578     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1579
1580     PA_CHECK_VALIDITY(s->context, s->channel_valid, PA_ERR_BADSTATE);
1581     PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1582
1583     pa_stream_ref(s);
1584
1585     t = pa_tagstruct_command(
1586             s->context,
1587             (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_DELETE_PLAYBACK_STREAM :
1588                         (s->direction == PA_STREAM_RECORD ? PA_COMMAND_DELETE_RECORD_STREAM : PA_COMMAND_DELETE_UPLOAD_STREAM)),
1589             &tag);
1590     pa_tagstruct_putu32(t, s->channel);
1591     pa_pstream_send_tagstruct(s->context->pstream, t);
1592     pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s, NULL);
1593
1594     pa_stream_unref(s);
1595     return 0;
1596 }
1597
1598 void pa_stream_set_read_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1599     pa_assert(s);
1600     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1601
1602     if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1603         return;
1604
1605     s->read_callback = cb;
1606     s->read_userdata = userdata;
1607 }
1608
1609 void pa_stream_set_write_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
1610     pa_assert(s);
1611     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1612
1613     if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1614         return;
1615
1616     s->write_callback = cb;
1617     s->write_userdata = userdata;
1618 }
1619
1620 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1621     pa_assert(s);
1622     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1623
1624     if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1625         return;
1626
1627     s->state_callback = cb;
1628     s->state_userdata = userdata;
1629 }
1630
1631 void pa_stream_set_overflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1632     pa_assert(s);
1633     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1634
1635     if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1636         return;
1637
1638     s->overflow_callback = cb;
1639     s->overflow_userdata = userdata;
1640 }
1641
1642 void pa_stream_set_underflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1643     pa_assert(s);
1644     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1645
1646     if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1647         return;
1648
1649     s->underflow_callback = cb;
1650     s->underflow_userdata = userdata;
1651 }
1652
1653 void pa_stream_set_latency_update_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1654     pa_assert(s);
1655     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1656
1657     if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1658         return;
1659
1660     s->latency_update_callback = cb;
1661     s->latency_update_userdata = userdata;
1662 }
1663
1664 void pa_stream_set_moved_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1665     pa_assert(s);
1666     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1667
1668     if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1669         return;
1670
1671     s->moved_callback = cb;
1672     s->moved_userdata = userdata;
1673 }
1674
1675 void pa_stream_set_suspended_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1676     pa_assert(s);
1677     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1678
1679     if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1680         return;
1681
1682     s->suspended_callback = cb;
1683     s->suspended_userdata = userdata;
1684 }
1685
1686 void pa_stream_set_started_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
1687     pa_assert(s);
1688     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1689
1690     if (s->state == PA_STREAM_TERMINATED || s->state == PA_STREAM_FAILED)
1691         return;
1692
1693     s->started_callback = cb;
1694     s->started_userdata = userdata;
1695 }
1696
1697 void pa_stream_simple_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1698     pa_operation *o = userdata;
1699     int success = 1;
1700
1701     pa_assert(pd);
1702     pa_assert(o);
1703     pa_assert(PA_REFCNT_VALUE(o) >= 1);
1704
1705     if (!o->context)
1706         goto finish;
1707
1708     if (command != PA_COMMAND_REPLY) {
1709         if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
1710             goto finish;
1711
1712         success = 0;
1713     } else if (!pa_tagstruct_eof(t)) {
1714         pa_context_fail(o->context, PA_ERR_PROTOCOL);
1715         goto finish;
1716     }
1717
1718     if (o->callback) {
1719         pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1720         cb(o->stream, success, o->userdata);
1721     }
1722
1723 finish:
1724     pa_operation_done(o);
1725     pa_operation_unref(o);
1726 }
1727
1728 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata) {
1729     pa_operation *o;
1730     pa_tagstruct *t;
1731     uint32_t tag;
1732
1733     pa_assert(s);
1734     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1735
1736     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1737     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1738
1739     s->corked = b;
1740
1741     o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1742
1743     t = pa_tagstruct_command(
1744             s->context,
1745             (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CORK_PLAYBACK_STREAM : PA_COMMAND_CORK_RECORD_STREAM),
1746             &tag);
1747     pa_tagstruct_putu32(t, s->channel);
1748     pa_tagstruct_put_boolean(t, !!b);
1749     pa_pstream_send_tagstruct(s->context->pstream, t);
1750     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);
1751
1752     check_smoother_status(s, FALSE, FALSE, FALSE);
1753
1754     /* This might cause the indexes to hang/start again, hence
1755      * let's request a timing update */
1756     request_auto_timing_update(s, TRUE);
1757
1758     return o;
1759 }
1760
1761 static pa_operation* stream_send_simple_command(pa_stream *s, uint32_t command, pa_stream_success_cb_t cb, void *userdata) {
1762     pa_tagstruct *t;
1763     pa_operation *o;
1764     uint32_t tag;
1765
1766     pa_assert(s);
1767     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1768
1769     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1770
1771     o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1772
1773     t = pa_tagstruct_command(s->context, command, &tag);
1774     pa_tagstruct_putu32(t, s->channel);
1775     pa_pstream_send_tagstruct(s->context->pstream, t);
1776     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);
1777
1778     return o;
1779 }
1780
1781 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1782     pa_operation *o;
1783
1784     pa_assert(s);
1785     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1786
1787     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1788     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1789
1790     if (!(o = stream_send_simple_command(s, (uint32_t) (s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_FLUSH_PLAYBACK_STREAM : PA_COMMAND_FLUSH_RECORD_STREAM), cb, userdata)))
1791         return NULL;
1792
1793     if (s->direction == PA_STREAM_PLAYBACK) {
1794
1795         if (s->write_index_corrections[s->current_write_index_correction].valid)
1796             s->write_index_corrections[s->current_write_index_correction].corrupt = TRUE;
1797
1798         if (s->buffer_attr.prebuf > 0)
1799             check_smoother_status(s, FALSE, FALSE, TRUE);
1800
1801         /* This will change the write index, but leave the
1802          * read index untouched. */
1803         invalidate_indexes(s, FALSE, TRUE);
1804
1805     } else
1806         /* For record streams this has no influence on the write
1807          * index, but the read index might jump. */
1808         invalidate_indexes(s, TRUE, FALSE);
1809
1810     return o;
1811 }
1812
1813 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1814     pa_operation *o;
1815
1816     pa_assert(s);
1817     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1818
1819     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1820     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1821     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1822
1823     if (!(o = stream_send_simple_command(s, PA_COMMAND_PREBUF_PLAYBACK_STREAM, cb, userdata)))
1824         return NULL;
1825
1826     /* This might cause the read index to hang again, hence
1827      * let's request a timing update */
1828     request_auto_timing_update(s, TRUE);
1829
1830     return o;
1831 }
1832
1833 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1834     pa_operation *o;
1835
1836     pa_assert(s);
1837     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1838
1839     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1840     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1841     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1842
1843     if (!(o = stream_send_simple_command(s, PA_COMMAND_TRIGGER_PLAYBACK_STREAM, cb, userdata)))
1844         return NULL;
1845
1846     /* This might cause the read index to start moving again, hence
1847      * let's request a timing update */
1848     request_auto_timing_update(s, TRUE);
1849
1850     return o;
1851 }
1852
1853 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata) {
1854     pa_operation *o;
1855
1856     pa_assert(s);
1857     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1858     pa_assert(name);
1859
1860     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1861     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1862
1863     if (s->context->version >= 13) {
1864         pa_proplist *p = pa_proplist_new();
1865
1866         pa_proplist_sets(p, PA_PROP_MEDIA_NAME, name);
1867         o = pa_stream_proplist_update(s, PA_UPDATE_REPLACE, p, cb, userdata);
1868         pa_proplist_free(p);
1869     } else {
1870         pa_tagstruct *t;
1871         uint32_t tag;
1872
1873         o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1874         t = pa_tagstruct_command(
1875                 s->context,
1876                 (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_NAME : PA_COMMAND_SET_PLAYBACK_STREAM_NAME),
1877                 &tag);
1878         pa_tagstruct_putu32(t, s->channel);
1879         pa_tagstruct_puts(t, name);
1880         pa_pstream_send_tagstruct(s->context->pstream, t);
1881         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);
1882     }
1883
1884     return o;
1885 }
1886
1887 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec) {
1888     pa_usec_t usec;
1889
1890     pa_assert(s);
1891     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1892
1893     PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1894     PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1895     PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1896     PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1897     PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1898
1899     if (s->smoother)
1900         usec = pa_smoother_get(s->smoother, pa_rtclock_usec());
1901     else
1902         usec = calc_time(s, FALSE);
1903
1904     /* Make sure the time runs monotonically */
1905     if (!(s->flags & PA_STREAM_NOT_MONOTONOUS)) {
1906         if (usec < s->previous_time)
1907             usec = s->previous_time;
1908         else
1909             s->previous_time = usec;
1910     }
1911
1912     if (r_usec)
1913         *r_usec = usec;
1914
1915     return 0;
1916 }
1917
1918 static pa_usec_t time_counter_diff(pa_stream *s, pa_usec_t a, pa_usec_t b, int *negative) {
1919     pa_assert(s);
1920     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1921
1922     if (negative)
1923         *negative = 0;
1924
1925     if (a >= b)
1926         return a-b;
1927     else {
1928         if (negative && s->direction == PA_STREAM_RECORD) {
1929             *negative = 1;
1930             return b-a;
1931         } else
1932             return 0;
1933     }
1934 }
1935
1936 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative) {
1937     pa_usec_t t, c;
1938     int r;
1939     int64_t cindex;
1940
1941     pa_assert(s);
1942     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1943     pa_assert(r_usec);
1944
1945     PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1946     PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1947     PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1948     PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1949     PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1950
1951     if ((r = pa_stream_get_time(s, &t)) < 0)
1952         return r;
1953
1954     if (s->direction == PA_STREAM_PLAYBACK)
1955         cindex = s->timing_info.write_index;
1956     else
1957         cindex = s->timing_info.read_index;
1958
1959     if (cindex < 0)
1960         cindex = 0;
1961
1962     c = pa_bytes_to_usec((uint64_t) cindex, &s->sample_spec);
1963
1964     if (s->direction == PA_STREAM_PLAYBACK)
1965         *r_usec = time_counter_diff(s, c, t, negative);
1966     else
1967         *r_usec = time_counter_diff(s, t, c, negative);
1968
1969     return 0;
1970 }
1971
1972 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s) {
1973     pa_assert(s);
1974     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1975
1976     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1977     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1978     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->timing_info_valid, PA_ERR_NODATA);
1979
1980     return &s->timing_info;
1981 }
1982
1983 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s) {
1984     pa_assert(s);
1985     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1986
1987     return &s->sample_spec;
1988 }
1989
1990 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s) {
1991     pa_assert(s);
1992     pa_assert(PA_REFCNT_VALUE(s) >= 1);
1993
1994     return &s->channel_map;
1995 }
1996
1997 const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s) {
1998     pa_assert(s);
1999     pa_assert(PA_REFCNT_VALUE(s) >= 1);
2000
2001     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2002     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2003     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 9, PA_ERR_NOTSUPPORTED);
2004
2005     return &s->buffer_attr;
2006 }
2007
2008 static void stream_set_buffer_attr_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2009     pa_operation *o = userdata;
2010     int success = 1;
2011
2012     pa_assert(pd);
2013     pa_assert(o);
2014     pa_assert(PA_REFCNT_VALUE(o) >= 1);
2015
2016     if (!o->context)
2017         goto finish;
2018
2019     if (command != PA_COMMAND_REPLY) {
2020         if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
2021             goto finish;
2022
2023         success = 0;
2024     } else {
2025         if (o->stream->direction == PA_STREAM_PLAYBACK) {
2026             if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
2027                 pa_tagstruct_getu32(t, &o->stream->buffer_attr.tlength) < 0 ||
2028                 pa_tagstruct_getu32(t, &o->stream->buffer_attr.prebuf) < 0 ||
2029                 pa_tagstruct_getu32(t, &o->stream->buffer_attr.minreq) < 0) {
2030                 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2031                 goto finish;
2032             }
2033         } else if (o->stream->direction == PA_STREAM_RECORD) {
2034             if (pa_tagstruct_getu32(t, &o->stream->buffer_attr.maxlength) < 0 ||
2035                 pa_tagstruct_getu32(t, &o->stream->buffer_attr.fragsize) < 0) {
2036                 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2037                 goto finish;
2038             }
2039         }
2040
2041         if (o->stream->context->version >= 13) {
2042             pa_usec_t usec;
2043
2044             if (pa_tagstruct_get_usec(t, &usec) < 0) {
2045                 pa_context_fail(o->context, PA_ERR_PROTOCOL);
2046                 goto finish;
2047             }
2048
2049             if (o->stream->direction == PA_STREAM_RECORD)
2050                 o->stream->timing_info.configured_source_usec = usec;
2051             else
2052                 o->stream->timing_info.configured_sink_usec = usec;
2053         }
2054
2055         if (!pa_tagstruct_eof(t)) {
2056             pa_context_fail(o->context, PA_ERR_PROTOCOL);
2057             goto finish;
2058         }
2059     }
2060
2061     if (o->callback) {
2062         pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
2063         cb(o->stream, success, o->userdata);
2064     }
2065
2066 finish:
2067     pa_operation_done(o);
2068     pa_operation_unref(o);
2069 }
2070
2071
2072 pa_operation* pa_stream_set_buffer_attr(pa_stream *s, const pa_buffer_attr *attr, pa_stream_success_cb_t cb, void *userdata) {
2073     pa_operation *o;
2074     pa_tagstruct *t;
2075     uint32_t tag;
2076
2077     pa_assert(s);
2078     pa_assert(PA_REFCNT_VALUE(s) >= 1);
2079     pa_assert(attr);
2080
2081     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2082     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2083     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2084
2085     o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2086
2087     t = pa_tagstruct_command(
2088             s->context,
2089             (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_BUFFER_ATTR : PA_COMMAND_SET_PLAYBACK_STREAM_BUFFER_ATTR),
2090             &tag);
2091     pa_tagstruct_putu32(t, s->channel);
2092
2093     pa_tagstruct_putu32(t, attr->maxlength);
2094
2095     if (s->direction == PA_STREAM_PLAYBACK)
2096         pa_tagstruct_put(
2097                 t,
2098                 PA_TAG_U32, attr->tlength,
2099                 PA_TAG_U32, attr->prebuf,
2100                 PA_TAG_U32, attr->minreq,
2101                 PA_TAG_INVALID);
2102     else
2103         pa_tagstruct_putu32(t, attr->fragsize);
2104
2105     if (s->context->version >= 13)
2106         pa_tagstruct_put_boolean(t, !!(s->flags & PA_STREAM_ADJUST_LATENCY));
2107
2108     if (s->context->version >= 14)
2109         pa_tagstruct_put_boolean(t, !!(s->flags & PA_STREAM_EARLY_REQUESTS));
2110
2111     pa_pstream_send_tagstruct(s->context->pstream, t);
2112     pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_set_buffer_attr_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
2113
2114     /* This might cause changes in the read/write indexex, hence let's
2115      * request a timing update */
2116     request_auto_timing_update(s, TRUE);
2117
2118     return o;
2119 }
2120
2121 uint32_t pa_stream_get_device_index(pa_stream *s) {
2122     pa_assert(s);
2123     pa_assert(PA_REFCNT_VALUE(s) >= 1);
2124
2125     PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2126     PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2127     PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
2128     PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->device_index != PA_INVALID_INDEX, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2129
2130     return s->device_index;
2131 }
2132
2133 const char *pa_stream_get_device_name(pa_stream *s) {
2134     pa_assert(s);
2135     pa_assert(PA_REFCNT_VALUE(s) >= 1);
2136
2137     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2138     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2139     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2140     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->device_name, PA_ERR_BADSTATE);
2141
2142     return s->device_name;
2143 }
2144
2145 int pa_stream_is_suspended(pa_stream *s) {
2146     pa_assert(s);
2147     pa_assert(PA_REFCNT_VALUE(s) >= 1);
2148
2149     PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2150     PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2151     PA_CHECK_VALIDITY(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2152
2153     return s->suspended;
2154 }
2155
2156 int pa_stream_is_corked(pa_stream *s) {
2157     pa_assert(s);
2158     pa_assert(PA_REFCNT_VALUE(s) >= 1);
2159
2160     PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2161     PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2162
2163     return s->corked;
2164 }
2165
2166 static void stream_update_sample_rate_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
2167     pa_operation *o = userdata;
2168     int success = 1;
2169
2170     pa_assert(pd);
2171     pa_assert(o);
2172     pa_assert(PA_REFCNT_VALUE(o) >= 1);
2173
2174     if (!o->context)
2175         goto finish;
2176
2177     if (command != PA_COMMAND_REPLY) {
2178         if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
2179             goto finish;
2180
2181         success = 0;
2182     } else {
2183
2184         if (!pa_tagstruct_eof(t)) {
2185             pa_context_fail(o->context, PA_ERR_PROTOCOL);
2186             goto finish;
2187         }
2188     }
2189
2190     o->stream->sample_spec.rate = PA_PTR_TO_UINT(o->private);
2191     pa_assert(pa_sample_spec_valid(&o->stream->sample_spec));
2192
2193     if (o->callback) {
2194         pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
2195         cb(o->stream, success, o->userdata);
2196     }
2197
2198 finish:
2199     pa_operation_done(o);
2200     pa_operation_unref(o);
2201 }
2202
2203
2204 pa_operation *pa_stream_update_sample_rate(pa_stream *s, uint32_t rate, pa_stream_success_cb_t cb, void *userdata) {
2205     pa_operation *o;
2206     pa_tagstruct *t;
2207     uint32_t tag;
2208
2209     pa_assert(s);
2210     pa_assert(PA_REFCNT_VALUE(s) >= 1);
2211
2212     PA_CHECK_VALIDITY_RETURN_NULL(s->context, rate > 0 && rate <= PA_RATE_MAX, PA_ERR_INVALID);
2213     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2214     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2215     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->flags & PA_STREAM_VARIABLE_RATE, PA_ERR_BADSTATE);
2216     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 12, PA_ERR_NOTSUPPORTED);
2217
2218     o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2219     o->private = PA_UINT_TO_PTR(rate);
2220
2221     t = pa_tagstruct_command(
2222             s->context,
2223             (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_UPDATE_RECORD_STREAM_SAMPLE_RATE : PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE),
2224             &tag);
2225     pa_tagstruct_putu32(t, s->channel);
2226     pa_tagstruct_putu32(t, rate);
2227
2228     pa_pstream_send_tagstruct(s->context->pstream, t);
2229     pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_update_sample_rate_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
2230
2231     return o;
2232 }
2233
2234 pa_operation *pa_stream_proplist_update(pa_stream *s, pa_update_mode_t mode, pa_proplist *p, pa_stream_success_cb_t cb, void *userdata) {
2235     pa_operation *o;
2236     pa_tagstruct *t;
2237     uint32_t tag;
2238
2239     pa_assert(s);
2240     pa_assert(PA_REFCNT_VALUE(s) >= 1);
2241
2242     PA_CHECK_VALIDITY_RETURN_NULL(s->context, mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE, PA_ERR_INVALID);
2243     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2244     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2245     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2246
2247     o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2248
2249     t = pa_tagstruct_command(
2250             s->context,
2251             (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_UPDATE_RECORD_STREAM_PROPLIST : PA_COMMAND_UPDATE_PLAYBACK_STREAM_PROPLIST),
2252             &tag);
2253     pa_tagstruct_putu32(t, s->channel);
2254     pa_tagstruct_putu32(t, (uint32_t) mode);
2255     pa_tagstruct_put_proplist(t, p);
2256
2257     pa_pstream_send_tagstruct(s->context->pstream, t);
2258     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);
2259
2260     /* Please note that we don't update s->proplist here, because we
2261      * don't export that field */
2262
2263     return o;
2264 }
2265
2266 pa_operation *pa_stream_proplist_remove(pa_stream *s, const char *const keys[], pa_stream_success_cb_t cb, void *userdata) {
2267     pa_operation *o;
2268     pa_tagstruct *t;
2269     uint32_t tag;
2270     const char * const*k;
2271
2272     pa_assert(s);
2273     pa_assert(PA_REFCNT_VALUE(s) >= 1);
2274
2275     PA_CHECK_VALIDITY_RETURN_NULL(s->context, keys && keys[0], PA_ERR_INVALID);
2276     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
2277     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
2278     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2279
2280     o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
2281
2282     t = pa_tagstruct_command(
2283             s->context,
2284             (uint32_t) (s->direction == PA_STREAM_RECORD ? PA_COMMAND_REMOVE_RECORD_STREAM_PROPLIST : PA_COMMAND_REMOVE_PLAYBACK_STREAM_PROPLIST),
2285             &tag);
2286     pa_tagstruct_putu32(t, s->channel);
2287
2288     for (k = keys; *k; k++)
2289         pa_tagstruct_puts(t, *k);
2290
2291     pa_tagstruct_puts(t, NULL);
2292
2293     pa_pstream_send_tagstruct(s->context->pstream, t);
2294     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);
2295
2296     /* Please note that we don't update s->proplist here, because we
2297      * don't export that field */
2298
2299     return o;
2300 }
2301
2302 int pa_stream_set_monitor_stream(pa_stream *s, uint32_t sink_input_idx) {
2303     pa_assert(s);
2304     pa_assert(PA_REFCNT_VALUE(s) >= 1);
2305
2306     PA_CHECK_VALIDITY(s->context, sink_input_idx != PA_INVALID_INDEX, PA_ERR_INVALID);
2307     PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
2308     PA_CHECK_VALIDITY(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED);
2309
2310     s->direct_on_input = sink_input_idx;
2311
2312     return 0;
2313 }
2314
2315 uint32_t pa_stream_get_monitor_stream(pa_stream *s) {
2316     pa_assert(s);
2317     pa_assert(PA_REFCNT_VALUE(s) >= 1);
2318
2319     PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direct_on_input != PA_INVALID_INDEX, PA_ERR_BADSTATE, PA_INVALID_INDEX);
2320     PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->context->version >= 13, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
2321
2322     return s->direct_on_input;
2323 }