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