r1006: configure: Use libx264_pic instead of libx264 if available.
[cinelerra_cv/mob.git] / cinelerra / preferences.C
blobb40e1d5c96468e1f39e7601e209c841dc50f73d8
1 #include "asset.h"
2 #include "audioconfig.h"
3 #include "audiodevice.inc"
4 #include "bcmeter.inc"
5 #include "cache.inc"
6 #include "clip.h"
7 #include "bchash.h"
8 #include "file.inc"
9 #include "filesystem.h"
10 #include "guicast.h"
11 #include "mutex.h"
12 #include "preferences.h"
13 #include "theme.h"
14 #include "videoconfig.h"
15 #include "videodevice.inc"
16 #include <string.h>
18 //#define CLAMP(x, y, z) (x) = ((x) < (y) ? (y) : ((x) > (z) ? (z) : (x)))
31 Preferences::Preferences()
33 // Set defaults
34         FileSystem fs;
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;
42         index_count = 100;
43         use_thumbnails = 1;
44         theme[0] = 0;
45         use_renderfarm = 0;
46         force_uniprocessor = 0;
47         renderfarm_port = DEAMON_PORT;
48         render_preroll = 0.5;
49         brender_preroll = 0;
50         renderfarm_mountpoint[0] = 0;
51         renderfarm_vfs = 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;
64         use_brender = 0;
65         brender_fragment = 1;
66         local_rate = 0.0;
68         use_tipwindow = 1;
70         for(int i = 0; i < MAXCHANNELS; i++)
71         {
72                 for(int j = 0; j < i + 1; j++)
73                 {
74                         int position = 180 - (360 * j / (i + 1));
75                         while(position < 0) position += 360;
76                         channel_positions[i * MAXCHANNELS + j] = position;
77                 }
78         }
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;
94         for(int j = 0; 
95                 j < preferences->renderfarm_nodes.total; 
96                 j++)
97         {
98                 double new_rate = preferences->renderfarm_rate.values[j];
99 // Put in the master node
100                 if(preferences->renderfarm_nodes.values[j][0] == '/')
101                 {
102                         if(!EQUIV(new_rate, 0.0))
103                                 local_rate = new_rate;
104                 }
105                 else
106 // Search for local node and copy it to that node
107                 if(!EQUIV(new_rate, 0.0))
108                 {
109                         for(int i = 0; i < renderfarm_nodes.total; i++)
110                         {
111                                 if(!strcmp(preferences->renderfarm_nodes.values[j], renderfarm_nodes.values[i]) &&
112                                         preferences->renderfarm_ports.values[j] == renderfarm_ports.values[i])
113                                 {
114                                         renderfarm_rate.values[i] = new_rate;
115                                         break;
116                                 }
117                         }
118                 }
119         }
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++)
147         {
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]);
152         }
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;
165 // Check boundaries
167         FileSystem fs;
168         if(strlen(index_directory))
169         {
170                 fs.complete_path(index_directory);
171                 fs.add_end_slash(index_directory);
172         }
173         
174         if(strlen(global_plugin_dir))
175         {
176                 fs.complete_path(global_plugin_dir);
177                 fs.add_end_slash(global_plugin_dir);
178         }
180         boundaries();
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");
192         copy_from(&that);
193         return *this;
196 void Preferences::print_channels(char *string, 
197         int *channel_positions, 
198         int channels)
200         char string3[BCTEXTLEN];
201         string[0] = 0;
202         for(int j = 0; j < channels; j++)
203         {
204                 sprintf(string3, "%d", channel_positions[j]);
205                 strcat(string, string3);
206                 if(j < channels - 1)
207                         strcat(string, ",");
208         }
211 void Preferences::scan_channels(char *string, 
212         int *channel_positions, 
213         int channels)
215         char string2[BCTEXTLEN];
216         int len = strlen(string);
217         int current_channel = 0;
218         for(int i = 0; i < len; i++)
219         {
220                 strcpy(string2, &string[i]);
221                 for(int j = 0; j < BCTEXTLEN; j++)
222                 {
223                         if(string2[j] == ',' || string2[j] == 0)
224                         {
225                                 i += j;
226                                 string2[j] = 0;
227                                 break;
228                         }
229                 }
230                 channel_positions[current_channel++] = atoi(string2);
231                 if(current_channel >= channels) break;
232         }
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"))
248         {
249                 strcpy(global_plugin_dir, getenv("GLOBAL_PLUGIN_DIR"));
250         }
252         strcpy(theme, DEFAULT_THEME);
253         defaults->get("THEME", theme);
255         for(int i = 0; i < MAXCHANNELS; i++)
256         {
257                 char string2[BCTEXTLEN];
258                 sprintf(string, "CHANNEL_POSITIONS%d", i);
259                 print_channels(string2, 
260                         &channel_positions[i * MAXCHANNELS], 
261                         i + 1);
263                 defaults->get(string, string2);
264                 
265                 scan_channels(string2,
266                         &channel_positions[i * MAXCHANNELS], 
267                         i + 1);
268         }
270         brender_asset->load_defaults(defaults, 
271                 "BRENDER_", 
272                 1,
273                 1,
274                 1,
275                 0,
276                 0);
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++)
296         {
297                 sprintf(string, "RENDERFARM_NODE%d", i);
298                 char result[BCTEXTLEN];
299                 int result_port = 0;
300                 int result_enabled = 0;
301                 float result_rate = 0.0;
303                 result[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);
315                 if(result[0] != 0)
316                 {
317                         add_node(result, result_port, result_enabled, result_rate);
318                 }
319         }
321         boundaries();
323         return 0;
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++)
343         {
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);
348         }
350         defaults->update("FORCE_UNIPROCESSOR", force_uniprocessor);
351         brender_asset->save_defaults(defaults, 
352                 "BRENDER_",
353                 1,
354                 1,
355                 1,
356                 0,
357                 0);
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++)
371         {
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]);
380         }
381         return 0;
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)
404         {
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);
410         }
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++)
429         {
430                 renderfarm_rate.values[i] = 0.0;
431         }
432         local_rate = 0.0;
435 void Preferences::set_rate(float rate, int node)
437 //printf("Preferences::set_rate %f %d\n", rate, node);
438         if(node < 0)
439         {
440                 local_rate = rate;
441         }
442         else
443         {
444                 int total = 0;
445                 for(int i = 0; i < renderfarm_nodes.total; i++)
446                 {
447                         if(renderfarm_enabled.values[i]) total++;
448                         if(total == node + 1)
449                         {
450                                 renderfarm_rate.values[i] = rate;
451                                 return;
452                         }
453                 }
454         }
457 float Preferences::get_avg_rate(int use_master_node)
459         preferences_lock->lock("Preferences::get_avg_rate");
460         float total = 0.0;
461         if(renderfarm_rate.total)
462         {
463                 int enabled = 0;
464                 if(use_master_node)
465                 {
466                         if(EQUIV(local_rate, 0.0))
467                         {
468                                 preferences_lock->unlock();
469                                 return 0.0;
470                         }
471                         else
472                         {
473                                 enabled++;
474                                 total += local_rate;
475                         }
476                 }
478                 for(int i = 0; i < renderfarm_rate.total; i++)
479                 {
480                         if(renderfarm_enabled.values[i])
481                         {
482                                 enabled++;
483                                 total += renderfarm_rate.values[i];
484                                 if(EQUIV(renderfarm_rate.values[i], 0.0)) 
485                                 {
486                                         preferences_lock->unlock();
487                                         return 0.0;
488                                 }
489                         }
490                 }
492                 if(enabled)
493                         total /= enabled;
494                 else
495                         total = 0.0;
496         }
497         preferences_lock->unlock();
499         return total;
502 void Preferences::sort_nodes()
504         int done = 0;
506         while(!done)
507         {
508                 done = 1;
509                 for(int i = 0; i < renderfarm_nodes.total - 1; i++)
510                 {
511                         if(strcmp(renderfarm_nodes.values[i], renderfarm_nodes.values[i + 1]) > 0)
512                         {
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;
527                                 done = 0;
528                         }
529                 }
530         }
533 void Preferences::edit_node(int number, 
534         char *new_text, 
535         int new_port, 
536         int new_enabled)
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()
549         int result = 0;
550         for(int i = 0; i < renderfarm_enabled.total; i++)
551                 if(renderfarm_enabled.values[i]) result++;
552         return result;
555 char* Preferences::get_node_hostname(int number)
557         int total = 0;
558         for(int i = 0; i < renderfarm_nodes.total; i++)
559         {
560                 if(renderfarm_enabled.values[i])
561                 {
562                         if(total == number)
563                                 return renderfarm_nodes.values[i];
564                         else
565                                 total++;
566                 }
567         }
568         return "";
571 int Preferences::get_node_port(int number)
573         int total = 0;
574         for(int i = 0; i < renderfarm_ports.total; i++)
575         {
576                 if(renderfarm_enabled.values[i])
577                 {
578                         if(total == number)
579                                 return renderfarm_ports.values[i];
580                         else
581                                 total++;
582                 }
583         }
584         return -1;
588 int Preferences::calculate_processors(int interactive)
590 /* Get processor count */
591         int result = 1;
592         FILE *proc;
594         if(force_uniprocessor && !interactive) return 1;
596         if(proc = fopen("/proc/cpuinfo", "r"))
597         {
598                 char string[BCTEXTLEN];
599                 while(!feof(proc))
600                 {
601                         fgets(string, BCTEXTLEN, proc);
602                         if(!strncasecmp(string, "processor", 9))
603                         {
604                                 char *ptr = strchr(string, ':');
605                                 if(ptr)
606                                 {
607                                         ptr++;
608                                         result = atol(ptr) + 1;
609                                 }
610                         }
611                         else
612                         if(!strncasecmp(string, "cpus detected", 13))
613                         {
614                                 char *ptr = strchr(string, ':');
615                                 if(ptr)
616                                 {
617                                         ptr++;
618                                         result = atol(ptr);
619                                 }
620                         }
621                 }
622                 fclose(proc);
623         }
625         return result;