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_mutex_unlock (&priv->lock);
464 g_signal_emit (pool, gst_rtsp_session_pool_signals[SIGNAL_SESSION_REMOVED],
467 g_object_unref (sess);
475 GstRTSPSessionPool *pool;
480 cleanup_func (gchar * sessionid, GstRTSPSession * sess, CleanupData * data)
484 expired = gst_rtsp_session_is_expired (sess, &data->now);
486 GST_DEBUG ("session expired");
487 data->removed = g_list_prepend (data->removed, g_object_ref (sess));
493 * gst_rtsp_session_pool_cleanup:
494 * @pool: a #GstRTSPSessionPool
496 * Inspect all the sessions in @pool and remove the sessions that are inactive
497 * for more than their timeout.
499 * Returns: the amount of sessions that got removed.
502 gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
504 GstRTSPSessionPoolPrivate *priv;
509 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
513 g_get_current_time (&data.now);
517 g_mutex_lock (&priv->lock);
519 g_hash_table_foreach_remove (priv->sessions, (GHRFunc) cleanup_func,
522 priv->sessions_cookie++;
523 g_mutex_unlock (&priv->lock);
525 for (walk = data.removed; walk; walk = walk->next) {
526 GstRTSPSession *sess = walk->data;
529 gst_rtsp_session_pool_signals[SIGNAL_SESSION_REMOVED], 0, sess);
531 g_object_unref (sess);
533 g_list_free (data.removed);
539 * gst_rtsp_session_pool_filter:
540 * @pool: a #GstRTSPSessionPool
541 * @func: (scope call) (allow-none): a callback
542 * @user_data: (closure): user data passed to @func
544 * Call @func for each session in @pool. The result value of @func determines
545 * what happens to the session. @func will be called with the session pool
546 * locked so no further actions on @pool can be performed from @func.
548 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be set to the
549 * expired state with gst_rtsp_session_set_expired() and removed from
552 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
554 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
555 * will also be added with an additional ref to the result GList of this
558 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for all sessions.
560 * Returns: (element-type GstRTSPSession) (transfer full): a GList with all
561 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
562 * element in the GList should be unreffed before the list is freed.
565 gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
566 GstRTSPSessionPoolFilterFunc func, gpointer user_data)
568 GstRTSPSessionPoolPrivate *priv;
575 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
581 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
583 g_mutex_lock (&priv->lock);
585 g_hash_table_iter_init (&iter, priv->sessions);
586 cookie = priv->sessions_cookie;
587 while (g_hash_table_iter_next (&iter, &key, &value)) {
588 GstRTSPSession *session = value;
589 GstRTSPFilterResult res;
593 /* only visit each session once */
594 if (g_hash_table_contains (visited, session))
597 g_hash_table_add (visited, g_object_ref (session));
598 g_mutex_unlock (&priv->lock);
600 res = func (pool, session, user_data);
602 g_mutex_lock (&priv->lock);
604 res = GST_RTSP_FILTER_REF;
606 changed = (cookie != priv->sessions_cookie);
609 case GST_RTSP_FILTER_REMOVE:
611 gboolean removed = TRUE;
614 /* something changed, check if we still have the session */
615 removed = g_hash_table_remove (priv->sessions, key);
617 g_hash_table_iter_remove (&iter);
620 /* if we managed to remove the session, update the cookie and
622 cookie = ++priv->sessions_cookie;
623 g_mutex_unlock (&priv->lock);
626 gst_rtsp_session_pool_signals[SIGNAL_SESSION_REMOVED], 0,
629 g_mutex_lock (&priv->lock);
630 /* cookie could have changed again, make sure we restart */
631 changed |= (cookie != priv->sessions_cookie);
635 case GST_RTSP_FILTER_REF:
637 result = g_list_prepend (result, g_object_ref (session));
639 case GST_RTSP_FILTER_KEEP:
646 g_mutex_unlock (&priv->lock);
649 g_hash_table_unref (visited);
657 GstRTSPSessionPool *pool;
662 collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
667 g_get_current_time (&now);
669 timeout = gst_rtsp_session_next_timeout (sess, &now);
670 GST_INFO ("%p: next timeout: %d", sess, timeout);
671 if (psrc->timeout == -1 || timeout < psrc->timeout)
672 psrc->timeout = timeout;
676 gst_pool_source_prepare (GSource * source, gint * timeout)
678 GstRTSPSessionPoolPrivate *priv;
682 psrc = (GstPoolSource *) source;
684 priv = psrc->pool->priv;
686 g_mutex_lock (&priv->lock);
687 g_hash_table_foreach (priv->sessions, (GHFunc) collect_timeout, psrc);
688 g_mutex_unlock (&priv->lock);
691 *timeout = psrc->timeout;
693 result = psrc->timeout == 0;
695 GST_INFO ("prepare %d, %d", psrc->timeout, result);
701 gst_pool_source_check (GSource * source)
705 return gst_pool_source_prepare (source, NULL);
709 gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
713 GstPoolSource *psrc = (GstPoolSource *) source;
714 GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;
716 GST_INFO ("dispatch");
719 res = func (psrc->pool, user_data);
727 gst_pool_source_finalize (GSource * source)
729 GstPoolSource *psrc = (GstPoolSource *) source;
731 GST_INFO ("finalize %p", psrc);
733 g_object_unref (psrc->pool);
737 static GSourceFuncs gst_pool_source_funcs = {
738 gst_pool_source_prepare,
739 gst_pool_source_check,
740 gst_pool_source_dispatch,
741 gst_pool_source_finalize
745 * gst_rtsp_session_pool_create_watch:
746 * @pool: a #GstRTSPSessionPool
748 * Create a #GSource that will be dispatched when the session should be cleaned
751 * Returns: (transfer full): a #GSource
754 gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
756 GstPoolSource *source;
758 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
760 source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
761 sizeof (GstPoolSource));
762 source->pool = g_object_ref (pool);
764 return (GSource *) source;