load alternatives.xml in autostart plugin
[enigma2.git] / lib / service / servicedvbrecord.cpp
1 #include <lib/service/servicedvbrecord.h>
2 #include <lib/base/eerror.h>
3 #include <lib/dvb/epgcache.h>
4
5 #include <fcntl.h>
6
7 DEFINE_REF(eDVBServiceRecord);
8
9 eDVBServiceRecord::eDVBServiceRecord(const eServiceReferenceDVB &ref): m_ref(ref)
10 {
11         CONNECT(m_service_handler.serviceEvent, eDVBServiceRecord::serviceEvent);
12         m_state = stateIdle;
13         m_want_record = 0;
14         m_tuned = 0;
15 }
16
17 void eDVBServiceRecord::serviceEvent(int event)
18 {
19         eDebug("RECORD service event %d", event);
20         switch (event)
21         {
22         case eDVBServicePMTHandler::eventTuned:
23         {
24                 eDebug("tuned..");
25                 m_tuned = 1;
26                 if (m_state == stateRecording && m_want_record)
27                         doRecord();
28                 break;
29         }
30         case eDVBServicePMTHandler::eventNewProgramInfo:
31         {
32                 if (m_state == stateIdle)
33                         doPrepare();
34                 else if (m_want_record) /* doRecord can be called from Prepared and Recording state */
35                         doRecord();
36                 break;
37         }
38         }
39 }
40
41 RESULT eDVBServiceRecord::prepare(const char *filename, time_t begTime, time_t endTime, int eit_event_id)
42 {
43         m_filename = filename;
44         if (m_state == stateIdle)
45         {
46                 int ret = doPrepare();
47                 if (!ret)
48                 {
49                         eEPGCache::getInstance()->Lock();
50                         const eit_event_struct *event = 0;
51                         if ( eit_event_id != -1 )
52                         {
53                                 eDebug("query epg event id %d", eit_event_id);
54                                 eEPGCache::getInstance()->lookupEventId(m_ref, eit_event_id, event);
55                         }
56                         if ( !event && (begTime != -1 && endTime != -1) )
57                         {
58                                 time_t queryTime = begTime + ((endTime-begTime)/2);
59                                 tm beg, end, query;
60                                 localtime_r(&begTime, &beg);
61                                 localtime_r(&endTime, &end);
62                                 localtime_r(&queryTime, &query);
63                                 eDebug("query stime %d:%d:%d, etime %d:%d:%d, qtime %d:%d:%d",
64                                         beg.tm_hour, beg.tm_min, beg.tm_sec,
65                                         end.tm_hour, end.tm_min, end.tm_sec,
66                                         query.tm_hour, query.tm_min, query.tm_sec);
67                                 eEPGCache::getInstance()->lookupEventTime(m_ref, queryTime, event);
68                         }
69                         if ( event )
70                         {
71                                 eDebug("found event.. store to disc");
72                                 std::string fname = filename;
73                                 fname.erase(fname.length()-2, 2);
74                                 fname+="eit";
75                                 int fd = open(fname.c_str(), O_CREAT|O_WRONLY, 0777);
76                                 if (fd>-1)
77                                 {
78                                         int evLen=HILO(event->descriptors_loop_length)+12/*EIT_LOOP_SIZE*/;
79                                         int wr = ::write( fd, (unsigned char*)event, evLen );
80                                         if ( wr != evLen )
81                                                 eDebug("eit write error (%m)");
82                                         ::close(fd);
83                                 }
84                         }
85                         eEPGCache::getInstance()->Unlock();
86                 }
87                 return ret;
88         }
89         else
90                 return -1;
91 }
92
93 RESULT eDVBServiceRecord::start()
94 {
95         m_want_record = 1;
96                 /* when tune wasn't yet successfully, doRecord stays in "prepared"-state which is fine. */
97         return doRecord();
98 }
99
100
101 RESULT eDVBServiceRecord::stop()
102 {
103         eDebug("stop recording!!");
104         if (m_state == stateRecording)
105         {
106                 if (m_record)
107                         m_record->stop();
108                 m_state = statePrepared;
109         }
110         
111         if (m_state == statePrepared)
112         {
113                 m_record = 0;
114                 m_state = stateIdle;
115         }
116         return 0;
117 }
118
119
120 int eDVBServiceRecord::doPrepare()
121 {
122                 /* allocate a ts recorder if we don't already have one. */
123         if (m_state == stateIdle)
124         {
125                 m_pids_active.clear();
126                 m_state = statePrepared;
127                 return m_service_handler.tune(m_ref, 0);
128         }
129         return 0;
130 }
131
132 int eDVBServiceRecord::doRecord()
133 {
134         int err = doPrepare();
135         if (err)
136                 return err;
137         
138         if (!m_tuned)
139                 return 0; /* try it again when we are tuned in */
140         
141         if (!m_record && m_tuned)
142         {
143
144                 eDebug("Recording to %s...", m_filename.c_str());
145                 ::remove(m_filename.c_str());
146                 int fd = ::open(m_filename.c_str(), O_WRONLY|O_CREAT|O_LARGEFILE, 0644);
147                 if (fd == -1)
148                 {
149                         eDebug("eDVBServiceRecord - can't open recording file!");
150                         return -1;
151                 }
152                 
153                         /* turn off kernel caching strategies */
154                 posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
155                 
156                 ePtr<iDVBDemux> demux;
157                 if (m_service_handler.getDataDemux(demux))
158                 {
159                         eDebug("eDVBServiceRecord - NO DEMUX available!");
160                         return -2;
161                 }
162                 demux->createTSRecorder(m_record);
163                 if (!m_record)
164                 {
165                         eDebug("eDVBServiceRecord - no ts recorder available.");
166                         return -3;
167                 }
168                 m_record->setTargetFD(fd);
169                 m_record->setTargetFilename(m_filename.c_str());
170         }
171         eDebug("starting recording..");
172         
173         eDVBServicePMTHandler::program program;
174         if (m_service_handler.getProgramInfo(program))
175                 eDebug("getting program info failed.");
176         else
177         {
178                 std::set<int> pids_to_record;
179                 
180                 pids_to_record.insert(0); // PAT
181                 
182                 if (program.pmtPid != -1)
183                         pids_to_record.insert(program.pmtPid); // PMT
184                 
185                 int timing_pid = -1;
186                 
187                 eDebugNoNewLine("RECORD: have %d video stream(s)", program.videoStreams.size());
188                 if (!program.videoStreams.empty())
189                 {
190                         eDebugNoNewLine(" (");
191                         for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
192                                 i(program.videoStreams.begin()); 
193                                 i != program.videoStreams.end(); ++i)
194                         {
195                                 pids_to_record.insert(i->pid);
196                                 
197                                 if (timing_pid == -1)
198                                         timing_pid = i->pid;
199                                 
200                                 if (i != program.videoStreams.begin())
201                                         eDebugNoNewLine(", ");
202                                 eDebugNoNewLine("%04x", i->pid);
203                         }
204                         eDebugNoNewLine(")");
205                 }
206                 eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
207                 if (!program.audioStreams.empty())
208                 {
209                         eDebugNoNewLine(" (");
210                         for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
211                                 i(program.audioStreams.begin()); 
212                                 i != program.audioStreams.end(); ++i)
213                         {
214                                 pids_to_record.insert(i->pid);
215
216                                 if (timing_pid == -1)
217                                         timing_pid = i->pid;
218                                 
219                                 if (i != program.audioStreams.begin())
220                                         eDebugNoNewLine(", ");
221                                 eDebugNoNewLine("%04x", i->pid);
222                         }
223                         eDebugNoNewLine(")");
224                 }
225                 eDebugNoNewLine(", and the pcr pid is %04x", program.pcrPid);
226                 if (program.pcrPid != 0x1fff)
227                         pids_to_record.insert(program.pcrPid);
228                 eDebug(", and the text pid is %04x", program.textPid);
229                 if (program.textPid != -1)
230                         pids_to_record.insert(program.textPid); // Videotext
231
232                         /* find out which pids are NEW and which pids are obsolete.. */
233                 std::set<int> new_pids, obsolete_pids;
234                 
235                 std::set_difference(pids_to_record.begin(), pids_to_record.end(), 
236                                 m_pids_active.begin(), m_pids_active.end(),
237                                 std::inserter(new_pids, new_pids.begin()));
238                 
239                 std::set_difference(
240                                 m_pids_active.begin(), m_pids_active.end(),
241                                 pids_to_record.begin(), pids_to_record.end(), 
242                                 std::inserter(new_pids, new_pids.begin())
243                                 );
244                 
245                 for (std::set<int>::iterator i(new_pids.begin()); i != new_pids.end(); ++i)
246                 {
247                         eDebug("ADD PID: %04x", *i);
248                         m_record->addPID(*i);
249                 }
250                 for (std::set<int>::iterator i(obsolete_pids.begin()); i != obsolete_pids.end(); ++i)
251                 {
252                         eDebug("REMOVED PID: %04x", *i);
253                         m_record->removePID(*i);
254                 }
255                 
256                 if (timing_pid != -1)
257                         m_record->setTimingPID(timing_pid);
258                 
259                 m_pids_active = pids_to_record;
260                 
261                 if (m_state != stateRecording)
262                 {
263                         m_record->start();
264                         m_state = stateRecording;
265                 }
266         }
267         return 0;
268 }