From 4482e6867df82e889f48a9e7c22f2e0887b1028c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 17 Apr 2006 00:11:04 +0000 Subject: add new JACK sink git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@735 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 382 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 382 insertions(+) create mode 100644 src/modules/module-jack-sink.c (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c new file mode 100644 index 00000000..3d372551 --- /dev/null +++ b/src/modules/module-jack-sink.c @@ -0,0 +1,382 @@ +/* $Id$ */ + +/*** + This file is part of polypaudio. + + polypaudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + polypaudio is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with polypaudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "module-jack-sink-symdef.h" + +PA_MODULE_AUTHOR("Lennart Poettering") +PA_MODULE_DESCRIPTION("Jack Sink") +PA_MODULE_VERSION(PACKAGE_VERSION) +PA_MODULE_USAGE( + "sink_name= " + "server_name= " + "channels= " + "connect=" + "port_prefix=" +) + +#define DEFAULT_SINK_NAME "jack_out" + +struct userdata { + pa_core *core; + pa_module *module; + + pa_sink *sink; + + unsigned channels; + + jack_port_t* port[PA_CHANNELS_MAX]; + jack_client_t *client; + + pthread_mutex_t mutex; + pthread_cond_t cond; + + void * buffer[PA_CHANNELS_MAX]; + jack_nframes_t frames_requested; + int quit_requested; + + int pipe_fds[2]; + pa_io_event *io_event; + + jack_nframes_t frames_in_buffer; + struct timeval timestamp; +}; + +static const char* const valid_modargs[] = { + "sink_name", + "server_name", + "channels", + "connect", + "port_prefix", + NULL +}; + +static void stop_sink(struct userdata *u) { + assert (u); + + jack_client_close(u->client); + u->client = NULL; + u->core->mainloop->io_free(u->io_event); + u->io_event = NULL; + pa_sink_disconnect(u->sink); + pa_sink_unref(u->sink); + u->sink = NULL; + pa_module_unload_request(u->module); +} + +static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) { + struct userdata *u = userdata; + char x; + + assert(m); + assert(flags == PA_IO_EVENT_INPUT); + assert(u); + assert(u->pipe_fds[0] == fd); + + read(fd, &x, 1); + + if (u->quit_requested) { + stop_sink(u); + u->quit_requested = 0; + return; + } + + pthread_mutex_lock(&u->mutex); + + if (u->frames_requested > 0) { + unsigned fs; + jack_nframes_t frame_idx; + pa_memchunk chunk; + + fs = pa_frame_size(&u->sink->sample_spec); + + pa_sink_render_full(u->sink, u->frames_requested * fs, &chunk); + + for (frame_idx = 0; frame_idx < u->frames_requested; frame_idx ++) { + unsigned c; + + for (c = 0; c < u->channels; c++) { + float *s = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx * u->channels) + c; + float *d = ((float*) u->buffer[c]) + frame_idx; + + *d = *s; + } + } + + pa_memblock_unref(chunk.memblock); + + u->frames_requested = 0; + + pthread_cond_signal(&u->cond); + } + + pthread_mutex_unlock(&u->mutex); +} + +static void request_render(struct userdata *u) { + char c = 'x'; + assert(u); + + assert(u->pipe_fds[1] >= 0); + write(u->pipe_fds[1], &c, 1); +} + +static void jack_shutdown(void *arg) { + struct userdata *u = arg; + assert(u); + + u->quit_requested = 1; + request_render(u); +} + +static int jack_process(jack_nframes_t nframes, void *arg) { + struct userdata *u = arg; + assert(u); + + if (jack_transport_query(u->client, NULL) == JackTransportRolling) { + unsigned c; + + pthread_mutex_lock(&u->mutex); + + u->frames_requested = nframes; + + for (c = 0; c < u->channels; c++) { + u->buffer[c] = jack_port_get_buffer(u->port[c], nframes); + assert(u->buffer[c]); + } + + request_render(u); + + pthread_cond_wait(&u->cond, &u->mutex); + + u->frames_in_buffer = nframes; + pa_gettimeofday(&u->timestamp); + + pthread_mutex_unlock(&u->mutex); + } + + return 0; +} + +static pa_usec_t sink_get_latency_cb(pa_sink *s) { + struct userdata *u; + pa_usec_t t, delta; + assert(s); + u = s->userdata; + + if (jack_transport_query(u->client, NULL) != JackTransportRolling) + return 0; + + delta = pa_timeval_age(&u->timestamp); + t = pa_bytes_to_usec(jack_port_get_total_latency(u->client, u->port[0]) * pa_frame_size(&s->sample_spec), &s->sample_spec); + + if (t > delta) + return t - delta; + else + return 0; +} + +static void jack_error_func(const char*t) { + pa_log_warn(__FILE__": JACK error >%s<", t); +} + +int pa__init(pa_core *c, pa_module*m) { + struct userdata *u = NULL; + pa_sample_spec ss; + pa_modargs *ma = NULL; + jack_status_t status; + const char *server_name; + uint32_t channels; + int connect = 1; + const char *pfx; + unsigned i; + + assert(c); + assert(m); + + jack_set_error_function(jack_error_func); + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log(__FILE__": failed to parse module arguments."); + goto fail; + } + + channels = c->default_sample_spec.channels; + if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) { + pa_log(__FILE__": failed to parse channels= argument."); + goto fail; + } + + if (pa_modargs_get_value_boolean(ma, "connect", &connect) < 0) { + pa_log(__FILE__": failed to parse connect= argument."); + goto fail; + } + + server_name = pa_modargs_get_value(ma, "server_name", NULL); + + u = pa_xnew0(struct userdata, 1); + m->userdata = u; + u->core = c; + u->module = m; + u->pipe_fds[0] = u->pipe_fds[1] = -1; + + pthread_mutex_init(&u->mutex, NULL); + pthread_cond_init(&u->cond, NULL); + + if (pipe(u->pipe_fds) < 0) { + pa_log(__FILE__": pipe() failed: %s", strerror(errno)); + goto fail; + } + + pa_make_nonblock_fd(u->pipe_fds[1]); + + if (!(u->client = jack_client_open("polypaudio", server_name ? JackServerName : JackNullOption, &status, server_name))) { + pa_log(__FILE__": jack_client_open() failed."); + goto fail; + } + + pa_log_info(__FILE__": Successfully connected as '%s'", jack_get_client_name(u->client)); + + ss.channels = u->channels = channels; + ss.rate = jack_get_sample_rate(u->client); + ss.format = PA_SAMPLE_FLOAT32NE; + + assert(pa_sample_spec_valid(&ss)); + + pfx = pa_modargs_get_value(ma, "port_prefix", "channel"); + + for (i = 0; i < ss.channels; i++) { + char tmp[64]; + + snprintf(tmp, sizeof(tmp), "%s_%i", pfx, i+1); + + if (!(u->port[i] = jack_port_register(u->client, tmp, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput/*|JackPortIsTerminal*/, 0))) { + pa_log(__FILE__": jack_port_register() failed."); + goto fail; + } + } + + if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL))) { + pa_log(__FILE__": failed to create sink."); + goto fail; + } + + u->sink->userdata = u; + pa_sink_set_owner(u->sink, m); + u->sink->description = pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u->client)); + u->sink->get_latency = sink_get_latency_cb; + + jack_set_process_callback(u->client, jack_process, u); + jack_on_shutdown(u->client, jack_shutdown, u); + + if (jack_activate(u->client)) { + pa_log(__FILE__": jack_activate() failed"); + goto fail; + } + + if (connect) { + const char **p, **ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput); + + for (i = 0, p = ports; i < ss.channels; i++, p++) { + + if (!p) { + pa_log(__FILE__": not enough physical output ports, leaving unconnected."); + break; + } + + pa_log_info(__FILE__": connecting %s to %s", jack_port_name(u->port[i]), *p); + + if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) { + pa_log(__FILE__": failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p); + break; + } + } + + free(ports); + } + + u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u); + + pa_modargs_free(ma); + + return 0; + +fail: + if (ma) + pa_modargs_free(ma); + + pa__done(c, m); + + return -1; +} + +void pa__done(pa_core *c, pa_module*m) { + struct userdata *u; + assert(c && m); + + if (!(u = m->userdata)) + return; + + if (u->client) + jack_client_close(u->client); + + if (u->io_event) + c->mainloop->io_free(u->io_event); + + if (u->sink) { + pa_sink_disconnect(u->sink); + pa_sink_unref(u->sink); + } + + if (u->pipe_fds[0] >= 0) + close(u->pipe_fds[0]); + if (u->pipe_fds[1] >= 0) + close(u->pipe_fds[1]); + + pthread_mutex_destroy(&u->mutex); + pthread_cond_destroy(&u->cond); + pa_xfree(u); +} -- cgit From cf85794e2349631f6d997592bceca2174c044f0c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 18 Apr 2006 13:20:50 +0000 Subject: * allow the user to set the jack client name * take the number of channels for the sink from the number of physical ports in the jack server * name the polypaudio ports in the jack server after their channel position in polypaudio git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@737 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 76 ++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 32 deletions(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 3d372551..4f7a66eb 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -52,10 +52,10 @@ PA_MODULE_DESCRIPTION("Jack Sink") PA_MODULE_VERSION(PACKAGE_VERSION) PA_MODULE_USAGE( "sink_name= " - "server_name= " + "server_name= " + "client_name= " "channels= " "connect=" - "port_prefix=" ) #define DEFAULT_SINK_NAME "jack_out" @@ -82,15 +82,15 @@ struct userdata { pa_io_event *io_event; jack_nframes_t frames_in_buffer; - struct timeval timestamp; + jack_nframes_t timestamp; }; static const char* const valid_modargs[] = { "sink_name", "server_name", + "client_name", "channels", "connect", - "port_prefix", NULL }; @@ -193,7 +193,7 @@ static int jack_process(jack_nframes_t nframes, void *arg) { pthread_cond_wait(&u->cond, &u->mutex); u->frames_in_buffer = nframes; - pa_gettimeofday(&u->timestamp); + u->timestamp = jack_get_current_transport_frame(u->client); pthread_mutex_unlock(&u->mutex); } @@ -203,20 +203,26 @@ static int jack_process(jack_nframes_t nframes, void *arg) { static pa_usec_t sink_get_latency_cb(pa_sink *s) { struct userdata *u; - pa_usec_t t, delta; + jack_nframes_t n, l, d; + assert(s); u = s->userdata; if (jack_transport_query(u->client, NULL) != JackTransportRolling) return 0; - delta = pa_timeval_age(&u->timestamp); - t = pa_bytes_to_usec(jack_port_get_total_latency(u->client, u->port[0]) * pa_frame_size(&s->sample_spec), &s->sample_spec); + n = jack_get_current_transport_frame(u->client); + + if (n < u->timestamp) + return 0; + + d = n - u->timestamp; + l = jack_port_get_total_latency(u->client, u->port[0]) + u->frames_in_buffer; - if (t > delta) - return t - delta; - else + if (d >= l) return 0; + + return pa_bytes_to_usec((l - d) * pa_frame_size(&s->sample_spec), &s->sample_spec); } static void jack_error_func(const char*t) { @@ -226,13 +232,14 @@ static void jack_error_func(const char*t) { int pa__init(pa_core *c, pa_module*m) { struct userdata *u = NULL; pa_sample_spec ss; + pa_channel_map cm; pa_modargs *ma = NULL; jack_status_t status; - const char *server_name; - uint32_t channels; + const char *server_name, *client_name; + uint32_t channels = 0; int connect = 1; - const char *pfx; unsigned i; + const char **ports = NULL, **p; assert(c); assert(m); @@ -244,18 +251,13 @@ int pa__init(pa_core *c, pa_module*m) { goto fail; } - channels = c->default_sample_spec.channels; - if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) { - pa_log(__FILE__": failed to parse channels= argument."); - goto fail; - } - if (pa_modargs_get_value_boolean(ma, "connect", &connect) < 0) { pa_log(__FILE__": failed to parse connect= argument."); goto fail; } server_name = pa_modargs_get_value(ma, "server_name", NULL); + client_name = pa_modargs_get_value(ma, "client_name", "polypaudio"); u = pa_xnew0(struct userdata, 1); m->userdata = u; @@ -273,11 +275,25 @@ int pa__init(pa_core *c, pa_module*m) { pa_make_nonblock_fd(u->pipe_fds[1]); - if (!(u->client = jack_client_open("polypaudio", server_name ? JackServerName : JackNullOption, &status, server_name))) { + if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) { pa_log(__FILE__": jack_client_open() failed."); goto fail; } + ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput); + + channels = 0; + for (p = ports; *p; p++) + channels++; + + if (!channels) + channels = c->default_sample_spec.channels; + + if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) { + pa_log(__FILE__": failed to parse channels= argument."); + goto fail; + } + pa_log_info(__FILE__": Successfully connected as '%s'", jack_get_client_name(u->client)); ss.channels = u->channels = channels; @@ -286,20 +302,16 @@ int pa__init(pa_core *c, pa_module*m) { assert(pa_sample_spec_valid(&ss)); - pfx = pa_modargs_get_value(ma, "port_prefix", "channel"); + pa_channel_map_init_auto(&cm, channels); for (i = 0; i < ss.channels; i++) { - char tmp[64]; - - snprintf(tmp, sizeof(tmp), "%s_%i", pfx, i+1); - - if (!(u->port[i] = jack_port_register(u->client, tmp, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput/*|JackPortIsTerminal*/, 0))) { + if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(cm.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) { pa_log(__FILE__": jack_port_register() failed."); goto fail; } } - if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL))) { + if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &cm))) { pa_log(__FILE__": failed to create sink."); goto fail; } @@ -318,11 +330,9 @@ int pa__init(pa_core *c, pa_module*m) { } if (connect) { - const char **p, **ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput); - for (i = 0, p = ports; i < ss.channels; i++, p++) { - if (!p) { + if (!*p) { pa_log(__FILE__": not enough physical output ports, leaving unconnected."); break; } @@ -335,11 +345,11 @@ int pa__init(pa_core *c, pa_module*m) { } } - free(ports); } u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u); + free(ports); pa_modargs_free(ma); return 0; @@ -347,6 +357,8 @@ int pa__init(pa_core *c, pa_module*m) { fail: if (ma) pa_modargs_free(ma); + + free(ports); pa__done(c, m); -- cgit From 746adcfed5dc396bc4820724b2e951369fc63aeb Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 18 Apr 2006 19:31:50 +0000 Subject: fix a couple of issues I found when compiling polypaudio with gcc 2.95 git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@754 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 4f7a66eb..f340ab6d 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -112,6 +112,7 @@ static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_ char x; assert(m); + assert(e); assert(flags == PA_IO_EVENT_INPUT); assert(u); assert(u->pipe_fds[0] == fd); -- cgit From 185a57cadd7b4e8e85c7fbecc866d7c279704e12 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 26 Apr 2006 15:40:14 +0000 Subject: support new channel_map argument in sink/source modules git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@803 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index f340ab6d..09030530 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -55,8 +55,8 @@ PA_MODULE_USAGE( "server_name= " "client_name= " "channels= " - "connect=" -) + "connect= " + "channel_map=") #define DEFAULT_SINK_NAME "jack_out" @@ -91,6 +91,7 @@ static const char* const valid_modargs[] = { "client_name", "channels", "connect", + "channel_map", NULL }; @@ -233,7 +234,7 @@ static void jack_error_func(const char*t) { int pa__init(pa_core *c, pa_module*m) { struct userdata *u = NULL; pa_sample_spec ss; - pa_channel_map cm; + pa_channel_map map; pa_modargs *ma = NULL; jack_status_t status; const char *server_name, *client_name; @@ -294,6 +295,12 @@ int pa__init(pa_core *c, pa_module*m) { pa_log(__FILE__": failed to parse channels= argument."); goto fail; } + + pa_channel_map_init_auto(&map, channels); + if (pa_modargs_get_channel_map(ma, &map) < 0 || map.channels != channels) { + pa_log(__FILE__": failed to parse channel_map= argument."); + goto fail; + } pa_log_info(__FILE__": Successfully connected as '%s'", jack_get_client_name(u->client)); @@ -303,16 +310,14 @@ int pa__init(pa_core *c, pa_module*m) { assert(pa_sample_spec_valid(&ss)); - pa_channel_map_init_auto(&cm, channels); - for (i = 0; i < ss.channels; i++) { - if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(cm.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) { + if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) { pa_log(__FILE__": jack_port_register() failed."); goto fail; } } - if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &cm))) { + if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) { pa_log(__FILE__": failed to create sink."); goto fail; } -- cgit From 4b6ab291a787ff597c938842b569a35434ab11d8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 16 May 2006 23:47:38 +0000 Subject: * modify pa_channel_map_init_auto() to take an extra argument specifying the standard to use (ALSA, AIFF, ...) * add some more validity checks to pa_source_new(),pa_sink_new(),pa_sink_input_new(),pa_source_output_new() git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@888 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 09030530..324a2cb3 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -296,7 +296,7 @@ int pa__init(pa_core *c, pa_module*m) { goto fail; } - pa_channel_map_init_auto(&map, channels); + pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA); if (pa_modargs_get_channel_map(ma, &map) < 0 || map.channels != channels) { pa_log(__FILE__": failed to parse channel_map= argument."); goto fail; -- cgit From d9cc2cfcb97c1b0449bcbfb6ab0301a58d77bd55 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Wed, 17 May 2006 16:34:18 +0000 Subject: Move xmalloc to the public side (libpolyp). git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@908 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 324a2cb3..1aa73495 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -36,12 +36,13 @@ #include +#include + #include #include #include #include #include -#include #include #include -- cgit From c47e937011f00eebab7230cedb58fd59f16487b4 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 17 May 2006 20:09:57 +0000 Subject: split polypcore/util.[ch] into polypcore/core-util.[ch] and polyp/util.[ch] git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@917 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 1aa73495..96db837a 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include #include #include -- cgit From 4e3dc7ce68561c16254712d713b2ccd472b8afe7 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Mon, 22 May 2006 15:20:46 +0000 Subject: Wrap strerror() in a function that makes it thread safe and converts the output to UTF-8. git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@945 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 96db837a..db2ba030 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -36,6 +36,7 @@ #include +#include #include #include @@ -272,7 +273,7 @@ int pa__init(pa_core *c, pa_module*m) { pthread_cond_init(&u->cond, NULL); if (pipe(u->pipe_fds) < 0) { - pa_log(__FILE__": pipe() failed: %s", strerror(errno)); + pa_log(__FILE__": pipe() failed: %s", pa_cstrerror(errno)); goto fail; } -- cgit From 4413b89d7a45587b545a31463ad2196767f45563 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 25 May 2006 17:16:55 +0000 Subject: * split pa_cstrerror() into its own file polypcore/core-error.[ch] * fix building of padsp * remove a warning when compiling padsp.c git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@972 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index db2ba030..fc2bfc45 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -36,9 +36,9 @@ #include -#include #include +#include #include #include #include -- cgit From f44ba092651aa75055e109e04b4164ea92ae7fdc Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 19 Jun 2006 21:53:48 +0000 Subject: big s/polyp/pulse/g git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1033 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index fc2bfc45..c6a161ff 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -1,20 +1,20 @@ /* $Id$ */ /*** - This file is part of polypaudio. + This file is part of PulseAudio. - polypaudio is free software; you can redistribute it and/or modify + PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - polypaudio is distributed in the hope that it will be useful, but + PulseAudio is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License - along with polypaudio; if not, write to the Free Software + along with PulseAudio; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. ***/ @@ -36,16 +36,16 @@ #include -#include +#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include #include "module-jack-sink-symdef.h" @@ -261,7 +261,7 @@ int pa__init(pa_core *c, pa_module*m) { } server_name = pa_modargs_get_value(ma, "server_name", NULL); - client_name = pa_modargs_get_value(ma, "client_name", "polypaudio"); + client_name = pa_modargs_get_value(ma, "client_name", "pulseaudio"); u = pa_xnew0(struct userdata, 1); m->userdata = u; -- cgit From 3cf16214334b4a1c51e56b0536abd8223d6813dd Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 19 Jun 2006 23:51:58 +0000 Subject: * more s/pulseaudio/PulseAudio/ replacements * name the per-user dir ~/.pulse (instead of .pulseaudio), just like /etc/pulse/ git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1039 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index c6a161ff..74f25d53 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -261,7 +261,7 @@ int pa__init(pa_core *c, pa_module*m) { } server_name = pa_modargs_get_value(ma, "server_name", NULL); - client_name = pa_modargs_get_value(ma, "client_name", "pulseaudio"); + client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio"); u = pa_xnew0(struct userdata, 1); m->userdata = u; -- cgit From 860be2e70b33ff5eeb9130f80c4b1c096a2a8f27 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 14 Jul 2006 22:42:01 +0000 Subject: try to use send(,,MSG_NOSIGNAL) instead of write() wherever possible (which will allow us to drop the SIGPIPE check). Cache the results of the last write()/send() to make sure that we do not issue more than necessary system calls. git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1083 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 74f25d53..d761c1f7 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -80,6 +80,7 @@ struct userdata { jack_nframes_t frames_requested; int quit_requested; + int pipe_fd_type; int pipe_fds[2]; pa_io_event *io_event; @@ -120,7 +121,7 @@ static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_ assert(u); assert(u->pipe_fds[0] == fd); - read(fd, &x, 1); + pa_read(fd, &x, 1, &u->pipe_fd_type); if (u->quit_requested) { stop_sink(u); @@ -165,7 +166,7 @@ static void request_render(struct userdata *u) { assert(u); assert(u->pipe_fds[1] >= 0); - write(u->pipe_fds[1], &c, 1); + pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type); } static void jack_shutdown(void *arg) { @@ -268,6 +269,7 @@ int pa__init(pa_core *c, pa_module*m) { u->core = c; u->module = m; u->pipe_fds[0] = u->pipe_fds[1] = -1; + u->pipe_fd_type = 0; pthread_mutex_init(&u->mutex, NULL); pthread_cond_init(&u->cond, NULL); -- cgit From a382492204ad3588c0c837e120e5bc31578df72a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 19 Jul 2006 21:48:35 +0000 Subject: * add new function pa_check_in_group() * abstract credential APis a little bit by introducing HAVE_CREDS and a structure pa_creds * rework credential authentication * fix module-volume-restore and friends for usage in system-wide instance * remove loopback= argument from moulde-*-protocol-tcp since it is a superset of listen= and usually a bad idea anyway since the user shouldn't load the TCP module at all if he doesn't want remote access * rename a few variables in the jack modules to make sure they don't conflict with symbols defined in the system headers * add server address for system-wide daemons to the default server list for the the client libs * update todo git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1109 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index d761c1f7..c645caa9 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -242,7 +242,7 @@ int pa__init(pa_core *c, pa_module*m) { jack_status_t status; const char *server_name, *client_name; uint32_t channels = 0; - int connect = 1; + int do_connect = 1; unsigned i; const char **ports = NULL, **p; @@ -256,7 +256,7 @@ int pa__init(pa_core *c, pa_module*m) { goto fail; } - if (pa_modargs_get_value_boolean(ma, "connect", &connect) < 0) { + if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) { pa_log(__FILE__": failed to parse connect= argument."); goto fail; } @@ -339,7 +339,7 @@ int pa__init(pa_core *c, pa_module*m) { goto fail; } - if (connect) { + if (do_connect) { for (i = 0, p = ports; i < ss.channels; i++, p++) { if (!*p) { -- cgit From bfa6604b1ddc5e2c0f1aaa15330363724856359b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 11 Aug 2006 23:58:55 +0000 Subject: don't set the sink/source descriptions manually, use the new functions pa_{sink,source}_set_description() instead git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1205 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index c645caa9..286f6f57 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -245,6 +245,7 @@ int pa__init(pa_core *c, pa_module*m) { int do_connect = 1; unsigned i; const char **ports = NULL, **p; + char *t; assert(c); assert(m); @@ -328,7 +329,8 @@ int pa__init(pa_core *c, pa_module*m) { u->sink->userdata = u; pa_sink_set_owner(u->sink, m); - u->sink->description = pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u->client)); + pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u->client))); + pa_xfree(t); u->sink->get_latency = sink_get_latency_cb; jack_set_process_callback(u->client, jack_process, u); -- cgit From e385d93e5aad6a6fce754c00c804ff1d6a6746d4 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 18 Aug 2006 21:38:40 +0000 Subject: remove all occurences of pa_logXXX(__FILE__": and replace them by pa_logXXX(" git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1272 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 286f6f57..47f77bab 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -231,7 +231,7 @@ static pa_usec_t sink_get_latency_cb(pa_sink *s) { } static void jack_error_func(const char*t) { - pa_log_warn(__FILE__": JACK error >%s<", t); + pa_log_warn("JACK error >%s<", t); } int pa__init(pa_core *c, pa_module*m) { @@ -253,12 +253,12 @@ int pa__init(pa_core *c, pa_module*m) { jack_set_error_function(jack_error_func); if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { - pa_log(__FILE__": failed to parse module arguments."); + pa_log("failed to parse module arguments."); goto fail; } if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) { - pa_log(__FILE__": failed to parse connect= argument."); + pa_log("failed to parse connect= argument."); goto fail; } @@ -276,14 +276,14 @@ int pa__init(pa_core *c, pa_module*m) { pthread_cond_init(&u->cond, NULL); if (pipe(u->pipe_fds) < 0) { - pa_log(__FILE__": pipe() failed: %s", pa_cstrerror(errno)); + pa_log("pipe() failed: %s", pa_cstrerror(errno)); goto fail; } pa_make_nonblock_fd(u->pipe_fds[1]); if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) { - pa_log(__FILE__": jack_client_open() failed."); + pa_log("jack_client_open() failed."); goto fail; } @@ -297,17 +297,17 @@ int pa__init(pa_core *c, pa_module*m) { channels = c->default_sample_spec.channels; if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) { - pa_log(__FILE__": failed to parse channels= argument."); + pa_log("failed to parse channels= argument."); goto fail; } pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA); if (pa_modargs_get_channel_map(ma, &map) < 0 || map.channels != channels) { - pa_log(__FILE__": failed to parse channel_map= argument."); + pa_log("failed to parse channel_map= argument."); goto fail; } - pa_log_info(__FILE__": Successfully connected as '%s'", jack_get_client_name(u->client)); + pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client)); ss.channels = u->channels = channels; ss.rate = jack_get_sample_rate(u->client); @@ -317,13 +317,13 @@ int pa__init(pa_core *c, pa_module*m) { for (i = 0; i < ss.channels; i++) { if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) { - pa_log(__FILE__": jack_port_register() failed."); + pa_log("jack_port_register() failed."); goto fail; } } if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) { - pa_log(__FILE__": failed to create sink."); + pa_log("failed to create sink."); goto fail; } @@ -337,7 +337,7 @@ int pa__init(pa_core *c, pa_module*m) { jack_on_shutdown(u->client, jack_shutdown, u); if (jack_activate(u->client)) { - pa_log(__FILE__": jack_activate() failed"); + pa_log("jack_activate() failed"); goto fail; } @@ -345,14 +345,14 @@ int pa__init(pa_core *c, pa_module*m) { for (i = 0, p = ports; i < ss.channels; i++, p++) { if (!*p) { - pa_log(__FILE__": not enough physical output ports, leaving unconnected."); + pa_log("not enough physical output ports, leaving unconnected."); break; } - pa_log_info(__FILE__": connecting %s to %s", jack_port_name(u->port[i]), *p); + pa_log_info("connecting %s to %s", jack_port_name(u->port[i]), *p); if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) { - pa_log(__FILE__": failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p); + pa_log("failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p); break; } } -- cgit From d210ebbb09daddb2c8c8e8e77243e088b0b19c4d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 26 Sep 2006 23:50:56 +0000 Subject: rework memory block management to be thread-safe and mostly lock-free. pa_memblock is now an opaque structure. Access to its fields is now done through various accessor functions in a thread-safe manner. pa_memblock_acquire() and pa_memblock_release() are now used to access the attached audio data. Why? To allow safe manipulation of the memory pointer maintained by the memory block. Internally _acquire() and _release() maintain a reference counter. Please do not confuse this reference counter whith the one maintained by pa_memblock_ref()/_unref()! As a side effect this patch removes all direct usages of AO_t and replaces it with pa_atomic_xxx based code. This stuff needs some serious testing love. Especially if threads are actively used. git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1404 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 47f77bab..66ded27f 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -135,22 +135,25 @@ static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_ unsigned fs; jack_nframes_t frame_idx; pa_memchunk chunk; + void *p; fs = pa_frame_size(&u->sink->sample_spec); pa_sink_render_full(u->sink, u->frames_requested * fs, &chunk); + p = pa_memblock_acquire(chunk.memblock); for (frame_idx = 0; frame_idx < u->frames_requested; frame_idx ++) { unsigned c; for (c = 0; c < u->channels; c++) { - float *s = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx * u->channels) + c; + float *s = ((float*) ((uint8_t*) p + chunk.index)) + (frame_idx * u->channels) + c; float *d = ((float*) u->buffer[c]) + frame_idx; *d = *s; } } + pa_memblock_release(chunk.memblock); pa_memblock_unref(chunk.memblock); u->frames_requested = 0; -- cgit From 8dc62142765249addf131b058c27f931ede1776b Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Mon, 6 Nov 2006 13:06:01 +0000 Subject: Revert r1404 and keep it on a development branch until it is fully tested. git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1409 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 66ded27f..47f77bab 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -135,25 +135,22 @@ static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_ unsigned fs; jack_nframes_t frame_idx; pa_memchunk chunk; - void *p; fs = pa_frame_size(&u->sink->sample_spec); pa_sink_render_full(u->sink, u->frames_requested * fs, &chunk); - p = pa_memblock_acquire(chunk.memblock); for (frame_idx = 0; frame_idx < u->frames_requested; frame_idx ++) { unsigned c; for (c = 0; c < u->channels; c++) { - float *s = ((float*) ((uint8_t*) p + chunk.index)) + (frame_idx * u->channels) + c; + float *s = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx * u->channels) + c; float *d = ((float*) u->buffer[c]) + frame_idx; *d = *s; } } - pa_memblock_release(chunk.memblock); pa_memblock_unref(chunk.memblock); u->frames_requested = 0; -- cgit From 521daf6f0ac4fa6a2fbfb5d523c0c743342dca2b Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Thu, 4 Jan 2007 13:43:45 +0000 Subject: Huge trailing whitespace cleanup. Let's keep the tree pure from here on, mmmkay? git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1418 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 70 +++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 35 deletions(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 47f77bab..6175536c 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -2,17 +2,17 @@ /*** This file is part of PulseAudio. - + PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + PulseAudio is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU Lesser General Public License along with PulseAudio; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 @@ -75,7 +75,7 @@ struct userdata { pthread_mutex_t mutex; pthread_cond_t cond; - + void * buffer[PA_CHANNELS_MAX]; jack_nframes_t frames_requested; int quit_requested; @@ -100,7 +100,7 @@ static const char* const valid_modargs[] = { static void stop_sink(struct userdata *u) { assert (u); - + jack_client_close(u->client); u->client = NULL; u->core->mainloop->io_free(u->io_event); @@ -114,7 +114,7 @@ static void stop_sink(struct userdata *u) { static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) { struct userdata *u = userdata; char x; - + assert(m); assert(e); assert(flags == PA_IO_EVENT_INPUT); @@ -122,39 +122,39 @@ static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_ assert(u->pipe_fds[0] == fd); pa_read(fd, &x, 1, &u->pipe_fd_type); - + if (u->quit_requested) { stop_sink(u); u->quit_requested = 0; return; } - + pthread_mutex_lock(&u->mutex); if (u->frames_requested > 0) { unsigned fs; jack_nframes_t frame_idx; pa_memchunk chunk; - + fs = pa_frame_size(&u->sink->sample_spec); pa_sink_render_full(u->sink, u->frames_requested * fs, &chunk); for (frame_idx = 0; frame_idx < u->frames_requested; frame_idx ++) { unsigned c; - + for (c = 0; c < u->channels; c++) { float *s = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx * u->channels) + c; float *d = ((float*) u->buffer[c]) + frame_idx; - + *d = *s; } } - + pa_memblock_unref(chunk.memblock); u->frames_requested = 0; - + pthread_cond_signal(&u->cond); } @@ -183,36 +183,36 @@ static int jack_process(jack_nframes_t nframes, void *arg) { if (jack_transport_query(u->client, NULL) == JackTransportRolling) { unsigned c; - + pthread_mutex_lock(&u->mutex); - + u->frames_requested = nframes; - + for (c = 0; c < u->channels; c++) { u->buffer[c] = jack_port_get_buffer(u->port[c], nframes); assert(u->buffer[c]); } - + request_render(u); - + pthread_cond_wait(&u->cond, &u->mutex); u->frames_in_buffer = nframes; u->timestamp = jack_get_current_transport_frame(u->client); - + pthread_mutex_unlock(&u->mutex); } - + return 0; } static pa_usec_t sink_get_latency_cb(pa_sink *s) { struct userdata *u; jack_nframes_t n, l, d; - + assert(s); u = s->userdata; - + if (jack_transport_query(u->client, NULL) != JackTransportRolling) return 0; @@ -226,7 +226,7 @@ static pa_usec_t sink_get_latency_cb(pa_sink *s) { if (d >= l) return 0; - + return pa_bytes_to_usec((l - d) * pa_frame_size(&s->sample_spec), &s->sample_spec); } @@ -246,12 +246,12 @@ int pa__init(pa_core *c, pa_module*m) { unsigned i; const char **ports = NULL, **p; char *t; - + assert(c); assert(m); jack_set_error_function(jack_error_func); - + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { pa_log("failed to parse module arguments."); goto fail; @@ -261,7 +261,7 @@ int pa__init(pa_core *c, pa_module*m) { pa_log("failed to parse connect= argument."); goto fail; } - + server_name = pa_modargs_get_value(ma, "server_name", NULL); client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio"); @@ -274,28 +274,28 @@ int pa__init(pa_core *c, pa_module*m) { pthread_mutex_init(&u->mutex, NULL); pthread_cond_init(&u->cond, NULL); - + if (pipe(u->pipe_fds) < 0) { pa_log("pipe() failed: %s", pa_cstrerror(errno)); goto fail; } pa_make_nonblock_fd(u->pipe_fds[1]); - + if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) { pa_log("jack_client_open() failed."); goto fail; } ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput); - + channels = 0; for (p = ports; *p; p++) channels++; if (!channels) channels = c->default_sample_spec.channels; - + if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) { pa_log("failed to parse channels= argument."); goto fail; @@ -306,7 +306,7 @@ int pa__init(pa_core *c, pa_module*m) { pa_log("failed to parse channel_map= argument."); goto fail; } - + pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client)); ss.channels = u->channels = channels; @@ -350,7 +350,7 @@ int pa__init(pa_core *c, pa_module*m) { } pa_log_info("connecting %s to %s", jack_port_name(u->port[i]), *p); - + if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) { pa_log("failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p); break; @@ -360,10 +360,10 @@ int pa__init(pa_core *c, pa_module*m) { } u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u); - + free(ports); pa_modargs_free(ma); - + return 0; fail: @@ -371,7 +371,7 @@ fail: pa_modargs_free(ma); free(ports); - + pa__done(c, m); return -1; -- cgit From 06211b7c8fd329137ae9003818543912a87d9898 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Tue, 13 Feb 2007 15:35:19 +0000 Subject: Add copyright notices to all relevant files. (based on svn log) git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1426 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 6175536c..c6a7e33f 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -3,6 +3,8 @@ /*** This file is part of PulseAudio. + Copyright 2006 Lennart Poettering + PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, -- cgit From eaafb79858fff11add29c8a1126a18e90d7785f9 Mon Sep 17 00:00:00 2001 From: Tanu Kaskinen Date: Mon, 20 Aug 2007 06:22:21 +0000 Subject: Modified the JACK sink heavily: * Made the sink realtime-safe. * To achieve the previous item, internal buffering was be added. New module argument: buffersize. * Removed the user's need to set the JACK transport to playing state before he could hear anything from PulseAudio. * In process of achieving the previous item, latency calculation got more inaccurate: the reported latency is now always a multiple of the JACK processing block size, and constant. * The JACK ports now have a running numbering in their names. git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1680 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 957 ++++++++++++++++++++++++++++++----------- 1 file changed, 716 insertions(+), 241 deletions(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index c6a7e33f..09a72e3a 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -3,7 +3,7 @@ /*** This file is part of PulseAudio. - Copyright 2006 Lennart Poettering + Copyright 2006, 2007 Lennart Poettering and Tanu Kaskinen PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published @@ -25,33 +25,32 @@ #include #endif -#include -#include -#include +#include #include #include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include #include -#include -#include #include #include +#include +#include #include +#include +#include #include -#include -#include +#include #include "module-jack-sink-symdef.h" -PA_MODULE_AUTHOR("Lennart Poettering") +PA_MODULE_AUTHOR("Lennart Poettering & Tanu Kaskinen") PA_MODULE_DESCRIPTION("Jack Sink") PA_MODULE_VERSION(PACKAGE_VERSION) PA_MODULE_USAGE( @@ -59,335 +58,806 @@ PA_MODULE_USAGE( "server_name= " "client_name= " "channels= " - "connect= " + "connect= " + "buffersize= " "channel_map=") #define DEFAULT_SINK_NAME "jack_out" +#define DEFAULT_CLIENT_NAME "PulseAudio(output)" +#define DEFAULT_RINGBUFFER_SIZE 4096 -struct userdata { - pa_core *core; - pa_module *module; +struct userdata { pa_sink *sink; unsigned channels; + unsigned frame_size; + + jack_port_t* j_ports[PA_CHANNELS_MAX]; + jack_client_t *j_client; - jack_port_t* port[PA_CHANNELS_MAX]; - jack_client_t *client; + jack_nframes_t j_buffersize; - pthread_mutex_t mutex; - pthread_cond_t cond; + /* For avoiding j_buffersize changes at a wrong moment. */ + pthread_mutex_t buffersize_mutex; - void * buffer[PA_CHANNELS_MAX]; - jack_nframes_t frames_requested; + /* The intermediate store where the pulse side writes to and the jack side + reads from. */ + jack_ringbuffer_t* ringbuffer; + + /* For signaling when there's room in the ringbuffer. */ + pthread_mutex_t cond_mutex; + pthread_cond_t ringbuffer_cond; + + pthread_t filler_thread; /* Keeps the ringbuffer filled. */ + + int ringbuffer_is_full; + int filler_thread_is_running; int quit_requested; int pipe_fd_type; int pipe_fds[2]; pa_io_event *io_event; +}; - jack_nframes_t frames_in_buffer; - jack_nframes_t timestamp; + +struct options { + char* sink_name; + int sink_name_given; + + char* server_name; /* May be NULL */ + int server_name_given; + + char* client_name; + int client_name_given; + + unsigned channels; + int channels_given; + + int connect; + int connect_given; + + unsigned buffersize; + int buffersize_given; + + pa_channel_map map; + int map_given; }; + static const char* const valid_modargs[] = { "sink_name", "server_name", "client_name", "channels", "connect", + "buffersize", "channel_map", NULL }; -static void stop_sink(struct userdata *u) { - assert (u); - - jack_client_close(u->client); - u->client = NULL; - u->core->mainloop->io_free(u->io_event); - u->io_event = NULL; - pa_sink_disconnect(u->sink); - pa_sink_unref(u->sink); - u->sink = NULL; - pa_module_unload_request(u->module); -} - -static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) { - struct userdata *u = userdata; - char x; - - assert(m); - assert(e); - assert(flags == PA_IO_EVENT_INPUT); - assert(u); - assert(u->pipe_fds[0] == fd); - - pa_read(fd, &x, 1, &u->pipe_fd_type); - if (u->quit_requested) { - stop_sink(u); - u->quit_requested = 0; - return; - } - - pthread_mutex_lock(&u->mutex); +/* Initialization functions. */ +static int parse_options(struct options* o, const char* argument); +static void set_default_channels(pa_module* self, struct options* o); +static int create_sink(pa_module* self, struct options *o); +static void connect_ports(pa_module* self); +static int start_filling_ringbuffer(pa_module* self); - if (u->frames_requested > 0) { - unsigned fs; - jack_nframes_t frame_idx; - pa_memchunk chunk; +/* Various callbacks. */ +static void jack_error_func(const char* t); +static pa_usec_t sink_get_latency_cb(pa_sink* s); +static int jack_process(jack_nframes_t nframes, void* arg); +static int jack_blocksize_cb(jack_nframes_t nframes, void* arg); +static void jack_shutdown(void* arg); +static void io_event_cb(pa_mainloop_api* m, pa_io_event* e, int fd, + pa_io_event_flags_t flags, void* userdata); - fs = pa_frame_size(&u->sink->sample_spec); +/* The ringbuffer filler thread runs in this function. */ +static void* fill_ringbuffer(void* arg); - pa_sink_render_full(u->sink, u->frames_requested * fs, &chunk); +/* request_render asks asynchronously the mainloop to call io_event_cb. */ +static void request_render(struct userdata* u); - for (frame_idx = 0; frame_idx < u->frames_requested; frame_idx ++) { - unsigned c; - for (c = 0; c < u->channels; c++) { - float *s = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx * u->channels) + c; - float *d = ((float*) u->buffer[c]) + frame_idx; - - *d = *s; - } +int pa__init(pa_core* c, pa_module* self) { + struct userdata* u = NULL; + struct options o; + unsigned i; + + assert(c); + assert(self); + + o.sink_name = NULL; + o.server_name = NULL; + o.client_name = NULL; + + self->userdata = pa_xnew0(struct userdata, 1); + u = self->userdata; + + u->pipe_fds[0] = u->pipe_fds[1] = -1; + u->pipe_fd_type = 0; + u->ringbuffer_is_full = 0; + u->filler_thread_is_running = 0; + u->quit_requested = 0; + pthread_mutex_init(&u->buffersize_mutex, NULL); + pthread_mutex_init(&u->cond_mutex, NULL); + pthread_cond_init(&u->ringbuffer_cond, NULL); + + if (parse_options(&o, self->argument) != 0) + goto fail; + + jack_set_error_function(jack_error_func); + + if (!(u->j_client = jack_client_open( + o.client_name, + o.server_name ? JackServerName : JackNullOption, + NULL, o.server_name))) { + pa_log_error("jack_client_open() failed."); + goto fail; + } + pa_log_info("Successfully connected as '%s'", + jack_get_client_name(u->j_client)); + + if (!o.channels_given) + set_default_channels(self, &o); + + u->channels = o.channels; + + if (!o.map_given) + pa_channel_map_init_auto(&o.map, u->channels, PA_CHANNEL_MAP_ALSA); + + for (i = 0; i < u->channels; i++) { + char* port_name = pa_sprintf_malloc( + "out_%i:%s", i+1, + pa_channel_position_to_string(o.map.map[i])); + + if (!(u->j_ports[i] = jack_port_register( + u->j_client, port_name, + JACK_DEFAULT_AUDIO_TYPE, + JackPortIsOutput|JackPortIsTerminal, 0))) { + pa_log("jack_port_register() failed."); + goto fail; } + + pa_xfree(port_name); + } + + if (pipe(u->pipe_fds) < 0) { + pa_log("pipe() failed: %s", pa_cstrerror(errno)); + goto fail; + } + pa_make_nonblock_fd(u->pipe_fds[1]); + + if (create_sink(self, &o) != 0) + goto fail; - pa_memblock_unref(chunk.memblock); - - u->frames_requested = 0; - - pthread_cond_signal(&u->cond); + u->frame_size = pa_frame_size(&u->sink->sample_spec); + u->j_buffersize = jack_get_buffer_size(u->j_client); + + /* If the ringbuffer size were equal to the jack buffer size, a full block + would never fit in the ringbuffer, because the ringbuffer can never be + totally full: one slot is always wasted. */ + if (o.buffersize <= u->j_buffersize) { + o.buffersize = u->j_buffersize + 1; + } + /* The actual ringbuffer size will be rounded up to the nearest power of + two. */ + if (!(u->ringbuffer = jack_ringbuffer_create( + o.buffersize * u->frame_size))) { + pa_log("jack_ringbuffer_create() failed."); + goto fail; + } + assert((u->ringbuffer->size % sizeof(float)) == 0); + pa_log_info("buffersize is %u frames (%u samples, %u bytes).", + u->ringbuffer->size / u->frame_size, + u->ringbuffer->size / sizeof(float), + u->ringbuffer->size); + + jack_set_process_callback(u->j_client, jack_process, u); + jack_set_buffer_size_callback(u->j_client, jack_blocksize_cb, u); + jack_on_shutdown(u->j_client, jack_shutdown, u); + + if (jack_activate(u->j_client)) { + pa_log("jack_activate() failed."); + goto fail; } - pthread_mutex_unlock(&u->mutex); -} + if (o.connect) + connect_ports(self); -static void request_render(struct userdata *u) { - char c = 'x'; - assert(u); + u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], + PA_IO_EVENT_INPUT, io_event_cb, self); + + if (start_filling_ringbuffer(self) != 0) + goto fail; - assert(u->pipe_fds[1] >= 0); - pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type); -} + pa_xfree(o.sink_name); + pa_xfree(o.server_name); + pa_xfree(o.client_name); + + return 0; -static void jack_shutdown(void *arg) { - struct userdata *u = arg; - assert(u); +fail: + pa_xfree(o.sink_name); + pa_xfree(o.server_name); + pa_xfree(o.client_name); + pa__done(c, self); - u->quit_requested = 1; - request_render(u); + return -1; } -static int jack_process(jack_nframes_t nframes, void *arg) { - struct userdata *u = arg; - assert(u); - - if (jack_transport_query(u->client, NULL) == JackTransportRolling) { - unsigned c; - pthread_mutex_lock(&u->mutex); +static int parse_options(struct options* o, const char* argument) { + pa_modargs *ma = NULL; + const char* arg_val; + pa_strbuf* strbuf; + + assert(o); - u->frames_requested = nframes; + if (!(ma = pa_modargs_new(argument, valid_modargs))) { + pa_log_error("Failed to parse module arguments."); + goto fail; + } - for (c = 0; c < u->channels; c++) { - u->buffer[c] = jack_port_get_buffer(u->port[c], nframes); - assert(u->buffer[c]); + strbuf = pa_strbuf_new(); + if ((arg_val = pa_modargs_get_value(ma, "sink_name", NULL))) { + pa_strbuf_puts(strbuf, arg_val); + o->sink_name = pa_strbuf_tostring(strbuf); + o->sink_name_given = 1; + } else { + pa_strbuf_puts(strbuf, DEFAULT_SINK_NAME); + o->sink_name = pa_strbuf_tostring(strbuf); + o->sink_name_given = 0; + } + pa_strbuf_free(strbuf); + + strbuf = pa_strbuf_new(); + if ((arg_val = pa_modargs_get_value(ma, "server_name", NULL))) { + pa_strbuf_puts(strbuf, arg_val); + o->server_name = pa_strbuf_tostring(strbuf); + o->server_name_given = 1; + } else { + o->server_name = NULL; + o->server_name_given = 0; + } + pa_strbuf_free(strbuf); + + strbuf = pa_strbuf_new(); + if ((arg_val = pa_modargs_get_value(ma, "client_name", NULL))) { + pa_strbuf_puts(strbuf, arg_val); + o->client_name = pa_strbuf_tostring(strbuf); + o->client_name_given = 1; + } else { + pa_strbuf_puts(strbuf, DEFAULT_CLIENT_NAME); + o->client_name = pa_strbuf_tostring(strbuf); + o->client_name_given = 1; + } + pa_strbuf_free(strbuf); + + if (pa_modargs_get_value(ma, "channels", NULL)) { + o->channels_given = 1; + if (pa_modargs_get_value_u32(ma, "channels", &o->channels) < 0 || + o->channels == 0 || + o->channels >= PA_CHANNELS_MAX) { + pa_log_error("Failed to parse the \"channels\" argument."); + goto fail; } + } else { + o->channels = 0; /* The actual default value is the number of physical + input ports in jack (unknown at the moment), or if + that's zero, then the default_sample_spec.channels + of the core. */ + o->channels_given = 0; + } - request_render(u); - - pthread_cond_wait(&u->cond, &u->mutex); - - u->frames_in_buffer = nframes; - u->timestamp = jack_get_current_transport_frame(u->client); - - pthread_mutex_unlock(&u->mutex); + if (pa_modargs_get_value(ma, "connect", NULL)) { + o->connect_given = 1; + if (pa_modargs_get_value_boolean(ma, "connect", &o->connect) < 0) { + pa_log_error("Failed to parse the \"connect\" argument."); + goto fail; + } + } else { + o->connect = 1; + o->connect_given = 0; } - return 0; -} + if (pa_modargs_get_value(ma, "buffersize", NULL)) { + o->buffersize_given = 1; + if (pa_modargs_get_value_u32(ma, "buffersize", &o->buffersize) < 0) { + pa_log_error("Failed to parse the \"buffersize\" argument."); + goto fail; + } + } else { + o->buffersize = DEFAULT_RINGBUFFER_SIZE; + o->buffersize_given = 0; + } -static pa_usec_t sink_get_latency_cb(pa_sink *s) { - struct userdata *u; - jack_nframes_t n, l, d; + if (pa_modargs_get_value(ma, "channel_map", NULL)) { + o->map_given = 1; + if (pa_modargs_get_channel_map(ma, &o->map) < 0) { + pa_log_error("Failed to parse the \"channel_map\" argument."); + goto fail; + } - assert(s); - u = s->userdata; + /* channel_map specifies the channel count too. */ + if (o->channels_given && (o->channels != o->map.channels)) { + pa_log_error( + "\"channels\" and \"channel_map\" arguments conficted. If you " + "use the \"channel_map\" argument, you can omit the " + "\"channels\" argument."); + goto fail; + } else { + o->channels = o->map.channels; + o->channels_given = 1; + } + } else { + /* The actual default value is the default alsa mappings, but that + can't be set until the channel count is known. Here we initialize + the map to some valid value, although the value won't be used. */ + pa_channel_map_init_stereo(&o->map); + o->map_given = 0; + } - if (jack_transport_query(u->client, NULL) != JackTransportRolling) - return 0; + pa_modargs_free(ma); - n = jack_get_current_transport_frame(u->client); + return 0; - if (n < u->timestamp) - return 0; +fail: + if (ma) + pa_modargs_free(ma); - d = n - u->timestamp; - l = jack_port_get_total_latency(u->client, u->port[0]) + u->frames_in_buffer; + return -1; +} - if (d >= l) - return 0; - return pa_bytes_to_usec((l - d) * pa_frame_size(&s->sample_spec), &s->sample_spec); +static void set_default_channels(pa_module* self, struct options* o) { + struct userdata* u; + const char **ports, **p; + + assert(self); + assert(o); + assert(self->userdata); + + u = self->userdata; + + assert(u->j_client); + assert(self->core); + + o->channels = 0; + + ports = jack_get_ports(u->j_client, NULL, JACK_DEFAULT_AUDIO_TYPE, + JackPortIsPhysical|JackPortIsInput); + + for (p = ports; *p; p++) + o->channels++; + + free(ports); + + if (o->channels >= PA_CHANNELS_MAX) + o->channels = PA_CHANNELS_MAX - 1; + + if (o->channels == 0) + o->channels = self->core->default_sample_spec.channels; } -static void jack_error_func(const char*t) { - pa_log_warn("JACK error >%s<", t); -} -int pa__init(pa_core *c, pa_module*m) { - struct userdata *u = NULL; +static int create_sink(pa_module* self, struct options* o) { + struct userdata* u; pa_sample_spec ss; - pa_channel_map map; - pa_modargs *ma = NULL; - jack_status_t status; - const char *server_name, *client_name; - uint32_t channels = 0; - int do_connect = 1; - unsigned i; - const char **ports = NULL, **p; char *t; + + assert(self); + assert(o); + assert(self->userdata); + + u = self->userdata; + + assert(u->j_client); + + ss.channels = u->channels; + ss.rate = jack_get_sample_rate(u->j_client); + ss.format = PA_SAMPLE_FLOAT32NE; + assert(pa_sample_spec_valid(&ss)); - assert(c); - assert(m); - - jack_set_error_function(jack_error_func); - - if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { - pa_log("failed to parse module arguments."); - goto fail; + if (!(u->sink = pa_sink_new(self->core, __FILE__, o->sink_name, 0, &ss, + &o->map))) { + pa_log("failed to create sink."); + return -1; } + + u->sink->userdata = u; + pa_sink_set_owner(u->sink, self); + + pa_sink_set_description( + u->sink, + t = pa_sprintf_malloc("Jack sink (%s)", + jack_get_client_name(u->j_client))); + pa_xfree(t); + + u->sink->get_latency = sink_get_latency_cb; + + return 0; +} - if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) { - pa_log("failed to parse connect= argument."); - goto fail; + +static void connect_ports(pa_module* self) { + struct userdata* u; + unsigned i; + const char **ports, **p; + + assert(self); + assert(self->userdata); + + u = self->userdata; + + assert(u->j_client); + + ports = jack_get_ports(u->j_client, NULL, JACK_DEFAULT_AUDIO_TYPE, + JackPortIsPhysical|JackPortIsInput); + + for (i = 0, p = ports; i < u->channels; i++, p++) { + assert(u->j_ports[i]); + + if (!*p) { + pa_log("Not enough physical output ports, leaving unconnected."); + break; + } + + pa_log_info("connecting %s to %s", + jack_port_name(u->j_ports[i]), *p); + + if (jack_connect(u->j_client, jack_port_name(u->j_ports[i]), *p)) { + pa_log("Failed to connect %s to %s, leaving unconnected.", + jack_port_name(u->j_ports[i]), *p); + break; + } } + + free(ports); +} - server_name = pa_modargs_get_value(ma, "server_name", NULL); - client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio"); - u = pa_xnew0(struct userdata, 1); - m->userdata = u; - u->core = c; - u->module = m; - u->pipe_fds[0] = u->pipe_fds[1] = -1; - u->pipe_fd_type = 0; +static int start_filling_ringbuffer(pa_module* self) { + struct userdata* u; + pthread_attr_t thread_attributes; - pthread_mutex_init(&u->mutex, NULL); - pthread_cond_init(&u->cond, NULL); + assert(self); + assert(self->userdata); - if (pipe(u->pipe_fds) < 0) { - pa_log("pipe() failed: %s", pa_cstrerror(errno)); + u = self->userdata; + + pthread_attr_init(&thread_attributes); + + if (pthread_attr_setinheritsched(&thread_attributes, + PTHREAD_INHERIT_SCHED) != 0) { + pa_log("pthread_attr_setinheritsched() failed."); goto fail; } - - pa_make_nonblock_fd(u->pipe_fds[1]); - - if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) { - pa_log("jack_client_open() failed."); + else if (pthread_create(&u->filler_thread, &thread_attributes, + fill_ringbuffer, u) != 0) { + pa_log("pthread_create() failed."); goto fail; } + + u->filler_thread_is_running = 1; + + pthread_attr_destroy(&thread_attributes); - ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput); - - channels = 0; - for (p = ports; *p; p++) - channels++; + return 0; + +fail: + pthread_attr_destroy(&thread_attributes); + return -1; +} - if (!channels) - channels = c->default_sample_spec.channels; - if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) { - pa_log("failed to parse channels= argument."); - goto fail; - } +static void jack_error_func(const char* t) { + pa_log_warn("JACK error >%s<", t); +} - pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA); - if (pa_modargs_get_channel_map(ma, &map) < 0 || map.channels != channels) { - pa_log("failed to parse channel_map= argument."); - goto fail; - } - pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client)); +static pa_usec_t sink_get_latency_cb(pa_sink* s) { + /* The latency is approximately the sum of the first port's latency, + buffersize of jack and the ringbuffer size. Maybe instead of using just + the first port, the max of all ports' latencies should be used? */ + struct userdata* u; + jack_nframes_t l; + + assert(s); + assert(s->userdata); - ss.channels = u->channels = channels; - ss.rate = jack_get_sample_rate(u->client); - ss.format = PA_SAMPLE_FLOAT32NE; + u = s->userdata; + + l = jack_port_get_total_latency(u->j_client, u->j_ports[0]) + + u->j_buffersize + u->ringbuffer->size / u->frame_size; + + return pa_bytes_to_usec(l * u->frame_size, &s->sample_spec); +} - assert(pa_sample_spec_valid(&ss)); - for (i = 0; i < ss.channels; i++) { - if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) { - pa_log("jack_port_register() failed."); - goto fail; - } +static int jack_process(jack_nframes_t nframes, void* arg) { + struct userdata* u = arg; + float* j_buffers[PA_CHANNELS_MAX]; + unsigned nsamples = u->channels * nframes; + unsigned sample_idx_part1, sample_idx_part2; + jack_nframes_t frame_idx; + jack_ringbuffer_data_t data[2]; /* In case the readable area in the + ringbuffer is non-continuous, the data + will be split in two parts. */ + unsigned chan; + unsigned samples_left_over; + + for (chan = 0; chan < u->channels; chan++) { + j_buffers[chan] = jack_port_get_buffer(u->j_ports[chan], nframes); + } + + jack_ringbuffer_get_read_vector(u->ringbuffer, data); + + /* We assume that the possible discontinuity doesn't happen in the middle + * of a sample. Should be a safe assumption. */ + assert(((data[0].len % sizeof(float)) == 0) || + (data[1].len == 0)); + + /* Copy from the first part of data until enough samples are copied or the + first part ends. */ + sample_idx_part1 = 0; + chan = 0; + frame_idx = 0; + while (sample_idx_part1 < nsamples && + ((sample_idx_part1 + 1) * sizeof(float)) <= data[0].len) { + float *s = ((float*) data[0].buf) + sample_idx_part1; + float *d = j_buffers[chan] + frame_idx; + *d = *s; + + sample_idx_part1++; + chan = (chan + 1) % u->channels; + frame_idx = sample_idx_part1 / u->channels; + } + + samples_left_over = nsamples - sample_idx_part1; + + /* Copy from the second part of data until enough samples are copied or the + second part ends. */ + sample_idx_part2 = 0; + while (sample_idx_part2 < samples_left_over && + ((sample_idx_part2 + 1) * sizeof(float)) <= data[1].len) { + float *s = ((float*) data[1].buf) + sample_idx_part2; + float *d = j_buffers[chan] + frame_idx; + *d = *s; + + sample_idx_part2++; + chan = (chan + 1) % u->channels; + frame_idx = (sample_idx_part1 + sample_idx_part2) / u->channels; } + + samples_left_over -= sample_idx_part2; + + /* If there's still samples left, fill the buffers with zeros. */ + while (samples_left_over > 0) { + float *d = j_buffers[chan] + frame_idx; + *d = 0.0; + + samples_left_over--; + chan = (chan + 1) % u->channels; + frame_idx = (nsamples - samples_left_over) / u->channels; + } + + jack_ringbuffer_read_advance( + u->ringbuffer, (sample_idx_part1 + sample_idx_part2) * sizeof(float)); + + /* Tell the rendering part that there is room in the ringbuffer. */ + u->ringbuffer_is_full = 0; + pthread_cond_signal(&u->ringbuffer_cond); + + return 0; +} - if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) { - pa_log("failed to create sink."); - goto fail; + +static int jack_blocksize_cb(jack_nframes_t nframes, void* arg) { + /* This gets called in the processing thread, so do we have to be realtime + safe? No, we can do whatever we want. User gets silence while we do it. + + In addition to just updating the j_buffersize field in userdata, we have + to create a new ringbuffer, if the new buffer size is bigger or equal to + the old ringbuffer size. */ + struct userdata* u = arg; + + assert(u); + + /* We don't want to change the blocksize and the ringbuffer while rendering + is going on. */ + pthread_mutex_lock(&u->buffersize_mutex); + + u->j_buffersize = nframes; + + if ((u->ringbuffer->size / u->frame_size) <= nframes) { + /* We have to create a new ringbuffer. What are we going to do with the + old data in the old buffer? We throw it away, because we're lazy + coders. The listening experience is likely to get ruined anyway + during the blocksize change. */ + jack_ringbuffer_free(u->ringbuffer); + + /* The actual ringbuffer size will be rounded up to the nearest power + of two. */ + if (!(u->ringbuffer = + jack_ringbuffer_create((nframes + 1) * u->frame_size))) { + pa_log_error( + "jack_ringbuffer_create() failed while changing jack's buffer " + "size, module exiting."); + jack_client_close(u->j_client); + u->quit_requested = 1; + } + assert((u->ringbuffer->size % sizeof(float)) == 0); + pa_log_info("buffersize is %u frames (%u samples, %u bytes).", + u->ringbuffer->size / u->frame_size, + u->ringbuffer->size / sizeof(float), + u->ringbuffer->size); } + + pthread_mutex_unlock(&u->buffersize_mutex); + + return 0; +} - u->sink->userdata = u; - pa_sink_set_owner(u->sink, m); - pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u->client))); - pa_xfree(t); - u->sink->get_latency = sink_get_latency_cb; - jack_set_process_callback(u->client, jack_process, u); - jack_on_shutdown(u->client, jack_shutdown, u); +static void jack_shutdown(void* arg) { + struct userdata* u = arg; + assert(u); - if (jack_activate(u->client)) { - pa_log("jack_activate() failed"); - goto fail; - } + u->quit_requested = 1; + request_render(u); +} - if (do_connect) { - for (i = 0, p = ports; i < ss.channels; i++, p++) { - if (!*p) { - pa_log("not enough physical output ports, leaving unconnected."); - break; - } +static void io_event_cb(pa_mainloop_api* m, pa_io_event* e, int fd, + pa_io_event_flags_t flags, void* userdata) { + pa_module* self = userdata; + struct userdata* u; + char x; + jack_ringbuffer_data_t buffer[2]; /* The write area in the ringbuffer may + be split in two parts. */ + pa_memchunk chunk; /* This is the data source. */ + unsigned part1_length, part2_length; + unsigned sample_idx_part1, sample_idx_part2; + unsigned chan; + unsigned frame_size; + int rem; + + assert(m); + assert(e); + assert(flags == PA_IO_EVENT_INPUT); + assert(self); + assert(self->userdata); - pa_log_info("connecting %s to %s", jack_port_name(u->port[i]), *p); + u = self->userdata; + + assert(u->pipe_fds[0] == fd); - if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) { - pa_log("failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p); - break; - } - } + pa_read(fd, &x, 1, &u->pipe_fd_type); + if (u->quit_requested) { + pa_module_unload_request(self); + return; } - u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u); - - free(ports); - pa_modargs_free(ma); + frame_size = u->frame_size; + + /* No blocksize changes during rendering, please. */ + pthread_mutex_lock(&u->buffersize_mutex); + + jack_ringbuffer_get_write_vector(u->ringbuffer, buffer); + assert(((buffer[0].len % sizeof(float)) == 0) || (buffer[1].len == 0)); + + part1_length = buffer[0].len / sizeof(float); + part2_length = buffer[1].len / sizeof(float); + + /* If the amount of free space is not a multiple of the frame size, we have + to adjust the lengths in order to not get confused with which sample is + which channel. */ + if ((rem = (part1_length + part2_length) % u->channels) != 0) { + if (part2_length >= rem) { + part2_length -= rem; + } else { + part1_length -= rem - part2_length; + part2_length = 0; + } + } + + /* pa_sink_render_full doesn't accept zero length, so we have do the + copying only if there's data to copy, which actually makes a kind of + sense. */ + if (part1_length > 0 || part2_length > 0) { + pa_sink_render_full(u->sink, + (part1_length + part2_length) * sizeof(float), + &chunk); + + /* Write to the first part of the buffer. */ + for (sample_idx_part1 = 0; + sample_idx_part1 < part1_length; + sample_idx_part1++) { + float *s = + ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + + sample_idx_part1; + float *d = ((float*) buffer[0].buf) + sample_idx_part1; + *d = *s; + } + + /* Write to the second part of the buffer. */ + for (sample_idx_part2 = 0; + sample_idx_part2 < part2_length; + sample_idx_part2++) { + float *s = + ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + + sample_idx_part1 + sample_idx_part2; + float *d = ((float*) buffer[1].buf) + sample_idx_part2; + *d = *s; + } + + pa_memblock_unref(chunk.memblock); + + jack_ringbuffer_write_advance( + u->ringbuffer, (part1_length + part2_length) * sizeof(float)); + } + + /* Blocksize can be changed again. */ + pthread_mutex_unlock(&u->buffersize_mutex); +} - return 0; -fail: - if (ma) - pa_modargs_free(ma); +static void* fill_ringbuffer(void* arg) { + struct userdata* u = arg; + + assert(u); + + while (!u->quit_requested) { + if (u->ringbuffer_is_full) { + pthread_mutex_lock(&u->cond_mutex); + pthread_cond_wait(&u->ringbuffer_cond, + &u->cond_mutex); + pthread_mutex_unlock(&u->cond_mutex); + } + /* No, it's not full yet, but this must be set to one as soon as + possible, because if the jack thread manages to process another + block before we set this to one, we may end up waiting without + a reason. */ + u->ringbuffer_is_full = 1; - free(ports); + request_render(u); + } + + return NULL; +} - pa__done(c, m); - return -1; +static void request_render(struct userdata* u) { + char c = 'x'; + + assert(u); + + assert(u->pipe_fds[1] >= 0); + pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type); } -void pa__done(pa_core *c, pa_module*m) { - struct userdata *u; - assert(c && m); +void pa__done(pa_core* c, pa_module* self) { + struct userdata* u; + + assert(c); + assert(self); - if (!(u = m->userdata)) + if (!self->userdata) return; - if (u->client) - jack_client_close(u->client); + u = self->userdata; + + if (u->filler_thread_is_running) { + u->quit_requested = 1; + pthread_cond_signal(&u->ringbuffer_cond); + pthread_join(u->filler_thread, NULL); + } + + if (u->j_client) + jack_client_close(u->j_client); if (u->io_event) c->mainloop->io_free(u->io_event); @@ -396,13 +866,18 @@ void pa__done(pa_core *c, pa_module*m) { pa_sink_disconnect(u->sink); pa_sink_unref(u->sink); } + + if (u->ringbuffer) + jack_ringbuffer_free(u->ringbuffer); if (u->pipe_fds[0] >= 0) - close(u->pipe_fds[0]); + pa_close(u->pipe_fds[0]); if (u->pipe_fds[1] >= 0) - close(u->pipe_fds[1]); - - pthread_mutex_destroy(&u->mutex); - pthread_cond_destroy(&u->cond); - pa_xfree(u); + pa_close(u->pipe_fds[1]); + + pthread_mutex_destroy(&u->buffersize_mutex); + pthread_cond_destroy(&u->ringbuffer_cond); + pthread_mutex_destroy(&u->cond_mutex); + pa_xfree(self->userdata); + self->userdata = NULL; } -- cgit From 6687dd013169fd8436aa1b45ccdacff074a40d05 Mon Sep 17 00:00:00 2001 From: Tanu Kaskinen Date: Fri, 24 Aug 2007 07:12:47 +0000 Subject: Corrected a bogus comment. git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1716 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 09a72e3a..a40ebe29 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -753,8 +753,7 @@ static void io_event_cb(pa_mainloop_api* m, pa_io_event* e, int fd, part2_length = buffer[1].len / sizeof(float); /* If the amount of free space is not a multiple of the frame size, we have - to adjust the lengths in order to not get confused with which sample is - which channel. */ + to truncate the lengths so that we process only complete frames. */ if ((rem = (part1_length + part2_length) % u->channels) != 0) { if (part2_length >= rem) { part2_length -= rem; -- cgit From a67c21f093202f142438689d3f7cfbdf4ea82eea Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 28 Oct 2007 19:13:50 +0000 Subject: merge 'lennart' branch back into trunk. git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1971 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 1025 ++++++++++++---------------------------- 1 file changed, 299 insertions(+), 726 deletions(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index a40ebe29..5019d656 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -3,7 +3,7 @@ /*** This file is part of PulseAudio. - Copyright 2006, 2007 Lennart Poettering and Tanu Kaskinen + Copyright 2006 Lennart Poettering PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published @@ -25,858 +25,431 @@ #include #endif -#include +#include +#include +#include #include #include +#include +#include +#include +#include #include -#include -#include -#include -#include -#include #include +#include #include #include -#include -#include #include -#include -#include #include -#include +#include +#include +#include +#include +#include #include "module-jack-sink-symdef.h" -PA_MODULE_AUTHOR("Lennart Poettering & Tanu Kaskinen") -PA_MODULE_DESCRIPTION("Jack Sink") +/* General overview: + * + * Because JACK has a very unflexible event loop management, which + * doesn't allow us to add our own event sources to the event thread + * we cannot use the JACK real-time thread for dispatching our PA + * work. Instead, we run an additional RT thread which does most of + * the PA handling, and have the JACK RT thread request data from it + * via pa_asyncmsgq. The cost is an additional context switch which + * should hopefully not be that expensive if RT scheduling is + * enabled. A better fix would only be possible with additional event + * source support in JACK. + */ + +PA_MODULE_AUTHOR("Lennart Poettering") +PA_MODULE_DESCRIPTION("JACK Sink") PA_MODULE_VERSION(PACKAGE_VERSION) PA_MODULE_USAGE( "sink_name= " "server_name= " "client_name= " "channels= " - "connect= " - "buffersize= " + "connect= " "channel_map=") #define DEFAULT_SINK_NAME "jack_out" -#define DEFAULT_CLIENT_NAME "PulseAudio(output)" -#define DEFAULT_RINGBUFFER_SIZE 4096 - struct userdata { + pa_core *core; + pa_module *module; pa_sink *sink; unsigned channels; - unsigned frame_size; - jack_port_t* j_ports[PA_CHANNELS_MAX]; - jack_client_t *j_client; + jack_port_t* port[PA_CHANNELS_MAX]; + jack_client_t *client; - jack_nframes_t j_buffersize; + void *buffer[PA_CHANNELS_MAX]; - /* For avoiding j_buffersize changes at a wrong moment. */ - pthread_mutex_t buffersize_mutex; + pa_thread_mq thread_mq; + pa_asyncmsgq *jack_msgq; + pa_rtpoll *rtpoll; + pa_rtpoll_item *rtpoll_item; - /* The intermediate store where the pulse side writes to and the jack side - reads from. */ - jack_ringbuffer_t* ringbuffer; - - /* For signaling when there's room in the ringbuffer. */ - pthread_mutex_t cond_mutex; - pthread_cond_t ringbuffer_cond; + pa_thread *thread; - pthread_t filler_thread; /* Keeps the ringbuffer filled. */ + jack_nframes_t frames_in_buffer; + jack_nframes_t saved_frame_time; + pa_bool_t saved_frame_time_valid; +}; - int ringbuffer_is_full; - int filler_thread_is_running; - int quit_requested; +static const char* const valid_modargs[] = { + "sink_name", + "server_name", + "client_name", + "channels", + "connect", + "channel_map", + NULL +}; - int pipe_fd_type; - int pipe_fds[2]; - pa_io_event *io_event; +enum { + SINK_MESSAGE_RENDER = PA_SINK_MESSAGE_MAX, + SINK_MESSAGE_ON_SHUTDOWN }; +static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *memchunk) { + struct userdata *u = PA_SINK(o)->userdata; -struct options { - char* sink_name; - int sink_name_given; + switch (code) { - char* server_name; /* May be NULL */ - int server_name_given; + case SINK_MESSAGE_RENDER: - char* client_name; - int client_name_given; + /* Handle the request from the JACK thread */ - unsigned channels; - int channels_given; + if (u->sink->thread_info.state == PA_SINK_RUNNING) { + pa_memchunk chunk; + size_t nbytes; + void *p; - int connect; - int connect_given; + pa_assert(offset > 0); + nbytes = offset * pa_frame_size(&u->sink->sample_spec); - unsigned buffersize; - int buffersize_given; + pa_sink_render_full(u->sink, nbytes, &chunk); - pa_channel_map map; - int map_given; -}; + p = (uint8_t*) pa_memblock_acquire(chunk.memblock) + chunk.index; + pa_deinterleave(p, u->buffer, u->channels, sizeof(float), offset); + pa_memblock_release(chunk.memblock); + pa_memblock_unref(chunk.memblock); + } else { + unsigned c; + pa_sample_spec ss; -static const char* const valid_modargs[] = { - "sink_name", - "server_name", - "client_name", - "channels", - "connect", - "buffersize", - "channel_map", - NULL -}; + /* Humm, we're not RUNNING, hence let's write some silence */ + ss = u->sink->sample_spec; + ss.channels = 1; -/* Initialization functions. */ -static int parse_options(struct options* o, const char* argument); -static void set_default_channels(pa_module* self, struct options* o); -static int create_sink(pa_module* self, struct options *o); -static void connect_ports(pa_module* self); -static int start_filling_ringbuffer(pa_module* self); + for (c = 0; c < u->channels; c++) + pa_silence_memory(u->buffer[c], offset * pa_sample_size(&ss), &ss); + } -/* Various callbacks. */ -static void jack_error_func(const char* t); -static pa_usec_t sink_get_latency_cb(pa_sink* s); -static int jack_process(jack_nframes_t nframes, void* arg); -static int jack_blocksize_cb(jack_nframes_t nframes, void* arg); -static void jack_shutdown(void* arg); -static void io_event_cb(pa_mainloop_api* m, pa_io_event* e, int fd, - pa_io_event_flags_t flags, void* userdata); + u->frames_in_buffer = offset; + u->saved_frame_time = * (jack_nframes_t*) data; + u->saved_frame_time_valid = TRUE; -/* The ringbuffer filler thread runs in this function. */ -static void* fill_ringbuffer(void* arg); + return 0; -/* request_render asks asynchronously the mainloop to call io_event_cb. */ -static void request_render(struct userdata* u); + case SINK_MESSAGE_ON_SHUTDOWN: + pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL); + return 0; + case PA_SINK_MESSAGE_GET_LATENCY: { + jack_nframes_t l, ft, d; + size_t n; -int pa__init(pa_core* c, pa_module* self) { - struct userdata* u = NULL; - struct options o; - unsigned i; - - assert(c); - assert(self); - - o.sink_name = NULL; - o.server_name = NULL; - o.client_name = NULL; - - self->userdata = pa_xnew0(struct userdata, 1); - u = self->userdata; - - u->pipe_fds[0] = u->pipe_fds[1] = -1; - u->pipe_fd_type = 0; - u->ringbuffer_is_full = 0; - u->filler_thread_is_running = 0; - u->quit_requested = 0; - pthread_mutex_init(&u->buffersize_mutex, NULL); - pthread_mutex_init(&u->cond_mutex, NULL); - pthread_cond_init(&u->ringbuffer_cond, NULL); - - if (parse_options(&o, self->argument) != 0) - goto fail; - - jack_set_error_function(jack_error_func); - - if (!(u->j_client = jack_client_open( - o.client_name, - o.server_name ? JackServerName : JackNullOption, - NULL, o.server_name))) { - pa_log_error("jack_client_open() failed."); - goto fail; - } - pa_log_info("Successfully connected as '%s'", - jack_get_client_name(u->j_client)); - - if (!o.channels_given) - set_default_channels(self, &o); - - u->channels = o.channels; - - if (!o.map_given) - pa_channel_map_init_auto(&o.map, u->channels, PA_CHANNEL_MAP_ALSA); - - for (i = 0; i < u->channels; i++) { - char* port_name = pa_sprintf_malloc( - "out_%i:%s", i+1, - pa_channel_position_to_string(o.map.map[i])); - - if (!(u->j_ports[i] = jack_port_register( - u->j_client, port_name, - JACK_DEFAULT_AUDIO_TYPE, - JackPortIsOutput|JackPortIsTerminal, 0))) { - pa_log("jack_port_register() failed."); - goto fail; + /* This is the "worst-case" latency */ + l = jack_port_get_total_latency(u->client, u->port[0]) + u->frames_in_buffer; + + if (u->saved_frame_time_valid) { + /* Adjust the worst case latency by the time that + * passed since we last handed data to JACK */ + + ft = jack_frame_time(u->client); + d = ft > u->saved_frame_time ? ft - u->saved_frame_time : 0; + l = l > d ? l - d : 0; + } + + /* Convert it to usec */ + n = l * pa_frame_size(&u->sink->sample_spec); + *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec); + + return 0; } - - pa_xfree(port_name); } - - if (pipe(u->pipe_fds) < 0) { - pa_log("pipe() failed: %s", pa_cstrerror(errno)); - goto fail; - } - pa_make_nonblock_fd(u->pipe_fds[1]); - - if (create_sink(self, &o) != 0) - goto fail; - u->frame_size = pa_frame_size(&u->sink->sample_spec); - u->j_buffersize = jack_get_buffer_size(u->j_client); - - /* If the ringbuffer size were equal to the jack buffer size, a full block - would never fit in the ringbuffer, because the ringbuffer can never be - totally full: one slot is always wasted. */ - if (o.buffersize <= u->j_buffersize) { - o.buffersize = u->j_buffersize + 1; - } - /* The actual ringbuffer size will be rounded up to the nearest power of - two. */ - if (!(u->ringbuffer = jack_ringbuffer_create( - o.buffersize * u->frame_size))) { - pa_log("jack_ringbuffer_create() failed."); - goto fail; - } - assert((u->ringbuffer->size % sizeof(float)) == 0); - pa_log_info("buffersize is %u frames (%u samples, %u bytes).", - u->ringbuffer->size / u->frame_size, - u->ringbuffer->size / sizeof(float), - u->ringbuffer->size); - - jack_set_process_callback(u->j_client, jack_process, u); - jack_set_buffer_size_callback(u->j_client, jack_blocksize_cb, u); - jack_on_shutdown(u->j_client, jack_shutdown, u); - - if (jack_activate(u->j_client)) { - pa_log("jack_activate() failed."); - goto fail; - } + return pa_sink_process_msg(o, code, data, offset, memchunk); +} - if (o.connect) - connect_ports(self); +static int jack_process(jack_nframes_t nframes, void *arg) { + struct userdata *u = arg; + unsigned c; + jack_nframes_t frame_time; + pa_assert(u); - u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], - PA_IO_EVENT_INPUT, io_event_cb, self); - - if (start_filling_ringbuffer(self) != 0) - goto fail; + /* We just forward the request to our other RT thread */ - pa_xfree(o.sink_name); - pa_xfree(o.server_name); - pa_xfree(o.client_name); - - return 0; + for (c = 0; c < u->channels; c++) + pa_assert_se(u->buffer[c] = jack_port_get_buffer(u->port[c], nframes)); -fail: - pa_xfree(o.sink_name); - pa_xfree(o.server_name); - pa_xfree(o.client_name); - pa__done(c, self); + frame_time = jack_frame_time(u->client); - return -1; + pa_assert_se(pa_asyncmsgq_send(u->jack_msgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_RENDER, &frame_time, nframes, NULL) == 0); + return 0; } +static void thread_func(void *userdata) { + struct userdata *u = userdata; -static int parse_options(struct options* o, const char* argument) { - pa_modargs *ma = NULL; - const char* arg_val; - pa_strbuf* strbuf; - - assert(o); + pa_assert(u); - if (!(ma = pa_modargs_new(argument, valid_modargs))) { - pa_log_error("Failed to parse module arguments."); - goto fail; - } + pa_log_debug("Thread starting up"); - strbuf = pa_strbuf_new(); - if ((arg_val = pa_modargs_get_value(ma, "sink_name", NULL))) { - pa_strbuf_puts(strbuf, arg_val); - o->sink_name = pa_strbuf_tostring(strbuf); - o->sink_name_given = 1; - } else { - pa_strbuf_puts(strbuf, DEFAULT_SINK_NAME); - o->sink_name = pa_strbuf_tostring(strbuf); - o->sink_name_given = 0; - } - pa_strbuf_free(strbuf); - - strbuf = pa_strbuf_new(); - if ((arg_val = pa_modargs_get_value(ma, "server_name", NULL))) { - pa_strbuf_puts(strbuf, arg_val); - o->server_name = pa_strbuf_tostring(strbuf); - o->server_name_given = 1; - } else { - o->server_name = NULL; - o->server_name_given = 0; - } - pa_strbuf_free(strbuf); - - strbuf = pa_strbuf_new(); - if ((arg_val = pa_modargs_get_value(ma, "client_name", NULL))) { - pa_strbuf_puts(strbuf, arg_val); - o->client_name = pa_strbuf_tostring(strbuf); - o->client_name_given = 1; - } else { - pa_strbuf_puts(strbuf, DEFAULT_CLIENT_NAME); - o->client_name = pa_strbuf_tostring(strbuf); - o->client_name_given = 1; - } - pa_strbuf_free(strbuf); - - if (pa_modargs_get_value(ma, "channels", NULL)) { - o->channels_given = 1; - if (pa_modargs_get_value_u32(ma, "channels", &o->channels) < 0 || - o->channels == 0 || - o->channels >= PA_CHANNELS_MAX) { - pa_log_error("Failed to parse the \"channels\" argument."); - goto fail; - } - } else { - o->channels = 0; /* The actual default value is the number of physical - input ports in jack (unknown at the moment), or if - that's zero, then the default_sample_spec.channels - of the core. */ - o->channels_given = 0; - } + if (u->core->high_priority) + pa_make_realtime(); - if (pa_modargs_get_value(ma, "connect", NULL)) { - o->connect_given = 1; - if (pa_modargs_get_value_boolean(ma, "connect", &o->connect) < 0) { - pa_log_error("Failed to parse the \"connect\" argument."); - goto fail; - } - } else { - o->connect = 1; - o->connect_given = 0; - } + pa_thread_mq_install(&u->thread_mq); + pa_rtpoll_install(u->rtpoll); - if (pa_modargs_get_value(ma, "buffersize", NULL)) { - o->buffersize_given = 1; - if (pa_modargs_get_value_u32(ma, "buffersize", &o->buffersize) < 0) { - pa_log_error("Failed to parse the \"buffersize\" argument."); - goto fail; - } - } else { - o->buffersize = DEFAULT_RINGBUFFER_SIZE; - o->buffersize_given = 0; - } + for (;;) { + int ret; - if (pa_modargs_get_value(ma, "channel_map", NULL)) { - o->map_given = 1; - if (pa_modargs_get_channel_map(ma, &o->map) < 0) { - pa_log_error("Failed to parse the \"channel_map\" argument."); + if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) goto fail; - } - /* channel_map specifies the channel count too. */ - if (o->channels_given && (o->channels != o->map.channels)) { - pa_log_error( - "\"channels\" and \"channel_map\" arguments conficted. If you " - "use the \"channel_map\" argument, you can omit the " - "\"channels\" argument."); - goto fail; - } else { - o->channels = o->map.channels; - o->channels_given = 1; - } - } else { - /* The actual default value is the default alsa mappings, but that - can't be set until the channel count is known. Here we initialize - the map to some valid value, although the value won't be used. */ - pa_channel_map_init_stereo(&o->map); - o->map_given = 0; + if (ret == 0) + goto finish; } - pa_modargs_free(ma); - - return 0; - fail: - if (ma) - pa_modargs_free(ma); + /* If this was no regular exit from the loop we have to continue + * processing messages until we received PA_MESSAGE_SHUTDOWN */ + pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL); + pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN); - return -1; +finish: + pa_log_debug("Thread shutting down"); } +static void jack_error_func(const char*t) { + char *s; -static void set_default_channels(pa_module* self, struct options* o) { - struct userdata* u; - const char **ports, **p; - - assert(self); - assert(o); - assert(self->userdata); - - u = self->userdata; - - assert(u->j_client); - assert(self->core); - - o->channels = 0; - - ports = jack_get_ports(u->j_client, NULL, JACK_DEFAULT_AUDIO_TYPE, - JackPortIsPhysical|JackPortIsInput); - - for (p = ports; *p; p++) - o->channels++; - - free(ports); - - if (o->channels >= PA_CHANNELS_MAX) - o->channels = PA_CHANNELS_MAX - 1; - - if (o->channels == 0) - o->channels = self->core->default_sample_spec.channels; + s = pa_xstrndup(t, strcspn(t, "\n\r")); + pa_log_warn("JACK error >%s<", s); + pa_xfree(s); } +static void jack_init(void *arg) { + struct userdata *u = arg; -static int create_sink(pa_module* self, struct options* o) { - struct userdata* u; - pa_sample_spec ss; - char *t; - - assert(self); - assert(o); - assert(self->userdata); - - u = self->userdata; - - assert(u->j_client); - - ss.channels = u->channels; - ss.rate = jack_get_sample_rate(u->j_client); - ss.format = PA_SAMPLE_FLOAT32NE; - assert(pa_sample_spec_valid(&ss)); + pa_log_info("JACK thread starting up."); - if (!(u->sink = pa_sink_new(self->core, __FILE__, o->sink_name, 0, &ss, - &o->map))) { - pa_log("failed to create sink."); - return -1; - } - - u->sink->userdata = u; - pa_sink_set_owner(u->sink, self); - - pa_sink_set_description( - u->sink, - t = pa_sprintf_malloc("Jack sink (%s)", - jack_get_client_name(u->j_client))); - pa_xfree(t); - - u->sink->get_latency = sink_get_latency_cb; - - return 0; + if (u->core->high_priority) + pa_make_realtime(); } +static void jack_shutdown(void* arg) { + struct userdata *u = arg; -static void connect_ports(pa_module* self) { - struct userdata* u; - unsigned i; - const char **ports, **p; - - assert(self); - assert(self->userdata); - - u = self->userdata; - - assert(u->j_client); - - ports = jack_get_ports(u->j_client, NULL, JACK_DEFAULT_AUDIO_TYPE, - JackPortIsPhysical|JackPortIsInput); - - for (i = 0, p = ports; i < u->channels; i++, p++) { - assert(u->j_ports[i]); - - if (!*p) { - pa_log("Not enough physical output ports, leaving unconnected."); - break; - } - - pa_log_info("connecting %s to %s", - jack_port_name(u->j_ports[i]), *p); - - if (jack_connect(u->j_client, jack_port_name(u->j_ports[i]), *p)) { - pa_log("Failed to connect %s to %s, leaving unconnected.", - jack_port_name(u->j_ports[i]), *p); - break; - } - } - - free(ports); + pa_log_info("JACK thread shutting down.."); + pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_ON_SHUTDOWN, NULL, 0, NULL, NULL); } +int pa__init(pa_module*m) { + struct userdata *u = NULL; + pa_sample_spec ss; + pa_channel_map map; + pa_modargs *ma = NULL; + jack_status_t status; + const char *server_name, *client_name; + uint32_t channels = 0; + int do_connect = 1; + unsigned i; + const char **ports = NULL, **p; + char *t; -static int start_filling_ringbuffer(pa_module* self) { - struct userdata* u; - pthread_attr_t thread_attributes; + pa_assert(m); - assert(self); - assert(self->userdata); + jack_set_error_function(jack_error_func); - u = self->userdata; - - pthread_attr_init(&thread_attributes); - - if (pthread_attr_setinheritsched(&thread_attributes, - PTHREAD_INHERIT_SCHED) != 0) { - pa_log("pthread_attr_setinheritsched() failed."); + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("Failed to parse module arguments."); goto fail; } - else if (pthread_create(&u->filler_thread, &thread_attributes, - fill_ringbuffer, u) != 0) { - pa_log("pthread_create() failed."); + + if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) { + pa_log("Failed to parse connect= argument."); goto fail; } - - u->filler_thread_is_running = 1; - - pthread_attr_destroy(&thread_attributes); - - return 0; - -fail: - pthread_attr_destroy(&thread_attributes); - return -1; -} + server_name = pa_modargs_get_value(ma, "server_name", NULL); + client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio JACK Sink"); + + u = pa_xnew0(struct userdata, 1); + u->core = m->core; + u->module = m; + m->userdata = u; + u->saved_frame_time_valid = FALSE; + pa_thread_mq_init(&u->thread_mq, m->core->mainloop); + u->rtpoll = pa_rtpoll_new(); + pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq); + + /* The queue linking the JACK thread and our RT thread */ + u->jack_msgq = pa_asyncmsgq_new(0); + + /* The msgq from the JACK RT thread should have an even higher + * priority than the normal message queues, to match the guarantee + * all other drivers make: supplying the audio device with data is + * the top priority -- and as long as that is possible we don't do + * anything else */ + u->rtpoll_item = pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY-1, u->jack_msgq); + + if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) { + pa_log("jack_client_open() failed."); + goto fail; + } -static void jack_error_func(const char* t) { - pa_log_warn("JACK error >%s<", t); -} - + ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput); -static pa_usec_t sink_get_latency_cb(pa_sink* s) { - /* The latency is approximately the sum of the first port's latency, - buffersize of jack and the ringbuffer size. Maybe instead of using just - the first port, the max of all ports' latencies should be used? */ - struct userdata* u; - jack_nframes_t l; - - assert(s); - assert(s->userdata); - - u = s->userdata; - - l = jack_port_get_total_latency(u->j_client, u->j_ports[0]) + - u->j_buffersize + u->ringbuffer->size / u->frame_size; - - return pa_bytes_to_usec(l * u->frame_size, &s->sample_spec); -} + channels = 0; + for (p = ports; *p; p++) + channels++; + if (!channels) + channels = m->core->default_sample_spec.channels; -static int jack_process(jack_nframes_t nframes, void* arg) { - struct userdata* u = arg; - float* j_buffers[PA_CHANNELS_MAX]; - unsigned nsamples = u->channels * nframes; - unsigned sample_idx_part1, sample_idx_part2; - jack_nframes_t frame_idx; - jack_ringbuffer_data_t data[2]; /* In case the readable area in the - ringbuffer is non-continuous, the data - will be split in two parts. */ - unsigned chan; - unsigned samples_left_over; - - for (chan = 0; chan < u->channels; chan++) { - j_buffers[chan] = jack_port_get_buffer(u->j_ports[chan], nframes); - } - - jack_ringbuffer_get_read_vector(u->ringbuffer, data); - - /* We assume that the possible discontinuity doesn't happen in the middle - * of a sample. Should be a safe assumption. */ - assert(((data[0].len % sizeof(float)) == 0) || - (data[1].len == 0)); - - /* Copy from the first part of data until enough samples are copied or the - first part ends. */ - sample_idx_part1 = 0; - chan = 0; - frame_idx = 0; - while (sample_idx_part1 < nsamples && - ((sample_idx_part1 + 1) * sizeof(float)) <= data[0].len) { - float *s = ((float*) data[0].buf) + sample_idx_part1; - float *d = j_buffers[chan] + frame_idx; - *d = *s; - - sample_idx_part1++; - chan = (chan + 1) % u->channels; - frame_idx = sample_idx_part1 / u->channels; - } - - samples_left_over = nsamples - sample_idx_part1; - - /* Copy from the second part of data until enough samples are copied or the - second part ends. */ - sample_idx_part2 = 0; - while (sample_idx_part2 < samples_left_over && - ((sample_idx_part2 + 1) * sizeof(float)) <= data[1].len) { - float *s = ((float*) data[1].buf) + sample_idx_part2; - float *d = j_buffers[chan] + frame_idx; - *d = *s; - - sample_idx_part2++; - chan = (chan + 1) % u->channels; - frame_idx = (sample_idx_part1 + sample_idx_part2) / u->channels; + if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) { + pa_log("Failed to parse channels= argument."); + goto fail; } - - samples_left_over -= sample_idx_part2; - - /* If there's still samples left, fill the buffers with zeros. */ - while (samples_left_over > 0) { - float *d = j_buffers[chan] + frame_idx; - *d = 0.0; - - samples_left_over--; - chan = (chan + 1) % u->channels; - frame_idx = (nsamples - samples_left_over) / u->channels; + + pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA); + if (pa_modargs_get_channel_map(ma, NULL, &map) < 0 || map.channels != channels) { + pa_log("Failed to parse channel_map= argument."); + goto fail; } - - jack_ringbuffer_read_advance( - u->ringbuffer, (sample_idx_part1 + sample_idx_part2) * sizeof(float)); - - /* Tell the rendering part that there is room in the ringbuffer. */ - u->ringbuffer_is_full = 0; - pthread_cond_signal(&u->ringbuffer_cond); - - return 0; -} + pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client)); + + ss.channels = u->channels = channels; + ss.rate = jack_get_sample_rate(u->client); + ss.format = PA_SAMPLE_FLOAT32NE; -static int jack_blocksize_cb(jack_nframes_t nframes, void* arg) { - /* This gets called in the processing thread, so do we have to be realtime - safe? No, we can do whatever we want. User gets silence while we do it. - - In addition to just updating the j_buffersize field in userdata, we have - to create a new ringbuffer, if the new buffer size is bigger or equal to - the old ringbuffer size. */ - struct userdata* u = arg; - - assert(u); - - /* We don't want to change the blocksize and the ringbuffer while rendering - is going on. */ - pthread_mutex_lock(&u->buffersize_mutex); - - u->j_buffersize = nframes; - - if ((u->ringbuffer->size / u->frame_size) <= nframes) { - /* We have to create a new ringbuffer. What are we going to do with the - old data in the old buffer? We throw it away, because we're lazy - coders. The listening experience is likely to get ruined anyway - during the blocksize change. */ - jack_ringbuffer_free(u->ringbuffer); - - /* The actual ringbuffer size will be rounded up to the nearest power - of two. */ - if (!(u->ringbuffer = - jack_ringbuffer_create((nframes + 1) * u->frame_size))) { - pa_log_error( - "jack_ringbuffer_create() failed while changing jack's buffer " - "size, module exiting."); - jack_client_close(u->j_client); - u->quit_requested = 1; + pa_assert(pa_sample_spec_valid(&ss)); + + for (i = 0; i < ss.channels; i++) { + if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) { + pa_log("jack_port_register() failed."); + goto fail; } - assert((u->ringbuffer->size % sizeof(float)) == 0); - pa_log_info("buffersize is %u frames (%u samples, %u bytes).", - u->ringbuffer->size / u->frame_size, - u->ringbuffer->size / sizeof(float), - u->ringbuffer->size); } - - pthread_mutex_unlock(&u->buffersize_mutex); - - return 0; -} + if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) { + pa_log("failed to create sink."); + goto fail; + } -static void jack_shutdown(void* arg) { - struct userdata* u = arg; - assert(u); + u->sink->parent.process_msg = sink_process_msg; + u->sink->userdata = u; + u->sink->flags = PA_SINK_LATENCY; - u->quit_requested = 1; - request_render(u); -} + pa_sink_set_module(u->sink, m); + pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); + pa_sink_set_rtpoll(u->sink, u->rtpoll); + pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u->client))); + pa_xfree(t); + jack_set_process_callback(u->client, jack_process, u); + jack_on_shutdown(u->client, jack_shutdown, u); + jack_set_thread_init_callback(u->client, jack_init, u); -static void io_event_cb(pa_mainloop_api* m, pa_io_event* e, int fd, - pa_io_event_flags_t flags, void* userdata) { - pa_module* self = userdata; - struct userdata* u; - char x; - jack_ringbuffer_data_t buffer[2]; /* The write area in the ringbuffer may - be split in two parts. */ - pa_memchunk chunk; /* This is the data source. */ - unsigned part1_length, part2_length; - unsigned sample_idx_part1, sample_idx_part2; - unsigned chan; - unsigned frame_size; - int rem; - - assert(m); - assert(e); - assert(flags == PA_IO_EVENT_INPUT); - assert(self); - assert(self->userdata); - - u = self->userdata; - - assert(u->pipe_fds[0] == fd); - - pa_read(fd, &x, 1, &u->pipe_fd_type); - - if (u->quit_requested) { - pa_module_unload_request(self); - return; + if (!(u->thread = pa_thread_new(thread_func, u))) { + pa_log("Failed to create thread."); + goto fail; } - frame_size = u->frame_size; - - /* No blocksize changes during rendering, please. */ - pthread_mutex_lock(&u->buffersize_mutex); - - jack_ringbuffer_get_write_vector(u->ringbuffer, buffer); - assert(((buffer[0].len % sizeof(float)) == 0) || (buffer[1].len == 0)); - - part1_length = buffer[0].len / sizeof(float); - part2_length = buffer[1].len / sizeof(float); - - /* If the amount of free space is not a multiple of the frame size, we have - to truncate the lengths so that we process only complete frames. */ - if ((rem = (part1_length + part2_length) % u->channels) != 0) { - if (part2_length >= rem) { - part2_length -= rem; - } else { - part1_length -= rem - part2_length; - part2_length = 0; - } + if (jack_activate(u->client)) { + pa_log("jack_activate() failed"); + goto fail; } - - /* pa_sink_render_full doesn't accept zero length, so we have do the - copying only if there's data to copy, which actually makes a kind of - sense. */ - if (part1_length > 0 || part2_length > 0) { - pa_sink_render_full(u->sink, - (part1_length + part2_length) * sizeof(float), - &chunk); - - /* Write to the first part of the buffer. */ - for (sample_idx_part1 = 0; - sample_idx_part1 < part1_length; - sample_idx_part1++) { - float *s = - ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + - sample_idx_part1; - float *d = ((float*) buffer[0].buf) + sample_idx_part1; - *d = *s; - } - - /* Write to the second part of the buffer. */ - for (sample_idx_part2 = 0; - sample_idx_part2 < part2_length; - sample_idx_part2++) { - float *s = - ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + - sample_idx_part1 + sample_idx_part2; - float *d = ((float*) buffer[1].buf) + sample_idx_part2; - *d = *s; + + if (do_connect) { + for (i = 0, p = ports; i < ss.channels; i++, p++) { + + if (!*p) { + pa_log("Not enough physical output ports, leaving unconnected."); + break; + } + + pa_log_info("Connecting %s to %s", jack_port_name(u->port[i]), *p); + + if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) { + pa_log("Failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p); + break; + } } - - pa_memblock_unref(chunk.memblock); - - jack_ringbuffer_write_advance( - u->ringbuffer, (part1_length + part2_length) * sizeof(float)); } - - /* Blocksize can be changed again. */ - pthread_mutex_unlock(&u->buffersize_mutex); -} + pa_sink_put(u->sink); -static void* fill_ringbuffer(void* arg) { - struct userdata* u = arg; - - assert(u); - - while (!u->quit_requested) { - if (u->ringbuffer_is_full) { - pthread_mutex_lock(&u->cond_mutex); - pthread_cond_wait(&u->ringbuffer_cond, - &u->cond_mutex); - pthread_mutex_unlock(&u->cond_mutex); - } - /* No, it's not full yet, but this must be set to one as soon as - possible, because if the jack thread manages to process another - block before we set this to one, we may end up waiting without - a reason. */ - u->ringbuffer_is_full = 1; + free(ports); + pa_modargs_free(ma); - request_render(u); - } - - return NULL; -} + return 0; + +fail: + if (ma) + pa_modargs_free(ma); + + free(ports); + pa__done(m); -static void request_render(struct userdata* u) { - char c = 'x'; - - assert(u); - - assert(u->pipe_fds[1] >= 0); - pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type); + return -1; } -void pa__done(pa_core* c, pa_module* self) { - struct userdata* u; - - assert(c); - assert(self); +void pa__done(pa_module*m) { + struct userdata *u; + + pa_assert(m); - if (!self->userdata) + if (!(u = m->userdata)) return; - u = self->userdata; - - if (u->filler_thread_is_running) { - u->quit_requested = 1; - pthread_cond_signal(&u->ringbuffer_cond); - pthread_join(u->filler_thread, NULL); + if (u->client) + jack_client_close(u->client); + + if (u->sink) + pa_sink_unlink(u->sink); + + if (u->thread) { + pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL); + pa_thread_free(u->thread); } - - if (u->j_client) - jack_client_close(u->j_client); - if (u->io_event) - c->mainloop->io_free(u->io_event); + pa_thread_mq_done(&u->thread_mq); - if (u->sink) { - pa_sink_disconnect(u->sink); + if (u->sink) pa_sink_unref(u->sink); - } - - if (u->ringbuffer) - jack_ringbuffer_free(u->ringbuffer); - - if (u->pipe_fds[0] >= 0) - pa_close(u->pipe_fds[0]); - if (u->pipe_fds[1] >= 0) - pa_close(u->pipe_fds[1]); - - pthread_mutex_destroy(&u->buffersize_mutex); - pthread_cond_destroy(&u->ringbuffer_cond); - pthread_mutex_destroy(&u->cond_mutex); - pa_xfree(self->userdata); - self->userdata = NULL; + + if (u->rtpoll_item) + pa_rtpoll_item_free(u->rtpoll_item); + + if (u->jack_msgq) + pa_asyncmsgq_unref(u->jack_msgq); + + if (u->rtpoll) + pa_rtpoll_free(u->rtpoll); + + pa_xfree(u); } -- cgit From 1dae2e644f1e7c9810befb95315ec22fec7c100c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 29 Oct 2007 20:32:53 +0000 Subject: we don't want to include assert.h anymore git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1986 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 1 - 1 file changed, 1 deletion(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 5019d656..2cf8a58c 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include -- cgit From 7bfd1b2f01613dd14b9ca478ae530c1641aa46a1 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 1 Nov 2007 02:58:26 +0000 Subject: make rtprio and nice level actually configurable git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@2014 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 2cf8a58c..840867ce 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -214,8 +214,8 @@ static void thread_func(void *userdata) { pa_log_debug("Thread starting up"); - if (u->core->high_priority) - pa_make_realtime(); + if (u->core->realtime_scheduling) + pa_make_realtime(u->core->realtime_priority); pa_thread_mq_install(&u->thread_mq); pa_rtpoll_install(u->rtpoll); @@ -253,8 +253,8 @@ static void jack_init(void *arg) { pa_log_info("JACK thread starting up."); - if (u->core->high_priority) - pa_make_realtime(); + if (u->core->realtime_scheduling) + pa_make_realtime(u->core->realtime_priority+4); } static void jack_shutdown(void* arg) { -- cgit From e313fe1b3d0d9f9945c41c151d72edbe9cf1ec54 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 9 Nov 2007 18:25:40 +0000 Subject: tag modules that may only be loaded once at most especially, and enforce that in the module loader git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@2043 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 840867ce..44da068f 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -64,16 +64,17 @@ * source support in JACK. */ -PA_MODULE_AUTHOR("Lennart Poettering") -PA_MODULE_DESCRIPTION("JACK Sink") -PA_MODULE_VERSION(PACKAGE_VERSION) +PA_MODULE_AUTHOR("Lennart Poettering"); +PA_MODULE_DESCRIPTION("JACK Sink"); +PA_MODULE_LOAD_ONCE(TRUE); +PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_USAGE( "sink_name= " "server_name= " "client_name= " "channels= " "connect= " - "channel_map=") + "channel_map="); #define DEFAULT_SINK_NAME "jack_out" -- cgit From 9b75b9de563bb61ef4b8f2000ed973fecba1137a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 14 Nov 2007 16:09:03 +0000 Subject: add missing pa_boolization git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@2056 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 44da068f..46e128c9 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -273,7 +273,7 @@ int pa__init(pa_module*m) { jack_status_t status; const char *server_name, *client_name; uint32_t channels = 0; - int do_connect = 1; + pa_bool_t do_connect = TRUE; unsigned i; const char **ports = NULL, **p; char *t; -- cgit From 86b9ef8c961bed9d3a65f044741bb423c26d8005 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 13 Feb 2008 22:13:44 +0000 Subject: deal with a possibly failing pa_channel_map_init_auto() correctly git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@2105 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/module-jack-sink.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/modules/module-jack-sink.c') diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 46e128c9..a42aa9ef 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -333,6 +333,7 @@ int pa__init(pa_module*m) { goto fail; } + pa_assert_se(pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_AUX)); pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA); if (pa_modargs_get_channel_map(ma, NULL, &map) < 0 || map.channels != channels) { pa_log("Failed to parse channel_map= argument."); -- cgit