4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/def.h>
32 #include <pulse/timeval.h>
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/pstream-util.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/hashmap.h>
41 #define LATENCY_IPOL_INTERVAL_USEC (100000L)
43 pa_stream
*pa_stream_new(pa_context
*c
, const char *name
, const pa_sample_spec
*ss
, const pa_channel_map
*map
) {
49 PA_CHECK_VALIDITY_RETURN_NULL(c
, ss
&& pa_sample_spec_valid(ss
), PA_ERR_INVALID
);
50 PA_CHECK_VALIDITY_RETURN_NULL(c
, !map
|| (pa_channel_map_valid(map
) && map
->channels
== ss
->channels
), PA_ERR_INVALID
);
52 s
= pa_xnew(pa_stream
, 1);
55 s
->mainloop
= c
->mainloop
;
57 s
->read_callback
= NULL
;
58 s
->read_userdata
= NULL
;
59 s
->write_callback
= NULL
;
60 s
->write_userdata
= NULL
;
61 s
->state_callback
= NULL
;
62 s
->state_userdata
= NULL
;
63 s
->overflow_callback
= NULL
;
64 s
->overflow_userdata
= NULL
;
65 s
->underflow_callback
= NULL
;
66 s
->underflow_userdata
= NULL
;
67 s
->latency_update_callback
= NULL
;
68 s
->latency_update_userdata
= NULL
;
70 s
->direction
= PA_STREAM_NODIRECTION
;
71 s
->name
= pa_xstrdup(name
);
76 s
->channel_map
= *map
;
78 pa_channel_map_init_auto(&s
->channel_map
, ss
->channels
, PA_CHANNEL_MAP_DEFAULT
);
82 s
->syncid
= c
->csyncid
++;
83 s
->device_index
= PA_INVALID_INDEX
;
84 s
->requested_bytes
= 0;
85 s
->state
= PA_STREAM_UNCONNECTED
;
86 memset(&s
->buffer_attr
, 0, sizeof(s
->buffer_attr
));
88 s
->peek_memchunk
.index
= 0;
89 s
->peek_memchunk
.length
= 0;
90 s
->peek_memchunk
.memblock
= NULL
;
92 s
->record_memblockq
= NULL
;
95 s
->timing_info_valid
= 0;
96 s
->read_index_not_before
= 0;
97 s
->write_index_not_before
= 0;
99 for (i
= 0; i
< PA_MAX_WRITE_INDEX_CORRECTIONS
; i
++)
100 s
->write_index_corrections
[i
].valid
= 0;
101 s
->current_write_index_correction
= 0;
105 s
->cached_time_valid
= 0;
107 s
->auto_timing_update_event
= NULL
;
108 s
->auto_timing_update_requested
= 0;
110 /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
111 PA_LLIST_PREPEND(pa_stream
, c
->streams
, s
);
117 static void stream_free(pa_stream
*s
) {
118 assert(s
&& !s
->context
&& !s
->channel_valid
);
120 if (s
->auto_timing_update_event
) {
122 s
->mainloop
->time_free(s
->auto_timing_update_event
);
125 if (s
->peek_memchunk
.memblock
)
126 pa_memblock_unref(s
->peek_memchunk
.memblock
);
128 if (s
->record_memblockq
)
129 pa_memblockq_free(s
->record_memblockq
);
135 void pa_stream_unref(pa_stream
*s
) {
143 pa_stream
* pa_stream_ref(pa_stream
*s
) {
151 pa_stream_state_t
pa_stream_get_state(pa_stream
*s
) {
158 pa_context
* pa_stream_get_context(pa_stream
*s
) {
165 uint32_t pa_stream_get_index(pa_stream
*s
) {
169 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, PA_INVALID_INDEX
);
171 return s
->device_index
;
174 void pa_stream_set_state(pa_stream
*s
, pa_stream_state_t st
) {
184 if (s
->state_callback
)
185 s
->state_callback(s
, s
->state_userdata
);
187 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
) && s
->context
) {
189 /* Detach from context */
192 /* Unref all operatio object that point to us */
193 for (o
= s
->context
->operations
; o
; o
= n
) {
197 pa_operation_cancel(o
);
200 /* Drop all outstanding replies for this stream */
201 if (s
->context
->pdispatch
)
202 pa_pdispatch_unregister_reply(s
->context
->pdispatch
, s
);
204 if (s
->channel_valid
)
205 pa_dynarray_put((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, s
->channel
, NULL
);
207 PA_LLIST_REMOVE(pa_stream
, s
->context
->streams
, s
);
211 s
->channel_valid
= 0;
219 void pa_command_stream_killed(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
220 pa_context
*c
= userdata
;
225 assert(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
);
231 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
232 !pa_tagstruct_eof(t
)) {
233 pa_context_fail(c
, PA_ERR_PROTOCOL
);
237 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, channel
)))
240 pa_context_set_error(c
, PA_ERR_KILLED
);
241 pa_stream_set_state(s
, PA_STREAM_FAILED
);
247 void pa_command_request(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
249 pa_context
*c
= userdata
;
250 uint32_t bytes
, channel
;
253 assert(command
== PA_COMMAND_REQUEST
);
259 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
260 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
261 !pa_tagstruct_eof(t
)) {
262 pa_context_fail(c
, PA_ERR_PROTOCOL
);
266 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
269 if (s
->state
== PA_STREAM_READY
) {
270 s
->requested_bytes
+= bytes
;
272 if (s
->requested_bytes
> 0 && s
->write_callback
)
273 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
280 void pa_command_overflow_or_underflow(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
282 pa_context
*c
= userdata
;
286 assert(command
== PA_COMMAND_OVERFLOW
|| command
== PA_COMMAND_UNDERFLOW
);
292 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
293 !pa_tagstruct_eof(t
)) {
294 pa_context_fail(c
, PA_ERR_PROTOCOL
);
298 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
301 if (s
->state
== PA_STREAM_READY
) {
303 if (command
== PA_COMMAND_OVERFLOW
) {
304 if (s
->overflow_callback
)
305 s
->overflow_callback(s
, s
->overflow_userdata
);
306 } else if (command
== PA_COMMAND_UNDERFLOW
) {
307 if (s
->underflow_callback
)
308 s
->underflow_callback(s
, s
->underflow_userdata
);
316 static void request_auto_timing_update(pa_stream
*s
, int force
) {
320 if (!(s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
))
323 if (s
->state
== PA_STREAM_READY
&&
324 (force
|| !s
->auto_timing_update_requested
)) {
327 /* pa_log("automatically requesting new timing data"); */
329 if ((o
= pa_stream_update_timing_info(s
, NULL
, NULL
))) {
330 pa_operation_unref(o
);
331 s
->auto_timing_update_requested
= 1;
335 pa_gettimeofday(&next
);
336 pa_timeval_add(&next
, LATENCY_IPOL_INTERVAL_USEC
);
337 s
->mainloop
->time_restart(s
->auto_timing_update_event
, &next
);
340 static void invalidate_indexes(pa_stream
*s
, int r
, int w
) {
343 /* pa_log("invalidate r:%u w:%u tag:%u", r, w, s->context->ctag); */
345 if (s
->state
!= PA_STREAM_READY
)
349 s
->write_index_not_before
= s
->context
->ctag
;
351 if (s
->timing_info_valid
)
352 s
->timing_info
.write_index_corrupt
= 1;
354 /* pa_log("write_index invalidated"); */
358 s
->read_index_not_before
= s
->context
->ctag
;
360 if (s
->timing_info_valid
)
361 s
->timing_info
.read_index_corrupt
= 1;
363 /* pa_log("read_index invalidated"); */
366 if ((s
->direction
== PA_STREAM_PLAYBACK
&& r
) ||
367 (s
->direction
== PA_STREAM_RECORD
&& w
))
368 s
->cached_time_valid
= 0;
370 request_auto_timing_update(s
, 1);
373 static void auto_timing_update_callback(PA_GCC_UNUSED pa_mainloop_api
*m
, PA_GCC_UNUSED pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
374 pa_stream
*s
= userdata
;
376 /* pa_log("time event"); */
379 request_auto_timing_update(s
, 0);
383 void pa_create_stream_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
384 pa_stream
*s
= userdata
;
388 assert(s
->state
== PA_STREAM_CREATING
);
392 if (command
!= PA_COMMAND_REPLY
) {
393 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
396 pa_stream_set_state(s
, PA_STREAM_FAILED
);
400 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
401 ((s
->direction
!= PA_STREAM_UPLOAD
) && pa_tagstruct_getu32(t
, &s
->device_index
) < 0) ||
402 ((s
->direction
!= PA_STREAM_RECORD
) && pa_tagstruct_getu32(t
, &s
->requested_bytes
) < 0)) {
403 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
407 if (pa_context_get_server_protocol_version(s
->context
) >= 9) {
408 if (s
->direction
== PA_STREAM_PLAYBACK
) {
409 if (pa_tagstruct_getu32(t
, &s
->buffer_attr
.maxlength
) < 0 ||
410 pa_tagstruct_getu32(t
, &s
->buffer_attr
.tlength
) < 0 ||
411 pa_tagstruct_getu32(t
, &s
->buffer_attr
.prebuf
) < 0 ||
412 pa_tagstruct_getu32(t
, &s
->buffer_attr
.minreq
) < 0) {
413 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
416 } else if (s
->direction
== PA_STREAM_RECORD
) {
417 if (pa_tagstruct_getu32(t
, &s
->buffer_attr
.maxlength
) < 0 ||
418 pa_tagstruct_getu32(t
, &s
->buffer_attr
.fragsize
) < 0) {
419 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
425 if (!pa_tagstruct_eof(t
)) {
426 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
430 if (s
->direction
== PA_STREAM_RECORD
) {
431 assert(!s
->record_memblockq
);
433 s
->record_memblockq
= pa_memblockq_new(
435 s
->buffer_attr
.maxlength
,
437 pa_frame_size(&s
->sample_spec
),
441 s
->context
->memblock_stat
);
444 s
->channel_valid
= 1;
445 pa_dynarray_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, s
->channel
, s
);
447 pa_stream_set_state(s
, PA_STREAM_READY
);
449 if (s
->direction
!= PA_STREAM_UPLOAD
&&
450 s
->flags
& PA_STREAM_AUTO_TIMING_UPDATE
) {
453 pa_gettimeofday(&tv
);
454 tv
.tv_usec
+= LATENCY_IPOL_INTERVAL_USEC
; /* every 100 ms */
456 assert(!s
->auto_timing_update_event
);
457 s
->auto_timing_update_event
= s
->mainloop
->time_new(s
->mainloop
, &tv
, &auto_timing_update_callback
, s
);
459 request_auto_timing_update(s
, 1);
462 if (s
->requested_bytes
> 0 && s
->ref
> 1 && s
->write_callback
)
463 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
469 static int create_stream(
470 pa_stream_direction_t direction
,
473 const pa_buffer_attr
*attr
,
474 pa_stream_flags_t flags
,
475 const pa_cvolume
*volume
,
476 pa_stream
*sync_stream
) {
484 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_UNCONNECTED
, PA_ERR_BADSTATE
);
485 PA_CHECK_VALIDITY(s
->context
, !(flags
& ~((direction
!= PA_STREAM_UPLOAD
?
486 PA_STREAM_START_CORKED
|
487 PA_STREAM_INTERPOLATE_TIMING
|
488 PA_STREAM_NOT_MONOTONOUS
|
489 PA_STREAM_AUTO_TIMING_UPDATE
: 0))), PA_ERR_INVALID
);
490 PA_CHECK_VALIDITY(s
->context
, !volume
|| volume
->channels
== s
->sample_spec
.channels
, PA_ERR_INVALID
);
491 PA_CHECK_VALIDITY(s
->context
, !sync_stream
|| (direction
== PA_STREAM_PLAYBACK
&& sync_stream
->direction
== PA_STREAM_PLAYBACK
), PA_ERR_INVALID
);
495 s
->direction
= direction
;
499 s
->syncid
= sync_stream
->syncid
;
502 s
->buffer_attr
= *attr
;
505 s
->buffer_attr
.tlength
= pa_bytes_per_second(&s
->sample_spec
)/2;
506 s
->buffer_attr
.maxlength
= (s
->buffer_attr
.tlength
*3)/2;
507 s
->buffer_attr
.minreq
= s
->buffer_attr
.tlength
/100;
508 s
->buffer_attr
.prebuf
= s
->buffer_attr
.tlength
- s
->buffer_attr
.minreq
;
509 s
->buffer_attr
.fragsize
= s
->buffer_attr
.tlength
/100;
513 dev
= s
->direction
== PA_STREAM_PLAYBACK
? s
->context
->conf
->default_sink
: s
->context
->conf
->default_source
;
515 t
= pa_tagstruct_command(
517 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
,
522 PA_TAG_STRING
, s
->name
,
523 PA_TAG_SAMPLE_SPEC
, &s
->sample_spec
,
524 PA_TAG_CHANNEL_MAP
, &s
->channel_map
,
525 PA_TAG_U32
, PA_INVALID_INDEX
,
527 PA_TAG_U32
, s
->buffer_attr
.maxlength
,
528 PA_TAG_BOOLEAN
, !!(flags
& PA_STREAM_START_CORKED
),
531 if (s
->direction
== PA_STREAM_PLAYBACK
) {
536 PA_TAG_U32
, s
->buffer_attr
.tlength
,
537 PA_TAG_U32
, s
->buffer_attr
.prebuf
,
538 PA_TAG_U32
, s
->buffer_attr
.minreq
,
539 PA_TAG_U32
, s
->syncid
,
543 volume
= pa_cvolume_reset(&cv
, s
->sample_spec
.channels
);
545 pa_tagstruct_put_cvolume(t
, volume
);
547 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
549 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
550 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
, NULL
);
552 pa_stream_set_state(s
, PA_STREAM_CREATING
);
558 int pa_stream_connect_playback(
561 const pa_buffer_attr
*attr
,
562 pa_stream_flags_t flags
,
564 pa_stream
*sync_stream
) {
569 return create_stream(PA_STREAM_PLAYBACK
, s
, dev
, attr
, flags
, volume
, sync_stream
);
572 int pa_stream_connect_record(
575 const pa_buffer_attr
*attr
,
576 pa_stream_flags_t flags
) {
581 return create_stream(PA_STREAM_RECORD
, s
, dev
, attr
, flags
, NULL
, NULL
);
588 void (*free_cb
)(void *p
),
590 pa_seek_mode_t seek
) {
598 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
599 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| s
->direction
== PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
600 PA_CHECK_VALIDITY(s
->context
, seek
<= PA_SEEK_RELATIVE_END
, PA_ERR_INVALID
);
601 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
|| (seek
== PA_SEEK_RELATIVE
&& offset
== 0), PA_ERR_INVALID
);
607 chunk
.memblock
= pa_memblock_new_user((void*) data
, length
, free_cb
, 1, s
->context
->memblock_stat
);
609 chunk
.memblock
= pa_memblock_new(length
, s
->context
->memblock_stat
);
610 memcpy(chunk
.memblock
->data
, data
, length
);
614 chunk
.length
= length
;
616 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, offset
, seek
, &chunk
);
617 pa_memblock_unref(chunk
.memblock
);
619 if (length
< s
->requested_bytes
)
620 s
->requested_bytes
-= length
;
622 s
->requested_bytes
= 0;
624 if (s
->direction
== PA_STREAM_PLAYBACK
) {
626 /* Update latency request correction */
627 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
) {
629 if (seek
== PA_SEEK_ABSOLUTE
) {
630 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 0;
631 s
->write_index_corrections
[s
->current_write_index_correction
].absolute
= 1;
632 s
->write_index_corrections
[s
->current_write_index_correction
].value
= offset
+ length
;
633 } else if (seek
== PA_SEEK_RELATIVE
) {
634 if (!s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
)
635 s
->write_index_corrections
[s
->current_write_index_correction
].value
+= offset
+ length
;
637 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 1;
640 /* Update the write index in the already available latency data */
641 if (s
->timing_info_valid
) {
643 if (seek
== PA_SEEK_ABSOLUTE
) {
644 s
->timing_info
.write_index_corrupt
= 0;
645 s
->timing_info
.write_index
= offset
+ length
;
646 } else if (seek
== PA_SEEK_RELATIVE
) {
647 if (!s
->timing_info
.write_index_corrupt
)
648 s
->timing_info
.write_index
+= offset
+ length
;
650 s
->timing_info
.write_index_corrupt
= 1;
653 if (!s
->timing_info_valid
|| s
->timing_info
.write_index_corrupt
)
654 request_auto_timing_update(s
, 1);
660 int pa_stream_peek(pa_stream
*s
, const void **data
, size_t *length
) {
666 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
667 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
669 if (!s
->peek_memchunk
.memblock
) {
671 if (pa_memblockq_peek(s
->record_memblockq
, &s
->peek_memchunk
) < 0) {
678 *data
= (const char*) s
->peek_memchunk
.memblock
->data
+ s
->peek_memchunk
.index
;
679 *length
= s
->peek_memchunk
.length
;
683 int pa_stream_drop(pa_stream
*s
) {
687 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
688 PA_CHECK_VALIDITY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
);
689 PA_CHECK_VALIDITY(s
->context
, s
->peek_memchunk
.memblock
, PA_ERR_BADSTATE
);
691 pa_memblockq_drop(s
->record_memblockq
, &s
->peek_memchunk
, s
->peek_memchunk
.length
);
693 /* Fix the simulated local read index */
694 if (s
->timing_info_valid
&& !s
->timing_info
.read_index_corrupt
)
695 s
->timing_info
.read_index
+= s
->peek_memchunk
.length
;
697 pa_memblock_unref(s
->peek_memchunk
.memblock
);
698 s
->peek_memchunk
.length
= 0;
699 s
->peek_memchunk
.index
= 0;
700 s
->peek_memchunk
.memblock
= NULL
;
705 size_t pa_stream_writable_size(pa_stream
*s
) {
709 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
710 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
!= PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
712 return s
->requested_bytes
;
715 size_t pa_stream_readable_size(pa_stream
*s
) {
719 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
, (size_t) -1);
720 PA_CHECK_VALIDITY_RETURN_ANY(s
->context
, s
->direction
== PA_STREAM_RECORD
, PA_ERR_BADSTATE
, (size_t) -1);
722 return pa_memblockq_get_length(s
->record_memblockq
);
725 pa_operation
* pa_stream_drain(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
733 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
734 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
736 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
738 t
= pa_tagstruct_command(s
->context
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
, &tag
);
739 pa_tagstruct_putu32(t
, s
->channel
);
740 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
741 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
);
746 static void stream_get_timing_info_callback(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
747 pa_operation
*o
= userdata
;
748 struct timeval local
, remote
, now
;
754 if (!o
->context
|| !o
->stream
)
757 i
= &o
->stream
->timing_info
;
759 /* 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); */
761 o
->stream
->timing_info_valid
= 0;
762 i
->write_index_corrupt
= 0;
763 i
->read_index_corrupt
= 0;
765 /* pa_log("timing update %u\n", tag); */
767 if (command
!= PA_COMMAND_REPLY
) {
768 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
771 } else if (pa_tagstruct_get_usec(t
, &i
->sink_usec
) < 0 ||
772 pa_tagstruct_get_usec(t
, &i
->source_usec
) < 0 ||
773 pa_tagstruct_get_boolean(t
, &i
->playing
) < 0 ||
774 pa_tagstruct_get_timeval(t
, &local
) < 0 ||
775 pa_tagstruct_get_timeval(t
, &remote
) < 0 ||
776 pa_tagstruct_gets64(t
, &i
->write_index
) < 0 ||
777 pa_tagstruct_gets64(t
, &i
->read_index
) < 0 ||
778 !pa_tagstruct_eof(t
)) {
779 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
783 o
->stream
->timing_info_valid
= 1;
785 pa_gettimeofday(&now
);
787 /* Calculcate timestamps */
788 if (pa_timeval_cmp(&local
, &remote
) <= 0 && pa_timeval_cmp(&remote
, &now
) <= 0) {
789 /* local and remote seem to have synchronized clocks */
791 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
)
792 i
->transport_usec
= pa_timeval_diff(&remote
, &local
);
794 i
->transport_usec
= pa_timeval_diff(&now
, &remote
);
796 i
->synchronized_clocks
= 1;
797 i
->timestamp
= remote
;
799 /* clocks are not synchronized, let's estimate latency then */
800 i
->transport_usec
= pa_timeval_diff(&now
, &local
)/2;
801 i
->synchronized_clocks
= 0;
802 i
->timestamp
= local
;
803 pa_timeval_add(&i
->timestamp
, i
->transport_usec
);
806 /* Invalidate read and write indexes if necessary */
807 if (tag
< o
->stream
->read_index_not_before
)
808 i
->read_index_corrupt
= 1;
810 if (tag
< o
->stream
->write_index_not_before
)
811 i
->write_index_corrupt
= 1;
813 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
814 /* Write index correction */
819 /* Go through the saved correction values and add up the total correction.*/
821 for (n
= 0, j
= o
->stream
->current_write_index_correction
+1;
822 n
< PA_MAX_WRITE_INDEX_CORRECTIONS
;
823 n
++, j
= (j
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
) {
825 /* Step over invalid data or out-of-date data */
826 if (!o
->stream
->write_index_corrections
[j
].valid
||
827 o
->stream
->write_index_corrections
[j
].tag
< ctag
)
830 /* Make sure that everything is in order */
831 ctag
= o
->stream
->write_index_corrections
[j
].tag
+1;
833 /* Now fix the write index */
834 if (o
->stream
->write_index_corrections
[j
].corrupt
) {
835 /* A corrupting seek was made */
837 i
->write_index_corrupt
= 1;
838 } else if (o
->stream
->write_index_corrections
[j
].absolute
) {
839 /* An absolute seek was made */
840 i
->write_index
= o
->stream
->write_index_corrections
[j
].value
;
841 i
->write_index_corrupt
= 0;
842 } else if (!i
->write_index_corrupt
) {
843 /* A relative seek was made */
844 i
->write_index
+= o
->stream
->write_index_corrections
[j
].value
;
849 if (o
->stream
->direction
== PA_STREAM_RECORD
) {
850 /* Read index correction */
852 if (!i
->read_index_corrupt
)
853 i
->read_index
-= pa_memblockq_get_length(o
->stream
->record_memblockq
);
856 o
->stream
->cached_time_valid
= 0;
859 o
->stream
->auto_timing_update_requested
= 0;
860 /* 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); */
862 /* Clear old correction entries */
863 if (o
->stream
->direction
== PA_STREAM_PLAYBACK
) {
866 for (n
= 0; n
< PA_MAX_WRITE_INDEX_CORRECTIONS
; n
++) {
867 if (!o
->stream
->write_index_corrections
[n
].valid
)
870 if (o
->stream
->write_index_corrections
[n
].tag
<= tag
)
871 o
->stream
->write_index_corrections
[n
].valid
= 0;
875 if (o
->stream
->latency_update_callback
)
876 o
->stream
->latency_update_callback(o
->stream
, o
->stream
->latency_update_userdata
);
878 if (o
->callback
&& o
->stream
&& o
->stream
->state
== PA_STREAM_READY
) {
879 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
880 cb(o
->stream
, o
->stream
->timing_info_valid
, o
->userdata
);
885 pa_operation_done(o
);
886 pa_operation_unref(o
);
889 pa_operation
* pa_stream_update_timing_info(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
899 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
900 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
902 if (s
->direction
== PA_STREAM_PLAYBACK
) {
903 /* Find a place to store the write_index correction data for this entry */
904 cidx
= (s
->current_write_index_correction
+ 1) % PA_MAX_WRITE_INDEX_CORRECTIONS
;
906 /* Check if we could allocate a correction slot. If not, there are too many outstanding queries */
907 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, !s
->write_index_corrections
[cidx
].valid
, PA_ERR_INTERNAL
);
909 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
911 t
= pa_tagstruct_command(
913 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_GET_PLAYBACK_LATENCY
: PA_COMMAND_GET_RECORD_LATENCY
,
915 pa_tagstruct_putu32(t
, s
->channel
);
916 pa_tagstruct_put_timeval(t
, pa_gettimeofday(&now
));
918 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
919 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
);
921 if (s
->direction
== PA_STREAM_PLAYBACK
) {
922 /* Fill in initial correction data */
923 o
->stream
->current_write_index_correction
= cidx
;
924 o
->stream
->write_index_corrections
[cidx
].valid
= 1;
925 o
->stream
->write_index_corrections
[cidx
].tag
= tag
;
926 o
->stream
->write_index_corrections
[cidx
].absolute
= 0;
927 o
->stream
->write_index_corrections
[cidx
].value
= 0;
928 o
->stream
->write_index_corrections
[cidx
].corrupt
= 0;
931 /* pa_log("requesting update %u\n", tag); */
936 void pa_stream_disconnect_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
937 pa_stream
*s
= userdata
;
945 if (command
!= PA_COMMAND_REPLY
) {
946 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
949 pa_stream_set_state(s
, PA_STREAM_FAILED
);
951 } else if (!pa_tagstruct_eof(t
)) {
952 pa_context_fail(s
->context
, PA_ERR_PROTOCOL
);
956 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
962 int pa_stream_disconnect(pa_stream
*s
) {
969 PA_CHECK_VALIDITY(s
->context
, s
->channel_valid
, PA_ERR_BADSTATE
);
970 PA_CHECK_VALIDITY(s
->context
, s
->context
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
974 t
= pa_tagstruct_command(
976 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
977 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
),
979 pa_tagstruct_putu32(t
, s
->channel
);
980 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
981 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
, NULL
);
987 void pa_stream_set_read_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
991 s
->read_callback
= cb
;
992 s
->read_userdata
= userdata
;
995 void pa_stream_set_write_callback(pa_stream
*s
, pa_stream_request_cb_t cb
, void *userdata
) {
999 s
->write_callback
= cb
;
1000 s
->write_userdata
= userdata
;
1003 void pa_stream_set_state_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1005 assert(s
->ref
>= 1);
1007 s
->state_callback
= cb
;
1008 s
->state_userdata
= userdata
;
1011 void pa_stream_set_overflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1013 assert(s
->ref
>= 1);
1015 s
->overflow_callback
= cb
;
1016 s
->overflow_userdata
= userdata
;
1019 void pa_stream_set_underflow_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1021 assert(s
->ref
>= 1);
1023 s
->underflow_callback
= cb
;
1024 s
->underflow_userdata
= userdata
;
1027 void pa_stream_set_latency_update_callback(pa_stream
*s
, pa_stream_notify_cb_t cb
, void *userdata
) {
1029 assert(s
->ref
>= 1);
1031 s
->latency_update_callback
= cb
;
1032 s
->latency_update_userdata
= userdata
;
1035 void pa_stream_simple_ack_callback(pa_pdispatch
*pd
, uint32_t command
, PA_GCC_UNUSED
uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
1036 pa_operation
*o
= userdata
;
1041 assert(o
->ref
>= 1);
1046 if (command
!= PA_COMMAND_REPLY
) {
1047 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
1051 } else if (!pa_tagstruct_eof(t
)) {
1052 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
1057 pa_stream_success_cb_t cb
= (pa_stream_success_cb_t
) o
->callback
;
1058 cb(o
->stream
, success
, o
->userdata
);
1062 pa_operation_done(o
);
1063 pa_operation_unref(o
);
1066 pa_operation
* pa_stream_cork(pa_stream
*s
, int b
, pa_stream_success_cb_t cb
, void *userdata
) {
1072 assert(s
->ref
>= 1);
1074 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1075 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1079 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1081 t
= pa_tagstruct_command(
1083 s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CORK_PLAYBACK_STREAM
: PA_COMMAND_CORK_RECORD_STREAM
,
1085 pa_tagstruct_putu32(t
, s
->channel
);
1086 pa_tagstruct_put_boolean(t
, !!b
);
1087 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1088 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
);
1090 if (s
->direction
== PA_STREAM_PLAYBACK
)
1091 invalidate_indexes(s
, 1, 0);
1096 static pa_operation
* stream_send_simple_command(pa_stream
*s
, uint32_t command
, pa_stream_success_cb_t cb
, void *userdata
) {
1102 assert(s
->ref
>= 1);
1104 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1106 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1108 t
= pa_tagstruct_command(s
->context
, command
, &tag
);
1109 pa_tagstruct_putu32(t
, s
->channel
);
1110 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1111 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
);
1116 pa_operation
* pa_stream_flush(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1119 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1121 if ((o
= stream_send_simple_command(s
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_FLUSH_PLAYBACK_STREAM
: PA_COMMAND_FLUSH_RECORD_STREAM
, cb
, userdata
))) {
1123 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1124 if (s
->write_index_corrections
[s
->current_write_index_correction
].valid
)
1125 s
->write_index_corrections
[s
->current_write_index_correction
].corrupt
= 1;
1127 if (s
->timing_info_valid
)
1128 s
->timing_info
.write_index_corrupt
= 1;
1130 if (s
->buffer_attr
.prebuf
> 0)
1131 invalidate_indexes(s
, 1, 0);
1133 request_auto_timing_update(s
, 1);
1135 invalidate_indexes(s
, 0, 1);
1141 pa_operation
* pa_stream_prebuf(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1144 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1145 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
1147 if ((o
= stream_send_simple_command(s
, PA_COMMAND_PREBUF_PLAYBACK_STREAM
, cb
, userdata
)))
1148 invalidate_indexes(s
, 1, 0);
1153 pa_operation
* pa_stream_trigger(pa_stream
*s
, pa_stream_success_cb_t cb
, void *userdata
) {
1156 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
== PA_STREAM_PLAYBACK
, PA_ERR_BADSTATE
);
1157 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->buffer_attr
.prebuf
> 0, PA_ERR_BADSTATE
);
1159 if ((o
= stream_send_simple_command(s
, PA_COMMAND_TRIGGER_PLAYBACK_STREAM
, cb
, userdata
)))
1160 invalidate_indexes(s
, 1, 0);
1165 pa_operation
* pa_stream_set_name(pa_stream
*s
, const char *name
, pa_stream_success_cb_t cb
, void *userdata
) {
1171 assert(s
->ref
>= 1);
1174 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1175 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1177 o
= pa_operation_new(s
->context
, s
, (pa_operation_cb_t
) cb
, userdata
);
1179 t
= pa_tagstruct_command(
1181 s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_SET_RECORD_STREAM_NAME
: PA_COMMAND_SET_PLAYBACK_STREAM_NAME
,
1183 pa_tagstruct_putu32(t
, s
->channel
);
1184 pa_tagstruct_puts(t
, name
);
1185 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
1186 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
);
1191 int pa_stream_get_time(pa_stream
*s
, pa_usec_t
*r_usec
) {
1195 assert(s
->ref
>= 1);
1197 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1198 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1199 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1200 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
1201 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
1203 if (s
->cached_time_valid
)
1204 /* We alredy calculated the time value for this timing info, so let's reuse it */
1205 usec
= s
->cached_time
;
1207 if (s
->direction
== PA_STREAM_PLAYBACK
) {
1208 /* The last byte that was written into the output device
1209 * had this time value associated */
1210 usec
= pa_bytes_to_usec(s
->timing_info
.read_index
< 0 ? 0 : (uint64_t) s
->timing_info
.read_index
, &s
->sample_spec
);
1213 /* Because the latency info took a little time to come
1214 * to us, we assume that the real output time is actually
1216 usec
+= s
->timing_info
.transport_usec
;
1218 /* However, the output device usually maintains a buffer
1219 too, hence the real sample currently played is a little
1221 if (s
->timing_info
.sink_usec
>= usec
)
1224 usec
-= s
->timing_info
.sink_usec
;
1227 } else if (s
->direction
== PA_STREAM_RECORD
) {
1228 /* The last byte written into the server side queue had
1229 * this time value associated */
1230 usec
= pa_bytes_to_usec(s
->timing_info
.write_index
< 0 ? 0 : (uint64_t) s
->timing_info
.write_index
, &s
->sample_spec
);
1233 /* Add transport latency */
1234 usec
+= s
->timing_info
.transport_usec
;
1236 /* Add latency of data in device buffer */
1237 usec
+= s
->timing_info
.source_usec
;
1239 /* If this is a monitor source, we need to correct the
1240 * time by the playback device buffer */
1241 if (s
->timing_info
.sink_usec
>= usec
)
1244 usec
-= s
->timing_info
.sink_usec
;
1248 s
->cached_time
= usec
;
1249 s
->cached_time_valid
= 1;
1252 /* Interpolate if requested */
1253 if (s
->flags
& PA_STREAM_INTERPOLATE_TIMING
) {
1255 /* We just add the time that passed since the latency info was
1259 usec
+= pa_timeval_diff(pa_gettimeofday(&now
), &s
->timing_info
.timestamp
);
1263 /* Make sure the time runs monotonically */
1264 if (!(s
->flags
& PA_STREAM_NOT_MONOTONOUS
)) {
1265 if (usec
< s
->previous_time
)
1266 usec
= s
->previous_time
;
1268 s
->previous_time
= usec
;
1277 static pa_usec_t
time_counter_diff(pa_stream
*s
, pa_usec_t a
, pa_usec_t b
, int *negative
) {
1279 assert(s
->ref
>= 1);
1287 if (negative
&& s
->direction
== PA_STREAM_RECORD
) {
1295 int pa_stream_get_latency(pa_stream
*s
, pa_usec_t
*r_usec
, int *negative
) {
1301 assert(s
->ref
>= 1);
1304 PA_CHECK_VALIDITY(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1305 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1306 PA_CHECK_VALIDITY(s
->context
, s
->timing_info_valid
, PA_ERR_NODATA
);
1307 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_PLAYBACK
|| !s
->timing_info
.write_index_corrupt
, PA_ERR_NODATA
);
1308 PA_CHECK_VALIDITY(s
->context
, s
->direction
!= PA_STREAM_RECORD
|| !s
->timing_info
.read_index_corrupt
, PA_ERR_NODATA
);
1310 if ((r
= pa_stream_get_time(s
, &t
)) < 0)
1313 if (s
->direction
== PA_STREAM_PLAYBACK
)
1314 cindex
= s
->timing_info
.write_index
;
1316 cindex
= s
->timing_info
.read_index
;
1321 c
= pa_bytes_to_usec(cindex
, &s
->sample_spec
);
1323 if (s
->direction
== PA_STREAM_PLAYBACK
)
1324 *r_usec
= time_counter_diff(s
, c
, t
, negative
);
1326 *r_usec
= time_counter_diff(s
, t
, c
, negative
);
1331 const pa_timing_info
* pa_stream_get_timing_info(pa_stream
*s
) {
1333 assert(s
->ref
>= 1);
1335 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1336 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1337 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->timing_info_valid
, PA_ERR_BADSTATE
);
1339 return &s
->timing_info
;
1342 const pa_sample_spec
* pa_stream_get_sample_spec(pa_stream
*s
) {
1344 assert(s
->ref
>= 1);
1346 return &s
->sample_spec
;
1349 const pa_channel_map
* pa_stream_get_channel_map(pa_stream
*s
) {
1351 assert(s
->ref
>= 1);
1353 return &s
->channel_map
;
1356 const pa_buffer_attr
* pa_stream_get_buffer_attr(pa_stream
*s
) {
1358 assert(s
->ref
>= 1);
1360 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->state
== PA_STREAM_READY
, PA_ERR_BADSTATE
);
1361 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
, s
->direction
!= PA_STREAM_UPLOAD
, PA_ERR_BADSTATE
);
1362 PA_CHECK_VALIDITY_RETURN_NULL(s
->context
,
1363 pa_context_get_server_protocol_version(s
->context
) >= 9, PA_ERR_NODATA
);
1365 return &s
->buffer_attr
;