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"
34 struct pa_stream
*pa_stream_new(struct pa_context
*c
, const char *name
, const struct pa_sample_spec
*ss
) {
38 s
= pa_xmalloc(sizeof(struct pa_stream
));
42 s
->read_callback
= NULL
;
43 s
->read_userdata
= NULL
;
44 s
->write_callback
= NULL
;
45 s
->write_userdata
= NULL
;
46 s
->state_callback
= NULL
;
47 s
->state_userdata
= NULL
;
49 s
->state
= PA_STREAM_NODIRECTION
;
50 s
->name
= pa_xstrdup(name
);
54 s
->device_index
= PA_INVALID_INDEX
;
55 s
->requested_bytes
= 0;
56 s
->state
= PA_STREAM_DISCONNECTED
;
57 memset(&s
->buffer_attr
, 0, sizeof(s
->buffer_attr
));
59 PA_LLIST_PREPEND(struct pa_stream
, c
->streams
, s
);
61 return pa_stream_ref(s
);
64 static void stream_free(struct pa_stream
*s
) {
70 void pa_stream_unref(struct pa_stream
*s
) {
71 assert(s
&& s
->ref
>= 1);
77 struct pa_stream
* pa_stream_ref(struct pa_stream
*s
) {
78 assert(s
&& s
->ref
>= 1);
83 enum pa_stream_state
pa_stream_get_state(struct pa_stream
*s
) {
84 assert(s
&& s
->ref
>= 1);
88 struct pa_context
* pa_stream_get_context(struct pa_stream
*s
) {
89 assert(s
&& s
->ref
>= 1);
93 uint32_t pa_stream_get_index(struct pa_stream
*s
) {
94 assert(s
&& s
->ref
>= 1);
95 return s
->device_index
;
98 void pa_stream_set_state(struct pa_stream
*s
, enum pa_stream_state st
) {
99 assert(s
&& s
->ref
>= 1);
108 if ((st
== PA_STREAM_FAILED
|| st
== PA_STREAM_TERMINATED
) && s
->context
) {
109 if (s
->channel_valid
)
110 pa_dynarray_put((s
->direction
== PA_STREAM_PLAYBACK
) ? s
->context
->playback_streams
: s
->context
->record_streams
, s
->channel
, NULL
);
112 PA_LLIST_REMOVE(struct pa_stream
, s
->context
->streams
, s
);
116 if (s
->state_callback
)
117 s
->state_callback(s
, s
->state_userdata
);
122 void pa_command_stream_killed(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
123 struct pa_context
*c
= userdata
;
126 assert(pd
&& (command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
|| command
== PA_COMMAND_RECORD_STREAM_KILLED
) && t
&& c
);
130 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
131 !pa_tagstruct_eof(t
)) {
132 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
136 if (!(s
= pa_dynarray_get(command
== PA_COMMAND_PLAYBACK_STREAM_KILLED
? c
->playback_streams
: c
->record_streams
, channel
)))
139 c
->error
= PA_ERROR_KILLED
;
140 pa_stream_set_state(s
, PA_STREAM_FAILED
);
146 void pa_command_request(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
148 struct pa_context
*c
= userdata
;
149 uint32_t bytes
, channel
;
150 assert(pd
&& command
== PA_COMMAND_REQUEST
&& t
&& c
);
154 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
155 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
156 !pa_tagstruct_eof(t
)) {
157 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
161 if (!(s
= pa_dynarray_get(c
->playback_streams
, channel
)))
164 if (s
->state
!= PA_STREAM_READY
)
169 s
->requested_bytes
+= bytes
;
171 if (s
->requested_bytes
&& s
->write_callback
)
172 s
->write_callback(s
, s
->requested_bytes
, s
->write_userdata
);
180 void pa_create_stream_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
181 struct pa_stream
*s
= userdata
;
182 assert(pd
&& s
&& s
->state
== PA_STREAM_CREATING
);
186 if (command
!= PA_COMMAND_REPLY
) {
187 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
190 pa_stream_set_state(s
, PA_STREAM_FAILED
);
194 if (pa_tagstruct_getu32(t
, &s
->channel
) < 0 ||
195 ((s
->direction
!= PA_STREAM_UPLOAD
) && pa_tagstruct_getu32(t
, &s
->device_index
) < 0) ||
196 !pa_tagstruct_eof(t
)) {
197 pa_context_fail(s
->context
, PA_ERROR_PROTOCOL
);
201 s
->channel_valid
= 1;
202 pa_dynarray_put((s
->direction
== PA_STREAM_RECORD
) ? s
->context
->record_streams
: s
->context
->playback_streams
, s
->channel
, s
);
203 pa_stream_set_state(s
, PA_STREAM_READY
);
209 static void create_stream(struct pa_stream
*s
, const char *dev
, const struct pa_buffer_attr
*attr
) {
210 struct pa_tagstruct
*t
;
212 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_DISCONNECTED
);
217 s
->buffer_attr
= *attr
;
219 s
->buffer_attr
.maxlength
= DEFAULT_MAXLENGTH
;
220 s
->buffer_attr
.tlength
= DEFAULT_TLENGTH
;
221 s
->buffer_attr
.prebuf
= DEFAULT_PREBUF
;
222 s
->buffer_attr
.minreq
= DEFAULT_MINREQ
;
223 s
->buffer_attr
.fragsize
= DEFAULT_FRAGSIZE
;
226 pa_stream_set_state(s
, PA_STREAM_CREATING
);
228 t
= pa_tagstruct_new(NULL
, 0);
232 if (s
->direction
== PA_STREAM_PLAYBACK
)
233 dev
= getenv(ENV_DEFAULT_SINK
);
235 dev
= getenv(ENV_DEFAULT_SOURCE
);
238 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_CREATE_PLAYBACK_STREAM
: PA_COMMAND_CREATE_RECORD_STREAM
);
239 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
240 pa_tagstruct_puts(t
, s
->name
);
241 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
242 pa_tagstruct_putu32(t
, (uint32_t) -1);
243 pa_tagstruct_puts(t
, dev
? dev
: "");
244 pa_tagstruct_putu32(t
, s
->buffer_attr
.maxlength
);
245 if (s
->direction
== PA_STREAM_PLAYBACK
) {
246 pa_tagstruct_putu32(t
, s
->buffer_attr
.tlength
);
247 pa_tagstruct_putu32(t
, s
->buffer_attr
.prebuf
);
248 pa_tagstruct_putu32(t
, s
->buffer_attr
.minreq
);
250 pa_tagstruct_putu32(t
, s
->buffer_attr
.fragsize
);
252 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
253 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_create_stream_callback
, s
);
258 void pa_stream_connect_playback(struct pa_stream
*s
, const char *dev
, const struct pa_buffer_attr
*attr
) {
259 assert(s
&& s
->context
->state
== PA_CONTEXT_READY
&& s
->ref
>= 1);
260 s
->direction
= PA_STREAM_PLAYBACK
;
261 create_stream(s
, dev
, attr
);
264 void pa_stream_connect_record(struct pa_stream
*s
, const char *dev
, const struct pa_buffer_attr
*attr
) {
265 assert(s
&& s
->context
->state
== PA_CONTEXT_READY
&& s
->ref
>= 1);
266 s
->direction
= PA_STREAM_RECORD
;
267 create_stream(s
, dev
, attr
);
270 void pa_stream_write(struct pa_stream
*s
, const void *data
, size_t length
, void (*free_cb
)(void *p
)) {
271 struct pa_memchunk chunk
;
272 assert(s
&& s
->context
&& data
&& length
&& s
->state
== PA_STREAM_READY
&& s
->ref
>= 1);
275 chunk
.memblock
= pa_memblock_new_user((void*) data
, length
, free_cb
, s
->context
->memblock_stat
);
276 assert(chunk
.memblock
&& chunk
.memblock
->data
);
278 chunk
.memblock
= pa_memblock_new(length
, s
->context
->memblock_stat
);
279 assert(chunk
.memblock
&& chunk
.memblock
->data
);
280 memcpy(chunk
.memblock
->data
, data
, length
);
283 chunk
.length
= length
;
285 pa_pstream_send_memblock(s
->context
->pstream
, s
->channel
, 0, &chunk
);
286 pa_memblock_unref(chunk
.memblock
);
288 if (length
< s
->requested_bytes
)
289 s
->requested_bytes
-= length
;
291 s
->requested_bytes
= 0;
294 size_t pa_stream_writable_size(struct pa_stream
*s
) {
295 assert(s
&& s
->state
== PA_STREAM_READY
&& s
->ref
>= 1);
296 return s
->requested_bytes
;
299 struct pa_operation
* pa_stream_drain(struct pa_stream
*s
, void (*cb
) (struct pa_stream
*s
, int success
, void *userdata
), void *userdata
) {
300 struct pa_operation
*o
;
301 struct pa_tagstruct
*t
;
303 assert(s
&& s
->ref
>= 1 && s
->state
== PA_STREAM_READY
);
305 o
= pa_operation_new(s
->context
, s
);
308 o
->userdata
= userdata
;
310 t
= pa_tagstruct_new(NULL
, 0);
312 pa_tagstruct_putu32(t
, PA_COMMAND_DRAIN_PLAYBACK_STREAM
);
313 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
314 pa_tagstruct_putu32(t
, s
->channel
);
315 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
316 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_simple_ack_callback
, o
);
318 return pa_operation_ref(o
);
321 static void stream_get_latency_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
322 struct pa_operation
*o
= userdata
;
324 assert(pd
&& o
&& o
->stream
&& o
->context
);
326 if (command
!= PA_COMMAND_REPLY
) {
327 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
330 latency
= (uint32_t) -1;
331 } else if (pa_tagstruct_getu32(t
, &latency
) < 0 || !pa_tagstruct_eof(t
)) {
332 pa_context_fail(o
->context
, PA_ERROR_PROTOCOL
);
337 void (*cb
)(struct pa_stream
*s
, uint32_t latency
, void *userdata
) = o
->callback
;
338 cb(o
->stream
, latency
, o
->userdata
);
342 pa_operation_done(o
);
343 pa_operation_unref(o
);
346 struct pa_operation
* pa_stream_get_latency(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*p
, uint32_t latency
, void *userdata
), void *userdata
) {
348 struct pa_operation
*o
;
349 struct pa_tagstruct
*t
;
351 o
= pa_operation_new(s
->context
, s
);
354 o
->userdata
= userdata
;
356 t
= pa_tagstruct_new(NULL
, 0);
358 pa_tagstruct_putu32(t
, PA_COMMAND_GET_PLAYBACK_LATENCY
);
359 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
360 pa_tagstruct_putu32(t
, s
->channel
);
361 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
362 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, stream_get_latency_callback
, o
);
364 return pa_operation_ref(o
);
367 void pa_stream_disconnect_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
368 struct pa_stream
*s
= userdata
;
369 assert(pd
&& s
&& s
->ref
>= 1);
373 if (command
!= PA_COMMAND_REPLY
) {
374 if (pa_context_handle_error(s
->context
, command
, t
) < 0)
377 pa_stream_set_state(s
, PA_STREAM_FAILED
);
379 } else if (!pa_tagstruct_eof(t
)) {
380 pa_context_fail(s
->context
, PA_ERROR_PROTOCOL
);
384 pa_stream_set_state(s
, PA_STREAM_TERMINATED
);
390 void pa_stream_disconnect(struct pa_stream
*s
) {
391 struct pa_tagstruct
*t
;
393 assert(s
&& s
->ref
>= 1);
395 if (!s
->channel_valid
|| !s
->context
->state
== PA_CONTEXT_READY
)
400 t
= pa_tagstruct_new(NULL
, 0);
403 pa_tagstruct_putu32(t
, s
->direction
== PA_STREAM_PLAYBACK
? PA_COMMAND_DELETE_PLAYBACK_STREAM
:
404 (s
->direction
== PA_STREAM_RECORD
? PA_COMMAND_DELETE_RECORD_STREAM
: PA_COMMAND_DELETE_UPLOAD_STREAM
));
405 pa_tagstruct_putu32(t
, tag
= s
->context
->ctag
++);
406 pa_tagstruct_putu32(t
, s
->channel
);
407 pa_pstream_send_tagstruct(s
->context
->pstream
, t
);
408 pa_pdispatch_register_reply(s
->context
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_stream_disconnect_callback
, s
);
413 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
) {
414 assert(s
&& s
->ref
>= 1);
415 s
->read_callback
= cb
;
416 s
->read_userdata
= userdata
;
419 void pa_stream_set_write_callback(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*p
, size_t length
, void *userdata
), void *userdata
) {
420 assert(s
&& s
->ref
>= 1);
421 s
->write_callback
= cb
;
422 s
->write_userdata
= userdata
;
425 void pa_stream_set_state_callback(struct pa_stream
*s
, void (*cb
)(struct pa_stream
*s
, void *userdata
), void *userdata
) {
426 assert(s
&& s
->ref
>= 1);
427 s
->state_callback
= cb
;
428 s
->state_userdata
= userdata
;
431 void pa_stream_simple_ack_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
432 struct pa_operation
*o
= userdata
;
434 assert(pd
&& o
&& o
->context
&& o
->ref
>= 1);
436 if (command
!= PA_COMMAND_REPLY
) {
437 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
441 } else if (!pa_tagstruct_eof(t
)) {
442 pa_context_fail(o
->context
, PA_ERROR_PROTOCOL
);
447 void (*cb
)(struct pa_stream
*s
, int success
, void *userdata
) = o
->callback
;
448 cb(o
->stream
, success
, o
->userdata
);
452 pa_operation_done(o
);
453 pa_operation_unref(o
);