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