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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 * SECTION:rtsp-session
21 * @short_description: An object to manage media
22 * @see_also: #GstRTSPSessionPool, #GstRTSPSessionMedia, #GstRTSPMedia
24 * The #GstRTSPSession is identified by an id, unique in the
25 * #GstRTSPSessionPool that created the session and manages media and its
28 * A #GstRTSPSession has a timeout that can be retrieved with
29 * gst_rtsp_session_get_timeout(). You can check if the sessions is expired with
30 * gst_rtsp_session_is_expired(). gst_rtsp_session_touch() will reset the
31 * expiration counter of the session.
33 * When a client configures a media with SETUP, a session will be created to
34 * keep track of the configuration of that media. With
35 * gst_rtsp_session_manage_media(), the media is added to the managed media
36 * in the session. With gst_rtsp_session_release_media() the media can be
37 * released again from the session. Managed media is identified in the sessions
38 * with a url. Use gst_rtsp_session_get_media() to get the media that matches
39 * (part of) the given url.
41 * The media in a session can be iterated with gst_rtsp_session_filter().
43 * Last reviewed on 2013-07-11 (1.0.0)
48 #include "rtsp-session.h"
50 #define GST_RTSP_SESSION_GET_PRIVATE(obj) \
51 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SESSION, GstRTSPSessionPrivate))
53 struct _GstRTSPSessionPrivate
55 GMutex lock; /* protects everything but sessionid and create_time */
59 gboolean timeout_always_visible;
60 GMutex last_access_lock;
61 gint64 last_access_monotonic_time;
62 gint64 last_access_real_time;
71 #define DEFAULT_TIMEOUT 60
73 #define DEFAULT_ALWAYS_VISIBLE FALSE
80 PROP_TIMEOUT_ALWAYS_VISIBLE,
84 GST_DEBUG_CATEGORY_STATIC (rtsp_session_debug);
85 #define GST_CAT_DEFAULT rtsp_session_debug
87 static void gst_rtsp_session_get_property (GObject * object, guint propid,
88 GValue * value, GParamSpec * pspec);
89 static void gst_rtsp_session_set_property (GObject * object, guint propid,
90 const GValue * value, GParamSpec * pspec);
91 static void gst_rtsp_session_finalize (GObject * obj);
93 G_DEFINE_TYPE (GstRTSPSession, gst_rtsp_session, G_TYPE_OBJECT);
96 gst_rtsp_session_class_init (GstRTSPSessionClass * klass)
98 GObjectClass *gobject_class;
100 g_type_class_add_private (klass, sizeof (GstRTSPSessionPrivate));
102 gobject_class = G_OBJECT_CLASS (klass);
104 gobject_class->get_property = gst_rtsp_session_get_property;
105 gobject_class->set_property = gst_rtsp_session_set_property;
106 gobject_class->finalize = gst_rtsp_session_finalize;
108 g_object_class_install_property (gobject_class, PROP_SESSIONID,
109 g_param_spec_string ("sessionid", "Sessionid", "the session id",
110 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
111 G_PARAM_STATIC_STRINGS));
113 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
114 g_param_spec_uint ("timeout", "timeout",
115 "the timeout of the session (0 = never)", 0, G_MAXUINT,
116 DEFAULT_TIMEOUT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
118 g_object_class_install_property (gobject_class, PROP_TIMEOUT_ALWAYS_VISIBLE,
119 g_param_spec_boolean ("timeout-always-visible", "Timeout Always Visible ",
120 "timeout always visible in header",
121 DEFAULT_ALWAYS_VISIBLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
123 GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsession", 0,
128 gst_rtsp_session_init (GstRTSPSession * session)
130 GstRTSPSessionPrivate *priv = GST_RTSP_SESSION_GET_PRIVATE (session);
132 session->priv = priv;
134 GST_INFO ("init session %p", session);
136 g_mutex_init (&priv->lock);
137 g_mutex_init (&priv->last_access_lock);
138 priv->timeout = DEFAULT_TIMEOUT;
140 gst_rtsp_session_touch (session);
144 gst_rtsp_session_finalize (GObject * obj)
146 GstRTSPSession *session;
147 GstRTSPSessionPrivate *priv;
149 session = GST_RTSP_SESSION (obj);
150 priv = session->priv;
152 GST_INFO ("finalize session %p", session);
155 g_list_free_full (priv->medias, g_object_unref);
157 /* free session id */
158 g_free (priv->sessionid);
159 g_mutex_clear (&priv->last_access_lock);
160 g_mutex_clear (&priv->lock);
162 G_OBJECT_CLASS (gst_rtsp_session_parent_class)->finalize (obj);
166 gst_rtsp_session_get_property (GObject * object, guint propid,
167 GValue * value, GParamSpec * pspec)
169 GstRTSPSession *session = GST_RTSP_SESSION (object);
170 GstRTSPSessionPrivate *priv = session->priv;
174 g_value_set_string (value, priv->sessionid);
177 g_value_set_uint (value, gst_rtsp_session_get_timeout (session));
179 case PROP_TIMEOUT_ALWAYS_VISIBLE:
180 g_value_set_boolean (value, priv->timeout_always_visible);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
188 gst_rtsp_session_set_property (GObject * object, guint propid,
189 const GValue * value, GParamSpec * pspec)
191 GstRTSPSession *session = GST_RTSP_SESSION (object);
192 GstRTSPSessionPrivate *priv = session->priv;
196 g_free (priv->sessionid);
197 priv->sessionid = g_value_dup_string (value);
200 gst_rtsp_session_set_timeout (session, g_value_get_uint (value));
202 case PROP_TIMEOUT_ALWAYS_VISIBLE:
203 g_mutex_lock (&priv->lock);
204 priv->timeout_always_visible = g_value_get_boolean (value);
205 g_mutex_unlock (&priv->lock);
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
213 * gst_rtsp_session_manage_media:
214 * @sess: a #GstRTSPSession
215 * @path: the path for the media
216 * @media: (transfer full): a #GstRTSPMedia
218 * Manage the media object @obj in @sess. @path will be used to retrieve this
219 * media from the session with gst_rtsp_session_get_media().
221 * Ownership is taken from @media.
223 * Returns: (transfer none): a new @GstRTSPSessionMedia object.
225 GstRTSPSessionMedia *
226 gst_rtsp_session_manage_media (GstRTSPSession * sess, const gchar * path,
227 GstRTSPMedia * media)
229 GstRTSPSessionPrivate *priv;
230 GstRTSPSessionMedia *result;
231 GstRTSPMediaStatus status;
233 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), NULL);
234 g_return_val_if_fail (path != NULL, NULL);
235 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
236 status = gst_rtsp_media_get_status (media);
237 g_return_val_if_fail (status == GST_RTSP_MEDIA_STATUS_PREPARED || status ==
238 GST_RTSP_MEDIA_STATUS_SUSPENDED, NULL);
242 result = gst_rtsp_session_media_new (path, media);
244 g_mutex_lock (&priv->lock);
245 priv->medias = g_list_prepend (priv->medias, result);
246 priv->medias_cookie++;
247 g_mutex_unlock (&priv->lock);
249 GST_INFO ("manage new media %p in session %p", media, result);
255 gst_rtsp_session_unset_transport_keepalive (GstRTSPSessionMedia * sessmedia)
260 media = gst_rtsp_session_media_get_media (sessmedia);
261 n_streams = gst_rtsp_media_n_streams (media);
263 for (i = 0; i < n_streams; i++) {
264 GstRTSPStreamTransport *transport =
265 gst_rtsp_session_media_get_transport (sessmedia, i);
270 gst_rtsp_stream_transport_set_keepalive (transport, NULL, NULL, NULL);
275 * gst_rtsp_session_release_media:
276 * @sess: a #GstRTSPSession
277 * @media: (transfer none): a #GstRTSPMedia
279 * Release the managed @media in @sess, freeing the memory allocated by it.
281 * Returns: %TRUE if there are more media session left in @sess.
284 gst_rtsp_session_release_media (GstRTSPSession * sess,
285 GstRTSPSessionMedia * media)
287 GstRTSPSessionPrivate *priv;
291 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
292 g_return_val_if_fail (media != NULL, FALSE);
296 g_mutex_lock (&priv->lock);
297 find = g_list_find (priv->medias, media);
299 priv->medias = g_list_delete_link (priv->medias, find);
300 priv->medias_cookie++;
302 more = (priv->medias != NULL);
303 g_mutex_unlock (&priv->lock);
306 gst_rtsp_session_unset_transport_keepalive (media);
309 g_object_unref (media);
315 * gst_rtsp_session_get_media:
316 * @sess: a #GstRTSPSession
317 * @path: the path for the media
318 * @matched: (out): the amount of matched characters
320 * Get the session media for @path. @matched will contain the number of matched
321 * characters of @path.
323 * Returns: (transfer none) (nullable): the configuration for @path in @sess.
325 GstRTSPSessionMedia *
326 gst_rtsp_session_get_media (GstRTSPSession * sess, const gchar * path,
329 GstRTSPSessionPrivate *priv;
330 GstRTSPSessionMedia *result;
334 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), NULL);
335 g_return_val_if_fail (path != NULL, NULL);
341 g_mutex_lock (&priv->lock);
342 for (walk = priv->medias; walk; walk = g_list_next (walk)) {
343 GstRTSPSessionMedia *test;
345 test = (GstRTSPSessionMedia *) walk->data;
347 /* find largest match */
348 if (gst_rtsp_session_media_matches (test, path, matched)) {
349 if (best < *matched) {
355 g_mutex_unlock (&priv->lock);
363 * gst_rtsp_session_filter:
364 * @sess: a #GstRTSPSession
365 * @func: (scope call) (allow-none): a callback
366 * @user_data: (closure): user data passed to @func
368 * Call @func for each media in @sess. The result value of @func determines
369 * what happens to the media. @func will be called with @sess
370 * locked so no further actions on @sess can be performed from @func.
372 * If @func returns #GST_RTSP_FILTER_REMOVE, the media will be removed from
375 * If @func returns #GST_RTSP_FILTER_KEEP, the media will remain in @sess.
377 * If @func returns #GST_RTSP_FILTER_REF, the media will remain in @sess but
378 * will also be added with an additional ref to the result #GList of this
381 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for all media.
383 * Returns: (element-type GstRTSPSessionMedia) (transfer full): a GList with all
384 * media for which @func returned #GST_RTSP_FILTER_REF. After usage, each
385 * element in the #GList should be unreffed before the list is freed.
388 gst_rtsp_session_filter (GstRTSPSession * sess,
389 GstRTSPSessionFilterFunc func, gpointer user_data)
391 GstRTSPSessionPrivate *priv;
392 GList *result, *walk, *next;
396 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), NULL);
402 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
404 g_mutex_lock (&priv->lock);
406 cookie = priv->medias_cookie;
407 for (walk = priv->medias; walk; walk = next) {
408 GstRTSPSessionMedia *media = walk->data;
409 GstRTSPFilterResult res;
412 next = g_list_next (walk);
415 /* only visit each media once */
416 if (g_hash_table_contains (visited, media))
419 g_hash_table_add (visited, g_object_ref (media));
420 g_mutex_unlock (&priv->lock);
422 res = func (sess, media, user_data);
424 g_mutex_lock (&priv->lock);
426 res = GST_RTSP_FILTER_REF;
428 changed = (cookie != priv->medias_cookie);
431 case GST_RTSP_FILTER_REMOVE:
433 priv->medias = g_list_remove (priv->medias, media);
435 priv->medias = g_list_delete_link (priv->medias, walk);
436 cookie = ++priv->medias_cookie;
437 g_object_unref (media);
439 case GST_RTSP_FILTER_REF:
440 result = g_list_prepend (result, g_object_ref (media));
442 case GST_RTSP_FILTER_KEEP:
449 g_mutex_unlock (&priv->lock);
452 g_hash_table_unref (visited);
458 * gst_rtsp_session_new:
459 * @sessionid: a session id
461 * Create a new #GstRTSPSession instance with @sessionid.
463 * Returns: (transfer full): a new #GstRTSPSession
466 gst_rtsp_session_new (const gchar * sessionid)
468 GstRTSPSession *result;
470 g_return_val_if_fail (sessionid != NULL, NULL);
472 result = g_object_new (GST_TYPE_RTSP_SESSION, "sessionid", sessionid, NULL);
478 * gst_rtsp_session_get_sessionid:
479 * @session: a #GstRTSPSession
481 * Get the sessionid of @session.
483 * Returns: (transfer none) (nullable): the sessionid of @session.
484 * The value remains valid as long as @session is alive.
487 gst_rtsp_session_get_sessionid (GstRTSPSession * session)
489 g_return_val_if_fail (GST_IS_RTSP_SESSION (session), NULL);
491 return session->priv->sessionid;
495 * gst_rtsp_session_get_header:
496 * @session: a #GstRTSPSession
498 * Get the string that can be placed in the Session header field.
500 * Returns: (transfer full) (nullable): the Session header of @session.
501 * g_free() after usage.
504 gst_rtsp_session_get_header (GstRTSPSession * session)
506 GstRTSPSessionPrivate *priv;
509 g_return_val_if_fail (GST_IS_RTSP_SESSION (session), NULL);
511 priv = session->priv;
514 g_mutex_lock (&priv->lock);
515 if (priv->timeout_always_visible || priv->timeout != 60)
516 result = g_strdup_printf ("%s;timeout=%d", priv->sessionid, priv->timeout);
518 result = g_strdup (priv->sessionid);
519 g_mutex_unlock (&priv->lock);
525 * gst_rtsp_session_set_timeout:
526 * @session: a #GstRTSPSession
527 * @timeout: the new timeout
529 * Configure @session for a timeout of @timeout seconds. The session will be
530 * cleaned up when there is no activity for @timeout seconds.
533 gst_rtsp_session_set_timeout (GstRTSPSession * session, guint timeout)
535 GstRTSPSessionPrivate *priv;
537 g_return_if_fail (GST_IS_RTSP_SESSION (session));
539 priv = session->priv;
541 g_mutex_lock (&priv->lock);
542 priv->timeout = timeout;
543 g_mutex_unlock (&priv->lock);
547 * gst_rtsp_session_get_timeout:
548 * @session: a #GstRTSPSession
550 * Get the timeout value of @session.
552 * Returns: the timeout of @session in seconds.
555 gst_rtsp_session_get_timeout (GstRTSPSession * session)
557 GstRTSPSessionPrivate *priv;
560 g_return_val_if_fail (GST_IS_RTSP_SESSION (session), 0);
562 priv = session->priv;
564 g_mutex_lock (&priv->lock);
566 g_mutex_unlock (&priv->lock);
572 * gst_rtsp_session_touch:
573 * @session: a #GstRTSPSession
575 * Update the last_access time of the session to the current time.
578 gst_rtsp_session_touch (GstRTSPSession * session)
580 GstRTSPSessionPrivate *priv;
582 g_return_if_fail (GST_IS_RTSP_SESSION (session));
584 priv = session->priv;
586 g_mutex_lock (&priv->last_access_lock);
587 priv->last_access_monotonic_time = g_get_monotonic_time ();
588 priv->last_access_real_time = g_get_real_time ();
589 g_mutex_unlock (&priv->last_access_lock);
593 * gst_rtsp_session_prevent_expire:
594 * @session: a #GstRTSPSession
596 * Prevent @session from expiring.
599 gst_rtsp_session_prevent_expire (GstRTSPSession * session)
601 g_return_if_fail (GST_IS_RTSP_SESSION (session));
603 g_atomic_int_add (&session->priv->expire_count, 1);
607 * gst_rtsp_session_allow_expire:
608 * @session: a #GstRTSPSession
610 * Allow @session to expire. This method must be called an equal
611 * amount of time as gst_rtsp_session_prevent_expire().
614 gst_rtsp_session_allow_expire (GstRTSPSession * session)
616 g_atomic_int_add (&session->priv->expire_count, -1);
620 * gst_rtsp_session_next_timeout_usec:
621 * @session: a #GstRTSPSession
622 * @now: the current monotonic time
624 * Get the amount of milliseconds till the session will expire.
626 * Returns: the amount of milliseconds since the session will time out.
629 gst_rtsp_session_next_timeout_usec (GstRTSPSession * session, gint64 now)
631 GstRTSPSessionPrivate *priv;
633 GstClockTime last_access, now_ns;
635 g_return_val_if_fail (GST_IS_RTSP_SESSION (session), -1);
637 priv = session->priv;
639 g_mutex_lock (&priv->lock);
640 /* If timeout is set to 0, we never timeout */
641 if (priv->timeout == 0) {
642 g_mutex_unlock (&priv->lock);
645 g_mutex_unlock (&priv->lock);
647 g_mutex_lock (&priv->last_access_lock);
648 if (g_atomic_int_get (&priv->expire_count) != 0) {
649 /* touch session when the expire count is not 0 */
650 priv->last_access_monotonic_time = g_get_monotonic_time ();
651 priv->last_access_real_time = g_get_real_time ();
654 last_access = GST_USECOND * (priv->last_access_monotonic_time);
656 /* add timeout allow for 5 seconds of extra time */
657 last_access += priv->timeout * GST_SECOND + (5 * GST_SECOND);
658 g_mutex_unlock (&priv->last_access_lock);
660 now_ns = GST_USECOND * now;
662 if (last_access > now_ns) {
663 res = GST_TIME_AS_MSECONDS (last_access - now_ns);
671 /****** Deprecated API *******/
674 * gst_rtsp_session_next_timeout:
675 * @session: a #GstRTSPSession
676 * @now: (transfer none): the current system time
678 * Get the amount of milliseconds till the session will expire.
680 * Returns: the amount of milliseconds since the session will time out.
682 * Deprecated: Use gst_rtsp_session_next_timeout_usec() instead.
684 #ifndef GST_REMOVE_DEPRECATED
686 gst_rtsp_session_next_timeout (GstRTSPSession * session, GTimeVal * now)
688 GstRTSPSessionPrivate *priv;
690 GstClockTime last_access, now_ns;
692 g_return_val_if_fail (GST_IS_RTSP_SESSION (session), -1);
693 g_return_val_if_fail (now != NULL, -1);
695 priv = session->priv;
697 g_mutex_lock (&priv->last_access_lock);
698 if (g_atomic_int_get (&priv->expire_count) != 0) {
699 /* touch session when the expire count is not 0 */
700 priv->last_access_monotonic_time = g_get_monotonic_time ();
701 priv->last_access_real_time = g_get_real_time ();
704 last_access = GST_USECOND * (priv->last_access_real_time);
706 /* add timeout allow for 5 seconds of extra time */
707 last_access += priv->timeout * GST_SECOND + (5 * GST_SECOND);
708 g_mutex_unlock (&priv->last_access_lock);
710 now_ns = GST_TIMEVAL_TO_TIME (*now);
712 if (last_access > now_ns) {
713 res = GST_TIME_AS_MSECONDS (last_access - now_ns);
723 * gst_rtsp_session_is_expired_usec:
724 * @session: a #GstRTSPSession
725 * @now: the current monotonic time
727 * Check if @session timeout out.
729 * Returns: %TRUE if @session timed out
732 gst_rtsp_session_is_expired_usec (GstRTSPSession * session, gint64 now)
736 res = (gst_rtsp_session_next_timeout_usec (session, now) == 0);
742 /****** Deprecated API *******/
745 * gst_rtsp_session_is_expired:
746 * @session: a #GstRTSPSession
747 * @now: (transfer none): the current system time
749 * Check if @session timeout out.
751 * Returns: %TRUE if @session timed out
753 * Deprecated: Use gst_rtsp_session_is_expired_usec() instead.
755 #ifndef GST_REMOVE_DEPRECATED
757 gst_rtsp_session_is_expired (GstRTSPSession * session, GTimeVal * now)
761 res = gst_rtsp_session_next_timeout_usec (session,
762 (now->tv_sec * G_USEC_PER_SEC) + (now->tv_usec));