docs: update docs and comments
[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            8554
27
28 enum
29 {
30   PROP_0,
31   PROP_BACKLOG,
32   PROP_PORT,
33   PROP_SESSION_POOL,
34   PROP_MEDIA_MAPPING,
35   PROP_LAST
36 };
37
38 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
39
40 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
41 #define GST_CAT_DEFAULT rtsp_server_debug
42
43 static void gst_rtsp_server_get_property (GObject *object, guint propid,
44     GValue *value, GParamSpec *pspec);
45 static void gst_rtsp_server_set_property (GObject *object, guint propid,
46     const GValue *value, GParamSpec *pspec);
47 static void gst_rtsp_server_finalize (GObject *object);
48
49 static GstRTSPClient * default_accept_client (GstRTSPServer *server,
50     GIOChannel *channel);
51
52 static void
53 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
54
55   GObjectClass *gobject_class;
56
57   gobject_class = G_OBJECT_CLASS (klass);
58   
59   gobject_class->get_property = gst_rtsp_server_get_property;
60   gobject_class->set_property = gst_rtsp_server_set_property;
61   gobject_class->finalize = gst_rtsp_server_finalize;
62   
63   /**
64    * GstRTSPServer::backlog
65    *
66    * The backlog argument defines the maximum length to which the queue of
67    * pending connections for the server may grow. If a connection request arrives
68    * when the queue is full, the client may receive an error with an indication of
69    * ECONNREFUSED or, if the underlying protocol supports retransmission, the
70    * request may be ignored so that a later reattempt at  connection succeeds.
71    */
72   g_object_class_install_property (gobject_class, PROP_BACKLOG,
73       g_param_spec_int ("backlog", "Backlog", "The maximum length to which the queue "
74               "of pending connections may grow",
75           0, G_MAXINT, DEFAULT_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
76   /**
77    * GstRTSPServer::port
78    *
79    * The session port of the server. This is the port where the server will
80    * listen on.
81    */
82   g_object_class_install_property (gobject_class, PROP_PORT,
83       g_param_spec_int ("port", "Port", "The port the server uses to listen on",
84           1, 65535, DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
85   /**
86    * GstRTSPServer::session-pool
87    *
88    * The session pool of the server. By default each server has a separate
89    * session pool but sessions can be shared between servers by setting the same
90    * session pool on multiple servers.
91    */
92   g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
93       g_param_spec_object ("session-pool", "Session Pool",
94           "The session pool to use for client session",
95           GST_TYPE_RTSP_SESSION_POOL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
96   /**
97    * GstRTSPServer::media-mapping
98    *
99    * The media mapping to use for this server. By default the server has no
100    * media mapping and thus cannot map urls to media streams.
101    */
102   g_object_class_install_property (gobject_class, PROP_MEDIA_MAPPING,
103       g_param_spec_object ("media-mapping", "Media Mapping",
104           "The media mapping to use for client session",
105           GST_TYPE_RTSP_MEDIA_MAPPING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
106
107   klass->accept_client = default_accept_client;
108
109   GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
110 }
111
112 static void
113 gst_rtsp_server_init (GstRTSPServer * server)
114 {
115   server->port = DEFAULT_PORT;
116   server->backlog = DEFAULT_BACKLOG;
117   server->session_pool = gst_rtsp_session_pool_new ();
118   server->media_mapping = gst_rtsp_media_mapping_new ();
119 }
120
121 static void
122 gst_rtsp_server_finalize (GObject *object)
123 {
124   GstRTSPServer *server = GST_RTSP_SERVER (object);
125
126   g_object_unref (server->session_pool);
127   g_object_unref (server->media_mapping);
128 }
129
130 /**
131  * gst_rtsp_server_new:
132  *
133  * Create a new #GstRTSPServer instance.
134  */
135 GstRTSPServer *
136 gst_rtsp_server_new (void)
137 {
138   GstRTSPServer *result;
139
140   result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
141
142   return result;
143 }
144
145 /**
146  * gst_rtsp_server_set_port:
147  * @server: a #GstRTSPServer
148  * @port: the port
149  *
150  * Configure @server to accept connections on the given port.
151  * @port should be a port number between 1 and 65535.
152  *
153  * This function must be called before the server is bound.
154  */
155 void
156 gst_rtsp_server_set_port (GstRTSPServer *server, gint port)
157 {
158   g_return_if_fail (GST_IS_RTSP_SERVER (server));
159   g_return_if_fail (port >= 1 && port <= 65535);
160
161   server->port = port;
162 }
163
164 /**
165  * gst_rtsp_server_get_port:
166  * @server: a #GstRTSPServer
167  *
168  * Get the port number on which the server will accept connections.
169  *
170  * Returns: the server port.
171  */
172 gint
173 gst_rtsp_server_get_port (GstRTSPServer *server)
174 {
175   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
176
177   return server->port;
178 }
179
180 /**
181  * gst_rtsp_server_set_backlog:
182  * @server: a #GstRTSPServer
183  * @backlog: the backlog
184  *
185  * configure the maximum amount of requests that may be queued for the
186  * server.
187  *
188  * This function must be called before the server is bound.
189  */
190 void
191 gst_rtsp_server_set_backlog (GstRTSPServer *server, gint backlog)
192 {
193   g_return_if_fail (GST_IS_RTSP_SERVER (server));
194
195   server->backlog = backlog;
196 }
197
198 /**
199  * gst_rtsp_server_get_backlog:
200  * @server: a #GstRTSPServer
201  *
202  * The maximum amount of queued requests for the server.
203  *
204  * Returns: the server backlog.
205  */
206 gint
207 gst_rtsp_server_get_backlog (GstRTSPServer *server)
208 {
209   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
210
211   return server->backlog;
212 }
213
214 /**
215  * gst_rtsp_server_set_session_pool:
216  * @server: a #GstRTSPServer
217  * @pool: a #GstRTSPSessionPool
218  *
219  * configure @pool to be used as the session pool of @server.
220  */
221 void
222 gst_rtsp_server_set_session_pool (GstRTSPServer *server, GstRTSPSessionPool *pool)
223 {
224   GstRTSPSessionPool *old;
225
226   g_return_if_fail (GST_IS_RTSP_SERVER (server));
227
228   old = server->session_pool;
229
230   if (old != pool) {
231     if (pool)
232       g_object_ref (pool);
233     server->session_pool = pool;
234     if (old)
235       g_object_unref (old);
236   }
237 }
238
239 /**
240  * gst_rtsp_server_get_session_pool:
241  * @server: a #GstRTSPServer
242  *
243  * Get the #GstRTSPSessionPool used as the session pool of @server.
244  *
245  * Returns: the #GstRTSPSessionPool used for sessions. g_object_unref() after
246  * usage.
247  */
248 GstRTSPSessionPool *
249 gst_rtsp_server_get_session_pool (GstRTSPServer *server)
250 {
251   GstRTSPSessionPool *result;
252
253   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
254
255   if ((result = server->session_pool))
256     g_object_ref (result);
257
258   return result;
259 }
260
261 /**
262  * gst_rtsp_server_set_media_mapping:
263  * @server: a #GstRTSPServer
264  * @mapping: a #GstRTSPMediaMapping
265  *
266  * configure @mapping to be used as the media mapping of @server.
267  */
268 void
269 gst_rtsp_server_set_media_mapping (GstRTSPServer *server, GstRTSPMediaMapping *mapping)
270 {
271   GstRTSPMediaMapping *old;
272
273   g_return_if_fail (GST_IS_RTSP_SERVER (server));
274
275   old = server->media_mapping;
276
277   if (old != mapping) {
278     if (mapping)
279       g_object_ref (mapping);
280     server->media_mapping = mapping;
281     if (old)
282       g_object_unref (old);
283   }
284 }
285
286
287 /**
288  * gst_rtsp_server_get_media_mapping:
289  * @server: a #GstRTSPServer
290  *
291  * Get the #GstRTSPMediaMapping used as the media mapping of @server.
292  *
293  * Returns: the #GstRTSPMediaMapping of @server. g_object_unref() after
294  * usage.
295  */
296 GstRTSPMediaMapping *
297 gst_rtsp_server_get_media_mapping (GstRTSPServer *server)
298 {
299   GstRTSPMediaMapping *result;
300
301   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
302
303   if ((result = server->media_mapping))
304     g_object_ref (result);
305
306   return result;
307 }
308
309 static void
310 gst_rtsp_server_get_property (GObject *object, guint propid,
311     GValue *value, GParamSpec *pspec)
312 {
313   GstRTSPServer *server = GST_RTSP_SERVER (object);
314
315   switch (propid) {
316     case PROP_PORT:
317       g_value_set_int (value, gst_rtsp_server_get_port (server));
318       break;
319     case PROP_BACKLOG:
320       g_value_set_int (value, gst_rtsp_server_get_backlog (server));
321       break;
322     case PROP_SESSION_POOL:
323       g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
324       break;
325     case PROP_MEDIA_MAPPING:
326       g_value_take_object (value, gst_rtsp_server_get_media_mapping (server));
327       break;
328     default:
329       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
330   }
331 }
332
333 static void
334 gst_rtsp_server_set_property (GObject *object, guint propid,
335     const GValue *value, GParamSpec *pspec)
336 {
337   GstRTSPServer *server = GST_RTSP_SERVER (object);
338
339   switch (propid) {
340     case PROP_PORT:
341       gst_rtsp_server_set_port (server, g_value_get_int (value));
342       break;
343     case PROP_BACKLOG:
344       gst_rtsp_server_set_backlog (server, g_value_get_int (value));
345       break;
346     case PROP_SESSION_POOL:
347       gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
348       break;
349     case PROP_MEDIA_MAPPING:
350       gst_rtsp_server_set_media_mapping (server, g_value_get_object (value));
351       break;
352     default:
353       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
354   }
355 }
356
357 /* Prepare a server socket for @server and make it listen on the configured port */
358 static gboolean
359 gst_rtsp_server_sink_init_send (GstRTSPServer * server)
360 {
361   int ret;
362
363   /* create server socket */
364   if ((server->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
365     goto no_socket;
366
367   GST_DEBUG_OBJECT (server, "opened sending server socket with fd %d",
368       server->server_sock.fd);
369
370   /* make address reusable */
371   ret = 1;
372   if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
373           (void *) &ret, sizeof (ret)) < 0)
374     goto reuse_failed;
375
376   /* keep connection alive; avoids SIGPIPE during write */
377   ret = 1;
378   if (setsockopt (server->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
379           (void *) &ret, sizeof (ret)) < 0)
380     goto keepalive_failed;
381
382   /* name the socket */
383   memset (&server->server_sin, 0, sizeof (server->server_sin));
384   server->server_sin.sin_family = AF_INET;        /* network socket */
385   server->server_sin.sin_port = htons (server->port);        /* on port */
386   server->server_sin.sin_addr.s_addr = htonl (INADDR_ANY);        /* for hosts */
387
388   /* bind it */
389   GST_DEBUG_OBJECT (server, "binding server socket to address");
390   ret = bind (server->server_sock.fd, (struct sockaddr *) &server->server_sin,
391       sizeof (server->server_sin));
392   if (ret)
393     goto bind_failed;
394
395   /* set the server socket to nonblocking */
396   fcntl (server->server_sock.fd, F_SETFL, O_NONBLOCK);
397
398   GST_DEBUG_OBJECT (server, "listening on server socket %d with queue of %d",
399       server->server_sock.fd, server->backlog);
400   if (listen (server->server_sock.fd, server->backlog) == -1)
401     goto listen_failed;
402
403   GST_DEBUG_OBJECT (server,
404       "listened on server socket %d, returning from connection setup",
405       server->server_sock.fd);
406
407   GST_INFO_OBJECT (server, "listening on port %d", server->port);
408
409   return TRUE;
410
411   /* ERRORS */
412 no_socket:
413   {
414     GST_ERROR_OBJECT (server, "failed to create socket: %s", g_strerror (errno));
415     return FALSE;
416   }
417 reuse_failed:
418   {
419     if (server->server_sock.fd >= 0) {
420       close (server->server_sock.fd);
421       server->server_sock.fd = -1;
422     }
423     GST_ERROR_OBJECT (server, "failed to reuse socket: %s", g_strerror (errno));
424     return FALSE;
425   }
426 keepalive_failed:
427   {
428     if (server->server_sock.fd >= 0) {
429       close (server->server_sock.fd);
430       server->server_sock.fd = -1;
431     }
432     GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s", g_strerror (errno));
433     return FALSE;
434   }
435 listen_failed:
436   {
437     if (server->server_sock.fd >= 0) {
438       close (server->server_sock.fd);
439       server->server_sock.fd = -1;
440     }
441     GST_ERROR_OBJECT (server, "failed to listen on socket: %s", g_strerror (errno));
442     return FALSE;
443   }
444 bind_failed:
445   {
446     if (server->server_sock.fd >= 0) {
447       close (server->server_sock.fd);
448       server->server_sock.fd = -1;
449     }
450     GST_ERROR_OBJECT (server, "failed to bind on socket: %s", g_strerror (errno));
451     return FALSE;
452   }
453 }
454
455 /* default method for creating a new client object in the server to accept and
456  * handle a client connection on this server */
457 static GstRTSPClient *
458 default_accept_client (GstRTSPServer *server, GIOChannel *channel)
459 {
460   GstRTSPClient *client;
461
462   /* a new client connected, create a session to handle the client. */
463   client = gst_rtsp_client_new ();
464
465   /* set the session pool that this client should use */
466   gst_rtsp_client_set_session_pool (client, server->session_pool);
467   /* set the media mapping that this client should use */
468   gst_rtsp_client_set_media_mapping (client, server->media_mapping);
469
470   /* accept connections for that client, this function returns after accepting
471    * the connection and will run the remainder of the communication with the
472    * client asyncronously. */
473   if (!gst_rtsp_client_accept (client, channel))
474     goto accept_failed;
475
476   return client;
477
478   /* ERRORS */
479 accept_failed:
480   {
481     GST_ERROR_OBJECT (server, "Could not accept client on server socket %d: %s (%d)",
482             server->server_sock.fd, g_strerror (errno), errno);
483     gst_object_unref (client);
484     return NULL;
485   }
486 }
487
488 /**
489  * gst_rtsp_server_io_func:
490  * @channel: a #GIOChannel
491  * @condition: the condition on @source
492  *
493  * A default #GIOFunc that creates a new #GstRTSPClient to accept and handle a
494  * new connection on @channel or @server.
495  *
496  * Returns: TRUE if the source could be connected, FALSE if an error occured.
497  */
498 gboolean
499 gst_rtsp_server_io_func (GIOChannel *channel, GIOCondition condition, GstRTSPServer *server)
500 {
501   GstRTSPClient *client = NULL;
502   GstRTSPServerClass *klass;
503
504   if (condition & G_IO_IN) {
505     klass = GST_RTSP_SERVER_GET_CLASS (server);
506
507     /* a new client connected, create a client object to handle the client. */
508     if (klass->accept_client)
509       client = klass->accept_client (server, channel);
510     if (client == NULL)
511       goto client_failed;
512
513     /* can unref the client now, when the request is finished, it will be
514      * unreffed async. */
515     gst_object_unref (client);
516   }
517   else {
518     GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
519   }
520   return TRUE;
521
522   /* ERRORS */
523 client_failed:
524   {
525     GST_ERROR_OBJECT (server, "failed to create a client");
526     return FALSE;
527   }
528 }
529
530 /**
531  * gst_rtsp_server_get_io_channel:
532  * @server: a #GstRTSPServer
533  *
534  * Create a #GIOChannel for @server.
535  *
536  * Returns: the GIOChannel for @server or NULL when an error occured.
537  */
538 GIOChannel *
539 gst_rtsp_server_get_io_channel (GstRTSPServer *server)
540 {
541   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
542
543   if (server->io_channel == NULL) {
544     if (!gst_rtsp_server_sink_init_send (server))
545       goto init_failed;
546
547     /* create IO channel for the socket */
548     server->io_channel = g_io_channel_unix_new (server->server_sock.fd);
549   }
550   return server->io_channel;
551
552 init_failed:
553   {
554     return NULL;
555   }
556 }
557
558 /**
559  * gst_rtsp_server_create_watch:
560  * @server: a #GstRTSPServer
561  *
562  * Create a #GSource for @server. The new source will have a default
563  * #GIOFunc of gst_rtsp_server_io_func().
564  *
565  * Returns: the #GSource for @server or NULL when an error occured.
566  */
567 GSource *
568 gst_rtsp_server_create_watch (GstRTSPServer *server)
569 {
570   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
571
572   if (server->io_watch == NULL) {
573     GIOChannel *channel;
574
575     channel = gst_rtsp_server_get_io_channel (server);
576     if (channel == NULL)
577       goto no_channel;
578      
579     /* create a watch for reads (new connections) and possible errors */
580     server->io_watch = g_io_create_watch (channel, G_IO_IN |
581                   G_IO_ERR | G_IO_HUP | G_IO_NVAL);
582
583     /* configure the callback */
584     g_source_set_callback (server->io_watch, (GSourceFunc) gst_rtsp_server_io_func, server, NULL);
585   }
586   return server->io_watch;
587
588 no_channel:
589   {
590     return NULL;
591   }
592 }
593
594 /**
595  * gst_rtsp_server_attach:
596  * @server: a #GstRTSPServer
597  * @context: a #GMainContext
598  *
599  * Attaches @server to @context. When the mainloop for @context is run, the
600  * server will be dispatched.
601  *
602  * This function should be called when the server properties and urls are fully
603  * configured and the server is ready to start.
604  *
605  * Returns: the ID (greater than 0) for the source within the GMainContext. 
606  */
607 guint
608 gst_rtsp_server_attach (GstRTSPServer *server, GMainContext *context)
609 {
610   guint res;
611   GSource *source;
612
613   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
614
615   source = gst_rtsp_server_create_watch (server);
616   if (source == NULL)
617     goto no_source;
618
619   res = g_source_attach (source, context);
620
621   return res;
622
623   /* ERRORS */
624 no_source:
625   {
626     return 0;
627   }
628 }