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