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);
753 unmanage_client (GstRTSPClient * client, GstRTSPServer * server)
755 GST_DEBUG_OBJECT (server, "unmanage client %p", client);
757 g_object_ref (server);
758 gst_rtsp_client_set_server (client, NULL);
760 GST_RTSP_SERVER_LOCK (server);
761 server->clients = g_list_remove (server->clients, client);
762 GST_RTSP_SERVER_UNLOCK (server);
763 g_object_unref (server);
765 g_object_unref (client);
768 /* add the client to the active list of clients, takes ownership of
771 manage_client (GstRTSPServer * server, GstRTSPClient * client)
773 GST_DEBUG_OBJECT (server, "manage client %p", client);
774 gst_rtsp_client_set_server (client, server);
776 GST_RTSP_SERVER_LOCK (server);
777 g_signal_connect (client, "closed", (GCallback) unmanage_client, server);
778 server->clients = g_list_prepend (server->clients, client);
779 GST_RTSP_SERVER_UNLOCK (server);
782 static GstRTSPClient *
783 default_create_client (GstRTSPServer * server)
785 GstRTSPClient *client;
787 /* a new client connected, create a session to handle the client. */
788 client = gst_rtsp_client_new ();
790 /* set the session pool that this client should use */
791 GST_RTSP_SERVER_LOCK (server);
792 gst_rtsp_client_set_session_pool (client, server->session_pool);
793 /* set the media mapping that this client should use */
794 gst_rtsp_client_set_media_mapping (client, server->media_mapping);
795 /* set authentication manager */
796 gst_rtsp_client_set_auth (client, server->auth);
797 GST_RTSP_SERVER_UNLOCK (server);
802 /* default method for creating a new client object in the server to accept and
803 * handle a client connection on this server */
805 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
806 GSocket * socket, GError ** error)
808 /* accept connections for that client, this function returns after accepting
809 * the connection and will run the remainder of the communication with the
810 * client asyncronously. */
811 if (!gst_rtsp_client_accept (client, socket, NULL, error))
819 GST_ERROR_OBJECT (server,
820 "Could not accept client on server : %s", (*error)->message);
826 * gst_rtsp_server_transfer_connection:
827 * @server: a #GstRTSPServer
828 * @socket: a network socket
829 * @ip: the IP address of the remote client
830 * @port: the port used by the other end
831 * @initial_buffer: any initial data that was already read from the socket
833 * Take an existing network socket and use it for an RTSP connection. This
834 * is used when transferring a socket from an HTTP server which should be used
835 * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
836 * that the HTTP server read from the socket while parsing the HTTP header.
838 * Returns: TRUE if all was ok, FALSE if an error occured.
841 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
842 const gchar * ip, gint port, const gchar * initial_buffer)
844 GstRTSPClient *client = NULL;
845 GstRTSPServerClass *klass;
846 GError *error = NULL;
848 klass = GST_RTSP_SERVER_GET_CLASS (server);
850 if (klass->create_client)
851 client = klass->create_client (server);
855 /* a new client connected, create a client object to handle the client. */
856 if (!gst_rtsp_client_create_from_socket (client, socket, ip, port,
857 initial_buffer, &error)) {
858 goto transfer_failed;
861 /* manage the client connection */
862 manage_client (server, client);
864 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
872 GST_ERROR_OBJECT (server, "failed to create a client");
877 GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
878 g_error_free (error);
879 gst_object_unref (client);
885 * gst_rtsp_server_io_func:
886 * @socket: a #GSocket
887 * @condition: the condition on @source
888 * @server: a #GstRTSPServer
890 * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
891 * new connection on @socket or @server.
893 * Returns: TRUE if the source could be connected, FALSE if an error occured.
896 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
897 GstRTSPServer * server)
899 gboolean result = TRUE;
900 GstRTSPClient *client = NULL;
901 GstRTSPServerClass *klass;
902 GError *error = NULL;
904 if (condition & G_IO_IN) {
905 klass = GST_RTSP_SERVER_GET_CLASS (server);
907 if (klass->create_client)
908 client = klass->create_client (server);
912 /* a new client connected, create a client object to handle the client. */
913 if (klass->accept_client)
914 result = klass->accept_client (server, client, socket, &error);
918 /* manage the client connection */
919 manage_client (server, client);
921 g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
924 GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
931 GST_ERROR_OBJECT (server, "failed to create a client");
936 GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
937 g_error_free (error);
938 gst_object_unref (client);
944 watch_destroyed (GstRTSPServer * server)
946 GST_DEBUG_OBJECT (server, "source destroyed");
947 g_object_unref (server);
951 * gst_rtsp_server_create_source:
952 * @server: a #GstRTSPServer
953 * @cancellable: a #GCancellable or %NULL.
956 * Create a #GSource for @server. The new source will have a default
957 * #GSocketSourceFunc of gst_rtsp_server_io_func().
959 * @cancellable if not NULL can be used to cancel the source, which will cause
960 * the source to trigger, reporting the current condition (which is likely 0
961 * unless cancellation happened at the same time as a condition change). You can
962 * check for this in the callback using g_cancellable_is_cancelled().
964 * Returns: the #GSource for @server or NULL when an error occured. Free with
968 gst_rtsp_server_create_source (GstRTSPServer * server,
969 GCancellable * cancellable, GError ** error)
974 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
976 socket = gst_rtsp_server_create_socket (server, NULL, error);
979 server->socket = g_object_ref (socket);
981 /* create a watch for reads (new connections) and possible errors */
982 source = g_socket_create_source (socket, G_IO_IN |
983 G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
984 g_object_unref (socket);
986 /* configure the callback */
987 g_source_set_callback (source,
988 (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
989 (GDestroyNotify) watch_destroyed);
995 GST_ERROR_OBJECT (server, "failed to create socket");
1001 * gst_rtsp_server_attach:
1002 * @server: a #GstRTSPServer
1003 * @context: (allow-none): a #GMainContext
1005 * Attaches @server to @context. When the mainloop for @context is run, the
1006 * server will be dispatched. When @context is NULL, the default context will be
1009 * This function should be called when the server properties and urls are fully
1010 * configured and the server is ready to start.
1012 * Returns: the ID (greater than 0) for the source within the GMainContext.
1015 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1019 GError *error = NULL;
1021 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1023 source = gst_rtsp_server_create_source (server, NULL, &error);
1027 res = g_source_attach (source, context);
1028 g_source_unref (source);
1035 GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1036 g_error_free (error);