Credit Andrew - improve in-tree documentation
[goodguy/cinelerra.git] / cinelerra / module.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 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 #include "attachmentpoint.h"
23 #include "bcsignals.h"
24 #include "cache.h"
25 #include "commonrender.h"
26 #include "edl.h"
27 #include "edlsession.h"
28 #include "filexml.h"
29 #include "module.h"
30 #include "mwindow.h"
31 #include "patch.h"
32 #include "patchbay.h"
33 #include "plugin.h"
34 #include "pluginarray.h"
35 #include "pluginserver.h"
36 #include "renderengine.h"
37 #include "sharedlocation.h"
38 #include "track.h"
39 #include "tracks.h"
40 #include "transportque.h"
41 #include "virtualconsole.h"
42
43
44 Module::Module(RenderEngine *renderengine,
45         CommonRender *commonrender,
46         PluginArray *plugin_array,
47         Track *track)
48 {
49         this->renderengine = renderengine;
50         this->commonrender = commonrender;
51         this->plugin_array = plugin_array;
52         this->track = track;
53         transition = 0;
54         transition_server = 0;
55         attachments = 0;
56         total_attachments = 0;
57         new_total_attachments = 0;
58         new_attachments = 0;
59         nested_edl = 0;
60         nested_renderengine = 0;
61         nested_command = 0;
62         private_cache = 0;
63         cache = 0;
64 }
65
66 Module::~Module()
67 {
68         if(attachments)
69         {
70                 for(int i = 0; i < track->plugin_set.total; i++)
71                 {
72                         if(attachments[i])
73                         {
74 // For some reason it isn't used here.
75 //                              attachments[i]->render_stop(0);
76                                 delete attachments[i];
77                         }
78                 }
79                 delete [] attachments;
80         }
81         if(transition_server)
82         {
83                 transition_server->close_plugin();
84                 delete transition_server;
85         }
86
87         delete nested_renderengine;
88         delete nested_command;
89         if(private_cache) delete cache;
90 }
91
92 void Module::create_objects()
93 {
94         create_new_attachments();
95         swap_attachments();
96 }
97
98 EDL* Module::get_edl()
99 {
100         return renderengine ? renderengine->get_edl() : edl;
101 }
102
103 Preferences* Module::get_preferences()
104 {
105         if( renderengine ) return renderengine->preferences;
106         if( plugin_array ) return plugin_array->mwindow->preferences;
107         return 0;
108 }
109
110
111 void Module::create_new_attachments()
112 {
113 // Not used in pluginarray
114         if(commonrender)
115         {
116                 new_total_attachments = track->plugin_set.size();
117                 if(new_total_attachments)
118                 {
119                         new_attachments = new AttachmentPoint*[new_total_attachments];
120                         for(int i = 0; i < new_total_attachments; i++)
121                         {
122                                 Plugin *plugin =
123                                         track->get_current_plugin(commonrender->current_position,
124                                                 i,
125                                                 renderengine->command->get_direction(),
126                                                 0,
127                                                 1);
128
129                                 if(plugin && plugin->plugin_type != PLUGIN_NONE && plugin->on)
130                                 {
131                                         new_attachments[i] = new_attachment(plugin);
132 // printf("Module::create_new_attachments %d new_attachment=%p\n",
133 // __LINE__,
134 // new_attachments[i]->virtual_plugins.values);
135                                 }
136                                 else
137                                 {
138                                         new_attachments[i] = 0;
139                                 }
140                         }
141                 }
142                 else
143                         new_attachments = 0;
144
145 // Create plugin servers in virtual console expansion
146         }
147 }
148
149 void Module::swap_attachments()
150 {
151 // for(int i = 0; i < total_attachments; i++)
152 // printf("Module::swap_attachments %d attachment=%p\n", __LINE__, attachments[i] ? attachments[i]->virtual_plugins.values : 0);
153 // for(int i = 0; i < new_total_attachments; i++)
154 // printf("Module::swap_attachments %d new_attachment=%p\n", __LINE__, new_attachments[i] ? new_attachments[i]->virtual_plugins.values : 0);
155
156 // None of this is used in a pluginarray
157         for(int i = 0;
158                 i < new_total_attachments &&
159                 i < total_attachments;
160                 i++)
161         {
162 // Delete new attachment which is identical to the old one and copy
163 // old attachment.
164                 if(new_attachments[i] &&
165                         attachments[i] &&
166                         new_attachments[i]->identical(attachments[i]))
167                 {
168 // printf("Module::swap_attachments %d virtual_plugins=%p new_virtual_plugins=%p\n",
169 // __LINE__,
170 // new_attachments[i]->virtual_plugins.values,
171 // new_attachments[i]->new_virtual_plugins.values);
172
173                         delete new_attachments[i];
174                         new_attachments[i] = attachments[i];
175                         attachments[i] = 0;
176                 }
177         }
178
179 // Delete old attachments which weren't identical to new ones
180         for(int i = 0; i < total_attachments; i++)
181         {
182                 if(attachments[i]) delete attachments[i];
183         }
184
185         if(attachments)
186         {
187                 delete [] attachments;
188         }
189
190         attachments = new_attachments;
191         total_attachments = new_total_attachments;
192
193         new_attachments = 0;
194         new_total_attachments = 0;
195
196 // for(int i = 0; i < total_attachments; i++)
197 // printf("Module::swap_attachments %d final_attachment=%p\n", __LINE__, attachments[i] ? attachments[i]->virtual_plugins.values : 0);
198 }
199
200 int Module::render_init()
201 {
202         for(int i = 0; i < total_attachments; i++)
203         {
204                 if(attachments[i])
205                         attachments[i]->render_init();
206         }
207
208         return 0;
209 }
210
211 void Module::render_stop()
212 {
213         for(int i = 0; i < total_attachments; i++)
214         {
215                 if(attachments[i])
216                         attachments[i]->render_stop();
217         }
218 }
219
220 AttachmentPoint* Module::attachment_of(Plugin *plugin)
221 {
222 //printf("Module::attachment_of 1 %d\n", total_attachments);
223         for(int i = 0; i < total_attachments; i++)
224         {
225 //printf("Module::attachment_of 2 %p\n", attachments[i]);
226                 if(attachments[i] &&
227                         attachments[i]->plugin == plugin) return attachments[i];
228         }
229         return 0;
230 }
231
232 AttachmentPoint* Module::get_attachment(int number)
233 {
234         if(number < total_attachments)
235                 return attachments[number];
236         else
237                 return 0;
238 }
239
240 void Module::reset_attachments()
241 {
242 //printf("Module::reset_attachments 1 %d\n", total_attachments);
243         for(int i = 0; i < total_attachments; i++)
244         {
245 //printf("Module::reset_attachments 2 %p\n", attachments[i]);
246                 AttachmentPoint *attachment = attachments[i];
247                 if(attachment) attachment->reset_status();
248         }
249 }
250
251 // Test plugins for reconfiguration.
252 // Used in playback
253 int Module::test_plugins()
254 {
255         if(total_attachments != track->plugin_set.total) return 1;
256
257         for(int i = 0; i < total_attachments; i++)
258         {
259                 AttachmentPoint *attachment = attachments[i];
260                 Plugin *plugin = track->get_current_plugin(
261                         commonrender->current_position,
262                         i,
263                         renderengine->command->get_direction(),
264                         0,
265                         1);
266 // One exists and one doesn't
267                 int use_plugin = plugin &&
268                         plugin->plugin_type != PLUGIN_NONE &&
269                         plugin->on;
270
271                 if((attachment && !use_plugin) ||
272                         (!attachment && use_plugin)) return 1;
273
274 // Plugin not the same
275                 if(plugin &&
276                         attachment &&
277                         attachment->plugin &&
278                         !plugin->identical(attachment->plugin)) return 1;
279         }
280
281         return 0;
282 }
283
284 void Module::update_transition(int64_t current_position,
285         int direction)
286 {
287         transition = track->get_current_transition(current_position,
288                 direction, 0, 0);
289
290 // For situations where we had a transition but not anymore,
291 // keep the server open.
292 // Maybe the same transition will follow and we won't need to reinit.
293 // (happens a lot while scrubbing over transitions left and right)
294
295
296 // If the current transition differs from the previous transition, delete the
297 // server.
298         if (transition && transition_server) {
299                 if (strcmp(transition->title, transition_server->plugin->title)) {
300                         transition_server->close_plugin();
301                         delete transition_server;
302                         transition_server = 0;
303                 }
304                 else {
305                         transition_server->plugin = transition;
306                 }
307         }
308
309         if(transition && !transition_server) {
310                 if(renderengine) {
311                         PluginServer *plugin_server = MWindow::scan_plugindb(transition->title,
312                                 track->data_type);
313                         transition_server = new PluginServer(*plugin_server);
314                         transition_server->open_plugin(0,
315                                 get_preferences(),
316                                 get_edl(),
317                                 transition);
318                         transition_server->init_realtime(
319                                 get_edl()->session->real_time_playback &&
320                                 renderengine->command->realtime,
321                                 1,
322                                 get_buffer_size());
323                 }
324                 else
325                 if(plugin_array) {
326                         PluginServer *plugin_server = MWindow::scan_plugindb(transition->title,
327                                 plugin_array->data_type);
328                         transition_server = new PluginServer(*plugin_server);
329                         transition_server->open_plugin(0,
330                                 get_preferences(),
331                                 get_edl(),
332                                 transition);
333                         transition_server->init_realtime(
334                                 0,
335                                 1,
336                                 get_buffer_size());
337                 }
338         }
339 }
340
341
342 void Module::dump()
343 {
344         printf("  Module title=%s\n", track->title);
345         printf("   Plugins total_attachments=%d\n", total_attachments);
346         for(int i = 0; i < total_attachments; i++)
347         {
348                 attachments[i]->dump();
349         }
350 }
351
352
353
354
355
356