lib/dvb/tstools.h/cpp: make eDVBTSTool thread safe (i.e. this fixes seeking in timesh...
[vuplus_dvbapp] / lib / dvb / tstools.cpp
index 6fe9113..2c26183 100644 (file)
@@ -7,6 +7,7 @@
 #include <stdio.h>
 
 eDVBTSTools::eDVBTSTools()
+       :m_file_lock(true)
 {
        m_pid = -1;
        m_maxrange = 256*1024;
@@ -47,6 +48,7 @@ int eDVBTSTools::openFile(const char *filename, int nostreaminfo)
        
        m_samples_taken = 0;
 
+       eSingleLocker l(m_file_lock);
        if (m_file.open(filename, 1) < 0)
                return -1;
        return 0;
@@ -54,6 +56,7 @@ int eDVBTSTools::openFile(const char *filename, int nostreaminfo)
 
 void eDVBTSTools::closeFile()
 {
+       eSingleLocker l(m_file_lock);
        m_file.close();
 }
 
@@ -71,13 +74,15 @@ void eDVBTSTools::setSearchRange(int maxrange)
 int eDVBTSTools::getPTS(off_t &offset, pts_t &pts, int fixed)
 {
        if (m_use_streaminfo)
-               return m_streaminfo.getPTS(offset, pts);
+               if (!m_streaminfo.getPTS(offset, pts))
+                       return 0;
        
        if (!m_file.valid())
                return -1;
 
        offset -= offset % 188;
-       
+
+       eSingleLocker l(m_file_lock);
        if (m_file.lseek(offset, SEEK_SET) < 0)
        {
                eDebug("lseek failed");
@@ -144,9 +149,9 @@ int eDVBTSTools::getPTS(off_t &offset, pts_t &pts, int fixed)
                        payload = packet + 4;
 
                
-               if (m_pid >= 0)
+/*             if (m_pid >= 0)
                        if (pid != m_pid)
-                               continue;
+                               continue; */
                if (!pusi)
                        continue;
                
@@ -186,7 +191,8 @@ int eDVBTSTools::fixupPTS(const off_t &offset, pts_t &now)
 {
        if (m_use_streaminfo)
        {
-               return m_streaminfo.fixupPTS(offset, now);
+               if (!m_streaminfo.fixupPTS(offset, now))
+                       return 0;
        } else
        {
                        /* for the simple case, we assume one epoch, with up to one wrap around in the middle. */
@@ -353,7 +359,8 @@ void eDVBTSTools::calcEnd()
 {
        if (!m_file.valid())
                return;
-       
+
+       eSingleLocker l(m_file_lock);
        off_t end = m_file.lseek(0, SEEK_END);
        
        if (llabs(end - m_last_filelength) > 1*1024*1024)
@@ -502,6 +509,7 @@ int eDVBTSTools::findPMT(int &pmt_pid, int &service_id)
                return -1;
        }
 
+       eSingleLocker l(m_file_lock);
        if (m_file.lseek(0, SEEK_SET) < 0)
        {
                eDebug("seek failed");
@@ -660,7 +668,8 @@ int eDVBTSTools::findNextPicture(off_t &offset, size_t &len, int &distance, int
        
        off_t new_offset = offset;
        size_t new_len = len;
-       
+       int first = 1;
+
        while (distance > 0)
        {
                int dir = direction;
@@ -674,8 +683,9 @@ int eDVBTSTools::findNextPicture(off_t &offset, size_t &len, int &distance, int
                
 //             eDebug("we moved %d, %d to go frames (now at %llx)", dir, distance, new_offset);
 
-               if (distance >= 0)
+               if (distance >= 0 || first)
                {
+                       first = 0;
                        offset = new_offset;
                        len = new_len;
                        nr_frames += abs(dir);