Commit b28710b0 authored by Max Kellermann's avatar Max Kellermann

output/openal: move functions into the class

parent 712ed555
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "config.h" #include "config.h"
#include "OpenALOutputPlugin.hxx" #include "OpenALOutputPlugin.hxx"
#include "../OutputAPI.hxx" #include "../OutputAPI.hxx"
#include "../Wrapper.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
...@@ -37,6 +38,8 @@ ...@@ -37,6 +38,8 @@
#define NUM_BUFFERS 16 #define NUM_BUFFERS 16
struct OpenALOutput { struct OpenALOutput {
friend struct AudioOutputWrapper<OpenALOutput>;
AudioOutput base; AudioOutput base;
const char *device_name; const char *device_name;
...@@ -51,9 +54,47 @@ struct OpenALOutput { ...@@ -51,9 +54,47 @@ struct OpenALOutput {
OpenALOutput() OpenALOutput()
:base(openal_output_plugin) {} :base(openal_output_plugin) {}
bool Initialize(const config_param &param, Error &error_r) { bool Configure(const config_param &param, Error &error);
return base.Configure(param, error_r);
static OpenALOutput *Create(const config_param &param, 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;
} }
gcc_pure
bool IsPlaying() const {
return GetSourceI(AL_SOURCE_STATE) == AL_PLAYING;
}
bool SetupContext(Error &error);
}; };
static constexpr Domain openal_output_domain("openal_output"); static constexpr Domain openal_output_domain("openal_output");
...@@ -83,200 +124,154 @@ openal_audio_format(AudioFormat &audio_format) ...@@ -83,200 +124,154 @@ openal_audio_format(AudioFormat &audio_format)
} }
} }
gcc_pure inline bool
static inline ALint OpenALOutput::SetupContext(Error &error)
openal_get_source_i(const OpenALOutput *od, ALenum param)
{
ALint value;
alGetSourcei(od->source, param, &value);
return value;
}
gcc_pure
static inline bool
openal_has_processed(const OpenALOutput *od)
{
return openal_get_source_i(od, AL_BUFFERS_PROCESSED) > 0;
}
gcc_pure
static inline ALint
openal_is_playing(const OpenALOutput *od)
{
return openal_get_source_i(od, AL_SOURCE_STATE) == AL_PLAYING;
}
static bool
openal_setup_context(OpenALOutput *od, Error &error)
{ {
od->device = alcOpenDevice(od->device_name); device = alcOpenDevice(device_name);
if (od->device == nullptr) { if (device == nullptr) {
error.Format(openal_output_domain, error.Format(openal_output_domain,
"Error opening OpenAL device \"%s\"", "Error opening OpenAL device \"%s\"",
od->device_name); device_name);
return false; return false;
} }
od->context = alcCreateContext(od->device, nullptr); context = alcCreateContext(device, nullptr);
if (od->context == nullptr) { if (context == nullptr) {
error.Format(openal_output_domain, error.Format(openal_output_domain,
"Error creating context for \"%s\"", "Error creating context for \"%s\"",
od->device_name); device_name);
alcCloseDevice(od->device); alcCloseDevice(device);
return false; return false;
} }
return true; return true;
} }
static AudioOutput * inline bool
openal_init(const config_param &param, Error &error) OpenALOutput::Configure(const config_param &param, Error &error)
{ {
const char *device_name = param.GetBlockValue("device"); if (!base.Configure(param, error))
if (device_name == nullptr) { return false;
device_name = alcGetString(nullptr, ALC_DEFAULT_DEVICE_SPECIFIER);
}
OpenALOutput *od = new OpenALOutput();
if (!od->Initialize(param, error)) {
delete od;
return nullptr;
}
od->device_name = device_name; device_name = param.GetBlockValue("device");
if (device_name == nullptr)
device_name = alcGetString(nullptr,
ALC_DEFAULT_DEVICE_SPECIFIER);
return &od->base; return true;
} }
static void inline OpenALOutput *
openal_finish(AudioOutput *ao) OpenALOutput::Create(const config_param &param, Error &error)
{ {
OpenALOutput *od = (OpenALOutput *)ao; OpenALOutput *oo = new OpenALOutput();
if (!oo->Configure(param, error)) {
delete oo;
return nullptr;
}
delete od; return oo;
} }
static bool inline bool
openal_open(AudioOutput *ao, AudioFormat &audio_format, OpenALOutput::Open(AudioFormat &audio_format, Error &error)
Error &error)
{ {
OpenALOutput *od = (OpenALOutput *)ao; format = openal_audio_format(audio_format);
od->format = openal_audio_format(audio_format); if (!SetupContext(error))
if (!openal_setup_context(od, error)) {
return false; return false;
}
alcMakeContextCurrent(od->context); alcMakeContextCurrent(context);
alGenBuffers(NUM_BUFFERS, od->buffers); alGenBuffers(NUM_BUFFERS, buffers);
if (alGetError() != AL_NO_ERROR) { if (alGetError() != AL_NO_ERROR) {
error.Set(openal_output_domain, "Failed to generate buffers"); error.Set(openal_output_domain, "Failed to generate buffers");
return false; return false;
} }
alGenSources(1, &od->source); alGenSources(1, &source);
if (alGetError() != AL_NO_ERROR) { if (alGetError() != AL_NO_ERROR) {
error.Set(openal_output_domain, "Failed to generate source"); error.Set(openal_output_domain, "Failed to generate source");
alDeleteBuffers(NUM_BUFFERS, od->buffers); alDeleteBuffers(NUM_BUFFERS, buffers);
return false; return false;
} }
od->filled = 0; filled = 0;
od->frequency = audio_format.sample_rate; frequency = audio_format.sample_rate;
return true; return true;
} }
static void inline void
openal_close(AudioOutput *ao) OpenALOutput::Close()
{ {
OpenALOutput *od = (OpenALOutput *)ao; alcMakeContextCurrent(context);
alDeleteSources(1, &source);
alcMakeContextCurrent(od->context); alDeleteBuffers(NUM_BUFFERS, buffers);
alDeleteSources(1, &od->source); alcDestroyContext(context);
alDeleteBuffers(NUM_BUFFERS, od->buffers); alcCloseDevice(device);
alcDestroyContext(od->context);
alcCloseDevice(od->device);
} }
static unsigned inline size_t
openal_delay(AudioOutput *ao) OpenALOutput::Play(const void *chunk, size_t size, gcc_unused Error &error)
{ {
OpenALOutput *od = (OpenALOutput *)ao; if (alcGetCurrentContext() != context)
alcMakeContextCurrent(context);
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;
}
static size_t
openal_play(AudioOutput *ao, const void *chunk, size_t size,
gcc_unused Error &error)
{
OpenALOutput *od = (OpenALOutput *)ao;
ALuint buffer; ALuint buffer;
if (filled < NUM_BUFFERS) {
if (alcGetCurrentContext() != od->context) {
alcMakeContextCurrent(od->context);
}
if (od->filled < NUM_BUFFERS) {
/* fill all buffers */ /* fill all buffers */
buffer = od->buffers[od->filled]; buffer = buffers[filled];
od->filled++; filled++;
} else { } else {
/* wait for processed buffer */ /* wait for processed buffer */
while (!openal_has_processed(od)) while (!HasProcessed())
usleep(10); usleep(10);
alSourceUnqueueBuffers(od->source, 1, &buffer); alSourceUnqueueBuffers(source, 1, &buffer);
} }
alBufferData(buffer, od->format, chunk, size, od->frequency); alBufferData(buffer, format, chunk, size, frequency);
alSourceQueueBuffers(od->source, 1, &buffer); alSourceQueueBuffers(source, 1, &buffer);
if (!openal_is_playing(od)) if (!IsPlaying())
alSourcePlay(od->source); alSourcePlay(source);
return size; return size;
} }
static void inline void
openal_cancel(AudioOutput *ao) OpenALOutput::Cancel()
{ {
OpenALOutput *od = (OpenALOutput *)ao; filled = 0;
alcMakeContextCurrent(context);
od->filled = 0; alSourceStop(source);
alcMakeContextCurrent(od->context);
alSourceStop(od->source);
/* force-unqueue all buffers */ /* force-unqueue all buffers */
alSourcei(od->source, AL_BUFFER, 0); alSourcei(source, AL_BUFFER, 0);
od->filled = 0; filled = 0;
} }
typedef AudioOutputWrapper<OpenALOutput> Wrapper;
const struct AudioOutputPlugin openal_output_plugin = { const struct AudioOutputPlugin openal_output_plugin = {
"openal", "openal",
nullptr, nullptr,
openal_init, &Wrapper::Init,
openal_finish, &Wrapper::Finish,
nullptr, nullptr,
nullptr, nullptr,
openal_open, &Wrapper::Open,
openal_close, &Wrapper::Close,
openal_delay, &Wrapper::Delay,
nullptr, nullptr,
openal_play, &Wrapper::Play,
nullptr, nullptr,
openal_cancel, &Wrapper::Cancel,
nullptr, nullptr,
nullptr, nullptr,
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment