port 7.2 mods: align_edits foreground plugin refresh_frame tweak, rework soundlevel...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / pluginserver.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "amodule.h"
23 #include "atrack.h"
24 #include "attachmentpoint.h"
25 #include "autoconf.h"
26 #include "bcsignals.h"
27 #include "cplayback.h"
28 #include "cstrdup.h"
29 #include "cwindow.h"
30 #include "edl.h"
31 #include "edlsession.h"
32 #include "file.h"
33 #include "floatautos.h"
34 #include "keyframes.h"
35 #include "localsession.h"
36 #include "mainerror.h"
37 #include "mainprogress.h"
38 #include "menueffects.h"
39 #include "mwindow.h"
40 #include "mwindowgui.h"
41 #include "playbackengine.h"
42 #include "plugin.h"
43 #include "pluginaclient.h"
44 #include "pluginaclientlad.h"
45 #include "pluginfclient.h"
46 #include "pluginclient.h"
47 #include "plugincommands.h"
48 #include "pluginserver.h"
49 #include "pluginvclient.h"
50 #include "preferences.h"
51 #include "samples.h"
52 #include "sema.h"
53 #include "mainsession.h"
54 #include "theme.h"
55 #include "trackcanvas.h"
56 #include "transportque.h"
57 #include "vdevicex11.h"
58 #include "vframe.h"
59 #include "videodevice.h"
60 #include "virtualanode.h"
61 #include "virtualvnode.h"
62 #include "vmodule.h"
63 #include "vtrack.h"
64
65
66 #include<stdio.h>
67 #include<unistd.h>
68 #include<fcntl.h>
69 #include <sys/types.h>
70 #include <sys/wait.h>
71 #include<sys/stat.h>
72 #include <sys/mman.h>
73
74
75 void PluginServer::init()
76 {
77         reset_parameters();
78         this->plugin_type = PLUGIN_TYPE_UNKNOWN;
79         plugin_obj = new PluginObj();
80         modules = new ArrayList<Module*>;
81         nodes = new ArrayList<VirtualNode*>;
82         tip = 0;
83         gui_id = -1;
84 }
85
86 PluginServer::PluginServer()
87 {
88         init();
89 }
90
91 PluginServer::PluginServer(MWindow *mwindow, const char *path, int type)
92 {
93         char fpath[BCTEXTLEN];
94         init();
95         this->plugin_type = type;
96         this->mwindow = mwindow;
97         if( type == PLUGIN_TYPE_FFMPEG ) {
98                 ff_name = cstrdup(path);
99                 sprintf(fpath, "ff_%s", path);
100                 path = fpath;
101         }
102         set_path(path);
103 }
104
105 PluginServer::PluginServer(PluginServer &that)
106 {
107         reset_parameters();
108         plugin_type = that.plugin_type;
109         plugin_obj = that.plugin_obj;
110         plugin_obj->add_user();
111         title = !that.title ? 0 : cstrdup(that.title);
112         tip = !that.tip ? 0 : cstrdup(that.tip);
113         path = !that.path ? 0 : cstrdup(that.path);
114         ff_name = !that.ff_name ? 0 : cstrdup(that.ff_name);
115         modules = new ArrayList<Module*>;
116         nodes = new ArrayList<VirtualNode*>;
117
118         attachment = that.attachment;
119         realtime = that.realtime;
120         multichannel = that.multichannel;
121         preferences = that.preferences;
122         synthesis = that.synthesis;
123         audio = that.audio;
124         video = that.video;
125         theme = that.theme;
126         fileio = that.fileio;
127         uses_gui = that.uses_gui;
128         mwindow = that.mwindow;
129         keyframe = that.keyframe;
130         new_plugin = that.new_plugin;
131
132         lad_descriptor = that.lad_descriptor;
133         lad_descriptor_function = that.lad_descriptor_function;
134         lad_index = that.lad_index;
135 }
136
137 PluginServer::~PluginServer()
138 {
139         close_plugin();
140         delete [] path;
141         delete [] ff_name;
142         delete [] title;
143         delete [] tip;
144         delete modules;
145         delete nodes;
146         delete picon;
147         plugin_obj->remove_user();
148 }
149
150 // Done only once at creation
151 int PluginServer::reset_parameters()
152 {
153         mwindow = 0;
154         keyframe = 0;
155         prompt = 0;
156         cleanup_plugin();
157
158         plugin_obj = 0;
159         client = 0; 
160         new_plugin = 0;
161         lad_index = -1;
162         lad_descriptor_function = 0;
163         lad_descriptor = 0;
164         ff_name = 0;
165         use_opengl = 0;
166         vdevice = 0;
167         plugin_type = 0;
168         start_auto = end_auto = 0;
169         autos = 0;
170         reverse = 0;
171         plugin_open = 0;
172         realtime = multichannel = fileio = 0;
173         synthesis = 0;
174         audio = video = theme = 0;
175         uses_gui = 0;
176         transition = 0;
177         title = 0;
178         tip = 0;
179         path = 0;
180         data_text = 0;
181         for( int i=sizeof(args)/sizeof(args[0]); --i>=0; ) args[i] = 0;
182         total_args = 0; 
183         dir_idx = 0;
184         modules = 0;
185         nodes = 0;
186         attachmentpoint = 0;
187         edl = 0;
188         preferences = 0;
189         prompt = 0;
190         temp_frame = 0;
191         picon = 0;
192
193         return 0;
194 }
195
196 // Done every time the plugin is opened or closed
197 int PluginServer::cleanup_plugin()
198 {
199         in_buffer_size = out_buffer_size = 0;
200         total_in_buffers = total_out_buffers = 0;
201         error_flag = 0;
202         written_samples = 0;
203         shared_buffers = 0;
204         new_buffers = 0;
205         written_samples = written_frames = 0;
206         gui_on = 0;
207         plugin = 0;
208         plugin_open = 0;
209         return 0;
210 }
211
212 void PluginServer::set_mwindow(MWindow *mwindow)
213 {
214         this->mwindow = mwindow;
215 }
216
217 void PluginServer::set_attachmentpoint(AttachmentPoint *attachmentpoint)
218 {
219         this->attachmentpoint = attachmentpoint;
220 }
221
222 void PluginServer::set_keyframe(KeyFrame *keyframe)
223 {
224         this->keyframe = keyframe;
225 }
226
227 void PluginServer::set_prompt(MenuEffectPrompt *prompt)
228 {
229         this->prompt = prompt;
230 }
231
232 void PluginServer::set_lad_index(int i)
233 {
234         this->lad_index = i;
235 }
236
237 int PluginServer::get_lad_index()
238 {
239         return this->lad_index;
240 }
241
242 int PluginServer::is_unknown()
243 {
244         return plugin_type == PLUGIN_TYPE_UNKNOWN ? 1 : 0;
245 }
246
247 int PluginServer::is_executable()
248 {
249         return  plugin_type == PLUGIN_TYPE_EXECUTABLE ? 1 : 0;
250 }
251
252 int PluginServer::is_builtin()
253 {
254         return plugin_type == PLUGIN_TYPE_BUILTIN ? 1 : 0;
255 }
256
257 int PluginServer::is_ladspa()
258 {
259         return plugin_type == PLUGIN_TYPE_LADSPA ? 1 : 0;
260 }
261
262 int PluginServer::is_ffmpeg()
263 {
264         return plugin_type == PLUGIN_TYPE_FFMPEG ? 1 : 0;
265 }
266
267 int PluginServer::is_lv2()
268 {
269         return plugin_type == PLUGIN_TYPE_LV2 ? 1 : 0;
270 }
271
272 void PluginServer::set_path(const char *path)
273 {
274         delete [] this->path;
275         this->path = cstrdup(path);
276 }
277
278 char* PluginServer::get_path()
279 {
280         return this->path;
281 }
282
283 int PluginServer::get_synthesis()
284 {
285         return synthesis;
286 }
287
288
289 void PluginServer::set_title(const char *string)
290 {
291         delete [] title;
292         title = cstrdup(string);
293 }
294
295 void PluginServer::generate_display_title(char *string)
296 {
297         char ltitle[BCTEXTLEN];
298         if( BC_Resources::locale_utf8 )
299                 strcpy(ltitle, _(title));
300         else
301                 BC_Resources::encode(BC_Resources::encoding, 0,
302                                 _(title),strlen(title)+1, ltitle,BCTEXTLEN);
303         if( plugin && plugin->track )
304                 sprintf(string, "%s: %s", plugin->track->title, ltitle);
305         else
306                 strcpy(string, ltitle);
307 }
308
309 void *PluginObj::load(const char *plugin_dir, const char *path)
310 {
311         char dlpath[BCTEXTLEN], *dp = dlpath;
312         const char *cp = path;
313         if( *cp != '/' ) {
314                 const char *bp = plugin_dir;
315                 while( *bp ) *dp++ = *bp++;
316                 *dp++ = '/';
317         }
318         while( *cp ) *dp++ = *cp++;
319         *dp = 0;
320         return dlobj = load(dlpath);
321 }
322
323 int PluginServer::load_obj()
324 {
325         void *obj = plugin_obj->obj();
326         if( !obj ) obj =plugin_obj->load(preferences->plugin_dir, path);
327         return obj ? 1 : 0;
328 }
329
330 const char *PluginServer::load_error()
331 {
332         return plugin_obj->load_error();
333 }
334
335 void *PluginServer::get_sym(const char *sym)
336 {
337         if( !plugin_obj->obj() ) return 0;
338         return plugin_obj->load_sym(sym);
339 }
340
341 // Open plugin for signal processing
342 int PluginServer::open_plugin(int master,
343         Preferences *preferences,
344         EDL *edl,
345         Plugin *plugin)
346 {
347         if( plugin_open ) return 0;
348
349         this->preferences = preferences;
350         this->plugin = plugin;
351         this->edl = edl;
352         if( !is_ffmpeg() && !is_lv2() && !is_executable() && !load_obj() ) {
353 // If the load failed, can't use error string to detect executable
354 //  because locale and language selection changes the load_error() message
355 //      if( !strstr(string, "executable") ) { set_title(path); plugin_type = PLUGIN_TYPE_EXECUTABLE; }
356                 eprintf("PluginServer::open_plugin: load_obj %s = %s\n", path, load_error());
357                 return PLUGINSERVER_NOT_RECOGNIZED;
358         }
359         if( is_unknown() || is_builtin() ) {
360                 new_plugin =
361                         (PluginClient* (*)(PluginServer*)) get_sym("new_plugin");
362                 if( new_plugin )
363                         plugin_type = PLUGIN_TYPE_BUILTIN;
364         }
365         if( is_unknown() || is_ladspa() ) {
366                 lad_descriptor_function =
367                         (LADSPA_Descriptor_Function) get_sym("ladspa_descriptor");
368                 if( lad_descriptor_function ) {
369                         if( lad_index < 0 ) return PLUGINSERVER_IS_LAD;
370                         lad_descriptor = lad_descriptor_function(lad_index);
371                         if( !lad_descriptor )
372                                 return PLUGINSERVER_NOT_RECOGNIZED;
373                         plugin_type = PLUGIN_TYPE_LADSPA;
374                 }
375         }
376         if( is_unknown() ) {
377                 fprintf(stderr, "PluginServer::open_plugin "
378                         " %d: plugin undefined in %s\n", __LINE__, path);
379                 return PLUGINSERVER_NOT_RECOGNIZED;
380         }
381         switch( plugin_type ) {
382         case PLUGIN_TYPE_EXECUTABLE:
383                 return PLUGINSERVER_OK;
384         case PLUGIN_TYPE_BUILTIN:
385                 client = new_plugin(this);
386                 break;
387         case PLUGIN_TYPE_FFMPEG:
388                 client = new_ffmpeg_plugin();
389                 break;
390         case PLUGIN_TYPE_LADSPA:
391                 client = new PluginAClientLAD(this);
392                 break;
393         case PLUGIN_TYPE_LV2:
394                 client = new_lv2_plugin();
395                 break;
396         default:
397                 client = 0;
398                 break;
399         }
400         if( !client )
401                 return PLUGINSERVER_NOT_RECOGNIZED;
402
403 // Run initialization functions
404         realtime = client->is_realtime();
405 // Don't load defaults when probing the directory.
406         if( !master ) {
407                 if( realtime )
408                         client->load_defaults_xml();
409                 else
410                         client->load_defaults();
411         }
412         audio = client->is_audio();
413         video = client->is_video();
414         theme = client->is_theme();
415         fileio = client->is_fileio();
416         uses_gui = client->uses_gui();
417         multichannel = client->is_multichannel();
418         synthesis = client->is_synthesis();
419         transition = client->is_transition();
420         set_title(client->plugin_title());
421
422 //printf("PluginServer::open_plugin 2\n");
423         plugin_open = 1;
424         return PLUGINSERVER_OK;
425 }
426
427 int PluginServer::close_plugin()
428 {
429         if( !plugin_open ) return 0;
430
431         if( client ) {
432 // Defaults are saved in the thread.
433 //              if( client->defaults ) client->save_defaults();
434                 delete client;
435         }
436
437 // shared object is persistent since plugin deletion would unlink its own object
438         plugin_open = 0;
439         cleanup_plugin();
440         return 0;
441 }
442
443 void PluginServer::client_side_close()
444 {
445 // Last command executed in client thread
446         if( plugin )
447                 mwindow->hide_plugin(plugin, 1);
448         else
449         if( prompt ) {
450                 prompt->lock_window();
451                 prompt->set_done(1);
452                 prompt->unlock_window();
453         }
454 }
455
456 void PluginServer::render_stop()
457 {
458         if( client )
459                 client->render_stop();
460         send_reset_gui_frames();
461 }
462
463 void PluginServer::write_table(FILE *fp, const char *path, int idx, int64_t mtime)
464 {
465         if( !fp ) return;
466         fprintf(fp, "%d \"%s\" \"%s\" %jd %d %d %d %d %d %d %d %d %d %d %d\n",
467                 plugin_type, path, title, mtime, idx, audio, video, theme, realtime,
468                 fileio, uses_gui, multichannel, synthesis, transition, lad_index);
469 }
470
471 int PluginServer::scan_table(char *text, int &type, char *path, char *title, int64_t &mtime)
472 {
473         int n = sscanf(text, "%d \"%[^\"]\" \"%[^\"]\" %jd", &type, path, title, &mtime);
474         return n < 4 ? 1 : 0;
475 }
476
477 int PluginServer::read_table(char *text)
478 {
479         char path[BCTEXTLEN], title[BCTEXTLEN];
480         int64_t mtime;
481         int n = sscanf(text, "%d \"%[^\"]\" \"%[^\"]\" %jd %d %d %d %d %d %d %d %d %d %d %d",
482                 &plugin_type, path, title, &mtime, &dir_idx, &audio, &video, &theme, &realtime,
483                 &fileio, &uses_gui, &multichannel, &synthesis, &transition, &lad_index);
484         if( n != 15 ) return 1;
485         set_title(title);
486         return 0;
487 }
488
489 int PluginServer::init_realtime(int realtime_sched,
490                 int total_in_buffers,
491                 int buffer_size)
492 {
493
494         if( !plugin_open ) return 0;
495
496 // set for realtime priority
497 // initialize plugin
498 // Call start_realtime
499         this->total_in_buffers = this->total_out_buffers = total_in_buffers;
500         client->plugin_init_realtime(realtime_sched,
501                 total_in_buffers,
502                 buffer_size);
503
504         return 0;
505 }
506
507
508 // Replaced by pull method but still needed for transitions
509 void PluginServer::process_transition(VFrame *input,
510                 VFrame *output,
511                 int64_t current_position,
512                 int64_t total_len)
513 {
514         if( !plugin_open ) return;
515         PluginVClient *vclient = (PluginVClient*)client;
516
517         vclient->source_position = current_position;
518         vclient->source_start = 0;
519         vclient->total_len = total_len;
520
521         vclient->input = new VFrame*[1];
522         vclient->output = new VFrame*[1];
523
524         vclient->input[0] = input;
525         vclient->output[0] = output;
526
527         vclient->process_realtime(input, output);
528         vclient->age_temp();
529         delete [] vclient->input;
530         delete [] vclient->output;
531         use_opengl = 0;
532 }
533
534 void PluginServer::process_transition(Samples *input,
535                 Samples *output,
536                 int64_t current_position,
537                 int64_t fragment_size,
538                 int64_t total_len)
539 {
540         if( !plugin_open ) return;
541         PluginAClient *aclient = (PluginAClient*)client;
542
543         aclient->source_position = current_position;
544         aclient->total_len = total_len;
545         aclient->source_start = 0;
546         aclient->process_realtime(fragment_size,
547                 input,
548                 output);
549 }
550
551
552 void PluginServer::process_buffer(VFrame **frame,
553         int64_t current_position,
554         double frame_rate,
555         int64_t total_len,
556         int direction)
557 {
558         if( !plugin_open ) return;
559         PluginVClient *vclient = (PluginVClient*)client;
560         vclient->in_buffer_size = vclient->out_buffer_size = 1;
561         vclient->source_position = current_position;
562         vclient->total_len = total_len;
563         vclient->frame_rate = frame_rate;
564         vclient->input = new VFrame*[total_in_buffers];
565         vclient->output = new VFrame*[total_in_buffers];
566         for( int i = 0; i < total_in_buffers; i++ ) {
567                 vclient->input[i] = frame[i];
568                 vclient->output[i] = frame[i];
569         }
570
571         if( plugin ) {
572                 vclient->source_start = (int64_t)plugin->startproject *
573                         frame_rate / vclient->project_frame_rate;
574         }
575         vclient->direction = direction;
576
577 //PRINT_TRACE
578 //printf("plugin=%p source_start=%ld\n", plugin, vclient->source_start);
579 //      vclient->begin_process_buffer();
580         if( multichannel )
581                 vclient->process_buffer(frame, current_position, frame_rate);
582         else
583                 vclient->process_buffer(frame[0], current_position, frame_rate);
584 //      vclient->end_process_buffer();
585
586         for( int i = 0; i < total_in_buffers; i++ )
587                 frame[i]->push_prev_effect(title);
588
589         delete [] vclient->input;
590         delete [] vclient->output;
591
592         vclient->age_temp();
593         use_opengl = 0;
594 }
595
596 void PluginServer::process_buffer(Samples **buffer,
597         int64_t current_position,
598         int64_t fragment_size,
599         int64_t sample_rate,
600         int64_t total_len,
601         int direction)
602 {
603         if( !plugin_open ) return;
604         PluginAClient *aclient = (PluginAClient*)client;
605         aclient->source_position = current_position;
606         aclient->total_len = total_len;
607         aclient->sample_rate = sample_rate;
608         aclient->in_buffer_size = aclient->out_buffer_size = fragment_size;
609         aclient->output_buffers = buffer;
610
611         if( plugin )
612                 aclient->source_start = plugin->startproject *
613                         sample_rate /
614                         aclient->project_sample_rate;
615
616         aclient->direction = direction;
617         aclient->begin_process_buffer();
618         if( multichannel ) {
619                 aclient->process_buffer(fragment_size,
620                         buffer, current_position, sample_rate);
621         }
622         else {
623                 aclient->process_buffer(fragment_size,
624                         buffer[0], current_position, sample_rate);
625         }
626         aclient->end_process_buffer();
627 }
628
629
630 void PluginServer::send_reset_gui_frames()
631 {
632         if( !attachmentpoint ) return;
633         attachmentpoint->reset_gui_frames(this);
634 }
635
636 void PluginServer::send_render_gui(void *data)
637 {
638         if( !attachmentpoint ) return;
639         attachmentpoint->render_gui(data, this);
640 }
641
642 void PluginServer::send_render_gui(void *data, int size)
643 {
644         if( !attachmentpoint ) return;
645         attachmentpoint->render_gui(data, size, this);
646 }
647
648 void PluginServer::render_gui(void *data)
649 {
650         if( !client ) return;
651         client->plugin_render_gui(data);
652 }
653
654 void PluginServer::render_gui(void *data, int size)
655 {
656         if( !client ) return;
657         client->plugin_render_gui(data, size);
658 }
659
660
661 PluginGUIs::PluginGUIs(MWindow *mwindow)
662 {
663         this->mwindow = mwindow;
664         this->next_id = 0;
665 }
666 PluginGUIs::~PluginGUIs()
667 {
668 }
669
670 void PluginGUIs::append(PluginServer *server)
671 {
672         server->gui_id = next_id++;
673         ArrayList<PluginServer*>::append(server);
674 }
675
676 PluginServer *PluginGUIs::gui_server(int gui_id)
677 {
678         for( int i=0; i<size(); ++i ) {
679                 PluginServer *plugin_server = get(i);
680                 if( plugin_server->gui_id == gui_id )
681                         return plugin_server;
682         }
683         return 0;
684 }
685
686
687 void PluginServer::reset_gui_frames()
688 {
689         mwindow->reset_plugin_gui_frames(plugin);
690 }
691
692 void PluginServer::reset_plugin_gui_frames()
693 {
694         if( !client ) return;
695         client->plugin_reset_gui_frames();
696 }
697
698 void PluginServer::render_gui_frames(PluginClientFrames *frames)
699 {
700         mwindow->render_plugin_gui_frames(frames, plugin);
701 }
702
703 void PluginServer::render_plugin_gui_frames(PluginClientFrames *frames)
704 {
705         if( !client ) return;
706         client->plugin_render_gui_frames(frames);
707 }
708
709 MainProgressBar* PluginServer::start_progress(char *string, int64_t length)
710 {
711         mwindow->gui->lock_window();
712         MainProgressBar *result = mwindow->mainprogress->start_progress(string, length);
713         mwindow->gui->unlock_window();
714         return result;
715 }
716
717 int64_t PluginServer::get_written_samples()
718 {
719         if( !plugin_open ) return 0;
720         return written_samples;
721 }
722
723 int64_t PluginServer::get_written_frames()
724 {
725         if( !plugin_open ) return 0;
726         return written_frames;
727 }
728
729
730 // ======================= Non-realtime plugin
731
732 int PluginServer::get_parameters(int64_t start, int64_t end, int channels)
733 {
734         if( !plugin_open ) return 0;
735
736         client->start = start;
737         client->end = end;
738         client->source_start = start;
739         client->total_len = end - start;
740         client->total_in_buffers = channels;
741
742 //PRINT_TRACE
743 //printf(" source_start=%ld total_len=%ld\n", client->source_start, client->total_len);
744
745         return client->plugin_get_parameters();
746 }
747
748 int PluginServer::set_interactive()
749 {
750         if( !plugin_open ) return 0;
751         client->set_interactive();
752         return 0;
753 }
754
755 void PluginServer::append_module(Module *module)
756 {
757         modules->append(module);
758 }
759
760 void PluginServer::append_node(VirtualNode *node)
761 {
762         nodes->append(node);
763 }
764
765 void PluginServer::reset_nodes()
766 {
767         nodes->remove_all();
768 }
769
770 int PluginServer::set_error()
771 {
772         error_flag = 1;
773         return 0;
774 }
775
776 int PluginServer::set_realtime_sched()
777 {
778         //struct sched_param params;
779         //params.sched_priority = 1;
780         //sched_setscheduler(0, SCHED_RR, &param);
781         return 0;
782 }
783
784
785 int PluginServer::process_loop(VFrame **buffers, int64_t &write_length)
786 {
787         if( !plugin_open ) return 1;
788         return client->plugin_process_loop(buffers, write_length);
789 }
790
791 int PluginServer::process_loop(Samples **buffers, int64_t &write_length)
792 {
793         if( !plugin_open ) return 1;
794         return client->plugin_process_loop(buffers, write_length);
795 }
796
797
798 int PluginServer::start_loop(int64_t start,
799         int64_t end,
800         int64_t buffer_size,
801         int total_buffers)
802 {
803         if( !plugin_open ) return 0;
804         client->plugin_start_loop(start, end, buffer_size, total_buffers);
805         return 0;
806 }
807
808 int PluginServer::stop_loop()
809 {
810         if( !plugin_open ) return 0;
811         return client->plugin_stop_loop();
812 }
813
814 int PluginServer::read_frame(VFrame *buffer,
815         int channel,
816         int64_t start_position)
817 {
818         ((VModule*)modules->values[channel])->render(buffer,
819                 start_position, PLAY_FORWARD,
820                 mwindow->edl->session->frame_rate, 0, 0);
821         return 0;
822 }
823
824 int PluginServer::read_samples(Samples *buffer,
825         int channel, int64_t sample_rate, int64_t start_position, int64_t len)
826 {
827 // len is now in buffer
828         if( !multichannel ) channel = 0;
829
830         if( nodes->total > channel )
831                 return ((VirtualANode*)nodes->values[channel])->read_data(buffer,
832                         len, start_position, sample_rate);
833         if( modules->total > channel )
834                 return ((AModule*)modules->values[channel])->render(buffer,
835                         len, start_position, PLAY_FORWARD, sample_rate, 0);
836         printf("PluginServer::read_samples no object available for channel=%d\n",
837                 channel);
838         return -1;
839 }
840
841
842 int PluginServer::read_samples(Samples *buffer,
843                 int channel, int64_t start_position, int64_t size)
844 {
845 // total_samples is now set in buffer
846         ((AModule*)modules->values[channel])->render(buffer,
847                 size, start_position, PLAY_FORWARD,
848                 mwindow->edl->session->sample_rate, 0);
849         return 0;
850 }
851
852 int PluginServer::read_frame(VFrame *buffer,
853                 int channel, int64_t start_position, double frame_rate,
854                 int use_opengl)
855 {
856 // Data source depends on whether we're part of a virtual console or a
857 // plugin array.
858 //     VirtualNode
859 //     Module
860 // If we're a VirtualNode, read_data in the virtual plugin node handles
861 //     backward propogation and produces the data.
862 // If we're a Module, render in the module produces the data.
863 //PRINT_TRACE
864
865         int result = -1;
866         if( !multichannel ) channel = 0;
867
868 // Push our name on the next effect stack
869         buffer->push_next_effect(title);
870 //printf("PluginServer::read_frame %p\n", buffer);
871 //buffer->dump_stacks();
872
873         if( nodes->total > channel ) {
874 //printf("PluginServer::read_frame %d\n", __LINE__);
875                 result = ((VirtualVNode*)nodes->values[channel])->read_data(buffer,
876                         start_position, frame_rate, use_opengl);
877         }
878         else
879         if( modules->total > channel ) {
880 //printf("PluginServer::read_frame %d\n", __LINE__);
881                 result = ((VModule*)modules->values[channel])->render(buffer,
882                         start_position, //                      PLAY_FORWARD,
883                         client->direction, frame_rate, 0, 0, use_opengl);
884         }
885         else {
886                 printf("PluginServer::read_frame no object available for channel=%d\n",
887                         channel);
888         }
889
890 // Pop our name from the next effect stack
891         buffer->pop_next_effect();
892
893         return result;
894 }
895
896
897 // Called by client
898 int PluginServer::get_gui_status()
899 {
900         if( plugin )
901                 return plugin->show ? GUI_ON : GUI_OFF;
902         else
903                 return GUI_OFF;
904 }
905
906 void PluginServer::raise_window()
907 {
908         if( !plugin_open ) return;
909         client->raise_window();
910 }
911
912 void PluginServer::show_gui()
913 {
914         if( !plugin_open ) return;
915         if( plugin ) {
916                 plugin->gui_id = gui_id;
917                 client->total_len = plugin->length;
918                 client->source_start = plugin->startproject;
919         }
920         if( video ) {
921                 client->source_position = Units::to_int64(
922                         mwindow->edl->local_session->get_selectionstart(1) *
923                                 mwindow->edl->session->frame_rate);
924         }
925         else
926         if( audio ) {
927                 client->source_position = Units::to_int64(
928                         mwindow->edl->local_session->get_selectionstart(1) *
929                                 mwindow->edl->session->sample_rate);
930         }
931
932         client->update_display_title();
933         client->show_gui();
934 }
935
936 void PluginServer::hide_gui()
937 {
938         if( !plugin_open ) return;
939         if( plugin ) plugin->gui_id = -1;
940         if( client->defaults ) client->save_defaults();
941         client->hide_gui();
942 }
943
944 void PluginServer::update_gui()
945 {
946         if( !plugin_open || !plugin ) return;
947
948         client->total_len = plugin->length;
949         client->source_start = plugin->startproject;
950
951         if( video ) {
952                 client->source_position = Units::to_int64(
953                         mwindow->edl->local_session->get_selectionstart(1) *
954                                 mwindow->edl->session->frame_rate);
955         }
956         else if( audio ) {
957                 client->source_position = Units::to_int64(
958                         mwindow->edl->local_session->get_selectionstart(1) *
959                                 mwindow->edl->session->sample_rate);
960         }
961
962         client->plugin_update_gui();
963 }
964
965 void PluginServer::update_title()
966 {
967         if( !plugin_open ) return;
968
969         client->update_display_title();
970 }
971
972
973 int PluginServer::set_string(char *string)
974 {
975         if( !plugin_open ) return 0;
976
977         client->set_string_client(string);
978         return 0;
979 }
980
981 int PluginServer::gui_open()
982 {
983         if( attachmentpoint ) return attachmentpoint->gui_open();
984         return 0;
985 }
986
987 void PluginServer::set_use_opengl(int value, VideoDevice *vdevice)
988 {
989         this->use_opengl = value;
990         this->vdevice = vdevice;
991 }
992
993 int PluginServer::get_use_opengl()
994 {
995         return use_opengl;
996 }
997
998
999 void PluginServer::run_opengl(PluginClient *plugin_client)
1000 {
1001         if( vdevice )
1002                 ((VDeviceX11*)vdevice->get_output_base())->run_plugin(plugin_client);
1003 }
1004
1005 // ============================= queries
1006
1007 void PluginServer::get_defaults_path(char *path)
1008 {
1009 // Get plugin name from path
1010         char *ptr1 = strrchr(get_path(), '/');
1011         char *ptr2 = strrchr(get_path(), '.');
1012         if( !ptr1 ) ptr1 = get_path();
1013         if( !ptr2 ) ptr2 = get_path() + strlen(get_path());
1014         char string2[BCTEXTLEN], *ptr3 = string2;
1015         while( ptr1 < ptr2 ) *ptr3++ = *ptr1++;
1016         *ptr3 = 0;
1017         sprintf(path, "%s/%s.xml", File::get_config_path(), string2);
1018 }
1019
1020 void PluginServer::save_defaults()
1021 {
1022         if( client ) client->save_defaults();
1023 }
1024
1025 int PluginServer::get_samplerate()
1026 {
1027         if( !plugin_open ) return 0;
1028         if( audio )
1029                 return client->get_samplerate();
1030         if( mwindow )
1031                 return mwindow->edl->session->sample_rate;
1032         printf("PluginServer::get_samplerate audio and mwindow == NULL\n");
1033         return 1;
1034 }
1035
1036
1037 double PluginServer::get_framerate()
1038 {
1039         if( !plugin_open ) return 0;
1040         if( video )
1041                 return client->get_framerate();
1042         if( mwindow )
1043                 return mwindow->edl->session->frame_rate;
1044         printf("PluginServer::get_framerate video and mwindow == NULL\n");
1045         return 1;
1046 }
1047
1048 int PluginServer::get_project_samplerate()
1049 {
1050         if( mwindow )
1051                 return mwindow->edl->session->sample_rate;
1052         if( edl )
1053                 return edl->session->sample_rate;
1054         printf("PluginServer::get_project_samplerate mwindow and edl are NULL.\n");
1055         return 1;
1056 }
1057
1058 double PluginServer::get_project_framerate()
1059 {
1060         if( mwindow )
1061                 return mwindow->edl->session->frame_rate;
1062         if( edl )
1063                 return edl->session->frame_rate;
1064         printf("PluginServer::get_project_framerate mwindow and edl are NULL.\n");
1065         return 1;
1066 }
1067
1068
1069 int PluginServer::detach_buffers()
1070 {
1071         ring_buffers_out.remove_all();
1072         offset_out_render.remove_all();
1073         double_buffer_out_render.remove_all();
1074         realtime_out_size.remove_all();
1075
1076         ring_buffers_in.remove_all();
1077         offset_in_render.remove_all();
1078         double_buffer_in_render.remove_all();
1079         realtime_in_size.remove_all();
1080
1081         out_buffer_size = 0;
1082         shared_buffers = 0;
1083         total_out_buffers = 0;
1084         in_buffer_size = 0;
1085         total_in_buffers = 0;
1086         return 0;
1087 }
1088
1089 int PluginServer::arm_buffer(int buffer_number,
1090                 int64_t offset_in, int64_t offset_out,
1091                 int double_buffer_in, int double_buffer_out)
1092 {
1093         offset_in_render.values[buffer_number] = offset_in;
1094         offset_out_render.values[buffer_number] = offset_out;
1095         double_buffer_in_render.values[buffer_number] = double_buffer_in;
1096         double_buffer_out_render.values[buffer_number] = double_buffer_out;
1097         return 0;
1098 }
1099
1100
1101 int PluginServer::set_automation(FloatAutos *autos, FloatAuto **start_auto, FloatAuto **end_auto, int reverse)
1102 {
1103         this->autos = autos;
1104         this->start_auto = start_auto;
1105         this->end_auto = end_auto;
1106         this->reverse = reverse;
1107         return 0;
1108 }
1109
1110
1111
1112 void PluginServer::save_data(KeyFrame *keyframe)
1113 {
1114         if( !plugin_open ) return;
1115         client->save_data(keyframe);
1116 }
1117
1118 KeyFrame* PluginServer::get_prev_keyframe(int64_t position)
1119 {
1120         KeyFrame *result = 0;
1121         if( plugin )
1122                 result = plugin->get_prev_keyframe(position, client->direction);
1123         else
1124                 result = keyframe;
1125         return result;
1126 }
1127
1128 KeyFrame* PluginServer::get_next_keyframe(int64_t position)
1129 {
1130         KeyFrame *result = !plugin ? 0 :
1131                 plugin->get_next_keyframe(position, client->direction);
1132         return result;
1133 }
1134
1135 // Called for
1136 KeyFrame* PluginServer::get_keyframe()
1137 {
1138         if( plugin )
1139 // Realtime plugin case
1140                 return plugin->get_keyframe();
1141 // Rendered plugin case
1142         return keyframe;
1143 }
1144
1145
1146 void PluginServer::apply_keyframe(KeyFrame *src)
1147 {
1148         if( !plugin )
1149                 keyframe->copy_data(src);
1150         else
1151 // Span keyframes
1152                 plugin->keyframes->update_parameter(src);
1153 }
1154
1155
1156 void PluginServer::get_camera(float *x, float *y, float *z,
1157         int64_t position, int direction)
1158 {
1159         plugin->track->automation->get_camera(x, y, z, position, direction);
1160 }
1161
1162 void PluginServer::get_projector(float *x, float *y, float *z,
1163         int64_t position, int direction)
1164 {
1165         plugin->track->automation->get_projector(x, y, z, position, direction);
1166 }
1167
1168
1169 int PluginServer::get_interpolation_type()
1170 {
1171         return plugin->edl->session->interpolation_type;
1172 }
1173
1174 Theme* PluginServer::new_theme()
1175 {
1176         if( theme )
1177                 return client->new_theme();
1178         return 0;
1179 }
1180
1181 Theme* PluginServer::get_theme()
1182 {
1183         if( mwindow ) return mwindow->theme;
1184         printf("PluginServer::get_theme mwindow not set\n");
1185         return 0;
1186 }
1187
1188
1189 void PluginServer::get_plugin_png_name(char *png_name)
1190 {
1191         char *pp = png_name, *ep = pp + BCSTRLEN-1;
1192         char *cp = strrchr(path, '/');
1193         cp = !cp ? path : cp+1;
1194         char *sp = strrchr(cp, '.');
1195         if( !sp ) sp = cp+strlen(cp);
1196         while( pp < ep && cp < sp ) *pp++ = *cp++;
1197         snprintf(pp, ep-pp, ".png");
1198 }
1199
1200 int PluginServer::get_plugin_png_path(char *png_path, const char *plugin_icons)
1201 {
1202         char png_name[BCSTRLEN];
1203         get_plugin_png_name(png_name);
1204         char *pp = png_path, *ep = pp + BCTEXTLEN-1;
1205         pp += snprintf(pp, ep-pp, "%s/picon/%s/%s",
1206                 File::get_plugin_path(), plugin_icons, png_name);
1207         return access(png_path,R_OK) ? 1 : 0;
1208 }
1209
1210 int PluginServer::get_plugin_png_path(char *png_path)
1211 {
1212         int ret = get_plugin_png_path(png_path, mwindow->preferences->plugin_icons);
1213         if( ret ) ret = get_plugin_png_path(png_path, DEFAULT_PICON);
1214         return ret;
1215 }
1216
1217 VFrame *PluginServer::get_plugin_images()
1218 {
1219         char png_path[BCTEXTLEN];
1220         if( !get_plugin_png_path(png_path) )
1221                 return VFramePng::vframe_png(png_path,0,0);
1222         char png_name[BCSTRLEN];
1223         get_plugin_png_name(png_name);
1224         unsigned char *data = mwindow->theme->get_image_data(png_name, 0);
1225         return data ? new VFramePng(data, 0.) : 0;
1226 }
1227
1228 VFrame *PluginServer::get_picon()
1229 {
1230         if( !picon )
1231                 picon = get_plugin_images();
1232         return picon;
1233 }
1234
1235 // Called when plugin interface is tweeked
1236 void PluginServer::sync_parameters()
1237 {
1238         if( video ) mwindow->restart_brender();
1239         mwindow->sync_parameters();
1240         mwindow->update_keyframe_guis();
1241         if( mwindow->edl->session->auto_conf->plugins ) {
1242                 mwindow->gui->lock_window("PluginServer::sync_parameters");
1243                 mwindow->gui->draw_overlays(1);
1244                 mwindow->gui->unlock_window();
1245         }
1246 }
1247
1248 int64_t PluginServer::get_startproject()
1249 {
1250         return !plugin ? -1 : plugin->startproject;
1251 }
1252 int64_t PluginServer::get_endproject()
1253 {
1254         return !plugin ? -1 : plugin->startproject + plugin->length;
1255 }
1256
1257 void PluginServer::dump(FILE *fp)
1258 {
1259         fprintf(fp,"    PluginServer %d %p %s %s %d\n",
1260                 __LINE__, this, path, title, realtime);
1261 }