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