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