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