fix ratio calculation for slow reverse skipping
[enigma2.git] / lib / dvb / dvb.cpp
index 8b442e3d28cf70d3d05bd90095f5c7964d79a083..f8f5a3785d567e97df50a3a6ddb7bcd860301b95 100644 (file)
@@ -310,7 +310,9 @@ RESULT eDVBResourceManager::allocateChannel(const eDVBChannelID &channelid, eUse
                        channel = m_cached_channel;
                        return 0;
                }
+               m_cached_channel_state_changed_conn.disconnect();
                m_cached_channel=0;
+               m_releaseCachedChannelTimer.stop();
        }
 
 //     eDebug("allocate channel.. %04x:%04x", channelid.transport_stream_id.get(), channelid.original_network_id.get());
@@ -358,7 +360,8 @@ RESULT eDVBResourceManager::allocateChannel(const eDVBChannelID &channelid, eUse
                return errChidNotFound;
        }
        m_cached_channel = channel = ch;
-       CONNECT(ch->m_stateChanged,eDVBResourceManager::DVBChannelStateChanged);
+       m_cached_channel_state_changed_conn =
+               CONNECT(ch->m_stateChanged,eDVBResourceManager::DVBChannelStateChanged);
 
        return 0;
 }
@@ -369,6 +372,7 @@ void eDVBResourceManager::DVBChannelStateChanged(iDVBChannel *chan)
        chan->getState(state);
        switch (state)
        {
+               case iDVBChannel::state_release:
                case iDVBChannel::state_ok:
                {
                        eDebug("stop release channel timer");
@@ -388,7 +392,7 @@ void eDVBResourceManager::DVBChannelStateChanged(iDVBChannel *chan)
 
 void eDVBResourceManager::releaseCachedChannel()
 {
-       eDebug("release cached channel");
+       eDebug("release cached channel (timer timeout)");
        m_cached_channel=0;
 }
 
@@ -397,7 +401,11 @@ RESULT eDVBResourceManager::allocateRawChannel(eUsePtr<iDVBChannel> &channel, in
        ePtr<eDVBAllocatedFrontend> fe;
 
        if (m_cached_channel)
+       {
+               m_cached_channel_state_changed_conn.disconnect();
                m_cached_channel=0;
+               m_releaseCachedChannelTimer.stop();
+       }
 
        if (allocateFrontendByIndex(fe, frontend_index))
                return errNoFrontend;
@@ -414,9 +422,6 @@ RESULT eDVBResourceManager::allocatePVRChannel(eUsePtr<iDVBPVRChannel> &channel)
 {
        ePtr<eDVBAllocatedDemux> demux;
 
-       if (m_cached_channel)
-               m_cached_channel=0;
-
        eDVBChannel *ch;
        ch = new eDVBChannel(this, 0);
        
@@ -680,13 +685,16 @@ void eDVBChannel::cueSheetEvent(int event)
                                eDebug("skipmode ratio is %lld:90000, bitrate is %d bit/s", m_cue->m_skipmode_ratio, bitrate);
                                                /* i agree that this might look a bit like black magic. */
                                m_skipmode_n = 512*1024; /* must be 1 iframe at least. */
-                               m_skipmode_m = bitrate / 8 / 90000 * m_cue->m_skipmode_ratio;
+                               m_skipmode_m = bitrate / 8 / 90000 * m_cue->m_skipmode_ratio / 8;
+                               
+                               if (m_cue->m_skipmode_ratio < 0)
+                                       m_skipmode_m -= m_skipmode_n;
        
                                eDebug("resolved to: %d %d", m_skipmode_m, m_skipmode_n);
                                
                                if (abs(m_skipmode_m) < abs(m_skipmode_n))
                                {
-                                       eFatal("damn, something is wrong with this calculation");
+                                       eWarning("something is wrong with this calculation");
                                        m_skipmode_n = m_skipmode_m = 0;
                                }
                                
@@ -761,8 +769,16 @@ void eDVBChannel::getNextSourceSpan(off_t current_offset, size_t bytes_read, off
                if (relative)
                {
                        pts_t now;
-                                       /* we're using the decoder's timestamp here. this 
-                                          won't work for radio (ouch). */
+                       if (!m_cue->m_decoder)
+                       {
+                               eDebug("no decoder - can't seek relative");
+                               continue;
+                       }
+                       if (m_cue->m_decoder->getPTS(0, now))
+                       {
+                               eDebug("decoder getPTS failed, can't seek relative");
+                               continue;
+                       }
                        if (getCurrentPosition(m_cue->m_decoding_demux, now, 1))
                        {
                                eDebug("seekTo: getCurrentPosition failed!");
@@ -814,7 +830,10 @@ void eDVBChannel::getNextSourceSpan(off_t current_offset, size_t bytes_read, off
 void eDVBChannel::AddUse()
 {
        if (++m_use_count > 1 && m_state == state_last_instance)
+       {
                m_state = state_ok;
+               m_stateChanged(this);
+       }
 }
 
 void eDVBChannel::ReleaseUse()
@@ -988,39 +1007,30 @@ RESULT eDVBChannel::getCurrentPosition(iDVBDemux *decoding_demux, pts_t &pos, in
        if (!decoding_demux)
                return -1;
        
-       off_t begin = 0;
-               /* getPTS for offset 0 is cached, so it doesn't harm. */
-       int r = m_tstools.getPTS(begin, pos);
-       if (r)
-       {
-               eDebug("tstools getpts(0) failed!");
-               return r;
-       }
-       
        pts_t now;
        
-                       /* TODO: this is a gross hack. */
-       r = decoding_demux->getSTC(now, mode ? 128 : 0);
-
-       if (r)
+       int r;
+       
+       if (mode == 0) /* demux */
        {
-               eDebug("demux getSTC failed");
-               return -1;
-       }
+               r = decoding_demux->getSTC(now, 0);
+               if (r)
+               {
+                       eDebug("demux getSTC failed");
+                       return -1;
+               }
+       } else
+               now = pos; /* fixup supplied */
        
-//     eDebug("STC: %08llx PTS: %08llx, diff %lld", now, pos, now - pos);
-               /* when we are less than 10 seconds before the start, return 0. */
-               /* (we're just waiting for the timespam to start) */
-       if ((now < pos) && ((pos - now) < 90000 * 10))
+       off_t off = 0; /* TODO: fixme */
+       r = m_tstools.fixupPTS(off, now);
+       if (r)
        {
-               pos = 0;
-               return 0;
+               eDebug("fixup PTS failed");
+               return -1;
        }
        
-       if (now < pos) /* wrap around */
-               pos = now + ((pts_t)1)<<33 - pos;
-       else
-               pos = now - pos;
+       pos = now;
        
        return 0;
 }
@@ -1093,9 +1103,10 @@ void eCueSheet::setSkipmode(const pts_t &ratio)
        m_event(evtSkipmode);
 }
 
-void eCueSheet::setDecodingDemux(iDVBDemux *demux)
+void eCueSheet::setDecodingDemux(iDVBDemux *demux, iTSMPEGDecoder *decoder)
 {
        m_decoding_demux = demux;
+       m_decoder = decoder;
 }
 
 RESULT eCueSheet::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &connection)