vtl: video sets session start/end markers.
[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 #ifndef MAX
50 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
51 #endif
52
53 VideoTimeLine::VideoTimeLine (PublicEditor *ed, ArdourCanvas::Group *vbg, int initial_height)
54         : editor (ed)
55                 , videotl_bar_group(vbg)
56                 , bar_height(initial_height)
57 {
58         video_start_offset = 0L;
59         video_offset = 0L;
60         video_offset_p = 0L;
61         video_duration = 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();
69         video_filename = "";
70         local_file = true;
71         video_file_fps = 25.0;
72         flush_frames = false;
73         vmonitor=0;
74         reopen_vmonitor=false;
75         find_xjadeo();
76
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());
79 }
80
81 VideoTimeLine::~VideoTimeLine ()
82 {
83         close_session();
84 }
85
86 /* close and save settings */
87 void
88 VideoTimeLine::save_session ()
89 {
90         if (!_session) {
91                 return;
92         }
93
94         LocaleGuard lg (X_("POSIX"));
95
96         bool is_dirty = false;
97
98         XMLNode* prev = _session->extra_xml (X_("Videomonitor"));
99
100         /* remember if vmonitor was open.. */
101         XMLNode* node = new XMLNode(X_("Videomonitor"));
102
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
107         }
108
109         /* VTL settings */
110         node = _session->extra_xml (X_("Videotimeline"));
111
112         if (node) {
113                 if (!(node->property(X_("id")) && node->property(X_("id"))->value() == id().to_s())) {
114                         node->add_property (X_("id"), id().to_s());
115                         is_dirty=true;
116                 }
117         }
118
119         /* remember timeline height.. */
120         if (node) {
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);
124                         is_dirty=true;
125                 }
126         }
127
128         /* save video-offset-lock */
129         if (node) {
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"));
132                         is_dirty=true;
133                 }
134         }
135         /* save video-offset */
136         if (node) {
137                 if (!(node->property(X_("VideoOffset")) && atoll(node->property(X_("VideoOffset"))->value().c_str())==video_offset)) {
138                         node->add_property (X_("VideoOffset"), video_offset);
139                         is_dirty=true;
140                 }
141         }
142
143         /* save 'auto_set_session_fps' */
144         if (node) {
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"));
147                         is_dirty=true;
148                 }
149         }
150         if (is_dirty) {
151                 _session->set_dirty ();
152         }
153 }
154
155 /* close and save settings */
156 void
157 VideoTimeLine::close_session ()
158 {
159         close_video_monitor();
160         save_session();
161
162         remove_frames();
163         video_filename = "";
164         video_duration = 0L;
165 }
166
167 /** load settings from session */
168 void
169 VideoTimeLine::set_session (ARDOUR::Session *s)
170 {
171         SessionHandlePtr::set_session (s);
172         if (!_session) { return ; }
173
174         LocaleGuard lg (X_("POSIX"));
175
176         XMLNode* node = _session->extra_xml (X_("Videotimeline"));
177         if (node) {
178                 ARDOUR_UI::instance()->start_video_server((Gtk::Window*)0, false);
179
180                 set_id(*node);
181
182                 const XMLProperty* proph = node->property (X_("Height"));
183                 if (proph) {
184                         editor->set_video_timeline_height(atoi(proph->value().c_str()));
185                 }
186 #if 0 /* TODO THINK: set FPS first time only ?! */
187                 const XMLProperty* propasfps = node->property (X_("AutoFPS"));
188                 if (propasfps) {
189                         auto_set_session_fps = atoi(propasfps->value().c_str())?true:false;
190                 }
191 #endif
192
193                 const XMLProperty* propoffset = node->property (X_("VideoOffset"));
194                 if (propoffset) {
195                         video_offset = atoll(propoffset->value().c_str());
196                         video_offset_p = video_offset;
197                 }
198
199                 const XMLProperty* proplock = node->property (X_("VideoOffsetLock"));
200                 if (proplock) {
201                         video_offset_lock = atoi(proplock->value().c_str())?true:false;
202                 }
203
204                 const XMLProperty* localfile = node->property (X_("LocalFile"));
205                 if (localfile) {
206                         local_file = atoi(localfile->value().c_str())?true:false;
207                 }
208
209                 const XMLProperty* propf = node->property (X_("Filename"));
210                 video_file_info(propf->value(), local_file);
211         }
212
213         node = _session->extra_xml (X_("Videomonitor"));
214         if (node) {
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);
218                 }
219         }
220
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());
223 }
224
225 void
226 VideoTimeLine::save_undo ()
227 {
228         video_offset_p = video_offset;
229 }
230
231 int
232 VideoTimeLine::set_state (const XMLNode& node, int /*version*/)
233 {
234         LocaleGuard lg (X_("POSIX"));
235         const XMLProperty* propoffset = node.property (X_("VideoOffset"));
236         if (propoffset) {
237                 video_offset = atoll(propoffset->value().c_str());
238         }
239         ARDOUR_UI::instance()->flush_videotimeline_cache(true);
240         return 0;
241 }
242
243 XMLNode&
244 VideoTimeLine::get_state ()
245 {
246         XMLNode* node = new XMLNode (X_("Videotimeline"));
247         LocaleGuard lg (X_("POSIX"));
248         node->add_property (X_("VideoOffset"), video_offset_p);
249         return *node;
250 }
251
252 void
253 VideoTimeLine::remove_frames ()
254 {
255         for (VideoFrames::iterator i = video_frames.begin(); i != video_frames.end(); ++i ) {
256                 VideoImageFrame *frame = (*i);
257                 delete frame;
258                 (*i) = 0;
259         }
260         video_frames.clear();
261 }
262
263 VideoImageFrame *
264 VideoTimeLine::get_video_frame (framepos_t vfn, int cut, int rightend)
265 {
266         if (vfn==0) cut=0;
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; }
271         }
272         return 0;
273 }
274
275 float
276 VideoTimeLine::get_apv()
277 {
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;
281
282         if (_session->config.get_use_video_file_fps()) {
283                 if (video_file_fps == 0 ) return apv;
284         } else {
285                 if (_session->timecode_frames_per_second() == 0 ) return apv;
286         }
287
288         if (_session->config.get_videotimeline_pullup()) {
289                 apv = _session->frame_rate();
290         } else {
291                 apv = _session->nominal_frame_rate();
292         }
293         if (_session->config.get_use_video_file_fps()) {
294                 apv /= video_file_fps;
295         } else {
296                 apv /= _session->timecode_frames_per_second();
297         }
298         return apv;
299 }
300
301 void
302 VideoTimeLine::update_video_timeline()
303 {
304         if (!_session) return;
305
306         if (_session->config.get_use_video_file_fps()) {
307                 if (video_file_fps == 0 ) return;
308         } else {
309                 if (_session->timecode_frames_per_second() == 0 ) return;
310         }
311
312         double frames_per_unit = editor->unit_to_frame(1.0);
313         framepos_t leftmost_frame =  editor->leftmost_position();
314
315         /* Outline:
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.
321          */
322
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 */
327
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 */
332
333         if (_session->config.get_videotimeline_pullup()) {
334                 apv = _session->frame_rate();
335         } else {
336                 apv = _session->nominal_frame_rate();
337         }
338         if (_session->config.get_use_video_file_fps()) {
339                 apv /= video_file_fps;
340         } else {
341                 apv /= _session->timecode_frames_per_second();
342         }
343
344         display_vframe_width = bar_height * video_aspect_ratio;
345
346         if (apv > frames_per_unit * display_vframe_width) {
347                 /* high-zoom: need space between successive video-frames */
348                 vtl_dist = rint(apv);
349         } else {
350                 /* continous timeline: skip video-frames */
351                 vtl_dist = ceil(display_vframe_width * frames_per_unit / apv) * apv;
352         }
353
354         assert (vtl_dist > 0);
355         assert (apv > 0);
356
357 #define GOFFSET (video_offset)
358
359         leftmost_video_frame = floor (floor((leftmost_frame - video_start_offset - GOFFSET ) / vtl_dist) * vtl_dist / apv);
360
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 */
363
364         /* expand timeline (cache next/prev page images) */
365         vtl_start -= visible_video_frames * vtl_dist;
366         visible_video_frames *=3;
367
368         if (vtl_start < GOFFSET ) {
369                 visible_video_frames += ceil(vtl_start/vtl_dist);
370                 vtl_start = GOFFSET;
371         }
372
373         /* apply video-file constraints */
374         if (vtl_start > video_start_offset + video_duration + GOFFSET ) {
375                 visible_video_frames = 0;
376         }
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;
380         }
381
382         if (flush_frames) {
383                 remove_frames();
384                 flush_frames=false;
385         }
386
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);
392         }
393
394         VideoFrames outdated_video_frames;
395         std::list<int> remaining;
396
397         outdated_video_frames = video_frames;
398
399 #if 1
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.
403          */
404         int cut =1;
405         if (vtl_dist/apv < 3.0) cut =0;
406 #else
407         int cut =0;
408 #endif
409
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 */
414
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
419                 }
420                 VideoImageFrame * frame = get_video_frame(vframeno, cut, rightend);
421                 if (frame) {
422                   frame->set_position(vfpos-leftmost_frame);
423                         outdated_video_frames.remove(frame);
424                 } else {
425                         remaining.push_back(vfcount);
426                 }
427         }
428
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 */
433                 } else {
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
442                         }
443                         frame->set_position(vfpos-leftmost_frame);
444                         frame->set_videoframe(vframeno, rightend);
445                 }
446         }
447 }
448
449 std::string
450 VideoTimeLine::translated_filename ()
451 {
452         if (!local_file){
453                 return video_filename;
454         } else {
455                 return video_map_path(server_docroot, video_filename);
456         }
457 }
458
459 bool
460 VideoTimeLine::video_file_info (std::string filename, bool local)
461 {
462
463         local_file = local;
464         if (filename.at(0) == G_DIR_SEPARATOR || !local_file) {
465                 video_filename = filename;
466         }  else {
467                 video_filename = Glib::build_filename (_session->session_directory().video_path(), filename);
468         }
469
470         long long int _duration;
471         double _start_offset;
472
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;
477                 return false;
478         }
479         video_duration = _duration * _session->nominal_frame_rate() / video_file_fps;
480         video_start_offset = _start_offset * _session->nominal_frame_rate();
481
482         if (auto_set_session_fps && video_file_fps != _session->timecode_frames_per_second()) {
483                 switch ((int)floorf(video_file_fps*1000.0)) {
484                         case 23976:
485                                 _session->config.set_timecode_format(timecode_23976);
486                                 break;
487                         case 24000:
488                                 _session->config.set_timecode_format(timecode_24);
489                                 break;
490                         case 24975:
491                         case 24976:
492                                 _session->config.set_timecode_format(timecode_24976);
493                                 break;
494                         case 25000:
495                                 _session->config.set_timecode_format(timecode_25);
496                                 break;
497                         case 29970:
498                                 _session->config.set_timecode_format(timecode_2997drop);
499                                 break;
500                         case 30000:
501                                 _session->config.set_timecode_format(timecode_30);
502                                 break;
503                         case 59940:
504                                 _session->config.set_timecode_format(timecode_5994);
505                                 break;
506                         case 60000:
507                                 _session->config.set_timecode_format(timecode_60);
508                                 break;
509                         default:
510                                 warning << _("Failed to set session-framerate: ") << video_file_fps << _(" does not have a corresponding option setting in Ardour.") << endmsg; /* TODO: gettext arg */
511                                 break;
512                 }
513                 _session->config.set_video_pullup(0); /* TODO only set if set_timecode_format() was successful ?!*/
514         }
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;
518         }
519         flush_local_cache ();
520
521         _session->maybe_update_session_range(
522                         MAX(get_offset(), 0),
523                         MAX(get_offset() + get_duration(), 0)
524                         );
525
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);
531                 }
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");
537 #endif
538         }
539         VtlUpdate();
540         return true;
541 }
542
543 bool
544 VideoTimeLine::check_server ()
545 {
546         bool ok = false;
547         char url[1024];
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) == '/')?"":"/"
551                         );
552         char *res=curl_http_get(url, NULL);
553         if (res) {
554                 if (strstr(res, "status: ok, online.")) { ok = true; }
555                 free(res);
556         }
557         return ok;
558 }
559
560 void
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);
576         }
577 }
578
579 void
580 VideoTimeLine::set_height (int height) {
581         bar_height = height;
582         flush_local_cache();
583 }
584
585 void
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.
589         }
590 }
591
592 void
593 VideoTimeLine::flush_local_cache () {
594         flush_frames = true;
595         vmon_update();
596 }
597
598 void
599 VideoTimeLine::flush_cache () {
600         flush_local_cache();
601         char url[1024];
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) == '/')?"":"/"
605                         );
606         char *res=curl_http_get(url, NULL);
607         if (res) {
608                 free (res);
609         }
610         if (vmonitor && vmonitor->is_started()) {
611                 reopen_vmonitor=true;
612                 vmonitor->quit();
613         }
614         video_file_info(video_filename, local_file);
615 }
616
617 /* config */
618 void
619 VideoTimeLine::parameter_changed (std::string const & p)
620 {
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 ());
625         }
626         if (p == "use-video-file-fps" || p == "videotimeline-pullup" ) { /* session->config parameter */
627                 VtlUpdate();
628         }
629 }
630
631 void
632 VideoTimeLine::set_video_server_url(std::string vsu) {
633         flush_local_cache ();
634         video_server_url = vsu;
635         VtlUpdate();
636 }
637
638 void
639 VideoTimeLine::set_video_server_docroot(std::string vsr) {
640         flush_local_cache ();
641         server_docroot = vsr;
642         VtlUpdate();
643 }
644
645 /* video-monitor for this timeline */
646 void
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;
653         }
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");
656         }
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");
660         }
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");
663         }
664         else  {
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').")
669                         << endmsg;
670         }
671 }
672
673 void
674 VideoTimeLine::open_video_monitor(bool interactive) {
675         if (!found_xjadeo()) return;
676         if (!vmonitor) {
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()) {
681                 return;
682         }
683
684         int xj_settings_mask = vmonitor->restore_settings_mask();
685         if (_session) {
686                 /* load mask from Session */
687                 XMLNode* node = _session->extra_xml (X_("XJRestoreSettings"));
688                 if (node) {
689                         const XMLProperty* prop = node->property (X_("mask"));
690                         if (prop) {
691                                 xj_settings_mask = atoi(prop->value().c_str());
692                         }
693                 }
694         }
695
696         if (interactive && Config->get_video_monitor_setup_dialog()) {
697                 if (open_video_monitor_dialog == 0) {
698                         open_video_monitor_dialog = new OpenVideoMonitorDialog(_session);
699                 }
700                 if (open_video_monitor_dialog->is_visible()) {
701                         return;
702                 }
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");
709                         return;
710                 }
711
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 ();
718                 }
719
720                 if (open_video_monitor_dialog->show_again()) {
721                         Config->set_video_monitor_setup_dialog(false);
722                 }
723 #if 1
724                 vmonitor->set_debug(open_video_monitor_dialog->enable_debug());
725 #endif
726                 vmonitor->restore_settings_mask(open_video_monitor_dialog->xj_settings_mask());
727         } else {
728                 vmonitor->restore_settings_mask(xj_settings_mask);
729         }
730
731
732         if (!vmonitor->start()) {
733                 warning << "launching xjadeo failed.." << endmsg;
734                 close_video_monitor();
735         } else {
736                 GuiUpdate("set-xjadeo-active-on");
737                 vmonitor->set_fps(video_file_fps);
738                 vmonitor->open(video_filename);
739         }
740 }
741
742 void
743 VideoTimeLine::close_video_monitor() {
744         if (vmonitor && vmonitor->is_started()) {
745                 vmonitor->quit();
746         }
747 }
748
749 void
750 VideoTimeLine::terminated_video_monitor () {
751         if (vmonitor) {
752                 delete vmonitor;
753         }
754         GuiUpdate("set-xjadeo-active-off");
755         vmonitor=0;
756   if (reopen_vmonitor) {
757                 reopen_vmonitor=false;
758                 open_video_monitor(false);
759         }
760 }
761
762 /*
763 void
764 VideoTimeLine::clear_video_monitor_session_state ()
765 {
766         if (vmonitor) {
767                 vmonitor->clear_session_state();
768         } else {
769           if (!_session) { return; }
770                 XMLNode* node = new XMLNode(X_("XJSettings"));
771                 _session->add_extra_xml (*node);
772                 _session->set_dirty ();
773         }
774 }
775 */
776
777 void
778 VideoTimeLine::manual_seek_video_monitor (framepos_t pos)
779 {
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
784 }
785
786 #endif /* WITH_VIDEOTIMELINE */