OpenALOutputPlugin.cxx 5.42 KB
Newer Older
Serge Ziryukin's avatar
Serge Ziryukin committed
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
Serge Ziryukin's avatar
Serge Ziryukin committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "config.h"
21
#include "OpenALOutputPlugin.hxx"
22
#include "../OutputAPI.hxx"
23
#include "../Wrapper.hxx"
24
#include "util/RuntimeError.hxx"
Serge Ziryukin's avatar
Serge Ziryukin committed
25

26
#include <unistd.h>
Serge Ziryukin's avatar
Serge Ziryukin committed
27

28
#ifndef __APPLE__
Serge Ziryukin's avatar
Serge Ziryukin committed
29 30
#include <AL/al.h>
#include <AL/alc.h>
31 32 33 34
#else
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#endif
Serge Ziryukin's avatar
Serge Ziryukin committed
35

36
class OpenALOutput {
37 38
	friend struct AudioOutputWrapper<OpenALOutput>;

39 40 41
	/* should be enough for buffer size = 2048 */
	static constexpr unsigned NUM_BUFFERS = 16;

42
	AudioOutput base;
43

Serge Ziryukin's avatar
Serge Ziryukin committed
44 45 46 47
	const char *device_name;
	ALCdevice *device;
	ALCcontext *context;
	ALuint buffers[NUM_BUFFERS];
48
	unsigned filled;
Serge Ziryukin's avatar
Serge Ziryukin committed
49 50 51
	ALuint source;
	ALenum format;
	ALuint frequency;
52

53
	OpenALOutput(const ConfigBlock &block);
54

55 56
	static OpenALOutput *Create(EventLoop &event_loop,
				    const ConfigBlock &block);
57

58
	void Open(AudioFormat &audio_format);
59 60 61
	void Close();

	gcc_pure
62
	std::chrono::steady_clock::duration Delay() const {
63
		return filled < NUM_BUFFERS || HasProcessed()
64
			? std::chrono::steady_clock::duration::zero()
65 66 67
			/* we don't know exactly how long we must wait
			   for the next buffer to finish, so this is a
			   random guess: */
68
			: std::chrono::milliseconds(50);
69 70
	}

71
	size_t Play(const void *chunk, size_t size);
72 73 74 75 76 77 78 79 80 81 82 83 84 85

	void Cancel();

private:
	gcc_pure
	ALint GetSourceI(ALenum param) const {
		ALint value;
		alGetSourcei(source, param, &value);
		return value;
	}

	gcc_pure
	bool HasProcessed() const {
		return GetSourceI(AL_BUFFERS_PROCESSED) > 0;
86
	}
87 88 89 90 91 92

	gcc_pure
	bool IsPlaying() const {
		return GetSourceI(AL_SOURCE_STATE) == AL_PLAYING;
	}

93 94 95 96
	/**
	 * Throws #std::runtime_error on error.
	 */
	void SetupContext();
Serge Ziryukin's avatar
Serge Ziryukin committed
97 98 99
};

static ALenum
100
openal_audio_format(AudioFormat &audio_format)
Serge Ziryukin's avatar
Serge Ziryukin committed
101
{
102
	/* note: cannot map SampleFormat::S8 to AL_FORMAT_STEREO8 or
103 104 105
	   AL_FORMAT_MONO8 since OpenAL expects unsigned 8 bit
	   samples, while MPD uses signed samples */

106 107 108
	switch (audio_format.format) {
	case SampleFormat::S16:
		if (audio_format.channels == 2)
Serge Ziryukin's avatar
Serge Ziryukin committed
109
			return AL_FORMAT_STEREO16;
110
		if (audio_format.channels == 1)
Serge Ziryukin's avatar
Serge Ziryukin committed
111
			return AL_FORMAT_MONO16;
112 113

		/* fall back to mono */
114
		audio_format.channels = 1;
115
		return openal_audio_format(audio_format);
Serge Ziryukin's avatar
Serge Ziryukin committed
116

117 118
	default:
		/* fall back to 16 bit */
119
		audio_format.format = SampleFormat::S16;
120
		return openal_audio_format(audio_format);
Serge Ziryukin's avatar
Serge Ziryukin committed
121 122 123
	}
}

124 125
inline void
OpenALOutput::SetupContext()
Serge Ziryukin's avatar
Serge Ziryukin committed
126
{
127
	device = alcOpenDevice(device_name);
128 129 130
	if (device == nullptr)
		throw FormatRuntimeError("Error opening OpenAL device \"%s\"",
					 device_name);
Serge Ziryukin's avatar
Serge Ziryukin committed
131

132 133 134
	context = alcCreateContext(device, nullptr);
	if (context == nullptr) {
		alcCloseDevice(device);
135 136
		throw FormatRuntimeError("Error creating context for \"%s\"",
					 device_name);
Serge Ziryukin's avatar
Serge Ziryukin committed
137 138 139
	}
}

140 141 142
OpenALOutput::OpenALOutput(const ConfigBlock &block)
	:base(openal_output_plugin, block),
	 device_name(block.GetBlockValue("device"))
Serge Ziryukin's avatar
Serge Ziryukin committed
143
{
144 145 146
	if (device_name == nullptr)
		device_name = alcGetString(nullptr,
					   ALC_DEFAULT_DEVICE_SPECIFIER);
Serge Ziryukin's avatar
Serge Ziryukin committed
147 148
}

149
inline OpenALOutput *
150
OpenALOutput::Create(EventLoop &, const ConfigBlock &block)
Serge Ziryukin's avatar
Serge Ziryukin committed
151
{
152
	return new OpenALOutput(block);
Serge Ziryukin's avatar
Serge Ziryukin committed
153 154
}

155 156
inline void
OpenALOutput::Open(AudioFormat &audio_format)
Serge Ziryukin's avatar
Serge Ziryukin committed
157
{
158
	format = openal_audio_format(audio_format);
Serge Ziryukin's avatar
Serge Ziryukin committed
159

160
	SetupContext();
Serge Ziryukin's avatar
Serge Ziryukin committed
161

162 163
	alcMakeContextCurrent(context);
	alGenBuffers(NUM_BUFFERS, buffers);
Serge Ziryukin's avatar
Serge Ziryukin committed
164

165 166
	if (alGetError() != AL_NO_ERROR)
		throw std::runtime_error("Failed to generate buffers");
Serge Ziryukin's avatar
Serge Ziryukin committed
167

168
	alGenSources(1, &source);
Serge Ziryukin's avatar
Serge Ziryukin committed
169 170

	if (alGetError() != AL_NO_ERROR) {
171
		alDeleteBuffers(NUM_BUFFERS, buffers);
172
		throw std::runtime_error("Failed to generate source");
Serge Ziryukin's avatar
Serge Ziryukin committed
173 174
	}

175 176
	filled = 0;
	frequency = audio_format.sample_rate;
Serge Ziryukin's avatar
Serge Ziryukin committed
177 178
}

179 180
inline void
OpenALOutput::Close()
Serge Ziryukin's avatar
Serge Ziryukin committed
181
{
182 183 184 185 186
	alcMakeContextCurrent(context);
	alDeleteSources(1, &source);
	alDeleteBuffers(NUM_BUFFERS, buffers);
	alcDestroyContext(context);
	alcCloseDevice(device);
Serge Ziryukin's avatar
Serge Ziryukin committed
187 188
}

189
inline size_t
190
OpenALOutput::Play(const void *chunk, size_t size)
191
{
192 193
	if (alcGetCurrentContext() != context)
		alcMakeContextCurrent(context);
194

Serge Ziryukin's avatar
Serge Ziryukin committed
195
	ALuint buffer;
196
	if (filled < NUM_BUFFERS) {
Serge Ziryukin's avatar
Serge Ziryukin committed
197
		/* fill all buffers */
198 199
		buffer = buffers[filled];
		filled++;
Serge Ziryukin's avatar
Serge Ziryukin committed
200 201
	} else {
		/* wait for processed buffer */
202
		while (!HasProcessed())
203
			usleep(10);
Serge Ziryukin's avatar
Serge Ziryukin committed
204

205
		alSourceUnqueueBuffers(source, 1, &buffer);
Serge Ziryukin's avatar
Serge Ziryukin committed
206 207
	}

208 209
	alBufferData(buffer, format, chunk, size, frequency);
	alSourceQueueBuffers(source, 1, &buffer);
Serge Ziryukin's avatar
Serge Ziryukin committed
210

211 212
	if (!IsPlaying())
		alSourcePlay(source);
Serge Ziryukin's avatar
Serge Ziryukin committed
213 214 215 216

	return size;
}

217 218
inline void
OpenALOutput::Cancel()
Serge Ziryukin's avatar
Serge Ziryukin committed
219
{
220 221 222
	filled = 0;
	alcMakeContextCurrent(context);
	alSourceStop(source);
223 224

	/* force-unqueue all buffers */
225 226
	alSourcei(source, AL_BUFFER, 0);
	filled = 0;
Serge Ziryukin's avatar
Serge Ziryukin committed
227 228
}

229 230
typedef AudioOutputWrapper<OpenALOutput> Wrapper;

231
const struct AudioOutputPlugin openal_output_plugin = {
232 233
	"openal",
	nullptr,
234 235
	&Wrapper::Init,
	&Wrapper::Finish,
236 237
	nullptr,
	nullptr,
238 239 240
	&Wrapper::Open,
	&Wrapper::Close,
	&Wrapper::Delay,
241
	nullptr,
242
	&Wrapper::Play,
243
	nullptr,
244
	&Wrapper::Cancel,
245 246
	nullptr,
	nullptr,
Serge Ziryukin's avatar
Serge Ziryukin committed
247
};