add .gitreview
[src/app-framework-binder.git] / plugins / media / media-rygel.c
1 /*
2  * Copyright (C) 2016 "IoT.bzh"
3  * Author "Manuel Bachmann"
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <string.h>
21
22 #include "media-api.h"
23 #include "media-rygel.h"
24
25 static void _rygel_device_cb (GUPnPControlPoint *, GUPnPDeviceProxy *, gpointer);
26 static void _rygel_av_transport_cb (GUPnPControlPoint *, GUPnPDeviceProxy *, gpointer);
27 static void _rygel_content_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer);
28 static void _rygel_metadata_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer);
29 static void _rygel_select_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer);
30 static void _rygel_upload_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer);
31 static void _rygel_transfer_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer);
32 static void _rygel_do_cb (GUPnPServiceProxy *, GUPnPServiceProxyAction *, gpointer);
33
34 static unsigned int client_count = 0;
35 static struct dev_ctx **dev_ctx = NULL;
36
37 /* -------------- MEDIA RYGEL IMPLEMENTATION ---------------- */
38
39 /* --- PUBLIC FUNCTIONS --- */
40
41 unsigned char _rygel_init (mediaCtxHandleT *ctx) {
42
43     GMainContext *loop;
44     GUPnPContext *context;
45     GUPnPControlPoint *control_point;
46     gint handler_cb;
47     struct timeval tv_start, tv_now;
48
49     context = gupnp_context_new (NULL, NULL, 0, NULL);
50
51     control_point = gupnp_control_point_new (context, URN_MEDIA_SERVER);
52
53     handler_cb = g_signal_connect (control_point, "device-proxy-available",
54                                    G_CALLBACK (_rygel_device_cb), ctx);
55
56     /* start searching for servers */
57     gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (control_point), TRUE);
58
59     loop = g_main_context_default ();
60
61     /* 5 seconds should be sufficient to find Rygel */
62     gettimeofday (&tv_start, NULL);
63     gettimeofday (&tv_now, NULL);
64     while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
65
66         g_main_context_iteration (loop, FALSE);
67
68         if (ctx->media_server)
69             break;
70         gettimeofday (&tv_now, NULL);
71     }
72     /* fail if we found no server */
73     if (!ctx->media_server)
74       return 0;
75
76     /* we have found the server ; stop looking for it... */
77     g_signal_handler_disconnect (control_point, handler_cb);
78
79     dev_ctx[client_count]->loop = loop;
80     dev_ctx[client_count]->context = context;
81     dev_ctx[client_count]->av_transport = NULL;
82     dev_ctx[client_count]->state = STOP;
83     dev_ctx[client_count]->target_state = STOP;
84     dev_ctx[client_count]->action_args = NULL;
85     dev_ctx[client_count]->transfer_started = 0;
86
87     client_count++;
88
89     return 1;
90 }
91
92 void _rygel_free (mediaCtxHandleT *ctx) {
93
94     dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server;
95
96     client_count--;
97
98     g_main_context_unref (dev_ctx_c->loop);
99     dev_ctx_c->loop = NULL;
100     dev_ctx_c->context = NULL;
101     dev_ctx_c->device_info = NULL;
102     dev_ctx_c->av_transport = NULL;
103     dev_ctx_c->content_dir = NULL;
104     dev_ctx_c->content_res = NULL;
105 }
106
107 json_object* _rygel_list (mediaCtxHandleT *ctx) {
108
109     dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server;
110     json_object *json_o, *json_a;
111     char *raw, *start, *end, *id, *title;
112     int length, i = 0;
113
114     if (!dev_ctx_c)
115       return NULL;
116
117     raw = _rygel_list_raw (dev_ctx_c, NULL);
118     if (!raw)
119       return NULL;
120
121     start = strstr (raw, "<dc:title>");
122     if (!start)
123       return NULL;
124
125     json_o = json_object_new_object ();
126     json_a = json_object_new_array ();
127     while (start) {
128         json_object *json_i, *json_id, *json_title;
129
130         start = strstr (start, "<dc:title>");
131         if (!start) break;
132         end = strstr (start, "</dc:title>");
133         start += 10;
134         length = end - start;
135
136         asprintf (&id, "%02d", i);
137
138         title = (char*) malloc (length+1);
139         strncpy (title, start, length);
140         title[length] = '\0';
141
142         json_i = json_object_new_object ();
143         json_id = json_object_new_string (id);
144         json_title = json_object_new_string (title);
145         json_object_object_add (json_i, "id", json_id);
146         json_object_object_add (json_i, "title", json_title);
147         json_object_array_add (json_a, json_i);
148
149         free (id); free (title); 
150         i++;
151     }
152
153     json_object_object_add (json_o, "list", json_a);
154
155     return json_o;
156 }
157
158 unsigned char _rygel_select (mediaCtxHandleT *ctx, unsigned int index) {
159
160     dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server;
161     unsigned int count;
162
163     if (!dev_ctx_c)
164       return 0;
165
166     if (!_rygel_list_raw (dev_ctx_c, &count) ||
167         index >= count)
168       return 0;
169
170     if (ctx->index != index)
171       dev_ctx_c->state = STOP;
172
173     return 1;
174 }
175
176 unsigned char _rygel_upload (mediaCtxHandleT *ctx, const char *path, void (*oncompletion)(void*,int), void *closure) {
177
178     dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server;
179     char *raw, *upload_id;
180
181     if (!dev_ctx_c)
182       return 0;
183
184     raw = _rygel_list_raw (dev_ctx_c, NULL);
185     if (!raw)
186       return 0;
187
188     /* for now, we always use the same upload container id */
189     upload_id = _rygel_find_upload_id (dev_ctx_c, raw);
190
191     return _rygel_start_uploading (dev_ctx_c, strdup(path), upload_id);
192 }
193
194 unsigned char _rygel_do (mediaCtxHandleT *ctx, State state, char *args) {
195
196     dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server;
197     unsigned int index = ctx->index;
198     unsigned int count;
199     char *raw, *id, *metadata, *uri;
200
201     if (!dev_ctx_c || dev_ctx_c->state == state)
202         return 0;
203
204     raw = _rygel_list_raw (dev_ctx_c, &count);
205     if (!raw || index >= count)
206       return 0;
207
208           id = _rygel_find_id_for_index (dev_ctx_c, raw, index);
209     metadata = _rygel_find_metadata_for_id (dev_ctx_c, id);
210          uri = _rygel_find_uri_for_metadata (dev_ctx_c, metadata);
211
212     return _rygel_start_doing (dev_ctx_c, uri, metadata, state, args);
213 }
214
215 /* --- LOCAL HELPER FUNCTIONS --- */
216
217 char* _rygel_list_raw (dev_ctx_T* dev_ctx_c, unsigned int *count) {
218
219     GUPnPServiceProxy *content_dir_proxy;
220     struct timeval tv_start, tv_now;
221
222     dev_ctx_c->content_res = NULL;
223     dev_ctx_c->content_num = 0;
224     content_dir_proxy = GUPNP_SERVICE_PROXY (dev_ctx_c->content_dir);
225
226     gupnp_service_proxy_begin_action (content_dir_proxy, "Browse", _rygel_content_cb, dev_ctx_c,
227                                       "ObjectID", G_TYPE_STRING, "Filesystem",
228                                       "BrowseFlag", G_TYPE_STRING, "BrowseDirectChildren",
229                                       "Filter", G_TYPE_STRING, "@childCount",
230                                       "StartingIndex", G_TYPE_UINT, 0,
231                                       "RequestedCount", G_TYPE_UINT, 64,
232                                       "SortCriteria", G_TYPE_STRING, "",
233                                        NULL);
234
235     gettimeofday (&tv_start, NULL);
236     gettimeofday (&tv_now, NULL);
237     while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
238
239         g_main_context_iteration (dev_ctx_c->loop, FALSE);
240
241         if (dev_ctx_c->content_res)
242             break;
243         gettimeofday (&tv_now, NULL);
244     }
245
246     if (count) *count = dev_ctx_c->content_num;
247     return dev_ctx_c->content_res;
248 }
249
250 char* _rygel_find_upload_id (dev_ctx_T* dev_ctx_c, char *raw) {
251
252     char *found;
253     char id[33];
254
255     found = strstr (raw, "parentID=\"");
256     found += 10;
257
258     /* IDs are 32-bit strings */
259     strncpy (id, found, 32);
260     id[32] = '\0';
261
262     return strdup (id);
263 }
264
265 char* _rygel_find_id_for_index (dev_ctx_T* dev_ctx_c, char *raw, unsigned int index) {
266
267     char *found = raw;
268     char id[33];
269     int i;
270
271     for (i = 0; i <= index; i++) {
272         found = strstr (found, "item id=");
273         found += 9;
274
275         if (i == index) {
276             /* IDs are 32-bit strings */
277             strncpy (id, found, 32);
278             id[32] = '\0';
279         }
280     }
281
282     return strdup (id);
283 }
284
285 char* _rygel_find_metadata_for_id (dev_ctx_T* dev_ctx_c, char *id) {
286
287     GUPnPServiceProxy *content_dir_proxy;
288     struct timeval tv_start, tv_now;
289
290     dev_ctx_c->content_res = NULL;
291
292     content_dir_proxy = GUPNP_SERVICE_PROXY (dev_ctx_c->content_dir);
293
294     gupnp_service_proxy_begin_action (content_dir_proxy, "Browse", _rygel_metadata_cb, dev_ctx_c,
295                                       "ObjectID", G_TYPE_STRING, id,
296                                       "BrowseFlag", G_TYPE_STRING, "BrowseMetadata",
297                                       "Filter", G_TYPE_STRING, "*",
298                                       "StartingIndex", G_TYPE_UINT, 0,
299                                       "RequestedCount", G_TYPE_UINT, 0,
300                                       "SortCriteria", G_TYPE_STRING, "",
301                                        NULL);
302
303     gettimeofday (&tv_start, NULL);
304     gettimeofday (&tv_now, NULL);
305     while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
306
307         g_main_context_iteration (dev_ctx_c->loop, FALSE);
308
309         if (dev_ctx_c->content_res)
310             break;
311         gettimeofday (&tv_now, NULL);
312     }
313
314     return dev_ctx_c->content_res;
315 }
316
317 char* _rygel_find_uri_for_metadata (dev_ctx_T* dev_ctx_c, char *metadata) {
318
319     char *start, *end, *uri = NULL;
320     int length;
321
322     /* position ourselves after the first "<res " tag */
323     start = strstr (metadata, "<res ");
324
325     while (start) {
326         start = strstr (start, "http://");
327         if (!start) break;
328         end = strstr (start, "</res>");
329         length = end - start;
330
331
332         uri = (char *)malloc (length + 1);
333         strncpy (uri, start, length);
334         uri[length] = '\0';
335         /* if the URI contains "primary_http", it is the main one ; stop here...*/
336         if (strstr (uri, "primary_http"))
337           break;
338
339         free (uri); start = end;
340     }
341
342     return uri;
343 }
344
345 char * _rygel_time_for_string (char *string) {
346
347     int total_seconds;
348     unsigned int hours, minutes, seconds;
349     char *time;
350
351     total_seconds = atoi (string);
352     hours = total_seconds / 3600;
353     minutes = (total_seconds / 60) - (hours * 60);
354     seconds = total_seconds - (hours * 3600) - (minutes * 60);
355
356     asprintf (&time, "%u:%02u:%02u", hours, minutes, seconds);
357
358     return time;
359 }
360
361 unsigned char _rygel_start_uploading (dev_ctx_T* dev_ctx_c, char *path, char *upload_id) {
362
363     GUPnPServiceProxy *content_dir_proxy;
364     GUPnPDIDLLiteWriter *didl_writer;
365     GUPnPDIDLLiteObject *didl_object;
366     char *didl, *content_type, *mime_type, *upnp_class;
367     struct timeval tv_start, tv_now;
368
369     didl_writer = gupnp_didl_lite_writer_new (NULL);
370     didl_object = GUPNP_DIDL_LITE_OBJECT (gupnp_didl_lite_writer_add_item (didl_writer));
371
372     /* create the metadata for the file */
373     gupnp_didl_lite_object_set_parent_id (didl_object, upload_id);
374     gupnp_didl_lite_object_set_id (didl_object, "");
375     gupnp_didl_lite_object_set_restricted (didl_object, FALSE);
376     gupnp_didl_lite_object_set_title (didl_object, g_path_get_basename (path));
377     /* deduce the UPnP class from the MIME type ("audio/ogg" e.g.) */
378     content_type = g_content_type_guess (path, NULL, 0, NULL);
379     mime_type = g_content_type_get_mime_type (content_type);
380     if (strstr (mime_type, "audio/"))
381       upnp_class = strdup ("object.item.audioItem.musicTrack");
382     else if (strstr (mime_type, "video/"))
383       upnp_class = strdup ("object.item.videoItem");
384     else if (strstr (mime_type, "image/"))
385       upnp_class = strdup ("object.item.imageItem");
386     else
387       upnp_class = strdup ("object.item");
388     gupnp_didl_lite_object_set_upnp_class (didl_object, upnp_class);
389     didl = gupnp_didl_lite_writer_get_string (didl_writer);
390
391     dev_ctx_c->transfer_path = path;
392     dev_ctx_c->transfer_started = 0;
393     content_dir_proxy = GUPNP_SERVICE_PROXY (dev_ctx_c->content_dir);
394
395     gupnp_service_proxy_begin_action (content_dir_proxy, "CreateObject", _rygel_upload_cb, dev_ctx_c,
396                                       "ContainerID", G_TYPE_STRING, upload_id,
397                                       "Elements", G_TYPE_STRING, didl,
398                                        NULL);
399
400     gettimeofday (&tv_start, NULL);
401     gettimeofday (&tv_now, NULL);
402     while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
403
404       g_main_context_iteration (dev_ctx_c->loop, FALSE);
405
406       if (dev_ctx_c->transfer_started)
407         break;
408       gettimeofday (&tv_now, NULL);
409     }
410     if (!dev_ctx_c->transfer_started)
411       return 0;
412
413     return 1;
414 }
415
416 unsigned char _rygel_start_doing (dev_ctx_T* dev_ctx_c, char *uri, char *metadata, State state, char *args) {
417
418     GUPnPServiceProxy *av_transport_proxy;
419     struct timeval tv_start, tv_now;
420
421     if (!dev_ctx_c->av_transport) {
422       if (!_rygel_find_av_transport (dev_ctx_c))
423          return 0;
424     }
425     dev_ctx_c->target_state = state;
426     dev_ctx_c->action_args = args;
427     av_transport_proxy = GUPNP_SERVICE_PROXY (dev_ctx_c->av_transport);
428
429     gupnp_service_proxy_begin_action (av_transport_proxy, "SetAVTransportURI", _rygel_select_cb, dev_ctx_c,
430                                       "InstanceID", G_TYPE_UINT, 0,
431                                       "CurrentURI", G_TYPE_STRING, uri,
432                                       "CurrentURIMetaData", G_TYPE_STRING, metadata,
433                                        NULL);
434
435     gettimeofday (&tv_start, NULL);
436     gettimeofday (&tv_now, NULL);
437     while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
438
439       g_main_context_iteration (dev_ctx_c->loop, FALSE);
440
441       if (dev_ctx_c->state == state)
442         break;
443       gettimeofday (&tv_now, NULL);
444     }
445     if (dev_ctx_c->state != state)
446       return 0;
447
448     return 1;
449 }
450
451 unsigned char _rygel_find_av_transport (dev_ctx_T* dev_ctx_c) {
452
453     GUPnPControlPoint *control_point;
454     gint handler_cb;
455     struct timeval tv_start, tv_now;
456
457     control_point = gupnp_control_point_new (dev_ctx_c->context, URN_MEDIA_RENDERER);
458
459     handler_cb = g_signal_connect (control_point, "device-proxy-available",
460                                    G_CALLBACK (_rygel_av_transport_cb), dev_ctx_c);
461
462     gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (control_point), TRUE);
463
464     gettimeofday (&tv_start, NULL);
465     gettimeofday (&tv_now, NULL);
466     while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
467
468         g_main_context_iteration (dev_ctx_c->loop, FALSE);
469
470         if (dev_ctx_c->av_transport)
471             break;
472         gettimeofday (&tv_now, NULL);
473     }
474     g_signal_handler_disconnect (control_point, handler_cb);
475
476     if (!dev_ctx_c->av_transport)
477       return 0;
478
479     return 1;
480 }
481
482
483  /* ---- LOCAL CALLBACK FUNCTIONS ---- */
484
485 static void _rygel_device_cb (GUPnPControlPoint *point, GUPnPDeviceProxy *proxy,
486                               gpointer data) {
487
488     mediaCtxHandleT *ctx = (mediaCtxHandleT*)data;
489     GUPnPDeviceInfo *device_info;
490     GUPnPServiceInfo *content_dir;
491     const char *device_name;
492
493     device_info = GUPNP_DEVICE_INFO (proxy);
494     device_name = gupnp_device_info_get_model_name (device_info);
495     content_dir = gupnp_device_info_get_service (device_info, URN_CONTENT_DIR);
496
497     if (strcmp (device_name, "Rygel") != 0)
498         return;
499     if (!content_dir)
500         return;
501
502     /* allocate the global array if it has not been not done */
503     if (!dev_ctx)
504         dev_ctx = (dev_ctx_T**) malloc (sizeof(dev_ctx_T));
505     else
506         dev_ctx = (dev_ctx_T**) realloc (dev_ctx, (client_count+1)*sizeof(dev_ctx_T));
507
508     /* create an element for the client in the global array */
509     dev_ctx[client_count] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
510     dev_ctx[client_count]->device_info = device_info;
511     dev_ctx[client_count]->content_dir = content_dir;
512
513     /* make the client context aware of it */
514     ctx->media_server = (void*)dev_ctx[client_count];
515 }
516
517 static void _rygel_av_transport_cb (GUPnPControlPoint *point, GUPnPDeviceProxy *proxy,
518                                     gpointer data) {
519
520     dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
521     GUPnPDeviceInfo *device_info;
522     GUPnPServiceInfo *av_transport;
523
524     device_info = GUPNP_DEVICE_INFO (proxy);
525     av_transport = gupnp_device_info_get_service (device_info, URN_AV_TRANSPORT);
526
527     dev_ctx_c->av_transport = av_transport;
528 }
529
530 static void _rygel_content_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action,
531                                gpointer data) {
532
533     dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
534     GUPnPServiceProxy *content_dir_proxy = GUPNP_SERVICE_PROXY (content_dir);
535     GError *error;
536     char *result;
537     guint32 number_returned;
538     guint32 total_matches;
539     char *found;
540     char subid[33];
541
542     gupnp_service_proxy_end_action (content_dir, action, &error,
543                                     "Result", G_TYPE_STRING, &result,
544                                     "NumberReturned", G_TYPE_UINT, &number_returned,
545                                     "TotalMatches", G_TYPE_UINT, &total_matches,
546                                      NULL);
547
548     if (number_returned == 0)
549         return;
550
551     if (number_returned == 1) {
552         found = strstr (result, "id=\"");       
553         found += 4;
554         strncpy (subid, found, 32); subid[32] = '\0';
555
556         gupnp_service_proxy_begin_action (content_dir_proxy, "Browse", _rygel_content_cb, dev_ctx_c,
557                                           "ObjectID", G_TYPE_STRING, subid,
558                                           "BrowseFlag", G_TYPE_STRING, "BrowseDirectChildren",
559                                           "Filter", G_TYPE_STRING, "@childCount",
560                                           "StartingIndex", G_TYPE_UINT, 0,
561                                           "RequestedCount", G_TYPE_UINT, 64,
562                                           "SortCriteria", G_TYPE_STRING, "",
563                                            NULL);
564         return;
565     }
566
567     if (number_returned > 1) {
568         dev_ctx_c->content_res = result;
569         dev_ctx_c->content_num = number_returned;
570     }
571 }
572
573 static void _rygel_metadata_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action,
574                                 gpointer data) {
575
576     dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
577     GError *error;
578     char *result;
579
580     gupnp_service_proxy_end_action (content_dir, action, &error,
581                                     "Result", G_TYPE_STRING, &result,
582                                      NULL);
583
584     dev_ctx_c->content_res = result;
585 }
586
587 static void _rygel_select_cb (GUPnPServiceProxy *av_transport, GUPnPServiceProxyAction *action,
588                               gpointer data)
589 {
590
591     dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
592     GUPnPServiceProxy *av_transport_proxy;
593     GError *error;
594     char *time;
595     struct timeval tv_start, tv_now;
596
597     av_transport_proxy = GUPNP_SERVICE_PROXY (av_transport);
598
599     gupnp_service_proxy_end_action (av_transport, action, &error, NULL);
600
601     switch (dev_ctx_c->target_state) {
602         case PLAY:
603           gupnp_service_proxy_begin_action (av_transport_proxy, "Play", _rygel_do_cb, dev_ctx_c,
604                                            "InstanceID", G_TYPE_UINT, 0,
605                                            "Speed", G_TYPE_STRING, "1",
606                                             NULL);
607           break;
608        case PAUSE:
609           gupnp_service_proxy_begin_action (av_transport_proxy, "Pause", _rygel_do_cb, dev_ctx_c,
610                                            "InstanceID", G_TYPE_UINT, 0,
611                                             NULL);
612           break;
613        case STOP:
614           gupnp_service_proxy_begin_action (av_transport_proxy, "Stop", _rygel_do_cb, dev_ctx_c,
615                                            "InstanceID", G_TYPE_UINT, 0,
616                                             NULL);
617           break;
618        case SEEK:
619           time = _rygel_time_for_string (dev_ctx_c->action_args);
620           gupnp_service_proxy_begin_action (av_transport_proxy, "Seek", _rygel_do_cb, dev_ctx_c,
621                                            "InstanceID", G_TYPE_UINT, 0,
622                                            "Unit", G_TYPE_STRING, "ABS_TIME",
623                                            "Target", G_TYPE_STRING, time,
624                                             NULL);
625        default:
626          break;
627     }
628
629     gettimeofday (&tv_start, NULL);
630     gettimeofday (&tv_now, NULL);
631     while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
632
633         g_main_context_iteration (dev_ctx_c->loop, FALSE);
634
635         if (dev_ctx_c->state == dev_ctx_c->target_state)
636             break;
637         gettimeofday (&tv_now, NULL);
638     }
639 }
640
641 static void _rygel_upload_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action,
642                               gpointer data)
643 {
644     dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
645     GUPnPServiceProxy *content_dir_proxy;
646     GError *error;
647     char *result, *start, *end, *dst_uri, *src_uri;
648     int length;
649     struct timeval tv_start, tv_now;
650
651     content_dir_proxy = GUPNP_SERVICE_PROXY (content_dir);
652
653     if (!gupnp_service_proxy_end_action (content_dir, action, &error,
654                                          "Result", G_TYPE_STRING, &result,
655                                           NULL))
656       return;
657
658     start = strstr (result, "<res importUri=\"");
659     if (!start)
660       return;
661
662     start += 16;
663     end = strstr (start, "\"");
664     length = end - start;
665
666     dst_uri = (char*) malloc(length+1);
667     strncpy (dst_uri, start, length);
668     dst_uri[length] = '\0';
669
670     asprintf (&src_uri, "http://%s:%u%s", gupnp_context_get_host_ip (dev_ctx_c->context),
671                                           gupnp_context_get_port (dev_ctx_c->context),
672                                           dev_ctx_c->transfer_path);
673
674     /* host the file */
675     gupnp_context_host_path (dev_ctx_c->context, dev_ctx_c->transfer_path,
676                                                  dev_ctx_c->transfer_path);
677
678     gupnp_service_proxy_begin_action (content_dir_proxy, "ImportResource", _rygel_transfer_cb, dev_ctx_c,
679                                       "SourceURI", G_TYPE_STRING, src_uri,
680                                       "DestinationURI", G_TYPE_STRING, dst_uri,
681                                        NULL);
682
683     gettimeofday (&tv_start, NULL);
684     gettimeofday (&tv_now, NULL);
685     while (tv_now.tv_sec - tv_start.tv_sec <= 5) {
686
687         g_main_context_iteration (dev_ctx_c->loop, FALSE);
688
689         if (dev_ctx_c->transfer_started)
690             break;
691         gettimeofday (&tv_now, NULL);
692     }
693 }
694
695 static void _rygel_transfer_cb (GUPnPServiceProxy *content_dir, GUPnPServiceProxyAction *action,
696                                 gpointer data)
697 {
698     dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
699     GError *error;
700     guint transfer_id;
701
702     if (!gupnp_service_proxy_end_action (content_dir, action, &error,
703                                          "TransferID", G_TYPE_UINT, &transfer_id,
704                                           NULL))
705       return;
706
707     dev_ctx_c->transfer_started = 1;
708 }
709
710 static void _rygel_do_cb (GUPnPServiceProxy *av_transport, GUPnPServiceProxyAction *action,
711                           gpointer data)
712 {
713     dev_ctx_T *dev_ctx_c = (dev_ctx_T*)data;
714     GError *error;
715
716     if (!gupnp_service_proxy_end_action (av_transport, action, &error,
717                                          NULL))
718       return;
719
720     dev_ctx_c->state = dev_ctx_c->target_state;
721 }