2008-10-09 12:29:12 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rtsp-session-pool.h"
|
|
|
|
|
|
|
|
#undef DEBUG
|
|
|
|
|
2009-02-04 18:52:50 +00:00
|
|
|
#define DEFAULT_MAX_SESSIONS 0
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_MAX_SESSIONS,
|
|
|
|
PROP_LAST
|
|
|
|
};
|
|
|
|
|
|
|
|
static void gst_rtsp_session_pool_get_property (GObject *object, guint propid,
|
|
|
|
GValue *value, GParamSpec *pspec);
|
|
|
|
static void gst_rtsp_session_pool_set_property (GObject *object, guint propid,
|
|
|
|
const GValue *value, GParamSpec *pspec);
|
2009-01-19 18:32:28 +00:00
|
|
|
static void gst_rtsp_session_pool_finalize (GObject * object);
|
|
|
|
|
2009-01-20 18:41:53 +00:00
|
|
|
static gchar * create_session_id (GstRTSPSessionPool *pool);
|
2009-01-19 18:32:28 +00:00
|
|
|
|
2008-10-09 12:29:12 +00:00
|
|
|
G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
2009-01-19 18:32:28 +00:00
|
|
|
|
2009-02-04 18:52:50 +00:00
|
|
|
gobject_class->get_property = gst_rtsp_session_pool_get_property;
|
|
|
|
gobject_class->set_property = gst_rtsp_session_pool_set_property;
|
2009-01-19 18:32:28 +00:00
|
|
|
gobject_class->finalize = gst_rtsp_session_pool_finalize;
|
2009-01-20 18:41:53 +00:00
|
|
|
|
2009-02-04 18:52:50 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_MAX_SESSIONS,
|
|
|
|
g_param_spec_uint ("max-sessions", "Max Sessions",
|
|
|
|
"the maximum amount of sessions (0 = unlimited)",
|
|
|
|
0, G_MAXUINT, DEFAULT_MAX_SESSIONS,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2009-01-20 18:41:53 +00:00
|
|
|
klass->create_session_id = create_session_id;
|
2008-10-09 12:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
|
|
|
|
{
|
|
|
|
pool->lock = g_mutex_new ();
|
|
|
|
pool->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
|
2009-02-04 16:00:42 +00:00
|
|
|
NULL, g_object_unref);
|
2009-02-04 18:52:50 +00:00
|
|
|
pool->max_sessions = DEFAULT_MAX_SESSIONS;
|
2009-01-19 18:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_session_pool_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstRTSPSessionPool * pool = GST_RTSP_SESSION_POOL (object);
|
|
|
|
|
|
|
|
g_mutex_free (pool->lock);
|
|
|
|
g_hash_table_unref (pool->sessions);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_rtsp_session_pool_parent_class)->finalize (object);
|
2008-10-09 12:29:12 +00:00
|
|
|
}
|
|
|
|
|
2009-02-04 18:52:50 +00:00
|
|
|
static void
|
|
|
|
gst_rtsp_session_pool_get_property (GObject *object, guint propid,
|
|
|
|
GValue *value, GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
|
|
|
|
|
|
|
|
switch (propid) {
|
|
|
|
case PROP_MAX_SESSIONS:
|
2009-02-10 15:21:17 +00:00
|
|
|
g_value_set_uint (value, gst_rtsp_session_pool_get_max_sessions (pool));
|
2009-02-04 18:52:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_session_pool_set_property (GObject *object, guint propid,
|
|
|
|
const GValue *value, GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
|
|
|
|
|
|
|
|
switch (propid) {
|
|
|
|
case PROP_MAX_SESSIONS:
|
2009-02-10 15:21:17 +00:00
|
|
|
gst_rtsp_session_pool_set_max_sessions (pool, g_value_get_uint (value));
|
2009-02-04 18:52:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-09 12:29:12 +00:00
|
|
|
/**
|
|
|
|
* gst_rtsp_session_pool_new:
|
|
|
|
*
|
|
|
|
* Create a new #GstRTSPSessionPool instance.
|
2009-01-20 18:41:53 +00:00
|
|
|
*
|
|
|
|
* Returns: A new #GstRTSPSessionPool. g_object_unref() after usage.
|
2008-10-09 12:29:12 +00:00
|
|
|
*/
|
|
|
|
GstRTSPSessionPool *
|
|
|
|
gst_rtsp_session_pool_new (void)
|
|
|
|
{
|
|
|
|
GstRTSPSessionPool *result;
|
|
|
|
|
|
|
|
result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-02-04 18:52:50 +00:00
|
|
|
/**
|
|
|
|
* gst_rtsp_session_pool_set_max_sessions:
|
|
|
|
* @pool: a #GstRTSPSessionPool
|
|
|
|
* @max: the maximum number of sessions
|
|
|
|
*
|
|
|
|
* Configure the maximum allowed number of sessions in @pool to @max.
|
|
|
|
* A value of 0 means an unlimited amount of sessions.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_rtsp_session_pool_set_max_sessions (GstRTSPSessionPool *pool, guint max)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
|
|
|
|
|
|
|
|
g_mutex_lock (pool->lock);
|
|
|
|
pool->max_sessions = max;
|
|
|
|
g_mutex_unlock (pool->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_session_pool_get_max_sessions:
|
|
|
|
* @pool: a #GstRTSPSessionPool
|
|
|
|
*
|
|
|
|
* Get the maximum allowed number of sessions in @pool. 0 means an unlimited
|
|
|
|
* amount of sessions.
|
|
|
|
*
|
|
|
|
* Returns: the maximum allowed number of sessions.
|
|
|
|
*/
|
|
|
|
guint
|
|
|
|
gst_rtsp_session_pool_get_max_sessions (GstRTSPSessionPool *pool)
|
|
|
|
{
|
|
|
|
guint result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
|
|
|
|
|
|
|
|
g_mutex_lock (pool->lock);
|
|
|
|
result = pool->max_sessions;
|
|
|
|
g_mutex_unlock (pool->lock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_session_pool_get_n_sessions:
|
|
|
|
* @pool: a #GstRTSPSessionPool
|
|
|
|
*
|
|
|
|
* Get the amount of active sessions in @pool.
|
|
|
|
*
|
|
|
|
* Returns: the amount of active sessions in @pool.
|
|
|
|
*/
|
|
|
|
guint
|
|
|
|
gst_rtsp_session_pool_get_n_sessions (GstRTSPSessionPool *pool)
|
|
|
|
{
|
|
|
|
guint result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
|
|
|
|
|
|
|
|
g_mutex_lock (pool->lock);
|
|
|
|
result = g_hash_table_size (pool->sessions);
|
|
|
|
g_mutex_unlock (pool->lock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-10-09 12:29:12 +00:00
|
|
|
/**
|
|
|
|
* gst_rtsp_session_pool_find:
|
|
|
|
* @pool: the pool to search
|
|
|
|
* @sessionid: the session id
|
|
|
|
*
|
2009-02-04 18:52:50 +00:00
|
|
|
* Find the session with @sessionid in @pool. The access time of the session
|
|
|
|
* will be updated with gst_rtsp_session_touch().
|
2008-10-09 12:29:12 +00:00
|
|
|
*
|
|
|
|
* Returns: the #GstRTSPSession with @sessionid or %NULL when the session did
|
|
|
|
* not exist. g_object_unref() after usage.
|
|
|
|
*/
|
|
|
|
GstRTSPSession *
|
|
|
|
gst_rtsp_session_pool_find (GstRTSPSessionPool *pool, const gchar *sessionid)
|
|
|
|
{
|
|
|
|
GstRTSPSession *result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
|
|
|
|
g_return_val_if_fail (sessionid != NULL, NULL);
|
|
|
|
|
|
|
|
g_mutex_lock (pool->lock);
|
|
|
|
result = g_hash_table_lookup (pool->sessions, sessionid);
|
2009-02-04 18:52:50 +00:00
|
|
|
if (result)
|
2008-10-09 12:29:12 +00:00
|
|
|
g_object_ref (result);
|
|
|
|
g_mutex_unlock (pool->lock);
|
|
|
|
|
2009-02-13 11:57:45 +00:00
|
|
|
if (result) {
|
2009-02-04 18:52:50 +00:00
|
|
|
gst_rtsp_session_touch (result);
|
2009-02-13 11:57:45 +00:00
|
|
|
}
|
2009-02-04 18:52:50 +00:00
|
|
|
|
2008-10-09 12:29:12 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *
|
2009-01-20 18:41:53 +00:00
|
|
|
create_session_id (GstRTSPSessionPool *pool)
|
2008-10-09 12:29:12 +00:00
|
|
|
{
|
|
|
|
gchar id[16];
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
id[i] = g_random_int_range ('a', 'z');
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_strndup (id, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_session_pool_create:
|
|
|
|
* @pool: a #GstRTSPSessionPool
|
|
|
|
*
|
|
|
|
* Create a new #GstRTSPSession object in @pool.
|
|
|
|
*
|
|
|
|
* Returns: a new #GstRTSPSession.
|
|
|
|
*/
|
|
|
|
GstRTSPSession *
|
|
|
|
gst_rtsp_session_pool_create (GstRTSPSessionPool *pool)
|
|
|
|
{
|
|
|
|
GstRTSPSession *result = NULL;
|
2009-01-20 18:41:53 +00:00
|
|
|
GstRTSPSessionPoolClass *klass;
|
|
|
|
gchar *id = NULL;
|
2009-02-04 18:52:50 +00:00
|
|
|
guint retry;
|
2008-10-09 12:29:12 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
|
|
|
|
|
2009-01-20 18:41:53 +00:00
|
|
|
klass = GST_RTSP_SESSION_POOL_GET_CLASS (pool);
|
|
|
|
|
2009-02-04 18:52:50 +00:00
|
|
|
retry = 0;
|
2008-10-09 12:29:12 +00:00
|
|
|
do {
|
|
|
|
/* start by creating a new random session id, we assume that this is random
|
|
|
|
* enough to not cause a collision, which we will check later */
|
2009-01-20 18:41:53 +00:00
|
|
|
if (klass->create_session_id)
|
|
|
|
id = klass->create_session_id (pool);
|
|
|
|
else
|
|
|
|
goto no_function;
|
|
|
|
|
|
|
|
if (id == NULL)
|
|
|
|
goto no_session;
|
2008-10-09 12:29:12 +00:00
|
|
|
|
|
|
|
g_mutex_lock (pool->lock);
|
2009-02-04 18:52:50 +00:00
|
|
|
/* check session limit */
|
|
|
|
if (pool->max_sessions > 0) {
|
|
|
|
if (g_hash_table_size (pool->sessions) >= pool->max_sessions)
|
|
|
|
goto too_many_sessions;
|
|
|
|
}
|
2008-10-09 12:29:12 +00:00
|
|
|
/* check if the sessionid existed */
|
|
|
|
result = g_hash_table_lookup (pool->sessions, id);
|
|
|
|
if (result) {
|
2009-02-04 18:52:50 +00:00
|
|
|
/* found, retry with a different session id */
|
2008-10-09 12:29:12 +00:00
|
|
|
result = NULL;
|
2009-02-04 18:52:50 +00:00
|
|
|
retry++;
|
|
|
|
if (retry > 100)
|
|
|
|
goto collision;
|
2008-10-09 12:29:12 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* not found, create session and insert it in the pool */
|
|
|
|
result = gst_rtsp_session_new (id);
|
|
|
|
/* take additional ref for the pool */
|
|
|
|
g_object_ref (result);
|
|
|
|
g_hash_table_insert (pool->sessions, result->sessionid, result);
|
|
|
|
}
|
|
|
|
g_mutex_unlock (pool->lock);
|
|
|
|
|
|
|
|
g_free (id);
|
|
|
|
} while (result == NULL);
|
|
|
|
|
|
|
|
return result;
|
2009-01-20 18:41:53 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
no_function:
|
|
|
|
{
|
|
|
|
g_warning ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
no_session:
|
|
|
|
{
|
|
|
|
g_warning ("can't create session id with GstRTSPSessionPool %p", pool);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-02-04 18:52:50 +00:00
|
|
|
collision:
|
|
|
|
{
|
|
|
|
g_warning ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
|
|
|
|
g_mutex_unlock (pool->lock);
|
|
|
|
g_free (id);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
too_many_sessions:
|
|
|
|
{
|
|
|
|
g_warning ("session pool reached max sessions of %d", pool->max_sessions);
|
|
|
|
g_mutex_unlock (pool->lock);
|
|
|
|
g_free (id);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-10-09 12:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_session_pool_remove:
|
|
|
|
* @pool: a #GstRTSPSessionPool
|
|
|
|
* @sess: a #GstRTSPSession
|
|
|
|
*
|
2009-02-04 18:52:50 +00:00
|
|
|
* Remove @sess from @pool, releasing the ref that the pool has on @sess.
|
2008-10-09 12:29:12 +00:00
|
|
|
*
|
2009-02-04 18:52:50 +00:00
|
|
|
* Returns: %TRUE if the session was found and removed.
|
2008-10-09 12:29:12 +00:00
|
|
|
*/
|
2009-02-04 18:52:50 +00:00
|
|
|
gboolean
|
2008-10-09 12:29:12 +00:00
|
|
|
gst_rtsp_session_pool_remove (GstRTSPSessionPool *pool, GstRTSPSession *sess)
|
|
|
|
{
|
|
|
|
gboolean found;
|
|
|
|
|
2009-02-04 18:52:50 +00:00
|
|
|
g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), FALSE);
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
|
2008-10-09 12:29:12 +00:00
|
|
|
|
|
|
|
g_mutex_lock (pool->lock);
|
2009-02-04 16:00:42 +00:00
|
|
|
found = g_hash_table_remove (pool->sessions, sess->sessionid);
|
2008-10-09 12:29:12 +00:00
|
|
|
g_mutex_unlock (pool->lock);
|
2009-02-04 18:52:50 +00:00
|
|
|
|
|
|
|
return found;
|
2008-10-09 12:29:12 +00:00
|
|
|
}
|
|
|
|
|
2009-02-04 19:10:39 +00:00
|
|
|
static gboolean
|
|
|
|
cleanup_func (gchar *sessionid, GstRTSPSession *sess, GstRTSPSessionPool *pool)
|
|
|
|
{
|
|
|
|
return gst_rtsp_session_is_expired (sess);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_session_pool_cleanup:
|
|
|
|
* @pool: a #GstRTSPSessionPool
|
|
|
|
*
|
|
|
|
* Inspect all the sessions in @pool and remove the sessions that are inactive
|
|
|
|
* for more than their timeout.
|
|
|
|
*
|
|
|
|
* Returns: the amount of sessions that got removed.
|
|
|
|
*/
|
|
|
|
guint
|
|
|
|
gst_rtsp_session_pool_cleanup (GstRTSPSessionPool *pool)
|
|
|
|
{
|
|
|
|
guint result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
|
|
|
|
|
|
|
|
g_mutex_lock (pool->lock);
|
|
|
|
result = g_hash_table_foreach_remove (pool->sessions, (GHRFunc) cleanup_func, pool);
|
|
|
|
g_mutex_unlock (pool->lock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|