Make more properties configurable in the server.
[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 DEFAULT_BACKLOG         5
26 #define DEFAULT_PORT            1554
27
28 enum
29 {
30   PROP_0,
31   PROP_BACKLOG,
32   PROP_PORT,
33   PROP_POOL,
34   PROP_LAST
35 };
36
37 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
38
39 static void gst_rtsp_server_get_property (GObject *object, guint propid,
40     GValue *value, GParamSpec *pspec);
41 static void gst_rtsp_server_set_property (GObject *object, guint propid,
42     const GValue *value, GParamSpec *pspec);
43
44 static void
45 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
46
47   GObjectClass *gobject_class;
48
49   gobject_class = G_OBJECT_CLASS (klass);
50   
51   gobject_class->get_property = gst_rtsp_server_get_property;
52   gobject_class->set_property = gst_rtsp_server_set_property;
53   
54   /**
55    * GstRTSPServer::backlog
56    *
57    * The backlog argument defines the maximum length to which the queue of
58    * pending connections for the server may grow. If a connection request arrives
59    * when the queue is full, the client may receive an error with an indication of
60    * ECONNREFUSED or, if the underlying protocol supports retransmission, the
61    * request may be ignored so that a later reattempt at  connection succeeds.
62    */
63   g_object_class_install_property (gobject_class, PROP_BACKLOG,
64       g_param_spec_int ("backlog", "Backlog", "The maximum length to which the queue "
65               "of pending connections may grow",
66           0, G_MAXINT, DEFAULT_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
67   /**
68    * GstRTSPServer::port
69    *
70    * The session port of the server. This is the port where the server will
71    * listen on.
72    */
73   g_object_class_install_property (gobject_class, PROP_PORT,
74       g_param_spec_int ("port", "Port", "The port the server uses to listen on",
75           1, 65535, DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
76   /**
77    * GstRTSPServer::pool
78    *
79    * The session pool of the server. By default each server has a separate
80    * session pool but sessions can be shared between servers by setting the same
81    * session pool on multiple servers.
82    */
83   g_object_class_install_property (gobject_class, PROP_POOL,
84       g_param_spec_object ("pool", "Pool", "The session pool to use for client session",
85           GST_TYPE_RTSP_SESSION_POOL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
86 }
87
88 static void
89 gst_rtsp_server_init (GstRTSPServer * server)
90 {
91   server->server_port = DEFAULT_PORT;
92   server->backlog = DEFAULT_BACKLOG;
93   server->pool = gst_rtsp_session_pool_new ();
94 }
95
96 /**
97  * gst_rtsp_server_new:
98  *
99  * Create a new #GstRTSPServer instance.
100  */
101 GstRTSPServer *
102 gst_rtsp_server_new (void)
103 {
104   GstRTSPServer *result;
105
106   result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
107
108   return result;
109 }
110
111 /**
112  * gst_rtsp_server_set_port:
113  * @server: a #GstRTSPServer
114  * @port: the port
115  *
116  * Configure @server to accept connections on the given port.
117  * @port should be a port number between 1 and 65535.
118  *
119  * This function must be called before the server is bound.
120  */
121 void
122 gst_rtsp_server_set_port (GstRTSPServer *server, gint port)
123 {
124   g_return_if_fail (GST_IS_RTSP_SERVER (server));
125   g_return_if_fail (port >= 1 && port <= 65535);
126
127   server->server_port = port;
128 }
129
130 /**
131  * gst_rtsp_server_get_port:
132  * @server: a #GstRTSPServer
133  *
134  * Get the port number on which the server will accept connections.
135  *
136  * Returns: the server port.
137  */
138 gint
139 gst_rtsp_server_get_port (GstRTSPServer *server)
140 {
141   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
142
143   return server->server_port;
144 }
145
146 /**
147  * gst_rtsp_server_set_backlog:
148  * @server: a #GstRTSPServer
149  * @backlog: the backlog
150  *
151  * configure the maximum amount of requests that may be queued for the
152  * server.
153  *
154  * This function must be called before the server is bound.
155  */
156 void
157 gst_rtsp_server_set_backlog (GstRTSPServer *server, gint backlog)
158 {
159   g_return_if_fail (GST_IS_RTSP_SERVER (server));
160
161   server->backlog = backlog;
162 }
163
164 /**
165  * gst_rtsp_server_get_backlog:
166  * @server: a #GstRTSPServer
167  *
168  * The maximum amount of queued requests for the server.
169  *
170  * Returns: the server backlog.
171  */
172 gint
173 gst_rtsp_server_get_backlog (GstRTSPServer *server)
174 {
175   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
176
177   return server->backlog;
178 }
179
180 /**
181  * gst_rtsp_server_set_session_pool:
182  * @server: a #GstRTSPServer
183  * @pool: a #GstRTSPSessionPool
184  *
185  * configure @pool to be used as the session pool of @server.
186  */
187 void
188 gst_rtsp_server_set_session_pool (GstRTSPServer *server, GstRTSPSessionPool *pool)
189 {
190   g_return_if_fail (GST_IS_RTSP_SERVER (server));
191
192   if (server->pool)
193     g_object_unref (server->pool);
194   if (pool)
195     pool = g_object_ref (pool);
196   server->pool = pool;
197 }
198
199
200 /**
201  * gst_rtsp_server_get_session_pool:
202  * @server: a #GstRTSPServer
203  *
204  * Get the #GstRTSPSessionPool used as the session pool of @server.
205  *
206  * Returns: the #GstRTSPSessionPool used for sessions. g_object_unref() after
207  * usage.
208  */
209 GstRTSPSessionPool *
210 gst_rtsp_server_get_session_pool (GstRTSPServer *server)
211 {
212   GstRTSPSessionPool *result;
213
214   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
215
216   if ((result = server->pool))
217     g_object_ref (result);
218
219   return result;
220 }
221
222 static void
223 gst_rtsp_server_get_property (GObject *object, guint propid,
224     GValue *value, GParamSpec *pspec)
225 {
226   GstRTSPServer *server = GST_RTSP_SERVER (object);
227
228   switch (propid) {
229     case PROP_PORT:
230       g_value_set_int (value, gst_rtsp_server_get_port (server));
231       break;
232     case PROP_BACKLOG:
233       g_value_set_int (value, gst_rtsp_server_get_backlog (server));
234       break;
235     case PROP_POOL:
236       g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
237       break;
238     default:
239       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
240   }
241 }
242
243 static void
244 gst_rtsp_server_set_property (GObject *object, guint propid,
245     const GValue *value, GParamSpec *pspec)
246 {
247   GstRTSPServer *server = GST_RTSP_SERVER (object);
248
249   switch (propid) {
250     case PROP_PORT:
251       gst_rtsp_server_set_port (server, g_value_get_int (value));
252       break;
253     case PROP_BACKLOG:
254       gst_rtsp_server_set_backlog (server, g_value_get_int (value));
255       break;
256     case PROP_POOL:
257       gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
258       break;
259     default:
260       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
261   }
262 }
263
264 /* Prepare a server socket for @server and make it listen on the configured port */
265 static gboolean
266 gst_rtsp_server_sink_init_send (GstRTSPServer * server)
267 {
268   int ret;
269
270   /* create server socket */
271   if ((server->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
272     goto no_socket;
273
274   GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d",
275       server->server_sock.fd);
276
277   /* make address reusable */
278   ret = 1;
279   if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
280           (void *) &ret, sizeof (ret)) < 0)
281     goto reuse_failed;
282
283   /* keep connection alive; avoids SIGPIPE during write */
284   ret = 1;
285   if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
286           (void *) &ret, sizeof (ret)) < 0)
287     goto keepalive_failed;
288
289   /* name the socket */
290   memset (&server->server_sin, 0, sizeof (server->server_sin));
291   server->server_sin.sin_family = AF_INET;        /* network socket */
292   server->server_sin.sin_port = htons (server->server_port);        /* on port */
293   server->server_sin.sin_addr.s_addr = htonl (INADDR_ANY);        /* for hosts */
294
295   /* bind it */
296   GST_DEBUG_OBJECT (server, "binding server socket to address");
297   ret = bind (server->server_sock.fd, (struct sockaddr *) &server->server_sin,
298       sizeof (server->server_sin));
299   if (ret)
300     goto bind_failed;
301
302   /* set the server socket to nonblocking */
303   fcntl (server->server_sock.fd, F_SETFL, O_NONBLOCK);
304
305   GST_DEBUG_OBJECT (server, "listening on server socket %d with queue of %d",
306       server->server_sock.fd, server->backlog);
307   if (listen (server->server_sock.fd, server->backlog) == -1)
308     goto listen_failed;
309
310   GST_DEBUG_OBJECT (server,
311       "listened on server socket %d, returning from connection setup",
312       server->server_sock.fd);
313
314   return TRUE;
315
316   /* ERRORS */
317 no_socket:
318   {
319     GST_ERROR_OBJECT (server, "failed to create socket: %s", g_strerror (errno));
320     return FALSE;
321   }
322 reuse_failed:
323   {
324     if (server->server_sock.fd >= 0) {
325       close (server->server_sock.fd);
326       server->server_sock.fd = -1;
327     }
328     GST_ERROR_OBJECT (server, "failed to reuse socket: %s", g_strerror (errno));
329     return FALSE;
330   }
331 keepalive_failed:
332   {
333     if (server->server_sock.fd >= 0) {
334       close (server->server_sock.fd);
335       server->server_sock.fd = -1;
336     }
337     GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s", g_strerror (errno));
338     return FALSE;
339   }
340 listen_failed:
341   {
342     if (server->server_sock.fd >= 0) {
343       close (server->server_sock.fd);
344       server->server_sock.fd = -1;
345     }
346     GST_ERROR_OBJECT (server, "failed to listen on socket: %s", g_strerror (errno));
347     return FALSE;
348   }
349 bind_failed:
350   {
351     if (server->server_sock.fd >= 0) {
352       close (server->server_sock.fd);
353       server->server_sock.fd = -1;
354     }
355     GST_ERROR_OBJECT (server, "failed to bind on socket: %s", g_strerror (errno));
356     return FALSE;
357   }
358 }
359
360 /**
361  * gst_rtsp_server_io_func:
362  * @channel: a #GIOChannel
363  * @condition: the condition on @source
364  *
365  * A default #GIOFunc that creates a new #GstRTSPClient to accept and handle a
366  * new connection on @channel or @server.
367  *
368  * Returns: TRUE if the source could be connected, FALSE if an error occured.
369  */
370 gboolean
371 gst_rtsp_server_io_func (GIOChannel *channel, GIOCondition condition, GstRTSPServer *server)
372 {
373   GstRTSPClient *client;
374
375   if (condition & G_IO_IN) {
376     /* a new client connected, create a session to handle the client. */
377     client = gst_rtsp_client_new ();
378
379     /* set the session pool that this client should use */
380     gst_rtsp_client_set_session_pool (client, server->pool);
381
382     /* accept connections for that client, this function returns after accepting
383      * the connection and will run the remainder of the communication with the
384      * client asyncronously. */
385     if (!gst_rtsp_client_accept (client, channel))
386       goto accept_failed;
387
388     /* can unref the client now, when the request is finished, it will be
389      * unreffed async. */
390     gst_object_unref (client);
391   }
392   else {
393     g_print ("received unknown event %08x", condition);
394   }
395   return TRUE;
396
397   /* ERRORS */
398 accept_failed:
399   {
400     g_error ("Could not accept client on server socket %d: %s (%d)",
401             server->server_sock.fd, g_strerror (errno), errno);
402     gst_object_unref (client);
403     return FALSE;
404   }
405 }
406
407 /**
408  * gst_rtsp_server_get_io_channel:
409  * @server: a #GstRTSPServer
410  *
411  * Create a #GIOChannel for @server.
412  *
413  * Returns: the GIOChannel for @server or NULL when an error occured.
414  */
415 GIOChannel *
416 gst_rtsp_server_get_io_channel (GstRTSPServer *server)
417 {
418   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
419
420   if (server->io_channel == NULL) {
421     if (!gst_rtsp_server_sink_init_send (server))
422       goto init_failed;
423
424     /* create IO channel for the socket */
425     server->io_channel = g_io_channel_unix_new (server->server_sock.fd);
426   }
427   return server->io_channel;
428
429 init_failed:
430   {
431     return NULL;
432   }
433 }
434
435 /**
436  * gst_rtsp_server_create_watch:
437  * @server: a #GstRTSPServer
438  *
439  * Create a #GSource for @server. The new source will have a default
440  * #GIOFunc of gst_rtsp_server_io_func().
441  *
442  * Returns: the #GSource for @server or NULL when an error occured.
443  */
444 GSource *
445 gst_rtsp_server_create_watch (GstRTSPServer *server)
446 {
447   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
448
449   if (server->io_watch == NULL) {
450     GIOChannel *channel;
451
452     channel = gst_rtsp_server_get_io_channel (server);
453     if (channel == NULL)
454       goto no_channel;
455      
456     /* create a watch for reads (new connections) and possible errors */
457     server->io_watch = g_io_create_watch (channel, G_IO_IN |
458                   G_IO_ERR | G_IO_HUP | G_IO_NVAL);
459
460     /* configure the callback */
461     g_source_set_callback (server->io_watch, (GSourceFunc) gst_rtsp_server_io_func, server, NULL);
462   }
463   return server->io_watch;
464
465 no_channel:
466   {
467     return NULL;
468   }
469 }
470
471 /**
472  * gst_rtsp_server_attach:
473  * @server: a #GstRTSPServer
474  * @context: a #GMainContext
475  *
476  * Attaches @server to @context. When the mainloop for @context is run, the
477  * server will be dispatched.
478  *
479  * This function should be called when the server properties and urls are fully
480  * configured and the server is ready to start.
481  *
482  * Returns: the ID (greater than 0) for the source within the GMainContext. 
483  */
484 guint
485 gst_rtsp_server_attach (GstRTSPServer *server, GMainContext *context)
486 {
487   guint res;
488   GSource *source;
489
490   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
491
492   source = gst_rtsp_server_create_watch (server);
493   if (source == NULL)
494     goto no_source;
495
496   res = g_source_attach (source, context);
497
498   return res;
499
500   /* ERRORS */
501 no_source:
502   {
503     return 0;
504   }
505 }