summaryrefslogtreecommitdiffstats
path: root/plugins/media/media-rygel.c
blob: 0af1f4adb564ef6fdeb1d8a08eee7223fd455423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "media-api.h"

/* -------------- MEDIA RYGEL IMPLEMENTATION ---------------- */

/* --- PUBLIC FUNCTIONS --- */

PUBLIC unsigned char _rygel_init (mediaCtxHandleT *ctx) {

    GMainContext *loop;
    GUPnPContext *context;
    GUPnPControlPoint *control_point;
    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 -1;

    dev_ctx[client_count]->loop = loop;
    dev_ctx[client_count]->context = context;

    client_count++;

    return 0;
}

PUBLIC 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->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;
    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;

    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);
    }

    return dev_ctx_c->content_res;
}

 /* ---- 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;

    /* 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));
    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_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 = strdup (result);
}