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 "gui_thread.h"
22 #include "ardour_ui.h"
25 #include "public_editor.h"
26 #include "video_monitor.h"
32 VideoMonitor::VideoMonitor (PublicEditor *ed, std::string xjadeo_bin_path)
35 manually_seeked_frame = 0;
36 fps =0.0; // = _session->timecode_frames_per_second();
37 sync_by_manual_seek = false;
38 _restore_settings_mask = 0;
39 clock_connection = sigc::connection();
40 state_connection = sigc::connection();
44 osdmode = 10; // 1: frameno, 2: timecode, 8: box
46 process = new SystemExec(xjadeo_bin_path, X_("-R"));
47 process->ReadStdout.connect_same_thread (*this, boost::bind (&VideoMonitor::parse_output, this, _1 ,_2));
48 process->Terminated.connect (*this, invalidator (*this), boost::bind (&VideoMonitor::terminated, this), gui_context());
51 VideoMonitor::~VideoMonitor ()
53 if (clock_connection.connected()) {
54 clock_connection.disconnect();
56 if (state_connection.connected()) {
57 state_connection.disconnect();
63 VideoMonitor::start ()
69 manually_seeked_frame = 0;
70 sync_by_manual_seek = false;
71 if (clock_connection.connected()) { clock_connection.disconnect(); }
73 if (process->start(debug_enable?2:1)) {
80 VideoMonitor::query_full_state (bool wait)
83 process->write_to_stdin("get windowsize\n");
84 process->write_to_stdin("get windowpos\n");
85 process->write_to_stdin("get letterbox\n");
86 process->write_to_stdin("get fullscreen\n");
87 process->write_to_stdin("get ontop\n");
88 process->write_to_stdin("get offset\n");
89 process->write_to_stdin("get osdcfg\n");
91 if (wait && knownstate !=127 && --timeout) {
100 if (!is_started()) return;
101 if (state_connection.connected()) { state_connection.disconnect(); }
102 if (clock_connection.connected()) { clock_connection.disconnect(); }
103 process->write_to_stdin("quit\n");
104 /* the 'quit' command should result in process termination
105 * but in case it fails (communication failure, SIGSTOP, ??)
109 while (is_started() && --timeout) {
114 process->terminate();
119 VideoMonitor::open (std::string filename)
121 if (!is_started()) return;
122 manually_seeked_frame = 0;
123 osdmode = 10; // 1: frameno, 2: timecode, 8: box
124 sync_by_manual_seek = false;
126 process->write_to_stdin("load " + filename + "\n");
127 process->write_to_stdin("set fps -1\n");
128 process->write_to_stdin("window resize 100%\n");
129 process->write_to_stdin("window ontop on\n");
130 process->write_to_stdin("set seekmode 1\n");
131 process->write_to_stdin("set override 47\n");
132 process->write_to_stdin("window letterbox on\n");
133 process->write_to_stdin("osd mode 10\n");
134 for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
135 if (skip_setting(it->first)) { continue; }
136 process->write_to_stdin(it->first + " " + it->second + "\n");
138 if (!state_connection.connected()) {
141 state_clk_divide = 0;
142 /* TODO once every two second or so -- state_clk_divide hack below */
143 state_connection = ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &VideoMonitor::querystate));
149 VideoMonitor::querystate ()
151 /* clock-divider hack -- RapidScreenUpdate == every_point_one_seconds */
152 state_clk_divide = (state_clk_divide + 1) % 300; // 30 secs
153 if (state_clk_divide == 0) {
155 query_full_state(false);
158 if (state_clk_divide%25 != 0) {
161 // every 2.5 seconds:
162 process->write_to_stdin("get fullscreen\n");
163 process->write_to_stdin("get ontop\n");
164 process->write_to_stdin("get osdcfg\n");
165 process->write_to_stdin("get letterbox\n");
169 VideoMonitor::skip_setting (std::string which)
171 if (_restore_settings_mask & XJ_OSD && which == "osd mode") { return true; }
172 if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
173 if (_restore_settings_mask & XJ_WINDOW_SIZE && which == "window size") { return true; }
174 if (_restore_settings_mask & XJ_WINDOW_POS && which == "window xy") { return true; }
175 if (_restore_settings_mask & XJ_WINDOW_ONTOP && which == "window ontop") { return true; }
176 if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
177 if (_restore_settings_mask & XJ_OFFSET && which == "set offset") { return true; }
178 if (_restore_settings_mask & XJ_FULLSCREEN && which == "window zoom") { return true; }
183 VideoMonitor::send_cmd (int what, int param)
185 bool osd_update = false;
186 if (!is_started()) return;
189 if (param) process->write_to_stdin("window ontop on\n");
190 else process->write_to_stdin("window ontop off\n");
193 if (param) osdmode |= 2;
198 if (param) osdmode |= 1;
203 if (param) osdmode |= 8;
208 if (param) process->write_to_stdin("window zoom on\n");
209 else process->write_to_stdin("window zoom off\n");
212 if (param) process->write_to_stdin("window letterbox on\n");
213 else process->write_to_stdin("window letterbox off\n");
216 process->write_to_stdin("window resize 100%");
221 if (osd_update >= 0) {
222 std::ostringstream osstream; osstream << "osd mode " << osdmode << "\n";
223 process->write_to_stdin(osstream.str());
228 VideoMonitor::is_started ()
230 return process->is_running();
234 VideoMonitor::parse_output (std::string d, size_t /*s*/)
236 std::string line = d;
237 std::string::size_type start = 0;
238 std::string::size_type end = 0;
241 end = d.find('\n', start);
242 if (end == std::string::npos) break;
243 line = d.substr(start,end-start);
245 if (line.length() <4 || line.at(0)!='@') continue;
248 printf("xjadeo: '%s'\n", line.c_str());
251 int status = atoi(line.substr(1,3).c_str());
252 switch(status / 100) {
255 PBD::warning << _("Video Monitor: File Not Found.") << endmsg;
256 /* check: we should only write from the main thread.
257 * However it should not matter for 'quit'.
259 process->write_to_stdin("quit\n");
261 case 1: /* requested async notifications */
262 case 3: /* warnings ; command succeeded, but status is negative. */
268 * 210: var=<int>x<int>
270 * 228: var=<smpte-string>
273 std::string::size_type equalsign = line.find('=');
274 std::string::size_type comment = line.find('#');
275 if (comment != std::string::npos) { line = line.substr(0,comment); }
276 if (equalsign != std::string::npos) {
277 std::string key = line.substr(5, equalsign - 5);
278 std::string value = line.substr(equalsign + 1);
280 std::cout << "parsed: " << key << " => " << value << std::endl;
282 if (key == "windowpos") {
284 if (xjadeo_settings["window xy"] != value) {
285 if (!starting && _session) _session->set_dirty ();
287 xjadeo_settings["window xy"] = value;
288 } else if(key == "windowsize") {
290 if (xjadeo_settings["window size"] != value) {
291 if (!starting && _session) _session->set_dirty ();
293 xjadeo_settings["window size"] = value;
294 } else if(key == "windowontop") {
296 if (starting || xjadeo_settings["window ontop"] != value) {
297 if (!starting && _session) _session->set_dirty ();
298 if (atoi(value.c_str())) { UiState("xjadeo-window-ontop-on"); }
299 else { UiState("xjadeo-window-ontop-off"); }
302 xjadeo_settings["window ontop"] = value;
303 } else if(key == "fullscreen") {
305 if (starting || xjadeo_settings["window zoom"] != value) {
306 if (!starting && _session) _session->set_dirty ();
307 if (atoi(value.c_str())) { UiState("xjadeo-window-fullscreen-on"); }
308 else { UiState("xjadeo-window-fullscreen-off"); }
311 xjadeo_settings["window zoom"] = value;
312 } else if(key == "letterbox") {
314 if (starting || xjadeo_settings["window letterbox"] != value) {
315 if (!starting && _session) _session->set_dirty ();
316 if (atoi(value.c_str())) { UiState("xjadeo-window-letterbox-on"); }
317 else { UiState("xjadeo-window-letterbox-off"); }
320 xjadeo_settings["window letterbox"] = value;
321 } else if(key == "osdmode") {
323 osdmode = atoi(value.c_str());
324 if (starting || atoi(xjadeo_settings["osd mode"].c_str()) != osdmode) {
325 if (!starting && _session) _session->set_dirty ();
326 if ((osdmode & 1) == 1) { UiState("xjadeo-window-osd-frame-on"); }
327 if ((osdmode & 1) == 0) { UiState("xjadeo-window-osd-frame-off"); }
328 if ((osdmode & 2) == 2) { UiState("xjadeo-window-osd-timecode-on"); }
329 if ((osdmode & 2) == 0) { UiState("xjadeo-window-osd-timecode-off"); }
330 if ((osdmode & 8) == 8) { UiState("xjadeo-window-osd-box-on"); }
331 if ((osdmode & 8) == 0) { UiState("xjadeo-window-osd-box-off"); }
334 xjadeo_settings["osd mode"] = value;
335 } else if(key == "offset") {
337 if (xjadeo_settings["set offset"] != value) {
338 if (!starting && _session) _session->set_dirty ();
340 xjadeo_settings["set offset"] = value;
352 VideoMonitor::terminated ()
354 process->terminate(); // from gui-context clean up
359 VideoMonitor::save_session ()
361 if (!_session) { return; }
362 XMLNode* node = _session->extra_xml (X_("XJSettings"));
365 for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
366 XMLNode* child = node->add_child (X_("XJSetting"));
367 child->add_property (X_("k"), it->first);
368 child->add_property (X_("v"), it->second);
374 VideoMonitor::set_session (ARDOUR::Session *s)
376 SessionHandlePtr::set_session (s);
377 if (!_session) { return; }
378 _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoMonitor::parameter_changed, this, _1), gui_context());
379 XMLNode* node = _session->extra_xml (X_("XJSettings"));
380 if (!node) { return;}
381 xjadeo_settings.clear();
383 XMLNodeList nlist = node->children();
384 XMLNodeConstIterator niter;
385 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
386 xjadeo_settings[(*niter)->property(X_("k"))->value()] = (*niter)->property(X_("v"))->value();
391 VideoMonitor::clear_session_state ()
393 xjadeo_settings.clear();
394 if (!_session) { return; }
395 XMLNode* node = new XMLNode(X_("XJSettings"));
396 _session->add_extra_xml (*node);
397 _session->set_dirty ();
401 VideoMonitor::set_custom_setting (const std::string k, const std::string v)
403 xjadeo_settings[k] = v;
404 return true; /* TODO: check if key is valid */
408 VideoMonitor::get_custom_setting (const std::string k)
410 return (xjadeo_settings[k]);
413 #define NO_OFFSET (1<<31) //< skip setting or modifying offset -- TODO check ARDOUR::frameoffset_t max value.
415 VideoMonitor::srsupdate ()
417 if (!_session) { return; }
418 if (editor->dragging_playhead()) { return ;}
419 manual_seek(_session->audible_frame(), false, NO_OFFSET);
423 VideoMonitor::set_offset (ARDOUR::frameoffset_t offset)
425 if (!is_started()) { return; }
426 if (!_session) { return; }
427 if (offset == NO_OFFSET ) { return; }
429 framecnt_t video_frame_offset;
430 framecnt_t audio_frame_rate;
431 if (_session->config.get_videotimeline_pullup()) {
432 audio_frame_rate = _session->frame_rate();
434 audio_frame_rate = _session->nominal_frame_rate();
437 /* Note: pull-up/down are applied here: frame_rate() vs. nominal_frame_rate() */
438 if (_session->config.get_use_video_file_fps()) {
439 video_frame_offset = floor(offset * fps / audio_frame_rate);
441 video_frame_offset = floor(offset * _session->timecode_frames_per_second() / audio_frame_rate);
444 // TODO remember if changed..
445 std::ostringstream osstream1; osstream1 << -1 * video_frame_offset;
446 process->write_to_stdin("set offset " + osstream1.str() + "\n");
450 VideoMonitor::manual_seek (framepos_t when, bool /*force*/, ARDOUR::frameoffset_t offset)
452 if (!is_started()) { return; }
453 if (!_session) { return; }
454 framecnt_t video_frame;
455 framecnt_t audio_frame_rate;
456 if (_session->config.get_videotimeline_pullup()) {
457 audio_frame_rate = _session->frame_rate();
459 audio_frame_rate = _session->nominal_frame_rate();
462 /* Note: pull-up/down are applied here: frame_rate() vs. nominal_frame_rate() */
463 if (_session->config.get_use_video_file_fps()) {
464 video_frame = floor(when * fps / audio_frame_rate);
466 video_frame = floor(when * _session->timecode_frames_per_second() / audio_frame_rate);
468 if (video_frame < 0 ) video_frame = 0;
470 if (video_frame == manually_seeked_frame) { return; }
471 manually_seeked_frame = video_frame;
474 std::cout <<"seek: " << video_frame << std::endl;
476 std::ostringstream osstream; osstream << video_frame;
477 process->write_to_stdin("seek " + osstream.str() + "\n");
483 VideoMonitor::parameter_changed (std::string const & p)
485 if (!is_started()) { return; }
486 if (!_session) { return; }
487 if (p != "external-sync" && p != "sync-source") {
494 VideoMonitor::xjadeo_sync_setup ()
496 if (!is_started()) { return; }
497 if (!_session) { return; }
499 bool my_manual_seek = true;
500 if (_session->config.get_external_sync()) {
501 if (ARDOUR::Config->get_sync_source() == ARDOUR::JACK)
502 my_manual_seek = false;
505 if (my_manual_seek != sync_by_manual_seek) {
506 if (sync_by_manual_seek) {
507 if (clock_connection.connected()) {
508 clock_connection.disconnect();
510 process->write_to_stdin("jack connect\n");
512 process->write_to_stdin("jack disconnect\n");
513 clock_connection = ARDOUR_UI::SuperRapidScreenUpdate.connect (sigc::mem_fun (*this, &VideoMonitor::srsupdate));
515 sync_by_manual_seek = my_manual_seek;