SndioOutputPlugin.cxx 4.47 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
 * 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"
22
#include "mixer/MixerList.hxx"
23
#include "mixer/Listener.hxx"
24 25 26
#include "util/Domain.hxx"
#include "Log.hxx"

27 28 29 30
#include <sndio.h>

#include <stdexcept>

31 32 33 34 35
#ifndef SIO_DEVANY
/* this macro is missing in libroar-dev 1.0~beta2-3 (Debian Wheezy) */
#define SIO_DEVANY "default"
#endif

36 37
static constexpr unsigned MPD_SNDIO_BUFFER_TIME_MS = 250;

38 39
static constexpr Domain sndio_output_domain("sndio_output");

40
SndioOutput::SndioOutput(const ConfigBlock &block)
41
	:AudioOutput(0),
42 43
	 device(block.GetBlockValue("device", SIO_DEVANY)),
	 buffer_time(block.GetBlockValue("buffer_time",
44 45
					 MPD_SNDIO_BUFFER_TIME_MS)),
	 raw_volume(SIO_MAXVOL)
46 47 48
{
}

49 50 51 52 53
static void
VolumeCallback(void *arg, unsigned int volume) {
	((SndioOutput *)arg)->VolumeChanged(volume);
}

54 55 56 57 58
AudioOutput *
SndioOutput::Create(EventLoop &, const ConfigBlock &block) {
	return new SndioOutput(block);
}

59 60 61
static bool
sndio_test_default_device()
{
62 63
	auto *hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0);
	if (!hdl) {
64
		FormatError(sndio_output_domain,
65
			    "Error opening default sndio device");
66 67 68
		return false;
	}

69
	sio_close(hdl);
70 71 72
	return true;
}

73 74
void
SndioOutput::Open(AudioFormat &audio_format)
75 76 77 78
{
	struct sio_par par;
	unsigned bits, rate, chans;

79 80
	hdl = sio_open(device, SIO_PLAY, 0);
	if (!hdl)
81
		throw std::runtime_error("Failed to open default sndio device");
82 83 84 85 86

	switch (audio_format.format) {
	case SampleFormat::S16:
		bits = 16;
		break;
87 88 89
	case SampleFormat::S24_P32:
		bits = 24;
		break;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	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;
108
	par.appbufsz = rate * buffer_time / 1000;
109

110 111 112
	if (!sio_setpar(hdl, &par) ||
	    !sio_getpar(hdl, &par)) {
		sio_close(hdl);
113
		throw std::runtime_error("Failed to set/get audio params");
114 115 116 117 118 119 120 121
	}

	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) {
122
		sio_close(hdl);
123
		throw std::runtime_error("Requested audio params cannot be satisfied");
124 125
	}

126 127
	// Set volume after opening fresh audio stream which does
	// know nothing about previous audio streams.
128
	sio_setvol(hdl, raw_volume);
129 130 131
	// sio_onvol returns 0 if no volume knob is available.
	// This is the case on raw audio devices rather than
	// the sndiod audio server.
132
	if (sio_onvol(hdl, VolumeCallback, this) == 0)
133 134
		raw_volume = -1;

135 136
	if (!sio_start(hdl)) {
		sio_close(hdl);
137
		throw std::runtime_error("Failed to start audio device");
138 139 140 141
	}
}

void
142
SndioOutput::Close()  noexcept
143
{
144
	sio_close(hdl);
145 146 147
}

size_t
148
SndioOutput::Play(const void *chunk, size_t size)
149
{
150
	size_t n;
151

152 153
	n = sio_write(hdl, chunk, size);
	if (n == 0 && sio_eof(hdl) != 0)
154
		throw std::runtime_error("sndio write failed");
155
	return n;
156 157
}

158
void
159 160 161
SndioOutput::SetVolume(unsigned int volume)
{
	sio_setvol(hdl, volume * SIO_MAXVOL / 100);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
}

static inline unsigned int
RawToPercent(int raw_volume) {
	return raw_volume < 0 ? 100 : raw_volume * 100 / SIO_MAXVOL;
}

void
SndioOutput::VolumeChanged(int _raw_volume) {
	if (raw_volume >= 0 && listener != nullptr && mixer != nullptr) {
		raw_volume = _raw_volume;
		listener->OnMixerVolumeChanged(*mixer,
		    RawToPercent(raw_volume));
	}
}

unsigned int
SndioOutput::GetVolume() {
	return RawToPercent(raw_volume);
}

void
SndioOutput::RegisterMixerListener(Mixer *_mixer, MixerListener *_listener) {
	mixer = _mixer;
	listener = _listener;
}

189 190
const struct AudioOutputPlugin sndio_output_plugin = {
	"sndio",
191
	sndio_test_default_device,
192 193
	SndioOutput::Create,
	&sndio_mixer_plugin,
194
};