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  * @see_also:
25  *
26  * This is a network sink that uses libcurl as a client to upload data to
27  * a local or network drive.
28  *
29  * ## Example launch line (upload a JPEG file to /home/test/images directory)
30  * |[
31  * gst-launch-1.0 filesrc location=image.jpg ! jpegparse ! curlfilesink  \
32  *     file-name=image.jpg  \
33  *     location=file:///home/test/images/
34  * ]|
35  *
36  */
37 
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41 
42 #include <curl/curl.h>
43 #include <string.h>
44 #include <stdio.h>
45 
46 #if HAVE_SYS_SOCKET_H
47 #include <sys/socket.h>
48 #endif
49 #include <sys/types.h>
50 #if HAVE_NETINET_IN_H
51 #include <netinet/in.h>
52 #endif
53 #include <unistd.h>
54 #if HAVE_NETINET_IP_H
55 #include <netinet/ip.h>
56 #endif
57 #if HAVE_NETINET_TCP_H
58 #include <netinet/tcp.h>
59 #endif
60 #include <sys/stat.h>
61 #include <fcntl.h>
62 
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 
99 static void
gst_curl_file_sink_class_init(GstCurlFileSinkClass * klass)100 gst_curl_file_sink_class_init (GstCurlFileSinkClass * klass)
101 {
102   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
103   GstCurlBaseSinkClass *gstcurlbasesink_class = (GstCurlBaseSinkClass *) klass;
104   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
105 
106   GST_DEBUG_CATEGORY_INIT (gst_curl_file_sink_debug, "curlfilesink", 0,
107       "curl file sink element");
108   GST_DEBUG_OBJECT (klass, "class_init");
109 
110   gst_element_class_set_static_metadata (element_class,
111       "Curl file sink",
112       "Sink/Network",
113       "Upload data over FILE protocol using libcurl",
114       "Patricia Muscalu <patricia@axis.com>");
115 
116   gobject_class->set_property = gst_curl_file_sink_set_property;
117   gobject_class->get_property = gst_curl_file_sink_get_property;
118 
119   gstcurlbasesink_class->set_protocol_dynamic_options_unlocked =
120       set_file_dynamic_options_unlocked;
121   gstcurlbasesink_class->set_options_unlocked = set_file_options_unlocked;
122   gstcurlbasesink_class->prepare_transfer = gst_curl_file_sink_prepare_transfer;
123 
124   g_object_class_install_property (gobject_class, PROP_CREATE_DIRS,
125       g_param_spec_boolean ("create-dirs", "Create missing directories",
126           "Attempt to create missing directory included in the path",
127           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
128 }
129 
130 static void
gst_curl_file_sink_init(GstCurlFileSink * sink)131 gst_curl_file_sink_init (GstCurlFileSink * sink)
132 {
133 }
134 
135 static void
gst_curl_file_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)136 gst_curl_file_sink_set_property (GObject * object, guint prop_id,
137     const GValue * value, GParamSpec * pspec)
138 {
139   GstCurlFileSink *sink;
140   GstState cur_state;
141 
142   g_return_if_fail (GST_IS_CURL_FILE_SINK (object));
143   sink = GST_CURL_FILE_SINK (object);
144 
145   gst_element_get_state (GST_ELEMENT (sink), &cur_state, NULL, 0);
146   if (cur_state != GST_STATE_PLAYING && cur_state != GST_STATE_PAUSED) {
147     GST_OBJECT_LOCK (sink);
148 
149     switch (prop_id) {
150       case PROP_CREATE_DIRS:
151         sink->create_dirs = g_value_get_boolean (value);
152         GST_DEBUG_OBJECT (sink, "create-dirs set to %d", sink->create_dirs);
153         break;
154 
155       default:
156         GST_DEBUG_OBJECT (sink, "invalid property id %d", prop_id);
157         break;
158     }
159 
160     GST_OBJECT_UNLOCK (sink);
161   }
162 }
163 
164 static void
gst_curl_file_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)165 gst_curl_file_sink_get_property (GObject * object, guint prop_id,
166     GValue * value, GParamSpec * pspec)
167 {
168   GstCurlFileSink *sink;
169 
170   g_return_if_fail (GST_IS_CURL_FILE_SINK (object));
171   sink = GST_CURL_FILE_SINK (object);
172 
173   switch (prop_id) {
174     case PROP_CREATE_DIRS:
175       g_value_set_boolean (value, sink->create_dirs);
176       break;
177     default:
178       GST_DEBUG_OBJECT (sink, "invalid property id");
179       break;
180   }
181 }
182 
183 static gboolean
set_file_dynamic_options_unlocked(GstCurlBaseSink * basesink)184 set_file_dynamic_options_unlocked (GstCurlBaseSink * basesink)
185 {
186   gchar *tmp = g_strdup_printf ("%s%s", basesink->url, basesink->file_name);
187   CURLcode res;
188 
189   res = curl_easy_setopt (basesink->curl, CURLOPT_URL, tmp);
190   g_free (tmp);
191   if (res != CURLE_OK) {
192     basesink->error = g_strdup_printf ("failed to set URL: %s",
193         curl_easy_strerror (res));
194     return FALSE;
195   }
196 
197   return TRUE;
198 }
199 
200 static gboolean
set_file_options_unlocked(GstCurlBaseSink * basesink)201 set_file_options_unlocked (GstCurlBaseSink * basesink)
202 {
203   CURLcode res;
204 
205   res = curl_easy_setopt (basesink->curl, CURLOPT_UPLOAD, 1L);
206   if (res != CURLE_OK) {
207     basesink->error = g_strdup_printf ("failed to prepare for upload: %s",
208         curl_easy_strerror (res));
209     return FALSE;
210   }
211 
212   return TRUE;
213 }
214 
215 static gboolean
gst_curl_file_sink_prepare_transfer(GstCurlBaseSink * basesink)216 gst_curl_file_sink_prepare_transfer (GstCurlBaseSink * basesink)
217 {
218   GstCurlFileSink *sink = GST_CURL_FILE_SINK (basesink);
219 
220   if (sink->create_dirs) {
221     gchar *file_name;
222     gchar *last_slash;
223 
224     gchar *url = g_strdup_printf ("%s%s", basesink->url, basesink->file_name);
225     file_name = g_filename_from_uri (url, NULL, NULL);
226     if (file_name == NULL) {
227       basesink->error = g_strdup_printf ("failed to parse file name '%s'", url);
228       g_free (url);
229       return FALSE;
230     }
231     g_free (url);
232 
233     last_slash = strrchr (file_name, G_DIR_SEPARATOR);
234     if (last_slash != NULL) {
235       /* create dir if file name contains dir component */
236       gchar *dir_name = g_strndup (file_name, last_slash - file_name);
237       if (g_mkdir_with_parents (dir_name, S_IRWXU) < 0) {
238         basesink->error = g_strdup_printf ("failed to create directory '%s'",
239             dir_name);
240         g_free (file_name);
241         g_free (dir_name);
242         return FALSE;
243       }
244       g_free (dir_name);
245     }
246     g_free (file_name);
247   }
248 
249   return TRUE;
250 }
251