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