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 "utils_videotl.h"
43 using namespace ARDOUR;
44 using namespace VideoUtils;
46 #define PREVIEW_WIDTH (240)
47 #define PREVIEW_HEIGHT (180)
50 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
53 AddVideoDialog::AddVideoDialog (Session* s)
54 : ArdourDialog (_("Set Video Track"))
55 , seek_slider (0,1000,1)
57 , pi_tcin ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
58 , pi_tcout ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
59 , pi_aspect ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
60 , pi_fps ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
61 , chooser (FILE_CHOOSER_ACTION_OPEN)
62 , xjadeo_checkbox (_("Launch External Video Monitor"))
63 , set_session_fps_checkbox (_("Adjust Session Framerate to Match Video Framerate"))
65 , harvid_reset (_("Reload docroot"))
66 , harvid_list (ListStore::create(harvid_list_columns))
67 , harvid_list_view (harvid_list)
68 , show_advanced(false)
69 , loaded_docroot(false)
72 set_name ("AddVideoDialog");
74 set_skip_taskbar_hint (true);
76 set_size_request (800, -1);
78 harvid_initialized = false;
79 std::string dstdir = video_dest_dir(_session->session_directory().video_path(), video_get_docroot(Config));
82 harvid_list_view.append_column("", pixBufRenderer);
83 harvid_list_view.append_column(_("Filename"), harvid_list_columns.filename);
85 harvid_list_view.get_column(0)->set_alignment(0.5);
86 harvid_list_view.get_column(0)->add_attribute(pixBufRenderer, "stock-id", harvid_list_columns.id);
87 harvid_list_view.get_column(1)->set_expand(true);
88 harvid_list_view.get_column(1)->set_sort_column(harvid_list_columns.filename);
89 harvid_list_view.set_enable_search(true);
90 harvid_list_view.set_search_column(1);
92 harvid_list_view.get_selection()->set_mode (SELECTION_SINGLE);
94 harvid_list_view.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &AddVideoDialog::harvid_list_view_selected));
95 harvid_list_view.signal_row_activated().connect (sigc::mem_fun (*this, &AddVideoDialog::harvid_list_view_activated));
97 Gtk::ScrolledWindow *scroll = manage(new ScrolledWindow);
98 scroll->add(harvid_list_view);
99 scroll->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
101 HBox* hbox = manage (new HBox);
102 harvid_path.set_alignment (0, 0.5);
103 hbox->pack_start (harvid_path, true, true);
104 hbox->pack_start (harvid_reset, false, false);
106 server_index_box.pack_start (*hbox, false, false);
107 server_index_box.pack_start (*scroll, true, true);
110 chooser.set_border_width (4);
112 /* some broken redraw behaviour - this is a bandaid */
113 chooser.signal_selection_changed().connect (mem_fun (chooser, &Widget::queue_draw));
115 chooser.set_current_folder (dstdir);
117 Gtk::FileFilter video_filter;
118 Gtk::FileFilter matchall_filter;
119 video_filter.add_custom (FILE_FILTER_FILENAME, mem_fun(*this, &AddVideoDialog::on_video_filter));
120 video_filter.set_name (_("Video files"));
122 matchall_filter.add_pattern ("*.*");
123 matchall_filter.set_name (_("All files"));
125 chooser.add_filter (video_filter);
126 chooser.add_filter (matchall_filter);
127 chooser.set_select_multiple (false);
129 file_chooser_box.pack_start (chooser, true, true, 0);
133 VBox* options_box = manage (new VBox);
135 l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
136 l->set_use_markup ();
138 options_box->pack_start (*l, false, true, 4);
139 options_box->pack_start (xjadeo_checkbox, false, true, 2);
140 options_box->pack_start (set_session_fps_checkbox, false, true, 2);
143 VBox* previewpane = manage (new VBox);
144 Gtk::Table *table = manage(new Table(5,2));
146 table->set_row_spacings(2);
147 table->set_col_spacings(4);
149 l = manage (new Label (_("<b>Video Information</b>"), Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER, false));
150 l->set_use_markup ();
151 table->attach (*l, 0, 2, 0, 1, FILL, FILL);
152 l = manage (new Label (_("Start:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
153 table->attach (*l, 0, 1, 1, 2, FILL, FILL);
154 table->attach (pi_tcin, 1, 2, 1, 2, FILL, FILL);
155 l = manage (new Label (_("End:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
156 table->attach (*l, 0, 1, 2, 3, FILL, FILL);
157 table->attach (pi_tcout, 1, 2, 2, 3, FILL, FILL);
158 l = manage (new Label (_("Frame rate:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
159 table->attach (*l, 0, 1, 3, 4, FILL, FILL);
160 table->attach (pi_fps, 1, 2, 3, 4, FILL, FILL);
161 l = manage (new Label (_("Aspect Ratio:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
162 table->attach (*l, 0, 1, 4, 5, FILL, FILL);
163 table->attach (pi_aspect, 1, 2, 4, 5, FILL, FILL);
165 preview_image = manage(new Gtk::Image);
167 imgbuf = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, true, 8, PREVIEW_WIDTH, PREVIEW_HEIGHT);
168 imgbuf->fill(RGBA_TO_UINT(127,0,0,255));
169 preview_image->set(imgbuf);
170 seek_slider.set_draw_value(false);
172 hbox = manage (new HBox);
173 hbox->pack_start (*table, true, false);
175 Gtk::Alignment *al = manage(new Gtk::Alignment());
176 al->set_size_request(-1, 20);
178 previewpane->pack_start (*preview_image, false, false);
179 previewpane->pack_start (seek_slider, false, false);
180 previewpane->pack_start (*al, false, false);
181 previewpane->pack_start (*hbox, true, true, 6);
183 /* Prepare Overall layout */
185 hbox = manage (new HBox);
186 hbox->pack_start (browser_container, true, true);
187 hbox->pack_start (*previewpane, false, false);
189 get_vbox()->set_spacing (4);
190 get_vbox()->pack_start (*hbox, true, true);
191 get_vbox()->pack_start (*options_box, false, false);
193 /* xjadeo checkbox */
194 if (ARDOUR_UI::instance()->video_timeline->found_xjadeo()
195 /* TODO xjadeo setup w/ xjremote */
196 && video_get_docroot(Config).size() > 0) {
197 xjadeo_checkbox.set_active(true); /* set in ardour_ui.cpp ?! */
199 printf("xjadeo was not found or video-server docroot is unset (remote video-server)\n");
200 xjadeo_checkbox.set_active(false);
201 xjadeo_checkbox.set_sensitive(false);
205 set_session_fps_checkbox.set_active(true);
208 add_button (Stock::CANCEL, RESPONSE_CANCEL);
209 ok_button = add_button (Stock::OK, RESPONSE_ACCEPT);
210 //ok_button->set_sensitive(false);
211 set_action_ok(false);
213 /* connect signals after eveything has been initialized */
214 chooser.signal_selection_changed().connect (mem_fun (*this, &AddVideoDialog::file_selection_changed));
215 chooser.signal_file_activated().connect (mem_fun (*this, &AddVideoDialog::file_activated));
216 //chooser.signal_update_preview().connect(sigc::mem_fun(*this, &AddVideoDialog::update_preview));
217 notebook.signal_switch_page().connect (sigc::hide_return (sigc::hide (sigc::hide (sigc::mem_fun (*this, &AddVideoDialog::page_switch)))));
218 seek_slider.signal_value_changed().connect(sigc::mem_fun(*this, &AddVideoDialog::seek_preview));
219 harvid_reset.signal_clicked().connect (sigc::mem_fun (*this, &AddVideoDialog::harvid_load_docroot));
222 AddVideoDialog::~AddVideoDialog ()
227 AddVideoDialog::on_show ()
229 /* overall layout depending on get_video_advanced_setup() and docroot */
230 for (int i = notebook.get_n_pages(); i > 0 ; --i) {
231 notebook.remove_page(i);
233 if (server_index_box.get_parent()) {
234 server_index_box.get_parent()->remove(server_index_box);
236 if (file_chooser_box.get_parent()) {
237 file_chooser_box.get_parent()->remove(file_chooser_box);
239 if (notebook.get_parent()) {
240 notebook.get_parent()->remove(notebook);
243 if (Config->get_video_advanced_setup()) {
244 notebook.append_page (server_index_box, _("VideoServerIndex"));
245 if (video_get_docroot(Config).size() > 0) {
246 notebook.append_page (file_chooser_box, _("Browse Files"));
248 browser_container.pack_start (notebook, true, true);
249 show_advanced = true;
250 if (!loaded_docroot) {
251 harvid_load_docroot();
254 browser_container.pack_start (file_chooser_box, true, true);
255 show_advanced = false;
256 loaded_docroot = false;
259 show_all_children ();
264 static bool check_video_file_extension(std::string file)
266 const char* suffixes[] = {
281 ".matroska", ".MATROSKA",
284 ".dirac" , ".DIRAC" ,
288 for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
289 if (file.rfind (suffixes[n]) == file.length() - strlen (suffixes[n])) {
298 AddVideoDialog::on_video_filter (const FileFilter::Info& filter_info)
300 return check_video_file_extension(filter_info.filename);
304 AddVideoDialog::file_name (bool &local_file)
306 int n = notebook.get_current_page ();
307 if (n == 1 || !show_advanced) {
309 return chooser.get_filename();
312 Gtk::TreeModel::iterator iter = harvid_list_view.get_selection()->get_selected();
315 std::string uri = (*iter)[harvid_list_columns.uri];
316 std::string video_server_url = video_get_server_url(Config);
318 /* check if video server is running locally */
319 if (video_get_docroot(Config).size() > 0
320 && !video_server_url.compare(0, 16, "http://localhost"))
322 /* check if the file can be accessed */
325 curl = curl_easy_init();
326 char *ue = curl_easy_unescape(curl, uri.c_str(), uri.length(), &plen);
327 #ifdef PLATFORM_WINDOWS
329 while (tmp = strchr(ue, '/')) *tmp = '\\';
331 std::string path = video_get_docroot(Config) + ue;
332 if (!::access(path.c_str(), R_OK)) {
336 curl_easy_cleanup(curl);
344 AddVideoDialog::import_option ()
346 int n = notebook.get_current_page ();
347 if (n == 0 && show_advanced) { return VTL_IMPORT_NONE; }
348 return VTL_IMPORT_TRANSCODE;
352 AddVideoDialog::launch_xjadeo ()
354 return xjadeo_checkbox.get_active();
358 AddVideoDialog::auto_set_session_fps ()
360 return set_session_fps_checkbox.get_active();
364 AddVideoDialog::clear_preview_image ()
366 imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
367 video_draw_cross(imgbuf);
368 preview_image->set(imgbuf);
369 preview_image->show();
373 AddVideoDialog::set_action_ok (bool yn)
376 ok_button->set_sensitive(true);
379 pi_tcin.set_text("-");
380 pi_tcout.set_text("-");
381 pi_aspect.set_text("-");
382 pi_fps.set_text("-");
383 ok_button->set_sensitive(false);
384 clear_preview_image();
389 AddVideoDialog::file_selection_changed ()
391 if (chooser.get_filename().size() > 0) {
392 std::string path = chooser.get_filename();
394 check_video_file_extension(path)
395 && Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_REGULAR | Glib::FILE_TEST_IS_SYMLINK)
396 && !Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_DIR);
399 seek_slider.set_value(0);
400 request_preview(video_map_path(video_get_docroot(Config), path));
403 set_action_ok(false);
408 AddVideoDialog::file_activated ()
410 if (chooser.get_filename().size() > 0) {
411 std::string path = chooser.get_filename();
412 // TODO check docroot -> set import options
414 check_video_file_extension(path)
415 && Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_REGULAR | Glib::FILE_TEST_IS_SYMLINK)
416 && !Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_DIR);
418 Gtk::Dialog::response(RESPONSE_ACCEPT);
423 /**** Tree List Interaction ***/
426 AddVideoDialog::harvid_list_view_selected () {
427 Gtk::TreeModel::iterator iter = harvid_list_view.get_selection()->get_selected();
428 // TODO check docroot -> set import options, xjadeo
430 set_action_ok(false);
433 if ((std::string)((*iter)[harvid_list_columns.id]) == Stock::DIRECTORY.id) {
434 set_action_ok(false);
437 seek_slider.set_value(0);
438 request_preview((*iter)[harvid_list_columns.uri]);
443 AddVideoDialog::harvid_list_view_activated (const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn*) {
444 Gtk::TreeModel::iterator iter = harvid_list->get_iter(path);
446 std::string type = (*iter)[harvid_list_columns.id];
447 std::string url = (*iter)[harvid_list_columns.uri];
450 printf ("A: %s %s %s\n",
451 ((std::string)((*iter)[harvid_list_columns.id])).c_str(),
452 ((std::string)((*iter)[harvid_list_columns.uri])).c_str(),
453 ((std::string)((*iter)[harvid_list_columns.filename])).c_str());
456 if (type == Gtk::Stock::DIRECTORY.id) {
457 harvid_request(url.c_str());
459 Gtk::Dialog::response(RESPONSE_ACCEPT);
464 AddVideoDialog::harvid_load_docroot() {
465 set_action_ok(false);
466 loaded_docroot = true;
468 std::string video_server_url = video_get_server_url(Config);
470 snprintf(url, sizeof(url), "%s%sindex/"
471 , video_server_url.c_str()
472 , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/");
474 harvid_initialized = true;
478 AddVideoDialog::page_switch() {
479 if (notebook.get_current_page () == 1 || show_advanced) {
480 file_selection_changed();
484 if (harvid_initialized) {
485 harvid_list_view_selected();
487 harvid_load_docroot();
492 /**** Harvid HTTP interface ***/
494 AddVideoDialog::harvid_request(std::string u)
498 snprintf(url, sizeof(url), "%s?format=csv", u.c_str());
500 harvid_list->clear();
502 char *res = a3_curl_http_get(url, &status);
504 printf("request failed\n"); // XXX
505 harvid_path.set_text(" - request failed -");
510 /* add up-to-parent */
511 size_t se = u.find_last_of("/", u.size()-2);
512 size_t ss = u.find("/index/");
513 if (se != string::npos && ss != string::npos && se > ss) {
514 TreeModel::iterator new_row = harvid_list->append();
515 TreeModel::Row row = *new_row;
516 row[harvid_list_columns.id ] = Gtk::Stock::DIRECTORY.id;
517 row[harvid_list_columns.uri ] = u.substr(0, se + 1);
518 row[harvid_list_columns.filename] = X_("..");
520 if (se != string::npos) {
522 std::string path = u.substr(ss + 6);
524 curl = curl_easy_init();
525 char *ue = curl_easy_unescape(curl, path.c_str(), path.length(), &plen);
526 harvid_path.set_text(std::string(ue));
527 curl_easy_cleanup(curl);
530 harvid_path.set_text(" ??? ");
535 std::vector<std::vector<std::string> > lines;
536 ParseCSV(std::string(res), lines);
537 for (std::vector<std::vector<std::string> >::iterator i = lines.begin(); i != lines.end(); ++i) {
538 TreeModel::iterator new_row = harvid_list->append();
539 TreeModel::Row row = *new_row;
541 if (i->at(0) == X_("D")) {
542 row[harvid_list_columns.id ] = Gtk::Stock::DIRECTORY.id;
543 row[harvid_list_columns.uri ] = i->at(1).c_str();
544 row[harvid_list_columns.filename] = i->at(2).c_str();
546 row[harvid_list_columns.id ] = Gtk::Stock::MEDIA_PLAY.id;
547 row[harvid_list_columns.uri ] = i->at(2).c_str();
548 row[harvid_list_columns.filename] = i->at(3).c_str();
556 AddVideoDialog::seek_preview()
558 if (preview_path.size() > 0)
559 request_preview(preview_path);
563 AddVideoDialog::request_preview(std::string u)
565 std::string video_server_url = video_get_server_url(Config);
567 double video_file_fps;
568 long long int video_duration;
569 double video_start_offset;
570 double video_aspect_ratio;
572 int clip_width = PREVIEW_WIDTH;
573 int clip_height = PREVIEW_HEIGHT;
574 int clip_xoff, clip_yoff;
576 if (!video_query_info(video_server_url, u,
577 video_file_fps, video_duration, video_start_offset, video_aspect_ratio))
579 printf("image preview info request failed\n");
580 // set_action_ok(false); // XXX only if docroot mismatch
582 pi_tcin.set_text("-");
583 pi_tcout.set_text("-");
584 pi_aspect.set_text("-");
585 pi_fps.set_text("-");
587 clear_preview_image();
591 if ((PREVIEW_WIDTH / (double)PREVIEW_HEIGHT) > video_aspect_ratio ) {
592 clip_width = MIN(PREVIEW_WIDTH, rint(clip_height * video_aspect_ratio));
594 clip_height = MIN(PREVIEW_HEIGHT, rint(clip_width / video_aspect_ratio));
597 pi_tcin.set_text(Timecode::timecode_format_sampletime(
598 video_start_offset, video_file_fps, video_file_fps, rint(video_file_fps*100.0)==2997));
599 pi_tcout.set_text(Timecode::timecode_format_sampletime(
600 video_start_offset + video_duration, video_file_fps, video_file_fps, rint(video_file_fps*100.0)==2997));
602 /* todo break out this code -> re-usability */
603 const int arc = rint(video_aspect_ratio*100);
607 pi_aspect.set_text(X_(" 1:1")); // square (large format stills)
610 pi_aspect.set_text(X_(" 5:4"));
613 pi_aspect.set_text(X_(" 4:3"));
616 pi_aspect.set_text(X_(" 47:35")); // 752x560, Super8-scans
620 pi_aspect.set_text(X_(" 1.37:1")); // 'Academy ratio' <= 1953
623 pi_aspect.set_text(X_(" 1.41:1")); // Lichtenberg ratio
626 pi_aspect.set_text(X_(" 3:2")); // classic 35mm
629 pi_aspect.set_text(X_(" 8:5")); // credit-card size
632 pi_aspect.set_text(X_(" 16:10")); // golden ratio 1.61803..
636 pi_aspect.set_text(X_(" 5:3")); // Super16, EU-widescreen
640 pi_aspect.set_text(X_(" 16:9")); // HD video
643 pi_aspect.set_text(X_(" 9:5"));
646 pi_aspect.set_text(X_(" 1.85:1")); // US widescreen cinema
649 pi_aspect.set_text(X_(" 2:1"));
653 pi_aspect.set_text(X_(" 2.40:1")); // Anamorphic
657 pi_aspect.set_text(X_(" 2.66:1")); // CinemaScope
660 pi_aspect.set_text(X_(" 2.75:1")); // Ultra Panavision
663 pi_aspect.set_text(X_(" 4.00:1")); // three 35mm 1.33:1 polyvision
666 pi_aspect.set_text(string_compose(X_(" %1:1"), video_aspect_ratio));
670 pi_fps.set_text(string_compose(_(" %1 fps"), video_file_fps));
672 clip_xoff = (PREVIEW_WIDTH - clip_width)/2;
673 clip_yoff = (PREVIEW_HEIGHT - clip_height)/2;
676 snprintf(url, sizeof(url), "%s%s?frame=%lli&w=%d&h=%di&file=%s&format=rgb"
677 , video_server_url.c_str()
678 , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
679 , (long long) (video_duration * seek_slider.get_value() / 1000.0)
680 , clip_width, clip_height, u.c_str());
682 char *data = a3_curl_http_get(url, NULL);
684 printf("image preview request failed %s\n", url);
685 imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
686 video_draw_cross(imgbuf);
689 Glib::RefPtr<Gdk::Pixbuf> tmp;
690 tmp = Gdk::Pixbuf::create_from_data ((guint8*) data, Gdk::COLORSPACE_RGB, false, 8, clip_width, clip_height, clip_width*3);
691 if (clip_width != PREVIEW_WIDTH || clip_height != PREVIEW_HEIGHT) {
692 imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
694 tmp->copy_area (0, 0, clip_width, clip_height, imgbuf, clip_xoff, clip_yoff);
698 preview_image->set(imgbuf);
699 preview_image->show();