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