Make vmethod to create and accept new clients.
[platform/upstream/gstreamer.git] / gst / rtsp-server / 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 DEFAULT_BACKLOG         5
26 #define DEFAULT_PORT            1554
27
28 enum
29 {
30   PROP_0,
31   PROP_BACKLOG,
32   PROP_PORT,
33   PROP_POOL,
34   PROP_LAST
35 };
36
37 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
38
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);
43
44 static GstRTSPClient * gst_rtsp_server_accept_client (GstRTSPServer *server,
45     GIOChannel *channel);
46
47 static void
48 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
49
50   GObjectClass *gobject_class;
51
52   gobject_class = G_OBJECT_CLASS (klass);
53   
54   gobject_class->get_property = gst_rtsp_server_get_property;
55   gobject_class->set_property = gst_rtsp_server_set_property;
56   
57   /**
58    * GstRTSPServer::backlog
59    *
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.
65    */
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));
70   /**
71    * GstRTSPServer::port
72    *
73    * The session port of the server. This is the port where the server will
74    * listen on.
75    */
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));
79   /**
80    * GstRTSPServer::pool
81    *
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.
85    */
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));
89
90   klass->accept_client = gst_rtsp_server_accept_client;
91 }
92
93 static void
94 gst_rtsp_server_init (GstRTSPServer * server)
95 {
96   server->server_port = DEFAULT_PORT;
97   server->backlog = DEFAULT_BACKLOG;
98   server->pool = gst_rtsp_session_pool_new ();
99 }
100
101 /**
102  * gst_rtsp_server_new:
103  *
104  * Create a new #GstRTSPServer instance.
105  */
106 GstRTSPServer *
107 gst_rtsp_server_new (void)
108 {
109   GstRTSPServer *result;
110
111   result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
112
113   return result;
114 }
115
116 /**
117  * gst_rtsp_server_set_port:
118  * @server: a #GstRTSPServer
119  * @port: the port
120  *
121  * Configure @server to accept connections on the given port.
122  * @port should be a port number between 1 and 65535.
123  *
124  * This function must be called before the server is bound.
125  */
126 void
127 gst_rtsp_server_set_port (GstRTSPServer *server, gint port)
128 {
129   g_return_if_fail (GST_IS_RTSP_SERVER (server));
130   g_return_if_fail (port >= 1 && port <= 65535);
131
132   server->server_port = port;
133 }
134
135 /**
136  * gst_rtsp_server_get_port:
137  * @server: a #GstRTSPServer
138  *
139  * Get the port number on which the server will accept connections.
140  *
141  * Returns: the server port.
142  */
143 gint
144 gst_rtsp_server_get_port (GstRTSPServer *server)
145 {
146   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
147
148   return server->server_port;
149 }
150
151 /**
152  * gst_rtsp_server_set_backlog:
153  * @server: a #GstRTSPServer
154  * @backlog: the backlog
155  *
156  * configure the maximum amount of requests that may be queued for the
157  * server.
158  *
159  * This function must be called before the server is bound.
160  */
161 void
162 gst_rtsp_server_set_backlog (GstRTSPServer *server, gint backlog)
163 {
164   g_return_if_fail (GST_IS_RTSP_SERVER (server));
165
166   server->backlog = backlog;
167 }
168
169 /**
170  * gst_rtsp_server_get_backlog:
171  * @server: a #GstRTSPServer
172  *
173  * The maximum amount of queued requests for the server.
174  *
175  * Returns: the server backlog.
176  */
177 gint
178 gst_rtsp_server_get_backlog (GstRTSPServer *server)
179 {
180   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
181
182   return server->backlog;
183 }
184
185 /**
186  * gst_rtsp_server_set_session_pool:
187  * @server: a #GstRTSPServer
188  * @pool: a #GstRTSPSessionPool
189  *
190  * configure @pool to be used as the session pool of @server.
191  */
192 void
193 gst_rtsp_server_set_session_pool (GstRTSPServer *server, GstRTSPSessionPool *pool)
194 {
195   g_return_if_fail (GST_IS_RTSP_SERVER (server));
196
197   if (server->pool)
198     g_object_unref (server->pool);
199   if (pool)
200     pool = g_object_ref (pool);
201   server->pool = pool;
202 }
203
204
205 /**
206  * gst_rtsp_server_get_session_pool:
207  * @server: a #GstRTSPServer
208  *
209  * Get the #GstRTSPSessionPool used as the session pool of @server.
210  *
211  * Returns: the #GstRTSPSessionPool used for sessions. g_object_unref() after
212  * usage.
213  */
214 GstRTSPSessionPool *
215 gst_rtsp_server_get_session_pool (GstRTSPServer *server)
216 {
217   GstRTSPSessionPool *result;
218
219   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
220
221   if ((result = server->pool))
222     g_object_ref (result);
223
224   return result;
225 }
226
227 static void
228 gst_rtsp_server_get_property (GObject *object, guint propid,
229     GValue *value, GParamSpec *pspec)
230 {
231   GstRTSPServer *server = GST_RTSP_SERVER (object);
232
233   switch (propid) {
234     case PROP_PORT:
235       g_value_set_int (value, gst_rtsp_server_get_port (server));
236       break;
237     case PROP_BACKLOG:
238       g_value_set_int (value, gst_rtsp_server_get_backlog (server));
239       break;
240     case PROP_POOL:
241       g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
242       break;
243     default:
244       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
245   }
246 }
247
248 static void
249 gst_rtsp_server_set_property (GObject *object, guint propid,
250     const GValue *value, GParamSpec *pspec)
251 {
252   GstRTSPServer *server = GST_RTSP_SERVER (object);
253
254   switch (propid) {
255     case PROP_PORT:
256       gst_rtsp_server_set_port (server, g_value_get_int (value));
257       break;
258     case PROP_BACKLOG:
259       gst_rtsp_server_set_backlog (server, g_value_get_int (value));
260       break;
261     case PROP_POOL:
262       gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
263       break;
264     default:
265       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
266   }
267 }
268
269 /* Prepare a server socket for @server and make it listen on the configured port */
270 static gboolean
271 gst_rtsp_server_sink_init_send (GstRTSPServer * server)
272 {
273   int ret;
274
275   /* create server socket */
276   if ((server->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
277     goto no_socket;
278
279   GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d",
280       server->server_sock.fd);
281
282   /* make address reusable */
283   ret = 1;
284   if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
285           (void *) &ret, sizeof (ret)) < 0)
286     goto reuse_failed;
287
288   /* keep connection alive; avoids SIGPIPE during write */
289   ret = 1;
290   if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
291           (void *) &ret, sizeof (ret)) < 0)
292     goto keepalive_failed;
293
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 */
299
300   /* bind it */
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));
304   if (ret)
305     goto bind_failed;
306
307   /* set the server socket to nonblocking */
308   fcntl (server->server_sock.fd, F_SETFL, O_NONBLOCK);
309
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)
313     goto listen_failed;
314
315   GST_DEBUG_OBJECT (server,
316       "listened on server socket %d, returning from connection setup",
317       server->server_sock.fd);
318
319   return TRUE;
320
321   /* ERRORS */
322 no_socket:
323   {
324     GST_ERROR_OBJECT (server, "failed to create socket: %s", g_strerror (errno));
325     return FALSE;
326   }
327 reuse_failed:
328   {
329     if (server->server_sock.fd >= 0) {
330       close (server->server_sock.fd);
331       server->server_sock.fd = -1;
332     }
333     GST_ERROR_OBJECT (server, "failed to reuse socket: %s", g_strerror (errno));
334     return FALSE;
335   }
336 keepalive_failed:
337   {
338     if (server->server_sock.fd >= 0) {
339       close (server->server_sock.fd);
340       server->server_sock.fd = -1;
341     }
342     GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s", g_strerror (errno));
343     return FALSE;
344   }
345 listen_failed:
346   {
347     if (server->server_sock.fd >= 0) {
348       close (server->server_sock.fd);
349       server->server_sock.fd = -1;
350     }
351     GST_ERROR_OBJECT (server, "failed to listen on socket: %s", g_strerror (errno));
352     return FALSE;
353   }
354 bind_failed:
355   {
356     if (server->server_sock.fd >= 0) {
357       close (server->server_sock.fd);
358       server->server_sock.fd = -1;
359     }
360     GST_ERROR_OBJECT (server, "failed to bind on socket: %s", g_strerror (errno));
361     return FALSE;
362   }
363 }
364
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)
369 {
370   GstRTSPClient *client;
371
372   /* a new client connected, create a session to handle the client. */
373   client = gst_rtsp_client_new ();
374
375   /* set the session pool that this client should use */
376   gst_rtsp_client_set_session_pool (client, server->pool);
377
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))
382     goto accept_failed;
383
384   return client;
385
386   /* ERRORS */
387 accept_failed:
388   {
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);
392     return NULL;
393   }
394 }
395
396 /**
397  * gst_rtsp_server_io_func:
398  * @channel: a #GIOChannel
399  * @condition: the condition on @source
400  *
401  * A default #GIOFunc that creates a new #GstRTSPClient to accept and handle a
402  * new connection on @channel or @server.
403  *
404  * Returns: TRUE if the source could be connected, FALSE if an error occured.
405  */
406 gboolean
407 gst_rtsp_server_io_func (GIOChannel *channel, GIOCondition condition, GstRTSPServer *server)
408 {
409   GstRTSPClient *client = NULL;
410   GstRTSPServerClass *klass;
411
412   if (condition & G_IO_IN) {
413     klass = GST_RTSP_SERVER_GET_CLASS (server);
414
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);
418     if (client == NULL)
419       goto client_failed;
420
421     /* can unref the client now, when the request is finished, it will be
422      * unreffed async. */
423     gst_object_unref (client);
424   }
425   else {
426     g_print ("received unknown event %08x", condition);
427   }
428   return TRUE;
429
430   /* ERRORS */
431 client_failed:
432   {
433     GST_ERROR_OBJECT (server, "failed to create a client");
434     return FALSE;
435   }
436 }
437
438 /**
439  * gst_rtsp_server_get_io_channel:
440  * @server: a #GstRTSPServer
441  *
442  * Create a #GIOChannel for @server.
443  *
444  * Returns: the GIOChannel for @server or NULL when an error occured.
445  */
446 GIOChannel *
447 gst_rtsp_server_get_io_channel (GstRTSPServer *server)
448 {
449   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
450
451   if (server->io_channel == NULL) {
452     if (!gst_rtsp_server_sink_init_send (server))
453       goto init_failed;
454
455     /* create IO channel for the socket */
456     server->io_channel = g_io_channel_unix_new (server->server_sock.fd);
457   }
458   return server->io_channel;
459
460 init_failed:
461   {
462     return NULL;
463   }
464 }
465
466 /**
467  * gst_rtsp_server_create_watch:
468  * @server: a #GstRTSPServer
469  *
470  * Create a #GSource for @server. The new source will have a default
471  * #GIOFunc of gst_rtsp_server_io_func().
472  *
473  * Returns: the #GSource for @server or NULL when an error occured.
474  */
475 GSource *
476 gst_rtsp_server_create_watch (GstRTSPServer *server)
477 {
478   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
479
480   if (server->io_watch == NULL) {
481     GIOChannel *channel;
482
483     channel = gst_rtsp_server_get_io_channel (server);
484     if (channel == NULL)
485       goto no_channel;
486      
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);
490
491     /* configure the callback */
492     g_source_set_callback (server->io_watch, (GSourceFunc) gst_rtsp_server_io_func, server, NULL);
493   }
494   return server->io_watch;
495
496 no_channel:
497   {
498     return NULL;
499   }
500 }
501
502 /**
503  * gst_rtsp_server_attach:
504  * @server: a #GstRTSPServer
505  * @context: a #GMainContext
506  *
507  * Attaches @server to @context. When the mainloop for @context is run, the
508  * server will be dispatched.
509  *
510  * This function should be called when the server properties and urls are fully
511  * configured and the server is ready to start.
512  *
513  * Returns: the ID (greater than 0) for the source within the GMainContext. 
514  */
515 guint
516 gst_rtsp_server_attach (GstRTSPServer *server, GMainContext *context)
517 {
518   guint res;
519   GSource *source;
520
521   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
522
523   source = gst_rtsp_server_create_watch (server);
524   if (source == NULL)
525     goto no_source;
526
527   res = g_source_attach (source, context);
528
529   return res;
530
531   /* ERRORS */
532 no_source:
533   {
534     return 0;
535   }
536 }