OutputPlugin.hxx 4.96 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
class EventLoop;
35 36 37 38

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

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

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

67 68
	/**
	 * Enable the device.  This may allocate resources, preparing
69
	 * for the device to be opened.
70
	 *
71
	 * Throws #std::runtime_error on error.
72
	 */
73
	void (*enable)(AudioOutput *data);
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
	 * Throws #std::runtime_error on error.
	 *
86 87 88
	 * @param audio_format the audio format in which data is going
	 * to be delivered; may be modified by the plugin
	 */
89
	void (*open)(AudioOutput *data, AudioFormat &audio_format);
90

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

96
	/**
97 98 99
	 * 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
100
	 * instead of doing a sleep inside the plugin, because this
101
	 * allows MPD to listen to commands meanwhile.
102
	 *
103
	 * @return the duration to wait
104
	 */
105
	std::chrono::steady_clock::duration (*delay)(AudioOutput *data) noexcept;
106

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

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

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

128 129 130 131
	/**
	 * Try to cancel data which may still be in the device's
	 * buffers.
	 */
132
	void (*cancel)(AudioOutput *data);
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.
	 *
142 143
	 * @return false on error (output will be closed by caller),
	 * true for continue to pause
144
	 */
145
	bool (*pause)(AudioOutput *data);
146 147 148

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

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

164
gcc_malloc
165
AudioOutput *
166 167
ao_plugin_init(EventLoop &event_loop,
	       const AudioOutputPlugin &plugin,
168
	       const ConfigBlock &block);
169

170
void
171
ao_plugin_finish(AudioOutput *ao) noexcept;
172

173
void
174
ao_plugin_enable(AudioOutput &ao);
175

176
void
177
ao_plugin_disable(AudioOutput &ao) noexcept;
178

179
void
180
ao_plugin_open(AudioOutput &ao, AudioFormat &audio_format);
181

182
void
183
ao_plugin_close(AudioOutput &ao) noexcept;
184

185
gcc_pure
186
std::chrono::steady_clock::duration
Max Kellermann's avatar
Max Kellermann committed
187
ao_plugin_delay(AudioOutput &ao) noexcept;
188

189
void
190
ao_plugin_send_tag(AudioOutput &ao, const Tag &tag);
191

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

195
void
196
ao_plugin_drain(AudioOutput &ao);
197

198
void
199
ao_plugin_cancel(AudioOutput &ao) noexcept;
200

201
bool
202
ao_plugin_pause(AudioOutput &ao);
203

204
#endif