python object refcount debugging code second try
[vuplus_dvbapp] / lib / service / servicedvb.cpp
index 077685f..627ccec 100644 (file)
@@ -27,8 +27,6 @@
 #include <byteswap.h>
 #include <netinet/in.h>
 
-#define INTERNAL_TELETEXT
-
 #ifndef BYTE_ORDER
 #error no byte order defined!
 #endif
@@ -212,6 +210,8 @@ std::string eStaticServiceDVBPVRInformation::getInfoString(const eServiceReferen
                return m_parser.m_description;
        case iServiceInformation::sServiceref:
                return m_parser.m_ref.toString();
+       case iServiceInformation::sTags:
+               return m_parser.m_tags;
        default:
                return "";
        }
@@ -382,7 +382,7 @@ RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list, bool sort
        
        if (!m_query)
                return -1;
-       
+
        while (!m_query->getNextResult(ref))
                list.push_back(ref);
 
@@ -406,7 +406,7 @@ RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list, bool sort
 //   unknown format string chars are returned as python None values !
 PyObject *eDVBServiceList::getContent(const char* format, bool sorted)
 {
-       PyObject *ret=0;
+       ePyObject ret;
        std::list<eServiceReference> tmplist;
        int retcount=1;
 
@@ -428,10 +428,10 @@ PyObject *eDVBServiceList::getContent(const char* format, bool sorted)
                for (int cnt=0; cnt < services; ++cnt)
                {
                        eServiceReference &ref=*it++;
-                       PyObject *tuple = retcount > 1 ? PyTuple_New(retcount) : 0;
+                       ePyObject tuple = retcount > 1 ? PyTuple_New(retcount) : 0;
                        for (int i=0; i < retcount; ++i)
                        {
-                               PyObject *tmp=0;
+                               ePyObject tmp;
                                switch(format[i])
                                {
                                case 'R':  // service reference (swig)object
@@ -478,7 +478,7 @@ PyObject *eDVBServiceList::getContent(const char* format, bool sorted)
                                PyList_SET_ITEM(ret, cnt, tuple);
                }
        }
-       return ret ? ret : PyList_New(0);
+       return ret ? (PyObject*)ret : (PyObject*)PyList_New(0);
 }
 
 RESULT eDVBServiceList::getNext(eServiceReference &ref)
@@ -489,11 +489,6 @@ RESULT eDVBServiceList::getNext(eServiceReference &ref)
        return m_query->getNextResult((eServiceReferenceDVB&)ref);
 }
 
-int eDVBServiceList::compareLessEqual(const eServiceReference &a, const eServiceReference &b)
-{
-       return m_query->compareLessEqual((const eServiceReferenceDVB&)a, (const eServiceReferenceDVB&)b);
-}
-
 RESULT eDVBServiceList::startEdit(ePtr<iMutableServiceList> &res)
 {
        if (m_parent.flags & eServiceReference::flagDirectory) // bouquet
@@ -1388,7 +1383,7 @@ int eDVBServiceBase::getFrontendInfo(int w)
 
 PyObject *eDVBServiceBase::getFrontendData(bool original)
 {
-       PyObject *ret=0;
+       ePyObject ret;
 
        eUsePtr<iDVBChannel> channel;
        if(!m_service_handler.getChannel(channel))
@@ -1406,8 +1401,8 @@ PyObject *eDVBServiceBase::getFrontendData(bool original)
                                        eDVBFrontendParametersSatellite osat;
                                        if (!feparm->getDVBS(osat))
                                        {
-                                               void PutToDict(PyObject *, const char*, long);
-                                               void PutToDict(PyObject *, const char*, const char*);
+                                               void PutToDict(ePyObject &, const char*, long);
+                                               void PutToDict(ePyObject &, const char*, const char*);
                                                PutToDict(ret, "orbital_position", osat.orbital_position);
                                                const char *tmp = "UNKNOWN";
                                                switch(osat.polarisation)
@@ -1531,11 +1526,11 @@ RESULT eDVBServicePlay::activateTimeshift()
 
 PyObject *eDVBServicePlay::getCutList()
 {
-       PyObject *list = PyList_New(0);
+       ePyObject list = PyList_New(0);
        
        for (std::multiset<struct cueEntry>::iterator i(m_cue_entries.begin()); i != m_cue_entries.end(); ++i)
        {
-               PyObject *tuple = PyTuple_New(2);
+               ePyObject tuple = PyTuple_New(2);
                PyTuple_SetItem(tuple, 0, PyLong_FromLongLong(i->where));
                PyTuple_SetItem(tuple, 1, PyInt_FromLong(i->what));
                PyList_Append(list, tuple);
@@ -1545,7 +1540,7 @@ PyObject *eDVBServicePlay::getCutList()
        return list;
 }
 
-void eDVBServicePlay::setCutList(PyObject *list)
+void eDVBServicePlay::setCutList(ePyObject list)
 {
        if (!PyList_Check(list))
                return;
@@ -1556,7 +1551,7 @@ void eDVBServicePlay::setCutList(PyObject *list)
        
        for (i=0; i<size; ++i)
        {
-               PyObject *tuple = PyList_GetItem(list, i);
+               ePyObject tuple = PyList_GetItem(list, i);
                if (!PyTuple_Check(tuple))
                {
                        eDebug("non-tuple in cutlist");
@@ -1567,7 +1562,7 @@ void eDVBServicePlay::setCutList(PyObject *list)
                        eDebug("cutlist entries need to be a 2-tuple");
                        continue;
                }
-               PyObject *ppts = PyTuple_GetItem(tuple, 0), *ptype = PyTuple_GetItem(tuple, 1);
+               ePyObject ppts = PyTuple_GetItem(tuple, 0), ptype = PyTuple_GetItem(tuple, 1);
                if (!(PyLong_Check(ppts) && PyInt_Check(ptype)))
                {
                        eDebug("cutlist entries need to be (pts, type)-tuples (%d %d)", PyLong_Check(ppts), PyInt_Check(ptype));
@@ -1761,10 +1756,8 @@ void eDVBServicePlay::updateDecoder()
                        m_decode_demux->getMPEGDecoder(m_decoder, m_is_primary);
                if (m_cue)
                        m_cue->setDecodingDemux(m_decode_demux, m_decoder);
-#ifdef INTERNAL_TELETEXT
                m_teletext_parser = new eDVBTeletextParser(m_decode_demux);
                m_teletext_parser->connectNewPage(slot(*this, &eDVBServicePlay::newSubtitlePage), m_new_subtitle_page_connection);
-#endif
                m_subtitle_parser = new eDVBSubtitleParser(m_decode_demux);
                m_subtitle_parser->connectNewPage(slot(*this, &eDVBServicePlay::newDVBSubtitlePage), m_new_dvb_subtitle_page_connection);
        }
@@ -1991,44 +1984,101 @@ void eDVBServicePlay::cutlistToCuesheet()
        m_cue->commitSpans();
 }
 
-RESULT eDVBServicePlay::enableSubtitles(eWidget *parent, PyObject *entry)
+RESULT eDVBServicePlay::enableSubtitles(eWidget *parent, ePyObject tuple)
 {
        if (m_subtitle_widget)
                disableSubtitles(parent);
-       
-       if (!PyInt_Check(entry))
-               return -1;
 
-       int page = PyInt_AsLong(entry);
+       ePyObject entry;
+       int tuplesize = PyTuple_Size(tuple);
+       int type = 0;
 
-       if (page > 0 && !m_teletext_parser)
-               return -1;
-       if (page < 0 && !m_subtitle_parser)
-               return -1;
+       if (!PyTuple_Check(tuple))
+               goto error_out;
+
+       if (tuplesize < 1)
+               goto error_out;
+
+       entry = PyTuple_GET_ITEM(tuple, 0);
+
+       if (!PyInt_Check(entry))
+               goto error_out;
 
-       m_subtitle_widget = new eSubtitleWidget(parent);
-       m_subtitle_widget->resize(parent->size()); /* full size */
+       type = PyInt_AsLong(entry);
 
-       if (page > 0)
+       if (type == 1)  // teletext subtitles
        {
-/*             eDVBServicePMTHandler &h = m_timeshift_active ? m_service_handler_timeshift : m_service_handler;
-               eDVBServicePMTHandler::program program;
-               if (h.getProgramInfo(program))
-                       eDebug("getting program info failed.");
-               else
+               int page, magazine, pid;
+               if (tuplesize < 4)
+                       goto error_out;
+
+               if (!m_teletext_parser)
                {
-                       eDebug("start teletext on pid %04x, page %d", program.textPid, page);
-                       m_teletext_parser->start(program.textPid);*/
-                       m_teletext_parser->setPage(page);
-//             }
+                       eDebug("enable teletext subtitles.. no parser !!!");
+                       return -1;
+               }
+
+               entry = PyTuple_GET_ITEM(tuple, 1);
+               if (!PyInt_Check(entry))
+                       goto error_out;
+               pid = PyInt_AsLong(entry);
+
+               entry = PyTuple_GET_ITEM(tuple, 2);
+               if (!PyInt_Check(entry))
+                       goto error_out;
+               page = PyInt_AsLong(entry);
+
+               entry = PyTuple_GET_ITEM(tuple, 3);
+               if (!PyInt_Check(entry))
+                       goto error_out;
+               magazine = PyInt_AsLong(entry);
+
+               m_subtitle_widget = new eSubtitleWidget(parent);
+               m_subtitle_widget->resize(parent->size()); /* full size */
+               m_teletext_parser->setPageAndMagazine(page, magazine);
+               if (m_dvb_service)
+                       m_dvb_service->setCacheEntry(eDVBService::cSUBTITLE,((pid&0xFFFF)<<16)|((page&0xFF)<<8)|(magazine&0xFF));
        }
-       else
+       else if (type == 0)
        {
-               int pid = -page;
-               m_subtitle_parser->start(pid);
+               int pid = 0, composition_page_id = 0, ancillary_page_id = 0;
+               if (!m_subtitle_parser)
+               {
+                       eDebug("enable dvb subtitles.. no parser !!!");
+                       return -1;
+               }
+               if (tuplesize < 4)
+                       goto error_out;
+
+               entry = PyTuple_GET_ITEM(tuple, 1);
+               if (!PyInt_Check(entry))
+                       goto error_out;
+               pid = PyInt_AsLong(entry);
+
+               entry = PyTuple_GET_ITEM(tuple, 2);
+               if (!PyInt_Check(entry))
+                       goto error_out;
+               composition_page_id = PyInt_AsLong(entry);
+
+               entry = PyTuple_GET_ITEM(tuple, 3);
+               if (!PyInt_Check(entry))
+                       goto error_out;
+               ancillary_page_id = PyInt_AsLong(entry);
+
+               m_subtitle_widget = new eSubtitleWidget(parent);
+               m_subtitle_widget->resize(parent->size()); /* full size */
+               m_subtitle_parser->start(pid, composition_page_id, ancillary_page_id);
+               if (m_dvb_service)
+                       m_dvb_service->setCacheEntry(eDVBService::cSUBTITLE, ((pid&0xFFFF)<<16)|((composition_page_id&0xFF)<<8)|(ancillary_page_id&0xFF));
        }
-
+       else
+               goto error_out;
        return 0;
+error_out:
+       eDebug("enableSubtitles needs a tuple as 2nd argument!\n"
+               "for teletext subtitles (0, pid, teletext_page, teletext_magazine)\n"
+               "for dvb subtitles (1, pid, composition_page_id, ancillary_page_id)");
+       return -1;
 }
 
 RESULT eDVBServicePlay::disableSubtitles(eWidget *parent)
@@ -2042,12 +2092,43 @@ RESULT eDVBServicePlay::disableSubtitles(eWidget *parent)
        }
        if (m_teletext_parser)
        {
-               m_teletext_parser->setPage(-1);
+               m_teletext_parser->setPageAndMagazine(-1, -1);
                m_subtitle_pages.clear();
        }
+       if (m_dvb_service)
+               m_dvb_service->setCacheEntry(eDVBService::cSUBTITLE, -1);
        return 0;
 }
 
+PyObject *eDVBServicePlay::getCachedSubtitle()
+{
+       if (m_dvb_service)
+       {
+               int tmp = m_dvb_service->getCacheEntry(eDVBService::cSUBTITLE);
+               if (tmp != -1)
+               {
+                       unsigned int data = (unsigned int)tmp;
+                       int pid = (data&0xFFFF0000)>>16;
+                       ePyObject tuple = PyTuple_New(4);
+                       eDVBServicePMTHandler::program program;
+                       eDVBServicePMTHandler &h = m_timeshift_active ? m_service_handler_timeshift : m_service_handler;
+                       if (!h.getProgramInfo(program))
+                       {
+                               if (program.textPid==pid) // teletext
+                                       PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(1)); // type teletext
+                               else
+                                       PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(0)); // type dvb
+                               PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong((data&0xFFFF0000)>>16)); // pid
+                               PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong((data&0xFF00)>>8)); // composition_page / page
+                               PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(data&0xFF)); // ancillary_page / magazine
+                               return tuple;
+                       }
+               }
+       }
+       Py_INCREF(Py_None);
+       return Py_None;
+}
+
 PyObject *eDVBServicePlay::getSubtitleList()
 {
        if (!m_teletext_parser)
@@ -2056,18 +2137,11 @@ PyObject *eDVBServicePlay::getSubtitleList()
                return Py_None;
        }
        
-       PyObject *l = PyList_New(0);
-       
-       for (std::set<int>::iterator i(m_teletext_parser->m_found_subtitle_pages.begin()); i != m_teletext_parser->m_found_subtitle_pages.end(); ++i)
-       {
-               PyObject *tuple = PyTuple_New(2);
-               char desc[20];
-               sprintf(desc, "Page %d", *i);
-               PyTuple_SetItem(tuple, 0, PyString_FromString(desc));
-               PyTuple_SetItem(tuple, 1, PyInt_FromLong(*i));
-               PyList_Append(l, tuple);
-               Py_DECREF(tuple);
-       }
+       ePyObject l = PyList_New(0);
+       std::set<int> added_ttx_pages;
+
+       std::set<eDVBServicePMTHandler::subtitleStream> &subs =
+               m_teletext_parser->m_found_subtitle_pages;
 
        eDVBServicePMTHandler &h = m_timeshift_active ? m_service_handler_timeshift : m_service_handler;
        eDVBServicePMTHandler::program program;
@@ -2078,11 +2152,58 @@ PyObject *eDVBServicePlay::getSubtitleList()
                for (std::vector<eDVBServicePMTHandler::subtitleStream>::iterator it(program.subtitleStreams.begin());
                        it != program.subtitleStreams.end(); ++it)
                {
-                       PyObject *tuple = PyTuple_New(2);
-                       char desc[20];
-                       sprintf(desc, "DVB %s", it->language_code.c_str());
-                       PyTuple_SetItem(tuple, 0, PyString_FromString(desc));
-                       PyTuple_SetItem(tuple, 1, PyInt_FromLong(-it->pid));
+                       switch(it->subtitling_type)
+                       {
+                               case 0x01: // ebu teletext subtitles
+                               {
+                                       int page_number = it->teletext_page_number & 0xFF;
+                                       int magazine_number = it->teletext_magazine_number & 7;
+                                       int hash = magazine_number << 8 | page_number;
+                                       if (added_ttx_pages.find(hash) == added_ttx_pages.end())
+                                       {
+                                               ePyObject tuple = PyTuple_New(5);
+                                               PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(1));
+                                               PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(it->pid));
+                                               PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(page_number));
+                                               PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(magazine_number));
+                                               PyTuple_SET_ITEM(tuple, 4, PyString_FromString(it->language_code.c_str()));
+                                               PyList_Append(l, tuple);
+                                               Py_DECREF(tuple);
+                                               added_ttx_pages.insert(hash);
+                                       }
+                                       break;
+                               }
+                               case 0x10 ... 0x13:
+                               case 0x20 ... 0x23: // dvb subtitles
+                               {
+                                       ePyObject tuple = PyTuple_New(5);
+                                       PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(0));
+                                       PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(it->pid));
+                                       PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(it->composition_page_id));
+                                       PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(it->ancillary_page_id));
+                                       PyTuple_SET_ITEM(tuple, 4, PyString_FromString(it->language_code.c_str()));
+                                       PyList_Insert(l, 0, tuple);
+                                       Py_DECREF(tuple);
+                                       break;
+                               }
+                       }
+               }
+       }
+
+       for (std::set<eDVBServicePMTHandler::subtitleStream>::iterator it(subs.begin());
+               it != subs.end(); ++it)
+       {
+               int page_number = it->teletext_page_number & 0xFF;
+               int magazine_number = it->teletext_magazine_number & 7;
+               int hash = magazine_number << 8 | page_number;
+               if (added_ttx_pages.find(hash) == added_ttx_pages.end())
+               {
+                       ePyObject tuple = PyTuple_New(5);
+                       PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(1));
+                       PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(it->pid));
+                       PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(page_number));
+                       PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(magazine_number));
+                       PyTuple_SET_ITEM(tuple, 4, PyString_FromString("und"));  // undetermined
                        PyList_Append(l, tuple);
                        Py_DECREF(tuple);
                }
@@ -2160,7 +2281,7 @@ void eDVBServicePlay::checkSubtitleTiming()
                        }
                } else
                {
-                       eDebug("start subtitle delay %d", diff / 90);
+//                     eDebug("start subtitle delay %d", diff / 90);
                        m_subtitle_sync_timer.start(diff / 90, 1);
                        break;
                }