OpenALOutputPlugin.cxx 6.16 KB
Newer Older
Serge Ziryukin's avatar
Serge Ziryukin committed
1
/*
2
 * Copyright (C) 2003-2013 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 24
#include "util/Error.hxx"
#include "util/Domain.hxx"
Serge Ziryukin's avatar
Serge Ziryukin committed
25 26 27

#include <glib.h>

28
#ifndef HAVE_OSX
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 37 38

/* should be enough for buffer size = 2048 */
#define NUM_BUFFERS 16

39
struct OpenALOutput {
40 41
	struct audio_output base;

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

51
	bool Initialize(const config_param &param, Error &error_r) {
52 53 54 55 56 57 58
		return ao_base_init(&base, &openal_output_plugin, param,
				    error_r);
	}

	void Deinitialize() {
		ao_base_finish(&base);
	}
Serge Ziryukin's avatar
Serge Ziryukin committed
59 60
};

61
static constexpr Domain openal_output_domain("openal_output");
Serge Ziryukin's avatar
Serge Ziryukin committed
62 63

static ALenum
64
openal_audio_format(AudioFormat &audio_format)
Serge Ziryukin's avatar
Serge Ziryukin committed
65
{
66
	/* note: cannot map SampleFormat::S8 to AL_FORMAT_STEREO8 or
67 68 69
	   AL_FORMAT_MONO8 since OpenAL expects unsigned 8 bit
	   samples, while MPD uses signed samples */

70 71 72
	switch (audio_format.format) {
	case SampleFormat::S16:
		if (audio_format.channels == 2)
Serge Ziryukin's avatar
Serge Ziryukin committed
73
			return AL_FORMAT_STEREO16;
74
		if (audio_format.channels == 1)
Serge Ziryukin's avatar
Serge Ziryukin committed
75
			return AL_FORMAT_MONO16;
76 77

		/* fall back to mono */
78
		audio_format.channels = 1;
79
		return openal_audio_format(audio_format);
Serge Ziryukin's avatar
Serge Ziryukin committed
80

81 82
	default:
		/* fall back to 16 bit */
83
		audio_format.format = SampleFormat::S16;
84
		return openal_audio_format(audio_format);
Serge Ziryukin's avatar
Serge Ziryukin committed
85 86 87
	}
}

88
gcc_pure
89
static inline ALint
90
openal_get_source_i(const OpenALOutput *od, ALenum param)
91 92 93 94 95 96
{
	ALint value;
	alGetSourcei(od->source, param, &value);
	return value;
}

97
gcc_pure
98
static inline bool
99
openal_has_processed(const OpenALOutput *od)
100 101 102 103
{
	return openal_get_source_i(od, AL_BUFFERS_PROCESSED) > 0;
}

104
gcc_pure
105
static inline ALint
106
openal_is_playing(const OpenALOutput *od)
107 108 109 110
{
	return openal_get_source_i(od, AL_SOURCE_STATE) == AL_PLAYING;
}

Serge Ziryukin's avatar
Serge Ziryukin committed
111
static bool
112
openal_setup_context(OpenALOutput *od, Error &error)
Serge Ziryukin's avatar
Serge Ziryukin committed
113 114 115
{
	od->device = alcOpenDevice(od->device_name);

116
	if (od->device == nullptr) {
117 118 119
		error.Format(openal_output_domain,
			     "Error opening OpenAL device \"%s\"",
			     od->device_name);
Serge Ziryukin's avatar
Serge Ziryukin committed
120 121 122
		return false;
	}

123
	od->context = alcCreateContext(od->device, nullptr);
Serge Ziryukin's avatar
Serge Ziryukin committed
124

125
	if (od->context == nullptr) {
126 127 128
		error.Format(openal_output_domain,
			     "Error creating context for \"%s\"",
			     od->device_name);
Serge Ziryukin's avatar
Serge Ziryukin committed
129 130 131 132 133 134 135
		alcCloseDevice(od->device);
		return false;
	}

	return true;
}

136
static struct audio_output *
137
openal_init(const config_param &param, Error &error)
Serge Ziryukin's avatar
Serge Ziryukin committed
138
{
139
	const char *device_name = param.GetBlockValue("device");
140 141
	if (device_name == nullptr) {
		device_name = alcGetString(nullptr, ALC_DEFAULT_DEVICE_SPECIFIER);
Serge Ziryukin's avatar
Serge Ziryukin committed
142 143
	}

144
	OpenALOutput *od = new OpenALOutput();
145
	if (!od->Initialize(param, error)) {
146 147
		delete od;
		return nullptr;
148 149
	}

150 151
	od->device_name = device_name;

152
	return &od->base;
Serge Ziryukin's avatar
Serge Ziryukin committed
153 154 155
}

static void
156
openal_finish(struct audio_output *ao)
Serge Ziryukin's avatar
Serge Ziryukin committed
157
{
158
	OpenALOutput *od = (OpenALOutput *)ao;
Serge Ziryukin's avatar
Serge Ziryukin committed
159

160 161
	od->Deinitialize();
	delete od;
Serge Ziryukin's avatar
Serge Ziryukin committed
162 163 164
}

static bool
165
openal_open(struct audio_output *ao, AudioFormat &audio_format,
166
	    Error &error)
Serge Ziryukin's avatar
Serge Ziryukin committed
167
{
168
	OpenALOutput *od = (OpenALOutput *)ao;
Serge Ziryukin's avatar
Serge Ziryukin committed
169 170 171 172 173 174 175 176 177 178 179

	od->format = openal_audio_format(audio_format);

	if (!openal_setup_context(od, error)) {
		return false;
	}

	alcMakeContextCurrent(od->context);
	alGenBuffers(NUM_BUFFERS, od->buffers);

	if (alGetError() != AL_NO_ERROR) {
180
		error.Set(openal_output_domain, "Failed to generate buffers");
Serge Ziryukin's avatar
Serge Ziryukin committed
181 182 183 184 185 186
		return false;
	}

	alGenSources(1, &od->source);

	if (alGetError() != AL_NO_ERROR) {
187
		error.Set(openal_output_domain, "Failed to generate source");
Serge Ziryukin's avatar
Serge Ziryukin committed
188 189 190 191 192
		alDeleteBuffers(NUM_BUFFERS, od->buffers);
		return false;
	}

	od->filled = 0;
193
	od->frequency = audio_format.sample_rate;
Serge Ziryukin's avatar
Serge Ziryukin committed
194 195 196 197 198

	return true;
}

static void
199
openal_close(struct audio_output *ao)
Serge Ziryukin's avatar
Serge Ziryukin committed
200
{
201
	OpenALOutput *od = (OpenALOutput *)ao;
Serge Ziryukin's avatar
Serge Ziryukin committed
202 203 204 205 206 207 208 209

	alcMakeContextCurrent(od->context);
	alDeleteSources(1, &od->source);
	alDeleteBuffers(NUM_BUFFERS, od->buffers);
	alcDestroyContext(od->context);
	alcCloseDevice(od->device);
}

210 211 212
static unsigned
openal_delay(struct audio_output *ao)
{
213
	OpenALOutput *od = (OpenALOutput *)ao;
214 215 216 217 218 219 220 221 222

	return od->filled < NUM_BUFFERS || openal_has_processed(od)
		? 0
		/* we don't know exactly how long we must wait for the
		   next buffer to finish, so this is a random
		   guess: */
		: 50;
}

Serge Ziryukin's avatar
Serge Ziryukin committed
223
static size_t
224
openal_play(struct audio_output *ao, const void *chunk, size_t size,
225
	    gcc_unused Error &error)
Serge Ziryukin's avatar
Serge Ziryukin committed
226
{
227
	OpenALOutput *od = (OpenALOutput *)ao;
Serge Ziryukin's avatar
Serge Ziryukin committed
228 229 230 231 232 233 234 235 236 237 238 239
	ALuint buffer;

	if (alcGetCurrentContext() != od->context) {
		alcMakeContextCurrent(od->context);
	}

	if (od->filled < NUM_BUFFERS) {
		/* fill all buffers */
		buffer = od->buffers[od->filled];
		od->filled++;
	} else {
		/* wait for processed buffer */
240 241
		while (!openal_has_processed(od))
			g_usleep(10);
Serge Ziryukin's avatar
Serge Ziryukin committed
242 243 244 245 246 247 248

		alSourceUnqueueBuffers(od->source, 1, &buffer);
	}

	alBufferData(buffer, od->format, chunk, size, od->frequency);
	alSourceQueueBuffers(od->source, 1, &buffer);

249
	if (!openal_is_playing(od))
Serge Ziryukin's avatar
Serge Ziryukin committed
250 251 252 253 254 255
		alSourcePlay(od->source);

	return size;
}

static void
256
openal_cancel(struct audio_output *ao)
Serge Ziryukin's avatar
Serge Ziryukin committed
257
{
258
	OpenALOutput *od = (OpenALOutput *)ao;
Serge Ziryukin's avatar
Serge Ziryukin committed
259 260 261 262

	od->filled = 0;
	alcMakeContextCurrent(od->context);
	alSourceStop(od->source);
263 264 265 266

	/* force-unqueue all buffers */
	alSourcei(od->source, AL_BUFFER, 0);
	od->filled = 0;
Serge Ziryukin's avatar
Serge Ziryukin committed
267 268 269
}

const struct audio_output_plugin openal_output_plugin = {
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	"openal",
	nullptr,
	openal_init,
	openal_finish,
	nullptr,
	nullptr,
	openal_open,
	openal_close,
	openal_delay,
	nullptr,
	openal_play,
	nullptr,
	openal_cancel,
	nullptr,
	nullptr,
Serge Ziryukin's avatar
Serge Ziryukin committed
285
};