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