2002-04-28 17:08:59 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2000 Wim Taymans <wtay@chello.be>
|
|
|
|
*
|
|
|
|
* gstfilesink.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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-06-29 14:05:49 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2004-01-18 21:36:20 +00:00
|
|
|
#include "../gst-i18n-lib.h"
|
|
|
|
|
2002-04-28 17:08:59 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "gstfilesink.h"
|
|
|
|
#include <string.h>
|
2003-09-13 01:06:53 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2004-05-07 02:36:28 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2003-09-13 01:06:53 +00:00
|
|
|
#include <unistd.h>
|
2004-05-07 02:36:28 +00:00
|
|
|
#endif
|
2002-04-28 17:08:59 +00:00
|
|
|
|
2004-01-18 21:36:20 +00:00
|
|
|
|
gst/: s/gst_pad_new/&_from_template/ register pad templates in the base_init function add static pad template definit...
Original commit message from CVS:
* gst/autoplug/gstspideridentity.c:
(gst_spider_identity_request_new_pad):
* gst/elements/gstaggregator.c: (gst_aggregator_base_init),
(gst_aggregator_init):
* gst/elements/gstfakesink.c: (gst_fakesink_base_init),
(gst_fakesink_init):
* gst/elements/gstfakesrc.c: (gst_fakesrc_base_init),
(gst_fakesrc_init):
* gst/elements/gstfdsink.c: (gst_fdsink_base_init),
(gst_fdsink_init):
* gst/elements/gstfdsrc.c: (gst_fdsrc_base_init), (gst_fdsrc_init):
* gst/elements/gstfilesink.c: (gst_filesink_base_init),
(gst_filesink_init):
* gst/elements/gstfilesrc.c: (gst_filesrc_base_init),
(gst_filesrc_init):
* gst/elements/gstidentity.c: (gst_identity_base_init),
(gst_identity_init):
* gst/elements/gstmultifilesrc.c: (gst_multifilesrc_base_init),
(gst_multifilesrc_init):
* gst/elements/gstpipefilter.c: (gst_pipefilter_base_init),
(gst_pipefilter_init):
* gst/elements/gststatistics.c: (gst_statistics_base_init),
(gst_statistics_init):
* gst/elements/gsttee.c: (gst_tee_base_init), (gst_tee_init):
* gst/gstqueue.c: (gst_queue_base_init), (gst_queue_init):
s/gst_pad_new/&_from_template/
register pad templates in the base_init function
add static pad template definitions
2004-08-17 14:11:23 +00:00
|
|
|
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
2003-10-31 19:32:47 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_filesink_debug);
|
2003-06-29 14:05:49 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_filesink_debug
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
GstElementDetails gst_filesink_details = GST_ELEMENT_DETAILS ("File Sink",
|
|
|
|
"Sink/File",
|
|
|
|
"Write stream to a file",
|
|
|
|
"Thomas <thomas@apestaart.org>");
|
2002-04-28 17:08:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* FileSink signals and args */
|
2004-03-13 15:27:01 +00:00
|
|
|
enum
|
|
|
|
{
|
2002-04-28 17:08:59 +00:00
|
|
|
/* FILL ME */
|
|
|
|
SIGNAL_HANDOFF,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
enum
|
|
|
|
{
|
2002-04-28 17:08:59 +00:00
|
|
|
ARG_0,
|
2003-06-01 12:27:39 +00:00
|
|
|
ARG_LOCATION
|
2002-04-28 17:08:59 +00:00
|
|
|
};
|
|
|
|
|
2004-02-04 19:38:33 +00:00
|
|
|
static const GstFormat *
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_get_formats (GstPad * pad)
|
2004-02-04 19:38:33 +00:00
|
|
|
{
|
|
|
|
static const GstFormat formats[] = {
|
|
|
|
GST_FORMAT_BYTES,
|
|
|
|
0,
|
|
|
|
};
|
2004-03-15 19:27:17 +00:00
|
|
|
|
2004-02-04 19:38:33 +00:00
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GstQueryType *
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_get_query_types (GstPad * pad)
|
2004-02-04 19:38:33 +00:00
|
|
|
{
|
|
|
|
static const GstQueryType types[] = {
|
|
|
|
GST_QUERY_TOTAL,
|
|
|
|
GST_QUERY_POSITION,
|
|
|
|
0
|
|
|
|
};
|
2004-03-15 19:27:17 +00:00
|
|
|
|
2004-02-04 19:38:33 +00:00
|
|
|
return types;
|
|
|
|
}
|
2002-04-28 17:08:59 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static void gst_filesink_dispose (GObject * object);
|
|
|
|
|
|
|
|
static void gst_filesink_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_filesink_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
2002-04-28 17:08:59 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static gboolean gst_filesink_open_file (GstFileSink * sink);
|
|
|
|
static void gst_filesink_close_file (GstFileSink * sink);
|
2002-04-28 17:08:59 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static gboolean gst_filesink_handle_event (GstPad * pad, GstEvent * event);
|
|
|
|
static gboolean gst_filesink_pad_query (GstPad * pad, GstQueryType type,
|
|
|
|
GstFormat * format, gint64 * value);
|
|
|
|
static void gst_filesink_chain (GstPad * pad, GstData * _data);
|
2002-04-28 17:08:59 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static void gst_filesink_uri_handler_init (gpointer g_iface,
|
|
|
|
gpointer iface_data);
|
2002-04-28 17:08:59 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static GstElementStateReturn gst_filesink_change_state (GstElement * element);
|
2002-04-28 17:08:59 +00:00
|
|
|
|
|
|
|
static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
|
|
|
|
|
2004-01-08 04:10:18 +00:00
|
|
|
static void
|
|
|
|
_do_init (GType filesink_type)
|
2002-04-28 17:08:59 +00:00
|
|
|
{
|
2004-01-08 04:10:18 +00:00
|
|
|
static const GInterfaceInfo urihandler_info = {
|
|
|
|
gst_filesink_uri_handler_init,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
2004-03-15 19:27:17 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
g_type_add_interface_static (filesink_type, GST_TYPE_URI_HANDLER,
|
|
|
|
&urihandler_info);
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_filesink_debug, "filesink", 0,
|
|
|
|
"filesink element");
|
2002-04-28 17:08:59 +00:00
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_BOILERPLATE_FULL (GstFileSink, gst_filesink, GstElement, GST_TYPE_ELEMENT,
|
|
|
|
_do_init);
|
|
|
|
|
2004-01-08 04:10:18 +00:00
|
|
|
|
2002-04-28 17:08:59 +00:00
|
|
|
static void
|
2003-10-31 19:32:47 +00:00
|
|
|
gst_filesink_base_init (gpointer g_class)
|
2002-04-28 17:08:59 +00:00
|
|
|
{
|
2003-10-31 19:32:47 +00:00
|
|
|
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
|
2002-04-28 17:08:59 +00:00
|
|
|
|
2003-10-31 19:32:47 +00:00
|
|
|
gstelement_class->change_state = gst_filesink_change_state;
|
gst/: s/gst_pad_new/&_from_template/ register pad templates in the base_init function add static pad template definit...
Original commit message from CVS:
* gst/autoplug/gstspideridentity.c:
(gst_spider_identity_request_new_pad):
* gst/elements/gstaggregator.c: (gst_aggregator_base_init),
(gst_aggregator_init):
* gst/elements/gstfakesink.c: (gst_fakesink_base_init),
(gst_fakesink_init):
* gst/elements/gstfakesrc.c: (gst_fakesrc_base_init),
(gst_fakesrc_init):
* gst/elements/gstfdsink.c: (gst_fdsink_base_init),
(gst_fdsink_init):
* gst/elements/gstfdsrc.c: (gst_fdsrc_base_init), (gst_fdsrc_init):
* gst/elements/gstfilesink.c: (gst_filesink_base_init),
(gst_filesink_init):
* gst/elements/gstfilesrc.c: (gst_filesrc_base_init),
(gst_filesrc_init):
* gst/elements/gstidentity.c: (gst_identity_base_init),
(gst_identity_init):
* gst/elements/gstmultifilesrc.c: (gst_multifilesrc_base_init),
(gst_multifilesrc_init):
* gst/elements/gstpipefilter.c: (gst_pipefilter_base_init),
(gst_pipefilter_init):
* gst/elements/gststatistics.c: (gst_statistics_base_init),
(gst_statistics_init):
* gst/elements/gsttee.c: (gst_tee_base_init), (gst_tee_init):
* gst/gstqueue.c: (gst_queue_base_init), (gst_queue_init):
s/gst_pad_new/&_from_template/
register pad templates in the base_init function
add static pad template definitions
2004-08-17 14:11:23 +00:00
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
|
|
|
gst_static_pad_template_get (&sinktemplate));
|
2003-10-31 19:32:47 +00:00
|
|
|
gst_element_class_set_details (gstelement_class, &gst_filesink_details);
|
|
|
|
}
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_class_init (GstFileSinkClass * klass)
|
2003-10-31 19:32:47 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
2002-04-28 17:08:59 +00:00
|
|
|
|
|
|
|
|
2002-11-21 23:52:30 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
|
2004-03-13 15:27:01 +00:00
|
|
|
g_param_spec_string ("location", "File Location",
|
2004-03-15 19:27:17 +00:00
|
|
|
"Location of the file to write", NULL, G_PARAM_READWRITE));
|
2002-04-28 17:08:59 +00:00
|
|
|
|
|
|
|
gst_filesink_signals[SIGNAL_HANDOFF] =
|
2004-03-13 15:27:01 +00:00
|
|
|
g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GstFileSinkClass, handoff), NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
|
2002-04-28 17:08:59 +00:00
|
|
|
|
|
|
|
gobject_class->set_property = gst_filesink_set_property;
|
|
|
|
gobject_class->get_property = gst_filesink_get_property;
|
2004-03-13 15:27:01 +00:00
|
|
|
gobject_class->dispose = gst_filesink_dispose;
|
2002-04-28 17:08:59 +00:00
|
|
|
}
|
2004-03-13 15:27:01 +00:00
|
|
|
static void
|
|
|
|
gst_filesink_init (GstFileSink * filesink)
|
2002-04-28 17:08:59 +00:00
|
|
|
{
|
|
|
|
GstPad *pad;
|
|
|
|
|
gst/: s/gst_pad_new/&_from_template/ register pad templates in the base_init function add static pad template definit...
Original commit message from CVS:
* gst/autoplug/gstspideridentity.c:
(gst_spider_identity_request_new_pad):
* gst/elements/gstaggregator.c: (gst_aggregator_base_init),
(gst_aggregator_init):
* gst/elements/gstfakesink.c: (gst_fakesink_base_init),
(gst_fakesink_init):
* gst/elements/gstfakesrc.c: (gst_fakesrc_base_init),
(gst_fakesrc_init):
* gst/elements/gstfdsink.c: (gst_fdsink_base_init),
(gst_fdsink_init):
* gst/elements/gstfdsrc.c: (gst_fdsrc_base_init), (gst_fdsrc_init):
* gst/elements/gstfilesink.c: (gst_filesink_base_init),
(gst_filesink_init):
* gst/elements/gstfilesrc.c: (gst_filesrc_base_init),
(gst_filesrc_init):
* gst/elements/gstidentity.c: (gst_identity_base_init),
(gst_identity_init):
* gst/elements/gstmultifilesrc.c: (gst_multifilesrc_base_init),
(gst_multifilesrc_init):
* gst/elements/gstpipefilter.c: (gst_pipefilter_base_init),
(gst_pipefilter_init):
* gst/elements/gststatistics.c: (gst_statistics_base_init),
(gst_statistics_init):
* gst/elements/gsttee.c: (gst_tee_base_init), (gst_tee_init):
* gst/gstqueue.c: (gst_queue_base_init), (gst_queue_init):
s/gst_pad_new/&_from_template/
register pad templates in the base_init function
add static pad template definitions
2004-08-17 14:11:23 +00:00
|
|
|
pad =
|
|
|
|
gst_pad_new_from_template (gst_static_pad_template_get (&sinktemplate),
|
|
|
|
"sink");
|
2002-04-28 17:08:59 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (filesink), pad);
|
|
|
|
gst_pad_set_chain_function (pad, gst_filesink_chain);
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_FLAG_SET (GST_ELEMENT (filesink), GST_ELEMENT_EVENT_AWARE);
|
2002-04-28 17:08:59 +00:00
|
|
|
|
2003-06-01 12:27:39 +00:00
|
|
|
gst_pad_set_query_function (pad, gst_filesink_pad_query);
|
|
|
|
gst_pad_set_query_type_function (pad, gst_filesink_get_query_types);
|
|
|
|
gst_pad_set_formats_function (pad, gst_filesink_get_formats);
|
|
|
|
|
2002-04-28 17:08:59 +00:00
|
|
|
filesink->filename = NULL;
|
|
|
|
filesink->file = NULL;
|
|
|
|
}
|
2003-11-24 03:21:54 +00:00
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_dispose (GObject * object)
|
2003-11-24 03:21:54 +00:00
|
|
|
{
|
|
|
|
GstFileSink *sink = GST_FILESINK (object);
|
2002-04-28 17:08:59 +00:00
|
|
|
|
2003-11-24 03:21:54 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-11-24 03:21:54 +00:00
|
|
|
g_free (sink->uri);
|
|
|
|
sink->uri = NULL;
|
|
|
|
g_free (sink->filename);
|
|
|
|
sink->filename = NULL;
|
|
|
|
}
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-11-24 03:21:54 +00:00
|
|
|
static gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_set_location (GstFileSink * sink, const gchar * location)
|
2003-11-24 03:21:54 +00:00
|
|
|
{
|
|
|
|
/* the element must be stopped or paused in order to do this */
|
|
|
|
if (GST_STATE (sink) > GST_STATE_PAUSED)
|
|
|
|
return FALSE;
|
|
|
|
if (GST_STATE (sink) == GST_STATE_PAUSED &&
|
|
|
|
GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
g_free (sink->filename);
|
|
|
|
g_free (sink->uri);
|
2004-01-07 13:13:03 +00:00
|
|
|
if (location != NULL) {
|
|
|
|
sink->filename = g_strdup (location);
|
|
|
|
sink->uri = gst_uri_construct ("file", location);
|
|
|
|
} else {
|
|
|
|
sink->filename = NULL;
|
|
|
|
sink->uri = NULL;
|
|
|
|
}
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-11-24 03:21:54 +00:00
|
|
|
if (GST_STATE (sink) == GST_STATE_PAUSED)
|
|
|
|
gst_filesink_open_file (sink);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2002-04-28 17:08:59 +00:00
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
2002-04-28 17:08:59 +00:00
|
|
|
{
|
|
|
|
GstFileSink *sink;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
sink = GST_FILESINK (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_LOCATION:
|
2003-11-24 03:21:54 +00:00
|
|
|
gst_filesink_set_location (sink, g_value_get_string (value));
|
2002-04-28 17:08:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
2003-06-01 12:27:39 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2002-04-28 17:08:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static void
|
|
|
|
gst_filesink_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
2002-04-28 17:08:59 +00:00
|
|
|
{
|
|
|
|
GstFileSink *sink;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-04-28 17:08:59 +00:00
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail (GST_IS_FILESINK (object));
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-04-28 17:08:59 +00:00
|
|
|
sink = GST_FILESINK (object);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-04-28 17:08:59 +00:00
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_LOCATION:
|
2003-06-01 12:27:39 +00:00
|
|
|
g_value_set_string (value, sink->filename);
|
2002-04-28 17:08:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_open_file (GstFileSink * sink)
|
2002-04-28 17:08:59 +00:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN), FALSE);
|
|
|
|
|
|
|
|
/* open the file */
|
2004-03-13 15:27:01 +00:00
|
|
|
if (sink->filename == NULL || sink->filename[0] == '\0') {
|
2004-01-29 23:17:58 +00:00
|
|
|
GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
|
2004-03-15 19:27:17 +00:00
|
|
|
(_("No file name specified for writing.")), (NULL));
|
2002-04-28 17:08:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2003-06-01 12:27:39 +00:00
|
|
|
|
2004-07-12 20:24:45 +00:00
|
|
|
sink->file = fopen (sink->filename, "wb");
|
2002-04-28 17:08:59 +00:00
|
|
|
if (sink->file == NULL) {
|
2004-01-29 23:17:58 +00:00
|
|
|
GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
|
2004-03-15 19:27:17 +00:00
|
|
|
(_("Could not open file \"%s\" for writing."), sink->filename),
|
|
|
|
GST_ERROR_SYSTEM);
|
2002-04-28 17:08:59 +00:00
|
|
|
return FALSE;
|
2004-01-18 21:36:20 +00:00
|
|
|
}
|
2002-04-28 17:08:59 +00:00
|
|
|
|
|
|
|
GST_FLAG_SET (sink, GST_FILESINK_OPEN);
|
|
|
|
|
|
|
|
sink->data_written = 0;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_close_file (GstFileSink * sink)
|
2002-04-28 17:08:59 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
if (fclose (sink->file) != 0) {
|
2004-01-29 23:17:58 +00:00
|
|
|
GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
|
2004-03-15 19:27:17 +00:00
|
|
|
(_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
|
2004-03-13 15:27:01 +00:00
|
|
|
} else {
|
2002-04-28 17:08:59 +00:00
|
|
|
GST_FLAG_UNSET (sink, GST_FILESINK_OPEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-01 12:27:39 +00:00
|
|
|
static gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_pad_query (GstPad * pad, GstQueryType type,
|
|
|
|
GstFormat * format, gint64 * value)
|
2003-06-01 12:27:39 +00:00
|
|
|
{
|
|
|
|
GstFileSink *sink = GST_FILESINK (GST_PAD_PARENT (pad));
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GST_QUERY_TOTAL:
|
|
|
|
switch (*format) {
|
2004-03-15 19:27:17 +00:00
|
|
|
case GST_FORMAT_BYTES:
|
|
|
|
if (GST_FLAG_IS_SET (GST_ELEMENT (sink), GST_FILESINK_OPEN)) {
|
|
|
|
*value = sink->data_written; /* FIXME - doesn't the kernel provide
|
|
|
|
such a function? */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return FALSE;
|
2003-06-01 12:27:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GST_QUERY_POSITION:
|
|
|
|
switch (*format) {
|
2004-03-15 19:27:17 +00:00
|
|
|
case GST_FORMAT_BYTES:
|
|
|
|
if (GST_FLAG_IS_SET (GST_ELEMENT (sink), GST_FILESINK_OPEN)) {
|
|
|
|
*value = ftell (sink->file);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return FALSE;
|
2003-06-01 12:27:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2002-04-28 17:08:59 +00:00
|
|
|
/* handle events (search) */
|
|
|
|
static gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_handle_event (GstPad * pad, GstEvent * event)
|
2002-04-28 17:08:59 +00:00
|
|
|
{
|
|
|
|
GstEventType type;
|
|
|
|
GstFileSink *filesink;
|
|
|
|
|
|
|
|
filesink = GST_FILESINK (gst_pad_get_parent (pad));
|
|
|
|
|
2004-04-29 22:12:35 +00:00
|
|
|
if (!(GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN))) {
|
|
|
|
gst_event_unref (event);
|
2004-03-18 13:01:21 +00:00
|
|
|
return FALSE;
|
2004-04-29 22:12:35 +00:00
|
|
|
}
|
|
|
|
|
2002-04-28 17:08:59 +00:00
|
|
|
type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GST_EVENT_SEEK:
|
2004-04-29 22:12:35 +00:00
|
|
|
if (GST_EVENT_SEEK_FORMAT (event) != GST_FORMAT_BYTES) {
|
|
|
|
gst_event_unref (event);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2002-05-26 21:54:27 +00:00
|
|
|
|
2004-04-29 22:12:35 +00:00
|
|
|
if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH) {
|
|
|
|
if (fflush (filesink->file)) {
|
|
|
|
gst_event_unref (event);
|
2004-03-15 19:27:17 +00:00
|
|
|
GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
|
|
|
|
(_("Error while writing to file \"%s\"."), filesink->filename),
|
|
|
|
GST_ERROR_SYSTEM);
|
2005-01-24 11:04:37 +00:00
|
|
|
return FALSE;
|
2004-04-29 22:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
2004-03-13 15:27:01 +00:00
|
|
|
|
|
|
|
switch (GST_EVENT_SEEK_METHOD (event)) {
|
2004-03-15 19:27:17 +00:00
|
|
|
case GST_SEEK_METHOD_SET:
|
|
|
|
fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_SET);
|
|
|
|
break;
|
|
|
|
case GST_SEEK_METHOD_CUR:
|
|
|
|
fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_CUR);
|
|
|
|
break;
|
|
|
|
case GST_SEEK_METHOD_END:
|
|
|
|
fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_END);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_warning ("unknown seek method!");
|
|
|
|
break;
|
2002-04-28 17:08:59 +00:00
|
|
|
}
|
2004-04-29 22:12:35 +00:00
|
|
|
gst_event_unref (event);
|
2002-04-28 17:08:59 +00:00
|
|
|
break;
|
2002-06-08 15:00:30 +00:00
|
|
|
case GST_EVENT_DISCONTINUOUS:
|
|
|
|
{
|
|
|
|
gint64 offset;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-06-08 15:00:30 +00:00
|
|
|
if (gst_event_discont_get_value (event, GST_FORMAT_BYTES, &offset))
|
2004-03-15 19:27:17 +00:00
|
|
|
fseek (filesink->file, offset, SEEK_SET);
|
2002-06-08 15:00:30 +00:00
|
|
|
|
2002-07-08 19:22:02 +00:00
|
|
|
gst_event_unref (event);
|
2002-06-08 15:00:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-06-01 12:27:39 +00:00
|
|
|
case GST_EVENT_FLUSH:
|
|
|
|
if (fflush (filesink->file)) {
|
2004-04-29 22:12:35 +00:00
|
|
|
gst_event_unref (event);
|
2004-03-15 19:27:17 +00:00
|
|
|
GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
|
|
|
|
(_("Error while writing to file \"%s\"."), filesink->filename),
|
|
|
|
GST_ERROR_SYSTEM);
|
2002-09-01 12:55:57 +00:00
|
|
|
}
|
2002-04-28 17:08:59 +00:00
|
|
|
break;
|
2003-06-01 12:27:39 +00:00
|
|
|
case GST_EVENT_EOS:
|
2004-04-29 22:12:35 +00:00
|
|
|
gst_event_unref (event);
|
2003-06-01 12:27:39 +00:00
|
|
|
gst_filesink_close_file (filesink);
|
|
|
|
gst_element_set_eos (GST_ELEMENT (filesink));
|
2002-04-28 17:08:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
gst_pad_event_default (pad, event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_filesink_chain:
|
|
|
|
* @pad: the pad this filesink is connected to
|
|
|
|
* @buf: the buffer that has to be absorbed
|
|
|
|
*
|
|
|
|
* take the buffer from the pad and write to file if it's open
|
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
static void
|
|
|
|
gst_filesink_chain (GstPad * pad, GstData * _data)
|
2002-04-28 17:08:59 +00:00
|
|
|
{
|
2003-10-08 16:06:02 +00:00
|
|
|
GstBuffer *buf = GST_BUFFER (_data);
|
2002-04-28 17:08:59 +00:00
|
|
|
GstFileSink *filesink;
|
|
|
|
|
|
|
|
g_return_if_fail (pad != NULL);
|
|
|
|
g_return_if_fail (GST_IS_PAD (pad));
|
|
|
|
g_return_if_fail (buf != NULL);
|
|
|
|
|
|
|
|
filesink = GST_FILESINK (gst_pad_get_parent (pad));
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
if (GST_IS_EVENT (buf)) {
|
|
|
|
gst_filesink_handle_event (pad, GST_EVENT (buf));
|
2002-04-28 17:08:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
if (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN)) {
|
2003-07-22 21:33:02 +00:00
|
|
|
guint bytes_written = 0, back_pending = 0;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
|
|
|
if (ftell (filesink->file) < filesink->data_written)
|
|
|
|
back_pending = filesink->data_written - ftell (filesink->file);
|
2003-09-13 01:06:53 +00:00
|
|
|
while (bytes_written < GST_BUFFER_SIZE (buf)) {
|
2003-06-07 09:35:37 +00:00
|
|
|
size_t wrote = fwrite (GST_BUFFER_DATA (buf) + bytes_written, 1,
|
2004-03-15 19:27:17 +00:00
|
|
|
GST_BUFFER_SIZE (buf) - bytes_written,
|
|
|
|
filesink->file);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-06-07 09:35:37 +00:00
|
|
|
if (wrote <= 0) {
|
2004-03-15 19:27:17 +00:00
|
|
|
GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
|
|
|
|
(_("Error while writing to file \"%s\"."), filesink->filename),
|
|
|
|
("Only %d of %d bytes written: %s",
|
|
|
|
bytes_written, GST_BUFFER_SIZE (buf), strerror (errno)));
|
|
|
|
break;
|
2003-06-07 09:35:37 +00:00
|
|
|
}
|
|
|
|
bytes_written += wrote;
|
2003-09-13 01:06:53 +00:00
|
|
|
}
|
2003-06-07 09:35:37 +00:00
|
|
|
|
2003-07-22 21:33:02 +00:00
|
|
|
filesink->data_written += bytes_written - back_pending;
|
2002-04-28 17:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
|
2003-06-01 12:27:39 +00:00
|
|
|
g_signal_emit (G_OBJECT (filesink),
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_signals[SIGNAL_HANDOFF], 0, filesink);
|
2002-04-28 17:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstElementStateReturn
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_change_state (GstElement * element)
|
2002-04-28 17:08:59 +00:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
|
|
|
|
|
2003-06-01 12:27:39 +00:00
|
|
|
switch (GST_STATE_TRANSITION (element)) {
|
|
|
|
case GST_STATE_PAUSED_TO_READY:
|
|
|
|
if (GST_FLAG_IS_SET (element, GST_FILESINK_OPEN))
|
2004-03-15 19:27:17 +00:00
|
|
|
gst_filesink_close_file (GST_FILESINK (element));
|
2003-06-01 12:27:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_STATE_READY_TO_PAUSED:
|
|
|
|
if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
|
2004-03-15 19:27:17 +00:00
|
|
|
if (!gst_filesink_open_file (GST_FILESINK (element)))
|
|
|
|
return GST_STATE_FAILURE;
|
2003-06-01 12:27:39 +00:00
|
|
|
}
|
|
|
|
break;
|
2002-04-28 17:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
|
|
|
return GST_ELEMENT_CLASS (parent_class)->change_state (element);
|
|
|
|
|
|
|
|
return GST_STATE_SUCCESS;
|
|
|
|
}
|
2003-11-24 03:21:54 +00:00
|
|
|
|
|
|
|
/*** GSTURIHANDLER INTERFACE *************************************************/
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gst_filesink_uri_get_type (void)
|
|
|
|
{
|
|
|
|
return GST_URI_SINK;
|
|
|
|
}
|
|
|
|
static gchar **
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_uri_get_protocols (void)
|
2003-11-24 03:21:54 +00:00
|
|
|
{
|
2004-03-13 15:27:01 +00:00
|
|
|
static gchar *protocols[] = { "file", NULL };
|
2004-03-15 19:27:17 +00:00
|
|
|
|
2003-11-24 03:21:54 +00:00
|
|
|
return protocols;
|
|
|
|
}
|
|
|
|
static const gchar *
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_uri_get_uri (GstURIHandler * handler)
|
2003-11-24 03:21:54 +00:00
|
|
|
{
|
|
|
|
GstFileSink *sink = GST_FILESINK (handler);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-11-24 03:21:54 +00:00
|
|
|
return sink->uri;
|
|
|
|
}
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2003-11-24 03:21:54 +00:00
|
|
|
static gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_filesink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
|
2003-11-24 03:21:54 +00:00
|
|
|
{
|
|
|
|
gchar *protocol, *location;
|
|
|
|
gboolean ret;
|
|
|
|
GstFileSink *sink = GST_FILESINK (handler);
|
|
|
|
|
|
|
|
protocol = gst_uri_get_protocol (uri);
|
|
|
|
if (strcmp (protocol, "file") != 0) {
|
|
|
|
g_free (protocol);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
g_free (protocol);
|
|
|
|
location = gst_uri_get_location (uri);
|
|
|
|
ret = gst_filesink_set_location (sink, location);
|
|
|
|
g_free (location);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_filesink_uri_handler_init (gpointer g_iface, gpointer iface_data)
|
|
|
|
{
|
|
|
|
GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
|
|
|
|
|
|
|
|
iface->get_type = gst_filesink_uri_get_type;
|
|
|
|
iface->get_protocols = gst_filesink_uri_get_protocols;
|
|
|
|
iface->get_uri = gst_filesink_uri_get_uri;
|
|
|
|
iface->set_uri = gst_filesink_uri_set_uri;
|
|
|
|
}
|