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_generic, G_TYPE_NONE, 1,
229 GST_TYPE_RTSP_CLIENT);
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.
287 * Returns: (transfer full): a new #GstRTSPServer
290 gst_rtsp_server_new (void)
292 GstRTSPServer *result;
294 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
300 * gst_rtsp_server_set_address:
301 * @server: a #GstRTSPServer
302 * @address: the address
304 * Configure @server to accept connections on the given address.
306 * This function must be called before the server is bound.
309 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
311 GstRTSPServerPrivate *priv;
313 g_return_if_fail (GST_IS_RTSP_SERVER (server));
314 g_return_if_fail (address != NULL);
318 GST_RTSP_SERVER_LOCK (server);
319 g_free (priv->address);
320 priv->address = g_strdup (address);
321 GST_RTSP_SERVER_UNLOCK (server);
325 * gst_rtsp_server_get_address:
326 * @server: a #GstRTSPServer
328 * Get the address on which the server will accept connections.
330 * Returns: (transfer full): the server address. g_free() after usage.
333 gst_rtsp_server_get_address (GstRTSPServer * server)
335 GstRTSPServerPrivate *priv;
338 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
342 GST_RTSP_SERVER_LOCK (server);
343 result = g_strdup (priv->address);
344 GST_RTSP_SERVER_UNLOCK (server);
350 * gst_rtsp_server_get_bound_port:
351 * @server: a #GstRTSPServer
353 * Get the port number where the server was bound to.
355 * Returns: the port number
358 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
360 GstRTSPServerPrivate *priv;
361 GSocketAddress *address;
364 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
368 GST_RTSP_SERVER_LOCK (server);
369 if (priv->socket == NULL)
372 address = g_socket_get_local_address (priv->socket, NULL);
373 result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
374 g_object_unref (address);
377 GST_RTSP_SERVER_UNLOCK (server);
383 * gst_rtsp_server_set_service:
384 * @server: a #GstRTSPServer
385 * @service: the service
387 * Configure @server to accept connections on the given service.
388 * @service should be a string containing the service name (see services(5)) or
389 * a string containing a port number between 1 and 65535.
391 * When @service is set to "0", the server will listen on a random free
392 * port. The actual used port can be retrieved with
393 * gst_rtsp_server_get_bound_port().
395 * This function must be called before the server is bound.
398 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
400 GstRTSPServerPrivate *priv;
402 g_return_if_fail (GST_IS_RTSP_SERVER (server));
403 g_return_if_fail (service != NULL);
407 GST_RTSP_SERVER_LOCK (server);
408 g_free (priv->service);
409 priv->service = g_strdup (service);
410 GST_RTSP_SERVER_UNLOCK (server);
414 * gst_rtsp_server_get_service:
415 * @server: a #GstRTSPServer
417 * Get the service on which the server will accept connections.
419 * Returns: (transfer full): the service. use g_free() after usage.
422 gst_rtsp_server_get_service (GstRTSPServer * server)
424 GstRTSPServerPrivate *priv;
427 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
431 GST_RTSP_SERVER_LOCK (server);
432 result = g_strdup (priv->service);
433 GST_RTSP_SERVER_UNLOCK (server);
439 * gst_rtsp_server_set_backlog:
440 * @server: a #GstRTSPServer
441 * @backlog: the backlog
443 * configure the maximum amount of requests that may be queued for the
446 * This function must be called before the server is bound.
449 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
451 GstRTSPServerPrivate *priv;
453 g_return_if_fail (GST_IS_RTSP_SERVER (server));
457 GST_RTSP_SERVER_LOCK (server);
458 priv->backlog = backlog;
459 GST_RTSP_SERVER_UNLOCK (server);
463 * gst_rtsp_server_get_backlog:
464 * @server: a #GstRTSPServer
466 * The maximum amount of queued requests for the server.
468 * Returns: the server backlog.
471 gst_rtsp_server_get_backlog (GstRTSPServer * server)
473 GstRTSPServerPrivate *priv;
476 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
480 GST_RTSP_SERVER_LOCK (server);
481 result = priv->backlog;
482 GST_RTSP_SERVER_UNLOCK (server);
488 * gst_rtsp_server_set_session_pool:
489 * @server: a #GstRTSPServer
490 * @pool: (transfer none): a #GstRTSPSessionPool
492 * configure @pool to be used as the session pool of @server.
495 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
496 GstRTSPSessionPool * pool)
498 GstRTSPServerPrivate *priv;
499 GstRTSPSessionPool *old;
501 g_return_if_fail (GST_IS_RTSP_SERVER (server));
508 GST_RTSP_SERVER_LOCK (server);
509 old = priv->session_pool;
510 priv->session_pool = pool;
511 GST_RTSP_SERVER_UNLOCK (server);
514 g_object_unref (old);
518 * gst_rtsp_server_get_session_pool:
519 * @server: a #GstRTSPServer
521 * Get the #GstRTSPSessionPool used as the session pool of @server.
523 * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
527 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
529 GstRTSPServerPrivate *priv;
530 GstRTSPSessionPool *result;
532 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
536 GST_RTSP_SERVER_LOCK (server);
537 if ((result = priv->session_pool))
538 g_object_ref (result);
539 GST_RTSP_SERVER_UNLOCK (server);
545 * gst_rtsp_server_set_mount_points:
546 * @server: a #GstRTSPServer
547 * @mounts: (transfer none): a #GstRTSPMountPoints
549 * configure @mounts to be used as the mount points of @server.
552 gst_rtsp_server_set_mount_points (GstRTSPServer * server,
553 GstRTSPMountPoints * mounts)
555 GstRTSPServerPrivate *priv;
556 GstRTSPMountPoints *old;
558 g_return_if_fail (GST_IS_RTSP_SERVER (server));
563 g_object_ref (mounts);
565 GST_RTSP_SERVER_LOCK (server);
566 old = priv->mount_points;
567 priv->mount_points = mounts;
568 GST_RTSP_SERVER_UNLOCK (server);
571 g_object_unref (old);
576 * gst_rtsp_server_get_mount_points:
577 * @server: a #GstRTSPServer
579 * Get the #GstRTSPMountPoints used as the mount points of @server.
581 * Returns: (transfer full): the #GstRTSPMountPoints of @server. g_object_unref() after
585 gst_rtsp_server_get_mount_points (GstRTSPServer * server)
587 GstRTSPServerPrivate *priv;
588 GstRTSPMountPoints *result;
590 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
594 GST_RTSP_SERVER_LOCK (server);
595 if ((result = priv->mount_points))
596 g_object_ref (result);
597 GST_RTSP_SERVER_UNLOCK (server);
603 * gst_rtsp_server_set_auth:
604 * @server: a #GstRTSPServer
605 * @auth: (transfer none): a #GstRTSPAuth
607 * configure @auth to be used as the authentication manager of @server.
610 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
612 GstRTSPServerPrivate *priv;
615 g_return_if_fail (GST_IS_RTSP_SERVER (server));
622 GST_RTSP_SERVER_LOCK (server);
625 GST_RTSP_SERVER_UNLOCK (server);
628 g_object_unref (old);
633 * gst_rtsp_server_get_auth:
634 * @server: a #GstRTSPServer
636 * Get the #GstRTSPAuth used as the authentication manager of @server.
638 * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
642 gst_rtsp_server_get_auth (GstRTSPServer * server)
644 GstRTSPServerPrivate *priv;
647 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
651 GST_RTSP_SERVER_LOCK (server);
652 if ((result = priv->auth))
653 g_object_ref (result);
654 GST_RTSP_SERVER_UNLOCK (server);
660 * gst_rtsp_server_set_thread_pool:
661 * @server: a #GstRTSPServer
662 * @pool: (transfer none): a #GstRTSPThreadPool
664 * configure @pool to be used as the thread pool of @server.
667 gst_rtsp_server_set_thread_pool (GstRTSPServer * server,
668 GstRTSPThreadPool * pool)
670 GstRTSPServerPrivate *priv;
671 GstRTSPThreadPool *old;
673 g_return_if_fail (GST_IS_RTSP_SERVER (server));
680 GST_RTSP_SERVER_LOCK (server);
681 old = priv->thread_pool;
682 priv->thread_pool = pool;
683 GST_RTSP_SERVER_UNLOCK (server);
686 g_object_unref (old);
690 * gst_rtsp_server_get_thread_pool:
691 * @server: a #GstRTSPServer
693 * Get the #GstRTSPThreadPool used as the thread pool of @server.
695 * Returns: (transfer full): the #GstRTSPThreadPool of @server. g_object_unref() after
699 gst_rtsp_server_get_thread_pool (GstRTSPServer * server)
701 GstRTSPServerPrivate *priv;
702 GstRTSPThreadPool *result;
704 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
708 GST_RTSP_SERVER_LOCK (server);
709 if ((result = priv->thread_pool))
710 g_object_ref (result);
711 GST_RTSP_SERVER_UNLOCK (server);
717 gst_rtsp_server_get_property (GObject * object, guint propid,
718 GValue * value, GParamSpec * pspec)
720 GstRTSPServer *server = GST_RTSP_SERVER (object);
724 g_value_take_string (value, gst_rtsp_server_get_address (server));
727 g_value_take_string (value, gst_rtsp_server_get_service (server));
729 case PROP_BOUND_PORT:
730 g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
733 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
735 case PROP_SESSION_POOL:
736 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
738 case PROP_MOUNT_POINTS:
739 g_value_take_object (value, gst_rtsp_server_get_mount_points (server));
742 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
747 gst_rtsp_server_set_property (GObject * object, guint propid,
748 const GValue * value, GParamSpec * pspec)
750 GstRTSPServer *server = GST_RTSP_SERVER (object);
754 gst_rtsp_server_set_address (server, g_value_get_string (value));
757 gst_rtsp_server_set_service (server, g_value_get_string (value));
760 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
762 case PROP_SESSION_POOL:
763 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
765 case PROP_MOUNT_POINTS:
766 gst_rtsp_server_set_mount_points (server, g_value_get_object (value));
769 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
774 * gst_rtsp_server_create_socket:
775 * @server: a #GstRTSPServer
776 * @cancellable: (allow-none): a #GCancellable
777 * @error: (out): a #GError
779 * Create a #GSocket for @server. The socket will listen on the
780 * configured service.
782 * Returns: (transfer full): the #GSocket for @server or %NULL when an error
786 gst_rtsp_server_create_socket (GstRTSPServer * server,
787 GCancellable * cancellable, GError ** error)
789 GstRTSPServerPrivate *priv;
790 GSocketConnectable *conn;
791 GSocketAddressEnumerator *enumerator;
792 GSocket *socket = NULL;
794 struct linger linger;
796 GError *sock_error = NULL;
797 GError *bind_error = NULL;
800 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
804 GST_RTSP_SERVER_LOCK (server);
805 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", priv->address,
808 /* resolve the server IP address */
809 port = atoi (priv->service);
810 if (port != 0 || !strcmp (priv->service, "0"))
811 conn = g_network_address_new (priv->address, port);
813 conn = g_network_service_new (priv->service, "tcp", priv->address);
815 enumerator = g_socket_connectable_enumerate (conn);
816 g_object_unref (conn);
818 /* create server socket, we loop through all the addresses until we manage to
819 * create a socket and bind. */
821 GSocketAddress *sockaddr;
824 g_socket_address_enumerator_next (enumerator, cancellable, error);
827 GST_DEBUG_OBJECT (server, "no more addresses %s",
828 *error ? (*error)->message : "");
830 GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
835 /* only keep the first error */
836 socket = g_socket_new (g_socket_address_get_family (sockaddr),
837 G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
838 sock_error ? NULL : &sock_error);
840 if (socket == NULL) {
841 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
842 sock_error->message);
843 g_object_unref (sockaddr);
847 if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
848 /* ask what port the socket has been bound to */
849 if (port == 0 || !strcmp (priv->service, "0")) {
850 GError *addr_error = NULL;
852 g_object_unref (sockaddr);
853 sockaddr = g_socket_get_local_address (socket, &addr_error);
855 if (addr_error != NULL) {
856 GST_DEBUG_OBJECT (server,
857 "failed to get the local address of a bound socket %s",
858 addr_error->message);
859 g_clear_error (&addr_error);
863 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr));
866 g_free (priv->service);
867 priv->service = g_strdup_printf ("%d", port);
869 GST_DEBUG_OBJECT (server, "failed to get the port of a bound socket");
872 g_object_unref (sockaddr);
876 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
877 bind_error->message);
878 g_object_unref (sockaddr);
879 g_object_unref (socket);
882 g_object_unref (enumerator);
887 g_clear_error (&sock_error);
888 g_clear_error (&bind_error);
890 GST_DEBUG_OBJECT (server, "opened sending server socket");
892 /* keep connection alive; avoids SIGPIPE during write */
893 g_socket_set_keepalive (socket, TRUE);
897 /* make sure socket is reset 5 seconds after close. This ensure that we can
898 * reuse the socket quickly while still having a chance to send data to the
902 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
903 (void *) &linger, sizeof (linger)) < 0)
908 /* set the server socket to nonblocking */
909 g_socket_set_blocking (socket, FALSE);
911 /* set listen backlog */
912 g_socket_set_listen_backlog (socket, priv->backlog);
914 if (!g_socket_listen (socket, error))
917 GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
918 socket, priv->backlog);
920 GST_RTSP_SERVER_UNLOCK (server);
927 GST_ERROR_OBJECT (server, "failed to create socket");
934 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
942 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
949 g_object_unref (socket);
953 g_propagate_error (error, sock_error);
955 g_error_free (sock_error);
958 if ((error == NULL) || (*error == NULL))
959 g_propagate_error (error, bind_error);
961 g_error_free (bind_error);
963 GST_RTSP_SERVER_UNLOCK (server);
968 struct _ClientContext
970 GstRTSPServer *server;
971 GstRTSPThread *thread;
972 GstRTSPClient *client;
976 free_client_context (ClientContext * ctx)
978 GST_DEBUG ("free context %p", ctx);
980 GST_RTSP_SERVER_LOCK (ctx->server);
982 gst_rtsp_thread_stop (ctx->thread);
983 GST_RTSP_SERVER_UNLOCK (ctx->server);
985 g_object_unref (ctx->client);
986 g_object_unref (ctx->server);
987 g_slice_free (ClientContext, ctx);
989 return G_SOURCE_REMOVE;
993 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
995 GstRTSPServer *server = ctx->server;
996 GstRTSPServerPrivate *priv = server->priv;
998 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
1000 GST_RTSP_SERVER_LOCK (server);
1001 priv->clients = g_list_remove (priv->clients, ctx);
1002 GST_RTSP_SERVER_UNLOCK (server);
1007 src = g_idle_source_new ();
1008 g_source_set_callback (src, (GSourceFunc) free_client_context, ctx, NULL);
1009 g_source_attach (src, ctx->thread->context);
1010 g_source_unref (src);
1012 free_client_context (ctx);
1016 /* add the client context to the active list of clients, takes ownership
1019 manage_client (GstRTSPServer * server, GstRTSPClient * client)
1021 ClientContext *cctx;
1022 GstRTSPServerPrivate *priv = server->priv;
1023 GMainContext *mainctx = NULL;
1024 GstRTSPContext ctx = { NULL };
1026 GST_DEBUG_OBJECT (server, "manage client %p", client);
1028 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1031 cctx = g_slice_new0 (ClientContext);
1032 cctx->server = g_object_ref (server);
1033 cctx->client = client;
1035 GST_RTSP_SERVER_LOCK (server);
1037 ctx.server = server;
1038 ctx.client = client;
1040 cctx->thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
1041 GST_RTSP_THREAD_TYPE_CLIENT, &ctx);
1043 mainctx = cctx->thread->context;
1046 /* find the context to add the watch */
1047 if ((source = g_main_current_source ()))
1048 mainctx = g_source_get_context (source);
1051 g_signal_connect (client, "closed", (GCallback) unmanage_client, cctx);
1052 priv->clients = g_list_prepend (priv->clients, cctx);
1054 gst_rtsp_client_attach (client, mainctx);
1056 GST_RTSP_SERVER_UNLOCK (server);
1059 static GstRTSPClient *
1060 default_create_client (GstRTSPServer * server)
1062 GstRTSPClient *client;
1063 GstRTSPServerPrivate *priv = server->priv;
1065 /* a new client connected, create a session to handle the client. */
1066 client = gst_rtsp_client_new ();
1068 /* set the session pool that this client should use */
1069 GST_RTSP_SERVER_LOCK (server);
1070 gst_rtsp_client_set_session_pool (client, priv->session_pool);
1071 /* set the mount points that this client should use */
1072 gst_rtsp_client_set_mount_points (client, priv->mount_points);
1073 /* set authentication manager */
1074 gst_rtsp_client_set_auth (client, priv->auth);
1075 /* set threadpool */
1076 gst_rtsp_client_set_thread_pool (client, priv->thread_pool);
1077 GST_RTSP_SERVER_UNLOCK (server);
1083 * gst_rtsp_server_transfer_connection:
1084 * @server: a #GstRTSPServer
1085 * @socket: (transfer full): a network socket
1086 * @ip: the IP address of the remote client
1087 * @port: the port used by the other end
1088 * @initial_buffer: any initial data that was already read from the socket
1090 * Take an existing network socket and use it for an RTSP connection. This
1091 * is used when transferring a socket from an HTTP server which should be used
1092 * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
1093 * that the HTTP server read from the socket while parsing the HTTP header.
1095 * Returns: TRUE if all was ok, FALSE if an error occurred.
1098 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
1099 const gchar * ip, gint port, const gchar * initial_buffer)
1101 GstRTSPClient *client = NULL;
1102 GstRTSPServerClass *klass;
1103 GstRTSPConnection *conn;
1106 klass = GST_RTSP_SERVER_GET_CLASS (server);
1108 if (klass->create_client)
1109 client = klass->create_client (server);
1113 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
1114 initial_buffer, &conn), no_connection);
1115 g_object_unref (socket);
1117 /* set connection on the client now */
1118 gst_rtsp_client_set_connection (client, conn);
1120 /* manage the client connection */
1121 manage_client (server, client);
1128 GST_ERROR_OBJECT (server, "failed to create a client");
1129 g_object_unref (socket);
1134 gchar *str = gst_rtsp_strresult (res);
1135 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
1137 g_object_unref (socket);
1143 * gst_rtsp_server_io_func:
1144 * @socket: a #GSocket
1145 * @condition: the condition on @source
1146 * @server: (transfer none): a #GstRTSPServer
1148 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
1149 * new connection on @socket or @server.
1151 * Returns: TRUE if the source could be connected, FALSE if an error occurred.
1154 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
1155 GstRTSPServer * server)
1157 GstRTSPServerPrivate *priv = server->priv;
1158 GstRTSPClient *client = NULL;
1159 GstRTSPServerClass *klass;
1161 GstRTSPConnection *conn = NULL;
1162 GstRTSPContext ctx = { NULL };
1164 if (condition & G_IO_IN) {
1165 /* a new client connected. */
1166 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, NULL),
1169 ctx.server = server;
1171 ctx.auth = priv->auth;
1172 gst_rtsp_context_push_current (&ctx);
1174 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_CONNECT))
1175 goto connection_refused;
1177 klass = GST_RTSP_SERVER_GET_CLASS (server);
1178 /* a new client connected, create a client object to handle the client. */
1179 if (klass->create_client)
1180 client = klass->create_client (server);
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 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
1193 gst_rtsp_context_pop_current (&ctx);
1195 return G_SOURCE_CONTINUE;
1200 gchar *str = gst_rtsp_strresult (res);
1201 GST_ERROR_OBJECT (server, "Could not accept client on socket %p: %s",
1208 GST_ERROR_OBJECT (server, "connection refused");
1209 gst_rtsp_connection_free (conn);
1214 GST_ERROR_OBJECT (server, "failed to create a client");
1215 gst_rtsp_connection_free (conn);
1221 watch_destroyed (GstRTSPServer * server)
1223 GstRTSPServerPrivate *priv = server->priv;
1225 GST_DEBUG_OBJECT (server, "source destroyed");
1227 g_object_unref (priv->socket);
1228 priv->socket = NULL;
1229 g_object_unref (server);
1233 * gst_rtsp_server_create_source:
1234 * @server: a #GstRTSPServer
1235 * @cancellable: (allow-none): a #GCancellable or %NULL.
1236 * @error: (out): a #GError
1238 * Create a #GSource for @server. The new source will have a default
1239 * #GSocketSourceFunc of gst_rtsp_server_io_func().
1241 * @cancellable if not %NULL can be used to cancel the source, which will cause
1242 * the source to trigger, reporting the current condition (which is likely 0
1243 * unless cancellation happened at the same time as a condition change). You can
1244 * check for this in the callback using g_cancellable_is_cancelled().
1246 * Returns: (transfer full): the #GSource for @server or %NULL when an error
1247 * occurred. Free with g_source_unref ()
1250 gst_rtsp_server_create_source (GstRTSPServer * server,
1251 GCancellable * cancellable, GError ** error)
1253 GstRTSPServerPrivate *priv;
1254 GSocket *socket, *old;
1257 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1259 priv = server->priv;
1261 socket = gst_rtsp_server_create_socket (server, NULL, error);
1265 GST_RTSP_SERVER_LOCK (server);
1267 priv->socket = g_object_ref (socket);
1268 GST_RTSP_SERVER_UNLOCK (server);
1271 g_object_unref (old);
1273 /* create a watch for reads (new connections) and possible errors */
1274 source = g_socket_create_source (socket, G_IO_IN |
1275 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
1276 g_object_unref (socket);
1278 /* configure the callback */
1279 g_source_set_callback (source,
1280 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
1281 (GDestroyNotify) watch_destroyed);
1287 GST_ERROR_OBJECT (server, "failed to create socket");
1293 * gst_rtsp_server_attach:
1294 * @server: a #GstRTSPServer
1295 * @context: (allow-none): a #GMainContext
1297 * Attaches @server to @context. When the mainloop for @context is run, the
1298 * server will be dispatched. When @context is %NULL, the default context will be
1301 * This function should be called when the server properties and urls are fully
1302 * configured and the server is ready to start.
1304 * Returns: the ID (greater than 0) for the source within the GMainContext.
1307 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1311 GError *error = NULL;
1313 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1315 source = gst_rtsp_server_create_source (server, NULL, &error);
1319 res = g_source_attach (source, context);
1320 g_source_unref (source);
1327 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1328 g_error_free (error);
1334 * gst_rtsp_server_client_filter:
1335 * @server: a #GstRTSPServer
1336 * @func: (scope call) (allow-none): a callback
1337 * @user_data: user data passed to @func
1339 * Call @func for each client managed by @server. The result value of @func
1340 * determines what happens to the client. @func will be called with @server
1341 * locked so no further actions on @server can be performed from @func.
1343 * If @func returns #GST_RTSP_FILTER_REMOVE, the client will be removed from
1346 * If @func returns #GST_RTSP_FILTER_KEEP, the client will remain in @server.
1348 * If @func returns #GST_RTSP_FILTER_REF, the client will remain in @server but
1349 * will also be added with an additional ref to the result #GList of this
1352 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each client.
1354 * Returns: (element-type GstRTSPClient) (transfer full): a #GList with all
1355 * clients for which @func returned #GST_RTSP_FILTER_REF. After usage, each
1356 * element in the #GList should be unreffed before the list is freed.
1359 gst_rtsp_server_client_filter (GstRTSPServer * server,
1360 GstRTSPServerClientFilterFunc func, gpointer user_data)
1362 GstRTSPServerPrivate *priv;
1363 GList *result, *walk, *next;
1365 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1367 priv = server->priv;
1371 GST_RTSP_SERVER_LOCK (server);
1372 for (walk = priv->clients; walk; walk = next) {
1373 ClientContext *cctx = walk->data;
1374 GstRTSPFilterResult res;
1376 next = g_list_next (walk);
1379 res = func (server, cctx->client, user_data);
1381 res = GST_RTSP_FILTER_REF;
1384 case GST_RTSP_FILTER_REMOVE:
1385 /* remove client, FIXME */
1387 case GST_RTSP_FILTER_REF:
1388 result = g_list_prepend (result, g_object_ref (cctx->client));
1390 case GST_RTSP_FILTER_KEEP:
1395 GST_RTSP_SERVER_UNLOCK (server);