Fix FSF address
[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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "rtsp-server.h"
24 #include "rtsp-client.h"
25
26 #define DEFAULT_ADDRESS         "0.0.0.0"
27 #define DEFAULT_BOUND_PORT      -1
28 /* #define DEFAULT_ADDRESS         "::0" */
29 #define DEFAULT_SERVICE         "8554"
30 #define DEFAULT_BACKLOG         5
31
32 /* Define to use the SO_LINGER option so that the server sockets can be resused
33  * sooner. Disabled for now because it is not very well implemented by various
34  * OSes and it causes clients to fail to read the TEARDOWN response. */
35 #undef USE_SOLINGER
36
37 enum
38 {
39   PROP_0,
40   PROP_ADDRESS,
41   PROP_SERVICE,
42   PROP_BOUND_PORT,
43   PROP_BACKLOG,
44
45   PROP_SESSION_POOL,
46   PROP_MEDIA_MAPPING,
47   PROP_LAST
48 };
49
50 enum
51 {
52   SIGNAL_CLIENT_CONNECTED,
53   SIGNAL_LAST
54 };
55
56 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
57
58 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
59 #define GST_CAT_DEFAULT rtsp_server_debug
60
61 static guint gst_rtsp_server_signals[SIGNAL_LAST] = { 0 };
62
63 static void gst_rtsp_server_get_property (GObject * object, guint propid,
64     GValue * value, GParamSpec * pspec);
65 static void gst_rtsp_server_set_property (GObject * object, guint propid,
66     const GValue * value, GParamSpec * pspec);
67 static void gst_rtsp_server_finalize (GObject * object);
68
69 static GstRTSPClient *default_create_client (GstRTSPServer * server);
70 static gboolean default_accept_client (GstRTSPServer * server,
71     GstRTSPClient * client, GSocket * socket, GError ** error);
72
73 static void
74 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
75 {
76   GObjectClass *gobject_class;
77
78   gobject_class = G_OBJECT_CLASS (klass);
79
80   gobject_class->get_property = gst_rtsp_server_get_property;
81   gobject_class->set_property = gst_rtsp_server_set_property;
82   gobject_class->finalize = gst_rtsp_server_finalize;
83
84   /**
85    * GstRTSPServer::address:
86    *
87    * The address of the server. This is the address where the server will
88    * listen on.
89    */
90   g_object_class_install_property (gobject_class, PROP_ADDRESS,
91       g_param_spec_string ("address", "Address",
92           "The address the server uses to listen on", DEFAULT_ADDRESS,
93           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
94   /**
95    * GstRTSPServer::service:
96    *
97    * The service of the server. This is either a string with the service name or
98    * a port number (as a string) the server will listen on.
99    */
100   g_object_class_install_property (gobject_class, PROP_SERVICE,
101       g_param_spec_string ("service", "Service",
102           "The service or port number the server uses to listen on",
103           DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
104   /**
105    * GstRTSPServer::bound-port:
106    *
107    * The actual port the server is listening on. Can be used to retrieve the
108    * port number when the server is started on port 0, which means bind to a
109    * random port. Set to -1 if the server has not been bound yet.
110    */
111   g_object_class_install_property (gobject_class, PROP_BOUND_PORT,
112       g_param_spec_int ("bound-port", "Bound port",
113           "The port number the server is listening on",
114           -1, G_MAXUINT16, DEFAULT_BOUND_PORT,
115           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
116   /**
117    * GstRTSPServer::backlog:
118    *
119    * The backlog argument defines the maximum length to which the queue of
120    * pending connections for the server may grow. If a connection request arrives
121    * when the queue is full, the client may receive an error with an indication of
122    * ECONNREFUSED or, if the underlying protocol supports retransmission, the
123    * request may be ignored so that a later reattempt at  connection succeeds.
124    */
125   g_object_class_install_property (gobject_class, PROP_BACKLOG,
126       g_param_spec_int ("backlog", "Backlog",
127           "The maximum length to which the queue "
128           "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
129           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
130   /**
131    * GstRTSPServer::session-pool:
132    *
133    * The session pool of the server. By default each server has a separate
134    * session pool but sessions can be shared between servers by setting the same
135    * session pool on multiple servers.
136    */
137   g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
138       g_param_spec_object ("session-pool", "Session Pool",
139           "The session pool to use for client session",
140           GST_TYPE_RTSP_SESSION_POOL,
141           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
142   /**
143    * GstRTSPServer::media-mapping:
144    *
145    * The media mapping to use for this server. By default the server has no
146    * media mapping and thus cannot map urls to media streams.
147    */
148   g_object_class_install_property (gobject_class, PROP_MEDIA_MAPPING,
149       g_param_spec_object ("media-mapping", "Media Mapping",
150           "The media mapping to use for client session",
151           GST_TYPE_RTSP_MEDIA_MAPPING,
152           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153
154   gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED] =
155       g_signal_new ("client-connected", G_TYPE_FROM_CLASS (gobject_class),
156       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPServerClass, client_connected),
157       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
158       gst_rtsp_client_get_type ());
159
160   klass->create_client = default_create_client;
161   klass->accept_client = default_accept_client;
162
163   GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
164 }
165
166 static void
167 gst_rtsp_server_init (GstRTSPServer * server)
168 {
169   g_mutex_init (&server->lock);
170   server->address = g_strdup (DEFAULT_ADDRESS);
171   server->service = g_strdup (DEFAULT_SERVICE);
172   server->socket = NULL;
173   server->backlog = DEFAULT_BACKLOG;
174   server->session_pool = gst_rtsp_session_pool_new ();
175   server->media_mapping = gst_rtsp_media_mapping_new ();
176 }
177
178 static void
179 gst_rtsp_server_finalize (GObject * object)
180 {
181   GstRTSPServer *server = GST_RTSP_SERVER (object);
182
183   GST_DEBUG_OBJECT (server, "finalize server");
184
185   g_free (server->address);
186   g_free (server->service);
187   if (server->socket)
188     g_object_unref (server->socket);
189
190   g_object_unref (server->session_pool);
191   g_object_unref (server->media_mapping);
192
193   if (server->auth)
194     g_object_unref (server->auth);
195
196   g_mutex_clear (&server->lock);
197
198   G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
199 }
200
201 /**
202  * gst_rtsp_server_new:
203  *
204  * Create a new #GstRTSPServer instance.
205  */
206 GstRTSPServer *
207 gst_rtsp_server_new (void)
208 {
209   GstRTSPServer *result;
210
211   result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
212
213   return result;
214 }
215
216 /**
217  * gst_rtsp_server_set_address:
218  * @server: a #GstRTSPServer
219  * @address: the address
220  *
221  * Configure @server to accept connections on the given address.
222  *
223  * This function must be called before the server is bound.
224  */
225 void
226 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
227 {
228   g_return_if_fail (GST_IS_RTSP_SERVER (server));
229   g_return_if_fail (address != NULL);
230
231   GST_RTSP_SERVER_LOCK (server);
232   g_free (server->address);
233   server->address = g_strdup (address);
234   GST_RTSP_SERVER_UNLOCK (server);
235 }
236
237 /**
238  * gst_rtsp_server_get_address:
239  * @server: a #GstRTSPServer
240  *
241  * Get the address on which the server will accept connections.
242  *
243  * Returns: the server address. g_free() after usage.
244  */
245 gchar *
246 gst_rtsp_server_get_address (GstRTSPServer * server)
247 {
248   gchar *result;
249   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
250
251   GST_RTSP_SERVER_LOCK (server);
252   result = g_strdup (server->address);
253   GST_RTSP_SERVER_UNLOCK (server);
254
255   return result;
256 }
257
258 /**
259  * gst_rtsp_server_get_bound_port:
260  * @server: a #GstRTSPServer
261  *
262  * Get the port number where the server was bound to.
263  *
264  * Returns: the port number
265  */
266 int
267 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
268 {
269   GSocketAddress *address;
270   int result = -1;
271
272   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
273
274   GST_RTSP_SERVER_LOCK (server);
275   if (server->socket == NULL)
276     goto out;
277
278   address = g_socket_get_local_address (server->socket, NULL);
279   result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
280   g_object_unref (address);
281
282 out:
283   GST_RTSP_SERVER_UNLOCK (server);
284
285   return result;
286 }
287
288 /**
289  * gst_rtsp_server_set_service:
290  * @server: a #GstRTSPServer
291  * @service: the service
292  *
293  * Configure @server to accept connections on the given service.
294  * @service should be a string containing the service name (see services(5)) or
295  * a string containing a port number between 1 and 65535.
296  *
297  * This function must be called before the server is bound.
298  */
299 void
300 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
301 {
302   g_return_if_fail (GST_IS_RTSP_SERVER (server));
303   g_return_if_fail (service != NULL);
304
305   GST_RTSP_SERVER_LOCK (server);
306   g_free (server->service);
307   server->service = g_strdup (service);
308   GST_RTSP_SERVER_UNLOCK (server);
309 }
310
311 /**
312  * gst_rtsp_server_get_service:
313  * @server: a #GstRTSPServer
314  *
315  * Get the service on which the server will accept connections.
316  *
317  * Returns: the service. use g_free() after usage.
318  */
319 gchar *
320 gst_rtsp_server_get_service (GstRTSPServer * server)
321 {
322   gchar *result;
323
324   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
325
326   GST_RTSP_SERVER_LOCK (server);
327   result = g_strdup (server->service);
328   GST_RTSP_SERVER_UNLOCK (server);
329
330   return result;
331 }
332
333 /**
334  * gst_rtsp_server_set_backlog:
335  * @server: a #GstRTSPServer
336  * @backlog: the backlog
337  *
338  * configure the maximum amount of requests that may be queued for the
339  * server.
340  *
341  * This function must be called before the server is bound.
342  */
343 void
344 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
345 {
346   g_return_if_fail (GST_IS_RTSP_SERVER (server));
347
348   GST_RTSP_SERVER_LOCK (server);
349   server->backlog = backlog;
350   GST_RTSP_SERVER_UNLOCK (server);
351 }
352
353 /**
354  * gst_rtsp_server_get_backlog:
355  * @server: a #GstRTSPServer
356  *
357  * The maximum amount of queued requests for the server.
358  *
359  * Returns: the server backlog.
360  */
361 gint
362 gst_rtsp_server_get_backlog (GstRTSPServer * server)
363 {
364   gint result;
365
366   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
367
368   GST_RTSP_SERVER_LOCK (server);
369   result = server->backlog;
370   GST_RTSP_SERVER_UNLOCK (server);
371
372   return result;
373 }
374
375 /**
376  * gst_rtsp_server_set_session_pool:
377  * @server: a #GstRTSPServer
378  * @pool: a #GstRTSPSessionPool
379  *
380  * configure @pool to be used as the session pool of @server.
381  */
382 void
383 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
384     GstRTSPSessionPool * pool)
385 {
386   GstRTSPSessionPool *old;
387
388   g_return_if_fail (GST_IS_RTSP_SERVER (server));
389
390   if (pool)
391     g_object_ref (pool);
392
393   GST_RTSP_SERVER_LOCK (server);
394   old = server->session_pool;
395   server->session_pool = pool;
396   GST_RTSP_SERVER_UNLOCK (server);
397
398   if (old)
399     g_object_unref (old);
400 }
401
402 /**
403  * gst_rtsp_server_get_session_pool:
404  * @server: a #GstRTSPServer
405  *
406  * Get the #GstRTSPSessionPool used as the session pool of @server.
407  *
408  * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
409  * usage.
410  */
411 GstRTSPSessionPool *
412 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
413 {
414   GstRTSPSessionPool *result;
415
416   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
417
418   GST_RTSP_SERVER_LOCK (server);
419   if ((result = server->session_pool))
420     g_object_ref (result);
421   GST_RTSP_SERVER_UNLOCK (server);
422
423   return result;
424 }
425
426 /**
427  * gst_rtsp_server_set_media_mapping:
428  * @server: a #GstRTSPServer
429  * @mapping: a #GstRTSPMediaMapping
430  *
431  * configure @mapping to be used as the media mapping of @server.
432  */
433 void
434 gst_rtsp_server_set_media_mapping (GstRTSPServer * server,
435     GstRTSPMediaMapping * mapping)
436 {
437   GstRTSPMediaMapping *old;
438
439   g_return_if_fail (GST_IS_RTSP_SERVER (server));
440
441   if (mapping)
442     g_object_ref (mapping);
443
444   GST_RTSP_SERVER_LOCK (server);
445   old = server->media_mapping;
446   server->media_mapping = mapping;
447   GST_RTSP_SERVER_UNLOCK (server);
448
449   if (old)
450     g_object_unref (old);
451 }
452
453
454 /**
455  * gst_rtsp_server_get_media_mapping:
456  * @server: a #GstRTSPServer
457  *
458  * Get the #GstRTSPMediaMapping used as the media mapping of @server.
459  *
460  * Returns: (transfer full): the #GstRTSPMediaMapping of @server. g_object_unref() after
461  * usage.
462  */
463 GstRTSPMediaMapping *
464 gst_rtsp_server_get_media_mapping (GstRTSPServer * server)
465 {
466   GstRTSPMediaMapping *result;
467
468   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
469
470   GST_RTSP_SERVER_LOCK (server);
471   if ((result = server->media_mapping))
472     g_object_ref (result);
473   GST_RTSP_SERVER_UNLOCK (server);
474
475   return result;
476 }
477
478 /**
479  * gst_rtsp_server_set_auth:
480  * @server: a #GstRTSPServer
481  * @auth: a #GstRTSPAuth
482  *
483  * configure @auth to be used as the authentication manager of @server.
484  */
485 void
486 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
487 {
488   GstRTSPAuth *old;
489
490   g_return_if_fail (GST_IS_RTSP_SERVER (server));
491
492   if (auth)
493     g_object_ref (auth);
494
495   GST_RTSP_SERVER_LOCK (server);
496   old = server->auth;
497   server->auth = auth;
498   GST_RTSP_SERVER_UNLOCK (server);
499
500   if (old)
501     g_object_unref (old);
502 }
503
504
505 /**
506  * gst_rtsp_server_get_auth:
507  * @server: a #GstRTSPServer
508  *
509  * Get the #GstRTSPAuth used as the authentication manager of @server.
510  *
511  * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
512  * usage.
513  */
514 GstRTSPAuth *
515 gst_rtsp_server_get_auth (GstRTSPServer * server)
516 {
517   GstRTSPAuth *result;
518
519   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
520
521   GST_RTSP_SERVER_LOCK (server);
522   if ((result = server->auth))
523     g_object_ref (result);
524   GST_RTSP_SERVER_UNLOCK (server);
525
526   return result;
527 }
528
529 static void
530 gst_rtsp_server_get_property (GObject * object, guint propid,
531     GValue * value, GParamSpec * pspec)
532 {
533   GstRTSPServer *server = GST_RTSP_SERVER (object);
534
535   switch (propid) {
536     case PROP_ADDRESS:
537       g_value_take_string (value, gst_rtsp_server_get_address (server));
538       break;
539     case PROP_SERVICE:
540       g_value_take_string (value, gst_rtsp_server_get_service (server));
541       break;
542     case PROP_BOUND_PORT:
543       g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
544       break;
545     case PROP_BACKLOG:
546       g_value_set_int (value, gst_rtsp_server_get_backlog (server));
547       break;
548     case PROP_SESSION_POOL:
549       g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
550       break;
551     case PROP_MEDIA_MAPPING:
552       g_value_take_object (value, gst_rtsp_server_get_media_mapping (server));
553       break;
554     default:
555       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
556   }
557 }
558
559 static void
560 gst_rtsp_server_set_property (GObject * object, guint propid,
561     const GValue * value, GParamSpec * pspec)
562 {
563   GstRTSPServer *server = GST_RTSP_SERVER (object);
564
565   switch (propid) {
566     case PROP_ADDRESS:
567       gst_rtsp_server_set_address (server, g_value_get_string (value));
568       break;
569     case PROP_SERVICE:
570       gst_rtsp_server_set_service (server, g_value_get_string (value));
571       break;
572     case PROP_BACKLOG:
573       gst_rtsp_server_set_backlog (server, g_value_get_int (value));
574       break;
575     case PROP_SESSION_POOL:
576       gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
577       break;
578     case PROP_MEDIA_MAPPING:
579       gst_rtsp_server_set_media_mapping (server, g_value_get_object (value));
580       break;
581     default:
582       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
583   }
584 }
585
586 /**
587  * gst_rtsp_server_create_socket:
588  * @server: a #GstRTSPServer
589  * @cancellable: a #GCancellable
590  * @error: a #GError
591  *
592  * Create a #GSocket for @server. The socket will listen on the
593  * configured service.
594  *
595  * Returns: (transfer full): the #GSocket for @server or NULL when an error occured.
596  */
597 GSocket *
598 gst_rtsp_server_create_socket (GstRTSPServer * server,
599     GCancellable * cancellable, GError ** error)
600 {
601   GSocketConnectable *conn;
602   GSocketAddressEnumerator *enumerator;
603   GSocket *socket = NULL;
604 #ifdef USE_SOLINGER
605   struct linger linger;
606 #endif
607   GError *sock_error = NULL;
608   GError *bind_error = NULL;
609   guint16 port;
610
611   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
612
613   GST_RTSP_SERVER_LOCK (server);
614   GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
615       server->service);
616
617   /* resolve the server IP address */
618   port = atoi (server->service);
619   if (port != 0 || !strcmp (server->service, "0"))
620     conn = g_network_address_new (server->address, port);
621   else
622     conn = g_network_service_new (server->service, "tcp", server->address);
623
624   enumerator = g_socket_connectable_enumerate (conn);
625   g_object_unref (conn);
626
627   /* create server socket, we loop through all the addresses until we manage to
628    * create a socket and bind. */
629   while (TRUE) {
630     GSocketAddress *sockaddr;
631
632     sockaddr =
633         g_socket_address_enumerator_next (enumerator, cancellable, error);
634     if (!sockaddr) {
635       if (!*error)
636         GST_DEBUG_OBJECT (server, "no more addresses %s",
637             *error ? (*error)->message : "");
638       else
639         GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
640             (*error)->message);
641       break;
642     }
643
644     /* only keep the first error */
645     socket = g_socket_new (g_socket_address_get_family (sockaddr),
646         G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
647         sock_error ? NULL : &sock_error);
648
649     if (socket == NULL) {
650       GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
651           sock_error->message);
652       continue;
653     }
654
655     if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
656       g_object_unref (sockaddr);
657       break;
658     }
659
660     GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
661         bind_error->message);
662     g_object_unref (sockaddr);
663     g_object_unref (socket);
664     socket = NULL;
665   }
666   g_object_unref (enumerator);
667
668   if (socket == NULL)
669     goto no_socket;
670
671   g_clear_error (&sock_error);
672   g_clear_error (&bind_error);
673
674   GST_DEBUG_OBJECT (server, "opened sending server socket");
675
676   /* keep connection alive; avoids SIGPIPE during write */
677   g_socket_set_keepalive (socket, TRUE);
678
679 #if 0
680 #ifdef USE_SOLINGER
681   /* make sure socket is reset 5 seconds after close. This ensure that we can
682    * reuse the socket quickly while still having a chance to send data to the
683    * client. */
684   linger.l_onoff = 1;
685   linger.l_linger = 5;
686   if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
687           (void *) &linger, sizeof (linger)) < 0)
688     goto linger_failed;
689 #endif
690 #endif
691
692   /* set the server socket to nonblocking */
693   g_socket_set_blocking (socket, FALSE);
694
695   /* set listen backlog */
696   g_socket_set_listen_backlog (socket, server->backlog);
697
698   if (!g_socket_listen (socket, error))
699     goto listen_failed;
700
701   GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
702       socket, server->backlog);
703
704   GST_RTSP_SERVER_UNLOCK (server);
705
706   return socket;
707
708   /* ERRORS */
709 no_socket:
710   {
711     GST_ERROR_OBJECT (server, "failed to create socket");
712     goto close_error;
713   }
714 #if 0
715 #ifdef USE_SOLINGER
716 linger_failed:
717   {
718     GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
719         g_strerror (errno));
720     goto close_error;
721   }
722 #endif
723 #endif
724 listen_failed:
725   {
726     GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
727         (*error)->message);
728     goto close_error;
729   }
730 close_error:
731   {
732     if (socket)
733       g_object_unref (socket);
734
735     if (sock_error) {
736       if (error == NULL)
737         g_propagate_error (error, sock_error);
738       else
739         g_error_free (sock_error);
740     }
741     if (bind_error) {
742       if ((error == NULL) || (*error == NULL))
743         g_propagate_error (error, bind_error);
744       else
745         g_error_free (bind_error);
746     }
747     GST_RTSP_SERVER_UNLOCK (server);
748     return NULL;
749   }
750 }
751
752 static void
753 unmanage_client (GstRTSPClient * client, GstRTSPServer * server)
754 {
755   GST_DEBUG_OBJECT (server, "unmanage client %p", client);
756
757   g_object_ref (server);
758   gst_rtsp_client_set_server (client, NULL);
759
760   GST_RTSP_SERVER_LOCK (server);
761   server->clients = g_list_remove (server->clients, client);
762   GST_RTSP_SERVER_UNLOCK (server);
763   g_object_unref (server);
764
765   g_object_unref (client);
766 }
767
768 /* add the client to the active list of clients, takes ownership of
769  * the client */
770 static void
771 manage_client (GstRTSPServer * server, GstRTSPClient * client)
772 {
773   GST_DEBUG_OBJECT (server, "manage client %p", client);
774   gst_rtsp_client_set_server (client, server);
775
776   GST_RTSP_SERVER_LOCK (server);
777   g_signal_connect (client, "closed", (GCallback) unmanage_client, server);
778   server->clients = g_list_prepend (server->clients, client);
779   GST_RTSP_SERVER_UNLOCK (server);
780 }
781
782 static GstRTSPClient *
783 default_create_client (GstRTSPServer * server)
784 {
785   GstRTSPClient *client;
786
787   /* a new client connected, create a session to handle the client. */
788   client = gst_rtsp_client_new ();
789
790   /* set the session pool that this client should use */
791   GST_RTSP_SERVER_LOCK (server);
792   gst_rtsp_client_set_session_pool (client, server->session_pool);
793   /* set the media mapping that this client should use */
794   gst_rtsp_client_set_media_mapping (client, server->media_mapping);
795   /* set authentication manager */
796   gst_rtsp_client_set_auth (client, server->auth);
797   GST_RTSP_SERVER_UNLOCK (server);
798
799   return client;
800 }
801
802 /* default method for creating a new client object in the server to accept and
803  * handle a client connection on this server */
804 static gboolean
805 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
806     GSocket * socket, GError ** error)
807 {
808   /* accept connections for that client, this function returns after accepting
809    * the connection and will run the remainder of the communication with the
810    * client asyncronously. */
811   if (!gst_rtsp_client_accept (client, socket, NULL, error))
812     goto accept_failed;
813
814   return TRUE;
815
816   /* ERRORS */
817 accept_failed:
818   {
819     GST_ERROR_OBJECT (server,
820         "Could not accept client on server : %s", (*error)->message);
821     return FALSE;
822   }
823 }
824
825 /**
826  * gst_rtsp_server_transfer_connection:
827  * @server: a #GstRTSPServer
828  * @socket: a network socket
829  * @ip: the IP address of the remote client
830  * @port: the port used by the other end
831  * @initial_buffer: any initial data that was already read from the socket
832  *
833  * Take an existing network socket and use it for an RTSP connection. This
834  * is used when transferring a socket from an HTTP server which should be used
835  * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
836  * that the HTTP server read from the socket while parsing the HTTP header.
837  *
838  * Returns: TRUE if all was ok, FALSE if an error occured.
839  */
840 gboolean
841 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
842     const gchar * ip, gint port, const gchar * initial_buffer)
843 {
844   GstRTSPClient *client = NULL;
845   GstRTSPServerClass *klass;
846   GError *error = NULL;
847
848   klass = GST_RTSP_SERVER_GET_CLASS (server);
849
850   if (klass->create_client)
851     client = klass->create_client (server);
852   if (client == NULL)
853     goto client_failed;
854
855   /* a new client connected, create a client object to handle the client. */
856   if (!gst_rtsp_client_create_from_socket (client, socket, ip, port,
857           initial_buffer, &error)) {
858     goto transfer_failed;
859   }
860
861   /* manage the client connection */
862   manage_client (server, client);
863
864   g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
865       client);
866
867   return TRUE;
868
869   /* ERRORS */
870 client_failed:
871   {
872     GST_ERROR_OBJECT (server, "failed to create a client");
873     return FALSE;
874   }
875 transfer_failed:
876   {
877     GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
878     g_error_free (error);
879     gst_object_unref (client);
880     return FALSE;
881   }
882 }
883
884 /**
885  * gst_rtsp_server_io_func:
886  * @socket: a #GSocket
887  * @condition: the condition on @source
888  * @server: a #GstRTSPServer
889  *
890  * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
891  * new connection on @socket or @server.
892  *
893  * Returns: TRUE if the source could be connected, FALSE if an error occured.
894  */
895 gboolean
896 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
897     GstRTSPServer * server)
898 {
899   gboolean result = TRUE;
900   GstRTSPClient *client = NULL;
901   GstRTSPServerClass *klass;
902   GError *error = NULL;
903
904   if (condition & G_IO_IN) {
905     klass = GST_RTSP_SERVER_GET_CLASS (server);
906
907     if (klass->create_client)
908       client = klass->create_client (server);
909     if (client == NULL)
910       goto client_failed;
911
912     /* a new client connected, create a client object to handle the client. */
913     if (klass->accept_client)
914       result = klass->accept_client (server, client, socket, &error);
915     if (!result)
916       goto accept_failed;
917
918     /* manage the client connection */
919     manage_client (server, client);
920
921     g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
922         client);
923   } else {
924     GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
925   }
926   return TRUE;
927
928   /* ERRORS */
929 client_failed:
930   {
931     GST_ERROR_OBJECT (server, "failed to create a client");
932     return FALSE;
933   }
934 accept_failed:
935   {
936     GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
937     g_error_free (error);
938     gst_object_unref (client);
939     return FALSE;
940   }
941 }
942
943 static void
944 watch_destroyed (GstRTSPServer * server)
945 {
946   GST_DEBUG_OBJECT (server, "source destroyed");
947   g_object_unref (server);
948 }
949
950 /**
951  * gst_rtsp_server_create_source:
952  * @server: a #GstRTSPServer
953  * @cancellable: a #GCancellable or %NULL.
954  * @error: a #GError
955  *
956  * Create a #GSource for @server. The new source will have a default
957  * #GSocketSourceFunc of gst_rtsp_server_io_func().
958  *
959  * @cancellable if not NULL can be used to cancel the source, which will cause
960  * the source to trigger, reporting the current condition (which is likely 0
961  * unless cancellation happened at the same time as a condition change). You can
962  * check for this in the callback using g_cancellable_is_cancelled().
963  *
964  * Returns: the #GSource for @server or NULL when an error occured. Free with
965  * g_source_unref ()
966  */
967 GSource *
968 gst_rtsp_server_create_source (GstRTSPServer * server,
969     GCancellable * cancellable, GError ** error)
970 {
971   GSocket *socket;
972   GSource *source;
973
974   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
975
976   socket = gst_rtsp_server_create_socket (server, NULL, error);
977   if (socket == NULL)
978     goto no_socket;
979   server->socket = g_object_ref (socket);
980
981   /* create a watch for reads (new connections) and possible errors */
982   source = g_socket_create_source (socket, G_IO_IN |
983       G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
984   g_object_unref (socket);
985
986   /* configure the callback */
987   g_source_set_callback (source,
988       (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
989       (GDestroyNotify) watch_destroyed);
990
991   return source;
992
993 no_socket:
994   {
995     GST_ERROR_OBJECT (server, "failed to create socket");
996     return NULL;
997   }
998 }
999
1000 /**
1001  * gst_rtsp_server_attach:
1002  * @server: a #GstRTSPServer
1003  * @context: (allow-none): a #GMainContext
1004  *
1005  * Attaches @server to @context. When the mainloop for @context is run, the
1006  * server will be dispatched. When @context is NULL, the default context will be
1007  * used).
1008  *
1009  * This function should be called when the server properties and urls are fully
1010  * configured and the server is ready to start.
1011  *
1012  * Returns: the ID (greater than 0) for the source within the GMainContext.
1013  */
1014 guint
1015 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1016 {
1017   guint res;
1018   GSource *source;
1019   GError *error = NULL;
1020
1021   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1022
1023   source = gst_rtsp_server_create_source (server, NULL, &error);
1024   if (source == NULL)
1025     goto no_source;
1026
1027   res = g_source_attach (source, context);
1028   g_source_unref (source);
1029
1030   return res;
1031
1032   /* ERRORS */
1033 no_source:
1034   {
1035     GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1036     g_error_free (error);
1037     return 0;
1038   }
1039 }