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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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 (GstRTSPServer * server)
261 GSocketAddress *address;
264 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
266 GST_RTSP_SERVER_LOCK (server);
267 if (server->socket == NULL)
270 address = g_socket_get_local_address (server->socket, NULL);
271 result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
272 g_object_unref (address);
275 GST_RTSP_SERVER_UNLOCK (server);
281 * gst_rtsp_server_set_service:
282 * @server: a #GstRTSPServer
283 * @service: the service
285 * Configure @server to accept connections on the given service.
286 * @service should be a string containing the service name (see services(5)) or
287 * a string containing a port number between 1 and 65535.
289 * This function must be called before the server is bound.
292 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
294 g_return_if_fail (GST_IS_RTSP_SERVER (server));
295 g_return_if_fail (service != NULL);
297 GST_RTSP_SERVER_LOCK (server);
298 g_free (server->service);
299 server->service = g_strdup (service);
300 GST_RTSP_SERVER_UNLOCK (server);
304 * gst_rtsp_server_get_service:
305 * @server: a #GstRTSPServer
307 * Get the service on which the server will accept connections.
309 * Returns: the service. use g_free() after usage.
312 gst_rtsp_server_get_service (GstRTSPServer * server)
316 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
318 GST_RTSP_SERVER_LOCK (server);
319 result = g_strdup (server->service);
320 GST_RTSP_SERVER_UNLOCK (server);
326 * gst_rtsp_server_set_backlog:
327 * @server: a #GstRTSPServer
328 * @backlog: the backlog
330 * configure the maximum amount of requests that may be queued for the
333 * This function must be called before the server is bound.
336 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
338 g_return_if_fail (GST_IS_RTSP_SERVER (server));
340 GST_RTSP_SERVER_LOCK (server);
341 server->backlog = backlog;
342 GST_RTSP_SERVER_UNLOCK (server);
346 * gst_rtsp_server_get_backlog:
347 * @server: a #GstRTSPServer
349 * The maximum amount of queued requests for the server.
351 * Returns: the server backlog.
354 gst_rtsp_server_get_backlog (GstRTSPServer * server)
358 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
360 GST_RTSP_SERVER_LOCK (server);
361 result = server->backlog;
362 GST_RTSP_SERVER_UNLOCK (server);
368 * gst_rtsp_server_set_session_pool:
369 * @server: a #GstRTSPServer
370 * @pool: a #GstRTSPSessionPool
372 * configure @pool to be used as the session pool of @server.
375 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
376 GstRTSPSessionPool * pool)
378 GstRTSPSessionPool *old;
380 g_return_if_fail (GST_IS_RTSP_SERVER (server));
385 GST_RTSP_SERVER_LOCK (server);
386 old = server->session_pool;
387 server->session_pool = pool;
388 GST_RTSP_SERVER_UNLOCK (server);
391 g_object_unref (old);
395 * gst_rtsp_server_get_session_pool:
396 * @server: a #GstRTSPServer
398 * Get the #GstRTSPSessionPool used as the session pool of @server.
400 * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
404 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
406 GstRTSPSessionPool *result;
408 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
410 GST_RTSP_SERVER_LOCK (server);
411 if ((result = server->session_pool))
412 g_object_ref (result);
413 GST_RTSP_SERVER_UNLOCK (server);
419 * gst_rtsp_server_set_media_mapping:
420 * @server: a #GstRTSPServer
421 * @mapping: a #GstRTSPMediaMapping
423 * configure @mapping to be used as the media mapping of @server.
426 gst_rtsp_server_set_media_mapping (GstRTSPServer * server,
427 GstRTSPMediaMapping * mapping)
429 GstRTSPMediaMapping *old;
431 g_return_if_fail (GST_IS_RTSP_SERVER (server));
434 g_object_ref (mapping);
436 GST_RTSP_SERVER_LOCK (server);
437 old = server->media_mapping;
438 server->media_mapping = mapping;
439 GST_RTSP_SERVER_UNLOCK (server);
442 g_object_unref (old);
447 * gst_rtsp_server_get_media_mapping:
448 * @server: a #GstRTSPServer
450 * Get the #GstRTSPMediaMapping used as the media mapping of @server.
452 * Returns: (transfer full): the #GstRTSPMediaMapping of @server. g_object_unref() after
455 GstRTSPMediaMapping *
456 gst_rtsp_server_get_media_mapping (GstRTSPServer * server)
458 GstRTSPMediaMapping *result;
460 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
462 GST_RTSP_SERVER_LOCK (server);
463 if ((result = server->media_mapping))
464 g_object_ref (result);
465 GST_RTSP_SERVER_UNLOCK (server);
471 * gst_rtsp_server_set_auth:
472 * @server: a #GstRTSPServer
473 * @auth: a #GstRTSPAuth
475 * configure @auth to be used as the authentication manager of @server.
478 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
482 g_return_if_fail (GST_IS_RTSP_SERVER (server));
487 GST_RTSP_SERVER_LOCK (server);
490 GST_RTSP_SERVER_UNLOCK (server);
493 g_object_unref (old);
498 * gst_rtsp_server_get_auth:
499 * @server: a #GstRTSPServer
501 * Get the #GstRTSPAuth used as the authentication manager of @server.
503 * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
507 gst_rtsp_server_get_auth (GstRTSPServer * server)
511 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
513 GST_RTSP_SERVER_LOCK (server);
514 if ((result = server->auth))
515 g_object_ref (result);
516 GST_RTSP_SERVER_UNLOCK (server);
522 gst_rtsp_server_get_property (GObject * object, guint propid,
523 GValue * value, GParamSpec * pspec)
525 GstRTSPServer *server = GST_RTSP_SERVER (object);
529 g_value_take_string (value, gst_rtsp_server_get_address (server));
532 g_value_take_string (value, gst_rtsp_server_get_service (server));
534 case PROP_BOUND_PORT:
535 g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
538 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
540 case PROP_SESSION_POOL:
541 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
543 case PROP_MEDIA_MAPPING:
544 g_value_take_object (value, gst_rtsp_server_get_media_mapping (server));
547 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
552 gst_rtsp_server_set_property (GObject * object, guint propid,
553 const GValue * value, GParamSpec * pspec)
555 GstRTSPServer *server = GST_RTSP_SERVER (object);
559 gst_rtsp_server_set_address (server, g_value_get_string (value));
562 gst_rtsp_server_set_service (server, g_value_get_string (value));
565 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
567 case PROP_SESSION_POOL:
568 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
570 case PROP_MEDIA_MAPPING:
571 gst_rtsp_server_set_media_mapping (server, g_value_get_object (value));
574 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
579 * gst_rtsp_server_create_socket:
580 * @server: a #GstRTSPServer
581 * @cancellable: a #GCancellable
584 * Create a #GSocket for @server. The socket will listen on the
585 * configured service.
587 * Returns: (transfer full): the #GSocket for @server or NULL when an error occured.
590 gst_rtsp_server_create_socket (GstRTSPServer * server,
591 GCancellable * cancellable, GError ** error)
593 GSocketConnectable *conn;
594 GSocketAddressEnumerator *enumerator;
595 GSocket *socket = NULL;
597 struct linger linger;
599 GError *sock_error = NULL;
600 GError *bind_error = NULL;
603 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
605 GST_RTSP_SERVER_LOCK (server);
606 GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
609 /* resolve the server IP address */
610 port = atoi (server->service);
611 if (port != 0 || !strcmp (server->service, "0"))
612 conn = g_network_address_new (server->address, port);
614 conn = g_network_service_new (server->service, "tcp", server->address);
616 enumerator = g_socket_connectable_enumerate (conn);
617 g_object_unref (conn);
619 /* create server socket, we loop through all the addresses until we manage to
620 * create a socket and bind. */
622 GSocketAddress *sockaddr;
625 g_socket_address_enumerator_next (enumerator, cancellable, error);
628 GST_DEBUG_OBJECT (server, "no more addresses %s",
629 *error ? (*error)->message : "");
631 GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
636 /* only keep the first error */
637 socket = g_socket_new (g_socket_address_get_family (sockaddr),
638 G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
639 sock_error ? NULL : &sock_error);
641 if (socket == NULL) {
642 GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
643 sock_error->message);
647 if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
648 g_object_unref (sockaddr);
652 GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
653 bind_error->message);
654 g_object_unref (sockaddr);
655 g_object_unref (socket);
658 g_object_unref (enumerator);
663 g_clear_error (&sock_error);
664 g_clear_error (&bind_error);
666 GST_DEBUG_OBJECT (server, "opened sending server socket");
668 /* keep connection alive; avoids SIGPIPE during write */
669 g_socket_set_keepalive (socket, TRUE);
673 /* make sure socket is reset 5 seconds after close. This ensure that we can
674 * reuse the socket quickly while still having a chance to send data to the
678 if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
679 (void *) &linger, sizeof (linger)) < 0)
684 /* set the server socket to nonblocking */
685 g_socket_set_blocking (socket, FALSE);
687 /* set listen backlog */
688 g_socket_set_listen_backlog (socket, server->backlog);
690 if (!g_socket_listen (socket, error))
693 GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
694 socket, server->backlog);
696 GST_RTSP_SERVER_UNLOCK (server);
703 GST_ERROR_OBJECT (server, "failed to create socket");
710 GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
718 GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
725 g_object_unref (socket);
729 g_propagate_error (error, sock_error);
731 g_error_free (sock_error);
734 if ((error == NULL) || (*error == NULL))
735 g_propagate_error (error, bind_error);
737 g_error_free (bind_error);
739 GST_RTSP_SERVER_UNLOCK (server);
745 unmanage_client (GstRTSPClient * client, GstRTSPServer * server)
747 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
749 g_object_ref (server);
750 gst_rtsp_client_set_server (client, NULL);
752 GST_RTSP_SERVER_LOCK (server);
753 server->clients = g_list_remove (server->clients, client);
754 GST_RTSP_SERVER_UNLOCK (server);
755 g_object_unref (server);
757 g_object_unref (client);
760 /* add the client to the active list of clients, takes ownership of
763 manage_client (GstRTSPServer * server, GstRTSPClient * client)
765 GST_DEBUG_OBJECT (server, "manage client %p", client);
766 gst_rtsp_client_set_server (client, server);
768 GST_RTSP_SERVER_LOCK (server);
769 g_signal_connect (client, "closed", (GCallback) unmanage_client, server);
770 server->clients = g_list_prepend (server->clients, client);
771 GST_RTSP_SERVER_UNLOCK (server);
774 static GstRTSPClient *
775 default_create_client (GstRTSPServer * server)
777 GstRTSPClient *client;
779 /* a new client connected, create a session to handle the client. */
780 client = gst_rtsp_client_new ();
782 /* set the session pool that this client should use */
783 GST_RTSP_SERVER_LOCK (server);
784 gst_rtsp_client_set_session_pool (client, server->session_pool);
785 /* set the media mapping that this client should use */
786 gst_rtsp_client_set_media_mapping (client, server->media_mapping);
787 /* set authentication manager */
788 gst_rtsp_client_set_auth (client, server->auth);
789 GST_RTSP_SERVER_UNLOCK (server);
794 /* default method for creating a new client object in the server to accept and
795 * handle a client connection on this server */
797 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
798 GSocket * socket, GError ** error)
800 /* accept connections for that client, this function returns after accepting
801 * the connection and will run the remainder of the communication with the
802 * client asyncronously. */
803 if (!gst_rtsp_client_accept (client, socket, NULL, error))
811 GST_ERROR_OBJECT (server,
812 "Could not accept client on server : %s", (*error)->message);
818 * gst_rtsp_server_transfer_connection:
819 * @server: a #GstRTSPServer
820 * @socket: a network socket
821 * @ip: the IP address of the remote client
822 * @port: the port used by the other end
823 * @initial_buffer: any initial data that was already read from the socket
825 * Take an existing network socket and use it for an RTSP connection. This
826 * is used when transferring a socket from an HTTP server which should be used
827 * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
828 * that the HTTP server read from the socket while parsing the HTTP header.
830 * Returns: TRUE if all was ok, FALSE if an error occured.
833 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
834 const gchar * ip, gint port, const gchar * initial_buffer)
836 GstRTSPClient *client = NULL;
837 GstRTSPServerClass *klass;
838 GError *error = NULL;
840 klass = GST_RTSP_SERVER_GET_CLASS (server);
842 if (klass->create_client)
843 client = klass->create_client (server);
847 /* a new client connected, create a client object to handle the client. */
848 if (!gst_rtsp_client_create_from_socket (client, socket, ip, port,
849 initial_buffer, &error)) {
850 goto transfer_failed;
853 /* manage the client connection */
854 manage_client (server, client);
856 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
864 GST_ERROR_OBJECT (server, "failed to create a client");
869 GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
870 g_error_free (error);
871 gst_object_unref (client);
877 * gst_rtsp_server_io_func:
878 * @socket: a #GSocket
879 * @condition: the condition on @source
881 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
882 * new connection on @socket or @server.
884 * Returns: TRUE if the source could be connected, FALSE if an error occured.
887 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
888 GstRTSPServer * server)
890 gboolean result = TRUE;
891 GstRTSPClient *client = NULL;
892 GstRTSPServerClass *klass;
893 GError *error = NULL;
895 if (condition & G_IO_IN) {
896 klass = GST_RTSP_SERVER_GET_CLASS (server);
898 if (klass->create_client)
899 client = klass->create_client (server);
903 /* a new client connected, create a client object to handle the client. */
904 if (klass->accept_client)
905 result = klass->accept_client (server, client, socket, &error);
909 /* manage the client connection */
910 manage_client (server, client);
912 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
915 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
922 GST_ERROR_OBJECT (server, "failed to create a client");
927 GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
928 g_error_free (error);
929 gst_object_unref (client);
935 watch_destroyed (GstRTSPServer * server)
937 GST_DEBUG_OBJECT (server, "source destroyed");
938 g_object_unref (server);
942 * gst_rtsp_server_create_source:
943 * @server: a #GstRTSPServer
944 * @cancellable: a #GCancellable or %NULL.
947 * Create a #GSource for @server. The new source will have a default
948 * #GSocketSourceFunc of gst_rtsp_server_io_func().
950 * @cancellable if not NULL can be used to cancel the source, which will cause
951 * the source to trigger, reporting the current condition (which is likely 0
952 * unless cancellation happened at the same time as a condition change). You can
953 * check for this in the callback using g_cancellable_is_cancelled().
955 * Returns: the #GSource for @server or NULL when an error occured. Free with
959 gst_rtsp_server_create_source (GstRTSPServer * server,
960 GCancellable * cancellable, GError ** error)
965 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
967 socket = gst_rtsp_server_create_socket (server, NULL, error);
970 server->socket = g_object_ref (socket);
972 /* create a watch for reads (new connections) and possible errors */
973 source = g_socket_create_source (socket, G_IO_IN |
974 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
975 g_object_unref (socket);
977 /* configure the callback */
978 g_source_set_callback (source,
979 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
980 (GDestroyNotify) watch_destroyed);
986 GST_ERROR_OBJECT (server, "failed to create socket");
992 * gst_rtsp_server_attach:
993 * @server: a #GstRTSPServer
994 * @context: (allow-none): a #GMainContext
996 * Attaches @server to @context. When the mainloop for @context is run, the
997 * server will be dispatched. When @context is NULL, the default context will be
1000 * This function should be called when the server properties and urls are fully
1001 * configured and the server is ready to start.
1003 * Returns: the ID (greater than 0) for the source within the GMainContext.
1006 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1010 GError *error = NULL;
1012 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1014 source = gst_rtsp_server_create_source (server, NULL, &error);
1018 res = g_source_attach (source, context);
1019 g_source_unref (source);
1026 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1027 g_error_free (error);