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.
23 #include "rtsp-server.h"
24 #include "rtsp-client.h"
26 #define GST_RTSP_SERVER_GET_PRIVATE(obj) \
27 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SERVER, GstRTSPServerPrivate))
29 #define GST_RTSP_SERVER_GET_LOCK(server) (&(GST_RTSP_SERVER_CAST(server)->priv->lock))
30 #define GST_RTSP_SERVER_LOCK(server) (g_mutex_lock(GST_RTSP_SERVER_GET_LOCK(server)))
31 #define GST_RTSP_SERVER_UNLOCK(server) (g_mutex_unlock(GST_RTSP_SERVER_GET_LOCK(server)))
33 struct _GstRTSPServerPrivate
35 GMutex lock; /* protects everything in this struct */
37 /* server information */
45 /* sessions on this server */
46 GstRTSPSessionPool *session_pool;
48 /* mount points for this server */
49 GstRTSPMountPoints *mount_points;
51 /* authentication manager */
54 /* the TLS certificate */
55 GTlsCertificate *certificate;
57 /* the clients that are connected */
59 GQueue loops; /* the main loops used in the threads */
62 #define DEFAULT_ADDRESS "0.0.0.0"
63 #define DEFAULT_BOUND_PORT -1
64 /* #define DEFAULT_ADDRESS "::0" */
65 #define DEFAULT_SERVICE "8554"
66 #define DEFAULT_BACKLOG 5
67 #define DEFAULT_MAX_THREADS 0
69 /* Define to use the SO_LINGER option so that the server sockets can be resused
70 * sooner. Disabled for now because it is not very well implemented by various
71 * OSes and it causes clients to fail to read the TEARDOWN response. */
90 SIGNAL_CLIENT_CONNECTED,
94 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
96 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
97 #define GST_CAT_DEFAULT rtsp_server_debug
99 typedef struct _ClientContext ClientContext;
100 typedef struct _Loop Loop;
102 static guint gst_rtsp_server_signals[SIGNAL_LAST] = { 0 };
104 static void gst_rtsp_server_get_property (GObject * object, guint propid,
105 GValue * value, GParamSpec * pspec);
106 static void gst_rtsp_server_set_property (GObject * object, guint propid,
107 const GValue * value, GParamSpec * pspec);
108 static void gst_rtsp_server_finalize (GObject * object);
110 static gpointer do_loop (Loop * loop);
111 static GstRTSPClient *default_create_client (GstRTSPServer * server);
112 static gboolean default_setup_connection (GstRTSPServer * server,
113 GstRTSPClient * client, GstRTSPConnection * conn);
116 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
118 GObjectClass *gobject_class;
120 g_type_class_add_private (klass, sizeof (GstRTSPServerPrivate));
122 gobject_class = G_OBJECT_CLASS (klass);
124 gobject_class->get_property = gst_rtsp_server_get_property;
125 gobject_class->set_property = gst_rtsp_server_set_property;
126 gobject_class->finalize = gst_rtsp_server_finalize;
129 * GstRTSPServer::address:
131 * The address of the server. This is the address where the server will
134 g_object_class_install_property (gobject_class, PROP_ADDRESS,
135 g_param_spec_string ("address", "Address",
136 "The address the server uses to listen on", DEFAULT_ADDRESS,
137 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
139 * GstRTSPServer::service:
141 * The service of the server. This is either a string with the service name or
142 * a port number (as a string) the server will listen on.
144 g_object_class_install_property (gobject_class, PROP_SERVICE,
145 g_param_spec_string ("service", "Service",
146 "The service or port number the server uses to listen on",
147 DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
149 * GstRTSPServer::bound-port:
151 * The actual port the server is listening on. Can be used to retrieve the
152 * port number when the server is started on port 0, which means bind to a
153 * random port. Set to -1 if the server has not been bound yet.
155 g_object_class_install_property (gobject_class, PROP_BOUND_PORT,
156 g_param_spec_int ("bound-port", "Bound port",
157 "The port number the server is listening on",
158 -1, G_MAXUINT16, DEFAULT_BOUND_PORT,
159 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
161 * GstRTSPServer::backlog:
163 * The backlog argument defines the maximum length to which the queue of
164 * pending connections for the server may grow. If a connection request arrives
165 * when the queue is full, the client may receive an error with an indication of
166 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
167 * request may be ignored so that a later reattempt at connection succeeds.
169 g_object_class_install_property (gobject_class, PROP_BACKLOG,
170 g_param_spec_int ("backlog", "Backlog",
171 "The maximum length to which the queue "
172 "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
173 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 * GstRTSPServer::session-pool:
177 * The session pool of the server. By default each server has a separate
178 * session pool but sessions can be shared between servers by setting the same
179 * session pool on multiple servers.
181 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
182 g_param_spec_object ("session-pool", "Session Pool",
183 "The session pool to use for client session",
184 GST_TYPE_RTSP_SESSION_POOL,
185 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187 * GstRTSPServer::mount-points:
189 * The mount points to use for this server. By default the server has no
190 * mount points and thus cannot map urls to media streams.
192 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
193 g_param_spec_object ("mount-points", "Mount Points",
194 "The mount points to use for client session",
195 GST_TYPE_RTSP_MOUNT_POINTS,
196 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 * GstRTSPServer::max-threads:
200 * The maximum amount of threads to use for client connections. A value of
201 * 0 means to use only the mainloop, -1 means an unlimited amount of
204 g_object_class_install_property (gobject_class, PROP_MAX_THREADS,
205 g_param_spec_int ("max-threads", "Max Threads",
206 "The maximum amount of threads to use for client connections "
207 "(0 = only mainloop, -1 = unlimited)", -1, G_MAXINT,
208 DEFAULT_MAX_THREADS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210 gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED] =
211 g_signal_new ("client-connected", G_TYPE_FROM_CLASS (gobject_class),
212 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPServerClass, client_connected),
213 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
214 gst_rtsp_client_get_type ());
216 klass->create_client = default_create_client;
217 klass->setup_connection = default_setup_connection;
219 klass->pool = g_thread_pool_new ((GFunc) do_loop, klass, -1, FALSE, NULL);
221 GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
225 gst_rtsp_server_init (GstRTSPServer * server)
227 GstRTSPServerPrivate *priv = GST_RTSP_SERVER_GET_PRIVATE (server);
231 g_mutex_init (&priv->lock);
232 priv->address = g_strdup (DEFAULT_ADDRESS);
233 priv->service = g_strdup (DEFAULT_SERVICE);
235 priv->backlog = DEFAULT_BACKLOG;
236 priv->session_pool = gst_rtsp_session_pool_new ();
237 priv->mount_points = gst_rtsp_mount_points_new ();
238 priv->max_threads = DEFAULT_MAX_THREADS;
239 g_queue_init (&priv->loops);
243 gst_rtsp_server_finalize (GObject * object)
245 GstRTSPServer *server = GST_RTSP_SERVER (object);
246 GstRTSPServerPrivate *priv = server->priv;
248 GST_DEBUG_OBJECT (server, "finalize server");
250 g_free (priv->address);
251 g_free (priv->service);
254 g_object_unref (priv->socket);
256 g_object_unref (priv->session_pool);
257 g_object_unref (priv->mount_points);
260 g_object_unref (priv->auth);
262 if (priv->certificate)
263 g_object_unref (priv->certificate);
265 g_mutex_clear (&priv->lock);
267 G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
271 * gst_rtsp_server_new:
273 * Create a new #GstRTSPServer instance.
276 gst_rtsp_server_new (void)
278 GstRTSPServer *result;
280 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
286 * gst_rtsp_server_set_address:
287 * @server: a #GstRTSPServer
288 * @address: the address
290 * Configure @server to accept connections on the given address.
292 * This function must be called before the server is bound.
295 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
297 GstRTSPServerPrivate *priv;
299 g_return_if_fail (GST_IS_RTSP_SERVER (server));
300 g_return_if_fail (address != NULL);
304 GST_RTSP_SERVER_LOCK (server);
305 g_free (priv->address);
306 priv->address = g_strdup (address);
307 GST_RTSP_SERVER_UNLOCK (server);
311 * gst_rtsp_server_get_address:
312 * @server: a #GstRTSPServer
314 * Get the address on which the server will accept connections.
316 * Returns: the server address. g_free() after usage.
319 gst_rtsp_server_get_address (GstRTSPServer * server)
321 GstRTSPServerPrivate *priv;
324 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
328 GST_RTSP_SERVER_LOCK (server);
329 result = g_strdup (priv->address);
330 GST_RTSP_SERVER_UNLOCK (server);
336 * gst_rtsp_server_get_bound_port:
337 * @server: a #GstRTSPServer
339 * Get the port number where the server was bound to.
341 * Returns: the port number
344 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
346 GstRTSPServerPrivate *priv;
347 GSocketAddress *address;
350 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
354 GST_RTSP_SERVER_LOCK (server);
355 if (priv->socket == NULL)
358 address = g_socket_get_local_address (priv->socket, NULL);
359 result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
360 g_object_unref (address);
363 GST_RTSP_SERVER_UNLOCK (server);
369 * gst_rtsp_server_set_service:
370 * @server: a #GstRTSPServer
371 * @service: the service
373 * Configure @server to accept connections on the given service.
374 * @service should be a string containing the service name (see services(5)) or
375 * a string containing a port number between 1 and 65535.
377 * This function must be called before the server is bound.
380 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
382 GstRTSPServerPrivate *priv;
384 g_return_if_fail (GST_IS_RTSP_SERVER (server));
385 g_return_if_fail (service != NULL);
389 GST_RTSP_SERVER_LOCK (server);
390 g_free (priv->service);
391 priv->service = g_strdup (service);
392 GST_RTSP_SERVER_UNLOCK (server);
396 * gst_rtsp_server_get_service:
397 * @server: a #GstRTSPServer
399 * Get the service on which the server will accept connections.
401 * Returns: the service. use g_free() after usage.
404 gst_rtsp_server_get_service (GstRTSPServer * server)
406 GstRTSPServerPrivate *priv;
409 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
413 GST_RTSP_SERVER_LOCK (server);
414 result = g_strdup (priv->service);
415 GST_RTSP_SERVER_UNLOCK (server);
421 * gst_rtsp_server_set_backlog:
422 * @server: a #GstRTSPServer
423 * @backlog: the backlog
425 * configure the maximum amount of requests that may be queued for the
428 * This function must be called before the server is bound.
431 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
433 GstRTSPServerPrivate *priv;
435 g_return_if_fail (GST_IS_RTSP_SERVER (server));
439 GST_RTSP_SERVER_LOCK (server);
440 priv->backlog = backlog;
441 GST_RTSP_SERVER_UNLOCK (server);
445 * gst_rtsp_server_get_backlog:
446 * @server: a #GstRTSPServer
448 * The maximum amount of queued requests for the server.
450 * Returns: the server backlog.
453 gst_rtsp_server_get_backlog (GstRTSPServer * server)
455 GstRTSPServerPrivate *priv;
458 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
462 GST_RTSP_SERVER_LOCK (server);
463 result = priv->backlog;
464 GST_RTSP_SERVER_UNLOCK (server);
470 * gst_rtsp_server_set_session_pool:
471 * @server: a #GstRTSPServer
472 * @pool: a #GstRTSPSessionPool
474 * configure @pool to be used as the session pool of @server.
477 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
478 GstRTSPSessionPool * pool)
480 GstRTSPServerPrivate *priv;
481 GstRTSPSessionPool *old;
483 g_return_if_fail (GST_IS_RTSP_SERVER (server));
490 GST_RTSP_SERVER_LOCK (server);
491 old = priv->session_pool;
492 priv->session_pool = pool;
493 GST_RTSP_SERVER_UNLOCK (server);
496 g_object_unref (old);
500 * gst_rtsp_server_get_session_pool:
501 * @server: a #GstRTSPServer
503 * Get the #GstRTSPSessionPool used as the session pool of @server.
505 * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
509 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
511 GstRTSPServerPrivate *priv;
512 GstRTSPSessionPool *result;
514 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
518 GST_RTSP_SERVER_LOCK (server);
519 if ((result = priv->session_pool))
520 g_object_ref (result);
521 GST_RTSP_SERVER_UNLOCK (server);
527 * gst_rtsp_server_set_mount_points:
528 * @server: a #GstRTSPServer
529 * @mounts: a #GstRTSPMountPoints
531 * configure @mounts to be used as the mount points of @server.
534 gst_rtsp_server_set_mount_points (GstRTSPServer * server,
535 GstRTSPMountPoints * mounts)
537 GstRTSPServerPrivate *priv;
538 GstRTSPMountPoints *old;
540 g_return_if_fail (GST_IS_RTSP_SERVER (server));
545 g_object_ref (mounts);
547 GST_RTSP_SERVER_LOCK (server);
548 old = priv->mount_points;
549 priv->mount_points = mounts;
550 GST_RTSP_SERVER_UNLOCK (server);
553 g_object_unref (old);
558 * gst_rtsp_server_get_mount_points:
559 * @server: a #GstRTSPServer
561 * Get the #GstRTSPMountPoints used as the mount points of @server.
563 * Returns: (transfer full): the #GstRTSPMountPoints of @server. g_object_unref() after
567 gst_rtsp_server_get_mount_points (GstRTSPServer * server)
569 GstRTSPServerPrivate *priv;
570 GstRTSPMountPoints *result;
572 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
576 GST_RTSP_SERVER_LOCK (server);
577 if ((result = priv->mount_points))
578 g_object_ref (result);
579 GST_RTSP_SERVER_UNLOCK (server);
585 * gst_rtsp_server_set_auth:
586 * @server: a #GstRTSPServer
587 * @auth: a #GstRTSPAuth
589 * configure @auth to be used as the authentication manager of @server.
592 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
594 GstRTSPServerPrivate *priv;
597 g_return_if_fail (GST_IS_RTSP_SERVER (server));
604 GST_RTSP_SERVER_LOCK (server);
607 GST_RTSP_SERVER_UNLOCK (server);
610 g_object_unref (old);
615 * gst_rtsp_server_get_auth:
616 * @server: a #GstRTSPServer
618 * Get the #GstRTSPAuth used as the authentication manager of @server.
620 * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
624 gst_rtsp_server_get_auth (GstRTSPServer * server)
626 GstRTSPServerPrivate *priv;
629 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
633 GST_RTSP_SERVER_LOCK (server);
634 if ((result = priv->auth))
635 g_object_ref (result);
636 GST_RTSP_SERVER_UNLOCK (server);
642 * gst_rtsp_server_set_max_threads:
643 * @server: a #GstRTSPServer
644 * @max_threads: maximum threads
646 * Set the maximum threads used by the server to handle client requests.
647 * A value of 0 will use the server mainloop, a value of -1 will use an
648 * unlimited number of threads.
651 gst_rtsp_server_set_max_threads (GstRTSPServer * server, gint max_threads)
653 GstRTSPServerPrivate *priv;
655 g_return_if_fail (GST_IS_RTSP_SERVER (server));
659 GST_RTSP_SERVER_LOCK (server);
660 priv->max_threads = max_threads;
661 GST_RTSP_SERVER_UNLOCK (server);
665 * gst_rtsp_server_get_max_threads:
666 * @server: a #GstRTSPServer
668 * Get the maximum number of threads used for client connections.
669 * See gst_rtsp_server_set_max_threads().
671 * Returns: the maximum number of threads.
674 gst_rtsp_server_get_max_threads (GstRTSPServer * server)
676 GstRTSPServerPrivate *priv;
679 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
683 GST_RTSP_SERVER_LOCK (server);
684 res = priv->max_threads;
685 GST_RTSP_SERVER_UNLOCK (server);
691 * gst_rtsp_server_set_tls_certificate:
692 * @server: a #GstRTSPServer
693 * @cert: (allow none): a #GTlsCertificate
695 * Set the TLS certificate for the server. Client connections will only
696 * be accepted when TLS is negotiated.
699 gst_rtsp_server_set_tls_certificate (GstRTSPServer * server,
700 GTlsCertificate * cert)
702 GstRTSPServerPrivate *priv;
703 GTlsCertificate *old;
705 g_return_if_fail (GST_IS_RTSP_SERVER (server));
712 GST_RTSP_SERVER_LOCK (server);
713 old = priv->certificate;
714 priv->certificate = cert;
715 GST_RTSP_SERVER_UNLOCK (server);
718 g_object_unref (old);
722 * gst_rtsp_server_get_tls_certificate:
723 * @server: a #GstRTSPServer
725 * Get the #GTlsCertificate used for negotiating TLS @server.
727 * Returns: (transfer full): the #GTlsCertificate of @server. g_object_unref() after
731 gst_rtsp_server_get_tls_certificate (GstRTSPServer * server)
733 GstRTSPServerPrivate *priv;
734 GTlsCertificate *result;
736 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
740 GST_RTSP_SERVER_LOCK (server);
741 if ((result = priv->certificate))
742 g_object_ref (result);
743 GST_RTSP_SERVER_UNLOCK (server);
749 gst_rtsp_server_get_property (GObject * object, guint propid,
750 GValue * value, GParamSpec * pspec)
752 GstRTSPServer *server = GST_RTSP_SERVER (object);
756 g_value_take_string (value, gst_rtsp_server_get_address (server));
759 g_value_take_string (value, gst_rtsp_server_get_service (server));
761 case PROP_BOUND_PORT:
762 g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
765 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
767 case PROP_SESSION_POOL:
768 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
770 case PROP_MOUNT_POINTS:
771 g_value_take_object (value, gst_rtsp_server_get_mount_points (server));
773 case PROP_MAX_THREADS:
774 g_value_set_int (value, gst_rtsp_server_get_max_threads (server));
777 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
782 gst_rtsp_server_set_property (GObject * object, guint propid,
783 const GValue * value, GParamSpec * pspec)
785 GstRTSPServer *server = GST_RTSP_SERVER (object);
789 gst_rtsp_server_set_address (server, g_value_get_string (value));
792 gst_rtsp_server_set_service (server, g_value_get_string (value));
795 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
797 case PROP_SESSION_POOL:
798 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
800 case PROP_MOUNT_POINTS:
801 gst_rtsp_server_set_mount_points (server, g_value_get_object (value));
803 case PROP_MAX_THREADS:
804 gst_rtsp_server_set_max_threads (server, g_value_get_int (value));
807 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
812 * gst_rtsp_server_create_socket:
813 * @server: a #GstRTSPServer
814 * @cancellable: a #GCancellable
817 * Create a #GSocket for @server. The socket will listen on the
818 * configured service.
820 * Returns: (transfer full): the #GSocket for @server or NULL when an error occured.
823 gst_rtsp_server_create_socket (GstRTSPServer * server,
824 GCancellable * cancellable, GError ** error)
826 GstRTSPServerPrivate *priv;
827 GSocketConnectable *conn;
828 GSocketAddressEnumerator *enumerator;
829 GSocket *socket = NULL;
831 struct linger linger;
833 GError *sock_error = NULL;
834 GError *bind_error = NULL;
837 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
841 GST_RTSP_SERVER_LOCK (server);
842 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", priv->address,
845 /* resolve the server IP address */
846 port = atoi (priv->service);
847 if (port != 0 || !strcmp (priv->service, "0"))
848 conn = g_network_address_new (priv->address, port);
850 conn = g_network_service_new (priv->service, "tcp", priv->address);
852 enumerator = g_socket_connectable_enumerate (conn);
853 g_object_unref (conn);
855 /* create server socket, we loop through all the addresses until we manage to
856 * create a socket and bind. */
858 GSocketAddress *sockaddr;
861 g_socket_address_enumerator_next (enumerator, cancellable, error);
864 GST_DEBUG_OBJECT (server, "no more addresses %s",
865 *error ? (*error)->message : "");
867 GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
872 /* only keep the first error */
873 socket = g_socket_new (g_socket_address_get_family (sockaddr),
874 G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
875 sock_error ? NULL : &sock_error);
877 if (socket == NULL) {
878 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
879 sock_error->message);
880 g_object_unref (sockaddr);
884 if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
885 g_object_unref (sockaddr);
889 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
890 bind_error->message);
891 g_object_unref (sockaddr);
892 g_object_unref (socket);
895 g_object_unref (enumerator);
900 g_clear_error (&sock_error);
901 g_clear_error (&bind_error);
903 GST_DEBUG_OBJECT (server, "opened sending server socket");
905 /* keep connection alive; avoids SIGPIPE during write */
906 g_socket_set_keepalive (socket, TRUE);
910 /* make sure socket is reset 5 seconds after close. This ensure that we can
911 * reuse the socket quickly while still having a chance to send data to the
915 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
916 (void *) &linger, sizeof (linger)) < 0)
921 /* set the server socket to nonblocking */
922 g_socket_set_blocking (socket, FALSE);
924 /* set listen backlog */
925 g_socket_set_listen_backlog (socket, priv->backlog);
927 if (!g_socket_listen (socket, error))
930 GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
931 socket, priv->backlog);
933 GST_RTSP_SERVER_UNLOCK (server);
940 GST_ERROR_OBJECT (server, "failed to create socket");
947 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
955 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
962 g_object_unref (socket);
966 g_propagate_error (error, sock_error);
968 g_error_free (sock_error);
971 if ((error == NULL) || (*error == NULL))
972 g_propagate_error (error, bind_error);
974 g_error_free (bind_error);
976 GST_RTSP_SERVER_UNLOCK (server);
985 GstRTSPServer *server;
987 GMainContext *mainctx;
990 /* must be called with the lock held */
992 loop_unref (Loop * loop)
994 GstRTSPServer *server = loop->server;
995 GstRTSPServerPrivate *priv = server->priv;
999 if (loop->refcnt <= 0) {
1000 g_queue_remove (&priv->loops, loop);
1001 g_main_loop_quit (loop->mainloop);
1005 struct _ClientContext
1007 GstRTSPServer *server;
1009 GstRTSPClient *client;
1013 free_client_context (ClientContext * ctx)
1015 GST_RTSP_SERVER_LOCK (ctx->server);
1017 loop_unref (ctx->loop);
1018 GST_RTSP_SERVER_UNLOCK (ctx->server);
1020 g_object_unref (ctx->client);
1021 g_slice_free (ClientContext, ctx);
1023 return G_SOURCE_REMOVE;
1027 do_loop (Loop * loop)
1029 GST_INFO ("enter mainloop");
1030 g_main_loop_run (loop->mainloop);
1031 GST_INFO ("exit mainloop");
1033 g_main_context_unref (loop->mainctx);
1034 g_main_loop_unref (loop->mainloop);
1035 g_object_unref (loop->server);
1036 g_slice_free (Loop, loop);
1041 /* Must be called with lock held */
1044 gst_rtsp_server_get_main_loop (GstRTSPServer * server)
1046 GstRTSPServerPrivate *priv = server->priv;
1049 if (priv->max_threads > 0 &&
1050 g_queue_get_length (&priv->loops) >= priv->max_threads) {
1051 loop = g_queue_pop_head (&priv->loops);
1054 GstRTSPServerClass *klass = GST_RTSP_SERVER_GET_CLASS (server);
1056 loop = g_slice_new0 (Loop);
1058 loop->server = g_object_ref (server);
1059 loop->mainctx = g_main_context_new ();
1060 loop->mainloop = g_main_loop_new (loop->mainctx, FALSE);
1062 g_thread_pool_push (klass->pool, loop, NULL);
1065 g_queue_push_tail (&priv->loops, loop);
1071 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
1073 GstRTSPServer *server = ctx->server;
1074 GstRTSPServerPrivate *priv = server->priv;
1076 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
1078 g_object_ref (server);
1080 GST_RTSP_SERVER_LOCK (server);
1081 priv->clients = g_list_remove (priv->clients, ctx);
1082 GST_RTSP_SERVER_UNLOCK (server);
1087 src = g_idle_source_new ();
1088 g_source_set_callback (src, (GSourceFunc) free_client_context, ctx, NULL);
1089 g_source_attach (src, ctx->loop->mainctx);
1090 g_source_unref (src);
1092 free_client_context (ctx);
1095 g_object_unref (server);
1098 /* add the client context to the active list of clients, takes ownership
1101 manage_client (GstRTSPServer * server, GstRTSPClient * client)
1104 GstRTSPServerPrivate *priv = server->priv;
1105 GMainContext *mainctx;
1107 GST_DEBUG_OBJECT (server, "manage client %p", client);
1109 ctx = g_slice_new0 (ClientContext);
1110 ctx->server = server;
1111 ctx->client = client;
1113 GST_RTSP_SERVER_LOCK (server);
1114 if (priv->max_threads == 0) {
1117 /* find the context to add the watch */
1118 if ((source = g_main_current_source ()))
1119 mainctx = g_source_get_context (source);
1123 ctx->loop = gst_rtsp_server_get_main_loop (server);
1124 mainctx = ctx->loop->mainctx;
1127 g_signal_connect (client, "closed", (GCallback) unmanage_client, ctx);
1128 priv->clients = g_list_prepend (priv->clients, ctx);
1130 gst_rtsp_client_attach (client, mainctx);
1132 GST_RTSP_SERVER_UNLOCK (server);
1135 static GstRTSPClient *
1136 default_create_client (GstRTSPServer * server)
1138 GstRTSPClient *client;
1139 GstRTSPServerPrivate *priv = server->priv;
1141 /* a new client connected, create a session to handle the client. */
1142 client = gst_rtsp_client_new ();
1144 /* set the session pool that this client should use */
1145 GST_RTSP_SERVER_LOCK (server);
1146 gst_rtsp_client_set_session_pool (client, priv->session_pool);
1147 /* set the mount points that this client should use */
1148 gst_rtsp_client_set_mount_points (client, priv->mount_points);
1149 /* set authentication manager */
1150 gst_rtsp_client_set_auth (client, priv->auth);
1151 GST_RTSP_SERVER_UNLOCK (server);
1157 default_setup_connection (GstRTSPServer * server, GstRTSPClient * client,
1158 GstRTSPConnection * conn)
1160 GstRTSPServerPrivate *priv = server->priv;
1162 GST_RTSP_SERVER_LOCK (server);
1163 if (priv->certificate) {
1164 GTlsConnection *tls;
1166 /* configure the connection */
1167 tls = gst_rtsp_connection_get_tls (conn, NULL);
1168 g_tls_connection_set_certificate (tls, priv->certificate);
1170 GST_RTSP_SERVER_UNLOCK (server);
1176 * gst_rtsp_server_transfer_connection:
1177 * @server: a #GstRTSPServer
1178 * @socket: a network socket
1179 * @ip: the IP address of the remote client
1180 * @port: the port used by the other end
1181 * @initial_buffer: any initial data that was already read from the socket
1183 * Take an existing network socket and use it for an RTSP connection. This
1184 * is used when transferring a socket from an HTTP server which should be used
1185 * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
1186 * that the HTTP server read from the socket while parsing the HTTP header.
1188 * Returns: TRUE if all was ok, FALSE if an error occured.
1191 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
1192 const gchar * ip, gint port, const gchar * initial_buffer)
1194 GstRTSPClient *client = NULL;
1195 GstRTSPServerClass *klass;
1196 GstRTSPConnection *conn;
1199 klass = GST_RTSP_SERVER_GET_CLASS (server);
1201 if (klass->create_client)
1202 client = klass->create_client (server);
1206 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
1207 initial_buffer, &conn), no_connection);
1209 /* set connection on the client now */
1210 gst_rtsp_client_set_connection (client, conn);
1212 /* manage the client connection */
1213 manage_client (server, client);
1215 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1223 GST_ERROR_OBJECT (server, "failed to create a client");
1228 gchar *str = gst_rtsp_strresult (res);
1229 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
1236 * gst_rtsp_server_io_func:
1237 * @socket: a #GSocket
1238 * @condition: the condition on @source
1239 * @server: a #GstRTSPServer
1241 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
1242 * new connection on @socket or @server.
1244 * Returns: TRUE if the source could be connected, FALSE if an error occured.
1247 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
1248 GstRTSPServer * server)
1250 GstRTSPClient *client = NULL;
1251 GstRTSPServerClass *klass;
1253 GstRTSPConnection *conn = NULL;
1255 if (condition & G_IO_IN) {
1257 klass = GST_RTSP_SERVER_GET_CLASS (server);
1259 /* a new client connected, create a client object to handle the client. */
1260 if (klass->create_client)
1261 client = klass->create_client (server);
1265 /* a new client connected. */
1266 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, NULL),
1269 if (klass->setup_connection)
1270 if (!klass->setup_connection (server, client, conn))
1273 /* set connection on the client now */
1274 gst_rtsp_client_set_connection (client, conn);
1276 /* manage the client connection */
1277 manage_client (server, client);
1279 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1282 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
1284 return G_SOURCE_CONTINUE;
1289 GST_ERROR_OBJECT (server, "failed to create a client");
1290 return G_SOURCE_CONTINUE;
1294 gchar *str = gst_rtsp_strresult (res);
1295 GST_ERROR_OBJECT (server, "Could not accept client on socket %p: %s",
1298 g_object_unref (client);
1299 return G_SOURCE_CONTINUE;
1303 GST_ERROR_OBJECT (server, "failed to setup client connection");
1304 gst_rtsp_connection_free (conn);
1305 g_object_unref (client);
1306 return G_SOURCE_CONTINUE;
1311 watch_destroyed (GstRTSPServer * server)
1313 GstRTSPServerPrivate *priv = server->priv;
1315 GST_DEBUG_OBJECT (server, "source destroyed");
1317 g_object_unref (priv->socket);
1318 priv->socket = NULL;
1319 g_object_unref (server);
1323 * gst_rtsp_server_create_source:
1324 * @server: a #GstRTSPServer
1325 * @cancellable: a #GCancellable or %NULL.
1328 * Create a #GSource for @server. The new source will have a default
1329 * #GSocketSourceFunc of gst_rtsp_server_io_func().
1331 * @cancellable if not NULL can be used to cancel the source, which will cause
1332 * the source to trigger, reporting the current condition (which is likely 0
1333 * unless cancellation happened at the same time as a condition change). You can
1334 * check for this in the callback using g_cancellable_is_cancelled().
1336 * Returns: the #GSource for @server or NULL when an error occured. Free with
1340 gst_rtsp_server_create_source (GstRTSPServer * server,
1341 GCancellable * cancellable, GError ** error)
1343 GstRTSPServerPrivate *priv;
1344 GSocket *socket, *old;
1347 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1349 priv = server->priv;
1351 socket = gst_rtsp_server_create_socket (server, NULL, error);
1355 GST_RTSP_SERVER_LOCK (server);
1357 priv->socket = g_object_ref (socket);
1358 GST_RTSP_SERVER_UNLOCK (server);
1361 g_object_unref (old);
1363 /* create a watch for reads (new connections) and possible errors */
1364 source = g_socket_create_source (socket, G_IO_IN |
1365 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
1366 g_object_unref (socket);
1368 /* configure the callback */
1369 g_source_set_callback (source,
1370 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
1371 (GDestroyNotify) watch_destroyed);
1377 GST_ERROR_OBJECT (server, "failed to create socket");
1383 * gst_rtsp_server_attach:
1384 * @server: a #GstRTSPServer
1385 * @context: (allow-none): a #GMainContext
1387 * Attaches @server to @context. When the mainloop for @context is run, the
1388 * server will be dispatched. When @context is NULL, the default context will be
1391 * This function should be called when the server properties and urls are fully
1392 * configured and the server is ready to start.
1394 * Returns: the ID (greater than 0) for the source within the GMainContext.
1397 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1401 GError *error = NULL;
1403 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1405 source = gst_rtsp_server_create_source (server, NULL, &error);
1409 res = g_source_attach (source, context);
1410 g_source_unref (source);
1417 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1418 g_error_free (error);