Ticket #91: realtime_safety.patch

File realtime_safety.patch, 28.1 kB (added by tanuk, 1 year ago)
  • src/modules/module-jack-sink.c

    old new  
    33/*** 
    44  This file is part of PulseAudio. 
    55 
    6   Copyright 2006 Lennart Poettering 
     6  Copyright 2006, 2007 Lennart Poettering and Tanu Kaskinen 
    77 
    88  PulseAudio is free software; you can redistribute it and/or modify 
    99  it under the terms of the GNU Lesser General Public License as published 
     
    3737#include <pthread.h> 
    3838 
    3939#include <jack/jack.h> 
     40#include <jack/ringbuffer.h> 
    4041 
    4142#include <pulse/xmalloc.h> 
    4243 
     
    5152 
    5253#include "module-jack-sink-symdef.h" 
    5354 
    54 PA_MODULE_AUTHOR("Lennart Poettering") 
     55PA_MODULE_AUTHOR("Lennart Poettering & Tanu Kaskinen") 
    5556PA_MODULE_DESCRIPTION("Jack Sink") 
    5657PA_MODULE_VERSION(PACKAGE_VERSION) 
    5758PA_MODULE_USAGE( 
     
    6061        "client_name=<jack client name> " 
    6162        "channels=<number of channels> " 
    6263        "connect=<connect ports?> " 
     64        "buffersize=<number of frames> " 
    6365        "channel_map=<channel map>") 
    6466 
    6567#define DEFAULT_SINK_NAME "jack_out" 
     68#define DEFAULT_CLIENT_NAME "PulseAudio" 
     69#define DEFAULT_RINGBUFFER_SIZE 4096 
    6670 
    6771struct userdata { 
    6872    pa_core *core; 
    6973    pa_module *module; 
    70  
     74     
    7175    pa_sink *sink; 
    72  
     76     
    7377    unsigned channels; 
    74  
    75     jack_port_t* port[PA_CHANNELS_MAX]; 
     78    unsigned frame_size; 
     79     
     80    jack_port_t * port[PA_CHANNELS_MAX]; 
    7681    jack_client_t *client; 
    77  
    78     pthread_mutex_t mutex; 
    79     pthread_cond_t cond; 
    80  
     82     
    8183    void * buffer[PA_CHANNELS_MAX]; 
    82     jack_nframes_t frames_requested; 
     84    jack_nframes_t blocksize; /* The amount of frames we process in one call; the jack buffer size. */ 
     85    int bufferptrs_are_dirty; /* Set to one when blocksize changes, and zeroed again in the process callback. */ 
     86     
     87    pthread_mutex_t blocksize_mutex; /* For avoiding blocksize changes at a wrong moment. */ 
     88     
     89    /* For signaling when there's room in the ringbuffer. */ 
     90    pthread_mutex_t cond_mutex; 
     91    pthread_cond_t ringbuffer_cond; 
     92     
     93    pthread_t filler_thread; /* Keeps the ringbuffer filled. */ 
     94     
     95    jack_ringbuffer_t *ringbuffer; 
     96     
     97    int ringbuffer_is_full; 
     98    int filler_thread_is_running; 
    8399    int quit_requested; 
    84100 
    85101    int pipe_fd_type; 
    86102    int pipe_fds[2]; 
    87103    pa_io_event *io_event; 
     104}; 
    88105 
    89     jack_nframes_t frames_in_buffer; 
    90     jack_nframes_t timestamp; 
     106struct options { 
     107    const char *sink_name; 
     108    const char *server_name; 
     109    const char *client_name; 
     110    unsigned channels; 
     111    int channels_given; 
     112    int connect; 
     113    unsigned buffersize; 
     114    pa_channel_map map; 
     115    int map_given; 
    91116}; 
    92117 
    93118static const char* const valid_modargs[] = { 
     
    96121    "client_name", 
    97122    "channels", 
    98123    "connect", 
     124    "buffersize", /* amount of internal buffering in frames */ 
    99125    "channel_map", 
    100126    NULL 
    101127}; 
    102128 
    103 static void stop_sink(struct userdata *u) { 
    104     assert (u); 
     129/* Initialization functions. */ 
     130static int parse_options(struct options *o, struct userdata *u); 
     131static void set_default_channels(struct options *o, struct userdata *u); 
     132static int create_sink(struct options *o, struct userdata *u); 
     133static void connect_ports(struct userdata *u); 
     134static int start_filling_ringbuffer(struct userdata *u); 
    105135 
    106     jack_client_close(u->client); 
    107     u->client = NULL; 
    108     u->core->mainloop->io_free(u->io_event); 
    109     u->io_event = NULL; 
    110     pa_sink_disconnect(u->sink); 
    111     pa_sink_unref(u->sink); 
    112     u->sink = NULL; 
    113     pa_module_unload_request(u->module); 
    114 
     136/* Various callbacks. */ 
     137static void jack_error_func(const char*t); 
     138static pa_usec_t sink_get_latency_cb(pa_sink *s); 
     139static int jack_process(jack_nframes_t nframes, void *arg); 
     140static int jack_blocksize_cb(jack_nframes_t nframes, void *arg); 
     141static void jack_shutdown(void *arg); 
     142static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata); 
    115143 
    116 static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) { 
    117     struct userdata *u = userdata; 
    118     char x; 
     144/* The ringbuffer filler thread runs in this function. */ 
     145static void* fill_ringbuffer(void *arg); 
    119146 
    120     assert(m); 
    121     assert(e); 
    122     assert(flags == PA_IO_EVENT_INPUT); 
    123     assert(u); 
    124     assert(u->pipe_fds[0] == fd); 
     147/* request_render asks asynchronously the mainloop to call io_event_cb. */ 
     148static void request_render(struct userdata *u); 
    125149 
    126     pa_read(fd, &x, 1, &u->pipe_fd_type); 
    127  
    128     if (u->quit_requested) { 
    129         stop_sink(u); 
    130         u->quit_requested = 0; 
    131         return; 
    132     } 
    133  
    134     pthread_mutex_lock(&u->mutex); 
    135  
    136     if (u->frames_requested > 0) { 
    137         unsigned fs; 
    138         jack_nframes_t frame_idx; 
    139         pa_memchunk chunk; 
    140  
    141         fs = pa_frame_size(&u->sink->sample_spec); 
    142  
    143         pa_sink_render_full(u->sink, u->frames_requested * fs, &chunk); 
    144  
    145         for (frame_idx = 0; frame_idx < u->frames_requested; frame_idx ++) { 
    146             unsigned c; 
    147  
    148             for (c = 0; c < u->channels; c++) { 
    149                 float *s = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx * u->channels) + c; 
    150                 float *d = ((float*) u->buffer[c]) + frame_idx; 
    151  
    152                 *d = *s; 
    153             } 
    154         } 
    155  
    156         pa_memblock_unref(chunk.memblock); 
    157  
    158         u->frames_requested = 0; 
    159  
    160         pthread_cond_signal(&u->cond); 
    161     } 
    162  
    163     pthread_mutex_unlock(&u->mutex); 
    164 } 
    165  
    166 static void request_render(struct userdata *u) { 
    167     char c = 'x'; 
    168     assert(u); 
    169  
    170     assert(u->pipe_fds[1] >= 0); 
    171     pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type); 
    172 } 
    173  
    174 static void jack_shutdown(void *arg) { 
    175     struct userdata *u = arg; 
    176     assert(u); 
    177  
    178     u->quit_requested = 1; 
    179     request_render(u); 
    180 } 
    181  
    182 static int jack_process(jack_nframes_t nframes, void *arg) { 
    183     struct userdata *u = arg; 
    184     assert(u); 
    185  
    186     if (jack_transport_query(u->client, NULL) == JackTransportRolling) { 
    187         unsigned c; 
    188  
    189         pthread_mutex_lock(&u->mutex); 
    190  
    191         u->frames_requested = nframes; 
    192  
    193         for (c = 0; c < u->channels; c++) { 
    194             u->buffer[c] = jack_port_get_buffer(u->port[c], nframes); 
    195             assert(u->buffer[c]); 
    196         } 
    197  
    198         request_render(u); 
    199  
    200         pthread_cond_wait(&u->cond, &u->mutex); 
    201  
    202         u->frames_in_buffer = nframes; 
    203         u->timestamp = jack_get_current_transport_frame(u->client); 
    204  
    205         pthread_mutex_unlock(&u->mutex); 
    206     } 
    207  
    208     return 0; 
    209 } 
    210  
    211 static pa_usec_t sink_get_latency_cb(pa_sink *s) { 
    212     struct userdata *u; 
    213     jack_nframes_t n, l, d; 
    214  
    215     assert(s); 
    216     u = s->userdata; 
    217  
    218     if (jack_transport_query(u->client, NULL) != JackTransportRolling) 
    219         return 0; 
    220  
    221     n = jack_get_current_transport_frame(u->client); 
    222  
    223     if (n < u->timestamp) 
    224         return 0; 
    225  
    226     d = n - u->timestamp; 
    227     l = jack_port_get_total_latency(u->client, u->port[0]) + u->frames_in_buffer; 
    228  
    229     if (d >= l) 
    230         return 0; 
    231  
    232     return pa_bytes_to_usec((l - d) * pa_frame_size(&s->sample_spec), &s->sample_spec); 
    233 } 
    234  
    235 static void jack_error_func(const char*t) { 
    236     pa_log_warn("JACK error >%s<", t); 
    237 } 
    238  
    239150int pa__init(pa_core *c, pa_module*m) { 
    240151    struct userdata *u = NULL; 
    241     pa_sample_spec ss; 
    242     pa_channel_map map; 
    243     pa_modargs *ma = NULL; 
    244     jack_status_t status; 
    245     const char *server_name, *client_name; 
    246     uint32_t channels = 0; 
    247     int do_connect = 1; 
     152    struct options o; 
    248153    unsigned i; 
    249     const char **ports = NULL, **p; 
    250     char *t; 
    251  
     154     
    252155    assert(c); 
    253156    assert(m); 
    254  
    255     jack_set_error_function(jack_error_func); 
    256  
    257     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { 
    258         pa_log("failed to parse module arguments."); 
    259         goto fail; 
    260     } 
    261  
    262     if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) { 
    263         pa_log("failed to parse connect= argument."); 
    264         goto fail; 
    265     } 
    266  
    267     server_name = pa_modargs_get_value(ma, "server_name", NULL); 
    268     client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio"); 
    269  
     157     
    270158    u = pa_xnew0(struct userdata, 1); 
     159     
    271160    m->userdata = u; 
    272161    u->core = c; 
    273162    u->module = m; 
    274163    u->pipe_fds[0] = u->pipe_fds[1] = -1; 
    275164    u->pipe_fd_type = 0; 
    276  
    277     pthread_mutex_init(&u->mutex, NULL); 
    278     pthread_cond_init(&u->cond, NULL); 
    279  
     165    u->ringbuffer_is_full = 0; 
     166    u->filler_thread_is_running = 0; 
     167    u->quit_requested = 0; 
     168    pthread_mutex_init(&u->blocksize_mutex, NULL); 
     169    pthread_mutex_init(&u->cond_mutex, NULL); 
     170    pthread_cond_init(&u->ringbuffer_cond, NULL); 
     171     
     172    if (parse_options(&o, u) != 0) 
     173        goto fail; 
     174     
     175    jack_set_error_function(jack_error_func); 
     176     
     177    if (!(u->client = jack_client_open(o.client_name, o.server_name ? JackServerName : JackNullOption, NULL, o.server_name))) { 
     178        pa_log("jack_client_open() failed."); 
     179        goto fail; 
     180    } 
     181    pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client)); 
     182     
     183    if (!o.channels_given) 
     184      set_default_channels(&o, u); 
     185    pa_log_info("Perhana."); 
     186    u->channels = o.channels; 
     187     
     188    if (!o.map_given) 
     189        pa_channel_map_init_auto(&o.map, u->channels, PA_CHANNEL_MAP_ALSA); 
     190    /* Check that the user gave a sensible channel map. */ 
     191    else if (o.map.channels != u->channels) { 
     192        pa_log("The given channel map doesn't match the number of channels./n" 
     193               "Please give a 'channels' parameter with as many channels as there/n" 
     194               "are channels in your 'channel_map' parameter."); 
     195        goto fail; 
     196    } 
     197     
     198    for (i = 0; i < u->channels; i++) { 
     199        if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(o.map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) { 
     200            pa_log("jack_port_register() failed."); 
     201            goto fail; 
     202        } 
     203    } 
     204    u->bufferptrs_are_dirty = 1; 
     205     
    280206    if (pipe(u->pipe_fds) < 0) { 
    281207        pa_log("pipe() failed: %s", pa_cstrerror(errno)); 
    282208        goto fail; 
    283209    } 
    284  
    285210    pa_make_nonblock_fd(u->pipe_fds[1]); 
    286  
    287     if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) { 
    288         pa_log("jack_client_open() failed."); 
     211     
     212    if (create_sink(&o, u) != 0) 
    289213        goto fail; 
     214     
     215    u->frame_size = pa_frame_size(&u->sink->sample_spec); 
     216    u->blocksize = jack_get_buffer_size(u->client); 
     217     
     218    /* If buffersize were equal to blocksize, a full block would never fit in the buffer, 
     219     * because the ringbuffer can never be totally full: one slot is always wasted. */ 
     220    if (o.buffersize <= u->blocksize) { 
     221        o.buffersize = u->blocksize + 1; 
    290222    } 
     223    /* The actual ringbuffer size will be rounded up to the nearest power of two. */ 
     224    if (!(u->ringbuffer = jack_ringbuffer_create(o.buffersize * u->frame_size))) { 
     225        pa_log("jack_ringbuffer_create() failed."); 
     226        goto fail; 
     227    } 
     228    assert(u->ringbuffer->size % u->frame_size == 0); 
     229    pa_log_info("buffersize is %u frames.", u->ringbuffer->size / u->frame_size); 
     230     
     231    jack_set_process_callback(u->client, jack_process, u); 
     232    jack_set_buffer_size_callback(u->client, jack_blocksize_cb, u); 
     233    jack_on_shutdown(u->client, jack_shutdown, u); 
     234     
     235    if (jack_activate(u->client)) { 
     236        pa_log("jack_activate() failed."); 
     237        goto fail; 
     238    } 
    291239 
    292     ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput); 
     240    if (o.connect) 
     241        connect_ports(u); 
    293242 
    294     channels = 0; 
    295     for (p = ports; *p; p++) 
    296         channels++; 
     243    u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u); 
     244     
     245    if (start_filling_ringbuffer(u) != 0) 
     246        goto fail; 
     247     
     248    return 0; 
    297249 
    298     if (!channels) 
    299         channels = c->default_sample_spec.channels
     250fail: 
     251    pa__done(c, m)
    300252 
    301     if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) { 
    302         pa_log("failed to parse channels= argument."); 
     253    return -1; 
     254
     255 
     256static int parse_options(struct options *o, struct userdata *u) { 
     257    pa_modargs *ma = NULL; 
     258     
     259    assert(o); 
     260     
     261    /* First initialize the default values */ 
     262    o->sink_name = DEFAULT_SINK_NAME; 
     263    o->server_name = NULL; 
     264    o->client_name = DEFAULT_CLIENT_NAME; 
     265    o->channels = -1; /* The actual default value is the number of physical input ports in jack, 
     266                       * or if that's zero, then the default_sample_spec.channels of the core. */ 
     267    o->connect = 1; 
     268    o->buffersize = DEFAULT_RINGBUFFER_SIZE; 
     269    pa_channel_map_init_stereo(&o->map); /* The actual default value is the default alsa mappings, 
     270                                          * but that can't be set until the channel count is known. */ 
     271     
     272    if (!(ma = pa_modargs_new(u->module->argument, valid_modargs))) { 
     273        pa_log("failed to parse module arguments."); 
    303274        goto fail; 
    304275    } 
    305  
    306     pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA); 
    307     if (pa_modargs_get_channel_map(ma, &map) < 0 || map.channels != channels) { 
    308         pa_log("failed to parse channel_map= argument."); 
     276     
     277    o->sink_name = pa_modargs_get_value(ma, "sink_name", o->sink_name); 
     278    o->server_name = pa_modargs_get_value(ma, "server_name", o->server_name); 
     279    o->client_name = pa_modargs_get_value(ma, "client_name", o->client_name); 
     280     
     281    if (pa_modargs_get_value(ma, "channels", NULL)) { 
     282        o->channels_given = 1; 
     283         
     284        if (pa_modargs_get_value_u32(ma, "channels", &o->channels) < 0 || o->channels == 0 || o->channels >= PA_CHANNELS_MAX) { 
     285            pa_log("failed to parse channels= argument."); 
     286            goto fail; 
     287        } 
     288    } 
     289     
     290    if (pa_modargs_get_value_boolean(ma, "connect", &o->connect) < 0) { 
     291        pa_log("failed to parse connect= argument."); 
    309292        goto fail; 
    310293    } 
     294     
     295    if (pa_modargs_get_value_u32(ma, "buffersize", &o->buffersize) < 0 || o->buffersize == 0) { 
     296        pa_log("failed to parse buffersize= argument."); 
     297        goto fail; 
     298    } 
     299     
     300    if (pa_modargs_get_value(ma, "channel_map", NULL)) { 
     301        o->map_given = 1; 
     302         
     303        if (pa_modargs_get_channel_map(ma, &o->map) < 0) { 
     304            pa_log("failed to parse channel_map= argument."); 
     305            goto fail; 
     306        } 
     307    } 
     308     
     309    return 0; 
     310     
     311fail: 
     312    if (ma) 
     313      pa_modargs_free(ma); 
     314     
     315    return -1; 
     316} 
    311317 
    312     pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client)); 
     318static void set_default_channels(struct options *o, struct userdata *u) { 
     319    const char **ports, **p; 
     320     
     321    assert(o); 
     322    assert(u); 
     323    assert(u->client); 
     324    assert(u->core); 
     325     
     326    o->channels = 0; 
     327     
     328    ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput); 
     329     
     330    for (p = ports; *p; p++) 
     331        o->channels++; 
     332     
     333    free(ports); 
     334     
     335    if (o->channels >= PA_CHANNELS_MAX) 
     336        o->channels = PA_CHANNELS_MAX - 1; 
     337     
     338    if (o->channels == 0) 
     339        o->channels = u->core->default_sample_spec.channels; 
     340
    313341 
    314     ss.channels = u->channels = channels; 
     342static int create_sink(struct options *o, struct userdata *u) { 
     343    pa_sample_spec ss; 
     344    char *t; 
     345     
     346    assert(o); 
     347    assert(u); 
     348    assert(u->client); 
     349     
     350    ss.channels = u->channels; 
    315351    ss.rate = jack_get_sample_rate(u->client); 
    316352    ss.format = PA_SAMPLE_FLOAT32NE; 
    317  
    318353    assert(pa_sample_spec_valid(&ss)); 
    319  
    320     for (i = 0; i < ss.channels; i++) { 
    321         if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) { 
    322             pa_log("jack_port_register() failed."); 
    323             goto fail; 
    324         } 
    325     } 
    326  
    327     if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) { 
     354     
     355    if (!(u->sink = pa_sink_new(u->core, __FILE__, o->sink_name, 0, &ss, &o->map))) { 
    328356        pa_log("failed to create sink."); 
    329         goto fail
     357        return -1
    330358    } 
    331  
     359     
    332360    u->sink->userdata = u; 
    333     pa_sink_set_owner(u->sink, m); 
     361    pa_sink_set_owner(u->sink, u->module); 
     362     
    334363    pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u->client))); 
    335364    pa_xfree(t); 
     365     
    336366    u->sink->get_latency = sink_get_latency_cb; 
     367     
     368    return 0; 
     369} 
    337370 
    338     jack_set_process_callback(u->client, jack_process, u); 
    339     jack_on_shutdown(u->client, jack_shutdown, u); 
     371static void connect_ports(struct userdata *u) { 
     372    unsigned i; 
     373    const char **ports, **p; 
     374     
     375    assert(u); 
     376    assert(u->client); 
     377     
     378    ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput); 
     379     
     380    for (i = 0, p = ports; i < u->channels; i++, p++) { 
     381        assert(u->port[i]); 
     382         
     383        if (!*p) { 
     384            pa_log("not enough physical output ports, leaving unconnected."); 
     385            break; 
     386        } 
     387         
     388        pa_log_info("connecting %s to %s", jack_port_name(u->port[i]), *p); 
     389         
     390        if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) { 
     391            pa_log("failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p); 
     392            break; 
     393        } 
     394    } 
     395     
     396    free(ports); 
     397
    340398 
    341     if (jack_activate(u->client)) { 
    342         pa_log("jack_activate() failed"); 
    343         goto fail; 
     399static int start_filling_ringbuffer(struct userdata *u) { 
     400    int retval = 0; 
     401     
     402    pthread_attr_t thread_attributes; 
     403    pthread_attr_init(&thread_attributes); 
     404    if (pthread_attr_setinheritsched(&thread_attributes, PTHREAD_INHERIT_SCHED) != 0) { 
     405        pa_log("pthread_attr_setinheritsched() failed."); 
     406        retval = -1; 
    344407    } 
     408    else if (pthread_create(&u->filler_thread, &thread_attributes, fill_ringbuffer, u) != 0) { 
     409        pa_log("pthread_create() failed."); 
     410        retval = -1; 
     411    } 
     412     
     413    u->filler_thread_is_running = 1; 
     414     
     415    pthread_attr_destroy(&thread_attributes); 
     416     
     417    return retval; 
     418} 
    345419 
    346     if (do_connect) { 
    347         for (i = 0, p = ports; i < ss.channels; i++, p++) { 
     420static void jack_error_func(const char*t) { 
     421    pa_log_warn("JACK error >%s<", t); 
     422
    348423 
    349             if (!*p) { 
    350                 pa_log("not enough physical output ports, leaving unconnected."); 
    351                 break; 
    352             } 
     424static pa_usec_t sink_get_latency_cb(pa_sink *s) { 
     425    /* The latency is the sum of the first port's latency, blocksize of jack and the ringbuffer size. 
     426     * Maybe instead of using just the first port, the max of all ports' latencies should be used?*/ 
     427    struct userdata *u; 
     428    jack_nframes_t l; 
     429     
     430    assert(s); 
     431    u = s->userdata; 
     432     
     433    l = jack_port_get_total_latency(u->client, u->port[0]) + u->blocksize + u->ringbuffer->size / u->frame_size; 
     434     
     435    return pa_bytes_to_usec(l * u->frame_size, &s->sample_spec); 
     436
    353437 
    354             pa_log_info("connecting %s to %s", jack_port_name(u->port[i]), *p); 
     438static int jack_process(jack_nframes_t nframes, void *arg) { 
     439    struct userdata *u = arg; 
     440    jack_nframes_t frame_idx_part1, frame_idx_part2; 
     441    jack_ringbuffer_data_t data[2]; /* In case the readable area in the ringbuffer is non-continuous, 
     442                                     * the data will be split in two parts. */ 
     443    unsigned c; 
     444    jack_nframes_t frames_left_over; 
     445     
     446    if (u->bufferptrs_are_dirty) { 
     447        for (c = 0; c < u->channels; c++) { 
     448            u->buffer[c] = jack_port_get_buffer(u->port[c], nframes); 
     449        } 
     450        u->bufferptrs_are_dirty = 0; 
     451    } 
     452     
     453    jack_ringbuffer_get_read_vector(u->ringbuffer, data); 
     454     
     455    /* We assume that the possible discontinuity doesn't happen in the middle 
     456     * of a frame. Should be a safe assumption. */ 
     457    assert((data[0].len % u->frame_size == 0) || (data[1].len == 0)); 
     458     
     459    /* Copy from the first part of data until enough frames are copied or the first part ends. */ 
     460    for (frame_idx_part1 = 0; frame_idx_part1 < nframes && ((frame_idx_part1 + 1) * u->frame_size) <= data[0].len; frame_idx_part1++) { 
     461        for (c = 0; c < u->channels; c++) { 
     462            float *s = ((float*) data[0].buf) + (frame_idx_part1 * u->channels) + c; 
     463            float *d = ((float*) u->buffer[c]) + frame_idx_part1; 
     464             
     465            *d = *s; 
     466        } 
     467    } 
     468     
     469    frames_left_over = nframes - frame_idx_part1; 
     470     
     471    /* Copy from the second part of data until enough frames are copied or the second part ends. */ 
     472    for (frame_idx_part2 = 0; frame_idx_part2 < frames_left_over && ((frame_idx_part2 + 1) * u->frame_size) <= data[1].len; frame_idx_part2++) { 
     473        for (c = 0; c < u->channels; c++) { 
     474            float *s = ((float*) data[1].buf) + (frame_idx_part2 * u->channels) + c; 
     475            float *d = ((float*) u->buffer[c]) + frame_idx_part1 + frame_idx_part2; 
     476             
     477            *d = *s; 
     478        } 
     479    } 
     480     
     481    frames_left_over -= frame_idx_part2; 
     482     
     483    /* If there's still frames left, fill the buffers with zeros. */ 
     484    for (; frames_left_over > 0; frames_left_over--) { 
     485        for (c = 0; c < u->channels; c++) { 
     486            float *d = ((float*) u->buffer[c]) + (nframes - frames_left_over); 
     487             
     488            *d = 0.0; 
     489        } 
     490    } 
     491     
     492    jack_ringbuffer_read_advance(u->ringbuffer, (frame_idx_part1 + frame_idx_part2) * u->frame_size); 
     493     
     494    /* Tell the rendering part that there is room in the ringbuffer. */ 
     495    u->ringbuffer_is_full = 0; 
     496    pthread_cond_signal(&u->ringbuffer_cond); 
     497     
     498    return 0; 
     499
    355500 
    356             if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) { 
    357                 pa_log("failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p); 
    358                 break; 
    359             } 
     501static int jack_blocksize_cb(jack_nframes_t nframes, void *arg) { 
     502    /* This gets called in the processing thread, so do we have to be realtime safe? 
     503     * No, we can do whatever we want. User gets silence while we do it. 
     504     *  
     505     * In addition to just updating the blocksize field in userdata, we have to create 
     506     * a new ringbuffer, if the new blocksize is bigger or equal to the old ringbuffer size. */ 
     507    struct userdata *u = arg; 
     508     
     509    assert(u); 
     510     
     511    /* We don't want to change the blocksize and the ringbuffer while rendering 
     512     * is going on. */ 
     513    pthread_mutex_lock(&u->blocksize_mutex); 
     514     
     515    u->bufferptrs_are_dirty = 1; 
     516     
     517    u->blocksize = nframes; 
     518     
     519    if ((u->ringbuffer->size / u->frame_size) <= nframes) { 
     520        /* We have to create a new ringbuffer. What are we going to do with the old 
     521         * data in the old buffer? We throw it away, because we're lazy coders. 
     522         * The listening experience is likely to get ruined anyway during the 
     523         * blocksize change. */ 
     524        jack_ringbuffer_free(u->ringbuffer); 
     525         
     526        /* The actual ringbuffer size will be rounded up to the nearest power of two. */ 
     527        if (!(u->ringbuffer = jack_ringbuffer_create((nframes + 1) * u->frame_size))) { 
     528            pa_log("jack_ringbuffer_create() failed while changing the blocksize, module exiting."); 
     529            jack_client_close(u->client); 
     530            u->quit_requested = 1; 
    360531        } 
    361  
     532        assert(u->ringbuffer->size % u->frame_size == 0); 
     533        pa_log_info("buffersize is %u frames.", u->ringbuffer->size / u->frame_size); 
    362534    } 
     535     
     536    pthread_mutex_unlock(&u->blocksize_mutex); 
     537     
     538    return 0; 
     539} 
    363540 
    364     u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u); 
     541static void jack_shutdown(void *arg) { 
     542    struct userdata *u = arg; 
     543    assert(u); 
    365544 
    366     free(ports); 
    367     pa_modargs_free(ma); 
     545    u->quit_requested = 1; 
     546    request_render(u); 
     547
    368548 
    369     return 0; 
     549static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) { 
     550    struct userdata *u = userdata; 
     551    char x; 
     552    jack_ringbuffer_data_t buffer[2]; /* The write area in the ringbuffer may be 
     553                                       * split in two parts. */ 
     554    pa_memchunk chunk; /* This is the data source. */ 
     555    jack_nframes_t part1_length, part2_length; 
     556    unsigned frame_idx_part1, frame_idx_part2, c; 
     557     
     558    assert(m); 
     559    assert(e); 
     560    assert(flags == PA_IO_EVENT_INPUT); 
     561    assert(u); 
     562    assert(u->pipe_fds[0] == fd); 
     563     
     564    pa_read(fd, &x, 1, &u->pipe_fd_type); 
    370565 
    371 fail: 
    372     if (ma) 
    373         pa_modargs_free(ma); 
     566    if (u->quit_requested) { 
     567        pa_module_unload_request(u->module); 
     568        return; 
     569    } 
     570     
     571    /* No blocksize changes during rendering, please. */ 
     572    pthread_mutex_lock(&u->blocksize_mutex); 
     573     
     574    jack_ringbuffer_get_write_vector(u->ringbuffer, buffer); 
     575    assert((buffer[0].len % u->frame_size == 0) || (buffer[1].len == 0)); 
     576     
     577    part1_length = buffer[0].len / u->frame_size; 
     578    part2_length = buffer[1].len / u->frame_size; 
     579     
     580    /* pa_sink_render_full doesn't accept zero length, so we have do the copying 
     581     * only if there's data to copy, which actually makes a kind of sense. */ 
     582    if (part1_length > 0 || part2_length > 0) { 
     583        pa_sink_render_full(u->sink, (part1_length + part2_length) * u->frame_size, &chunk); 
     584         
     585        /* Write to the first part of the buffer. */ 
     586        for (frame_idx_part1 = 0; (frame_idx_part1 < part1_length); frame_idx_part1++) { 
     587            for (c = 0; c < u->channels; c++) { 
     588                float *s = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx_part1 * u->channels) + c; 
     589                float *d = ((float*) buffer[0].buf) + (frame_idx_part1 * u->channels) + c; 
     590                 
     591                *d = *s; 
     592            } 
     593        } 
     594         
     595        /* Write to the second part of the buffer. */ 
     596        for (frame_idx_part2 = 0; frame_idx_part2 < part2_length; frame_idx_part2++) { 
     597            for (c = 0; c < u->channels; c++) { 
     598                float *s = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + ((frame_idx_part1 + frame_idx_part2) * u->channels) + c; 
     599                float *d = ((float*) buffer[1].buf) + (frame_idx_part2 * u->channels) + c; 
     600                 
     601                *d = *s; 
     602            } 
     603        } 
     604         
     605        pa_memblock_unref(chunk.memblock); 
     606         
     607        jack_ringbuffer_write_advance(u->ringbuffer, (part1_length + part2_length) * u->frame_size); 
     608    } 
     609     
     610    /* Blocksize can be changed again. */ 
     611    pthread_mutex_unlock(&u->blocksize_mutex); 
     612
    374613 
    375     free(ports); 
     614static void* fill_ringbuffer(void *arg) { 
     615    struct userdata *u = arg; 
     616    assert(u); 
     617     
     618    while (!u->quit_requested) { 
     619        if (u->ringbuffer_is_full) { 
     620            pthread_mutex_lock(&u->cond_mutex); 
     621            pthread_cond_wait(&u->ringbuffer_cond, &u->cond_mutex); 
     622            pthread_mutex_unlock(&u->cond_mutex); 
     623        } 
     624        u->ringbuffer_is_full = 1; /* No, it's not yet full, but this must be set to one as 
     625                                    * soon as possible, because if the jack thread manages 
     626                                    * to process another block before we set this to one, 
     627                                    * we may end up waiting without a reason. */ 
     628        request_render(u); 
     629    } 
     630     
     631    return NULL; 
     632
    376633 
    377     pa__done(c, m); 
     634static void request_render(struct userdata *u) { 
     635    char c = 'x'; 
     636    assert(u); 
    378637 
    379     return -1; 
     638    assert(u->pipe_fds[1] >= 0); 
     639    pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type); 
    380640} 
    381641 
    382642void pa__done(pa_core *c, pa_module*m) { 
     
    385645 
    386646    if (!(u = m->userdata)) 
    387647        return; 
    388  
     648     
     649    if (u->filler_thread_is_running) { 
     650        u->quit_requested = 1; 
     651        pthread_cond_signal(&u->ringbuffer_cond); 
     652        pthread_join(u->filler_thread, NULL); 
     653    } 
     654     
    389655    if (u->client) 
    390656        jack_client_close(u->client); 
    391657 
     
    396662        pa_sink_disconnect(u->sink); 
    397663        pa_sink_unref(u->sink); 
    398664    } 
     665     
     666    if (u->ringbuffer) 
     667        jack_ringbuffer_free(u->ringbuffer); 
    399668 
    400669    if (u->pipe_fds[0] >= 0) 
    401670        close(u->pipe_fds[0]); 
    402671    if (u->pipe_fds[1] >= 0) 
    403672        close(u->pipe_fds[1]); 
    404  
    405     pthread_mutex_destroy(&u->mutex); 
    406     pthread_cond_destroy(&u->cond); 
     673     
     674    pthread_mutex_destroy(&u->blocksize_mutex); 
     675    pthread_cond_destroy(&u->ringbuffer_cond); 
     676    pthread_mutex_destroy(&u->cond_mutex); 
    407677    pa_xfree(u); 
    408678}