OutputPlugin.hxx 4.95 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 <stddef.h>

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

/**
 * A plugin which controls an audio output device.
 */
37
struct AudioOutputPlugin {
38 39 40 41 42 43 44 45 46
	/**
	 * 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.
	 */
47
	bool (*test_default_device)();
48 49 50 51 52

	/**
	 * Configure and initialize the device, but do not open it
	 * yet.
	 *
53
	 * @param param the configuration section, or nullptr if there is
54
	 * no configuration
55
	 * @return nullptr on error, or an opaque pointer to the plugin's
56 57
	 * data
	 */
58
	AudioOutput *(*init)(const ConfigBlock &block, Error &error);
59 60 61 62

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

65 66 67 68 69 70 71 72
	/**
	 * Enable the device.  This may allocate resources, preparing
	 * for the device to be opened.  Enabling a device cannot
	 * fail: if an error occurs during that, it should be reported
	 * by the open() method.
	 *
	 * @return true on success, false on error
	 */
73
	bool (*enable)(AudioOutput *data, Error &error);
74 75 76 77 78

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

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

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

95 96 97 98 99 100 101 102
	/**
	 * Returns a positive number if the output thread shall delay
	 * the next call to play() or pause().  This should be
	 * implemented instead of doing a sleep inside the plugin,
	 * because this allows MPD to listen to commands meanwhile.
	 *
	 * @return the number of milliseconds to wait
	 */
103
	unsigned (*delay)(AudioOutput *data);
104

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

111 112
	/**
	 * Play a chunk of audio data.
113 114
	 *
	 * @return the number of bytes played, or 0 on error
115
	 */
116
	size_t (*play)(AudioOutput *data,
117
		       const void *chunk, size_t size,
118
		       Error &error);
119

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

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

131 132 133 134 135 136 137 138 139 140 141
	/**
	 * 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.
	 *
	 * @return false on error (output will be closed then), true
	 * for continue to pause
	 */
142
	bool (*pause)(AudioOutput *data);
143 144 145

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

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

161
gcc_malloc
162
AudioOutput *
163
ao_plugin_init(const AudioOutputPlugin *plugin,
164
	       const ConfigBlock &block,
165
	       Error &error);
166

167
void
168
ao_plugin_finish(AudioOutput *ao);
169

170
bool
171
ao_plugin_enable(AudioOutput *ao, Error &error);
172

173
void
174
ao_plugin_disable(AudioOutput *ao);
175

176
bool
177
ao_plugin_open(AudioOutput *ao, AudioFormat &audio_format,
178
	       Error &error);
179

180
void
181
ao_plugin_close(AudioOutput *ao);
182

183
gcc_pure
184
unsigned
185
ao_plugin_delay(AudioOutput *ao);
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
	       Error &error);
193

194
void
195
ao_plugin_drain(AudioOutput *ao);
196

197
void
198
ao_plugin_cancel(AudioOutput *ao);
199

200
bool
201
ao_plugin_pause(AudioOutput *ao);
202

203
#endif