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 G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);
27 gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
29 GObjectClass *gobject_class;
31 gobject_class = G_OBJECT_CLASS (klass);
35 gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
37 pool->lock = g_mutex_new ();
38 pool->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
43 * gst_rtsp_session_pool_new:
45 * Create a new #GstRTSPSessionPool instance.
48 gst_rtsp_session_pool_new (void)
50 GstRTSPSessionPool *result;
52 result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
58 * gst_rtsp_session_pool_find:
59 * @pool: the pool to search
60 * @sessionid: the session id
62 * Find the session with @sessionid in @pool.
64 * Returns: the #GstRTSPSession with @sessionid or %NULL when the session did
65 * not exist. g_object_unref() after usage.
68 gst_rtsp_session_pool_find (GstRTSPSessionPool *pool, const gchar *sessionid)
70 GstRTSPSession *result;
72 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
73 g_return_val_if_fail (sessionid != NULL, NULL);
75 g_mutex_lock (pool->lock);
76 result = g_hash_table_lookup (pool->sessions, sessionid);
78 g_object_ref (result);
79 g_mutex_unlock (pool->lock);
85 create_session_id (void)
90 for (i = 0; i < 16; i++) {
91 id[i] = g_random_int_range ('a', 'z');
94 return g_strndup (id, 16);
98 * gst_rtsp_session_pool_create:
99 * @pool: a #GstRTSPSessionPool
101 * Create a new #GstRTSPSession object in @pool.
103 * Returns: a new #GstRTSPSession.
106 gst_rtsp_session_pool_create (GstRTSPSessionPool *pool)
108 GstRTSPSession *result = NULL;
111 g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
114 /* start by creating a new random session id, we assume that this is random
115 * enough to not cause a collision, which we will check later */
116 id = create_session_id ();
118 g_mutex_lock (pool->lock);
119 /* check if the sessionid existed */
120 result = g_hash_table_lookup (pool->sessions, id);
122 /* found, retry with a different session id*/
126 /* not found, create session and insert it in the pool */
127 result = gst_rtsp_session_new (id);
128 /* take additional ref for the pool */
129 g_object_ref (result);
130 g_hash_table_insert (pool->sessions, result->sessionid, result);
132 g_mutex_unlock (pool->lock);
135 } while (result == NULL);
141 * gst_rtsp_session_pool_remove:
142 * @pool: a #GstRTSPSessionPool
143 * @sess: a #GstRTSPSession
145 * Remove @sess from @pool and Clean it up.
147 * Returns: a new #GstRTSPSession.
150 gst_rtsp_session_pool_remove (GstRTSPSessionPool *pool, GstRTSPSession *sess)
154 g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
155 g_return_if_fail (GST_IS_RTSP_SESSION (sess));
157 g_mutex_lock (pool->lock);
158 found = g_hash_table_remove (pool->sessions, sess);
159 g_mutex_unlock (pool->lock);
162 g_object_unref (sess);