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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * @short_description: The main server object
22 * @see_also: #GstRTSPClient, #GstRTSPThreadPool
24 * The server object is the object listening for connections on a port and
25 * creating #GstRTSPClient objects to handle those connections.
27 * The server will listen on the address set with gst_rtsp_server_set_address()
28 * and the port or service configured with gst_rtsp_server_set_service().
29 * Use gst_rtsp_server_set_backlog() to configure the amount of pending requests
30 * that the server will keep. By default the server listens on the current
31 * network (0.0.0.0) and port 8554.
33 * The server will require an SSL connection when a TLS certificate has been
34 * set in the auth object with gst_rtsp_auth_set_tls_certificate().
36 * To start the server, use gst_rtsp_server_attach() to attach it to a
37 * #GMainContext. For more control, gst_rtsp_server_create_source() and
38 * gst_rtsp_server_create_socket() can be used to get a #GSource and #GSocket
41 * gst_rtsp_server_transfer_connection() can be used to transfer an existing
42 * socket to the RTSP server, for example from an HTTP server.
44 * Once the server socket is attached to a mainloop, it will start accepting
45 * connections. When a new connection is received, a new #GstRTSPClient object
46 * is created to handle the connection. The new client will be configured with
47 * the server #GstRTSPAuth, #GstRTSPMountPoints, #GstRTSPSessionPool and
50 * The server uses the configured #GstRTSPThreadPool object to handle the
51 * remainder of the communication with this client.
53 * Last reviewed on 2013-07-11 (1.0.0)
58 #include "rtsp-server.h"
59 #include "rtsp-client.h"
61 #define GST_RTSP_SERVER_GET_PRIVATE(obj) \
62 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SERVER, GstRTSPServerPrivate))
64 #define GST_RTSP_SERVER_GET_LOCK(server) (&(GST_RTSP_SERVER_CAST(server)->priv->lock))
65 #define GST_RTSP_SERVER_LOCK(server) (g_mutex_lock(GST_RTSP_SERVER_GET_LOCK(server)))
66 #define GST_RTSP_SERVER_UNLOCK(server) (g_mutex_unlock(GST_RTSP_SERVER_GET_LOCK(server)))
68 struct _GstRTSPServerPrivate
70 GMutex lock; /* protects everything in this struct */
72 /* server information */
79 /* sessions on this server */
80 GstRTSPSessionPool *session_pool;
82 /* mount points for this server */
83 GstRTSPMountPoints *mount_points;
85 /* authentication manager */
88 /* resource manager */
89 GstRTSPThreadPool *thread_pool;
91 /* the clients that are connected */
95 #define DEFAULT_ADDRESS "0.0.0.0"
96 #define DEFAULT_BOUND_PORT -1
97 /* #define DEFAULT_ADDRESS "::0" */
98 #define DEFAULT_SERVICE "8554"
99 #define DEFAULT_BACKLOG 5
101 /* Define to use the SO_LINGER option so that the server sockets can be resused
102 * sooner. Disabled for now because it is not very well implemented by various
103 * OSes and it causes clients to fail to read the TEARDOWN response. */
121 SIGNAL_CLIENT_CONNECTED,
125 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
127 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
128 #define GST_CAT_DEFAULT rtsp_server_debug
130 typedef struct _ClientContext ClientContext;
132 static guint gst_rtsp_server_signals[SIGNAL_LAST] = { 0 };
134 static void gst_rtsp_server_get_property (GObject * object, guint propid,
135 GValue * value, GParamSpec * pspec);
136 static void gst_rtsp_server_set_property (GObject * object, guint propid,
137 const GValue * value, GParamSpec * pspec);
138 static void gst_rtsp_server_finalize (GObject * object);
140 static GstRTSPClient *default_create_client (GstRTSPServer * server);
143 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
145 GObjectClass *gobject_class;
147 g_type_class_add_private (klass, sizeof (GstRTSPServerPrivate));
149 gobject_class = G_OBJECT_CLASS (klass);
151 gobject_class->get_property = gst_rtsp_server_get_property;
152 gobject_class->set_property = gst_rtsp_server_set_property;
153 gobject_class->finalize = gst_rtsp_server_finalize;
156 * GstRTSPServer::address:
158 * The address of the server. This is the address where the server will
161 g_object_class_install_property (gobject_class, PROP_ADDRESS,
162 g_param_spec_string ("address", "Address",
163 "The address the server uses to listen on", DEFAULT_ADDRESS,
164 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
166 * GstRTSPServer::service:
168 * The service of the server. This is either a string with the service name or
169 * a port number (as a string) the server will listen on.
171 g_object_class_install_property (gobject_class, PROP_SERVICE,
172 g_param_spec_string ("service", "Service",
173 "The service or port number the server uses to listen on",
174 DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
176 * GstRTSPServer::bound-port:
178 * The actual port the server is listening on. Can be used to retrieve the
179 * port number when the server is started on port 0, which means bind to a
180 * random port. Set to -1 if the server has not been bound yet.
182 g_object_class_install_property (gobject_class, PROP_BOUND_PORT,
183 g_param_spec_int ("bound-port", "Bound port",
184 "The port number the server is listening on",
185 -1, G_MAXUINT16, DEFAULT_BOUND_PORT,
186 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
188 * GstRTSPServer::backlog:
190 * The backlog argument defines the maximum length to which the queue of
191 * pending connections for the server may grow. If a connection request arrives
192 * when the queue is full, the client may receive an error with an indication of
193 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
194 * request may be ignored so that a later reattempt at connection succeeds.
196 g_object_class_install_property (gobject_class, PROP_BACKLOG,
197 g_param_spec_int ("backlog", "Backlog",
198 "The maximum length to which the queue "
199 "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
200 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
202 * GstRTSPServer::session-pool:
204 * The session pool of the server. By default each server has a separate
205 * session pool but sessions can be shared between servers by setting the same
206 * session pool on multiple servers.
208 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
209 g_param_spec_object ("session-pool", "Session Pool",
210 "The session pool to use for client session",
211 GST_TYPE_RTSP_SESSION_POOL,
212 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 * GstRTSPServer::mount-points:
216 * The mount points to use for this server. By default the server has no
217 * mount points and thus cannot map urls to media streams.
219 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
220 g_param_spec_object ("mount-points", "Mount Points",
221 "The mount points to use for client session",
222 GST_TYPE_RTSP_MOUNT_POINTS,
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225 gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED] =
226 g_signal_new ("client-connected", G_TYPE_FROM_CLASS (gobject_class),
227 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPServerClass, client_connected),
228 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
229 gst_rtsp_client_get_type ());
231 klass->create_client = default_create_client;
233 GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
237 gst_rtsp_server_init (GstRTSPServer * server)
239 GstRTSPServerPrivate *priv = GST_RTSP_SERVER_GET_PRIVATE (server);
243 g_mutex_init (&priv->lock);
244 priv->address = g_strdup (DEFAULT_ADDRESS);
245 priv->service = g_strdup (DEFAULT_SERVICE);
247 priv->backlog = DEFAULT_BACKLOG;
248 priv->session_pool = gst_rtsp_session_pool_new ();
249 priv->mount_points = gst_rtsp_mount_points_new ();
250 priv->thread_pool = gst_rtsp_thread_pool_new ();
254 gst_rtsp_server_finalize (GObject * object)
256 GstRTSPServer *server = GST_RTSP_SERVER (object);
257 GstRTSPServerPrivate *priv = server->priv;
259 GST_DEBUG_OBJECT (server, "finalize server");
261 g_free (priv->address);
262 g_free (priv->service);
265 g_object_unref (priv->socket);
267 if (priv->session_pool)
268 g_object_unref (priv->session_pool);
269 if (priv->mount_points)
270 g_object_unref (priv->mount_points);
271 if (priv->thread_pool)
272 g_object_unref (priv->thread_pool);
275 g_object_unref (priv->auth);
277 g_mutex_clear (&priv->lock);
279 G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
283 * gst_rtsp_server_new:
285 * Create a new #GstRTSPServer instance.
288 gst_rtsp_server_new (void)
290 GstRTSPServer *result;
292 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
298 * gst_rtsp_server_set_address:
299 * @server: a #GstRTSPServer
300 * @address: the address
302 * Configure @server to accept connections on the given address.
304 * This function must be called before the server is bound.
307 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
309 GstRTSPServerPrivate *priv;
311 g_return_if_fail (GST_IS_RTSP_SERVER (server));
312 g_return_if_fail (address != NULL);
316 GST_RTSP_SERVER_LOCK (server);
317 g_free (priv->address);
318 priv->address = g_strdup (address);
319 GST_RTSP_SERVER_UNLOCK (server);
323 * gst_rtsp_server_get_address:
324 * @server: a #GstRTSPServer
326 * Get the address on which the server will accept connections.
328 * Returns: the server address. g_free() after usage.
331 gst_rtsp_server_get_address (GstRTSPServer * server)
333 GstRTSPServerPrivate *priv;
336 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
340 GST_RTSP_SERVER_LOCK (server);
341 result = g_strdup (priv->address);
342 GST_RTSP_SERVER_UNLOCK (server);
348 * gst_rtsp_server_get_bound_port:
349 * @server: a #GstRTSPServer
351 * Get the port number where the server was bound to.
353 * Returns: the port number
356 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
358 GstRTSPServerPrivate *priv;
359 GSocketAddress *address;
362 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
366 GST_RTSP_SERVER_LOCK (server);
367 if (priv->socket == NULL)
370 address = g_socket_get_local_address (priv->socket, NULL);
371 result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
372 g_object_unref (address);
375 GST_RTSP_SERVER_UNLOCK (server);
381 * gst_rtsp_server_set_service:
382 * @server: a #GstRTSPServer
383 * @service: the service
385 * Configure @server to accept connections on the given service.
386 * @service should be a string containing the service name (see services(5)) or
387 * a string containing a port number between 1 and 65535.
389 * When @service is set to "0", the server will listen on a random free
390 * port. The actual used port can be retrieved with
391 * gst_rtsp_server_get_bound_port().
393 * This function must be called before the server is bound.
396 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
398 GstRTSPServerPrivate *priv;
400 g_return_if_fail (GST_IS_RTSP_SERVER (server));
401 g_return_if_fail (service != NULL);
405 GST_RTSP_SERVER_LOCK (server);
406 g_free (priv->service);
407 priv->service = g_strdup (service);
408 GST_RTSP_SERVER_UNLOCK (server);
412 * gst_rtsp_server_get_service:
413 * @server: a #GstRTSPServer
415 * Get the service on which the server will accept connections.
417 * Returns: the service. use g_free() after usage.
420 gst_rtsp_server_get_service (GstRTSPServer * server)
422 GstRTSPServerPrivate *priv;
425 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
429 GST_RTSP_SERVER_LOCK (server);
430 result = g_strdup (priv->service);
431 GST_RTSP_SERVER_UNLOCK (server);
437 * gst_rtsp_server_set_backlog:
438 * @server: a #GstRTSPServer
439 * @backlog: the backlog
441 * configure the maximum amount of requests that may be queued for the
444 * This function must be called before the server is bound.
447 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
449 GstRTSPServerPrivate *priv;
451 g_return_if_fail (GST_IS_RTSP_SERVER (server));
455 GST_RTSP_SERVER_LOCK (server);
456 priv->backlog = backlog;
457 GST_RTSP_SERVER_UNLOCK (server);
461 * gst_rtsp_server_get_backlog:
462 * @server: a #GstRTSPServer
464 * The maximum amount of queued requests for the server.
466 * Returns: the server backlog.
469 gst_rtsp_server_get_backlog (GstRTSPServer * server)
471 GstRTSPServerPrivate *priv;
474 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
478 GST_RTSP_SERVER_LOCK (server);
479 result = priv->backlog;
480 GST_RTSP_SERVER_UNLOCK (server);
486 * gst_rtsp_server_set_session_pool:
487 * @server: a #GstRTSPServer
488 * @pool: a #GstRTSPSessionPool
490 * configure @pool to be used as the session pool of @server.
493 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
494 GstRTSPSessionPool * pool)
496 GstRTSPServerPrivate *priv;
497 GstRTSPSessionPool *old;
499 g_return_if_fail (GST_IS_RTSP_SERVER (server));
506 GST_RTSP_SERVER_LOCK (server);
507 old = priv->session_pool;
508 priv->session_pool = pool;
509 GST_RTSP_SERVER_UNLOCK (server);
512 g_object_unref (old);
516 * gst_rtsp_server_get_session_pool:
517 * @server: a #GstRTSPServer
519 * Get the #GstRTSPSessionPool used as the session pool of @server.
521 * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
525 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
527 GstRTSPServerPrivate *priv;
528 GstRTSPSessionPool *result;
530 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
534 GST_RTSP_SERVER_LOCK (server);
535 if ((result = priv->session_pool))
536 g_object_ref (result);
537 GST_RTSP_SERVER_UNLOCK (server);
543 * gst_rtsp_server_set_mount_points:
544 * @server: a #GstRTSPServer
545 * @mounts: a #GstRTSPMountPoints
547 * configure @mounts to be used as the mount points of @server.
550 gst_rtsp_server_set_mount_points (GstRTSPServer * server,
551 GstRTSPMountPoints * mounts)
553 GstRTSPServerPrivate *priv;
554 GstRTSPMountPoints *old;
556 g_return_if_fail (GST_IS_RTSP_SERVER (server));
561 g_object_ref (mounts);
563 GST_RTSP_SERVER_LOCK (server);
564 old = priv->mount_points;
565 priv->mount_points = mounts;
566 GST_RTSP_SERVER_UNLOCK (server);
569 g_object_unref (old);
574 * gst_rtsp_server_get_mount_points:
575 * @server: a #GstRTSPServer
577 * Get the #GstRTSPMountPoints used as the mount points of @server.
579 * Returns: (transfer full): the #GstRTSPMountPoints of @server. g_object_unref() after
583 gst_rtsp_server_get_mount_points (GstRTSPServer * server)
585 GstRTSPServerPrivate *priv;
586 GstRTSPMountPoints *result;
588 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
592 GST_RTSP_SERVER_LOCK (server);
593 if ((result = priv->mount_points))
594 g_object_ref (result);
595 GST_RTSP_SERVER_UNLOCK (server);
601 * gst_rtsp_server_set_auth:
602 * @server: a #GstRTSPServer
603 * @auth: a #GstRTSPAuth
605 * configure @auth to be used as the authentication manager of @server.
608 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
610 GstRTSPServerPrivate *priv;
613 g_return_if_fail (GST_IS_RTSP_SERVER (server));
620 GST_RTSP_SERVER_LOCK (server);
623 GST_RTSP_SERVER_UNLOCK (server);
626 g_object_unref (old);
631 * gst_rtsp_server_get_auth:
632 * @server: a #GstRTSPServer
634 * Get the #GstRTSPAuth used as the authentication manager of @server.
636 * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
640 gst_rtsp_server_get_auth (GstRTSPServer * server)
642 GstRTSPServerPrivate *priv;
645 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
649 GST_RTSP_SERVER_LOCK (server);
650 if ((result = priv->auth))
651 g_object_ref (result);
652 GST_RTSP_SERVER_UNLOCK (server);
658 * gst_rtsp_server_set_thread_pool:
659 * @server: a #GstRTSPServer
660 * @pool: a #GstRTSPThreadPool
662 * configure @pool to be used as the thread pool of @server.
665 gst_rtsp_server_set_thread_pool (GstRTSPServer * server,
666 GstRTSPThreadPool * pool)
668 GstRTSPServerPrivate *priv;
669 GstRTSPThreadPool *old;
671 g_return_if_fail (GST_IS_RTSP_SERVER (server));
678 GST_RTSP_SERVER_LOCK (server);
679 old = priv->thread_pool;
680 priv->thread_pool = pool;
681 GST_RTSP_SERVER_UNLOCK (server);
684 g_object_unref (old);
688 * gst_rtsp_server_get_thread_pool:
689 * @server: a #GstRTSPServer
691 * Get the #GstRTSPThreadPool used as the thread pool of @server.
693 * Returns: (transfer full): the #GstRTSPThreadPool of @server. g_object_unref() after
697 gst_rtsp_server_get_thread_pool (GstRTSPServer * server)
699 GstRTSPServerPrivate *priv;
700 GstRTSPThreadPool *result;
702 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
706 GST_RTSP_SERVER_LOCK (server);
707 if ((result = priv->thread_pool))
708 g_object_ref (result);
709 GST_RTSP_SERVER_UNLOCK (server);
715 gst_rtsp_server_get_property (GObject * object, guint propid,
716 GValue * value, GParamSpec * pspec)
718 GstRTSPServer *server = GST_RTSP_SERVER (object);
722 g_value_take_string (value, gst_rtsp_server_get_address (server));
725 g_value_take_string (value, gst_rtsp_server_get_service (server));
727 case PROP_BOUND_PORT:
728 g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
731 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
733 case PROP_SESSION_POOL:
734 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
736 case PROP_MOUNT_POINTS:
737 g_value_take_object (value, gst_rtsp_server_get_mount_points (server));
740 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
745 gst_rtsp_server_set_property (GObject * object, guint propid,
746 const GValue * value, GParamSpec * pspec)
748 GstRTSPServer *server = GST_RTSP_SERVER (object);
752 gst_rtsp_server_set_address (server, g_value_get_string (value));
755 gst_rtsp_server_set_service (server, g_value_get_string (value));
758 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
760 case PROP_SESSION_POOL:
761 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
763 case PROP_MOUNT_POINTS:
764 gst_rtsp_server_set_mount_points (server, g_value_get_object (value));
767 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
772 * gst_rtsp_server_create_socket:
773 * @server: a #GstRTSPServer
774 * @cancellable: a #GCancellable
777 * Create a #GSocket for @server. The socket will listen on the
778 * configured service.
780 * Returns: (transfer full): the #GSocket for @server or %NULL when an error occured.
783 gst_rtsp_server_create_socket (GstRTSPServer * server,
784 GCancellable * cancellable, GError ** error)
786 GstRTSPServerPrivate *priv;
787 GSocketConnectable *conn;
788 GSocketAddressEnumerator *enumerator;
789 GSocket *socket = NULL;
791 struct linger linger;
793 GError *sock_error = NULL;
794 GError *bind_error = NULL;
797 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
801 GST_RTSP_SERVER_LOCK (server);
802 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", priv->address,
805 /* resolve the server IP address */
806 port = atoi (priv->service);
807 if (port != 0 || !strcmp (priv->service, "0"))
808 conn = g_network_address_new (priv->address, port);
810 conn = g_network_service_new (priv->service, "tcp", priv->address);
812 enumerator = g_socket_connectable_enumerate (conn);
813 g_object_unref (conn);
815 /* create server socket, we loop through all the addresses until we manage to
816 * create a socket and bind. */
818 GSocketAddress *sockaddr;
821 g_socket_address_enumerator_next (enumerator, cancellable, error);
824 GST_DEBUG_OBJECT (server, "no more addresses %s",
825 *error ? (*error)->message : "");
827 GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
832 /* only keep the first error */
833 socket = g_socket_new (g_socket_address_get_family (sockaddr),
834 G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
835 sock_error ? NULL : &sock_error);
837 if (socket == NULL) {
838 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
839 sock_error->message);
840 g_object_unref (sockaddr);
844 if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
845 /* ask what port the socket has been bound to */
846 if (port == 0 || !strcmp (priv->service, "0")) {
847 GError *addr_error = NULL;
849 g_object_unref (sockaddr);
850 sockaddr = g_socket_get_local_address (socket, &addr_error);
852 if (addr_error != NULL) {
853 GST_DEBUG_OBJECT (server,
854 "failed to get the local address of a bound socket %s",
855 addr_error->message);
856 g_clear_error (&addr_error);
860 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr));
863 g_free (priv->service);
864 priv->service = g_strdup_printf ("%d", port);
866 GST_DEBUG_OBJECT (server, "failed to get the port of a bound socket");
869 g_object_unref (sockaddr);
873 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
874 bind_error->message);
875 g_object_unref (sockaddr);
876 g_object_unref (socket);
879 g_object_unref (enumerator);
884 g_clear_error (&sock_error);
885 g_clear_error (&bind_error);
887 GST_DEBUG_OBJECT (server, "opened sending server socket");
889 /* keep connection alive; avoids SIGPIPE during write */
890 g_socket_set_keepalive (socket, TRUE);
894 /* make sure socket is reset 5 seconds after close. This ensure that we can
895 * reuse the socket quickly while still having a chance to send data to the
899 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
900 (void *) &linger, sizeof (linger)) < 0)
905 /* set the server socket to nonblocking */
906 g_socket_set_blocking (socket, FALSE);
908 /* set listen backlog */
909 g_socket_set_listen_backlog (socket, priv->backlog);
911 if (!g_socket_listen (socket, error))
914 GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
915 socket, priv->backlog);
917 GST_RTSP_SERVER_UNLOCK (server);
924 GST_ERROR_OBJECT (server, "failed to create socket");
931 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
939 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
946 g_object_unref (socket);
950 g_propagate_error (error, sock_error);
952 g_error_free (sock_error);
955 if ((error == NULL) || (*error == NULL))
956 g_propagate_error (error, bind_error);
958 g_error_free (bind_error);
960 GST_RTSP_SERVER_UNLOCK (server);
965 struct _ClientContext
967 GstRTSPServer *server;
968 GstRTSPThread *thread;
969 GstRTSPClient *client;
973 free_client_context (ClientContext * ctx)
975 GST_DEBUG ("free context %p", ctx);
977 GST_RTSP_SERVER_LOCK (ctx->server);
979 gst_rtsp_thread_stop (ctx->thread);
980 GST_RTSP_SERVER_UNLOCK (ctx->server);
982 g_object_unref (ctx->client);
983 g_object_unref (ctx->server);
984 g_slice_free (ClientContext, ctx);
986 return G_SOURCE_REMOVE;
990 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
992 GstRTSPServer *server = ctx->server;
993 GstRTSPServerPrivate *priv = server->priv;
995 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
997 GST_RTSP_SERVER_LOCK (server);
998 priv->clients = g_list_remove (priv->clients, ctx);
999 GST_RTSP_SERVER_UNLOCK (server);
1004 src = g_idle_source_new ();
1005 g_source_set_callback (src, (GSourceFunc) free_client_context, ctx, NULL);
1006 g_source_attach (src, ctx->thread->context);
1007 g_source_unref (src);
1009 free_client_context (ctx);
1013 /* add the client context to the active list of clients, takes ownership
1016 manage_client (GstRTSPServer * server, GstRTSPClient * client)
1018 ClientContext *cctx;
1019 GstRTSPServerPrivate *priv = server->priv;
1020 GMainContext *mainctx = NULL;
1021 GstRTSPContext ctx = { NULL };
1023 GST_DEBUG_OBJECT (server, "manage client %p", client);
1025 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1028 cctx = g_slice_new0 (ClientContext);
1029 cctx->server = g_object_ref (server);
1030 cctx->client = client;
1032 GST_RTSP_SERVER_LOCK (server);
1034 ctx.server = server;
1035 ctx.client = client;
1037 cctx->thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
1038 GST_RTSP_THREAD_TYPE_CLIENT, &ctx);
1040 mainctx = cctx->thread->context;
1043 /* find the context to add the watch */
1044 if ((source = g_main_current_source ()))
1045 mainctx = g_source_get_context (source);
1048 g_signal_connect (client, "closed", (GCallback) unmanage_client, cctx);
1049 priv->clients = g_list_prepend (priv->clients, cctx);
1051 gst_rtsp_client_attach (client, mainctx);
1053 GST_RTSP_SERVER_UNLOCK (server);
1056 static GstRTSPClient *
1057 default_create_client (GstRTSPServer * server)
1059 GstRTSPClient *client;
1060 GstRTSPServerPrivate *priv = server->priv;
1062 /* a new client connected, create a session to handle the client. */
1063 client = gst_rtsp_client_new ();
1065 /* set the session pool that this client should use */
1066 GST_RTSP_SERVER_LOCK (server);
1067 gst_rtsp_client_set_session_pool (client, priv->session_pool);
1068 /* set the mount points that this client should use */
1069 gst_rtsp_client_set_mount_points (client, priv->mount_points);
1070 /* set authentication manager */
1071 gst_rtsp_client_set_auth (client, priv->auth);
1072 /* set threadpool */
1073 gst_rtsp_client_set_thread_pool (client, priv->thread_pool);
1074 GST_RTSP_SERVER_UNLOCK (server);
1080 * gst_rtsp_server_transfer_connection:
1081 * @server: a #GstRTSPServer
1082 * @socket: (transfer full): a network socket
1083 * @ip: the IP address of the remote client
1084 * @port: the port used by the other end
1085 * @initial_buffer: any initial data that was already read from the socket
1087 * Take an existing network socket and use it for an RTSP connection. This
1088 * is used when transferring a socket from an HTTP server which should be used
1089 * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
1090 * that the HTTP server read from the socket while parsing the HTTP header.
1092 * Returns: TRUE if all was ok, FALSE if an error occured.
1095 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
1096 const gchar * ip, gint port, const gchar * initial_buffer)
1098 GstRTSPClient *client = NULL;
1099 GstRTSPServerClass *klass;
1100 GstRTSPConnection *conn;
1103 klass = GST_RTSP_SERVER_GET_CLASS (server);
1105 if (klass->create_client)
1106 client = klass->create_client (server);
1110 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
1111 initial_buffer, &conn), no_connection);
1112 g_object_unref (socket);
1114 /* set connection on the client now */
1115 gst_rtsp_client_set_connection (client, conn);
1117 /* manage the client connection */
1118 manage_client (server, client);
1125 GST_ERROR_OBJECT (server, "failed to create a client");
1126 g_object_unref (socket);
1131 gchar *str = gst_rtsp_strresult (res);
1132 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
1134 g_object_unref (socket);
1140 * gst_rtsp_server_io_func:
1141 * @socket: a #GSocket
1142 * @condition: the condition on @source
1143 * @server: a #GstRTSPServer
1145 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
1146 * new connection on @socket or @server.
1148 * Returns: TRUE if the source could be connected, FALSE if an error occured.
1151 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
1152 GstRTSPServer * server)
1154 GstRTSPServerPrivate *priv = server->priv;
1155 GstRTSPClient *client = NULL;
1156 GstRTSPServerClass *klass;
1158 GstRTSPConnection *conn = NULL;
1159 GstRTSPContext ctx = { NULL };
1161 if (condition & G_IO_IN) {
1162 /* a new client connected. */
1163 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, NULL),
1166 ctx.server = server;
1168 ctx.auth = priv->auth;
1169 gst_rtsp_context_push_current (&ctx);
1171 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_CONNECT))
1172 goto connection_refused;
1174 klass = GST_RTSP_SERVER_GET_CLASS (server);
1175 /* a new client connected, create a client object to handle the client. */
1176 if (klass->create_client)
1177 client = klass->create_client (server);
1181 /* set connection on the client now */
1182 gst_rtsp_client_set_connection (client, conn);
1184 /* manage the client connection */
1185 manage_client (server, client);
1187 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
1190 gst_rtsp_context_pop_current (&ctx);
1192 return G_SOURCE_CONTINUE;
1197 gchar *str = gst_rtsp_strresult (res);
1198 GST_ERROR_OBJECT (server, "Could not accept client on socket %p: %s",
1205 GST_ERROR_OBJECT (server, "connection refused");
1206 gst_rtsp_connection_free (conn);
1211 GST_ERROR_OBJECT (server, "failed to create a client");
1212 gst_rtsp_connection_free (conn);
1218 watch_destroyed (GstRTSPServer * server)
1220 GstRTSPServerPrivate *priv = server->priv;
1222 GST_DEBUG_OBJECT (server, "source destroyed");
1224 g_object_unref (priv->socket);
1225 priv->socket = NULL;
1226 g_object_unref (server);
1230 * gst_rtsp_server_create_source:
1231 * @server: a #GstRTSPServer
1232 * @cancellable: a #GCancellable or %NULL.
1235 * Create a #GSource for @server. The new source will have a default
1236 * #GSocketSourceFunc of gst_rtsp_server_io_func().
1238 * @cancellable if not %NULL can be used to cancel the source, which will cause
1239 * the source to trigger, reporting the current condition (which is likely 0
1240 * unless cancellation happened at the same time as a condition change). You can
1241 * check for this in the callback using g_cancellable_is_cancelled().
1243 * Returns: the #GSource for @server or %NULL when an error occured. Free with
1247 gst_rtsp_server_create_source (GstRTSPServer * server,
1248 GCancellable * cancellable, GError ** error)
1250 GstRTSPServerPrivate *priv;
1251 GSocket *socket, *old;
1254 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1256 priv = server->priv;
1258 socket = gst_rtsp_server_create_socket (server, NULL, error);
1262 GST_RTSP_SERVER_LOCK (server);
1264 priv->socket = g_object_ref (socket);
1265 GST_RTSP_SERVER_UNLOCK (server);
1268 g_object_unref (old);
1270 /* create a watch for reads (new connections) and possible errors */
1271 source = g_socket_create_source (socket, G_IO_IN |
1272 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
1273 g_object_unref (socket);
1275 /* configure the callback */
1276 g_source_set_callback (source,
1277 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
1278 (GDestroyNotify) watch_destroyed);
1284 GST_ERROR_OBJECT (server, "failed to create socket");
1290 * gst_rtsp_server_attach:
1291 * @server: a #GstRTSPServer
1292 * @context: (allow-none): a #GMainContext
1294 * Attaches @server to @context. When the mainloop for @context is run, the
1295 * server will be dispatched. When @context is %NULL, the default context will be
1298 * This function should be called when the server properties and urls are fully
1299 * configured and the server is ready to start.
1301 * Returns: the ID (greater than 0) for the source within the GMainContext.
1304 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1308 GError *error = NULL;
1310 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1312 source = gst_rtsp_server_create_source (server, NULL, &error);
1316 res = g_source_attach (source, context);
1317 g_source_unref (source);
1324 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1325 g_error_free (error);
1331 * gst_rtsp_server_client_filter:
1332 * @server: a #GstRTSPServer
1333 * @func: (scope call) (allow-none): a callback
1334 * @user_data: user data passed to @func
1336 * Call @func for each client managed by @server. The result value of @func
1337 * determines what happens to the client. @func will be called with @server
1338 * locked so no further actions on @server can be performed from @func.
1340 * If @func returns #GST_RTSP_FILTER_REMOVE, the client will be removed from
1343 * If @func returns #GST_RTSP_FILTER_KEEP, the client will remain in @server.
1345 * If @func returns #GST_RTSP_FILTER_REF, the client will remain in @server but
1346 * will also be added with an additional ref to the result #GList of this
1349 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each client.
1351 * Returns: (element-type GstRTSPClient) (transfer full): a #GList with all
1352 * clients for which @func returned #GST_RTSP_FILTER_REF. After usage, each
1353 * element in the #GList should be unreffed before the list is freed.
1356 gst_rtsp_server_client_filter (GstRTSPServer * server,
1357 GstRTSPServerClientFilterFunc func, gpointer user_data)
1359 GstRTSPServerPrivate *priv;
1360 GList *result, *walk, *next;
1362 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1364 priv = server->priv;
1368 GST_RTSP_SERVER_LOCK (server);
1369 for (walk = priv->clients; walk; walk = next) {
1370 ClientContext *cctx = walk->data;
1371 GstRTSPFilterResult res;
1373 next = g_list_next (walk);
1376 res = func (server, cctx->client, user_data);
1378 res = GST_RTSP_FILTER_REF;
1381 case GST_RTSP_FILTER_REMOVE:
1382 /* remove client, FIXME */
1384 case GST_RTSP_FILTER_REF:
1385 result = g_list_prepend (result, g_object_ref (cctx->client));
1387 case GST_RTSP_FILTER_KEEP:
1392 GST_RTSP_SERVER_UNLOCK (server);