stream: release some locks in error cases
[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       /* ask what port the socket has been bound to */
846       if (port == 0 || !strcmp (priv->service, "0")) {
847         GError *addr_error = NULL;
848
849         g_object_unref (sockaddr);
850         sockaddr = g_socket_get_local_address (socket, &addr_error);
851
852         if (addr_error != NULL) {
853           GST_DEBUG_OBJECT (server,
854               "failed to get the local address of a bound socket %s",
855               addr_error->message);
856           g_clear_error (&addr_error);
857           break;
858         }
859         port =
860             g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr));
861
862         if (port != 0) {
863           g_free (priv->service);
864           priv->service = g_strdup_printf ("%d", port);
865         } else {
866           GST_DEBUG_OBJECT (server, "failed to get the port of a bound socket");
867         }
868       }
869       g_object_unref (sockaddr);
870       break;
871     }
872
873     GST_DEBUG_OBJECT (server, "failed to bind socket (%s), try next",
874         bind_error->message);
875     g_object_unref (sockaddr);
876     g_object_unref (socket);
877     socket = NULL;
878   }
879   g_object_unref (enumerator);
880
881   if (socket == NULL)
882     goto no_socket;
883
884   g_clear_error (&sock_error);
885   g_clear_error (&bind_error);
886
887   GST_DEBUG_OBJECT (server, "opened sending server socket");
888
889   /* keep connection alive; avoids SIGPIPE during write */
890   g_socket_set_keepalive (socket, TRUE);
891
892 #if 0
893 #ifdef USE_SOLINGER
894   /* make sure socket is reset 5 seconds after close. This ensure that we can
895    * reuse the socket quickly while still having a chance to send data to the
896    * client. */
897   linger.l_onoff = 1;
898   linger.l_linger = 5;
899   if (setsockopt (sockfd, SOL_SOCKET, SO_LINGER,
900           (void *) &linger, sizeof (linger)) < 0)
901     goto linger_failed;
902 #endif
903 #endif
904
905   /* set the server socket to nonblocking */
906   g_socket_set_blocking (socket, FALSE);
907
908   /* set listen backlog */
909   g_socket_set_listen_backlog (socket, priv->backlog);
910
911   if (!g_socket_listen (socket, error))
912     goto listen_failed;
913
914   GST_DEBUG_OBJECT (server, "listening on server socket %p with queue of %d",
915       socket, priv->backlog);
916
917   GST_RTSP_SERVER_UNLOCK (server);
918
919   return socket;
920
921   /* ERRORS */
922 no_socket:
923   {
924     GST_ERROR_OBJECT (server, "failed to create socket");
925     goto close_error;
926   }
927 #if 0
928 #ifdef USE_SOLINGER
929 linger_failed:
930   {
931     GST_ERROR_OBJECT (server, "failed to no linger socket: %s",
932         g_strerror (errno));
933     goto close_error;
934   }
935 #endif
936 #endif
937 listen_failed:
938   {
939     GST_ERROR_OBJECT (server, "failed to listen on socket: %s",
940         (*error)->message);
941     goto close_error;
942   }
943 close_error:
944   {
945     if (socket)
946       g_object_unref (socket);
947
948     if (sock_error) {
949       if (error == NULL)
950         g_propagate_error (error, sock_error);
951       else
952         g_error_free (sock_error);
953     }
954     if (bind_error) {
955       if ((error == NULL) || (*error == NULL))
956         g_propagate_error (error, bind_error);
957       else
958         g_error_free (bind_error);
959     }
960     GST_RTSP_SERVER_UNLOCK (server);
961     return NULL;
962   }
963 }
964
965 struct _ClientContext
966 {
967   GstRTSPServer *server;
968   GstRTSPThread *thread;
969   GstRTSPClient *client;
970 };
971
972 static gboolean
973 free_client_context (ClientContext * ctx)
974 {
975   GST_DEBUG ("free context %p", ctx);
976
977   GST_RTSP_SERVER_LOCK (ctx->server);
978   if (ctx->thread)
979     gst_rtsp_thread_stop (ctx->thread);
980   GST_RTSP_SERVER_UNLOCK (ctx->server);
981
982   g_object_unref (ctx->client);
983   g_object_unref (ctx->server);
984   g_slice_free (ClientContext, ctx);
985
986   return G_SOURCE_REMOVE;
987 }
988
989 static void
990 unmanage_client (GstRTSPClient * client, ClientContext * ctx)
991 {
992   GstRTSPServer *server = ctx->server;
993   GstRTSPServerPrivate *priv = server->priv;
994
995   GST_DEBUG_OBJECT (server, "unmanage client %p", client);
996
997   GST_RTSP_SERVER_LOCK (server);
998   priv->clients = g_list_remove (priv->clients, ctx);
999   GST_RTSP_SERVER_UNLOCK (server);
1000
1001   if (ctx->thread) {
1002     GSource *src;
1003
1004     src = g_idle_source_new ();
1005     g_source_set_callback (src, (GSourceFunc) free_client_context, ctx, NULL);
1006     g_source_attach (src, ctx->thread->context);
1007     g_source_unref (src);
1008   } else {
1009     free_client_context (ctx);
1010   }
1011 }
1012
1013 /* add the client context to the active list of clients, takes ownership
1014  * of client */
1015 static void
1016 manage_client (GstRTSPServer * server, GstRTSPClient * client)
1017 {
1018   ClientContext *cctx;
1019   GstRTSPServerPrivate *priv = server->priv;
1020   GMainContext *mainctx = NULL;
1021   GstRTSPContext ctx = { NULL };
1022
1023   GST_DEBUG_OBJECT (server, "manage client %p", client);
1024
1025   g_signal_emit (server, gst_rtsp_server_signals[SIGNAL_CLIENT_CONNECTED], 0,
1026       client);
1027
1028   cctx = g_slice_new0 (ClientContext);
1029   cctx->server = g_object_ref (server);
1030   cctx->client = client;
1031
1032   GST_RTSP_SERVER_LOCK (server);
1033
1034   ctx.server = server;
1035   ctx.client = client;
1036
1037   cctx->thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
1038       GST_RTSP_THREAD_TYPE_CLIENT, &ctx);
1039   if (cctx->thread)
1040     mainctx = cctx->thread->context;
1041   else {
1042     GSource *source;
1043     /* find the context to add the watch */
1044     if ((source = g_main_current_source ()))
1045       mainctx = g_source_get_context (source);
1046   }
1047
1048   g_signal_connect (client, "closed", (GCallback) unmanage_client, cctx);
1049   priv->clients = g_list_prepend (priv->clients, cctx);
1050
1051   gst_rtsp_client_attach (client, mainctx);
1052
1053   GST_RTSP_SERVER_UNLOCK (server);
1054 }
1055
1056 static GstRTSPClient *
1057 default_create_client (GstRTSPServer * server)
1058 {
1059   GstRTSPClient *client;
1060   GstRTSPServerPrivate *priv = server->priv;
1061
1062   /* a new client connected, create a session to handle the client. */
1063   client = gst_rtsp_client_new ();
1064
1065   /* set the session pool that this client should use */
1066   GST_RTSP_SERVER_LOCK (server);
1067   gst_rtsp_client_set_session_pool (client, priv->session_pool);
1068   /* set the mount points that this client should use */
1069   gst_rtsp_client_set_mount_points (client, priv->mount_points);
1070   /* set authentication manager */
1071   gst_rtsp_client_set_auth (client, priv->auth);
1072   /* set threadpool */
1073   gst_rtsp_client_set_thread_pool (client, priv->thread_pool);
1074   GST_RTSP_SERVER_UNLOCK (server);
1075
1076   return client;
1077 }
1078
1079 /**
1080  * gst_rtsp_server_transfer_connection:
1081  * @server: a #GstRTSPServer
1082  * @socket: (transfer full): a network socket
1083  * @ip: the IP address of the remote client
1084  * @port: the port used by the other end
1085  * @initial_buffer: any initial data that was already read from the socket
1086  *
1087  * Take an existing network socket and use it for an RTSP connection. This
1088  * is used when transferring a socket from an HTTP server which should be used
1089  * as an RTSP over HTTP tunnel. The @initial_buffer contains any remaining data
1090  * that the HTTP server read from the socket while parsing the HTTP header.
1091  *
1092  * Returns: TRUE if all was ok, FALSE if an error occured.
1093  */
1094 gboolean
1095 gst_rtsp_server_transfer_connection (GstRTSPServer * server, GSocket * socket,
1096     const gchar * ip, gint port, const gchar * initial_buffer)
1097 {
1098   GstRTSPClient *client = NULL;
1099   GstRTSPServerClass *klass;
1100   GstRTSPConnection *conn;
1101   GstRTSPResult res;
1102
1103   klass = GST_RTSP_SERVER_GET_CLASS (server);
1104
1105   if (klass->create_client)
1106     client = klass->create_client (server);
1107   if (client == NULL)
1108     goto client_failed;
1109
1110   GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
1111           initial_buffer, &conn), no_connection);
1112   g_object_unref (socket);
1113
1114   /* set connection on the client now */
1115   gst_rtsp_client_set_connection (client, conn);
1116
1117   /* manage the client connection */
1118   manage_client (server, client);
1119
1120   return TRUE;
1121
1122   /* ERRORS */
1123 client_failed:
1124   {
1125     GST_ERROR_OBJECT (server, "failed to create a client");
1126     g_object_unref (socket);
1127     return FALSE;
1128   }
1129 no_connection:
1130   {
1131     gchar *str = gst_rtsp_strresult (res);
1132     GST_ERROR ("could not create connection from socket %p: %s", socket, str);
1133     g_free (str);
1134     g_object_unref (socket);
1135     return FALSE;
1136   }
1137 }
1138
1139 /**
1140  * gst_rtsp_server_io_func:
1141  * @socket: a #GSocket
1142  * @condition: the condition on @source
1143  * @server: a #GstRTSPServer
1144  *
1145  * A default #GSocketSourceFunc that creates a new #GstRTSPClient to accept and handle a
1146  * new connection on @socket or @server.
1147  *
1148  * Returns: TRUE if the source could be connected, FALSE if an error occured.
1149  */
1150 gboolean
1151 gst_rtsp_server_io_func (GSocket * socket, GIOCondition condition,
1152     GstRTSPServer * server)
1153 {
1154   GstRTSPServerPrivate *priv = server->priv;
1155   GstRTSPClient *client = NULL;
1156   GstRTSPServerClass *klass;
1157   GstRTSPResult res;
1158   GstRTSPConnection *conn = NULL;
1159   GstRTSPContext ctx = { NULL };
1160
1161   if (condition & G_IO_IN) {
1162     /* a new client connected. */
1163     GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, NULL),
1164         accept_failed);
1165
1166     ctx.server = server;
1167     ctx.conn = conn;
1168     ctx.auth = priv->auth;
1169     gst_rtsp_context_push_current (&ctx);
1170
1171     if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_CONNECT))
1172       goto connection_refused;
1173
1174     klass = GST_RTSP_SERVER_GET_CLASS (server);
1175     /* a new client connected, create a client object to handle the client. */
1176     if (klass->create_client)
1177       client = klass->create_client (server);
1178     if (client == NULL)
1179       goto client_failed;
1180
1181     /* set connection on the client now */
1182     gst_rtsp_client_set_connection (client, conn);
1183
1184     /* manage the client connection */
1185     manage_client (server, client);
1186   } else {
1187     GST_WARNING_OBJECT (server, "received unknown event %08x", condition);
1188   }
1189 exit:
1190   gst_rtsp_context_pop_current (&ctx);
1191
1192   return G_SOURCE_CONTINUE;
1193
1194   /* ERRORS */
1195 accept_failed:
1196   {
1197     gchar *str = gst_rtsp_strresult (res);
1198     GST_ERROR_OBJECT (server, "Could not accept client on socket %p: %s",
1199         socket, str);
1200     g_free (str);
1201     goto exit;
1202   }
1203 connection_refused:
1204   {
1205     GST_ERROR_OBJECT (server, "connection refused");
1206     gst_rtsp_connection_free (conn);
1207     goto exit;
1208   }
1209 client_failed:
1210   {
1211     GST_ERROR_OBJECT (server, "failed to create a client");
1212     gst_rtsp_connection_free (conn);
1213     goto exit;
1214   }
1215 }
1216
1217 static void
1218 watch_destroyed (GstRTSPServer * server)
1219 {
1220   GstRTSPServerPrivate *priv = server->priv;
1221
1222   GST_DEBUG_OBJECT (server, "source destroyed");
1223
1224   g_object_unref (priv->socket);
1225   priv->socket = NULL;
1226   g_object_unref (server);
1227 }
1228
1229 /**
1230  * gst_rtsp_server_create_source:
1231  * @server: a #GstRTSPServer
1232  * @cancellable: a #GCancellable or %NULL.
1233  * @error: a #GError
1234  *
1235  * Create a #GSource for @server. The new source will have a default
1236  * #GSocketSourceFunc of gst_rtsp_server_io_func().
1237  *
1238  * @cancellable if not %NULL can be used to cancel the source, which will cause
1239  * the source to trigger, reporting the current condition (which is likely 0
1240  * unless cancellation happened at the same time as a condition change). You can
1241  * check for this in the callback using g_cancellable_is_cancelled().
1242  *
1243  * Returns: the #GSource for @server or %NULL when an error occured. Free with
1244  * g_source_unref ()
1245  */
1246 GSource *
1247 gst_rtsp_server_create_source (GstRTSPServer * server,
1248     GCancellable * cancellable, GError ** error)
1249 {
1250   GstRTSPServerPrivate *priv;
1251   GSocket *socket, *old;
1252   GSource *source;
1253
1254   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1255
1256   priv = server->priv;
1257
1258   socket = gst_rtsp_server_create_socket (server, NULL, error);
1259   if (socket == NULL)
1260     goto no_socket;
1261
1262   GST_RTSP_SERVER_LOCK (server);
1263   old = priv->socket;
1264   priv->socket = g_object_ref (socket);
1265   GST_RTSP_SERVER_UNLOCK (server);
1266
1267   if (old)
1268     g_object_unref (old);
1269
1270   /* create a watch for reads (new connections) and possible errors */
1271   source = g_socket_create_source (socket, G_IO_IN |
1272       G_IO_ERR | G_IO_HUP | G_IO_NVAL, cancellable);
1273   g_object_unref (socket);
1274
1275   /* configure the callback */
1276   g_source_set_callback (source,
1277       (GSourceFunc) gst_rtsp_server_io_func, g_object_ref (server),
1278       (GDestroyNotify) watch_destroyed);
1279
1280   return source;
1281
1282 no_socket:
1283   {
1284     GST_ERROR_OBJECT (server, "failed to create socket");
1285     return NULL;
1286   }
1287 }
1288
1289 /**
1290  * gst_rtsp_server_attach:
1291  * @server: a #GstRTSPServer
1292  * @context: (allow-none): a #GMainContext
1293  *
1294  * Attaches @server to @context. When the mainloop for @context is run, the
1295  * server will be dispatched. When @context is %NULL, the default context will be
1296  * used).
1297  *
1298  * This function should be called when the server properties and urls are fully
1299  * configured and the server is ready to start.
1300  *
1301  * Returns: the ID (greater than 0) for the source within the GMainContext.
1302  */
1303 guint
1304 gst_rtsp_server_attach (GstRTSPServer * server, GMainContext * context)
1305 {
1306   guint res;
1307   GSource *source;
1308   GError *error = NULL;
1309
1310   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), 0);
1311
1312   source = gst_rtsp_server_create_source (server, NULL, &error);
1313   if (source == NULL)
1314     goto no_source;
1315
1316   res = g_source_attach (source, context);
1317   g_source_unref (source);
1318
1319   return res;
1320
1321   /* ERRORS */
1322 no_source:
1323   {
1324     GST_ERROR_OBJECT (server, "failed to create watch: %s", error->message);
1325     g_error_free (error);
1326     return 0;
1327   }
1328 }
1329
1330 /**
1331  * gst_rtsp_server_client_filter:
1332  * @server: a #GstRTSPServer
1333  * @func: (scope call) (allow-none): a callback
1334  * @user_data: user data passed to @func
1335  *
1336  * Call @func for each client managed by @server. The result value of @func
1337  * determines what happens to the client. @func will be called with @server
1338  * locked so no further actions on @server can be performed from @func.
1339  *
1340  * If @func returns #GST_RTSP_FILTER_REMOVE, the client will be removed from
1341  * @server.
1342  *
1343  * If @func returns #GST_RTSP_FILTER_KEEP, the client will remain in @server.
1344  *
1345  * If @func returns #GST_RTSP_FILTER_REF, the client will remain in @server but
1346  * will also be added with an additional ref to the result #GList of this
1347  * function..
1348  *
1349  * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each client.
1350  *
1351  * Returns: (element-type GstRTSPClient) (transfer full): a #GList with all
1352  * clients for which @func returned #GST_RTSP_FILTER_REF. After usage, each
1353  * element in the #GList should be unreffed before the list is freed.
1354  */
1355 GList *
1356 gst_rtsp_server_client_filter (GstRTSPServer * server,
1357     GstRTSPServerClientFilterFunc func, gpointer user_data)
1358 {
1359   GstRTSPServerPrivate *priv;
1360   GList *result, *walk, *next;
1361
1362   g_return_val_if_fail (GST_IS_RTSP_SERVER (server), NULL);
1363
1364   priv = server->priv;
1365
1366   result = NULL;
1367
1368   GST_RTSP_SERVER_LOCK (server);
1369   for (walk = priv->clients; walk; walk = next) {
1370     ClientContext *cctx = walk->data;
1371     GstRTSPFilterResult res;
1372
1373     next = g_list_next (walk);
1374
1375     if (func)
1376       res = func (server, cctx->client, user_data);
1377     else
1378       res = GST_RTSP_FILTER_REF;
1379
1380     switch (res) {
1381       case GST_RTSP_FILTER_REMOVE:
1382         /* remove client, FIXME */
1383         break;
1384       case GST_RTSP_FILTER_REF:
1385         result = g_list_prepend (result, g_object_ref (cctx->client));
1386         break;
1387       case GST_RTSP_FILTER_KEEP:
1388       default:
1389         break;
1390     }
1391   }
1392   GST_RTSP_SERVER_UNLOCK (server);
1393
1394   return result;
1395 }