OutputPlugin.hxx 5.15 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 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
class Error;
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

/**
 * A plugin which controls an audio output device.
 */
struct audio_output_plugin {
	/**
	 * 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.
	 *
51
	 * @param param the configuration section, or nullptr if there is
52
	 * no configuration
53
	 * @return nullptr on error, or an opaque pointer to the plugin's
54 55
	 * data
	 */
56
	struct audio_output *(*init)(const config_param &param,
57
				     Error &error);
58 59 60 61

	/**
	 * Free resources allocated by this device.
	 */
62
	void (*finish)(struct audio_output *data);
63

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

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

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

89 90 91
	/**
	 * Close the device.
	 */
92
	void (*close)(struct audio_output *data);
93

94 95 96 97 98 99 100 101
	/**
	 * 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
	 */
102
	unsigned (*delay)(struct audio_output *data);
103

104 105 106 107
	/**
	 * Display metadata for the next chunk.  Optional method,
	 * because not all devices can display metadata.
	 */
Max Kellermann's avatar
Max Kellermann committed
108
	void (*send_tag)(struct audio_output *data, const Tag *tag);
109

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

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

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

130 131 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.
	 *
	 * @return false on error (output will be closed then), true
	 * for continue to pause
	 */
141
	bool (*pause)(struct audio_output *data);
142 143 144

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

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

160
gcc_malloc
161
struct audio_output *
162
ao_plugin_init(const struct audio_output_plugin *plugin,
163
	       const config_param &param,
164
	       Error &error);
165

166 167
void
ao_plugin_finish(struct audio_output *ao);
168

169
bool
170
ao_plugin_enable(struct audio_output *ao, Error &error);
171

172 173
void
ao_plugin_disable(struct audio_output *ao);
174

175
bool
176
ao_plugin_open(struct audio_output *ao, AudioFormat &audio_format,
177
	       Error &error);
178

179 180
void
ao_plugin_close(struct audio_output *ao);
181

182
gcc_pure
183 184
unsigned
ao_plugin_delay(struct audio_output *ao);
185

186
void
Max Kellermann's avatar
Max Kellermann committed
187
ao_plugin_send_tag(struct audio_output *ao, const Tag *tag);
188

189 190
size_t
ao_plugin_play(struct audio_output *ao, const void *chunk, size_t size,
191
	       Error &error);
192

193 194
void
ao_plugin_drain(struct audio_output *ao);
195

196 197
void
ao_plugin_cancel(struct audio_output *ao);
198

199 200
bool
ao_plugin_pause(struct audio_output *ao);
201

202
#endif