f29712e326a570bd9a9ba6d570a80b1ec87c5c26
[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         debug_enable = false;
43
44         process = new SystemExec(xjadeo_bin_path, X_("-R"));
45         process->ReadStdout.connect (*this, invalidator (*this), boost::bind (&VideoMonitor::parse_output, this, _1 ,_2), gui_context());
46         process->Terminated.connect (*this, invalidator (*this), boost::bind (&VideoMonitor::terminated, this), gui_context());
47 }
48
49 VideoMonitor::~VideoMonitor ()
50 {
51         if (clock_connection.connected()) {
52                 clock_connection.disconnect();
53         }
54         delete process;
55 }
56
57 bool
58 VideoMonitor::start ()
59 {
60         if (is_started()) {
61                 return true;
62         }
63
64         manually_seeked_frame = 0;
65         sync_by_manual_seek = false;
66         if (clock_connection.connected()) { clock_connection.disconnect(); }
67
68         if (process->start(debug_enable?2:1)) {
69                 return false;
70         }
71         return true;
72 }
73
74 void
75 VideoMonitor::quit ()
76 {
77         if (!is_started()) return;
78         process->write_to_stdin("get windowsize\n");
79         process->write_to_stdin("get windowpos\n");
80         process->write_to_stdin("get letterbox\n");
81         process->write_to_stdin("get fullscreen\n");
82         process->write_to_stdin("get ontop\n");
83         process->write_to_stdin("get offset\n");
84         process->write_to_stdin("get osdcfg\n");
85         process->write_to_stdin("quit\n");
86 #if 1
87         /* wait for replies to the config requests above.
88          * the 'quit' command should result in process termination
89          * but in case it fails (communication failure, SIGSTOP, ??)
90          * here's a timeout..
91          */
92         int timeout = 40;
93         while (is_started() && --timeout) {
94                 usleep(50000);
95         }
96         if (timeout == 0) {
97                 printf("xjadeo connection: time-out. session may not be saved.\n");
98         }
99 #endif
100         process->terminate();
101         if (clock_connection.connected()) { clock_connection.disconnect(); }
102 }
103
104 void
105 VideoMonitor::open (std::string filename)
106 {
107         if (!is_started()) return;
108         manually_seeked_frame = 0;
109         sync_by_manual_seek = false;
110         process->write_to_stdin("load " + filename + "\n");
111         process->write_to_stdin("set fps -1\n");
112         process->write_to_stdin("window resize 100%\n");
113         process->write_to_stdin("window ontop on\n");
114         process->write_to_stdin("set seekmode 1\n");
115         process->write_to_stdin("set override 47\n");
116         process->write_to_stdin("window letterbox on\n");
117         process->write_to_stdin("osd mode 10\n");
118         for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
119                 if (skip_setting(it->first)) { continue; }
120                 process->write_to_stdin(it->first + " " + it->second + "\n");
121         }
122         xjadeo_sync_setup();
123 }
124
125 bool
126 VideoMonitor::skip_setting (std::string which)
127 {
128         if (_restore_settings_mask & XJ_OSD && which == "osd mode") { return true; }
129         if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
130         if (_restore_settings_mask & XJ_WINDOW_SIZE && which == "window size") { return true; }
131         if (_restore_settings_mask & XJ_WINDOW_POS && which == "window xy") { return true; }
132         if (_restore_settings_mask & XJ_WINDOW_ONTOP && which == "window ontop") { return true; }
133         if (_restore_settings_mask & XJ_LETTERBOX && which == "window letterbox") { return true; }
134         if (_restore_settings_mask & XJ_OFFSET && which == "set offset") { return true; }
135         if (_restore_settings_mask & XJ_FULLSCREEN && which == "window zoom") { return true; }
136         return false;
137 }
138
139 bool
140 VideoMonitor::is_started ()
141 {
142         return process->is_running();
143 }
144
145 void
146 VideoMonitor::parse_output (std::string d, size_t s)
147 {
148         std::string line = d;
149         std::string::size_type start = 0;
150         std::string::size_type end = 0;
151
152         while (1) {
153                 end = d.find('\n', start);
154                 if (end == std::string::npos) break;
155                 line = d.substr(start,end-start);
156                 start=end+1;
157                 if (line.length() <4 || line.at(0)!='@') continue;
158 #if 1 /* DEBUG */
159                 if (debug_enable) {
160                         printf("xjadeo: '%s'\n", line.c_str());
161                 }
162 #endif
163                 int status = atoi(line.substr(1,3).c_str());
164                 switch(status / 100) {
165                         case 4: /* errors */
166                                 if (status == 403) {
167                                         PBD::warning << _("Video Monitor: File Not Found.") << endmsg;
168                                         /* check: we should only write from the main thread.
169                                          * However it should not matter for 'quit'.
170                                          */
171                                         process->write_to_stdin("quit\n");
172                                 }
173                         case 1: /* requested async notifications */
174                         case 3: /* warnings ; command succeeded, but status is negative. */
175                                 break;
176                         case 2:
177 /* replies:
178  * 201: var=<int>
179  * 202: var=<double>
180  * 210: var=<int>x<int>
181  * 220: var=<string>
182  * 228: var=<smpte-string>
183  */
184                         {
185                                 std::string::size_type equalsign = line.find('=');
186                                 std::string::size_type comment = line.find('#');
187                                 if (comment != std::string::npos) { line = line.substr(0,comment); }
188                                 if (equalsign != std::string::npos) {
189                                         std::string key = line.substr(5, equalsign - 5);
190                                         std::string value = line.substr(equalsign + 1);
191 #if 0 /* DEBUG */
192                                         std::cout << "parsed: " << key << " => " << value << std::endl;
193 #endif
194                                                if(key ==  "windowpos") {
195                                                 xjadeo_settings["window xy"] = value;
196                                         } else if(key ==  "windowsize") {
197                                                 xjadeo_settings["window size"] = value;
198                                         } else if(key ==  "windowontop") {
199                                                 xjadeo_settings["window ontop"] = value;
200                                         } else if(key ==  "fullscreen") {
201                                                 xjadeo_settings["window zoom"] = value;
202                                         } else if(key ==  "letterbox") {
203                                                 xjadeo_settings["window letterbox"] = value;
204                                         } else if(key ==  "osdmode") {
205                                                 xjadeo_settings["osd mode"] = value;
206                                         } else if(key ==  "offset") {
207                                                 xjadeo_settings["set offset"] = value;
208                                         }
209                                 }
210                         }
211                                 break;
212                         default:
213                                 break;
214                 }
215         }
216 }
217
218 void
219 VideoMonitor::terminated ()
220 {
221         save_session();
222         Terminated();
223 }
224
225 void
226 VideoMonitor::save_session ()
227 {
228         if (!_session) { return; }
229         bool is_dirty = false;
230
231         XMLNode* prev = _session->extra_xml (X_("XJSettings"));
232         XMLNode* node = new XMLNode(X_("XJSettings"));
233         XMLNodeList nlist;
234         if (!prev) { is_dirty = true; }
235         else { nlist = prev->children(); }
236
237         for(XJSettings::const_iterator it = xjadeo_settings.begin(); it != xjadeo_settings.end(); ++it) {
238           XMLNode* child = node->add_child (X_("XJSetting"));
239                 child->add_property (X_("k"), it->first);
240                 child->add_property (X_("v"), it->second);
241                 if (!is_dirty) {
242                         bool found = false;
243                         XMLNodeConstIterator i;
244                         for (i = nlist.begin(); i != nlist.end(); ++i) {
245                                 if ((*i)->property(X_("k"))->value() == it->first &&
246                                     (*i)->property(X_("v"))->value() == it->second ) {
247                                         found=true;
248                                         break;
249                                 }
250                         }
251                         if (!found) {is_dirty = true;}
252                 }
253         }
254
255         if (is_dirty) {
256           _session->add_extra_xml (*node);
257           _session->set_dirty ();
258         }
259 }
260
261
262 void
263 VideoMonitor::set_session (ARDOUR::Session *s)
264 {
265         SessionHandlePtr::set_session (s);
266         if (!_session) { return; }
267         _session->config.ParameterChanged.connect (*this, invalidator (*this), ui_bind (&VideoMonitor::parameter_changed, this, _1), gui_context());
268         XMLNode* node = _session->extra_xml (X_("XJSettings"));
269         if (!node) { return;}
270         xjadeo_settings.clear();
271
272         XMLNodeList nlist = node->children();
273         XMLNodeConstIterator niter;
274         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
275                 xjadeo_settings[(*niter)->property(X_("k"))->value()] = (*niter)->property(X_("v"))->value();
276   }
277 }
278
279 void
280 VideoMonitor::clear_session_state ()
281 {
282         xjadeo_settings.clear();
283         if (!_session) { return; }
284         XMLNode* node = new XMLNode(X_("XJSettings"));
285         _session->add_extra_xml (*node);
286         _session->set_dirty ();
287 }
288
289 bool
290 VideoMonitor::set_custom_setting (const std::string k, const std::string v)
291 {
292         xjadeo_settings[k] = v;
293         return true; /* TODO: check if key is valid */
294 }
295
296 const std::string
297 VideoMonitor::get_custom_setting (const std::string k)
298 {
299         return (xjadeo_settings[k]);
300 }
301 #define NO_OFFSET (1<<31) //< skip setting or modifying offset --  TODO check ARDOUR::frameoffset_t max value.
302 void
303 VideoMonitor::srsupdate ()
304 {
305         if (!_session) { return; }
306         if (editor->dragging_playhead()) { return ;}
307         manual_seek(_session->audible_frame(), false, NO_OFFSET);
308 }
309
310 void
311 VideoMonitor::set_offset (ARDOUR::frameoffset_t offset)
312 {
313         if (!is_started()) { return; }
314         if (!_session) { return; }
315         if (offset == NO_OFFSET ) { return; }
316
317         framecnt_t video_frame_offset;
318         framecnt_t audio_frame_rate;
319         if (_session->config.get_videotimeline_pullup()) {
320                 audio_frame_rate = _session->frame_rate();
321         } else {
322                 audio_frame_rate = _session->nominal_frame_rate();
323         }
324
325         /* Note: pull-up/down are applied here: frame_rate() vs. nominal_frame_rate() */
326         if (_session->config.get_use_video_file_fps()) {
327                 video_frame_offset = floor(offset * fps / audio_frame_rate);
328         } else {
329                 video_frame_offset = floor(offset * _session->timecode_frames_per_second() / audio_frame_rate);
330         }
331
332         // TODO remember if changed..
333         std::ostringstream osstream1; osstream1 << -1 * video_frame_offset;
334         process->write_to_stdin("set offset " + osstream1.str() + "\n");
335 }
336
337 void
338 VideoMonitor::manual_seek (framepos_t when, bool force, ARDOUR::frameoffset_t offset)
339 {
340         if (!is_started()) { return; }
341         if (!_session) { return; }
342         framecnt_t video_frame;
343         framecnt_t audio_frame_rate;
344         if (_session->config.get_videotimeline_pullup()) {
345                 audio_frame_rate = _session->frame_rate();
346         } else {
347                 audio_frame_rate = _session->nominal_frame_rate();
348         }
349
350         /* Note: pull-up/down are applied here: frame_rate() vs. nominal_frame_rate() */
351         if (_session->config.get_use_video_file_fps()) {
352                 video_frame = floor(when * fps / audio_frame_rate);
353         } else {
354                 video_frame = floor(when * _session->timecode_frames_per_second() / audio_frame_rate);
355         }
356         if (video_frame < 0 ) video_frame = 0;
357
358         if (video_frame == manually_seeked_frame) { return; }
359         manually_seeked_frame = video_frame;
360
361 #if 0 /* DEBUG */
362         std::cout <<"seek: " << video_frame << std::endl;
363 #endif
364         std::ostringstream osstream; osstream << video_frame;
365         process->write_to_stdin("seek " + osstream.str() + "\n");
366
367         set_offset(offset);
368 }
369
370 void
371 VideoMonitor::parameter_changed (std::string const & p)
372 {
373         if (!is_started()) { return; }
374         if (!_session) { return; }
375         if (p != "external-sync" && p != "sync-source") {
376                 return;
377         }
378         xjadeo_sync_setup();
379 }
380
381 void
382 VideoMonitor::xjadeo_sync_setup ()
383 {
384         if (!is_started()) { return; }
385         if (!_session) { return; }
386
387         bool my_manual_seek = true;
388         if (_session->config.get_external_sync()) {
389                 if (ARDOUR::Config->get_sync_source() == ARDOUR::JACK)
390                         my_manual_seek = false;
391         }
392
393         if (my_manual_seek != sync_by_manual_seek) {
394                 if (sync_by_manual_seek) {
395                         if (clock_connection.connected()) {
396                                 clock_connection.disconnect();
397                         }
398                         process->write_to_stdin("jack connect\n");
399                 } else {
400                         process->write_to_stdin("jack disconnect\n");
401                         clock_connection = ARDOUR_UI::SuperRapidScreenUpdate.connect (sigc::mem_fun (*this, &VideoMonitor::srsupdate));
402                 }
403                 sync_by_manual_seek = my_manual_seek;
404         }
405 }
406 #endif /* WITH_VIDEOTIMELINE */