4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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 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.
16 You should have received a copy of the GNU 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
30 #include "polyplib-internal.h"
32 #include "pstream-util.h"
35 struct pa_stream
*pa_stream_new(struct pa_context
*c
, const char *name
, const struct pa_sample_spec
*ss
) {
39 s
= pa_xmalloc(sizeof(struct pa_stream
));
43 s
->read_callback
= NULL
;
44 s
->read_userdata
= NULL
;
45 s
->write_callback
= NULL
;
46 s
->write_userdata
= NULL
;
47 s
->state_callback
= NULL
;
48 s
->state_userdata
= NULL
;
50 s
->state
= PA_STREAM_NODIRECTION
;
51 s
->name
= pa_xstrdup(name
);
55 s
->device_index
= PA_INVALID_INDEX
;
56 s
->requested_bytes
= 0;
57 s
->state
= PA_STREAM_DISCONNECTED
;
58 memset(&s
->buffer_attr
, 0, sizeof(s
->buffer_attr
));
60 PA_LLIST_PREPEND(struct pa_stream
, c
->streams
, s
);
62 return pa_stream_ref(s
);
65 static void stream_free(struct pa_stream
*s
) {
71 void pa_stream_unref(struct pa_stream
*s
) {
72 assert(s
&& s
->ref
>= 1);
78 struct pa_stream
* pa_stream_ref(struct pa_stream
*s
) {
79 assert(s
&& s
->ref
>= 1);
84 enum pa_stream_state
pa_stream_get_state(struct pa_stream
*s
) {
85 assert(s
&& s
->ref
>= 1);
89 struct pa_context
* pa_stream_get_context(struct pa_stream
*s
) {
90 assert(s
&& s
->ref
>= 1);
94 uint32_t pa_stream_get_index(struct pa_stream
*s
) {
95 assert(s
&& s
->ref
>= 1);
96 return s
->device_index
;
99 void pa_stream_set_state(struct pa_stream
*s
, enum pa_stream_state st
) {
100 assert(s
&& s
->ref
>= 1);
109 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
) && s
->context
) {
110 if (s
->channel_valid
)
111 pa_dynarray_put((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, s
->channel
, NULL
);
113 PA_LLIST_REMOVE(struct pa_stream
, s
->context
->streams
, s
);
117 if (s
->state_callback
)
118 s
->state_callback(s
, s
->state_userdata
);
123 void pa_command_stream_killed(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
124 struct pa_context
*c
= userdata
;
127 assert(pd
&& (command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
) && t
&& c
);
131 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
132 !pa_tagstruct_eof(t
)) {
133 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
137 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, channel
)))
140 c
->error
= PA_ERROR_KILLED
;
141 pa_stream_set_state(s
, PA_STREAM_FAILED
);
147 void pa_command_request(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
149 struct pa_context
*c
= userdata
;
150 uint32_t bytes
, channel
;
151 assert(pd
&& command
== PA_COMMAND_REQUEST
&& t
&& c
);
155 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
156 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
157 !pa_tagstruct_eof(t
)) {
158 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
162 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
165 if (s
->state
!= PA_STREAM_READY
)
170 s
->requested_bytes
+= bytes
;
172 if (s
->requested_bytes
&& s
->write_callback
)
173 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
181 void pa_create_stream_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
182 struct pa_stream
*s
= userdata
;
183 assert(pd
&& s
&& s
->state
== PA_STREAM_CREATING
);
187 if (command
!= PA_COMMAND_REPLY
) {
188 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
191 pa_stream_set_state(s
, PA_STREAM_FAILED
);
195 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
196 ((s
->direction
!= PA_STREAM_UPLOAD
) && pa_tagstruct_getu32(t
, &s
->device_index
) < 0) ||
197 ((s
->direction
== PA_STREAM_PLAYBACK
) && pa_tagstruct_getu32(t
, &s
->requested_bytes
) < 0) ||
198 !pa_tagstruct_eof(t
)) {
199 pa_context_fail(s
->context
, PA_ERROR_PROTOCOL
);
203 s
->channel_valid
= 1;
204 pa_dynarray_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, s
->channel
, s
);
205 pa_stream_set_state(s
, PA_STREAM_READY
);
207 if (s
->requested_bytes
&& s
->ref
> 1 && s
->write_callback
)
208 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
214 static void create_stream(struct pa_stream
*s
, const char *dev
, const struct pa_buffer_attr
*attr
) {
215 struct pa_tagstruct
*t
;
217 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_DISCONNECTED
);
222 s
->buffer_attr
= *attr
;
224 s
->buffer_attr
.maxlength
= DEFAULT_MAXLENGTH
;
225 s
->buffer_attr
.tlength
= DEFAULT_TLENGTH
;
226 s
->buffer_attr
.prebuf
= DEFAULT_PREBUF
;
227 s
->buffer_attr
.minreq
= DEFAULT_MINREQ
;
228 s
->buffer_attr
.fragsize
= DEFAULT_FRAGSIZE
;
231 pa_stream_set_state(s
, PA_STREAM_CREATING
);
233 t
= pa_tagstruct_new(NULL
, 0);
237 if (s
->direction
== PA_STREAM_PLAYBACK
)
238 dev
= getenv(ENV_DEFAULT_SINK
);
240 dev
= getenv(ENV_DEFAULT_SOURCE
);
243 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
);
244 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
245 pa_tagstruct_puts(t
, s
->name
);
246 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
247 pa_tagstruct_putu32(t
, PA_INVALID_INDEX
);
248 pa_tagstruct_puts(t
, dev
? dev
: "");
249 pa_tagstruct_putu32(t
, s
->buffer_attr
.maxlength
);
250 if (s
->direction
== PA_STREAM_PLAYBACK
) {
251 pa_tagstruct_putu32(t
, s
->buffer_attr
.tlength
);
252 pa_tagstruct_putu32(t
, s
->buffer_attr
.prebuf
);
253 pa_tagstruct_putu32(t
, s
->buffer_attr
.minreq
);
255 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
257 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
258 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
);
263 void pa_stream_connect_playback(struct pa_stream
*s
, const char *dev
, const struct pa_buffer_attr
*attr
) {
264 assert(s
&& s
->context
->state
== PA_CONTEXT_READY
&& s
->ref
>= 1);
265 s
->direction
= PA_STREAM_PLAYBACK
;
266 create_stream(s
, dev
, attr
);
269 void pa_stream_connect_record(struct pa_stream
*s
, const char *dev
, const struct pa_buffer_attr
*attr
) {
270 assert(s
&& s
->context
->state
== PA_CONTEXT_READY
&& s
->ref
>= 1);
271 s
->direction
= PA_STREAM_RECORD
;
272 create_stream(s
, dev
, attr
);
275 void pa_stream_write(struct pa_stream
*s
, const void *data
, size_t length
, void (*free_cb
)(void *p
), size_t delta
) {
276 struct pa_memchunk chunk
;
277 assert(s
&& s
->context
&& data
&& length
&& s
->state
== PA_STREAM_READY
&& s
->ref
>= 1);
280 chunk
.memblock
= pa_memblock_new_user((void*) data
, length
, free_cb
, s
->context
->memblock_stat
);
281 assert(chunk
.memblock
&& chunk
.memblock
->data
);
283 chunk
.memblock
= pa_memblock_new(length
, s
->context
->memblock_stat
);
284 assert(chunk
.memblock
&& chunk
.memblock
->data
);
285 memcpy(chunk
.memblock
->data
, data
, length
);
288 chunk
.length
= length
;
290 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, delta
, &chunk
);
291 pa_memblock_unref(chunk
.memblock
);
293 if (length
< s
->requested_bytes
)
294 s
->requested_bytes
-= length
;
296 s
->requested_bytes
= 0;
299 size_t pa_stream_writable_size(struct pa_stream
*s
) {
300 assert(s
&& s
->state
== PA_STREAM_READY
&& s
->ref
>= 1);
301 return s
->requested_bytes
;
304 struct pa_operation
* pa_stream_drain(struct pa_stream
*s
, void (*cb
) (struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
305 struct pa_operation
*o
;
306 struct pa_tagstruct
*t
;
308 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
);
310 o
= pa_operation_new(s
->context
, s
);
313 o
->userdata
= userdata
;
315 t
= pa_tagstruct_new(NULL
, 0);
317 pa_tagstruct_putu32(t
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
);
318 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
319 pa_tagstruct_putu32(t
, s
->channel
);
320 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
321 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
323 return pa_operation_ref(o
);
326 static void stream_get_latency_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
327 struct pa_operation
*o
= userdata
;
328 struct pa_latency_info i
, *p
= NULL
;
329 struct timeval local
, remote
, now
;
330 assert(pd
&& o
&& o
->stream
&& o
->context
);
332 if (command
!= PA_COMMAND_REPLY
) {
333 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
336 } else if (pa_tagstruct_get_usec(t
, &i
.buffer_usec
) < 0 ||
337 pa_tagstruct_get_usec(t
, &i
.sink_usec
) < 0 ||
338 pa_tagstruct_get_boolean(t
, &i
.playing
) < 0 ||
339 pa_tagstruct_getu32(t
, &i
.queue_length
) < 0 ||
340 pa_tagstruct_get_timeval(t
, &local
) < 0 ||
341 pa_tagstruct_get_timeval(t
, &remote
) < 0 ||
342 !pa_tagstruct_eof(t
)) {
343 pa_context_fail(o
->context
, PA_ERROR_PROTOCOL
);
348 gettimeofday(&now
, NULL
);
350 if (pa_timeval_cmp(&local
, &remote
) < 0 && pa_timeval_cmp(&remote
, &now
))
351 /* local and remote seem to have synchronized clocks */
352 i
.transport_usec
= pa_timeval_diff(&remote
, &local
);
354 /* clocks are not synchronized, let's estimate latency then */
355 i
.transport_usec
= pa_timeval_diff(&now
, &local
)/2;
358 void (*cb
)(struct pa_stream
*s
, const struct pa_latency_info
*i
, void *userdata
) = o
->callback
;
359 cb(o
->stream
, p
, o
->userdata
);
363 pa_operation_done(o
);
364 pa_operation_unref(o
);
367 struct pa_operation
* pa_stream_get_latency(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*p
, const struct pa_latency_info
*i
, void *userdata
), void *userdata
) {
369 struct pa_operation
*o
;
370 struct pa_tagstruct
*t
;
373 o
= pa_operation_new(s
->context
, s
);
376 o
->userdata
= userdata
;
378 t
= pa_tagstruct_new(NULL
, 0);
380 pa_tagstruct_putu32(t
, PA_COMMAND_GET_PLAYBACK_LATENCY
);
381 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
382 pa_tagstruct_putu32(t
, s
->channel
);
384 gettimeofday(&now
, NULL
);
385 pa_tagstruct_put_timeval(t
, &now
);
387 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
388 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, stream_get_latency_callback
, o
);
390 return pa_operation_ref(o
);
393 void pa_stream_disconnect_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
394 struct pa_stream
*s
= userdata
;
395 assert(pd
&& s
&& s
->ref
>= 1);
399 if (command
!= PA_COMMAND_REPLY
) {
400 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
403 pa_stream_set_state(s
, PA_STREAM_FAILED
);
405 } else if (!pa_tagstruct_eof(t
)) {
406 pa_context_fail(s
->context
, PA_ERROR_PROTOCOL
);
410 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
416 void pa_stream_disconnect(struct pa_stream
*s
) {
417 struct pa_tagstruct
*t
;
419 assert(s
&& s
->ref
>= 1);
421 if (!s
->channel_valid
|| !s
->context
->state
== PA_CONTEXT_READY
)
426 t
= pa_tagstruct_new(NULL
, 0);
429 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
430 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
));
431 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
432 pa_tagstruct_putu32(t
, s
->channel
);
433 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
434 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
);
439 void pa_stream_set_read_callback(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*p
, const void*data
, size_t length
, void *userdata
), void *userdata
) {
440 assert(s
&& s
->ref
>= 1);
441 s
->read_callback
= cb
;
442 s
->read_userdata
= userdata
;
445 void pa_stream_set_write_callback(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*p
, size_t length
, void *userdata
), void *userdata
) {
446 assert(s
&& s
->ref
>= 1);
447 s
->write_callback
= cb
;
448 s
->write_userdata
= userdata
;
451 void pa_stream_set_state_callback(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, void *userdata
), void *userdata
) {
452 assert(s
&& s
->ref
>= 1);
453 s
->state_callback
= cb
;
454 s
->state_userdata
= userdata
;
457 void pa_stream_simple_ack_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
458 struct pa_operation
*o
= userdata
;
460 assert(pd
&& o
&& o
->context
&& o
->ref
>= 1);
462 if (command
!= PA_COMMAND_REPLY
) {
463 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
467 } else if (!pa_tagstruct_eof(t
)) {
468 pa_context_fail(o
->context
, PA_ERROR_PROTOCOL
);
473 void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
) = o
->callback
;
474 cb(o
->stream
, success
, o
->userdata
);
478 pa_operation_done(o
);
479 pa_operation_unref(o
);
482 struct pa_operation
* pa_stream_cork(struct pa_stream
*s
, int b
, void (*cb
) (struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
483 struct pa_operation
*o
;
484 struct pa_tagstruct
*t
;
486 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
);
488 o
= pa_operation_new(s
->context
, s
);
491 o
->userdata
= userdata
;
493 t
= pa_tagstruct_new(NULL
, 0);
495 pa_tagstruct_putu32(t
, PA_COMMAND_CORK_PLAYBACK_STREAM
);
496 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
497 pa_tagstruct_putu32(t
, s
->channel
);
498 pa_tagstruct_putu32(t
, !!b
);
499 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
500 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
502 return pa_operation_ref(o
);
505 struct pa_operation
* pa_stream_send_simple_command(struct pa_stream
*s
, uint32_t command
, void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
506 struct pa_tagstruct
*t
;
507 struct pa_operation
*o
;
509 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
);
511 o
= pa_operation_new(s
->context
, s
);
513 o
->userdata
= userdata
;
515 t
= pa_tagstruct_new(NULL
, 0);
516 pa_tagstruct_putu32(t
, command
);
517 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
518 pa_tagstruct_putu32(t
, s
->channel
);
519 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
520 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
522 return pa_operation_ref(o
);
526 struct pa_operation
* pa_stream_flush(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
527 return pa_stream_send_simple_command(s
, PA_COMMAND_FLUSH_PLAYBACK_STREAM
, cb
, userdata
);
530 struct pa_operation
* pa_stream_trigger(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
531 return pa_stream_send_simple_command(s
, PA_COMMAND_TRIGGER_PLAYBACK_STREAM
, cb
, userdata
);