support new channel_map argument in sink/source modules
[profile/ivi/pulseaudio.git] / src / modules / module-jack-source.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 Lesser 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 Lesser 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 <stdlib.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <limits.h>
35 #include <pthread.h>
36
37 #include <jack/jack.h>
38
39 #include <polypcore/iochannel.h>
40 #include <polypcore/source.h>
41 #include <polypcore/module.h>
42 #include <polypcore/util.h>
43 #include <polypcore/modargs.h>
44 #include <polypcore/xmalloc.h>
45 #include <polypcore/log.h>
46 #include <polyp/mainloop-api.h>
47
48 #include "module-jack-source-symdef.h"
49
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("Jack Source")
52 PA_MODULE_VERSION(PACKAGE_VERSION)
53 PA_MODULE_USAGE(
54         "source_name=<name of source> "
55         "server_name=<jack server name> "
56         "client_name=<jack client name> "
57         "channels=<number of channels> "
58         "connect=<connect ports?>"
59         "channel_map=<channel map>")
60
61 #define DEFAULT_SOURCE_NAME "jack_in"
62
63 struct userdata {
64     pa_core *core;
65     pa_module *module;
66
67     pa_source *source;
68
69     unsigned channels;
70
71     jack_port_t* port[PA_CHANNELS_MAX];
72     jack_client_t *client;
73
74     pthread_mutex_t mutex;
75     pthread_cond_t cond;
76     
77     void * buffer[PA_CHANNELS_MAX];
78     jack_nframes_t frames_posted;
79     int quit_requested;
80
81     int pipe_fds[2];
82     pa_io_event *io_event;
83
84     jack_nframes_t frames_in_buffer;
85     jack_nframes_t timestamp;
86 };
87
88 static const char* const valid_modargs[] = {
89     "source_name",
90     "server_name",
91     "client_name",
92     "channels",
93     "connect",
94     "channel_map",
95     NULL
96 };
97
98 static void stop_source(struct userdata *u) {
99     assert (u);
100     
101     jack_client_close(u->client);
102     u->client = NULL;
103     u->core->mainloop->io_free(u->io_event);
104     u->io_event = NULL;
105     pa_source_disconnect(u->source);
106     pa_source_unref(u->source);
107     u->source = NULL;
108     pa_module_unload_request(u->module);
109 }
110
111 static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
112     struct userdata *u = userdata;
113     char x;
114     
115     assert(m);
116     assert(flags == PA_IO_EVENT_INPUT);
117     assert(u);
118     assert(u->pipe_fds[0] == fd);
119
120     read(fd, &x, 1);
121     
122     if (u->quit_requested) {
123         stop_source(u);
124         u->quit_requested = 0;
125         return;
126     }
127     
128     pthread_mutex_lock(&u->mutex);
129
130     if (u->frames_posted > 0) {
131         unsigned fs;
132         jack_nframes_t frame_idx;
133         pa_memchunk chunk;
134         
135         fs = pa_frame_size(&u->source->sample_spec);
136
137         chunk.memblock = pa_memblock_new(chunk.length = u->frames_posted * fs, u->core->memblock_stat);
138         chunk.index = 0;
139         
140         for (frame_idx = 0; frame_idx < u->frames_posted; frame_idx ++) {
141             unsigned c;
142                 
143             for (c = 0; c < u->channels; c++) {
144                 float *s = ((float*) u->buffer[c]) + frame_idx;
145                 float *d = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx * u->channels) + c;
146                 
147                 *d = *s;
148             }
149         }
150
151         pa_source_post(u->source, &chunk);
152         pa_memblock_unref(chunk.memblock);
153
154         u->frames_posted = 0;
155         
156         pthread_cond_signal(&u->cond);
157     }
158
159     pthread_mutex_unlock(&u->mutex);
160 }
161
162 static void request_post(struct userdata *u) {
163     char c = 'x';
164     assert(u);
165
166     assert(u->pipe_fds[1] >= 0);
167     write(u->pipe_fds[1], &c, 1);
168 }
169
170 static void jack_shutdown(void *arg) {
171     struct userdata *u = arg;
172     assert(u);
173
174     u->quit_requested = 1;
175     request_post(u);
176 }
177
178 static int jack_process(jack_nframes_t nframes, void *arg) {
179     struct userdata *u = arg;
180     assert(u);
181
182     if (jack_transport_query(u->client, NULL) == JackTransportRolling) {
183         unsigned c;
184         
185         pthread_mutex_lock(&u->mutex);
186         
187         u->frames_posted = nframes;
188         
189         for (c = 0; c < u->channels; c++) {
190             u->buffer[c] = jack_port_get_buffer(u->port[c], nframes);
191             assert(u->buffer[c]);
192         }
193         
194         request_post(u);
195         
196         pthread_cond_wait(&u->cond, &u->mutex);
197
198         u->frames_in_buffer = nframes;
199         u->timestamp = jack_get_current_transport_frame(u->client);
200         
201         pthread_mutex_unlock(&u->mutex);
202     }
203     
204     return 0;
205 }
206
207 static pa_usec_t source_get_latency_cb(pa_source *s) {
208     struct userdata *u;
209     jack_nframes_t n, l, d;
210     
211     assert(s);
212     u = s->userdata;
213     
214     if (jack_transport_query(u->client, NULL) != JackTransportRolling)
215         return 0;
216
217     n = jack_get_current_transport_frame(u->client);
218
219     if (n < u->timestamp)
220         return 0;
221
222     d = n - u->timestamp;
223     l = jack_port_get_total_latency(u->client, u->port[0]);
224     
225     return pa_bytes_to_usec((l + d) * pa_frame_size(&s->sample_spec), &s->sample_spec);
226 }
227
228 static void jack_error_func(const char*t) {
229     pa_log_warn(__FILE__": JACK error >%s<", t);
230 }
231
232 int pa__init(pa_core *c, pa_module*m) {
233     struct userdata *u = NULL;
234     pa_sample_spec ss;
235     pa_channel_map map;
236     pa_modargs *ma = NULL;
237     jack_status_t status;
238     const char *server_name, *client_name;
239     uint32_t channels = 0;
240     int connect = 1;
241     unsigned i;
242     const char **ports = NULL, **p;
243     
244     assert(c);
245     assert(m);
246
247     jack_set_error_function(jack_error_func);
248     
249     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
250         pa_log(__FILE__": failed to parse module arguments.");
251         goto fail;
252     }
253
254     if (pa_modargs_get_value_boolean(ma, "connect", &connect) < 0) {
255         pa_log(__FILE__": failed to parse connect= argument.");
256         goto fail;
257     }
258         
259     server_name = pa_modargs_get_value(ma, "server_name", NULL);
260     client_name = pa_modargs_get_value(ma, "client_name", "polypaudio");
261
262     u = pa_xnew0(struct userdata, 1);
263     m->userdata = u;
264     u->core = c;
265     u->module = m;
266     u->pipe_fds[0] = u->pipe_fds[1] = -1;
267
268     pthread_mutex_init(&u->mutex, NULL);
269     pthread_cond_init(&u->cond, NULL);
270     
271     if (pipe(u->pipe_fds) < 0) {
272         pa_log(__FILE__": pipe() failed: %s", strerror(errno));
273         goto fail;
274     }
275
276     pa_make_nonblock_fd(u->pipe_fds[1]);
277     
278     if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
279         pa_log(__FILE__": jack_client_open() failed.");
280         goto fail;
281     }
282
283     ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
284     
285     channels = 0;
286     for (p = ports; *p; p++)
287         channels++;
288
289     if (!channels)
290         channels = c->default_sample_spec.channels;
291     
292     if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) {
293         pa_log(__FILE__": failed to parse channels= argument.");
294         goto fail;
295     }
296
297     pa_channel_map_init_auto(&map, channels);
298     if (pa_modargs_get_channel_map(ma, &map) < 0 || map.channels != channels) {
299         pa_log(__FILE__": failed to parse channel_map= argument.");
300         goto fail;
301     }
302     
303     pa_log_info(__FILE__": Successfully connected as '%s'", jack_get_client_name(u->client));
304
305     ss.channels = u->channels = channels;
306     ss.rate = jack_get_sample_rate(u->client);
307     ss.format = PA_SAMPLE_FLOAT32NE;
308
309     assert(pa_sample_spec_valid(&ss));
310
311     for (i = 0; i < ss.channels; i++) {
312         if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput|JackPortIsTerminal, 0))) {
313             pa_log(__FILE__": jack_port_register() failed.");
314             goto fail;
315         }
316     }
317
318     if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
319         pa_log(__FILE__": failed to create source.");
320         goto fail;
321     }
322
323     u->source->userdata = u;
324     pa_source_set_owner(u->source, m);
325     u->source->description = pa_sprintf_malloc("Jack source (%s)", jack_get_client_name(u->client));
326     u->source->get_latency = source_get_latency_cb;
327
328     jack_set_process_callback(u->client, jack_process, u);
329     jack_on_shutdown(u->client, jack_shutdown, u);
330
331     if (jack_activate(u->client)) {
332         pa_log(__FILE__": jack_activate() failed");
333         goto fail;
334     }
335
336     if (connect) {
337         for (i = 0, p = ports; i < ss.channels; i++, p++) {
338
339             if (!*p) {
340                 pa_log(__FILE__": not enough physical output ports, leaving unconnected.");
341                 break;
342             }
343
344             pa_log_info(__FILE__": connecting %s to %s", jack_port_name(u->port[i]), *p);
345             
346             if (jack_connect(u->client, *p, jack_port_name(u->port[i]))) {
347                 pa_log(__FILE__": failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
348                 break;
349             }
350         }
351
352     }
353
354     u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u);
355     
356     free(ports);
357     pa_modargs_free(ma);
358     
359     return 0;
360
361 fail:
362     if (ma)
363         pa_modargs_free(ma);
364
365     free(ports);
366         
367     pa__done(c, m);
368
369     return -1;
370 }
371
372 void pa__done(pa_core *c, pa_module*m) {
373     struct userdata *u;
374     assert(c && m);
375
376     if (!(u = m->userdata))
377         return;
378
379     if (u->client)
380         jack_client_close(u->client);
381
382     if (u->io_event)
383         c->mainloop->io_free(u->io_event);
384
385     if (u->source) {
386         pa_source_disconnect(u->source);
387         pa_source_unref(u->source);
388     }
389
390     if (u->pipe_fds[0] >= 0)
391         close(u->pipe_fds[0]);
392     if (u->pipe_fds[1] >= 0)
393         close(u->pipe_fds[1]);
394
395     pthread_mutex_destroy(&u->mutex);
396     pthread_cond_destroy(&u->cond);
397     pa_xfree(u);
398 }