Merge branch 'master' into windows
[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 "pbd/convert.h"
26 #include "ardour/session_directory.h"
27
28 #include "ardour_ui.h"
29 #include "public_editor.h"
30 #include "gui_thread.h"
31 #include "utils.h"
32 #include "canvas_impl.h"
33 #include "simpleline.h"
34 #include "utils_videotl.h"
35 #include "rgb_macros.h"
36 #include "video_timeline.h"
37
38 #include <gtkmm2ext/utils.h>
39 #include <pthread.h>
40 #include <curl/curl.h>
41
42 #include "i18n.h"
43
44 using namespace std;
45 using namespace ARDOUR;
46 using namespace PBD;
47 using namespace Timecode;
48 using namespace VideoUtils;
49
50 VideoTimeLine::VideoTimeLine (PublicEditor *ed, ArdourCanvas::Group *vbg, int initial_height)
51         : editor (ed)
52                 , videotl_bar_group(vbg)
53                 , bar_height(initial_height)
54 {
55         video_start_offset = 0L;
56         video_offset = 0L;
57         video_offset_p = 0L;
58         video_duration = 0L;
59         auto_set_session_fps = false;
60         video_offset_lock = false;
61         video_aspect_ratio = 4.0/3.0;
62         Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoTimeLine::parameter_changed, this, _1), gui_context());
63         video_server_url = video_get_server_url(Config);
64         server_docroot   = video_get_docroot(Config);
65         video_filename = "";
66         local_file = true;
67         video_file_fps = 25.0;
68         flush_frames = false;
69         vmonitor=0;
70         reopen_vmonitor=false;
71         find_xjadeo();
72
73         VtlUpdate.connect (*this, invalidator (*this), boost::bind (&PublicEditor::queue_visual_videotimeline_update, editor), gui_context());
74         GuiUpdate.connect (*this, invalidator (*this), boost::bind (&VideoTimeLine::gui_update, this, _1), gui_context());
75 }
76
77 VideoTimeLine::~VideoTimeLine ()
78 {
79         close_session();
80 }
81
82 /* close and save settings */
83 void
84 VideoTimeLine::save_session ()
85 {
86         if (!_session) {
87                 return;
88         }
89
90         LocaleGuard lg (X_("POSIX"));
91
92         XMLNode* node = new XMLNode(X_("Videomonitor"));
93         if (!node) return;
94         node->add_property (X_("active"), (vmonitor && vmonitor->is_started())?"yes":"no");
95         _session->add_extra_xml (*node);
96
97         if (vmonitor) {
98                 if (vmonitor->is_started()) {
99                         vmonitor->query_full_state(true);
100                 }
101                 vmonitor->save_session();
102         }
103
104         /* VTL settings */
105         node = _session->extra_xml (X_("Videotimeline"));
106         if (!node) return;
107         node->add_property (X_("id"), id().to_s());
108         node->add_property (X_("Height"), editor->get_videotl_bar_height());
109         node->add_property (X_("VideoOffsetLock"), video_offset_lock?X_("1"):X_("0"));
110         node->add_property (X_("VideoOffset"), video_offset);
111         node->add_property (X_("AutoFPS"), auto_set_session_fps?X_("1"):X_("0"));
112 }
113
114 /* close and save settings */
115 void
116 VideoTimeLine::close_session ()
117 {
118         if (video_duration == 0) {
119                 return;
120         }
121         sessionsave.disconnect();
122         close_video_monitor();
123
124         remove_frames();
125         video_filename = "";
126         video_duration = 0;
127         GuiUpdate("set-xjadeo-sensitive-off");
128         GuiUpdate("video-unavailable");
129 }
130
131 void
132 VideoTimeLine::sync_session_state ()
133 {
134         if (!_session || !vmonitor || !vmonitor->is_started()) {
135                 return;
136         }
137         save_session();
138 }
139
140 /** load settings from session */
141 void
142 VideoTimeLine::set_session (ARDOUR::Session *s)
143 {
144         SessionHandlePtr::set_session (s);
145         if (!_session) { return ; }
146
147         _session->SaveSession.connect_same_thread (sessionsave, boost::bind (&VideoTimeLine::save_session, this));
148         LocaleGuard lg (X_("POSIX"));
149
150         XMLNode* node = _session->extra_xml (X_("Videotimeline"));
151
152         if (!node || !node->property (X_("Filename"))) {
153                 return;
154         }
155
156         ARDOUR_UI::instance()->start_video_server((Gtk::Window*)0, false);
157
158         set_id(*node);
159
160         const XMLProperty* proph = node->property (X_("Height"));
161         if (proph) {
162                 editor->set_video_timeline_height(atoi(proph->value()));
163         }
164 #if 0 /* TODO THINK: set FPS first time only ?! */
165         const XMLProperty* propasfps = node->property (X_("AutoFPS"));
166         if (propasfps) {
167                 auto_set_session_fps = atoi(propasfps->value())?true:false;
168         }
169 #endif
170
171         const XMLProperty* propoffset = node->property (X_("VideoOffset"));
172         if (propoffset) {
173                 video_offset = atoll(propoffset->value());
174                 video_offset_p = video_offset;
175         }
176
177         const XMLProperty* proplock = node->property (X_("VideoOffsetLock"));
178         if (proplock) {
179                 video_offset_lock = atoi(proplock->value())?true:false;
180         }
181
182         const XMLProperty* localfile = node->property (X_("LocalFile"));
183         if (localfile) {
184                 local_file = atoi(localfile->value())?true:false;
185         }
186
187         const XMLProperty* propf = node->property (X_("Filename"));
188         video_file_info(propf->value(), local_file);
189
190         if ((node = _session->extra_xml (X_("Videomonitor")))) {
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());
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         leftmost_video_frame = floor (floor((leftmost_frame - video_start_offset - video_offset ) / vtl_dist) * vtl_dist / apv);
353
354         vtl_start = rint (video_offset + video_start_offset + leftmost_video_frame * apv);
355         visible_video_frames = 2 + ceil(editor->current_page_frames() / 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 < video_offset ) {
362                 visible_video_frames += ceil(vtl_start/vtl_dist);
363                 vtl_start = video_offset;
364         }
365
366         /* apply video-file constraints */
367         if (vtl_start > video_start_offset + video_duration + video_offset ) {
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 + video_offset) {
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 - video_offset) / apv); /* unit: video-frames */
406                 vfpos = (vframeno * apv ) + video_offset; /* audio-frame  corresponding to /rounded/ video-frame */
407
408                 int rightend = -1; /* unit: pixels */
409                 if (vfpos + vtl_dist > video_start_offset + video_duration + video_offset) {
410                         rightend = display_vframe_width * (video_start_offset + video_duration + video_offset - 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-leftmost_frame);
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); /* 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 - video_offset) / apv);  /* unit: video-frames */
431                         int rightend = -1; /* unit: pixels */
432                         if (vfpos + vtl_dist > video_start_offset + video_duration + video_offset) {
433                                 rightend = display_vframe_width * (video_start_offset + video_duration + video_offset - vfpos) / vtl_dist;
434                                 //printf("lf(n): %lu\n", vframeno); // XXX
435                         }
436                         frame->set_position(vfpos-leftmost_frame);
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                 video_duration = 0;
471                 GuiUpdate("set-xjadeo-sensitive-off");
472                 GuiUpdate("video-unavailable");
473                 return false;
474         }
475         video_duration = _duration * _session->nominal_frame_rate() / video_file_fps;
476         video_start_offset = _start_offset * _session->nominal_frame_rate();
477
478         if (auto_set_session_fps && video_file_fps != _session->timecode_frames_per_second()) {
479                 switch ((int)floorf(video_file_fps*1000.0)) {
480                         case 23976:
481                                 _session->config.set_timecode_format(timecode_23976);
482                                 break;
483                         case 24000:
484                                 _session->config.set_timecode_format(timecode_24);
485                                 break;
486                         case 24975:
487                         case 24976:
488                                 _session->config.set_timecode_format(timecode_24976);
489                                 break;
490                         case 25000:
491                                 _session->config.set_timecode_format(timecode_25);
492                                 break;
493                         case 29970:
494                                 _session->config.set_timecode_format(timecode_2997drop);
495                                 break;
496                         case 30000:
497                                 _session->config.set_timecode_format(timecode_30);
498                                 break;
499                         case 59940:
500                                 _session->config.set_timecode_format(timecode_5994);
501                                 break;
502                         case 60000:
503                                 _session->config.set_timecode_format(timecode_60);
504                                 break;
505                         default:
506                                 warning << string_compose (
507                                                 _("Failed to set session-framerate: '%1' does not have a corresponding option setting in %2."),
508                                                 video_file_fps, PROGRAM_NAME ) << endmsg;
509                                 break;
510                 }
511                 _session->config.set_video_pullup(0); /* TODO only set if set_timecode_format() was successful ?!*/
512         }
513         if (floor(video_file_fps*100) != floor(_session->timecode_frames_per_second()*100)) {
514                 warning << string_compose(
515                                 _("Video file's framerate is not equal to %1 session timecode's framerate: '%2' vs '%3'"),
516                                         PROGRAM_NAME, video_file_fps, _session->timecode_frames_per_second())
517                                 << endmsg;
518         }
519         flush_local_cache ();
520
521         if (found_xjadeo() && local_file) {
522                 GuiUpdate("set-xjadeo-sensitive-on");
523                 if (vmonitor && vmonitor->is_started()) {
524 #if 1
525                         /* xjadeo <= 0.6.4 has a bug where changing the video-file may segfauls
526                          * if the geometry changes to a different line-size alignment
527                          */
528                         reopen_vmonitor = true;
529                         vmonitor->quit();
530 #else
531                         vmonitor->set_fps(video_file_fps);
532                         vmonitor->open(video_filename);
533 #endif
534                 }
535         } else if (!local_file) {
536 #if 1 /* temp debug/devel message */
537                 // TODO - call xjremote remotely.
538                 printf("the given video file can not be accessed on localhost, video monitoring is not currently supported for this case\n");
539                 GuiUpdate("set-xjadeo-sensitive-off");
540 #endif
541         }
542         VtlUpdate();
543         GuiUpdate("video-available");
544         return true;
545 }
546
547 bool
548 VideoTimeLine::check_server ()
549 {
550         bool ok = false;
551         char url[1024];
552         snprintf(url, sizeof(url), "%s%sstatus"
553                         , video_server_url.c_str()
554                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
555                         );
556         char *res=a3_curl_http_get(url, NULL);
557         if (res) {
558                 if (strstr(res, "status: ok, online.")) { ok = true; }
559                 free(res);
560         }
561         return ok;
562 }
563
564 bool
565 VideoTimeLine::check_server_docroot ()
566 {
567         bool ok = true;
568         char url[1024];
569         std::vector<std::vector<std::string> > lines;
570
571         if (video_server_url.find("/localhost:") == string::npos) {
572                 return true;
573         }
574         snprintf(url, sizeof(url), "%s%src?format=csv"
575                         , video_server_url.c_str()
576                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
577                         );
578         char *res=a3_curl_http_get(url, NULL);
579         if (!res) {
580                 return false;
581         }
582
583         ParseCSV(std::string(res), lines);
584         if (   lines.empty()
585                         || lines.at(0).empty()
586                         || lines.at(0).at(0) != video_get_docroot(Config)) {
587                 warning << string_compose(
588                                 _("Video-server docroot mismatch. %1: '%2', video-server: '%3'. This usually means that the video server was not started by ardour and uses a different document-root."),
589                                 PROGRAM_NAME, video_get_docroot(Config), lines.at(0).at(0))
590                 << endmsg;
591                 ok = false; // TODO allow to override
592         }
593         free(res);
594         return ok;
595 }
596
597 void
598 VideoTimeLine::gui_update(std::string const & t) {
599         /* this is to be called via GuiUpdate() only. */
600         ENSURE_GUI_THREAD (*this, &VideoTimeLine::queue_visual_videotimeline_update)
601         if (t == "videotimeline-update") {
602                 editor->queue_visual_videotimeline_update();
603         } else if (t == "set-xjadeo-active-off") {
604                 editor->toggle_xjadeo_proc(0);
605         } else if (t == "set-xjadeo-active-on") {
606                 editor->toggle_xjadeo_proc(1);
607         } else if (t == "set-xjadeo-sensitive-on") {
608                 editor->set_xjadeo_sensitive(true);
609         } else if (t == "set-xjadeo-sensitive-off") {
610                 editor->toggle_xjadeo_proc(0);
611                 //close_video_monitor();
612                 editor->set_xjadeo_sensitive(false);
613         } else if (t == "xjadeo-window-ontop-on") {
614                 editor->toggle_xjadeo_viewoption(1, 1);
615         } else if (t == "xjadeo-window-ontop-off") {
616                 editor->toggle_xjadeo_viewoption(1, 0);
617         } else if (t == "xjadeo-window-osd-timecode-on") {
618                 editor->toggle_xjadeo_viewoption(2, 1);
619         } else if (t == "xjadeo-window-osd-timecode-off") {
620                 editor->toggle_xjadeo_viewoption(2, 0);
621         } else if (t == "xjadeo-window-osd-frame-on") {
622                 editor->toggle_xjadeo_viewoption(3, 1);
623         } else if (t == "xjadeo-window-osd-frame-off") {
624                 editor->toggle_xjadeo_viewoption(3, 0);
625         } else if (t == "xjadeo-window-osd-box-on") {
626                 editor->toggle_xjadeo_viewoption(4, 1);
627         } else if (t == "xjadeo-window-osd-box-off") {
628                 editor->toggle_xjadeo_viewoption(4, 0);
629         } else if (t == "xjadeo-window-fullscreen-on") {
630                 editor->toggle_xjadeo_viewoption(5, 1);
631         } else if (t == "xjadeo-window-fullscreen-off") {
632                 editor->toggle_xjadeo_viewoption(5, 0);
633         } else if (t == "xjadeo-window-letterbox-on") {
634                 editor->toggle_xjadeo_viewoption(6, 1);
635         } else if (t == "xjadeo-window-letterbox-off") {
636                 editor->toggle_xjadeo_viewoption(6, 0);
637         } else if (t == "video-available") {
638                 editor->set_close_video_sensitive(true);
639         } else if (t == "video-unavailable") {
640                 editor->set_close_video_sensitive(false);
641         }
642 }
643
644 void
645 VideoTimeLine::set_height (int height) {
646         if (_session && bar_height != height) {
647                 _session->set_dirty ();
648         }
649         bar_height = height;
650         flush_local_cache();
651 }
652
653 void
654 VideoTimeLine::vmon_update () {
655         if (vmonitor && vmonitor->is_started()) {
656                 vmonitor->set_offset(video_offset); // TODO proper re-init xjadeo w/o restart not just offset.
657         }
658 }
659
660 void
661 VideoTimeLine::flush_local_cache () {
662         flush_frames = true;
663         vmon_update();
664 }
665
666 void
667 VideoTimeLine::flush_cache () {
668         flush_local_cache();
669         char url[1024];
670         snprintf(url, sizeof(url), "%s%sadmin/flush_cache"
671                         , video_server_url.c_str()
672                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
673                         );
674         char *res=a3_curl_http_get(url, NULL);
675         if (res) {
676                 free (res);
677         }
678         if (vmonitor && vmonitor->is_started()) {
679                 reopen_vmonitor=true;
680                 vmonitor->quit();
681         }
682         video_file_info(video_filename, local_file);
683 }
684
685 /* config */
686 void
687 VideoTimeLine::parameter_changed (std::string const & p)
688 {
689         if (p == "video-server-url") {
690                 set_video_server_url (video_get_server_url(Config));
691         } else if (p == "video-server-docroot") {
692                 set_video_server_docroot (video_get_docroot(Config));
693         } else if (p == "video-advanced-setup") {
694                 set_video_server_url (video_get_server_url(Config));
695                 set_video_server_docroot (video_get_docroot(Config));
696         }
697         if (p == "use-video-file-fps" || p == "videotimeline-pullup" ) { /* session->config parameter */
698                 VtlUpdate();
699         }
700 }
701
702 void
703 VideoTimeLine::set_video_server_url(std::string vsu) {
704         flush_local_cache ();
705         video_server_url = vsu;
706         VtlUpdate();
707 }
708
709 void
710 VideoTimeLine::set_video_server_docroot(std::string vsr) {
711         flush_local_cache ();
712         server_docroot = vsr;
713         VtlUpdate();
714 }
715
716 /* video-monitor for this timeline */
717 void
718 VideoTimeLine::find_xjadeo () {
719         std::string xjadeo_file_path;
720         if (getenv("XJREMOTE")) {
721                 _xjadeo_bin = strdup(getenv("XJREMOTE")); // XXX TODO: free it?!
722         } else if (find_file_in_search_path (Searchpath(Glib::getenv("PATH")), X_("xjremote"), xjadeo_file_path)) {
723                 _xjadeo_bin = xjadeo_file_path;
724         }
725         else if (Glib::file_test(X_("/Applications/Jadeo.app/Contents/MacOS/xjremote"), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
726                 _xjadeo_bin = X_("/Applications/Jadeo.app/Contents/MacOS/xjremote");
727         }
728         /* TODO: win32: allow to configure PATH to xjremote */
729         else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjremote.exe"), Glib::FILE_TEST_EXISTS)) {
730                 _xjadeo_bin = X_("C:\\Program Files\\xjadeo\\xjremote.exe");
731         }
732         else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjremote.bat"), Glib::FILE_TEST_EXISTS)) {
733                 _xjadeo_bin = X_("C:\\Program Files\\xjadeo\\xjremote.bat");
734         }
735         else  {
736                 _xjadeo_bin = X_("");
737                 warning << _("Video-monitor 'xjadeo' was not found. Please install http://xjadeo.sf.net/ "
738                                 "(a custom path to xjadeo can be specified by setting the XJREMOTE environment variable. "
739                                 "It should point to an application compatible with xjadeo's remote-control interface 'xjremote').")
740                         << endmsg;
741         }
742 }
743
744 void
745 VideoTimeLine::open_video_monitor() {
746         if (!found_xjadeo()) return;
747         if (!vmonitor) {
748                 vmonitor = new VideoMonitor(editor, _xjadeo_bin);
749                 vmonitor->set_session(_session);
750                 vmonitor->set_offset(video_offset);
751                 vmonitor->Terminated.connect (sigc::mem_fun (*this, &VideoTimeLine::terminated_video_monitor));
752                 vmonitor->UiState.connect (*this, invalidator (*this), boost::bind (&VideoTimeLine::gui_update, this, _1), gui_context());
753         } else if (vmonitor->is_started()) {
754                 return;
755         }
756
757 #if 0
758         /* unused for now.
759          * the idea is to selective ignore certain monitor window
760          * states if xjadeo is not running on the same host as ardour.
761          * However with the removal of the video-monitor-startup-dialogue
762          * (git rev 5a4d0fff0) these settings are currently not accessible.
763          */
764         int xj_settings_mask = vmonitor->restore_settings_mask();
765         if (_session) {
766                 /* load mask from Session */
767                 XMLNode* node = _session->extra_xml (X_("XJRestoreSettings"));
768                 if (node) {
769                         const XMLProperty* prop = node->property (X_("mask"));
770                         if (prop) {
771                                 xj_settings_mask = atoi(prop->value());
772                         }
773                 }
774         }
775
776         vmonitor->restore_settings_mask(xj_settings_mask);
777 #endif
778
779         if (!vmonitor->start()) {
780                 warning << "launching xjadeo failed.." << endmsg;
781                 close_video_monitor();
782         } else {
783                 GuiUpdate("set-xjadeo-active-on");
784                 vmonitor->set_fps(video_file_fps);
785                 vmonitor->open(video_filename);
786
787                 if (_session) {
788                         XMLNode* node = _session->extra_xml (X_("Videomonitor"));
789                         if (node) {
790                                 const XMLProperty* prop = node->property (X_("active"));
791                                 if (prop && prop->value() != "yes") _session->set_dirty ();
792                         } else {
793                                 _session->set_dirty ();
794                         }
795                 }
796
797         }
798 }
799
800 void
801 VideoTimeLine::close_video_monitor() {
802         if (vmonitor && vmonitor->is_started()) {
803                 vmonitor->quit();
804         }
805 }
806
807 void
808 VideoTimeLine::control_video_monitor(int what, int param) {
809         if (!vmonitor || !vmonitor->is_started()) {
810                 return;
811         }
812         vmonitor->send_cmd(what, param);
813 }
814
815
816 void
817 VideoTimeLine::terminated_video_monitor () {
818         if (vmonitor) {
819                 vmonitor->save_session();
820                 delete vmonitor;
821         }
822         vmonitor=0;
823         GuiUpdate("set-xjadeo-active-off");
824         if (reopen_vmonitor) {
825                 reopen_vmonitor=false;
826                 open_video_monitor();
827         } else {
828                 if (_session) {
829                         _session->set_dirty ();
830                 }
831         }
832 }
833
834 void
835 VideoTimeLine::manual_seek_video_monitor (framepos_t pos)
836 {
837         if (!vmonitor) { return; }
838         if (!vmonitor->is_started()) { return; }
839         if (!vmonitor->synced_by_manual_seeks()) { return; }
840         vmonitor->manual_seek(pos, false, video_offset); // XXX -> set offset in xjadeo
841 }