mpd/src/output/plugins/OpenALOutputPlugin.cxx

278 lines
5.8 KiB
C++
Raw Normal View History

2009-08-31 09:26:22 +02:00
/*
2016-02-26 17:54:05 +01:00
* Copyright 2003-2016 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.
*/
#include "config.h"
2013-04-17 01:04:27 +02:00
#include "OpenALOutputPlugin.hxx"
2014-01-23 23:49:50 +01:00
#include "../OutputAPI.hxx"
#include "../Wrapper.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
2009-08-31 09:26:22 +02:00
#include <unistd.h>
2009-08-31 09:26:22 +02:00
#ifndef __APPLE__
2009-08-31 09:26:22 +02:00
#include <AL/al.h>
#include <AL/alc.h>
#else
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#endif
2009-08-31 09:26:22 +02:00
2015-01-03 20:00:16 +01:00
class OpenALOutput {
friend struct AudioOutputWrapper<OpenALOutput>;
/* should be enough for buffer size = 2048 */
static constexpr unsigned NUM_BUFFERS = 16;
AudioOutput base;
2009-08-31 09:26:22 +02:00
const char *device_name;
ALCdevice *device;
ALCcontext *context;
ALuint buffers[NUM_BUFFERS];
unsigned filled;
2009-08-31 09:26:22 +02:00
ALuint source;
ALenum format;
ALuint frequency;
2013-04-17 01:04:27 +02:00
OpenALOutput()
:base(openal_output_plugin) {}
bool Configure(const ConfigBlock &block, Error &error);
static OpenALOutput *Create(const ConfigBlock &block, Error &error);
bool Open(AudioFormat &audio_format, Error &error);
void Close();
gcc_pure
unsigned Delay() const {
return filled < NUM_BUFFERS || HasProcessed()
? 0
/* we don't know exactly how long we must wait
for the next buffer to finish, so this is a
random guess: */
: 50;
}
size_t Play(const void *chunk, size_t size, Error &error);
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;
2013-04-17 01:04:27 +02:00
}
gcc_pure
bool IsPlaying() const {
return GetSourceI(AL_SOURCE_STATE) == AL_PLAYING;
}
bool SetupContext(Error &error);
2009-08-31 09:26:22 +02:00
};
static constexpr Domain openal_output_domain("openal_output");
2009-08-31 09:26:22 +02:00
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
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;
/* fall back to mono */
2013-08-03 21:00:50 +02:00
audio_format.channels = 1;
return openal_audio_format(audio_format);
2009-08-31 09:26:22 +02:00
default:
/* fall back to 16 bit */
2013-08-03 21:00:50 +02:00
audio_format.format = SampleFormat::S16;
return openal_audio_format(audio_format);
2009-08-31 09:26:22 +02:00
}
}
inline bool
OpenALOutput::SetupContext(Error &error)
2009-08-31 09:26:22 +02:00
{
device = alcOpenDevice(device_name);
2009-08-31 09:26:22 +02:00
if (device == nullptr) {
error.Format(openal_output_domain,
"Error opening OpenAL device \"%s\"",
device_name);
2009-08-31 09:26:22 +02:00
return false;
}
context = alcCreateContext(device, nullptr);
2009-08-31 09:26:22 +02:00
if (context == nullptr) {
error.Format(openal_output_domain,
"Error creating context for \"%s\"",
device_name);
alcCloseDevice(device);
2009-08-31 09:26:22 +02:00
return false;
}
return true;
}
inline bool
OpenALOutput::Configure(const ConfigBlock &block, Error &error)
2009-08-31 09:26:22 +02:00
{
if (!base.Configure(block, error))
return false;
device_name = block.GetBlockValue("device");
if (device_name == nullptr)
device_name = alcGetString(nullptr,
ALC_DEFAULT_DEVICE_SPECIFIER);
2009-09-06 21:33:34 +02:00
return true;
2009-08-31 09:26:22 +02:00
}
inline OpenALOutput *
OpenALOutput::Create(const ConfigBlock &block, Error &error)
2009-08-31 09:26:22 +02:00
{
OpenALOutput *oo = new OpenALOutput();
if (!oo->Configure(block, error)) {
delete oo;
return nullptr;
}
2009-08-31 09:26:22 +02:00
return oo;
2009-08-31 09:26:22 +02:00
}
inline bool
OpenALOutput::Open(AudioFormat &audio_format, Error &error)
2009-08-31 09:26:22 +02:00
{
format = openal_audio_format(audio_format);
2009-08-31 09:26:22 +02:00
if (!SetupContext(error))
2009-08-31 09:26:22 +02:00
return false;
alcMakeContextCurrent(context);
alGenBuffers(NUM_BUFFERS, buffers);
2009-08-31 09:26:22 +02:00
if (alGetError() != AL_NO_ERROR) {
error.Set(openal_output_domain, "Failed to generate buffers");
2009-08-31 09:26:22 +02:00
return false;
}
alGenSources(1, &source);
2009-08-31 09:26:22 +02:00
if (alGetError() != AL_NO_ERROR) {
error.Set(openal_output_domain, "Failed to generate source");
alDeleteBuffers(NUM_BUFFERS, buffers);
2009-08-31 09:26:22 +02:00
return false;
}
filled = 0;
frequency = audio_format.sample_rate;
2009-08-31 09:26:22 +02:00
return true;
}
inline void
OpenALOutput::Close()
2009-08-31 09:26:22 +02:00
{
alcMakeContextCurrent(context);
alDeleteSources(1, &source);
alDeleteBuffers(NUM_BUFFERS, buffers);
alcDestroyContext(context);
alcCloseDevice(device);
2009-08-31 09:26:22 +02:00
}
inline size_t
OpenALOutput::Play(const void *chunk, size_t size, gcc_unused Error &error)
{
if (alcGetCurrentContext() != context)
alcMakeContextCurrent(context);
2009-08-31 09:26:22 +02:00
ALuint buffer;
if (filled < NUM_BUFFERS) {
2009-08-31 09:26:22 +02:00
/* fill all buffers */
buffer = buffers[filled];
filled++;
2009-08-31 09:26:22 +02:00
} else {
/* wait for processed buffer */
while (!HasProcessed())
usleep(10);
2009-08-31 09:26:22 +02:00
alSourceUnqueueBuffers(source, 1, &buffer);
2009-08-31 09:26:22 +02:00
}
alBufferData(buffer, format, chunk, size, frequency);
alSourceQueueBuffers(source, 1, &buffer);
2009-08-31 09:26:22 +02:00
if (!IsPlaying())
alSourcePlay(source);
2009-08-31 09:26:22 +02:00
return size;
}
inline void
OpenALOutput::Cancel()
2009-08-31 09:26:22 +02:00
{
filled = 0;
alcMakeContextCurrent(context);
alSourceStop(source);
/* force-unqueue all buffers */
alSourcei(source, AL_BUFFER, 0);
filled = 0;
2009-08-31 09:26:22 +02:00
}
typedef AudioOutputWrapper<OpenALOutput> Wrapper;
const struct AudioOutputPlugin openal_output_plugin = {
2013-04-17 01:04:27 +02:00
"openal",
nullptr,
&Wrapper::Init,
&Wrapper::Finish,
2013-04-17 01:04:27 +02:00
nullptr,
nullptr,
&Wrapper::Open,
&Wrapper::Close,
&Wrapper::Delay,
2013-04-17 01:04:27 +02:00
nullptr,
&Wrapper::Play,
2013-04-17 01:04:27 +02:00
nullptr,
&Wrapper::Cancel,
2013-04-17 01:04:27 +02:00
nullptr,
nullptr,
2009-08-31 09:26:22 +02:00
};