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 */
53 guint sessions_cookie;
56 #define DEFAULT_MAX_SESSIONS 0
65 static const gchar session_id_charset[] =
66 { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
67 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
68 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
69 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7',
70 '8', '9', '$', '-', '_', '.', '+'
75 SIGNAL_SESSION_REMOVED,
79 static guint gst_rtsp_session_pool_signals[SIGNAL_LAST] = { 0 };
81 GST_DEBUG_CATEGORY_STATIC (rtsp_session_debug);
82 #define GST_CAT_DEFAULT rtsp_session_debug
84 static void gst_rtsp_session_pool_get_property (GObject * object, guint propid,
85 GValue * value, GParamSpec * pspec);
86 static void gst_rtsp_session_pool_set_property (GObject * object, guint propid,
87 const GValue * value, GParamSpec * pspec);
88 static void gst_rtsp_session_pool_finalize (GObject * object);
90 static gchar *create_session_id (GstRTSPSessionPool * pool);
91 static GstRTSPSession *create_session (GstRTSPSessionPool * pool,
94 G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);
97 gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
99 GObjectClass *gobject_class;
101 g_type_class_add_private (klass, sizeof (GstRTSPSessionPoolPrivate));
103 gobject_class = G_OBJECT_CLASS (klass);
105 gobject_class->get_property = gst_rtsp_session_pool_get_property;
106 gobject_class->set_property = gst_rtsp_session_pool_set_property;
107 gobject_class->finalize = gst_rtsp_session_pool_finalize;
109 g_object_class_install_property (gobject_class, PROP_MAX_SESSIONS,
110 g_param_spec_uint ("max-sessions", "Max Sessions",
111 "the maximum amount of sessions (0 = unlimited)",
112 0, G_MAXUINT, DEFAULT_MAX_SESSIONS,
113 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
115 gst_rtsp_session_pool_signals[SIGNAL_SESSION_REMOVED] =
116 g_signal_new ("session-removed", G_TYPE_FROM_CLASS (klass),
117 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPSessionPoolClass,
118 session_removed), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
119 1, GST_TYPE_RTSP_SESSION);
121 klass->create_session_id = create_session_id;
122 klass->create_session = create_session;
124 GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsessionpool", 0,
125 "GstRTSPSessionPool");
129 gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
131 GstRTSPSessionPoolPrivate *priv = GST_RTSP_SESSION_POOL_GET_PRIVATE (pool);
135 g_mutex_init (&priv->lock);
136 priv->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
137 NULL, g_object_unref);
138 priv->max_sessions = DEFAULT_MAX_SESSIONS;
141 static GstRTSPFilterResult
142 remove_sessions_func (GstRTSPSessionPool * pool, GstRTSPSession * session,
145 return GST_RTSP_FILTER_REMOVE;
149 gst_rtsp_session_pool_finalize (GObject * object)
151 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
152 GstRTSPSessionPoolPrivate *priv = pool->priv;
154 gst_rtsp_session_pool_filter (pool, remove_sessions_func, NULL);
155 g_hash_table_unref (priv->sessions);
156 g_mutex_clear (&priv->lock);
158 G_OBJECT_CLASS (gst_rtsp_session_pool_parent_class)->finalize (object);
162 gst_rtsp_session_pool_get_property (GObject * object, guint propid,
163 GValue * value, GParamSpec * pspec)
165 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
168 case PROP_MAX_SESSIONS:
169 g_value_set_uint (value, gst_rtsp_session_pool_get_max_sessions (pool));
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
178 gst_rtsp_session_pool_set_property (GObject * object, guint propid,
179 const GValue * value, GParamSpec * pspec)
181 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
184 case PROP_MAX_SESSIONS:
185 gst_rtsp_session_pool_set_max_sessions (pool, g_value_get_uint (value));
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
194 * gst_rtsp_session_pool_new:
196 * Create a new #GstRTSPSessionPool instance.
198 * Returns: (transfer full): A new #GstRTSPSessionPool. g_object_unref() after
202 gst_rtsp_session_pool_new (void)
204 GstRTSPSessionPool *result;
206 result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
212 * gst_rtsp_session_pool_set_max_sessions:
213 * @pool: a #GstRTSPSessionPool
214 * @max: the maximum number of sessions
216 * Configure the maximum allowed number of sessions in @pool to @max.
217 * A value of 0 means an unlimited amount of sessions.
220 gst_rtsp_session_pool_set_max_sessions (GstRTSPSessionPool * pool, guint max)
222 GstRTSPSessionPoolPrivate *priv;
224 g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
228 g_mutex_lock (&priv->lock);
229 priv->max_sessions = max;
230 g_mutex_unlock (&priv->lock);
234 * gst_rtsp_session_pool_get_max_sessions:
235 * @pool: a #GstRTSPSessionPool
237 * Get the maximum allowed number of sessions in @pool. 0 means an unlimited
238 * amount of sessions.
240 * Returns: the maximum allowed number of sessions.
243 gst_rtsp_session_pool_get_max_sessions (GstRTSPSessionPool * pool)
245 GstRTSPSessionPoolPrivate *priv;
248 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
252 g_mutex_lock (&priv->lock);
253 result = priv->max_sessions;
254 g_mutex_unlock (&priv->lock);
260 * gst_rtsp_session_pool_get_n_sessions:
261 * @pool: a #GstRTSPSessionPool
263 * Get the amount of active sessions in @pool.
265 * Returns: the amount of active sessions in @pool.
268 gst_rtsp_session_pool_get_n_sessions (GstRTSPSessionPool * pool)
270 GstRTSPSessionPoolPrivate *priv;
273 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
277 g_mutex_lock (&priv->lock);
278 result = g_hash_table_size (priv->sessions);
279 g_mutex_unlock (&priv->lock);
285 * gst_rtsp_session_pool_find:
286 * @pool: the pool to search
287 * @sessionid: the session id
289 * Find the session with @sessionid in @pool. The access time of the session
290 * will be updated with gst_rtsp_session_touch().
292 * Returns: (transfer full) (nullable): the #GstRTSPSession with @sessionid
293 * or %NULL when the session did not exist. g_object_unref() after usage.
296 gst_rtsp_session_pool_find (GstRTSPSessionPool * pool, const gchar * sessionid)
298 GstRTSPSessionPoolPrivate *priv;
299 GstRTSPSession *result;
301 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
302 g_return_val_if_fail (sessionid != NULL, NULL);
306 g_mutex_lock (&priv->lock);
307 result = g_hash_table_lookup (priv->sessions, sessionid);
309 g_object_ref (result);
310 gst_rtsp_session_touch (result);
312 g_mutex_unlock (&priv->lock);
318 create_session_id (GstRTSPSessionPool * pool)
323 for (i = 0; i < 16; i++) {
325 session_id_charset[g_random_int_range (0,
326 G_N_ELEMENTS (session_id_charset))];
330 return g_uri_escape_string (id, NULL, FALSE);
333 static GstRTSPSession *
334 create_session (GstRTSPSessionPool * pool, const gchar * id)
336 return gst_rtsp_session_new (id);
340 * gst_rtsp_session_pool_create:
341 * @pool: a #GstRTSPSessionPool
343 * Create a new #GstRTSPSession object in @pool.
345 * Returns: (transfer full): a new #GstRTSPSession.
348 gst_rtsp_session_pool_create (GstRTSPSessionPool * pool)
350 GstRTSPSessionPoolPrivate *priv;
351 GstRTSPSession *result = NULL;
352 GstRTSPSessionPoolClass *klass;
356 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
360 klass = GST_RTSP_SESSION_POOL_GET_CLASS (pool);
364 /* start by creating a new random session id, we assume that this is random
365 * enough to not cause a collision, which we will check later */
366 if (klass->create_session_id)
367 id = klass->create_session_id (pool);
374 g_mutex_lock (&priv->lock);
375 /* check session limit */
376 if (priv->max_sessions > 0) {
377 if (g_hash_table_size (priv->sessions) >= priv->max_sessions)
378 goto too_many_sessions;
380 /* check if the sessionid existed */
381 result = g_hash_table_lookup (priv->sessions, id);
383 /* found, retry with a different session id */
389 /* not found, create session and insert it in the pool */
390 if (klass->create_session)
391 result = create_session (pool, id);
393 goto too_many_sessions;
394 /* take additional ref for the pool */
395 g_object_ref (result);
396 g_hash_table_insert (priv->sessions,
397 (gchar *) gst_rtsp_session_get_sessionid (result), result);
398 priv->sessions_cookie++;
400 g_mutex_unlock (&priv->lock);
403 } while (result == NULL);
410 GST_WARNING ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
415 GST_WARNING ("can't create session id with GstRTSPSessionPool %p", pool);
420 GST_WARNING ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
421 g_mutex_unlock (&priv->lock);
427 GST_WARNING ("session pool reached max sessions of %d", priv->max_sessions);
428 g_mutex_unlock (&priv->lock);
435 * gst_rtsp_session_pool_remove:
436 * @pool: a #GstRTSPSessionPool
437 * @sess: (transfer none): a #GstRTSPSession
439 * Remove @sess from @pool, releasing the ref that the pool has on @sess.
441 * Returns: %TRUE if the session was found and removed.
444 gst_rtsp_session_pool_remove (GstRTSPSessionPool * pool, GstRTSPSession * sess)
446 GstRTSPSessionPoolPrivate *priv;
449 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), FALSE);
450 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
454 g_mutex_lock (&priv->lock);
457 g_hash_table_remove (priv->sessions,
458 gst_rtsp_session_get_sessionid (sess));
460 priv->sessions_cookie++;
461 g_signal_emit (pool, gst_rtsp_session_pool_signals[SIGNAL_SESSION_REMOVED],
464 g_object_unref (sess);
465 g_mutex_unlock (&priv->lock);
473 GstRTSPSessionPool *pool;
477 cleanup_func (gchar * sessionid, GstRTSPSession * sess, CleanupData * data)
481 expired = gst_rtsp_session_is_expired (sess, &data->now);
483 GST_DEBUG ("session expired, emitting signal");
484 g_signal_emit (data->pool,
485 gst_rtsp_session_pool_signals[SIGNAL_SESSION_REMOVED], 0, sess);
491 * gst_rtsp_session_pool_cleanup:
492 * @pool: a #GstRTSPSessionPool
494 * Inspect all the sessions in @pool and remove the sessions that are inactive
495 * for more than their timeout.
497 * Returns: the amount of sessions that got removed.
500 gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
502 GstRTSPSessionPoolPrivate *priv;
506 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
510 g_get_current_time (&data.now);
513 g_mutex_lock (&priv->lock);
515 g_hash_table_foreach_remove (priv->sessions, (GHRFunc) cleanup_func,
518 priv->sessions_cookie++;
519 g_mutex_unlock (&priv->lock);
525 * gst_rtsp_session_pool_filter:
526 * @pool: a #GstRTSPSessionPool
527 * @func: (scope call) (allow-none): a callback
528 * @user_data: (closure): user data passed to @func
530 * Call @func for each session in @pool. The result value of @func determines
531 * what happens to the session. @func will be called with the session pool
532 * locked so no further actions on @pool can be performed from @func.
534 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be set to the
535 * expired state with gst_rtsp_session_set_expired() and removed from
538 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
540 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
541 * will also be added with an additional ref to the result GList of this
544 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for all sessions.
546 * Returns: (element-type GstRTSPSession) (transfer full): a GList with all
547 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
548 * element in the GList should be unreffed before the list is freed.
551 gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
552 GstRTSPSessionPoolFilterFunc func, gpointer user_data)
554 GstRTSPSessionPoolPrivate *priv;
561 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
567 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
569 g_mutex_lock (&priv->lock);
571 g_hash_table_iter_init (&iter, priv->sessions);
572 cookie = priv->sessions_cookie;
573 while (g_hash_table_iter_next (&iter, &key, &value)) {
574 GstRTSPSession *session = value;
575 GstRTSPFilterResult res;
579 /* only visit each session once */
580 if (g_hash_table_contains (visited, session))
583 g_hash_table_add (visited, g_object_ref (session));
584 g_mutex_unlock (&priv->lock);
586 res = func (pool, session, user_data);
588 g_mutex_lock (&priv->lock);
590 res = GST_RTSP_FILTER_REF;
592 changed = (cookie != priv->sessions_cookie);
595 case GST_RTSP_FILTER_REMOVE:
597 gst_rtsp_session_pool_signals[SIGNAL_SESSION_REMOVED], 0, session);
600 g_hash_table_remove (priv->sessions, key);
602 g_hash_table_iter_remove (&iter);
603 cookie = ++priv->sessions_cookie;
605 case GST_RTSP_FILTER_REF:
607 result = g_list_prepend (result, g_object_ref (session));
609 case GST_RTSP_FILTER_KEEP:
616 g_mutex_unlock (&priv->lock);
619 g_hash_table_unref (visited);
627 GstRTSPSessionPool *pool;
632 collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
637 g_get_current_time (&now);
639 timeout = gst_rtsp_session_next_timeout (sess, &now);
640 GST_INFO ("%p: next timeout: %d", sess, timeout);
641 if (psrc->timeout == -1 || timeout < psrc->timeout)
642 psrc->timeout = timeout;
646 gst_pool_source_prepare (GSource * source, gint * timeout)
648 GstRTSPSessionPoolPrivate *priv;
652 psrc = (GstPoolSource *) source;
654 priv = psrc->pool->priv;
656 g_mutex_lock (&priv->lock);
657 g_hash_table_foreach (priv->sessions, (GHFunc) collect_timeout, psrc);
658 g_mutex_unlock (&priv->lock);
661 *timeout = psrc->timeout;
663 result = psrc->timeout == 0;
665 GST_INFO ("prepare %d, %d", psrc->timeout, result);
671 gst_pool_source_check (GSource * source)
675 return gst_pool_source_prepare (source, NULL);
679 gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
683 GstPoolSource *psrc = (GstPoolSource *) source;
684 GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;
686 GST_INFO ("dispatch");
689 res = func (psrc->pool, user_data);
697 gst_pool_source_finalize (GSource * source)
699 GstPoolSource *psrc = (GstPoolSource *) source;
701 GST_INFO ("finalize %p", psrc);
703 g_object_unref (psrc->pool);
707 static GSourceFuncs gst_pool_source_funcs = {
708 gst_pool_source_prepare,
709 gst_pool_source_check,
710 gst_pool_source_dispatch,
711 gst_pool_source_finalize
715 * gst_rtsp_session_pool_create_watch:
716 * @pool: a #GstRTSPSessionPool
718 * Create a #GSource that will be dispatched when the session should be cleaned
721 * Returns: (transfer full): a #GSource
724 gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
726 GstPoolSource *source;
728 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
730 source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
731 sizeof (GstPoolSource));
732 source->pool = g_object_ref (pool);
734 return (GSource *) source;