fix typo, add tsid correction
[enigma2.git] / lib / dvb / pmt.cpp
index bdb10b18adfb41d916f4d654b9d8f73f790c7b1e..e940e58f4c569691c5c9d76263b31a9c738d8e78 100644 (file)
@@ -14,6 +14,7 @@
 #include <dvbsi++/stream_identifier_descriptor.h>
 #include <dvbsi++/subtitling_descriptor.h>
 #include <dvbsi++/teletext_descriptor.h>
+#include <lib/base/nconfig.h> // access to python config
 
 eDVBServicePMTHandler::eDVBServicePMTHandler()
        :m_ca_servicePtr(0), m_dvb_scan(0), m_decode_demux_num(0xFF)
@@ -87,9 +88,9 @@ void eDVBServicePMTHandler::PMTready(int error)
        {
                m_have_cached_program = false;
                serviceEvent(eventNewProgramInfo);
-               eEPGCache::getInstance()->PMTready(this);
                if (!m_pvr_channel) // don't send campmt to camd.socket for playbacked services
                {
+                       eEPGCache::getInstance()->PMTready(this);
                        if(!m_ca_servicePtr)
                        {
                                int demuxes[2] = {0,0};
@@ -141,7 +142,7 @@ void eDVBServicePMTHandler::PATready(int)
 
 PyObject *eDVBServicePMTHandler::getCaIds()
 {
-       PyObject *ret=0;
+       ePyObject ret;
 
        program prog;
 
@@ -157,7 +158,7 @@ PyObject *eDVBServicePMTHandler::getCaIds()
                }
        }
 
-       return ret ? ret : PyList_New(0);
+       return ret ? (PyObject*)ret : (PyObject*)PyList_New(0);
 }
 
 int eDVBServicePMTHandler::getProgramInfo(struct program &program)
@@ -175,11 +176,14 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
        program.pmtPid = -1;
        program.textPid = -1;
 
+       int first_ac3 = -1;
+       program.defaultAudioStream = 0;
+
        if ( m_service && !m_service->cacheEmpty() )
        {
                cached_vpid = m_service->getCacheEntry(eDVBService::cVPID);
-               cached_apid_mpeg = m_service->getCacheEntry(eDVBService::cAC3PID);
-               cached_apid_ac3 = m_service->getCacheEntry(eDVBService::cAPID);
+               cached_apid_mpeg = m_service->getCacheEntry(eDVBService::cAPID);
+               cached_apid_ac3 = m_service->getCacheEntry(eDVBService::cAC3PID);
                cached_tpid = m_service->getCacheEntry(eDVBService::cTPID);
        }
 
@@ -316,14 +320,16 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                                        if (isaudio)
                                        {
                                                audio.pid = (*es)->getPid();
-                                               if ( !program.audioStreams.empty() &&
-                                                       ( audio.pid == cached_apid_ac3 || audio.pid == cached_apid_mpeg) )
-                                               {
-                                                       program.audioStreams.push_back(program.audioStreams[0]);
-                                                       program.audioStreams[0] = audio;
-                                               }
-                                               else
-                                                       program.audioStreams.push_back(audio);
+
+                                                       /* if we find the cached pids, this will be our default stream */
+                                               if (audio.pid == cached_apid_ac3 || audio.pid == cached_apid_mpeg)
+                                                       program.defaultAudioStream = program.audioStreams.size();
+
+                                                       /* also, we need to know the first non-mpeg (i.e. "ac3"/dts/...) stream */
+                                               if ((audio.type != audioStream::atMPEG) && ((first_ac3 == -1) || (audio.pid == cached_apid_ac3)))
+                                                       first_ac3 = program.audioStreams.size();
+
+                                               program.audioStreams.push_back(audio);
                                        }
                                        else if (isvideo)
                                        {
@@ -350,6 +356,21 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                                }
                        }
                        ret = 0;
+
+
+                       /* finally some fixup: if our default audio stream is an MPEG audio stream, 
+                          and we have 'defaultac3' set, use the first available ac3 stream instead.
+                          (note: if an ac3 audio stream was selected before, this will be also stored
+                          in 'fisrt_ac3', so we don't need to worry. */
+                       bool defaultac3 = false;
+                       std::string default_ac3;
+
+                       if (!ePythonConfigQuery::getConfigValue("config.av.defaultac3", default_ac3))
+                               defaultac3 = default_ac3 == "True";
+
+                       if (defaultac3 && (first_ac3 != -1))
+                               program.defaultAudioStream = first_ac3;
+
                        m_cached_program = program;
                        m_have_cached_program = true;
                }
@@ -496,8 +517,9 @@ int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref, int use_decode_demux,
        } else
        {
                eDVBMetaParser parser;
-               
-               if (parser.parseFile(ref.path))
+
+               int ret=parser.parseFile(ref.path);
+               if (ret || !parser.m_ref.getServiceID().get() /* incorrect sid in meta file or recordings.epl*/ )
                {
                        eWarning("no .meta file found, trying to find PMT pid");
                        eDVBTSTools tstools;
@@ -544,7 +566,7 @@ int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref, int use_decode_demux,
                }
        } else
        {
-               serviceEvent(eventTuneFailed);
+               serviceEvent(eventNoResources);
                return res;
        }
 
@@ -856,3 +878,52 @@ void eDVBCAService::sendCAPMT()
                ++m_sendstate;
        }
 }
+
+static PyObject *createTuple(int pid, const char *type)
+{
+       PyObject *r = PyTuple_New(2);
+       PyTuple_SetItem(r, 0, PyInt_FromLong(pid));
+       PyTuple_SetItem(r, 1, PyString_FromString(type));
+       return r;
+}
+
+static inline void PyList_AppendSteal(PyObject *list, PyObject *item)
+{
+       PyList_Append(list, item);
+       Py_DECREF(item);
+}
+
+PyObject *eDVBServicePMTHandler::program::createPythonObject()
+{
+       PyObject *r = PyDict_New();
+
+       PyObject *l = PyList_New(0);
+       
+       PyList_AppendSteal(l, createTuple(0, "pat"));
+
+       if (pmtPid != -1)
+               PyList_AppendSteal(l, createTuple(pmtPid, "pmt"));
+       
+       for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
+                       i(videoStreams.begin()); 
+                       i != videoStreams.end(); ++i)
+               PyList_AppendSteal(l, createTuple(i->pid, "video"));
+
+       for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
+                       i(audioStreams.begin()); 
+                       i != audioStreams.end(); ++i)
+               PyList_AppendSteal(l, createTuple(i->pid, "audio"));
+
+       for (std::vector<eDVBServicePMTHandler::subtitleStream>::const_iterator
+                       i(subtitleStreams.begin());
+                       i != subtitleStreams.end(); ++i)
+               PyList_AppendSteal(l, createTuple(i->pid, "subtitle"));
+
+       PyList_AppendSteal(l, createTuple(pcrPid, "pcr"));
+
+       if (textPid != -1)
+               PyList_AppendSteal(l, createTuple(textPid, "text"));
+               
+       PyDict_SetItemString(r, "pids", l);
+       return r;
+}