Merge commit 'e8eb8e9694379204' into experimental
[enigma2.git] / lib / service / servicedvbrecord.cpp
index c2767e8d42b0b29cd8435365b9791535a3ccf1bf..419c26bab2149f2ac60fcd26a9841af88d56437f 100644 (file)
@@ -1,6 +1,7 @@
 #include <lib/service/servicedvbrecord.h>
 #include <lib/base/eerror.h>
 #include <lib/dvb/epgcache.h>
+#include <lib/dvb/metaparser.h>
 #include <fcntl.h>
 
        /* for cutlist */
@@ -83,7 +84,7 @@ void eDVBServiceRecord::serviceEvent(int event)
        }
 }
 
-RESULT eDVBServiceRecord::prepare(const char *filename, time_t begTime, time_t endTime, int eit_event_id)
+RESULT eDVBServiceRecord::prepare(const char *filename, time_t begTime, time_t endTime, int eit_event_id, const char *name, const char *descr, const char *tags)
 {
        m_filename = filename;
        m_streaming = 0;
@@ -93,51 +94,91 @@ RESULT eDVBServiceRecord::prepare(const char *filename, time_t begTime, time_t e
                int ret = doPrepare();
                if (!ret)
                {
-                       eEPGCache::getInstance()->Lock();
-                       const eit_event_struct *event = 0;
                        eServiceReferenceDVB ref = m_ref.getParentServiceReference();
+                       ePtr<eDVBResourceManager> res_mgr;
+                       eDVBMetaParser meta;
+                       std::string service_data;
                        if (!ref.valid())
                                ref = m_ref;
-                       if ( eit_event_id != -1 )
+                       if (!eDVBResourceManager::getInstance(res_mgr))
                        {
-                               eDebug("query epg event id %d", eit_event_id);
-                               eEPGCache::getInstance()->lookupEventId(ref, eit_event_id, event);
-                       }
-                       if ( !event && (begTime != -1 && endTime != -1) )
-                       {
-                               time_t queryTime = begTime + ((endTime-begTime)/2);
-                               tm beg, end, query;
-                               localtime_r(&begTime, &beg);
-                               localtime_r(&endTime, &end);
-                               localtime_r(&queryTime, &query);
-                               eDebug("query stime %d:%d:%d, etime %d:%d:%d, qtime %d:%d:%d",
-                                       beg.tm_hour, beg.tm_min, beg.tm_sec,
-                                       end.tm_hour, end.tm_min, end.tm_sec,
-                                       query.tm_hour, query.tm_min, query.tm_sec);
-                               eEPGCache::getInstance()->lookupEventTime(ref, queryTime, event);
+                               ePtr<iDVBChannelList> db;
+                               if (!res_mgr->getChannelList(db))
+                               {
+                                       ePtr<eDVBService> service;
+                                       if (!db->getService(ref, service))
+                                       {
+                                               char tmp[255];
+                                               sprintf(tmp, "f:%x", service->m_flags);
+                                               service_data += tmp;
+                                               // cached pids
+                                               for (int x=0; x < eDVBService::cacheMax; ++x)
+                                               {
+                                                       int entry = service->getCacheEntry((eDVBService::cacheID)x);
+                                                       if (entry != -1)
+                                                       {
+                                                               sprintf(tmp, ",c:%02d%04x", x, entry);
+                                                               service_data += tmp;
+                                                       }
+                                               }
+                                       }
+                               }
                        }
-                       if ( event )
+                       meta.m_time_create = begTime;
+                       meta.m_ref = m_ref;
+                       meta.m_data_ok = 1;
+                       meta.m_service_data = service_data;
+                       if (name)
+                               meta.m_name = name;
+                       if (descr)
+                               meta.m_description = descr;
+                       if (tags)
+                               meta.m_tags = tags;
+                       ret = meta.updateMeta(filename) ? -255 : 0;
+                       if (!ret)
                        {
-                               eDebug("found event.. store to disc");
-                               std::string fname = filename;
-                               fname.erase(fname.length()-2, 2);
-                               fname+="eit";
-                               int fd = open(fname.c_str(), O_CREAT|O_WRONLY, 0777);
-                               if (fd>-1)
+                               const eit_event_struct *event = 0;
+                               eEPGCache::getInstance()->Lock();
+                               if ( eit_event_id != -1 )
                                {
-                                       int evLen=HILO(event->descriptors_loop_length)+12/*EIT_LOOP_SIZE*/;
-                                       int wr = ::write( fd, (unsigned char*)event, evLen );
-                                       if ( wr != evLen )
-                                               eDebug("eit write error (%m)");
-                                       ::close(fd);
+                                       eDebug("query epg event id %d", eit_event_id);
+                                       eEPGCache::getInstance()->lookupEventId(ref, eit_event_id, event);
                                }
+                               if ( !event && (begTime != -1 && endTime != -1) )
+                               {
+                                       time_t queryTime = begTime + ((endTime-begTime)/2);
+                                       tm beg, end, query;
+                                       localtime_r(&begTime, &beg);
+                                       localtime_r(&endTime, &end);
+                                       localtime_r(&queryTime, &query);
+                                       eDebug("query stime %d:%d:%d, etime %d:%d:%d, qtime %d:%d:%d",
+                                               beg.tm_hour, beg.tm_min, beg.tm_sec,
+                                               end.tm_hour, end.tm_min, end.tm_sec,
+                                               query.tm_hour, query.tm_min, query.tm_sec);
+                                       eEPGCache::getInstance()->lookupEventTime(ref, queryTime, event);
+                               }
+                               if ( event )
+                               {
+                                       eDebug("found event.. store to disc");
+                                       std::string fname = filename;
+                                       fname.erase(fname.length()-2, 2);
+                                       fname+="eit";
+                                       int fd = open(fname.c_str(), O_CREAT|O_WRONLY, 0777);
+                                       if (fd>-1)
+                                       {
+                                               int evLen=HILO(event->descriptors_loop_length)+12/*EIT_LOOP_SIZE*/;
+                                               int wr = ::write( fd, (unsigned char*)event, evLen );
+                                               if ( wr != evLen )
+                                                       eDebug("eit write error (%m)");
+                                               ::close(fd);
+                                       }
+                               }
+                               eEPGCache::getInstance()->Unlock();
                        }
-                       eEPGCache::getInstance()->Unlock();
                }
                return ret;
        }
-       else
-               return -1;
+       return -1;
 }
 
 RESULT eDVBServiceRecord::prepareStreaming()
@@ -270,7 +311,7 @@ int eDVBServiceRecord::doRecord()
                        if (program.pmtPid != -1)
                                pids_to_record.insert(program.pmtPid); // PMT
 
-                       int timing_pid = -1;
+                       int timing_pid = -1, timing_pid_type = -1;
 
                        eDebugNoNewLine("RECORD: have %d video stream(s)", program.videoStreams.size());
                        if (!program.videoStreams.empty())
@@ -283,7 +324,10 @@ int eDVBServiceRecord::doRecord()
                                        pids_to_record.insert(i->pid);
                                        
                                        if (timing_pid == -1)
+                                       {
                                                timing_pid = i->pid;
+                                               timing_pid_type = i->type;
+                                       }
                                        
                                        if (i != program.videoStreams.begin())
                                                        eDebugNoNewLine(", ");
@@ -302,7 +346,10 @@ int eDVBServiceRecord::doRecord()
                                        pids_to_record.insert(i->pid);
        
                                        if (timing_pid == -1)
+                                       {
                                                timing_pid = i->pid;
+                                               timing_pid_type = -1;
+                                       }
                                
                                        if (i != program.audioStreams.begin())
                                                eDebugNoNewLine(", ");
@@ -342,7 +389,7 @@ int eDVBServiceRecord::doRecord()
                        std::set_difference(
                                        m_pids_active.begin(), m_pids_active.end(),
                                        pids_to_record.begin(), pids_to_record.end(), 
-                                       std::inserter(new_pids, new_pids.begin())
+                                       std::inserter(obsolete_pids, obsolete_pids.begin())
                                        );
                        
                        for (std::set<int>::iterator i(new_pids.begin()); i != new_pids.end(); ++i)
@@ -358,7 +405,7 @@ int eDVBServiceRecord::doRecord()
                        }
 
                        if (timing_pid != -1)
-                               m_record->setTimingPID(timing_pid);
+                               m_record->setTimingPID(timing_pid, timing_pid_type);
 
                        m_pids_active = pids_to_record;
 
@@ -446,8 +493,7 @@ void eDVBServiceRecord::gotNewEvent()
                        eDebug("getting PCR failed!");
                else
                {
-                       static int i;
-                       m_event_timestamps[/* event_id*/ ++i] = p;
+                       m_event_timestamps[event_id] = p;
                        eDebug("pcr of eit change: %llx", p);
                }
        }
@@ -456,6 +502,8 @@ void eDVBServiceRecord::gotNewEvent()
                eDebug("[eDVBServiceRecord] now running: %s (%d seconds)", event_now->getEventName().c_str(), event_now->getDuration());
        
        m_last_event_id = event_id;
+
+       m_event((iRecordableService*)this, evNewEventInfo);
 }
 
 void eDVBServiceRecord::saveCutlist()
@@ -501,3 +549,29 @@ void eDVBServiceRecord::saveCutlist()
        }
        
 }
+
+RESULT eDVBServiceRecord::subServices(ePtr<iSubserviceList> &ptr)
+{
+       ptr = this;
+       return 0;
+}
+
+int eDVBServiceRecord::getNumberOfSubservices()
+{
+       ePtr<eServiceEvent> evt;
+       if (!m_event_handler.getEvent(evt, 0))
+               return evt->getNumOfLinkageServices();
+       return 0;
+}
+
+RESULT eDVBServiceRecord::getSubservice(eServiceReference &sub, unsigned int n)
+{
+       ePtr<eServiceEvent> evt;
+       if (!m_event_handler.getEvent(evt, 0))
+       {
+               if (!evt->getLinkageService(sub, m_ref, n))
+                       return 0;
+       }
+       sub.type=eServiceReference::idInvalid;
+       return -1;
+}