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 */
96 #define DEFAULT_ADDRESS "0.0.0.0"
97 #define DEFAULT_BOUND_PORT -1
98 /* #define DEFAULT_ADDRESS "::0" */
99 #define DEFAULT_SERVICE "8554"
100 #define DEFAULT_BACKLOG 5
102 /* Define to use the SO_LINGER option so that the server sockets can be resused
103 * sooner. Disabled for now because it is not very well implemented by various
104 * OSes and it causes clients to fail to read the TEARDOWN response. */
122 SIGNAL_CLIENT_CONNECTED,
126 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
128 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
129 #define GST_CAT_DEFAULT rtsp_server_debug
131 typedef struct _ClientContext ClientContext;
133 static guint gst_rtsp_server_signals[SIGNAL_LAST] = { 0 };
135 static void gst_rtsp_server_get_property (GObject * object, guint propid,
136 GValue * value, GParamSpec * pspec);
137 static void gst_rtsp_server_set_property (GObject * object, guint propid,
138 const GValue * value, GParamSpec * pspec);
139 static void gst_rtsp_server_finalize (GObject * object);
141 static GstRTSPClient *default_create_client (GstRTSPServer * server);
144 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
146 GObjectClass *gobject_class;
148 g_type_class_add_private (klass, sizeof (GstRTSPServerPrivate));
150 gobject_class = G_OBJECT_CLASS (klass);
152 gobject_class->get_property = gst_rtsp_server_get_property;
153 gobject_class->set_property = gst_rtsp_server_set_property;
154 gobject_class->finalize = gst_rtsp_server_finalize;
157 * GstRTSPServer::address:
159 * The address of the server. This is the address where the server will
162 g_object_class_install_property (gobject_class, PROP_ADDRESS,
163 g_param_spec_string ("address", "Address",
164 "The address the server uses to listen on", DEFAULT_ADDRESS,
165 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167 * GstRTSPServer::service:
169 * The service of the server. This is either a string with the service name or
170 * a port number (as a string) the server will listen on.
172 g_object_class_install_property (gobject_class, PROP_SERVICE,
173 g_param_spec_string ("service", "Service",
174 "The service or port number the server uses to listen on",
175 DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177 * GstRTSPServer::bound-port:
179 * The actual port the server is listening on. Can be used to retrieve the
180 * port number when the server is started on port 0, which means bind to a
181 * random port. Set to -1 if the server has not been bound yet.
183 g_object_class_install_property (gobject_class, PROP_BOUND_PORT,
184 g_param_spec_int ("bound-port", "Bound port",
185 "The port number the server is listening on",
186 -1, G_MAXUINT16, DEFAULT_BOUND_PORT,
187 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
189 * GstRTSPServer::backlog:
191 * The backlog argument defines the maximum length to which the queue of
192 * pending connections for the server may grow. If a connection request arrives
193 * when the queue is full, the client may receive an error with an indication of
194 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
195 * request may be ignored so that a later reattempt at connection succeeds.
197 g_object_class_install_property (gobject_class, PROP_BACKLOG,
198 g_param_spec_int ("backlog", "Backlog",
199 "The maximum length to which the queue "
200 "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
201 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203 * GstRTSPServer::session-pool:
205 * The session pool of the server. By default each server has a separate
206 * session pool but sessions can be shared between servers by setting the same
207 * session pool on multiple servers.
209 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
210 g_param_spec_object ("session-pool", "Session Pool",
211 "The session pool to use for client session",
212 GST_TYPE_RTSP_SESSION_POOL,
213 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215 * GstRTSPServer::mount-points:
217 * The mount points to use for this server. By default the server has no
218 * mount points and thus cannot map urls to media streams.
220 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
221 g_param_spec_object ("mount-points", "Mount Points",
222 "The mount points to use for client session",
223 GST_TYPE_RTSP_MOUNT_POINTS,
224 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED] =
227 g_signal_new ("client-connected", G_TYPE_FROM_CLASS (gobject_class),
228 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPServerClass, client_connected),
229 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
230 GST_TYPE_RTSP_CLIENT);
232 klass->create_client = default_create_client;
234 GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
238 gst_rtsp_server_init (GstRTSPServer * server)
240 GstRTSPServerPrivate *priv = GST_RTSP_SERVER_GET_PRIVATE (server);
244 g_mutex_init (&priv->lock);
245 priv->address = g_strdup (DEFAULT_ADDRESS);
246 priv->service = g_strdup (DEFAULT_SERVICE);
248 priv->backlog = DEFAULT_BACKLOG;
249 priv->session_pool = gst_rtsp_session_pool_new ();
250 priv->mount_points = gst_rtsp_mount_points_new ();
251 priv->thread_pool = gst_rtsp_thread_pool_new ();
255 gst_rtsp_server_finalize (GObject * object)
257 GstRTSPServer *server = GST_RTSP_SERVER (object);
258 GstRTSPServerPrivate *priv = server->priv;
260 GST_DEBUG_OBJECT (server, "finalize server");
262 g_free (priv->address);
263 g_free (priv->service);
266 g_object_unref (priv->socket);
268 if (priv->session_pool)
269 g_object_unref (priv->session_pool);
270 if (priv->mount_points)
271 g_object_unref (priv->mount_points);
272 if (priv->thread_pool)
273 g_object_unref (priv->thread_pool);
276 g_object_unref (priv->auth);
278 g_mutex_clear (&priv->lock);
280 G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
284 * gst_rtsp_server_new:
286 * Create a new #GstRTSPServer instance.
288 * Returns: (transfer full): a new #GstRTSPServer
291 gst_rtsp_server_new (void)
293 GstRTSPServer *result;
295 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
301 * gst_rtsp_server_set_address:
302 * @server: a #GstRTSPServer
303 * @address: the address
305 * Configure @server to accept connections on the given address.
307 * This function must be called before the server is bound.
310 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
312 GstRTSPServerPrivate *priv;
314 g_return_if_fail (GST_IS_RTSP_SERVER (server));
315 g_return_if_fail (address != NULL);
319 GST_RTSP_SERVER_LOCK (server);
320 g_free (priv->address);
321 priv->address = g_strdup (address);
322 GST_RTSP_SERVER_UNLOCK (server);
326 * gst_rtsp_server_get_address:
327 * @server: a #GstRTSPServer
329 * Get the address on which the server will accept connections.
331 * Returns: (transfer full): the server address. g_free() after usage.
334 gst_rtsp_server_get_address (GstRTSPServer * server)
336 GstRTSPServerPrivate *priv;
339 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
343 GST_RTSP_SERVER_LOCK (server);
344 result = g_strdup (priv->address);
345 GST_RTSP_SERVER_UNLOCK (server);
351 * gst_rtsp_server_get_bound_port:
352 * @server: a #GstRTSPServer
354 * Get the port number where the server was bound to.
356 * Returns: the port number
359 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
361 GstRTSPServerPrivate *priv;
362 GSocketAddress *address;
365 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
369 GST_RTSP_SERVER_LOCK (server);
370 if (priv->socket == NULL)
373 address = g_socket_get_local_address (priv->socket, NULL);
374 result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
375 g_object_unref (address);
378 GST_RTSP_SERVER_UNLOCK (server);
384 * gst_rtsp_server_set_service:
385 * @server: a #GstRTSPServer
386 * @service: the service
388 * Configure @server to accept connections on the given service.
389 * @service should be a string containing the service name (see services(5)) or
390 * a string containing a port number between 1 and 65535.
392 * When @service is set to "0", the server will listen on a random free
393 * port. The actual used port can be retrieved with
394 * gst_rtsp_server_get_bound_port().
396 * This function must be called before the server is bound.
399 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
401 GstRTSPServerPrivate *priv;
403 g_return_if_fail (GST_IS_RTSP_SERVER (server));
404 g_return_if_fail (service != NULL);
408 GST_RTSP_SERVER_LOCK (server);
409 g_free (priv->service);
410 priv->service = g_strdup (service);
411 GST_RTSP_SERVER_UNLOCK (server);
415 * gst_rtsp_server_get_service:
416 * @server: a #GstRTSPServer
418 * Get the service on which the server will accept connections.
420 * Returns: (transfer full): the service. use g_free() after usage.
423 gst_rtsp_server_get_service (GstRTSPServer * server)
425 GstRTSPServerPrivate *priv;
428 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
432 GST_RTSP_SERVER_LOCK (server);
433 result = g_strdup (priv->service);
434 GST_RTSP_SERVER_UNLOCK (server);
440 * gst_rtsp_server_set_backlog:
441 * @server: a #GstRTSPServer
442 * @backlog: the backlog
444 * configure the maximum amount of requests that may be queued for the
447 * This function must be called before the server is bound.
450 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
452 GstRTSPServerPrivate *priv;
454 g_return_if_fail (GST_IS_RTSP_SERVER (server));
458 GST_RTSP_SERVER_LOCK (server);
459 priv->backlog = backlog;
460 GST_RTSP_SERVER_UNLOCK (server);
464 * gst_rtsp_server_get_backlog:
465 * @server: a #GstRTSPServer
467 * The maximum amount of queued requests for the server.
469 * Returns: the server backlog.
472 gst_rtsp_server_get_backlog (GstRTSPServer * server)
474 GstRTSPServerPrivate *priv;
477 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
481 GST_RTSP_SERVER_LOCK (server);
482 result = priv->backlog;
483 GST_RTSP_SERVER_UNLOCK (server);
489 * gst_rtsp_server_set_session_pool:
490 * @server: a #GstRTSPServer
491 * @pool: (transfer none): a #GstRTSPSessionPool
493 * configure @pool to be used as the session pool of @server.
496 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
497 GstRTSPSessionPool * pool)
499 GstRTSPServerPrivate *priv;
500 GstRTSPSessionPool *old;
502 g_return_if_fail (GST_IS_RTSP_SERVER (server));
509 GST_RTSP_SERVER_LOCK (server);
510 old = priv->session_pool;
511 priv->session_pool = pool;
512 GST_RTSP_SERVER_UNLOCK (server);
515 g_object_unref (old);
519 * gst_rtsp_server_get_session_pool:
520 * @server: a #GstRTSPServer
522 * Get the #GstRTSPSessionPool used as the session pool of @server.
524 * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
528 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
530 GstRTSPServerPrivate *priv;
531 GstRTSPSessionPool *result;
533 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
537 GST_RTSP_SERVER_LOCK (server);
538 if ((result = priv->session_pool))
539 g_object_ref (result);
540 GST_RTSP_SERVER_UNLOCK (server);
546 * gst_rtsp_server_set_mount_points:
547 * @server: a #GstRTSPServer
548 * @mounts: (transfer none): a #GstRTSPMountPoints
550 * configure @mounts to be used as the mount points of @server.
553 gst_rtsp_server_set_mount_points (GstRTSPServer * server,
554 GstRTSPMountPoints * mounts)
556 GstRTSPServerPrivate *priv;
557 GstRTSPMountPoints *old;
559 g_return_if_fail (GST_IS_RTSP_SERVER (server));
564 g_object_ref (mounts);
566 GST_RTSP_SERVER_LOCK (server);
567 old = priv->mount_points;
568 priv->mount_points = mounts;
569 GST_RTSP_SERVER_UNLOCK (server);
572 g_object_unref (old);
577 * gst_rtsp_server_get_mount_points:
578 * @server: a #GstRTSPServer
580 * Get the #GstRTSPMountPoints used as the mount points of @server.
582 * Returns: (transfer full): the #GstRTSPMountPoints of @server. g_object_unref() after
586 gst_rtsp_server_get_mount_points (GstRTSPServer * server)
588 GstRTSPServerPrivate *priv;
589 GstRTSPMountPoints *result;
591 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
595 GST_RTSP_SERVER_LOCK (server);
596 if ((result = priv->mount_points))
597 g_object_ref (result);
598 GST_RTSP_SERVER_UNLOCK (server);
604 * gst_rtsp_server_set_auth:
605 * @server: a #GstRTSPServer
606 * @auth: (transfer none): a #GstRTSPAuth
608 * configure @auth to be used as the authentication manager of @server.
611 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
613 GstRTSPServerPrivate *priv;
616 g_return_if_fail (GST_IS_RTSP_SERVER (server));
623 GST_RTSP_SERVER_LOCK (server);
626 GST_RTSP_SERVER_UNLOCK (server);
629 g_object_unref (old);
634 * gst_rtsp_server_get_auth:
635 * @server: a #GstRTSPServer
637 * Get the #GstRTSPAuth used as the authentication manager of @server.
639 * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
643 gst_rtsp_server_get_auth (GstRTSPServer * server)
645 GstRTSPServerPrivate *priv;
648 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
652 GST_RTSP_SERVER_LOCK (server);
653 if ((result = priv->auth))
654 g_object_ref (result);
655 GST_RTSP_SERVER_UNLOCK (server);
661 * gst_rtsp_server_set_thread_pool:
662 * @server: a #GstRTSPServer
663 * @pool: (transfer none): a #GstRTSPThreadPool
665 * configure @pool to be used as the thread pool of @server.
668 gst_rtsp_server_set_thread_pool (GstRTSPServer * server,
669 GstRTSPThreadPool * pool)
671 GstRTSPServerPrivate *priv;
672 GstRTSPThreadPool *old;
674 g_return_if_fail (GST_IS_RTSP_SERVER (server));
681 GST_RTSP_SERVER_LOCK (server);
682 old = priv->thread_pool;
683 priv->thread_pool = pool;
684 GST_RTSP_SERVER_UNLOCK (server);
687 g_object_unref (old);
691 * gst_rtsp_server_get_thread_pool:
692 * @server: a #GstRTSPServer
694 * Get the #GstRTSPThreadPool used as the thread pool of @server.
696 * Returns: (transfer full): the #GstRTSPThreadPool of @server. g_object_unref() after
700 gst_rtsp_server_get_thread_pool (GstRTSPServer * server)
702 GstRTSPServerPrivate *priv;
703 GstRTSPThreadPool *result;
705 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
709 GST_RTSP_SERVER_LOCK (server);
710 if ((result = priv->thread_pool))
711 g_object_ref (result);
712 GST_RTSP_SERVER_UNLOCK (server);
718 gst_rtsp_server_get_property (GObject * object, guint propid,
719 GValue * value, GParamSpec * pspec)
721 GstRTSPServer *server = GST_RTSP_SERVER (object);
725 g_value_take_string (value, gst_rtsp_server_get_address (server));
728 g_value_take_string (value, gst_rtsp_server_get_service (server));
730 case PROP_BOUND_PORT:
731 g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
734 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
736 case PROP_SESSION_POOL:
737 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
739 case PROP_MOUNT_POINTS:
740 g_value_take_object (value, gst_rtsp_server_get_mount_points (server));
743 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
748 gst_rtsp_server_set_property (GObject * object, guint propid,
749 const GValue * value, GParamSpec * pspec)
751 GstRTSPServer *server = GST_RTSP_SERVER (object);
755 gst_rtsp_server_set_address (server, g_value_get_string (value));
758 gst_rtsp_server_set_service (server, g_value_get_string (value));
761 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
763 case PROP_SESSION_POOL:
764 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
766 case PROP_MOUNT_POINTS:
767 gst_rtsp_server_set_mount_points (server, g_value_get_object (value));
770 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
775 * gst_rtsp_server_create_socket:
776 * @server: a #GstRTSPServer
777 * @cancellable: (allow-none): a #GCancellable
778 * @error: (out): a #GError
780 * Create a #GSocket for @server. The socket will listen on the
781 * configured service.
783 * Returns: (transfer full): the #GSocket for @server or %NULL when an error
787 gst_rtsp_server_create_socket (GstRTSPServer * server,
788 GCancellable * cancellable, GError ** error)
790 GstRTSPServerPrivate *priv;
791 GSocketConnectable *conn;
792 GSocketAddressEnumerator *enumerator;
793 GSocket *socket = NULL;
795 struct linger linger;
797 GError *sock_error = NULL;
798 GError *bind_error = NULL;
801 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
805 GST_RTSP_SERVER_LOCK (server);
806 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", priv->address,
809 /* resolve the server IP address */
810 port = atoi (priv->service);
811 if (port != 0 || !strcmp (priv->service, "0"))
812 conn = g_network_address_new (priv->address, port);
814 conn = g_network_service_new (priv->service, "tcp", priv->address);
816 enumerator = g_socket_connectable_enumerate (conn);
817 g_object_unref (conn);
819 /* create server socket, we loop through all the addresses until we manage to
820 * create a socket and bind. */
822 GSocketAddress *sockaddr;
825 g_socket_address_enumerator_next (enumerator, cancellable, error);
828 GST_DEBUG_OBJECT (server, "no more addresses %s",
829 *error ? (*error)->message : "");
831 GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
836 /* only keep the first error */
837 socket = g_socket_new (g_socket_address_get_family (sockaddr),
838 G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
839 sock_error ? NULL : &sock_error);
841 if (socket == NULL) {
842 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
843 sock_error->message);
844 g_object_unref (sockaddr);
848 if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
849 /* ask what port the socket has been bound to */
850 if (port == 0 || !strcmp (priv->service, "0")) {
851 GError *addr_error = NULL;
853 g_object_unref (sockaddr);
854 sockaddr = g_socket_get_local_address (socket, &addr_error);
856 if (addr_error != NULL) {
857 GST_DEBUG_OBJECT (server,
858 "failed to get the local address of a bound socket %s",
859 addr_error->message);
860 g_clear_error (&addr_error);
864 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr));
867 g_free (priv->service);
868 priv->service = g_strdup_printf ("%d", port);
870 GST_DEBUG_OBJECT (server, "failed to get the port of a bound socket");
873 g_object_unref (sockaddr);
877 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
878 bind_error->message);
879 g_object_unref (sockaddr);
880 g_object_unref (socket);
883 g_object_unref (enumerator);
888 g_clear_error (&sock_error);
889 g_clear_error (&bind_error);
891 GST_DEBUG_OBJECT (server, "opened sending server socket");
893 /* keep connection alive; avoids SIGPIPE during write */
894 g_socket_set_keepalive (socket, TRUE);
898 /* make sure socket is reset 5 seconds after close. This ensure that we can
899 * reuse the socket quickly while still having a chance to send data to the
903 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
904 (void *) &linger, sizeof (linger)) < 0)
909 /* set the server socket to nonblocking */
910 g_socket_set_blocking (socket, FALSE);
912 /* set listen backlog */
913 g_socket_set_listen_backlog (socket, priv->backlog);
915 if (!g_socket_listen (socket, error))
918 GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
919 socket, priv->backlog);
921 GST_RTSP_SERVER_UNLOCK (server);
928 GST_ERROR_OBJECT (server, "failed to create socket");
935 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
943 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
950 g_object_unref (socket);
954 g_propagate_error (error, sock_error);
956 g_error_free (sock_error);
959 if ((error == NULL) || (*error == NULL))
960 g_propagate_error (error, bind_error);
962 g_error_free (bind_error);
964 GST_RTSP_SERVER_UNLOCK (server);
969 struct _ClientContext
971 GstRTSPServer *server;
972 GstRTSPThread *thread;
973 GstRTSPClient *client;
977 free_client_context (ClientContext * ctx)
979 GST_DEBUG ("free context %p", ctx);
981 GST_RTSP_SERVER_LOCK (ctx->server);
983 gst_rtsp_thread_stop (ctx->thread);
984 GST_RTSP_SERVER_UNLOCK (ctx->server);
986 g_object_unref (ctx->client);
987 g_object_unref (ctx->server);
988 g_slice_free (ClientContext, ctx);
990 return G_SOURCE_REMOVE;
994 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
996 GstRTSPServer *server = ctx->server;
997 GstRTSPServerPrivate *priv = server->priv;
999 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
1001 GST_RTSP_SERVER_LOCK (server);
1002 priv->clients = g_list_remove (priv->clients, ctx);
1003 priv->clients_cookie++;
1004 GST_RTSP_SERVER_UNLOCK (server);
1009 src = g_idle_source_new ();
1010 g_source_set_callback (src, (GSourceFunc) free_client_context, ctx, NULL);
1011 g_source_attach (src, ctx->thread->context);
1012 g_source_unref (src);
1014 free_client_context (ctx);
1018 /* add the client context to the active list of clients, takes ownership
1021 manage_client (GstRTSPServer * server, GstRTSPClient * client)
1023 ClientContext *cctx;
1024 GstRTSPServerPrivate *priv = server->priv;
1025 GMainContext *mainctx = NULL;
1026 GstRTSPContext ctx = { NULL };
1028 GST_DEBUG_OBJECT (server, "manage client %p", client);
1030 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1033 cctx = g_slice_new0 (ClientContext);
1034 cctx->server = g_object_ref (server);
1035 cctx->client = client;
1037 GST_RTSP_SERVER_LOCK (server);
1039 ctx.server = server;
1040 ctx.client = client;
1042 cctx->thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
1043 GST_RTSP_THREAD_TYPE_CLIENT, &ctx);
1045 mainctx = cctx->thread->context;
1048 /* find the context to add the watch */
1049 if ((source = g_main_current_source ()))
1050 mainctx = g_source_get_context (source);
1053 g_signal_connect (client, "closed", (GCallback) unmanage_client, cctx);
1054 priv->clients = g_list_prepend (priv->clients, cctx);
1055 priv->clients_cookie++;
1057 gst_rtsp_client_attach (client, mainctx);
1059 GST_RTSP_SERVER_UNLOCK (server);
1062 static GstRTSPClient *
1063 default_create_client (GstRTSPServer * server)
1065 GstRTSPClient *client;
1066 GstRTSPServerPrivate *priv = server->priv;
1068 /* a new client connected, create a session to handle the client. */
1069 client = gst_rtsp_client_new ();
1071 /* set the session pool that this client should use */
1072 GST_RTSP_SERVER_LOCK (server);
1073 gst_rtsp_client_set_session_pool (client, priv->session_pool);
1074 /* set the mount points that this client should use */
1075 gst_rtsp_client_set_mount_points (client, priv->mount_points);
1076 /* set authentication manager */
1077 gst_rtsp_client_set_auth (client, priv->auth);
1078 /* set threadpool */
1079 gst_rtsp_client_set_thread_pool (client, priv->thread_pool);
1080 GST_RTSP_SERVER_UNLOCK (server);
1086 * gst_rtsp_server_transfer_connection:
1087 * @server: a #GstRTSPServer
1088 * @socket: (transfer full): a network socket
1089 * @ip: the IP address of the remote client
1090 * @port: the port used by the other end
1091 * @initial_buffer: any initial data that was already read from the socket
1093 * Take an existing network socket and use it for an RTSP connection. This
1094 * is used when transferring a socket from an HTTP server which should be used
1095 * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
1096 * that the HTTP server read from the socket while parsing the HTTP header.
1098 * Returns: TRUE if all was ok, FALSE if an error occurred.
1101 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
1102 const gchar * ip, gint port, const gchar * initial_buffer)
1104 GstRTSPClient *client = NULL;
1105 GstRTSPServerClass *klass;
1106 GstRTSPConnection *conn;
1109 klass = GST_RTSP_SERVER_GET_CLASS (server);
1111 if (klass->create_client)
1112 client = klass->create_client (server);
1116 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
1117 initial_buffer, &conn), no_connection);
1118 g_object_unref (socket);
1120 /* set connection on the client now */
1121 gst_rtsp_client_set_connection (client, conn);
1123 /* manage the client connection */
1124 manage_client (server, client);
1131 GST_ERROR_OBJECT (server, "failed to create a client");
1132 g_object_unref (socket);
1137 gchar *str = gst_rtsp_strresult (res);
1138 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
1140 g_object_unref (socket);
1146 * gst_rtsp_server_io_func:
1147 * @socket: a #GSocket
1148 * @condition: the condition on @source
1149 * @server: (transfer none): a #GstRTSPServer
1151 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
1152 * new connection on @socket or @server.
1154 * Returns: TRUE if the source could be connected, FALSE if an error occurred.
1157 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
1158 GstRTSPServer * server)
1160 GstRTSPServerPrivate *priv = server->priv;
1161 GstRTSPClient *client = NULL;
1162 GstRTSPServerClass *klass;
1164 GstRTSPConnection *conn = NULL;
1165 GstRTSPContext ctx = { NULL };
1167 if (condition & G_IO_IN) {
1168 /* a new client connected. */
1169 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, NULL),
1172 ctx.server = server;
1174 ctx.auth = priv->auth;
1175 gst_rtsp_context_push_current (&ctx);
1177 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_CONNECT))
1178 goto connection_refused;
1180 klass = GST_RTSP_SERVER_GET_CLASS (server);
1181 /* a new client connected, create a client object to handle the client. */
1182 if (klass->create_client)
1183 client = klass->create_client (server);
1187 /* set connection on the client now */
1188 gst_rtsp_client_set_connection (client, conn);
1190 /* manage the client connection */
1191 manage_client (server, client);
1193 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
1196 gst_rtsp_context_pop_current (&ctx);
1198 return G_SOURCE_CONTINUE;
1203 gchar *str = gst_rtsp_strresult (res);
1204 GST_ERROR_OBJECT (server, "Could not accept client on socket %p: %s",
1211 GST_ERROR_OBJECT (server, "connection refused");
1212 gst_rtsp_connection_free (conn);
1217 GST_ERROR_OBJECT (server, "failed to create a client");
1218 gst_rtsp_connection_free (conn);
1224 watch_destroyed (GstRTSPServer * server)
1226 GstRTSPServerPrivate *priv = server->priv;
1228 GST_DEBUG_OBJECT (server, "source destroyed");
1230 g_object_unref (priv->socket);
1231 priv->socket = NULL;
1232 g_object_unref (server);
1236 * gst_rtsp_server_create_source:
1237 * @server: a #GstRTSPServer
1238 * @cancellable: (allow-none): a #GCancellable or %NULL.
1239 * @error: (out): a #GError
1241 * Create a #GSource for @server. The new source will have a default
1242 * #GSocketSourceFunc of gst_rtsp_server_io_func().
1244 * @cancellable if not %NULL can be used to cancel the source, which will cause
1245 * the source to trigger, reporting the current condition (which is likely 0
1246 * unless cancellation happened at the same time as a condition change). You can
1247 * check for this in the callback using g_cancellable_is_cancelled().
1249 * This takes a reference on @server until @source is destroyed.
1251 * Returns: (transfer full): the #GSource for @server or %NULL when an error
1252 * occurred. Free with g_source_unref ()
1255 gst_rtsp_server_create_source (GstRTSPServer * server,
1256 GCancellable * cancellable, GError ** error)
1258 GstRTSPServerPrivate *priv;
1259 GSocket *socket, *old;
1262 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1264 priv = server->priv;
1266 socket = gst_rtsp_server_create_socket (server, NULL, error);
1270 GST_RTSP_SERVER_LOCK (server);
1272 priv->socket = g_object_ref (socket);
1273 GST_RTSP_SERVER_UNLOCK (server);
1276 g_object_unref (old);
1278 /* create a watch for reads (new connections) and possible errors */
1279 source = g_socket_create_source (socket, G_IO_IN |
1280 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
1281 g_object_unref (socket);
1283 /* configure the callback */
1284 g_source_set_callback (source,
1285 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
1286 (GDestroyNotify) watch_destroyed);
1292 GST_ERROR_OBJECT (server, "failed to create socket");
1298 * gst_rtsp_server_attach:
1299 * @server: a #GstRTSPServer
1300 * @context: (allow-none): a #GMainContext
1302 * Attaches @server to @context. When the mainloop for @context is run, the
1303 * server will be dispatched. When @context is %NULL, the default context will be
1306 * This function should be called when the server properties and urls are fully
1307 * configured and the server is ready to start.
1309 * This takes a reference on @server until the source is destroyed. Note that
1310 * if @context is not the default main context as returned by
1311 * g_main_context_default() (or %NULL), g_source_remove() cannot be used to
1312 * destroy the source. In that case it is recommended to use
1313 * gst_rtsp_server_create_source() and attach it to @context manually.
1315 * Returns: the ID (greater than 0) for the source within the GMainContext.
1318 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1322 GError *error = NULL;
1324 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1326 source = gst_rtsp_server_create_source (server, NULL, &error);
1330 res = g_source_attach (source, context);
1331 g_source_unref (source);
1338 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1339 g_error_free (error);
1345 * gst_rtsp_server_client_filter:
1346 * @server: a #GstRTSPServer
1347 * @func: (scope call) (allow-none): a callback
1348 * @user_data: user data passed to @func
1350 * Call @func for each client managed by @server. The result value of @func
1351 * determines what happens to the client. @func will be called with @server
1352 * locked so no further actions on @server can be performed from @func.
1354 * If @func returns #GST_RTSP_FILTER_REMOVE, the client will be removed from
1357 * If @func returns #GST_RTSP_FILTER_KEEP, the client will remain in @server.
1359 * If @func returns #GST_RTSP_FILTER_REF, the client will remain in @server but
1360 * will also be added with an additional ref to the result #GList of this
1363 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each client.
1365 * Returns: (element-type GstRTSPClient) (transfer full): a #GList with all
1366 * clients for which @func returned #GST_RTSP_FILTER_REF. After usage, each
1367 * element in the #GList should be unreffed before the list is freed.
1370 gst_rtsp_server_client_filter (GstRTSPServer * server,
1371 GstRTSPServerClientFilterFunc func, gpointer user_data)
1373 GstRTSPServerPrivate *priv;
1374 GList *result, *walk, *next;
1375 GHashTable *visited;
1378 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1380 priv = server->priv;
1384 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
1386 GST_RTSP_SERVER_LOCK (server);
1388 cookie = priv->clients_cookie;
1389 for (walk = priv->clients; walk; walk = next) {
1390 ClientContext *cctx = walk->data;
1391 GstRTSPClient *client = cctx->client;
1392 GstRTSPFilterResult res;
1395 next = g_list_next (walk);
1398 /* only visit each media once */
1399 if (g_hash_table_contains (visited, client))
1402 g_hash_table_add (visited, g_object_ref (client));
1403 GST_RTSP_SERVER_UNLOCK (server);
1405 res = func (server, client, user_data);
1407 GST_RTSP_SERVER_LOCK (server);
1409 res = GST_RTSP_FILTER_REF;
1411 changed = (cookie != priv->clients_cookie);
1414 case GST_RTSP_FILTER_REMOVE:
1415 GST_RTSP_SERVER_UNLOCK (server);
1417 gst_rtsp_client_close (client);
1419 GST_RTSP_SERVER_LOCK (server);
1420 changed |= (cookie != priv->clients_cookie);
1422 case GST_RTSP_FILTER_REF:
1423 result = g_list_prepend (result, g_object_ref (client));
1425 case GST_RTSP_FILTER_KEEP:
1432 GST_RTSP_SERVER_UNLOCK (server);
1435 g_hash_table_unref (visited);