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, GSocket * socket, GError ** error);
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 g_mutex_init (&server->lock);
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_clear (&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_create_socket:
550 * @server: a #GstRTSPServer
551 * @cancellable: a #GCancellable
554 * Create a #GSocket for @server. The socket will listen on the
555 * configured service.
557 * Returns: the #GSocket for @server or NULL when an error occured.
560 gst_rtsp_server_create_socket (GstRTSPServer * server,
561 GCancellable * cancellable, GError ** error)
563 GSocketConnectable *conn;
564 GSocketAddressEnumerator *enumerator;
567 struct linger linger;
569 GError *sock_error = NULL;
570 GError *bind_error = NULL;
573 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
575 GST_RTSP_SERVER_LOCK (server);
576 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
579 /* resolve the server IP address */
580 port = atoi (server->service);
582 conn = g_network_address_new (server->address, port);
584 conn = g_network_service_new (server->service, "tcp", server->address);
586 enumerator = g_socket_connectable_enumerate (conn);
587 g_object_unref (conn);
589 /* create server socket, we loop through all the addresses until we manage to
590 * create a socket and bind. */
592 GSocketAddress *sockaddr;
595 g_socket_address_enumerator_next (enumerator, cancellable, error);
597 GST_DEBUG_OBJECT (server, "no more addresses %s", (*error)->message);
601 /* only keep the first error */
602 socket = g_socket_new (g_socket_address_get_family (sockaddr),
603 G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
604 sock_error ? NULL : &sock_error);
606 if (socket == NULL) {
607 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
608 sock_error->message);
612 if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
613 g_object_unref (sockaddr);
617 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
618 bind_error->message);
619 g_object_unref (sockaddr);
620 g_object_unref (socket);
623 g_object_unref (enumerator);
628 g_clear_error (&sock_error);
629 g_clear_error (&bind_error);
631 GST_DEBUG_OBJECT (server, "opened sending server socket");
633 /* keep connection alive; avoids SIGPIPE during write */
634 g_socket_set_keepalive (socket, TRUE);
638 /* make sure socket is reset 5 seconds after close. This ensure that we can
639 * reuse the socket quickly while still having a chance to send data to the
643 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
644 (void *) &linger, sizeof (linger)) < 0)
649 /* set the server socket to nonblocking */
650 g_socket_set_blocking (socket, FALSE);
652 /* set listen backlog */
653 g_socket_set_listen_backlog (socket, server->backlog);
655 if (!g_socket_listen (socket, error))
658 GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
659 socket, server->backlog);
661 GST_RTSP_SERVER_UNLOCK (server);
668 GST_ERROR_OBJECT (server, "failed to create socket");
675 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
683 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
690 g_object_unref (socket);
694 g_propagate_error (error, sock_error);
696 g_error_free (sock_error);
700 g_propagate_error (error, bind_error);
702 g_error_free (bind_error);
704 GST_RTSP_SERVER_UNLOCK (server);
710 unmanage_client (GstRTSPClient * client, GstRTSPServer * server)
712 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
714 g_object_ref (server);
715 gst_rtsp_client_set_server (client, NULL);
717 GST_RTSP_SERVER_LOCK (server);
718 server->clients = g_list_remove (server->clients, client);
719 GST_RTSP_SERVER_UNLOCK (server);
720 g_object_unref (server);
722 g_object_unref (client);
725 /* add the client to the active list of clients, takes ownership of
728 manage_client (GstRTSPServer * server, GstRTSPClient * client)
730 GST_DEBUG_OBJECT (server, "manage client %p", client);
731 gst_rtsp_client_set_server (client, server);
733 GST_RTSP_SERVER_LOCK (server);
734 g_signal_connect (client, "closed", (GCallback) unmanage_client, server);
735 server->clients = g_list_prepend (server->clients, client);
736 GST_RTSP_SERVER_UNLOCK (server);
739 static GstRTSPClient *
740 default_create_client (GstRTSPServer * server)
742 GstRTSPClient *client;
744 /* a new client connected, create a session to handle the client. */
745 client = gst_rtsp_client_new ();
747 /* set the session pool that this client should use */
748 GST_RTSP_SERVER_LOCK (server);
749 gst_rtsp_client_set_session_pool (client, server->session_pool);
750 /* set the media mapping that this client should use */
751 gst_rtsp_client_set_media_mapping (client, server->media_mapping);
752 /* set authentication manager */
753 gst_rtsp_client_set_auth (client, server->auth);
754 GST_RTSP_SERVER_UNLOCK (server);
759 /* default method for creating a new client object in the server to accept and
760 * handle a client connection on this server */
762 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
763 GSocket * socket, GError ** error)
765 /* accept connections for that client, this function returns after accepting
766 * the connection and will run the remainder of the communication with the
767 * client asyncronously. */
768 if (!gst_rtsp_client_accept (client, socket, NULL, error))
776 GST_ERROR_OBJECT (server,
777 "Could not accept client on server : %s", (*error)->message);
783 * gst_rtsp_server_io_func:
784 * @socket: a #GSocket
785 * @condition: the condition on @source
787 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
788 * new connection on @socket or @server.
790 * Returns: TRUE if the source could be connected, FALSE if an error occured.
793 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
794 GstRTSPServer * server)
797 GstRTSPClient *client = NULL;
798 GstRTSPServerClass *klass;
799 GError *error = NULL;
801 if (condition & G_IO_IN) {
802 klass = GST_RTSP_SERVER_GET_CLASS (server);
804 if (klass->create_client)
805 client = klass->create_client (server);
809 /* a new client connected, create a client object to handle the client. */
810 if (klass->accept_client)
811 result = klass->accept_client (server, client, socket, &error);
815 /* manage the client connection */
816 manage_client (server, client);
818 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
821 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
828 GST_ERROR_OBJECT (server, "failed to create a client");
833 GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
834 g_error_free (error);
835 gst_object_unref (client);
841 watch_destroyed (GstRTSPServer * server)
843 GST_DEBUG_OBJECT (server, "source destroyed");
844 g_object_unref (server);
848 * gst_rtsp_server_create_source:
849 * @server: a #GstRTSPServer
850 * @cancellable: a #GCancellable or %NULL.
853 * Create a #GSource for @server. The new source will have a default
854 * #GSocketSourceFunc of gst_rtsp_server_io_func().
856 * @cancellable if not NULL can be used to cancel the source, which will cause
857 * the source to trigger, reporting the current condition (which is likely 0
858 * unless cancellation happened at the same time as a condition change). You can
859 * check for this in the callback using g_cancellable_is_cancelled().
861 * Returns: the #GSource for @server or NULL when an error occured. Free with
865 gst_rtsp_server_create_source (GstRTSPServer * server,
866 GCancellable * cancellable, GError ** error)
871 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
873 socket = gst_rtsp_server_create_socket (server, NULL, error);
877 /* create a watch for reads (new connections) and possible errors */
878 source = g_socket_create_source (socket, G_IO_IN |
879 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
880 g_object_unref (socket);
882 /* configure the callback */
883 g_source_set_callback (source,
884 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
885 (GDestroyNotify) watch_destroyed);
891 GST_ERROR_OBJECT (server, "failed to create socket");
897 * gst_rtsp_server_attach:
898 * @server: a #GstRTSPServer
899 * @context: a #GMainContext
902 * Attaches @server to @context. When the mainloop for @context is run, the
903 * server will be dispatched. When @context is NULL, the default context will be
906 * This function should be called when the server properties and urls are fully
907 * configured and the server is ready to start.
909 * Returns: the ID (greater than 0) for the source within the GMainContext.
912 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
916 GError *error = NULL;
918 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
920 source = gst_rtsp_server_create_source (server, NULL, &error);
924 res = g_source_attach (source, context);
925 g_source_unref (source);
932 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
933 g_error_free (error);