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

struct config_param;
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 47 48 49 50 51 52
	/**
	 * 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.
	 */
	bool (*test_default_device)(void);

	/**
	 * 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 config_param &param,
59
				     Error &error);
60 61 62 63

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

66 67 68 69 70 71 72 73
	/**
	 * 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
	 */
74
	bool (*enable)(AudioOutput *data, Error &error);
75 76 77 78 79

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

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

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

96 97 98 99 100 101 102 103
	/**
	 * 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
	 */
104
	unsigned (*delay)(AudioOutput *data);
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
	 *
	 * @return the number of bytes played, or 0 on error
116
	 */
117
	size_t (*play)(AudioOutput *data,
118
		       const void *chunk, size_t size,
119
		       Error &error);
120

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

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

132 133 134 135 136 137 138 139 140 141 142
	/**
	 * 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
	 */
143
	bool (*pause)(AudioOutput *data);
144 145 146

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

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

162
gcc_malloc
163
AudioOutput *
164
ao_plugin_init(const AudioOutputPlugin *plugin,
165
	       const config_param &param,
166
	       Error &error);
167

168
void
169
ao_plugin_finish(AudioOutput *ao);
170

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

174
void
175
ao_plugin_disable(AudioOutput *ao);
176

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

181
void
182
ao_plugin_close(AudioOutput *ao);
183

184
gcc_pure
185
unsigned
186
ao_plugin_delay(AudioOutput *ao);
187

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

191
size_t
192
ao_plugin_play(AudioOutput *ao, const void *chunk, size_t size,
193
	       Error &error);
194

195
void
196
ao_plugin_drain(AudioOutput *ao);
197

198
void
199
ao_plugin_cancel(AudioOutput *ao);
200

201
bool
202
ao_plugin_pause(AudioOutput *ao);
203

204
#endif