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 */
76 gboolean use_client_settings;
80 /* sessions on this server */
81 GstRTSPSessionPool *session_pool;
83 /* mount points for this server */
84 GstRTSPMountPoints *mount_points;
86 /* authentication manager */
89 /* resource manager */
90 GstRTSPThreadPool *thread_pool;
92 /* the clients that are connected */
96 #define DEFAULT_ADDRESS "0.0.0.0"
97 #define DEFAULT_BOUND_PORT -1
98 /* #define DEFAULT_ADDRESS "::0" */
99 #define DEFAULT_SERVICE "8554"
100 #define DEFAULT_BACKLOG 5
101 #define DEFAULT_USE_CLIENT_SETTINGS FALSE
103 /* Define to use the SO_LINGER option so that the server sockets can be resused
104 * sooner. Disabled for now because it is not very well implemented by various
105 * OSes and it causes clients to fail to read the TEARDOWN response. */
118 PROP_USE_CLIENT_SETTINGS,
124 SIGNAL_CLIENT_CONNECTED,
128 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
130 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
131 #define GST_CAT_DEFAULT rtsp_server_debug
133 typedef struct _ClientContext ClientContext;
135 static guint gst_rtsp_server_signals[SIGNAL_LAST] = { 0 };
137 static void gst_rtsp_server_get_property (GObject * object, guint propid,
138 GValue * value, GParamSpec * pspec);
139 static void gst_rtsp_server_set_property (GObject * object, guint propid,
140 const GValue * value, GParamSpec * pspec);
141 static void gst_rtsp_server_finalize (GObject * object);
143 static GstRTSPClient *default_create_client (GstRTSPServer * server);
146 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
148 GObjectClass *gobject_class;
150 g_type_class_add_private (klass, sizeof (GstRTSPServerPrivate));
152 gobject_class = G_OBJECT_CLASS (klass);
154 gobject_class->get_property = gst_rtsp_server_get_property;
155 gobject_class->set_property = gst_rtsp_server_set_property;
156 gobject_class->finalize = gst_rtsp_server_finalize;
159 * GstRTSPServer::address:
161 * The address of the server. This is the address where the server will
164 g_object_class_install_property (gobject_class, PROP_ADDRESS,
165 g_param_spec_string ("address", "Address",
166 "The address the server uses to listen on", DEFAULT_ADDRESS,
167 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 * GstRTSPServer::service:
171 * The service of the server. This is either a string with the service name or
172 * a port number (as a string) the server will listen on.
174 g_object_class_install_property (gobject_class, PROP_SERVICE,
175 g_param_spec_string ("service", "Service",
176 "The service or port number the server uses to listen on",
177 DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
179 * GstRTSPServer::bound-port:
181 * The actual port the server is listening on. Can be used to retrieve the
182 * port number when the server is started on port 0, which means bind to a
183 * random port. Set to -1 if the server has not been bound yet.
185 g_object_class_install_property (gobject_class, PROP_BOUND_PORT,
186 g_param_spec_int ("bound-port", "Bound port",
187 "The port number the server is listening on",
188 -1, G_MAXUINT16, DEFAULT_BOUND_PORT,
189 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
191 * GstRTSPServer::backlog:
193 * The backlog argument defines the maximum length to which the queue of
194 * pending connections for the server may grow. If a connection request arrives
195 * when the queue is full, the client may receive an error with an indication of
196 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
197 * request may be ignored so that a later reattempt at connection succeeds.
199 g_object_class_install_property (gobject_class, PROP_BACKLOG,
200 g_param_spec_int ("backlog", "Backlog",
201 "The maximum length to which the queue "
202 "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
203 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
205 * GstRTSPServer::session-pool:
207 * The session pool of the server. By default each server has a separate
208 * session pool but sessions can be shared between servers by setting the same
209 * session pool on multiple servers.
211 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
212 g_param_spec_object ("session-pool", "Session Pool",
213 "The session pool to use for client session",
214 GST_TYPE_RTSP_SESSION_POOL,
215 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
217 * GstRTSPServer::mount-points:
219 * The mount points to use for this server. By default the server has no
220 * mount points and thus cannot map urls to media streams.
222 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
223 g_param_spec_object ("mount-points", "Mount Points",
224 "The mount points to use for client session",
225 GST_TYPE_RTSP_MOUNT_POINTS,
226 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 * GstRTSPServer::use-client-settings:
230 * Use client transport settings (destination, port pair and ttl for
231 * multicast. FALSE means that the server settings will be used.
233 g_object_class_install_property (gobject_class, PROP_USE_CLIENT_SETTINGS,
234 g_param_spec_boolean ("use-client-settings", "Use Client Settings",
235 "Use client settings for ttl, destination and port pair in multicast",
236 DEFAULT_USE_CLIENT_SETTINGS,
237 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED] =
240 g_signal_new ("client-connected", G_TYPE_FROM_CLASS (gobject_class),
241 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPServerClass, client_connected),
242 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
243 gst_rtsp_client_get_type ());
245 klass->create_client = default_create_client;
247 GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
251 gst_rtsp_server_init (GstRTSPServer * server)
253 GstRTSPServerPrivate *priv = GST_RTSP_SERVER_GET_PRIVATE (server);
257 g_mutex_init (&priv->lock);
258 priv->address = g_strdup (DEFAULT_ADDRESS);
259 priv->service = g_strdup (DEFAULT_SERVICE);
261 priv->backlog = DEFAULT_BACKLOG;
262 priv->session_pool = gst_rtsp_session_pool_new ();
263 priv->mount_points = gst_rtsp_mount_points_new ();
264 priv->thread_pool = gst_rtsp_thread_pool_new ();
265 priv->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
269 gst_rtsp_server_finalize (GObject * object)
271 GstRTSPServer *server = GST_RTSP_SERVER (object);
272 GstRTSPServerPrivate *priv = server->priv;
274 GST_DEBUG_OBJECT (server, "finalize server");
276 g_free (priv->address);
277 g_free (priv->service);
280 g_object_unref (priv->socket);
282 if (priv->session_pool)
283 g_object_unref (priv->session_pool);
284 if (priv->mount_points)
285 g_object_unref (priv->mount_points);
286 if (priv->thread_pool)
287 g_object_unref (priv->thread_pool);
290 g_object_unref (priv->auth);
292 g_mutex_clear (&priv->lock);
294 G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
298 * gst_rtsp_server_new:
300 * Create a new #GstRTSPServer instance.
303 gst_rtsp_server_new (void)
305 GstRTSPServer *result;
307 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
313 * gst_rtsp_server_set_address:
314 * @server: a #GstRTSPServer
315 * @address: the address
317 * Configure @server to accept connections on the given address.
319 * This function must be called before the server is bound.
322 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
324 GstRTSPServerPrivate *priv;
326 g_return_if_fail (GST_IS_RTSP_SERVER (server));
327 g_return_if_fail (address != NULL);
331 GST_RTSP_SERVER_LOCK (server);
332 g_free (priv->address);
333 priv->address = g_strdup (address);
334 GST_RTSP_SERVER_UNLOCK (server);
338 * gst_rtsp_server_get_address:
339 * @server: a #GstRTSPServer
341 * Get the address on which the server will accept connections.
343 * Returns: the server address. g_free() after usage.
346 gst_rtsp_server_get_address (GstRTSPServer * server)
348 GstRTSPServerPrivate *priv;
351 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
355 GST_RTSP_SERVER_LOCK (server);
356 result = g_strdup (priv->address);
357 GST_RTSP_SERVER_UNLOCK (server);
363 * gst_rtsp_server_get_bound_port:
364 * @server: a #GstRTSPServer
366 * Get the port number where the server was bound to.
368 * Returns: the port number
371 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
373 GstRTSPServerPrivate *priv;
374 GSocketAddress *address;
377 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
381 GST_RTSP_SERVER_LOCK (server);
382 if (priv->socket == NULL)
385 address = g_socket_get_local_address (priv->socket, NULL);
386 result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
387 g_object_unref (address);
390 GST_RTSP_SERVER_UNLOCK (server);
396 * gst_rtsp_server_set_service:
397 * @server: a #GstRTSPServer
398 * @service: the service
400 * Configure @server to accept connections on the given service.
401 * @service should be a string containing the service name (see services(5)) or
402 * a string containing a port number between 1 and 65535.
404 * When @service is set to "0", the server will listen on a random free
405 * port. The actual used port can be retrieved with
406 * gst_rtsp_server_get_bound_port().
408 * This function must be called before the server is bound.
411 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
413 GstRTSPServerPrivate *priv;
415 g_return_if_fail (GST_IS_RTSP_SERVER (server));
416 g_return_if_fail (service != NULL);
420 GST_RTSP_SERVER_LOCK (server);
421 g_free (priv->service);
422 priv->service = g_strdup (service);
423 GST_RTSP_SERVER_UNLOCK (server);
427 * gst_rtsp_server_get_service:
428 * @server: a #GstRTSPServer
430 * Get the service on which the server will accept connections.
432 * Returns: the service. use g_free() after usage.
435 gst_rtsp_server_get_service (GstRTSPServer * server)
437 GstRTSPServerPrivate *priv;
440 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
444 GST_RTSP_SERVER_LOCK (server);
445 result = g_strdup (priv->service);
446 GST_RTSP_SERVER_UNLOCK (server);
452 * gst_rtsp_server_set_backlog:
453 * @server: a #GstRTSPServer
454 * @backlog: the backlog
456 * configure the maximum amount of requests that may be queued for the
459 * This function must be called before the server is bound.
462 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
464 GstRTSPServerPrivate *priv;
466 g_return_if_fail (GST_IS_RTSP_SERVER (server));
470 GST_RTSP_SERVER_LOCK (server);
471 priv->backlog = backlog;
472 GST_RTSP_SERVER_UNLOCK (server);
476 * gst_rtsp_server_get_backlog:
477 * @server: a #GstRTSPServer
479 * The maximum amount of queued requests for the server.
481 * Returns: the server backlog.
484 gst_rtsp_server_get_backlog (GstRTSPServer * server)
486 GstRTSPServerPrivate *priv;
489 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
493 GST_RTSP_SERVER_LOCK (server);
494 result = priv->backlog;
495 GST_RTSP_SERVER_UNLOCK (server);
501 * gst_rtsp_server_set_session_pool:
502 * @server: a #GstRTSPServer
503 * @pool: a #GstRTSPSessionPool
505 * configure @pool to be used as the session pool of @server.
508 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
509 GstRTSPSessionPool * pool)
511 GstRTSPServerPrivate *priv;
512 GstRTSPSessionPool *old;
514 g_return_if_fail (GST_IS_RTSP_SERVER (server));
521 GST_RTSP_SERVER_LOCK (server);
522 old = priv->session_pool;
523 priv->session_pool = pool;
524 GST_RTSP_SERVER_UNLOCK (server);
527 g_object_unref (old);
531 * gst_rtsp_server_get_session_pool:
532 * @server: a #GstRTSPServer
534 * Get the #GstRTSPSessionPool used as the session pool of @server.
536 * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
540 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
542 GstRTSPServerPrivate *priv;
543 GstRTSPSessionPool *result;
545 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
549 GST_RTSP_SERVER_LOCK (server);
550 if ((result = priv->session_pool))
551 g_object_ref (result);
552 GST_RTSP_SERVER_UNLOCK (server);
558 * gst_rtsp_server_set_mount_points:
559 * @server: a #GstRTSPServer
560 * @mounts: a #GstRTSPMountPoints
562 * configure @mounts to be used as the mount points of @server.
565 gst_rtsp_server_set_mount_points (GstRTSPServer * server,
566 GstRTSPMountPoints * mounts)
568 GstRTSPServerPrivate *priv;
569 GstRTSPMountPoints *old;
571 g_return_if_fail (GST_IS_RTSP_SERVER (server));
576 g_object_ref (mounts);
578 GST_RTSP_SERVER_LOCK (server);
579 old = priv->mount_points;
580 priv->mount_points = mounts;
581 GST_RTSP_SERVER_UNLOCK (server);
584 g_object_unref (old);
589 * gst_rtsp_server_get_mount_points:
590 * @server: a #GstRTSPServer
592 * Get the #GstRTSPMountPoints used as the mount points of @server.
594 * Returns: (transfer full): the #GstRTSPMountPoints of @server. g_object_unref() after
598 gst_rtsp_server_get_mount_points (GstRTSPServer * server)
600 GstRTSPServerPrivate *priv;
601 GstRTSPMountPoints *result;
603 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
607 GST_RTSP_SERVER_LOCK (server);
608 if ((result = priv->mount_points))
609 g_object_ref (result);
610 GST_RTSP_SERVER_UNLOCK (server);
616 * gst_rtsp_server_set_auth:
617 * @server: a #GstRTSPServer
618 * @auth: a #GstRTSPAuth
620 * configure @auth to be used as the authentication manager of @server.
623 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
625 GstRTSPServerPrivate *priv;
628 g_return_if_fail (GST_IS_RTSP_SERVER (server));
635 GST_RTSP_SERVER_LOCK (server);
638 GST_RTSP_SERVER_UNLOCK (server);
641 g_object_unref (old);
646 * gst_rtsp_server_get_auth:
647 * @server: a #GstRTSPServer
649 * Get the #GstRTSPAuth used as the authentication manager of @server.
651 * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
655 gst_rtsp_server_get_auth (GstRTSPServer * server)
657 GstRTSPServerPrivate *priv;
660 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
664 GST_RTSP_SERVER_LOCK (server);
665 if ((result = priv->auth))
666 g_object_ref (result);
667 GST_RTSP_SERVER_UNLOCK (server);
673 * gst_rtsp_server_set_thread_pool:
674 * @server: a #GstRTSPServer
675 * @pool: a #GstRTSPThreadPool
677 * configure @pool to be used as the thread pool of @server.
680 gst_rtsp_server_set_thread_pool (GstRTSPServer * server,
681 GstRTSPThreadPool * pool)
683 GstRTSPServerPrivate *priv;
684 GstRTSPThreadPool *old;
686 g_return_if_fail (GST_IS_RTSP_SERVER (server));
693 GST_RTSP_SERVER_LOCK (server);
694 old = priv->thread_pool;
695 priv->thread_pool = pool;
696 GST_RTSP_SERVER_UNLOCK (server);
699 g_object_unref (old);
703 * gst_rtsp_server_get_thread_pool:
704 * @server: a #GstRTSPServer
706 * Get the #GstRTSPThreadPool used as the thread pool of @server.
708 * Returns: (transfer full): the #GstRTSPThreadPool of @server. g_object_unref() after
712 gst_rtsp_server_get_thread_pool (GstRTSPServer * server)
714 GstRTSPServerPrivate *priv;
715 GstRTSPThreadPool *result;
717 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
721 GST_RTSP_SERVER_LOCK (server);
722 if ((result = priv->thread_pool))
723 g_object_ref (result);
724 GST_RTSP_SERVER_UNLOCK (server);
730 * gst_rtsp_server_set_use_client_settings:
731 * @server: a #GstRTSPServer
732 * @use_client_settings: whether to use client settings for multicast
734 * Use client transport settings (destination, port pair and ttl) for
736 * When @use_client_settings is %FALSE, the server settings will be
740 gst_rtsp_server_set_use_client_settings (GstRTSPServer * server,
741 gboolean use_client_settings)
743 GstRTSPServerPrivate *priv;
745 g_return_if_fail (GST_IS_RTSP_SERVER (server));
749 GST_RTSP_SERVER_LOCK (server);
750 priv->use_client_settings = use_client_settings;
751 GST_RTSP_SERVER_UNLOCK (server);
755 * gst_rtsp_server_get_use_client_settings:
756 * @server: a #GstRTSPServer
758 * Check if client transport settings (destination, port pair and ttl) for
759 * multicast will be used.
762 gst_rtsp_server_get_use_client_settings (GstRTSPServer * server)
764 GstRTSPServerPrivate *priv;
767 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), FALSE);
771 GST_RTSP_SERVER_LOCK (server);
772 res = priv->use_client_settings;
773 GST_RTSP_SERVER_UNLOCK (server);
779 gst_rtsp_server_get_property (GObject * object, guint propid,
780 GValue * value, GParamSpec * pspec)
782 GstRTSPServer *server = GST_RTSP_SERVER (object);
786 g_value_take_string (value, gst_rtsp_server_get_address (server));
789 g_value_take_string (value, gst_rtsp_server_get_service (server));
791 case PROP_BOUND_PORT:
792 g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
795 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
797 case PROP_SESSION_POOL:
798 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
800 case PROP_MOUNT_POINTS:
801 g_value_take_object (value, gst_rtsp_server_get_mount_points (server));
803 case PROP_USE_CLIENT_SETTINGS:
804 g_value_set_boolean (value,
805 gst_rtsp_server_get_use_client_settings (server));
808 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
813 gst_rtsp_server_set_property (GObject * object, guint propid,
814 const GValue * value, GParamSpec * pspec)
816 GstRTSPServer *server = GST_RTSP_SERVER (object);
820 gst_rtsp_server_set_address (server, g_value_get_string (value));
823 gst_rtsp_server_set_service (server, g_value_get_string (value));
826 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
828 case PROP_SESSION_POOL:
829 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
831 case PROP_MOUNT_POINTS:
832 gst_rtsp_server_set_mount_points (server, g_value_get_object (value));
834 case PROP_USE_CLIENT_SETTINGS:
835 gst_rtsp_server_set_use_client_settings (server,
836 g_value_get_boolean (value));
839 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
844 * gst_rtsp_server_create_socket:
845 * @server: a #GstRTSPServer
846 * @cancellable: a #GCancellable
849 * Create a #GSocket for @server. The socket will listen on the
850 * configured service.
852 * Returns: (transfer full): the #GSocket for @server or NULL when an error occured.
855 gst_rtsp_server_create_socket (GstRTSPServer * server,
856 GCancellable * cancellable, GError ** error)
858 GstRTSPServerPrivate *priv;
859 GSocketConnectable *conn;
860 GSocketAddressEnumerator *enumerator;
861 GSocket *socket = NULL;
863 struct linger linger;
865 GError *sock_error = NULL;
866 GError *bind_error = NULL;
869 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
873 GST_RTSP_SERVER_LOCK (server);
874 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", priv->address,
877 /* resolve the server IP address */
878 port = atoi (priv->service);
879 if (port != 0 || !strcmp (priv->service, "0"))
880 conn = g_network_address_new (priv->address, port);
882 conn = g_network_service_new (priv->service, "tcp", priv->address);
884 enumerator = g_socket_connectable_enumerate (conn);
885 g_object_unref (conn);
887 /* create server socket, we loop through all the addresses until we manage to
888 * create a socket and bind. */
890 GSocketAddress *sockaddr;
893 g_socket_address_enumerator_next (enumerator, cancellable, error);
896 GST_DEBUG_OBJECT (server, "no more addresses %s",
897 *error ? (*error)->message : "");
899 GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
904 /* only keep the first error */
905 socket = g_socket_new (g_socket_address_get_family (sockaddr),
906 G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
907 sock_error ? NULL : &sock_error);
909 if (socket == NULL) {
910 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
911 sock_error->message);
912 g_object_unref (sockaddr);
916 if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
917 g_object_unref (sockaddr);
921 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
922 bind_error->message);
923 g_object_unref (sockaddr);
924 g_object_unref (socket);
927 g_object_unref (enumerator);
932 g_clear_error (&sock_error);
933 g_clear_error (&bind_error);
935 GST_DEBUG_OBJECT (server, "opened sending server socket");
937 /* keep connection alive; avoids SIGPIPE during write */
938 g_socket_set_keepalive (socket, TRUE);
942 /* make sure socket is reset 5 seconds after close. This ensure that we can
943 * reuse the socket quickly while still having a chance to send data to the
947 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
948 (void *) &linger, sizeof (linger)) < 0)
953 /* set the server socket to nonblocking */
954 g_socket_set_blocking (socket, FALSE);
956 /* set listen backlog */
957 g_socket_set_listen_backlog (socket, priv->backlog);
959 if (!g_socket_listen (socket, error))
962 GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
963 socket, priv->backlog);
965 GST_RTSP_SERVER_UNLOCK (server);
972 GST_ERROR_OBJECT (server, "failed to create socket");
979 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
987 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
994 g_object_unref (socket);
998 g_propagate_error (error, sock_error);
1000 g_error_free (sock_error);
1003 if ((error == NULL) || (*error == NULL))
1004 g_propagate_error (error, bind_error);
1006 g_error_free (bind_error);
1008 GST_RTSP_SERVER_UNLOCK (server);
1013 struct _ClientContext
1015 GstRTSPServer *server;
1016 GstRTSPThread *thread;
1017 GstRTSPClient *client;
1021 free_client_context (ClientContext * ctx)
1023 GST_RTSP_SERVER_LOCK (ctx->server);
1025 gst_rtsp_thread_stop (ctx->thread);
1026 GST_RTSP_SERVER_UNLOCK (ctx->server);
1028 g_object_unref (ctx->client);
1029 g_object_unref (ctx->server);
1030 g_slice_free (ClientContext, ctx);
1032 return G_SOURCE_REMOVE;
1036 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
1038 GstRTSPServer *server = ctx->server;
1039 GstRTSPServerPrivate *priv = server->priv;
1041 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
1043 GST_RTSP_SERVER_LOCK (server);
1044 priv->clients = g_list_remove (priv->clients, ctx);
1045 GST_RTSP_SERVER_UNLOCK (server);
1050 src = g_idle_source_new ();
1051 g_source_set_callback (src, (GSourceFunc) free_client_context, ctx, NULL);
1052 g_source_attach (src, ctx->thread->context);
1053 g_source_unref (src);
1055 free_client_context (ctx);
1059 /* add the client context to the active list of clients, takes ownership
1062 manage_client (GstRTSPServer * server, GstRTSPClient * client)
1065 GstRTSPServerPrivate *priv = server->priv;
1066 GMainContext *mainctx = NULL;
1067 GstRTSPClientState state = { NULL };
1069 GST_DEBUG_OBJECT (server, "manage client %p", client);
1071 ctx = g_slice_new0 (ClientContext);
1072 ctx->server = g_object_ref (server);
1073 ctx->client = client;
1075 GST_RTSP_SERVER_LOCK (server);
1077 state.server = server;
1078 state.client = client;
1080 ctx->thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
1081 GST_RTSP_THREAD_TYPE_CLIENT, &state);
1083 mainctx = ctx->thread->context;
1086 /* find the context to add the watch */
1087 if ((source = g_main_current_source ()))
1088 mainctx = g_source_get_context (source);
1091 g_signal_connect (client, "closed", (GCallback) unmanage_client, ctx);
1092 priv->clients = g_list_prepend (priv->clients, ctx);
1094 gst_rtsp_client_attach (client, mainctx);
1096 GST_RTSP_SERVER_UNLOCK (server);
1099 static GstRTSPClient *
1100 default_create_client (GstRTSPServer * server)
1102 GstRTSPClient *client;
1103 GstRTSPServerPrivate *priv = server->priv;
1105 /* a new client connected, create a session to handle the client. */
1106 client = gst_rtsp_client_new ();
1108 /* set the session pool that this client should use */
1109 GST_RTSP_SERVER_LOCK (server);
1110 gst_rtsp_client_set_session_pool (client, priv->session_pool);
1111 /* set the mount points that this client should use */
1112 gst_rtsp_client_set_mount_points (client, priv->mount_points);
1113 /* set authentication manager */
1114 gst_rtsp_client_set_auth (client, priv->auth);
1115 /* set threadpool */
1116 gst_rtsp_client_set_thread_pool (client, priv->thread_pool);
1117 /* check if client transport settings for multicast are allowed */
1118 gst_rtsp_client_set_use_client_settings (client, priv->use_client_settings);
1119 GST_RTSP_SERVER_UNLOCK (server);
1125 * gst_rtsp_server_transfer_connection:
1126 * @server: a #GstRTSPServer
1127 * @socket: a network socket
1128 * @ip: the IP address of the remote client
1129 * @port: the port used by the other end
1130 * @initial_buffer: any initial data that was already read from the socket
1132 * Take an existing network socket and use it for an RTSP connection. This
1133 * is used when transferring a socket from an HTTP server which should be used
1134 * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
1135 * that the HTTP server read from the socket while parsing the HTTP header.
1137 * Returns: TRUE if all was ok, FALSE if an error occured.
1140 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
1141 const gchar * ip, gint port, const gchar * initial_buffer)
1143 GstRTSPClient *client = NULL;
1144 GstRTSPServerClass *klass;
1145 GstRTSPConnection *conn;
1148 klass = GST_RTSP_SERVER_GET_CLASS (server);
1150 if (klass->create_client)
1151 client = klass->create_client (server);
1155 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
1156 initial_buffer, &conn), no_connection);
1158 /* set connection on the client now */
1159 gst_rtsp_client_set_connection (client, conn);
1161 /* manage the client connection */
1162 manage_client (server, client);
1164 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1172 GST_ERROR_OBJECT (server, "failed to create a client");
1177 gchar *str = gst_rtsp_strresult (res);
1178 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
1185 * gst_rtsp_server_io_func:
1186 * @socket: a #GSocket
1187 * @condition: the condition on @source
1188 * @server: a #GstRTSPServer
1190 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
1191 * new connection on @socket or @server.
1193 * Returns: TRUE if the source could be connected, FALSE if an error occured.
1196 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
1197 GstRTSPServer * server)
1199 GstRTSPServerPrivate *priv = server->priv;
1200 GstRTSPClient *client = NULL;
1201 GstRTSPServerClass *klass;
1203 GstRTSPConnection *conn = NULL;
1204 GstRTSPClientState state = { NULL };
1206 if (condition & G_IO_IN) {
1207 /* a new client connected. */
1208 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, NULL),
1211 state.server = server;
1213 state.auth = priv->auth;
1214 gst_rtsp_client_state_push_current (&state);
1216 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_CONNECT))
1217 goto connection_refused;
1219 klass = GST_RTSP_SERVER_GET_CLASS (server);
1220 /* a new client connected, create a client object to handle the client. */
1221 if (klass->create_client)
1222 client = klass->create_client (server);
1226 /* set connection on the client now */
1227 gst_rtsp_client_set_connection (client, conn);
1229 /* manage the client connection */
1230 manage_client (server, client);
1232 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1235 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
1238 gst_rtsp_client_state_pop_current (&state);
1240 return G_SOURCE_CONTINUE;
1245 gchar *str = gst_rtsp_strresult (res);
1246 GST_ERROR_OBJECT (server, "Could not accept client on socket %p: %s",
1253 GST_ERROR_OBJECT (server, "connection refused");
1254 gst_rtsp_connection_free (conn);
1259 GST_ERROR_OBJECT (server, "failed to create a client");
1260 gst_rtsp_connection_free (conn);
1266 watch_destroyed (GstRTSPServer * server)
1268 GstRTSPServerPrivate *priv = server->priv;
1270 GST_DEBUG_OBJECT (server, "source destroyed");
1272 g_object_unref (priv->socket);
1273 priv->socket = NULL;
1274 g_object_unref (server);
1278 * gst_rtsp_server_create_source:
1279 * @server: a #GstRTSPServer
1280 * @cancellable: a #GCancellable or %NULL.
1283 * Create a #GSource for @server. The new source will have a default
1284 * #GSocketSourceFunc of gst_rtsp_server_io_func().
1286 * @cancellable if not NULL can be used to cancel the source, which will cause
1287 * the source to trigger, reporting the current condition (which is likely 0
1288 * unless cancellation happened at the same time as a condition change). You can
1289 * check for this in the callback using g_cancellable_is_cancelled().
1291 * Returns: the #GSource for @server or NULL when an error occured. Free with
1295 gst_rtsp_server_create_source (GstRTSPServer * server,
1296 GCancellable * cancellable, GError ** error)
1298 GstRTSPServerPrivate *priv;
1299 GSocket *socket, *old;
1302 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1304 priv = server->priv;
1306 socket = gst_rtsp_server_create_socket (server, NULL, error);
1310 GST_RTSP_SERVER_LOCK (server);
1312 priv->socket = g_object_ref (socket);
1313 GST_RTSP_SERVER_UNLOCK (server);
1316 g_object_unref (old);
1318 /* create a watch for reads (new connections) and possible errors */
1319 source = g_socket_create_source (socket, G_IO_IN |
1320 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
1321 g_object_unref (socket);
1323 /* configure the callback */
1324 g_source_set_callback (source,
1325 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
1326 (GDestroyNotify) watch_destroyed);
1332 GST_ERROR_OBJECT (server, "failed to create socket");
1338 * gst_rtsp_server_attach:
1339 * @server: a #GstRTSPServer
1340 * @context: (allow-none): a #GMainContext
1342 * Attaches @server to @context. When the mainloop for @context is run, the
1343 * server will be dispatched. When @context is NULL, the default context will be
1346 * This function should be called when the server properties and urls are fully
1347 * configured and the server is ready to start.
1349 * Returns: the ID (greater than 0) for the source within the GMainContext.
1352 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1356 GError *error = NULL;
1358 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1360 source = gst_rtsp_server_create_source (server, NULL, &error);
1364 res = g_source_attach (source, context);
1365 g_source_unref (source);
1372 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1373 g_error_free (error);