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