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