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