]> code.delx.au - pulseaudio/blob - src/polyp/stream.c
* rename "latency correction" to "write index correction"
[pulseaudio] / src / polyp / stream.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <polyp/def.h>
32 #include <polypcore/xmalloc.h>
33 #include <polypcore/pstream-util.h>
34 #include <polypcore/util.h>
35 #include <polypcore/log.h>
36 #include <polypcore/hashmap.h>
37
38 #include "internal.h"
39
40 #define LATENCY_IPOL_INTERVAL_USEC (10000L)
41
42 pa_stream *pa_stream_new(pa_context *c, const char *name, const pa_sample_spec *ss, const pa_channel_map *map) {
43 pa_stream *s;
44 int i;
45
46 assert(c);
47
48 PA_CHECK_VALIDITY_RETURN_NULL(c, ss && pa_sample_spec_valid(ss), PA_ERR_INVALID);
49 PA_CHECK_VALIDITY_RETURN_NULL(c, !map || (pa_channel_map_valid(map) && map->channels == ss->channels), PA_ERR_INVALID);
50
51 s = pa_xnew(pa_stream, 1);
52 s->ref = 1;
53 s->context = c;
54 s->mainloop = c->mainloop;
55
56 s->read_callback = NULL;
57 s->read_userdata = NULL;
58 s->write_callback = NULL;
59 s->write_userdata = NULL;
60 s->state_callback = NULL;
61 s->state_userdata = NULL;
62 s->overflow_callback = NULL;
63 s->overflow_userdata = NULL;
64 s->underflow_callback = NULL;
65 s->underflow_userdata = NULL;
66
67 s->direction = PA_STREAM_NODIRECTION;
68 s->name = pa_xstrdup(name);
69 s->sample_spec = *ss;
70 s->flags = 0;
71
72 if (map)
73 s->channel_map = *map;
74 else
75 pa_channel_map_init_auto(&s->channel_map, ss->channels);
76
77 s->channel = 0;
78 s->channel_valid = 0;
79 s->syncid = c->csyncid++;
80 s->device_index = PA_INVALID_INDEX;
81 s->requested_bytes = 0;
82 s->state = PA_STREAM_UNCONNECTED;
83 memset(&s->buffer_attr, 0, sizeof(s->buffer_attr));
84
85 s->peek_memchunk.index = 0;
86 s->peek_memchunk.length = 0;
87 s->peek_memchunk.memblock = NULL;
88
89 s->record_memblockq = NULL;
90
91 s->previous_time = 0;
92 s->timing_info_valid = 0;
93 s->read_index_not_before = 0;
94 s->write_index_not_before = 0;
95
96 for (i = 0; i < PA_MAX_WRITE_INDEX_CORRECTIONS; i++)
97 s->write_index_corrections[i].valid = 0;
98 s->current_write_index_correction = 0;
99
100 s->corked = 0;
101
102 s->ipol_usec_valid = 0;
103 s->ipol_timestamp.tv_sec = 0;
104 s->ipol_timestamp.tv_usec = 0;
105
106 s->auto_timing_update_event = NULL;
107 s->auto_timing_update_requested = 0;
108
109 PA_LLIST_PREPEND(pa_stream, c->streams, s);
110
111 /* The context and stream will point at each other. We cannot ref count
112 both though since that will create a loop. */
113 pa_context_ref(s->context);
114
115 return s;
116 }
117
118 static void stream_free(pa_stream *s) {
119 assert(s && s->context && !s->channel_valid);
120
121 PA_LLIST_REMOVE(pa_stream, s->context->streams, s);
122
123 pa_context_unref(s->context);
124
125 if (s->auto_timing_update_event) {
126 assert(s->mainloop);
127 s->mainloop->time_free(s->auto_timing_update_event);
128 }
129
130 if (s->peek_memchunk.memblock)
131 pa_memblock_unref(s->peek_memchunk.memblock);
132
133 if (s->record_memblockq)
134 pa_memblockq_free(s->record_memblockq);
135
136 pa_xfree(s->name);
137 pa_xfree(s);
138 }
139
140 void pa_stream_unref(pa_stream *s) {
141 assert(s);
142 assert(s->ref >= 1);
143
144 if (--(s->ref) == 0)
145 stream_free(s);
146 }
147
148 pa_stream* pa_stream_ref(pa_stream *s) {
149 assert(s);
150 assert(s->ref >= 1);
151
152 s->ref++;
153 return s;
154 }
155
156 pa_stream_state_t pa_stream_get_state(pa_stream *s) {
157 assert(s);
158 assert(s->ref >= 1);
159
160 return s->state;
161 }
162
163 pa_context* pa_stream_get_context(pa_stream *s) {
164 assert(s);
165 assert(s->ref >= 1);
166
167 return s->context;
168 }
169
170 uint32_t pa_stream_get_index(pa_stream *s) {
171 assert(s);
172 assert(s->ref >= 1);
173
174 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
175
176 return s->device_index;
177 }
178
179 void pa_stream_set_state(pa_stream *s, pa_stream_state_t st) {
180 assert(s);
181 assert(s->ref >= 1);
182
183 if (s->state == st)
184 return;
185
186 pa_stream_ref(s);
187
188 s->state = st;
189
190 if ((st == PA_STREAM_FAILED || st == PA_STREAM_TERMINATED) && s->context) {
191 /* Detach from context */
192
193 if (s->channel_valid)
194 pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, NULL);
195
196 s->channel = 0;
197 s->channel_valid = 0;
198
199 /* We keep a ref as long as we're connected */
200 pa_stream_unref(s);
201 }
202
203 if (s->state_callback)
204 s->state_callback(s, s->state_userdata);
205
206 pa_stream_unref(s);
207 }
208
209 void pa_command_stream_killed(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
210 pa_context *c = userdata;
211 pa_stream *s;
212 uint32_t channel;
213
214 assert(pd);
215 assert(command == PA_COMMAND_PLAYBACK_STREAM_KILLED || command == PA_COMMAND_RECORD_STREAM_KILLED);
216 assert(t);
217 assert(c);
218
219 pa_context_ref(c);
220
221 if (pa_tagstruct_getu32(t, &channel) < 0 ||
222 !pa_tagstruct_eof(t)) {
223 pa_context_fail(c, PA_ERR_PROTOCOL);
224 goto finish;
225 }
226
227 if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel)))
228 goto finish;
229
230 pa_context_set_error(c, PA_ERR_KILLED);
231 pa_stream_set_state(s, PA_STREAM_FAILED);
232
233 finish:
234 pa_context_unref(c);
235 }
236
237 void pa_command_request(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
238 pa_stream *s;
239 pa_context *c = userdata;
240 uint32_t bytes, channel;
241
242 assert(pd);
243 assert(command == PA_COMMAND_REQUEST);
244 assert(t);
245 assert(c);
246
247 pa_context_ref(c);
248
249 if (pa_tagstruct_getu32(t, &channel) < 0 ||
250 pa_tagstruct_getu32(t, &bytes) < 0 ||
251 !pa_tagstruct_eof(t)) {
252 pa_context_fail(c, PA_ERR_PROTOCOL);
253 goto finish;
254 }
255
256 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
257 goto finish;
258
259 if (s->state == PA_STREAM_READY) {
260 s->requested_bytes += bytes;
261
262 if (s->requested_bytes > 0 && s->write_callback)
263 s->write_callback(s, s->requested_bytes, s->write_userdata);
264 }
265
266 finish:
267 pa_context_unref(c);
268 }
269
270 void pa_command_overflow_or_underflow(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
271 pa_stream *s;
272 pa_context *c = userdata;
273 uint32_t channel;
274
275 assert(pd);
276 assert(command == PA_COMMAND_OVERFLOW || command == PA_COMMAND_UNDERFLOW);
277 assert(t);
278 assert(c);
279
280 pa_context_ref(c);
281
282 if (pa_tagstruct_getu32(t, &channel) < 0 ||
283 !pa_tagstruct_eof(t)) {
284 pa_context_fail(c, PA_ERR_PROTOCOL);
285 goto finish;
286 }
287
288 if (!(s = pa_dynarray_get(c->playback_streams, channel)))
289 goto finish;
290
291 if (s->state == PA_STREAM_READY) {
292
293 if (command == PA_COMMAND_OVERFLOW) {
294 if (s->overflow_callback)
295 s->overflow_callback(s, s->overflow_userdata);
296 } else if (command == PA_COMMAND_UNDERFLOW) {
297 if (s->underflow_callback)
298 s->underflow_callback(s, s->underflow_userdata);
299 }
300 }
301
302 finish:
303 pa_context_unref(c);
304 }
305
306 static void request_auto_timing_update(pa_stream *s, int force) {
307 struct timeval next;
308 assert(s);
309
310 if (!(s->flags & PA_STREAM_AUTO_TIMING_UPDATE))
311 return;
312
313 if (s->state == PA_STREAM_READY &&
314 (force || !s->auto_timing_update_requested)) {
315 pa_operation *o;
316
317 /* pa_log("automatically requesting new timing data"); */
318
319 if ((o = pa_stream_update_timing_info(s, NULL, NULL))) {
320 pa_operation_unref(o);
321 s->auto_timing_update_requested = 1;
322 }
323 }
324
325 pa_gettimeofday(&next);
326 pa_timeval_add(&next, LATENCY_IPOL_INTERVAL_USEC);
327 s->mainloop->time_restart(s->auto_timing_update_event, &next);
328 }
329
330 static void invalidate_indexes(pa_stream *s, int r, int w) {
331 assert(s);
332
333 pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag);
334
335 if (s->state != PA_STREAM_READY)
336 return;
337
338 if (w) {
339 s->write_index_not_before = s->context->ctag;
340
341 if (s->timing_info_valid)
342 s->timing_info.write_index_corrupt = 1;
343
344 pa_log("write_index invalidated");
345
346 }
347
348 if (r) {
349 s->read_index_not_before = s->context->ctag;
350
351 if (s->timing_info_valid)
352 s->timing_info.read_index_corrupt = 1;
353
354 pa_log("read_index invalidated");
355 }
356
357 if ((s->direction == PA_STREAM_PLAYBACK && r) ||
358 (s->direction == PA_STREAM_RECORD && w))
359 s->ipol_usec_valid = 0;
360
361 request_auto_timing_update(s, 1);
362 }
363
364 static void auto_timing_update_callback(pa_mainloop_api *m, pa_time_event *e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
365 pa_stream *s = userdata;
366
367 pa_log("time event");
368
369 pa_stream_ref(s);
370 request_auto_timing_update(s, 0);
371 pa_stream_unref(s);
372 }
373
374 void pa_create_stream_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
375 pa_stream *s = userdata;
376
377 assert(pd);
378 assert(t);
379 assert(s);
380 assert(s->state == PA_STREAM_CREATING);
381
382 pa_stream_ref(s);
383
384 if (command != PA_COMMAND_REPLY) {
385 if (pa_context_handle_error(s->context, command, t) < 0)
386 goto finish;
387
388 pa_stream_set_state(s, PA_STREAM_FAILED);
389 goto finish;
390 }
391
392 if (pa_tagstruct_getu32(t, &s->channel) < 0 ||
393 ((s->direction != PA_STREAM_UPLOAD) && pa_tagstruct_getu32(t, &s->device_index) < 0) ||
394 ((s->direction != PA_STREAM_RECORD) && pa_tagstruct_getu32(t, &s->requested_bytes) < 0) ||
395 !pa_tagstruct_eof(t)) {
396 pa_context_fail(s->context, PA_ERR_PROTOCOL);
397 goto finish;
398 }
399
400 if (s->direction == PA_STREAM_RECORD) {
401 assert(!s->record_memblockq);
402
403 s->record_memblockq = pa_memblockq_new(
404 0,
405 s->buffer_attr.maxlength,
406 0,
407 pa_frame_size(&s->sample_spec),
408 1,
409 0,
410 NULL,
411 s->context->memblock_stat);
412 }
413
414 s->channel_valid = 1;
415 pa_dynarray_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, s->channel, s);
416
417 /* We add an extra ref as long as we're connected (i.e. in the dynarray) */
418 pa_stream_ref(s);
419
420 pa_stream_set_state(s, PA_STREAM_READY);
421
422 if (s->direction != PA_STREAM_UPLOAD &&
423 s->flags & PA_STREAM_AUTO_TIMING_UPDATE) {
424 struct timeval tv;
425
426 pa_gettimeofday(&tv);
427 tv.tv_usec += LATENCY_IPOL_INTERVAL_USEC; /* every 100 ms */
428
429 assert(!s->auto_timing_update_event);
430 s->auto_timing_update_event = s->mainloop->time_new(s->mainloop, &tv, &auto_timing_update_callback, s);
431
432 request_auto_timing_update(s, 1);
433 }
434
435 if (s->requested_bytes > 0 && s->ref > 1 && s->write_callback)
436 s->write_callback(s, s->requested_bytes, s->write_userdata);
437
438 finish:
439 pa_stream_unref(s);
440 }
441
442 static int create_stream(
443 pa_stream_direction_t direction,
444 pa_stream *s,
445 const char *dev,
446 const pa_buffer_attr *attr,
447 pa_stream_flags_t flags,
448 const pa_cvolume *volume,
449 pa_stream *sync_stream) {
450
451 pa_tagstruct *t;
452 uint32_t tag;
453
454 assert(s);
455 assert(s->ref >= 1);
456
457 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
458 PA_CHECK_VALIDITY(s->context, !(flags & ~((direction != PA_STREAM_UPLOAD ?
459 PA_STREAM_START_CORKED|
460 PA_STREAM_INTERPOLATE_TIMING|
461 PA_STREAM_NOT_MONOTONOUS|
462 PA_STREAM_AUTO_TIMING_UPDATE : 0))), PA_ERR_INVALID);
463 PA_CHECK_VALIDITY(s->context, direction == PA_STREAM_PLAYBACK || flags == 0, PA_ERR_INVALID);
464 PA_CHECK_VALIDITY(s->context, !volume || volume->channels == s->sample_spec.channels, PA_ERR_INVALID);
465 PA_CHECK_VALIDITY(s->context, !sync_stream || (direction == PA_STREAM_PLAYBACK && sync_stream->direction == PA_STREAM_PLAYBACK), PA_ERR_INVALID);
466
467 pa_stream_ref(s);
468
469 s->direction = direction;
470 s->flags = flags;
471
472 if (sync_stream)
473 s->syncid = sync_stream->syncid;
474
475 if (attr)
476 s->buffer_attr = *attr;
477 else {
478 /* half a second */
479 s->buffer_attr.tlength = pa_bytes_per_second(&s->sample_spec)/2;
480 s->buffer_attr.maxlength = (s->buffer_attr.tlength*3)/2;
481 s->buffer_attr.minreq = s->buffer_attr.tlength/100;
482 s->buffer_attr.prebuf = s->buffer_attr.tlength - s->buffer_attr.minreq;
483 s->buffer_attr.fragsize = s->buffer_attr.tlength/100;
484 }
485
486 if (!dev)
487 dev = s->direction == PA_STREAM_PLAYBACK ? s->context->conf->default_sink : s->context->conf->default_source;
488
489 t = pa_tagstruct_command(
490 s->context,
491 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM,
492 &tag);
493
494 pa_tagstruct_put(
495 t,
496 PA_TAG_STRING, s->name,
497 PA_TAG_SAMPLE_SPEC, &s->sample_spec,
498 PA_TAG_CHANNEL_MAP, &s->channel_map,
499 PA_TAG_U32, PA_INVALID_INDEX,
500 PA_TAG_STRING, dev,
501 PA_TAG_U32, s->buffer_attr.maxlength,
502 PA_TAG_BOOLEAN, !!(flags & PA_STREAM_START_CORKED),
503 PA_TAG_INVALID);
504
505 if (s->direction == PA_STREAM_PLAYBACK) {
506 pa_cvolume cv;
507
508 pa_tagstruct_put(
509 t,
510 PA_TAG_U32, s->buffer_attr.tlength,
511 PA_TAG_U32, s->buffer_attr.prebuf,
512 PA_TAG_U32, s->buffer_attr.minreq,
513 PA_TAG_U32, s->syncid,
514 PA_TAG_INVALID);
515
516 if (!volume)
517 volume = pa_cvolume_reset(&cv, s->sample_spec.channels);
518
519 pa_tagstruct_put_cvolume(t, volume);
520 } else
521 pa_tagstruct_putu32(t, s->buffer_attr.fragsize);
522
523 pa_pstream_send_tagstruct(s->context->pstream, t);
524 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s);
525
526 pa_stream_set_state(s, PA_STREAM_CREATING);
527
528 pa_stream_unref(s);
529 return 0;
530 }
531
532 int pa_stream_connect_playback(
533 pa_stream *s,
534 const char *dev,
535 const pa_buffer_attr *attr,
536 pa_stream_flags_t flags,
537 pa_cvolume *volume,
538 pa_stream *sync_stream) {
539
540 assert(s);
541 assert(s->ref >= 1);
542
543 return create_stream(PA_STREAM_PLAYBACK, s, dev, attr, flags, volume, sync_stream);
544 }
545
546 int pa_stream_connect_record(
547 pa_stream *s,
548 const char *dev,
549 const pa_buffer_attr *attr,
550 pa_stream_flags_t flags) {
551
552 assert(s);
553 assert(s->ref >= 1);
554
555 return create_stream(PA_STREAM_RECORD, s, dev, attr, flags, NULL, NULL);
556 }
557
558 int pa_stream_write(
559 pa_stream *s,
560 const void *data,
561 size_t length,
562 void (*free_cb)(void *p),
563 int64_t offset,
564 pa_seek_mode_t seek) {
565
566 pa_memchunk chunk;
567
568 assert(s);
569 assert(s->ref >= 1);
570 assert(data);
571
572 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
573 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || s->direction == PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
574 PA_CHECK_VALIDITY(s->context, seek <= PA_SEEK_RELATIVE_END, PA_ERR_INVALID);
575 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_PLAYBACK || (seek == PA_SEEK_RELATIVE && offset == 0), PA_ERR_INVALID);
576
577 if (length <= 0)
578 return 0;
579
580 if (free_cb)
581 chunk.memblock = pa_memblock_new_user((void*) data, length, free_cb, 1, s->context->memblock_stat);
582 else {
583 chunk.memblock = pa_memblock_new(length, s->context->memblock_stat);
584 memcpy(chunk.memblock->data, data, length);
585 }
586
587 chunk.index = 0;
588 chunk.length = length;
589
590 pa_pstream_send_memblock(s->context->pstream, s->channel, offset, seek, &chunk);
591 pa_memblock_unref(chunk.memblock);
592
593 if (length < s->requested_bytes)
594 s->requested_bytes -= length;
595 else
596 s->requested_bytes = 0;
597
598 if (s->direction == PA_STREAM_PLAYBACK) {
599
600 /* Update latency request correction */
601 if (s->write_index_corrections[s->current_write_index_correction].valid) {
602
603 if (seek == PA_SEEK_ABSOLUTE) {
604 s->write_index_corrections[s->current_write_index_correction].corrupt = 0;
605 s->write_index_corrections[s->current_write_index_correction].absolute = 1;
606 s->write_index_corrections[s->current_write_index_correction].value = offset + length;
607 } else if (seek == PA_SEEK_RELATIVE) {
608 if (!s->write_index_corrections[s->current_write_index_correction].corrupt)
609 s->write_index_corrections[s->current_write_index_correction].value += offset + length;
610 } else
611 s->write_index_corrections[s->current_write_index_correction].corrupt = 1;
612 }
613
614 /* Update the write index in the already available latency data */
615 if (s->timing_info_valid) {
616
617 if (seek == PA_SEEK_ABSOLUTE) {
618 s->timing_info.write_index_corrupt = 0;
619 s->timing_info.write_index = offset + length;
620 } else if (seek == PA_SEEK_RELATIVE) {
621 if (!s->timing_info.write_index_corrupt)
622 s->timing_info.write_index += offset + length;
623 } else
624 s->timing_info.write_index_corrupt = 1;
625 }
626
627 if (!s->timing_info_valid || s->timing_info.write_index_corrupt)
628 request_auto_timing_update(s, 1);
629 }
630
631 return 0;
632 }
633
634 int pa_stream_peek(pa_stream *s, const void **data, size_t *length) {
635 assert(s);
636 assert(s->ref >= 1);
637 assert(data);
638 assert(length);
639
640 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
641 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
642
643 if (!s->peek_memchunk.memblock) {
644
645 if (pa_memblockq_peek(s->record_memblockq, &s->peek_memchunk) < 0) {
646 *data = NULL;
647 *length = 0;
648 return 0;
649 }
650 }
651
652 *data = (const char*) s->peek_memchunk.memblock->data + s->peek_memchunk.index;
653 *length = s->peek_memchunk.length;
654 return 0;
655 }
656
657 int pa_stream_drop(pa_stream *s) {
658 assert(s);
659 assert(s->ref >= 1);
660
661 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
662 PA_CHECK_VALIDITY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE);
663 PA_CHECK_VALIDITY(s->context, s->peek_memchunk.memblock, PA_ERR_BADSTATE);
664
665 pa_memblockq_drop(s->record_memblockq, &s->peek_memchunk, s->peek_memchunk.length);
666
667 pa_memblock_unref(s->peek_memchunk.memblock);
668 s->peek_memchunk.length = 0;
669 s->peek_memchunk.index = 0;
670 s->peek_memchunk.memblock = NULL;
671
672 return 0;
673 }
674
675 size_t pa_stream_writable_size(pa_stream *s) {
676 assert(s);
677 assert(s->ref >= 1);
678
679 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
680 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction != PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
681
682 return s->requested_bytes;
683 }
684
685 size_t pa_stream_readable_size(pa_stream *s) {
686 assert(s);
687 assert(s->ref >= 1);
688
689 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE, (size_t) -1);
690 PA_CHECK_VALIDITY_RETURN_ANY(s->context, s->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, (size_t) -1);
691
692 return pa_memblockq_get_length(s->record_memblockq);
693 }
694
695 pa_operation * pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
696 pa_operation *o;
697 pa_tagstruct *t;
698 uint32_t tag;
699
700 assert(s);
701 assert(s->ref >= 1);
702
703 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
704 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
705
706 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
707
708 t = pa_tagstruct_command(s->context, PA_COMMAND_DRAIN_PLAYBACK_STREAM, &tag);
709 pa_tagstruct_putu32(t, s->channel);
710 pa_pstream_send_tagstruct(s->context->pstream, t);
711 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, o);
712
713 return pa_operation_ref(o);
714 }
715
716 static void stream_get_timing_info_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
717 pa_operation *o = userdata;
718 struct timeval local, remote, now;
719 pa_timing_info *i;
720
721 assert(pd);
722 assert(o);
723 assert(o->stream);
724 assert(o->context);
725
726 i = &o->stream->timing_info;
727
728 pa_log("pre corrupt w:%u r:%u\n", !o->stream->timing_info_valid || i->write_index_corrupt,!o->stream->timing_info_valid || i->read_index_corrupt);
729
730 o->stream->timing_info_valid = 0;
731 i->write_index_corrupt = 0;
732 i->read_index_corrupt = 0;
733
734 pa_log("timing update %u\n", tag);
735
736 if (command != PA_COMMAND_REPLY) {
737 if (pa_context_handle_error(o->context, command, t) < 0)
738 goto finish;
739
740 } else if (pa_tagstruct_get_usec(t, &i->sink_usec) < 0 ||
741 pa_tagstruct_get_usec(t, &i->source_usec) < 0 ||
742 pa_tagstruct_get_boolean(t, &i->playing) < 0 ||
743 pa_tagstruct_get_timeval(t, &local) < 0 ||
744 pa_tagstruct_get_timeval(t, &remote) < 0 ||
745 pa_tagstruct_gets64(t, &i->write_index) < 0 ||
746 pa_tagstruct_gets64(t, &i->read_index) < 0 ||
747 !pa_tagstruct_eof(t)) {
748 pa_context_fail(o->context, PA_ERR_PROTOCOL);
749 goto finish;
750
751 } else {
752 o->stream->timing_info_valid = 1;
753
754 pa_gettimeofday(&now);
755
756 /* Calculcate timestamps */
757 if (pa_timeval_cmp(&local, &remote) <= 0 && pa_timeval_cmp(&remote, &now) <= 0) {
758 /* local and remote seem to have synchronized clocks */
759
760 if (o->stream->direction == PA_STREAM_PLAYBACK)
761 i->transport_usec = pa_timeval_diff(&remote, &local);
762 else
763 i->transport_usec = pa_timeval_diff(&now, &remote);
764
765 i->synchronized_clocks = 1;
766 i->timestamp = remote;
767 } else {
768 /* clocks are not synchronized, let's estimate latency then */
769 i->transport_usec = pa_timeval_diff(&now, &local)/2;
770 i->synchronized_clocks = 0;
771 i->timestamp = local;
772 pa_timeval_add(&i->timestamp, i->transport_usec);
773 }
774
775 /* Invalidate read and write indexes if necessary */
776 if (tag < o->stream->read_index_not_before)
777 i->read_index_corrupt = 1;
778
779 if (tag < o->stream->write_index_not_before)
780 i->write_index_corrupt = 1;
781
782 if (o->stream->direction == PA_STREAM_PLAYBACK) {
783 /* Write index correction */
784
785 int n, j;
786 uint32_t ctag = tag;
787
788 /* Go through the saved correction values and add up the total correction.*/
789
790 for (n = 0, j = o->stream->current_write_index_correction+1;
791 n < PA_MAX_WRITE_INDEX_CORRECTIONS;
792 n++, j = (j + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS) {
793
794 /* Step over invalid data or out-of-date data */
795 if (!o->stream->write_index_corrections[j].valid ||
796 o->stream->write_index_corrections[j].tag < ctag)
797 continue;
798
799 /* Make sure that everything is in order */
800 ctag = o->stream->write_index_corrections[j].tag+1;
801
802 /* Now fix the write index */
803 if (o->stream->write_index_corrections[j].corrupt) {
804 /* A corrupting seek was made */
805 i->write_index = 0;
806 i->write_index_corrupt = 1;
807 } else if (o->stream->write_index_corrections[j].absolute) {
808 /* An absolute seek was made */
809 i->write_index = o->stream->write_index_corrections[j].value;
810 i->write_index_corrupt = 0;
811 } else if (!i->write_index_corrupt) {
812 /* A relative seek was made */
813 i->write_index += o->stream->write_index_corrections[j].value;
814 }
815 }
816 }
817
818 o->stream->ipol_timestamp = now;
819 o->stream->ipol_usec_valid = 0;
820 }
821
822 o->stream->auto_timing_update_requested = 0;
823 pa_log("post corrupt w:%u r:%u\n", i->write_index_corrupt || !o->stream->timing_info_valid, i->read_index_corrupt || !o->stream->timing_info_valid);
824
825 /* Clear old correction entries */
826 if (o->stream->direction == PA_STREAM_PLAYBACK) {
827 int n;
828
829 for (n = 0; n < PA_MAX_WRITE_INDEX_CORRECTIONS; n++) {
830 if (!o->stream->write_index_corrections[n].valid)
831 continue;
832
833 if (o->stream->write_index_corrections[n].tag <= tag)
834 o->stream->write_index_corrections[n].valid = 0;
835 }
836 }
837
838 if (o->callback) {
839 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
840 cb(o->stream, o->stream->timing_info_valid, o->userdata);
841 }
842
843 finish:
844
845 pa_operation_done(o);
846 pa_operation_unref(o);
847 }
848
849 pa_operation* pa_stream_update_timing_info(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
850 uint32_t tag;
851 pa_operation *o;
852 pa_tagstruct *t;
853 struct timeval now;
854 int cidx;
855
856 assert(s);
857 assert(s->ref >= 1);
858
859 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
860 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
861
862 if (s->direction == PA_STREAM_PLAYBACK) {
863 /* Find a place to store the write_index correction data for this entry */
864 cidx = (s->current_write_index_correction + 1) % PA_MAX_WRITE_INDEX_CORRECTIONS;
865
866 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
867 PA_CHECK_VALIDITY_RETURN_NULL(s->context, !s->write_index_corrections[cidx].valid, PA_ERR_INTERNAL);
868 }
869 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
870
871 t = pa_tagstruct_command(
872 s->context,
873 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_GET_PLAYBACK_LATENCY : PA_COMMAND_GET_RECORD_LATENCY,
874 &tag);
875 pa_tagstruct_putu32(t, s->channel);
876 pa_tagstruct_put_timeval(t, pa_gettimeofday(&now));
877
878 pa_pstream_send_tagstruct(s->context->pstream, t);
879 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_timing_info_callback, o);
880
881 if (s->direction == PA_STREAM_PLAYBACK) {
882 /* Fill in initial correction data */
883 o->stream->current_write_index_correction = cidx;
884 o->stream->write_index_corrections[cidx].valid = 1;
885 o->stream->write_index_corrections[cidx].tag = tag;
886 o->stream->write_index_corrections[cidx].absolute = 0;
887 o->stream->write_index_corrections[cidx].value = 0;
888 o->stream->write_index_corrections[cidx].corrupt = 0;
889 }
890
891 pa_log("requesting update %u\n", tag);
892
893 return pa_operation_ref(o);
894 }
895
896 void pa_stream_disconnect_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
897 pa_stream *s = userdata;
898
899 assert(pd);
900 assert(s);
901 assert(s->ref >= 1);
902
903 pa_stream_ref(s);
904
905 if (command != PA_COMMAND_REPLY) {
906 if (pa_context_handle_error(s->context, command, t) < 0)
907 goto finish;
908
909 pa_stream_set_state(s, PA_STREAM_FAILED);
910 goto finish;
911 } else if (!pa_tagstruct_eof(t)) {
912 pa_context_fail(s->context, PA_ERR_PROTOCOL);
913 goto finish;
914 }
915
916 pa_stream_set_state(s, PA_STREAM_TERMINATED);
917
918 finish:
919 pa_stream_unref(s);
920 }
921
922 int pa_stream_disconnect(pa_stream *s) {
923 pa_tagstruct *t;
924 uint32_t tag;
925
926 assert(s);
927 assert(s->ref >= 1);
928
929 PA_CHECK_VALIDITY(s->context, s->channel_valid, PA_ERR_BADSTATE);
930 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
931
932 pa_stream_ref(s);
933
934 t = pa_tagstruct_command(
935 s->context,
936 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_DELETE_PLAYBACK_STREAM :
937 (s->direction == PA_STREAM_RECORD ? PA_COMMAND_DELETE_RECORD_STREAM : PA_COMMAND_DELETE_UPLOAD_STREAM),
938 &tag);
939 pa_tagstruct_putu32(t, s->channel);
940 pa_pstream_send_tagstruct(s->context->pstream, t);
941 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s);
942
943 pa_stream_unref(s);
944 return 0;
945 }
946
947 void pa_stream_set_read_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
948 assert(s);
949 assert(s->ref >= 1);
950
951 s->read_callback = cb;
952 s->read_userdata = userdata;
953 }
954
955 void pa_stream_set_write_callback(pa_stream *s, pa_stream_request_cb_t cb, void *userdata) {
956 assert(s);
957 assert(s->ref >= 1);
958
959 s->write_callback = cb;
960 s->write_userdata = userdata;
961 }
962
963 void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
964 assert(s);
965 assert(s->ref >= 1);
966
967 s->state_callback = cb;
968 s->state_userdata = userdata;
969 }
970
971 void pa_stream_set_overflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
972 assert(s);
973 assert(s->ref >= 1);
974
975 s->overflow_callback = cb;
976 s->overflow_userdata = userdata;
977 }
978
979 void pa_stream_set_underflow_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata) {
980 assert(s);
981 assert(s->ref >= 1);
982
983 s->underflow_callback = cb;
984 s->underflow_userdata = userdata;
985 }
986
987 void pa_stream_simple_ack_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
988 pa_operation *o = userdata;
989 int success = 1;
990
991 assert(pd);
992 assert(o);
993 assert(o->context);
994 assert(o->ref >= 1);
995
996 if (command != PA_COMMAND_REPLY) {
997 if (pa_context_handle_error(o->context, command, t) < 0)
998 goto finish;
999
1000 success = 0;
1001 } else if (!pa_tagstruct_eof(t)) {
1002 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1003 goto finish;
1004 }
1005
1006 if (o->callback) {
1007 pa_stream_success_cb_t cb = (pa_stream_success_cb_t) o->callback;
1008 cb(o->stream, success, o->userdata);
1009 }
1010
1011 finish:
1012 pa_operation_done(o);
1013 pa_operation_unref(o);
1014 }
1015
1016 pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata) {
1017 pa_operation *o;
1018 pa_tagstruct *t;
1019 uint32_t tag;
1020
1021 assert(s);
1022 assert(s->ref >= 1);
1023
1024 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1025 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1026
1027 s->corked = b;
1028
1029 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1030
1031 t = pa_tagstruct_command(
1032 s->context,
1033 s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CORK_PLAYBACK_STREAM : PA_COMMAND_CORK_RECORD_STREAM,
1034 &tag);
1035 pa_tagstruct_putu32(t, s->channel);
1036 pa_tagstruct_put_boolean(t, !!b);
1037 pa_pstream_send_tagstruct(s->context->pstream, t);
1038 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, o);
1039
1040 if (s->direction == PA_STREAM_PLAYBACK)
1041 invalidate_indexes(s, 1, 0);
1042
1043 return pa_operation_ref(o);
1044 }
1045
1046 static pa_operation* stream_send_simple_command(pa_stream *s, uint32_t command, pa_stream_success_cb_t cb, void *userdata) {
1047 pa_tagstruct *t;
1048 pa_operation *o;
1049 uint32_t tag;
1050
1051 assert(s);
1052 assert(s->ref >= 1);
1053
1054 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1055
1056 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1057
1058 t = pa_tagstruct_command(s->context, command, &tag);
1059 pa_tagstruct_putu32(t, s->channel);
1060 pa_pstream_send_tagstruct(s->context->pstream, t);
1061 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, o);
1062
1063 return pa_operation_ref(o);
1064 }
1065
1066 pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1067 pa_operation *o;
1068
1069 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1070
1071 if ((o = stream_send_simple_command(s, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_FLUSH_PLAYBACK_STREAM : PA_COMMAND_FLUSH_RECORD_STREAM, cb, userdata))) {
1072
1073 if (s->direction == PA_STREAM_PLAYBACK) {
1074 if (s->write_index_corrections[s->current_write_index_correction].valid)
1075 s->write_index_corrections[s->current_write_index_correction].corrupt = 1;
1076
1077 if (s->timing_info_valid)
1078 s->timing_info.write_index_corrupt = 1;
1079
1080 if (s->buffer_attr.prebuf > 0)
1081 invalidate_indexes(s, 1, 0);
1082 else
1083 request_auto_timing_update(s, 1);
1084 } else
1085 invalidate_indexes(s, 0, 1);
1086 }
1087
1088 return o;
1089 }
1090
1091 pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1092 pa_operation *o;
1093
1094 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1095 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1096
1097 if ((o = stream_send_simple_command(s, PA_COMMAND_PREBUF_PLAYBACK_STREAM, cb, userdata)))
1098 invalidate_indexes(s, 1, 0);
1099
1100 return o;
1101 }
1102
1103 pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata) {
1104 pa_operation *o;
1105
1106 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE);
1107 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->buffer_attr.prebuf > 0, PA_ERR_BADSTATE);
1108
1109 if ((o = stream_send_simple_command(s, PA_COMMAND_TRIGGER_PLAYBACK_STREAM, cb, userdata)))
1110 invalidate_indexes(s, 1, 0);
1111
1112 return o;
1113 }
1114
1115 pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata) {
1116 pa_operation *o;
1117 pa_tagstruct *t;
1118 uint32_t tag;
1119
1120 assert(s);
1121 assert(s->ref >= 1);
1122 assert(name);
1123
1124 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1125 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1126
1127 o = pa_operation_new(s->context, s, (pa_operation_cb_t) cb, userdata);
1128
1129 t = pa_tagstruct_command(
1130 s->context,
1131 s->direction == PA_STREAM_RECORD ? PA_COMMAND_SET_RECORD_STREAM_NAME : PA_COMMAND_SET_PLAYBACK_STREAM_NAME,
1132 &tag);
1133 pa_tagstruct_putu32(t, s->channel);
1134 pa_tagstruct_puts(t, name);
1135 pa_pstream_send_tagstruct(s->context->pstream, t);
1136 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_simple_ack_callback, o);
1137
1138 return pa_operation_ref(o);
1139 }
1140
1141 int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec) {
1142 pa_usec_t usec = 0;
1143
1144 assert(s);
1145 assert(s->ref >= 1);
1146
1147 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1148 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1149 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1150 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1151 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1152
1153 if (s->flags & PA_STREAM_INTERPOLATE_TIMING && s->ipol_usec_valid)
1154 usec = s->ipol_usec;
1155 else {
1156 if (s->direction == PA_STREAM_PLAYBACK) {
1157 /* The last byte that was written into the output device
1158 * had this time value associated */
1159 usec = pa_bytes_to_usec(s->timing_info.read_index < 0 ? 0 : (uint64_t) s->timing_info.read_index, &s->sample_spec);
1160
1161 if (!s->corked) {
1162 /* Because the latency info took a little time to come
1163 * to us, we assume that the real output time is actually
1164 * a little ahead */
1165 usec += s->timing_info.transport_usec;
1166
1167 /* However, the output device usually maintains a buffer
1168 too, hence the real sample currently played is a little
1169 back */
1170 if (s->timing_info.sink_usec >= usec)
1171 usec = 0;
1172 else
1173 usec -= s->timing_info.sink_usec;
1174 }
1175
1176 } else if (s->direction == PA_STREAM_RECORD) {
1177 /* The last byte written into the server side queue had
1178 * this time value associated */
1179 usec = pa_bytes_to_usec(s->timing_info.write_index < 0 ? 0 : (uint64_t) s->timing_info.write_index, &s->sample_spec);
1180
1181 if (!s->corked) {
1182 /* Add transport latency */
1183 usec += s->timing_info.transport_usec;
1184
1185 /* Add latency of data in device buffer */
1186 usec += s->timing_info.source_usec;
1187
1188 /* If this is a monitor source, we need to correct the
1189 * time by the playback device buffer */
1190 if (s->timing_info.sink_usec >= usec)
1191 usec = 0;
1192 else
1193 usec -= s->timing_info.sink_usec;
1194 }
1195 }
1196
1197 if (s->flags & PA_STREAM_INTERPOLATE_TIMING) {
1198 s->ipol_usec = usec;
1199 s->ipol_usec_valid = 1;
1200 }
1201 }
1202
1203 /* Interpolate if requested */
1204 if (s->flags & PA_STREAM_INTERPOLATE_TIMING) {
1205
1206 /* We just add the time that passed since the latency info was
1207 * current */
1208 if (!s->corked) {
1209 struct timeval now;
1210
1211 usec += pa_timeval_diff(pa_gettimeofday(&now), &s->ipol_timestamp);
1212 s->ipol_timestamp = now;
1213 }
1214 }
1215
1216 /* Make sure the time runs monotonically */
1217 if (!(s->flags & PA_STREAM_NOT_MONOTONOUS)) {
1218 if (usec < s->previous_time)
1219 usec = s->previous_time;
1220 else
1221 s->previous_time = usec;
1222 }
1223
1224 if (r_usec)
1225 *r_usec = usec;
1226
1227 return 0;
1228 }
1229
1230 static pa_usec_t time_counter_diff(pa_stream *s, pa_usec_t a, pa_usec_t b, int *negative) {
1231 assert(s);
1232 assert(s->ref >= 1);
1233
1234 if (negative)
1235 *negative = 0;
1236
1237 if (a >= b)
1238 return a-b;
1239 else {
1240 if (negative && s->direction == PA_STREAM_RECORD) {
1241 *negative = 1;
1242 return b-a;
1243 } else
1244 return 0;
1245 }
1246 }
1247
1248 int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative) {
1249 pa_usec_t t, c;
1250 int r;
1251 int64_t cindex;
1252
1253 assert(s);
1254 assert(s->ref >= 1);
1255 assert(r_usec);
1256
1257 PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1258 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1259 PA_CHECK_VALIDITY(s->context, s->timing_info_valid, PA_ERR_NODATA);
1260 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_PLAYBACK || !s->timing_info.write_index_corrupt, PA_ERR_NODATA);
1261 PA_CHECK_VALIDITY(s->context, s->direction != PA_STREAM_RECORD || !s->timing_info.read_index_corrupt, PA_ERR_NODATA);
1262
1263 if ((r = pa_stream_get_time(s, &t)) < 0)
1264 return r;
1265
1266 if (s->direction == PA_STREAM_PLAYBACK)
1267 cindex = s->timing_info.write_index;
1268 else
1269 cindex = s->timing_info.read_index;
1270
1271 if (cindex < 0)
1272 cindex = 0;
1273
1274 c = pa_bytes_to_usec(cindex, &s->sample_spec);
1275
1276 if (s->direction == PA_STREAM_PLAYBACK)
1277 *r_usec = time_counter_diff(s, c, t, negative);
1278 else
1279 *r_usec = time_counter_diff(s, t, c, negative);
1280
1281 return 0;
1282 }
1283
1284 const pa_timing_info* pa_stream_get_timing_info(pa_stream *s) {
1285 assert(s);
1286 assert(s->ref >= 1);
1287
1288 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
1289 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
1290 PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->timing_info_valid, PA_ERR_BADSTATE);
1291
1292 return &s->timing_info;
1293 }
1294
1295 const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s) {
1296 assert(s);
1297 assert(s->ref >= 1);
1298
1299 return &s->sample_spec;
1300 }
1301
1302 const pa_channel_map* pa_stream_get_channel_map(pa_stream *s) {
1303 assert(s);
1304 assert(s->ref >= 1);
1305
1306 return &s->channel_map;
1307 }