diff --git a/gst/rtsp-server/rtsp-session-pool.c b/gst/rtsp-server/rtsp-session-pool.c index 8ce18efbf9..43a5887baf 100644 --- a/gst/rtsp-server/rtsp-session-pool.c +++ b/gst/rtsp-server/rtsp-session-pool.c @@ -352,3 +352,31 @@ gst_rtsp_session_pool_remove (GstRTSPSessionPool *pool, GstRTSPSession *sess) return found; } +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; +} diff --git a/gst/rtsp-server/rtsp-session-pool.h b/gst/rtsp-server/rtsp-session-pool.h index 30df9bd264..dba8b764f6 100644 --- a/gst/rtsp-server/rtsp-session-pool.h +++ b/gst/rtsp-server/rtsp-session-pool.h @@ -85,6 +85,9 @@ GstRTSPSession * gst_rtsp_session_pool_find (GstRTSPSessionPoo gboolean gst_rtsp_session_pool_remove (GstRTSPSessionPool *pool, GstRTSPSession *sess); +/* perform session maintenance */ +guint gst_rtsp_session_pool_cleanup (GstRTSPSessionPool *pool); + G_END_DECLS #endif /* __GST_RTSP_SESSION_POOL_H__ */ diff --git a/gst/rtsp-server/rtsp-session.c b/gst/rtsp-server/rtsp-session.c index 3c538b4b62..8e8ae0eef0 100644 --- a/gst/rtsp-server/rtsp-session.c +++ b/gst/rtsp-server/rtsp-session.c @@ -397,6 +397,35 @@ gst_rtsp_session_touch (GstRTSPSession *session) g_get_current_time (&session->last_access); } +/** + * gst_rtsp_session_is_expired: + * @session: a #GstRTSPSession + * + * Check if @session timeout out. + * + * Returns: %TRUE if @session timed out + */ +gboolean +gst_rtsp_session_is_expired (GstRTSPSession *session) +{ + gboolean res; + GstClockTime last_access, now_ns; + GTimeVal now; + + g_return_val_if_fail (GST_IS_RTSP_SESSION (session), FALSE); + + last_access = GST_TIMEVAL_TO_TIME (session->last_access); + /* add timeout */ + last_access += session->timeout * GST_SECOND; + + g_get_current_time (&now); + now_ns = GST_TIMEVAL_TO_TIME (now); + + res = now_ns > last_access; + + return res; +} + /** * gst_rtsp_session_stream_init_udp: * @stream: a #GstRTSPSessionStream diff --git a/gst/rtsp-server/rtsp-session.h b/gst/rtsp-server/rtsp-session.h index d87922bd66..bcda0887d5 100644 --- a/gst/rtsp-server/rtsp-session.h +++ b/gst/rtsp-server/rtsp-session.h @@ -117,8 +117,9 @@ const gchar * gst_rtsp_session_get_sessionid (GstRTSPSession *se void gst_rtsp_session_set_timeout (GstRTSPSession *session, guint timeout); guint gst_rtsp_session_get_timeout (GstRTSPSession *session); -/* touch the session, update last_access */ +/* session timeout stuff */ void gst_rtsp_session_touch (GstRTSPSession *session); +gboolean gst_rtsp_session_is_expired (GstRTSPSession *session); /* handle media in a session */ GstRTSPSessionMedia * gst_rtsp_session_manage_media (GstRTSPSession *sess,