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.
20 #ifdef WITH_VIDEOTIMELINE
25 #include <sigc++/bind.h>
26 #include <curl/curl.h>
28 #include "pbd/error.h"
29 #include "pbd/convert.h"
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
32 #include "ardour/session_directory.h"
33 #include "ardour/profile.h"
34 #include "ardour/template_utils.h"
35 #include "ardour/session.h"
36 #include "ardour_ui.h"
39 #include "add_video_dialog.h"
40 #include "utils_videotl.h"
46 using namespace ARDOUR;
48 #define PREVIEW_WIDTH (240)
49 #define PREVIEW_HEIGHT (180)
52 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
55 AddVideoDialog::AddVideoDialog (Session* s)
56 : ArdourDialog (_("Set Video Track"))
57 , seek_slider (0,1000,1)
59 , pi_duration ("-", Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false)
60 , pi_aspect ("-", Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false)
61 , pi_fps ("-", Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false)
62 , chooser (FILE_CHOOSER_ACTION_OPEN)
63 , xjadeo_checkbox (_("Launch External Video Monitor"))
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)
71 set_name ("AddVideoDialog");
72 set_position (Gtk::WIN_POS_MOUSE);
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(), Config->get_video_server_docroot());
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);
94 //Glib::RefPtr<Gtk::TreeModelSort> refTreeModelSort = Gtk::TreeModelSort::create(harvid_list_view.get_model());
95 //refTreeModelSort->set_sort_column(harvid_list_columns.filename, Gtk::SORT_ASCENDING);
96 //harvid_list_view.set_model(refTreeModelSort);
98 harvid_list_view.get_selection()->set_mode (SELECTION_SINGLE);
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));
103 VBox* vbox = manage (new VBox);
104 Gtk::ScrolledWindow *scroll = manage(new ScrolledWindow);
105 scroll->add(harvid_list_view);
106 scroll->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
108 HBox* hbox = manage (new HBox);
109 harvid_path.set_alignment (0, 0.5);
110 hbox->pack_start (harvid_path, true, true);
111 hbox->pack_start (harvid_reset, false, false);
113 vbox->pack_start (*hbox, false, false);
114 vbox->pack_start (*scroll, true, true);
116 notebook.append_page (*vbox, _("VideoServerIndex"));
121 chooser.set_border_width (4);
123 /* some broken redraw behaviour - this is a bandaid */
124 chooser.signal_selection_changed().connect (mem_fun (chooser, &Widget::queue_draw));
126 chooser.set_current_folder (dstdir);
128 Gtk::FileFilter video_filter;
129 Gtk::FileFilter matchall_filter;
130 video_filter.add_custom (FILE_FILTER_FILENAME, mem_fun(*this, &AddVideoDialog::on_video_filter));
131 video_filter.set_name (_("Video files"));
133 matchall_filter.add_pattern ("*.*");
134 matchall_filter.set_name (_("All files"));
136 chooser.add_filter (video_filter);
137 chooser.add_filter (matchall_filter);
138 chooser.set_select_multiple (false);
140 /* file import options */
141 import_combo.set_name ("PaddedButton");
142 import_combo.append_text(_("Reference From Current Location"));
143 import_combo.append_text(_("Hardlink or Copy to Session"));
144 import_combo.append_text(_("Transcode to Session"));
145 import_combo.set_active(2);
147 vbox = manage (new VBox);
148 vbox->pack_start (chooser, true, true, 0);
149 vbox->pack_start (import_combo, false, true, 4);
151 if (Config->get_video_server_docroot().size() > 0) {
152 notebook.append_page (*vbox, _("Browse Files"));
157 VBox* options_box = manage (new VBox);
159 l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
160 l->set_use_markup ();
162 options_box->pack_start (*l, false, true, 4);
163 options_box->pack_start (xjadeo_checkbox, false, true, 2);
164 options_box->pack_start (set_session_fps_checkbox, false, true, 2);
167 VBox* previewpane = manage (new VBox);
168 Gtk::Table *table = manage(new Table(4,2));
170 table->set_row_spacings(2);
171 table->set_col_spacings(4);
173 l = manage (new Label (_("<b>Video Information</b>"), Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER, false));
174 l->set_use_markup ();
175 table->attach (*l, 0, 2, 0, 1, FILL, FILL);
176 l = manage (new Label (_("Duration:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
177 table->attach (*l, 0, 1, 1, 2, FILL, FILL);
178 table->attach (pi_duration, 1, 2, 1, 2, FILL, FILL);
179 l = manage (new Label (_("Frame rate:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
180 table->attach (*l, 0, 1, 2, 3, FILL, FILL);
181 table->attach (pi_fps, 1, 2, 2, 3, FILL, FILL);
182 l = manage (new Label (_("Aspect Ratio:"), Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, false));
183 table->attach (*l, 0, 1, 3, 4, FILL, FILL);
184 table->attach (pi_aspect, 1, 2, 3, 4, FILL, FILL);
186 preview_image = manage(new Gtk::Image);
188 imgbuf = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, true, 8, PREVIEW_WIDTH, PREVIEW_HEIGHT);
189 imgbuf->fill(RGBA_TO_UINT(127,0,0,255));
190 preview_image->set(imgbuf);
191 seek_slider.set_draw_value(false);
193 hbox = manage (new HBox);
194 hbox->pack_start (*table, true, false);
196 Gtk::Alignment *al = manage(new Gtk::Alignment());
197 al->set_size_request(-1, 20);
199 previewpane->pack_start (*al, false, false);
200 previewpane->pack_start (*hbox, true, true, 6);
201 previewpane->pack_start (*preview_image, false, false);
202 previewpane->pack_start (seek_slider, false, false);
205 hbox = manage (new HBox);
206 hbox->pack_start (notebook, true, true);
207 hbox->pack_start (*previewpane, false, false);
209 get_vbox()->set_spacing (4);
210 get_vbox()->pack_start (*hbox, true, true);
211 get_vbox()->pack_start (*options_box, false, false);
214 /* xjadeo checkbox */
215 if (ARDOUR_UI::instance()->video_timeline->found_xjadeo()
216 /* TODO xjadeo setup w/ xjremote */
217 && Config->get_video_server_docroot().size() > 0) {
218 xjadeo_checkbox.set_active(true); /* set in ardour_ui.cpp ?! */
220 printf("xjadeo was not found or video-server docroot is unset (remote video-server)\n");
221 xjadeo_checkbox.set_active(false);
222 xjadeo_checkbox.set_sensitive(false);
226 set_session_fps_checkbox.set_active(true);
229 add_button (Stock::CANCEL, RESPONSE_CANCEL);
230 ok_button = add_button (Stock::OK, RESPONSE_ACCEPT);
231 //ok_button->set_sensitive(false);
232 set_action_ok(false);
234 /* connect signals after eveything has been initialized */
235 chooser.signal_selection_changed().connect (mem_fun (*this, &AddVideoDialog::file_selection_changed));
236 chooser.signal_file_activated().connect (mem_fun (*this, &AddVideoDialog::file_activated));
237 //chooser.signal_update_preview().connect(sigc::mem_fun(*this, &AddVideoDialog::update_preview));
238 notebook.signal_switch_page().connect (sigc::hide_return (sigc::hide (sigc::hide (sigc::mem_fun (*this, &AddVideoDialog::page_switch)))));
239 seek_slider.signal_value_changed().connect(sigc::mem_fun(*this, &AddVideoDialog::seek_preview));
240 harvid_reset.signal_clicked().connect (sigc::mem_fun (*this, &AddVideoDialog::harvid_load_docroot));
242 show_all_children ();
245 AddVideoDialog::~AddVideoDialog ()
250 AddVideoDialog::on_show ()
255 static bool check_video_file_extension(std::string file)
257 const char* suffixes[] = {
272 ".matroska", ".MATROSKA",
275 ".dirac" , ".DIRAC" ,
279 for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
280 if (file.rfind (suffixes[n]) == file.length() - strlen (suffixes[n])) {
289 AddVideoDialog::on_video_filter (const FileFilter::Info& filter_info)
291 return check_video_file_extension(filter_info.filename);
295 AddVideoDialog::file_name (bool &local_file)
297 int n = notebook.get_current_page ();
300 return chooser.get_filename();
303 Gtk::TreeModel::iterator iter = harvid_list_view.get_selection()->get_selected();
306 std::string uri = (*iter)[harvid_list_columns.uri];
307 std::string video_server_url = Config->get_video_server_url();
309 /* check if video server is running locally */
310 if (Config->get_video_server_docroot().size() > 0
311 && !video_server_url.compare(0, 16, "http://localhost"))
313 /* check if the file can be accessed */
316 curl = curl_easy_init();
317 char *ue = curl_easy_unescape(curl, uri.c_str(), uri.length(), &plen);
318 std::string path = Config->get_video_server_docroot() + ue;
319 if (!::access(path.c_str(), R_OK)) {
323 curl_easy_cleanup(curl);
331 AddVideoDialog::import_option ()
333 int n = notebook.get_current_page ();
334 if (n == 0) { return VTL_IMPORT_NONE; }
335 int i = import_combo.get_active_row_number();
336 return static_cast<VtlImportOption>(i);
340 AddVideoDialog::launch_xjadeo ()
342 return xjadeo_checkbox.get_active();
346 AddVideoDialog::auto_set_session_fps ()
348 return set_session_fps_checkbox.get_active();
352 AddVideoDialog::set_action_ok (bool yn)
355 ok_button->set_sensitive(true);
358 pi_duration.set_text("-");
359 pi_aspect.set_text("-");
360 pi_fps.set_text("-");
361 ok_button->set_sensitive(false);
362 imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
363 video_draw_cross(imgbuf);
364 preview_image->set(imgbuf);
365 preview_image->show();
370 AddVideoDialog::file_selection_changed ()
372 if (chooser.get_filename().size() > 0) {
373 std::string path = chooser.get_filename();
375 check_video_file_extension(path)
376 && Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_REGULAR | Glib::FILE_TEST_IS_SYMLINK)
377 && !Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_DIR);
380 request_preview(video_map_path(Config->get_video_server_docroot(), path));
383 set_action_ok(false);
388 AddVideoDialog::file_activated ()
390 if (chooser.get_filename().size() > 0) {
391 std::string path = chooser.get_filename();
392 // TODO check docroot -> set import options
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);
398 Gtk::Dialog::response(RESPONSE_ACCEPT);
403 /**** Tree List Interaction ***/
406 AddVideoDialog::harvid_list_view_selected () {
407 Gtk::TreeModel::iterator iter = harvid_list_view.get_selection()->get_selected();
408 // TODO check docroot -> set import options, xjadeo
410 set_action_ok(false);
413 if ((std::string)((*iter)[harvid_list_columns.id]) == Stock::DIRECTORY.id) {
414 set_action_ok(false);
417 request_preview((*iter)[harvid_list_columns.uri]);
422 AddVideoDialog::harvid_list_view_activated (const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn*) {
423 Gtk::TreeModel::iterator iter = harvid_list->get_iter(path);
425 std::string type = (*iter)[harvid_list_columns.id];
426 std::string url = (*iter)[harvid_list_columns.uri];
429 printf ("A: %s %s %s\n",
430 ((std::string)((*iter)[harvid_list_columns.id])).c_str(),
431 ((std::string)((*iter)[harvid_list_columns.uri])).c_str(),
432 ((std::string)((*iter)[harvid_list_columns.filename])).c_str());
435 if (type == Gtk::Stock::DIRECTORY.id) {
436 harvid_request(url.c_str());
438 Gtk::Dialog::response(RESPONSE_ACCEPT);
443 AddVideoDialog::harvid_load_docroot() {
444 set_action_ok(false);
446 std::string video_server_url = Config->get_video_server_url();
448 snprintf(url, sizeof(url), "%s%sindex/"
449 , video_server_url.c_str()
450 , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/");
452 harvid_initialized = true;
456 AddVideoDialog::page_switch() {
457 if (notebook.get_current_page () == 1) {
458 file_selection_changed();
462 if (harvid_initialized) {
463 harvid_list_view_selected();
465 harvid_load_docroot();
470 /**** Harvid HTTP interface ***/
472 AddVideoDialog::harvid_request(std::string u)
476 snprintf(url, sizeof(url), "%s?format=csv", u.c_str());
478 harvid_list->clear();
480 char *res = curl_http_get(url, &status);
482 printf("request failed\n"); // XXX
483 harvid_path.set_text(" - request failed -");
488 /* add up-to-parent */
489 size_t se = u.find_last_of("/", u.size()-2);
490 size_t ss = u.find("/index/");
491 if (se != string::npos && ss != string::npos && se > ss) {
492 TreeModel::iterator new_row = harvid_list->append();
493 TreeModel::Row row = *new_row;
494 row[harvid_list_columns.id ] = Gtk::Stock::DIRECTORY.id;
495 row[harvid_list_columns.uri ] = u.substr(0, se + 1);
496 row[harvid_list_columns.filename] = X_("..");
498 if (se != string::npos) {
500 std::string path = u.substr(ss + 6);
502 curl = curl_easy_init();
503 char *ue = curl_easy_unescape(curl, path.c_str(), path.length(), &plen);
504 harvid_path.set_text(std::string(ue));
505 curl_easy_cleanup(curl);
508 harvid_path.set_text(" ??? ");
513 std::vector<std::vector<std::string> > lines;
514 ParseCSV(std::string(res), lines);
515 for (std::vector<std::vector<std::string> >::iterator i = lines.begin(); i != lines.end(); ++i) {
516 TreeModel::iterator new_row = harvid_list->append();
517 TreeModel::Row row = *new_row;
519 if (i->at(0) == X_("D")) {
520 row[harvid_list_columns.id ] = Gtk::Stock::DIRECTORY.id;
521 row[harvid_list_columns.uri ] = i->at(1).c_str();
522 row[harvid_list_columns.filename] = i->at(2).c_str();
524 row[harvid_list_columns.id ] = Gtk::Stock::MEDIA_PLAY.id;
525 row[harvid_list_columns.uri ] = i->at(2).c_str();
526 row[harvid_list_columns.filename] = i->at(3).c_str();
534 AddVideoDialog::seek_preview()
536 if (preview_path.size() > 0)
537 request_preview(preview_path);
541 AddVideoDialog::request_preview(std::string u)
543 std::string video_server_url = Config->get_video_server_url();
545 double video_file_fps;
546 long long int video_duration;
547 double video_start_offset;
548 double video_aspect_ratio;
550 int clip_width = PREVIEW_WIDTH;
551 int clip_height = PREVIEW_HEIGHT;
552 int clip_xoff, clip_yoff;
554 if (!video_query_info(video_server_url, u,
555 video_file_fps, video_duration, video_start_offset, video_aspect_ratio))
557 printf("image preview info request failed\n");
558 // set_action_ok(false); // XXX only if docroot mismatch
560 pi_duration.set_text("-");
561 pi_aspect.set_text("-");
562 pi_fps.set_text("-");
566 if ((PREVIEW_WIDTH / (double)PREVIEW_HEIGHT) > video_aspect_ratio ) {
567 clip_width = MIN(PREVIEW_WIDTH, rint(clip_height * video_aspect_ratio));
569 clip_height = MIN(PREVIEW_HEIGHT, rint(clip_width / video_aspect_ratio));
572 pi_duration.set_text(string_compose("%1 sec", video_duration / video_file_fps));
573 pi_aspect.set_text(string_compose("%1", video_aspect_ratio));
574 pi_fps.set_text(string_compose("%1 fps", video_file_fps));
576 clip_xoff = (PREVIEW_WIDTH - clip_width)/2;
577 clip_yoff = (PREVIEW_HEIGHT - clip_height)/2;
580 snprintf(url, sizeof(url), "%s%s?frame=%lli&w=%d&h=%di&file=%s&format=rgb"
581 , video_server_url.c_str()
582 , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
583 , (long long) (video_duration * seek_slider.get_value() / 1000.0)
584 , clip_width, clip_height, u.c_str());
586 char *data = curl_http_get(url, NULL);
588 printf("image preview request failed %s\n", url);
589 imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
590 video_draw_cross(imgbuf);
593 Glib::RefPtr<Gdk::Pixbuf> tmp;
594 tmp = Gdk::Pixbuf::create_from_data ((guint8*) data, Gdk::COLORSPACE_RGB, false, 8, clip_width, clip_height, clip_width*3);
595 if (clip_width != PREVIEW_WIDTH || clip_height != PREVIEW_HEIGHT) {
596 imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
598 tmp->copy_area (0, 0, clip_width, clip_height, imgbuf, clip_xoff, clip_yoff);
602 preview_image->set(imgbuf);
603 preview_image->show();
606 #endif /* WITH_VIDEOTIMELINE */