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"
26 #define DEFAULT_PORT 1554
33 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
35 static void gst_rtsp_server_get_property (GObject *object, guint propid,
36 GValue *value, GParamSpec *pspec);
37 static void gst_rtsp_server_set_property (GObject *object, guint propid,
38 const GValue *value, GParamSpec *pspec);
41 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
43 GObjectClass *gobject_class;
45 gobject_class = G_OBJECT_CLASS (klass);
47 gobject_class->get_property = gst_rtsp_server_get_property;
48 gobject_class->set_property = gst_rtsp_server_set_property;
50 g_object_class_install_property (gobject_class, PROP_PORT,
51 g_param_spec_int ("port", "Port", "The port the server uses",
52 1, 65535, DEFAULT_PORT,
53 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
57 gst_rtsp_server_init (GstRTSPServer * server)
59 server->server_port = DEFAULT_PORT;
60 server->pool = gst_rtsp_session_pool_new ();
64 * gst_rtsp_server_new:
66 * Create a new #GstRTSPServer instance.
69 gst_rtsp_server_new (void)
71 GstRTSPServer *result;
73 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
79 gst_rtsp_server_get_property (GObject *object, guint propid,
80 GValue *value, GParamSpec *pspec)
82 GstRTSPServer *server = GST_RTSP_SERVER (object);
86 g_value_set_int (value, server->server_port);
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
94 gst_rtsp_server_set_property (GObject *object, guint propid,
95 const GValue *value, GParamSpec *pspec)
97 GstRTSPServer *server = GST_RTSP_SERVER (object);
101 server->server_port = g_value_get_int (value);
104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
109 gst_rtsp_server_sink_init_send (GstRTSPServer * server)
113 /* create server socket */
114 if ((server->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
117 GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d",
118 server->server_sock.fd);
120 /* make address reusable */
122 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
123 (void *) &ret, sizeof (ret)) < 0)
126 /* keep connection alive; avoids SIGPIPE during write */
128 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
129 (void *) &ret, sizeof (ret)) < 0)
130 goto keepalive_failed;
132 /* name the socket */
133 memset (&server->server_sin, 0, sizeof (server->server_sin));
134 server->server_sin.sin_family = AF_INET; /* network socket */
135 server->server_sin.sin_port = htons (server->server_port); /* on port */
136 server->server_sin.sin_addr.s_addr = htonl (INADDR_ANY); /* for hosts */
139 GST_DEBUG_OBJECT (server, "binding server socket to address");
140 ret = bind (server->server_sock.fd, (struct sockaddr *) &server->server_sin,
141 sizeof (server->server_sin));
145 /* set the server socket to nonblocking */
146 fcntl (server->server_sock.fd, F_SETFL, O_NONBLOCK);
148 GST_DEBUG_OBJECT (server, "listening on server socket %d with queue of %d",
149 server->server_sock.fd, TCP_BACKLOG);
150 if (listen (server->server_sock.fd, TCP_BACKLOG) == -1)
153 GST_DEBUG_OBJECT (server,
154 "listened on server socket %d, returning from connection setup",
155 server->server_sock.fd);
162 GST_ERROR_OBJECT (server, "failed to create socket: %s", g_strerror (errno));
167 if (server->server_sock.fd >= 0) {
168 close (server->server_sock.fd);
169 server->server_sock.fd = -1;
171 GST_ERROR_OBJECT (server, "failed to reuse socket: %s", g_strerror (errno));
176 if (server->server_sock.fd >= 0) {
177 close (server->server_sock.fd);
178 server->server_sock.fd = -1;
180 GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s", g_strerror (errno));
185 if (server->server_sock.fd >= 0) {
186 close (server->server_sock.fd);
187 server->server_sock.fd = -1;
189 GST_ERROR_OBJECT (server, "failed to listen on socket: %s", g_strerror (errno));
194 if (server->server_sock.fd >= 0) {
195 close (server->server_sock.fd);
196 server->server_sock.fd = -1;
198 GST_ERROR_OBJECT (server, "failed to bind on socket: %s", g_strerror (errno));
203 /* called when an event is available on our server socket */
205 server_dispatch (GIOChannel *source, GIOCondition condition, GstRTSPServer *server)
207 if (condition & G_IO_IN) {
208 GstRTSPClient *client;
210 /* a new client connected, create a session to handle the client. */
211 client = gst_rtsp_client_new ();
213 /* set the session pool that this client should use */
214 gst_rtsp_client_set_session_pool (client, server->pool);
216 /* accept connections for that client, this function returns after accepting
217 * the connection and will run the remainder of the communication with the
218 * client asyncronously. */
219 if (!gst_rtsp_client_accept (client, source))
222 /* can unref the client now, when the request is finished, it will be
224 gst_object_unref (client);
227 g_print ("received unknown event %08x", condition);
234 g_error ("Could not accept client on server socket %d: %s (%d)",
235 server->server_sock.fd, g_strerror (errno), errno);
241 * gst_rtsp_server_attach:
242 * @server: a #GstRTSPServer
243 * @context: a #GMainContext
245 * Attaches @server to @context. When the mainloop for @context is run, the
246 * server will be dispatched.
248 * This function should be called when the server properties and urls are fully
249 * configured and the server is ready to start.
251 * Returns: the ID (greater than 0) for the source within the GMainContext.
254 gst_rtsp_server_attach (GstRTSPServer *server, GMainContext *context)
258 if (!gst_rtsp_server_sink_init_send (server))
261 /* create IO channel for the socket */
262 server->io_channel = g_io_channel_unix_new (server->server_sock.fd);
264 /* create a watch for reads (new connections) and possible errors */
265 server->io_watch = g_io_create_watch (server->io_channel, G_IO_IN |
266 G_IO_ERR | G_IO_HUP | G_IO_NVAL);
268 /* configure the callback */
269 g_source_set_callback (server->io_watch, (GSourceFunc) server_dispatch, server, NULL);
271 res = g_source_attach (server->io_watch, context);