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
22 #include <sigc++/bind.h>
23 #include "ardour/tempo.h"
25 #include "pbd/file_utils.h"
26 #include "ardour/session_directory.h"
28 #include "ardour_ui.h"
29 #include "public_editor.h"
30 #include "gui_thread.h"
32 #include "canvas_impl.h"
33 #include "simpleline.h"
34 #include "utils_videotl.h"
35 #include "rgb_macros.h"
36 #include "video_timeline.h"
38 #include <gtkmm2ext/utils.h>
40 #include <curl/curl.h>
45 using namespace ARDOUR;
47 using namespace Timecode;
50 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
53 VideoTimeLine::VideoTimeLine (PublicEditor *ed, ArdourCanvas::Group *vbg, int initial_height)
55 , videotl_bar_group(vbg)
56 , bar_height(initial_height)
58 video_start_offset = 0L;
62 auto_set_session_fps = false;
63 video_offset_lock = false;
64 video_aspect_ratio = 4.0/3.0;
65 open_video_monitor_dialog = 0;
66 Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoTimeLine::parameter_changed, this, _1), gui_context());
67 video_server_url = Config->get_video_server_url();
68 server_docroot = Config->get_video_server_docroot();
71 video_file_fps = 25.0;
74 reopen_vmonitor=false;
77 VtlUpdate.connect (*this, invalidator (*this), boost::bind (&PublicEditor::queue_visual_videotimeline_update, editor), gui_context());
78 GuiUpdate.connect (*this, invalidator (*this), boost::bind (&VideoTimeLine::gui_update, this, _1), gui_context());
81 VideoTimeLine::~VideoTimeLine ()
86 /* close and save settings */
88 VideoTimeLine::save_session ()
94 LocaleGuard lg (X_("POSIX"));
96 bool is_dirty = false;
98 XMLNode* prev = _session->extra_xml (X_("Videomonitor"));
100 /* remember if vmonitor was open.. */
101 XMLNode* node = new XMLNode(X_("Videomonitor"));
103 node->add_property (X_("active"), (vmonitor && vmonitor->is_started())?"yes":"no");
104 if (!prev || !(prev->property (X_("active")) && prev->property (X_("active"))->value() == node->property(X_("active"))->value()) ){
105 _session->add_extra_xml (*node);
106 is_dirty=true; // TODO not if !prev && value==default
110 node = _session->extra_xml (X_("Videotimeline"));
113 if (!(node->property(X_("id")) && node->property(X_("id"))->value() == id().to_s())) {
114 node->add_property (X_("id"), id().to_s());
119 /* remember timeline height.. */
121 int h = editor->get_videotl_bar_height();
122 if (!(node->property(X_("Height")) && atoi(node->property(X_("Height"))->value().c_str())==h)) {
123 node->add_property (X_("Height"), h);
128 /* save video-offset-lock */
130 if (!(node->property(X_("VideoOffsetLock")) && atoi(node->property(X_("VideoOffsetLock"))->value().c_str())==video_offset_lock)) {
131 node->add_property (X_("VideoOffsetLock"), video_offset_lock?X_("1"):X_("0"));
135 /* save video-offset */
137 if (!(node->property(X_("VideoOffset")) && atoll(node->property(X_("VideoOffset"))->value().c_str())==video_offset)) {
138 node->add_property (X_("VideoOffset"), video_offset);
143 /* save 'auto_set_session_fps' */
145 if (!(node->property(X_("AutoFPS")) && atoi(node->property(X_("AutoFPS"))->value().c_str())==auto_set_session_fps)) {
146 node->add_property (X_("AutoFPS"), auto_set_session_fps?X_("1"):X_("0"));
151 _session->set_dirty ();
155 /* close and save settings */
157 VideoTimeLine::close_session ()
159 close_video_monitor();
167 /** load settings from session */
169 VideoTimeLine::set_session (ARDOUR::Session *s)
171 SessionHandlePtr::set_session (s);
172 if (!_session) { return ; }
174 LocaleGuard lg (X_("POSIX"));
176 XMLNode* node = _session->extra_xml (X_("Videotimeline"));
178 ARDOUR_UI::instance()->start_video_server((Gtk::Window*)0, false);
182 const XMLProperty* proph = node->property (X_("Height"));
184 editor->set_video_timeline_height(atoi(proph->value().c_str()));
186 #if 0 /* TODO THINK: set FPS first time only ?! */
187 const XMLProperty* propasfps = node->property (X_("AutoFPS"));
189 auto_set_session_fps = atoi(propasfps->value().c_str())?true:false;
193 const XMLProperty* propoffset = node->property (X_("VideoOffset"));
195 video_offset = atoll(propoffset->value().c_str());
196 video_offset_p = video_offset;
199 const XMLProperty* proplock = node->property (X_("VideoOffsetLock"));
201 video_offset_lock = atoi(proplock->value().c_str())?true:false;
204 const XMLProperty* localfile = node->property (X_("LocalFile"));
206 local_file = atoi(localfile->value().c_str())?true:false;
209 const XMLProperty* propf = node->property (X_("Filename"));
210 video_file_info(propf->value(), local_file);
213 node = _session->extra_xml (X_("Videomonitor"));
215 const XMLProperty* prop = node->property (X_("active"));
216 if (prop->value() == "yes" && found_xjadeo() && !video_filename.empty() && local_file) {
217 open_video_monitor(false);
221 _session->register_with_memento_command_factory(id(), this);
222 _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoTimeLine::parameter_changed, this, _1), gui_context());
226 VideoTimeLine::save_undo ()
228 video_offset_p = video_offset;
232 VideoTimeLine::set_state (const XMLNode& node, int /*version*/)
234 LocaleGuard lg (X_("POSIX"));
235 const XMLProperty* propoffset = node.property (X_("VideoOffset"));
237 video_offset = atoll(propoffset->value().c_str());
239 ARDOUR_UI::instance()->flush_videotimeline_cache(true);
244 VideoTimeLine::get_state ()
246 XMLNode* node = new XMLNode (X_("Videotimeline"));
247 LocaleGuard lg (X_("POSIX"));
248 node->add_property (X_("VideoOffset"), video_offset_p);
253 VideoTimeLine::remove_frames ()
255 for (VideoFrames::iterator i = video_frames.begin(); i != video_frames.end(); ++i ) {
256 VideoImageFrame *frame = (*i);
260 video_frames.clear();
264 VideoTimeLine::get_video_frame (framepos_t vfn, int cut, int rightend)
267 for (VideoFrames::iterator i = video_frames.begin(); i != video_frames.end(); ++i) {
268 VideoImageFrame *frame = (*i);
269 if (abs(frame->get_video_frame_number()-vfn)<=cut
270 && frame->get_rightend() == rightend) { return frame; }
276 VideoTimeLine::get_apv()
278 // XXX: dup code - TODO use this fn in update_video_timeline()
279 float apv = -1; /* audio frames per video frame; */
280 if (!_session) return apv;
282 if (_session->config.get_use_video_file_fps()) {
283 if (video_file_fps == 0 ) return apv;
285 if (_session->timecode_frames_per_second() == 0 ) return apv;
288 if (_session->config.get_videotimeline_pullup()) {
289 apv = _session->frame_rate();
291 apv = _session->nominal_frame_rate();
293 if (_session->config.get_use_video_file_fps()) {
294 apv /= video_file_fps;
296 apv /= _session->timecode_frames_per_second();
302 VideoTimeLine::update_video_timeline()
304 if (!_session) return;
306 if (_session->config.get_use_video_file_fps()) {
307 if (video_file_fps == 0 ) return;
309 if (_session->timecode_frames_per_second() == 0 ) return;
312 double frames_per_unit = editor->unit_to_frame(1.0);
313 framepos_t leftmost_frame = editor->leftmost_position();
316 * 1) calculate how many frames there should be in current zoom (plus 1 page on each side)
317 * 2) calculate first frame and distance between video-frames (according to zoom)
318 * 3) destroy/add frames
319 * 4) reposition existing frames
320 * 5) assign framenumber to frames -> request/decode video.
323 /* video-file and session properties */
324 double display_vframe_width; /* unit: pixels ; width of one thumbnail in the timeline */
325 float apv; /* audio frames per video frame; */
326 framepos_t leftmost_video_frame; /* unit: video-frame number ; temporary var -> vtl_start */
328 /* variables needed to render videotimeline -- what needs to computed first */
329 framepos_t vtl_start; /* unit: audio-frames ; first displayed video-frame */
330 framepos_t vtl_dist; /* unit: audio-frames ; distance between displayed video-frames */
331 unsigned int visible_video_frames; /* number of frames that fit on current canvas */
333 if (_session->config.get_videotimeline_pullup()) {
334 apv = _session->frame_rate();
336 apv = _session->nominal_frame_rate();
338 if (_session->config.get_use_video_file_fps()) {
339 apv /= video_file_fps;
341 apv /= _session->timecode_frames_per_second();
344 display_vframe_width = bar_height * video_aspect_ratio;
346 if (apv > frames_per_unit * display_vframe_width) {
347 /* high-zoom: need space between successive video-frames */
348 vtl_dist = rint(apv);
350 /* continous timeline: skip video-frames */
351 vtl_dist = ceil(display_vframe_width * frames_per_unit / apv) * apv;
354 assert (vtl_dist > 0);
357 #define GOFFSET (video_offset)
359 leftmost_video_frame = floor (floor((leftmost_frame - video_start_offset - GOFFSET ) / vtl_dist) * vtl_dist / apv);
361 vtl_start = rint (GOFFSET + video_start_offset + leftmost_video_frame * apv);
362 visible_video_frames = 2 + ceil(editor->current_page_frames() / vtl_dist); /* +2 left+right partial frames */
364 /* expand timeline (cache next/prev page images) */
365 vtl_start -= visible_video_frames * vtl_dist;
366 visible_video_frames *=3;
368 if (vtl_start < GOFFSET ) {
369 visible_video_frames += ceil(vtl_start/vtl_dist);
373 /* apply video-file constraints */
374 if (vtl_start > video_start_offset + video_duration + GOFFSET ) {
375 visible_video_frames = 0;
377 /* TODO optimize: compute rather than iterate */
378 while (visible_video_frames > 0 && vtl_start + (visible_video_frames-1) * vtl_dist >= video_start_offset + video_duration + GOFFSET) {
379 --visible_video_frames;
387 while (video_frames.size() < visible_video_frames) {
388 VideoImageFrame *frame;
389 frame = new VideoImageFrame(*editor, *videotl_bar_group, display_vframe_width, bar_height, video_server_url, translated_filename());
390 frame->ImgChanged.connect (*this, invalidator (*this), boost::bind (&PublicEditor::queue_visual_videotimeline_update, editor), gui_context());
391 video_frames.push_back(frame);
394 VideoFrames outdated_video_frames;
395 std::list<int> remaining;
397 outdated_video_frames = video_frames;
400 /* when zoomed out, ignore shifts by +-1 frame
401 * which can occur due to rounding errors when
402 * scrolling to a new leftmost-audio frame.
405 if (vtl_dist/apv < 3.0) cut =0;
410 for (unsigned int vfcount=0; vfcount < visible_video_frames; ++vfcount){
411 framepos_t vfpos = vtl_start + vfcount * vtl_dist; /* unit: audio-frames */
412 framepos_t vframeno = rint ( (vfpos - GOFFSET) / apv); /* unit: video-frames */
413 vfpos = (vframeno * apv ) + GOFFSET; /* audio-frame corresponding to /rounded/ video-frame */
415 int rightend = -1; /* unit: pixels */
416 if (vfpos + vtl_dist > video_start_offset + video_duration + GOFFSET) {
417 rightend = display_vframe_width * (video_start_offset + video_duration + GOFFSET - vfpos) / vtl_dist;
418 //printf("lf(e): %lu\n", vframeno); // XXX
420 VideoImageFrame * frame = get_video_frame(vframeno, cut, rightend);
422 frame->set_position(vfpos-leftmost_frame);
423 outdated_video_frames.remove(frame);
425 remaining.push_back(vfcount);
429 for (VideoFrames::iterator i = outdated_video_frames.begin(); i != outdated_video_frames.end(); ++i ) {
430 VideoImageFrame *frame = (*i);
431 if (remaining.empty()) {
432 frame->set_position(-2 * vtl_dist); /* move off screen */
434 int vfcount=remaining.front();
435 remaining.pop_front();
436 framepos_t vfpos = vtl_start + vfcount * vtl_dist; /* unit: audio-frames */
437 framepos_t vframeno = rint ((vfpos - GOFFSET) / apv); /* unit: video-frames */
438 int rightend = -1; /* unit: pixels */
439 if (vfpos + vtl_dist > video_start_offset + video_duration + GOFFSET) {
440 rightend = display_vframe_width * (video_start_offset + video_duration + GOFFSET - vfpos) / vtl_dist;
441 //printf("lf(n): %lu\n", vframeno); // XXX
443 frame->set_position(vfpos-leftmost_frame);
444 frame->set_videoframe(vframeno, rightend);
450 VideoTimeLine::translated_filename ()
453 return video_filename;
455 return video_map_path(server_docroot, video_filename);
460 VideoTimeLine::video_file_info (std::string filename, bool local)
464 if (filename.at(0) == G_DIR_SEPARATOR || !local_file) {
465 video_filename = filename;
467 video_filename = Glib::build_filename (_session->session_directory().video_path(), filename);
470 long long int _duration;
471 double _start_offset;
473 if (!video_query_info(
474 video_server_url, translated_filename(),
475 video_file_fps, _duration, _start_offset, video_aspect_ratio)) {
476 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;
479 video_duration = _duration * _session->nominal_frame_rate() / video_file_fps;
480 video_start_offset = _start_offset * _session->nominal_frame_rate();
482 if (auto_set_session_fps && video_file_fps != _session->timecode_frames_per_second()) {
483 switch ((int)floorf(video_file_fps*1000.0)) {
485 _session->config.set_timecode_format(timecode_23976);
488 _session->config.set_timecode_format(timecode_24);
492 _session->config.set_timecode_format(timecode_24976);
495 _session->config.set_timecode_format(timecode_25);
498 _session->config.set_timecode_format(timecode_2997drop);
501 _session->config.set_timecode_format(timecode_30);
504 _session->config.set_timecode_format(timecode_5994);
507 _session->config.set_timecode_format(timecode_60);
510 warning << _("Failed to set session-framerate: ") << video_file_fps << _(" does not have a corresponding option setting in Ardour.") << endmsg; /* TODO: gettext arg */
513 _session->config.set_video_pullup(0); /* TODO only set if set_timecode_format() was successful ?!*/
515 if (video_file_fps != _session->timecode_frames_per_second()) {
516 warning << _("Video file's framerate is not equal to Ardour session timecode's framerate: ")
517 << video_file_fps << _(" vs ") << _session->timecode_frames_per_second() << endmsg;
519 flush_local_cache ();
521 _session->maybe_update_session_range(
522 MAX(get_offset(), 0),
523 MAX(get_offset() + get_duration(), 0)
526 if (found_xjadeo() && local_file) {
527 GuiUpdate("set-xjadeo-sensitive-on");
528 if (vmonitor && vmonitor->is_started()) {
529 vmonitor->set_fps(video_file_fps);
530 vmonitor->open(video_filename);
532 } else if (!local_file) {
533 #if 1 /* temp debug/devel message */
534 // TODO - call xjremote remotely.
535 printf("the given video file can not be accessed on localhost, video monitoring is not currently supported for this case\n");
536 GuiUpdate("set-xjadeo-sensitive-off");
544 VideoTimeLine::check_server ()
548 snprintf(url, sizeof(url), "%s%sstatus"
549 , video_server_url.c_str()
550 , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
552 char *res=curl_http_get(url, NULL);
554 if (strstr(res, "status: ok, online.")) { ok = true; }
561 VideoTimeLine::gui_update(std::string const & t) {
562 /* this is to be called via GuiUpdate() only. */
563 ENSURE_GUI_THREAD (*this, &VideoTimeLine::queue_visual_videotimeline_update)
564 if (t == "videotimeline-update") {
565 editor->queue_visual_videotimeline_update();
566 } else if (t == "set-xjadeo-active-off") {
567 editor->toggle_xjadeo_proc(0);
568 } else if (t == "set-xjadeo-active-on") {
569 editor->toggle_xjadeo_proc(1);
570 } else if (t == "set-xjadeo-sensitive-on") {
571 editor->set_xjadeo_sensitive(true);
572 } else if (t == "set-xjadeo-sensitive-off") {
573 editor->toggle_xjadeo_proc(0);
574 //close_video_monitor();
575 editor->set_xjadeo_sensitive(false);
580 VideoTimeLine::set_height (int height) {
586 VideoTimeLine::vmon_update () {
587 if (vmonitor && vmonitor->is_started()) {
588 vmonitor->set_offset( GOFFSET); // TODO proper re-init xjadeo w/o restart not just offset.
593 VideoTimeLine::flush_local_cache () {
599 VideoTimeLine::flush_cache () {
602 snprintf(url, sizeof(url), "%s%sadmin/flush_cache"
603 , video_server_url.c_str()
604 , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
606 char *res=curl_http_get(url, NULL);
610 if (vmonitor && vmonitor->is_started()) {
611 reopen_vmonitor=true;
614 video_file_info(video_filename, local_file);
619 VideoTimeLine::parameter_changed (std::string const & p)
621 if (p == "video-server-url") {
622 set_video_server_url (Config->get_video_server_url ());
623 } else if (p == "video-server-docroot") {
624 set_video_server_docroot (Config->get_video_server_docroot ());
626 if (p == "use-video-file-fps" || p == "videotimeline-pullup" ) { /* session->config parameter */
632 VideoTimeLine::set_video_server_url(std::string vsu) {
633 flush_local_cache ();
634 video_server_url = vsu;
639 VideoTimeLine::set_video_server_docroot(std::string vsr) {
640 flush_local_cache ();
641 server_docroot = vsr;
645 /* video-monitor for this timeline */
647 VideoTimeLine::find_xjadeo () {
648 std::string xjadeo_file_path;
649 if (getenv("XJREMOTE")) {
650 _xjadeo_bin = strdup(getenv("XJREMOTE")); // XXX TODO: free it?!
651 } else if (find_file_in_search_path (SearchPath(Glib::getenv("PATH")), X_("xjremote"), xjadeo_file_path)) {
652 _xjadeo_bin = xjadeo_file_path;
654 else if (Glib::file_test(X_("/Applications/Jadeo.app/Contents/MacOS/xjremote"), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
655 _xjadeo_bin = X_("/Applications/Jadeo.app/Contents/MacOS/xjremote");
657 /* TODO: win32: allow to configure PATH to xjremote */
658 else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjremote.exe"), Glib::FILE_TEST_EXISTS)) {
659 _xjadeo_bin = X_("C:\\Program Files\\xjadeo\\xjremote.exe");
661 else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjremote.bat"), Glib::FILE_TEST_EXISTS)) {
662 _xjadeo_bin = X_("C:\\Program Files\\xjadeo\\xjremote.bat");
665 _xjadeo_bin = X_("");
666 warning << _("Video-monitor 'xjadeo' was not found. Please install http://xjadeo.sf.net/ "
667 "(a custom path to xjadeo can be specified by setting the XJREMOTE environment variable. "
668 "It should point to an application compatible with xjadeo's remote-control interface 'xjremote').")
674 VideoTimeLine::open_video_monitor(bool interactive) {
675 if (!found_xjadeo()) return;
677 vmonitor = new VideoMonitor(editor, _xjadeo_bin);
678 vmonitor->set_session(_session);
679 vmonitor->Terminated.connect (sigc::mem_fun (*this, &VideoTimeLine::terminated_video_monitor));
680 } else if (vmonitor->is_started()) {
684 int xj_settings_mask = vmonitor->restore_settings_mask();
686 /* load mask from Session */
687 XMLNode* node = _session->extra_xml (X_("XJRestoreSettings"));
689 const XMLProperty* prop = node->property (X_("mask"));
691 xj_settings_mask = atoi(prop->value().c_str());
696 if (interactive && Config->get_video_monitor_setup_dialog()) {
697 if (open_video_monitor_dialog == 0) {
698 open_video_monitor_dialog = new OpenVideoMonitorDialog(_session);
700 if (open_video_monitor_dialog->is_visible()) {
703 open_video_monitor_dialog->setup_settings_mask(xj_settings_mask);
704 open_video_monitor_dialog->set_filename(video_filename);
705 Gtk::ResponseType r = (Gtk::ResponseType) open_video_monitor_dialog->run ();
706 open_video_monitor_dialog->hide();
707 if (r != Gtk::RESPONSE_ACCEPT) {
708 GuiUpdate("set-xjadeo-active-off");
712 if (_session && (xj_settings_mask != open_video_monitor_dialog->xj_settings_mask()) ) {
713 /* save mask to Session */
714 XMLNode* node = new XMLNode(X_("XJRestoreSettings"));
715 node->add_property (X_("mask"), (const long) open_video_monitor_dialog->xj_settings_mask() );
716 _session->add_extra_xml (*node);
717 _session->set_dirty ();
720 if (open_video_monitor_dialog->show_again()) {
721 Config->set_video_monitor_setup_dialog(false);
724 vmonitor->set_debug(open_video_monitor_dialog->enable_debug());
726 vmonitor->restore_settings_mask(open_video_monitor_dialog->xj_settings_mask());
728 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::terminated_video_monitor () {
754 GuiUpdate("set-xjadeo-active-off");
756 if (reopen_vmonitor) {
757 reopen_vmonitor=false;
758 open_video_monitor(false);
764 VideoTimeLine::clear_video_monitor_session_state ()
767 vmonitor->clear_session_state();
769 if (!_session) { return; }
770 XMLNode* node = new XMLNode(X_("XJSettings"));
771 _session->add_extra_xml (*node);
772 _session->set_dirty ();
778 VideoTimeLine::manual_seek_video_monitor (framepos_t pos)
780 if (!vmonitor) { return; }
781 if (!vmonitor->is_started()) { return; }
782 if (!vmonitor->synced_by_manual_seeks()) { return; }
783 vmonitor->manual_seek(pos, false, GOFFSET); // XXX -> set offset in xjadeo
786 #endif /* WITH_VIDEOTIMELINE */