tstools.h/cpp: migrate to iDataSource
[vuplus_dvbapp] / lib / dvb / tstools.cpp
index d5ad249..7739f60 100644 (file)
@@ -7,7 +7,6 @@
 #include <stdio.h>
 
 eDVBTSTools::eDVBTSTools()
 #include <stdio.h>
 
 eDVBTSTools::eDVBTSTools()
-       :m_file_lock(true)
 {
        m_pid = -1;
        m_maxrange = 256*1024;
 {
        m_pid = -1;
        m_maxrange = 256*1024;
@@ -23,19 +22,41 @@ eDVBTSTools::eDVBTSTools()
        m_futile = 0;
 }
 
        m_futile = 0;
 }
 
+void eDVBTSTools::closeSource()
+{
+       m_source = NULL;
+}
+
 eDVBTSTools::~eDVBTSTools()
 {
 eDVBTSTools::~eDVBTSTools()
 {
-       closeFile();
+       closeSource();
 }
 
 int eDVBTSTools::openFile(const char *filename, int nostreaminfo)
 {
 }
 
 int eDVBTSTools::openFile(const char *filename, int nostreaminfo)
 {
+       eRawFile *f = new eRawFile();
+       ePtr<iDataSource> src = f;
+
+       eSingleLocker l(f->getLock());
+
+       if (f->open(filename, 1) < 0)
+               return -1;
+
+       setSource(src, filename);
+
+       return 0;
+}
+
+void eDVBTSTools::setSource(ePtr<iDataSource> source, const char *stream_info_filename)
+{
        closeFile();
        closeFile();
-       
-       if (!nostreaminfo)
+
+       m_source = source;
+
+       if (stream_info_filename)
        {
        {
-               eDebug("loading streaminfo for %s", filename);
-               m_streaminfo.load(filename);
+               eDebug("loading streaminfo for %s", stream_info_filename);
+               m_streaminfo.load(stream_info_filename);
        }
        
        if (!m_streaminfo.empty())
        }
        
        if (!m_streaminfo.empty())
@@ -45,19 +66,16 @@ int eDVBTSTools::openFile(const char *filename, int nostreaminfo)
 //             eDebug("no recorded stream information available");
                m_use_streaminfo = 0;
        }
 //             eDebug("no recorded stream information available");
                m_use_streaminfo = 0;
        }
-       
-       m_samples_taken = 0;
 
 
-       eSingleLocker l(m_file_lock);
-       if (m_file.open(filename, 1) < 0)
-               return -1;
-       return 0;
+       m_samples_taken = 0;
 }
 
 void eDVBTSTools::closeFile()
 {
 }
 
 void eDVBTSTools::closeFile()
 {
-       eSingleLocker l(m_file_lock);
-       m_file.close();
+       if (m_source) {
+               eSingleLocker l(m_source->getLock());
+               closeSource();
+       }
 }
 
 void eDVBTSTools::setSyncPID(int pid)
 }
 
 void eDVBTSTools::setSyncPID(int pid)
@@ -77,13 +95,16 @@ int eDVBTSTools::getPTS(off_t &offset, pts_t &pts, int fixed)
                if (!m_streaminfo.getPTS(offset, pts))
                        return 0;
        
                if (!m_streaminfo.getPTS(offset, pts))
                        return 0;
        
-       if (!m_file.valid())
+       if (!m_source->valid())
                return -1;
 
        offset -= offset % 188;
 
                return -1;
 
        offset -= offset % 188;
 
-       eSingleLocker l(m_file_lock);
-       if (m_file.lseek(offset, SEEK_SET) < 0)
+       iDataSourcePositionRestorer r(m_source);
+
+       eSingleLocker l(m_source->getLock());
+
+       if (m_source->lseek(offset, SEEK_SET) < 0)
        {
                eDebug("lseek failed");
                return -1;
        {
                eDebug("lseek failed");
                return -1;
@@ -94,7 +115,7 @@ int eDVBTSTools::getPTS(off_t &offset, pts_t &pts, int fixed)
        while (left >= 188)
        {
                unsigned char packet[188];
        while (left >= 188)
        {
                unsigned char packet[188];
-               if (m_file.read(packet, 188) != 188)
+               if (m_source->read(packet, 188) != 188)
                {
                        eDebug("read error");
                        break;
                {
                        eDebug("read error");
                        break;
@@ -112,7 +133,7 @@ int eDVBTSTools::getPTS(off_t &offset, pts_t &pts, int fixed)
                                        break;
                                ++i;
                        }
                                        break;
                                ++i;
                        }
-                       offset = m_file.lseek(i - 188, SEEK_CUR);
+                       offset = m_source->lseek(i - 188, SEEK_CUR);
                        continue;
                }
                
                        continue;
                }
                
@@ -404,7 +425,7 @@ int eDVBTSTools::getNextAccessPoint(pts_t &ts, const pts_t &start, int direction
 
 void eDVBTSTools::calcBegin()
 {
 
 void eDVBTSTools::calcBegin()
 {
-       if (!m_file.valid())
+       if (!m_source->valid())
                return;
 
        if (!(m_begin_valid || m_futile))
                return;
 
        if (!(m_begin_valid || m_futile))
@@ -419,11 +440,14 @@ void eDVBTSTools::calcBegin()
 
 void eDVBTSTools::calcEnd()
 {
 
 void eDVBTSTools::calcEnd()
 {
-       if (!m_file.valid())
+       if (!m_source->valid())
                return;
 
                return;
 
-       eSingleLocker l(m_file_lock);
-       off_t end = m_file.lseek(0, SEEK_END);
+       iDataSourcePositionRestorer r(m_source);
+
+       eSingleLocker l(m_source->getLock());
+
+       off_t end = m_source->lseek(0, SEEK_END);
        
        if (llabs(end - m_last_filelength) > 1*1024*1024)
        {
        
        if (llabs(end - m_last_filelength) > 1*1024*1024)
        {
@@ -573,14 +597,17 @@ int eDVBTSTools::takeSample(off_t off, pts_t &p)
 int eDVBTSTools::findPMT(int &pmt_pid, int &service_id)
 {
                /* FIXME: this will be factored out soon! */
 int eDVBTSTools::findPMT(int &pmt_pid, int &service_id)
 {
                /* FIXME: this will be factored out soon! */
-       if (!m_file.valid())
+       if (!m_source->valid())
        {
                eDebug(" file not valid");
                return -1;
        }
 
        {
                eDebug(" file not valid");
                return -1;
        }
 
-       eSingleLocker l(m_file_lock);
-       if (m_file.lseek(0, SEEK_SET) < 0)
+       iDataSourcePositionRestorer r(m_source);
+
+       eSingleLocker l(m_source->getLock());
+
+       if (m_source->lseek(0, SEEK_SET) < 0)
        {
                eDebug("seek failed");
                return -1;
        {
                eDebug("seek failed");
                return -1;
@@ -591,7 +618,7 @@ int eDVBTSTools::findPMT(int &pmt_pid, int &service_id)
        while (left >= 188)
        {
                unsigned char packet[188];
        while (left >= 188)
        {
                unsigned char packet[188];
-               if (m_file.read(packet, 188) != 188)
+               if (m_source->read(packet, 188) != 188)
                {
                        eDebug("read error");
                        break;
                {
                        eDebug("read error");
                        break;
@@ -607,7 +634,7 @@ int eDVBTSTools::findPMT(int &pmt_pid, int &service_id)
                                        break;
                                ++i;
                        }
                                        break;
                                ++i;
                        }
-                       m_file.lseek(i - 188, SEEK_CUR);
+                       m_source->lseek(i - 188, SEEK_CUR);
                        continue;
                }
                
                        continue;
                }