lirc: fix logic behind mute buttons
[profile/ivi/pulseaudio-panda.git] / src / modules / module-jack-source.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2006 Lennart Poettering
5
6   PulseAudio 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.1 of the License,
9   or (at your option) any later version.
10
11   PulseAudio 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 PulseAudio; 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 <errno.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <limits.h>
34
35 #include <jack/jack.h>
36
37 #include <pulse/xmalloc.h>
38
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/source.h>
41 #include <pulsecore/module.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/modargs.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/thread.h>
46 #include <pulsecore/thread-mq.h>
47 #include <pulsecore/rtpoll.h>
48 #include <pulsecore/sample-util.h>
49
50 #include "module-jack-source-symdef.h"
51
52 /* See module-jack-sink for a few comments how this module basically
53  * works */
54
55 PA_MODULE_AUTHOR("Lennart Poettering");
56 PA_MODULE_DESCRIPTION("JACK Source");
57 PA_MODULE_VERSION(PACKAGE_VERSION);
58 PA_MODULE_LOAD_ONCE(TRUE);
59 PA_MODULE_USAGE(
60         "source_name=<name of source> "
61         "server_name=<jack server name> "
62         "client_name=<jack client name> "
63         "channels=<number of channels> "
64         "connect=<connect ports?>"
65         "channel_map=<channel map>");
66
67 #define DEFAULT_SOURCE_NAME "jack_in"
68
69 struct userdata {
70     pa_core *core;
71     pa_module *module;
72     pa_source *source;
73
74     unsigned channels;
75
76     jack_port_t* port[PA_CHANNELS_MAX];
77     jack_client_t *client;
78
79     pa_thread_mq thread_mq;
80     pa_asyncmsgq *jack_msgq;
81     pa_rtpoll *rtpoll;
82     pa_rtpoll_item *rtpoll_item;
83
84     pa_thread *thread;
85
86     jack_nframes_t saved_frame_time;
87     pa_bool_t saved_frame_time_valid;
88 };
89
90 static const char* const valid_modargs[] = {
91     "source_name",
92     "server_name",
93     "client_name",
94     "channels",
95     "connect",
96     "channel_map",
97     NULL
98 };
99
100 enum {
101     SOURCE_MESSAGE_POST = PA_SOURCE_MESSAGE_MAX,
102     SOURCE_MESSAGE_ON_SHUTDOWN
103 };
104
105 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
106     struct userdata *u = PA_SOURCE(o)->userdata;
107
108     switch (code) {
109
110         case SOURCE_MESSAGE_POST:
111
112             /* Handle the new block from the JACK thread */
113             pa_assert(chunk);
114             pa_assert(chunk->length > 0);
115
116             if (u->source->thread_info.state == PA_SOURCE_RUNNING)
117                 pa_source_post(u->source, chunk);
118
119             u->saved_frame_time = (jack_nframes_t) offset;
120             u->saved_frame_time_valid = TRUE;
121
122             return 0;
123
124         case SOURCE_MESSAGE_ON_SHUTDOWN:
125             pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
126             return 0;
127
128         case PA_SOURCE_MESSAGE_GET_LATENCY: {
129             jack_nframes_t l, ft, d;
130             size_t n;
131
132             /* This is the "worst-case" latency */
133             l = jack_port_get_total_latency(u->client, u->port[0]);
134
135             if (u->saved_frame_time_valid) {
136                 /* Adjust the worst case latency by the time that
137                  * passed since we last handed data to JACK */
138
139                 ft = jack_frame_time(u->client);
140                 d = ft > u->saved_frame_time ? ft - u->saved_frame_time : 0;
141                 l += d;
142             }
143
144             /* Convert it to usec */
145             n = l * pa_frame_size(&u->source->sample_spec);
146             *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
147
148             return 0;
149         }
150     }
151
152     return pa_source_process_msg(o, code, data, offset, chunk);
153 }
154
155 static int jack_process(jack_nframes_t nframes, void *arg) {
156     unsigned c;
157     struct userdata *u = arg;
158     const void *buffer[PA_CHANNELS_MAX];
159     void *p;
160     jack_nframes_t frame_time;
161     pa_memchunk chunk;
162
163     pa_assert(u);
164
165     for (c = 0; c < u->channels; c++)
166         pa_assert_se(buffer[c] = jack_port_get_buffer(u->port[c], nframes));
167
168     /* We interleave the data and pass it on to the other RT thread */
169
170     pa_memchunk_reset(&chunk);
171     chunk.length = nframes * pa_frame_size(&u->source->sample_spec);
172     chunk.memblock = pa_memblock_new(u->core->mempool, chunk.length);
173     p = pa_memblock_acquire(chunk.memblock);
174     pa_interleave(buffer, u->channels, p, sizeof(float), nframes);
175     pa_memblock_release(chunk.memblock);
176
177     frame_time = jack_frame_time(u->client);
178
179     pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_POST, NULL, frame_time, &chunk, NULL);
180
181     pa_memblock_unref(chunk.memblock);
182
183     return 0;
184 }
185
186 static void thread_func(void *userdata) {
187     struct userdata *u = userdata;
188
189     pa_assert(u);
190
191     pa_log_debug("Thread starting up");
192
193     if (u->core->realtime_scheduling)
194         pa_make_realtime(u->core->realtime_priority);
195
196     pa_thread_mq_install(&u->thread_mq);
197     pa_rtpoll_install(u->rtpoll);
198
199     for (;;) {
200         int ret;
201
202         if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
203             goto fail;
204
205         if (ret == 0)
206             goto finish;
207     }
208
209 fail:
210     /* If this was no regular exit from the loop we have to continue
211      * processing messages until we received PA_MESSAGE_SHUTDOWN */
212     pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
213     pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
214
215 finish:
216     pa_log_debug("Thread shutting down");
217 }
218
219 static void jack_error_func(const char*t) {
220     char *s;
221
222     s = pa_xstrndup(t, strcspn(t, "\n\r"));
223     pa_log_warn("JACK error >%s<", s);
224     pa_xfree(s);
225 }
226
227 static void jack_init(void *arg) {
228     struct userdata *u = arg;
229
230     pa_log_info("JACK thread starting up.");
231
232     if (u->core->realtime_scheduling)
233         pa_make_realtime(u->core->realtime_priority+4);
234 }
235
236 static void jack_shutdown(void* arg) {
237     struct userdata *u = arg;
238
239     pa_log_info("JACK thread shutting down..");
240     pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_ON_SHUTDOWN, NULL, 0, NULL, NULL);
241 }
242
243 int pa__init(pa_module*m) {
244     struct userdata *u = NULL;
245     pa_sample_spec ss;
246     pa_channel_map map;
247     pa_modargs *ma = NULL;
248     jack_status_t status;
249     const char *server_name, *client_name;
250     uint32_t channels = 0;
251     pa_bool_t do_connect = TRUE;
252     unsigned i;
253     const char **ports = NULL, **p;
254     pa_source_new_data data;
255
256     pa_assert(m);
257
258     jack_set_error_function(jack_error_func);
259
260     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
261         pa_log("Failed to parse module arguments.");
262         goto fail;
263     }
264
265     if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) {
266         pa_log("Failed to parse connect= argument.");
267         goto fail;
268     }
269
270     server_name = pa_modargs_get_value(ma, "server_name", NULL);
271     client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio JACK Source");
272
273     m->userdata = u = pa_xnew0(struct userdata, 1);
274     u->core = m->core;
275     u->module = m;
276     u->saved_frame_time_valid = FALSE;
277     u->rtpoll = pa_rtpoll_new();
278     pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
279
280     u->jack_msgq = pa_asyncmsgq_new(0);
281     u->rtpoll_item = pa_rtpoll_item_new_asyncmsgq_read(u->rtpoll, PA_RTPOLL_EARLY-1, u->jack_msgq);
282
283     if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
284         pa_log("jack_client_open() failed.");
285         goto fail;
286     }
287
288     ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
289
290     channels = 0;
291     for (p = ports; *p; p++)
292         channels++;
293
294     if (!channels)
295         channels = m->core->default_sample_spec.channels;
296
297     if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 ||
298         channels <= 0 ||
299         channels >= PA_CHANNELS_MAX) {
300         pa_log("failed to parse channels= argument.");
301         goto fail;
302     }
303
304     if (channels == m->core->default_channel_map.channels)
305         map = m->core->default_channel_map;
306     else
307         pa_channel_map_init_extend(&map, channels, PA_CHANNEL_MAP_ALSA);
308
309     if (pa_modargs_get_channel_map(ma, NULL, &map) < 0 || map.channels != channels) {
310         pa_log("failed to parse channel_map= argument.");
311         goto fail;
312     }
313
314     pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client));
315
316     u->channels = ss.channels = (uint8_t) channels;
317     ss.rate = jack_get_sample_rate(u->client);
318     ss.format = PA_SAMPLE_FLOAT32NE;
319
320     pa_assert(pa_sample_spec_valid(&ss));
321
322     for (i = 0; i < ss.channels; i++) {
323         if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput|JackPortIsTerminal, 0))) {
324             pa_log("jack_port_register() failed.");
325             goto fail;
326         }
327     }
328
329     pa_source_new_data_init(&data);
330     data.driver = __FILE__;
331     data.module = m;
332     pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
333     pa_source_new_data_set_sample_spec(&data, &ss);
334     pa_source_new_data_set_channel_map(&data, &map);
335     pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "jack");
336     if (server_name)
337         pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, server_name);
338     pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Jack source (%s)", jack_get_client_name(u->client));
339     pa_proplist_sets(data.proplist, "jack.client_name", jack_get_client_name(u->client));
340
341     u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
342     pa_source_new_data_done(&data);
343
344     if (!u->source) {
345         pa_log("Failed to create source.");
346         goto fail;
347     }
348
349     u->source->parent.process_msg = source_process_msg;
350     u->source->userdata = u;
351
352     pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
353     pa_source_set_rtpoll(u->source, u->rtpoll);
354
355     jack_set_process_callback(u->client, jack_process, u);
356     jack_on_shutdown(u->client, jack_shutdown, u);
357     jack_set_thread_init_callback(u->client, jack_init, u);
358
359     if (!(u->thread = pa_thread_new(thread_func, u))) {
360         pa_log("Failed to create thread.");
361         goto fail;
362     }
363
364     if (jack_activate(u->client)) {
365         pa_log("jack_activate() failed");
366         goto fail;
367     }
368
369     if (do_connect) {
370         for (i = 0, p = ports; i < ss.channels; i++, p++) {
371
372             if (!*p) {
373                 pa_log("Not enough physical output ports, leaving unconnected.");
374                 break;
375             }
376
377             pa_log_info("Connecting %s to %s", jack_port_name(u->port[i]), *p);
378
379             if (jack_connect(u->client, *p, jack_port_name(u->port[i]))) {
380                 pa_log("Failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
381                 break;
382             }
383         }
384
385     }
386
387     pa_source_put(u->source);
388
389     free(ports);
390     pa_modargs_free(ma);
391
392     return 0;
393
394 fail:
395     if (ma)
396         pa_modargs_free(ma);
397
398     free(ports);
399
400     pa__done(m);
401
402     return -1;
403 }
404
405 int pa__get_n_used(pa_module *m) {
406     struct userdata *u;
407
408     pa_assert(m);
409     pa_assert_se(u = m->userdata);
410
411     return pa_source_linked_by(u->source);
412 }
413
414 void pa__done(pa_module*m) {
415     struct userdata *u;
416     pa_assert(m);
417
418     if (!(u = m->userdata))
419         return;
420
421     if (u->client)
422         jack_client_close(u->client);
423
424     if (u->source)
425         pa_source_unlink(u->source);
426
427     if (u->thread) {
428         pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
429         pa_thread_free(u->thread);
430     }
431
432     pa_thread_mq_done(&u->thread_mq);
433
434     if (u->source)
435         pa_source_unref(u->source);
436
437     if (u->rtpoll_item)
438         pa_rtpoll_item_free(u->rtpoll_item);
439
440     if (u->jack_msgq)
441         pa_asyncmsgq_unref(u->jack_msgq);
442
443     if (u->rtpoll)
444         pa_rtpoll_free(u->rtpoll);
445
446     pa_xfree(u);
447 }