media: disconnect from signal handlers in unprepare()
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-session.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19 #include <string.h>
20
21 #include "rtsp-session.h"
22
23 #define GST_RTSP_SESSION_GET_PRIVATE(obj)  \
24        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SESSION, GstRTSPSessionPrivate))
25
26 struct _GstRTSPSessionPrivate
27 {
28   GMutex lock;                  /* protects everything but sessionid and create_time */
29   gchar *sessionid;
30
31   guint timeout;
32   GTimeVal create_time;         /* immutable */
33   GTimeVal last_access;
34   gint expire_count;
35
36   GList *medias;
37 };
38
39 #undef DEBUG
40
41 #define DEFAULT_TIMEOUT 60
42
43 enum
44 {
45   PROP_0,
46   PROP_SESSIONID,
47   PROP_TIMEOUT,
48   PROP_LAST
49 };
50
51 GST_DEBUG_CATEGORY_STATIC (rtsp_session_debug);
52 #define GST_CAT_DEFAULT rtsp_session_debug
53
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);
59
60 G_DEFINE_TYPE (GstRTSPSession, gst_rtsp_session, G_TYPE_OBJECT);
61
62 static void
63 gst_rtsp_session_class_init (GstRTSPSessionClass * klass)
64 {
65   GObjectClass *gobject_class;
66
67   g_type_class_add_private (klass, sizeof (GstRTSPSessionPrivate));
68
69   gobject_class = G_OBJECT_CLASS (klass);
70
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;
74
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));
79
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));
84
85   GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsession", 0,
86       "GstRTSPSession");
87 }
88
89 static void
90 gst_rtsp_session_init (GstRTSPSession * session)
91 {
92   GstRTSPSessionPrivate *priv = GST_RTSP_SESSION_GET_PRIVATE (session);
93
94   session->priv = priv;
95
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);
100 }
101
102 static void
103 gst_rtsp_session_finalize (GObject * obj)
104 {
105   GstRTSPSession *session;
106   GstRTSPSessionPrivate *priv;
107
108   session = GST_RTSP_SESSION (obj);
109   priv = session->priv;
110
111   GST_INFO ("finalize session %p", session);
112
113   /* free all media */
114   g_list_free_full (priv->medias, g_object_unref);
115
116   /* free session id */
117   g_free (priv->sessionid);
118   g_mutex_clear (&priv->lock);
119
120   G_OBJECT_CLASS (gst_rtsp_session_parent_class)->finalize (obj);
121 }
122
123 static void
124 gst_rtsp_session_get_property (GObject * object, guint propid,
125     GValue * value, GParamSpec * pspec)
126 {
127   GstRTSPSession *session = GST_RTSP_SESSION (object);
128   GstRTSPSessionPrivate *priv = session->priv;
129
130   switch (propid) {
131     case PROP_SESSIONID:
132       g_value_set_string (value, priv->sessionid);
133       break;
134     case PROP_TIMEOUT:
135       g_value_set_uint (value, gst_rtsp_session_get_timeout (session));
136       break;
137     default:
138       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
139   }
140 }
141
142 static void
143 gst_rtsp_session_set_property (GObject * object, guint propid,
144     const GValue * value, GParamSpec * pspec)
145 {
146   GstRTSPSession *session = GST_RTSP_SESSION (object);
147   GstRTSPSessionPrivate *priv = session->priv;
148
149   switch (propid) {
150     case PROP_SESSIONID:
151       g_free (priv->sessionid);
152       priv->sessionid = g_value_dup_string (value);
153       break;
154     case PROP_TIMEOUT:
155       gst_rtsp_session_set_timeout (session, g_value_get_uint (value));
156       break;
157     default:
158       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
159   }
160 }
161
162 /**
163  * gst_rtsp_session_manage_media:
164  * @sess: a #GstRTSPSession
165  * @uri: the uri for the media
166  * @media: (transfer full): a #GstRTSPMedia
167  *
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().
170  *
171  * Ownership is taken from @media.
172  *
173  * Returns: (transfer none): a new @GstRTSPSessionMedia object.
174  */
175 GstRTSPSessionMedia *
176 gst_rtsp_session_manage_media (GstRTSPSession * sess, const GstRTSPUrl * uri,
177     GstRTSPMedia * media)
178 {
179   GstRTSPSessionPrivate *priv;
180   GstRTSPSessionMedia *result;
181
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);
187
188   priv = sess->priv;
189
190   result = gst_rtsp_session_media_new (uri, media);
191
192   g_mutex_lock (&priv->lock);
193   priv->medias = g_list_prepend (priv->medias, result);
194   g_mutex_unlock (&priv->lock);
195
196   GST_INFO ("manage new media %p in session %p", media, result);
197
198   return result;
199 }
200
201 /**
202  * gst_rtsp_session_release_media:
203  * @sess: a #GstRTSPSession
204  * @media: a #GstRTSPMedia
205  *
206  * Release the managed @media in @sess, freeing the memory allocated by it.
207  *
208  * Returns: %TRUE if there are more media session left in @sess.
209  */
210 gboolean
211 gst_rtsp_session_release_media (GstRTSPSession * sess,
212     GstRTSPSessionMedia * media)
213 {
214   GstRTSPSessionPrivate *priv;
215   GList *find;
216   gboolean more;
217
218   g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
219   g_return_val_if_fail (media != NULL, FALSE);
220
221   priv = sess->priv;
222
223   g_mutex_lock (&priv->lock);
224   find = g_list_find (priv->medias, media);
225   if (find)
226     priv->medias = g_list_delete_link (priv->medias, find);
227   more = (priv->medias != NULL);
228   g_mutex_unlock (&priv->lock);
229
230   if (find)
231     g_object_unref (media);
232
233   return more;
234 }
235
236 /**
237  * gst_rtsp_session_get_media:
238  * @sess: a #GstRTSPSession
239  * @url: the url for the media
240  *
241  * Get the session media of the @url.
242  *
243  * Returns: (transfer none): the configuration for @url in @sess.
244  */
245 GstRTSPSessionMedia *
246 gst_rtsp_session_get_media (GstRTSPSession * sess, const GstRTSPUrl * url)
247 {
248   GstRTSPSessionPrivate *priv;
249   GstRTSPSessionMedia *result;
250   GList *walk;
251
252   g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), NULL);
253   g_return_val_if_fail (url != NULL, NULL);
254
255   priv = sess->priv;
256   result = NULL;
257
258   g_mutex_lock (&priv->lock);
259   for (walk = priv->medias; walk; walk = g_list_next (walk)) {
260     result = (GstRTSPSessionMedia *) walk->data;
261
262     if (gst_rtsp_session_media_matches_url (result, url))
263       break;
264
265     result = NULL;
266   }
267   g_mutex_unlock (&priv->lock);
268
269   return result;
270 }
271
272 /**
273  * gst_rtsp_session_filter:
274  * @sess: a #GstRTSPSession
275  * @func: (scope call): a callback
276  * @user_data: user data passed to @func
277  *
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.
281  *
282  * If @func returns #GST_RTSP_FILTER_REMOVE, the media will be removed from
283  * @sess.
284  *
285  * If @func returns #GST_RTSP_FILTER_KEEP, the media will remain in @sess.
286  *
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
289  * function..
290  *
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.
294  */
295 GList *
296 gst_rtsp_session_filter (GstRTSPSession * sess,
297     GstRTSPSessionFilterFunc func, gpointer user_data)
298 {
299   GstRTSPSessionPrivate *priv;
300   GList *result, *walk, *next;
301
302   g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), NULL);
303   g_return_val_if_fail (func != NULL, NULL);
304
305   priv = sess->priv;
306
307   result = NULL;
308
309   g_mutex_lock (&priv->lock);
310   for (walk = priv->medias; walk; walk = next) {
311     GstRTSPSessionMedia *media = walk->data;
312
313     next = g_list_next (walk);
314
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);
319         break;
320       case GST_RTSP_FILTER_REF:
321         result = g_list_prepend (result, g_object_ref (media));
322         break;
323       case GST_RTSP_FILTER_KEEP:
324       default:
325         break;
326     }
327   }
328   g_mutex_unlock (&priv->lock);
329
330   return result;
331 }
332
333 /**
334  * gst_rtsp_session_new:
335  * @sessionid: a session id
336  *
337  * Create a new #GstRTSPSession instance with @sessionid.
338  */
339 GstRTSPSession *
340 gst_rtsp_session_new (const gchar * sessionid)
341 {
342   GstRTSPSession *result;
343
344   g_return_val_if_fail (sessionid != NULL, NULL);
345
346   result = g_object_new (GST_TYPE_RTSP_SESSION, "sessionid", sessionid, NULL);
347
348   return result;
349 }
350
351 /**
352  * gst_rtsp_session_get_sessionid:
353  * @session: a #GstRTSPSession
354  *
355  * Get the sessionid of @session.
356  *
357  * Returns: the sessionid of @session. The value remains valid as long as
358  * @session is alive.
359  */
360 const gchar *
361 gst_rtsp_session_get_sessionid (GstRTSPSession * session)
362 {
363   g_return_val_if_fail (GST_IS_RTSP_SESSION (session), NULL);
364
365   return session->priv->sessionid;
366 }
367
368 /**
369  * gst_rtsp_session_get_header:
370  * @session: a #GstRTSPSession
371  *
372  * Get the string that can be placed in the Session header field.
373  *
374  * Returns: (transfer full): the Session header of @session. g_free() after usage.
375  */
376 gchar *
377 gst_rtsp_session_get_header (GstRTSPSession * session)
378 {
379   GstRTSPSessionPrivate *priv;
380   gchar *result;
381
382   g_return_val_if_fail (GST_IS_RTSP_SESSION (session), NULL);
383
384   priv = session->priv;
385
386   g_mutex_lock (&priv->lock);
387   if (priv->timeout != 60)
388     result = g_strdup_printf ("%s; timeout=%d", priv->sessionid, priv->timeout);
389   else
390     result = g_strdup (priv->sessionid);
391   g_mutex_unlock (&priv->lock);
392
393   return result;
394 }
395
396 /**
397  * gst_rtsp_session_set_timeout:
398  * @session: a #GstRTSPSession
399  * @timeout: the new timeout
400  *
401  * Configure @session for a timeout of @timeout seconds. The session will be
402  * cleaned up when there is no activity for @timeout seconds.
403  */
404 void
405 gst_rtsp_session_set_timeout (GstRTSPSession * session, guint timeout)
406 {
407   GstRTSPSessionPrivate *priv;
408
409   g_return_if_fail (GST_IS_RTSP_SESSION (session));
410
411   priv = session->priv;
412
413   g_mutex_lock (&priv->lock);
414   priv->timeout = timeout;
415   g_mutex_unlock (&priv->lock);
416 }
417
418 /**
419  * gst_rtsp_session_get_timeout:
420  * @session: a #GstRTSPSession
421  *
422  * Get the timeout value of @session.
423  *
424  * Returns: the timeout of @session in seconds.
425  */
426 guint
427 gst_rtsp_session_get_timeout (GstRTSPSession * session)
428 {
429   GstRTSPSessionPrivate *priv;
430   guint res;
431
432   g_return_val_if_fail (GST_IS_RTSP_SESSION (session), 0);
433
434   priv = session->priv;
435
436   g_mutex_lock (&priv->lock);
437   res = priv->timeout;
438   g_mutex_unlock (&priv->lock);
439
440   return res;
441 }
442
443 /**
444  * gst_rtsp_session_touch:
445  * @session: a #GstRTSPSession
446  *
447  * Update the last_access time of the session to the current time.
448  */
449 void
450 gst_rtsp_session_touch (GstRTSPSession * session)
451 {
452   GstRTSPSessionPrivate *priv;
453
454   g_return_if_fail (GST_IS_RTSP_SESSION (session));
455
456   priv = session->priv;
457
458   g_mutex_lock (&priv->lock);
459   g_get_current_time (&priv->last_access);
460   g_mutex_unlock (&priv->lock);
461 }
462
463 /**
464  * gst_rtsp_session_prevent_expire:
465  * @session: a #GstRTSPSession
466  *
467  * Prevent @session from expiring.
468  */
469 void
470 gst_rtsp_session_prevent_expire (GstRTSPSession * session)
471 {
472   g_return_if_fail (GST_IS_RTSP_SESSION (session));
473
474   g_atomic_int_add (&session->priv->expire_count, 1);
475 }
476
477 /**
478  * gst_rtsp_session_allow_expire:
479  * @session: a #GstRTSPSession
480  *
481  * Allow @session to expire. This method must be called an equal
482  * amount of time as gst_rtsp_session_prevent_expire().
483  */
484 void
485 gst_rtsp_session_allow_expire (GstRTSPSession * session)
486 {
487   g_atomic_int_add (&session->priv->expire_count, -1);
488 }
489
490 /**
491  * gst_rtsp_session_next_timeout:
492  * @session: a #GstRTSPSession
493  * @now: the current system time
494  *
495  * Get the amount of milliseconds till the session will expire.
496  *
497  * Returns: the amount of milliseconds since the session will time out.
498  */
499 gint
500 gst_rtsp_session_next_timeout (GstRTSPSession * session, GTimeVal * now)
501 {
502   GstRTSPSessionPrivate *priv;
503   gint res;
504   GstClockTime last_access, now_ns;
505
506   g_return_val_if_fail (GST_IS_RTSP_SESSION (session), -1);
507   g_return_val_if_fail (now != NULL, -1);
508
509   priv = session->priv;
510
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);
515   }
516
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);
521
522   now_ns = GST_TIMEVAL_TO_TIME (*now);
523
524   if (last_access > now_ns)
525     res = GST_TIME_AS_MSECONDS (last_access - now_ns);
526   else
527     res = 0;
528
529   return res;
530 }
531
532 /**
533  * gst_rtsp_session_is_expired:
534  * @session: a #GstRTSPSession
535  * @now: the current system time
536  *
537  * Check if @session timeout out.
538  *
539  * Returns: %TRUE if @session timed out
540  */
541 gboolean
542 gst_rtsp_session_is_expired (GstRTSPSession * session, GTimeVal * now)
543 {
544   gboolean res;
545
546   res = (gst_rtsp_session_next_timeout (session, now) == 0);
547
548   return res;
549 }