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