Initial import
[platform/upstream/gst-rtsp-server.git] / src / rtsp-session-pool.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "rtsp-session-pool.h"
21
22 #undef DEBUG
23
24 G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);
25
26 static void
27 gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
28 {
29   GObjectClass *gobject_class;
30
31   gobject_class = G_OBJECT_CLASS (klass);
32 }
33
34 static void
35 gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
36 {
37   pool->lock = g_mutex_new ();
38   pool->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
39                   g_free, NULL);
40 }
41
42 /**
43  * gst_rtsp_session_pool_new:
44  *
45  * Create a new #GstRTSPSessionPool instance.
46  */
47 GstRTSPSessionPool *
48 gst_rtsp_session_pool_new (void)
49 {
50   GstRTSPSessionPool *result;
51
52   result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
53
54   return result;
55 }
56
57 /**
58  * gst_rtsp_session_pool_find:
59  * @pool: the pool to search
60  * @sessionid: the session id
61  *
62  * Find the session with @sessionid in @pool.
63  *
64  * Returns: the #GstRTSPSession with @sessionid or %NULL when the session did
65  * not exist. g_object_unref() after usage.
66  */
67 GstRTSPSession *
68 gst_rtsp_session_pool_find (GstRTSPSessionPool *pool, const gchar *sessionid)
69 {
70   GstRTSPSession *result;
71
72   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
73   g_return_val_if_fail (sessionid != NULL, NULL);
74
75   g_mutex_lock (pool->lock);
76   result = g_hash_table_lookup (pool->sessions, sessionid);
77   if (result)
78     g_object_ref (result);
79   g_mutex_unlock (pool->lock);
80
81   return result;
82 }
83
84 static gchar *
85 create_session_id (void)
86 {
87   gchar id[16];
88   gint i;
89
90   for (i = 0; i < 16; i++) {
91     id[i] = g_random_int_range ('a', 'z');
92   }
93
94   return g_strndup (id, 16);
95 }
96
97 /**
98  * gst_rtsp_session_pool_create:
99  * @pool: a #GstRTSPSessionPool
100  *
101  * Create a new #GstRTSPSession object in @pool.
102  *
103  * Returns: a new #GstRTSPSession.
104  */
105 GstRTSPSession *
106 gst_rtsp_session_pool_create (GstRTSPSessionPool *pool)
107 {
108   GstRTSPSession *result = NULL;
109   gchar *id;
110
111   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
112
113   do {
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 ();
117
118     g_mutex_lock (pool->lock);
119     /* check if the sessionid existed */
120     result = g_hash_table_lookup (pool->sessions, id);
121     if (result) {
122       /* found, retry with a different session id*/
123       result = NULL;
124     }
125     else {
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);
131     }
132     g_mutex_unlock (pool->lock);
133
134     g_free (id);
135   } while (result == NULL);
136
137   return result;
138 }
139
140 /**
141  * gst_rtsp_session_pool_remove:
142  * @pool: a #GstRTSPSessionPool
143  * @sess: a #GstRTSPSession
144  *
145  * Remove @sess from @pool and Clean it up.
146  *
147  * Returns: a new #GstRTSPSession.
148  */
149 void
150 gst_rtsp_session_pool_remove (GstRTSPSessionPool *pool, GstRTSPSession *sess)
151 {
152   gboolean found;
153
154   g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
155   g_return_if_fail (GST_IS_RTSP_SESSION (sess));
156
157   g_mutex_lock (pool->lock);
158   found = g_hash_table_remove (pool->sessions, sess);
159   g_mutex_unlock (pool->lock);
160
161   if (found) {
162     g_object_unref (sess);
163   }
164 }
165