2009-02-22 17:11:14 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-02-22 17:11:14 +01:00
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-02-22 17:11:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MPD_ENCODER_PLUGIN_H
|
|
|
|
#define MPD_ENCODER_PLUGIN_H
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2012-04-05 00:05:21 +02:00
|
|
|
#include <assert.h>
|
2009-02-22 17:11:14 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
struct encoder_plugin;
|
|
|
|
struct audio_format;
|
|
|
|
struct config_param;
|
|
|
|
struct tag;
|
|
|
|
|
|
|
|
struct encoder {
|
|
|
|
const struct encoder_plugin *plugin;
|
2012-04-05 00:05:21 +02:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2012-04-05 00:03:38 +02:00
|
|
|
bool open, pre_tag, tag, end;
|
2012-04-05 00:05:21 +02:00
|
|
|
#endif
|
2009-02-22 17:11:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct encoder_plugin {
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
struct encoder *(*init)(const struct config_param *param,
|
|
|
|
GError **error);
|
|
|
|
|
|
|
|
void (*finish)(struct encoder *encoder);
|
|
|
|
|
|
|
|
bool (*open)(struct encoder *encoder,
|
|
|
|
struct audio_format *audio_format,
|
|
|
|
GError **error);
|
|
|
|
|
|
|
|
void (*close)(struct encoder *encoder);
|
|
|
|
|
2012-04-05 00:03:38 +02:00
|
|
|
bool (*end)(struct encoder *encoder, GError **error);
|
|
|
|
|
2009-02-22 17:11:14 +01:00
|
|
|
bool (*flush)(struct encoder *encoder, GError **error);
|
|
|
|
|
2011-07-20 20:54:34 +02:00
|
|
|
bool (*pre_tag)(struct encoder *encoder, GError **error);
|
|
|
|
|
2009-02-22 17:11:14 +01:00
|
|
|
bool (*tag)(struct encoder *encoder, const struct tag *tag,
|
|
|
|
GError **error);
|
|
|
|
|
|
|
|
bool (*write)(struct encoder *encoder,
|
|
|
|
const void *data, size_t length,
|
|
|
|
GError **error);
|
|
|
|
|
|
|
|
size_t (*read)(struct encoder *encoder, void *dest, size_t length);
|
2009-12-03 19:39:34 +01:00
|
|
|
|
|
|
|
const char *(*get_mime_type)(struct encoder *encoder);
|
2009-02-22 17:11:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes an encoder object. This should be used by encoder
|
|
|
|
* plugins to initialize their base class.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
encoder_struct_init(struct encoder *encoder,
|
|
|
|
const struct encoder_plugin *plugin)
|
|
|
|
{
|
|
|
|
encoder->plugin = plugin;
|
2012-04-05 00:05:21 +02:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
encoder->open = false;
|
|
|
|
#endif
|
2009-02-22 17:11:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new encoder object.
|
|
|
|
*
|
|
|
|
* @param plugin the encoder plugin
|
|
|
|
* @param param optional configuration
|
2011-03-31 21:43:53 +02:00
|
|
|
* @param error location to store the error occurring, or NULL to ignore errors.
|
2009-02-22 17:11:14 +01:00
|
|
|
* @return an encoder object on success, NULL on failure
|
|
|
|
*/
|
|
|
|
static inline struct encoder *
|
|
|
|
encoder_init(const struct encoder_plugin *plugin,
|
|
|
|
const struct config_param *param, GError **error)
|
|
|
|
{
|
|
|
|
return plugin->init(param, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frees an encoder object.
|
|
|
|
*
|
|
|
|
* @param encoder the encoder
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
encoder_finish(struct encoder *encoder)
|
|
|
|
{
|
2012-04-05 00:05:21 +02:00
|
|
|
assert(!encoder->open);
|
|
|
|
|
2009-02-22 17:11:14 +01:00
|
|
|
encoder->plugin->finish(encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @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
|
2011-03-31 21:43:53 +02:00
|
|
|
* @param error location to store the error occurring, or NULL to ignore errors.
|
2009-02-22 17:11:14 +01:00
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
encoder_open(struct encoder *encoder, struct audio_format *audio_format,
|
|
|
|
GError **error)
|
|
|
|
{
|
2012-04-05 00:05:21 +02:00
|
|
|
assert(!encoder->open);
|
|
|
|
|
|
|
|
bool success = encoder->plugin->open(encoder, audio_format, error);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
encoder->open = success;
|
2012-04-05 00:03:38 +02:00
|
|
|
encoder->pre_tag = encoder->tag = encoder->end = false;
|
2012-04-05 00:05:21 +02:00
|
|
|
#endif
|
|
|
|
return success;
|
2009-02-22 17:11:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
encoder_close(struct encoder *encoder)
|
|
|
|
{
|
2012-04-05 00:05:21 +02:00
|
|
|
assert(encoder->open);
|
|
|
|
|
2009-02-22 17:11:14 +01:00
|
|
|
if (encoder->plugin->close != NULL)
|
|
|
|
encoder->plugin->close(encoder);
|
2012-04-05 00:05:21 +02:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
encoder->open = false;
|
|
|
|
#endif
|
2009-02-22 17:11:14 +01:00
|
|
|
}
|
|
|
|
|
2012-04-05 00:03:38 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @param error location to store the error occuring, or NULL to ignore errors.
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
encoder_end(struct encoder *encoder, GError **error)
|
|
|
|
{
|
|
|
|
assert(encoder->open);
|
|
|
|
assert(!encoder->end);
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
encoder->end = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* this method is optional */
|
|
|
|
return encoder->plugin->end != NULL
|
|
|
|
? encoder->plugin->end(encoder, error)
|
|
|
|
: true;
|
2009-02-22 17:11:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flushes an encoder object, make everything which might currently be
|
|
|
|
* buffered available by encoder_read().
|
|
|
|
*
|
|
|
|
* @param encoder the encoder
|
2011-03-31 21:43:53 +02:00
|
|
|
* @param error location to store the error occurring, or NULL to ignore errors.
|
2009-02-22 17:11:14 +01:00
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
encoder_flush(struct encoder *encoder, GError **error)
|
|
|
|
{
|
2012-04-05 00:05:21 +02:00
|
|
|
assert(encoder->open);
|
|
|
|
assert(!encoder->pre_tag);
|
|
|
|
assert(!encoder->tag);
|
2012-04-05 00:03:38 +02:00
|
|
|
assert(!encoder->end);
|
2012-04-05 00:05:21 +02:00
|
|
|
|
2009-02-22 17:11:14 +01:00
|
|
|
/* this method is optional */
|
|
|
|
return encoder->plugin->flush != NULL
|
|
|
|
? encoder->plugin->flush(encoder, error)
|
|
|
|
: true;
|
|
|
|
}
|
|
|
|
|
2011-07-20 20:54:34 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @param error location to store the error occuring, or NULL to ignore errors.
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
encoder_pre_tag(struct encoder *encoder, GError **error)
|
|
|
|
{
|
2012-04-05 00:05:21 +02:00
|
|
|
assert(encoder->open);
|
|
|
|
assert(!encoder->pre_tag);
|
|
|
|
assert(!encoder->tag);
|
2012-04-05 00:03:38 +02:00
|
|
|
assert(!encoder->end);
|
2012-04-05 00:05:21 +02:00
|
|
|
|
2011-07-20 20:54:34 +02:00
|
|
|
/* this method is optional */
|
2012-04-05 00:05:21 +02:00
|
|
|
bool success = encoder->plugin->pre_tag != NULL
|
2011-07-20 20:54:34 +02:00
|
|
|
? encoder->plugin->pre_tag(encoder, error)
|
|
|
|
: true;
|
2012-04-05 00:05:21 +02:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
encoder->pre_tag = success;
|
|
|
|
#endif
|
|
|
|
return success;
|
2011-07-20 20:54:34 +02:00
|
|
|
}
|
|
|
|
|
2009-02-22 17:11:14 +01:00
|
|
|
/**
|
|
|
|
* Sends a tag to the encoder.
|
|
|
|
*
|
2011-07-20 20:54:34 +02:00
|
|
|
* Instructions: call encoder_pre_tag(); then obtain flushed data with
|
|
|
|
* encoder_read(); finally call encoder_tag().
|
|
|
|
*
|
2009-02-22 17:11:14 +01:00
|
|
|
* @param encoder the encoder
|
|
|
|
* @param tag the tag object
|
2011-03-31 21:43:53 +02:00
|
|
|
* @param error location to store the error occurring, or NULL to ignore errors.
|
2009-02-22 17:11:14 +01:00
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
encoder_tag(struct encoder *encoder, const struct tag *tag, GError **error)
|
|
|
|
{
|
2012-04-05 00:05:21 +02:00
|
|
|
assert(encoder->open);
|
|
|
|
assert(!encoder->pre_tag);
|
|
|
|
assert(encoder->tag);
|
2012-04-05 00:03:38 +02:00
|
|
|
assert(!encoder->end);
|
2012-04-05 00:05:21 +02:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
encoder->tag = false;
|
|
|
|
#endif
|
|
|
|
|
2009-02-22 17:11:14 +01:00
|
|
|
/* this method is optional */
|
|
|
|
return encoder->plugin->tag != NULL
|
|
|
|
? encoder->plugin->tag(encoder, tag, error)
|
|
|
|
: 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
|
2011-03-31 21:43:53 +02:00
|
|
|
* @param error location to store the error occurring, or NULL to ignore errors.
|
2009-02-22 17:11:14 +01:00
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
encoder_write(struct encoder *encoder, const void *data, size_t length,
|
|
|
|
GError **error)
|
|
|
|
{
|
2012-04-05 00:05:21 +02:00
|
|
|
assert(encoder->open);
|
|
|
|
assert(!encoder->pre_tag);
|
|
|
|
assert(!encoder->tag);
|
2012-04-05 00:03:38 +02:00
|
|
|
assert(!encoder->end);
|
2012-04-05 00:05:21 +02:00
|
|
|
|
2009-02-22 17:11:14 +01:00
|
|
|
return encoder->plugin->write(encoder, data, length, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads encoded data from the encoder.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
encoder_read(struct encoder *encoder, void *dest, size_t length)
|
|
|
|
{
|
2012-04-05 00:05:21 +02:00
|
|
|
assert(encoder->open);
|
|
|
|
assert(!encoder->pre_tag || !encoder->tag);
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (encoder->pre_tag) {
|
|
|
|
encoder->pre_tag = false;
|
|
|
|
encoder->tag = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-22 17:11:14 +01:00
|
|
|
return encoder->plugin->read(encoder, dest, length);
|
|
|
|
}
|
|
|
|
|
2009-12-03 19:39:34 +01:00
|
|
|
/**
|
|
|
|
* Get mime type of encoded content.
|
|
|
|
*
|
|
|
|
* @param plugin the encoder plugin
|
|
|
|
* @return an constant string, NULL on failure
|
|
|
|
*/
|
|
|
|
static inline const char *
|
|
|
|
encoder_get_mime_type(struct encoder *encoder)
|
|
|
|
{
|
|
|
|
/* this method is optional */
|
|
|
|
return encoder->plugin->get_mime_type != NULL
|
|
|
|
? encoder->plugin->get_mime_type(encoder)
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-22 17:11:14 +01:00
|
|
|
#endif
|