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
32 /* Define to use the SO_LINGER option so that the server sockets can be resused
33 * sooner. Disabled for now because it is not very well implemented by various
34 * OSes and it causes clients to fail to read the TEARDOWN response. */
52 SIGNAL_CLIENT_CONNECTED,
56 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
58 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
59 #define GST_CAT_DEFAULT rtsp_server_debug
61 static guint gst_rtsp_server_signals[SIGNAL_LAST] = { 0 };
63 static void gst_rtsp_server_get_property (GObject * object, guint propid,
64 GValue * value, GParamSpec * pspec);
65 static void gst_rtsp_server_set_property (GObject * object, guint propid,
66 const GValue * value, GParamSpec * pspec);
67 static void gst_rtsp_server_finalize (GObject * object);
69 static GstRTSPClient *default_create_client (GstRTSPServer * server);
70 static gboolean default_accept_client (GstRTSPServer * server,
71 GstRTSPClient * client, GSocket * socket, GError ** error);
74 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
76 GObjectClass *gobject_class;
78 gobject_class = G_OBJECT_CLASS (klass);
80 gobject_class->get_property = gst_rtsp_server_get_property;
81 gobject_class->set_property = gst_rtsp_server_set_property;
82 gobject_class->finalize = gst_rtsp_server_finalize;
85 * GstRTSPServer::address:
87 * The address of the server. This is the address where the server will
90 g_object_class_install_property (gobject_class, PROP_ADDRESS,
91 g_param_spec_string ("address", "Address",
92 "The address the server uses to listen on", DEFAULT_ADDRESS,
93 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
95 * GstRTSPServer::service:
97 * The service of the server. This is either a string with the service name or
98 * a port number (as a string) the server will listen on.
100 g_object_class_install_property (gobject_class, PROP_SERVICE,
101 g_param_spec_string ("service", "Service",
102 "The service or port number the server uses to listen on",
103 DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
105 * GstRTSPServer::bound-port:
107 * The actual port the server is listening on. Can be used to retrieve the
108 * port number when the server is started on port 0, which means bind to a
109 * random port. Set to -1 if the server has not been bound yet.
111 g_object_class_install_property (gobject_class, PROP_BOUND_PORT,
112 g_param_spec_int ("bound-port", "Bound port",
113 "The port number the server is listening on",
114 -1, G_MAXUINT16, DEFAULT_BOUND_PORT,
115 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
117 * GstRTSPServer::backlog:
119 * The backlog argument defines the maximum length to which the queue of
120 * pending connections for the server may grow. If a connection request arrives
121 * when the queue is full, the client may receive an error with an indication of
122 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
123 * request may be ignored so that a later reattempt at connection succeeds.
125 g_object_class_install_property (gobject_class, PROP_BACKLOG,
126 g_param_spec_int ("backlog", "Backlog",
127 "The maximum length to which the queue "
128 "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
129 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
131 * GstRTSPServer::session-pool:
133 * The session pool of the server. By default each server has a separate
134 * session pool but sessions can be shared between servers by setting the same
135 * session pool on multiple servers.
137 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
138 g_param_spec_object ("session-pool", "Session Pool",
139 "The session pool to use for client session",
140 GST_TYPE_RTSP_SESSION_POOL,
141 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
143 * GstRTSPServer::media-mapping:
145 * The media mapping to use for this server. By default the server has no
146 * media mapping and thus cannot map urls to media streams.
148 g_object_class_install_property (gobject_class, PROP_MEDIA_MAPPING,
149 g_param_spec_object ("media-mapping", "Media Mapping",
150 "The media mapping to use for client session",
151 GST_TYPE_RTSP_MEDIA_MAPPING,
152 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
154 gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED] =
155 g_signal_new ("client-connected", G_TYPE_FROM_CLASS (gobject_class),
156 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPServerClass, client_connected),
157 NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
158 gst_rtsp_client_get_type ());
160 klass->create_client = default_create_client;
161 klass->accept_client = default_accept_client;
163 GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
167 gst_rtsp_server_init (GstRTSPServer * server)
169 g_mutex_init (&server->lock);
170 server->address = g_strdup (DEFAULT_ADDRESS);
171 server->service = g_strdup (DEFAULT_SERVICE);
172 server->socket = NULL;
173 server->backlog = DEFAULT_BACKLOG;
174 server->session_pool = gst_rtsp_session_pool_new ();
175 server->media_mapping = gst_rtsp_media_mapping_new ();
179 gst_rtsp_server_finalize (GObject * object)
181 GstRTSPServer *server = GST_RTSP_SERVER (object);
183 GST_DEBUG_OBJECT (server, "finalize server");
185 g_free (server->address);
186 g_free (server->service);
188 g_object_unref (server->socket);
190 g_object_unref (server->session_pool);
191 g_object_unref (server->media_mapping);
194 g_object_unref (server->auth);
196 g_mutex_clear (&server->lock);
198 G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
202 * gst_rtsp_server_new:
204 * Create a new #GstRTSPServer instance.
207 gst_rtsp_server_new (void)
209 GstRTSPServer *result;
211 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
217 * gst_rtsp_server_set_address:
218 * @server: a #GstRTSPServer
219 * @address: the address
221 * Configure @server to accept connections on the given address.
223 * This function must be called before the server is bound.
226 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
228 g_return_if_fail (GST_IS_RTSP_SERVER (server));
229 g_return_if_fail (address != NULL);
231 GST_RTSP_SERVER_LOCK (server);
232 g_free (server->address);
233 server->address = g_strdup (address);
234 GST_RTSP_SERVER_UNLOCK (server);
238 * gst_rtsp_server_get_address:
239 * @server: a #GstRTSPServer
241 * Get the address on which the server will accept connections.
243 * Returns: the server address. g_free() after usage.
246 gst_rtsp_server_get_address (GstRTSPServer * server)
249 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
251 GST_RTSP_SERVER_LOCK (server);
252 result = g_strdup (server->address);
253 GST_RTSP_SERVER_UNLOCK (server);
259 * gst_rtsp_server_get_bound_port:
260 * @server: a #GstRTSPServer
262 * Get the port number where the server was bound to.
264 * Returns: the port number
267 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
269 GSocketAddress *address;
272 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
274 GST_RTSP_SERVER_LOCK (server);
275 if (server->socket == NULL)
278 address = g_socket_get_local_address (server->socket, NULL);
279 result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
280 g_object_unref (address);
283 GST_RTSP_SERVER_UNLOCK (server);
289 * gst_rtsp_server_set_service:
290 * @server: a #GstRTSPServer
291 * @service: the service
293 * Configure @server to accept connections on the given service.
294 * @service should be a string containing the service name (see services(5)) or
295 * a string containing a port number between 1 and 65535.
297 * This function must be called before the server is bound.
300 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
302 g_return_if_fail (GST_IS_RTSP_SERVER (server));
303 g_return_if_fail (service != NULL);
305 GST_RTSP_SERVER_LOCK (server);
306 g_free (server->service);
307 server->service = g_strdup (service);
308 GST_RTSP_SERVER_UNLOCK (server);
312 * gst_rtsp_server_get_service:
313 * @server: a #GstRTSPServer
315 * Get the service on which the server will accept connections.
317 * Returns: the service. use g_free() after usage.
320 gst_rtsp_server_get_service (GstRTSPServer * server)
324 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
326 GST_RTSP_SERVER_LOCK (server);
327 result = g_strdup (server->service);
328 GST_RTSP_SERVER_UNLOCK (server);
334 * gst_rtsp_server_set_backlog:
335 * @server: a #GstRTSPServer
336 * @backlog: the backlog
338 * configure the maximum amount of requests that may be queued for the
341 * This function must be called before the server is bound.
344 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
346 g_return_if_fail (GST_IS_RTSP_SERVER (server));
348 GST_RTSP_SERVER_LOCK (server);
349 server->backlog = backlog;
350 GST_RTSP_SERVER_UNLOCK (server);
354 * gst_rtsp_server_get_backlog:
355 * @server: a #GstRTSPServer
357 * The maximum amount of queued requests for the server.
359 * Returns: the server backlog.
362 gst_rtsp_server_get_backlog (GstRTSPServer * server)
366 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
368 GST_RTSP_SERVER_LOCK (server);
369 result = server->backlog;
370 GST_RTSP_SERVER_UNLOCK (server);
376 * gst_rtsp_server_set_session_pool:
377 * @server: a #GstRTSPServer
378 * @pool: a #GstRTSPSessionPool
380 * configure @pool to be used as the session pool of @server.
383 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
384 GstRTSPSessionPool * pool)
386 GstRTSPSessionPool *old;
388 g_return_if_fail (GST_IS_RTSP_SERVER (server));
393 GST_RTSP_SERVER_LOCK (server);
394 old = server->session_pool;
395 server->session_pool = pool;
396 GST_RTSP_SERVER_UNLOCK (server);
399 g_object_unref (old);
403 * gst_rtsp_server_get_session_pool:
404 * @server: a #GstRTSPServer
406 * Get the #GstRTSPSessionPool used as the session pool of @server.
408 * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
412 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
414 GstRTSPSessionPool *result;
416 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
418 GST_RTSP_SERVER_LOCK (server);
419 if ((result = server->session_pool))
420 g_object_ref (result);
421 GST_RTSP_SERVER_UNLOCK (server);
427 * gst_rtsp_server_set_media_mapping:
428 * @server: a #GstRTSPServer
429 * @mapping: a #GstRTSPMediaMapping
431 * configure @mapping to be used as the media mapping of @server.
434 gst_rtsp_server_set_media_mapping (GstRTSPServer * server,
435 GstRTSPMediaMapping * mapping)
437 GstRTSPMediaMapping *old;
439 g_return_if_fail (GST_IS_RTSP_SERVER (server));
442 g_object_ref (mapping);
444 GST_RTSP_SERVER_LOCK (server);
445 old = server->media_mapping;
446 server->media_mapping = mapping;
447 GST_RTSP_SERVER_UNLOCK (server);
450 g_object_unref (old);
455 * gst_rtsp_server_get_media_mapping:
456 * @server: a #GstRTSPServer
458 * Get the #GstRTSPMediaMapping used as the media mapping of @server.
460 * Returns: (transfer full): the #GstRTSPMediaMapping of @server. g_object_unref() after
463 GstRTSPMediaMapping *
464 gst_rtsp_server_get_media_mapping (GstRTSPServer * server)
466 GstRTSPMediaMapping *result;
468 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
470 GST_RTSP_SERVER_LOCK (server);
471 if ((result = server->media_mapping))
472 g_object_ref (result);
473 GST_RTSP_SERVER_UNLOCK (server);
479 * gst_rtsp_server_set_auth:
480 * @server: a #GstRTSPServer
481 * @auth: a #GstRTSPAuth
483 * configure @auth to be used as the authentication manager of @server.
486 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
490 g_return_if_fail (GST_IS_RTSP_SERVER (server));
495 GST_RTSP_SERVER_LOCK (server);
498 GST_RTSP_SERVER_UNLOCK (server);
501 g_object_unref (old);
506 * gst_rtsp_server_get_auth:
507 * @server: a #GstRTSPServer
509 * Get the #GstRTSPAuth used as the authentication manager of @server.
511 * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
515 gst_rtsp_server_get_auth (GstRTSPServer * server)
519 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
521 GST_RTSP_SERVER_LOCK (server);
522 if ((result = server->auth))
523 g_object_ref (result);
524 GST_RTSP_SERVER_UNLOCK (server);
530 gst_rtsp_server_get_property (GObject * object, guint propid,
531 GValue * value, GParamSpec * pspec)
533 GstRTSPServer *server = GST_RTSP_SERVER (object);
537 g_value_take_string (value, gst_rtsp_server_get_address (server));
540 g_value_take_string (value, gst_rtsp_server_get_service (server));
542 case PROP_BOUND_PORT:
543 g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
546 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
548 case PROP_SESSION_POOL:
549 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
551 case PROP_MEDIA_MAPPING:
552 g_value_take_object (value, gst_rtsp_server_get_media_mapping (server));
555 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
560 gst_rtsp_server_set_property (GObject * object, guint propid,
561 const GValue * value, GParamSpec * pspec)
563 GstRTSPServer *server = GST_RTSP_SERVER (object);
567 gst_rtsp_server_set_address (server, g_value_get_string (value));
570 gst_rtsp_server_set_service (server, g_value_get_string (value));
573 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
575 case PROP_SESSION_POOL:
576 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
578 case PROP_MEDIA_MAPPING:
579 gst_rtsp_server_set_media_mapping (server, g_value_get_object (value));
582 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
587 * gst_rtsp_server_create_socket:
588 * @server: a #GstRTSPServer
589 * @cancellable: a #GCancellable
592 * Create a #GSocket for @server. The socket will listen on the
593 * configured service.
595 * Returns: (transfer full): the #GSocket for @server or NULL when an error occured.
598 gst_rtsp_server_create_socket (GstRTSPServer * server,
599 GCancellable * cancellable, GError ** error)
601 GSocketConnectable *conn;
602 GSocketAddressEnumerator *enumerator;
603 GSocket *socket = NULL;
605 struct linger linger;
607 GError *sock_error = NULL;
608 GError *bind_error = NULL;
611 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
613 GST_RTSP_SERVER_LOCK (server);
614 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
617 /* resolve the server IP address */
618 port = atoi (server->service);
619 if (port != 0 || !strcmp (server->service, "0"))
620 conn = g_network_address_new (server->address, port);
622 conn = g_network_service_new (server->service, "tcp", server->address);
624 enumerator = g_socket_connectable_enumerate (conn);
625 g_object_unref (conn);
627 /* create server socket, we loop through all the addresses until we manage to
628 * create a socket and bind. */
630 GSocketAddress *sockaddr;
633 g_socket_address_enumerator_next (enumerator, cancellable, error);
636 GST_DEBUG_OBJECT (server, "no more addresses %s",
637 *error ? (*error)->message : "");
639 GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
644 /* only keep the first error */
645 socket = g_socket_new (g_socket_address_get_family (sockaddr),
646 G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
647 sock_error ? NULL : &sock_error);
649 if (socket == NULL) {
650 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
651 sock_error->message);
655 if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
656 g_object_unref (sockaddr);
660 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
661 bind_error->message);
662 g_object_unref (sockaddr);
663 g_object_unref (socket);
666 g_object_unref (enumerator);
671 g_clear_error (&sock_error);
672 g_clear_error (&bind_error);
674 GST_DEBUG_OBJECT (server, "opened sending server socket");
676 /* keep connection alive; avoids SIGPIPE during write */
677 g_socket_set_keepalive (socket, TRUE);
681 /* make sure socket is reset 5 seconds after close. This ensure that we can
682 * reuse the socket quickly while still having a chance to send data to the
686 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
687 (void *) &linger, sizeof (linger)) < 0)
692 /* set the server socket to nonblocking */
693 g_socket_set_blocking (socket, FALSE);
695 /* set listen backlog */
696 g_socket_set_listen_backlog (socket, server->backlog);
698 if (!g_socket_listen (socket, error))
701 GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
702 socket, server->backlog);
704 GST_RTSP_SERVER_UNLOCK (server);
711 GST_ERROR_OBJECT (server, "failed to create socket");
718 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
726 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
733 g_object_unref (socket);
737 g_propagate_error (error, sock_error);
739 g_error_free (sock_error);
742 if ((error == NULL) || (*error == NULL))
743 g_propagate_error (error, bind_error);
745 g_error_free (bind_error);
747 GST_RTSP_SERVER_UNLOCK (server);
754 GstRTSPServer *server;
756 GMainContext *context;
757 GstRTSPClient *client;
761 free_client_context (ClientContext * ctx)
763 g_main_context_unref (ctx->context);
765 g_main_loop_unref (ctx->loop);
766 g_object_unref (ctx->client);
767 g_slice_free (ClientContext, ctx);
771 do_loop (ClientContext * ctx)
773 GST_INFO ("enter mainloop");
774 g_main_loop_run (ctx->loop);
775 GST_INFO ("exit mainloop");
777 free_client_context (ctx);
783 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
785 GstRTSPServer *server = ctx->server;
787 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
789 g_object_ref (server);
790 gst_rtsp_client_set_server (client, NULL);
792 GST_RTSP_SERVER_LOCK (server);
793 server->clients = g_list_remove (server->clients, ctx);
794 GST_RTSP_SERVER_UNLOCK (server);
797 g_main_loop_quit (ctx->loop);
799 free_client_context (ctx);
801 g_object_unref (server);
804 /* add the client context to the active list of clients, takes ownership
807 manage_client (GstRTSPServer * server, GstRTSPClient * client)
811 GST_DEBUG_OBJECT (server, "manage client %p", client);
812 gst_rtsp_client_set_server (client, server);
814 ctx = g_slice_new0 (ClientContext);
815 ctx->server = server;
816 ctx->client = client;
821 /* find the context to add the watch */
822 if ((source = g_main_current_source ()))
823 ctx->context = g_main_context_ref (g_source_get_context (source));
828 ctx->context = g_main_context_new ();
829 ctx->loop = g_main_loop_new (ctx->context, TRUE);
830 ctx->dothread = TRUE;
832 gst_rtsp_client_attach (client, ctx->context);
834 GST_RTSP_SERVER_LOCK (server);
835 g_signal_connect (client, "closed", (GCallback) unmanage_client, ctx);
836 server->clients = g_list_prepend (server->clients, ctx);
837 GST_RTSP_SERVER_UNLOCK (server);
842 thread = g_thread_new ("MainLoop Thread", (GThreadFunc) do_loop, ctx);
843 g_thread_unref (thread);
847 static GstRTSPClient *
848 default_create_client (GstRTSPServer * server)
850 GstRTSPClient *client;
852 /* a new client connected, create a session to handle the client. */
853 client = gst_rtsp_client_new ();
855 /* set the session pool that this client should use */
856 GST_RTSP_SERVER_LOCK (server);
857 gst_rtsp_client_set_session_pool (client, server->session_pool);
858 /* set the media mapping that this client should use */
859 gst_rtsp_client_set_media_mapping (client, server->media_mapping);
860 /* set authentication manager */
861 gst_rtsp_client_set_auth (client, server->auth);
862 GST_RTSP_SERVER_UNLOCK (server);
867 /* default method for creating a new client object in the server to accept and
868 * handle a client connection on this server */
870 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
871 GSocket * socket, GError ** error)
873 /* accept connections for that client, this function returns after accepting
874 * the connection and will run the remainder of the communication with the
875 * client asyncronously. */
876 if (!gst_rtsp_client_accept (client, socket, NULL, error))
884 GST_ERROR_OBJECT (server,
885 "Could not accept client on server : %s", (*error)->message);
891 * gst_rtsp_server_transfer_connection:
892 * @server: a #GstRTSPServer
893 * @socket: a network socket
894 * @ip: the IP address of the remote client
895 * @port: the port used by the other end
896 * @initial_buffer: any initial data that was already read from the socket
898 * Take an existing network socket and use it for an RTSP connection. This
899 * is used when transferring a socket from an HTTP server which should be used
900 * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
901 * that the HTTP server read from the socket while parsing the HTTP header.
903 * Returns: TRUE if all was ok, FALSE if an error occured.
906 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
907 const gchar * ip, gint port, const gchar * initial_buffer)
909 GstRTSPClient *client = NULL;
910 GstRTSPServerClass *klass;
911 GError *error = NULL;
913 klass = GST_RTSP_SERVER_GET_CLASS (server);
915 if (klass->create_client)
916 client = klass->create_client (server);
920 /* a new client connected, create a client object to handle the client. */
921 if (!gst_rtsp_client_use_socket (client, socket, ip,
922 port, initial_buffer, &error)) {
923 goto transfer_failed;
926 /* manage the client connection */
927 manage_client (server, client);
929 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
937 GST_ERROR_OBJECT (server, "failed to create a client");
942 GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
943 g_error_free (error);
944 g_object_unref (client);
950 * gst_rtsp_server_io_func:
951 * @socket: a #GSocket
952 * @condition: the condition on @source
953 * @server: a #GstRTSPServer
955 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
956 * new connection on @socket or @server.
958 * Returns: TRUE if the source could be connected, FALSE if an error occured.
961 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
962 GstRTSPServer * server)
964 gboolean result = TRUE;
965 GstRTSPClient *client = NULL;
966 GstRTSPServerClass *klass;
967 GError *error = NULL;
969 if (condition & G_IO_IN) {
970 klass = GST_RTSP_SERVER_GET_CLASS (server);
972 if (klass->create_client)
973 client = klass->create_client (server);
977 /* a new client connected, create a client object to handle the client. */
978 if (klass->accept_client)
979 result = klass->accept_client (server, client, socket, &error);
983 /* manage the client connection */
984 manage_client (server, client);
986 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
989 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
996 GST_ERROR_OBJECT (server, "failed to create a client");
1001 GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
1002 g_error_free (error);
1003 g_object_unref (client);
1009 watch_destroyed (GstRTSPServer * server)
1011 GST_DEBUG_OBJECT (server, "source destroyed");
1012 g_object_unref (server);
1016 * gst_rtsp_server_create_source:
1017 * @server: a #GstRTSPServer
1018 * @cancellable: a #GCancellable or %NULL.
1021 * Create a #GSource for @server. The new source will have a default
1022 * #GSocketSourceFunc of gst_rtsp_server_io_func().
1024 * @cancellable if not NULL can be used to cancel the source, which will cause
1025 * the source to trigger, reporting the current condition (which is likely 0
1026 * unless cancellation happened at the same time as a condition change). You can
1027 * check for this in the callback using g_cancellable_is_cancelled().
1029 * Returns: the #GSource for @server or NULL when an error occured. Free with
1033 gst_rtsp_server_create_source (GstRTSPServer * server,
1034 GCancellable * cancellable, GError ** error)
1039 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1041 socket = gst_rtsp_server_create_socket (server, NULL, error);
1044 server->socket = g_object_ref (socket);
1046 /* create a watch for reads (new connections) and possible errors */
1047 source = g_socket_create_source (socket, G_IO_IN |
1048 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
1049 g_object_unref (socket);
1051 /* configure the callback */
1052 g_source_set_callback (source,
1053 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
1054 (GDestroyNotify) watch_destroyed);
1060 GST_ERROR_OBJECT (server, "failed to create socket");
1066 * gst_rtsp_server_attach:
1067 * @server: a #GstRTSPServer
1068 * @context: (allow-none): a #GMainContext
1070 * Attaches @server to @context. When the mainloop for @context is run, the
1071 * server will be dispatched. When @context is NULL, the default context will be
1074 * This function should be called when the server properties and urls are fully
1075 * configured and the server is ready to start.
1077 * Returns: the ID (greater than 0) for the source within the GMainContext.
1080 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1084 GError *error = NULL;
1086 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1088 source = gst_rtsp_server_create_source (server, NULL, &error);
1092 res = g_source_attach (source, context);
1093 g_source_unref (source);
1100 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1101 g_error_free (error);