Parse information from harvid independent from the locale
[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
25 #include "pbd/error.h"
26
27 #include "ardour/ardour.h"
28 #include "ardour/session_directory.h"
29
30 #include "ardour_http.h"
31 #include "utils.h"
32 #include "utils_videotl.h"
33 #include "video_image_frame.h"
34
35 #ifdef WAF_BUILD
36 #include "gtk2ardour-version.h"
37 #endif
38
39 #ifndef ARDOUR_CURL_TIMEOUT
40 #define ARDOUR_CURL_TIMEOUT (60)
41 #endif
42 #include "pbd/i18n.h"
43
44 using namespace Gtk;
45 using namespace std;
46 using namespace PBD;
47 using namespace ARDOUR;
48 using namespace VideoUtils;
49
50 bool
51 VideoUtils::confirm_video_outfn (Gtk::Window& parent, std::string outfn, std::string docroot)
52 {
53         /* replace docroot's '/' to G_DIR_SEPARATOR for the comparison */
54         size_t look_here = 0;
55         size_t found_here;
56         const char ds = G_DIR_SEPARATOR;
57         while((found_here = docroot.find('/', look_here)) != string::npos) {
58                 docroot.replace(found_here, 1, std::string(&ds, 1));
59                 look_here = found_here + 1;
60         }
61
62         if (!docroot.empty() && docroot.compare(0, docroot.length(), outfn, 0, docroot.length())) {
63                 ArdourDialog confirm (_("Destination is outside Video Server's docroot. "), true);
64                 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?"));
65                 confirm.get_vbox()->pack_start (m, true, true);
66                 confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
67                 confirm.add_button (_("Continue"), Gtk::RESPONSE_ACCEPT);
68                 confirm.show_all ();
69                 if (confirm.run() == RESPONSE_CANCEL) { return false; }
70         }
71
72         if (Glib::file_test(outfn, Glib::FILE_TEST_EXISTS)) {
73                 bool overwrite = ARDOUR_UI_UTILS::overwrite_file_dialog (parent,
74                                                                          _("Confirm Overwrite"),
75                                                                          _("A file with the same name already exists. Do you want to overwrite it?"));
76
77                 if (!overwrite) {
78                         return false;
79                 }
80         }
81
82         std::string dir = Glib::path_get_dirname (outfn);
83         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
84                 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
85                 return false;
86         }
87         return true;
88 }
89
90 std::string
91 VideoUtils::video_dest_dir (const std::string sessiondir, const std::string docroot)
92 {
93         std::string dir = docroot;
94         if (dir.empty() || !dir.compare(0, dir.length(), sessiondir, 0, dir.length())) {
95                 dir=sessiondir;
96         }
97         if ((dir.empty() || dir.at(dir.length()-1) != G_DIR_SEPARATOR)) { dir += G_DIR_SEPARATOR; }
98
99         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
100                 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
101         }
102         return dir;
103 }
104
105 std::string
106 VideoUtils::video_get_docroot (ARDOUR::RCConfiguration* config)
107 {
108         if (config->get_video_advanced_setup()) {
109                 return config->get_video_server_docroot();
110         }
111 #ifndef PLATFORM_WINDOWS
112         return X_("/");
113 #else
114         return X_("C:\\");
115 #endif
116 }
117
118 std::string
119 VideoUtils::video_get_server_url (ARDOUR::RCConfiguration* config)
120 {
121         if (config->get_video_advanced_setup()) {
122                 return config->get_video_server_url();
123         }
124         return X_("http://127.0.0.1:1554");
125 }
126
127
128 std::string
129 VideoUtils::strip_file_extension (const std::string infile)
130 {
131         std::string rv;
132         char *ext, *bn = strdup(infile.c_str());
133         if ((ext=strrchr(bn, '.'))) {
134                 if (!strchr(ext, G_DIR_SEPARATOR)) {
135                         *ext = 0;
136                 }
137         }
138         rv = std::string(bn);
139         free(bn);
140         return rv;
141 }
142
143 std::string
144 VideoUtils::get_file_extension (const std::string infile)
145 {
146         std::string rv = "";
147         char *ext, *bn = strdup(infile.c_str());
148         if ((ext=strrchr(bn, '.'))) {
149                 if (!strchr(ext, G_DIR_SEPARATOR)) {
150                         rv=std::string(ext+1);
151                 }
152         }
153         free(bn);
154         return rv;
155 }
156
157 std::string
158 VideoUtils::video_dest_file (const std::string dir, const std::string infile)
159 {
160         return Glib::build_filename(dir, strip_file_extension(Glib::path_get_basename(infile)) + ".avi");
161 }
162
163 std::string
164 VideoUtils::video_map_path (std::string server_docroot, std::string filepath)
165 {
166         std::string rv = filepath;
167
168         /* strip docroot */
169         if (server_docroot.length() > 0) {
170                 if (rv.compare(0, server_docroot.length(), server_docroot) == 0 ) {
171                         rv = rv.substr(server_docroot.length());
172                 }
173         }
174
175         /* replace all G_DIR_SEPARATOR with '/' */
176         size_t look_here = 0;
177         size_t found_here;
178         while((found_here = rv.find(G_DIR_SEPARATOR, look_here)) != string::npos) {
179                 rv.replace(found_here, 1, "/");
180                 look_here = found_here + 1;
181         }
182
183         CURL *curl;
184         char *ue;
185         curl = curl_easy_init();
186         ue = curl_easy_escape(curl, rv.c_str(),rv.length());
187         if (ue) {
188                 rv = std::string(ue);
189                 curl_free(ue);
190         }
191         curl_easy_cleanup(curl);
192
193         return rv;
194 }
195
196 void
197 VideoUtils::ParseCSV (const std::string &csv, std::vector<std::vector<std::string> > &lines)
198 {
199         bool inQuote(false);
200         bool newLine(false);
201         std::string field;
202         lines.clear();
203         std::vector<std::string> line;
204
205         std::string::const_iterator aChar = csv.begin();
206         while (aChar != csv.end()) {
207                 switch (*aChar) {
208                 case '"':
209                  newLine = false;
210                  inQuote = !inQuote;
211                  break;
212
213                 case ',':
214                  newLine = false;
215                  if (inQuote == true) {
216                                 field += *aChar;
217                  } else {
218                                 line.push_back(field);
219                                 field.clear();
220                  }
221                  break;
222
223                 case '\n':
224                 case '\r':
225                  if (inQuote == true) {
226                                 field += *aChar;
227                  } else {
228                                 if (newLine == false) {
229                                          line.push_back(field);
230                                          lines.push_back(line);
231                                          field.clear();
232                                          line.clear();
233                                          newLine = true;
234                                 }
235                  }
236                  break;
237
238                 default:
239                          newLine = false;
240                          field.push_back(*aChar);
241                          break;
242                 }
243                 aChar++;
244         }
245
246          if (field.size())
247                 line.push_back(field);
248
249          if (line.size())
250                 lines.push_back(line);
251 }
252
253 bool
254 VideoUtils::video_query_info (
255                 std::string video_server_url,
256                 std::string filepath,
257                 double &video_file_fps,
258                 long long int &video_duration,
259                 double &video_start_offset,
260                 double &video_aspect_ratio
261                 )
262 {
263         LocaleGuard lg;
264         char url[2048];
265
266         snprintf(url, sizeof(url), "%s%sinfo/?file=%s&format=csv"
267                         , video_server_url.c_str()
268                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
269                         , filepath.c_str());
270         std::string res = ArdourCurl::http_get (url);
271         if (res.empty ()) {
272                 return false;
273         }
274
275         std::vector<std::vector<std::string> > lines;
276         ParseCSV(res, lines);
277
278         if (lines.empty() || lines.at(0).empty() || lines.at(0).size() != 6) {
279                 return false;
280         }
281         if (atoi(lines.at(0).at(0)) != 1) return false; // version
282         video_start_offset = 0.0;
283         video_aspect_ratio = atof (lines.at(0).at(3));
284         video_file_fps = atof (lines.at(0).at(4));
285         video_duration = atoll(lines.at(0).at(5));
286
287         if (video_aspect_ratio < 0.01 || video_file_fps < 0.01) {
288                 /* catch errors early, aspect == 0 or fps == 0 will
289                  * wreak havoc down the road */
290                 return false;
291         }
292         return true;
293 }
294
295 void
296 VideoUtils::video_draw_cross (Glib::RefPtr<Gdk::Pixbuf> img)
297 {
298
299         int rowstride = img->get_rowstride();
300         int n_channels = img->get_n_channels();
301         guchar *pixels, *p;
302         pixels = img->get_pixels();
303
304         int x,y;
305         int clip_width = img->get_width();
306         int clip_height = img->get_height();
307
308         for (x=0;x<clip_width;x++) {
309                 y = clip_height * x / clip_width;
310                 p = pixels + y * rowstride + x * n_channels;
311                 p[0] = 192; p[1] = 192; p[2] = 192;
312                 if (n_channels>3) p[3] = 255;
313                 p = pixels + y * rowstride + (clip_width-x-1) * n_channels;
314                 p[0] = 192; p[1] = 192; p[2] = 192;
315                 if (n_channels>3) p[3] = 255;
316         }
317 }
318