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 GST_DEBUG_CATEGORY_STATIC (rtsp_session_debug);
34 #define GST_CAT_DEFAULT rtsp_session_debug
36 static void gst_rtsp_session_pool_get_property (GObject * object, guint propid,
37 GValue * value, GParamSpec * pspec);
38 static void gst_rtsp_session_pool_set_property (GObject * object, guint propid,
39 const GValue * value, GParamSpec * pspec);
40 static void gst_rtsp_session_pool_finalize (GObject * object);
42 static gchar *create_session_id (GstRTSPSessionPool * pool);
44 G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);
47 gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
49 GObjectClass *gobject_class;
51 gobject_class = G_OBJECT_CLASS (klass);
53 gobject_class->get_property = gst_rtsp_session_pool_get_property;
54 gobject_class->set_property = gst_rtsp_session_pool_set_property;
55 gobject_class->finalize = gst_rtsp_session_pool_finalize;
57 g_object_class_install_property (gobject_class, PROP_MAX_SESSIONS,
58 g_param_spec_uint ("max-sessions", "Max Sessions",
59 "the maximum amount of sessions (0 = unlimited)",
60 0, G_MAXUINT, DEFAULT_MAX_SESSIONS,
61 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
63 klass->create_session_id = create_session_id;
65 GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsessionpool", 0,
66 "GstRTSPSessionPool");
70 gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
72 pool->lock = g_mutex_new ();
73 pool->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
74 NULL, g_object_unref);
75 pool->max_sessions = DEFAULT_MAX_SESSIONS;
79 gst_rtsp_session_pool_finalize (GObject * object)
81 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
83 g_mutex_free (pool->lock);
84 g_hash_table_unref (pool->sessions);
86 G_OBJECT_CLASS (gst_rtsp_session_pool_parent_class)->finalize (object);
90 gst_rtsp_session_pool_get_property (GObject * object, guint propid,
91 GValue * value, GParamSpec * pspec)
93 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
96 case PROP_MAX_SESSIONS:
97 g_value_set_uint (value, gst_rtsp_session_pool_get_max_sessions (pool));
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
106 gst_rtsp_session_pool_set_property (GObject * object, guint propid,
107 const GValue * value, GParamSpec * pspec)
109 GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
112 case PROP_MAX_SESSIONS:
113 gst_rtsp_session_pool_set_max_sessions (pool, g_value_get_uint (value));
116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
122 * gst_rtsp_session_pool_new:
124 * Create a new #GstRTSPSessionPool instance.
126 * Returns: A new #GstRTSPSessionPool. g_object_unref() after usage.
129 gst_rtsp_session_pool_new (void)
131 GstRTSPSessionPool *result;
133 result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
139 * gst_rtsp_session_pool_set_max_sessions:
140 * @pool: a #GstRTSPSessionPool
141 * @max: the maximum number of sessions
143 * Configure the maximum allowed number of sessions in @pool to @max.
144 * A value of 0 means an unlimited amount of sessions.
147 gst_rtsp_session_pool_set_max_sessions (GstRTSPSessionPool * pool, guint max)
149 g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
151 g_mutex_lock (pool->lock);
152 pool->max_sessions = max;
153 g_mutex_unlock (pool->lock);
157 * gst_rtsp_session_pool_get_max_sessions:
158 * @pool: a #GstRTSPSessionPool
160 * Get the maximum allowed number of sessions in @pool. 0 means an unlimited
161 * amount of sessions.
163 * Returns: the maximum allowed number of sessions.
166 gst_rtsp_session_pool_get_max_sessions (GstRTSPSessionPool * pool)
170 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
172 g_mutex_lock (pool->lock);
173 result = pool->max_sessions;
174 g_mutex_unlock (pool->lock);
180 * gst_rtsp_session_pool_get_n_sessions:
181 * @pool: a #GstRTSPSessionPool
183 * Get the amount of active sessions in @pool.
185 * Returns: the amount of active sessions in @pool.
188 gst_rtsp_session_pool_get_n_sessions (GstRTSPSessionPool * pool)
192 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
194 g_mutex_lock (pool->lock);
195 result = g_hash_table_size (pool->sessions);
196 g_mutex_unlock (pool->lock);
202 * gst_rtsp_session_pool_find:
203 * @pool: the pool to search
204 * @sessionid: the session id
206 * Find the session with @sessionid in @pool. The access time of the session
207 * will be updated with gst_rtsp_session_touch().
209 * Returns: the #GstRTSPSession with @sessionid or %NULL when the session did
210 * not exist. g_object_unref() after usage.
213 gst_rtsp_session_pool_find (GstRTSPSessionPool * pool, const gchar * sessionid)
215 GstRTSPSession *result;
217 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
218 g_return_val_if_fail (sessionid != NULL, NULL);
220 g_mutex_lock (pool->lock);
221 result = g_hash_table_lookup (pool->sessions, sessionid);
223 g_object_ref (result);
224 gst_rtsp_session_touch (result);
226 g_mutex_unlock (pool->lock);
232 create_session_id (GstRTSPSessionPool * pool)
237 for (i = 0; i < 16; i++) {
238 id[i] = g_random_int_range ('a', 'z');
241 return g_strndup (id, 16);
245 * gst_rtsp_session_pool_create:
246 * @pool: a #GstRTSPSessionPool
248 * Create a new #GstRTSPSession object in @pool.
250 * Returns: a new #GstRTSPSession.
253 gst_rtsp_session_pool_create (GstRTSPSessionPool * pool)
255 GstRTSPSession *result = NULL;
256 GstRTSPSessionPoolClass *klass;
260 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
262 klass = GST_RTSP_SESSION_POOL_GET_CLASS (pool);
266 /* start by creating a new random session id, we assume that this is random
267 * enough to not cause a collision, which we will check later */
268 if (klass->create_session_id)
269 id = klass->create_session_id (pool);
276 g_mutex_lock (pool->lock);
277 /* check session limit */
278 if (pool->max_sessions > 0) {
279 if (g_hash_table_size (pool->sessions) >= pool->max_sessions)
280 goto too_many_sessions;
282 /* check if the sessionid existed */
283 result = g_hash_table_lookup (pool->sessions, id);
285 /* found, retry with a different session id */
291 /* not found, create session and insert it in the pool */
292 result = gst_rtsp_session_new (id);
293 /* take additional ref for the pool */
294 g_object_ref (result);
295 g_hash_table_insert (pool->sessions, result->sessionid, result);
297 g_mutex_unlock (pool->lock);
300 } while (result == NULL);
307 GST_WARNING ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
312 GST_WARNING ("can't create session id with GstRTSPSessionPool %p", pool);
317 GST_WARNING ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
318 g_mutex_unlock (pool->lock);
324 GST_WARNING ("session pool reached max sessions of %d", pool->max_sessions);
325 g_mutex_unlock (pool->lock);
332 * gst_rtsp_session_pool_remove:
333 * @pool: a #GstRTSPSessionPool
334 * @sess: a #GstRTSPSession
336 * Remove @sess from @pool, releasing the ref that the pool has on @sess.
338 * Returns: %TRUE if the session was found and removed.
341 gst_rtsp_session_pool_remove (GstRTSPSessionPool * pool, GstRTSPSession * sess)
345 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), FALSE);
346 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
348 g_mutex_lock (pool->lock);
349 found = g_hash_table_remove (pool->sessions, sess->sessionid);
350 g_mutex_unlock (pool->lock);
356 cleanup_func (gchar * sessionid, GstRTSPSession * sess, GTimeVal * now)
358 return gst_rtsp_session_is_expired (sess, now);
362 * gst_rtsp_session_pool_cleanup:
363 * @pool: a #GstRTSPSessionPool
365 * Inspect all the sessions in @pool and remove the sessions that are inactive
366 * for more than their timeout.
368 * Returns: the amount of sessions that got removed.
371 gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
376 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
378 g_get_current_time (&now);
380 g_mutex_lock (pool->lock);
382 g_hash_table_foreach_remove (pool->sessions, (GHRFunc) cleanup_func,
384 g_mutex_unlock (pool->lock);
391 GstRTSPSessionPool *pool;
392 GstRTSPSessionFilterFunc func;
398 filter_func (gchar * sessionid, GstRTSPSession * sess, FilterData * data)
400 switch (data->func (data->pool, sess, data->user_data)) {
401 case GST_RTSP_FILTER_REMOVE:
403 case GST_RTSP_FILTER_REF:
405 data->list = g_list_prepend (data->list, g_object_ref (sess));
408 case GST_RTSP_FILTER_KEEP:
414 * gst_rtsp_session_pool_filter:
415 * @pool: a #GstRTSPSessionPool
417 * @user_data: user data passed to @func
419 * Call @func for each session in @pool. The result value of @func determines
420 * what happens to the session. @func will be called with the session pool
421 * locked so no further actions on @pool can be performed from @func.
423 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
426 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
428 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
429 * will also be added with an additional ref to the result GList of this
432 * Returns: a GList with all sessions for which @func returned
433 * #GST_RTSP_FILTER_REF. After usage, each element in the GList should be unreffed
434 * before the list is freed.
437 gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
438 GstRTSPSessionFilterFunc func, gpointer user_data)
442 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
443 g_return_val_if_fail (func != NULL, NULL);
447 data.user_data = user_data;
450 g_mutex_lock (pool->lock);
451 g_hash_table_foreach_remove (pool->sessions, (GHRFunc) filter_func, &data);
452 g_mutex_unlock (pool->lock);
460 GstRTSPSessionPool *pool;
465 collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
470 g_source_get_current_time ((GSource *) psrc, &now);
472 timeout = gst_rtsp_session_next_timeout (sess, &now);
473 GST_INFO ("%p: next timeout: %d", sess, timeout);
474 if (psrc->timeout == -1 || timeout < psrc->timeout)
475 psrc->timeout = timeout;
479 gst_pool_source_prepare (GSource * source, gint * timeout)
484 psrc = (GstPoolSource *) source;
487 g_mutex_lock (psrc->pool->lock);
488 g_hash_table_foreach (psrc->pool->sessions, (GHFunc) collect_timeout, psrc);
489 g_mutex_unlock (psrc->pool->lock);
492 *timeout = psrc->timeout;
494 result = psrc->timeout == 0;
496 GST_INFO ("prepare %d, %d", psrc->timeout, result);
502 gst_pool_source_check (GSource * source)
506 return gst_pool_source_prepare (source, NULL);
510 gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
514 GstPoolSource *psrc = (GstPoolSource *) source;
515 GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;
517 GST_INFO ("dispatch");
520 res = func (psrc->pool, user_data);
528 gst_pool_source_finalize (GSource * source)
530 GstPoolSource *psrc = (GstPoolSource *) source;
532 GST_INFO ("finalize %p", psrc);
534 g_object_unref (psrc->pool);
538 static GSourceFuncs gst_pool_source_funcs = {
539 gst_pool_source_prepare,
540 gst_pool_source_check,
541 gst_pool_source_dispatch,
542 gst_pool_source_finalize
546 * gst_rtsp_session_pool_create_watch:
547 * @pool: a #GstRTSPSessionPool
549 * A GSource that will be dispatched when the session should be cleaned up.
552 gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
554 GstPoolSource *source;
556 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
558 source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
559 sizeof (GstPoolSource));
560 source->pool = g_object_ref (pool);
562 return (GSource *) source;