docs: improve docs
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-session-pool.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 /**
20  * SECTION:rtsp-session-pool
21  * @short_description: An object for managing sessions
22  * @see_also: #GstRTSPSession
23  *
24  * The #GstRTSPSessionPool object manages a list of #GstRTSPSession objects.
25  *
26  * The maximum number of sessions can be configured with
27  * gst_rtsp_session_pool_set_max_sessions(). The current number of sessions can
28  * be retrieved with gst_rtsp_session_pool_get_n_sessions().
29  *
30  * Use gst_rtsp_session_pool_create() to create a new #GstRTSPSession object.
31  * The session object can be found again with its id and
32  * gst_rtsp_session_pool_find().
33  *
34  * All sessions can be iterated with gst_rtsp_session_pool_filter().
35  *
36  * Run gst_rtsp_session_pool_cleanup() periodically to remove timed out sessions
37  * or use gst_rtsp_session_pool_create_watch() to be notified when session
38  * cleanup should be performed.
39  *
40  * Last reviewed on 2013-07-11 (1.0.0)
41  */
42
43 #include "rtsp-session-pool.h"
44
45 #define GST_RTSP_SESSION_POOL_GET_PRIVATE(obj)  \
46          (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SESSION_POOL, GstRTSPSessionPoolPrivate))
47
48 struct _GstRTSPSessionPoolPrivate
49 {
50   GMutex lock;                  /* protects everything in this struct */
51   guint max_sessions;
52   GHashTable *sessions;
53 };
54
55 #define DEFAULT_MAX_SESSIONS 0
56
57 enum
58 {
59   PROP_0,
60   PROP_MAX_SESSIONS,
61   PROP_LAST
62 };
63
64 static const gchar session_id_charset[] =
65     { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
66   'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
67   'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
68   'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7',
69   '8', '9', '$', '-', '_', '.', '+'
70 };
71
72 GST_DEBUG_CATEGORY_STATIC (rtsp_session_debug);
73 #define GST_CAT_DEFAULT rtsp_session_debug
74
75 static void gst_rtsp_session_pool_get_property (GObject * object, guint propid,
76     GValue * value, GParamSpec * pspec);
77 static void gst_rtsp_session_pool_set_property (GObject * object, guint propid,
78     const GValue * value, GParamSpec * pspec);
79 static void gst_rtsp_session_pool_finalize (GObject * object);
80
81 static gchar *create_session_id (GstRTSPSessionPool * pool);
82 static GstRTSPSession *create_session (GstRTSPSessionPool * pool,
83     const gchar * id);
84
85 G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);
86
87 static void
88 gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
89 {
90   GObjectClass *gobject_class;
91
92   g_type_class_add_private (klass, sizeof (GstRTSPSessionPoolPrivate));
93
94   gobject_class = G_OBJECT_CLASS (klass);
95
96   gobject_class->get_property = gst_rtsp_session_pool_get_property;
97   gobject_class->set_property = gst_rtsp_session_pool_set_property;
98   gobject_class->finalize = gst_rtsp_session_pool_finalize;
99
100   g_object_class_install_property (gobject_class, PROP_MAX_SESSIONS,
101       g_param_spec_uint ("max-sessions", "Max Sessions",
102           "the maximum amount of sessions (0 = unlimited)",
103           0, G_MAXUINT, DEFAULT_MAX_SESSIONS,
104           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
105
106   klass->create_session_id = create_session_id;
107   klass->create_session = create_session;
108
109   GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsessionpool", 0,
110       "GstRTSPSessionPool");
111 }
112
113 static void
114 gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
115 {
116   GstRTSPSessionPoolPrivate *priv = GST_RTSP_SESSION_POOL_GET_PRIVATE (pool);
117
118   pool->priv = priv;
119
120   g_mutex_init (&priv->lock);
121   priv->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
122       NULL, g_object_unref);
123   priv->max_sessions = DEFAULT_MAX_SESSIONS;
124 }
125
126 static void
127 gst_rtsp_session_pool_finalize (GObject * object)
128 {
129   GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
130   GstRTSPSessionPoolPrivate *priv = pool->priv;
131
132   g_mutex_clear (&priv->lock);
133   g_hash_table_unref (priv->sessions);
134
135   G_OBJECT_CLASS (gst_rtsp_session_pool_parent_class)->finalize (object);
136 }
137
138 static void
139 gst_rtsp_session_pool_get_property (GObject * object, guint propid,
140     GValue * value, GParamSpec * pspec)
141 {
142   GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
143
144   switch (propid) {
145     case PROP_MAX_SESSIONS:
146       g_value_set_uint (value, gst_rtsp_session_pool_get_max_sessions (pool));
147       break;
148     default:
149       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
150       break;
151   }
152 }
153
154 static void
155 gst_rtsp_session_pool_set_property (GObject * object, guint propid,
156     const GValue * value, GParamSpec * pspec)
157 {
158   GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
159
160   switch (propid) {
161     case PROP_MAX_SESSIONS:
162       gst_rtsp_session_pool_set_max_sessions (pool, g_value_get_uint (value));
163       break;
164     default:
165       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
166       break;
167   }
168 }
169
170 /**
171  * gst_rtsp_session_pool_new:
172  *
173  * Create a new #GstRTSPSessionPool instance.
174  *
175  * Returns: A new #GstRTSPSessionPool. g_object_unref() after usage.
176  */
177 GstRTSPSessionPool *
178 gst_rtsp_session_pool_new (void)
179 {
180   GstRTSPSessionPool *result;
181
182   result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);
183
184   return result;
185 }
186
187 /**
188  * gst_rtsp_session_pool_set_max_sessions:
189  * @pool: a #GstRTSPSessionPool
190  * @max: the maximum number of sessions
191  *
192  * Configure the maximum allowed number of sessions in @pool to @max.
193  * A value of 0 means an unlimited amount of sessions.
194  */
195 void
196 gst_rtsp_session_pool_set_max_sessions (GstRTSPSessionPool * pool, guint max)
197 {
198   GstRTSPSessionPoolPrivate *priv;
199
200   g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));
201
202   priv = pool->priv;
203
204   g_mutex_lock (&priv->lock);
205   priv->max_sessions = max;
206   g_mutex_unlock (&priv->lock);
207 }
208
209 /**
210  * gst_rtsp_session_pool_get_max_sessions:
211  * @pool: a #GstRTSPSessionPool
212  *
213  * Get the maximum allowed number of sessions in @pool. 0 means an unlimited
214  * amount of sessions.
215  *
216  * Returns: the maximum allowed number of sessions.
217  */
218 guint
219 gst_rtsp_session_pool_get_max_sessions (GstRTSPSessionPool * pool)
220 {
221   GstRTSPSessionPoolPrivate *priv;
222   guint result;
223
224   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
225
226   priv = pool->priv;
227
228   g_mutex_lock (&priv->lock);
229   result = priv->max_sessions;
230   g_mutex_unlock (&priv->lock);
231
232   return result;
233 }
234
235 /**
236  * gst_rtsp_session_pool_get_n_sessions:
237  * @pool: a #GstRTSPSessionPool
238  *
239  * Get the amount of active sessions in @pool.
240  *
241  * Returns: the amount of active sessions in @pool.
242  */
243 guint
244 gst_rtsp_session_pool_get_n_sessions (GstRTSPSessionPool * pool)
245 {
246   GstRTSPSessionPoolPrivate *priv;
247   guint result;
248
249   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
250
251   priv = pool->priv;
252
253   g_mutex_lock (&priv->lock);
254   result = g_hash_table_size (priv->sessions);
255   g_mutex_unlock (&priv->lock);
256
257   return result;
258 }
259
260 /**
261  * gst_rtsp_session_pool_find:
262  * @pool: the pool to search
263  * @sessionid: the session id
264  *
265  * Find the session with @sessionid in @pool. The access time of the session
266  * will be updated with gst_rtsp_session_touch().
267  *
268  * Returns: (transfer full): the #GstRTSPSession with @sessionid or %NULL when the session did
269  * not exist. g_object_unref() after usage.
270  */
271 GstRTSPSession *
272 gst_rtsp_session_pool_find (GstRTSPSessionPool * pool, const gchar * sessionid)
273 {
274   GstRTSPSessionPoolPrivate *priv;
275   GstRTSPSession *result;
276
277   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
278   g_return_val_if_fail (sessionid != NULL, NULL);
279
280   priv = pool->priv;
281
282   g_mutex_lock (&priv->lock);
283   result = g_hash_table_lookup (priv->sessions, sessionid);
284   if (result) {
285     g_object_ref (result);
286     gst_rtsp_session_touch (result);
287   }
288   g_mutex_unlock (&priv->lock);
289
290   return result;
291 }
292
293 static gchar *
294 create_session_id (GstRTSPSessionPool * pool)
295 {
296   gchar id[16];
297   gint i;
298
299   for (i = 0; i < 16; i++) {
300     id[i] =
301         session_id_charset[g_random_int_range (0,
302             G_N_ELEMENTS (session_id_charset))];
303   }
304
305   return g_strndup (id, 16);
306 }
307
308 static GstRTSPSession *
309 create_session (GstRTSPSessionPool * pool, const gchar * id)
310 {
311   return gst_rtsp_session_new (id);
312 }
313
314 /**
315  * gst_rtsp_session_pool_create:
316  * @pool: a #GstRTSPSessionPool
317  *
318  * Create a new #GstRTSPSession object in @pool.
319  *
320  * Returns: (transfer none): a new #GstRTSPSession.
321  */
322 GstRTSPSession *
323 gst_rtsp_session_pool_create (GstRTSPSessionPool * pool)
324 {
325   GstRTSPSessionPoolPrivate *priv;
326   GstRTSPSession *result = NULL;
327   GstRTSPSessionPoolClass *klass;
328   gchar *id = NULL;
329   guint retry;
330
331   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
332
333   priv = pool->priv;
334
335   klass = GST_RTSP_SESSION_POOL_GET_CLASS (pool);
336
337   retry = 0;
338   do {
339     /* start by creating a new random session id, we assume that this is random
340      * enough to not cause a collision, which we will check later  */
341     if (klass->create_session_id)
342       id = klass->create_session_id (pool);
343     else
344       goto no_function;
345
346     if (id == NULL)
347       goto no_session;
348
349     g_mutex_lock (&priv->lock);
350     /* check session limit */
351     if (priv->max_sessions > 0) {
352       if (g_hash_table_size (priv->sessions) >= priv->max_sessions)
353         goto too_many_sessions;
354     }
355     /* check if the sessionid existed */
356     result = g_hash_table_lookup (priv->sessions, id);
357     if (result) {
358       /* found, retry with a different session id */
359       result = NULL;
360       retry++;
361       if (retry > 100)
362         goto collision;
363     } else {
364       /* not found, create session and insert it in the pool */
365       if (klass->create_session)
366         result = create_session (pool, id);
367       if (result == NULL)
368         goto too_many_sessions;
369       /* take additional ref for the pool */
370       g_object_ref (result);
371       g_hash_table_insert (priv->sessions,
372           (gchar *) gst_rtsp_session_get_sessionid (result), result);
373     }
374     g_mutex_unlock (&priv->lock);
375
376     g_free (id);
377   } while (result == NULL);
378
379   return result;
380
381   /* ERRORS */
382 no_function:
383   {
384     GST_WARNING ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
385     return NULL;
386   }
387 no_session:
388   {
389     GST_WARNING ("can't create session id with GstRTSPSessionPool %p", pool);
390     return NULL;
391   }
392 collision:
393   {
394     GST_WARNING ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
395     g_mutex_unlock (&priv->lock);
396     g_free (id);
397     return NULL;
398   }
399 too_many_sessions:
400   {
401     GST_WARNING ("session pool reached max sessions of %d", priv->max_sessions);
402     g_mutex_unlock (&priv->lock);
403     g_free (id);
404     return NULL;
405   }
406 }
407
408 /**
409  * gst_rtsp_session_pool_remove:
410  * @pool: a #GstRTSPSessionPool
411  * @sess: a #GstRTSPSession
412  *
413  * Remove @sess from @pool, releasing the ref that the pool has on @sess.
414  *
415  * Returns: %TRUE if the session was found and removed.
416  */
417 gboolean
418 gst_rtsp_session_pool_remove (GstRTSPSessionPool * pool, GstRTSPSession * sess)
419 {
420   GstRTSPSessionPoolPrivate *priv;
421   gboolean found;
422
423   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), FALSE);
424   g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
425
426   priv = pool->priv;
427
428   g_mutex_lock (&priv->lock);
429   found =
430       g_hash_table_remove (priv->sessions,
431       gst_rtsp_session_get_sessionid (sess));
432   g_mutex_unlock (&priv->lock);
433
434   return found;
435 }
436
437 static gboolean
438 cleanup_func (gchar * sessionid, GstRTSPSession * sess, GTimeVal * now)
439 {
440   return gst_rtsp_session_is_expired (sess, now);
441 }
442
443 /**
444  * gst_rtsp_session_pool_cleanup:
445  * @pool: a #GstRTSPSessionPool
446  *
447  * Inspect all the sessions in @pool and remove the sessions that are inactive
448  * for more than their timeout.
449  *
450  * Returns: the amount of sessions that got removed.
451  */
452 guint
453 gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
454 {
455   GstRTSPSessionPoolPrivate *priv;
456   guint result;
457   GTimeVal now;
458
459   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
460
461   priv = pool->priv;
462
463   g_get_current_time (&now);
464
465   g_mutex_lock (&priv->lock);
466   result =
467       g_hash_table_foreach_remove (priv->sessions, (GHRFunc) cleanup_func,
468       &now);
469   g_mutex_unlock (&priv->lock);
470
471   return result;
472 }
473
474 typedef struct
475 {
476   GstRTSPSessionPool *pool;
477   GstRTSPSessionPoolFilterFunc func;
478   gpointer user_data;
479   GList *list;
480 } FilterData;
481
482 static gboolean
483 filter_func (gchar * sessionid, GstRTSPSession * sess, FilterData * data)
484 {
485   switch (data->func (data->pool, sess, data->user_data)) {
486     case GST_RTSP_FILTER_REMOVE:
487       return TRUE;
488     case GST_RTSP_FILTER_REF:
489       /* keep ref */
490       data->list = g_list_prepend (data->list, g_object_ref (sess));
491       /* fallthrough */
492     default:
493     case GST_RTSP_FILTER_KEEP:
494       return FALSE;
495   }
496 }
497
498 /**
499  * gst_rtsp_session_pool_filter:
500  * @pool: a #GstRTSPSessionPool
501  * @func: (scope call): a callback
502  * @user_data: user data passed to @func
503  *
504  * Call @func for each session in @pool. The result value of @func determines
505  * what happens to the session. @func will be called with the session pool
506  * locked so no further actions on @pool can be performed from @func.
507  *
508  * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
509  * @pool.
510  *
511  * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
512  *
513  * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
514  * will also be added with an additional ref to the result GList of this
515  * function..
516  *
517  * Returns: (element-type GstRTSPSession) (transfer full): a GList with all
518  * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
519  * element in the GList should be unreffed before the list is freed.
520  */
521 GList *
522 gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
523     GstRTSPSessionPoolFilterFunc func, gpointer user_data)
524 {
525   GstRTSPSessionPoolPrivate *priv;
526   FilterData data;
527
528   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
529   g_return_val_if_fail (func != NULL, NULL);
530
531   priv = pool->priv;
532
533   data.pool = pool;
534   data.func = func;
535   data.user_data = user_data;
536   data.list = NULL;
537
538   g_mutex_lock (&priv->lock);
539   g_hash_table_foreach_remove (priv->sessions, (GHRFunc) filter_func, &data);
540   g_mutex_unlock (&priv->lock);
541
542   return data.list;
543 }
544
545 typedef struct
546 {
547   GSource source;
548   GstRTSPSessionPool *pool;
549   gint timeout;
550 } GstPoolSource;
551
552 static void
553 collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
554 {
555   gint timeout;
556   GTimeVal now;
557
558   g_get_current_time (&now);
559
560   timeout = gst_rtsp_session_next_timeout (sess, &now);
561   GST_INFO ("%p: next timeout: %d", sess, timeout);
562   if (psrc->timeout == -1 || timeout < psrc->timeout)
563     psrc->timeout = timeout;
564 }
565
566 static gboolean
567 gst_pool_source_prepare (GSource * source, gint * timeout)
568 {
569   GstRTSPSessionPoolPrivate *priv;
570   GstPoolSource *psrc;
571   gboolean result;
572
573   psrc = (GstPoolSource *) source;
574   psrc->timeout = -1;
575   priv = psrc->pool->priv;
576
577   g_mutex_lock (&priv->lock);
578   g_hash_table_foreach (priv->sessions, (GHFunc) collect_timeout, psrc);
579   g_mutex_unlock (&priv->lock);
580
581   if (timeout)
582     *timeout = psrc->timeout;
583
584   result = psrc->timeout == 0;
585
586   GST_INFO ("prepare %d, %d", psrc->timeout, result);
587
588   return result;
589 }
590
591 static gboolean
592 gst_pool_source_check (GSource * source)
593 {
594   GST_INFO ("check");
595
596   return gst_pool_source_prepare (source, NULL);
597 }
598
599 static gboolean
600 gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
601     gpointer user_data)
602 {
603   gboolean res;
604   GstPoolSource *psrc = (GstPoolSource *) source;
605   GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;
606
607   GST_INFO ("dispatch");
608
609   if (func)
610     res = func (psrc->pool, user_data);
611   else
612     res = FALSE;
613
614   return res;
615 }
616
617 static void
618 gst_pool_source_finalize (GSource * source)
619 {
620   GstPoolSource *psrc = (GstPoolSource *) source;
621
622   GST_INFO ("finalize %p", psrc);
623
624   g_object_unref (psrc->pool);
625   psrc->pool = NULL;
626 }
627
628 static GSourceFuncs gst_pool_source_funcs = {
629   gst_pool_source_prepare,
630   gst_pool_source_check,
631   gst_pool_source_dispatch,
632   gst_pool_source_finalize
633 };
634
635 /**
636  * gst_rtsp_session_pool_create_watch:
637  * @pool: a #GstRTSPSessionPool
638  *
639  * A GSource that will be dispatched when the session should be cleaned up.
640  */
641 GSource *
642 gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
643 {
644   GstPoolSource *source;
645
646   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
647
648   source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
649       sizeof (GstPoolSource));
650   source->pool = g_object_ref (pool);
651
652   return (GSource *) source;
653 }