24f2282bc1ae47a019bba338762a3f8f80b34493
[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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "rtsp-server.h"
24 #include "rtsp-client.h"
25
26 #define GST_RTSP_SERVER_GET_PRIVATE(obj)  \
27        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SERVER, GstRTSPServerPrivate))
28
29 #define GST_RTSP_SERVER_GET_LOCK(server)  (&(GST_RTSP_SERVER_CAST(server)->priv->lock))
30 #define GST_RTSP_SERVER_LOCK(server)      (g_mutex_lock(GST_RTSP_SERVER_GET_LOCK(server)))
31 #define GST_RTSP_SERVER_UNLOCK(server)    (g_mutex_unlock(GST_RTSP_SERVER_GET_LOCK(server)))
32
33 struct _GstRTSPServerPrivate
34 {
35   GMutex lock;                  /* protects everything in this struct */
36
37   /* server information */
38   gchar *address;
39   gchar *service;
40   gint backlog;
41   gint max_threads;
42
43   GSocket *socket;
44
45   /* sessions on this server */
46   GstRTSPSessionPool *session_pool;
47
48   /* mount points for this server */
49   GstRTSPMountPoints *mount_points;
50
51   /* authentication manager */
52   GstRTSPAuth *auth;
53
54   /* the clients that are connected */
55   GList *clients;
56   GQueue loops;                 /* the main loops used in the threads */
57 };
58
59 #define DEFAULT_ADDRESS         "0.0.0.0"
60 #define DEFAULT_BOUND_PORT      -1
61 /* #define DEFAULT_ADDRESS         "::0" */
62 #define DEFAULT_SERVICE         "8554"
63 #define DEFAULT_BACKLOG         5
64 #define DEFAULT_MAX_THREADS     0
65
66 /* Define to use the SO_LINGER option so that the server sockets can be resused
67  * sooner. Disabled for now because it is not very well implemented by various
68  * OSes and it causes clients to fail to read the TEARDOWN response. */
69 #undef USE_SOLINGER
70
71 enum
72 {
73   PROP_0,
74   PROP_ADDRESS,
75   PROP_SERVICE,
76   PROP_BOUND_PORT,
77   PROP_BACKLOG,
78
79   PROP_SESSION_POOL,
80   PROP_MOUNT_POINTS,
81   PROP_MAX_THREADS,
82   PROP_LAST
83 };
84
85 enum
86 {
87   SIGNAL_CLIENT_CONNECTED,
88   SIGNAL_LAST
89 };
90
91 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
92
93 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
94 #define GST_CAT_DEFAULT rtsp_server_debug
95
96 typedef struct _ClientContext ClientContext;
97 typedef struct _Loop Loop;
98
99 static guint gst_rtsp_server_signals[SIGNAL_LAST] = { 0 };
100
101 static void gst_rtsp_server_get_property (GObject * object, guint propid,
102     GValue * value, GParamSpec * pspec);
103 static void gst_rtsp_server_set_property (GObject * object, guint propid,
104     const GValue * value, GParamSpec * pspec);
105 static void gst_rtsp_server_finalize (GObject * object);
106
107 static gpointer do_loop (Loop * loop);
108 static GstRTSPClient *default_create_client (GstRTSPServer * server);
109
110 static void
111 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
112 {
113   GObjectClass *gobject_class;
114
115   g_type_class_add_private (klass, sizeof (GstRTSPServerPrivate));
116
117   gobject_class = G_OBJECT_CLASS (klass);
118
119   gobject_class->get_property = gst_rtsp_server_get_property;
120   gobject_class->set_property = gst_rtsp_server_set_property;
121   gobject_class->finalize = gst_rtsp_server_finalize;
122
123   /**
124    * GstRTSPServer::address:
125    *
126    * The address of the server. This is the address where the server will
127    * listen on.
128    */
129   g_object_class_install_property (gobject_class, PROP_ADDRESS,
130       g_param_spec_string ("address", "Address",
131           "The address the server uses to listen on", DEFAULT_ADDRESS,
132           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
133   /**
134    * GstRTSPServer::service:
135    *
136    * The service of the server. This is either a string with the service name or
137    * a port number (as a string) the server will listen on.
138    */
139   g_object_class_install_property (gobject_class, PROP_SERVICE,
140       g_param_spec_string ("service", "Service",
141           "The service or port number the server uses to listen on",
142           DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
143   /**
144    * GstRTSPServer::bound-port:
145    *
146    * The actual port the server is listening on. Can be used to retrieve the
147    * port number when the server is started on port 0, which means bind to a
148    * random port. Set to -1 if the server has not been bound yet.
149    */
150   g_object_class_install_property (gobject_class, PROP_BOUND_PORT,
151       g_param_spec_int ("bound-port", "Bound port",
152           "The port number the server is listening on",
153           -1, G_MAXUINT16, DEFAULT_BOUND_PORT,
154           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
155   /**
156    * GstRTSPServer::backlog:
157    *
158    * The backlog argument defines the maximum length to which the queue of
159    * pending connections for the server may grow. If a connection request arrives
160    * when the queue is full, the client may receive an error with an indication of
161    * ECONNREFUSED or, if the underlying protocol supports retransmission, the
162    * request may be ignored so that a later reattempt at  connection succeeds.
163    */
164   g_object_class_install_property (gobject_class, PROP_BACKLOG,
165       g_param_spec_int ("backlog", "Backlog",
166           "The maximum length to which the queue "
167           "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
168           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169   /**
170    * GstRTSPServer::session-pool:
171    *
172    * The session pool of the server. By default each server has a separate
173    * session pool but sessions can be shared between servers by setting the same
174    * session pool on multiple servers.
175    */
176   g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
177       g_param_spec_object ("session-pool", "Session Pool",
178           "The session pool to use for client session",
179           GST_TYPE_RTSP_SESSION_POOL,
180           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
181   /**
182    * GstRTSPServer::mount-points:
183    *
184    * The mount points to use for this server. By default the server has no
185    * mount points and thus cannot map urls to media streams.
186    */
187   g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
188       g_param_spec_object ("mount-points", "Mount Points",
189           "The mount points to use for client session",
190           GST_TYPE_RTSP_MOUNT_POINTS,
191           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192   /**
193    * GstRTSPServer::max-threads:
194    *
195    * The maximum amount of threads to use for client connections. A value of
196    * 0 means to use only the mainloop, -1 means an unlimited amount of
197    * threads.
198    */
199   g_object_class_install_property (gobject_class, PROP_MAX_THREADS,
200       g_param_spec_int ("max-threads", "Max Threads",
201           "The maximum amount of threads to use for client connections "
202           "(0 = only mainloop, -1 = unlimited)", -1, G_MAXINT,
203           DEFAULT_MAX_THREADS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204
205   gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED] =
206       g_signal_new ("client-connected", G_TYPE_FROM_CLASS (gobject_class),
207       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPServerClass, client_connected),
208       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
209       gst_rtsp_client_get_type ());
210
211   klass->create_client = default_create_client;
212
213   klass->pool = g_thread_pool_new ((GFunc) do_loop, klass, -1, FALSE, NULL);
214
215   GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
216 }
217
218 static void
219 gst_rtsp_server_init (GstRTSPServer * server)
220 {
221   GstRTSPServerPrivate *priv = GST_RTSP_SERVER_GET_PRIVATE (server);
222
223   server->priv = priv;
224
225   g_mutex_init (&priv->lock);
226   priv->address = g_strdup (DEFAULT_ADDRESS);
227   priv->service = g_strdup (DEFAULT_SERVICE);
228   priv->socket = NULL;
229   priv->backlog = DEFAULT_BACKLOG;
230   priv->session_pool = gst_rtsp_session_pool_new ();
231   priv->mount_points = gst_rtsp_mount_points_new ();
232   priv->max_threads = DEFAULT_MAX_THREADS;
233   g_queue_init (&priv->loops);
234 }
235
236 static void
237 gst_rtsp_server_finalize (GObject * object)
238 {
239   GstRTSPServer *server = GST_RTSP_SERVER (object);
240   GstRTSPServerPrivate *priv = server->priv;
241
242   GST_DEBUG_OBJECT (server, "finalize server");
243
244   g_free (priv->address);
245   g_free (priv->service);
246
247   if (priv->socket)
248     g_object_unref (priv->socket);
249
250   g_object_unref (priv->session_pool);
251   g_object_unref (priv->mount_points);
252
253   if (priv->auth)
254     g_object_unref (priv->auth);
255
256   g_mutex_clear (&priv->lock);
257
258   G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
259 }
260
261 /**
262  * gst_rtsp_server_new:
263  *
264  * Create a new #GstRTSPServer instance.
265  */
266 GstRTSPServer *
267 gst_rtsp_server_new (void)
268 {
269   GstRTSPServer *result;
270
271   result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
272
273   return result;
274 }
275
276 /**
277  * gst_rtsp_server_set_address:
278  * @server: a #GstRTSPServer
279  * @address: the address
280  *
281  * Configure @server to accept connections on the given address.
282  *
283  * This function must be called before the server is bound.
284  */
285 void
286 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
287 {
288   GstRTSPServerPrivate *priv;
289
290   g_return_if_fail (GST_IS_RTSP_SERVER (server));
291   g_return_if_fail (address != NULL);
292
293   priv = server->priv;
294
295   GST_RTSP_SERVER_LOCK (server);
296   g_free (priv->address);
297   priv->address = g_strdup (address);
298   GST_RTSP_SERVER_UNLOCK (server);
299 }
300
301 /**
302  * gst_rtsp_server_get_address:
303  * @server: a #GstRTSPServer
304  *
305  * Get the address on which the server will accept connections.
306  *
307  * Returns: the server address. g_free() after usage.
308  */
309 gchar *
310 gst_rtsp_server_get_address (GstRTSPServer * server)
311 {
312   GstRTSPServerPrivate *priv;
313   gchar *result;
314
315   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
316
317   priv = server->priv;
318
319   GST_RTSP_SERVER_LOCK (server);
320   result = g_strdup (priv->address);
321   GST_RTSP_SERVER_UNLOCK (server);
322
323   return result;
324 }
325
326 /**
327  * gst_rtsp_server_get_bound_port:
328  * @server: a #GstRTSPServer
329  *
330  * Get the port number where the server was bound to.
331  *
332  * Returns: the port number
333  */
334 int
335 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
336 {
337   GstRTSPServerPrivate *priv;
338   GSocketAddress *address;
339   int result = -1;
340
341   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
342
343   priv = server->priv;
344
345   GST_RTSP_SERVER_LOCK (server);
346   if (priv->socket == NULL)
347     goto out;
348
349   address = g_socket_get_local_address (priv->socket, NULL);
350   result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
351   g_object_unref (address);
352
353 out:
354   GST_RTSP_SERVER_UNLOCK (server);
355
356   return result;
357 }
358
359 /**
360  * gst_rtsp_server_set_service:
361  * @server: a #GstRTSPServer
362  * @service: the service
363  *
364  * Configure @server to accept connections on the given service.
365  * @service should be a string containing the service name (see services(5)) or
366  * a string containing a port number between 1 and 65535.
367  *
368  * This function must be called before the server is bound.
369  */
370 void
371 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
372 {
373   GstRTSPServerPrivate *priv;
374
375   g_return_if_fail (GST_IS_RTSP_SERVER (server));
376   g_return_if_fail (service != NULL);
377
378   priv = server->priv;
379
380   GST_RTSP_SERVER_LOCK (server);
381   g_free (priv->service);
382   priv->service = g_strdup (service);
383   GST_RTSP_SERVER_UNLOCK (server);
384 }
385
386 /**
387  * gst_rtsp_server_get_service:
388  * @server: a #GstRTSPServer
389  *
390  * Get the service on which the server will accept connections.
391  *
392  * Returns: the service. use g_free() after usage.
393  */
394 gchar *
395 gst_rtsp_server_get_service (GstRTSPServer * server)
396 {
397   GstRTSPServerPrivate *priv;
398   gchar *result;
399
400   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
401
402   priv = server->priv;
403
404   GST_RTSP_SERVER_LOCK (server);
405   result = g_strdup (priv->service);
406   GST_RTSP_SERVER_UNLOCK (server);
407
408   return result;
409 }
410
411 /**
412  * gst_rtsp_server_set_backlog:
413  * @server: a #GstRTSPServer
414  * @backlog: the backlog
415  *
416  * configure the maximum amount of requests that may be queued for the
417  * server.
418  *
419  * This function must be called before the server is bound.
420  */
421 void
422 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
423 {
424   GstRTSPServerPrivate *priv;
425
426   g_return_if_fail (GST_IS_RTSP_SERVER (server));
427
428   priv = server->priv;
429
430   GST_RTSP_SERVER_LOCK (server);
431   priv->backlog = backlog;
432   GST_RTSP_SERVER_UNLOCK (server);
433 }
434
435 /**
436  * gst_rtsp_server_get_backlog:
437  * @server: a #GstRTSPServer
438  *
439  * The maximum amount of queued requests for the server.
440  *
441  * Returns: the server backlog.
442  */
443 gint
444 gst_rtsp_server_get_backlog (GstRTSPServer * server)
445 {
446   GstRTSPServerPrivate *priv;
447   gint result;
448
449   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
450
451   priv = server->priv;
452
453   GST_RTSP_SERVER_LOCK (server);
454   result = priv->backlog;
455   GST_RTSP_SERVER_UNLOCK (server);
456
457   return result;
458 }
459
460 /**
461  * gst_rtsp_server_set_session_pool:
462  * @server: a #GstRTSPServer
463  * @pool: a #GstRTSPSessionPool
464  *
465  * configure @pool to be used as the session pool of @server.
466  */
467 void
468 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
469     GstRTSPSessionPool * pool)
470 {
471   GstRTSPServerPrivate *priv;
472   GstRTSPSessionPool *old;
473
474   g_return_if_fail (GST_IS_RTSP_SERVER (server));
475
476   priv = server->priv;
477
478   if (pool)
479     g_object_ref (pool);
480
481   GST_RTSP_SERVER_LOCK (server);
482   old = priv->session_pool;
483   priv->session_pool = pool;
484   GST_RTSP_SERVER_UNLOCK (server);
485
486   if (old)
487     g_object_unref (old);
488 }
489
490 /**
491  * gst_rtsp_server_get_session_pool:
492  * @server: a #GstRTSPServer
493  *
494  * Get the #GstRTSPSessionPool used as the session pool of @server.
495  *
496  * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
497  * usage.
498  */
499 GstRTSPSessionPool *
500 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
501 {
502   GstRTSPServerPrivate *priv;
503   GstRTSPSessionPool *result;
504
505   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
506
507   priv = server->priv;
508
509   GST_RTSP_SERVER_LOCK (server);
510   if ((result = priv->session_pool))
511     g_object_ref (result);
512   GST_RTSP_SERVER_UNLOCK (server);
513
514   return result;
515 }
516
517 /**
518  * gst_rtsp_server_set_mount_points:
519  * @server: a #GstRTSPServer
520  * @mounts: a #GstRTSPMountPoints
521  *
522  * configure @mounts to be used as the mount points of @server.
523  */
524 void
525 gst_rtsp_server_set_mount_points (GstRTSPServer * server,
526     GstRTSPMountPoints * mounts)
527 {
528   GstRTSPServerPrivate *priv;
529   GstRTSPMountPoints *old;
530
531   g_return_if_fail (GST_IS_RTSP_SERVER (server));
532
533   priv = server->priv;
534
535   if (mounts)
536     g_object_ref (mounts);
537
538   GST_RTSP_SERVER_LOCK (server);
539   old = priv->mount_points;
540   priv->mount_points = mounts;
541   GST_RTSP_SERVER_UNLOCK (server);
542
543   if (old)
544     g_object_unref (old);
545 }
546
547
548 /**
549  * gst_rtsp_server_get_mount_points:
550  * @server: a #GstRTSPServer
551  *
552  * Get the #GstRTSPMountPoints used as the mount points of @server.
553  *
554  * Returns: (transfer full): the #GstRTSPMountPoints of @server. g_object_unref() after
555  * usage.
556  */
557 GstRTSPMountPoints *
558 gst_rtsp_server_get_mount_points (GstRTSPServer * server)
559 {
560   GstRTSPServerPrivate *priv;
561   GstRTSPMountPoints *result;
562
563   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
564
565   priv = server->priv;
566
567   GST_RTSP_SERVER_LOCK (server);
568   if ((result = priv->mount_points))
569     g_object_ref (result);
570   GST_RTSP_SERVER_UNLOCK (server);
571
572   return result;
573 }
574
575 /**
576  * gst_rtsp_server_set_auth:
577  * @server: a #GstRTSPServer
578  * @auth: a #GstRTSPAuth
579  *
580  * configure @auth to be used as the authentication manager of @server.
581  */
582 void
583 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
584 {
585   GstRTSPServerPrivate *priv;
586   GstRTSPAuth *old;
587
588   g_return_if_fail (GST_IS_RTSP_SERVER (server));
589
590   priv = server->priv;
591
592   if (auth)
593     g_object_ref (auth);
594
595   GST_RTSP_SERVER_LOCK (server);
596   old = priv->auth;
597   priv->auth = auth;
598   GST_RTSP_SERVER_UNLOCK (server);
599
600   if (old)
601     g_object_unref (old);
602 }
603
604
605 /**
606  * gst_rtsp_server_get_auth:
607  * @server: a #GstRTSPServer
608  *
609  * Get the #GstRTSPAuth used as the authentication manager of @server.
610  *
611  * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
612  * usage.
613  */
614 GstRTSPAuth *
615 gst_rtsp_server_get_auth (GstRTSPServer * server)
616 {
617   GstRTSPServerPrivate *priv;
618   GstRTSPAuth *result;
619
620   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
621
622   priv = server->priv;
623
624   GST_RTSP_SERVER_LOCK (server);
625   if ((result = priv->auth))
626     g_object_ref (result);
627   GST_RTSP_SERVER_UNLOCK (server);
628
629   return result;
630 }
631
632 /**
633  * gst_rtsp_server_set_max_threads:
634  * @server: a #GstRTSPServer
635  * @max_threads: maximum threads
636  *
637  * Set the maximum threads used by the server to handle client requests.
638  * A value of 0 will use the server mainloop, a value of -1 will use an
639  * unlimited number of threads.
640  */
641 void
642 gst_rtsp_server_set_max_threads (GstRTSPServer * server, gint max_threads)
643 {
644   GstRTSPServerPrivate *priv;
645
646   g_return_if_fail (GST_IS_RTSP_SERVER (server));
647
648   priv = server->priv;
649
650   GST_RTSP_SERVER_LOCK (server);
651   priv->max_threads = max_threads;
652   GST_RTSP_SERVER_UNLOCK (server);
653 }
654
655 /**
656  * gst_rtsp_server_get_max_threads:
657  * @server: a #GstRTSPServer
658  *
659  * Get the maximum number of threads used for client connections.
660  * See gst_rtsp_server_set_max_threads().
661  *
662  * Returns: the maximum number of threads.
663  */
664 gint
665 gst_rtsp_server_get_max_threads (GstRTSPServer * server)
666 {
667   GstRTSPServerPrivate *priv;
668   gint res;
669
670   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
671
672   priv = server->priv;
673
674   GST_RTSP_SERVER_LOCK (server);
675   res = priv->max_threads;
676   GST_RTSP_SERVER_UNLOCK (server);
677
678   return res;
679 }
680
681
682 static void
683 gst_rtsp_server_get_property (GObject * object, guint propid,
684     GValue * value, GParamSpec * pspec)
685 {
686   GstRTSPServer *server = GST_RTSP_SERVER (object);
687
688   switch (propid) {
689     case PROP_ADDRESS:
690       g_value_take_string (value, gst_rtsp_server_get_address (server));
691       break;
692     case PROP_SERVICE:
693       g_value_take_string (value, gst_rtsp_server_get_service (server));
694       break;
695     case PROP_BOUND_PORT:
696       g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
697       break;
698     case PROP_BACKLOG:
699       g_value_set_int (value, gst_rtsp_server_get_backlog (server));
700       break;
701     case PROP_SESSION_POOL:
702       g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
703       break;
704     case PROP_MOUNT_POINTS:
705       g_value_take_object (value, gst_rtsp_server_get_mount_points (server));
706       break;
707     case PROP_MAX_THREADS:
708       g_value_set_int (value, gst_rtsp_server_get_max_threads (server));
709       break;
710     default:
711       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
712   }
713 }
714
715 static void
716 gst_rtsp_server_set_property (GObject * object, guint propid,
717     const GValue * value, GParamSpec * pspec)
718 {
719   GstRTSPServer *server = GST_RTSP_SERVER (object);
720
721   switch (propid) {
722     case PROP_ADDRESS:
723       gst_rtsp_server_set_address (server, g_value_get_string (value));
724       break;
725     case PROP_SERVICE:
726       gst_rtsp_server_set_service (server, g_value_get_string (value));
727       break;
728     case PROP_BACKLOG:
729       gst_rtsp_server_set_backlog (server, g_value_get_int (value));
730       break;
731     case PROP_SESSION_POOL:
732       gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
733       break;
734     case PROP_MOUNT_POINTS:
735       gst_rtsp_server_set_mount_points (server, g_value_get_object (value));
736       break;
737     case PROP_MAX_THREADS:
738       gst_rtsp_server_set_max_threads (server, g_value_get_int (value));
739       break;
740     default:
741       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
742   }
743 }
744
745 /**
746  * gst_rtsp_server_create_socket:
747  * @server: a #GstRTSPServer
748  * @cancellable: a #GCancellable
749  * @error: a #GError
750  *
751  * Create a #GSocket for @server. The socket will listen on the
752  * configured service.
753  *
754  * Returns: (transfer full): the #GSocket for @server or NULL when an error occured.
755  */
756 GSocket *
757 gst_rtsp_server_create_socket (GstRTSPServer * server,
758     GCancellable * cancellable, GError ** error)
759 {
760   GstRTSPServerPrivate *priv;
761   GSocketConnectable *conn;
762   GSocketAddressEnumerator *enumerator;
763   GSocket *socket = NULL;
764 #ifdef USE_SOLINGER
765   struct linger linger;
766 #endif
767   GError *sock_error = NULL;
768   GError *bind_error = NULL;
769   guint16 port;
770
771   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
772
773   priv = server->priv;
774
775   GST_RTSP_SERVER_LOCK (server);
776   GST_DEBUG_OBJECT (server, "getting address info of %s/%s", priv->address,
777       priv->service);
778
779   /* resolve the server IP address */
780   port = atoi (priv->service);
781   if (port != 0 || !strcmp (priv->service, "0"))
782     conn = g_network_address_new (priv->address, port);
783   else
784     conn = g_network_service_new (priv->service, "tcp", priv->address);
785
786   enumerator = g_socket_connectable_enumerate (conn);
787   g_object_unref (conn);
788
789   /* create server socket, we loop through all the addresses until we manage to
790    * create a socket and bind. */
791   while (TRUE) {
792     GSocketAddress *sockaddr;
793
794     sockaddr =
795         g_socket_address_enumerator_next (enumerator, cancellable, error);
796     if (!sockaddr) {
797       if (!*error)
798         GST_DEBUG_OBJECT (server, "no more addresses %s",
799             *error ? (*error)->message : "");
800       else
801         GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
802             (*error)->message);
803       break;
804     }
805
806     /* only keep the first error */
807     socket = g_socket_new (g_socket_address_get_family (sockaddr),
808         G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
809         sock_error ? NULL : &sock_error);
810
811     if (socket == NULL) {
812       GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
813           sock_error->message);
814       g_object_unref (sockaddr);
815       continue;
816     }
817
818     if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
819       g_object_unref (sockaddr);
820       break;
821     }
822
823     GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
824         bind_error->message);
825     g_object_unref (sockaddr);
826     g_object_unref (socket);
827     socket = NULL;
828   }
829   g_object_unref (enumerator);
830
831   if (socket == NULL)
832     goto no_socket;
833
834   g_clear_error (&sock_error);
835   g_clear_error (&bind_error);
836
837   GST_DEBUG_OBJECT (server, "opened sending server socket");
838
839   /* keep connection alive; avoids SIGPIPE during write */
840   g_socket_set_keepalive (socket, TRUE);
841
842 #if 0
843 #ifdef USE_SOLINGER
844   /* make sure socket is reset 5 seconds after close. This ensure that we can
845    * reuse the socket quickly while still having a chance to send data to the
846    * client. */
847   linger.l_onoff = 1;
848   linger.l_linger = 5;
849   if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
850           (void *) &linger, sizeof (linger)) < 0)
851     goto linger_failed;
852 #endif
853 #endif
854
855   /* set the server socket to nonblocking */
856   g_socket_set_blocking (socket, FALSE);
857
858   /* set listen backlog */
859   g_socket_set_listen_backlog (socket, priv->backlog);
860
861   if (!g_socket_listen (socket, error))
862     goto listen_failed;
863
864   GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
865       socket, priv->backlog);
866
867   GST_RTSP_SERVER_UNLOCK (server);
868
869   return socket;
870
871   /* ERRORS */
872 no_socket:
873   {
874     GST_ERROR_OBJECT (server, "failed to create socket");
875     goto close_error;
876   }
877 #if 0
878 #ifdef USE_SOLINGER
879 linger_failed:
880   {
881     GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
882         g_strerror (errno));
883     goto close_error;
884   }
885 #endif
886 #endif
887 listen_failed:
888   {
889     GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
890         (*error)->message);
891     goto close_error;
892   }
893 close_error:
894   {
895     if (socket)
896       g_object_unref (socket);
897
898     if (sock_error) {
899       if (error == NULL)
900         g_propagate_error (error, sock_error);
901       else
902         g_error_free (sock_error);
903     }
904     if (bind_error) {
905       if ((error == NULL) || (*error == NULL))
906         g_propagate_error (error, bind_error);
907       else
908         g_error_free (bind_error);
909     }
910     GST_RTSP_SERVER_UNLOCK (server);
911     return NULL;
912   }
913 }
914
915 struct _Loop
916 {
917   gint refcnt;
918
919   GstRTSPServer *server;
920   GMainLoop *mainloop;
921   GMainContext *mainctx;
922 };
923
924 /* must be called with the lock held */
925 static void
926 loop_unref (Loop * loop)
927 {
928   GstRTSPServer *server = loop->server;
929   GstRTSPServerPrivate *priv = server->priv;
930
931   loop->refcnt--;
932
933   if (loop->refcnt <= 0) {
934     g_queue_remove (&priv->loops, loop);
935     g_main_loop_quit (loop->mainloop);
936   }
937 }
938
939 struct _ClientContext
940 {
941   GstRTSPServer *server;
942   Loop *loop;
943   GstRTSPClient *client;
944 };
945
946 static gboolean
947 free_client_context (ClientContext * ctx)
948 {
949   GST_RTSP_SERVER_LOCK (ctx->server);
950   if (ctx->loop)
951     loop_unref (ctx->loop);
952   GST_RTSP_SERVER_UNLOCK (ctx->server);
953
954   g_object_unref (ctx->client);
955   g_slice_free (ClientContext, ctx);
956
957   return G_SOURCE_REMOVE;
958 }
959
960 static gpointer
961 do_loop (Loop * loop)
962 {
963   GST_INFO ("enter mainloop");
964   g_main_loop_run (loop->mainloop);
965   GST_INFO ("exit mainloop");
966
967   g_main_context_unref (loop->mainctx);
968   g_main_loop_unref (loop->mainloop);
969   g_object_unref (loop->server);
970   g_slice_free (Loop, loop);
971
972   return NULL;
973 }
974
975 /* Must be called with lock held */
976
977 static Loop *
978 gst_rtsp_server_get_main_loop (GstRTSPServer * server)
979 {
980   GstRTSPServerPrivate *priv = server->priv;
981   Loop *loop;
982
983   if (priv->max_threads > 0 &&
984       g_queue_get_length (&priv->loops) >= priv->max_threads) {
985     loop = g_queue_pop_head (&priv->loops);
986     loop->refcnt++;
987   } else {
988     GstRTSPServerClass *klass = GST_RTSP_SERVER_GET_CLASS (server);
989
990     loop = g_slice_new0 (Loop);
991     loop->refcnt = 1;
992     loop->server = g_object_ref (server);
993     loop->mainctx = g_main_context_new ();
994     loop->mainloop = g_main_loop_new (loop->mainctx, FALSE);
995
996     g_thread_pool_push (klass->pool, loop, NULL);
997   }
998
999   g_queue_push_tail (&priv->loops, loop);
1000
1001   return loop;
1002 }
1003
1004 static void
1005 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
1006 {
1007   GstRTSPServer *server = ctx->server;
1008   GstRTSPServerPrivate *priv = server->priv;
1009
1010   GST_DEBUG_OBJECT (server, "unmanage client %p", client);
1011
1012   g_object_ref (server);
1013
1014   GST_RTSP_SERVER_LOCK (server);
1015   priv->clients = g_list_remove (priv->clients, ctx);
1016   GST_RTSP_SERVER_UNLOCK (server);
1017
1018   if (ctx->loop) {
1019     GSource *src;
1020
1021     src = g_idle_source_new ();
1022     g_source_set_callback (src, (GSourceFunc) free_client_context, ctx, NULL);
1023     g_source_attach (src, ctx->loop->mainctx);
1024     g_source_unref (src);
1025   } else {
1026     free_client_context (ctx);
1027   }
1028
1029   g_object_unref (server);
1030 }
1031
1032 /* add the client context to the active list of clients, takes ownership
1033  * of client */
1034 static void
1035 manage_client (GstRTSPServer * server, GstRTSPClient * client)
1036 {
1037   ClientContext *ctx;
1038   GstRTSPServerPrivate *priv = server->priv;
1039   GMainContext *mainctx;
1040
1041   GST_DEBUG_OBJECT (server, "manage client %p", client);
1042
1043   ctx = g_slice_new0 (ClientContext);
1044   ctx->server = server;
1045   ctx->client = client;
1046
1047   GST_RTSP_SERVER_LOCK (server);
1048   if (priv->max_threads == 0) {
1049     GSource *source;
1050
1051     /* find the context to add the watch */
1052     if ((source = g_main_current_source ()))
1053       mainctx = g_source_get_context (source);
1054     else
1055       mainctx = NULL;
1056   } else {
1057     ctx->loop = gst_rtsp_server_get_main_loop (server);
1058     mainctx = ctx->loop->mainctx;
1059   }
1060
1061   g_signal_connect (client, "closed", (GCallback) unmanage_client, ctx);
1062   priv->clients = g_list_prepend (priv->clients, ctx);
1063
1064   gst_rtsp_client_attach (client, mainctx);
1065
1066   GST_RTSP_SERVER_UNLOCK (server);
1067 }
1068
1069 static GstRTSPClient *
1070 default_create_client (GstRTSPServer * server)
1071 {
1072   GstRTSPClient *client;
1073   GstRTSPServerPrivate *priv = server->priv;
1074
1075   /* a new client connected, create a session to handle the client. */
1076   client = gst_rtsp_client_new ();
1077
1078   /* set the session pool that this client should use */
1079   GST_RTSP_SERVER_LOCK (server);
1080   gst_rtsp_client_set_session_pool (client, priv->session_pool);
1081   /* set the mount points that this client should use */
1082   gst_rtsp_client_set_mount_points (client, priv->mount_points);
1083   /* set authentication manager */
1084   gst_rtsp_client_set_auth (client, priv->auth);
1085   GST_RTSP_SERVER_UNLOCK (server);
1086
1087   return client;
1088 }
1089
1090 /**
1091  * gst_rtsp_server_transfer_connection:
1092  * @server: a #GstRTSPServer
1093  * @socket: a network socket
1094  * @ip: the IP address of the remote client
1095  * @port: the port used by the other end
1096  * @initial_buffer: any initial data that was already read from the socket
1097  *
1098  * Take an existing network socket and use it for an RTSP connection. This
1099  * is used when transferring a socket from an HTTP server which should be used
1100  * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
1101  * that the HTTP server read from the socket while parsing the HTTP header.
1102  *
1103  * Returns: TRUE if all was ok, FALSE if an error occured.
1104  */
1105 gboolean
1106 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
1107     const gchar * ip, gint port, const gchar * initial_buffer)
1108 {
1109   GstRTSPClient *client = NULL;
1110   GstRTSPServerClass *klass;
1111   GstRTSPConnection *conn;
1112   GstRTSPResult res;
1113
1114   klass = GST_RTSP_SERVER_GET_CLASS (server);
1115
1116   if (klass->create_client)
1117     client = klass->create_client (server);
1118   if (client == NULL)
1119     goto client_failed;
1120
1121   GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
1122           initial_buffer, &conn), no_connection);
1123
1124   /* set connection on the client now */
1125   gst_rtsp_client_set_connection (client, conn);
1126
1127   /* manage the client connection */
1128   manage_client (server, client);
1129
1130   g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1131       client);
1132
1133   return TRUE;
1134
1135   /* ERRORS */
1136 client_failed:
1137   {
1138     GST_ERROR_OBJECT (server, "failed to create a client");
1139     return FALSE;
1140   }
1141 no_connection:
1142   {
1143     gchar *str = gst_rtsp_strresult (res);
1144     GST_ERROR ("could not create connection from socket %p: %s", socket, str);
1145     g_free (str);
1146     return FALSE;
1147   }
1148 }
1149
1150 /**
1151  * gst_rtsp_server_io_func:
1152  * @socket: a #GSocket
1153  * @condition: the condition on @source
1154  * @server: a #GstRTSPServer
1155  *
1156  * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
1157  * new connection on @socket or @server.
1158  *
1159  * Returns: TRUE if the source could be connected, FALSE if an error occured.
1160  */
1161 gboolean
1162 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
1163     GstRTSPServer * server)
1164 {
1165   GstRTSPClient *client = NULL;
1166   GstRTSPServerClass *klass;
1167   GstRTSPResult res;
1168
1169   if (condition & G_IO_IN) {
1170     GstRTSPConnection *conn;
1171
1172     klass = GST_RTSP_SERVER_GET_CLASS (server);
1173
1174     /* a new client connected, create a client object to handle the client. */
1175     if (klass->create_client)
1176       client = klass->create_client (server);
1177     if (client == NULL)
1178       goto client_failed;
1179
1180     /* a new client connected. */
1181     GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, NULL),
1182         accept_failed);
1183
1184     /* set connection on the client now */
1185     gst_rtsp_client_set_connection (client, conn);
1186
1187     /* manage the client connection */
1188     manage_client (server, client);
1189
1190     g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1191         client);
1192   } else {
1193     GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
1194   }
1195   return G_SOURCE_CONTINUE;
1196
1197   /* ERRORS */
1198 client_failed:
1199   {
1200     GST_ERROR_OBJECT (server, "failed to create a client");
1201     return G_SOURCE_CONTINUE;
1202   }
1203 accept_failed:
1204   {
1205     gchar *str = gst_rtsp_strresult (res);
1206     GST_ERROR_OBJECT (server, "Could not accept client on socket %p: %s",
1207         socket, str);
1208     g_free (str);
1209     g_object_unref (client);
1210     return G_SOURCE_CONTINUE;
1211   }
1212 }
1213
1214 static void
1215 watch_destroyed (GstRTSPServer * server)
1216 {
1217   GstRTSPServerPrivate *priv = server->priv;
1218
1219   GST_DEBUG_OBJECT (server, "source destroyed");
1220
1221   g_object_unref (priv->socket);
1222   priv->socket = NULL;
1223   g_object_unref (server);
1224 }
1225
1226 /**
1227  * gst_rtsp_server_create_source:
1228  * @server: a #GstRTSPServer
1229  * @cancellable: a #GCancellable or %NULL.
1230  * @error: a #GError
1231  *
1232  * Create a #GSource for @server. The new source will have a default
1233  * #GSocketSourceFunc of gst_rtsp_server_io_func().
1234  *
1235  * @cancellable if not NULL can be used to cancel the source, which will cause
1236  * the source to trigger, reporting the current condition (which is likely 0
1237  * unless cancellation happened at the same time as a condition change). You can
1238  * check for this in the callback using g_cancellable_is_cancelled().
1239  *
1240  * Returns: the #GSource for @server or NULL when an error occured. Free with
1241  * g_source_unref ()
1242  */
1243 GSource *
1244 gst_rtsp_server_create_source (GstRTSPServer * server,
1245     GCancellable * cancellable, GError ** error)
1246 {
1247   GstRTSPServerPrivate *priv;
1248   GSocket *socket, *old;
1249   GSource *source;
1250
1251   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1252
1253   priv = server->priv;
1254
1255   socket = gst_rtsp_server_create_socket (server, NULL, error);
1256   if (socket == NULL)
1257     goto no_socket;
1258
1259   GST_RTSP_SERVER_LOCK (server);
1260   old = priv->socket;
1261   priv->socket = g_object_ref (socket);
1262   GST_RTSP_SERVER_UNLOCK (server);
1263
1264   if (old)
1265     g_object_unref (old);
1266
1267   /* create a watch for reads (new connections) and possible errors */
1268   source = g_socket_create_source (socket, G_IO_IN |
1269       G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
1270   g_object_unref (socket);
1271
1272   /* configure the callback */
1273   g_source_set_callback (source,
1274       (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
1275       (GDestroyNotify) watch_destroyed);
1276
1277   return source;
1278
1279 no_socket:
1280   {
1281     GST_ERROR_OBJECT (server, "failed to create socket");
1282     return NULL;
1283   }
1284 }
1285
1286 /**
1287  * gst_rtsp_server_attach:
1288  * @server: a #GstRTSPServer
1289  * @context: (allow-none): a #GMainContext
1290  *
1291  * Attaches @server to @context. When the mainloop for @context is run, the
1292  * server will be dispatched. When @context is NULL, the default context will be
1293  * used).
1294  *
1295  * This function should be called when the server properties and urls are fully
1296  * configured and the server is ready to start.
1297  *
1298  * Returns: the ID (greater than 0) for the source within the GMainContext.
1299  */
1300 guint
1301 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1302 {
1303   guint res;
1304   GSource *source;
1305   GError *error = NULL;
1306
1307   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1308
1309   source = gst_rtsp_server_create_source (server, NULL, &error);
1310   if (source == NULL)
1311     goto no_source;
1312
1313   res = g_source_attach (source, context);
1314   g_source_unref (source);
1315
1316   return res;
1317
1318   /* ERRORS */
1319 no_source:
1320   {
1321     GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1322     g_error_free (error);
1323     return 0;
1324   }
1325 }