diff --git a/gst/videomixer/Makefile.am b/gst/videomixer/Makefile.am index 193d8a7457..583224847a 100644 --- a/gst/videomixer/Makefile.am +++ b/gst/videomixer/Makefile.am @@ -3,14 +3,27 @@ plugin_LTLIBRARIES = libgstvideomixer.la ORC_SOURCE=blendorc include $(top_srcdir)/common/orc.mak -libgstvideomixer_la_SOURCES = videomixer.c blend.c +libgstvideomixer_la_SOURCES = \ + videomixer.c \ + blend.c \ + videomixer2.c \ + gstcollectpads2.c + nodist_libgstvideomixer_la_SOURCES = $(ORC_NODIST_SOURCES) libgstvideomixer_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) \ - $(GST_BASE_CFLAGS) $(GST_CONTROLLER_CFLAGS) $(GST_CFLAGS) $(ORC_CFLAGS) -libgstvideomixer_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) -lgstvideo-@GST_MAJORMINOR@ \ + $(GST_BASE_CFLAGS) $(GST_CONTROLLER_CFLAGS) $(GST_CFLAGS) $(ORC_CFLAGS) \ + -DGstCollectPads2=GstVideoMixer2Pads \ + -DGstCollectPads2Class=GstVideoMixer2PadsClass +libgstvideomixer_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) \ + -lgstvideo-@GST_MAJORMINOR@ \ $(GST_BASE_LIBS) $(GST_CONTROLLER_LIBS) $(GST_LIBS) $(ORC_LIBS) libgstvideomixer_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) libgstvideomixer_la_LIBTOOLFLAGS = --tag=disable-static # headers we need but don't want installed -noinst_HEADERS = videomixer.h videomixerpad.h blend.h +noinst_HEADERS = \ + videomixer.h \ + videomixerpad.h \ + blend.h \ + videomixer2.h \ + gstcollectpads2.h diff --git a/gst/videomixer/gstcollectpads2.c b/gst/videomixer/gstcollectpads2.c new file mode 100644 index 0000000000..058a064a04 --- /dev/null +++ b/gst/videomixer/gstcollectpads2.c @@ -0,0 +1,1935 @@ +/* GStreamer + * Copyright (C) 2005 Wim Taymans + * Copyright (C) 2008 Mark Nauwelaerts + * Copyright (C) 2010 Sebastian Dröge + * + * 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. + * + * + * Collectpads are created with gst_collect_pads2_new(). A callback should then + * be installed with gst_collect_pads2_set_function (). + * + * + * 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. + * + * + * For each pad, data is queued in the _chain function or by + * performing a pull_range. + * + * + * When data is queued on all pads in waiting mode, the callback function is called. + * + * + * 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. + * + * + * Data can also be dequeued in byte units using the gst_collect_pads2_available(), + * gst_collect_pads2_read() and gst_collect_pads2_flush() calls. + * + * + * 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. + * + * + * 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. + * + * + * 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. + * + * + * + * Last reviewed on 2008-03-14 (0.10.17) + */ + +#include "gstcollectpads2.h" + +GST_DEBUG_CATEGORY_STATIC (collect_pads2_debug); +#define GST_CAT_DEFAULT collect_pads2_debug + +GST_BOILERPLATE (GstCollectPads2, gst_collect_pads2, GstObject, + GST_TYPE_OBJECT); + +static void gst_collect_pads2_clear (GstCollectPads2 * pads, + GstCollectData2 * data); +static GstFlowReturn gst_collect_pads2_chain (GstPad * pad, GstBuffer * buffer); +static gboolean gst_collect_pads2_event (GstPad * pad, GstEvent * event); +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. + */ +#define GST_COLLECT_PADS2_GET_EVT_COND(pads) (((GstCollectPads2 *)pads)->evt_cond) +#define GST_COLLECT_PADS2_GET_EVT_LOCK(pads) (((GstCollectPads2 *)pads)->evt_lock) +#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 */\ + while (cookie == ((GstCollectPads2 *) pads)->evt_cookie) \ + g_cond_wait (GST_COLLECT_PADS2_GET_EVT_COND (pads), \ + GST_COLLECT_PADS2_GET_EVT_LOCK (pads)); \ + cookie = ((GstCollectPads2 *) pads)->evt_cookie; \ + 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 */\ + while (cookie == ((GstCollectPads2 *) pads)->evt_cookie) \ + g_cond_timed_wait (GST_COLLECT_PADS2_GET_EVT_COND (pads), \ + GST_COLLECT_PADS2_GET_EVT_LOCK (pads), &tv); \ + cookie = ((GstCollectPads2 *) pads)->evt_cookie; \ + 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 */ \ + ++(((GstCollectPads2 *) pads)->evt_cookie); \ + 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)); \ + cookie = ((GstCollectPads2 *) pads)->evt_cookie; \ + g_mutex_unlock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads)); \ +} G_STMT_END + +static void +gst_collect_pads2_base_init (gpointer g_class) +{ + /* Do nothing here */ +} + +static void +gst_collect_pads2_class_init (GstCollectPads2Class * klass) +{ + GObjectClass *gobject_class = (GObjectClass *) klass; + + GST_DEBUG_CATEGORY_INIT (collect_pads2_debug, "collectpads2", 0, + "GstCollectPads2"); + + gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_collect_pads2_finalize); +} + +static void +gst_collect_pads2_init (GstCollectPads2 * pads, GstCollectPads2Class * g_class) +{ + pads->data = NULL; + pads->cookie = 0; + pads->numpads = 0; + pads->queuedpads = 0; + pads->eospads = 0; + pads->started = FALSE; + + g_static_rec_mutex_init (&pads->stream_lock); + + pads->func = gst_collect_pads2_default_collected; + pads->user_data = NULL; + pads->event_func = NULL; + pads->event_user_data = NULL; + + pads->prepare_buffer_func = NULL; + pads->prepare_buffer_user_data = NULL; + + /* members for default muxing */ + pads->buffer_func = NULL; + pads->buffer_user_data = NULL; + pads->compare_func = gst_collect_pads2_default_compare_func; + pads->compare_user_data = NULL; + pads->earliest_data = NULL; + pads->earliest_time = GST_CLOCK_TIME_NONE; + + /* members to manage the pad list */ + pads->pad_cookie = 0; + pads->pad_list = NULL; + + /* members for event */ + pads->evt_lock = g_mutex_new (); + pads->evt_cond = g_cond_new (); + pads->evt_cookie = 0; +} + +static void +gst_collect_pads2_finalize (GObject * object) +{ + GstCollectPads2 *pads = GST_COLLECT_PADS2 (object); + + GST_DEBUG_OBJECT (object, "finalize"); + + g_static_rec_mutex_free (&pads->stream_lock); + + g_cond_free (pads->evt_cond); + g_mutex_free (pads->evt_lock); + + /* Remove pads and free pads list */ + g_slist_foreach (pads->pad_list, (GFunc) unref_data, NULL); + g_slist_foreach (pads->data, (GFunc) unref_data, NULL); + g_slist_free (pads->data); + g_slist_free (pads->pad_list); + + 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. + */ +GstCollectPads2 * +gst_collect_pads2_new (void) +{ + GstCollectPads2 *newcoll; + + newcoll = g_object_new (GST_TYPE_COLLECT_PADS2, NULL); + + return newcoll; +} + +/** + * gst_collect_pads2_set_prepare_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 + * for every buffer that arrives. + * + * MT safe. + */ +void +gst_collect_pads2_set_prepare_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); + pads->prepare_buffer_func = func; + pads->prepare_buffer_user_data = user_data; + GST_OBJECT_UNLOCK (pads); +} + +/* Must be called with GstObject lock! */ +static void +gst_collect_pads2_set_buffer_function_locked (GstCollectPads2 * pads, + GstCollectPads2BufferFunction func, gpointer user_data) +{ + pads->buffer_func = func; + pads->buffer_user_data = user_data; +} + +/** + * 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. + */ +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. + */ +/* 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); + pads->compare_func = func; + pads->compare_user_data = user_data; + 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. + */ +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); + pads->func = func; + pads->user_data = user_data; + gst_collect_pads2_set_buffer_function_locked (pads, NULL, NULL); + GST_OBJECT_UNLOCK (pads); +} + +static void +ref_data (GstCollectData2 * data) +{ + g_assert (data != NULL); + + g_atomic_int_inc (&(data->refcount)); +} + +static void +unref_data (GstCollectData2 * data) +{ + g_assert (data != NULL); + g_assert (data->refcount > 0); + + if (!g_atomic_int_dec_and_test (&(data->refcount))) + return; + + if (data->destroy_notify) + data->destroy_notify (data); + + g_object_unref (data->pad); + if (data->buffer) { + gst_buffer_unref (data->buffer); + } + 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. + */ +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); + pads->event_func = func; + pads->event_user_data = user_data; + GST_OBJECT_UNLOCK (pads); +} + +/** + * 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. + */ +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.12 + * + * 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); + 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; + data->refcount = 1; + data->destroy_notify = destroy_notify; + + GST_OBJECT_LOCK (pads); + GST_OBJECT_LOCK (pad); + gst_pad_set_element_private (pad, data); + GST_OBJECT_UNLOCK (pad); + pads->pad_list = g_slist_append (pads->pad_list, data); + 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. */ + if (!pads->started) { + pads->data = g_slist_append (pads->data, data); + ref_data (data); + } + /* activate the pad when needed */ + if (pads->started) + gst_pad_set_active (pad, TRUE); + pads->pad_cookie++; + 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. + */ +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); + list = g_slist_find_custom (pads->pad_list, pad, (GCompareFunc) find_pad); + 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. */ + if (!pads->started) { + 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 */ + pads->pad_list = g_slist_delete_link (pads->pad_list, list); + pads->pad_cookie++; + + /* signal waiters because something changed */ + GST_COLLECT_PADS2_EVT_BROADCAST (pads); + + /* deactivate the pad when needed */ + if (!pads->started) + 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. + */ +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. + */ +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. + */ +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.7. + */ +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. + */ +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 */ + collected = pads->pad_list; + 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 */ + pads->started = TRUE; + 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. + */ +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 */ + pads->started = FALSE; + pads->eospads = 0; + pads->queuedpads = 0; + + /* loop over the master pad list and flush buffers */ + collected = pads->pad_list; + 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); + } + + if (pads->earliest_data) + unref_data (pads->earliest_data); + pads->earliest_data = NULL; + pads->earliest_time = GST_CLOCK_TIME_NONE; + + 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. + */ +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. + */ +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)) + pads->queuedpads--; + } + + 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. + */ +/* 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 */ + size = GST_BUFFER_SIZE (buffer) - pdata->pos; + 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_read: + * @pads: the collectspads to query + * @data: the data to use + * @bytes: a pointer to a byte array + * @size: the number of bytes to read + * + * Get a pointer in @bytes where @size bytes can be read from the + * given pad data. + * + * This function should be called with @pads STREAM_LOCK held, such as + * in the callback. + * + * Returns: The number of bytes available for consumption in the + * memory pointed to by @bytes. This can be less than @size and + * is 0 if the pad is end-of-stream. + * + * MT safe. + */ +guint +gst_collect_pads2_read (GstCollectPads2 * pads, GstCollectData2 * data, + guint8 ** bytes, guint size) +{ + guint readsize; + 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); + g_return_val_if_fail (bytes != NULL, 0); + + /* no buffer, must be EOS */ + if ((buffer = data->buffer) == NULL) + return 0; + + readsize = MIN (size, GST_BUFFER_SIZE (buffer) - data->pos); + + *bytes = GST_BUFFER_DATA (buffer) + data->pos; + + return readsize; +} + +/** + * 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. + */ +guint +gst_collect_pads2_flush (GstCollectPads2 * pads, GstCollectData2 * data, + guint size) +{ + guint flushsize; + 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; + + /* this is what we can flush at max */ + flushsize = MIN (size, GST_BUFFER_SIZE (buffer) - data->pos); + + data->pos += size; + + if (data->pos >= GST_BUFFER_SIZE (buffer)) + /* _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.18 + * + * 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; + + readsize = MIN (size, GST_BUFFER_SIZE (buffer) - data->pos); + + return gst_buffer_create_sub (buffer, data->pos, readsize); +} + +/** + * 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.18 + * + * 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) { + gst_collect_pads2_flush (pads, data, GST_BUFFER_SIZE (buffer)); + } + 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. + */ +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) + pads->queuedpads--; + else + pads->queuedpads++; + } + + /* 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); + if (G_UNLIKELY (pads->pad_cookie != pads->cookie)) { + GSList *collected; + + /* clear list and stats */ + g_slist_foreach (pads->data, (GFunc) unref_data, NULL); + g_slist_free (pads->data); + pads->data = NULL; + pads->numpads = 0; + pads->queuedpads = 0; + pads->eospads = 0; + if (pads->earliest_data) + unref_data (pads->earliest_data); + pads->earliest_data = NULL; + pads->earliest_time = GST_CLOCK_TIME_NONE; + + /* loop over the master pad list */ + collected = pads->pad_list; + for (; collected; collected = g_slist_next (collected)) { + GstCollectData2 *data; + + /* update the stats */ + pads->numpads++; + data = collected->data; + if (GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_EOS)) + pads->eospads++; + else if (data->buffer || !GST_COLLECT_PADS2_STATE_IS_SET (data, + GST_COLLECT_PADS2_STATE_WAITING)) + pads->queuedpads++; + + /* 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 */ + pads->cookie = pads->pad_cookie; + } + 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); + func = pads->func; + user_data = pads->user_data; + GST_OBJECT_UNLOCK (pads); + + g_return_val_if_fail (pads->func != NULL, GST_FLOW_NOT_SUPPORTED); + + /* check for new pads, update stats etc.. */ + gst_collect_pads2_check_pads (pads); + + if (G_UNLIKELY (pads->eospads == pads->numpads)) { + /* 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", + pads->numpads, GST_DEBUG_FUNCPTR_NAME (func)); + + flow_ret = func (pads, user_data); + } else { + gboolean collected = FALSE; + + /* We call the collected function as long as our condition matches. */ + while (((pads->queuedpads + pads->eospads) >= pads->numpads)) { + GST_DEBUG_OBJECT (pads, "All active pads (%d + %d >= %d) have data, " + "calling %s", pads->queuedpads, pads->eospads, pads->numpads, + 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 */ + if (pads->queuedpads == 0) + break; + } + if (!collected) + GST_DEBUG_OBJECT (pads, "Not all active pads (%d) have data, continuing", + pads->numpads); + } + 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. */ + if (pads->earliest_data == NULL) + 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 */ + cmp_res = pads->compare_func (pads, data, data->segment.start, + pads->earliest_data, pads->earliest_time, pads->compare_user_data); + 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. + */ +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); + if (best == NULL || pads->compare_func (pads, data, timestamp, + best, best_time, pads->compare_user_data) < 0) { + 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) +{ + if (pads->earliest_data) + unref_data (pads->earliest_data); + gst_collect_pads2_find_best_pad (pads, &pads->earliest_data, + &pads->earliest_time); + if (pads->earliest_data) + ref_data (pads->earliest_data); + 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); + func = pads->buffer_func; + buffer_user_data = pads->buffer_user_data; + 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; + } + + best = pads->earliest_data; + + /* No data collected means EOS. */ + if (G_UNLIKELY (best == NULL)) { + ret = func (pads, best, NULL, buffer_user_data); + if (ret == GST_FLOW_OK) + ret = GST_FLOW_UNEXPECTED; + 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); + +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 +gst_collect_pads2_event (GstPad * pad, GstEvent * event) +{ + 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); + event_func = pads->event_func; + event_user_data = pads->event_user_data; + buffer_func = pads->buffer_func; + 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); + if (!res) + res = gst_pad_event_default (pad, event); + + /* 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 */ + if (pads->earliest_data == data) { + unref_data (data); + pads->earliest_data = NULL; + pads->earliest_time = GST_CLOCK_TIME_NONE; + } + } + + 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)) + pads->queuedpads++; + pads->eospads--; + 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)) + pads->queuedpads--; + pads->eospads++; + } + /* 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; + } + case GST_EVENT_NEWSEGMENT: + { + gint64 start, stop, time; + gdouble rate, arate; + GstFormat format; + gboolean update; + gint cmp_res; + + GST_COLLECT_PADS2_STREAM_LOCK (pads); + + gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format, + &start, &stop, &time); + + GST_DEBUG_OBJECT (data->pad, "got newsegment, start %" GST_TIME_FORMAT + ", stop %" GST_TIME_FORMAT, GST_TIME_ARGS (start), + GST_TIME_ARGS (stop)); + + gst_segment_set_newsegment_full (&data->segment, update, rate, arate, + format, start, stop, time); + + 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 */ + if (format != GST_FORMAT_TIME) { + 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 */ + if (!pads->earliest_data || pads->earliest_data == data) { + gst_collect_pads2_recalculate_full (pads); + goto newsegment_done; + } + + /* Check if the waiting state of the pad should change. */ + cmp_res = pads->compare_func (pads, data, start, pads->earliest_data, + pads->earliest_time, pads->compare_user_data); + + if (cmp_res > 0) + /* Stop waiting */ + gst_collect_pads2_set_waiting (pads, data, FALSE); + + newsegment_done: + GST_COLLECT_PADS2_STREAM_UNLOCK (pads); + /* we must not forward this event since multiple segments will be + * accumulated and this is certainly not what we want. */ + goto forward_or_eat; + } + default: + /* forward other events */ + goto forward_or_default; + } + +forward_or_default: + if (GST_EVENT_IS_SERIALIZED (event)) { + GST_COLLECT_PADS2_STREAM_LOCK (pads); + need_unlock = TRUE; + } + if (event_func) + res = event_func (pads, data, event, event_user_data); + if (!res) + res = gst_pad_event_default (pad, event); + if (need_unlock) + GST_COLLECT_PADS2_STREAM_UNLOCK (pads); + goto done; + +forward_or_eat: + if (GST_EVENT_IS_SERIALIZED (event)) { + GST_COLLECT_PADS2_STREAM_LOCK (pads); + need_unlock = TRUE; + } + if (event_func) + res = event_func (pads, data, event, event_user_data); + if (!res) { + gst_event_unref (event); + res = TRUE; + } + if (need_unlock) + GST_COLLECT_PADS2_STREAM_UNLOCK (pads); + goto done; + +done: + unref_data (data); + return res; + + /* ERRORS */ +pad_removed: + { + GST_DEBUG ("%s got removed from collectpads", GST_OBJECT_NAME (pad)); + GST_OBJECT_UNLOCK (pad); + return FALSE; + } +} + +/* For each buffer we receive we check if our collected condition is reached + * and if so we call the collected function. When this is done we check if + * data has been unqueued. If data is still queued we wait holding the stream + * lock to make sure no EOS event can happen while we are ready to be + * collected + */ +static GstFlowReturn +gst_collect_pads2_chain (GstPad * pad, GstBuffer * buffer) +{ + GstCollectData2 *data; + GstCollectPads2 *pads; + guint64 size; + GstFlowReturn ret; + GstBuffer **buffer_p; + guint32 cookie; + GstCollectPads2BufferFunction prepare_buffer_func; + gpointer prepare_buffer_user_data; + + GST_DEBUG ("Got buffer for pad %s:%s", GST_DEBUG_PAD_NAME (pad)); + + /* 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 no_data; + ref_data (data); + GST_OBJECT_UNLOCK (pad); + + pads = data->collect; + GST_OBJECT_LOCK (pads); + prepare_buffer_func = pads->prepare_buffer_func; + prepare_buffer_user_data = pads->prepare_buffer_user_data; + GST_OBJECT_UNLOCK (pads); + + size = GST_BUFFER_SIZE (buffer); + + GST_COLLECT_PADS2_STREAM_LOCK (pads); + /* if not started, bail out */ + if (G_UNLIKELY (!pads->started)) + goto not_started; + /* check if this pad is flushing */ + if (G_UNLIKELY (GST_COLLECT_PADS2_STATE_IS_SET (data, + GST_COLLECT_PADS2_STATE_FLUSHING))) + goto flushing; + /* pad was EOS, we can refuse this data */ + if (G_UNLIKELY (GST_COLLECT_PADS2_STATE_IS_SET (data, + GST_COLLECT_PADS2_STATE_EOS))) + goto unexpected; + + GST_DEBUG_OBJECT (pads, "Queuing buffer %p for pad %s:%s", buffer, + GST_DEBUG_PAD_NAME (pad)); + + if (prepare_buffer_func) { + ret = prepare_buffer_func (pads, data, buffer, prepare_buffer_user_data); + if (ret == GST_COLLECT_PADS2_FLOW_DROP) { + GST_DEBUG_OBJECT (pads, "Dropping buffer as requested"); + ret = GST_FLOW_OK; + goto unlock_done; + } else if (ret == GST_FLOW_UNEXPECTED) { + goto unexpected; + } else if (ret != GST_FLOW_OK) { + goto error; + } + } + + /* One more pad has data queued */ + if (GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_WAITING)) + pads->queuedpads++; + buffer_p = &data->buffer; + gst_buffer_replace (buffer_p, buffer); + + /* update segment last position if in TIME */ + if (G_LIKELY (data->segment.format == GST_FORMAT_TIME)) { + GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer); + + if (GST_CLOCK_TIME_IS_VALID (timestamp)) + gst_segment_set_last_stop (&data->segment, GST_FORMAT_TIME, timestamp); + } + + /* While we have data queued on this pad try to collect stuff */ + do { + /* Check if our collected condition is matched and call the collected + * function if it is */ + ret = gst_collect_pads2_check_collected (pads); + /* when an error occurs, we want to report this back to the caller ASAP + * without having to block if the buffer was not popped */ + if (G_UNLIKELY (ret != GST_FLOW_OK)) + goto error; + + /* data was consumed, we can exit and accept new data */ + if (data->buffer == NULL) + break; + + /* Having the _INIT here means we don't care about any broadcast up to here + * (most of which occur with STREAM_LOCK held, so could not have happened + * anyway). We do care about e.g. a remove initiated broadcast as of this + * point. Putting it here also makes this thread ignores any evt it raised + * itself (as is a usual WAIT semantic). + */ + GST_COLLECT_PADS2_EVT_INIT (cookie); + + /* pad could be removed and re-added */ + unref_data (data); + GST_OBJECT_LOCK (pad); + if (G_UNLIKELY ((data = gst_pad_get_element_private (pad)) == NULL)) + goto pad_removed; + ref_data (data); + GST_OBJECT_UNLOCK (pad); + + GST_DEBUG_OBJECT (pads, "Pad %s:%s has a buffer queued, waiting", + GST_DEBUG_PAD_NAME (pad)); + + /* wait to be collected, this must happen from another thread triggered + * by the _chain function of another pad. We release the lock so we + * can get stopped or flushed as well. We can however not get EOS + * because we still hold the STREAM_LOCK. + */ + GST_COLLECT_PADS2_STREAM_UNLOCK (pads); + GST_COLLECT_PADS2_EVT_WAIT (pads, cookie); + GST_COLLECT_PADS2_STREAM_LOCK (pads); + + GST_DEBUG_OBJECT (pads, "Pad %s:%s resuming", GST_DEBUG_PAD_NAME (pad)); + + /* after a signal, we could be stopped */ + if (G_UNLIKELY (!pads->started)) + goto not_started; + /* check if this pad is flushing */ + if (G_UNLIKELY (GST_COLLECT_PADS2_STATE_IS_SET (data, + GST_COLLECT_PADS2_STATE_FLUSHING))) + goto flushing; + } + while (data->buffer != NULL); + +unlock_done: + GST_COLLECT_PADS2_STREAM_UNLOCK (pads); + unref_data (data); + gst_buffer_unref (buffer); + return ret; + +pad_removed: + { + GST_WARNING ("%s got removed from collectpads", GST_OBJECT_NAME (pad)); + GST_OBJECT_UNLOCK (pad); + ret = GST_FLOW_NOT_LINKED; + goto unlock_done; + } + /* ERRORS */ +no_data: + { + GST_DEBUG ("%s got removed from collectpads", GST_OBJECT_NAME (pad)); + GST_OBJECT_UNLOCK (pad); + gst_buffer_unref (buffer); + return GST_FLOW_NOT_LINKED; + } +not_started: + { + GST_DEBUG ("not started"); + gst_collect_pads2_clear (pads, data); + ret = GST_FLOW_WRONG_STATE; + goto unlock_done; + } +flushing: + { + GST_DEBUG ("pad %s:%s is flushing", GST_DEBUG_PAD_NAME (pad)); + gst_collect_pads2_clear (pads, data); + ret = GST_FLOW_WRONG_STATE; + goto unlock_done; + } +unexpected: + { + /* we should not post an error for this, just inform upstream that + * we don't expect anything anymore */ + GST_DEBUG ("pad %s:%s is eos", GST_DEBUG_PAD_NAME (pad)); + ret = GST_FLOW_UNEXPECTED; + goto unlock_done; + } +error: + { + /* we print the error, the element should post a reasonable error + * message for fatal errors */ + GST_DEBUG ("collect failed, reason %d (%s)", ret, gst_flow_get_name (ret)); + gst_collect_pads2_clear (pads, data); + goto unlock_done; + } +} diff --git a/gst/videomixer/gstcollectpads2.h b/gst/videomixer/gstcollectpads2.h new file mode 100644 index 0000000000..3c10448c45 --- /dev/null +++ b/gst/videomixer/gstcollectpads2.h @@ -0,0 +1,336 @@ +/* GStreamer + * Copyright (C) 2005 Wim Taymans + * Copyright (C) 2008 Mark Nauwelaerts + * + * gstcollectpads2.h: + * + * 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. + */ + +#ifndef __GST_COLLECT_PADS2_H__ +#define __GST_COLLECT_PADS2_H__ + +#include + +G_BEGIN_DECLS + +#define GST_TYPE_COLLECT_PADS2 (gst_collect_pads2_get_type()) +#define GST_COLLECT_PADS2(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_COLLECT_PADS2,GstCollectPads2)) +#define GST_COLLECT_PADS2_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_COLLECT_PADS2,GstCollectPads2Class)) +#define GST_COLLECT_PADS2_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj),GST_TYPE_COLLECT_PADS2,GstCollectPads2Class)) +#define GST_IS_COLLECT_PADS2(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_COLLECT_PADS2)) +#define GST_IS_COLLECT_PADS2_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_COLLECT_PADS2)) + +typedef struct _GstCollectData2 GstCollectData2; +typedef struct _GstCollectPads2 GstCollectPads2; +typedef struct _GstCollectPads2Class GstCollectPads2Class; + +/** + * GstCollectData2DestroyNotify: + * @data: the #GstCollectData2 that will be freed + * + * A function that will be called when the #GstCollectData2 will be freed. + * It is passed the pointer to the structure and should free any custom + * memory and resources allocated for it. + * + * Since: 0.10.12 + */ +typedef void (*GstCollectData2DestroyNotify) (GstCollectData2 *data); + +/** + * GstCollectPads2StateFlags: + * @GST_COLLECT_PADS2_STATE_EOS: Set if collectdata's pad is EOS. + * @GST_COLLECT_PADS2_STATE_FLUSHING: Set if collectdata's pad is flushing. + * @GST_COLLECT_PADS2_STATE_NEW_SEGMENT: Set if collectdata's pad received a + * new_segment event. + * @GST_COLLECT_PADS2_STATE_WAITING: Set if collectdata's pad must be waited + * for when collecting. + * @GST_COLLECT_PADS2_STATE_LOCKED: Set collectdata's pad WAITING state must + * not be changed. + * #GstCollectPads2StateFlags indicate private state of a collectdata('s pad). + */ +typedef enum { + GST_COLLECT_PADS2_STATE_EOS = 1 << 0, + GST_COLLECT_PADS2_STATE_FLUSHING = 1 << 1, + GST_COLLECT_PADS2_STATE_NEW_SEGMENT = 1 << 2, + GST_COLLECT_PADS2_STATE_WAITING = 1 << 3, + GST_COLLECT_PADS2_STATE_LOCKED = 1 << 4 +} GstCollectPads2StateFlags; + +/** + * GST_COLLECT_PADS2_STATE: + * @data: a #GstCollectData2. + * + * A flags word containing #GstCollectPads2StateFlags flags set + * on this collected pad. + */ +#define GST_COLLECT_PADS2_STATE(data) (((GstCollectData2 *) data)->state) +/** + * GST_COLLECT_PADS2_STATE_IS_SET: + * @data: a #GstCollectData2. + * @flag: the #GstCollectPads2StateFlags to check. + * + * Gives the status of a specific flag on a collected pad. + */ +#define GST_COLLECT_PADS2_STATE_IS_SET(data,flag) !!(GST_COLLECT_PADS2_STATE (data) & flag) +/** + * GST_COLLECT_PADS2_STATE_SET: + * @data: a #GstCollectData2. + * @flag: the #GstCollectPads2StateFlags to set. + * + * Sets a state flag on a collected pad. + */ +#define GST_COLLECT_PADS2_STATE_SET(data,flag) (GST_COLLECT_PADS2_STATE (data) |= flag) +/** + * GST_COLLECT_PADS2_STATE_UNSET: + * @data: a #GstCollectData2. + * @flag: the #GstCollectPads2StateFlags to clear. + * + * Clears a state flag on a collected pad. + */ +#define GST_COLLECT_PADS2_STATE_UNSET(data,flag) (GST_COLLECT_PADS2_STATE (data) &= ~(flag)) + +#define GST_COLLECT_PADS2_FLOW_DROP GST_FLOW_CUSTOM_SUCCESS + +/** + * GstCollectData2: + * @collect: owner #GstCollectPads2 + * @pad: #GstPad managed by this data + * @buffer: currently queued buffer. + * @pos: position in the buffer + * @segment: last segment received. + * + * Structure used by the collect_pads2. + */ +struct _GstCollectData2 +{ + /* with STREAM_LOCK of @collect */ + GstCollectPads2 *collect; + GstPad *pad; + GstBuffer *buffer; + guint pos; + GstSegment segment; + + /*< private >*/ + /* state: bitfield for easier extension; + * eos, flushing, new_segment, waiting */ + guint state; + + /* refcounting for struct, and destroy callback */ + GstCollectData2DestroyNotify destroy_notify; + gint refcount; + + gpointer _gst_reserved[GST_PADDING]; +}; + +/** + * GstCollectPads2Function: + * @pads: the #GstCollectPads2 that trigered the callback + * @user_data: user data passed to gst_collect_pads2_set_function() + * + * A function that will be called when all pads have received data. + * + * Returns: #GST_FLOW_OK for success + */ +typedef GstFlowReturn (*GstCollectPads2Function) (GstCollectPads2 *pads, gpointer user_data); + +/** + * GstCollectPads2BufferFunction: + * @pads: the #GstCollectPads2 that trigered the callback + * @data: the #GstCollectData2 of pad that has received the buffer + * @buffer: the #GstBuffer + * @user_data: user data passed to gst_collect_pads2_set_buffer_function() + * + * A function that will be called when a (considered oldest) buffer can be muxed. + * If all pads have reached EOS, this function is called with NULL @buffer + * and NULL @data. + * + * Returns: #GST_FLOW_OK for success + */ +typedef GstFlowReturn (*GstCollectPads2BufferFunction) (GstCollectPads2 *pads, GstCollectData2 *data, + GstBuffer *buffer, gpointer user_data); + +/** + * GstCollectPads2CompareFunction: + * @pads: the #GstCollectPads that is comparing the timestamps + * @data1: the first #GstCollectData2 + * @timestamp1: the first timestamp + * @data2: the second #GstCollectData2 + * @timestamp2: the second timestamp + * @user_data: user data passed to gst_collect_pads2_set_compare_function() + * + * A function for comparing two timestamps of buffers or newsegments collected on one pad. + * + * Returns: Integer less than zero when first timestamp is deemed older than the second one. + * Zero if the timestamps are deemed equally old. + * Integer greate than zero when second timestamp is deemed older than the first one. + */ +typedef gint (*GstCollectPads2CompareFunction) (GstCollectPads2 *pads, + GstCollectData2 * data1, GstClockTime timestamp1, + GstCollectData2 * data2, GstClockTime timestamp2, + gpointer user_data); + +/** + * GstCollectPads2EventFunction: + * @pads: the #GstCollectPads2 that trigered the callback + * @pad: the #GstPad that received an event + * @event: the #GstEvent received + * @user_data: user data passed to gst_collect_pads2_set_event_function() + * + * A function that will be called after collectpads has processed the event. + * + * Returns: %TRUE if the pad could handle the event + */ +typedef gboolean (*GstCollectPads2EventFunction) (GstCollectPads2 *pads, GstCollectData2 * pad, + GstEvent * event, gpointer user_data); + +/** + * GST_COLLECT_PADS2_GET_STREAM_LOCK: + * @pads: a #GstCollectPads2 + * + * Get the stream lock of @pads. The stream lock is used to coordinate and + * serialize execution among the various streams being collected, and in + * protecting the resources used to accomplish this. + */ +#define GST_COLLECT_PADS2_GET_STREAM_LOCK(pads) (&((GstCollectPads2 *)pads)->stream_lock) +/** + * GST_COLLECT_PADS2_STREAM_LOCK: + * @pads: a #GstCollectPads2 + * + * Lock the stream lock of @pads. + */ +#define GST_COLLECT_PADS2_STREAM_LOCK(pads) (g_static_rec_mutex_lock(GST_COLLECT_PADS2_GET_STREAM_LOCK (pads))) +/** + * GST_COLLECT_PADS2_STREAM_UNLOCK: + * @pads: a #GstCollectPads2 + * + * Unlock the stream lock of @pads. + */ +#define GST_COLLECT_PADS2_STREAM_UNLOCK(pads) (g_static_rec_mutex_unlock(GST_COLLECT_PADS2_GET_STREAM_LOCK (pads))) + +/** + * GstCollectPads2: + * @data: #GList of #GstCollectData2 managed by this #GstCollectPads2. + * + * Collectpads object. + */ +struct _GstCollectPads2 { + GstObject object; + + /*< public >*/ /* with LOCK and/or STREAM_LOCK */ + GSList *data; /* list of CollectData items */ + + /*< private >*/ + GStaticRecMutex stream_lock; /* used to serialize collection among several streams */ + /* 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 prepare_buffer_func; /* function and user_data for prepare buffer callback */ + gpointer prepare_buffer_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; + + /* no other lock needed */ + GMutex *evt_lock; /* these make up sort of poor man's event signaling */ + GCond *evt_cond; + guint32 evt_cookie; + + gpointer _gst_reserved[GST_PADDING + 0]; + +}; + +struct _GstCollectPads2Class { + GstObjectClass parent_class; + + /*< private >*/ + gpointer _gst_reserved[GST_PADDING]; +}; + +GType gst_collect_pads2_get_type(void); + +/* creating the object */ +GstCollectPads2* gst_collect_pads2_new (void); + +/* set the callbacks */ +void gst_collect_pads2_set_function (GstCollectPads2 *pads, GstCollectPads2Function func, + gpointer user_data); +void gst_collect_pads2_set_prepare_buffer_function (GstCollectPads2 *pads, + GstCollectPads2BufferFunction func, gpointer user_data); +void gst_collect_pads2_set_buffer_function (GstCollectPads2 *pads, + GstCollectPads2BufferFunction func, gpointer user_data); +void gst_collect_pads2_set_event_function (GstCollectPads2 *pads, + GstCollectPads2EventFunction func, gpointer user_data); +void gst_collect_pads2_set_compare_function (GstCollectPads2 *pads, + GstCollectPads2CompareFunction func, gpointer user_data); + +/* pad management */ +GstCollectData2* gst_collect_pads2_add_pad (GstCollectPads2 *pads, GstPad *pad, guint size); +GstCollectData2* gst_collect_pads2_add_pad_full (GstCollectPads2 *pads, GstPad *pad, guint size, GstCollectData2DestroyNotify destroy_notify, + gboolean lock); +gboolean gst_collect_pads2_remove_pad (GstCollectPads2 *pads, GstPad *pad); +gboolean gst_collect_pads2_is_active (GstCollectPads2 *pads, GstPad *pad); +void gst_mux_pads_set_locked (GstCollectPads2 *pads, GstCollectData2 *data, + gboolean locked); + +/* start/stop collection */ +GstFlowReturn gst_collect_pads2_collect (GstCollectPads2 *pads); +GstFlowReturn gst_collect_pads2_collect_range (GstCollectPads2 *pads, guint64 offset, guint length); + +void gst_collect_pads2_start (GstCollectPads2 *pads); +void gst_collect_pads2_stop (GstCollectPads2 *pads); +void gst_collect_pads2_set_flushing (GstCollectPads2 *pads, gboolean flushing); + +/* get collected buffers */ +GstBuffer* gst_collect_pads2_peek (GstCollectPads2 *pads, GstCollectData2 *data); +GstBuffer* gst_collect_pads2_pop (GstCollectPads2 *pads, GstCollectData2 *data); + +/* get collected bytes */ +guint gst_collect_pads2_available (GstCollectPads2 *pads); +guint gst_collect_pads2_read (GstCollectPads2 *pads, GstCollectData2 *data, + guint8 **bytes, guint size); +guint gst_collect_pads2_flush (GstCollectPads2 *pads, GstCollectData2 *data, + guint size); +GstBuffer* gst_collect_pads2_read_buffer (GstCollectPads2 * pads, GstCollectData2 * data, + guint size); +GstBuffer* gst_collect_pads2_take_buffer (GstCollectPads2 * pads, GstCollectData2 * data, + guint size); + +/* setting and unsetting waiting mode */ +void gst_collect_pads2_set_waiting (GstCollectPads2 *pads, GstCollectData2 *data, + gboolean waiting); + + +G_END_DECLS + +#endif /* __GST_COLLECT_PADS22_H__ */ diff --git a/gst/videomixer/videomixer.c b/gst/videomixer/videomixer.c index c96aec6bb4..b87efac54f 100644 --- a/gst/videomixer/videomixer.c +++ b/gst/videomixer/videomixer.c @@ -85,6 +85,7 @@ #endif #include "videomixer.h" +#include "videomixer2.h" GST_DEBUG_CATEGORY_STATIC (gst_videomixer_debug); #define GST_CAT_DEFAULT gst_videomixer_debug @@ -1855,7 +1856,7 @@ plugin_init (GstPlugin * plugin) gst_video_mixer_init_blend (); return gst_element_register (plugin, "videomixer", GST_RANK_PRIMARY, - GST_TYPE_VIDEO_MIXER); + GST_TYPE_VIDEO_MIXER) && gst_videomixer2_register (plugin); } GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, diff --git a/gst/videomixer/videomixer2.c b/gst/videomixer/videomixer2.c new file mode 100644 index 0000000000..8c9cbc042f --- /dev/null +++ b/gst/videomixer/videomixer2.c @@ -0,0 +1,1951 @@ +/* Generic video mixer plugin + * Copyright (C) 2004, 2008 Wim Taymans + * Copyright (C) 2010 Sebastian Dröge + * + * 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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include "videomixer2.h" + +#include + +GST_DEBUG_CATEGORY_STATIC (gst_videomixer2_debug); +#define GST_CAT_DEFAULT gst_videomixer2_debug + +#define GST_VIDEO_MIXER2_GET_LOCK(mix) \ + (GST_VIDEO_MIXER2(mix)->lock) +#define GST_VIDEO_MIXER2_LOCK(mix) \ + (g_mutex_lock(GST_VIDEO_MIXER2_GET_LOCK (mix))) +#define GST_VIDEO_MIXER2_UNLOCK(mix) \ + (g_mutex_unlock(GST_VIDEO_MIXER2_GET_LOCK (mix))) + +static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";" GST_VIDEO_CAPS_BGRA ";" + GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_ABGR ";" + GST_VIDEO_CAPS_YUV ("Y444") ";" GST_VIDEO_CAPS_YUV ("Y42B") ";" + GST_VIDEO_CAPS_YUV ("YUY2") ";" GST_VIDEO_CAPS_YUV ("UYVY") ";" + GST_VIDEO_CAPS_YUV ("YVYU") ";" + GST_VIDEO_CAPS_YUV ("I420") ";" GST_VIDEO_CAPS_YUV ("YV12") ";" + GST_VIDEO_CAPS_YUV ("Y41B") ";" GST_VIDEO_CAPS_RGB ";" + GST_VIDEO_CAPS_BGR ";" GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR ";" + GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_BGRx) + ); + +static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d", + GST_PAD_SINK, + GST_PAD_REQUEST, + GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";" GST_VIDEO_CAPS_BGRA ";" + GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_ABGR ";" + GST_VIDEO_CAPS_YUV ("Y444") ";" GST_VIDEO_CAPS_YUV ("Y42B") ";" + GST_VIDEO_CAPS_YUV ("YUY2") ";" GST_VIDEO_CAPS_YUV ("UYVY") ";" + GST_VIDEO_CAPS_YUV ("YVYU") ";" + GST_VIDEO_CAPS_YUV ("I420") ";" GST_VIDEO_CAPS_YUV ("YV12") ";" + GST_VIDEO_CAPS_YUV ("Y41B") ";" GST_VIDEO_CAPS_RGB ";" + GST_VIDEO_CAPS_BGR ";" GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR ";" + GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_BGRx) + ); + +static void gst_videomixer2_child_proxy_init (gpointer g_iface, + gpointer iface_data); +static gboolean gst_videomixer2_push_sink_event (GstVideoMixer2 * mix, + GstEvent * event); +static void gst_videomixer2_release_pad (GstElement * element, GstPad * pad); +static void gst_videomixer2_reset_qos (GstVideoMixer2 * mix); + +static void +_do_init (GType object_type) +{ + static const GInterfaceInfo child_proxy_info = { + (GInterfaceInitFunc) gst_videomixer2_child_proxy_init, + NULL, + NULL + }; + + g_type_add_interface_static (object_type, GST_TYPE_CHILD_PROXY, + &child_proxy_info); +} + +/* GstVideoMixer2Pad */ +#define GST_TYPE_VIDEO_MIXER2_PAD (gst_videomixer2_pad_get_type()) +#define GST_VIDEO_MIXER2_PAD(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VIDEO_MIXER2_PAD, GstVideoMixer2Pad)) +#define GST_VIDEO_MIXER2_PAD_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VIDEO_MIXER_PAD, GstVideoMixer2PadClass)) +#define GST_IS_VIDEO_MIXER2_PAD(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VIDEO_MIXER2_PAD)) +#define GST_IS_VIDEO_MIXER2_PAD_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VIDEO_MIXER2_PAD)) + +typedef struct _GstVideoMixer2Pad GstVideoMixer2Pad; +typedef struct _GstVideoMixer2PadClass GstVideoMixer2PadClass; +typedef struct _GstVideoMixer2Collect GstVideoMixer2Collect; + +struct _GstVideoMixer2Collect +{ + GstCollectData2 collect; /* we extend the CollectData */ + + GstVideoMixer2Pad *mixpad; + + GstBuffer *queued; /* buffer for which we don't know the end time yet */ + + GstBuffer *buffer; /* buffer that should be blended now */ + GstClockTime start_time; + GstClockTime end_time; +}; + +/* all information needed for one video stream */ +struct _GstVideoMixer2Pad +{ + GstPad parent; /* subclass the pad */ + + /* caps */ + gint width, height; + gint fps_n; + gint fps_d; + + /* properties */ + gint xpos, ypos; + guint zorder; + gdouble alpha; + + GstVideoMixer2Collect *mixcol; +}; + +struct _GstVideoMixer2PadClass +{ + GstPadClass parent_class; +}; + +#define DEFAULT_PAD_ZORDER 0 +#define DEFAULT_PAD_XPOS 0 +#define DEFAULT_PAD_YPOS 0 +#define DEFAULT_PAD_ALPHA 1.0 +enum +{ + PROP_PAD_0, + PROP_PAD_ZORDER, + PROP_PAD_XPOS, + PROP_PAD_YPOS, + PROP_PAD_ALPHA +}; + +GType gst_videomixer2_pad_get_type (void); +G_DEFINE_TYPE (GstVideoMixer2Pad, gst_videomixer2_pad, GST_TYPE_PAD); + +static void +gst_videomixer2_collect_free (GstCollectData2 * data) +{ + GstVideoMixer2Collect *cdata = (GstVideoMixer2Collect *) data; + + gst_buffer_replace (&cdata->buffer, NULL); +} + +static gboolean +gst_videomixer2_update_src_caps (GstVideoMixer2 * mix) +{ + GSList *l; + gint best_width = -1, best_height = -1; + gdouble best_fps = -1, cur_fps; + gint best_fps_n = -1, best_fps_d = -1; + gboolean ret = TRUE; + + GST_VIDEO_MIXER2_LOCK (mix); + + for (l = mix->sinkpads; l; l = l->next) { + GstVideoMixer2Pad *mpad = l->data; + gint this_width, this_height; + + if (mpad->fps_n == 0 || mpad->fps_d == 0 || + mpad->width == 0 || mpad->height == 0) + continue; + + this_width = mpad->width + MAX (mpad->xpos, 0); + this_height = mpad->height + MAX (mpad->ypos, 0); + + if (best_width < this_width) + best_width = this_width; + if (best_height < this_height) + best_height = this_height; + + if (mpad->fps_d == 0) + cur_fps = 0.0; + else + gst_util_fraction_to_double (mpad->fps_n, mpad->fps_d, &cur_fps); + if (best_fps < cur_fps) { + best_fps = cur_fps; + best_fps_n = mpad->fps_n; + best_fps_d = mpad->fps_d; + } + } + + if (best_fps_n <= 0 && best_fps_d <= 0) { + best_fps_n = 25; + best_fps_d = 1; + best_fps = 25.0; + } + + if (best_width > 0 && best_height > 0 && best_fps > 0) { + GstCaps *caps, *peercaps; + GstStructure *s; + + if (mix->fps_n != best_fps_n || mix->fps_d != best_fps_d) { + if (mix->segment.last_stop != -1) { + mix->ts_offset = mix->segment.last_stop - mix->segment.start; + mix->nframes = 0; + } + } + + caps = gst_video_format_new_caps (mix->format, + best_width, best_height, best_fps_n, best_fps_d, + mix->par_n, mix->par_d); + + peercaps = gst_pad_peer_get_caps (mix->srcpad); + if (peercaps) { + GstCaps *tmp; + + s = gst_caps_get_structure (caps, 0); + gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT, "height", + GST_TYPE_INT_RANGE, 1, G_MAXINT, "framerate", GST_TYPE_FRACTION_RANGE, + 0, 1, G_MAXINT, 1, NULL); + + tmp = gst_caps_intersect (caps, peercaps); + gst_caps_unref (caps); + gst_caps_unref (peercaps); + caps = tmp; + if (gst_caps_is_empty (caps)) { + GST_VIDEO_MIXER2_UNLOCK (mix); + goto done; + } + + gst_caps_truncate (caps); + s = gst_caps_get_structure (caps, 0); + gst_structure_fixate_field_nearest_int (s, "width", best_width); + gst_structure_fixate_field_nearest_int (s, "height", best_height); + gst_structure_fixate_field_nearest_fraction (s, "framerate", best_fps_n, + best_fps_d); + + gst_structure_get_int (s, "width", &best_width); + gst_structure_get_int (s, "height", &best_height); + gst_structure_get_fraction (s, "fraction", &best_fps_n, &best_fps_d); + } + + mix->fps_n = best_fps_n; + mix->fps_d = best_fps_d; + mix->width = best_width; + mix->height = best_height; + + GST_VIDEO_MIXER2_UNLOCK (mix); + ret = gst_pad_set_caps (mix->srcpad, caps); + gst_caps_unref (caps); + } else { + GST_VIDEO_MIXER2_UNLOCK (mix); + } + +done: + return ret; +} + + +static gboolean +gst_videomixer2_pad_sink_setcaps (GstPad * pad, GstCaps * caps) +{ + GstVideoMixer2 *mix; + GstVideoMixer2Pad *mixpad; + GstVideoFormat fmt; + gint width, height; + gint fps_n = 0, fps_d = 0; + gint par_n = 1, par_d = 1; + gboolean ret = FALSE; + GstStructure *s; + + GST_INFO_OBJECT (pad, "Setting caps %" GST_PTR_FORMAT, caps); + + mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad)); + mixpad = GST_VIDEO_MIXER2_PAD (pad); + + if (!gst_video_format_parse_caps (caps, &fmt, &width, &height) || + !gst_video_parse_caps_pixel_aspect_ratio (caps, &par_n, &par_d)) { + GST_ERROR_OBJECT (pad, "Failed to parse caps"); + goto beach; + } + + s = gst_caps_get_structure (caps, 0); + if (gst_structure_has_field (s, "framerate") + && !gst_video_parse_caps_framerate (caps, &fps_n, &fps_d)) { + GST_ERROR_OBJECT (pad, "Failed to parse caps"); + goto beach; + } + + GST_VIDEO_MIXER2_LOCK (mix); + if (mix->format != GST_VIDEO_FORMAT_UNKNOWN) { + if (mix->format != fmt || mix->par_n != par_n || mix->par_d != par_d) { + GST_ERROR_OBJECT (pad, "Caps not compatible with other pads' caps"); + GST_VIDEO_MIXER2_UNLOCK (mix); + goto beach; + } + } + + mix->format = fmt; + mix->par_n = par_n; + mix->par_d = par_d; + mixpad->fps_n = fps_n; + mixpad->fps_d = fps_d; + mixpad->width = width; + mixpad->height = height; + + GST_VIDEO_MIXER2_UNLOCK (mix); + + ret = gst_videomixer2_update_src_caps (mix); + +beach: + gst_object_unref (mix); + + return ret; +} + +static GstCaps * +gst_videomixer2_pad_sink_getcaps (GstPad * pad) +{ + GstVideoMixer2 *mix; + GstVideoMixer2Pad *mixpad; + GstCaps *srccaps; + GstStructure *s; + gint i, n; + + mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad)); + mixpad = GST_VIDEO_MIXER2_PAD (pad); + + srccaps = gst_pad_get_fixed_caps_func (GST_PAD (mix->srcpad)); + srccaps = gst_caps_make_writable (srccaps); + + n = gst_caps_get_size (srccaps); + for (i = 0; i < n; i++) { + s = gst_caps_get_structure (srccaps, i); + gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT, + "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, + "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL); + if (!gst_structure_has_field (s, "pixel-aspect-ratio")) + gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1, + NULL); + } + + GST_DEBUG_OBJECT (pad, "Returning %" GST_PTR_FORMAT, srccaps); + + return srccaps; +} + +static gboolean +gst_videomixer2_pad_sink_acceptcaps (GstPad * pad, GstCaps * caps) +{ + gboolean ret; + GstVideoMixer2 *mix; + GstCaps *accepted_caps; + gint i, n; + GstStructure *s; + + mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad)); + GST_DEBUG_OBJECT (pad, "%" GST_PTR_FORMAT, caps); + + accepted_caps = gst_pad_get_fixed_caps_func (GST_PAD (mix->srcpad)); + accepted_caps = gst_caps_make_writable (accepted_caps); + GST_LOG_OBJECT (pad, "src caps %" GST_PTR_FORMAT, accepted_caps); + + n = gst_caps_get_size (accepted_caps); + for (i = 0; i < n; i++) { + s = gst_caps_get_structure (accepted_caps, i); + gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT, + "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, + "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL); + if (!gst_structure_has_field (s, "pixel-aspect-ratio")) + gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1, + NULL); + } + + ret = gst_caps_can_intersect (caps, accepted_caps); + GST_INFO_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT, (ret ? "" : "not "), + caps); + GST_INFO_OBJECT (pad, "acceptable caps are %" GST_PTR_FORMAT, accepted_caps); + + gst_caps_unref (accepted_caps); + + gst_object_unref (mix); + return ret; +} + +static void +gst_videomixer2_pad_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (object); + + switch (prop_id) { + case PROP_PAD_ZORDER: + g_value_set_uint (value, pad->zorder); + break; + case PROP_PAD_XPOS: + g_value_set_int (value, pad->xpos); + break; + case PROP_PAD_YPOS: + g_value_set_int (value, pad->ypos); + break; + case PROP_PAD_ALPHA: + g_value_set_double (value, pad->alpha); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static int +pad_zorder_compare (const GstVideoMixer2Pad * pad1, + const GstVideoMixer2Pad * pad2) +{ + return pad1->zorder - pad2->zorder; +} + +static void +gst_videomixer2_pad_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (object); + GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (GST_PAD (pad))); + + switch (prop_id) { + case PROP_PAD_ZORDER: + GST_VIDEO_MIXER2_LOCK (mix); + pad->zorder = g_value_get_uint (value); + + mix->sinkpads = g_slist_sort (mix->sinkpads, + (GCompareFunc) pad_zorder_compare); + GST_VIDEO_MIXER2_UNLOCK (mix); + break; + case PROP_PAD_XPOS: + pad->xpos = g_value_get_int (value); + break; + case PROP_PAD_YPOS: + pad->ypos = g_value_get_int (value); + break; + case PROP_PAD_ALPHA: + pad->alpha = g_value_get_double (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } + + gst_object_unref (mix); +} + +static void +gst_videomixer2_pad_class_init (GstVideoMixer2PadClass * klass) +{ + GObjectClass *gobject_class = (GObjectClass *) klass; + + gobject_class->set_property = gst_videomixer2_pad_set_property; + gobject_class->get_property = gst_videomixer2_pad_get_property; + + g_object_class_install_property (gobject_class, PROP_PAD_ZORDER, + g_param_spec_uint ("zorder", "Z-Order", "Z Order of the picture", + 0, 10000, DEFAULT_PAD_ZORDER, + G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS)); + g_object_class_install_property (gobject_class, PROP_PAD_XPOS, + g_param_spec_int ("xpos", "X Position", "X Position of the picture", + G_MININT, G_MAXINT, DEFAULT_PAD_XPOS, + G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS)); + g_object_class_install_property (gobject_class, PROP_PAD_YPOS, + g_param_spec_int ("ypos", "Y Position", "Y Position of the picture", + G_MININT, G_MAXINT, DEFAULT_PAD_YPOS, + G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS)); + g_object_class_install_property (gobject_class, PROP_PAD_ALPHA, + g_param_spec_double ("alpha", "Alpha", "Alpha of the picture", 0.0, 1.0, + DEFAULT_PAD_ALPHA, + G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS)); +} + +static void +gst_videomixer2_pad_init (GstVideoMixer2Pad * mixerpad) +{ + /* setup some pad functions */ + gst_pad_set_setcaps_function (GST_PAD (mixerpad), + gst_videomixer2_pad_sink_setcaps); + gst_pad_set_acceptcaps_function (GST_PAD (mixerpad), + GST_DEBUG_FUNCPTR (gst_videomixer2_pad_sink_acceptcaps)); + gst_pad_set_getcaps_function (GST_PAD (mixerpad), + gst_videomixer2_pad_sink_getcaps); + + mixerpad->zorder = DEFAULT_PAD_ZORDER; + mixerpad->xpos = DEFAULT_PAD_XPOS; + mixerpad->ypos = DEFAULT_PAD_YPOS; + mixerpad->alpha = DEFAULT_PAD_ALPHA; +} + +/* GstVideoMixer2 */ +#define DEFAULT_BACKGROUND VIDEO_MIXER2_BACKGROUND_CHECKER +enum +{ + PROP_0, + PROP_BACKGROUND +}; + +#define GST_TYPE_VIDEO_MIXER2_BACKGROUND (gst_videomixer2_background_get_type()) +static GType +gst_videomixer2_background_get_type (void) +{ + static GType video_mixer_background_type = 0; + + static const GEnumValue video_mixer_background[] = { + {VIDEO_MIXER2_BACKGROUND_CHECKER, "Checker pattern", "checker"}, + {VIDEO_MIXER2_BACKGROUND_BLACK, "Black", "black"}, + {VIDEO_MIXER2_BACKGROUND_WHITE, "White", "white"}, + {0, NULL, NULL}, + }; + + if (!video_mixer_background_type) { + video_mixer_background_type = + g_enum_register_static ("GstVideoMixer2Background", + video_mixer_background); + } + return video_mixer_background_type; +} + + +GST_BOILERPLATE_FULL (GstVideoMixer2, gst_videomixer2, GstElement, + GST_TYPE_ELEMENT, _do_init); + +static void +gst_videomixer2_update_qos (GstVideoMixer2 * mix, gdouble proportion, + GstClockTimeDiff diff, GstClockTime timestamp) +{ + GST_DEBUG_OBJECT (mix, + "Updating QoS: proportion %lf, diff %s%" GST_TIME_FORMAT ", timestamp %" + GST_TIME_FORMAT, proportion, (diff < 0) ? "-" : "", + GST_TIME_ARGS (ABS (diff)), GST_TIME_ARGS (timestamp)); + + GST_OBJECT_LOCK (mix); + mix->proportion = proportion; + if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) { + if (G_UNLIKELY (diff > 0)) + mix->earliest_time = + timestamp + 2 * diff + gst_util_uint64_scale_int (GST_SECOND, + mix->fps_d, mix->fps_n); + else + mix->earliest_time = timestamp + diff; + } else { + mix->earliest_time = GST_CLOCK_TIME_NONE; + } + GST_OBJECT_UNLOCK (mix); +} + +static void +gst_videomixer2_reset_qos (GstVideoMixer2 * mix) +{ + gst_videomixer2_update_qos (mix, 0.5, 0, GST_CLOCK_TIME_NONE); + mix->qos_processed = mix->qos_dropped = 0; +} + +static void +gst_videomixer2_read_qos (GstVideoMixer2 * mix, gdouble * proportion, + GstClockTime * time) +{ + GST_OBJECT_LOCK (mix); + *proportion = mix->proportion; + *time = mix->earliest_time; + GST_OBJECT_UNLOCK (mix); +} + +static void +gst_videomixer2_reset (GstVideoMixer2 * mix) +{ + GSList *l; + + mix->format = GST_VIDEO_FORMAT_UNKNOWN; + mix->width = mix->height = 0; + mix->fps_n = mix->fps_d = 0; + mix->par_n = mix->par_d = 0; + mix->ts_offset = 0; + mix->nframes = 0; + + gst_segment_init (&mix->segment, GST_FORMAT_TIME); + mix->segment.last_stop = -1; + + gst_videomixer2_reset_qos (mix); + + for (l = mix->sinkpads; l; l = l->next) { + GstVideoMixer2Pad *p = l->data; + GstVideoMixer2Collect *mixcol = p->mixcol; + + gst_buffer_replace (&mixcol->buffer, NULL); + mixcol->start_time = -1; + mixcol->end_time = -1; + + p->fps_n = p->fps_d = 0; + p->width = p->height = 0; + } + + mix->newseg_pending = TRUE; + mix->flush_stop_pending = FALSE; +} + +/* 1 == OK + * 0 == need more data + * -1 == EOS + * -2 == error + */ +static gint +gst_videomixer2_fill_queues (GstVideoMixer2 * mix, + GstClockTime output_start_time, GstClockTime output_end_time) +{ + GSList *l; + gboolean eos = TRUE; + gboolean need_more_data = FALSE; + + for (l = mix->sinkpads; l; l = l->next) { + GstVideoMixer2Pad *pad = l->data; + GstVideoMixer2Collect *mixcol = pad->mixcol; + GstSegment *segment = &pad->mixcol->collect.segment; + GstBuffer *buf; + + buf = gst_collect_pads2_peek (mix->collect, &mixcol->collect); + if (buf) { + GstClockTime start_time, end_time; + + start_time = GST_BUFFER_TIMESTAMP (buf); + if (start_time == -1) { + gst_buffer_unref (buf); + GST_ERROR_OBJECT (pad, "Need timestamped buffers!"); + return -2; + } + + /* FIXME: Make all this work with negative rates */ + + if ((mixcol->buffer && start_time < GST_BUFFER_TIMESTAMP (mixcol->buffer)) + || (mixcol->queued + && start_time < GST_BUFFER_TIMESTAMP (mixcol->queued))) { + GST_WARNING_OBJECT (pad, "Buffer from the past, dropping"); + gst_buffer_unref (buf); + buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect); + gst_buffer_unref (buf); + need_more_data = TRUE; + continue; + } + + if (mixcol->queued) { + end_time = start_time - GST_BUFFER_TIMESTAMP (mixcol->queued); + start_time = GST_BUFFER_TIMESTAMP (mixcol->queued); + gst_buffer_unref (buf); + buf = gst_buffer_ref (mixcol->queued); + } else { + end_time = GST_BUFFER_DURATION (buf); + + if (end_time == -1) { + mixcol->queued = buf; + need_more_data = TRUE; + continue; + } + } + + g_assert (start_time != -1 && end_time != -1); + end_time += start_time; /* convert from duration to position */ + + if (mixcol->end_time != -1 && mixcol->end_time > end_time) { + GST_WARNING_OBJECT (pad, "Buffer from the past, dropping"); + if (buf == mixcol->queued) { + gst_buffer_unref (buf); + gst_buffer_replace (&mixcol->queued, NULL); + } else { + gst_buffer_unref (buf); + buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect); + gst_buffer_unref (buf); + } + + need_more_data = TRUE; + continue; + } + + /* Check if it's inside the segment */ + if (start_time >= segment->stop || end_time < segment->start) { + GST_DEBUG_OBJECT (pad, "Buffer outside the segment"); + + if (buf == mixcol->queued) { + gst_buffer_unref (buf); + gst_buffer_replace (&mixcol->queued, NULL); + } else { + gst_buffer_unref (buf); + buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect); + gst_buffer_unref (buf); + } + + need_more_data = TRUE; + continue; + } + + /* Clip to segment and convert to running time */ + start_time = MAX (start_time, segment->start); + if (segment->stop != -1) + end_time = MIN (end_time, segment->stop); + start_time = + gst_segment_to_running_time (segment, GST_FORMAT_TIME, start_time); + end_time = + gst_segment_to_running_time (segment, GST_FORMAT_TIME, end_time); + g_assert (start_time != -1 && end_time != -1); + + /* Convert to the output segment rate */ + if (mix->segment.abs_rate != 1.0) { + start_time *= mix->segment.abs_rate; + end_time *= mix->segment.abs_rate; + } + + if (end_time >= output_start_time && start_time < output_end_time) { + GST_DEBUG_OBJECT (pad, + "Taking new buffer with start time %" GST_TIME_FORMAT, + GST_TIME_ARGS (start_time)); + gst_buffer_replace (&mixcol->buffer, buf); + mixcol->start_time = start_time; + mixcol->end_time = end_time; + + if (buf == mixcol->queued) { + gst_buffer_unref (buf); + gst_buffer_replace (mixcol->queued, NULL); + } else { + gst_buffer_unref (buf); + buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect); + gst_buffer_unref (buf); + } + eos = FALSE; + } else if (start_time >= output_end_time) { + GST_DEBUG_OBJECT (pad, "Keeping buffer until %" GST_TIME_FORMAT, + GST_TIME_ARGS (start_time)); + eos = FALSE; + } else { + GST_DEBUG_OBJECT (pad, "Too old buffer -- dropping"); + if (buf == mixcol->queued) { + gst_buffer_unref (buf); + gst_buffer_replace (&mixcol->queued, NULL); + } else { + gst_buffer_unref (buf); + buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect); + gst_buffer_unref (buf); + } + + need_more_data = TRUE; + continue; + } + } else { + if (mixcol->end_time != -1) { + if (mixcol->end_time < output_start_time) { + gst_buffer_replace (&mixcol->buffer, NULL); + mixcol->start_time = mixcol->end_time = -1; + if (!GST_COLLECT_PADS2_STATE_IS_SET (mixcol, + GST_COLLECT_PADS2_STATE_EOS)) + need_more_data = TRUE; + } else { + eos = FALSE; + } + } + } + } + + if (need_more_data) + return 0; + if (eos) + return -1; + + return 1; +} + +static GstFlowReturn +gst_videomixer2_blend_buffers (GstVideoMixer2 * mix, + GstClockTime output_start_time, GstClockTime output_end_time, + GstBuffer ** outbuf) +{ + GSList *l; + GstFlowReturn ret; + guint outsize; + + outsize = gst_video_format_get_size (mix->format, mix->width, mix->height); + ret = gst_pad_alloc_buffer_and_set_caps (mix->srcpad, GST_BUFFER_OFFSET_NONE, + outsize, GST_PAD_CAPS (mix->srcpad), outbuf); + if (ret != GST_FLOW_OK) + return ret; + + GST_BUFFER_TIMESTAMP (*outbuf) = output_start_time; + GST_BUFFER_DURATION (*outbuf) = output_end_time - output_start_time; + + switch (mix->background) { + case VIDEO_MIXER2_BACKGROUND_CHECKER: + mix->fill_checker (GST_BUFFER_DATA (*outbuf), mix->width, mix->height); + break; + case VIDEO_MIXER2_BACKGROUND_BLACK: + mix->fill_color (GST_BUFFER_DATA (*outbuf), mix->width, + mix->height, 16, 128, 128); + break; + case VIDEO_MIXER2_BACKGROUND_WHITE: + mix->fill_color (GST_BUFFER_DATA (*outbuf), mix->width, + mix->height, 240, 128, 128); + break; + } + + for (l = mix->sinkpads; l; l = l->next) { + GstVideoMixer2Pad *pad = l->data; + GstVideoMixer2Collect *mixcol = pad->mixcol; + + if (mixcol->buffer != NULL) { + GstClockTime timestamp; + gint64 stream_time; + GstSegment *seg; + + seg = &mixcol->collect.segment; + + timestamp = GST_BUFFER_TIMESTAMP (mixcol->buffer); + + stream_time = + gst_segment_to_stream_time (seg, GST_FORMAT_TIME, timestamp); + + /* sync object properties on stream time */ + if (GST_CLOCK_TIME_IS_VALID (stream_time)) + gst_object_sync_values (G_OBJECT (pad), stream_time); + + mix->blend (GST_BUFFER_DATA (mixcol->buffer), + pad->xpos, pad->ypos, pad->width, pad->height, pad->alpha, + GST_BUFFER_DATA (*outbuf), mix->width, mix->height); + } + } + + return GST_FLOW_OK; +} + +/* Perform qos calculations before processing the next frame. Returns TRUE if + * the frame should be processed, FALSE if the frame can be dropped entirely */ +static gint64 +gst_videomixer2_do_qos (GstVideoMixer2 * mix, GstClockTime timestamp) +{ + GstClockTime qostime, earliest_time; + gdouble proportion; + gint64 jitter; + + /* no timestamp, can't do QoS => process frame */ + if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) { + GST_LOG_OBJECT (mix, "invalid timestamp, can't do QoS, process frame"); + return -1; + } + + /* get latest QoS observation values */ + gst_videomixer2_read_qos (mix, &proportion, &earliest_time); + + /* skip qos if we have no observation (yet) => process frame */ + if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) { + GST_LOG_OBJECT (mix, "no observation yet, process frame"); + return -1; + } + + /* qos is done on running time */ + qostime = + gst_segment_to_running_time (&mix->segment, GST_FORMAT_TIME, timestamp); + + /* see how our next timestamp relates to the latest qos timestamp */ + GST_LOG_OBJECT (mix, "qostime %" GST_TIME_FORMAT ", earliest %" + GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time)); + + jitter = GST_CLOCK_DIFF (qostime, earliest_time); + if (qostime != GST_CLOCK_TIME_NONE && jitter > 0) { + GST_DEBUG_OBJECT (mix, "we are late, drop frame"); + return jitter; + } + + GST_LOG_OBJECT (mix, "process frame"); + return jitter; +} + +static GstFlowReturn +gst_videomixer2_collected (GstCollectPads2 * pads, GstVideoMixer2 * mix) +{ + GstFlowReturn ret; + GstClockTime output_start_time, output_end_time; + GstBuffer *outbuf = NULL; + gint res; + gint64 jitter; + + /* If we're not negotiated yet... */ + if (mix->format == GST_VIDEO_FORMAT_UNKNOWN) + return GST_FLOW_NOT_NEGOTIATED; + + if (g_atomic_int_compare_and_exchange (&mix->flush_stop_pending, TRUE, FALSE)) { + GST_DEBUG_OBJECT (mix, "pending flush stop"); + gst_pad_push_event (mix->srcpad, gst_event_new_flush_stop ()); + } + + GST_VIDEO_MIXER2_LOCK (mix); + + if (mix->newseg_pending) { + GST_DEBUG_OBJECT (mix, "Sending NEWSEGMENT event"); + if (!gst_pad_push_event (mix->srcpad, gst_event_new_new_segment_full (FALSE, + mix->segment.rate, mix->segment.applied_rate, + mix->segment.format, mix->segment.start, mix->segment.stop, + mix->segment.time))) { + ret = GST_FLOW_ERROR; + goto done; + } + mix->newseg_pending = FALSE; + } + + if (mix->segment.last_stop == -1) + output_start_time = mix->segment.start; + else + output_start_time = mix->segment.last_stop; + + if (output_start_time >= mix->segment.stop) { + GST_DEBUG_OBJECT (mix, "Segment done"); + gst_pad_push_event (mix->srcpad, gst_event_new_eos ()); + ret = GST_FLOW_UNEXPECTED; + goto done; + } + + output_end_time = + mix->ts_offset + gst_util_uint64_scale (mix->nframes + 1, + GST_SECOND * mix->fps_d, mix->fps_n); + if (mix->segment.stop != -1) + output_end_time = MIN (output_end_time, mix->segment.stop); + + res = gst_videomixer2_fill_queues (mix, output_start_time, output_end_time); + + if (res == 0) { + GST_DEBUG_OBJECT (mix, "Need more data for decisions"); + ret = GST_FLOW_OK; + goto done; + } else if (res == -1) { + GST_DEBUG_OBJECT (mix, "All sinkpads are EOS -- forwarding"); + gst_pad_push_event (mix->srcpad, gst_event_new_eos ()); + ret = GST_FLOW_UNEXPECTED; + goto done; + } else if (res == -2) { + GST_ERROR_OBJECT (mix, "Error collecting buffers"); + ret = GST_FLOW_ERROR; + goto done; + } + + jitter = gst_videomixer2_do_qos (mix, output_start_time); + if (jitter <= 0) { + ret = + gst_videomixer2_blend_buffers (mix, output_start_time, + output_end_time, &outbuf); + mix->qos_processed++; + } else { + GstMessage *msg; + + mix->qos_dropped++; + + /* TODO: live */ + msg = + gst_message_new_qos (GST_OBJECT_CAST (mix), FALSE, + gst_segment_to_running_time (&mix->segment, GST_FORMAT_TIME, + output_start_time), gst_segment_to_stream_time (&mix->segment, + GST_FORMAT_TIME, output_start_time), output_start_time, + output_end_time - output_start_time); + gst_message_set_qos_values (msg, jitter, mix->proportion, 1000000); + gst_message_set_qos_stats (msg, GST_FORMAT_BUFFERS, mix->qos_processed, + mix->qos_dropped); + gst_element_post_message (GST_ELEMENT_CAST (mix), msg); + + ret = GST_FLOW_OK; + } + + gst_segment_set_last_stop (&mix->segment, GST_FORMAT_TIME, output_end_time); + mix->nframes++; + + GST_VIDEO_MIXER2_UNLOCK (mix); + if (outbuf) { + GST_LOG_OBJECT (mix, + "Pushing buffer with ts %" GST_TIME_FORMAT " and duration %" + GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)), + GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf))); + ret = gst_pad_push (mix->srcpad, outbuf); + } + GST_VIDEO_MIXER2_LOCK (mix); + +done: + GST_VIDEO_MIXER2_UNLOCK (mix); + + return ret; +} + +static GstCaps * +gst_videomixer2_src_getcaps (GstPad * pad) +{ + GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad)); + GstCaps *caps; + GstStructure *s; + gint n; + + if (mix->format != GST_VIDEO_FORMAT_UNKNOWN) { + caps = gst_caps_copy (GST_PAD_CAPS (mix->srcpad)); + } else { + caps = gst_caps_copy (gst_pad_get_pad_template_caps (mix->srcpad)); + } + + n = gst_caps_get_size (caps) - 1; + for (; n >= 0; n--) { + s = gst_caps_get_structure (caps, n); + gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT, + "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL); + if (mix->fps_d != 0) { + gst_structure_set (s, + "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL); + } + } + + gst_object_unref (mix); + + return caps; +} + +static gboolean +gst_videomixer2_query_duration (GstVideoMixer2 * mix, GstQuery * query) +{ + gint64 max; + gboolean res; + GstFormat format; + GstIterator *it; + gboolean done; + + /* parse format */ + gst_query_parse_duration (query, &format, NULL); + + max = -1; + res = TRUE; + done = FALSE; + + /* Take maximum of all durations */ + it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix)); + while (!done) { + GstIteratorResult ires; + gpointer item; + + ires = gst_iterator_next (it, &item); + switch (ires) { + case GST_ITERATOR_DONE: + done = TRUE; + break; + case GST_ITERATOR_OK: + { + GstPad *pad = GST_PAD_CAST (item); + gint64 duration; + + /* ask sink peer for duration */ + res &= gst_pad_query_peer_duration (pad, &format, &duration); + /* take max from all valid return values */ + if (res) { + /* valid unknown length, stop searching */ + if (duration == -1) { + max = duration; + done = TRUE; + } + /* else see if bigger than current max */ + else if (duration > max) + max = duration; + } + gst_object_unref (pad); + break; + } + case GST_ITERATOR_RESYNC: + max = -1; + res = TRUE; + gst_iterator_resync (it); + break; + default: + res = FALSE; + done = TRUE; + break; + } + } + gst_iterator_free (it); + + if (res) { + /* and store the max */ + GST_DEBUG_OBJECT (mix, "Total duration in format %s: %" + GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max)); + gst_query_set_duration (query, format, max); + } + + return res; +} + +static gboolean +gst_videomixer2_query_latency (GstVideoMixer2 * mix, GstQuery * query) +{ + GstClockTime min, max; + gboolean live; + gboolean res; + GstIterator *it; + gboolean done; + + res = TRUE; + done = FALSE; + live = FALSE; + min = 0; + max = GST_CLOCK_TIME_NONE; + + /* Take maximum of all latency values */ + it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix)); + while (!done) { + GstIteratorResult ires; + gpointer item; + + ires = gst_iterator_next (it, &item); + switch (ires) { + case GST_ITERATOR_DONE: + done = TRUE; + break; + case GST_ITERATOR_OK: + { + GstPad *pad = GST_PAD_CAST (item); + GstQuery *peerquery; + GstClockTime min_cur, max_cur; + gboolean live_cur; + + peerquery = gst_query_new_latency (); + + /* Ask peer for latency */ + res &= gst_pad_peer_query (pad, peerquery); + + /* take max from all valid return values */ + if (res) { + gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur); + + if (min_cur > min) + min = min_cur; + + if (max_cur != GST_CLOCK_TIME_NONE && + ((max != GST_CLOCK_TIME_NONE && max_cur > max) || + (max == GST_CLOCK_TIME_NONE))) + max = max_cur; + + live = live || live_cur; + } + + gst_query_unref (peerquery); + gst_object_unref (pad); + break; + } + case GST_ITERATOR_RESYNC: + live = FALSE; + min = 0; + max = GST_CLOCK_TIME_NONE; + res = TRUE; + gst_iterator_resync (it); + break; + default: + res = FALSE; + done = TRUE; + break; + } + } + gst_iterator_free (it); + + if (res) { + /* store the results */ + GST_DEBUG_OBJECT (mix, "Calculated total latency: live %s, min %" + GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, + (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max)); + gst_query_set_latency (query, live, min, max); + } + + return res; +} + +static gboolean +gst_videomixer2_src_query (GstPad * pad, GstQuery * query) +{ + GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad)); + gboolean res = FALSE; + + switch (GST_QUERY_TYPE (query)) { + case GST_QUERY_POSITION: + { + GstFormat format; + + gst_query_parse_position (query, &format, NULL); + + switch (format) { + case GST_FORMAT_TIME: + gst_query_set_position (query, format, + gst_segment_to_stream_time (&mix->segment, GST_FORMAT_TIME, + mix->segment.last_stop)); + res = TRUE; + break; + default: + break; + } + break; + } + case GST_QUERY_DURATION: + res = gst_videomixer2_query_duration (mix, query); + break; + case GST_QUERY_LATENCY: + res = gst_videomixer2_query_latency (mix, query); + break; + default: + /* FIXME, needs a custom query handler because we have multiple + * sinkpads */ + res = FALSE; + gst_query_unref (query); + break; + } + + gst_object_unref (mix); + return res; +} + +static gboolean +gst_videomixer2_src_event (GstPad * pad, GstEvent * event) +{ + GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad)); + gboolean result; + + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_QOS:{ + GstClockTimeDiff diff; + GstClockTime timestamp; + gdouble proportion; + + gst_event_parse_qos (event, &proportion, &diff, ×tamp); + + gst_videomixer2_update_qos (mix, proportion, diff, timestamp); + + result = gst_videomixer2_push_sink_event (mix, event); + break; + } + case GST_EVENT_SEEK: + { + gdouble rate; + GstFormat fmt; + GstSeekFlags flags; + GstSeekType start_type, stop_type; + gint64 start, stop; + GSList *l; + gdouble abs_rate; + + /* parse the seek parameters */ + gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type, + &start, &stop_type, &stop); + + if (rate <= 0.0) { + GST_ERROR_OBJECT (mix, "Negative rates not supported yet"); + result = FALSE; + gst_event_unref (event); + break; + } + + GST_DEBUG_OBJECT (mix, "Handling SEEK event"); + + /* check if we are flushing */ + if (flags & GST_SEEK_FLAG_FLUSH) { + /* flushing seek, start flush downstream, the flush will be done + * when all pads received a FLUSH_STOP. */ + gst_pad_push_event (mix->srcpad, gst_event_new_flush_start ()); + + /* make sure we accept nothing anymore and return WRONG_STATE */ + gst_collect_pads2_set_flushing (mix->collect, TRUE); + } + + /* now wait for the collected to be finished and mark a new + * segment */ + GST_COLLECT_PADS2_STREAM_LOCK (mix->collect); + + abs_rate = ABS (rate); + + GST_VIDEO_MIXER2_LOCK (mix); + for (l = mix->sinkpads; l; l = l->next) { + GstVideoMixer2Pad *p = l->data; + + if (flags & GST_SEEK_FLAG_FLUSH) { + gst_buffer_replace (&p->mixcol->buffer, NULL); + p->mixcol->start_time = p->mixcol->end_time = -1; + continue; + } + + /* Convert to the output segment rate */ + if (mix->segment.abs_rate != abs_rate) { + if (mix->segment.abs_rate != 1.0 && p->mixcol->buffer) { + p->mixcol->start_time /= mix->segment.abs_rate; + p->mixcol->end_time /= mix->segment.abs_rate; + } + if (abs_rate != 1.0 && p->mixcol->buffer) { + p->mixcol->start_time *= abs_rate; + p->mixcol->end_time *= abs_rate; + } + } + } + GST_VIDEO_MIXER2_UNLOCK (mix); + + gst_segment_set_seek (&mix->segment, rate, fmt, flags, start_type, start, + stop_type, stop, NULL); + mix->segment.last_stop = -1; + mix->ts_offset = 0; + mix->nframes = 0; + mix->newseg_pending = TRUE; + + if (flags & GST_SEEK_FLAG_FLUSH) { + gst_collect_pads2_set_flushing (mix->collect, FALSE); + + /* we can't send FLUSH_STOP here since upstream could start pushing data + * after we unlock mix->collect. + * We set flush_stop_pending to TRUE instead and send FLUSH_STOP after + * forwarding the seek upstream or from gst_videomixer_collected, + * whichever happens first. + */ + mix->flush_stop_pending = TRUE; + } + + GST_COLLECT_PADS2_STREAM_UNLOCK (mix->collect); + + gst_videomixer2_reset_qos (mix); + + result = gst_videomixer2_push_sink_event (mix, event); + + if (g_atomic_int_compare_and_exchange (&mix->flush_stop_pending, TRUE, + FALSE)) { + GST_DEBUG_OBJECT (mix, "pending flush stop"); + gst_pad_push_event (mix->srcpad, gst_event_new_flush_stop ()); + } + + break; + } + case GST_EVENT_NAVIGATION: + /* navigation is rather pointless. */ + result = FALSE; + gst_event_unref (event); + break; + default: + /* just forward the rest for now */ + result = gst_videomixer2_push_sink_event (mix, event); + break; + } + gst_object_unref (mix); + + return result; +} + +static gboolean +gst_videomixer2_src_setcaps (GstPad * pad, GstCaps * caps) +{ + GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent_element (pad)); + gboolean ret = FALSE; + GstVideoFormat fmt; + gint width, height; + gint fps_n, fps_d; + gint par_n, par_d; + + GST_INFO_OBJECT (pad, "set src caps: %" GST_PTR_FORMAT, caps); + + mix->blend = NULL; + mix->fill_checker = NULL; + mix->fill_color = NULL; + + if (!gst_video_format_parse_caps (caps, &fmt, &width, &height) || + !gst_video_parse_caps_framerate (caps, &fps_n, &fps_d) || + !gst_video_parse_caps_pixel_aspect_ratio (caps, &par_n, &par_d)) + goto done; + + GST_VIDEO_MIXER2_LOCK (mix); + + if (mix->fps_n != fps_n || mix->fps_d != fps_d) { + if (mix->segment.last_stop != -1) { + mix->ts_offset = mix->segment.last_stop - mix->segment.start; + mix->nframes = 0; + } + gst_videomixer2_reset_qos (mix); + } + + mix->format = fmt; + mix->width = width; + mix->height = height; + mix->fps_n = fps_n; + mix->fps_d = fps_d; + mix->par_n = par_n; + mix->par_d = par_d; + + switch (mix->format) { + case GST_VIDEO_FORMAT_AYUV: + mix->blend = gst_video_mixer_blend_ayuv; + mix->fill_checker = gst_video_mixer_fill_checker_ayuv; + mix->fill_color = gst_video_mixer_fill_color_ayuv; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_ARGB: + mix->blend = gst_video_mixer_blend_argb; + mix->fill_checker = gst_video_mixer_fill_checker_argb; + mix->fill_color = gst_video_mixer_fill_color_argb; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_BGRA: + mix->blend = gst_video_mixer_blend_bgra; + mix->fill_checker = gst_video_mixer_fill_checker_bgra; + mix->fill_color = gst_video_mixer_fill_color_bgra; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_ABGR: + mix->blend = gst_video_mixer_blend_abgr; + mix->fill_checker = gst_video_mixer_fill_checker_abgr; + mix->fill_color = gst_video_mixer_fill_color_abgr; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_RGBA: + mix->blend = gst_video_mixer_blend_rgba; + mix->fill_checker = gst_video_mixer_fill_checker_rgba; + mix->fill_color = gst_video_mixer_fill_color_rgba; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_Y444: + mix->blend = gst_video_mixer_blend_y444; + mix->fill_checker = gst_video_mixer_fill_checker_y444; + mix->fill_color = gst_video_mixer_fill_color_y444; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_Y42B: + mix->blend = gst_video_mixer_blend_y42b; + mix->fill_checker = gst_video_mixer_fill_checker_y42b; + mix->fill_color = gst_video_mixer_fill_color_y42b; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_YUY2: + mix->blend = gst_video_mixer_blend_yuy2; + mix->fill_checker = gst_video_mixer_fill_checker_yuy2; + mix->fill_color = gst_video_mixer_fill_color_yuy2; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_UYVY: + mix->blend = gst_video_mixer_blend_uyvy; + mix->fill_checker = gst_video_mixer_fill_checker_uyvy; + mix->fill_color = gst_video_mixer_fill_color_uyvy; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_YVYU: + mix->blend = gst_video_mixer_blend_yvyu; + mix->fill_checker = gst_video_mixer_fill_checker_yvyu; + mix->fill_color = gst_video_mixer_fill_color_yvyu; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_I420: + mix->blend = gst_video_mixer_blend_i420; + mix->fill_checker = gst_video_mixer_fill_checker_i420; + mix->fill_color = gst_video_mixer_fill_color_i420; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_YV12: + mix->blend = gst_video_mixer_blend_yv12; + mix->fill_checker = gst_video_mixer_fill_checker_yv12; + mix->fill_color = gst_video_mixer_fill_color_yv12; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_Y41B: + mix->blend = gst_video_mixer_blend_y41b; + mix->fill_checker = gst_video_mixer_fill_checker_y41b; + mix->fill_color = gst_video_mixer_fill_color_y41b; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_RGB: + mix->blend = gst_video_mixer_blend_rgb; + mix->fill_checker = gst_video_mixer_fill_checker_rgb; + mix->fill_color = gst_video_mixer_fill_color_rgb; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_BGR: + mix->blend = gst_video_mixer_blend_bgr; + mix->fill_checker = gst_video_mixer_fill_checker_bgr; + mix->fill_color = gst_video_mixer_fill_color_bgr; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_xRGB: + mix->blend = gst_video_mixer_blend_xrgb; + mix->fill_checker = gst_video_mixer_fill_checker_xrgb; + mix->fill_color = gst_video_mixer_fill_color_xrgb; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_xBGR: + mix->blend = gst_video_mixer_blend_xbgr; + mix->fill_checker = gst_video_mixer_fill_checker_xbgr; + mix->fill_color = gst_video_mixer_fill_color_xbgr; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_RGBx: + mix->blend = gst_video_mixer_blend_rgbx; + mix->fill_checker = gst_video_mixer_fill_checker_rgbx; + mix->fill_color = gst_video_mixer_fill_color_rgbx; + ret = TRUE; + break; + case GST_VIDEO_FORMAT_BGRx: + mix->blend = gst_video_mixer_blend_bgrx; + mix->fill_checker = gst_video_mixer_fill_checker_bgrx; + mix->fill_color = gst_video_mixer_fill_color_bgrx; + ret = TRUE; + break; + default: + break; + } + GST_VIDEO_MIXER2_UNLOCK (mix); + +done: + gst_object_unref (mix); + + return ret; +} + +static GstFlowReturn +gst_videomixer2_sink_prepare_buffer (GstCollectPads2 * pads, + GstCollectData2 * data, GstBuffer * buf, GstVideoMixer2 * mix) +{ + GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (data->pad); + GstVideoMixer2Collect *mixcol = pad->mixcol; + GstClockTime start_time, end_time; + gint64 last_stop; + + start_time = GST_BUFFER_TIMESTAMP (buf); + if (start_time == -1) { + GST_ERROR_OBJECT (pad, "Timestamped buffers required!"); + return GST_FLOW_ERROR; + } + + end_time = GST_BUFFER_DURATION (buf); + if (end_time == -1) + end_time = gst_util_uint64_scale_int (GST_SECOND, pad->fps_d, pad->fps_n); + if (end_time == -1) + return GST_FLOW_OK; + + start_time = MAX (start_time, mixcol->collect.segment.start); + start_time = + gst_segment_to_running_time (&mixcol->collect.segment, + GST_FORMAT_TIME, start_time); + + end_time += GST_BUFFER_TIMESTAMP (buf); + if (mixcol->collect.segment.stop != -1) + end_time = MIN (end_time, mixcol->collect.segment.stop); + end_time = + gst_segment_to_running_time (&mixcol->collect.segment, + GST_FORMAT_TIME, end_time); + + /* Convert to the output segment rate */ + if (mix->segment.abs_rate != 1.0) { + start_time *= mix->segment.abs_rate; + end_time *= mix->segment.abs_rate; + } + + last_stop = mix->segment.last_stop; + if (mix->segment.last_stop == -1) + last_stop = mix->segment.start; + + if (mixcol->buffer != NULL && end_time < mixcol->end_time) { + return GST_COLLECT_PADS2_FLOW_DROP; + } + + return GST_FLOW_OK; +} + +static gboolean +gst_videomixer2_sink_event (GstCollectPads2 * pads, GstCollectData2 * cdata, + GstEvent * event, GstVideoMixer2 * mix) +{ + GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (cdata->pad); + gboolean ret = TRUE; + + GST_DEBUG_OBJECT (pad, "Got %s event on pad %s:%s", + GST_EVENT_TYPE_NAME (event), GST_DEBUG_PAD_NAME (pad)); + + // return FALSE => event will be forwarded + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_NEWSEGMENT:{ + GstFormat fmt; + gst_event_parse_new_segment (event, NULL, NULL, &fmt, NULL, NULL, NULL); + + g_assert (fmt == GST_FORMAT_TIME); + /* eat NEWSEGMENT events, collectpads2 unrefs the event */ + ret = FALSE; + break; + } + case GST_EVENT_FLUSH_STOP: + mix->newseg_pending = TRUE; + mix->flush_stop_pending = FALSE; + gst_videomixer2_reset_qos (mix); + gst_buffer_replace (&pad->mixcol->buffer, NULL); + pad->mixcol->start_time = -1; + pad->mixcol->end_time = -1; + + gst_segment_init (&mix->segment, GST_FORMAT_TIME); + mix->segment.last_stop = -1; + mix->ts_offset = 0; + mix->nframes = 0; + + gst_pad_push_event (mix->srcpad, event); + break; + default: + gst_pad_push_event (mix->srcpad, event); + break; + } + + return ret; +} + +static gboolean +forward_event_func (GstPad * pad, GValue * ret, GstEvent * event) +{ + gst_event_ref (event); + GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event)); + if (!gst_pad_push_event (pad, event)) { + g_value_set_boolean (ret, FALSE); + GST_WARNING_OBJECT (pad, "Sending event %p (%s) failed.", + event, GST_EVENT_TYPE_NAME (event)); + } else { + GST_LOG_OBJECT (pad, "Sent event %p (%s).", + event, GST_EVENT_TYPE_NAME (event)); + } + gst_object_unref (pad); + return TRUE; +} + +static gboolean +gst_videomixer2_push_sink_event (GstVideoMixer2 * mix, GstEvent * event) +{ + GstIterator *it; + GValue vret = { 0 }; + + GST_LOG_OBJECT (mix, "Forwarding event %p (%s)", event, + GST_EVENT_TYPE_NAME (event)); + + g_value_init (&vret, G_TYPE_BOOLEAN); + g_value_set_boolean (&vret, TRUE); + it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix)); + gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func, &vret, + event); + gst_iterator_free (it); + gst_event_unref (event); + + return g_value_get_boolean (&vret); +} + +/* GstElement vmethods */ +static GstStateChangeReturn +gst_videomixer2_change_state (GstElement * element, GstStateChange transition) +{ + GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (element); + GstStateChangeReturn ret; + + switch (transition) { + case GST_STATE_CHANGE_READY_TO_PAUSED: + GST_LOG_OBJECT (mix, "starting collectpads"); + gst_collect_pads2_start (mix->collect); + break; + case GST_STATE_CHANGE_PAUSED_TO_READY: + GST_LOG_OBJECT (mix, "stopping collectpads"); + gst_collect_pads2_stop (mix->collect); + break; + default: + break; + } + + ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); + + switch (transition) { + case GST_STATE_CHANGE_PAUSED_TO_READY: + gst_videomixer2_reset (mix); + break; + default: + break; + } + + return ret; +} + +static GstPad * +gst_videomixer2_request_new_pad (GstElement * element, + GstPadTemplate * templ, const gchar * req_name) +{ + GstVideoMixer2 *mix; + GstVideoMixer2Pad *mixpad; + GstElementClass *klass = GST_ELEMENT_GET_CLASS (element); + + mix = GST_VIDEO_MIXER2 (element); + + if (templ == gst_element_class_get_pad_template (klass, "sink_%d")) { + gint serial = 0; + gchar *name = NULL; + GstVideoMixer2Collect *mixcol = NULL; + + GST_VIDEO_MIXER2_LOCK (mix); + if (req_name == NULL || strlen (req_name) < 6 + || !g_str_has_prefix (req_name, "sink_")) { + /* no name given when requesting the pad, use next available int */ + serial = mix->next_sinkpad++; + } else { + /* parse serial number from requested padname */ + serial = g_ascii_strtoull (&req_name[5], NULL, 10); + if (serial >= mix->next_sinkpad) + mix->next_sinkpad = serial + 1; + } + /* create new pad with the name */ + name = g_strdup_printf ("sink_%d", serial); + mixpad = g_object_new (GST_TYPE_VIDEO_MIXER2_PAD, "name", name, "direction", + templ->direction, "template", templ, NULL); + g_free (name); + + mixpad->zorder = mix->numpads; + mixpad->xpos = DEFAULT_PAD_XPOS; + mixpad->ypos = DEFAULT_PAD_YPOS; + mixpad->alpha = DEFAULT_PAD_ALPHA; + + mixcol = (GstVideoMixer2Collect *) + gst_collect_pads2_add_pad_full (mix->collect, GST_PAD (mixpad), + sizeof (GstVideoMixer2Collect), + (GstCollectData2DestroyNotify) gst_videomixer2_collect_free, TRUE); + + /* Keep track of each other */ + mixcol->mixpad = mixpad; + mixpad->mixcol = mixcol; + + mixcol->start_time = -1; + mixcol->end_time = -1; + + /* Keep an internal list of mixpads for zordering */ + mix->sinkpads = g_slist_append (mix->sinkpads, mixpad); + mix->numpads++; + GST_VIDEO_MIXER2_UNLOCK (mix); + } else { + return NULL; + } + + GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (mixpad)); + + /* add the pad to the element */ + gst_element_add_pad (element, GST_PAD (mixpad)); + gst_child_proxy_child_added (GST_OBJECT (mix), GST_OBJECT (mixpad)); + + return GST_PAD (mixpad); +} + +static void +gst_videomixer2_release_pad (GstElement * element, GstPad * pad) +{ + GstVideoMixer2 *mix = NULL; + GstVideoMixer2Pad *mixpad; + gboolean update_caps; + + mix = GST_VIDEO_MIXER2 (element); + + GST_VIDEO_MIXER2_LOCK (mix); + if (G_UNLIKELY (g_slist_find (mix->sinkpads, pad) == NULL)) { + g_warning ("Unknown pad %s", GST_PAD_NAME (pad)); + goto error; + } + + mixpad = GST_VIDEO_MIXER2_PAD (pad); + + mix->sinkpads = g_slist_remove (mix->sinkpads, pad); + gst_child_proxy_child_removed (GST_OBJECT (mix), GST_OBJECT (mixpad)); + mix->numpads--; + + update_caps = mix->format != GST_VIDEO_FORMAT_UNKNOWN; + GST_VIDEO_MIXER2_UNLOCK (mix); + + gst_collect_pads2_remove_pad (mix->collect, pad); + + if (update_caps) + gst_videomixer2_update_src_caps (mix); + + gst_element_remove_pad (element, pad); + return; +error: + GST_VIDEO_MIXER2_UNLOCK (mix); +} + +/* GObject vmethods */ +static void +gst_videomixer2_finalize (GObject * o) +{ + GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (o); + + gst_object_unref (mix->collect); + g_mutex_free (mix->lock); + + G_OBJECT_CLASS (parent_class)->finalize (o); +} + +static void +gst_videomixer2_get_property (GObject * object, + guint prop_id, GValue * value, GParamSpec * pspec) +{ + GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (object); + + switch (prop_id) { + case PROP_BACKGROUND: + g_value_set_enum (value, mix->background); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_videomixer2_set_property (GObject * object, + guint prop_id, const GValue * value, GParamSpec * pspec) +{ + GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (object); + + switch (prop_id) { + case PROP_BACKGROUND: + mix->background = g_value_get_enum (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +/* GstChildProxy implementation */ +static GstObject * +gst_videomixer2_child_proxy_get_child_by_index (GstChildProxy * child_proxy, + guint index) +{ + GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (child_proxy); + GstObject *obj; + + GST_VIDEO_MIXER2_LOCK (mix); + if ((obj = g_slist_nth_data (mix->sinkpads, index))) + gst_object_ref (obj); + GST_VIDEO_MIXER2_UNLOCK (mix); + return obj; +} + +static guint +gst_videomixer2_child_proxy_get_children_count (GstChildProxy * child_proxy) +{ + guint count = 0; + GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (child_proxy); + + GST_VIDEO_MIXER2_LOCK (mix); + count = mix->numpads; + GST_VIDEO_MIXER2_UNLOCK (mix); + GST_INFO_OBJECT (mix, "Children Count: %d", count); + return count; +} + +static void +gst_videomixer2_child_proxy_init (gpointer g_iface, gpointer iface_data) +{ + GstChildProxyInterface *iface = g_iface; + + GST_INFO ("intializing child proxy interface"); + iface->get_child_by_index = gst_videomixer2_child_proxy_get_child_by_index; + iface->get_children_count = gst_videomixer2_child_proxy_get_children_count; +} + +/* GObject boilerplate */ +static void +gst_videomixer2_base_init (gpointer g_class) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_factory)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_factory)); + + gst_element_class_set_details_simple (element_class, "Video mixer 2", + "Filter/Editor/Video", + "Mix multiple video streams", "Wim Taymans , " + "Sebastian Dröge "); +} + +static void +gst_videomixer2_class_init (GstVideoMixer2Class * klass) +{ + GObjectClass *gobject_class = (GObjectClass *) klass; + GstElementClass *gstelement_class = (GstElementClass *) klass; + + gobject_class->finalize = gst_videomixer2_finalize; + + gobject_class->get_property = gst_videomixer2_get_property; + gobject_class->set_property = gst_videomixer2_set_property; + + g_object_class_install_property (gobject_class, PROP_BACKGROUND, + g_param_spec_enum ("background", "Background", "Background type", + GST_TYPE_VIDEO_MIXER2_BACKGROUND, + DEFAULT_BACKGROUND, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + gstelement_class->request_new_pad = + GST_DEBUG_FUNCPTR (gst_videomixer2_request_new_pad); + gstelement_class->release_pad = + GST_DEBUG_FUNCPTR (gst_videomixer2_release_pad); + gstelement_class->change_state = + GST_DEBUG_FUNCPTR (gst_videomixer2_change_state); + + /* Register the pad class */ + (void) (GST_TYPE_VIDEO_MIXER2_PAD); + /* Register the background enum */ + (void) (GST_TYPE_VIDEO_MIXER2_BACKGROUND); +} + +static void +gst_videomixer2_init (GstVideoMixer2 * mix, GstVideoMixer2Class * g_class) +{ + GstElementClass *klass = GST_ELEMENT_GET_CLASS (mix); + + mix->srcpad = + gst_pad_new_from_template (gst_element_class_get_pad_template (klass, + "src"), "src"); + gst_pad_set_getcaps_function (GST_PAD (mix->srcpad), + GST_DEBUG_FUNCPTR (gst_videomixer2_src_getcaps)); + gst_pad_set_setcaps_function (GST_PAD (mix->srcpad), + GST_DEBUG_FUNCPTR (gst_videomixer2_src_setcaps)); + gst_pad_set_query_function (GST_PAD (mix->srcpad), + GST_DEBUG_FUNCPTR (gst_videomixer2_src_query)); + gst_pad_set_event_function (GST_PAD (mix->srcpad), + GST_DEBUG_FUNCPTR (gst_videomixer2_src_event)); + gst_element_add_pad (GST_ELEMENT (mix), mix->srcpad); + + mix->collect = gst_collect_pads2_new (); + mix->background = DEFAULT_BACKGROUND; + + gst_collect_pads2_set_function (mix->collect, + (GstCollectPads2Function) GST_DEBUG_FUNCPTR (gst_videomixer2_collected), + mix); + gst_collect_pads2_set_event_function (mix->collect, + (GstCollectPads2EventFunction) gst_videomixer2_sink_event, mix); + gst_collect_pads2_set_prepare_buffer_function (mix->collect, + (GstCollectPads2BufferFunction) gst_videomixer2_sink_prepare_buffer, mix); + + mix->lock = g_mutex_new (); + /* initialize variables */ + gst_videomixer2_reset (mix); +} + +/* Element registration */ +gboolean +gst_videomixer2_register (GstPlugin * plugin) +{ + GST_DEBUG_CATEGORY_INIT (gst_videomixer2_debug, "videomixer2", 0, + "video mixer 2"); + + return gst_element_register (plugin, "videomixer2", GST_RANK_SECONDARY, + GST_TYPE_VIDEO_MIXER2); +} diff --git a/gst/videomixer/videomixer2.h b/gst/videomixer/videomixer2.h new file mode 100644 index 0000000000..2b698a4ce5 --- /dev/null +++ b/gst/videomixer/videomixer2.h @@ -0,0 +1,121 @@ +/* Generic video mixer plugin + * Copyright (C) 2008 Wim Taymans + * Copyright (C) 2010 Sebastian Dröge + * + * 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. + */ + +#ifndef __GST_VIDEO_MIXER2_H__ +#define __GST_VIDEO_MIXER2_H__ + +#include +#include + +#include "blend.h" +#include "gstcollectpads2.h" + +G_BEGIN_DECLS + +#define GST_TYPE_VIDEO_MIXER2 (gst_videomixer2_get_type()) +#define GST_VIDEO_MIXER2(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VIDEO_MIXER2, GstVideoMixer2)) +#define GST_VIDEO_MIXER2_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VIDEO_MIXER2, GstVideoMixer2Class)) +#define GST_IS_VIDEO_MIXER2(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VIDEO_MIXER2)) +#define GST_IS_VIDEO_MIXER2_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VIDEO_MIXER2)) + +typedef struct _GstVideoMixer2 GstVideoMixer2; +typedef struct _GstVideoMixer2Class GstVideoMixer2Class; + +/** + * GstVideoMixer2Background: + * @VIDEO_MIXER2_BACKGROUND_CHECKER: checker pattern background + * @VIDEO_MIXER2_BACKGROUND_BLACK: solid color black background + * @VIDEO_MIXER2_BACKGROUND_WHITE: solid color white background + * + * The different backgrounds videomixer can blend over. + */ +typedef enum +{ + VIDEO_MIXER2_BACKGROUND_CHECKER, + VIDEO_MIXER2_BACKGROUND_BLACK, + VIDEO_MIXER2_BACKGROUND_WHITE +} +GstVideoMixer2Background; + +/** + * GstVideoMixer2: + * + * The opaque #GstVideoMixer2 structure. + */ +struct _GstVideoMixer2 +{ + GstElement element; + + /* pad */ + GstPad *srcpad; + + /* Lock to prevent the state to change while blending */ + GMutex *lock; + /* Sink pads using Collect Pads 2*/ + GstCollectPads2 *collect; + + /* sinkpads, a GSList of GstVideoMixer2Pads */ + GSList *sinkpads; + gint numpads; + /* Next available sinkpad index */ + gint next_sinkpad; + + /* Output caps */ + GstVideoFormat format; + gint width, height; + gint fps_n; + gint fps_d; + gint par_n; + gint par_d; + + gboolean newseg_pending; + gboolean flush_stop_pending; + + GstVideoMixer2Background background; + + /* Current downstream segment */ + GstSegment segment; + GstClockTime ts_offset; + guint64 nframes; + + /* QoS stuff */ + gdouble proportion; + GstClockTime earliest_time; + guint64 qos_processed, qos_dropped; + + BlendFunction blend; + FillCheckerFunction fill_checker; + FillColorFunction fill_color; +}; + +struct _GstVideoMixer2Class +{ + GstElementClass parent_class; +}; + +GType gst_videomixer2_get_type (void); +gboolean gst_videomixer2_register (GstPlugin * plugin); + +G_END_DECLS +#endif /* __GST_VIDEO_MIXER2_H__ */