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"
30 #include "packagedispatcher.h"
31 #include "packagerenderer.h"
32 #include "preferences.h"
38 PackageDispatcher::PackageDispatcher()
41 package_lock = new Mutex("PackageDispatcher::package_lock");
45 PackageDispatcher::~PackageDispatcher()
49 for(int i = 0; i < total_packages; i++)
54 delete packaging_engine;
58 int PackageDispatcher::create_packages(MWindow *mwindow,
60 Preferences *preferences,
69 this->mwindow = mwindow;
71 this->preferences = preferences;
72 this->strategy = strategy;
73 this->default_asset = default_asset;
74 this->total_start = total_start;
75 this->total_end = total_end;
77 nodes = preferences->get_enabled_nodes();
78 audio_position = Units::to_int64(total_start * default_asset->sample_rate);
79 video_position = Units::to_int64(total_start * default_asset->frame_rate);
80 audio_end = Units::to_int64(total_end * default_asset->sample_rate);
81 video_end = Units::to_int64(total_end * default_asset->frame_rate);
85 // printf("PackageDispatcher::create_packages 1 %d %f %f\n",
88 // default_asset->frame_rate);
92 if(strategy == SINGLE_PASS)
94 total_len = this->total_end - this->total_start;
95 package_len = total_len;
96 min_package_len = total_len;
99 packages = new RenderPackage*[total_allocated];
100 packages[0] = new RenderPackage;
101 packages[0]->audio_start = audio_position;
102 packages[0]->audio_end = audio_end;
103 packages[0]->video_start = video_position;
104 packages[0]->video_end = video_end;
105 packages[0]->audio_do = default_asset->audio_data;
106 packages[0]->video_do = default_asset->video_data;
107 strcpy(packages[0]->path, default_asset->path);
110 if(strategy == SINGLE_PASS_FARM)
112 packaging_engine = File::new_packaging_engine(default_asset);
113 packaging_engine->create_packages_single_farm(
121 if(strategy == FILE_PER_LABEL || strategy == FILE_PER_LABEL_FARM)
123 Label *label = edl->labels->first;
125 packages = new RenderPackage*[edl->labels->total() + 2];
127 Render::get_starting_number(default_asset->path,
133 while(audio_position < audio_end)
135 RenderPackage *package =
136 packages[total_packages] =
138 package->audio_start = audio_position;
139 package->video_start = video_position;
140 package->audio_do = default_asset->audio_data;
141 package->video_do = default_asset->video_data;
145 (label->position < (double)audio_position / default_asset->sample_rate ||
146 EQUIV(label->position, (double)audio_position / default_asset->sample_rate)))
153 package->audio_end = Units::to_int64(total_end * default_asset->sample_rate);
154 package->video_end = Units::to_int64(total_end * default_asset->frame_rate);
158 package->audio_end = Units::to_int64(label->position * default_asset->sample_rate);
159 package->video_end = Units::to_int64(label->position * default_asset->frame_rate);
162 if(package->audio_end > audio_end)
164 package->audio_end = audio_end;
167 if(package->video_end > video_end)
169 package->video_end = video_end;
172 audio_position = package->audio_end;
173 video_position = package->video_end;
174 // Create file number differently if image file sequence
175 Render::create_filename(package->path,
185 total_allocated = total_packages;
188 if(strategy == BRENDER_FARM)
190 total_len = this->total_end - this->total_start;
192 // Create packages as they're requested.
197 Render::get_starting_number(default_asset->path,
204 if(preferences->renderfarm_nodes.total == 1)
206 package_len = total_len;
207 min_package_len = total_len;
211 package_len = preferences->brender_fragment /
212 edl->session->frame_rate;
213 min_package_len = 1.0 / edl->session->frame_rate;
217 // Test existence of every output file.
218 // Only if this isn't a background render or non interactive.
219 if(strategy != BRENDER_FARM &&
223 ArrayList<char*> paths;
224 get_package_paths(&paths);
225 result = ConfirmSave::test_files(mwindow, &paths);
226 paths.remove_all_objects();
232 void PackageDispatcher::get_package_paths(ArrayList<char*> *path_list)
234 if (strategy == SINGLE_PASS_FARM)
235 packaging_engine->get_package_paths(path_list);
238 for(int i = 0; i < total_allocated; i++)
239 path_list->append(strdup(packages[i]->path));
240 path_list->set_free();
245 RenderPackage* PackageDispatcher::get_package(double frames_per_second,
250 package_lock->lock("PackageDispatcher::get_package");
252 if(debug) printf("PackageDispatcher::get_package %d %f %d %d\n",
258 // Store new frames per second for the node
259 if(!EQUIV(frames_per_second, 0))
261 preferences->set_rate(frames_per_second, client_number);
262 if(mwindow) mwindow->preferences->copy_rates_from(preferences);
265 // Use previous frames per second
267 frames_per_second = preferences->get_rate(client_number);
270 if(debug) printf("PackageDispatcher::get_package %d %f %d %d\n",
276 float avg_frames_per_second = preferences->get_avg_rate(use_local_rate);
278 RenderPackage *result = 0;
279 //printf("PackageDispatcher::get_package 1 %d\n", strategy);
280 if(strategy == SINGLE_PASS ||
281 strategy == FILE_PER_LABEL ||
282 strategy == FILE_PER_LABEL_FARM)
284 if(current_package < total_packages)
286 result = packages[current_package];
291 if(strategy == SINGLE_PASS_FARM)
293 result = packaging_engine->get_package_single_farm(frames_per_second,
298 if(strategy == BRENDER_FARM)
300 //printf("Dispatcher::get_package 1 %d %d\n", video_position, video_end);
301 if(video_position < video_end)
303 // Allocate new packages
304 if(total_packages == 0)
306 total_allocated = 256;
307 packages = new RenderPackage*[total_allocated];
310 if(total_packages >= total_allocated)
312 RenderPackage **old_packages = packages;
313 total_allocated *= 2;
314 packages = new RenderPackage*[total_allocated];
317 total_packages * sizeof(RenderPackage*));
318 delete [] old_packages;
321 // Calculate package.
322 result = packages[total_packages] = new RenderPackage;
325 // No load balancing data exists
326 if(EQUIV(frames_per_second, 0) ||
327 EQUIV(avg_frames_per_second, 0))
329 scaled_len = package_len;
332 // Load balancing data exists
334 scaled_len = package_len *
336 avg_frames_per_second;
339 scaled_len = MAX(scaled_len, min_package_len);
341 // Always an image file sequence
342 result->audio_start = audio_position;
343 result->video_start = video_position;
344 result->audio_end = result->audio_start +
345 Units::to_int64(scaled_len * default_asset->sample_rate);
346 result->video_end = result->video_start +
347 Units::to_int64(scaled_len * default_asset->frame_rate);
348 if(result->video_end == result->video_start) result->video_end++;
349 audio_position = result->audio_end;
350 video_position = result->video_end;
351 result->audio_do = default_asset->audio_data;
352 result->video_do = default_asset->video_data;
355 // The frame numbers are read from the vframe objects themselves.
356 Render::create_filename(result->path,
361 //printf("PackageDispatcher::get_package 2 %s\n", result->path);
369 package_lock->unlock();
371 if(debug && result) printf("PackageDispatcher::get_package %d %ld\n", __LINE__, (long)(result->video_end - result->video_start));
376 ArrayList<Indexable*>* PackageDispatcher::get_asset_list()
378 ArrayList<Indexable*> *assets = new ArrayList<Indexable*>;
381 if(debug) printf("PackageDispatcher::get_asset_list %d\n", __LINE__);
382 if(debug) default_asset->dump();
383 for(int i = 0; i < current_package; i++)
385 Asset *asset = new Asset;
386 asset->copy_from(default_asset, 1);
387 strcpy(asset->path, packages[i]->path);
388 asset->video_length = packages[i]->video_end - packages[i]->video_start;
389 asset->audio_length = packages[i]->audio_end - packages[i]->audio_start;
390 assets->append(asset);
391 if(debug) printf("PackageDispatcher::get_asset_list %d\n", __LINE__);
392 if(debug) asset->dump();
398 int64_t PackageDispatcher::get_progress_max()
400 if (strategy == SINGLE_PASS_FARM)
401 return packaging_engine->get_progress_max();
403 return Units::to_int64(default_asset->sample_rate *
404 (total_end - total_start)) +
405 Units::to_int64(preferences->render_preroll *
407 default_asset->sample_rate);
410 int PackageDispatcher::get_total_packages()
412 return total_allocated;
415 int PackageDispatcher::packages_are_done()
417 if (packaging_engine)
418 return packaging_engine->packages_are_done();