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