Original commit message from CVS:
fixes #112657
This commit is contained in:
Thomas Vander Stichele 2003-05-10 16:57:38 +00:00
parent a3a051705a
commit bc5cced5e1
2 changed files with 106 additions and 72 deletions

View file

@ -47,7 +47,8 @@ enum {
ARG_THRESHOLD, ARG_THRESHOLD,
ARG_THRESHOLD_DB, ARG_THRESHOLD_DB,
ARG_RUN_LENGTH, ARG_RUN_LENGTH,
ARG_PRE_LENGTH ARG_PRE_LENGTH,
ARG_LEAKY
}; };
GST_PAD_TEMPLATE_FACTORY (cutter_src_factory, GST_PAD_TEMPLATE_FACTORY (cutter_src_factory,
@ -76,13 +77,16 @@ static void gst_cutter_class_init (GstCutterClass *klass);
static void gst_cutter_init (GstCutter *filter); static void gst_cutter_init (GstCutter *filter);
static void gst_cutter_set_property (GObject *object, guint prop_id, static void gst_cutter_set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec); const GValue *value,
GParamSpec *pspec);
static void gst_cutter_get_property (GObject *object, guint prop_id, static void gst_cutter_get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec); GValue *value, GParamSpec *pspec);
static void gst_cutter_chain (GstPad *pad, GstBuffer *buf); static void gst_cutter_chain (GstPad *pad, GstBuffer *buf);
static double inline gst_cutter_16bit_ms (gint16* data, guint numsamples); static double
static double inline gst_cutter_8bit_ms (gint8* data, guint numsamples); inline gst_cutter_16bit_ms (gint16* data, guint numsamples);
static double
inline gst_cutter_8bit_ms (gint8* data, guint numsamples);
void gst_cutter_get_caps (GstPad *pad, GstCutter* filter); void gst_cutter_get_caps (GstPad *pad, GstCutter* filter);
@ -91,25 +95,24 @@ static guint gst_cutter_signals[LAST_SIGNAL] = { 0 };
GType GType
gst_cutter_get_type(void) { gst_cutter_get_type (void) {
static GType cutter_type = 0; static GType cutter_type = 0;
if (!cutter_type) { if (!cutter_type) {
static const GTypeInfo cutter_info = { static const GTypeInfo cutter_info = {
sizeof(GstCutterClass), NULL, NULL, (GClassInitFunc)gst_cutter_class_init, sizeof (GstCutterClass), NULL, NULL,
NULL, (GClassInitFunc) gst_cutter_class_init, NULL, NULL,
NULL, sizeof (GstCutter), 0,
sizeof(GstCutter), (GInstanceInitFunc) gst_cutter_init,
0,
(GInstanceInitFunc)gst_cutter_init,
}; };
cutter_type = g_type_register_static(GST_TYPE_ELEMENT, "GstCutter", &cutter_info, 0); cutter_type = g_type_register_static (GST_TYPE_ELEMENT, "GstCutter",
&cutter_info, 0);
} }
return cutter_type; return cutter_type;
} }
static GstPadLinkReturn static GstPadLinkReturn
gst_cutter_connect (GstPad *pad, GstCaps *caps) gst_cutter_link (GstPad *pad, GstCaps *caps)
{ {
GstCutter *filter; GstCutter *filter;
GstPad *otherpad; GstPad *otherpad;
@ -136,24 +139,33 @@ gst_cutter_class_init (GstCutterClass *klass)
parent_class = g_type_class_ref (GST_TYPE_ELEMENT); parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_THRESHOLD, g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_THRESHOLD,
g_param_spec_double ("threshold", "threshold", "threshold", g_param_spec_double ("threshold", "Threshold",
-G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE)); /* CHECKME */ "Volume threshold before trigger",
-G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_THRESHOLD_DB, g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_THRESHOLD_DB,
g_param_spec_double ("threshold_dB", "threshold_dB", "threshold_dB", g_param_spec_double ("threshold_dB", "Threshold (dB)",
-G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE)); /* CHECKME */ "Volume threshold before trigger (in dB)",
-G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RUN_LENGTH, g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RUN_LENGTH,
g_param_spec_double ("runlength", "runlength", "runlength", g_param_spec_double ("runlength", "Runlength",
-G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE)); /* CHECKME */ "Length of drop below threshold before cut_stop (seconds)",
0.0, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PRE_LENGTH, g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PRE_LENGTH,
g_param_spec_double ("prelength", "prelength", "prelength", g_param_spec_double ("prelength", "prelength",
-G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE)); /* CHECKME */ "Length of pre-recording buffer (seconds)",
0.0, G_MAXDOUBLE, 0.0, G_PARAM_READWRITE));
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LEAKY,
g_param_spec_boolean ("leaky", "Leaky",
"do we leak buffers when below threshold ?",
FALSE, G_PARAM_READWRITE));
gst_cutter_signals[CUT_START] = gst_cutter_signals[CUT_START] =
g_signal_new ("cut_start", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, g_signal_new ("cut_start", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GstCutterClass, cut_start), NULL, NULL, G_STRUCT_OFFSET (GstCutterClass, cut_start), NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
gst_cutter_signals[CUT_STOP] = gst_cutter_signals[CUT_STOP] =
g_signal_new ("cut_stop", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, g_signal_new ("cut_stop", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GstCutterClass, cut_stop), NULL, NULL, G_STRUCT_OFFSET (GstCutterClass, cut_stop), NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
@ -176,13 +188,14 @@ gst_cutter_init (GstCutter *filter)
filter->pre_length = 0.2; filter->pre_length = 0.2;
filter->pre_run_length = 0.0; filter->pre_run_length = 0.0;
filter->pre_buffer = NULL; filter->pre_buffer = NULL;
filter->leaky = FALSE;
gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad); gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
gst_pad_set_chain_function (filter->sinkpad, gst_cutter_chain); gst_pad_set_chain_function (filter->sinkpad, gst_cutter_chain);
gst_pad_set_link_function (filter->sinkpad, gst_cutter_connect); gst_pad_set_link_function (filter->sinkpad, gst_cutter_link);
filter->srcpad = gst_pad_new ("src", GST_PAD_SRC); filter->srcpad = gst_pad_new ("src", GST_PAD_SRC);
gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad); gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
gst_pad_set_link_function (filter->srcpad, gst_cutter_connect); gst_pad_set_link_function (filter->srcpad, gst_cutter_link);
} }
static void static void
@ -204,13 +217,13 @@ gst_cutter_chain (GstPad *pad, GstBuffer *buf)
g_return_if_fail (GST_IS_CUTTER (filter)); g_return_if_fail (GST_IS_CUTTER (filter));
if (gst_audio_is_buffer_framed (pad, buf) == FALSE) if (gst_audio_is_buffer_framed (pad, buf) == FALSE)
g_warning ("cutter: audio buffer is not framed !\n"); g_warning ("audio buffer is not framed !\n");
if (!filter->have_caps) gst_cutter_get_caps (pad, filter); if (!filter->have_caps) gst_cutter_get_caps (pad, filter);
in_data = (gint16 *) GST_BUFFER_DATA (buf); in_data = (gint16 *) GST_BUFFER_DATA (buf);
GST_DEBUG (GST_CAT_PLUGIN_INFO, GST_DEBUG (GST_CAT_PLUGIN_INFO,
"cutter: length of prerec buffer: %.3f sec\n", "length of prerec buffer: %.3f sec",
filter->pre_run_length); filter->pre_run_length);
/* calculate mean square value on buffer */ /* calculate mean square value on buffer */
@ -236,7 +249,7 @@ gst_cutter_chain (GstPad *pad, GstBuffer *buf)
* if not, reset * if not, reset
*/ */
GST_DEBUG (GST_CAT_PLUGIN_INFO, GST_DEBUG (GST_CAT_PLUGIN_INFO,
"DEBUG: cutter: ms %f, RMS %f, audio length %f\n", "buffer stats: ms %f, RMS %f, audio length %f",
ms, RMS, gst_audio_length (filter->srcpad, buf)); ms, RMS, gst_audio_length (filter->srcpad, buf));
if (RMS < filter->threshold_level) if (RMS < filter->threshold_level)
filter->silent_run_length += gst_audio_length (filter->srcpad, buf); filter->silent_run_length += gst_audio_length (filter->srcpad, buf);
@ -262,24 +275,35 @@ gst_cutter_chain (GstPad *pad, GstBuffer *buf)
} }
else else
{ {
gint count = 0;
/* g_print ("DEBUG: cutter: start from here, turning on out\n"); */ /* g_print ("DEBUG: cutter: start from here, turning on out\n"); */
/* first of all, flush current buffer */ /* first of all, flush current buffer */
g_signal_emit (G_OBJECT (filter), gst_cutter_signals[CUT_START], 0); g_signal_emit (G_OBJECT (filter), gst_cutter_signals[CUT_START], 0);
GST_DEBUG (GST_CAT_PLUGIN_INFO, "DEBUG: cutter: flushing buffer"); GST_DEBUG (GST_CAT_PLUGIN_INFO,
"flushing buffer of length %.3f",
filter->pre_run_length);
while (filter->pre_buffer) while (filter->pre_buffer)
{ {
prebuf = (g_list_first (filter->pre_buffer))->data; prebuf = (g_list_first (filter->pre_buffer))->data;
filter->pre_buffer = g_list_remove (filter->pre_buffer, prebuf); filter->pre_buffer = g_list_remove (filter->pre_buffer, prebuf);
gst_pad_push (filter->srcpad, prebuf); gst_pad_push (filter->srcpad, prebuf);
++count;
}
GST_DEBUG (GST_CAT_PLUGIN_INFO, "flushed %d buffers", count);
filter->pre_run_length = 0.0; filter->pre_run_length = 0.0;
} }
} }
} /* now check if we have to send the new buffer to the internal buffer cache
/* now check if we have to add the new buffer to the cache or to the pad */ * or to the srcpad */
if (filter->silent) if (filter->silent)
{ {
/* we ref it before putting it in the pre_buffer */ /* we ref it before putting it in the pre_buffer */
/* FIXME: we shouldn't probably do this, because the buffer
* arrives reffed already; the plugin should just push it
* or unref it to make it disappear */
/*
gst_buffer_ref (buf); gst_buffer_ref (buf);
*/
filter->pre_buffer = g_list_append (filter->pre_buffer, buf); filter->pre_buffer = g_list_append (filter->pre_buffer, buf);
filter->pre_run_length += gst_audio_length (filter->srcpad, buf); filter->pre_run_length += gst_audio_length (filter->srcpad, buf);
while (filter->pre_run_length > filter->pre_length) while (filter->pre_run_length > filter->pre_length)
@ -288,6 +312,8 @@ gst_cutter_chain (GstPad *pad, GstBuffer *buf)
g_assert (GST_IS_BUFFER (prebuf)); g_assert (GST_IS_BUFFER (prebuf));
filter->pre_buffer = g_list_remove (filter->pre_buffer, prebuf); filter->pre_buffer = g_list_remove (filter->pre_buffer, prebuf);
filter->pre_run_length -= gst_audio_length (filter->srcpad, prebuf); filter->pre_run_length -= gst_audio_length (filter->srcpad, prebuf);
/* only pass buffers if we don't leak */
if (!filter->leaky)
gst_pad_push (filter->srcpad, prebuf); gst_pad_push (filter->srcpad, prebuf);
/* we unref it after getting it out of the pre_buffer */ /* we unref it after getting it out of the pre_buffer */
gst_buffer_unref (prebuf); gst_buffer_unref (prebuf);
@ -320,7 +346,7 @@ gst_cutter_set_property (GObject *object, guint prop_id,
/* set the level */ /* set the level */
filter->threshold_level = g_value_get_double (value); filter->threshold_level = g_value_get_double (value);
GST_DEBUG (GST_CAT_PLUGIN_INFO, GST_DEBUG (GST_CAT_PLUGIN_INFO,
"DEBUG: cutter: set threshold level to %f\n", "DEBUG: set threshold level to %f",
filter->threshold_level); filter->threshold_level);
break; break;
case ARG_THRESHOLD_DB: case ARG_THRESHOLD_DB:
@ -330,7 +356,7 @@ gst_cutter_set_property (GObject *object, guint prop_id,
*/ */
filter->threshold_level = pow (10, g_value_get_double (value) / 20); filter->threshold_level = pow (10, g_value_get_double (value) / 20);
GST_DEBUG (GST_CAT_PLUGIN_INFO, GST_DEBUG (GST_CAT_PLUGIN_INFO,
"DEBUG: cutter: set threshold level to %f\n", "DEBUG: set threshold level to %f",
filter->threshold_level); filter->threshold_level);
break; break;
case ARG_RUN_LENGTH: case ARG_RUN_LENGTH:
@ -341,6 +367,10 @@ gst_cutter_set_property (GObject *object, guint prop_id,
/* set the length of the pre-record block */ /* set the length of the pre-record block */
filter->pre_length = g_value_get_double (value); filter->pre_length = g_value_get_double (value);
break; break;
case ARG_LEAKY:
/* set if the pre-record buffer is leaky or not */
filter->leaky = g_value_get_boolean (value);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -370,6 +400,9 @@ gst_cutter_get_property (GObject *object, guint prop_id,
case ARG_PRE_LENGTH: case ARG_PRE_LENGTH:
g_value_set_double (value, filter->pre_length); g_value_set_double (value, filter->pre_length);
break; break;
case ARG_LEAKY:
g_value_set_boolean (value, filter->leaky);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -414,7 +447,7 @@ gst_cutter_get_caps (GstPad *pad, GstCutter* filter)
/* FIXME : Please change this to a better warning method ! */ /* FIXME : Please change this to a better warning method ! */
g_assert (caps != NULL); g_assert (caps != NULL);
if (caps == NULL) if (caps == NULL)
printf ("WARNING: cutter: get_caps: Could not get caps of pad !\n"); printf ("WARNING: get_caps: Could not get caps of pad !\n");
gst_caps_get_int (caps, "width", &filter->width); gst_caps_get_int (caps, "width", &filter->width);
filter->max_sample = gst_audio_highest_sample_value (pad); filter->max_sample = gst_audio_highest_sample_value (pad);
filter->have_caps = TRUE; filter->have_caps = TRUE;

View file

@ -61,7 +61,8 @@ struct _GstCutter
double pre_length; /* how long can the pre-record buffer be ? */ double pre_length; /* how long can the pre-record buffer be ? */
double pre_run_length; /* how long is it currently ? */ double pre_run_length; /* how long is it currently ? */
GList *pre_buffer; GList *pre_buffer; /* list of GstBuffers in pre-record buffer */
gboolean leaky; /* do we leak an overflowing prebuffer ? */
gboolean have_caps; /* did we get the needed caps yet ? */ gboolean have_caps; /* did we get the needed caps yet ? */
gint width; /* bit width of data */ gint width; /* bit width of data */