]> code.delx.au - pulseaudio/blob - src/modules/module-jack-sink.c
rework memory block management to be thread-safe and mostly lock-free.
[pulseaudio] / src / modules / module-jack-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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 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 <sys/stat.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <limits.h>
35 #include <pthread.h>
36
37 #include <jack/jack.h>
38
39 #include <pulse/xmalloc.h>
40
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>
49
50 #include "module-jack-sink-symdef.h"
51
52 PA_MODULE_AUTHOR("Lennart Poettering")
53 PA_MODULE_DESCRIPTION("Jack Sink")
54 PA_MODULE_VERSION(PACKAGE_VERSION)
55 PA_MODULE_USAGE(
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>")
62
63 #define DEFAULT_SINK_NAME "jack_out"
64
65 struct userdata {
66 pa_core *core;
67 pa_module *module;
68
69 pa_sink *sink;
70
71 unsigned channels;
72
73 jack_port_t* port[PA_CHANNELS_MAX];
74 jack_client_t *client;
75
76 pthread_mutex_t mutex;
77 pthread_cond_t cond;
78
79 void * buffer[PA_CHANNELS_MAX];
80 jack_nframes_t frames_requested;
81 int quit_requested;
82
83 int pipe_fd_type;
84 int pipe_fds[2];
85 pa_io_event *io_event;
86
87 jack_nframes_t frames_in_buffer;
88 jack_nframes_t timestamp;
89 };
90
91 static const char* const valid_modargs[] = {
92 "sink_name",
93 "server_name",
94 "client_name",
95 "channels",
96 "connect",
97 "channel_map",
98 NULL
99 };
100
101 static void stop_sink(struct userdata *u) {
102 assert (u);
103
104 jack_client_close(u->client);
105 u->client = NULL;
106 u->core->mainloop->io_free(u->io_event);
107 u->io_event = NULL;
108 pa_sink_disconnect(u->sink);
109 pa_sink_unref(u->sink);
110 u->sink = NULL;
111 pa_module_unload_request(u->module);
112 }
113
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;
116 char x;
117
118 assert(m);
119 assert(e);
120 assert(flags == PA_IO_EVENT_INPUT);
121 assert(u);
122 assert(u->pipe_fds[0] == fd);
123
124 pa_read(fd, &x, 1, &u->pipe_fd_type);
125
126 if (u->quit_requested) {
127 stop_sink(u);
128 u->quit_requested = 0;
129 return;
130 }
131
132 pthread_mutex_lock(&u->mutex);
133
134 if (u->frames_requested > 0) {
135 unsigned fs;
136 jack_nframes_t frame_idx;
137 pa_memchunk chunk;
138 void *p;
139
140 fs = pa_frame_size(&u->sink->sample_spec);
141
142 pa_sink_render_full(u->sink, u->frames_requested * fs, &chunk);
143 p = pa_memblock_acquire(chunk.memblock);
144
145 for (frame_idx = 0; frame_idx < u->frames_requested; frame_idx ++) {
146 unsigned c;
147
148 for (c = 0; c < u->channels; c++) {
149 float *s = ((float*) ((uint8_t*) p + chunk.index)) + (frame_idx * u->channels) + c;
150 float *d = ((float*) u->buffer[c]) + frame_idx;
151
152 *d = *s;
153 }
154 }
155
156 pa_memblock_release(chunk.memblock);
157 pa_memblock_unref(chunk.memblock);
158
159 u->frames_requested = 0;
160
161 pthread_cond_signal(&u->cond);
162 }
163
164 pthread_mutex_unlock(&u->mutex);
165 }
166
167 static void request_render(struct userdata *u) {
168 char c = 'x';
169 assert(u);
170
171 assert(u->pipe_fds[1] >= 0);
172 pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type);
173 }
174
175 static void jack_shutdown(void *arg) {
176 struct userdata *u = arg;
177 assert(u);
178
179 u->quit_requested = 1;
180 request_render(u);
181 }
182
183 static int jack_process(jack_nframes_t nframes, void *arg) {
184 struct userdata *u = arg;
185 assert(u);
186
187 if (jack_transport_query(u->client, NULL) == JackTransportRolling) {
188 unsigned c;
189
190 pthread_mutex_lock(&u->mutex);
191
192 u->frames_requested = nframes;
193
194 for (c = 0; c < u->channels; c++) {
195 u->buffer[c] = jack_port_get_buffer(u->port[c], nframes);
196 assert(u->buffer[c]);
197 }
198
199 request_render(u);
200
201 pthread_cond_wait(&u->cond, &u->mutex);
202
203 u->frames_in_buffer = nframes;
204 u->timestamp = jack_get_current_transport_frame(u->client);
205
206 pthread_mutex_unlock(&u->mutex);
207 }
208
209 return 0;
210 }
211
212 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
213 struct userdata *u;
214 jack_nframes_t n, l, d;
215
216 assert(s);
217 u = s->userdata;
218
219 if (jack_transport_query(u->client, NULL) != JackTransportRolling)
220 return 0;
221
222 n = jack_get_current_transport_frame(u->client);
223
224 if (n < u->timestamp)
225 return 0;
226
227 d = n - u->timestamp;
228 l = jack_port_get_total_latency(u->client, u->port[0]) + u->frames_in_buffer;
229
230 if (d >= l)
231 return 0;
232
233 return pa_bytes_to_usec((l - d) * pa_frame_size(&s->sample_spec), &s->sample_spec);
234 }
235
236 static void jack_error_func(const char*t) {
237 pa_log_warn("JACK error >%s<", t);
238 }
239
240 int pa__init(pa_core *c, 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 int do_connect = 1;
249 unsigned i;
250 const char **ports = NULL, **p;
251 char *t;
252
253 assert(c);
254 assert(m);
255
256 jack_set_error_function(jack_error_func);
257
258 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
259 pa_log("failed to parse module arguments.");
260 goto fail;
261 }
262
263 if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) {
264 pa_log("failed to parse connect= argument.");
265 goto fail;
266 }
267
268 server_name = pa_modargs_get_value(ma, "server_name", NULL);
269 client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio");
270
271 u = pa_xnew0(struct userdata, 1);
272 m->userdata = u;
273 u->core = c;
274 u->module = m;
275 u->pipe_fds[0] = u->pipe_fds[1] = -1;
276 u->pipe_fd_type = 0;
277
278 pthread_mutex_init(&u->mutex, NULL);
279 pthread_cond_init(&u->cond, NULL);
280
281 if (pipe(u->pipe_fds) < 0) {
282 pa_log("pipe() failed: %s", pa_cstrerror(errno));
283 goto fail;
284 }
285
286 pa_make_nonblock_fd(u->pipe_fds[1]);
287
288 if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
289 pa_log("jack_client_open() failed.");
290 goto fail;
291 }
292
293 ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
294
295 channels = 0;
296 for (p = ports; *p; p++)
297 channels++;
298
299 if (!channels)
300 channels = c->default_sample_spec.channels;
301
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.");
304 goto fail;
305 }
306
307 pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA);
308 if (pa_modargs_get_channel_map(ma, &map) < 0 || map.channels != channels) {
309 pa_log("failed to parse channel_map= argument.");
310 goto fail;
311 }
312
313 pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client));
314
315 ss.channels = u->channels = channels;
316 ss.rate = jack_get_sample_rate(u->client);
317 ss.format = PA_SAMPLE_FLOAT32NE;
318
319 assert(pa_sample_spec_valid(&ss));
320
321 for (i = 0; i < ss.channels; i++) {
322 if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) {
323 pa_log("jack_port_register() failed.");
324 goto fail;
325 }
326 }
327
328 if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
329 pa_log("failed to create sink.");
330 goto fail;
331 }
332
333 u->sink->userdata = u;
334 pa_sink_set_owner(u->sink, m);
335 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u->client)));
336 pa_xfree(t);
337 u->sink->get_latency = sink_get_latency_cb;
338
339 jack_set_process_callback(u->client, jack_process, u);
340 jack_on_shutdown(u->client, jack_shutdown, u);
341
342 if (jack_activate(u->client)) {
343 pa_log("jack_activate() failed");
344 goto fail;
345 }
346
347 if (do_connect) {
348 for (i = 0, p = ports; i < ss.channels; i++, p++) {
349
350 if (!*p) {
351 pa_log("not enough physical output ports, leaving unconnected.");
352 break;
353 }
354
355 pa_log_info("connecting %s to %s", jack_port_name(u->port[i]), *p);
356
357 if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) {
358 pa_log("failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
359 break;
360 }
361 }
362
363 }
364
365 u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u);
366
367 free(ports);
368 pa_modargs_free(ma);
369
370 return 0;
371
372 fail:
373 if (ma)
374 pa_modargs_free(ma);
375
376 free(ports);
377
378 pa__done(c, m);
379
380 return -1;
381 }
382
383 void pa__done(pa_core *c, pa_module*m) {
384 struct userdata *u;
385 assert(c && m);
386
387 if (!(u = m->userdata))
388 return;
389
390 if (u->client)
391 jack_client_close(u->client);
392
393 if (u->io_event)
394 c->mainloop->io_free(u->io_event);
395
396 if (u->sink) {
397 pa_sink_disconnect(u->sink);
398 pa_sink_unref(u->sink);
399 }
400
401 if (u->pipe_fds[0] >= 0)
402 close(u->pipe_fds[0]);
403 if (u->pipe_fds[1] >= 0)
404 close(u->pipe_fds[1]);
405
406 pthread_mutex_destroy(&u->mutex);
407 pthread_cond_destroy(&u->cond);
408 pa_xfree(u);
409 }