media: disconnect from signal handlers in unprepare()
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-session-pool.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "rtsp-session-pool.h"
21
22 #define GST_RTSP_SESSION_POOL_GET_PRIVATE(obj)  \
23          (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SESSION_POOL, GstRTSPSessionPoolPrivate))
24
25 struct _GstRTSPSessionPoolPrivate
26 {
27   GMutex lock;                  /* protects everything in this struct */
28   guint max_sessions;
29   GHashTable *sessions;
30 };
31
32 #define DEFAULT_MAX_SESSIONS 0
33
34 enum
35 {
36   PROP_0,
37   PROP_MAX_SESSIONS,
38   PROP_LAST
39 };
40
41 static const gchar session_id_charset[] =
42     { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
43   'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
44   'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
45   'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7',
46   '8', '9', '$', '-', '_', '.', '+'
47 };
48
49 GST_DEBUG_CATEGORY_STATIC (rtsp_session_debug);
50 #define GST_CAT_DEFAULT rtsp_session_debug
51
52 static void gst_rtsp_session_pool_get_property (GObject * object, guint propid,
53     GValue * value, GParamSpec * pspec);
54 static void gst_rtsp_session_pool_set_property (GObject * object, guint propid,
55     const GValue * value, GParamSpec * pspec);
56 static void gst_rtsp_session_pool_finalize (GObject * object);
57
58 static gchar *create_session_id (GstRTSPSessionPool * pool);
59
60 G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);
61
62 static void
63 gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
64 {
65   GObjectClass *gobject_class;
66
67   g_type_class_add_private (klass, sizeof (GstRTSPSessionPoolPrivate));
68
69   gobject_class = G_OBJECT_CLASS (klass);
70
71   gobject_class->get_property = gst_rtsp_session_pool_get_property;
72   gobject_class->set_property = gst_rtsp_session_pool_set_property;
73   gobject_class->finalize = gst_rtsp_session_pool_finalize;
74
75   g_object_class_install_property (gobject_class, PROP_MAX_SESSIONS,
76       g_param_spec_uint ("max-sessions", "Max Sessions",
77           "the maximum amount of sessions (0 = unlimited)",
78           0, G_MAXUINT, DEFAULT_MAX_SESSIONS,
79           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
80
81   klass->create_session_id = create_session_id;
82
83   GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsessionpool", 0,
84       "GstRTSPSessionPool");
85 }
86
87 static void
88 gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
89 {
90   GstRTSPSessionPoolPrivate *priv = GST_RTSP_SESSION_POOL_GET_PRIVATE (pool);
91
92   pool->priv = priv;
93
94   g_mutex_init (&priv->lock);
95   priv->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
96       NULL, g_object_unref);
97   priv->max_sessions = DEFAULT_MAX_SESSIONS;
98 }
99
100 static void
101 gst_rtsp_session_pool_finalize (GObject * object)
102 {
103   GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
104   GstRTSPSessionPoolPrivate *priv = pool->priv;
105
106   g_mutex_clear (&priv->lock);
107   g_hash_table_unref (priv->sessions);
108
109   G_OBJECT_CLASS (gst_rtsp_session_pool_parent_class)->finalize (object);
110 }
111
112 static void
113 gst_rtsp_session_pool_get_property (GObject * object, guint propid,
114     GValue * value, GParamSpec * pspec)
115 {
116   GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
117
118   switch (propid) {
119     case PROP_MAX_SESSIONS:
120       g_value_set_uint (value, gst_rtsp_session_pool_get_max_sessions (pool));
121       break;
122     default:
123       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
124       break;
125   }
126 }
127
128 static void
129 gst_rtsp_session_pool_set_property (GObject * object, guint propid,
130     const GValue * value, GParamSpec * pspec)
131 {
132   GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
133
134   switch (propid) {
135     case PROP_MAX_SESSIONS:
136       gst_rtsp_session_pool_set_max_sessions (pool, g_value_get_uint (value));
137       break;
138     default:
139       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
140       break;
141   }
142 }
143
144 /**
145  * gst_rtsp_session_pool_new:
146  *
147  * Create a new #GstRTSPSessionPool instance.
148  *
149  * Returns: A new #GstRTSPSessionPool. g_object_unref() after usage.
150  */
151 GstRTSPSessionPool *
152 gst_rtsp_session_pool_new (void)
153 {
154   GstRTSPSessionPool *result;
155
156   result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
157
158   return result;
159 }
160
161 /**
162  * gst_rtsp_session_pool_set_max_sessions:
163  * @pool: a #GstRTSPSessionPool
164  * @max: the maximum number of sessions
165  *
166  * Configure the maximum allowed number of sessions in @pool to @max.
167  * A value of 0 means an unlimited amount of sessions.
168  */
169 void
170 gst_rtsp_session_pool_set_max_sessions (GstRTSPSessionPool * pool, guint max)
171 {
172   GstRTSPSessionPoolPrivate *priv;
173
174   g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
175
176   priv = pool->priv;
177
178   g_mutex_lock (&priv->lock);
179   priv->max_sessions = max;
180   g_mutex_unlock (&priv->lock);
181 }
182
183 /**
184  * gst_rtsp_session_pool_get_max_sessions:
185  * @pool: a #GstRTSPSessionPool
186  *
187  * Get the maximum allowed number of sessions in @pool. 0 means an unlimited
188  * amount of sessions.
189  *
190  * Returns: the maximum allowed number of sessions.
191  */
192 guint
193 gst_rtsp_session_pool_get_max_sessions (GstRTSPSessionPool * pool)
194 {
195   GstRTSPSessionPoolPrivate *priv;
196   guint result;
197
198   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
199
200   priv = pool->priv;
201
202   g_mutex_lock (&priv->lock);
203   result = priv->max_sessions;
204   g_mutex_unlock (&priv->lock);
205
206   return result;
207 }
208
209 /**
210  * gst_rtsp_session_pool_get_n_sessions:
211  * @pool: a #GstRTSPSessionPool
212  *
213  * Get the amount of active sessions in @pool.
214  *
215  * Returns: the amount of active sessions in @pool.
216  */
217 guint
218 gst_rtsp_session_pool_get_n_sessions (GstRTSPSessionPool * pool)
219 {
220   GstRTSPSessionPoolPrivate *priv;
221   guint result;
222
223   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
224
225   priv = pool->priv;
226
227   g_mutex_lock (&priv->lock);
228   result = g_hash_table_size (priv->sessions);
229   g_mutex_unlock (&priv->lock);
230
231   return result;
232 }
233
234 /**
235  * gst_rtsp_session_pool_find:
236  * @pool: the pool to search
237  * @sessionid: the session id
238  *
239  * Find the session with @sessionid in @pool. The access time of the session
240  * will be updated with gst_rtsp_session_touch().
241  *
242  * Returns: (transfer full): the #GstRTSPSession with @sessionid or %NULL when the session did
243  * not exist. g_object_unref() after usage.
244  */
245 GstRTSPSession *
246 gst_rtsp_session_pool_find (GstRTSPSessionPool * pool, const gchar * sessionid)
247 {
248   GstRTSPSessionPoolPrivate *priv;
249   GstRTSPSession *result;
250
251   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
252   g_return_val_if_fail (sessionid != NULL, NULL);
253
254   priv = pool->priv;
255
256   g_mutex_lock (&priv->lock);
257   result = g_hash_table_lookup (priv->sessions, sessionid);
258   if (result) {
259     g_object_ref (result);
260     gst_rtsp_session_touch (result);
261   }
262   g_mutex_unlock (&priv->lock);
263
264   return result;
265 }
266
267 static gchar *
268 create_session_id (GstRTSPSessionPool * pool)
269 {
270   gchar id[16];
271   gint i;
272
273   for (i = 0; i < 16; i++) {
274     id[i] =
275         session_id_charset[g_random_int_range (0,
276             G_N_ELEMENTS (session_id_charset))];
277   }
278
279   return g_strndup (id, 16);
280 }
281
282 /**
283  * gst_rtsp_session_pool_create:
284  * @pool: a #GstRTSPSessionPool
285  *
286  * Create a new #GstRTSPSession object in @pool.
287  *
288  * Returns: (transfer none): a new #GstRTSPSession.
289  */
290 GstRTSPSession *
291 gst_rtsp_session_pool_create (GstRTSPSessionPool * pool)
292 {
293   GstRTSPSessionPoolPrivate *priv;
294   GstRTSPSession *result = NULL;
295   GstRTSPSessionPoolClass *klass;
296   gchar *id = NULL;
297   guint retry;
298
299   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
300
301   priv = pool->priv;
302
303   klass = GST_RTSP_SESSION_POOL_GET_CLASS (pool);
304
305   retry = 0;
306   do {
307     /* start by creating a new random session id, we assume that this is random
308      * enough to not cause a collision, which we will check later  */
309     if (klass->create_session_id)
310       id = klass->create_session_id (pool);
311     else
312       goto no_function;
313
314     if (id == NULL)
315       goto no_session;
316
317     g_mutex_lock (&priv->lock);
318     /* check session limit */
319     if (priv->max_sessions > 0) {
320       if (g_hash_table_size (priv->sessions) >= priv->max_sessions)
321         goto too_many_sessions;
322     }
323     /* check if the sessionid existed */
324     result = g_hash_table_lookup (priv->sessions, id);
325     if (result) {
326       /* found, retry with a different session id */
327       result = NULL;
328       retry++;
329       if (retry > 100)
330         goto collision;
331     } else {
332       /* not found, create session and insert it in the pool */
333       result = gst_rtsp_session_new (id);
334       /* take additional ref for the pool */
335       g_object_ref (result);
336       g_hash_table_insert (priv->sessions,
337           (gchar *) gst_rtsp_session_get_sessionid (result), result);
338     }
339     g_mutex_unlock (&priv->lock);
340
341     g_free (id);
342   } while (result == NULL);
343
344   return result;
345
346   /* ERRORS */
347 no_function:
348   {
349     GST_WARNING ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
350     return NULL;
351   }
352 no_session:
353   {
354     GST_WARNING ("can't create session id with GstRTSPSessionPool %p", pool);
355     return NULL;
356   }
357 collision:
358   {
359     GST_WARNING ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
360     g_mutex_unlock (&priv->lock);
361     g_free (id);
362     return NULL;
363   }
364 too_many_sessions:
365   {
366     GST_WARNING ("session pool reached max sessions of %d", priv->max_sessions);
367     g_mutex_unlock (&priv->lock);
368     g_free (id);
369     return NULL;
370   }
371 }
372
373 /**
374  * gst_rtsp_session_pool_remove:
375  * @pool: a #GstRTSPSessionPool
376  * @sess: a #GstRTSPSession
377  *
378  * Remove @sess from @pool, releasing the ref that the pool has on @sess.
379  *
380  * Returns: %TRUE if the session was found and removed.
381  */
382 gboolean
383 gst_rtsp_session_pool_remove (GstRTSPSessionPool * pool, GstRTSPSession * sess)
384 {
385   GstRTSPSessionPoolPrivate *priv;
386   gboolean found;
387
388   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), FALSE);
389   g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
390
391   priv = pool->priv;
392
393   g_mutex_lock (&priv->lock);
394   found =
395       g_hash_table_remove (priv->sessions,
396       gst_rtsp_session_get_sessionid (sess));
397   g_mutex_unlock (&priv->lock);
398
399   return found;
400 }
401
402 static gboolean
403 cleanup_func (gchar * sessionid, GstRTSPSession * sess, GTimeVal * now)
404 {
405   return gst_rtsp_session_is_expired (sess, now);
406 }
407
408 /**
409  * gst_rtsp_session_pool_cleanup:
410  * @pool: a #GstRTSPSessionPool
411  *
412  * Inspect all the sessions in @pool and remove the sessions that are inactive
413  * for more than their timeout.
414  *
415  * Returns: the amount of sessions that got removed.
416  */
417 guint
418 gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
419 {
420   GstRTSPSessionPoolPrivate *priv;
421   guint result;
422   GTimeVal now;
423
424   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
425
426   priv = pool->priv;
427
428   g_get_current_time (&now);
429
430   g_mutex_lock (&priv->lock);
431   result =
432       g_hash_table_foreach_remove (priv->sessions, (GHRFunc) cleanup_func,
433       &now);
434   g_mutex_unlock (&priv->lock);
435
436   return result;
437 }
438
439 typedef struct
440 {
441   GstRTSPSessionPool *pool;
442   GstRTSPSessionPoolFilterFunc func;
443   gpointer user_data;
444   GList *list;
445 } FilterData;
446
447 static gboolean
448 filter_func (gchar * sessionid, GstRTSPSession * sess, FilterData * data)
449 {
450   switch (data->func (data->pool, sess, data->user_data)) {
451     case GST_RTSP_FILTER_REMOVE:
452       return TRUE;
453     case GST_RTSP_FILTER_REF:
454       /* keep ref */
455       data->list = g_list_prepend (data->list, g_object_ref (sess));
456       /* fallthrough */
457     default:
458     case GST_RTSP_FILTER_KEEP:
459       return FALSE;
460   }
461 }
462
463 /**
464  * gst_rtsp_session_pool_filter:
465  * @pool: a #GstRTSPSessionPool
466  * @func: (scope call): a callback
467  * @user_data: user data passed to @func
468  *
469  * Call @func for each session in @pool. The result value of @func determines
470  * what happens to the session. @func will be called with the session pool
471  * locked so no further actions on @pool can be performed from @func.
472  *
473  * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
474  * @pool.
475  *
476  * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
477  *
478  * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
479  * will also be added with an additional ref to the result GList of this
480  * function..
481  *
482  * Returns: (element-type GstRTSPSession) (transfer full): a GList with all
483  * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
484  * element in the GList should be unreffed before the list is freed.
485  */
486 GList *
487 gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
488     GstRTSPSessionPoolFilterFunc func, gpointer user_data)
489 {
490   GstRTSPSessionPoolPrivate *priv;
491   FilterData data;
492
493   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
494   g_return_val_if_fail (func != NULL, NULL);
495
496   priv = pool->priv;
497
498   data.pool = pool;
499   data.func = func;
500   data.user_data = user_data;
501   data.list = NULL;
502
503   g_mutex_lock (&priv->lock);
504   g_hash_table_foreach_remove (priv->sessions, (GHRFunc) filter_func, &data);
505   g_mutex_unlock (&priv->lock);
506
507   return data.list;
508 }
509
510 typedef struct
511 {
512   GSource source;
513   GstRTSPSessionPool *pool;
514   gint timeout;
515 } GstPoolSource;
516
517 static void
518 collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
519 {
520   gint timeout;
521   GTimeVal now;
522   gint64 tmp;
523
524   tmp = g_source_get_time ((GSource *) psrc);
525   now.tv_sec = tmp / G_USEC_PER_SEC;
526   now.tv_usec = tmp % G_USEC_PER_SEC;
527
528   timeout = gst_rtsp_session_next_timeout (sess, &now);
529   GST_INFO ("%p: next timeout: %d", sess, timeout);
530   if (psrc->timeout == -1 || timeout < psrc->timeout)
531     psrc->timeout = timeout;
532 }
533
534 static gboolean
535 gst_pool_source_prepare (GSource * source, gint * timeout)
536 {
537   GstRTSPSessionPoolPrivate *priv;
538   GstPoolSource *psrc;
539   gboolean result;
540
541   psrc = (GstPoolSource *) source;
542   psrc->timeout = -1;
543   priv = psrc->pool->priv;
544
545   g_mutex_lock (&priv->lock);
546   g_hash_table_foreach (priv->sessions, (GHFunc) collect_timeout, psrc);
547   g_mutex_unlock (&priv->lock);
548
549   if (timeout)
550     *timeout = psrc->timeout;
551
552   result = psrc->timeout == 0;
553
554   GST_INFO ("prepare %d, %d", psrc->timeout, result);
555
556   return result;
557 }
558
559 static gboolean
560 gst_pool_source_check (GSource * source)
561 {
562   GST_INFO ("check");
563
564   return gst_pool_source_prepare (source, NULL);
565 }
566
567 static gboolean
568 gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
569     gpointer user_data)
570 {
571   gboolean res;
572   GstPoolSource *psrc = (GstPoolSource *) source;
573   GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;
574
575   GST_INFO ("dispatch");
576
577   if (func)
578     res = func (psrc->pool, user_data);
579   else
580     res = FALSE;
581
582   return res;
583 }
584
585 static void
586 gst_pool_source_finalize (GSource * source)
587 {
588   GstPoolSource *psrc = (GstPoolSource *) source;
589
590   GST_INFO ("finalize %p", psrc);
591
592   g_object_unref (psrc->pool);
593   psrc->pool = NULL;
594 }
595
596 static GSourceFuncs gst_pool_source_funcs = {
597   gst_pool_source_prepare,
598   gst_pool_source_check,
599   gst_pool_source_dispatch,
600   gst_pool_source_finalize
601 };
602
603 /**
604  * gst_rtsp_session_pool_create_watch:
605  * @pool: a #GstRTSPSessionPool
606  *
607  * A GSource that will be dispatched when the session should be cleaned up.
608  */
609 GSource *
610 gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
611 {
612   GstPoolSource *source;
613
614   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
615
616   source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
617       sizeof (GstPoolSource));
618   source->pool = g_object_ref (pool);
619
620   return (GSource *) source;
621 }