2001-12-23 16:42:33 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2000 Wim Taymans <wtay@chello.be>
|
|
|
|
* 2001 Bastien Nocera <hadess@hadess.net>
|
2003-12-23 21:28:35 +00:00
|
|
|
* 2003 Colin Walters <walters@verbum.org>
|
2001-12-23 16:42:33 +00:00
|
|
|
*
|
|
|
|
* gstgnomevfssink.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 19:46:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2003-10-31 20:03:29 +00:00
|
|
|
|
2004-01-19 13:50:39 +00:00
|
|
|
#include "gst/gst-i18n-plugin.h"
|
2004-01-18 21:46:58 +00:00
|
|
|
|
2003-10-31 20:03:29 +00:00
|
|
|
#include "gstgnomevfs.h"
|
|
|
|
|
2001-12-23 16:42:33 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
#include <libgnomevfs/gnome-vfs.h>
|
2001-12-26 23:56:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2001-12-23 16:42:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define GST_TYPE_GNOMEVFSSINK \
|
|
|
|
(gst_gnomevfssink_get_type())
|
|
|
|
#define GST_GNOMEVFSSINK(obj) \
|
|
|
|
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_GNOMEVFSSINK,GstGnomeVFSSink))
|
|
|
|
#define GST_GNOMEVFSSINK_CLASS(klass) \
|
|
|
|
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_GNOMEVFSSINK,GstGnomeVFSSinkClass))
|
|
|
|
#define GST_IS_GNOMEVFSSINK(obj) \
|
|
|
|
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_GNOMEVFSSINK))
|
|
|
|
#define GST_IS_GNOMEVFSSINK_CLASS(obj) \
|
|
|
|
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_GNOMEVFSSINK))
|
|
|
|
|
|
|
|
typedef struct _GstGnomeVFSSink GstGnomeVFSSink;
|
|
|
|
typedef struct _GstGnomeVFSSinkClass GstGnomeVFSSinkClass;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
GST_GNOMEVFSSINK_OPEN = GST_ELEMENT_FLAG_LAST,
|
|
|
|
|
|
|
|
GST_GNOMEVFSSINK_FLAG_LAST = GST_ELEMENT_FLAG_LAST + 2,
|
|
|
|
} GstGnomeVFSSinkFlags;
|
|
|
|
|
|
|
|
struct _GstGnomeVFSSink {
|
|
|
|
GstElement element;
|
|
|
|
|
|
|
|
/* filename */
|
|
|
|
gchar *filename;
|
|
|
|
/* uri */
|
|
|
|
GnomeVFSURI *uri;
|
|
|
|
/* handle */
|
|
|
|
GnomeVFSHandle *handle;
|
|
|
|
/* wether to erase a file or not */
|
|
|
|
gboolean erase;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GstGnomeVFSSinkClass {
|
|
|
|
GstElementClass parent_class;
|
|
|
|
|
|
|
|
/* signals */
|
|
|
|
void (*handoff) (GstElement *element,GstPad *pad);
|
|
|
|
void (*erase_ask) (GstElement *element,GstPad *pad);
|
|
|
|
};
|
|
|
|
|
2002-09-18 19:02:52 +00:00
|
|
|
/* elementfactory information */
|
2003-10-31 20:03:29 +00:00
|
|
|
static GstElementDetails gst_gnomevfssink_details = GST_ELEMENT_DETAILS (
|
2001-12-23 16:42:33 +00:00
|
|
|
"GnomeVFS Sink",
|
2002-04-20 21:42:51 +00:00
|
|
|
"Sink/File",
|
|
|
|
"Write stream to a GnomeVFS URI",
|
2003-10-31 20:03:29 +00:00
|
|
|
"Bastien Nocera <hadess@hadess.net>"
|
|
|
|
);
|
2001-12-23 16:42:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* GnomeVFSSink signals and args */
|
|
|
|
enum {
|
|
|
|
/* FILL ME */
|
|
|
|
SIGNAL_HANDOFF,
|
|
|
|
SIGNAL_ERASE_ASK,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_0,
|
|
|
|
ARG_LOCATION,
|
2003-12-23 21:28:35 +00:00
|
|
|
ARG_HANDLE,
|
2001-12-23 16:42:33 +00:00
|
|
|
ARG_ERASE
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-10-31 20:03:29 +00:00
|
|
|
static void gst_gnomevfssink_base_init (gpointer g_class);
|
2001-12-23 16:42:33 +00:00
|
|
|
static void gst_gnomevfssink_class_init (GstGnomeVFSSinkClass *klass);
|
|
|
|
static void gst_gnomevfssink_init (GstGnomeVFSSink *gnomevfssink);
|
|
|
|
|
|
|
|
static void gst_gnomevfssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
|
|
|
|
static void gst_gnomevfssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
|
|
|
|
|
|
|
|
static gboolean gst_gnomevfssink_open_file (GstGnomeVFSSink *sink);
|
|
|
|
static void gst_gnomevfssink_close_file (GstGnomeVFSSink *sink);
|
|
|
|
|
2003-10-08 16:08:22 +00:00
|
|
|
static void gst_gnomevfssink_chain (GstPad *pad,GstData *_data);
|
2001-12-23 16:42:33 +00:00
|
|
|
|
|
|
|
static GstElementStateReturn gst_gnomevfssink_change_state (GstElement *element);
|
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
|
|
|
static guint gst_gnomevfssink_signals[LAST_SIGNAL] = { 0 };
|
|
|
|
|
|
|
|
GType
|
|
|
|
gst_gnomevfssink_get_type (void)
|
|
|
|
{
|
|
|
|
static GType gnomevfssink_type = 0;
|
|
|
|
|
|
|
|
if (!gnomevfssink_type) {
|
|
|
|
static const GTypeInfo gnomevfssink_info = {
|
2003-10-31 20:03:29 +00:00
|
|
|
sizeof(GstGnomeVFSSinkClass),
|
|
|
|
gst_gnomevfssink_base_init,
|
2001-12-23 16:42:33 +00:00
|
|
|
NULL,
|
|
|
|
(GClassInitFunc)gst_gnomevfssink_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof(GstGnomeVFSSink),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc)gst_gnomevfssink_init,
|
|
|
|
};
|
|
|
|
gnomevfssink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstGnomeVFSSink", &gnomevfssink_info, 0);
|
|
|
|
}
|
|
|
|
return gnomevfssink_type;
|
|
|
|
}
|
|
|
|
|
2003-10-31 20:03:29 +00:00
|
|
|
static void
|
|
|
|
gst_gnomevfssink_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
|
|
|
|
gst_element_class_set_details (element_class, &gst_gnomevfssink_details);
|
|
|
|
}
|
|
|
|
|
2001-12-23 16:42:33 +00:00
|
|
|
static void
|
|
|
|
gst_gnomevfssink_class_init (GstGnomeVFSSinkClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass*)klass;
|
|
|
|
gstelement_class = (GstElementClass*)klass;
|
|
|
|
|
|
|
|
parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
|
|
|
|
|
|
|
|
|
2002-03-18 04:41:34 +00:00
|
|
|
gst_element_class_install_std_props (
|
2001-12-23 16:42:33 +00:00
|
|
|
GST_ELEMENT_CLASS (klass),
|
|
|
|
"location", ARG_LOCATION, G_PARAM_READWRITE,
|
|
|
|
NULL);
|
|
|
|
|
2003-12-23 21:28:35 +00:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
ARG_HANDLE,
|
|
|
|
g_param_spec_pointer ("handle",
|
|
|
|
"GnomeVFSHandle",
|
|
|
|
"Handle for GnomeVFS",
|
|
|
|
G_PARAM_READWRITE));
|
|
|
|
|
2001-12-23 16:42:33 +00:00
|
|
|
gst_gnomevfssink_signals[SIGNAL_HANDOFF] =
|
|
|
|
g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GstGnomeVFSSinkClass, handoff), NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
|
|
|
|
|
|
|
|
gst_gnomevfssink_signals[SIGNAL_ERASE_ASK] =
|
|
|
|
g_signal_new ("erase-ask", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GstGnomeVFSSinkClass, erase_ask), NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
|
|
|
|
|
|
|
|
|
|
|
|
gobject_class->set_property = gst_gnomevfssink_set_property;
|
|
|
|
gobject_class->get_property = gst_gnomevfssink_get_property;
|
|
|
|
|
|
|
|
gstelement_class->change_state = gst_gnomevfssink_change_state;
|
|
|
|
|
|
|
|
/* gnome vfs engine init */
|
|
|
|
if (gnome_vfs_initialized() == FALSE)
|
|
|
|
gnome_vfs_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gnomevfssink_init (GstGnomeVFSSink *gnomevfssink)
|
|
|
|
{
|
|
|
|
GstPad *pad;
|
|
|
|
pad = gst_pad_new ("sink", GST_PAD_SINK);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (gnomevfssink), pad);
|
|
|
|
gst_pad_set_chain_function (pad, gst_gnomevfssink_chain);
|
|
|
|
|
|
|
|
gnomevfssink->filename = NULL;
|
|
|
|
gnomevfssink->uri = NULL;
|
|
|
|
gnomevfssink->handle = NULL;
|
|
|
|
gnomevfssink->erase = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gnomevfssink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GstGnomeVFSSink *sink;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
sink = GST_GNOMEVFSSINK (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_LOCATION:
|
|
|
|
if (sink->filename)
|
|
|
|
g_free (sink->filename);
|
|
|
|
if (sink->uri)
|
|
|
|
g_free (sink->uri);
|
|
|
|
sink->filename = g_strdup (g_value_get_string (value));
|
2003-12-23 21:28:35 +00:00
|
|
|
sink->uri = gnome_vfs_uri_new (sink->filename);
|
|
|
|
break;
|
|
|
|
case ARG_HANDLE:
|
|
|
|
sink->handle = g_value_get_pointer (value);
|
2001-12-23 16:42:33 +00:00
|
|
|
break;
|
|
|
|
case ARG_ERASE:
|
|
|
|
sink->erase = g_value_get_boolean (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gnomevfssink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GstGnomeVFSSink *sink;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail (GST_IS_GNOMEVFSSINK (object));
|
|
|
|
|
|
|
|
sink = GST_GNOMEVFSSINK (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_LOCATION:
|
|
|
|
g_value_set_string (value, sink->filename);
|
|
|
|
break;
|
2003-12-23 21:28:35 +00:00
|
|
|
case ARG_HANDLE:
|
|
|
|
g_value_set_pointer (value, sink->handle);
|
|
|
|
break;
|
2001-12-23 16:42:33 +00:00
|
|
|
case ARG_ERASE:
|
|
|
|
g_value_set_boolean (value, sink->erase);
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_gnomevfssink_open_file (GstGnomeVFSSink *sink)
|
|
|
|
{
|
|
|
|
GnomeVFSResult result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_GNOMEVFSSINK_OPEN), FALSE);
|
|
|
|
|
2003-12-23 21:28:35 +00:00
|
|
|
if (sink->filename) {
|
|
|
|
/* open the file */
|
|
|
|
result = gnome_vfs_create_uri(&(sink->handle), sink->uri,
|
|
|
|
GNOME_VFS_OPEN_WRITE, sink->erase,
|
|
|
|
GNOME_VFS_PERM_USER_READ | GNOME_VFS_PERM_USER_WRITE
|
|
|
|
| GNOME_VFS_PERM_GROUP_READ);
|
|
|
|
GST_DEBUG ("open: %s", gnome_vfs_result_to_string(result));
|
|
|
|
if (result != GNOME_VFS_OK) {
|
|
|
|
if (sink->erase == FALSE) {
|
|
|
|
g_signal_emit (G_OBJECT (sink),
|
|
|
|
gst_gnomevfssink_signals[SIGNAL_ERASE_ASK], 0,
|
|
|
|
sink->erase);
|
|
|
|
}
|
2004-01-18 21:46:58 +00:00
|
|
|
gst_element_error (sink, RESOURCE, OPEN_WRITE,
|
|
|
|
(_("Error opening vfs file \"%s\""), sink->filename),
|
|
|
|
GST_ERROR_SYSTEM);
|
2003-12-23 21:28:35 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
g_return_val_if_fail (sink->handle != NULL, FALSE);
|
2001-12-23 16:42:33 +00:00
|
|
|
|
|
|
|
GST_FLAG_SET (sink, GST_GNOMEVFSSINK_OPEN);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gnomevfssink_close_file (GstGnomeVFSSink *sink)
|
|
|
|
{
|
|
|
|
GnomeVFSResult result;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_FLAG_IS_SET (sink, GST_GNOMEVFSSINK_OPEN));
|
|
|
|
|
2003-12-23 21:28:35 +00:00
|
|
|
if (sink->filename) {
|
|
|
|
/* close the file */
|
|
|
|
result = gnome_vfs_close(sink->handle);
|
2001-12-23 16:42:33 +00:00
|
|
|
|
2003-12-23 21:28:35 +00:00
|
|
|
if (result != GNOME_VFS_OK)
|
2004-01-18 21:46:58 +00:00
|
|
|
gst_element_error (sink, RESOURCE, CLOSE,
|
|
|
|
(_("Error closing file \"%s\""), sink->filename),
|
|
|
|
GST_ERROR_SYSTEM);
|
2001-12-23 16:42:33 +00:00
|
|
|
}
|
2003-12-23 21:28:35 +00:00
|
|
|
|
|
|
|
GST_FLAG_UNSET (sink, GST_GNOMEVFSSINK_OPEN);
|
2001-12-23 16:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_gnomevfssink_chain:
|
|
|
|
* @pad: the pad this gnomevfssink 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
|
|
|
|
*/
|
|
|
|
static void
|
2003-10-08 16:08:22 +00:00
|
|
|
gst_gnomevfssink_chain (GstPad *pad, GstData *_data)
|
2001-12-23 16:42:33 +00:00
|
|
|
{
|
2003-10-08 16:08:22 +00:00
|
|
|
GstBuffer *buf = GST_BUFFER (_data);
|
2001-12-23 16:42:33 +00:00
|
|
|
GstGnomeVFSSink *sink;
|
|
|
|
GnomeVFSResult result;
|
|
|
|
GnomeVFSFileSize bytes_written;
|
|
|
|
|
|
|
|
g_return_if_fail (pad != NULL);
|
|
|
|
g_return_if_fail (GST_IS_PAD (pad));
|
|
|
|
g_return_if_fail (buf != NULL);
|
|
|
|
|
|
|
|
sink = GST_GNOMEVFSSINK (gst_pad_get_parent (pad));
|
|
|
|
|
|
|
|
if (GST_FLAG_IS_SET (sink, GST_GNOMEVFSSINK_OPEN))
|
|
|
|
{
|
|
|
|
result = gnome_vfs_write(sink->handle, GST_BUFFER_DATA(buf), GST_BUFFER_SIZE (buf), &bytes_written);
|
2003-06-29 19:46:12 +00:00
|
|
|
GST_DEBUG ("write: %s, written_bytes: %" G_GUINT64_FORMAT, gnome_vfs_result_to_string(result), bytes_written);
|
2001-12-23 16:42:33 +00:00
|
|
|
if (bytes_written < GST_BUFFER_SIZE (buf))
|
|
|
|
{
|
2003-02-02 05:26:29 +00:00
|
|
|
printf ("gnomevfssink : Warning : %d bytes should be written, only %" G_GUINT64_FORMAT " bytes written\n",
|
2001-12-23 16:42:33 +00:00
|
|
|
GST_BUFFER_SIZE (buf), bytes_written);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
|
|
|
|
g_signal_emit (G_OBJECT (sink), gst_gnomevfssink_signals[SIGNAL_HANDOFF], 0,
|
|
|
|
sink);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstElementStateReturn
|
|
|
|
gst_gnomevfssink_change_state (GstElement *element)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_GNOMEVFSSINK (element), GST_STATE_FAILURE);
|
|
|
|
|
|
|
|
if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
|
|
|
|
if (GST_FLAG_IS_SET (element, GST_GNOMEVFSSINK_OPEN))
|
|
|
|
gst_gnomevfssink_close_file (GST_GNOMEVFSSINK (element));
|
|
|
|
} else {
|
|
|
|
if (!GST_FLAG_IS_SET (element, GST_GNOMEVFSSINK_OPEN)) {
|
|
|
|
if (!gst_gnomevfssink_open_file (GST_GNOMEVFSSINK (element)))
|
|
|
|
return GST_STATE_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
|
|
|
return GST_ELEMENT_CLASS (parent_class)->change_state (element);
|
|
|
|
|
|
|
|
return GST_STATE_SUCCESS;
|
|
|
|
}
|
|
|
|
|