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