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