2016-06-21 19:28:41 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sndio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "SndioOutputPlugin.hxx"
|
|
|
|
#include "config/ConfigError.hxx"
|
|
|
|
#include "../OutputAPI.hxx"
|
|
|
|
#include "../Wrapper.hxx"
|
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
|
|
|
#include "Log.hxx"
|
|
|
|
|
2016-06-22 10:58:44 +02:00
|
|
|
#ifndef SIO_DEVANY
|
|
|
|
/* this macro is missing in libroar-dev 1.0~beta2-3 (Debian Wheezy) */
|
|
|
|
#define SIO_DEVANY "default"
|
|
|
|
#endif
|
|
|
|
|
2016-06-22 15:10:23 +02:00
|
|
|
static constexpr unsigned MPD_SNDIO_BUFFER_TIME_MS = 250;
|
|
|
|
|
2016-06-21 19:28:41 +02:00
|
|
|
class SndioOutput {
|
|
|
|
friend struct AudioOutputWrapper<SndioOutput>;
|
|
|
|
AudioOutput base;
|
2016-06-22 10:57:26 +02:00
|
|
|
const char *device;
|
2016-06-22 15:10:23 +02:00
|
|
|
unsigned buffer_time; /* in ms */
|
2016-06-21 19:28:41 +02:00
|
|
|
struct sio_hdl *sio_hdl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SndioOutput()
|
2016-06-22 11:01:37 +02:00
|
|
|
:base(sndio_output_plugin) {}
|
2016-06-21 19:28:41 +02:00
|
|
|
~SndioOutput() {}
|
|
|
|
|
|
|
|
bool Configure(const ConfigBlock &block, Error &error);
|
|
|
|
|
|
|
|
static SndioOutput *Create(const ConfigBlock &block, Error &error);
|
|
|
|
|
|
|
|
bool Open(AudioFormat &audio_format, Error &error);
|
|
|
|
void Close();
|
|
|
|
unsigned Delay() const;
|
|
|
|
size_t Play(const void *chunk, size_t size, Error &error);
|
|
|
|
void Cancel();
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr Domain sndio_output_domain("sndio_output");
|
|
|
|
|
|
|
|
bool
|
|
|
|
SndioOutput::Configure(const ConfigBlock &block, Error &error)
|
|
|
|
{
|
|
|
|
if (!base.Configure(block, error))
|
|
|
|
return false;
|
2016-06-22 10:57:26 +02:00
|
|
|
device = block.GetBlockValue("device", SIO_DEVANY);
|
2016-06-22 15:10:23 +02:00
|
|
|
buffer_time = block.GetBlockValue("buffer_time",
|
|
|
|
MPD_SNDIO_BUFFER_TIME_MS);
|
2016-06-21 19:28:41 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SndioOutput *
|
|
|
|
SndioOutput::Create(const ConfigBlock &block, Error &error)
|
|
|
|
{
|
|
|
|
SndioOutput *ao = new SndioOutput();
|
|
|
|
|
|
|
|
if (!ao->Configure(block, error)) {
|
|
|
|
delete ao;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ao;
|
|
|
|
}
|
|
|
|
|
2016-06-22 10:52:58 +02:00
|
|
|
static bool
|
|
|
|
sndio_test_default_device()
|
|
|
|
{
|
|
|
|
struct sio_hdl *sio_hdl;
|
|
|
|
|
|
|
|
sio_hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0);
|
|
|
|
if (!sio_hdl) {
|
|
|
|
FormatError(sndio_output_domain,
|
|
|
|
"Error opening default sndio device");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sio_close(sio_hdl);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-21 19:28:41 +02:00
|
|
|
bool
|
|
|
|
SndioOutput::Open(AudioFormat &audio_format, gcc_unused Error &error)
|
|
|
|
{
|
|
|
|
struct sio_par par;
|
|
|
|
unsigned bits, rate, chans;
|
|
|
|
|
2016-06-22 10:57:26 +02:00
|
|
|
sio_hdl = sio_open(device, SIO_PLAY, 0);
|
2016-06-21 19:28:41 +02:00
|
|
|
if (!sio_hdl) {
|
|
|
|
error.Format(sndio_output_domain, -1,
|
|
|
|
"Failed to open default sndio device");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (audio_format.format) {
|
|
|
|
case SampleFormat::S16:
|
|
|
|
bits = 16;
|
|
|
|
break;
|
|
|
|
case SampleFormat::S32:
|
|
|
|
bits = 32;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
audio_format.format = SampleFormat::S16;
|
|
|
|
bits = 16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rate = audio_format.sample_rate;
|
|
|
|
chans = audio_format.channels;
|
|
|
|
|
|
|
|
sio_initpar(&par);
|
|
|
|
par.bits = bits;
|
|
|
|
par.rate = rate;
|
|
|
|
par.pchan = chans;
|
|
|
|
par.sig = 1;
|
|
|
|
par.le = SIO_LE_NATIVE;
|
2016-06-22 15:10:23 +02:00
|
|
|
par.appbufsz = rate * buffer_time / 1000;
|
2016-06-21 19:28:41 +02:00
|
|
|
|
|
|
|
if (!sio_setpar(sio_hdl, &par) ||
|
|
|
|
!sio_getpar(sio_hdl, &par)) {
|
|
|
|
error.Format(sndio_output_domain, -1,
|
|
|
|
"Failed to set/get audio params");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (par.bits != bits ||
|
|
|
|
par.rate < rate * 995 / 1000 ||
|
|
|
|
par.rate > rate * 1005 / 1000 ||
|
|
|
|
par.pchan != chans ||
|
|
|
|
par.sig != 1 ||
|
|
|
|
par.le != SIO_LE_NATIVE) {
|
|
|
|
error.Format(sndio_output_domain, -1,
|
|
|
|
"Requested audio params cannot be satisfied");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sio_start(sio_hdl)) {
|
|
|
|
error.Format(sndio_output_domain, -1,
|
|
|
|
"Failed to start audio device");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
err:
|
|
|
|
sio_close(sio_hdl);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SndioOutput::Close()
|
|
|
|
{
|
2016-06-22 11:01:37 +02:00
|
|
|
sio_close(sio_hdl);
|
2016-06-21 19:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
SndioOutput::Play(const void *chunk, size_t size, Error &error)
|
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
n = sio_write(sio_hdl, chunk, size);
|
|
|
|
if (n < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
error.FormatErrno("Failed to write %zu bytes to sndio output", size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef AudioOutputWrapper<SndioOutput> Wrapper;
|
|
|
|
|
|
|
|
const struct AudioOutputPlugin sndio_output_plugin = {
|
|
|
|
"sndio",
|
2016-06-22 10:52:58 +02:00
|
|
|
sndio_test_default_device,
|
2016-06-21 19:28:41 +02:00
|
|
|
&Wrapper::Init,
|
|
|
|
&Wrapper::Finish,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
&Wrapper::Open,
|
|
|
|
&Wrapper::Close,
|
2016-06-22 11:27:47 +02:00
|
|
|
nullptr,
|
2016-06-21 19:28:41 +02:00
|
|
|
nullptr,
|
|
|
|
&Wrapper::Play,
|
|
|
|
nullptr,
|
2016-06-22 11:27:47 +02:00
|
|
|
nullptr,
|
2016-06-21 19:28:41 +02:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
};
|