fix video-monitor session settings
[ardour.git] / gtk2_ardour / video_monitor.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 "pbd/file_utils.h"
21 #include "pbd/convert.h"
22 #include "gui_thread.h"
23 #include "ardour_ui.h"
24
25 #include <stdio.h>
26 #include "public_editor.h"
27 #include "video_monitor.h"
28
29 #include "i18n.h"
30
31 using namespace std;
32 using namespace PBD;
33
34 VideoMonitor::VideoMonitor (PublicEditor *ed, std::string xjadeo_bin_path)
35         : editor (ed)
36 {
37         manually_seeked_frame = 0;
38         fps =0.0; // = _session->timecode_frames_per_second();
39         sync_by_manual_seek = false;
40         _restore_settings_mask = 0;
41         clock_connection = sigc::connection();
42         state_connection = sigc::connection();
43         debug_enable = false;
44         state_clk_divide = 0;
45         starting = 0;
46         osdmode = 10; // 1: frameno, 2: timecode, 8: box
47
48         process = new SystemExec(xjadeo_bin_path, X_("-R"));
49         process->ReadStdout.connect_same_thread (*this, boost::bind (&VideoMonitor::parse_output, this, _1 ,_2));
50         process->Terminated.connect (*this, invalidator (*this), boost::bind (&VideoMonitor::terminated, this), gui_context());
51 }
52
53 VideoMonitor::~VideoMonitor ()
54 {
55         if (clock_connection.connected()) {
56                 clock_connection.disconnect();
57         }
58         if (state_connection.connected()) {
59                 state_connection.disconnect();
60         }
61         delete process;
62 }
63
64 bool
65 VideoMonitor::start ()
66 {
67         if (is_started()) {
68                 return true;
69         }
70
71         manually_seeked_frame = 0;
72         sync_by_manual_seek = false;
73         if (clock_connection.connected()) { clock_connection.disconnect(); }
74
75         if (process->start(debug_enable?2:1)) {
76                 return false;
77         }
78         return true;
79 }
80
81 void
82 VideoMonitor::query_full_state (bool wait)
83 {
84         knownstate = 0;
85         process->write_to_stdin("get windowsize\n");
86         process->write_to_stdin("get windowpos\n");
87         process->write_to_stdin("get letterbox\n");
88         process->write_to_stdin("get fullscreen\n");
89         process->write_to_stdin("get ontop\n");
90         process->write_to_stdin("get offset\n");
91         process->write_to_stdin("get osdcfg\n");
92         int timeout = 40;
93         if (wait && knownstate !=127 && --timeout) {
94                 usleep(50000);
95                 sched_yield();
96         }
97 }
98
99 void
100 VideoMonitor::quit ()
101 {
102         if (!is_started()) return;
103         if (state_connection.connected()) { state_connection.disconnect(); }
104         if (clock_connection.connected()) { clock_connection.disconnect(); }
105         query_full_state(true);
106         process->write_to_stdin("quit\n");
107         /* the 'quit' command should result in process termination
108          * but in case it fails (communication failure, SIGSTOP, ??)
109          * here's a timeout..
110          */
111         int timeout = 40;
112         while (is_started() && --timeout) {
113                 usleep(50000);
114                 sched_yield();
115         }
116         if (timeout <= 0) {
117                 process->terminate();
118         }
119 }
120
121 void
122 VideoMonitor::open (std::string filename)
123 {
124         if (!is_started()) return;
125         manually_seeked_frame = 0;
126         osdmode = 10; // 1: frameno, 2: timecode, 8: box
127         sync_by_manual_seek = false;
128         starting = 15;
129         process->write_to_stdin("load " + filename + "\n");
130         process->write_to_stdin("set fps -1\n");
131         process->write_to_stdin("window resize 100%\n");
132         process->write_to_stdin("window ontop on\n");
133         process->write_to_stdin("set seekmode 1\n");
134         process->write_to_stdin("set override 47\n");
135         process->write_to_stdin("window letterbox on\n");
136         process->write_to_stdin("osd mode 10\n");
137         for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
138                 if (skip_setting(it->first)) { continue; }
139                 process->write_to_stdin(it->first + " " + it->second + "\n");
140         }
141         if (!state_connection.connected()) {
142                 starting = 15;
143                 querystate();
144                 state_clk_divide = 0;
145                 /* TODO once every two second or so -- state_clk_divide hack below */
146                 state_connection = ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &VideoMonitor::querystate));
147         }
148         xjadeo_sync_setup();
149 }
150
151 void
152 VideoMonitor::querystate ()
153 {
154         /* clock-divider hack -- RapidScreenUpdate == every_point_one_seconds */
155         state_clk_divide = (state_clk_divide + 1) % 300; // 30 secs
156         if (state_clk_divide == 0) {
157                 // every 30 seconds
158                 query_full_state(false);
159                 return;
160         }
161         if (state_clk_divide%25 != 0) {
162                 return;
163         }
164         // every 2.5 seconds:
165         process->write_to_stdin("get fullscreen\n");
166         process->write_to_stdin("get ontop\n");
167         process->write_to_stdin("get osdcfg\n");
168         process->write_to_stdin("get letterbox\n");
169 }
170
171 bool
172 VideoMonitor::skip_setting (std::string which)
173 {
174         if (_restore_settings_mask & XJ_OSD && which == "osd mode") { return true; }
175         if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
176         if (_restore_settings_mask & XJ_WINDOW_SIZE && which == "window size") { return true; }
177         if (_restore_settings_mask & XJ_WINDOW_POS && which == "window xy") { return true; }
178         if (_restore_settings_mask & XJ_WINDOW_ONTOP && which == "window ontop") { return true; }
179         if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
180         if (_restore_settings_mask & XJ_OFFSET && which == "set offset") { return true; }
181         if (_restore_settings_mask & XJ_FULLSCREEN && which == "window zoom") { return true; }
182         return false;
183 }
184
185 void
186 VideoMonitor::send_cmd (int what, int param)
187 {
188         bool osd_update = false;
189         if (!is_started()) return;
190         switch (what) {
191                 case 1:
192                         if (param) process->write_to_stdin("window ontop on\n");
193                         else process->write_to_stdin("window ontop off\n");
194                         break;
195                 case 2:
196                         if (param) osdmode |= 2;
197                         else osdmode &= ~2;
198                         osd_update = true;
199                         break;
200                 case 3:
201                         if (param) osdmode |= 1;
202                         else osdmode &= ~1;
203                         osd_update = true;
204                         break;
205                 case 4:
206                         if (param) osdmode |= 8;
207                         else osdmode &= ~8;
208                         osd_update = true;
209                         break;
210                 case 5:
211                         if (param) process->write_to_stdin("window zoom on\n");
212                         else process->write_to_stdin("window zoom off\n");
213                         break;
214                 case 6:
215                         if (param) process->write_to_stdin("window letterbox on\n");
216                         else process->write_to_stdin("window letterbox off\n");
217                         break;
218                 case 7:
219                         process->write_to_stdin("window resize 100%");
220                         break;
221                 default:
222                         break;
223         }
224         if (osd_update >= 0) {
225                 std::ostringstream osstream; osstream << "osd mode " << osdmode << "\n";
226                 process->write_to_stdin(osstream.str());
227         }
228 }
229
230 bool
231 VideoMonitor::is_started ()
232 {
233         return process->is_running();
234 }
235
236 void
237 VideoMonitor::parse_output (std::string d, size_t /*s*/)
238 {
239         std::string line = d;
240         std::string::size_type start = 0;
241         std::string::size_type end = 0;
242
243         while (1) {
244                 end = d.find('\n', start);
245                 if (end == std::string::npos) break;
246                 line = d.substr(start,end-start);
247                 start=end+1;
248                 if (line.length() <4 || line.at(0)!='@') continue;
249 #if 1 /* DEBUG */
250                 if (debug_enable) {
251                         printf("xjadeo: '%s'\n", line.c_str());
252                 }
253 #endif
254                 int status = atoi(line.substr(1,3));
255                 switch(status / 100) {
256                         case 4: /* errors */
257                                 if (status == 403) {
258                                         PBD::warning << _("Video Monitor: File Not Found.") << endmsg;
259                                         /* check: we should only write from the main thread.
260                                          * However it should not matter for 'quit'.
261                                          */
262                                         process->write_to_stdin("quit\n");
263                                 }
264                         case 1: /* requested async notifications */
265                         case 3: /* warnings ; command succeeded, but status is negative. */
266                                 break;
267                         case 2:
268 /* replies:
269  * 201: var=<int>
270  * 202: var=<double>
271  * 210: var=<int>x<int>
272  * 220: var=<string>
273  * 228: var=<smpte-string>
274  */
275                         {
276                                 std::string::size_type equalsign = line.find('=');
277                                 std::string::size_type comment = line.find('#');
278                                 if (comment != std::string::npos) { line = line.substr(0,comment); }
279                                 if (equalsign != std::string::npos) {
280                                         std::string key = line.substr(5, equalsign - 5);
281                                         std::string value = line.substr(equalsign + 1);
282 #if 0 /* DEBUG */
283                                         std::cout << "parsed: " << key << " => " << value << std::endl;
284 #endif
285                                         if       (key ==  "windowpos") {
286                                                 knownstate |= 16;
287                                                 if (xjadeo_settings["window xy"] != value) {
288                                                         if (!starting && _session) _session->set_dirty ();
289                                                 }
290                                                 xjadeo_settings["window xy"] = value;
291                                         } else if(key ==  "windowsize") {
292                                                 knownstate |= 32;
293                                                 if (xjadeo_settings["window size"] != value) {
294                                                         if (!starting && _session) _session->set_dirty ();
295                                                 }
296                                                 xjadeo_settings["window size"] = value;
297                                         } else if(key ==  "windowontop") {
298                                                 knownstate |= 2;
299                                                 if (starting || xjadeo_settings["window ontop"] != value) {
300                                                         if (!starting && _session) _session->set_dirty ();
301                                                         if (atoi(value)) { UiState("xjadeo-window-ontop-on"); }
302                                                         else { UiState("xjadeo-window-ontop-off"); }
303                                                         starting &= ~2;
304                                                 }
305                                                 xjadeo_settings["window ontop"] = value;
306                                         } else if(key ==  "fullscreen") {
307                                                 knownstate |= 4;
308                                                 if (starting || xjadeo_settings["window zoom"] != value) {
309                                                         if (!starting && _session) _session->set_dirty ();
310                                                         if (atoi(value)) { UiState("xjadeo-window-fullscreen-on"); }
311                                                         else { UiState("xjadeo-window-fullscreen-off"); }
312                                                         starting &= ~4;
313                                                 }
314                                                 xjadeo_settings["window zoom"] = value;
315                                         } else if(key ==  "letterbox") {
316                                                 knownstate |= 8;
317                                                 if (starting || xjadeo_settings["window letterbox"] != value) {
318                                                         if (!starting && _session) _session->set_dirty ();
319                                                         if (atoi(value)) { UiState("xjadeo-window-letterbox-on"); }
320                                                         else { UiState("xjadeo-window-letterbox-off"); }
321                                                         starting &= ~8;
322                                                 }
323                                                 xjadeo_settings["window letterbox"] = value;
324                                         } else if(key ==  "osdmode") {
325                                                 knownstate |= 1;
326                                                 osdmode = atoi(value);
327                                                 if (starting || atoi(xjadeo_settings["osd mode"]) != osdmode) {
328                                                         if (!starting && _session) _session->set_dirty ();
329                                                         if ((osdmode & 1) == 1) { UiState("xjadeo-window-osd-frame-on"); }
330                                                         if ((osdmode & 1) == 0) { UiState("xjadeo-window-osd-frame-off"); }
331                                                         if ((osdmode & 2) == 2) { UiState("xjadeo-window-osd-timecode-on"); }
332                                                         if ((osdmode & 2) == 0) { UiState("xjadeo-window-osd-timecode-off"); }
333                                                         if ((osdmode & 8) == 8) { UiState("xjadeo-window-osd-box-on"); }
334                                                         if ((osdmode & 8) == 0) { UiState("xjadeo-window-osd-box-off"); }
335                                                 }
336                                                 starting &= ~1;
337                                                 xjadeo_settings["osd mode"] = value;
338                                         } else if(key ==  "offset") {
339                                                 knownstate |= 64;
340                                                 if (xjadeo_settings["set offset"] != value) {
341                                                         if (!starting && _session) _session->set_dirty ();
342                                                 }
343                                                 xjadeo_settings["set offset"] = value;
344                                         }
345                                 }
346                         }
347                                 break;
348                         default:
349                                 break;
350                 }
351         }
352 }
353
354 void
355 VideoMonitor::terminated ()
356 {
357         process->terminate(); // from gui-context clean up
358         Terminated();
359 }
360
361 void
362 VideoMonitor::save_session ()
363 {
364         if (!_session) { return; }
365         XMLNode* node = _session->extra_xml (X_("XJSettings"));
366         if (!node) return;
367
368         for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
369           XMLNode* child = node->add_child (X_("XJSetting"));
370                 child->add_property (X_("k"), it->first);
371                 child->add_property (X_("v"), it->second);
372         }
373 }
374
375
376 void
377 VideoMonitor::set_session (ARDOUR::Session *s)
378 {
379         SessionHandlePtr::set_session (s);
380         if (!_session) { return; }
381         _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoMonitor::parameter_changed, this, _1), gui_context());
382         XMLNode* node = _session->extra_xml (X_("XJSettings"));
383         if (!node) { return;}
384         xjadeo_settings.clear();
385
386         XMLNodeList nlist = node->children();
387         XMLNodeConstIterator niter;
388         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
389                 xjadeo_settings[(*niter)->property(X_("k"))->value()] = (*niter)->property(X_("v"))->value();
390   }
391 }
392
393 void
394 VideoMonitor::clear_session_state ()
395 {
396         xjadeo_settings.clear();
397         if (!_session) { return; }
398         XMLNode* node = new XMLNode(X_("XJSettings"));
399         _session->add_extra_xml (*node);
400         _session->set_dirty ();
401 }
402
403 bool
404 VideoMonitor::set_custom_setting (const std::string k, const std::string v)
405 {
406         xjadeo_settings[k] = v;
407         return true; /* TODO: check if key is valid */
408 }
409
410 const std::string
411 VideoMonitor::get_custom_setting (const std::string k)
412 {
413         return (xjadeo_settings[k]);
414 }
415
416 #define NO_OFFSET (1<<31) //< skip setting or modifying offset --  TODO check ARDOUR::frameoffset_t max value.
417 void
418 VideoMonitor::srsupdate ()
419 {
420         if (!_session) { return; }
421         if (editor->dragging_playhead()) { return ;}
422         manual_seek(_session->audible_frame(), false, NO_OFFSET);
423 }
424
425 void
426 VideoMonitor::set_offset (ARDOUR::frameoffset_t offset)
427 {
428         if (!is_started()) { return; }
429         if (!_session) { return; }
430         if (offset == NO_OFFSET ) { return; }
431
432         framecnt_t video_frame_offset;
433         framecnt_t audio_frame_rate;
434         if (_session->config.get_videotimeline_pullup()) {
435                 audio_frame_rate = _session->frame_rate();
436         } else {
437                 audio_frame_rate = _session->nominal_frame_rate();
438         }
439
440         /* Note: pull-up/down are applied here: frame_rate() vs. nominal_frame_rate() */
441         if (_session->config.get_use_video_file_fps()) {
442                 video_frame_offset = floor(offset * fps / audio_frame_rate);
443         } else {
444                 video_frame_offset = floor(offset * _session->timecode_frames_per_second() / audio_frame_rate);
445         }
446
447         // TODO remember if changed..
448         std::ostringstream osstream1; osstream1 << -1 * video_frame_offset;
449         process->write_to_stdin("set offset " + osstream1.str() + "\n");
450 }
451
452 void
453 VideoMonitor::manual_seek (framepos_t when, bool /*force*/, ARDOUR::frameoffset_t offset)
454 {
455         if (!is_started()) { return; }
456         if (!_session) { return; }
457         framecnt_t video_frame;
458         framecnt_t audio_frame_rate;
459         if (_session->config.get_videotimeline_pullup()) {
460                 audio_frame_rate = _session->frame_rate();
461         } else {
462                 audio_frame_rate = _session->nominal_frame_rate();
463         }
464
465         /* Note: pull-up/down are applied here: frame_rate() vs. nominal_frame_rate() */
466         if (_session->config.get_use_video_file_fps()) {
467                 video_frame = floor(when * fps / audio_frame_rate);
468         } else {
469                 video_frame = floor(when * _session->timecode_frames_per_second() / audio_frame_rate);
470         }
471         if (video_frame < 0 ) video_frame = 0;
472
473         if (video_frame == manually_seeked_frame) { return; }
474         manually_seeked_frame = video_frame;
475
476 #if 0 /* DEBUG */
477         std::cout <<"seek: " << video_frame << std::endl;
478 #endif
479         std::ostringstream osstream; osstream << video_frame;
480         process->write_to_stdin("seek " + osstream.str() + "\n");
481
482         set_offset(offset);
483 }
484
485 void
486 VideoMonitor::parameter_changed (std::string const & p)
487 {
488         if (!is_started()) { return; }
489         if (!_session) { return; }
490         if (p != "external-sync" && p != "sync-source") {
491                 return;
492         }
493         xjadeo_sync_setup();
494 }
495
496 void
497 VideoMonitor::xjadeo_sync_setup ()
498 {
499         if (!is_started()) { return; }
500         if (!_session) { return; }
501
502         bool my_manual_seek = true;
503         if (_session->config.get_external_sync()) {
504                 if (ARDOUR::Config->get_sync_source() == ARDOUR::JACK)
505                         my_manual_seek = false;
506         }
507
508         if (my_manual_seek != sync_by_manual_seek) {
509                 if (sync_by_manual_seek) {
510                         if (clock_connection.connected()) {
511                                 clock_connection.disconnect();
512                         }
513                         process->write_to_stdin("jack connect\n");
514                 } else {
515                         process->write_to_stdin("jack disconnect\n");
516                         clock_connection = ARDOUR_UI::SuperRapidScreenUpdate.connect (sigc::mem_fun (*this, &VideoMonitor::srsupdate));
517                 }
518                 sync_by_manual_seek = my_manual_seek;
519         }
520 }