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 iDataSource
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);
22 int m_fd; /* for uncached */
23 FILE *m_file; /* for cached */
24 off_t m_current_offset, m_length;
26 off_t lseek_internal(off_t offset, int whence);
29 class eStaticServiceM2TSInformation: public iStaticServiceInformation
31 DECLARE_REF(eStaticServiceM2TSInformation);
32 eServiceReference m_ref;
33 eDVBMetaParser m_parser;
35 eStaticServiceM2TSInformation(const eServiceReference &ref);
36 RESULT getName(const eServiceReference &ref, std::string &name);
37 int getLength(const eServiceReference &ref);
38 RESULT getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &SWIG_OUTPUT, time_t start_time);
39 int isPlayable(const eServiceReference &ref, const eServiceReference &ignore) { return 1; }
40 int getInfo(const eServiceReference &ref, int w);
41 std::string getInfoString(const eServiceReference &ref,int w);
42 PyObject *getInfoObject(const eServiceReference &r, int what);
45 DEFINE_REF(eStaticServiceM2TSInformation);
47 eStaticServiceM2TSInformation::eStaticServiceM2TSInformation(const eServiceReference &ref)
50 m_parser.parseFile(ref.path);
53 RESULT eStaticServiceM2TSInformation::getName(const eServiceReference &ref, std::string &name)
56 if (m_parser.m_name.size())
57 name = m_parser.m_name;
61 size_t n = name.rfind('/');
62 if (n != std::string::npos)
63 name = name.substr(n + 1);
68 int eStaticServiceM2TSInformation::getLength(const eServiceReference &ref)
75 stat(ref.path.c_str(), &s);
77 eM2TSFile *file = new eM2TSFile(ref.path.c_str());
78 ePtr<iDataSource> source = file;
83 tstools.setSource(source);
85 /* check if cached data is still valid */
86 if (m_parser.m_data_ok && (s.st_size == m_parser.m_filesize) && (m_parser.m_length))
87 return m_parser.m_length / 90000;
89 /* open again, this time with stream info */
90 tstools.setSource(source, ref.path.c_str());
92 /* otherwise, re-calc length and update meta file */
94 if (tstools.calcLen(len))
97 m_parser.m_length = len;
98 m_parser.m_filesize = s.st_size;
99 m_parser.updateMeta(ref.path);
100 return m_parser.m_length / 90000;
103 int eStaticServiceM2TSInformation::getInfo(const eServiceReference &ref, int w)
107 case iServiceInformation::sDescription:
108 return iServiceInformation::resIsString;
109 case iServiceInformation::sServiceref:
110 return iServiceInformation::resIsString;
111 case iServiceInformation::sFileSize:
112 return m_parser.m_filesize;
113 case iServiceInformation::sTimeCreate:
114 if (m_parser.m_time_create)
115 return m_parser.m_time_create;
117 return iServiceInformation::resNA;
119 return iServiceInformation::resNA;
123 std::string eStaticServiceM2TSInformation::getInfoString(const eServiceReference &ref,int w)
127 case iServiceInformation::sDescription:
128 return m_parser.m_description;
129 case iServiceInformation::sServiceref:
130 return m_parser.m_ref.toString();
131 case iServiceInformation::sTags:
132 return m_parser.m_tags;
138 PyObject *eStaticServiceM2TSInformation::getInfoObject(const eServiceReference &r, int what)
142 case iServiceInformation::sFileSize:
143 return PyLong_FromLongLong(m_parser.m_filesize);
149 RESULT eStaticServiceM2TSInformation::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &evt, time_t start_time)
151 if (!ref.path.empty())
153 ePtr<eServiceEvent> event = new eServiceEvent;
154 std::string filename = ref.path;
155 filename.erase(filename.length()-4, 2);
157 if (!event->parseFrom(filename, (m_parser.m_ref.getTransportStreamID().get()<<16)|m_parser.m_ref.getOriginalNetworkID().get()))
167 DEFINE_REF(eM2TSFile);
169 eM2TSFile::eM2TSFile(const char *filename, bool cached)
170 :m_lock(false), m_fd(-1), m_file(NULL), m_current_offset(0), m_length(0), m_cached(cached)
173 m_fd = ::open(filename, O_RDONLY | O_LARGEFILE);
175 m_file = ::fopen64(filename, "rb");
177 m_current_offset = m_length = lseek_internal(0, SEEK_END);
180 eM2TSFile::~eM2TSFile()
198 off_t eM2TSFile::lseek(off_t offset, int whence)
200 eSingleLocker l(m_lock);
202 offset = (offset * 192) / 188;
203 ASSERT(!(offset % 192));
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*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];
230 unsigned char *buf = (unsigned char*)b;
233 offset = (offset * 192) / 188;
234 ASSERT(!(offset % 192));
235 ASSERT(!(count % 188));
237 if (offset != m_current_offset)
239 m_current_offset = lseek_internal(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;
252 memcpy(buf+rd, tmp+4, 188);
254 ASSERT(buf[rd] == 0x47);
257 m_current_offset += 188;
263 int eM2TSFile::valid()
271 off_t eM2TSFile::length()
276 eServiceFactoryM2TS::eServiceFactoryM2TS()
278 ePtr<eServiceCenter> sc;
279 eServiceCenter::getPrivInstance(sc);
282 std::list<std::string> extensions;
283 extensions.push_back("m2ts");
284 extensions.push_back("mts");
285 sc->addServiceFactory(eServiceFactoryM2TS::id, this, extensions);
289 eServiceFactoryM2TS::~eServiceFactoryM2TS()
291 ePtr<eServiceCenter> sc;
293 eServiceCenter::getPrivInstance(sc);
295 sc->removeServiceFactory(eServiceFactoryM2TS::id);
298 RESULT eServiceFactoryM2TS::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
300 ptr = new eServiceM2TS(ref);
304 RESULT eServiceFactoryM2TS::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
310 RESULT eServiceFactoryM2TS::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
316 RESULT eServiceFactoryM2TS::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
318 ptr=new eStaticServiceM2TSInformation(ref);
322 RESULT eServiceFactoryM2TS::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
328 eServiceM2TS::eServiceM2TS(const eServiceReference &ref)
329 :eDVBServicePlay(ref, NULL)
333 ePtr<iDataSource> eServiceM2TS::createDataSource(eServiceReferenceDVB &ref)
335 ePtr<iDataSource> source = new eM2TSFile(ref.path.c_str());
339 RESULT eServiceM2TS::isCurrentlySeekable()
341 return 1; // for fast winding we need index files... so only skip forward/backward yet
344 eAutoInitPtr<eServiceFactoryM2TS> init_eServiceFactoryM2TS(eAutoInitNumbers::service+1, "eServiceFactoryM2TS");