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: (transfer full): A new #GstRTSPSessionPool. g_object_unref() after
179 gst_rtsp_session_pool_new (void)
181 GstRTSPSessionPool *result;
183 result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
189 * gst_rtsp_session_pool_set_max_sessions:
190 * @pool: a #GstRTSPSessionPool
191 * @max: the maximum number of sessions
193 * Configure the maximum allowed number of sessions in @pool to @max.
194 * A value of 0 means an unlimited amount of sessions.
197 gst_rtsp_session_pool_set_max_sessions (GstRTSPSessionPool * pool, guint max)
199 GstRTSPSessionPoolPrivate *priv;
201 g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
205 g_mutex_lock (&priv->lock);
206 priv->max_sessions = max;
207 g_mutex_unlock (&priv->lock);
211 * gst_rtsp_session_pool_get_max_sessions:
212 * @pool: a #GstRTSPSessionPool
214 * Get the maximum allowed number of sessions in @pool. 0 means an unlimited
215 * amount of sessions.
217 * Returns: the maximum allowed number of sessions.
220 gst_rtsp_session_pool_get_max_sessions (GstRTSPSessionPool * pool)
222 GstRTSPSessionPoolPrivate *priv;
225 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
229 g_mutex_lock (&priv->lock);
230 result = priv->max_sessions;
231 g_mutex_unlock (&priv->lock);
237 * gst_rtsp_session_pool_get_n_sessions:
238 * @pool: a #GstRTSPSessionPool
240 * Get the amount of active sessions in @pool.
242 * Returns: the amount of active sessions in @pool.
245 gst_rtsp_session_pool_get_n_sessions (GstRTSPSessionPool * pool)
247 GstRTSPSessionPoolPrivate *priv;
250 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
254 g_mutex_lock (&priv->lock);
255 result = g_hash_table_size (priv->sessions);
256 g_mutex_unlock (&priv->lock);
262 * gst_rtsp_session_pool_find:
263 * @pool: the pool to search
264 * @sessionid: the session id
266 * Find the session with @sessionid in @pool. The access time of the session
267 * will be updated with gst_rtsp_session_touch().
269 * Returns: (transfer full): the #GstRTSPSession with @sessionid or %NULL when the session did
270 * not exist. g_object_unref() after usage.
273 gst_rtsp_session_pool_find (GstRTSPSessionPool * pool, const gchar * sessionid)
275 GstRTSPSessionPoolPrivate *priv;
276 GstRTSPSession *result;
278 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
279 g_return_val_if_fail (sessionid != NULL, NULL);
283 g_mutex_lock (&priv->lock);
284 result = g_hash_table_lookup (priv->sessions, sessionid);
286 g_object_ref (result);
287 gst_rtsp_session_touch (result);
289 g_mutex_unlock (&priv->lock);
295 create_session_id (GstRTSPSessionPool * pool)
300 for (i = 0; i < 16; i++) {
302 session_id_charset[g_random_int_range (0,
303 G_N_ELEMENTS (session_id_charset))];
307 return g_uri_escape_string (id, NULL, FALSE);
310 static GstRTSPSession *
311 create_session (GstRTSPSessionPool * pool, const gchar * id)
313 return gst_rtsp_session_new (id);
317 * gst_rtsp_session_pool_create:
318 * @pool: a #GstRTSPSessionPool
320 * Create a new #GstRTSPSession object in @pool.
322 * Returns: (transfer full): a new #GstRTSPSession.
325 gst_rtsp_session_pool_create (GstRTSPSessionPool * pool)
327 GstRTSPSessionPoolPrivate *priv;
328 GstRTSPSession *result = NULL;
329 GstRTSPSessionPoolClass *klass;
333 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
337 klass = GST_RTSP_SESSION_POOL_GET_CLASS (pool);
341 /* start by creating a new random session id, we assume that this is random
342 * enough to not cause a collision, which we will check later */
343 if (klass->create_session_id)
344 id = klass->create_session_id (pool);
351 g_mutex_lock (&priv->lock);
352 /* check session limit */
353 if (priv->max_sessions > 0) {
354 if (g_hash_table_size (priv->sessions) >= priv->max_sessions)
355 goto too_many_sessions;
357 /* check if the sessionid existed */
358 result = g_hash_table_lookup (priv->sessions, id);
360 /* found, retry with a different session id */
366 /* not found, create session and insert it in the pool */
367 if (klass->create_session)
368 result = create_session (pool, id);
370 goto too_many_sessions;
371 /* take additional ref for the pool */
372 g_object_ref (result);
373 g_hash_table_insert (priv->sessions,
374 (gchar *) gst_rtsp_session_get_sessionid (result), result);
376 g_mutex_unlock (&priv->lock);
379 } while (result == NULL);
386 GST_WARNING ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
391 GST_WARNING ("can't create session id with GstRTSPSessionPool %p", pool);
396 GST_WARNING ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
397 g_mutex_unlock (&priv->lock);
403 GST_WARNING ("session pool reached max sessions of %d", priv->max_sessions);
404 g_mutex_unlock (&priv->lock);
411 * gst_rtsp_session_pool_remove:
412 * @pool: a #GstRTSPSessionPool
413 * @sess: (transfer none): a #GstRTSPSession
415 * Remove @sess from @pool, releasing the ref that the pool has on @sess.
417 * Returns: %TRUE if the session was found and removed.
420 gst_rtsp_session_pool_remove (GstRTSPSessionPool * pool, GstRTSPSession * sess)
422 GstRTSPSessionPoolPrivate *priv;
425 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), FALSE);
426 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
430 g_mutex_lock (&priv->lock);
432 g_hash_table_remove (priv->sessions,
433 gst_rtsp_session_get_sessionid (sess));
434 g_mutex_unlock (&priv->lock);
440 cleanup_func (gchar * sessionid, GstRTSPSession * sess, GTimeVal * now)
442 return gst_rtsp_session_is_expired (sess, now);
446 * gst_rtsp_session_pool_cleanup:
447 * @pool: a #GstRTSPSessionPool
449 * Inspect all the sessions in @pool and remove the sessions that are inactive
450 * for more than their timeout.
452 * Returns: the amount of sessions that got removed.
455 gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
457 GstRTSPSessionPoolPrivate *priv;
461 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
465 g_get_current_time (&now);
467 g_mutex_lock (&priv->lock);
469 g_hash_table_foreach_remove (priv->sessions, (GHRFunc) cleanup_func,
471 g_mutex_unlock (&priv->lock);
478 GstRTSPSessionPool *pool;
479 GstRTSPSessionPoolFilterFunc func;
485 filter_func (gchar * sessionid, GstRTSPSession * sess, FilterData * data)
487 GstRTSPFilterResult res;
490 res = data->func (data->pool, sess, data->user_data);
492 res = GST_RTSP_FILTER_REF;
495 case GST_RTSP_FILTER_REMOVE:
497 case GST_RTSP_FILTER_REF:
499 data->list = g_list_prepend (data->list, g_object_ref (sess));
502 case GST_RTSP_FILTER_KEEP:
508 * gst_rtsp_session_pool_filter:
509 * @pool: a #GstRTSPSessionPool
510 * @func: (scope call) (allow-none): a callback
511 * @user_data: (closure): user data passed to @func
513 * Call @func for each session in @pool. The result value of @func determines
514 * what happens to the session. @func will be called with the session pool
515 * locked so no further actions on @pool can be performed from @func.
517 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
520 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
522 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
523 * will also be added with an additional ref to the result GList of this
526 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for all sessions.
528 * Returns: (element-type GstRTSPSession) (transfer full): a GList with all
529 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
530 * element in the GList should be unreffed before the list is freed.
533 gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
534 GstRTSPSessionPoolFilterFunc func, gpointer user_data)
536 GstRTSPSessionPoolPrivate *priv;
539 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
545 data.user_data = user_data;
548 g_mutex_lock (&priv->lock);
549 g_hash_table_foreach_remove (priv->sessions, (GHRFunc) filter_func, &data);
550 g_mutex_unlock (&priv->lock);
558 GstRTSPSessionPool *pool;
563 collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
568 g_get_current_time (&now);
570 timeout = gst_rtsp_session_next_timeout (sess, &now);
571 GST_INFO ("%p: next timeout: %d", sess, timeout);
572 if (psrc->timeout == -1 || timeout < psrc->timeout)
573 psrc->timeout = timeout;
577 gst_pool_source_prepare (GSource * source, gint * timeout)
579 GstRTSPSessionPoolPrivate *priv;
583 psrc = (GstPoolSource *) source;
585 priv = psrc->pool->priv;
587 g_mutex_lock (&priv->lock);
588 g_hash_table_foreach (priv->sessions, (GHFunc) collect_timeout, psrc);
589 g_mutex_unlock (&priv->lock);
592 *timeout = psrc->timeout;
594 result = psrc->timeout == 0;
596 GST_INFO ("prepare %d, %d", psrc->timeout, result);
602 gst_pool_source_check (GSource * source)
606 return gst_pool_source_prepare (source, NULL);
610 gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
614 GstPoolSource *psrc = (GstPoolSource *) source;
615 GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;
617 GST_INFO ("dispatch");
620 res = func (psrc->pool, user_data);
628 gst_pool_source_finalize (GSource * source)
630 GstPoolSource *psrc = (GstPoolSource *) source;
632 GST_INFO ("finalize %p", psrc);
634 g_object_unref (psrc->pool);
638 static GSourceFuncs gst_pool_source_funcs = {
639 gst_pool_source_prepare,
640 gst_pool_source_check,
641 gst_pool_source_dispatch,
642 gst_pool_source_finalize
646 * gst_rtsp_session_pool_create_watch:
647 * @pool: a #GstRTSPSessionPool
649 * Create a #GSource that will be dispatched when the session should be cleaned
652 * Returns: (transfer full): a #GSource
655 gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
657 GstPoolSource *source;
659 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
661 source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
662 sizeof (GstPoolSource));
663 source->pool = g_object_ref (pool);
665 return (GSource *) source;