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);
162 * gst_rtsp_server_new:
164 * Create a new #GstRTSPServer instance.
167 gst_rtsp_server_new (void)
169 GstRTSPServer *result;
171 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
177 * gst_rtsp_server_set_address:
178 * @server: a #GstRTSPServer
179 * @address: the address
181 * Configure @server to accept connections on the given address.
183 * This function must be called before the server is bound.
186 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
188 g_return_if_fail (GST_IS_RTSP_SERVER (server));
189 g_return_if_fail (address != NULL);
191 g_free (server->address);
192 server->address = g_strdup (address);
196 * gst_rtsp_server_get_address:
197 * @server: a #GstRTSPServer
199 * Get the address on which the server will accept connections.
201 * Returns: the server address. g_free() after usage.
204 gst_rtsp_server_get_address (GstRTSPServer * server)
206 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
208 return g_strdup (server->address);
212 * gst_rtsp_server_set_service:
213 * @server: a #GstRTSPServer
214 * @service: the service
216 * Configure @server to accept connections on the given service.
217 * @service should be a string containing the service name (see services(5)) or
218 * a string containing a port number between 1 and 65535.
220 * This function must be called before the server is bound.
223 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
225 g_return_if_fail (GST_IS_RTSP_SERVER (server));
226 g_return_if_fail (service != NULL);
228 g_free (server->service);
229 server->service = g_strdup (service);
233 * gst_rtsp_server_get_service:
234 * @server: a #GstRTSPServer
236 * Get the service on which the server will accept connections.
238 * Returns: the service. use g_free() after usage.
241 gst_rtsp_server_get_service (GstRTSPServer * server)
243 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
245 return g_strdup (server->service);
249 * gst_rtsp_server_set_backlog:
250 * @server: a #GstRTSPServer
251 * @backlog: the backlog
253 * configure the maximum amount of requests that may be queued for the
256 * This function must be called before the server is bound.
259 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
261 g_return_if_fail (GST_IS_RTSP_SERVER (server));
263 server->backlog = backlog;
267 * gst_rtsp_server_get_backlog:
268 * @server: a #GstRTSPServer
270 * The maximum amount of queued requests for the server.
272 * Returns: the server backlog.
275 gst_rtsp_server_get_backlog (GstRTSPServer * server)
277 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
279 return server->backlog;
283 * gst_rtsp_server_set_session_pool:
284 * @server: a #GstRTSPServer
285 * @pool: a #GstRTSPSessionPool
287 * configure @pool to be used as the session pool of @server.
290 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
291 GstRTSPSessionPool * pool)
293 GstRTSPSessionPool *old;
295 g_return_if_fail (GST_IS_RTSP_SERVER (server));
297 old = server->session_pool;
302 server->session_pool = pool;
304 g_object_unref (old);
309 * gst_rtsp_server_get_session_pool:
310 * @server: a #GstRTSPServer
312 * Get the #GstRTSPSessionPool used as the session pool of @server.
314 * Returns: the #GstRTSPSessionPool used for sessions. g_object_unref() after
318 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
320 GstRTSPSessionPool *result;
322 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
324 if ((result = server->session_pool))
325 g_object_ref (result);
331 * gst_rtsp_server_set_media_mapping:
332 * @server: a #GstRTSPServer
333 * @mapping: a #GstRTSPMediaMapping
335 * configure @mapping to be used as the media mapping of @server.
338 gst_rtsp_server_set_media_mapping (GstRTSPServer * server,
339 GstRTSPMediaMapping * mapping)
341 GstRTSPMediaMapping *old;
343 g_return_if_fail (GST_IS_RTSP_SERVER (server));
345 old = server->media_mapping;
347 if (old != mapping) {
349 g_object_ref (mapping);
350 server->media_mapping = mapping;
352 g_object_unref (old);
358 * gst_rtsp_server_get_media_mapping:
359 * @server: a #GstRTSPServer
361 * Get the #GstRTSPMediaMapping used as the media mapping of @server.
363 * Returns: the #GstRTSPMediaMapping of @server. g_object_unref() after
366 GstRTSPMediaMapping *
367 gst_rtsp_server_get_media_mapping (GstRTSPServer * server)
369 GstRTSPMediaMapping *result;
371 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
373 if ((result = server->media_mapping))
374 g_object_ref (result);
380 * gst_rtsp_server_set_auth:
381 * @server: a #GstRTSPServer
382 * @auth: a #GstRTSPAuth
384 * configure @auth to be used as the authentication manager of @server.
387 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
391 g_return_if_fail (GST_IS_RTSP_SERVER (server));
400 g_object_unref (old);
406 * gst_rtsp_server_get_auth:
407 * @server: a #GstRTSPServer
409 * Get the #GstRTSPAuth used as the authentication manager of @server.
411 * Returns: the #GstRTSPAuth of @server. g_object_unref() after
415 gst_rtsp_server_get_auth (GstRTSPServer * server)
419 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
421 if ((result = server->auth))
422 g_object_ref (result);
428 gst_rtsp_server_get_property (GObject * object, guint propid,
429 GValue * value, GParamSpec * pspec)
431 GstRTSPServer *server = GST_RTSP_SERVER (object);
435 g_value_take_string (value, gst_rtsp_server_get_address (server));
438 g_value_take_string (value, gst_rtsp_server_get_service (server));
441 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
443 case PROP_SESSION_POOL:
444 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
446 case PROP_MEDIA_MAPPING:
447 g_value_take_object (value, gst_rtsp_server_get_media_mapping (server));
450 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
455 gst_rtsp_server_set_property (GObject * object, guint propid,
456 const GValue * value, GParamSpec * pspec)
458 GstRTSPServer *server = GST_RTSP_SERVER (object);
462 gst_rtsp_server_set_address (server, g_value_get_string (value));
465 gst_rtsp_server_set_service (server, g_value_get_string (value));
468 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
470 case PROP_SESSION_POOL:
471 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
473 case PROP_MEDIA_MAPPING:
474 gst_rtsp_server_set_media_mapping (server, g_value_get_object (value));
477 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
481 /* Prepare a server socket for @server and make it listen on the configured port */
483 gst_rtsp_server_sink_init_send (GstRTSPServer * server)
486 struct addrinfo hints;
487 struct addrinfo *result, *rp;
489 struct linger linger;
492 memset (&hints, 0, sizeof (struct addrinfo));
493 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
494 hints.ai_socktype = SOCK_STREAM; /* stream socket */
495 hints.ai_flags = AI_PASSIVE | AI_CANONNAME; /* For wildcard IP address */
496 hints.ai_protocol = 0; /* Any protocol */
497 hints.ai_canonname = NULL;
498 hints.ai_addr = NULL;
499 hints.ai_next = NULL;
501 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
504 /* resolve the server IP address */
506 getaddrinfo (server->address, server->service, &hints, &result)) != 0)
509 /* create server socket, we loop through all the addresses until we manage to
510 * create a socket and bind. */
511 for (rp = result; rp; rp = rp->ai_next) {
512 sockfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
514 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
519 /* make address reusable */
521 if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR,
522 (void *) &ret, sizeof (ret)) < 0) {
523 /* warn but try to bind anyway */
524 GST_WARNING_OBJECT (server, "failed to reuse socker (%s)",
528 if (bind (sockfd, rp->ai_addr, rp->ai_addrlen) == 0) {
529 GST_DEBUG_OBJECT (server, "bind on %s", rp->ai_canonname);
533 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
537 freeaddrinfo (result);
542 server->server_sock.fd = sockfd;
544 GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d",
545 server->server_sock.fd);
547 /* keep connection alive; avoids SIGPIPE during write */
549 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
550 (void *) &ret, sizeof (ret)) < 0)
551 goto keepalive_failed;
554 /* make sure socket is reset 5 seconds after close. This ensure that we can
555 * reuse the socket quickly while still having a chance to send data to the
559 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_LINGER,
560 (void *) &linger, sizeof (linger)) < 0)
564 /* set the server socket to nonblocking */
565 fcntl (server->server_sock.fd, F_SETFL, O_NONBLOCK);
567 GST_DEBUG_OBJECT (server, "listening on server socket %d with queue of %d",
568 server->server_sock.fd, server->backlog);
569 if (listen (server->server_sock.fd, server->backlog) == -1)
572 GST_DEBUG_OBJECT (server,
573 "listened on server socket %d, returning from connection setup",
574 server->server_sock.fd);
576 GST_INFO_OBJECT (server, "listening on service %s", server->service);
583 GST_ERROR_OBJECT (server, "failed to resolve address: %s",
589 GST_ERROR_OBJECT (server, "failed to create socket: %s",
595 GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s",
602 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
609 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
615 if (server->server_sock.fd >= 0) {
616 close (server->server_sock.fd);
617 server->server_sock.fd = -1;
623 /* add the client to the active list of clients, takes ownership of
626 manage_client (GstRTSPServer * server, GstRTSPClient * client)
628 gst_rtsp_client_set_server (client, server);
630 /* can unref the client now, when the request is finished, it will be
632 gst_object_unref (client);
635 static GstRTSPClient *
636 default_create_client (GstRTSPServer * server)
638 GstRTSPClient *client;
640 /* a new client connected, create a session to handle the client. */
641 client = gst_rtsp_client_new ();
643 /* set the session pool that this client should use */
644 gst_rtsp_client_set_session_pool (client, server->session_pool);
645 /* set the media mapping that this client should use */
646 gst_rtsp_client_set_media_mapping (client, server->media_mapping);
647 /* set authentication manager */
648 gst_rtsp_client_set_auth (client, server->auth);
653 /* default method for creating a new client object in the server to accept and
654 * handle a client connection on this server */
656 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
657 GIOChannel * channel)
659 /* accept connections for that client, this function returns after accepting
660 * the connection and will run the remainder of the communication with the
661 * client asyncronously. */
662 if (!gst_rtsp_client_accept (client, channel))
670 GST_ERROR_OBJECT (server,
671 "Could not accept client on server socket %d: %s (%d)",
672 server->server_sock.fd, g_strerror (errno), errno);
678 * gst_rtsp_server_io_func:
679 * @channel: a #GIOChannel
680 * @condition: the condition on @source
682 * A default #GIOFunc that creates a new #GstRTSPClient to accept and handle a
683 * new connection on @channel or @server.
685 * Returns: TRUE if the source could be connected, FALSE if an error occured.
688 gst_rtsp_server_io_func (GIOChannel * channel, GIOCondition condition,
689 GstRTSPServer * server)
692 GstRTSPClient *client = NULL;
693 GstRTSPServerClass *klass;
695 if (condition & G_IO_IN) {
696 klass = GST_RTSP_SERVER_GET_CLASS (server);
698 if (klass->create_client)
699 client = klass->create_client (server);
703 /* a new client connected, create a client object to handle the client. */
704 if (klass->accept_client)
705 result = klass->accept_client (server, client, channel);
709 /* manage the client connection */
710 manage_client (server, client);
712 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
719 GST_ERROR_OBJECT (server, "failed to create a client");
724 GST_ERROR_OBJECT (server, "failed to accept client");
725 gst_object_unref (client);
731 * gst_rtsp_server_get_io_channel:
732 * @server: a #GstRTSPServer
734 * Create a #GIOChannel for @server.
736 * Returns: the GIOChannel for @server or NULL when an error occured.
739 gst_rtsp_server_get_io_channel (GstRTSPServer * server)
741 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
743 if (server->io_channel == NULL) {
744 if (!gst_rtsp_server_sink_init_send (server))
747 /* create IO channel for the socket */
748 server->io_channel = g_io_channel_unix_new (server->server_sock.fd);
750 return server->io_channel;
754 GST_ERROR_OBJECT (server, "failed to initialize server");
760 * gst_rtsp_server_create_watch:
761 * @server: a #GstRTSPServer
763 * Create a #GSource for @server. The new source will have a default
764 * #GIOFunc of gst_rtsp_server_io_func().
766 * Returns: the #GSource for @server or NULL when an error occured.
769 gst_rtsp_server_create_watch (GstRTSPServer * server)
771 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
773 if (server->io_watch == NULL) {
776 channel = gst_rtsp_server_get_io_channel (server);
780 /* create a watch for reads (new connections) and possible errors */
781 server->io_watch = g_io_create_watch (channel, G_IO_IN |
782 G_IO_ERR | G_IO_HUP | G_IO_NVAL);
784 /* configure the callback */
785 g_source_set_callback (server->io_watch,
786 (GSourceFunc) gst_rtsp_server_io_func, server, NULL);
788 return server->io_watch;
792 GST_ERROR_OBJECT (server, "failed to create IO channel");
798 * gst_rtsp_server_attach:
799 * @server: a #GstRTSPServer
800 * @context: a #GMainContext
802 * Attaches @server to @context. When the mainloop for @context is run, the
803 * server will be dispatched.
805 * This function should be called when the server properties and urls are fully
806 * configured and the server is ready to start.
808 * Returns: the ID (greater than 0) for the source within the GMainContext.
811 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
816 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
818 source = gst_rtsp_server_create_watch (server);
822 res = g_source_attach (source, context);
829 GST_ERROR_OBJECT (server, "failed to create watch");