386 lines
8.6 KiB
C
Raw Permalink Normal View History

2012-04-21 23:13:50 +02:00
/* RetroArch - A frontend for libretro.
2014-01-01 01:50:59 +01:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
*
2012-04-21 23:13:50 +02:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-01-01 03:53:30 +01:00
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
2012-04-21 23:13:50 +02:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-01-01 03:53:30 +01:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 23:31:57 +02:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-01-01 03:53:30 +01:00
* If not, see <http://www.gnu.org/licenses/>.
*/
2017-04-29 13:20:50 +02:00
#include <stdio.h>
2015-09-14 03:44:06 +02:00
#include <stdint.h>
2011-01-01 03:53:30 +01:00
#include <stdlib.h>
2015-09-14 03:44:06 +02:00
#include <string.h>
2011-01-01 03:53:30 +01:00
#include <jack/jack.h>
#include <jack/types.h>
#include <jack/ringbuffer.h>
2015-09-14 03:44:06 +02:00
#include <boolean.h>
2015-03-15 05:10:13 +01:00
#include <rthreads/rthreads.h>
2011-01-01 03:53:30 +01:00
#include "../../configuration.h"
#include "../audio_driver.h"
2015-11-23 12:03:38 +01:00
#include "../../verbosity.h"
2015-09-14 03:44:06 +02:00
#define FRAMES(x) (x / (sizeof(float) * 2))
2011-01-01 03:53:30 +01:00
typedef struct jack
{
jack_client_t *client;
jack_port_t *ports[2];
jack_ringbuffer_t *buffer;
2016-05-11 19:02:43 +02:00
#ifdef HAVE_THREADS
2015-03-15 05:10:13 +01:00
scond_t *cond;
slock_t *cond_lock;
2016-05-11 19:02:43 +02:00
#endif
2012-02-20 18:39:50 +01:00
size_t buffer_size;
volatile bool shutdown;
bool nonblock;
bool is_paused;
2011-01-01 03:53:30 +01:00
} jack_t;
static size_t read_deinterleaved(float *dst[2], jack_nframes_t dst_offset,
jack_ringbuffer_data_t buf, jack_nframes_t nframes)
{
int i;
jack_nframes_t j, frames_avail;
const float *src = (const float *)buf.buf;
if (nframes <= 0)
return 0;
frames_avail = FRAMES(buf.len);
nframes = nframes < frames_avail ? nframes : frames_avail;
for (j = 0; j < nframes; j++)
for (i = 0; i < 2; i++)
dst[i][dst_offset + j] = *src++;
return nframes;
}
2011-01-01 03:53:30 +01:00
static int process_cb(jack_nframes_t nframes, void *data)
{
2013-10-22 21:26:33 +02:00
int i;
jack_nframes_t read = 0;
2011-12-24 13:46:12 +01:00
jack_t *jd = (jack_t*)data;
jack_ringbuffer_data_t buf[2];
float *dst[2];
2015-01-12 05:05:56 +01:00
2011-01-01 03:53:30 +01:00
if (nframes <= 0)
{
2016-05-11 19:02:43 +02:00
#ifdef HAVE_THREADS
2015-03-15 05:10:13 +01:00
scond_signal(jd->cond);
2016-05-11 19:02:43 +02:00
#endif
2011-01-01 03:53:30 +01:00
return 0;
}
for (i = 0; i < 2; i++)
dst[i] = (float *)jack_port_get_buffer(jd->ports[i], nframes);
2011-01-01 03:53:30 +01:00
jack_ringbuffer_get_read_vector(jd->buffer, buf);
2011-01-01 03:53:30 +01:00
2013-10-22 21:26:33 +02:00
for (i = 0; i < 2; i++)
read += read_deinterleaved(dst, read, buf[i], nframes - read);
2016-09-08 11:59:44 +02:00
jack_ringbuffer_read_advance(jd->buffer, read * sizeof(float) * 2);
for (; read < nframes; read++)
for (i = 0; i < 2; i++)
dst[i][read] = 0.0f;
2011-01-01 03:53:30 +01:00
2016-05-11 19:02:43 +02:00
#ifdef HAVE_THREADS
2015-03-15 05:10:13 +01:00
scond_signal(jd->cond);
2016-05-11 19:02:43 +02:00
#endif
2011-01-01 03:53:30 +01:00
return 0;
}
static void shutdown_cb(void *data)
{
2011-12-24 13:46:12 +01:00
jack_t *jd = (jack_t*)data;
2015-01-12 05:05:56 +01:00
if (!jd)
return;
2011-01-01 03:53:30 +01:00
jd->shutdown = true;
2016-05-11 19:02:43 +02:00
#ifdef HAVE_THREADS
2015-03-15 05:10:13 +01:00
scond_signal(jd->cond);
2016-05-11 19:02:43 +02:00
#endif
2011-01-01 03:53:30 +01:00
}
static int parse_ports(char **dest_ports, const char **jports)
2011-01-01 03:53:30 +01:00
{
2013-10-22 21:26:33 +02:00
int i;
2016-12-06 07:14:34 +01:00
char *save = NULL;
int parsed = 0;
settings_t *settings = config_get_ptr();
char *audio_device_cpy = strdup(settings->arrays.audio_device);
2016-12-06 07:14:34 +01:00
const char *con = strtok_r(audio_device_cpy, ",", &save);
2013-10-22 21:26:33 +02:00
2011-01-01 03:53:30 +01:00
if (con)
dest_ports[parsed++] = strdup(con);
con = strtok_r(NULL, ",", &save);
2011-01-01 03:53:30 +01:00
if (con)
dest_ports[parsed++] = strdup(con);
2011-01-01 03:53:30 +01:00
2013-10-22 21:26:33 +02:00
for (i = parsed; i < 2; i++)
dest_ports[i] = strdup(jports[i]);
free(audio_device_cpy);
return 2;
}
static size_t find_buffersize(jack_t *jd, int latency, unsigned out_rate)
{
2015-01-12 05:05:56 +01:00
jack_latency_range_t range;
2016-09-08 11:41:58 +02:00
int i, buffer_frames, min_buffer_frames;
int jack_latency = 0;
int frames = latency * out_rate / 1000;
2013-10-22 21:26:33 +02:00
for (i = 0; i < 2; i++)
2011-03-06 16:58:33 +01:00
{
jack_port_get_latency_range(jd->ports[i], JackPlaybackLatency, &range);
2011-12-24 13:46:12 +01:00
if ((int)range.max > jack_latency)
2011-03-06 16:58:33 +01:00
jack_latency = range.max;
}
2017-03-24 01:46:28 +01:00
RARCH_LOG("[JACK]: Jack latency is %d frames.\n", jack_latency);
2017-03-24 01:46:28 +01:00
buffer_frames = frames - jack_latency;
2015-01-12 05:05:56 +01:00
min_buffer_frames = jack_get_buffer_size(jd->client) * 2;
2017-03-24 01:46:28 +01:00
RARCH_LOG("[JACK]: Minimum buffer size is %d frames.\n", min_buffer_frames);
if (buffer_frames < min_buffer_frames)
buffer_frames = min_buffer_frames;
return buffer_frames * sizeof(jack_default_audio_sample_t);
2011-01-01 03:53:30 +01:00
}
static void *ja_init(const char *device,
unsigned rate, unsigned latency,
unsigned block_frames,
unsigned *new_rate)
2011-01-01 03:53:30 +01:00
{
2013-10-22 21:26:33 +02:00
int i;
2015-01-12 05:05:56 +01:00
char *dest_ports[2];
2016-09-08 11:41:58 +02:00
const char **jports = NULL;
size_t bufsize = 0;
int parsed = 0;
jack_t *jd = (jack_t*)calloc(1, sizeof(jack_t));
2015-01-12 05:05:56 +01:00
if (!jd)
2011-01-01 03:53:30 +01:00
return NULL;
2016-05-11 19:02:43 +02:00
#ifdef HAVE_THREADS
2015-03-15 05:10:13 +01:00
jd->cond = scond_new();
jd->cond_lock = slock_new();
2016-05-11 19:02:43 +02:00
#endif
2011-01-01 03:53:30 +01:00
2012-09-07 21:56:26 +02:00
jd->client = jack_client_open("RetroArch", JackNullOption, NULL);
2020-01-04 10:39:33 +01:00
if (!jd->client)
2011-01-01 03:53:30 +01:00
goto error;
*new_rate = jack_get_sample_rate(jd->client);
2011-01-01 03:53:30 +01:00
jack_set_process_callback(jd->client, process_cb, jd);
jack_on_shutdown(jd->client, shutdown_cb, jd);
jd->ports[0] = jack_port_register(jd->client, "left", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
jd->ports[1] = jack_port_register(jd->client, "right", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
2020-01-04 10:39:33 +01:00
if (!jd->ports[0] || !jd->ports[1])
2011-01-01 03:53:30 +01:00
{
2017-03-24 01:46:28 +01:00
RARCH_ERR("[JACK]: Failed to register ports.\n");
2011-01-01 03:53:30 +01:00
goto error;
}
2011-01-01 03:53:30 +01:00
jports = jack_get_ports(jd->client, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
2020-01-04 10:39:33 +01:00
if (!jports)
2011-01-01 03:53:30 +01:00
{
2017-03-24 01:46:28 +01:00
RARCH_ERR("[JACK]: Failed to get ports.\n");
2011-01-01 03:53:30 +01:00
goto error;
}
2017-03-24 01:46:28 +01:00
bufsize = find_buffersize(jd, latency, *new_rate);
2012-02-20 18:39:50 +01:00
jd->buffer_size = bufsize;
2017-03-24 01:46:28 +01:00
RARCH_LOG("[JACK]: Internal buffer size: %d frames.\n", (int)(bufsize / sizeof(jack_default_audio_sample_t)));
jd->buffer = jack_ringbuffer_create(bufsize);
if (!jd->buffer)
{
RARCH_ERR("[JACK]: Failed to create buffers.\n");
goto error;
}
parsed = parse_ports(dest_ports, jports);
2011-01-01 03:53:30 +01:00
if (jack_activate(jd->client) < 0)
{
2017-03-24 01:46:28 +01:00
RARCH_ERR("[JACK]: Failed to activate Jack...\n");
2011-01-01 03:53:30 +01:00
goto error;
}
2013-10-22 21:26:33 +02:00
for (i = 0; i < 2; i++)
2011-01-01 03:53:30 +01:00
{
if (jack_connect(jd->client, jack_port_name(jd->ports[i]), dest_ports[i]))
{
2017-03-24 01:46:28 +01:00
RARCH_ERR("[JACK]: Failed to connect to Jack port.\n");
2011-01-01 03:53:30 +01:00
goto error;
}
}
2013-10-22 21:26:33 +02:00
for (i = 0; i < parsed; i++)
free(dest_ports[i]);
2011-01-01 03:53:30 +01:00
jack_free(jports);
return jd;
error:
2016-09-22 20:45:24 +02:00
for (i = 0; i < parsed; i++)
free(dest_ports[i]);
2020-01-04 10:39:33 +01:00
if (jports)
2011-01-01 03:53:30 +01:00
jack_free(jports);
2016-05-23 22:17:28 +02:00
free(jd);
2011-01-01 03:53:30 +01:00
return NULL;
}
static ssize_t ja_write(void *data, const void *buf_, size_t size)
2011-01-01 03:53:30 +01:00
{
jack_t *jd = (jack_t*)data;
const char *buf = (const char *)buf_;
size_t written = 0;
2011-01-01 03:53:30 +01:00
while (size > 0)
2011-01-01 03:53:30 +01:00
{
size_t avail, to_write;
2011-01-01 03:53:30 +01:00
if (jd->shutdown)
return 0;
avail = jack_ringbuffer_write_space(jd->buffer);
2011-01-01 03:53:30 +01:00
to_write = size < avail ? size : avail;
/* make sure to only write multiples of the sample size */
to_write = (to_write / sizeof(float)) * sizeof(float);
if (to_write > 0)
2011-01-01 04:50:35 +01:00
{
jack_ringbuffer_write(jd->buffer, buf, to_write);
buf += to_write;
size -= to_write;
written += to_write;
2011-01-01 04:50:35 +01:00
}
else if (!jd->nonblock)
2011-01-01 04:50:35 +01:00
{
#ifdef HAVE_THREADS
2015-03-15 05:10:13 +01:00
slock_lock(jd->cond_lock);
scond_wait(jd->cond, jd->cond_lock);
slock_unlock(jd->cond_lock);
2016-05-11 19:02:43 +02:00
#endif
continue;
}
else
break;
2011-01-01 03:53:30 +01:00
}
return written;
2011-01-01 03:53:30 +01:00
}
static bool ja_stop(void *data)
2011-01-01 03:53:30 +01:00
{
jack_t *jd = (jack_t*)data;
if (jd)
jd->is_paused = true;
2011-01-01 03:53:30 +01:00
return true;
}
static bool ja_alive(void *data)
{
jack_t *jd = (jack_t*)data;
2015-01-12 05:05:56 +01:00
if (!jd)
return false;
return !jd->is_paused;
}
static void ja_set_nonblock_state(void *data, bool state)
2011-01-01 03:53:30 +01:00
{
2011-12-24 13:46:12 +01:00
jack_t *jd = (jack_t*)data;
2015-01-12 05:05:56 +01:00
if (jd)
jd->nonblock = state;
2011-01-01 03:53:30 +01:00
}
static bool ja_start(void *data, bool is_shutdown)
2011-01-01 03:53:30 +01:00
{
jack_t *jd = (jack_t*)data;
if (jd)
jd->is_paused = false;
2011-01-01 03:53:30 +01:00
return true;
}
static void ja_free(void *data)
2011-01-01 03:53:30 +01:00
{
2013-10-22 21:26:33 +02:00
int i;
2011-12-24 13:46:12 +01:00
jack_t *jd = (jack_t*)data;
2011-01-01 03:53:30 +01:00
2011-01-01 04:50:35 +01:00
jd->shutdown = true;
2020-01-04 10:39:33 +01:00
if (jd->client)
2011-01-01 03:53:30 +01:00
{
jack_deactivate(jd->client);
jack_client_close(jd->client);
}
if (jd->buffer)
jack_ringbuffer_free(jd->buffer);
2011-01-01 03:53:30 +01:00
2016-05-11 19:02:43 +02:00
#ifdef HAVE_THREADS
2015-03-15 05:10:13 +01:00
if (jd->cond_lock)
slock_free(jd->cond_lock);
if (jd->cond)
scond_free(jd->cond);
2016-05-11 19:02:43 +02:00
#endif
2011-01-01 03:53:30 +01:00
free(jd);
}
static bool ja_use_float(void *data)
{
(void)data;
return true;
}
2012-02-20 18:39:50 +01:00
static size_t ja_write_avail(void *data)
{
jack_t *jd = (jack_t*)data;
return jack_ringbuffer_write_space(jd->buffer);
2012-02-20 18:39:50 +01:00
}
static size_t ja_buffer_size(void *data)
{
jack_t *jd = (jack_t*)data;
return jd->buffer_size;
}
2014-09-11 07:06:20 +02:00
audio_driver_t audio_jack = {
2011-12-24 13:46:12 +01:00
ja_init,
ja_write,
ja_stop,
ja_start,
ja_alive,
2011-12-24 13:46:12 +01:00
ja_set_nonblock_state,
ja_free,
ja_use_float,
2012-02-20 18:39:50 +01:00
"jack",
NULL,
NULL,
2012-02-20 18:39:50 +01:00
ja_write_avail,
ja_buffer_size,
2011-01-01 03:53:30 +01:00
};