output/OpenAL: migrate from class Error to C++ exceptions

This commit is contained in:
Max Kellermann 2016-11-08 15:19:30 +01:00
parent db7eec042e
commit 96f8f1da0c
1 changed files with 23 additions and 51 deletions

View File

@ -21,8 +21,7 @@
#include "OpenALOutputPlugin.hxx" #include "OpenALOutputPlugin.hxx"
#include "../OutputAPI.hxx" #include "../OutputAPI.hxx"
#include "../Wrapper.hxx" #include "../Wrapper.hxx"
#include "util/Error.hxx" #include "util/RuntimeError.hxx"
#include "util/Domain.hxx"
#include <unistd.h> #include <unistd.h>
@ -51,10 +50,7 @@ class OpenALOutput {
ALenum format; ALenum format;
ALuint frequency; ALuint frequency;
OpenALOutput() OpenALOutput(const ConfigBlock &block);
:base(openal_output_plugin) {}
bool Configure(const ConfigBlock &block, Error &error);
static OpenALOutput *Create(const ConfigBlock &block, Error &error); static OpenALOutput *Create(const ConfigBlock &block, Error &error);
@ -94,11 +90,12 @@ private:
return GetSourceI(AL_SOURCE_STATE) == AL_PLAYING; return GetSourceI(AL_SOURCE_STATE) == AL_PLAYING;
} }
bool SetupContext(Error &error); /**
* Throws #std::runtime_error on error.
*/
void SetupContext();
}; };
static constexpr Domain openal_output_domain("openal_output");
static ALenum static ALenum
openal_audio_format(AudioFormat &audio_format) openal_audio_format(AudioFormat &audio_format)
{ {
@ -124,80 +121,55 @@ openal_audio_format(AudioFormat &audio_format)
} }
} }
inline bool inline void
OpenALOutput::SetupContext(Error &error) OpenALOutput::SetupContext()
{ {
device = alcOpenDevice(device_name); device = alcOpenDevice(device_name);
if (device == nullptr)
if (device == nullptr) { throw FormatRuntimeError("Error opening OpenAL device \"%s\"",
error.Format(openal_output_domain, device_name);
"Error opening OpenAL device \"%s\"",
device_name);
return false;
}
context = alcCreateContext(device, nullptr); context = alcCreateContext(device, nullptr);
if (context == nullptr) { if (context == nullptr) {
error.Format(openal_output_domain,
"Error creating context for \"%s\"",
device_name);
alcCloseDevice(device); alcCloseDevice(device);
return false; throw FormatRuntimeError("Error creating context for \"%s\"",
device_name);
} }
return true;
} }
inline bool OpenALOutput::OpenALOutput(const ConfigBlock &block)
OpenALOutput::Configure(const ConfigBlock &block, Error &error) :base(openal_output_plugin, block),
device_name(block.GetBlockValue("device"))
{ {
if (!base.Configure(block, error))
return false;
device_name = block.GetBlockValue("device");
if (device_name == nullptr) if (device_name == nullptr)
device_name = alcGetString(nullptr, device_name = alcGetString(nullptr,
ALC_DEFAULT_DEVICE_SPECIFIER); ALC_DEFAULT_DEVICE_SPECIFIER);
return true;
} }
inline OpenALOutput * inline OpenALOutput *
OpenALOutput::Create(const ConfigBlock &block, Error &error) OpenALOutput::Create(const ConfigBlock &block, Error &)
{ {
OpenALOutput *oo = new OpenALOutput(); return new OpenALOutput(block);
if (!oo->Configure(block, error)) {
delete oo;
return nullptr;
}
return oo;
} }
inline bool inline bool
OpenALOutput::Open(AudioFormat &audio_format, Error &error) OpenALOutput::Open(AudioFormat &audio_format, Error &)
{ {
format = openal_audio_format(audio_format); format = openal_audio_format(audio_format);
if (!SetupContext(error)) SetupContext();
return false;
alcMakeContextCurrent(context); alcMakeContextCurrent(context);
alGenBuffers(NUM_BUFFERS, buffers); alGenBuffers(NUM_BUFFERS, buffers);
if (alGetError() != AL_NO_ERROR) { if (alGetError() != AL_NO_ERROR)
error.Set(openal_output_domain, "Failed to generate buffers"); throw std::runtime_error("Failed to generate buffers");
return false;
}
alGenSources(1, &source); alGenSources(1, &source);
if (alGetError() != AL_NO_ERROR) { if (alGetError() != AL_NO_ERROR) {
error.Set(openal_output_domain, "Failed to generate source");
alDeleteBuffers(NUM_BUFFERS, buffers); alDeleteBuffers(NUM_BUFFERS, buffers);
return false; throw std::runtime_error("Failed to generate source");
} }
filled = 0; filled = 0;