X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=plugins%2Fmedia%2Fmedia-rygel.c;h=003277955e81d57dbc34a56b67ac10f3c0d5bcf0;hb=39c2ebc125fcc694ac349ae196b62729c7f05037;hp=3ad008efbb398dcd06785c0b345358f3a974451d;hpb=4c833aad4d3dcef303b34b70f86bb1e98153dc97;p=src%2Fapp-framework-binder.git diff --git a/plugins/media/media-rygel.c b/plugins/media/media-rygel.c index 3ad008ef..00327795 100644 --- a/plugins/media/media-rygel.c +++ b/plugins/media/media-rygel.c @@ -2,27 +2,43 @@ * Copyright (C) 2016 "IoT.bzh" * Author "Manuel Bachmann" * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * 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 * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * http://www.apache.org/licenses/LICENSE-2.0 * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * 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 +#include + #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 --- */ -PUBLIC unsigned char _rygel_init (mediaCtxHandleT *ctx) { +unsigned char _rygel_init (mediaCtxHandleT *ctx) { GMainContext *loop; GUPnPContext *context; @@ -65,13 +81,15 @@ PUBLIC unsigned char _rygel_init (mediaCtxHandleT *ctx) { 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; } -PUBLIC void _rygel_free (mediaCtxHandleT *ctx) { +void _rygel_free (mediaCtxHandleT *ctx) { dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server; @@ -86,43 +104,58 @@ PUBLIC void _rygel_free (mediaCtxHandleT *ctx) { dev_ctx_c->content_res = NULL; } -PUBLIC char* _rygel_list (mediaCtxHandleT *ctx) { +json_object* _rygel_list (mediaCtxHandleT *ctx) { dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server; - char *raw, *start, *end, *title, *result = NULL; + 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; - if (raw) { - start = strstr (raw, ""); - if (!start) return NULL; + start = strstr (raw, ""); + if (!start) + return NULL; - result = strdup(""); + json_o = json_object_new_object (); + json_a = json_object_new_array (); + while (start) { + json_object *json_i, *json_id, *json_title; - while (start) { - start = strstr (start, ""); - if (!start) break; - end = strstr (start, ""); - start += 10; length = end - start; + start = strstr (start, ""); + if (!start) break; + end = strstr (start, ""); + start += 10; + length = end - start; - title = (char*) malloc (length+1); - strncpy (title, start, length); - title[length] = '\0'; + asprintf (&id, "%02d", i); - asprintf (&result, "%s%02d:%s::", result, i, title); + title = (char*) malloc (length+1); + strncpy (title, start, length); + title[length] = '\0'; - free (title); i++; - } + 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++; } - return result; + json_object_object_add (json_o, "list", json_a); + + return json_o; } -PUBLIC unsigned char _rygel_choose (mediaCtxHandleT *ctx, unsigned int index) { +unsigned char _rygel_select (mediaCtxHandleT *ctx, unsigned int index) { dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server; unsigned int count; @@ -140,7 +173,25 @@ PUBLIC unsigned char _rygel_choose (mediaCtxHandleT *ctx, unsigned int index) { return 1; } -PUBLIC unsigned char _rygel_do (mediaCtxHandleT *ctx, State state) { +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; @@ -158,19 +209,18 @@ PUBLIC unsigned char _rygel_do (mediaCtxHandleT *ctx, State state) { 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); + return _rygel_start_doing (dev_ctx_c, uri, metadata, state, args); } /* --- LOCAL HELPER FUNCTIONS --- */ -STATIC char* _rygel_list_raw (dev_ctx_T* dev_ctx_c, unsigned int *count) { +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, @@ -197,7 +247,22 @@ STATIC char* _rygel_list_raw (dev_ctx_T* dev_ctx_c, unsigned int *count) { 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* _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]; @@ -208,16 +273,16 @@ STATIC char* _rygel_find_id_for_index (dev_ctx_T* dev_ctx_c, char *raw, unsigned found += 9; if (i == index) { - /* IDs are 32-bit numbers */ + /* IDs are 32-bit strings */ strncpy (id, found, 32); id[32] = '\0'; } } - return strdup(id); + return strdup (id); } -STATIC char* _rygel_find_metadata_for_id (dev_ctx_T* dev_ctx_c, char *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; @@ -249,7 +314,7 @@ STATIC char* _rygel_find_metadata_for_id (dev_ctx_T* dev_ctx_c, char *id) { return dev_ctx_c->content_res; } -STATIC char* _rygel_find_uri_for_metadata (dev_ctx_T* dev_ctx_c, char *metadata) { +char* _rygel_find_uri_for_metadata (dev_ctx_T* dev_ctx_c, char *metadata) { char *start, *end, *uri = NULL; int length; @@ -277,7 +342,78 @@ STATIC char* _rygel_find_uri_for_metadata (dev_ctx_T* dev_ctx_c, char *metadata) return uri; } -STATIC unsigned char _rygel_start_doing (dev_ctx_T* dev_ctx_c, char *uri, char *metadata, State state) { +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; @@ -287,14 +423,14 @@ STATIC unsigned char _rygel_start_doing (dev_ctx_T* dev_ctx_c, char *uri, char * 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); + NULL); gettimeofday (&tv_start, NULL); gettimeofday (&tv_now, NULL); @@ -312,7 +448,7 @@ STATIC unsigned char _rygel_start_doing (dev_ctx_T* dev_ctx_c, char *uri, char * return 1; } -STATIC unsigned char _rygel_find_av_transport (dev_ctx_T* dev_ctx_c) { +unsigned char _rygel_find_av_transport (dev_ctx_T* dev_ctx_c) { GUPnPControlPoint *control_point; gint handler_cb; @@ -346,7 +482,7 @@ STATIC unsigned char _rygel_find_av_transport (dev_ctx_T* dev_ctx_c) { /* ---- LOCAL CALLBACK FUNCTIONS ---- */ -STATIC void _rygel_device_cb (GUPnPControlPoint *point, GUPnPDeviceProxy *proxy, +static void _rygel_device_cb (GUPnPControlPoint *point, GUPnPDeviceProxy *proxy, gpointer data) { mediaCtxHandleT *ctx = (mediaCtxHandleT*)data; @@ -378,7 +514,7 @@ 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, +static void _rygel_av_transport_cb (GUPnPControlPoint *point, GUPnPDeviceProxy *proxy, gpointer data) { dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data; @@ -391,7 +527,7 @@ STATIC void _rygel_av_transport_cb (GUPnPControlPoint *point, GUPnPDeviceProxy * dev_ctx_c->av_transport = av_transport; } -STATIC void _rygel_content_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action, +static void _rygel_content_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action, gpointer data) { dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data; @@ -434,7 +570,7 @@ STATIC void _rygel_content_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxy } } -STATIC void _rygel_metadata_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action, +static void _rygel_metadata_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action, gpointer data) { dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data; @@ -448,13 +584,14 @@ STATIC void _rygel_metadata_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProx dev_ctx_c->content_res = result; } -STATIC void _rygel_select_cb (GUPnPServiceProxy *av_transport, GUPnPServiceProxyAction *action, - gpointer data) +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); @@ -478,6 +615,13 @@ STATIC void _rygel_select_cb (GUPnPServiceProxy *av_transport, GUPnPServiceProxy "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; } @@ -494,14 +638,84 @@ STATIC void _rygel_select_cb (GUPnPServiceProxy *av_transport, GUPnPServiceProxy } } -STATIC void _rygel_do_cb (GUPnPServiceProxy *av_transport, GUPnPServiceProxyAction *action, +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, "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; - gupnp_service_proxy_end_action (av_transport, action, &error, - NULL); + if (!gupnp_service_proxy_end_action (av_transport, action, &error, + NULL)) + return; dev_ctx_c->state = dev_ctx_c->target_state; }