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.
20 #include <sys/ioctl.h>
22 #include "rtsp-server.h"
23 #include "rtsp-client.h"
25 #define DEFAULT_BACKLOG 5
26 #define DEFAULT_PORT 1554
37 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
39 static void gst_rtsp_server_get_property (GObject *object, guint propid,
40 GValue *value, GParamSpec *pspec);
41 static void gst_rtsp_server_set_property (GObject *object, guint propid,
42 const GValue *value, GParamSpec *pspec);
45 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
47 GObjectClass *gobject_class;
49 gobject_class = G_OBJECT_CLASS (klass);
51 gobject_class->get_property = gst_rtsp_server_get_property;
52 gobject_class->set_property = gst_rtsp_server_set_property;
55 * GstRTSPServer::backlog
57 * The backlog argument defines the maximum length to which the queue of
58 * pending connections for the server may grow. If a connection request arrives
59 * when the queue is full, the client may receive an error with an indication of
60 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
61 * request may be ignored so that a later reattempt at connection succeeds.
63 g_object_class_install_property (gobject_class, PROP_BACKLOG,
64 g_param_spec_int ("backlog", "Backlog", "The maximum length to which the queue "
65 "of pending connections may grow",
66 0, G_MAXINT, DEFAULT_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
70 * The session port of the server. This is the port where the server will
73 g_object_class_install_property (gobject_class, PROP_PORT,
74 g_param_spec_int ("port", "Port", "The port the server uses to listen on",
75 1, 65535, DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
79 * The session pool of the server. By default each server has a separate
80 * session pool but sessions can be shared between servers by setting the same
81 * session pool on multiple servers.
83 g_object_class_install_property (gobject_class, PROP_POOL,
84 g_param_spec_object ("pool", "Pool", "The session pool to use for client session",
85 GST_TYPE_RTSP_SESSION_POOL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
89 gst_rtsp_server_init (GstRTSPServer * server)
91 server->server_port = DEFAULT_PORT;
92 server->backlog = DEFAULT_BACKLOG;
93 server->pool = gst_rtsp_session_pool_new ();
97 * gst_rtsp_server_new:
99 * Create a new #GstRTSPServer instance.
102 gst_rtsp_server_new (void)
104 GstRTSPServer *result;
106 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
112 * gst_rtsp_server_set_port:
113 * @server: a #GstRTSPServer
116 * Configure @server to accept connections on the given port.
117 * @port should be a port number between 1 and 65535.
119 * This function must be called before the server is bound.
122 gst_rtsp_server_set_port (GstRTSPServer *server, gint port)
124 g_return_if_fail (GST_IS_RTSP_SERVER (server));
125 g_return_if_fail (port >= 1 && port <= 65535);
127 server->server_port = port;
131 * gst_rtsp_server_get_port:
132 * @server: a #GstRTSPServer
134 * Get the port number on which the server will accept connections.
136 * Returns: the server port.
139 gst_rtsp_server_get_port (GstRTSPServer *server)
141 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
143 return server->server_port;
147 * gst_rtsp_server_set_backlog:
148 * @server: a #GstRTSPServer
149 * @backlog: the backlog
151 * configure the maximum amount of requests that may be queued for the
154 * This function must be called before the server is bound.
157 gst_rtsp_server_set_backlog (GstRTSPServer *server, gint backlog)
159 g_return_if_fail (GST_IS_RTSP_SERVER (server));
161 server->backlog = backlog;
165 * gst_rtsp_server_get_backlog:
166 * @server: a #GstRTSPServer
168 * The maximum amount of queued requests for the server.
170 * Returns: the server backlog.
173 gst_rtsp_server_get_backlog (GstRTSPServer *server)
175 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
177 return server->backlog;
181 * gst_rtsp_server_set_session_pool:
182 * @server: a #GstRTSPServer
183 * @pool: a #GstRTSPSessionPool
185 * configure @pool to be used as the session pool of @server.
188 gst_rtsp_server_set_session_pool (GstRTSPServer *server, GstRTSPSessionPool *pool)
190 g_return_if_fail (GST_IS_RTSP_SERVER (server));
193 g_object_unref (server->pool);
195 pool = g_object_ref (pool);
201 * gst_rtsp_server_get_session_pool:
202 * @server: a #GstRTSPServer
204 * Get the #GstRTSPSessionPool used as the session pool of @server.
206 * Returns: the #GstRTSPSessionPool used for sessions. g_object_unref() after
210 gst_rtsp_server_get_session_pool (GstRTSPServer *server)
212 GstRTSPSessionPool *result;
214 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
216 if ((result = server->pool))
217 g_object_ref (result);
223 gst_rtsp_server_get_property (GObject *object, guint propid,
224 GValue *value, GParamSpec *pspec)
226 GstRTSPServer *server = GST_RTSP_SERVER (object);
230 g_value_set_int (value, gst_rtsp_server_get_port (server));
233 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
236 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
244 gst_rtsp_server_set_property (GObject *object, guint propid,
245 const GValue *value, GParamSpec *pspec)
247 GstRTSPServer *server = GST_RTSP_SERVER (object);
251 gst_rtsp_server_set_port (server, g_value_get_int (value));
254 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
257 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
260 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
264 /* Prepare a server socket for @server and make it listen on the configured port */
266 gst_rtsp_server_sink_init_send (GstRTSPServer * server)
270 /* create server socket */
271 if ((server->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
274 GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d",
275 server->server_sock.fd);
277 /* make address reusable */
279 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
280 (void *) &ret, sizeof (ret)) < 0)
283 /* keep connection alive; avoids SIGPIPE during write */
285 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
286 (void *) &ret, sizeof (ret)) < 0)
287 goto keepalive_failed;
289 /* name the socket */
290 memset (&server->server_sin, 0, sizeof (server->server_sin));
291 server->server_sin.sin_family = AF_INET; /* network socket */
292 server->server_sin.sin_port = htons (server->server_port); /* on port */
293 server->server_sin.sin_addr.s_addr = htonl (INADDR_ANY); /* for hosts */
296 GST_DEBUG_OBJECT (server, "binding server socket to address");
297 ret = bind (server->server_sock.fd, (struct sockaddr *) &server->server_sin,
298 sizeof (server->server_sin));
302 /* set the server socket to nonblocking */
303 fcntl (server->server_sock.fd, F_SETFL, O_NONBLOCK);
305 GST_DEBUG_OBJECT (server, "listening on server socket %d with queue of %d",
306 server->server_sock.fd, server->backlog);
307 if (listen (server->server_sock.fd, server->backlog) == -1)
310 GST_DEBUG_OBJECT (server,
311 "listened on server socket %d, returning from connection setup",
312 server->server_sock.fd);
319 GST_ERROR_OBJECT (server, "failed to create socket: %s", g_strerror (errno));
324 if (server->server_sock.fd >= 0) {
325 close (server->server_sock.fd);
326 server->server_sock.fd = -1;
328 GST_ERROR_OBJECT (server, "failed to reuse socket: %s", g_strerror (errno));
333 if (server->server_sock.fd >= 0) {
334 close (server->server_sock.fd);
335 server->server_sock.fd = -1;
337 GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s", g_strerror (errno));
342 if (server->server_sock.fd >= 0) {
343 close (server->server_sock.fd);
344 server->server_sock.fd = -1;
346 GST_ERROR_OBJECT (server, "failed to listen on socket: %s", g_strerror (errno));
351 if (server->server_sock.fd >= 0) {
352 close (server->server_sock.fd);
353 server->server_sock.fd = -1;
355 GST_ERROR_OBJECT (server, "failed to bind on socket: %s", g_strerror (errno));
361 * gst_rtsp_server_io_func:
362 * @channel: a #GIOChannel
363 * @condition: the condition on @source
365 * A default #GIOFunc that creates a new #GstRTSPClient to accept and handle a
366 * new connection on @channel or @server.
368 * Returns: TRUE if the source could be connected, FALSE if an error occured.
371 gst_rtsp_server_io_func (GIOChannel *channel, GIOCondition condition, GstRTSPServer *server)
373 GstRTSPClient *client;
375 if (condition & G_IO_IN) {
376 /* a new client connected, create a session to handle the client. */
377 client = gst_rtsp_client_new ();
379 /* set the session pool that this client should use */
380 gst_rtsp_client_set_session_pool (client, server->pool);
382 /* accept connections for that client, this function returns after accepting
383 * the connection and will run the remainder of the communication with the
384 * client asyncronously. */
385 if (!gst_rtsp_client_accept (client, channel))
388 /* can unref the client now, when the request is finished, it will be
390 gst_object_unref (client);
393 g_print ("received unknown event %08x", condition);
400 g_error ("Could not accept client on server socket %d: %s (%d)",
401 server->server_sock.fd, g_strerror (errno), errno);
402 gst_object_unref (client);
408 * gst_rtsp_server_get_io_channel:
409 * @server: a #GstRTSPServer
411 * Create a #GIOChannel for @server.
413 * Returns: the GIOChannel for @server or NULL when an error occured.
416 gst_rtsp_server_get_io_channel (GstRTSPServer *server)
418 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
420 if (server->io_channel == NULL) {
421 if (!gst_rtsp_server_sink_init_send (server))
424 /* create IO channel for the socket */
425 server->io_channel = g_io_channel_unix_new (server->server_sock.fd);
427 return server->io_channel;
436 * gst_rtsp_server_create_watch:
437 * @server: a #GstRTSPServer
439 * Create a #GSource for @server. The new source will have a default
440 * #GIOFunc of gst_rtsp_server_io_func().
442 * Returns: the #GSource for @server or NULL when an error occured.
445 gst_rtsp_server_create_watch (GstRTSPServer *server)
447 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
449 if (server->io_watch == NULL) {
452 channel = gst_rtsp_server_get_io_channel (server);
456 /* create a watch for reads (new connections) and possible errors */
457 server->io_watch = g_io_create_watch (channel, G_IO_IN |
458 G_IO_ERR | G_IO_HUP | G_IO_NVAL);
460 /* configure the callback */
461 g_source_set_callback (server->io_watch, (GSourceFunc) gst_rtsp_server_io_func, server, NULL);
463 return server->io_watch;
472 * gst_rtsp_server_attach:
473 * @server: a #GstRTSPServer
474 * @context: a #GMainContext
476 * Attaches @server to @context. When the mainloop for @context is run, the
477 * server will be dispatched.
479 * This function should be called when the server properties and urls are fully
480 * configured and the server is ready to start.
482 * Returns: the ID (greater than 0) for the source within the GMainContext.
485 gst_rtsp_server_attach (GstRTSPServer *server, GMainContext *context)
490 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
492 source = gst_rtsp_server_create_watch (server);
496 res = g_source_attach (source, context);