2006-11-10 18:51:10 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2000 Wim Taymans <wtay@chello.be>
|
|
|
|
* 2006 Wim Taymans <wim@fluendo.com>
|
|
|
|
* 2006 David A. Schleef <ds@schleef.org>
|
|
|
|
*
|
|
|
|
* gstmultifilesink.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:element-multifilesink
|
|
|
|
* @see_also: #GstFileSrc
|
|
|
|
*
|
2007-10-25 23:42:52 +00:00
|
|
|
* Write incoming data to a series of sequentially-named files.
|
2009-09-11 10:16:18 +00:00
|
|
|
*
|
2010-04-08 15:05:46 +00:00
|
|
|
* The filename property should contain a string with a \%d placeholder that will
|
2009-09-11 10:16:18 +00:00
|
|
|
* be substituted with the index for each filename.
|
|
|
|
*
|
2009-09-11 11:12:54 +00:00
|
|
|
* If the #GstMultiFileSink:post-messages property is #TRUE, it sends an application
|
2009-09-11 10:16:18 +00:00
|
|
|
* message named
|
|
|
|
* <classname>"GstMultiFileSink"</classname> after writing each
|
|
|
|
* buffer.
|
|
|
|
*
|
|
|
|
* The message's structure contains these fields:
|
|
|
|
* <itemizedlist>
|
|
|
|
* <listitem>
|
|
|
|
* <para>
|
|
|
|
* #gchar *
|
|
|
|
* <classname>"filename"</classname>:
|
|
|
|
* the filename where the buffer was written.
|
|
|
|
* </para>
|
|
|
|
* </listitem>
|
|
|
|
* <listitem>
|
|
|
|
* <para>
|
|
|
|
* #gint
|
|
|
|
* <classname>"index"</classname>:
|
|
|
|
* the index of the buffer.
|
|
|
|
* </para>
|
|
|
|
* </listitem>
|
|
|
|
* <listitem>
|
|
|
|
* <para>
|
|
|
|
* #GstClockTime
|
|
|
|
* <classname>"timestamp"</classname>:
|
|
|
|
* the timestamp of the buffer.
|
|
|
|
* </para>
|
|
|
|
* </listitem>
|
|
|
|
* <listitem>
|
|
|
|
* <para>
|
|
|
|
* #GstClockTime
|
|
|
|
* <classname>"stream-time"</classname>:
|
|
|
|
* the stream time of the buffer.
|
|
|
|
* </para>
|
|
|
|
* </listitem>
|
|
|
|
* <listitem>
|
|
|
|
* <para>
|
|
|
|
* #GstClockTime
|
|
|
|
* <classname>"running-time"</classname>:
|
|
|
|
* the running_time of the buffer.
|
|
|
|
* </para>
|
|
|
|
* </listitem>
|
|
|
|
* <listitem>
|
|
|
|
* <para>
|
|
|
|
* #GstClockTime
|
|
|
|
* <classname>"duration"</classname>:
|
|
|
|
* the duration of the buffer.
|
|
|
|
* </para>
|
|
|
|
* </listitem>
|
|
|
|
* <listitem>
|
|
|
|
* <para>
|
|
|
|
* #guint64
|
|
|
|
* <classname>"offset"</classname>:
|
|
|
|
* the offset of the buffer that triggered the message.
|
|
|
|
* </para>
|
|
|
|
* </listitem>
|
|
|
|
* <listitem>
|
|
|
|
* <para>
|
|
|
|
* #guint64
|
|
|
|
* <classname>"offset-end"</classname>:
|
|
|
|
* the offset-end of the buffer that triggered the message.
|
|
|
|
* </para>
|
|
|
|
* </listitem>
|
|
|
|
* </itemizedlist>
|
|
|
|
*
|
|
|
|
* <refsect2>
|
2010-04-08 07:15:08 +00:00
|
|
|
* <title>Example launch line</title>
|
2009-09-11 10:16:18 +00:00
|
|
|
* |[
|
|
|
|
* gst-launch audiotestsrc ! multifilesink
|
2009-09-11 11:12:54 +00:00
|
|
|
* gst-launch videotestsrc ! multifilesink post-messages=true filename="frame%d"
|
2009-09-11 10:16:18 +00:00
|
|
|
* ]|
|
|
|
|
* </refsect2>
|
|
|
|
*
|
|
|
|
* Last reviewed on 2009-09-11 (0.10.17)
|
2006-11-10 18:51:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2009-09-11 10:16:18 +00:00
|
|
|
#include <gst/base/gstbasetransform.h>
|
2009-09-13 19:30:34 +00:00
|
|
|
#include <glib/gstdio.h>
|
2007-07-03 08:01:18 +00:00
|
|
|
#include "gstmultifilesink.h"
|
2006-11-10 18:51:10 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_multi_file_sink_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_multi_file_sink_debug
|
|
|
|
|
2009-09-11 10:16:18 +00:00
|
|
|
#define DEFAULT_LOCATION "%05d"
|
|
|
|
#define DEFAULT_INDEX 0
|
2009-09-11 11:12:54 +00:00
|
|
|
#define DEFAULT_POST_MESSAGES FALSE
|
2009-09-13 19:30:34 +00:00
|
|
|
#define DEFAULT_NEXT_FILE GST_MULTI_FILE_SINK_NEXT_BUFFER
|
2011-07-18 07:38:26 +00:00
|
|
|
#define DEFAULT_MAX_FILES 0
|
2009-09-11 10:16:18 +00:00
|
|
|
|
2006-11-10 18:51:10 +00:00
|
|
|
enum
|
|
|
|
{
|
2009-09-11 10:16:18 +00:00
|
|
|
PROP_0,
|
|
|
|
PROP_LOCATION,
|
|
|
|
PROP_INDEX,
|
2009-09-11 11:12:54 +00:00
|
|
|
PROP_POST_MESSAGES,
|
2009-09-13 19:30:34 +00:00
|
|
|
PROP_NEXT_FILE,
|
2011-07-18 07:38:26 +00:00
|
|
|
PROP_MAX_FILES,
|
2009-09-11 10:16:18 +00:00
|
|
|
PROP_LAST
|
2006-11-10 18:51:10 +00:00
|
|
|
};
|
|
|
|
|
2009-09-11 10:16:18 +00:00
|
|
|
static void gst_multi_file_sink_finalize (GObject * object);
|
2006-11-10 18:51:10 +00:00
|
|
|
|
|
|
|
static void gst_multi_file_sink_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_multi_file_sink_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
|
2009-09-13 19:30:34 +00:00
|
|
|
static gboolean gst_multi_file_sink_stop (GstBaseSink * sink);
|
2006-11-10 18:51:10 +00:00
|
|
|
static GstFlowReturn gst_multi_file_sink_render (GstBaseSink * sink,
|
|
|
|
GstBuffer * buffer);
|
2011-01-03 03:19:27 +00:00
|
|
|
static gboolean gst_multi_file_sink_set_caps (GstBaseSink * sink,
|
|
|
|
GstCaps * caps);
|
2011-07-17 21:36:55 +00:00
|
|
|
static gboolean gst_multi_file_sink_open_next_file (GstMultiFileSink *
|
|
|
|
multifilesink);
|
|
|
|
static void gst_multi_file_sink_close_file (GstMultiFileSink * multifilesink,
|
|
|
|
GstBuffer * buffer);
|
2011-07-18 07:38:26 +00:00
|
|
|
static void gst_multi_file_sink_ensure_max_files (GstMultiFileSink *
|
|
|
|
multifilesink);
|
2006-11-10 18:51:10 +00:00
|
|
|
|
2009-09-13 19:30:34 +00:00
|
|
|
#define GST_TYPE_MULTI_FILE_SINK_NEXT (gst_multi_file_sink_next_get_type ())
|
|
|
|
static GType
|
|
|
|
gst_multi_file_sink_next_get_type (void)
|
|
|
|
{
|
|
|
|
static GType multi_file_sync_next_type = 0;
|
|
|
|
static const GEnumValue next_types[] = {
|
|
|
|
{GST_MULTI_FILE_SINK_NEXT_BUFFER, "New file for each buffer", "buffer"},
|
|
|
|
{GST_MULTI_FILE_SINK_NEXT_DISCONT, "New file after each discontinuity",
|
|
|
|
"discont"},
|
2010-02-24 03:48:10 +00:00
|
|
|
{GST_MULTI_FILE_SINK_NEXT_KEY_FRAME, "New file at each key frame "
|
|
|
|
"(Useful for MPEG-TS segmenting)", "key-frame"},
|
2009-09-13 19:30:34 +00:00
|
|
|
{0, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!multi_file_sync_next_type) {
|
|
|
|
multi_file_sync_next_type =
|
|
|
|
g_enum_register_static ("GstMultiFileSinkNext", next_types);
|
|
|
|
}
|
|
|
|
|
|
|
|
return multi_file_sync_next_type;
|
|
|
|
}
|
|
|
|
|
2006-11-10 18:51:10 +00:00
|
|
|
GST_BOILERPLATE (GstMultiFileSink, gst_multi_file_sink, GstBaseSink,
|
|
|
|
GST_TYPE_BASE_SINK);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_multi_file_sink_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_multi_file_sink_debug, "multifilesink", 0,
|
|
|
|
"multifilesink element");
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
|
|
|
gst_static_pad_template_get (&sinktemplate));
|
2010-03-18 13:31:35 +00:00
|
|
|
gst_element_class_set_details_simple (gstelement_class, "Multi-File Sink",
|
|
|
|
"Sink/File",
|
|
|
|
"Write buffers to a sequentially named set of files",
|
|
|
|
"David Schleef <ds@schleef.org>");
|
2006-11-10 18:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_multi_file_sink_class_init (GstMultiFileSinkClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->set_property = gst_multi_file_sink_set_property;
|
|
|
|
gobject_class->get_property = gst_multi_file_sink_get_property;
|
|
|
|
|
2009-09-11 10:16:18 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_LOCATION,
|
2006-11-10 18:51:10 +00:00
|
|
|
g_param_spec_string ("location", "File Location",
|
2010-10-13 14:21:23 +00:00
|
|
|
"Location of the file to write", NULL,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2009-09-11 10:16:18 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_INDEX,
|
2007-10-25 23:42:52 +00:00
|
|
|
g_param_spec_int ("index", "Index",
|
|
|
|
"Index to use with location property to create file names. The "
|
2009-09-11 10:16:18 +00:00
|
|
|
"index is incremented by one for each buffer written.",
|
2010-10-13 14:21:23 +00:00
|
|
|
0, G_MAXINT, DEFAULT_INDEX,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2009-09-11 10:16:18 +00:00
|
|
|
/**
|
2009-09-11 11:12:54 +00:00
|
|
|
* GstMultiFileSink:post-messages
|
2009-09-11 10:16:18 +00:00
|
|
|
*
|
|
|
|
* Post a message on the GstBus for each file.
|
|
|
|
*
|
|
|
|
* Since: 0.10.17
|
|
|
|
*/
|
2009-09-11 11:12:54 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_POST_MESSAGES,
|
|
|
|
g_param_spec_boolean ("post-messages", "Post Messages",
|
|
|
|
"Post a message for each file with information of the buffer",
|
2010-10-13 14:21:23 +00:00
|
|
|
DEFAULT_POST_MESSAGES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2009-09-13 19:30:34 +00:00
|
|
|
/**
|
|
|
|
* GstMultiFileSink:next-file
|
|
|
|
*
|
|
|
|
* When to start a new file.
|
|
|
|
*
|
|
|
|
* Since: 0.10.17
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (gobject_class, PROP_NEXT_FILE,
|
|
|
|
g_param_spec_enum ("next-file", "Next File",
|
|
|
|
"When to start a new file",
|
|
|
|
GST_TYPE_MULTI_FILE_SINK_NEXT, DEFAULT_NEXT_FILE,
|
2010-10-13 14:21:23 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_STATIC_STRINGS));
|
2009-09-11 10:16:18 +00:00
|
|
|
|
2011-07-18 07:38:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GstMultiFileSink:max-files
|
|
|
|
*
|
|
|
|
* Maximum number of files to keep on disk. Once the maximum is reached, old
|
|
|
|
* files start to be deleted to make room for new ones.
|
|
|
|
*
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (gobject_class, PROP_MAX_FILES,
|
|
|
|
g_param_spec_uint ("max-files", "Max files",
|
|
|
|
"Maximum number of files to keep on disk. Once the maximum is reached,"
|
|
|
|
"old files start to be deleted to make room for new ones.",
|
|
|
|
0, G_MAXUINT, DEFAULT_MAX_FILES,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2009-09-11 10:16:18 +00:00
|
|
|
gobject_class->finalize = gst_multi_file_sink_finalize;
|
2006-11-10 18:51:10 +00:00
|
|
|
|
|
|
|
gstbasesink_class->get_times = NULL;
|
2009-09-13 19:30:34 +00:00
|
|
|
gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_multi_file_sink_stop);
|
2006-11-10 18:51:10 +00:00
|
|
|
gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_multi_file_sink_render);
|
2011-01-03 03:19:27 +00:00
|
|
|
gstbasesink_class->set_caps =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_multi_file_sink_set_caps);
|
2006-11-10 18:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-10-25 23:42:52 +00:00
|
|
|
gst_multi_file_sink_init (GstMultiFileSink * multifilesink,
|
2006-11-10 18:51:10 +00:00
|
|
|
GstMultiFileSinkClass * g_class)
|
|
|
|
{
|
2007-10-25 23:42:52 +00:00
|
|
|
multifilesink->filename = g_strdup (DEFAULT_LOCATION);
|
|
|
|
multifilesink->index = DEFAULT_INDEX;
|
2009-09-11 11:12:54 +00:00
|
|
|
multifilesink->post_messages = DEFAULT_POST_MESSAGES;
|
2011-07-18 07:38:26 +00:00
|
|
|
multifilesink->max_files = DEFAULT_MAX_FILES;
|
|
|
|
multifilesink->files = NULL;
|
|
|
|
multifilesink->n_files = 0;
|
2006-11-10 18:51:10 +00:00
|
|
|
|
2009-09-11 10:16:18 +00:00
|
|
|
gst_base_sink_set_sync (GST_BASE_SINK (multifilesink), FALSE);
|
2010-02-24 03:48:10 +00:00
|
|
|
|
|
|
|
multifilesink->next_segment = GST_CLOCK_TIME_NONE;
|
2006-11-10 18:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-09-11 10:16:18 +00:00
|
|
|
gst_multi_file_sink_finalize (GObject * object)
|
2006-11-10 18:51:10 +00:00
|
|
|
{
|
|
|
|
GstMultiFileSink *sink = GST_MULTI_FILE_SINK (object);
|
|
|
|
|
|
|
|
g_free (sink->filename);
|
2011-07-18 12:30:51 +00:00
|
|
|
g_slist_foreach (sink->files, (GFunc) g_free, NULL);
|
|
|
|
g_slist_free (sink->files);
|
2007-09-24 10:53:36 +00:00
|
|
|
|
2009-09-11 10:16:18 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
2006-11-10 18:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_multi_file_sink_set_location (GstMultiFileSink * sink,
|
|
|
|
const gchar * location)
|
|
|
|
{
|
|
|
|
g_free (sink->filename);
|
2009-09-11 10:16:18 +00:00
|
|
|
/* FIXME: validate location to have just one %d */
|
|
|
|
sink->filename = g_strdup (location);
|
2006-11-10 18:51:10 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-01-28 10:29:42 +00:00
|
|
|
|
2006-11-10 18:51:10 +00:00
|
|
|
static void
|
|
|
|
gst_multi_file_sink_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstMultiFileSink *sink = GST_MULTI_FILE_SINK (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2009-09-11 10:16:18 +00:00
|
|
|
case PROP_LOCATION:
|
2006-11-10 18:51:10 +00:00
|
|
|
gst_multi_file_sink_set_location (sink, g_value_get_string (value));
|
|
|
|
break;
|
2009-09-11 10:16:18 +00:00
|
|
|
case PROP_INDEX:
|
2007-10-25 23:42:52 +00:00
|
|
|
sink->index = g_value_get_int (value);
|
|
|
|
break;
|
2009-09-11 11:12:54 +00:00
|
|
|
case PROP_POST_MESSAGES:
|
|
|
|
sink->post_messages = g_value_get_boolean (value);
|
2009-09-11 10:16:18 +00:00
|
|
|
break;
|
2009-09-13 19:30:34 +00:00
|
|
|
case PROP_NEXT_FILE:
|
|
|
|
sink->next_file = g_value_get_enum (value);
|
|
|
|
break;
|
2011-07-18 07:38:26 +00:00
|
|
|
case PROP_MAX_FILES:
|
|
|
|
sink->max_files = g_value_get_uint (value);
|
|
|
|
break;
|
2006-11-10 18:51:10 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_multi_file_sink_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstMultiFileSink *sink = GST_MULTI_FILE_SINK (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2009-09-11 10:16:18 +00:00
|
|
|
case PROP_LOCATION:
|
2006-11-10 18:51:10 +00:00
|
|
|
g_value_set_string (value, sink->filename);
|
|
|
|
break;
|
2009-09-11 10:16:18 +00:00
|
|
|
case PROP_INDEX:
|
2007-10-25 23:42:52 +00:00
|
|
|
g_value_set_int (value, sink->index);
|
|
|
|
break;
|
2009-09-11 11:12:54 +00:00
|
|
|
case PROP_POST_MESSAGES:
|
|
|
|
g_value_set_boolean (value, sink->post_messages);
|
2009-09-11 10:16:18 +00:00
|
|
|
break;
|
2009-09-13 19:30:34 +00:00
|
|
|
case PROP_NEXT_FILE:
|
|
|
|
g_value_set_enum (value, sink->next_file);
|
|
|
|
break;
|
2011-07-18 07:38:26 +00:00
|
|
|
case PROP_MAX_FILES:
|
|
|
|
g_value_set_uint (value, sink->max_files);
|
|
|
|
break;
|
2006-11-10 18:51:10 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-13 19:30:34 +00:00
|
|
|
static gboolean
|
|
|
|
gst_multi_file_sink_stop (GstBaseSink * sink)
|
2006-11-10 18:51:10 +00:00
|
|
|
{
|
2007-10-25 23:42:52 +00:00
|
|
|
GstMultiFileSink *multifilesink;
|
2011-01-03 03:19:27 +00:00
|
|
|
int i;
|
2006-11-10 18:51:10 +00:00
|
|
|
|
2007-10-25 23:42:52 +00:00
|
|
|
multifilesink = GST_MULTI_FILE_SINK (sink);
|
2006-11-10 18:51:10 +00:00
|
|
|
|
2009-09-13 19:30:34 +00:00
|
|
|
if (multifilesink->file != NULL) {
|
|
|
|
fclose (multifilesink->file);
|
|
|
|
multifilesink->file = NULL;
|
|
|
|
}
|
2009-09-11 10:16:18 +00:00
|
|
|
|
2011-01-03 03:19:27 +00:00
|
|
|
if (multifilesink->streamheaders) {
|
|
|
|
for (i = 0; i < multifilesink->n_streamheaders; i++) {
|
|
|
|
gst_buffer_unref (multifilesink->streamheaders[i]);
|
|
|
|
}
|
|
|
|
g_free (multifilesink->streamheaders);
|
2011-08-21 12:41:14 +00:00
|
|
|
multifilesink->streamheaders = NULL;
|
2011-01-03 03:19:27 +00:00
|
|
|
}
|
|
|
|
|
2009-09-13 19:30:34 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2009-09-11 10:16:18 +00:00
|
|
|
|
2009-09-13 19:30:34 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_multi_file_sink_post_message (GstMultiFileSink * multifilesink,
|
|
|
|
GstBuffer * buffer, const char *filename)
|
|
|
|
{
|
2009-09-11 11:12:54 +00:00
|
|
|
if (multifilesink->post_messages) {
|
2009-09-11 10:16:18 +00:00
|
|
|
GstClockTime duration, timestamp;
|
|
|
|
GstClockTime running_time, stream_time;
|
|
|
|
guint64 offset, offset_end;
|
|
|
|
GstStructure *s;
|
|
|
|
GstSegment *segment;
|
|
|
|
GstFormat format;
|
|
|
|
|
2009-09-13 19:30:34 +00:00
|
|
|
segment = &GST_BASE_SINK (multifilesink)->segment;
|
2009-09-11 10:16:18 +00:00
|
|
|
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));
|
2006-11-10 18:51:10 +00:00
|
|
|
}
|
2009-09-13 19:30:34 +00:00
|
|
|
}
|
2006-11-10 18:51:10 +00:00
|
|
|
|
2009-09-13 19:30:34 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_multi_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstMultiFileSink *multifilesink;
|
|
|
|
guint size;
|
|
|
|
guint8 *data;
|
|
|
|
gchar *filename;
|
|
|
|
gboolean ret;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
size = GST_BUFFER_SIZE (buffer);
|
|
|
|
data = GST_BUFFER_DATA (buffer);
|
|
|
|
|
|
|
|
multifilesink = GST_MULTI_FILE_SINK (sink);
|
|
|
|
|
|
|
|
switch (multifilesink->next_file) {
|
|
|
|
case GST_MULTI_FILE_SINK_NEXT_BUFFER:
|
2011-07-18 07:38:26 +00:00
|
|
|
gst_multi_file_sink_ensure_max_files (multifilesink);
|
|
|
|
|
2009-09-13 19:30:34 +00:00
|
|
|
filename = g_strdup_printf (multifilesink->filename,
|
|
|
|
multifilesink->index);
|
|
|
|
ret = g_file_set_contents (filename, (char *) data, size, &error);
|
|
|
|
if (!ret)
|
|
|
|
goto write_error;
|
|
|
|
|
2011-07-18 07:38:26 +00:00
|
|
|
multifilesink->files = g_slist_append (multifilesink->files, filename);
|
|
|
|
multifilesink->n_files += 1;
|
|
|
|
|
2009-09-13 19:30:34 +00:00
|
|
|
gst_multi_file_sink_post_message (multifilesink, buffer, filename);
|
|
|
|
multifilesink->index++;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case GST_MULTI_FILE_SINK_NEXT_DISCONT:
|
|
|
|
if (GST_BUFFER_IS_DISCONT (buffer)) {
|
2011-07-17 21:36:55 +00:00
|
|
|
if (multifilesink->file)
|
|
|
|
gst_multi_file_sink_close_file (multifilesink, buffer);
|
2009-09-13 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (multifilesink->file == NULL) {
|
2011-07-17 21:36:55 +00:00
|
|
|
if (!gst_multi_file_sink_open_next_file (multifilesink))
|
2010-02-24 03:48:10 +00:00
|
|
|
goto stdio_write_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fwrite (GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer), 1,
|
|
|
|
multifilesink->file);
|
|
|
|
if (ret != 1)
|
|
|
|
goto stdio_write_error;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case GST_MULTI_FILE_SINK_NEXT_KEY_FRAME:
|
|
|
|
if (multifilesink->next_segment == GST_CLOCK_TIME_NONE) {
|
|
|
|
if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
|
|
|
|
multifilesink->next_segment = GST_BUFFER_TIMESTAMP (buffer) +
|
|
|
|
10 * GST_SECOND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
|
|
|
|
GST_BUFFER_TIMESTAMP (buffer) >= multifilesink->next_segment &&
|
|
|
|
!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
|
2011-07-17 21:36:55 +00:00
|
|
|
if (multifilesink->file)
|
|
|
|
gst_multi_file_sink_close_file (multifilesink, buffer);
|
2010-02-24 03:48:10 +00:00
|
|
|
|
|
|
|
multifilesink->next_segment += 10 * GST_SECOND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (multifilesink->file == NULL) {
|
2011-01-03 03:19:27 +00:00
|
|
|
int i;
|
|
|
|
|
2011-07-17 21:36:55 +00:00
|
|
|
if (!gst_multi_file_sink_open_next_file (multifilesink))
|
2009-09-13 19:30:34 +00:00
|
|
|
goto stdio_write_error;
|
2011-01-03 03:19:27 +00:00
|
|
|
|
|
|
|
if (multifilesink->streamheaders) {
|
|
|
|
for (i = 0; i < multifilesink->n_streamheaders; i++) {
|
|
|
|
ret = fwrite (GST_BUFFER_DATA (multifilesink->streamheaders[i]),
|
|
|
|
GST_BUFFER_SIZE (multifilesink->streamheaders[i]), 1,
|
|
|
|
multifilesink->file);
|
|
|
|
if (ret != 1)
|
|
|
|
goto stdio_write_error;
|
|
|
|
}
|
|
|
|
}
|
2009-09-13 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = fwrite (GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer), 1,
|
|
|
|
multifilesink->file);
|
|
|
|
if (ret != 1)
|
|
|
|
goto stdio_write_error;
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
2008-02-08 03:44:12 +00:00
|
|
|
|
2009-09-11 10:16:18 +00:00
|
|
|
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;
|
|
|
|
}
|
2009-09-13 19:30:34 +00:00
|
|
|
stdio_write_error:
|
|
|
|
GST_ELEMENT_ERROR (multifilesink, RESOURCE, WRITE,
|
|
|
|
("Error while writing to file."), (NULL));
|
|
|
|
return GST_FLOW_ERROR;
|
2006-11-10 18:51:10 +00:00
|
|
|
}
|
2011-01-03 03:19:27 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_multi_file_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstMultiFileSink *multifilesink;
|
|
|
|
GstStructure *structure;
|
|
|
|
|
|
|
|
multifilesink = GST_MULTI_FILE_SINK (sink);
|
|
|
|
|
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
if (structure) {
|
|
|
|
const GValue *value;
|
|
|
|
|
|
|
|
value = gst_structure_get_value (structure, "streamheader");
|
|
|
|
|
|
|
|
if (GST_VALUE_HOLDS_ARRAY (value)) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (multifilesink->streamheaders) {
|
|
|
|
for (i = 0; i < multifilesink->n_streamheaders; i++) {
|
|
|
|
gst_buffer_unref (multifilesink->streamheaders[i]);
|
|
|
|
}
|
|
|
|
g_free (multifilesink->streamheaders);
|
|
|
|
}
|
|
|
|
|
|
|
|
multifilesink->n_streamheaders = gst_value_array_get_size (value);
|
|
|
|
multifilesink->streamheaders =
|
|
|
|
g_malloc (sizeof (GstBuffer *) * multifilesink->n_streamheaders);
|
|
|
|
|
|
|
|
for (i = 0; i < multifilesink->n_streamheaders; i++) {
|
|
|
|
multifilesink->streamheaders[i] =
|
|
|
|
gst_buffer_ref (gst_value_get_buffer (gst_value_array_get_value
|
|
|
|
(value, i)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-07-17 21:36:55 +00:00
|
|
|
|
2011-07-18 07:38:26 +00:00
|
|
|
static void
|
|
|
|
gst_multi_file_sink_ensure_max_files (GstMultiFileSink * multifilesink)
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
|
|
|
|
while (multifilesink->max_files &&
|
|
|
|
multifilesink->n_files >= multifilesink->max_files) {
|
|
|
|
filename = multifilesink->files->data;
|
|
|
|
g_remove (filename);
|
2011-11-24 17:14:53 +00:00
|
|
|
g_free (filename);
|
2011-07-18 07:38:26 +00:00
|
|
|
multifilesink->files = g_slist_delete_link (multifilesink->files,
|
|
|
|
multifilesink->files);
|
|
|
|
multifilesink->n_files -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 21:36:55 +00:00
|
|
|
static gboolean
|
|
|
|
gst_multi_file_sink_open_next_file (GstMultiFileSink * multifilesink)
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
|
|
|
|
g_return_val_if_fail (multifilesink->file == NULL, FALSE);
|
|
|
|
|
2011-07-18 07:38:26 +00:00
|
|
|
gst_multi_file_sink_ensure_max_files (multifilesink);
|
2011-07-17 21:36:55 +00:00
|
|
|
filename = g_strdup_printf (multifilesink->filename, multifilesink->index);
|
|
|
|
multifilesink->file = g_fopen (filename, "wb");
|
2011-07-18 07:38:26 +00:00
|
|
|
if (multifilesink->file == NULL) {
|
|
|
|
g_free (filename);
|
2011-07-17 21:36:55 +00:00
|
|
|
return FALSE;
|
2011-07-18 07:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
multifilesink->files = g_slist_append (multifilesink->files, filename);
|
|
|
|
multifilesink->n_files += 1;
|
2011-07-17 21:36:55 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_multi_file_sink_close_file (GstMultiFileSink * multifilesink,
|
|
|
|
GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
|
|
|
|
fclose (multifilesink->file);
|
|
|
|
multifilesink->file = NULL;
|
|
|
|
|
|
|
|
if (buffer) {
|
|
|
|
filename = g_strdup_printf (multifilesink->filename, multifilesink->index);
|
|
|
|
gst_multi_file_sink_post_message (multifilesink, buffer, filename);
|
|
|
|
g_free (filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
multifilesink->index++;
|
|
|
|
}
|