1 #include <lib/base/init_num.h>
2 #include <lib/base/init.h>
3 #include <lib/dvb/metaparser.h>
4 #include <lib/service/servicem2ts.h>
6 DEFINE_REF(eServiceFactoryM2TS)
8 class eM2TSFile: public iTsSource
10 DECLARE_REF(eM2TSFile);
13 eM2TSFile(const char *filename, bool cached=false);
17 off_t lseek(off_t offset, int whence);
18 ssize_t read(off_t offset, void *buf, size_t count);
23 int m_fd; /* for uncached */
24 FILE *m_file; /* for cached */
25 off_t m_current_offset, m_length;
27 off_t lseek_internal(off_t offset, int whence);
30 class eStaticServiceM2TSInformation: public iStaticServiceInformation
32 DECLARE_REF(eStaticServiceM2TSInformation);
33 eServiceReference m_ref;
34 eDVBMetaParser m_parser;
36 eStaticServiceM2TSInformation(const eServiceReference &ref);
37 RESULT getName(const eServiceReference &ref, std::string &name);
38 int getLength(const eServiceReference &ref);
39 RESULT getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &SWIG_OUTPUT, time_t start_time);
40 int isPlayable(const eServiceReference &ref, const eServiceReference &ignore) { return 1; }
41 int getInfo(const eServiceReference &ref, int w);
42 std::string getInfoString(const eServiceReference &ref,int w);
43 PyObject *getInfoObject(const eServiceReference &r, int what);
46 DEFINE_REF(eStaticServiceM2TSInformation);
48 eStaticServiceM2TSInformation::eStaticServiceM2TSInformation(const eServiceReference &ref)
51 m_parser.parseFile(ref.path);
54 RESULT eStaticServiceM2TSInformation::getName(const eServiceReference &ref, std::string &name)
57 if (m_parser.m_name.size())
58 name = m_parser.m_name;
62 size_t n = name.rfind('/');
63 if (n != std::string::npos)
64 name = name.substr(n + 1);
69 int eStaticServiceM2TSInformation::getLength(const eServiceReference &ref)
76 stat(ref.path.c_str(), &s);
78 eM2TSFile *file = new eM2TSFile(ref.path.c_str());
79 ePtr<iTsSource> source = file;
84 tstools.setSource(source);
86 /* check if cached data is still valid */
87 if (m_parser.m_data_ok && (s.st_size == m_parser.m_filesize) && (m_parser.m_length))
88 return m_parser.m_length / 90000;
90 /* open again, this time with stream info */
91 tstools.setSource(source, ref.path.c_str());
93 /* otherwise, re-calc length and update meta file */
95 if (tstools.calcLen(len))
98 m_parser.m_length = len;
99 m_parser.m_filesize = s.st_size;
100 m_parser.updateMeta(ref.path);
101 return m_parser.m_length / 90000;
104 int eStaticServiceM2TSInformation::getInfo(const eServiceReference &ref, int w)
108 case iServiceInformation::sDescription:
109 return iServiceInformation::resIsString;
110 case iServiceInformation::sServiceref:
111 return iServiceInformation::resIsString;
112 case iServiceInformation::sFileSize:
113 return m_parser.m_filesize;
114 case iServiceInformation::sTimeCreate:
115 if (m_parser.m_time_create)
116 return m_parser.m_time_create;
118 return iServiceInformation::resNA;
120 return iServiceInformation::resNA;
124 std::string eStaticServiceM2TSInformation::getInfoString(const eServiceReference &ref,int w)
128 case iServiceInformation::sDescription:
129 return m_parser.m_description;
130 case iServiceInformation::sServiceref:
131 return m_parser.m_ref.toString();
132 case iServiceInformation::sTags:
133 return m_parser.m_tags;
139 PyObject *eStaticServiceM2TSInformation::getInfoObject(const eServiceReference &r, int what)
143 case iServiceInformation::sFileSize:
144 return PyLong_FromLongLong(m_parser.m_filesize);
150 RESULT eStaticServiceM2TSInformation::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &evt, time_t start_time)
152 if (!ref.path.empty())
154 ePtr<eServiceEvent> event = new eServiceEvent;
155 std::string filename = ref.path;
156 filename.erase(filename.length()-4, 2);
158 if (!event->parseFrom(filename, (m_parser.m_ref.getTransportStreamID().get()<<16)|m_parser.m_ref.getOriginalNetworkID().get()))
168 DEFINE_REF(eM2TSFile);
170 eM2TSFile::eM2TSFile(const char *filename, bool cached)
171 :m_lock(false), m_sync_offset(0), m_fd(-1), m_file(NULL), m_current_offset(0), m_length(0), m_cached(cached)
174 m_fd = ::open(filename, O_RDONLY | O_LARGEFILE);
176 m_file = ::fopen64(filename, "rb");
178 m_current_offset = m_length = lseek_internal(0, SEEK_END);
181 eM2TSFile::~eM2TSFile()
199 off_t eM2TSFile::lseek(off_t offset, int whence)
201 eSingleLocker l(m_lock);
203 offset = (offset % 188) + (offset * 192) / 188;
205 if (offset != m_current_offset)
206 m_current_offset = lseek_internal(offset, whence);
208 return m_current_offset;
211 off_t eM2TSFile::lseek_internal(off_t offset, int whence)
216 ret = ::lseek(m_fd, offset, whence);
219 if (::fseeko(m_file, offset, whence) < 0)
221 ret = ::ftello(m_file);
223 return ret <= 0 ? ret : (ret % 192) + (ret*188) / 192;
226 ssize_t eM2TSFile::read(off_t offset, void *b, size_t count)
228 eSingleLocker l(m_lock);
229 unsigned char tmp[192*3];
230 unsigned char *buf = (unsigned char*)b;
233 offset = (offset % 188) + (offset * 192) / 188;
236 if ((offset+m_sync_offset) != m_current_offset)
238 // eDebug("seekTo %lld", offset+m_sync_offset);
239 m_current_offset = lseek_internal(offset+m_sync_offset, SEEK_SET);
240 if (m_current_offset < 0)
241 return m_current_offset;
247 ret = ::read(m_fd, tmp, 192);
249 ret = ::fread(tmp, 1, 192, m_file);
250 if (ret < 0 || ret < 192)
251 return rd ? rd : ret;
256 eDebug("short read at pos %lld async!!", m_current_offset);
262 ret = ::read(m_fd, tmp+192, 384);
264 ret = ::fread(tmp+192, 1, 384, m_file);
267 eDebugNoNewLine("m2ts out of sync at pos %lld, real %lld:", offset + m_sync_offset, m_current_offset);
269 eDebugNoNewLine(" %02x", tmp[x]);
273 eDebug("m2ts out of sync at pos %lld, real %lld", offset + m_sync_offset, m_current_offset);
277 if (tmp[x] == 0x47 && tmp[x+192] == 0x47)
279 int add_offs = (x - 4);
280 eDebug("sync found at pos %d, sync_offset is now %d, old was %d", x, add_offs + m_sync_offset, m_sync_offset);
281 m_sync_offset += add_offs;
288 memcpy(buf+rd, tmp+4, 188);
291 m_current_offset += 188;
294 m_sync_offset %= 188;
299 int eM2TSFile::valid()
307 off_t eM2TSFile::length()
312 eServiceFactoryM2TS::eServiceFactoryM2TS()
314 ePtr<eServiceCenter> sc;
315 eServiceCenter::getPrivInstance(sc);
318 std::list<std::string> extensions;
319 extensions.push_back("m2ts");
320 extensions.push_back("mts");
321 sc->addServiceFactory(eServiceFactoryM2TS::id, this, extensions);
325 eServiceFactoryM2TS::~eServiceFactoryM2TS()
327 ePtr<eServiceCenter> sc;
329 eServiceCenter::getPrivInstance(sc);
331 sc->removeServiceFactory(eServiceFactoryM2TS::id);
334 RESULT eServiceFactoryM2TS::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
336 ptr = new eServiceM2TS(ref);
340 RESULT eServiceFactoryM2TS::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
346 RESULT eServiceFactoryM2TS::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
352 RESULT eServiceFactoryM2TS::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
354 ptr=new eStaticServiceM2TSInformation(ref);
358 RESULT eServiceFactoryM2TS::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
364 eServiceM2TS::eServiceM2TS(const eServiceReference &ref)
365 :eDVBServicePlay(ref, NULL)
369 ePtr<iTsSource> eServiceM2TS::createTsSource(eServiceReferenceDVB &ref)
371 ePtr<iTsSource> source = new eM2TSFile(ref.path.c_str());
375 RESULT eServiceM2TS::isCurrentlySeekable()
377 return 1; // for fast winding we need index files... so only skip forward/backward yet
380 eAutoInitPtr<eServiceFactoryM2TS> init_eServiceFactoryM2TS(eAutoInitNumbers::service+1, "eServiceFactoryM2TS");