1aa73495040a31b81b8b564f29ab0bf21c229546
[profile/ivi/pulseaudio.git] / src / modules / module-jack-sink.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 <polyp/xmalloc.h>
40
41 #include <polypcore/iochannel.h>
42 #include <polypcore/sink.h>
43 #include <polypcore/module.h>
44 #include <polypcore/util.h>
45 #include <polypcore/modargs.h>
46 #include <polypcore/log.h>
47 #include <polyp/mainloop-api.h>
48
49 #include "module-jack-sink-symdef.h"
50
51 PA_MODULE_AUTHOR("Lennart Poettering")
52 PA_MODULE_DESCRIPTION("Jack Sink")
53 PA_MODULE_VERSION(PACKAGE_VERSION)
54 PA_MODULE_USAGE(
55         "sink_name=<name of sink> "
56         "server_name=<jack server name> "
57         "client_name=<jack client name> "
58         "channels=<number of channels> "
59         "connect=<connect ports?> "
60         "channel_map=<channel map>")
61
62 #define DEFAULT_SINK_NAME "jack_out"
63
64 struct userdata {
65     pa_core *core;
66     pa_module *module;
67
68     pa_sink *sink;
69
70     unsigned channels;
71
72     jack_port_t* port[PA_CHANNELS_MAX];
73     jack_client_t *client;
74
75     pthread_mutex_t mutex;
76     pthread_cond_t cond;
77     
78     void * buffer[PA_CHANNELS_MAX];
79     jack_nframes_t frames_requested;
80     int quit_requested;
81
82     int pipe_fds[2];
83     pa_io_event *io_event;
84
85     jack_nframes_t frames_in_buffer;
86     jack_nframes_t timestamp;
87 };
88
89 static const char* const valid_modargs[] = {
90     "sink_name",
91     "server_name",
92     "client_name",
93     "channels",
94     "connect",
95     "channel_map",
96     NULL
97 };
98
99 static void stop_sink(struct userdata *u) {
100     assert (u);
101     
102     jack_client_close(u->client);
103     u->client = NULL;
104     u->core->mainloop->io_free(u->io_event);
105     u->io_event = NULL;
106     pa_sink_disconnect(u->sink);
107     pa_sink_unref(u->sink);
108     u->sink = NULL;
109     pa_module_unload_request(u->module);
110 }
111
112 static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
113     struct userdata *u = userdata;
114     char x;
115     
116     assert(m);
117     assert(e);
118     assert(flags == PA_IO_EVENT_INPUT);
119     assert(u);
120     assert(u->pipe_fds[0] == fd);
121
122     read(fd, &x, 1);
123     
124     if (u->quit_requested) {
125         stop_sink(u);
126         u->quit_requested = 0;
127         return;
128     }
129     
130     pthread_mutex_lock(&u->mutex);
131
132     if (u->frames_requested > 0) {
133         unsigned fs;
134         jack_nframes_t frame_idx;
135         pa_memchunk chunk;
136         
137         fs = pa_frame_size(&u->sink->sample_spec);
138
139         pa_sink_render_full(u->sink, u->frames_requested * fs, &chunk);
140
141         for (frame_idx = 0; frame_idx < u->frames_requested; frame_idx ++) {
142             unsigned c;
143                 
144             for (c = 0; c < u->channels; c++) {
145                 float *s = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx * u->channels) + c;
146                 float *d = ((float*) u->buffer[c]) + frame_idx;
147                 
148                 *d = *s;
149             }
150         }
151         
152         pa_memblock_unref(chunk.memblock);
153
154         u->frames_requested = 0;
155         
156         pthread_cond_signal(&u->cond);
157     }
158
159     pthread_mutex_unlock(&u->mutex);
160 }
161
162 static void request_render(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_render(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_requested = 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_render(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 sink_get_latency_cb(pa_sink *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]) + u->frames_in_buffer;
224
225     if (d >= l)
226         return 0;
227     
228     return pa_bytes_to_usec((l - d) * pa_frame_size(&s->sample_spec), &s->sample_spec);
229 }
230
231 static void jack_error_func(const char*t) {
232     pa_log_warn(__FILE__": JACK error >%s<", t);
233 }
234
235 int pa__init(pa_core *c, pa_module*m) {
236     struct userdata *u = NULL;
237     pa_sample_spec ss;
238     pa_channel_map map;
239     pa_modargs *ma = NULL;
240     jack_status_t status;
241     const char *server_name, *client_name;
242     uint32_t channels = 0;
243     int connect = 1;
244     unsigned i;
245     const char **ports = NULL, **p;
246     
247     assert(c);
248     assert(m);
249
250     jack_set_error_function(jack_error_func);
251     
252     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
253         pa_log(__FILE__": failed to parse module arguments.");
254         goto fail;
255     }
256
257     if (pa_modargs_get_value_boolean(ma, "connect", &connect) < 0) {
258         pa_log(__FILE__": failed to parse connect= argument.");
259         goto fail;
260     }
261         
262     server_name = pa_modargs_get_value(ma, "server_name", NULL);
263     client_name = pa_modargs_get_value(ma, "client_name", "polypaudio");
264
265     u = pa_xnew0(struct userdata, 1);
266     m->userdata = u;
267     u->core = c;
268     u->module = m;
269     u->pipe_fds[0] = u->pipe_fds[1] = -1;
270
271     pthread_mutex_init(&u->mutex, NULL);
272     pthread_cond_init(&u->cond, NULL);
273     
274     if (pipe(u->pipe_fds) < 0) {
275         pa_log(__FILE__": pipe() failed: %s", strerror(errno));
276         goto fail;
277     }
278
279     pa_make_nonblock_fd(u->pipe_fds[1]);
280     
281     if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
282         pa_log(__FILE__": jack_client_open() failed.");
283         goto fail;
284     }
285
286     ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
287     
288     channels = 0;
289     for (p = ports; *p; p++)
290         channels++;
291
292     if (!channels)
293         channels = c->default_sample_spec.channels;
294     
295     if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) {
296         pa_log(__FILE__": failed to parse channels= argument.");
297         goto fail;
298     }
299
300     pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA);
301     if (pa_modargs_get_channel_map(ma, &map) < 0 || map.channels != channels) {
302         pa_log(__FILE__": failed to parse channel_map= argument.");
303         goto fail;
304     }
305     
306     pa_log_info(__FILE__": Successfully connected as '%s'", jack_get_client_name(u->client));
307
308     ss.channels = u->channels = channels;
309     ss.rate = jack_get_sample_rate(u->client);
310     ss.format = PA_SAMPLE_FLOAT32NE;
311
312     assert(pa_sample_spec_valid(&ss));
313
314     for (i = 0; i < ss.channels; i++) {
315         if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) {
316             pa_log(__FILE__": jack_port_register() failed.");
317             goto fail;
318         }
319     }
320
321     if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
322         pa_log(__FILE__": failed to create sink.");
323         goto fail;
324     }
325
326     u->sink->userdata = u;
327     pa_sink_set_owner(u->sink, m);
328     u->sink->description = pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u->client));
329     u->sink->get_latency = sink_get_latency_cb;
330
331     jack_set_process_callback(u->client, jack_process, u);
332     jack_on_shutdown(u->client, jack_shutdown, u);
333
334     if (jack_activate(u->client)) {
335         pa_log(__FILE__": jack_activate() failed");
336         goto fail;
337     }
338
339     if (connect) {
340         for (i = 0, p = ports; i < ss.channels; i++, p++) {
341
342             if (!*p) {
343                 pa_log(__FILE__": not enough physical output ports, leaving unconnected.");
344                 break;
345             }
346
347             pa_log_info(__FILE__": connecting %s to %s", jack_port_name(u->port[i]), *p);
348             
349             if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) {
350                 pa_log(__FILE__": failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
351                 break;
352             }
353         }
354
355     }
356
357     u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u);
358     
359     free(ports);
360     pa_modargs_free(ma);
361     
362     return 0;
363
364 fail:
365     if (ma)
366         pa_modargs_free(ma);
367
368     free(ports);
369         
370     pa__done(c, m);
371
372     return -1;
373 }
374
375 void pa__done(pa_core *c, pa_module*m) {
376     struct userdata *u;
377     assert(c && m);
378
379     if (!(u = m->userdata))
380         return;
381
382     if (u->client)
383         jack_client_close(u->client);
384
385     if (u->io_event)
386         c->mainloop->io_free(u->io_event);
387
388     if (u->sink) {
389         pa_sink_disconnect(u->sink);
390         pa_sink_unref(u->sink);
391     }
392
393     if (u->pipe_fds[0] >= 0)
394         close(u->pipe_fds[0]);
395     if (u->pipe_fds[1] >= 0)
396         close(u->pipe_fds[1]);
397
398     pthread_mutex_destroy(&u->mutex);
399     pthread_cond_destroy(&u->cond);
400     pa_xfree(u);
401 }