rtsp-server: sprinkle some allow-none annotations for g-i
[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[17];
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   id[16] = 0;
305
306   return g_uri_escape_string (id, NULL, FALSE);
307 }
308
309 static GstRTSPSession *
310 create_session (GstRTSPSessionPool * pool, const gchar * id)
311 {
312   return gst_rtsp_session_new (id);
313 }
314
315 /**
316  * gst_rtsp_session_pool_create:
317  * @pool: a #GstRTSPSessionPool
318  *
319  * Create a new #GstRTSPSession object in @pool.
320  *
321  * Returns: (transfer none): a new #GstRTSPSession.
322  */
323 GstRTSPSession *
324 gst_rtsp_session_pool_create (GstRTSPSessionPool * pool)
325 {
326   GstRTSPSessionPoolPrivate *priv;
327   GstRTSPSession *result = NULL;
328   GstRTSPSessionPoolClass *klass;
329   gchar *id = NULL;
330   guint retry;
331
332   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
333
334   priv = pool->priv;
335
336   klass = GST_RTSP_SESSION_POOL_GET_CLASS (pool);
337
338   retry = 0;
339   do {
340     /* start by creating a new random session id, we assume that this is random
341      * enough to not cause a collision, which we will check later  */
342     if (klass->create_session_id)
343       id = klass->create_session_id (pool);
344     else
345       goto no_function;
346
347     if (id == NULL)
348       goto no_session;
349
350     g_mutex_lock (&priv->lock);
351     /* check session limit */
352     if (priv->max_sessions > 0) {
353       if (g_hash_table_size (priv->sessions) >= priv->max_sessions)
354         goto too_many_sessions;
355     }
356     /* check if the sessionid existed */
357     result = g_hash_table_lookup (priv->sessions, id);
358     if (result) {
359       /* found, retry with a different session id */
360       result = NULL;
361       retry++;
362       if (retry > 100)
363         goto collision;
364     } else {
365       /* not found, create session and insert it in the pool */
366       if (klass->create_session)
367         result = create_session (pool, id);
368       if (result == NULL)
369         goto too_many_sessions;
370       /* take additional ref for the pool */
371       g_object_ref (result);
372       g_hash_table_insert (priv->sessions,
373           (gchar *) gst_rtsp_session_get_sessionid (result), result);
374     }
375     g_mutex_unlock (&priv->lock);
376
377     g_free (id);
378   } while (result == NULL);
379
380   return result;
381
382   /* ERRORS */
383 no_function:
384   {
385     GST_WARNING ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
386     return NULL;
387   }
388 no_session:
389   {
390     GST_WARNING ("can't create session id with GstRTSPSessionPool %p", pool);
391     return NULL;
392   }
393 collision:
394   {
395     GST_WARNING ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
396     g_mutex_unlock (&priv->lock);
397     g_free (id);
398     return NULL;
399   }
400 too_many_sessions:
401   {
402     GST_WARNING ("session pool reached max sessions of %d", priv->max_sessions);
403     g_mutex_unlock (&priv->lock);
404     g_free (id);
405     return NULL;
406   }
407 }
408
409 /**
410  * gst_rtsp_session_pool_remove:
411  * @pool: a #GstRTSPSessionPool
412  * @sess: a #GstRTSPSession
413  *
414  * Remove @sess from @pool, releasing the ref that the pool has on @sess.
415  *
416  * Returns: %TRUE if the session was found and removed.
417  */
418 gboolean
419 gst_rtsp_session_pool_remove (GstRTSPSessionPool * pool, GstRTSPSession * sess)
420 {
421   GstRTSPSessionPoolPrivate *priv;
422   gboolean found;
423
424   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), FALSE);
425   g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
426
427   priv = pool->priv;
428
429   g_mutex_lock (&priv->lock);
430   found =
431       g_hash_table_remove (priv->sessions,
432       gst_rtsp_session_get_sessionid (sess));
433   g_mutex_unlock (&priv->lock);
434
435   return found;
436 }
437
438 static gboolean
439 cleanup_func (gchar * sessionid, GstRTSPSession * sess, GTimeVal * now)
440 {
441   return gst_rtsp_session_is_expired (sess, now);
442 }
443
444 /**
445  * gst_rtsp_session_pool_cleanup:
446  * @pool: a #GstRTSPSessionPool
447  *
448  * Inspect all the sessions in @pool and remove the sessions that are inactive
449  * for more than their timeout.
450  *
451  * Returns: the amount of sessions that got removed.
452  */
453 guint
454 gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
455 {
456   GstRTSPSessionPoolPrivate *priv;
457   guint result;
458   GTimeVal now;
459
460   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);
461
462   priv = pool->priv;
463
464   g_get_current_time (&now);
465
466   g_mutex_lock (&priv->lock);
467   result =
468       g_hash_table_foreach_remove (priv->sessions, (GHRFunc) cleanup_func,
469       &now);
470   g_mutex_unlock (&priv->lock);
471
472   return result;
473 }
474
475 typedef struct
476 {
477   GstRTSPSessionPool *pool;
478   GstRTSPSessionPoolFilterFunc func;
479   gpointer user_data;
480   GList *list;
481 } FilterData;
482
483 static gboolean
484 filter_func (gchar * sessionid, GstRTSPSession * sess, FilterData * data)
485 {
486   GstRTSPFilterResult res;
487
488   if (data->func)
489     res = data->func (data->pool, sess, data->user_data);
490   else
491     res = GST_RTSP_FILTER_REF;
492
493   switch (res) {
494     case GST_RTSP_FILTER_REMOVE:
495       return TRUE;
496     case GST_RTSP_FILTER_REF:
497       /* keep ref */
498       data->list = g_list_prepend (data->list, g_object_ref (sess));
499       /* fallthrough */
500     default:
501     case GST_RTSP_FILTER_KEEP:
502       return FALSE;
503   }
504 }
505
506 /**
507  * gst_rtsp_session_pool_filter:
508  * @pool: a #GstRTSPSessionPool
509  * @func: (scope call) (allow-none): a callback
510  * @user_data: user data passed to @func
511  *
512  * Call @func for each session in @pool. The result value of @func determines
513  * what happens to the session. @func will be called with the session pool
514  * locked so no further actions on @pool can be performed from @func.
515  *
516  * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
517  * @pool.
518  *
519  * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
520  *
521  * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
522  * will also be added with an additional ref to the result GList of this
523  * function..
524  *
525  * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for all sessions.
526  *
527  * Returns: (element-type GstRTSPSession) (transfer full): a GList with all
528  * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
529  * element in the GList should be unreffed before the list is freed.
530  */
531 GList *
532 gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
533     GstRTSPSessionPoolFilterFunc func, gpointer user_data)
534 {
535   GstRTSPSessionPoolPrivate *priv;
536   FilterData data;
537
538   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
539
540   priv = pool->priv;
541
542   data.pool = pool;
543   data.func = func;
544   data.user_data = user_data;
545   data.list = NULL;
546
547   g_mutex_lock (&priv->lock);
548   g_hash_table_foreach_remove (priv->sessions, (GHRFunc) filter_func, &data);
549   g_mutex_unlock (&priv->lock);
550
551   return data.list;
552 }
553
554 typedef struct
555 {
556   GSource source;
557   GstRTSPSessionPool *pool;
558   gint timeout;
559 } GstPoolSource;
560
561 static void
562 collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
563 {
564   gint timeout;
565   GTimeVal now;
566
567   g_get_current_time (&now);
568
569   timeout = gst_rtsp_session_next_timeout (sess, &now);
570   GST_INFO ("%p: next timeout: %d", sess, timeout);
571   if (psrc->timeout == -1 || timeout < psrc->timeout)
572     psrc->timeout = timeout;
573 }
574
575 static gboolean
576 gst_pool_source_prepare (GSource * source, gint * timeout)
577 {
578   GstRTSPSessionPoolPrivate *priv;
579   GstPoolSource *psrc;
580   gboolean result;
581
582   psrc = (GstPoolSource *) source;
583   psrc->timeout = -1;
584   priv = psrc->pool->priv;
585
586   g_mutex_lock (&priv->lock);
587   g_hash_table_foreach (priv->sessions, (GHFunc) collect_timeout, psrc);
588   g_mutex_unlock (&priv->lock);
589
590   if (timeout)
591     *timeout = psrc->timeout;
592
593   result = psrc->timeout == 0;
594
595   GST_INFO ("prepare %d, %d", psrc->timeout, result);
596
597   return result;
598 }
599
600 static gboolean
601 gst_pool_source_check (GSource * source)
602 {
603   GST_INFO ("check");
604
605   return gst_pool_source_prepare (source, NULL);
606 }
607
608 static gboolean
609 gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
610     gpointer user_data)
611 {
612   gboolean res;
613   GstPoolSource *psrc = (GstPoolSource *) source;
614   GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;
615
616   GST_INFO ("dispatch");
617
618   if (func)
619     res = func (psrc->pool, user_data);
620   else
621     res = FALSE;
622
623   return res;
624 }
625
626 static void
627 gst_pool_source_finalize (GSource * source)
628 {
629   GstPoolSource *psrc = (GstPoolSource *) source;
630
631   GST_INFO ("finalize %p", psrc);
632
633   g_object_unref (psrc->pool);
634   psrc->pool = NULL;
635 }
636
637 static GSourceFuncs gst_pool_source_funcs = {
638   gst_pool_source_prepare,
639   gst_pool_source_check,
640   gst_pool_source_dispatch,
641   gst_pool_source_finalize
642 };
643
644 /**
645  * gst_rtsp_session_pool_create_watch:
646  * @pool: a #GstRTSPSessionPool
647  *
648  * A GSource that will be dispatched when the session should be cleaned up.
649  */
650 GSource *
651 gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
652 {
653   GstPoolSource *source;
654
655   g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
656
657   source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
658       sizeof (GstPoolSource));
659   source->pool = g_object_ref (pool);
660
661   return (GSource *) source;
662 }