]> code.delx.au - pulseaudio/blob - src/pacat.c
adjust file references due to renaming
[pulseaudio] / src / pacat.c
1 #include <signal.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <unistd.h>
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include "polyplib.h"
10 #include "polyplib-error.h"
11 #include "mainloop.h"
12 #include "mainloop-signal.h"
13
14 static enum { RECORD, PLAYBACK } mode = PLAYBACK;
15
16 static struct pa_context *context = NULL;
17 static struct pa_stream *stream = NULL;
18 static struct pa_mainloop_api *mainloop_api = NULL;
19
20 static void *buffer = NULL;
21 static size_t buffer_length = 0, buffer_index = 0;
22
23 static void* stdio_source = NULL;
24
25 static void quit(int ret) {
26 assert(mainloop_api);
27 mainloop_api->quit(mainloop_api, ret);
28 }
29
30 static void context_die_callback(struct pa_context *c, void *userdata) {
31 assert(c);
32 fprintf(stderr, "Connection to server shut down, exiting.\n");
33 quit(1);
34 }
35
36 static void stream_die_callback(struct pa_stream *s, void *userdata) {
37 assert(s);
38 fprintf(stderr, "Stream deleted, exiting.\n");
39 quit(1);
40 }
41
42 static void do_stream_write(size_t length) {
43 size_t l;
44 assert(length);
45
46 if (!buffer || !buffer_length)
47 return;
48
49 l = length;
50 if (l > buffer_length)
51 l = buffer_length;
52
53 pa_stream_write(stream, buffer+buffer_index, l);
54 buffer_length -= l;
55 buffer_index += l;
56
57 if (!buffer_length) {
58 free(buffer);
59 buffer = NULL;
60 buffer_index = buffer_length = 0;
61 }
62 }
63
64 static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
65 assert(s && length);
66
67 if (stdio_source)
68 mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_INPUT);
69
70 if (!buffer)
71 return;
72
73 do_stream_write(length);
74 }
75
76 static void stream_read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) {
77 assert(s && data && length);
78
79 if (stdio_source)
80 mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_OUTPUT);
81
82 if (buffer) {
83 fprintf(stderr, "Buffer overrrun, dropping incoming data\n");
84 return;
85 }
86
87 buffer = malloc(buffer_length = length);
88 assert(buffer);
89 memcpy(buffer, data, length);
90 buffer_index = 0;
91 }
92
93 static void stream_complete_callback(struct pa_stream*s, int success, void *userdata) {
94 assert(s);
95
96 if (!success) {
97 fprintf(stderr, "Stream creation failed: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
98 quit(1);
99 return;
100 }
101
102 fprintf(stderr, "Stream created.\n");
103 }
104
105 static void context_complete_callback(struct pa_context *c, int success, void *userdata) {
106 static const struct pa_sample_spec ss = {
107 .format = PA_SAMPLE_S16LE,
108 .rate = 44100,
109 .channels = 2
110 };
111
112 assert(c && !stream);
113
114 if (!success) {
115 fprintf(stderr, "Connection failed: %s\n", pa_strerror(pa_context_errno(c)));
116 goto fail;
117 }
118
119 fprintf(stderr, "Connection established.\n");
120
121 if (!(stream = pa_stream_new(c, mode == PLAYBACK ? PA_STREAM_PLAYBACK : PA_STREAM_RECORD, NULL, "pacat", &ss, NULL, stream_complete_callback, NULL))) {
122 fprintf(stderr, "pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(c)));
123 goto fail;
124 }
125
126 pa_stream_set_die_callback(stream, stream_die_callback, NULL);
127 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
128 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
129
130 return;
131
132 fail:
133 quit(1);
134 }
135
136 static void context_drain_complete(struct pa_context*c, void *userdata) {
137 quit(0);
138 }
139
140 static void stream_drain_complete(struct pa_stream*s, void *userdata) {
141 fprintf(stderr, "Playback stream drained.\n");
142
143 pa_stream_free(stream);
144 stream = NULL;
145
146 if (pa_context_drain(context, context_drain_complete, NULL) < 0)
147 quit(0);
148 else
149 fprintf(stderr, "Draining connection to server.\n");
150 }
151
152 static void stdin_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
153 size_t l, w = 0;
154 ssize_t r;
155 assert(a == mainloop_api && id && stdio_source == id);
156
157 if (buffer) {
158 mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_NULL);
159 return;
160 }
161
162 if (!stream || !pa_stream_is_ready(stream) || !(l = w = pa_stream_writable_size(stream)))
163 l = 4096;
164
165 buffer = malloc(l);
166 assert(buffer);
167 if ((r = read(fd, buffer, l)) <= 0) {
168 if (r == 0) {
169 fprintf(stderr, "Got EOF.\n");
170 pa_stream_drain(stream, stream_drain_complete, NULL);
171 } else {
172 fprintf(stderr, "read() failed: %s\n", strerror(errno));
173 quit(1);
174 }
175
176 mainloop_api->cancel_io(mainloop_api, stdio_source);
177 stdio_source = NULL;
178 return;
179 }
180
181 buffer_length = r;
182 buffer_index = 0;
183
184 if (w)
185 do_stream_write(w);
186 }
187
188 static void stdout_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
189 ssize_t r;
190 assert(a == mainloop_api && id && stdio_source == id);
191
192 if (!buffer) {
193 mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_NULL);
194 return;
195 }
196
197 assert(buffer_length);
198
199 if ((r = write(fd, buffer+buffer_index, buffer_length)) <= 0) {
200 fprintf(stderr, "write() failed: %s\n", strerror(errno));
201 quit(1);
202
203 mainloop_api->cancel_io(mainloop_api, stdio_source);
204 stdio_source = NULL;
205 return;
206 }
207
208 buffer_length -= r;
209 buffer_index += r;
210
211 if (!buffer_length) {
212 free(buffer);
213 buffer = NULL;
214 buffer_length = buffer_index = 0;
215 }
216 }
217
218 static void exit_signal_callback(void *id, int sig, void *userdata) {
219 fprintf(stderr, "Got SIGINT, exiting.\n");
220 quit(0);
221
222 }
223
224 static void stream_get_latency_callback(struct pa_stream *s, uint32_t latency, void *userdata) {
225 assert(s);
226
227 if (latency == (uint32_t) -1) {
228 fprintf(stderr, "Failed to get latency: %s\n", strerror(errno));
229 quit(1);
230 return;
231 }
232
233 fprintf(stderr, "Current latency is %u usecs.\n", latency);
234 }
235
236 static void sigusr1_signal_callback(void *id, int sig, void *userdata) {
237 if (mode == PLAYBACK) {
238 fprintf(stderr, "Got SIGUSR1, requesting latency.\n");
239 pa_stream_get_latency(stream, stream_get_latency_callback, NULL);
240 }
241 }
242
243 int main(int argc, char *argv[]) {
244 struct pa_mainloop* m = NULL;
245 int ret = 1, r;
246 char *bn;
247
248 if (!(bn = strrchr(argv[0], '/')))
249 bn = argv[0];
250
251 if (strstr(bn, "rec") || strstr(bn, "mon"))
252 mode = RECORD;
253 else if (strstr(bn, "cat") || strstr(bn, "play"))
254 mode = PLAYBACK;
255
256 fprintf(stderr, "Opening a %s stream.\n", mode == RECORD ? "recording" : "playback");
257
258 if (!(m = pa_mainloop_new())) {
259 fprintf(stderr, "pa_mainloop_new() failed.\n");
260 goto quit;
261 }
262
263 mainloop_api = pa_mainloop_get_api(m);
264
265 r = pa_signal_init(mainloop_api);
266 assert(r == 0);
267 pa_signal_register(SIGINT, exit_signal_callback, NULL);
268 pa_signal_register(SIGUSR1, sigusr1_signal_callback, NULL);
269 signal(SIGPIPE, SIG_IGN);
270
271 if (!(stdio_source = mainloop_api->source_io(mainloop_api,
272 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
273 mode == PLAYBACK ? PA_MAINLOOP_API_IO_EVENT_INPUT : PA_MAINLOOP_API_IO_EVENT_OUTPUT,
274 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
275 fprintf(stderr, "source_io() failed.\n");
276 goto quit;
277 }
278
279 if (!(context = pa_context_new(mainloop_api, argv[0]))) {
280 fprintf(stderr, "pa_context_new() failed.\n");
281 goto quit;
282 }
283
284 if (pa_context_connect(context, NULL, context_complete_callback, NULL) < 0) {
285 fprintf(stderr, "pa_context_connext() failed.\n");
286 goto quit;
287 }
288
289 pa_context_set_die_callback(context, context_die_callback, NULL);
290
291 if (pa_mainloop_run(m, &ret) < 0) {
292 fprintf(stderr, "pa_mainloop_run() failed.\n");
293 goto quit;
294 }
295
296 quit:
297 if (stream)
298 pa_stream_free(stream);
299 if (context)
300 pa_context_free(context);
301
302 if (m) {
303 pa_signal_done();
304 pa_mainloop_free(m);
305 }
306
307 if (buffer)
308 free(buffer);
309
310 return ret;
311 }