6 #include "edlsession.h"
8 #include "filesystem.h"
10 #include "preferences.h"
14 // edl came from a command which won't exist anymore
15 CICache::CICache(EDL *edl,
16 Preferences *preferences,
17 ArrayList<PluginServer*> *plugindb)
21 this->edl->create_objects();
23 this->plugindb = plugindb;
24 this->preferences = preferences;
25 check_in_lock = new Mutex("CICache::check_in_lock");
26 check_out_lock = new Mutex("CICache::check_out_lock");
27 total_lock = new Mutex("CICache::total_lock");
32 while(last) delete last;
35 delete check_out_lock;
39 void CICache::set_edl(EDL *edl)
44 void CICache::update(File* &file)
47 for(CICacheItem *current = first; current; current = NEXT)
49 if(!current->asset->test_path(file->asset->path))
51 if(file != current->file)
61 append(item = new CICacheItem(this, file));
62 item->asset = new Asset(*(file->asset));
63 file->set_asset(item->asset);
66 File* CICache::check_out(Asset *asset)
70 check_out_lock->lock("CICache::check_out");
72 // search for it in the cache
73 CICacheItem *current, *new_item = 0;
75 for(current = first; current && !new_item; current = NEXT)
77 if(!strcmp(current->asset->path, asset->path))
84 // didn't find it so create a new one
87 new_item = append(new CICacheItem(this, asset));
94 // opened successfully
95 new_item->item_lock->lock();
96 new_item->checked_out = 1;
98 result = new_item->file;
108 check_out_lock->unlock();
113 int CICache::check_in(Asset *asset)
115 check_in_lock->lock();
117 CICacheItem *current;
119 total_lock->lock("CICache::check_in");
120 for(current = first; current && !result; current = NEXT)
122 // Pointers are different
123 if(!strcmp(current->asset->path, asset->path))
125 current->checked_out = 0;
126 current->item_lock->unlock();
130 total_lock->unlock();
132 check_in_lock->unlock();
139 int CICache::delete_entry(char *path)
141 Asset *asset = edl->assets->get_asset(path);
142 if(asset) delete_entry(asset);
146 int CICache::delete_entry(Asset *asset)
150 CICacheItem *current, *temp;
152 for(current = first; current; current = temp)
155 if(current->asset->equivalent(*asset, 0, 0))
157 if(!current->checked_out)
163 printf("CICache::delete_entry asset checked out\n");
175 check_out_lock->lock("CICache::age");
176 CICacheItem *current;
178 for(current = first; current; current = NEXT)
183 // delete old assets if memory usage is exceeded
184 int64_t memory_usage;
188 memory_usage = get_memory_usage();
190 if(memory_usage > preferences->cache_size)
192 result = delete_oldest();
194 }while(memory_usage > preferences->cache_size && !result);
196 check_out_lock->unlock();
199 int64_t CICache::get_memory_usage()
201 CICacheItem *current;
204 for(current = first; current; current = NEXT)
212 int CICache::delete_oldest()
214 CICacheItem *current;
215 int highest_counter = 1;
216 CICacheItem *oldest = 0;
218 for(current = last; current; current = PREVIOUS)
220 if(current->counter >= highest_counter)
223 highest_counter = current->counter;
227 if(highest_counter > 1 && oldest && !oldest->checked_out)
229 total_lock->lock("CICache::delete_oldest");
231 total_lock->unlock();
236 return 1; // nothing was old enough to delete
243 CICacheItem *current;
245 for(current = first; current; current = NEXT)
247 printf("cache item %x\n", current);
248 printf(" asset %x\n", current->asset);
249 printf(" %s\n", current->asset->path);
250 printf(" counter %lld\n", current->counter);
253 printf("total size %lld\n", get_memory_usage());
257 int CICache::lock_all()
259 check_in_lock->lock("CICache::lock_all");
260 check_out_lock->lock("CICache::lock_all");
263 int CICache::unlock_all()
265 check_in_lock->unlock();
266 check_out_lock->unlock();
279 // File not already opened.
280 CICacheItem::CICacheItem(CICache *cache, Asset *asset)
281 : ListItem<CICacheItem>()
285 this->asset = new Asset;
286 item_lock = new Mutex("CICacheItem::item_lock");
288 // Must copy Asset since this belongs to an EDL which won't exist forever.
289 *this->asset = *asset;
294 file->set_processors(cache->preferences->processors);
295 file->set_preload(cache->edl->session->playback_preload);
298 // Copy decoding parameters from session to asset so file can see them.
299 this->asset->divx_use_deblocking = cache->edl->session->mpeg4_deblock;
303 if(result = file->open_file(cache->plugindb, this->asset, 1, 0, -1, -1))
310 // File already opened
311 CICacheItem::CICacheItem(CICache *cache, File *file)
312 : ListItem<CICacheItem>()
315 this->asset = new Asset;
316 item_lock = new Mutex("CICacheItem::item_lock");
317 *this->asset = *file->asset;
322 file->set_processors(cache->preferences->processors);
323 file->set_preload(cache->edl->session->playback_preload);
326 CICacheItem::~CICacheItem()