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