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 clients that are connected */
56 GQueue loops; /* the main loops used in the threads */
59 #define DEFAULT_ADDRESS "0.0.0.0"
60 #define DEFAULT_BOUND_PORT -1
61 /* #define DEFAULT_ADDRESS "::0" */
62 #define DEFAULT_SERVICE "8554"
63 #define DEFAULT_BACKLOG 5
64 #define DEFAULT_MAX_THREADS 0
66 /* Define to use the SO_LINGER option so that the server sockets can be resused
67 * sooner. Disabled for now because it is not very well implemented by various
68 * OSes and it causes clients to fail to read the TEARDOWN response. */
87 SIGNAL_CLIENT_CONNECTED,
91 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
93 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
94 #define GST_CAT_DEFAULT rtsp_server_debug
96 typedef struct _ClientContext ClientContext;
97 typedef struct _Loop Loop;
99 static guint gst_rtsp_server_signals[SIGNAL_LAST] = { 0 };
101 static void gst_rtsp_server_get_property (GObject * object, guint propid,
102 GValue * value, GParamSpec * pspec);
103 static void gst_rtsp_server_set_property (GObject * object, guint propid,
104 const GValue * value, GParamSpec * pspec);
105 static void gst_rtsp_server_finalize (GObject * object);
107 static gpointer do_loop (Loop * loop);
108 static GstRTSPClient *default_create_client (GstRTSPServer * server);
111 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
113 GObjectClass *gobject_class;
115 g_type_class_add_private (klass, sizeof (GstRTSPServerPrivate));
117 gobject_class = G_OBJECT_CLASS (klass);
119 gobject_class->get_property = gst_rtsp_server_get_property;
120 gobject_class->set_property = gst_rtsp_server_set_property;
121 gobject_class->finalize = gst_rtsp_server_finalize;
124 * GstRTSPServer::address:
126 * The address of the server. This is the address where the server will
129 g_object_class_install_property (gobject_class, PROP_ADDRESS,
130 g_param_spec_string ("address", "Address",
131 "The address the server uses to listen on", DEFAULT_ADDRESS,
132 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
134 * GstRTSPServer::service:
136 * The service of the server. This is either a string with the service name or
137 * a port number (as a string) the server will listen on.
139 g_object_class_install_property (gobject_class, PROP_SERVICE,
140 g_param_spec_string ("service", "Service",
141 "The service or port number the server uses to listen on",
142 DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
144 * GstRTSPServer::bound-port:
146 * The actual port the server is listening on. Can be used to retrieve the
147 * port number when the server is started on port 0, which means bind to a
148 * random port. Set to -1 if the server has not been bound yet.
150 g_object_class_install_property (gobject_class, PROP_BOUND_PORT,
151 g_param_spec_int ("bound-port", "Bound port",
152 "The port number the server is listening on",
153 -1, G_MAXUINT16, DEFAULT_BOUND_PORT,
154 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
156 * GstRTSPServer::backlog:
158 * The backlog argument defines the maximum length to which the queue of
159 * pending connections for the server may grow. If a connection request arrives
160 * when the queue is full, the client may receive an error with an indication of
161 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
162 * request may be ignored so that a later reattempt at connection succeeds.
164 g_object_class_install_property (gobject_class, PROP_BACKLOG,
165 g_param_spec_int ("backlog", "Backlog",
166 "The maximum length to which the queue "
167 "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
168 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170 * GstRTSPServer::session-pool:
172 * The session pool of the server. By default each server has a separate
173 * session pool but sessions can be shared between servers by setting the same
174 * session pool on multiple servers.
176 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
177 g_param_spec_object ("session-pool", "Session Pool",
178 "The session pool to use for client session",
179 GST_TYPE_RTSP_SESSION_POOL,
180 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182 * GstRTSPServer::mount-points:
184 * The mount points to use for this server. By default the server has no
185 * mount points and thus cannot map urls to media streams.
187 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
188 g_param_spec_object ("mount-points", "Mount Points",
189 "The mount points to use for client session",
190 GST_TYPE_RTSP_MOUNT_POINTS,
191 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
193 * GstRTSPServer::max-threads:
195 * The maximum amount of threads to use for client connections. A value of
196 * 0 means to use only the mainloop, -1 means an unlimited amount of
199 g_object_class_install_property (gobject_class, PROP_MAX_THREADS,
200 g_param_spec_int ("max-threads", "Max Threads",
201 "The maximum amount of threads to use for client connections "
202 "(0 = only mainloop, -1 = unlimited)", -1, G_MAXINT,
203 DEFAULT_MAX_THREADS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
205 gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED] =
206 g_signal_new ("client-connected", G_TYPE_FROM_CLASS (gobject_class),
207 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPServerClass, client_connected),
208 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
209 gst_rtsp_client_get_type ());
211 klass->create_client = default_create_client;
213 klass->pool = g_thread_pool_new ((GFunc) do_loop, klass, -1, FALSE, NULL);
215 GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
219 gst_rtsp_server_init (GstRTSPServer * server)
221 GstRTSPServerPrivate *priv = GST_RTSP_SERVER_GET_PRIVATE (server);
225 g_mutex_init (&priv->lock);
226 priv->address = g_strdup (DEFAULT_ADDRESS);
227 priv->service = g_strdup (DEFAULT_SERVICE);
229 priv->backlog = DEFAULT_BACKLOG;
230 priv->session_pool = gst_rtsp_session_pool_new ();
231 priv->mount_points = gst_rtsp_mount_points_new ();
232 priv->max_threads = DEFAULT_MAX_THREADS;
233 g_queue_init (&priv->loops);
237 gst_rtsp_server_finalize (GObject * object)
239 GstRTSPServer *server = GST_RTSP_SERVER (object);
240 GstRTSPServerPrivate *priv = server->priv;
242 GST_DEBUG_OBJECT (server, "finalize server");
244 g_free (priv->address);
245 g_free (priv->service);
248 g_object_unref (priv->socket);
250 g_object_unref (priv->session_pool);
251 g_object_unref (priv->mount_points);
254 g_object_unref (priv->auth);
256 g_mutex_clear (&priv->lock);
258 G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
262 * gst_rtsp_server_new:
264 * Create a new #GstRTSPServer instance.
267 gst_rtsp_server_new (void)
269 GstRTSPServer *result;
271 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
277 * gst_rtsp_server_set_address:
278 * @server: a #GstRTSPServer
279 * @address: the address
281 * Configure @server to accept connections on the given address.
283 * This function must be called before the server is bound.
286 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
288 GstRTSPServerPrivate *priv;
290 g_return_if_fail (GST_IS_RTSP_SERVER (server));
291 g_return_if_fail (address != NULL);
295 GST_RTSP_SERVER_LOCK (server);
296 g_free (priv->address);
297 priv->address = g_strdup (address);
298 GST_RTSP_SERVER_UNLOCK (server);
302 * gst_rtsp_server_get_address:
303 * @server: a #GstRTSPServer
305 * Get the address on which the server will accept connections.
307 * Returns: the server address. g_free() after usage.
310 gst_rtsp_server_get_address (GstRTSPServer * server)
312 GstRTSPServerPrivate *priv;
315 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
319 GST_RTSP_SERVER_LOCK (server);
320 result = g_strdup (priv->address);
321 GST_RTSP_SERVER_UNLOCK (server);
327 * gst_rtsp_server_get_bound_port:
328 * @server: a #GstRTSPServer
330 * Get the port number where the server was bound to.
332 * Returns: the port number
335 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
337 GstRTSPServerPrivate *priv;
338 GSocketAddress *address;
341 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
345 GST_RTSP_SERVER_LOCK (server);
346 if (priv->socket == NULL)
349 address = g_socket_get_local_address (priv->socket, NULL);
350 result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
351 g_object_unref (address);
354 GST_RTSP_SERVER_UNLOCK (server);
360 * gst_rtsp_server_set_service:
361 * @server: a #GstRTSPServer
362 * @service: the service
364 * Configure @server to accept connections on the given service.
365 * @service should be a string containing the service name (see services(5)) or
366 * a string containing a port number between 1 and 65535.
368 * This function must be called before the server is bound.
371 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
373 GstRTSPServerPrivate *priv;
375 g_return_if_fail (GST_IS_RTSP_SERVER (server));
376 g_return_if_fail (service != NULL);
380 GST_RTSP_SERVER_LOCK (server);
381 g_free (priv->service);
382 priv->service = g_strdup (service);
383 GST_RTSP_SERVER_UNLOCK (server);
387 * gst_rtsp_server_get_service:
388 * @server: a #GstRTSPServer
390 * Get the service on which the server will accept connections.
392 * Returns: the service. use g_free() after usage.
395 gst_rtsp_server_get_service (GstRTSPServer * server)
397 GstRTSPServerPrivate *priv;
400 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
404 GST_RTSP_SERVER_LOCK (server);
405 result = g_strdup (priv->service);
406 GST_RTSP_SERVER_UNLOCK (server);
412 * gst_rtsp_server_set_backlog:
413 * @server: a #GstRTSPServer
414 * @backlog: the backlog
416 * configure the maximum amount of requests that may be queued for the
419 * This function must be called before the server is bound.
422 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
424 GstRTSPServerPrivate *priv;
426 g_return_if_fail (GST_IS_RTSP_SERVER (server));
430 GST_RTSP_SERVER_LOCK (server);
431 priv->backlog = backlog;
432 GST_RTSP_SERVER_UNLOCK (server);
436 * gst_rtsp_server_get_backlog:
437 * @server: a #GstRTSPServer
439 * The maximum amount of queued requests for the server.
441 * Returns: the server backlog.
444 gst_rtsp_server_get_backlog (GstRTSPServer * server)
446 GstRTSPServerPrivate *priv;
449 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
453 GST_RTSP_SERVER_LOCK (server);
454 result = priv->backlog;
455 GST_RTSP_SERVER_UNLOCK (server);
461 * gst_rtsp_server_set_session_pool:
462 * @server: a #GstRTSPServer
463 * @pool: a #GstRTSPSessionPool
465 * configure @pool to be used as the session pool of @server.
468 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
469 GstRTSPSessionPool * pool)
471 GstRTSPServerPrivate *priv;
472 GstRTSPSessionPool *old;
474 g_return_if_fail (GST_IS_RTSP_SERVER (server));
481 GST_RTSP_SERVER_LOCK (server);
482 old = priv->session_pool;
483 priv->session_pool = pool;
484 GST_RTSP_SERVER_UNLOCK (server);
487 g_object_unref (old);
491 * gst_rtsp_server_get_session_pool:
492 * @server: a #GstRTSPServer
494 * Get the #GstRTSPSessionPool used as the session pool of @server.
496 * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
500 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
502 GstRTSPServerPrivate *priv;
503 GstRTSPSessionPool *result;
505 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
509 GST_RTSP_SERVER_LOCK (server);
510 if ((result = priv->session_pool))
511 g_object_ref (result);
512 GST_RTSP_SERVER_UNLOCK (server);
518 * gst_rtsp_server_set_mount_points:
519 * @server: a #GstRTSPServer
520 * @mounts: a #GstRTSPMountPoints
522 * configure @mounts to be used as the mount points of @server.
525 gst_rtsp_server_set_mount_points (GstRTSPServer * server,
526 GstRTSPMountPoints * mounts)
528 GstRTSPServerPrivate *priv;
529 GstRTSPMountPoints *old;
531 g_return_if_fail (GST_IS_RTSP_SERVER (server));
536 g_object_ref (mounts);
538 GST_RTSP_SERVER_LOCK (server);
539 old = priv->mount_points;
540 priv->mount_points = mounts;
541 GST_RTSP_SERVER_UNLOCK (server);
544 g_object_unref (old);
549 * gst_rtsp_server_get_mount_points:
550 * @server: a #GstRTSPServer
552 * Get the #GstRTSPMountPoints used as the mount points of @server.
554 * Returns: (transfer full): the #GstRTSPMountPoints of @server. g_object_unref() after
558 gst_rtsp_server_get_mount_points (GstRTSPServer * server)
560 GstRTSPServerPrivate *priv;
561 GstRTSPMountPoints *result;
563 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
567 GST_RTSP_SERVER_LOCK (server);
568 if ((result = priv->mount_points))
569 g_object_ref (result);
570 GST_RTSP_SERVER_UNLOCK (server);
576 * gst_rtsp_server_set_auth:
577 * @server: a #GstRTSPServer
578 * @auth: a #GstRTSPAuth
580 * configure @auth to be used as the authentication manager of @server.
583 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
585 GstRTSPServerPrivate *priv;
588 g_return_if_fail (GST_IS_RTSP_SERVER (server));
595 GST_RTSP_SERVER_LOCK (server);
598 GST_RTSP_SERVER_UNLOCK (server);
601 g_object_unref (old);
606 * gst_rtsp_server_get_auth:
607 * @server: a #GstRTSPServer
609 * Get the #GstRTSPAuth used as the authentication manager of @server.
611 * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
615 gst_rtsp_server_get_auth (GstRTSPServer * server)
617 GstRTSPServerPrivate *priv;
620 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
624 GST_RTSP_SERVER_LOCK (server);
625 if ((result = priv->auth))
626 g_object_ref (result);
627 GST_RTSP_SERVER_UNLOCK (server);
633 * gst_rtsp_server_set_max_threads:
634 * @server: a #GstRTSPServer
635 * @max_threads: maximum threads
637 * Set the maximum threads used by the server to handle client requests.
638 * A value of 0 will use the server mainloop, a value of -1 will use an
639 * unlimited number of threads.
642 gst_rtsp_server_set_max_threads (GstRTSPServer * server, gint max_threads)
644 GstRTSPServerPrivate *priv;
646 g_return_if_fail (GST_IS_RTSP_SERVER (server));
650 GST_RTSP_SERVER_LOCK (server);
651 priv->max_threads = max_threads;
652 GST_RTSP_SERVER_UNLOCK (server);
656 * gst_rtsp_server_get_max_threads:
657 * @server: a #GstRTSPServer
659 * Get the maximum number of threads used for client connections.
660 * See gst_rtsp_server_set_max_threads().
662 * Returns: the maximum number of threads.
665 gst_rtsp_server_get_max_threads (GstRTSPServer * server)
667 GstRTSPServerPrivate *priv;
670 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
674 GST_RTSP_SERVER_LOCK (server);
675 res = priv->max_threads;
676 GST_RTSP_SERVER_UNLOCK (server);
683 gst_rtsp_server_get_property (GObject * object, guint propid,
684 GValue * value, GParamSpec * pspec)
686 GstRTSPServer *server = GST_RTSP_SERVER (object);
690 g_value_take_string (value, gst_rtsp_server_get_address (server));
693 g_value_take_string (value, gst_rtsp_server_get_service (server));
695 case PROP_BOUND_PORT:
696 g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
699 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
701 case PROP_SESSION_POOL:
702 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
704 case PROP_MOUNT_POINTS:
705 g_value_take_object (value, gst_rtsp_server_get_mount_points (server));
707 case PROP_MAX_THREADS:
708 g_value_set_int (value, gst_rtsp_server_get_max_threads (server));
711 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
716 gst_rtsp_server_set_property (GObject * object, guint propid,
717 const GValue * value, GParamSpec * pspec)
719 GstRTSPServer *server = GST_RTSP_SERVER (object);
723 gst_rtsp_server_set_address (server, g_value_get_string (value));
726 gst_rtsp_server_set_service (server, g_value_get_string (value));
729 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
731 case PROP_SESSION_POOL:
732 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
734 case PROP_MOUNT_POINTS:
735 gst_rtsp_server_set_mount_points (server, g_value_get_object (value));
737 case PROP_MAX_THREADS:
738 gst_rtsp_server_set_max_threads (server, g_value_get_int (value));
741 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
746 * gst_rtsp_server_create_socket:
747 * @server: a #GstRTSPServer
748 * @cancellable: a #GCancellable
751 * Create a #GSocket for @server. The socket will listen on the
752 * configured service.
754 * Returns: (transfer full): the #GSocket for @server or NULL when an error occured.
757 gst_rtsp_server_create_socket (GstRTSPServer * server,
758 GCancellable * cancellable, GError ** error)
760 GstRTSPServerPrivate *priv;
761 GSocketConnectable *conn;
762 GSocketAddressEnumerator *enumerator;
763 GSocket *socket = NULL;
765 struct linger linger;
767 GError *sock_error = NULL;
768 GError *bind_error = NULL;
771 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
775 GST_RTSP_SERVER_LOCK (server);
776 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", priv->address,
779 /* resolve the server IP address */
780 port = atoi (priv->service);
781 if (port != 0 || !strcmp (priv->service, "0"))
782 conn = g_network_address_new (priv->address, port);
784 conn = g_network_service_new (priv->service, "tcp", priv->address);
786 enumerator = g_socket_connectable_enumerate (conn);
787 g_object_unref (conn);
789 /* create server socket, we loop through all the addresses until we manage to
790 * create a socket and bind. */
792 GSocketAddress *sockaddr;
795 g_socket_address_enumerator_next (enumerator, cancellable, error);
798 GST_DEBUG_OBJECT (server, "no more addresses %s",
799 *error ? (*error)->message : "");
801 GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
806 /* only keep the first error */
807 socket = g_socket_new (g_socket_address_get_family (sockaddr),
808 G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
809 sock_error ? NULL : &sock_error);
811 if (socket == NULL) {
812 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
813 sock_error->message);
814 g_object_unref (sockaddr);
818 if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
819 g_object_unref (sockaddr);
823 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
824 bind_error->message);
825 g_object_unref (sockaddr);
826 g_object_unref (socket);
829 g_object_unref (enumerator);
834 g_clear_error (&sock_error);
835 g_clear_error (&bind_error);
837 GST_DEBUG_OBJECT (server, "opened sending server socket");
839 /* keep connection alive; avoids SIGPIPE during write */
840 g_socket_set_keepalive (socket, TRUE);
844 /* make sure socket is reset 5 seconds after close. This ensure that we can
845 * reuse the socket quickly while still having a chance to send data to the
849 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
850 (void *) &linger, sizeof (linger)) < 0)
855 /* set the server socket to nonblocking */
856 g_socket_set_blocking (socket, FALSE);
858 /* set listen backlog */
859 g_socket_set_listen_backlog (socket, priv->backlog);
861 if (!g_socket_listen (socket, error))
864 GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
865 socket, priv->backlog);
867 GST_RTSP_SERVER_UNLOCK (server);
874 GST_ERROR_OBJECT (server, "failed to create socket");
881 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
889 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
896 g_object_unref (socket);
900 g_propagate_error (error, sock_error);
902 g_error_free (sock_error);
905 if ((error == NULL) || (*error == NULL))
906 g_propagate_error (error, bind_error);
908 g_error_free (bind_error);
910 GST_RTSP_SERVER_UNLOCK (server);
919 GstRTSPServer *server;
921 GMainContext *mainctx;
924 /* must be called with the lock held */
926 loop_unref (Loop * loop)
928 GstRTSPServer *server = loop->server;
929 GstRTSPServerPrivate *priv = server->priv;
933 if (loop->refcnt <= 0) {
934 g_queue_remove (&priv->loops, loop);
935 g_main_loop_quit (loop->mainloop);
939 struct _ClientContext
941 GstRTSPServer *server;
943 GstRTSPClient *client;
947 free_client_context (ClientContext * ctx)
949 GST_RTSP_SERVER_LOCK (ctx->server);
951 loop_unref (ctx->loop);
952 GST_RTSP_SERVER_UNLOCK (ctx->server);
954 g_object_unref (ctx->client);
955 g_slice_free (ClientContext, ctx);
957 return G_SOURCE_REMOVE;
961 do_loop (Loop * loop)
963 GST_INFO ("enter mainloop");
964 g_main_loop_run (loop->mainloop);
965 GST_INFO ("exit mainloop");
967 g_main_context_unref (loop->mainctx);
968 g_main_loop_unref (loop->mainloop);
969 g_object_unref (loop->server);
970 g_slice_free (Loop, loop);
975 /* Must be called with lock held */
978 gst_rtsp_server_get_main_loop (GstRTSPServer * server)
980 GstRTSPServerPrivate *priv = server->priv;
983 if (priv->max_threads > 0 &&
984 g_queue_get_length (&priv->loops) >= priv->max_threads) {
985 loop = g_queue_pop_head (&priv->loops);
988 GstRTSPServerClass *klass = GST_RTSP_SERVER_GET_CLASS (server);
990 loop = g_slice_new0 (Loop);
992 loop->server = g_object_ref (server);
993 loop->mainctx = g_main_context_new ();
994 loop->mainloop = g_main_loop_new (loop->mainctx, FALSE);
996 g_thread_pool_push (klass->pool, loop, NULL);
999 g_queue_push_tail (&priv->loops, loop);
1005 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
1007 GstRTSPServer *server = ctx->server;
1008 GstRTSPServerPrivate *priv = server->priv;
1010 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
1012 g_object_ref (server);
1014 GST_RTSP_SERVER_LOCK (server);
1015 priv->clients = g_list_remove (priv->clients, ctx);
1016 GST_RTSP_SERVER_UNLOCK (server);
1021 src = g_idle_source_new ();
1022 g_source_set_callback (src, (GSourceFunc) free_client_context, ctx, NULL);
1023 g_source_attach (src, ctx->loop->mainctx);
1024 g_source_unref (src);
1026 free_client_context (ctx);
1029 g_object_unref (server);
1032 /* add the client context to the active list of clients, takes ownership
1035 manage_client (GstRTSPServer * server, GstRTSPClient * client)
1038 GstRTSPServerPrivate *priv = server->priv;
1039 GMainContext *mainctx;
1041 GST_DEBUG_OBJECT (server, "manage client %p", client);
1043 ctx = g_slice_new0 (ClientContext);
1044 ctx->server = server;
1045 ctx->client = client;
1047 GST_RTSP_SERVER_LOCK (server);
1048 if (priv->max_threads == 0) {
1051 /* find the context to add the watch */
1052 if ((source = g_main_current_source ()))
1053 mainctx = g_source_get_context (source);
1057 ctx->loop = gst_rtsp_server_get_main_loop (server);
1058 mainctx = ctx->loop->mainctx;
1061 g_signal_connect (client, "closed", (GCallback) unmanage_client, ctx);
1062 priv->clients = g_list_prepend (priv->clients, ctx);
1064 gst_rtsp_client_attach (client, mainctx);
1066 GST_RTSP_SERVER_UNLOCK (server);
1069 static GstRTSPClient *
1070 default_create_client (GstRTSPServer * server)
1072 GstRTSPClient *client;
1073 GstRTSPServerPrivate *priv = server->priv;
1075 /* a new client connected, create a session to handle the client. */
1076 client = gst_rtsp_client_new ();
1078 /* set the session pool that this client should use */
1079 GST_RTSP_SERVER_LOCK (server);
1080 gst_rtsp_client_set_session_pool (client, priv->session_pool);
1081 /* set the mount points that this client should use */
1082 gst_rtsp_client_set_mount_points (client, priv->mount_points);
1083 /* set authentication manager */
1084 gst_rtsp_client_set_auth (client, priv->auth);
1085 GST_RTSP_SERVER_UNLOCK (server);
1091 * gst_rtsp_server_transfer_connection:
1092 * @server: a #GstRTSPServer
1093 * @socket: a network socket
1094 * @ip: the IP address of the remote client
1095 * @port: the port used by the other end
1096 * @initial_buffer: any initial data that was already read from the socket
1098 * Take an existing network socket and use it for an RTSP connection. This
1099 * is used when transferring a socket from an HTTP server which should be used
1100 * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
1101 * that the HTTP server read from the socket while parsing the HTTP header.
1103 * Returns: TRUE if all was ok, FALSE if an error occured.
1106 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
1107 const gchar * ip, gint port, const gchar * initial_buffer)
1109 GstRTSPClient *client = NULL;
1110 GstRTSPServerClass *klass;
1111 GstRTSPConnection *conn;
1114 klass = GST_RTSP_SERVER_GET_CLASS (server);
1116 if (klass->create_client)
1117 client = klass->create_client (server);
1121 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
1122 initial_buffer, &conn), no_connection);
1124 /* set connection on the client now */
1125 gst_rtsp_client_set_connection (client, conn);
1127 /* manage the client connection */
1128 manage_client (server, client);
1130 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1138 GST_ERROR_OBJECT (server, "failed to create a client");
1143 gchar *str = gst_rtsp_strresult (res);
1144 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
1151 * gst_rtsp_server_io_func:
1152 * @socket: a #GSocket
1153 * @condition: the condition on @source
1154 * @server: a #GstRTSPServer
1156 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
1157 * new connection on @socket or @server.
1159 * Returns: TRUE if the source could be connected, FALSE if an error occured.
1162 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
1163 GstRTSPServer * server)
1165 GstRTSPClient *client = NULL;
1166 GstRTSPServerClass *klass;
1169 if (condition & G_IO_IN) {
1170 GstRTSPConnection *conn;
1172 klass = GST_RTSP_SERVER_GET_CLASS (server);
1174 /* a new client connected, create a client object to handle the client. */
1175 if (klass->create_client)
1176 client = klass->create_client (server);
1180 /* a new client connected. */
1181 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, NULL),
1184 /* set connection on the client now */
1185 gst_rtsp_client_set_connection (client, conn);
1187 /* manage the client connection */
1188 manage_client (server, client);
1190 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1193 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
1195 return G_SOURCE_CONTINUE;
1200 GST_ERROR_OBJECT (server, "failed to create a client");
1201 return G_SOURCE_CONTINUE;
1205 gchar *str = gst_rtsp_strresult (res);
1206 GST_ERROR_OBJECT (server, "Could not accept client on socket %p: %s",
1209 g_object_unref (client);
1210 return G_SOURCE_CONTINUE;
1215 watch_destroyed (GstRTSPServer * server)
1217 GstRTSPServerPrivate *priv = server->priv;
1219 GST_DEBUG_OBJECT (server, "source destroyed");
1221 g_object_unref (priv->socket);
1222 priv->socket = NULL;
1223 g_object_unref (server);
1227 * gst_rtsp_server_create_source:
1228 * @server: a #GstRTSPServer
1229 * @cancellable: a #GCancellable or %NULL.
1232 * Create a #GSource for @server. The new source will have a default
1233 * #GSocketSourceFunc of gst_rtsp_server_io_func().
1235 * @cancellable if not NULL can be used to cancel the source, which will cause
1236 * the source to trigger, reporting the current condition (which is likely 0
1237 * unless cancellation happened at the same time as a condition change). You can
1238 * check for this in the callback using g_cancellable_is_cancelled().
1240 * Returns: the #GSource for @server or NULL when an error occured. Free with
1244 gst_rtsp_server_create_source (GstRTSPServer * server,
1245 GCancellable * cancellable, GError ** error)
1247 GstRTSPServerPrivate *priv;
1248 GSocket *socket, *old;
1251 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1253 priv = server->priv;
1255 socket = gst_rtsp_server_create_socket (server, NULL, error);
1259 GST_RTSP_SERVER_LOCK (server);
1261 priv->socket = g_object_ref (socket);
1262 GST_RTSP_SERVER_UNLOCK (server);
1265 g_object_unref (old);
1267 /* create a watch for reads (new connections) and possible errors */
1268 source = g_socket_create_source (socket, G_IO_IN |
1269 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
1270 g_object_unref (socket);
1272 /* configure the callback */
1273 g_source_set_callback (source,
1274 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
1275 (GDestroyNotify) watch_destroyed);
1281 GST_ERROR_OBJECT (server, "failed to create socket");
1287 * gst_rtsp_server_attach:
1288 * @server: a #GstRTSPServer
1289 * @context: (allow-none): a #GMainContext
1291 * Attaches @server to @context. When the mainloop for @context is run, the
1292 * server will be dispatched. When @context is NULL, the default context will be
1295 * This function should be called when the server properties and urls are fully
1296 * configured and the server is ready to start.
1298 * Returns: the ID (greater than 0) for the source within the GMainContext.
1301 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1305 GError *error = NULL;
1307 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1309 source = gst_rtsp_server_create_source (server, NULL, &error);
1313 res = g_source_attach (source, context);
1314 g_source_unref (source);
1321 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1322 g_error_free (error);