2d859b692ad404ced8a40fd82680469c2e60b532
[ardour.git] / gtk2_ardour / utils_videotl.cc
1 /*
2     Copyright (C) 2010-2013 Paul Davis
3     Author: Robin Gareus <robin@gareus.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20 #include <cstdio>
21 #include <string>
22 #include <cerrno>
23 #include <gtkmm.h>
24 #include <curl/curl.h>
25
26 #include "pbd/error.h"
27 #include "ardour/ardour.h"
28 #include "ardour/session_directory.h"
29 #include "video_image_frame.h"
30 #include "utils_videotl.h"
31 #include "utils.h"
32
33 #ifdef WAF_BUILD
34 #include "gtk2ardour-version.h"
35 #endif
36
37 #ifndef ARDOUR_CURL_TIMEOUT
38 #define ARDOUR_CURL_TIMEOUT (60)
39 #endif
40 #include "pbd/i18n.h"
41
42 using namespace Gtk;
43 using namespace std;
44 using namespace PBD;
45 using namespace ARDOUR;
46 using namespace VideoUtils;
47
48 bool
49 VideoUtils::confirm_video_outfn (Gtk::Window& parent, std::string outfn, std::string docroot)
50 {
51         /* replace docroot's '/' to G_DIR_SEPARATOR for the comparison */
52         size_t look_here = 0;
53         size_t found_here;
54         const char ds = G_DIR_SEPARATOR;
55         while((found_here = docroot.find('/', look_here)) != string::npos) {
56                 docroot.replace(found_here, 1, std::string(&ds, 1));
57                 look_here = found_here + 1;
58         }
59
60         if (!docroot.empty() && docroot.compare(0, docroot.length(), outfn, 0, docroot.length())) {
61                 ArdourDialog confirm (_("Destination is outside Video Server's docroot. "), true);
62                 Label m (_("The destination file path is outside of the Video Server's docroot. The file will not be readable by the Video Server. Do you still want to continue?"));
63                 confirm.get_vbox()->pack_start (m, true, true);
64                 confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
65                 confirm.add_button (_("Continue"), Gtk::RESPONSE_ACCEPT);
66                 confirm.show_all ();
67                 if (confirm.run() == RESPONSE_CANCEL) { return false; }
68         }
69
70         if (Glib::file_test(outfn, Glib::FILE_TEST_EXISTS)) {
71                 bool overwrite = ARDOUR_UI_UTILS::overwrite_file_dialog (parent,
72                                                                          _("Confirm Overwrite"),
73                                                                          _("A file with the same name already exists. Do you want to overwrite it?"));
74
75                 if (!overwrite) {
76                         return false;
77                 }
78         }
79
80         std::string dir = Glib::path_get_dirname (outfn);
81         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
82                 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
83                 return false;
84         }
85         return true;
86 }
87
88 std::string
89 VideoUtils::video_dest_dir (const std::string sessiondir, const std::string docroot)
90 {
91         std::string dir = docroot;
92         if (dir.empty() || !dir.compare(0, dir.length(), sessiondir, 0, dir.length())) {
93                 dir=sessiondir;
94         }
95         if ((dir.empty() || dir.at(dir.length()-1) != G_DIR_SEPARATOR)) { dir += G_DIR_SEPARATOR; }
96
97         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
98                 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
99         }
100         return dir;
101 }
102
103 std::string
104 VideoUtils::video_get_docroot (ARDOUR::RCConfiguration* config)
105 {
106         if (config->get_video_advanced_setup()) {
107                 return config->get_video_server_docroot();
108         }
109 #ifndef PLATFORM_WINDOWS
110         return X_("/");
111 #else
112         return X_("C:\\");
113 #endif
114 }
115
116 std::string
117 VideoUtils::video_get_server_url (ARDOUR::RCConfiguration* config)
118 {
119         if (config->get_video_advanced_setup()) {
120                 return config->get_video_server_url();
121         }
122         return X_("http://127.0.0.1:1554");
123 }
124
125
126 std::string
127 VideoUtils::strip_file_extension (const std::string infile)
128 {
129         std::string rv;
130         char *ext, *bn = strdup(infile.c_str());
131         if ((ext=strrchr(bn, '.'))) {
132                 if (!strchr(ext, G_DIR_SEPARATOR)) {
133                         *ext = 0;
134                 }
135         }
136         rv = std::string(bn);
137         free(bn);
138         return rv;
139 }
140
141 std::string
142 VideoUtils::get_file_extension (const std::string infile)
143 {
144         std::string rv = "";
145         char *ext, *bn = strdup(infile.c_str());
146         if ((ext=strrchr(bn, '.'))) {
147                 if (!strchr(ext, G_DIR_SEPARATOR)) {
148                         rv=std::string(ext+1);
149                 }
150         }
151         free(bn);
152         return rv;
153 }
154
155 std::string
156 VideoUtils::video_dest_file (const std::string dir, const std::string infile)
157 {
158         return Glib::build_filename(dir, strip_file_extension(Glib::path_get_basename(infile)) + ".avi");
159 }
160
161 std::string
162 VideoUtils::video_map_path (std::string server_docroot, std::string filepath)
163 {
164         std::string rv = filepath;
165
166         /* strip docroot */
167         if (server_docroot.length() > 0) {
168                 if (rv.compare(0, server_docroot.length(), server_docroot) == 0 ) {
169                         rv = rv.substr(server_docroot.length());
170                 }
171         }
172
173         /* replace all G_DIR_SEPARATOR with '/' */
174         size_t look_here = 0;
175         size_t found_here;
176         while((found_here = rv.find(G_DIR_SEPARATOR, look_here)) != string::npos) {
177                 rv.replace(found_here, 1, "/");
178                 look_here = found_here + 1;
179         }
180
181         CURL *curl;
182         char *ue;
183         curl = curl_easy_init();
184         ue = curl_easy_escape(curl, rv.c_str(),rv.length());
185         if (ue) {
186                 rv = std::string(ue);
187                 curl_free(ue);
188         }
189         curl_easy_cleanup(curl);
190
191         return rv;
192 }
193
194 void
195 VideoUtils::ParseCSV (const std::string &csv, std::vector<std::vector<std::string> > &lines)
196 {
197         bool inQuote(false);
198         bool newLine(false);
199         std::string field;
200         lines.clear();
201         std::vector<std::string> line;
202
203         std::string::const_iterator aChar = csv.begin();
204         while (aChar != csv.end()) {
205                 switch (*aChar) {
206                 case '"':
207                  newLine = false;
208                  inQuote = !inQuote;
209                  break;
210
211                 case ',':
212                  newLine = false;
213                  if (inQuote == true) {
214                                 field += *aChar;
215                  } else {
216                                 line.push_back(field);
217                                 field.clear();
218                  }
219                  break;
220
221                 case '\n':
222                 case '\r':
223                  if (inQuote == true) {
224                                 field += *aChar;
225                  } else {
226                                 if (newLine == false) {
227                                          line.push_back(field);
228                                          lines.push_back(line);
229                                          field.clear();
230                                          line.clear();
231                                          newLine = true;
232                                 }
233                  }
234                  break;
235
236                 default:
237                          newLine = false;
238                          field.push_back(*aChar);
239                          break;
240                 }
241                 aChar++;
242         }
243
244          if (field.size())
245                 line.push_back(field);
246
247          if (line.size())
248                 lines.push_back(line);
249 }
250
251 bool
252 VideoUtils::video_query_info (
253                 std::string video_server_url,
254                 std::string filepath,
255                 double &video_file_fps,
256                 long long int &video_duration,
257                 double &video_start_offset,
258                 double &video_aspect_ratio
259                 )
260 {
261         char url[2048];
262
263         snprintf(url, sizeof(url), "%s%sinfo/?file=%s&format=csv"
264                         , video_server_url.c_str()
265                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
266                         , filepath.c_str());
267         char *res = a3_curl_http_get(url, NULL);
268         if (!res) {
269                 return false;
270         }
271
272         std::vector<std::vector<std::string> > lines;
273         ParseCSV(std::string(res), lines);
274         free(res);
275
276         if (lines.empty() || lines.at(0).empty() || lines.at(0).size() != 6) {
277                 return false;
278         }
279         if (atoi(lines.at(0).at(0)) != 1) return false; // version
280         video_start_offset = 0.0;
281         video_aspect_ratio = atof (lines.at(0).at(3));
282         video_file_fps = atof (lines.at(0).at(4));
283         video_duration = atoll(lines.at(0).at(5));
284         return true;
285 }
286
287 void
288 VideoUtils::video_draw_cross (Glib::RefPtr<Gdk::Pixbuf> img)
289 {
290
291         int rowstride = img->get_rowstride();
292         int n_channels = img->get_n_channels();
293         guchar *pixels, *p;
294         pixels = img->get_pixels();
295
296         int x,y;
297         int clip_width = img->get_width();
298         int clip_height = img->get_height();
299
300         for (x=0;x<clip_width;x++) {
301                 y = clip_height * x / clip_width;
302                 p = pixels + y * rowstride + x * n_channels;
303                 p[0] = 192; p[1] = 192; p[2] = 192;
304                 if (n_channels>3) p[3] = 255;
305                 p = pixels + y * rowstride + (clip_width-x-1) * n_channels;
306                 p[0] = 192; p[1] = 192; p[2] = 192;
307                 if (n_channels>3) p[3] = 255;
308         }
309 }
310
311
312 extern "C" {
313 #include <curl/curl.h>
314
315         struct A3MemoryStruct {
316                 char *data;
317                 size_t size;
318         };
319
320         static size_t
321         WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
322                 size_t realsize = size * nmemb;
323                 struct A3MemoryStruct *mem = (struct A3MemoryStruct *)data;
324
325                 mem->data = (char *)realloc(mem->data, mem->size + realsize + 1);
326                 if (mem->data) {
327                         memcpy(&(mem->data[mem->size]), ptr, realsize);
328                         mem->size += realsize;
329                         mem->data[mem->size] = 0;
330                 }
331                 return realsize;
332         }
333
334         char *a3_curl_http_get (const char *u, int *status) {
335                 CURL *curl;
336                 CURLcode res;
337                 struct A3MemoryStruct chunk;
338                 long int httpstatus;
339                 if (status) *status = 0;
340                 //Glib::usleep(500000); return NULL; // TEST & DEBUG
341                 if (strncmp("http://", u, 7)) return NULL;
342
343                 chunk.data=NULL;
344                 chunk.size=0;
345
346                 curl = curl_easy_init();
347                 if(!curl) return NULL;
348                 curl_easy_setopt(curl, CURLOPT_URL, u);
349
350                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
351                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
352                 curl_easy_setopt(curl, CURLOPT_USERAGENT, PROGRAM_NAME VERSIONSTRING);
353                 curl_easy_setopt(curl, CURLOPT_TIMEOUT, ARDOUR_CURL_TIMEOUT);
354                 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
355 #ifdef CURLERRORDEBUG
356                 char curlerror[CURL_ERROR_SIZE] = "";
357                 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curlerror);
358 #endif
359
360                 res = curl_easy_perform(curl);
361                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpstatus);
362                 curl_easy_cleanup(curl);
363                 if (status) *status = httpstatus;
364                 if (res) {
365 #ifdef CURLERRORDEBUG
366                         printf("a3_curl_http_get() failed: %s\n", curlerror);
367 #endif
368                         return NULL;
369                 }
370                 if (httpstatus != 200) {
371                         free (chunk.data);
372                         chunk.data = NULL;
373                 }
374                 return (chunk.data);
375         }
376
377 } /* end extern "C" */