Initialize gstreamer.
[profile/ivi/rygel.git] / src / rygel / rygel-main.vala
1 /*
2  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
3  * Copyright (C) 2008 Zeeshan Ali (Khattak) <zeeshanak@gnome.org>.
4  *
5  * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Library General Public
22  * License as published by the Free Software Foundation; either
23  * version 2 of the License, or (at your option) any later version.
24  */
25
26 using GUPnP;
27 using GConf;
28 using CStuff;
29
30 public class Rygel.Main : Object {
31     private PluginLoader plugin_loader;
32     private MediaServerFactory ms_factory;
33     private List<MediaServer> media_servers;
34
35     private MainLoop main_loop;
36
37     private int exit_code;
38
39     public Main () throws GLib.Error {
40         this.media_servers = new List<MediaServer> ();
41         this.plugin_loader = new PluginLoader ();
42         this.ms_factory = new MediaServerFactory ();
43         this.main_loop = new GLib.MainLoop (null, false);
44
45         this.exit_code = 0;
46
47         this.plugin_loader.plugin_available += this.on_plugin_loaded;
48
49         Utils.on_application_exit (this.application_exit_cb);
50     }
51
52     public int run () {
53         this.plugin_loader.load_plugins ();
54
55         this.main_loop.run ();
56
57         return this.exit_code;
58     }
59
60     public void exit (int exit_code) {
61         this.exit_code = exit_code;
62         this.main_loop.quit ();
63     }
64
65     private void application_exit_cb () {
66         this.exit (0);
67     }
68
69     private void on_plugin_loaded (PluginLoader plugin_loader,
70                                    Plugin       plugin) {
71         try {
72             var server = this.ms_factory.create_media_server (plugin);
73
74             /* Make our device available */
75             server.available = true;
76
77             media_servers.append (server);
78         } catch (GLib.Error error) {
79             warning ("Failed to create MediaServer for %s. Reason: %s\n",
80                      plugin.name,
81                      error.message);
82         }
83     }
84
85     public static int main (string[] args) {
86         Main main;
87
88         // initialize gstreamer
89         Gst.init (ref args);
90
91         try {
92             main = new Main ();
93         } catch (GLib.Error err) {
94             error ("%s", err.message);
95
96             return -1;
97         }
98
99         int exit_code = main.run ();
100
101         return exit_code;
102     }
103 }
104