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
24 #include "confirmsave.h"
26 #include "edlsession.h"
28 #include "mainerror.h"
31 #include "packagedispatcher.h"
32 #include "packagerenderer.h"
33 #include "preferences.h"
39 PackageDispatcher::PackageDispatcher()
42 package_lock = new Mutex("PackageDispatcher::package_lock");
46 PackageDispatcher::~PackageDispatcher()
50 for(int i = 0; i < total_packages; i++)
55 delete packaging_engine;
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)
66 this->mwindow = mwindow;
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;
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);
82 // printf("PackageDispatcher::create_packages 1 %d %f %f\n",
85 // default_asset->frame_rate);
89 total_len = this->total_end - this->total_start;
90 package_len = total_len;
91 min_package_len = total_len;
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);
104 case SINGLE_PASS_FARM:
105 packaging_engine = (PackagingEngine*)new PackagingEngineDefault();
106 packaging_engine->create_packages_single_farm(edl, preferences,
107 default_asset, total_start, total_end);
110 case FILE_PER_LABEL_FARM:
111 label = edl->labels->first;
113 packages = new RenderPackage*[edl->labels->total() + 2];
115 eprintf(_("Render file per label and no labels\n"));
120 Render::get_starting_number(default_asset->path,
121 current_number, number_start, total_digits, 3);
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;
132 (label->position < (double)audio_position / default_asset->sample_rate ||
133 EQUIV(label->position, (double)audio_position / default_asset->sample_rate)) ) {
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);
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);
146 if( package->audio_end > audio_end ) {
147 package->audio_end = audio_end;
150 if( package->video_end > video_end ) {
151 package->video_end = video_end;
154 audio_position = package->audio_end;
155 video_position = package->video_end;
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);
162 total_allocated = total_packages;
165 total_len = this->total_end - this->total_start;
167 // Create packages as they're requested.
172 Render::get_starting_number(default_asset->path,
173 current_number, number_start, total_digits, 6);
176 if( preferences->renderfarm_nodes.total == 1 ) {
177 package_len = total_len;
178 min_package_len = total_len;
181 package_len = preferences->brender_fragment /
182 edl->session->frame_rate;
183 min_package_len = 1.0 / edl->session->frame_rate;
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 paths.set_array_delete();
193 get_package_paths(&paths);
194 result = ConfirmSave::test_files(mwindow, &paths);
195 paths.remove_all_objects();
201 void PackageDispatcher::get_package_paths(ArrayList<char*> *path_list)
203 if (strategy == SINGLE_PASS_FARM)
204 packaging_engine->get_package_paths(path_list);
206 for( int i=0; i<total_allocated; ++i )
207 path_list->append(cstrdup(packages[i]->path));
212 RenderPackage* PackageDispatcher::get_package(double frames_per_second,
213 int client_number, int use_local_rate)
215 package_lock->lock("PackageDispatcher::get_package");
217 // Store new frames per second for the node
218 if( !EQUIV(frames_per_second, 0) ) {
219 preferences->set_rate(frames_per_second, client_number);
220 if(mwindow) mwindow->preferences->copy_rates_from(preferences);
223 // Use previous frames per second
224 frames_per_second = preferences->get_rate(client_number);
227 float avg_frames_per_second = preferences->get_avg_rate(use_local_rate);
229 RenderPackage *result = 0;
231 //printf("PackageDispatcher::get_package 1 %d\n", strategy);
234 case FILE_PER_LABEL_FARM:
235 if( current_package < total_packages )
236 result = packages[current_package++];
238 case SINGLE_PASS_FARM:
239 result = packaging_engine->get_package_single_farm(frames_per_second,
240 client_number, use_local_rate);
243 //printf("Dispatcher::get_package 1 %d %d\n", video_position, video_end);
244 if( video_position < video_end ) {
245 // Allocate new packages
246 if( total_packages == 0 ) {
247 total_allocated = 256;
248 packages = new RenderPackage*[total_allocated];
250 else if( total_packages >= total_allocated ) {
251 RenderPackage **old_packages = packages;
252 total_allocated *= 2;
253 packages = new RenderPackage*[total_allocated];
256 total_packages * sizeof(RenderPackage*));
257 delete [] old_packages;
260 // Calculate package.
261 result = packages[total_packages] = new RenderPackage;
264 // No load balancing data exists
265 if( EQUIV(frames_per_second, 0) ||
266 EQUIV(avg_frames_per_second, 0)) {
267 scaled_len = package_len;
270 // Load balancing data exists
271 scaled_len = package_len *
272 frames_per_second / avg_frames_per_second;
275 scaled_len = MAX(scaled_len, min_package_len);
277 // Always an image file sequence
278 result->audio_start = audio_position;
279 result->video_start = video_position;
280 result->audio_end = result->audio_start +
281 Units::to_int64(scaled_len * default_asset->sample_rate);
282 if( result->audio_end > audio_end ) result->audio_end = audio_end;
283 result->video_end = result->video_start +
284 Units::to_int64(scaled_len * default_asset->frame_rate);
285 if( result->video_end > video_end ) result->video_end = video_end;
286 if( result->video_end == result->video_start ) result->video_end++;
287 audio_position = result->audio_end;
288 video_position = result->video_end;
289 result->audio_do = default_asset->audio_data;
290 result->video_do = default_asset->video_data;
293 // The frame numbers are read from the vframe objects themselves.
294 Render::create_filename(result->path, default_asset->path,
295 0, total_digits, number_start);
296 //printf("PackageDispatcher::get_package 2 %s\n", result->path);
304 package_lock->unlock();
309 int PackageDispatcher::get_asset_list(ArrayList<Indexable*> &idxbls)
311 if( strategy == SINGLE_PASS_FARM )
312 return packaging_engine->get_asset_list(idxbls);
313 for( int i=0; i<current_package; ++i ) {
314 Asset *asset = new Asset;
315 asset->copy_from(default_asset, 1);
316 strcpy(asset->path, packages[i]->path);
317 asset->video_length = packages[i]->video_end - packages[i]->video_start;
318 asset->audio_length = packages[i]->audio_end - packages[i]->audio_start;
319 idxbls.append(asset);
321 return current_package;
324 int64_t PackageDispatcher::get_progress_max()
326 return strategy == SINGLE_PASS_FARM ?
327 packaging_engine->get_progress_max() :
328 Units::to_int64(default_asset->sample_rate * (total_end - total_start)) +
329 Units::to_int64(preferences->render_preroll *
330 total_allocated * default_asset->sample_rate);
333 RenderPackage *PackageDispatcher::get_package(int number)
335 return packages[number];
338 int PackageDispatcher::get_total_packages()
340 return total_allocated;
343 int PackageDispatcher::packages_are_done()
345 return packaging_engine ? packaging_engine->packages_are_done() : 0;