r885: Don't delete a borrowed frame.
[cinelerra_cv/ct.git] / cinelerra / pluginclient.h
blob40f3a5be40a6dd6f9cde7e55141159731e0ed9bc
1 #ifndef PLUGINCLIENT_H
2 #define PLUGINCLIENT_H
4 // Base class inherited by all the different types of plugins.
6 #define BCASTDIR "~/.bcast/"
8 class PluginClient;
11 #include "arraylist.h"
12 #include "condition.h"
13 #include "edlsession.inc"
14 #include "keyframe.h"
15 #include "mainprogress.inc"
16 #include "maxbuffers.h"
17 #include "plugincommands.h"
18 #include "pluginserver.inc"
19 #include "theme.inc"
20 #include "vframe.h"
23 extern "C"
25 extern PluginClient* new_plugin(PluginServer *server);
28 class PluginClientAuto
30 public:
31 float position;
32 float intercept;
33 float slope;
39 // Convenience functions
41 #define REGISTER_PLUGIN(class_title) \
42 PluginClient* new_plugin(PluginServer *server) \
43 { \
44 return new class_title(server); \
48 #define WINDOW_CLOSE_EVENT(window_class) \
49 int window_class::close_event() \
50 { \
51 /* Set result to 1 to indicate a client side close */ \
52 set_done(1); \
53 return 1; \
57 #define PLUGIN_THREAD_HEADER(plugin_class, thread_class, window_class) \
58 class thread_class : public Thread \
59 { \
60 public: \
61 thread_class(plugin_class *plugin); \
62 ~thread_class(); \
63 void run(); \
64 window_class *window; \
65 plugin_class *plugin; \
69 #define PLUGIN_THREAD_OBJECT(plugin_class, thread_class, window_class) \
70 thread_class::thread_class(plugin_class *plugin) \
71 : Thread(0, 0, 1) \
72 { \
73 this->plugin = plugin; \
74 } \
76 thread_class::~thread_class() \
77 { \
78 delete window; \
79 } \
81 void thread_class::run() \
82 { \
83 BC_DisplayInfo info; \
84 window = new window_class(plugin, \
85 info.get_abs_cursor_x() - 75, \
86 info.get_abs_cursor_y() - 65); \
87 window->create_objects(); \
89 /* Only set it here so tracking doesn't update it until everything is created. */ \
90 plugin->thread = this; \
91 int result = window->run_window(); \
92 /* This is needed when the GUI is closed from itself */ \
93 if(result) plugin->client_side_close(); \
99 #define PLUGIN_CLASS_MEMBERS(config_name, thread_name) \
100 int load_configuration(); \
101 VFrame* new_picon(); \
102 char* plugin_title(); \
103 int show_gui(); \
104 int set_string(); \
105 void raise_window(); \
106 BC_Hash *defaults; \
107 config_name config; \
108 thread_name *thread;
110 #define PLUGIN_CONSTRUCTOR_MACRO \
111 thread = 0; \
112 defaults = 0; \
113 load_defaults(); \
115 #define PLUGIN_DESTRUCTOR_MACRO \
116 if(thread) \
118 /* This is needed when the GUI is closed from elsewhere than itself */ \
119 /* Since we now use autodelete, this is all that has to be done, thread will take care of itself ... */ \
120 /* Thread join will wait if this was not called from the thread itself or go on if it was */ \
121 thread->window->lock_window("PLUGIN_DESTRUCTOR_MACRO"); \
122 thread->window->set_done(0); \
123 thread->window->unlock_window(); \
124 thread->join(); \
128 if(defaults) save_defaults(); \
129 if(defaults) delete defaults;
134 #define SHOW_GUI_MACRO(plugin_class, thread_class) \
135 int plugin_class::show_gui() \
137 load_configuration(); \
138 thread_class *new_thread = new thread_class(this); \
139 new_thread->start(); \
140 return 0; \
143 #define RAISE_WINDOW_MACRO(plugin_class) \
144 void plugin_class::raise_window() \
146 if(thread) \
148 thread->window->lock_window(); \
149 thread->window->raise_window(); \
150 thread->window->flush(); \
151 thread->window->unlock_window(); \
155 #define SET_STRING_MACRO(plugin_class) \
156 int plugin_class::set_string() \
158 if(thread) \
160 thread->window->lock_window(); \
161 thread->window->set_title(gui_string); \
162 thread->window->unlock_window(); \
164 return 0; \
167 #define NEW_PICON_MACRO(plugin_class) \
168 VFrame* plugin_class::new_picon() \
170 return new VFrame(picon_png); \
173 #define LOAD_CONFIGURATION_MACRO(plugin_class, config_class) \
174 int plugin_class::load_configuration() \
176 KeyFrame *prev_keyframe, *next_keyframe; \
177 prev_keyframe = get_prev_keyframe(get_source_position()); \
178 next_keyframe = get_next_keyframe(get_source_position()); \
180 int64_t next_position = edl_to_local(next_keyframe->position); \
181 int64_t prev_position = edl_to_local(prev_keyframe->position); \
183 config_class old_config, prev_config, next_config; \
184 old_config.copy_from(config); \
185 read_data(prev_keyframe); \
186 prev_config.copy_from(config); \
187 read_data(next_keyframe); \
188 next_config.copy_from(config); \
190 config.interpolate(prev_config, \
191 next_config, \
192 (next_position == prev_position) ? \
193 get_source_position() : \
194 prev_position, \
195 (next_position == prev_position) ? \
196 get_source_position() + 1 : \
197 next_position, \
198 get_source_position()); \
200 if(!config.equivalent(old_config)) \
201 return 1; \
202 else \
203 return 0; \
211 class PluginClient
213 public:
214 PluginClient(PluginServer *server);
215 virtual ~PluginClient();
218 // Queries for the plugin server.
219 virtual int is_realtime();
220 virtual int is_audio();
221 virtual int is_video();
222 virtual int is_fileio();
223 virtual int is_theme();
224 virtual int uses_gui();
225 virtual int is_multichannel();
226 virtual int is_synthesis();
227 virtual int is_transition();
228 virtual char* plugin_title(); // return the title of the plugin
229 virtual VFrame* new_picon();
230 virtual Theme* new_theme();
231 // Get theme being used by Cinelerra currently. Used by all plugins.
232 Theme* get_theme();
239 // Non realtime signal processors define these.
240 // Give the samplerate of the output for a non realtime plugin.
241 // For realtime plugins give the requested samplerate.
242 virtual int get_samplerate();
243 // Give the framerate of the output for a non realtime plugin.
244 // For realtime plugins give the requested framerate.
245 virtual double get_framerate();
246 virtual int delete_nonrealtime_parameters();
247 virtual int start_plugin(); // run a non realtime plugin
248 virtual int get_parameters(); // get information from user before non realtime processing
249 virtual int64_t get_in_buffers(int64_t recommended_size); // return desired size for input buffers
250 virtual int64_t get_out_buffers(int64_t recommended_size); // return desired size for output buffers
251 virtual int start_loop();
252 virtual int process_loop();
253 virtual int stop_loop();
258 // Realtime commands for signal processors.
259 // These must be defined by the plugin itself.
260 virtual int set_string(); // Set the string identifying the plugin to modules and patches.
261 // cause the plugin to show the gui
262 virtual int show_gui();
263 // cause the plugin to hide the gui
264 void client_side_close();
265 void update_display_title();
266 // Raise the GUI
267 virtual void raise_window() {};
268 virtual void update_gui() {};
269 virtual void save_data(KeyFrame *keyframe) {}; // write the plugin settings to text in text format
270 virtual void read_data(KeyFrame *keyframe) {}; // read the plugin settings from the text
271 int send_hide_gui(); // should be sent when the GUI recieves a close event from the user
273 int get_configure_change(); // get propogated configuration change from a send_configure_change
275 // Called by plugin server to update GUI with rendered data.
276 virtual void plugin_render_gui(void *data) {};
277 virtual void plugin_render_gui(void *data, int size) {};
278 virtual int plugin_process_loop(VFrame **buffers, int64_t &write_length) { return 1; };
279 virtual int plugin_process_loop(double **buffers, int64_t &write_length) { return 1; };
280 // get parameters depending on video or audio
281 virtual int init_realtime_parameters();
282 // release objects which are required after playback stops
283 virtual void render_stop() {};
284 int get_gui_status();
285 char* get_gui_string();
287 // Used by themes
288 // Used by plugins which need to know where they are.
289 char* get_path();
291 // Return keyframe objects. The position in the resulting object
292 // is relative to the EDL rate. This is the only way to avoid copying the
293 // data for every frame.
294 // If the result is the default keyframe, the keyframe's position is 0.
295 // position - relative to EDL rate or local rate to allow simple
296 // passing of get_source_position.
297 // If -1 the tracking position in the edl is used.
298 // is_local - if 1, the position is converted to the EDL rate.
299 KeyFrame* get_prev_keyframe(int64_t position, int is_local = 1);
300 KeyFrame* get_next_keyframe(int64_t position, int is_local = 1);
301 // get current camera and projector position
302 void get_camera(float *x, float *y, float *z, int64_t position);
303 void get_projector(float *x, float *y, float *z, int64_t position);
304 // When this plugin is adjusted, propogate parameters back to EDL and virtual
305 // console. This gets a keyframe from the EDL, with the position set to the
306 // EDL tracking position.
307 int send_configure_change();
310 // Called from process_buffer
311 // Returns 1 if a GUI is open so OpenGL routines can determine if
312 // they can run.
313 int gui_open();
317 // Length of source. For effects it's the plugin length. For transitions
318 // it's the transition length. Relative to the requested rate.
319 // The only way to get smooth interpolation is to make all position queries
320 // relative to the requested rate.
321 int64_t get_total_len();
323 // For realtime plugins gets the lowest sample of the plugin in the requested
324 // rate. For others it's the start of the EDL selection in the EDL rate.
325 int64_t get_source_start();
327 // Convert the position relative to the requested rate to the position
328 // relative to the EDL rate. If the argument is < 0, it is not changed.
329 // Used for interpreting keyframes.
330 virtual int64_t local_to_edl(int64_t position);
332 // Convert the EDL position to the local position.
333 virtual int64_t edl_to_local(int64_t position);
335 // For transitions the source_position is the playback position relative
336 // to the start of the transition.
337 // For realtime effects, the start of the most recent process_buffer in forward
338 // and the end of the range to process in reverse. Relative to start of EDL in
339 // the requested rate.
340 int64_t get_source_position();
342 // Get the EDL Session. May return 0 if the server has no edl.
343 EDLSession* get_edlsession();
346 // Get the direction of the most recent process_buffer
347 int get_direction();
349 // Plugin must call this before performing OpenGL operations.
350 // Returns 1 if the user supports opengl buffers.
351 int get_use_opengl();
353 // Get total tracks to process
354 int get_total_buffers();
356 // Get size of buffer to fill in non-realtime plugin
357 int get_buffer_size();
359 // Get interpolation used by EDL from overlayframe.inc
360 int get_interpolation_type();
362 // Get the values from the color picker
363 float get_red();
364 float get_green();
365 float get_blue();
369 // Operations for file handlers
370 virtual int open_file() { return 0; };
371 virtual int get_audio_parameters() { return 0; };
372 virtual int get_video_parameters() { return 0; };
373 virtual int check_header(char *path) { return 0; };
374 virtual int open_file(char *path, int wr, int rd) { return 1; };
375 virtual int close_file() { return 0; };
381 // All plugins define these.
382 virtual int load_defaults(); // load default settings for the plugin
383 virtual int save_defaults(); // save the current settings as defaults
388 // Non realtime operations for signal processors.
389 virtual int plugin_start_loop(int64_t start,
390 int64_t end,
391 int64_t buffer_size,
392 int total_buffers);
393 int plugin_stop_loop();
394 int plugin_process_loop();
395 MainProgressBar* start_progress(char *string, int64_t length);
396 // get samplerate of EDL
397 int get_project_samplerate();
398 // get framerate of EDL
399 double get_project_framerate();
400 // Total number of processors - 1
401 int get_project_smp();
402 int get_aspect_ratio(float &aspect_w, float &aspect_h);
405 int write_frames(int64_t total_frames); // returns 1 for failure / tells the server that all output channel buffers are ready to go
406 int write_samples(int64_t total_samples); // returns 1 for failure / tells the server that all output channel buffers are ready to go
407 virtual int plugin_get_parameters();
408 char* get_defaultdir(); // Directory defaults should be stored in
409 void set_interactive();
411 // Realtime operations.
412 int reset();
413 virtual int plugin_command_derived(int plugin_command) {}; // Extension of plugin_run for derived plugins
414 int plugin_get_range();
415 int plugin_init_realtime(int realtime_priority,
416 int total_in_buffers,
417 int buffer_size);
421 // create pointers to buffers of the plugin's type before realtime rendering
422 virtual int delete_buffer_ptrs();
427 // communication convenience routines for the base class
428 int stop_gui_client();
429 int save_data_client();
430 int load_data_client();
431 int set_string_client(char *string); // set the string identifying the plugin
432 int send_cancelled(); // non realtime plugin sends when cancelled
434 // ================================= Buffers ===============================
436 // number of double buffers for each channel
437 ArrayList<int> double_buffers_in;
438 ArrayList<int> double_buffers_out;
439 // When arming buffers need to know the offsets in all the buffers and which
440 // double buffers for each channel before rendering.
441 ArrayList<int64_t> offset_in_render;
442 ArrayList<int64_t> offset_out_render;
443 ArrayList<int64_t> double_buffer_in_render;
444 ArrayList<int64_t> double_buffer_out_render;
445 // total size of each buffer depends on if it's a master or node
446 ArrayList<int64_t> realtime_in_size;
447 ArrayList<int64_t> realtime_out_size;
449 // ================================= Automation ===========================
451 ArrayList<PluginClientAuto> automation;
453 // ================================== Messages ===========================
454 char gui_string[BCTEXTLEN]; // string identifying module and plugin
455 int master_gui_on; // Status of the master gui plugin
456 int client_gui_on; // Status of this client's gui
458 int show_initially; // set to show a realtime plugin initially
459 // range in project for processing
460 int64_t start, end;
461 int interactive; // for the progress bar plugin
462 int success;
463 int total_out_buffers; // total send buffers allocated by the server
464 int total_in_buffers; // total recieve buffers allocated by the server
465 int wr, rd; // File permissions for fileio plugins.
467 // These give the largest fragment the plugin is expected to handle.
468 // size of a send buffer to the server
469 int64_t out_buffer_size;
470 // size of a recieve buffer from the server
471 int64_t in_buffer_size;
476 // Direction of most recent process_buffer
477 int direction;
479 // Operating system scheduling
480 int realtime_priority;
482 // Position relative to start of EDL in requested rate. Calculated for every process
483 // command. Used for keyframes.
484 int64_t source_position;
485 // For realtime plugins gets the lowest sample of the plugin in the requested
486 // rate. For others it's always 0.
487 int64_t source_start;
488 // Length of source. For effects it's the plugin length. For transitions
489 // it's the transition length. Relative to the requested rate.
490 int64_t total_len;
491 // Total number of processors available - 1
492 int smp;
493 PluginServer *server;
495 private:
497 // File handlers:
498 // Asset *asset; // Point to asset structure in shared memory
502 #endif