NOOP - use PBD's std::string to number functions
[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 "pbd/convert.h"
22 #include "gui_thread.h"
23 #include "ardour_ui.h"
24
25 #include <stdio.h>
26 #include "public_editor.h"
27 #include "video_monitor.h"
28
29 #include "i18n.h"
30
31 using namespace std;
32 using namespace PBD;
33
34 VideoMonitor::VideoMonitor (PublicEditor *ed, std::string xjadeo_bin_path)
35         : editor (ed)
36 {
37         manually_seeked_frame = 0;
38         fps =0.0; // = _session->timecode_frames_per_second();
39         sync_by_manual_seek = false;
40         _restore_settings_mask = 0;
41         clock_connection = sigc::connection();
42         state_connection = sigc::connection();
43         debug_enable = false;
44         state_clk_divide = 0;
45         starting = 0;
46         osdmode = 10; // 1: frameno, 2: timecode, 8: box
47
48         process = new SystemExec(xjadeo_bin_path, X_("-R"));
49         process->ReadStdout.connect_same_thread (*this, boost::bind (&VideoMonitor::parse_output, this, _1 ,_2));
50         process->Terminated.connect (*this, invalidator (*this), boost::bind (&VideoMonitor::terminated, this), gui_context());
51 }
52
53 VideoMonitor::~VideoMonitor ()
54 {
55         if (clock_connection.connected()) {
56                 clock_connection.disconnect();
57         }
58         if (state_connection.connected()) {
59                 state_connection.disconnect();
60         }
61         delete process;
62 }
63
64 bool
65 VideoMonitor::start ()
66 {
67         if (is_started()) {
68                 return true;
69         }
70
71         manually_seeked_frame = 0;
72         sync_by_manual_seek = false;
73         if (clock_connection.connected()) { clock_connection.disconnect(); }
74
75         if (process->start(debug_enable?2:1)) {
76                 return false;
77         }
78         return true;
79 }
80
81 void
82 VideoMonitor::query_full_state (bool wait)
83 {
84         knownstate = 0;
85         process->write_to_stdin("get windowsize\n");
86         process->write_to_stdin("get windowpos\n");
87         process->write_to_stdin("get letterbox\n");
88         process->write_to_stdin("get fullscreen\n");
89         process->write_to_stdin("get ontop\n");
90         process->write_to_stdin("get offset\n");
91         process->write_to_stdin("get osdcfg\n");
92         int timeout = 40;
93         if (wait && knownstate !=127 && --timeout) {
94                 usleep(50000);
95                 sched_yield();
96         }
97 }
98
99 void
100 VideoMonitor::quit ()
101 {
102         if (!is_started()) return;
103         if (state_connection.connected()) { state_connection.disconnect(); }
104         if (clock_connection.connected()) { clock_connection.disconnect(); }
105         process->write_to_stdin("quit\n");
106         /* the 'quit' command should result in process termination
107          * but in case it fails (communication failure, SIGSTOP, ??)
108          * here's a timeout..
109          */
110         int timeout = 40;
111         while (is_started() && --timeout) {
112                 usleep(50000);
113                 sched_yield();
114         }
115         if (timeout <= 0) {
116                 process->terminate();
117         }
118 }
119
120 void
121 VideoMonitor::open (std::string filename)
122 {
123         if (!is_started()) return;
124         manually_seeked_frame = 0;
125         osdmode = 10; // 1: frameno, 2: timecode, 8: box
126         sync_by_manual_seek = false;
127         starting = 15;
128         process->write_to_stdin("load " + filename + "\n");
129         process->write_to_stdin("set fps -1\n");
130         process->write_to_stdin("window resize 100%\n");
131         process->write_to_stdin("window ontop on\n");
132         process->write_to_stdin("set seekmode 1\n");
133         process->write_to_stdin("set override 47\n");
134         process->write_to_stdin("window letterbox on\n");
135         process->write_to_stdin("osd mode 10\n");
136         for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
137                 if (skip_setting(it->first)) { continue; }
138                 process->write_to_stdin(it->first + " " + it->second + "\n");
139         }
140         if (!state_connection.connected()) {
141                 starting = 15;
142                 querystate();
143                 state_clk_divide = 0;
144                 /* TODO once every two second or so -- state_clk_divide hack below */
145                 state_connection = ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &VideoMonitor::querystate));
146         }
147         xjadeo_sync_setup();
148 }
149
150 void
151 VideoMonitor::querystate ()
152 {
153         /* clock-divider hack -- RapidScreenUpdate == every_point_one_seconds */
154         state_clk_divide = (state_clk_divide + 1) % 300; // 30 secs
155         if (state_clk_divide == 0) {
156                 // every 30 seconds
157                 query_full_state(false);
158                 return;
159         }
160         if (state_clk_divide%25 != 0) {
161                 return;
162         }
163         // every 2.5 seconds:
164         process->write_to_stdin("get fullscreen\n");
165         process->write_to_stdin("get ontop\n");
166         process->write_to_stdin("get osdcfg\n");
167         process->write_to_stdin("get letterbox\n");
168 }
169
170 bool
171 VideoMonitor::skip_setting (std::string which)
172 {
173         if (_restore_settings_mask & XJ_OSD && which == "osd mode") { return true; }
174         if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
175         if (_restore_settings_mask & XJ_WINDOW_SIZE && which == "window size") { return true; }
176         if (_restore_settings_mask & XJ_WINDOW_POS && which == "window xy") { return true; }
177         if (_restore_settings_mask & XJ_WINDOW_ONTOP && which == "window ontop") { return true; }
178         if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
179         if (_restore_settings_mask & XJ_OFFSET && which == "set offset") { return true; }
180         if (_restore_settings_mask & XJ_FULLSCREEN && which == "window zoom") { return true; }
181         return false;
182 }
183
184 void
185 VideoMonitor::send_cmd (int what, int param)
186 {
187         bool osd_update = false;
188         if (!is_started()) return;
189         switch (what) {
190                 case 1:
191                         if (param) process->write_to_stdin("window ontop on\n");
192                         else process->write_to_stdin("window ontop off\n");
193                         break;
194                 case 2:
195                         if (param) osdmode |= 2;
196                         else osdmode &= ~2;
197                         osd_update = true;
198                         break;
199                 case 3:
200                         if (param) osdmode |= 1;
201                         else osdmode &= ~1;
202                         osd_update = true;
203                         break;
204                 case 4:
205                         if (param) osdmode |= 8;
206                         else osdmode &= ~8;
207                         osd_update = true;
208                         break;
209                 case 5:
210                         if (param) process->write_to_stdin("window zoom on\n");
211                         else process->write_to_stdin("window zoom off\n");
212                         break;
213                 case 6:
214                         if (param) process->write_to_stdin("window letterbox on\n");
215                         else process->write_to_stdin("window letterbox off\n");
216                         break;
217                 case 7:
218                         process->write_to_stdin("window resize 100%");
219                         break;
220                 default:
221                         break;
222         }
223         if (osd_update >= 0) {
224                 std::ostringstream osstream; osstream << "osd mode " << osdmode << "\n";
225                 process->write_to_stdin(osstream.str());
226         }
227 }
228
229 bool
230 VideoMonitor::is_started ()
231 {
232         return process->is_running();
233 }
234
235 void
236 VideoMonitor::parse_output (std::string d, size_t /*s*/)
237 {
238         std::string line = d;
239         std::string::size_type start = 0;
240         std::string::size_type end = 0;
241
242         while (1) {
243                 end = d.find('\n', start);
244                 if (end == std::string::npos) break;
245                 line = d.substr(start,end-start);
246                 start=end+1;
247                 if (line.length() <4 || line.at(0)!='@') continue;
248 #if 1 /* DEBUG */
249                 if (debug_enable) {
250                         printf("xjadeo: '%s'\n", line.c_str());
251                 }
252 #endif
253                 int status = atoi(line.substr(1,3));
254                 switch(status / 100) {
255                         case 4: /* errors */
256                                 if (status == 403) {
257                                         PBD::warning << _("Video Monitor: File Not Found.") << endmsg;
258                                         /* check: we should only write from the main thread.
259                                          * However it should not matter for 'quit'.
260                                          */
261                                         process->write_to_stdin("quit\n");
262                                 }
263                         case 1: /* requested async notifications */
264                         case 3: /* warnings ; command succeeded, but status is negative. */
265                                 break;
266                         case 2:
267 /* replies:
268  * 201: var=<int>
269  * 202: var=<double>
270  * 210: var=<int>x<int>
271  * 220: var=<string>
272  * 228: var=<smpte-string>
273  */
274                         {
275                                 std::string::size_type equalsign = line.find('=');
276                                 std::string::size_type comment = line.find('#');
277                                 if (comment != std::string::npos) { line = line.substr(0,comment); }
278                                 if (equalsign != std::string::npos) {
279                                         std::string key = line.substr(5, equalsign - 5);
280                                         std::string value = line.substr(equalsign + 1);
281 #if 0 /* DEBUG */
282                                         std::cout << "parsed: " << key << " => " << value << std::endl;
283 #endif
284                                         if       (key ==  "windowpos") {
285                                                 knownstate |= 16;
286                                                 if (xjadeo_settings["window xy"] != value) {
287                                                         if (!starting && _session) _session->set_dirty ();
288                                                 }
289                                                 xjadeo_settings["window xy"] = value;
290                                         } else if(key ==  "windowsize") {
291                                                 knownstate |= 32;
292                                                 if (xjadeo_settings["window size"] != value) {
293                                                         if (!starting && _session) _session->set_dirty ();
294                                                 }
295                                                 xjadeo_settings["window size"] = value;
296                                         } else if(key ==  "windowontop") {
297                                                 knownstate |= 2;
298                                                 if (starting || xjadeo_settings["window ontop"] != value) {
299                                                         if (!starting && _session) _session->set_dirty ();
300                                                         if (atoi(value)) { UiState("xjadeo-window-ontop-on"); }
301                                                         else { UiState("xjadeo-window-ontop-off"); }
302                                                         starting &= ~2;
303                                                 }
304                                                 xjadeo_settings["window ontop"] = value;
305                                         } else if(key ==  "fullscreen") {
306                                                 knownstate |= 4;
307                                                 if (starting || xjadeo_settings["window zoom"] != value) {
308                                                         if (!starting && _session) _session->set_dirty ();
309                                                         if (atoi(value)) { UiState("xjadeo-window-fullscreen-on"); }
310                                                         else { UiState("xjadeo-window-fullscreen-off"); }
311                                                         starting &= ~4;
312                                                 }
313                                                 xjadeo_settings["window zoom"] = value;
314                                         } else if(key ==  "letterbox") {
315                                                 knownstate |= 8;
316                                                 if (starting || xjadeo_settings["window letterbox"] != value) {
317                                                         if (!starting && _session) _session->set_dirty ();
318                                                         if (atoi(value)) { UiState("xjadeo-window-letterbox-on"); }
319                                                         else { UiState("xjadeo-window-letterbox-off"); }
320                                                         starting &= ~8;
321                                                 }
322                                                 xjadeo_settings["window letterbox"] = value;
323                                         } else if(key ==  "osdmode") {
324                                                 knownstate |= 1;
325                                                 osdmode = atoi(value);
326                                                 if (starting || atoi(xjadeo_settings["osd mode"]) != osdmode) {
327                                                         if (!starting && _session) _session->set_dirty ();
328                                                         if ((osdmode & 1) == 1) { UiState("xjadeo-window-osd-frame-on"); }
329                                                         if ((osdmode & 1) == 0) { UiState("xjadeo-window-osd-frame-off"); }
330                                                         if ((osdmode & 2) == 2) { UiState("xjadeo-window-osd-timecode-on"); }
331                                                         if ((osdmode & 2) == 0) { UiState("xjadeo-window-osd-timecode-off"); }
332                                                         if ((osdmode & 8) == 8) { UiState("xjadeo-window-osd-box-on"); }
333                                                         if ((osdmode & 8) == 0) { UiState("xjadeo-window-osd-box-off"); }
334                                                 }
335                                                 starting &= ~1;
336                                                 xjadeo_settings["osd mode"] = value;
337                                         } else if(key ==  "offset") {
338                                                 knownstate |= 64;
339                                                 if (xjadeo_settings["set offset"] != value) {
340                                                         if (!starting && _session) _session->set_dirty ();
341                                                 }
342                                                 xjadeo_settings["set offset"] = value;
343                                         }
344                                 }
345                         }
346                                 break;
347                         default:
348                                 break;
349                 }
350         }
351 }
352
353 void
354 VideoMonitor::terminated ()
355 {
356         process->terminate(); // from gui-context clean up
357         Terminated();
358 }
359
360 void
361 VideoMonitor::save_session ()
362 {
363         if (!_session) { return; }
364         XMLNode* node = _session->extra_xml (X_("XJSettings"));
365         if (!node) return;
366
367         for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
368           XMLNode* child = node->add_child (X_("XJSetting"));
369                 child->add_property (X_("k"), it->first);
370                 child->add_property (X_("v"), it->second);
371         }
372 }
373
374
375 void
376 VideoMonitor::set_session (ARDOUR::Session *s)
377 {
378         SessionHandlePtr::set_session (s);
379         if (!_session) { return; }
380         _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoMonitor::parameter_changed, this, _1), gui_context());
381         XMLNode* node = _session->extra_xml (X_("XJSettings"));
382         if (!node) { return;}
383         xjadeo_settings.clear();
384
385         XMLNodeList nlist = node->children();
386         XMLNodeConstIterator niter;
387         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
388                 xjadeo_settings[(*niter)->property(X_("k"))->value()] = (*niter)->property(X_("v"))->value();
389   }
390 }
391
392 void
393 VideoMonitor::clear_session_state ()
394 {
395         xjadeo_settings.clear();
396         if (!_session) { return; }
397         XMLNode* node = new XMLNode(X_("XJSettings"));
398         _session->add_extra_xml (*node);
399         _session->set_dirty ();
400 }
401
402 bool
403 VideoMonitor::set_custom_setting (const std::string k, const std::string v)
404 {
405         xjadeo_settings[k] = v;
406         return true; /* TODO: check if key is valid */
407 }
408
409 const std::string
410 VideoMonitor::get_custom_setting (const std::string k)
411 {
412         return (xjadeo_settings[k]);
413 }
414
415 #define NO_OFFSET (1<<31) //< skip setting or modifying offset --  TODO check ARDOUR::frameoffset_t max value.
416 void
417 VideoMonitor::srsupdate ()
418 {
419         if (!_session) { return; }
420         if (editor->dragging_playhead()) { return ;}
421         manual_seek(_session->audible_frame(), false, NO_OFFSET);
422 }
423
424 void
425 VideoMonitor::set_offset (ARDOUR::frameoffset_t offset)
426 {
427         if (!is_started()) { return; }
428         if (!_session) { return; }
429         if (offset == NO_OFFSET ) { return; }
430
431         framecnt_t video_frame_offset;
432         framecnt_t audio_frame_rate;
433         if (_session->config.get_videotimeline_pullup()) {
434                 audio_frame_rate = _session->frame_rate();
435         } else {
436                 audio_frame_rate = _session->nominal_frame_rate();
437         }
438
439         /* Note: pull-up/down are applied here: frame_rate() vs. nominal_frame_rate() */
440         if (_session->config.get_use_video_file_fps()) {
441                 video_frame_offset = floor(offset * fps / audio_frame_rate);
442         } else {
443                 video_frame_offset = floor(offset * _session->timecode_frames_per_second() / audio_frame_rate);
444         }
445
446         // TODO remember if changed..
447         std::ostringstream osstream1; osstream1 << -1 * video_frame_offset;
448         process->write_to_stdin("set offset " + osstream1.str() + "\n");
449 }
450
451 void
452 VideoMonitor::manual_seek (framepos_t when, bool /*force*/, ARDOUR::frameoffset_t offset)
453 {
454         if (!is_started()) { return; }
455         if (!_session) { return; }
456         framecnt_t video_frame;
457         framecnt_t audio_frame_rate;
458         if (_session->config.get_videotimeline_pullup()) {
459                 audio_frame_rate = _session->frame_rate();
460         } else {
461                 audio_frame_rate = _session->nominal_frame_rate();
462         }
463
464         /* Note: pull-up/down are applied here: frame_rate() vs. nominal_frame_rate() */
465         if (_session->config.get_use_video_file_fps()) {
466                 video_frame = floor(when * fps / audio_frame_rate);
467         } else {
468                 video_frame = floor(when * _session->timecode_frames_per_second() / audio_frame_rate);
469         }
470         if (video_frame < 0 ) video_frame = 0;
471
472         if (video_frame == manually_seeked_frame) { return; }
473         manually_seeked_frame = video_frame;
474
475 #if 0 /* DEBUG */
476         std::cout <<"seek: " << video_frame << std::endl;
477 #endif
478         std::ostringstream osstream; osstream << video_frame;
479         process->write_to_stdin("seek " + osstream.str() + "\n");
480
481         set_offset(offset);
482 }
483
484 void
485 VideoMonitor::parameter_changed (std::string const & p)
486 {
487         if (!is_started()) { return; }
488         if (!_session) { return; }
489         if (p != "external-sync" && p != "sync-source") {
490                 return;
491         }
492         xjadeo_sync_setup();
493 }
494
495 void
496 VideoMonitor::xjadeo_sync_setup ()
497 {
498         if (!is_started()) { return; }
499         if (!_session) { return; }
500
501         bool my_manual_seek = true;
502         if (_session->config.get_external_sync()) {
503                 if (ARDOUR::Config->get_sync_source() == ARDOUR::JACK)
504                         my_manual_seek = false;
505         }
506
507         if (my_manual_seek != sync_by_manual_seek) {
508                 if (sync_by_manual_seek) {
509                         if (clock_connection.connected()) {
510                                 clock_connection.disconnect();
511                         }
512                         process->write_to_stdin("jack connect\n");
513                 } else {
514                         process->write_to_stdin("jack disconnect\n");
515                         clock_connection = ARDOUR_UI::SuperRapidScreenUpdate.connect (sigc::mem_fun (*this, &VideoMonitor::srsupdate));
516                 }
517                 sync_by_manual_seek = my_manual_seek;
518         }
519 }