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