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);
60 G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);
63 gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
65 GObjectClass *gobject_class;
67 g_type_class_add_private (klass, sizeof (GstRTSPSessionPoolPrivate));
69 gobject_class = G_OBJECT_CLASS (klass);
71 gobject_class->get_property = gst_rtsp_session_pool_get_property;
72 gobject_class->set_property = gst_rtsp_session_pool_set_property;
73 gobject_class->finalize = gst_rtsp_session_pool_finalize;
75 g_object_class_install_property (gobject_class, PROP_MAX_SESSIONS,
76 g_param_spec_uint ("max-sessions", "Max Sessions",
77 "the maximum amount of sessions (0 = unlimited)",
78 0, G_MAXUINT, DEFAULT_MAX_SESSIONS,
79 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
81 klass->create_session_id = create_session_id;
83 GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsessionpool", 0,
84 "GstRTSPSessionPool");
88 gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
90 GstRTSPSessionPoolPrivate *priv = GST_RTSP_SESSION_POOL_GET_PRIVATE (pool);
94 g_mutex_init (&priv->lock);
95 priv->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
96 NULL, g_object_unref);
97 priv->max_sessions = DEFAULT_MAX_SESSIONS;
101 gst_rtsp_session_pool_finalize (GObject * object)
103 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
104 GstRTSPSessionPoolPrivate *priv = pool->priv;
106 g_mutex_clear (&priv->lock);
107 g_hash_table_unref (priv->sessions);
109 G_OBJECT_CLASS (gst_rtsp_session_pool_parent_class)->finalize (object);
113 gst_rtsp_session_pool_get_property (GObject * object, guint propid,
114 GValue * value, GParamSpec * pspec)
116 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
119 case PROP_MAX_SESSIONS:
120 g_value_set_uint (value, gst_rtsp_session_pool_get_max_sessions (pool));
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
129 gst_rtsp_session_pool_set_property (GObject * object, guint propid,
130 const GValue * value, GParamSpec * pspec)
132 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
135 case PROP_MAX_SESSIONS:
136 gst_rtsp_session_pool_set_max_sessions (pool, g_value_get_uint (value));
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
145 * gst_rtsp_session_pool_new:
147 * Create a new #GstRTSPSessionPool instance.
149 * Returns: A new #GstRTSPSessionPool. g_object_unref() after usage.
152 gst_rtsp_session_pool_new (void)
154 GstRTSPSessionPool *result;
156 result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
162 * gst_rtsp_session_pool_set_max_sessions:
163 * @pool: a #GstRTSPSessionPool
164 * @max: the maximum number of sessions
166 * Configure the maximum allowed number of sessions in @pool to @max.
167 * A value of 0 means an unlimited amount of sessions.
170 gst_rtsp_session_pool_set_max_sessions (GstRTSPSessionPool * pool, guint max)
172 GstRTSPSessionPoolPrivate *priv;
174 g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
178 g_mutex_lock (&priv->lock);
179 priv->max_sessions = max;
180 g_mutex_unlock (&priv->lock);
184 * gst_rtsp_session_pool_get_max_sessions:
185 * @pool: a #GstRTSPSessionPool
187 * Get the maximum allowed number of sessions in @pool. 0 means an unlimited
188 * amount of sessions.
190 * Returns: the maximum allowed number of sessions.
193 gst_rtsp_session_pool_get_max_sessions (GstRTSPSessionPool * pool)
195 GstRTSPSessionPoolPrivate *priv;
198 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
202 g_mutex_lock (&priv->lock);
203 result = priv->max_sessions;
204 g_mutex_unlock (&priv->lock);
210 * gst_rtsp_session_pool_get_n_sessions:
211 * @pool: a #GstRTSPSessionPool
213 * Get the amount of active sessions in @pool.
215 * Returns: the amount of active sessions in @pool.
218 gst_rtsp_session_pool_get_n_sessions (GstRTSPSessionPool * pool)
220 GstRTSPSessionPoolPrivate *priv;
223 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
227 g_mutex_lock (&priv->lock);
228 result = g_hash_table_size (priv->sessions);
229 g_mutex_unlock (&priv->lock);
235 * gst_rtsp_session_pool_find:
236 * @pool: the pool to search
237 * @sessionid: the session id
239 * Find the session with @sessionid in @pool. The access time of the session
240 * will be updated with gst_rtsp_session_touch().
242 * Returns: (transfer full): the #GstRTSPSession with @sessionid or %NULL when the session did
243 * not exist. g_object_unref() after usage.
246 gst_rtsp_session_pool_find (GstRTSPSessionPool * pool, const gchar * sessionid)
248 GstRTSPSessionPoolPrivate *priv;
249 GstRTSPSession *result;
251 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
252 g_return_val_if_fail (sessionid != NULL, NULL);
256 g_mutex_lock (&priv->lock);
257 result = g_hash_table_lookup (priv->sessions, sessionid);
259 g_object_ref (result);
260 gst_rtsp_session_touch (result);
262 g_mutex_unlock (&priv->lock);
268 create_session_id (GstRTSPSessionPool * pool)
273 for (i = 0; i < 16; i++) {
275 session_id_charset[g_random_int_range (0,
276 G_N_ELEMENTS (session_id_charset))];
279 return g_strndup (id, 16);
283 * gst_rtsp_session_pool_create:
284 * @pool: a #GstRTSPSessionPool
286 * Create a new #GstRTSPSession object in @pool.
288 * Returns: (transfer none): a new #GstRTSPSession.
291 gst_rtsp_session_pool_create (GstRTSPSessionPool * pool)
293 GstRTSPSessionPoolPrivate *priv;
294 GstRTSPSession *result = NULL;
295 GstRTSPSessionPoolClass *klass;
299 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
303 klass = GST_RTSP_SESSION_POOL_GET_CLASS (pool);
307 /* start by creating a new random session id, we assume that this is random
308 * enough to not cause a collision, which we will check later */
309 if (klass->create_session_id)
310 id = klass->create_session_id (pool);
317 g_mutex_lock (&priv->lock);
318 /* check session limit */
319 if (priv->max_sessions > 0) {
320 if (g_hash_table_size (priv->sessions) >= priv->max_sessions)
321 goto too_many_sessions;
323 /* check if the sessionid existed */
324 result = g_hash_table_lookup (priv->sessions, id);
326 /* found, retry with a different session id */
332 /* not found, create session and insert it in the pool */
333 result = gst_rtsp_session_new (id);
334 /* take additional ref for the pool */
335 g_object_ref (result);
336 g_hash_table_insert (priv->sessions,
337 (gchar *) gst_rtsp_session_get_sessionid (result), result);
339 g_mutex_unlock (&priv->lock);
342 } while (result == NULL);
349 GST_WARNING ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
354 GST_WARNING ("can't create session id with GstRTSPSessionPool %p", pool);
359 GST_WARNING ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
360 g_mutex_unlock (&priv->lock);
366 GST_WARNING ("session pool reached max sessions of %d", priv->max_sessions);
367 g_mutex_unlock (&priv->lock);
374 * gst_rtsp_session_pool_remove:
375 * @pool: a #GstRTSPSessionPool
376 * @sess: a #GstRTSPSession
378 * Remove @sess from @pool, releasing the ref that the pool has on @sess.
380 * Returns: %TRUE if the session was found and removed.
383 gst_rtsp_session_pool_remove (GstRTSPSessionPool * pool, GstRTSPSession * sess)
385 GstRTSPSessionPoolPrivate *priv;
388 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), FALSE);
389 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
393 g_mutex_lock (&priv->lock);
395 g_hash_table_remove (priv->sessions,
396 gst_rtsp_session_get_sessionid (sess));
397 g_mutex_unlock (&priv->lock);
403 cleanup_func (gchar * sessionid, GstRTSPSession * sess, GTimeVal * now)
405 return gst_rtsp_session_is_expired (sess, now);
409 * gst_rtsp_session_pool_cleanup:
410 * @pool: a #GstRTSPSessionPool
412 * Inspect all the sessions in @pool and remove the sessions that are inactive
413 * for more than their timeout.
415 * Returns: the amount of sessions that got removed.
418 gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
420 GstRTSPSessionPoolPrivate *priv;
424 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
428 g_get_current_time (&now);
430 g_mutex_lock (&priv->lock);
432 g_hash_table_foreach_remove (priv->sessions, (GHRFunc) cleanup_func,
434 g_mutex_unlock (&priv->lock);
441 GstRTSPSessionPool *pool;
442 GstRTSPSessionPoolFilterFunc func;
448 filter_func (gchar * sessionid, GstRTSPSession * sess, FilterData * data)
450 switch (data->func (data->pool, sess, data->user_data)) {
451 case GST_RTSP_FILTER_REMOVE:
453 case GST_RTSP_FILTER_REF:
455 data->list = g_list_prepend (data->list, g_object_ref (sess));
458 case GST_RTSP_FILTER_KEEP:
464 * gst_rtsp_session_pool_filter:
465 * @pool: a #GstRTSPSessionPool
466 * @func: (scope call): a callback
467 * @user_data: user data passed to @func
469 * Call @func for each session in @pool. The result value of @func determines
470 * what happens to the session. @func will be called with the session pool
471 * locked so no further actions on @pool can be performed from @func.
473 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
476 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
478 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
479 * will also be added with an additional ref to the result GList of this
482 * Returns: (element-type GstRTSPSession) (transfer full): a GList with all
483 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
484 * element in the GList should be unreffed before the list is freed.
487 gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
488 GstRTSPSessionPoolFilterFunc func, gpointer user_data)
490 GstRTSPSessionPoolPrivate *priv;
493 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
494 g_return_val_if_fail (func != NULL, NULL);
500 data.user_data = user_data;
503 g_mutex_lock (&priv->lock);
504 g_hash_table_foreach_remove (priv->sessions, (GHRFunc) filter_func, &data);
505 g_mutex_unlock (&priv->lock);
513 GstRTSPSessionPool *pool;
518 collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
524 tmp = g_source_get_time ((GSource *) psrc);
525 now.tv_sec = tmp / G_USEC_PER_SEC;
526 now.tv_usec = tmp % G_USEC_PER_SEC;
528 timeout = gst_rtsp_session_next_timeout (sess, &now);
529 GST_INFO ("%p: next timeout: %d", sess, timeout);
530 if (psrc->timeout == -1 || timeout < psrc->timeout)
531 psrc->timeout = timeout;
535 gst_pool_source_prepare (GSource * source, gint * timeout)
537 GstRTSPSessionPoolPrivate *priv;
541 psrc = (GstPoolSource *) source;
543 priv = psrc->pool->priv;
545 g_mutex_lock (&priv->lock);
546 g_hash_table_foreach (priv->sessions, (GHFunc) collect_timeout, psrc);
547 g_mutex_unlock (&priv->lock);
550 *timeout = psrc->timeout;
552 result = psrc->timeout == 0;
554 GST_INFO ("prepare %d, %d", psrc->timeout, result);
560 gst_pool_source_check (GSource * source)
564 return gst_pool_source_prepare (source, NULL);
568 gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
572 GstPoolSource *psrc = (GstPoolSource *) source;
573 GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;
575 GST_INFO ("dispatch");
578 res = func (psrc->pool, user_data);
586 gst_pool_source_finalize (GSource * source)
588 GstPoolSource *psrc = (GstPoolSource *) source;
590 GST_INFO ("finalize %p", psrc);
592 g_object_unref (psrc->pool);
596 static GSourceFuncs gst_pool_source_funcs = {
597 gst_pool_source_prepare,
598 gst_pool_source_check,
599 gst_pool_source_dispatch,
600 gst_pool_source_finalize
604 * gst_rtsp_session_pool_create_watch:
605 * @pool: a #GstRTSPSessionPool
607 * A GSource that will be dispatched when the session should be cleaned up.
610 gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
612 GstPoolSource *source;
614 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
616 source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
617 sizeof (GstPoolSource));
618 source->pool = g_object_ref (pool);
620 return (GSource *) source;