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
37 #include <jack/jack.h>
39 #include <pulse/xmalloc.h>
41 #include <pulsecore/core-error.h>
42 #include <pulsecore/source.h>
43 #include <pulsecore/module.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/modargs.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/thread.h>
48 #include <pulsecore/thread-mq.h>
49 #include <pulsecore/rtpoll.h>
50 #include <pulsecore/sample-util.h>
52 #include "module-jack-source-symdef.h"
54 /* See module-jack-sink for a few comments how this module basically
57 PA_MODULE_AUTHOR("Lennart Poettering");
58 PA_MODULE_DESCRIPTION("JACK Source");
59 PA_MODULE_VERSION(PACKAGE_VERSION
);
60 PA_MODULE_LOAD_ONCE(TRUE
);
62 "source_name=<name of source> "
63 "server_name=<jack server name> "
64 "client_name=<jack client name> "
65 "channels=<number of channels> "
66 "connect=<connect ports?>"
67 "channel_map=<channel map>");
69 #define DEFAULT_SOURCE_NAME "jack_in"
78 jack_port_t
* port
[PA_CHANNELS_MAX
];
79 jack_client_t
*client
;
81 pa_thread_mq thread_mq
;
82 pa_asyncmsgq
*jack_msgq
;
84 pa_rtpoll_item
*rtpoll_item
;
88 jack_nframes_t saved_frame_time
;
89 pa_bool_t saved_frame_time_valid
;
92 static const char* const valid_modargs
[] = {
103 SOURCE_MESSAGE_POST
= PA_SOURCE_MESSAGE_MAX
,
104 SOURCE_MESSAGE_ON_SHUTDOWN
107 static int source_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
108 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
112 case SOURCE_MESSAGE_POST
:
114 /* Handle the new block from the JACK thread */
116 pa_assert(chunk
->length
> 0);
118 if (u
->source
->thread_info
.state
== PA_SOURCE_RUNNING
)
119 pa_source_post(u
->source
, chunk
);
121 u
->saved_frame_time
= offset
;
122 u
->saved_frame_time_valid
= TRUE
;
126 case SOURCE_MESSAGE_ON_SHUTDOWN
:
127 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
130 case PA_SOURCE_MESSAGE_GET_LATENCY
: {
131 jack_nframes_t l
, ft
, d
;
134 /* This is the "worst-case" latency */
135 l
= jack_port_get_total_latency(u
->client
, u
->port
[0]);
137 if (u
->saved_frame_time_valid
) {
138 /* Adjust the worst case latency by the time that
139 * passed since we last handed data to JACK */
141 ft
= jack_frame_time(u
->client
);
142 d
= ft
> u
->saved_frame_time
? ft
- u
->saved_frame_time
: 0;
146 /* Convert it to usec */
147 n
= l
* pa_frame_size(&u
->source
->sample_spec
);
148 *((pa_usec_t
*) data
) = pa_bytes_to_usec(n
, &u
->source
->sample_spec
);
154 return pa_source_process_msg(o
, code
, data
, offset
, chunk
);
157 static int jack_process(jack_nframes_t nframes
, void *arg
) {
159 struct userdata
*u
= arg
;
160 const void *buffer
[PA_CHANNELS_MAX
];
162 jack_nframes_t frame_time
;
167 for (c
= 0; c
< u
->channels
; c
++)
168 pa_assert(buffer
[c
] = jack_port_get_buffer(u
->port
[c
], nframes
));
170 /* We interleave the data and pass it on to the other RT thread */
172 pa_memchunk_reset(&chunk
);
173 chunk
.length
= nframes
* pa_frame_size(&u
->source
->sample_spec
);
174 chunk
.memblock
= pa_memblock_new(u
->core
->mempool
, chunk
.length
);
175 p
= pa_memblock_acquire(chunk
.memblock
);
176 pa_interleave(buffer
, u
->channels
, p
, sizeof(float), nframes
);
177 pa_memblock_release(chunk
.memblock
);
179 frame_time
= jack_frame_time(u
->client
);
181 pa_asyncmsgq_post(u
->jack_msgq
, PA_MSGOBJECT(u
->source
), SOURCE_MESSAGE_POST
, NULL
, frame_time
, &chunk
, NULL
);
183 pa_memblock_unref(chunk
.memblock
);
188 static void thread_func(void *userdata
) {
189 struct userdata
*u
= userdata
;
193 pa_log_debug("Thread starting up");
195 if (u
->core
->realtime_scheduling
)
196 pa_make_realtime(u
->core
->realtime_priority
);
198 pa_thread_mq_install(&u
->thread_mq
);
199 pa_rtpoll_install(u
->rtpoll
);
204 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0)
212 /* If this was no regular exit from the loop we have to continue
213 * processing messages until we received PA_MESSAGE_SHUTDOWN */
214 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
215 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
218 pa_log_debug("Thread shutting down");
221 static void jack_error_func(const char*t
) {
224 s
= pa_xstrndup(t
, strcspn(t
, "\n\r"));
225 pa_log_warn("JACK error >%s<", s
);
229 static void jack_init(void *arg
) {
230 struct userdata
*u
= arg
;
232 pa_log_info("JACK thread starting up.");
234 if (u
->core
->realtime_scheduling
)
235 pa_make_realtime(u
->core
->realtime_priority
+4);
238 static void jack_shutdown(void* arg
) {
239 struct userdata
*u
= arg
;
241 pa_log_info("JACK thread shutting down..");
242 pa_asyncmsgq_post(u
->jack_msgq
, PA_MSGOBJECT(u
->source
), SOURCE_MESSAGE_ON_SHUTDOWN
, NULL
, 0, NULL
, NULL
);
245 int pa__init(pa_module
*m
) {
246 struct userdata
*u
= NULL
;
249 pa_modargs
*ma
= NULL
;
250 jack_status_t status
;
251 const char *server_name
, *client_name
;
252 uint32_t channels
= 0;
253 pa_bool_t do_connect
= TRUE
;
255 const char **ports
= NULL
, **p
;
257 pa_source_new_data data
;
261 jack_set_error_function(jack_error_func
);
263 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
264 pa_log("Failed to parse module arguments.");
268 if (pa_modargs_get_value_boolean(ma
, "connect", &do_connect
) < 0) {
269 pa_log("Failed to parse connect= argument.");
273 server_name
= pa_modargs_get_value(ma
, "server_name", NULL
);
274 client_name
= pa_modargs_get_value(ma
, "client_name", "PulseAudio JACK Source");
276 u
= pa_xnew0(struct userdata
, 1);
280 u
->saved_frame_time_valid
= FALSE
;
282 u
->rtpoll
= pa_rtpoll_new();
283 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
, u
->rtpoll
);
285 u
->jack_msgq
= pa_asyncmsgq_new(0);
286 u
->rtpoll_item
= pa_rtpoll_item_new_asyncmsgq_read(u
->rtpoll
, PA_RTPOLL_EARLY
-1, u
->jack_msgq
);
288 if (!(u
->client
= jack_client_open(client_name
, server_name
? JackServerName
: JackNullOption
, &status
, server_name
))) {
289 pa_log("jack_client_open() failed.");
293 ports
= jack_get_ports(u
->client
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsOutput
);
296 for (p
= ports
; *p
; p
++)
300 channels
= m
->core
->default_sample_spec
.channels
;
302 if (pa_modargs_get_value_u32(ma
, "channels", &channels
) < 0 || channels
<= 0 || channels
>= PA_CHANNELS_MAX
) {
303 pa_log("failed to parse channels= argument.");
307 pa_assert_se(pa_channel_map_init_auto(&map
, channels
, PA_CHANNEL_MAP_AUX
));
308 pa_channel_map_init_auto(&map
, channels
, PA_CHANNEL_MAP_ALSA
);
309 if (pa_modargs_get_channel_map(ma
, NULL
, &map
) < 0 || map
.channels
!= channels
) {
310 pa_log("failed to parse channel_map= argument.");
314 pa_log_info("Successfully connected as '%s'", jack_get_client_name(u
->client
));
316 ss
.channels
= u
->channels
= channels
;
317 ss
.rate
= jack_get_sample_rate(u
->client
);
318 ss
.format
= PA_SAMPLE_FLOAT32NE
;
320 pa_assert(pa_sample_spec_valid(&ss
));
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.");
329 pa_source_new_data_init(&data
);
330 data
.driver
= __FILE__
;
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");
337 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, server_name
);
338 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, t
= pa_sprintf_malloc("Jack source (%s)", jack_get_client_name(u
->client
)));
340 pa_proplist_sets(data
.proplist
, "jack.client_name", jack_get_client_name(u
->client
));
342 u
->source
= pa_source_new(m
->core
, &data
, PA_SOURCE_LATENCY
);
343 pa_source_new_data_done(&data
);
346 pa_log("Failed to create source.");
350 u
->source
->parent
.process_msg
= source_process_msg
;
351 u
->source
->userdata
= u
;
353 pa_source_set_asyncmsgq(u
->source
, u
->thread_mq
.inq
);
354 pa_source_set_rtpoll(u
->source
, u
->rtpoll
);
356 jack_set_process_callback(u
->client
, jack_process
, u
);
357 jack_on_shutdown(u
->client
, jack_shutdown
, u
);
358 jack_set_thread_init_callback(u
->client
, jack_init
, u
);
360 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
361 pa_log("Failed to create thread.");
365 if (jack_activate(u
->client
)) {
366 pa_log("jack_activate() failed");
371 for (i
= 0, p
= ports
; i
< ss
.channels
; i
++, p
++) {
374 pa_log("not enough physical output ports, leaving unconnected.");
378 pa_log_info("connecting %s to %s", jack_port_name(u
->port
[i
]), *p
);
380 if (jack_connect(u
->client
, *p
, jack_port_name(u
->port
[i
]))) {
381 pa_log("failed to connect %s to %s, leaving unconnected.", jack_port_name(u
->port
[i
]), *p
);
388 pa_source_put(u
->source
);
406 void pa__done(pa_module
*m
) {
410 if (!(u
= m
->userdata
))
414 jack_client_close(u
->client
);
417 pa_source_unlink(u
->source
);
420 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
421 pa_thread_free(u
->thread
);
424 pa_thread_mq_done(&u
->thread_mq
);
427 pa_source_unref(u
->source
);
430 pa_rtpoll_item_free(u
->rtpoll_item
);
433 pa_asyncmsgq_unref(u
->jack_msgq
);
436 pa_rtpoll_free(u
->rtpoll
);