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);
163 G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
167 * gst_rtsp_server_new:
169 * Create a new #GstRTSPServer instance.
172 gst_rtsp_server_new (void)
174 GstRTSPServer *result;
176 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
182 * gst_rtsp_server_set_address:
183 * @server: a #GstRTSPServer
184 * @address: the address
186 * Configure @server to accept connections on the given address.
188 * This function must be called before the server is bound.
191 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
193 g_return_if_fail (GST_IS_RTSP_SERVER (server));
194 g_return_if_fail (address != NULL);
196 g_free (server->address);
197 server->address = g_strdup (address);
201 * gst_rtsp_server_get_address:
202 * @server: a #GstRTSPServer
204 * Get the address on which the server will accept connections.
206 * Returns: the server address. g_free() after usage.
209 gst_rtsp_server_get_address (GstRTSPServer * server)
211 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
213 return g_strdup (server->address);
217 * gst_rtsp_server_set_service:
218 * @server: a #GstRTSPServer
219 * @service: the service
221 * Configure @server to accept connections on the given service.
222 * @service should be a string containing the service name (see services(5)) or
223 * a string containing a port number between 1 and 65535.
225 * This function must be called before the server is bound.
228 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
230 g_return_if_fail (GST_IS_RTSP_SERVER (server));
231 g_return_if_fail (service != NULL);
233 g_free (server->service);
234 server->service = g_strdup (service);
238 * gst_rtsp_server_get_service:
239 * @server: a #GstRTSPServer
241 * Get the service on which the server will accept connections.
243 * Returns: the service. use g_free() after usage.
246 gst_rtsp_server_get_service (GstRTSPServer * server)
248 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
250 return g_strdup (server->service);
254 * gst_rtsp_server_set_backlog:
255 * @server: a #GstRTSPServer
256 * @backlog: the backlog
258 * configure the maximum amount of requests that may be queued for the
261 * This function must be called before the server is bound.
264 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
266 g_return_if_fail (GST_IS_RTSP_SERVER (server));
268 server->backlog = backlog;
272 * gst_rtsp_server_get_backlog:
273 * @server: a #GstRTSPServer
275 * The maximum amount of queued requests for the server.
277 * Returns: the server backlog.
280 gst_rtsp_server_get_backlog (GstRTSPServer * server)
282 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
284 return server->backlog;
288 * gst_rtsp_server_set_session_pool:
289 * @server: a #GstRTSPServer
290 * @pool: a #GstRTSPSessionPool
292 * configure @pool to be used as the session pool of @server.
295 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
296 GstRTSPSessionPool * pool)
298 GstRTSPSessionPool *old;
300 g_return_if_fail (GST_IS_RTSP_SERVER (server));
302 old = server->session_pool;
307 server->session_pool = pool;
309 g_object_unref (old);
314 * gst_rtsp_server_get_session_pool:
315 * @server: a #GstRTSPServer
317 * Get the #GstRTSPSessionPool used as the session pool of @server.
319 * Returns: the #GstRTSPSessionPool used for sessions. g_object_unref() after
323 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
325 GstRTSPSessionPool *result;
327 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
329 if ((result = server->session_pool))
330 g_object_ref (result);
336 * gst_rtsp_server_set_media_mapping:
337 * @server: a #GstRTSPServer
338 * @mapping: a #GstRTSPMediaMapping
340 * configure @mapping to be used as the media mapping of @server.
343 gst_rtsp_server_set_media_mapping (GstRTSPServer * server,
344 GstRTSPMediaMapping * mapping)
346 GstRTSPMediaMapping *old;
348 g_return_if_fail (GST_IS_RTSP_SERVER (server));
350 old = server->media_mapping;
352 if (old != mapping) {
354 g_object_ref (mapping);
355 server->media_mapping = mapping;
357 g_object_unref (old);
363 * gst_rtsp_server_get_media_mapping:
364 * @server: a #GstRTSPServer
366 * Get the #GstRTSPMediaMapping used as the media mapping of @server.
368 * Returns: the #GstRTSPMediaMapping of @server. g_object_unref() after
371 GstRTSPMediaMapping *
372 gst_rtsp_server_get_media_mapping (GstRTSPServer * server)
374 GstRTSPMediaMapping *result;
376 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
378 if ((result = server->media_mapping))
379 g_object_ref (result);
385 * gst_rtsp_server_set_auth:
386 * @server: a #GstRTSPServer
387 * @auth: a #GstRTSPAuth
389 * configure @auth to be used as the authentication manager of @server.
392 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
396 g_return_if_fail (GST_IS_RTSP_SERVER (server));
405 g_object_unref (old);
411 * gst_rtsp_server_get_auth:
412 * @server: a #GstRTSPServer
414 * Get the #GstRTSPAuth used as the authentication manager of @server.
416 * Returns: the #GstRTSPAuth of @server. g_object_unref() after
420 gst_rtsp_server_get_auth (GstRTSPServer * server)
424 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
426 if ((result = server->auth))
427 g_object_ref (result);
433 gst_rtsp_server_get_property (GObject * object, guint propid,
434 GValue * value, GParamSpec * pspec)
436 GstRTSPServer *server = GST_RTSP_SERVER (object);
440 g_value_take_string (value, gst_rtsp_server_get_address (server));
443 g_value_take_string (value, gst_rtsp_server_get_service (server));
446 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
448 case PROP_SESSION_POOL:
449 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
451 case PROP_MEDIA_MAPPING:
452 g_value_take_object (value, gst_rtsp_server_get_media_mapping (server));
455 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
460 gst_rtsp_server_set_property (GObject * object, guint propid,
461 const GValue * value, GParamSpec * pspec)
463 GstRTSPServer *server = GST_RTSP_SERVER (object);
467 gst_rtsp_server_set_address (server, g_value_get_string (value));
470 gst_rtsp_server_set_service (server, g_value_get_string (value));
473 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
475 case PROP_SESSION_POOL:
476 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
478 case PROP_MEDIA_MAPPING:
479 gst_rtsp_server_set_media_mapping (server, g_value_get_object (value));
482 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
486 /* Prepare a server socket for @server and make it listen on the configured port */
488 gst_rtsp_server_sink_init_send (GstRTSPServer * server)
491 struct addrinfo hints;
492 struct addrinfo *result, *rp;
494 struct linger linger;
497 memset (&hints, 0, sizeof (struct addrinfo));
498 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
499 hints.ai_socktype = SOCK_STREAM; /* stream socket */
500 hints.ai_flags = AI_PASSIVE | AI_CANONNAME; /* For wildcard IP address */
501 hints.ai_protocol = 0; /* Any protocol */
502 hints.ai_canonname = NULL;
503 hints.ai_addr = NULL;
504 hints.ai_next = NULL;
506 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
509 /* resolve the server IP address */
511 getaddrinfo (server->address, server->service, &hints, &result)) != 0)
514 /* create server socket, we loop through all the addresses until we manage to
515 * create a socket and bind. */
516 for (rp = result; rp; rp = rp->ai_next) {
517 sockfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
519 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
524 /* make address reusable */
526 if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR,
527 (void *) &ret, sizeof (ret)) < 0) {
528 /* warn but try to bind anyway */
529 GST_WARNING_OBJECT (server, "failed to reuse socker (%s)",
533 if (bind (sockfd, rp->ai_addr, rp->ai_addrlen) == 0) {
534 GST_DEBUG_OBJECT (server, "bind on %s", rp->ai_canonname);
538 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
542 freeaddrinfo (result);
547 server->server_sock.fd = sockfd;
549 GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d",
550 server->server_sock.fd);
552 /* keep connection alive; avoids SIGPIPE during write */
554 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
555 (void *) &ret, sizeof (ret)) < 0)
556 goto keepalive_failed;
559 /* make sure socket is reset 5 seconds after close. This ensure that we can
560 * reuse the socket quickly while still having a chance to send data to the
564 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_LINGER,
565 (void *) &linger, sizeof (linger)) < 0)
569 /* set the server socket to nonblocking */
570 fcntl (server->server_sock.fd, F_SETFL, O_NONBLOCK);
572 GST_DEBUG_OBJECT (server, "listening on server socket %d with queue of %d",
573 server->server_sock.fd, server->backlog);
574 if (listen (server->server_sock.fd, server->backlog) == -1)
577 GST_DEBUG_OBJECT (server,
578 "listened on server socket %d, returning from connection setup",
579 server->server_sock.fd);
581 GST_INFO_OBJECT (server, "listening on service %s", server->service);
588 GST_ERROR_OBJECT (server, "failed to resolve address: %s",
594 GST_ERROR_OBJECT (server, "failed to create socket: %s",
600 GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s",
607 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
614 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
620 if (server->server_sock.fd >= 0) {
621 close (server->server_sock.fd);
622 server->server_sock.fd = -1;
629 unmanage_client (GstRTSPClient * client, GstRTSPServer * server)
631 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
633 g_mutex_lock (server->lock);
634 server->clients = g_list_remove (server->clients, client);
635 g_mutex_unlock (server->lock);
637 g_object_unref (client);
640 /* add the client to the active list of clients, takes ownership of
643 manage_client (GstRTSPServer * server, GstRTSPClient * client)
645 GST_DEBUG_OBJECT (server, "manage client %p", client);
646 gst_rtsp_client_set_server (client, server);
648 g_mutex_lock (server->lock);
649 g_signal_connect (client, "closed", (GCallback) unmanage_client, server);
650 server->clients = g_list_prepend (server->clients, client);
651 g_mutex_unlock (server->lock);
654 static GstRTSPClient *
655 default_create_client (GstRTSPServer * server)
657 GstRTSPClient *client;
659 /* a new client connected, create a session to handle the client. */
660 client = gst_rtsp_client_new ();
662 /* set the session pool that this client should use */
663 gst_rtsp_client_set_session_pool (client, server->session_pool);
664 /* set the media mapping that this client should use */
665 gst_rtsp_client_set_media_mapping (client, server->media_mapping);
666 /* set authentication manager */
667 gst_rtsp_client_set_auth (client, server->auth);
672 /* default method for creating a new client object in the server to accept and
673 * handle a client connection on this server */
675 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
676 GIOChannel * channel)
678 /* accept connections for that client, this function returns after accepting
679 * the connection and will run the remainder of the communication with the
680 * client asyncronously. */
681 if (!gst_rtsp_client_accept (client, channel))
689 GST_ERROR_OBJECT (server,
690 "Could not accept client on server socket %d: %s (%d)",
691 server->server_sock.fd, g_strerror (errno), errno);
697 * gst_rtsp_server_io_func:
698 * @channel: a #GIOChannel
699 * @condition: the condition on @source
701 * A default #GIOFunc that creates a new #GstRTSPClient to accept and handle a
702 * new connection on @channel or @server.
704 * Returns: TRUE if the source could be connected, FALSE if an error occured.
707 gst_rtsp_server_io_func (GIOChannel * channel, GIOCondition condition,
708 GstRTSPServer * server)
711 GstRTSPClient *client = NULL;
712 GstRTSPServerClass *klass;
714 if (condition & G_IO_IN) {
715 klass = GST_RTSP_SERVER_GET_CLASS (server);
717 if (klass->create_client)
718 client = klass->create_client (server);
722 /* a new client connected, create a client object to handle the client. */
723 if (klass->accept_client)
724 result = klass->accept_client (server, client, channel);
728 /* manage the client connection */
729 manage_client (server, client);
731 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
738 GST_ERROR_OBJECT (server, "failed to create a client");
743 GST_ERROR_OBJECT (server, "failed to accept client");
744 gst_object_unref (client);
750 * gst_rtsp_server_get_io_channel:
751 * @server: a #GstRTSPServer
753 * Create a #GIOChannel for @server.
755 * Returns: the GIOChannel for @server or NULL when an error occured.
758 gst_rtsp_server_get_io_channel (GstRTSPServer * server)
760 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
762 if (server->io_channel == NULL) {
763 if (!gst_rtsp_server_sink_init_send (server))
766 /* create IO channel for the socket */
767 server->io_channel = g_io_channel_unix_new (server->server_sock.fd);
769 return server->io_channel;
773 GST_ERROR_OBJECT (server, "failed to initialize server");
779 * gst_rtsp_server_create_watch:
780 * @server: a #GstRTSPServer
782 * Create a #GSource for @server. The new source will have a default
783 * #GIOFunc of gst_rtsp_server_io_func().
785 * Returns: the #GSource for @server or NULL when an error occured.
788 gst_rtsp_server_create_watch (GstRTSPServer * server)
790 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
792 if (server->io_watch == NULL) {
795 channel = gst_rtsp_server_get_io_channel (server);
799 /* create a watch for reads (new connections) and possible errors */
800 server->io_watch = g_io_create_watch (channel, G_IO_IN |
801 G_IO_ERR | G_IO_HUP | G_IO_NVAL);
803 /* configure the callback */
804 g_source_set_callback (server->io_watch,
805 (GSourceFunc) gst_rtsp_server_io_func, server, NULL);
807 return server->io_watch;
811 GST_ERROR_OBJECT (server, "failed to create IO channel");
817 * gst_rtsp_server_attach:
818 * @server: a #GstRTSPServer
819 * @context: a #GMainContext
821 * Attaches @server to @context. When the mainloop for @context is run, the
822 * server will be dispatched.
824 * This function should be called when the server properties and urls are fully
825 * configured and the server is ready to start.
827 * Returns: the ID (greater than 0) for the source within the GMainContext.
830 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
835 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
837 source = gst_rtsp_server_create_watch (server);
841 res = g_source_attach (source, context);
848 GST_ERROR_OBJECT (server, "failed to create watch");