2 #include "audioconfig.h"
3 #include "audiodevice.inc"
9 #include "filesystem.h"
12 #include "preferences.h"
14 #include "videoconfig.h"
15 #include "videodevice.inc"
18 //#define CLAMP(x, y, z) (x) = ((x) < (y) ? (y) : ((x) > (z) ? (z) : (x)))
31 Preferences::Preferences()
36 preferences_lock = new Mutex("Preferences::preferences_lock");
37 sprintf(index_directory, BCASTDIR);
38 if(strlen(index_directory))
39 fs.complete_path(index_directory);
40 cache_size = 0xa00000;
41 index_size = 0x300000;
46 force_uniprocessor = 0;
47 renderfarm_port = DEAMON_PORT;
50 renderfarm_mountpoint[0] = 0;
52 renderfarm_job_count = 20;
53 processors = calculate_processors(0);
54 real_processors = calculate_processors(1);
56 // Default brender asset
57 brender_asset = new Asset;
58 brender_asset->audio_data = 0;
59 brender_asset->video_data = 1;
60 sprintf(brender_asset->path, "/tmp/brender");
61 brender_asset->format = FILE_JPEG_LIST;
62 brender_asset->jpeg_quality = 80;
70 for(int i = 0; i < MAXCHANNELS; i++)
72 for(int j = 0; j < i + 1; j++)
74 int position = 180 - (360 * j / (i + 1));
75 while(position < 0) position += 360;
76 channel_positions[i * MAXCHANNELS + j] = position;
81 Preferences::~Preferences()
83 Garbage::delete_object(brender_asset);
84 delete preferences_lock;
87 void Preferences::copy_rates_from(Preferences *preferences)
89 preferences_lock->lock("Preferences::copy_rates_from");
90 // Need to match node titles in case the order changed and in case
91 // one of the nodes in the source is the master node.
92 local_rate = preferences->local_rate;
95 j < preferences->renderfarm_nodes.total;
98 double new_rate = preferences->renderfarm_rate.values[j];
99 // Put in the master node
100 if(preferences->renderfarm_nodes.values[j][0] == '/')
102 if(!EQUIV(new_rate, 0.0))
103 local_rate = new_rate;
106 // Search for local node and copy it to that node
107 if(!EQUIV(new_rate, 0.0))
109 for(int i = 0; i < renderfarm_nodes.total; i++)
111 if(!strcmp(preferences->renderfarm_nodes.values[j], renderfarm_nodes.values[i]) &&
112 preferences->renderfarm_ports.values[j] == renderfarm_ports.values[i])
114 renderfarm_rate.values[i] = new_rate;
121 //printf("Preferences::copy_rates_from 1 %f %f\n", local_rate, preferences->local_rate);
122 preferences_lock->unlock();
125 void Preferences::copy_from(Preferences *that)
127 // ================================= Performance ================================
128 strcpy(index_directory, that->index_directory);
129 index_size = that->index_size;
130 index_count = that->index_count;
131 use_thumbnails = that->use_thumbnails;
132 strcpy(global_plugin_dir, that->global_plugin_dir);
133 strcpy(theme, that->theme);
135 use_tipwindow = that->use_tipwindow;
137 cache_size = that->cache_size;
138 force_uniprocessor = that->force_uniprocessor;
139 processors = that->processors;
140 real_processors = that->real_processors;
141 renderfarm_nodes.remove_all_objects();
142 renderfarm_ports.remove_all();
143 renderfarm_enabled.remove_all();
144 renderfarm_rate.remove_all();
145 local_rate = that->local_rate;
146 for(int i = 0; i < that->renderfarm_nodes.total; i++)
148 add_node(that->renderfarm_nodes.values[i],
149 that->renderfarm_ports.values[i],
150 that->renderfarm_enabled.values[i],
151 that->renderfarm_rate.values[i]);
153 use_renderfarm = that->use_renderfarm;
154 renderfarm_port = that->renderfarm_port;
155 render_preroll = that->render_preroll;
156 brender_preroll = that->brender_preroll;
157 renderfarm_job_count = that->renderfarm_job_count;
158 renderfarm_vfs = that->renderfarm_vfs;
159 strcpy(renderfarm_mountpoint, that->renderfarm_mountpoint);
160 renderfarm_consolidate = that->renderfarm_consolidate;
161 use_brender = that->use_brender;
162 brender_fragment = that->brender_fragment;
163 *brender_asset = *that->brender_asset;
168 if(strlen(index_directory))
170 fs.complete_path(index_directory);
171 fs.add_end_slash(index_directory);
174 if(strlen(global_plugin_dir))
176 fs.complete_path(global_plugin_dir);
177 fs.add_end_slash(global_plugin_dir);
183 void Preferences::boundaries()
185 renderfarm_job_count = MAX(renderfarm_job_count, 1);
186 CLAMP(cache_size, MIN_CACHE_SIZE, MAX_CACHE_SIZE);
189 Preferences& Preferences::operator=(Preferences &that)
191 printf("Preferences::operator=\n");
196 void Preferences::print_channels(char *string,
197 int *channel_positions,
200 char string3[BCTEXTLEN];
202 for(int j = 0; j < channels; j++)
204 sprintf(string3, "%d", channel_positions[j]);
205 strcat(string, string3);
211 void Preferences::scan_channels(char *string,
212 int *channel_positions,
215 char string2[BCTEXTLEN];
216 int len = strlen(string);
217 int current_channel = 0;
218 for(int i = 0; i < len; i++)
220 strcpy(string2, &string[i]);
221 for(int j = 0; j < BCTEXTLEN; j++)
223 if(string2[j] == ',' || string2[j] == 0)
230 channel_positions[current_channel++] = atoi(string2);
231 if(current_channel >= channels) break;
235 int Preferences::load_defaults(BC_Hash *defaults)
237 char string[BCTEXTLEN];
239 use_tipwindow = defaults->get("USE_TIPWINDOW", use_tipwindow);
240 defaults->get("INDEX_DIRECTORY", index_directory);
241 index_size = defaults->get("INDEX_SIZE", index_size);
242 index_count = defaults->get("INDEX_COUNT", index_count);
243 use_thumbnails = defaults->get("USE_THUMBNAILS", use_thumbnails);
245 sprintf(global_plugin_dir, PLUGIN_DIR);
246 // defaults->get("GLOBAL_PLUGIN_DIR", global_plugin_dir);
247 if(getenv("GLOBAL_PLUGIN_DIR"))
249 strcpy(global_plugin_dir, getenv("GLOBAL_PLUGIN_DIR"));
252 strcpy(theme, DEFAULT_THEME);
253 defaults->get("THEME", theme);
255 for(int i = 0; i < MAXCHANNELS; i++)
257 char string2[BCTEXTLEN];
258 sprintf(string, "CHANNEL_POSITIONS%d", i);
259 print_channels(string2,
260 &channel_positions[i * MAXCHANNELS],
263 defaults->get(string, string2);
265 scan_channels(string2,
266 &channel_positions[i * MAXCHANNELS],
270 brender_asset->load_defaults(defaults,
280 force_uniprocessor = defaults->get("FORCE_UNIPROCESSOR", 0);
281 use_brender = defaults->get("USE_BRENDER", use_brender);
282 brender_fragment = defaults->get("BRENDER_FRAGMENT", brender_fragment);
283 cache_size = defaults->get("CACHE_SIZE", cache_size);
284 local_rate = defaults->get("LOCAL_RATE", local_rate);
285 use_renderfarm = defaults->get("USE_RENDERFARM", use_renderfarm);
286 renderfarm_port = defaults->get("RENDERFARM_PORT", renderfarm_port);
287 render_preroll = defaults->get("RENDERFARM_PREROLL", render_preroll);
288 brender_preroll = defaults->get("BRENDER_PREROLL", brender_preroll);
289 renderfarm_job_count = defaults->get("RENDERFARM_JOBS_COUNT", renderfarm_job_count);
290 renderfarm_consolidate = defaults->get("RENDERFARM_CONSOLIDATE", renderfarm_consolidate);
291 // renderfarm_vfs = defaults->get("RENDERFARM_VFS", renderfarm_vfs);
292 defaults->get("RENDERFARM_MOUNTPOINT", renderfarm_mountpoint);
293 int renderfarm_total = defaults->get("RENDERFARM_TOTAL", 0);
295 for(int i = 0; i < renderfarm_total; i++)
297 sprintf(string, "RENDERFARM_NODE%d", i);
298 char result[BCTEXTLEN];
300 int result_enabled = 0;
301 float result_rate = 0.0;
304 defaults->get(string, result);
306 sprintf(string, "RENDERFARM_PORT%d", i);
307 result_port = defaults->get(string, renderfarm_port);
309 sprintf(string, "RENDERFARM_ENABLED%d", i);
310 result_enabled = defaults->get(string, result_enabled);
312 sprintf(string, "RENDERFARM_RATE%d", i);
313 result_rate = defaults->get(string, result_rate);
317 add_node(result, result_port, result_enabled, result_rate);
326 int Preferences::save_defaults(BC_Hash *defaults)
328 char string[BCTEXTLEN];
331 defaults->update("USE_TIPWINDOW", use_tipwindow);
333 defaults->update("CACHE_SIZE", cache_size);
334 defaults->update("INDEX_DIRECTORY", index_directory);
335 defaults->update("INDEX_SIZE", index_size);
336 defaults->update("INDEX_COUNT", index_count);
337 defaults->update("USE_THUMBNAILS", use_thumbnails);
338 // defaults->update("GLOBAL_PLUGIN_DIR", global_plugin_dir);
339 defaults->update("THEME", theme);
342 for(int i = 0; i < MAXCHANNELS; i++)
344 char string2[BCTEXTLEN];
345 sprintf(string, "CHANNEL_POSITIONS%d", i);
346 print_channels(string2, &channel_positions[i * MAXCHANNELS], i + 1);
347 defaults->update(string, string2);
350 defaults->update("FORCE_UNIPROCESSOR", force_uniprocessor);
351 brender_asset->save_defaults(defaults,
358 defaults->update("USE_BRENDER", use_brender);
359 defaults->update("BRENDER_FRAGMENT", brender_fragment);
360 defaults->update("USE_RENDERFARM", use_renderfarm);
361 defaults->update("LOCAL_RATE", local_rate);
362 defaults->update("RENDERFARM_PORT", renderfarm_port);
363 defaults->update("RENDERFARM_PREROLL", render_preroll);
364 defaults->update("BRENDER_PREROLL", brender_preroll);
365 // defaults->update("RENDERFARM_VFS", renderfarm_vfs);
366 defaults->update("RENDERFARM_MOUNTPOINT", renderfarm_mountpoint);
367 defaults->update("RENDERFARM_JOBS_COUNT", renderfarm_job_count);
368 defaults->update("RENDERFARM_CONSOLIDATE", renderfarm_consolidate);
369 defaults->update("RENDERFARM_TOTAL", (int64_t)renderfarm_nodes.total);
370 for(int i = 0; i < renderfarm_nodes.total; i++)
372 sprintf(string, "RENDERFARM_NODE%d", i);
373 defaults->update(string, renderfarm_nodes.values[i]);
374 sprintf(string, "RENDERFARM_PORT%d", i);
375 defaults->update(string, renderfarm_ports.values[i]);
376 sprintf(string, "RENDERFARM_ENABLED%d", i);
377 defaults->update(string, renderfarm_enabled.values[i]);
378 sprintf(string, "RENDERFARM_RATE%d", i);
379 defaults->update(string, renderfarm_rate.values[i]);
385 void Preferences::add_node(char *text, int port, int enabled, float rate)
387 if(text[0] == 0) return;
389 preferences_lock->lock("Preferences::add_node");
390 char *new_item = new char[strlen(text) + 1];
391 strcpy(new_item, text);
392 renderfarm_nodes.append(new_item);
393 renderfarm_nodes.set_array_delete();
394 renderfarm_ports.append(port);
395 renderfarm_enabled.append(enabled);
396 renderfarm_rate.append(rate);
397 preferences_lock->unlock();
400 void Preferences::delete_node(int number)
402 preferences_lock->lock("Preferences::delete_node");
403 if(number < renderfarm_nodes.total)
405 delete [] renderfarm_nodes.values[number];
406 renderfarm_nodes.remove_number(number);
407 renderfarm_ports.remove_number(number);
408 renderfarm_enabled.remove_number(number);
409 renderfarm_rate.remove_number(number);
411 preferences_lock->unlock();
414 void Preferences::delete_nodes()
416 preferences_lock->lock("Preferences::delete_nodes");
417 for(int i = 0; i < renderfarm_nodes.total; i++)
418 delete [] renderfarm_nodes.values[i];
419 renderfarm_nodes.remove_all();
420 renderfarm_ports.remove_all();
421 renderfarm_enabled.remove_all();
422 renderfarm_rate.remove_all();
423 preferences_lock->unlock();
426 void Preferences::reset_rates()
428 for(int i = 0; i < renderfarm_nodes.total; i++)
430 renderfarm_rate.values[i] = 0.0;
435 void Preferences::set_rate(float rate, int node)
437 //printf("Preferences::set_rate %f %d\n", rate, node);
445 for(int i = 0; i < renderfarm_nodes.total; i++)
447 if(renderfarm_enabled.values[i]) total++;
448 if(total == node + 1)
450 renderfarm_rate.values[i] = rate;
457 float Preferences::get_avg_rate(int use_master_node)
459 preferences_lock->lock("Preferences::get_avg_rate");
461 if(renderfarm_rate.total)
466 if(EQUIV(local_rate, 0.0))
468 preferences_lock->unlock();
478 for(int i = 0; i < renderfarm_rate.total; i++)
480 if(renderfarm_enabled.values[i])
483 total += renderfarm_rate.values[i];
484 if(EQUIV(renderfarm_rate.values[i], 0.0))
486 preferences_lock->unlock();
497 preferences_lock->unlock();
502 void Preferences::sort_nodes()
509 for(int i = 0; i < renderfarm_nodes.total - 1; i++)
511 if(strcmp(renderfarm_nodes.values[i], renderfarm_nodes.values[i + 1]) > 0)
513 char *temp = renderfarm_nodes.values[i];
514 int temp_port = renderfarm_ports.values[i];
516 renderfarm_nodes.values[i] = renderfarm_nodes.values[i + 1];
517 renderfarm_nodes.values[i + 1] = temp;
519 renderfarm_ports.values[i] = renderfarm_ports.values[i + 1];
520 renderfarm_ports.values[i + 1] = temp_port;
522 renderfarm_enabled.values[i] = renderfarm_enabled.values[i + 1];
523 renderfarm_enabled.values[i + 1] = temp_port;
525 renderfarm_rate.values[i] = renderfarm_rate.values[i + 1];
526 renderfarm_rate.values[i + 1] = temp_port;
533 void Preferences::edit_node(int number,
538 char *new_item = new char[strlen(new_text) + 1];
539 strcpy(new_item, new_text);
541 delete [] renderfarm_nodes.values[number];
542 renderfarm_nodes.values[number] = new_item;
543 renderfarm_ports.values[number] = new_port;
544 renderfarm_enabled.values[number] = new_enabled;
547 int Preferences::get_enabled_nodes()
550 for(int i = 0; i < renderfarm_enabled.total; i++)
551 if(renderfarm_enabled.values[i]) result++;
555 char* Preferences::get_node_hostname(int number)
558 for(int i = 0; i < renderfarm_nodes.total; i++)
560 if(renderfarm_enabled.values[i])
563 return renderfarm_nodes.values[i];
571 int Preferences::get_node_port(int number)
574 for(int i = 0; i < renderfarm_ports.total; i++)
576 if(renderfarm_enabled.values[i])
579 return renderfarm_ports.values[i];
588 int Preferences::calculate_processors(int interactive)
590 /* Get processor count */
594 if(force_uniprocessor && !interactive) return 1;
596 if(proc = fopen("/proc/cpuinfo", "r"))
598 char string[BCTEXTLEN];
601 fgets(string, BCTEXTLEN, proc);
602 if(!strncasecmp(string, "processor", 9))
604 char *ptr = strchr(string, ':');
608 result = atol(ptr) + 1;
612 if(!strncasecmp(string, "cpus detected", 13))
614 char *ptr = strchr(string, ':');