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