correct autospawning
[profile/ivi/pulseaudio-panda.git] / 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
72 static void context_drain_complete(struct pa_context *c, void *userdata) {
73     pa_context_disconnect(c);
74 }
75
76 static void drain(void) {
77     struct pa_operation *o;
78     if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
79         pa_context_disconnect(context);
80     else
81         pa_operation_unref(o);
82 }
83
84 static void stat_callback(struct pa_context *c, const struct pa_stat_info *i, void *userdata) {
85     if (!i) {
86         fprintf(stderr, "Failed to get statistics: %s\n", pa_strerror(pa_context_errno(c)));
87         quit(1);
88         return;
89     }
90     
91     fprintf(stderr, "Currently in use: %u blocks containing %u bytes total.\n"
92             "Allocated during whole lifetime: %u blocks containing %u bytes total.\n",
93             i->memblock_total, i->memblock_total_size, i->memblock_allocated, i->memblock_allocated_size);
94     drain();
95 }
96
97 static void play_sample_callback(struct pa_context *c, int success, void *userdata) {
98     if (!success) {
99         fprintf(stderr, "Failed to play sample: %s\n", pa_strerror(pa_context_errno(c)));
100         quit(1);
101         return;
102     }
103
104     drain();
105 }
106
107 static void remove_sample_callback(struct pa_context *c, int success, void *userdata) {
108     if (!success) {
109         fprintf(stderr, "Failed to remove sample: %s\n", pa_strerror(pa_context_errno(c)));
110         quit(1);
111         return;
112     }
113
114     drain();
115 }
116
117 static void stream_state_callback(struct pa_stream *s, void *userdata) {
118     assert(s);
119
120     switch (pa_stream_get_state(s)) {
121         case PA_STREAM_CREATING:
122         case PA_STREAM_READY:
123             break;
124             
125         case PA_STREAM_TERMINATED:
126             drain();
127             break;
128             
129         case PA_STREAM_FAILED:
130         default:
131             fprintf(stderr, "Failed to upload sample: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
132             quit(1);
133     }
134 }
135
136 static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
137     sf_count_t l;
138     float *d;
139     assert(s && length && sndfile);
140
141     d = malloc(length);
142     assert(d);
143
144     assert(sample_length >= length);
145     l = length/pa_frame_size(&sample_spec);
146
147     if ((sf_readf_float(sndfile, d, l)) != l) {
148         free(d);
149         fprintf(stderr, "Premature end of file\n");
150         quit(1);
151     }
152     
153     pa_stream_write(s, d, length, free, 0);
154
155     sample_length -= length;
156
157     if (sample_length  <= 0) {
158         pa_stream_set_write_callback(sample_stream, NULL, NULL);
159         pa_stream_finish_upload(sample_stream);
160     }
161 }
162
163 static void context_state_callback(struct pa_context *c, void *userdata) {
164     assert(c);
165     switch (pa_context_get_state(c)) {
166         case PA_CONTEXT_CONNECTING:
167         case PA_CONTEXT_AUTHORIZING:
168         case PA_CONTEXT_SETTING_NAME:
169             break;
170
171         case PA_CONTEXT_READY:
172             if (action == STAT)
173                 pa_operation_unref(pa_context_stat(c, stat_callback, NULL));
174             else if (action == PLAY_SAMPLE)
175                 pa_operation_unref(pa_context_play_sample(c, process_argv[2], NULL, 0x100, play_sample_callback, NULL));
176             else if (action == REMOVE_SAMPLE)
177                 pa_operation_unref(pa_context_remove_sample(c, process_argv[2], remove_sample_callback, NULL));
178             else if (action == UPLOAD_SAMPLE) {
179
180                 sample_stream = pa_stream_new(c, sample_name, &sample_spec);
181                 assert(sample_stream);
182
183                 pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
184                 pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
185                 pa_stream_connect_upload(sample_stream, sample_length);
186             } else {
187                 assert(action == EXIT);
188                 pa_context_exit_daemon(c);
189                 drain();
190             }
191             break;
192
193         case PA_CONTEXT_TERMINATED:
194             quit(0);
195             break;
196
197         case PA_CONTEXT_FAILED:
198         default:
199             fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
200             quit(1);
201     }
202 }
203
204 static void exit_signal_callback(struct pa_mainloop_api *m, struct pa_signal_event *e, int sig, void *userdata) {
205     fprintf(stderr, "Got SIGINT, exiting.\n");
206     quit(0);
207 }
208
209 int main(int argc, char *argv[]) {
210     struct pa_mainloop* m = NULL;
211     char tmp[PATH_MAX];
212     
213     int ret = 1, r;
214
215     if (argc >= 2) {
216         if (!strcmp(argv[1], "stat"))
217             action = STAT;
218         else if (!strcmp(argv[1], "exit"))
219             action = EXIT;
220         else if (!strcmp(argv[1], "scache_upload")) {
221             struct SF_INFO sfinfo;
222             action = UPLOAD_SAMPLE;
223
224             if (argc < 3) {
225                 fprintf(stderr, "Please specify a sample file to load\n");
226                 goto quit;
227             }
228
229             if (argc >= 4)
230                 sample_name = argv[3];
231             else {
232                 char *f = strrchr(argv[2], '/');
233                 size_t n;
234                 if (f)
235                     f++;
236                 else
237                     f = argv[2];
238
239                 n = strcspn(f, ".");
240                 strncpy(sample_name = tmp, f, n);
241                 tmp[n] = 0; 
242             }
243             
244             memset(&sfinfo, 0, sizeof(sfinfo));
245             if (!(sndfile = sf_open(argv[2], SFM_READ, &sfinfo))) {
246                 fprintf(stderr, "Failed to open sound file.\n");
247                 goto quit;
248             }
249             
250             sample_spec.format =  PA_SAMPLE_FLOAT32;
251             sample_spec.rate = sfinfo.samplerate;
252             sample_spec.channels = sfinfo.channels;
253
254             sample_length = sfinfo.frames*pa_frame_size(&sample_spec);
255         } else if (!strcmp(argv[1], "scache_play")) {
256             action = PLAY_SAMPLE;
257             if (argc < 3) {
258                 fprintf(stderr, "You have to specify a sample name to play\n");
259                 goto quit;
260             }
261         } else if (!strcmp(argv[1], "scache_remove")) {
262             action = REMOVE_SAMPLE;
263             if (argc < 3) {
264                 fprintf(stderr, "You have to specify a sample name to remove\n");
265                 goto quit;
266             }
267         }
268     }
269
270     if (action == NONE) {
271         fprintf(stderr, "No valid action specified. Use one of: stat, exit, scache_upload, scache_play, scache_remove\n");
272         goto quit;
273     }
274
275     process_argv = argv;
276     
277     if (!(m = pa_mainloop_new())) {
278         fprintf(stderr, "pa_mainloop_new() failed.\n");
279         goto quit;
280     }
281
282     mainloop_api = pa_mainloop_get_api(m);
283
284     r = pa_signal_init(mainloop_api);
285     assert(r == 0);
286     pa_signal_new(SIGINT, exit_signal_callback, NULL);
287     signal(SIGPIPE, SIG_IGN);
288     
289     if (!(context = pa_context_new(mainloop_api, argv[0]))) {
290         fprintf(stderr, "pa_context_new() failed.\n");
291         goto quit;
292     }
293
294     pa_context_set_state_callback(context, context_state_callback, NULL);
295     pa_context_connect(context, NULL, 1, NULL);
296
297     if (pa_mainloop_run(m, &ret) < 0) {
298         fprintf(stderr, "pa_mainloop_run() failed.\n");
299         goto quit;
300     }
301
302 quit:
303     if (sample_stream)
304         pa_stream_unref(sample_stream);
305
306     if (context)
307         pa_context_unref(context);
308
309     if (m) {
310         pa_signal_done();
311         pa_mainloop_free(m);
312     }
313     
314     if (sndfile)
315         sf_close(sndfile);
316
317     return ret;
318 }