Initial import
[platform/upstream/gst-rtsp-server.git] / src / rtsp-server.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <sys/ioctl.h>
21
22 #include "rtsp-server.h"
23 #include "rtsp-client.h"
24
25 #define TCP_BACKLOG             5
26 #define DEFAULT_PORT            1554
27
28 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
29
30 static void
31 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
32 {
33   GObjectClass *gobject_class;
34
35   gobject_class = G_OBJECT_CLASS (klass);
36 }
37
38 static void
39 gst_rtsp_server_init (GstRTSPServer * server)
40 {
41   server->server_port = DEFAULT_PORT;
42   server->pool = gst_rtsp_session_pool_new ();
43 }
44
45 /**
46  * gst_rtsp_server_new:
47  *
48  * Create a new #GstRTSPServer instance.
49  */
50 GstRTSPServer *
51 gst_rtsp_server_new (void)
52 {
53   GstRTSPServer *result;
54
55   result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
56
57   return result;
58 }
59
60 static gboolean
61 gst_rtsp_server_sink_init_send (GstRTSPServer * server)
62 {
63   int ret;
64
65   /* create server socket */
66   if ((server->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
67     goto no_socket;
68
69   GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d",
70       server->server_sock.fd);
71
72   /* make address reusable */
73   ret = 1;
74   if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
75           (void *) &ret, sizeof (ret)) < 0)
76     goto reuse_failed;
77
78   /* keep connection alive; avoids SIGPIPE during write */
79   ret = 1;
80   if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
81           (void *) &ret, sizeof (ret)) < 0)
82     goto keepalive_failed;
83
84   /* name the socket */
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 */
89
90   /* bind it */
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));
94   if (ret)
95     goto bind_failed;
96
97   /* set the server socket to nonblocking */
98   fcntl (server->server_sock.fd, F_SETFL, O_NONBLOCK);
99
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)
103     goto listen_failed;
104
105   GST_DEBUG_OBJECT (server,
106       "listened on server socket %d, returning from connection setup",
107       server->server_sock.fd);
108
109   return TRUE;
110
111   /* ERRORS */
112 no_socket:
113   {
114     GST_ERROR_OBJECT (server, "failed to create socket: %s", g_strerror (errno));
115     return FALSE;
116   }
117 reuse_failed:
118   {
119     if (server->server_sock.fd >= 0) {
120       close (server->server_sock.fd);
121       server->server_sock.fd = -1;
122     }
123     GST_ERROR_OBJECT (server, "failed to reuse socket: %s", g_strerror (errno));
124     return FALSE;
125   }
126 keepalive_failed:
127   {
128     if (server->server_sock.fd >= 0) {
129       close (server->server_sock.fd);
130       server->server_sock.fd = -1;
131     }
132     GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s", g_strerror (errno));
133     return FALSE;
134   }
135 listen_failed:
136   {
137     if (server->server_sock.fd >= 0) {
138       close (server->server_sock.fd);
139       server->server_sock.fd = -1;
140     }
141     GST_ERROR_OBJECT (server, "failed to listen on socket: %s", g_strerror (errno));
142     return FALSE;
143   }
144 bind_failed:
145   {
146     if (server->server_sock.fd >= 0) {
147       close (server->server_sock.fd);
148       server->server_sock.fd = -1;
149     }
150     GST_ERROR_OBJECT (server, "failed to bind on socket: %s", g_strerror (errno));
151     return FALSE;
152   }
153 }
154
155 /* called when an event is available on our server socket */
156 static gboolean
157 server_dispatch (GIOChannel *source, GIOCondition condition, GstRTSPServer *server)
158 {
159   if (condition & G_IO_IN) {
160     GstRTSPClient *client;
161
162     /* a new client connected, create a session to handle the client. */
163     client = gst_rtsp_client_new ();
164
165     /* set the session pool that this client should use */
166     gst_rtsp_client_set_session_pool (client, server->pool);
167
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))
172       goto accept_failed;
173
174     /* can unref the client now, when the request is finished, it will be
175      * unreffed async. */
176     gst_object_unref (client);
177   }
178   else {
179     g_print ("received unknown event %08x", condition);
180   }
181   return TRUE;
182
183   /* ERRORS */
184 accept_failed:
185   {
186     g_error ("Could not accept client on server socket %d: %s (%d)",
187             server->server_sock.fd, g_strerror (errno), errno);
188     return FALSE;
189   }
190 }
191
192 /**
193  * gst_rtsp_server_attach:
194  * @server: a #GstRTSPServer
195  * @context: a #GMainContext
196  *
197  * Attaches @server to @context. When the mainloop for @context is run, the
198  * server will be dispatched.
199  *
200  * This function should be called when the server properties and urls are fully
201  * configured and the server is ready to start.
202  *
203  * Returns: the ID (greater than 0) for the source within the GMainContext. 
204  */
205 guint
206 gst_rtsp_server_attach (GstRTSPServer *server, GMainContext *context)
207 {
208   guint res;
209
210   if (!gst_rtsp_server_sink_init_send (server))
211     goto init_failed;
212
213   /* create IO channel for the socket */
214   server->io_channel = g_io_channel_unix_new (server->server_sock.fd);
215
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);
219
220   /* configure the callback */
221   g_source_set_callback (server->io_watch, (GSourceFunc) server_dispatch, server, NULL);
222
223   res = g_source_attach (server->io_watch, context);
224
225   return res;
226
227   /* ERRORS */
228 init_failed:
229   {
230     return 0;
231   }
232 }