2 Copyright (C) 2010 Paul Davis
3 Author: Robin Gareus <robin@gareus.org>
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.
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.
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.
20 #include "pbd/file_utils.h"
21 #include "pbd/convert.h"
22 #include "gui_thread.h"
27 #include "public_editor.h"
29 #include "video_monitor.h"
35 using namespace ARDOUR_UI_UTILS;
37 VideoMonitor::VideoMonitor (PublicEditor *ed, std::string xjadeo_bin_path)
40 manually_seeked_frame = 0;
41 fps =0.0; // = _session->timecode_frames_per_second();
42 sync_by_manual_seek = true;
43 _restore_settings_mask = 0;
44 clock_connection = sigc::connection();
45 state_connection = sigc::connection();
49 osdmode = 10; // 1: frameno, 2: timecode, 8: box
51 process = new ARDOUR::SystemExec(xjadeo_bin_path, X_("-R -J"));
52 process->ReadStdout.connect_same_thread (*this, boost::bind (&VideoMonitor::parse_output, this, _1 ,_2));
53 process->Terminated.connect (*this, invalidator (*this), boost::bind (&VideoMonitor::terminated, this), gui_context());
54 XJKeyEvent.connect (*this, invalidator (*this), boost::bind (&VideoMonitor::forward_keyevent, this, _1), gui_context());
57 VideoMonitor::~VideoMonitor ()
59 if (clock_connection.connected()) {
60 clock_connection.disconnect();
62 if (state_connection.connected()) {
63 state_connection.disconnect();
69 VideoMonitor::start ()
75 manually_seeked_frame = 0;
76 sync_by_manual_seek = false;
77 if (clock_connection.connected()) { clock_connection.disconnect(); }
79 if (process->start(debug_enable?2:1)) {
86 VideoMonitor::query_full_state (bool wait)
89 process->write_to_stdin("get windowsize\n");
90 process->write_to_stdin("get windowpos\n");
91 process->write_to_stdin("get letterbox\n");
92 process->write_to_stdin("get fullscreen\n");
93 process->write_to_stdin("get ontop\n");
94 process->write_to_stdin("get offset\n");
95 process->write_to_stdin("get osdcfg\n");
97 if (wait && knownstate !=127 && --timeout) {
104 VideoMonitor::quit ()
106 if (!is_started()) return;
107 if (state_connection.connected()) { state_connection.disconnect(); }
108 if (clock_connection.connected()) { clock_connection.disconnect(); }
109 query_full_state(true);
110 process->write_to_stdin("quit\n");
111 /* the 'quit' command should result in process termination
112 * but in case it fails (communication failure, SIGSTOP, ??)
116 while (is_started() && --timeout) {
121 process->terminate();
126 VideoMonitor::open (std::string filename)
128 if (!is_started()) return;
129 manually_seeked_frame = 0;
130 osdmode = 10; // 1: frameno, 2: timecode, 8: box
132 process->write_to_stdin("load " + filename + "\n");
133 process->write_to_stdin("set fps -1\n");
134 process->write_to_stdin("window resize 100%\n");
135 process->write_to_stdin("window ontop on\n");
136 process->write_to_stdin("set seekmode 1\n");
137 /* override bitwise flags -- see xjadeo.h
138 * 0x0001 : ignore 'q', ESC / quit
139 * 0x0002 : ignore "window closed by WM" / quit
140 * 0x0004 : (osx only) menu-exit / quit
141 * 0x0008 : ignore mouse-button 1 -- resize
142 * 0x0010 : no A/V offset control with keyboard
143 * 0x0020 : don't use jack-session
144 * 0x0040 : disable jack transport control
145 * 0x0080 : disallow sync source change (OSX menu)
146 * 0x0100 : disallow file open (OSX menu, X11 DnD)
148 process->write_to_stdin("set override 504\n");
149 process->write_to_stdin("notify keyboard\n");
150 process->write_to_stdin("notify settings\n");
151 process->write_to_stdin("window letterbox on\n");
152 process->write_to_stdin("osd mode 10\n");
153 for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
154 if (skip_setting(it->first)) { continue; }
155 process->write_to_stdin(it->first + " " + it->second + "\n");
157 if (!state_connection.connected()) {
160 state_clk_divide = 0;
161 /* TODO once every two second or so -- state_clk_divide hack below */
162 state_connection = Timers::rapid_connect (sigc::mem_fun (*this, &VideoMonitor::querystate));
164 sync_by_manual_seek = true;
165 clock_connection = Timers::fps_connect (sigc::mem_fun (*this, &VideoMonitor::srsupdate));
170 VideoMonitor::querystate ()
172 /* clock-divider hack -- RapidScreenUpdate == every_point_one_seconds */
173 state_clk_divide = (state_clk_divide + 1) % 300; // 30 secs
174 if (state_clk_divide == 0) {
176 query_full_state(false);
179 if (state_clk_divide%25 != 0) {
182 // every 2.5 seconds:
183 process->write_to_stdin("get fullscreen\n");
184 process->write_to_stdin("get ontop\n");
185 process->write_to_stdin("get osdcfg\n");
186 process->write_to_stdin("get letterbox\n");
190 VideoMonitor::skip_setting (std::string which)
192 if (_restore_settings_mask & XJ_OSD && which == "osd mode") { return true; }
193 if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
194 if (_restore_settings_mask & XJ_WINDOW_SIZE && which == "window size") { return true; }
195 if (_restore_settings_mask & XJ_WINDOW_POS && which == "window xy") { return true; }
196 if (_restore_settings_mask & XJ_WINDOW_ONTOP && which == "window ontop") { return true; }
197 if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
198 if (_restore_settings_mask & XJ_OFFSET && which == "set offset") { return true; }
199 if (_restore_settings_mask & XJ_FULLSCREEN && which == "window zoom") { return true; }
204 VideoMonitor::send_cmd (int what, int param)
206 bool osd_update = false;
207 int prev_osdmode = osdmode;
208 if (!is_started()) return;
211 if (param) process->write_to_stdin("window ontop on\n");
212 else process->write_to_stdin("window ontop off\n");
215 if (param) osdmode |= 2;
217 osd_update = (prev_osdmode != osdmode);
220 if (param) osdmode |= 1;
222 osd_update = (prev_osdmode != osdmode);
225 if (param) osdmode |= 8;
227 osd_update = (prev_osdmode != osdmode);
230 if (param) process->write_to_stdin("window zoom on\n");
231 else process->write_to_stdin("window zoom off\n");
234 if (param) process->write_to_stdin("window letterbox on\n");
235 else process->write_to_stdin("window letterbox off\n");
238 process->write_to_stdin("window resize 100%");
244 std::ostringstream osstream; osstream << "osd mode " << osdmode << "\n";
245 process->write_to_stdin(osstream.str());
250 VideoMonitor::is_started ()
252 return process->is_running();
256 VideoMonitor::forward_keyevent (unsigned int keyval)
258 emulate_key_event (keyval);
262 VideoMonitor::parse_output (std::string d, size_t /*s*/)
264 std::string line = d;
265 std::string::size_type start = 0;
266 std::string::size_type end = 0;
269 end = d.find('\n', start);
270 if (end == std::string::npos) break;
271 line = d.substr(start,end-start);
273 if (line.length() <4 || line.at(0)!='@') continue;
276 printf("xjadeo: '%s'\n", line.c_str());
279 int status = atoi(line.substr(1,3));
280 switch(status / 100) {
283 PBD::warning << _("Video Monitor: File Not Found.") << endmsg;
284 /* check: we should only write from the main thread.
285 * However it should not matter for 'quit'.
287 process->write_to_stdin("quit\n");
291 printf("xjadeo: error '%s'\n", line.c_str());
294 case 3: /* async notifications */
296 std::string::size_type equalsign = line.find('=');
297 std::string::size_type comment = line.find('#');
298 if (comment != std::string::npos) { line = line.substr(0,comment); }
299 if (equalsign != std::string::npos) {
300 std::string key = line.substr(5, equalsign - 5);
301 std::string value = line.substr(equalsign + 1);
303 if (status == 310 && key=="keypress") {
305 XJKeyEvent((unsigned int)atoi(value));
309 std::string msg = line.substr(5);
310 printf("xjadeo: async '%s' -> '%s'\n", key, value);
316 std::string msg = line.substr(5);
317 printf("xjadeo: async '%s'\n", msg.c_str());
321 case 1: /* text messages - command reply */
323 case 8: /* comments / info for humans */
329 * 210: var=<int>x<int>
331 * 228: var=<smpte-string>
334 std::string::size_type equalsign = line.find('=');
335 std::string::size_type comment = line.find('#');
336 if (comment != std::string::npos) { line = line.substr(0,comment); }
337 if (equalsign != std::string::npos) {
338 std::string key = line.substr(5, equalsign - 5);
339 std::string value = line.substr(equalsign + 1);
341 std::cout << "parsed: " << key << " => " << value << std::endl;
343 if (key == "windowpos") {
345 if (xjadeo_settings["window xy"] != value) {
346 if (!starting && _session) _session->set_dirty ();
348 xjadeo_settings["window xy"] = value;
349 } else if(key == "windowsize") {
351 if (xjadeo_settings["window size"] != value) {
352 if (!starting && _session) _session->set_dirty ();
354 xjadeo_settings["window size"] = value;
355 } else if(key == "windowontop") {
357 if (starting || xjadeo_settings["window ontop"] != value) {
358 if (!starting && _session) _session->set_dirty ();
359 if (atoi(value)) { UiState("xjadeo-window-ontop-on"); }
360 else { UiState("xjadeo-window-ontop-off"); }
363 xjadeo_settings["window ontop"] = value;
364 } else if(key == "fullscreen") {
366 if (starting || xjadeo_settings["window zoom"] != value) {
367 if (!starting && _session) _session->set_dirty ();
368 if (atoi(value)) { UiState("xjadeo-window-fullscreen-on"); }
369 else { UiState("xjadeo-window-fullscreen-off"); }
372 xjadeo_settings["window zoom"] = value;
373 } else if(key == "letterbox") {
375 if (starting || xjadeo_settings["window letterbox"] != value) {
376 if (!starting && _session) _session->set_dirty ();
377 if (atoi(value)) { UiState("xjadeo-window-letterbox-on"); }
378 else { UiState("xjadeo-window-letterbox-off"); }
381 xjadeo_settings["window letterbox"] = value;
382 } else if(key == "osdmode") {
384 osdmode = atoi(value);
385 if (starting || atoi(xjadeo_settings["osd mode"]) != osdmode) {
386 if (!starting && _session) _session->set_dirty ();
387 if ((osdmode & 1) == 1) { UiState("xjadeo-window-osd-frame-on"); }
388 if ((osdmode & 1) == 0) { UiState("xjadeo-window-osd-frame-off"); }
389 if ((osdmode & 2) == 2) { UiState("xjadeo-window-osd-timecode-on"); }
390 if ((osdmode & 2) == 0) { UiState("xjadeo-window-osd-timecode-off"); }
391 if ((osdmode & 8) == 8) { UiState("xjadeo-window-osd-box-on"); }
392 if ((osdmode & 8) == 0) { UiState("xjadeo-window-osd-box-off"); }
395 xjadeo_settings["osd mode"] = value;
396 } else if(key == "offset") {
398 if (xjadeo_settings["set offset"] != value) {
399 if (!starting && _session) _session->set_dirty ();
401 xjadeo_settings["set offset"] = value;
404 printf("xjadeo: '%s' -> '%s'\n", key.c_str(), value.c_str());
417 VideoMonitor::terminated ()
419 process->terminate(); // from gui-context clean up
424 VideoMonitor::save_session ()
426 if (!_session) { return; }
427 XMLNode* node = _session->extra_xml (X_("XJSettings"), true);
429 node->remove_nodes_and_delete("XJSetting");
431 for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
432 XMLNode* child = node->add_child (X_("XJSetting"));
433 child->set_property (X_("k"), it->first);
434 child->set_property (X_("v"), it->second);
440 VideoMonitor::set_session (ARDOUR::Session *s)
442 SessionHandlePtr::set_session (s);
443 if (!_session) { return; }
444 ARDOUR::Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoMonitor::parameter_changed, this, _1), gui_context());
445 _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoMonitor::parameter_changed, this, _1), gui_context());
446 XMLNode* node = _session->extra_xml (X_("XJSettings"));
447 if (!node) { return;}
448 xjadeo_settings.clear();
450 XMLNodeList nlist = node->children();
451 XMLNodeConstIterator niter;
452 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
453 xjadeo_settings[(*niter)->property(X_("k"))->value()] = (*niter)->property(X_("v"))->value();
458 VideoMonitor::set_custom_setting (const std::string k, const std::string v)
460 xjadeo_settings[k] = v;
461 return true; /* TODO: check if key is valid */
465 VideoMonitor::get_custom_setting (const std::string k)
467 return (xjadeo_settings[k]);
470 #define NO_OFFSET (Temporal::max_samplepos) //< skip setting or modifying offset
472 VideoMonitor::srsupdate ()
474 if (!_session) { return; }
475 if (editor->dragging_playhead()) { return ;}
476 manual_seek(_session->audible_sample(), false, NO_OFFSET);
480 VideoMonitor::set_offset (ARDOUR::sampleoffset_t offset)
482 if (!is_started()) { return; }
483 if (!_session) { return; }
484 if (offset == NO_OFFSET ) { return; }
486 samplecnt_t video_frame_offset;
487 samplecnt_t audio_sample_rate;
488 if (_session->config.get_videotimeline_pullup()) {
489 audio_sample_rate = _session->sample_rate();
491 audio_sample_rate = _session->nominal_sample_rate();
494 /* Note: pull-up/down are applied here: sample_rate() vs. nominal_sample_rate() */
495 if (_session->config.get_use_video_file_fps()) {
496 video_frame_offset = floor(offset * fps / audio_sample_rate);
498 video_frame_offset = floor(offset * _session->timecode_frames_per_second() / audio_sample_rate);
501 if (video_offset == video_frame_offset) { return; }
502 video_offset = video_frame_offset;
504 std::ostringstream osstream1; osstream1 << -1 * video_frame_offset;
505 process->write_to_stdin("set offset " + osstream1.str() + "\n");
509 VideoMonitor::manual_seek (samplepos_t when, bool /*force*/, ARDOUR::sampleoffset_t offset)
511 if (!is_started()) { return; }
512 if (!_session) { return; }
513 samplecnt_t video_frame;
514 samplecnt_t audio_sample_rate;
515 if (_session->config.get_videotimeline_pullup()) {
516 audio_sample_rate = _session->sample_rate();
518 audio_sample_rate = _session->nominal_sample_rate();
521 /* Note: pull-up/down are applied here: sample_rate() vs. nominal_sample_rate() */
522 if (_session->config.get_use_video_file_fps()) {
523 video_frame = floor(when * fps / audio_sample_rate);
525 video_frame = floor(when * _session->timecode_frames_per_second() / audio_sample_rate);
527 if (video_frame < 0 ) video_frame = 0;
529 if (video_frame == manually_seeked_frame) { return; }
530 manually_seeked_frame = video_frame;
533 std::cout <<"seek: " << video_frame << std::endl;
535 std::ostringstream osstream; osstream << video_frame;
536 process->write_to_stdin("seek " + osstream.str() + "\n");
542 VideoMonitor::parameter_changed (std::string const & p)
544 if (!is_started()) { return; }
545 if (!_session) { return; }
546 if (p != "external-sync" && p != "sync-source") {
553 VideoMonitor::xjadeo_sync_setup ()
555 if (!is_started()) { return; }
556 if (!_session) { return; }
558 bool my_manual_seek = true;
559 if (_session->synced_to_engine ()) {
560 my_manual_seek = false;
563 if (my_manual_seek != sync_by_manual_seek) {
564 if (sync_by_manual_seek) {
565 if (clock_connection.connected()) {
566 clock_connection.disconnect();
568 process->write_to_stdin("jack connect\n");
570 process->write_to_stdin("jack disconnect\n");
571 clock_connection = Timers::fps_connect (sigc::mem_fun (*this, &VideoMonitor::srsupdate));
573 sync_by_manual_seek = my_manual_seek;