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);
44 static GstRTSPClient * gst_rtsp_server_accept_client (GstRTSPServer *server,
48 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
50 GObjectClass *gobject_class;
52 gobject_class = G_OBJECT_CLASS (klass);
54 gobject_class->get_property = gst_rtsp_server_get_property;
55 gobject_class->set_property = gst_rtsp_server_set_property;
58 * GstRTSPServer::backlog
60 * The backlog argument defines the maximum length to which the queue of
61 * pending connections for the server may grow. If a connection request arrives
62 * when the queue is full, the client may receive an error with an indication of
63 * ECONNREFUSED or, if the underlying protocol supports retransmission, the
64 * request may be ignored so that a later reattempt at connection succeeds.
66 g_object_class_install_property (gobject_class, PROP_BACKLOG,
67 g_param_spec_int ("backlog", "Backlog", "The maximum length to which the queue "
68 "of pending connections may grow",
69 0, G_MAXINT, DEFAULT_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
73 * The session port of the server. This is the port where the server will
76 g_object_class_install_property (gobject_class, PROP_PORT,
77 g_param_spec_int ("port", "Port", "The port the server uses to listen on",
78 1, 65535, DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
82 * The session pool of the server. By default each server has a separate
83 * session pool but sessions can be shared between servers by setting the same
84 * session pool on multiple servers.
86 g_object_class_install_property (gobject_class, PROP_POOL,
87 g_param_spec_object ("pool", "Pool", "The session pool to use for client session",
88 GST_TYPE_RTSP_SESSION_POOL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
90 klass->accept_client = gst_rtsp_server_accept_client;
94 gst_rtsp_server_init (GstRTSPServer * server)
96 server->server_port = DEFAULT_PORT;
97 server->backlog = DEFAULT_BACKLOG;
98 server->pool = gst_rtsp_session_pool_new ();
102 * gst_rtsp_server_new:
104 * Create a new #GstRTSPServer instance.
107 gst_rtsp_server_new (void)
109 GstRTSPServer *result;
111 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
117 * gst_rtsp_server_set_port:
118 * @server: a #GstRTSPServer
121 * Configure @server to accept connections on the given port.
122 * @port should be a port number between 1 and 65535.
124 * This function must be called before the server is bound.
127 gst_rtsp_server_set_port (GstRTSPServer *server, gint port)
129 g_return_if_fail (GST_IS_RTSP_SERVER (server));
130 g_return_if_fail (port >= 1 && port <= 65535);
132 server->server_port = port;
136 * gst_rtsp_server_get_port:
137 * @server: a #GstRTSPServer
139 * Get the port number on which the server will accept connections.
141 * Returns: the server port.
144 gst_rtsp_server_get_port (GstRTSPServer *server)
146 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
148 return server->server_port;
152 * gst_rtsp_server_set_backlog:
153 * @server: a #GstRTSPServer
154 * @backlog: the backlog
156 * configure the maximum amount of requests that may be queued for the
159 * This function must be called before the server is bound.
162 gst_rtsp_server_set_backlog (GstRTSPServer *server, gint backlog)
164 g_return_if_fail (GST_IS_RTSP_SERVER (server));
166 server->backlog = backlog;
170 * gst_rtsp_server_get_backlog:
171 * @server: a #GstRTSPServer
173 * The maximum amount of queued requests for the server.
175 * Returns: the server backlog.
178 gst_rtsp_server_get_backlog (GstRTSPServer *server)
180 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
182 return server->backlog;
186 * gst_rtsp_server_set_session_pool:
187 * @server: a #GstRTSPServer
188 * @pool: a #GstRTSPSessionPool
190 * configure @pool to be used as the session pool of @server.
193 gst_rtsp_server_set_session_pool (GstRTSPServer *server, GstRTSPSessionPool *pool)
195 g_return_if_fail (GST_IS_RTSP_SERVER (server));
198 g_object_unref (server->pool);
200 pool = g_object_ref (pool);
206 * gst_rtsp_server_get_session_pool:
207 * @server: a #GstRTSPServer
209 * Get the #GstRTSPSessionPool used as the session pool of @server.
211 * Returns: the #GstRTSPSessionPool used for sessions. g_object_unref() after
215 gst_rtsp_server_get_session_pool (GstRTSPServer *server)
217 GstRTSPSessionPool *result;
219 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
221 if ((result = server->pool))
222 g_object_ref (result);
228 gst_rtsp_server_get_property (GObject *object, guint propid,
229 GValue *value, GParamSpec *pspec)
231 GstRTSPServer *server = GST_RTSP_SERVER (object);
235 g_value_set_int (value, gst_rtsp_server_get_port (server));
238 g_value_set_int (value, gst_rtsp_server_get_backlog (server));
241 g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
249 gst_rtsp_server_set_property (GObject *object, guint propid,
250 const GValue *value, GParamSpec *pspec)
252 GstRTSPServer *server = GST_RTSP_SERVER (object);
256 gst_rtsp_server_set_port (server, g_value_get_int (value));
259 gst_rtsp_server_set_backlog (server, g_value_get_int (value));
262 gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
265 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
269 /* Prepare a server socket for @server and make it listen on the configured port */
271 gst_rtsp_server_sink_init_send (GstRTSPServer * server)
275 /* create server socket */
276 if ((server->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
279 GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d",
280 server->server_sock.fd);
282 /* make address reusable */
284 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
285 (void *) &ret, sizeof (ret)) < 0)
288 /* keep connection alive; avoids SIGPIPE during write */
290 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
291 (void *) &ret, sizeof (ret)) < 0)
292 goto keepalive_failed;
294 /* name the socket */
295 memset (&server->server_sin, 0, sizeof (server->server_sin));
296 server->server_sin.sin_family = AF_INET; /* network socket */
297 server->server_sin.sin_port = htons (server->server_port); /* on port */
298 server->server_sin.sin_addr.s_addr = htonl (INADDR_ANY); /* for hosts */
301 GST_DEBUG_OBJECT (server, "binding server socket to address");
302 ret = bind (server->server_sock.fd, (struct sockaddr *) &server->server_sin,
303 sizeof (server->server_sin));
307 /* set the server socket to nonblocking */
308 fcntl (server->server_sock.fd, F_SETFL, O_NONBLOCK);
310 GST_DEBUG_OBJECT (server, "listening on server socket %d with queue of %d",
311 server->server_sock.fd, server->backlog);
312 if (listen (server->server_sock.fd, server->backlog) == -1)
315 GST_DEBUG_OBJECT (server,
316 "listened on server socket %d, returning from connection setup",
317 server->server_sock.fd);
324 GST_ERROR_OBJECT (server, "failed to create socket: %s", g_strerror (errno));
329 if (server->server_sock.fd >= 0) {
330 close (server->server_sock.fd);
331 server->server_sock.fd = -1;
333 GST_ERROR_OBJECT (server, "failed to reuse socket: %s", g_strerror (errno));
338 if (server->server_sock.fd >= 0) {
339 close (server->server_sock.fd);
340 server->server_sock.fd = -1;
342 GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s", g_strerror (errno));
347 if (server->server_sock.fd >= 0) {
348 close (server->server_sock.fd);
349 server->server_sock.fd = -1;
351 GST_ERROR_OBJECT (server, "failed to listen on socket: %s", g_strerror (errno));
356 if (server->server_sock.fd >= 0) {
357 close (server->server_sock.fd);
358 server->server_sock.fd = -1;
360 GST_ERROR_OBJECT (server, "failed to bind on socket: %s", g_strerror (errno));
365 /* default method for creating a new client object in the server to accept and
366 * handle a client connection on this server */
367 static GstRTSPClient *
368 gst_rtsp_server_accept_client (GstRTSPServer *server, GIOChannel *channel)
370 GstRTSPClient *client;
372 /* a new client connected, create a session to handle the client. */
373 client = gst_rtsp_client_new ();
375 /* set the session pool that this client should use */
376 gst_rtsp_client_set_session_pool (client, server->pool);
378 /* accept connections for that client, this function returns after accepting
379 * the connection and will run the remainder of the communication with the
380 * client asyncronously. */
381 if (!gst_rtsp_client_accept (client, channel))
389 g_error ("Could not accept client on server socket %d: %s (%d)",
390 server->server_sock.fd, g_strerror (errno), errno);
391 gst_object_unref (client);
397 * gst_rtsp_server_io_func:
398 * @channel: a #GIOChannel
399 * @condition: the condition on @source
401 * A default #GIOFunc that creates a new #GstRTSPClient to accept and handle a
402 * new connection on @channel or @server.
404 * Returns: TRUE if the source could be connected, FALSE if an error occured.
407 gst_rtsp_server_io_func (GIOChannel *channel, GIOCondition condition, GstRTSPServer *server)
409 GstRTSPClient *client = NULL;
410 GstRTSPServerClass *klass;
412 if (condition & G_IO_IN) {
413 klass = GST_RTSP_SERVER_GET_CLASS (server);
415 /* a new client connected, create a client object to handle the client. */
416 if (klass->accept_client)
417 client = klass->accept_client (server, channel);
421 /* can unref the client now, when the request is finished, it will be
423 gst_object_unref (client);
426 g_print ("received unknown event %08x", condition);
433 GST_ERROR_OBJECT (server, "failed to create a client");
439 * gst_rtsp_server_get_io_channel:
440 * @server: a #GstRTSPServer
442 * Create a #GIOChannel for @server.
444 * Returns: the GIOChannel for @server or NULL when an error occured.
447 gst_rtsp_server_get_io_channel (GstRTSPServer *server)
449 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
451 if (server->io_channel == NULL) {
452 if (!gst_rtsp_server_sink_init_send (server))
455 /* create IO channel for the socket */
456 server->io_channel = g_io_channel_unix_new (server->server_sock.fd);
458 return server->io_channel;
467 * gst_rtsp_server_create_watch:
468 * @server: a #GstRTSPServer
470 * Create a #GSource for @server. The new source will have a default
471 * #GIOFunc of gst_rtsp_server_io_func().
473 * Returns: the #GSource for @server or NULL when an error occured.
476 gst_rtsp_server_create_watch (GstRTSPServer *server)
478 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
480 if (server->io_watch == NULL) {
483 channel = gst_rtsp_server_get_io_channel (server);
487 /* create a watch for reads (new connections) and possible errors */
488 server->io_watch = g_io_create_watch (channel, G_IO_IN |
489 G_IO_ERR | G_IO_HUP | G_IO_NVAL);
491 /* configure the callback */
492 g_source_set_callback (server->io_watch, (GSourceFunc) gst_rtsp_server_io_func, server, NULL);
494 return server->io_watch;
503 * gst_rtsp_server_attach:
504 * @server: a #GstRTSPServer
505 * @context: a #GMainContext
507 * Attaches @server to @context. When the mainloop for @context is run, the
508 * server will be dispatched.
510 * This function should be called when the server properties and urls are fully
511 * configured and the server is ready to start.
513 * Returns: the ID (greater than 0) for the source within the GMainContext.
516 gst_rtsp_server_attach (GstRTSPServer *server, GMainContext *context)
521 g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
523 source = gst_rtsp_server_create_watch (server);
527 res = g_source_attach (source, context);