gstcollectpads2.c 61 KB
Newer Older
Sebastian Dröge's avatar
Sebastian Dröge committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/* GStreamer
 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
 * Copyright (C) 2008 Mark Nauwelaerts <mnauw@users.sourceforge.net>
 * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
 *
 * gstcollectpads2.c:
 *
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
/**
 * SECTION:gstcollectpads2
 * @short_description: manages a set of pads that operate in collect mode
 * @see_also:
 *
 * Manages a set of pads that operate in collect mode. This means that control
 * is given to the manager of this object when all pads have data.
 * <itemizedlist>
 *   <listitem><para>
 *     Collectpads are created with gst_collect_pads2_new(). A callback should then
 *     be installed with gst_collect_pads2_set_function ().
 *   </para></listitem>
 *   <listitem><para>
 *     Pads are added to the collection with gst_collect_pads2_add_pad()/
 *     gst_collect_pads2_remove_pad(). The pad
 *     has to be a sinkpad. The chain and event functions of the pad are
 *     overridden. The element_private of the pad is used to store
 *     private information for the collectpads.
 *   </para></listitem>
 *   <listitem><para>
 *     For each pad, data is queued in the _chain function or by
 *     performing a pull_range.
 *   </para></listitem>
 *   <listitem><para>
 *     When data is queued on all pads in waiting mode, the callback function is called.
 *   </para></listitem>
 *   <listitem><para>
 *     Data can be dequeued from the pad with the gst_collect_pads2_pop() method.
 *     One can peek at the data with the gst_collect_pads2_peek() function.
 *     These functions will return NULL if the pad received an EOS event. When all
 *     pads return NULL from a gst_collect_pads2_peek(), the element can emit an EOS
 *     event itself.
 *   </para></listitem>
 *   <listitem><para>
 *     Data can also be dequeued in byte units using the gst_collect_pads2_available(),
 *     gst_collect_pads2_read() and gst_collect_pads2_flush() calls.
 *   </para></listitem>
 *   <listitem><para>
 *     Elements should call gst_collect_pads2_start() and gst_collect_pads2_stop() in
 *     their state change functions to start and stop the processing of the collectpads.
 *     The gst_collect_pads2_stop() call should be called before calling the parent
 *     element state change function in the PAUSED_TO_READY state change to ensure
 *     no pad is blocked and the element can finish streaming.
 *   </para></listitem>
 *   <listitem><para>
 *     gst_collect_pads2_collect() and gst_collect_pads2_collect_range() can be used by
 *     elements that start a #GstTask to drive the collect_pads2. This feature is however
 *     not yet implemented.
 *   </para></listitem>
 *   <listitem><para>
 *     gst_collect_pads2_set_waiting() sets a pad to waiting or non-waiting mode.
 *     CollectPads element is not waiting for data to be collected on non-waiting pads.
 *     Thus these pads may but need not have data when the callback is called.
 *     All pads are in waiting mode by default.
 *   </para></listitem>
 * </itemizedlist>
 *
80
 * Last reviewed on 2011-10-28 (0.10.36)
Sebastian Dröge's avatar
Sebastian Dröge committed
81 82 83 84
 *
 * Since: 0.10.36
 */

85 86 87 88 89 90
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <gst/gst_private.h>

Sebastian Dröge's avatar
Sebastian Dröge committed
91 92
#include "gstcollectpads2.h"

93 94
#include "../../../gst/glib-compat-private.h"

Sebastian Dröge's avatar
Sebastian Dröge committed
95 96 97
GST_DEBUG_CATEGORY_STATIC (collect_pads2_debug);
#define GST_CAT_DEFAULT collect_pads2_debug

98 99
#define parent_class gst_collect_pads2_parent_class
G_DEFINE_TYPE (GstCollectPads2, gst_collect_pads2, GST_TYPE_OBJECT);
Sebastian Dröge's avatar
Sebastian Dröge committed
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
struct _GstCollectData2Private
{
  /* refcounting for struct, and destroy callback */
  GstCollectData2DestroyNotify destroy_notify;
  gint refcount;
};

struct _GstCollectPads2Private
{
  /* with LOCK and/or STREAM_LOCK */
  gboolean started;

  /* with STREAM_LOCK */
  guint32 cookie;               /* @data list cookie */
  guint numpads;                /* number of pads in @data */
  guint queuedpads;             /* number of pads with a buffer */
  guint eospads;                /* number of pads that are EOS */
  GstClockTime earliest_time;   /* Current earliest time */
  GstCollectData2 *earliest_data;       /* Pad data for current earliest time */

  /* with LOCK */
  GSList *pad_list;             /* updated pad list */
  guint32 pad_cookie;           /* updated cookie */

  GstCollectPads2Function func; /* function and user_data for callback */
  gpointer user_data;
  GstCollectPads2BufferFunction buffer_func;    /* function and user_data for buffer callback */
  gpointer buffer_user_data;
  GstCollectPads2CompareFunction compare_func;
  gpointer compare_user_data;
  GstCollectPads2EventFunction event_func;      /* function and data for event callback */
  gpointer event_user_data;
  GstCollectPads2ClipFunction clip_func;
  gpointer clip_user_data;

  /* no other lock needed */
137 138
  GMutex evt_lock;              /* these make up sort of poor man's event signaling */
  GCond evt_cond;
139 140 141
  guint32 evt_cookie;
};

Sebastian Dröge's avatar
Sebastian Dröge committed
142 143
static void gst_collect_pads2_clear (GstCollectPads2 * pads,
    GstCollectData2 * data);
144 145 146 147
static GstFlowReturn gst_collect_pads2_chain (GstPad * pad, GstObject * parent,
    GstBuffer * buffer);
static gboolean gst_collect_pads2_event (GstPad * pad, GstObject * parent,
    GstEvent * event);
Sebastian Dröge's avatar
Sebastian Dröge committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
static void gst_collect_pads2_finalize (GObject * object);
static GstFlowReturn gst_collect_pads2_default_collected (GstCollectPads2 *
    pads, gpointer user_data);
static gint gst_collect_pads2_default_compare_func (GstCollectPads2 * pads,
    GstCollectData2 * data1, GstClockTime timestamp1, GstCollectData2 * data2,
    GstClockTime timestamp2, gpointer user_data);
static gboolean gst_collect_pads2_recalculate_full (GstCollectPads2 * pads);
static void ref_data (GstCollectData2 * data);
static void unref_data (GstCollectData2 * data);

/* Some properties are protected by LOCK, others by STREAM_LOCK
 * However, manipulating either of these partitions may require
 * to signal/wake a _WAIT, so use a separate (sort of) event to prevent races
 * Alternative implementations are possible, e.g. some low-level re-implementing
 * of the 2 above locks to drop both of them atomically when going into _WAIT.
 */
164 165
#define GST_COLLECT_PADS2_GET_EVT_COND(pads) (&((GstCollectPads2 *)pads)->priv->evt_cond)
#define GST_COLLECT_PADS2_GET_EVT_LOCK(pads) (&((GstCollectPads2 *)pads)->priv->evt_lock)
Sebastian Dröge's avatar
Sebastian Dröge committed
166 167 168
#define GST_COLLECT_PADS2_EVT_WAIT(pads, cookie) G_STMT_START {    \
  g_mutex_lock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads));            \
  /* should work unless a lot of event'ing and thread starvation */\
169
  while (cookie == ((GstCollectPads2 *) pads)->priv->evt_cookie)         \
Sebastian Dröge's avatar
Sebastian Dröge committed
170 171
    g_cond_wait (GST_COLLECT_PADS2_GET_EVT_COND (pads),            \
        GST_COLLECT_PADS2_GET_EVT_LOCK (pads));                    \
172
  cookie = ((GstCollectPads2 *) pads)->priv->evt_cookie;                 \
Sebastian Dröge's avatar
Sebastian Dröge committed
173 174 175 176 177 178 179 180 181 182
  g_mutex_unlock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads));          \
} G_STMT_END
#define GST_COLLECT_PADS2_EVT_WAIT_TIMED(pads, cookie, timeout) G_STMT_START { \
  GTimeVal __tv; \
  \
  g_get_current_time (&tv); \
  g_time_val_add (&tv, timeout); \
  \
  g_mutex_lock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads));            \
  /* should work unless a lot of event'ing and thread starvation */\
183
  while (cookie == ((GstCollectPads2 *) pads)->priv->evt_cookie)         \
Sebastian Dröge's avatar
Sebastian Dröge committed
184 185
    g_cond_timed_wait (GST_COLLECT_PADS2_GET_EVT_COND (pads),            \
        GST_COLLECT_PADS2_GET_EVT_LOCK (pads), &tv);                    \
186
  cookie = ((GstCollectPads2 *) pads)->priv->evt_cookie;                 \
Sebastian Dröge's avatar
Sebastian Dröge committed
187 188 189 190 191
  g_mutex_unlock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads));          \
} G_STMT_END
#define GST_COLLECT_PADS2_EVT_BROADCAST(pads) G_STMT_START {       \
  g_mutex_lock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads));            \
  /* never mind wrap-around */                                     \
192
  ++(((GstCollectPads2 *) pads)->priv->evt_cookie);                      \
Sebastian Dröge's avatar
Sebastian Dröge committed
193 194 195 196 197
  g_cond_broadcast (GST_COLLECT_PADS2_GET_EVT_COND (pads));        \
  g_mutex_unlock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads));          \
} G_STMT_END
#define GST_COLLECT_PADS2_EVT_INIT(cookie) G_STMT_START {          \
  g_mutex_lock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads));            \
198
  cookie = ((GstCollectPads2 *) pads)->priv->evt_cookie;                 \
Sebastian Dröge's avatar
Sebastian Dröge committed
199 200 201 202 203 204 205 206
  g_mutex_unlock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads));          \
} G_STMT_END

static void
gst_collect_pads2_class_init (GstCollectPads2Class * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;

207 208
  g_type_class_add_private (klass, sizeof (GstCollectPads2Private));

Sebastian Dröge's avatar
Sebastian Dröge committed
209 210 211 212 213 214 215
  GST_DEBUG_CATEGORY_INIT (collect_pads2_debug, "collectpads2", 0,
      "GstCollectPads2");

  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_collect_pads2_finalize);
}

static void
216
gst_collect_pads2_init (GstCollectPads2 * pads)
Sebastian Dröge's avatar
Sebastian Dröge committed
217
{
218 219 220 221
  pads->priv =
      G_TYPE_INSTANCE_GET_PRIVATE (pads, GST_TYPE_COLLECT_PADS2,
      GstCollectPads2Private);

Sebastian Dröge's avatar
Sebastian Dröge committed
222
  pads->data = NULL;
223 224 225 226 227
  pads->priv->cookie = 0;
  pads->priv->numpads = 0;
  pads->priv->queuedpads = 0;
  pads->priv->eospads = 0;
  pads->priv->started = FALSE;
Sebastian Dröge's avatar
Sebastian Dröge committed
228

Wim Taymans's avatar
Wim Taymans committed
229
  g_rec_mutex_init (&pads->stream_lock);
Sebastian Dröge's avatar
Sebastian Dröge committed
230

231 232 233 234
  pads->priv->func = gst_collect_pads2_default_collected;
  pads->priv->user_data = NULL;
  pads->priv->event_func = NULL;
  pads->priv->event_user_data = NULL;
Sebastian Dröge's avatar
Sebastian Dröge committed
235 236

  /* members for default muxing */
237 238 239 240 241 242
  pads->priv->buffer_func = NULL;
  pads->priv->buffer_user_data = NULL;
  pads->priv->compare_func = gst_collect_pads2_default_compare_func;
  pads->priv->compare_user_data = NULL;
  pads->priv->earliest_data = NULL;
  pads->priv->earliest_time = GST_CLOCK_TIME_NONE;
Sebastian Dröge's avatar
Sebastian Dröge committed
243 244

  /* members to manage the pad list */
245 246
  pads->priv->pad_cookie = 0;
  pads->priv->pad_list = NULL;
Sebastian Dröge's avatar
Sebastian Dröge committed
247 248

  /* members for event */
249 250
  g_mutex_init (&pads->priv->evt_lock);
  g_cond_init (&pads->priv->evt_cond);
251
  pads->priv->evt_cookie = 0;
Sebastian Dröge's avatar
Sebastian Dröge committed
252 253 254 255 256 257 258 259 260
}

static void
gst_collect_pads2_finalize (GObject * object)
{
  GstCollectPads2 *pads = GST_COLLECT_PADS2 (object);

  GST_DEBUG_OBJECT (object, "finalize");

Wim Taymans's avatar
Wim Taymans committed
261
  g_rec_mutex_clear (&pads->stream_lock);
Sebastian Dröge's avatar
Sebastian Dröge committed
262

263 264
  g_cond_clear (&pads->priv->evt_cond);
  g_mutex_clear (&pads->priv->evt_lock);
Sebastian Dröge's avatar
Sebastian Dröge committed
265 266

  /* Remove pads and free pads list */
267
  g_slist_foreach (pads->priv->pad_list, (GFunc) unref_data, NULL);
Sebastian Dröge's avatar
Sebastian Dröge committed
268 269
  g_slist_foreach (pads->data, (GFunc) unref_data, NULL);
  g_slist_free (pads->data);
270
  g_slist_free (pads->priv->pad_list);
Sebastian Dröge's avatar
Sebastian Dröge committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

/**
 * gst_collect_pads2_new:
 *
 * Create a new instance of #GstCollectsPads.
 *
 * Returns: a new #GstCollectPads2, or NULL in case of an error.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
GstCollectPads2 *
gst_collect_pads2_new (void)
{
  GstCollectPads2 *newcoll;

  newcoll = g_object_new (GST_TYPE_COLLECT_PADS2, NULL);

  return newcoll;
}

/* Must be called with GstObject lock! */
static void
gst_collect_pads2_set_buffer_function_locked (GstCollectPads2 * pads,
    GstCollectPads2BufferFunction func, gpointer user_data)
{
301 302
  pads->priv->buffer_func = func;
  pads->priv->buffer_user_data = user_data;
Sebastian Dröge's avatar
Sebastian Dröge committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
}

/**
 * gst_collect_pads2_set_buffer_function:
 * @pads: the collectpads to use
 * @func: the function to set
 * @user_data: user data passed to the function
 *
 * Set the callback function and user data that will be called with
 * the oldest buffer when all pads have been collected.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
void
gst_collect_pads2_set_buffer_function (GstCollectPads2 * pads,
    GstCollectPads2BufferFunction func, gpointer user_data)
{
  g_return_if_fail (pads != NULL);
  g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));

  GST_OBJECT_LOCK (pads);
  gst_collect_pads2_set_buffer_function_locked (pads, func, user_data);
  GST_OBJECT_UNLOCK (pads);
}

/**
 * gst_collect_pads2_set_compare_function:
 * @pads: the pads to use
 * @func: the function to set
 * @user_data: user data passed to the function
 *
 * Set the timestamp comparison function.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
/* NOTE allowing to change comparison seems not advisable;
no known use-case, and collaboration with default algorithm is unpredictable.
If custom compairing/operation is needed, just use a collect function of
your own */
void
gst_collect_pads2_set_compare_function (GstCollectPads2 * pads,
    GstCollectPads2CompareFunction func, gpointer user_data)
{
  g_return_if_fail (pads != NULL);
  g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));

  GST_OBJECT_LOCK (pads);
354 355
  pads->priv->compare_func = func;
  pads->priv->compare_user_data = user_data;
Sebastian Dröge's avatar
Sebastian Dröge committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
  GST_OBJECT_UNLOCK (pads);
}

/**
 * gst_collect_pads2_set_function:
 * @pads: the collectspads to use
 * @func: the function to set
 * @user_data: user data passed to the function
 *
 * CollectPads provides a default collection algorithm that will determine
 * the oldest buffer available on all of its pads, and then delegate
 * to a configured callback.
 * However, if circumstances are more complicated and/or more control
 * is desired, this sets a callback that will be invoked instead when
 * all the pads added to the collection have buffers queued.
 * Evidently, this callback is not compatible with
 * gst_collect_pads2_set_buffer_function() callback.
 * If this callback is set, the former will be unset.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
void
gst_collect_pads2_set_function (GstCollectPads2 * pads,
    GstCollectPads2Function func, gpointer user_data)
{
  g_return_if_fail (pads != NULL);
  g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));

  GST_OBJECT_LOCK (pads);
387 388
  pads->priv->func = func;
  pads->priv->user_data = user_data;
Sebastian Dröge's avatar
Sebastian Dröge committed
389 390 391 392 393 394 395 396 397
  gst_collect_pads2_set_buffer_function_locked (pads, NULL, NULL);
  GST_OBJECT_UNLOCK (pads);
}

static void
ref_data (GstCollectData2 * data)
{
  g_assert (data != NULL);

398
  g_atomic_int_inc (&(data->priv->refcount));
Sebastian Dröge's avatar
Sebastian Dröge committed
399 400 401 402 403 404
}

static void
unref_data (GstCollectData2 * data)
{
  g_assert (data != NULL);
405
  g_assert (data->priv->refcount > 0);
Sebastian Dröge's avatar
Sebastian Dröge committed
406

407
  if (!g_atomic_int_dec_and_test (&(data->priv->refcount)))
Sebastian Dröge's avatar
Sebastian Dröge committed
408 409
    return;

410 411
  if (data->priv->destroy_notify)
    data->priv->destroy_notify (data);
Sebastian Dröge's avatar
Sebastian Dröge committed
412 413 414 415 416

  g_object_unref (data->pad);
  if (data->buffer) {
    gst_buffer_unref (data->buffer);
  }
417
  g_free (data->priv);
Sebastian Dröge's avatar
Sebastian Dröge committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
  g_free (data);
}

/**
 * gst_collect_pads2_set_event_function:
 * @pads: the collectspads to use
 * @func: the function to set
 * @user_data: user data passed to the function
 *
 * Set the event callback function and user data that will be called after
 * collectpads has processed and event originating from one of the collected
 * pads.  If the event being processed is a serialized one, this callback is
 * called with @pads STREAM_LOCK held, otherwise not.  As this lock should be
 * held when calling a number of CollectPads functions, it should be acquired
 * if so (unusually) needed.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
void
gst_collect_pads2_set_event_function (GstCollectPads2 * pads,
    GstCollectPads2EventFunction func, gpointer user_data)
{
  g_return_if_fail (pads != NULL);
  g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));

  GST_OBJECT_LOCK (pads);
446 447
  pads->priv->event_func = func;
  pads->priv->event_user_data = user_data;
Sebastian Dröge's avatar
Sebastian Dröge committed
448 449 450
  GST_OBJECT_UNLOCK (pads);
}

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
/**
* gst_collect_pads2_clip_running:
* @pads: the collectspads to use
* @cdata: collect data of corrsponding pad
* @buf: buffer being clipped
* @outbuf: output buffer with running time, or NULL if clipped
* @user_data: user data (unused)
*
* Convenience clipping function that converts incoming buffer's timestamp
* to running time, or clips the buffer if outside configured segment.
*
* Since: 0.10.37
*/
GstFlowReturn
gst_collect_pads2_clip_running_time (GstCollectPads2 * pads,
    GstCollectData2 * cdata, GstBuffer * buf, GstBuffer ** outbuf,
    gpointer user_data)
{
  GstClockTime time;

  *outbuf = buf;
  time = GST_BUFFER_TIMESTAMP (buf);

  /* invalid left alone and passed */
  if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (time))) {
    time = gst_segment_to_running_time (&cdata->segment, GST_FORMAT_TIME, time);
    if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time))) {
      GST_DEBUG_OBJECT (cdata->pad, "clipping buffer on pad outside segment");
      gst_buffer_unref (buf);
      *outbuf = NULL;
    } else {
      GST_LOG_OBJECT (cdata->pad, "buffer ts %" GST_TIME_FORMAT " -> %"
          GST_TIME_FORMAT " running time",
          GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), GST_TIME_ARGS (time));
485
      *outbuf = gst_buffer_make_writable (buf);
486 487 488 489 490 491 492
      GST_BUFFER_TIMESTAMP (*outbuf) = time;
    }
  }

  return GST_FLOW_OK;
}

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
 /**
 * gst_collect_pads2_set_clip_function:
 * @pads: the collectspads to use
 * @clipfunc: clip function to install
 * @user_data: user data to pass to @clip_func
 *
 * Install a clipping function that is called right after a buffer is received
 * on a pad managed by @pads. See #GstCollectPad2ClipFunction for more info.
 *
 * Since: 0.10.36
 */
void
gst_collect_pads2_set_clip_function (GstCollectPads2 * pads,
    GstCollectPads2ClipFunction clipfunc, gpointer user_data)
{
  g_return_if_fail (pads != NULL);
  g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));

511 512
  pads->priv->clip_func = clipfunc;
  pads->priv->clip_user_data = user_data;
513 514
}

Sebastian Dröge's avatar
Sebastian Dröge committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
/**
 * gst_collect_pads2_add_pad:
 * @pads: the collectspads to use
 * @pad: the pad to add
 * @size: the size of the returned #GstCollectData2 structure
 *
 * Add a pad to the collection of collect pads. The pad has to be
 * a sinkpad. The refcount of the pad is incremented. Use
 * gst_collect_pads2_remove_pad() to remove the pad from the collection
 * again.
 *
 * You specify a size for the returned #GstCollectData2 structure
 * so that you can use it to store additional information.
 *
 * The pad will be automatically activated in push mode when @pads is
 * started.
 *
 * This function calls gst_collect_pads2_add_pad() passing a value of NULL
 * for destroy_notify and TRUE for locked.
 *
 * Returns: a new #GstCollectData2 to identify the new pad. Or NULL
 *   if wrong parameters are supplied.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
GstCollectData2 *
gst_collect_pads2_add_pad (GstCollectPads2 * pads, GstPad * pad, guint size)
{
  return gst_collect_pads2_add_pad_full (pads, pad, size, NULL, TRUE);
}

/**
 * gst_collect_pads2_add_pad_full:
 * @pads: the collectspads to use
 * @pad: the pad to add
 * @size: the size of the returned #GstCollectData2 structure
 * @destroy_notify: function to be called before the returned #GstCollectData2
 * structure is freed
 * @lock: whether to lock this pad in usual waiting state
 *
 * Add a pad to the collection of collect pads. The pad has to be
 * a sinkpad. The refcount of the pad is incremented. Use
 * gst_collect_pads2_remove_pad() to remove the pad from the collection
 * again.
 *
 * You specify a size for the returned #GstCollectData2 structure
 * so that you can use it to store additional information.
 *
 * You can also specify a #GstCollectData2DestroyNotify that will be called
 * just before the #GstCollectData2 structure is freed. It is passed the
 * pointer to the structure and should free any custom memory and resources
 * allocated for it.
 *
 * Keeping a pad locked in waiting state is only relevant when using
 * the default collection algorithm (providing the oldest buffer).
 * It ensures a buffer must be available on this pad for a collection
 * to take place.  This is of typical use to a muxer element where
 * non-subtitle streams should always be in waiting state,
 * e.g. to assure that caps information is available on all these streams
 * when initial headers have to be written.
 *
 * The pad will be automatically activated in push mode when @pads is
 * started.
 *
 * Since: 0.10.36
 *
 * Returns: a new #GstCollectData2 to identify the new pad. Or NULL
 *   if wrong parameters are supplied.
 *
 * MT safe.
 */
GstCollectData2 *
gst_collect_pads2_add_pad_full (GstCollectPads2 * pads, GstPad * pad,
    guint size, GstCollectData2DestroyNotify destroy_notify, gboolean lock)
{
  GstCollectData2 *data;

  g_return_val_if_fail (pads != NULL, NULL);
  g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), NULL);
  g_return_val_if_fail (pad != NULL, NULL);
  g_return_val_if_fail (GST_PAD_IS_SINK (pad), NULL);
  g_return_val_if_fail (size >= sizeof (GstCollectData2), NULL);

  GST_DEBUG_OBJECT (pads, "adding pad %s:%s", GST_DEBUG_PAD_NAME (pad));

  data = g_malloc0 (size);
603
  data->priv = g_new0 (GstCollectData2Private, 1);
Sebastian Dröge's avatar
Sebastian Dröge committed
604 605 606 607 608 609 610
  data->collect = pads;
  data->pad = gst_object_ref (pad);
  data->buffer = NULL;
  data->pos = 0;
  gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
  data->state = GST_COLLECT_PADS2_STATE_WAITING;
  data->state |= lock ? GST_COLLECT_PADS2_STATE_LOCKED : 0;
611 612
  data->priv->refcount = 1;
  data->priv->destroy_notify = destroy_notify;
Sebastian Dröge's avatar
Sebastian Dröge committed
613 614 615 616 617

  GST_OBJECT_LOCK (pads);
  GST_OBJECT_LOCK (pad);
  gst_pad_set_element_private (pad, data);
  GST_OBJECT_UNLOCK (pad);
618
  pads->priv->pad_list = g_slist_append (pads->priv->pad_list, data);
Sebastian Dröge's avatar
Sebastian Dröge committed
619 620 621 622 623 624
  gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_collect_pads2_chain));
  gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_collect_pads2_event));
  /* backward compat, also add to data if stopped, so that the element already
   * has this in the public data list before going PAUSED (typically)
   * this can only be done when we are stopped because we don't take the
   * STREAM_LOCK to protect the pads->data list. */
625
  if (!pads->priv->started) {
Sebastian Dröge's avatar
Sebastian Dröge committed
626 627 628 629
    pads->data = g_slist_append (pads->data, data);
    ref_data (data);
  }
  /* activate the pad when needed */
630
  if (pads->priv->started)
Sebastian Dröge's avatar
Sebastian Dröge committed
631
    gst_pad_set_active (pad, TRUE);
632
  pads->priv->pad_cookie++;
Sebastian Dröge's avatar
Sebastian Dröge committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
  GST_OBJECT_UNLOCK (pads);

  return data;
}

static gint
find_pad (GstCollectData2 * data, GstPad * pad)
{
  if (data->pad == pad)
    return 0;
  return 1;
}

/**
 * gst_collect_pads2_remove_pad:
 * @pads: the collectspads to use
 * @pad: the pad to remove
 *
 * Remove a pad from the collection of collect pads. This function will also
 * free the #GstCollectData2 and all the resources that were allocated with
 * gst_collect_pads2_add_pad().
 *
 * The pad will be deactivated automatically when @pads is stopped.
 *
 * Returns: %TRUE if the pad could be removed.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
gboolean
gst_collect_pads2_remove_pad (GstCollectPads2 * pads, GstPad * pad)
{
  GstCollectData2 *data;
  GSList *list;

  g_return_val_if_fail (pads != NULL, FALSE);
  g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), FALSE);
  g_return_val_if_fail (pad != NULL, FALSE);
  g_return_val_if_fail (GST_IS_PAD (pad), FALSE);

  GST_DEBUG_OBJECT (pads, "removing pad %s:%s", GST_DEBUG_PAD_NAME (pad));

  GST_OBJECT_LOCK (pads);
677 678
  list =
      g_slist_find_custom (pads->priv->pad_list, pad, (GCompareFunc) find_pad);
Sebastian Dröge's avatar
Sebastian Dröge committed
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
  if (!list)
    goto unknown_pad;

  data = (GstCollectData2 *) list->data;

  GST_DEBUG_OBJECT (pads, "found pad %s:%s at %p", GST_DEBUG_PAD_NAME (pad),
      data);

  /* clear the stuff we configured */
  gst_pad_set_chain_function (pad, NULL);
  gst_pad_set_event_function (pad, NULL);
  GST_OBJECT_LOCK (pad);
  gst_pad_set_element_private (pad, NULL);
  GST_OBJECT_UNLOCK (pad);

  /* backward compat, also remove from data if stopped, note that this function
   * can only be called when we are stopped because we don't take the
   * STREAM_LOCK to protect the pads->data list. */
697
  if (!pads->priv->started) {
Sebastian Dröge's avatar
Sebastian Dröge committed
698 699 700 701 702 703 704 705 706 707 708
    GSList *dlist;

    dlist = g_slist_find_custom (pads->data, pad, (GCompareFunc) find_pad);
    if (dlist) {
      GstCollectData2 *pdata = dlist->data;

      pads->data = g_slist_delete_link (pads->data, dlist);
      unref_data (pdata);
    }
  }
  /* remove from the pad list */
709 710
  pads->priv->pad_list = g_slist_delete_link (pads->priv->pad_list, list);
  pads->priv->pad_cookie++;
Sebastian Dröge's avatar
Sebastian Dröge committed
711 712 713 714 715

  /* signal waiters because something changed */
  GST_COLLECT_PADS2_EVT_BROADCAST (pads);

  /* deactivate the pad when needed */
716
  if (!pads->priv->started)
Sebastian Dröge's avatar
Sebastian Dröge committed
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
    gst_pad_set_active (pad, FALSE);

  /* clean and free the collect data */
  unref_data (data);

  GST_OBJECT_UNLOCK (pads);

  return TRUE;

unknown_pad:
  {
    GST_WARNING_OBJECT (pads, "cannot remove unknown pad %s:%s",
        GST_DEBUG_PAD_NAME (pad));
    GST_OBJECT_UNLOCK (pads);
    return FALSE;
  }
}

/**
 * gst_collect_pads2_is_active:
 * @pads: the collectspads to use
 * @pad: the pad to check
 *
 * Check if a pad is active.
 *
 * This function is currently not implemented.
 *
 * Returns: %TRUE if the pad is active.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
gboolean
gst_collect_pads2_is_active (GstCollectPads2 * pads, GstPad * pad)
{
  g_return_val_if_fail (pads != NULL, FALSE);
  g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), FALSE);
  g_return_val_if_fail (pad != NULL, FALSE);
  g_return_val_if_fail (GST_IS_PAD (pad), FALSE);

  g_warning ("gst_collect_pads2_is_active() is not implemented");

  return FALSE;
}

/**
 * gst_collect_pads2_collect:
 * @pads: the collectspads to use
 *
 * Collect data on all pads. This function is usually called
 * from a #GstTask function in an element. 
 *
 * This function is currently not implemented.
 *
 * Returns: #GstFlowReturn of the operation.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
GstFlowReturn
gst_collect_pads2_collect (GstCollectPads2 * pads)
{
  g_return_val_if_fail (pads != NULL, GST_FLOW_ERROR);
  g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), GST_FLOW_ERROR);

  g_warning ("gst_collect_pads2_collect() is not implemented");

  return GST_FLOW_NOT_SUPPORTED;
}

/**
 * gst_collect_pads2_collect_range:
 * @pads: the collectspads to use
 * @offset: the offset to collect
 * @length: the length to collect
 *
 * Collect data with @offset and @length on all pads. This function
 * is typically called in the getrange function of an element. 
 *
 * This function is currently not implemented.
 *
 * Returns: #GstFlowReturn of the operation.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
GstFlowReturn
gst_collect_pads2_collect_range (GstCollectPads2 * pads, guint64 offset,
    guint length)
{
  g_return_val_if_fail (pads != NULL, GST_FLOW_ERROR);
  g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), GST_FLOW_ERROR);

  g_warning ("gst_collect_pads2_collect_range() is not implemented");

  return GST_FLOW_NOT_SUPPORTED;
}

/*
 * Must be called with STREAM_LOCK.
 */
static void
gst_collect_pads2_set_flushing_unlocked (GstCollectPads2 * pads,
    gboolean flushing)
{
  GSList *walk = NULL;

  /* Update the pads flushing flag */
  for (walk = pads->data; walk; walk = g_slist_next (walk)) {
    GstCollectData2 *cdata = walk->data;

    if (GST_IS_PAD (cdata->pad)) {
      GST_OBJECT_LOCK (cdata->pad);
      if (flushing)
        GST_PAD_SET_FLUSHING (cdata->pad);
      else
        GST_PAD_UNSET_FLUSHING (cdata->pad);
      if (flushing)
        GST_COLLECT_PADS2_STATE_SET (cdata, GST_COLLECT_PADS2_STATE_FLUSHING);
      else
        GST_COLLECT_PADS2_STATE_UNSET (cdata, GST_COLLECT_PADS2_STATE_FLUSHING);
      gst_collect_pads2_clear (pads, cdata);
      GST_OBJECT_UNLOCK (cdata->pad);
    }
  }

  /* inform _chain of changes */
  GST_COLLECT_PADS2_EVT_BROADCAST (pads);
}

/**
 * gst_collect_pads2_set_flushing:
 * @pads: the collectspads to use
 * @flushing: desired state of the pads
 *
 * Change the flushing state of all the pads in the collection. No pad
 * is able to accept anymore data when @flushing is %TRUE. Calling this
 * function with @flushing %FALSE makes @pads accept data again.
 * Caller must ensure that downstream streaming (thread) is not blocked,
 * e.g. by sending a FLUSH_START downstream.
 *
 * MT safe.
 *
 *
 * Since: 0.10.36
 */
void
gst_collect_pads2_set_flushing (GstCollectPads2 * pads, gboolean flushing)
{
  g_return_if_fail (pads != NULL);
  g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));

  /* NOTE since this eventually calls _pop, some (STREAM_)LOCK is needed here */
  GST_COLLECT_PADS2_STREAM_LOCK (pads);
  gst_collect_pads2_set_flushing_unlocked (pads, flushing);
  GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
}

/**
 * gst_collect_pads2_start:
 * @pads: the collectspads to use
 *
 * Starts the processing of data in the collect_pads2.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
void
gst_collect_pads2_start (GstCollectPads2 * pads)
{
  GSList *collected;

  g_return_if_fail (pads != NULL);
  g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));

  GST_DEBUG_OBJECT (pads, "starting collect pads");

  /* make sure stop and collect cannot be called anymore */
  GST_COLLECT_PADS2_STREAM_LOCK (pads);

  /* make pads streamable */
  GST_OBJECT_LOCK (pads);

  /* loop over the master pad list and reset the segment */
905
  collected = pads->priv->pad_list;
Sebastian Dröge's avatar
Sebastian Dröge committed
906 907 908 909 910 911 912 913 914 915
  for (; collected; collected = g_slist_next (collected)) {
    GstCollectData2 *data;

    data = collected->data;
    gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
  }

  gst_collect_pads2_set_flushing_unlocked (pads, FALSE);

  /* Start collect pads */
916
  pads->priv->started = TRUE;
Sebastian Dröge's avatar
Sebastian Dröge committed
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
  GST_OBJECT_UNLOCK (pads);
  GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
}

/**
 * gst_collect_pads2_stop:
 * @pads: the collectspads to use
 *
 * Stops the processing of data in the collect_pads2. this function
 * will also unblock any blocking operations.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
void
gst_collect_pads2_stop (GstCollectPads2 * pads)
{
  GSList *collected;

  g_return_if_fail (pads != NULL);
  g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));

  GST_DEBUG_OBJECT (pads, "stopping collect pads");

  /* make sure collect and start cannot be called anymore */
  GST_COLLECT_PADS2_STREAM_LOCK (pads);

  /* make pads not accept data anymore */
  GST_OBJECT_LOCK (pads);
  gst_collect_pads2_set_flushing_unlocked (pads, TRUE);

  /* Stop collect pads */
950 951 952
  pads->priv->started = FALSE;
  pads->priv->eospads = 0;
  pads->priv->queuedpads = 0;
Sebastian Dröge's avatar
Sebastian Dröge committed
953 954

  /* loop over the master pad list and flush buffers */
955
  collected = pads->priv->pad_list;
Sebastian Dröge's avatar
Sebastian Dröge committed
956 957 958 959 960 961 962 963 964 965 966 967 968
  for (; collected; collected = g_slist_next (collected)) {
    GstCollectData2 *data;
    GstBuffer **buffer_p;

    data = collected->data;
    if (data->buffer) {
      buffer_p = &data->buffer;
      gst_buffer_replace (buffer_p, NULL);
      data->pos = 0;
    }
    GST_COLLECT_PADS2_STATE_UNSET (data, GST_COLLECT_PADS2_STATE_EOS);
  }

969 970 971 972
  if (pads->priv->earliest_data)
    unref_data (pads->priv->earliest_data);
  pads->priv->earliest_data = NULL;
  pads->priv->earliest_time = GST_CLOCK_TIME_NONE;
Sebastian Dröge's avatar
Sebastian Dröge committed
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044

  GST_OBJECT_UNLOCK (pads);
  /* Wake them up so they can end the chain functions. */
  GST_COLLECT_PADS2_EVT_BROADCAST (pads);

  GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
}

/**
 * gst_collect_pads2_peek:
 * @pads: the collectspads to peek
 * @data: the data to use
 *
 * Peek at the buffer currently queued in @data. This function
 * should be called with the @pads STREAM_LOCK held, such as in the callback
 * handler.
 *
 * Returns: The buffer in @data or NULL if no buffer is queued.
 *  should unref the buffer after usage.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
GstBuffer *
gst_collect_pads2_peek (GstCollectPads2 * pads, GstCollectData2 * data)
{
  GstBuffer *result;

  g_return_val_if_fail (pads != NULL, NULL);
  g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), NULL);
  g_return_val_if_fail (data != NULL, NULL);

  if ((result = data->buffer))
    gst_buffer_ref (result);

  GST_DEBUG_OBJECT (pads, "Peeking at pad %s:%s: buffer=%p",
      GST_DEBUG_PAD_NAME (data->pad), result);

  return result;
}

/**
 * gst_collect_pads2_pop:
 * @pads: the collectspads to pop
 * @data: the data to use
 *
 * Pop the buffer currently queued in @data. This function
 * should be called with the @pads STREAM_LOCK held, such as in the callback
 * handler.
 *
 * Returns: The buffer in @data or NULL if no buffer was queued.
 *   You should unref the buffer after usage.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
GstBuffer *
gst_collect_pads2_pop (GstCollectPads2 * pads, GstCollectData2 * data)
{
  GstBuffer *result;

  g_return_val_if_fail (pads != NULL, NULL);
  g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), NULL);
  g_return_val_if_fail (data != NULL, NULL);

  if ((result = data->buffer)) {
    data->buffer = NULL;
    data->pos = 0;
    /* one less pad with queued data now */
    if (GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_WAITING))
1045
      pads->priv->queuedpads--;
Sebastian Dröge's avatar
Sebastian Dröge committed
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
  }

  GST_COLLECT_PADS2_EVT_BROADCAST (pads);

  GST_DEBUG_OBJECT (pads, "Pop buffer on pad %s:%s: buffer=%p",
      GST_DEBUG_PAD_NAME (data->pad), result);

  return result;
}

/* pop and unref the currently queued buffer, should be called with STREAM_LOCK
 * held */
static void
gst_collect_pads2_clear (GstCollectPads2 * pads, GstCollectData2 * data)
{
  GstBuffer *buf;

  if ((buf = gst_collect_pads2_pop (pads, data)))
    gst_buffer_unref (buf);
}

/**
 * gst_collect_pads2_available:
 * @pads: the collectspads to query
 *
 * Query how much bytes can be read from each queued buffer. This means
 * that the result of this call is the maximum number of bytes that can
 * be read from each of the pads.
 *
 * This function should be called with @pads STREAM_LOCK held, such as
 * in the callback.
 *
 * Returns: The maximum number of bytes queued on all pads. This function
 * returns 0 if a pad has no queued buffer.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
/* we might pre-calculate this in some struct field,
 * but would then have to maintain this in _chain and particularly _pop, etc,
 * even if element is never interested in this information */
guint
gst_collect_pads2_available (GstCollectPads2 * pads)
{
  GSList *collected;
  guint result = G_MAXUINT;

  g_return_val_if_fail (pads != NULL, 0);
  g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), 0);

  collected = pads->data;
  for (; collected; collected = g_slist_next (collected)) {
    GstCollectData2 *pdata;
    GstBuffer *buffer;
    gint size;

    pdata = (GstCollectData2 *) collected->data;

    /* ignore pad with EOS */
    if (G_UNLIKELY (GST_COLLECT_PADS2_STATE_IS_SET (pdata,
                GST_COLLECT_PADS2_STATE_EOS))) {
      GST_DEBUG_OBJECT (pads, "pad %p is EOS", pdata);
      continue;
    }

    /* an empty buffer without EOS is weird when we get here.. */
    if (G_UNLIKELY ((buffer = pdata->buffer) == NULL)) {
      GST_WARNING_OBJECT (pads, "pad %p has no buffer", pdata);
      goto not_filled;
    }

    /* this is the size left of the buffer */
Wim Taymans's avatar
Wim Taymans committed
1119
    size = gst_buffer_get_size (buffer) - pdata->pos;
Sebastian Dröge's avatar
Sebastian Dröge committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
    GST_DEBUG_OBJECT (pads, "pad %p has %d bytes left", pdata, size);

    /* need to return the min of all available data */
    if (size < result)
      result = size;
  }
  /* nothing changed, all must be EOS then, return 0 */
  if (G_UNLIKELY (result == G_MAXUINT))
    result = 0;

  return result;

not_filled:
  {
    return 0;
  }
}

/**
 * gst_collect_pads2_flush:
 * @pads: the collectspads to query
 * @data: the data to use
 * @size: the number of bytes to flush
 *
 * Flush @size bytes from the pad @data.
 *
 * This function should be called with @pads STREAM_LOCK held, such as
 * in the callback.
 *
 * Returns: The number of bytes flushed This can be less than @size and
 * is 0 if the pad was end-of-stream.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
guint
gst_collect_pads2_flush (GstCollectPads2 * pads, GstCollectData2 * data,
    guint size)
{
  guint flushsize;
Wim Taymans's avatar
Wim Taymans committed
1161
  gsize bsize;
Sebastian Dröge's avatar
Sebastian Dröge committed
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
  GstBuffer *buffer;

  g_return_val_if_fail (pads != NULL, 0);
  g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), 0);
  g_return_val_if_fail (data != NULL, 0);

  /* no buffer, must be EOS */
  if ((buffer = data->buffer) == NULL)
    return 0;

Wim Taymans's avatar
Wim Taymans committed
1172 1173
  bsize = gst_buffer_get_size (buffer);

Sebastian Dröge's avatar
Sebastian Dröge committed
1174
  /* this is what we can flush at max */
Wim Taymans's avatar
Wim Taymans committed
1175
  flushsize = MIN (size, bsize - data->pos);
Sebastian Dröge's avatar
Sebastian Dröge committed
1176 1177 1178

  data->pos += size;

Wim Taymans's avatar
Wim Taymans committed
1179
  if (data->pos >= bsize)
Sebastian Dröge's avatar
Sebastian Dröge committed
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
    /* _clear will also reset data->pos to 0 */
    gst_collect_pads2_clear (pads, data);

  return flushsize;
}

/**
 * gst_collect_pads2_read_buffer:
 * @pads: the collectspads to query
 * @data: the data to use
 * @size: the number of bytes to read
 *
 * Get a subbuffer of @size bytes from the given pad @data.
 *
 * This function should be called with @pads STREAM_LOCK held, such as in the
 * callback.
 *
 * Since: 0.10.36
 *
 * Returns: A sub buffer. The size of the buffer can be less that requested.
 * A return of NULL signals that the pad is end-of-stream.
 * Unref the buffer after use.
 *
 * MT safe.
 */
GstBuffer *
gst_collect_pads2_read_buffer (GstCollectPads2 * pads, GstCollectData2 * data,
    guint size)
{
  guint readsize;
  GstBuffer *buffer;

  g_return_val_if_fail (pads != NULL, NULL);
  g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), NULL);
  g_return_val_if_fail (data != NULL, NULL);

  /* no buffer, must be EOS */
  if ((buffer = data->buffer) == NULL)
    return NULL;

Wim Taymans's avatar
Wim Taymans committed
1220
  readsize = MIN (size, gst_buffer_get_size (buffer) - data->pos);
Sebastian Dröge's avatar
Sebastian Dröge committed
1221

Wim Taymans's avatar
Wim Taymans committed
1222 1223
  return gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, data->pos,
      readsize);
Sebastian Dröge's avatar
Sebastian Dröge committed
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
}

/**
 * gst_collect_pads2_take_buffer:
 * @pads: the collectspads to query
 * @data: the data to use
 * @size: the number of bytes to read
 *
 * Get a subbuffer of @size bytes from the given pad @data. Flushes the amount
 * of read bytes.
 *
 * This function should be called with @pads STREAM_LOCK held, such as in the
 * callback.
 *
 * Since: 0.10.36
 *
 * Returns: A sub buffer. The size of the buffer can be less that requested.
 * A return of NULL signals that the pad is end-of-stream.
 * Unref the buffer after use.
 *
 * MT safe.
 */
GstBuffer *
gst_collect_pads2_take_buffer (GstCollectPads2 * pads, GstCollectData2 * data,
    guint size)
{
  GstBuffer *buffer = gst_collect_pads2_read_buffer (pads, data, size);

  if (buffer) {
Wim Taymans's avatar
Wim Taymans committed
1253
    gst_collect_pads2_flush (pads, data, gst_buffer_get_size (buffer));
Sebastian Dröge's avatar
Sebastian Dröge committed
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
  }
  return buffer;
}

/**
 * gst_collect_pads2_set_waiting:
 * @pads: the collectspads
 * @data: the data to use
 * @waiting: boolean indicating whether this pad should operate
 *           in waiting or non-waiting mode
 *
 * Sets a pad to waiting or non-waiting mode, if at least this pad
 * has not been created with locked waiting state,
 * in which case nothing happens.
 *
 * This function should be called with @pads STREAM_LOCK held, such as
 * in the callback.
 *
 * MT safe.
 *
 * Since: 0.10.36
 */
void
gst_collect_pads2_set_waiting (GstCollectPads2 * pads, GstCollectData2 * data,
    gboolean waiting)
{
  g_return_if_fail (pads != NULL);
  g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));
  g_return_if_fail (data != NULL);

  GST_DEBUG_OBJECT (pads, "Setting pad %s to waiting %d, locked %d",
      GST_PAD_NAME (data->pad), waiting,
      GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_LOCKED));

  /* Do something only on a change and if not locked */
  if (!GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_LOCKED) &&
      (GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_WAITING) !=
          ! !waiting)) {
    /* Set waiting state for this pad */
    if (waiting)
      GST_COLLECT_PADS2_STATE_SET (data, GST_COLLECT_PADS2_STATE_WAITING);
    else
      GST_COLLECT_PADS2_STATE_UNSET (data, GST_COLLECT_PADS2_STATE_WAITING);
    /* Update number of queued pads if needed */
    if (!data->buffer &&
        !GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_EOS)) {
      if (waiting)
1301
        pads->priv->queuedpads--;
Sebastian Dröge's avatar
Sebastian Dröge committed
1302
      else
1303
        pads->priv->queuedpads++;
Sebastian Dröge's avatar
Sebastian Dröge committed
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
    }

    /* signal waiters because something changed */
    GST_COLLECT_PADS2_EVT_BROADCAST (pads);
  }
}

/* see if pads were added or removed and update our stats. Any pad
 * added after releasing the LOCK will get collected in the next
 * round.
 *
 * We can do a quick check by checking the cookies, that get changed
 * whenever the pad list is updated.
 *
 * Must be called with STREAM_LOCK.
 */
static void
gst_collect_pads2_check_pads (GstCollectPads2 * pads)
{
  /* the master list and cookie are protected with LOCK */
  GST_OBJECT_LOCK (pads);
1325
  if (G_UNLIKELY (pads->priv->pad_cookie != pads->priv->cookie)) {
Sebastian Dröge's avatar
Sebastian Dröge committed
1326 1327 1328 1329 1330 1331
    GSList *collected;

    /* clear list and stats */
    g_slist_foreach (pads->data, (GFunc) unref_data, NULL);
    g_slist_free (pads->data);
    pads->data = NULL;
1332 1333 1334 1335 1336 1337 1338
    pads->priv->numpads = 0;
    pads->priv->queuedpads = 0;
    pads->priv->eospads = 0;
    if (pads->priv->earliest_data)
      unref_data (pads->priv->earliest_data);
    pads->priv->earliest_data = NULL;
    pads->priv->earliest_time = GST_CLOCK_TIME_NONE;
Sebastian Dröge's avatar
Sebastian Dröge committed
1339 1340

    /* loop over the master pad list */
1341
    collected = pads->priv->pad_list;
Sebastian Dröge's avatar
Sebastian Dröge committed
1342 1343 1344 1345
    for (; collected; collected = g_slist_next (collected)) {
      GstCollectData2 *data;

      /* update the stats */
1346
      pads->priv->numpads++;
Sebastian Dröge's avatar
Sebastian Dröge committed
1347 1348
      data = collected->data;
      if (GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_EOS))
1349
        pads->priv->eospads++;
Sebastian Dröge's avatar
Sebastian Dröge committed
1350 1351
      else if (data->buffer || !GST_COLLECT_PADS2_STATE_IS_SET (data,
              GST_COLLECT_PADS2_STATE_WAITING))
1352
        pads->priv->queuedpads++;
Sebastian Dröge's avatar
Sebastian Dröge committed
1353 1354 1355 1356 1357 1358 1359

      /* add to the list of pads to collect */
      ref_data (data);
      /* preserve order of adding/requesting pads */
      pads->data = g_slist_append (pads->data, data);
    }
    /* and update the cookie */
1360
    pads->priv->cookie = pads->priv->pad_cookie;
Sebastian Dröge's avatar
Sebastian Dröge committed
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
  }
  GST_OBJECT_UNLOCK (pads);
}

/* checks if all the pads are collected and call the collectfunction
 *
 * Should be called with STREAM_LOCK.
 *
 * Returns: The #GstFlowReturn of collection.
 */
static GstFlowReturn
gst_collect_pads2_check_collected (GstCollectPads2 * pads)
{
  GstFlowReturn flow_ret = GST_FLOW_OK;
  GstCollectPads2Function func;
  gpointer user_data;

  g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), GST_FLOW_ERROR);

  GST_OBJECT_LOCK (pads);
1381 1382
  func = pads->priv->func;
  user_data = pads->priv->user_data;
Sebastian Dröge's avatar
Sebastian Dröge committed
1383 1384
  GST_OBJECT_UNLOCK (pads);

1385
  g_return_val_if_fail (pads->priv->func != NULL, GST_FLOW_NOT_SUPPORTED);
Sebastian Dröge's avatar
Sebastian Dröge committed
1386 1387 1388 1389

  /* check for new pads, update stats etc.. */
  gst_collect_pads2_check_pads (pads);

1390
  if (G_UNLIKELY (pads->priv->eospads == pads->priv->numpads)) {
Sebastian Dröge's avatar
Sebastian Dröge committed
1391 1392 1393
    /* If all our pads are EOS just collect once to let the element
     * do its final EOS handling. */
    GST_DEBUG_OBJECT (pads, "All active pads (%d) are EOS, calling %s",
1394
        pads->priv->numpads, GST_DEBUG_FUNCPTR_NAME (func));
Sebastian Dröge's avatar
Sebastian Dröge committed
1395 1396 1397 1398 1399 1400

    flow_ret = func (pads, user_data);
  } else {
    gboolean collected = FALSE;

    /* We call the collected function as long as our condition matches. */
1401 1402 1403 1404 1405
    while (((pads->priv->queuedpads + pads->priv->eospads) >=
            pads->priv->numpads)) {
      GST_DEBUG_OBJECT (pads,
          "All active pads (%d + %d >= %d) have data, " "calling %s",
          pads->priv->queuedpads, pads->priv->eospads, pads->priv->numpads,
Sebastian Dröge's avatar
Sebastian Dröge committed
1406 1407 1408 1409 1410 1411 1412 1413 1414
          GST_DEBUG_FUNCPTR_NAME (func));

      flow_ret = func (pads, user_data);
      collected = TRUE;

      /* break on error */
      if (flow_ret != GST_FLOW_OK)
        break;
      /* Don't keep looping after telling the element EOS or flushing */
1415
      if (pads->priv->queuedpads == 0)
Sebastian Dröge's avatar
Sebastian Dröge committed
1416 1417 1418 1419
        break;
    }
    if (!collected)
      GST_DEBUG_OBJECT (pads, "Not all active pads (%d) have data, continuing",
1420
          pads->priv->numpads);
Sebastian Dröge's avatar
Sebastian Dröge committed
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
  }
  return flow_ret;
}


/* General overview:
 * - only pad with a buffer can determine earliest_data (and earliest_time)
 * - only segment info determines (non-)waiting state
 * - ? perhaps use _stream_time for comparison
 *   (which muxers might have use as well ?)
 */

/*
 * Function to recalculate the waiting state of all pads.
 *
 * Must be called with STREAM_LOCK.
 *
 * Returns TRUE if a pad was set to waiting
 * (from non-waiting state).
 */
static gboolean
gst_collect_pads2_recalculate_waiting (GstCollectPads2 * pads)
{
  GSList *collected;
  gboolean result = FALSE;

  /* If earliest time is not known, there is nothing to do. */
1448
  if (pads->priv->earliest_data == NULL)
Sebastian Dröge's avatar
Sebastian Dröge committed
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
    return FALSE;

  for (collected = pads->data; collected; collected = g_slist_next (collected)) {
    GstCollectData2 *data = (GstCollectData2 *) collected->data;
    int cmp_res;

    /* check if pad has a segment */
    if (data->segment.format == GST_FORMAT_UNDEFINED)
      continue;

    /* check segment format */
    if (data->segment.format != GST_FORMAT_TIME) {
      GST_ERROR_OBJECT (pads, "GstCollectPads2 can handle only time segments.");
      continue;
    }

    /* check if the waiting state should be changed */
1466 1467 1468
    cmp_res = pads->priv->compare_func (pads, data, data->segment.start,
        pads->priv->earliest_data, pads->priv->earliest_time,
        pads->priv->compare_user_data);
Sebastian Dröge's avatar
Sebastian Dröge committed
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
    if (cmp_res > 0)
      /* stop waiting */
      gst_collect_pads2_set_waiting (pads, data, FALSE);
    else {
      if (!GST_COLLECT_PADS2_STATE_IS_SET (data,
              GST_COLLECT_PADS2_STATE_WAITING)) {
        /* start waiting */
        gst_collect_pads2_set_waiting (pads, data, TRUE);
        result = TRUE;
      }
    }
  }

  return result;
}

/**
 * gst_collect_pads2_find_best_pad:
 * @pads: the collectpads to use
 * @data: returns the collectdata for earliest data
 * @time: returns the earliest available buffertime
 *
 * Find the oldest/best pad, i.e. pad holding the oldest buffer and
 * and return the corresponding #GstCollectData2 and buffertime.
 *
 * This function should be called with STREAM_LOCK held,
 * such as in the callback.
 *
 * Since: 0.10.36
 */
static void
gst_collect_pads2_find_best_pad (GstCollectPads2 * pads,
    GstCollectData2 ** data, GstClockTime * time)
{
  GSList *collected;
  GstCollectData2 *best = NULL;
  GstClockTime best_time = GST_CLOCK_TIME_NONE;

  g_return_if_fail (data != NULL);
  g_return_if_fail (time != NULL);

  for (collected = pads->data; collected; collected = g_slist_next (collected)) {
    GstBuffer *buffer;
    GstCollectData2 *data = (GstCollectData2 *) collected->data;
    GstClockTime timestamp;

    buffer = gst_collect_pads2_peek (pads, data);
    /* if we have a buffer check if it is better then the current best one */
    if (buffer != NULL) {
      timestamp = GST_BUFFER_TIMESTAMP (buffer);
      gst_buffer_unref (buffer);
1520 1521
      if (best == NULL || pads->priv->compare_func (pads, data, timestamp,
              best, best_time, pads->priv->compare_user_data) < 0) {
Sebastian Dröge's avatar
Sebastian Dröge committed
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
        best = data;
        best_time = timestamp;
      }
    }
  }

  /* set earliest time */
  *data = best;
  *time = best_time;

  GST_DEBUG_OBJECT (pads, "best pad %s, best time %" GST_TIME_FORMAT,
      best ? GST_PAD_NAME (((GstCollectData2 *) best)->pad) : "(nil)",
      GST_TIME_ARGS (best_time));
}

/*
 * Function to recalculate earliest_data and earliest_timestamp. This also calls
 * gst_collect_pads2_recalculate_waiting
 *
 * Must be called with STREAM_LOCK.
 */
static gboolean
gst_collect_pads2_recalculate_full (GstCollectPads2 * pads)
{
1546 1547 1548 1549 1550 1551
  if (pads->priv->earliest_data)
    unref_data (pads->priv->earliest_data);
  gst_collect_pads2_find_best_pad (pads, &pads->priv->earliest_data,
      &pads->priv->earliest_time);
  if (pads->priv->earliest_data)
    ref_data (pads->priv->earliest_data);
Sebastian Dröge's avatar
Sebastian Dröge committed
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
  return gst_collect_pads2_recalculate_waiting (pads);
}

/*
 * Default collect callback triggered when #GstCollectPads2 gathered all data.
 *
 * Called with STREAM_LOCK.
 */
static GstFlowReturn
gst_collect_pads2_default_collected (GstCollectPads2 * pads, gpointer user_data)
{
  GstCollectData2 *best = NULL;
  GstBuffer *buffer;
  GstFlowReturn ret = GST_FLOW_OK;
  GstCollectPads2BufferFunction func;
  gpointer buffer_user_data;

  g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), GST_FLOW_ERROR);

  GST_OBJECT_LOCK (pads);
1572 1573
  func = pads->priv->buffer_func;
  buffer_user_data = pads->priv->buffer_user_data;
Sebastian Dröge's avatar
Sebastian Dröge committed
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
  GST_OBJECT_UNLOCK (pads);

  g_return_val_if_fail (func != NULL, GST_FLOW_NOT_SUPPORTED);

  /* Find the oldest pad at all cost */
  if (gst_collect_pads2_recalculate_full (pads)) {
    /* waiting was switched on,
     * so give another thread a chance to deliver a possibly
     * older buffer; don't charge on yet with the current oldest */
    ret = GST_FLOW_OK;
1584
    goto done;
Sebastian Dröge's avatar
Sebastian Dröge committed
1585 1586
  }

1587
  best = pads->priv->earliest_data;
Sebastian Dröge's avatar
Sebastian Dröge committed
1588 1589 1590 1591 1592

  /* No data collected means EOS. */
  if (G_UNLIKELY (best == NULL)) {
    ret = func (pads, best, NULL, buffer_user_data);
    if (ret == GST_FLOW_OK)
1593
      ret = GST_FLOW_EOS;
Sebastian Dröge's avatar
Sebastian Dröge committed
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
    goto done;
  }

  /* make sure that the pad we take a buffer from is waiting;
   * otherwise popping a buffer will seem not to have happened
   * and collectpads can get into a busy loop */
  gst_collect_pads2_set_waiting (pads, best, TRUE);

  /* Send buffer */
  buffer = gst_collect_pads2_pop (pads, best);
  ret = func (pads, best, buffer, buffer_user_data);

1606 1607 1608 1609 1610
  /* maybe non-waiting was forced to waiting above due to
   * newsegment events coming too sparsely,
   * so re-check to restore state to avoid hanging/waiting */
  gst_collect_pads2_recalculate_full (pads);

Sebastian Dröge's avatar
Sebastian Dröge committed
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
done:
  return ret;
}

/*
 * Default timestamp compare function.
 */
static gint
gst_collect_pads2_default_compare_func (GstCollectPads2 * pads,
    GstCollectData2 * data1, GstClockTime timestamp1,
    GstCollectData2 * data2, GstClockTime timestamp2, gpointer user_data)
{

  GST_LOG_OBJECT (pads, "comparing %" GST_TIME_FORMAT
      " and %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp1),
      GST_TIME_ARGS (timestamp2));
  /* non-valid timestamps go first as they are probably headers or so */
  if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp1)))
    return GST_CLOCK_TIME_IS_VALID (timestamp2) ? -1 : 0;

  if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp2)))
    return 1;

  /* compare timestamp */
  if (timestamp1 < timestamp2)
    return -1;

  if (timestamp1 > timestamp2)
    return 1;

  return 0;
}

static gboolean
1645
gst_collect_pads2_event (GstPad * pad, GstObject * parent, GstEvent * event)
Sebastian Dröge's avatar
Sebastian Dröge committed
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
{
  gboolean res = FALSE, need_unlock = FALSE;
  GstCollectData2 *data;
  GstCollectPads2 *pads;
  GstCollectPads2EventFunction event_func;
  GstCollectPads2BufferFunction buffer_func;
  gpointer event_user_data;

  /* some magic to get the managing collect_pads2 */
  GST_OBJECT_LOCK (pad);
  data = (GstCollectData2 *) gst_pad_get_element_private (pad);
  if (G_UNLIKELY (data == NULL))
    goto pad_removed;
  ref_data (data);
  GST_OBJECT_UNLOCK (pad);

  res = FALSE;

  pads = data->collect;

  GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
      GST_DEBUG_PAD_NAME (data->pad));

  GST_OBJECT_LOCK (pads);
1670 1671 1672
  event_func = pads->priv->event_func;
  event_user_data = pads->priv->event_user_data;
  buffer_func = pads->priv->buffer_func;
Sebastian Dröge's avatar
Sebastian Dröge committed
1673 1674 1675 1676 1677 1678 1679 1680
  GST_OBJECT_UNLOCK (pads);

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_FLUSH_START:
    {
      /* forward event to unblock check_collected */
      if (event_func)
        res = event_func (pads, data, event, event_user_data);
1681
      else
1682
        res = gst_pad_event_default (pad, parent, event);
Sebastian Dröge's avatar
Sebastian Dröge committed
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695

      /* now unblock the chain function.
       * no cond per pad, so they all unblock, 
       * non-flushing block again */
      GST_COLLECT_PADS2_STREAM_LOCK (pads);
      GST_COLLECT_PADS2_STATE_SET (data, GST_COLLECT_PADS2_STATE_FLUSHING);
      gst_collect_pads2_clear (pads, data);

      /* cater for possible default muxing functionality */
      if (buffer_func) {
        /* restore to initial state */
        gst_collect_pads2_set_waiting (pads, data, TRUE);
        /* if the current pad is affected, reset state, recalculate later */
1696
        if (pads->priv->earliest_data == data) {
Sebastian Dröge's avatar
Sebastian Dröge committed
1697
          unref_data (data);
1698 1699
          pads->priv->earliest_data = NULL;
          pads->priv->earliest_time = GST_CLOCK_TIME_NONE;
Sebastian Dröge's avatar
Sebastian Dröge committed
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
        }
      }

      GST_COLLECT_PADS2_STREAM_UNLOCK (pads);

      /* event already cleaned up by forwarding */
      res = TRUE;
      goto done;
    }
    case GST_EVENT_FLUSH_STOP:
    {
      /* flush the 1 buffer queue */
      GST_COLLECT_PADS2_STREAM_LOCK (pads);
      GST_COLLECT_PADS2_STATE_UNSET (data, GST_COLLECT_PADS2_STATE_FLUSHING);
      gst_collect_pads2_clear (pads, data);
      /* we need new segment info after the flush */
      gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
      GST_COLLECT_PADS2_STATE_UNSET (data, GST_COLLECT_PADS2_STATE_NEW_SEGMENT);
      /* if the pad was EOS, remove the EOS flag and
       * decrement the number of eospads */
      if (G_UNLIKELY (GST_COLLECT_PADS2_STATE_IS_SET (data,
                  GST_COLLECT_PADS2_STATE_EOS))) {
        if (!GST_COLLECT_PADS2_STATE_IS_SET (data,
                GST_COLLECT_PADS2_STATE_WAITING))
1724 1725
          pads->priv->queuedpads++;
        pads->priv->eospads--;
Sebastian Dröge's avatar
Sebastian Dröge committed
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
        GST_COLLECT_PADS2_STATE_UNSET (data, GST_COLLECT_PADS2_STATE_EOS);
      }
      GST_COLLECT_PADS2_STREAM_UNLOCK (pads);

      /* forward event */
      goto forward_or_default;
    }
    case GST_EVENT_EOS:
    {
      GST_COLLECT_PADS2_STREAM_LOCK (pads);
      /* if the pad was not EOS, make it EOS and so we
       * have one more eospad */
      if (G_LIKELY (!GST_COLLECT_PADS2_STATE_IS_SET (data,
                  GST_COLLECT_PADS2_STATE_EOS))) {
        GST_COLLECT_PADS2_STATE_SET (data, GST_COLLECT_PADS2_STATE_EOS);
        if (!GST_COLLECT_PADS2_STATE_IS_SET (data,
                GST_COLLECT_PADS2_STATE_WAITING))
1743 1744
          pads->priv->queuedpads--;
        pads->priv->eospads++;
Sebastian Dröge's avatar
Sebastian Dröge committed
1745 1746 1747 1748 1749 1750 1751
      }
      /* check if we need collecting anything, we ignore the result. */
      gst_collect_pads2_check_collected (pads);
      GST_COLLECT_PADS2_STREAM_UNLOCK (pads);

      goto forward_or_eat;
    }
Wim Taymans's avatar
Wim Taymans committed
1752
    case GST_EVENT_SEGMENT:
Sebastian Dröge's avatar
Sebastian Dröge committed
1753
    {
Wim Taymans's avatar
Wim Taymans committed
1754
      GstSegment seg;
Sebastian Dröge's avatar
Sebastian Dröge committed
1755 1756 1757 1758
      gint cmp_res;

      GST_COLLECT_PADS2_STREAM_LOCK (pads);

Wim Taymans's avatar
Wim Taymans committed
1759
      gst_event_copy_segment (event, &seg);
Sebastian Dröge's avatar
Sebastian Dröge committed
1760

Wim Taymans's avatar
Wim Taymans committed
1761
      GST_DEBUG_OBJECT (data->pad, "got segment %" GST_SEGMENT_FORMAT, &seg);
Sebastian Dröge's avatar
Sebastian Dröge committed
1762

Wim Taymans's avatar
Wim Taymans committed
1763
      data->segment = seg;
Sebastian Dröge's avatar
Sebastian Dröge committed
1764 1765 1766 1767 1768 1769 1770
      GST_COLLECT_PADS2_STATE_SET (data, GST_COLLECT_PADS2_STATE_NEW_SEGMENT);

      /* default muxing functionality */
      if (!buffer_func)
        goto newsegment_done;

      /* default collection can not handle other segment formats than time */
Wim Taymans's avatar
Wim Taymans committed
1771
      if (seg.format != GST_FORMAT_TIME) {
Sebastian Dröge's avatar
Sebastian Dröge committed
1772 1773 1774 1775 1776 1777 1778
        GST_ERROR_OBJECT (pads, "GstCollectPads2 default collecting "
            "can only handle time segments.");
        goto newsegment_done;
      }

      /* If oldest time is not known, or current pad got newsegment;
       * recalculate the state */