summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-08-18 01:40:54 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2010-08-18 01:40:54 +0200
commite6ed2da856ec02f3982af4017f20ff873dde0485 (patch)
treed550157c897892ffa3ce0025f9237e2c59dd7906
parent4f153bf7b20a76da5563f86047a8c0e74436644d (diff)
Dynamically select decoder using decoder_find().
-rw-r--r--commands.c11
-rw-r--r--decoder.c28
-rw-r--r--decoder.h4
3 files changed, 39 insertions, 4 deletions
diff --git a/commands.c b/commands.c
index dd32a7c..38d760f 100644
--- a/commands.c
+++ b/commands.c
@@ -197,10 +197,17 @@ static void commands_get_mp3(GSocketConnection *connection, const gchar *cmd) {
g_string_free(string, TRUE);
- extern const struct decoder_plugin decoder_mpg123_decoder;
+ const struct decoder_plugin *decoder = decoder_find(path);
extern const struct encoder_plugin encoder_lame_encoder;
- transcode((GInputStream*)is, &decoder_mpg123_decoder, os, &encoder_lame_encoder);
+ if(decoder == NULL) {
+ g_warning("no suitable decoder found");
+ goto commands_get_mp3_file_unref;
+ } else {
+ g_debug("using decoder %s", decoder->name);
+ }
+
+ transcode((GInputStream*)is, decoder, os, &encoder_lame_encoder);
commands_get_mp3_file_unref:
diff --git a/decoder.c b/decoder.c
index 555757d..2f866c6 100644
--- a/decoder.c
+++ b/decoder.c
@@ -13,3 +13,31 @@ gssize decoder_decode(struct decoder *decoder, const guchar *inbuf,
void decoder_close(struct decoder *decoder) {
decoder->decoder->close(decoder->data);
}
+
+extern const struct decoder_plugin decoder_mpg123_decoder;
+
+static const struct decoder_plugin *decoder_plugins[] = {
+ &decoder_mpg123_decoder,
+ NULL,
+};
+
+const struct decoder_plugin *decoder_find(const gchar *filename) {
+ gchar *ext = g_strrstr(filename, ".");
+ if(ext == NULL) {
+ g_warning("decoder_find: no file extension found");
+ return NULL;
+ }
+ // exclude the dot
+ ext++;
+
+ for(int i = 0; decoder_plugins[i]; i++) {
+ const struct decoder_plugin *plugin = decoder_plugins[i];
+ for(int j = 0; plugin[i].extensions[j]; j++) {
+ if(g_strcmp0(plugin[i].extensions[j], ext) == 0) {
+ return plugin;
+ }
+ }
+ }
+
+ return NULL;
+}
diff --git a/decoder.h b/decoder.h
index 0a29c64..9139a84 100644
--- a/decoder.h
+++ b/decoder.h
@@ -20,11 +20,11 @@ struct decoder {
gpointer data;
};
-extern struct decoder_plugin *decoder_plugins;
-
gboolean decoder_init(struct decoder *decoder);
gssize decoder_decode(struct decoder *decoder, const guchar *inbuf,
gsize inbuf_size, guchar *outbuf, gsize outbuf_size);
void decoder_close(struct decoder *decoder);
+const struct decoder_plugin *decoder_find(const gchar *filename);
+
#endif