rtsp-session-pool.c 19.8 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* GStreamer
 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
16 17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Wim Taymans's avatar
Wim Taymans committed
18
 */
Wim Taymans's avatar
Wim Taymans committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/**
 * SECTION:rtsp-session-pool
 * @short_description: An object for managing sessions
 * @see_also: #GstRTSPSession
 *
 * The #GstRTSPSessionPool object manages a list of #GstRTSPSession objects.
 *
 * The maximum number of sessions can be configured with
 * gst_rtsp_session_pool_set_max_sessions(). The current number of sessions can
 * be retrieved with gst_rtsp_session_pool_get_n_sessions().
 *
 * Use gst_rtsp_session_pool_create() to create a new #GstRTSPSession object.
 * The session object can be found again with its id and
 * gst_rtsp_session_pool_find().
 *
 * All sessions can be iterated with gst_rtsp_session_pool_filter().
 *
 * Run gst_rtsp_session_pool_cleanup() periodically to remove timed out sessions
 * or use gst_rtsp_session_pool_create_watch() to be notified when session
 * cleanup should be performed.
 *
 * Last reviewed on 2013-07-11 (1.0.0)
 */
Wim Taymans's avatar
Wim Taymans committed
42 43 44

#include "rtsp-session-pool.h"

45 46 47 48 49
#define GST_RTSP_SESSION_POOL_GET_PRIVATE(obj)  \
         (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SESSION_POOL, GstRTSPSessionPoolPrivate))

struct _GstRTSPSessionPoolPrivate
{
50
  GMutex lock;                  /* protects everything in this struct */
51 52
  guint max_sessions;
  GHashTable *sessions;
53
  guint sessions_cookie;
54 55
};

56 57 58 59 60 61 62 63 64
#define DEFAULT_MAX_SESSIONS 0

enum
{
  PROP_0,
  PROP_MAX_SESSIONS,
  PROP_LAST
};

65 66 67 68 69
static const gchar session_id_charset[] =
    { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
  'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7',
70
  '8', '9', '-', '_', '.', '+'  /* '$' Live555 in VLC strips off $ chars */
71 72
};

73 74 75 76 77 78 79 80
enum
{
  SIGNAL_SESSION_REMOVED,
  SIGNAL_LAST
};

static guint gst_rtsp_session_pool_signals[SIGNAL_LAST] = { 0 };

81
GST_DEBUG_CATEGORY_STATIC (rtsp_session_debug);
82 83
#define GST_CAT_DEFAULT rtsp_session_debug

Edward Hervey's avatar
Edward Hervey committed
84 85 86 87
static void gst_rtsp_session_pool_get_property (GObject * object, guint propid,
    GValue * value, GParamSpec * pspec);
static void gst_rtsp_session_pool_set_property (GObject * object, guint propid,
    const GValue * value, GParamSpec * pspec);
88 89
static void gst_rtsp_session_pool_finalize (GObject * object);

Edward Hervey's avatar
Edward Hervey committed
90
static gchar *create_session_id (GstRTSPSessionPool * pool);
91 92
static GstRTSPSession *create_session (GstRTSPSessionPool * pool,
    const gchar * id);
93

Wim Taymans's avatar
Wim Taymans committed
94 95 96 97 98 99 100
G_DEFINE_TYPE (GstRTSPSessionPool, gst_rtsp_session_pool, G_TYPE_OBJECT);

static void
gst_rtsp_session_pool_class_init (GstRTSPSessionPoolClass * klass)
{
  GObjectClass *gobject_class;

101 102
  g_type_class_add_private (klass, sizeof (GstRTSPSessionPoolPrivate));

Wim Taymans's avatar
Wim Taymans committed
103
  gobject_class = G_OBJECT_CLASS (klass);
104

105 106
  gobject_class->get_property = gst_rtsp_session_pool_get_property;
  gobject_class->set_property = gst_rtsp_session_pool_set_property;
107
  gobject_class->finalize = gst_rtsp_session_pool_finalize;
108

109 110 111 112 113 114
  g_object_class_install_property (gobject_class, PROP_MAX_SESSIONS,
      g_param_spec_uint ("max-sessions", "Max Sessions",
          "the maximum amount of sessions (0 = unlimited)",
          0, G_MAXUINT, DEFAULT_MAX_SESSIONS,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

115 116 117 118 119 120
  gst_rtsp_session_pool_signals[SIGNAL_SESSION_REMOVED] =
      g_signal_new ("session-removed", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPSessionPoolClass,
          session_removed), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
      1, GST_TYPE_RTSP_SESSION);

121
  klass->create_session_id = create_session_id;
122
  klass->create_session = create_session;
123

124 125
  GST_DEBUG_CATEGORY_INIT (rtsp_session_debug, "rtspsessionpool", 0,
      "GstRTSPSessionPool");
Wim Taymans's avatar
Wim Taymans committed
126 127 128 129 130
}

static void
gst_rtsp_session_pool_init (GstRTSPSessionPool * pool)
{
131 132 133 134 135 136
  GstRTSPSessionPoolPrivate *priv = GST_RTSP_SESSION_POOL_GET_PRIVATE (pool);

  pool->priv = priv;

  g_mutex_init (&priv->lock);
  priv->sessions = g_hash_table_new_full (g_str_hash, g_str_equal,
Edward Hervey's avatar
Edward Hervey committed
137
      NULL, g_object_unref);
138
  priv->max_sessions = DEFAULT_MAX_SESSIONS;
139 140
}

141 142 143 144 145 146 147
static GstRTSPFilterResult
remove_sessions_func (GstRTSPSessionPool * pool, GstRTSPSession * session,
    gpointer user_data)
{
  return GST_RTSP_FILTER_REMOVE;
}

148 149 150
static void
gst_rtsp_session_pool_finalize (GObject * object)
{
Edward Hervey's avatar
Edward Hervey committed
151
  GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);
152
  GstRTSPSessionPoolPrivate *priv = pool->priv;
153

154
  gst_rtsp_session_pool_filter (pool, remove_sessions_func, NULL);
155
  g_hash_table_unref (priv->sessions);
156
  g_mutex_clear (&priv->lock);
Edward Hervey's avatar
Edward Hervey committed
157

158
  G_OBJECT_CLASS (gst_rtsp_session_pool_parent_class)->finalize (object);
Wim Taymans's avatar
Wim Taymans committed
159 160
}

161
static void
Edward Hervey's avatar
Edward Hervey committed
162 163
gst_rtsp_session_pool_get_property (GObject * object, guint propid,
    GValue * value, GParamSpec * pspec)
164 165 166 167 168
{
  GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);

  switch (propid) {
    case PROP_MAX_SESSIONS:
169
      g_value_set_uint (value, gst_rtsp_session_pool_get_max_sessions (pool));
170 171 172 173 174 175 176 177
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
      break;
  }
}

static void
Edward Hervey's avatar
Edward Hervey committed
178 179
gst_rtsp_session_pool_set_property (GObject * object, guint propid,
    const GValue * value, GParamSpec * pspec)
180 181 182 183 184
{
  GstRTSPSessionPool *pool = GST_RTSP_SESSION_POOL (object);

  switch (propid) {
    case PROP_MAX_SESSIONS:
185
      gst_rtsp_session_pool_set_max_sessions (pool, g_value_get_uint (value));
186 187 188 189 190 191 192
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
      break;
  }
}

Wim Taymans's avatar
Wim Taymans committed
193 194 195 196
/**
 * gst_rtsp_session_pool_new:
 *
 * Create a new #GstRTSPSessionPool instance.
197
 *
198 199
 * Returns: (transfer full): A new #GstRTSPSessionPool. g_object_unref() after
 * usage.
Wim Taymans's avatar
Wim Taymans committed
200 201 202 203 204 205 206 207 208 209 210
 */
GstRTSPSessionPool *
gst_rtsp_session_pool_new (void)
{
  GstRTSPSessionPool *result;

  result = g_object_new (GST_TYPE_RTSP_SESSION_POOL, NULL);

  return result;
}

211 212 213 214 215 216 217 218 219
/**
 * gst_rtsp_session_pool_set_max_sessions:
 * @pool: a #GstRTSPSessionPool
 * @max: the maximum number of sessions
 *
 * Configure the maximum allowed number of sessions in @pool to @max.
 * A value of 0 means an unlimited amount of sessions.
 */
void
Edward Hervey's avatar
Edward Hervey committed
220
gst_rtsp_session_pool_set_max_sessions (GstRTSPSessionPool * pool, guint max)
221
{
222 223
  GstRTSPSessionPoolPrivate *priv;

224 225
  g_return_if_fail (GST_IS_RTSP_SESSION_POOL (pool));

226 227 228 229 230
  priv = pool->priv;

  g_mutex_lock (&priv->lock);
  priv->max_sessions = max;
  g_mutex_unlock (&priv->lock);
231 232 233 234 235 236 237 238 239 240 241 242
}

/**
 * gst_rtsp_session_pool_get_max_sessions:
 * @pool: a #GstRTSPSessionPool
 *
 * Get the maximum allowed number of sessions in @pool. 0 means an unlimited
 * amount of sessions.
 *
 * Returns: the maximum allowed number of sessions.
 */
guint
Edward Hervey's avatar
Edward Hervey committed
243
gst_rtsp_session_pool_get_max_sessions (GstRTSPSessionPool * pool)
244
{
245
  GstRTSPSessionPoolPrivate *priv;
246 247 248 249
  guint result;

  g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);

250 251 252 253 254
  priv = pool->priv;

  g_mutex_lock (&priv->lock);
  result = priv->max_sessions;
  g_mutex_unlock (&priv->lock);
255 256 257 258 259 260 261 262 263 264 265 266 267

  return result;
}

/**
 * gst_rtsp_session_pool_get_n_sessions:
 * @pool: a #GstRTSPSessionPool
 *
 * Get the amount of active sessions in @pool.
 *
 * Returns: the amount of active sessions in @pool.
 */
guint
Edward Hervey's avatar
Edward Hervey committed
268
gst_rtsp_session_pool_get_n_sessions (GstRTSPSessionPool * pool)
269
{
270
  GstRTSPSessionPoolPrivate *priv;
271 272 273 274
  guint result;

  g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);

275 276 277 278 279
  priv = pool->priv;

  g_mutex_lock (&priv->lock);
  result = g_hash_table_size (priv->sessions);
  g_mutex_unlock (&priv->lock);
280 281 282 283

  return result;
}

Wim Taymans's avatar
Wim Taymans committed
284 285 286 287 288
/**
 * gst_rtsp_session_pool_find:
 * @pool: the pool to search
 * @sessionid: the session id
 *
289 290
 * Find the session with @sessionid in @pool. The access time of the session
 * will be updated with gst_rtsp_session_touch().
Wim Taymans's avatar
Wim Taymans committed
291
 *
292 293
 * Returns: (transfer full) (nullable): the #GstRTSPSession with @sessionid
 * or %NULL when the session did not exist. g_object_unref() after usage.
Wim Taymans's avatar
Wim Taymans committed
294 295
 */
GstRTSPSession *
Edward Hervey's avatar
Edward Hervey committed
296
gst_rtsp_session_pool_find (GstRTSPSessionPool * pool, const gchar * sessionid)
Wim Taymans's avatar
Wim Taymans committed
297
{
298
  GstRTSPSessionPoolPrivate *priv;
Wim Taymans's avatar
Wim Taymans committed
299 300 301 302 303
  GstRTSPSession *result;

  g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);
  g_return_val_if_fail (sessionid != NULL, NULL);

304 305 306 307
  priv = pool->priv;

  g_mutex_lock (&priv->lock);
  result = g_hash_table_lookup (priv->sessions, sessionid);
308
  if (result) {
Wim Taymans's avatar
Wim Taymans committed
309
    g_object_ref (result);
310
    gst_rtsp_session_touch (result);
311
  }
312
  g_mutex_unlock (&priv->lock);
313

Wim Taymans's avatar
Wim Taymans committed
314 315 316 317
  return result;
}

static gchar *
Edward Hervey's avatar
Edward Hervey committed
318
create_session_id (GstRTSPSessionPool * pool)
Wim Taymans's avatar
Wim Taymans committed
319
{
320
  gchar id[16];
Wim Taymans's avatar
Wim Taymans committed
321 322 323
  gint i;

  for (i = 0; i < 16; i++) {
324 325 326
    id[i] =
        session_id_charset[g_random_int_range (0,
            G_N_ELEMENTS (session_id_charset))];
Wim Taymans's avatar
Wim Taymans committed
327 328
  }

329
  return g_strndup (id, 16);
Wim Taymans's avatar
Wim Taymans committed
330 331
}

332 333 334 335 336 337
static GstRTSPSession *
create_session (GstRTSPSessionPool * pool, const gchar * id)
{
  return gst_rtsp_session_new (id);
}

Wim Taymans's avatar
Wim Taymans committed
338 339 340 341 342 343
/**
 * gst_rtsp_session_pool_create:
 * @pool: a #GstRTSPSessionPool
 *
 * Create a new #GstRTSPSession object in @pool.
 *
344
 * Returns: (transfer full): a new #GstRTSPSession.
Wim Taymans's avatar
Wim Taymans committed
345 346
 */
GstRTSPSession *
Edward Hervey's avatar
Edward Hervey committed
347
gst_rtsp_session_pool_create (GstRTSPSessionPool * pool)
Wim Taymans's avatar
Wim Taymans committed
348
{
349
  GstRTSPSessionPoolPrivate *priv;
Wim Taymans's avatar
Wim Taymans committed
350
  GstRTSPSession *result = NULL;
351 352
  GstRTSPSessionPoolClass *klass;
  gchar *id = NULL;
353
  guint retry;
Wim Taymans's avatar
Wim Taymans committed
354 355 356

  g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);

357 358
  priv = pool->priv;

359 360
  klass = GST_RTSP_SESSION_POOL_GET_CLASS (pool);

361
  retry = 0;
Wim Taymans's avatar
Wim Taymans committed
362 363 364
  do {
    /* start by creating a new random session id, we assume that this is random
     * enough to not cause a collision, which we will check later  */
365 366 367 368 369 370 371
    if (klass->create_session_id)
      id = klass->create_session_id (pool);
    else
      goto no_function;

    if (id == NULL)
      goto no_session;
Wim Taymans's avatar
Wim Taymans committed
372

373
    g_mutex_lock (&priv->lock);
374
    /* check session limit */
375 376
    if (priv->max_sessions > 0) {
      if (g_hash_table_size (priv->sessions) >= priv->max_sessions)
Edward Hervey's avatar
Edward Hervey committed
377
        goto too_many_sessions;
378
    }
Wim Taymans's avatar
Wim Taymans committed
379
    /* check if the sessionid existed */
380
    result = g_hash_table_lookup (priv->sessions, id);
Wim Taymans's avatar
Wim Taymans committed
381
    if (result) {
382
      /* found, retry with a different session id */
Wim Taymans's avatar
Wim Taymans committed
383
      result = NULL;
384 385
      retry++;
      if (retry > 100)
Edward Hervey's avatar
Edward Hervey committed
386 387
        goto collision;
    } else {
Wim Taymans's avatar
Wim Taymans committed
388
      /* not found, create session and insert it in the pool */
389 390 391 392
      if (klass->create_session)
        result = create_session (pool, id);
      if (result == NULL)
        goto too_many_sessions;
Wim Taymans's avatar
Wim Taymans committed
393 394
      /* take additional ref for the pool */
      g_object_ref (result);
395 396
      g_hash_table_insert (priv->sessions,
          (gchar *) gst_rtsp_session_get_sessionid (result), result);
397
      priv->sessions_cookie++;
Wim Taymans's avatar
Wim Taymans committed
398
    }
399
    g_mutex_unlock (&priv->lock);
Wim Taymans's avatar
Wim Taymans committed
400 401 402 403 404

    g_free (id);
  } while (result == NULL);

  return result;
405 406 407 408

  /* ERRORS */
no_function:
  {
409
    GST_WARNING ("no create_session_id vmethod in GstRTSPSessionPool %p", pool);
410 411 412 413
    return NULL;
  }
no_session:
  {
414
    GST_WARNING ("can't create session id with GstRTSPSessionPool %p", pool);
415 416
    return NULL;
  }
417 418
collision:
  {
419
    GST_WARNING ("can't find unique sessionid for GstRTSPSessionPool %p", pool);
420
    g_mutex_unlock (&priv->lock);
421 422 423 424 425
    g_free (id);
    return NULL;
  }
too_many_sessions:
  {
426 427
    GST_WARNING ("session pool reached max sessions of %d", priv->max_sessions);
    g_mutex_unlock (&priv->lock);
428 429 430
    g_free (id);
    return NULL;
  }
Wim Taymans's avatar
Wim Taymans committed
431 432 433 434 435
}

/**
 * gst_rtsp_session_pool_remove:
 * @pool: a #GstRTSPSessionPool
436
 * @sess: (transfer none): a #GstRTSPSession
Wim Taymans's avatar
Wim Taymans committed
437
 *
438
 * Remove @sess from @pool, releasing the ref that the pool has on @sess.
Wim Taymans's avatar
Wim Taymans committed
439
 *
440
 * Returns: %TRUE if the session was found and removed.
Wim Taymans's avatar
Wim Taymans committed
441
 */
442
gboolean
Edward Hervey's avatar
Edward Hervey committed
443
gst_rtsp_session_pool_remove (GstRTSPSessionPool * pool, GstRTSPSession * sess)
Wim Taymans's avatar
Wim Taymans committed
444
{
445
  GstRTSPSessionPoolPrivate *priv;
Wim Taymans's avatar
Wim Taymans committed
446 447
  gboolean found;

448 449
  g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), FALSE);
  g_return_val_if_fail (GST_IS_RTSP_SESSION (sess), FALSE);
Wim Taymans's avatar
Wim Taymans committed
450

451 452 453
  priv = pool->priv;

  g_mutex_lock (&priv->lock);
454
  g_object_ref (sess);
455 456 457
  found =
      g_hash_table_remove (priv->sessions,
      gst_rtsp_session_get_sessionid (sess));
458
  if (found)
459
    priv->sessions_cookie++;
460 461 462
  g_mutex_unlock (&priv->lock);

  if (found)
463 464
    g_signal_emit (pool, gst_rtsp_session_pool_signals[SIGNAL_SESSION_REMOVED],
        0, sess);
465

466
  g_object_unref (sess);
467 468

  return found;
Wim Taymans's avatar
Wim Taymans committed
469 470
}

471 472
typedef struct
{
473
  gint64 now_monotonic_time;
474
  GstRTSPSessionPool *pool;
475
  GList *removed;
476 477
} CleanupData;

Wim Taymans's avatar
Wim Taymans committed
478
static gboolean
479
cleanup_func (gchar * sessionid, GstRTSPSession * sess, CleanupData * data)
Wim Taymans's avatar
Wim Taymans committed
480
{
481 482
  gboolean expired;

483 484
  expired = gst_rtsp_session_is_expired_usec (sess, data->now_monotonic_time);

485
  if (expired) {
486 487
    GST_DEBUG ("session expired");
    data->removed = g_list_prepend (data->removed, g_object_ref (sess));
488
  }
489

490
  return expired;
Wim Taymans's avatar
Wim Taymans committed
491 492 493 494 495 496 497 498 499 500 501 502
}

/**
 * gst_rtsp_session_pool_cleanup:
 * @pool: a #GstRTSPSessionPool
 *
 * Inspect all the sessions in @pool and remove the sessions that are inactive
 * for more than their timeout.
 *
 * Returns: the amount of sessions that got removed.
 */
guint
Edward Hervey's avatar
Edward Hervey committed
503
gst_rtsp_session_pool_cleanup (GstRTSPSessionPool * pool)
Wim Taymans's avatar
Wim Taymans committed
504
{
505
  GstRTSPSessionPoolPrivate *priv;
Wim Taymans's avatar
Wim Taymans committed
506
  guint result;
507
  CleanupData data;
508
  GList *walk;
Wim Taymans's avatar
Wim Taymans committed
509 510 511

  g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), 0);

512 513
  priv = pool->priv;

514 515
  data.now_monotonic_time = g_get_monotonic_time ();

516
  data.pool = pool;
517
  data.removed = NULL;
518

519
  g_mutex_lock (&priv->lock);
Edward Hervey's avatar
Edward Hervey committed
520
  result =
521
      g_hash_table_foreach_remove (priv->sessions, (GHRFunc) cleanup_func,
522
      &data);
523 524
  if (result > 0)
    priv->sessions_cookie++;
525
  g_mutex_unlock (&priv->lock);
Wim Taymans's avatar
Wim Taymans committed
526

527 528 529 530 531 532 533 534 535 536
  for (walk = data.removed; walk; walk = walk->next) {
    GstRTSPSession *sess = walk->data;

    g_signal_emit (pool,
        gst_rtsp_session_pool_signals[SIGNAL_SESSION_REMOVED], 0, sess);

    g_object_unref (sess);
  }
  g_list_free (data.removed);

Wim Taymans's avatar
Wim Taymans committed
537 538
  return result;
}
539

540 541 542
/**
 * gst_rtsp_session_pool_filter:
 * @pool: a #GstRTSPSessionPool
543
 * @func: (scope call) (allow-none): a callback
544
 * @user_data: (closure): user data passed to @func
545 546 547 548 549
 *
 * Call @func for each session in @pool. The result value of @func determines
 * what happens to the session. @func will be called with the session pool
 * locked so no further actions on @pool can be performed from @func.
 *
550 551
 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be set to the
 * expired state with gst_rtsp_session_set_expired() and removed from
552 553 554 555 556 557 558 559
 * @pool.
 *
 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @pool.
 *
 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @pool but
 * will also be added with an additional ref to the result GList of this
 * function..
 *
560 561
 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for all sessions.
 *
562 563 564
 * Returns: (element-type GstRTSPSession) (transfer full): a GList with all
 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
 * element in the GList should be unreffed before the list is freed.
565 566
 */
GList *
Edward Hervey's avatar
Edward Hervey committed
567
gst_rtsp_session_pool_filter (GstRTSPSessionPool * pool,
568
    GstRTSPSessionPoolFilterFunc func, gpointer user_data)
569
{
570
  GstRTSPSessionPoolPrivate *priv;
571 572 573 574 575
  GHashTableIter iter;
  gpointer key, value;
  GList *result;
  GHashTable *visited;
  guint cookie;
576 577 578

  g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);

579 580
  priv = pool->priv;

581 582 583
  result = NULL;
  if (func)
    visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
584

585
  g_mutex_lock (&priv->lock);
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
restart:
  g_hash_table_iter_init (&iter, priv->sessions);
  cookie = priv->sessions_cookie;
  while (g_hash_table_iter_next (&iter, &key, &value)) {
    GstRTSPSession *session = value;
    GstRTSPFilterResult res;
    gboolean changed;

    if (func) {
      /* only visit each session once */
      if (g_hash_table_contains (visited, session))
        continue;

      g_hash_table_add (visited, g_object_ref (session));
      g_mutex_unlock (&priv->lock);

      res = func (pool, session, user_data);

      g_mutex_lock (&priv->lock);
    } else
      res = GST_RTSP_FILTER_REF;

    changed = (cookie != priv->sessions_cookie);

    switch (res) {
      case GST_RTSP_FILTER_REMOVE:
612 613
      {
        gboolean removed = TRUE;
614 615

        if (changed)
616 617
          /* something changed, check if we still have the session */
          removed = g_hash_table_remove (priv->sessions, key);
618 619
        else
          g_hash_table_iter_remove (&iter);
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634

        if (removed) {
          /* if we managed to remove the session, update the cookie and
           * signal */
          cookie = ++priv->sessions_cookie;
          g_mutex_unlock (&priv->lock);

          g_signal_emit (pool,
              gst_rtsp_session_pool_signals[SIGNAL_SESSION_REMOVED], 0,
              session);

          g_mutex_lock (&priv->lock);
          /* cookie could have changed again, make sure we restart */
          changed |= (cookie != priv->sessions_cookie);
        }
635
        break;
636
      }
637 638 639 640 641 642 643 644 645 646 647
      case GST_RTSP_FILTER_REF:
        /* keep ref */
        result = g_list_prepend (result, g_object_ref (session));
        break;
      case GST_RTSP_FILTER_KEEP:
      default:
        break;
    }
    if (changed)
      goto restart;
  }
648
  g_mutex_unlock (&priv->lock);
649

650 651 652 653
  if (func)
    g_hash_table_unref (visited);

  return result;
654 655
}

656 657 658 659 660 661 662 663
typedef struct
{
  GSource source;
  GstRTSPSessionPool *pool;
  gint timeout;
} GstPoolSource;

static void
Edward Hervey's avatar
Edward Hervey committed
664
collect_timeout (gchar * sessionid, GstRTSPSession * sess, GstPoolSource * psrc)
665 666
{
  gint timeout;
667 668 669
  gint64 now_monotonic_time;

  now_monotonic_time = g_get_monotonic_time ();
670

671
  timeout = gst_rtsp_session_next_timeout_usec (sess, now_monotonic_time);
672

673
  GST_INFO ("%p: next timeout: %d", sess, timeout);
674 675 676 677 678 679 680
  if (psrc->timeout == -1 || timeout < psrc->timeout)
    psrc->timeout = timeout;
}

static gboolean
gst_pool_source_prepare (GSource * source, gint * timeout)
{
681
  GstRTSPSessionPoolPrivate *priv;
682 683 684 685 686
  GstPoolSource *psrc;
  gboolean result;

  psrc = (GstPoolSource *) source;
  psrc->timeout = -1;
687
  priv = psrc->pool->priv;
688

689 690 691
  g_mutex_lock (&priv->lock);
  g_hash_table_foreach (priv->sessions, (GHFunc) collect_timeout, psrc);
  g_mutex_unlock (&priv->lock);
692 693 694 695 696 697

  if (timeout)
    *timeout = psrc->timeout;

  result = psrc->timeout == 0;

698
  GST_INFO ("prepare %d, %d", psrc->timeout, result);
699 700 701 702 703 704 705

  return result;
}

static gboolean
gst_pool_source_check (GSource * source)
{
706
  GST_INFO ("check");
707 708 709 710 711 712 713 714 715 716 717 718

  return gst_pool_source_prepare (source, NULL);
}

static gboolean
gst_pool_source_dispatch (GSource * source, GSourceFunc callback,
    gpointer user_data)
{
  gboolean res;
  GstPoolSource *psrc = (GstPoolSource *) source;
  GstRTSPSessionPoolFunc func = (GstRTSPSessionPoolFunc) callback;

719
  GST_INFO ("dispatch");
720 721 722 723 724 725 726 727 728 729 730 731 732 733

  if (func)
    res = func (psrc->pool, user_data);
  else
    res = FALSE;

  return res;
}

static void
gst_pool_source_finalize (GSource * source)
{
  GstPoolSource *psrc = (GstPoolSource *) source;

734
  GST_INFO ("finalize %p", psrc);
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750

  g_object_unref (psrc->pool);
  psrc->pool = NULL;
}

static GSourceFuncs gst_pool_source_funcs = {
  gst_pool_source_prepare,
  gst_pool_source_check,
  gst_pool_source_dispatch,
  gst_pool_source_finalize
};

/**
 * gst_rtsp_session_pool_create_watch:
 * @pool: a #GstRTSPSessionPool
 *
751 752 753 754
 * Create a #GSource that will be dispatched when the session should be cleaned
 * up.
 *
 * Returns: (transfer full): a #GSource
755 756
 */
GSource *
Edward Hervey's avatar
Edward Hervey committed
757
gst_rtsp_session_pool_create_watch (GstRTSPSessionPool * pool)
758 759 760 761 762 763 764 765 766 767 768
{
  GstPoolSource *source;

  g_return_val_if_fail (GST_IS_RTSP_SESSION_POOL (pool), NULL);

  source = (GstPoolSource *) g_source_new (&gst_pool_source_funcs,
      sizeof (GstPoolSource));
  source->pool = g_object_ref (pool);

  return (GSource *) source;
}