summaryrefslogtreecommitdiff
path: root/ext/curl
diff options
context:
space:
mode:
authorPatricia Muscalu <patricia@axis.com>2012-01-23 09:28:10 +0100
committerTim-Philipp Müller <tim.muller@collabora.co.uk>2012-05-12 11:53:01 +0100
commitf139656becf17dce39be0fe180796794aa3b8875 (patch)
treeee4b27bbb9c477262829ebd3cfdf76231d854954 /ext/curl
parentae39cbfa577adf48f50f11b67121670275480d92 (diff)
downloadgstreamer-plugins-bad-f139656becf17dce39be0fe180796794aa3b8875.tar.gz
curl: new curlfilesink element
https://bugzilla.gnome.org/show_bug.cgi?id=653741
Diffstat (limited to 'ext/curl')
-rw-r--r--ext/curl/Makefile.am6
-rw-r--r--ext/curl/gstcurl.c5
-rw-r--r--ext/curl/gstcurlfilesink.c233
-rw-r--r--ext/curl/gstcurlfilesink.h58
4 files changed, 300 insertions, 2 deletions
diff --git a/ext/curl/Makefile.am b/ext/curl/Makefile.am
index 6a0a156cd..fb2fe2ede 100644
--- a/ext/curl/Makefile.am
+++ b/ext/curl/Makefile.am
@@ -3,7 +3,8 @@ plugin_LTLIBRARIES = libgstcurl.la
libgstcurl_la_SOURCES = gstcurl.c \
gstcurlbasesink.c \
gstcurltlssink.c \
- gstcurlhttpsink.c
+ gstcurlhttpsink.c \
+ gstcurlfilesink.c
libgstcurl_la_CFLAGS = \
$(GST_PLUGINS_BAD_CFLAGS) \
$(GST_BASE_CFLAGS) \
@@ -19,4 +20,5 @@ libgstcurl_la_LIBTOOLFLAGS = --tag=disable-static
noinst_HEADERS = gstcurlbasesink.h \
gstcurltlssink.h \
- gstcurlhttpsink.h
+ gstcurlhttpsink.h \
+ gstcurlfilesink.h
diff --git a/ext/curl/gstcurl.c b/ext/curl/gstcurl.c
index c1dc8f48b..4bc96e9af 100644
--- a/ext/curl/gstcurl.c
+++ b/ext/curl/gstcurl.c
@@ -23,6 +23,7 @@
#include "gstcurlbasesink.h"
#include "gstcurltlssink.h"
#include "gstcurlhttpsink.h"
+#include "gstcurlfilesink.h"
static gboolean
plugin_init (GstPlugin * plugin)
@@ -32,6 +33,10 @@ plugin_init (GstPlugin * plugin)
GST_TYPE_CURL_HTTP_SINK))
return FALSE;
+ if (!gst_element_register (plugin, "curlfilesink", GST_RANK_NONE,
+ GST_TYPE_CURL_FILE_SINK))
+ return FALSE;
+
return TRUE;
}
diff --git a/ext/curl/gstcurlfilesink.c b/ext/curl/gstcurlfilesink.c
new file mode 100644
index 000000000..287e507f4
--- /dev/null
+++ b/ext/curl/gstcurlfilesink.c
@@ -0,0 +1,233 @@
+/* GStreamer
+ * Copyright (C) 2011 Axis Communications <dev-gstreamer@axis.com>
+ *
+ * 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-curlfilesink
+ * @short_description: sink that uploads data to a server using libcurl
+ * @see_also:
+ *
+ * This is a network sink that uses libcurl as a client to upload data to
+ * a local or network drive.
+ *
+ * <refsect2>
+ * <title>Example launch line (upload a JPEG file to /home/test/images directory)</title>
+ * |[
+ * gst-launch filesrc location=image.jpg ! jpegparse ! curlfilesink \
+ * file-name=image.jpg \
+ * location=file:///home/test/images/
+ * ]|
+ * </refsect2>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <curl/curl.h>
+#include <string.h>
+#include <stdio.h>
+
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <unistd.h>
+#include <netinet/ip.h>
+#include <netinet/tcp.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "gstcurlbasesink.h"
+#include "gstcurlfilesink.h"
+
+/* Default values */
+#define GST_CAT_DEFAULT gst_curl_file_sink_debug
+
+
+/* Plugin specific settings */
+
+GST_DEBUG_CATEGORY_STATIC (gst_curl_file_sink_debug);
+
+enum
+{
+ PROP_0,
+ PROP_CREATE_DIRS
+};
+
+
+/* Object class function declarations */
+
+
+/* private functions */
+static void gst_curl_file_sink_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * pspec);
+static void gst_curl_file_sink_get_property (GObject * object, guint prop_id,
+ GValue * value, GParamSpec * pspec);
+
+static gboolean set_file_options_unlocked (GstCurlBaseSink * curlbasesink);
+static gboolean set_file_dynamic_options_unlocked
+ (GstCurlBaseSink * curlbasesink);
+static gboolean gst_curl_file_sink_prepare_transfer (GstCurlBaseSink *
+ curlbasesink);
+
+#define gst_curl_file_sink_parent_class parent_class
+G_DEFINE_TYPE (GstCurlFileSink, gst_curl_file_sink, GST_TYPE_CURL_BASE_SINK);
+
+static void
+gst_curl_file_sink_class_init (GstCurlFileSinkClass * klass)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+ GstCurlBaseSinkClass *gstcurlbasesink_class = (GstCurlBaseSinkClass *) klass;
+ GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
+
+ GST_DEBUG_CATEGORY_INIT (gst_curl_file_sink_debug, "curlfilesink", 0,
+ "curl file sink element");
+ GST_DEBUG_OBJECT (klass, "class_init");
+
+ gst_element_class_set_details_simple (element_class,
+ "Curl file sink",
+ "Sink/Network",
+ "Upload data over FILE protocol using libcurl",
+ "Patricia Muscalu <patricia@axis.com>");
+
+ gobject_class->set_property = gst_curl_file_sink_set_property;
+ gobject_class->get_property = gst_curl_file_sink_get_property;
+
+ gstcurlbasesink_class->set_protocol_dynamic_options_unlocked =
+ set_file_dynamic_options_unlocked;
+ gstcurlbasesink_class->set_options_unlocked = set_file_options_unlocked;
+ gstcurlbasesink_class->prepare_transfer = gst_curl_file_sink_prepare_transfer;
+
+ g_object_class_install_property (gobject_class, PROP_CREATE_DIRS,
+ g_param_spec_boolean ("create-dirs", "Create missing directories",
+ "Attempt to create missing directory included in the path",
+ FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+}
+
+static void
+gst_curl_file_sink_init (GstCurlFileSink * sink)
+{
+}
+
+static void
+gst_curl_file_sink_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * pspec)
+{
+ GstCurlFileSink *sink;
+ GstState cur_state;
+
+ g_return_if_fail (GST_IS_CURL_FILE_SINK (object));
+ sink = GST_CURL_FILE_SINK (object);
+
+ gst_element_get_state (GST_ELEMENT (sink), &cur_state, NULL, 0);
+ if (cur_state != GST_STATE_PLAYING && cur_state != GST_STATE_PAUSED) {
+ GST_OBJECT_LOCK (sink);
+
+ switch (prop_id) {
+ case PROP_CREATE_DIRS:
+ sink->create_dirs = g_value_get_boolean (value);
+ GST_DEBUG_OBJECT (sink, "create-dirs set to %d", sink->create_dirs);
+ break;
+
+ default:
+ GST_DEBUG_OBJECT (sink, "invalid property id %d", prop_id);
+ break;
+ }
+
+ GST_OBJECT_UNLOCK (sink);
+ }
+}
+
+static void
+gst_curl_file_sink_get_property (GObject * object, guint prop_id,
+ GValue * value, GParamSpec * pspec)
+{
+ GstCurlFileSink *sink;
+
+ g_return_if_fail (GST_IS_CURL_FILE_SINK (object));
+ sink = GST_CURL_FILE_SINK (object);
+
+ switch (prop_id) {
+ case PROP_CREATE_DIRS:
+ g_value_set_boolean (value, sink->create_dirs);
+ break;
+ default:
+ GST_DEBUG_OBJECT (sink, "invalid property id");
+ break;
+ }
+}
+
+static gboolean
+set_file_dynamic_options_unlocked (GstCurlBaseSink * basesink)
+{
+ gchar *tmp = g_strdup_printf ("%s%s", basesink->url, basesink->file_name);
+
+ curl_easy_setopt (basesink->curl, CURLOPT_URL, tmp);
+
+ g_free (tmp);
+
+ return TRUE;
+}
+
+static gboolean
+set_file_options_unlocked (GstCurlBaseSink * basesink)
+{
+ curl_easy_setopt (basesink->curl, CURLOPT_UPLOAD, 1L);
+
+ return TRUE;
+}
+
+static gboolean
+gst_curl_file_sink_prepare_transfer (GstCurlBaseSink * basesink)
+{
+ GstCurlFileSink *sink = GST_CURL_FILE_SINK (basesink);
+
+ if (sink->create_dirs) {
+ gchar *file_name;
+ gchar *last_slash;
+
+ gchar *url = g_strdup_printf ("%s%s", basesink->url, basesink->file_name);
+ file_name = g_filename_from_uri (url, NULL, NULL);
+ if (file_name == NULL) {
+ GST_DEBUG_OBJECT (sink, "failed to parse file name of '%s'", url);
+ GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, ("failed to parse file name"),
+ (NULL));
+ g_free (url);
+ return FALSE;
+ }
+ g_free (url);
+
+ last_slash = strrchr (file_name, G_DIR_SEPARATOR);
+ if (last_slash != NULL) {
+ /* create dir if file name contains dir component */
+ gchar *dir_name = g_strndup (file_name, last_slash - file_name);
+ if (g_mkdir_with_parents (dir_name, S_IRWXU) < 0) {
+ GST_DEBUG_OBJECT (sink, "failed to create directory '%s'", dir_name);
+ GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
+ ("failed to create directory"), (NULL));
+ g_free (file_name);
+ g_free (dir_name);
+ return FALSE;
+ }
+ g_free (dir_name);
+ }
+ g_free (file_name);
+ }
+
+ return TRUE;
+}
diff --git a/ext/curl/gstcurlfilesink.h b/ext/curl/gstcurlfilesink.h
new file mode 100644
index 000000000..31c4bf360
--- /dev/null
+++ b/ext/curl/gstcurlfilesink.h
@@ -0,0 +1,58 @@
+/* GStreamer
+ * Copyright (C) 2011 Axis Communications <dev-gstreamer@axis.com>
+ *
+ * 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.
+ */
+
+#ifndef __GST_CURL_FILE_SINK__
+#define __GST_CURL_FILE_SINK__
+
+#include <gst/gst.h>
+#include <gst/base/gstbasesink.h>
+#include <curl/curl.h>
+#include "gstcurlbasesink.h"
+
+G_BEGIN_DECLS
+#define GST_TYPE_CURL_FILE_SINK \
+ (gst_curl_file_sink_get_type())
+#define GST_CURL_FILE_SINK(obj) \
+ (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_CURL_FILE_SINK, GstCurlFileSink))
+#define GST_CURL_FILE_SINK_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_CURL_FILE_SINK, GstCurlFileSinkClass))
+#define GST_IS_CURL_FILE_SINK(obj) \
+ (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_CURL_FILE_SINK))
+#define GST_IS_CURL_FILE_SINK_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_CURL_FILE_SINK))
+typedef struct _GstCurlFileSink GstCurlFileSink;
+typedef struct _GstCurlFileSinkClass GstCurlFileSinkClass;
+
+struct _GstCurlFileSink
+{
+ GstCurlBaseSink parent;
+
+ /*< private > */
+ gboolean create_dirs;
+};
+
+struct _GstCurlFileSinkClass
+{
+ GstCurlBaseSinkClass parent_class;
+};
+
+GType gst_curl_file_sink_get_type (void);
+
+G_END_DECLS
+#endif