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