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