2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2013-04-17 01:04:27 +02:00
|
|
|
#include "OpenALOutputPlugin.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2022-11-28 21:58:21 +01:00
|
|
|
#include "lib/fmt/RuntimeError.hxx"
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2014-02-24 20:33:12 +01:00
|
|
|
#include <unistd.h>
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2013-12-02 13:08:03 +01:00
|
|
|
#ifndef __APPLE__
|
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>
|
2020-05-28 13:22:33 +02:00
|
|
|
/* on macOS, OpenAL is deprecated, but since the user asked to enable
|
|
|
|
this plugin, let's ignore the compiler warnings */
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
2009-09-07 14:01:38 +02:00
|
|
|
#endif
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
class OpenALOutput final : AudioOutput {
|
2015-01-03 20:00:41 +01:00
|
|
|
/* should be enough for buffer size = 2048 */
|
|
|
|
static constexpr unsigned NUM_BUFFERS = 16;
|
|
|
|
|
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
|
|
|
|
2020-02-01 13:59:55 +01:00
|
|
|
explicit OpenALOutput(const ConfigBlock &block);
|
2015-01-02 18:40:16 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
public:
|
|
|
|
static AudioOutput *Create(EventLoop &,
|
|
|
|
const ConfigBlock &block) {
|
|
|
|
return new OpenALOutput(block);
|
|
|
|
}
|
2015-01-02 18:40:16 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
private:
|
|
|
|
void Open(AudioFormat &audio_format) override;
|
|
|
|
void Close() noexcept override;
|
2015-01-02 18:40:16 +01:00
|
|
|
|
2023-03-06 15:57:36 +01:00
|
|
|
[[nodiscard]] [[gnu::pure]]
|
2017-08-09 16:58:44 +02:00
|
|
|
std::chrono::steady_clock::duration Delay() const noexcept override {
|
2015-01-02 18:40:16 +01:00
|
|
|
return filled < NUM_BUFFERS || HasProcessed()
|
2016-12-28 21:44:18 +01:00
|
|
|
? std::chrono::steady_clock::duration::zero()
|
2015-01-02 18:40:16 +01:00
|
|
|
/* we don't know exactly how long we must wait
|
|
|
|
for the next buffer to finish, so this is a
|
|
|
|
random guess: */
|
2016-12-28 21:44:18 +01:00
|
|
|
: std::chrono::milliseconds(50);
|
2015-01-02 18:40:16 +01:00
|
|
|
}
|
|
|
|
|
2022-07-12 12:31:35 +02:00
|
|
|
std::size_t Play(std::span<const std::byte> src) override;
|
2015-01-02 18:40:16 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void Cancel() noexcept override;
|
2015-01-02 18:40:16 +01:00
|
|
|
|
2023-03-06 15:57:36 +01:00
|
|
|
[[nodiscard]] [[gnu::pure]]
|
2017-06-03 21:33:44 +02:00
|
|
|
ALint GetSourceI(ALenum param) const noexcept {
|
2015-01-02 18:40:16 +01:00
|
|
|
ALint value;
|
|
|
|
alGetSourcei(source, param, &value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:57:36 +01:00
|
|
|
[[nodiscard]] [[gnu::pure]]
|
2017-06-03 21:33:44 +02:00
|
|
|
bool HasProcessed() const noexcept {
|
2015-01-02 18:40:16 +01:00
|
|
|
return GetSourceI(AL_BUFFERS_PROCESSED) > 0;
|
2013-04-17 01:04:27 +02:00
|
|
|
}
|
2015-01-02 18:40:16 +01:00
|
|
|
|
2023-03-06 15:57:36 +01:00
|
|
|
[[nodiscard]] [[gnu::pure]]
|
2017-06-03 21:33:44 +02:00
|
|
|
bool IsPlaying() const noexcept {
|
2015-01-02 18:40:16 +01:00
|
|
|
return GetSourceI(AL_SOURCE_STATE) == AL_PLAYING;
|
|
|
|
}
|
|
|
|
|
2016-11-08 15:19:30 +01:00
|
|
|
/**
|
2019-07-03 22:10:26 +02:00
|
|
|
* Throws on error.
|
2016-11-08 15:19:30 +01:00
|
|
|
*/
|
|
|
|
void SetupContext();
|
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
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 15:19:30 +01:00
|
|
|
inline void
|
|
|
|
OpenALOutput::SetupContext()
|
2009-08-31 09:26:22 +02:00
|
|
|
{
|
2015-01-02 18:40:16 +01:00
|
|
|
device = alcOpenDevice(device_name);
|
2016-11-08 15:19:30 +01:00
|
|
|
if (device == nullptr)
|
2022-11-28 21:58:21 +01:00
|
|
|
throw FmtRuntimeError("Error opening OpenAL device \"{}\"",
|
|
|
|
device_name);
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2015-01-02 18:40:16 +01:00
|
|
|
context = alcCreateContext(device, nullptr);
|
|
|
|
if (context == nullptr) {
|
|
|
|
alcCloseDevice(device);
|
2022-11-28 21:58:21 +01:00
|
|
|
throw FmtRuntimeError("Error creating context for \"{}\"",
|
|
|
|
device_name);
|
2009-08-31 09:26:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 15:19:30 +01:00
|
|
|
OpenALOutput::OpenALOutput(const ConfigBlock &block)
|
2017-08-09 16:58:44 +02:00
|
|
|
:AudioOutput(0),
|
2016-11-08 15:19:30 +01:00
|
|
|
device_name(block.GetBlockValue("device"))
|
2009-08-31 09:26:22 +02:00
|
|
|
{
|
2015-01-02 18:40:16 +01:00
|
|
|
if (device_name == nullptr)
|
|
|
|
device_name = alcGetString(nullptr,
|
|
|
|
ALC_DEFAULT_DEVICE_SPECIFIER);
|
2009-08-31 09:26:22 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
2016-11-09 11:56:01 +01:00
|
|
|
OpenALOutput::Open(AudioFormat &audio_format)
|
2009-08-31 09:26:22 +02:00
|
|
|
{
|
2015-01-02 18:40:16 +01:00
|
|
|
format = openal_audio_format(audio_format);
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2016-11-08 15:19:30 +01:00
|
|
|
SetupContext();
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2015-01-02 18:40:16 +01:00
|
|
|
alcMakeContextCurrent(context);
|
|
|
|
alGenBuffers(NUM_BUFFERS, buffers);
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2016-11-08 15:19:30 +01:00
|
|
|
if (alGetError() != AL_NO_ERROR)
|
|
|
|
throw std::runtime_error("Failed to generate buffers");
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2015-01-02 18:40:16 +01:00
|
|
|
alGenSources(1, &source);
|
2009-08-31 09:26:22 +02:00
|
|
|
|
|
|
|
if (alGetError() != AL_NO_ERROR) {
|
2015-01-02 18:40:16 +01:00
|
|
|
alDeleteBuffers(NUM_BUFFERS, buffers);
|
2016-11-08 15:19:30 +01:00
|
|
|
throw std::runtime_error("Failed to generate source");
|
2009-08-31 09:26:22 +02:00
|
|
|
}
|
|
|
|
|
2015-01-02 18:40:16 +01:00
|
|
|
filled = 0;
|
|
|
|
frequency = audio_format.sample_rate;
|
2009-08-31 09:26:22 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
|
|
|
OpenALOutput::Close() noexcept
|
2009-08-31 09:26:22 +02:00
|
|
|
{
|
2015-01-02 18:40:16 +01:00
|
|
|
alcMakeContextCurrent(context);
|
|
|
|
alDeleteSources(1, &source);
|
|
|
|
alDeleteBuffers(NUM_BUFFERS, buffers);
|
|
|
|
alcDestroyContext(context);
|
|
|
|
alcCloseDevice(device);
|
2009-08-31 09:26:22 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 12:31:35 +02:00
|
|
|
std::size_t
|
|
|
|
OpenALOutput::Play(std::span<const std::byte> src)
|
2011-12-13 21:48:15 +01:00
|
|
|
{
|
2015-01-02 18:40:16 +01:00
|
|
|
if (alcGetCurrentContext() != context)
|
|
|
|
alcMakeContextCurrent(context);
|
2011-12-13 21:48:15 +01:00
|
|
|
|
2009-08-31 09:26:22 +02:00
|
|
|
ALuint buffer;
|
2015-01-02 18:40:16 +01:00
|
|
|
if (filled < NUM_BUFFERS) {
|
2009-08-31 09:26:22 +02:00
|
|
|
/* fill all buffers */
|
2015-01-02 18:40:16 +01:00
|
|
|
buffer = buffers[filled];
|
|
|
|
filled++;
|
2009-08-31 09:26:22 +02:00
|
|
|
} else {
|
|
|
|
/* wait for processed buffer */
|
2015-01-02 18:40:16 +01:00
|
|
|
while (!HasProcessed())
|
2014-02-24 20:33:12 +01:00
|
|
|
usleep(10);
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2015-01-02 18:40:16 +01:00
|
|
|
alSourceUnqueueBuffers(source, 1, &buffer);
|
2009-08-31 09:26:22 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 12:31:35 +02:00
|
|
|
alBufferData(buffer, format, src.data(), src.size(), frequency);
|
2015-01-02 18:40:16 +01:00
|
|
|
alSourceQueueBuffers(source, 1, &buffer);
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2015-01-02 18:40:16 +01:00
|
|
|
if (!IsPlaying())
|
|
|
|
alSourcePlay(source);
|
2009-08-31 09:26:22 +02:00
|
|
|
|
2022-07-12 12:31:35 +02:00
|
|
|
return src.size();
|
2009-08-31 09:26:22 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
|
|
|
OpenALOutput::Cancel() noexcept
|
2009-08-31 09:26:22 +02:00
|
|
|
{
|
2015-01-02 18:40:16 +01:00
|
|
|
filled = 0;
|
|
|
|
alcMakeContextCurrent(context);
|
|
|
|
alSourceStop(source);
|
2011-12-13 21:45:26 +01:00
|
|
|
|
|
|
|
/* force-unqueue all buffers */
|
2015-01-02 18:40:16 +01:00
|
|
|
alSourcei(source, AL_BUFFER, 0);
|
|
|
|
filled = 0;
|
2009-08-31 09:26:22 +02:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:22:27 +01:00
|
|
|
const struct AudioOutputPlugin openal_output_plugin = {
|
2013-04-17 01:04:27 +02:00
|
|
|
"openal",
|
|
|
|
nullptr,
|
2017-08-09 16:58:44 +02:00
|
|
|
OpenALOutput::Create,
|
2013-04-17 01:04:27 +02:00
|
|
|
nullptr,
|
2009-08-31 09:26:22 +02:00
|
|
|
};
|