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