Skip to content

Commit

Permalink
channel cache improvements
Browse files Browse the repository at this point in the history
- check cache for orphaned entries
- create cache dynamically from vdr channel entries
  (this fixes some problems with new vdr channels not in the cache)
  • Loading branch information
pipelka committed Sep 20, 2013
1 parent 00bb444 commit c2a7e7f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
59 changes: 52 additions & 7 deletions src/live/channelcache.c
Expand Up @@ -154,9 +154,12 @@ void cChannelCache::AddToCache(const cChannel* channel) {

std::map<uint32_t, cChannelCache>::iterator i = m_cache.find(uid);

// channel already in cache
if(i != m_cache.end())
return;
// valid channel already in cache
if(i != m_cache.end()) {
if(i->second.size() != 0) {
return;
}
}

// create new cache item
cChannelCache item;
Expand Down Expand Up @@ -205,7 +208,16 @@ void cChannelCache::AddToCache(const cChannel* channel) {
}

cChannelCache cChannelCache::GetFromCache(uint32_t channeluid) {
static cChannelCache empty;

Lock();

std::map<uint32_t, cChannelCache>::iterator i = m_cache.find(channeluid);
if(i == m_cache.end()) {
Unlock();
return empty;
}

cChannelCache result = m_cache[channeluid];
Unlock();

Expand Down Expand Up @@ -239,17 +251,48 @@ void cChannelCache::SaveChannelCacheData() {
rename(filename, filenamenew);
}

void cChannelCache::LoadChannelCacheData() {
m_cache.clear();
void cChannelCache::gc() {
cMutexLock lock(&m_access);
std::map<uint32_t, cChannelCache> m_newcache;

// preload cache with VDR channel entries
INFOLOG("channel cache garbage collection ...");
INFOLOG("before: %i channels in cache", m_cache.size());

// remove orphaned cache entries
XVDRChannels.Lock(false);
cChannels *channels = XVDRChannels.Get();

for (cChannel *channel = channels->First(); channel; channel = channels->Next(channel)) {
AddToCache(channel);
uint32_t uid = CreateChannelUID(channel);

// ignore invalid channels
if(uid == 0)
continue;

// lookup channel in current cache
std::map<uint32_t, cChannelCache>::iterator i = m_cache.find(uid);
if(i == m_cache.end())
continue;

// add to new cache if it exists
m_newcache[uid] = i->second;
}
XVDRChannels.Unlock();

// regenerate cache
m_cache.clear();
std::map<uint32_t, cChannelCache>::iterator i;

for(i = m_newcache.begin(); i != m_newcache.end(); i++) {
m_cache[i->first] = i->second;
}

INFOLOG("after: %i channels in cache", m_cache.size());
}

void cChannelCache::LoadChannelCacheData() {
m_cache.clear();

// load cache
std::fstream in;
cString filename = AddDirectory(XVDRServerConfig.CacheDirectory, CHANNEL_CACHE_FILE);
Expand Down Expand Up @@ -288,6 +331,8 @@ void cChannelCache::LoadChannelCacheData() {
if(uid != 0)
m_cache[uid] = cache;
}

gc();
}


Expand Down
2 changes: 2 additions & 0 deletions src/live/channelcache.h
Expand Up @@ -66,6 +66,8 @@ class cChannelCache : public std::map<int, cStreamInfo> {

static cChannelCache GetFromCache(uint32_t channeluid);

static void gc();

private:

static void Lock() { m_access.Lock(); }
Expand Down
29 changes: 22 additions & 7 deletions src/live/livestreamer.c
Expand Up @@ -258,18 +258,33 @@ int cLiveStreamer::StreamChannel(const cChannel *channel, int sock, bool waitfor
m_PatFilter = new cLivePatFilter(this, channel);

// get cached demuxer data
DEBUGLOG("Creating demuxers");
cChannelCache cache = cChannelCache::GetFromCache(m_uid);
if(cache.size() != 0) {

// channel not found in cache -> add it from vdr
if(cache.size() == 0) {
INFOLOG("adding channel to cache");
cChannelCache::AddToCache(channel);
cache = cChannelCache::GetFromCache(m_uid);
}

// channel already in cache
else {
INFOLOG("Channel information found in cache");
}

// recheck cache item
if(cache.size() != 0) {
INFOLOG("Creating demuxers");
cache.CreateDemuxers(this);
if(!Attach()) {
INFOLOG("Unable to attach receiver !");
return XVDR_RET_DATALOCKED;
}
RequestStreamChange();
}

if(!Attach()) {
INFOLOG("Unable to attach receiver !");
return XVDR_RET_DATALOCKED;
}

RequestStreamChange();

DEBUGLOG("Starting PAT scanner");
m_Device->AttachFilter(m_PatFilter);

Expand Down

0 comments on commit c2a7e7f

Please sign in to comment.