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