| 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; |
|---|
| 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. */ |
|---|
| | 137 | static void jack_error_func(const char*t); |
|---|
| | 138 | static pa_usec_t sink_get_latency_cb(pa_sink *s); |
|---|
| | 139 | static int jack_process(jack_nframes_t nframes, void *arg); |
|---|
| | 140 | static int jack_blocksize_cb(jack_nframes_t nframes, void *arg); |
|---|
| | 141 | static void jack_shutdown(void *arg); |
|---|
| | 142 | static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata); |
|---|
| 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 | | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| | 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 | } |
|---|
| 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 | |
|---|
| | 256 | static 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."); |
|---|
| 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."); |
|---|
| 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))) { |
|---|
| 338 | | jack_set_process_callback(u->client, jack_process, u); |
|---|
| 339 | | jack_on_shutdown(u->client, jack_shutdown, u); |
|---|
| | 371 | static 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 | } |
|---|
| 349 | | if (!*p) { |
|---|
| 350 | | pa_log("not enough physical output ports, leaving unconnected."); |
|---|
| 351 | | break; |
|---|
| 352 | | } |
|---|
| | 424 | static 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 | } |
|---|
| 354 | | pa_log_info("connecting %s to %s", jack_port_name(u->port[i]), *p); |
|---|
| | 438 | static 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 | } |
|---|
| 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 | | } |
|---|
| | 501 | static 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; |
|---|
| 369 | | return 0; |
|---|
| | 549 | static 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); |
|---|
| 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 | } |
|---|
| 375 | | free(ports); |
|---|
| | 614 | static 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 | } |
|---|