improve delays created by vicon drawing locks, reset_cache segv fix, gang track toolt...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / pluginclient.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2011 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 #ifndef PLUGINCLIENT_H
23 #define PLUGINCLIENT_H
24
25 // Base class inherited by all the different types of plugins.
26
27 #define MAX_FRAME_BUFFER 8192
28
29 class PluginClient;
30
31
32 #include "arraylist.h"
33 #include "linklist.h"
34 #include "bchash.inc"
35 #include "condition.h"
36 #include "edlsession.inc"
37 #include "keyframe.h"
38 #include "mainprogress.inc"
39 #include "maxbuffers.h"
40 #include "pluginclient.inc"
41 #include "plugincommands.h"
42 #include "pluginserver.inc"
43 #include "samples.inc"
44 #include "theme.inc"
45 #include "vframe.h"
46
47
48 extern "C"
49 {
50 extern PluginClient* new_plugin(PluginServer *server);
51 }
52
53 class PluginClientAuto
54 {
55 public:
56         float position;
57         float intercept;
58         float slope;
59 };
60
61
62
63
64 // Convenience functions
65
66 #define REGISTER_PLUGIN(class_title) \
67 PluginClient* new_plugin(PluginServer *server) \
68 { \
69         return new class_title(server); \
70 }
71
72
73
74
75
76
77
78
79 // Prototypes for user to put in class header
80 #define PLUGIN_CLASS_MEMBERS(config_name) \
81         int load_configuration(); \
82         const char* plugin_title(); \
83         PluginClientWindow* new_window(); \
84         config_name config;
85
86
87
88 // Prototypes for user to put in class header
89 #define PLUGIN_CLASS_MEMBERS2(config_name) \
90         int load_configuration(); \
91         const char* plugin_title(); \
92         PluginClientWindow* new_window(); \
93         config_name config;
94
95
96
97
98
99 #define NEW_WINDOW_MACRO(plugin_class, window_class) \
100 PluginClientWindow* plugin_class::new_window() \
101 { \
102         return new window_class(this); \
103 }
104
105 // Not all plugins load configuration the same way.  Use this to define
106 // the normal way.
107 #define LOAD_CONFIGURATION_MACRO(plugin_class, config_class) \
108 int plugin_class::load_configuration() \
109 { \
110         KeyFrame *prev_keyframe, *next_keyframe; \
111         prev_keyframe = get_prev_keyframe(get_source_position()); \
112         next_keyframe = get_next_keyframe(get_source_position()); \
113  \
114         int64_t next_position = edl_to_local(next_keyframe->position); \
115         int64_t prev_position = edl_to_local(prev_keyframe->position); \
116  \
117         config_class old_config, prev_config, next_config; \
118         old_config.copy_from(config); \
119         read_data(prev_keyframe); \
120         prev_config.copy_from(config); \
121         read_data(next_keyframe); \
122         next_config.copy_from(config); \
123  \
124         config.interpolate(prev_config,  \
125                 next_config,  \
126                 (next_position == prev_position) ? \
127                         get_source_position() : \
128                         prev_position, \
129                 (next_position == prev_position) ? \
130                         get_source_position() + 1 : \
131                         next_position, \
132                 get_source_position()); \
133  \
134         if(!config.equivalent(old_config)) \
135                 return 1; \
136         else \
137                 return 0; \
138 }
139
140
141 class PluginClientFrame : public ListItem<PluginClientFrame>
142 {
143 public:
144         PluginClientFrame();
145         virtual ~PluginClientFrame();
146 // offset in EDL seconds for synchronizing with GUI
147         double position;
148 };
149
150 class PluginClientFrames : public List<PluginClientFrame>
151 {
152 public:
153         PluginClientFrames();
154         ~PluginClientFrames();
155
156         static int fwd_cmpr(PluginClientFrame *ap, PluginClientFrame *bp);
157         static int rev_cmpr(PluginClientFrame *ap, PluginClientFrame *bp);
158         void fwd_sort() { sort(fwd_cmpr); }
159         void rev_sort() { sort(rev_cmpr); }
160         void sort_position(int dir);
161         void reset();
162         void add_gui_frame(PluginClientFrame *frame);
163         void concatenate(PluginClientFrames *frames);
164         PluginClientFrame *get_gui_frame(double pos, int dir);
165
166         int count;
167 };
168
169
170 class PluginClientWindow : public BC_Window
171 {
172 public:
173         PluginClientWindow(PluginClient *client,
174                 int w, int h, int min_w, int min_h, int allow_resize);
175         PluginClientWindow(const char *title, int x, int y,
176                 int w, int h, int min_w, int min_h, int allow_resize);
177         virtual ~PluginClientWindow();
178
179         virtual int translation_event();
180         virtual int close_event();
181         virtual void done_event(int result) {}
182 // A listener for PluginParam events
183         virtual void param_updated();
184
185         PluginClient *client;
186 };
187
188 // A GUI helper
189 class PluginFPot : public BC_FPot
190 {
191 public:
192         PluginFPot(PluginParam *param, int x, int y);
193         int handle_event();
194                 PluginParam *param;
195 };
196
197 class PluginIPot : public BC_IPot
198 {
199 public:
200         PluginIPot(PluginParam *param, int x, int y);
201         int handle_event();
202                 PluginParam *param;
203 };
204
205 class PluginQPot : public BC_QPot
206 {
207 public:
208         PluginQPot(PluginParam *param, int x, int y);
209         int handle_event();
210                 PluginParam *param;
211 };
212
213 class PluginText : public BC_TextBox
214 {
215 public:
216         PluginText(PluginParam *param, int x, int y, int value);
217         PluginText(PluginParam *param, int x, int y, float value);
218         int handle_event();
219                 PluginParam *param;
220 };
221
222 class PluginParam
223 {
224 public:
225         PluginParam(PluginClient *plugin, PluginClientWindow *gui,
226                 int x1, int x2, int x3, int y, int text_w,
227                 int *output_i, float *output_f, // floating point output
228                 int *output_q, // frequency output
229                 const char *title, float min, float max);
230         ~PluginParam();
231
232         void initialize();
233         void update(int skip_text, int skip_pot);
234 // set the number of fractional digits
235         void set_precision(int digits);
236
237 // possible outputs
238         float *output_f;
239         PluginFPot *fpot;
240         int *output_i;
241         PluginIPot *ipot;
242         int *output_q;
243         PluginQPot *qpot;
244
245         char *title;
246         PluginText *text;
247         PluginClientWindow *gui;
248         PluginClient *plugin;
249         int x1, x2, x3;
250         int y, text_w;
251         float min, max;
252         int precision;
253 };
254
255
256 class PluginClientThread : public Thread
257 {
258 public:
259         PluginClientThread(PluginClient *client);
260         ~PluginClientThread();
261         void run();
262
263         friend class PluginClient;
264
265         BC_WindowBase* get_window();
266         PluginClient* get_client();
267         PluginClientWindow *window;
268         PluginClient *client;
269
270 private:
271         Condition *init_complete;
272 };
273
274
275
276 class PluginClient
277 {
278 public:
279         PluginClient(PluginServer *server);
280         virtual ~PluginClient();
281
282         friend class PluginClientThread;
283         friend class PluginClientWindow;
284
285 // Queries for the plugin server.
286         virtual int is_realtime();
287         virtual int is_audio();
288         virtual int is_video();
289         virtual int is_fileio();
290         virtual int is_theme();
291         virtual int uses_gui();
292         virtual int is_multichannel();
293         virtual int is_synthesis();
294         virtual int is_transition();
295         virtual const char* plugin_title();   // return the title of the plugin
296         virtual Theme* new_theme();
297 // Get theme being used by Cinelerra currently.  Used by all plugins.
298         Theme* get_theme();
299
300 // Non realtime signal processors define these.
301 // Give the samplerate of the output for a non realtime plugin.
302 // For realtime plugins give the requested samplerate.
303         virtual int get_samplerate();
304 // Give the framerate of the output for a non realtime plugin.
305 // For realtime plugins give the requested framerate.
306         virtual double get_framerate();
307         virtual int delete_nonrealtime_parameters();
308         virtual int get_parameters();    // get information from user before non realtime processing
309         virtual int64_t get_in_buffers(int64_t recommended_size);  // return desired size for input buffers
310         virtual int64_t get_out_buffers(int64_t recommended_size);       // return desired size for output buffers
311         virtual int start_loop();
312         virtual int process_loop();
313         virtual int stop_loop();
314 // Hash files are the defaults for rendered plugins
315         virtual int load_defaults();       // load default settings for the plugin
316         virtual int save_defaults();      // save the current settings as defaults
317         BC_Hash* get_defaults();
318
319 // Realtime commands for signal processors.
320 // These must be defined by the plugin itself.
321 // Set the GUI title identifying the plugin to modules and patches.
322         int set_string();
323 // cause the plugin to create a new GUI class
324         virtual BC_WindowBase* new_window();
325 // Load the current keyframe.  Return 1 if it changed.
326         virtual int load_configuration();
327 // cause the plugin to hide the gui
328         void client_side_close();
329         void update_display_title();
330 // Raise the GUI
331         void raise_window();
332 // Create GUI
333         int show_gui();
334 // XML keyframes are the defaults for realtime plugins
335         void load_defaults_xml();
336         void save_defaults_xml();
337 // Tell the client if the load is the defaults
338         int is_defaults();
339
340         virtual void update_gui();
341         virtual void save_data(KeyFrame *keyframe) {};  // write the plugin settings to text in text format
342         virtual void read_data(KeyFrame *keyframe) {};  // read the plugin settings from the text
343         int send_hide_gui();                                                                    // should be sent when the GUI receives a close event from the user
344 // Destroys the window but not the thread pointer.
345         void hide_gui();
346         void plugin_update_gui();
347         virtual void begin_process_buffer() {}
348         virtual void end_process_buffer() {}
349         virtual int plugin_process_loop(VFrame **buffers, int64_t &write_length) { return 1; };
350         virtual int plugin_process_loop(Samples **buffers, int64_t &write_length) { return 1; };
351 // get parameters depending on video or audio
352         virtual int init_realtime_parameters();
353 // release objects which are required after playback stops
354         virtual void render_stop() {};
355         int get_gui_status();
356         char* get_gui_string();
357
358 // Used by themes
359 // Used by plugins which need to know where they are.
360         char* get_path();
361 // Get the directory for plugins
362         char* get_plugin_dir();
363
364 // Return keyframe objects.  The position in the resulting object
365 // is relative to the EDL rate.  This is the only way to avoid copying the
366 // data for every frame.
367 // If the result is the default keyframe, the keyframe's position is 0.
368 // position - relative to EDL rate or local rate to allow simple
369 //       passing of get_source_position.
370 //       If -1 the tracking position in the edl is used.
371 // is_local - if 1, the position is converted to the EDL rate.
372         KeyFrame* get_prev_keyframe(int64_t position, int is_local = 1);
373         KeyFrame* get_next_keyframe(int64_t position, int is_local = 1);
374 // get current camera and projector position
375         void get_camera(float *x, float *y, float *z, int64_t position);
376         void get_projector(float *x, float *y, float *z, int64_t position);
377
378         void output_to_track(float ox, float oy, float &tx, float &ty);
379         void track_to_output(float tx, float ty, float &ox, float &oy);
380 // When this plugin is adjusted, propogate parameters back to EDL and virtual
381 // console.  This gets a keyframe from the EDL, with the position set to the
382 // EDL tracking position.
383         int send_configure_change();
384         virtual void span_keyframes(KeyFrame *src, int64_t start, int64_t end);
385
386 // Called from process_buffer
387 // Returns 1 if a GUI is open so OpenGL routines can determine if
388 // they can run.
389         int gui_open();
390
391
392 // Length of source.  For effects it's the plugin length.  For transitions
393 // it's the transition length.  Relative to the requested rate.
394 // The only way to get smooth interpolation is to make all position queries
395 // relative to the requested rate.
396         int64_t get_total_len();
397
398 // For realtime plugins gets the lowest sample of the plugin in the requested
399 // rate.  For others it's the start of the EDL selection in the EDL rate.
400         int64_t get_source_start();
401
402 // Convert the position relative to the requested rate to the position
403 // relative to the EDL rate.  If the argument is < 0, it is not changed.
404 // Used for interpreting keyframes.
405         virtual int64_t local_to_edl(int64_t position);
406
407 // Convert the EDL position to the local position.
408         virtual int64_t edl_to_local(int64_t position);
409
410 // For transitions the source_position is the playback position relative
411 // to the start of the transition.
412 // For realtime effects, the start of the most recent process_buffer in forward
413 // and the end of the range to process in reverse.  Relative to start of EDL in
414 // the requested rate.
415         int64_t get_source_position();
416
417 // Get server edl
418         EDL *get_edl();
419 // Get the direction of the most recent process_buffer
420         int get_direction();
421
422 // position and direction for plugin gui tracking draws
423         double get_tracking_position();
424         int get_tracking_direction();
425
426 // Plugin must call this before performing OpenGL operations.
427 // Returns 1 if the user supports opengl buffers.
428         int get_use_opengl();
429         int to_ram(VFrame *vframe);
430
431 // Get total tracks to process
432         int get_total_buffers();
433
434 // Get size of buffer to fill in non-realtime plugin
435         int get_buffer_size();
436
437 // Get interpolation used by EDL from overlayframe.inc
438         int get_interpolation_type();
439
440 // Get the values from the color picker
441         float get_red();
442         float get_green();
443         float get_blue();
444
445 // Operations for file handlers
446         virtual int open_file() { return 0; };
447         virtual int get_audio_parameters() { return 0; };
448         virtual int get_video_parameters() { return 0; };
449         virtual int check_header(char *path) { return 0; };
450         virtual int open_file(char *path, int wr, int rd) { return 1; };
451         virtual int close_file() { return 0; };
452
453 // All plugins define these.
454         PluginClientThread* get_thread();
455
456 // Non realtime operations for signal processors.
457         virtual int plugin_start_loop(int64_t start, int64_t end,
458                 int64_t buffer_size, int total_buffers);
459         int plugin_stop_loop();
460         int plugin_process_loop();
461         MainProgressBar* start_progress(char *string, int64_t length);
462 // get samplerate of EDL
463         int get_project_samplerate();
464 // get framerate of EDL
465         double get_project_framerate();
466 // get asset path
467         const char *get_source_path();
468 // Total number of processors - 1
469         int get_project_smp();
470         int get_aspect_ratio(float &aspect_w, float &aspect_h);
471
472
473         int write_frames(int64_t total_frames);  // returns 1 for failure / tells the server that all output channel buffers are ready to go
474         int write_samples(int64_t total_samples);  // returns 1 for failure / tells the server that all output channel buffers are ready to go
475         virtual int plugin_get_parameters();
476         const char* get_defaultdir();    // Directory defaults should be stored in
477         void set_interactive();
478
479 // Realtime operations.
480         int reset();
481 // Extension of plugin_run for derived plugins
482         virtual int plugin_command_derived(int plugin_command) { return 0; };
483         int plugin_get_range();
484         int plugin_init_realtime(int realtime_priority,
485                 int total_in_buffers, int buffer_size);
486
487 // GUI updating wrappers for realtime plugins
488 // Append frame to queue for next send_frame_buffer
489         void add_gui_frame(PluginClientFrame *frame);
490         int get_gui_frames();
491
492         virtual void render_gui(void *data);
493         virtual void render_gui(void *data, int size);
494         void send_render_gui();
495         void send_render_gui(void *data);
496         void send_render_gui(void *data, int size);
497         void plugin_render_gui(void *data);
498         void plugin_render_gui(void *data, int size);
499
500         void reset_gui_frames();
501         void reset_plugin_gui_frames();
502         void plugin_reset_gui_frames();
503         void plugin_render_gui_frames(PluginClientFrames *frames);
504 // first frame ready
505         int pending_gui_frame();
506 // Called by client to get the total number of frames to draw in update_gui
507         int pending_gui_frames();
508 // pop frames until buffer passes position=pos(-1 or seconds) in direction=dir(-1,0,1)
509         PluginClientFrame *get_gui_frame(double pos, int dir);
510         PluginClientFrame* next_gui_frame();
511
512 // communication convenience routines for the base class
513         int stop_gui_client();
514         int save_data_client();
515         int load_data_client();
516         int set_string_client(char *string);                            // set the string identifying the plugin
517         int send_cancelled();           // non realtime plugin sends when cancelled
518
519 // ================================= Buffers ===============================
520
521 // number of double buffers for each channel
522         ArrayList<int> double_buffers_in;
523         ArrayList<int> double_buffers_out;
524 // When arming buffers need to know the offsets in all the buffers and which
525 // double buffers for each channel before rendering.
526         ArrayList<int64_t> offset_in_render;
527         ArrayList<int64_t> offset_out_render;
528         ArrayList<int64_t> double_buffer_in_render;
529         ArrayList<int64_t> double_buffer_out_render;
530 // total size of each buffer depends on if it's a master or node
531         ArrayList<int64_t> realtime_in_size;
532         ArrayList<int64_t> realtime_out_size;
533
534 // ================================= Automation ===========================
535
536         ArrayList<PluginClientAuto> automation;
537
538 // ================================== Messages ===========================
539         char gui_string[BCTEXTLEN];               // string identifying module and plugin
540         int master_gui_on;                        // Status of the master gui plugin
541         int client_gui_on;                        // Status of this client's gui
542
543         int show_initially;                      // set to show a realtime plugin initially
544 // range in project for processing
545         int64_t start, end;
546         int interactive;                                // for the progress bar plugin
547         int success;
548         int total_out_buffers;            // total send buffers allocated by the server
549         int total_in_buffers;              // total receive buffers allocated by the server
550         int wr, rd;                                      // File permissions for fileio plugins.
551
552 // These give the largest fragment the plugin is expected to handle.
553 // size of a send buffer to the server
554         int64_t out_buffer_size;
555 // size of a receive buffer from the server
556         int64_t in_buffer_size;
557
558
559
560
561 // Direction of most recent process_buffer
562         int direction;
563
564 // Operating system scheduling
565         int realtime_priority;
566
567 // Position relative to start of EDL in requested rate.  Calculated for every process
568 // command.  Used for keyframes.
569         int64_t source_position;
570 // For realtime plugins gets the lowest sample of the plugin in the requested
571 // rate.  For others it's always 0.
572         int64_t source_start;
573 // Length of source.  For effects it's the plugin length.  For transitions
574 // it's the transition length.  Relative to the requested rate.
575         int64_t total_len;
576 // Total number of processors available - 1
577         int smp;
578         PluginServer *server;
579         BC_Hash *defaults;
580         PluginClientThread *thread;
581
582 // Frames for updating GUI
583         PluginClientFrames client_frames;
584 // Time of last GUI update
585         Timer *update_timer;
586
587
588 private:
589         int using_defaults;
590 // Temporaries set in new_window
591         int window_x, window_y;
592 // File handlers:
593 //      Asset *asset;    // Point to asset structure in shared memory
594 };
595
596
597 #endif