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.
26 #include <sys/types.h>
27 #include <netinet/in.h>
29 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #include <sys/ioctl.h>
35 #include "rtsp-server.h"
36 #include "rtsp-client.h"
38 #define DEFAULT_ADDRESS "0.0.0.0"
39 /* #define DEFAULT_ADDRESS "::0" */
40 #define DEFAULT_SERVICE "8554"
41 #define DEFAULT_BACKLOG 5
43 /* Define to use the SO_LINGER option so that the server sockets can be resused
44 * sooner. Disabled for now because it is not very well implemented by various
45 * OSes and it causes clients to fail to read the TEARDOWN response. */
62 SIGNAL_CLIENT_CONNECTED,
66 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
68 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
69 #define GST_CAT_DEFAULT rtsp_server_debug
71 static guint gst_rtsp_server_signals[SIGNAL_LAST] = { 0 };
73 static void gst_rtsp_server_get_property (GObject * object, guint propid,
74 GValue * value, GParamSpec * pspec);
75 static void gst_rtsp_server_set_property (GObject * object, guint propid,
76 const GValue * value, GParamSpec * pspec);
77 static void gst_rtsp_server_finalize (GObject * object);
79 static GstRTSPClient *default_create_client (GstRTSPServer * server);
80 static gboolean default_accept_client (GstRTSPServer * server,
81 GstRTSPClient * client, GIOChannel * channel);
84 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
86 GObjectClass *gobject_class;
88 gobject_class = G_OBJECT_CLASS (klass);
90 gobject_class->get_property = gst_rtsp_server_get_property;
91 gobject_class->set_property = gst_rtsp_server_set_property;
92 gobject_class->finalize = gst_rtsp_server_finalize;
95 * GstRTSPServer::address
97 * The address of the server. This is the address where the server will
100 g_object_class_install_property (gobject_class, PROP_ADDRESS,
101 g_param_spec_string ("address", "Address",
102 "The address the server uses to listen on", DEFAULT_ADDRESS,
103 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
105 * GstRTSPServer::service
107 * The service of the server. This is either a string with the service name or
108 * a port number (as a string) the server will listen on.
110 g_object_class_install_property (gobject_class, PROP_SERVICE,
111 g_param_spec_string ("service", "Service",
112 "The service or port number the server uses to listen on",
113 DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
115 * GstRTSPServer::backlog
117 * The backlog argument defines the maximum length to which the queue of
118 * pending connections for the server may grow. If a connection request arrives
119 * when the queue is full, the client may receive an error with an indication of
120 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
121 * request may be ignored so that a later reattempt at connection succeeds.
123 g_object_class_install_property (gobject_class, PROP_BACKLOG,
124 g_param_spec_int ("backlog", "Backlog",
125 "The maximum length to which the queue "
126 "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
127 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
129 * GstRTSPServer::session-pool
131 * The session pool of the server. By default each server has a separate
132 * session pool but sessions can be shared between servers by setting the same
133 * session pool on multiple servers.
135 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
136 g_param_spec_object ("session-pool", "Session Pool",
137 "The session pool to use for client session",
138 GST_TYPE_RTSP_SESSION_POOL,
139 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
141 * GstRTSPServer::media-mapping
143 * The media mapping to use for this server. By default the server has no
144 * media mapping and thus cannot map urls to media streams.
146 g_object_class_install_property (gobject_class, PROP_MEDIA_MAPPING,
147 g_param_spec_object ("media-mapping", "Media Mapping",
148 "The media mapping to use for client session",
149 GST_TYPE_RTSP_MEDIA_MAPPING,
150 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152 gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED] =
153 g_signal_new ("client-connected", G_TYPE_FROM_CLASS (gobject_class),
154 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPServerClass, client_connected),
155 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
156 gst_rtsp_client_get_type ());
158 klass->create_client = default_create_client;
159 klass->accept_client = default_accept_client;
161 GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
165 gst_rtsp_server_init (GstRTSPServer * server)
167 server->lock = g_mutex_new ();
168 server->address = g_strdup (DEFAULT_ADDRESS);
169 server->service = g_strdup (DEFAULT_SERVICE);
170 server->backlog = DEFAULT_BACKLOG;
171 server->session_pool = gst_rtsp_session_pool_new ();
172 server->media_mapping = gst_rtsp_media_mapping_new ();
176 gst_rtsp_server_finalize (GObject * object)
178 GstRTSPServer *server = GST_RTSP_SERVER (object);
180 GST_DEBUG_OBJECT (server, "finalize server");
182 g_free (server->address);
183 g_free (server->service);
185 g_object_unref (server->session_pool);
186 g_object_unref (server->media_mapping);
189 g_object_unref (server->auth);
191 g_mutex_free (server->lock);
193 G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
197 * gst_rtsp_server_new:
199 * Create a new #GstRTSPServer instance.
202 gst_rtsp_server_new (void)
204 GstRTSPServer *result;
206 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
212 * gst_rtsp_server_set_address:
213 * @server: a #GstRTSPServer
214 * @address: the address
216 * Configure @server to accept connections on the given address.
218 * This function must be called before the server is bound.
221 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
223 g_return_if_fail (GST_IS_RTSP_SERVER (server));
224 g_return_if_fail (address != NULL);
226 GST_RTSP_SERVER_LOCK (server);
227 g_free (server->address);
228 server->address = g_strdup (address);
229 GST_RTSP_SERVER_UNLOCK (server);
233 * gst_rtsp_server_get_address:
234 * @server: a #GstRTSPServer
236 * Get the address on which the server will accept connections.
238 * Returns: the server address. g_free() after usage.
241 gst_rtsp_server_get_address (GstRTSPServer * server)
244 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
246 GST_RTSP_SERVER_LOCK (server);
247 result = g_strdup (server->address);
248 GST_RTSP_SERVER_UNLOCK (server);
254 * gst_rtsp_server_set_service:
255 * @server: a #GstRTSPServer
256 * @service: the service
258 * Configure @server to accept connections on the given service.
259 * @service should be a string containing the service name (see services(5)) or
260 * a string containing a port number between 1 and 65535.
262 * This function must be called before the server is bound.
265 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
267 g_return_if_fail (GST_IS_RTSP_SERVER (server));
268 g_return_if_fail (service != NULL);
270 GST_RTSP_SERVER_LOCK (server);
271 g_free (server->service);
272 server->service = g_strdup (service);
273 GST_RTSP_SERVER_UNLOCK (server);
277 * gst_rtsp_server_get_service:
278 * @server: a #GstRTSPServer
280 * Get the service on which the server will accept connections.
282 * Returns: the service. use g_free() after usage.
285 gst_rtsp_server_get_service (GstRTSPServer * server)
289 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
291 GST_RTSP_SERVER_LOCK (server);
292 result = g_strdup (server->service);
293 GST_RTSP_SERVER_UNLOCK (server);
299 * gst_rtsp_server_set_backlog:
300 * @server: a #GstRTSPServer
301 * @backlog: the backlog
303 * configure the maximum amount of requests that may be queued for the
306 * This function must be called before the server is bound.
309 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
311 g_return_if_fail (GST_IS_RTSP_SERVER (server));
313 GST_RTSP_SERVER_LOCK (server);
314 server->backlog = backlog;
315 GST_RTSP_SERVER_UNLOCK (server);
319 * gst_rtsp_server_get_backlog:
320 * @server: a #GstRTSPServer
322 * The maximum amount of queued requests for the server.
324 * Returns: the server backlog.
327 gst_rtsp_server_get_backlog (GstRTSPServer * server)
331 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
333 GST_RTSP_SERVER_LOCK (server);
334 result = server->backlog;
335 GST_RTSP_SERVER_UNLOCK (server);
341 * gst_rtsp_server_set_session_pool:
342 * @server: a #GstRTSPServer
343 * @pool: a #GstRTSPSessionPool
345 * configure @pool to be used as the session pool of @server.
348 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
349 GstRTSPSessionPool * pool)
351 GstRTSPSessionPool *old;
353 g_return_if_fail (GST_IS_RTSP_SERVER (server));
358 GST_RTSP_SERVER_LOCK (server);
359 old = server->session_pool;
360 server->session_pool = pool;
361 GST_RTSP_SERVER_UNLOCK (server);
364 g_object_unref (old);
368 * gst_rtsp_server_get_session_pool:
369 * @server: a #GstRTSPServer
371 * Get the #GstRTSPSessionPool used as the session pool of @server.
373 * Returns: the #GstRTSPSessionPool used for sessions. g_object_unref() after
377 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
379 GstRTSPSessionPool *result;
381 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
383 GST_RTSP_SERVER_LOCK (server);
384 if ((result = server->session_pool))
385 g_object_ref (result);
386 GST_RTSP_SERVER_UNLOCK (server);
392 * gst_rtsp_server_set_media_mapping:
393 * @server: a #GstRTSPServer
394 * @mapping: a #GstRTSPMediaMapping
396 * configure @mapping to be used as the media mapping of @server.
399 gst_rtsp_server_set_media_mapping (GstRTSPServer * server,
400 GstRTSPMediaMapping * mapping)
402 GstRTSPMediaMapping *old;
404 g_return_if_fail (GST_IS_RTSP_SERVER (server));
407 g_object_ref (mapping);
409 GST_RTSP_SERVER_LOCK (server);
410 old = server->media_mapping;
411 server->media_mapping = mapping;
412 GST_RTSP_SERVER_UNLOCK (server);
415 g_object_unref (old);
420 * gst_rtsp_server_get_media_mapping:
421 * @server: a #GstRTSPServer
423 * Get the #GstRTSPMediaMapping used as the media mapping of @server.
425 * Returns: the #GstRTSPMediaMapping of @server. g_object_unref() after
428 GstRTSPMediaMapping *
429 gst_rtsp_server_get_media_mapping (GstRTSPServer * server)
431 GstRTSPMediaMapping *result;
433 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
435 GST_RTSP_SERVER_LOCK (server);
436 if ((result = server->media_mapping))
437 g_object_ref (result);
438 GST_RTSP_SERVER_UNLOCK (server);
444 * gst_rtsp_server_set_auth:
445 * @server: a #GstRTSPServer
446 * @auth: a #GstRTSPAuth
448 * configure @auth to be used as the authentication manager of @server.
451 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
455 g_return_if_fail (GST_IS_RTSP_SERVER (server));
460 GST_RTSP_SERVER_LOCK (server);
463 GST_RTSP_SERVER_UNLOCK (server);
466 g_object_unref (old);
471 * gst_rtsp_server_get_auth:
472 * @server: a #GstRTSPServer
474 * Get the #GstRTSPAuth used as the authentication manager of @server.
476 * Returns: the #GstRTSPAuth of @server. g_object_unref() after
480 gst_rtsp_server_get_auth (GstRTSPServer * server)
484 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
486 GST_RTSP_SERVER_LOCK (server);
487 if ((result = server->auth))
488 g_object_ref (result);
489 GST_RTSP_SERVER_UNLOCK (server);
495 gst_rtsp_server_get_property (GObject * object, guint propid,
496 GValue * value, GParamSpec * pspec)
498 GstRTSPServer *server = GST_RTSP_SERVER (object);
502 g_value_take_string (value, gst_rtsp_server_get_address (server));
505 g_value_take_string (value, gst_rtsp_server_get_service (server));
508 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
510 case PROP_SESSION_POOL:
511 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
513 case PROP_MEDIA_MAPPING:
514 g_value_take_object (value, gst_rtsp_server_get_media_mapping (server));
517 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
522 gst_rtsp_server_set_property (GObject * object, guint propid,
523 const GValue * value, GParamSpec * pspec)
525 GstRTSPServer *server = GST_RTSP_SERVER (object);
529 gst_rtsp_server_set_address (server, g_value_get_string (value));
532 gst_rtsp_server_set_service (server, g_value_get_string (value));
535 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
537 case PROP_SESSION_POOL:
538 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
540 case PROP_MEDIA_MAPPING:
541 gst_rtsp_server_set_media_mapping (server, g_value_get_object (value));
544 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
549 * gst_rtsp_server_get_io_channel:
550 * @server: a #GstRTSPServer
552 * Create a #GIOChannel for @server. The io channel will listen on the
553 * configured service.
555 * Returns: the GIOChannel for @server or NULL when an error occured.
558 gst_rtsp_server_get_io_channel (GstRTSPServer * server)
561 int ret, sockfd = -1;
562 struct addrinfo hints;
563 struct addrinfo *result, *rp;
565 struct linger linger;
568 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
570 memset (&hints, 0, sizeof (struct addrinfo));
571 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
572 hints.ai_socktype = SOCK_STREAM; /* stream socket */
573 hints.ai_flags = AI_PASSIVE | AI_CANONNAME; /* For wildcard IP address */
574 hints.ai_protocol = 0; /* Any protocol */
575 hints.ai_canonname = NULL;
576 hints.ai_addr = NULL;
577 hints.ai_next = NULL;
579 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
582 GST_RTSP_SERVER_LOCK (server);
583 /* resolve the server IP address */
585 getaddrinfo (server->address, server->service, &hints, &result)) != 0)
588 /* create server socket, we loop through all the addresses until we manage to
589 * create a socket and bind. */
590 for (rp = result; rp; rp = rp->ai_next) {
591 sockfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
593 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
598 /* make address reusable */
600 if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR,
601 (void *) &ret, sizeof (ret)) < 0) {
602 /* warn but try to bind anyway */
603 GST_WARNING_OBJECT (server, "failed to reuse socker (%s)",
607 if (bind (sockfd, rp->ai_addr, rp->ai_addrlen) == 0) {
608 GST_DEBUG_OBJECT (server, "bind on %s", rp->ai_canonname);
612 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
617 freeaddrinfo (result);
622 GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d", sockfd);
624 /* keep connection alive; avoids SIGPIPE during write */
626 if (setsockopt (sockfd, SOL_SOCKET, SO_KEEPALIVE,
627 (void *) &ret, sizeof (ret)) < 0)
628 goto keepalive_failed;
631 /* make sure socket is reset 5 seconds after close. This ensure that we can
632 * reuse the socket quickly while still having a chance to send data to the
636 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
637 (void *) &linger, sizeof (linger)) < 0)
641 /* set the server socket to nonblocking */
642 fcntl (sockfd, F_SETFL, O_NONBLOCK);
644 GST_DEBUG_OBJECT (server, "listening on server socket %d with queue of %d",
645 sockfd, server->backlog);
646 if (listen (sockfd, server->backlog) == -1)
649 GST_DEBUG_OBJECT (server,
650 "listened on server socket %d, returning from connection setup", sockfd);
652 /* create IO channel for the socket */
653 channel = g_io_channel_unix_new (sockfd);
654 g_io_channel_set_close_on_unref (channel, TRUE);
656 GST_INFO_OBJECT (server, "listening on service %s", server->service);
657 GST_RTSP_SERVER_UNLOCK (server);
664 GST_ERROR_OBJECT (server, "failed to resolve address: %s",
670 GST_ERROR_OBJECT (server, "failed to create socket: %s",
676 GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s",
683 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
690 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
699 GST_RTSP_SERVER_UNLOCK (server);
705 unmanage_client (GstRTSPClient * client, GstRTSPServer * server)
707 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
709 g_object_ref (server);
710 gst_rtsp_client_set_server (client, NULL);
712 GST_RTSP_SERVER_LOCK (server);
713 server->clients = g_list_remove (server->clients, client);
714 GST_RTSP_SERVER_UNLOCK (server);
715 g_object_unref (server);
717 g_object_unref (client);
720 /* add the client to the active list of clients, takes ownership of
723 manage_client (GstRTSPServer * server, GstRTSPClient * client)
725 GST_DEBUG_OBJECT (server, "manage client %p", client);
726 gst_rtsp_client_set_server (client, server);
728 GST_RTSP_SERVER_LOCK (server);
729 g_signal_connect (client, "closed", (GCallback) unmanage_client, server);
730 server->clients = g_list_prepend (server->clients, client);
731 GST_RTSP_SERVER_UNLOCK (server);
734 static GstRTSPClient *
735 default_create_client (GstRTSPServer * server)
737 GstRTSPClient *client;
739 /* a new client connected, create a session to handle the client. */
740 client = gst_rtsp_client_new ();
742 /* set the session pool that this client should use */
743 GST_RTSP_SERVER_LOCK (server);
744 gst_rtsp_client_set_session_pool (client, server->session_pool);
745 /* set the media mapping that this client should use */
746 gst_rtsp_client_set_media_mapping (client, server->media_mapping);
747 /* set authentication manager */
748 gst_rtsp_client_set_auth (client, server->auth);
749 GST_RTSP_SERVER_UNLOCK (server);
754 /* default method for creating a new client object in the server to accept and
755 * handle a client connection on this server */
757 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
758 GIOChannel * channel)
760 /* accept connections for that client, this function returns after accepting
761 * the connection and will run the remainder of the communication with the
762 * client asyncronously. */
763 if (!gst_rtsp_client_accept (client, channel))
771 GST_ERROR_OBJECT (server,
772 "Could not accept client on server : %s (%d)", g_strerror (errno),
779 * gst_rtsp_server_io_func:
780 * @channel: a #GIOChannel
781 * @condition: the condition on @source
783 * A default #GIOFunc that creates a new #GstRTSPClient to accept and handle a
784 * new connection on @channel or @server.
786 * Returns: TRUE if the source could be connected, FALSE if an error occured.
789 gst_rtsp_server_io_func (GIOChannel * channel, GIOCondition condition,
790 GstRTSPServer * server)
793 GstRTSPClient *client = NULL;
794 GstRTSPServerClass *klass;
796 if (condition & G_IO_IN) {
797 klass = GST_RTSP_SERVER_GET_CLASS (server);
799 if (klass->create_client)
800 client = klass->create_client (server);
804 /* a new client connected, create a client object to handle the client. */
805 if (klass->accept_client)
806 result = klass->accept_client (server, client, channel);
810 /* manage the client connection */
811 manage_client (server, client);
813 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
816 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
823 GST_ERROR_OBJECT (server, "failed to create a client");
828 GST_ERROR_OBJECT (server, "failed to accept client");
829 gst_object_unref (client);
835 watch_destroyed (GstRTSPServer * server)
837 GST_DEBUG_OBJECT (server, "source destroyed");
838 g_object_unref (server);
842 * gst_rtsp_server_create_watch:
843 * @server: a #GstRTSPServer
845 * Create a #GSource for @server. The new source will have a default
846 * #GIOFunc of gst_rtsp_server_io_func().
848 * Returns: the #GSource for @server or NULL when an error occured.
851 gst_rtsp_server_create_watch (GstRTSPServer * server)
856 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
858 channel = gst_rtsp_server_get_io_channel (server);
862 /* create a watch for reads (new connections) and possible errors */
863 source = g_io_create_watch (channel, G_IO_IN |
864 G_IO_ERR | G_IO_HUP | G_IO_NVAL);
865 g_io_channel_unref (channel);
867 /* configure the callback */
868 g_source_set_callback (source,
869 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
870 (GDestroyNotify) watch_destroyed);
876 GST_ERROR_OBJECT (server, "failed to create IO channel");
882 * gst_rtsp_server_attach:
883 * @server: a #GstRTSPServer
884 * @context: a #GMainContext
886 * Attaches @server to @context. When the mainloop for @context is run, the
887 * server will be dispatched. When @context is NULL, the default context will be
890 * This function should be called when the server properties and urls are fully
891 * configured and the server is ready to start.
893 * Returns: the ID (greater than 0) for the source within the GMainContext.
896 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
901 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
903 source = gst_rtsp_server_create_watch (server);
907 res = g_source_attach (source, context);
908 g_source_unref (source);
915 GST_ERROR_OBJECT (server, "failed to create watch");