videotimline
[ardour.git] / gtk2_ardour / video_timeline.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3     Author: Robin Gareus <robin@gareus.org>
4
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.
9
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.
14
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.
18
19 */
20 #ifdef WITH_VIDEOTIMELINE
21
22 #include <sigc++/bind.h>
23 #include "ardour/tempo.h"
24
25 #include "pbd/file_utils.h"
26 #include "ardour/session_directory.h"
27
28 #include "ardour_ui.h"
29 #include "public_editor.h"
30 #include "gui_thread.h"
31 #include "utils.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"
37
38 #include <gtkmm2ext/utils.h>
39 #include <pthread.h>
40 #include <curl/curl.h>
41
42 #include "i18n.h"
43
44 using namespace std;
45 using namespace ARDOUR;
46 using namespace PBD;
47 using namespace Timecode;
48
49
50 VideoTimeLine::VideoTimeLine (PublicEditor *ed, ArdourCanvas::Group *vbg, int initial_height)
51         : editor (ed)
52                 , videotl_bar_group(vbg)
53                 , bar_height(initial_height)
54 {
55         video_start_offset = 0L;
56         video_offset = 0L;
57         video_offset_p = 0L;
58         video_duration = 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 = Config->get_video_server_url();
65         server_docroot   = Config->get_video_server_docroot();
66         video_filename = "";
67         local_file = true;
68         video_file_fps = 25.0;
69         flush_frames = false;
70         vmonitor=0;
71         reopen_vmonitor=false;
72         find_xjadeo();
73
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());
76 }
77
78 VideoTimeLine::~VideoTimeLine ()
79 {
80         close_session();
81 }
82
83 /* close and save settings */
84 void
85 VideoTimeLine::save_session ()
86 {
87         if (!_session) {
88                 return;
89         }
90
91         LocaleGuard lg (X_("POSIX"));
92
93         bool is_dirty = false;
94
95         XMLNode* prev = _session->extra_xml (X_("Videomonitor"));
96
97         /* remember if vmonitor was open.. */
98         XMLNode* node = new XMLNode(X_("Videomonitor"));
99
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
104         }
105
106         /* VTL settings */
107         node = _session->extra_xml (X_("Videotimeline"));
108
109         if (node) {
110                 if (!(node->property(X_("id")) && node->property(X_("id"))->value() == id().to_s())) {
111                         node->add_property (X_("id"), id().to_s());
112                         is_dirty=true;
113                 }
114         }
115
116         /* remember timeline height.. */
117         if (node) {
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);
121                         is_dirty=true;
122                 }
123         }
124
125         /* save video-offset-lock */
126         if (node) {
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"));
129                         is_dirty=true;
130                 }
131         }
132         /* save video-offset */
133         if (node) {
134                 if (!(node->property(X_("VideoOffset")) && atoll(node->property(X_("VideoOffset"))->value().c_str())==video_offset)) {
135                         node->add_property (X_("VideoOffset"), video_offset);
136                         is_dirty=true;
137                 }
138         }
139
140         /* save 'auto_set_session_fps' */
141         if (node) {
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"));
144                         is_dirty=true;
145                 }
146         }
147         if (is_dirty) {
148                 _session->set_dirty ();
149         }
150 }
151
152 /* close and save settings */
153 void
154 VideoTimeLine::close_session ()
155 {
156         close_video_monitor();
157         save_session();
158
159         remove_frames();
160         video_filename = "";
161         video_duration = 0L;
162 }
163
164 /** load settings from session */
165 void
166 VideoTimeLine::set_session (ARDOUR::Session *s)
167 {
168         SessionHandlePtr::set_session (s);
169         if (!_session) { return ; }
170
171         LocaleGuard lg (X_("POSIX"));
172
173         XMLNode* node = _session->extra_xml (X_("Videotimeline"));
174         if (node) {
175                 ARDOUR_UI::instance()->start_video_server((Gtk::Window*)0, false);
176
177                 set_id(*node);
178
179                 const XMLProperty* proph = node->property (X_("Height"));
180                 if (proph) {
181                         editor->set_video_timeline_height(atoi(proph->value().c_str()));
182                 }
183 #if 0 /* TODO THINK: set FPS first time only ?! */
184                 const XMLProperty* propasfps = node->property (X_("AutoFPS"));
185                 if (propasfps) {
186                         auto_set_session_fps = atoi(propasfps->value().c_str())?true:false;
187                 }
188 #endif
189
190                 const XMLProperty* propoffset = node->property (X_("VideoOffset"));
191                 if (propoffset) {
192                         video_offset = atoll(propoffset->value().c_str());
193                         video_offset_p = video_offset;
194                 }
195
196                 const XMLProperty* proplock = node->property (X_("VideoOffsetLock"));
197                 if (proplock) {
198                         video_offset_lock = atoi(proplock->value().c_str())?true:false;
199                 }
200
201                 const XMLProperty* localfile = node->property (X_("LocalFile"));
202                 if (localfile) {
203                         local_file = atoi(localfile->value().c_str())?true:false;
204                 }
205
206                 const XMLProperty* propf = node->property (X_("Filename"));
207                 video_file_info(propf->value(), local_file);
208         }
209
210         node = _session->extra_xml (X_("Videomonitor"));
211         if (node) {
212                 const XMLProperty* prop = node->property (X_("active"));
213                 if (prop->value() == "yes" && found_xjadeo() && !video_filename.empty() && local_file) {
214                         open_video_monitor(false);
215                 }
216         }
217
218         _session->register_with_memento_command_factory(id(), this);
219         _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoTimeLine::parameter_changed, this, _1), gui_context());
220 }
221
222 void
223 VideoTimeLine::save_undo ()
224 {
225         video_offset_p = video_offset;
226 }
227
228 int
229 VideoTimeLine::set_state (const XMLNode& node, int /*version*/)
230 {
231         LocaleGuard lg (X_("POSIX"));
232         const XMLProperty* propoffset = node.property (X_("VideoOffset"));
233         if (propoffset) {
234                 video_offset = atoll(propoffset->value().c_str());
235         }
236         ARDOUR_UI::instance()->flush_videotimeline_cache(true);
237         return 0;
238 }
239
240 XMLNode&
241 VideoTimeLine::get_state ()
242 {
243         XMLNode* node = new XMLNode (X_("Videotimeline"));
244         LocaleGuard lg (X_("POSIX"));
245         node->add_property (X_("VideoOffset"), video_offset_p);
246         return *node;
247 }
248
249 void
250 VideoTimeLine::remove_frames ()
251 {
252         for (VideoFrames::iterator i = video_frames.begin(); i != video_frames.end(); ++i ) {
253                 VideoImageFrame *frame = (*i);
254                 delete frame;
255                 (*i) = 0;
256         }
257         video_frames.clear();
258 }
259
260 VideoImageFrame *
261 VideoTimeLine::get_video_frame (framepos_t vfn, int cut, int rightend)
262 {
263         if (vfn==0) cut=0;
264         for (VideoFrames::iterator i = video_frames.begin(); i != video_frames.end(); ++i) {
265                 VideoImageFrame *frame = (*i);
266                 if (abs(frame->get_video_frame_number()-vfn)<=cut
267                     && frame->get_rightend() == rightend) { return frame; }
268         }
269         return 0;
270 }
271
272 float
273 VideoTimeLine::get_apv()
274 {
275         // XXX: dup code - TODO use this fn in update_video_timeline()
276         float apv = -1; /* audio frames per video frame; */
277         if (!_session) return apv;
278
279         if (_session->config.get_use_video_file_fps()) {
280                 if (video_file_fps == 0 ) return apv;
281         } else {
282                 if (_session->timecode_frames_per_second() == 0 ) return apv;
283         }
284
285         if (_session->config.get_videotimeline_pullup()) {
286                 apv = _session->frame_rate();
287         } else {
288                 apv = _session->nominal_frame_rate();
289         }
290         if (_session->config.get_use_video_file_fps()) {
291                 apv /= video_file_fps;
292         } else {
293                 apv /= _session->timecode_frames_per_second();
294         }
295         return apv;
296 }
297
298 void
299 VideoTimeLine::update_video_timeline()
300 {
301         if (!_session) return;
302
303         if (_session->config.get_use_video_file_fps()) {
304                 if (video_file_fps == 0 ) return;
305         } else {
306                 if (_session->timecode_frames_per_second() == 0 ) return;
307         }
308
309         double frames_per_unit = editor->unit_to_frame(1.0);
310         framepos_t leftmost_frame =  editor->leftmost_position();
311
312         /* Outline:
313          * 1) calculate how many frames there should be in current zoom (plus 1 page on each side)
314          * 2) calculate first frame and distance between video-frames (according to zoom)
315          * 3) destroy/add frames
316          * 4) reposition existing frames
317          * 5) assign framenumber to frames -> request/decode video.
318          */
319
320         /* video-file and session properties */
321         double display_vframe_width; /* unit: pixels ; width of one thumbnail in the timeline */
322         float apv; /* audio frames per video frame; */
323         framepos_t leftmost_video_frame; /* unit: video-frame number ; temporary var -> vtl_start */
324
325         /* variables needed to render videotimeline -- what needs to computed first */
326         framepos_t vtl_start; /* unit: audio-frames ; first displayed video-frame */
327         framepos_t vtl_dist;  /* unit: audio-frames ; distance between displayed video-frames */
328         unsigned int visible_video_frames; /* number of frames that fit on current canvas */
329
330         if (_session->config.get_videotimeline_pullup()) {
331                 apv = _session->frame_rate();
332         } else {
333                 apv = _session->nominal_frame_rate();
334         }
335         if (_session->config.get_use_video_file_fps()) {
336                 apv /= video_file_fps;
337         } else {
338                 apv /= _session->timecode_frames_per_second();
339         }
340
341         display_vframe_width = bar_height * video_aspect_ratio;
342
343         if (apv > frames_per_unit * display_vframe_width) {
344                 /* high-zoom: need space between successive video-frames */
345                 vtl_dist = rint(apv);
346         } else {
347                 /* continous timeline: skip video-frames */
348                 vtl_dist = ceil(display_vframe_width * frames_per_unit / apv) * apv;
349         }
350
351         assert (vtl_dist > 0);
352         assert (apv > 0);
353
354 #define GOFFSET (video_offset)
355
356         leftmost_video_frame = floor (floor((leftmost_frame - video_start_offset - GOFFSET ) / vtl_dist) * vtl_dist / apv);
357
358         vtl_start = rint (GOFFSET + video_start_offset + leftmost_video_frame * apv);
359         visible_video_frames = 2 + ceil(editor->current_page_frames() / vtl_dist); /* +2 left+right partial frames */
360
361         /* expand timeline (cache next/prev page images) */
362         vtl_start -= visible_video_frames * vtl_dist;
363         visible_video_frames *=3;
364
365         if (vtl_start < GOFFSET ) {
366                 visible_video_frames += ceil(vtl_start/vtl_dist);
367                 vtl_start = GOFFSET;
368         }
369
370         /* apply video-file constraints */
371         if (vtl_start > video_start_offset + video_duration + GOFFSET ) {
372                 visible_video_frames = 0;
373         }
374         /* TODO optimize: compute rather than iterate */
375         while (visible_video_frames > 0 && vtl_start + (visible_video_frames-1) * vtl_dist >= video_start_offset + video_duration + GOFFSET) {
376                 --visible_video_frames;
377         }
378
379         if (flush_frames) {
380                 remove_frames();
381                 flush_frames=false;
382         }
383
384         while (video_frames.size() < visible_video_frames) {
385                 VideoImageFrame *frame;
386                 frame = new VideoImageFrame(*editor, *videotl_bar_group, display_vframe_width, bar_height, video_server_url, translated_filename());
387                 frame->ImgChanged.connect (*this, invalidator (*this), boost::bind (&PublicEditor::queue_visual_videotimeline_update, editor), gui_context());
388                 video_frames.push_back(frame);
389         }
390
391         VideoFrames outdated_video_frames;
392         std::list<int> remaining;
393
394         outdated_video_frames = video_frames;
395
396 #if 1
397         /* when zoomed out, ignore shifts by +-1 frame
398          * which can occur due to rounding errors when
399          * scrolling to a new leftmost-audio frame.
400          */
401         int cut =1;
402         if (vtl_dist/apv < 3.0) cut =0;
403 #else
404         int cut =0;
405 #endif
406
407         for (unsigned int vfcount=0; vfcount < visible_video_frames; ++vfcount){
408                 framepos_t vfpos = vtl_start + vfcount * vtl_dist; /* unit: audio-frames */
409                 framepos_t vframeno = rint ( (vfpos - GOFFSET) / apv); /* unit: video-frames */
410                 vfpos = (vframeno * apv ) + GOFFSET; /* audio-frame  corresponding to /rounded/ video-frame */
411
412                 int rightend = -1; /* unit: pixels */
413                 if (vfpos + vtl_dist > video_start_offset + video_duration + GOFFSET) {
414                         rightend = display_vframe_width * (video_start_offset + video_duration + GOFFSET - vfpos) / vtl_dist;
415                         //printf("lf(e): %lu\n", vframeno); // XXX
416                 }
417                 VideoImageFrame * frame = get_video_frame(vframeno, cut, rightend);
418                 if (frame) {
419                   frame->set_position(vfpos-leftmost_frame);
420                         outdated_video_frames.remove(frame);
421                 } else {
422                         remaining.push_back(vfcount);
423                 }
424         }
425
426         for (VideoFrames::iterator i = outdated_video_frames.begin(); i != outdated_video_frames.end(); ++i ) {
427                 VideoImageFrame *frame = (*i);
428                 if (remaining.empty()) {
429                   frame->set_position(-2 * vtl_dist); /* move off screen */
430                 } else {
431                         int vfcount=remaining.front();
432                         remaining.pop_front();
433                         framepos_t vfpos = vtl_start + vfcount * vtl_dist; /* unit: audio-frames */
434                         framepos_t vframeno = rint ((vfpos - GOFFSET) / apv);  /* unit: video-frames */
435                         int rightend = -1; /* unit: pixels */
436                         if (vfpos + vtl_dist > video_start_offset + video_duration + GOFFSET) {
437                                 rightend = display_vframe_width * (video_start_offset + video_duration + GOFFSET - vfpos) / vtl_dist;
438                                 //printf("lf(n): %lu\n", vframeno); // XXX
439                         }
440                         frame->set_position(vfpos-leftmost_frame);
441                         frame->set_videoframe(vframeno, rightend);
442                 }
443         }
444 }
445
446 std::string
447 VideoTimeLine::translated_filename ()
448 {
449         if (!local_file){
450                 return video_filename;
451         } else {
452                 return video_map_path(server_docroot, video_filename);
453         }
454 }
455
456 bool
457 VideoTimeLine::video_file_info (std::string filename, bool local)
458 {
459         local_file = local;
460         if (filename.at(0) == G_DIR_SEPARATOR || !local_file) {
461                 video_filename = filename;
462         }  else {
463                 video_filename = Glib::build_filename (_session->session_directory().video_path(), filename);
464         }
465
466         long long int _duration;
467         double _start_offset;
468
469         if (!video_query_info(
470                         video_server_url, translated_filename(),
471                         video_file_fps, _duration, _start_offset, video_aspect_ratio)) {
472                 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;
473                 return false;
474         }
475         video_duration = _duration * _session->nominal_frame_rate() / video_file_fps;
476         video_start_offset = _start_offset * _session->nominal_frame_rate();
477
478         if (auto_set_session_fps && video_file_fps != _session->timecode_frames_per_second()) {
479                 switch ((int)floorf(video_file_fps*1000.0)) {
480                         case 23976:
481                                 _session->config.set_timecode_format(timecode_23976);
482                                 break;
483                         case 24000:
484                                 _session->config.set_timecode_format(timecode_24);
485                                 break;
486                         case 24975:
487                         case 24976:
488                                 _session->config.set_timecode_format(timecode_24976);
489                                 break;
490                         case 25000:
491                                 _session->config.set_timecode_format(timecode_25);
492                                 break;
493                         case 29970:
494                                 _session->config.set_timecode_format(timecode_2997drop);
495                                 break;
496                         case 30000:
497                                 _session->config.set_timecode_format(timecode_30);
498                                 break;
499                         case 59940:
500                                 _session->config.set_timecode_format(timecode_5994);
501                                 break;
502                         case 60000:
503                                 _session->config.set_timecode_format(timecode_60);
504                                 break;
505                         default:
506                                 warning << _("Failed to set session-framerate: ") << video_file_fps << _(" does not have a corresponding option setting in Ardour.") << endmsg; /* TODO: gettext arg */
507                                 break;
508                 }
509                 _session->config.set_video_pullup(0); /* TODO only set if set_timecode_format() was successful ?!*/
510         }
511         if (video_file_fps != _session->timecode_frames_per_second()) {
512                 warning << _("Video file's framerate is not equal to Ardour session timecode's framerate: ")
513                         << video_file_fps << _(" vs ") << _session->timecode_frames_per_second() << endmsg;
514         }
515         flush_local_cache ();
516
517         if (found_xjadeo() && local_file) {
518                 GuiUpdate("set-xjadeo-sensitive-on");
519                 if (vmonitor && vmonitor->is_started()) {
520                         vmonitor->set_fps(video_file_fps);
521                         vmonitor->open(video_filename);
522                 }
523         } else if (!local_file) {
524                 GuiUpdate("set-xjadeo-sensitive-off");
525         }
526         VtlUpdate();
527         return true;
528 }
529
530 bool
531 VideoTimeLine::check_server ()
532 {
533         bool ok = false;
534         char url[1024];
535         snprintf(url, sizeof(url), "%s%sstatus"
536                         , video_server_url.c_str()
537                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
538                         );
539         char *res=curl_http_get(url, NULL);
540         if (res) {
541                 if (strstr(res, "status: ok, online.")) { ok = true; }
542                 free(res);
543         }
544         return ok;
545 }
546
547 void
548 VideoTimeLine::gui_update(std::string const & t) {
549         /* this is to be called via GuiUpdate() only. */
550         ENSURE_GUI_THREAD (*this, &VideoTimeLine::queue_visual_videotimeline_update)
551         if (t == "videotimeline-update") {
552                 editor->queue_visual_videotimeline_update();
553         } else if (t == "set-xjadeo-active-off") {
554                 editor->toggle_xjadeo_proc(0);
555         } else if (t == "set-xjadeo-active-on") {
556                 editor->toggle_xjadeo_proc(1);
557         } else if (t == "set-xjadeo-sensitive-on") {
558                 editor->set_xjadeo_sensitive(true);
559         } else if (t == "set-xjadeo-sensitive-off") {
560                 editor->toggle_xjadeo_proc(0);
561                 //close_video_monitor();
562                 editor->set_xjadeo_sensitive(false);
563         }
564 }
565
566 void
567 VideoTimeLine::set_height (int height) {
568         bar_height = height;
569         flush_local_cache();
570 }
571
572 void
573 VideoTimeLine::vmon_update () {
574         if (vmonitor && vmonitor->is_started()) {
575                 vmonitor->set_offset( GOFFSET); // TODO proper re-init xjadeo w/o restart not just offset.
576         }
577 }
578
579 void
580 VideoTimeLine::flush_local_cache () {
581         flush_frames = true;
582         vmon_update();
583 }
584
585 void
586 VideoTimeLine::flush_cache () {
587         flush_local_cache();
588         char url[1024];
589         snprintf(url, sizeof(url), "%s%sadmin/flush_cache"
590                         , video_server_url.c_str()
591                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
592                         );
593         char *res=curl_http_get(url, NULL);
594         if (res) {
595                 free (res);
596         }
597         if (vmonitor && vmonitor->is_started()) {
598                 reopen_vmonitor=true;
599                 vmonitor->quit();
600         }
601         video_file_info(video_filename, local_file);
602 }
603
604 /* config */
605 void
606 VideoTimeLine::parameter_changed (std::string const & p)
607 {
608         if (p == "video-server-url") {
609                 set_video_server_url (Config->get_video_server_url ());
610         } else if (p == "video-server-docroot") {
611                 set_video_server_docroot (Config->get_video_server_docroot ());
612         }
613         if (p == "use-video-file-fps" || p == "videotimeline-pullup" ) { /* session->config parameter */
614                 VtlUpdate();
615         }
616 }
617
618 void
619 VideoTimeLine::set_video_server_url(std::string vsu) {
620         flush_local_cache ();
621         video_server_url = vsu;
622         VtlUpdate();
623 }
624
625 void
626 VideoTimeLine::set_video_server_docroot(std::string vsr) {
627         flush_local_cache ();
628         server_docroot = vsr;
629         VtlUpdate();
630 }
631
632 /* video-monitor for this timeline */
633 void
634 VideoTimeLine::find_xjadeo () {
635         std::string xjadeo_file_path;
636         if (getenv("XJREMOTE")) {
637                 _xjadeo_bin = strdup(getenv("XJREMOTE")); // XXX TODO: free it?!
638         } else if (find_file_in_search_path (SearchPath(Glib::getenv("PATH")), X_("xjremote"), xjadeo_file_path)) {
639                 _xjadeo_bin = xjadeo_file_path;
640         }
641         else if (Glib::file_test(X_("/Applications/Jadeo.app/Contents/MacOS/xjremote"), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
642                 _xjadeo_bin = X_("/Applications/Jadeo.app/Contents/MacOS/xjremote");
643         }
644         /* TODO: win32: allow to configure PATH to xjremote */
645         else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjremote.exe"), Glib::FILE_TEST_EXISTS)) {
646                 _xjadeo_bin = X_("C:\\Program Files\\xjadeo\\xjremote.exe");
647         }
648         else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjremote.bat"), Glib::FILE_TEST_EXISTS)) {
649                 _xjadeo_bin = X_("C:\\Program Files\\xjadeo\\xjremote.bat");
650         }
651         else  {
652                 _xjadeo_bin = X_("");
653         }
654 }
655
656 void
657 VideoTimeLine::open_video_monitor(bool interactive) {
658         if (!found_xjadeo()) return;
659         if (!vmonitor) {
660                 vmonitor = new VideoMonitor(editor, _xjadeo_bin);
661                 vmonitor->set_session(_session);
662                 vmonitor->Terminated.connect (sigc::mem_fun (*this, &VideoTimeLine::terminated_video_monitor));
663         } else if (vmonitor->is_started()) {
664                 return;
665         }
666
667         int xj_settings_mask = vmonitor->restore_settings_mask();
668         if (_session) {
669                 /* load mask from Session */
670                 XMLNode* node = _session->extra_xml (X_("XJRestoreSettings"));
671                 if (node) {
672                         const XMLProperty* prop = node->property (X_("mask"));
673                         if (prop) {
674                                 xj_settings_mask = atoi(prop->value().c_str());
675                         }
676                 }
677         }
678
679         if (interactive && Config->get_video_monitor_setup_dialog()) {
680                 if (open_video_monitor_dialog == 0) {
681                         open_video_monitor_dialog = new OpenVideoMonitorDialog(_session);
682                 }
683                 if (open_video_monitor_dialog->is_visible()) {
684                         return;
685                 }
686                 open_video_monitor_dialog->setup_settings_mask(xj_settings_mask);
687                 open_video_monitor_dialog->set_filename(video_filename);
688                 Gtk::ResponseType r = (Gtk::ResponseType) open_video_monitor_dialog->run ();
689                 open_video_monitor_dialog->hide();
690                 if (r != Gtk::RESPONSE_ACCEPT) {
691                         GuiUpdate("set-xjadeo-active-off");
692                         return;
693                 }
694
695                 if (_session && (xj_settings_mask != open_video_monitor_dialog->xj_settings_mask()) ) {
696                         /* save mask to Session */
697                         XMLNode* node = new XMLNode(X_("XJRestoreSettings"));
698                         node->add_property (X_("mask"), (const long) open_video_monitor_dialog->xj_settings_mask() );
699                         _session->add_extra_xml (*node);
700                         _session->set_dirty ();
701                 }
702
703                 if (open_video_monitor_dialog->show_again()) {
704                         Config->set_video_monitor_setup_dialog(false);
705                 }
706 #if 1
707                 vmonitor->set_debug(open_video_monitor_dialog->enable_debug());
708 #endif
709                 vmonitor->restore_settings_mask(open_video_monitor_dialog->xj_settings_mask());
710         } else {
711                 vmonitor->restore_settings_mask(xj_settings_mask);
712         }
713
714
715         if (!vmonitor->start()) {
716                 warning << "launching xjadeo failed.." << endmsg;
717                 close_video_monitor();
718         } else {
719                 GuiUpdate("set-xjadeo-active-on");
720                 vmonitor->set_fps(video_file_fps);
721                 vmonitor->open(video_filename);
722         }
723 }
724
725 void
726 VideoTimeLine::close_video_monitor() {
727         if (vmonitor && vmonitor->is_started()) {
728                 vmonitor->quit();
729         }
730 }
731
732 void
733 VideoTimeLine::terminated_video_monitor () {
734         if (vmonitor) {
735                 delete vmonitor;
736         }
737         GuiUpdate("set-xjadeo-active-off");
738         vmonitor=0;
739   if (reopen_vmonitor) {
740                 reopen_vmonitor=false;
741                 open_video_monitor(false);
742         }
743 }
744
745 /*
746 void
747 VideoTimeLine::clear_video_monitor_session_state ()
748 {
749         if (vmonitor) {
750                 vmonitor->clear_session_state();
751         } else {
752           if (!_session) { return; }
753                 XMLNode* node = new XMLNode(X_("XJSettings"));
754                 _session->add_extra_xml (*node);
755                 _session->set_dirty ();
756         }
757 }
758 */
759
760 void
761 VideoTimeLine::manual_seek_video_monitor (framepos_t pos)
762 {
763         if (!vmonitor) { return; }
764         if (!vmonitor->is_started()) { return; }
765         if (!vmonitor->synced_by_manual_seeks()) { return; }
766         vmonitor->manual_seek(pos, false, GOFFSET); // XXX -> set offset in xjadeo
767 }
768
769 #endif /* WITH_VIDEOTIMELINE */