]> code.delx.au - pulseaudio/blob - polyp/pactl.c
28b187b044cef55675b34a9a4244de70fdf18818
[pulseaudio] / polyp / pactl.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <signal.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <limits.h>
34
35 #include <sndfile.h>
36
37 #include <polyp/polyplib.h>
38 #include <polyp/polyplib-error.h>
39 #include <polyp/mainloop.h>
40 #include <polyp/mainloop-signal.h>
41 #include <polyp/sample.h>
42
43 #define BUFSIZE 1024
44
45 static struct pa_context *context = NULL;
46 static struct pa_mainloop_api *mainloop_api = NULL;
47
48 static char **process_argv = NULL;
49
50 static SNDFILE *sndfile = NULL;
51 static struct pa_stream *sample_stream = NULL;
52 static struct pa_sample_spec sample_spec;
53 static size_t sample_length = 0;
54
55 static char *sample_name = NULL;
56
57 static enum {
58 NONE,
59 EXIT,
60 STAT,
61 UPLOAD_SAMPLE,
62 PLAY_SAMPLE,
63 REMOVE_SAMPLE
64 } action = NONE;
65
66 static void quit(int ret) {
67 assert(mainloop_api);
68 mainloop_api->quit(mainloop_api, ret);
69 }
70
71 static void context_die_callback(struct pa_context *c, void *userdata) {
72 assert(c);
73 fprintf(stderr, "Connection to server shut down, exiting.\n");
74 quit(1);
75 }
76
77 static void context_drain_complete(struct pa_context *c, void *userdata) {
78 assert(c);
79 fprintf(stderr, "Connection to server shut down, exiting.\n");
80 quit(0);
81 }
82
83 static void drain(void) {
84 if (pa_context_drain(context, context_drain_complete, NULL) < 0)
85 quit(0);
86 }
87
88 static void stat_callback(struct pa_context *c, uint32_t blocks, uint32_t total, void *userdata) {
89 if (blocks == (uint32_t) -1) {
90 fprintf(stderr, "Failed to get statistics: %s\n", pa_strerror(pa_context_errno(c)));
91 quit(1);
92 return;
93 }
94
95 fprintf(stderr, "Currently in use: %u blocks containing %u bytes total.\n", blocks, total);
96 drain();
97 }
98
99 static void play_sample_callback(struct pa_context *c, int success, void *userdata) {
100 if (!success) {
101 fprintf(stderr, "Failed to play sample: %s\n", pa_strerror(pa_context_errno(c)));
102 quit(1);
103 return;
104 }
105
106 drain();
107 }
108
109 static void remove_sample_callback(struct pa_context *c, int success, void *userdata) {
110 if (!success) {
111 fprintf(stderr, "Failed to remove sample: %s\n", pa_strerror(pa_context_errno(c)));
112 quit(1);
113 return;
114 }
115
116 drain();
117 }
118
119 static void stream_die_callback(struct pa_stream *s, void *userdata) {
120 assert(s);
121 fprintf(stderr, "Stream deleted, exiting.\n");
122 quit(1);
123 }
124
125 static void finish_sample_callback(struct pa_stream *s, int success, void *userdata) {
126 assert(s);
127
128 if (!success) {
129 fprintf(stderr, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(context)));
130 quit(1);
131 return;
132 }
133
134 drain();
135 }
136
137 static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
138 sf_count_t l;
139 float *d;
140 assert(s && length && sndfile);
141
142 d = malloc(length);
143 assert(d);
144
145 assert(sample_length >= length);
146 l = length/pa_frame_size(&sample_spec);
147
148 if ((sf_readf_float(sndfile, d, l)) != l) {
149 free(d);
150 fprintf(stderr, "Premature end of file\n");
151 quit(1);
152 }
153
154 pa_stream_write(s, d, length);
155 free(d);
156
157 sample_length -= length;
158
159 if (sample_length <= 0) {
160 pa_stream_set_write_callback(sample_stream, NULL, NULL);
161 pa_stream_finish_sample(sample_stream, finish_sample_callback, NULL);
162 }
163 }
164
165 static void upload_callback(struct pa_stream *s, int success, void *userdata) {
166 if (!success) {
167 fprintf(stderr, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(context)));
168 quit(1);
169 }
170 }
171
172 static void context_complete_callback(struct pa_context *c, int success, void *userdata) {
173 assert(c);
174
175 if (!success) {
176 fprintf(stderr, "Connection failed: %s\n", pa_strerror(pa_context_errno(c)));
177 goto fail;
178 }
179
180 fprintf(stderr, "Connection established.\n");
181
182 if (action == STAT)
183 pa_context_stat(c, stat_callback, NULL);
184 else if (action == PLAY_SAMPLE)
185 pa_context_play_sample(c, process_argv[2], NULL, 0x100, play_sample_callback, NULL);
186 else if (action == REMOVE_SAMPLE)
187 pa_context_remove_sample(c, process_argv[2], remove_sample_callback, NULL);
188 else if (action == UPLOAD_SAMPLE) {
189 if (!(sample_stream = pa_context_upload_sample(c, sample_name, &sample_spec, sample_length, upload_callback, NULL))) {
190 fprintf(stderr, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(c)));
191 goto fail;
192 }
193
194 pa_stream_set_die_callback(sample_stream, stream_die_callback, NULL);
195 pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
196 } else {
197 assert(action == EXIT);
198 pa_context_exit(c);
199 drain();
200 }
201
202 return;
203
204 fail:
205 quit(1);
206 }
207
208 static void exit_signal_callback(void *id, int sig, void *userdata) {
209 fprintf(stderr, "Got SIGINT, exiting.\n");
210 quit(0);
211 }
212
213 int main(int argc, char *argv[]) {
214 struct pa_mainloop* m = NULL;
215 char tmp[PATH_MAX];
216
217 int ret = 1, r;
218
219 if (argc >= 2) {
220 if (!strcmp(argv[1], "stat"))
221 action = STAT;
222 else if (!strcmp(argv[1], "exit"))
223 action = EXIT;
224 else if (!strcmp(argv[1], "scache_upload")) {
225 struct SF_INFO sfinfo;
226 action = UPLOAD_SAMPLE;
227
228 if (argc < 3) {
229 fprintf(stderr, "Please specify a sample file to load\n");
230 goto quit;
231 }
232
233 if (argc >= 4)
234 sample_name = argv[3];
235 else {
236 char *f = strrchr(argv[2], '/');
237 if (f)
238 f++;
239 else
240 f = argv[2];
241
242 strncpy(sample_name = tmp, f, strcspn(f, "."));
243 }
244
245 memset(&sfinfo, 0, sizeof(sfinfo));
246 if (!(sndfile = sf_open(argv[2], SFM_READ, &sfinfo))) {
247 fprintf(stderr, "Failed to open sound file.\n");
248 goto quit;
249 }
250
251 sample_spec.format = PA_SAMPLE_FLOAT32;
252 sample_spec.rate = sfinfo.samplerate;
253 sample_spec.channels = sfinfo.channels;
254
255 sample_length = sfinfo.frames*pa_frame_size(&sample_spec);
256 } else if (!strcmp(argv[1], "scache_play")) {
257 action = PLAY_SAMPLE;
258 if (argc < 3) {
259 fprintf(stderr, "You have to specify a sample name to play\n");
260 goto quit;
261 }
262 } else if (!strcmp(argv[1], "scache_remove")) {
263 action = REMOVE_SAMPLE;
264 if (argc < 3) {
265 fprintf(stderr, "You have to specify a sample name to remove\n");
266 goto quit;
267 }
268 }
269 }
270
271 if (action == NONE) {
272 fprintf(stderr, "No valid action specified. Use one of: stat, exit, scache_upload, scache_play, scache_remove\n");
273 goto quit;
274 }
275
276 process_argv = argv;
277
278 if (!(m = pa_mainloop_new())) {
279 fprintf(stderr, "pa_mainloop_new() failed.\n");
280 goto quit;
281 }
282
283 mainloop_api = pa_mainloop_get_api(m);
284
285 r = pa_signal_init(mainloop_api);
286 assert(r == 0);
287 pa_signal_register(SIGINT, exit_signal_callback, NULL);
288 signal(SIGPIPE, SIG_IGN);
289
290 if (!(context = pa_context_new(mainloop_api, argv[0]))) {
291 fprintf(stderr, "pa_context_new() failed.\n");
292 goto quit;
293 }
294
295 if (pa_context_connect(context, NULL, context_complete_callback, NULL) < 0) {
296 fprintf(stderr, "pa_context_connext() failed.\n");
297 goto quit;
298 }
299
300 pa_context_set_die_callback(context, context_die_callback, NULL);
301
302 if (pa_mainloop_run(m, &ret) < 0) {
303 fprintf(stderr, "pa_mainloop_run() failed.\n");
304 goto quit;
305 }
306
307 quit:
308 if (context)
309 pa_context_free(context);
310
311 if (m) {
312 pa_signal_done();
313 pa_mainloop_free(m);
314 }
315
316 if (sndfile)
317 sf_close(sndfile);
318
319 return ret;
320 }