2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
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.
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.
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.
20 * SECTION:rtsp-session-pool
21 * @short_description: An object for managing sessions
22 * @see_also: #GstRTSPSession
24 * The #GstRTSPSessionPool object manages a list of #GstRTSPSession objects.
26 * The maximum number of sessions can be configured with
27 * gst_rtsp_session_pool_set_max_sessions(). The current number of sessions can
28 * be retrieved with gst_rtsp_session_pool_get_n_sessions().
30 * Use gst_rtsp_session_pool_create() to create a new #GstRTSPSession object.
31 * The session object can be found again with its id and
32 * gst_rtsp_session_pool_find().
34 * All sessions can be iterated with gst_rtsp_session_pool_filter().
36 * Run gst_rtsp_session_pool_cleanup() periodically to remove timed out sessions
37 * or use gst_rtsp_session_pool_create_watch() to be notified when session
38 * cleanup should be performed.
40 * Last reviewed on 2013-07-11 (1.0.0)
43 #include "rtsp-session-pool.h"
45 #define GST_RTSP_SESSION_POOL_GET_PRIVATE(obj) \
46 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SESSION_POOL, GstRTSPSessionPoolPrivate))
48 struct _GstRTSPSessionPoolPrivate
50 GMutex lock; /* protects everything in this struct */
55 #define DEFAULT_MAX_SESSIONS 0
64 static const gchar session_id_charset[] =
65 { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
66 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
67 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
68 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7',
69 '8', '9', '$', '-', '_', '.', '+'
72 GST_DEBUG_CATEGORY_STATIC (rtsp_session_debug);
73 #define GST_CAT_DEFAULT rtsp_session_debug
75 static void gst_rtsp_session_pool_get_property (GObject * object, guint propid,
76 GValue * value, GParamSpec * pspec);
77 static void gst_rtsp_session_pool_set_property (GObject * object, guint propid,
78 const GValue * value, GParamSpec * pspec);
79 static void gst_rtsp_session_pool_finalize (GObject * object);
81 static gchar *create_session_id (GstRTSPSessionPool * pool);
82 static GstRTSPSession *create_session (GstRTSPSessionPool * pool,
85 G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);
88 gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
90 GObjectClass *gobject_class;
92 g_type_class_add_private (klass, sizeof (GstRTSPSessionPoolPrivate));
94 gobject_class = G_OBJECT_CLASS (klass);
96 gobject_class->get_property = gst_rtsp_session_pool_get_property;
97 gobject_class->set_property = gst_rtsp_session_pool_set_property;
98 gobject_class->finalize = gst_rtsp_session_pool_finalize;
100 g_object_class_install_property (gobject_class, PROP_MAX_SESSIONS,
101 g_param_spec_uint ("max-sessions", "Max Sessions",
102 "the maximum amount of sessions (0 = unlimited)",
103 0, G_MAXUINT, DEFAULT_MAX_SESSIONS,
104 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
106 klass->create_session_id = create_session_id;
107 klass->create_session = create_session;
109 GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsessionpool", 0,
110 "GstRTSPSessionPool");
114 gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
116 GstRTSPSessionPoolPrivate *priv = GST_RTSP_SESSION_POOL_GET_PRIVATE (pool);
120 g_mutex_init (&priv->lock);
121 priv->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
122 NULL, g_object_unref);
123 priv->max_sessions = DEFAULT_MAX_SESSIONS;
127 gst_rtsp_session_pool_finalize (GObject * object)
129 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
130 GstRTSPSessionPoolPrivate *priv = pool->priv;
132 g_mutex_clear (&priv->lock);
133 g_hash_table_unref (priv->sessions);
135 G_OBJECT_CLASS (gst_rtsp_session_pool_parent_class)->finalize (object);
139 gst_rtsp_session_pool_get_property (GObject * object, guint propid,
140 GValue * value, GParamSpec * pspec)
142 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
145 case PROP_MAX_SESSIONS:
146 g_value_set_uint (value, gst_rtsp_session_pool_get_max_sessions (pool));
149 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
155 gst_rtsp_session_pool_set_property (GObject * object, guint propid,
156 const GValue * value, GParamSpec * pspec)
158 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
161 case PROP_MAX_SESSIONS:
162 gst_rtsp_session_pool_set_max_sessions (pool, g_value_get_uint (value));
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
171 * gst_rtsp_session_pool_new:
173 * Create a new #GstRTSPSessionPool instance.
175 * Returns: A new #GstRTSPSessionPool. g_object_unref() after usage.
178 gst_rtsp_session_pool_new (void)
180 GstRTSPSessionPool *result;
182 result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
188 * gst_rtsp_session_pool_set_max_sessions:
189 * @pool: a #GstRTSPSessionPool
190 * @max: the maximum number of sessions
192 * Configure the maximum allowed number of sessions in @pool to @max.
193 * A value of 0 means an unlimited amount of sessions.
196 gst_rtsp_session_pool_set_max_sessions (GstRTSPSessionPool * pool, guint max)
198 GstRTSPSessionPoolPrivate *priv;
200 g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
204 g_mutex_lock (&priv->lock);
205 priv->max_sessions = max;
206 g_mutex_unlock (&priv->lock);
210 * gst_rtsp_session_pool_get_max_sessions:
211 * @pool: a #GstRTSPSessionPool
213 * Get the maximum allowed number of sessions in @pool. 0 means an unlimited
214 * amount of sessions.
216 * Returns: the maximum allowed number of sessions.
219 gst_rtsp_session_pool_get_max_sessions (GstRTSPSessionPool * pool)
221 GstRTSPSessionPoolPrivate *priv;
224 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
228 g_mutex_lock (&priv->lock);
229 result = priv->max_sessions;
230 g_mutex_unlock (&priv->lock);
236 * gst_rtsp_session_pool_get_n_sessions:
237 * @pool: a #GstRTSPSessionPool
239 * Get the amount of active sessions in @pool.
241 * Returns: the amount of active sessions in @pool.
244 gst_rtsp_session_pool_get_n_sessions (GstRTSPSessionPool * pool)
246 GstRTSPSessionPoolPrivate *priv;
249 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
253 g_mutex_lock (&priv->lock);
254 result = g_hash_table_size (priv->sessions);
255 g_mutex_unlock (&priv->lock);
261 * gst_rtsp_session_pool_find:
262 * @pool: the pool to search
263 * @sessionid: the session id
265 * Find the session with @sessionid in @pool. The access time of the session
266 * will be updated with gst_rtsp_session_touch().
268 * Returns: (transfer full): the #GstRTSPSession with @sessionid or %NULL when the session did
269 * not exist. g_object_unref() after usage.
272 gst_rtsp_session_pool_find (GstRTSPSessionPool * pool, const gchar * sessionid)
274 GstRTSPSessionPoolPrivate *priv;
275 GstRTSPSession *result;
277 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
278 g_return_val_if_fail (sessionid != NULL, NULL);
282 g_mutex_lock (&priv->lock);
283 result = g_hash_table_lookup (priv->sessions, sessionid);
285 g_object_ref (result);
286 gst_rtsp_session_touch (result);
288 g_mutex_unlock (&priv->lock);
294 create_session_id (GstRTSPSessionPool * pool)
299 for (i = 0; i < 16; i++) {
301 session_id_charset[g_random_int_range (0,
302 G_N_ELEMENTS (session_id_charset))];
306 return g_uri_escape_string (id, NULL, FALSE);
309 static GstRTSPSession *
310 create_session (GstRTSPSessionPool * pool, const gchar * id)
312 return gst_rtsp_session_new (id);
316 * gst_rtsp_session_pool_create:
317 * @pool: a #GstRTSPSessionPool
319 * Create a new #GstRTSPSession object in @pool.
321 * Returns: (transfer none): a new #GstRTSPSession.
324 gst_rtsp_session_pool_create (GstRTSPSessionPool * pool)
326 GstRTSPSessionPoolPrivate *priv;
327 GstRTSPSession *result = NULL;
328 GstRTSPSessionPoolClass *klass;
332 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
336 klass = GST_RTSP_SESSION_POOL_GET_CLASS (pool);
340 /* start by creating a new random session id, we assume that this is random
341 * enough to not cause a collision, which we will check later */
342 if (klass->create_session_id)
343 id = klass->create_session_id (pool);
350 g_mutex_lock (&priv->lock);
351 /* check session limit */
352 if (priv->max_sessions > 0) {
353 if (g_hash_table_size (priv->sessions) >= priv->max_sessions)
354 goto too_many_sessions;
356 /* check if the sessionid existed */
357 result = g_hash_table_lookup (priv->sessions, id);
359 /* found, retry with a different session id */
365 /* not found, create session and insert it in the pool */
366 if (klass->create_session)
367 result = create_session (pool, id);
369 goto too_many_sessions;
370 /* take additional ref for the pool */
371 g_object_ref (result);
372 g_hash_table_insert (priv->sessions,
373 (gchar *) gst_rtsp_session_get_sessionid (result), result);
375 g_mutex_unlock (&priv->lock);
378 } while (result == NULL);
385 GST_WARNING ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
390 GST_WARNING ("can't create session id with GstRTSPSessionPool %p", pool);
395 GST_WARNING ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
396 g_mutex_unlock (&priv->lock);
402 GST_WARNING ("session pool reached max sessions of %d", priv->max_sessions);
403 g_mutex_unlock (&priv->lock);
410 * gst_rtsp_session_pool_remove:
411 * @pool: a #GstRTSPSessionPool
412 * @sess: a #GstRTSPSession
414 * Remove @sess from @pool, releasing the ref that the pool has on @sess.
416 * Returns: %TRUE if the session was found and removed.
419 gst_rtsp_session_pool_remove (GstRTSPSessionPool * pool, GstRTSPSession * sess)
421 GstRTSPSessionPoolPrivate *priv;
424 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), FALSE);
425 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
429 g_mutex_lock (&priv->lock);
431 g_hash_table_remove (priv->sessions,
432 gst_rtsp_session_get_sessionid (sess));
433 g_mutex_unlock (&priv->lock);
439 cleanup_func (gchar * sessionid, GstRTSPSession * sess, GTimeVal * now)
441 return gst_rtsp_session_is_expired (sess, now);
445 * gst_rtsp_session_pool_cleanup:
446 * @pool: a #GstRTSPSessionPool
448 * Inspect all the sessions in @pool and remove the sessions that are inactive
449 * for more than their timeout.
451 * Returns: the amount of sessions that got removed.
454 gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
456 GstRTSPSessionPoolPrivate *priv;
460 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
464 g_get_current_time (&now);
466 g_mutex_lock (&priv->lock);
468 g_hash_table_foreach_remove (priv->sessions, (GHRFunc) cleanup_func,
470 g_mutex_unlock (&priv->lock);
477 GstRTSPSessionPool *pool;
478 GstRTSPSessionPoolFilterFunc func;
484 filter_func (gchar * sessionid, GstRTSPSession * sess, FilterData * data)
486 GstRTSPFilterResult res;
489 res = data->func (data->pool, sess, data->user_data);
491 res = GST_RTSP_FILTER_REF;
494 case GST_RTSP_FILTER_REMOVE:
496 case GST_RTSP_FILTER_REF:
498 data->list = g_list_prepend (data->list, g_object_ref (sess));
501 case GST_RTSP_FILTER_KEEP:
507 * gst_rtsp_session_pool_filter:
508 * @pool: a #GstRTSPSessionPool
509 * @func: (scope call): a callback
510 * @user_data: user data passed to @func
512 * Call @func for each session in @pool. The result value of @func determines
513 * what happens to the session. @func will be called with the session pool
514 * locked so no further actions on @pool can be performed from @func.
516 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
519 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
521 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
522 * will also be added with an additional ref to the result GList of this
525 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for all sessions.
527 * Returns: (element-type GstRTSPSession) (transfer full): a GList with all
528 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
529 * element in the GList should be unreffed before the list is freed.
532 gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
533 GstRTSPSessionPoolFilterFunc func, gpointer user_data)
535 GstRTSPSessionPoolPrivate *priv;
538 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
544 data.user_data = user_data;
547 g_mutex_lock (&priv->lock);
548 g_hash_table_foreach_remove (priv->sessions, (GHRFunc) filter_func, &data);
549 g_mutex_unlock (&priv->lock);
557 GstRTSPSessionPool *pool;
562 collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
567 g_get_current_time (&now);
569 timeout = gst_rtsp_session_next_timeout (sess, &now);
570 GST_INFO ("%p: next timeout: %d", sess, timeout);
571 if (psrc->timeout == -1 || timeout < psrc->timeout)
572 psrc->timeout = timeout;
576 gst_pool_source_prepare (GSource * source, gint * timeout)
578 GstRTSPSessionPoolPrivate *priv;
582 psrc = (GstPoolSource *) source;
584 priv = psrc->pool->priv;
586 g_mutex_lock (&priv->lock);
587 g_hash_table_foreach (priv->sessions, (GHFunc) collect_timeout, psrc);
588 g_mutex_unlock (&priv->lock);
591 *timeout = psrc->timeout;
593 result = psrc->timeout == 0;
595 GST_INFO ("prepare %d, %d", psrc->timeout, result);
601 gst_pool_source_check (GSource * source)
605 return gst_pool_source_prepare (source, NULL);
609 gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
613 GstPoolSource *psrc = (GstPoolSource *) source;
614 GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;
616 GST_INFO ("dispatch");
619 res = func (psrc->pool, user_data);
627 gst_pool_source_finalize (GSource * source)
629 GstPoolSource *psrc = (GstPoolSource *) source;
631 GST_INFO ("finalize %p", psrc);
633 g_object_unref (psrc->pool);
637 static GSourceFuncs gst_pool_source_funcs = {
638 gst_pool_source_prepare,
639 gst_pool_source_check,
640 gst_pool_source_dispatch,
641 gst_pool_source_finalize
645 * gst_rtsp_session_pool_create_watch:
646 * @pool: a #GstRTSPSessionPool
648 * A GSource that will be dispatched when the session should be cleaned up.
651 gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
653 GstPoolSource *source;
655 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
657 source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
658 sizeof (GstPoolSource));
659 source->pool = g_object_ref (pool);
661 return (GSource *) source;