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