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