2008-09-07 14:09:51 +00:00
|
|
|
/*
|
|
|
|
* JACK audio output driver for MPlayer
|
2004-06-25 18:11:15 +00:00
|
|
|
*
|
2007-07-09 19:50:36 +00:00
|
|
|
* Copyleft 2001 by Felix Bünemann (atmosfear@users.sf.net)
|
|
|
|
* and Reimar Döffinger (Reimar.Doeffinger@stud.uni-karlsruhe.de)
|
2008-09-07 14:09:51 +00:00
|
|
|
*
|
2013-11-11 13:33:32 +00:00
|
|
|
* Copyleft 2013 by William Light <wrl@illest.net> for the mpv project
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2008-09-07 14:09:51 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2008-09-07 14:09:51 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2008-09-07 14:09:51 +00:00
|
|
|
* 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 General Public License along
|
2015-04-13 07:36:54 +00:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2004-06-25 18:11:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2005-06-01 09:20:46 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-06-05 08:37:48 +00:00
|
|
|
#include <unistd.h>
|
2004-06-25 18:11:15 +00:00
|
|
|
|
2005-01-12 22:00:02 +00:00
|
|
|
#include "config.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2005-06-01 09:20:46 +00:00
|
|
|
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "ao.h"
|
2014-03-07 14:24:32 +00:00
|
|
|
#include "internal.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "audio/format.h"
|
2017-03-16 21:23:07 +00:00
|
|
|
#include "osdep/atomic.h"
|
2005-06-01 09:20:46 +00:00
|
|
|
#include "osdep/timer.h"
|
2016-09-05 19:04:41 +00:00
|
|
|
#include "options/m_config.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_option.h"
|
2004-06-25 18:11:15 +00:00
|
|
|
|
2005-06-01 09:20:46 +00:00
|
|
|
#include <jack/jack.h>
|
|
|
|
|
2017-10-10 13:51:16 +00:00
|
|
|
#if !HAVE_GPL
|
|
|
|
#error GPL only
|
|
|
|
#endif
|
|
|
|
|
2016-09-05 19:04:41 +00:00
|
|
|
struct jack_opts {
|
|
|
|
char *port;
|
|
|
|
char *client_name;
|
2013-07-21 22:03:57 +00:00
|
|
|
int connect;
|
|
|
|
int autostart;
|
|
|
|
int stdlayout;
|
2016-09-05 19:04:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define OPT_BASE_STRUCT struct jack_opts
|
|
|
|
static const struct m_sub_options ao_jack_conf = {
|
|
|
|
.opts = (const struct m_option[]){
|
|
|
|
OPT_STRING("jack-port", port, 0),
|
|
|
|
OPT_STRING("jack-name", client_name, 0),
|
|
|
|
OPT_FLAG("jack-autostart", autostart, 0),
|
|
|
|
OPT_FLAG("jack-connect", connect, 0),
|
|
|
|
OPT_CHOICE("jack-std-channel-layout", stdlayout, 0,
|
|
|
|
({"waveext", 0}, {"any", 1})),
|
|
|
|
{0}
|
|
|
|
},
|
|
|
|
.defaults = &(const struct jack_opts) {
|
|
|
|
.client_name = "mpv",
|
|
|
|
.connect = 1,
|
|
|
|
},
|
|
|
|
.size = sizeof(struct jack_opts),
|
|
|
|
};
|
|
|
|
|
|
|
|
struct priv {
|
|
|
|
jack_client_t *client;
|
2017-03-16 21:23:07 +00:00
|
|
|
|
|
|
|
atomic_uint graph_latency_max;
|
|
|
|
atomic_uint buffer_size;
|
|
|
|
|
2014-03-05 17:01:40 +00:00
|
|
|
int last_chunk;
|
2004-06-25 18:11:15 +00:00
|
|
|
|
2013-11-11 13:33:32 +00:00
|
|
|
int num_ports;
|
2014-03-08 23:12:53 +00:00
|
|
|
jack_port_t *ports[MP_NUM_CHANNELS];
|
audio/out/pull: remove race conditions
There were subtle and minor race conditions in the pull.c code, and AOs
using it (jack, portaudio, sdl, wasapi). Attempt to remove these.
There was at least a race condition in the ao_reset() implementation:
mp_ring_reset() was called concurrently to the audio callback. While the
ringbuffer uses atomics to allow concurrent access, the reset function
wasn't concurrency-safe (and can't easily be made to).
Fix this by stopping the audio callback before doing a reset. After
that, we can do anything without needing synchronization. The callback
is resumed when resuming playback at a later point.
Don't call driver->pause, and make driver->resume and driver->reset
start/stop the audio callback. In the initial state, the audio callback
must be disabled.
JackAudio of course is different. Maybe there is no way to suspend the
audio callback without "disconnecting" it (what jack_deactivate() would
do), so I'm not trying my luck, and implemented a really bad hack doing
active waiting until we get the audio callback into a state where it
won't interfere. Once the callback goes from AO_STATE_WAIT to NONE, we
can be sure that the callback doesn't access the ringbuffer or anything
else anymore. Since both sched_yield() and pthread_yield() apparently
are not always available, use mp_sleep_us(1) to avoid burning CPU during
active waiting.
The ao_jack.c change also removes a race condition: apparently we didn't
initialize _all_ ao fields before starting the audio callback.
In ao_wasapi.c, I'm not sure whether reset really waits for the audio
callback to return. Kovensky says it's not guaranteed, so disable the
reset callback - for now the behavior of ao_wasapi.c is like with
ao_jack.c, and active waiting is used to deal with the audio callback.
2014-05-29 00:24:17 +00:00
|
|
|
|
|
|
|
int activated;
|
2016-09-05 19:04:41 +00:00
|
|
|
|
|
|
|
struct jack_opts *opts;
|
2009-03-09 18:21:47 +00:00
|
|
|
};
|
|
|
|
|
2017-03-16 21:23:07 +00:00
|
|
|
static int graph_order_cb(void *arg)
|
|
|
|
{
|
|
|
|
struct ao *ao = arg;
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
|
|
|
jack_latency_range_t jack_latency_range;
|
|
|
|
jack_port_get_latency_range(p->ports[0], JackPlaybackLatency,
|
|
|
|
&jack_latency_range);
|
|
|
|
atomic_store(&p->graph_latency_max, jack_latency_range.max);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int buffer_size_cb(jack_nframes_t nframes, void *arg)
|
|
|
|
{
|
|
|
|
struct ao *ao = arg;
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
|
|
|
atomic_store(&p->buffer_size, nframes);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-08 23:12:53 +00:00
|
|
|
static int process(jack_nframes_t nframes, void *arg)
|
2013-06-07 13:38:08 +00:00
|
|
|
{
|
2013-06-07 13:44:49 +00:00
|
|
|
struct ao *ao = arg;
|
2013-06-07 14:42:29 +00:00
|
|
|
struct priv *p = ao->priv;
|
2013-11-11 13:33:32 +00:00
|
|
|
|
2014-03-08 23:12:53 +00:00
|
|
|
void *buffers[MP_NUM_CHANNELS];
|
2013-11-11 13:33:32 +00:00
|
|
|
|
2014-03-08 23:12:53 +00:00
|
|
|
for (int i = 0; i < p->num_ports; i++)
|
|
|
|
buffers[i] = jack_port_get_buffer(p->ports[i], nframes);
|
2014-03-05 17:01:40 +00:00
|
|
|
|
2017-03-16 21:23:07 +00:00
|
|
|
jack_nframes_t jack_latency =
|
|
|
|
atomic_load(&p->graph_latency_max) + atomic_load(&p->buffer_size);
|
|
|
|
|
2014-03-08 23:12:53 +00:00
|
|
|
int64_t end_time = mp_time_us();
|
2017-03-16 21:23:07 +00:00
|
|
|
end_time += (jack_latency + nframes) / (double)ao->samplerate * 1000000.0;
|
2013-11-11 13:33:32 +00:00
|
|
|
|
2014-03-08 23:12:53 +00:00
|
|
|
ao_read_data(ao, buffers, nframes, end_time);
|
2013-11-11 13:33:32 +00:00
|
|
|
|
2013-06-07 13:38:08 +00:00
|
|
|
return 0;
|
2005-06-01 09:20:46 +00:00
|
|
|
}
|
2004-06-25 18:11:15 +00:00
|
|
|
|
2013-11-11 11:59:38 +00:00
|
|
|
static int
|
|
|
|
connect_to_outports(struct ao *ao)
|
2013-04-05 21:06:22 +00:00
|
|
|
{
|
2013-07-21 22:03:57 +00:00
|
|
|
struct priv *p = ao->priv;
|
2013-11-11 11:59:38 +00:00
|
|
|
|
2016-09-05 19:04:41 +00:00
|
|
|
char *port_name = (p->opts->port && p->opts->port[0]) ? p->opts->port : NULL;
|
2013-06-07 13:38:08 +00:00
|
|
|
const char **matching_ports = NULL;
|
|
|
|
int port_flags = JackPortIsInput;
|
|
|
|
int i;
|
|
|
|
|
2013-11-11 11:59:38 +00:00
|
|
|
if (!port_name)
|
|
|
|
port_flags |= JackPortIsPhysical;
|
|
|
|
|
2018-09-02 01:45:56 +00:00
|
|
|
const char *port_type = JACK_DEFAULT_AUDIO_TYPE; // exclude MIDI ports
|
|
|
|
matching_ports = jack_get_ports(p->client, port_name, port_type, port_flags);
|
2013-11-11 11:59:38 +00:00
|
|
|
|
|
|
|
if (!matching_ports || !matching_ports[0]) {
|
|
|
|
MP_FATAL(ao, "no ports to connect to\n");
|
|
|
|
goto err_get_ports;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < p->num_ports && matching_ports[i]; i++) {
|
2014-03-08 23:12:53 +00:00
|
|
|
if (jack_connect(p->client, jack_port_name(p->ports[i]),
|
|
|
|
matching_ports[i]))
|
|
|
|
{
|
2013-11-11 11:59:38 +00:00
|
|
|
MP_FATAL(ao, "connecting failed\n");
|
|
|
|
goto err_connect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(matching_ports);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_connect:
|
|
|
|
free(matching_ports);
|
|
|
|
err_get_ports:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
create_ports(struct ao *ao, int nports)
|
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
2013-11-11 13:33:32 +00:00
|
|
|
char pname[30];
|
2013-11-11 11:59:38 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nports; i++) {
|
|
|
|
snprintf(pname, sizeof(pname), "out_%d", i);
|
2014-03-08 23:12:53 +00:00
|
|
|
p->ports[i] = jack_port_register(p->client, pname, JACK_DEFAULT_AUDIO_TYPE,
|
|
|
|
JackPortIsOutput, 0);
|
2013-11-11 11:59:38 +00:00
|
|
|
|
2014-03-08 23:12:53 +00:00
|
|
|
if (!p->ports[i]) {
|
2013-11-11 11:59:38 +00:00
|
|
|
MP_FATAL(ao, "not enough ports available\n");
|
|
|
|
goto err_port_register;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p->num_ports = nports;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_port_register:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
audio/out/pull: remove race conditions
There were subtle and minor race conditions in the pull.c code, and AOs
using it (jack, portaudio, sdl, wasapi). Attempt to remove these.
There was at least a race condition in the ao_reset() implementation:
mp_ring_reset() was called concurrently to the audio callback. While the
ringbuffer uses atomics to allow concurrent access, the reset function
wasn't concurrency-safe (and can't easily be made to).
Fix this by stopping the audio callback before doing a reset. After
that, we can do anything without needing synchronization. The callback
is resumed when resuming playback at a later point.
Don't call driver->pause, and make driver->resume and driver->reset
start/stop the audio callback. In the initial state, the audio callback
must be disabled.
JackAudio of course is different. Maybe there is no way to suspend the
audio callback without "disconnecting" it (what jack_deactivate() would
do), so I'm not trying my luck, and implemented a really bad hack doing
active waiting until we get the audio callback into a state where it
won't interfere. Once the callback goes from AO_STATE_WAIT to NONE, we
can be sure that the callback doesn't access the ringbuffer or anything
else anymore. Since both sched_yield() and pthread_yield() apparently
are not always available, use mp_sleep_us(1) to avoid burning CPU during
active waiting.
The ao_jack.c change also removes a race condition: apparently we didn't
initialize _all_ ao fields before starting the audio callback.
In ao_wasapi.c, I'm not sure whether reset really waits for the audio
callback to return. Kovensky says it's not guaranteed, so disable the
reset callback - for now the behavior of ao_wasapi.c is like with
ao_jack.c, and active waiting is used to deal with the audio callback.
2014-05-29 00:24:17 +00:00
|
|
|
static void resume(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
if (!p->activated) {
|
|
|
|
p->activated = true;
|
|
|
|
|
|
|
|
if (jack_activate(p->client))
|
|
|
|
MP_FATAL(ao, "activate failed\n");
|
|
|
|
|
2016-09-05 19:04:41 +00:00
|
|
|
if (p->opts->connect)
|
audio/out/pull: remove race conditions
There were subtle and minor race conditions in the pull.c code, and AOs
using it (jack, portaudio, sdl, wasapi). Attempt to remove these.
There was at least a race condition in the ao_reset() implementation:
mp_ring_reset() was called concurrently to the audio callback. While the
ringbuffer uses atomics to allow concurrent access, the reset function
wasn't concurrency-safe (and can't easily be made to).
Fix this by stopping the audio callback before doing a reset. After
that, we can do anything without needing synchronization. The callback
is resumed when resuming playback at a later point.
Don't call driver->pause, and make driver->resume and driver->reset
start/stop the audio callback. In the initial state, the audio callback
must be disabled.
JackAudio of course is different. Maybe there is no way to suspend the
audio callback without "disconnecting" it (what jack_deactivate() would
do), so I'm not trying my luck, and implemented a really bad hack doing
active waiting until we get the audio callback into a state where it
won't interfere. Once the callback goes from AO_STATE_WAIT to NONE, we
can be sure that the callback doesn't access the ringbuffer or anything
else anymore. Since both sched_yield() and pthread_yield() apparently
are not always available, use mp_sleep_us(1) to avoid burning CPU during
active waiting.
The ao_jack.c change also removes a race condition: apparently we didn't
initialize _all_ ao fields before starting the audio callback.
In ao_wasapi.c, I'm not sure whether reset really waits for the audio
callback to return. Kovensky says it's not guaranteed, so disable the
reset callback - for now the behavior of ao_wasapi.c is like with
ao_jack.c, and active waiting is used to deal with the audio callback.
2014-05-29 00:24:17 +00:00
|
|
|
connect_to_outports(ao);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-11 11:59:38 +00:00
|
|
|
static int init(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
2013-06-07 13:38:08 +00:00
|
|
|
struct mp_chmap_sel sel = {0};
|
2013-11-11 11:59:38 +00:00
|
|
|
jack_options_t open_options;
|
2013-07-07 16:37:55 +00:00
|
|
|
|
2016-09-05 19:04:41 +00:00
|
|
|
p->opts = mp_get_config_group(ao, ao->global, &ao_jack_conf);
|
|
|
|
|
2013-11-11 13:33:32 +00:00
|
|
|
ao->format = AF_FORMAT_FLOATP;
|
|
|
|
|
2016-09-05 19:04:41 +00:00
|
|
|
switch (p->opts->stdlayout) {
|
2013-11-11 11:59:38 +00:00
|
|
|
case 0:
|
2013-07-07 16:37:55 +00:00
|
|
|
mp_chmap_sel_add_waveext(&sel);
|
2013-11-11 11:59:38 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-07-21 22:03:57 +00:00
|
|
|
mp_chmap_sel_add_any(&sel);
|
2013-07-07 16:37:55 +00:00
|
|
|
}
|
|
|
|
|
2013-06-07 13:44:49 +00:00
|
|
|
if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels))
|
2013-11-11 11:59:38 +00:00
|
|
|
goto err_chmap;
|
2013-06-07 13:38:08 +00:00
|
|
|
|
2013-11-11 11:59:38 +00:00
|
|
|
open_options = JackNullOption;
|
2016-09-05 19:04:41 +00:00
|
|
|
if (!p->opts->autostart)
|
2013-06-07 13:38:08 +00:00
|
|
|
open_options |= JackNoStartServer;
|
2013-11-11 11:59:38 +00:00
|
|
|
|
2016-09-05 19:04:41 +00:00
|
|
|
p->client = jack_client_open(p->opts->client_name, open_options, NULL);
|
2013-06-07 14:42:29 +00:00
|
|
|
if (!p->client) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_FATAL(ao, "cannot open server\n");
|
2013-11-11 11:59:38 +00:00
|
|
|
goto err_client_open;
|
2013-06-03 17:38:04 +00:00
|
|
|
}
|
2013-06-07 13:38:08 +00:00
|
|
|
|
2013-11-11 11:59:38 +00:00
|
|
|
if (create_ports(ao, ao->channels.num))
|
|
|
|
goto err_create_ports;
|
|
|
|
|
2013-11-11 13:33:32 +00:00
|
|
|
jack_set_process_callback(p->client, process, ao);
|
2013-06-07 13:38:08 +00:00
|
|
|
|
2013-06-07 14:42:29 +00:00
|
|
|
ao->samplerate = jack_get_sample_rate(p->client);
|
2013-11-11 11:59:38 +00:00
|
|
|
|
2017-03-16 21:23:07 +00:00
|
|
|
jack_set_buffer_size_callback(p->client, buffer_size_cb, ao);
|
|
|
|
jack_set_graph_order_callback(p->client, graph_order_cb, ao);
|
2013-06-07 13:38:08 +00:00
|
|
|
|
2013-06-07 14:42:29 +00:00
|
|
|
if (!ao_chmap_sel_get_def(ao, &sel, &ao->channels, p->num_ports))
|
2013-11-11 11:59:38 +00:00
|
|
|
goto err_chmap_sel_get_def;
|
2013-06-07 13:38:08 +00:00
|
|
|
|
2013-06-07 13:44:49 +00:00
|
|
|
return 0;
|
2005-06-01 09:20:46 +00:00
|
|
|
|
2013-11-11 11:59:38 +00:00
|
|
|
err_chmap_sel_get_def:
|
|
|
|
err_create_ports:
|
|
|
|
jack_client_close(p->client);
|
|
|
|
err_client_open:
|
|
|
|
err_chmap:
|
2013-06-07 13:44:49 +00:00
|
|
|
return -1;
|
2005-06-01 09:20:46 +00:00
|
|
|
}
|
2004-06-25 18:11:15 +00:00
|
|
|
|
2013-06-07 13:44:49 +00:00
|
|
|
// close audio device
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao)
|
2013-06-07 13:44:49 +00:00
|
|
|
{
|
2013-06-07 14:42:29 +00:00
|
|
|
struct priv *p = ao->priv;
|
2013-11-11 13:33:32 +00:00
|
|
|
|
2013-06-07 14:42:29 +00:00
|
|
|
jack_client_close(p->client);
|
2013-06-07 13:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct ao_driver audio_out_jack = {
|
2013-10-23 17:07:27 +00:00
|
|
|
.description = "JACK audio output",
|
|
|
|
.name = "jack",
|
2013-06-07 13:44:49 +00:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
audio/out/pull: remove race conditions
There were subtle and minor race conditions in the pull.c code, and AOs
using it (jack, portaudio, sdl, wasapi). Attempt to remove these.
There was at least a race condition in the ao_reset() implementation:
mp_ring_reset() was called concurrently to the audio callback. While the
ringbuffer uses atomics to allow concurrent access, the reset function
wasn't concurrency-safe (and can't easily be made to).
Fix this by stopping the audio callback before doing a reset. After
that, we can do anything without needing synchronization. The callback
is resumed when resuming playback at a later point.
Don't call driver->pause, and make driver->resume and driver->reset
start/stop the audio callback. In the initial state, the audio callback
must be disabled.
JackAudio of course is different. Maybe there is no way to suspend the
audio callback without "disconnecting" it (what jack_deactivate() would
do), so I'm not trying my luck, and implemented a really bad hack doing
active waiting until we get the audio callback into a state where it
won't interfere. Once the callback goes from AO_STATE_WAIT to NONE, we
can be sure that the callback doesn't access the ringbuffer or anything
else anymore. Since both sched_yield() and pthread_yield() apparently
are not always available, use mp_sleep_us(1) to avoid burning CPU during
active waiting.
The ao_jack.c change also removes a race condition: apparently we didn't
initialize _all_ ao fields before starting the audio callback.
In ao_wasapi.c, I'm not sure whether reset really waits for the audio
callback to return. Kovensky says it's not guaranteed, so disable the
reset callback - for now the behavior of ao_wasapi.c is like with
ao_jack.c, and active waiting is used to deal with the audio callback.
2014-05-29 00:24:17 +00:00
|
|
|
.resume = resume,
|
2013-07-21 22:03:57 +00:00
|
|
|
.priv_size = sizeof(struct priv),
|
2016-09-05 19:04:41 +00:00
|
|
|
.global_opts = &ao_jack_conf,
|
2013-06-07 13:44:49 +00:00
|
|
|
};
|