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