5 #include "edlsession.h"
7 #include "filesystem.h"
8 #include "preferences.h"
12 // edl came from a command which won't exist anymore
13 CICache::CICache(EDL *edl,
14 Preferences *preferences,
15 ArrayList<PluginServer*> *plugindb)
19 this->edl->create_objects();
21 this->plugindb = plugindb;
22 this->preferences = preferences;
27 while(last) delete last;
31 void CICache::set_edl(EDL *edl)
36 void CICache::update(File* &file)
39 for(CICacheItem *current = first; current; current = NEXT)
41 if(!current->asset->test_path(file->asset->path))
43 if(file != current->file)
53 append(item = new CICacheItem(this, file));
54 item->asset = new Asset(*(file->asset));
55 file->set_asset(item->asset);
58 File* CICache::check_out(Asset *asset)
62 check_out_lock.lock();
64 //printf("CICache::check_out 1 %s\n", asset->path);
65 // search for it in the cache
66 CICacheItem *current, *new_item = 0;
67 //printf("CICache::check_out 1 %s\n", asset->path);
69 for(current = first; current && !new_item; current = NEXT)
71 if(!strcmp(current->asset->path, asset->path))
77 //printf("CICache::check_out 1 %s\n", asset->path);
79 // didn't find it so create a new one
82 new_item = append(new CICacheItem(this, asset));
85 //printf("CICache::check_out 1 %s\n", asset->path);
90 // opened successfully
91 new_item->item_lock.lock();
92 new_item->checked_out = 1;
94 result = new_item->file;
103 //printf("CICache::check_out 1 %s\n", asset->path);
105 check_out_lock.unlock();
106 //printf("CICache::check_out 100 %s\n", asset->path);
111 int CICache::check_in(Asset *asset)
113 check_in_lock.lock();
115 CICacheItem *current;
118 for(current = first; current && !result; current = NEXT)
120 // Pointers are different
121 if(!strcmp(current->asset->path, asset->path))
123 current->checked_out = 0;
124 current->item_lock.unlock();
130 check_in_lock.unlock();
137 int CICache::delete_entry(char *path)
139 Asset *asset = edl->assets->get_asset(path);
140 if(asset) delete_entry(asset);
144 int CICache::delete_entry(Asset *asset)
148 CICacheItem *current, *temp;
150 for(current = first; current; current = temp)
153 if(current->asset->equivalent(*asset, 0, 0))
155 if(!current->checked_out)
161 printf("CICache::delete_entry asset checked out\n");
173 check_out_lock.lock();
174 CICacheItem *current;
176 for(current = first; current; current = NEXT)
181 // delete old assets if memory usage is exceeded
182 int64_t memory_usage;
186 memory_usage = get_memory_usage();
188 if(memory_usage > preferences->cache_size)
190 result = delete_oldest();
192 }while(memory_usage > preferences->cache_size && !result);
194 check_out_lock.unlock();
197 int64_t CICache::get_memory_usage()
199 CICacheItem *current;
202 for(current = first; current; current = NEXT)
210 int CICache::delete_oldest()
212 CICacheItem *current;
213 int highest_counter = 1;
214 CICacheItem *oldest = 0;
216 for(current = last; current; current = PREVIOUS)
218 if(current->counter >= highest_counter)
221 highest_counter = current->counter;
225 if(highest_counter > 1 && oldest && !oldest->checked_out)
234 return 1; // nothing was old enough to delete
241 CICacheItem *current;
243 for(current = first; current; current = NEXT)
245 printf("cache item %x\n", current);
246 printf(" asset %x\n", current->asset);
247 printf(" %s\n", current->asset->path);
248 printf(" counter %lld\n", current->counter);
251 printf("total size %lld\n", get_memory_usage());
255 int CICache::lock_all()
257 check_in_lock.lock();
258 check_out_lock.lock();
261 int CICache::unlock_all()
263 check_in_lock.unlock();
264 check_out_lock.unlock();
277 // File not already opened.
278 CICacheItem::CICacheItem(CICache *cache, Asset *asset)
279 : ListItem<CICacheItem>()
281 //printf("CICacheItem::CICacheItem 1\n");
284 this->asset = new Asset;
286 // Must copy Asset since this belongs to an EDL which won't exist forever.
287 //printf("CICacheItem::CICacheItem 1\n");
288 *this->asset = *asset;
289 //printf("CICacheItem::CICacheItem 1\n");
294 //printf("CICacheItem::CICacheItem 1\n");
295 file->set_processors(cache->edl->session->smp ? 2: 1);
296 //printf("CICacheItem::CICacheItem 1\n");
297 file->set_preload(cache->edl->session->playback_preload);
298 //printf("CICacheItem::CICacheItem 1\n");
301 // Copy decoding parameters from session to asset so file can see them.
302 this->asset->divx_use_deblocking = cache->edl->session->mpeg4_deblock;
304 //printf("CICacheItem::CICacheItem 1\n");
307 if(result = file->open_file(cache->plugindb, this->asset, 1, 0, -1, -1))
314 // File already opened
315 CICacheItem::CICacheItem(CICache *cache, File *file)
316 : ListItem<CICacheItem>()
319 this->asset = new Asset;
320 *this->asset = *file->asset;
325 file->set_processors(cache->edl->session->smp ? 2: 1);
326 file->set_preload(cache->edl->session->playback_preload);
329 CICacheItem::~CICacheItem()