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 DEFAULT_ADDRESS "0.0.0.0"
27 #define DEFAULT_BOUND_PORT -1
28 /* #define DEFAULT_ADDRESS "::0" */
29 #define DEFAULT_SERVICE "8554"
30 #define DEFAULT_BACKLOG 5
31 #define DEFAULT_MAX_THREADS 0
33 /* Define to use the SO_LINGER option so that the server sockets can be resused
34 * sooner. Disabled for now because it is not very well implemented by various
35 * OSes and it causes clients to fail to read the TEARDOWN response. */
54 SIGNAL_CLIENT_CONNECTED,
58 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
60 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
61 #define GST_CAT_DEFAULT rtsp_server_debug
63 typedef struct _ClientContext ClientContext;
65 static guint gst_rtsp_server_signals[SIGNAL_LAST] = { 0 };
67 static void gst_rtsp_server_get_property (GObject * object, guint propid,
68 GValue * value, GParamSpec * pspec);
69 static void gst_rtsp_server_set_property (GObject * object, guint propid,
70 const GValue * value, GParamSpec * pspec);
71 static void gst_rtsp_server_finalize (GObject * object);
73 static gpointer do_loop (ClientContext * ctx);
74 static GstRTSPClient *default_create_client (GstRTSPServer * server);
75 static gboolean default_accept_client (GstRTSPServer * server,
76 GstRTSPClient * client, GSocket * socket, GError ** error);
79 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
81 GObjectClass *gobject_class;
83 gobject_class = G_OBJECT_CLASS (klass);
85 gobject_class->get_property = gst_rtsp_server_get_property;
86 gobject_class->set_property = gst_rtsp_server_set_property;
87 gobject_class->finalize = gst_rtsp_server_finalize;
90 * GstRTSPServer::address:
92 * The address of the server. This is the address where the server will
95 g_object_class_install_property (gobject_class, PROP_ADDRESS,
96 g_param_spec_string ("address", "Address",
97 "The address the server uses to listen on", DEFAULT_ADDRESS,
98 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
100 * GstRTSPServer::service:
102 * The service of the server. This is either a string with the service name or
103 * a port number (as a string) the server will listen on.
105 g_object_class_install_property (gobject_class, PROP_SERVICE,
106 g_param_spec_string ("service", "Service",
107 "The service or port number the server uses to listen on",
108 DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
110 * GstRTSPServer::bound-port:
112 * The actual port the server is listening on. Can be used to retrieve the
113 * port number when the server is started on port 0, which means bind to a
114 * random port. Set to -1 if the server has not been bound yet.
116 g_object_class_install_property (gobject_class, PROP_BOUND_PORT,
117 g_param_spec_int ("bound-port", "Bound port",
118 "The port number the server is listening on",
119 -1, G_MAXUINT16, DEFAULT_BOUND_PORT,
120 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
122 * GstRTSPServer::backlog:
124 * The backlog argument defines the maximum length to which the queue of
125 * pending connections for the server may grow. If a connection request arrives
126 * when the queue is full, the client may receive an error with an indication of
127 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
128 * request may be ignored so that a later reattempt at connection succeeds.
130 g_object_class_install_property (gobject_class, PROP_BACKLOG,
131 g_param_spec_int ("backlog", "Backlog",
132 "The maximum length to which the queue "
133 "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
134 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
136 * GstRTSPServer::session-pool:
138 * The session pool of the server. By default each server has a separate
139 * session pool but sessions can be shared between servers by setting the same
140 * session pool on multiple servers.
142 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
143 g_param_spec_object ("session-pool", "Session Pool",
144 "The session pool to use for client session",
145 GST_TYPE_RTSP_SESSION_POOL,
146 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
148 * GstRTSPServer::mount-points:
150 * The mount points to use for this server. By default the server has no
151 * mount points and thus cannot map urls to media streams.
153 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
154 g_param_spec_object ("mount-points", "Mount Points",
155 "The mount points to use for client session",
156 GST_TYPE_RTSP_MOUNT_POINTS,
157 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159 * GstRTSPServer::max-threads:
161 * The maximum amount of threads to use for client connections. A value of
162 * 0 means to use only the mainloop, -1 means an unlimited amount of
165 g_object_class_install_property (gobject_class, PROP_MAX_THREADS,
166 g_param_spec_int ("max-threads", "Max Threads",
167 "The maximum amount of threads to use for client connections "
168 "(0 = only mainloop, -1 = unlimited)", -1, G_MAXINT,
169 DEFAULT_MAX_THREADS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171 gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED] =
172 g_signal_new ("client-connected", G_TYPE_FROM_CLASS (gobject_class),
173 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPServerClass, client_connected),
174 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
175 gst_rtsp_client_get_type ());
177 klass->create_client = default_create_client;
178 klass->accept_client = default_accept_client;
180 klass->pool = g_thread_pool_new ((GFunc) do_loop, klass, -1, FALSE, NULL);
182 GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
186 gst_rtsp_server_init (GstRTSPServer * server)
188 g_mutex_init (&server->lock);
189 server->address = g_strdup (DEFAULT_ADDRESS);
190 server->service = g_strdup (DEFAULT_SERVICE);
191 server->socket = NULL;
192 server->backlog = DEFAULT_BACKLOG;
193 server->session_pool = gst_rtsp_session_pool_new ();
194 server->mount_points = gst_rtsp_mount_points_new ();
195 server->max_threads = DEFAULT_MAX_THREADS;
199 gst_rtsp_server_finalize (GObject * object)
201 GstRTSPServer *server = GST_RTSP_SERVER (object);
203 GST_DEBUG_OBJECT (server, "finalize server");
205 g_free (server->address);
206 g_free (server->service);
209 g_object_unref (server->socket);
211 g_object_unref (server->session_pool);
212 g_object_unref (server->mount_points);
215 g_object_unref (server->auth);
217 g_mutex_clear (&server->lock);
219 G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
223 * gst_rtsp_server_new:
225 * Create a new #GstRTSPServer instance.
228 gst_rtsp_server_new (void)
230 GstRTSPServer *result;
232 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
238 * gst_rtsp_server_set_address:
239 * @server: a #GstRTSPServer
240 * @address: the address
242 * Configure @server to accept connections on the given address.
244 * This function must be called before the server is bound.
247 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
249 g_return_if_fail (GST_IS_RTSP_SERVER (server));
250 g_return_if_fail (address != NULL);
252 GST_RTSP_SERVER_LOCK (server);
253 g_free (server->address);
254 server->address = g_strdup (address);
255 GST_RTSP_SERVER_UNLOCK (server);
259 * gst_rtsp_server_get_address:
260 * @server: a #GstRTSPServer
262 * Get the address on which the server will accept connections.
264 * Returns: the server address. g_free() after usage.
267 gst_rtsp_server_get_address (GstRTSPServer * server)
270 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
272 GST_RTSP_SERVER_LOCK (server);
273 result = g_strdup (server->address);
274 GST_RTSP_SERVER_UNLOCK (server);
280 * gst_rtsp_server_get_bound_port:
281 * @server: a #GstRTSPServer
283 * Get the port number where the server was bound to.
285 * Returns: the port number
288 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
290 GSocketAddress *address;
293 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
295 GST_RTSP_SERVER_LOCK (server);
296 if (server->socket == NULL)
299 address = g_socket_get_local_address (server->socket, NULL);
300 result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
301 g_object_unref (address);
304 GST_RTSP_SERVER_UNLOCK (server);
310 * gst_rtsp_server_set_service:
311 * @server: a #GstRTSPServer
312 * @service: the service
314 * Configure @server to accept connections on the given service.
315 * @service should be a string containing the service name (see services(5)) or
316 * a string containing a port number between 1 and 65535.
318 * This function must be called before the server is bound.
321 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
323 g_return_if_fail (GST_IS_RTSP_SERVER (server));
324 g_return_if_fail (service != NULL);
326 GST_RTSP_SERVER_LOCK (server);
327 g_free (server->service);
328 server->service = g_strdup (service);
329 GST_RTSP_SERVER_UNLOCK (server);
333 * gst_rtsp_server_get_service:
334 * @server: a #GstRTSPServer
336 * Get the service on which the server will accept connections.
338 * Returns: the service. use g_free() after usage.
341 gst_rtsp_server_get_service (GstRTSPServer * server)
345 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
347 GST_RTSP_SERVER_LOCK (server);
348 result = g_strdup (server->service);
349 GST_RTSP_SERVER_UNLOCK (server);
355 * gst_rtsp_server_set_backlog:
356 * @server: a #GstRTSPServer
357 * @backlog: the backlog
359 * configure the maximum amount of requests that may be queued for the
362 * This function must be called before the server is bound.
365 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
367 g_return_if_fail (GST_IS_RTSP_SERVER (server));
369 GST_RTSP_SERVER_LOCK (server);
370 server->backlog = backlog;
371 GST_RTSP_SERVER_UNLOCK (server);
375 * gst_rtsp_server_get_backlog:
376 * @server: a #GstRTSPServer
378 * The maximum amount of queued requests for the server.
380 * Returns: the server backlog.
383 gst_rtsp_server_get_backlog (GstRTSPServer * server)
387 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
389 GST_RTSP_SERVER_LOCK (server);
390 result = server->backlog;
391 GST_RTSP_SERVER_UNLOCK (server);
397 * gst_rtsp_server_set_session_pool:
398 * @server: a #GstRTSPServer
399 * @pool: a #GstRTSPSessionPool
401 * configure @pool to be used as the session pool of @server.
404 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
405 GstRTSPSessionPool * pool)
407 GstRTSPSessionPool *old;
409 g_return_if_fail (GST_IS_RTSP_SERVER (server));
414 GST_RTSP_SERVER_LOCK (server);
415 old = server->session_pool;
416 server->session_pool = pool;
417 GST_RTSP_SERVER_UNLOCK (server);
420 g_object_unref (old);
424 * gst_rtsp_server_get_session_pool:
425 * @server: a #GstRTSPServer
427 * Get the #GstRTSPSessionPool used as the session pool of @server.
429 * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
433 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
435 GstRTSPSessionPool *result;
437 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
439 GST_RTSP_SERVER_LOCK (server);
440 if ((result = server->session_pool))
441 g_object_ref (result);
442 GST_RTSP_SERVER_UNLOCK (server);
448 * gst_rtsp_server_set_mount_points:
449 * @server: a #GstRTSPServer
450 * @mounts: a #GstRTSPMountPoints
452 * configure @mounts to be used as the mount points of @server.
455 gst_rtsp_server_set_mount_points (GstRTSPServer * server,
456 GstRTSPMountPoints * mounts)
458 GstRTSPMountPoints *old;
460 g_return_if_fail (GST_IS_RTSP_SERVER (server));
463 g_object_ref (mounts);
465 GST_RTSP_SERVER_LOCK (server);
466 old = server->mount_points;
467 server->mount_points = mounts;
468 GST_RTSP_SERVER_UNLOCK (server);
471 g_object_unref (old);
476 * gst_rtsp_server_get_mount_points:
477 * @server: a #GstRTSPServer
479 * Get the #GstRTSPMountPoints used as the mount points of @server.
481 * Returns: (transfer full): the #GstRTSPMountPoints of @server. g_object_unref() after
485 gst_rtsp_server_get_mount_points (GstRTSPServer * server)
487 GstRTSPMountPoints *result;
489 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
491 GST_RTSP_SERVER_LOCK (server);
492 if ((result = server->mount_points))
493 g_object_ref (result);
494 GST_RTSP_SERVER_UNLOCK (server);
500 * gst_rtsp_server_set_auth:
501 * @server: a #GstRTSPServer
502 * @auth: a #GstRTSPAuth
504 * configure @auth to be used as the authentication manager of @server.
507 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
511 g_return_if_fail (GST_IS_RTSP_SERVER (server));
516 GST_RTSP_SERVER_LOCK (server);
519 GST_RTSP_SERVER_UNLOCK (server);
522 g_object_unref (old);
527 * gst_rtsp_server_get_auth:
528 * @server: a #GstRTSPServer
530 * Get the #GstRTSPAuth used as the authentication manager of @server.
532 * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
536 gst_rtsp_server_get_auth (GstRTSPServer * server)
540 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
542 GST_RTSP_SERVER_LOCK (server);
543 if ((result = server->auth))
544 g_object_ref (result);
545 GST_RTSP_SERVER_UNLOCK (server);
551 * gst_rtsp_server_set_max_threads:
552 * @server: a #GstRTSPServer
553 * @max_threads: maximum threads
555 * Set the maximum threads used by the server to handle client requests.
556 * A value of 0 will use the server mainloop, a value of -1 will use an
557 * unlimited number of threads.
560 gst_rtsp_server_set_max_threads (GstRTSPServer * server, gint max_threads)
562 g_return_if_fail (GST_IS_RTSP_SERVER (server));
564 GST_RTSP_SERVER_LOCK (server);
565 server->max_threads = max_threads;
566 GST_RTSP_SERVER_UNLOCK (server);
570 * gst_rtsp_server_get_max_threads:
571 * @server: a #GstRTSPServer
573 * Get the maximum number of threads used for client connections.
574 * See gst_rtsp_server_set_max_threads().
576 * Returns: the maximum number of threads.
579 gst_rtsp_server_get_max_threads (GstRTSPServer * server)
583 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
585 GST_RTSP_SERVER_LOCK (server);
586 res = server->max_threads;
587 GST_RTSP_SERVER_UNLOCK (server);
594 gst_rtsp_server_get_property (GObject * object, guint propid,
595 GValue * value, GParamSpec * pspec)
597 GstRTSPServer *server = GST_RTSP_SERVER (object);
601 g_value_take_string (value, gst_rtsp_server_get_address (server));
604 g_value_take_string (value, gst_rtsp_server_get_service (server));
606 case PROP_BOUND_PORT:
607 g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
610 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
612 case PROP_SESSION_POOL:
613 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
615 case PROP_MOUNT_POINTS:
616 g_value_take_object (value, gst_rtsp_server_get_mount_points (server));
618 case PROP_MAX_THREADS:
619 g_value_set_int (value, gst_rtsp_server_get_max_threads (server));
622 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
627 gst_rtsp_server_set_property (GObject * object, guint propid,
628 const GValue * value, GParamSpec * pspec)
630 GstRTSPServer *server = GST_RTSP_SERVER (object);
634 gst_rtsp_server_set_address (server, g_value_get_string (value));
637 gst_rtsp_server_set_service (server, g_value_get_string (value));
640 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
642 case PROP_SESSION_POOL:
643 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
645 case PROP_MOUNT_POINTS:
646 gst_rtsp_server_set_mount_points (server, g_value_get_object (value));
648 case PROP_MAX_THREADS:
649 gst_rtsp_server_set_max_threads (server, g_value_get_int (value));
652 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
657 * gst_rtsp_server_create_socket:
658 * @server: a #GstRTSPServer
659 * @cancellable: a #GCancellable
662 * Create a #GSocket for @server. The socket will listen on the
663 * configured service.
665 * Returns: (transfer full): the #GSocket for @server or NULL when an error occured.
668 gst_rtsp_server_create_socket (GstRTSPServer * server,
669 GCancellable * cancellable, GError ** error)
671 GSocketConnectable *conn;
672 GSocketAddressEnumerator *enumerator;
673 GSocket *socket = NULL;
675 struct linger linger;
677 GError *sock_error = NULL;
678 GError *bind_error = NULL;
681 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
683 GST_RTSP_SERVER_LOCK (server);
684 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
687 /* resolve the server IP address */
688 port = atoi (server->service);
689 if (port != 0 || !strcmp (server->service, "0"))
690 conn = g_network_address_new (server->address, port);
692 conn = g_network_service_new (server->service, "tcp", server->address);
694 enumerator = g_socket_connectable_enumerate (conn);
695 g_object_unref (conn);
697 /* create server socket, we loop through all the addresses until we manage to
698 * create a socket and bind. */
700 GSocketAddress *sockaddr;
703 g_socket_address_enumerator_next (enumerator, cancellable, error);
706 GST_DEBUG_OBJECT (server, "no more addresses %s",
707 *error ? (*error)->message : "");
709 GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
714 /* only keep the first error */
715 socket = g_socket_new (g_socket_address_get_family (sockaddr),
716 G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
717 sock_error ? NULL : &sock_error);
719 if (socket == NULL) {
720 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
721 sock_error->message);
722 g_object_unref (sockaddr);
726 if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
727 g_object_unref (sockaddr);
731 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
732 bind_error->message);
733 g_object_unref (sockaddr);
734 g_object_unref (socket);
737 g_object_unref (enumerator);
742 g_clear_error (&sock_error);
743 g_clear_error (&bind_error);
745 GST_DEBUG_OBJECT (server, "opened sending server socket");
747 /* keep connection alive; avoids SIGPIPE during write */
748 g_socket_set_keepalive (socket, TRUE);
752 /* make sure socket is reset 5 seconds after close. This ensure that we can
753 * reuse the socket quickly while still having a chance to send data to the
757 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
758 (void *) &linger, sizeof (linger)) < 0)
763 /* set the server socket to nonblocking */
764 g_socket_set_blocking (socket, FALSE);
766 /* set listen backlog */
767 g_socket_set_listen_backlog (socket, server->backlog);
769 if (!g_socket_listen (socket, error))
772 GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
773 socket, server->backlog);
775 GST_RTSP_SERVER_UNLOCK (server);
782 GST_ERROR_OBJECT (server, "failed to create socket");
789 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
797 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
804 g_object_unref (socket);
808 g_propagate_error (error, sock_error);
810 g_error_free (sock_error);
813 if ((error == NULL) || (*error == NULL))
814 g_propagate_error (error, bind_error);
816 g_error_free (bind_error);
818 GST_RTSP_SERVER_UNLOCK (server);
823 struct _ClientContext
825 GstRTSPServer *server;
827 GMainContext *context;
828 GstRTSPClient *client;
832 free_client_context (ClientContext * ctx)
834 g_main_context_unref (ctx->context);
836 g_main_loop_unref (ctx->loop);
837 g_object_unref (ctx->client);
838 g_slice_free (ClientContext, ctx);
842 do_loop (ClientContext * ctx)
844 GST_INFO ("enter mainloop");
845 g_main_loop_run (ctx->loop);
846 GST_INFO ("exit mainloop");
848 free_client_context (ctx);
854 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
856 GstRTSPServer *server = ctx->server;
858 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
860 g_object_ref (server);
862 GST_RTSP_SERVER_LOCK (server);
863 server->clients = g_list_remove (server->clients, ctx);
864 GST_RTSP_SERVER_UNLOCK (server);
867 g_main_loop_quit (ctx->loop);
869 free_client_context (ctx);
871 g_object_unref (server);
874 /* add the client context to the active list of clients, takes ownership
877 manage_client (GstRTSPServer * server, GstRTSPClient * client)
881 GST_DEBUG_OBJECT (server, "manage client %p", client);
883 ctx = g_slice_new0 (ClientContext);
884 ctx->server = server;
885 ctx->client = client;
886 if (server->max_threads == 0) {
889 /* find the context to add the watch */
890 if ((source = g_main_current_source ()))
891 ctx->context = g_main_context_ref (g_source_get_context (source));
895 ctx->context = g_main_context_new ();
896 ctx->loop = g_main_loop_new (ctx->context, TRUE);
898 gst_rtsp_client_attach (client, ctx->context);
900 GST_RTSP_SERVER_LOCK (server);
901 g_signal_connect (client, "closed", (GCallback) unmanage_client, ctx);
902 server->clients = g_list_prepend (server->clients, ctx);
903 GST_RTSP_SERVER_UNLOCK (server);
906 GstRTSPServerClass *klass = GST_RTSP_SERVER_GET_CLASS (server);
908 g_thread_pool_push (klass->pool, ctx, NULL);
912 static GstRTSPClient *
913 default_create_client (GstRTSPServer * server)
915 GstRTSPClient *client;
917 /* a new client connected, create a session to handle the client. */
918 client = gst_rtsp_client_new ();
920 /* set the session pool that this client should use */
921 GST_RTSP_SERVER_LOCK (server);
922 gst_rtsp_client_set_session_pool (client, server->session_pool);
923 /* set the mount points that this client should use */
924 gst_rtsp_client_set_mount_points (client, server->mount_points);
925 /* set authentication manager */
926 gst_rtsp_client_set_auth (client, server->auth);
927 GST_RTSP_SERVER_UNLOCK (server);
932 /* default method for creating a new client object in the server to accept and
933 * handle a client connection on this server */
935 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
936 GSocket * socket, GError ** error)
938 /* accept connections for that client, this function returns after accepting
939 * the connection and will run the remainder of the communication with the
940 * client asyncronously. */
941 if (!gst_rtsp_client_accept (client, socket, NULL, error))
949 GST_ERROR_OBJECT (server,
950 "Could not accept client on server : %s", (*error)->message);
956 * gst_rtsp_server_transfer_connection:
957 * @server: a #GstRTSPServer
958 * @socket: a network socket
959 * @ip: the IP address of the remote client
960 * @port: the port used by the other end
961 * @initial_buffer: any initial data that was already read from the socket
963 * Take an existing network socket and use it for an RTSP connection. This
964 * is used when transferring a socket from an HTTP server which should be used
965 * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
966 * that the HTTP server read from the socket while parsing the HTTP header.
968 * Returns: TRUE if all was ok, FALSE if an error occured.
971 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
972 const gchar * ip, gint port, const gchar * initial_buffer)
974 GstRTSPClient *client = NULL;
975 GstRTSPServerClass *klass;
976 GError *error = NULL;
978 klass = GST_RTSP_SERVER_GET_CLASS (server);
980 if (klass->create_client)
981 client = klass->create_client (server);
985 /* a new client connected, create a client object to handle the client. */
986 if (!gst_rtsp_client_use_socket (client, socket, ip,
987 port, initial_buffer, &error)) {
988 goto transfer_failed;
991 /* manage the client connection */
992 manage_client (server, client);
994 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1002 GST_ERROR_OBJECT (server, "failed to create a client");
1007 GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
1008 g_error_free (error);
1009 g_object_unref (client);
1015 * gst_rtsp_server_io_func:
1016 * @socket: a #GSocket
1017 * @condition: the condition on @source
1018 * @server: a #GstRTSPServer
1020 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
1021 * new connection on @socket or @server.
1023 * Returns: TRUE if the source could be connected, FALSE if an error occured.
1026 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
1027 GstRTSPServer * server)
1029 gboolean result = TRUE;
1030 GstRTSPClient *client = NULL;
1031 GstRTSPServerClass *klass;
1032 GError *error = NULL;
1034 if (condition & G_IO_IN) {
1035 klass = GST_RTSP_SERVER_GET_CLASS (server);
1037 if (klass->create_client)
1038 client = klass->create_client (server);
1042 /* a new client connected, create a client object to handle the client. */
1043 if (klass->accept_client)
1044 result = klass->accept_client (server, client, socket, &error);
1048 /* manage the client connection */
1049 manage_client (server, client);
1051 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1054 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
1061 GST_ERROR_OBJECT (server, "failed to create a client");
1066 GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
1067 g_error_free (error);
1068 g_object_unref (client);
1074 watch_destroyed (GstRTSPServer * server)
1076 GST_DEBUG_OBJECT (server, "source destroyed");
1077 g_object_unref (server->socket);
1078 server->socket = NULL;
1079 g_object_unref (server);
1083 * gst_rtsp_server_create_source:
1084 * @server: a #GstRTSPServer
1085 * @cancellable: a #GCancellable or %NULL.
1088 * Create a #GSource for @server. The new source will have a default
1089 * #GSocketSourceFunc of gst_rtsp_server_io_func().
1091 * @cancellable if not NULL can be used to cancel the source, which will cause
1092 * the source to trigger, reporting the current condition (which is likely 0
1093 * unless cancellation happened at the same time as a condition change). You can
1094 * check for this in the callback using g_cancellable_is_cancelled().
1096 * Returns: the #GSource for @server or NULL when an error occured. Free with
1100 gst_rtsp_server_create_source (GstRTSPServer * server,
1101 GCancellable * cancellable, GError ** error)
1103 GSocket *socket, *old;
1106 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1108 socket = gst_rtsp_server_create_socket (server, NULL, error);
1112 GST_RTSP_SERVER_LOCK (server);
1113 old = server->socket;
1114 server->socket = g_object_ref (socket);
1115 GST_RTSP_SERVER_UNLOCK (server);
1118 g_object_unref (old);
1120 /* create a watch for reads (new connections) and possible errors */
1121 source = g_socket_create_source (socket, G_IO_IN |
1122 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
1123 g_object_unref (socket);
1125 /* configure the callback */
1126 g_source_set_callback (source,
1127 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
1128 (GDestroyNotify) watch_destroyed);
1134 GST_ERROR_OBJECT (server, "failed to create socket");
1140 * gst_rtsp_server_attach:
1141 * @server: a #GstRTSPServer
1142 * @context: (allow-none): a #GMainContext
1144 * Attaches @server to @context. When the mainloop for @context is run, the
1145 * server will be dispatched. When @context is NULL, the default context will be
1148 * This function should be called when the server properties and urls are fully
1149 * configured and the server is ready to start.
1151 * Returns: the ID (greater than 0) for the source within the GMainContext.
1154 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1158 GError *error = NULL;
1160 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1162 source = gst_rtsp_server_create_source (server, NULL, &error);
1166 res = g_source_attach (source, context);
1167 g_source_unref (source);
1174 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1175 g_error_free (error);