2008-03-29 00:45:18 +00:00
|
|
|
/*
|
2012-09-12 20:03:06 +00:00
|
|
|
* GStreamer
|
2008-03-29 00:45:18 +00:00
|
|
|
*
|
2012-09-12 20:03:06 +00:00
|
|
|
* Copyright 2012 Collabora Ltd
|
2008-03-29 00:45:18 +00:00
|
|
|
* Copyright 2008 Nokia Corporation
|
|
|
|
* @author: Olivier Crete <olivier.crete@collabora.co.uk>
|
|
|
|
*
|
|
|
|
* With parts copied from the adder plugin which is
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2001 Thomas <thomas@apestaart.org>
|
|
|
|
* 2005,2006 Wim Taymans <wim@fluendo.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
2009-02-10 18:38:11 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-liveadder
|
|
|
|
* @see_also: adder
|
|
|
|
*
|
|
|
|
* The live adder allows to mix several streams into one by adding the data.
|
|
|
|
* Mixed data is clamped to the min/max values of the data format.
|
|
|
|
*
|
|
|
|
* Unlike the adder, the liveadder mixes the streams according the their
|
|
|
|
* timestamps and waits for some milli-seconds before trying doing the mixing.
|
|
|
|
*/
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "liveadder.h"
|
|
|
|
|
|
|
|
#include <gst/audio/audio.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define DEFAULT_LATENCY_MS 60
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (live_adder_debug);
|
|
|
|
#define GST_CAT_DEFAULT (live_adder_debug)
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_live_adder_sink_template =
|
2012-09-12 20:03:06 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink_%u",
|
2009-02-10 16:18:32 +00:00
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_REQUEST,
|
2012-09-12 20:03:06 +00:00
|
|
|
GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE ("{ S8, U8, "
|
|
|
|
GST_AUDIO_NE (S16) "," GST_AUDIO_NE (U16) ","
|
|
|
|
GST_AUDIO_NE (S32) "," GST_AUDIO_NE (U32) ","
|
|
|
|
GST_AUDIO_NE (F32) "," GST_AUDIO_NE (F64) "}"))
|
2009-02-10 16:18:32 +00:00
|
|
|
);
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_live_adder_src_template =
|
2012-09-12 20:03:06 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
2009-02-10 16:18:32 +00:00
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2012-09-12 20:03:06 +00:00
|
|
|
GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE ("{ S8, U8, "
|
|
|
|
GST_AUDIO_NE (S16) "," GST_AUDIO_NE (U16) ","
|
|
|
|
GST_AUDIO_NE (S32) "," GST_AUDIO_NE (U32) ","
|
|
|
|
GST_AUDIO_NE (F32) "," GST_AUDIO_NE (F64) "}"))
|
2009-02-10 16:18:32 +00:00
|
|
|
);
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
/* Valve signals and args */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_LATENCY,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _GstLiveAdderPadPrivate
|
|
|
|
{
|
|
|
|
GstSegment segment;
|
|
|
|
gboolean eos;
|
|
|
|
|
2008-04-03 00:57:28 +00:00
|
|
|
GstClockTime expected_timestamp;
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
} GstLiveAdderPadPrivate;
|
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
G_DEFINE_TYPE (GstLiveAdder, gst_live_adder, GST_TYPE_ELEMENT);
|
2008-03-29 00:45:18 +00:00
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
static void gst_live_adder_finalize (GObject * object);
|
2008-03-29 00:45:18 +00:00
|
|
|
static void
|
|
|
|
gst_live_adder_set_property (GObject * object,
|
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec);
|
|
|
|
static void
|
|
|
|
gst_live_adder_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec);
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
static GstPad *gst_live_adder_request_new_pad (GstElement * element,
|
2012-09-12 20:03:06 +00:00
|
|
|
GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
|
2009-02-10 16:18:32 +00:00
|
|
|
static void gst_live_adder_release_pad (GstElement * element, GstPad * pad);
|
2008-03-29 00:45:18 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_live_adder_change_state (GstElement * element, GstStateChange transition);
|
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
static gboolean gst_live_adder_setcaps (GstLiveAdder * adder, GstPad * pad,
|
|
|
|
GstCaps * caps);
|
|
|
|
static GstCaps *gst_live_adder_sink_getcaps (GstLiveAdder * adder, GstPad * pad,
|
|
|
|
GstCaps * filter);
|
|
|
|
static gboolean gst_live_adder_src_activate_mode (GstPad * pad,
|
|
|
|
GstObject * parent, GstPadMode mode, gboolean active);
|
|
|
|
static gboolean gst_live_adder_src_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
2008-03-29 00:45:18 +00:00
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
static void gst_live_adder_loop (gpointer data);
|
2012-09-12 20:03:06 +00:00
|
|
|
static gboolean gst_live_adder_src_query (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query);
|
|
|
|
static gboolean gst_live_adder_sink_query (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query);
|
|
|
|
static gboolean gst_live_adder_sink_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
static void reset_pad_private (GstPad * pad);
|
2008-05-14 23:16:43 +00:00
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
/* clipping versions */
|
|
|
|
#define MAKE_FUNC(name,type,ttype,min,max) \
|
|
|
|
static void name (type *out, type *in, gint bytes) { \
|
|
|
|
gint i; \
|
|
|
|
for (i = 0; i < bytes / sizeof (type); i++) \
|
|
|
|
out[i] = CLAMP ((ttype)out[i] + (ttype)in[i], min, max); \
|
|
|
|
}
|
|
|
|
|
|
|
|
/* non-clipping versions (for float) */
|
|
|
|
#define MAKE_FUNC_NC(name,type,ttype) \
|
|
|
|
static void name (type *out, type *in, gint bytes) { \
|
|
|
|
gint i; \
|
|
|
|
for (i = 0; i < bytes / sizeof (type); i++) \
|
|
|
|
out[i] = (ttype)out[i] + (ttype)in[i]; \
|
|
|
|
}
|
|
|
|
|
|
|
|
/* *INDENT-OFF* */
|
2008-04-24 22:46:27 +00:00
|
|
|
MAKE_FUNC (add_int32, gint32, gint64, G_MININT32, G_MAXINT32)
|
|
|
|
MAKE_FUNC (add_int16, gint16, gint32, G_MININT16, G_MAXINT16)
|
|
|
|
MAKE_FUNC (add_int8, gint8, gint16, G_MININT8, G_MAXINT8)
|
|
|
|
MAKE_FUNC (add_uint32, guint32, guint64, 0, G_MAXUINT32)
|
|
|
|
MAKE_FUNC (add_uint16, guint16, guint32, 0, G_MAXUINT16)
|
|
|
|
MAKE_FUNC (add_uint8, guint8, guint16, 0, G_MAXUINT8)
|
2008-03-29 00:45:18 +00:00
|
|
|
MAKE_FUNC_NC (add_float64, gdouble, gdouble)
|
|
|
|
MAKE_FUNC_NC (add_float32, gfloat, gfloat)
|
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_live_adder_class_init (GstLiveAdderClass * klass)
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
2012-09-12 20:03:06 +00:00
|
|
|
GObjectClass *gobject_class = (GObjectClass *) klass;
|
2010-09-28 11:16:58 +00:00
|
|
|
GstElementClass *gstelement_class = (GstElementClass *) klass;
|
2008-03-29 00:45:18 +00:00
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (live_adder_debug, "liveadder", 0, "Live Adder");
|
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
|
|
|
gst_static_pad_template_get (&gst_live_adder_src_template));
|
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
|
|
|
gst_static_pad_template_get (&gst_live_adder_sink_template));
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_class, "Live Adder element",
|
2010-03-18 16:30:26 +00:00
|
|
|
"Generic/Audio",
|
|
|
|
"Mixes live/discontinuous audio streams",
|
|
|
|
"Olivier Crete <olivier.crete@collabora.co.uk>");
|
2010-09-28 11:16:58 +00:00
|
|
|
|
|
|
|
gobject_class->finalize = gst_live_adder_finalize;
|
|
|
|
gobject_class->set_property = gst_live_adder_set_property;
|
|
|
|
gobject_class->get_property = gst_live_adder_get_property;
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
gstelement_class->request_new_pad = gst_live_adder_request_new_pad;
|
|
|
|
gstelement_class->release_pad = gst_live_adder_release_pad;
|
|
|
|
gstelement_class->change_state = gst_live_adder_change_state;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_LATENCY,
|
2013-04-26 19:33:18 +00:00
|
|
|
g_param_spec_uint ("latency", "Buffering latency",
|
|
|
|
"Amount of data to buffer (in milliseconds)",
|
|
|
|
0, G_MAXUINT, DEFAULT_LATENCY_MS,
|
2010-09-28 11:15:57 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2008-03-29 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_live_adder_init (GstLiveAdder * adder)
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
2010-11-28 04:13:38 +00:00
|
|
|
adder->srcpad =
|
|
|
|
gst_pad_new_from_static_template (&gst_live_adder_src_template, "src");
|
2008-03-29 00:45:18 +00:00
|
|
|
gst_pad_set_query_function (adder->srcpad,
|
2012-09-12 20:03:06 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_live_adder_src_query));
|
2008-04-02 22:46:12 +00:00
|
|
|
gst_pad_set_event_function (adder->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_live_adder_src_event));
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_pad_set_activatemode_function (adder->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_live_adder_src_activate_mode));
|
2008-03-29 00:45:18 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
|
|
|
|
|
|
|
|
adder->padcount = 0;
|
|
|
|
adder->func = NULL;
|
2013-02-09 19:53:51 +00:00
|
|
|
g_cond_init (&adder->not_empty_cond);
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
adder->next_timestamp = GST_CLOCK_TIME_NONE;
|
|
|
|
|
|
|
|
adder->latency_ms = DEFAULT_LATENCY_MS;
|
|
|
|
|
|
|
|
adder->buffers = g_queue_new ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_live_adder_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstLiveAdder *adder = GST_LIVE_ADDER (object);
|
|
|
|
|
2013-02-09 19:53:51 +00:00
|
|
|
g_cond_clear (&adder->not_empty_cond);
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
g_queue_foreach (adder->buffers, (GFunc) gst_mini_object_unref, NULL);
|
2009-02-10 16:18:32 +00:00
|
|
|
while (g_queue_pop_head (adder->buffers)) {
|
|
|
|
}
|
2008-03-29 00:45:18 +00:00
|
|
|
g_queue_free (adder->buffers);
|
|
|
|
|
|
|
|
g_list_free (adder->sinkpads);
|
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
G_OBJECT_CLASS (gst_live_adder_parent_class)->finalize (object);
|
2008-03-29 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_live_adder_set_property (GObject * object,
|
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstLiveAdder *adder = GST_LIVE_ADDER (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_LATENCY:
|
|
|
|
{
|
|
|
|
guint64 new_latency, old_latency;
|
|
|
|
|
|
|
|
new_latency = g_value_get_uint (value);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
old_latency = adder->latency_ms;
|
|
|
|
adder->latency_ms = new_latency;
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
|
|
|
|
/* post message if latency changed, this will inform the parent pipeline
|
|
|
|
* that a latency reconfiguration is possible/needed. */
|
|
|
|
if (new_latency != old_latency) {
|
|
|
|
GST_DEBUG_OBJECT (adder, "latency changed to: %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (new_latency));
|
|
|
|
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (adder),
|
|
|
|
gst_message_new_latency (GST_OBJECT_CAST (adder)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_live_adder_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstLiveAdder *adder = GST_LIVE_ADDER (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_LATENCY:
|
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
g_value_set_uint (value, adder->latency_ms);
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* we can only accept caps that we and downstream can handle. */
|
|
|
|
static GstCaps *
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_live_adder_sink_getcaps (GstLiveAdder * adder, GstPad * pad,
|
|
|
|
GstCaps * filter)
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
|
|
|
GstCaps *result, *peercaps, *sinkcaps;
|
|
|
|
|
|
|
|
/* get the downstream possible caps */
|
2012-09-12 20:03:06 +00:00
|
|
|
peercaps = gst_pad_peer_query_caps (adder->srcpad, filter);
|
2008-03-29 00:45:18 +00:00
|
|
|
/* get the allowed caps on this sinkpad, we use the fixed caps function so
|
|
|
|
* that it does not call recursively in this function. */
|
2012-09-12 20:03:06 +00:00
|
|
|
sinkcaps = gst_pad_get_current_caps (pad);
|
|
|
|
if (!sinkcaps)
|
|
|
|
sinkcaps = gst_pad_get_pad_template_caps (pad);
|
2008-03-29 00:45:18 +00:00
|
|
|
if (peercaps) {
|
|
|
|
/* if the peer has caps, intersect */
|
|
|
|
GST_DEBUG_OBJECT (adder, "intersecting peer and template caps");
|
|
|
|
result = gst_caps_intersect (peercaps, sinkcaps);
|
|
|
|
gst_caps_unref (sinkcaps);
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_caps_unref (peercaps);
|
2008-03-29 00:45:18 +00:00
|
|
|
} else {
|
|
|
|
/* the peer has no caps (or there is no peer), just use the allowed caps
|
|
|
|
* of this sinkpad. */
|
|
|
|
GST_DEBUG_OBJECT (adder, "no peer caps, using sinkcaps");
|
|
|
|
result = sinkcaps;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
struct SetCapsIterCtx
|
|
|
|
{
|
|
|
|
GstPad *pad;
|
|
|
|
GstCaps *caps;
|
|
|
|
gboolean all_valid;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_other_caps (const GValue * item, gpointer user_data)
|
|
|
|
{
|
|
|
|
GstPad *otherpad = GST_PAD (g_value_get_object (item));
|
|
|
|
struct SetCapsIterCtx *ctx = user_data;
|
|
|
|
|
|
|
|
if (otherpad == ctx->pad)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!gst_pad_peer_query_accept_caps (otherpad, ctx->caps))
|
|
|
|
ctx->all_valid = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_other_caps (const GValue * item, gpointer user_data)
|
|
|
|
{
|
|
|
|
GstPad *otherpad = GST_PAD (g_value_get_object (item));
|
|
|
|
struct SetCapsIterCtx *ctx = user_data;
|
|
|
|
|
|
|
|
if (otherpad == ctx->pad)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!gst_pad_set_caps (otherpad, ctx->caps))
|
|
|
|
ctx->all_valid = FALSE;
|
|
|
|
}
|
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
/* the first caps we receive on any of the sinkpads will define the caps for all
|
|
|
|
* the other sinkpads because we can only mix streams with the same caps.
|
|
|
|
* */
|
|
|
|
static gboolean
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_live_adder_setcaps (GstLiveAdder * adder, GstPad * pad, GstCaps * caps)
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
2012-09-12 20:03:06 +00:00
|
|
|
GstIterator *iter;
|
|
|
|
struct SetCapsIterCtx ctx;
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (adder, "setting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
|
|
|
|
GST_PAD_NAME (pad), caps);
|
|
|
|
|
|
|
|
/* FIXME, see if the other pads can accept the format. Also lock the
|
|
|
|
* format on the other pads to this new format. */
|
2012-09-12 20:03:06 +00:00
|
|
|
iter = gst_element_iterate_sink_pads (GST_ELEMENT (adder));
|
|
|
|
ctx.pad = pad;
|
|
|
|
ctx.caps = caps;
|
|
|
|
ctx.all_valid = TRUE;
|
|
|
|
while (gst_iterator_foreach (iter, check_other_caps, &ctx) ==
|
|
|
|
GST_ITERATOR_RESYNC) {
|
|
|
|
ctx.all_valid = TRUE;
|
|
|
|
gst_iterator_resync (iter);
|
|
|
|
}
|
|
|
|
if (!ctx.all_valid) {
|
|
|
|
GST_WARNING_OBJECT (adder, "Caps are not acceptable by other sinkpads");
|
|
|
|
gst_iterator_free (iter);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-03-29 00:45:18 +00:00
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
while (gst_iterator_foreach (iter, set_other_caps, &ctx) ==
|
|
|
|
GST_ITERATOR_RESYNC) {
|
|
|
|
ctx.all_valid = TRUE;
|
|
|
|
gst_iterator_resync (iter);
|
|
|
|
}
|
|
|
|
gst_iterator_free (iter);
|
|
|
|
|
|
|
|
if (!ctx.all_valid) {
|
|
|
|
GST_WARNING_OBJECT (adder, "Could not set caps on the other sink pads");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-03-29 00:45:18 +00:00
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
if (!gst_pad_set_caps (adder->srcpad, caps)) {
|
|
|
|
GST_WARNING_OBJECT (adder, "Could not set caps downstream");
|
|
|
|
return FALSE;
|
2008-03-29 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
GST_OBJECT_LOCK (adder);
|
2008-03-29 00:45:18 +00:00
|
|
|
/* parse caps now */
|
2012-09-12 20:03:06 +00:00
|
|
|
if (!gst_audio_info_from_caps (&adder->info, caps))
|
|
|
|
goto not_supported;
|
|
|
|
|
|
|
|
if (GST_AUDIO_INFO_IS_INTEGER (&adder->info)) {
|
|
|
|
switch (GST_AUDIO_INFO_WIDTH (&adder->info)) {
|
2008-03-29 00:45:18 +00:00
|
|
|
case 8:
|
2012-09-12 20:03:06 +00:00
|
|
|
adder->func = GST_AUDIO_INFO_IS_SIGNED (&adder->info) ?
|
|
|
|
(GstLiveAdderFunction) add_int8 : (GstLiveAdderFunction) add_uint8;
|
2008-03-29 00:45:18 +00:00
|
|
|
break;
|
|
|
|
case 16:
|
2012-09-12 20:03:06 +00:00
|
|
|
adder->func = GST_AUDIO_INFO_IS_SIGNED (&adder->info) ?
|
2009-02-10 16:18:32 +00:00
|
|
|
(GstLiveAdderFunction) add_int16 : (GstLiveAdderFunction)
|
2012-09-12 20:03:06 +00:00
|
|
|
add_uint16;
|
2008-03-29 00:45:18 +00:00
|
|
|
break;
|
|
|
|
case 32:
|
2012-09-12 20:03:06 +00:00
|
|
|
adder->func = GST_AUDIO_INFO_IS_SIGNED (&adder->info) ?
|
2009-02-10 16:18:32 +00:00
|
|
|
(GstLiveAdderFunction) add_int32 : (GstLiveAdderFunction)
|
2012-09-12 20:03:06 +00:00
|
|
|
add_uint32;
|
2008-03-29 00:45:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto not_supported;
|
|
|
|
}
|
2012-09-12 20:03:06 +00:00
|
|
|
} else if (GST_AUDIO_INFO_IS_FLOAT (&adder->info)) {
|
|
|
|
switch (GST_AUDIO_INFO_WIDTH (&adder->info)) {
|
2008-03-29 00:45:18 +00:00
|
|
|
case 32:
|
|
|
|
adder->func = (GstLiveAdderFunction) add_float32;
|
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
adder->func = (GstLiveAdderFunction) add_float64;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto not_supported;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
goto not_supported;
|
|
|
|
}
|
|
|
|
|
2008-04-28 23:49:48 +00:00
|
|
|
GST_OBJECT_UNLOCK (adder);
|
2008-03-29 00:45:18 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
not_supported:
|
|
|
|
{
|
2008-04-28 23:49:48 +00:00
|
|
|
GST_OBJECT_UNLOCK (adder);
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_DEBUG_OBJECT (adder, "unsupported format set as caps");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_live_adder_flush_start (GstLiveAdder * adder)
|
|
|
|
{
|
2008-03-31 17:11:16 +00:00
|
|
|
GST_DEBUG_OBJECT (adder, "Disabling pop on queue");
|
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
/* mark ourselves as flushing */
|
2012-02-08 15:37:13 +00:00
|
|
|
adder->srcresult = GST_FLOW_FLUSHING;
|
2008-05-14 23:26:43 +00:00
|
|
|
|
|
|
|
/* Empty the queue */
|
|
|
|
g_queue_foreach (adder->buffers, (GFunc) gst_mini_object_unref, NULL);
|
2009-02-10 16:18:32 +00:00
|
|
|
while (g_queue_pop_head (adder->buffers));
|
2008-05-14 23:26:43 +00:00
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
/* unlock clock, we just unschedule, the entry will be released by the
|
|
|
|
* locking streaming thread. */
|
|
|
|
if (adder->clock_id)
|
|
|
|
gst_clock_id_unschedule (adder->clock_id);
|
2008-04-21 03:26:14 +00:00
|
|
|
|
2013-02-09 19:53:51 +00:00
|
|
|
g_cond_broadcast (&adder->not_empty_cond);
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_live_adder_src_activate_mode (GstPad * pad, GstObject * parent,
|
|
|
|
GstPadMode mode, gboolean active)
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
2012-09-12 20:03:06 +00:00
|
|
|
GstLiveAdder *adder = GST_LIVE_ADDER (parent);
|
2008-03-29 00:45:18 +00:00
|
|
|
gboolean result = TRUE;
|
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
if (mode == GST_PAD_MODE_PULL)
|
|
|
|
return FALSE;
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
if (active) {
|
2008-05-15 00:02:14 +00:00
|
|
|
/* Mark as non flushing */
|
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
adder->srcresult = GST_FLOW_OK;
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
/* start pushing out buffers */
|
|
|
|
GST_DEBUG_OBJECT (adder, "Starting task on srcpad");
|
|
|
|
gst_pad_start_task (adder->srcpad,
|
2012-06-20 08:34:48 +00:00
|
|
|
(GstTaskFunction) gst_live_adder_loop, adder, NULL);
|
2008-03-29 00:45:18 +00:00
|
|
|
} else {
|
|
|
|
/* make sure all data processing stops ASAP */
|
|
|
|
gst_live_adder_flush_start (adder);
|
|
|
|
|
|
|
|
/* NOTE this will hardlock if the state change is called from the src pad
|
|
|
|
* task thread because we will _join() the thread. */
|
|
|
|
GST_DEBUG_OBJECT (adder, "Stopping task on srcpad");
|
|
|
|
result = gst_pad_stop_task (pad);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_live_adder_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
2012-09-12 20:03:06 +00:00
|
|
|
GstLiveAdder *adder = GST_LIVE_ADDER (parent);
|
2008-03-29 00:45:18 +00:00
|
|
|
GstLiveAdderPadPrivate *padprivate = NULL;
|
2012-09-12 20:03:06 +00:00
|
|
|
gboolean ret = TRUE;
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
padprivate = gst_pad_get_element_private (pad);
|
|
|
|
|
|
|
|
if (!padprivate)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (adder, "received %s", GST_EVENT_TYPE_NAME (event));
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
2012-09-12 20:03:06 +00:00
|
|
|
case GST_EVENT_CAPS:
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
2012-09-12 20:03:06 +00:00
|
|
|
GstCaps *caps;
|
2008-03-29 00:45:18 +00:00
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_event_parse_caps (event, &caps);
|
|
|
|
ret = gst_live_adder_setcaps (adder, pad, caps);
|
2008-04-18 19:29:41 +00:00
|
|
|
gst_event_unref (event);
|
2012-09-12 20:03:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_EVENT_SEGMENT:
|
|
|
|
{
|
|
|
|
const GstSegment *segment;
|
|
|
|
GstSegment livesegment;
|
|
|
|
|
|
|
|
gst_event_parse_segment (event, &segment);
|
2008-04-18 19:29:41 +00:00
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
/* we need time for now */
|
2012-09-12 20:03:06 +00:00
|
|
|
if (segment->format != GST_FORMAT_TIME)
|
2008-03-29 00:45:18 +00:00
|
|
|
goto newseg_wrong_format;
|
|
|
|
|
|
|
|
/* now configure the values, we need these to time the release of the
|
|
|
|
* buffers on the srcpad. */
|
|
|
|
GST_OBJECT_LOCK (adder);
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_segment_copy_into (segment, &padprivate->segment);
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_OBJECT_UNLOCK (adder);
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
|
|
|
|
gst_segment_init (&livesegment, GST_FORMAT_TIME);
|
|
|
|
gst_pad_push_event (adder->srcpad, gst_event_new_segment (&livesegment));
|
2008-03-29 00:45:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_EVENT_FLUSH_START:
|
|
|
|
gst_live_adder_flush_start (adder);
|
|
|
|
ret = gst_pad_push_event (adder->srcpad, event);
|
|
|
|
break;
|
|
|
|
case GST_EVENT_FLUSH_STOP:
|
2008-03-31 18:44:21 +00:00
|
|
|
GST_OBJECT_LOCK (adder);
|
2008-05-15 01:22:57 +00:00
|
|
|
adder->next_timestamp = GST_CLOCK_TIME_NONE;
|
2008-05-14 23:17:52 +00:00
|
|
|
reset_pad_private (pad);
|
2008-03-31 18:44:21 +00:00
|
|
|
GST_OBJECT_UNLOCK (adder);
|
2008-05-15 01:22:57 +00:00
|
|
|
ret = gst_pad_push_event (adder->srcpad, event);
|
2012-09-12 20:03:06 +00:00
|
|
|
ret = gst_live_adder_src_activate_mode (adder->srcpad, GST_OBJECT (adder),
|
|
|
|
GST_PAD_MODE_PUSH, TRUE);
|
2008-03-29 00:45:18 +00:00
|
|
|
break;
|
|
|
|
case GST_EVENT_EOS:
|
|
|
|
{
|
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
|
|
|
|
ret = adder->srcresult == GST_FLOW_OK;
|
|
|
|
if (ret && !padprivate->eos) {
|
|
|
|
GST_DEBUG_OBJECT (adder, "queuing EOS");
|
|
|
|
padprivate->eos = TRUE;
|
2013-02-09 19:53:51 +00:00
|
|
|
g_cond_broadcast (&adder->not_empty_cond);
|
2008-03-29 00:45:18 +00:00
|
|
|
} else if (padprivate->eos) {
|
|
|
|
GST_DEBUG_OBJECT (adder, "dropping EOS, we are already EOS");
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (adder, "dropping EOS, reason %s",
|
|
|
|
gst_flow_get_name (adder->srcresult));
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
ret = gst_pad_push_event (adder->srcpad, event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
newseg_wrong_format:
|
|
|
|
{
|
2012-09-12 20:03:06 +00:00
|
|
|
GST_DEBUG_OBJECT (adder, "received non TIME segment");
|
2008-03-29 00:45:18 +00:00
|
|
|
ret = FALSE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-15 01:21:42 +00:00
|
|
|
static gboolean
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_live_adder_query_pos_dur (GstLiveAdder * adder, GstFormat format,
|
2009-02-10 16:18:32 +00:00
|
|
|
gboolean position, gint64 * outvalue)
|
2008-05-15 01:21:42 +00:00
|
|
|
{
|
2008-07-04 18:57:21 +00:00
|
|
|
gint64 max = G_MININT64;
|
|
|
|
gboolean res = TRUE;
|
2008-05-15 01:21:42 +00:00
|
|
|
GstIterator *it;
|
2008-07-04 18:57:21 +00:00
|
|
|
gboolean done = FALSE;
|
2008-05-15 01:21:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
|
|
|
|
while (!done) {
|
|
|
|
GstIteratorResult ires;
|
2012-09-12 20:03:06 +00:00
|
|
|
GValue item = { 0 };
|
2008-05-15 01:21:42 +00:00
|
|
|
|
|
|
|
ires = gst_iterator_next (it, &item);
|
|
|
|
switch (ires) {
|
|
|
|
case GST_ITERATOR_DONE:
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
case GST_ITERATOR_OK:
|
2009-02-10 16:18:32 +00:00
|
|
|
{
|
2012-09-12 20:03:06 +00:00
|
|
|
GstPad *pad = GST_PAD_CAST (g_value_get_object (&item));
|
2009-02-10 16:18:32 +00:00
|
|
|
gint64 value;
|
|
|
|
gboolean curres;
|
|
|
|
|
|
|
|
/* ask sink peer for duration */
|
|
|
|
if (position)
|
2012-09-12 20:03:06 +00:00
|
|
|
curres = gst_pad_peer_query_position (pad, format, &value);
|
2009-02-10 16:18:32 +00:00
|
|
|
else
|
2012-09-12 20:03:06 +00:00
|
|
|
curres = gst_pad_peer_query_duration (pad, format, &value);
|
2009-02-10 16:18:32 +00:00
|
|
|
|
|
|
|
/* take max from all valid return values */
|
|
|
|
/* Only if the format is the one we requested, otherwise ignore it ?
|
|
|
|
*/
|
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
if (curres) {
|
2009-02-10 16:18:32 +00:00
|
|
|
res &= curres;
|
|
|
|
|
|
|
|
/* valid unknown length, stop searching */
|
|
|
|
if (value == -1) {
|
|
|
|
max = value;
|
|
|
|
done = TRUE;
|
|
|
|
} else if (value > max) {
|
|
|
|
max = value;
|
2008-05-15 01:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-10 16:18:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-05-15 01:21:42 +00:00
|
|
|
case GST_ITERATOR_RESYNC:
|
|
|
|
max = -1;
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = FALSE;
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gst_iterator_free (it);
|
|
|
|
|
2008-07-04 18:57:21 +00:00
|
|
|
if (res)
|
|
|
|
*outvalue = max;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME:
|
|
|
|
*
|
|
|
|
* When we add a new stream (or remove a stream) the duration might
|
|
|
|
* also become invalid again and we need to post a new DURATION
|
|
|
|
* message to notify this fact to the parent.
|
|
|
|
* For now we take the max of all the upstream elements so the simple
|
|
|
|
* cases work at least somewhat.
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
gst_live_adder_query_duration (GstLiveAdder * adder, GstQuery * query)
|
|
|
|
{
|
|
|
|
GstFormat format;
|
|
|
|
gint64 max;
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
/* parse format */
|
|
|
|
gst_query_parse_duration (query, &format, NULL);
|
|
|
|
|
|
|
|
res = gst_live_adder_query_pos_dur (adder, format, FALSE, &max);
|
|
|
|
|
2008-05-15 01:21:42 +00:00
|
|
|
if (res) {
|
|
|
|
/* and store the max */
|
|
|
|
gst_query_set_duration (query, format, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-07-04 18:57:21 +00:00
|
|
|
static gboolean
|
|
|
|
gst_live_adder_query_position (GstLiveAdder * adder, GstQuery * query)
|
|
|
|
{
|
|
|
|
GstFormat format;
|
|
|
|
gint64 max;
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
/* parse format */
|
|
|
|
gst_query_parse_position (query, &format, NULL);
|
|
|
|
|
|
|
|
res = gst_live_adder_query_pos_dur (adder, format, TRUE, &max);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
/* and store the max */
|
|
|
|
gst_query_set_position (query, format, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-15 01:21:42 +00:00
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
static gboolean
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_live_adder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
2012-09-12 20:03:06 +00:00
|
|
|
GstLiveAdder *adder = GST_LIVE_ADDER (parent);
|
2008-03-29 00:45:18 +00:00
|
|
|
gboolean res = FALSE;
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_LATENCY:
|
|
|
|
{
|
|
|
|
/* We need to send the query upstream and add the returned latency to our
|
|
|
|
* own */
|
2008-04-03 16:54:15 +00:00
|
|
|
GstClockTime min_latency = 0, max_latency = G_MAXUINT64;
|
2012-09-12 20:03:06 +00:00
|
|
|
GValue item = { 0 };
|
2008-03-29 00:45:18 +00:00
|
|
|
GstIterator *iter = NULL;
|
|
|
|
gboolean done = FALSE;
|
|
|
|
|
|
|
|
iter = gst_element_iterate_sink_pads (GST_ELEMENT (adder));
|
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
switch (gst_iterator_next (iter, &item)) {
|
|
|
|
case GST_ITERATOR_OK:
|
2009-02-10 16:18:32 +00:00
|
|
|
{
|
2012-09-12 20:03:06 +00:00
|
|
|
GstPad *sinkpad = GST_PAD (g_value_get_object (&item));
|
2009-02-10 16:18:32 +00:00
|
|
|
GstClockTime pad_min_latency, pad_max_latency;
|
|
|
|
gboolean pad_us_live;
|
|
|
|
|
|
|
|
if (gst_pad_peer_query (sinkpad, query)) {
|
|
|
|
gst_query_parse_latency (query, &pad_us_live, &pad_min_latency,
|
|
|
|
&pad_max_latency);
|
|
|
|
|
|
|
|
res = TRUE;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (adder, "Peer latency for pad %s: min %"
|
|
|
|
GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
|
|
|
|
GST_PAD_NAME (sinkpad),
|
|
|
|
GST_TIME_ARGS (pad_min_latency),
|
|
|
|
GST_TIME_ARGS (pad_max_latency));
|
|
|
|
|
|
|
|
min_latency = MAX (pad_min_latency, min_latency);
|
|
|
|
max_latency = MIN (pad_max_latency, max_latency);
|
2008-03-29 00:45:18 +00:00
|
|
|
}
|
2009-02-10 16:18:32 +00:00
|
|
|
}
|
2008-03-29 00:45:18 +00:00
|
|
|
break;
|
|
|
|
case GST_ITERATOR_RESYNC:
|
2008-04-03 16:54:15 +00:00
|
|
|
min_latency = 0;
|
|
|
|
max_latency = G_MAXUINT64;
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
gst_iterator_resync (iter);
|
|
|
|
break;
|
|
|
|
case GST_ITERATOR_ERROR:
|
|
|
|
GST_ERROR_OBJECT (adder, "Error looping sink pads");
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
case GST_ITERATOR_DONE:
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gst_iterator_free (iter);
|
|
|
|
|
|
|
|
if (res) {
|
2008-04-28 23:28:51 +00:00
|
|
|
GstClockTime my_latency = adder->latency_ms * GST_MSECOND;
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
adder->peer_latency = min_latency;
|
2008-04-28 23:28:51 +00:00
|
|
|
min_latency += my_latency;
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
|
2008-04-28 23:28:51 +00:00
|
|
|
/* Make sure we don't risk an overflow */
|
|
|
|
if (max_latency < G_MAXUINT64 - my_latency)
|
|
|
|
max_latency += my_latency;
|
|
|
|
else
|
2008-04-29 14:08:02 +00:00
|
|
|
max_latency = G_MAXUINT64;
|
2008-03-29 00:45:18 +00:00
|
|
|
gst_query_set_latency (query, TRUE, min_latency, max_latency);
|
|
|
|
GST_DEBUG_OBJECT (adder, "Calculated total latency : min %"
|
|
|
|
GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-05-15 01:21:42 +00:00
|
|
|
case GST_QUERY_DURATION:
|
|
|
|
res = gst_live_adder_query_duration (adder, query);
|
|
|
|
break;
|
2008-07-04 18:57:21 +00:00
|
|
|
case GST_QUERY_POSITION:
|
|
|
|
res = gst_live_adder_query_position (adder, query);
|
|
|
|
break;
|
2008-03-29 00:45:18 +00:00
|
|
|
default:
|
2012-09-12 20:03:06 +00:00
|
|
|
res = gst_pad_query_default (pad, parent, query);
|
2008-03-29 00:45:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_live_adder_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
|
|
|
|
{
|
|
|
|
GstLiveAdder *adder = GST_LIVE_ADDER (parent);
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_CAPS:
|
|
|
|
{
|
|
|
|
GstCaps *filter;
|
|
|
|
GstCaps *result;
|
|
|
|
|
|
|
|
gst_query_parse_caps (query, &filter);
|
|
|
|
result = gst_live_adder_sink_getcaps (adder, pad, filter);
|
|
|
|
gst_query_set_caps_result (query, result);
|
2014-04-09 13:07:19 +00:00
|
|
|
gst_caps_unref (result);
|
2012-09-12 20:03:06 +00:00
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
res = gst_pad_query_default (pad, parent, query);
|
|
|
|
break;
|
|
|
|
}
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-04-02 22:46:12 +00:00
|
|
|
static gboolean
|
2012-09-12 20:03:06 +00:00
|
|
|
forward_event_func (const GValue * item, GValue * ret, gpointer user_data)
|
2008-04-02 22:46:12 +00:00
|
|
|
{
|
2012-09-12 20:03:06 +00:00
|
|
|
GstPad *pad = GST_PAD (g_value_get_object (item));
|
|
|
|
GstEvent *event = user_data;
|
|
|
|
|
2008-04-02 22:46:12 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* forwards the event to all sinkpads, takes ownership of the
|
|
|
|
* event
|
|
|
|
*
|
|
|
|
* Returns: TRUE if the event could be forwarded on all
|
|
|
|
* sinkpads.
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
forward_event (GstLiveAdder * adder, GstEvent * event)
|
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
GstIterator *it;
|
|
|
|
GValue vret = { 0 };
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (adder, "Forwarding event %p (%s)", event,
|
|
|
|
GST_EVENT_TYPE_NAME (event));
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
g_value_init (&vret, G_TYPE_BOOLEAN);
|
|
|
|
g_value_set_boolean (&vret, TRUE);
|
|
|
|
it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_iterator_fold (it, forward_event_func, &vret, event);
|
2008-04-02 22:46:12 +00:00
|
|
|
gst_iterator_free (it);
|
|
|
|
|
|
|
|
ret = g_value_get_boolean (&vret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_live_adder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2008-04-02 22:46:12 +00:00
|
|
|
{
|
2012-09-12 20:03:06 +00:00
|
|
|
GstLiveAdder *adder = GST_LIVE_ADDER (parent);
|
2008-04-02 22:46:12 +00:00
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_QOS:
|
2008-04-28 16:12:35 +00:00
|
|
|
/* TODO : QoS might be tricky */
|
2008-04-02 22:46:12 +00:00
|
|
|
result = FALSE;
|
|
|
|
break;
|
|
|
|
case GST_EVENT_NAVIGATION:
|
2008-04-28 16:12:35 +00:00
|
|
|
/* TODO : navigation is rather pointless. */
|
2008-04-02 22:46:12 +00:00
|
|
|
result = FALSE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* just forward the rest for now */
|
|
|
|
result = forward_event (adder, event);
|
|
|
|
break;
|
|
|
|
}
|
2008-04-28 18:07:11 +00:00
|
|
|
|
|
|
|
gst_event_unref (event);
|
2008-04-02 22:46:12 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
static guint
|
2009-02-10 16:18:32 +00:00
|
|
|
gst_live_adder_length_from_duration (GstLiveAdder * adder,
|
|
|
|
GstClockTime duration)
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
2012-09-12 20:03:06 +00:00
|
|
|
guint64 ret = GST_AUDIO_INFO_BPF (&adder->info) *
|
2013-09-18 15:51:26 +00:00
|
|
|
gst_util_uint64_scale_int_round (duration,
|
|
|
|
GST_AUDIO_INFO_RATE (&adder->info), GST_SECOND);
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
return (guint) ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_live_live_adder_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
2012-09-12 20:03:06 +00:00
|
|
|
GstLiveAdder *adder = GST_LIVE_ADDER (parent);
|
2008-03-29 00:45:18 +00:00
|
|
|
GstLiveAdderPadPrivate *padprivate = NULL;
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
GList *item = NULL;
|
|
|
|
GstClockTime skip = 0;
|
2009-02-10 16:18:32 +00:00
|
|
|
gint64 drift = 0; /* Positive if new buffer after old buffer */
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
|
|
|
|
ret = adder->srcresult;
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
GST_DEBUG ("Incoming buffer time:%" GST_TIME_FORMAT " duration:%"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
|
2008-08-27 15:16:27 +00:00
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
2008-05-02 19:10:04 +00:00
|
|
|
GST_DEBUG_OBJECT (adder, "Passing non-ok result from src: %s",
|
|
|
|
gst_flow_get_name (ret));
|
2008-03-29 00:45:18 +00:00
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
padprivate = gst_pad_get_element_private (pad);
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
if (!padprivate) {
|
2008-03-29 00:45:18 +00:00
|
|
|
ret = GST_FLOW_NOT_LINKED;
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
if (padprivate->eos) {
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_DEBUG_OBJECT (adder, "Received buffer after EOS");
|
2012-09-12 20:03:06 +00:00
|
|
|
ret = GST_FLOW_EOS;
|
2008-03-29 00:45:18 +00:00
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
|
2008-04-28 22:36:06 +00:00
|
|
|
goto invalid_timestamp;
|
2008-03-29 00:45:18 +00:00
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
if (padprivate->segment.format == GST_FORMAT_UNDEFINED) {
|
2008-05-01 18:25:54 +00:00
|
|
|
GST_WARNING_OBJECT (adder, "No new-segment received,"
|
|
|
|
" initializing segment with time 0..-1");
|
|
|
|
gst_segment_init (&padprivate->segment, GST_FORMAT_TIME);
|
|
|
|
}
|
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
buffer = gst_buffer_make_writable (buffer);
|
2008-08-27 15:16:27 +00:00
|
|
|
|
|
|
|
drift = GST_BUFFER_TIMESTAMP (buffer) - padprivate->expected_timestamp;
|
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
/* Just see if we receive invalid timestamp/durations */
|
2008-04-03 00:57:28 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (padprivate->expected_timestamp) &&
|
2009-02-10 16:18:32 +00:00
|
|
|
!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT) &&
|
2008-08-27 15:16:27 +00:00
|
|
|
(drift != 0)) {
|
2008-04-03 17:09:56 +00:00
|
|
|
GST_LOG_OBJECT (adder,
|
2008-03-29 00:45:18 +00:00
|
|
|
"Timestamp discontinuity without the DISCONT flag set"
|
2009-02-10 16:18:32 +00:00
|
|
|
" (expected %" GST_TIME_FORMAT ", got %" GST_TIME_FORMAT
|
2009-02-23 14:25:03 +00:00
|
|
|
" drift:%" G_GINT64_FORMAT "ms)",
|
|
|
|
GST_TIME_ARGS (padprivate->expected_timestamp),
|
2009-02-10 16:18:32 +00:00
|
|
|
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)), drift / GST_MSECOND);
|
2008-08-27 15:16:27 +00:00
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
/* We accept drifts of 10ms */
|
|
|
|
if (ABS (drift) < (10 * GST_MSECOND)) {
|
2008-08-27 15:16:27 +00:00
|
|
|
GST_DEBUG ("Correcting minor drift");
|
|
|
|
GST_BUFFER_TIMESTAMP (buffer) = padprivate->expected_timestamp;
|
|
|
|
}
|
|
|
|
}
|
2008-03-29 00:45:18 +00:00
|
|
|
|
2008-04-03 00:57:28 +00:00
|
|
|
|
|
|
|
/* If there is no duration, lets set one */
|
|
|
|
if (!GST_BUFFER_DURATION_IS_VALID (buffer)) {
|
2012-09-12 20:03:06 +00:00
|
|
|
GST_BUFFER_DURATION (buffer) = (gst_buffer_get_size (buffer) * GST_SECOND) /
|
|
|
|
(GST_AUDIO_INFO_BPF (&adder->info) *
|
|
|
|
GST_AUDIO_INFO_RATE (&adder->info));
|
2008-04-03 00:57:28 +00:00
|
|
|
padprivate->expected_timestamp = GST_CLOCK_TIME_NONE;
|
|
|
|
} else {
|
|
|
|
padprivate->expected_timestamp = GST_BUFFER_TIMESTAMP (buffer) +
|
2008-04-03 00:40:12 +00:00
|
|
|
GST_BUFFER_DURATION (buffer);
|
2008-04-03 00:57:28 +00:00
|
|
|
}
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lets clip the buffer to the segment (so we don't have to worry about
|
|
|
|
* cliping afterwards).
|
|
|
|
* This should also guarantee us that we'll have valid timestamps and
|
|
|
|
* durations afterwards
|
|
|
|
*/
|
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
buffer = gst_audio_buffer_clip (buffer, &padprivate->segment,
|
|
|
|
GST_AUDIO_INFO_RATE (&adder->info), GST_AUDIO_INFO_BPF (&adder->info));
|
2008-03-29 00:45:18 +00:00
|
|
|
|
2008-04-29 00:03:39 +00:00
|
|
|
/* buffer can be NULL if it's completely outside of the segment */
|
2008-08-27 15:16:27 +00:00
|
|
|
if (!buffer) {
|
|
|
|
GST_DEBUG ("Buffer completely outside of configured segment, dropping it");
|
2008-04-29 00:03:39 +00:00
|
|
|
goto out;
|
2008-08-27 15:16:27 +00:00
|
|
|
}
|
2008-04-29 00:03:39 +00:00
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
/*
|
|
|
|
* Make sure all incoming buffers share the same timestamping
|
|
|
|
*/
|
2009-02-10 16:18:32 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (buffer) =
|
|
|
|
gst_segment_to_running_time (&padprivate->segment,
|
|
|
|
padprivate->segment.format, GST_BUFFER_TIMESTAMP (buffer));
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (adder->next_timestamp) &&
|
|
|
|
GST_BUFFER_TIMESTAMP (buffer) < adder->next_timestamp) {
|
|
|
|
if (GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer) <
|
|
|
|
adder->next_timestamp) {
|
|
|
|
GST_DEBUG_OBJECT (adder, "Buffer is late, dropping (ts: %" GST_TIME_FORMAT
|
|
|
|
" duration: %" GST_TIME_FORMAT ")",
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
goto out;
|
|
|
|
} else {
|
|
|
|
skip = adder->next_timestamp - GST_BUFFER_TIMESTAMP (buffer);
|
|
|
|
GST_DEBUG_OBJECT (adder, "Buffer is partially late, skipping %"
|
2009-02-10 16:18:32 +00:00
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (skip));
|
2008-03-29 00:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If our new buffer's head is higher than the queue's head, lets wake up,
|
|
|
|
* we may not have to wait for as long
|
|
|
|
*/
|
|
|
|
if (adder->clock_id &&
|
2008-04-29 00:36:00 +00:00
|
|
|
g_queue_peek_head (adder->buffers) != NULL &&
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (buffer) + skip <
|
|
|
|
GST_BUFFER_TIMESTAMP (g_queue_peek_head (adder->buffers)))
|
|
|
|
gst_clock_id_unschedule (adder->clock_id);
|
|
|
|
|
|
|
|
for (item = g_queue_peek_head_link (adder->buffers);
|
2009-02-10 16:18:32 +00:00
|
|
|
item; item = g_list_next (item)) {
|
2008-03-29 00:45:18 +00:00
|
|
|
GstBuffer *oldbuffer = item->data;
|
|
|
|
GstClockTime old_skip = 0;
|
|
|
|
GstClockTime mix_duration = 0;
|
|
|
|
GstClockTime mix_start = 0;
|
|
|
|
GstClockTime mix_end = 0;
|
2012-09-12 20:03:06 +00:00
|
|
|
GstMapInfo oldmap, map;
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
/* We haven't reached our place yet */
|
|
|
|
if (GST_BUFFER_TIMESTAMP (buffer) + skip >=
|
|
|
|
GST_BUFFER_TIMESTAMP (oldbuffer) + GST_BUFFER_DURATION (oldbuffer))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We're past our place, lets insert ouselves here */
|
|
|
|
if (GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer) <=
|
|
|
|
GST_BUFFER_TIMESTAMP (oldbuffer))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* if we reach this spot, we have overlap, so we must mix */
|
|
|
|
|
|
|
|
/* First make a subbuffer with the non-overlapping part */
|
2009-02-10 16:18:32 +00:00
|
|
|
if (GST_BUFFER_TIMESTAMP (buffer) + skip < GST_BUFFER_TIMESTAMP (oldbuffer)) {
|
2008-03-29 00:45:18 +00:00
|
|
|
GstBuffer *subbuffer = NULL;
|
|
|
|
GstClockTime subbuffer_duration = GST_BUFFER_TIMESTAMP (oldbuffer) -
|
|
|
|
(GST_BUFFER_TIMESTAMP (buffer) + skip);
|
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
subbuffer = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL,
|
2008-03-29 00:45:18 +00:00
|
|
|
gst_live_adder_length_from_duration (adder, skip),
|
|
|
|
gst_live_adder_length_from_duration (adder, subbuffer_duration));
|
|
|
|
|
|
|
|
GST_BUFFER_TIMESTAMP (subbuffer) = GST_BUFFER_TIMESTAMP (buffer) + skip;
|
|
|
|
GST_BUFFER_DURATION (subbuffer) = subbuffer_duration;
|
|
|
|
|
|
|
|
skip += subbuffer_duration;
|
|
|
|
|
|
|
|
g_queue_insert_before (adder->buffers, item, subbuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we are on the overlapping part */
|
|
|
|
oldbuffer = gst_buffer_make_writable (oldbuffer);
|
|
|
|
item->data = oldbuffer;
|
|
|
|
|
|
|
|
old_skip = GST_BUFFER_TIMESTAMP (buffer) + skip -
|
|
|
|
GST_BUFFER_TIMESTAMP (oldbuffer);
|
|
|
|
|
2008-04-29 00:34:12 +00:00
|
|
|
mix_start = GST_BUFFER_TIMESTAMP (oldbuffer) + old_skip;
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
if (GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer) <
|
|
|
|
GST_BUFFER_TIMESTAMP (oldbuffer) + GST_BUFFER_DURATION (oldbuffer))
|
|
|
|
mix_end = GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
|
|
|
|
else
|
|
|
|
mix_end = GST_BUFFER_TIMESTAMP (oldbuffer) +
|
|
|
|
GST_BUFFER_DURATION (oldbuffer);
|
|
|
|
|
|
|
|
mix_duration = mix_end - mix_start;
|
|
|
|
|
2013-10-14 14:14:42 +00:00
|
|
|
if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_GAP)) {
|
|
|
|
GST_BUFFER_FLAG_UNSET (oldbuffer, GST_BUFFER_FLAG_GAP);
|
|
|
|
gst_buffer_map (oldbuffer, &oldmap, GST_MAP_WRITE);
|
|
|
|
gst_buffer_map (buffer, &map, GST_MAP_READ);
|
|
|
|
adder->func (oldmap.data +
|
|
|
|
gst_live_adder_length_from_duration (adder, old_skip),
|
|
|
|
map.data +
|
|
|
|
gst_live_adder_length_from_duration (adder, skip),
|
|
|
|
gst_live_adder_length_from_duration (adder, mix_duration));
|
|
|
|
gst_buffer_unmap (oldbuffer, &oldmap);
|
|
|
|
gst_buffer_unmap (buffer, &map);
|
|
|
|
}
|
2008-03-29 00:45:18 +00:00
|
|
|
skip += mix_duration;
|
|
|
|
}
|
|
|
|
|
2013-02-09 19:53:51 +00:00
|
|
|
g_cond_broadcast (&adder->not_empty_cond);
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
if (skip == GST_BUFFER_DURATION (buffer)) {
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
} else {
|
|
|
|
if (skip) {
|
|
|
|
GstClockTime subbuffer_duration = GST_BUFFER_DURATION (buffer) - skip;
|
|
|
|
GstClockTime subbuffer_ts = GST_BUFFER_TIMESTAMP (buffer) + skip;
|
2012-09-12 20:03:06 +00:00
|
|
|
GstBuffer *new_buffer = gst_buffer_copy_region (buffer,
|
|
|
|
GST_BUFFER_COPY_ALL,
|
2008-03-29 00:45:18 +00:00
|
|
|
gst_live_adder_length_from_duration (adder, skip),
|
|
|
|
gst_live_adder_length_from_duration (adder, subbuffer_duration));
|
2011-12-12 13:59:33 +00:00
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
buffer = new_buffer;
|
2012-09-12 20:03:06 +00:00
|
|
|
GST_BUFFER_PTS (buffer) = subbuffer_ts;
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_BUFFER_DURATION (buffer) = subbuffer_duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item)
|
|
|
|
g_queue_insert_before (adder->buffers, item, buffer);
|
|
|
|
else
|
|
|
|
g_queue_push_tail (adder->buffers, buffer);
|
|
|
|
}
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
out:
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
|
|
|
|
return ret;
|
2008-04-28 22:36:06 +00:00
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
invalid_timestamp:
|
2008-04-28 22:36:06 +00:00
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
GST_ELEMENT_ERROR (adder, STREAM, FAILED,
|
|
|
|
("Buffer without a valid timestamp received"),
|
|
|
|
("Invalid timestamp received on buffer"));
|
|
|
|
|
|
|
|
return GST_FLOW_ERROR;
|
2008-03-29 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This only works because the GstObject lock is taken
|
|
|
|
*
|
|
|
|
* It checks if all sink pads are EOS
|
|
|
|
*/
|
|
|
|
static gboolean
|
2009-02-10 16:18:32 +00:00
|
|
|
check_eos_locked (GstLiveAdder * adder)
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
|
|
|
GList *item;
|
|
|
|
|
2008-05-02 19:09:54 +00:00
|
|
|
/* We can't be EOS if we have no sinkpads */
|
|
|
|
if (adder->sinkpads == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
for (item = adder->sinkpads; item; item = g_list_next (item)) {
|
2008-03-29 00:45:18 +00:00
|
|
|
GstPad *pad = item->data;
|
|
|
|
GstLiveAdderPadPrivate *padprivate = gst_pad_get_element_private (pad);
|
|
|
|
|
|
|
|
if (padprivate && padprivate->eos != TRUE)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-04-28 15:59:27 +00:00
|
|
|
return TRUE;
|
2008-03-29 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_live_adder_loop (gpointer data)
|
|
|
|
{
|
|
|
|
GstLiveAdder *adder = GST_LIVE_ADDER (data);
|
|
|
|
GstClockTime buffer_timestamp = 0;
|
|
|
|
GstClockTime sync_time = 0;
|
|
|
|
GstClock *clock = NULL;
|
|
|
|
GstClockID id = NULL;
|
|
|
|
GstClockReturn ret;
|
|
|
|
GstBuffer *buffer = NULL;
|
|
|
|
GstFlowReturn result;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
again:
|
2008-03-29 00:45:18 +00:00
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
for (;;) {
|
2008-04-21 03:26:14 +00:00
|
|
|
if (adder->srcresult != GST_FLOW_OK)
|
|
|
|
goto flushing;
|
2008-03-29 00:45:18 +00:00
|
|
|
if (!g_queue_is_empty (adder->buffers))
|
|
|
|
break;
|
|
|
|
if (check_eos_locked (adder))
|
|
|
|
goto eos;
|
2013-02-09 19:53:51 +00:00
|
|
|
g_cond_wait (&adder->not_empty_cond, GST_OBJECT_GET_LOCK (adder));
|
2008-03-29 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer_timestamp = GST_BUFFER_TIMESTAMP (g_queue_peek_head (adder->buffers));
|
|
|
|
|
|
|
|
clock = GST_ELEMENT_CLOCK (adder);
|
2008-04-28 15:48:50 +00:00
|
|
|
|
2008-04-28 15:49:56 +00:00
|
|
|
/* If we have no clock, then we can't do anything.. error */
|
2008-04-28 15:48:50 +00:00
|
|
|
if (!clock) {
|
2008-05-14 23:49:10 +00:00
|
|
|
if (adder->playing)
|
|
|
|
goto no_clock;
|
|
|
|
else
|
|
|
|
goto push_buffer;
|
2008-04-28 15:48:50 +00:00
|
|
|
}
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (adder, "sync to timestamp %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (buffer_timestamp));
|
|
|
|
|
|
|
|
sync_time = buffer_timestamp + GST_ELEMENT_CAST (adder)->base_time;
|
|
|
|
/* add latency, this includes our own latency and the peer latency. */
|
|
|
|
sync_time += adder->latency_ms * GST_MSECOND;
|
|
|
|
sync_time += adder->peer_latency;
|
|
|
|
|
|
|
|
/* create an entry for the clock */
|
|
|
|
id = adder->clock_id = gst_clock_new_single_shot_id (clock, sync_time);
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
|
|
|
|
ret = gst_clock_id_wait (id, NULL);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
|
|
|
|
/* and free the entry */
|
|
|
|
gst_clock_id_unref (id);
|
|
|
|
adder->clock_id = NULL;
|
|
|
|
|
|
|
|
/* at this point, the clock could have been unlocked by a timeout, a new
|
|
|
|
* head element was added to the queue or because we are shutting down. Check
|
|
|
|
* for shutdown first. */
|
|
|
|
|
|
|
|
if (adder->srcresult != GST_FLOW_OK)
|
|
|
|
goto flushing;
|
|
|
|
|
|
|
|
if (ret == GST_CLOCK_UNSCHEDULED) {
|
|
|
|
GST_DEBUG_OBJECT (adder,
|
|
|
|
"Wait got unscheduled, will retry to push with new buffer");
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != GST_CLOCK_OK && ret != GST_CLOCK_EARLY)
|
|
|
|
goto clock_error;
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
push_buffer:
|
2008-05-14 23:49:10 +00:00
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
buffer = g_queue_pop_head (adder->buffers);
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
goto again;
|
|
|
|
|
2008-04-28 18:34:09 +00:00
|
|
|
/*
|
|
|
|
* We make sure the timestamps are exactly contiguous
|
|
|
|
* If its only small skew (due to rounding errors), we correct it
|
|
|
|
* silently. Otherwise we put the discont flag
|
|
|
|
*/
|
2008-03-29 00:45:18 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (adder->next_timestamp) &&
|
2008-04-03 00:40:12 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (buffer) != adder->next_timestamp) {
|
2008-05-21 17:20:05 +00:00
|
|
|
GstClockTimeDiff diff = GST_CLOCK_DIFF (GST_BUFFER_TIMESTAMP (buffer),
|
|
|
|
adder->next_timestamp);
|
|
|
|
if (diff < 0)
|
|
|
|
diff = -diff;
|
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
if (diff < GST_SECOND / GST_AUDIO_INFO_RATE (&adder->info)) {
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (buffer) = adder->next_timestamp;
|
|
|
|
GST_DEBUG_OBJECT (adder, "Correcting slight skew");
|
2009-02-10 16:18:32 +00:00
|
|
|
GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DISCONT);
|
2008-03-29 00:45:18 +00:00
|
|
|
} else {
|
2009-02-10 16:18:32 +00:00
|
|
|
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_DEBUG_OBJECT (adder, "Expected buffer at %" GST_TIME_FORMAT
|
2009-02-10 16:18:32 +00:00
|
|
|
", but is at %" GST_TIME_FORMAT ", setting discont",
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_TIME_ARGS (adder->next_timestamp),
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
|
|
|
|
}
|
|
|
|
} else {
|
2009-02-10 16:18:32 +00:00
|
|
|
GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DISCONT);
|
2008-03-29 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
|
|
|
|
GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
|
2008-04-02 22:46:27 +00:00
|
|
|
|
2008-04-03 00:40:12 +00:00
|
|
|
if (GST_BUFFER_DURATION_IS_VALID (buffer))
|
|
|
|
adder->next_timestamp = GST_BUFFER_TIMESTAMP (buffer) +
|
|
|
|
GST_BUFFER_DURATION (buffer);
|
|
|
|
else
|
|
|
|
adder->next_timestamp = GST_CLOCK_TIME_NONE;
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
|
2008-12-13 05:09:46 +00:00
|
|
|
GST_LOG_OBJECT (adder, "About to push buffer time:%" GST_TIME_FORMAT
|
2009-02-10 16:18:32 +00:00
|
|
|
" duration:%" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
|
2008-08-27 15:16:27 +00:00
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
result = gst_pad_push (adder->srcpad, buffer);
|
|
|
|
if (result != GST_FLOW_OK)
|
|
|
|
goto pause;
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
flushing:
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (adder, "we are flushing");
|
|
|
|
gst_pad_pause_task (adder->srcpad);
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
clock_error:
|
|
|
|
{
|
|
|
|
gst_pad_pause_task (adder->srcpad);
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
GST_ELEMENT_ERROR (adder, STREAM, MUX, ("Error with the clock"),
|
|
|
|
("Error with the clock: %d", ret));
|
|
|
|
GST_ERROR_OBJECT (adder, "Error with the clock: %d", ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
no_clock:
|
|
|
|
{
|
|
|
|
gst_pad_pause_task (adder->srcpad);
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
GST_ELEMENT_ERROR (adder, STREAM, MUX, ("No available clock"),
|
|
|
|
("No available clock"));
|
|
|
|
GST_ERROR_OBJECT (adder, "No available clock");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pause:
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
2009-06-19 15:21:28 +00:00
|
|
|
GST_DEBUG_OBJECT (adder, "pausing task, reason %s",
|
|
|
|
gst_flow_get_name (result));
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
|
|
|
|
/* store result */
|
|
|
|
adder->srcresult = result;
|
|
|
|
/* we don't post errors or anything because upstream will do that for us
|
|
|
|
* when we pass the return value upstream. */
|
|
|
|
gst_pad_pause_task (adder->srcpad);
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-02-10 16:18:32 +00:00
|
|
|
eos:
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
|
|
|
/* store result, we are flushing now */
|
|
|
|
GST_DEBUG_OBJECT (adder, "We are EOS, pushing EOS downstream");
|
2012-09-12 20:03:06 +00:00
|
|
|
adder->srcresult = GST_FLOW_EOS;
|
2008-03-29 00:45:18 +00:00
|
|
|
gst_pad_pause_task (adder->srcpad);
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
2008-05-01 20:09:40 +00:00
|
|
|
gst_pad_push_event (adder->srcpad, gst_event_new_eos ());
|
2008-03-29 00:45:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstPad *
|
|
|
|
gst_live_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
|
2012-09-12 20:03:06 +00:00
|
|
|
const gchar * ignored_name, const GstCaps * caps)
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
|
|
|
gchar *name;
|
|
|
|
GstLiveAdder *adder;
|
|
|
|
GstPad *newpad;
|
|
|
|
gint padcount;
|
|
|
|
GstLiveAdderPadPrivate *padprivate = NULL;
|
|
|
|
|
|
|
|
if (templ->direction != GST_PAD_SINK)
|
|
|
|
goto not_sink;
|
|
|
|
|
|
|
|
adder = GST_LIVE_ADDER (element);
|
|
|
|
|
|
|
|
/* increment pad counter */
|
2011-06-04 21:04:44 +00:00
|
|
|
#if GLIB_CHECK_VERSION(2,29,5)
|
|
|
|
padcount = g_atomic_int_add (&adder->padcount, 1);
|
|
|
|
#else
|
2008-03-29 00:45:18 +00:00
|
|
|
padcount = g_atomic_int_exchange_and_add (&adder->padcount, 1);
|
2011-06-04 21:04:44 +00:00
|
|
|
#endif
|
2008-03-29 00:45:18 +00:00
|
|
|
|
2011-11-04 11:22:37 +00:00
|
|
|
name = g_strdup_printf ("sink_%u", padcount);
|
2008-03-29 00:45:18 +00:00
|
|
|
newpad = gst_pad_new_from_template (templ, name);
|
|
|
|
GST_DEBUG_OBJECT (adder, "request new pad %s", name);
|
|
|
|
g_free (name);
|
|
|
|
|
|
|
|
gst_pad_set_event_function (newpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_live_adder_sink_event));
|
2012-09-12 20:03:06 +00:00
|
|
|
gst_pad_set_query_function (newpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_live_adder_sink_query));
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
padprivate = g_new0 (GstLiveAdderPadPrivate, 1);
|
|
|
|
|
|
|
|
gst_segment_init (&padprivate->segment, GST_FORMAT_UNDEFINED);
|
|
|
|
padprivate->eos = FALSE;
|
2008-04-03 00:57:28 +00:00
|
|
|
padprivate->expected_timestamp = GST_CLOCK_TIME_NONE;
|
2008-03-29 00:45:18 +00:00
|
|
|
|
|
|
|
gst_pad_set_element_private (newpad, padprivate);
|
|
|
|
|
|
|
|
gst_pad_set_chain_function (newpad, gst_live_live_adder_chain);
|
|
|
|
|
2008-04-03 02:03:18 +00:00
|
|
|
|
|
|
|
if (!gst_pad_set_active (newpad, TRUE))
|
|
|
|
goto could_not_activate;
|
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
/* takes ownership of the pad */
|
|
|
|
if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
|
|
|
|
goto could_not_add;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
adder->sinkpads = g_list_prepend (adder->sinkpads, newpad);
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
|
|
|
|
return newpad;
|
|
|
|
|
|
|
|
/* errors */
|
|
|
|
not_sink:
|
|
|
|
{
|
|
|
|
g_warning ("gstadder: request new pad that is not a SINK pad\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
could_not_add:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (adder, "could not add pad");
|
2008-04-03 02:03:18 +00:00
|
|
|
g_free (padprivate);
|
|
|
|
gst_object_unref (newpad);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
could_not_activate:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (adder, "could not activate new pad");
|
|
|
|
g_free (padprivate);
|
2008-03-29 00:45:18 +00:00
|
|
|
gst_object_unref (newpad);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_live_adder_release_pad (GstElement * element, GstPad * pad)
|
|
|
|
{
|
|
|
|
GstLiveAdder *adder;
|
|
|
|
GstLiveAdderPadPrivate *padprivate;
|
|
|
|
|
|
|
|
adder = GST_LIVE_ADDER (element);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (adder, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (element);
|
|
|
|
padprivate = gst_pad_get_element_private (pad);
|
|
|
|
gst_pad_set_element_private (pad, NULL);
|
2009-02-10 16:18:32 +00:00
|
|
|
adder->sinkpads = g_list_remove_all (adder->sinkpads, pad);
|
2008-03-29 00:45:18 +00:00
|
|
|
GST_OBJECT_UNLOCK (element);
|
|
|
|
|
|
|
|
g_free (padprivate);
|
|
|
|
|
|
|
|
gst_element_remove_pad (element, pad);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-10 16:18:32 +00:00
|
|
|
reset_pad_private (GstPad * pad)
|
2008-03-29 00:45:18 +00:00
|
|
|
{
|
|
|
|
GstLiveAdderPadPrivate *padprivate;
|
|
|
|
|
|
|
|
padprivate = gst_pad_get_element_private (pad);
|
|
|
|
|
|
|
|
if (!padprivate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gst_segment_init (&padprivate->segment, GST_FORMAT_UNDEFINED);
|
|
|
|
|
2008-04-03 00:57:28 +00:00
|
|
|
padprivate->expected_timestamp = GST_CLOCK_TIME_NONE;
|
2008-03-29 00:45:18 +00:00
|
|
|
padprivate->eos = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_live_adder_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstLiveAdder *adder;
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
|
|
|
|
adder = GST_LIVE_ADDER (element);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
2008-05-14 23:49:48 +00:00
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
adder->segment_pending = TRUE;
|
|
|
|
adder->peer_latency = 0;
|
|
|
|
adder->next_timestamp = GST_CLOCK_TIME_NONE;
|
|
|
|
g_list_foreach (adder->sinkpads, (GFunc) reset_pad_private, NULL);
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
adder->playing = FALSE;
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
break;
|
2008-03-29 00:45:18 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-12 20:03:06 +00:00
|
|
|
ret = GST_ELEMENT_CLASS (gst_live_adder_parent_class)->change_state (element,
|
|
|
|
transition);
|
2008-03-29 00:45:18 +00:00
|
|
|
|
2008-05-14 23:49:48 +00:00
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
|
|
|
|
GST_OBJECT_LOCK (adder);
|
|
|
|
adder->playing = TRUE;
|
|
|
|
GST_OBJECT_UNLOCK (adder);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-03-29 00:45:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
plugin_init (GstPlugin * plugin)
|
|
|
|
{
|
|
|
|
if (!gst_element_register (plugin, "liveadder", GST_RANK_NONE,
|
|
|
|
GST_TYPE_LIVE_ADDER)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
2012-04-05 16:02:56 +00:00
|
|
|
liveadder,
|
2008-03-29 00:45:18 +00:00
|
|
|
"Adds multiple live discontinuous streams",
|
2009-02-10 16:18:32 +00:00
|
|
|
plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
|