docs: update docs
[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   gboolean use_client_settings;
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   /* resource manager */
55   GstRTSPThreadPool *thread_pool;
56
57   /* the TLS certificate */
58   GTlsCertificate *certificate;
59
60   /* the clients that are connected */
61   GList *clients;
62 };
63
64 #define DEFAULT_ADDRESS         "0.0.0.0"
65 #define DEFAULT_BOUND_PORT      -1
66 /* #define DEFAULT_ADDRESS         "::0" */
67 #define DEFAULT_SERVICE         "8554"
68 #define DEFAULT_BACKLOG         5
69 #define DEFAULT_USE_CLIENT_SETTINGS     FALSE
70
71 /* Define to use the SO_LINGER option so that the server sockets can be resused
72  * sooner. Disabled for now because it is not very well implemented by various
73  * OSes and it causes clients to fail to read the TEARDOWN response. */
74 #undef USE_SOLINGER
75
76 enum
77 {
78   PROP_0,
79   PROP_ADDRESS,
80   PROP_SERVICE,
81   PROP_BOUND_PORT,
82   PROP_BACKLOG,
83
84   PROP_SESSION_POOL,
85   PROP_MOUNT_POINTS,
86   PROP_USE_CLIENT_SETTINGS,
87   PROP_LAST
88 };
89
90 enum
91 {
92   SIGNAL_CLIENT_CONNECTED,
93   SIGNAL_LAST
94 };
95
96 G_DEFINE_TYPE (GstRTSPServer, gst_rtsp_server, G_TYPE_OBJECT);
97
98 GST_DEBUG_CATEGORY_STATIC (rtsp_server_debug);
99 #define GST_CAT_DEFAULT rtsp_server_debug
100
101 typedef struct _ClientContext ClientContext;
102 typedef struct _Loop Loop;
103
104 static guint gst_rtsp_server_signals[SIGNAL_LAST] = { 0 };
105
106 static void gst_rtsp_server_get_property (GObject * object, guint propid,
107     GValue * value, GParamSpec * pspec);
108 static void gst_rtsp_server_set_property (GObject * object, guint propid,
109     const GValue * value, GParamSpec * pspec);
110 static void gst_rtsp_server_finalize (GObject * object);
111
112 static GstRTSPClient *default_create_client (GstRTSPServer * server);
113 static gboolean default_setup_connection (GstRTSPServer * server,
114     GstRTSPClient * client, GstRTSPConnection * conn);
115
116 static void
117 gst_rtsp_server_class_init (GstRTSPServerClass * klass)
118 {
119   GObjectClass *gobject_class;
120
121   g_type_class_add_private (klass, sizeof (GstRTSPServerPrivate));
122
123   gobject_class = G_OBJECT_CLASS (klass);
124
125   gobject_class->get_property = gst_rtsp_server_get_property;
126   gobject_class->set_property = gst_rtsp_server_set_property;
127   gobject_class->finalize = gst_rtsp_server_finalize;
128
129   /**
130    * GstRTSPServer::address:
131    *
132    * The address of the server. This is the address where the server will
133    * listen on.
134    */
135   g_object_class_install_property (gobject_class, PROP_ADDRESS,
136       g_param_spec_string ("address", "Address",
137           "The address the server uses to listen on", DEFAULT_ADDRESS,
138           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
139   /**
140    * GstRTSPServer::service:
141    *
142    * The service of the server. This is either a string with the service name or
143    * a port number (as a string) the server will listen on.
144    */
145   g_object_class_install_property (gobject_class, PROP_SERVICE,
146       g_param_spec_string ("service", "Service",
147           "The service or port number the server uses to listen on",
148           DEFAULT_SERVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
149   /**
150    * GstRTSPServer::bound-port:
151    *
152    * The actual port the server is listening on. Can be used to retrieve the
153    * port number when the server is started on port 0, which means bind to a
154    * random port. Set to -1 if the server has not been bound yet.
155    */
156   g_object_class_install_property (gobject_class, PROP_BOUND_PORT,
157       g_param_spec_int ("bound-port", "Bound port",
158           "The port number the server is listening on",
159           -1, G_MAXUINT16, DEFAULT_BOUND_PORT,
160           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
161   /**
162    * GstRTSPServer::backlog:
163    *
164    * The backlog argument defines the maximum length to which the queue of
165    * pending connections for the server may grow. If a connection request arrives
166    * when the queue is full, the client may receive an error with an indication of
167    * ECONNREFUSED or, if the underlying protocol supports retransmission, the
168    * request may be ignored so that a later reattempt at  connection succeeds.
169    */
170   g_object_class_install_property (gobject_class, PROP_BACKLOG,
171       g_param_spec_int ("backlog", "Backlog",
172           "The maximum length to which the queue "
173           "of pending connections may grow", 0, G_MAXINT, DEFAULT_BACKLOG,
174           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175   /**
176    * GstRTSPServer::session-pool:
177    *
178    * The session pool of the server. By default each server has a separate
179    * session pool but sessions can be shared between servers by setting the same
180    * session pool on multiple servers.
181    */
182   g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
183       g_param_spec_object ("session-pool", "Session Pool",
184           "The session pool to use for client session",
185           GST_TYPE_RTSP_SESSION_POOL,
186           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187   /**
188    * GstRTSPServer::mount-points:
189    *
190    * The mount points to use for this server. By default the server has no
191    * mount points and thus cannot map urls to media streams.
192    */
193   g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
194       g_param_spec_object ("mount-points", "Mount Points",
195           "The mount points to use for client session",
196           GST_TYPE_RTSP_MOUNT_POINTS,
197           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198   /**
199    * GstRTSPServer::use-client-settings:
200    *
201    * Use client transport settings (destination, port pair and ttl for
202    * multicast. FALSE means that the server settings will be used.
203    */
204   g_object_class_install_property (gobject_class, PROP_USE_CLIENT_SETTINGS,
205       g_param_spec_boolean ("use-client-settings", "Use Client Settings",
206           "Use client settings for ttl, destination and port pair in multicast",
207           DEFAULT_USE_CLIENT_SETTINGS,
208           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   GST_DEBUG_CATEGORY_INIT (rtsp_server_debug, "rtspserver", 0, "GstRTSPServer");
220 }
221
222 static void
223 gst_rtsp_server_init (GstRTSPServer * server)
224 {
225   GstRTSPServerPrivate *priv = GST_RTSP_SERVER_GET_PRIVATE (server);
226
227   server->priv = priv;
228
229   g_mutex_init (&priv->lock);
230   priv->address = g_strdup (DEFAULT_ADDRESS);
231   priv->service = g_strdup (DEFAULT_SERVICE);
232   priv->socket = NULL;
233   priv->backlog = DEFAULT_BACKLOG;
234   priv->session_pool = gst_rtsp_session_pool_new ();
235   priv->mount_points = gst_rtsp_mount_points_new ();
236   priv->thread_pool = gst_rtsp_thread_pool_new ();
237   priv->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
238 }
239
240 static void
241 gst_rtsp_server_finalize (GObject * object)
242 {
243   GstRTSPServer *server = GST_RTSP_SERVER (object);
244   GstRTSPServerPrivate *priv = server->priv;
245
246   GST_DEBUG_OBJECT (server, "finalize server");
247
248   g_free (priv->address);
249   g_free (priv->service);
250
251   if (priv->socket)
252     g_object_unref (priv->socket);
253
254   if (priv->session_pool)
255     g_object_unref (priv->session_pool);
256   if (priv->mount_points)
257     g_object_unref (priv->mount_points);
258   if (priv->thread_pool)
259     g_object_unref (priv->thread_pool);
260
261   if (priv->auth)
262     g_object_unref (priv->auth);
263
264   if (priv->certificate)
265     g_object_unref (priv->certificate);
266
267   g_mutex_clear (&priv->lock);
268
269   G_OBJECT_CLASS (gst_rtsp_server_parent_class)->finalize (object);
270 }
271
272 /**
273  * gst_rtsp_server_new:
274  *
275  * Create a new #GstRTSPServer instance.
276  */
277 GstRTSPServer *
278 gst_rtsp_server_new (void)
279 {
280   GstRTSPServer *result;
281
282   result = g_object_new (GST_TYPE_RTSP_SERVER, NULL);
283
284   return result;
285 }
286
287 /**
288  * gst_rtsp_server_set_address:
289  * @server: a #GstRTSPServer
290  * @address: the address
291  *
292  * Configure @server to accept connections on the given address.
293  *
294  * This function must be called before the server is bound.
295  */
296 void
297 gst_rtsp_server_set_address (GstRTSPServer * server, const gchar * address)
298 {
299   GstRTSPServerPrivate *priv;
300
301   g_return_if_fail (GST_IS_RTSP_SERVER (server));
302   g_return_if_fail (address != NULL);
303
304   priv = server->priv;
305
306   GST_RTSP_SERVER_LOCK (server);
307   g_free (priv->address);
308   priv->address = g_strdup (address);
309   GST_RTSP_SERVER_UNLOCK (server);
310 }
311
312 /**
313  * gst_rtsp_server_get_address:
314  * @server: a #GstRTSPServer
315  *
316  * Get the address on which the server will accept connections.
317  *
318  * Returns: the server address. g_free() after usage.
319  */
320 gchar *
321 gst_rtsp_server_get_address (GstRTSPServer * server)
322 {
323   GstRTSPServerPrivate *priv;
324   gchar *result;
325
326   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
327
328   priv = server->priv;
329
330   GST_RTSP_SERVER_LOCK (server);
331   result = g_strdup (priv->address);
332   GST_RTSP_SERVER_UNLOCK (server);
333
334   return result;
335 }
336
337 /**
338  * gst_rtsp_server_get_bound_port:
339  * @server: a #GstRTSPServer
340  *
341  * Get the port number where the server was bound to.
342  *
343  * Returns: the port number
344  */
345 int
346 gst_rtsp_server_get_bound_port (GstRTSPServer * server)
347 {
348   GstRTSPServerPrivate *priv;
349   GSocketAddress *address;
350   int result = -1;
351
352   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), result);
353
354   priv = server->priv;
355
356   GST_RTSP_SERVER_LOCK (server);
357   if (priv->socket == NULL)
358     goto out;
359
360   address = g_socket_get_local_address (priv->socket, NULL);
361   result = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
362   g_object_unref (address);
363
364 out:
365   GST_RTSP_SERVER_UNLOCK (server);
366
367   return result;
368 }
369
370 /**
371  * gst_rtsp_server_set_service:
372  * @server: a #GstRTSPServer
373  * @service: the service
374  *
375  * Configure @server to accept connections on the given service.
376  * @service should be a string containing the service name (see services(5)) or
377  * a string containing a port number between 1 and 65535.
378  *
379  * This function must be called before the server is bound.
380  */
381 void
382 gst_rtsp_server_set_service (GstRTSPServer * server, const gchar * service)
383 {
384   GstRTSPServerPrivate *priv;
385
386   g_return_if_fail (GST_IS_RTSP_SERVER (server));
387   g_return_if_fail (service != NULL);
388
389   priv = server->priv;
390
391   GST_RTSP_SERVER_LOCK (server);
392   g_free (priv->service);
393   priv->service = g_strdup (service);
394   GST_RTSP_SERVER_UNLOCK (server);
395 }
396
397 /**
398  * gst_rtsp_server_get_service:
399  * @server: a #GstRTSPServer
400  *
401  * Get the service on which the server will accept connections.
402  *
403  * Returns: the service. use g_free() after usage.
404  */
405 gchar *
406 gst_rtsp_server_get_service (GstRTSPServer * server)
407 {
408   GstRTSPServerPrivate *priv;
409   gchar *result;
410
411   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
412
413   priv = server->priv;
414
415   GST_RTSP_SERVER_LOCK (server);
416   result = g_strdup (priv->service);
417   GST_RTSP_SERVER_UNLOCK (server);
418
419   return result;
420 }
421
422 /**
423  * gst_rtsp_server_set_backlog:
424  * @server: a #GstRTSPServer
425  * @backlog: the backlog
426  *
427  * configure the maximum amount of requests that may be queued for the
428  * server.
429  *
430  * This function must be called before the server is bound.
431  */
432 void
433 gst_rtsp_server_set_backlog (GstRTSPServer * server, gint backlog)
434 {
435   GstRTSPServerPrivate *priv;
436
437   g_return_if_fail (GST_IS_RTSP_SERVER (server));
438
439   priv = server->priv;
440
441   GST_RTSP_SERVER_LOCK (server);
442   priv->backlog = backlog;
443   GST_RTSP_SERVER_UNLOCK (server);
444 }
445
446 /**
447  * gst_rtsp_server_get_backlog:
448  * @server: a #GstRTSPServer
449  *
450  * The maximum amount of queued requests for the server.
451  *
452  * Returns: the server backlog.
453  */
454 gint
455 gst_rtsp_server_get_backlog (GstRTSPServer * server)
456 {
457   GstRTSPServerPrivate *priv;
458   gint result;
459
460   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), -1);
461
462   priv = server->priv;
463
464   GST_RTSP_SERVER_LOCK (server);
465   result = priv->backlog;
466   GST_RTSP_SERVER_UNLOCK (server);
467
468   return result;
469 }
470
471 /**
472  * gst_rtsp_server_set_session_pool:
473  * @server: a #GstRTSPServer
474  * @pool: a #GstRTSPSessionPool
475  *
476  * configure @pool to be used as the session pool of @server.
477  */
478 void
479 gst_rtsp_server_set_session_pool (GstRTSPServer * server,
480     GstRTSPSessionPool * pool)
481 {
482   GstRTSPServerPrivate *priv;
483   GstRTSPSessionPool *old;
484
485   g_return_if_fail (GST_IS_RTSP_SERVER (server));
486
487   priv = server->priv;
488
489   if (pool)
490     g_object_ref (pool);
491
492   GST_RTSP_SERVER_LOCK (server);
493   old = priv->session_pool;
494   priv->session_pool = pool;
495   GST_RTSP_SERVER_UNLOCK (server);
496
497   if (old)
498     g_object_unref (old);
499 }
500
501 /**
502  * gst_rtsp_server_get_session_pool:
503  * @server: a #GstRTSPServer
504  *
505  * Get the #GstRTSPSessionPool used as the session pool of @server.
506  *
507  * Returns: (transfer full): the #GstRTSPSessionPool used for sessions. g_object_unref() after
508  * usage.
509  */
510 GstRTSPSessionPool *
511 gst_rtsp_server_get_session_pool (GstRTSPServer * server)
512 {
513   GstRTSPServerPrivate *priv;
514   GstRTSPSessionPool *result;
515
516   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
517
518   priv = server->priv;
519
520   GST_RTSP_SERVER_LOCK (server);
521   if ((result = priv->session_pool))
522     g_object_ref (result);
523   GST_RTSP_SERVER_UNLOCK (server);
524
525   return result;
526 }
527
528 /**
529  * gst_rtsp_server_set_mount_points:
530  * @server: a #GstRTSPServer
531  * @mounts: a #GstRTSPMountPoints
532  *
533  * configure @mounts to be used as the mount points of @server.
534  */
535 void
536 gst_rtsp_server_set_mount_points (GstRTSPServer * server,
537     GstRTSPMountPoints * mounts)
538 {
539   GstRTSPServerPrivate *priv;
540   GstRTSPMountPoints *old;
541
542   g_return_if_fail (GST_IS_RTSP_SERVER (server));
543
544   priv = server->priv;
545
546   if (mounts)
547     g_object_ref (mounts);
548
549   GST_RTSP_SERVER_LOCK (server);
550   old = priv->mount_points;
551   priv->mount_points = mounts;
552   GST_RTSP_SERVER_UNLOCK (server);
553
554   if (old)
555     g_object_unref (old);
556 }
557
558
559 /**
560  * gst_rtsp_server_get_mount_points:
561  * @server: a #GstRTSPServer
562  *
563  * Get the #GstRTSPMountPoints used as the mount points of @server.
564  *
565  * Returns: (transfer full): the #GstRTSPMountPoints of @server. g_object_unref() after
566  * usage.
567  */
568 GstRTSPMountPoints *
569 gst_rtsp_server_get_mount_points (GstRTSPServer * server)
570 {
571   GstRTSPServerPrivate *priv;
572   GstRTSPMountPoints *result;
573
574   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
575
576   priv = server->priv;
577
578   GST_RTSP_SERVER_LOCK (server);
579   if ((result = priv->mount_points))
580     g_object_ref (result);
581   GST_RTSP_SERVER_UNLOCK (server);
582
583   return result;
584 }
585
586 /**
587  * gst_rtsp_server_set_auth:
588  * @server: a #GstRTSPServer
589  * @auth: a #GstRTSPAuth
590  *
591  * configure @auth to be used as the authentication manager of @server.
592  */
593 void
594 gst_rtsp_server_set_auth (GstRTSPServer * server, GstRTSPAuth * auth)
595 {
596   GstRTSPServerPrivate *priv;
597   GstRTSPAuth *old;
598
599   g_return_if_fail (GST_IS_RTSP_SERVER (server));
600
601   priv = server->priv;
602
603   if (auth)
604     g_object_ref (auth);
605
606   GST_RTSP_SERVER_LOCK (server);
607   old = priv->auth;
608   priv->auth = auth;
609   GST_RTSP_SERVER_UNLOCK (server);
610
611   if (old)
612     g_object_unref (old);
613 }
614
615
616 /**
617  * gst_rtsp_server_get_auth:
618  * @server: a #GstRTSPServer
619  *
620  * Get the #GstRTSPAuth used as the authentication manager of @server.
621  *
622  * Returns: (transfer full): the #GstRTSPAuth of @server. g_object_unref() after
623  * usage.
624  */
625 GstRTSPAuth *
626 gst_rtsp_server_get_auth (GstRTSPServer * server)
627 {
628   GstRTSPServerPrivate *priv;
629   GstRTSPAuth *result;
630
631   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
632
633   priv = server->priv;
634
635   GST_RTSP_SERVER_LOCK (server);
636   if ((result = priv->auth))
637     g_object_ref (result);
638   GST_RTSP_SERVER_UNLOCK (server);
639
640   return result;
641 }
642
643 /**
644  * gst_rtsp_server_set_thread_pool:
645  * @server: a #GstRTSPServer
646  * @pool: a #GstRTSPThreadPool
647  *
648  * configure @pool to be used as the thread pool of @server.
649  */
650 void
651 gst_rtsp_server_set_thread_pool (GstRTSPServer * server,
652     GstRTSPThreadPool * pool)
653 {
654   GstRTSPServerPrivate *priv;
655   GstRTSPThreadPool *old;
656
657   g_return_if_fail (GST_IS_RTSP_SERVER (server));
658
659   priv = server->priv;
660
661   if (pool)
662     g_object_ref (pool);
663
664   GST_RTSP_SERVER_LOCK (server);
665   old = priv->thread_pool;
666   priv->thread_pool = pool;
667   GST_RTSP_SERVER_UNLOCK (server);
668
669   if (old)
670     g_object_unref (old);
671 }
672
673 /**
674  * gst_rtsp_server_get_thread_pool:
675  * @server: a #GstRTSPServer
676  *
677  * Get the #GstRTSPThreadPool used as the thread pool of @server.
678  *
679  * Returns: (transfer full): the #GstRTSPThreadPool of @server. g_object_unref() after
680  * usage.
681  */
682 GstRTSPThreadPool *
683 gst_rtsp_server_get_thread_pool (GstRTSPServer * server)
684 {
685   GstRTSPServerPrivate *priv;
686   GstRTSPThreadPool *result;
687
688   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
689
690   priv = server->priv;
691
692   GST_RTSP_SERVER_LOCK (server);
693   if ((result = priv->thread_pool))
694     g_object_ref (result);
695   GST_RTSP_SERVER_UNLOCK (server);
696
697   return result;
698 }
699
700 /**
701  * gst_rtsp_server_set_use_client_settings:
702  * @server: a #GstRTSPServer
703  * @use_client_settings: whether to use client settings for multicast
704  *
705  * Use client transport settings (destination, port pair and ttl) for
706  * multicast.
707  * When @use_client_settings is %FALSE, the server settings will be
708  * used.
709  */
710 void
711 gst_rtsp_server_set_use_client_settings (GstRTSPServer * server,
712     gboolean use_client_settings)
713 {
714   GstRTSPServerPrivate *priv;
715
716   g_return_if_fail (GST_IS_RTSP_SERVER (server));
717
718   priv = server->priv;
719
720   GST_RTSP_SERVER_LOCK (server);
721   priv->use_client_settings = use_client_settings;
722   GST_RTSP_SERVER_UNLOCK (server);
723 }
724
725 /**
726  * gst_rtsp_server_get_use_client_settings:
727  * @server: a #GstRTSPServer
728  *
729  * Check if client transport settings (destination, port pair and ttl) for
730  * multicast will be used.
731  */
732 gboolean
733 gst_rtsp_server_get_use_client_settings (GstRTSPServer * server)
734 {
735   GstRTSPServerPrivate *priv;
736   gboolean res;
737
738   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), FALSE);
739
740   priv = server->priv;
741
742   GST_RTSP_SERVER_LOCK (server);
743   res = priv->use_client_settings;
744   GST_RTSP_SERVER_UNLOCK (server);
745
746   return res;
747 }
748
749 /**
750  * gst_rtsp_server_set_tls_certificate:
751  * @server: a #GstRTSPServer
752  * @cert: (allow none): a #GTlsCertificate
753  *
754  * Set the TLS certificate for the server. Client connections will only
755  * be accepted when TLS is negotiated.
756  */
757 void
758 gst_rtsp_server_set_tls_certificate (GstRTSPServer * server,
759     GTlsCertificate * cert)
760 {
761   GstRTSPServerPrivate *priv;
762   GTlsCertificate *old;
763
764   g_return_if_fail (GST_IS_RTSP_SERVER (server));
765
766   priv = server->priv;
767
768   if (cert)
769     g_object_ref (cert);
770
771   GST_RTSP_SERVER_LOCK (server);
772   old = priv->certificate;
773   priv->certificate = cert;
774   GST_RTSP_SERVER_UNLOCK (server);
775
776   if (old)
777     g_object_unref (old);
778 }
779
780 /**
781  * gst_rtsp_server_get_tls_certificate:
782  * @server: a #GstRTSPServer
783  *
784  * Get the #GTlsCertificate used for negotiating TLS @server.
785  *
786  * Returns: (transfer full): the #GTlsCertificate of @server. g_object_unref() after
787  * usage.
788  */
789 GTlsCertificate *
790 gst_rtsp_server_get_tls_certificate (GstRTSPServer * server)
791 {
792   GstRTSPServerPrivate *priv;
793   GTlsCertificate *result;
794
795   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
796
797   priv = server->priv;
798
799   GST_RTSP_SERVER_LOCK (server);
800   if ((result = priv->certificate))
801     g_object_ref (result);
802   GST_RTSP_SERVER_UNLOCK (server);
803
804   return result;
805 }
806
807 static void
808 gst_rtsp_server_get_property (GObject * object, guint propid,
809     GValue * value, GParamSpec * pspec)
810 {
811   GstRTSPServer *server = GST_RTSP_SERVER (object);
812
813   switch (propid) {
814     case PROP_ADDRESS:
815       g_value_take_string (value, gst_rtsp_server_get_address (server));
816       break;
817     case PROP_SERVICE:
818       g_value_take_string (value, gst_rtsp_server_get_service (server));
819       break;
820     case PROP_BOUND_PORT:
821       g_value_set_int (value, gst_rtsp_server_get_bound_port (server));
822       break;
823     case PROP_BACKLOG:
824       g_value_set_int (value, gst_rtsp_server_get_backlog (server));
825       break;
826     case PROP_SESSION_POOL:
827       g_value_take_object (value, gst_rtsp_server_get_session_pool (server));
828       break;
829     case PROP_MOUNT_POINTS:
830       g_value_take_object (value, gst_rtsp_server_get_mount_points (server));
831       break;
832     case PROP_USE_CLIENT_SETTINGS:
833       g_value_set_boolean (value,
834           gst_rtsp_server_get_use_client_settings (server));
835       break;
836     default:
837       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
838   }
839 }
840
841 static void
842 gst_rtsp_server_set_property (GObject * object, guint propid,
843     const GValue * value, GParamSpec * pspec)
844 {
845   GstRTSPServer *server = GST_RTSP_SERVER (object);
846
847   switch (propid) {
848     case PROP_ADDRESS:
849       gst_rtsp_server_set_address (server, g_value_get_string (value));
850       break;
851     case PROP_SERVICE:
852       gst_rtsp_server_set_service (server, g_value_get_string (value));
853       break;
854     case PROP_BACKLOG:
855       gst_rtsp_server_set_backlog (server, g_value_get_int (value));
856       break;
857     case PROP_SESSION_POOL:
858       gst_rtsp_server_set_session_pool (server, g_value_get_object (value));
859       break;
860     case PROP_MOUNT_POINTS:
861       gst_rtsp_server_set_mount_points (server, g_value_get_object (value));
862       break;
863     case PROP_USE_CLIENT_SETTINGS:
864       gst_rtsp_server_set_use_client_settings (server,
865           g_value_get_boolean (value));
866       break;
867     default:
868       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
869   }
870 }
871
872 /**
873  * gst_rtsp_server_create_socket:
874  * @server: a #GstRTSPServer
875  * @cancellable: a #GCancellable
876  * @error: a #GError
877  *
878  * Create a #GSocket for @server. The socket will listen on the
879  * configured service.
880  *
881  * Returns: (transfer full): the #GSocket for @server or NULL when an error occured.
882  */
883 GSocket *
884 gst_rtsp_server_create_socket (GstRTSPServer * server,
885     GCancellable * cancellable, GError ** error)
886 {
887   GstRTSPServerPrivate *priv;
888   GSocketConnectable *conn;
889   GSocketAddressEnumerator *enumerator;
890   GSocket *socket = NULL;
891 #ifdef USE_SOLINGER
892   struct linger linger;
893 #endif
894   GError *sock_error = NULL;
895   GError *bind_error = NULL;
896   guint16 port;
897
898   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
899
900   priv = server->priv;
901
902   GST_RTSP_SERVER_LOCK (server);
903   GST_DEBUG_OBJECT (server, "getting address info of %s/%s", priv->address,
904       priv->service);
905
906   /* resolve the server IP address */
907   port = atoi (priv->service);
908   if (port != 0 || !strcmp (priv->service, "0"))
909     conn = g_network_address_new (priv->address, port);
910   else
911     conn = g_network_service_new (priv->service, "tcp", priv->address);
912
913   enumerator = g_socket_connectable_enumerate (conn);
914   g_object_unref (conn);
915
916   /* create server socket, we loop through all the addresses until we manage to
917    * create a socket and bind. */
918   while (TRUE) {
919     GSocketAddress *sockaddr;
920
921     sockaddr =
922         g_socket_address_enumerator_next (enumerator, cancellable, error);
923     if (!sockaddr) {
924       if (!*error)
925         GST_DEBUG_OBJECT (server, "no more addresses %s",
926             *error ? (*error)->message : "");
927       else
928         GST_DEBUG_OBJECT (server, "failed to retrieve next address %s",
929             (*error)->message);
930       break;
931     }
932
933     /* only keep the first error */
934     socket = g_socket_new (g_socket_address_get_family (sockaddr),
935         G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP,
936         sock_error ? NULL : &sock_error);
937
938     if (socket == NULL) {
939       GST_DEBUG_OBJECT (server, "failed to make socket (%s), try next",
940           sock_error->message);
941       g_object_unref (sockaddr);
942       continue;
943     }
944
945     if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
946       g_object_unref (sockaddr);
947       break;
948     }
949
950     GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
951         bind_error->message);
952     g_object_unref (sockaddr);
953     g_object_unref (socket);
954     socket = NULL;
955   }
956   g_object_unref (enumerator);
957
958   if (socket == NULL)
959     goto no_socket;
960
961   g_clear_error (&sock_error);
962   g_clear_error (&bind_error);
963
964   GST_DEBUG_OBJECT (server, "opened sending server socket");
965
966   /* keep connection alive; avoids SIGPIPE during write */
967   g_socket_set_keepalive (socket, TRUE);
968
969 #if 0
970 #ifdef USE_SOLINGER
971   /* make sure socket is reset 5 seconds after close. This ensure that we can
972    * reuse the socket quickly while still having a chance to send data to the
973    * client. */
974   linger.l_onoff = 1;
975   linger.l_linger = 5;
976   if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
977           (void *) &linger, sizeof (linger)) < 0)
978     goto linger_failed;
979 #endif
980 #endif
981
982   /* set the server socket to nonblocking */
983   g_socket_set_blocking (socket, FALSE);
984
985   /* set listen backlog */
986   g_socket_set_listen_backlog (socket, priv->backlog);
987
988   if (!g_socket_listen (socket, error))
989     goto listen_failed;
990
991   GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
992       socket, priv->backlog);
993
994   GST_RTSP_SERVER_UNLOCK (server);
995
996   return socket;
997
998   /* ERRORS */
999 no_socket:
1000   {
1001     GST_ERROR_OBJECT (server, "failed to create socket");
1002     goto close_error;
1003   }
1004 #if 0
1005 #ifdef USE_SOLINGER
1006 linger_failed:
1007   {
1008     GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
1009         g_strerror (errno));
1010     goto close_error;
1011   }
1012 #endif
1013 #endif
1014 listen_failed:
1015   {
1016     GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
1017         (*error)->message);
1018     goto close_error;
1019   }
1020 close_error:
1021   {
1022     if (socket)
1023       g_object_unref (socket);
1024
1025     if (sock_error) {
1026       if (error == NULL)
1027         g_propagate_error (error, sock_error);
1028       else
1029         g_error_free (sock_error);
1030     }
1031     if (bind_error) {
1032       if ((error == NULL) || (*error == NULL))
1033         g_propagate_error (error, bind_error);
1034       else
1035         g_error_free (bind_error);
1036     }
1037     GST_RTSP_SERVER_UNLOCK (server);
1038     return NULL;
1039   }
1040 }
1041
1042 struct _ClientContext
1043 {
1044   GstRTSPServer *server;
1045   GstRTSPThread *thread;
1046   GstRTSPClient *client;
1047 };
1048
1049 static gboolean
1050 free_client_context (ClientContext * ctx)
1051 {
1052   GST_RTSP_SERVER_LOCK (ctx->server);
1053   if (ctx->thread)
1054     gst_rtsp_thread_stop (ctx->thread);
1055   GST_RTSP_SERVER_UNLOCK (ctx->server);
1056
1057   g_object_unref (ctx->client);
1058   g_slice_free (ClientContext, ctx);
1059
1060   return G_SOURCE_REMOVE;
1061 }
1062
1063 static void
1064 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
1065 {
1066   GstRTSPServer *server = ctx->server;
1067   GstRTSPServerPrivate *priv = server->priv;
1068
1069   GST_DEBUG_OBJECT (server, "unmanage client %p", client);
1070
1071   g_object_ref (server);
1072
1073   GST_RTSP_SERVER_LOCK (server);
1074   priv->clients = g_list_remove (priv->clients, ctx);
1075   GST_RTSP_SERVER_UNLOCK (server);
1076
1077   if (ctx->thread) {
1078     GSource *src;
1079
1080     src = g_idle_source_new ();
1081     g_source_set_callback (src, (GSourceFunc) free_client_context, ctx, NULL);
1082     g_source_attach (src, ctx->thread->context);
1083     g_source_unref (src);
1084   } else {
1085     free_client_context (ctx);
1086   }
1087
1088   g_object_unref (server);
1089 }
1090
1091 /* add the client context to the active list of clients, takes ownership
1092  * of client */
1093 static void
1094 manage_client (GstRTSPServer * server, GstRTSPClient * client)
1095 {
1096   ClientContext *ctx;
1097   GstRTSPServerPrivate *priv = server->priv;
1098   GMainContext *mainctx = NULL;
1099   GstRTSPClientState state = { NULL };
1100
1101   GST_DEBUG_OBJECT (server, "manage client %p", client);
1102
1103   ctx = g_slice_new0 (ClientContext);
1104   ctx->server = server;
1105   ctx->client = client;
1106
1107   GST_RTSP_SERVER_LOCK (server);
1108
1109   state.server = server;
1110   state.client = client;
1111
1112   ctx->thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
1113       GST_RTSP_THREAD_TYPE_CLIENT, &state);
1114   if (ctx->thread)
1115     mainctx = ctx->thread->context;
1116   else {
1117     GSource *source;
1118     /* find the context to add the watch */
1119     if ((source = g_main_current_source ()))
1120       mainctx = g_source_get_context (source);
1121   }
1122
1123   g_signal_connect (client, "closed", (GCallback) unmanage_client, ctx);
1124   priv->clients = g_list_prepend (priv->clients, ctx);
1125
1126   gst_rtsp_client_attach (client, mainctx);
1127
1128   GST_RTSP_SERVER_UNLOCK (server);
1129 }
1130
1131 static GstRTSPClient *
1132 default_create_client (GstRTSPServer * server)
1133 {
1134   GstRTSPClient *client;
1135   GstRTSPServerPrivate *priv = server->priv;
1136
1137   /* a new client connected, create a session to handle the client. */
1138   client = gst_rtsp_client_new ();
1139
1140   /* set the session pool that this client should use */
1141   GST_RTSP_SERVER_LOCK (server);
1142   gst_rtsp_client_set_session_pool (client, priv->session_pool);
1143   /* set the mount points that this client should use */
1144   gst_rtsp_client_set_mount_points (client, priv->mount_points);
1145   /* set authentication manager */
1146   gst_rtsp_client_set_auth (client, priv->auth);
1147   /* set threadpool */
1148   gst_rtsp_client_set_thread_pool (client, priv->thread_pool);
1149   /* check if client transport settings for multicast are allowed */
1150   gst_rtsp_client_set_use_client_settings (client, priv->use_client_settings);
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 }