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
28 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
31 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
33 GObjectClass *gobject_class;
35 gobject_class = G_OBJECT_CLASS (klass);
39 gst_rtsp_server_init (GstRTSPServer * server)
41 server->server_port = DEFAULT_PORT;
42 server->pool = gst_rtsp_session_pool_new ();
46 * gst_rtsp_server_new:
48 * Create a new #GstRTSPServer instance.
51 gst_rtsp_server_new (void)
53 GstRTSPServer *result;
55 result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
61 gst_rtsp_server_sink_init_send (GstRTSPServer * server)
65 /* create server socket */
66 if ((server->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
69 GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d",
70 server->server_sock.fd);
72 /* make address reusable */
74 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
75 (void *) &ret, sizeof (ret)) < 0)
78 /* keep connection alive; avoids SIGPIPE during write */
80 if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
81 (void *) &ret, sizeof (ret)) < 0)
82 goto keepalive_failed;
85 memset (&server->server_sin, 0, sizeof (server->server_sin));
86 server->server_sin.sin_family = AF_INET; /* network socket */
87 server->server_sin.sin_port = htons (server->server_port); /* on port */
88 server->server_sin.sin_addr.s_addr = htonl (INADDR_ANY); /* for hosts */
91 GST_DEBUG_OBJECT (server, "binding server socket to address");
92 ret = bind (server->server_sock.fd, (struct sockaddr *) &server->server_sin,
93 sizeof (server->server_sin));
97 /* set the server socket to nonblocking */
98 fcntl (server->server_sock.fd, F_SETFL, O_NONBLOCK);
100 GST_DEBUG_OBJECT (server, "listening on server socket %d with queue of %d",
101 server->server_sock.fd, TCP_BACKLOG);
102 if (listen (server->server_sock.fd, TCP_BACKLOG) == -1)
105 GST_DEBUG_OBJECT (server,
106 "listened on server socket %d, returning from connection setup",
107 server->server_sock.fd);
114 GST_ERROR_OBJECT (server, "failed to create socket: %s", g_strerror (errno));
119 if (server->server_sock.fd >= 0) {
120 close (server->server_sock.fd);
121 server->server_sock.fd = -1;
123 GST_ERROR_OBJECT (server, "failed to reuse socket: %s", g_strerror (errno));
128 if (server->server_sock.fd >= 0) {
129 close (server->server_sock.fd);
130 server->server_sock.fd = -1;
132 GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s", g_strerror (errno));
137 if (server->server_sock.fd >= 0) {
138 close (server->server_sock.fd);
139 server->server_sock.fd = -1;
141 GST_ERROR_OBJECT (server, "failed to listen on socket: %s", g_strerror (errno));
146 if (server->server_sock.fd >= 0) {
147 close (server->server_sock.fd);
148 server->server_sock.fd = -1;
150 GST_ERROR_OBJECT (server, "failed to bind on socket: %s", g_strerror (errno));
155 /* called when an event is available on our server socket */
157 server_dispatch (GIOChannel *source, GIOCondition condition, GstRTSPServer *server)
159 if (condition & G_IO_IN) {
160 GstRTSPClient *client;
162 /* a new client connected, create a session to handle the client. */
163 client = gst_rtsp_client_new ();
165 /* set the session pool that this client should use */
166 gst_rtsp_client_set_session_pool (client, server->pool);
168 /* accept connections for that client, this function returns after accepting
169 * the connection and will run the remainder of the communication with the
170 * client asyncronously. */
171 if (!gst_rtsp_client_accept (client, source))
174 /* can unref the client now, when the request is finished, it will be
176 gst_object_unref (client);
179 g_print ("received unknown event %08x", condition);
186 g_error ("Could not accept client on server socket %d: %s (%d)",
187 server->server_sock.fd, g_strerror (errno), errno);
193 * gst_rtsp_server_attach:
194 * @server: a #GstRTSPServer
195 * @context: a #GMainContext
197 * Attaches @server to @context. When the mainloop for @context is run, the
198 * server will be dispatched.
200 * This function should be called when the server properties and urls are fully
201 * configured and the server is ready to start.
203 * Returns: the ID (greater than 0) for the source within the GMainContext.
206 gst_rtsp_server_attach (GstRTSPServer *server, GMainContext *context)
210 if (!gst_rtsp_server_sink_init_send (server))
213 /* create IO channel for the socket */
214 server->io_channel = g_io_channel_unix_new (server->server_sock.fd);
216 /* create a watch for reads (new connections) and possible errors */
217 server->io_watch = g_io_create_watch (server->io_channel, G_IO_IN |
218 G_IO_ERR | G_IO_HUP | G_IO_NVAL);
220 /* configure the callback */
221 g_source_set_callback (server->io_watch, (GSourceFunc) server_dispatch, server, NULL);
223 res = g_source_attach (server->io_watch, context);