2009-08-31 09:26:22 +02:00
|
|
|
/*
|
2013-04-17 01:04:27 +02:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-08-31 09:26:22 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2009-09-07 14:01:38 +02:00
|
|
|
#include "config.h"
|
2013-04-17 01:04:27 +02:00
|
|
|
#include "OpenALOutputPlugin.hxx"
|
2013-07-30 08:34:10 +02:00
|
|
|
#include "OutputAPI.hxx"
|
2009-08-31 09:26:22 +02:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2009-09-07 14:01:38 +02:00
|
|
|
#ifndef HAVE_OSX
|
2009-08-31 09:26:22 +02:00
|
|
|
#include <AL/al.h>
|
|
|
|
#include <AL/alc.h>
|
2009-09-07 14:01:38 +02:00
|
|
|
#else
|
|
|
|
#include <OpenAL/al.h>
|
|
|
|
#include <OpenAL/alc.h>
|
|
|
|
#endif
|
2009-08-31 09:26:22 +02:00
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "openal"
|
|
|
|
|
|
|
|
/* should be enough for buffer size = 2048 */
|
|
|
|
#define NUM_BUFFERS 16
|
|
|
|
|
2013-04-17 01:04:27 +02:00
|
|
|
struct OpenALOutput {
|
2011-09-16 23:31:48 +02:00
|
|
|
struct audio_output base;
|
|
|
|
|
2009-08-31 09:26:22 +02:00
|
|
|
const char *device_name;
|
|
|
|
ALCdevice *device;
|
|
|
|
ALCcontext *context;
|
|
|
|
ALuint buffers[NUM_BUFFERS];
|
2011-12-13 21:40:44 +01:00
|
|
|
unsigned filled;
|
2009-08-31 09:26:22 +02:00
|
|
|
ALuint source;
|
|
|
|
ALenum format;
|
|
|
|
ALuint frequency;
|
2013-04-17 01:04:27 +02:00
|
|
|
|
2013-08-04 12:25:08 +02:00
|
|
|
bool Initialize(const config_param ¶m, GError **error_r) {
|
2013-04-17 01:04:27 +02:00
|
|
|
return ao_base_init(&base, &openal_output_plugin, param,
|
|
|
|
error_r);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Deinitialize() {
|
|
|
|
ao_base_finish(&base);
|
|
|
|
}
|
2009-08-31 09:26:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline GQuark
|
|
|
|
openal_output_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("openal_output");
|
|
|
|
}
|
|
|
|
|
|
|
|
static ALenum
|
2013-08-03 21:00:50 +02:00
|
|
|
openal_audio_format(AudioFormat &audio_format)
|
2009-08-31 09:26:22 +02:00
|
|
|
{
|
2013-08-03 21:00:50 +02:00
|
|
|
/* note: cannot map SampleFormat::S8 to AL_FORMAT_STEREO8 or
|
2011-12-13 21:32:19 +01:00
|
|
|
AL_FORMAT_MONO8 since OpenAL expects unsigned 8 bit
|
|
|
|
samples, while MPD uses signed samples */
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
switch (audio_format.format) {
|
|
|
|
case SampleFormat::S16:
|
|
|
|
if (audio_format.channels == 2)
|
2009-08-31 09:26:22 +02:00
|
|
|
return AL_FORMAT_STEREO16;
|
2013-08-03 21:00:50 +02:00
|
|
|
if (audio_format.channels == 1)
|
2009-08-31 09:26:22 +02:00
|
|
|
return AL_FORMAT_MONO16;
|
2011-10-08 14:39:40 +02:00
|
|
|
|
|
|
|
/* fall back to mono */
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.channels = 1;
|
2011-10-08 14:39:40 +02:00
|
|
|
return openal_audio_format(audio_format);
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
default:
|
|
|
|
/* fall back to 16 bit */
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = SampleFormat::S16;
|
2011-10-08 14:39:40 +02:00
|
|
|
return openal_audio_format(audio_format);
|
2009-08-31 09:26:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-13 21:55:41 +01:00
|
|
|
G_GNUC_PURE
|
|
|
|
static inline ALint
|
2013-04-17 01:04:27 +02:00
|
|
|
openal_get_source_i(const OpenALOutput *od, ALenum param)
|
2011-12-13 21:55:41 +01:00
|
|
|
{
|
|
|
|
ALint value;
|
|
|
|
alGetSourcei(od->source, param, &value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_GNUC_PURE
|
|
|
|
static inline bool
|
2013-04-17 01:04:27 +02:00
|
|
|
openal_has_processed(const OpenALOutput *od)
|
2011-12-13 21:55:41 +01:00
|
|
|
{
|
|
|
|
return openal_get_source_i(od, AL_BUFFERS_PROCESSED) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_GNUC_PURE
|
|
|
|
static inline ALint
|
2013-04-17 01:04:27 +02:00
|
|
|
openal_is_playing(const OpenALOutput *od)
|
2011-12-13 21:55:41 +01:00
|
|
|
{
|
|
|
|
return openal_get_source_i(od, AL_SOURCE_STATE) == AL_PLAYING;
|
|
|
|
}
|
|
|
|
|
2009-08-31 09:26:22 +02:00
|
|
|
static bool
|
2013-04-17 01:04:27 +02:00
|
|
|
openal_setup_context(OpenALOutput *od,
|
2009-08-31 09:26:22 +02:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
od->device = alcOpenDevice(od->device_name);
|
|
|
|
|
2013-04-17 01:04:27 +02:00
|
|
|
if (od->device == nullptr) {
|
2009-08-31 09:26:22 +02:00
|
|
|
g_set_error(error, openal_output_quark(), 0,
|
|
|
|
"Error opening OpenAL device \"%s\"\n",
|
|
|
|
od->device_name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-17 01:04:27 +02:00
|
|
|
od->context = alcCreateContext(od->device, nullptr);
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2013-04-17 01:04:27 +02:00
|
|
|
if (od->context == nullptr) {
|
2009-08-31 09:26:22 +02:00
|
|
|
g_set_error(error, openal_output_quark(), 0,
|
|
|
|
"Error creating context for \"%s\"\n",
|
|
|
|
od->device_name);
|
|
|
|
alcCloseDevice(od->device);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
static struct audio_output *
|
2013-08-04 12:25:08 +02:00
|
|
|
openal_init(const config_param ¶m, GError **error_r)
|
2009-08-31 09:26:22 +02:00
|
|
|
{
|
2013-08-04 12:25:08 +02:00
|
|
|
const char *device_name = param.GetBlockValue("device");
|
2013-04-17 01:04:27 +02:00
|
|
|
if (device_name == nullptr) {
|
|
|
|
device_name = alcGetString(nullptr, ALC_DEFAULT_DEVICE_SPECIFIER);
|
2009-08-31 09:26:22 +02:00
|
|
|
}
|
|
|
|
|
2013-04-17 01:04:27 +02:00
|
|
|
OpenALOutput *od = new OpenALOutput();
|
|
|
|
if (!od->Initialize(param, error_r)) {
|
|
|
|
delete od;
|
|
|
|
return nullptr;
|
2011-09-16 23:31:48 +02:00
|
|
|
}
|
|
|
|
|
2009-09-06 21:33:34 +02:00
|
|
|
od->device_name = device_name;
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
return &od->base;
|
2009-08-31 09:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
openal_finish(struct audio_output *ao)
|
2009-08-31 09:26:22 +02:00
|
|
|
{
|
2013-04-17 01:04:27 +02:00
|
|
|
OpenALOutput *od = (OpenALOutput *)ao;
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2013-04-17 01:04:27 +02:00
|
|
|
od->Deinitialize();
|
|
|
|
delete od;
|
2009-08-31 09:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-08-03 21:00:50 +02:00
|
|
|
openal_open(struct audio_output *ao, AudioFormat &audio_format,
|
2009-08-31 09:26:22 +02:00
|
|
|
GError **error)
|
|
|
|
{
|
2013-04-17 01:04:27 +02:00
|
|
|
OpenALOutput *od = (OpenALOutput *)ao;
|
2009-08-31 09:26:22 +02:00
|
|
|
|
|
|
|
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) {
|
|
|
|
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;
|
2013-08-03 21:00:50 +02:00
|
|
|
od->frequency = audio_format.sample_rate;
|
2009-08-31 09:26:22 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
openal_close(struct audio_output *ao)
|
2009-08-31 09:26:22 +02:00
|
|
|
{
|
2013-04-17 01:04:27 +02:00
|
|
|
OpenALOutput *od = (OpenALOutput *)ao;
|
2009-08-31 09:26:22 +02:00
|
|
|
|
|
|
|
alcMakeContextCurrent(od->context);
|
|
|
|
alDeleteSources(1, &od->source);
|
|
|
|
alDeleteBuffers(NUM_BUFFERS, od->buffers);
|
|
|
|
alcDestroyContext(od->context);
|
|
|
|
alcCloseDevice(od->device);
|
|
|
|
}
|
|
|
|
|
2011-12-13 21:48:15 +01:00
|
|
|
static unsigned
|
|
|
|
openal_delay(struct audio_output *ao)
|
|
|
|
{
|
2013-04-17 01:04:27 +02:00
|
|
|
OpenALOutput *od = (OpenALOutput *)ao;
|
2011-12-13 21:48:15 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-08-31 09:26:22 +02:00
|
|
|
static size_t
|
2011-09-16 23:31:48 +02:00
|
|
|
openal_play(struct audio_output *ao, const void *chunk, size_t size,
|
2009-08-31 09:26:22 +02:00
|
|
|
G_GNUC_UNUSED GError **error)
|
|
|
|
{
|
2013-04-17 01:04:27 +02:00
|
|
|
OpenALOutput *od = (OpenALOutput *)ao;
|
2009-08-31 09:26:22 +02:00
|
|
|
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 */
|
2011-12-13 21:48:15 +01:00
|
|
|
while (!openal_has_processed(od))
|
|
|
|
g_usleep(10);
|
2009-08-31 09:26:22 +02:00
|
|
|
|
|
|
|
alSourceUnqueueBuffers(od->source, 1, &buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
alBufferData(buffer, od->format, chunk, size, od->frequency);
|
|
|
|
alSourceQueueBuffers(od->source, 1, &buffer);
|
|
|
|
|
2011-12-13 21:55:41 +01:00
|
|
|
if (!openal_is_playing(od))
|
2009-08-31 09:26:22 +02:00
|
|
|
alSourcePlay(od->source);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
openal_cancel(struct audio_output *ao)
|
2009-08-31 09:26:22 +02:00
|
|
|
{
|
2013-04-17 01:04:27 +02:00
|
|
|
OpenALOutput *od = (OpenALOutput *)ao;
|
2009-08-31 09:26:22 +02:00
|
|
|
|
|
|
|
od->filled = 0;
|
|
|
|
alcMakeContextCurrent(od->context);
|
|
|
|
alSourceStop(od->source);
|
2011-12-13 21:45:26 +01:00
|
|
|
|
|
|
|
/* force-unqueue all buffers */
|
|
|
|
alSourcei(od->source, AL_BUFFER, 0);
|
|
|
|
od->filled = 0;
|
2009-08-31 09:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct audio_output_plugin openal_output_plugin = {
|
2013-04-17 01:04:27 +02:00
|
|
|
"openal",
|
|
|
|
nullptr,
|
|
|
|
openal_init,
|
|
|
|
openal_finish,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
openal_open,
|
|
|
|
openal_close,
|
|
|
|
openal_delay,
|
|
|
|
nullptr,
|
|
|
|
openal_play,
|
|
|
|
nullptr,
|
|
|
|
openal_cancel,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2009-08-31 09:26:22 +02:00
|
|
|
};
|