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 "utils_videotl.h"
34 #include "rgb_macros.h"
35 #include "video_timeline.h"
37 #include <gtkmm2ext/utils.h>
39 #include <curl/curl.h>
44 using namespace ARDOUR;
46 using namespace Timecode;
48 VideoTimeLine::VideoTimeLine (PublicEditor *ed, ArdourCanvas::Group *vbg, int initial_height)
50 , videotl_bar_group(vbg)
51 , bar_height(initial_height)
53 video_start_offset = 0L;
57 auto_set_session_fps = false;
58 video_offset_lock = false;
59 video_aspect_ratio = 4.0/3.0;
60 Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoTimeLine::parameter_changed, this, _1), gui_context());
61 video_server_url = video_get_server_url(Config);
62 server_docroot = video_get_docroot(Config);
65 video_file_fps = 25.0;
68 reopen_vmonitor=false;
71 VtlUpdate.connect (*this, invalidator (*this), boost::bind (&PublicEditor::queue_visual_videotimeline_update, editor), gui_context());
72 GuiUpdate.connect (*this, invalidator (*this), boost::bind (&VideoTimeLine::gui_update, this, _1), gui_context());
75 VideoTimeLine::~VideoTimeLine ()
80 /* close and save settings */
82 VideoTimeLine::save_session ()
88 LocaleGuard lg (X_("POSIX"));
90 bool is_dirty = false;
92 XMLNode* prev = _session->extra_xml (X_("Videomonitor"));
94 /* remember if vmonitor was open.. */
95 XMLNode* node = new XMLNode(X_("Videomonitor"));
97 node->add_property (X_("active"), (vmonitor && vmonitor->is_started())?"yes":"no");
98 if (!prev || !(prev->property (X_("active")) && prev->property (X_("active"))->value() == node->property(X_("active"))->value()) ){
99 _session->add_extra_xml (*node);
100 is_dirty=true; // TODO not if !prev && value==default
104 node = _session->extra_xml (X_("Videotimeline"));
107 if (!(node->property(X_("id")) && node->property(X_("id"))->value() == id().to_s())) {
108 node->add_property (X_("id"), id().to_s());
113 /* remember timeline height.. */
115 int h = editor->get_videotl_bar_height();
116 if (!(node->property(X_("Height")) && atoi(node->property(X_("Height"))->value().c_str())==h)) {
117 node->add_property (X_("Height"), h);
122 /* save video-offset-lock */
124 if (!(node->property(X_("VideoOffsetLock")) && atoi(node->property(X_("VideoOffsetLock"))->value().c_str())==video_offset_lock)) {
125 node->add_property (X_("VideoOffsetLock"), video_offset_lock?X_("1"):X_("0"));
129 /* save video-offset */
131 if (!(node->property(X_("VideoOffset")) && atoll(node->property(X_("VideoOffset"))->value().c_str())==video_offset)) {
132 node->add_property (X_("VideoOffset"), video_offset);
137 /* save 'auto_set_session_fps' */
139 if (!(node->property(X_("AutoFPS")) && atoi(node->property(X_("AutoFPS"))->value().c_str())==auto_set_session_fps)) {
140 node->add_property (X_("AutoFPS"), auto_set_session_fps?X_("1"):X_("0"));
145 _session->set_dirty ();
149 /* close and save settings */
151 VideoTimeLine::close_session ()
153 if (video_duration == 0) {
157 close_video_monitor();
162 GuiUpdate("set-xjadeo-sensitive-off");
165 /** load settings from session */
167 VideoTimeLine::set_session (ARDOUR::Session *s)
169 SessionHandlePtr::set_session (s);
170 if (!_session) { return ; }
172 LocaleGuard lg (X_("POSIX"));
174 XMLNode* node = _session->extra_xml (X_("Videotimeline"));
176 if (!node || !node->property (X_("Filename"))) {
181 ARDOUR_UI::instance()->start_video_server((Gtk::Window*)0, false);
185 const XMLProperty* proph = node->property (X_("Height"));
187 editor->set_video_timeline_height(atoi(proph->value().c_str()));
189 #if 0 /* TODO THINK: set FPS first time only ?! */
190 const XMLProperty* propasfps = node->property (X_("AutoFPS"));
192 auto_set_session_fps = atoi(propasfps->value().c_str())?true:false;
196 const XMLProperty* propoffset = node->property (X_("VideoOffset"));
198 video_offset = atoll(propoffset->value().c_str());
199 video_offset_p = video_offset;
202 const XMLProperty* proplock = node->property (X_("VideoOffsetLock"));
204 video_offset_lock = atoi(proplock->value().c_str())?true:false;
207 const XMLProperty* localfile = node->property (X_("LocalFile"));
209 local_file = atoi(localfile->value().c_str())?true:false;
212 const XMLProperty* propf = node->property (X_("Filename"));
213 video_file_info(propf->value(), local_file);
216 node = _session->extra_xml (X_("Videomonitor"));
218 const XMLProperty* prop = node->property (X_("active"));
219 if (prop && prop->value() == "yes" && found_xjadeo() && !video_filename.empty() && local_file) {
220 open_video_monitor();
224 _session->register_with_memento_command_factory(id(), this);
225 _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoTimeLine::parameter_changed, this, _1), gui_context());
229 VideoTimeLine::save_undo ()
231 video_offset_p = video_offset;
235 VideoTimeLine::set_state (const XMLNode& node, int /*version*/)
237 LocaleGuard lg (X_("POSIX"));
238 const XMLProperty* propoffset = node.property (X_("VideoOffset"));
240 video_offset = atoll(propoffset->value().c_str());
242 ARDOUR_UI::instance()->flush_videotimeline_cache(true);
247 VideoTimeLine::get_state ()
249 XMLNode* node = new XMLNode (X_("Videotimeline"));
250 LocaleGuard lg (X_("POSIX"));
251 node->add_property (X_("VideoOffset"), video_offset_p);
256 VideoTimeLine::remove_frames ()
258 for (VideoFrames::iterator i = video_frames.begin(); i != video_frames.end(); ++i ) {
259 VideoImageFrame *frame = (*i);
263 video_frames.clear();
267 VideoTimeLine::get_video_frame (framepos_t vfn, int cut, int rightend)
270 for (VideoFrames::iterator i = video_frames.begin(); i != video_frames.end(); ++i) {
271 VideoImageFrame *frame = (*i);
272 if (abs(frame->get_video_frame_number()-vfn)<=cut
273 && frame->get_rightend() == rightend) { return frame; }
279 VideoTimeLine::get_apv()
281 // XXX: dup code - TODO use this fn in update_video_timeline()
282 float apv = -1; /* audio frames per video frame; */
283 if (!_session) return apv;
285 if (_session->config.get_use_video_file_fps()) {
286 if (video_file_fps == 0 ) return apv;
288 if (_session->timecode_frames_per_second() == 0 ) return apv;
291 if (_session->config.get_videotimeline_pullup()) {
292 apv = _session->frame_rate();
294 apv = _session->nominal_frame_rate();
296 if (_session->config.get_use_video_file_fps()) {
297 apv /= video_file_fps;
299 apv /= _session->timecode_frames_per_second();
305 VideoTimeLine::update_video_timeline()
307 if (!_session) return;
309 if (_session->config.get_use_video_file_fps()) {
310 if (video_file_fps == 0 ) return;
312 if (_session->timecode_frames_per_second() == 0 ) return;
315 double frames_per_unit = editor->unit_to_frame(1.0);
316 framepos_t leftmost_frame = editor->leftmost_position();
319 * 1) calculate how many frames there should be in current zoom (plus 1 page on each side)
320 * 2) calculate first frame and distance between video-frames (according to zoom)
321 * 3) destroy/add frames
322 * 4) reposition existing frames
323 * 5) assign framenumber to frames -> request/decode video.
326 /* video-file and session properties */
327 double display_vframe_width; /* unit: pixels ; width of one thumbnail in the timeline */
328 float apv; /* audio frames per video frame; */
329 framepos_t leftmost_video_frame; /* unit: video-frame number ; temporary var -> vtl_start */
331 /* variables needed to render videotimeline -- what needs to computed first */
332 framepos_t vtl_start; /* unit: audio-frames ; first displayed video-frame */
333 framepos_t vtl_dist; /* unit: audio-frames ; distance between displayed video-frames */
334 unsigned int visible_video_frames; /* number of frames that fit on current canvas */
336 if (_session->config.get_videotimeline_pullup()) {
337 apv = _session->frame_rate();
339 apv = _session->nominal_frame_rate();
341 if (_session->config.get_use_video_file_fps()) {
342 apv /= video_file_fps;
344 apv /= _session->timecode_frames_per_second();
347 display_vframe_width = bar_height * video_aspect_ratio;
349 if (apv > frames_per_unit * display_vframe_width) {
350 /* high-zoom: need space between successive video-frames */
351 vtl_dist = rint(apv);
353 /* continous timeline: skip video-frames */
354 vtl_dist = ceil(display_vframe_width * frames_per_unit / apv) * apv;
357 assert (vtl_dist > 0);
360 #define GOFFSET (video_offset)
362 leftmost_video_frame = floor (floor((leftmost_frame - video_start_offset - GOFFSET ) / vtl_dist) * vtl_dist / apv);
364 vtl_start = rint (GOFFSET + video_start_offset + leftmost_video_frame * apv);
365 visible_video_frames = 2 + ceil(editor->current_page_frames() / vtl_dist); /* +2 left+right partial frames */
367 /* expand timeline (cache next/prev page images) */
368 vtl_start -= visible_video_frames * vtl_dist;
369 visible_video_frames *=3;
371 if (vtl_start < GOFFSET ) {
372 visible_video_frames += ceil(vtl_start/vtl_dist);
376 /* apply video-file constraints */
377 if (vtl_start > video_start_offset + video_duration + GOFFSET ) {
378 visible_video_frames = 0;
380 /* TODO optimize: compute rather than iterate */
381 while (visible_video_frames > 0 && vtl_start + (visible_video_frames-1) * vtl_dist >= video_start_offset + video_duration + GOFFSET) {
382 --visible_video_frames;
390 while (video_frames.size() < visible_video_frames) {
391 VideoImageFrame *frame;
392 frame = new VideoImageFrame(*editor, *videotl_bar_group, display_vframe_width, bar_height, video_server_url, translated_filename());
393 frame->ImgChanged.connect (*this, invalidator (*this), boost::bind (&PublicEditor::queue_visual_videotimeline_update, editor), gui_context());
394 video_frames.push_back(frame);
397 VideoFrames outdated_video_frames;
398 std::list<int> remaining;
400 outdated_video_frames = video_frames;
403 /* when zoomed out, ignore shifts by +-1 frame
404 * which can occur due to rounding errors when
405 * scrolling to a new leftmost-audio frame.
408 if (vtl_dist/apv < 3.0) cut =0;
413 for (unsigned int vfcount=0; vfcount < visible_video_frames; ++vfcount){
414 framepos_t vfpos = vtl_start + vfcount * vtl_dist; /* unit: audio-frames */
415 framepos_t vframeno = rint ( (vfpos - GOFFSET) / apv); /* unit: video-frames */
416 vfpos = (vframeno * apv ) + GOFFSET; /* audio-frame corresponding to /rounded/ video-frame */
418 int rightend = -1; /* unit: pixels */
419 if (vfpos + vtl_dist > video_start_offset + video_duration + GOFFSET) {
420 rightend = display_vframe_width * (video_start_offset + video_duration + GOFFSET - vfpos) / vtl_dist;
421 //printf("lf(e): %lu\n", vframeno); // XXX
423 VideoImageFrame * frame = get_video_frame(vframeno, cut, rightend);
425 frame->set_position(vfpos-leftmost_frame);
426 outdated_video_frames.remove(frame);
428 remaining.push_back(vfcount);
432 for (VideoFrames::iterator i = outdated_video_frames.begin(); i != outdated_video_frames.end(); ++i ) {
433 VideoImageFrame *frame = (*i);
434 if (remaining.empty()) {
435 frame->set_position(-2 * vtl_dist); /* move off screen */
437 int vfcount=remaining.front();
438 remaining.pop_front();
439 framepos_t vfpos = vtl_start + vfcount * vtl_dist; /* unit: audio-frames */
440 framepos_t vframeno = rint ((vfpos - GOFFSET) / apv); /* unit: video-frames */
441 int rightend = -1; /* unit: pixels */
442 if (vfpos + vtl_dist > video_start_offset + video_duration + GOFFSET) {
443 rightend = display_vframe_width * (video_start_offset + video_duration + GOFFSET - vfpos) / vtl_dist;
444 //printf("lf(n): %lu\n", vframeno); // XXX
446 frame->set_position(vfpos-leftmost_frame);
447 frame->set_videoframe(vframeno, rightend);
453 VideoTimeLine::translated_filename ()
456 return video_filename;
458 return video_map_path(server_docroot, video_filename);
463 VideoTimeLine::video_file_info (std::string filename, bool local)
467 if (filename.at(0) == G_DIR_SEPARATOR || !local_file) {
468 video_filename = filename;
470 video_filename = Glib::build_filename (_session->session_directory().video_path(), filename);
473 long long int _duration;
474 double _start_offset;
476 if (!video_query_info(
477 video_server_url, translated_filename(),
478 video_file_fps, _duration, _start_offset, video_aspect_ratio)) {
479 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;
482 video_duration = _duration * _session->nominal_frame_rate() / video_file_fps;
483 video_start_offset = _start_offset * _session->nominal_frame_rate();
485 if (auto_set_session_fps && video_file_fps != _session->timecode_frames_per_second()) {
486 switch ((int)floorf(video_file_fps*1000.0)) {
488 _session->config.set_timecode_format(timecode_23976);
491 _session->config.set_timecode_format(timecode_24);
495 _session->config.set_timecode_format(timecode_24976);
498 _session->config.set_timecode_format(timecode_25);
501 _session->config.set_timecode_format(timecode_2997drop);
504 _session->config.set_timecode_format(timecode_30);
507 _session->config.set_timecode_format(timecode_5994);
510 _session->config.set_timecode_format(timecode_60);
513 warning << _("Failed to set session-framerate: ") << video_file_fps << _(" does not have a corresponding option setting in Ardour.") << endmsg; /* TODO: gettext arg */
516 _session->config.set_video_pullup(0); /* TODO only set if set_timecode_format() was successful ?!*/
518 if (video_file_fps != _session->timecode_frames_per_second()) {
519 warning << _("Video file's framerate is not equal to Ardour session timecode's framerate: ")
520 << video_file_fps << _(" vs ") << _session->timecode_frames_per_second() << endmsg;
522 flush_local_cache ();
524 if (found_xjadeo() && local_file) {
525 GuiUpdate("set-xjadeo-sensitive-on");
526 if (vmonitor && vmonitor->is_started()) {
528 /* xjadeo <= 0.6.4 has a bug where changing the video-file may segfauls
529 * if the geometry changes to a different line-size alignment
531 reopen_vmonitor = true;
534 vmonitor->set_fps(video_file_fps);
535 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);
582 } else if (t == "xjadeo-window-ontop-on") {
583 editor->toggle_xjadeo_viewoption(1, 1);
584 } else if (t == "xjadeo-window-ontop-off") {
585 editor->toggle_xjadeo_viewoption(1, 0);
586 } else if (t == "xjadeo-window-osd-timecode-on") {
587 editor->toggle_xjadeo_viewoption(2, 1);
588 } else if (t == "xjadeo-window-osd-timecode-off") {
589 editor->toggle_xjadeo_viewoption(2, 0);
590 } else if (t == "xjadeo-window-osd-frame-on") {
591 editor->toggle_xjadeo_viewoption(3, 1);
592 } else if (t == "xjadeo-window-osd-frame-off") {
593 editor->toggle_xjadeo_viewoption(3, 0);
594 } else if (t == "xjadeo-window-osd-box-on") {
595 editor->toggle_xjadeo_viewoption(4, 1);
596 } else if (t == "xjadeo-window-osd-box-off") {
597 editor->toggle_xjadeo_viewoption(4, 0);
598 } else if (t == "xjadeo-window-fullscreen-on") {
599 editor->toggle_xjadeo_viewoption(5, 1);
600 } else if (t == "xjadeo-window-fullscreen-off") {
601 editor->toggle_xjadeo_viewoption(5, 0);
602 } else if (t == "xjadeo-window-letterbox-on") {
603 editor->toggle_xjadeo_viewoption(6, 1);
604 } else if (t == "xjadeo-window-letterbox-off") {
605 editor->toggle_xjadeo_viewoption(6, 0);
610 VideoTimeLine::set_height (int height) {
616 VideoTimeLine::vmon_update () {
617 if (vmonitor && vmonitor->is_started()) {
618 vmonitor->set_offset( GOFFSET); // TODO proper re-init xjadeo w/o restart not just offset.
623 VideoTimeLine::flush_local_cache () {
629 VideoTimeLine::flush_cache () {
632 snprintf(url, sizeof(url), "%s%sadmin/flush_cache"
633 , video_server_url.c_str()
634 , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
636 char *res=curl_http_get(url, NULL);
640 if (vmonitor && vmonitor->is_started()) {
641 reopen_vmonitor=true;
644 video_file_info(video_filename, local_file);
649 VideoTimeLine::parameter_changed (std::string const & p)
651 if (p == "video-server-url") {
652 set_video_server_url (video_get_server_url(Config));
653 } else if (p == "video-server-docroot") {
654 set_video_server_docroot (video_get_docroot(Config));
655 } else if (p == "video-advanced-setup") {
656 set_video_server_url (video_get_server_url(Config));
657 set_video_server_docroot (video_get_docroot(Config));
659 if (p == "use-video-file-fps" || p == "videotimeline-pullup" ) { /* session->config parameter */
665 VideoTimeLine::set_video_server_url(std::string vsu) {
666 flush_local_cache ();
667 video_server_url = vsu;
672 VideoTimeLine::set_video_server_docroot(std::string vsr) {
673 flush_local_cache ();
674 server_docroot = vsr;
678 /* video-monitor for this timeline */
680 VideoTimeLine::find_xjadeo () {
681 std::string xjadeo_file_path;
682 if (getenv("XJREMOTE")) {
683 _xjadeo_bin = strdup(getenv("XJREMOTE")); // XXX TODO: free it?!
684 } else if (find_file_in_search_path (SearchPath(Glib::getenv("PATH")), X_("xjremote"), xjadeo_file_path)) {
685 _xjadeo_bin = xjadeo_file_path;
687 else if (Glib::file_test(X_("/Applications/Jadeo.app/Contents/MacOS/xjremote"), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
688 _xjadeo_bin = X_("/Applications/Jadeo.app/Contents/MacOS/xjremote");
690 /* TODO: win32: allow to configure PATH to xjremote */
691 else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjremote.exe"), Glib::FILE_TEST_EXISTS)) {
692 _xjadeo_bin = X_("C:\\Program Files\\xjadeo\\xjremote.exe");
694 else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjremote.bat"), Glib::FILE_TEST_EXISTS)) {
695 _xjadeo_bin = X_("C:\\Program Files\\xjadeo\\xjremote.bat");
698 _xjadeo_bin = X_("");
699 warning << _("Video-monitor 'xjadeo' was not found. Please install http://xjadeo.sf.net/ "
700 "(a custom path to xjadeo can be specified by setting the XJREMOTE environment variable. "
701 "It should point to an application compatible with xjadeo's remote-control interface 'xjremote').")
707 VideoTimeLine::open_video_monitor() {
708 if (!found_xjadeo()) return;
710 vmonitor = new VideoMonitor(editor, _xjadeo_bin);
711 vmonitor->set_session(_session);
712 vmonitor->Terminated.connect (sigc::mem_fun (*this, &VideoTimeLine::terminated_video_monitor));
713 vmonitor->UiState.connect (*this, invalidator (*this), boost::bind (&VideoTimeLine::gui_update, this, _1), gui_context());
714 } else if (vmonitor->is_started()) {
718 int xj_settings_mask = vmonitor->restore_settings_mask();
720 /* load mask from Session */
721 XMLNode* node = _session->extra_xml (X_("XJRestoreSettings"));
723 const XMLProperty* prop = node->property (X_("mask"));
725 xj_settings_mask = atoi(prop->value().c_str());
730 vmonitor->restore_settings_mask(xj_settings_mask);
732 if (!vmonitor->start()) {
733 warning << "launching xjadeo failed.." << endmsg;
734 close_video_monitor();
736 GuiUpdate("set-xjadeo-active-on");
737 vmonitor->set_fps(video_file_fps);
738 vmonitor->open(video_filename);
743 VideoTimeLine::close_video_monitor() {
744 if (vmonitor && vmonitor->is_started()) {
750 VideoTimeLine::control_video_monitor(int what, int param) {
751 if (!vmonitor || !vmonitor->is_started()) {
754 vmonitor->send_cmd(what, param);
759 VideoTimeLine::terminated_video_monitor () {
763 GuiUpdate("set-xjadeo-active-off");
765 if (reopen_vmonitor) {
766 reopen_vmonitor=false;
767 open_video_monitor();
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 */