2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2019-06-17 11:17:30 +02:00
|
|
|
* Copyright 2003-2019 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2006-10-18 04:49:13 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2006-10-18 04:49:13 +02:00
|
|
|
*/
|
|
|
|
|
2009-02-16 18:41:30 +01:00
|
|
|
#include "config.h"
|
2013-04-17 00:24:44 +02:00
|
|
|
#include "JackOutputPlugin.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2016-11-08 15:10:38 +01:00
|
|
|
#include "util/ScopeExit.hxx"
|
2014-12-25 22:18:22 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2016-03-18 14:03:00 +01:00
|
|
|
#include "util/IterableSplitString.hxx"
|
2016-11-08 15:04:11 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Domain.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2006-10-18 04:49:13 +02:00
|
|
|
#include <jack/jack.h>
|
|
|
|
#include <jack/types.h>
|
|
|
|
#include <jack/ringbuffer.h>
|
|
|
|
|
2014-12-04 09:14:11 +01:00
|
|
|
#include <unistd.h> /* for usleep() */
|
2009-01-02 17:23:10 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-12-24 09:52:32 +01:00
|
|
|
static constexpr unsigned MAX_PORTS = 16;
|
2009-11-06 18:55:26 +01:00
|
|
|
|
2014-12-24 15:14:51 +01:00
|
|
|
static constexpr size_t jack_sample_size = sizeof(jack_default_audio_sample_t);
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
struct JackOutput final : AudioOutput {
|
2009-11-05 20:01:18 +01:00
|
|
|
/**
|
|
|
|
* libjack options passed to jack_client_open().
|
|
|
|
*/
|
2016-11-08 15:04:11 +01:00
|
|
|
jack_options_t options = JackNullOption;
|
2009-11-05 20:01:18 +01:00
|
|
|
|
2009-02-25 17:32:58 +01:00
|
|
|
const char *name;
|
2008-09-24 07:20:55 +02:00
|
|
|
|
2016-11-08 15:04:11 +01:00
|
|
|
const char *const server_name;
|
2009-11-07 17:26:21 +01:00
|
|
|
|
2008-08-26 08:27:16 +02:00
|
|
|
/* configuration */
|
2009-11-06 01:35:19 +01:00
|
|
|
|
2014-12-03 21:39:45 +01:00
|
|
|
std::string source_ports[MAX_PORTS];
|
2009-11-06 18:58:35 +01:00
|
|
|
unsigned num_source_ports;
|
|
|
|
|
2014-12-03 21:39:45 +01:00
|
|
|
std::string destination_ports[MAX_PORTS];
|
2009-11-06 18:55:26 +01:00
|
|
|
unsigned num_destination_ports;
|
2019-07-15 18:52:43 +02:00
|
|
|
/* overrides num_destination_ports*/
|
|
|
|
bool auto_destination_ports;
|
2009-11-06 01:35:19 +01:00
|
|
|
|
2009-10-21 21:39:26 +02:00
|
|
|
size_t ringbuffer_size;
|
2008-08-26 08:27:16 +02:00
|
|
|
|
2009-01-30 19:43:31 +01:00
|
|
|
/* the current audio format */
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat audio_format;
|
2008-09-24 07:20:36 +02:00
|
|
|
|
2008-08-26 08:27:16 +02:00
|
|
|
/* jack library stuff */
|
2009-11-06 18:58:35 +01:00
|
|
|
jack_port_t *ports[MAX_PORTS];
|
2006-10-18 04:49:13 +02:00
|
|
|
jack_client_t *client;
|
2009-11-06 18:58:35 +01:00
|
|
|
jack_ringbuffer_t *ringbuffer[MAX_PORTS];
|
2009-01-29 23:15:55 +01:00
|
|
|
|
2009-01-29 23:15:27 +01:00
|
|
|
bool shutdown;
|
2009-10-21 18:33:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* While this flag is set, the "process" callback generates
|
|
|
|
* silence.
|
|
|
|
*/
|
|
|
|
bool pause;
|
2013-04-17 00:24:44 +02:00
|
|
|
|
2016-11-08 15:04:11 +01:00
|
|
|
explicit JackOutput(const ConfigBlock &block);
|
2014-01-28 23:39:48 +01:00
|
|
|
|
2016-11-08 15:04:11 +01:00
|
|
|
/**
|
|
|
|
* Connect the JACK client and performs some basic setup
|
|
|
|
* (e.g. register callbacks).
|
|
|
|
*
|
2019-07-03 22:10:26 +02:00
|
|
|
* Throws on error.
|
2016-11-08 15:04:11 +01:00
|
|
|
*/
|
|
|
|
void Connect();
|
2014-12-24 15:15:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect the JACK client.
|
|
|
|
*/
|
2017-08-11 12:41:11 +02:00
|
|
|
void Disconnect() noexcept;
|
2014-12-24 15:15:53 +01:00
|
|
|
|
2017-08-11 12:41:11 +02:00
|
|
|
void Shutdown() noexcept {
|
2014-12-24 15:15:53 +01:00
|
|
|
shutdown = true;
|
2013-04-17 00:24:44 +02:00
|
|
|
}
|
2014-12-24 15:15:53 +01:00
|
|
|
|
2016-11-08 15:04:11 +01:00
|
|
|
/**
|
2019-07-03 22:10:26 +02:00
|
|
|
* Throws on error.
|
2016-11-08 15:04:11 +01:00
|
|
|
*/
|
|
|
|
void Start();
|
2017-08-11 12:41:11 +02:00
|
|
|
void Stop() noexcept;
|
2014-12-24 15:15:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the number of frames guaranteed to be available
|
|
|
|
* on all channels.
|
|
|
|
*/
|
|
|
|
gcc_pure
|
2017-05-08 14:44:49 +02:00
|
|
|
jack_nframes_t GetAvailable() const noexcept;
|
2014-12-24 15:15:53 +01:00
|
|
|
|
|
|
|
void Process(jack_nframes_t nframes);
|
|
|
|
|
2014-12-24 23:00:46 +01:00
|
|
|
/**
|
|
|
|
* @return the number of frames that were written
|
|
|
|
*/
|
2014-12-24 23:02:19 +01:00
|
|
|
size_t WriteSamples(const float *src, size_t n_frames);
|
2014-12-24 23:00:46 +01:00
|
|
|
|
2017-08-11 12:39:42 +02:00
|
|
|
/* virtual methods from class AudioOutput */
|
|
|
|
|
|
|
|
void Enable() override;
|
|
|
|
void Disable() noexcept override;
|
|
|
|
|
|
|
|
void Open(AudioFormat &new_audio_format) override;
|
|
|
|
|
|
|
|
void Close() noexcept override {
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
std::chrono::steady_clock::duration Delay() const noexcept override {
|
2017-05-23 10:58:30 +02:00
|
|
|
return pause && !shutdown
|
2016-12-28 21:44:18 +01:00
|
|
|
? std::chrono::seconds(1)
|
|
|
|
: std::chrono::steady_clock::duration::zero();
|
2014-12-29 23:45:14 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
size_t Play(const void *chunk, size_t size) override;
|
2014-12-29 23:45:14 +01:00
|
|
|
|
2017-09-08 14:45:53 +02:00
|
|
|
bool Pause() override;
|
2008-10-24 17:29:37 +02:00
|
|
|
};
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain jack_output_domain("jack_output");
|
2009-02-26 22:04:59 +01:00
|
|
|
|
2016-11-08 15:04:11 +01:00
|
|
|
/**
|
2019-07-03 22:10:26 +02:00
|
|
|
* Throws on error.
|
2016-11-08 15:04:11 +01:00
|
|
|
*/
|
|
|
|
static unsigned
|
|
|
|
parse_port_list(const char *source, std::string dest[])
|
|
|
|
{
|
|
|
|
unsigned n = 0;
|
|
|
|
for (auto i : IterableSplitString(source, ',')) {
|
|
|
|
if (n >= MAX_PORTS)
|
|
|
|
throw std::runtime_error("too many port names");
|
|
|
|
|
|
|
|
dest[n++] = std::string(i.data, i.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
throw std::runtime_error("at least one port name expected");
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
JackOutput::JackOutput(const ConfigBlock &block)
|
2017-08-09 16:58:44 +02:00
|
|
|
:AudioOutput(FLAG_ENABLE_DISABLE|FLAG_PAUSE),
|
2016-11-08 15:04:11 +01:00
|
|
|
name(block.GetBlockValue("client_name", nullptr)),
|
|
|
|
server_name(block.GetBlockValue("server_name", nullptr))
|
|
|
|
{
|
|
|
|
if (name != nullptr)
|
|
|
|
options = jack_options_t(options | JackUseExactName);
|
|
|
|
else
|
|
|
|
/* if there's a no configured client name, we don't
|
|
|
|
care about the JackUseExactName option */
|
|
|
|
name = "Music Player Daemon";
|
|
|
|
|
|
|
|
if (server_name != nullptr)
|
|
|
|
options = jack_options_t(options | JackServerName);
|
|
|
|
|
|
|
|
if (!block.GetBlockValue("autostart", false))
|
|
|
|
options = jack_options_t(options | JackNoStartServer);
|
|
|
|
|
|
|
|
/* configure the source ports */
|
|
|
|
|
|
|
|
const char *value = block.GetBlockValue("source_ports", "left,right");
|
|
|
|
num_source_ports = parse_port_list(value, source_ports);
|
|
|
|
|
|
|
|
/* configure the destination ports */
|
|
|
|
|
|
|
|
value = block.GetBlockValue("destination_ports", nullptr);
|
|
|
|
if (value == nullptr) {
|
|
|
|
/* compatibility with MPD < 0.16 */
|
|
|
|
value = block.GetBlockValue("ports", nullptr);
|
|
|
|
if (value != nullptr)
|
|
|
|
FormatWarning(jack_output_domain,
|
|
|
|
"deprecated option 'ports' in line %d",
|
|
|
|
block.line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value != nullptr) {
|
|
|
|
num_destination_ports =
|
|
|
|
parse_port_list(value, destination_ports);
|
|
|
|
} else {
|
|
|
|
num_destination_ports = 0;
|
|
|
|
}
|
|
|
|
|
2019-07-15 18:52:43 +02:00
|
|
|
auto_destination_ports = block.GetBlockValue("auto_destination_ports", true);
|
|
|
|
|
2016-11-08 15:04:11 +01:00
|
|
|
if (num_destination_ports > 0 &&
|
|
|
|
num_destination_ports != num_source_ports)
|
|
|
|
FormatWarning(jack_output_domain,
|
|
|
|
"number of source ports (%u) mismatches the "
|
|
|
|
"number of destination ports (%u) in line %d",
|
|
|
|
num_source_ports, num_destination_ports,
|
|
|
|
block.line);
|
|
|
|
|
2018-01-02 17:22:25 +01:00
|
|
|
ringbuffer_size = block.GetPositiveValue("ringbuffer_size", 32768u);
|
2016-11-08 15:04:11 +01:00
|
|
|
}
|
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
inline jack_nframes_t
|
2017-05-08 14:44:49 +02:00
|
|
|
JackOutput::GetAvailable() const noexcept
|
2010-03-10 19:18:51 +01:00
|
|
|
{
|
2014-12-24 15:15:53 +01:00
|
|
|
size_t min = jack_ringbuffer_read_space(ringbuffer[0]);
|
2010-03-10 19:18:51 +01:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
for (unsigned i = 1; i < audio_format.channels; ++i) {
|
|
|
|
size_t current = jack_ringbuffer_read_space(ringbuffer[i]);
|
2010-03-10 19:18:51 +01:00
|
|
|
if (current < min)
|
|
|
|
min = current;
|
|
|
|
}
|
|
|
|
|
2011-03-16 18:08:54 +01:00
|
|
|
assert(min % jack_sample_size == 0);
|
2010-03-10 19:18:51 +01:00
|
|
|
|
2011-03-16 18:08:54 +01:00
|
|
|
return min / jack_sample_size;
|
2010-03-10 19:18:51 +01:00
|
|
|
}
|
|
|
|
|
2014-12-25 22:18:22 +01:00
|
|
|
/**
|
|
|
|
* Call jack_ringbuffer_read_advance() on all buffers in the list.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
MultiReadAdvance(ConstBuffer<jack_ringbuffer_t *> buffers,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
for (auto *i : buffers)
|
|
|
|
jack_ringbuffer_read_advance(i, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a specific amount of "silence" to the given port.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
WriteSilence(jack_port_t &port, jack_nframes_t nframes)
|
|
|
|
{
|
|
|
|
jack_default_audio_sample_t *out =
|
|
|
|
(jack_default_audio_sample_t *)
|
|
|
|
jack_port_get_buffer(&port, nframes);
|
|
|
|
if (out == nullptr)
|
|
|
|
/* workaround for libjack1 bug: if the server
|
|
|
|
connection fails, the process callback is invoked
|
|
|
|
anyway, but unable to get a buffer */
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::fill_n(out, nframes, 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a specific amount of "silence" to all ports in the list.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
MultiWriteSilence(ConstBuffer<jack_port_t *> ports, jack_nframes_t nframes)
|
|
|
|
{
|
|
|
|
for (auto *i : ports)
|
|
|
|
WriteSilence(*i, nframes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy data from the buffer to the port. If the buffer underruns,
|
|
|
|
* fill with silence.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
Copy(jack_port_t &dest, jack_nframes_t nframes,
|
|
|
|
jack_ringbuffer_t &src, jack_nframes_t available)
|
|
|
|
{
|
|
|
|
jack_default_audio_sample_t *out =
|
|
|
|
(jack_default_audio_sample_t *)
|
|
|
|
jack_port_get_buffer(&dest, nframes);
|
|
|
|
if (out == nullptr)
|
|
|
|
/* workaround for libjack1 bug: if the server
|
|
|
|
connection fails, the process callback is
|
|
|
|
invoked anyway, but unable to get a
|
|
|
|
buffer */
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* copy from buffer to port */
|
|
|
|
jack_ringbuffer_read(&src, (char *)out,
|
|
|
|
available * jack_sample_size);
|
|
|
|
|
|
|
|
/* ringbuffer underrun, fill with silence */
|
|
|
|
std::fill(out + available, out + nframes, 0.0);
|
|
|
|
}
|
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
inline void
|
|
|
|
JackOutput::Process(jack_nframes_t nframes)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2008-10-24 08:44:40 +02:00
|
|
|
if (nframes <= 0)
|
2014-12-24 15:15:53 +01:00
|
|
|
return;
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
jack_nframes_t available = GetAvailable();
|
2014-12-24 16:01:07 +01:00
|
|
|
|
2014-12-24 23:22:45 +01:00
|
|
|
const unsigned n_channels = audio_format.channels;
|
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
if (pause) {
|
2010-03-10 19:48:27 +01:00
|
|
|
/* empty the ring buffers */
|
|
|
|
|
2014-12-25 22:18:22 +01:00
|
|
|
MultiReadAdvance({ringbuffer, n_channels},
|
|
|
|
available * jack_sample_size);
|
2010-03-10 19:48:27 +01:00
|
|
|
|
2009-10-21 18:33:05 +02:00
|
|
|
/* generate silence while MPD is paused */
|
|
|
|
|
2014-12-25 22:18:22 +01:00
|
|
|
MultiWriteSilence({ports, n_channels}, nframes);
|
2009-10-21 18:33:05 +02:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
return;
|
2009-10-21 18:33:05 +02:00
|
|
|
}
|
|
|
|
|
2010-03-10 19:18:51 +01:00
|
|
|
if (available > nframes)
|
|
|
|
available = nframes;
|
2007-03-23 12:07:04 +01:00
|
|
|
|
2014-12-25 22:18:22 +01:00
|
|
|
for (unsigned i = 0; i < n_channels; ++i)
|
|
|
|
Copy(*ports[i], nframes, *ringbuffer[i], available);
|
2007-04-09 22:18:11 +02:00
|
|
|
|
2009-11-06 18:58:35 +01:00
|
|
|
/* generate silence for the unused source ports */
|
|
|
|
|
2014-12-25 22:18:22 +01:00
|
|
|
MultiWriteSilence({ports + n_channels, num_source_ports - n_channels},
|
|
|
|
nframes);
|
2014-12-24 15:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mpd_jack_process(jack_nframes_t nframes, void *arg)
|
|
|
|
{
|
|
|
|
JackOutput &jo = *(JackOutput *) arg;
|
2009-11-06 18:58:35 +01:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
jo.Process(nframes);
|
2006-10-18 04:49:13 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static void
|
|
|
|
mpd_jack_shutdown(void *arg)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2014-12-24 15:15:53 +01:00
|
|
|
JackOutput &jo = *(JackOutput *) arg;
|
|
|
|
|
|
|
|
jo.Shutdown();
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static void
|
2013-08-03 21:00:50 +02:00
|
|
|
set_audioformat(JackOutput *jd, AudioFormat &audio_format)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.sample_rate = jack_get_sample_rate(jd->client);
|
2009-11-05 20:02:04 +01:00
|
|
|
|
2009-11-06 18:58:35 +01:00
|
|
|
if (jd->num_source_ports == 1)
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.channels = 1;
|
|
|
|
else if (audio_format.channels > jd->num_source_ports)
|
|
|
|
audio_format.channels = 2;
|
2008-10-24 17:36:11 +02:00
|
|
|
|
2014-12-24 22:40:00 +01:00
|
|
|
/* JACK uses 32 bit float in the range [-1 .. 1] - just like
|
|
|
|
MPD's SampleFormat::FLOAT*/
|
|
|
|
static_assert(jack_sample_size == sizeof(float), "Expected float32");
|
|
|
|
audio_format.format = SampleFormat::FLOAT;
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2008-10-24 17:29:37 +02:00
|
|
|
static void
|
|
|
|
mpd_jack_error(const char *msg)
|
2006-12-30 01:14:45 +01:00
|
|
|
{
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(jack_output_domain, msg);
|
2006-12-30 01:14:45 +01:00
|
|
|
}
|
|
|
|
|
2009-01-30 19:43:25 +01:00
|
|
|
#ifdef HAVE_JACK_SET_INFO_FUNCTION
|
2009-01-30 15:57:43 +01:00
|
|
|
static void
|
|
|
|
mpd_jack_info(const char *msg)
|
|
|
|
{
|
2013-11-04 22:20:11 +01:00
|
|
|
LogDefault(jack_output_domain, msg);
|
2009-01-30 15:57:43 +01:00
|
|
|
}
|
2009-01-30 19:43:25 +01:00
|
|
|
#endif
|
2009-01-30 15:57:43 +01:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
void
|
2017-08-11 12:41:11 +02:00
|
|
|
JackOutput::Disconnect() noexcept
|
2009-10-21 21:37:11 +02:00
|
|
|
{
|
2014-12-24 15:15:53 +01:00
|
|
|
assert(client != nullptr);
|
2009-10-21 21:37:11 +02:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
jack_deactivate(client);
|
|
|
|
jack_client_close(client);
|
|
|
|
client = nullptr;
|
2009-10-21 21:37:11 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 15:04:11 +01:00
|
|
|
void
|
|
|
|
JackOutput::Connect()
|
2009-10-21 21:37:11 +02:00
|
|
|
{
|
2014-12-24 15:15:53 +01:00
|
|
|
shutdown = false;
|
2009-10-21 21:37:11 +02:00
|
|
|
|
2014-12-24 15:21:23 +01:00
|
|
|
jack_status_t status;
|
2014-12-24 15:15:53 +01:00
|
|
|
client = jack_client_open(name, options, &status, server_name);
|
2016-11-08 15:04:11 +01:00
|
|
|
if (client == nullptr)
|
|
|
|
throw FormatRuntimeError("Failed to connect to JACK server, status=%d",
|
|
|
|
status);
|
2009-10-21 21:37:11 +02:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
jack_set_process_callback(client, mpd_jack_process, this);
|
|
|
|
jack_on_shutdown(client, mpd_jack_shutdown, this);
|
2009-10-21 21:37:11 +02:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
for (unsigned i = 0; i < num_source_ports; ++i) {
|
|
|
|
ports[i] = jack_port_register(client,
|
|
|
|
source_ports[i].c_str(),
|
|
|
|
JACK_DEFAULT_AUDIO_TYPE,
|
|
|
|
JackPortIsOutput, 0);
|
|
|
|
if (ports[i] == nullptr) {
|
|
|
|
Disconnect();
|
2016-11-08 15:04:11 +01:00
|
|
|
throw FormatRuntimeError("Cannot register output port \"%s\"",
|
|
|
|
source_ports[i].c_str());
|
2009-10-21 21:37:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
mpd_jack_test_default_device(void)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
inline void
|
|
|
|
JackOutput::Enable()
|
2014-12-24 15:15:53 +01:00
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < num_source_ports; ++i)
|
|
|
|
ringbuffer[i] = nullptr;
|
|
|
|
|
2016-11-08 15:04:11 +01:00
|
|
|
Connect();
|
2014-12-24 15:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2017-08-09 16:58:44 +02:00
|
|
|
JackOutput::Disable() noexcept
|
2014-12-24 15:15:53 +01:00
|
|
|
{
|
|
|
|
if (client != nullptr)
|
|
|
|
Disconnect();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < num_source_ports; ++i) {
|
|
|
|
if (ringbuffer[i] != nullptr) {
|
|
|
|
jack_ringbuffer_free(ringbuffer[i]);
|
|
|
|
ringbuffer[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 21:55:29 +02:00
|
|
|
static AudioOutput *
|
2017-01-25 09:47:43 +01:00
|
|
|
mpd_jack_init(EventLoop &, const ConfigBlock &block)
|
2014-12-24 15:15:53 +01:00
|
|
|
{
|
2009-01-29 23:12:08 +01:00
|
|
|
jack_set_error_function(mpd_jack_error);
|
2009-01-30 19:43:25 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_JACK_SET_INFO_FUNCTION
|
2009-01-30 15:57:43 +01:00
|
|
|
jack_set_info_function(mpd_jack_info);
|
2009-01-30 19:43:25 +01:00
|
|
|
#endif
|
2009-01-29 23:12:08 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
return new JackOutput(block);
|
2006-12-30 01:14:45 +01:00
|
|
|
}
|
|
|
|
|
2009-10-21 21:37:11 +02:00
|
|
|
/**
|
|
|
|
* Stops the playback on the JACK connection.
|
|
|
|
*/
|
2014-12-24 15:15:53 +01:00
|
|
|
void
|
2017-08-11 12:41:11 +02:00
|
|
|
JackOutput::Stop() noexcept
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2014-12-24 15:15:53 +01:00
|
|
|
if (client == nullptr)
|
2009-10-21 21:37:11 +02:00
|
|
|
return;
|
2009-01-29 18:11:30 +01:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
if (shutdown)
|
2009-10-21 21:37:11 +02:00
|
|
|
/* the connection has failed; close it */
|
2014-12-24 15:15:53 +01:00
|
|
|
Disconnect();
|
2009-10-21 21:37:11 +02:00
|
|
|
else
|
|
|
|
/* the connection is alive: just stop playback */
|
2014-12-24 15:15:53 +01:00
|
|
|
jack_deactivate(client);
|
2009-10-21 21:37:11 +02:00
|
|
|
}
|
2009-01-29 17:08:44 +01:00
|
|
|
|
2016-11-08 15:04:11 +01:00
|
|
|
inline void
|
|
|
|
JackOutput::Start()
|
2009-10-21 21:37:11 +02:00
|
|
|
{
|
2014-12-24 15:15:53 +01:00
|
|
|
assert(client != nullptr);
|
|
|
|
assert(audio_format.channels <= num_source_ports);
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2009-10-21 21:37:11 +02:00
|
|
|
/* allocate the ring buffers on the first open(); these
|
|
|
|
persist until MPD exits. It's too unsafe to delete them
|
|
|
|
because we can never know when mpd_jack_process() gets
|
|
|
|
called */
|
2014-12-24 15:15:53 +01:00
|
|
|
for (unsigned i = 0; i < num_source_ports; ++i) {
|
|
|
|
if (ringbuffer[i] == nullptr)
|
|
|
|
ringbuffer[i] =
|
|
|
|
jack_ringbuffer_create(ringbuffer_size);
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2009-11-05 20:01:50 +01:00
|
|
|
/* clear the ring buffer to be sure that data from
|
|
|
|
previous playbacks are gone */
|
2014-12-24 15:15:53 +01:00
|
|
|
jack_ringbuffer_reset(ringbuffer[i]);
|
2009-11-05 20:01:50 +01:00
|
|
|
}
|
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
if ( jack_activate(client) ) {
|
|
|
|
Stop();
|
2016-11-08 15:04:11 +01:00
|
|
|
throw std::runtime_error("cannot activate client");
|
2009-01-29 18:11:23 +01:00
|
|
|
}
|
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
const char *dports[MAX_PORTS], **jports;
|
|
|
|
unsigned num_dports;
|
|
|
|
if (num_destination_ports == 0) {
|
2019-07-15 18:52:43 +02:00
|
|
|
/* if user requests no auto connect, we are done */
|
|
|
|
if (!auto_destination_ports) {
|
|
|
|
return;
|
|
|
|
}
|
2009-01-29 23:12:16 +01:00
|
|
|
/* no output ports were configured - ask libjack for
|
|
|
|
defaults */
|
2014-12-24 15:15:53 +01:00
|
|
|
jports = jack_get_ports(client, nullptr, nullptr,
|
2009-01-29 23:12:16 +01:00
|
|
|
JackPortIsPhysical | JackPortIsInput);
|
2013-04-17 00:24:44 +02:00
|
|
|
if (jports == nullptr) {
|
2014-12-24 15:15:53 +01:00
|
|
|
Stop();
|
2016-11-08 15:04:11 +01:00
|
|
|
throw std::runtime_error("no ports found");
|
2009-01-29 23:12:16 +01:00
|
|
|
}
|
|
|
|
|
2013-04-17 00:24:44 +02:00
|
|
|
assert(*jports != nullptr);
|
2009-11-06 18:55:26 +01:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
for (num_dports = 0; num_dports < MAX_PORTS &&
|
|
|
|
jports[num_dports] != nullptr;
|
|
|
|
++num_dports) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(jack_output_domain,
|
|
|
|
"destination_port[%u] = '%s'\n",
|
2014-12-24 15:15:53 +01:00
|
|
|
num_dports,
|
|
|
|
jports[num_dports]);
|
|
|
|
dports[num_dports] = jports[num_dports];
|
2009-11-06 18:55:26 +01:00
|
|
|
}
|
2009-01-30 19:44:58 +01:00
|
|
|
} else {
|
|
|
|
/* use the configured output ports */
|
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
num_dports = num_destination_ports;
|
|
|
|
for (unsigned i = 0; i < num_dports; ++i)
|
|
|
|
dports[i] = destination_ports[i].c_str();
|
2009-01-30 19:44:58 +01:00
|
|
|
|
2013-04-17 00:24:44 +02:00
|
|
|
jports = nullptr;
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 15:10:38 +01:00
|
|
|
AtScopeExit(jports) { free(jports); };
|
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
assert(num_dports > 0);
|
2009-11-06 18:55:26 +01:00
|
|
|
|
2014-12-24 15:21:23 +01:00
|
|
|
const char *duplicate_port = nullptr;
|
2014-12-24 15:15:53 +01:00
|
|
|
if (audio_format.channels >= 2 && num_dports == 1) {
|
2009-11-06 18:55:26 +01:00
|
|
|
/* mix stereo signal on one speaker */
|
|
|
|
|
2014-12-24 16:27:13 +01:00
|
|
|
std::fill(dports + num_dports, dports + audio_format.channels,
|
|
|
|
dports[0]);
|
2014-12-24 15:15:53 +01:00
|
|
|
} else if (num_dports > audio_format.channels) {
|
2019-08-02 07:47:45 +02:00
|
|
|
if (audio_format.channels == 1 && num_dports >= 2) {
|
2009-11-06 18:58:35 +01:00
|
|
|
/* mono input file: connect the one source
|
|
|
|
channel to the both destination channels */
|
2014-12-24 15:15:53 +01:00
|
|
|
duplicate_port = dports[1];
|
|
|
|
num_dports = 1;
|
2009-11-06 18:58:35 +01:00
|
|
|
} else
|
|
|
|
/* connect only as many ports as we need */
|
2014-12-24 15:15:53 +01:00
|
|
|
num_dports = audio_format.channels;
|
2009-11-06 18:55:26 +01:00
|
|
|
}
|
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
assert(num_dports <= num_source_ports);
|
2009-11-06 18:55:26 +01:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
for (unsigned i = 0; i < num_dports; ++i) {
|
|
|
|
int ret = jack_connect(client, jack_port_name(ports[i]),
|
|
|
|
dports[i]);
|
2009-01-29 23:14:04 +01:00
|
|
|
if (ret != 0) {
|
2014-12-24 15:15:53 +01:00
|
|
|
Stop();
|
2016-11-08 15:04:11 +01:00
|
|
|
throw FormatRuntimeError("Not a valid JACK port: %s",
|
|
|
|
dports[i]);
|
2009-01-29 23:14:04 +01:00
|
|
|
}
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2013-04-17 00:24:44 +02:00
|
|
|
if (duplicate_port != nullptr) {
|
2009-11-05 20:02:04 +01:00
|
|
|
/* mono input file: connect the one source channel to
|
|
|
|
the both destination channels */
|
|
|
|
int ret;
|
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
ret = jack_connect(client, jack_port_name(ports[0]),
|
2009-11-06 18:58:35 +01:00
|
|
|
duplicate_port);
|
2009-11-05 20:02:04 +01:00
|
|
|
if (ret != 0) {
|
2014-12-24 15:15:53 +01:00
|
|
|
Stop();
|
2016-11-08 15:04:11 +01:00
|
|
|
throw FormatRuntimeError("Not a valid JACK port: %s",
|
|
|
|
duplicate_port);
|
2009-11-05 20:02:04 +01:00
|
|
|
}
|
|
|
|
}
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
inline void
|
|
|
|
JackOutput::Open(AudioFormat &new_audio_format)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2014-12-24 15:15:53 +01:00
|
|
|
pause = false;
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
if (client != nullptr && shutdown)
|
|
|
|
Disconnect();
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2016-11-08 15:04:11 +01:00
|
|
|
if (client == nullptr)
|
|
|
|
Connect();
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
set_audioformat(this, new_audio_format);
|
|
|
|
audio_format = new_audio_format;
|
2006-10-18 04:49:13 +02:00
|
|
|
|
2016-11-08 15:04:11 +01:00
|
|
|
Start();
|
2014-12-24 15:15:53 +01:00
|
|
|
}
|
2009-11-05 20:02:04 +01:00
|
|
|
|
2014-12-24 23:00:46 +01:00
|
|
|
inline size_t
|
2014-12-24 23:02:19 +01:00
|
|
|
JackOutput::WriteSamples(const float *src, size_t n_frames)
|
2008-10-24 17:36:11 +02:00
|
|
|
{
|
2014-12-24 23:08:39 +01:00
|
|
|
assert(n_frames > 0);
|
|
|
|
|
2014-12-24 23:04:29 +01:00
|
|
|
const unsigned n_channels = audio_format.channels;
|
2014-12-24 23:08:39 +01:00
|
|
|
|
2014-12-24 22:58:25 +01:00
|
|
|
float *dest[MAX_CHANNELS];
|
2019-08-04 06:34:43 +02:00
|
|
|
size_t space = SIZE_MAX;
|
2014-12-24 22:58:25 +01:00
|
|
|
for (unsigned i = 0; i < n_channels; ++i) {
|
|
|
|
jack_ringbuffer_data_t d[2];
|
|
|
|
jack_ringbuffer_get_write_vector(ringbuffer[i], d);
|
|
|
|
|
|
|
|
/* choose the first non-empty writable area */
|
|
|
|
const jack_ringbuffer_data_t &e = d[d[0].len == 0];
|
|
|
|
|
|
|
|
if (e.len < space)
|
2014-12-24 23:08:39 +01:00
|
|
|
/* send data symmetrically */
|
2014-12-24 23:22:54 +01:00
|
|
|
space = e.len;
|
2014-12-24 22:58:25 +01:00
|
|
|
|
|
|
|
dest[i] = (float *)e.buf;
|
2014-12-24 23:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
space /= jack_sample_size;
|
|
|
|
if (space == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const size_t result = n_frames = std::min(space, n_frames);
|
2014-12-24 23:00:46 +01:00
|
|
|
|
2014-12-24 23:04:29 +01:00
|
|
|
while (n_frames-- > 0)
|
2014-12-24 22:58:25 +01:00
|
|
|
for (unsigned i = 0; i < n_channels; ++i)
|
|
|
|
*dest[i]++ = *src++;
|
|
|
|
|
|
|
|
const size_t per_channel_advance = result * jack_sample_size;
|
|
|
|
for (unsigned i = 0; i < n_channels; ++i)
|
|
|
|
jack_ringbuffer_write_advance(ringbuffer[i],
|
|
|
|
per_channel_advance);
|
2014-12-24 23:00:46 +01:00
|
|
|
|
|
|
|
return result;
|
2008-10-24 17:36:11 +02:00
|
|
|
}
|
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
inline size_t
|
2016-11-09 11:56:01 +01:00
|
|
|
JackOutput::Play(const void *chunk, size_t size)
|
2006-10-18 04:49:13 +02:00
|
|
|
{
|
2014-12-24 15:15:53 +01:00
|
|
|
pause = false;
|
2009-10-21 18:33:05 +02:00
|
|
|
|
2014-12-24 15:15:53 +01:00
|
|
|
const size_t frame_size = audio_format.GetFrameSize();
|
2008-10-24 16:56:10 +02:00
|
|
|
assert(size % frame_size == 0);
|
2008-10-24 16:39:43 +02:00
|
|
|
size /= frame_size;
|
2009-02-23 09:29:56 +01:00
|
|
|
|
2009-02-26 22:04:59 +01:00
|
|
|
while (true) {
|
2016-11-08 15:04:11 +01:00
|
|
|
if (shutdown)
|
|
|
|
throw std::runtime_error("Refusing to play, because "
|
|
|
|
"there is no client thread");
|
2009-02-26 22:04:59 +01:00
|
|
|
|
2014-12-24 23:08:39 +01:00
|
|
|
size_t frames_written =
|
|
|
|
WriteSamples((const float *)chunk, size);
|
|
|
|
if (frames_written > 0)
|
|
|
|
return frames_written * frame_size;
|
2006-12-30 01:14:45 +01:00
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
/* XXX do something more intelligent to
|
|
|
|
synchronize */
|
2014-12-04 09:14:11 +01:00
|
|
|
usleep(1000);
|
2007-04-09 22:18:11 +02:00
|
|
|
}
|
2006-10-18 04:49:13 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 23:45:14 +01:00
|
|
|
inline bool
|
2017-09-08 14:45:53 +02:00
|
|
|
JackOutput::Pause()
|
2009-10-21 18:33:05 +02:00
|
|
|
{
|
2014-12-29 23:45:14 +01:00
|
|
|
if (shutdown)
|
2009-10-21 18:33:05 +02:00
|
|
|
return false;
|
|
|
|
|
2014-12-29 23:45:14 +01:00
|
|
|
pause = true;
|
2009-10-21 18:33:05 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:22:27 +01:00
|
|
|
const struct AudioOutputPlugin jack_output_plugin = {
|
2013-04-17 00:24:44 +02:00
|
|
|
"jack",
|
|
|
|
mpd_jack_test_default_device,
|
|
|
|
mpd_jack_init,
|
|
|
|
nullptr,
|
2006-10-18 04:49:13 +02:00
|
|
|
};
|