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 */
41 gboolean use_client_settings;
45 /* sessions on this server */
46 GstRTSPSessionPool *session_pool;
48 /* mount points for this server */
49 GstRTSPMountPoints *mount_points;
51 /* authentication manager */
54 /* resource manager */
55 GstRTSPThreadPool *thread_pool;
57 /* the TLS certificate */
58 GTlsCertificate *certificate;
60 /* the clients that are connected */
64 #define DEFAULT_ADDRESS "0.0.0.0"
65 #define DEFAULT_BOUND_PORT -1
66 /* #define DEFAULT_ADDRESS "::0" */
67 #define DEFAULT_SERVICE "8554"
68 #define DEFAULT_BACKLOG 5
69 #define DEFAULT_USE_CLIENT_SETTINGS FALSE
71 /* Define to use the SO_LINGER option so that the server sockets can be resused
72 * sooner. Disabled for now because it is not very well implemented by various
73 * OSes and it causes clients to fail to read the TEARDOWN response. */
86 PROP_USE_CLIENT_SETTINGS,
92 SIGNAL_CLIENT_CONNECTED,
96 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
98 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
99 #define GST_CAT_DEFAULT rtsp_server_debug
101 typedef struct _ClientContext ClientContext;
102 typedef struct _Loop Loop;
104 static guint gst_rtsp_server_signals[SIGNAL_LAST] = { 0 };
106 static void gst_rtsp_server_get_property (GObject * object, guint propid,
107 GValue * value, GParamSpec * pspec);
108 static void gst_rtsp_server_set_property (GObject * object, guint propid,
109 const GValue * value, GParamSpec * pspec);
110 static void gst_rtsp_server_finalize (GObject * object);
112 static GstRTSPClient *default_create_client (GstRTSPServer * server);
113 static gboolean default_setup_connection (GstRTSPServer * server,
114 GstRTSPClient * client, GstRTSPConnection * conn);
117 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
119 GObjectClass *gobject_class;
121 g_type_class_add_private (klass, sizeof (GstRTSPServerPrivate));
123 gobject_class = G_OBJECT_CLASS (klass);
125 gobject_class->get_property = gst_rtsp_server_get_property;
126 gobject_class->set_property = gst_rtsp_server_set_property;
127 gobject_class->finalize = gst_rtsp_server_finalize;
130 * GstRTSPServer::address:
132 * The address of the server. This is the address where the server will
135 g_object_class_install_property (gobject_class, PROP_ADDRESS,
136 g_param_spec_string ("address", "Address",
137 "The address the server uses to listen on", DEFAULT_ADDRESS,
138 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
140 * GstRTSPServer::service:
142 * The service of the server. This is either a string with the service name or
143 * a port number (as a string) the server will listen on.
145 g_object_class_install_property (gobject_class, PROP_SERVICE,
146 g_param_spec_string ("service", "Service",
147 "The service or port number the server uses to listen on",
148 DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
150 * GstRTSPServer::bound-port:
152 * The actual port the server is listening on. Can be used to retrieve the
153 * port number when the server is started on port 0, which means bind to a
154 * random port. Set to -1 if the server has not been bound yet.
156 g_object_class_install_property (gobject_class, PROP_BOUND_PORT,
157 g_param_spec_int ("bound-port", "Bound port",
158 "The port number the server is listening on",
159 -1, G_MAXUINT16, DEFAULT_BOUND_PORT,
160 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
162 * GstRTSPServer::backlog:
164 * The backlog argument defines the maximum length to which the queue of
165 * pending connections for the server may grow. If a connection request arrives
166 * when the queue is full, the client may receive an error with an indication of
167 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
168 * request may be ignored so that a later reattempt at connection succeeds.
170 g_object_class_install_property (gobject_class, PROP_BACKLOG,
171 g_param_spec_int ("backlog", "Backlog",
172 "The maximum length to which the queue "
173 "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
174 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
176 * GstRTSPServer::session-pool:
178 * The session pool of the server. By default each server has a separate
179 * session pool but sessions can be shared between servers by setting the same
180 * session pool on multiple servers.
182 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
183 g_param_spec_object ("session-pool", "Session Pool",
184 "The session pool to use for client session",
185 GST_TYPE_RTSP_SESSION_POOL,
186 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 * GstRTSPServer::mount-points:
190 * The mount points to use for this server. By default the server has no
191 * mount points and thus cannot map urls to media streams.
193 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
194 g_param_spec_object ("mount-points", "Mount Points",
195 "The mount points to use for client session",
196 GST_TYPE_RTSP_MOUNT_POINTS,
197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
199 * GstRTSPServer::use-client-settings:
201 * Use client transport settings (destination, port pair and ttl for
202 * multicast. FALSE means that the server settings will be used.
204 g_object_class_install_property (gobject_class, PROP_USE_CLIENT_SETTINGS,
205 g_param_spec_boolean ("use-client-settings", "Use Client Settings",
206 "Use client settings for ttl, destination and port pair in multicast",
207 DEFAULT_USE_CLIENT_SETTINGS,
208 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210 gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED] =
211 g_signal_new ("client-connected", G_TYPE_FROM_CLASS (gobject_class),
212 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPServerClass, client_connected),
213 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
214 gst_rtsp_client_get_type ());
216 klass->create_client = default_create_client;
217 klass->setup_connection = default_setup_connection;
219 GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
223 gst_rtsp_server_init (GstRTSPServer * server)
225 GstRTSPServerPrivate *priv = GST_RTSP_SERVER_GET_PRIVATE (server);
229 g_mutex_init (&priv->lock);
230 priv->address = g_strdup (DEFAULT_ADDRESS);
231 priv->service = g_strdup (DEFAULT_SERVICE);
233 priv->backlog = DEFAULT_BACKLOG;
234 priv->session_pool = gst_rtsp_session_pool_new ();
235 priv->mount_points = gst_rtsp_mount_points_new ();
236 priv->thread_pool = gst_rtsp_thread_pool_new ();
237 priv->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
241 gst_rtsp_server_finalize (GObject * object)
243 GstRTSPServer *server = GST_RTSP_SERVER (object);
244 GstRTSPServerPrivate *priv = server->priv;
246 GST_DEBUG_OBJECT (server, "finalize server");
248 g_free (priv->address);
249 g_free (priv->service);
252 g_object_unref (priv->socket);
254 if (priv->session_pool)
255 g_object_unref (priv->session_pool);
256 if (priv->mount_points)
257 g_object_unref (priv->mount_points);
258 if (priv->thread_pool)
259 g_object_unref (priv->thread_pool);
262 g_object_unref (priv->auth);
264 if (priv->certificate)
265 g_object_unref (priv->certificate);
267 g_mutex_clear (&priv->lock);
269 G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
273 * gst_rtsp_server_new:
275 * Create a new #GstRTSPServer instance.
278 gst_rtsp_server_new (void)
280 GstRTSPServer *result;
282 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
288 * gst_rtsp_server_set_address:
289 * @server: a #GstRTSPServer
290 * @address: the address
292 * Configure @server to accept connections on the given address.
294 * This function must be called before the server is bound.
297 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
299 GstRTSPServerPrivate *priv;
301 g_return_if_fail (GST_IS_RTSP_SERVER (server));
302 g_return_if_fail (address != NULL);
306 GST_RTSP_SERVER_LOCK (server);
307 g_free (priv->address);
308 priv->address = g_strdup (address);
309 GST_RTSP_SERVER_UNLOCK (server);
313 * gst_rtsp_server_get_address:
314 * @server: a #GstRTSPServer
316 * Get the address on which the server will accept connections.
318 * Returns: the server address. g_free() after usage.
321 gst_rtsp_server_get_address (GstRTSPServer * server)
323 GstRTSPServerPrivate *priv;
326 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
330 GST_RTSP_SERVER_LOCK (server);
331 result = g_strdup (priv->address);
332 GST_RTSP_SERVER_UNLOCK (server);
338 * gst_rtsp_server_get_bound_port:
339 * @server: a #GstRTSPServer
341 * Get the port number where the server was bound to.
343 * Returns: the port number
346 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
348 GstRTSPServerPrivate *priv;
349 GSocketAddress *address;
352 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
356 GST_RTSP_SERVER_LOCK (server);
357 if (priv->socket == NULL)
360 address = g_socket_get_local_address (priv->socket, NULL);
361 result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
362 g_object_unref (address);
365 GST_RTSP_SERVER_UNLOCK (server);
371 * gst_rtsp_server_set_service:
372 * @server: a #GstRTSPServer
373 * @service: the service
375 * Configure @server to accept connections on the given service.
376 * @service should be a string containing the service name (see services(5)) or
377 * a string containing a port number between 1 and 65535.
379 * This function must be called before the server is bound.
382 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
384 GstRTSPServerPrivate *priv;
386 g_return_if_fail (GST_IS_RTSP_SERVER (server));
387 g_return_if_fail (service != NULL);
391 GST_RTSP_SERVER_LOCK (server);
392 g_free (priv->service);
393 priv->service = g_strdup (service);
394 GST_RTSP_SERVER_UNLOCK (server);
398 * gst_rtsp_server_get_service:
399 * @server: a #GstRTSPServer
401 * Get the service on which the server will accept connections.
403 * Returns: the service. use g_free() after usage.
406 gst_rtsp_server_get_service (GstRTSPServer * server)
408 GstRTSPServerPrivate *priv;
411 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
415 GST_RTSP_SERVER_LOCK (server);
416 result = g_strdup (priv->service);
417 GST_RTSP_SERVER_UNLOCK (server);
423 * gst_rtsp_server_set_backlog:
424 * @server: a #GstRTSPServer
425 * @backlog: the backlog
427 * configure the maximum amount of requests that may be queued for the
430 * This function must be called before the server is bound.
433 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
435 GstRTSPServerPrivate *priv;
437 g_return_if_fail (GST_IS_RTSP_SERVER (server));
441 GST_RTSP_SERVER_LOCK (server);
442 priv->backlog = backlog;
443 GST_RTSP_SERVER_UNLOCK (server);
447 * gst_rtsp_server_get_backlog:
448 * @server: a #GstRTSPServer
450 * The maximum amount of queued requests for the server.
452 * Returns: the server backlog.
455 gst_rtsp_server_get_backlog (GstRTSPServer * server)
457 GstRTSPServerPrivate *priv;
460 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
464 GST_RTSP_SERVER_LOCK (server);
465 result = priv->backlog;
466 GST_RTSP_SERVER_UNLOCK (server);
472 * gst_rtsp_server_set_session_pool:
473 * @server: a #GstRTSPServer
474 * @pool: a #GstRTSPSessionPool
476 * configure @pool to be used as the session pool of @server.
479 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
480 GstRTSPSessionPool * pool)
482 GstRTSPServerPrivate *priv;
483 GstRTSPSessionPool *old;
485 g_return_if_fail (GST_IS_RTSP_SERVER (server));
492 GST_RTSP_SERVER_LOCK (server);
493 old = priv->session_pool;
494 priv->session_pool = pool;
495 GST_RTSP_SERVER_UNLOCK (server);
498 g_object_unref (old);
502 * gst_rtsp_server_get_session_pool:
503 * @server: a #GstRTSPServer
505 * Get the #GstRTSPSessionPool used as the session pool of @server.
507 * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
511 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
513 GstRTSPServerPrivate *priv;
514 GstRTSPSessionPool *result;
516 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
520 GST_RTSP_SERVER_LOCK (server);
521 if ((result = priv->session_pool))
522 g_object_ref (result);
523 GST_RTSP_SERVER_UNLOCK (server);
529 * gst_rtsp_server_set_mount_points:
530 * @server: a #GstRTSPServer
531 * @mounts: a #GstRTSPMountPoints
533 * configure @mounts to be used as the mount points of @server.
536 gst_rtsp_server_set_mount_points (GstRTSPServer * server,
537 GstRTSPMountPoints * mounts)
539 GstRTSPServerPrivate *priv;
540 GstRTSPMountPoints *old;
542 g_return_if_fail (GST_IS_RTSP_SERVER (server));
547 g_object_ref (mounts);
549 GST_RTSP_SERVER_LOCK (server);
550 old = priv->mount_points;
551 priv->mount_points = mounts;
552 GST_RTSP_SERVER_UNLOCK (server);
555 g_object_unref (old);
560 * gst_rtsp_server_get_mount_points:
561 * @server: a #GstRTSPServer
563 * Get the #GstRTSPMountPoints used as the mount points of @server.
565 * Returns: (transfer full): the #GstRTSPMountPoints of @server. g_object_unref() after
569 gst_rtsp_server_get_mount_points (GstRTSPServer * server)
571 GstRTSPServerPrivate *priv;
572 GstRTSPMountPoints *result;
574 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
578 GST_RTSP_SERVER_LOCK (server);
579 if ((result = priv->mount_points))
580 g_object_ref (result);
581 GST_RTSP_SERVER_UNLOCK (server);
587 * gst_rtsp_server_set_auth:
588 * @server: a #GstRTSPServer
589 * @auth: a #GstRTSPAuth
591 * configure @auth to be used as the authentication manager of @server.
594 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
596 GstRTSPServerPrivate *priv;
599 g_return_if_fail (GST_IS_RTSP_SERVER (server));
606 GST_RTSP_SERVER_LOCK (server);
609 GST_RTSP_SERVER_UNLOCK (server);
612 g_object_unref (old);
617 * gst_rtsp_server_get_auth:
618 * @server: a #GstRTSPServer
620 * Get the #GstRTSPAuth used as the authentication manager of @server.
622 * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
626 gst_rtsp_server_get_auth (GstRTSPServer * server)
628 GstRTSPServerPrivate *priv;
631 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
635 GST_RTSP_SERVER_LOCK (server);
636 if ((result = priv->auth))
637 g_object_ref (result);
638 GST_RTSP_SERVER_UNLOCK (server);
644 * gst_rtsp_server_set_thread_pool:
645 * @server: a #GstRTSPServer
646 * @pool: a #GstRTSPThreadPool
648 * configure @pool to be used as the thread pool of @server.
651 gst_rtsp_server_set_thread_pool (GstRTSPServer * server,
652 GstRTSPThreadPool * pool)
654 GstRTSPServerPrivate *priv;
655 GstRTSPThreadPool *old;
657 g_return_if_fail (GST_IS_RTSP_SERVER (server));
664 GST_RTSP_SERVER_LOCK (server);
665 old = priv->thread_pool;
666 priv->thread_pool = pool;
667 GST_RTSP_SERVER_UNLOCK (server);
670 g_object_unref (old);
674 * gst_rtsp_server_get_thread_pool:
675 * @server: a #GstRTSPServer
677 * Get the #GstRTSPThreadPool used as the thread pool of @server.
679 * Returns: (transfer full): the #GstRTSPThreadPool of @server. g_object_unref() after
683 gst_rtsp_server_get_thread_pool (GstRTSPServer * server)
685 GstRTSPServerPrivate *priv;
686 GstRTSPThreadPool *result;
688 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
692 GST_RTSP_SERVER_LOCK (server);
693 if ((result = priv->thread_pool))
694 g_object_ref (result);
695 GST_RTSP_SERVER_UNLOCK (server);
701 * gst_rtsp_server_set_use_client_settings:
702 * @server: a #GstRTSPServer
703 * @use_client_settings: whether to use client settings for multicast
705 * Use client transport settings (destination, port pair and ttl) for
707 * When @use_client_settings is %FALSE, the server settings will be
711 gst_rtsp_server_set_use_client_settings (GstRTSPServer * server,
712 gboolean use_client_settings)
714 GstRTSPServerPrivate *priv;
716 g_return_if_fail (GST_IS_RTSP_SERVER (server));
720 GST_RTSP_SERVER_LOCK (server);
721 priv->use_client_settings = use_client_settings;
722 GST_RTSP_SERVER_UNLOCK (server);
726 * gst_rtsp_server_get_use_client_settings:
727 * @server: a #GstRTSPServer
729 * Check if client transport settings (destination, port pair and ttl) for
730 * multicast will be used.
733 gst_rtsp_server_get_use_client_settings (GstRTSPServer * server)
735 GstRTSPServerPrivate *priv;
738 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), FALSE);
742 GST_RTSP_SERVER_LOCK (server);
743 res = priv->use_client_settings;
744 GST_RTSP_SERVER_UNLOCK (server);
750 * gst_rtsp_server_set_tls_certificate:
751 * @server: a #GstRTSPServer
752 * @cert: (allow none): a #GTlsCertificate
754 * Set the TLS certificate for the server. Client connections will only
755 * be accepted when TLS is negotiated.
758 gst_rtsp_server_set_tls_certificate (GstRTSPServer * server,
759 GTlsCertificate * cert)
761 GstRTSPServerPrivate *priv;
762 GTlsCertificate *old;
764 g_return_if_fail (GST_IS_RTSP_SERVER (server));
771 GST_RTSP_SERVER_LOCK (server);
772 old = priv->certificate;
773 priv->certificate = cert;
774 GST_RTSP_SERVER_UNLOCK (server);
777 g_object_unref (old);
781 * gst_rtsp_server_get_tls_certificate:
782 * @server: a #GstRTSPServer
784 * Get the #GTlsCertificate used for negotiating TLS @server.
786 * Returns: (transfer full): the #GTlsCertificate of @server. g_object_unref() after
790 gst_rtsp_server_get_tls_certificate (GstRTSPServer * server)
792 GstRTSPServerPrivate *priv;
793 GTlsCertificate *result;
795 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
799 GST_RTSP_SERVER_LOCK (server);
800 if ((result = priv->certificate))
801 g_object_ref (result);
802 GST_RTSP_SERVER_UNLOCK (server);
808 gst_rtsp_server_get_property (GObject * object, guint propid,
809 GValue * value, GParamSpec * pspec)
811 GstRTSPServer *server = GST_RTSP_SERVER (object);
815 g_value_take_string (value, gst_rtsp_server_get_address (server));
818 g_value_take_string (value, gst_rtsp_server_get_service (server));
820 case PROP_BOUND_PORT:
821 g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
824 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
826 case PROP_SESSION_POOL:
827 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
829 case PROP_MOUNT_POINTS:
830 g_value_take_object (value, gst_rtsp_server_get_mount_points (server));
832 case PROP_USE_CLIENT_SETTINGS:
833 g_value_set_boolean (value,
834 gst_rtsp_server_get_use_client_settings (server));
837 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
842 gst_rtsp_server_set_property (GObject * object, guint propid,
843 const GValue * value, GParamSpec * pspec)
845 GstRTSPServer *server = GST_RTSP_SERVER (object);
849 gst_rtsp_server_set_address (server, g_value_get_string (value));
852 gst_rtsp_server_set_service (server, g_value_get_string (value));
855 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
857 case PROP_SESSION_POOL:
858 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
860 case PROP_MOUNT_POINTS:
861 gst_rtsp_server_set_mount_points (server, g_value_get_object (value));
863 case PROP_USE_CLIENT_SETTINGS:
864 gst_rtsp_server_set_use_client_settings (server,
865 g_value_get_boolean (value));
868 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
873 * gst_rtsp_server_create_socket:
874 * @server: a #GstRTSPServer
875 * @cancellable: a #GCancellable
878 * Create a #GSocket for @server. The socket will listen on the
879 * configured service.
881 * Returns: (transfer full): the #GSocket for @server or NULL when an error occured.
884 gst_rtsp_server_create_socket (GstRTSPServer * server,
885 GCancellable * cancellable, GError ** error)
887 GstRTSPServerPrivate *priv;
888 GSocketConnectable *conn;
889 GSocketAddressEnumerator *enumerator;
890 GSocket *socket = NULL;
892 struct linger linger;
894 GError *sock_error = NULL;
895 GError *bind_error = NULL;
898 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
902 GST_RTSP_SERVER_LOCK (server);
903 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", priv->address,
906 /* resolve the server IP address */
907 port = atoi (priv->service);
908 if (port != 0 || !strcmp (priv->service, "0"))
909 conn = g_network_address_new (priv->address, port);
911 conn = g_network_service_new (priv->service, "tcp", priv->address);
913 enumerator = g_socket_connectable_enumerate (conn);
914 g_object_unref (conn);
916 /* create server socket, we loop through all the addresses until we manage to
917 * create a socket and bind. */
919 GSocketAddress *sockaddr;
922 g_socket_address_enumerator_next (enumerator, cancellable, error);
925 GST_DEBUG_OBJECT (server, "no more addresses %s",
926 *error ? (*error)->message : "");
928 GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
933 /* only keep the first error */
934 socket = g_socket_new (g_socket_address_get_family (sockaddr),
935 G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
936 sock_error ? NULL : &sock_error);
938 if (socket == NULL) {
939 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
940 sock_error->message);
941 g_object_unref (sockaddr);
945 if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
946 g_object_unref (sockaddr);
950 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
951 bind_error->message);
952 g_object_unref (sockaddr);
953 g_object_unref (socket);
956 g_object_unref (enumerator);
961 g_clear_error (&sock_error);
962 g_clear_error (&bind_error);
964 GST_DEBUG_OBJECT (server, "opened sending server socket");
966 /* keep connection alive; avoids SIGPIPE during write */
967 g_socket_set_keepalive (socket, TRUE);
971 /* make sure socket is reset 5 seconds after close. This ensure that we can
972 * reuse the socket quickly while still having a chance to send data to the
976 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
977 (void *) &linger, sizeof (linger)) < 0)
982 /* set the server socket to nonblocking */
983 g_socket_set_blocking (socket, FALSE);
985 /* set listen backlog */
986 g_socket_set_listen_backlog (socket, priv->backlog);
988 if (!g_socket_listen (socket, error))
991 GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
992 socket, priv->backlog);
994 GST_RTSP_SERVER_UNLOCK (server);
1001 GST_ERROR_OBJECT (server, "failed to create socket");
1008 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
1009 g_strerror (errno));
1016 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
1023 g_object_unref (socket);
1027 g_propagate_error (error, sock_error);
1029 g_error_free (sock_error);
1032 if ((error == NULL) || (*error == NULL))
1033 g_propagate_error (error, bind_error);
1035 g_error_free (bind_error);
1037 GST_RTSP_SERVER_UNLOCK (server);
1042 struct _ClientContext
1044 GstRTSPServer *server;
1045 GstRTSPThread *thread;
1046 GstRTSPClient *client;
1050 free_client_context (ClientContext * ctx)
1052 GST_RTSP_SERVER_LOCK (ctx->server);
1054 gst_rtsp_thread_stop (ctx->thread);
1055 GST_RTSP_SERVER_UNLOCK (ctx->server);
1057 g_object_unref (ctx->client);
1058 g_slice_free (ClientContext, ctx);
1060 return G_SOURCE_REMOVE;
1064 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
1066 GstRTSPServer *server = ctx->server;
1067 GstRTSPServerPrivate *priv = server->priv;
1069 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
1071 g_object_ref (server);
1073 GST_RTSP_SERVER_LOCK (server);
1074 priv->clients = g_list_remove (priv->clients, ctx);
1075 GST_RTSP_SERVER_UNLOCK (server);
1080 src = g_idle_source_new ();
1081 g_source_set_callback (src, (GSourceFunc) free_client_context, ctx, NULL);
1082 g_source_attach (src, ctx->thread->context);
1083 g_source_unref (src);
1085 free_client_context (ctx);
1088 g_object_unref (server);
1091 /* add the client context to the active list of clients, takes ownership
1094 manage_client (GstRTSPServer * server, GstRTSPClient * client)
1097 GstRTSPServerPrivate *priv = server->priv;
1098 GMainContext *mainctx = NULL;
1099 GstRTSPClientState state = { NULL };
1101 GST_DEBUG_OBJECT (server, "manage client %p", client);
1103 ctx = g_slice_new0 (ClientContext);
1104 ctx->server = server;
1105 ctx->client = client;
1107 GST_RTSP_SERVER_LOCK (server);
1109 state.server = server;
1110 state.client = client;
1112 ctx->thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
1113 GST_RTSP_THREAD_TYPE_CLIENT, &state);
1115 mainctx = ctx->thread->context;
1118 /* find the context to add the watch */
1119 if ((source = g_main_current_source ()))
1120 mainctx = g_source_get_context (source);
1123 g_signal_connect (client, "closed", (GCallback) unmanage_client, ctx);
1124 priv->clients = g_list_prepend (priv->clients, ctx);
1126 gst_rtsp_client_attach (client, mainctx);
1128 GST_RTSP_SERVER_UNLOCK (server);
1131 static GstRTSPClient *
1132 default_create_client (GstRTSPServer * server)
1134 GstRTSPClient *client;
1135 GstRTSPServerPrivate *priv = server->priv;
1137 /* a new client connected, create a session to handle the client. */
1138 client = gst_rtsp_client_new ();
1140 /* set the session pool that this client should use */
1141 GST_RTSP_SERVER_LOCK (server);
1142 gst_rtsp_client_set_session_pool (client, priv->session_pool);
1143 /* set the mount points that this client should use */
1144 gst_rtsp_client_set_mount_points (client, priv->mount_points);
1145 /* set authentication manager */
1146 gst_rtsp_client_set_auth (client, priv->auth);
1147 /* set threadpool */
1148 gst_rtsp_client_set_thread_pool (client, priv->thread_pool);
1149 /* check if client transport settings for multicast are allowed */
1150 gst_rtsp_client_set_use_client_settings (client, priv->use_client_settings);
1151 GST_RTSP_SERVER_UNLOCK (server);
1157 default_setup_connection (GstRTSPServer * server, GstRTSPClient * client,
1158 GstRTSPConnection * conn)
1160 GstRTSPServerPrivate *priv = server->priv;
1162 GST_RTSP_SERVER_LOCK (server);
1163 if (priv->certificate) {
1164 GTlsConnection *tls;
1166 /* configure the connection */
1167 tls = gst_rtsp_connection_get_tls (conn, NULL);
1168 g_tls_connection_set_certificate (tls, priv->certificate);
1170 GST_RTSP_SERVER_UNLOCK (server);
1176 * gst_rtsp_server_transfer_connection:
1177 * @server: a #GstRTSPServer
1178 * @socket: a network socket
1179 * @ip: the IP address of the remote client
1180 * @port: the port used by the other end
1181 * @initial_buffer: any initial data that was already read from the socket
1183 * Take an existing network socket and use it for an RTSP connection. This
1184 * is used when transferring a socket from an HTTP server which should be used
1185 * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
1186 * that the HTTP server read from the socket while parsing the HTTP header.
1188 * Returns: TRUE if all was ok, FALSE if an error occured.
1191 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
1192 const gchar * ip, gint port, const gchar * initial_buffer)
1194 GstRTSPClient *client = NULL;
1195 GstRTSPServerClass *klass;
1196 GstRTSPConnection *conn;
1199 klass = GST_RTSP_SERVER_GET_CLASS (server);
1201 if (klass->create_client)
1202 client = klass->create_client (server);
1206 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
1207 initial_buffer, &conn), no_connection);
1209 /* set connection on the client now */
1210 gst_rtsp_client_set_connection (client, conn);
1212 /* manage the client connection */
1213 manage_client (server, client);
1215 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1223 GST_ERROR_OBJECT (server, "failed to create a client");
1228 gchar *str = gst_rtsp_strresult (res);
1229 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
1236 * gst_rtsp_server_io_func:
1237 * @socket: a #GSocket
1238 * @condition: the condition on @source
1239 * @server: a #GstRTSPServer
1241 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
1242 * new connection on @socket or @server.
1244 * Returns: TRUE if the source could be connected, FALSE if an error occured.
1247 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
1248 GstRTSPServer * server)
1250 GstRTSPClient *client = NULL;
1251 GstRTSPServerClass *klass;
1253 GstRTSPConnection *conn = NULL;
1255 if (condition & G_IO_IN) {
1257 klass = GST_RTSP_SERVER_GET_CLASS (server);
1259 /* a new client connected, create a client object to handle the client. */
1260 if (klass->create_client)
1261 client = klass->create_client (server);
1265 /* a new client connected. */
1266 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, NULL),
1269 if (klass->setup_connection)
1270 if (!klass->setup_connection (server, client, conn))
1273 /* set connection on the client now */
1274 gst_rtsp_client_set_connection (client, conn);
1276 /* manage the client connection */
1277 manage_client (server, client);
1279 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1282 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
1284 return G_SOURCE_CONTINUE;
1289 GST_ERROR_OBJECT (server, "failed to create a client");
1290 return G_SOURCE_CONTINUE;
1294 gchar *str = gst_rtsp_strresult (res);
1295 GST_ERROR_OBJECT (server, "Could not accept client on socket %p: %s",
1298 g_object_unref (client);
1299 return G_SOURCE_CONTINUE;
1303 GST_ERROR_OBJECT (server, "failed to setup client connection");
1304 gst_rtsp_connection_free (conn);
1305 g_object_unref (client);
1306 return G_SOURCE_CONTINUE;
1311 watch_destroyed (GstRTSPServer * server)
1313 GstRTSPServerPrivate *priv = server->priv;
1315 GST_DEBUG_OBJECT (server, "source destroyed");
1317 g_object_unref (priv->socket);
1318 priv->socket = NULL;
1319 g_object_unref (server);
1323 * gst_rtsp_server_create_source:
1324 * @server: a #GstRTSPServer
1325 * @cancellable: a #GCancellable or %NULL.
1328 * Create a #GSource for @server. The new source will have a default
1329 * #GSocketSourceFunc of gst_rtsp_server_io_func().
1331 * @cancellable if not NULL can be used to cancel the source, which will cause
1332 * the source to trigger, reporting the current condition (which is likely 0
1333 * unless cancellation happened at the same time as a condition change). You can
1334 * check for this in the callback using g_cancellable_is_cancelled().
1336 * Returns: the #GSource for @server or NULL when an error occured. Free with
1340 gst_rtsp_server_create_source (GstRTSPServer * server,
1341 GCancellable * cancellable, GError ** error)
1343 GstRTSPServerPrivate *priv;
1344 GSocket *socket, *old;
1347 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1349 priv = server->priv;
1351 socket = gst_rtsp_server_create_socket (server, NULL, error);
1355 GST_RTSP_SERVER_LOCK (server);
1357 priv->socket = g_object_ref (socket);
1358 GST_RTSP_SERVER_UNLOCK (server);
1361 g_object_unref (old);
1363 /* create a watch for reads (new connections) and possible errors */
1364 source = g_socket_create_source (socket, G_IO_IN |
1365 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
1366 g_object_unref (socket);
1368 /* configure the callback */
1369 g_source_set_callback (source,
1370 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
1371 (GDestroyNotify) watch_destroyed);
1377 GST_ERROR_OBJECT (server, "failed to create socket");
1383 * gst_rtsp_server_attach:
1384 * @server: a #GstRTSPServer
1385 * @context: (allow-none): a #GMainContext
1387 * Attaches @server to @context. When the mainloop for @context is run, the
1388 * server will be dispatched. When @context is NULL, the default context will be
1391 * This function should be called when the server properties and urls are fully
1392 * configured and the server is ready to start.
1394 * Returns: the ID (greater than 0) for the source within the GMainContext.
1397 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1401 GError *error = NULL;
1403 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1405 source = gst_rtsp_server_create_source (server, NULL, &error);
1409 res = g_source_attach (source, context);
1410 g_source_unref (source);
1417 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1418 g_error_free (error);