2 Copyright (C) 2010 Paul Davis
3 Author: Robin Gareus <robin@gareus.org>
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.
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.
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.
20 #ifdef WITH_VIDEOTIMELINE
22 #include "pbd/file_utils.h"
23 #include "gui_thread.h"
24 #include "ardour_ui.h"
27 #include "public_editor.h"
28 #include "video_monitor.h"
34 VideoMonitor::VideoMonitor (PublicEditor *ed, std::string xjadeo_bin_path)
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();
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());
49 VideoMonitor::~VideoMonitor ()
51 if (clock_connection.connected()) {
52 clock_connection.disconnect();
58 VideoMonitor::start ()
64 manually_seeked_frame = 0;
65 sync_by_manual_seek = false;
66 if (clock_connection.connected()) { clock_connection.disconnect(); }
68 if (process->start(debug_enable?2:1)) {
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");
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, ??)
93 while (is_started() && --timeout) {
97 printf("xjadeo connection: time-out. session may not be saved.\n");
100 process->terminate();
101 if (clock_connection.connected()) { clock_connection.disconnect(); }
105 VideoMonitor::open (std::string filename)
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");
126 VideoMonitor::skip_setting (std::string which)
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; }
140 VideoMonitor::is_started ()
142 return process->is_running();
146 VideoMonitor::parse_output (std::string d, size_t s)
148 std::string line = d;
149 std::string::size_type start = 0;
150 std::string::size_type end = 0;
153 end = d.find('\n', start);
154 if (end == std::string::npos) break;
155 line = d.substr(start,end-start);
157 if (line.length() <4 || line.at(0)!='@') continue;
160 printf("xjadeo: '%s'\n", line.c_str());
163 int status = atoi(line.substr(1,3).c_str());
164 switch(status / 100) {
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'.
171 process->write_to_stdin("quit\n");
173 case 1: /* requested async notifications */
174 case 3: /* warnings ; command succeeded, but status is negative. */
180 * 210: var=<int>x<int>
182 * 228: var=<smpte-string>
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);
192 std::cout << "parsed: " << key << " => " << value << std::endl;
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;
219 VideoMonitor::terminated ()
226 VideoMonitor::save_session ()
228 if (!_session) { return; }
229 bool is_dirty = false;
231 XMLNode* prev = _session->extra_xml (X_("XJSettings"));
232 XMLNode* node = new XMLNode(X_("XJSettings"));
234 if (!prev) { is_dirty = true; }
235 else { nlist = prev->children(); }
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);
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 ) {
251 if (!found) {is_dirty = true;}
256 _session->add_extra_xml (*node);
257 _session->set_dirty ();
263 VideoMonitor::set_session (ARDOUR::Session *s)
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();
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();
280 VideoMonitor::clear_session_state ()
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 ();
290 VideoMonitor::set_custom_setting (const std::string k, const std::string v)
292 xjadeo_settings[k] = v;
293 return true; /* TODO: check if key is valid */
297 VideoMonitor::get_custom_setting (const std::string k)
299 return (xjadeo_settings[k]);
301 #define NO_OFFSET (1<<31) //< skip setting or modifying offset -- TODO check ARDOUR::frameoffset_t max value.
303 VideoMonitor::srsupdate ()
305 if (!_session) { return; }
306 if (editor->dragging_playhead()) { return ;}
307 manual_seek(_session->audible_frame(), false, NO_OFFSET);
311 VideoMonitor::set_offset (ARDOUR::frameoffset_t offset)
313 if (!is_started()) { return; }
314 if (!_session) { return; }
315 if (offset == NO_OFFSET ) { return; }
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();
322 audio_frame_rate = _session->nominal_frame_rate();
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);
329 video_frame_offset = floor(offset * _session->timecode_frames_per_second() / audio_frame_rate);
332 // TODO remember if changed..
333 std::ostringstream osstream1; osstream1 << -1 * video_frame_offset;
334 process->write_to_stdin("set offset " + osstream1.str() + "\n");
338 VideoMonitor::manual_seek (framepos_t when, bool force, ARDOUR::frameoffset_t offset)
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();
347 audio_frame_rate = _session->nominal_frame_rate();
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);
354 video_frame = floor(when * _session->timecode_frames_per_second() / audio_frame_rate);
356 if (video_frame < 0 ) video_frame = 0;
358 if (video_frame == manually_seeked_frame) { return; }
359 manually_seeked_frame = video_frame;
362 std::cout <<"seek: " << video_frame << std::endl;
364 std::ostringstream osstream; osstream << video_frame;
365 process->write_to_stdin("seek " + osstream.str() + "\n");
371 VideoMonitor::parameter_changed (std::string const & p)
373 if (!is_started()) { return; }
374 if (!_session) { return; }
375 if (p != "external-sync" && p != "sync-source") {
382 VideoMonitor::xjadeo_sync_setup ()
384 if (!is_started()) { return; }
385 if (!_session) { return; }
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;
393 if (my_manual_seek != sync_by_manual_seek) {
394 if (sync_by_manual_seek) {
395 if (clock_connection.connected()) {
396 clock_connection.disconnect();
398 process->write_to_stdin("jack connect\n");
400 process->write_to_stdin("jack disconnect\n");
401 clock_connection = ARDOUR_UI::SuperRapidScreenUpdate.connect (sigc::mem_fun (*this, &VideoMonitor::srsupdate));
403 sync_by_manual_seek = my_manual_seek;
406 #endif /* WITH_VIDEOTIMELINE */