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