OutputPlugin.hxx 4.85 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

Max Kellermann's avatar
Max Kellermann committed
20 21
#ifndef MPD_OUTPUT_PLUGIN_HXX
#define MPD_OUTPUT_PLUGIN_HXX
22

23
#include "Compiler.h"
24

25 26
#include <chrono>

27 28
#include <stddef.h>

29
struct ConfigBlock;
30
struct AudioFormat;
Max Kellermann's avatar
Max Kellermann committed
31
struct Tag;
32
struct AudioOutput;
33
struct MixerPlugin;
34 35 36 37

/**
 * A plugin which controls an audio output device.
 */
38
struct AudioOutputPlugin {
39 40 41 42 43 44 45 46 47
	/**
	 * the plugin's name
	 */
	const char *name;

	/**
	 * Test if this plugin can provide a default output, in case
	 * none has been configured.  This method is optional.
	 */
48
	bool (*test_default_device)();
49 50 51 52 53

	/**
	 * Configure and initialize the device, but do not open it
	 * yet.
	 *
54 55
	 * Throws #std::runtime_error on error.
	 *
56
	 * @param param the configuration section, or nullptr if there is
57 58
	 * no configuration
	 */
59
	AudioOutput *(*init)(const ConfigBlock &block);
60 61 62 63

	/**
	 * Free resources allocated by this device.
	 */
64
	void (*finish)(AudioOutput *data);
65

66 67
	/**
	 * Enable the device.  This may allocate resources, preparing
68
	 * for the device to be opened.
69
	 *
70
	 * Throws #std::runtime_error on error.
71
	 */
72
	void (*enable)(AudioOutput *data);
73 74 75 76 77

	/**
	 * Disables the device.  It is closed before this method is
	 * called.
	 */
78
	void (*disable)(AudioOutput *data);
79

80 81
	/**
	 * Really open the device.
82
	 *
83 84
	 * Throws #std::runtime_error on error.
	 *
85 86 87
	 * @param audio_format the audio format in which data is going
	 * to be delivered; may be modified by the plugin
	 */
88
	void (*open)(AudioOutput *data, AudioFormat &audio_format);
89

90 91 92
	/**
	 * Close the device.
	 */
93
	void (*close)(AudioOutput *data);
94

95
	/**
96 97 98
	 * Returns a positive number if the output thread shall further
	 * delay the next call to play() or pause(), which will happen
	 * until this function returns 0.  This should be implemented
99
	 * instead of doing a sleep inside the plugin, because this
100
	 * allows MPD to listen to commands meanwhile.
101
	 *
102
	 * @return the duration to wait
103
	 */
104
	std::chrono::steady_clock::duration (*delay)(AudioOutput *data) noexcept;
105

106 107 108 109
	/**
	 * Display metadata for the next chunk.  Optional method,
	 * because not all devices can display metadata.
	 */
110
	void (*send_tag)(AudioOutput *data, const Tag &tag);
111

112 113
	/**
	 * Play a chunk of audio data.
114
	 *
115 116
	 * Throws #std::runtime_error on error.
	 *
117
	 * @return the number of bytes played
118
	 */
119
	size_t (*play)(AudioOutput *data,
120
		       const void *chunk, size_t size);
121

122 123 124
	/**
	 * Wait until the device has finished playing.
	 */
125
	void (*drain)(AudioOutput *data);
126

127 128 129 130
	/**
	 * Try to cancel data which may still be in the device's
	 * buffers.
	 */
131
	void (*cancel)(AudioOutput *data);
132

133 134 135 136 137 138 139 140
	/**
	 * Pause the device.  If supported, it may perform a special
	 * action, which keeps the device open, but does not play
	 * anything.  Output plugins like "shout" might want to play
	 * silence during pause, so their clients won't be
	 * disconnected.  Plugins which do not support pausing will
	 * simply be closed, and have to be reopened when unpaused.
	 *
141 142
	 * @return false on error (output will be closed by caller),
	 * true for continue to pause
143
	 */
144
	bool (*pause)(AudioOutput *data);
145 146 147

	/**
	 * The mixer plugin associated with this output plugin.  This
148
	 * may be nullptr if no mixer plugin is implemented.  When
149
	 * created, this mixer plugin gets the same #ConfigParam as
150 151
	 * this audio output device.
	 */
152
	const MixerPlugin *mixer_plugin;
153 154
};

155
static inline bool
156
ao_plugin_test_default_device(const AudioOutputPlugin *plugin)
157
{
158
	return plugin->test_default_device != nullptr
159 160 161 162
		? plugin->test_default_device()
		: false;
}

163
gcc_malloc
164
AudioOutput *
165
ao_plugin_init(const AudioOutputPlugin *plugin,
166
	       const ConfigBlock &block);
167

168
void
169
ao_plugin_finish(AudioOutput *ao);
170

171 172
void
ao_plugin_enable(AudioOutput *ao);
173

174
void
175
ao_plugin_disable(AudioOutput *ao);
176

177 178
void
ao_plugin_open(AudioOutput *ao, AudioFormat &audio_format);
179

180
void
181
ao_plugin_close(AudioOutput *ao);
182

183
gcc_pure
184
std::chrono::steady_clock::duration
185
ao_plugin_delay(AudioOutput *ao) noexcept;
186

187
void
188
ao_plugin_send_tag(AudioOutput *ao, const Tag &tag);
189

190
size_t
191
ao_plugin_play(AudioOutput *ao, const void *chunk, size_t size);
192

193
void
194
ao_plugin_drain(AudioOutput *ao);
195

196
void
197
ao_plugin_cancel(AudioOutput *ao);
198

199
bool
200
ao_plugin_pause(AudioOutput *ao);
201

202
#endif