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
|
2012-11-04 00:14:25 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2008-10-09 12:29:12 +00:00
|
|
|
*/
|
2013-07-11 14:57:14 +00:00
|
|
|
/**
|
|
|
|
* SECTION:rtsp-session-pool
|
|
|
|
* @short_description: An object for managing sessions
|
|
|
|
* @see_also: #GstRTSPSession
|
|
|
|
*
|
|
|
|
* The #GstRTSPSessionPool object manages a list of #GstRTSPSession objects.
|
|
|
|
*
|
|
|
|
* The maximum number of sessions can be configured with
|
|
|
|
* gst_rtsp_session_pool_set_max_sessions(). The current number of sessions can
|
|
|
|
* be retrieved with gst_rtsp_session_pool_get_n_sessions().
|
|
|
|
*
|
|
|
|
* Use gst_rtsp_session_pool_create() to create a new #GstRTSPSession object.
|
|
|
|
* The session object can be found again with its id and
|
|
|
|
* gst_rtsp_session_pool_find().
|
|
|
|
*
|
|
|
|
* All sessions can be iterated with gst_rtsp_session_pool_filter().
|
|
|
|
*
|
|
|
|
* Run gst_rtsp_session_pool_cleanup() periodically to remove timed out sessions
|
|
|
|
* or use gst_rtsp_session_pool_create_watch() to be notified when session
|
|
|
|
* cleanup should be performed.
|
|
|
|
*
|
|
|
|
* Last reviewed on 2013-07-11 (1.0.0)
|
|
|
|
*/
|
2008-10-09 12:29:12 +00:00
|
|
|
|
|
|
|
#include "rtsp-session-pool.h"
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
#define GST_RTSP_SESSION_POOL_GET_PRIVATE(obj) \
|
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SESSION_POOL, GstRTSPSessionPoolPrivate))
|
|
|
|
|
|
|
|
struct _GstRTSPSessionPoolPrivate
|
|
|
|
{
|
2013-02-18 19:59:58 +00:00
|
|
|
GMutex lock; /* protects everything in this struct */
|
2012-11-29 10:11:05 +00:00
|
|
|
guint max_sessions;
|
|
|
|
GHashTable *sessions;
|
|
|
|
};
|
|
|
|
|
2009-02-04 18:52:50 +00:00
|
|
|
#define DEFAULT_MAX_SESSIONS 0
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_MAX_SESSIONS,
|
|
|
|
PROP_LAST
|
|
|
|
};
|
|
|
|
|
2011-03-03 19:38:03 +00:00
|
|
|
static const gchar session_id_charset[] =
|
|
|
|
{ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
|
|
|
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
|
|
|
|
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
|
|
|
|
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
'8', '9', '$', '-', '_', '.', '+'
|
|
|
|
};
|
|
|
|
|
2011-01-12 17:14:48 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (rtsp_session_debug);
|
2009-11-21 18:20:23 +00:00
|
|
|
#define GST_CAT_DEFAULT rtsp_session_debug
|
|
|
|
|
2010-12-11 09:48:42 +00:00
|
|
|
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);
|
|
|
|
|
2010-12-11 09:48:42 +00:00
|
|
|
static gchar *create_session_id (GstRTSPSessionPool * pool);
|
2013-07-11 14:28:09 +00:00
|
|
|
static GstRTSPSession *create_session (GstRTSPSessionPool * pool,
|
|
|
|
const gchar * id);
|
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;
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
g_type_class_add_private (klass, sizeof (GstRTSPSessionPoolPrivate));
|
|
|
|
|
2008-10-09 12:29:12 +00:00
|
|
|
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;
|
2013-07-11 14:28:09 +00:00
|
|
|
klass->create_session = create_session;
|
2009-02-13 18:58:17 +00:00
|
|
|
|
2011-01-12 17:14:48 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsessionpool", 0,
|
|
|
|
"GstRTSPSessionPool");
|
2008-10-09 12:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
|
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPSessionPoolPrivate *priv = GST_RTSP_SESSION_POOL_GET_PRIVATE (pool);
|
|
|
|
|
|
|
|
pool->priv = priv;
|
|
|
|
|
|
|
|
g_mutex_init (&priv->lock);
|
|
|
|
priv->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
|
2010-12-11 09:48:42 +00:00
|
|
|
NULL, g_object_unref);
|
2012-11-29 10:11:05 +00:00
|
|
|
priv->max_sessions = DEFAULT_MAX_SESSIONS;
|
2009-01-19 18:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_session_pool_finalize (GObject * object)
|
|
|
|
{
|
2010-12-11 09:48:42 +00:00
|
|
|
GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPSessionPoolPrivate *priv = pool->priv;
|
2009-01-19 18:32:28 +00:00
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_clear (&priv->lock);
|
|
|
|
g_hash_table_unref (priv->sessions);
|
2010-12-11 09:48:42 +00:00
|
|
|
|
2009-01-19 18:32:28 +00:00
|
|
|
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
|
2010-12-11 09:48:42 +00:00
|
|
|
gst_rtsp_session_pool_get_property (GObject * object, guint propid,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
2009-02-04 18:52:50 +00:00
|
|
|
{
|
|
|
|
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
|
2010-12-11 09:48:42 +00:00
|
|
|
gst_rtsp_session_pool_set_property (GObject * object, guint propid,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
2009-02-04 18:52:50 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
*
|
2014-03-23 20:24:48 +00:00
|
|
|
* Returns: (transfer full): 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
|
2010-12-11 09:48:42 +00:00
|
|
|
gst_rtsp_session_pool_set_max_sessions (GstRTSPSessionPool * pool, guint max)
|
2009-02-04 18:52:50 +00:00
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPSessionPoolPrivate *priv;
|
|
|
|
|
2009-02-04 18:52:50 +00:00
|
|
|
g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
priv = pool->priv;
|
|
|
|
|
|
|
|
g_mutex_lock (&priv->lock);
|
|
|
|
priv->max_sessions = max;
|
|
|
|
g_mutex_unlock (&priv->lock);
|
2009-02-04 18:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2010-12-11 09:48:42 +00:00
|
|
|
gst_rtsp_session_pool_get_max_sessions (GstRTSPSessionPool * pool)
|
2009-02-04 18:52:50 +00:00
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPSessionPoolPrivate *priv;
|
2009-02-04 18:52:50 +00:00
|
|
|
guint result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
priv = pool->priv;
|
|
|
|
|
|
|
|
g_mutex_lock (&priv->lock);
|
|
|
|
result = priv->max_sessions;
|
|
|
|
g_mutex_unlock (&priv->lock);
|
2009-02-04 18:52:50 +00:00
|
|
|
|
|
|
|
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
|
2010-12-11 09:48:42 +00:00
|
|
|
gst_rtsp_session_pool_get_n_sessions (GstRTSPSessionPool * pool)
|
2009-02-04 18:52:50 +00:00
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPSessionPoolPrivate *priv;
|
2009-02-04 18:52:50 +00:00
|
|
|
guint result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
priv = pool->priv;
|
|
|
|
|
|
|
|
g_mutex_lock (&priv->lock);
|
|
|
|
result = g_hash_table_size (priv->sessions);
|
|
|
|
g_mutex_unlock (&priv->lock);
|
2009-02-04 18:52:50 +00:00
|
|
|
|
|
|
|
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
|
|
|
*
|
2012-10-01 17:46:15 +00:00
|
|
|
* Returns: (transfer full): the #GstRTSPSession with @sessionid or %NULL when the session did
|
2008-10-09 12:29:12 +00:00
|
|
|
* not exist. g_object_unref() after usage.
|
|
|
|
*/
|
|
|
|
GstRTSPSession *
|
2010-12-11 09:48:42 +00:00
|
|
|
gst_rtsp_session_pool_find (GstRTSPSessionPool * pool, const gchar * sessionid)
|
2008-10-09 12:29:12 +00:00
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPSessionPoolPrivate *priv;
|
2008-10-09 12:29:12 +00:00
|
|
|
GstRTSPSession *result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
|
|
|
|
g_return_val_if_fail (sessionid != NULL, NULL);
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
priv = pool->priv;
|
|
|
|
|
|
|
|
g_mutex_lock (&priv->lock);
|
|
|
|
result = g_hash_table_lookup (priv->sessions, sessionid);
|
2009-02-13 11:57:45 +00:00
|
|
|
if (result) {
|
2009-03-11 18:38:06 +00:00
|
|
|
g_object_ref (result);
|
2009-02-04 18:52:50 +00:00
|
|
|
gst_rtsp_session_touch (result);
|
2009-02-13 11:57:45 +00:00
|
|
|
}
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_unlock (&priv->lock);
|
2009-02-04 18:52:50 +00:00
|
|
|
|
2008-10-09 12:29:12 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *
|
2010-12-11 09:48:42 +00:00
|
|
|
create_session_id (GstRTSPSessionPool * pool)
|
2008-10-09 12:29:12 +00:00
|
|
|
{
|
2013-10-30 21:16:54 +00:00
|
|
|
gchar id[17];
|
2008-10-09 12:29:12 +00:00
|
|
|
gint i;
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
2011-03-03 19:38:03 +00:00
|
|
|
id[i] =
|
|
|
|
session_id_charset[g_random_int_range (0,
|
|
|
|
G_N_ELEMENTS (session_id_charset))];
|
2008-10-09 12:29:12 +00:00
|
|
|
}
|
2013-10-30 21:16:54 +00:00
|
|
|
id[16] = 0;
|
2008-10-09 12:29:12 +00:00
|
|
|
|
2013-10-30 21:16:54 +00:00
|
|
|
return g_uri_escape_string (id, NULL, FALSE);
|
2008-10-09 12:29:12 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 14:28:09 +00:00
|
|
|
static GstRTSPSession *
|
|
|
|
create_session (GstRTSPSessionPool * pool, const gchar * id)
|
|
|
|
{
|
|
|
|
return gst_rtsp_session_new (id);
|
|
|
|
}
|
|
|
|
|
2008-10-09 12:29:12 +00:00
|
|
|
/**
|
|
|
|
* gst_rtsp_session_pool_create:
|
|
|
|
* @pool: a #GstRTSPSessionPool
|
|
|
|
*
|
|
|
|
* Create a new #GstRTSPSession object in @pool.
|
|
|
|
*
|
2014-04-11 21:52:49 +00:00
|
|
|
* Returns: (transfer full): a new #GstRTSPSession.
|
2008-10-09 12:29:12 +00:00
|
|
|
*/
|
|
|
|
GstRTSPSession *
|
2010-12-11 09:48:42 +00:00
|
|
|
gst_rtsp_session_pool_create (GstRTSPSessionPool * pool)
|
2008-10-09 12:29:12 +00:00
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPSessionPoolPrivate *priv;
|
2008-10-09 12:29:12 +00:00
|
|
|
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);
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
priv = pool->priv;
|
|
|
|
|
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
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_lock (&priv->lock);
|
2009-02-04 18:52:50 +00:00
|
|
|
/* check session limit */
|
2012-11-29 10:11:05 +00:00
|
|
|
if (priv->max_sessions > 0) {
|
|
|
|
if (g_hash_table_size (priv->sessions) >= priv->max_sessions)
|
2010-12-11 09:48:42 +00:00
|
|
|
goto too_many_sessions;
|
2009-02-04 18:52:50 +00:00
|
|
|
}
|
2008-10-09 12:29:12 +00:00
|
|
|
/* check if the sessionid existed */
|
2012-11-29 10:11:05 +00:00
|
|
|
result = g_hash_table_lookup (priv->sessions, id);
|
2008-10-09 12:29:12 +00:00
|
|
|
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)
|
2010-12-11 09:48:42 +00:00
|
|
|
goto collision;
|
|
|
|
} else {
|
2008-10-09 12:29:12 +00:00
|
|
|
/* not found, create session and insert it in the pool */
|
2013-07-11 14:28:09 +00:00
|
|
|
if (klass->create_session)
|
|
|
|
result = create_session (pool, id);
|
|
|
|
if (result == NULL)
|
|
|
|
goto too_many_sessions;
|
2008-10-09 12:29:12 +00:00
|
|
|
/* take additional ref for the pool */
|
|
|
|
g_object_ref (result);
|
2012-11-29 10:11:05 +00:00
|
|
|
g_hash_table_insert (priv->sessions,
|
|
|
|
(gchar *) gst_rtsp_session_get_sessionid (result), result);
|
2008-10-09 12:29:12 +00:00
|
|
|
}
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_unlock (&priv->lock);
|
2008-10-09 12:29:12 +00:00
|
|
|
|
|
|
|
g_free (id);
|
|
|
|
} while (result == NULL);
|
|
|
|
|
|
|
|
return result;
|
2009-01-20 18:41:53 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
no_function:
|
|
|
|
{
|
2009-11-21 18:20:23 +00:00
|
|
|
GST_WARNING ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
|
2009-01-20 18:41:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
no_session:
|
|
|
|
{
|
2009-11-21 18:20:23 +00:00
|
|
|
GST_WARNING ("can't create session id with GstRTSPSessionPool %p", pool);
|
2009-01-20 18:41:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-02-04 18:52:50 +00:00
|
|
|
collision:
|
|
|
|
{
|
2009-11-21 18:20:23 +00:00
|
|
|
GST_WARNING ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_unlock (&priv->lock);
|
2009-02-04 18:52:50 +00:00
|
|
|
g_free (id);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
too_many_sessions:
|
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GST_WARNING ("session pool reached max sessions of %d", priv->max_sessions);
|
|
|
|
g_mutex_unlock (&priv->lock);
|
2009-02-04 18:52:50 +00:00
|
|
|
g_free (id);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-10-09 12:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_session_pool_remove:
|
|
|
|
* @pool: a #GstRTSPSessionPool
|
2014-03-23 20:24:48 +00:00
|
|
|
* @sess: (transfer none): a #GstRTSPSession
|
2008-10-09 12:29:12 +00:00
|
|
|
*
|
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
|
2010-12-11 09:48:42 +00:00
|
|
|
gst_rtsp_session_pool_remove (GstRTSPSessionPool * pool, GstRTSPSession * sess)
|
2008-10-09 12:29:12 +00:00
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPSessionPoolPrivate *priv;
|
2008-10-09 12:29:12 +00:00
|
|
|
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
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
priv = pool->priv;
|
|
|
|
|
|
|
|
g_mutex_lock (&priv->lock);
|
|
|
|
found =
|
|
|
|
g_hash_table_remove (priv->sessions,
|
|
|
|
gst_rtsp_session_get_sessionid (sess));
|
|
|
|
g_mutex_unlock (&priv->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
|
2010-12-11 09:48:42 +00:00
|
|
|
cleanup_func (gchar * sessionid, GstRTSPSession * sess, GTimeVal * now)
|
2009-02-04 19:10:39 +00:00
|
|
|
{
|
2009-02-13 18:58:17 +00:00
|
|
|
return gst_rtsp_session_is_expired (sess, now);
|
2009-02-04 19:10:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2010-12-11 09:48:42 +00:00
|
|
|
gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
|
2009-02-04 19:10:39 +00:00
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPSessionPoolPrivate *priv;
|
2009-02-04 19:10:39 +00:00
|
|
|
guint result;
|
2009-02-13 18:58:17 +00:00
|
|
|
GTimeVal now;
|
2009-02-04 19:10:39 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
priv = pool->priv;
|
|
|
|
|
2009-02-13 18:58:17 +00:00
|
|
|
g_get_current_time (&now);
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_lock (&priv->lock);
|
2010-12-11 09:48:42 +00:00
|
|
|
result =
|
2012-11-29 10:11:05 +00:00
|
|
|
g_hash_table_foreach_remove (priv->sessions, (GHRFunc) cleanup_func,
|
2010-12-11 09:48:42 +00:00
|
|
|
&now);
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_unlock (&priv->lock);
|
2009-02-04 19:10:39 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2009-02-13 18:58:17 +00:00
|
|
|
|
2009-06-30 19:27:53 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstRTSPSessionPool *pool;
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPSessionPoolFilterFunc func;
|
2009-06-30 19:27:53 +00:00
|
|
|
gpointer user_data;
|
|
|
|
GList *list;
|
|
|
|
} FilterData;
|
|
|
|
|
|
|
|
static gboolean
|
2010-12-11 09:48:42 +00:00
|
|
|
filter_func (gchar * sessionid, GstRTSPSession * sess, FilterData * data)
|
2009-06-30 19:27:53 +00:00
|
|
|
{
|
2013-11-15 15:35:05 +00:00
|
|
|
GstRTSPFilterResult res;
|
|
|
|
|
|
|
|
if (data->func)
|
|
|
|
res = data->func (data->pool, sess, data->user_data);
|
|
|
|
else
|
|
|
|
res = GST_RTSP_FILTER_REF;
|
|
|
|
|
|
|
|
switch (res) {
|
2009-06-30 19:27:53 +00:00
|
|
|
case GST_RTSP_FILTER_REMOVE:
|
|
|
|
return TRUE;
|
|
|
|
case GST_RTSP_FILTER_REF:
|
|
|
|
/* keep ref */
|
|
|
|
data->list = g_list_prepend (data->list, g_object_ref (sess));
|
|
|
|
/* fallthrough */
|
|
|
|
default:
|
|
|
|
case GST_RTSP_FILTER_KEEP:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_session_pool_filter:
|
|
|
|
* @pool: a #GstRTSPSessionPool
|
2013-11-18 10:47:04 +00:00
|
|
|
* @func: (scope call) (allow-none): a callback
|
2014-03-23 20:24:48 +00:00
|
|
|
* @user_data: (closure): user data passed to @func
|
2009-06-30 19:27:53 +00:00
|
|
|
*
|
|
|
|
* Call @func for each session in @pool. The result value of @func determines
|
|
|
|
* what happens to the session. @func will be called with the session pool
|
|
|
|
* locked so no further actions on @pool can be performed from @func.
|
|
|
|
*
|
|
|
|
* If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
|
|
|
|
* @pool.
|
|
|
|
*
|
|
|
|
* If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
|
|
|
|
*
|
|
|
|
* If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
|
|
|
|
* will also be added with an additional ref to the result GList of this
|
|
|
|
* function..
|
|
|
|
*
|
2013-11-15 15:35:05 +00:00
|
|
|
* When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for all sessions.
|
|
|
|
*
|
2012-10-01 17:46:15 +00:00
|
|
|
* Returns: (element-type GstRTSPSession) (transfer full): a GList with all
|
|
|
|
* sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
|
|
|
|
* element in the GList should be unreffed before the list is freed.
|
2009-06-30 19:27:53 +00:00
|
|
|
*/
|
|
|
|
GList *
|
2010-12-11 09:48:42 +00:00
|
|
|
gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPSessionPoolFilterFunc func, gpointer user_data)
|
2009-06-30 19:27:53 +00:00
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPSessionPoolPrivate *priv;
|
2009-06-30 19:27:53 +00:00
|
|
|
FilterData data;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
priv = pool->priv;
|
|
|
|
|
2009-06-30 19:27:53 +00:00
|
|
|
data.pool = pool;
|
|
|
|
data.func = func;
|
|
|
|
data.user_data = user_data;
|
|
|
|
data.list = NULL;
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_lock (&priv->lock);
|
|
|
|
g_hash_table_foreach_remove (priv->sessions, (GHRFunc) filter_func, &data);
|
|
|
|
g_mutex_unlock (&priv->lock);
|
2009-06-30 19:27:53 +00:00
|
|
|
|
|
|
|
return data.list;
|
|
|
|
}
|
|
|
|
|
2009-02-13 18:58:17 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GSource source;
|
|
|
|
GstRTSPSessionPool *pool;
|
|
|
|
gint timeout;
|
|
|
|
} GstPoolSource;
|
|
|
|
|
|
|
|
static void
|
2010-12-11 09:48:42 +00:00
|
|
|
collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
|
2009-02-13 18:58:17 +00:00
|
|
|
{
|
|
|
|
gint timeout;
|
|
|
|
GTimeVal now;
|
|
|
|
|
2013-05-30 08:46:33 +00:00
|
|
|
g_get_current_time (&now);
|
2009-02-13 18:58:17 +00:00
|
|
|
|
|
|
|
timeout = gst_rtsp_session_next_timeout (sess, &now);
|
2009-11-21 18:20:23 +00:00
|
|
|
GST_INFO ("%p: next timeout: %d", sess, timeout);
|
2009-02-13 18:58:17 +00:00
|
|
|
if (psrc->timeout == -1 || timeout < psrc->timeout)
|
|
|
|
psrc->timeout = timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_pool_source_prepare (GSource * source, gint * timeout)
|
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPSessionPoolPrivate *priv;
|
2009-02-13 18:58:17 +00:00
|
|
|
GstPoolSource *psrc;
|
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
psrc = (GstPoolSource *) source;
|
|
|
|
psrc->timeout = -1;
|
2012-11-29 10:11:05 +00:00
|
|
|
priv = psrc->pool->priv;
|
2009-02-13 18:58:17 +00:00
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_lock (&priv->lock);
|
|
|
|
g_hash_table_foreach (priv->sessions, (GHFunc) collect_timeout, psrc);
|
|
|
|
g_mutex_unlock (&priv->lock);
|
2009-02-13 18:58:17 +00:00
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
*timeout = psrc->timeout;
|
|
|
|
|
|
|
|
result = psrc->timeout == 0;
|
|
|
|
|
2009-11-21 18:20:23 +00:00
|
|
|
GST_INFO ("prepare %d, %d", psrc->timeout, result);
|
2009-02-13 18:58:17 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_pool_source_check (GSource * source)
|
|
|
|
{
|
2009-11-21 18:20:23 +00:00
|
|
|
GST_INFO ("check");
|
2009-02-13 18:58:17 +00:00
|
|
|
|
|
|
|
return gst_pool_source_prepare (source, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
gboolean res;
|
|
|
|
GstPoolSource *psrc = (GstPoolSource *) source;
|
|
|
|
GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;
|
|
|
|
|
2009-11-21 18:20:23 +00:00
|
|
|
GST_INFO ("dispatch");
|
2009-02-13 18:58:17 +00:00
|
|
|
|
|
|
|
if (func)
|
|
|
|
res = func (psrc->pool, user_data);
|
|
|
|
else
|
|
|
|
res = FALSE;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pool_source_finalize (GSource * source)
|
|
|
|
{
|
|
|
|
GstPoolSource *psrc = (GstPoolSource *) source;
|
|
|
|
|
2009-11-21 18:20:23 +00:00
|
|
|
GST_INFO ("finalize %p", psrc);
|
2009-02-13 18:58:17 +00:00
|
|
|
|
|
|
|
g_object_unref (psrc->pool);
|
|
|
|
psrc->pool = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GSourceFuncs gst_pool_source_funcs = {
|
|
|
|
gst_pool_source_prepare,
|
|
|
|
gst_pool_source_check,
|
|
|
|
gst_pool_source_dispatch,
|
|
|
|
gst_pool_source_finalize
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_session_pool_create_watch:
|
|
|
|
* @pool: a #GstRTSPSessionPool
|
|
|
|
*
|
2014-03-23 20:24:48 +00:00
|
|
|
* Create a #GSource that will be dispatched when the session should be cleaned
|
|
|
|
* up.
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): a #GSource
|
2009-02-13 18:58:17 +00:00
|
|
|
*/
|
|
|
|
GSource *
|
2010-12-11 09:48:42 +00:00
|
|
|
gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
|
2009-02-13 18:58:17 +00:00
|
|
|
{
|
|
|
|
GstPoolSource *source;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
|
|
|
|
|
|
|
|
source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
|
|
|
|
sizeof (GstPoolSource));
|
|
|
|
source->pool = g_object_ref (pool);
|
|
|
|
|
|
|
|
return (GSource *) source;
|
|
|
|
}
|