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