aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/media/media-api.c78
-rw-r--r--plugins/media/media-api.h1
-rw-r--r--plugins/media/media-rygel.c339
-rw-r--r--plugins/media/media-rygel.h22
4 files changed, 420 insertions, 20 deletions
diff --git a/plugins/media/media-api.c b/plugins/media/media-api.c
index 4365862a..dc73136f 100644
--- a/plugins/media/media-api.c
+++ b/plugins/media/media-api.c
@@ -27,6 +27,7 @@ STATIC mediaCtxHandleT* initMediaCtx () {
ctx = malloc (sizeof(mediaCtxHandleT));
ctx->media_server = NULL;
+ ctx->index = 0;
return ctx;
}
@@ -57,10 +58,11 @@ STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CHECK */
STATIC json_object* list (AFB_request *request) { /* AFB_SESSION_CHECK */
+ mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
json_object *jresp;
char *result;
- result = _rygel_list (request->context);
+ result = _rygel_list (ctx);
if (!result)
return jsonNewMessage(AFB_FAIL, "No content found in media server");
@@ -70,15 +72,83 @@ STATIC json_object* list (AFB_request *request) { /* AFB_SESSION_CHECK */
return jresp;
}
+STATIC json_object* choose (AFB_request *request) { /* AFB_SESSION_CHECK */
+
+ mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
+ const char *value = getQueryValue (request, "value");
+ json_object *jresp;
+ unsigned int index;
+ char index_str[5];
+
+ /* 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)
+ return jsonNewMessage(AFB_FAIL, "Chosen index cannot be negative");
+
+ /* "?value=" parameter is positive */
+ else if (atoi(value) >= 0) {
+ index = (unsigned int) atoi(value);
+
+ if (!_rygel_choose (ctx, index))
+ return jsonNewMessage(AFB_FAIL, "Chosen index superior to current media count");
+
+ ctx->index = index;
+ jresp = json_object_new_object();
+ json_object_object_add (jresp, "index", json_object_new_string (value));
+ }
+
+ return jresp;
+}
+
+STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
+
+ mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
+
+ if (!_rygel_do (ctx, PLAY))
+ return jsonNewMessage(AFB_FAIL, "Could not play chosen media");
+
+ return jsonNewMessage(AFB_SUCCESS, "PLaying media");
+}
+
+STATIC json_object* stop (AFB_request *request) { /* AFB_SESSION_CHECK */
+
+ mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
+
+ if (!_rygel_do (ctx, STOP))
+ return jsonNewMessage(AFB_FAIL, "Could not stop chosen media");
+
+ return jsonNewMessage(AFB_SUCCESS, "Stopped media");
+}
+
+STATIC json_object* paused (AFB_request *request) { /* AFB_SESSION_CHECK */
+
+ mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
+
+ if (!_rygel_do (ctx, PAUSE))
+ return jsonNewMessage(AFB_FAIL, "Could not pause chosen media");
+
+ return jsonNewMessage(AFB_SUCCESS, "Paused media");
+}
+
STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Media API");
}
STATIC AFB_restapi pluginApis[]= {
- {"init" , AFB_SESSION_CHECK, (AFB_apiCB)init , "Media API - init"},
- {"list" , AFB_SESSION_CHECK, (AFB_apiCB)list , "Media API - list"},
- {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Media API - ping"},
+ {"init" , AFB_SESSION_CHECK, (AFB_apiCB)init , "Media API - init" },
+ {"list" , AFB_SESSION_CHECK, (AFB_apiCB)list , "Media API - list" },
+ {"choose" , AFB_SESSION_CHECK, (AFB_apiCB)choose , "Media API - choose" },
+ {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Media API - play" },
+ {"stop" , AFB_SESSION_CHECK, (AFB_apiCB)stop , "Media API - stop" },
+ {"paused" , AFB_SESSION_CHECK, (AFB_apiCB)paused , "Media API - paused" },
+ {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Media API - ping" },
{NULL}
};
diff --git a/plugins/media/media-api.h b/plugins/media/media-api.h
index ae19a311..70dcc1e6 100644
--- a/plugins/media/media-api.h
+++ b/plugins/media/media-api.h
@@ -26,6 +26,7 @@
/* 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;
PUBLIC char* _rygel_list (mediaCtxHandleT *);
diff --git a/plugins/media/media-rygel.c b/plugins/media/media-rygel.c
index 0af1f4ad..3ad008ef 100644
--- a/plugins/media/media-rygel.c
+++ b/plugins/media/media-rygel.c
@@ -27,6 +27,7 @@ PUBLIC 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);
@@ -54,14 +55,20 @@ PUBLIC unsigned char _rygel_init (mediaCtxHandleT *ctx) {
}
/* fail if we found no server */
if (!ctx->media_server)
- return -1;
+ 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;
client_count++;
- return 0;
+ return 1;
}
PUBLIC void _rygel_free (mediaCtxHandleT *ctx) {
@@ -74,21 +81,95 @@ PUBLIC void _rygel_free (mediaCtxHandleT *ctx) {
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;
- if (dev_ctx_c->content_res)
- free (dev_ctx_c->content_res);
dev_ctx_c->content_res = NULL;
}
PUBLIC char* _rygel_list (mediaCtxHandleT *ctx) {
dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server;
+ char *raw, *start, *end, *title, *result = NULL;
+ int length, i = 0;
+
+ if (!dev_ctx_c)
+ return NULL;
+
+ raw = _rygel_list_raw (dev_ctx_c, NULL);
+
+ if (raw) {
+ start = strstr (raw, "<dc:title>");
+ if (!start) return NULL;
+
+ result = strdup("");
+
+ while (start) {
+ start = strstr (start, "<dc:title>");
+ if (!start) break;
+ end = strstr (start, "</dc:title>");
+ start += 10; length = end - start;
+
+ title = (char*) malloc (length+1);
+ strncpy (title, start, length);
+ title[length] = '\0';
+
+ asprintf (&result, "%s%02d:%s::", result, i, title);
+
+ free (title); i++;
+ }
+ }
+
+ return result;
+}
+
+PUBLIC unsigned char _rygel_choose (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;
+}
+
+PUBLIC unsigned char _rygel_do (mediaCtxHandleT *ctx, State state) {
+
+ 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);
+}
+
+/* --- LOCAL HELPER FUNCTIONS --- */
+
+STATIC char* _rygel_list_raw (dev_ctx_T* dev_ctx_c, unsigned int *count) {
+
GUPnPServiceProxy *content_dir_proxy;
struct timeval tv_start, tv_now;
- if (dev_ctx_c->content_res)
- free (dev_ctx_c->content_res);
dev_ctx_c->content_res = NULL;
+ dev_ctx_c->content_num = 0;
content_dir_proxy = GUPNP_SERVICE_PROXY (dev_ctx_c->content_dir);
@@ -112,13 +193,161 @@ PUBLIC char* _rygel_list (mediaCtxHandleT *ctx) {
gettimeofday (&tv_now, NULL);
}
+ if (count) *count = dev_ctx_c->content_num;
return dev_ctx_c->content_res;
}
+STATIC 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 numbers */
+ strncpy (id, found, 32);
+ id[32] = '\0';
+ }
+ }
+
+ return strdup(id);
+}
+
+STATIC 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;
+}
+
+STATIC 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;
+}
+
+STATIC unsigned char _rygel_start_doing (dev_ctx_T* dev_ctx_c, char *uri, char *metadata, State state) {
+
+ 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;
+
+ 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;
+}
+
+STATIC 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) {
+ gpointer data) {
mediaCtxHandleT *ctx = (mediaCtxHandleT*)data;
GUPnPDeviceInfo *device_info;
@@ -134,9 +363,6 @@ STATIC void _rygel_device_cb (GUPnPControlPoint *point, GUPnPDeviceProxy *proxy,
if (!content_dir)
return;
- /* we have found Rygel ; stop looking for it... */
- g_signal_handler_disconnect (point, handler_cb);
-
/* allocate the global array if it has not been not done */
if (!dev_ctx)
dev_ctx = (dev_ctx_T**) malloc (sizeof(dev_ctx_T));
@@ -152,8 +378,21 @@ STATIC void _rygel_device_cb (GUPnPControlPoint *point, GUPnPDeviceProxy *proxy,
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) {
+ gpointer data) {
dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
GUPnPServiceProxy *content_dir_proxy = GUPNP_SERVICE_PROXY (content_dir);
@@ -189,6 +428,80 @@ STATIC void _rygel_content_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxy
return;
}
- if (number_returned > 1)
- dev_ctx_c->content_res = strdup (result);
+ 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;
+ 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;
+ 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_do_cb (GUPnPServiceProxy *av_transport, GUPnPServiceProxyAction *action,
+ gpointer data)
+{
+ dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
+ GError *error;
+
+ gupnp_service_proxy_end_action (av_transport, action, &error,
+ NULL);
+
+ dev_ctx_c->state = dev_ctx_c->target_state;
}
diff --git a/plugins/media/media-rygel.h b/plugins/media/media-rygel.h
index 37fa53c4..da028c13 100644
--- a/plugins/media/media-rygel.h
+++ b/plugins/media/media-rygel.h
@@ -26,9 +26,12 @@
#include "local-def.h"
-#define URN_MEDIA_SERVER "urn:schemas-upnp-org:device:MediaServer:1"
-#define URN_CONTENT_DIR "urn:schemas-upnp-org:service:ContentDirectory"
+#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 } State;
typedef struct dev_ctx dev_ctx_T;
struct dev_ctx {
@@ -36,13 +39,26 @@ struct dev_ctx {
GUPnPContext *context;
GUPnPDeviceInfo *device_info;
GUPnPServiceInfo *content_dir;
+ GUPnPServiceInfo *av_transport;
char *content_res;
+ int content_num;
+ State state;
+ State target_state;
};
+STATIC char* _rygel_list_raw (dev_ctx_T *, unsigned int *);
+STATIC char* _rygel_find_id_for_index (dev_ctx_T *, char *, unsigned int);
+STATIC char* _rygel_find_metadata_for_id (dev_ctx_T *, char *);
+STATIC char* _rygel_find_uri_for_metadata (dev_ctx_T *, char *);
+STATIC unsigned char _rygel_start_doing (dev_ctx_T *, char *, char *, State);
+STATIC 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_do_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer);
-static gint handler_cb;
static unsigned int client_count = 0;
static struct dev_ctx **dev_ctx = NULL;