summaryrefslogtreecommitdiff
path: root/examples/standalone-server.c
blob: d43b99e121a3b77a2a19998ff54be95fe4543d9e (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
/*
 * Copyright (C) 2012 Openismus GmbH.
 * Copyright (C) 2012 Intel Corporation.
 *
 * Author: Jens Georg <jensg@openismus.com>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * Demo application for librygel-server.
 *
 * Creates a stand-alone UPnP server that exposes the contents of the current
 * folder or the folder passed as argument.
 *
 * Usage:
 *   standalone-server [<folder>]
 *
 * The server listens on wlan0 and eth0 by default.
 */

#include <gio/gio.h>
#include <rygel-server.h>
#include <rygel-core.h>

int main (int argc, char *argv[])
{
    RygelMediaServer *server;
    RygelSimpleContainer *root_container;
    char *path;
    GFile *source_dir;
    GFileEnumerator *enumerator;
    GFileInfo *info;
    int i;
    GMainLoop *loop;
    GError *error = NULL;

#if !GLIB_CHECK_VERSION(2, 36, 0)
    g_type_init ();
#endif

    rygel_media_engine_init (&error);

    if (error != NULL) {
        g_print ("Could not initialize media engine: %s\n",
                 error->message);
        g_error_free (error);

        return EXIT_FAILURE;
    }

    g_set_application_name ("Standalone-Server");

    root_container = rygel_simple_container_new_root ("Sample implementation");
    if (argc >= 2) {
        path = g_strdup (argv[1]);
    } else {
        path = g_get_current_dir ();
    }

    source_dir = g_file_new_for_commandline_arg (path);
    g_free (path);

    enumerator = g_file_enumerate_children (source_dir,
                                            G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
                                            G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
                                            G_FILE_QUERY_INFO_NONE,
                                            NULL,
                                            NULL);
    info = g_file_enumerator_next_file (enumerator, NULL, NULL);
    i = 0;
    while (info != NULL) {
        GFile *file;
        const char *display_name, *content_type;
        char *uri, *id;
        RygelMediaItem *item = NULL;
        GError *error = NULL;

        display_name = g_file_info_get_display_name (info);
        content_type = g_file_info_get_content_type (info);
        file = g_file_get_child_for_display_name (source_dir, display_name, &error);
        if (error != NULL) {
            g_critical ("Failed to get child: %s", error->message);

            return 127;
        }
        uri = g_file_get_uri (file);
        g_object_unref (file);
        id = g_strdup_printf ("%06d", i);

        if (g_str_has_prefix (content_type, "audio/")) {
            item = RYGEL_MEDIA_ITEM (rygel_audio_item_new (id,
                                         RYGEL_MEDIA_CONTAINER (root_container),
                                         display_name,
                                         RYGEL_AUDIO_ITEM_UPNP_CLASS));
        } else if (g_str_has_prefix (content_type, "video/")) {
            item = RYGEL_MEDIA_ITEM (rygel_video_item_new (id,
                                         RYGEL_MEDIA_CONTAINER (root_container),
                                         display_name,
                                         RYGEL_VIDEO_ITEM_UPNP_CLASS));
        } else if (g_str_has_prefix (content_type, "image/")) {
            item = RYGEL_MEDIA_ITEM (rygel_image_item_new (id,
                                         RYGEL_MEDIA_CONTAINER (root_container),
                                         display_name,
                                         RYGEL_IMAGE_ITEM_UPNP_CLASS));
        }
        g_free (id);

        if (item != NULL) {
            rygel_media_file_item_set_mime_type (RYGEL_MEDIA_FILE_ITEM (item),
                                                 content_type);

            rygel_media_object_add_uri (RYGEL_MEDIA_OBJECT (item), uri);

            rygel_simple_container_add_child_item (root_container, item);
            rygel_media_file_item_add_engine_resources (RYGEL_MEDIA_FILE_ITEM (item),
                                                        NULL,
                                                        NULL);
        }

        i++;
        info = g_file_enumerator_next_file (enumerator, NULL, NULL);
    }

    server = rygel_media_server_new ("LibRygel sample server",
                                     RYGEL_MEDIA_CONTAINER (root_container),
                                     RYGEL_PLUGIN_CAPABILITIES_NONE);
    rygel_media_device_add_interface (RYGEL_MEDIA_DEVICE (server), "eth0");
    rygel_media_device_add_interface (RYGEL_MEDIA_DEVICE (server), "wlan0");

    loop = g_main_loop_new (NULL, FALSE);
    g_main_loop_run (loop);
}