4 #include "edlsession.h"
7 #include "packagedispatcher.h"
8 #include "packagerenderer.h"
9 #include "preferences.h"
15 PackageDispatcher::PackageDispatcher()
18 package_lock = new Mutex;
21 PackageDispatcher::~PackageDispatcher()
25 for(int i = 0; i < total_packages; i++)
32 int PackageDispatcher::create_packages(MWindow *mwindow,
34 Preferences *preferences,
42 this->mwindow = mwindow;
44 this->preferences = preferences;
45 this->strategy = strategy;
46 this->default_asset = default_asset;
47 this->total_start = total_start;
48 this->total_end = total_end;
50 nodes = preferences->get_enabled_nodes();
51 audio_position = Units::to_int64(total_start * default_asset->sample_rate);
52 video_position = Units::to_int64(total_start * default_asset->frame_rate);
53 audio_end = Units::to_int64(total_end * default_asset->sample_rate);
54 video_end = Units::to_int64(total_end * default_asset->frame_rate);
58 // printf("PackageDispatcher::create_packages 1 %d %f %f\n",
61 // default_asset->frame_rate);
65 if(strategy == SINGLE_PASS)
67 total_len = this->total_end - this->total_start;
68 package_len = total_len;
69 min_package_len = total_len;
72 packages = new RenderPackage*[total_allocated];
73 packages[0] = new RenderPackage;
74 packages[0]->audio_start = audio_position;
75 packages[0]->audio_end = audio_end;
76 packages[0]->video_start = video_position;
77 packages[0]->video_end = video_end;
78 strcpy(packages[0]->path, default_asset->path);
81 if(strategy == SINGLE_PASS_FARM)
83 total_len = this->total_end - this->total_start;
84 total_packages = preferences->renderfarm_job_count;
85 total_allocated = total_packages + nodes;
86 packages = new RenderPackage*[total_allocated];
87 package_len = total_len / total_packages;
88 min_package_len = 2.0 / edl->session->frame_rate;
91 //printf("PackageDispatcher::create_packages: %f / %d = %f\n", total_len, total_packages, package_len);
92 Render::get_starting_number(default_asset->path,
98 for(int i = 0; i < total_allocated; i++)
100 RenderPackage *package = packages[i] = new RenderPackage;
102 // Create file number differently if image file sequence
103 Render::create_filename(package->path,
112 if(strategy == FILE_PER_LABEL || strategy == FILE_PER_LABEL_FARM)
114 Label *label = edl->labels->first;
116 packages = new RenderPackage*[edl->labels->total() + 2];
118 Render::get_starting_number(default_asset->path,
124 while(audio_position < audio_end)
126 RenderPackage *package =
127 packages[total_packages] =
129 package->audio_start = audio_position;
130 package->video_start = video_position;
134 (label->position < (double)audio_position / default_asset->sample_rate ||
135 EQUIV(label->position, (double)audio_position / default_asset->sample_rate)))
142 package->audio_end = Units::to_int64(total_end * default_asset->sample_rate);
143 package->video_end = Units::to_int64(total_end * default_asset->frame_rate);
147 package->audio_end = Units::to_int64(label->position * default_asset->sample_rate);
148 package->video_end = Units::to_int64(label->position * default_asset->frame_rate);
151 if(package->audio_end > audio_end)
153 package->audio_end = audio_end;
156 if(package->video_end > video_end)
158 package->video_end = video_end;
161 audio_position = package->audio_end;
162 video_position = package->video_end;
163 // Create file number differently if image file sequence
164 Render::create_filename(package->path,
174 total_allocated = total_packages;
177 if(strategy == BRENDER_FARM)
179 total_len = this->total_end - this->total_start;
181 // Create packages as they're requested.
186 Render::get_starting_number(default_asset->path,
193 if(preferences->renderfarm_nodes.total == 1)
195 package_len = total_len;
196 min_package_len = total_len;
200 package_len = preferences->brender_fragment /
201 edl->session->frame_rate;
202 min_package_len = 1.0 / edl->session->frame_rate;
206 // Test existence of every output file
207 if(strategy != BRENDER_FARM)
209 for(int i = 0; i < total_allocated && !result; i++)
211 Asset temp_asset(packages[i]->path);
212 result = Render::test_existence(mwindow, &temp_asset);
219 RenderPackage* PackageDispatcher::get_package(double frames_per_second,
223 package_lock->lock();
225 preferences->set_rate(frames_per_second, client_number);
226 mwindow->preferences->copy_rates_from(preferences);
227 float avg_frames_per_second = preferences->get_avg_rate(use_local_rate);
229 RenderPackage *result = 0;
230 if(strategy == SINGLE_PASS ||
231 strategy == FILE_PER_LABEL ||
232 strategy == FILE_PER_LABEL_FARM)
234 if(current_package < total_packages)
236 result = packages[current_package];
241 if(strategy == SINGLE_PASS_FARM)
243 //printf("PackageDispatcher::get_package 1\n");
244 if(audio_position < audio_end ||
245 video_position < video_end)
248 //printf("PackageDispatcher::get_package 2\n");
250 result = packages[current_package];
251 result->audio_start = audio_position;
252 result->video_start = video_position;
254 //printf("PackageDispatcher::get_package 3\n");
255 if(current_package >= total_allocated - 1)
257 result->audio_end = audio_end;
258 result->video_end = video_end;
259 audio_position = result->audio_end;
260 video_position = result->video_end;
263 // No useful speed data
264 if(EQUIV(frames_per_second, 0) ||
265 EQUIV(avg_frames_per_second, 0))
267 scaled_len = MAX(package_len, min_package_len);
269 //printf("PackageDispatcher::get_package 4\n");
270 result->audio_end = audio_position +
271 Units::round(scaled_len * default_asset->sample_rate);
272 result->video_end = video_position +
273 Units::round(scaled_len * default_asset->frame_rate);
275 //printf("PackageDispatcher::get_package 5\n");
276 // If we get here without any useful speed data render the whole thing.
277 if(current_package >= total_packages - 1)
279 result->audio_end = audio_end;
280 result->video_end = video_end;
284 result->audio_end = MIN(audio_end, result->audio_end);
285 result->video_end = MIN(video_end, result->video_end);
288 //printf("PackageDispatcher::get_package 6\n");
289 audio_position = result->audio_end;
290 video_position = result->video_end;
293 // Useful speed data and future packages exist. Scale the
294 // package size to fit the requestor.
296 scaled_len = package_len *
298 avg_frames_per_second;
299 scaled_len = MAX(scaled_len, min_package_len);
301 //printf("PackageDispatcher::get_package 7\n");
302 result->audio_end = result->audio_start +
303 Units::to_int64(scaled_len * default_asset->sample_rate);
304 result->video_end = result->video_start +
305 Units::to_int64(scaled_len * default_asset->frame_rate);
307 //printf("PackageDispatcher::get_package 8\n");
308 result->audio_end = MIN(audio_end, result->audio_end);
309 result->video_end = MIN(video_end, result->video_end);
311 //printf("PackageDispatcher::get_package 9\n");
312 audio_position = result->audio_end;
313 video_position = result->video_end;
315 //printf("PackageDispatcher::get_package 10\n");
316 // Package size is no longer touched between total_packages and total_allocated
317 if(current_package < total_packages - 1)
319 package_len = (double)(audio_end - audio_position) /
320 (double)default_asset->sample_rate /
321 (double)(total_packages - current_package);
324 //printf("PackageDispatcher::get_package 11\n");
328 //printf("Dispatcher::get_package 2 %d %d %d %d\n",
329 //result->audio_start, result->video_start, result->audio_end, result->video_end);
333 if(strategy == BRENDER_FARM)
335 //printf("Dispatcher::get_package 1 %d %d\n", video_position, video_end);
336 if(video_position < video_end)
338 // Allocate new packages
339 if(total_packages == 0)
341 total_allocated = 256;
342 packages = new RenderPackage*[total_allocated];
345 if(total_packages >= total_allocated)
347 RenderPackage **old_packages = packages;
348 total_allocated *= 2;
349 packages = new RenderPackage*[total_allocated];
352 total_packages * sizeof(RenderPackage*));
353 delete [] old_packages;
356 // Calculate package.
357 result = packages[total_packages] = new RenderPackage;
360 // No load balancing data exists
361 if(EQUIV(frames_per_second, 0) ||
362 EQUIV(avg_frames_per_second, 0))
364 scaled_len = package_len;
367 // Load balancing data exists
369 scaled_len = package_len *
371 avg_frames_per_second;
374 scaled_len = MAX(scaled_len, min_package_len);
376 // Always an image file sequence
377 result->audio_start = audio_position;
378 result->video_start = video_position;
379 result->audio_end = result->audio_start +
380 Units::to_int64(scaled_len * default_asset->sample_rate);
381 result->video_end = result->video_start +
382 Units::to_int64(scaled_len * default_asset->frame_rate);
383 if(result->video_end == result->video_start) result->video_end++;
384 audio_position = result->audio_end;
385 video_position = result->video_end;
386 // The frame numbers are read from the vframe objects themselves.
387 Render::create_filename(result->path,
392 //printf("PackageDispatcher::get_package 2 %s\n", result->path);
400 package_lock->unlock();
406 ArrayList<Asset*>* PackageDispatcher::get_asset_list()
408 ArrayList<Asset*> *assets = new ArrayList<Asset*>;
410 for(int i = 0; i < current_package; i++)
412 Asset *asset = new Asset;
413 *asset = *default_asset;
414 strcpy(asset->path, packages[i]->path);
415 asset->video_length = packages[i]->video_end - packages[i]->video_start;
416 asset->audio_length = packages[i]->audio_end - packages[i]->audio_start;
417 assets->append(asset);