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>
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.
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.
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.
23 #include "pbd/file_utils.h"
24 #include "pbd/convert.h"
25 #include "ardour/filesystem_paths.h"
27 #include "gui_thread.h"
30 #include "public_editor.h"
32 #include "video_monitor.h"
38 using namespace ARDOUR_UI_UTILS;
40 VideoMonitor::VideoMonitor (PublicEditor *ed, std::string xjadeo_bin_path)
43 manually_seeked_frame = 0;
44 fps =0.0; // = _session->timecode_frames_per_second();
45 sync_by_manual_seek = true;
46 _restore_settings_mask = 0;
47 clock_connection = sigc::connection();
48 state_connection = sigc::connection();
52 osdmode = 10; // 1: frameno, 2: timecode, 8: box
54 process = new ARDOUR::SystemExec(xjadeo_bin_path, X_("-R -J"));
55 process->ReadStdout.connect_same_thread (*this, boost::bind (&VideoMonitor::parse_output, this, _1 ,_2));
56 process->Terminated.connect (*this, invalidator (*this), boost::bind (&VideoMonitor::terminated, this), gui_context());
57 XJKeyEvent.connect (*this, invalidator (*this), boost::bind (&VideoMonitor::forward_keyevent, this, _1), gui_context());
60 VideoMonitor::~VideoMonitor ()
62 if (clock_connection.connected()) {
63 clock_connection.disconnect();
65 if (state_connection.connected()) {
66 state_connection.disconnect();
72 VideoMonitor::start ()
78 manually_seeked_frame = 0;
79 sync_by_manual_seek = false;
80 if (clock_connection.connected()) { clock_connection.disconnect(); }
82 if (process->start (debug_enable ? SystemExec::MergeWithStdin : SystemExec::IgnoreAndClose)) {
89 VideoMonitor::query_full_state (bool wait)
92 process->write_to_stdin("get windowsize\n");
93 process->write_to_stdin("get windowpos\n");
94 process->write_to_stdin("get letterbox\n");
95 process->write_to_stdin("get fullscreen\n");
96 process->write_to_stdin("get ontop\n");
97 process->write_to_stdin("get offset\n");
98 process->write_to_stdin("get osdcfg\n");
100 if (wait && knownstate !=127 && --timeout) {
107 VideoMonitor::quit ()
109 if (!is_started()) return;
110 if (state_connection.connected()) { state_connection.disconnect(); }
111 if (clock_connection.connected()) { clock_connection.disconnect(); }
112 query_full_state(true);
113 process->write_to_stdin("quit\n");
114 /* the 'quit' command should result in process termination
115 * but in case it fails (communication failure, SIGSTOP, ??)
119 while (is_started() && --timeout) {
124 process->terminate();
129 VideoMonitor::open (std::string filename)
131 if (!is_started()) return;
132 manually_seeked_frame = 0;
133 osdmode = 10; // 1: frameno, 2: timecode, 8: box
136 std::string ardour_mono_file;
137 if (find_file (ARDOUR::ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
138 process->write_to_stdin("osd font "+ ardour_mono_file +"\n");
141 process->write_to_stdin("load " + filename + "\n");
143 process->write_to_stdin("set fps -1\n");
144 process->write_to_stdin("window resize 100%\n");
145 process->write_to_stdin("window ontop on\n");
146 process->write_to_stdin("set seekmode 1\n");
147 /* override bitwise flags -- see xjadeo.h
148 * 0x0001 : ignore 'q', ESC / quit
149 * 0x0002 : ignore "window closed by WM" / quit
150 * 0x0004 : (osx only) menu-exit / quit
151 * 0x0008 : ignore mouse-button 1 -- resize
152 * 0x0010 : no A/V offset control with keyboard
153 * 0x0020 : don't use jack-session
154 * 0x0040 : disable jack transport control
155 * 0x0080 : disallow sync source change (OSX menu)
156 * 0x0100 : disallow file open (OSX menu, X11 DnD)
158 process->write_to_stdin("set override 504\n");
159 process->write_to_stdin("notify keyboard\n");
160 process->write_to_stdin("notify settings\n");
161 process->write_to_stdin("window letterbox on\n");
162 process->write_to_stdin("osd mode 10\n");
164 for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
165 if (skip_setting(it->first)) { continue; }
166 process->write_to_stdin(it->first + " " + it->second + "\n");
168 if (!state_connection.connected()) {
171 state_clk_divide = 0;
172 /* TODO once every two second or so -- state_clk_divide hack below */
173 state_connection = Timers::rapid_connect (sigc::mem_fun (*this, &VideoMonitor::querystate));
175 sync_by_manual_seek = true;
176 clock_connection = Timers::fps_connect (sigc::mem_fun (*this, &VideoMonitor::srsupdate));
181 VideoMonitor::querystate ()
183 /* clock-divider hack -- RapidScreenUpdate == every_point_one_seconds */
184 state_clk_divide = (state_clk_divide + 1) % 300; // 30 secs
185 if (state_clk_divide == 0) {
187 query_full_state(false);
190 if (state_clk_divide%25 != 0) {
193 // every 2.5 seconds:
194 process->write_to_stdin("get fullscreen\n");
195 process->write_to_stdin("get ontop\n");
196 process->write_to_stdin("get osdcfg\n");
197 process->write_to_stdin("get letterbox\n");
201 VideoMonitor::skip_setting (std::string which)
203 if (_restore_settings_mask & XJ_OSD && which == "osd mode") { return true; }
204 if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
205 if (_restore_settings_mask & XJ_WINDOW_SIZE && which == "window size") { return true; }
206 if (_restore_settings_mask & XJ_WINDOW_POS && which == "window xy") { return true; }
207 if (_restore_settings_mask & XJ_WINDOW_ONTOP && which == "window ontop") { return true; }
208 if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
209 if (_restore_settings_mask & XJ_OFFSET && which == "set offset") { return true; }
210 if (_restore_settings_mask & XJ_FULLSCREEN && which == "window zoom") { return true; }
215 VideoMonitor::send_cmd (int what, int param)
217 bool osd_update = false;
218 int prev_osdmode = osdmode;
219 if (!is_started()) return;
222 if (param) process->write_to_stdin("window ontop on\n");
223 else process->write_to_stdin("window ontop off\n");
226 if (param) osdmode |= 2;
228 osd_update = (prev_osdmode != osdmode);
231 if (param) osdmode |= 1;
233 osd_update = (prev_osdmode != osdmode);
236 if (param) osdmode |= 8;
238 osd_update = (prev_osdmode != osdmode);
241 if (param) process->write_to_stdin("window zoom on\n");
242 else process->write_to_stdin("window zoom off\n");
245 if (param) process->write_to_stdin("window letterbox on\n");
246 else process->write_to_stdin("window letterbox off\n");
249 process->write_to_stdin("window resize 100%");
255 std::ostringstream osstream; osstream << "osd mode " << osdmode << "\n";
256 process->write_to_stdin(osstream.str());
261 VideoMonitor::is_started ()
263 return process->is_running();
267 VideoMonitor::forward_keyevent (unsigned int keyval)
269 emulate_key_event (keyval);
273 VideoMonitor::parse_output (std::string d, size_t /*s*/)
275 std::string line = d;
276 std::string::size_type start = 0;
277 std::string::size_type end = 0;
280 end = d.find('\n', start);
281 if (end == std::string::npos) break;
282 line = d.substr(start,end-start);
284 if (line.length() <4 || line.at(0)!='@') continue;
287 printf("xjadeo: '%s'\n", line.c_str());
290 int status = atoi(line.substr(1,3));
291 switch(status / 100) {
294 PBD::warning << _("Video Monitor: File Not Found.") << endmsg;
295 /* check: we should only write from the main thread.
296 * However it should not matter for 'quit'.
298 process->write_to_stdin("quit\n");
302 printf("xjadeo: error '%s'\n", line.c_str());
305 case 3: /* async notifications */
307 std::string::size_type equalsign = line.find('=');
308 std::string::size_type comment = line.find('#');
309 if (comment != std::string::npos) { line = line.substr(0,comment); }
310 if (equalsign != std::string::npos) {
311 std::string key = line.substr(5, equalsign - 5);
312 std::string value = line.substr(equalsign + 1);
314 if (status == 310 && key=="keypress") {
316 XJKeyEvent((unsigned int)atoi(value));
320 std::string msg = line.substr(5);
321 printf("xjadeo: async '%s' -> '%s'\n", key, value);
327 std::string msg = line.substr(5);
328 printf("xjadeo: async '%s'\n", msg.c_str());
332 case 1: /* text messages - command reply */
334 case 8: /* comments / info for humans */
340 * 210: var=<int>x<int>
342 * 228: var=<smpte-string>
345 std::string::size_type equalsign = line.find('=');
346 std::string::size_type comment = line.find('#');
347 if (comment != std::string::npos) { line = line.substr(0,comment); }
348 if (equalsign != std::string::npos) {
349 std::string key = line.substr(5, equalsign - 5);
350 std::string value = line.substr(equalsign + 1);
352 std::cout << "parsed: " << key << " => " << value << std::endl;
354 if (key == "windowpos") {
356 if (xjadeo_settings["window xy"] != value) {
357 if (!starting && _session) _session->set_dirty ();
359 xjadeo_settings["window xy"] = value;
360 } else if(key == "windowsize") {
362 if (xjadeo_settings["window size"] != value) {
363 if (!starting && _session) _session->set_dirty ();
365 xjadeo_settings["window size"] = value;
366 } else if(key == "windowontop") {
368 if (starting || xjadeo_settings["window ontop"] != value) {
369 if (!starting && _session) _session->set_dirty ();
370 if (atoi(value)) { UiState("xjadeo-window-ontop-on"); }
371 else { UiState("xjadeo-window-ontop-off"); }
374 xjadeo_settings["window ontop"] = value;
375 } else if(key == "fullscreen") {
377 if (starting || xjadeo_settings["window zoom"] != value) {
378 if (!starting && _session) _session->set_dirty ();
379 if (atoi(value)) { UiState("xjadeo-window-fullscreen-on"); }
380 else { UiState("xjadeo-window-fullscreen-off"); }
383 xjadeo_settings["window zoom"] = value;
384 } else if(key == "letterbox") {
386 if (starting || xjadeo_settings["window letterbox"] != value) {
387 if (!starting && _session) _session->set_dirty ();
388 if (atoi(value)) { UiState("xjadeo-window-letterbox-on"); }
389 else { UiState("xjadeo-window-letterbox-off"); }
392 xjadeo_settings["window letterbox"] = value;
393 } else if(key == "osdmode") {
395 osdmode = atoi(value) & ~0x40;
396 if (starting || atoi(xjadeo_settings["osd mode"]) != osdmode) {
397 if (!starting && _session) _session->set_dirty ();
398 if ((osdmode & 1) == 1) { UiState("xjadeo-window-osd-frame-on"); }
399 if ((osdmode & 1) == 0) { UiState("xjadeo-window-osd-frame-off"); }
400 if ((osdmode & 2) == 2) { UiState("xjadeo-window-osd-timecode-on"); }
401 if ((osdmode & 2) == 0) { UiState("xjadeo-window-osd-timecode-off"); }
402 if ((osdmode & 8) == 8) { UiState("xjadeo-window-osd-box-on"); }
403 if ((osdmode & 8) == 0) { UiState("xjadeo-window-osd-box-off"); }
406 xjadeo_settings["osd mode"] = value;
407 } else if(key == "offset") {
409 if (xjadeo_settings["set offset"] != value) {
410 if (!starting && _session) _session->set_dirty ();
412 xjadeo_settings["set offset"] = value;
415 printf("xjadeo: '%s' -> '%s'\n", key.c_str(), value.c_str());
428 VideoMonitor::terminated ()
430 process->terminate(); // from gui-context clean up
435 VideoMonitor::save_session ()
437 if (!_session) { return; }
438 XMLNode* node = _session->extra_xml (X_("XJSettings"), true);
440 node->remove_nodes_and_delete("XJSetting");
442 for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
443 XMLNode* child = node->add_child (X_("XJSetting"));
444 child->set_property (X_("k"), it->first);
445 child->set_property (X_("v"), it->second);
451 VideoMonitor::set_session (ARDOUR::Session *s)
453 SessionHandlePtr::set_session (s);
454 if (!_session) { return; }
455 ARDOUR::Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoMonitor::parameter_changed, this, _1), gui_context());
456 _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoMonitor::parameter_changed, this, _1), gui_context());
457 XMLNode* node = _session->extra_xml (X_("XJSettings"));
458 if (!node) { return;}
459 xjadeo_settings.clear();
461 XMLNodeList nlist = node->children();
462 XMLNodeConstIterator niter;
463 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
464 xjadeo_settings[(*niter)->property(X_("k"))->value()] = (*niter)->property(X_("v"))->value();
469 VideoMonitor::set_custom_setting (const std::string k, const std::string v)
471 xjadeo_settings[k] = v;
472 return true; /* TODO: check if key is valid */
476 VideoMonitor::get_custom_setting (const std::string k)
478 return (xjadeo_settings[k]);
481 #define NO_OFFSET (Temporal::max_samplepos) //< skip setting or modifying offset
483 VideoMonitor::srsupdate ()
485 if (!_session) { return; }
486 if (editor->dragging_playhead() || editor->preview_video_drag_active()) {
489 manual_seek(_session->audible_sample(), false, NO_OFFSET);
493 VideoMonitor::set_offset (ARDOUR::sampleoffset_t offset)
495 if (!is_started()) { return; }
496 if (!_session) { return; }
497 if (offset == NO_OFFSET ) { return; }
499 samplecnt_t video_frame_offset;
500 samplecnt_t audio_sample_rate;
501 if (_session->config.get_videotimeline_pullup()) {
502 audio_sample_rate = _session->sample_rate();
504 audio_sample_rate = _session->nominal_sample_rate();
507 /* Note: pull-up/down are applied here: sample_rate() vs. nominal_sample_rate() */
508 if (_session->config.get_use_video_file_fps()) {
509 video_frame_offset = floor(offset * fps / audio_sample_rate);
511 video_frame_offset = floor(offset * _session->timecode_frames_per_second() / audio_sample_rate);
514 if (video_offset == video_frame_offset) { return; }
515 video_offset = video_frame_offset;
517 std::ostringstream osstream1; osstream1 << -1 * video_frame_offset;
518 process->write_to_stdin("set offset " + osstream1.str() + "\n");
522 VideoMonitor::manual_seek (samplepos_t when, bool /*force*/, ARDOUR::sampleoffset_t offset)
524 if (!is_started()) { return; }
525 if (!_session) { return; }
526 samplecnt_t video_frame;
527 samplecnt_t audio_sample_rate;
528 if (_session->config.get_videotimeline_pullup()) {
529 audio_sample_rate = _session->sample_rate();
531 audio_sample_rate = _session->nominal_sample_rate();
534 /* Note: pull-up/down are applied here: sample_rate() vs. nominal_sample_rate() */
535 if (_session->config.get_use_video_file_fps()) {
536 video_frame = floor(when * fps / audio_sample_rate);
538 video_frame = floor(when * _session->timecode_frames_per_second() / audio_sample_rate);
540 if (video_frame < 0 ) video_frame = 0;
542 if (video_frame == manually_seeked_frame) { return; }
543 manually_seeked_frame = video_frame;
546 std::cout <<"seek: " << video_frame << std::endl;
548 std::ostringstream osstream; osstream << video_frame;
549 process->write_to_stdin("seek " + osstream.str() + "\n");
555 VideoMonitor::parameter_changed (std::string const & p)
557 if (!is_started()) { return; }
558 if (!_session) { return; }
559 if (p != "external-sync" && p != "sync-source") {
566 VideoMonitor::xjadeo_sync_setup ()
568 if (!is_started()) { return; }
569 if (!_session) { return; }
571 bool my_manual_seek = true;
572 if (_session->synced_to_engine ()) {
573 my_manual_seek = false;
576 if (my_manual_seek != sync_by_manual_seek) {
577 if (sync_by_manual_seek) {
578 if (clock_connection.connected()) {
579 clock_connection.disconnect();
581 process->write_to_stdin("jack connect\n");
583 process->write_to_stdin("jack disconnect\n");
584 clock_connection = Timers::fps_connect (sigc::mem_fun (*this, &VideoMonitor::srsupdate));
586 sync_by_manual_seek = my_manual_seek;