rtsp-server: allow binding on port 0 (binds on a random port)
[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 <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 int
259 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
260 {
261   GSocketAddress *address;
262   int result = -1;
263
264   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
265
266   GST_RTSP_SERVER_LOCK (server);
267   if (server->socket == NULL)
268     goto out;
269
270   address = g_socket_get_local_address (server->socket, NULL);
271   result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
272   g_object_unref (address);
273
274 out:
275   GST_RTSP_SERVER_UNLOCK (server);
276
277   return result;
278 }
279
280 /**
281  * gst_rtsp_server_set_service:
282  * @server: a #GstRTSPServer
283  * @service: the service
284  *
285  * Configure @server to accept connections on the given service.
286  * @service should be a string containing the service name (see services(5)) or
287  * a string containing a port number between 1 and 65535.
288  *
289  * This function must be called before the server is bound.
290  */
291 void
292 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
293 {
294   g_return_if_fail (GST_IS_RTSP_SERVER (server));
295   g_return_if_fail (service != NULL);
296
297   GST_RTSP_SERVER_LOCK (server);
298   g_free (server->service);
299   server->service = g_strdup (service);
300   GST_RTSP_SERVER_UNLOCK (server);
301 }
302
303 /**
304  * gst_rtsp_server_get_service:
305  * @server: a #GstRTSPServer
306  *
307  * Get the service on which the server will accept connections.
308  *
309  * Returns: the service. use g_free() after usage.
310  */
311 gchar *
312 gst_rtsp_server_get_service (GstRTSPServer * server)
313 {
314   gchar *result;
315
316   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
317
318   GST_RTSP_SERVER_LOCK (server);
319   result = g_strdup (server->service);
320   GST_RTSP_SERVER_UNLOCK (server);
321
322   return result;
323 }
324
325 /**
326  * gst_rtsp_server_set_backlog:
327  * @server: a #GstRTSPServer
328  * @backlog: the backlog
329  *
330  * configure the maximum amount of requests that may be queued for the
331  * server.
332  *
333  * This function must be called before the server is bound.
334  */
335 void
336 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
337 {
338   g_return_if_fail (GST_IS_RTSP_SERVER (server));
339
340   GST_RTSP_SERVER_LOCK (server);
341   server->backlog = backlog;
342   GST_RTSP_SERVER_UNLOCK (server);
343 }
344
345 /**
346  * gst_rtsp_server_get_backlog:
347  * @server: a #GstRTSPServer
348  *
349  * The maximum amount of queued requests for the server.
350  *
351  * Returns: the server backlog.
352  */
353 gint
354 gst_rtsp_server_get_backlog (GstRTSPServer * server)
355 {
356   gint result;
357
358   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
359
360   GST_RTSP_SERVER_LOCK (server);
361   result = server->backlog;
362   GST_RTSP_SERVER_UNLOCK (server);
363
364   return result;
365 }
366
367 /**
368  * gst_rtsp_server_set_session_pool:
369  * @server: a #GstRTSPServer
370  * @pool: a #GstRTSPSessionPool
371  *
372  * configure @pool to be used as the session pool of @server.
373  */
374 void
375 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
376     GstRTSPSessionPool * pool)
377 {
378   GstRTSPSessionPool *old;
379
380   g_return_if_fail (GST_IS_RTSP_SERVER (server));
381
382   if (pool)
383     g_object_ref (pool);
384
385   GST_RTSP_SERVER_LOCK (server);
386   old = server->session_pool;
387   server->session_pool = pool;
388   GST_RTSP_SERVER_UNLOCK (server);
389
390   if (old)
391     g_object_unref (old);
392 }
393
394 /**
395  * gst_rtsp_server_get_session_pool:
396  * @server: a #GstRTSPServer
397  *
398  * Get the #GstRTSPSessionPool used as the session pool of @server.
399  *
400  * Returns: the #GstRTSPSessionPool used for sessions. g_object_unref() after
401  * usage.
402  */
403 GstRTSPSessionPool *
404 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
405 {
406   GstRTSPSessionPool *result;
407
408   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
409
410   GST_RTSP_SERVER_LOCK (server);
411   if ((result = server->session_pool))
412     g_object_ref (result);
413   GST_RTSP_SERVER_UNLOCK (server);
414
415   return result;
416 }
417
418 /**
419  * gst_rtsp_server_set_media_mapping:
420  * @server: a #GstRTSPServer
421  * @mapping: a #GstRTSPMediaMapping
422  *
423  * configure @mapping to be used as the media mapping of @server.
424  */
425 void
426 gst_rtsp_server_set_media_mapping (GstRTSPServer * server,
427     GstRTSPMediaMapping * mapping)
428 {
429   GstRTSPMediaMapping *old;
430
431   g_return_if_fail (GST_IS_RTSP_SERVER (server));
432
433   if (mapping)
434     g_object_ref (mapping);
435
436   GST_RTSP_SERVER_LOCK (server);
437   old = server->media_mapping;
438   server->media_mapping = mapping;
439   GST_RTSP_SERVER_UNLOCK (server);
440
441   if (old)
442     g_object_unref (old);
443 }
444
445
446 /**
447  * gst_rtsp_server_get_media_mapping:
448  * @server: a #GstRTSPServer
449  *
450  * Get the #GstRTSPMediaMapping used as the media mapping of @server.
451  *
452  * Returns: the #GstRTSPMediaMapping of @server. g_object_unref() after
453  * usage.
454  */
455 GstRTSPMediaMapping *
456 gst_rtsp_server_get_media_mapping (GstRTSPServer * server)
457 {
458   GstRTSPMediaMapping *result;
459
460   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
461
462   GST_RTSP_SERVER_LOCK (server);
463   if ((result = server->media_mapping))
464     g_object_ref (result);
465   GST_RTSP_SERVER_UNLOCK (server);
466
467   return result;
468 }
469
470 /**
471  * gst_rtsp_server_set_auth:
472  * @server: a #GstRTSPServer
473  * @auth: a #GstRTSPAuth
474  *
475  * configure @auth to be used as the authentication manager of @server.
476  */
477 void
478 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
479 {
480   GstRTSPAuth *old;
481
482   g_return_if_fail (GST_IS_RTSP_SERVER (server));
483
484   if (auth)
485     g_object_ref (auth);
486
487   GST_RTSP_SERVER_LOCK (server);
488   old = server->auth;
489   server->auth = auth;
490   GST_RTSP_SERVER_UNLOCK (server);
491
492   if (old)
493     g_object_unref (old);
494 }
495
496
497 /**
498  * gst_rtsp_server_get_auth:
499  * @server: a #GstRTSPServer
500  *
501  * Get the #GstRTSPAuth used as the authentication manager of @server.
502  *
503  * Returns: the #GstRTSPAuth of @server. g_object_unref() after
504  * usage.
505  */
506 GstRTSPAuth *
507 gst_rtsp_server_get_auth (GstRTSPServer * server)
508 {
509   GstRTSPAuth *result;
510
511   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
512
513   GST_RTSP_SERVER_LOCK (server);
514   if ((result = server->auth))
515     g_object_ref (result);
516   GST_RTSP_SERVER_UNLOCK (server);
517
518   return result;
519 }
520
521 static void
522 gst_rtsp_server_get_property (GObject * object, guint propid,
523     GValue * value, GParamSpec * pspec)
524 {
525   GstRTSPServer *server = GST_RTSP_SERVER (object);
526
527   switch (propid) {
528     case PROP_ADDRESS:
529       g_value_take_string (value, gst_rtsp_server_get_address (server));
530       break;
531     case PROP_SERVICE:
532       g_value_take_string (value, gst_rtsp_server_get_service (server));
533       break;
534     case PROP_BOUND_PORT:
535       g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
536       break;
537     case PROP_BACKLOG:
538       g_value_set_int (value, gst_rtsp_server_get_backlog (server));
539       break;
540     case PROP_SESSION_POOL:
541       g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
542       break;
543     case PROP_MEDIA_MAPPING:
544       g_value_take_object (value, gst_rtsp_server_get_media_mapping (server));
545       break;
546     default:
547       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
548   }
549 }
550
551 static void
552 gst_rtsp_server_set_property (GObject * object, guint propid,
553     const GValue * value, GParamSpec * pspec)
554 {
555   GstRTSPServer *server = GST_RTSP_SERVER (object);
556
557   switch (propid) {
558     case PROP_ADDRESS:
559       gst_rtsp_server_set_address (server, g_value_get_string (value));
560       break;
561     case PROP_SERVICE:
562       gst_rtsp_server_set_service (server, g_value_get_string (value));
563       break;
564     case PROP_BACKLOG:
565       gst_rtsp_server_set_backlog (server, g_value_get_int (value));
566       break;
567     case PROP_SESSION_POOL:
568       gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
569       break;
570     case PROP_MEDIA_MAPPING:
571       gst_rtsp_server_set_media_mapping (server, g_value_get_object (value));
572       break;
573     default:
574       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
575   }
576 }
577
578 /**
579  * gst_rtsp_server_create_socket:
580  * @server: a #GstRTSPServer
581  * @cancellable: a #GCancellable
582  * @error: a #GError
583  *
584  * Create a #GSocket for @server. The socket will listen on the
585  * configured service.
586  *
587  * Returns: the #GSocket for @server or NULL when an error occured.
588  */
589 GSocket *
590 gst_rtsp_server_create_socket (GstRTSPServer * server,
591     GCancellable * cancellable, GError ** error)
592 {
593   GSocketConnectable *conn;
594   GSocketAddressEnumerator *enumerator;
595   GSocket *socket = NULL;
596 #ifdef USE_SOLINGER
597   struct linger linger;
598 #endif
599   GError *sock_error = NULL;
600   GError *bind_error = NULL;
601   guint16 port;
602
603   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
604
605   GST_RTSP_SERVER_LOCK (server);
606   GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
607       server->service);
608
609   /* resolve the server IP address */
610   port = atoi (server->service);
611   if (port != 0 || !strcmp (server->service, "0"))
612     conn = g_network_address_new (server->address, port);
613   else
614     conn = g_network_service_new (server->service, "tcp", server->address);
615
616   enumerator = g_socket_connectable_enumerate (conn);
617   g_object_unref (conn);
618
619   /* create server socket, we loop through all the addresses until we manage to
620    * create a socket and bind. */
621   while (TRUE) {
622     GSocketAddress *sockaddr;
623
624     sockaddr =
625         g_socket_address_enumerator_next (enumerator, cancellable, error);
626     if (!sockaddr) {
627       if (!*error)
628         GST_DEBUG_OBJECT (server, "no more addresses %s",
629             *error ? (*error)->message : "");
630       else
631         GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
632             (*error)->message);
633       break;
634     }
635
636     /* only keep the first error */
637     socket = g_socket_new (g_socket_address_get_family (sockaddr),
638         G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
639         sock_error ? NULL : &sock_error);
640
641     if (socket == NULL) {
642       GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
643           sock_error->message);
644       continue;
645     }
646
647     if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
648       g_object_unref (sockaddr);
649       break;
650     }
651
652     GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
653         bind_error->message);
654     g_object_unref (sockaddr);
655     g_object_unref (socket);
656     socket = NULL;
657   }
658   g_object_unref (enumerator);
659
660   if (socket == NULL)
661     goto no_socket;
662
663   g_clear_error (&sock_error);
664   g_clear_error (&bind_error);
665
666   GST_DEBUG_OBJECT (server, "opened sending server socket");
667
668   /* keep connection alive; avoids SIGPIPE during write */
669   g_socket_set_keepalive (socket, TRUE);
670
671 #if 0
672 #ifdef USE_SOLINGER
673   /* make sure socket is reset 5 seconds after close. This ensure that we can
674    * reuse the socket quickly while still having a chance to send data to the
675    * client. */
676   linger.l_onoff = 1;
677   linger.l_linger = 5;
678   if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
679           (void *) &linger, sizeof (linger)) < 0)
680     goto linger_failed;
681 #endif
682 #endif
683
684   /* set the server socket to nonblocking */
685   g_socket_set_blocking (socket, FALSE);
686
687   /* set listen backlog */
688   g_socket_set_listen_backlog (socket, server->backlog);
689
690   if (!g_socket_listen (socket, error))
691     goto listen_failed;
692
693   GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
694       socket, server->backlog);
695
696   GST_RTSP_SERVER_UNLOCK (server);
697
698   return socket;
699
700   /* ERRORS */
701 no_socket:
702   {
703     GST_ERROR_OBJECT (server, "failed to create socket");
704     goto close_error;
705   }
706 #if 0
707 #ifdef USE_SOLINGER
708 linger_failed:
709   {
710     GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
711         g_strerror (errno));
712     goto close_error;
713   }
714 #endif
715 #endif
716 listen_failed:
717   {
718     GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
719         (*error)->message);
720     goto close_error;
721   }
722 close_error:
723   {
724     if (socket)
725       g_object_unref (socket);
726
727     if (sock_error) {
728       if (error == NULL)
729         g_propagate_error (error, sock_error);
730       else
731         g_error_free (sock_error);
732     }
733     if (bind_error) {
734       if ((error == NULL) || (*error == NULL))
735         g_propagate_error (error, bind_error);
736       else
737         g_error_free (bind_error);
738     }
739     GST_RTSP_SERVER_UNLOCK (server);
740     return NULL;
741   }
742 }
743
744 static void
745 unmanage_client (GstRTSPClient * client, GstRTSPServer * server)
746 {
747   GST_DEBUG_OBJECT (server, "unmanage client %p", client);
748
749   g_object_ref (server);
750   gst_rtsp_client_set_server (client, NULL);
751
752   GST_RTSP_SERVER_LOCK (server);
753   server->clients = g_list_remove (server->clients, client);
754   GST_RTSP_SERVER_UNLOCK (server);
755   g_object_unref (server);
756
757   g_object_unref (client);
758 }
759
760 /* add the client to the active list of clients, takes ownership of
761  * the client */
762 static void
763 manage_client (GstRTSPServer * server, GstRTSPClient * client)
764 {
765   GST_DEBUG_OBJECT (server, "manage client %p", client);
766   gst_rtsp_client_set_server (client, server);
767
768   GST_RTSP_SERVER_LOCK (server);
769   g_signal_connect (client, "closed", (GCallback) unmanage_client, server);
770   server->clients = g_list_prepend (server->clients, client);
771   GST_RTSP_SERVER_UNLOCK (server);
772 }
773
774 static GstRTSPClient *
775 default_create_client (GstRTSPServer * server)
776 {
777   GstRTSPClient *client;
778
779   /* a new client connected, create a session to handle the client. */
780   client = gst_rtsp_client_new ();
781
782   /* set the session pool that this client should use */
783   GST_RTSP_SERVER_LOCK (server);
784   gst_rtsp_client_set_session_pool (client, server->session_pool);
785   /* set the media mapping that this client should use */
786   gst_rtsp_client_set_media_mapping (client, server->media_mapping);
787   /* set authentication manager */
788   gst_rtsp_client_set_auth (client, server->auth);
789   GST_RTSP_SERVER_UNLOCK (server);
790
791   return client;
792 }
793
794 /* default method for creating a new client object in the server to accept and
795  * handle a client connection on this server */
796 static gboolean
797 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
798     GSocket * socket, GError ** error)
799 {
800   /* accept connections for that client, this function returns after accepting
801    * the connection and will run the remainder of the communication with the
802    * client asyncronously. */
803   if (!gst_rtsp_client_accept (client, socket, NULL, error))
804     goto accept_failed;
805
806   return TRUE;
807
808   /* ERRORS */
809 accept_failed:
810   {
811     GST_ERROR_OBJECT (server,
812         "Could not accept client on server : %s", (*error)->message);
813     return FALSE;
814   }
815 }
816
817 /**
818  * gst_rtsp_server_transfer_connection:
819  * @server: a #GstRTSPServer
820  * @socket: a network socket
821  * @ip: the IP address of the remote client
822  * @port: the port used by the other end
823  * @initial_buffer: any initial data that was already read from the socket
824  *
825  * Take an existing network socket and use it for an RTSP connection. This
826  * is used when transferring a socket from an HTTP server which should be used
827  * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
828  * that the HTTP server read from the socket while parsing the HTTP header.
829  *
830  * Returns: TRUE if all was ok, FALSE if an error occured.
831  */
832 gboolean
833 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
834     const gchar * ip, gint port, const gchar * initial_buffer)
835 {
836   GstRTSPClient *client = NULL;
837   GstRTSPServerClass *klass;
838   GError *error = NULL;
839
840   klass = GST_RTSP_SERVER_GET_CLASS (server);
841
842   if (klass->create_client)
843     client = klass->create_client (server);
844   if (client == NULL)
845     goto client_failed;
846
847   /* a new client connected, create a client object to handle the client. */
848   if (!gst_rtsp_client_create_from_socket (client, socket, ip, port,
849           initial_buffer, &error)) {
850     goto transfer_failed;
851   }
852
853   /* manage the client connection */
854   manage_client (server, client);
855
856   g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
857       client);
858
859   return TRUE;
860
861   /* ERRORS */
862 client_failed:
863   {
864     GST_ERROR_OBJECT (server, "failed to create a client");
865     return FALSE;
866   }
867 transfer_failed:
868   {
869     GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
870     g_error_free (error);
871     gst_object_unref (client);
872     return FALSE;
873   }
874 }
875
876 /**
877  * gst_rtsp_server_io_func:
878  * @socket: a #GSocket
879  * @condition: the condition on @source
880  *
881  * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
882  * new connection on @socket or @server.
883  *
884  * Returns: TRUE if the source could be connected, FALSE if an error occured.
885  */
886 gboolean
887 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
888     GstRTSPServer * server)
889 {
890   gboolean result = TRUE;
891   GstRTSPClient *client = NULL;
892   GstRTSPServerClass *klass;
893   GError *error = NULL;
894
895   if (condition & G_IO_IN) {
896     klass = GST_RTSP_SERVER_GET_CLASS (server);
897
898     if (klass->create_client)
899       client = klass->create_client (server);
900     if (client == NULL)
901       goto client_failed;
902
903     /* a new client connected, create a client object to handle the client. */
904     if (klass->accept_client)
905       result = klass->accept_client (server, client, socket, &error);
906     if (!result)
907       goto accept_failed;
908
909     /* manage the client connection */
910     manage_client (server, client);
911
912     g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
913         client);
914   } else {
915     GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
916   }
917   return TRUE;
918
919   /* ERRORS */
920 client_failed:
921   {
922     GST_ERROR_OBJECT (server, "failed to create a client");
923     return FALSE;
924   }
925 accept_failed:
926   {
927     GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
928     g_error_free (error);
929     gst_object_unref (client);
930     return FALSE;
931   }
932 }
933
934 static void
935 watch_destroyed (GstRTSPServer * server)
936 {
937   GST_DEBUG_OBJECT (server, "source destroyed");
938   g_object_unref (server);
939 }
940
941 /**
942  * gst_rtsp_server_create_source:
943  * @server: a #GstRTSPServer
944  * @cancellable: a #GCancellable or %NULL.
945  * @error: a #GError
946  *
947  * Create a #GSource for @server. The new source will have a default
948  * #GSocketSourceFunc of gst_rtsp_server_io_func().
949  *
950  * @cancellable if not NULL can be used to cancel the source, which will cause
951  * the source to trigger, reporting the current condition (which is likely 0
952  * unless cancellation happened at the same time as a condition change). You can
953  * check for this in the callback using g_cancellable_is_cancelled().
954  *
955  * Returns: the #GSource for @server or NULL when an error occured. Free with
956  * g_source_unref ()
957  */
958 GSource *
959 gst_rtsp_server_create_source (GstRTSPServer * server,
960     GCancellable * cancellable, GError ** error)
961 {
962   GSocket *socket;
963   GSource *source;
964
965   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
966
967   socket = gst_rtsp_server_create_socket (server, NULL, error);
968   server->socket = g_object_ref (socket);
969   if (socket == NULL)
970     goto no_socket;
971
972   /* create a watch for reads (new connections) and possible errors */
973   source = g_socket_create_source (socket, G_IO_IN |
974       G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
975   g_object_unref (socket);
976
977   /* configure the callback */
978   g_source_set_callback (source,
979       (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
980       (GDestroyNotify) watch_destroyed);
981
982   return source;
983
984 no_socket:
985   {
986     GST_ERROR_OBJECT (server, "failed to create socket");
987     return NULL;
988   }
989 }
990
991 /**
992  * gst_rtsp_server_attach:
993  * @server: a #GstRTSPServer
994  * @context: a #GMainContext
995  * @error: a #GError
996  *
997  * Attaches @server to @context. When the mainloop for @context is run, the
998  * server will be dispatched. When @context is NULL, the default context will be
999  * used).
1000  *
1001  * This function should be called when the server properties and urls are fully
1002  * configured and the server is ready to start.
1003  *
1004  * Returns: the ID (greater than 0) for the source within the GMainContext.
1005  */
1006 guint
1007 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1008 {
1009   guint res;
1010   GSource *source;
1011   GError *error = NULL;
1012
1013   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1014
1015   source = gst_rtsp_server_create_source (server, NULL, &error);
1016   if (source == NULL)
1017     goto no_source;
1018
1019   res = g_source_attach (source, context);
1020   g_source_unref (source);
1021
1022   return res;
1023
1024   /* ERRORS */
1025 no_source:
1026   {
1027     GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1028     g_error_free (error);
1029     return 0;
1030   }
1031 }