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