ardour
video_monitor.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Paul Davis
3  Author: Robin Gareus <robin@gareus.org>
4 
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.
9 
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.
14 
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.
18 
19 */
20 #include "pbd/file_utils.h"
21 #include "pbd/convert.h"
22 #include "gui_thread.h"
23 #include "timers.h"
24 #include "utils.h"
25 
26 #include <stdio.h>
27 #include "public_editor.h"
28 #include "editor.h"
29 #include "video_monitor.h"
30 
31 #include "i18n.h"
32 
33 using namespace std;
34 using namespace PBD;
35 using namespace ARDOUR_UI_UTILS;
36 
37 VideoMonitor::VideoMonitor (PublicEditor *ed, std::string xjadeo_bin_path)
38  : editor (ed)
39 {
41  fps =0.0; // = _session->timecode_frames_per_second();
42  sync_by_manual_seek = true;
44  clock_connection = sigc::connection();
45  state_connection = sigc::connection();
46  debug_enable = false;
47  state_clk_divide = 0;
48  starting = 0;
49  osdmode = 10; // 1: frameno, 2: timecode, 8: box
50 
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());
55 }
56 
58 {
59  if (clock_connection.connected()) {
60  clock_connection.disconnect();
61  }
62  if (state_connection.connected()) {
63  state_connection.disconnect();
64  }
65  delete process;
66 }
67 
68 bool
70 {
71  if (is_started()) {
72  return true;
73  }
74 
76  sync_by_manual_seek = false;
77  if (clock_connection.connected()) { clock_connection.disconnect(); }
78 
79  if (process->start(debug_enable?2:1)) {
80  return false;
81  }
82  return true;
83 }
84 
85 void
87 {
88  knownstate = 0;
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");
96  int timeout = 40;
97  if (wait && knownstate !=127 && --timeout) {
98  Glib::usleep(50000);
99  sched_yield();
100  }
101 }
102 
103 void
105 {
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, ??)
113  * here's a timeout..
114  */
115  int timeout = 40;
116  while (is_started() && --timeout) {
117  Glib::usleep(50000);
118  sched_yield();
119  }
120  if (timeout <= 0) {
121  process->terminate();
122  }
123 }
124 
125 void
126 VideoMonitor::open (std::string filename)
127 {
128  if (!is_started()) return;
130  osdmode = 10; // 1: frameno, 2: timecode, 8: box
131  starting = 15;
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)
147  */
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");
156  }
157  if (!state_connection.connected()) {
158  starting = 15;
159  querystate();
160  state_clk_divide = 0;
161  /* TODO once every two second or so -- state_clk_divide hack below */
163  }
164  sync_by_manual_seek = true;
167 }
168 
169 void
171 {
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) {
175  // every 30 seconds
176  query_full_state(false);
177  return;
178  }
179  if (state_clk_divide%25 != 0) {
180  return;
181  }
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");
187 }
188 
189 bool
190 VideoMonitor::skip_setting (std::string which)
191 {
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; }
200  return false;
201 }
202 
203 void
204 VideoMonitor::send_cmd (int what, int param)
205 {
206  bool osd_update = false;
207  int prev_osdmode = osdmode;
208  if (!is_started()) return;
209  switch (what) {
210  case 1:
211  if (param) process->write_to_stdin("window ontop on\n");
212  else process->write_to_stdin("window ontop off\n");
213  break;
214  case 2:
215  if (param) osdmode |= 2;
216  else osdmode &= ~2;
217  osd_update = (prev_osdmode != osdmode);
218  break;
219  case 3:
220  if (param) osdmode |= 1;
221  else osdmode &= ~1;
222  osd_update = (prev_osdmode != osdmode);
223  break;
224  case 4:
225  if (param) osdmode |= 8;
226  else osdmode &= ~8;
227  osd_update = (prev_osdmode != osdmode);
228  break;
229  case 5:
230  if (param) process->write_to_stdin("window zoom on\n");
231  else process->write_to_stdin("window zoom off\n");
232  break;
233  case 6:
234  if (param) process->write_to_stdin("window letterbox on\n");
235  else process->write_to_stdin("window letterbox off\n");
236  break;
237  case 7:
238  process->write_to_stdin("window resize 100%");
239  break;
240  default:
241  break;
242  }
243  if (osd_update) {
244  std::ostringstream osstream; osstream << "osd mode " << osdmode << "\n";
245  process->write_to_stdin(osstream.str());
246  }
247 }
248 
249 bool
251 {
252  return process->is_running();
253 }
254 
255 void
256 VideoMonitor::forward_keyevent (unsigned int keyval)
257 {
258  Editor* ed = dynamic_cast<Editor*>(&PublicEditor::instance());
259  if (!ed) return;
260  emulate_key_event(ed, keyval);
261 }
262 
263 void
264 VideoMonitor::parse_output (std::string d, size_t /*s*/)
265 {
266  std::string line = d;
267  std::string::size_type start = 0;
268  std::string::size_type end = 0;
269 
270  while (1) {
271  end = d.find('\n', start);
272  if (end == std::string::npos) break;
273  line = d.substr(start,end-start);
274  start=end+1;
275  if (line.length() <4 || line.at(0)!='@') continue;
276 #if 1 /* DEBUG */
277  if (debug_enable) {
278  printf("xjadeo: '%s'\n", line.c_str());
279  }
280 #endif
281  int status = atoi(line.substr(1,3));
282  switch(status / 100) {
283  case 4: /* errors */
284  if (status == 403) {
285  PBD::warning << _("Video Monitor: File Not Found.") << endmsg;
286  /* check: we should only write from the main thread.
287  * However it should not matter for 'quit'.
288  */
289  process->write_to_stdin("quit\n");
290  }
291 #ifdef DEBUG_XJCOM
292  else
293  printf("xjadeo: error '%s'\n", line.c_str());
294 #endif
295  break;
296  case 3: /* async notifications */
297  {
298  std::string::size_type equalsign = line.find('=');
299  std::string::size_type comment = line.find('#');
300  if (comment != std::string::npos) { line = line.substr(0,comment); }
301  if (equalsign != std::string::npos) {
302  std::string key = line.substr(5, equalsign - 5);
303  std::string value = line.substr(equalsign + 1);
304 
305  if (status == 310 && key=="keypress") {
306  /* keyboard event */
307  XJKeyEvent((unsigned int)atoi(value));
308  }
309 #ifdef DEBUG_XJCOM
310  else {
311  std::string msg = line.substr(5);
312  printf("xjadeo: async '%s' -> '%s'\n", key, value);
313  }
314 #endif
315  }
316 #ifdef DEBUG_XJCOM
317  else {
318  std::string msg = line.substr(5);
319  printf("xjadeo: async '%s'\n", msg.c_str());
320  }
321 #endif
322  } break;
323  case 1: /* text messages - command reply */
324  break;
325  case 8: /* comments / info for humans */
326  break;
327  case 2:
328 /* replies:
329  * 201: var=<int>
330  * 202: var=<double>
331  * 210: var=<int>x<int>
332  * 220: var=<string>
333  * 228: var=<smpte-string>
334  */
335  {
336  std::string::size_type equalsign = line.find('=');
337  std::string::size_type comment = line.find('#');
338  if (comment != std::string::npos) { line = line.substr(0,comment); }
339  if (equalsign != std::string::npos) {
340  std::string key = line.substr(5, equalsign - 5);
341  std::string value = line.substr(equalsign + 1);
342 #if 0 /* DEBUG */
343  std::cout << "parsed: " << key << " => " << value << std::endl;
344 #endif
345  if (key == "windowpos") {
346  knownstate |= 16;
347  if (xjadeo_settings["window xy"] != value) {
348  if (!starting && _session) _session->set_dirty ();
349  }
350  xjadeo_settings["window xy"] = value;
351  } else if(key == "windowsize") {
352  knownstate |= 32;
353  if (xjadeo_settings["window size"] != value) {
354  if (!starting && _session) _session->set_dirty ();
355  }
356  xjadeo_settings["window size"] = value;
357  } else if(key == "windowontop") {
358  knownstate |= 2;
359  if (starting || xjadeo_settings["window ontop"] != value) {
360  if (!starting && _session) _session->set_dirty ();
361  if (atoi(value)) { UiState("xjadeo-window-ontop-on"); }
362  else { UiState("xjadeo-window-ontop-off"); }
363  starting &= ~2;
364  }
365  xjadeo_settings["window ontop"] = value;
366  } else if(key == "fullscreen") {
367  knownstate |= 4;
368  if (starting || xjadeo_settings["window zoom"] != value) {
369  if (!starting && _session) _session->set_dirty ();
370  if (atoi(value)) { UiState("xjadeo-window-fullscreen-on"); }
371  else { UiState("xjadeo-window-fullscreen-off"); }
372  starting &= ~4;
373  }
374  xjadeo_settings["window zoom"] = value;
375  } else if(key == "letterbox") {
376  knownstate |= 8;
377  if (starting || xjadeo_settings["window letterbox"] != value) {
378  if (!starting && _session) _session->set_dirty ();
379  if (atoi(value)) { UiState("xjadeo-window-letterbox-on"); }
380  else { UiState("xjadeo-window-letterbox-off"); }
381  starting &= ~8;
382  }
383  xjadeo_settings["window letterbox"] = value;
384  } else if(key == "osdmode") {
385  knownstate |= 1;
386  osdmode = atoi(value);
387  if (starting || atoi(xjadeo_settings["osd mode"]) != osdmode) {
388  if (!starting && _session) _session->set_dirty ();
389  if ((osdmode & 1) == 1) { UiState("xjadeo-window-osd-frame-on"); }
390  if ((osdmode & 1) == 0) { UiState("xjadeo-window-osd-frame-off"); }
391  if ((osdmode & 2) == 2) { UiState("xjadeo-window-osd-timecode-on"); }
392  if ((osdmode & 2) == 0) { UiState("xjadeo-window-osd-timecode-off"); }
393  if ((osdmode & 8) == 8) { UiState("xjadeo-window-osd-box-on"); }
394  if ((osdmode & 8) == 0) { UiState("xjadeo-window-osd-box-off"); }
395  }
396  starting &= ~1;
397  xjadeo_settings["osd mode"] = value;
398  } else if(key == "offset") {
399  knownstate |= 64;
400  if (xjadeo_settings["set offset"] != value) {
401  if (!starting && _session) _session->set_dirty ();
402  }
403  xjadeo_settings["set offset"] = value;
404 #ifdef DEBUG_XJCOM
405  } else {
406  printf("xjadeo: '%s' -> '%s'\n", key.c_str(), value.c_str());
407 #endif
408  }
409  }
410  }
411  break;
412  default:
413  break;
414  }
415  }
416 }
417 
418 void
420 {
421  process->terminate(); // from gui-context clean up
422  Terminated();
423 }
424 
425 void
427 {
428  if (!_session) { return; }
429  XMLNode* node = _session->extra_xml (X_("XJSettings"), true);
430  if (!node) return;
431  node->remove_nodes_and_delete("XJSetting");
432 
433  for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
434  XMLNode* child = node->add_child (X_("XJSetting"));
435  child->add_property (X_("k"), it->first);
436  child->add_property (X_("v"), it->second);
437  }
438 }
439 
440 
441 void
443 {
444  SessionHandlePtr::set_session (s);
445  if (!_session) { return; }
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 (ARDOUR::max_framepos) //< skip setting or modifying offset
472 void
474 {
475  if (!_session) { return; }
476  if (editor->dragging_playhead()) { return ;}
478 }
479 
480 void
482 {
483  if (!is_started()) { return; }
484  if (!_session) { return; }
485  if (offset == NO_OFFSET ) { return; }
486 
487  framecnt_t video_frame_offset;
488  framecnt_t audio_sample_rate;
489  if (_session->config.get_videotimeline_pullup()) {
490  audio_sample_rate = _session->frame_rate();
491  } else {
492  audio_sample_rate = _session->nominal_frame_rate();
493  }
494 
495  /* Note: pull-up/down are applied here: frame_rate() vs. nominal_frame_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
511 {
512  if (!is_started()) { return; }
513  if (!_session) { return; }
514  framecnt_t video_frame;
515  framecnt_t audio_sample_rate;
516  if (_session->config.get_videotimeline_pullup()) {
517  audio_sample_rate = _session->frame_rate();
518  } else {
519  audio_sample_rate = _session->nominal_frame_rate();
520  }
521 
522  /* Note: pull-up/down are applied here: frame_rate() vs. nominal_frame_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  }
551 }
552 
553 void
555 {
556  if (!is_started()) { return; }
557  if (!_session) { return; }
558 
559  bool my_manual_seek = true;
560  if (_session->config.get_external_sync()) {
561  if (ARDOUR::Config->get_sync_source() == ARDOUR::Engine)
562  my_manual_seek = false;
563  }
564 
565  if (my_manual_seek != sync_by_manual_seek) {
566  if (sync_by_manual_seek) {
567  if (clock_connection.connected()) {
568  clock_connection.disconnect();
569  }
570  process->write_to_stdin("jack connect\n");
571  } else {
572  process->write_to_stdin("jack disconnect\n");
574  }
575  sync_by_manual_seek = my_manual_seek;
576  }
577 }
framepos_t audible_frame() const
Definition: session.cc:1760
void manual_seek(ARDOUR::framepos_t, bool, ARDOUR::frameoffset_t)
double timecode_frames_per_second() const
Definition: session_time.cc:55
framecnt_t nominal_frame_rate() const
Definition: session.h:367
ARDOUR::framepos_t manually_seeked_frame
int atoi(const string &s)
Definition: convert.cc:140
#define ui_bind(f,...)
Definition: gui_thread.h:37
bool emulate_key_event(Gtk::Widget *, unsigned int)
Definition: utils.cc:323
PBD::Signal0< void > Terminated
Definition: system_exec.h:183
XJSettings xjadeo_settings
Definition: video_monitor.h:98
bool set_custom_setting(const std::string, const std::string)
VideoMonitor(PublicEditor *, std::string)
void send_cmd(int what, int param)
#define NO_OFFSET
Representation of the interface of the Editor class.
Definition: Beats.hpp:239
LIBPBD_API Transmitter warning
void set_offset(ARDOUR::frameoffset_t)
const XMLNodeList & children(const std::string &str=std::string()) const
Definition: xml++.cc:329
std::ostream & endmsg(std::ostream &ostr)
Definition: transmitter.h:71
SessionConfiguration config
Definition: session.h:866
XMLNode * add_child(const char *)
Definition: xml++.cc:351
framecnt_t frame_rate() const
Definition: session.h:365
PBD::Signal1< void, unsigned int > XJKeyEvent
void save_session()
#define invalidator(x)
Definition: gui_thread.h:40
PBD::Signal1< void, std::string > UiState
Definition: video_monitor.h:77
void query_full_state(bool)
std::list< XMLNode * > XMLNodeList
Definition: xml++.h:44
virtual bool dragging_playhead() const =0
#define _(Text)
Definition: i18n.h:11
bool skip_setting(std::string)
#define X_(Text)
Definition: i18n.h:13
int64_t framecnt_t
Definition: types.h:76
LIBARDOUR_API RCConfiguration * Config
Definition: globals.cc:119
sigc::connection clock_connection
void parameter_changed(std::string const &p)
int write_to_stdin(std::string d, size_t len=0)
Definition: system_exec.cc:915
#define gui_context()
Definition: gui_thread.h:36
void set_session(ARDOUR::Session *s)
void xjadeo_sync_setup()
void open(std::string)
ARDOUR::SystemExec * process
Definition: video_monitor.h:86
int64_t frameoffset_t
Definition: types.h:71
PublicEditor * editor
Definition: video_monitor.h:85
PBD::Signal1< void, std::string > ParameterChanged
Definition: configuration.h:44
XMLProperty * add_property(const char *name, const std::string &value)
static PublicEditor & instance()
int start(int stderr_mode=1)
Definition: system_exec.h:38
Definition: editor.h:134
Definition: xml++.h:95
bool sync_by_manual_seek
sigc::connection fps_connect(const sigc::slot< void > &slot)
Definition: timers.cc:205
XMLNode * extra_xml(const std::string &str, bool add_if_missing=false)
Definition: stateful.cc:77
Definition: debug.h:30
const std::string get_custom_setting(const std::string)
PBD::Signal2< void, std::string, size_t > ReadStdout
Definition: system_exec.h:176
sigc::connection state_connection
sigc::signal< void > Terminated
Definition: video_monitor.h:76
int _restore_settings_mask
Definition: video_monitor.h:96
void remove_nodes_and_delete(const std::string &)
sigc::connection rapid_connect(const sigc::slot< void > &slot)
Definition: timers.cc:183
XMLNodeList::const_iterator XMLNodeConstIterator
Definition: xml++.h:49
int64_t framepos_t
void forward_keyevent(unsigned int)
ARDOUR::frameoffset_t video_offset
ARDOUR::Session * _session
void parse_output(std::string d, size_t s)
virtual ~VideoMonitor()