Debian Codegraph @

src/av-cp/main.c

raw
1 /*
2 * Copyright (C) 2007 Zeeshan Ali (Khattak) <[email protected]>
3 * Copyright (C) 2013 Jens Georg <[email protected]>
4 *
5 * Authors: Zeeshan Ali (Khattak) <[email protected]>
6 * Jens Georg <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23 #include "config.h"
24
25 #include <libgupnp/gupnp-control-point.h>
26 #include <libgupnp-av/gupnp-av.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <locale.h>
30 #include <gmodule.h>
31 #include <glib/gi18n.h>
32
33 #include "gui.h"
34 #include "renderer-combo.h"
35 #include "playlist-treeview.h"
36 #include "server-device.h"
37
38 #define MEDIA_RENDERER "urn:schemas-upnp-org:device:MediaRenderer:1"
39 #define MEDIA_SERVER "urn:schemas-upnp-org:device:MediaServer:1"
40
41 static int upnp_port = 0;
42 static char **interfaces = NULL;
43 static char *user_agent = NULL;
44 static gboolean ipv4 = TRUE;
45 static gboolean ipv6 = TRUE;
46
47 // clang-format off
48 static GOptionEntry entries[] =
49 {
50 { "port", 'p', 0, G_OPTION_ARG_INT, &upnp_port, N_("Network PORT to use for UPnP"), "PORT" },
51 { "interface", 'i', 0, G_OPTION_ARG_STRING_ARRAY, &interfaces, N_("Network interfaces to use for UPnP communication"), "INTERFACE" },
52 { "user-agent", 'u', 0, G_OPTION_ARG_STRING, &user_agent, N_("Application part of the User-Agent header to use for UPnP communication"), "USER-AGENT" },
53 { "v4", '4', 0, G_OPTION_ARG_NONE, &ipv4, N_("Use the IPv4 protocol family"), NULL },
54 { "v6", '6', 0, G_OPTION_ARG_NONE, &ipv6, N_("Use the IPv6 protocol family"), NULL },
55 { "no-v4", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &ipv4, N_("Do not use the IPv4 protocol family"), NULL },
56 { "no-v6", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &ipv6, N_("Do not use the IPv6 protocol family"), NULL },
57 { NULL }
58 };
59 // clang-format on
60
61 static GUPnPContextManager *context_manager;
62
63 void application_exit (void);
64
65 static void
66 dms_proxy_available_cb (GUPnPControlPoint *cp,
67 GUPnPDeviceProxy *proxy)
68 {
69 add_media_server (proxy);
70 }
71
72 static void
73 dmr_proxy_available_cb (GUPnPControlPoint *cp,
74 GUPnPDeviceProxy *proxy)
75 {
76 add_media_renderer (proxy);
77 }
78
79 static void
80 dms_proxy_unavailable_cb (GUPnPControlPoint *cp,
81 GUPnPDeviceProxy *proxy)
82 {
83 remove_media_server (proxy);
84 }
85
86 static void
87 dmr_proxy_unavailable_cb (GUPnPControlPoint *cp,
88 GUPnPDeviceProxy *proxy)
89 {
90 remove_media_renderer (proxy);
91 }
92
93 static void
94 on_rescan_button_clicked (GtkButton *button,
95 gpointer user_data)
96 {
97 GUPnPContextManager *cm = GUPNP_CONTEXT_MANAGER (user_data);
98
99 gupnp_context_manager_rescan_control_points (cm);
100 }
101
102 static void
103 on_context_available (GUPnPContextManager *cm,
104 GUPnPContext *context,
105 gpointer user_data)
106 {
107 GUPnPControlPoint *dms_cp;
108 GUPnPControlPoint *dmr_cp;
109
110 dms_cp = gupnp_control_point_new (context, MEDIA_SERVER);
111 dmr_cp = gupnp_control_point_new (context, MEDIA_RENDERER);
112
113 g_signal_connect (dms_cp,
114 "device-proxy-available",
115 G_CALLBACK (dms_proxy_available_cb),
116 NULL);
117 g_signal_connect (dmr_cp,
118 "device-proxy-available",
119 G_CALLBACK (dmr_proxy_available_cb),
120 NULL);
121 g_signal_connect (dms_cp,
122 "device-proxy-unavailable",
123 G_CALLBACK (dms_proxy_unavailable_cb),
124 NULL);
125 g_signal_connect (dmr_cp,
126 "device-proxy-unavailable",
127 G_CALLBACK (dmr_proxy_unavailable_cb),
128 NULL);
129
130 gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (dms_cp),
131 TRUE);
132 gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (dmr_cp),
133 TRUE);
134
135 /* Let context manager take care of the control point life cycle */
136 gupnp_context_manager_manage_control_point (context_manager, dms_cp);
137 gupnp_context_manager_manage_control_point (context_manager, dmr_cp);
138
139 /* We don't need to keep our own references to the control points */
140 g_object_unref (dms_cp);
141 g_object_unref (dmr_cp);
142 }
143
144 static gboolean
145 init_upnp (int port)
146 {
147 GUPnPContextFilter *context_filter;
148 GtkButton *button;
149 GUPnPResourceFactory *factory;
150 GSocketFamily family = G_SOCKET_FAMILY_INVALID;
151
152 factory = gupnp_resource_factory_get_default ();
153
154 /* Work-around bgo#764498 */
155
156 gupnp_resource_factory_register_resource_proxy_type (
157 factory,
158 "urn:schemas-upnp-org:device:MediaServer:1",
159 AV_CP_TYPE_MEDIA_SERVER);
160
161 gupnp_resource_factory_register_resource_proxy_type (
162 factory,
163 "urn:schemas-upnp-org:device:MediaServer:2",
164 AV_CP_TYPE_MEDIA_SERVER);
165
166 gupnp_resource_factory_register_resource_proxy_type (
167 factory,
168 "urn:schemas-upnp-org:device:MediaServer:3",
169 AV_CP_TYPE_MEDIA_SERVER);
170
171 gupnp_resource_factory_register_resource_proxy_type (
172 factory,
173 "urn:schemas-upnp-org:device:MediaServer:4",
174 AV_CP_TYPE_MEDIA_SERVER);
175
176 if (!(ipv4 && ipv6)) {
177 if (ipv4) {
178 family = G_SOCKET_FAMILY_IPV4;
179 } else if (ipv6) {
180 family = G_SOCKET_FAMILY_IPV6;
181 }
182 }
183
184 context_manager =
185 gupnp_context_manager_create_full (GSSDP_UDA_VERSION_1_0,
186 family,
187 port);
188 g_assert (context_manager != NULL);
189
190 if (interfaces != NULL) {
191 context_filter = gupnp_context_manager_get_context_filter (
192 context_manager);
193 gupnp_context_filter_add_entryv (context_filter, interfaces);
194 gupnp_context_filter_set_enabled (context_filter, TRUE);
195 }
196
197 g_signal_connect (context_manager,
198 "context-available",
199 G_CALLBACK (on_context_available),
200 NULL);
201
202 button = get_rescan_button ();
203 g_signal_connect (G_OBJECT (button),
204 "clicked",
205 G_CALLBACK (on_rescan_button_clicked),
206 context_manager);
207
208 return TRUE;
209 }
210
211 static void
212 deinit_upnp (void)
213 {
214 g_object_unref (context_manager);
215 }
216
217 G_MODULE_EXPORT
218 void
219 application_exit (void)
220 {
221 deinit_upnp ();
222 deinit_ui ();
223
224 gtk_main_quit ();
225 }
226
227 gint
228 main (gint argc,
229 gchar *argv[])
230 {
231 GError *error = NULL;
232 GOptionContext *context = NULL;
233
234 setlocale (LC_ALL, "");
235 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
236 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
237 textdomain (GETTEXT_PACKAGE);
238
239 context = g_option_context_new (_("- UPnP AV control point"));
240 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
241 g_option_context_add_group (context, gtk_get_option_group (TRUE));
242
243 if (!g_option_context_parse (context, &argc, &argv, &error)) {
244 g_print (_("Could not parse options: %s\n"), error->message);
245
246 return -4;
247 }
248
249 if (user_agent != NULL) {
250 g_set_prgname (user_agent);
251 }
252
253 if (!init_ui ()) {
254 return -2;
255 }
256
257 if (!init_upnp (upnp_port)) {
258 return -3;
259 }
260
261 gtk_main ();
262
263 g_option_context_free (context);
264
265 return 0;
266 }
267