4 * Copyright (C) 2008 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
23 #include "audioconfig.h"
24 #include "audiodevice.inc"
25 #include "bcmeter.inc"
26 #include "bcsignals.h"
31 #include "filesystem.h"
33 #include "indexfile.h"
35 #include "preferences.h"
36 #include "shbtnprefs.h"
38 #include "videoconfig.h"
39 #include "videodevice.inc"
43 #include <sys/types.h>
47 //#define CLAMP(x, y, z) (x) = ((x) < (y) ? (y) : ((x) > (z) ? (z) : (x)))
49 Preferences::Preferences()
54 preferences_lock = new Mutex("Preferences::preferences_lock");
57 // initial plugin path from build -DPLUGIN_DIR="..."
58 sprintf(plugin_dir, "%s/", File::get_plugin_path());
59 sprintf(index_directory, "%s/", File::get_config_path());
60 if(strlen(index_directory))
61 fs.complete_path(index_directory);
62 cache_size = 0x1000000;
63 index_size = 0x400000;
66 keyframe_reticle = HAIRLINE_DRAGGING;
71 force_uniprocessor = 0;
72 renderfarm_port = DEAMON_PORT;
75 renderfarm_mountpoint[0] = 0;
77 renderfarm_job_count = 20;
78 processors = calculate_processors(0);
79 real_processors = calculate_processors(1);
80 ffmpeg_early_probe = 0;
81 ffmpeg_marker_indexes = 1;
83 dvd_yuv420p_interlace = 0;
85 // Default brender asset
86 brender_asset = new Asset;
87 brender_asset->audio_data = 0;
88 brender_asset->video_data = 1;
89 sprintf(brender_asset->path, "/tmp/brender");
90 brender_asset->format = FILE_JPEG_LIST;
91 brender_asset->jpeg_quality = 80;
101 android_port = 23432;
102 strcpy(android_pin, "cinelerra");
104 for(int i = 0; i < MAXCHANNELS; i++)
106 for(int j = 0; j < i + 1; j++)
108 int position = 180 - (360 * j / (i + 1));
109 while(position < 0) position += 360;
110 channel_positions[i * MAXCHANNELS + j] = position;
115 Preferences::~Preferences()
117 brender_asset->Garbage::remove_user();
118 shbtn_prefs.remove_all_objects();
119 renderfarm_nodes.remove_all_objects();
120 delete preferences_lock;
123 void Preferences::copy_rates_from(Preferences *preferences)
125 preferences_lock->lock("Preferences::copy_rates_from");
126 // Need to match node titles in case the order changed and in case
127 // one of the nodes in the source is the master node.
128 local_rate = preferences->local_rate;
131 j < preferences->renderfarm_nodes.total;
134 double new_rate = preferences->renderfarm_rate.values[j];
135 // Put in the master node
136 if(preferences->renderfarm_nodes.values[j][0] == '/')
138 if(!EQUIV(new_rate, 0.0))
139 local_rate = new_rate;
142 // Search for local node and copy it to that node
143 if(!EQUIV(new_rate, 0.0))
145 for(int i = 0; i < renderfarm_nodes.total; i++)
147 if(!strcmp(preferences->renderfarm_nodes.values[j], renderfarm_nodes.values[i]) &&
148 preferences->renderfarm_ports.values[j] == renderfarm_ports.values[i])
150 renderfarm_rate.values[i] = new_rate;
157 //printf("Preferences::copy_rates_from 1 %f %f\n", local_rate, preferences->local_rate);
158 preferences_lock->unlock();
161 void Preferences::copy_from(Preferences *that)
163 // ================================= Performance ================================
164 strcpy(index_directory, that->index_directory);
165 index_size = that->index_size;
166 index_count = that->index_count;
167 use_thumbnails = that->use_thumbnails;
168 keyframe_reticle = that->keyframe_reticle;
169 strcpy(theme, that->theme);
171 use_tipwindow = that->use_tipwindow;
172 scan_commercials = that->scan_commercials;
173 android_remote = that->android_remote;
174 android_port = that->android_port;
175 strcpy(android_pin, that->android_pin);
176 this->shbtn_prefs.remove_all_objects();
177 for( int i=0; i<that->shbtn_prefs.size(); ++i )
178 this->shbtn_prefs.append(new ShBtnPref(*that->shbtn_prefs[i]));
179 cache_size = that->cache_size;
180 force_uniprocessor = that->force_uniprocessor;
181 trap_sigsegv = that->trap_sigsegv;
182 trap_sigintr = that->trap_sigintr;
183 processors = that->processors;
184 real_processors = that->real_processors;
185 ffmpeg_early_probe = that->ffmpeg_early_probe;
186 ffmpeg_marker_indexes = that->ffmpeg_marker_indexes;
187 warn_indexes = that->warn_indexes;
188 dvd_yuv420p_interlace = that->dvd_yuv420p_interlace;
189 renderfarm_nodes.remove_all_objects();
190 renderfarm_ports.remove_all();
191 renderfarm_enabled.remove_all();
192 renderfarm_rate.remove_all();
193 local_rate = that->local_rate;
194 for(int i = 0; i < that->renderfarm_nodes.size(); i++)
196 add_node(that->renderfarm_nodes.get(i),
197 that->renderfarm_ports.get(i),
198 that->renderfarm_enabled.get(i),
199 that->renderfarm_rate.get(i));
201 use_renderfarm = that->use_renderfarm;
202 renderfarm_port = that->renderfarm_port;
203 render_preroll = that->render_preroll;
204 brender_preroll = that->brender_preroll;
205 renderfarm_job_count = that->renderfarm_job_count;
206 renderfarm_vfs = that->renderfarm_vfs;
207 strcpy(renderfarm_mountpoint, that->renderfarm_mountpoint);
208 renderfarm_consolidate = that->renderfarm_consolidate;
209 use_brender = that->use_brender;
210 brender_fragment = that->brender_fragment;
211 brender_asset->copy_from(that->brender_asset, 0);
216 if(strlen(index_directory))
218 fs.complete_path(index_directory);
219 fs.add_end_slash(index_directory);
222 // if(strlen(global_plugin_dir))
224 // fs.complete_path(global_plugin_dir);
225 // fs.add_end_slash(global_plugin_dir);
229 // Redo with the proper value of force_uniprocessor
230 processors = calculate_processors(0);
234 void Preferences::boundaries()
236 renderfarm_job_count = MAX(renderfarm_job_count, 1);
237 CLAMP(cache_size, MIN_CACHE_SIZE, MAX_CACHE_SIZE);
240 Preferences& Preferences::operator=(Preferences &that)
242 printf("Preferences::operator=\n");
247 void Preferences::print_channels(char *string,
248 int *channel_positions,
251 char string3[BCTEXTLEN];
253 for(int j = 0; j < channels; j++)
255 sprintf(string3, "%d", channel_positions[j]);
256 strcat(string, string3);
262 void Preferences::scan_channels(char *string,
263 int *channel_positions,
266 char string2[BCTEXTLEN];
267 int len = strlen(string);
268 int current_channel = 0;
269 for(int i = 0; i < len; i++)
271 strcpy(string2, &string[i]);
272 for(int j = 0; j < BCTEXTLEN; j++)
274 if(string2[j] == ',' || string2[j] == 0)
281 channel_positions[current_channel++] = atoi(string2);
282 if(current_channel >= channels) break;
286 int Preferences::load_defaults(BC_Hash *defaults)
288 char string[BCTEXTLEN];
290 use_tipwindow = defaults->get("USE_TIPWINDOW", use_tipwindow);
291 scan_commercials = defaults->get("SCAN_COMMERCIALS", scan_commercials);
292 android_remote = defaults->get("ANDROID_REMOTE", android_remote);
293 android_port = defaults->get("ANDROID_PORT", android_port);
294 defaults->get("ANDROID_PIN", android_pin);
295 defaults->get("INDEX_DIRECTORY", index_directory);
296 index_size = defaults->get("INDEX_SIZE", index_size);
297 index_count = defaults->get("INDEX_COUNT", index_count);
298 use_thumbnails = defaults->get("USE_THUMBNAILS", use_thumbnails);
299 keyframe_reticle = defaults->get("KEYFRAME_RETICLE", keyframe_reticle);
300 trap_sigsegv = defaults->get("TRAP_SIGSEGV", trap_sigsegv);
301 trap_sigintr = defaults->get("TRAP_SIGINTR", trap_sigintr);
303 strcpy(theme, DEFAULT_THEME);
304 defaults->get("THEME", theme);
306 for(int i = 0; i < MAXCHANNELS; i++)
308 char string2[BCTEXTLEN];
309 sprintf(string, "CHANNEL_POSITIONS%d", i);
310 print_channels(string2,
311 &channel_positions[i * MAXCHANNELS],
314 defaults->get(string, string2);
316 scan_channels(string2,
317 &channel_positions[i * MAXCHANNELS],
321 brender_asset->load_defaults(defaults,
331 force_uniprocessor = defaults->get("FORCE_UNIPROCESSOR", force_uniprocessor);
332 ffmpeg_early_probe = defaults->get("FFMPEG_EARLY_PROBE", ffmpeg_early_probe);
333 ffmpeg_marker_indexes = defaults->get("FFMPEG_MARKER_INDEXES", ffmpeg_marker_indexes);
334 warn_indexes = defaults->get("WARN_INDEXES", warn_indexes);
335 dvd_yuv420p_interlace = defaults->get("DVD_YUV420P_INTERLACE", dvd_yuv420p_interlace);
336 use_brender = defaults->get("USE_BRENDER", use_brender);
337 brender_fragment = defaults->get("BRENDER_FRAGMENT", brender_fragment);
338 cache_size = defaults->get("CACHE_SIZE", cache_size);
339 local_rate = defaults->get("LOCAL_RATE", local_rate);
340 use_renderfarm = defaults->get("USE_RENDERFARM", use_renderfarm);
341 renderfarm_port = defaults->get("RENDERFARM_PORT", renderfarm_port);
342 render_preroll = defaults->get("RENDERFARM_PREROLL", render_preroll);
343 brender_preroll = defaults->get("BRENDER_PREROLL", brender_preroll);
344 renderfarm_job_count = defaults->get("RENDERFARM_JOBS_COUNT", renderfarm_job_count);
345 renderfarm_consolidate = defaults->get("RENDERFARM_CONSOLIDATE", renderfarm_consolidate);
346 // renderfarm_vfs = defaults->get("RENDERFARM_VFS", renderfarm_vfs);
347 defaults->get("RENDERFARM_MOUNTPOINT", renderfarm_mountpoint);
348 int renderfarm_total = defaults->get("RENDERFARM_TOTAL", 0);
350 for(int i = 0; i < renderfarm_total; i++)
352 sprintf(string, "RENDERFARM_NODE%d", i);
353 char result[BCTEXTLEN];
355 int result_enabled = 0;
356 float result_rate = 0.0;
359 defaults->get(string, result);
361 sprintf(string, "RENDERFARM_PORT%d", i);
362 result_port = defaults->get(string, renderfarm_port);
364 sprintf(string, "RENDERFARM_ENABLED%d", i);
365 result_enabled = defaults->get(string, result_enabled);
367 sprintf(string, "RENDERFARM_RATE%d", i);
368 result_rate = defaults->get(string, result_rate);
372 add_node(result, result_port, result_enabled, result_rate);
376 shbtn_prefs.remove_all_objects();
377 int shbtns_total = defaults->get("SHBTNS_TOTAL", -1);
378 if( shbtns_total < 0 ) {
379 shbtn_prefs.append(new ShBtnPref("Features5", "firefox file://$CIN_DAT/doc/Features5.pdf", 0));
380 shbtn_prefs.append(new ShBtnPref("Online Help", "firefox https://cinelerra-cv.org/docs.php", 0));
381 shbtn_prefs.append(new ShBtnPref("Orignal Manual", "firefox file://$CIN_DAT/doc/cinelerra.html", 0));
384 for( int i=0; i<shbtns_total; ++i ) {
385 char name[BCTEXTLEN], commands[BCTEXTLEN];
386 sprintf(string, "SHBTN%d_NAME", i);
387 defaults->get(string, name);
388 sprintf(string, "SHBTN%d_COMMANDS", i);
389 defaults->get(string, commands);
390 sprintf(string, "SHBTN%d_WARN", i);
391 int warn = defaults->get(string, 0);
392 shbtn_prefs.append(new ShBtnPref(name, commands, warn));
395 // Redo with the proper value of force_uniprocessor
396 processors = calculate_processors(0);
401 int Preferences::save_defaults(BC_Hash *defaults)
403 char string[BCTEXTLEN];
406 defaults->update("USE_TIPWINDOW", use_tipwindow);
407 defaults->update("SCAN_COMMERCIALS", scan_commercials);
408 defaults->update("ANDROID_REMOTE", android_remote);
409 defaults->update("ANDROID_PIN", android_pin);
410 defaults->update("ANDROID_PORT", android_port);
412 defaults->update("CACHE_SIZE", cache_size);
413 defaults->update("INDEX_DIRECTORY", index_directory);
414 defaults->update("INDEX_SIZE", index_size);
415 defaults->update("INDEX_COUNT", index_count);
416 defaults->update("USE_THUMBNAILS", use_thumbnails);
417 defaults->update("KEYFRAME_RETICLE", keyframe_reticle);
418 defaults->update("TRAP_SIGSEGV", trap_sigsegv);
419 defaults->update("TRAP_SIGINTR", trap_sigintr);
420 // defaults->update("GLOBAL_PLUGIN_DIR", global_plugin_dir);
421 defaults->update("THEME", theme);
424 for(int i = 0; i < MAXCHANNELS; i++)
426 char string2[BCTEXTLEN];
427 sprintf(string, "CHANNEL_POSITIONS%d", i);
428 print_channels(string2, &channel_positions[i * MAXCHANNELS], i + 1);
429 defaults->update(string, string2);
432 defaults->update("FORCE_UNIPROCESSOR", force_uniprocessor);
433 defaults->update("FFMPEG_EARLY_PROBE", ffmpeg_early_probe);
434 defaults->update("FFMPEG_MARKER_INDEXES", ffmpeg_marker_indexes);
435 defaults->update("WARN_INDEXES", warn_indexes);
436 defaults->update("DVD_YUV420P_INTERLACE", dvd_yuv420p_interlace);
437 brender_asset->save_defaults(defaults,
444 defaults->update("USE_BRENDER", use_brender);
445 defaults->update("BRENDER_FRAGMENT", brender_fragment);
446 defaults->update("USE_RENDERFARM", use_renderfarm);
447 defaults->update("LOCAL_RATE", local_rate);
448 defaults->update("RENDERFARM_PORT", renderfarm_port);
449 defaults->update("RENDERFARM_PREROLL", render_preroll);
450 defaults->update("BRENDER_PREROLL", brender_preroll);
451 // defaults->update("RENDERFARM_VFS", renderfarm_vfs);
452 defaults->update("RENDERFARM_MOUNTPOINT", renderfarm_mountpoint);
453 defaults->update("RENDERFARM_JOBS_COUNT", renderfarm_job_count);
454 defaults->update("RENDERFARM_CONSOLIDATE", renderfarm_consolidate);
455 defaults->update("RENDERFARM_TOTAL", (int64_t)renderfarm_nodes.total);
456 for(int i = 0; i < renderfarm_nodes.total; i++)
458 sprintf(string, "RENDERFARM_NODE%d", i);
459 defaults->update(string, renderfarm_nodes.values[i]);
460 sprintf(string, "RENDERFARM_PORT%d", i);
461 defaults->update(string, renderfarm_ports.values[i]);
462 sprintf(string, "RENDERFARM_ENABLED%d", i);
463 defaults->update(string, renderfarm_enabled.values[i]);
464 sprintf(string, "RENDERFARM_RATE%d", i);
465 defaults->update(string, renderfarm_rate.values[i]);
467 defaults->update("SHBTNS_TOTAL", shbtn_prefs.size());
468 for( int i=0; i<shbtn_prefs.size(); ++i ) {
469 ShBtnPref *pref = shbtn_prefs[i];
470 sprintf(string, "SHBTN%d_NAME", i);
471 defaults->update(string, pref->name);
472 sprintf(string, "SHBTN%d_COMMANDS", i);
473 defaults->update(string, pref->commands);
474 sprintf(string, "SHBTN%d_WARN", i);
475 defaults->update(string, pref->warn);
481 void Preferences::add_node(const char *text, int port, int enabled, float rate)
483 if(text[0] == 0) return;
485 preferences_lock->lock("Preferences::add_node");
486 char *new_item = new char[strlen(text) + 1];
487 strcpy(new_item, text);
488 renderfarm_nodes.append(new_item);
489 renderfarm_nodes.set_array_delete();
490 renderfarm_ports.append(port);
491 renderfarm_enabled.append(enabled);
492 renderfarm_rate.append(rate);
493 preferences_lock->unlock();
496 void Preferences::delete_node(int number)
498 preferences_lock->lock("Preferences::delete_node");
499 if(number < renderfarm_nodes.total && number >= 0)
501 delete [] renderfarm_nodes.values[number];
502 renderfarm_nodes.remove_number(number);
503 renderfarm_ports.remove_number(number);
504 renderfarm_enabled.remove_number(number);
505 renderfarm_rate.remove_number(number);
507 preferences_lock->unlock();
510 void Preferences::delete_nodes()
512 preferences_lock->lock("Preferences::delete_nodes");
513 for(int i = 0; i < renderfarm_nodes.total; i++)
514 delete [] renderfarm_nodes.values[i];
515 renderfarm_nodes.remove_all();
516 renderfarm_ports.remove_all();
517 renderfarm_enabled.remove_all();
518 renderfarm_rate.remove_all();
519 preferences_lock->unlock();
522 void Preferences::reset_rates()
524 for(int i = 0; i < renderfarm_nodes.total; i++)
526 renderfarm_rate.values[i] = 0.0;
531 float Preferences::get_rate(int node)
540 for(int i = 0; i < renderfarm_nodes.size(); i++)
542 if(renderfarm_enabled.get(i)) total++;
543 if(total == node + 1)
545 return renderfarm_rate.get(i);
553 void Preferences::set_rate(float rate, int node)
555 //printf("Preferences::set_rate %f %d\n", rate, node);
563 for(int i = 0; i < renderfarm_nodes.size(); i++)
565 if(renderfarm_enabled.get(i)) total++;
566 if(total == node + 1)
568 renderfarm_rate.set(i, rate);
575 float Preferences::get_avg_rate(int use_master_node)
577 preferences_lock->lock("Preferences::get_avg_rate");
579 if(renderfarm_rate.total)
584 if(EQUIV(local_rate, 0.0))
586 preferences_lock->unlock();
596 for(int i = 0; i < renderfarm_rate.total; i++)
598 if(renderfarm_enabled.values[i])
601 total += renderfarm_rate.values[i];
602 if(EQUIV(renderfarm_rate.values[i], 0.0))
604 preferences_lock->unlock();
615 preferences_lock->unlock();
620 void Preferences::sort_nodes()
627 for(int i = 0; i < renderfarm_nodes.total - 1; i++)
629 if(strcmp(renderfarm_nodes.values[i], renderfarm_nodes.values[i + 1]) > 0)
631 char *temp = renderfarm_nodes.values[i];
632 int temp_port = renderfarm_ports.values[i];
634 renderfarm_nodes.values[i] = renderfarm_nodes.values[i + 1];
635 renderfarm_nodes.values[i + 1] = temp;
637 renderfarm_ports.values[i] = renderfarm_ports.values[i + 1];
638 renderfarm_ports.values[i + 1] = temp_port;
640 renderfarm_enabled.values[i] = renderfarm_enabled.values[i + 1];
641 renderfarm_enabled.values[i + 1] = temp_port;
643 renderfarm_rate.values[i] = renderfarm_rate.values[i + 1];
644 renderfarm_rate.values[i + 1] = temp_port;
651 void Preferences::edit_node(int number,
652 const char *new_text,
656 char *new_item = new char[strlen(new_text) + 1];
657 strcpy(new_item, new_text);
659 delete [] renderfarm_nodes.values[number];
660 renderfarm_nodes.values[number] = new_item;
661 renderfarm_ports.values[number] = new_port;
662 renderfarm_enabled.values[number] = new_enabled;
665 int Preferences::get_enabled_nodes()
668 for(int i = 0; i < renderfarm_enabled.total; i++)
669 if(renderfarm_enabled.values[i]) result++;
673 const char* Preferences::get_node_hostname(int number)
676 for(int i = 0; i < renderfarm_nodes.total; i++)
678 if(renderfarm_enabled.values[i])
681 return renderfarm_nodes.values[i];
689 int Preferences::get_node_port(int number)
692 for(int i = 0; i < renderfarm_ports.total; i++)
694 if(renderfarm_enabled.values[i])
697 return renderfarm_ports.values[i];
705 int Preferences::get_asset_file_path(Asset *asset, char *path)
707 strcpy(path, asset->path);
708 int result = !access(path, R_OK) ? 0 : -1;
709 if( !result && ( asset->format == FILE_MPEG ||
710 asset->format == FILE_VMPEG || asset->format == FILE_AMPEG ) ) {
711 char source_filename[BCTEXTLEN], index_filename[BCTEXTLEN];
712 IndexFile::get_index_filename(source_filename,
713 index_directory, index_filename, asset->path, ".toc");
714 strcpy(path, index_filename);
715 if( access(path, R_OK) )
718 // result = 0, asset->path/toc exist, -1 no asset, 1 no toc
723 int Preferences::calculate_processors(int interactive)
725 if(force_uniprocessor && !interactive) return 1;
726 return BC_WindowBase::get_resources()->machine_cpus;