summaryrefslogtreecommitdiffstats
path: root/bindings/media
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2016-06-23 20:34:57 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2016-06-23 20:42:57 +0200
commit7059e59cddc1c81321639875636e88895bc14309 (patch)
tree2e857745ae2dd18814bdfe2d6e3806151a51a43e /bindings/media
parentef908d903929988ad01f9df94415fc9c3ddebcac (diff)
vocabulary: moving from 'plugin' to 'binding'
Change-Id: Ic9e118df2bede1fefbb591f8ae7887266b7324ca Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'bindings/media')
-rw-r--r--bindings/media/CMakeLists.txt18
-rw-r--r--bindings/media/export.map1
-rw-r--r--bindings/media/media-api.c331
-rw-r--r--bindings/media/media-api.h29
-rw-r--r--bindings/media/media-rygel.c721
-rw-r--r--bindings/media/media-rygel.h68
6 files changed, 1168 insertions, 0 deletions
diff --git a/bindings/media/CMakeLists.txt b/bindings/media/CMakeLists.txt
new file mode 100644
index 00000000..2866dfbc
--- /dev/null
+++ b/bindings/media/CMakeLists.txt
@@ -0,0 +1,18 @@
+INCLUDE(FindPkgConfig)
+PKG_CHECK_MODULES(gupnp gupnp-1.0 gupnp-av-1.0 gssdp-1.0 gobject-2.0 gio-2.0)
+
+IF(gupnp_FOUND)
+
+ MESSAGE(STATUS "gupnp found ; will compile Media binding... (binding)")
+
+ INCLUDE_DIRECTORIES( ${include_dirs} ${gupnp_INCLUDE_DIRS})
+ ADD_LIBRARY(media-api MODULE media-api.c media-rygel.c)
+ SET_TARGET_PROPERTIES(media-api PROPERTIES
+ PREFIX ""
+ LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map"
+ )
+ TARGET_LINK_LIBRARIES(media-api ${link_libraries} ${gupnp_LIBRARIES})
+ INSTALL(TARGETS media-api
+ LIBRARY DESTINATION ${binding_install_dir})
+
+ENDIF(gupnp_FOUND)
diff --git a/bindings/media/export.map b/bindings/media/export.map
new file mode 100644
index 00000000..0ef1ac79
--- /dev/null
+++ b/bindings/media/export.map
@@ -0,0 +1 @@
+{ global: afbBindingV1Register; local: *; };
diff --git a/bindings/media/media-api.c b/bindings/media/media-api.c
new file mode 100644
index 00000000..578d0066
--- /dev/null
+++ b/bindings/media/media-api.c
@@ -0,0 +1,331 @@
+/*
+ * Copyright (C) 2016 "IoT.bzh"
+ * Author "Manuel Bachmann"
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+
+#include <string.h>
+
+#include <json-c/json.h>
+
+#include "media-api.h"
+#include "media-rygel.h"
+
+#include <afb/afb-plugin.h>
+#include <afb/afb-req-itf.h>
+
+json_object* _rygel_list (mediaCtxHandleT *);
+
+/* ------ LOCAL HELPER FUNCTIONS --------- */
+
+/* private client context creation ; default values */
+static mediaCtxHandleT* initMediaCtx () {
+
+ mediaCtxHandleT *ctx;
+
+ ctx = malloc (sizeof(mediaCtxHandleT));
+ ctx->media_server = NULL;
+ ctx->index = 0;
+
+ return ctx;
+}
+
+/* ------ PUBLIC PLUGIN FUNCTIONS --------- */
+
+static void init (struct afb_req request) { /* AFB_SESSION_CHECK */
+
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
+ json_object *jresp;
+
+ /* create a private client context */
+ if (!ctx) {
+ ctx = initMediaCtx();
+ afb_req_context_set (request, ctx, free);
+ }
+
+ /* initialize server connection */
+ if (!ctx->media_server)
+ _rygel_init (ctx);
+
+ jresp = json_object_new_object ();
+ json_object_object_add (jresp, "init", json_object_new_string ("success"));
+ afb_req_success (request, jresp, "Media - Initialized");
+}
+
+static void list (struct afb_req request) { /* AFB_SESSION_CHECK */
+
+ 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) {
+ afb_req_fail (request, "failed", "no content found in media server");
+ return;
+ }
+
+ afb_req_success (request, jresp, "Media - Listed");
+}
+
+static void selecting (struct afb_req request) { /* AFB_SESSION_CHECK */
+
+ 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);
+ jresp = json_object_new_object();
+ json_object_object_add (jresp, "index", json_object_new_string (index_str));
+ }
+
+ /* "?value=" parameter is negative */
+ else if (atoi(value) < 0) {
+ afb_req_fail (request, "failed", "chosen index cannot be negative");
+ return;
+ }
+
+ /* "?value=" parameter is positive */
+ else if (atoi(value) >= 0) {
+ index = (unsigned int) atoi(value);
+
+ if (!_rygel_select (ctx, index)) {
+ afb_req_fail (request, "failed", "chosen index superior to current media count");
+ return;
+ }
+
+ ctx->index = index;
+ jresp = json_object_new_object();
+ json_object_object_add (jresp, "index", json_object_new_string (value));
+ }
+ else
+ jresp = NULL;
+
+ afb_req_success (request, jresp, "Media - Listed");
+}
+
+static void play (struct afb_req request) { /* AFB_SESSION_CHECK */
+
+ 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;
+ }
+
+ jresp = json_object_new_object ();
+ json_object_object_add (jresp, "play", json_object_new_string ("success"));
+ afb_req_success (request, jresp, "Media - Listed");
+}
+
+static void stop (struct afb_req request) { /* AFB_SESSION_CHECK */
+
+ 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;
+ }
+
+ jresp = json_object_new_object ();
+ json_object_object_add (jresp, "stop", json_object_new_string ("success"));
+ afb_req_success (request, jresp, "Media - Stopped");
+}
+
+static void pausing (struct afb_req request) { /* AFB_SESSION_CHECK */
+
+ 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;
+ }
+
+ jresp = json_object_new_object();
+ json_object_object_add (jresp, "pause", json_object_new_string ("success"));
+ afb_req_success (request, jresp, "Media - Paused");
+}
+
+static void seek (struct afb_req request) { /* AFB_SESSION_CHECK */
+
+ 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");
+ return;
+ }
+
+ if (!_rygel_do (ctx, SEEK, (char *)value)) {
+ afb_req_fail (request, "failed", "could not seek chosen media");
+ return;
+ }
+
+ jresp = json_object_new_object();
+ json_object_object_add (jresp, "seek", json_object_new_string ("success"));
+ afb_req_success (request, jresp, "Media - Sought");
+}
+
+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;
+}
+
+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);
+ afb_req_unref(request);
+}
+
+static void upload (struct afb_req request) { /* AFB_SESSION_CHECK */
+
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
+ struct afb_req *prequest;
+ struct afb_arg argfile;
+ char *path;
+
+ /* check that context is initialized */
+ if (ctx == NULL) {
+ afb_req_fail (request, "failed", "uninitialized");
+ return;
+ }
+
+ /* 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;
+ }
+
+ /* 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;
+ }
+
+ /* 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)) {
+ afb_req_unref(afb_req_unstore(prequest));
+ unlink(path);
+ afb_req_fail (request, "failed", "Error when uploading file to media server... could not complete");
+ }
+ free(path);
+}
+
+static void ping (struct afb_req request) { /* AFB_SESSION_NONE */
+ afb_req_success (request, NULL, "Media - Ping succeeded");
+}
+
+
+static const struct AFB_verb_desc_v1 verbs[]= {
+ {"init" , AFB_SESSION_CHECK, init , "Media API - init" },
+ {"list" , AFB_SESSION_CHECK, list , "Media API - list" },
+ {"select" , AFB_SESSION_CHECK, selecting , "Media API - select" },
+ {"play" , AFB_SESSION_CHECK, play , "Media API - play" },
+ {"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, upload , "Media API - upload" },
+ {"ping" , AFB_SESSION_NONE, ping , "Media API - ping" },
+ {NULL}
+};
+
+static const struct AFB_plugin pluginDesc = {
+ .type = AFB_PLUGIN_VERSION_1,
+ .v1 = {
+ .info = "Application Framework Binder - Media plugin",
+ .prefix = "media",
+ .verbs = verbs
+ }
+};
+
+const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf)
+{
+ return &pluginDesc;
+}
diff --git a/bindings/media/media-api.h b/bindings/media/media-api.h
new file mode 100644
index 00000000..c1d764c2
--- /dev/null
+++ b/bindings/media/media-api.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2016 "IoT.bzh"
+ * Author "Manuel Bachmann"
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef MEDIA_API_H
+#define MEDIA_API_H
+
+/* -------------- PLUGIN DEFINITIONS ----------------- */
+
+/* private client context [will be destroyed when client leaves] */
+typedef struct {
+ void *media_server; /* handle to implementation (Rygel...) */
+ unsigned int index; /* currently selected media file */
+} mediaCtxHandleT;
+
+#endif /* MEDIA_API_H */
diff --git a/bindings/media/media-rygel.c b/bindings/media/media-rygel.c
new file mode 100644
index 00000000..00327795
--- /dev/null
+++ b/bindings/media/media-rygel.c
@@ -0,0 +1,721 @@
+/*
+ * Copyright (C) 2016 "IoT.bzh"
+ * Author "Manuel Bachmann"
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <string.h>
+
+#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 --- */
+
+unsigned char _rygel_init (mediaCtxHandleT *ctx) {
+
+ GMainContext *loop;
+ GUPnPContext *context;
+ GUPnPControlPoint *control_point;
+ gint handler_cb;
+ struct timeval tv_start, tv_now;
+
+ context = gupnp_context_new (NULL, NULL, 0, NULL);
+
+ control_point = gupnp_control_point_new (context, URN_MEDIA_SERVER);
+
+ handler_cb = g_signal_connect (control_point, "device-proxy-available",
+ G_CALLBACK (_rygel_device_cb), ctx);
+
+ /* start searching for servers */
+ gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (control_point), TRUE);
+
+ loop = g_main_context_default ();
+
+ /* 5 seconds should be sufficient to find Rygel */
+ gettimeofday (&tv_start, NULL);
+ gettimeofday (&tv_now, NULL);
+ while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
+
+ g_main_context_iteration (loop, FALSE);
+
+ if (ctx->media_server)
+ break;
+ gettimeofday (&tv_now, NULL);
+ }
+ /* fail if we found no server */
+ if (!ctx->media_server)
+ return 0;
+
+ /* we have found the server ; stop looking for it... */
+ g_signal_handler_disconnect (control_point, handler_cb);
+
+ dev_ctx[client_count]->loop = loop;
+ dev_ctx[client_count]->context = context;
+ dev_ctx[client_count]->av_transport = NULL;
+ dev_ctx[client_count]->state = STOP;
+ dev_ctx[client_count]->target_state = STOP;
+ dev_ctx[client_count]->action_args = NULL;
+ dev_ctx[client_count]->transfer_started = 0;
+
+ client_count++;
+
+ return 1;
+}
+
+void _rygel_free (mediaCtxHandleT *ctx) {
+
+ dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server;
+
+ client_count--;
+
+ g_main_context_unref (dev_ctx_c->loop);
+ dev_ctx_c->loop = NULL;
+ dev_ctx_c->context = NULL;
+ dev_ctx_c->device_info = NULL;
+ dev_ctx_c->av_transport = NULL;
+ dev_ctx_c->content_dir = NULL;
+ dev_ctx_c->content_res = NULL;
+}
+
+json_object* _rygel_list (mediaCtxHandleT *ctx) {
+
+ dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server;
+ json_object *json_o, *json_a;
+ char *raw, *start, *end, *id, *title;
+ int length, i = 0;
+
+ if (!dev_ctx_c)
+ return NULL;
+
+ raw = _rygel_list_raw (dev_ctx_c, NULL);
+ if (!raw)
+ return NULL;
+
+ start = strstr (raw, "<dc:title>");
+ if (!start)
+ return NULL;
+
+ json_o = json_object_new_object ();
+ json_a = json_object_new_array ();
+ while (start) {
+ json_object *json_i, *json_id, *json_title;
+
+ start = strstr (start, "<dc:title>");
+ if (!start) break;
+ end = strstr (start, "</dc:title>");
+ start += 10;
+ length = end - start;
+
+ asprintf (&id, "%02d", i);
+
+ title = (char*) malloc (length+1);
+ strncpy (title, start, length);
+ title[length] = '\0';
+
+ json_i = json_object_new_object ();
+ json_id = json_object_new_string (id);
+ json_title = json_object_new_string (title);
+ json_object_object_add (json_i, "id", json_id);
+ json_object_object_add (json_i, "title", json_title);
+ json_object_array_add (json_a, json_i);
+
+ free (id); free (title);
+ i++;
+ }
+
+ json_object_object_add (json_o, "list", json_a);
+
+ return json_o;
+}
+
+unsigned char _rygel_select (mediaCtxHandleT *ctx, unsigned int index) {
+
+ dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server;
+ unsigned int count;
+
+ if (!dev_ctx_c)
+ return 0;
+
+ if (!_rygel_list_raw (dev_ctx_c, &count) ||
+ index >= count)
+ return 0;
+
+ if (ctx->index != index)
+ dev_ctx_c->state = STOP;
+
+ return 1;
+}
+
+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;
+
+ if (!dev_ctx_c)
+ return 0;
+
+ raw = _rygel_list_raw (dev_ctx_c, NULL);
+ if (!raw)
+ return 0;
+
+ /* 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, strdup(path), upload_id);
+}
+
+unsigned char _rygel_do (mediaCtxHandleT *ctx, State state, char *args) {
+
+ dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server;
+ unsigned int index = ctx->index;
+ unsigned int count;
+ char *raw, *id, *metadata, *uri;
+
+ if (!dev_ctx_c || dev_ctx_c->state == state)
+ return 0;
+
+ raw = _rygel_list_raw (dev_ctx_c, &count);
+ if (!raw || index >= count)
+ return 0;
+
+ id = _rygel_find_id_for_index (dev_ctx_c, raw, index);
+ metadata = _rygel_find_metadata_for_id (dev_ctx_c, id);
+ uri = _rygel_find_uri_for_metadata (dev_ctx_c, metadata);
+
+ return _rygel_start_doing (dev_ctx_c, uri, metadata, state, args);
+}
+
+/* --- LOCAL HELPER FUNCTIONS --- */
+
+char* _rygel_list_raw (dev_ctx_T* dev_ctx_c, unsigned int *count) {
+
+ GUPnPServiceProxy *content_dir_proxy;
+ struct timeval tv_start, tv_now;
+
+ dev_ctx_c->content_res = NULL;
+ dev_ctx_c->content_num = 0;
+ content_dir_proxy = GUPNP_SERVICE_PROXY (dev_ctx_c->content_dir);
+
+ gupnp_service_proxy_begin_action (content_dir_proxy, "Browse", _rygel_content_cb, dev_ctx_c,
+ "ObjectID", G_TYPE_STRING, "Filesystem",
+ "BrowseFlag", G_TYPE_STRING, "BrowseDirectChildren",
+ "Filter", G_TYPE_STRING, "@childCount",
+ "StartingIndex", G_TYPE_UINT, 0,
+ "RequestedCount", G_TYPE_UINT, 64,
+ "SortCriteria", G_TYPE_STRING, "",
+ NULL);
+
+ gettimeofday (&tv_start, NULL);
+ gettimeofday (&tv_now, NULL);
+ while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
+
+ g_main_context_iteration (dev_ctx_c->loop, FALSE);
+
+ if (dev_ctx_c->content_res)
+ break;
+ gettimeofday (&tv_now, NULL);
+ }
+
+ if (count) *count = dev_ctx_c->content_num;
+ return dev_ctx_c->content_res;
+}
+
+char* _rygel_find_upload_id (dev_ctx_T* dev_ctx_c, char *raw) {
+
+ char *found;
+ char id[33];
+
+ found = strstr (raw, "parentID=\"");
+ found += 10;
+
+ /* IDs are 32-bit strings */
+ strncpy (id, found, 32);
+ id[32] = '\0';
+
+ return strdup (id);
+}
+
+char* _rygel_find_id_for_index (dev_ctx_T* dev_ctx_c, char *raw, unsigned int index) {
+
+ char *found = raw;
+ char id[33];
+ int i;
+
+ for (i = 0; i <= index; i++) {
+ found = strstr (found, "item id=");
+ found += 9;
+
+ if (i == index) {
+ /* IDs are 32-bit strings */
+ strncpy (id, found, 32);
+ id[32] = '\0';
+ }
+ }
+
+ return strdup (id);
+}
+
+char* _rygel_find_metadata_for_id (dev_ctx_T* dev_ctx_c, char *id) {
+
+ GUPnPServiceProxy *content_dir_proxy;
+ struct timeval tv_start, tv_now;
+
+ dev_ctx_c->content_res = NULL;
+
+ content_dir_proxy = GUPNP_SERVICE_PROXY (dev_ctx_c->content_dir);
+
+ gupnp_service_proxy_begin_action (content_dir_proxy, "Browse", _rygel_metadata_cb, dev_ctx_c,
+ "ObjectID", G_TYPE_STRING, id,
+ "BrowseFlag", G_TYPE_STRING, "BrowseMetadata",
+ "Filter", G_TYPE_STRING, "*",
+ "StartingIndex", G_TYPE_UINT, 0,
+ "RequestedCount", G_TYPE_UINT, 0,
+ "SortCriteria", G_TYPE_STRING, "",
+ NULL);
+
+ gettimeofday (&tv_start, NULL);
+ gettimeofday (&tv_now, NULL);
+ while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
+
+ g_main_context_iteration (dev_ctx_c->loop, FALSE);
+
+ if (dev_ctx_c->content_res)
+ break;
+ gettimeofday (&tv_now, NULL);
+ }
+
+ return dev_ctx_c->content_res;
+}
+
+char* _rygel_find_uri_for_metadata (dev_ctx_T* dev_ctx_c, char *metadata) {
+
+ char *start, *end, *uri = NULL;
+ int length;
+
+ /* position ourselves after the first "<res " tag */
+ start = strstr (metadata, "<res ");
+
+ while (start) {
+ start = strstr (start, "http://");
+ if (!start) break;
+ end = strstr (start, "</res>");
+ length = end - start;
+
+
+ uri = (char *)malloc (length + 1);
+ strncpy (uri, start, length);
+ uri[length] = '\0';
+ /* if the URI contains "primary_http", it is the main one ; stop here...*/
+ if (strstr (uri, "primary_http"))
+ break;
+
+ free (uri); start = end;
+ }
+
+ return uri;
+}
+
+char * _rygel_time_for_string (char *string) {
+
+ int total_seconds;
+ unsigned int hours, minutes, seconds;
+ char *time;
+
+ total_seconds = atoi (string);
+ hours = total_seconds / 3600;
+ minutes = (total_seconds / 60) - (hours * 60);
+ seconds = total_seconds - (hours * 3600) - (minutes * 60);
+
+ asprintf (&time, "%u:%02u:%02u", hours, minutes, seconds);
+
+ return time;
+}
+
+unsigned char _rygel_start_uploading (dev_ctx_T* dev_ctx_c, char *path, char *upload_id) {
+
+ GUPnPServiceProxy *content_dir_proxy;
+ GUPnPDIDLLiteWriter *didl_writer;
+ GUPnPDIDLLiteObject *didl_object;
+ char *didl, *content_type, *mime_type, *upnp_class;
+ struct timeval tv_start, tv_now;
+
+ didl_writer = gupnp_didl_lite_writer_new (NULL);
+ didl_object = GUPNP_DIDL_LITE_OBJECT (gupnp_didl_lite_writer_add_item (didl_writer));
+
+ /* create the metadata for the file */
+ gupnp_didl_lite_object_set_parent_id (didl_object, upload_id);
+ gupnp_didl_lite_object_set_id (didl_object, "");
+ gupnp_didl_lite_object_set_restricted (didl_object, FALSE);
+ gupnp_didl_lite_object_set_title (didl_object, g_path_get_basename (path));
+ /* deduce the UPnP class from the MIME type ("audio/ogg" e.g.) */
+ content_type = g_content_type_guess (path, NULL, 0, NULL);
+ mime_type = g_content_type_get_mime_type (content_type);
+ if (strstr (mime_type, "audio/"))
+ upnp_class = strdup ("object.item.audioItem.musicTrack");
+ else if (strstr (mime_type, "video/"))
+ upnp_class = strdup ("object.item.videoItem");
+ else if (strstr (mime_type, "image/"))
+ upnp_class = strdup ("object.item.imageItem");
+ else
+ upnp_class = strdup ("object.item");
+ gupnp_didl_lite_object_set_upnp_class (didl_object, upnp_class);
+ didl = gupnp_didl_lite_writer_get_string (didl_writer);
+
+ dev_ctx_c->transfer_path = path;
+ dev_ctx_c->transfer_started = 0;
+ content_dir_proxy = GUPNP_SERVICE_PROXY (dev_ctx_c->content_dir);
+
+ gupnp_service_proxy_begin_action (content_dir_proxy, "CreateObject", _rygel_upload_cb, dev_ctx_c,
+ "ContainerID", G_TYPE_STRING, upload_id,
+ "Elements", G_TYPE_STRING, didl,
+ NULL);
+
+ gettimeofday (&tv_start, NULL);
+ gettimeofday (&tv_now, NULL);
+ while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
+
+ g_main_context_iteration (dev_ctx_c->loop, FALSE);
+
+ if (dev_ctx_c->transfer_started)
+ break;
+ gettimeofday (&tv_now, NULL);
+ }
+ if (!dev_ctx_c->transfer_started)
+ return 0;
+
+ return 1;
+}
+
+unsigned char _rygel_start_doing (dev_ctx_T* dev_ctx_c, char *uri, char *metadata, State state, char *args) {
+
+ GUPnPServiceProxy *av_transport_proxy;
+ struct timeval tv_start, tv_now;
+
+ if (!dev_ctx_c->av_transport) {
+ if (!_rygel_find_av_transport (dev_ctx_c))
+ return 0;
+ }
+ dev_ctx_c->target_state = state;
+ dev_ctx_c->action_args = args;
+ av_transport_proxy = GUPNP_SERVICE_PROXY (dev_ctx_c->av_transport);
+
+ gupnp_service_proxy_begin_action (av_transport_proxy, "SetAVTransportURI", _rygel_select_cb, dev_ctx_c,
+ "InstanceID", G_TYPE_UINT, 0,
+ "CurrentURI", G_TYPE_STRING, uri,
+ "CurrentURIMetaData", G_TYPE_STRING, metadata,
+ NULL);
+
+ gettimeofday (&tv_start, NULL);
+ gettimeofday (&tv_now, NULL);
+ while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
+
+ g_main_context_iteration (dev_ctx_c->loop, FALSE);
+
+ if (dev_ctx_c->state == state)
+ break;
+ gettimeofday (&tv_now, NULL);
+ }
+ if (dev_ctx_c->state != state)
+ return 0;
+
+ return 1;
+}
+
+unsigned char _rygel_find_av_transport (dev_ctx_T* dev_ctx_c) {
+
+ GUPnPControlPoint *control_point;
+ gint handler_cb;
+ struct timeval tv_start, tv_now;
+
+ control_point = gupnp_control_point_new (dev_ctx_c->context, URN_MEDIA_RENDERER);
+
+ handler_cb = g_signal_connect (control_point, "device-proxy-available",
+ G_CALLBACK (_rygel_av_transport_cb), dev_ctx_c);
+
+ gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (control_point), TRUE);
+
+ gettimeofday (&tv_start, NULL);
+ gettimeofday (&tv_now, NULL);
+ while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
+
+ g_main_context_iteration (dev_ctx_c->loop, FALSE);
+
+ if (dev_ctx_c->av_transport)
+ break;
+ gettimeofday (&tv_now, NULL);
+ }
+ g_signal_handler_disconnect (control_point, handler_cb);
+
+ if (!dev_ctx_c->av_transport)
+ return 0;
+
+ return 1;
+}
+
+
+ /* ---- LOCAL CALLBACK FUNCTIONS ---- */
+
+static void _rygel_device_cb (GUPnPControlPoint *point, GUPnPDeviceProxy *proxy,
+ gpointer data) {
+
+ mediaCtxHandleT *ctx = (mediaCtxHandleT*)data;
+ GUPnPDeviceInfo *device_info;
+ GUPnPServiceInfo *content_dir;
+ const char *device_name;
+
+ device_info = GUPNP_DEVICE_INFO (proxy);
+ device_name = gupnp_device_info_get_model_name (device_info);
+ content_dir = gupnp_device_info_get_service (device_info, URN_CONTENT_DIR);
+
+ if (strcmp (device_name, "Rygel") != 0)
+ return;
+ if (!content_dir)
+ return;
+
+ /* allocate the global array if it has not been not done */
+ if (!dev_ctx)
+ dev_ctx = (dev_ctx_T**) malloc (sizeof(dev_ctx_T));
+ else
+ dev_ctx = (dev_ctx_T**) realloc (dev_ctx, (client_count+1)*sizeof(dev_ctx_T));
+
+ /* create an element for the client in the global array */
+ dev_ctx[client_count] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
+ dev_ctx[client_count]->device_info = device_info;
+ dev_ctx[client_count]->content_dir = content_dir;
+
+ /* make the client context aware of it */
+ ctx->media_server = (void*)dev_ctx[client_count];
+}
+
+static void _rygel_av_transport_cb (GUPnPControlPoint *point, GUPnPDeviceProxy *proxy,
+ gpointer data) {
+
+ dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
+ GUPnPDeviceInfo *device_info;
+ GUPnPServiceInfo *av_transport;
+
+ device_info = GUPNP_DEVICE_INFO (proxy);
+ av_transport = gupnp_device_info_get_service (device_info, URN_AV_TRANSPORT);
+
+ dev_ctx_c->av_transport = av_transport;
+}
+
+static void _rygel_content_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action,
+ gpointer data) {
+
+ dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
+ GUPnPServiceProxy *content_dir_proxy = GUPNP_SERVICE_PROXY (content_dir);
+ GError *error;
+ char *result;
+ guint32 number_returned;
+ guint32 total_matches;
+ char *found;
+ char subid[33];
+
+ gupnp_service_proxy_end_action (content_dir, action, &error,
+ "Result", G_TYPE_STRING, &result,
+ "NumberReturned", G_TYPE_UINT, &number_returned,
+ "TotalMatches", G_TYPE_UINT, &total_matches,
+ NULL);
+
+ if (number_returned == 0)
+ return;
+
+ if (number_returned == 1) {
+ found = strstr (result, "id=\"");
+ found += 4;
+ strncpy (subid, found, 32); subid[32] = '\0';
+
+ gupnp_service_proxy_begin_action (content_dir_proxy, "Browse", _rygel_content_cb, dev_ctx_c,
+ "ObjectID", G_TYPE_STRING, subid,
+ "BrowseFlag", G_TYPE_STRING, "BrowseDirectChildren",
+ "Filter", G_TYPE_STRING, "@childCount",
+ "StartingIndex", G_TYPE_UINT, 0,
+ "RequestedCount", G_TYPE_UINT, 64,
+ "SortCriteria", G_TYPE_STRING, "",
+ NULL);
+ return;
+ }
+
+ if (number_returned > 1) {
+ dev_ctx_c->content_res = result;
+ dev_ctx_c->content_num = number_returned;
+ }
+}
+
+static void _rygel_metadata_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action,
+ gpointer data) {
+
+ dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
+ GError *error;
+ char *result;
+
+ gupnp_service_proxy_end_action (content_dir, action, &error,
+ "Result", G_TYPE_STRING, &result,
+ NULL);
+
+ dev_ctx_c->content_res = result;
+}
+
+static void _rygel_select_cb (GUPnPServiceProxy *av_transport, GUPnPServiceProxyAction *action,
+ gpointer data)
+{
+
+ dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
+ GUPnPServiceProxy *av_transport_proxy;
+ GError *error;
+ char *time;
+ struct timeval tv_start, tv_now;
+
+ av_transport_proxy = GUPNP_SERVICE_PROXY (av_transport);
+
+ gupnp_service_proxy_end_action (av_transport, action, &error, NULL);
+
+ switch (dev_ctx_c->target_state) {
+ case PLAY:
+ gupnp_service_proxy_begin_action (av_transport_proxy, "Play", _rygel_do_cb, dev_ctx_c,
+ "InstanceID", G_TYPE_UINT, 0,
+ "Speed", G_TYPE_STRING, "1",
+ NULL);
+ break;
+ case PAUSE:
+ gupnp_service_proxy_begin_action (av_transport_proxy, "Pause", _rygel_do_cb, dev_ctx_c,
+ "InstanceID", G_TYPE_UINT, 0,
+ NULL);
+ break;
+ case STOP:
+ gupnp_service_proxy_begin_action (av_transport_proxy, "Stop", _rygel_do_cb, dev_ctx_c,
+ "InstanceID", G_TYPE_UINT, 0,
+ NULL);
+ break;
+ case SEEK:
+ time = _rygel_time_for_string (dev_ctx_c->action_args);
+ gupnp_service_proxy_begin_action (av_transport_proxy, "Seek", _rygel_do_cb, dev_ctx_c,
+ "InstanceID", G_TYPE_UINT, 0,
+ "Unit", G_TYPE_STRING, "ABS_TIME",
+ "Target", G_TYPE_STRING, time,
+ NULL);
+ default:
+ break;
+ }
+
+ gettimeofday (&tv_start, NULL);
+ gettimeofday (&tv_now, NULL);
+ while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
+
+ g_main_context_iteration (dev_ctx_c->loop, FALSE);
+
+ if (dev_ctx_c->state == dev_ctx_c->target_state)
+ break;
+ gettimeofday (&tv_now, NULL);
+ }
+}
+
+static void _rygel_upload_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action,
+ gpointer data)
+{
+ dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
+ GUPnPServiceProxy *content_dir_proxy;
+ GError *error;
+ char *result, *start, *end, *dst_uri, *src_uri;
+ int length;
+ struct timeval tv_start, tv_now;
+
+ content_dir_proxy = GUPNP_SERVICE_PROXY (content_dir);
+
+ if (!gupnp_service_proxy_end_action (content_dir, action, &error,
+ "Result", G_TYPE_STRING, &result,
+ NULL))
+ return;
+
+ start = strstr (result, "<res importUri=\"");
+ if (!start)
+ return;
+
+ start += 16;
+ end = strstr (start, "\"");
+ length = end - start;
+
+ dst_uri = (char*) malloc(length+1);
+ strncpy (dst_uri, start, length);
+ dst_uri[length] = '\0';
+
+ asprintf (&src_uri, "http://%s:%u%s", gupnp_context_get_host_ip (dev_ctx_c->context),
+ gupnp_context_get_port (dev_ctx_c->context),
+ dev_ctx_c->transfer_path);
+
+ /* host the file */
+ gupnp_context_host_path (dev_ctx_c->context, dev_ctx_c->transfer_path,
+ dev_ctx_c->transfer_path);
+
+ gupnp_service_proxy_begin_action (content_dir_proxy, "ImportResource", _rygel_transfer_cb, dev_ctx_c,
+ "SourceURI", G_TYPE_STRING, src_uri,
+ "DestinationURI", G_TYPE_STRING, dst_uri,
+ NULL);
+
+ gettimeofday (&tv_start, NULL);
+ gettimeofday (&tv_now, NULL);
+ while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
+
+ g_main_context_iteration (dev_ctx_c->loop, FALSE);
+
+ if (dev_ctx_c->transfer_started)
+ break;
+ gettimeofday (&tv_now, NULL);
+ }
+}
+
+static void _rygel_transfer_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action,
+ gpointer data)
+{
+ dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
+ GError *error;
+ guint transfer_id;
+
+ if (!gupnp_service_proxy_end_action (content_dir, action, &error,
+ "TransferID", G_TYPE_UINT, &transfer_id,
+ NULL))
+ return;
+
+ dev_ctx_c->transfer_started = 1;
+}
+
+static void _rygel_do_cb (GUPnPServiceProxy *av_transport, GUPnPServiceProxyAction *action,
+ gpointer data)
+{
+ dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
+ GError *error;
+
+ if (!gupnp_service_proxy_end_action (av_transport, action, &error,
+ NULL))
+ return;
+
+ dev_ctx_c->state = dev_ctx_c->target_state;
+}
diff --git a/bindings/media/media-rygel.h b/bindings/media/media-rygel.h
new file mode 100644
index 00000000..951c6b89
--- /dev/null
+++ b/bindings/media/media-rygel.h
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2016 "IoT.bzh"
+ * Author "Manuel Bachmann"
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef MEDIA_RYGEL_H
+#define MEDIA_RYGEL_H
+
+/* --------------- MEDIA RYGEL DEFINITIONS ------------------ */
+
+#include <sys/time.h>
+#include <json-c/json.h>
+#include <libgupnp/gupnp-control-point.h>
+#include <libgupnp-av/gupnp-av.h>
+
+#include "media-api.h"
+
+#define URN_MEDIA_SERVER "urn:schemas-upnp-org:device:MediaServer:1"
+#define URN_MEDIA_RENDERER "urn:schemas-upnp-org:device:MediaRenderer:1"
+#define URN_CONTENT_DIR "urn:schemas-upnp-org:service:ContentDirectory"
+#define URN_AV_TRANSPORT "urn:schemas-upnp-org:service:AVTransport"
+
+typedef enum { PLAY, PAUSE, STOP, SEEK } State;
+typedef struct dev_ctx dev_ctx_T;
+
+struct dev_ctx {
+ GMainContext *loop;
+ GUPnPContext *context;
+ GUPnPDeviceInfo *device_info;
+ GUPnPServiceInfo *content_dir;
+ GUPnPServiceInfo *av_transport;
+ char *content_res;
+ int content_num;
+ State state;
+ State target_state;
+ char *action_args;
+ char *transfer_path;
+ unsigned char transfer_started;
+};
+
+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 *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 *);
+char* _rygel_find_upload_id (dev_ctx_T *, char *);
+char* _rygel_find_id_for_index (dev_ctx_T *, char *, unsigned int);
+char* _rygel_find_metadata_for_id (dev_ctx_T *, char *);
+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 *);
+#endif /* MEDIA_RYGEL_H */