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::media-mapping:
150 * The media mapping to use for this server. By default the server has no
151 * media mapping and thus cannot map urls to media streams.
153 g_object_class_install_property (gobject_class, PROP_MEDIA_MAPPING,
154 g_param_spec_object ("media-mapping", "Media Mapping",
155 "The media mapping to use for client session",
156 GST_TYPE_RTSP_MEDIA_MAPPING,
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->media_mapping = gst_rtsp_media_mapping_new ();
198 gst_rtsp_server_finalize (GObject * object)
200 GstRTSPServer *server = GST_RTSP_SERVER (object);
202 GST_DEBUG_OBJECT (server, "finalize server");
204 g_free (server->address);
205 g_free (server->service);
207 g_object_unref (server->socket);
209 g_object_unref (server->session_pool);
210 g_object_unref (server->media_mapping);
213 g_object_unref (server->auth);
215 g_mutex_clear (&server->lock);
217 G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
221 * gst_rtsp_server_new:
223 * Create a new #GstRTSPServer instance.
226 gst_rtsp_server_new (void)
228 GstRTSPServer *result;
230 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
236 * gst_rtsp_server_set_address:
237 * @server: a #GstRTSPServer
238 * @address: the address
240 * Configure @server to accept connections on the given address.
242 * This function must be called before the server is bound.
245 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
247 g_return_if_fail (GST_IS_RTSP_SERVER (server));
248 g_return_if_fail (address != NULL);
250 GST_RTSP_SERVER_LOCK (server);
251 g_free (server->address);
252 server->address = g_strdup (address);
253 GST_RTSP_SERVER_UNLOCK (server);
257 * gst_rtsp_server_get_address:
258 * @server: a #GstRTSPServer
260 * Get the address on which the server will accept connections.
262 * Returns: the server address. g_free() after usage.
265 gst_rtsp_server_get_address (GstRTSPServer * server)
268 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
270 GST_RTSP_SERVER_LOCK (server);
271 result = g_strdup (server->address);
272 GST_RTSP_SERVER_UNLOCK (server);
278 * gst_rtsp_server_get_bound_port:
279 * @server: a #GstRTSPServer
281 * Get the port number where the server was bound to.
283 * Returns: the port number
286 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
288 GSocketAddress *address;
291 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
293 GST_RTSP_SERVER_LOCK (server);
294 if (server->socket == NULL)
297 address = g_socket_get_local_address (server->socket, NULL);
298 result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
299 g_object_unref (address);
302 GST_RTSP_SERVER_UNLOCK (server);
308 * gst_rtsp_server_set_service:
309 * @server: a #GstRTSPServer
310 * @service: the service
312 * Configure @server to accept connections on the given service.
313 * @service should be a string containing the service name (see services(5)) or
314 * a string containing a port number between 1 and 65535.
316 * This function must be called before the server is bound.
319 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
321 g_return_if_fail (GST_IS_RTSP_SERVER (server));
322 g_return_if_fail (service != NULL);
324 GST_RTSP_SERVER_LOCK (server);
325 g_free (server->service);
326 server->service = g_strdup (service);
327 GST_RTSP_SERVER_UNLOCK (server);
331 * gst_rtsp_server_get_service:
332 * @server: a #GstRTSPServer
334 * Get the service on which the server will accept connections.
336 * Returns: the service. use g_free() after usage.
339 gst_rtsp_server_get_service (GstRTSPServer * server)
343 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
345 GST_RTSP_SERVER_LOCK (server);
346 result = g_strdup (server->service);
347 GST_RTSP_SERVER_UNLOCK (server);
353 * gst_rtsp_server_set_backlog:
354 * @server: a #GstRTSPServer
355 * @backlog: the backlog
357 * configure the maximum amount of requests that may be queued for the
360 * This function must be called before the server is bound.
363 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
365 g_return_if_fail (GST_IS_RTSP_SERVER (server));
367 GST_RTSP_SERVER_LOCK (server);
368 server->backlog = backlog;
369 GST_RTSP_SERVER_UNLOCK (server);
373 * gst_rtsp_server_get_backlog:
374 * @server: a #GstRTSPServer
376 * The maximum amount of queued requests for the server.
378 * Returns: the server backlog.
381 gst_rtsp_server_get_backlog (GstRTSPServer * server)
385 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
387 GST_RTSP_SERVER_LOCK (server);
388 result = server->backlog;
389 GST_RTSP_SERVER_UNLOCK (server);
395 * gst_rtsp_server_set_session_pool:
396 * @server: a #GstRTSPServer
397 * @pool: a #GstRTSPSessionPool
399 * configure @pool to be used as the session pool of @server.
402 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
403 GstRTSPSessionPool * pool)
405 GstRTSPSessionPool *old;
407 g_return_if_fail (GST_IS_RTSP_SERVER (server));
412 GST_RTSP_SERVER_LOCK (server);
413 old = server->session_pool;
414 server->session_pool = pool;
415 GST_RTSP_SERVER_UNLOCK (server);
418 g_object_unref (old);
422 * gst_rtsp_server_get_session_pool:
423 * @server: a #GstRTSPServer
425 * Get the #GstRTSPSessionPool used as the session pool of @server.
427 * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
431 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
433 GstRTSPSessionPool *result;
435 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
437 GST_RTSP_SERVER_LOCK (server);
438 if ((result = server->session_pool))
439 g_object_ref (result);
440 GST_RTSP_SERVER_UNLOCK (server);
446 * gst_rtsp_server_set_media_mapping:
447 * @server: a #GstRTSPServer
448 * @mapping: a #GstRTSPMediaMapping
450 * configure @mapping to be used as the media mapping of @server.
453 gst_rtsp_server_set_media_mapping (GstRTSPServer * server,
454 GstRTSPMediaMapping * mapping)
456 GstRTSPMediaMapping *old;
458 g_return_if_fail (GST_IS_RTSP_SERVER (server));
461 g_object_ref (mapping);
463 GST_RTSP_SERVER_LOCK (server);
464 old = server->media_mapping;
465 server->media_mapping = mapping;
466 GST_RTSP_SERVER_UNLOCK (server);
469 g_object_unref (old);
474 * gst_rtsp_server_get_media_mapping:
475 * @server: a #GstRTSPServer
477 * Get the #GstRTSPMediaMapping used as the media mapping of @server.
479 * Returns: (transfer full): the #GstRTSPMediaMapping of @server. g_object_unref() after
482 GstRTSPMediaMapping *
483 gst_rtsp_server_get_media_mapping (GstRTSPServer * server)
485 GstRTSPMediaMapping *result;
487 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
489 GST_RTSP_SERVER_LOCK (server);
490 if ((result = server->media_mapping))
491 g_object_ref (result);
492 GST_RTSP_SERVER_UNLOCK (server);
498 * gst_rtsp_server_set_auth:
499 * @server: a #GstRTSPServer
500 * @auth: a #GstRTSPAuth
502 * configure @auth to be used as the authentication manager of @server.
505 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
509 g_return_if_fail (GST_IS_RTSP_SERVER (server));
514 GST_RTSP_SERVER_LOCK (server);
517 GST_RTSP_SERVER_UNLOCK (server);
520 g_object_unref (old);
525 * gst_rtsp_server_get_auth:
526 * @server: a #GstRTSPServer
528 * Get the #GstRTSPAuth used as the authentication manager of @server.
530 * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
534 gst_rtsp_server_get_auth (GstRTSPServer * server)
538 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
540 GST_RTSP_SERVER_LOCK (server);
541 if ((result = server->auth))
542 g_object_ref (result);
543 GST_RTSP_SERVER_UNLOCK (server);
549 * gst_rtsp_server_set_max_threads:
550 * @server: a #GstRTSPServer
551 * @max_threads: maximum threads
553 * Set the maximum threads used by the server to handle client requests.
554 * A value of 0 will use the server mainloop, a value of -1 will use an
555 * unlimited number of threads.
558 gst_rtsp_server_set_max_threads (GstRTSPServer * server, gint max_threads)
560 g_return_if_fail (GST_IS_RTSP_SERVER (server));
562 GST_RTSP_SERVER_LOCK (server);
563 server->max_threads = max_threads;
564 GST_RTSP_SERVER_UNLOCK (server);
568 * gst_rtsp_server_get_max_threads:
569 * @server: a #GstRTSPServer
571 * Get the maximum number of threads used for client connections.
572 * See gst_rtsp_server_set_max_threads().
574 * Returns: the maximum number of threads.
577 gst_rtsp_server_get_max_threads (GstRTSPServer * server)
581 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
583 GST_RTSP_SERVER_LOCK (server);
584 res = server->max_threads;
585 GST_RTSP_SERVER_UNLOCK (server);
592 gst_rtsp_server_get_property (GObject * object, guint propid,
593 GValue * value, GParamSpec * pspec)
595 GstRTSPServer *server = GST_RTSP_SERVER (object);
599 g_value_take_string (value, gst_rtsp_server_get_address (server));
602 g_value_take_string (value, gst_rtsp_server_get_service (server));
604 case PROP_BOUND_PORT:
605 g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
608 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
610 case PROP_SESSION_POOL:
611 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
613 case PROP_MEDIA_MAPPING:
614 g_value_take_object (value, gst_rtsp_server_get_media_mapping (server));
616 case PROP_MAX_THREADS:
617 g_value_set_int (value, gst_rtsp_server_get_max_threads (server));
620 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
625 gst_rtsp_server_set_property (GObject * object, guint propid,
626 const GValue * value, GParamSpec * pspec)
628 GstRTSPServer *server = GST_RTSP_SERVER (object);
632 gst_rtsp_server_set_address (server, g_value_get_string (value));
635 gst_rtsp_server_set_service (server, g_value_get_string (value));
638 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
640 case PROP_SESSION_POOL:
641 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
643 case PROP_MEDIA_MAPPING:
644 gst_rtsp_server_set_media_mapping (server, g_value_get_object (value));
646 case PROP_MAX_THREADS:
647 gst_rtsp_server_set_max_threads (server, g_value_get_int (value));
650 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
655 * gst_rtsp_server_create_socket:
656 * @server: a #GstRTSPServer
657 * @cancellable: a #GCancellable
660 * Create a #GSocket for @server. The socket will listen on the
661 * configured service.
663 * Returns: (transfer full): the #GSocket for @server or NULL when an error occured.
666 gst_rtsp_server_create_socket (GstRTSPServer * server,
667 GCancellable * cancellable, GError ** error)
669 GSocketConnectable *conn;
670 GSocketAddressEnumerator *enumerator;
671 GSocket *socket = NULL;
673 struct linger linger;
675 GError *sock_error = NULL;
676 GError *bind_error = NULL;
679 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
681 GST_RTSP_SERVER_LOCK (server);
682 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
685 /* resolve the server IP address */
686 port = atoi (server->service);
687 if (port != 0 || !strcmp (server->service, "0"))
688 conn = g_network_address_new (server->address, port);
690 conn = g_network_service_new (server->service, "tcp", server->address);
692 enumerator = g_socket_connectable_enumerate (conn);
693 g_object_unref (conn);
695 /* create server socket, we loop through all the addresses until we manage to
696 * create a socket and bind. */
698 GSocketAddress *sockaddr;
701 g_socket_address_enumerator_next (enumerator, cancellable, error);
704 GST_DEBUG_OBJECT (server, "no more addresses %s",
705 *error ? (*error)->message : "");
707 GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
712 /* only keep the first error */
713 socket = g_socket_new (g_socket_address_get_family (sockaddr),
714 G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
715 sock_error ? NULL : &sock_error);
717 if (socket == NULL) {
718 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
719 sock_error->message);
723 if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
724 g_object_unref (sockaddr);
728 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
729 bind_error->message);
730 g_object_unref (sockaddr);
731 g_object_unref (socket);
734 g_object_unref (enumerator);
739 g_clear_error (&sock_error);
740 g_clear_error (&bind_error);
742 GST_DEBUG_OBJECT (server, "opened sending server socket");
744 /* keep connection alive; avoids SIGPIPE during write */
745 g_socket_set_keepalive (socket, TRUE);
749 /* make sure socket is reset 5 seconds after close. This ensure that we can
750 * reuse the socket quickly while still having a chance to send data to the
754 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
755 (void *) &linger, sizeof (linger)) < 0)
760 /* set the server socket to nonblocking */
761 g_socket_set_blocking (socket, FALSE);
763 /* set listen backlog */
764 g_socket_set_listen_backlog (socket, server->backlog);
766 if (!g_socket_listen (socket, error))
769 GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
770 socket, server->backlog);
772 GST_RTSP_SERVER_UNLOCK (server);
779 GST_ERROR_OBJECT (server, "failed to create socket");
786 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
794 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
801 g_object_unref (socket);
805 g_propagate_error (error, sock_error);
807 g_error_free (sock_error);
810 if ((error == NULL) || (*error == NULL))
811 g_propagate_error (error, bind_error);
813 g_error_free (bind_error);
815 GST_RTSP_SERVER_UNLOCK (server);
820 struct _ClientContext
822 GstRTSPServer *server;
824 GMainContext *context;
825 GstRTSPClient *client;
829 free_client_context (ClientContext * ctx)
831 g_main_context_unref (ctx->context);
833 g_main_loop_unref (ctx->loop);
834 g_object_unref (ctx->client);
835 g_slice_free (ClientContext, ctx);
839 do_loop (ClientContext * ctx)
841 GST_INFO ("enter mainloop");
842 g_main_loop_run (ctx->loop);
843 GST_INFO ("exit mainloop");
845 free_client_context (ctx);
851 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
853 GstRTSPServer *server = ctx->server;
855 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
857 g_object_ref (server);
858 gst_rtsp_client_set_server (client, NULL);
860 GST_RTSP_SERVER_LOCK (server);
861 server->clients = g_list_remove (server->clients, ctx);
862 GST_RTSP_SERVER_UNLOCK (server);
865 g_main_loop_quit (ctx->loop);
867 free_client_context (ctx);
869 g_object_unref (server);
872 /* add the client context to the active list of clients, takes ownership
875 manage_client (GstRTSPServer * server, GstRTSPClient * client)
879 GST_DEBUG_OBJECT (server, "manage client %p", client);
880 gst_rtsp_client_set_server (client, server);
882 ctx = g_slice_new0 (ClientContext);
883 ctx->server = server;
884 ctx->client = client;
885 if (server->max_threads == 0) {
888 /* find the context to add the watch */
889 if ((source = g_main_current_source ()))
890 ctx->context = g_main_context_ref (g_source_get_context (source));
894 ctx->context = g_main_context_new ();
895 ctx->loop = g_main_loop_new (ctx->context, TRUE);
897 gst_rtsp_client_attach (client, ctx->context);
899 GST_RTSP_SERVER_LOCK (server);
900 g_signal_connect (client, "closed", (GCallback) unmanage_client, ctx);
901 server->clients = g_list_prepend (server->clients, ctx);
902 GST_RTSP_SERVER_UNLOCK (server);
905 GstRTSPServerClass *klass = GST_RTSP_SERVER_GET_CLASS (server);
907 g_thread_pool_push (klass->pool, ctx, NULL);
911 static GstRTSPClient *
912 default_create_client (GstRTSPServer * server)
914 GstRTSPClient *client;
916 /* a new client connected, create a session to handle the client. */
917 client = gst_rtsp_client_new ();
919 /* set the session pool that this client should use */
920 GST_RTSP_SERVER_LOCK (server);
921 gst_rtsp_client_set_session_pool (client, server->session_pool);
922 /* set the media mapping that this client should use */
923 gst_rtsp_client_set_media_mapping (client, server->media_mapping);
924 /* set authentication manager */
925 gst_rtsp_client_set_auth (client, server->auth);
926 GST_RTSP_SERVER_UNLOCK (server);
931 /* default method for creating a new client object in the server to accept and
932 * handle a client connection on this server */
934 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
935 GSocket * socket, GError ** error)
937 /* accept connections for that client, this function returns after accepting
938 * the connection and will run the remainder of the communication with the
939 * client asyncronously. */
940 if (!gst_rtsp_client_accept (client, socket, NULL, error))
948 GST_ERROR_OBJECT (server,
949 "Could not accept client on server : %s", (*error)->message);
955 * gst_rtsp_server_transfer_connection:
956 * @server: a #GstRTSPServer
957 * @socket: a network socket
958 * @ip: the IP address of the remote client
959 * @port: the port used by the other end
960 * @initial_buffer: any initial data that was already read from the socket
962 * Take an existing network socket and use it for an RTSP connection. This
963 * is used when transferring a socket from an HTTP server which should be used
964 * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
965 * that the HTTP server read from the socket while parsing the HTTP header.
967 * Returns: TRUE if all was ok, FALSE if an error occured.
970 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
971 const gchar * ip, gint port, const gchar * initial_buffer)
973 GstRTSPClient *client = NULL;
974 GstRTSPServerClass *klass;
975 GError *error = NULL;
977 klass = GST_RTSP_SERVER_GET_CLASS (server);
979 if (klass->create_client)
980 client = klass->create_client (server);
984 /* a new client connected, create a client object to handle the client. */
985 if (!gst_rtsp_client_use_socket (client, socket, ip,
986 port, initial_buffer, &error)) {
987 goto transfer_failed;
990 /* manage the client connection */
991 manage_client (server, client);
993 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1001 GST_ERROR_OBJECT (server, "failed to create a client");
1006 GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
1007 g_error_free (error);
1008 g_object_unref (client);
1014 * gst_rtsp_server_io_func:
1015 * @socket: a #GSocket
1016 * @condition: the condition on @source
1017 * @server: a #GstRTSPServer
1019 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
1020 * new connection on @socket or @server.
1022 * Returns: TRUE if the source could be connected, FALSE if an error occured.
1025 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
1026 GstRTSPServer * server)
1028 gboolean result = TRUE;
1029 GstRTSPClient *client = NULL;
1030 GstRTSPServerClass *klass;
1031 GError *error = NULL;
1033 if (condition & G_IO_IN) {
1034 klass = GST_RTSP_SERVER_GET_CLASS (server);
1036 if (klass->create_client)
1037 client = klass->create_client (server);
1041 /* a new client connected, create a client object to handle the client. */
1042 if (klass->accept_client)
1043 result = klass->accept_client (server, client, socket, &error);
1047 /* manage the client connection */
1048 manage_client (server, client);
1050 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1053 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
1060 GST_ERROR_OBJECT (server, "failed to create a client");
1065 GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
1066 g_error_free (error);
1067 g_object_unref (client);
1073 watch_destroyed (GstRTSPServer * server)
1075 GST_DEBUG_OBJECT (server, "source destroyed");
1076 g_object_unref (server);
1080 * gst_rtsp_server_create_source:
1081 * @server: a #GstRTSPServer
1082 * @cancellable: a #GCancellable or %NULL.
1085 * Create a #GSource for @server. The new source will have a default
1086 * #GSocketSourceFunc of gst_rtsp_server_io_func().
1088 * @cancellable if not NULL can be used to cancel the source, which will cause
1089 * the source to trigger, reporting the current condition (which is likely 0
1090 * unless cancellation happened at the same time as a condition change). You can
1091 * check for this in the callback using g_cancellable_is_cancelled().
1093 * Returns: the #GSource for @server or NULL when an error occured. Free with
1097 gst_rtsp_server_create_source (GstRTSPServer * server,
1098 GCancellable * cancellable, GError ** error)
1103 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1105 socket = gst_rtsp_server_create_socket (server, NULL, error);
1108 server->socket = g_object_ref (socket);
1110 /* create a watch for reads (new connections) and possible errors */
1111 source = g_socket_create_source (socket, G_IO_IN |
1112 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
1113 g_object_unref (socket);
1115 /* configure the callback */
1116 g_source_set_callback (source,
1117 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
1118 (GDestroyNotify) watch_destroyed);
1124 GST_ERROR_OBJECT (server, "failed to create socket");
1130 * gst_rtsp_server_attach:
1131 * @server: a #GstRTSPServer
1132 * @context: (allow-none): a #GMainContext
1134 * Attaches @server to @context. When the mainloop for @context is run, the
1135 * server will be dispatched. When @context is NULL, the default context will be
1138 * This function should be called when the server properties and urls are fully
1139 * configured and the server is ready to start.
1141 * Returns: the ID (greater than 0) for the source within the GMainContext.
1144 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1148 GError *error = NULL;
1150 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1152 source = gst_rtsp_server_create_source (server, NULL, &error);
1156 res = g_source_attach (source, context);
1157 g_source_unref (source);
1164 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1165 g_error_free (error);