Add an example server plugin
[profile/ivi/rygel.git] / examples / standalone-server.c
1 /*
2  * Copyright (C) 2012 Openismus GmbH.
3  * Copyright (C) 2012 Intel Corporation.
4  *
5  * Author: Jens Georg <jensg@openismus.com>
6  *
7  * This file is part of Rygel.
8  *
9  * Rygel is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Rygel is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23
24 /*
25  * Demo application for librygel-server.
26  *
27  * Creates a stand-alone UPnP server that exposes the contents of the current
28  * folder or the folder passed as argument.
29  *
30  * Usage:
31  *   standalone-server [<folder>]
32  *
33  * The server listens on wlan0 and eth0 by default.
34  */
35
36 #include <gio/gio.h>
37 #include <rygel-server.h>
38 #include <rygel-core.h>
39
40 int main (int argc, char *argv[])
41 {
42     RygelMediaServer *server;
43     RygelSimpleContainer *root_container;
44     char *path;
45     GFile *source_dir;
46     GFileEnumerator *enumerator;
47     GFileInfo *info;
48     int i;
49     GMainLoop *loop;
50     GError *error = NULL;
51
52     g_type_init ();
53
54     rygel_media_engine_init (&error);
55
56     if (error != NULL) {
57         g_print ("Could not initialize media engine: %s\n",
58                  error->message);
59         g_error_free (error);
60
61         return EXIT_FAILURE;
62     }
63
64     g_set_application_name ("Standalone-Server");
65
66     root_container = rygel_simple_container_new_root ("Sample implementation");
67     if (argc >= 2) {
68         path = g_strdup (argv[1]);
69     } else {
70         path = g_get_current_dir ();
71     }
72
73     source_dir = g_file_new_for_commandline_arg (path);
74     g_free (path);
75
76     enumerator = g_file_enumerate_children (source_dir,
77                                             G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
78                                             G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
79                                             G_FILE_QUERY_INFO_NONE,
80                                             NULL,
81                                             NULL);
82     info = g_file_enumerator_next_file (enumerator, NULL, NULL);
83     i = 0;
84     while (info != NULL) {
85         GFile *file;
86         const char *display_name, *content_type;
87         char *uri, *id;
88         RygelMediaItem *item = NULL;
89         GError *error = NULL;
90
91         display_name = g_file_info_get_display_name (info);
92         content_type = g_file_info_get_content_type (info);
93         file = g_file_get_child_for_display_name (source_dir, display_name, &error);
94         if (error != NULL) {
95             g_critical ("Failed to get child: %s", error->message);
96
97             return 127;
98         }
99         uri = g_file_get_uri (file);
100         g_object_unref (file);
101         id = g_strdup_printf ("%06d", i);
102
103         if (g_str_has_prefix (content_type, "audio/")) {
104             item = rygel_audio_item_new (id,
105                                          root_container,
106                                          display_name,
107                                          RYGEL_AUDIO_ITEM_UPNP_CLASS);
108         } else if (g_str_has_prefix (content_type, "video/")) {
109             item = rygel_video_item_new (id,
110                                          root_container,
111                                          display_name,
112                                          RYGEL_VIDEO_ITEM_UPNP_CLASS);
113         } else if (g_str_has_prefix (content_type, "image/")) {
114             item = rygel_image_item_new (id,
115                                          root_container,
116                                          display_name,
117                                          RYGEL_IMAGE_ITEM_UPNP_CLASS);
118         }
119         g_free (id);
120
121         if (item != NULL) {
122             RygelMediaObject *object;
123
124             RYGEL_MEDIA_ITEM (item)->mime_type = g_strdup (content_type);
125
126             object = RYGEL_MEDIA_OBJECT (item);
127             gee_collection_add (GEE_COLLECTION (object->uris), uri);
128             rygel_simple_container_add_child_item (root_container, item);
129         }
130
131         i++;
132         info = g_file_enumerator_next_file (enumerator, NULL, NULL);
133     }
134
135     server = rygel_media_server_new ("LibRygel sample server", root_container);
136     rygel_media_device_add_interface (RYGEL_MEDIA_DEVICE (server), "eth0");
137     rygel_media_device_add_interface (RYGEL_MEDIA_DEVICE (server), "wlan0");
138
139     loop = g_main_loop_new (NULL, FALSE);
140     g_main_loop_run (loop);
141 }