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