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