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