Update codec-quality display when ExportFormat changes
[ardour.git] / gtk2_ardour / add_video_dialog.cc
index a7de9d2c331fca80649c749728b6c04b60dc90e0..5c0db1ff2631b5d6c9f46b41200f2e65c3f74e85 100644 (file)
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
 */
-#ifdef WITH_VIDEOTIMELINE
-
 #include <cstdio>
 #include <cmath>
 
 #include <sigc++/bind.h>
 #include <curl/curl.h>
 
+#include <gtkmm/box.h>
+#include <gtkmm/filechooserdialog.h>
+#include <gtkmm/scrolledwindow.h>
+#include <gtkmm/stock.h>
+
 #include "pbd/error.h"
 #include "pbd/convert.h"
 #include "gtkmm2ext/utils.h"
 #include "ardour/session.h"
 #include "ardour_ui.h"
 
-#include "utils.h"
 #include "add_video_dialog.h"
+#include "ardour_http.h"
 #include "utils_videotl.h"
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace Gtk;
 using namespace std;
 using namespace PBD;
 using namespace ARDOUR;
+using namespace VideoUtils;
 
 #define PREVIEW_WIDTH (240)
 #define PREVIEW_HEIGHT (180)
@@ -61,16 +65,17 @@ AddVideoDialog::AddVideoDialog (Session* s)
        , pi_aspect ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
        , pi_fps ("-", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false)
        , chooser (FILE_CHOOSER_ACTION_OPEN)
-       , xjadeo_checkbox (_("Launch External Video Monitor"))
+       , xjadeo_checkbox (_("Open Video Monitor Window"))
        , set_session_fps_checkbox (_("Adjust Session Framerate to Match Video Framerate"))
        , harvid_path ("")
        , harvid_reset (_("Reload docroot"))
        , harvid_list (ListStore::create(harvid_list_columns))
        , harvid_list_view (harvid_list)
+       , show_advanced(false)
+       , loaded_docroot(false)
 {
        set_session (s);
        set_name ("AddVideoDialog");
-       set_position (Gtk::WIN_POS_MOUSE);
        set_modal (true);
        set_skip_taskbar_hint (true);
        set_resizable (true);
@@ -79,47 +84,37 @@ AddVideoDialog::AddVideoDialog (Session* s)
        harvid_initialized = false;
        std::string dstdir = video_dest_dir(_session->session_directory().video_path(), video_get_docroot(Config));
 
-       if (Config->get_video_advanced_setup()) {
-
-               /* Harvid Browser */
-               harvid_list_view.append_column("", pixBufRenderer);
-               harvid_list_view.append_column(_("Filename"), harvid_list_columns.filename);
-
-               harvid_list_view.get_column(0)->set_alignment(0.5);
-               harvid_list_view.get_column(0)->add_attribute(pixBufRenderer, "stock-id", harvid_list_columns.id);
-               harvid_list_view.get_column(1)->set_expand(true);
-               harvid_list_view.get_column(1)->set_sort_column(harvid_list_columns.filename);
-               harvid_list_view.set_enable_search(true);
-               harvid_list_view.set_search_column(1);
+       /* Harvid Browser */
+       harvid_list_view.append_column("", pixBufRenderer);
+       harvid_list_view.append_column(_("Filename"), harvid_list_columns.filename);
 
-               harvid_list_view.get_selection()->set_mode (SELECTION_SINGLE);
+       harvid_list_view.get_column(0)->set_alignment(0.5);
+       harvid_list_view.get_column(0)->add_attribute(pixBufRenderer, "stock-id", harvid_list_columns.id);
+       harvid_list_view.get_column(1)->set_expand(true);
+       harvid_list_view.get_column(1)->set_sort_column(harvid_list_columns.filename);
+       harvid_list_view.set_enable_search(true);
+       harvid_list_view.set_search_column(1);
 
-               harvid_list_view.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &AddVideoDialog::harvid_list_view_selected));
-               harvid_list_view.signal_row_activated().connect (sigc::mem_fun (*this, &AddVideoDialog::harvid_list_view_activated));
+       harvid_list_view.get_selection()->set_mode (SELECTION_SINGLE);
 
-               VBox* vbox = manage (new VBox);
-               Gtk::ScrolledWindow *scroll = manage(new ScrolledWindow);
-               scroll->add(harvid_list_view);
-               scroll->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
+       harvid_list_view.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &AddVideoDialog::harvid_list_view_selected));
+       harvid_list_view.signal_row_activated().connect (sigc::mem_fun (*this, &AddVideoDialog::harvid_list_view_activated));
 
-               HBox* hbox = manage (new HBox);
-               harvid_path.set_alignment (0, 0.5);
-               hbox->pack_start (harvid_path, true, true);
-               hbox->pack_start (harvid_reset, false, false);
+       Gtk::ScrolledWindow *scroll = manage(new ScrolledWindow);
+       scroll->add(harvid_list_view);
+       scroll->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
 
-               vbox->pack_start (*hbox, false, false);
-               vbox->pack_start (*scroll, true, true);
+       HBox* hbox = manage (new HBox);
+       harvid_path.set_alignment (0, 0.5);
+       hbox->pack_start (harvid_path, true, true);
+       hbox->pack_start (harvid_reset, false, false);
 
-               notebook.append_page (*vbox, _("VideoServerIndex"));
-       } else {
-               /* dummy entry */
-               VBox* vbox = manage (new VBox);
-               notebook.append_page (*vbox, _("VideoServerIndex"));
-       }
+       server_index_box.pack_start (*hbox, false, false);
+       server_index_box.pack_start (*scroll, true, true);
 
        /* file chooser */
        chooser.set_border_width (4);
-#ifdef GTKOSX
+#ifdef __APPLE__
        /* some broken redraw behaviour - this is a bandaid */
        chooser.signal_selection_changed().connect (mem_fun (chooser, &Widget::queue_draw));
 #endif
@@ -137,13 +132,7 @@ AddVideoDialog::AddVideoDialog (Session* s)
        chooser.add_filter (matchall_filter);
        chooser.set_select_multiple (false);
 
-       VBox* vboxfb = manage (new VBox);
-       vboxfb->pack_start (chooser, true, true, 0);
-
-       if (video_get_docroot(Config).size() > 0 &&
-                       Config->get_video_advanced_setup()) {
-               notebook.append_page (*vboxfb, _("Browse Files"));
-       }
+       file_chooser_box.pack_start (chooser, true, true, 0);
 
        /* Global Options*/
        Gtk::Label* l;
@@ -186,7 +175,7 @@ AddVideoDialog::AddVideoDialog (Session* s)
        preview_image->set(imgbuf);
        seek_slider.set_draw_value(false);
 
-       HBox* hbox = manage (new HBox);
+       hbox = manage (new HBox);
        hbox->pack_start (*table, true, false);
 
        Gtk::Alignment *al = manage(new Gtk::Alignment());
@@ -197,24 +186,23 @@ AddVideoDialog::AddVideoDialog (Session* s)
        previewpane->pack_start (*al, false, false);
        previewpane->pack_start (*hbox, true, true, 6);
 
-       /* Overall layout */
+       /* Prepare Overall layout */
+
        hbox = manage (new HBox);
-       if (Config->get_video_advanced_setup()) {
-               hbox->pack_start (notebook, true, true);
-       } else {
-               hbox->pack_start (*vboxfb, true, true);
-       }
+       hbox->pack_start (browser_container, true, true);
        hbox->pack_start (*previewpane, false, false);
 
        get_vbox()->set_spacing (4);
        get_vbox()->pack_start (*hbox, true, true);
        get_vbox()->pack_start (*options_box, false, false);
 
-
        /* xjadeo checkbox */
        if (ARDOUR_UI::instance()->video_timeline->found_xjadeo()
+#ifndef PLATFORM_WINDOWS
                        /* TODO xjadeo setup w/ xjremote */
-                       && video_get_docroot(Config).size() > 0) {
+                       && video_get_docroot(Config).size() > 0
+#endif
+                ) {
                xjadeo_checkbox.set_active(true);  /* set in ardour_ui.cpp ?! */
        } else {
                printf("xjadeo was not found or video-server docroot is unset (remote video-server)\n");
@@ -238,8 +226,6 @@ AddVideoDialog::AddVideoDialog (Session* s)
        notebook.signal_switch_page().connect (sigc::hide_return (sigc::hide (sigc::hide (sigc::mem_fun (*this, &AddVideoDialog::page_switch)))));
        seek_slider.signal_value_changed().connect(sigc::mem_fun(*this, &AddVideoDialog::seek_preview));
        harvid_reset.signal_clicked().connect (sigc::mem_fun (*this, &AddVideoDialog::harvid_load_docroot));
-
-       show_all_children ();
 }
 
 AddVideoDialog::~AddVideoDialog ()
@@ -249,6 +235,38 @@ AddVideoDialog::~AddVideoDialog ()
 void
 AddVideoDialog::on_show ()
 {
+       /* overall layout depending on get_video_advanced_setup() and docroot */
+       for (int i = notebook.get_n_pages(); i > 0 ; --i) {
+               notebook.remove_page(i);
+       }
+       if (server_index_box.get_parent()) {
+               server_index_box.get_parent()->remove(server_index_box);
+       }
+       if (file_chooser_box.get_parent()) {
+               file_chooser_box.get_parent()->remove(file_chooser_box);
+       }
+       if (notebook.get_parent()) {
+               notebook.get_parent()->remove(notebook);
+       }
+
+       if (Config->get_video_advanced_setup()) {
+               notebook.append_page (server_index_box, _("VideoServerIndex"));
+               if (video_get_docroot(Config).size() > 0) {
+                       notebook.append_page (file_chooser_box, _("Browse Files"));
+               }
+               browser_container.pack_start (notebook, true, true);
+               show_advanced = true;
+               if (!loaded_docroot) {
+                       harvid_load_docroot();
+               }
+       } else {
+               browser_container.pack_start (file_chooser_box, true, true);
+               show_advanced = false;
+               loaded_docroot = false;
+       }
+
+       show_all_children ();
+
        Dialog::on_show ();
 }
 
@@ -260,6 +278,9 @@ static bool check_video_file_extension(std::string file)
                ".ogg"     , ".OGG"     ,
                ".ogv"     , ".OGV"     ,
                ".mpg"     , ".MPG"     ,
+               ".mpeg"    , ".MPEG"    ,
+               ".mts"     , ".MTS"     ,
+               ".m2t"     , ".M2T"     ,
                ".mov"     , ".MOV"     ,
                ".mp4"     , ".MP4"     ,
                ".mkv"     , ".MKV"     ,
@@ -274,6 +295,9 @@ static bool check_video_file_extension(std::string file)
                ".dv"      , ".DV"      ,
                ".dirac"   , ".DIRAC"   ,
                ".webm"    , ".WEBM"    ,
+               ".wmv"     , ".WMV"     ,
+               ".ts"      , ".TS"      ,
+               ".mxf"     , ".MXF"     ,
        };
 
        for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
@@ -295,7 +319,7 @@ std::string
 AddVideoDialog::file_name (bool &local_file)
 {
        int n = notebook.get_current_page ();
-       if (n == 1 || ! Config->get_video_advanced_setup()) {
+       if (n == 1 || !show_advanced) {
                local_file = true;
                return chooser.get_filename();
        } else {
@@ -307,14 +331,25 @@ AddVideoDialog::file_name (bool &local_file)
                std::string video_server_url = video_get_server_url(Config);
 
                /* check if video server is running locally */
-               if (video_get_docroot(Config).size() > 0
-                               && !video_server_url.compare(0, 16, "http://localhost"))
+               if (
+#ifdef PLATFORM_WINDOWS
+                               (video_get_docroot(Config).size() > 0 || !show_advanced)
+#else
+                               video_get_docroot(Config).size() > 0
+#endif
+                               &&
+                               (0 == video_server_url.compare (0, 16, "http://127.0.0.1") || 0 == video_server_url.compare (0, 16, "http://localhost"))
+                  )
                {
                        /* check if the file can be accessed */
                        int plen;
                        CURL *curl;
                        curl = curl_easy_init();
                        char *ue = curl_easy_unescape(curl, uri.c_str(), uri.length(), &plen);
+#ifdef PLATFORM_WINDOWS
+                       char *tmp;
+                       while ((tmp = strchr(ue, '/'))) *tmp = '\\';
+#endif
                        std::string path = video_get_docroot(Config) + ue;
                        if (!::access(path.c_str(), R_OK)) {
                                uri = path;
@@ -331,7 +366,7 @@ enum VtlImportOption
 AddVideoDialog::import_option ()
 {
        int n = notebook.get_current_page ();
-       if (n == 0 && Config->get_video_advanced_setup()) { return VTL_IMPORT_NONE; }
+       if (n == 0 && show_advanced) { return VTL_IMPORT_NONE; }
        return VTL_IMPORT_TRANSCODE;
 }
 
@@ -347,6 +382,15 @@ AddVideoDialog::auto_set_session_fps ()
        return set_session_fps_checkbox.get_active();
 }
 
+void
+AddVideoDialog::clear_preview_image ()
+{
+       imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
+       video_draw_cross(imgbuf);
+       preview_image->set(imgbuf);
+       preview_image->show();
+}
+
 void
 AddVideoDialog::set_action_ok (bool yn)
 {
@@ -359,10 +403,7 @@ AddVideoDialog::set_action_ok (bool yn)
                pi_aspect.set_text("-");
                pi_fps.set_text("-");
                ok_button->set_sensitive(false);
-               imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
-               video_draw_cross(imgbuf);
-               preview_image->set(imgbuf);
-               preview_image->show();
+               clear_preview_image();
        }
 }
 
@@ -372,8 +413,7 @@ AddVideoDialog::file_selection_changed ()
        if (chooser.get_filename().size() > 0) {
                std::string path = chooser.get_filename();
                bool ok =
-                               check_video_file_extension(path)
-                               &&  Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_REGULAR | Glib::FILE_TEST_IS_SYMLINK)
+                               Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_REGULAR | Glib::FILE_TEST_IS_SYMLINK)
                                && !Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_DIR);
                set_action_ok(ok);
                if (ok) {
@@ -392,8 +432,7 @@ AddVideoDialog::file_activated ()
                std::string path = chooser.get_filename();
                // TODO check docroot -> set import options
                bool ok =
-                               check_video_file_extension(path)
-                               &&  Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_REGULAR | Glib::FILE_TEST_IS_SYMLINK)
+                               Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_REGULAR | Glib::FILE_TEST_IS_SYMLINK)
                                && !Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_DIR);
                if (ok) {
                        Gtk::Dialog::response(RESPONSE_ACCEPT);
@@ -444,6 +483,7 @@ AddVideoDialog::harvid_list_view_activated (const Gtk::TreeModel::Path& path, Gt
 void
 AddVideoDialog::harvid_load_docroot() {
        set_action_ok(false);
+       loaded_docroot = true;
 
        std::string video_server_url = video_get_server_url(Config);
        char url[2048];
@@ -456,7 +496,7 @@ AddVideoDialog::harvid_load_docroot() {
 
 bool
 AddVideoDialog::page_switch() {
-       if (notebook.get_current_page () == 1 || Config->get_video_advanced_setup()) {
+       if (notebook.get_current_page () == 1 || show_advanced) {
                file_selection_changed();
                return true;
        }
@@ -479,7 +519,7 @@ AddVideoDialog::harvid_request(std::string u)
 
        harvid_list->clear();
 
-       char *res = curl_http_get(url, &status);
+       char* res = ArdourCurl::http_get (url, &status, false);
        if (status != 200) {
                printf("request failed\n"); // XXX
                harvid_path.set_text(" - request failed -");
@@ -563,6 +603,8 @@ AddVideoDialog::request_preview(std::string u)
                pi_tcout.set_text("-");
                pi_aspect.set_text("-");
                pi_fps.set_text("-");
+
+               clear_preview_image();
                return;
        }
 
@@ -657,7 +699,7 @@ AddVideoDialog::request_preview(std::string u)
                , (long long) (video_duration * seek_slider.get_value() / 1000.0)
                , clip_width, clip_height, u.c_str());
 
-       char *data = curl_http_get(url, NULL);
+       char* data = ArdourCurl::http_get (url, NULL, false);
        if (!data) {
                printf("image preview request failed %s\n", url);
                imgbuf->fill(RGBA_TO_UINT(0,0,0,255));
@@ -676,5 +718,3 @@ AddVideoDialog::request_preview(std::string u)
        preview_image->set(imgbuf);
        preview_image->show();
 }
-
-#endif /* WITH_VIDEOTIMELINE */