|
Packit |
971217 |
/* GStreamer
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
|
|
Packit |
971217 |
* Copyright (C) 2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* This library is free software; you can redistribute it and/or
|
|
Packit |
971217 |
* modify it under the terms of the GNU Library General Public
|
|
Packit |
971217 |
* License as published by the Free Software Foundation; either
|
|
Packit |
971217 |
* version 2 of the License, or (at your option) any later version.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
971217 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
971217 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
971217 |
* Library General Public License for more details.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* You should have received a copy of the GNU Library General Public
|
|
Packit |
971217 |
* License along with this library; if not, write to the
|
|
Packit |
971217 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
Packit |
971217 |
* Boston, MA 02110-1301, USA.
|
|
Packit |
971217 |
*/
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/**
|
|
Packit |
971217 |
* SECTION:element-giosrc
|
|
Packit |
971217 |
* @title: giosrc
|
|
Packit |
971217 |
* @see_also: #GstFileSrc, #GstGnomeVFSSrc, #GstGioSink
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* This plugin reads data from a local or remote location specified
|
|
Packit |
971217 |
* by an URI. This location can be specified using any protocol supported by
|
|
Packit |
971217 |
* the GIO library or it's VFS backends. Common protocols are 'file', 'http',
|
|
Packit |
971217 |
* 'ftp', or 'smb'.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* If an URI or #GFile is not mounted giosrc will post a message of type
|
|
Packit |
971217 |
* %GST_MESSAGE_ELEMENT with name "not-mounted" on the bus. The message
|
|
Packit |
971217 |
* also contains the #GFile and the corresponding URI.
|
|
Packit |
971217 |
* Applications can use the "not-mounted" message to mount the #GFile
|
|
Packit |
971217 |
* by calling g_file_mount_enclosing_volume() and then restart the
|
|
Packit |
971217 |
* pipeline after the mounting has succeeded. Note that right after the
|
|
Packit |
971217 |
* "not-mounted" message a normal error message is posted on the bus which
|
|
Packit |
971217 |
* should be ignored if "not-mounted" is handled by the application, for
|
|
Packit |
971217 |
* example by calling gst_bus_set_flushing(bus, TRUE) after the "not-mounted"
|
|
Packit |
971217 |
* message was received and gst_bus_set_flushing(bus, FALSE) after the
|
|
Packit |
971217 |
* mounting was successful.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* ## Example launch lines
|
|
Packit |
971217 |
* |[
|
|
Packit |
971217 |
* gst-launch-1.0 -v giosrc location=file:///home/joe/foo.xyz ! fakesink
|
|
Packit |
971217 |
* ]|
|
|
Packit |
971217 |
* The above pipeline will simply read a local file and do nothing with the
|
|
Packit |
971217 |
* data read. Instead of giosrc, we could just as well have used the
|
|
Packit |
971217 |
* filesrc element here.
|
|
Packit |
971217 |
* |[
|
|
Packit |
971217 |
* gst-launch-1.0 -v giosrc location=smb://othercomputer/foo.xyz ! filesink location=/home/joe/foo.xyz
|
|
Packit |
971217 |
* ]|
|
|
Packit |
971217 |
* The above pipeline will copy a file from a remote host to the local file
|
|
Packit |
971217 |
* system using the Samba protocol.
|
|
Packit |
971217 |
* |[
|
|
Packit |
971217 |
* gst-launch-1.0 -v giosrc location=smb://othercomputer/demo.mp3 ! decodebin ! audioconvert ! audioresample ! autoaudiosink
|
|
Packit |
971217 |
* ]|
|
|
Packit |
971217 |
* The above pipeline will read and decode and play an mp3 file from a
|
|
Packit |
971217 |
* SAMBA server.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
*/
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* FIXME: We would like to mount the enclosing volume of an URL
|
|
Packit |
971217 |
* if it isn't mounted yet but this is possible async-only.
|
|
Packit |
971217 |
* Unfortunately this requires a running main loop from the
|
|
Packit |
971217 |
* default context and we can't guarantuee this!
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* We would also like to do authentication while mounting.
|
|
Packit |
971217 |
*/
|
|
Packit |
971217 |
|
|
Packit |
971217 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
971217 |
#include <config.h>
|
|
Packit |
971217 |
#endif
|
|
Packit |
971217 |
|
|
Packit |
971217 |
#include "gstgiosrc.h"
|
|
Packit |
971217 |
#include <string.h>
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_DEBUG_CATEGORY_STATIC (gst_gio_src_debug);
|
|
Packit |
971217 |
#define GST_CAT_DEFAULT gst_gio_src_debug
|
|
Packit |
971217 |
|
|
Packit |
971217 |
enum
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
PROP_0,
|
|
Packit |
971217 |
PROP_LOCATION,
|
|
Packit |
971217 |
PROP_FILE
|
|
Packit |
971217 |
};
|
|
Packit |
971217 |
|
|
Packit |
971217 |
#define gst_gio_src_parent_class parent_class
|
|
Packit |
971217 |
G_DEFINE_TYPE_WITH_CODE (GstGioSrc, gst_gio_src,
|
|
Packit |
971217 |
GST_TYPE_GIO_BASE_SRC, gst_gio_uri_handler_do_init (g_define_type_id));
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static void gst_gio_src_finalize (GObject * object);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static void gst_gio_src_set_property (GObject * object, guint prop_id,
|
|
Packit |
971217 |
const GValue * value, GParamSpec * pspec);
|
|
Packit |
971217 |
static void gst_gio_src_get_property (GObject * object, guint prop_id,
|
|
Packit |
971217 |
GValue * value, GParamSpec * pspec);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static GInputStream *gst_gio_src_get_stream (GstGioBaseSrc * bsrc);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static gboolean gst_gio_src_query (GstBaseSrc * base_src, GstQuery * query);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static void
|
|
Packit |
971217 |
gst_gio_src_class_init (GstGioSrcClass * klass)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
GObjectClass *gobject_class = (GObjectClass *) klass;
|
|
Packit |
971217 |
GstElementClass *gstelement_class = (GstElementClass *) klass;
|
|
Packit |
971217 |
GstBaseSrcClass *gstbasesrc_class = (GstBaseSrcClass *) klass;
|
|
Packit |
971217 |
GstGioBaseSrcClass *gstgiobasesrc_class = (GstGioBaseSrcClass *) klass;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_DEBUG_CATEGORY_INIT (gst_gio_src_debug, "gio_src", 0, "GIO source");
|
|
Packit |
971217 |
|
|
Packit |
971217 |
gobject_class->finalize = gst_gio_src_finalize;
|
|
Packit |
971217 |
gobject_class->set_property = gst_gio_src_set_property;
|
|
Packit |
971217 |
gobject_class->get_property = gst_gio_src_get_property;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
g_object_class_install_property (gobject_class, PROP_LOCATION,
|
|
Packit |
971217 |
g_param_spec_string ("location", "Location", "URI location to read from",
|
|
Packit |
971217 |
NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/**
|
|
Packit |
971217 |
* GstGioSrc:file:
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* %GFile to read from.
|
|
Packit |
971217 |
*/
|
|
Packit |
971217 |
g_object_class_install_property (gobject_class, PROP_FILE,
|
|
Packit |
971217 |
g_param_spec_object ("file", "File", "GFile to read from",
|
|
Packit |
971217 |
G_TYPE_FILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
Packit |
971217 |
|
|
Packit |
971217 |
gst_element_class_set_static_metadata (gstelement_class, "GIO source",
|
|
Packit |
971217 |
"Source/File",
|
|
Packit |
971217 |
"Read from any GIO-supported location",
|
|
Packit |
971217 |
"Ren\xc3\xa9 Stadler <mail@renestadler.de>, "
|
|
Packit |
971217 |
"Sebastian Dröge <sebastian.droege@collabora.co.uk>");
|
|
Packit |
971217 |
|
|
Packit |
971217 |
gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_gio_src_query);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
gstgiobasesrc_class->get_stream = GST_DEBUG_FUNCPTR (gst_gio_src_get_stream);
|
|
Packit |
971217 |
gstgiobasesrc_class->close_on_stop = TRUE;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static void
|
|
Packit |
971217 |
gst_gio_src_init (GstGioSrc * src)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static void
|
|
Packit |
971217 |
gst_gio_src_finalize (GObject * object)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
GstGioSrc *src = GST_GIO_SRC (object);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (src->file) {
|
|
Packit |
971217 |
g_object_unref (src->file);
|
|
Packit |
971217 |
src->file = NULL;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static void
|
|
Packit |
971217 |
gst_gio_src_set_property (GObject * object, guint prop_id,
|
|
Packit |
971217 |
const GValue * value, GParamSpec * pspec)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
GstGioSrc *src = GST_GIO_SRC (object);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
switch (prop_id) {
|
|
Packit |
971217 |
case PROP_LOCATION:{
|
|
Packit |
971217 |
const gchar *uri = NULL;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (GST_STATE (src) == GST_STATE_PLAYING ||
|
|
Packit |
971217 |
GST_STATE (src) == GST_STATE_PAUSED) {
|
|
Packit |
971217 |
GST_WARNING
|
|
Packit |
971217 |
("Setting a new location or GFile not supported in PLAYING or PAUSED state");
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_OBJECT_LOCK (GST_OBJECT (src));
|
|
Packit |
971217 |
if (src->file)
|
|
Packit |
971217 |
g_object_unref (src->file);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
uri = g_value_get_string (value);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (uri) {
|
|
Packit |
971217 |
src->file = g_file_new_for_uri (uri);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (!src->file) {
|
|
Packit |
971217 |
GST_ERROR ("Could not create GFile for URI '%s'", uri);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
} else {
|
|
Packit |
971217 |
src->file = NULL;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
GST_OBJECT_UNLOCK (GST_OBJECT (src));
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
case PROP_FILE:
|
|
Packit |
971217 |
if (GST_STATE (src) == GST_STATE_PLAYING ||
|
|
Packit |
971217 |
GST_STATE (src) == GST_STATE_PAUSED) {
|
|
Packit |
971217 |
GST_WARNING
|
|
Packit |
971217 |
("Setting a new location or GFile not supported in PLAYING or PAUSED state");
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_OBJECT_LOCK (GST_OBJECT (src));
|
|
Packit |
971217 |
if (src->file)
|
|
Packit |
971217 |
g_object_unref (src->file);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
src->file = g_value_dup_object (value);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_OBJECT_UNLOCK (GST_OBJECT (src));
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
default:
|
|
Packit |
971217 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static void
|
|
Packit |
971217 |
gst_gio_src_get_property (GObject * object, guint prop_id,
|
|
Packit |
971217 |
GValue * value, GParamSpec * pspec)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
GstGioSrc *src = GST_GIO_SRC (object);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
switch (prop_id) {
|
|
Packit |
971217 |
case PROP_LOCATION:{
|
|
Packit |
971217 |
gchar *uri;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_OBJECT_LOCK (GST_OBJECT (src));
|
|
Packit |
971217 |
if (src->file) {
|
|
Packit |
971217 |
uri = g_file_get_uri (src->file);
|
|
Packit |
971217 |
g_value_set_string (value, uri);
|
|
Packit |
971217 |
g_free (uri);
|
|
Packit |
971217 |
} else {
|
|
Packit |
971217 |
g_value_set_string (value, NULL);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
GST_OBJECT_UNLOCK (GST_OBJECT (src));
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
case PROP_FILE:
|
|
Packit |
971217 |
GST_OBJECT_LOCK (GST_OBJECT (src));
|
|
Packit |
971217 |
g_value_set_object (value, src->file);
|
|
Packit |
971217 |
GST_OBJECT_UNLOCK (GST_OBJECT (src));
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
default:
|
|
Packit |
971217 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static gboolean
|
|
Packit |
971217 |
gst_gio_src_query (GstBaseSrc * base_src, GstQuery * query)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
gboolean res;
|
|
Packit |
971217 |
GstGioSrc *src = GST_GIO_SRC (base_src);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
switch (GST_QUERY_TYPE (query)) {
|
|
Packit |
971217 |
case GST_QUERY_SCHEDULING:
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
gchar *scheme;
|
|
Packit |
971217 |
GstSchedulingFlags flags;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
flags = 0;
|
|
Packit |
971217 |
if (src->file == NULL)
|
|
Packit |
971217 |
goto forward_parent;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
scheme = g_file_get_uri_scheme (src->file);
|
|
Packit |
971217 |
if (scheme == NULL)
|
|
Packit |
971217 |
goto forward_parent;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (strcmp (scheme, "file") == 0) {
|
|
Packit |
971217 |
GST_LOG_OBJECT (src, "local URI, assuming random access is possible");
|
|
Packit |
971217 |
flags |= GST_SCHEDULING_FLAG_SEEKABLE;
|
|
Packit |
971217 |
} else if (strcmp (scheme, "http") == 0 || strcmp (scheme, "https") == 0) {
|
|
Packit |
971217 |
GST_LOG_OBJECT (src, "blacklisted protocol '%s', "
|
|
Packit |
971217 |
"no random access possible", scheme);
|
|
Packit |
971217 |
} else {
|
|
Packit |
971217 |
GST_LOG_OBJECT (src, "unhandled protocol '%s', asking parent", scheme);
|
|
Packit |
971217 |
goto forward_parent;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
g_free (scheme);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
gst_query_set_scheduling (query, flags, 1, -1, 0);
|
|
Packit |
971217 |
gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
|
|
Packit |
971217 |
if (flags & GST_SCHEDULING_FLAG_SEEKABLE)
|
|
Packit |
971217 |
gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
res = TRUE;
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
default:
|
|
Packit |
971217 |
forward_parent:
|
|
Packit |
971217 |
res = GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_SRC_CLASS,
|
|
Packit |
971217 |
query, (base_src, query), FALSE);
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
return res;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static GInputStream *
|
|
Packit |
971217 |
gst_gio_src_get_stream (GstGioBaseSrc * bsrc)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
GstGioSrc *src = GST_GIO_SRC (bsrc);
|
|
Packit |
971217 |
GError *err = NULL;
|
|
Packit |
971217 |
GInputStream *stream;
|
|
Packit |
971217 |
GCancellable *cancel = bsrc->cancel;
|
|
Packit |
971217 |
gchar *uri = NULL;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (src->file == NULL) {
|
|
Packit |
971217 |
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
|
|
Packit |
971217 |
("No location or GFile given"));
|
|
Packit |
971217 |
return NULL;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
uri = g_file_get_uri (src->file);
|
|
Packit |
971217 |
if (!uri)
|
|
Packit |
971217 |
uri = g_strdup ("(null)");
|
|
Packit |
971217 |
|
|
Packit |
971217 |
stream = G_INPUT_STREAM (g_file_read (src->file, cancel, &err));
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (stream == NULL && !gst_gio_error (src, "g_file_read", &err, NULL)) {
|
|
Packit |
971217 |
if (GST_GIO_ERROR_MATCHES (err, NOT_FOUND)) {
|
|
Packit |
971217 |
GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
|
|
Packit |
971217 |
("Could not open location %s for reading: %s", uri, err->message));
|
|
Packit |
971217 |
} else if (GST_GIO_ERROR_MATCHES (err, NOT_MOUNTED)) {
|
|
Packit |
971217 |
gst_element_post_message (GST_ELEMENT_CAST (src),
|
|
Packit |
971217 |
gst_message_new_element (GST_OBJECT_CAST (src),
|
|
Packit |
971217 |
gst_structure_new ("not-mounted", "file", G_TYPE_FILE, src->file,
|
|
Packit |
971217 |
"uri", G_TYPE_STRING, uri, NULL)));
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
|
|
Packit |
971217 |
("Location %s not mounted: %s", uri, err->message));
|
|
Packit |
971217 |
} else {
|
|
Packit |
971217 |
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
|
|
Packit |
971217 |
("Could not open location %s for reading: %s", uri, err->message));
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
g_free (uri);
|
|
Packit |
971217 |
g_clear_error (&err;;
|
|
Packit |
971217 |
return NULL;
|
|
Packit |
971217 |
} else if (stream == NULL) {
|
|
Packit |
971217 |
g_free (uri);
|
|
Packit |
971217 |
return NULL;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_DEBUG_OBJECT (src, "opened location %s", uri);
|
|
Packit |
971217 |
g_free (uri);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
return stream;
|
|
Packit |
971217 |
}
|