rtsp: fix compiler warnings
[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, GSocket * socket, GError ** error);
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   g_mutex_init (&server->lock);
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_clear (&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_create_socket:
550  * @server: a #GstRTSPServer
551  * @cancellable: a #GCancellable
552  * @error: a #GError
553  *
554  * Create a #GSocket for @server. The socket will listen on the
555  * configured service.
556  *
557  * Returns: the #GSocket for @server or NULL when an error occured.
558  */
559 GSocket *
560 gst_rtsp_server_create_socket (GstRTSPServer * server,
561     GCancellable * cancellable, GError ** error)
562 {
563   GSocketConnectable *conn;
564   GSocketAddressEnumerator *enumerator;
565   GSocket *socket = NULL;
566 #ifdef USE_SOLINGER
567   struct linger linger;
568 #endif
569   GError *sock_error = NULL;
570   GError *bind_error = NULL;
571   guint16 port;
572
573   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
574
575   GST_RTSP_SERVER_LOCK (server);
576   GST_DEBUG_OBJECT (server, "getting address info of %s/%s", server->address,
577       server->service);
578
579   /* resolve the server IP address */
580   port = atoi (server->service);
581   if (port != 0)
582     conn = g_network_address_new (server->address, port);
583   else
584     conn = g_network_service_new (server->service, "tcp", server->address);
585
586   enumerator = g_socket_connectable_enumerate (conn);
587   g_object_unref (conn);
588
589   /* create server socket, we loop through all the addresses until we manage to
590    * create a socket and bind. */
591   while (TRUE) {
592     GSocketAddress *sockaddr;
593
594     sockaddr =
595         g_socket_address_enumerator_next (enumerator, cancellable, error);
596     if (!sockaddr) {
597       GST_DEBUG_OBJECT (server, "no more addresses %s", (*error)->message);
598       break;
599     }
600
601     /* only keep the first error */
602     socket = g_socket_new (g_socket_address_get_family (sockaddr),
603         G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
604         sock_error ? NULL : &sock_error);
605
606     if (socket == NULL) {
607       GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
608           sock_error->message);
609       continue;
610     }
611
612     if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
613       g_object_unref (sockaddr);
614       break;
615     }
616
617     GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
618         bind_error->message);
619     g_object_unref (sockaddr);
620     g_object_unref (socket);
621     socket = NULL;
622   }
623   g_object_unref (enumerator);
624
625   if (socket == NULL)
626     goto no_socket;
627
628   g_clear_error (&sock_error);
629   g_clear_error (&bind_error);
630
631   GST_DEBUG_OBJECT (server, "opened sending server socket");
632
633   /* keep connection alive; avoids SIGPIPE during write */
634   g_socket_set_keepalive (socket, TRUE);
635
636 #if 0
637 #ifdef USE_SOLINGER
638   /* make sure socket is reset 5 seconds after close. This ensure that we can
639    * reuse the socket quickly while still having a chance to send data to the
640    * client. */
641   linger.l_onoff = 1;
642   linger.l_linger = 5;
643   if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
644           (void *) &linger, sizeof (linger)) < 0)
645     goto linger_failed;
646 #endif
647 #endif
648
649   /* set the server socket to nonblocking */
650   g_socket_set_blocking (socket, FALSE);
651
652   /* set listen backlog */
653   g_socket_set_listen_backlog (socket, server->backlog);
654
655   if (!g_socket_listen (socket, error))
656     goto listen_failed;
657
658   GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
659       socket, server->backlog);
660
661   GST_RTSP_SERVER_UNLOCK (server);
662
663   return socket;
664
665   /* ERRORS */
666 no_socket:
667   {
668     GST_ERROR_OBJECT (server, "failed to create socket");
669     goto close_error;
670   }
671 #if 0
672 #ifdef USE_SOLINGER
673 linger_failed:
674   {
675     GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
676         g_strerror (errno));
677     goto close_error;
678   }
679 #endif
680 #endif
681 listen_failed:
682   {
683     GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
684         (*error)->message);
685     goto close_error;
686   }
687 close_error:
688   {
689     if (socket)
690       g_object_unref (socket);
691
692     if (sock_error) {
693       if (error == NULL)
694         g_propagate_error (error, sock_error);
695       else
696         g_error_free (sock_error);
697     }
698     if (bind_error) {
699       if (error == NULL)
700         g_propagate_error (error, bind_error);
701       else
702         g_error_free (bind_error);
703     }
704     GST_RTSP_SERVER_UNLOCK (server);
705     return NULL;
706   }
707 }
708
709 static void
710 unmanage_client (GstRTSPClient * client, GstRTSPServer * server)
711 {
712   GST_DEBUG_OBJECT (server, "unmanage client %p", client);
713
714   g_object_ref (server);
715   gst_rtsp_client_set_server (client, NULL);
716
717   GST_RTSP_SERVER_LOCK (server);
718   server->clients = g_list_remove (server->clients, client);
719   GST_RTSP_SERVER_UNLOCK (server);
720   g_object_unref (server);
721
722   g_object_unref (client);
723 }
724
725 /* add the client to the active list of clients, takes ownership of
726  * the client */
727 static void
728 manage_client (GstRTSPServer * server, GstRTSPClient * client)
729 {
730   GST_DEBUG_OBJECT (server, "manage client %p", client);
731   gst_rtsp_client_set_server (client, server);
732
733   GST_RTSP_SERVER_LOCK (server);
734   g_signal_connect (client, "closed", (GCallback) unmanage_client, server);
735   server->clients = g_list_prepend (server->clients, client);
736   GST_RTSP_SERVER_UNLOCK (server);
737 }
738
739 static GstRTSPClient *
740 default_create_client (GstRTSPServer * server)
741 {
742   GstRTSPClient *client;
743
744   /* a new client connected, create a session to handle the client. */
745   client = gst_rtsp_client_new ();
746
747   /* set the session pool that this client should use */
748   GST_RTSP_SERVER_LOCK (server);
749   gst_rtsp_client_set_session_pool (client, server->session_pool);
750   /* set the media mapping that this client should use */
751   gst_rtsp_client_set_media_mapping (client, server->media_mapping);
752   /* set authentication manager */
753   gst_rtsp_client_set_auth (client, server->auth);
754   GST_RTSP_SERVER_UNLOCK (server);
755
756   return client;
757 }
758
759 /* default method for creating a new client object in the server to accept and
760  * handle a client connection on this server */
761 static gboolean
762 default_accept_client (GstRTSPServer * server, GstRTSPClient * client,
763     GSocket * socket, GError ** error)
764 {
765   /* accept connections for that client, this function returns after accepting
766    * the connection and will run the remainder of the communication with the
767    * client asyncronously. */
768   if (!gst_rtsp_client_accept (client, socket, NULL, error))
769     goto accept_failed;
770
771   return TRUE;
772
773   /* ERRORS */
774 accept_failed:
775   {
776     GST_ERROR_OBJECT (server,
777         "Could not accept client on server : %s", (*error)->message);
778     return FALSE;
779   }
780 }
781
782 /**
783  * gst_rtsp_server_io_func:
784  * @socket: a #GSocket
785  * @condition: the condition on @source
786  *
787  * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
788  * new connection on @socket 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 (GSocket * socket, GIOCondition condition,
794     GstRTSPServer * server)
795 {
796   gboolean result = TRUE;
797   GstRTSPClient *client = NULL;
798   GstRTSPServerClass *klass;
799   GError *error = NULL;
800
801   if (condition & G_IO_IN) {
802     klass = GST_RTSP_SERVER_GET_CLASS (server);
803
804     if (klass->create_client)
805       client = klass->create_client (server);
806     if (client == NULL)
807       goto client_failed;
808
809     /* a new client connected, create a client object to handle the client. */
810     if (klass->accept_client)
811       result = klass->accept_client (server, client, socket, &error);
812     if (!result)
813       goto accept_failed;
814
815     /* manage the client connection */
816     manage_client (server, client);
817
818     g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
819         client);
820   } else {
821     GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
822   }
823   return TRUE;
824
825   /* ERRORS */
826 client_failed:
827   {
828     GST_ERROR_OBJECT (server, "failed to create a client");
829     return FALSE;
830   }
831 accept_failed:
832   {
833     GST_ERROR_OBJECT (server, "failed to accept client: %s", error->message);
834     g_error_free (error);
835     gst_object_unref (client);
836     return FALSE;
837   }
838 }
839
840 static void
841 watch_destroyed (GstRTSPServer * server)
842 {
843   GST_DEBUG_OBJECT (server, "source destroyed");
844   g_object_unref (server);
845 }
846
847 /**
848  * gst_rtsp_server_create_source:
849  * @server: a #GstRTSPServer
850  * @cancellable: a #GCancellable or %NULL.
851  * @error: a #GError
852  *
853  * Create a #GSource for @server. The new source will have a default
854  * #GSocketSourceFunc of gst_rtsp_server_io_func().
855  *
856  * @cancellable if not NULL can be used to cancel the source, which will cause
857  * the source to trigger, reporting the current condition (which is likely 0
858  * unless cancellation happened at the same time as a condition change). You can
859  * check for this in the callback using g_cancellable_is_cancelled().
860  *
861  * Returns: the #GSource for @server or NULL when an error occured. Free with
862  * g_source_unref ()
863  */
864 GSource *
865 gst_rtsp_server_create_source (GstRTSPServer * server,
866     GCancellable * cancellable, GError ** error)
867 {
868   GSocket *socket;
869   GSource *source;
870
871   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
872
873   socket = gst_rtsp_server_create_socket (server, NULL, error);
874   if (socket == NULL)
875     goto no_socket;
876
877   /* create a watch for reads (new connections) and possible errors */
878   source = g_socket_create_source (socket, G_IO_IN |
879       G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
880   g_object_unref (socket);
881
882   /* configure the callback */
883   g_source_set_callback (source,
884       (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
885       (GDestroyNotify) watch_destroyed);
886
887   return source;
888
889 no_socket:
890   {
891     GST_ERROR_OBJECT (server, "failed to create socket");
892     return NULL;
893   }
894 }
895
896 /**
897  * gst_rtsp_server_attach:
898  * @server: a #GstRTSPServer
899  * @context: a #GMainContext
900  * @error: a #GError
901  *
902  * Attaches @server to @context. When the mainloop for @context is run, the
903  * server will be dispatched. When @context is NULL, the default context will be
904  * used).
905  *
906  * This function should be called when the server properties and urls are fully
907  * configured and the server is ready to start.
908  *
909  * Returns: the ID (greater than 0) for the source within the GMainContext.
910  */
911 guint
912 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
913 {
914   guint res;
915   GSource *source;
916   GError *error = NULL;
917
918   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
919
920   source = gst_rtsp_server_create_source (server, NULL, &error);
921   if (source == NULL)
922     goto no_source;
923
924   res = g_source_attach (source, context);
925   g_source_unref (source);
926
927   return res;
928
929   /* ERRORS */
930 no_source:
931   {
932     GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
933     g_error_free (error);
934     return 0;
935   }
936 }