9ef9df07033529d9d04440d49d406e79658c473b
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / packagedispatcher.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2011 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 "asset.h"
23 #include "clip.h"
24 #include "confirmsave.h"
25 #include "edl.h"
26 #include "edlsession.h"
27 #include "labels.h"
28 #include "mainerror.h"
29 #include "mutex.h"
30 #include "mwindow.h"
31 #include "packagedispatcher.h"
32 #include "packagerenderer.h"
33 #include "preferences.h"
34 #include "render.h"
35 #include "file.h"
36
37
38
39 PackageDispatcher::PackageDispatcher()
40 {
41         packages = 0;
42         package_lock = new Mutex("PackageDispatcher::package_lock");
43         packaging_engine = 0;
44 }
45
46 PackageDispatcher::~PackageDispatcher()
47 {
48         if(packages)
49         {
50                 for(int i = 0; i < total_packages; i++)
51                         delete packages[i];
52                 delete [] packages;
53         }
54         if (packaging_engine)
55                 delete packaging_engine;
56         delete package_lock;
57 }
58
59 int PackageDispatcher::create_packages(MWindow *mwindow, EDL *edl,
60         Preferences *preferences, int strategy, Asset *default_asset,
61         double total_start, double total_end, int test_overwrite)
62 {
63         Label *label;
64         int result = 0;
65
66         this->mwindow = mwindow;
67         this->edl = edl;
68         this->preferences = preferences;
69         this->strategy = strategy;
70         this->default_asset = default_asset;
71         this->total_start = total_start;
72         this->total_end = total_end;
73
74         nodes = preferences->get_enabled_nodes();
75         audio_position = Units::to_int64(total_start * default_asset->sample_rate);
76         video_position = Units::to_int64(total_start * default_asset->frame_rate);
77         audio_end = Units::to_int64(total_end * default_asset->sample_rate);
78         video_end = Units::to_int64(total_end * default_asset->frame_rate);
79         current_package = 0;
80
81 // sleep(1);
82 // printf("PackageDispatcher::create_packages 1 %d %f %f\n",
83 // video_end,
84 // total_end,
85 // default_asset->frame_rate);
86
87         switch( strategy ) {
88         case SINGLE_PASS:
89                 total_len = this->total_end - this->total_start;
90                 package_len = total_len;
91                 min_package_len = total_len;
92                 total_packages = 1;
93                 total_allocated = 1;
94                 packages = new RenderPackage*[total_allocated];
95                 packages[0] = new RenderPackage;
96                 packages[0]->audio_start = audio_position;
97                 packages[0]->audio_end = audio_end;
98                 packages[0]->video_start = video_position;
99                 packages[0]->video_end = video_end;
100                 packages[0]->audio_do = default_asset->audio_data;
101                 packages[0]->video_do = default_asset->video_data;
102                 strcpy(packages[0]->path, default_asset->path);
103                 break;
104         case SINGLE_PASS_FARM:
105                 packaging_engine = File::new_packaging_engine(default_asset);
106                 packaging_engine->create_packages_single_farm(edl, preferences,
107                                 default_asset, total_start, total_end);
108                 break;
109         case FILE_PER_LABEL:
110         case FILE_PER_LABEL_FARM:
111                 label = edl->labels->first;
112                 total_packages = 0;
113                 packages = new RenderPackage*[edl->labels->total() + 2];
114                 if( !label ) {
115                         eprintf(_("Render file per label and no labels\n"));
116                         result = 1;
117                         break;
118                 }
119
120                 Render::get_starting_number(default_asset->path,
121                         current_number, number_start, total_digits, 3);
122
123                 while( audio_position < audio_end ) {
124                         RenderPackage *package = new RenderPackage;
125                         packages[total_packages++] = package;
126                         package->audio_start = audio_position;
127                         package->video_start = video_position;
128                         package->audio_do = default_asset->audio_data;
129                         package->video_do = default_asset->video_data;
130
131                         while( label &&
132                                 (label->position < (double)audio_position / default_asset->sample_rate ||
133                                 EQUIV(label->position, (double)audio_position / default_asset->sample_rate)) ) {
134                                 label = label->next;
135                         }
136
137                         if( !label ) {
138                                 package->audio_end = Units::to_int64(total_end * default_asset->sample_rate);
139                                 package->video_end = Units::to_int64(total_end * default_asset->frame_rate);
140                         }
141                         else {
142                                 package->audio_end = Units::to_int64(label->position * default_asset->sample_rate);
143                                 package->video_end = Units::to_int64(label->position * default_asset->frame_rate);
144                         }
145
146                         if( package->audio_end > audio_end ) {
147                                 package->audio_end = audio_end;
148                         }
149
150                         if( package->video_end > video_end ) {
151                                 package->video_end = video_end;
152                         }
153
154                         audio_position = package->audio_end;
155                         video_position = package->video_end;
156
157 // Create file number differently if image file sequence
158                         Render::create_filename(package->path, default_asset->path,
159                                 current_number++, total_digits, number_start);
160                 }
161
162                 total_allocated = total_packages;
163                 break;
164         case BRENDER_FARM:
165                 total_len = this->total_end - this->total_start;
166
167 // Create packages as they're requested.
168                 total_packages = 0;
169                 total_allocated = 0;
170                 packages = 0;
171
172                 Render::get_starting_number(default_asset->path,
173                         current_number, number_start, total_digits, 6);
174
175 // Master node only
176                 if( preferences->renderfarm_nodes.total == 1 ) {
177                         package_len = total_len;
178                         min_package_len = total_len;
179                 }
180                 else {
181                         package_len = preferences->brender_fragment /
182                                 edl->session->frame_rate;
183                         min_package_len = 1.0 / edl->session->frame_rate;
184                 }
185                 break;
186         }
187
188 // Test existence of every output file.
189 // Only if this isn't a background render or non interactive.
190         if( strategy != BRENDER_FARM && test_overwrite && mwindow ) {
191                 ArrayList<char*> paths;
192                 get_package_paths(&paths);
193                 result = ConfirmSave::test_files(mwindow, &paths);
194                 paths.remove_all_objects();
195         }
196
197         return result;
198 }
199
200 void PackageDispatcher::get_package_paths(ArrayList<char*> *path_list)
201 {
202                 if (strategy == SINGLE_PASS_FARM)
203                         packaging_engine->get_package_paths(path_list);
204                 else {
205                         for( int i=0; i<total_allocated; ++i )
206                                 path_list->append(cstrdup(packages[i]->path));
207                 }
208
209 }
210
211 RenderPackage* PackageDispatcher::get_package(double frames_per_second,
212                 int client_number, int use_local_rate)
213 {
214         package_lock->lock("PackageDispatcher::get_package");
215
216 // Store new frames per second for the node
217         if( !EQUIV(frames_per_second, 0) ) {
218                 preferences->set_rate(frames_per_second, client_number);
219                 if(mwindow) mwindow->preferences->copy_rates_from(preferences);
220         }
221         else {
222 // Use previous frames per second
223                 frames_per_second = preferences->get_rate(client_number);
224         }
225
226         float avg_frames_per_second = preferences->get_avg_rate(use_local_rate);
227
228         RenderPackage *result = 0;
229         switch( strategy ) {
230 //printf("PackageDispatcher::get_package 1 %d\n", strategy);
231         case SINGLE_PASS:
232         case FILE_PER_LABEL:
233         case FILE_PER_LABEL_FARM:
234                 if( current_package < total_packages )
235                         result = packages[current_package++];
236                 break;
237         case SINGLE_PASS_FARM:
238                 result = packaging_engine->get_package_single_farm(frames_per_second,
239                                         client_number, use_local_rate);
240                 break;
241         case BRENDER_FARM:
242 //printf("Dispatcher::get_package 1 %d %d\n", video_position, video_end);
243                 if( video_position < video_end ) {
244 // Allocate new packages
245                         if( total_packages == 0 ) {
246                                 total_allocated = 256;
247                                 packages = new RenderPackage*[total_allocated];
248                         }
249                         else if( total_packages >= total_allocated ) {
250                                 RenderPackage **old_packages = packages;
251                                 total_allocated *= 2;
252                                 packages = new RenderPackage*[total_allocated];
253                                 memcpy(packages,
254                                         old_packages,
255                                         total_packages * sizeof(RenderPackage*));
256                                 delete [] old_packages;
257                         }
258
259 // Calculate package.
260                         result = packages[total_packages] = new RenderPackage;
261                         double scaled_len;
262
263 // No load balancing data exists
264                         if( EQUIV(frames_per_second, 0) ||
265                              EQUIV(avg_frames_per_second, 0)) {
266                                 scaled_len = package_len;
267                         }
268                         else {
269 // Load balancing data exists
270                                 scaled_len = package_len *
271                                         frames_per_second / avg_frames_per_second;
272                         }
273
274                         scaled_len = MAX(scaled_len, min_package_len);
275
276 // Always an image file sequence
277                         result->audio_start = audio_position;
278                         result->video_start = video_position;
279                         result->audio_end = result->audio_start +
280                                 Units::to_int64(scaled_len * default_asset->sample_rate);
281                         result->video_end = result->video_start +
282                                 Units::to_int64(scaled_len * default_asset->frame_rate);
283                         if(result->video_end == result->video_start) result->video_end++;
284                         audio_position = result->audio_end;
285                         video_position = result->video_end;
286                         result->audio_do = default_asset->audio_data;
287                         result->video_do = default_asset->video_data;
288
289
290 // The frame numbers are read from the vframe objects themselves.
291                         Render::create_filename(result->path, default_asset->path,
292                                         0, total_digits, number_start);
293 //printf("PackageDispatcher::get_package 2 %s\n", result->path);
294                         ++current_number;
295                         ++total_packages;
296                         ++current_package;
297                 }
298                 break;
299         }
300
301         package_lock->unlock();
302         return result;
303 }
304
305
306 int PackageDispatcher::get_asset_list(ArrayList<Indexable*> &idxbls)
307 {
308         if( strategy == SINGLE_PASS_FARM )
309                 return packaging_engine->get_asset_list(idxbls);
310         for( int i=0; i<current_package; ++i ) {
311                 Asset *asset = new Asset;
312                 asset->copy_from(default_asset, 1);
313                 strcpy(asset->path, packages[i]->path);
314                 asset->video_length = packages[i]->video_end - packages[i]->video_start;
315                 asset->audio_length = packages[i]->audio_end - packages[i]->audio_start;
316                 idxbls.append(asset);
317         }
318         return current_package;
319 }
320
321 int64_t PackageDispatcher::get_progress_max()
322 {
323         return strategy == SINGLE_PASS_FARM ?
324                 packaging_engine->get_progress_max() :
325                 Units::to_int64(default_asset->sample_rate * (total_end - total_start)) +
326                         Units::to_int64(preferences->render_preroll *
327                                 total_allocated * default_asset->sample_rate);
328 }
329
330 int PackageDispatcher::get_total_packages()
331 {
332         return total_allocated;
333 }
334
335 int PackageDispatcher::packages_are_done()
336 {
337         return packaging_engine ? packaging_engine->packages_are_done() : 0;
338 }
339