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