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.
21 #include "rtsp-session.h"
23 #define GST_RTSP_SESSION_GET_PRIVATE(obj) \
24 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SESSION, GstRTSPSessionPrivate))
26 struct _GstRTSPSessionPrivate
28 GMutex lock; /* protects everything but sessionid and create_time */
32 GTimeVal create_time; /* immutable */
41 #define DEFAULT_TIMEOUT 60
51 GST_DEBUG_CATEGORY_STATIC (rtsp_session_debug);
52 #define GST_CAT_DEFAULT rtsp_session_debug
54 static void gst_rtsp_session_get_property (GObject * object, guint propid,
55 GValue * value, GParamSpec * pspec);
56 static void gst_rtsp_session_set_property (GObject * object, guint propid,
57 const GValue * value, GParamSpec * pspec);
58 static void gst_rtsp_session_finalize (GObject * obj);
60 G_DEFINE_TYPE (GstRTSPSession, gst_rtsp_session, G_TYPE_OBJECT);
63 gst_rtsp_session_class_init (GstRTSPSessionClass * klass)
65 GObjectClass *gobject_class;
67 g_type_class_add_private (klass, sizeof (GstRTSPSessionPrivate));
69 gobject_class = G_OBJECT_CLASS (klass);
71 gobject_class->get_property = gst_rtsp_session_get_property;
72 gobject_class->set_property = gst_rtsp_session_set_property;
73 gobject_class->finalize = gst_rtsp_session_finalize;
75 g_object_class_install_property (gobject_class, PROP_SESSIONID,
76 g_param_spec_string ("sessionid", "Sessionid", "the session id",
77 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
78 G_PARAM_STATIC_STRINGS));
80 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
81 g_param_spec_uint ("timeout", "timeout",
82 "the timeout of the session (0 = never)", 0, G_MAXUINT,
83 DEFAULT_TIMEOUT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
85 GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsession", 0,
90 gst_rtsp_session_init (GstRTSPSession * session)
92 GstRTSPSessionPrivate *priv = GST_RTSP_SESSION_GET_PRIVATE (session);
96 g_mutex_init (&priv->lock);
97 priv->timeout = DEFAULT_TIMEOUT;
98 g_get_current_time (&priv->create_time);
99 gst_rtsp_session_touch (session);
103 gst_rtsp_session_finalize (GObject * obj)
105 GstRTSPSession *session;
106 GstRTSPSessionPrivate *priv;
108 session = GST_RTSP_SESSION (obj);
109 priv = session->priv;
111 GST_INFO ("finalize session %p", session);
114 g_list_free_full (priv->medias, g_object_unref);
116 /* free session id */
117 g_free (priv->sessionid);
118 g_mutex_clear (&priv->lock);
120 G_OBJECT_CLASS (gst_rtsp_session_parent_class)->finalize (obj);
124 gst_rtsp_session_get_property (GObject * object, guint propid,
125 GValue * value, GParamSpec * pspec)
127 GstRTSPSession *session = GST_RTSP_SESSION (object);
128 GstRTSPSessionPrivate *priv = session->priv;
132 g_value_set_string (value, priv->sessionid);
135 g_value_set_uint (value, gst_rtsp_session_get_timeout (session));
138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
143 gst_rtsp_session_set_property (GObject * object, guint propid,
144 const GValue * value, GParamSpec * pspec)
146 GstRTSPSession *session = GST_RTSP_SESSION (object);
147 GstRTSPSessionPrivate *priv = session->priv;
151 g_free (priv->sessionid);
152 priv->sessionid = g_value_dup_string (value);
155 gst_rtsp_session_set_timeout (session, g_value_get_uint (value));
158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
163 * gst_rtsp_session_manage_media:
164 * @sess: a #GstRTSPSession
165 * @uri: the uri for the media
166 * @media: (transfer full): a #GstRTSPMedia
168 * Manage the media object @obj in @sess. @uri will be used to retrieve this
169 * media from the session with gst_rtsp_session_get_media().
171 * Ownership is taken from @media.
173 * Returns: (transfer none): a new @GstRTSPSessionMedia object.
175 GstRTSPSessionMedia *
176 gst_rtsp_session_manage_media (GstRTSPSession * sess, const GstRTSPUrl * uri,
177 GstRTSPMedia * media)
179 GstRTSPSessionPrivate *priv;
180 GstRTSPSessionMedia *result;
182 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), NULL);
183 g_return_val_if_fail (uri != NULL, NULL);
184 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
185 g_return_val_if_fail (gst_rtsp_media_get_status (media) ==
186 GST_RTSP_MEDIA_STATUS_PREPARED, NULL);
190 result = gst_rtsp_session_media_new (uri, media);
192 g_mutex_lock (&priv->lock);
193 priv->medias = g_list_prepend (priv->medias, result);
194 g_mutex_unlock (&priv->lock);
196 GST_INFO ("manage new media %p in session %p", media, result);
202 * gst_rtsp_session_release_media:
203 * @sess: a #GstRTSPSession
204 * @media: a #GstRTSPMedia
206 * Release the managed @media in @sess, freeing the memory allocated by it.
208 * Returns: %TRUE if there are more media session left in @sess.
211 gst_rtsp_session_release_media (GstRTSPSession * sess,
212 GstRTSPSessionMedia * media)
214 GstRTSPSessionPrivate *priv;
218 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
219 g_return_val_if_fail (media != NULL, FALSE);
223 g_mutex_lock (&priv->lock);
224 find = g_list_find (priv->medias, media);
226 priv->medias = g_list_delete_link (priv->medias, find);
227 more = (priv->medias != NULL);
228 g_mutex_unlock (&priv->lock);
231 g_object_unref (media);
237 * gst_rtsp_session_get_media:
238 * @sess: a #GstRTSPSession
239 * @url: the url for the media
241 * Get the session media of the @url.
243 * Returns: (transfer none): the configuration for @url in @sess.
245 GstRTSPSessionMedia *
246 gst_rtsp_session_get_media (GstRTSPSession * sess, const GstRTSPUrl * url)
248 GstRTSPSessionPrivate *priv;
249 GstRTSPSessionMedia *result;
252 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), NULL);
253 g_return_val_if_fail (url != NULL, NULL);
258 g_mutex_lock (&priv->lock);
259 for (walk = priv->medias; walk; walk = g_list_next (walk)) {
260 result = (GstRTSPSessionMedia *) walk->data;
262 if (gst_rtsp_session_media_matches_url (result, url))
267 g_mutex_unlock (&priv->lock);
273 * gst_rtsp_session_filter:
274 * @sess: a #GstRTSPSession
275 * @func: (scope call): a callback
276 * @user_data: user data passed to @func
278 * Call @func for each media in @sess. The result value of @func determines
279 * what happens to the media. @func will be called with @sess
280 * locked so no further actions on @sess can be performed from @func.
282 * If @func returns #GST_RTSP_FILTER_REMOVE, the media will be removed from
285 * If @func returns #GST_RTSP_FILTER_KEEP, the media will remain in @sess.
287 * If @func returns #GST_RTSP_FILTER_REF, the media will remain in @sess but
288 * will also be added with an additional ref to the result #GList of this
291 * Returns: (element-type GstRTSPSessionMedia) (transfer full): a GList with all
292 * media for which @func returned #GST_RTSP_FILTER_REF. After usage, each
293 * element in the #GList should be unreffed before the list is freed.
296 gst_rtsp_session_filter (GstRTSPSession * sess,
297 GstRTSPSessionFilterFunc func, gpointer user_data)
299 GstRTSPSessionPrivate *priv;
300 GList *result, *walk, *next;
302 g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), NULL);
303 g_return_val_if_fail (func != NULL, NULL);
309 g_mutex_lock (&priv->lock);
310 for (walk = priv->medias; walk; walk = next) {
311 GstRTSPSessionMedia *media = walk->data;
313 next = g_list_next (walk);
315 switch (func (sess, media, user_data)) {
316 case GST_RTSP_FILTER_REMOVE:
317 g_object_unref (media);
318 priv->medias = g_list_delete_link (priv->medias, walk);
320 case GST_RTSP_FILTER_REF:
321 result = g_list_prepend (result, g_object_ref (media));
323 case GST_RTSP_FILTER_KEEP:
328 g_mutex_unlock (&priv->lock);
334 * gst_rtsp_session_new:
335 * @sessionid: a session id
337 * Create a new #GstRTSPSession instance with @sessionid.
340 gst_rtsp_session_new (const gchar * sessionid)
342 GstRTSPSession *result;
344 g_return_val_if_fail (sessionid != NULL, NULL);
346 result = g_object_new (GST_TYPE_RTSP_SESSION, "sessionid", sessionid, NULL);
352 * gst_rtsp_session_get_sessionid:
353 * @session: a #GstRTSPSession
355 * Get the sessionid of @session.
357 * Returns: the sessionid of @session. The value remains valid as long as
361 gst_rtsp_session_get_sessionid (GstRTSPSession * session)
363 g_return_val_if_fail (GST_IS_RTSP_SESSION (session), NULL);
365 return session->priv->sessionid;
369 * gst_rtsp_session_get_header:
370 * @session: a #GstRTSPSession
372 * Get the string that can be placed in the Session header field.
374 * Returns: (transfer full): the Session header of @session. g_free() after usage.
377 gst_rtsp_session_get_header (GstRTSPSession * session)
379 GstRTSPSessionPrivate *priv;
382 g_return_val_if_fail (GST_IS_RTSP_SESSION (session), NULL);
384 priv = session->priv;
386 g_mutex_lock (&priv->lock);
387 if (priv->timeout != 60)
388 result = g_strdup_printf ("%s; timeout=%d", priv->sessionid, priv->timeout);
390 result = g_strdup (priv->sessionid);
391 g_mutex_unlock (&priv->lock);
397 * gst_rtsp_session_set_timeout:
398 * @session: a #GstRTSPSession
399 * @timeout: the new timeout
401 * Configure @session for a timeout of @timeout seconds. The session will be
402 * cleaned up when there is no activity for @timeout seconds.
405 gst_rtsp_session_set_timeout (GstRTSPSession * session, guint timeout)
407 GstRTSPSessionPrivate *priv;
409 g_return_if_fail (GST_IS_RTSP_SESSION (session));
411 priv = session->priv;
413 g_mutex_lock (&priv->lock);
414 priv->timeout = timeout;
415 g_mutex_unlock (&priv->lock);
419 * gst_rtsp_session_get_timeout:
420 * @session: a #GstRTSPSession
422 * Get the timeout value of @session.
424 * Returns: the timeout of @session in seconds.
427 gst_rtsp_session_get_timeout (GstRTSPSession * session)
429 GstRTSPSessionPrivate *priv;
432 g_return_val_if_fail (GST_IS_RTSP_SESSION (session), 0);
434 priv = session->priv;
436 g_mutex_lock (&priv->lock);
438 g_mutex_unlock (&priv->lock);
444 * gst_rtsp_session_touch:
445 * @session: a #GstRTSPSession
447 * Update the last_access time of the session to the current time.
450 gst_rtsp_session_touch (GstRTSPSession * session)
452 GstRTSPSessionPrivate *priv;
454 g_return_if_fail (GST_IS_RTSP_SESSION (session));
456 priv = session->priv;
458 g_mutex_lock (&priv->lock);
459 g_get_current_time (&priv->last_access);
460 g_mutex_unlock (&priv->lock);
464 * gst_rtsp_session_prevent_expire:
465 * @session: a #GstRTSPSession
467 * Prevent @session from expiring.
470 gst_rtsp_session_prevent_expire (GstRTSPSession * session)
472 g_return_if_fail (GST_IS_RTSP_SESSION (session));
474 g_atomic_int_add (&session->priv->expire_count, 1);
478 * gst_rtsp_session_allow_expire:
479 * @session: a #GstRTSPSession
481 * Allow @session to expire. This method must be called an equal
482 * amount of time as gst_rtsp_session_prevent_expire().
485 gst_rtsp_session_allow_expire (GstRTSPSession * session)
487 g_atomic_int_add (&session->priv->expire_count, -1);
491 * gst_rtsp_session_next_timeout:
492 * @session: a #GstRTSPSession
493 * @now: the current system time
495 * Get the amount of milliseconds till the session will expire.
497 * Returns: the amount of milliseconds since the session will time out.
500 gst_rtsp_session_next_timeout (GstRTSPSession * session, GTimeVal * now)
502 GstRTSPSessionPrivate *priv;
504 GstClockTime last_access, now_ns;
506 g_return_val_if_fail (GST_IS_RTSP_SESSION (session), -1);
507 g_return_val_if_fail (now != NULL, -1);
509 priv = session->priv;
511 g_mutex_lock (&priv->lock);
512 if (g_atomic_int_get (&priv->expire_count) != 0) {
513 /* touch session when the expire count is not 0 */
514 g_get_current_time (&priv->last_access);
517 last_access = GST_TIMEVAL_TO_TIME (priv->last_access);
518 /* add timeout allow for 5 seconds of extra time */
519 last_access += priv->timeout * GST_SECOND + (5 * GST_SECOND);
520 g_mutex_unlock (&priv->lock);
522 now_ns = GST_TIMEVAL_TO_TIME (*now);
524 if (last_access > now_ns)
525 res = GST_TIME_AS_MSECONDS (last_access - now_ns);
533 * gst_rtsp_session_is_expired:
534 * @session: a #GstRTSPSession
535 * @now: the current system time
537 * Check if @session timeout out.
539 * Returns: %TRUE if @session timed out
542 gst_rtsp_session_is_expired (GstRTSPSession * session, GTimeVal * now)
546 res = (gst_rtsp_session_next_timeout (session, now) == 0);