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