summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-08-17 18:20:07 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2010-08-17 18:20:07 +0200
commit1cbd3896bab58f2ccfb17c0fe36062e3748724da (patch)
treea407178222fd9b854a20516c067b4d363d4c3e92
parentac5e220d0e17f57f2fb3c4dedde186f238fa89ed (diff)
Added get_mp3 handler.
-rw-r--r--commands.c62
1 files changed, 61 insertions, 1 deletions
diff --git a/commands.c b/commands.c
index 21b0703..8a9efff 100644
--- a/commands.c
+++ b/commands.c
@@ -1,5 +1,8 @@
#include "commands.h"
#include "music.h"
+#include "transcode.h"
+#include "decoder.h"
+#include "encoder.h"
#include <glib.h>
#include <string.h>
@@ -107,7 +110,7 @@ static void commands_get_raw(GSocketConnection *connection, const gchar *cmd) {
GString *string = g_string_new(NULL);
g_string_append(string, "HTTP/1.1 200 OK\r\n");
- g_string_append(string, "content-type: octet-stream\r\n");
+ g_string_append(string, "content-type: application/octet-stream\r\n");
g_string_append_printf(string, "content-length: %lu\r\n", filesize);
g_string_append(string, "\r\n");
@@ -141,12 +144,69 @@ commands_get_raw_free_path:
g_free(path);
}
+static void commands_get_mp3(GSocketConnection *connection, const gchar *cmd) {
+ GError *error = NULL;
+ gchar **data = g_strsplit(cmd, " ", 2);
+ g_assert(data[0] != NULL && data[1] != NULL);
+
+ gchar *path = g_strdup(data[1]);
+ g_strfreev(data);
+
+ struct file *f = music_find_file(path);
+
+ if(f == NULL) {
+ g_warning("couldn't find %s", path);
+ send_404(connection);
+ goto commands_get_mp3_free_path;
+ }
+
+ GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection);
+ GFile *file = g_file_new_for_path(path);
+ GFileInputStream *is = g_file_read(file, NULL, &error);
+
+ if(is == NULL) {
+ g_warning(error->message);
+ g_error_free(error);
+ goto commands_get_mp3_file_unref;
+ }
+
+ GString *string = g_string_new(NULL);
+ g_string_append(string, "HTTP/1.1 200 OK\r\n");
+ g_string_append(string, "content-type: application/octet-stream\r\n");
+ g_string_append(string, "\r\n");
+
+ if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL,
+ &error) == FALSE) {
+ g_warning(error->message);
+ g_error_free(error);
+ g_string_free(string, TRUE);
+ goto commands_get_mp3_file_unref;
+ }
+
+ g_string_free(string, TRUE);
+
+ extern const struct decoder_plugin decoder_mpg123_decoder;
+ extern const struct encoder_plugin encoder_lame_encoder;
+
+ transcode((GInputStream*)is, &decoder_mpg123_decoder, os, &encoder_lame_encoder);
+
+commands_get_mp3_file_unref:
+
+ g_object_unref(is);
+ g_object_unref(file);
+
+commands_get_mp3_free_path:
+ g_free(path);
+}
+
void commands_handle(GSocketConnection *connection, const gchar *cmd) {
g_debug("handling command string %s", cmd);
if(g_ascii_strncasecmp(cmd, "/list", 5) == 0) {
commands_list(connection, cmd);
} else if(g_ascii_strncasecmp(cmd, "/get_raw", 8) == 0) {
commands_get_raw(connection, cmd);
+ } else if(g_ascii_strncasecmp(cmd, "/get_mp3", 8) == 0) {
+ commands_get_mp3(connection, cmd);
} else {
g_warning("no command handlers found");
}