openal_plugin.c 5.94 KB
Newer Older
Serge Ziryukin's avatar
Serge Ziryukin committed
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 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 22
#include "output_api.h"
#include "timer.h"
Serge Ziryukin's avatar
Serge Ziryukin committed
23 24 25

#include <glib.h>

26
#ifndef HAVE_OSX
Serge Ziryukin's avatar
Serge Ziryukin committed
27 28
#include <AL/al.h>
#include <AL/alc.h>
29 30 31 32
#else
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#endif
Serge Ziryukin's avatar
Serge Ziryukin committed
33 34 35 36 37 38 39 40 41 42 43

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "openal"

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

struct openal_data {
	const char *device_name;
	ALCdevice *device;
	ALCcontext *context;
44
	struct timer *timer;
Serge Ziryukin's avatar
Serge Ziryukin committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	ALuint buffers[NUM_BUFFERS];
	int filled;
	ALuint source;
	ALenum format;
	ALuint frequency;
};

static inline GQuark
openal_output_quark(void)
{
	return g_quark_from_static_string("openal_output");
}

static ALenum
openal_audio_format(struct audio_format *audio_format)
{
61 62
	switch (audio_format->format) {
	case SAMPLE_FORMAT_S16:
Serge Ziryukin's avatar
Serge Ziryukin committed
63 64 65 66 67 68
		if (audio_format->channels == 2)
			return AL_FORMAT_STEREO16;
		if (audio_format->channels == 1)
			return AL_FORMAT_MONO16;
		break;

69
	case SAMPLE_FORMAT_S8:
Serge Ziryukin's avatar
Serge Ziryukin committed
70 71 72 73 74
		if (audio_format->channels == 2)
			return AL_FORMAT_STEREO8;
		if (audio_format->channels == 1)
			return AL_FORMAT_MONO8;
		break;
75 76 77 78 79 80 81 82 83

	default:
		/* fall back to 16 bit */
		audio_format->format = SAMPLE_FORMAT_S16;
		if (audio_format->channels == 2)
			return AL_FORMAT_STEREO16;
		if (audio_format->channels == 1)
			return AL_FORMAT_MONO16;
		break;
Serge Ziryukin's avatar
Serge Ziryukin committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	}

	return 0;
}

static bool
openal_setup_context(struct openal_data *od,
		     GError **error)
{
	od->device = alcOpenDevice(od->device_name);

	if (od->device == NULL) {
		g_set_error(error, openal_output_quark(), 0,
			    "Error opening OpenAL device \"%s\"\n",
			    od->device_name);
		return false;
	}

	od->context = alcCreateContext(od->device, NULL);

	if (od->context == NULL) {
		g_set_error(error, openal_output_quark(), 0,
			    "Error creating context for \"%s\"\n",
			    od->device_name);
		alcCloseDevice(od->device);
		return false;
	}

	return true;
}

static void
openal_unqueue_buffers(struct openal_data *od)
{
	ALint num;
	ALuint buffer;

	alGetSourcei(od->source, AL_BUFFERS_QUEUED, &num);

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

static void *
openal_init(G_GNUC_UNUSED const struct audio_format *audio_format,
	    const struct config_param *param,
	    G_GNUC_UNUSED GError **error)
{
	const char *device_name = config_get_block_string(param, "device", NULL);
	struct openal_data *od;

	if (device_name == NULL) {
		device_name = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
	}

140 141 142
	od = g_new(struct openal_data, 1);
	od->device_name = device_name;

Serge Ziryukin's avatar
Serge Ziryukin committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	return od;
}

static void
openal_finish(void *data)
{
	struct openal_data *od = data;

	g_free(od);
}

static bool
openal_open(void *data, struct audio_format *audio_format,
	    GError **error)
{
	struct openal_data *od = data;

	od->format = openal_audio_format(audio_format);

	if (!od->format) {
163
		struct audio_format_string s;
Serge Ziryukin's avatar
Serge Ziryukin committed
164
		g_set_error(error, openal_output_quark(), 0,
165 166
			    "Unsupported audio format: %s",
			    audio_format_to_string(audio_format, &s));
Serge Ziryukin's avatar
Serge Ziryukin committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
		return false;
	}

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

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

	if (alGetError() != AL_NO_ERROR) {
		g_set_error(error, openal_output_quark(), 0,
			    "Failed to generate buffers");
		return false;
	}

	alGenSources(1, &od->source);

	if (alGetError() != AL_NO_ERROR) {
		g_set_error(error, openal_output_quark(), 0,
			    "Failed to generate source");
		alDeleteBuffers(NUM_BUFFERS, od->buffers);
		return false;
	}

	od->filled = 0;
	od->timer = timer_new(audio_format);
	od->frequency = audio_format->sample_rate;

	return true;
}

static void
openal_close(void *data)
{
	struct openal_data *od = data;

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

static size_t
openal_play(void *data, const void *chunk, size_t size,
	    G_GNUC_UNUSED GError **error)
{
	struct openal_data *od = data;
	ALuint buffer;
	ALint num, state;

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

	alGetSourcei(od->source, AL_BUFFERS_PROCESSED, &num);

	if (od->filled < NUM_BUFFERS) {
		/* fill all buffers */
		buffer = od->buffers[od->filled];
		od->filled++;
	} else {
		/* wait for processed buffer */
		while (num < 1) {
			if (!od->timer->started) {
				timer_start(od->timer);
			} else {
				timer_sync(od->timer);
			}

			timer_add(od->timer, size);

			alGetSourcei(od->source, AL_BUFFERS_PROCESSED, &num);
		}

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

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

	if (state != AL_PLAYING) {
		alSourcePlay(od->source);
	}

	return size;
}

static void
openal_cancel(void *data)
{
	struct openal_data *od = data;

	od->filled = 0;
	alcMakeContextCurrent(od->context);
	alSourceStop(od->source);
	openal_unqueue_buffers(od);
}

const struct audio_output_plugin openal_output_plugin = {
	.name = "openal",
	.init = openal_init,
	.finish = openal_finish,
	.open = openal_open,
	.close = openal_close,
	.play = openal_play,
	.cancel = openal_cancel,
};