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