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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "rtsp-session-pool.h"
24 #define DEFAULT_MAX_SESSIONS 0
33 static const gchar session_id_charset[] =
34 { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
35 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
36 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
37 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7',
38 '8', '9', '$', '-', '_', '.', '+'
41 GST_DEBUG_CATEGORY_STATIC (rtsp_session_debug);
42 #define GST_CAT_DEFAULT rtsp_session_debug
44 static void gst_rtsp_session_pool_get_property (GObject * object, guint propid,
45 GValue * value, GParamSpec * pspec);
46 static void gst_rtsp_session_pool_set_property (GObject * object, guint propid,
47 const GValue * value, GParamSpec * pspec);
48 static void gst_rtsp_session_pool_finalize (GObject * object);
50 static gchar *create_session_id (GstRTSPSessionPool * pool);
52 G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);
55 gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
57 GObjectClass *gobject_class;
59 gobject_class = G_OBJECT_CLASS (klass);
61 gobject_class->get_property = gst_rtsp_session_pool_get_property;
62 gobject_class->set_property = gst_rtsp_session_pool_set_property;
63 gobject_class->finalize = gst_rtsp_session_pool_finalize;
65 g_object_class_install_property (gobject_class, PROP_MAX_SESSIONS,
66 g_param_spec_uint ("max-sessions", "Max Sessions",
67 "the maximum amount of sessions (0 = unlimited)",
68 0, G_MAXUINT, DEFAULT_MAX_SESSIONS,
69 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
71 klass->create_session_id = create_session_id;
73 GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsessionpool", 0,
74 "GstRTSPSessionPool");
78 gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
80 pool->lock = g_mutex_new ();
81 pool->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
82 NULL, g_object_unref);
83 pool->max_sessions = DEFAULT_MAX_SESSIONS;
87 gst_rtsp_session_pool_finalize (GObject * object)
89 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
91 g_mutex_free (pool->lock);
92 g_hash_table_unref (pool->sessions);
94 G_OBJECT_CLASS (gst_rtsp_session_pool_parent_class)->finalize (object);
98 gst_rtsp_session_pool_get_property (GObject * object, guint propid,
99 GValue * value, GParamSpec * pspec)
101 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
104 case PROP_MAX_SESSIONS:
105 g_value_set_uint (value, gst_rtsp_session_pool_get_max_sessions (pool));
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
114 gst_rtsp_session_pool_set_property (GObject * object, guint propid,
115 const GValue * value, GParamSpec * pspec)
117 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
120 case PROP_MAX_SESSIONS:
121 gst_rtsp_session_pool_set_max_sessions (pool, g_value_get_uint (value));
124 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
130 * gst_rtsp_session_pool_new:
132 * Create a new #GstRTSPSessionPool instance.
134 * Returns: A new #GstRTSPSessionPool. g_object_unref() after usage.
137 gst_rtsp_session_pool_new (void)
139 GstRTSPSessionPool *result;
141 result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
147 * gst_rtsp_session_pool_set_max_sessions:
148 * @pool: a #GstRTSPSessionPool
149 * @max: the maximum number of sessions
151 * Configure the maximum allowed number of sessions in @pool to @max.
152 * A value of 0 means an unlimited amount of sessions.
155 gst_rtsp_session_pool_set_max_sessions (GstRTSPSessionPool * pool, guint max)
157 g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
159 g_mutex_lock (pool->lock);
160 pool->max_sessions = max;
161 g_mutex_unlock (pool->lock);
165 * gst_rtsp_session_pool_get_max_sessions:
166 * @pool: a #GstRTSPSessionPool
168 * Get the maximum allowed number of sessions in @pool. 0 means an unlimited
169 * amount of sessions.
171 * Returns: the maximum allowed number of sessions.
174 gst_rtsp_session_pool_get_max_sessions (GstRTSPSessionPool * pool)
178 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
180 g_mutex_lock (pool->lock);
181 result = pool->max_sessions;
182 g_mutex_unlock (pool->lock);
188 * gst_rtsp_session_pool_get_n_sessions:
189 * @pool: a #GstRTSPSessionPool
191 * Get the amount of active sessions in @pool.
193 * Returns: the amount of active sessions in @pool.
196 gst_rtsp_session_pool_get_n_sessions (GstRTSPSessionPool * pool)
200 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
202 g_mutex_lock (pool->lock);
203 result = g_hash_table_size (pool->sessions);
204 g_mutex_unlock (pool->lock);
210 * gst_rtsp_session_pool_find:
211 * @pool: the pool to search
212 * @sessionid: the session id
214 * Find the session with @sessionid in @pool. The access time of the session
215 * will be updated with gst_rtsp_session_touch().
217 * Returns: the #GstRTSPSession with @sessionid or %NULL when the session did
218 * not exist. g_object_unref() after usage.
221 gst_rtsp_session_pool_find (GstRTSPSessionPool * pool, const gchar * sessionid)
223 GstRTSPSession *result;
225 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
226 g_return_val_if_fail (sessionid != NULL, NULL);
228 g_mutex_lock (pool->lock);
229 result = g_hash_table_lookup (pool->sessions, sessionid);
231 g_object_ref (result);
232 gst_rtsp_session_touch (result);
234 g_mutex_unlock (pool->lock);
240 create_session_id (GstRTSPSessionPool * pool)
245 for (i = 0; i < 16; i++) {
247 session_id_charset[g_random_int_range (0,
248 G_N_ELEMENTS (session_id_charset))];
251 return g_strndup (id, 16);
255 * gst_rtsp_session_pool_create:
256 * @pool: a #GstRTSPSessionPool
258 * Create a new #GstRTSPSession object in @pool.
260 * Returns: a new #GstRTSPSession.
263 gst_rtsp_session_pool_create (GstRTSPSessionPool * pool)
265 GstRTSPSession *result = NULL;
266 GstRTSPSessionPoolClass *klass;
270 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
272 klass = GST_RTSP_SESSION_POOL_GET_CLASS (pool);
276 /* start by creating a new random session id, we assume that this is random
277 * enough to not cause a collision, which we will check later */
278 if (klass->create_session_id)
279 id = klass->create_session_id (pool);
286 g_mutex_lock (pool->lock);
287 /* check session limit */
288 if (pool->max_sessions > 0) {
289 if (g_hash_table_size (pool->sessions) >= pool->max_sessions)
290 goto too_many_sessions;
292 /* check if the sessionid existed */
293 result = g_hash_table_lookup (pool->sessions, id);
295 /* found, retry with a different session id */
301 /* not found, create session and insert it in the pool */
302 result = gst_rtsp_session_new (id);
303 /* take additional ref for the pool */
304 g_object_ref (result);
305 g_hash_table_insert (pool->sessions, result->sessionid, result);
307 g_mutex_unlock (pool->lock);
310 } while (result == NULL);
317 GST_WARNING ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
322 GST_WARNING ("can't create session id with GstRTSPSessionPool %p", pool);
327 GST_WARNING ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
328 g_mutex_unlock (pool->lock);
334 GST_WARNING ("session pool reached max sessions of %d", pool->max_sessions);
335 g_mutex_unlock (pool->lock);
342 * gst_rtsp_session_pool_remove:
343 * @pool: a #GstRTSPSessionPool
344 * @sess: a #GstRTSPSession
346 * Remove @sess from @pool, releasing the ref that the pool has on @sess.
348 * Returns: %TRUE if the session was found and removed.
351 gst_rtsp_session_pool_remove (GstRTSPSessionPool * pool, GstRTSPSession * sess)
355 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), FALSE);
356 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
358 g_mutex_lock (pool->lock);
359 found = g_hash_table_remove (pool->sessions, sess->sessionid);
360 g_mutex_unlock (pool->lock);
366 cleanup_func (gchar * sessionid, GstRTSPSession * sess, GTimeVal * now)
368 return gst_rtsp_session_is_expired (sess, now);
372 * gst_rtsp_session_pool_cleanup:
373 * @pool: a #GstRTSPSessionPool
375 * Inspect all the sessions in @pool and remove the sessions that are inactive
376 * for more than their timeout.
378 * Returns: the amount of sessions that got removed.
381 gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
386 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
388 g_get_current_time (&now);
390 g_mutex_lock (pool->lock);
392 g_hash_table_foreach_remove (pool->sessions, (GHRFunc) cleanup_func,
394 g_mutex_unlock (pool->lock);
401 GstRTSPSessionPool *pool;
402 GstRTSPSessionFilterFunc func;
408 filter_func (gchar * sessionid, GstRTSPSession * sess, FilterData * data)
410 switch (data->func (data->pool, sess, data->user_data)) {
411 case GST_RTSP_FILTER_REMOVE:
413 case GST_RTSP_FILTER_REF:
415 data->list = g_list_prepend (data->list, g_object_ref (sess));
418 case GST_RTSP_FILTER_KEEP:
424 * gst_rtsp_session_pool_filter:
425 * @pool: a #GstRTSPSessionPool
427 * @user_data: user data passed to @func
429 * Call @func for each session in @pool. The result value of @func determines
430 * what happens to the session. @func will be called with the session pool
431 * locked so no further actions on @pool can be performed from @func.
433 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
436 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
438 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
439 * will also be added with an additional ref to the result GList of this
442 * Returns: a GList with all sessions for which @func returned
443 * #GST_RTSP_FILTER_REF. After usage, each element in the GList should be unreffed
444 * before the list is freed.
447 gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
448 GstRTSPSessionFilterFunc func, gpointer user_data)
452 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
453 g_return_val_if_fail (func != NULL, NULL);
457 data.user_data = user_data;
460 g_mutex_lock (pool->lock);
461 g_hash_table_foreach_remove (pool->sessions, (GHRFunc) filter_func, &data);
462 g_mutex_unlock (pool->lock);
470 GstRTSPSessionPool *pool;
475 collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
480 g_get_current_time (&now);
482 timeout = gst_rtsp_session_next_timeout (sess, &now);
483 GST_INFO ("%p: next timeout: %d", sess, timeout);
484 if (psrc->timeout == -1 || timeout < psrc->timeout)
485 psrc->timeout = timeout;
489 gst_pool_source_prepare (GSource * source, gint * timeout)
494 psrc = (GstPoolSource *) source;
497 g_mutex_lock (psrc->pool->lock);
498 g_hash_table_foreach (psrc->pool->sessions, (GHFunc) collect_timeout, psrc);
499 g_mutex_unlock (psrc->pool->lock);
502 *timeout = psrc->timeout;
504 result = psrc->timeout == 0;
506 GST_INFO ("prepare %d, %d", psrc->timeout, result);
512 gst_pool_source_check (GSource * source)
516 return gst_pool_source_prepare (source, NULL);
520 gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
524 GstPoolSource *psrc = (GstPoolSource *) source;
525 GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;
527 GST_INFO ("dispatch");
530 res = func (psrc->pool, user_data);
538 gst_pool_source_finalize (GSource * source)
540 GstPoolSource *psrc = (GstPoolSource *) source;
542 GST_INFO ("finalize %p", psrc);
544 g_object_unref (psrc->pool);
548 static GSourceFuncs gst_pool_source_funcs = {
549 gst_pool_source_prepare,
550 gst_pool_source_check,
551 gst_pool_source_dispatch,
552 gst_pool_source_finalize
556 * gst_rtsp_session_pool_create_watch:
557 * @pool: a #GstRTSPSessionPool
559 * A GSource that will be dispatched when the session should be cleaned up.
562 gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
564 GstPoolSource *source;
566 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
568 source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
569 sizeof (GstPoolSource));
570 source->pool = g_object_ref (pool);
572 return (GSource *) source;