r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / module.C
blob12fdc978a5d18e99ddd4f29cbf9a05997bde0e4f
1 #include "attachmentpoint.h"
2 #include "commonrender.h"
3 #include "edl.h"
4 #include "edlsession.h"
5 #include "filexml.h"
6 #include "module.h"
7 #include "patch.h"
8 #include "patchbay.h"
9 #include "plugin.h"
10 #include "pluginarray.h"
11 #include "pluginserver.h"
12 #include "renderengine.h"
13 #include "sharedlocation.h"
14 #include "track.h"
15 #include "tracks.h"
16 #include "transportque.h"
19 Module::Module(RenderEngine *renderengine, 
20         CommonRender *commonrender, 
21         PluginArray *plugin_array,
22         Track *track)
24         this->renderengine = renderengine;
25         this->commonrender = commonrender;
26         this->plugin_array = plugin_array;
27         this->track = track;
28         transition = 0;
29         transition_server = 0;
30         attachments = 0;
31         total_attachments = 0;
32         new_total_attachments = 0;
33         new_attachments = 0;
36 Module::~Module()
38         if(attachments)
39         {
40                 for(int i = 0; i < track->plugin_set.total; i++)
41                 {
42                         if(attachments[i])
43                         {
44 // For some reason it isn't used here.
45 //                              attachments[i]->render_stop(0);
46                                 delete attachments[i];
47                         }
48                 }
49                 delete [] attachments;
50         }
51         if(transition_server)
52         {
53                 transition_server->close_plugin();
54                 delete transition_server;
55         }
58 void Module::create_objects()
60         create_new_attachments();
61         swap_attachments();
64 EDL* Module::get_edl()
66         if(renderengine) 
67                 return renderengine->edl;
68         else
69                 return edl;
72 void Module::create_new_attachments()
74 // Not used in pluginarray
75         if(commonrender)
76         {
77                 new_total_attachments = track->plugin_set.total;
78                 if(new_total_attachments)
79                 {
80                         new_attachments = new AttachmentPoint*[new_total_attachments];
81                         for(int i = 0; i < new_total_attachments; i++)
82                         {
83                                 Plugin *plugin = 
84                                         track->get_current_plugin(commonrender->current_position, 
85                                                 i, 
86                                                 renderengine->command->get_direction(),
87                                                 0);
89                                 if(plugin && plugin->plugin_type != PLUGIN_NONE && plugin->on)
90                                         new_attachments[i] = new_attachment(plugin);
91                                 else
92                                         new_attachments[i] = 0;
93                         }
94                 }
95                 else
96                         new_attachments = 0;
98 // Create plugin servers later when nodes attach
99         }
102 void Module::swap_attachments()
104 // None of this is used in a pluginarray
105         for(int new_attachment = 0, old_attachment = 0; 
106                 new_attachment < new_total_attachments &&
107                 old_attachment < total_attachments; 
108                 new_attachment++, old_attachment++)
109         {
110 // Copy any old attachment which is identical to a new one
111                 if(new_attachments[new_attachment] &&
112                         attachments[old_attachment] &&
113                         new_attachments[new_attachment]->plugin ==
114                         attachments[old_attachment]->plugin)
115                 {
116                         delete new_attachments[new_attachment];
117                         new_attachments[new_attachment] = attachments[old_attachment];
118                         attachments[old_attachment] = 0;
119                 }
120         }
122 // Delete old attachments which weren't identical to new ones
123         for(int i = 0; i < total_attachments; i++)
124         {
125                 if(attachments[i]) delete attachments[i];
126         }
128         if(attachments)
129         {
130                 delete [] attachments;
131         }
132         
133         attachments = new_attachments;
134         total_attachments = new_total_attachments;
137 int Module::render_init()
139         for(int i = 0; i < total_attachments; i++)
140         {
141                 if(attachments[i])
142                         attachments[i]->render_init();
143         }
145         return 0;
148 AttachmentPoint* Module::attachment_of(Plugin *plugin)
150 //printf("Module::attachment_of 1 %d\n", total_attachments);
151         for(int i = 0; i < total_attachments; i++)
152         {
153 //printf("Module::attachment_of 2 %p\n", attachments[i]);
154                 if(attachments[i] && 
155                         attachments[i]->plugin == plugin) return attachments[i];
156         }
157         return 0;
161 // Test plugins for reconfiguration.
162 // Used in playback
163 int Module::test_plugins()
165         if(total_attachments != track->plugin_set.total) return 1;
167         for(int i = 0; i < total_attachments; i++)
168         {
169                 AttachmentPoint *attachment = attachments[i];
170                 Plugin *plugin = track->get_current_plugin(commonrender->current_position, 
171                         i, 
172                         renderengine->command->get_direction(),
173                         0);
174 // One exists and one doesn't
175                 int use_plugin = plugin &&
176                         plugin->plugin_type != PLUGIN_NONE &&
177                         plugin->on;
179                 if((attachment && !use_plugin) || 
180                         (!attachment && use_plugin)) return 1;
182 // Plugin not the same
183                 if(plugin && 
184                         attachment &&
185                         attachment->plugin && 
186                         !plugin->identical(attachment->plugin)) return 1;
187         }
189         return 0;
192 void Module::update_transition(int64_t current_position, int direction)
194 //printf("Module::update_transition 1\n");
195         Plugin *transition = track->get_current_transition(current_position, 
196                 direction,
197                 0);
198 //printf("Module::update_transition 2 %p\n", transition);
200         if((!transition && this->transition) || 
201                 (transition && this->transition && strcmp(transition->title, this->transition->title)))
202         {
203                 this->transition = 0;
205                 transition_server->close_plugin();
206                 delete transition_server;
207                 transition_server = 0;
208         }
209 //printf("Module::update_transition 7\n");
211         if(transition && !this->transition)
212         {
213                 this->transition = transition;
214 //printf("Module::update_transition 7\n");
215                 if(renderengine)
216                 {
217                         PluginServer *plugin_server = renderengine->scan_plugindb(transition->title);
218                         transition_server = new PluginServer(*plugin_server);
219                         transition_server->open_plugin(0, get_edl(), transition);
220                         transition_server->init_realtime(
221                                 get_edl()->session->real_time_playback &&
222                                 renderengine->command->realtime,
223                                 1,
224                                 get_buffer_size());
225                 }
226                 else
227                 if(plugin_array)
228                 {
229                         PluginServer *plugin_server = plugin_array->scan_plugindb(transition->title);
230                         transition_server = new PluginServer(*plugin_server);
231                         transition_server->open_plugin(0, get_edl(), transition);
232                         transition_server->init_realtime(
233                                 0,
234                                 1,
235                                 get_buffer_size());
236                 }
237 //printf("Module::update_transition 7\n");
238         }
240 //printf("Module::update_transition 8\n");
241 // Test transition
242 //      
243 //      if((transition && !this->transition) || (!transition && this->transition)) return 1;
244 //      if(transition && transition != this->transition->plugin) return 1;
248 void Module::dump()
250         printf("  Module title=%s\n", track->title);
251         printf("   Plugins total_attachments=%d\n", total_attachments);
252         for(int i = 0; i < total_attachments; i++)
253         {
254                 if(attachments[i])
255                         attachments[i]->dump();
256                 else
257                         printf("    No Plugin\n");
258         }