release cached channel after 3 seconds when not needed
[enigma2.git] / lib / dvb / dvb.cpp
index 601e627..8b442e3 100644 (file)
@@ -43,6 +43,7 @@ DEFINE_REF(eDVBResourceManager);
 eDVBResourceManager *eDVBResourceManager::instance;
 
 eDVBResourceManager::eDVBResourceManager()
+       :m_releaseCachedChannelTimer(eApp)
 {
        avail = 1;
        busy = 0;
@@ -63,6 +64,8 @@ eDVBResourceManager::eDVBResourceManager()
        
        eDebug("found %d adapter, %d frontends and %d demux", 
                m_adapter.size(), m_frontend.size(), m_demux.size());
+
+       CONNECT(m_releaseCachedChannelTimer.timeout, eDVBResourceManager::releaseCachedChannel);
 }
 
 
@@ -355,10 +358,40 @@ RESULT eDVBResourceManager::allocateChannel(const eDVBChannelID &channelid, eUse
                return errChidNotFound;
        }
        m_cached_channel = channel = ch;
+       CONNECT(ch->m_stateChanged,eDVBResourceManager::DVBChannelStateChanged);
 
        return 0;
 }
 
+void eDVBResourceManager::DVBChannelStateChanged(iDVBChannel *chan)
+{
+       int state=0;
+       chan->getState(state);
+       switch (state)
+       {
+               case iDVBChannel::state_ok:
+               {
+                       eDebug("stop release channel timer");
+                       m_releaseCachedChannelTimer.stop();
+                       break;
+               }
+               case iDVBChannel::state_last_instance:
+               {
+                       eDebug("start release channel timer");
+                       m_releaseCachedChannelTimer.start(3000, true);
+                       break;
+               }
+               default: // ignore all other events
+                       break;
+       }
+}
+
+void eDVBResourceManager::releaseCachedChannel()
+{
+       eDebug("release cached channel");
+       m_cached_channel=0;
+}
+
 RESULT eDVBResourceManager::allocateRawChannel(eUsePtr<iDVBChannel> &channel, int frontend_index)
 {
        ePtr<eDVBAllocatedFrontend> fe;
@@ -780,7 +813,8 @@ void eDVBChannel::getNextSourceSpan(off_t current_offset, size_t bytes_read, off
 
 void eDVBChannel::AddUse()
 {
-       ++m_use_count;
+       if (++m_use_count > 1 && m_state == state_last_instance)
+               m_state = state_ok;
 }
 
 void eDVBChannel::ReleaseUse()
@@ -790,6 +824,11 @@ void eDVBChannel::ReleaseUse()
                m_state = state_release;
                m_stateChanged(this);
        }
+       else if (m_use_count == 1)
+       {
+               m_state = state_last_instance;
+               m_stateChanged(this);
+       }
 }
 
 RESULT eDVBChannel::setChannel(const eDVBChannelID &channelid, ePtr<iDVBFrontendParameters> &feparm)