2
* Copyright (C) 2003-2013 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
#ifndef MPD_ENCODER_PLUGIN_HXX
21
#define MPD_ENCODER_PLUGIN_HXX
34
const EncoderPlugin &plugin;
37
bool open, pre_tag, tag, end;
40
explicit Encoder(const EncoderPlugin &_plugin)
48
struct EncoderPlugin {
51
Encoder *(*init)(const config_param ¶m,
54
void (*finish)(Encoder *encoder);
56
bool (*open)(Encoder *encoder,
57
AudioFormat &audio_format,
60
void (*close)(Encoder *encoder);
62
bool (*end)(Encoder *encoder, Error &error);
64
bool (*flush)(Encoder *encoder, Error &error);
66
bool (*pre_tag)(Encoder *encoder, Error &error);
68
bool (*tag)(Encoder *encoder, const Tag *tag,
71
bool (*write)(Encoder *encoder,
72
const void *data, size_t length,
75
size_t (*read)(Encoder *encoder, void *dest, size_t length);
77
const char *(*get_mime_type)(Encoder *encoder);
81
* Creates a new encoder object.
83
* @param plugin the encoder plugin
84
* @param param optional configuration
85
* @param error location to store the error occurring, or nullptr to ignore errors.
86
* @return an encoder object on success, nullptr on failure
88
static inline Encoder *
89
encoder_init(const EncoderPlugin &plugin, const config_param ¶m,
92
return plugin.init(param, error_r);
96
* Frees an encoder object.
98
* @param encoder the encoder
101
encoder_finish(Encoder *encoder)
103
assert(!encoder->open);
105
encoder->plugin.finish(encoder);
109
* Opens an encoder object. You must call this prior to using it.
110
* Before you free it, you must call encoder_close(). You may open
111
* and close (reuse) one encoder any number of times.
113
* After this function returns successfully and before the first
114
* encoder_write() call, you should invoke encoder_read() to obtain
117
* @param encoder the encoder
118
* @param audio_format the encoder's input audio format; the plugin
119
* may modify the struct to adapt it to its abilities
120
* @return true on success
123
encoder_open(Encoder *encoder, AudioFormat &audio_format,
126
assert(!encoder->open);
128
bool success = encoder->plugin.open(encoder, audio_format, error);
130
encoder->open = success;
131
encoder->pre_tag = encoder->tag = encoder->end = false;
137
* Closes an encoder object. This disables the encoder, and readies
138
* it for reusal by calling encoder_open() again.
140
* @param encoder the encoder
143
encoder_close(Encoder *encoder)
145
assert(encoder->open);
147
if (encoder->plugin.close != nullptr)
148
encoder->plugin.close(encoder);
151
encoder->open = false;
156
* Ends the stream: flushes the encoder object, generate an
157
* end-of-stream marker (if applicable), make everything which might
158
* currently be buffered available by encoder_read().
160
* After this function has been called, the encoder may not be usable
161
* for more data, and only encoder_read() and encoder_close() can be
164
* @param encoder the encoder
165
* @return true on success
168
encoder_end(Encoder *encoder, Error &error)
170
assert(encoder->open);
171
assert(!encoder->end);
177
/* this method is optional */
178
return encoder->plugin.end != nullptr
179
? encoder->plugin.end(encoder, error)
184
* Flushes an encoder object, make everything which might currently be
185
* buffered available by encoder_read().
187
* @param encoder the encoder
188
* @return true on success
191
encoder_flush(Encoder *encoder, Error &error)
193
assert(encoder->open);
194
assert(!encoder->pre_tag);
195
assert(!encoder->tag);
196
assert(!encoder->end);
198
/* this method is optional */
199
return encoder->plugin.flush != nullptr
200
? encoder->plugin.flush(encoder, error)
205
* Prepare for sending a tag to the encoder. This is used by some
206
* encoders to flush the previous sub-stream, in preparation to begin
209
* @param encoder the encoder
210
* @param tag the tag object
211
* @return true on success
214
encoder_pre_tag(Encoder *encoder, Error &error)
216
assert(encoder->open);
217
assert(!encoder->pre_tag);
218
assert(!encoder->tag);
219
assert(!encoder->end);
221
/* this method is optional */
222
bool success = encoder->plugin.pre_tag != nullptr
223
? encoder->plugin.pre_tag(encoder, error)
227
encoder->pre_tag = success;
233
* Sends a tag to the encoder.
235
* Instructions: call encoder_pre_tag(); then obtain flushed data with
236
* encoder_read(); finally call encoder_tag().
238
* @param encoder the encoder
239
* @param tag the tag object
240
* @return true on success
243
encoder_tag(Encoder *encoder, const Tag *tag, Error &error)
245
assert(encoder->open);
246
assert(!encoder->pre_tag);
247
assert(encoder->tag);
248
assert(!encoder->end);
251
encoder->tag = false;
254
/* this method is optional */
255
return encoder->plugin.tag != nullptr
256
? encoder->plugin.tag(encoder, tag, error)
261
* Writes raw PCM data to the encoder.
263
* @param encoder the encoder
264
* @param data the buffer containing PCM samples
265
* @param length the length of the buffer in bytes
266
* @return true on success
269
encoder_write(Encoder *encoder, const void *data, size_t length,
272
assert(encoder->open);
273
assert(!encoder->pre_tag);
274
assert(!encoder->tag);
275
assert(!encoder->end);
277
return encoder->plugin.write(encoder, data, length, error);
281
* Reads encoded data from the encoder.
283
* Call this repeatedly until no more data is returned.
285
* @param encoder the encoder
286
* @param dest the destination buffer to copy to
287
* @param length the maximum length of the destination buffer
288
* @return the number of bytes written to #dest
291
encoder_read(Encoder *encoder, void *dest, size_t length)
293
assert(encoder->open);
294
assert(!encoder->pre_tag || !encoder->tag);
297
if (encoder->pre_tag) {
298
encoder->pre_tag = false;
303
return encoder->plugin.read(encoder, dest, length);
307
* Get mime type of encoded content.
309
* @param plugin the encoder plugin
310
* @return an constant string, nullptr on failure
312
static inline const char *
313
encoder_get_mime_type(Encoder *encoder)
315
/* this method is optional */
316
return encoder->plugin.get_mime_type != nullptr
317
? encoder->plugin.get_mime_type(encoder)