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();
22 this->edl->copy_all(edl);
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)
41 this->edl->copy_all(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("CICache::check_out");
96 new_item->checked_out = 1;
98 result = new_item->file;
109 //printf("CICache::check_out %s\n", asset->path);
110 check_out_lock->unlock();
115 int CICache::check_in(Asset *asset)
117 check_in_lock->lock("CICache::check_in");
119 CICacheItem *current;
121 total_lock->lock("CICache::check_in");
122 for(current = first; current && !result; current = NEXT)
124 // Pointers are different
125 if(!strcmp(current->asset->path, asset->path))
127 current->checked_out = 0;
128 current->item_lock->unlock();
132 total_lock->unlock();
134 check_in_lock->unlock();
141 int CICache::delete_entry(char *path)
143 Asset *asset = edl->assets->get_asset(path);
144 if(asset) delete_entry(asset);
148 int CICache::delete_entry(Asset *asset)
152 CICacheItem *current, *temp;
154 for(current = first; current; current = temp)
157 if(current->asset->equivalent(*asset, 0, 0))
159 if(!current->checked_out)
165 printf("CICache::delete_entry asset checked out\n");
177 check_out_lock->lock("CICache::age");
178 CICacheItem *current;
180 for(current = first; current; current = NEXT)
185 // delete old assets if memory usage is exceeded
186 int64_t memory_usage;
190 memory_usage = get_memory_usage();
192 //printf("CICache::age 3 %p %lld %lld\n", this, memory_usage, preferences->cache_size);
193 if(memory_usage > preferences->cache_size)
195 result = delete_oldest();
197 }while(memory_usage > preferences->cache_size && !result);
199 check_out_lock->unlock();
202 int64_t CICache::get_memory_usage()
204 CICacheItem *current;
207 for(current = first; current; current = NEXT)
209 File *file = current->file;
210 if(file) result += file->get_memory_usage();
216 int CICache::delete_oldest()
218 CICacheItem *current;
219 int highest_counter = 1;
220 CICacheItem *oldest = 0;
222 for(current = last; current; current = PREVIOUS)
224 if(current->counter >= highest_counter)
227 highest_counter = current->counter;
231 // if(highest_counter > 1 && oldest)
234 total_lock->lock("CICache::delete_oldest");
237 // Got the oldest file. Try requesting cache purge.
238 if(!oldest->file || oldest->file->purge_cache())
240 // Delete the file if cache already empty and not checked out.
241 if(!oldest->checked_out) delete oldest;
243 total_lock->unlock();
248 return 1; // nothing was old enough to delete
255 CICacheItem *current;
257 printf("CICache::dump total size %lld\n", get_memory_usage());
258 for(current = first; current; current = NEXT)
260 printf("cache item %x asset %x %s counter %lld\n",
263 current->asset->path,
270 int CICache::lock_all()
272 check_in_lock->lock("CICache::lock_all");
273 check_out_lock->lock("CICache::lock_all");
276 int CICache::unlock_all()
278 check_in_lock->unlock();
279 check_out_lock->unlock();
292 // File not already opened.
293 CICacheItem::CICacheItem(CICache *cache, Asset *asset)
294 : ListItem<CICacheItem>()
298 this->asset = new Asset;
299 item_lock = new Mutex("CICacheItem::item_lock");
301 // Must copy Asset since this belongs to an EDL which won't exist forever.
302 *this->asset = *asset;
307 file->set_processors(cache->preferences->processors);
308 file->set_preload(cache->edl->session->playback_preload);
311 // Copy decoding parameters from session to asset so file can see them.
312 this->asset->divx_use_deblocking = cache->edl->session->mpeg4_deblock;
316 if(result = file->open_file(cache->plugindb, this->asset, 1, 0, -1, -1))
323 // File already opened
324 CICacheItem::CICacheItem(CICache *cache, File *file)
325 : ListItem<CICacheItem>()
328 this->asset = new Asset;
329 item_lock = new Mutex("CICacheItem::item_lock");
330 *this->asset = *file->asset;
335 file->set_processors(cache->preferences->processors);
336 file->set_preload(cache->edl->session->playback_preload);
339 CICacheItem::~CICacheItem()