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