Merge branch 'patches' of https://github.com/jdekozak/ardour
[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 #if 1
536                         /* xjadeo <= 0.6.4 has a bug where changing the video-file may segfauls
537                          * if the geometry changes to a different line-size alignment
538                          */
539                         reopen_vmonitor = true;
540                         vmonitor->quit();
541 #else
542                         vmonitor->set_fps(video_file_fps);
543                         vmonitor->open(video_filename);
544 #endif
545                 }
546         } else if (!local_file) {
547 #if 1 /* temp debug/devel message */
548                 // TODO - call xjremote remotely.
549                 printf("the given video file can not be accessed on localhost, video monitoring is not currently supported for this case\n");
550                 GuiUpdate("set-xjadeo-sensitive-off");
551 #endif
552         }
553         VtlUpdate();
554         return true;
555 }
556
557 bool
558 VideoTimeLine::check_server ()
559 {
560         bool ok = false;
561         char url[1024];
562         snprintf(url, sizeof(url), "%s%sstatus"
563                         , video_server_url.c_str()
564                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
565                         );
566         char *res=curl_http_get(url, NULL);
567         if (res) {
568                 if (strstr(res, "status: ok, online.")) { ok = true; }
569                 free(res);
570         }
571         return ok;
572 }
573
574 void
575 VideoTimeLine::gui_update(std::string const & t) {
576         /* this is to be called via GuiUpdate() only. */
577         ENSURE_GUI_THREAD (*this, &VideoTimeLine::queue_visual_videotimeline_update)
578         if (t == "videotimeline-update") {
579                 editor->queue_visual_videotimeline_update();
580         } else if (t == "set-xjadeo-active-off") {
581                 editor->toggle_xjadeo_proc(0);
582         } else if (t == "set-xjadeo-active-on") {
583                 editor->toggle_xjadeo_proc(1);
584         } else if (t == "set-xjadeo-sensitive-on") {
585                 editor->set_xjadeo_sensitive(true);
586         } else if (t == "set-xjadeo-sensitive-off") {
587                 editor->toggle_xjadeo_proc(0);
588                 //close_video_monitor();
589                 editor->set_xjadeo_sensitive(false);
590         }
591 }
592
593 void
594 VideoTimeLine::set_height (int height) {
595         bar_height = height;
596         flush_local_cache();
597 }
598
599 void
600 VideoTimeLine::vmon_update () {
601         if (vmonitor && vmonitor->is_started()) {
602                 vmonitor->set_offset( GOFFSET); // TODO proper re-init xjadeo w/o restart not just offset.
603         }
604 }
605
606 void
607 VideoTimeLine::flush_local_cache () {
608         flush_frames = true;
609         vmon_update();
610 }
611
612 void
613 VideoTimeLine::flush_cache () {
614         flush_local_cache();
615         char url[1024];
616         snprintf(url, sizeof(url), "%s%sadmin/flush_cache"
617                         , video_server_url.c_str()
618                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
619                         );
620         char *res=curl_http_get(url, NULL);
621         if (res) {
622                 free (res);
623         }
624         if (vmonitor && vmonitor->is_started()) {
625                 reopen_vmonitor=true;
626                 vmonitor->quit();
627         }
628         video_file_info(video_filename, local_file);
629 }
630
631 /* config */
632 void
633 VideoTimeLine::parameter_changed (std::string const & p)
634 {
635         if (p == "video-server-url") {
636                 set_video_server_url (video_get_server_url(Config));
637         } else if (p == "video-server-docroot") {
638                 set_video_server_docroot (video_get_docroot(Config));
639         } else if (p == "video-advanced-setup") {
640                 set_video_server_url (video_get_server_url(Config));
641                 set_video_server_docroot (video_get_docroot(Config));
642         }
643         if (p == "use-video-file-fps" || p == "videotimeline-pullup" ) { /* session->config parameter */
644                 VtlUpdate();
645         }
646 }
647
648 void
649 VideoTimeLine::set_video_server_url(std::string vsu) {
650         flush_local_cache ();
651         video_server_url = vsu;
652         VtlUpdate();
653 }
654
655 void
656 VideoTimeLine::set_video_server_docroot(std::string vsr) {
657         flush_local_cache ();
658         server_docroot = vsr;
659         VtlUpdate();
660 }
661
662 /* video-monitor for this timeline */
663 void
664 VideoTimeLine::find_xjadeo () {
665         std::string xjadeo_file_path;
666         if (getenv("XJREMOTE")) {
667                 _xjadeo_bin = strdup(getenv("XJREMOTE")); // XXX TODO: free it?!
668         } else if (find_file_in_search_path (SearchPath(Glib::getenv("PATH")), X_("xjremote"), xjadeo_file_path)) {
669                 _xjadeo_bin = xjadeo_file_path;
670         }
671         else if (Glib::file_test(X_("/Applications/Jadeo.app/Contents/MacOS/xjremote"), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
672                 _xjadeo_bin = X_("/Applications/Jadeo.app/Contents/MacOS/xjremote");
673         }
674         /* TODO: win32: allow to configure PATH to xjremote */
675         else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjremote.exe"), Glib::FILE_TEST_EXISTS)) {
676                 _xjadeo_bin = X_("C:\\Program Files\\xjadeo\\xjremote.exe");
677         }
678         else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjremote.bat"), Glib::FILE_TEST_EXISTS)) {
679                 _xjadeo_bin = X_("C:\\Program Files\\xjadeo\\xjremote.bat");
680         }
681         else  {
682                 _xjadeo_bin = X_("");
683                 warning << _("Video-monitor 'xjadeo' was not found. Please install http://xjadeo.sf.net/ "
684                                 "(a custom path to xjadeo can be specified by setting the XJREMOTE environment variable. "
685                                 "It should point to an application compatible with xjadeo's remote-control interface 'xjremote').")
686                         << endmsg;
687         }
688 }
689
690 void
691 VideoTimeLine::open_video_monitor(bool interactive) {
692         if (!found_xjadeo()) return;
693         if (!vmonitor) {
694                 vmonitor = new VideoMonitor(editor, _xjadeo_bin);
695                 vmonitor->set_session(_session);
696                 vmonitor->Terminated.connect (sigc::mem_fun (*this, &VideoTimeLine::terminated_video_monitor));
697         } else if (vmonitor->is_started()) {
698                 return;
699         }
700
701         int xj_settings_mask = vmonitor->restore_settings_mask();
702         if (_session) {
703                 /* load mask from Session */
704                 XMLNode* node = _session->extra_xml (X_("XJRestoreSettings"));
705                 if (node) {
706                         const XMLProperty* prop = node->property (X_("mask"));
707                         if (prop) {
708                                 xj_settings_mask = atoi(prop->value().c_str());
709                         }
710                 }
711         }
712
713         if (interactive && Config->get_video_monitor_setup_dialog()) {
714                 if (open_video_monitor_dialog == 0) {
715                         open_video_monitor_dialog = new OpenVideoMonitorDialog(_session);
716                 }
717                 if (open_video_monitor_dialog->is_visible()) {
718                         return;
719                 }
720                 open_video_monitor_dialog->setup_settings_mask(xj_settings_mask);
721                 open_video_monitor_dialog->set_filename(video_filename);
722                 Gtk::ResponseType r = (Gtk::ResponseType) open_video_monitor_dialog->run ();
723                 open_video_monitor_dialog->hide();
724                 if (r != Gtk::RESPONSE_ACCEPT) {
725                         GuiUpdate("set-xjadeo-active-off");
726                         return;
727                 }
728
729                 if (_session && (xj_settings_mask != open_video_monitor_dialog->xj_settings_mask()) ) {
730                         /* save mask to Session */
731                         XMLNode* node = new XMLNode(X_("XJRestoreSettings"));
732                         node->add_property (X_("mask"), (const long) open_video_monitor_dialog->xj_settings_mask() );
733                         _session->add_extra_xml (*node);
734                         _session->set_dirty ();
735                 }
736
737                 if (open_video_monitor_dialog->show_again()) {
738                         Config->set_video_monitor_setup_dialog(false);
739                 }
740 #if 1
741                 vmonitor->set_debug(open_video_monitor_dialog->enable_debug());
742 #endif
743                 vmonitor->restore_settings_mask(open_video_monitor_dialog->xj_settings_mask());
744         } else {
745                 vmonitor->restore_settings_mask(xj_settings_mask);
746         }
747
748
749         if (!vmonitor->start()) {
750                 warning << "launching xjadeo failed.." << endmsg;
751                 close_video_monitor();
752         } else {
753                 GuiUpdate("set-xjadeo-active-on");
754                 vmonitor->set_fps(video_file_fps);
755                 vmonitor->open(video_filename);
756         }
757 }
758
759 void
760 VideoTimeLine::close_video_monitor() {
761         if (vmonitor && vmonitor->is_started()) {
762                 vmonitor->quit();
763         }
764 }
765
766 void
767 VideoTimeLine::terminated_video_monitor () {
768         if (vmonitor) {
769                 delete vmonitor;
770         }
771         GuiUpdate("set-xjadeo-active-off");
772         vmonitor=0;
773   if (reopen_vmonitor) {
774                 reopen_vmonitor=false;
775                 open_video_monitor(false);
776         }
777 }
778
779 /*
780 void
781 VideoTimeLine::clear_video_monitor_session_state ()
782 {
783         if (vmonitor) {
784                 vmonitor->clear_session_state();
785         } else {
786           if (!_session) { return; }
787                 XMLNode* node = new XMLNode(X_("XJSettings"));
788                 _session->add_extra_xml (*node);
789                 _session->set_dirty ();
790         }
791 }
792 */
793
794 void
795 VideoTimeLine::manual_seek_video_monitor (framepos_t pos)
796 {
797         if (!vmonitor) { return; }
798         if (!vmonitor->is_started()) { return; }
799         if (!vmonitor->synced_by_manual_seeks()) { return; }
800         vmonitor->manual_seek(pos, false, GOFFSET); // XXX -> set offset in xjadeo
801 }
802
803 #endif /* WITH_VIDEOTIMELINE */