Blame gst/gio/gstgiosink.c

Packit 0652a1
/* GStreamer
Packit 0652a1
 *
Packit 0652a1
 * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
Packit 0652a1
 * Copyright (C) 2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
Packit 0652a1
 * 
Packit 0652a1
 * This library is free software; you can redistribute it and/or
Packit 0652a1
 * modify it under the terms of the GNU Library General Public
Packit 0652a1
 * License as published by the Free Software Foundation; either
Packit 0652a1
 * version 2 of the License, or (at your option) any later version.
Packit 0652a1
 *
Packit 0652a1
 * This library is distributed in the hope that it will be useful,
Packit 0652a1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0652a1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0652a1
 * Library General Public License for more details.
Packit 0652a1
 *
Packit 0652a1
 * You should have received a copy of the GNU Library General Public
Packit 0652a1
 * License along with this library; if not, write to the
Packit 0652a1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 0652a1
 * Boston, MA 02110-1301, USA.
Packit 0652a1
 */
Packit 0652a1
Packit 0652a1
/**
Packit 0652a1
 * SECTION:element-giosink
Packit 0652a1
 * @title: giosink
Packit 0652a1
 * @see_also: #GstFileSink, #GstGnomeVFSSink, #GstGioSrc
Packit 0652a1
 *
Packit 0652a1
 * This plugin writes incoming data to a local or remote location specified
Packit 0652a1
 * by an URI. This location can be specified using any protocol supported by
Packit 0652a1
 * the GIO library or it's VFS backends. Common protocols are 'file', 'ftp',
Packit 0652a1
 * or 'smb'.
Packit 0652a1
 *
Packit 0652a1
 * If the URI or #GFile already exists giosink will post a message of
Packit 0652a1
 * type %GST_MESSAGE_ELEMENT with name "file-exists" on the bus. The message
Packit 0652a1
 * also contains the #GFile and the corresponding URI.
Packit 0652a1
 * Applications can use the "file-exists" message to notify the user about
Packit 0652a1
 * the problem and to set a different target location or to remove the
Packit 0652a1
 * existing file. Note that right after the "file-exists" message a normal
Packit 0652a1
 * error message is posted on the bus which should be ignored if "file-exists"
Packit 0652a1
 * is handled by the application, for example by calling
Packit 0652a1
 * gst_bus_set_flushing(bus, TRUE) after the "file-exists" message was
Packit 0652a1
 * received and gst_bus_set_flushing(bus, FALSE) after the problem is
Packit 0652a1
 * resolved.
Packit 0652a1
 *
Packit 0652a1
 * Similar to the "file-exist" message a "not-mounted" message is posted
Packit 0652a1
 * on the bus if the target location is not mounted yet and needs to be
Packit 0652a1
 * mounted. This message can be used by application to mount the location
Packit 0652a1
 * and retry after the location was mounted successfully.
Packit 0652a1
 *
Packit 0652a1
 * ## Example pipelines
Packit 0652a1
 * |[
Packit 0652a1
 * gst-launch-1.0 -v filesrc location=input.xyz ! giosink location=file:///home/joe/out.xyz
Packit 0652a1
 * ]|
Packit 0652a1
 *  The above pipeline will simply copy a local file. Instead of giosink,
Packit 0652a1
 * we could just as well have used the filesink element here.
Packit 0652a1
 * |[
Packit 0652a1
 * gst-launch-1.0 -v uridecodebin uri=file:///path/to/audio.file ! audioconvert ! flacenc ! giosink location=smb://othercomputer/foo.flac
Packit 0652a1
 * ]|
Packit 0652a1
 *  The above pipeline will re-encode an audio file into FLAC format and store
Packit 0652a1
 * it on a remote host using the Samba protocol.
Packit 0652a1
 * |[
Packit 0652a1
 * gst-launch-1.0 -v audiotestsrc num-buffers=100 ! vorbisenc ! oggmux ! giosink location=file:///home/foo/bar.ogg
Packit 0652a1
 * ]|
Packit 0652a1
 *  The above pipeline will encode a 440Hz sine wave to Ogg Vorbis and stores
Packit 0652a1
 * it in the home directory of user foo.
Packit 0652a1
 *
Packit 0652a1
 */
Packit 0652a1
Packit 0652a1
/* FIXME: We would like to mount the enclosing volume of an URL
Packit 0652a1
 *        if it isn't mounted yet but this is possible async-only.
Packit 0652a1
 *        Unfortunately this requires a running main loop from the
Packit 0652a1
 *        default context and we can't guarantee this!
Packit 0652a1
 *
Packit 0652a1
 *        We would also like to do authentication while mounting.
Packit 0652a1
 */
Packit 0652a1
Packit 0652a1
#ifdef HAVE_CONFIG_H
Packit 0652a1
#include <config.h>
Packit 0652a1
#endif
Packit 0652a1
Packit 0652a1
#include "gstgiosink.h"
Packit 0652a1
Packit 0652a1
GST_DEBUG_CATEGORY_STATIC (gst_gio_sink_debug);
Packit 0652a1
#define GST_CAT_DEFAULT gst_gio_sink_debug
Packit 0652a1
Packit 0652a1
/* Filter signals and args */
Packit 0652a1
enum
Packit 0652a1
{
Packit 0652a1
  LAST_SIGNAL
Packit 0652a1
};
Packit 0652a1
Packit 0652a1
enum
Packit 0652a1
{
Packit 0652a1
  PROP_0,
Packit 0652a1
  PROP_LOCATION,
Packit 0652a1
  PROP_FILE
Packit 0652a1
};
Packit 0652a1
Packit 0652a1
#define gst_gio_sink_parent_class parent_class
Packit 0652a1
G_DEFINE_TYPE_WITH_CODE (GstGioSink, gst_gio_sink, GST_TYPE_GIO_BASE_SINK,
Packit 0652a1
    gst_gio_uri_handler_do_init (g_define_type_id));
Packit 0652a1
Packit 0652a1
static void gst_gio_sink_finalize (GObject * object);
Packit 0652a1
static void gst_gio_sink_set_property (GObject * object, guint prop_id,
Packit 0652a1
    const GValue * value, GParamSpec * pspec);
Packit 0652a1
static void gst_gio_sink_get_property (GObject * object, guint prop_id,
Packit 0652a1
    GValue * value, GParamSpec * pspec);
Packit 0652a1
static GOutputStream *gst_gio_sink_get_stream (GstGioBaseSink * base_sink);
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_gio_sink_class_init (GstGioSinkClass * klass)
Packit 0652a1
{
Packit 0652a1
  GObjectClass *gobject_class = (GObjectClass *) klass;
Packit 0652a1
  GstElementClass *gstelement_class = (GstElementClass *) klass;
Packit 0652a1
  GstGioBaseSinkClass *gstgiobasesink_class = (GstGioBaseSinkClass *) klass;
Packit 0652a1
Packit 0652a1
  GST_DEBUG_CATEGORY_INIT (gst_gio_sink_debug, "gio_sink", 0, "GIO sink");
Packit 0652a1
Packit 0652a1
  gobject_class->finalize = gst_gio_sink_finalize;
Packit 0652a1
  gobject_class->set_property = gst_gio_sink_set_property;
Packit 0652a1
  gobject_class->get_property = gst_gio_sink_get_property;
Packit 0652a1
Packit 0652a1
  g_object_class_install_property (gobject_class, PROP_LOCATION,
Packit 0652a1
      g_param_spec_string ("location", "Location", "URI location to write to",
Packit 0652a1
          NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Packit 0652a1
Packit 0652a1
  /**
Packit 0652a1
   * GstGioSink:file:
Packit 0652a1
   *
Packit 0652a1
   * %GFile to write to.
Packit 0652a1
   */
Packit 0652a1
  g_object_class_install_property (gobject_class, PROP_FILE,
Packit 0652a1
      g_param_spec_object ("file", "File", "GFile to write to",
Packit 0652a1
          G_TYPE_FILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Packit 0652a1
Packit 0652a1
  gst_element_class_set_static_metadata (gstelement_class, "GIO sink",
Packit 0652a1
      "Sink/File",
Packit 0652a1
      "Write to any GIO-supported location",
Packit 0652a1
      "Ren\xc3\xa9 Stadler <mail@renestadler.de>, "
Packit 0652a1
      "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
Packit 0652a1
Packit 0652a1
  gstgiobasesink_class->get_stream =
Packit 0652a1
      GST_DEBUG_FUNCPTR (gst_gio_sink_get_stream);
Packit 0652a1
  gstgiobasesink_class->close_on_stop = TRUE;
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_gio_sink_init (GstGioSink * sink)
Packit 0652a1
{
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_gio_sink_finalize (GObject * object)
Packit 0652a1
{
Packit 0652a1
  GstGioSink *sink = GST_GIO_SINK (object);
Packit 0652a1
Packit 0652a1
  if (sink->file) {
Packit 0652a1
    g_object_unref (sink->file);
Packit 0652a1
    sink->file = NULL;
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_gio_sink_set_property (GObject * object, guint prop_id,
Packit 0652a1
    const GValue * value, GParamSpec * pspec)
Packit 0652a1
{
Packit 0652a1
  GstGioSink *sink = GST_GIO_SINK (object);
Packit 0652a1
Packit 0652a1
  switch (prop_id) {
Packit 0652a1
    case PROP_LOCATION:{
Packit 0652a1
      const gchar *uri = NULL;
Packit 0652a1
Packit 0652a1
      if (GST_STATE (sink) == GST_STATE_PLAYING ||
Packit 0652a1
          GST_STATE (sink) == GST_STATE_PAUSED) {
Packit 0652a1
        GST_WARNING
Packit 0652a1
            ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
Packit 0652a1
        break;
Packit 0652a1
      }
Packit 0652a1
Packit 0652a1
      GST_OBJECT_LOCK (GST_OBJECT (sink));
Packit 0652a1
      if (sink->file)
Packit 0652a1
        g_object_unref (sink->file);
Packit 0652a1
Packit 0652a1
      uri = g_value_get_string (value);
Packit 0652a1
Packit 0652a1
      if (uri) {
Packit 0652a1
        sink->file = g_file_new_for_uri (uri);
Packit 0652a1
Packit 0652a1
        if (!sink->file) {
Packit 0652a1
          GST_ERROR ("Could not create GFile for URI '%s'", uri);
Packit 0652a1
        }
Packit 0652a1
      } else {
Packit 0652a1
        sink->file = NULL;
Packit 0652a1
      }
Packit 0652a1
      GST_OBJECT_UNLOCK (GST_OBJECT (sink));
Packit 0652a1
      break;
Packit 0652a1
    }
Packit 0652a1
    case PROP_FILE:
Packit 0652a1
      if (GST_STATE (sink) == GST_STATE_PLAYING ||
Packit 0652a1
          GST_STATE (sink) == GST_STATE_PAUSED) {
Packit 0652a1
        GST_WARNING
Packit 0652a1
            ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
Packit 0652a1
        break;
Packit 0652a1
      }
Packit 0652a1
Packit 0652a1
      GST_OBJECT_LOCK (GST_OBJECT (sink));
Packit 0652a1
      if (sink->file)
Packit 0652a1
        g_object_unref (sink->file);
Packit 0652a1
Packit 0652a1
      sink->file = g_value_dup_object (value);
Packit 0652a1
Packit 0652a1
      GST_OBJECT_UNLOCK (GST_OBJECT (sink));
Packit 0652a1
      break;
Packit 0652a1
    default:
Packit 0652a1
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 0652a1
      break;
Packit 0652a1
  }
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_gio_sink_get_property (GObject * object, guint prop_id,
Packit 0652a1
    GValue * value, GParamSpec * pspec)
Packit 0652a1
{
Packit 0652a1
  GstGioSink *sink = GST_GIO_SINK (object);
Packit 0652a1
Packit 0652a1
  switch (prop_id) {
Packit 0652a1
    case PROP_LOCATION:{
Packit 0652a1
      gchar *uri;
Packit 0652a1
Packit 0652a1
      GST_OBJECT_LOCK (GST_OBJECT (sink));
Packit 0652a1
      if (sink->file) {
Packit 0652a1
        uri = g_file_get_uri (sink->file);
Packit 0652a1
        g_value_set_string (value, uri);
Packit 0652a1
        g_free (uri);
Packit 0652a1
      } else {
Packit 0652a1
        g_value_set_string (value, NULL);
Packit 0652a1
      }
Packit 0652a1
      GST_OBJECT_UNLOCK (GST_OBJECT (sink));
Packit 0652a1
      break;
Packit 0652a1
    }
Packit 0652a1
    case PROP_FILE:
Packit 0652a1
      GST_OBJECT_LOCK (GST_OBJECT (sink));
Packit 0652a1
      g_value_set_object (value, sink->file);
Packit 0652a1
      GST_OBJECT_UNLOCK (GST_OBJECT (sink));
Packit 0652a1
      break;
Packit 0652a1
    default:
Packit 0652a1
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 0652a1
      break;
Packit 0652a1
  }
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static GOutputStream *
Packit 0652a1
gst_gio_sink_get_stream (GstGioBaseSink * bsink)
Packit 0652a1
{
Packit 0652a1
  GstGioSink *sink = GST_GIO_SINK (bsink);
Packit 0652a1
  GOutputStream *stream;
Packit 0652a1
  GCancellable *cancel = GST_GIO_BASE_SINK (sink)->cancel;
Packit 0652a1
  GError *err = NULL;
Packit 0652a1
  gchar *uri;
Packit 0652a1
Packit 0652a1
  if (sink->file == NULL) {
Packit 0652a1
    GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
Packit 0652a1
        ("No location or GFile given"));
Packit 0652a1
    return NULL;
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  uri = g_file_get_uri (sink->file);
Packit 0652a1
  if (!uri)
Packit 0652a1
    uri = g_strdup ("(null)");
Packit 0652a1
Packit 0652a1
  stream =
Packit 0652a1
      G_OUTPUT_STREAM (g_file_create (sink->file, G_FILE_CREATE_NONE, cancel,
Packit 0652a1
          &err));
Packit 0652a1
Packit 0652a1
  if (!stream) {
Packit 0652a1
    if (!gst_gio_error (sink, "g_file_create", &err, NULL)) {
Packit 0652a1
      /*if (GST_GIO_ERROR_MATCHES (err, EXISTS)) */
Packit 0652a1
      /* FIXME: Retry with replace if overwrite == TRUE! */
Packit 0652a1
Packit 0652a1
      if (GST_GIO_ERROR_MATCHES (err, NOT_FOUND)) {
Packit 0652a1
        GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (NULL),
Packit 0652a1
            ("Could not open location %s for writing: %s", uri, err->message));
Packit 0652a1
      } else if (GST_GIO_ERROR_MATCHES (err, EXISTS)) {
Packit 0652a1
        gst_element_post_message (GST_ELEMENT_CAST (sink),
Packit 0652a1
            gst_message_new_element (GST_OBJECT_CAST (sink),
Packit 0652a1
                gst_structure_new ("file-exists", "file", G_TYPE_FILE,
Packit 0652a1
                    sink->file, "uri", G_TYPE_STRING, uri, NULL)));
Packit 0652a1
Packit 0652a1
        GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
Packit 0652a1
            ("Location %s already exists: %s", uri, err->message));
Packit 0652a1
      } else if (GST_GIO_ERROR_MATCHES (err, NOT_MOUNTED)) {
Packit 0652a1
        gst_element_post_message (GST_ELEMENT_CAST (sink),
Packit 0652a1
            gst_message_new_element (GST_OBJECT_CAST (sink),
Packit 0652a1
                gst_structure_new ("not-mounted", "file", G_TYPE_FILE,
Packit 0652a1
                    sink->file, "uri", G_TYPE_STRING, uri, NULL)));
Packit 0652a1
Packit 0652a1
        GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
Packit 0652a1
            ("Location %s not mounted: %s", uri, err->message));
Packit 0652a1
      } else {
Packit 0652a1
        GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
Packit 0652a1
            ("Could not open location %s for writing: %s", uri, err->message));
Packit 0652a1
      }
Packit 0652a1
Packit 0652a1
      g_clear_error (&err;;
Packit 0652a1
    }
Packit 0652a1
    g_free (uri);
Packit 0652a1
    return NULL;
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  GST_DEBUG_OBJECT (sink, "opened location %s", uri);
Packit 0652a1
Packit 0652a1
  g_free (uri);
Packit 0652a1
Packit 0652a1
  return stream;
Packit 0652a1
}