4 This file is part of polypaudio.
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.
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.
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
37 #include <jack/jack.h>
39 #include <polyp/xmalloc.h>
41 #include <polypcore/iochannel.h>
42 #include <polypcore/source.h>
43 #include <polypcore/module.h>
44 #include <polypcore/core-util.h>
45 #include <polypcore/modargs.h>
46 #include <polypcore/log.h>
47 #include <polyp/mainloop-api.h>
49 #include "module-jack-source-symdef.h"
51 PA_MODULE_AUTHOR("Lennart Poettering")
52 PA_MODULE_DESCRIPTION("Jack Source")
53 PA_MODULE_VERSION(PACKAGE_VERSION
)
55 "source_name=<name of source> "
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>")
62 #define DEFAULT_SOURCE_NAME "jack_in"
72 jack_port_t
* port
[PA_CHANNELS_MAX
];
73 jack_client_t
*client
;
75 pthread_mutex_t mutex
;
78 void * buffer
[PA_CHANNELS_MAX
];
79 jack_nframes_t frames_posted
;
83 pa_io_event
*io_event
;
85 jack_nframes_t frames_in_buffer
;
86 jack_nframes_t timestamp
;
89 static const char* const valid_modargs
[] = {
99 static void stop_source(struct userdata
*u
) {
102 jack_client_close(u
->client
);
104 u
->core
->mainloop
->io_free(u
->io_event
);
106 pa_source_disconnect(u
->source
);
107 pa_source_unref(u
->source
);
109 pa_module_unload_request(u
->module
);
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
;
117 assert(flags
== PA_IO_EVENT_INPUT
);
119 assert(u
->pipe_fds
[0] == fd
);
123 if (u
->quit_requested
) {
125 u
->quit_requested
= 0;
129 pthread_mutex_lock(&u
->mutex
);
131 if (u
->frames_posted
> 0) {
133 jack_nframes_t frame_idx
;
136 fs
= pa_frame_size(&u
->source
->sample_spec
);
138 chunk
.memblock
= pa_memblock_new(chunk
.length
= u
->frames_posted
* fs
, u
->core
->memblock_stat
);
141 for (frame_idx
= 0; frame_idx
< u
->frames_posted
; frame_idx
++) {
144 for (c
= 0; c
< u
->channels
; c
++) {
145 float *s
= ((float*) u
->buffer
[c
]) + frame_idx
;
146 float *d
= ((float*) ((uint8_t*) chunk
.memblock
->data
+ chunk
.index
)) + (frame_idx
* u
->channels
) + c
;
152 pa_source_post(u
->source
, &chunk
);
153 pa_memblock_unref(chunk
.memblock
);
155 u
->frames_posted
= 0;
157 pthread_cond_signal(&u
->cond
);
160 pthread_mutex_unlock(&u
->mutex
);
163 static void request_post(struct userdata
*u
) {
167 assert(u
->pipe_fds
[1] >= 0);
168 write(u
->pipe_fds
[1], &c
, 1);
171 static void jack_shutdown(void *arg
) {
172 struct userdata
*u
= arg
;
175 u
->quit_requested
= 1;
179 static int jack_process(jack_nframes_t nframes
, void *arg
) {
180 struct userdata
*u
= arg
;
183 if (jack_transport_query(u
->client
, NULL
) == JackTransportRolling
) {
186 pthread_mutex_lock(&u
->mutex
);
188 u
->frames_posted
= nframes
;
190 for (c
= 0; c
< u
->channels
; c
++) {
191 u
->buffer
[c
] = jack_port_get_buffer(u
->port
[c
], nframes
);
192 assert(u
->buffer
[c
]);
197 pthread_cond_wait(&u
->cond
, &u
->mutex
);
199 u
->frames_in_buffer
= nframes
;
200 u
->timestamp
= jack_get_current_transport_frame(u
->client
);
202 pthread_mutex_unlock(&u
->mutex
);
208 static pa_usec_t
source_get_latency_cb(pa_source
*s
) {
210 jack_nframes_t n
, l
, d
;
215 if (jack_transport_query(u
->client
, NULL
) != JackTransportRolling
)
218 n
= jack_get_current_transport_frame(u
->client
);
220 if (n
< u
->timestamp
)
223 d
= n
- u
->timestamp
;
224 l
= jack_port_get_total_latency(u
->client
, u
->port
[0]);
226 return pa_bytes_to_usec((l
+ d
) * pa_frame_size(&s
->sample_spec
), &s
->sample_spec
);
229 static void jack_error_func(const char*t
) {
230 pa_log_warn(__FILE__
": JACK error >%s<", t
);
233 int pa__init(pa_core
*c
, pa_module
*m
) {
234 struct userdata
*u
= NULL
;
237 pa_modargs
*ma
= NULL
;
238 jack_status_t status
;
239 const char *server_name
, *client_name
;
240 uint32_t channels
= 0;
243 const char **ports
= NULL
, **p
;
248 jack_set_error_function(jack_error_func
);
250 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
251 pa_log(__FILE__
": failed to parse module arguments.");
255 if (pa_modargs_get_value_boolean(ma
, "connect", &connect
) < 0) {
256 pa_log(__FILE__
": failed to parse connect= argument.");
260 server_name
= pa_modargs_get_value(ma
, "server_name", NULL
);
261 client_name
= pa_modargs_get_value(ma
, "client_name", "polypaudio");
263 u
= pa_xnew0(struct userdata
, 1);
267 u
->pipe_fds
[0] = u
->pipe_fds
[1] = -1;
269 pthread_mutex_init(&u
->mutex
, NULL
);
270 pthread_cond_init(&u
->cond
, NULL
);
272 if (pipe(u
->pipe_fds
) < 0) {
273 pa_log(__FILE__
": pipe() failed: %s", strerror(errno
));
277 pa_make_nonblock_fd(u
->pipe_fds
[1]);
279 if (!(u
->client
= jack_client_open(client_name
, server_name
? JackServerName
: JackNullOption
, &status
, server_name
))) {
280 pa_log(__FILE__
": jack_client_open() failed.");
284 ports
= jack_get_ports(u
->client
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsOutput
);
287 for (p
= ports
; *p
; p
++)
291 channels
= c
->default_sample_spec
.channels
;
293 if (pa_modargs_get_value_u32(ma
, "channels", &channels
) < 0 || channels
<= 0 || channels
>= PA_CHANNELS_MAX
) {
294 pa_log(__FILE__
": failed to parse channels= argument.");
298 pa_channel_map_init_auto(&map
, channels
, PA_CHANNEL_MAP_ALSA
);
299 if (pa_modargs_get_channel_map(ma
, &map
) < 0 || map
.channels
!= channels
) {
300 pa_log(__FILE__
": failed to parse channel_map= argument.");
304 pa_log_info(__FILE__
": Successfully connected as '%s'", jack_get_client_name(u
->client
));
306 ss
.channels
= u
->channels
= channels
;
307 ss
.rate
= jack_get_sample_rate(u
->client
);
308 ss
.format
= PA_SAMPLE_FLOAT32NE
;
310 assert(pa_sample_spec_valid(&ss
));
312 for (i
= 0; i
< ss
.channels
; i
++) {
313 if (!(u
->port
[i
] = jack_port_register(u
->client
, pa_channel_position_to_string(map
.map
[i
]), JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
|JackPortIsTerminal
, 0))) {
314 pa_log(__FILE__
": jack_port_register() failed.");
319 if (!(u
->source
= pa_source_new(c
, __FILE__
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &ss
, &map
))) {
320 pa_log(__FILE__
": failed to create source.");
324 u
->source
->userdata
= u
;
325 pa_source_set_owner(u
->source
, m
);
326 u
->source
->description
= pa_sprintf_malloc("Jack source (%s)", jack_get_client_name(u
->client
));
327 u
->source
->get_latency
= source_get_latency_cb
;
329 jack_set_process_callback(u
->client
, jack_process
, u
);
330 jack_on_shutdown(u
->client
, jack_shutdown
, u
);
332 if (jack_activate(u
->client
)) {
333 pa_log(__FILE__
": jack_activate() failed");
338 for (i
= 0, p
= ports
; i
< ss
.channels
; i
++, p
++) {
341 pa_log(__FILE__
": not enough physical output ports, leaving unconnected.");
345 pa_log_info(__FILE__
": connecting %s to %s", jack_port_name(u
->port
[i
]), *p
);
347 if (jack_connect(u
->client
, *p
, jack_port_name(u
->port
[i
]))) {
348 pa_log(__FILE__
": failed to connect %s to %s, leaving unconnected.", jack_port_name(u
->port
[i
]), *p
);
355 u
->io_event
= c
->mainloop
->io_new(c
->mainloop
, u
->pipe_fds
[0], PA_IO_EVENT_INPUT
, io_event_cb
, u
);
373 void pa__done(pa_core
*c
, pa_module
*m
) {
377 if (!(u
= m
->userdata
))
381 jack_client_close(u
->client
);
384 c
->mainloop
->io_free(u
->io_event
);
387 pa_source_disconnect(u
->source
);
388 pa_source_unref(u
->source
);
391 if (u
->pipe_fds
[0] >= 0)
392 close(u
->pipe_fds
[0]);
393 if (u
->pipe_fds
[1] >= 0)
394 close(u
->pipe_fds
[1]);
396 pthread_mutex_destroy(&u
->mutex
);
397 pthread_cond_destroy(&u
->cond
);