1 /* GStreamer
2 * Copyright (C) 2011 Axis Communications <dev-gstreamer@axis.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * SECTION:element-curlfilesink
22 * @title: curlfilesink
23 * @short_description: sink that uploads data to a server using libcurl
24 *
25 * This is a network sink that uses libcurl as a client to upload data to
26 * a local or network drive.
27 *
28 * ## Example launch line (upload a JPEG file to /home/test/images directory)
29 * |[
30 * gst-launch-1.0 filesrc location=image.jpg ! jpegparse ! curlfilesink \
31 * file-name=image.jpg \
32 * location=file:///home/test/images/
33 * ]|
34 *
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include <curl/curl.h>
42 #include <string.h>
43 #include <stdio.h>
44
45 #if HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
47 #endif
48 #include <sys/types.h>
49 #if HAVE_NETINET_IN_H
50 #include <netinet/in.h>
51 #endif
52 #include <unistd.h>
53 #if HAVE_NETINET_IP_H
54 #include <netinet/ip.h>
55 #endif
56 #if HAVE_NETINET_TCP_H
57 #include <netinet/tcp.h>
58 #endif
59 #include <sys/stat.h>
60 #include <fcntl.h>
61
62 #include "gstcurlelements.h"
63 #include "gstcurlbasesink.h"
64 #include "gstcurlfilesink.h"
65
66 /* Default values */
67 #define GST_CAT_DEFAULT gst_curl_file_sink_debug
68
69
70 /* Plugin specific settings */
71
72 GST_DEBUG_CATEGORY_STATIC (gst_curl_file_sink_debug);
73
74 enum
75 {
76 PROP_0,
77 PROP_CREATE_DIRS
78 };
79
80
81 /* Object class function declarations */
82
83
84 /* private functions */
85 static void gst_curl_file_sink_set_property (GObject * object, guint prop_id,
86 const GValue * value, GParamSpec * pspec);
87 static void gst_curl_file_sink_get_property (GObject * object, guint prop_id,
88 GValue * value, GParamSpec * pspec);
89
90 static gboolean set_file_options_unlocked (GstCurlBaseSink * curlbasesink);
91 static gboolean set_file_dynamic_options_unlocked
92 (GstCurlBaseSink * curlbasesink);
93 static gboolean gst_curl_file_sink_prepare_transfer (GstCurlBaseSink *
94 curlbasesink);
95
96 #define gst_curl_file_sink_parent_class parent_class
97 G_DEFINE_TYPE (GstCurlFileSink, gst_curl_file_sink, GST_TYPE_CURL_BASE_SINK);
98 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (curlfilesink, "curlfilesink",
99 GST_RANK_NONE, GST_TYPE_CURL_FILE_SINK, curl_element_init (plugin));
100
101 static void
gst_curl_file_sink_class_init(GstCurlFileSinkClass * klass)102 gst_curl_file_sink_class_init (GstCurlFileSinkClass * klass)
103 {
104 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
105 GstCurlBaseSinkClass *gstcurlbasesink_class = (GstCurlBaseSinkClass *) klass;
106 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
107
108 GST_DEBUG_CATEGORY_INIT (gst_curl_file_sink_debug, "curlfilesink", 0,
109 "curl file sink element");
110
111 gst_element_class_set_static_metadata (element_class,
112 "Curl file sink",
113 "Sink/Network",
114 "Upload data over FILE protocol using libcurl",
115 "Patricia Muscalu <patricia@axis.com>");
116
117 gobject_class->set_property = gst_curl_file_sink_set_property;
118 gobject_class->get_property = gst_curl_file_sink_get_property;
119
120 gstcurlbasesink_class->set_protocol_dynamic_options_unlocked =
121 set_file_dynamic_options_unlocked;
122 gstcurlbasesink_class->set_options_unlocked = set_file_options_unlocked;
123 gstcurlbasesink_class->prepare_transfer = gst_curl_file_sink_prepare_transfer;
124
125 g_object_class_install_property (gobject_class, PROP_CREATE_DIRS,
126 g_param_spec_boolean ("create-dirs", "Create missing directories",
127 "Attempt to create missing directory included in the path",
128 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
129 }
130
131 static void
gst_curl_file_sink_init(GstCurlFileSink * sink)132 gst_curl_file_sink_init (GstCurlFileSink * sink)
133 {
134 }
135
136 static void
gst_curl_file_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)137 gst_curl_file_sink_set_property (GObject * object, guint prop_id,
138 const GValue * value, GParamSpec * pspec)
139 {
140 GstCurlFileSink *sink;
141 GstState cur_state;
142
143 g_return_if_fail (GST_IS_CURL_FILE_SINK (object));
144 sink = GST_CURL_FILE_SINK (object);
145
146 gst_element_get_state (GST_ELEMENT (sink), &cur_state, NULL, 0);
147 if (cur_state != GST_STATE_PLAYING && cur_state != GST_STATE_PAUSED) {
148 GST_OBJECT_LOCK (sink);
149
150 switch (prop_id) {
151 case PROP_CREATE_DIRS:
152 sink->create_dirs = g_value_get_boolean (value);
153 GST_DEBUG_OBJECT (sink, "create-dirs set to %d", sink->create_dirs);
154 break;
155
156 default:
157 GST_DEBUG_OBJECT (sink, "invalid property id %d", prop_id);
158 break;
159 }
160
161 GST_OBJECT_UNLOCK (sink);
162 }
163 }
164
165 static void
gst_curl_file_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)166 gst_curl_file_sink_get_property (GObject * object, guint prop_id,
167 GValue * value, GParamSpec * pspec)
168 {
169 GstCurlFileSink *sink;
170
171 g_return_if_fail (GST_IS_CURL_FILE_SINK (object));
172 sink = GST_CURL_FILE_SINK (object);
173
174 switch (prop_id) {
175 case PROP_CREATE_DIRS:
176 g_value_set_boolean (value, sink->create_dirs);
177 break;
178 default:
179 GST_DEBUG_OBJECT (sink, "invalid property id");
180 break;
181 }
182 }
183
184 static gboolean
set_file_dynamic_options_unlocked(GstCurlBaseSink * basesink)185 set_file_dynamic_options_unlocked (GstCurlBaseSink * basesink)
186 {
187 gchar *tmp = g_strdup_printf ("%s%s", basesink->url, basesink->file_name);
188 CURLcode res;
189
190 res = curl_easy_setopt (basesink->curl, CURLOPT_URL, tmp);
191 g_free (tmp);
192 if (res != CURLE_OK) {
193 basesink->error = g_strdup_printf ("failed to set URL: %s",
194 curl_easy_strerror (res));
195 return FALSE;
196 }
197
198 return TRUE;
199 }
200
201 static gboolean
set_file_options_unlocked(GstCurlBaseSink * basesink)202 set_file_options_unlocked (GstCurlBaseSink * basesink)
203 {
204 CURLcode res;
205
206 res = curl_easy_setopt (basesink->curl, CURLOPT_UPLOAD, 1L);
207 if (res != CURLE_OK) {
208 basesink->error = g_strdup_printf ("failed to prepare for upload: %s",
209 curl_easy_strerror (res));
210 return FALSE;
211 }
212
213 return TRUE;
214 }
215
216 static gboolean
gst_curl_file_sink_prepare_transfer(GstCurlBaseSink * basesink)217 gst_curl_file_sink_prepare_transfer (GstCurlBaseSink * basesink)
218 {
219 GstCurlFileSink *sink = GST_CURL_FILE_SINK (basesink);
220
221 if (sink->create_dirs) {
222 gchar *file_name;
223 gchar *last_slash;
224
225 gchar *url = g_strdup_printf ("%s%s", basesink->url, basesink->file_name);
226 file_name = g_filename_from_uri (url, NULL, NULL);
227 if (file_name == NULL) {
228 basesink->error = g_strdup_printf ("failed to parse file name '%s'", url);
229 g_free (url);
230 return FALSE;
231 }
232 g_free (url);
233
234 last_slash = strrchr (file_name, G_DIR_SEPARATOR);
235 if (last_slash != NULL) {
236 /* create dir if file name contains dir component */
237 gchar *dir_name = g_strndup (file_name, last_slash - file_name);
238 if (g_mkdir_with_parents (dir_name, S_IRWXU) < 0) {
239 basesink->error = g_strdup_printf ("failed to create directory '%s'",
240 dir_name);
241 g_free (file_name);
242 g_free (dir_name);
243 return FALSE;
244 }
245 g_free (dir_name);
246 }
247 g_free (file_name);
248 }
249
250 return TRUE;
251 }
252