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 static void gst_rtsp_session_pool_finalize (GObject * object);
27 G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);
30 gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
32 GObjectClass *gobject_class;
34 gobject_class = G_OBJECT_CLASS (klass);
36 gobject_class->finalize = gst_rtsp_session_pool_finalize;
40 gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
42 pool->lock = g_mutex_new ();
43 pool->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
44 g_free, g_object_unref);
48 gst_rtsp_session_pool_finalize (GObject * object)
50 GstRTSPSessionPool * pool = GST_RTSP_SESSION_POOL (object);
52 g_mutex_free (pool->lock);
53 g_hash_table_unref (pool->sessions);
55 G_OBJECT_CLASS (gst_rtsp_session_pool_parent_class)->finalize (object);
59 * gst_rtsp_session_pool_new:
61 * Create a new #GstRTSPSessionPool instance.
64 gst_rtsp_session_pool_new (void)
66 GstRTSPSessionPool *result;
68 result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
74 * gst_rtsp_session_pool_find:
75 * @pool: the pool to search
76 * @sessionid: the session id
78 * Find the session with @sessionid in @pool.
80 * Returns: the #GstRTSPSession with @sessionid or %NULL when the session did
81 * not exist. g_object_unref() after usage.
84 gst_rtsp_session_pool_find (GstRTSPSessionPool *pool, const gchar *sessionid)
86 GstRTSPSession *result;
88 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
89 g_return_val_if_fail (sessionid != NULL, NULL);
91 g_mutex_lock (pool->lock);
92 result = g_hash_table_lookup (pool->sessions, sessionid);
94 g_object_ref (result);
95 g_mutex_unlock (pool->lock);
101 create_session_id (void)
106 for (i = 0; i < 16; i++) {
107 id[i] = g_random_int_range ('a', 'z');
110 return g_strndup (id, 16);
114 * gst_rtsp_session_pool_create:
115 * @pool: a #GstRTSPSessionPool
117 * Create a new #GstRTSPSession object in @pool.
119 * Returns: a new #GstRTSPSession.
122 gst_rtsp_session_pool_create (GstRTSPSessionPool *pool)
124 GstRTSPSession *result = NULL;
127 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
130 /* start by creating a new random session id, we assume that this is random
131 * enough to not cause a collision, which we will check later */
132 id = create_session_id ();
134 g_mutex_lock (pool->lock);
135 /* check if the sessionid existed */
136 result = g_hash_table_lookup (pool->sessions, id);
138 /* found, retry with a different session id*/
142 /* not found, create session and insert it in the pool */
143 result = gst_rtsp_session_new (id);
144 /* take additional ref for the pool */
145 g_object_ref (result);
146 g_hash_table_insert (pool->sessions, result->sessionid, result);
148 g_mutex_unlock (pool->lock);
151 } while (result == NULL);
157 * gst_rtsp_session_pool_remove:
158 * @pool: a #GstRTSPSessionPool
159 * @sess: a #GstRTSPSession
161 * Remove @sess from @pool and Clean it up.
163 * Returns: a new #GstRTSPSession.
166 gst_rtsp_session_pool_remove (GstRTSPSessionPool *pool, GstRTSPSession *sess)
170 g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
171 g_return_if_fail (GST_IS_RTSP_SESSION (sess));
173 g_mutex_lock (pool->lock);
174 found = g_hash_table_remove (pool->sessions, sess);
175 g_mutex_unlock (pool->lock);