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