4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
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.
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.
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
39 #include <jack/jack.h>
41 #include <pulse/xmalloc.h>
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>
52 #include "module-jack-source-symdef.h"
54 PA_MODULE_AUTHOR("Lennart Poettering")
55 PA_MODULE_DESCRIPTION("Jack Source")
56 PA_MODULE_VERSION(PACKAGE_VERSION)
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>")
65 #define DEFAULT_SOURCE_NAME "jack_in"
75 jack_port_t* port[PA_CHANNELS_MAX];
76 jack_client_t *client;
78 pthread_mutex_t mutex;
81 void * buffer[PA_CHANNELS_MAX];
82 jack_nframes_t frames_posted;
87 pa_io_event *io_event;
89 jack_nframes_t frames_in_buffer;
90 jack_nframes_t timestamp;
93 static const char* const valid_modargs[] = {
103 static void stop_source(struct userdata *u) {
106 jack_client_close(u->client);
108 u->core->mainloop->io_free(u->io_event);
110 pa_source_disconnect(u->source);
111 pa_source_unref(u->source);
113 pa_module_unload_request(u->module);
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;
121 assert(flags == PA_IO_EVENT_INPUT);
123 assert(u->pipe_fds[0] == fd);
125 pa_read(fd, &x, 1, &u->pipe_fd_type);
127 if (u->quit_requested) {
129 u->quit_requested = 0;
133 pthread_mutex_lock(&u->mutex);
135 if (u->frames_posted > 0) {
137 jack_nframes_t frame_idx;
141 fs = pa_frame_size(&u->source->sample_spec);
143 chunk.memblock = pa_memblock_new(u->core->mempool, chunk.length = u->frames_posted * fs);
146 p = pa_memblock_acquire(chunk.memblock);
148 for (frame_idx = 0; frame_idx < u->frames_posted; frame_idx ++) {
151 for (c = 0; c < u->channels; c++) {
152 float *s = ((float*) u->buffer[c]) + frame_idx;
153 float *d = ((float*) ((uint8_t*) p + chunk.index)) + (frame_idx * u->channels) + c;
159 pa_memblock_release(chunk.memblock);
161 pa_source_post(u->source, &chunk);
162 pa_memblock_unref(chunk.memblock);
164 u->frames_posted = 0;
166 pthread_cond_signal(&u->cond);
169 pthread_mutex_unlock(&u->mutex);
172 static void request_post(struct userdata *u) {
176 assert(u->pipe_fds[1] >= 0);
177 pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type);
180 static void jack_shutdown(void *arg) {
181 struct userdata *u = arg;
184 u->quit_requested = 1;
188 static int jack_process(jack_nframes_t nframes, void *arg) {
189 struct userdata *u = arg;
192 if (jack_transport_query(u->client, NULL) == JackTransportRolling) {
195 pthread_mutex_lock(&u->mutex);
197 u->frames_posted = nframes;
199 for (c = 0; c < u->channels; c++) {
200 u->buffer[c] = jack_port_get_buffer(u->port[c], nframes);
201 assert(u->buffer[c]);
206 pthread_cond_wait(&u->cond, &u->mutex);
208 u->frames_in_buffer = nframes;
209 u->timestamp = jack_get_current_transport_frame(u->client);
211 pthread_mutex_unlock(&u->mutex);
217 static pa_usec_t source_get_latency_cb(pa_source *s) {
219 jack_nframes_t n, l, d;
224 if (jack_transport_query(u->client, NULL) != JackTransportRolling)
227 n = jack_get_current_transport_frame(u->client);
229 if (n < u->timestamp)
232 d = n - u->timestamp;
233 l = jack_port_get_total_latency(u->client, u->port[0]);
235 return pa_bytes_to_usec((l + d) * pa_frame_size(&s->sample_spec), &s->sample_spec);
238 static void jack_error_func(const char*t) {
239 pa_log_warn("JACK error >%s<", t);
242 int pa__init(pa_core *c, pa_module*m) {
243 struct userdata *u = NULL;
246 pa_modargs *ma = NULL;
247 jack_status_t status;
248 const char *server_name, *client_name;
249 uint32_t channels = 0;
252 const char **ports = NULL, **p;
258 jack_set_error_function(jack_error_func);
260 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
261 pa_log("failed to parse module arguments.");
265 if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) {
266 pa_log("failed to parse connect= argument.");
270 server_name = pa_modargs_get_value(ma, "server_name", NULL);
271 client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio");
273 u = pa_xnew0(struct userdata, 1);
277 u->pipe_fds[0] = u->pipe_fds[1] = -1;
280 pthread_mutex_init(&u->mutex, NULL);
281 pthread_cond_init(&u->cond, NULL);
283 if (pipe(u->pipe_fds) < 0) {
284 pa_log("pipe() failed: %s", pa_cstrerror(errno));
288 pa_make_nonblock_fd(u->pipe_fds[1]);
290 if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
291 pa_log("jack_client_open() failed.");
295 ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
298 for (p = ports; *p; p++)
302 channels = c->default_sample_spec.channels;
304 if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) {
305 pa_log("failed to parse channels= argument.");
309 pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA);
310 if (pa_modargs_get_channel_map(ma, &map) < 0 || map.channels != channels) {
311 pa_log("failed to parse channel_map= argument.");
315 pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client));
317 ss.channels = u->channels = channels;
318 ss.rate = jack_get_sample_rate(u->client);
319 ss.format = PA_SAMPLE_FLOAT32NE;
321 assert(pa_sample_spec_valid(&ss));
323 for (i = 0; i < ss.channels; i++) {
324 if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput|JackPortIsTerminal, 0))) {
325 pa_log("jack_port_register() failed.");
330 if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
331 pa_log("failed to create source.");
335 u->source->userdata = u;
336 pa_source_set_owner(u->source, m);
337 pa_source_set_description(u->source, t = pa_sprintf_malloc("Jack source (%s)", jack_get_client_name(u->client)));
339 u->source->get_latency = source_get_latency_cb;
341 jack_set_process_callback(u->client, jack_process, u);
342 jack_on_shutdown(u->client, jack_shutdown, u);
344 if (jack_activate(u->client)) {
345 pa_log("jack_activate() failed");
350 for (i = 0, p = ports; i < ss.channels; i++, p++) {
353 pa_log("not enough physical output ports, leaving unconnected.");
357 pa_log_info("connecting %s to %s", jack_port_name(u->port[i]), *p);
359 if (jack_connect(u->client, *p, jack_port_name(u->port[i]))) {
360 pa_log("failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
367 u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u);
385 void pa__done(pa_core *c, pa_module*m) {
389 if (!(u = m->userdata))
393 jack_client_close(u->client);
396 c->mainloop->io_free(u->io_event);
399 pa_source_disconnect(u->source);
400 pa_source_unref(u->source);
403 if (u->pipe_fds[0] >= 0)
404 close(u->pipe_fds[0]);
405 if (u->pipe_fds[1] >= 0)
406 close(u->pipe_fds[1]);
408 pthread_mutex_destroy(&u->mutex);
409 pthread_cond_destroy(&u->cond);