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 g_object_unref (sockaddr);
849 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
850 bind_error->message);
851 g_object_unref (sockaddr);
852 g_object_unref (socket);
855 g_object_unref (enumerator);
860 g_clear_error (&sock_error);
861 g_clear_error (&bind_error);
863 GST_DEBUG_OBJECT (server, "opened sending server socket");
865 /* keep connection alive; avoids SIGPIPE during write */
866 g_socket_set_keepalive (socket, TRUE);
870 /* make sure socket is reset 5 seconds after close. This ensure that we can
871 * reuse the socket quickly while still having a chance to send data to the
875 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
876 (void *) &linger, sizeof (linger)) < 0)
881 /* set the server socket to nonblocking */
882 g_socket_set_blocking (socket, FALSE);
884 /* set listen backlog */
885 g_socket_set_listen_backlog (socket, priv->backlog);
887 if (!g_socket_listen (socket, error))
890 GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
891 socket, priv->backlog);
893 GST_RTSP_SERVER_UNLOCK (server);
900 GST_ERROR_OBJECT (server, "failed to create socket");
907 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
915 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
922 g_object_unref (socket);
926 g_propagate_error (error, sock_error);
928 g_error_free (sock_error);
931 if ((error == NULL) || (*error == NULL))
932 g_propagate_error (error, bind_error);
934 g_error_free (bind_error);
936 GST_RTSP_SERVER_UNLOCK (server);
941 struct _ClientContext
943 GstRTSPServer *server;
944 GstRTSPThread *thread;
945 GstRTSPClient *client;
949 free_client_context (ClientContext * ctx)
951 GST_RTSP_SERVER_LOCK (ctx->server);
953 gst_rtsp_thread_stop (ctx->thread);
954 GST_RTSP_SERVER_UNLOCK (ctx->server);
956 g_object_unref (ctx->client);
957 g_object_unref (ctx->server);
958 g_slice_free (ClientContext, ctx);
960 return G_SOURCE_REMOVE;
964 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
966 GstRTSPServer *server = ctx->server;
967 GstRTSPServerPrivate *priv = server->priv;
969 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
971 GST_RTSP_SERVER_LOCK (server);
972 priv->clients = g_list_remove (priv->clients, ctx);
973 GST_RTSP_SERVER_UNLOCK (server);
978 src = g_idle_source_new ();
979 g_source_set_callback (src, (GSourceFunc) free_client_context, ctx, NULL);
980 g_source_attach (src, ctx->thread->context);
981 g_source_unref (src);
983 free_client_context (ctx);
987 /* add the client context to the active list of clients, takes ownership
990 manage_client (GstRTSPServer * server, GstRTSPClient * client)
993 GstRTSPServerPrivate *priv = server->priv;
994 GMainContext *mainctx = NULL;
995 GstRTSPClientState state = { NULL };
997 GST_DEBUG_OBJECT (server, "manage client %p", client);
999 ctx = g_slice_new0 (ClientContext);
1000 ctx->server = g_object_ref (server);
1001 ctx->client = client;
1003 GST_RTSP_SERVER_LOCK (server);
1005 state.server = server;
1006 state.client = client;
1008 ctx->thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
1009 GST_RTSP_THREAD_TYPE_CLIENT, &state);
1011 mainctx = ctx->thread->context;
1014 /* find the context to add the watch */
1015 if ((source = g_main_current_source ()))
1016 mainctx = g_source_get_context (source);
1019 g_signal_connect (client, "closed", (GCallback) unmanage_client, ctx);
1020 priv->clients = g_list_prepend (priv->clients, ctx);
1022 gst_rtsp_client_attach (client, mainctx);
1024 GST_RTSP_SERVER_UNLOCK (server);
1027 static GstRTSPClient *
1028 default_create_client (GstRTSPServer * server)
1030 GstRTSPClient *client;
1031 GstRTSPServerPrivate *priv = server->priv;
1033 /* a new client connected, create a session to handle the client. */
1034 client = gst_rtsp_client_new ();
1036 /* set the session pool that this client should use */
1037 GST_RTSP_SERVER_LOCK (server);
1038 gst_rtsp_client_set_session_pool (client, priv->session_pool);
1039 /* set the mount points that this client should use */
1040 gst_rtsp_client_set_mount_points (client, priv->mount_points);
1041 /* set authentication manager */
1042 gst_rtsp_client_set_auth (client, priv->auth);
1043 /* set threadpool */
1044 gst_rtsp_client_set_thread_pool (client, priv->thread_pool);
1045 GST_RTSP_SERVER_UNLOCK (server);
1051 * gst_rtsp_server_transfer_connection:
1052 * @server: a #GstRTSPServer
1053 * @socket: a network socket
1054 * @ip: the IP address of the remote client
1055 * @port: the port used by the other end
1056 * @initial_buffer: any initial data that was already read from the socket
1058 * Take an existing network socket and use it for an RTSP connection. This
1059 * is used when transferring a socket from an HTTP server which should be used
1060 * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
1061 * that the HTTP server read from the socket while parsing the HTTP header.
1063 * Returns: TRUE if all was ok, FALSE if an error occured.
1066 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
1067 const gchar * ip, gint port, const gchar * initial_buffer)
1069 GstRTSPClient *client = NULL;
1070 GstRTSPServerClass *klass;
1071 GstRTSPConnection *conn;
1074 klass = GST_RTSP_SERVER_GET_CLASS (server);
1076 if (klass->create_client)
1077 client = klass->create_client (server);
1081 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
1082 initial_buffer, &conn), no_connection);
1084 /* set connection on the client now */
1085 gst_rtsp_client_set_connection (client, conn);
1087 /* manage the client connection */
1088 manage_client (server, client);
1090 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1098 GST_ERROR_OBJECT (server, "failed to create a client");
1103 gchar *str = gst_rtsp_strresult (res);
1104 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
1111 * gst_rtsp_server_io_func:
1112 * @socket: a #GSocket
1113 * @condition: the condition on @source
1114 * @server: a #GstRTSPServer
1116 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
1117 * new connection on @socket or @server.
1119 * Returns: TRUE if the source could be connected, FALSE if an error occured.
1122 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
1123 GstRTSPServer * server)
1125 GstRTSPServerPrivate *priv = server->priv;
1126 GstRTSPClient *client = NULL;
1127 GstRTSPServerClass *klass;
1129 GstRTSPConnection *conn = NULL;
1130 GstRTSPClientState state = { NULL };
1132 if (condition & G_IO_IN) {
1133 /* a new client connected. */
1134 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, NULL),
1137 state.server = server;
1139 state.auth = priv->auth;
1140 gst_rtsp_client_state_push_current (&state);
1142 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_CONNECT))
1143 goto connection_refused;
1145 klass = GST_RTSP_SERVER_GET_CLASS (server);
1146 /* a new client connected, create a client object to handle the client. */
1147 if (klass->create_client)
1148 client = klass->create_client (server);
1152 /* set connection on the client now */
1153 gst_rtsp_client_set_connection (client, conn);
1155 /* manage the client connection */
1156 manage_client (server, client);
1158 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1161 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
1164 gst_rtsp_client_state_pop_current (&state);
1166 return G_SOURCE_CONTINUE;
1171 gchar *str = gst_rtsp_strresult (res);
1172 GST_ERROR_OBJECT (server, "Could not accept client on socket %p: %s",
1179 GST_ERROR_OBJECT (server, "connection refused");
1180 gst_rtsp_connection_free (conn);
1185 GST_ERROR_OBJECT (server, "failed to create a client");
1186 gst_rtsp_connection_free (conn);
1192 watch_destroyed (GstRTSPServer * server)
1194 GstRTSPServerPrivate *priv = server->priv;
1196 GST_DEBUG_OBJECT (server, "source destroyed");
1198 g_object_unref (priv->socket);
1199 priv->socket = NULL;
1200 g_object_unref (server);
1204 * gst_rtsp_server_create_source:
1205 * @server: a #GstRTSPServer
1206 * @cancellable: a #GCancellable or %NULL.
1209 * Create a #GSource for @server. The new source will have a default
1210 * #GSocketSourceFunc of gst_rtsp_server_io_func().
1212 * @cancellable if not NULL can be used to cancel the source, which will cause
1213 * the source to trigger, reporting the current condition (which is likely 0
1214 * unless cancellation happened at the same time as a condition change). You can
1215 * check for this in the callback using g_cancellable_is_cancelled().
1217 * Returns: the #GSource for @server or NULL when an error occured. Free with
1221 gst_rtsp_server_create_source (GstRTSPServer * server,
1222 GCancellable * cancellable, GError ** error)
1224 GstRTSPServerPrivate *priv;
1225 GSocket *socket, *old;
1228 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1230 priv = server->priv;
1232 socket = gst_rtsp_server_create_socket (server, NULL, error);
1236 GST_RTSP_SERVER_LOCK (server);
1238 priv->socket = g_object_ref (socket);
1239 GST_RTSP_SERVER_UNLOCK (server);
1242 g_object_unref (old);
1244 /* create a watch for reads (new connections) and possible errors */
1245 source = g_socket_create_source (socket, G_IO_IN |
1246 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
1247 g_object_unref (socket);
1249 /* configure the callback */
1250 g_source_set_callback (source,
1251 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
1252 (GDestroyNotify) watch_destroyed);
1258 GST_ERROR_OBJECT (server, "failed to create socket");
1264 * gst_rtsp_server_attach:
1265 * @server: a #GstRTSPServer
1266 * @context: (allow-none): a #GMainContext
1268 * Attaches @server to @context. When the mainloop for @context is run, the
1269 * server will be dispatched. When @context is NULL, the default context will be
1272 * This function should be called when the server properties and urls are fully
1273 * configured and the server is ready to start.
1275 * Returns: the ID (greater than 0) for the source within the GMainContext.
1278 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1282 GError *error = NULL;
1284 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1286 source = gst_rtsp_server_create_source (server, NULL, &error);
1290 res = g_source_attach (source, context);
1291 g_source_unref (source);
1298 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1299 g_error_free (error);