Use XMLNode::get/set_property API in VideoTimeLine class
[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/types_convert.h"
26 #include "ardour/session_directory.h"
27
28 #include "ardour_ui.h"
29 #include "ardour_http.h"
30 #include "public_editor.h"
31 #include "gui_thread.h"
32 #include "utils_videotl.h"
33 #include "rgb_macros.h"
34 #include "video_timeline.h"
35 #include "video_tool_paths.h"
36
37 #include <gtkmm2ext/utils.h>
38 #include <pthread.h>
39 #include <curl/curl.h>
40
41 #include "pbd/i18n.h"
42
43 using namespace std;
44 using namespace ARDOUR;
45 using namespace PBD;
46 using namespace Timecode;
47 using namespace VideoUtils;
48
49 VideoTimeLine::VideoTimeLine (PublicEditor *ed, ArdourCanvas::Container *vbg, int initial_height)
50         : editor (ed)
51                 , videotl_group(vbg)
52                 , bar_height(initial_height)
53 {
54         video_start_offset = 0L;
55         video_offset = 0L;
56         video_offset_p = 0L;
57         video_duration = 0L;
58         auto_set_session_fps = false;
59         video_offset_lock = false;
60         video_aspect_ratio = 4.0/3.0;
61         Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoTimeLine::parameter_changed, this, _1), gui_context());
62         video_filename = "";
63         local_file = true;
64         video_file_fps = 25.0;
65         flush_frames = false;
66         vmonitor=0;
67         reopen_vmonitor=false;
68         find_xjadeo();
69         find_harvid();
70         video_server_url = video_get_server_url(Config);
71         server_docroot   = video_get_docroot(Config);
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;
91
92         XMLNode* node = new XMLNode(X_("Videomonitor"));
93         if (!node) return;
94         node->set_property (X_("active"), (vmonitor && vmonitor->is_started()));
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->set_property (X_("id"), id());
108         node->set_property (X_("Height"), editor->get_videotl_bar_height());
109         node->set_property (X_("VideoOffsetLock"), video_offset_lock);
110         node->set_property (X_("VideoOffset"), video_offset);
111         node->set_property (X_("AutoFPS"), auto_set_session_fps);
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->SessionSaveUnderway.connect_same_thread (sessionsave, boost::bind (&VideoTimeLine::save_session, this));
148         LocaleGuard lg;
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         int height;
161         if (node->get_property (X_("Height"), height)) {
162                 editor->set_video_timeline_height(height);
163         }
164 #if 0 /* TODO THINK: set FPS first time only ?! */
165         XMLProperty const * propasfps = node->property (X_("AutoFPS"));
166         if (propasfps) {
167                 auto_set_session_fps = atoi(propasfps->value())?true:false;
168         }
169 #endif
170
171         if (node->get_property (X_("VideoOffset"), video_offset)) {
172                 video_offset_p = video_offset;
173         }
174
175         node->get_property (X_("VideoOffsetLock"), video_offset_lock);
176         node->get_property (X_("LocalFile"), local_file);
177
178         std::string filename;
179         if (node->get_property (X_("Filename"), filename)) {
180                 video_file_info (filename, local_file);
181         }
182
183         if ((node = _session->extra_xml (X_("Videomonitor")))) {
184                 bool active;
185                 if (node->get_property (X_("active"), active) && active && found_xjadeo () &&
186                     !video_filename.empty () && local_file) {
187                         open_video_monitor();
188                 }
189         }
190
191         _session->register_with_memento_command_factory(id(), this);
192         _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoTimeLine::parameter_changed, this, _1), gui_context());
193 }
194
195 void
196 VideoTimeLine::set_offset_locked (bool v) {
197         if (_session && v != video_offset_lock) {
198                 _session->set_dirty ();
199         }
200         video_offset_lock = v;
201 }
202
203 void
204 VideoTimeLine::toggle_offset_locked () {
205         video_offset_lock = !video_offset_lock;
206         if (_session) {
207                 _session->set_dirty ();
208         }
209 }
210
211 void
212 VideoTimeLine::save_undo ()
213 {
214         if (_session && video_offset_p != video_offset) {
215                 _session->set_dirty ();
216         }
217         video_offset_p = video_offset;
218 }
219
220 int
221 VideoTimeLine::set_state (const XMLNode& node, int /*version*/)
222 {
223         LocaleGuard lg;
224         node.get_property (X_("VideoOffset"), video_offset);
225         ARDOUR_UI::instance()->flush_videotimeline_cache(true);
226         return 0;
227 }
228
229 XMLNode&
230 VideoTimeLine::get_state ()
231 {
232         XMLNode* node = new XMLNode (X_("Videotimeline"));
233         LocaleGuard lg;
234         node->set_property (X_("VideoOffset"), video_offset_p);
235         return *node;
236 }
237
238 void
239 VideoTimeLine::remove_frames ()
240 {
241         for (VideoFrames::iterator i = video_frames.begin(); i != video_frames.end(); ++i ) {
242                 VideoImageFrame *frame = (*i);
243                 delete frame;
244                 (*i) = 0;
245         }
246         video_frames.clear();
247 }
248
249 VideoImageFrame *
250 VideoTimeLine::get_video_frame (framepos_t vfn, int cut, int rightend)
251 {
252         if (vfn==0) cut=0;
253         for (VideoFrames::iterator i = video_frames.begin(); i != video_frames.end(); ++i) {
254                 VideoImageFrame *frame = (*i);
255                 if (abs(frame->get_video_frame_number()-vfn)<=cut
256                     && frame->get_rightend() == rightend) { return frame; }
257         }
258         return 0;
259 }
260
261 float
262 VideoTimeLine::get_apv()
263 {
264         // XXX: dup code - TODO use this fn in update_video_timeline()
265         float apv = -1; /* audio samples per video frame; */
266         if (!_session) return apv;
267
268         if (_session->config.get_use_video_file_fps()) {
269                 if (video_file_fps == 0 ) return apv;
270         } else {
271                 if (_session->timecode_frames_per_second() == 0 ) return apv;
272         }
273
274         if (_session->config.get_videotimeline_pullup()) {
275                 apv = _session->frame_rate();
276         } else {
277                 apv = _session->nominal_frame_rate();
278         }
279         if (_session->config.get_use_video_file_fps()) {
280                 apv /= video_file_fps;
281         } else {
282                 apv /= _session->timecode_frames_per_second();
283         }
284         return apv;
285 }
286
287 void
288 VideoTimeLine::update_video_timeline()
289 {
290         if (!_session) return;
291
292         if (_session->config.get_use_video_file_fps()) {
293                 if (video_file_fps == 0 ) return;
294         } else {
295                 if (_session->timecode_frames_per_second() == 0 ) return;
296         }
297
298         const double samples_per_pixel = editor->get_current_zoom();
299         const framepos_t leftmost_sample =  editor->leftmost_sample();
300
301         /* Outline:
302          * 1) calculate how many frames there should be in current zoom (plus 1 page on each side)
303          * 2) calculate first frame and distance between video-frames (according to zoom)
304          * 3) destroy/add frames
305          * 4) reposition existing frames
306          * 5) assign framenumber to frames -> request/decode video.
307          */
308
309         /* video-file and session properties */
310         double display_vframe_width; /* unit: pixels ; width of one thumbnail in the timeline */
311         float apv; /* audio samples per video frame; */
312         framepos_t leftmost_video_frame; /* unit: video-frame number ; temporary var -> vtl_start */
313
314         /* variables needed to render videotimeline -- what needs to computed first */
315         framepos_t vtl_start; /* unit: audio-samples ; first displayed video-frame */
316         framepos_t vtl_dist;  /* unit: audio-samples ; distance between displayed video-frames */
317         unsigned int visible_video_frames; /* number of frames that fit on current canvas */
318
319         if (_session->config.get_videotimeline_pullup()) {
320                 apv = _session->frame_rate();
321         } else {
322                 apv = _session->nominal_frame_rate();
323         }
324         if (_session->config.get_use_video_file_fps()) {
325                 apv /= video_file_fps;
326         } else {
327                 apv /= _session->timecode_frames_per_second();
328         }
329
330         display_vframe_width = bar_height * video_aspect_ratio;
331
332         if (apv > samples_per_pixel * display_vframe_width) {
333                 /* high-zoom: need space between successive video-frames */
334                 vtl_dist = rint(apv);
335         } else {
336                 /* continous timeline: skip video-frames */
337                 vtl_dist = ceil(display_vframe_width * samples_per_pixel / apv) * apv;
338         }
339
340         assert (vtl_dist > 0);
341         assert (apv > 0);
342
343         leftmost_video_frame = floor (floor((long double)(leftmost_sample - video_start_offset - video_offset ) / vtl_dist) * vtl_dist / apv);
344
345         vtl_start = rint (video_offset + video_start_offset + leftmost_video_frame * apv);
346         visible_video_frames = 2 + ceil((double)editor->current_page_samples() / vtl_dist); /* +2 left+right partial frames */
347
348         /* expand timeline (cache next/prev page images) */
349         vtl_start -= visible_video_frames * vtl_dist;
350         visible_video_frames *=3;
351
352         /* don't request frames that are too far to the right */
353         if (vtl_start < video_offset) {
354                 visible_video_frames = std::max((double)0.0, (double)visible_video_frames + ceil((double)(vtl_start - video_offset)/vtl_dist));
355                 vtl_start = video_offset;
356         }
357
358         /* apply video-file constraints
359          * (first frame in video is at video_start_offset) */
360         if (vtl_start > video_start_offset + video_duration + video_offset ) {
361                 visible_video_frames = 0;
362         }
363         /* trim end.
364          * end = position on timeline (video-offset)  minus  video-file's first frame position
365          * TODO optimize: compute rather than iterate */
366         while (visible_video_frames > 0 && vtl_start + (visible_video_frames-1) * vtl_dist >= video_start_offset + video_duration + video_offset) {
367                 --visible_video_frames;
368         }
369
370         if (flush_frames) {
371                 remove_frames();
372                 flush_frames=false;
373         }
374
375         while (video_frames.size() < visible_video_frames) {
376                 VideoImageFrame *frame;
377                 frame = new VideoImageFrame(*editor, *videotl_group, display_vframe_width, bar_height, video_server_url, translated_filename());
378                 frame->ImgChanged.connect (*this, invalidator (*this), boost::bind (&PublicEditor::queue_visual_videotimeline_update, editor), gui_context());
379                 video_frames.push_back(frame);
380         }
381
382         VideoFrames outdated_video_frames;
383         std::list<int> remaining;
384
385         outdated_video_frames = video_frames;
386
387 #if 1
388         /* when zoomed out, ignore shifts by +-1 frame
389          * which can occur due to rounding errors when
390          * scrolling to a new leftmost-audio frame.
391          */
392         int cut =1;
393         if (vtl_dist/apv < 3.0) cut =0;
394 #else
395         int cut =0;
396 #endif
397
398         for (unsigned int vfcount=0; vfcount < visible_video_frames; ++vfcount){
399                 framepos_t vfpos = vtl_start + vfcount * vtl_dist; /* unit: audio-frames */
400                 framepos_t vframeno = rint ( (vfpos - video_offset) / apv); /* unit: video-frames */
401                 vfpos = (vframeno * apv ) + video_offset; /* audio-frame  corresponding to /rounded/ video-frame */
402
403                 int rightend = -1; /* unit: pixels */
404                 if (vfpos + vtl_dist > video_start_offset + video_duration + video_offset) {
405                         rightend = display_vframe_width * (video_start_offset + video_duration + video_offset - vfpos) / vtl_dist;
406                         //printf("lf(e): %lu\n", vframeno); // XXX
407                 }
408                 VideoImageFrame * frame = get_video_frame(vframeno, cut, rightend);
409                 if (frame) {
410                   frame->set_position(vfpos);
411                         outdated_video_frames.remove(frame);
412                 } else {
413                         remaining.push_back(vfcount);
414                 }
415         }
416
417         for (VideoFrames::iterator i = outdated_video_frames.begin(); i != outdated_video_frames.end(); ++i ) {
418                 VideoImageFrame *frame = (*i);
419                 if (remaining.empty()) {
420                   frame->set_position(-2 * vtl_dist + leftmost_sample); /* move off screen */
421                 } else {
422                         int vfcount=remaining.front();
423                         remaining.pop_front();
424                         framepos_t vfpos = vtl_start + vfcount * vtl_dist; /* unit: audio-frames */
425                         framepos_t vframeno = rint ((vfpos - video_offset) / apv);  /* unit: video-frames */
426                         int rightend = -1; /* unit: pixels */
427                         if (vfpos + vtl_dist > video_start_offset + video_duration + video_offset) {
428                                 rightend = display_vframe_width * (video_start_offset + video_duration + video_offset - vfpos) / vtl_dist;
429                                 //printf("lf(n): %lu\n", vframeno); // XXX
430                         }
431                         frame->set_position(vfpos);
432                         frame->set_videoframe(vframeno, rightend);
433                 }
434         }
435 }
436
437 std::string
438 VideoTimeLine::translated_filename ()
439 {
440         if (!local_file){
441                 return video_filename;
442         } else {
443                 return video_map_path(server_docroot, video_filename);
444         }
445 }
446
447 bool
448 VideoTimeLine::video_file_info (std::string filename, bool local)
449 {
450
451         local_file = local;
452         if (Glib::path_is_absolute(filename) || !local_file)
453         {
454                 video_filename = filename;
455         }  else {
456                 video_filename = Glib::build_filename (_session->session_directory().video_path(), filename);
457         }
458
459         long long int _duration;
460         double _start_offset;
461
462         if (!video_query_info(
463                         video_server_url, translated_filename(),
464                         video_file_fps, _duration, _start_offset, video_aspect_ratio)) {
465                 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;
466                 video_duration = 0;
467                 GuiUpdate("set-xjadeo-sensitive-off");
468                 GuiUpdate("video-unavailable");
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 << string_compose (
503                                                 _("Failed to set session-framerate: '%1' does not have a corresponding option setting in %2."),
504                                                 video_file_fps, PROGRAM_NAME ) << endmsg;
505                                 break;
506                 }
507                 _session->config.set_video_pullup(0); /* TODO only set if set_timecode_format() was successful ?!*/
508         }
509         if (floor(video_file_fps*100) != floor(_session->timecode_frames_per_second()*100)) {
510                 warning << string_compose(
511                                 _("Video file's framerate is not equal to %1 session timecode's framerate: '%2' vs '%3'"),
512                                         PROGRAM_NAME, video_file_fps, _session->timecode_frames_per_second())
513                                 << endmsg;
514         }
515         flush_local_cache ();
516
517         if (found_xjadeo() && local_file) {
518                 GuiUpdate("set-xjadeo-sensitive-on");
519                 if (vmonitor && vmonitor->is_started()) {
520 #if 1
521                         /* xjadeo <= 0.6.4 has a bug where changing the video-file may segfauls
522                          * if the geometry changes to a different line-size alignment
523                          */
524                         reopen_vmonitor = true;
525                         vmonitor->quit();
526 #else
527                         vmonitor->set_fps(video_file_fps);
528                         vmonitor->open(video_filename);
529 #endif
530                 }
531         } else if (!local_file) {
532 #if 1 /* temp debug/devel message */
533                 // TODO - call xjremote remotely.
534                 printf("the given video file can not be accessed on localhost, video monitoring is not currently supported for this case\n");
535                 GuiUpdate("set-xjadeo-sensitive-off");
536 #endif
537         }
538         VtlUpdate();
539         GuiUpdate("video-available");
540         return true;
541 }
542
543 bool
544 VideoTimeLine::check_server ()
545 {
546         bool ok = false;
547         char url[1024];
548         snprintf(url, sizeof(url), "%s%sstatus"
549                         , video_server_url.c_str()
550                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
551                         );
552         char* res = ArdourCurl::http_get (url, NULL);
553         if (res) {
554                 if (strstr(res, "status: ok, online.")) { ok = true; }
555                 free(res);
556         }
557         return ok;
558 }
559
560 bool
561 VideoTimeLine::check_server_docroot ()
562 {
563         bool ok = true;
564         char url[1024];
565         std::vector<std::vector<std::string> > lines;
566
567         if (video_server_url.find("/localhost:") == string::npos) {
568                 return true;
569         }
570         snprintf(url, sizeof(url), "%s%src?format=csv"
571                         , video_server_url.c_str()
572                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
573                         );
574         char* res = ArdourCurl::http_get (url, NULL);
575         if (!res) {
576                 return false;
577         }
578
579         ParseCSV(std::string(res), lines);
580         if (   lines.empty()
581                         || lines.at(0).empty()
582                         || lines.at(0).at(0) != video_get_docroot(Config)) {
583                 warning << string_compose(
584                                 _("Video-server docroot mismatch. %1: '%2', video-server: '%3'. This usually means that the video server was not started by %1 and uses a different document-root."),
585                                 PROGRAM_NAME, video_get_docroot(Config), lines.at(0).at(0))
586                 << endmsg;
587                 ok = false; // TODO allow to override
588         }
589         free(res);
590         return ok;
591 }
592
593 void
594 VideoTimeLine::gui_update(std::string const & t) {
595         /* this is to be called via GuiUpdate() only. */
596         ENSURE_GUI_THREAD (*this, &VideoTimeLine::queue_visual_videotimeline_update)
597         if (t == "videotimeline-update") {
598                 editor->queue_visual_videotimeline_update();
599         } else if (t == "set-xjadeo-active-off") {
600                 editor->toggle_xjadeo_proc(0);
601         } else if (t == "set-xjadeo-active-on") {
602                 editor->toggle_xjadeo_proc(1);
603         } else if (t == "set-xjadeo-sensitive-on") {
604                 editor->set_xjadeo_sensitive(true);
605         } else if (t == "set-xjadeo-sensitive-off") {
606                 editor->toggle_xjadeo_proc(0);
607                 //close_video_monitor();
608                 editor->set_xjadeo_sensitive(false);
609         } else if (t == "xjadeo-window-ontop-on") {
610                 editor->toggle_xjadeo_viewoption(1, 1);
611         } else if (t == "xjadeo-window-ontop-off") {
612                 editor->toggle_xjadeo_viewoption(1, 0);
613         } else if (t == "xjadeo-window-osd-timecode-on") {
614                 editor->toggle_xjadeo_viewoption(2, 1);
615         } else if (t == "xjadeo-window-osd-timecode-off") {
616                 editor->toggle_xjadeo_viewoption(2, 0);
617         } else if (t == "xjadeo-window-osd-frame-on") {
618                 editor->toggle_xjadeo_viewoption(3, 1);
619         } else if (t == "xjadeo-window-osd-frame-off") {
620                 editor->toggle_xjadeo_viewoption(3, 0);
621         } else if (t == "xjadeo-window-osd-box-on") {
622                 editor->toggle_xjadeo_viewoption(4, 1);
623         } else if (t == "xjadeo-window-osd-box-off") {
624                 editor->toggle_xjadeo_viewoption(4, 0);
625         } else if (t == "xjadeo-window-fullscreen-on") {
626                 editor->toggle_xjadeo_viewoption(5, 1);
627         } else if (t == "xjadeo-window-fullscreen-off") {
628                 editor->toggle_xjadeo_viewoption(5, 0);
629         } else if (t == "xjadeo-window-letterbox-on") {
630                 editor->toggle_xjadeo_viewoption(6, 1);
631         } else if (t == "xjadeo-window-letterbox-off") {
632                 editor->toggle_xjadeo_viewoption(6, 0);
633         } else if (t == "video-available") {
634                 editor->set_close_video_sensitive(true);
635         } else if (t == "video-unavailable") {
636                 editor->set_close_video_sensitive(false);
637         }
638 }
639
640 void
641 VideoTimeLine::set_height (int height) {
642         if (_session && bar_height != height) {
643                 _session->set_dirty ();
644         }
645         bar_height = height;
646         flush_local_cache();
647 }
648
649 void
650 VideoTimeLine::vmon_update () {
651         if (vmonitor && vmonitor->is_started()) {
652                 vmonitor->set_offset(video_offset); // TODO proper re-init xjadeo w/o restart not just offset.
653         }
654 }
655
656 void
657 VideoTimeLine::flush_local_cache () {
658         flush_frames = true;
659         vmon_update();
660 }
661
662 void
663 VideoTimeLine::flush_cache () {
664         flush_local_cache();
665         char url[1024];
666         snprintf(url, sizeof(url), "%s%sadmin/flush_cache"
667                         , video_server_url.c_str()
668                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
669                         );
670         char* res = ArdourCurl::http_get (url, NULL);
671         if (res) {
672                 free (res);
673         }
674         if (vmonitor && vmonitor->is_started()) {
675                 reopen_vmonitor=true;
676                 vmonitor->quit();
677         }
678         video_file_info(video_filename, local_file);
679 }
680
681 /* config */
682 void
683 VideoTimeLine::parameter_changed (std::string const & p)
684 {
685         if (p == "video-server-url") {
686                 set_video_server_url (video_get_server_url(Config));
687         } else if (p == "video-server-docroot") {
688                 set_video_server_docroot (video_get_docroot(Config));
689         } else if (p == "video-advanced-setup") {
690                 set_video_server_url (video_get_server_url(Config));
691                 set_video_server_docroot (video_get_docroot(Config));
692         }
693         if (p == "use-video-file-fps" || p == "videotimeline-pullup" ) { /* session->config parameter */
694                 VtlUpdate();
695         }
696 }
697
698 void
699 VideoTimeLine::set_video_server_url(std::string vsu) {
700         flush_local_cache ();
701         video_server_url = vsu;
702         VtlUpdate();
703 }
704
705 void
706 VideoTimeLine::set_video_server_docroot(std::string vsr) {
707         flush_local_cache ();
708         server_docroot = vsr;
709         VtlUpdate();
710 }
711
712 /* video-monitor for this timeline */
713 void
714 VideoTimeLine::xjadeo_readversion (std::string d, size_t /* s */) {
715         xjadeo_version += d;
716 }
717
718 void
719 VideoTimeLine::find_xjadeo () {
720         if (!ArdourVideoToolPaths::xjadeo_exe(_xjadeo_bin)) {
721                 warning << _("Video-monitor 'xjadeo' was not found. Please install http://xjadeo.sf.net/ "
722                                 "(a custom path to xjadeo can be specified by setting the XJREMOTE environment variable. "
723                                 "It should point to an application compatible with xjadeo's remote-control interface 'xjremote').\n"
724                                 "\n"
725                                 "see also http://manual.ardour.org/video-timeline/setup/")
726                         << endmsg;
727         }
728
729         if (found_xjadeo ()) {
730                 ARDOUR::SystemExec version_check(_xjadeo_bin, X_("--version"));
731                 xjadeo_version = "";
732                 version_check.ReadStdout.connect_same_thread (*this, boost::bind (&VideoTimeLine::xjadeo_readversion, this, _1 ,_2));
733                 version_check.Terminated.connect_same_thread (*this, boost::bind (&VideoTimeLine::xjadeo_readversion, this, "\n" ,1));
734                 if (version_check.start(2)) {
735                         warning << _(
736                                         "Video-monitor 'xjadeo' cannot be launched."
737                                         ) << endmsg;
738                         _xjadeo_bin = X_("");
739                         return;
740                 }
741
742 #ifdef PLATFORM_WINDOWS
743                 version_check.wait (); // 40ms timeout
744 #else
745                 version_check.wait (WNOHANG);
746 #endif
747
748                 int timeout = 300;
749                 while (xjadeo_version.empty() && --timeout) {
750                         Glib::usleep(10000);
751                 }
752
753                 bool v_ok = false;
754                 size_t vo = xjadeo_version.find(" version ");
755                 if (vo != string::npos) {
756                         int v_major, v_minor, v_micro;
757                         if(sscanf(xjadeo_version.substr(vo + 9, string::npos).c_str(),"%d.%d.%d",
758                                                 &v_major, &v_minor, &v_micro) == 3)
759                         {
760                                 if (v_major >= 1) v_ok = true;
761                                 else if (v_major == 0 && v_minor >= 8) v_ok = true;
762                                 else if (v_major == 0 && v_minor >= 7 && v_micro >= 7) v_ok = true;
763                         }
764                 }
765                 if (!v_ok) {
766                         _xjadeo_bin = X_("");
767                         warning << _(
768                                         "Video-monitor 'xjadeo' is too old. "
769                                         "Please install xjadeo version 0.7.7 or later. http://xjadeo.sf.net/"
770                                         ) << endmsg;
771                 }
772         }
773 }
774
775 void
776 VideoTimeLine::harvid_readversion (std::string d, size_t /* s */) {
777         harvid_version += d;
778 }
779
780 void
781 VideoTimeLine::find_harvid () {
782         /* This is mainly for the benefit of the windows version:
783          * harvid >= 0.8.2 allows an empty docroot and ardour can
784          * pass the drive-letter along.
785          *
786          * It is a chicken/egg w.r.t. the video-server dialog
787          * but needed for default preferences and initial settings.
788          */
789         std::string harvid_bin;
790         if (VideoUtils::harvid_version != 0x0) {
791                 return;
792         }
793         if (!ArdourVideoToolPaths::harvid_exe(harvid_bin)) {
794                 return;
795         }
796         if (harvid_bin.empty ()) {
797                 return;
798         }
799         ARDOUR::SystemExec version_check(harvid_bin, X_("--version"));
800         harvid_version = "";
801         version_check.ReadStdout.connect_same_thread (*this, boost::bind (&VideoTimeLine::harvid_readversion, this, _1 ,_2));
802         version_check.Terminated.connect_same_thread (*this, boost::bind (&VideoTimeLine::harvid_readversion, this, "\n" ,1));
803         if (version_check.start(2)) {
804                 return;
805         }
806
807 #ifdef PLATFORM_WINDOWS
808         version_check.wait (); // 40ms timeout
809 #else
810         version_check.wait (WNOHANG);
811 #endif
812
813         int timeout = 300;
814         while (harvid_version.empty() && --timeout) {
815                 Glib::usleep(10000);
816         }
817
818         size_t vo = harvid_version.find("harvid v");
819         if (vo != string::npos) {
820                 int v_major, v_minor, v_micro;
821                 if(sscanf(harvid_version.substr(vo + 8, string::npos).c_str(),"%d.%d.%d",
822                                         &v_major, &v_minor, &v_micro) == 3)
823                 {
824                         VideoUtils::harvid_version = (v_major << 16) | (v_minor << 8) | v_micro;
825                         info << "harvid version: "<< hex << VideoUtils::harvid_version << endmsg;
826                 }
827         }
828 }
829
830 void
831 VideoTimeLine::open_video_monitor() {
832         if (!found_xjadeo()) return;
833         if (!vmonitor) {
834                 vmonitor = new VideoMonitor(editor, _xjadeo_bin);
835                 vmonitor->set_session(_session);
836                 vmonitor->set_offset(video_offset);
837                 vmonitor->Terminated.connect (sigc::mem_fun (*this, &VideoTimeLine::terminated_video_monitor));
838                 vmonitor->UiState.connect (*this, invalidator (*this), boost::bind (&VideoTimeLine::gui_update, this, _1), gui_context());
839         } else if (vmonitor->is_started()) {
840                 return;
841         }
842
843 #if 0
844         /* unused for now.
845          * the idea is to selective ignore certain monitor window
846          * states if xjadeo is not running on the same host as ardour.
847          * However with the removal of the video-monitor-startup-dialogue
848          * (git rev 5a4d0fff0) these settings are currently not accessible.
849          */
850         int xj_settings_mask = vmonitor->restore_settings_mask();
851         if (_session) {
852                 /* load mask from Session */
853                 XMLNode* node = _session->extra_xml (X_("XJRestoreSettings"));
854                 if (node) {
855                         XMLProperty const * prop = node->property (X_("mask"));
856                         if (prop) {
857                                 xj_settings_mask = atoi(prop->value());
858                         }
859                 }
860         }
861
862         vmonitor->restore_settings_mask(xj_settings_mask);
863 #endif
864
865         if (!vmonitor->start()) {
866                 warning << "launching xjadeo failed.." << endmsg;
867                 close_video_monitor();
868         } else {
869                 GuiUpdate("set-xjadeo-active-on");
870                 vmonitor->set_fps(video_file_fps);
871                 vmonitor->open(video_filename);
872
873                 if (_session) {
874                         XMLNode* node = _session->extra_xml (X_("Videomonitor"));
875                         if (node) {
876                                 bool active;
877                                 if (node->get_property (X_("active"), active) && !active) {
878                                         _session->set_dirty ();
879                                 }
880                         } else {
881                                 _session->set_dirty ();
882                         }
883                 }
884
885         }
886 }
887
888 void
889 VideoTimeLine::close_video_monitor() {
890         if (vmonitor && vmonitor->is_started()) {
891                 vmonitor->quit();
892         }
893 }
894
895 void
896 VideoTimeLine::control_video_monitor(int what, int param) {
897         if (!vmonitor || !vmonitor->is_started()) {
898                 return;
899         }
900         vmonitor->send_cmd(what, param);
901 }
902
903
904 void
905 VideoTimeLine::terminated_video_monitor () {
906         if (vmonitor) {
907                 vmonitor->save_session();
908                 delete vmonitor;
909         }
910         vmonitor=0;
911         GuiUpdate("set-xjadeo-active-off");
912         if (reopen_vmonitor) {
913                 reopen_vmonitor=false;
914                 open_video_monitor();
915         } else {
916                 if (_session) {
917                         _session->set_dirty ();
918                 }
919         }
920 }
921
922 void
923 VideoTimeLine::manual_seek_video_monitor (framepos_t pos)
924 {
925         if (!vmonitor) { return; }
926         if (!vmonitor->is_started()) { return; }
927         if (!vmonitor->synced_by_manual_seeks()) { return; }
928         vmonitor->manual_seek(pos, false, video_offset); // XXX -> set offset in xjadeo
929 }