update ca, sv language
[enigma2.git] / lib / service / servicedvb.cpp
index c95923c94aca405cb367f62ee6f36ae0a16f4ba5..d5167952f477558d2540700f9b80062bdee33c21 100644 (file)
@@ -3,6 +3,7 @@
 #include <string>
 #include <lib/service/servicedvb.h>
 #include <lib/service/service.h>
+#include <lib/base/estring.h>
 #include <lib/base/init_num.h>
 #include <lib/base/init.h>
 #include <lib/base/nconfig.h> // access to python config
@@ -26,8 +27,6 @@
 #include <byteswap.h>
 #include <netinet/in.h>
 
-#define INTERNAL_TELETEXT
-
 #ifndef BYTE_ORDER
 #error no byte order defined!
 #endif
@@ -211,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 "";
        }
@@ -1157,7 +1158,9 @@ int eDVBServicePlay::getInfo(int w)
        if (w == sCAIDs)
                return resIsPyObject;
 
-       if (m_service_handler.getProgramInfo(program))
+       eDVBServicePMTHandler &h = m_timeshift_active ? m_service_handler_timeshift : m_service_handler;
+       
+       if (h.getProgramInfo(program))
                return -1;
        
        switch (w)
@@ -1246,7 +1249,8 @@ PyObject *eDVBServicePlay::getInfoObject(int w)
 int eDVBServicePlay::getNumberOfTracks()
 {
        eDVBServicePMTHandler::program program;
-       if (m_service_handler.getProgramInfo(program))
+       eDVBServicePMTHandler &h = m_timeshift_active ? m_service_handler_timeshift : m_service_handler;
+       if (h.getProgramInfo(program))
                return 0;
        return program.audioStreams.size();
 }
@@ -1264,8 +1268,9 @@ RESULT eDVBServicePlay::selectTrack(unsigned int i)
 RESULT eDVBServicePlay::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
 {
        eDVBServicePMTHandler::program program;
+       eDVBServicePMTHandler &h = m_timeshift_active ? m_service_handler_timeshift : m_service_handler;
 
-       if (m_service_handler.getProgramInfo(program))
+       if (h.getProgramInfo(program))
                return -1;
        
        if (i >= program.audioStreams.size())
@@ -1302,8 +1307,9 @@ RESULT eDVBServicePlay::getTrackInfo(struct iAudioTrackInfo &info, unsigned int
 int eDVBServicePlay::selectAudioStream(int i)
 {
        eDVBServicePMTHandler::program program;
+       eDVBServicePMTHandler &h = m_timeshift_active ? m_service_handler_timeshift : m_service_handler;
 
-       if (m_service_handler.getProgramInfo(program))
+       if (h.getProgramInfo(program))
                return -1;
        
        if ((unsigned int)i >= program.audioStreams.size())
@@ -1332,6 +1338,8 @@ int eDVBServicePlay::selectAudioStream(int i)
                }
        }
 
+       h.resetCachedProgram();
+
        return 0;
 }
 
@@ -1357,7 +1365,7 @@ std::string eDVBServicePlay::getRadioText(int x)
                switch(x)
                {
                        case 0:
-                               return m_radiotext_parser->getCurrentText();
+                               return convertLatin1UTF8(m_radiotext_parser->getCurrentText());
                }
        return "";
 }
@@ -1588,7 +1596,9 @@ void eDVBServicePlay::updateTimeshiftPids()
                return;
        
        eDVBServicePMTHandler::program program;
-       if (m_service_handler.getProgramInfo(program))
+       eDVBServicePMTHandler &h = m_timeshift_active ? m_service_handler_timeshift : m_service_handler;
+
+       if (h.getProgramInfo(program))
                return;
        else
        {
@@ -1641,16 +1651,16 @@ void eDVBServicePlay::switchToLive()
        m_teletext_parser = 0;
        m_radiotext_parser = 0;
        m_subtitle_parser = 0;
-       m_new_dvb_subtitle_region_connection = 0;
+       m_new_dvb_subtitle_page_connection = 0;
        m_new_subtitle_page_connection = 0;
        m_radiotext_updated_connection = 0;
 
                /* free the timeshift service handler, we need the resources */
        m_service_handler_timeshift.free();
        m_timeshift_active = 0;
-       
+
        m_event((iPlayableService*)this, evSeekableStatusChanged);
-       
+
        updateDecoder();
 }
 
@@ -1658,23 +1668,23 @@ void eDVBServicePlay::switchToTimeshift()
 {
        if (m_timeshift_active)
                return;
-       
+
        m_decode_demux = 0;
        m_decoder = 0;
        m_teletext_parser = 0;
        m_radiotext_parser = 0;
        m_subtitle_parser = 0;
        m_new_subtitle_page_connection = 0;
-       m_new_dvb_subtitle_region_connection = 0;
+       m_new_dvb_subtitle_page_connection = 0;
        m_radiotext_updated_connection = 0;
 
        m_timeshift_active = 1;
 
        m_event((iPlayableService*)this, evSeekableStatusChanged);
-       
+
        eServiceReferenceDVB r = (eServiceReferenceDVB&)m_reference;
        r.path = m_timeshift_file;
-       
+
        m_cue = new eCueSheet();
        m_service_handler_timeshift.tune(r, 1, m_cue); /* use the decoder demux for everything */
        updateDecoder(); /* mainly to switch off PCR */
@@ -1702,7 +1712,7 @@ void eDVBServicePlay::updateDecoder()
                {
                        eDebugNoNewLine(" (");
                        for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
-                               i(program.videoStreams.begin()); 
+                               i(program.videoStreams.begin());
                                i != program.videoStreams.end(); ++i)
                        {
                                if (vpid == -1)
@@ -1721,7 +1731,7 @@ void eDVBServicePlay::updateDecoder()
                {
                        eDebugNoNewLine(" (");
                        for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
-                               i(program.audioStreams.begin()); 
+                               i(program.audioStreams.begin());
                                i != program.audioStreams.end(); ++i)
                        {
                                if (apid == -1 || (apidtype == eDVBAudio::aMPEG && defaultac3))
@@ -1751,12 +1761,10 @@ 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->connectNewRegion(slot(*this, &eDVBServicePlay::newDVBSubtitleRegion), m_new_dvb_subtitle_region_connection);
+               m_subtitle_parser->connectNewPage(slot(*this, &eDVBServicePlay::newDVBSubtitlePage), m_new_dvb_subtitle_page_connection);
        }
 
        if (m_decoder)
@@ -1816,11 +1824,7 @@ void eDVBServicePlay::updateDecoder()
 
                m_decoder->setTextPID(tpid);
 
-               if (m_teletext_parser)
-                       m_teletext_parser->start(tpid);
-
-//             if (m_subtitle_parser && program.subtitleStreams.size() > 0)
-//                     m_subtitle_parser->start(program.subtitleStreams[0].pid);
+               m_teletext_parser->start(program.textPid);
 
                if (!m_is_primary)
                        m_decoder->setTrickmode(1);
@@ -1985,34 +1989,151 @@ void eDVBServicePlay::cutlistToCuesheet()
        m_cue->commitSpans();
 }
 
-RESULT eDVBServicePlay::enableSubtitles(eWidget *parent, PyObject *entry)
+RESULT eDVBServicePlay::enableSubtitles(eWidget *parent, PyObject *tuple)
 {
        if (m_subtitle_widget)
                disableSubtitles(parent);
-       
-       if (!m_teletext_parser)
-               return -1;
-       
+
+       PyObject *entry = 0;
+       int tuplesize = PyTuple_Size(tuple);
+       int type = 0;
+
+       if (!PyTuple_Check(tuple))
+               goto error_out;
+
+       if (tuplesize < 1)
+               goto error_out;
+
+       entry = PyTuple_GET_ITEM(tuple, 0);
+
        if (!PyInt_Check(entry))
-               return -1;
-       
-       m_subtitle_widget = new eSubtitleWidget(parent);
-       m_subtitle_widget->resize(parent->size()); /* full size */
-       
-       int page = PyInt_AsLong(entry);
-       
-       m_teletext_parser->setPage(page);
+               goto error_out;
+
+       type = PyInt_AsLong(entry);
+
+       if (type == 1)  // teletext subtitles
+       {
+               int page, magazine, pid;
+               if (tuplesize < 4)
+                       goto error_out;
+
+               if (!m_teletext_parser)
+               {
+                       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 if (type == 0)
+       {
+               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)
 {
        delete m_subtitle_widget;
        m_subtitle_widget = 0;
+       if (m_subtitle_parser)
+       {
+               m_subtitle_parser->stop();
+               m_dvb_subtitle_pages.clear();
+       }
+       if (m_teletext_parser)
+       {
+               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;
+                       PyObject *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)
@@ -2022,17 +2143,77 @@ PyObject *eDVBServicePlay::getSubtitleList()
        }
        
        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)
+       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;
+       if (h.getProgramInfo(program))
+               eDebug("getting program info failed.");
+       else
        {
-               PyObject *tuple = PyTuple_New(2);
-               char desc[20];
-               sprintf(desc, "Page %x", *i);
-               PyTuple_SetItem(tuple, 0, PyString_FromString(desc));
-               PyTuple_SetItem(tuple, 1, PyInt_FromLong(*i));
-               PyList_Append(l, tuple);
+               for (std::vector<eDVBServicePMTHandler::subtitleStream>::iterator it(program.subtitleStreams.begin());
+                       it != program.subtitleStreams.end(); ++it)
+               {
+                       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())
+                                       {
+                                               PyObject *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
+                               {
+                                       PyObject *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())
+               {
+                       PyObject *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);
+               }
+       }
+
        return l;
 }
 
@@ -2041,26 +2222,43 @@ void eDVBServicePlay::newSubtitlePage(const eDVBTeletextSubtitlePage &page)
        if (m_subtitle_widget)
        {
                m_subtitle_pages.push_back(page);
-               
                checkSubtitleTiming();
        }
 }
 
 void eDVBServicePlay::checkSubtitleTiming()
 {
+//     eDebug("checkSubtitleTiming");
+       if (!m_subtitle_widget)
+               return;
        while (1)
        {
-               if (m_subtitle_pages.empty())
+               enum { TELETEXT, DVB } type;
+               eDVBTeletextSubtitlePage page;
+               eDVBSubtitlePage dvb_page;
+               pts_t show_time;
+               if (!m_subtitle_pages.empty())
+               {
+                       page = m_subtitle_pages.front();
+                       type = TELETEXT;
+                       show_time = page.m_pts;
+               }
+               else if (!m_dvb_subtitle_pages.empty())
+               {
+                       dvb_page = m_dvb_subtitle_pages.front();
+                       type = DVB;
+                       show_time = dvb_page.m_show_time;
+               }
+               else
                        return;
        
-               eDVBTeletextSubtitlePage p = m_subtitle_pages.front();
-       
                pts_t pos = 0;
        
                if (m_decoder)
                        m_decoder->getPTS(0, pos);
-       
-               int diff = p.m_pts - pos;
+
+//             eDebug("%lld %lld", pos, show_time);
+               int diff =  show_time - pos;
                if (diff < 0)
                {
                        eDebug("[late (%d ms)]", -diff / 90);
@@ -2074,24 +2272,34 @@ void eDVBServicePlay::checkSubtitleTiming()
        
                if (!diff)
                {
-                       m_subtitle_widget->setPage(p);
-                       m_subtitle_pages.pop_front();
+                       if (type == TELETEXT)
+                       {
+                               eDebug("display teletext subtitle page");
+                               m_subtitle_widget->setPage(page);
+                               m_subtitle_pages.pop_front();
+                       }
+                       else
+                       {
+                               eDebug("display dvb subtitle Page");
+                               m_subtitle_widget->setPage(dvb_page);
+                               m_dvb_subtitle_pages.pop_front();
+                       }
                } else
                {
+//                     eDebug("start subtitle delay %d", diff / 90);
                        m_subtitle_sync_timer.start(diff / 90, 1);
                        break;
                }
        }
 }
 
-void eDVBServicePlay::newDVBSubtitleRegion(const eDVBSubtitleRegion &p)
+void eDVBServicePlay::newDVBSubtitlePage(const eDVBSubtitlePage &p)
 {
-       eDebug("new dvb subtitle region");
-}
-
-void eDVBServicePlay::checkDvbSubtitleTiming()
-{
-       eDebug("check dvb subtitle timing");
+       if (m_subtitle_widget)
+       {
+               m_dvb_subtitle_pages.push_back(p);
+               checkSubtitleTiming();
+       }
 }
 
 int eDVBServicePlay::getAC3Delay()