00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef MPD_ENCODER_PLUGIN_H
00021 #define MPD_ENCODER_PLUGIN_H
00022
00023 #include <glib.h>
00024
00025 #include <assert.h>
00026 #include <stdbool.h>
00027 #include <stddef.h>
00028
00029 struct encoder_plugin;
00030 struct audio_format;
00031 struct config_param;
00032 struct tag;
00033
00034 struct encoder {
00035 const struct encoder_plugin *plugin;
00036
00037 #ifndef NDEBUG
00038 bool open, pre_tag, tag, end;
00039 #endif
00040 };
00041
00042 struct encoder_plugin {
00043 const char *name;
00044
00045 struct encoder *(*init)(const struct config_param *param,
00046 GError **error);
00047
00048 void (*finish)(struct encoder *encoder);
00049
00050 bool (*open)(struct encoder *encoder,
00051 struct audio_format *audio_format,
00052 GError **error);
00053
00054 void (*close)(struct encoder *encoder);
00055
00056 bool (*end)(struct encoder *encoder, GError **error);
00057
00058 bool (*flush)(struct encoder *encoder, GError **error);
00059
00060 bool (*pre_tag)(struct encoder *encoder, GError **error);
00061
00062 bool (*tag)(struct encoder *encoder, const struct tag *tag,
00063 GError **error);
00064
00065 bool (*write)(struct encoder *encoder,
00066 const void *data, size_t length,
00067 GError **error);
00068
00069 size_t (*read)(struct encoder *encoder, void *dest, size_t length);
00070
00071 const char *(*get_mime_type)(struct encoder *encoder);
00072 };
00073
00078 static inline void
00079 encoder_struct_init(struct encoder *encoder,
00080 const struct encoder_plugin *plugin)
00081 {
00082 encoder->plugin = plugin;
00083
00084 #ifndef NDEBUG
00085 encoder->open = false;
00086 #endif
00087 }
00088
00097 static inline struct encoder *
00098 encoder_init(const struct encoder_plugin *plugin,
00099 const struct config_param *param, GError **error)
00100 {
00101 return plugin->init(param, error);
00102 }
00103
00109 static inline void
00110 encoder_finish(struct encoder *encoder)
00111 {
00112 assert(!encoder->open);
00113
00114 encoder->plugin->finish(encoder);
00115 }
00116
00132 static inline bool
00133 encoder_open(struct encoder *encoder, struct audio_format *audio_format,
00134 GError **error)
00135 {
00136 assert(!encoder->open);
00137
00138 bool success = encoder->plugin->open(encoder, audio_format, error);
00139 #ifndef NDEBUG
00140 encoder->open = success;
00141 encoder->pre_tag = encoder->tag = encoder->end = false;
00142 #endif
00143 return success;
00144 }
00145
00152 static inline void
00153 encoder_close(struct encoder *encoder)
00154 {
00155 assert(encoder->open);
00156
00157 if (encoder->plugin->close != NULL)
00158 encoder->plugin->close(encoder);
00159
00160 #ifndef NDEBUG
00161 encoder->open = false;
00162 #endif
00163 }
00164
00178 static inline bool
00179 encoder_end(struct encoder *encoder, GError **error)
00180 {
00181 assert(encoder->open);
00182 assert(!encoder->end);
00183
00184 #ifndef NDEBUG
00185 encoder->end = true;
00186 #endif
00187
00188
00189 return encoder->plugin->end != NULL
00190 ? encoder->plugin->end(encoder, error)
00191 : true;
00192 }
00193
00202 static inline bool
00203 encoder_flush(struct encoder *encoder, GError **error)
00204 {
00205 assert(encoder->open);
00206 assert(!encoder->pre_tag);
00207 assert(!encoder->tag);
00208 assert(!encoder->end);
00209
00210
00211 return encoder->plugin->flush != NULL
00212 ? encoder->plugin->flush(encoder, error)
00213 : true;
00214 }
00215
00226 static inline bool
00227 encoder_pre_tag(struct encoder *encoder, GError **error)
00228 {
00229 assert(encoder->open);
00230 assert(!encoder->pre_tag);
00231 assert(!encoder->tag);
00232 assert(!encoder->end);
00233
00234
00235 bool success = encoder->plugin->pre_tag != NULL
00236 ? encoder->plugin->pre_tag(encoder, error)
00237 : true;
00238
00239 #ifndef NDEBUG
00240 encoder->pre_tag = success;
00241 #endif
00242 return success;
00243 }
00244
00256 static inline bool
00257 encoder_tag(struct encoder *encoder, const struct tag *tag, GError **error)
00258 {
00259 assert(encoder->open);
00260 assert(!encoder->pre_tag);
00261 assert(encoder->tag);
00262 assert(!encoder->end);
00263
00264 #ifndef NDEBUG
00265 encoder->tag = false;
00266 #endif
00267
00268
00269 return encoder->plugin->tag != NULL
00270 ? encoder->plugin->tag(encoder, tag, error)
00271 : true;
00272 }
00273
00283 static inline bool
00284 encoder_write(struct encoder *encoder, const void *data, size_t length,
00285 GError **error)
00286 {
00287 assert(encoder->open);
00288 assert(!encoder->pre_tag);
00289 assert(!encoder->tag);
00290 assert(!encoder->end);
00291
00292 return encoder->plugin->write(encoder, data, length, error);
00293 }
00294
00305 static inline size_t
00306 encoder_read(struct encoder *encoder, void *dest, size_t length)
00307 {
00308 assert(encoder->open);
00309 assert(!encoder->pre_tag || !encoder->tag);
00310
00311 #ifndef NDEBUG
00312 if (encoder->pre_tag) {
00313 encoder->pre_tag = false;
00314 encoder->tag = true;
00315 }
00316 #endif
00317
00318 return encoder->plugin->read(encoder, dest, length);
00319 }
00320
00327 static inline const char *
00328 encoder_get_mime_type(struct encoder *encoder)
00329 {
00330
00331 return encoder->plugin->get_mime_type != NULL
00332 ? encoder->plugin->get_mime_type(encoder)
00333 : NULL;
00334 }
00335
00336 #endif