1 /*
2  * Copyright (C) 2013-2017 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2013-2018 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2015 André Nusser <andre.nusser@googlemail.com>
5  * Copyright (C) 2016 Tim Mayberry <mojofunk@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #include <cstdio>
22 #include <string>
23 #include <cerrno>
24 #include <gtkmm.h>
25 
26 #include "pbd/error.h"
27 #include "pbd/string_convert.h"
28 
29 #include "ardour/ardour.h"
30 #include "ardour/session_directory.h"
31 
32 #include "ardour_http.h"
33 #include "utils.h"
34 #include "utils_videotl.h"
35 #include "video_image_frame.h"
36 
37 #ifdef WAF_BUILD
38 #include "gtk2ardour-version.h"
39 #endif
40 
41 #ifndef ARDOUR_CURL_TIMEOUT
42 #define ARDOUR_CURL_TIMEOUT (60)
43 #endif
44 #include "pbd/i18n.h"
45 
46 using namespace Gtk;
47 using namespace std;
48 using namespace PBD;
49 using namespace ARDOUR;
50 using namespace VideoUtils;
51 
52 unsigned int VideoUtils::harvid_version = 0x0;
53 
54 bool
confirm_video_outfn(Gtk::Window & parent,std::string outfn,std::string docroot)55 VideoUtils::confirm_video_outfn (Gtk::Window& parent, std::string outfn, std::string docroot)
56 {
57 	/* replace docroot's '/' to G_DIR_SEPARATOR for the comparison */
58 	size_t look_here = 0;
59 	size_t found_here;
60 	const char ds = G_DIR_SEPARATOR;
61 	while((found_here = docroot.find('/', look_here)) != string::npos) {
62 		docroot.replace(found_here, 1, std::string(&ds, 1));
63 		look_here = found_here + 1;
64 	}
65 
66 	if (!docroot.empty() && docroot.compare(0, docroot.length(), outfn, 0, docroot.length())) {
67 		ArdourDialog confirm (_("Destination is outside Video Server's docroot. "), true);
68 		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?"));
69 		confirm.get_vbox()->pack_start (m, true, true);
70 		confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
71 		confirm.add_button (_("Continue"), Gtk::RESPONSE_ACCEPT);
72 		confirm.show_all ();
73 		switch (confirm.run ()) {
74 			case Gtk::RESPONSE_ACCEPT:
75 				break;
76 			default:
77 				return false;
78 		}
79 	}
80 
81 	if (Glib::file_test(outfn, Glib::FILE_TEST_EXISTS)) {
82 		bool overwrite = ARDOUR_UI_UTILS::overwrite_file_dialog (parent,
83 		                                                         _("Confirm Overwrite"),
84 		                                                         _("A file with the same name already exists. Do you want to overwrite it?"));
85 
86 		if (!overwrite) {
87 			return false;
88 		}
89 	}
90 
91 	std::string dir = Glib::path_get_dirname (outfn);
92 	if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
93 		error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
94 		return false;
95 	}
96 	return true;
97 }
98 
99 std::string
video_dest_dir(const std::string sessiondir,const std::string docroot)100 VideoUtils::video_dest_dir (const std::string sessiondir, const std::string docroot)
101 {
102 	std::string dir = docroot;
103 	if (dir.empty() || !dir.compare(0, dir.length(), sessiondir, 0, dir.length())) {
104 		dir=sessiondir;
105 	}
106 	if ((dir.empty() || dir.at(dir.length()-1) != G_DIR_SEPARATOR)) { dir += G_DIR_SEPARATOR; }
107 
108 	if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
109 		error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
110 	}
111 	return dir;
112 }
113 
114 std::string
video_get_docroot(ARDOUR::RCConfiguration * config)115 VideoUtils::video_get_docroot (ARDOUR::RCConfiguration* config)
116 {
117 	if (config->get_video_advanced_setup()) {
118 		return config->get_video_server_docroot();
119 	}
120 #ifndef PLATFORM_WINDOWS
121 	return X_("/");
122 #else
123 	if (harvid_version >= 0x000802) { // 0.8.2
124 		return X_("");
125 	} else {
126 		return X_("C:\\");
127 	}
128 #endif
129 }
130 
131 std::string
video_get_server_url(ARDOUR::RCConfiguration * config)132 VideoUtils::video_get_server_url (ARDOUR::RCConfiguration* config)
133 {
134 	if (config->get_video_advanced_setup()) {
135 		return config->get_video_server_url();
136 	}
137 	return X_("http://127.0.0.1:1554");
138 }
139 
140 
141 std::string
strip_file_extension(const std::string infile)142 VideoUtils::strip_file_extension (const std::string infile)
143 {
144 	std::string rv;
145 	char *ext, *bn = strdup(infile.c_str());
146 	if ((ext=strrchr(bn, '.'))) {
147 		if (!strchr(ext, G_DIR_SEPARATOR)) {
148 			*ext = 0;
149 		}
150 	}
151 	rv = std::string(bn);
152 	free(bn);
153 	return rv;
154 }
155 
156 std::string
get_file_extension(const std::string infile)157 VideoUtils::get_file_extension (const std::string infile)
158 {
159 	std::string rv = "";
160 	char *ext, *bn = strdup(infile.c_str());
161 	if ((ext=strrchr(bn, '.'))) {
162 		if (!strchr(ext, G_DIR_SEPARATOR)) {
163 			rv=std::string(ext+1);
164 		}
165 	}
166 	free(bn);
167 	return rv;
168 }
169 
170 std::string
video_dest_file(const std::string dir,const std::string infile)171 VideoUtils::video_dest_file (const std::string dir, const std::string infile)
172 {
173 	return Glib::build_filename(dir, strip_file_extension(Glib::path_get_basename(infile)) + ".avi");
174 }
175 
176 std::string
video_map_path(std::string server_docroot,std::string filepath)177 VideoUtils::video_map_path (std::string server_docroot, std::string filepath)
178 {
179 	std::string rv = filepath;
180 
181 	/* strip docroot */
182 	if (server_docroot.length() > 0) {
183 		if (rv.compare(0, server_docroot.length(), server_docroot) == 0 ) {
184 			rv = rv.substr(server_docroot.length());
185 		}
186 	}
187 
188 	/* replace all G_DIR_SEPARATOR with '/' */
189 	size_t look_here = 0;
190 	size_t found_here;
191 	while((found_here = rv.find(G_DIR_SEPARATOR, look_here)) != string::npos) {
192 		rv.replace(found_here, 1, "/");
193 		look_here = found_here + 1;
194 	}
195 
196 	CURL *curl;
197 	char *ue;
198 	curl = curl_easy_init();
199 	ue = curl_easy_escape(curl, rv.c_str(),rv.length());
200 	if (ue) {
201 		rv = std::string(ue);
202 		curl_free(ue);
203 	}
204 	curl_easy_cleanup(curl);
205 
206 	return rv;
207 }
208 
209 void
ParseCSV(const std::string & csv,std::vector<std::vector<std::string>> & lines)210 VideoUtils::ParseCSV (const std::string &csv, std::vector<std::vector<std::string> > &lines)
211 {
212 	bool inQuote(false);
213 	bool newLine(false);
214 	std::string field;
215 	lines.clear();
216 	std::vector<std::string> line;
217 
218 	std::string::const_iterator aChar = csv.begin();
219 	while (aChar != csv.end()) {
220 		switch (*aChar) {
221 		case '"':
222 		 newLine = false;
223 		 inQuote = !inQuote;
224 		 break;
225 
226 		case ',':
227 		 newLine = false;
228 		 if (inQuote == true) {
229 				field += *aChar;
230 		 } else {
231 				line.push_back(field);
232 				field.clear();
233 		 }
234 		 break;
235 
236 		case '\n':
237 		case '\r':
238 		 if (inQuote == true) {
239 				field += *aChar;
240 		 } else {
241 				if (newLine == false) {
242 					 line.push_back(field);
243 					 lines.push_back(line);
244 					 field.clear();
245 					 line.clear();
246 					 newLine = true;
247 				}
248 		 }
249 		 break;
250 
251 		default:
252 			 newLine = false;
253 			 field.push_back(*aChar);
254 			 break;
255 		}
256 		aChar++;
257 	}
258 
259 	 if (field.size())
260 		line.push_back(field);
261 
262 	 if (line.size())
263 		lines.push_back(line);
264 }
265 
266 bool
video_query_info(std::string video_server_url,std::string filepath,double & video_file_fps,long long int & video_duration,double & video_start_offset,double & video_aspect_ratio)267 VideoUtils::video_query_info (
268 		std::string video_server_url,
269 		std::string filepath,
270 		double &video_file_fps,
271 		long long int &video_duration,
272 		double &video_start_offset,
273 		double &video_aspect_ratio
274 		)
275 {
276 	char url[2048];
277 
278 	snprintf(url, sizeof(url), "%s%sinfo/?file=%s&format=csv"
279 			, video_server_url.c_str()
280 			, (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
281 			, filepath.c_str());
282 	std::string res = ArdourCurl::http_get (url, false);
283 	if (res.empty ()) {
284 		return false;
285 	}
286 
287 	std::vector<std::vector<std::string> > lines;
288 	ParseCSV(res, lines);
289 
290 	if (lines.empty() || lines.at(0).empty() || lines.at(0).size() != 6) {
291 		return false;
292 	}
293 	if (atoi(lines.at(0).at(0)) != 1) return false; // version
294 	video_start_offset = 0.0;
295 	video_aspect_ratio = string_to<double>(lines.at(0).at(3));
296 	video_file_fps = string_to<double>(lines.at(0).at(4));
297 	video_duration = string_to<int64_t>(lines.at(0).at(5));
298 
299 	if (video_aspect_ratio < 0.01 || video_file_fps < 0.01) {
300 		/* catch errors early, aspect == 0 or fps == 0 will
301 		 * wreak havoc down the road */
302 		return false;
303 	}
304 	return true;
305 }
306 
307 void
video_draw_cross(Glib::RefPtr<Gdk::Pixbuf> img)308 VideoUtils::video_draw_cross (Glib::RefPtr<Gdk::Pixbuf> img)
309 {
310 
311 	int rowstride = img->get_rowstride();
312 	int n_channels = img->get_n_channels();
313 	guchar *pixels, *p;
314 	pixels = img->get_pixels();
315 
316 	int x,y;
317 	int clip_width = img->get_width();
318 	int clip_height = img->get_height();
319 
320 	for (x=0;x<clip_width;x++) {
321 		y = clip_height * x / clip_width;
322 		p = pixels + y * rowstride + x * n_channels;
323 		p[0] = 192; p[1] = 192; p[2] = 192;
324 		if (n_channels>3) p[3] = 255;
325 		p = pixels + y * rowstride + (clip_width-x-1) * n_channels;
326 		p[0] = 192; p[1] = 192; p[2] = 192;
327 		if (n_channels>3) p[3] = 255;
328 	}
329 }
330 
331