2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
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.
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.
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.
20 #include <sys/ioctl.h>
22 #include "rtsp-server.h"
23 #include "rtsp-client.h"
25 #define DEFAULT_ADDRESS "0.0.0.0"
26 /* #define DEFAULT_ADDRESS "::0" */
27 #define DEFAULT_SERVICE "8554"
28 #define DEFAULT_BACKLOG 5
30 /* Define to use the SO_LINGER option so that the server sockets can be resused
31 * sooner. Disabled for now because it is not very well implemented by various
32 * OSes and it causes clients to fail to read the TEARDOWN response. */
47 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
49 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
50 #define GST_CAT_DEFAULT rtsp_server_debug
52 static void gst_rtsp_server_get_property (GObject * object, guint propid,
53 GValue * value, GParamSpec * pspec);
54 static void gst_rtsp_server_set_property (GObject * object, guint propid,
55 const GValue * value, GParamSpec * pspec);
56 static void gst_rtsp_server_finalize (GObject * object);
58 static GstRTSPClient *default_create_client (GstRTSPServer * server);
59 static gboolean default_accept_client (GstRTSPServer * server,
60 GstRTSPClient * client, GIOChannel * channel);
63 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
65 GObjectClass *gobject_class;
67 gobject_class = G_OBJECT_CLASS (klass);
69 gobject_class->get_property = gst_rtsp_server_get_property;
70 gobject_class->set_property = gst_rtsp_server_set_property;
71 gobject_class->finalize = gst_rtsp_server_finalize;
74 * GstRTSPServer::address
76 * The address of the server. This is the address where the server will
79 g_object_class_install_property (gobject_class, PROP_ADDRESS,
80 g_param_spec_string ("address", "Address",
81 "The address the server uses to listen on", DEFAULT_ADDRESS,
82 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
84 * GstRTSPServer::service
86 * The service of the server. This is either a string with the service name or
87 * a port number (as a string) the server will listen on.
89 g_object_class_install_property (gobject_class, PROP_SERVICE,
90 g_param_spec_string ("service", "Service",
91 "The service or port number the server uses to listen on",
92 DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
94 * GstRTSPServer::backlog
96 * The backlog argument defines the maximum length to which the queue of
97 * pending connections for the server may grow. If a connection request arrives
98 * when the queue is full, the client may receive an error with an indication of
99 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
100 * request may be ignored so that a later reattempt at connection succeeds.
102 g_object_class_install_property (gobject_class, PROP_BACKLOG,
103 g_param_spec_int ("backlog", "Backlog",
104 "The maximum length to which the queue "
105 "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
106 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
108 * GstRTSPServer::session-pool
110 * The session pool of the server. By default each server has a separate
111 * session pool but sessions can be shared between servers by setting the same
112 * session pool on multiple servers.
114 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
115 g_param_spec_object ("session-pool", "Session Pool",
116 "The session pool to use for client session",
117 GST_TYPE_RTSP_SESSION_POOL,
118 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
120 * GstRTSPServer::media-mapping
122 * The media mapping to use for this server. By default the server has no
123 * media mapping and thus cannot map urls to media streams.
125 g_object_class_install_property (gobject_class, PROP_MEDIA_MAPPING,
126 g_param_spec_object ("media-mapping", "Media Mapping",
127 "The media mapping to use for client session",
128 GST_TYPE_RTSP_MEDIA_MAPPING,
129 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
131 klass->create_client = default_create_client;
132 klass->accept_client = default_accept_client;
134 GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
138 gst_rtsp_server_init (GstRTSPServer * server)
140 server->lock = g_mutex_new ();
141 server->address = g_strdup (DEFAULT_ADDRESS);
142 server->service = g_strdup (DEFAULT_SERVICE);
143 server->backlog = DEFAULT_BACKLOG;
144 server->session_pool = gst_rtsp_session_pool_new ();
145 server->media_mapping = gst_rtsp_media_mapping_new ();
149 gst_rtsp_server_finalize (GObject * object)
151 GstRTSPServer *server = GST_RTSP_SERVER (object);
153 g_mutex_free (server->lock);
154 g_free (server->address);
155 g_free (server->service);
157 g_object_unref (server->session_pool);
158 g_object_unref (server->media_mapping);
161 g_object_unref (server->auth);
165 * gst_rtsp_server_new:
167 * Create a new #GstRTSPServer instance.
170 gst_rtsp_server_new (void)
172 GstRTSPServer *result;
174 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
180 * gst_rtsp_server_set_address:
181 * @server: a #GstRTSPServer
182 * @address: the address
184 * Configure @server to accept connections on the given address.
186 * This function must be called before the server is bound.
189 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
191 g_return_if_fail (GST_IS_RTSP_SERVER (server));
192 g_return_if_fail (address != NULL);
194 g_free (server->address);
195 server->address = g_strdup (address);
199 * gst_rtsp_server_get_address:
200 * @server: a #GstRTSPServer
202 * Get the address on which the server will accept connections.
204 * Returns: the server address. g_free() after usage.
207 gst_rtsp_server_get_address (GstRTSPServer * server)
209 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
211 return g_strdup (server->address);
215 * gst_rtsp_server_set_service:
216 * @server: a #GstRTSPServer
217 * @service: the service
219 * Configure @server to accept connections on the given service.
220 * @service should be a string containing the service name (see services(5)) or
221 * a string containing a port number between 1 and 65535.
223 * This function must be called before the server is bound.
226 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
228 g_return_if_fail (GST_IS_RTSP_SERVER (server));
229 g_return_if_fail (service != NULL);
231 g_free (server->service);
232 server->service = g_strdup (service);
236 * gst_rtsp_server_get_service:
237 * @server: a #GstRTSPServer
239 * Get the service on which the server will accept connections.
241 * Returns: the service. use g_free() after usage.
244 gst_rtsp_server_get_service (GstRTSPServer * server)
246 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
248 return g_strdup (server->service);
252 * gst_rtsp_server_set_backlog:
253 * @server: a #GstRTSPServer
254 * @backlog: the backlog
256 * configure the maximum amount of requests that may be queued for the
259 * This function must be called before the server is bound.
262 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
264 g_return_if_fail (GST_IS_RTSP_SERVER (server));
266 server->backlog = backlog;
270 * gst_rtsp_server_get_backlog:
271 * @server: a #GstRTSPServer
273 * The maximum amount of queued requests for the server.
275 * Returns: the server backlog.
278 gst_rtsp_server_get_backlog (GstRTSPServer * server)
280 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
282 return server->backlog;
286 * gst_rtsp_server_set_session_pool:
287 * @server: a #GstRTSPServer
288 * @pool: a #GstRTSPSessionPool
290 * configure @pool to be used as the session pool of @server.
293 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
294 GstRTSPSessionPool * pool)
296 GstRTSPSessionPool *old;
298 g_return_if_fail (GST_IS_RTSP_SERVER (server));
300 old = server->session_pool;
305 server->session_pool = pool;
307 g_object_unref (old);
312 * gst_rtsp_server_get_session_pool:
313 * @server: a #GstRTSPServer
315 * Get the #GstRTSPSessionPool used as the session pool of @server.
317 * Returns: the #GstRTSPSessionPool used for sessions. g_object_unref() after
321 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
323 GstRTSPSessionPool *result;
325 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
327 if ((result = server->session_pool))
328 g_object_ref (result);
334 * gst_rtsp_server_set_media_mapping:
335 * @server: a #GstRTSPServer
336 * @mapping: a #GstRTSPMediaMapping
338 * configure @mapping to be used as the media mapping of @server.
341 gst_rtsp_server_set_media_mapping (GstRTSPServer * server,
342 GstRTSPMediaMapping * mapping)
344 GstRTSPMediaMapping *old;
346 g_return_if_fail (GST_IS_RTSP_SERVER (server));
348 old = server->media_mapping;
350 if (old != mapping) {
352 g_object_ref (mapping);
353 server->media_mapping = mapping;
355 g_object_unref (old);
361 * gst_rtsp_server_get_media_mapping:
362 * @server: a #GstRTSPServer
364 * Get the #GstRTSPMediaMapping used as the media mapping of @server.
366 * Returns: the #GstRTSPMediaMapping of @server. g_object_unref() after
369 GstRTSPMediaMapping *
370 gst_rtsp_server_get_media_mapping (GstRTSPServer * server)
372 GstRTSPMediaMapping *result;
374 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
376 if ((result = server->media_mapping))
377 g_object_ref (result);
383 * gst_rtsp_server_set_auth:
384 * @server: a #GstRTSPServer
385 * @auth: a #GstRTSPAuth
387 * configure @auth to be used as the authentication manager of @server.
390 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
394 g_return_if_fail (GST_IS_RTSP_SERVER (server));
403 g_object_unref (old);
409 * gst_rtsp_server_get_auth:
410 * @server: a #GstRTSPServer
412 * Get the #GstRTSPAuth used as the authentication manager of @server.
414 * Returns: the #GstRTSPAuth of @server. g_object_unref() after
418 gst_rtsp_server_get_auth (GstRTSPServer * server)
422 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
424 if ((result = server->auth))
425 g_object_ref (result);
431 gst_rtsp_server_get_property (GObject * object, guint propid,
432 GValue * value, GParamSpec * pspec)
434 GstRTSPServer *server = GST_RTSP_SERVER (object);
438 g_value_take_string (value, gst_rtsp_server_get_address (server));
441 g_value_take_string (value, gst_rtsp_server_get_service (server));
444 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
446 case PROP_SESSION_POOL:
447 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
449 case PROP_MEDIA_MAPPING:
450 g_value_take_object (value, gst_rtsp_server_get_media_mapping (server));
453 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
458 gst_rtsp_server_set_property (GObject * object, guint propid,
459 const GValue * value, GParamSpec * pspec)
461 GstRTSPServer *server = GST_RTSP_SERVER (object);
465 gst_rtsp_server_set_address (server, g_value_get_string (value));
468 gst_rtsp_server_set_service (server, g_value_get_string (value));
471 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
473 case PROP_SESSION_POOL:
474 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
476 case PROP_MEDIA_MAPPING:
477 gst_rtsp_server_set_media_mapping (server, g_value_get_object (value));
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
484 /* Prepare a server socket for @server and make it listen on the configured port */
486 gst_rtsp_server_sink_init_send (GstRTSPServer * server)
489 struct addrinfo hints;
490 struct addrinfo *result, *rp;
492 struct linger linger;
495 memset (&hints, 0, sizeof (struct addrinfo));
496 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
497 hints.ai_socktype = SOCK_STREAM; /* stream socket */
498 hints.ai_flags = AI_PASSIVE | AI_CANONNAME; /* For wildcard IP address */
499 hints.ai_protocol = 0; /* Any protocol */
500 hints.ai_canonname = NULL;
501 hints.ai_addr = NULL;
502 hints.ai_next = NULL;
504 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
507 /* resolve the server IP address */
509 getaddrinfo (server->address, server->service, &hints, &result)) != 0)
512 /* create server socket, we loop through all the addresses until we manage to
513 * create a socket and bind. */
514 for (rp = result; rp; rp = rp->ai_next) {
515 sockfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
517 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
522 /* make address reusable */
524 if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR,
525 (void *) &ret, sizeof (ret)) < 0) {
526 /* warn but try to bind anyway */
527 GST_WARNING_OBJECT (server, "failed to reuse socker (%s)",
531 if (bind (sockfd, rp->ai_addr, rp->ai_addrlen) == 0) {
532 GST_DEBUG_OBJECT (server, "bind on %s", rp->ai_canonname);
536 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
540 freeaddrinfo (result);
545 server->server_sock.fd = sockfd;
547 GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d",
548 server->server_sock.fd);
550 /* keep connection alive; avoids SIGPIPE during write */
552 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
553 (void *) &ret, sizeof (ret)) < 0)
554 goto keepalive_failed;
557 /* make sure socket is reset 5 seconds after close. This ensure that we can
558 * reuse the socket quickly while still having a chance to send data to the
562 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_LINGER,
563 (void *) &linger, sizeof (linger)) < 0)
567 /* set the server socket to nonblocking */
568 fcntl (server->server_sock.fd, F_SETFL, O_NONBLOCK);
570 GST_DEBUG_OBJECT (server, "listening on server socket %d with queue of %d",
571 server->server_sock.fd, server->backlog);
572 if (listen (server->server_sock.fd, server->backlog) == -1)
575 GST_DEBUG_OBJECT (server,
576 "listened on server socket %d, returning from connection setup",
577 server->server_sock.fd);
579 GST_INFO_OBJECT (server, "listening on service %s", server->service);
586 GST_ERROR_OBJECT (server, "failed to resolve address: %s",
592 GST_ERROR_OBJECT (server, "failed to create socket: %s",
598 GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s",
605 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
612 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
618 if (server->server_sock.fd >= 0) {
619 close (server->server_sock.fd);
620 server->server_sock.fd = -1;
626 /* add the client to the active list of clients, takes ownership of
629 manage_client (GstRTSPServer * server, GstRTSPClient * client)
631 gst_rtsp_client_set_server (client, server);
633 /* can unref the client now, when the request is finished, it will be
635 gst_object_unref (client);
638 static GstRTSPClient *
639 default_create_client (GstRTSPServer * server)
641 GstRTSPClient *client;
643 /* a new client connected, create a session to handle the client. */
644 client = gst_rtsp_client_new ();
646 /* set the session pool that this client should use */
647 gst_rtsp_client_set_session_pool (client, server->session_pool);
648 /* set the media mapping that this client should use */
649 gst_rtsp_client_set_media_mapping (client, server->media_mapping);
650 /* set authentication manager */
651 gst_rtsp_client_set_auth (client, server->auth);
656 /* default method for creating a new client object in the server to accept and
657 * handle a client connection on this server */
659 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
660 GIOChannel * channel)
662 /* accept connections for that client, this function returns after accepting
663 * the connection and will run the remainder of the communication with the
664 * client asyncronously. */
665 if (!gst_rtsp_client_accept (client, channel))
673 GST_ERROR_OBJECT (server,
674 "Could not accept client on server socket %d: %s (%d)",
675 server->server_sock.fd, g_strerror (errno), errno);
681 * gst_rtsp_server_io_func:
682 * @channel: a #GIOChannel
683 * @condition: the condition on @source
685 * A default #GIOFunc that creates a new #GstRTSPClient to accept and handle a
686 * new connection on @channel or @server.
688 * Returns: TRUE if the source could be connected, FALSE if an error occured.
691 gst_rtsp_server_io_func (GIOChannel * channel, GIOCondition condition,
692 GstRTSPServer * server)
695 GstRTSPClient *client = NULL;
696 GstRTSPServerClass *klass;
698 if (condition & G_IO_IN) {
699 klass = GST_RTSP_SERVER_GET_CLASS (server);
701 if (klass->create_client)
702 client = klass->create_client (server);
706 /* a new client connected, create a client object to handle the client. */
707 if (klass->accept_client)
708 result = klass->accept_client (server, client, channel);
712 /* manage the client connection */
713 manage_client (server, client);
715 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
722 GST_ERROR_OBJECT (server, "failed to create a client");
727 GST_ERROR_OBJECT (server, "failed to accept client");
728 gst_object_unref (client);
734 * gst_rtsp_server_get_io_channel:
735 * @server: a #GstRTSPServer
737 * Create a #GIOChannel for @server.
739 * Returns: the GIOChannel for @server or NULL when an error occured.
742 gst_rtsp_server_get_io_channel (GstRTSPServer * server)
744 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
746 if (server->io_channel == NULL) {
747 if (!gst_rtsp_server_sink_init_send (server))
750 /* create IO channel for the socket */
751 server->io_channel = g_io_channel_unix_new (server->server_sock.fd);
753 return server->io_channel;
757 GST_ERROR_OBJECT (server, "failed to initialize server");
763 * gst_rtsp_server_create_watch:
764 * @server: a #GstRTSPServer
766 * Create a #GSource for @server. The new source will have a default
767 * #GIOFunc of gst_rtsp_server_io_func().
769 * Returns: the #GSource for @server or NULL when an error occured.
772 gst_rtsp_server_create_watch (GstRTSPServer * server)
774 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
776 if (server->io_watch == NULL) {
779 channel = gst_rtsp_server_get_io_channel (server);
783 /* create a watch for reads (new connections) and possible errors */
784 server->io_watch = g_io_create_watch (channel, G_IO_IN |
785 G_IO_ERR | G_IO_HUP | G_IO_NVAL);
787 /* configure the callback */
788 g_source_set_callback (server->io_watch,
789 (GSourceFunc) gst_rtsp_server_io_func, server, NULL);
791 return server->io_watch;
795 GST_ERROR_OBJECT (server, "failed to create IO channel");
801 * gst_rtsp_server_attach:
802 * @server: a #GstRTSPServer
803 * @context: a #GMainContext
805 * Attaches @server to @context. When the mainloop for @context is run, the
806 * server will be dispatched.
808 * This function should be called when the server properties and urls are fully
809 * configured and the server is ready to start.
811 * Returns: the ID (greater than 0) for the source within the GMainContext.
814 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
819 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
821 source = gst_rtsp_server_create_watch (server);
825 res = g_source_attach (source, context);
832 GST_ERROR_OBJECT (server, "failed to create watch");