2 Copyright (C) 2010-2013 Paul Davis
3 Author: Robin Gareus <robin@gareus.org>
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.
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.
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.
23 #include <sigc++/bind.h>
24 #include <curl/curl.h>
26 #include "pbd/error.h"
27 #include "pbd/convert.h"
28 #include "gtkmm2ext/utils.h"
29 #include "gtkmm2ext/rgb_macros.h"
30 #include "ardour/session_directory.h"
31 #include "ardour/profile.h"
32 #include "ardour/template_utils.h"
33 #include "ardour/session.h"
34 #include "ardour_ui.h"
36 #include "add_video_dialog.h"
37 #include "ardour_http.h"
38 #include "utils_videotl.h"
44 using namespace ARDOUR;
45 using namespace VideoUtils;
47 #define PREVIEW_WIDTH (240)
48 #define PREVIEW_HEIGHT (180)
51 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
54 AddVideoDialog::AddVideoDialog (Session* s)
55 : ArdourDialog (_("Set Video Track"))
56 , seek_slider (0,1000,1)
58 , pi_tcin ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
59 , pi_tcout ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
60 , pi_aspect ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
61 , pi_fps ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
62 , chooser (FILE_CHOOSER_ACTION_OPEN)
63 , xjadeo_checkbox (_("Open Video Monitor Window"))
64 , set_session_fps_checkbox (_("Adjust Session Framerate to Match Video Framerate"))
66 , harvid_reset (_("Reload docroot"))
67 , harvid_list (ListStore::create(harvid_list_columns))
68 , harvid_list_view (harvid_list)
69 , show_advanced(false)
70 , loaded_docroot(false)
73 set_name ("AddVideoDialog");
75 set_skip_taskbar_hint (true);
77 set_size_request (800, -1);
79 harvid_initialized = false;
80 std::string dstdir = video_dest_dir(_session->session_directory().video_path(), video_get_docroot(Config));
83 harvid_list_view.append_column("", pixBufRenderer);
84 harvid_list_view.append_column(_("Filename"), harvid_list_columns.filename);
86 harvid_list_view.get_column(0)->set_alignment(0.5);
87 harvid_list_view.get_column(0)->add_attribute(pixBufRenderer, "stock-id", harvid_list_columns.id);
88 harvid_list_view.get_column(1)->set_expand(true);
89 harvid_list_view.get_column(1)->set_sort_column(harvid_list_columns.filename);
90 harvid_list_view.set_enable_search(true);
91 harvid_list_view.set_search_column(1);
93 harvid_list_view.get_selection()->set_mode (SELECTION_SINGLE);
95 harvid_list_view.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &AddVideoDialog::harvid_list_view_selected));
96 harvid_list_view.signal_row_activated().connect (sigc::mem_fun (*this, &AddVideoDialog::harvid_list_view_activated));
98 Gtk::ScrolledWindow *scroll = manage(new ScrolledWindow);
99 scroll->add(harvid_list_view);
100 scroll->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
102 HBox* hbox = manage (new HBox);
103 harvid_path.set_alignment (0, 0.5);
104 hbox->pack_start (harvid_path, true, true);
105 hbox->pack_start (harvid_reset, false, false);
107 server_index_box.pack_start (*hbox, false, false);
108 server_index_box.pack_start (*scroll, true, true);
111 chooser.set_border_width (4);
113 /* some broken redraw behaviour - this is a bandaid */
114 chooser.signal_selection_changed().connect (mem_fun (chooser, &Widget::queue_draw));
116 chooser.set_current_folder (dstdir);
118 Gtk::FileFilter video_filter;
119 Gtk::FileFilter matchall_filter;
120 video_filter.add_custom (FILE_FILTER_FILENAME, mem_fun(*this, &AddVideoDialog::on_video_filter));
121 video_filter.set_name (_("Video files"));
123 matchall_filter.add_pattern ("*.*");
124 matchall_filter.set_name (_("All files"));
126 chooser.add_filter (video_filter);
127 chooser.add_filter (matchall_filter);
128 chooser.set_select_multiple (false);
130 file_chooser_box.pack_start (chooser, true, true, 0);
134 VBox* options_box = manage (new VBox);
136 l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
137 l->set_use_markup ();
139 options_box->pack_start (*l, false, true, 4);
140 options_box->pack_start (xjadeo_checkbox, false, true, 2);
141 options_box->pack_start (set_session_fps_checkbox, false, true, 2);
144 VBox* previewpane = manage (new VBox);
145 Gtk::Table *table = manage(new Table(5,2));
147 table->set_row_spacings(2);
148 table->set_col_spacings(4);
150 l = manage (new Label (_("<b>Video Information</b>"), Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER, false));
151 l->set_use_markup ();
152 table->attach (*l, 0, 2, 0, 1, FILL, FILL);
153 l = manage (new Label (_("Start:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
154 table->attach (*l, 0, 1, 1, 2, FILL, FILL);
155 table->attach (pi_tcin, 1, 2, 1, 2, FILL, FILL);
156 l = manage (new Label (_("End:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
157 table->attach (*l, 0, 1, 2, 3, FILL, FILL);
158 table->attach (pi_tcout, 1, 2, 2, 3, FILL, FILL);
159 l = manage (new Label (_("Frame rate:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
160 table->attach (*l, 0, 1, 3, 4, FILL, FILL);
161 table->attach (pi_fps, 1, 2, 3, 4, FILL, FILL);
162 l = manage (new Label (_("Aspect Ratio:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
163 table->attach (*l, 0, 1, 4, 5, FILL, FILL);
164 table->attach (pi_aspect, 1, 2, 4, 5, FILL, FILL);
166 preview_image = manage(new Gtk::Image);
168 imgbuf = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, true, 8, PREVIEW_WIDTH, PREVIEW_HEIGHT);
169 imgbuf->fill(RGBA_TO_UINT(127,0,0,255));
170 preview_image->set(imgbuf);
171 seek_slider.set_draw_value(false);
173 hbox = manage (new HBox);
174 hbox->pack_start (*table, true, false);
176 Gtk::Alignment *al = manage(new Gtk::Alignment());
177 al->set_size_request(-1, 20);
179 previewpane->pack_start (*preview_image, false, false);
180 previewpane->pack_start (seek_slider, false, false);
181 previewpane->pack_start (*al, false, false);
182 previewpane->pack_start (*hbox, true, true, 6);
184 /* Prepare Overall layout */
186 hbox = manage (new HBox);
187 hbox->pack_start (browser_container, true, true);
188 hbox->pack_start (*previewpane, false, false);
190 get_vbox()->set_spacing (4);
191 get_vbox()->pack_start (*hbox, true, true);
192 get_vbox()->pack_start (*options_box, false, false);
194 /* xjadeo checkbox */
195 if (ARDOUR_UI::instance()->video_timeline->found_xjadeo()
196 #ifndef PLATFORM_WINDOWS
197 /* TODO xjadeo setup w/ xjremote */
198 && video_get_docroot(Config).size() > 0
201 xjadeo_checkbox.set_active(true); /* set in ardour_ui.cpp ?! */
203 printf("xjadeo was not found or video-server docroot is unset (remote video-server)\n");
204 xjadeo_checkbox.set_active(false);
205 xjadeo_checkbox.set_sensitive(false);
209 set_session_fps_checkbox.set_active(true);
212 add_button (Stock::CANCEL, RESPONSE_CANCEL);
213 ok_button = add_button (Stock::OK, RESPONSE_ACCEPT);
214 //ok_button->set_sensitive(false);
215 set_action_ok(false);
217 /* connect signals after eveything has been initialized */
218 chooser.signal_selection_changed().connect (mem_fun (*this, &AddVideoDialog::file_selection_changed));
219 chooser.signal_file_activated().connect (mem_fun (*this, &AddVideoDialog::file_activated));
220 //chooser.signal_update_preview().connect(sigc::mem_fun(*this, &AddVideoDialog::update_preview));
221 notebook.signal_switch_page().connect (sigc::hide_return (sigc::hide (sigc::hide (sigc::mem_fun (*this, &AddVideoDialog::page_switch)))));
222 seek_slider.signal_value_changed().connect(sigc::mem_fun(*this, &AddVideoDialog::seek_preview));
223 harvid_reset.signal_clicked().connect (sigc::mem_fun (*this, &AddVideoDialog::harvid_load_docroot));
226 AddVideoDialog::~AddVideoDialog ()
231 AddVideoDialog::on_show ()
233 /* overall layout depending on get_video_advanced_setup() and docroot */
234 for (int i = notebook.get_n_pages(); i > 0 ; --i) {
235 notebook.remove_page(i);
237 if (server_index_box.get_parent()) {
238 server_index_box.get_parent()->remove(server_index_box);
240 if (file_chooser_box.get_parent()) {
241 file_chooser_box.get_parent()->remove(file_chooser_box);
243 if (notebook.get_parent()) {
244 notebook.get_parent()->remove(notebook);
247 if (Config->get_video_advanced_setup()) {
248 notebook.append_page (server_index_box, _("VideoServerIndex"));
249 if (video_get_docroot(Config).size() > 0) {
250 notebook.append_page (file_chooser_box, _("Browse Files"));
252 browser_container.pack_start (notebook, true, true);
253 show_advanced = true;
254 if (!loaded_docroot) {
255 harvid_load_docroot();
258 browser_container.pack_start (file_chooser_box, true, true);
259 show_advanced = false;
260 loaded_docroot = false;
263 show_all_children ();
268 static bool check_video_file_extension(std::string file)
270 const char* suffixes[] = {
288 ".matroska", ".MATROSKA",
291 ".dirac" , ".DIRAC" ,
298 for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
299 if (file.rfind (suffixes[n]) == file.length() - strlen (suffixes[n])) {
308 AddVideoDialog::on_video_filter (const FileFilter::Info& filter_info)
310 return check_video_file_extension(filter_info.filename);
314 AddVideoDialog::file_name (bool &local_file)
316 int n = notebook.get_current_page ();
317 if (n == 1 || !show_advanced) {
319 return chooser.get_filename();
322 Gtk::TreeModel::iterator iter = harvid_list_view.get_selection()->get_selected();
325 std::string uri = (*iter)[harvid_list_columns.uri];
326 std::string video_server_url = video_get_server_url(Config);
328 /* check if video server is running locally */
330 #ifdef PLATFORM_WINDOWS
331 (video_get_docroot(Config).size() > 0 || !show_advanced)
333 video_get_docroot(Config).size() > 0
336 (0 == video_server_url.compare (0, 16, "http://127.0.0.1") || 0 == video_server_url.compare (0, 16, "http://localhost"))
339 /* check if the file can be accessed */
342 curl = curl_easy_init();
343 char *ue = curl_easy_unescape(curl, uri.c_str(), uri.length(), &plen);
344 #ifdef PLATFORM_WINDOWS
346 while ((tmp = strchr(ue, '/'))) *tmp = '\\';
348 std::string path = video_get_docroot(Config) + ue;
349 if (!::access(path.c_str(), R_OK)) {
353 curl_easy_cleanup(curl);
361 AddVideoDialog::import_option ()
363 int n = notebook.get_current_page ();
364 if (n == 0 && show_advanced) { return VTL_IMPORT_NONE; }
365 return VTL_IMPORT_TRANSCODE;
369 AddVideoDialog::launch_xjadeo ()
371 return xjadeo_checkbox.get_active();
375 AddVideoDialog::auto_set_session_fps ()
377 return set_session_fps_checkbox.get_active();
381 AddVideoDialog::clear_preview_image ()
383 imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
384 video_draw_cross(imgbuf);
385 preview_image->set(imgbuf);
386 preview_image->show();
390 AddVideoDialog::set_action_ok (bool yn)
393 ok_button->set_sensitive(true);
396 pi_tcin.set_text("-");
397 pi_tcout.set_text("-");
398 pi_aspect.set_text("-");
399 pi_fps.set_text("-");
400 ok_button->set_sensitive(false);
401 clear_preview_image();
406 AddVideoDialog::file_selection_changed ()
408 if (chooser.get_filename().size() > 0) {
409 std::string path = chooser.get_filename();
411 Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_REGULAR | Glib::FILE_TEST_IS_SYMLINK)
412 && !Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_DIR);
415 seek_slider.set_value(0);
416 request_preview(video_map_path(video_get_docroot(Config), path));
419 set_action_ok(false);
424 AddVideoDialog::file_activated ()
426 if (chooser.get_filename().size() > 0) {
427 std::string path = chooser.get_filename();
428 // TODO check docroot -> set import options
430 Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_REGULAR | Glib::FILE_TEST_IS_SYMLINK)
431 && !Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_DIR);
433 Gtk::Dialog::response(RESPONSE_ACCEPT);
438 /**** Tree List Interaction ***/
441 AddVideoDialog::harvid_list_view_selected () {
442 Gtk::TreeModel::iterator iter = harvid_list_view.get_selection()->get_selected();
443 // TODO check docroot -> set import options, xjadeo
445 set_action_ok(false);
448 if ((std::string)((*iter)[harvid_list_columns.id]) == Stock::DIRECTORY.id) {
449 set_action_ok(false);
452 seek_slider.set_value(0);
453 request_preview((*iter)[harvid_list_columns.uri]);
458 AddVideoDialog::harvid_list_view_activated (const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn*) {
459 Gtk::TreeModel::iterator iter = harvid_list->get_iter(path);
461 std::string type = (*iter)[harvid_list_columns.id];
462 std::string url = (*iter)[harvid_list_columns.uri];
465 printf ("A: %s %s %s\n",
466 ((std::string)((*iter)[harvid_list_columns.id])).c_str(),
467 ((std::string)((*iter)[harvid_list_columns.uri])).c_str(),
468 ((std::string)((*iter)[harvid_list_columns.filename])).c_str());
471 if (type == Gtk::Stock::DIRECTORY.id) {
472 harvid_request(url.c_str());
474 Gtk::Dialog::response(RESPONSE_ACCEPT);
479 AddVideoDialog::harvid_load_docroot() {
480 set_action_ok(false);
481 loaded_docroot = true;
483 std::string video_server_url = video_get_server_url(Config);
485 snprintf(url, sizeof(url), "%s%sindex/"
486 , video_server_url.c_str()
487 , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/");
489 harvid_initialized = true;
493 AddVideoDialog::page_switch() {
494 if (notebook.get_current_page () == 1 || show_advanced) {
495 file_selection_changed();
499 if (harvid_initialized) {
500 harvid_list_view_selected();
502 harvid_load_docroot();
507 /**** Harvid HTTP interface ***/
509 AddVideoDialog::harvid_request(std::string u)
513 snprintf(url, sizeof(url), "%s?format=csv", u.c_str());
515 harvid_list->clear();
517 char* res = ArdourCurl::http_get (url, &status);
519 printf("request failed\n"); // XXX
520 harvid_path.set_text(" - request failed -");
525 /* add up-to-parent */
526 size_t se = u.find_last_of("/", u.size()-2);
527 size_t ss = u.find("/index/");
528 if (se != string::npos && ss != string::npos && se > ss) {
529 TreeModel::iterator new_row = harvid_list->append();
530 TreeModel::Row row = *new_row;
531 row[harvid_list_columns.id ] = Gtk::Stock::DIRECTORY.id;
532 row[harvid_list_columns.uri ] = u.substr(0, se + 1);
533 row[harvid_list_columns.filename] = X_("..");
535 if (se != string::npos) {
537 std::string path = u.substr(ss + 6);
539 curl = curl_easy_init();
540 char *ue = curl_easy_unescape(curl, path.c_str(), path.length(), &plen);
541 harvid_path.set_text(std::string(ue));
542 curl_easy_cleanup(curl);
545 harvid_path.set_text(" ??? ");
550 std::vector<std::vector<std::string> > lines;
551 ParseCSV(std::string(res), lines);
552 for (std::vector<std::vector<std::string> >::iterator i = lines.begin(); i != lines.end(); ++i) {
553 TreeModel::iterator new_row = harvid_list->append();
554 TreeModel::Row row = *new_row;
556 if (i->at(0) == X_("D")) {
557 row[harvid_list_columns.id ] = Gtk::Stock::DIRECTORY.id;
558 row[harvid_list_columns.uri ] = i->at(1).c_str();
559 row[harvid_list_columns.filename] = i->at(2).c_str();
561 row[harvid_list_columns.id ] = Gtk::Stock::MEDIA_PLAY.id;
562 row[harvid_list_columns.uri ] = i->at(2).c_str();
563 row[harvid_list_columns.filename] = i->at(3).c_str();
571 AddVideoDialog::seek_preview()
573 if (preview_path.size() > 0)
574 request_preview(preview_path);
578 AddVideoDialog::request_preview(std::string u)
580 std::string video_server_url = video_get_server_url(Config);
582 double video_file_fps;
583 long long int video_duration;
584 double video_start_offset;
585 double video_aspect_ratio;
587 int clip_width = PREVIEW_WIDTH;
588 int clip_height = PREVIEW_HEIGHT;
589 int clip_xoff, clip_yoff;
591 if (!video_query_info(video_server_url, u,
592 video_file_fps, video_duration, video_start_offset, video_aspect_ratio))
594 printf("image preview info request failed\n");
595 // set_action_ok(false); // XXX only if docroot mismatch
597 pi_tcin.set_text("-");
598 pi_tcout.set_text("-");
599 pi_aspect.set_text("-");
600 pi_fps.set_text("-");
602 clear_preview_image();
606 if ((PREVIEW_WIDTH / (double)PREVIEW_HEIGHT) > video_aspect_ratio ) {
607 clip_width = MIN(PREVIEW_WIDTH, rint(clip_height * video_aspect_ratio));
609 clip_height = MIN(PREVIEW_HEIGHT, rint(clip_width / video_aspect_ratio));
612 pi_tcin.set_text(Timecode::timecode_format_sampletime(
613 video_start_offset, video_file_fps, video_file_fps, rint(video_file_fps*100.0)==2997));
614 pi_tcout.set_text(Timecode::timecode_format_sampletime(
615 video_start_offset + video_duration, video_file_fps, video_file_fps, rint(video_file_fps*100.0)==2997));
617 /* todo break out this code -> re-usability */
618 const int arc = rint(video_aspect_ratio*100);
622 pi_aspect.set_text(X_(" 1:1")); // square (large format stills)
625 pi_aspect.set_text(X_(" 5:4"));
628 pi_aspect.set_text(X_(" 4:3"));
631 pi_aspect.set_text(X_(" 47:35")); // 752x560, Super8-scans
635 pi_aspect.set_text(X_(" 1.37:1")); // 'Academy ratio' <= 1953
638 pi_aspect.set_text(X_(" 1.41:1")); // Lichtenberg ratio
641 pi_aspect.set_text(X_(" 3:2")); // classic 35mm
644 pi_aspect.set_text(X_(" 8:5")); // credit-card size
647 pi_aspect.set_text(X_(" 16:10")); // golden ratio 1.61803..
651 pi_aspect.set_text(X_(" 5:3")); // Super16, EU-widescreen
655 pi_aspect.set_text(X_(" 16:9")); // HD video
658 pi_aspect.set_text(X_(" 9:5"));
661 pi_aspect.set_text(X_(" 1.85:1")); // US widescreen cinema
664 pi_aspect.set_text(X_(" 2:1"));
668 pi_aspect.set_text(X_(" 2.40:1")); // Anamorphic
672 pi_aspect.set_text(X_(" 2.66:1")); // CinemaScope
675 pi_aspect.set_text(X_(" 2.75:1")); // Ultra Panavision
678 pi_aspect.set_text(X_(" 4.00:1")); // three 35mm 1.33:1 polyvision
681 pi_aspect.set_text(string_compose(X_(" %1:1"), video_aspect_ratio));
685 pi_fps.set_text(string_compose(_(" %1 fps"), video_file_fps));
687 clip_xoff = (PREVIEW_WIDTH - clip_width)/2;
688 clip_yoff = (PREVIEW_HEIGHT - clip_height)/2;
691 snprintf(url, sizeof(url), "%s%s?frame=%lli&w=%d&h=%di&file=%s&format=rgb"
692 , video_server_url.c_str()
693 , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
694 , (long long) (video_duration * seek_slider.get_value() / 1000.0)
695 , clip_width, clip_height, u.c_str());
697 char* data = ArdourCurl::http_get (url, NULL);
699 printf("image preview request failed %s\n", url);
700 imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
701 video_draw_cross(imgbuf);
704 Glib::RefPtr<Gdk::Pixbuf> tmp;
705 tmp = Gdk::Pixbuf::create_from_data ((guint8*) data, Gdk::COLORSPACE_RGB, false, 8, clip_width, clip_height, clip_width*3);
706 if (clip_width != PREVIEW_WIDTH || clip_height != PREVIEW_HEIGHT) {
707 imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
709 tmp->copy_area (0, 0, clip_width, clip_height, imgbuf, clip_xoff, clip_yoff);
713 preview_image->set(imgbuf);
714 preview_image->show();