From aacefae70dd8c6b21398fa7a60574a24e1287eb0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Fri, 13 May 2016 10:33:29 +0200 Subject: [PATCH] media: uploading - first step MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I469dac8883f2c8e94fa2384612723ff29e6eae41 Signed-off-by: José Bollo --- plugins/media/media-api.c | 149 +++++++++++++++++++++++++++++++++----------- plugins/media/media-rygel.c | 16 ++++- plugins/media/media-rygel.h | 14 +---- 3 files changed, 129 insertions(+), 50 deletions(-) diff --git a/plugins/media/media-api.c b/plugins/media/media-api.c index 460894b1..7b4fa18f 100644 --- a/plugins/media/media-api.c +++ b/plugins/media/media-api.c @@ -16,6 +16,9 @@ */ #define _GNU_SOURCE + +#include + #include #include "media-api.h" @@ -44,7 +47,7 @@ static mediaCtxHandleT* initMediaCtx () { static void init (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); json_object *jresp; /* create a private client context */ @@ -64,9 +67,15 @@ static void init (struct afb_req request) { /* AFB_SESSION_CHECK */ static void list (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); json_object *jresp; + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + jresp = _rygel_list (ctx); if (!jresp) { @@ -79,12 +88,18 @@ static void list (struct afb_req request) { /* AFB_SESSION_CHECK */ static void selecting (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); const char *value = afb_req_value (request, "value"); json_object *jresp; unsigned int index; char index_str[5]; + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + /* no "?value=" parameter : return current index */ if (!value) { snprintf (index_str, sizeof(index_str), "%d", ctx->index); @@ -119,9 +134,15 @@ static void selecting (struct afb_req request) { /* AFB_SESSION_CHECK */ static void play (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); json_object *jresp; + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + if (!_rygel_do (ctx, PLAY, NULL)) { afb_req_fail (request, "failed", "could not play chosen media"); return; @@ -134,9 +155,15 @@ static void play (struct afb_req request) { /* AFB_SESSION_CHECK */ static void stop (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); json_object *jresp; + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + if (!_rygel_do (ctx, STOP, NULL)) { afb_req_fail (request, "failed", "could not stop chosen media"); return; @@ -149,9 +176,15 @@ static void stop (struct afb_req request) { /* AFB_SESSION_CHECK */ static void pausing (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); json_object *jresp; + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + if (!_rygel_do (ctx, PAUSE, NULL)) { afb_req_fail (request, "failed", "could not pause chosen media"); return; @@ -164,10 +197,16 @@ static void pausing (struct afb_req request) { /* AFB_SESSION_CHECK */ static void seek (struct afb_req request) { /* AFB_SESSION_CHECK */ - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); + mediaCtxHandleT *ctx = afb_req_context_get(request); const char *value = afb_req_value (request, "value"); json_object *jresp; + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } + /* no "?value=" parameter : return error */ if (!value) { afb_req_fail (request, "failed", "you must provide a time"); @@ -184,38 +223,78 @@ static void seek (struct afb_req request) { /* AFB_SESSION_CHECK */ afb_req_success (request, jresp, "Media - Sought"); } -#if 0 -static void upload (AFB_request *request, AFB_PostItem *item) { /* AFB_SESSION_CHECK */ +static char *renamed_filename(struct afb_arg argfile) +{ + char *result; + const char *e = strrchr(argfile.path, '/'); + if (e == NULL) + result = strdup(argfile.value); + else { + result = malloc((++e - argfile.path) + strlen(argfile.value) + 1); + if (result != NULL) + strcpy(stpncpy(result, argfile.path, e - argfile.path), argfile.value); + } + return result; +} - mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request); - AFB_PostCtx *postFileCtx; - json_object *jresp; - char *path; +static void on_uploaded(struct afb_req *prequest, int status) +{ + struct afb_req request = afb_req_unstore(prequest); + struct afb_arg argfile = afb_req_get(request, "file-upload"); + char *file = renamed_filename(argfile); + if (file != NULL) + unlink(file); + free(file); + if (status) + afb_req_fail (request, "failed", "expected file not received"); + else + afb_req_success_f (request, NULL, "uploaded file %s", argfile.value); +} - /* item is !NULL until transfer is complete */ - if (item != NULL) - return getPostFile (request, item, "media"); +static void upload (struct afb_req request) { /* AFB_SESSION_CHECK */ - /* target intermediary file path */ - path = getPostPath (request); + mediaCtxHandleT *ctx = afb_req_context_get(request); + struct afb_req *prequest; + struct afb_arg argfile; + char *path; - if (!path) - fprintf (stderr, "Error encoutered during intermediary file transfer\n"); + /* check that context is initialized */ + if (ctx == NULL) { + afb_req_fail (request, "failed", "uninitialized"); + return; + } - else if (!_rygel_upload (ctx, path)) { - request->errcode = MHD_HTTP_EXPECTATION_FAILED; - request->jresp = jsonNewMessage (AFB_FAIL, "Error when uploading file to media server... could not complete"); + /* get the file */ + argfile = afb_req_get(request, "file-upload"); + if (!argfile.value || !argfile.path) { + afb_req_fail (request, "failed", "expected file not received"); + return; } - else { - request->errcode = MHD_HTTP_OK; - request->jresp = jsonNewMessage (AFB_SUCCESS, "upload=%s done", path); + /* rename the file */ + path = renamed_filename(argfile); + if (path == NULL) { + afb_req_fail (request, "failed", "out of memory"); + return; + } + if (rename(argfile.path, path) != 0) { + free(path); + afb_req_fail (request, "failed", "system error"); + return; } - /* finalizes file transfer */ - return getPostFile (request, item, NULL); + /* for asynchronous processing */ + prequest = afb_req_store(request); + if (path == NULL) { + unlink(path); + afb_req_fail (request, "failed", "out of memory"); + } + else if (!_rygel_upload (ctx, path, (void*)on_uploaded, prequest)) { + unlink(path); + afb_req_fail (afb_req_unstore(prequest), "failed", "Error when uploading file to media server... could not complete"); + } + free(path); } -#endif static void ping (struct afb_req request) { /* AFB_SESSION_NONE */ afb_req_success (request, NULL, "Media - Ping succeeded"); @@ -230,19 +309,19 @@ static const struct AFB_restapi pluginApis[]= { {"stop" , AFB_SESSION_CHECK, stop , "Media API - stop" }, {"pause" , AFB_SESSION_CHECK, pausing , "Media API - pause" }, {"seek" , AFB_SESSION_CHECK, seek , "Media API - seek" }, -// {"upload" , AFB_SESSION_CHECK, (AFB_apiCB)upload , "Media API - upload" }, +// {"upload" , AFB_SESSION_CHECK, upload , "Media API - upload" }, {"ping" , AFB_SESSION_NONE, ping , "Media API - ping" }, {NULL} }; static const struct AFB_plugin pluginDesc = { - .type = AFB_PLUGIN_JSON, - .info = "Application Framework Binder - Media plugin", - .prefix = "media", - .apis = pluginApis + .type = AFB_PLUGIN_JSON, + .info = "Application Framework Binder - Media plugin", + .prefix = "media", + .apis = pluginApis }; const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf) { - return &pluginDesc; + return &pluginDesc; } diff --git a/plugins/media/media-rygel.c b/plugins/media/media-rygel.c index 66e2901a..00327795 100644 --- a/plugins/media/media-rygel.c +++ b/plugins/media/media-rygel.c @@ -22,6 +22,18 @@ #include "media-api.h" #include "media-rygel.h" +static void _rygel_device_cb (GUPnPControlPoint *, GUPnPDeviceProxy *, gpointer); +static void _rygel_av_transport_cb (GUPnPControlPoint *, GUPnPDeviceProxy *, gpointer); +static void _rygel_content_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); +static void _rygel_metadata_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); +static void _rygel_select_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); +static void _rygel_upload_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); +static void _rygel_transfer_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); +static void _rygel_do_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); + +static unsigned int client_count = 0; +static struct dev_ctx **dev_ctx = NULL; + /* -------------- MEDIA RYGEL IMPLEMENTATION ---------------- */ /* --- PUBLIC FUNCTIONS --- */ @@ -161,7 +173,7 @@ unsigned char _rygel_select (mediaCtxHandleT *ctx, unsigned int index) { return 1; } -unsigned char _rygel_upload (mediaCtxHandleT *ctx, char *path) { +unsigned char _rygel_upload (mediaCtxHandleT *ctx, const char *path, void (*oncompletion)(void*,int), void *closure) { dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server; char *raw, *upload_id; @@ -176,7 +188,7 @@ unsigned char _rygel_upload (mediaCtxHandleT *ctx, char *path) { /* for now, we always use the same upload container id */ upload_id = _rygel_find_upload_id (dev_ctx_c, raw); - return _rygel_start_uploading (dev_ctx_c, path, upload_id); + return _rygel_start_uploading (dev_ctx_c, strdup(path), upload_id); } unsigned char _rygel_do (mediaCtxHandleT *ctx, State state, char *args) { diff --git a/plugins/media/media-rygel.h b/plugins/media/media-rygel.h index 0cc84011..951c6b89 100644 --- a/plugins/media/media-rygel.h +++ b/plugins/media/media-rygel.h @@ -54,7 +54,7 @@ unsigned char _rygel_init (mediaCtxHandleT *); void _rygel_free (mediaCtxHandleT *); json_object* _rygel_list (mediaCtxHandleT *); unsigned char _rygel_select (mediaCtxHandleT *, unsigned int); -unsigned char _rygel_upload (mediaCtxHandleT *, char *); +unsigned char _rygel_upload (mediaCtxHandleT *ctx, const char *path, void (*oncompletion)(void*,int), void *closure); unsigned char _rygel_do (mediaCtxHandleT *, State, char *); char* _rygel_list_raw (dev_ctx_T *, unsigned int *); @@ -65,16 +65,4 @@ char* _rygel_find_uri_for_metadata (dev_ctx_T *, char *); unsigned char _rygel_start_uploading (dev_ctx_T *, char *, char *); unsigned char _rygel_start_doing (dev_ctx_T *, char *, char *, State, char *); unsigned char _rygel_find_av_transport (dev_ctx_T *); -static void _rygel_device_cb (GUPnPControlPoint *, GUPnPDeviceProxy *, gpointer); -static void _rygel_av_transport_cb (GUPnPControlPoint *, GUPnPDeviceProxy *, gpointer); -static void _rygel_content_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); -static void _rygel_metadata_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); -static void _rygel_select_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); -static void _rygel_upload_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); -static void _rygel_transfer_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); -static void _rygel_do_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer); - -static unsigned int client_count = 0; -static struct dev_ctx **dev_ctx = NULL; - #endif /* MEDIA_RYGEL_H */ -- 2.16.6