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