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