Merge branch 'master' into cairocanvas
[ardour.git] / gtk2_ardour / video_monitor.cc
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 "gui_thread.h"
22 #include "ardour_ui.h"
23
24 #include <stdio.h>
25 #include "public_editor.h"
26 #include "video_monitor.h"
27
28 #include "i18n.h"
29
30 using namespace std;
31
32 VideoMonitor::VideoMonitor (PublicEditor *ed, std::string xjadeo_bin_path)
33         : editor (ed)
34 {
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();
41         debug_enable = false;
42         state_clk_divide = 0;
43         starting = 0;
44         osdmode = 10; // 1: frameno, 2: timecode, 8: box
45
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());
49 }
50
51 VideoMonitor::~VideoMonitor ()
52 {
53         if (clock_connection.connected()) {
54                 clock_connection.disconnect();
55         }
56         if (state_connection.connected()) {
57                 state_connection.disconnect();
58         }
59         delete process;
60 }
61
62 bool
63 VideoMonitor::start ()
64 {
65         if (is_started()) {
66                 return true;
67         }
68
69         manually_seeked_frame = 0;
70         sync_by_manual_seek = false;
71         if (clock_connection.connected()) { clock_connection.disconnect(); }
72
73         if (process->start(debug_enable?2:1)) {
74                 return false;
75         }
76         return true;
77 }
78
79 void
80 VideoMonitor::query_full_state (bool wait)
81 {
82         knownstate = 0;
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");
90         int timeout = 40;
91         if (wait && knownstate !=127 && --timeout) {
92                 usleep(50000);
93                 sched_yield();
94         }
95 }
96
97 void
98 VideoMonitor::quit ()
99 {
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, ??)
106          * here's a timeout..
107          */
108         int timeout = 40;
109         while (is_started() && --timeout) {
110                 usleep(50000);
111                 sched_yield();
112         }
113         if (timeout <= 0) {
114                 process->terminate();
115         }
116 }
117
118 void
119 VideoMonitor::open (std::string filename)
120 {
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;
125         starting = 15;
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");
137         }
138         if (!state_connection.connected()) {
139                 starting = 15;
140                 querystate();
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));
144         }
145         xjadeo_sync_setup();
146 }
147
148 void
149 VideoMonitor::querystate ()
150 {
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) {
154                 // every 30 seconds
155                 query_full_state(false);
156                 return;
157         }
158         if (state_clk_divide%25 != 0) {
159                 return;
160         }
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");
166 }
167
168 bool
169 VideoMonitor::skip_setting (std::string which)
170 {
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; }
179         return false;
180 }
181
182 void
183 VideoMonitor::send_cmd (int what, int param)
184 {
185         bool osd_update = false;
186         if (!is_started()) return;
187         switch (what) {
188                 case 1:
189                         if (param) process->write_to_stdin("window ontop on\n");
190                         else process->write_to_stdin("window ontop off\n");
191                         break;
192                 case 2:
193                         if (param) osdmode |= 2;
194                         else osdmode &= ~2;
195                         osd_update = true;
196                         break;
197                 case 3:
198                         if (param) osdmode |= 1;
199                         else osdmode &= ~1;
200                         osd_update = true;
201                         break;
202                 case 4:
203                         if (param) osdmode |= 8;
204                         else osdmode &= ~8;
205                         osd_update = true;
206                         break;
207                 case 5:
208                         if (param) process->write_to_stdin("window zoom on\n");
209                         else process->write_to_stdin("window zoom off\n");
210                         break;
211                 case 6:
212                         if (param) process->write_to_stdin("window letterbox on\n");
213                         else process->write_to_stdin("window letterbox off\n");
214                         break;
215                 case 7:
216                         process->write_to_stdin("window resize 100%");
217                         break;
218                 default:
219                         break;
220         }
221         if (osd_update >= 0) {
222                 std::ostringstream osstream; osstream << "osd mode " << osdmode << "\n";
223                 process->write_to_stdin(osstream.str());
224         }
225 }
226
227 bool
228 VideoMonitor::is_started ()
229 {
230         return process->is_running();
231 }
232
233 void
234 VideoMonitor::parse_output (std::string d, size_t s)
235 {
236         std::string line = d;
237         std::string::size_type start = 0;
238         std::string::size_type end = 0;
239
240         while (1) {
241                 end = d.find('\n', start);
242                 if (end == std::string::npos) break;
243                 line = d.substr(start,end-start);
244                 start=end+1;
245                 if (line.length() <4 || line.at(0)!='@') continue;
246 #if 1 /* DEBUG */
247                 if (debug_enable) {
248                         printf("xjadeo: '%s'\n", line.c_str());
249                 }
250 #endif
251                 int status = atoi(line.substr(1,3).c_str());
252                 switch(status / 100) {
253                         case 4: /* errors */
254                                 if (status == 403) {
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'.
258                                          */
259                                         process->write_to_stdin("quit\n");
260                                 }
261                         case 1: /* requested async notifications */
262                         case 3: /* warnings ; command succeeded, but status is negative. */
263                                 break;
264                         case 2:
265 /* replies:
266  * 201: var=<int>
267  * 202: var=<double>
268  * 210: var=<int>x<int>
269  * 220: var=<string>
270  * 228: var=<smpte-string>
271  */
272                         {
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);
279 #if 0 /* DEBUG */
280                                         std::cout << "parsed: " << key << " => " << value << std::endl;
281 #endif
282                                         if       (key ==  "windowpos") {
283                                                 knownstate |= 16;
284                                                 if (xjadeo_settings["window xy"] != value) {
285                                                         if (!starting && _session) _session->set_dirty ();
286                                                 }
287                                                 xjadeo_settings["window xy"] = value;
288                                         } else if(key ==  "windowsize") {
289                                                 knownstate |= 32;
290                                                 if (xjadeo_settings["window size"] != value) {
291                                                         if (!starting && _session) _session->set_dirty ();
292                                                 }
293                                                 xjadeo_settings["window size"] = value;
294                                         } else if(key ==  "windowontop") {
295                                                 knownstate |= 2;
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"); }
300                                                         starting &= ~2;
301                                                 }
302                                                 xjadeo_settings["window ontop"] = value;
303                                         } else if(key ==  "fullscreen") {
304                                                 knownstate |= 4;
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"); }
309                                                         starting &= ~4;
310                                                 }
311                                                 xjadeo_settings["window zoom"] = value;
312                                         } else if(key ==  "letterbox") {
313                                                 knownstate |= 8;
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"); }
318                                                         starting &= ~8;
319                                                 }
320                                                 xjadeo_settings["window letterbox"] = value;
321                                         } else if(key ==  "osdmode") {
322                                                 knownstate |= 1;
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"); }
332                                                 }
333                                                 starting &= ~1;
334                                                 xjadeo_settings["osd mode"] = value;
335                                         } else if(key ==  "offset") {
336                                                 knownstate |= 64;
337                                                 if (xjadeo_settings["set offset"] != value) {
338                                                         if (!starting && _session) _session->set_dirty ();
339                                                 }
340                                                 xjadeo_settings["set offset"] = value;
341                                         }
342                                 }
343                         }
344                                 break;
345                         default:
346                                 break;
347                 }
348         }
349 }
350
351 void
352 VideoMonitor::terminated ()
353 {
354         process->terminate(); // from gui-context clean up
355         Terminated();
356 }
357
358 void
359 VideoMonitor::save_session ()
360 {
361         if (!_session) { return; }
362         XMLNode* node = _session->extra_xml (X_("XJSettings"));
363         if (!node) return;
364
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);
369         }
370 }
371
372
373 void
374 VideoMonitor::set_session (ARDOUR::Session *s)
375 {
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();
382
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();
387   }
388 }
389
390 void
391 VideoMonitor::clear_session_state ()
392 {
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 ();
398 }
399
400 bool
401 VideoMonitor::set_custom_setting (const std::string k, const std::string v)
402 {
403         xjadeo_settings[k] = v;
404         return true; /* TODO: check if key is valid */
405 }
406
407 const std::string
408 VideoMonitor::get_custom_setting (const std::string k)
409 {
410         return (xjadeo_settings[k]);
411 }
412
413 #define NO_OFFSET (1<<31) //< skip setting or modifying offset --  TODO check ARDOUR::frameoffset_t max value.
414 void
415 VideoMonitor::srsupdate ()
416 {
417         if (!_session) { return; }
418         if (editor->dragging_playhead()) { return ;}
419         manual_seek(_session->audible_frame(), false, NO_OFFSET);
420 }
421
422 void
423 VideoMonitor::set_offset (ARDOUR::frameoffset_t offset)
424 {
425         if (!is_started()) { return; }
426         if (!_session) { return; }
427         if (offset == NO_OFFSET ) { return; }
428
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();
433         } else {
434                 audio_frame_rate = _session->nominal_frame_rate();
435         }
436
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);
440         } else {
441                 video_frame_offset = floor(offset * _session->timecode_frames_per_second() / audio_frame_rate);
442         }
443
444         // TODO remember if changed..
445         std::ostringstream osstream1; osstream1 << -1 * video_frame_offset;
446         process->write_to_stdin("set offset " + osstream1.str() + "\n");
447 }
448
449 void
450 VideoMonitor::manual_seek (framepos_t when, bool force, ARDOUR::frameoffset_t offset)
451 {
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();
458         } else {
459                 audio_frame_rate = _session->nominal_frame_rate();
460         }
461
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);
465         } else {
466                 video_frame = floor(when * _session->timecode_frames_per_second() / audio_frame_rate);
467         }
468         if (video_frame < 0 ) video_frame = 0;
469
470         if (video_frame == manually_seeked_frame) { return; }
471         manually_seeked_frame = video_frame;
472
473 #if 0 /* DEBUG */
474         std::cout <<"seek: " << video_frame << std::endl;
475 #endif
476         std::ostringstream osstream; osstream << video_frame;
477         process->write_to_stdin("seek " + osstream.str() + "\n");
478
479         set_offset(offset);
480 }
481
482 void
483 VideoMonitor::parameter_changed (std::string const & p)
484 {
485         if (!is_started()) { return; }
486         if (!_session) { return; }
487         if (p != "external-sync" && p != "sync-source") {
488                 return;
489         }
490         xjadeo_sync_setup();
491 }
492
493 void
494 VideoMonitor::xjadeo_sync_setup ()
495 {
496         if (!is_started()) { return; }
497         if (!_session) { return; }
498
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;
503         }
504
505         if (my_manual_seek != sync_by_manual_seek) {
506                 if (sync_by_manual_seek) {
507                         if (clock_connection.connected()) {
508                                 clock_connection.disconnect();
509                         }
510                         process->write_to_stdin("jack connect\n");
511                 } else {
512                         process->write_to_stdin("jack disconnect\n");
513                         clock_connection = ARDOUR_UI::SuperRapidScreenUpdate.connect (sigc::mem_fun (*this, &VideoMonitor::srsupdate));
514                 }
515                 sync_by_manual_seek = my_manual_seek;
516         }
517 }