]> code.delx.au - pulseaudio/blob - src/simple.c
fix recording for simpel and esound protocols
[pulseaudio] / src / simple.c
1 #include <assert.h>
2 #include <stdlib.h>
3
4 #include "simple.h"
5 #include "polyp.h"
6 #include "mainloop.h"
7 #include "polyp-error.h"
8
9 struct pa_simple {
10 struct pa_mainloop *mainloop;
11 struct pa_context *context;
12 struct pa_stream *stream;
13
14 int dead, drained;
15 };
16
17 static int check_error(struct pa_simple *p, int *perror) {
18 assert(p);
19
20 if (pa_context_is_dead(p->context) || (p->stream && pa_stream_is_dead(p->stream))) {
21 if (perror)
22 *perror = pa_context_errno(p->context);
23 return -1;
24 }
25
26 return 0;
27 }
28
29 static int iterate(struct pa_simple *p, int block, int *perror) {
30 assert(p && p->context && p->mainloop);
31
32 if (check_error(p, perror) < 0)
33 return -1;
34
35 if (!block && !pa_context_is_pending(p->context))
36 return 0;
37
38 do {
39 if (pa_mainloop_iterate(p->mainloop, 1, NULL) < 0) {
40 if (perror)
41 *perror = PA_ERROR_INTERNAL;
42 return -1;
43 }
44
45 if (check_error(p, perror) < 0)
46 return -1;
47
48 } while (pa_context_is_pending(p->context));
49
50 return 0;
51 }
52
53 struct pa_simple* pa_simple_new(
54 const char *server,
55 const char *name,
56 enum pa_stream_direction dir,
57 const char *dev,
58 const char *stream_name,
59 const struct pa_sample_spec *ss,
60 const struct pa_buffer_attr *attr,
61 int *perror) {
62
63 struct pa_simple *p;
64 int error = PA_ERROR_INTERNAL;
65 assert(ss);
66
67 p = malloc(sizeof(struct pa_simple));
68 assert(p);
69 p->context = NULL;
70 p->stream = NULL;
71 p->mainloop = pa_mainloop_new();
72 assert(p->mainloop);
73 p->dead = 0;
74
75 if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name)))
76 goto fail;
77
78 if (pa_context_connect(p->context, server, NULL, NULL) < 0) {
79 error = pa_context_errno(p->context);
80 goto fail;
81 }
82
83 /* Wait until the context is ready */
84 while (!pa_context_is_ready(p->context)) {
85 if (iterate(p, 1, &error) < 0)
86 goto fail;
87 }
88
89 if (!(p->stream = pa_stream_new(p->context, dir, dev, stream_name, ss, attr, NULL, NULL)))
90 goto fail;
91
92 /* Wait until the stream is ready */
93 while (!pa_stream_is_ready(p->stream)) {
94 if (iterate(p, 1, &error) < 0)
95 goto fail;
96 }
97
98 return p;
99
100 fail:
101 if (perror)
102 *perror = error;
103 pa_simple_free(p);
104 return NULL;
105 }
106
107 void pa_simple_free(struct pa_simple *s) {
108 assert(s);
109
110 if (s->stream)
111 pa_stream_free(s->stream);
112
113 if (s->context)
114 pa_context_free(s->context);
115
116 if (s->mainloop)
117 pa_mainloop_free(s->mainloop);
118
119 free(s);
120 }
121
122 int pa_simple_write(struct pa_simple *p, const void*data, size_t length, int *perror) {
123 assert(p && data);
124
125 while (length > 0) {
126 size_t l;
127
128 while (!(l = pa_stream_writable_size(p->stream)))
129 if (iterate(p, 1, perror) < 0)
130 return -1;
131
132 if (l > length)
133 l = length;
134
135 pa_stream_write(p->stream, data, l);
136 data += l;
137 length -= l;
138 }
139
140 /* Make sure that no data is pending for write */
141 if (iterate(p, 0, perror) < 0)
142 return -1;
143
144 return 0;
145 }
146
147 int pa_simple_read(struct pa_simple *s, void*data, size_t length, int *perror) {
148 assert(0);
149 }
150
151
152 static void drain_complete(struct pa_stream *s, void *userdata) {
153 struct pa_simple *p = userdata;
154 assert(s && p);
155 p->drained = 1;
156 }
157
158 int pa_simple_drain(struct pa_simple *p, int *perror) {
159 assert(p);
160 p->drained = 0;
161 pa_stream_drain(p->stream, drain_complete, p);
162
163 while (!p->drained) {
164 if (iterate(p, 1, perror) < 0) {
165 pa_stream_drain(p->stream, NULL, NULL);
166 return -1;
167 }
168 }
169
170 return 0;
171 }