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 #include "rtsp-session-pool.h"
22 #define GST_RTSP_SESSION_POOL_GET_PRIVATE(obj) \
23 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SESSION_POOL, GstRTSPSessionPoolPrivate))
25 struct _GstRTSPSessionPoolPrivate
27 GMutex lock; /* protects everything in this struct */
32 #define DEFAULT_MAX_SESSIONS 0
41 static const gchar session_id_charset[] =
42 { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
43 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
44 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
45 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7',
46 '8', '9', '$', '-', '_', '.', '+'
49 GST_DEBUG_CATEGORY_STATIC (rtsp_session_debug);
50 #define GST_CAT_DEFAULT rtsp_session_debug
52 static void gst_rtsp_session_pool_get_property (GObject * object, guint propid,
53 GValue * value, GParamSpec * pspec);
54 static void gst_rtsp_session_pool_set_property (GObject * object, guint propid,
55 const GValue * value, GParamSpec * pspec);
56 static void gst_rtsp_session_pool_finalize (GObject * object);
58 static gchar *create_session_id (GstRTSPSessionPool * pool);
59 static GstRTSPSession *create_session (GstRTSPSessionPool * pool,
62 G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);
65 gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
67 GObjectClass *gobject_class;
69 g_type_class_add_private (klass, sizeof (GstRTSPSessionPoolPrivate));
71 gobject_class = G_OBJECT_CLASS (klass);
73 gobject_class->get_property = gst_rtsp_session_pool_get_property;
74 gobject_class->set_property = gst_rtsp_session_pool_set_property;
75 gobject_class->finalize = gst_rtsp_session_pool_finalize;
77 g_object_class_install_property (gobject_class, PROP_MAX_SESSIONS,
78 g_param_spec_uint ("max-sessions", "Max Sessions",
79 "the maximum amount of sessions (0 = unlimited)",
80 0, G_MAXUINT, DEFAULT_MAX_SESSIONS,
81 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
83 klass->create_session_id = create_session_id;
84 klass->create_session = create_session;
86 GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsessionpool", 0,
87 "GstRTSPSessionPool");
91 gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
93 GstRTSPSessionPoolPrivate *priv = GST_RTSP_SESSION_POOL_GET_PRIVATE (pool);
97 g_mutex_init (&priv->lock);
98 priv->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
99 NULL, g_object_unref);
100 priv->max_sessions = DEFAULT_MAX_SESSIONS;
104 gst_rtsp_session_pool_finalize (GObject * object)
106 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
107 GstRTSPSessionPoolPrivate *priv = pool->priv;
109 g_mutex_clear (&priv->lock);
110 g_hash_table_unref (priv->sessions);
112 G_OBJECT_CLASS (gst_rtsp_session_pool_parent_class)->finalize (object);
116 gst_rtsp_session_pool_get_property (GObject * object, guint propid,
117 GValue * value, GParamSpec * pspec)
119 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
122 case PROP_MAX_SESSIONS:
123 g_value_set_uint (value, gst_rtsp_session_pool_get_max_sessions (pool));
126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
132 gst_rtsp_session_pool_set_property (GObject * object, guint propid,
133 const GValue * value, GParamSpec * pspec)
135 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
138 case PROP_MAX_SESSIONS:
139 gst_rtsp_session_pool_set_max_sessions (pool, g_value_get_uint (value));
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
148 * gst_rtsp_session_pool_new:
150 * Create a new #GstRTSPSessionPool instance.
152 * Returns: A new #GstRTSPSessionPool. g_object_unref() after usage.
155 gst_rtsp_session_pool_new (void)
157 GstRTSPSessionPool *result;
159 result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
165 * gst_rtsp_session_pool_set_max_sessions:
166 * @pool: a #GstRTSPSessionPool
167 * @max: the maximum number of sessions
169 * Configure the maximum allowed number of sessions in @pool to @max.
170 * A value of 0 means an unlimited amount of sessions.
173 gst_rtsp_session_pool_set_max_sessions (GstRTSPSessionPool * pool, guint max)
175 GstRTSPSessionPoolPrivate *priv;
177 g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
181 g_mutex_lock (&priv->lock);
182 priv->max_sessions = max;
183 g_mutex_unlock (&priv->lock);
187 * gst_rtsp_session_pool_get_max_sessions:
188 * @pool: a #GstRTSPSessionPool
190 * Get the maximum allowed number of sessions in @pool. 0 means an unlimited
191 * amount of sessions.
193 * Returns: the maximum allowed number of sessions.
196 gst_rtsp_session_pool_get_max_sessions (GstRTSPSessionPool * pool)
198 GstRTSPSessionPoolPrivate *priv;
201 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
205 g_mutex_lock (&priv->lock);
206 result = priv->max_sessions;
207 g_mutex_unlock (&priv->lock);
213 * gst_rtsp_session_pool_get_n_sessions:
214 * @pool: a #GstRTSPSessionPool
216 * Get the amount of active sessions in @pool.
218 * Returns: the amount of active sessions in @pool.
221 gst_rtsp_session_pool_get_n_sessions (GstRTSPSessionPool * pool)
223 GstRTSPSessionPoolPrivate *priv;
226 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
230 g_mutex_lock (&priv->lock);
231 result = g_hash_table_size (priv->sessions);
232 g_mutex_unlock (&priv->lock);
238 * gst_rtsp_session_pool_find:
239 * @pool: the pool to search
240 * @sessionid: the session id
242 * Find the session with @sessionid in @pool. The access time of the session
243 * will be updated with gst_rtsp_session_touch().
245 * Returns: (transfer full): the #GstRTSPSession with @sessionid or %NULL when the session did
246 * not exist. g_object_unref() after usage.
249 gst_rtsp_session_pool_find (GstRTSPSessionPool * pool, const gchar * sessionid)
251 GstRTSPSessionPoolPrivate *priv;
252 GstRTSPSession *result;
254 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
255 g_return_val_if_fail (sessionid != NULL, NULL);
259 g_mutex_lock (&priv->lock);
260 result = g_hash_table_lookup (priv->sessions, sessionid);
262 g_object_ref (result);
263 gst_rtsp_session_touch (result);
265 g_mutex_unlock (&priv->lock);
271 create_session_id (GstRTSPSessionPool * pool)
276 for (i = 0; i < 16; i++) {
278 session_id_charset[g_random_int_range (0,
279 G_N_ELEMENTS (session_id_charset))];
282 return g_strndup (id, 16);
285 static GstRTSPSession *
286 create_session (GstRTSPSessionPool * pool, const gchar * id)
288 return gst_rtsp_session_new (id);
292 * gst_rtsp_session_pool_create:
293 * @pool: a #GstRTSPSessionPool
295 * Create a new #GstRTSPSession object in @pool.
297 * Returns: (transfer none): a new #GstRTSPSession.
300 gst_rtsp_session_pool_create (GstRTSPSessionPool * pool)
302 GstRTSPSessionPoolPrivate *priv;
303 GstRTSPSession *result = NULL;
304 GstRTSPSessionPoolClass *klass;
308 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
312 klass = GST_RTSP_SESSION_POOL_GET_CLASS (pool);
316 /* start by creating a new random session id, we assume that this is random
317 * enough to not cause a collision, which we will check later */
318 if (klass->create_session_id)
319 id = klass->create_session_id (pool);
326 g_mutex_lock (&priv->lock);
327 /* check session limit */
328 if (priv->max_sessions > 0) {
329 if (g_hash_table_size (priv->sessions) >= priv->max_sessions)
330 goto too_many_sessions;
332 /* check if the sessionid existed */
333 result = g_hash_table_lookup (priv->sessions, id);
335 /* found, retry with a different session id */
341 /* not found, create session and insert it in the pool */
342 if (klass->create_session)
343 result = create_session (pool, id);
345 goto too_many_sessions;
346 /* take additional ref for the pool */
347 g_object_ref (result);
348 g_hash_table_insert (priv->sessions,
349 (gchar *) gst_rtsp_session_get_sessionid (result), result);
351 g_mutex_unlock (&priv->lock);
354 } while (result == NULL);
361 GST_WARNING ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
366 GST_WARNING ("can't create session id with GstRTSPSessionPool %p", pool);
371 GST_WARNING ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
372 g_mutex_unlock (&priv->lock);
378 GST_WARNING ("session pool reached max sessions of %d", priv->max_sessions);
379 g_mutex_unlock (&priv->lock);
386 * gst_rtsp_session_pool_remove:
387 * @pool: a #GstRTSPSessionPool
388 * @sess: a #GstRTSPSession
390 * Remove @sess from @pool, releasing the ref that the pool has on @sess.
392 * Returns: %TRUE if the session was found and removed.
395 gst_rtsp_session_pool_remove (GstRTSPSessionPool * pool, GstRTSPSession * sess)
397 GstRTSPSessionPoolPrivate *priv;
400 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), FALSE);
401 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
405 g_mutex_lock (&priv->lock);
407 g_hash_table_remove (priv->sessions,
408 gst_rtsp_session_get_sessionid (sess));
409 g_mutex_unlock (&priv->lock);
415 cleanup_func (gchar * sessionid, GstRTSPSession * sess, GTimeVal * now)
417 return gst_rtsp_session_is_expired (sess, now);
421 * gst_rtsp_session_pool_cleanup:
422 * @pool: a #GstRTSPSessionPool
424 * Inspect all the sessions in @pool and remove the sessions that are inactive
425 * for more than their timeout.
427 * Returns: the amount of sessions that got removed.
430 gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
432 GstRTSPSessionPoolPrivate *priv;
436 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
440 g_get_current_time (&now);
442 g_mutex_lock (&priv->lock);
444 g_hash_table_foreach_remove (priv->sessions, (GHRFunc) cleanup_func,
446 g_mutex_unlock (&priv->lock);
453 GstRTSPSessionPool *pool;
454 GstRTSPSessionPoolFilterFunc func;
460 filter_func (gchar * sessionid, GstRTSPSession * sess, FilterData * data)
462 switch (data->func (data->pool, sess, data->user_data)) {
463 case GST_RTSP_FILTER_REMOVE:
465 case GST_RTSP_FILTER_REF:
467 data->list = g_list_prepend (data->list, g_object_ref (sess));
470 case GST_RTSP_FILTER_KEEP:
476 * gst_rtsp_session_pool_filter:
477 * @pool: a #GstRTSPSessionPool
478 * @func: (scope call): a callback
479 * @user_data: user data passed to @func
481 * Call @func for each session in @pool. The result value of @func determines
482 * what happens to the session. @func will be called with the session pool
483 * locked so no further actions on @pool can be performed from @func.
485 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
488 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
490 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
491 * will also be added with an additional ref to the result GList of this
494 * Returns: (element-type GstRTSPSession) (transfer full): a GList with all
495 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
496 * element in the GList should be unreffed before the list is freed.
499 gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
500 GstRTSPSessionPoolFilterFunc func, gpointer user_data)
502 GstRTSPSessionPoolPrivate *priv;
505 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
506 g_return_val_if_fail (func != NULL, NULL);
512 data.user_data = user_data;
515 g_mutex_lock (&priv->lock);
516 g_hash_table_foreach_remove (priv->sessions, (GHRFunc) filter_func, &data);
517 g_mutex_unlock (&priv->lock);
525 GstRTSPSessionPool *pool;
530 collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
535 g_get_current_time (&now);
537 timeout = gst_rtsp_session_next_timeout (sess, &now);
538 GST_INFO ("%p: next timeout: %d", sess, timeout);
539 if (psrc->timeout == -1 || timeout < psrc->timeout)
540 psrc->timeout = timeout;
544 gst_pool_source_prepare (GSource * source, gint * timeout)
546 GstRTSPSessionPoolPrivate *priv;
550 psrc = (GstPoolSource *) source;
552 priv = psrc->pool->priv;
554 g_mutex_lock (&priv->lock);
555 g_hash_table_foreach (priv->sessions, (GHFunc) collect_timeout, psrc);
556 g_mutex_unlock (&priv->lock);
559 *timeout = psrc->timeout;
561 result = psrc->timeout == 0;
563 GST_INFO ("prepare %d, %d", psrc->timeout, result);
569 gst_pool_source_check (GSource * source)
573 return gst_pool_source_prepare (source, NULL);
577 gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
581 GstPoolSource *psrc = (GstPoolSource *) source;
582 GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;
584 GST_INFO ("dispatch");
587 res = func (psrc->pool, user_data);
595 gst_pool_source_finalize (GSource * source)
597 GstPoolSource *psrc = (GstPoolSource *) source;
599 GST_INFO ("finalize %p", psrc);
601 g_object_unref (psrc->pool);
605 static GSourceFuncs gst_pool_source_funcs = {
606 gst_pool_source_prepare,
607 gst_pool_source_check,
608 gst_pool_source_dispatch,
609 gst_pool_source_finalize
613 * gst_rtsp_session_pool_create_watch:
614 * @pool: a #GstRTSPSessionPool
616 * A GSource that will be dispatched when the session should be cleaned up.
619 gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
621 GstPoolSource *source;
623 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
625 source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
626 sizeof (GstPoolSource));
627 source->pool = g_object_ref (pool);
629 return (GSource *) source;