Added port property to GstRTSPServer class.
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-server.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <sys/ioctl.h>
21
22 #include "rtsp-server.h"
23 #include "rtsp-client.h"
24
25 #define TCP_BACKLOG             5
26 #define DEFAULT_PORT            1554
27 enum
28 {
29   ARG_0,
30   PROP_PORT
31 };
32
33 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
34
35 static void gst_rtsp_server_get_property (GObject *object, guint propid,
36     GValue *value, GParamSpec *pspec);
37 static void gst_rtsp_server_set_property (GObject *object, guint propid,
38     const GValue *value, GParamSpec *pspec);
39
40 static void
41 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
42
43   GObjectClass *gobject_class;
44
45   gobject_class = G_OBJECT_CLASS (klass);
46   
47   gobject_class->get_property = gst_rtsp_server_get_property;
48   gobject_class->set_property = gst_rtsp_server_set_property;
49   
50   g_object_class_install_property (gobject_class, PROP_PORT,
51       g_param_spec_int ("port", "Port", "The port the server uses",
52           1, 65535, DEFAULT_PORT,
53           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
54 }
55
56 static void
57 gst_rtsp_server_init (GstRTSPServer * server)
58 {
59   server->server_port = DEFAULT_PORT;
60   server->pool = gst_rtsp_session_pool_new ();
61 }
62
63 /**
64  * gst_rtsp_server_new:
65  *
66  * Create a new #GstRTSPServer instance.
67  */
68 GstRTSPServer *
69 gst_rtsp_server_new (void)
70 {
71   GstRTSPServer *result;
72
73   result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
74
75   return result;
76 }
77
78 static void
79 gst_rtsp_server_get_property (GObject *object, guint propid,
80     GValue *value, GParamSpec *pspec)
81 {
82   GstRTSPServer *server = GST_RTSP_SERVER (object);
83
84   switch (propid) {
85     case PROP_PORT:
86       g_value_set_int (value, server->server_port);
87       break;
88     default:
89       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
90   }
91 }
92
93 static void
94 gst_rtsp_server_set_property (GObject *object, guint propid,
95     const GValue *value, GParamSpec *pspec)
96 {
97   GstRTSPServer *server = GST_RTSP_SERVER (object);
98
99   switch (propid) {
100     case PROP_PORT:
101       server->server_port = g_value_get_int (value);
102       break;
103     default:
104       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
105   }
106 }
107
108 static gboolean
109 gst_rtsp_server_sink_init_send (GstRTSPServer * server)
110 {
111   int ret;
112
113   /* create server socket */
114   if ((server->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
115     goto no_socket;
116
117   GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d",
118       server->server_sock.fd);
119
120   /* make address reusable */
121   ret = 1;
122   if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
123           (void *) &ret, sizeof (ret)) < 0)
124     goto reuse_failed;
125
126   /* keep connection alive; avoids SIGPIPE during write */
127   ret = 1;
128   if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
129           (void *) &ret, sizeof (ret)) < 0)
130     goto keepalive_failed;
131
132   /* name the socket */
133   memset (&server->server_sin, 0, sizeof (server->server_sin));
134   server->server_sin.sin_family = AF_INET;        /* network socket */
135   server->server_sin.sin_port = htons (server->server_port);        /* on port */
136   server->server_sin.sin_addr.s_addr = htonl (INADDR_ANY);        /* for hosts */
137
138   /* bind it */
139   GST_DEBUG_OBJECT (server, "binding server socket to address");
140   ret = bind (server->server_sock.fd, (struct sockaddr *) &server->server_sin,
141       sizeof (server->server_sin));
142   if (ret)
143     goto bind_failed;
144
145   /* set the server socket to nonblocking */
146   fcntl (server->server_sock.fd, F_SETFL, O_NONBLOCK);
147
148   GST_DEBUG_OBJECT (server, "listening on server socket %d with queue of %d",
149       server->server_sock.fd, TCP_BACKLOG);
150   if (listen (server->server_sock.fd, TCP_BACKLOG) == -1)
151     goto listen_failed;
152
153   GST_DEBUG_OBJECT (server,
154       "listened on server socket %d, returning from connection setup",
155       server->server_sock.fd);
156
157   return TRUE;
158
159   /* ERRORS */
160 no_socket:
161   {
162     GST_ERROR_OBJECT (server, "failed to create socket: %s", g_strerror (errno));
163     return FALSE;
164   }
165 reuse_failed:
166   {
167     if (server->server_sock.fd >= 0) {
168       close (server->server_sock.fd);
169       server->server_sock.fd = -1;
170     }
171     GST_ERROR_OBJECT (server, "failed to reuse socket: %s", g_strerror (errno));
172     return FALSE;
173   }
174 keepalive_failed:
175   {
176     if (server->server_sock.fd >= 0) {
177       close (server->server_sock.fd);
178       server->server_sock.fd = -1;
179     }
180     GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s", g_strerror (errno));
181     return FALSE;
182   }
183 listen_failed:
184   {
185     if (server->server_sock.fd >= 0) {
186       close (server->server_sock.fd);
187       server->server_sock.fd = -1;
188     }
189     GST_ERROR_OBJECT (server, "failed to listen on socket: %s", g_strerror (errno));
190     return FALSE;
191   }
192 bind_failed:
193   {
194     if (server->server_sock.fd >= 0) {
195       close (server->server_sock.fd);
196       server->server_sock.fd = -1;
197     }
198     GST_ERROR_OBJECT (server, "failed to bind on socket: %s", g_strerror (errno));
199     return FALSE;
200   }
201 }
202
203 /* called when an event is available on our server socket */
204 static gboolean
205 server_dispatch (GIOChannel *source, GIOCondition condition, GstRTSPServer *server)
206 {
207   if (condition & G_IO_IN) {
208     GstRTSPClient *client;
209
210     /* a new client connected, create a session to handle the client. */
211     client = gst_rtsp_client_new ();
212
213     /* set the session pool that this client should use */
214     gst_rtsp_client_set_session_pool (client, server->pool);
215
216     /* accept connections for that client, this function returns after accepting
217      * the connection and will run the remainder of the communication with the
218      * client asyncronously. */
219     if (!gst_rtsp_client_accept (client, source))
220       goto accept_failed;
221
222     /* can unref the client now, when the request is finished, it will be
223      * unreffed async. */
224     gst_object_unref (client);
225   }
226   else {
227     g_print ("received unknown event %08x", condition);
228   }
229   return TRUE;
230
231   /* ERRORS */
232 accept_failed:
233   {
234     g_error ("Could not accept client on server socket %d: %s (%d)",
235             server->server_sock.fd, g_strerror (errno), errno);
236     return FALSE;
237   }
238 }
239
240 /**
241  * gst_rtsp_server_attach:
242  * @server: a #GstRTSPServer
243  * @context: a #GMainContext
244  *
245  * Attaches @server to @context. When the mainloop for @context is run, the
246  * server will be dispatched.
247  *
248  * This function should be called when the server properties and urls are fully
249  * configured and the server is ready to start.
250  *
251  * Returns: the ID (greater than 0) for the source within the GMainContext. 
252  */
253 guint
254 gst_rtsp_server_attach (GstRTSPServer *server, GMainContext *context)
255 {
256   guint res;
257
258   if (!gst_rtsp_server_sink_init_send (server))
259     goto init_failed;
260
261   /* create IO channel for the socket */
262   server->io_channel = g_io_channel_unix_new (server->server_sock.fd);
263
264   /* create a watch for reads (new connections) and possible errors */
265   server->io_watch = g_io_create_watch (server->io_channel, G_IO_IN |
266                   G_IO_ERR | G_IO_HUP | G_IO_NVAL);
267
268   /* configure the callback */
269   g_source_set_callback (server->io_watch, (GSourceFunc) server_dispatch, server, NULL);
270
271   res = g_source_attach (server->io_watch, context);
272
273   return res;
274
275   /* ERRORS */
276 init_failed:
277   {
278     return 0;
279   }
280 }