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"
23 #include "ardour_ui.h"
27 #include "public_editor.h"
29 #include "video_monitor.h"
36 VideoMonitor::VideoMonitor (PublicEditor *ed, std::string xjadeo_bin_path)
39 manually_seeked_frame = 0;
40 fps =0.0; // = _session->timecode_frames_per_second();
41 sync_by_manual_seek = false;
42 _restore_settings_mask = 0;
43 clock_connection = sigc::connection();
44 state_connection = sigc::connection();
48 osdmode = 10; // 1: frameno, 2: timecode, 8: box
50 process = new SystemExec(xjadeo_bin_path, X_("-R"));
51 process->ReadStdout.connect_same_thread (*this, boost::bind (&VideoMonitor::parse_output, this, _1 ,_2));
52 process->Terminated.connect (*this, invalidator (*this), boost::bind (&VideoMonitor::terminated, this), gui_context());
53 XJKeyEvent.connect (*this, invalidator (*this), boost::bind (&VideoMonitor::forward_keyevent, this, _1), gui_context());
56 VideoMonitor::~VideoMonitor ()
58 if (clock_connection.connected()) {
59 clock_connection.disconnect();
61 if (state_connection.connected()) {
62 state_connection.disconnect();
68 VideoMonitor::start ()
74 manually_seeked_frame = 0;
75 sync_by_manual_seek = false;
76 if (clock_connection.connected()) { clock_connection.disconnect(); }
78 if (process->start(debug_enable?2:1)) {
85 VideoMonitor::query_full_state (bool wait)
88 process->write_to_stdin("get windowsize\n");
89 process->write_to_stdin("get windowpos\n");
90 process->write_to_stdin("get letterbox\n");
91 process->write_to_stdin("get fullscreen\n");
92 process->write_to_stdin("get ontop\n");
93 process->write_to_stdin("get offset\n");
94 process->write_to_stdin("get osdcfg\n");
96 if (wait && knownstate !=127 && --timeout) {
103 VideoMonitor::quit ()
105 if (!is_started()) return;
106 if (state_connection.connected()) { state_connection.disconnect(); }
107 if (clock_connection.connected()) { clock_connection.disconnect(); }
108 query_full_state(true);
109 process->write_to_stdin("quit\n");
110 /* the 'quit' command should result in process termination
111 * but in case it fails (communication failure, SIGSTOP, ??)
115 while (is_started() && --timeout) {
120 process->terminate();
125 VideoMonitor::open (std::string filename)
127 if (!is_started()) return;
128 manually_seeked_frame = 0;
129 osdmode = 10; // 1: frameno, 2: timecode, 8: box
130 sync_by_manual_seek = false;
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 * 0x01 : ignore 'q', ESC / quite
139 * 0x02 : ignore "window closed by WM" / quit
140 * 0x04 : (osx only) menu-exit / quit
141 * 0x08 : ignore mouse-button 1 -- resize
142 * 0x10 : no A/V offset
143 * 0x20 : don't use jack-session
144 * 0x40 : no jack-transport control play/pause/rewind
146 process->write_to_stdin("set override 120\n");
147 process->write_to_stdin("notify keyboard\n");
148 process->write_to_stdin("notify settings\n");
149 process->write_to_stdin("window letterbox on\n");
150 process->write_to_stdin("osd mode 10\n");
151 for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
152 if (skip_setting(it->first)) { continue; }
153 process->write_to_stdin(it->first + " " + it->second + "\n");
155 if (!state_connection.connected()) {
158 state_clk_divide = 0;
159 /* TODO once every two second or so -- state_clk_divide hack below */
160 state_connection = ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &VideoMonitor::querystate));
166 VideoMonitor::querystate ()
168 /* clock-divider hack -- RapidScreenUpdate == every_point_one_seconds */
169 state_clk_divide = (state_clk_divide + 1) % 300; // 30 secs
170 if (state_clk_divide == 0) {
172 query_full_state(false);
175 if (state_clk_divide%25 != 0) {
178 // every 2.5 seconds:
179 process->write_to_stdin("get fullscreen\n");
180 process->write_to_stdin("get ontop\n");
181 process->write_to_stdin("get osdcfg\n");
182 process->write_to_stdin("get letterbox\n");
186 VideoMonitor::skip_setting (std::string which)
188 if (_restore_settings_mask & XJ_OSD && which == "osd mode") { return true; }
189 if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
190 if (_restore_settings_mask & XJ_WINDOW_SIZE && which == "window size") { return true; }
191 if (_restore_settings_mask & XJ_WINDOW_POS && which == "window xy") { return true; }
192 if (_restore_settings_mask & XJ_WINDOW_ONTOP && which == "window ontop") { return true; }
193 if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
194 if (_restore_settings_mask & XJ_OFFSET && which == "set offset") { return true; }
195 if (_restore_settings_mask & XJ_FULLSCREEN && which == "window zoom") { return true; }
200 VideoMonitor::send_cmd (int what, int param)
202 bool osd_update = false;
203 if (!is_started()) return;
206 if (param) process->write_to_stdin("window ontop on\n");
207 else process->write_to_stdin("window ontop off\n");
210 if (param) osdmode |= 2;
215 if (param) osdmode |= 1;
220 if (param) osdmode |= 8;
225 if (param) process->write_to_stdin("window zoom on\n");
226 else process->write_to_stdin("window zoom off\n");
229 if (param) process->write_to_stdin("window letterbox on\n");
230 else process->write_to_stdin("window letterbox off\n");
233 process->write_to_stdin("window resize 100%");
238 if (osd_update >= 0) {
239 std::ostringstream osstream; osstream << "osd mode " << osdmode << "\n";
240 process->write_to_stdin(osstream.str());
245 VideoMonitor::is_started ()
247 return process->is_running();
251 VideoMonitor::forward_keyevent (unsigned int keyval)
253 Editor* ed = dynamic_cast<Editor*>(&PublicEditor::instance());
256 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET(ed->gobj()));
257 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
258 GdkKeymapKey *keymapkey = NULL;
261 if (!gdk_keymap_get_entries_for_keyval(keymap, keyval, &keymapkey, &n_keys)) return;
262 if (n_keys !=1) { g_free(keymapkey); return;}
265 ev.type = GDK_KEY_PRESS;
266 ev.window = ed->get_window()->gobj();
267 ev.send_event = FALSE;
272 ev.string = (gchar*) "";
273 ev.hardware_keycode = keymapkey[0].keycode;
274 ev.group = keymapkey[0].group;
277 forward_key_press(&ev);
278 ev.type = GDK_KEY_RELEASE;
279 forward_key_press(&ev);
283 VideoMonitor::parse_output (std::string d, size_t /*s*/)
285 std::string line = d;
286 std::string::size_type start = 0;
287 std::string::size_type end = 0;
290 end = d.find('\n', start);
291 if (end == std::string::npos) break;
292 line = d.substr(start,end-start);
294 if (line.length() <4 || line.at(0)!='@') continue;
297 printf("xjadeo: '%s'\n", line.c_str());
300 int status = atoi(line.substr(1,3));
301 switch(status / 100) {
304 PBD::warning << _("Video Monitor: File Not Found.") << endmsg;
305 /* check: we should only write from the main thread.
306 * However it should not matter for 'quit'.
308 process->write_to_stdin("quit\n");
312 printf("xjadeo: error '%s'\n", line.c_str());
315 case 3: /* async notifications */
317 std::string::size_type equalsign = line.find('=');
318 std::string::size_type comment = line.find('#');
319 if (comment != std::string::npos) { line = line.substr(0,comment); }
320 if (equalsign != std::string::npos) {
321 std::string key = line.substr(5, equalsign - 5);
322 std::string value = line.substr(equalsign + 1);
324 if (status == 310 && key=="keypress") {
326 XJKeyEvent((unsigned int)atoi(value));
330 std::string msg = line.substr(5);
331 printf("xjadeo: async '%s' -> '%s'\n", key, value);
337 std::string msg = line.substr(5);
338 printf("xjadeo: async '%s'\n", msg.c_str());
342 case 1: /* text messages - command reply */
344 case 8: /* comments / info for humans */
350 * 210: var=<int>x<int>
352 * 228: var=<smpte-string>
355 std::string::size_type equalsign = line.find('=');
356 std::string::size_type comment = line.find('#');
357 if (comment != std::string::npos) { line = line.substr(0,comment); }
358 if (equalsign != std::string::npos) {
359 std::string key = line.substr(5, equalsign - 5);
360 std::string value = line.substr(equalsign + 1);
362 std::cout << "parsed: " << key << " => " << value << std::endl;
364 if (key == "windowpos") {
366 if (xjadeo_settings["window xy"] != value) {
367 if (!starting && _session) _session->set_dirty ();
369 xjadeo_settings["window xy"] = value;
370 } else if(key == "windowsize") {
372 if (xjadeo_settings["window size"] != value) {
373 if (!starting && _session) _session->set_dirty ();
375 xjadeo_settings["window size"] = value;
376 } else if(key == "windowontop") {
378 if (starting || xjadeo_settings["window ontop"] != value) {
379 if (!starting && _session) _session->set_dirty ();
380 if (atoi(value)) { UiState("xjadeo-window-ontop-on"); }
381 else { UiState("xjadeo-window-ontop-off"); }
384 xjadeo_settings["window ontop"] = value;
385 } else if(key == "fullscreen") {
387 if (starting || xjadeo_settings["window zoom"] != value) {
388 if (!starting && _session) _session->set_dirty ();
389 if (atoi(value)) { UiState("xjadeo-window-fullscreen-on"); }
390 else { UiState("xjadeo-window-fullscreen-off"); }
393 xjadeo_settings["window zoom"] = value;
394 } else if(key == "letterbox") {
396 if (starting || xjadeo_settings["window letterbox"] != value) {
397 if (!starting && _session) _session->set_dirty ();
398 if (atoi(value)) { UiState("xjadeo-window-letterbox-on"); }
399 else { UiState("xjadeo-window-letterbox-off"); }
402 xjadeo_settings["window letterbox"] = value;
403 } else if(key == "osdmode") {
405 osdmode = atoi(value);
406 if (starting || atoi(xjadeo_settings["osd mode"]) != osdmode) {
407 if (!starting && _session) _session->set_dirty ();
408 if ((osdmode & 1) == 1) { UiState("xjadeo-window-osd-frame-on"); }
409 if ((osdmode & 1) == 0) { UiState("xjadeo-window-osd-frame-off"); }
410 if ((osdmode & 2) == 2) { UiState("xjadeo-window-osd-timecode-on"); }
411 if ((osdmode & 2) == 0) { UiState("xjadeo-window-osd-timecode-off"); }
412 if ((osdmode & 8) == 8) { UiState("xjadeo-window-osd-box-on"); }
413 if ((osdmode & 8) == 0) { UiState("xjadeo-window-osd-box-off"); }
416 xjadeo_settings["osd mode"] = value;
417 } else if(key == "offset") {
419 if (xjadeo_settings["set offset"] != value) {
420 if (!starting && _session) _session->set_dirty ();
422 xjadeo_settings["set offset"] = value;
425 printf("xjadeo: '%s' -> '%s'\n", key.c_str(), value.c_str());
438 VideoMonitor::terminated ()
440 process->terminate(); // from gui-context clean up
445 VideoMonitor::save_session ()
447 if (!_session) { return; }
448 XMLNode* node = _session->extra_xml (X_("XJSettings"));
451 for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
452 XMLNode* child = node->add_child (X_("XJSetting"));
453 child->add_property (X_("k"), it->first);
454 child->add_property (X_("v"), it->second);
460 VideoMonitor::set_session (ARDOUR::Session *s)
462 SessionHandlePtr::set_session (s);
463 if (!_session) { return; }
464 _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoMonitor::parameter_changed, this, _1), gui_context());
465 XMLNode* node = _session->extra_xml (X_("XJSettings"));
466 if (!node) { return;}
467 xjadeo_settings.clear();
469 XMLNodeList nlist = node->children();
470 XMLNodeConstIterator niter;
471 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
472 xjadeo_settings[(*niter)->property(X_("k"))->value()] = (*niter)->property(X_("v"))->value();
477 VideoMonitor::clear_session_state ()
479 xjadeo_settings.clear();
480 if (!_session) { return; }
481 XMLNode* node = new XMLNode(X_("XJSettings"));
482 _session->add_extra_xml (*node);
483 _session->set_dirty ();
487 VideoMonitor::set_custom_setting (const std::string k, const std::string v)
489 xjadeo_settings[k] = v;
490 return true; /* TODO: check if key is valid */
494 VideoMonitor::get_custom_setting (const std::string k)
496 return (xjadeo_settings[k]);
499 #define NO_OFFSET (1<<31) //< skip setting or modifying offset -- TODO check ARDOUR::frameoffset_t max value.
501 VideoMonitor::srsupdate ()
503 if (!_session) { return; }
504 if (editor->dragging_playhead()) { return ;}
505 manual_seek(_session->audible_frame(), false, NO_OFFSET);
509 VideoMonitor::set_offset (ARDOUR::frameoffset_t offset)
511 if (!is_started()) { return; }
512 if (!_session) { return; }
513 if (offset == NO_OFFSET ) { return; }
515 framecnt_t video_frame_offset;
516 framecnt_t audio_frame_rate;
517 if (_session->config.get_videotimeline_pullup()) {
518 audio_frame_rate = _session->frame_rate();
520 audio_frame_rate = _session->nominal_frame_rate();
523 /* Note: pull-up/down are applied here: frame_rate() vs. nominal_frame_rate() */
524 if (_session->config.get_use_video_file_fps()) {
525 video_frame_offset = floor(offset * fps / audio_frame_rate);
527 video_frame_offset = floor(offset * _session->timecode_frames_per_second() / audio_frame_rate);
530 // TODO remember if changed..
531 std::ostringstream osstream1; osstream1 << -1 * video_frame_offset;
532 process->write_to_stdin("set offset " + osstream1.str() + "\n");
536 VideoMonitor::manual_seek (framepos_t when, bool /*force*/, ARDOUR::frameoffset_t offset)
538 if (!is_started()) { return; }
539 if (!_session) { return; }
540 framecnt_t video_frame;
541 framecnt_t audio_frame_rate;
542 if (_session->config.get_videotimeline_pullup()) {
543 audio_frame_rate = _session->frame_rate();
545 audio_frame_rate = _session->nominal_frame_rate();
548 /* Note: pull-up/down are applied here: frame_rate() vs. nominal_frame_rate() */
549 if (_session->config.get_use_video_file_fps()) {
550 video_frame = floor(when * fps / audio_frame_rate);
552 video_frame = floor(when * _session->timecode_frames_per_second() / audio_frame_rate);
554 if (video_frame < 0 ) video_frame = 0;
556 if (video_frame == manually_seeked_frame) { return; }
557 manually_seeked_frame = video_frame;
560 std::cout <<"seek: " << video_frame << std::endl;
562 std::ostringstream osstream; osstream << video_frame;
563 process->write_to_stdin("seek " + osstream.str() + "\n");
569 VideoMonitor::parameter_changed (std::string const & p)
571 if (!is_started()) { return; }
572 if (!_session) { return; }
573 if (p != "external-sync" && p != "sync-source") {
580 VideoMonitor::xjadeo_sync_setup ()
582 if (!is_started()) { return; }
583 if (!_session) { return; }
585 bool my_manual_seek = true;
586 if (_session->config.get_external_sync()) {
587 if (ARDOUR::Config->get_sync_source() == ARDOUR::JACK)
588 my_manual_seek = false;
591 if (my_manual_seek != sync_by_manual_seek) {
592 if (sync_by_manual_seek) {
593 if (clock_connection.connected()) {
594 clock_connection.disconnect();
596 process->write_to_stdin("jack connect\n");
598 process->write_to_stdin("jack disconnect\n");
599 clock_connection = ARDOUR_UI::SuperRapidScreenUpdate.connect (sigc::mem_fun (*this, &VideoMonitor::srsupdate));
601 sync_by_manual_seek = my_manual_seek;