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. */
60 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
62 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
63 #define GST_CAT_DEFAULT rtsp_server_debug
65 static void gst_rtsp_server_get_property (GObject * object, guint propid,
66 GValue * value, GParamSpec * pspec);
67 static void gst_rtsp_server_set_property (GObject * object, guint propid,
68 const GValue * value, GParamSpec * pspec);
69 static void gst_rtsp_server_finalize (GObject * object);
71 static GstRTSPClient *default_create_client (GstRTSPServer * server);
72 static gboolean default_accept_client (GstRTSPServer * server,
73 GstRTSPClient * client, GIOChannel * channel);
76 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
78 GObjectClass *gobject_class;
80 gobject_class = G_OBJECT_CLASS (klass);
82 gobject_class->get_property = gst_rtsp_server_get_property;
83 gobject_class->set_property = gst_rtsp_server_set_property;
84 gobject_class->finalize = gst_rtsp_server_finalize;
87 * GstRTSPServer::address
89 * The address of the server. This is the address where the server will
92 g_object_class_install_property (gobject_class, PROP_ADDRESS,
93 g_param_spec_string ("address", "Address",
94 "The address the server uses to listen on", DEFAULT_ADDRESS,
95 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
97 * GstRTSPServer::service
99 * The service of the server. This is either a string with the service name or
100 * a port number (as a string) the server will listen on.
102 g_object_class_install_property (gobject_class, PROP_SERVICE,
103 g_param_spec_string ("service", "Service",
104 "The service or port number the server uses to listen on",
105 DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
107 * GstRTSPServer::backlog
109 * The backlog argument defines the maximum length to which the queue of
110 * pending connections for the server may grow. If a connection request arrives
111 * when the queue is full, the client may receive an error with an indication of
112 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
113 * request may be ignored so that a later reattempt at connection succeeds.
115 g_object_class_install_property (gobject_class, PROP_BACKLOG,
116 g_param_spec_int ("backlog", "Backlog",
117 "The maximum length to which the queue "
118 "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
119 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
121 * GstRTSPServer::session-pool
123 * The session pool of the server. By default each server has a separate
124 * session pool but sessions can be shared between servers by setting the same
125 * session pool on multiple servers.
127 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
128 g_param_spec_object ("session-pool", "Session Pool",
129 "The session pool to use for client session",
130 GST_TYPE_RTSP_SESSION_POOL,
131 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
133 * GstRTSPServer::media-mapping
135 * The media mapping to use for this server. By default the server has no
136 * media mapping and thus cannot map urls to media streams.
138 g_object_class_install_property (gobject_class, PROP_MEDIA_MAPPING,
139 g_param_spec_object ("media-mapping", "Media Mapping",
140 "The media mapping to use for client session",
141 GST_TYPE_RTSP_MEDIA_MAPPING,
142 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
144 klass->create_client = default_create_client;
145 klass->accept_client = default_accept_client;
147 GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
151 gst_rtsp_server_init (GstRTSPServer * server)
153 server->lock = g_mutex_new ();
154 server->address = g_strdup (DEFAULT_ADDRESS);
155 server->service = g_strdup (DEFAULT_SERVICE);
156 server->backlog = DEFAULT_BACKLOG;
157 server->session_pool = gst_rtsp_session_pool_new ();
158 server->media_mapping = gst_rtsp_media_mapping_new ();
162 gst_rtsp_server_finalize (GObject * object)
164 GstRTSPServer *server = GST_RTSP_SERVER (object);
166 GST_DEBUG_OBJECT (server, "finalize server");
168 g_free (server->address);
169 g_free (server->service);
171 g_object_unref (server->session_pool);
172 g_object_unref (server->media_mapping);
175 g_object_unref (server->auth);
177 g_mutex_free (server->lock);
179 G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
183 * gst_rtsp_server_new:
185 * Create a new #GstRTSPServer instance.
188 gst_rtsp_server_new (void)
190 GstRTSPServer *result;
192 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
198 * gst_rtsp_server_set_address:
199 * @server: a #GstRTSPServer
200 * @address: the address
202 * Configure @server to accept connections on the given address.
204 * This function must be called before the server is bound.
207 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
209 g_return_if_fail (GST_IS_RTSP_SERVER (server));
210 g_return_if_fail (address != NULL);
212 GST_RTSP_SERVER_LOCK (server);
213 g_free (server->address);
214 server->address = g_strdup (address);
215 GST_RTSP_SERVER_UNLOCK (server);
219 * gst_rtsp_server_get_address:
220 * @server: a #GstRTSPServer
222 * Get the address on which the server will accept connections.
224 * Returns: the server address. g_free() after usage.
227 gst_rtsp_server_get_address (GstRTSPServer * server)
230 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
232 GST_RTSP_SERVER_LOCK (server);
233 result = g_strdup (server->address);
234 GST_RTSP_SERVER_UNLOCK (server);
240 * gst_rtsp_server_set_service:
241 * @server: a #GstRTSPServer
242 * @service: the service
244 * Configure @server to accept connections on the given service.
245 * @service should be a string containing the service name (see services(5)) or
246 * a string containing a port number between 1 and 65535.
248 * This function must be called before the server is bound.
251 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
253 g_return_if_fail (GST_IS_RTSP_SERVER (server));
254 g_return_if_fail (service != NULL);
256 GST_RTSP_SERVER_LOCK (server);
257 g_free (server->service);
258 server->service = g_strdup (service);
259 GST_RTSP_SERVER_UNLOCK (server);
263 * gst_rtsp_server_get_service:
264 * @server: a #GstRTSPServer
266 * Get the service on which the server will accept connections.
268 * Returns: the service. use g_free() after usage.
271 gst_rtsp_server_get_service (GstRTSPServer * server)
275 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
277 GST_RTSP_SERVER_LOCK (server);
278 result = g_strdup (server->service);
279 GST_RTSP_SERVER_UNLOCK (server);
285 * gst_rtsp_server_set_backlog:
286 * @server: a #GstRTSPServer
287 * @backlog: the backlog
289 * configure the maximum amount of requests that may be queued for the
292 * This function must be called before the server is bound.
295 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
297 g_return_if_fail (GST_IS_RTSP_SERVER (server));
299 GST_RTSP_SERVER_LOCK (server);
300 server->backlog = backlog;
301 GST_RTSP_SERVER_UNLOCK (server);
305 * gst_rtsp_server_get_backlog:
306 * @server: a #GstRTSPServer
308 * The maximum amount of queued requests for the server.
310 * Returns: the server backlog.
313 gst_rtsp_server_get_backlog (GstRTSPServer * server)
317 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
319 GST_RTSP_SERVER_LOCK (server);
320 result = server->backlog;
321 GST_RTSP_SERVER_UNLOCK (server);
327 * gst_rtsp_server_set_session_pool:
328 * @server: a #GstRTSPServer
329 * @pool: a #GstRTSPSessionPool
331 * configure @pool to be used as the session pool of @server.
334 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
335 GstRTSPSessionPool * pool)
337 GstRTSPSessionPool *old;
339 g_return_if_fail (GST_IS_RTSP_SERVER (server));
344 GST_RTSP_SERVER_LOCK (server);
345 old = server->session_pool;
346 server->session_pool = pool;
347 GST_RTSP_SERVER_UNLOCK (server);
350 g_object_unref (old);
354 * gst_rtsp_server_get_session_pool:
355 * @server: a #GstRTSPServer
357 * Get the #GstRTSPSessionPool used as the session pool of @server.
359 * Returns: the #GstRTSPSessionPool used for sessions. g_object_unref() after
363 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
365 GstRTSPSessionPool *result;
367 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
369 GST_RTSP_SERVER_LOCK (server);
370 if ((result = server->session_pool))
371 g_object_ref (result);
372 GST_RTSP_SERVER_UNLOCK (server);
378 * gst_rtsp_server_set_media_mapping:
379 * @server: a #GstRTSPServer
380 * @mapping: a #GstRTSPMediaMapping
382 * configure @mapping to be used as the media mapping of @server.
385 gst_rtsp_server_set_media_mapping (GstRTSPServer * server,
386 GstRTSPMediaMapping * mapping)
388 GstRTSPMediaMapping *old;
390 g_return_if_fail (GST_IS_RTSP_SERVER (server));
393 g_object_ref (mapping);
395 GST_RTSP_SERVER_LOCK (server);
396 old = server->media_mapping;
397 server->media_mapping = mapping;
398 GST_RTSP_SERVER_UNLOCK (server);
401 g_object_unref (old);
406 * gst_rtsp_server_get_media_mapping:
407 * @server: a #GstRTSPServer
409 * Get the #GstRTSPMediaMapping used as the media mapping of @server.
411 * Returns: the #GstRTSPMediaMapping of @server. g_object_unref() after
414 GstRTSPMediaMapping *
415 gst_rtsp_server_get_media_mapping (GstRTSPServer * server)
417 GstRTSPMediaMapping *result;
419 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
421 GST_RTSP_SERVER_LOCK (server);
422 if ((result = server->media_mapping))
423 g_object_ref (result);
424 GST_RTSP_SERVER_UNLOCK (server);
430 * gst_rtsp_server_set_auth:
431 * @server: a #GstRTSPServer
432 * @auth: a #GstRTSPAuth
434 * configure @auth to be used as the authentication manager of @server.
437 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
441 g_return_if_fail (GST_IS_RTSP_SERVER (server));
446 GST_RTSP_SERVER_LOCK (server);
449 GST_RTSP_SERVER_UNLOCK (server);
452 g_object_unref (old);
457 * gst_rtsp_server_get_auth:
458 * @server: a #GstRTSPServer
460 * Get the #GstRTSPAuth used as the authentication manager of @server.
462 * Returns: the #GstRTSPAuth of @server. g_object_unref() after
466 gst_rtsp_server_get_auth (GstRTSPServer * server)
470 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
472 GST_RTSP_SERVER_LOCK (server);
473 if ((result = server->auth))
474 g_object_ref (result);
475 GST_RTSP_SERVER_UNLOCK (server);
481 gst_rtsp_server_get_property (GObject * object, guint propid,
482 GValue * value, GParamSpec * pspec)
484 GstRTSPServer *server = GST_RTSP_SERVER (object);
488 g_value_take_string (value, gst_rtsp_server_get_address (server));
491 g_value_take_string (value, gst_rtsp_server_get_service (server));
494 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
496 case PROP_SESSION_POOL:
497 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
499 case PROP_MEDIA_MAPPING:
500 g_value_take_object (value, gst_rtsp_server_get_media_mapping (server));
503 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
508 gst_rtsp_server_set_property (GObject * object, guint propid,
509 const GValue * value, GParamSpec * pspec)
511 GstRTSPServer *server = GST_RTSP_SERVER (object);
515 gst_rtsp_server_set_address (server, g_value_get_string (value));
518 gst_rtsp_server_set_service (server, g_value_get_string (value));
521 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
523 case PROP_SESSION_POOL:
524 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
526 case PROP_MEDIA_MAPPING:
527 gst_rtsp_server_set_media_mapping (server, g_value_get_object (value));
530 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
535 * gst_rtsp_server_get_io_channel:
536 * @server: a #GstRTSPServer
538 * Create a #GIOChannel for @server. The io channel will listen on the
539 * configured service.
541 * Returns: the GIOChannel for @server or NULL when an error occured.
544 gst_rtsp_server_get_io_channel (GstRTSPServer * server)
547 int ret, sockfd = -1;
548 struct addrinfo hints;
549 struct addrinfo *result, *rp;
551 struct linger linger;
554 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
556 memset (&hints, 0, sizeof (struct addrinfo));
557 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
558 hints.ai_socktype = SOCK_STREAM; /* stream socket */
559 hints.ai_flags = AI_PASSIVE | AI_CANONNAME; /* For wildcard IP address */
560 hints.ai_protocol = 0; /* Any protocol */
561 hints.ai_canonname = NULL;
562 hints.ai_addr = NULL;
563 hints.ai_next = NULL;
565 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
568 GST_RTSP_SERVER_LOCK (server);
569 /* resolve the server IP address */
571 getaddrinfo (server->address, server->service, &hints, &result)) != 0)
574 /* create server socket, we loop through all the addresses until we manage to
575 * create a socket and bind. */
576 for (rp = result; rp; rp = rp->ai_next) {
577 sockfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
579 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
584 /* make address reusable */
586 if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR,
587 (void *) &ret, sizeof (ret)) < 0) {
588 /* warn but try to bind anyway */
589 GST_WARNING_OBJECT (server, "failed to reuse socker (%s)",
593 if (bind (sockfd, rp->ai_addr, rp->ai_addrlen) == 0) {
594 GST_DEBUG_OBJECT (server, "bind on %s", rp->ai_canonname);
598 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
603 freeaddrinfo (result);
608 GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d", sockfd);
610 /* keep connection alive; avoids SIGPIPE during write */
612 if (setsockopt (sockfd, SOL_SOCKET, SO_KEEPALIVE,
613 (void *) &ret, sizeof (ret)) < 0)
614 goto keepalive_failed;
617 /* make sure socket is reset 5 seconds after close. This ensure that we can
618 * reuse the socket quickly while still having a chance to send data to the
622 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
623 (void *) &linger, sizeof (linger)) < 0)
627 /* set the server socket to nonblocking */
628 fcntl (sockfd, F_SETFL, O_NONBLOCK);
630 GST_DEBUG_OBJECT (server, "listening on server socket %d with queue of %d",
631 sockfd, server->backlog);
632 if (listen (sockfd, server->backlog) == -1)
635 GST_DEBUG_OBJECT (server,
636 "listened on server socket %d, returning from connection setup", sockfd);
638 /* create IO channel for the socket */
639 channel = g_io_channel_unix_new (sockfd);
640 g_io_channel_set_close_on_unref (channel, TRUE);
642 GST_INFO_OBJECT (server, "listening on service %s", server->service);
643 GST_RTSP_SERVER_UNLOCK (server);
650 GST_ERROR_OBJECT (server, "failed to resolve address: %s",
656 GST_ERROR_OBJECT (server, "failed to create socket: %s",
662 GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s",
669 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
676 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
685 GST_RTSP_SERVER_UNLOCK (server);
691 unmanage_client (GstRTSPClient * client, GstRTSPServer * server)
693 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
695 gst_rtsp_client_set_server (client, NULL);
697 GST_RTSP_SERVER_LOCK (server);
698 server->clients = g_list_remove (server->clients, client);
699 GST_RTSP_SERVER_UNLOCK (server);
701 g_object_unref (client);
704 /* add the client to the active list of clients, takes ownership of
707 manage_client (GstRTSPServer * server, GstRTSPClient * client)
709 GST_DEBUG_OBJECT (server, "manage client %p", client);
710 gst_rtsp_client_set_server (client, server);
712 GST_RTSP_SERVER_LOCK (server);
713 g_signal_connect (client, "closed", (GCallback) unmanage_client, server);
714 server->clients = g_list_prepend (server->clients, client);
715 GST_RTSP_SERVER_UNLOCK (server);
718 static GstRTSPClient *
719 default_create_client (GstRTSPServer * server)
721 GstRTSPClient *client;
723 /* a new client connected, create a session to handle the client. */
724 client = gst_rtsp_client_new ();
726 /* set the session pool that this client should use */
727 GST_RTSP_SERVER_LOCK (server);
728 gst_rtsp_client_set_session_pool (client, server->session_pool);
729 /* set the media mapping that this client should use */
730 gst_rtsp_client_set_media_mapping (client, server->media_mapping);
731 /* set authentication manager */
732 gst_rtsp_client_set_auth (client, server->auth);
733 GST_RTSP_SERVER_UNLOCK (server);
738 /* default method for creating a new client object in the server to accept and
739 * handle a client connection on this server */
741 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
742 GIOChannel * channel)
744 /* accept connections for that client, this function returns after accepting
745 * the connection and will run the remainder of the communication with the
746 * client asyncronously. */
747 if (!gst_rtsp_client_accept (client, channel))
755 GST_ERROR_OBJECT (server,
756 "Could not accept client on server : %s (%d)", g_strerror (errno),
763 * gst_rtsp_server_io_func:
764 * @channel: a #GIOChannel
765 * @condition: the condition on @source
767 * A default #GIOFunc that creates a new #GstRTSPClient to accept and handle a
768 * new connection on @channel or @server.
770 * Returns: TRUE if the source could be connected, FALSE if an error occured.
773 gst_rtsp_server_io_func (GIOChannel * channel, GIOCondition condition,
774 GstRTSPServer * server)
777 GstRTSPClient *client = NULL;
778 GstRTSPServerClass *klass;
780 if (condition & G_IO_IN) {
781 klass = GST_RTSP_SERVER_GET_CLASS (server);
783 if (klass->create_client)
784 client = klass->create_client (server);
788 /* a new client connected, create a client object to handle the client. */
789 if (klass->accept_client)
790 result = klass->accept_client (server, client, channel);
794 /* manage the client connection */
795 manage_client (server, client);
797 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
804 GST_ERROR_OBJECT (server, "failed to create a client");
809 GST_ERROR_OBJECT (server, "failed to accept client");
810 gst_object_unref (client);
816 watch_destroyed (GstRTSPServer * server)
818 GST_DEBUG_OBJECT (server, "source destroyed");
819 g_object_unref (server);
823 * gst_rtsp_server_create_watch:
824 * @server: a #GstRTSPServer
826 * Create a #GSource for @server. The new source will have a default
827 * #GIOFunc of gst_rtsp_server_io_func().
829 * Returns: the #GSource for @server or NULL when an error occured.
832 gst_rtsp_server_create_watch (GstRTSPServer * server)
837 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
839 channel = gst_rtsp_server_get_io_channel (server);
843 /* create a watch for reads (new connections) and possible errors */
844 source = g_io_create_watch (channel, G_IO_IN |
845 G_IO_ERR | G_IO_HUP | G_IO_NVAL);
846 g_io_channel_unref (channel);
848 /* configure the callback */
849 g_source_set_callback (source,
850 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
851 (GDestroyNotify) watch_destroyed);
857 GST_ERROR_OBJECT (server, "failed to create IO channel");
863 * gst_rtsp_server_attach:
864 * @server: a #GstRTSPServer
865 * @context: a #GMainContext
867 * Attaches @server to @context. When the mainloop for @context is run, the
868 * server will be dispatched. When @context is NULL, the default context will be
871 * This function should be called when the server properties and urls are fully
872 * configured and the server is ready to start.
874 * Returns: the ID (greater than 0) for the source within the GMainContext.
877 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
882 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
884 source = gst_rtsp_server_create_watch (server);
888 res = g_source_attach (source, context);
889 g_source_unref (source);
896 GST_ERROR_OBJECT (server, "failed to create watch");