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
33 #define DEFAULT_MAX_SESSIONS 0
42 static const gchar session_id_charset[] =
43 { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
44 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
45 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
46 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7',
47 '8', '9', '$', '-', '_', '.', '+'
50 GST_DEBUG_CATEGORY_STATIC (rtsp_session_debug);
51 #define GST_CAT_DEFAULT rtsp_session_debug
53 static void gst_rtsp_session_pool_get_property (GObject * object, guint propid,
54 GValue * value, GParamSpec * pspec);
55 static void gst_rtsp_session_pool_set_property (GObject * object, guint propid,
56 const GValue * value, GParamSpec * pspec);
57 static void gst_rtsp_session_pool_finalize (GObject * object);
59 static gchar *create_session_id (GstRTSPSessionPool * pool);
61 G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);
64 gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
66 GObjectClass *gobject_class;
68 g_type_class_add_private (klass, sizeof (GstRTSPSessionPoolPrivate));
70 gobject_class = G_OBJECT_CLASS (klass);
72 gobject_class->get_property = gst_rtsp_session_pool_get_property;
73 gobject_class->set_property = gst_rtsp_session_pool_set_property;
74 gobject_class->finalize = gst_rtsp_session_pool_finalize;
76 g_object_class_install_property (gobject_class, PROP_MAX_SESSIONS,
77 g_param_spec_uint ("max-sessions", "Max Sessions",
78 "the maximum amount of sessions (0 = unlimited)",
79 0, G_MAXUINT, DEFAULT_MAX_SESSIONS,
80 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
82 klass->create_session_id = create_session_id;
84 GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsessionpool", 0,
85 "GstRTSPSessionPool");
89 gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
91 GstRTSPSessionPoolPrivate *priv = GST_RTSP_SESSION_POOL_GET_PRIVATE (pool);
95 g_mutex_init (&priv->lock);
96 priv->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
97 NULL, g_object_unref);
98 priv->max_sessions = DEFAULT_MAX_SESSIONS;
102 gst_rtsp_session_pool_finalize (GObject * object)
104 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
105 GstRTSPSessionPoolPrivate *priv = pool->priv;
107 g_mutex_clear (&priv->lock);
108 g_hash_table_unref (priv->sessions);
110 G_OBJECT_CLASS (gst_rtsp_session_pool_parent_class)->finalize (object);
114 gst_rtsp_session_pool_get_property (GObject * object, guint propid,
115 GValue * value, GParamSpec * pspec)
117 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
120 case PROP_MAX_SESSIONS:
121 g_value_set_uint (value, gst_rtsp_session_pool_get_max_sessions (pool));
124 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
130 gst_rtsp_session_pool_set_property (GObject * object, guint propid,
131 const GValue * value, GParamSpec * pspec)
133 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
136 case PROP_MAX_SESSIONS:
137 gst_rtsp_session_pool_set_max_sessions (pool, g_value_get_uint (value));
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
146 * gst_rtsp_session_pool_new:
148 * Create a new #GstRTSPSessionPool instance.
150 * Returns: A new #GstRTSPSessionPool. g_object_unref() after usage.
153 gst_rtsp_session_pool_new (void)
155 GstRTSPSessionPool *result;
157 result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
163 * gst_rtsp_session_pool_set_max_sessions:
164 * @pool: a #GstRTSPSessionPool
165 * @max: the maximum number of sessions
167 * Configure the maximum allowed number of sessions in @pool to @max.
168 * A value of 0 means an unlimited amount of sessions.
171 gst_rtsp_session_pool_set_max_sessions (GstRTSPSessionPool * pool, guint max)
173 GstRTSPSessionPoolPrivate *priv;
175 g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
179 g_mutex_lock (&priv->lock);
180 priv->max_sessions = max;
181 g_mutex_unlock (&priv->lock);
185 * gst_rtsp_session_pool_get_max_sessions:
186 * @pool: a #GstRTSPSessionPool
188 * Get the maximum allowed number of sessions in @pool. 0 means an unlimited
189 * amount of sessions.
191 * Returns: the maximum allowed number of sessions.
194 gst_rtsp_session_pool_get_max_sessions (GstRTSPSessionPool * pool)
196 GstRTSPSessionPoolPrivate *priv;
199 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
203 g_mutex_lock (&priv->lock);
204 result = priv->max_sessions;
205 g_mutex_unlock (&priv->lock);
211 * gst_rtsp_session_pool_get_n_sessions:
212 * @pool: a #GstRTSPSessionPool
214 * Get the amount of active sessions in @pool.
216 * Returns: the amount of active sessions in @pool.
219 gst_rtsp_session_pool_get_n_sessions (GstRTSPSessionPool * pool)
221 GstRTSPSessionPoolPrivate *priv;
224 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
228 g_mutex_lock (&priv->lock);
229 result = g_hash_table_size (priv->sessions);
230 g_mutex_unlock (&priv->lock);
236 * gst_rtsp_session_pool_find:
237 * @pool: the pool to search
238 * @sessionid: the session id
240 * Find the session with @sessionid in @pool. The access time of the session
241 * will be updated with gst_rtsp_session_touch().
243 * Returns: (transfer full): the #GstRTSPSession with @sessionid or %NULL when the session did
244 * not exist. g_object_unref() after usage.
247 gst_rtsp_session_pool_find (GstRTSPSessionPool * pool, const gchar * sessionid)
249 GstRTSPSessionPoolPrivate *priv;
250 GstRTSPSession *result;
252 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
253 g_return_val_if_fail (sessionid != NULL, NULL);
257 g_mutex_lock (&priv->lock);
258 result = g_hash_table_lookup (priv->sessions, sessionid);
260 g_object_ref (result);
261 gst_rtsp_session_touch (result);
263 g_mutex_unlock (&priv->lock);
269 create_session_id (GstRTSPSessionPool * pool)
274 for (i = 0; i < 16; i++) {
276 session_id_charset[g_random_int_range (0,
277 G_N_ELEMENTS (session_id_charset))];
280 return g_strndup (id, 16);
284 * gst_rtsp_session_pool_create:
285 * @pool: a #GstRTSPSessionPool
287 * Create a new #GstRTSPSession object in @pool.
289 * Returns: (transfer none): a new #GstRTSPSession.
292 gst_rtsp_session_pool_create (GstRTSPSessionPool * pool)
294 GstRTSPSessionPoolPrivate *priv;
295 GstRTSPSession *result = NULL;
296 GstRTSPSessionPoolClass *klass;
300 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
304 klass = GST_RTSP_SESSION_POOL_GET_CLASS (pool);
308 /* start by creating a new random session id, we assume that this is random
309 * enough to not cause a collision, which we will check later */
310 if (klass->create_session_id)
311 id = klass->create_session_id (pool);
318 g_mutex_lock (&priv->lock);
319 /* check session limit */
320 if (priv->max_sessions > 0) {
321 if (g_hash_table_size (priv->sessions) >= priv->max_sessions)
322 goto too_many_sessions;
324 /* check if the sessionid existed */
325 result = g_hash_table_lookup (priv->sessions, id);
327 /* found, retry with a different session id */
333 /* not found, create session and insert it in the pool */
334 result = gst_rtsp_session_new (id);
335 /* take additional ref for the pool */
336 g_object_ref (result);
337 g_hash_table_insert (priv->sessions,
338 (gchar *) gst_rtsp_session_get_sessionid (result), result);
340 g_mutex_unlock (&priv->lock);
343 } while (result == NULL);
350 GST_WARNING ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
355 GST_WARNING ("can't create session id with GstRTSPSessionPool %p", pool);
360 GST_WARNING ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
361 g_mutex_unlock (&priv->lock);
367 GST_WARNING ("session pool reached max sessions of %d", priv->max_sessions);
368 g_mutex_unlock (&priv->lock);
375 * gst_rtsp_session_pool_remove:
376 * @pool: a #GstRTSPSessionPool
377 * @sess: a #GstRTSPSession
379 * Remove @sess from @pool, releasing the ref that the pool has on @sess.
381 * Returns: %TRUE if the session was found and removed.
384 gst_rtsp_session_pool_remove (GstRTSPSessionPool * pool, GstRTSPSession * sess)
386 GstRTSPSessionPoolPrivate *priv;
389 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), FALSE);
390 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
394 g_mutex_lock (&priv->lock);
396 g_hash_table_remove (priv->sessions,
397 gst_rtsp_session_get_sessionid (sess));
398 g_mutex_unlock (&priv->lock);
404 cleanup_func (gchar * sessionid, GstRTSPSession * sess, GTimeVal * now)
406 return gst_rtsp_session_is_expired (sess, now);
410 * gst_rtsp_session_pool_cleanup:
411 * @pool: a #GstRTSPSessionPool
413 * Inspect all the sessions in @pool and remove the sessions that are inactive
414 * for more than their timeout.
416 * Returns: the amount of sessions that got removed.
419 gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
421 GstRTSPSessionPoolPrivate *priv;
425 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
429 g_get_current_time (&now);
431 g_mutex_lock (&priv->lock);
433 g_hash_table_foreach_remove (priv->sessions, (GHRFunc) cleanup_func,
435 g_mutex_unlock (&priv->lock);
442 GstRTSPSessionPool *pool;
443 GstRTSPSessionPoolFilterFunc func;
449 filter_func (gchar * sessionid, GstRTSPSession * sess, FilterData * data)
451 switch (data->func (data->pool, sess, data->user_data)) {
452 case GST_RTSP_FILTER_REMOVE:
454 case GST_RTSP_FILTER_REF:
456 data->list = g_list_prepend (data->list, g_object_ref (sess));
459 case GST_RTSP_FILTER_KEEP:
465 * gst_rtsp_session_pool_filter:
466 * @pool: a #GstRTSPSessionPool
467 * @func: (scope call): a callback
468 * @user_data: user data passed to @func
470 * Call @func for each session in @pool. The result value of @func determines
471 * what happens to the session. @func will be called with the session pool
472 * locked so no further actions on @pool can be performed from @func.
474 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
477 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
479 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
480 * will also be added with an additional ref to the result GList of this
483 * Returns: (element-type GstRTSPSession) (transfer full): a GList with all
484 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
485 * element in the GList should be unreffed before the list is freed.
488 gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
489 GstRTSPSessionPoolFilterFunc func, gpointer user_data)
491 GstRTSPSessionPoolPrivate *priv;
494 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
495 g_return_val_if_fail (func != NULL, NULL);
501 data.user_data = user_data;
504 g_mutex_lock (&priv->lock);
505 g_hash_table_foreach_remove (priv->sessions, (GHRFunc) filter_func, &data);
506 g_mutex_unlock (&priv->lock);
514 GstRTSPSessionPool *pool;
519 collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
525 tmp = g_source_get_time ((GSource *) psrc);
526 now.tv_sec = tmp / G_USEC_PER_SEC;
527 now.tv_usec = tmp % G_USEC_PER_SEC;
529 timeout = gst_rtsp_session_next_timeout (sess, &now);
530 GST_INFO ("%p: next timeout: %d", sess, timeout);
531 if (psrc->timeout == -1 || timeout < psrc->timeout)
532 psrc->timeout = timeout;
536 gst_pool_source_prepare (GSource * source, gint * timeout)
538 GstRTSPSessionPoolPrivate *priv;
542 psrc = (GstPoolSource *) source;
544 priv = psrc->pool->priv;
546 g_mutex_lock (&priv->lock);
547 g_hash_table_foreach (priv->sessions, (GHFunc) collect_timeout, psrc);
548 g_mutex_unlock (&priv->lock);
551 *timeout = psrc->timeout;
553 result = psrc->timeout == 0;
555 GST_INFO ("prepare %d, %d", psrc->timeout, result);
561 gst_pool_source_check (GSource * source)
565 return gst_pool_source_prepare (source, NULL);
569 gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
573 GstPoolSource *psrc = (GstPoolSource *) source;
574 GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;
576 GST_INFO ("dispatch");
579 res = func (psrc->pool, user_data);
587 gst_pool_source_finalize (GSource * source)
589 GstPoolSource *psrc = (GstPoolSource *) source;
591 GST_INFO ("finalize %p", psrc);
593 g_object_unref (psrc->pool);
597 static GSourceFuncs gst_pool_source_funcs = {
598 gst_pool_source_prepare,
599 gst_pool_source_check,
600 gst_pool_source_dispatch,
601 gst_pool_source_finalize
605 * gst_rtsp_session_pool_create_watch:
606 * @pool: a #GstRTSPSessionPool
608 * A GSource that will be dispatched when the session should be cleaned up.
611 gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
613 GstPoolSource *source;
615 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
617 source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
618 sizeof (GstPoolSource));
619 source->pool = g_object_ref (pool);
621 return (GSource *) source;