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