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 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   channel = g_io_channel_unix_new (sockfd);
654   g_io_channel_set_close_on_unref (channel, TRUE);
655
656   GST_INFO_OBJECT (server, "listening on service %s", server->service);
657   GST_RTSP_SERVER_UNLOCK (server);
658
659   return channel;
660
661   /* ERRORS */
662 no_address:
663   {
664     GST_ERROR_OBJECT (server, "failed to resolve address: %s",
665         gai_strerror (ret));
666     goto close_error;
667   }
668 no_socket:
669   {
670     GST_ERROR_OBJECT (server, "failed to create socket: %s",
671         g_strerror (errno));
672     goto close_error;
673   }
674 keepalive_failed:
675   {
676     GST_ERROR_OBJECT (server, "failed to configure keepalive socket: %s",
677         g_strerror (errno));
678     goto close_error;
679   }
680 #ifdef USE_SOLINGER
681 linger_failed:
682   {
683     GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
684         g_strerror (errno));
685     goto close_error;
686   }
687 #endif
688 listen_failed:
689   {
690     GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
691         g_strerror (errno));
692     goto close_error;
693   }
694 close_error:
695   {
696     if (sockfd >= 0) {
697       close (sockfd);
698     }
699     GST_RTSP_SERVER_UNLOCK (server);
700     return NULL;
701   }
702 }
703
704 static void
705 unmanage_client (GstRTSPClient * client, GstRTSPServer * server)
706 {
707   GST_DEBUG_OBJECT (server, "unmanage client %p", client);
708
709   gst_rtsp_client_set_server (client, NULL);
710
711   GST_RTSP_SERVER_LOCK (server);
712   server->clients = g_list_remove (server->clients, client);
713   GST_RTSP_SERVER_UNLOCK (server);
714
715   g_object_unref (client);
716 }
717
718 /* add the client to the active list of clients, takes ownership of
719  * the client */
720 static void
721 manage_client (GstRTSPServer * server, GstRTSPClient * client)
722 {
723   GST_DEBUG_OBJECT (server, "manage client %p", client);
724   gst_rtsp_client_set_server (client, server);
725
726   GST_RTSP_SERVER_LOCK (server);
727   g_signal_connect (client, "closed", (GCallback) unmanage_client, server);
728   server->clients = g_list_prepend (server->clients, client);
729   GST_RTSP_SERVER_UNLOCK (server);
730 }
731
732 static GstRTSPClient *
733 default_create_client (GstRTSPServer * server)
734 {
735   GstRTSPClient *client;
736
737   /* a new client connected, create a session to handle the client. */
738   client = gst_rtsp_client_new ();
739
740   /* set the session pool that this client should use */
741   GST_RTSP_SERVER_LOCK (server);
742   gst_rtsp_client_set_session_pool (client, server->session_pool);
743   /* set the media mapping that this client should use */
744   gst_rtsp_client_set_media_mapping (client, server->media_mapping);
745   /* set authentication manager */
746   gst_rtsp_client_set_auth (client, server->auth);
747   GST_RTSP_SERVER_UNLOCK (server);
748
749   return client;
750 }
751
752 /* default method for creating a new client object in the server to accept and
753  * handle a client connection on this server */
754 static gboolean
755 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
756     GIOChannel * channel)
757 {
758   /* accept connections for that client, this function returns after accepting
759    * the connection and will run the remainder of the communication with the
760    * client asyncronously. */
761   if (!gst_rtsp_client_accept (client, channel))
762     goto accept_failed;
763
764   return TRUE;
765
766   /* ERRORS */
767 accept_failed:
768   {
769     GST_ERROR_OBJECT (server,
770         "Could not accept client on server : %s (%d)", g_strerror (errno),
771         errno);
772     return FALSE;
773   }
774 }
775
776 /**
777  * gst_rtsp_server_io_func:
778  * @channel: a #GIOChannel
779  * @condition: the condition on @source
780  *
781  * A default #GIOFunc that creates a new #GstRTSPClient to accept and handle a
782  * new connection on @channel or @server.
783  *
784  * Returns: TRUE if the source could be connected, FALSE if an error occured.
785  */
786 gboolean
787 gst_rtsp_server_io_func (GIOChannel * channel, GIOCondition condition,
788     GstRTSPServer * server)
789 {
790   gboolean result;
791   GstRTSPClient *client = NULL;
792   GstRTSPServerClass *klass;
793
794   if (condition & G_IO_IN) {
795     klass = GST_RTSP_SERVER_GET_CLASS (server);
796
797     if (klass->create_client)
798       client = klass->create_client (server);
799     if (client == NULL)
800       goto client_failed;
801
802     /* a new client connected, create a client object to handle the client. */
803     if (klass->accept_client)
804       result = klass->accept_client (server, client, channel);
805     if (!result)
806       goto accept_failed;
807
808     /* manage the client connection */
809     manage_client (server, client);
810
811     g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
812         client);
813   } else {
814     GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
815   }
816   return TRUE;
817
818   /* ERRORS */
819 client_failed:
820   {
821     GST_ERROR_OBJECT (server, "failed to create a client");
822     return FALSE;
823   }
824 accept_failed:
825   {
826     GST_ERROR_OBJECT (server, "failed to accept client");
827     gst_object_unref (client);
828     return FALSE;
829   }
830 }
831
832 static void
833 watch_destroyed (GstRTSPServer * server)
834 {
835   GST_DEBUG_OBJECT (server, "source destroyed");
836   g_object_unref (server);
837 }
838
839 /**
840  * gst_rtsp_server_create_watch:
841  * @server: a #GstRTSPServer
842  *
843  * Create a #GSource for @server. The new source will have a default
844  * #GIOFunc of gst_rtsp_server_io_func().
845  *
846  * Returns: the #GSource for @server or NULL when an error occured.
847  */
848 GSource *
849 gst_rtsp_server_create_watch (GstRTSPServer * server)
850 {
851   GIOChannel *channel;
852   GSource *source;
853
854   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
855
856   channel = gst_rtsp_server_get_io_channel (server);
857   if (channel == NULL)
858     goto no_channel;
859
860   /* create a watch for reads (new connections) and possible errors */
861   source = g_io_create_watch (channel, G_IO_IN |
862       G_IO_ERR | G_IO_HUP | G_IO_NVAL);
863   g_io_channel_unref (channel);
864
865   /* configure the callback */
866   g_source_set_callback (source,
867       (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
868       (GDestroyNotify) watch_destroyed);
869
870   return source;
871
872 no_channel:
873   {
874     GST_ERROR_OBJECT (server, "failed to create IO channel");
875     return NULL;
876   }
877 }
878
879 /**
880  * gst_rtsp_server_attach:
881  * @server: a #GstRTSPServer
882  * @context: a #GMainContext
883  *
884  * Attaches @server to @context. When the mainloop for @context is run, the
885  * server will be dispatched. When @context is NULL, the default context will be
886  * used).
887  *
888  * This function should be called when the server properties and urls are fully
889  * configured and the server is ready to start.
890  *
891  * Returns: the ID (greater than 0) for the source within the GMainContext.
892  */
893 guint
894 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
895 {
896   guint res;
897   GSource *source;
898
899   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
900
901   source = gst_rtsp_server_create_watch (server);
902   if (source == NULL)
903     goto no_source;
904
905   res = g_source_attach (source, context);
906   g_source_unref (source);
907
908   return res;
909
910   /* ERRORS */
911 no_source:
912   {
913     GST_ERROR_OBJECT (server, "failed to create watch");
914     return 0;
915   }
916 }