]>
code.delx.au - pulseaudio/blob - src/simple.c
7 #include "polyp-error.h"
10 struct pa_mainloop
*mainloop
;
11 struct pa_context
*context
;
12 struct pa_stream
*stream
;
17 static int check_error(struct pa_simple
*p
, int *perror
) {
20 if (pa_context_is_dead(p
->context
) || (p
->stream
&& pa_stream_is_dead(p
->stream
))) {
22 *perror
= pa_context_errno(p
->context
);
29 static int iterate(struct pa_simple
*p
, int block
, int *perror
) {
30 assert(p
&& p
->context
&& p
->mainloop
);
32 if (check_error(p
, perror
) < 0)
35 if (!block
&& !pa_context_is_pending(p
->context
))
39 if (pa_mainloop_iterate(p
->mainloop
, 1, NULL
) < 0) {
41 *perror
= PA_ERROR_INTERNAL
;
45 if (check_error(p
, perror
) < 0)
48 } while (pa_context_is_pending(p
->context
));
53 struct pa_simple
* pa_simple_new(
56 enum pa_stream_direction dir
,
58 const char *stream_name
,
59 const struct pa_sample_spec
*ss
,
60 const struct pa_buffer_attr
*attr
,
64 int error
= PA_ERROR_INTERNAL
;
67 p
= malloc(sizeof(struct pa_simple
));
71 p
->mainloop
= pa_mainloop_new();
75 if (!(p
->context
= pa_context_new(pa_mainloop_get_api(p
->mainloop
), name
)))
78 if (pa_context_connect(p
->context
, server
, NULL
, NULL
) < 0) {
79 error
= pa_context_errno(p
->context
);
83 /* Wait until the context is ready */
84 while (!pa_context_is_ready(p
->context
)) {
85 if (iterate(p
, 1, &error
) < 0)
89 if (!(p
->stream
= pa_stream_new(p
->context
, dir
, dev
, stream_name
, ss
, attr
, NULL
, NULL
)))
92 /* Wait until the stream is ready */
93 while (!pa_stream_is_ready(p
->stream
)) {
94 if (iterate(p
, 1, &error
) < 0)
107 void pa_simple_free(struct pa_simple
*s
) {
111 pa_stream_free(s
->stream
);
114 pa_context_free(s
->context
);
117 pa_mainloop_free(s
->mainloop
);
122 int pa_simple_write(struct pa_simple
*p
, const void*data
, size_t length
, int *perror
) {
128 while (!(l
= pa_stream_writable_size(p
->stream
)))
129 if (iterate(p
, 1, perror
) < 0)
135 pa_stream_write(p
->stream
, data
, l
);
140 /* Make sure that no data is pending for write */
141 if (iterate(p
, 0, perror
) < 0)
147 int pa_simple_read(struct pa_simple
*s
, void*data
, size_t length
, int *perror
) {
152 static void drain_complete(struct pa_stream
*s
, void *userdata
) {
153 struct pa_simple
*p
= userdata
;
158 int pa_simple_drain(struct pa_simple
*p
, int *perror
) {
161 pa_stream_drain(p
->stream
, drain_complete
, p
);
163 while (!p
->drained
) {
164 if (iterate(p
, 1, perror
) < 0) {
165 pa_stream_drain(p
->stream
, NULL
, NULL
);