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 = File::new_packaging_engine(default_asset);
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 get_package_paths(&paths);
193 result = ConfirmSave::test_files(mwindow, &paths);
194 paths.remove_all_objects();
200 void PackageDispatcher::get_package_paths(ArrayList<char*> *path_list)
202 if (strategy == SINGLE_PASS_FARM)
203 packaging_engine->get_package_paths(path_list);
205 for( int i=0; i<total_allocated; ++i )
206 path_list->append(cstrdup(packages[i]->path));
211 RenderPackage* PackageDispatcher::get_package(double frames_per_second,
212 int client_number, int use_local_rate)
214 package_lock->lock("PackageDispatcher::get_package");
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);
222 // Use previous frames per second
223 frames_per_second = preferences->get_rate(client_number);
226 float avg_frames_per_second = preferences->get_avg_rate(use_local_rate);
228 RenderPackage *result = 0;
230 //printf("PackageDispatcher::get_package 1 %d\n", strategy);
233 case FILE_PER_LABEL_FARM:
234 if( current_package < total_packages )
235 result = packages[current_package++];
237 case SINGLE_PASS_FARM:
238 result = packaging_engine->get_package_single_farm(frames_per_second,
239 client_number, use_local_rate);
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];
249 else if( total_packages >= total_allocated ) {
250 RenderPackage **old_packages = packages;
251 total_allocated *= 2;
252 packages = new RenderPackage*[total_allocated];
255 total_packages * sizeof(RenderPackage*));
256 delete [] old_packages;
259 // Calculate package.
260 result = packages[total_packages] = new RenderPackage;
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;
269 // Load balancing data exists
270 scaled_len = package_len *
271 frames_per_second / avg_frames_per_second;
274 scaled_len = MAX(scaled_len, min_package_len);
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;
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);
301 package_lock->unlock();
306 int PackageDispatcher::get_asset_list(ArrayList<Indexable*> &idxbls)
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);
318 return current_package;
321 int64_t PackageDispatcher::get_progress_max()
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);
330 int PackageDispatcher::get_total_packages()
332 return total_allocated;
335 int PackageDispatcher::packages_are_done()
337 return packaging_engine ? packaging_engine->packages_are_done() : 0;