4 This file is part of PulseAudio.
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 of the License,
9 or (at your option) any later version.
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.
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
37 #include <jack/jack.h>
39 #include <pulse/xmalloc.h>
41 #include <pulsecore/core-error.h>
42 #include <pulsecore/iochannel.h>
43 #include <pulsecore/sink.h>
44 #include <pulsecore/module.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/modargs.h>
47 #include <pulsecore/log.h>
48 #include <pulse/mainloop-api.h>
50 #include "module-jack-sink-symdef.h"
52 PA_MODULE_AUTHOR("Lennart Poettering")
53 PA_MODULE_DESCRIPTION("Jack Sink")
54 PA_MODULE_VERSION(PACKAGE_VERSION
)
56 "sink_name=<name of sink> "
57 "server_name=<jack server name> "
58 "client_name=<jack client name> "
59 "channels=<number of channels> "
60 "connect=<connect ports?> "
61 "channel_map=<channel map>")
63 #define DEFAULT_SINK_NAME "jack_out"
73 jack_port_t
* port
[PA_CHANNELS_MAX
];
74 jack_client_t
*client
;
76 pthread_mutex_t mutex
;
79 void * buffer
[PA_CHANNELS_MAX
];
80 jack_nframes_t frames_requested
;
85 pa_io_event
*io_event
;
87 jack_nframes_t frames_in_buffer
;
88 jack_nframes_t timestamp
;
91 static const char* const valid_modargs
[] = {
101 static void stop_sink(struct userdata
*u
) {
104 jack_client_close(u
->client
);
106 u
->core
->mainloop
->io_free(u
->io_event
);
108 pa_sink_disconnect(u
->sink
);
109 pa_sink_unref(u
->sink
);
111 pa_module_unload_request(u
->module
);
114 static void io_event_cb(pa_mainloop_api
*m
, pa_io_event
*e
, int fd
, pa_io_event_flags_t flags
, void *userdata
) {
115 struct userdata
*u
= userdata
;
120 assert(flags
== PA_IO_EVENT_INPUT
);
122 assert(u
->pipe_fds
[0] == fd
);
124 pa_read(fd
, &x
, 1, &u
->pipe_fd_type
);
126 if (u
->quit_requested
) {
128 u
->quit_requested
= 0;
132 pthread_mutex_lock(&u
->mutex
);
134 if (u
->frames_requested
> 0) {
136 jack_nframes_t frame_idx
;
139 fs
= pa_frame_size(&u
->sink
->sample_spec
);
141 pa_sink_render_full(u
->sink
, u
->frames_requested
* fs
, &chunk
);
143 for (frame_idx
= 0; frame_idx
< u
->frames_requested
; frame_idx
++) {
146 for (c
= 0; c
< u
->channels
; c
++) {
147 float *s
= ((float*) ((uint8_t*) chunk
.memblock
->data
+ chunk
.index
)) + (frame_idx
* u
->channels
) + c
;
148 float *d
= ((float*) u
->buffer
[c
]) + frame_idx
;
154 pa_memblock_unref(chunk
.memblock
);
156 u
->frames_requested
= 0;
158 pthread_cond_signal(&u
->cond
);
161 pthread_mutex_unlock(&u
->mutex
);
164 static void request_render(struct userdata
*u
) {
168 assert(u
->pipe_fds
[1] >= 0);
169 pa_write(u
->pipe_fds
[1], &c
, 1, &u
->pipe_fd_type
);
172 static void jack_shutdown(void *arg
) {
173 struct userdata
*u
= arg
;
176 u
->quit_requested
= 1;
180 static int jack_process(jack_nframes_t nframes
, void *arg
) {
181 struct userdata
*u
= arg
;
184 if (jack_transport_query(u
->client
, NULL
) == JackTransportRolling
) {
187 pthread_mutex_lock(&u
->mutex
);
189 u
->frames_requested
= nframes
;
191 for (c
= 0; c
< u
->channels
; c
++) {
192 u
->buffer
[c
] = jack_port_get_buffer(u
->port
[c
], nframes
);
193 assert(u
->buffer
[c
]);
198 pthread_cond_wait(&u
->cond
, &u
->mutex
);
200 u
->frames_in_buffer
= nframes
;
201 u
->timestamp
= jack_get_current_transport_frame(u
->client
);
203 pthread_mutex_unlock(&u
->mutex
);
209 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
211 jack_nframes_t n
, l
, d
;
216 if (jack_transport_query(u
->client
, NULL
) != JackTransportRolling
)
219 n
= jack_get_current_transport_frame(u
->client
);
221 if (n
< u
->timestamp
)
224 d
= n
- u
->timestamp
;
225 l
= jack_port_get_total_latency(u
->client
, u
->port
[0]) + u
->frames_in_buffer
;
230 return pa_bytes_to_usec((l
- d
) * pa_frame_size(&s
->sample_spec
), &s
->sample_spec
);
233 static void jack_error_func(const char*t
) {
234 pa_log_warn(__FILE__
": JACK error >%s<", t
);
237 int pa__init(pa_core
*c
, pa_module
*m
) {
238 struct userdata
*u
= NULL
;
241 pa_modargs
*ma
= NULL
;
242 jack_status_t status
;
243 const char *server_name
, *client_name
;
244 uint32_t channels
= 0;
247 const char **ports
= NULL
, **p
;
252 jack_set_error_function(jack_error_func
);
254 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
255 pa_log(__FILE__
": failed to parse module arguments.");
259 if (pa_modargs_get_value_boolean(ma
, "connect", &connect
) < 0) {
260 pa_log(__FILE__
": failed to parse connect= argument.");
264 server_name
= pa_modargs_get_value(ma
, "server_name", NULL
);
265 client_name
= pa_modargs_get_value(ma
, "client_name", "PulseAudio");
267 u
= pa_xnew0(struct userdata
, 1);
271 u
->pipe_fds
[0] = u
->pipe_fds
[1] = -1;
274 pthread_mutex_init(&u
->mutex
, NULL
);
275 pthread_cond_init(&u
->cond
, NULL
);
277 if (pipe(u
->pipe_fds
) < 0) {
278 pa_log(__FILE__
": pipe() failed: %s", pa_cstrerror(errno
));
282 pa_make_nonblock_fd(u
->pipe_fds
[1]);
284 if (!(u
->client
= jack_client_open(client_name
, server_name
? JackServerName
: JackNullOption
, &status
, server_name
))) {
285 pa_log(__FILE__
": jack_client_open() failed.");
289 ports
= jack_get_ports(u
->client
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsInput
);
292 for (p
= ports
; *p
; p
++)
296 channels
= c
->default_sample_spec
.channels
;
298 if (pa_modargs_get_value_u32(ma
, "channels", &channels
) < 0 || channels
<= 0 || channels
>= PA_CHANNELS_MAX
) {
299 pa_log(__FILE__
": failed to parse channels= argument.");
303 pa_channel_map_init_auto(&map
, channels
, PA_CHANNEL_MAP_ALSA
);
304 if (pa_modargs_get_channel_map(ma
, &map
) < 0 || map
.channels
!= channels
) {
305 pa_log(__FILE__
": failed to parse channel_map= argument.");
309 pa_log_info(__FILE__
": Successfully connected as '%s'", jack_get_client_name(u
->client
));
311 ss
.channels
= u
->channels
= channels
;
312 ss
.rate
= jack_get_sample_rate(u
->client
);
313 ss
.format
= PA_SAMPLE_FLOAT32NE
;
315 assert(pa_sample_spec_valid(&ss
));
317 for (i
= 0; i
< ss
.channels
; i
++) {
318 if (!(u
->port
[i
] = jack_port_register(u
->client
, pa_channel_position_to_string(map
.map
[i
]), JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
|JackPortIsTerminal
, 0))) {
319 pa_log(__FILE__
": jack_port_register() failed.");
324 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, &map
))) {
325 pa_log(__FILE__
": failed to create sink.");
329 u
->sink
->userdata
= u
;
330 pa_sink_set_owner(u
->sink
, m
);
331 u
->sink
->description
= pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u
->client
));
332 u
->sink
->get_latency
= sink_get_latency_cb
;
334 jack_set_process_callback(u
->client
, jack_process
, u
);
335 jack_on_shutdown(u
->client
, jack_shutdown
, u
);
337 if (jack_activate(u
->client
)) {
338 pa_log(__FILE__
": jack_activate() failed");
343 for (i
= 0, p
= ports
; i
< ss
.channels
; i
++, p
++) {
346 pa_log(__FILE__
": not enough physical output ports, leaving unconnected.");
350 pa_log_info(__FILE__
": connecting %s to %s", jack_port_name(u
->port
[i
]), *p
);
352 if (jack_connect(u
->client
, jack_port_name(u
->port
[i
]), *p
)) {
353 pa_log(__FILE__
": failed to connect %s to %s, leaving unconnected.", jack_port_name(u
->port
[i
]), *p
);
360 u
->io_event
= c
->mainloop
->io_new(c
->mainloop
, u
->pipe_fds
[0], PA_IO_EVENT_INPUT
, io_event_cb
, u
);
378 void pa__done(pa_core
*c
, pa_module
*m
) {
382 if (!(u
= m
->userdata
))
386 jack_client_close(u
->client
);
389 c
->mainloop
->io_free(u
->io_event
);
392 pa_sink_disconnect(u
->sink
);
393 pa_sink_unref(u
->sink
);
396 if (u
->pipe_fds
[0] >= 0)
397 close(u
->pipe_fds
[0]);
398 if (u
->pipe_fds
[1] >= 0)
399 close(u
->pipe_fds
[1]);
401 pthread_mutex_destroy(&u
->mutex
);
402 pthread_cond_destroy(&u
->cond
);