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