multifilesink: post messages for each buffer

Add a silent property that can be set to FALSE to post messages on the bus for
each written file.
Do some more cleanups.
Add some docs.

Fixes #594663
This commit is contained in:
Wim Taymans 2009-09-11 12:16:18 +02:00 committed by Wim Taymans
parent 411c71da13
commit f68cd7e708
2 changed files with 186 additions and 63 deletions

View file

@ -26,12 +26,90 @@
* @see_also: #GstFileSrc * @see_also: #GstFileSrc
* *
* Write incoming data to a series of sequentially-named files. * Write incoming data to a series of sequentially-named files.
*
* The filename property should contain a string with a %d placeholder that will
* be substituted with the index for each filename.
*
* If the #GstMultiFileSink:silent property is #FALSE, it sends an application
* message named
* <classname>&quot;GstMultiFileSink&quot;</classname> after writing each
* buffer.
*
* The message's structure contains these fields:
* <itemizedlist>
* <listitem>
* <para>
* #gchar *
* <classname>&quot;filename&quot;</classname>:
* the filename where the buffer was written.
* </para>
* </listitem>
* <listitem>
* <para>
* #gint
* <classname>&quot;index&quot;</classname>:
* the index of the buffer.
* </para>
* </listitem>
* <listitem>
* <para>
* #GstClockTime
* <classname>&quot;timestamp&quot;</classname>:
* the timestamp of the buffer.
* </para>
* </listitem>
* <listitem>
* <para>
* #GstClockTime
* <classname>&quot;stream-time&quot;</classname>:
* the stream time of the buffer.
* </para>
* </listitem>
* <listitem>
* <para>
* #GstClockTime
* <classname>&quot;running-time&quot;</classname>:
* the running_time of the buffer.
* </para>
* </listitem>
* <listitem>
* <para>
* #GstClockTime
* <classname>&quot;duration&quot;</classname>:
* the duration of the buffer.
* </para>
* </listitem>
* <listitem>
* <para>
* #guint64
* <classname>&quot;offset&quot;</classname>:
* the offset of the buffer that triggered the message.
* </para>
* </listitem>
* <listitem>
* <para>
* #guint64
* <classname>&quot;offset-end&quot;</classname>:
* the offset-end of the buffer that triggered the message.
* </para>
* </listitem>
* </itemizedlist>
*
* <title>Example launch line</title>
* <refsect2>
* |[
* gst-launch audiotestsrc ! multifilesink
* gst-launch videotestsrc ! multifilesink silent=false filename="frame%d"
* ]|
* </refsect2>
*
* Last reviewed on 2009-09-11 (0.10.17)
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include "config.h" # include "config.h"
#endif #endif
#include <gst/base/gstbasetransform.h>
#include "gstmultifilesink.h" #include "gstmultifilesink.h"
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
@ -48,17 +126,20 @@ GST_ELEMENT_DETAILS ("Multi-File Sink",
"Write buffers to a sequentially named set of files", "Write buffers to a sequentially named set of files",
"David Schleef <ds@schleef.org>"); "David Schleef <ds@schleef.org>");
enum
{
ARG_0,
ARG_LOCATION,
ARG_INDEX
};
#define DEFAULT_LOCATION "%05d" #define DEFAULT_LOCATION "%05d"
#define DEFAULT_INDEX 0 #define DEFAULT_INDEX 0
#define DEFAULT_SILENT TRUE
static void gst_multi_file_sink_dispose (GObject * object); enum
{
PROP_0,
PROP_LOCATION,
PROP_INDEX,
PROP_SILENT,
PROP_LAST
};
static void gst_multi_file_sink_finalize (GObject * object);
static void gst_multi_file_sink_set_property (GObject * object, guint prop_id, static void gst_multi_file_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec); const GValue * value, GParamSpec * pspec);
@ -68,8 +149,6 @@ static void gst_multi_file_sink_get_property (GObject * object, guint prop_id,
static GstFlowReturn gst_multi_file_sink_render (GstBaseSink * sink, static GstFlowReturn gst_multi_file_sink_render (GstBaseSink * sink,
GstBuffer * buffer); GstBuffer * buffer);
GST_BOILERPLATE (GstMultiFileSink, gst_multi_file_sink, GstBaseSink, GST_BOILERPLATE (GstMultiFileSink, gst_multi_file_sink, GstBaseSink,
GST_TYPE_BASE_SINK); GST_TYPE_BASE_SINK);
@ -96,16 +175,28 @@ gst_multi_file_sink_class_init (GstMultiFileSinkClass * klass)
gobject_class->set_property = gst_multi_file_sink_set_property; gobject_class->set_property = gst_multi_file_sink_set_property;
gobject_class->get_property = gst_multi_file_sink_get_property; gobject_class->get_property = gst_multi_file_sink_get_property;
g_object_class_install_property (gobject_class, ARG_LOCATION, g_object_class_install_property (gobject_class, PROP_LOCATION,
g_param_spec_string ("location", "File Location", g_param_spec_string ("location", "File Location",
"Location of the file to write", NULL, G_PARAM_READWRITE)); "Location of the file to write", NULL, G_PARAM_READWRITE));
g_object_class_install_property (gobject_class, ARG_INDEX,
g_object_class_install_property (gobject_class, PROP_INDEX,
g_param_spec_int ("index", "Index", g_param_spec_int ("index", "Index",
"Index to use with location property to create file names. The " "Index to use with location property to create file names. The "
"index is incremented by one for each buffer read.", "index is incremented by one for each buffer written.",
0, INT_MAX, DEFAULT_INDEX, G_PARAM_READWRITE)); 0, G_MAXINT, DEFAULT_INDEX, G_PARAM_READWRITE));
/**
* GstMultiFileSink:silent
*
* Post a message on the GstBus for each file.
*
* Since: 0.10.17
*/
g_object_class_install_property (gobject_class, PROP_SILENT,
g_param_spec_boolean ("silent", "Silent",
"Message to show a message for each file to save with the timestamp "
" of the buffer", DEFAULT_SILENT, G_PARAM_READWRITE));
gobject_class->dispose = gst_multi_file_sink_dispose; gobject_class->finalize = gst_multi_file_sink_finalize;
gstbasesink_class->get_times = NULL; gstbasesink_class->get_times = NULL;
gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_multi_file_sink_render); gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_multi_file_sink_render);
@ -117,19 +208,19 @@ gst_multi_file_sink_init (GstMultiFileSink * multifilesink,
{ {
multifilesink->filename = g_strdup (DEFAULT_LOCATION); multifilesink->filename = g_strdup (DEFAULT_LOCATION);
multifilesink->index = DEFAULT_INDEX; multifilesink->index = DEFAULT_INDEX;
multifilesink->silent = DEFAULT_SILENT;
GST_BASE_SINK (multifilesink)->sync = FALSE; gst_base_sink_set_sync (GST_BASE_SINK (multifilesink), FALSE);
} }
static void static void
gst_multi_file_sink_dispose (GObject * object) gst_multi_file_sink_finalize (GObject * object)
{ {
GstMultiFileSink *sink = GST_MULTI_FILE_SINK (object); GstMultiFileSink *sink = GST_MULTI_FILE_SINK (object);
g_free (sink->filename); g_free (sink->filename);
sink->filename = NULL;
G_OBJECT_CLASS (parent_class)->dispose (object); G_OBJECT_CLASS (parent_class)->finalize (object);
} }
static gboolean static gboolean
@ -137,12 +228,8 @@ gst_multi_file_sink_set_location (GstMultiFileSink * sink,
const gchar * location) const gchar * location)
{ {
g_free (sink->filename); g_free (sink->filename);
if (location != NULL) { /* FIXME: validate location to have just one %d */
/* FIXME: validate location to have just one %d */ sink->filename = g_strdup (location);
sink->filename = g_strdup (location);
} else {
sink->filename = NULL;
}
return TRUE; return TRUE;
} }
@ -154,12 +241,15 @@ gst_multi_file_sink_set_property (GObject * object, guint prop_id,
GstMultiFileSink *sink = GST_MULTI_FILE_SINK (object); GstMultiFileSink *sink = GST_MULTI_FILE_SINK (object);
switch (prop_id) { switch (prop_id) {
case ARG_LOCATION: case PROP_LOCATION:
gst_multi_file_sink_set_location (sink, g_value_get_string (value)); gst_multi_file_sink_set_location (sink, g_value_get_string (value));
break; break;
case ARG_INDEX: case PROP_INDEX:
sink->index = g_value_get_int (value); sink->index = g_value_get_int (value);
break; break;
case PROP_SILENT:
sink->silent = 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;
@ -173,65 +263,98 @@ gst_multi_file_sink_get_property (GObject * object, guint prop_id,
GstMultiFileSink *sink = GST_MULTI_FILE_SINK (object); GstMultiFileSink *sink = GST_MULTI_FILE_SINK (object);
switch (prop_id) { switch (prop_id) {
case ARG_LOCATION: case PROP_LOCATION:
g_value_set_string (value, sink->filename); g_value_set_string (value, sink->filename);
break; break;
case ARG_INDEX: case PROP_INDEX:
g_value_set_int (value, sink->index); g_value_set_int (value, sink->index);
break; break;
case PROP_SILENT:
g_value_set_boolean (value, sink->silent);
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;
} }
} }
static gchar *
gst_multi_file_sink_get_filename (GstMultiFileSink * multifilesink)
{
gchar *filename;
filename = g_strdup_printf (multifilesink->filename, multifilesink->index);
return filename;
}
static GstFlowReturn static GstFlowReturn
gst_multi_file_sink_render (GstBaseSink * sink, GstBuffer * buffer) gst_multi_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
{ {
GstMultiFileSink *multifilesink; GstMultiFileSink *multifilesink;
guint size; guint size;
guint8 *data;
gchar *filename; gchar *filename;
gboolean ret; gboolean ret;
GError *error = NULL; GError *error = NULL;
size = GST_BUFFER_SIZE (buffer); size = GST_BUFFER_SIZE (buffer);
data = GST_BUFFER_DATA (buffer);
multifilesink = GST_MULTI_FILE_SINK (sink); multifilesink = GST_MULTI_FILE_SINK (sink);
filename = gst_multi_file_sink_get_filename (multifilesink); filename = g_strdup_printf (multifilesink->filename, multifilesink->index);
ret = g_file_set_contents (filename, (char *) data, size, &error);
ret = g_file_set_contents (filename, (char *) GST_BUFFER_DATA (buffer), if (!ret)
size, &error); goto write_error;
if (ret) {
multifilesink->index++; if (!multifilesink->silent) {
g_free (filename); GstClockTime duration, timestamp;
return GST_FLOW_OK; GstClockTime running_time, stream_time;
guint64 offset, offset_end;
GstStructure *s;
GstSegment *segment;
GstFormat format;
segment = &sink->segment;
format = segment->format;
timestamp = GST_BUFFER_TIMESTAMP (buffer);
duration = GST_BUFFER_DURATION (buffer);
offset = GST_BUFFER_OFFSET (buffer);
offset_end = GST_BUFFER_OFFSET_END (buffer);
running_time = gst_segment_to_running_time (segment, format, timestamp);
stream_time = gst_segment_to_stream_time (segment, format, timestamp);
s = gst_structure_new ("GstMultiFileSink",
"filename", G_TYPE_STRING, filename,
"index", G_TYPE_INT, multifilesink->index,
"timestamp", G_TYPE_UINT64, timestamp,
"stream-time", G_TYPE_UINT64, stream_time,
"running-time", G_TYPE_UINT64, running_time,
"duration", G_TYPE_UINT64, duration,
"offset", G_TYPE_UINT64, offset,
"offset-end", G_TYPE_UINT64, offset_end, NULL);
gst_element_post_message (GST_ELEMENT_CAST (multifilesink),
gst_message_new_element (GST_OBJECT_CAST (multifilesink), s));
} }
switch (error->code) { multifilesink->index++;
case G_FILE_ERROR_NOSPC:{
GST_ELEMENT_ERROR (multifilesink, RESOURCE, NO_SPACE_LEFT, (NULL),
(NULL));
break;
}
default:{
GST_ELEMENT_ERROR (multifilesink, RESOURCE, WRITE,
("Error while writing to file \"%s\".", filename),
("%s", g_strerror (errno)));
}
}
g_error_free (error);
g_free (filename); g_free (filename);
return GST_FLOW_ERROR; return GST_FLOW_OK;
/* ERRORS */
write_error:
{
switch (error->code) {
case G_FILE_ERROR_NOSPC:{
GST_ELEMENT_ERROR (multifilesink, RESOURCE, NO_SPACE_LEFT, (NULL),
(NULL));
break;
}
default:{
GST_ELEMENT_ERROR (multifilesink, RESOURCE, WRITE,
("Error while writing to file \"%s\".", filename),
("%s", g_strerror (errno)));
}
}
g_error_free (error);
g_free (filename);
return GST_FLOW_ERROR;
}
} }

View file

@ -56,8 +56,8 @@ struct _GstMultiFileSink
GstBaseSink parent; GstBaseSink parent;
gchar *filename; gchar *filename;
gchar *uri; gint index;
int index; gboolean silent;
}; };
struct _GstMultiFileSinkClass struct _GstMultiFileSinkClass