SndioOutputPlugin.cxx 4.3 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * 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 "config.h"
#include "SndioOutputPlugin.hxx"
#include "../OutputAPI.hxx"
#include "../Wrapper.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"

27 28 29 30 31
/* work around a C++ incompatibility if the sndio API is emulated by
   libroar: libroar's "struct roar_service_stream" has a member named
   "new", which is an illegal identifier in C++ */
#define new new_

32 33
#include <sndio.h>

34 35 36
/* undo the libroar workaround */
#undef new

37 38
#include <stdexcept>

39 40 41 42 43
#ifndef SIO_DEVANY
/* this macro is missing in libroar-dev 1.0~beta2-3 (Debian Wheezy) */
#define SIO_DEVANY "default"
#endif

44 45
static constexpr unsigned MPD_SNDIO_BUFFER_TIME_MS = 250;

46 47
static constexpr Domain sndio_output_domain("sndio_output");

48 49 50
class SndioOutput {
	friend struct AudioOutputWrapper<SndioOutput>;
	AudioOutput base;
51 52
	const char *const device;
	const unsigned buffer_time; /* in ms */
53 54 55
	struct sio_hdl *sio_hdl;

public:
56
	SndioOutput(const ConfigBlock &block);
57

58 59
	static SndioOutput *Create(EventLoop &event_loop,
				   const ConfigBlock &block);
60

61
	void Open(AudioFormat &audio_format);
62
	void Close();
63
	size_t Play(const void *chunk, size_t size);
64 65 66
	void Cancel();
};

67 68 69 70 71
SndioOutput::SndioOutput(const ConfigBlock &block)
	:base(sndio_output_plugin, block),
	 device(block.GetBlockValue("device", SIO_DEVANY)),
	 buffer_time(block.GetBlockValue("buffer_time",
					 MPD_SNDIO_BUFFER_TIME_MS))
72 73 74 75
{
}

SndioOutput *
76
SndioOutput::Create(EventLoop &, const ConfigBlock &block)
77
{
78
	return new SndioOutput(block);
79 80
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
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;
}

97 98
void
SndioOutput::Open(AudioFormat &audio_format)
99 100 101 102
{
	struct sio_par par;
	unsigned bits, rate, chans;

103
	sio_hdl = sio_open(device, SIO_PLAY, 0);
104 105
	if (!sio_hdl)
		throw std::runtime_error("Failed to open default sndio device");
106 107 108 109 110

	switch (audio_format.format) {
	case SampleFormat::S16:
		bits = 16;
		break;
111 112 113
	case SampleFormat::S24_P32:
		bits = 24;
		break;
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	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;
132
	par.appbufsz = rate * buffer_time / 1000;
133 134 135

	if (!sio_setpar(sio_hdl, &par) ||
	    !sio_getpar(sio_hdl, &par)) {
136
		sio_close(sio_hdl);
137
		throw std::runtime_error("Failed to set/get audio params");
138 139 140 141 142 143 144 145
	}

	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) {
146
		sio_close(sio_hdl);
147
		throw std::runtime_error("Requested audio params cannot be satisfied");
148 149 150
	}

	if (!sio_start(sio_hdl)) {
151
		sio_close(sio_hdl);
152
		throw std::runtime_error("Failed to start audio device");
153 154 155 156 157 158
	}
}

void
SndioOutput::Close()
{
159
	sio_close(sio_hdl);
160 161 162
}

size_t
163
SndioOutput::Play(const void *chunk, size_t size)
164
{
165
	size_t n;
166

167 168
	n = sio_write(sio_hdl, chunk, size);
	if (n == 0 && sio_eof(sio_hdl) != 0)
169
		throw std::runtime_error("sndio write failed");
170
	return n;
171 172 173 174 175 176
}

typedef AudioOutputWrapper<SndioOutput> Wrapper;

const struct AudioOutputPlugin sndio_output_plugin = {
	"sndio",
177
	sndio_test_default_device,
178 179 180 181 182 183
	&Wrapper::Init,
	&Wrapper::Finish,
	nullptr,
	nullptr,
	&Wrapper::Open,
	&Wrapper::Close,
184
	nullptr,
185 186 187
	nullptr,
	&Wrapper::Play,
	nullptr,
188
	nullptr,
189 190 191
	nullptr,
	nullptr,
};