EncoderPlugin.hxx 7.41 KB
Newer Older
Max Kellermann's avatar
Max Kellermann committed
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
Max Kellermann's avatar
Max Kellermann committed
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.
Max Kellermann's avatar
Max Kellermann committed
18 19
 */

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

23
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
24 25 26
#include <stdbool.h>
#include <stddef.h>

27
struct EncoderPlugin;
28
struct AudioFormat;
Max Kellermann's avatar
Max Kellermann committed
29
struct config_param;
Max Kellermann's avatar
Max Kellermann committed
30
struct Tag;
31
class Error;
Max Kellermann's avatar
Max Kellermann committed
32

33 34
struct Encoder {
	const EncoderPlugin &plugin;
35 36

#ifndef NDEBUG
37
	bool open, pre_tag, tag, end;
38
#endif
39 40 41 42 43 44 45

	explicit Encoder(const EncoderPlugin &_plugin)
		:plugin(_plugin)
#ifndef NDEBUG
		, open(false)
#endif
	{}
Max Kellermann's avatar
Max Kellermann committed
46 47
};

48
struct EncoderPlugin {
Max Kellermann's avatar
Max Kellermann committed
49 50
	const char *name;

51
	Encoder *(*init)(const config_param &param,
52
			 Error &error);
Max Kellermann's avatar
Max Kellermann committed
53

54
	void (*finish)(Encoder *encoder);
Max Kellermann's avatar
Max Kellermann committed
55

56
	bool (*open)(Encoder *encoder,
57
		     AudioFormat &audio_format,
58
		     Error &error);
Max Kellermann's avatar
Max Kellermann committed
59

60
	void (*close)(Encoder *encoder);
Max Kellermann's avatar
Max Kellermann committed
61

62
	bool (*end)(Encoder *encoder, Error &error);
63

64
	bool (*flush)(Encoder *encoder, Error &error);
Max Kellermann's avatar
Max Kellermann committed
65

66
	bool (*pre_tag)(Encoder *encoder, Error &error);
67

Max Kellermann's avatar
Max Kellermann committed
68
	bool (*tag)(Encoder *encoder, const Tag *tag,
69
		    Error &error);
Max Kellermann's avatar
Max Kellermann committed
70

71
	bool (*write)(Encoder *encoder,
Max Kellermann's avatar
Max Kellermann committed
72
		      const void *data, size_t length,
73
		      Error &error);
Max Kellermann's avatar
Max Kellermann committed
74

75
	size_t (*read)(Encoder *encoder, void *dest, size_t length);
76

77
	const char *(*get_mime_type)(Encoder *encoder);
Max Kellermann's avatar
Max Kellermann committed
78 79 80 81 82 83 84
};

/**
 * Creates a new encoder object.
 *
 * @param plugin the encoder plugin
 * @param param optional configuration
85 86
 * @param error location to store the error occurring, or nullptr to ignore errors.
 * @return an encoder object on success, nullptr on failure
Max Kellermann's avatar
Max Kellermann committed
87
 */
88
static inline Encoder *
89
encoder_init(const EncoderPlugin &plugin, const config_param &param,
90
	     Error &error_r)
Max Kellermann's avatar
Max Kellermann committed
91
{
92
	return plugin.init(param, error_r);
Max Kellermann's avatar
Max Kellermann committed
93 94 95 96 97 98 99 100
}

/**
 * Frees an encoder object.
 *
 * @param encoder the encoder
 */
static inline void
101
encoder_finish(Encoder *encoder)
Max Kellermann's avatar
Max Kellermann committed
102
{
103 104
	assert(!encoder->open);

105
	encoder->plugin.finish(encoder);
Max Kellermann's avatar
Max Kellermann committed
106 107 108 109 110 111 112
}

/**
 * Opens an encoder object.  You must call this prior to using it.
 * Before you free it, you must call encoder_close().  You may open
 * and close (reuse) one encoder any number of times.
 *
113 114 115 116
 * After this function returns successfully and before the first
 * encoder_write() call, you should invoke encoder_read() to obtain
 * the file header.
 *
Max Kellermann's avatar
Max Kellermann committed
117 118 119 120 121 122
 * @param encoder the encoder
 * @param audio_format the encoder's input audio format; the plugin
 * may modify the struct to adapt it to its abilities
 * @return true on success
 */
static inline bool
123
encoder_open(Encoder *encoder, AudioFormat &audio_format,
124
	     Error &error)
Max Kellermann's avatar
Max Kellermann committed
125
{
126 127
	assert(!encoder->open);

128
	bool success = encoder->plugin.open(encoder, audio_format, error);
129 130
#ifndef NDEBUG
	encoder->open = success;
131
	encoder->pre_tag = encoder->tag = encoder->end = false;
132 133
#endif
	return success;
Max Kellermann's avatar
Max Kellermann committed
134 135 136 137 138 139 140 141 142
}

/**
 * Closes an encoder object.  This disables the encoder, and readies
 * it for reusal by calling encoder_open() again.
 *
 * @param encoder the encoder
 */
static inline void
143
encoder_close(Encoder *encoder)
Max Kellermann's avatar
Max Kellermann committed
144
{
145 146
	assert(encoder->open);

147
	if (encoder->plugin.close != nullptr)
148
		encoder->plugin.close(encoder);
149 150 151 152

#ifndef NDEBUG
	encoder->open = false;
#endif
Max Kellermann's avatar
Max Kellermann committed
153 154
}

155 156 157 158 159 160 161 162 163 164 165 166 167
/**
 * Ends the stream: flushes the encoder object, generate an
 * end-of-stream marker (if applicable), make everything which might
 * currently be buffered available by encoder_read().
 *
 * After this function has been called, the encoder may not be usable
 * for more data, and only encoder_read() and encoder_close() can be
 * called.
 *
 * @param encoder the encoder
 * @return true on success
 */
static inline bool
168
encoder_end(Encoder *encoder, Error &error)
169 170 171 172 173 174 175 176 177
{
	assert(encoder->open);
	assert(!encoder->end);

#ifndef NDEBUG
	encoder->end = true;
#endif

	/* this method is optional */
178
	return encoder->plugin.end != nullptr
179
		? encoder->plugin.end(encoder, error)
180
		: true;
Max Kellermann's avatar
Max Kellermann committed
181 182 183 184 185 186 187 188 189 190
}

/**
 * Flushes an encoder object, make everything which might currently be
 * buffered available by encoder_read().
 *
 * @param encoder the encoder
 * @return true on success
 */
static inline bool
191
encoder_flush(Encoder *encoder, Error &error)
Max Kellermann's avatar
Max Kellermann committed
192
{
193 194 195
	assert(encoder->open);
	assert(!encoder->pre_tag);
	assert(!encoder->tag);
196
	assert(!encoder->end);
197

Max Kellermann's avatar
Max Kellermann committed
198
	/* this method is optional */
199
	return encoder->plugin.flush != nullptr
200
		? encoder->plugin.flush(encoder, error)
Max Kellermann's avatar
Max Kellermann committed
201 202 203
		: true;
}

204 205 206 207 208 209 210 211 212 213
/**
 * Prepare for sending a tag to the encoder.  This is used by some
 * encoders to flush the previous sub-stream, in preparation to begin
 * a new one.
 *
 * @param encoder the encoder
 * @param tag the tag object
 * @return true on success
 */
static inline bool
214
encoder_pre_tag(Encoder *encoder, Error &error)
215
{
216 217 218
	assert(encoder->open);
	assert(!encoder->pre_tag);
	assert(!encoder->tag);
219
	assert(!encoder->end);
220

221
	/* this method is optional */
222
	bool success = encoder->plugin.pre_tag != nullptr
223
		? encoder->plugin.pre_tag(encoder, error)
224
		: true;
225 226 227 228 229

#ifndef NDEBUG
	encoder->pre_tag = success;
#endif
	return success;
230 231
}

Max Kellermann's avatar
Max Kellermann committed
232 233 234
/**
 * Sends a tag to the encoder.
 *
235 236 237
 * Instructions: call encoder_pre_tag(); then obtain flushed data with
 * encoder_read(); finally call encoder_tag().
 *
Max Kellermann's avatar
Max Kellermann committed
238 239 240 241 242
 * @param encoder the encoder
 * @param tag the tag object
 * @return true on success
 */
static inline bool
243
encoder_tag(Encoder *encoder, const Tag *tag, Error &error)
Max Kellermann's avatar
Max Kellermann committed
244
{
245 246 247
	assert(encoder->open);
	assert(!encoder->pre_tag);
	assert(encoder->tag);
248
	assert(!encoder->end);
249 250 251 252 253

#ifndef NDEBUG
	encoder->tag = false;
#endif

Max Kellermann's avatar
Max Kellermann committed
254
	/* this method is optional */
255
	return encoder->plugin.tag != nullptr
256
		? encoder->plugin.tag(encoder, tag, error)
Max Kellermann's avatar
Max Kellermann committed
257 258 259 260 261 262 263 264 265 266 267 268
		: true;
}

/**
 * Writes raw PCM data to the encoder.
 *
 * @param encoder the encoder
 * @param data the buffer containing PCM samples
 * @param length the length of the buffer in bytes
 * @return true on success
 */
static inline bool
269
encoder_write(Encoder *encoder, const void *data, size_t length,
270
	      Error &error)
Max Kellermann's avatar
Max Kellermann committed
271
{
272 273 274
	assert(encoder->open);
	assert(!encoder->pre_tag);
	assert(!encoder->tag);
275
	assert(!encoder->end);
276

277
	return encoder->plugin.write(encoder, data, length, error);
Max Kellermann's avatar
Max Kellermann committed
278 279 280 281 282
}

/**
 * Reads encoded data from the encoder.
 *
283 284
 * Call this repeatedly until no more data is returned.
 *
Max Kellermann's avatar
Max Kellermann committed
285 286 287 288 289 290
 * @param encoder the encoder
 * @param dest the destination buffer to copy to
 * @param length the maximum length of the destination buffer
 * @return the number of bytes written to #dest
 */
static inline size_t
291
encoder_read(Encoder *encoder, void *dest, size_t length)
Max Kellermann's avatar
Max Kellermann committed
292
{
293 294 295 296 297 298 299 300 301 302
	assert(encoder->open);
	assert(!encoder->pre_tag || !encoder->tag);

#ifndef NDEBUG
	if (encoder->pre_tag) {
		encoder->pre_tag = false;
		encoder->tag = true;
	}
#endif

303
	return encoder->plugin.read(encoder, dest, length);
Max Kellermann's avatar
Max Kellermann committed
304 305
}

306 307 308 309
/**
 * Get mime type of encoded content.
 *
 * @param plugin the encoder plugin
310
 * @return an constant string, nullptr on failure
311 312
 */
static inline const char *
313
encoder_get_mime_type(Encoder *encoder)
314 315
{
	/* this method is optional */
316
	return encoder->plugin.get_mime_type != nullptr
317
		? encoder->plugin.get_mime_type(encoder)
318
		: nullptr;
319 320
}

Max Kellermann's avatar
Max Kellermann committed
321
#endif