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