4 * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #ifndef PLUGINCLIENT_H
23 #define PLUGINCLIENT_H
25 // Base class inherited by all the different types of plugins.
27 #define MAX_FRAME_BUFFER 8192
32 #include "arraylist.h"
35 #include "condition.h"
36 #include "edlsession.inc"
38 #include "mainprogress.inc"
39 #include "maxbuffers.h"
40 #include "pluginclient.inc"
41 #include "plugincommands.h"
42 #include "pluginserver.inc"
43 #include "samples.inc"
50 extern PluginClient* new_plugin(PluginServer *server);
53 class PluginClientAuto
64 // Convenience functions
66 #define REGISTER_PLUGIN(class_title) \
67 PluginClient* new_plugin(PluginServer *server) \
69 return new class_title(server); \
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(); \
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(); \
99 #define NEW_WINDOW_MACRO(plugin_class, window_class) \
100 PluginClientWindow* plugin_class::new_window() \
102 return new window_class(this); \
105 // Not all plugins load configuration the same way. Use this to define
107 #define LOAD_CONFIGURATION_MACRO(plugin_class, config_class) \
108 int plugin_class::load_configuration() \
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); \
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()); \
129 return !config.equivalent(old_config) ? 1 : 0; \
133 class PluginClientFrame : public ListItem<PluginClientFrame>
137 virtual ~PluginClientFrame();
138 // offset in EDL seconds for synchronizing with GUI
142 class PluginClientFrames : public List<PluginClientFrame>
145 PluginClientFrames();
146 ~PluginClientFrames();
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);
154 void add_gui_frame(PluginClientFrame *frame);
155 void concatenate(PluginClientFrames *frames);
156 PluginClientFrame *get_gui_frame(double pos, int dir);
162 class PluginClientWindow : public BC_Window
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();
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();
177 PluginClient *client;
181 class PluginFPot : public BC_FPot
184 PluginFPot(PluginParam *param, int x, int y);
189 class PluginIPot : public BC_IPot
192 PluginIPot(PluginParam *param, int x, int y);
197 class PluginQPot : public BC_QPot
200 PluginQPot(PluginParam *param, int x, int y);
205 class PluginText : public BC_TextBox
208 PluginText(PluginParam *param, int x, int y, int value);
209 PluginText(PluginParam *param, int x, int y, float value);
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);
225 void update(int skip_text, int skip_pot);
226 // set the number of fractional digits
227 void set_precision(int digits);
239 PluginClientWindow *gui;
240 PluginClient *plugin;
248 class PluginClientThread : public Thread
251 PluginClientThread(PluginClient *client);
252 ~PluginClientThread();
255 friend class PluginClient;
257 BC_WindowBase* get_window();
258 PluginClient* get_client();
259 PluginClientWindow *window;
260 PluginClient *client;
263 Condition *init_complete;
271 PluginClient(PluginServer *server);
272 virtual ~PluginClient();
274 friend class PluginClientThread;
275 friend class PluginClientWindow;
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.
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();
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.
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();
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
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.
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();
351 // Used by plugins which need to know where they are.
353 // Get the directory for plugins
354 char* get_plugin_dir();
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);
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);
378 // Called from process_buffer
379 // Returns 1 if a GUI is open so OpenGL routines can determine if
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();
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();
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);
399 // Convert the EDL position to the local position.
400 virtual int64_t edl_to_local(int64_t position);
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();
411 // Get the direction of the most recent process_buffer
414 // position and direction for plugin gui tracking draws
415 double get_tracking_position();
416 int get_tracking_direction();
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);
423 // Get total tracks to process
424 int get_total_buffers();
426 // Get size of buffer to fill in non-realtime plugin
427 int get_buffer_size();
429 // Get interpolation used by EDL from overlayframe.inc
430 int get_interpolation_type();
432 // Get the values from the color picker
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; };
445 // All plugins define these.
446 PluginClientThread* get_thread();
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();
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);
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();
471 // Realtime operations.
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);
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();
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);
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);
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();
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
511 // ================================= Buffers ===============================
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;
526 // ================================= Automation ===========================
528 ArrayList<PluginClientAuto> automation;
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
535 int show_initially; // set to show a realtime plugin initially
536 // range in project for processing
538 int interactive; // for the progress bar plugin
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.
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;
553 // Direction of most recent process_buffer
556 // Operating system scheduling
557 int realtime_priority;
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.
568 // Total number of processors available - 1
570 PluginServer *server;
572 PluginClientThread *thread;
574 // Frames for updating GUI
575 PluginClientFrames client_frames;
576 // Time of last GUI update
582 // Temporaries set in new_window
583 int window_x, window_y;
585 // Asset *asset; // Point to asset structure in shared memory