Fix crash when closing session, recursive mon-section removal
[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 #include "pbd/string_convert.h"
27
28 #include "ardour/ardour.h"
29 #include "ardour/session_directory.h"
30
31 #include "ardour_http.h"
32 #include "utils.h"
33 #include "utils_videotl.h"
34 #include "video_image_frame.h"
35
36 #ifdef WAF_BUILD
37 #include "gtk2ardour-version.h"
38 #endif
39
40 #ifndef ARDOUR_CURL_TIMEOUT
41 #define ARDOUR_CURL_TIMEOUT (60)
42 #endif
43 #include "pbd/i18n.h"
44
45 using namespace Gtk;
46 using namespace std;
47 using namespace PBD;
48 using namespace ARDOUR;
49 using namespace VideoUtils;
50
51 unsigned int VideoUtils::harvid_version = 0x0;
52
53 bool
54 VideoUtils::confirm_video_outfn (Gtk::Window& parent, std::string outfn, std::string docroot)
55 {
56         /* replace docroot's '/' to G_DIR_SEPARATOR for the comparison */
57         size_t look_here = 0;
58         size_t found_here;
59         const char ds = G_DIR_SEPARATOR;
60         while((found_here = docroot.find('/', look_here)) != string::npos) {
61                 docroot.replace(found_here, 1, std::string(&ds, 1));
62                 look_here = found_here + 1;
63         }
64
65         if (!docroot.empty() && docroot.compare(0, docroot.length(), outfn, 0, docroot.length())) {
66                 ArdourDialog confirm (_("Destination is outside Video Server's docroot. "), true);
67                 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?"));
68                 confirm.get_vbox()->pack_start (m, true, true);
69                 confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
70                 confirm.add_button (_("Continue"), Gtk::RESPONSE_ACCEPT);
71                 confirm.show_all ();
72                 if (confirm.run() == RESPONSE_CANCEL) { return false; }
73         }
74
75         if (Glib::file_test(outfn, Glib::FILE_TEST_EXISTS)) {
76                 bool overwrite = ARDOUR_UI_UTILS::overwrite_file_dialog (parent,
77                                                                          _("Confirm Overwrite"),
78                                                                          _("A file with the same name already exists. Do you want to overwrite it?"));
79
80                 if (!overwrite) {
81                         return false;
82                 }
83         }
84
85         std::string dir = Glib::path_get_dirname (outfn);
86         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
87                 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
88                 return false;
89         }
90         return true;
91 }
92
93 std::string
94 VideoUtils::video_dest_dir (const std::string sessiondir, const std::string docroot)
95 {
96         std::string dir = docroot;
97         if (dir.empty() || !dir.compare(0, dir.length(), sessiondir, 0, dir.length())) {
98                 dir=sessiondir;
99         }
100         if ((dir.empty() || dir.at(dir.length()-1) != G_DIR_SEPARATOR)) { dir += G_DIR_SEPARATOR; }
101
102         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
103                 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
104         }
105         return dir;
106 }
107
108 std::string
109 VideoUtils::video_get_docroot (ARDOUR::RCConfiguration* config)
110 {
111         if (config->get_video_advanced_setup()) {
112                 return config->get_video_server_docroot();
113         }
114 #ifndef PLATFORM_WINDOWS
115         return X_("/");
116 #else
117         if (harvid_version >= 0x000802) { // 0.8.2
118                 return X_("");
119         } else {
120                 return X_("C:\\");
121         }
122 #endif
123 }
124
125 std::string
126 VideoUtils::video_get_server_url (ARDOUR::RCConfiguration* config)
127 {
128         if (config->get_video_advanced_setup()) {
129                 return config->get_video_server_url();
130         }
131         return X_("http://127.0.0.1:1554");
132 }
133
134
135 std::string
136 VideoUtils::strip_file_extension (const std::string infile)
137 {
138         std::string rv;
139         char *ext, *bn = strdup(infile.c_str());
140         if ((ext=strrchr(bn, '.'))) {
141                 if (!strchr(ext, G_DIR_SEPARATOR)) {
142                         *ext = 0;
143                 }
144         }
145         rv = std::string(bn);
146         free(bn);
147         return rv;
148 }
149
150 std::string
151 VideoUtils::get_file_extension (const std::string infile)
152 {
153         std::string rv = "";
154         char *ext, *bn = strdup(infile.c_str());
155         if ((ext=strrchr(bn, '.'))) {
156                 if (!strchr(ext, G_DIR_SEPARATOR)) {
157                         rv=std::string(ext+1);
158                 }
159         }
160         free(bn);
161         return rv;
162 }
163
164 std::string
165 VideoUtils::video_dest_file (const std::string dir, const std::string infile)
166 {
167         return Glib::build_filename(dir, strip_file_extension(Glib::path_get_basename(infile)) + ".avi");
168 }
169
170 std::string
171 VideoUtils::video_map_path (std::string server_docroot, std::string filepath)
172 {
173         std::string rv = filepath;
174
175         /* strip docroot */
176         if (server_docroot.length() > 0) {
177                 if (rv.compare(0, server_docroot.length(), server_docroot) == 0 ) {
178                         rv = rv.substr(server_docroot.length());
179                 }
180         }
181
182         /* replace all G_DIR_SEPARATOR with '/' */
183         size_t look_here = 0;
184         size_t found_here;
185         while((found_here = rv.find(G_DIR_SEPARATOR, look_here)) != string::npos) {
186                 rv.replace(found_here, 1, "/");
187                 look_here = found_here + 1;
188         }
189
190         CURL *curl;
191         char *ue;
192         curl = curl_easy_init();
193         ue = curl_easy_escape(curl, rv.c_str(),rv.length());
194         if (ue) {
195                 rv = std::string(ue);
196                 curl_free(ue);
197         }
198         curl_easy_cleanup(curl);
199
200         return rv;
201 }
202
203 void
204 VideoUtils::ParseCSV (const std::string &csv, std::vector<std::vector<std::string> > &lines)
205 {
206         bool inQuote(false);
207         bool newLine(false);
208         std::string field;
209         lines.clear();
210         std::vector<std::string> line;
211
212         std::string::const_iterator aChar = csv.begin();
213         while (aChar != csv.end()) {
214                 switch (*aChar) {
215                 case '"':
216                  newLine = false;
217                  inQuote = !inQuote;
218                  break;
219
220                 case ',':
221                  newLine = false;
222                  if (inQuote == true) {
223                                 field += *aChar;
224                  } else {
225                                 line.push_back(field);
226                                 field.clear();
227                  }
228                  break;
229
230                 case '\n':
231                 case '\r':
232                  if (inQuote == true) {
233                                 field += *aChar;
234                  } else {
235                                 if (newLine == false) {
236                                          line.push_back(field);
237                                          lines.push_back(line);
238                                          field.clear();
239                                          line.clear();
240                                          newLine = true;
241                                 }
242                  }
243                  break;
244
245                 default:
246                          newLine = false;
247                          field.push_back(*aChar);
248                          break;
249                 }
250                 aChar++;
251         }
252
253          if (field.size())
254                 line.push_back(field);
255
256          if (line.size())
257                 lines.push_back(line);
258 }
259
260 bool
261 VideoUtils::video_query_info (
262                 std::string video_server_url,
263                 std::string filepath,
264                 double &video_file_fps,
265                 long long int &video_duration,
266                 double &video_start_offset,
267                 double &video_aspect_ratio
268                 )
269 {
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, false);
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 = string_to<double>(lines.at(0).at(3));
290         video_file_fps = string_to<double>(lines.at(0).at(4));
291         video_duration = string_to<int64_t>(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