2 Copyright (C) 2010 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
23 #include <sigc++/bind.h>
24 #include "ardour/tempo.h"
26 #include "pbd/file_utils.h"
27 #include "ardour/session_directory.h"
29 #include "ardour_ui.h"
30 #include "public_editor.h"
31 #include "gui_thread.h"
33 #include "canvas_impl.h"
34 #include "simpleline.h"
35 #include "utils_videotl.h"
36 #include "rgb_macros.h"
37 #include "video_timeline.h"
39 #include <gtkmm2ext/utils.h>
41 #include <curl/curl.h>
46 using namespace ARDOUR;
48 using namespace Timecode;
50 VideoTimeLine::VideoTimeLine (PublicEditor *ed, ArdourCanvas::Group *vbg, int initial_height)
52 , videotl_bar_group(vbg)
53 , bar_height(initial_height)
55 video_start_offset = 0L;
59 auto_set_session_fps = false;
60 video_offset_lock = false;
61 video_aspect_ratio = 4.0/3.0;
62 open_video_monitor_dialog = 0;
63 Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoTimeLine::parameter_changed, this, _1), gui_context());
64 video_server_url = video_get_server_url(Config);
65 server_docroot = video_get_docroot(Config);
68 video_file_fps = 25.0;
71 reopen_vmonitor=false;
74 VtlUpdate.connect (*this, invalidator (*this), boost::bind (&PublicEditor::queue_visual_videotimeline_update, editor), gui_context());
75 GuiUpdate.connect (*this, invalidator (*this), boost::bind (&VideoTimeLine::gui_update, this, _1), gui_context());
78 VideoTimeLine::~VideoTimeLine ()
83 /* close and save settings */
85 VideoTimeLine::save_session ()
91 LocaleGuard lg (X_("POSIX"));
93 bool is_dirty = false;
95 XMLNode* prev = _session->extra_xml (X_("Videomonitor"));
97 /* remember if vmonitor was open.. */
98 XMLNode* node = new XMLNode(X_("Videomonitor"));
100 node->add_property (X_("active"), (vmonitor && vmonitor->is_started())?"yes":"no");
101 if (!prev || !(prev->property (X_("active")) && prev->property (X_("active"))->value() == node->property(X_("active"))->value()) ){
102 _session->add_extra_xml (*node);
103 is_dirty=true; // TODO not if !prev && value==default
107 node = _session->extra_xml (X_("Videotimeline"));
110 if (!(node->property(X_("id")) && node->property(X_("id"))->value() == id().to_s())) {
111 node->add_property (X_("id"), id().to_s());
116 /* remember timeline height.. */
118 int h = editor->get_videotl_bar_height();
119 if (!(node->property(X_("Height")) && atoi(node->property(X_("Height"))->value().c_str())==h)) {
120 node->add_property (X_("Height"), h);
125 /* save video-offset-lock */
127 if (!(node->property(X_("VideoOffsetLock")) && atoi(node->property(X_("VideoOffsetLock"))->value().c_str())==video_offset_lock)) {
128 node->add_property (X_("VideoOffsetLock"), video_offset_lock?X_("1"):X_("0"));
132 /* save video-offset */
134 if (!(node->property(X_("VideoOffset")) && atoll(node->property(X_("VideoOffset"))->value().c_str())==video_offset)) {
135 node->add_property (X_("VideoOffset"), video_offset);
140 /* save 'auto_set_session_fps' */
142 if (!(node->property(X_("AutoFPS")) && atoi(node->property(X_("AutoFPS"))->value().c_str())==auto_set_session_fps)) {
143 node->add_property (X_("AutoFPS"), auto_set_session_fps?X_("1"):X_("0"));
148 _session->set_dirty ();
152 /* close and save settings */
154 VideoTimeLine::close_session ()
156 if (video_duration == 0) {
159 close_video_monitor();
165 GuiUpdate("set-xjadeo-sensitive-off");
168 /** load settings from session */
170 VideoTimeLine::set_session (ARDOUR::Session *s)
172 SessionHandlePtr::set_session (s);
173 if (!_session) { return ; }
175 LocaleGuard lg (X_("POSIX"));
177 XMLNode* node = _session->extra_xml (X_("Videotimeline"));
179 if (!node || !node->property (X_("Filename"))) {
184 ARDOUR_UI::instance()->start_video_server((Gtk::Window*)0, false);
188 const XMLProperty* proph = node->property (X_("Height"));
190 editor->set_video_timeline_height(atoi(proph->value().c_str()));
192 #if 0 /* TODO THINK: set FPS first time only ?! */
193 const XMLProperty* propasfps = node->property (X_("AutoFPS"));
195 auto_set_session_fps = atoi(propasfps->value().c_str())?true:false;
199 const XMLProperty* propoffset = node->property (X_("VideoOffset"));
201 video_offset = atoll(propoffset->value().c_str());
202 video_offset_p = video_offset;
205 const XMLProperty* proplock = node->property (X_("VideoOffsetLock"));
207 video_offset_lock = atoi(proplock->value().c_str())?true:false;
210 const XMLProperty* localfile = node->property (X_("LocalFile"));
212 local_file = atoi(localfile->value().c_str())?true:false;
215 const XMLProperty* propf = node->property (X_("Filename"));
216 video_file_info(propf->value(), local_file);
219 node = _session->extra_xml (X_("Videomonitor"));
221 const XMLProperty* prop = node->property (X_("active"));
222 if (prop && prop->value() == "yes" && found_xjadeo() && !video_filename.empty() && local_file) {
223 open_video_monitor(false);
227 _session->register_with_memento_command_factory(id(), this);
228 _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoTimeLine::parameter_changed, this, _1), gui_context());
232 VideoTimeLine::save_undo ()
234 video_offset_p = video_offset;
238 VideoTimeLine::set_state (const XMLNode& node, int /*version*/)
240 LocaleGuard lg (X_("POSIX"));
241 const XMLProperty* propoffset = node.property (X_("VideoOffset"));
243 video_offset = atoll(propoffset->value().c_str());
245 ARDOUR_UI::instance()->flush_videotimeline_cache(true);
250 VideoTimeLine::get_state ()
252 XMLNode* node = new XMLNode (X_("Videotimeline"));
253 LocaleGuard lg (X_("POSIX"));
254 node->add_property (X_("VideoOffset"), video_offset_p);
259 VideoTimeLine::remove_frames ()
261 for (VideoFrames::iterator i = video_frames.begin(); i != video_frames.end(); ++i ) {
262 VideoImageFrame *frame = (*i);
266 video_frames.clear();
270 VideoTimeLine::get_video_frame (framepos_t vfn, int cut, int rightend)
273 for (VideoFrames::iterator i = video_frames.begin(); i != video_frames.end(); ++i) {
274 VideoImageFrame *frame = (*i);
275 if (abs(frame->get_video_frame_number()-vfn)<=cut
276 && frame->get_rightend() == rightend) { return frame; }
282 VideoTimeLine::get_apv()
284 // XXX: dup code - TODO use this fn in update_video_timeline()
285 float apv = -1; /* audio frames per video frame; */
286 if (!_session) return apv;
288 if (_session->config.get_use_video_file_fps()) {
289 if (video_file_fps == 0 ) return apv;
291 if (_session->timecode_frames_per_second() == 0 ) return apv;
294 if (_session->config.get_videotimeline_pullup()) {
295 apv = _session->frame_rate();
297 apv = _session->nominal_frame_rate();
299 if (_session->config.get_use_video_file_fps()) {
300 apv /= video_file_fps;
302 apv /= _session->timecode_frames_per_second();
308 VideoTimeLine::update_video_timeline()
310 if (!_session) return;
312 if (_session->config.get_use_video_file_fps()) {
313 if (video_file_fps == 0 ) return;
315 if (_session->timecode_frames_per_second() == 0 ) return;
318 double frames_per_unit = editor->unit_to_frame(1.0);
319 framepos_t leftmost_frame = editor->leftmost_position();
322 * 1) calculate how many frames there should be in current zoom (plus 1 page on each side)
323 * 2) calculate first frame and distance between video-frames (according to zoom)
324 * 3) destroy/add frames
325 * 4) reposition existing frames
326 * 5) assign framenumber to frames -> request/decode video.
329 /* video-file and session properties */
330 double display_vframe_width; /* unit: pixels ; width of one thumbnail in the timeline */
331 float apv; /* audio frames per video frame; */
332 framepos_t leftmost_video_frame; /* unit: video-frame number ; temporary var -> vtl_start */
334 /* variables needed to render videotimeline -- what needs to computed first */
335 framepos_t vtl_start; /* unit: audio-frames ; first displayed video-frame */
336 framepos_t vtl_dist; /* unit: audio-frames ; distance between displayed video-frames */
337 unsigned int visible_video_frames; /* number of frames that fit on current canvas */
339 if (_session->config.get_videotimeline_pullup()) {
340 apv = _session->frame_rate();
342 apv = _session->nominal_frame_rate();
344 if (_session->config.get_use_video_file_fps()) {
345 apv /= video_file_fps;
347 apv /= _session->timecode_frames_per_second();
350 display_vframe_width = bar_height * video_aspect_ratio;
352 if (apv > frames_per_unit * display_vframe_width) {
353 /* high-zoom: need space between successive video-frames */
354 vtl_dist = rint(apv);
356 /* continous timeline: skip video-frames */
357 vtl_dist = ceil(display_vframe_width * frames_per_unit / apv) * apv;
360 assert (vtl_dist > 0);
363 #define GOFFSET (video_offset)
365 leftmost_video_frame = floor (floor((leftmost_frame - video_start_offset - GOFFSET ) / vtl_dist) * vtl_dist / apv);
367 vtl_start = rint (GOFFSET + video_start_offset + leftmost_video_frame * apv);
368 visible_video_frames = 2 + ceil(editor->current_page_frames() / vtl_dist); /* +2 left+right partial frames */
370 /* expand timeline (cache next/prev page images) */
371 vtl_start -= visible_video_frames * vtl_dist;
372 visible_video_frames *=3;
374 if (vtl_start < GOFFSET ) {
375 visible_video_frames += ceil(vtl_start/vtl_dist);
379 /* apply video-file constraints */
380 if (vtl_start > video_start_offset + video_duration + GOFFSET ) {
381 visible_video_frames = 0;
383 /* TODO optimize: compute rather than iterate */
384 while (visible_video_frames > 0 && vtl_start + (visible_video_frames-1) * vtl_dist >= video_start_offset + video_duration + GOFFSET) {
385 --visible_video_frames;
393 while (video_frames.size() < visible_video_frames) {
394 VideoImageFrame *frame;
395 frame = new VideoImageFrame(*editor, *videotl_bar_group, display_vframe_width, bar_height, video_server_url, translated_filename());
396 frame->ImgChanged.connect (*this, invalidator (*this), boost::bind (&PublicEditor::queue_visual_videotimeline_update, editor), gui_context());
397 video_frames.push_back(frame);
400 VideoFrames outdated_video_frames;
401 std::list<int> remaining;
403 outdated_video_frames = video_frames;
406 /* when zoomed out, ignore shifts by +-1 frame
407 * which can occur due to rounding errors when
408 * scrolling to a new leftmost-audio frame.
411 if (vtl_dist/apv < 3.0) cut =0;
416 for (unsigned int vfcount=0; vfcount < visible_video_frames; ++vfcount){
417 framepos_t vfpos = vtl_start + vfcount * vtl_dist; /* unit: audio-frames */
418 framepos_t vframeno = rint ( (vfpos - GOFFSET) / apv); /* unit: video-frames */
419 vfpos = (vframeno * apv ) + GOFFSET; /* audio-frame corresponding to /rounded/ video-frame */
421 int rightend = -1; /* unit: pixels */
422 if (vfpos + vtl_dist > video_start_offset + video_duration + GOFFSET) {
423 rightend = display_vframe_width * (video_start_offset + video_duration + GOFFSET - vfpos) / vtl_dist;
424 //printf("lf(e): %lu\n", vframeno); // XXX
426 VideoImageFrame * frame = get_video_frame(vframeno, cut, rightend);
428 frame->set_position(vfpos-leftmost_frame);
429 outdated_video_frames.remove(frame);
431 remaining.push_back(vfcount);
435 for (VideoFrames::iterator i = outdated_video_frames.begin(); i != outdated_video_frames.end(); ++i ) {
436 VideoImageFrame *frame = (*i);
437 if (remaining.empty()) {
438 frame->set_position(-2 * vtl_dist); /* move off screen */
440 int vfcount=remaining.front();
441 remaining.pop_front();
442 framepos_t vfpos = vtl_start + vfcount * vtl_dist; /* unit: audio-frames */
443 framepos_t vframeno = rint ((vfpos - GOFFSET) / apv); /* unit: video-frames */
444 int rightend = -1; /* unit: pixels */
445 if (vfpos + vtl_dist > video_start_offset + video_duration + GOFFSET) {
446 rightend = display_vframe_width * (video_start_offset + video_duration + GOFFSET - vfpos) / vtl_dist;
447 //printf("lf(n): %lu\n", vframeno); // XXX
449 frame->set_position(vfpos-leftmost_frame);
450 frame->set_videoframe(vframeno, rightend);
456 VideoTimeLine::translated_filename ()
459 return video_filename;
461 return video_map_path(server_docroot, video_filename);
466 VideoTimeLine::video_file_info (std::string filename, bool local)
470 if (filename.at(0) == G_DIR_SEPARATOR || !local_file) {
471 video_filename = filename;
473 video_filename = Glib::build_filename (_session->session_directory().video_path(), filename);
476 long long int _duration;
477 double _start_offset;
479 if (!video_query_info(
480 video_server_url, translated_filename(),
481 video_file_fps, _duration, _start_offset, video_aspect_ratio)) {
482 warning << _("Parsing video file info failed. Is the Video Server running? Is the file readable by the Video Server? Does the docroot match? Is it a video file?") << endmsg;
485 video_duration = _duration * _session->nominal_frame_rate() / video_file_fps;
486 video_start_offset = _start_offset * _session->nominal_frame_rate();
488 if (auto_set_session_fps && video_file_fps != _session->timecode_frames_per_second()) {
489 switch ((int)floorf(video_file_fps*1000.0)) {
491 _session->config.set_timecode_format(timecode_23976);
494 _session->config.set_timecode_format(timecode_24);
498 _session->config.set_timecode_format(timecode_24976);
501 _session->config.set_timecode_format(timecode_25);
504 _session->config.set_timecode_format(timecode_2997drop);
507 _session->config.set_timecode_format(timecode_30);
510 _session->config.set_timecode_format(timecode_5994);
513 _session->config.set_timecode_format(timecode_60);
516 warning << _("Failed to set session-framerate: ") << video_file_fps << _(" does not have a corresponding option setting in Ardour.") << endmsg; /* TODO: gettext arg */
519 _session->config.set_video_pullup(0); /* TODO only set if set_timecode_format() was successful ?!*/
521 if (video_file_fps != _session->timecode_frames_per_second()) {
522 warning << _("Video file's framerate is not equal to Ardour session timecode's framerate: ")
523 << video_file_fps << _(" vs ") << _session->timecode_frames_per_second() << endmsg;
525 flush_local_cache ();
527 _session->maybe_update_session_range(
528 std::max(get_offset(), (ARDOUR::frameoffset_t) 0),
529 std::max(get_offset() + get_duration(), (ARDOUR::frameoffset_t) 0)
532 if (found_xjadeo() && local_file) {
533 GuiUpdate("set-xjadeo-sensitive-on");
534 if (vmonitor && vmonitor->is_started()) {
535 vmonitor->set_fps(video_file_fps);
536 vmonitor->open(video_filename);
538 } else if (!local_file) {
539 #if 1 /* temp debug/devel message */
540 // TODO - call xjremote remotely.
541 printf("the given video file can not be accessed on localhost, video monitoring is not currently supported for this case\n");
542 GuiUpdate("set-xjadeo-sensitive-off");
550 VideoTimeLine::check_server ()
554 snprintf(url, sizeof(url), "%s%sstatus"
555 , video_server_url.c_str()
556 , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
558 char *res=curl_http_get(url, NULL);
560 if (strstr(res, "status: ok, online.")) { ok = true; }
567 VideoTimeLine::gui_update(std::string const & t) {
568 /* this is to be called via GuiUpdate() only. */
569 ENSURE_GUI_THREAD (*this, &VideoTimeLine::queue_visual_videotimeline_update)
570 if (t == "videotimeline-update") {
571 editor->queue_visual_videotimeline_update();
572 } else if (t == "set-xjadeo-active-off") {
573 editor->toggle_xjadeo_proc(0);
574 } else if (t == "set-xjadeo-active-on") {
575 editor->toggle_xjadeo_proc(1);
576 } else if (t == "set-xjadeo-sensitive-on") {
577 editor->set_xjadeo_sensitive(true);
578 } else if (t == "set-xjadeo-sensitive-off") {
579 editor->toggle_xjadeo_proc(0);
580 //close_video_monitor();
581 editor->set_xjadeo_sensitive(false);
586 VideoTimeLine::set_height (int height) {
592 VideoTimeLine::vmon_update () {
593 if (vmonitor && vmonitor->is_started()) {
594 vmonitor->set_offset( GOFFSET); // TODO proper re-init xjadeo w/o restart not just offset.
599 VideoTimeLine::flush_local_cache () {
605 VideoTimeLine::flush_cache () {
608 snprintf(url, sizeof(url), "%s%sadmin/flush_cache"
609 , video_server_url.c_str()
610 , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
612 char *res=curl_http_get(url, NULL);
616 if (vmonitor && vmonitor->is_started()) {
617 reopen_vmonitor=true;
620 video_file_info(video_filename, local_file);
625 VideoTimeLine::parameter_changed (std::string const & p)
627 if (p == "video-server-url") {
628 set_video_server_url (video_get_server_url(Config));
629 } else if (p == "video-server-docroot") {
630 set_video_server_docroot (video_get_docroot(Config));
631 } else if (p == "video-advanced-setup") {
632 set_video_server_url (video_get_server_url(Config));
633 set_video_server_docroot (video_get_docroot(Config));
635 if (p == "use-video-file-fps" || p == "videotimeline-pullup" ) { /* session->config parameter */
641 VideoTimeLine::set_video_server_url(std::string vsu) {
642 flush_local_cache ();
643 video_server_url = vsu;
648 VideoTimeLine::set_video_server_docroot(std::string vsr) {
649 flush_local_cache ();
650 server_docroot = vsr;
654 /* video-monitor for this timeline */
656 VideoTimeLine::find_xjadeo () {
657 std::string xjadeo_file_path;
658 if (getenv("XJREMOTE")) {
659 _xjadeo_bin = strdup(getenv("XJREMOTE")); // XXX TODO: free it?!
660 } else if (find_file_in_search_path (SearchPath(Glib::getenv("PATH")), X_("xjremote"), xjadeo_file_path)) {
661 _xjadeo_bin = xjadeo_file_path;
663 else if (Glib::file_test(X_("/Applications/Jadeo.app/Contents/MacOS/xjremote"), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
664 _xjadeo_bin = X_("/Applications/Jadeo.app/Contents/MacOS/xjremote");
666 /* TODO: win32: allow to configure PATH to xjremote */
667 else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjremote.exe"), Glib::FILE_TEST_EXISTS)) {
668 _xjadeo_bin = X_("C:\\Program Files\\xjadeo\\xjremote.exe");
670 else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjremote.bat"), Glib::FILE_TEST_EXISTS)) {
671 _xjadeo_bin = X_("C:\\Program Files\\xjadeo\\xjremote.bat");
674 _xjadeo_bin = X_("");
675 warning << _("Video-monitor 'xjadeo' was not found. Please install http://xjadeo.sf.net/ "
676 "(a custom path to xjadeo can be specified by setting the XJREMOTE environment variable. "
677 "It should point to an application compatible with xjadeo's remote-control interface 'xjremote').")
683 VideoTimeLine::open_video_monitor(bool interactive) {
684 if (!found_xjadeo()) return;
686 vmonitor = new VideoMonitor(editor, _xjadeo_bin);
687 vmonitor->set_session(_session);
688 vmonitor->Terminated.connect (sigc::mem_fun (*this, &VideoTimeLine::terminated_video_monitor));
689 } else if (vmonitor->is_started()) {
693 int xj_settings_mask = vmonitor->restore_settings_mask();
695 /* load mask from Session */
696 XMLNode* node = _session->extra_xml (X_("XJRestoreSettings"));
698 const XMLProperty* prop = node->property (X_("mask"));
700 xj_settings_mask = atoi(prop->value().c_str());
705 if (interactive && Config->get_video_monitor_setup_dialog()) {
706 if (open_video_monitor_dialog == 0) {
707 open_video_monitor_dialog = new OpenVideoMonitorDialog(_session);
709 if (open_video_monitor_dialog->is_visible()) {
712 open_video_monitor_dialog->setup_settings_mask(xj_settings_mask);
713 open_video_monitor_dialog->set_filename(video_filename);
714 Gtk::ResponseType r = (Gtk::ResponseType) open_video_monitor_dialog->run ();
715 open_video_monitor_dialog->hide();
716 if (r != Gtk::RESPONSE_ACCEPT) {
717 GuiUpdate("set-xjadeo-active-off");
721 if (_session && (xj_settings_mask != open_video_monitor_dialog->xj_settings_mask()) ) {
722 /* save mask to Session */
723 XMLNode* node = new XMLNode(X_("XJRestoreSettings"));
724 node->add_property (X_("mask"), (const long) open_video_monitor_dialog->xj_settings_mask() );
725 _session->add_extra_xml (*node);
726 _session->set_dirty ();
729 if (open_video_monitor_dialog->show_again()) {
730 Config->set_video_monitor_setup_dialog(false);
733 vmonitor->set_debug(open_video_monitor_dialog->enable_debug());
735 vmonitor->restore_settings_mask(open_video_monitor_dialog->xj_settings_mask());
737 vmonitor->restore_settings_mask(xj_settings_mask);
741 if (!vmonitor->start()) {
742 warning << "launching xjadeo failed.." << endmsg;
743 close_video_monitor();
745 GuiUpdate("set-xjadeo-active-on");
746 vmonitor->set_fps(video_file_fps);
747 vmonitor->open(video_filename);
752 VideoTimeLine::close_video_monitor() {
753 if (vmonitor && vmonitor->is_started()) {
759 VideoTimeLine::terminated_video_monitor () {
763 GuiUpdate("set-xjadeo-active-off");
765 if (reopen_vmonitor) {
766 reopen_vmonitor=false;
767 open_video_monitor(false);
773 VideoTimeLine::clear_video_monitor_session_state ()
776 vmonitor->clear_session_state();
778 if (!_session) { return; }
779 XMLNode* node = new XMLNode(X_("XJSettings"));
780 _session->add_extra_xml (*node);
781 _session->set_dirty ();
787 VideoTimeLine::manual_seek_video_monitor (framepos_t pos)
789 if (!vmonitor) { return; }
790 if (!vmonitor->is_started()) { return; }
791 if (!vmonitor->synced_by_manual_seeks()) { return; }
792 vmonitor->manual_seek(pos, false, GOFFSET); // XXX -> set offset in xjadeo
795 #endif /* WITH_VIDEOTIMELINE */