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