1 /*
2  * Copyright (C) 2013-2018 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2013-2019 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 along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 #include <cstdio>
20 #include <cmath>
21 
22 #include <sigc++/bind.h>
23 #include <curl/curl.h>
24 
25 #include <gtkmm/box.h>
26 #include <gtkmm/filechooserdialog.h>
27 #include <gtkmm/scrolledwindow.h>
28 #include <gtkmm/stock.h>
29 
30 #include "pbd/error.h"
31 #include "pbd/convert.h"
32 #include "gtkmm2ext/utils.h"
33 #include "gtkmm2ext/rgb_macros.h"
34 #include "ardour/session_directory.h"
35 #include "ardour/profile.h"
36 #include "ardour/template_utils.h"
37 #include "ardour/session.h"
38 #include "ardour_ui.h"
39 
40 #include "add_video_dialog.h"
41 #include "ardour_http.h"
42 #include "utils_videotl.h"
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 #define PREVIEW_WIDTH (240)
52 #define PREVIEW_HEIGHT (180)
53 
54 #ifndef MIN
55 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
56 #endif
57 
AddVideoDialog(Session * s)58 AddVideoDialog::AddVideoDialog (Session* s)
59 	: ArdourDialog (_("Set Video Track"))
60 	, seek_slider (0,1000,1)
61 	, preview_path ("")
62 	, pi_tcin ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
63 	, pi_tcout ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
64 	, pi_aspect ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
65 	, pi_fps ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
66 	, chooser (FILE_CHOOSER_ACTION_OPEN)
67 	, xjadeo_checkbox (_("Open Video Monitor Window"))
68 	, set_session_fps_checkbox (_("Adjust Session Framerate to Match Video Framerate"))
69 	, harvid_path ("")
70 	, harvid_reset (_("Reload docroot"))
71 	, harvid_list (ListStore::create(harvid_list_columns))
72 	, harvid_list_view (harvid_list)
73 	, show_advanced(false)
74 	, loaded_docroot(false)
75 {
76 	set_session (s);
77 	set_name ("AddVideoDialog");
78 	set_modal (true);
79 	set_skip_taskbar_hint (true);
80 	set_resizable (true);
81 	set_size_request (800, -1);
82 
83 	harvid_initialized = false;
84 	std::string dstdir = video_dest_dir(_session->session_directory().video_path(), video_get_docroot(Config));
85 
86 	/* Harvid Browser */
87 	harvid_list_view.append_column("", pixBufRenderer);
88 	harvid_list_view.append_column(_("Filename"), harvid_list_columns.filename);
89 
90 	harvid_list_view.get_column(0)->set_alignment(0.5);
91 	harvid_list_view.get_column(0)->add_attribute(pixBufRenderer, "stock-id", harvid_list_columns.id);
92 	harvid_list_view.get_column(1)->set_expand(true);
93 	harvid_list_view.get_column(1)->set_sort_column(harvid_list_columns.filename);
94 	harvid_list_view.set_enable_search(true);
95 	harvid_list_view.set_search_column(1);
96 
97 	harvid_list_view.get_selection()->set_mode (SELECTION_SINGLE);
98 
99 	harvid_list_view.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &AddVideoDialog::harvid_list_view_selected));
100 	harvid_list_view.signal_row_activated().connect (sigc::mem_fun (*this, &AddVideoDialog::harvid_list_view_activated));
101 
102 	Gtk::ScrolledWindow *scroll = manage(new ScrolledWindow);
103 	scroll->add(harvid_list_view);
104 	scroll->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
105 
106 	HBox* hbox = manage (new HBox);
107 	harvid_path.set_alignment (0, 0.5);
108 	hbox->pack_start (harvid_path, true, true);
109 	hbox->pack_start (harvid_reset, false, false);
110 
111 	server_index_box.pack_start (*hbox, false, false);
112 	server_index_box.pack_start (*scroll, true, true);
113 
114 	/* file chooser */
115 	chooser.set_border_width (4);
116 	Gtkmm2ext::add_volume_shortcuts (chooser);
117 #ifdef __APPLE__
118 	/* some broken redraw behaviour - this is a bandaid */
119 	chooser.signal_selection_changed().connect (mem_fun (chooser, &Widget::queue_draw));
120 #endif
121 	chooser.set_current_folder (dstdir);
122 
123 	Gtk::FileFilter video_filter;
124 	Gtk::FileFilter matchall_filter;
125 	video_filter.add_custom (FILE_FILTER_FILENAME, mem_fun(*this, &AddVideoDialog::on_video_filter));
126 	video_filter.set_name (_("Video files"));
127 
128 	matchall_filter.add_pattern ("*.*");
129 	matchall_filter.set_name (_("All files"));
130 
131 	chooser.add_filter (video_filter);
132 	chooser.add_filter (matchall_filter);
133 	chooser.set_select_multiple (false);
134 
135 	file_chooser_box.pack_start (chooser, true, true, 0);
136 
137 	/* Global Options*/
138 	Gtk::Label* l;
139 	VBox* options_box = manage (new VBox);
140 
141 	l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
142 	l->set_use_markup ();
143 
144 	options_box->pack_start (*l, false, true, 4);
145 	options_box->pack_start (xjadeo_checkbox, false, true, 2);
146 	options_box->pack_start (set_session_fps_checkbox, false, true, 2);
147 
148 	/* preview pane */
149 	VBox* previewpane = manage (new VBox);
150 	Gtk::Table *table = manage(new Table(5,2));
151 
152 	table->set_row_spacings(2);
153 	table->set_col_spacings(4);
154 
155 	l = manage (new Label (_("<b>Video Information</b>"), Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER, false));
156 	l->set_use_markup ();
157 	table->attach (*l, 0, 2, 0, 1, FILL, FILL);
158 	l = manage (new Label (_("Start:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
159 	table->attach (*l, 0, 1, 1, 2, FILL, FILL);
160 	table->attach (pi_tcin, 1, 2, 1, 2, FILL, FILL);
161 	l = manage (new Label (_("End:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
162 	table->attach (*l, 0, 1, 2, 3, FILL, FILL);
163 	table->attach (pi_tcout, 1, 2, 2, 3, FILL, FILL);
164 	l = manage (new Label (_("Frame rate:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
165 	table->attach (*l, 0, 1, 3, 4, FILL, FILL);
166 	table->attach (pi_fps, 1, 2, 3, 4, FILL, FILL);
167 	l = manage (new Label (_("Aspect Ratio:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
168 	table->attach (*l, 0, 1, 4, 5, FILL, FILL);
169 	table->attach (pi_aspect, 1, 2, 4, 5, FILL, FILL);
170 
171 	preview_image = manage(new Gtk::Image);
172 
173 	imgbuf = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, true, 8, PREVIEW_WIDTH, PREVIEW_HEIGHT);
174 	imgbuf->fill(RGBA_TO_UINT(127,0,0,255));
175 	preview_image->set(imgbuf);
176 	seek_slider.set_draw_value(false);
177 
178 	hbox = manage (new HBox);
179 	hbox->pack_start (*table, true, false);
180 
181 	Gtk::Alignment *al = manage(new Gtk::Alignment());
182 	al->set_size_request(-1, 20);
183 
184 	previewpane->pack_start (*preview_image, false, false);
185 	previewpane->pack_start (seek_slider, false, false);
186 	previewpane->pack_start (*al, false, false);
187 	previewpane->pack_start (*hbox, true, true, 6);
188 
189 	/* Prepare Overall layout */
190 
191 	hbox = manage (new HBox);
192 	hbox->pack_start (browser_container, true, true);
193 	hbox->pack_start (*previewpane, false, false);
194 
195 	get_vbox()->set_spacing (4);
196 	get_vbox()->pack_start (*hbox, true, true);
197 	get_vbox()->pack_start (*options_box, false, false);
198 
199 	/* xjadeo checkbox */
200 	if (ARDOUR_UI::instance()->video_timeline->found_xjadeo()
201 #ifndef PLATFORM_WINDOWS
202 			/* TODO xjadeo setup w/ xjremote */
203 			&& video_get_docroot(Config).size() > 0
204 #endif
205 		 ) {
206 		xjadeo_checkbox.set_active(true);  /* set in ardour_ui.cpp ?! */
207 	} else {
208 		printf("xjadeo was not found or video-server docroot is unset (remote video-server)\n");
209 		xjadeo_checkbox.set_active(false);
210 		xjadeo_checkbox.set_sensitive(false);
211 	}
212 
213 	/* FPS checkbox */
214 	set_session_fps_checkbox.set_active(true);
215 
216 	/* Buttons */
217 	add_button (Stock::CANCEL, RESPONSE_CANCEL);
218 	ok_button = add_button (Stock::OK, RESPONSE_ACCEPT);
219 	//ok_button->set_sensitive(false);
220 	set_action_ok(false);
221 
222 	/* connect signals after eveything has been initialized */
223 	chooser.signal_selection_changed().connect (mem_fun (*this, &AddVideoDialog::file_selection_changed));
224 	chooser.signal_file_activated().connect (mem_fun (*this, &AddVideoDialog::file_activated));
225 	//chooser.signal_update_preview().connect(sigc::mem_fun(*this, &AddVideoDialog::update_preview));
226 	notebook.signal_switch_page().connect (sigc::hide_return (sigc::hide (sigc::hide (sigc::mem_fun (*this, &AddVideoDialog::page_switch)))));
227 	seek_slider.signal_value_changed().connect(sigc::mem_fun(*this, &AddVideoDialog::seek_preview));
228 	harvid_reset.signal_clicked().connect (sigc::mem_fun (*this, &AddVideoDialog::harvid_load_docroot));
229 }
230 
~AddVideoDialog()231 AddVideoDialog::~AddVideoDialog ()
232 {
233 }
234 
235 void
on_show()236 AddVideoDialog::on_show ()
237 {
238 	/* overall layout depending on get_video_advanced_setup() and docroot */
239 	for (int i = notebook.get_n_pages(); i > 0 ; --i) {
240 		notebook.remove_page(i);
241 	}
242 	if (server_index_box.get_parent()) {
243 		server_index_box.get_parent()->remove(server_index_box);
244 	}
245 	if (file_chooser_box.get_parent()) {
246 		file_chooser_box.get_parent()->remove(file_chooser_box);
247 	}
248 	if (notebook.get_parent()) {
249 		notebook.get_parent()->remove(notebook);
250 	}
251 
252 	if (Config->get_video_advanced_setup()) {
253 		notebook.append_page (server_index_box, _("VideoServerIndex"));
254 		if (video_get_docroot(Config).size() > 0) {
255 			notebook.append_page (file_chooser_box, _("Browse Files"));
256 		}
257 		browser_container.pack_start (notebook, true, true);
258 		show_advanced = true;
259 		if (!loaded_docroot) {
260 			harvid_load_docroot();
261 		}
262 	} else {
263 		browser_container.pack_start (file_chooser_box, true, true);
264 		show_advanced = false;
265 		loaded_docroot = false;
266 	}
267 
268 	show_all_children ();
269 
270 	Dialog::on_show ();
271 }
272 
check_video_file_extension(std::string file)273 static bool check_video_file_extension(std::string file)
274 {
275 	const char* suffixes[] = {
276 		".avi"     , ".AVI"     ,
277 		".mov"     , ".MOV"     ,
278 		".ogg"     , ".OGG"     ,
279 		".ogv"     , ".OGV"     ,
280 		".mpg"     , ".MPG"     ,
281 		".mpeg"    , ".MPEG"    ,
282 		".mts"     , ".MTS"     ,
283 		".m2t"     , ".M2T"     ,
284 		".mov"     , ".MOV"     ,
285 		".mp4"     , ".MP4"     ,
286 		".mkv"     , ".MKV"     ,
287 		".vob"     , ".VOB"     ,
288 		".asf"     , ".ASF"     ,
289 		".avs"     , ".AVS"     ,
290 		".dts"     , ".DTS"     ,
291 		".flv"     , ".FLV"     ,
292 		".m4v"     , ".M4V"     ,
293 		".matroska", ".MATROSKA",
294 		".h264"    , ".H264"    ,
295 		".dv"      , ".DV"      ,
296 		".dirac"   , ".DIRAC"   ,
297 		".webm"    , ".WEBM"    ,
298 		".wmv"     , ".WMV"     ,
299 		".ts"      , ".TS"      ,
300 		".mxf"     , ".MXF"     ,
301 	};
302 
303 	for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
304 		if (file.rfind (suffixes[n]) == file.length() - strlen (suffixes[n])) {
305 			return true;
306 		}
307 	}
308 
309 	return false;
310 }
311 
312 bool
on_video_filter(const FileFilter::Info & filter_info)313 AddVideoDialog::on_video_filter (const FileFilter::Info& filter_info)
314 {
315 	return check_video_file_extension(filter_info.filename);
316 }
317 
318 std::string
file_name(bool & local_file)319 AddVideoDialog::file_name (bool &local_file)
320 {
321 	int n = notebook.get_current_page ();
322 	if (n == 1 || !show_advanced) {
323 		local_file = true;
324 		return chooser.get_filename();
325 	} else {
326 		local_file = false;
327 		Gtk::TreeModel::iterator iter = harvid_list_view.get_selection()->get_selected();
328 		if(!iter) return "";
329 
330 		std::string uri = (*iter)[harvid_list_columns.uri];
331 		std::string video_server_url = video_get_server_url(Config);
332 
333 		/* check if video server is running locally */
334 		if (
335 #ifdef PLATFORM_WINDOWS
336 				(video_get_docroot(Config).size() > 0 || !show_advanced)
337 #else
338 				video_get_docroot(Config).size() > 0
339 #endif
340 				&&
341 				(0 == video_server_url.compare (0, 16, "http://127.0.0.1") || 0 == video_server_url.compare (0, 16, "http://localhost"))
342 		   )
343 		{
344 			/* check if the file can be accessed */
345 			int plen;
346 			CURL *curl;
347 			curl = curl_easy_init();
348 			char *ue = curl_easy_unescape(curl, uri.c_str(), uri.length(), &plen);
349 #ifdef PLATFORM_WINDOWS
350 			char *tmp;
351 			while ((tmp = strchr(ue, '/'))) *tmp = '\\';
352 #endif
353 			std::string path = video_get_docroot(Config) + ue;
354 			if (!::access(path.c_str(), R_OK)) {
355 				uri = path;
356 				local_file = true;
357 			}
358 			curl_easy_cleanup(curl);
359 			curl_free(ue);
360 		}
361 		return uri;
362 	}
363 }
364 
365 enum VtlImportOption
import_option()366 AddVideoDialog::import_option ()
367 {
368 	int n = notebook.get_current_page ();
369 	if (n == 0 && show_advanced) { return VTL_IMPORT_NONE; }
370 	return VTL_IMPORT_TRANSCODE;
371 }
372 
373 bool
launch_xjadeo()374 AddVideoDialog::launch_xjadeo ()
375 {
376 	return xjadeo_checkbox.get_active();
377 }
378 
379 bool
auto_set_session_fps()380 AddVideoDialog::auto_set_session_fps ()
381 {
382 	return set_session_fps_checkbox.get_active();
383 }
384 
385 void
clear_preview_image()386 AddVideoDialog::clear_preview_image ()
387 {
388 	imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
389 	video_draw_cross(imgbuf);
390 	preview_image->set(imgbuf);
391 	preview_image->show();
392 }
393 
394 void
set_action_ok(bool yn)395 AddVideoDialog::set_action_ok (bool yn)
396 {
397 	if (yn) {
398 		ok_button->set_sensitive(true);
399 	} else {
400 		preview_path = "";
401 		pi_tcin.set_text("-");
402 		pi_tcout.set_text("-");
403 		pi_aspect.set_text("-");
404 		pi_fps.set_text("-");
405 		ok_button->set_sensitive(false);
406 		clear_preview_image();
407 	}
408 }
409 
410 void
file_selection_changed()411 AddVideoDialog::file_selection_changed ()
412 {
413 	if (chooser.get_filename().size() > 0) {
414 		std::string path = chooser.get_filename();
415 		bool ok =
416 				Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_REGULAR | Glib::FILE_TEST_IS_SYMLINK)
417 				&& !Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_DIR);
418 		set_action_ok(ok);
419 		if (ok) {
420 			seek_slider.set_value(0);
421 			request_preview(video_map_path(video_get_docroot(Config), path));
422 		}
423 	} else {
424 		set_action_ok(false);
425 	}
426 }
427 
428 void
file_activated()429 AddVideoDialog::file_activated ()
430 {
431 	if (chooser.get_filename().size() > 0) {
432 		std::string path = chooser.get_filename();
433 		// TODO check docroot -> set import options
434 		bool ok =
435 				Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_REGULAR | Glib::FILE_TEST_IS_SYMLINK)
436 				&& !Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_DIR);
437 		if (ok) {
438 			Gtk::Dialog::response(RESPONSE_ACCEPT);
439 		}
440 	}
441 }
442 
443 /**** Tree List Interaction ***/
444 
445 void
harvid_list_view_selected()446 AddVideoDialog::harvid_list_view_selected () {
447 	Gtk::TreeModel::iterator iter = harvid_list_view.get_selection()->get_selected();
448 	// TODO check docroot -> set import options, xjadeo
449 	if(!iter) {
450 		set_action_ok(false);
451 		return;
452 	}
453 	if ((std::string)((*iter)[harvid_list_columns.id]) == Stock::DIRECTORY.id) {
454 		set_action_ok(false);
455 	} else {
456 		set_action_ok(true);
457 		seek_slider.set_value(0);
458 		request_preview((*iter)[harvid_list_columns.uri]);
459 	}
460 }
461 
462 void
harvid_list_view_activated(const Gtk::TreeModel::Path & path,Gtk::TreeViewColumn *)463 AddVideoDialog::harvid_list_view_activated (const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn*) {
464 	Gtk::TreeModel::iterator iter = harvid_list->get_iter(path);
465 	if (!iter) return;
466 	std::string type = (*iter)[harvid_list_columns.id];
467 	std::string url = (*iter)[harvid_list_columns.uri];
468 
469 #if 0
470 	printf ("A: %s %s %s\n",
471 			((std::string)((*iter)[harvid_list_columns.id])).c_str(),
472 			((std::string)((*iter)[harvid_list_columns.uri])).c_str(),
473 			((std::string)((*iter)[harvid_list_columns.filename])).c_str());
474 #endif
475 
476 	if (type == Gtk::Stock::DIRECTORY.id) {
477 		harvid_request(url.c_str());
478 	} else {
479 		Gtk::Dialog::response(RESPONSE_ACCEPT);
480 	}
481 }
482 
483 void
harvid_load_docroot()484 AddVideoDialog::harvid_load_docroot() {
485 	set_action_ok(false);
486 	loaded_docroot = true;
487 
488 	std::string video_server_url = video_get_server_url(Config);
489 	char url[2048];
490 	snprintf(url, sizeof(url), "%s%sindex/"
491 		, video_server_url.c_str()
492 		, (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/");
493 	harvid_request(url);
494 	harvid_initialized = true;
495 }
496 
497 bool
page_switch()498 AddVideoDialog::page_switch() {
499 	if (notebook.get_current_page () == 1 || show_advanced) {
500 		file_selection_changed();
501 		return true;
502 	}
503 
504 	if (harvid_initialized) {
505 		harvid_list_view_selected();
506 	} else {
507 		harvid_load_docroot();
508 	}
509 	return true;
510 }
511 
512 /**** Harvid HTTP interface ***/
513 void
harvid_request(std::string u)514 AddVideoDialog::harvid_request(std::string u)
515 {
516 	char url[2048];
517 	int status;
518 	snprintf(url, sizeof(url), "%s?format=csv", u.c_str());
519 
520 	harvid_list->clear();
521 
522 	char* res = ArdourCurl::http_get (url, &status, false);
523 	if (status != 200) {
524 		printf("request failed\n"); // XXX
525 		harvid_path.set_text(" - request failed -");
526 		free(res);
527 		return;
528 	}
529 
530 	/* add up-to-parent */
531 	size_t se = u.find_last_of("/", u.size()-2);
532 	size_t ss = u.find("/index/");
533 	if (se != string::npos && ss != string::npos && se > ss) {
534 		TreeModel::iterator new_row = harvid_list->append();
535 		TreeModel::Row row = *new_row;
536 		row[harvid_list_columns.id      ] = Gtk::Stock::DIRECTORY.id;
537 		row[harvid_list_columns.uri     ] = u.substr(0, se + 1);
538 		row[harvid_list_columns.filename] = X_("..");
539 	}
540 	if (se != string::npos) {
541 		int plen;
542 		std::string path = u.substr(ss + 6);
543 		CURL *curl;
544 		curl = curl_easy_init();
545 		char *ue = curl_easy_unescape(curl, path.c_str(), path.length(), &plen);
546 		harvid_path.set_text(std::string(ue));
547 		curl_easy_cleanup(curl);
548 		curl_free(ue);
549 	} else {
550 		harvid_path.set_text(" ??? ");
551 	}
552 
553 	if (!res) return;
554 
555 	std::vector<std::vector<std::string> > lines;
556 	ParseCSV(std::string(res), lines);
557 	for (std::vector<std::vector<std::string> >::iterator i = lines.begin(); i != lines.end(); ++i) {
558 		TreeModel::iterator new_row = harvid_list->append();
559 		TreeModel::Row row = *new_row;
560 
561 		if (i->at(0) == X_("D")) {
562 			row[harvid_list_columns.id      ] = Gtk::Stock::DIRECTORY.id;
563 			row[harvid_list_columns.uri     ] = i->at(1).c_str();
564 			row[harvid_list_columns.filename] = i->at(2).c_str();
565 		} else {
566 			row[harvid_list_columns.id      ] = Gtk::Stock::MEDIA_PLAY.id;
567 			row[harvid_list_columns.uri     ] = i->at(2).c_str();
568 			row[harvid_list_columns.filename] = i->at(3).c_str();
569 		}
570 	}
571 
572 	free(res);
573 }
574 
575 void
seek_preview()576 AddVideoDialog::seek_preview()
577 {
578 	if (preview_path.size() > 0)
579 		request_preview(preview_path);
580 }
581 
582 void
request_preview(std::string u)583 AddVideoDialog::request_preview(std::string u)
584 {
585 	std::string video_server_url = video_get_server_url(Config);
586 
587 	double video_file_fps;
588 	long long int video_duration;
589 	double video_start_offset;
590 	double video_aspect_ratio;
591 
592 	int clip_width = PREVIEW_WIDTH;
593 	int clip_height = PREVIEW_HEIGHT;
594 	int clip_xoff, clip_yoff;
595 
596 	if (!video_query_info(video_server_url, u,
597 			video_file_fps, video_duration, video_start_offset, video_aspect_ratio))
598 	{
599 		printf("image preview info request failed\n");
600 		// set_action_ok(false); // XXX only if docroot mismatch
601 		preview_path = "";
602 		pi_tcin.set_text("-");
603 		pi_tcout.set_text("-");
604 		pi_aspect.set_text("-");
605 		pi_fps.set_text("-");
606 
607 		clear_preview_image();
608 		return;
609 	}
610 
611 	if ((PREVIEW_WIDTH / (double)PREVIEW_HEIGHT) > video_aspect_ratio ) {
612 		clip_width = MIN(PREVIEW_WIDTH, rint(clip_height * video_aspect_ratio));
613 	} else {
614 		clip_height = MIN(PREVIEW_HEIGHT, rint(clip_width / video_aspect_ratio));
615 	}
616 
617 	pi_tcin.set_text(Timecode::timecode_format_sampletime(
618 				video_start_offset, video_file_fps, video_file_fps, rint(video_file_fps*100.0)==2997));
619 	pi_tcout.set_text(Timecode::timecode_format_sampletime(
620 				video_start_offset + video_duration, video_file_fps, video_file_fps, rint(video_file_fps*100.0)==2997));
621 
622 	/* todo break out this code -> re-usability */
623 	const int arc = rint(video_aspect_ratio*100);
624 
625 	switch (arc) {
626 		case 100:
627 			pi_aspect.set_text(X_(" 1:1"));  // square (large format stills)
628 			break;
629 		case 125:
630 			pi_aspect.set_text(X_(" 5:4"));
631 			break;
632 		case 133:
633 			pi_aspect.set_text(X_(" 4:3"));
634 			break;
635 		case 134:
636 			pi_aspect.set_text(X_(" 47:35")); // 752x560, Super8-scans
637 			break;
638 		case 137:
639 		case 138:
640 			pi_aspect.set_text(X_(" 1.37:1")); // 'Academy ratio' <= 1953
641 			break;
642 		case 141:
643 			pi_aspect.set_text(X_(" 1.41:1")); //  Lichtenberg ratio
644 			break;
645 		case 150:
646 			pi_aspect.set_text(X_(" 3:2"));  // classic 35mm
647 			break;
648 		case 160:
649 			pi_aspect.set_text(X_(" 8:5"));  // credit-card size
650 			break;
651 		case 162:
652 			pi_aspect.set_text(X_(" 16:10")); // golden ratio 1.61803..
653 			break;
654 		case 166:
655 		case 167:
656 			pi_aspect.set_text(X_(" 5:3")); // Super16, EU-widescreen
657 			break;
658 		case 177:
659 		case 178:
660 			pi_aspect.set_text(X_(" 16:9")); // HD video
661 			break;
662 		case 180:
663 			pi_aspect.set_text(X_(" 9:5"));
664 			break;
665 		case 185:
666 			pi_aspect.set_text(X_(" 1.85:1")); // US widescreen cinema
667 			break;
668 		case 200:
669 			pi_aspect.set_text(X_(" 2:1"));
670 			break;
671 		case 239:
672 		case 240:
673 			pi_aspect.set_text(X_(" 2.40:1")); // Anamorphic
674 			break;
675 		case 266:
676 		case 267:
677 			pi_aspect.set_text(X_(" 2.66:1")); // CinemaScope
678 			break;
679 		case 275:
680 			pi_aspect.set_text(X_(" 2.75:1")); // Ultra Panavision
681 			break;
682 		case 400:
683 			pi_aspect.set_text(X_(" 4.00:1")); // three 35mm 1.33:1 polyvision
684 			break;
685 		default:
686 			pi_aspect.set_text(string_compose(X_(" %1:1"), video_aspect_ratio));
687 		break;
688 	}
689 
690 	pi_fps.set_text(string_compose(_(" %1 fps"), video_file_fps));
691 
692 	clip_xoff = (PREVIEW_WIDTH - clip_width)/2;
693 	clip_yoff = (PREVIEW_HEIGHT - clip_height)/2;
694 
695 	char url[2048];
696 	snprintf(url, sizeof(url), "%s%s?frame=%lli&w=%d&h=%di&file=%s&format=rgb"
697 		, video_server_url.c_str()
698 		, (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
699 		, (long long) (video_duration * seek_slider.get_value() / 1000.0)
700 		, clip_width, clip_height, u.c_str());
701 
702 	char* data = ArdourCurl::http_get (url, NULL, false);
703 	if (!data) {
704 		printf("image preview request failed %s\n", url);
705 		imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
706 		video_draw_cross(imgbuf);
707 		preview_path = "";
708 	} else {
709 		Glib::RefPtr<Gdk::Pixbuf> tmp;
710 		tmp = Gdk::Pixbuf::create_from_data ((guint8*) data, Gdk::COLORSPACE_RGB, false, 8, clip_width, clip_height, clip_width*3);
711 		if (clip_width != PREVIEW_WIDTH || clip_height != PREVIEW_HEIGHT) {
712 			imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
713 		}
714 		tmp->copy_area (0, 0, clip_width, clip_height, imgbuf, clip_xoff, clip_yoff);
715 		preview_path = u;
716 		free(data);
717 	}
718 	preview_image->set(imgbuf);
719 	preview_image->show();
720 }
721