OpenALOutputPlugin.cxx 5.14 KB
Newer Older
Serge Ziryukin's avatar
Serge Ziryukin committed
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "config.h"
21
#include "OpenALOutputPlugin.hxx"
22
#include "../OutputAPI.hxx"
23
#include "util/RuntimeError.hxx"
Serge Ziryukin's avatar
Serge Ziryukin committed
24

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

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

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

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

48
	OpenALOutput(const ConfigBlock &block);
49

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

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

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

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

72
	void Cancel() noexcept override;
73 74 75

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return size;
}

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

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

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