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