Merge CV, ver=5.1; ops/methods from HV, and interface from CV where possible
[goodguy/history.git] / cinelerra-5.1 / cinelerra / attachmentpoint.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 "filexml.h"
24 #include "edl.h"
25 #include "edlsession.h"
26 #include "mwindow.h"
27 #include "plugin.h"
28 #include "pluginserver.h"
29 #include "renderengine.h"
30 #include "transportque.h"
31 #include "virtualnode.h"
32
33
34
35 AttachmentPoint::AttachmentPoint(RenderEngine *renderengine,
36         Plugin *plugin,
37         int data_type)
38 {
39         reset_parameters();
40         this->plugin = plugin;
41         this->plugin_id = plugin->id;
42         this->renderengine = renderengine;
43         this->data_type = data_type;
44         plugin_server = MWindow::scan_plugindb(plugin->title,
45                 data_type);
46 }
47
48 AttachmentPoint::~AttachmentPoint()
49 {
50         delete_buffer_vector();
51         plugin_servers.remove_all_objects();
52 }
53
54
55 int AttachmentPoint::reset_parameters()
56 {
57         plugin_server = 0;
58         reset_status();
59         return 0;
60 }
61
62
63 void AttachmentPoint::reset_status()
64 {
65         if(!this) printf("AttachmentPoint::reset_status NULL\n");
66         start_position = 0;
67         len = 0;
68         sample_rate = 0;
69         frame_rate = 0;
70         is_processed = 0;
71 }
72
73
74 int AttachmentPoint::identical(AttachmentPoint *old)
75 {
76         return plugin_id == old->plugin_id;
77 }
78
79
80 int AttachmentPoint::render_init()
81 {
82         if(!this) printf("AttachmentPoint::render_init NULL\n");
83         if(plugin_server && plugin->on)
84         {
85 // Start new plugin servers if the number of nodes changed.
86 // The number of nodes can change independantly of the module's
87 // attachment count.
88                 if(virtual_plugins.total != new_virtual_plugins.total)
89                 {
90                         plugin_servers.remove_all_objects();
91                         for(int i = 0; i < new_virtual_plugins.total; i++)
92                         {
93                                 if(i == 0 || !plugin_server->multichannel)
94                                 {
95                                         PluginServer *new_server;
96                                         plugin_servers.append(new_server = new PluginServer(*plugin_server));
97                                         new_server->set_attachmentpoint(this);
98                                         plugin_servers.values[i]->open_plugin(0,
99                                                 renderengine->preferences,
100                                                 renderengine->get_edl(),
101                                                 plugin);
102                                         plugin_servers.values[i]->init_realtime(
103                                                 renderengine->get_edl()->session->real_time_playback &&
104                                                         renderengine->command->realtime,
105                                                 plugin_server->multichannel ? new_virtual_plugins.total : 1,
106                                                 get_buffer_size());
107                                 }
108                         }
109                 }
110
111 // Set new back pointers in the plugin servers
112                 if(plugin_server->multichannel && plugin_servers.total)
113                 {
114                         PluginServer *new_server = plugin_servers.values[0];
115                         new_server->reset_nodes();
116                         for(int i = 0; i < new_virtual_plugins.total; i++)
117                         {
118                                 new_server->append_node(new_virtual_plugins.values[i]);
119                         }
120                 }
121                 else
122                 {
123                         for(int i = 0; i < new_virtual_plugins.total; i++)
124                         {
125                                 PluginServer *new_server = plugin_servers.values[i];
126                                 new_server->reset_nodes();
127                                 new_server->append_node(new_virtual_plugins.values[i]);
128                         }
129                 }
130         }
131
132 // Delete old plugin servers
133         delete_buffer_vector();
134         virtual_plugins.remove_all();
135
136 // Set new plugin servers
137         for(int i = 0; i < new_virtual_plugins.total; i++)
138                 virtual_plugins.append(new_virtual_plugins.values[i]);
139         new_virtual_plugins.remove_all();
140
141
142         return 0;
143 }
144
145 void AttachmentPoint::render_stop()
146 {
147         if(plugin_server && plugin->on)
148         {
149                 for(int i = 0; i < plugin_servers.total; i++)
150                 {
151                         plugin_servers.values[i]->render_stop();
152                 }
153         }
154 }
155
156 int AttachmentPoint::attach_virtual_plugin(VirtualNode *virtual_plugin)
157 {
158         if(!this) printf("AttachmentPoint::attach_virtual_plugin NULL\n");
159         int buffer_number = 0;
160
161         if(plugin_server && plugin->on)
162         {
163 // add virtual plugin to list of new virtual plugins
164                 new_virtual_plugins.append(virtual_plugin);
165 //printf("AttachmentPoint::attach_virtual_plugin 1 %d\n", new_virtual_plugins.total);
166 // Always increment buffer number since this also corresponds to what
167 // plugin server to access if single channel.
168                 buffer_number = new_virtual_plugins.total - 1;
169         }
170         else
171         if(!plugin->on)
172         {
173                 printf("AttachmentPoint::attach_virtual_plugin attempt to attach plugin when off.\n");
174         }
175         else
176         if(!plugin_server)
177         {
178                 printf("AttachmentPoint::attach_virtual_plugin attempt to attach when no plugin_server.\n");
179         }
180
181         return buffer_number;
182 }
183
184 int AttachmentPoint::multichannel_shared(int search_new)
185 {
186         if(!this) printf("AttachmentPoint::multichannel_shared NULL\n");
187         if(search_new)
188         {
189                 if(new_virtual_plugins.total &&
190                         plugin_server &&
191                         plugin_server->multichannel) return 1;
192         }
193         else
194         {
195                 if(virtual_plugins.total &&
196                         plugin_server &&
197                         plugin_server->multichannel) return 1;
198         }
199         return 0;
200 }
201
202 int AttachmentPoint::singlechannel()
203 {
204         if(!this) printf("AttachmentPoint::singlechannel NULL\n");
205         if(plugin_server && !plugin_server->multichannel) return 1;
206         return 0;
207 }
208
209
210 void AttachmentPoint::render_gui(void *data, PluginServer *server)
211 {
212 //printf("AttachmentPoint::render_gui 1 %p %p\n", server, plugin_servers.get(0));
213         if(!this) printf("AttachmentPoint::render_gui 1 NULL\n");
214
215 // Discard if not 1st plugin server, so single channel plugins don't get double GUI updates
216         if(server != plugin_servers.get(0)) return;
217
218         if(renderengine && renderengine->mwindow)
219                 renderengine->mwindow->render_plugin_gui(data, plugin);
220 }
221
222 void AttachmentPoint::render_gui(void *data, int size, PluginServer *server)
223 {
224         if(!this) printf("AttachmentPoint::render_gui 2 NULL\n");
225
226 // Discard if not 1st plugin server, so single channel plugins don't get double GUI updates
227         if(server != plugin_servers.get(0)) return;
228
229         if(renderengine && renderengine->mwindow)
230                 renderengine->mwindow->render_plugin_gui(data, size, plugin);
231 }
232
233 int AttachmentPoint::gui_open()
234 {
235         if(renderengine && renderengine->mwindow)
236                 return renderengine->mwindow->plugin_gui_open(plugin);
237         return 0;
238 }
239
240 int AttachmentPoint::dump(FILE *fp)
241 {
242         if(this)
243         {
244                 fprintf(fp,"    Attachmentpoint this=%p virtual_plugins=%d\n", this, new_virtual_plugins.total);
245                 if(plugin_server) plugin_server->dump(fp);
246         }
247         else
248         {
249                 fprintf(fp,"    No Plugin\n");
250         }
251         return 0;
252 }
253