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_accept_client (GstRTSPServer * server,
59 GIOChannel * channel);
62 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
64 GObjectClass *gobject_class;
66 gobject_class = G_OBJECT_CLASS (klass);
68 gobject_class->get_property = gst_rtsp_server_get_property;
69 gobject_class->set_property = gst_rtsp_server_set_property;
70 gobject_class->finalize = gst_rtsp_server_finalize;
73 * GstRTSPServer::address
75 * The address of the server. This is the address where the server will
78 g_object_class_install_property (gobject_class, PROP_ADDRESS,
79 g_param_spec_string ("address", "Address",
80 "The address the server uses to listen on", DEFAULT_ADDRESS,
81 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
83 * GstRTSPServer::service
85 * The service of the server. This is either a string with the service name or
86 * a port number (as a string) the server will listen on.
88 g_object_class_install_property (gobject_class, PROP_SERVICE,
89 g_param_spec_string ("service", "Service",
90 "The service or port number the server uses to listen on",
91 DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
93 * GstRTSPServer::backlog
95 * The backlog argument defines the maximum length to which the queue of
96 * pending connections for the server may grow. If a connection request arrives
97 * when the queue is full, the client may receive an error with an indication of
98 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
99 * request may be ignored so that a later reattempt at connection succeeds.
101 g_object_class_install_property (gobject_class, PROP_BACKLOG,
102 g_param_spec_int ("backlog", "Backlog",
103 "The maximum length to which the queue "
104 "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
105 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
107 * GstRTSPServer::session-pool
109 * The session pool of the server. By default each server has a separate
110 * session pool but sessions can be shared between servers by setting the same
111 * session pool on multiple servers.
113 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
114 g_param_spec_object ("session-pool", "Session Pool",
115 "The session pool to use for client session",
116 GST_TYPE_RTSP_SESSION_POOL,
117 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
119 * GstRTSPServer::media-mapping
121 * The media mapping to use for this server. By default the server has no
122 * media mapping and thus cannot map urls to media streams.
124 g_object_class_install_property (gobject_class, PROP_MEDIA_MAPPING,
125 g_param_spec_object ("media-mapping", "Media Mapping",
126 "The media mapping to use for client session",
127 GST_TYPE_RTSP_MEDIA_MAPPING,
128 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
130 klass->accept_client = default_accept_client;
132 GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
136 gst_rtsp_server_init (GstRTSPServer * server)
138 server->address = g_strdup (DEFAULT_ADDRESS);
139 server->service = g_strdup (DEFAULT_SERVICE);
140 server->backlog = DEFAULT_BACKLOG;
141 server->session_pool = gst_rtsp_session_pool_new ();
142 server->media_mapping = gst_rtsp_media_mapping_new ();
146 gst_rtsp_server_finalize (GObject * object)
148 GstRTSPServer *server = GST_RTSP_SERVER (object);
150 g_free (server->address);
151 g_free (server->service);
153 g_object_unref (server->session_pool);
154 g_object_unref (server->media_mapping);
158 * gst_rtsp_server_new:
160 * Create a new #GstRTSPServer instance.
163 gst_rtsp_server_new (void)
165 GstRTSPServer *result;
167 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
173 * gst_rtsp_server_set_address:
174 * @server: a #GstRTSPServer
175 * @address: the address
177 * Configure @server to accept connections on the given address.
179 * This function must be called before the server is bound.
182 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
184 g_return_if_fail (GST_IS_RTSP_SERVER (server));
185 g_return_if_fail (address != NULL);
187 g_free (server->address);
188 server->address = g_strdup (address);
192 * gst_rtsp_server_get_address:
193 * @server: a #GstRTSPServer
195 * Get the address on which the server will accept connections.
197 * Returns: the server address. g_free() after usage.
200 gst_rtsp_server_get_address (GstRTSPServer * server)
202 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
204 return g_strdup (server->address);
208 * gst_rtsp_server_set_service:
209 * @server: a #GstRTSPServer
210 * @service: the service
212 * Configure @server to accept connections on the given service.
213 * @service should be a string containing the service name (see services(5)) or
214 * a string containing a port number between 1 and 65535.
216 * This function must be called before the server is bound.
219 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
221 g_return_if_fail (GST_IS_RTSP_SERVER (server));
222 g_return_if_fail (service != NULL);
224 g_free (server->service);
225 server->service = g_strdup (service);
229 * gst_rtsp_server_get_service:
230 * @server: a #GstRTSPServer
232 * Get the service on which the server will accept connections.
234 * Returns: the service. use g_free() after usage.
237 gst_rtsp_server_get_service (GstRTSPServer * server)
239 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
241 return g_strdup (server->service);
245 * gst_rtsp_server_set_backlog:
246 * @server: a #GstRTSPServer
247 * @backlog: the backlog
249 * configure the maximum amount of requests that may be queued for the
252 * This function must be called before the server is bound.
255 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
257 g_return_if_fail (GST_IS_RTSP_SERVER (server));
259 server->backlog = backlog;
263 * gst_rtsp_server_get_backlog:
264 * @server: a #GstRTSPServer
266 * The maximum amount of queued requests for the server.
268 * Returns: the server backlog.
271 gst_rtsp_server_get_backlog (GstRTSPServer * server)
273 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
275 return server->backlog;
279 * gst_rtsp_server_set_session_pool:
280 * @server: a #GstRTSPServer
281 * @pool: a #GstRTSPSessionPool
283 * configure @pool to be used as the session pool of @server.
286 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
287 GstRTSPSessionPool * pool)
289 GstRTSPSessionPool *old;
291 g_return_if_fail (GST_IS_RTSP_SERVER (server));
293 old = server->session_pool;
298 server->session_pool = pool;
300 g_object_unref (old);
305 * gst_rtsp_server_get_session_pool:
306 * @server: a #GstRTSPServer
308 * Get the #GstRTSPSessionPool used as the session pool of @server.
310 * Returns: the #GstRTSPSessionPool used for sessions. g_object_unref() after
314 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
316 GstRTSPSessionPool *result;
318 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
320 if ((result = server->session_pool))
321 g_object_ref (result);
327 * gst_rtsp_server_set_media_mapping:
328 * @server: a #GstRTSPServer
329 * @mapping: a #GstRTSPMediaMapping
331 * configure @mapping to be used as the media mapping of @server.
334 gst_rtsp_server_set_media_mapping (GstRTSPServer * server,
335 GstRTSPMediaMapping * mapping)
337 GstRTSPMediaMapping *old;
339 g_return_if_fail (GST_IS_RTSP_SERVER (server));
341 old = server->media_mapping;
343 if (old != mapping) {
345 g_object_ref (mapping);
346 server->media_mapping = mapping;
348 g_object_unref (old);
354 * gst_rtsp_server_get_media_mapping:
355 * @server: a #GstRTSPServer
357 * Get the #GstRTSPMediaMapping used as the media mapping of @server.
359 * Returns: the #GstRTSPMediaMapping of @server. g_object_unref() after
362 GstRTSPMediaMapping *
363 gst_rtsp_server_get_media_mapping (GstRTSPServer * server)
365 GstRTSPMediaMapping *result;
367 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
369 if ((result = server->media_mapping))
370 g_object_ref (result);
376 gst_rtsp_server_get_property (GObject * object, guint propid,
377 GValue * value, GParamSpec * pspec)
379 GstRTSPServer *server = GST_RTSP_SERVER (object);
383 g_value_take_string (value, gst_rtsp_server_get_address (server));
386 g_value_take_string (value, gst_rtsp_server_get_service (server));
389 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
391 case PROP_SESSION_POOL:
392 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
394 case PROP_MEDIA_MAPPING:
395 g_value_take_object (value, gst_rtsp_server_get_media_mapping (server));
398 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
403 gst_rtsp_server_set_property (GObject * object, guint propid,
404 const GValue * value, GParamSpec * pspec)
406 GstRTSPServer *server = GST_RTSP_SERVER (object);
410 gst_rtsp_server_set_address (server, g_value_get_string (value));
413 gst_rtsp_server_set_service (server, g_value_get_string (value));
416 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
418 case PROP_SESSION_POOL:
419 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
421 case PROP_MEDIA_MAPPING:
422 gst_rtsp_server_set_media_mapping (server, g_value_get_object (value));
425 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
429 /* Prepare a server socket for @server and make it listen on the configured port */
431 gst_rtsp_server_sink_init_send (GstRTSPServer * server)
434 struct addrinfo hints;
435 struct addrinfo *result, *rp;
437 struct linger linger;
440 memset (&hints, 0, sizeof (struct addrinfo));
441 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
442 hints.ai_socktype = SOCK_STREAM; /* stream socket */
443 hints.ai_flags = AI_PASSIVE | AI_CANONNAME; /* For wildcard IP address */
444 hints.ai_protocol = 0; /* Any protocol */
445 hints.ai_canonname = NULL;
446 hints.ai_addr = NULL;
447 hints.ai_next = NULL;
449 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
452 /* resolve the server IP address */
454 getaddrinfo (server->address, server->service, &hints, &result)) != 0)
457 /* create server socket, we loop through all the addresses until we manage to
458 * create a socket and bind. */
459 for (rp = result; rp; rp = rp->ai_next) {
460 sockfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
462 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
467 if (bind (sockfd, rp->ai_addr, rp->ai_addrlen) == 0) {
468 GST_DEBUG_OBJECT (server, "bind on %s", rp->ai_canonname);
472 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
476 freeaddrinfo (result);
481 server->server_sock.fd = sockfd;
483 GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d",
484 server->server_sock.fd);
486 /* make address reusable */
488 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
489 (void *) &ret, sizeof (ret)) < 0)
492 /* keep connection alive; avoids SIGPIPE during write */
494 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
495 (void *) &ret, sizeof (ret)) < 0)
496 goto keepalive_failed;
499 /* make sure socket is reset 5 seconds after close. This ensure that we can
500 * reuse the socket quickly while still having a chance to send data to the
504 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_LINGER,
505 (void *) &linger, sizeof (linger)) < 0)
509 /* set the server socket to nonblocking */
510 fcntl (server->server_sock.fd, F_SETFL, O_NONBLOCK);
512 GST_DEBUG_OBJECT (server, "listening on server socket %d with queue of %d",
513 server->server_sock.fd, server->backlog);
514 if (listen (server->server_sock.fd, server->backlog) == -1)
517 GST_DEBUG_OBJECT (server,
518 "listened on server socket %d, returning from connection setup",
519 server->server_sock.fd);
521 GST_INFO_OBJECT (server, "listening on service %s", server->service);
528 GST_ERROR_OBJECT (server, "failed to resolve address: %s",
534 GST_ERROR_OBJECT (server, "failed to create socket: %s",
540 GST_ERROR_OBJECT (server, "failed to reuse socket: %s", g_strerror (errno));
545 GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s",
552 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
559 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
565 if (server->server_sock.fd >= 0) {
566 close (server->server_sock.fd);
567 server->server_sock.fd = -1;
573 /* default method for creating a new client object in the server to accept and
574 * handle a client connection on this server */
575 static GstRTSPClient *
576 default_accept_client (GstRTSPServer * server, GIOChannel * channel)
578 GstRTSPClient *client;
580 /* a new client connected, create a session to handle the client. */
581 client = gst_rtsp_client_new ();
583 /* set the session pool that this client should use */
584 gst_rtsp_client_set_session_pool (client, server->session_pool);
585 /* set the media mapping that this client should use */
586 gst_rtsp_client_set_media_mapping (client, server->media_mapping);
588 /* accept connections for that client, this function returns after accepting
589 * the connection and will run the remainder of the communication with the
590 * client asyncronously. */
591 if (!gst_rtsp_client_accept (client, channel))
599 GST_ERROR_OBJECT (server,
600 "Could not accept client on server socket %d: %s (%d)",
601 server->server_sock.fd, g_strerror (errno), errno);
602 gst_object_unref (client);
608 * gst_rtsp_server_io_func:
609 * @channel: a #GIOChannel
610 * @condition: the condition on @source
612 * A default #GIOFunc that creates a new #GstRTSPClient to accept and handle a
613 * new connection on @channel or @server.
615 * Returns: TRUE if the source could be connected, FALSE if an error occured.
618 gst_rtsp_server_io_func (GIOChannel * channel, GIOCondition condition,
619 GstRTSPServer * server)
621 GstRTSPClient *client = NULL;
622 GstRTSPServerClass *klass;
624 if (condition & G_IO_IN) {
625 klass = GST_RTSP_SERVER_GET_CLASS (server);
627 /* a new client connected, create a client object to handle the client. */
628 if (klass->accept_client)
629 client = klass->accept_client (server, channel);
633 /* can unref the client now, when the request is finished, it will be
635 gst_object_unref (client);
637 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
644 GST_ERROR_OBJECT (server, "failed to create a client");
650 * gst_rtsp_server_get_io_channel:
651 * @server: a #GstRTSPServer
653 * Create a #GIOChannel for @server.
655 * Returns: the GIOChannel for @server or NULL when an error occured.
658 gst_rtsp_server_get_io_channel (GstRTSPServer * server)
660 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
662 if (server->io_channel == NULL) {
663 if (!gst_rtsp_server_sink_init_send (server))
666 /* create IO channel for the socket */
667 server->io_channel = g_io_channel_unix_new (server->server_sock.fd);
669 return server->io_channel;
673 GST_ERROR_OBJECT (server, "failed to initialize server");
679 * gst_rtsp_server_create_watch:
680 * @server: a #GstRTSPServer
682 * Create a #GSource for @server. The new source will have a default
683 * #GIOFunc of gst_rtsp_server_io_func().
685 * Returns: the #GSource for @server or NULL when an error occured.
688 gst_rtsp_server_create_watch (GstRTSPServer * server)
690 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
692 if (server->io_watch == NULL) {
695 channel = gst_rtsp_server_get_io_channel (server);
699 /* create a watch for reads (new connections) and possible errors */
700 server->io_watch = g_io_create_watch (channel, G_IO_IN |
701 G_IO_ERR | G_IO_HUP | G_IO_NVAL);
703 /* configure the callback */
704 g_source_set_callback (server->io_watch,
705 (GSourceFunc) gst_rtsp_server_io_func, server, NULL);
707 return server->io_watch;
711 GST_ERROR_OBJECT (server, "failed to create IO channel");
717 * gst_rtsp_server_attach:
718 * @server: a #GstRTSPServer
719 * @context: a #GMainContext
721 * Attaches @server to @context. When the mainloop for @context is run, the
722 * server will be dispatched.
724 * This function should be called when the server properties and urls are fully
725 * configured and the server is ready to start.
727 * Returns: the ID (greater than 0) for the source within the GMainContext.
730 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
735 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
737 source = gst_rtsp_server_create_watch (server);
741 res = g_source_attach (source, context);
748 GST_ERROR_OBJECT (server, "failed to create watch");