OpenALOutputPlugin.cxx 5.13 KB
Newer Older
Serge Ziryukin's avatar
Serge Ziryukin committed
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
Serge Ziryukin's avatar
Serge Ziryukin committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "OpenALOutputPlugin.hxx"
21
#include "../OutputAPI.hxx"
22
#include "util/RuntimeError.hxx"
Serge Ziryukin's avatar
Serge Ziryukin committed
23

24
#include <unistd.h>
Serge Ziryukin's avatar
Serge Ziryukin committed
25

26
#ifndef __APPLE__
Serge Ziryukin's avatar
Serge Ziryukin committed
27 28
#include <AL/al.h>
#include <AL/alc.h>
29 30 31 32
#else
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#endif
Serge Ziryukin's avatar
Serge Ziryukin committed
33

34
class OpenALOutput final : AudioOutput {
35 36 37
	/* should be enough for buffer size = 2048 */
	static constexpr unsigned NUM_BUFFERS = 16;

Serge Ziryukin's avatar
Serge Ziryukin committed
38 39 40 41
	const char *device_name;
	ALCdevice *device;
	ALCcontext *context;
	ALuint buffers[NUM_BUFFERS];
42
	unsigned filled;
Serge Ziryukin's avatar
Serge Ziryukin committed
43 44 45
	ALuint source;
	ALenum format;
	ALuint frequency;
46

47
	OpenALOutput(const ConfigBlock &block);
48

49 50 51 52 53
public:
	static AudioOutput *Create(EventLoop &,
				   const ConfigBlock &block) {
		return new OpenALOutput(block);
	}
54

55 56 57
private:
	void Open(AudioFormat &audio_format) override;
	void Close() noexcept override;
58 59

	gcc_pure
60
	std::chrono::steady_clock::duration Delay() const noexcept override {
61
		return filled < NUM_BUFFERS || HasProcessed()
62
			? std::chrono::steady_clock::duration::zero()
63 64 65
			/* we don't know exactly how long we must wait
			   for the next buffer to finish, so this is a
			   random guess: */
66
			: std::chrono::milliseconds(50);
67 68
	}

69
	size_t Play(const void *chunk, size_t size) override;
70

71
	void Cancel() noexcept override;
72 73 74

private:
	gcc_pure
75
	ALint GetSourceI(ALenum param) const noexcept {
76 77 78 79 80 81
		ALint value;
		alGetSourcei(source, param, &value);
		return value;
	}

	gcc_pure
82
	bool HasProcessed() const noexcept {
83
		return GetSourceI(AL_BUFFERS_PROCESSED) > 0;
84
	}
85 86

	gcc_pure
87
	bool IsPlaying() const noexcept {
88 89 90
		return GetSourceI(AL_SOURCE_STATE) == AL_PLAYING;
	}

91 92 93 94
	/**
	 * Throws #std::runtime_error on error.
	 */
	void SetupContext();
Serge Ziryukin's avatar
Serge Ziryukin committed
95 96 97
};

static ALenum
98
openal_audio_format(AudioFormat &audio_format)
Serge Ziryukin's avatar
Serge Ziryukin committed
99
{
100
	/* note: cannot map SampleFormat::S8 to AL_FORMAT_STEREO8 or
101 102 103
	   AL_FORMAT_MONO8 since OpenAL expects unsigned 8 bit
	   samples, while MPD uses signed samples */

104 105 106
	switch (audio_format.format) {
	case SampleFormat::S16:
		if (audio_format.channels == 2)
Serge Ziryukin's avatar
Serge Ziryukin committed
107
			return AL_FORMAT_STEREO16;
108
		if (audio_format.channels == 1)
Serge Ziryukin's avatar
Serge Ziryukin committed
109
			return AL_FORMAT_MONO16;
110 111

		/* fall back to mono */
112
		audio_format.channels = 1;
113
		return openal_audio_format(audio_format);
Serge Ziryukin's avatar
Serge Ziryukin committed
114

115 116
	default:
		/* fall back to 16 bit */
117
		audio_format.format = SampleFormat::S16;
118
		return openal_audio_format(audio_format);
Serge Ziryukin's avatar
Serge Ziryukin committed
119 120 121
	}
}

122 123
inline void
OpenALOutput::SetupContext()
Serge Ziryukin's avatar
Serge Ziryukin committed
124
{
125
	device = alcOpenDevice(device_name);
126 127 128
	if (device == nullptr)
		throw FormatRuntimeError("Error opening OpenAL device \"%s\"",
					 device_name);
Serge Ziryukin's avatar
Serge Ziryukin committed
129

130 131 132
	context = alcCreateContext(device, nullptr);
	if (context == nullptr) {
		alcCloseDevice(device);
133 134
		throw FormatRuntimeError("Error creating context for \"%s\"",
					 device_name);
Serge Ziryukin's avatar
Serge Ziryukin committed
135 136 137
	}
}

138
OpenALOutput::OpenALOutput(const ConfigBlock &block)
139
	:AudioOutput(0),
140
	 device_name(block.GetBlockValue("device"))
Serge Ziryukin's avatar
Serge Ziryukin committed
141
{
142 143 144
	if (device_name == nullptr)
		device_name = alcGetString(nullptr,
					   ALC_DEFAULT_DEVICE_SPECIFIER);
Serge Ziryukin's avatar
Serge Ziryukin committed
145 146
}

147
void
148
OpenALOutput::Open(AudioFormat &audio_format)
Serge Ziryukin's avatar
Serge Ziryukin committed
149
{
150
	format = openal_audio_format(audio_format);
Serge Ziryukin's avatar
Serge Ziryukin committed
151

152
	SetupContext();
Serge Ziryukin's avatar
Serge Ziryukin committed
153

154 155
	alcMakeContextCurrent(context);
	alGenBuffers(NUM_BUFFERS, buffers);
Serge Ziryukin's avatar
Serge Ziryukin committed
156

157 158
	if (alGetError() != AL_NO_ERROR)
		throw std::runtime_error("Failed to generate buffers");
Serge Ziryukin's avatar
Serge Ziryukin committed
159

160
	alGenSources(1, &source);
Serge Ziryukin's avatar
Serge Ziryukin committed
161 162

	if (alGetError() != AL_NO_ERROR) {
163
		alDeleteBuffers(NUM_BUFFERS, buffers);
164
		throw std::runtime_error("Failed to generate source");
Serge Ziryukin's avatar
Serge Ziryukin committed
165 166
	}

167 168
	filled = 0;
	frequency = audio_format.sample_rate;
Serge Ziryukin's avatar
Serge Ziryukin committed
169 170
}

171 172
void
OpenALOutput::Close() noexcept
Serge Ziryukin's avatar
Serge Ziryukin committed
173
{
174 175 176 177 178
	alcMakeContextCurrent(context);
	alDeleteSources(1, &source);
	alDeleteBuffers(NUM_BUFFERS, buffers);
	alcDestroyContext(context);
	alcCloseDevice(device);
Serge Ziryukin's avatar
Serge Ziryukin committed
179 180
}

181
size_t
182
OpenALOutput::Play(const void *chunk, size_t size)
183
{
184 185
	if (alcGetCurrentContext() != context)
		alcMakeContextCurrent(context);
186

Serge Ziryukin's avatar
Serge Ziryukin committed
187
	ALuint buffer;
188
	if (filled < NUM_BUFFERS) {
Serge Ziryukin's avatar
Serge Ziryukin committed
189
		/* fill all buffers */
190 191
		buffer = buffers[filled];
		filled++;
Serge Ziryukin's avatar
Serge Ziryukin committed
192 193
	} else {
		/* wait for processed buffer */
194
		while (!HasProcessed())
195
			usleep(10);
Serge Ziryukin's avatar
Serge Ziryukin committed
196

197
		alSourceUnqueueBuffers(source, 1, &buffer);
Serge Ziryukin's avatar
Serge Ziryukin committed
198 199
	}

200 201
	alBufferData(buffer, format, chunk, size, frequency);
	alSourceQueueBuffers(source, 1, &buffer);
Serge Ziryukin's avatar
Serge Ziryukin committed
202

203 204
	if (!IsPlaying())
		alSourcePlay(source);
Serge Ziryukin's avatar
Serge Ziryukin committed
205 206 207 208

	return size;
}

209 210
void
OpenALOutput::Cancel() noexcept
Serge Ziryukin's avatar
Serge Ziryukin committed
211
{
212 213 214
	filled = 0;
	alcMakeContextCurrent(context);
	alSourceStop(source);
215 216

	/* force-unqueue all buffers */
217 218
	alSourcei(source, AL_BUFFER, 0);
	filled = 0;
Serge Ziryukin's avatar
Serge Ziryukin committed
219 220
}

221
const struct AudioOutputPlugin openal_output_plugin = {
222 223
	"openal",
	nullptr,
224
	OpenALOutput::Create,
225
	nullptr,
Serge Ziryukin's avatar
Serge Ziryukin committed
226
};