]> code.delx.au - pulseaudio/blob - src/modules/module-jack-sink.c
a40ebe290dc1dfa77d28b807146c5f58bbe444ba
[pulseaudio] / src / modules / module-jack-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006, 2007 Lennart Poettering and Tanu Kaskinen
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 <pthread.h>
29 #include <assert.h>
30 #include <errno.h>
31
32 #include <jack/jack.h>
33 #include <jack/ringbuffer.h>
34 #include <jack/types.h>
35
36 #include <pulse/mainloop-api.h>
37 #include <pulse/sample.h>
38 #include <pulse/channelmap.h>
39 #include <pulse/xmalloc.h>
40
41 #include <pulsecore/sink.h>
42 #include <pulsecore/module.h>
43 #include <pulsecore/core.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/core-error.h>
47 #include <pulsecore/pipe.h>
48 #include <pulsecore/modargs.h>
49 #include <pulsecore/strbuf.h>
50
51 #include "module-jack-sink-symdef.h"
52
53 PA_MODULE_AUTHOR("Lennart Poettering & Tanu Kaskinen")
54 PA_MODULE_DESCRIPTION("Jack Sink")
55 PA_MODULE_VERSION(PACKAGE_VERSION)
56 PA_MODULE_USAGE(
57 "sink_name=<name of sink> "
58 "server_name=<jack server name> "
59 "client_name=<jack client name> "
60 "channels=<number of channels> "
61 "connect=<connect ports automatically?> "
62 "buffersize=<intermediate buffering in frames> "
63 "channel_map=<channel map>")
64
65 #define DEFAULT_SINK_NAME "jack_out"
66 #define DEFAULT_CLIENT_NAME "PulseAudio(output)"
67 #define DEFAULT_RINGBUFFER_SIZE 4096
68
69
70 struct userdata {
71 pa_sink *sink;
72
73 unsigned channels;
74 unsigned frame_size;
75
76 jack_port_t* j_ports[PA_CHANNELS_MAX];
77 jack_client_t *j_client;
78
79 jack_nframes_t j_buffersize;
80
81 /* For avoiding j_buffersize changes at a wrong moment. */
82 pthread_mutex_t buffersize_mutex;
83
84 /* The intermediate store where the pulse side writes to and the jack side
85 reads from. */
86 jack_ringbuffer_t* ringbuffer;
87
88 /* For signaling when there's room in the ringbuffer. */
89 pthread_mutex_t cond_mutex;
90 pthread_cond_t ringbuffer_cond;
91
92 pthread_t filler_thread; /* Keeps the ringbuffer filled. */
93
94 int ringbuffer_is_full;
95 int filler_thread_is_running;
96 int quit_requested;
97
98 int pipe_fd_type;
99 int pipe_fds[2];
100 pa_io_event *io_event;
101 };
102
103
104 struct options {
105 char* sink_name;
106 int sink_name_given;
107
108 char* server_name; /* May be NULL */
109 int server_name_given;
110
111 char* client_name;
112 int client_name_given;
113
114 unsigned channels;
115 int channels_given;
116
117 int connect;
118 int connect_given;
119
120 unsigned buffersize;
121 int buffersize_given;
122
123 pa_channel_map map;
124 int map_given;
125 };
126
127
128 static const char* const valid_modargs[] = {
129 "sink_name",
130 "server_name",
131 "client_name",
132 "channels",
133 "connect",
134 "buffersize",
135 "channel_map",
136 NULL
137 };
138
139
140 /* Initialization functions. */
141 static int parse_options(struct options* o, const char* argument);
142 static void set_default_channels(pa_module* self, struct options* o);
143 static int create_sink(pa_module* self, struct options *o);
144 static void connect_ports(pa_module* self);
145 static int start_filling_ringbuffer(pa_module* self);
146
147 /* Various callbacks. */
148 static void jack_error_func(const char* t);
149 static pa_usec_t sink_get_latency_cb(pa_sink* s);
150 static int jack_process(jack_nframes_t nframes, void* arg);
151 static int jack_blocksize_cb(jack_nframes_t nframes, void* arg);
152 static void jack_shutdown(void* arg);
153 static void io_event_cb(pa_mainloop_api* m, pa_io_event* e, int fd,
154 pa_io_event_flags_t flags, void* userdata);
155
156 /* The ringbuffer filler thread runs in this function. */
157 static void* fill_ringbuffer(void* arg);
158
159 /* request_render asks asynchronously the mainloop to call io_event_cb. */
160 static void request_render(struct userdata* u);
161
162
163 int pa__init(pa_core* c, pa_module* self) {
164 struct userdata* u = NULL;
165 struct options o;
166 unsigned i;
167
168 assert(c);
169 assert(self);
170
171 o.sink_name = NULL;
172 o.server_name = NULL;
173 o.client_name = NULL;
174
175 self->userdata = pa_xnew0(struct userdata, 1);
176 u = self->userdata;
177
178 u->pipe_fds[0] = u->pipe_fds[1] = -1;
179 u->pipe_fd_type = 0;
180 u->ringbuffer_is_full = 0;
181 u->filler_thread_is_running = 0;
182 u->quit_requested = 0;
183 pthread_mutex_init(&u->buffersize_mutex, NULL);
184 pthread_mutex_init(&u->cond_mutex, NULL);
185 pthread_cond_init(&u->ringbuffer_cond, NULL);
186
187 if (parse_options(&o, self->argument) != 0)
188 goto fail;
189
190 jack_set_error_function(jack_error_func);
191
192 if (!(u->j_client = jack_client_open(
193 o.client_name,
194 o.server_name ? JackServerName : JackNullOption,
195 NULL, o.server_name))) {
196 pa_log_error("jack_client_open() failed.");
197 goto fail;
198 }
199 pa_log_info("Successfully connected as '%s'",
200 jack_get_client_name(u->j_client));
201
202 if (!o.channels_given)
203 set_default_channels(self, &o);
204
205 u->channels = o.channels;
206
207 if (!o.map_given)
208 pa_channel_map_init_auto(&o.map, u->channels, PA_CHANNEL_MAP_ALSA);
209
210 for (i = 0; i < u->channels; i++) {
211 char* port_name = pa_sprintf_malloc(
212 "out_%i:%s", i+1,
213 pa_channel_position_to_string(o.map.map[i]));
214
215 if (!(u->j_ports[i] = jack_port_register(
216 u->j_client, port_name,
217 JACK_DEFAULT_AUDIO_TYPE,
218 JackPortIsOutput|JackPortIsTerminal, 0))) {
219 pa_log("jack_port_register() failed.");
220 goto fail;
221 }
222
223 pa_xfree(port_name);
224 }
225
226 if (pipe(u->pipe_fds) < 0) {
227 pa_log("pipe() failed: %s", pa_cstrerror(errno));
228 goto fail;
229 }
230 pa_make_nonblock_fd(u->pipe_fds[1]);
231
232 if (create_sink(self, &o) != 0)
233 goto fail;
234
235 u->frame_size = pa_frame_size(&u->sink->sample_spec);
236 u->j_buffersize = jack_get_buffer_size(u->j_client);
237
238 /* If the ringbuffer size were equal to the jack buffer size, a full block
239 would never fit in the ringbuffer, because the ringbuffer can never be
240 totally full: one slot is always wasted. */
241 if (o.buffersize <= u->j_buffersize) {
242 o.buffersize = u->j_buffersize + 1;
243 }
244 /* The actual ringbuffer size will be rounded up to the nearest power of
245 two. */
246 if (!(u->ringbuffer = jack_ringbuffer_create(
247 o.buffersize * u->frame_size))) {
248 pa_log("jack_ringbuffer_create() failed.");
249 goto fail;
250 }
251 assert((u->ringbuffer->size % sizeof(float)) == 0);
252 pa_log_info("buffersize is %u frames (%u samples, %u bytes).",
253 u->ringbuffer->size / u->frame_size,
254 u->ringbuffer->size / sizeof(float),
255 u->ringbuffer->size);
256
257 jack_set_process_callback(u->j_client, jack_process, u);
258 jack_set_buffer_size_callback(u->j_client, jack_blocksize_cb, u);
259 jack_on_shutdown(u->j_client, jack_shutdown, u);
260
261 if (jack_activate(u->j_client)) {
262 pa_log("jack_activate() failed.");
263 goto fail;
264 }
265
266 if (o.connect)
267 connect_ports(self);
268
269 u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0],
270 PA_IO_EVENT_INPUT, io_event_cb, self);
271
272 if (start_filling_ringbuffer(self) != 0)
273 goto fail;
274
275 pa_xfree(o.sink_name);
276 pa_xfree(o.server_name);
277 pa_xfree(o.client_name);
278
279 return 0;
280
281 fail:
282 pa_xfree(o.sink_name);
283 pa_xfree(o.server_name);
284 pa_xfree(o.client_name);
285 pa__done(c, self);
286
287 return -1;
288 }
289
290
291 static int parse_options(struct options* o, const char* argument) {
292 pa_modargs *ma = NULL;
293 const char* arg_val;
294 pa_strbuf* strbuf;
295
296 assert(o);
297
298 if (!(ma = pa_modargs_new(argument, valid_modargs))) {
299 pa_log_error("Failed to parse module arguments.");
300 goto fail;
301 }
302
303 strbuf = pa_strbuf_new();
304 if ((arg_val = pa_modargs_get_value(ma, "sink_name", NULL))) {
305 pa_strbuf_puts(strbuf, arg_val);
306 o->sink_name = pa_strbuf_tostring(strbuf);
307 o->sink_name_given = 1;
308 } else {
309 pa_strbuf_puts(strbuf, DEFAULT_SINK_NAME);
310 o->sink_name = pa_strbuf_tostring(strbuf);
311 o->sink_name_given = 0;
312 }
313 pa_strbuf_free(strbuf);
314
315 strbuf = pa_strbuf_new();
316 if ((arg_val = pa_modargs_get_value(ma, "server_name", NULL))) {
317 pa_strbuf_puts(strbuf, arg_val);
318 o->server_name = pa_strbuf_tostring(strbuf);
319 o->server_name_given = 1;
320 } else {
321 o->server_name = NULL;
322 o->server_name_given = 0;
323 }
324 pa_strbuf_free(strbuf);
325
326 strbuf = pa_strbuf_new();
327 if ((arg_val = pa_modargs_get_value(ma, "client_name", NULL))) {
328 pa_strbuf_puts(strbuf, arg_val);
329 o->client_name = pa_strbuf_tostring(strbuf);
330 o->client_name_given = 1;
331 } else {
332 pa_strbuf_puts(strbuf, DEFAULT_CLIENT_NAME);
333 o->client_name = pa_strbuf_tostring(strbuf);
334 o->client_name_given = 1;
335 }
336 pa_strbuf_free(strbuf);
337
338 if (pa_modargs_get_value(ma, "channels", NULL)) {
339 o->channels_given = 1;
340 if (pa_modargs_get_value_u32(ma, "channels", &o->channels) < 0 ||
341 o->channels == 0 ||
342 o->channels >= PA_CHANNELS_MAX) {
343 pa_log_error("Failed to parse the \"channels\" argument.");
344 goto fail;
345 }
346 } else {
347 o->channels = 0; /* The actual default value is the number of physical
348 input ports in jack (unknown at the moment), or if
349 that's zero, then the default_sample_spec.channels
350 of the core. */
351 o->channels_given = 0;
352 }
353
354 if (pa_modargs_get_value(ma, "connect", NULL)) {
355 o->connect_given = 1;
356 if (pa_modargs_get_value_boolean(ma, "connect", &o->connect) < 0) {
357 pa_log_error("Failed to parse the \"connect\" argument.");
358 goto fail;
359 }
360 } else {
361 o->connect = 1;
362 o->connect_given = 0;
363 }
364
365 if (pa_modargs_get_value(ma, "buffersize", NULL)) {
366 o->buffersize_given = 1;
367 if (pa_modargs_get_value_u32(ma, "buffersize", &o->buffersize) < 0) {
368 pa_log_error("Failed to parse the \"buffersize\" argument.");
369 goto fail;
370 }
371 } else {
372 o->buffersize = DEFAULT_RINGBUFFER_SIZE;
373 o->buffersize_given = 0;
374 }
375
376 if (pa_modargs_get_value(ma, "channel_map", NULL)) {
377 o->map_given = 1;
378 if (pa_modargs_get_channel_map(ma, &o->map) < 0) {
379 pa_log_error("Failed to parse the \"channel_map\" argument.");
380 goto fail;
381 }
382
383 /* channel_map specifies the channel count too. */
384 if (o->channels_given && (o->channels != o->map.channels)) {
385 pa_log_error(
386 "\"channels\" and \"channel_map\" arguments conficted. If you "
387 "use the \"channel_map\" argument, you can omit the "
388 "\"channels\" argument.");
389 goto fail;
390 } else {
391 o->channels = o->map.channels;
392 o->channels_given = 1;
393 }
394 } else {
395 /* The actual default value is the default alsa mappings, but that
396 can't be set until the channel count is known. Here we initialize
397 the map to some valid value, although the value won't be used. */
398 pa_channel_map_init_stereo(&o->map);
399 o->map_given = 0;
400 }
401
402 pa_modargs_free(ma);
403
404 return 0;
405
406 fail:
407 if (ma)
408 pa_modargs_free(ma);
409
410 return -1;
411 }
412
413
414 static void set_default_channels(pa_module* self, struct options* o) {
415 struct userdata* u;
416 const char **ports, **p;
417
418 assert(self);
419 assert(o);
420 assert(self->userdata);
421
422 u = self->userdata;
423
424 assert(u->j_client);
425 assert(self->core);
426
427 o->channels = 0;
428
429 ports = jack_get_ports(u->j_client, NULL, JACK_DEFAULT_AUDIO_TYPE,
430 JackPortIsPhysical|JackPortIsInput);
431
432 for (p = ports; *p; p++)
433 o->channels++;
434
435 free(ports);
436
437 if (o->channels >= PA_CHANNELS_MAX)
438 o->channels = PA_CHANNELS_MAX - 1;
439
440 if (o->channels == 0)
441 o->channels = self->core->default_sample_spec.channels;
442 }
443
444
445 static int create_sink(pa_module* self, struct options* o) {
446 struct userdata* u;
447 pa_sample_spec ss;
448 char *t;
449
450 assert(self);
451 assert(o);
452 assert(self->userdata);
453
454 u = self->userdata;
455
456 assert(u->j_client);
457
458 ss.channels = u->channels;
459 ss.rate = jack_get_sample_rate(u->j_client);
460 ss.format = PA_SAMPLE_FLOAT32NE;
461 assert(pa_sample_spec_valid(&ss));
462
463 if (!(u->sink = pa_sink_new(self->core, __FILE__, o->sink_name, 0, &ss,
464 &o->map))) {
465 pa_log("failed to create sink.");
466 return -1;
467 }
468
469 u->sink->userdata = u;
470 pa_sink_set_owner(u->sink, self);
471
472 pa_sink_set_description(
473 u->sink,
474 t = pa_sprintf_malloc("Jack sink (%s)",
475 jack_get_client_name(u->j_client)));
476 pa_xfree(t);
477
478 u->sink->get_latency = sink_get_latency_cb;
479
480 return 0;
481 }
482
483
484 static void connect_ports(pa_module* self) {
485 struct userdata* u;
486 unsigned i;
487 const char **ports, **p;
488
489 assert(self);
490 assert(self->userdata);
491
492 u = self->userdata;
493
494 assert(u->j_client);
495
496 ports = jack_get_ports(u->j_client, NULL, JACK_DEFAULT_AUDIO_TYPE,
497 JackPortIsPhysical|JackPortIsInput);
498
499 for (i = 0, p = ports; i < u->channels; i++, p++) {
500 assert(u->j_ports[i]);
501
502 if (!*p) {
503 pa_log("Not enough physical output ports, leaving unconnected.");
504 break;
505 }
506
507 pa_log_info("connecting %s to %s",
508 jack_port_name(u->j_ports[i]), *p);
509
510 if (jack_connect(u->j_client, jack_port_name(u->j_ports[i]), *p)) {
511 pa_log("Failed to connect %s to %s, leaving unconnected.",
512 jack_port_name(u->j_ports[i]), *p);
513 break;
514 }
515 }
516
517 free(ports);
518 }
519
520
521 static int start_filling_ringbuffer(pa_module* self) {
522 struct userdata* u;
523 pthread_attr_t thread_attributes;
524
525 assert(self);
526 assert(self->userdata);
527
528 u = self->userdata;
529
530 pthread_attr_init(&thread_attributes);
531
532 if (pthread_attr_setinheritsched(&thread_attributes,
533 PTHREAD_INHERIT_SCHED) != 0) {
534 pa_log("pthread_attr_setinheritsched() failed.");
535 goto fail;
536 }
537 else if (pthread_create(&u->filler_thread, &thread_attributes,
538 fill_ringbuffer, u) != 0) {
539 pa_log("pthread_create() failed.");
540 goto fail;
541 }
542
543 u->filler_thread_is_running = 1;
544
545 pthread_attr_destroy(&thread_attributes);
546
547 return 0;
548
549 fail:
550 pthread_attr_destroy(&thread_attributes);
551 return -1;
552 }
553
554
555 static void jack_error_func(const char* t) {
556 pa_log_warn("JACK error >%s<", t);
557 }
558
559
560 static pa_usec_t sink_get_latency_cb(pa_sink* s) {
561 /* The latency is approximately the sum of the first port's latency,
562 buffersize of jack and the ringbuffer size. Maybe instead of using just
563 the first port, the max of all ports' latencies should be used? */
564 struct userdata* u;
565 jack_nframes_t l;
566
567 assert(s);
568 assert(s->userdata);
569
570 u = s->userdata;
571
572 l = jack_port_get_total_latency(u->j_client, u->j_ports[0]) +
573 u->j_buffersize + u->ringbuffer->size / u->frame_size;
574
575 return pa_bytes_to_usec(l * u->frame_size, &s->sample_spec);
576 }
577
578
579 static int jack_process(jack_nframes_t nframes, void* arg) {
580 struct userdata* u = arg;
581 float* j_buffers[PA_CHANNELS_MAX];
582 unsigned nsamples = u->channels * nframes;
583 unsigned sample_idx_part1, sample_idx_part2;
584 jack_nframes_t frame_idx;
585 jack_ringbuffer_data_t data[2]; /* In case the readable area in the
586 ringbuffer is non-continuous, the data
587 will be split in two parts. */
588 unsigned chan;
589 unsigned samples_left_over;
590
591 for (chan = 0; chan < u->channels; chan++) {
592 j_buffers[chan] = jack_port_get_buffer(u->j_ports[chan], nframes);
593 }
594
595 jack_ringbuffer_get_read_vector(u->ringbuffer, data);
596
597 /* We assume that the possible discontinuity doesn't happen in the middle
598 * of a sample. Should be a safe assumption. */
599 assert(((data[0].len % sizeof(float)) == 0) ||
600 (data[1].len == 0));
601
602 /* Copy from the first part of data until enough samples are copied or the
603 first part ends. */
604 sample_idx_part1 = 0;
605 chan = 0;
606 frame_idx = 0;
607 while (sample_idx_part1 < nsamples &&
608 ((sample_idx_part1 + 1) * sizeof(float)) <= data[0].len) {
609 float *s = ((float*) data[0].buf) + sample_idx_part1;
610 float *d = j_buffers[chan] + frame_idx;
611 *d = *s;
612
613 sample_idx_part1++;
614 chan = (chan + 1) % u->channels;
615 frame_idx = sample_idx_part1 / u->channels;
616 }
617
618 samples_left_over = nsamples - sample_idx_part1;
619
620 /* Copy from the second part of data until enough samples are copied or the
621 second part ends. */
622 sample_idx_part2 = 0;
623 while (sample_idx_part2 < samples_left_over &&
624 ((sample_idx_part2 + 1) * sizeof(float)) <= data[1].len) {
625 float *s = ((float*) data[1].buf) + sample_idx_part2;
626 float *d = j_buffers[chan] + frame_idx;
627 *d = *s;
628
629 sample_idx_part2++;
630 chan = (chan + 1) % u->channels;
631 frame_idx = (sample_idx_part1 + sample_idx_part2) / u->channels;
632 }
633
634 samples_left_over -= sample_idx_part2;
635
636 /* If there's still samples left, fill the buffers with zeros. */
637 while (samples_left_over > 0) {
638 float *d = j_buffers[chan] + frame_idx;
639 *d = 0.0;
640
641 samples_left_over--;
642 chan = (chan + 1) % u->channels;
643 frame_idx = (nsamples - samples_left_over) / u->channels;
644 }
645
646 jack_ringbuffer_read_advance(
647 u->ringbuffer, (sample_idx_part1 + sample_idx_part2) * sizeof(float));
648
649 /* Tell the rendering part that there is room in the ringbuffer. */
650 u->ringbuffer_is_full = 0;
651 pthread_cond_signal(&u->ringbuffer_cond);
652
653 return 0;
654 }
655
656
657 static int jack_blocksize_cb(jack_nframes_t nframes, void* arg) {
658 /* This gets called in the processing thread, so do we have to be realtime
659 safe? No, we can do whatever we want. User gets silence while we do it.
660
661 In addition to just updating the j_buffersize field in userdata, we have
662 to create a new ringbuffer, if the new buffer size is bigger or equal to
663 the old ringbuffer size. */
664 struct userdata* u = arg;
665
666 assert(u);
667
668 /* We don't want to change the blocksize and the ringbuffer while rendering
669 is going on. */
670 pthread_mutex_lock(&u->buffersize_mutex);
671
672 u->j_buffersize = nframes;
673
674 if ((u->ringbuffer->size / u->frame_size) <= nframes) {
675 /* We have to create a new ringbuffer. What are we going to do with the
676 old data in the old buffer? We throw it away, because we're lazy
677 coders. The listening experience is likely to get ruined anyway
678 during the blocksize change. */
679 jack_ringbuffer_free(u->ringbuffer);
680
681 /* The actual ringbuffer size will be rounded up to the nearest power
682 of two. */
683 if (!(u->ringbuffer =
684 jack_ringbuffer_create((nframes + 1) * u->frame_size))) {
685 pa_log_error(
686 "jack_ringbuffer_create() failed while changing jack's buffer "
687 "size, module exiting.");
688 jack_client_close(u->j_client);
689 u->quit_requested = 1;
690 }
691 assert((u->ringbuffer->size % sizeof(float)) == 0);
692 pa_log_info("buffersize is %u frames (%u samples, %u bytes).",
693 u->ringbuffer->size / u->frame_size,
694 u->ringbuffer->size / sizeof(float),
695 u->ringbuffer->size);
696 }
697
698 pthread_mutex_unlock(&u->buffersize_mutex);
699
700 return 0;
701 }
702
703
704 static void jack_shutdown(void* arg) {
705 struct userdata* u = arg;
706 assert(u);
707
708 u->quit_requested = 1;
709 request_render(u);
710 }
711
712
713 static void io_event_cb(pa_mainloop_api* m, pa_io_event* e, int fd,
714 pa_io_event_flags_t flags, void* userdata) {
715 pa_module* self = userdata;
716 struct userdata* u;
717 char x;
718 jack_ringbuffer_data_t buffer[2]; /* The write area in the ringbuffer may
719 be split in two parts. */
720 pa_memchunk chunk; /* This is the data source. */
721 unsigned part1_length, part2_length;
722 unsigned sample_idx_part1, sample_idx_part2;
723 unsigned chan;
724 unsigned frame_size;
725 int rem;
726
727 assert(m);
728 assert(e);
729 assert(flags == PA_IO_EVENT_INPUT);
730 assert(self);
731 assert(self->userdata);
732
733 u = self->userdata;
734
735 assert(u->pipe_fds[0] == fd);
736
737 pa_read(fd, &x, 1, &u->pipe_fd_type);
738
739 if (u->quit_requested) {
740 pa_module_unload_request(self);
741 return;
742 }
743
744 frame_size = u->frame_size;
745
746 /* No blocksize changes during rendering, please. */
747 pthread_mutex_lock(&u->buffersize_mutex);
748
749 jack_ringbuffer_get_write_vector(u->ringbuffer, buffer);
750 assert(((buffer[0].len % sizeof(float)) == 0) || (buffer[1].len == 0));
751
752 part1_length = buffer[0].len / sizeof(float);
753 part2_length = buffer[1].len / sizeof(float);
754
755 /* If the amount of free space is not a multiple of the frame size, we have
756 to truncate the lengths so that we process only complete frames. */
757 if ((rem = (part1_length + part2_length) % u->channels) != 0) {
758 if (part2_length >= rem) {
759 part2_length -= rem;
760 } else {
761 part1_length -= rem - part2_length;
762 part2_length = 0;
763 }
764 }
765
766 /* pa_sink_render_full doesn't accept zero length, so we have do the
767 copying only if there's data to copy, which actually makes a kind of
768 sense. */
769 if (part1_length > 0 || part2_length > 0) {
770 pa_sink_render_full(u->sink,
771 (part1_length + part2_length) * sizeof(float),
772 &chunk);
773
774 /* Write to the first part of the buffer. */
775 for (sample_idx_part1 = 0;
776 sample_idx_part1 < part1_length;
777 sample_idx_part1++) {
778 float *s =
779 ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) +
780 sample_idx_part1;
781 float *d = ((float*) buffer[0].buf) + sample_idx_part1;
782 *d = *s;
783 }
784
785 /* Write to the second part of the buffer. */
786 for (sample_idx_part2 = 0;
787 sample_idx_part2 < part2_length;
788 sample_idx_part2++) {
789 float *s =
790 ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) +
791 sample_idx_part1 + sample_idx_part2;
792 float *d = ((float*) buffer[1].buf) + sample_idx_part2;
793 *d = *s;
794 }
795
796 pa_memblock_unref(chunk.memblock);
797
798 jack_ringbuffer_write_advance(
799 u->ringbuffer, (part1_length + part2_length) * sizeof(float));
800 }
801
802 /* Blocksize can be changed again. */
803 pthread_mutex_unlock(&u->buffersize_mutex);
804 }
805
806
807 static void* fill_ringbuffer(void* arg) {
808 struct userdata* u = arg;
809
810 assert(u);
811
812 while (!u->quit_requested) {
813 if (u->ringbuffer_is_full) {
814 pthread_mutex_lock(&u->cond_mutex);
815 pthread_cond_wait(&u->ringbuffer_cond,
816 &u->cond_mutex);
817 pthread_mutex_unlock(&u->cond_mutex);
818 }
819 /* No, it's not full yet, but this must be set to one as soon as
820 possible, because if the jack thread manages to process another
821 block before we set this to one, we may end up waiting without
822 a reason. */
823 u->ringbuffer_is_full = 1;
824
825 request_render(u);
826 }
827
828 return NULL;
829 }
830
831
832 static void request_render(struct userdata* u) {
833 char c = 'x';
834
835 assert(u);
836
837 assert(u->pipe_fds[1] >= 0);
838 pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type);
839 }
840
841 void pa__done(pa_core* c, pa_module* self) {
842 struct userdata* u;
843
844 assert(c);
845 assert(self);
846
847 if (!self->userdata)
848 return;
849
850 u = self->userdata;
851
852 if (u->filler_thread_is_running) {
853 u->quit_requested = 1;
854 pthread_cond_signal(&u->ringbuffer_cond);
855 pthread_join(u->filler_thread, NULL);
856 }
857
858 if (u->j_client)
859 jack_client_close(u->j_client);
860
861 if (u->io_event)
862 c->mainloop->io_free(u->io_event);
863
864 if (u->sink) {
865 pa_sink_disconnect(u->sink);
866 pa_sink_unref(u->sink);
867 }
868
869 if (u->ringbuffer)
870 jack_ringbuffer_free(u->ringbuffer);
871
872 if (u->pipe_fds[0] >= 0)
873 pa_close(u->pipe_fds[0]);
874 if (u->pipe_fds[1] >= 0)
875 pa_close(u->pipe_fds[1]);
876
877 pthread_mutex_destroy(&u->buffersize_mutex);
878 pthread_cond_destroy(&u->ringbuffer_cond);
879 pthread_mutex_destroy(&u->cond_mutex);
880 pa_xfree(self->userdata);
881 self->userdata = NULL;
882 }