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